/[cmucl]/src/code/fd-stream.lisp
ViewVC logotype

Contents of /src/code/fd-stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.117 - (show annotations)
Sun Aug 15 13:03:43 2010 UTC (3 years, 8 months ago) by rtoy
Branch: MAIN
Changes since 1.116: +9 -2 lines
When decoding-error is T, use a question mark for non-unicode builds
instead of the (invalid) Unicode replacement character.
1 ;;; -*- Log: code.log; Package: LISP -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/fd-stream.lisp,v 1.117 2010/08/15 13:03:43 rtoy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Streams for UNIX file descriptors.
13 ;;;
14 ;;; Written by William Lott, July 1989 - January 1990.
15 ;;; Some tuning by Rob MacLachlan.
16 ;;;
17 ;;; **********************************************************************
18
19
20 (in-package "SYSTEM")
21
22 (intl:textdomain "cmucl")
23
24 (export '(fd-stream fd-stream-p fd-stream-fd make-fd-stream
25 io-timeout beep *beep-function* output-raw-bytes
26 *tty* *stdin* *stdout* *stderr*
27 binary-text-stream))
28
29
30 (in-package "EXTENSIONS")
31
32 (export '(*backup-extension*))
33
34
35 (in-package "LISP")
36
37 (export '(file-stream file-string-length))
38
39
40 ;;;; Buffer manipulation routines.
41
42 (defvar *available-buffers* ()
43 "List of available buffers. Each buffer is an sap pointing to
44 bytes-per-buffer of memory.")
45
46 (defvar lisp::*enable-stream-buffer-p* nil)
47
48 (defconstant bytes-per-buffer (* 4 1024)
49 "Number of bytes per buffer.")
50
51 ;; This limit is rather arbitrary
52 (defconstant max-stream-element-size 1024
53 "The maximum supported byte size for a stream element-type.")
54
55 ;;; NEXT-AVAILABLE-BUFFER -- Internal.
56 ;;;
57 ;;; Returns the next available buffer, creating one if necessary.
58 ;;;
59 (declaim (inline next-available-buffer))
60 ;;;
61 (defun next-available-buffer ()
62 (if *available-buffers*
63 (pop *available-buffers*)
64 (allocate-system-memory bytes-per-buffer)))
65
66 (declaim (inline buffer-sap bref (setf bref) buffer-copy))
67
68 (defun buffer-sap (thing &optional offset)
69 (declare (type simple-stream-buffer thing) (type (or fixnum null) offset)
70 (optimize (speed 3) (space 2) (debug 0) (safety 0)
71 ;; Suppress the note about having to box up the return:
72 (ext:inhibit-warnings 3)))
73 (let ((sap (if (vectorp thing) (sys:vector-sap thing) thing)))
74 (if offset (sys:sap+ sap offset) sap)))
75
76 (defun bref (buffer index)
77 (declare (type simple-stream-buffer buffer)
78 (type (integer 0 #.most-positive-fixnum) index))
79 (sys:sap-ref-8 (buffer-sap buffer) index))
80
81 (defun (setf bref) (octet buffer index)
82 (declare (type (unsigned-byte 8) octet)
83 (type simple-stream-buffer buffer)
84 (type (integer 0 #.most-positive-fixnum) index))
85 (setf (sys:sap-ref-8 (buffer-sap buffer) index) octet))
86
87 (defun buffer-copy (src soff dst doff length)
88 (declare (type simple-stream-buffer src dst)
89 (type fixnum soff doff length))
90 (sys:without-gcing ;; is this necessary??
91 (kernel:system-area-copy (buffer-sap src) (* soff 8)
92 (buffer-sap dst) (* doff 8)
93 (* length 8))))
94
95 #-(or big-endian little-endian)
96 (eval-when (:compile-toplevel)
97 (push (c::backend-byte-order c::*target-backend*) *features*))
98
99 (defun vector-elt-width (vector)
100 ;; Return octet-width of vector elements
101 (etypecase vector
102 ;; (simple-array fixnum (*)) not supported
103 ;; (simple-array base-char (*)) treated specially; don't call this
104 ((simple-array bit (*)) 1/8)
105 ((simple-array (unsigned-byte 2) (*)) 1/4)
106 ((simple-array (unsigned-byte 4) (*)) 1/2)
107 ((simple-array (signed-byte 8) (*)) 1)
108 ((simple-array (unsigned-byte 8) (*)) 1)
109 ((simple-array (signed-byte 16) (*)) 2)
110 ((simple-array (unsigned-byte 16) (*)) 2)
111 ((simple-array (signed-byte 32) (*)) 4)
112 ((simple-array (unsigned-byte 32) (*)) 4)
113 ((simple-array single-float (*)) 4)
114 ((simple-array double-float (*)) 8)
115 ((simple-array (complex single-float) (*)) 8)
116 ((simple-array (complex double-float) (*)) 16)
117 #+long-float
118 ((simple-array long-float (*)) 10)
119 #+long-float
120 ((simple-array (complex long-float) (*)) 20)
121 #+double-double
122 ((simple-array double-double-float (*)) 16)
123 #+double-double
124 ((simple-array (complex double-double-float) (*)) 32)))
125
126 (defun endian-swap-value (vector endian-swap)
127 (case endian-swap
128 (:network-order
129 #+big-endian 0
130 ;; This is needed because the little-endian (x86) architectures
131 ;; store the lowest indexed element in the least significant part
132 ;; of a byte. On a big-endian machine (sparc, ppc), the lowest
133 ;; indexed element is at the most significant part of a byte.
134 #+little-endian
135 (typecase vector
136 ((array (unsigned-byte 4) (*))
137 -1)
138 ((array (unsigned-byte 2) (*))
139 -2)
140 ((array (unsigned-byte 1) (*))
141 -8)
142 (t
143 (1- (vector-elt-width vector)))))
144 (:byte-8 0)
145 (:byte-16 1)
146 (:byte-32 3)
147 (:byte-64 7)
148 (:byte-128 15)
149 ;; additions by Lynn Quam
150 (:machine-endian 0)
151 (:big-endian
152 #+big-endian 0
153 #+little-endian
154 (typecase vector
155 ((array (unsigned-byte 4) (*))
156 -1)
157 ((array (unsigned-byte 2) (*))
158 -2)
159 ((array (unsigned-byte 1) (*))
160 -8)
161 (t
162 (1- (vector-elt-width vector)))))
163 (:little-endian
164 #+big-endian
165 (typecase vector
166 ((array (unsigned-byte 4) (*))
167 -1)
168 ((array (unsigned-byte 2) (*))
169 -2)
170 ((array (unsigned-byte 1) (*))
171 -8)
172 (t
173 (1- (vector-elt-width vector))))
174 #+little-endian 0)
175 (otherwise endian-swap)))
176
177
178 ;;;; The FD-STREAM structure.
179
180 ;;;; Superclass defined by the ANSI Spec
181 (defstruct (file-stream
182 (:include lisp-stream)
183 (:constructor nil)
184 (:copier nil)))
185
186 (defstruct (fd-stream
187 (:print-function %print-fd-stream)
188 (:constructor %make-fd-stream)
189 (:include file-stream
190 (misc #'fd-stream-misc-routine)))
191
192 (name nil) ; The name of this stream
193 (file nil) ; The file this stream is for
194 ;;
195 ;; The backup file namestring for the old file, for :if-exists :rename or
196 ;; :rename-and-delete.
197 (original nil :type (or simple-string null))
198 (delete-original nil) ; for :if-exists :rename-and-delete
199 ;;
200 ;;; Number of bytes per element.
201 (element-size 1 :type index)
202 (element-type 'base-char) ; The type of element being transfered.
203 (fd -1 :type fixnum) ; The file descriptor
204 ;;
205 ;; Controls when the output buffer is flushed.
206 (buffering :full :type (member :full :line :none))
207 ;;
208 ;; Character position if known.
209 (char-pos nil :type (or index null))
210 ;;
211 ;; T if input is waiting on FD. :EOF if we hit EOF.
212 (listen nil :type (member nil t :eof))
213 ;;
214 ;; The input buffer.
215 (unread nil)
216 (ibuf-sap nil :type (or system-area-pointer null))
217 (ibuf-length nil :type (or index null))
218 (ibuf-head 0 :type index)
219 (ibuf-tail 0 :type index)
220
221 ;; The output buffer.
222 (obuf-sap nil :type (or system-area-pointer null))
223 (obuf-length nil :type (or index null))
224 (obuf-tail 0 :type index)
225
226 ;; Output flushed, but not written due to non-blocking io.
227 (output-later nil)
228 (handler nil)
229 ;;
230 ;; Timeout specified for this stream, or NIL if none.
231 (timeout nil :type (or index null))
232 ;;
233 ;; Pathname of the file this stream is opened to (returned by PATHNAME.)
234 (pathname nil :type (or pathname null))
235 ;;
236 ;; External format support
237 ;;
238 ;; @@ I want to use :default here, but keyword pkg isn't set up yet at boot
239 ;; so initialize to NIL and fix it in SET-ROUTINES
240 #+unicode
241 (external-format nil :type (or null keyword cons))
242 ;;
243 ;; State for octets-to-char (for reading from a stream). The
244 ;; contents of the state can be anything and is defined by the
245 ;; external format.
246 #+unicode
247 (oc-state nil)
248 ;;
249 ;; State for char-to-octets (for writing to a stream). The contents
250 ;; of the state can be anything and is defined by the external
251 ;; format. If not NIL, then the CAR is used by char-to-octets to
252 ;; hold some state information, and the CDR is available to the
253 ;; external format to hold whatever state information is needed.
254 #+unicode
255 (co-state nil)
256 #+unicode
257 (last-char-read-size 0 :type index)
258 ;; Saved state needed for (setf stream-external-format) when the
259 ;; fast string-buffer is used.
260 #+unicode
261 (saved-oc-state nil)
262 ;;
263 ;; The number of octets in in-buffer. Normally equal to
264 ;; in-buffer-length, but could be less if we reached the
265 ;; end-of-file.
266 #+unicode
267 (in-length 0 :type index)
268 ;;
269 ;; Indicates how to handle errors when converting octets to
270 ;; characters. If NIL, then the external format should handle it
271 ;; itself, doing whatever is deemed appropriate. If non-NIL, this
272 ;; should be a function (or symbol) that the external format can
273 ;; funcall to deal with the error. The function should take three
274 ;; arguments: a message string, the offending octet, and the number
275 ;; of octets read so far in decoding; if the function returns it
276 ;; should return the codepoint of the desired replacement character.
277 (octets-to-char-error nil :type (or null symbol function))
278 ;;
279 ;; Like OCTETS-TO-CHAR-ERROR, but for converting characters to
280 ;; octets for output. The function takes two arguments: a message
281 ;; string and the codepoint that cannot be converted. The function
282 ;; should return the octet that should be output.
283 (char-to-octets-error nil :type (or null symbol function)))
284
285 (defun %print-fd-stream (fd-stream stream depth)
286 (declare (ignore depth) (stream stream))
287 (format stream "#<Stream for ~A>"
288 (fd-stream-name fd-stream)))
289
290 ;; CMUCL extension. This is a FD-STREAM, but it allows reading and
291 ;; writing of 8-bit characters and unsigned bytes from the stream.
292 (defstruct (binary-text-stream
293 (:print-function %print-binary-text-stream)
294 (:constructor %make-binary-text-stream)
295 (:include fd-stream)))
296
297 (defun %print-binary-text-stream (fd-stream stream depth)
298 (declare (ignore depth) (stream stream))
299 (format stream "#<Binary-text Stream for ~A>"
300 (fd-stream-name fd-stream)))
301
302 (define-condition io-timeout (stream-error)
303 ((direction :reader io-timeout-direction :initarg :direction))
304 (:report
305 (lambda (condition stream)
306 (declare (stream stream))
307 (format stream (intl:gettext "Timeout ~(~A~)ing ~S.")
308 (io-timeout-direction condition)
309 (stream-error-stream condition)))))
310
311
312 ;;;; Output routines and related noise.
313
314 (defvar *output-routines* ()
315 "List of all available output routines. Each element is a list of the
316 element-type output, the kind of buffering, the function name, and the number
317 of bytes per element.")
318
319 ;;; DO-OUTPUT-LATER -- internal
320 ;;;
321 ;;; Called by the server when we can write to the given file descriptor.
322 ;;; Attempt to write the data again. If it worked, remove the data from the
323 ;;; output-later list. If it didn't work, something is wrong.
324 ;;;
325 (defun do-output-later (stream)
326 (let* ((stuff (pop (fd-stream-output-later stream)))
327 (base (car stuff))
328 (start (cadr stuff))
329 (end (caddr stuff))
330 (reuse-sap (cadddr stuff))
331 (length (- end start)))
332 (declare (type index start end length))
333 (multiple-value-bind
334 (count errno)
335 (unix:unix-write (fd-stream-fd stream)
336 base
337 start
338 length)
339 (cond ((not count)
340 (if (= errno unix:ewouldblock)
341 (error (intl:gettext "Write would have blocked, but SERVER told us to go."))
342 (error (intl:gettext "While writing ~S: ~A")
343 stream (unix:get-unix-error-msg errno))))
344 ((eql count length) ; Hot damn, it worked.
345 (when reuse-sap
346 (push base *available-buffers*)))
347 ((not (null count)) ; Sorta worked.
348 (push (list base
349 (the index (+ start count))
350 end)
351 (fd-stream-output-later stream))))))
352 (unless (fd-stream-output-later stream)
353 (system:remove-fd-handler (fd-stream-handler stream))
354 (setf (fd-stream-handler stream) nil)))
355
356 ;;; OUTPUT-LATER -- internal
357 ;;;
358 ;;; Arrange to output the string when we can write on the file descriptor.
359 ;;;
360 (defun output-later (stream base start end reuse-sap)
361 (cond ((null (fd-stream-output-later stream))
362 (setf (fd-stream-output-later stream)
363 (list (list base start end reuse-sap)))
364 (setf (fd-stream-handler stream)
365 (system:add-fd-handler (fd-stream-fd stream)
366 :output
367 #'(lambda (fd)
368 (declare (ignore fd))
369 (do-output-later stream)))))
370 (t
371 (nconc (fd-stream-output-later stream)
372 (list (list base start end reuse-sap)))))
373 (when reuse-sap
374 (let ((new-buffer (next-available-buffer)))
375 (setf (fd-stream-obuf-sap stream) new-buffer)
376 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
377
378 ;;; DO-OUTPUT -- internal
379 ;;;
380 ;;; Output the given noise. Check to see if there are any pending writes. If
381 ;;; so, just queue this one. Otherwise, try to write it. If this would block,
382 ;;; queue it.
383 ;;;
384 (defun do-output (stream base start end reuse-sap)
385 (declare (type fd-stream stream)
386 (type (or system-area-pointer (simple-array * (*))) base)
387 (type index start end))
388 (if (not (null (fd-stream-output-later stream))) ; something buffered.
389 (progn
390 (output-later stream base start end reuse-sap)
391 ;; ### check to see if any of this noise can be output
392 )
393 (let ((length (- end start)))
394 (multiple-value-bind
395 (count errno)
396 (unix:unix-write (fd-stream-fd stream) base start length)
397 (cond ((not count)
398 (if (= errno unix:ewouldblock)
399 (output-later stream base start end reuse-sap)
400 (error 'simple-stream-error
401 :stream stream
402 :format-control "while writing: ~A"
403 :format-arguments (list (unix:get-unix-error-msg errno)))))
404 ((not (eql count length))
405 (output-later stream base (the index (+ start count))
406 end reuse-sap)))))))
407
408 #+unicode
409 (stream::def-ef-macro ef-flush (extfmt lisp stream::+ef-max+ stream::+ef-flush+)
410 `(lambda (stream)
411 (declare (type fd-stream stream))
412 (let* ((tail (fd-stream-obuf-tail stream)))
413 (declare (type index tail))
414 (cond
415 ((stream::ef-flush-state ,(stream::find-external-format extfmt))
416 (let* ((sap (fd-stream-obuf-sap stream))
417 (len (fd-stream-obuf-length stream)))
418 (declare (type sys:system-area-pointer sap)
419 (type index len)
420 (ignorable sap len))
421 (stream::flush-state ,extfmt
422 (fd-stream-co-state stream)
423 (lambda (byte)
424 (when (= tail len)
425 (do-output stream sap 0 tail t)
426 (setq sap (fd-stream-obuf-sap stream)
427 tail 0))
428 (setf (bref sap (1- (incf tail))) byte))
429 (fd-stream-char-to-octets-error stream))
430 (setf (fd-stream-obuf-tail stream) tail)))
431 (t
432 ;; No flush-state function, so just output a replacement
433 ;; character (or signal an error). We hack the co-state to
434 ;; what we need for this to work. This should be ok because
435 ;; we're closing the file anyway.
436 (let* ((state (fd-stream-co-state stream))
437 (c (car state)))
438 (when (and state c)
439 (setf (fd-stream-co-state stream)
440 (cons nil (cdr state)))
441 (funcall (ef-cout (fd-stream-external-format stream))
442 stream
443 ;; Handle bare surrogates or use the
444 ;; replacement character.
445 (if (lisp::surrogatep c)
446 (if (fd-stream-char-to-octets-error stream)
447 (funcall (fd-stream-char-to-octets-error stream)
448 "Flushing bare surrogate #x~4,0X is illegal"
449 (char-code c))
450 (code-char stream:+replacement-character-code+))
451 c))))))
452 (values))))
453
454 ;;; FLUSH-OUTPUT-BUFFER -- internal
455 ;;;
456 ;;; Flush any data in the output buffer.
457 ;;;
458 (defun flush-output-buffer (stream)
459 (let ((length (fd-stream-obuf-tail stream)))
460 (unless (= length 0)
461 (do-output stream (fd-stream-obuf-sap stream) 0 length t)
462 (setf (fd-stream-obuf-tail stream) 0))))
463
464 ;;; DEF-OUTPUT-ROUTINES -- internal
465 ;;;
466 ;;; Define output routines that output numbers size bytes long for the
467 ;;; given bufferings. Use body to do the actual output.
468 ;;;
469 (defmacro def-output-routines ((name size &rest bufferings) &body body)
470 (declare (optimize (speed 1)))
471 (cons 'progn
472 (mapcar
473 #'(lambda (buffering)
474 (let ((function
475 (intern (let ((*print-case* :upcase))
476 (format nil name (car buffering))))))
477 `(progn
478 (defun ,function (stream byte)
479 ,(unless (eq (car buffering) :none)
480 `(when (< (fd-stream-obuf-length stream)
481 (+ (fd-stream-obuf-tail stream)
482 ,size))
483 (flush-output-buffer stream)))
484 ;;
485 ;; If there is any input read from UNIX but not
486 ;; supplied to the user of the stream, reposition
487 ;; to the real file position as seen from Lisp.
488 ,(unless (eq (car buffering) :none)
489 `(when (> (fd-stream-ibuf-tail stream)
490 (fd-stream-ibuf-head stream))
491 (file-position stream (file-position stream))))
492 ,@body
493 (incf (fd-stream-obuf-tail stream) ,size)
494 ,(ecase (car buffering)
495 (:none
496 `(flush-output-buffer stream))
497 (:line
498 `(when (eql (char-code byte) (char-code #\Newline))
499 (flush-output-buffer stream)))
500 (:full
501 ))
502 (values))
503 (setf *output-routines*
504 (nconc *output-routines*
505 ',(mapcar
506 #'(lambda (type)
507 (list type
508 (car buffering)
509 function
510 size))
511 (cdr buffering)))))))
512 bufferings)))
513
514 #-unicode
515 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
516 1
517 (:none character)
518 (:line character)
519 (:full character))
520 (if (char= byte #\Newline)
521 (setf (fd-stream-char-pos stream) 0)
522 (incf (fd-stream-char-pos stream)))
523 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
524 (char-code byte)))
525
526 #+unicode
527 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
528 1
529 (:none character)
530 (:line character)
531 (:full character))
532 (if (char= byte #\Newline)
533 (setf (fd-stream-char-pos stream) 0)
534 (incf (fd-stream-char-pos stream)))
535 ;; FIXME! We only use the low 8 bits of a character!
536 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
537 (logand #xff (char-code byte))))
538
539 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
540 1
541 (:none (unsigned-byte 8))
542 (:full (unsigned-byte 8)))
543 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
544 byte))
545
546 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
547 1
548 (:none (signed-byte 8))
549 (:full (signed-byte 8)))
550 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
551 (fd-stream-obuf-tail stream))
552 byte))
553
554 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
555 2
556 (:none (unsigned-byte 16))
557 (:full (unsigned-byte 16)))
558 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
559 byte))
560
561 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
562 2
563 (:none (signed-byte 16))
564 (:full (signed-byte 16)))
565 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
566 (fd-stream-obuf-tail stream))
567 byte))
568
569 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
570 4
571 (:none (unsigned-byte 32))
572 (:full (unsigned-byte 32)))
573 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
574 byte))
575
576 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
577 4
578 (:none (signed-byte 32))
579 (:full (signed-byte 32)))
580 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
581 (fd-stream-obuf-tail stream))
582 byte))
583
584 (stream::def-ef-macro ef-cout (extfmt lisp stream::+ef-max+ stream::+ef-cout+)
585 `(lambda (stream char)
586 (declare (type fd-stream stream)
587 (type character char)
588 (optimize (speed 3) (space 0) (debug 0) (safety 0)))
589 ;; If there is any input read from UNIX but not
590 ;; supplied to the user of the stream, reposition
591 ;; to the real file position as seen from Lisp.
592 (when (> (fd-stream-ibuf-tail stream)
593 (fd-stream-ibuf-head stream))
594 (file-position stream (file-position stream)))
595 (let* ((sap (fd-stream-obuf-sap stream))
596 (len (fd-stream-obuf-length stream))
597 (tail (fd-stream-obuf-tail stream)))
598 (declare (type sys:system-area-pointer sap) (type index len tail))
599 (stream::char-to-octets ,extfmt
600 char
601 (fd-stream-co-state stream)
602 (lambda (byte)
603 (when (= tail len)
604 (do-output stream sap 0 tail t)
605 (setq sap (fd-stream-obuf-sap stream)
606 tail 0))
607 (setf (bref sap (1- (incf tail))) byte))
608 (fd-stream-char-to-octets-error stream))
609 (setf (fd-stream-obuf-tail stream) tail))
610 (if (char= char #\Newline)
611 (setf (fd-stream-char-pos stream) 0)
612 (incf (fd-stream-char-pos stream)))
613 (ecase (fd-stream-buffering stream)
614 (:none (flush-output-buffer stream))
615 (:line (when (char= char #\Newline) (flush-output-buffer stream)))
616 (:full #| do nothing |#))
617 (values)))
618
619
620 ;;; OUTPUT-RAW-BYTES -- public
621 ;;;
622 ;;; Does the actual output. If there is space to buffer the string, buffer
623 ;;; it. If the string would normally fit in the buffer, but doesn't because
624 ;;; of other stuff in the buffer, flush the old noise out of the buffer and
625 ;;; put the string in it. Otherwise we have a very long string, so just
626 ;;; send it directly (after flushing the buffer, of course).
627 ;;;
628 (defun output-raw-bytes (stream thing &optional start end)
629 "Output THING to stream. THING can be any kind of vector or a sap. If THING
630 is a SAP, END must be supplied (as length won't work)."
631 (let ((start (or start 0))
632 (end (or end (length (the (simple-array * (*)) thing)))))
633 (declare (type index start end))
634 ;;
635 ;; If there is any input read from UNIX but not
636 ;; supplied to the user of the stream, reposition
637 ;; to the real file position as seen from Lisp.
638 (when (> (fd-stream-ibuf-tail stream)
639 (fd-stream-ibuf-head stream))
640 (file-position stream (file-position stream)))
641 (let* ((len (fd-stream-obuf-length stream))
642 (tail (fd-stream-obuf-tail stream))
643 (space (- len tail))
644 (bytes (- end start))
645 (newtail (+ tail bytes)))
646 (cond ((minusp bytes) ; Error case
647 (cerror (intl:gettext "Just go on as if nothing happened...")
648 (intl:gettext "~S called with :END before :START!")
649 'output-raw-bytes))
650 ((zerop bytes)) ; Easy case
651 ((<= bytes space)
652 (if (system-area-pointer-p thing)
653 (system-area-copy thing
654 (* start vm:byte-bits)
655 (fd-stream-obuf-sap stream)
656 (* tail vm:byte-bits)
657 (* bytes vm:byte-bits))
658 (copy-to-system-area thing
659 (+ (* start vm:byte-bits)
660 (* vm:vector-data-offset vm:word-bits))
661 (fd-stream-obuf-sap stream)
662 (* tail vm:byte-bits)
663 (* bytes vm:byte-bits)))
664 (setf (fd-stream-obuf-tail stream) newtail))
665 ((<= bytes len)
666 (flush-output-buffer stream)
667 (if (system-area-pointer-p thing)
668 (system-area-copy thing
669 (* start vm:byte-bits)
670 (fd-stream-obuf-sap stream)
671 0
672 (* bytes vm:byte-bits))
673 (copy-to-system-area thing
674 (+ (* start vm:byte-bits)
675 (* vm:vector-data-offset vm:word-bits))
676 (fd-stream-obuf-sap stream)
677 0
678 (* bytes vm:byte-bits)))
679 (setf (fd-stream-obuf-tail stream) bytes))
680 (t
681 (flush-output-buffer stream)
682 (do-output stream thing start end nil))))))
683
684 ;;; FD-SOUT -- internal
685 ;;;
686 ;;; Routine to use to output a string. If the stream is unbuffered, slam
687 ;;; the string down the file descriptor, otherwise use OUTPUT-RAW-BYTES to
688 ;;; buffer the string. Update charpos by checking to see where the last newline
689 ;;; was.
690 ;;;
691 ;;; Note: some bozos (the FASL dumper) call write-string with things other
692 ;;; than strings. Therefore, we must make sure we have a string before calling
693 ;;; position on it.
694 ;;;
695
696 (stream::def-ef-macro ef-sout (extfmt lisp stream::+ef-max+ stream::+ef-sout+)
697 `(lambda (stream string start end)
698 (declare (type fd-stream stream)
699 (type simple-string string)
700 (type index start end)
701 (optimize (speed 3) (space 0) (safety 0) (debug 0)))
702 ;; If there is any input read from UNIX but not
703 ;; supplied to the user of the stream, reposition
704 ;; to the real file position as seen from Lisp.
705 ;; (maybe the caller should do this?)
706 (when (> (fd-stream-ibuf-tail stream)
707 (fd-stream-ibuf-head stream))
708 (file-position stream (file-position stream)))
709 (let* ((sap (fd-stream-obuf-sap stream))
710 (len (fd-stream-obuf-length stream))
711 (tail (fd-stream-obuf-tail stream)))
712 (declare (type sys:system-area-pointer sap) (type index len tail))
713 (dotimes (i (- end start))
714 (stream::char-to-octets ,extfmt
715 (schar string (+ i start))
716 (fd-stream-co-state stream)
717 (lambda (byte)
718 (when (= tail len)
719 (do-output stream sap 0 tail t)
720 (setq sap (fd-stream-obuf-sap stream)
721 tail 0))
722 (setf (bref sap (1- (incf tail))) byte))
723 (fd-stream-char-to-octets-error stream)))
724 (setf (fd-stream-obuf-tail stream) tail))))
725
726
727 #-unicode
728 (defun fd-sout (stream thing start end)
729 (let ((start (or start 0))
730 (end (or end (length (the vector thing)))))
731 (declare (type index start end))
732 (if (stringp thing)
733 (let ((last-newline (and (find #\newline (the simple-string thing)
734 :start start :end end)
735 (position #\newline (the simple-string thing)
736 :from-end t
737 :start start
738 :end end))))
739 (ecase (fd-stream-buffering stream)
740 (:full
741 (output-raw-bytes stream thing start end))
742 (:line
743 (output-raw-bytes stream thing start end)
744 (when last-newline
745 (flush-output-buffer stream)))
746 (:none
747 (do-output stream thing start end nil)))
748 (if last-newline
749 (setf (fd-stream-char-pos stream)
750 (- end last-newline 1))
751 (incf (fd-stream-char-pos stream)
752 (- end start))))
753 (ecase (fd-stream-buffering stream)
754 ((:line :full)
755 (output-raw-bytes stream thing start end))
756 (:none
757 (do-output stream thing start end nil))))))
758
759 #+unicode
760 ;; Temporary. The final version is defined in fd-stream-extfmt.lisp
761 (defun fd-sout (stream thing start end)
762 (declare (type string thing))
763 (let ((start (or start 0))
764 (end (or end (length (the vector thing)))))
765 (declare (type index start end))
766 (cond
767 ((stringp thing) ; FIXME - remove this test
768 (let ((out (fd-stream-out stream)))
769 (do ((index start (+ index 1)))
770 ((>= index end))
771 (funcall out stream (elt thing index))))))))
772
773 (defmacro output-wrapper ((stream size buffering) &body body)
774 (let ((stream-var (gensym)))
775 `(let ((,stream-var ,stream))
776 ,(unless (eq (car buffering) :none)
777 `(when (< (fd-stream-obuf-length ,stream-var)
778 (+ (fd-stream-obuf-tail ,stream-var)
779 ,size))
780 (flush-output-buffer ,stream-var)))
781 ,(unless (eq (car buffering) :none)
782 `(when (> (fd-stream-ibuf-tail ,stream-var)
783 (fd-stream-ibuf-head ,stream-var))
784 (file-position ,stream-var (file-position ,stream-var))))
785
786 ,@body
787 (incf (fd-stream-obuf-tail ,stream-var) ,size)
788 ,(ecase (car buffering)
789 (:none
790 `(flush-output-buffer ,stream-var))
791 (:line
792 `(when (eq (char-code byte) (char-code #\Newline))
793 (flush-output-buffer ,stream-var)))
794 (:full))
795 (values))))
796
797 ;;; PICK-OUTPUT-ROUTINE -- internal
798 ;;;
799 ;;; Find an output routine to use given the type and buffering. Return as
800 ;;; multiple values the routine, the real type transfered, and the number of
801 ;;; bytes per element.
802 ;;;
803 (defun pick-output-routine (type buffering)
804 (dolist (entry *output-routines*)
805 (when (and (subtypep type (car entry))
806 (eq buffering (cadr entry)))
807 (return-from pick-output-routine
808 (values (symbol-function (caddr entry))
809 (car entry)
810 (cadddr entry)))))
811 ;; KLUDGE: also see comments in PICK-INPUT-ROUTINE
812 (loop for i from 40 by 8 to max-stream-element-size ; ARB (KLUDGE)
813 if (subtypep type `(unsigned-byte ,i))
814 do (return-from pick-output-routine
815 (values
816 (ecase buffering
817 (:none
818 (lambda (stream byte)
819 (output-wrapper (stream (/ i 8) (:none))
820 (loop for j from 0 below (/ i 8)
821 do (setf (sap-ref-8
822 (fd-stream-obuf-sap stream)
823 (+ j (fd-stream-obuf-tail stream)))
824 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
825 (:full
826 (lambda (stream byte)
827 (output-wrapper (stream (/ i 8) (:full))
828 (loop for j from 0 below (/ i 8)
829 do (setf (sap-ref-8
830 (fd-stream-obuf-sap stream)
831 (+ j (fd-stream-obuf-tail stream)))
832 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
833 `(unsigned-byte ,i)
834 (/ i 8))))
835 (loop for i from 40 by 8 to max-stream-element-size ; ARB (KLUDGE)
836 if (subtypep type `(signed-byte ,i))
837 do (return-from pick-output-routine
838 (values
839 (ecase buffering
840 (:none
841 (lambda (stream byte)
842 (output-wrapper (stream (/ i 8) (:none))
843 (loop for j from 0 below (/ i 8)
844 do (setf (sap-ref-8
845 (fd-stream-obuf-sap stream)
846 (+ j (fd-stream-obuf-tail stream)))
847 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
848 (:full
849 (lambda (stream byte)
850 (output-wrapper (stream (/ i 8) (:full))
851 (loop for j from 0 below (/ i 8)
852 do (setf (sap-ref-8
853 (fd-stream-obuf-sap stream)
854 (+ j (fd-stream-obuf-tail stream)))
855 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
856 `(signed-byte ,i)
857 (/ i 8)))))
858
859 ;;;; Input routines and related noise.
860
861 (defvar *input-routines* ()
862 "List of all available input routines. Each element is a list of the
863 element-type input, the function name, and the number of bytes per element.")
864
865 ;;; DO-INPUT -- internal
866 ;;;
867 ;;; Fills the input buffer, and returns the first character. Throws to
868 ;;; eof-input-catcher if the eof was reached. Drops into system:server if
869 ;;; necessary.
870 ;;;
871 (defun do-input (stream)
872 (let ((fd (fd-stream-fd stream))
873 (ibuf-sap (fd-stream-ibuf-sap stream))
874 (buflen (fd-stream-ibuf-length stream))
875 (head (fd-stream-ibuf-head stream))
876 (lcrs #-unicode 0
877 #+unicode (fd-stream-last-char-read-size stream))
878 (tail (fd-stream-ibuf-tail stream)))
879 (declare (type index head lcrs tail))
880 (unless (zerop head)
881 (cond ((eql head tail)
882 (setf head lcrs)
883 (setf tail lcrs)
884 (setf (fd-stream-ibuf-head stream) lcrs)
885 (setf (fd-stream-ibuf-tail stream) lcrs))
886 (t
887 (decf tail (- head lcrs))
888 (system-area-copy ibuf-sap (* (- head lcrs) vm:byte-bits)
889 ibuf-sap 0 (* tail vm:byte-bits))
890 (setf head lcrs)
891 (setf (fd-stream-ibuf-head stream) lcrs)
892 (setf (fd-stream-ibuf-tail stream) tail))))
893 (setf (fd-stream-listen stream) nil)
894 (multiple-value-bind
895 (count errno)
896 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
897 (unix:fd-zero read-fds)
898 (unix:fd-set fd read-fds)
899 (unix:unix-fast-select (1+ fd) (alien:addr read-fds) nil nil 0 0))
900 ;; Wait if input is not available or if interrupted.
901 (when (or (eql count 0)
902 (and (not count) (eql errno unix:eintr)))
903 (unless #-mp (system:wait-until-fd-usable
904 fd :input (fd-stream-timeout stream))
905 #+mp (mp:process-wait-until-fd-usable
906 fd :input (fd-stream-timeout stream))
907 (error 'io-timeout :stream stream :direction :read))))
908 (multiple-value-bind
909 (count errno)
910 (unix:unix-read fd
911 (system:int-sap (+ (system:sap-int ibuf-sap) tail))
912 (- buflen tail))
913 (cond ((null count)
914 ;; What kinds of errors do we want to look at and what do
915 ;; we want them to do?
916 (cond ((eql errno unix:ewouldblock)
917 (unless #-mp (system:wait-until-fd-usable
918 fd :input (fd-stream-timeout stream))
919 #+mp (mp:process-wait-until-fd-usable
920 fd :input (fd-stream-timeout stream))
921 (error 'io-timeout :stream stream :direction :read))
922 (do-input stream))
923 ((eql errno unix:econnreset)
924 (error 'socket-error
925 :format-control "Socket connection reset: ~A"
926 :format-arguments (list (unix:get-unix-error-msg errno))
927 :errno errno))
928 (t
929 (error (intl:gettext "Error reading ~S: ~A")
930 stream
931 (unix:get-unix-error-msg errno)))))
932 ((zerop count)
933 (setf (fd-stream-listen stream) :eof)
934 (throw 'eof-input-catcher nil))
935 (t
936 (incf (fd-stream-ibuf-tail stream) count))))))
937
938 ;;; INPUT-AT-LEAST -- internal
939 ;;;
940 ;;; Makes sure there are at least ``bytes'' number of bytes in the input
941 ;;; buffer. Keeps calling do-input until that condition is met.
942 ;;;
943 (defmacro input-at-least (stream bytes)
944 (let ((stream-var (gensym))
945 (bytes-var (gensym)))
946 `(let ((,stream-var ,stream)
947 (,bytes-var ,bytes))
948 (loop
949 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
950 (fd-stream-ibuf-head ,stream-var))
951 ,bytes-var)
952 (return))
953 (do-input ,stream-var)))))
954
955 ;;; INPUT-WRAPPER -- internal
956 ;;;
957 ;;; Macro to wrap around all input routines to handle eof-error noise.
958 ;;;
959 (defmacro input-wrapper ((stream bytes eof-error eof-value &optional type) &body read-forms)
960 (let ((stream-var (gensym))
961 (element-var (gensym)))
962 `(let ((,stream-var ,stream))
963 (if (fd-stream-unread ,stream-var) ;;@@
964 (prog1
965 ,(if (eq type 'character)
966 `(fd-stream-unread ,stream-var)
967 `(char-code (fd-stream-unread ,stream-var)))
968 (setf (fd-stream-unread ,stream-var) nil)
969 (setf (fd-stream-listen ,stream-var) nil))
970 (let ((,element-var
971 (catch 'eof-input-catcher
972 (input-at-least ,stream-var ,bytes)
973 ,@read-forms)))
974 (cond (,element-var
975 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
976 ,element-var)
977 (t
978 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
979
980 ;;; DEF-INPUT-ROUTINE -- internal
981 ;;;
982 ;;; Defines an input routine.
983 ;;;
984 (defmacro def-input-routine (name
985 (type size sap head)
986 &rest body)
987 `(progn
988 (defun ,name (stream eof-error eof-value)
989 (input-wrapper (stream ,size eof-error eof-value ,type)
990 (let ((,sap (fd-stream-ibuf-sap stream))
991 (,head (fd-stream-ibuf-head stream)))
992 ,@body)))
993 (setf *input-routines*
994 (nconc *input-routines*
995 (list (list ',type ',name ',size))))))
996
997 ;;; INPUT-CHARACTER -- internal
998 ;;;
999 ;;; Routine to use in stream-in slot for reading string chars.
1000 ;;;
1001 (def-input-routine input-character
1002 (character 1 sap head)
1003 (code-char (sap-ref-8 sap head)))
1004
1005 ;;; INPUT-UNSIGNED-8BIT-BYTE -- internal
1006 ;;;
1007 ;;; Routine to read in an unsigned 8 bit number.
1008 ;;;
1009 (def-input-routine input-unsigned-8bit-byte
1010 ((unsigned-byte 8) 1 sap head)
1011 (sap-ref-8 sap head))
1012
1013 ;;; INPUT-SIGNED-8BIT-BYTE -- internal
1014 ;;;
1015 ;;; Routine to read in a signed 8 bit number.
1016 ;;;
1017 (def-input-routine input-signed-8bit-number
1018 ((signed-byte 8) 1 sap head)
1019 (signed-sap-ref-8 sap head))
1020
1021 ;;; INPUT-UNSIGNED-16BIT-BYTE -- internal
1022 ;;;
1023 ;;; Routine to read in an unsigned 16 bit number.
1024 ;;;
1025 (def-input-routine input-unsigned-16bit-byte
1026 ((unsigned-byte 16) 2 sap head)
1027 (sap-ref-16 sap head))
1028
1029 ;;; INPUT-SIGNED-16BIT-BYTE -- internal
1030 ;;;
1031 ;;; Routine to read in a signed 16 bit number.
1032 ;;;
1033 (def-input-routine input-signed-16bit-byte
1034 ((signed-byte 16) 2 sap head)
1035 (signed-sap-ref-16 sap head))
1036
1037 ;;; INPUT-UNSIGNED-32BIT-BYTE -- internal
1038 ;;;
1039 ;;; Routine to read in a unsigned 32 bit number.
1040 ;;;
1041 (def-input-routine input-unsigned-32bit-byte
1042 ((unsigned-byte 32) 4 sap head)
1043 (sap-ref-32 sap head))
1044
1045 ;;; INPUT-SIGNED-32BIT-BYTE -- internal
1046 ;;;
1047 ;;; Routine to read in a signed 32 bit number.
1048 ;;;
1049 (def-input-routine input-signed-32bit-byte
1050 ((signed-byte 32) 4 sap head)
1051 (signed-sap-ref-32 sap head))
1052
1053 (stream::def-ef-macro ef-cin (extfmt lisp stream::+ef-max+ stream::+ef-cin+)
1054 `(lambda (stream eof-error-p eof-value)
1055 (declare (type fd-stream stream)
1056 #|(optimize (speed 3) (space 0) (debug 0) (safety 0))|#)
1057 (let* ((head (fd-stream-ibuf-head stream))
1058 (ch (catch 'eof-input-catcher
1059 (stream::octets-to-char ,extfmt
1060 (fd-stream-oc-state stream)
1061 (fd-stream-last-char-read-size stream)
1062 ;;@@ Note: need proper EOF handling...
1063 (progn
1064 (when (= head (fd-stream-ibuf-tail stream))
1065 (let ((sofar (- head (fd-stream-ibuf-head
1066 stream))))
1067 (do-input stream)
1068 (setf head (+ (fd-stream-ibuf-head stream)
1069 sofar))))
1070 (bref (fd-stream-ibuf-sap stream)
1071 (1- (incf head))))
1072 (lambda (n) (decf head n))))))
1073 (declare (type index head))
1074 (if ch
1075 (progn
1076 (setf (fd-stream-ibuf-head stream) head)
1077 ch)
1078 (eof-or-lose stream eof-error-p eof-value)))))
1079
1080 #+(or)
1081 (stream::def-ef-macro ef-sin (extfmt lisp stream::+ef-max+ stream::+ef-sin+)
1082 `(lambda (stream string char start end)
1083 (declare (type fd-stream stream)
1084 (type simple-string string)
1085 (type (or character null) char)
1086 (type index start end)
1087 (optimize (speed 3) (space 0) (debug 0) (safety 0)))
1088 (let ((sap (fd-stream-ibuf-sap stream))
1089 (head (fd-stream-ibuf-head stream))
1090 (tail (fd-stream-ibuf-tail stream))
1091 (curr start))
1092 (declare (type sys:system-area-pointer sap)
1093 (type index head tail curr))
1094 (loop
1095 ;;@@ Fix EOF handling
1096 (let* ((sz 0)
1097 (ch (catch 'eof-input-catcher
1098 (stream::octets-to-char ,extfmt
1099 (fd-stream-oc-state stream)
1100 sz
1101 (progn
1102 (when (= head tail)
1103 (let ((sofar (- head
1104 (fd-stream-ibuf-head
1105 stream))))
1106 (do-input stream)
1107 (setq head
1108 (+ (fd-stream-ibuf-head
1109 stream)
1110 sofar)
1111 tail
1112 (fd-stream-ibuf-tail
1113 stream))))
1114 (bref sap (1- (incf head))))
1115 (lambda (n) (decf head n))))))
1116 (declare (type index sz)
1117 (type (or null character) ch))
1118 (when (null ch)
1119 (return (values (- curr start) :eof)))
1120 (setf (fd-stream-last-char-read-size stream) sz)
1121 (incf (fd-stream-ibuf-head stream) sz)
1122 (when (and char (char= ch char))
1123 (return (values (- curr start) t)))
1124 (setf (schar string (1- (incf curr))) ch)
1125 (when (= curr end)
1126 (return (values (- curr start) nil))))))))
1127
1128 ;;; PICK-INPUT-ROUTINE -- internal
1129 ;;;
1130 ;;; Find an input routine to use given the type. Return as multiple values
1131 ;;; the routine, the real type transfered, and the number of bytes per element.
1132 ;;;
1133 (defun pick-input-routine (type)
1134 (dolist (entry *input-routines*)
1135 (when (subtypep type (car entry))
1136 (return-from pick-input-routine
1137 (values (symbol-function (cadr entry))
1138 (car entry)
1139 (caddr entry)))))
1140 ;; FIXME: let's do it the hard way, then (but ignore things like
1141 ;; endianness, efficiency, and the necessary coupling between these
1142 ;; and the output routines). -- CSR, 2004-02-09
1143 (loop for i from 40 by 8 to max-stream-element-size ; ARB (well, KLUDGE really)
1144 if (subtypep type `(unsigned-byte ,i))
1145 do (return-from pick-input-routine
1146 (values
1147 (lambda (stream eof-error eof-value)
1148 (input-wrapper (stream (/ i 8) eof-error eof-value)
1149 (let ((sap (fd-stream-ibuf-sap stream))
1150 (head (fd-stream-ibuf-head stream)))
1151 (loop for j from 0 below (/ i 8)
1152 with result = 0
1153 do (setf result
1154 (+ (* 256 result)
1155 (sap-ref-8 sap (+ head j))))
1156 finally (return result)))))
1157 `(unsigned-byte ,i)
1158 (/ i 8))))
1159 (loop for i from 40 by 8 to max-stream-element-size ; ARB (well, KLUDGE really)
1160 if (subtypep type `(signed-byte ,i))
1161 do (return-from pick-input-routine
1162 (values
1163 (lambda (stream eof-error eof-value)
1164 (input-wrapper (stream (/ i 8) eof-error eof-value)
1165 (let ((sap (fd-stream-ibuf-sap stream))
1166 (head (fd-stream-ibuf-head stream)))
1167 (loop for j from 0 below (/ i 8)
1168 with result = 0
1169 do (setf result
1170 (+ (* 256 result)
1171 (sap-ref-8 sap (+ head j))))
1172 finally (return (if (logbitp (1- i) result)
1173 (dpb result (byte i 0) -1)
1174 result))))))
1175 `(signed-byte ,i)
1176 (/ i 8)))))
1177
1178 ;;; STRING-FROM-SAP -- internal
1179 ;;;
1180 ;;; Returns a string constructed from the sap, start, and end.
1181 ;;;
1182 (defun string-from-sap (sap start end)
1183 (declare (type index start end))
1184 (let* ((length (- end start))
1185 (string (make-string length)))
1186 (copy-from-system-area sap (* start vm:byte-bits)
1187 string (* vm:vector-data-offset vm:word-bits)
1188 (* length vm:byte-bits))
1189 string))
1190
1191 #|
1192 ;;; FD-STREAM-READ-N-BYTES -- internal
1193 ;;;
1194 ;;; This version waits using server. I changed to the non-server version
1195 ;;; because it allows this method to be used by CLX w/o confusing serve-event.
1196 ;;; The non-server method is also significantly more efficient for large
1197 ;;; reads. -- Ram
1198 ;;;
1199 ;;; The n-bin routine.
1200 ;;;
1201 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
1202 (declare (type stream stream) (type index start requested))
1203 (let* ((sap (fd-stream-ibuf-sap stream))
1204 (elsize (fd-stream-element-size stream))
1205 (offset (* elsize start))
1206 (bytes (* elsize requested))
1207 (result
1208 (catch 'eof-input-catcher
1209 (loop
1210 (input-at-least stream 1)
1211 (let* ((head (fd-stream-ibuf-head stream))
1212 (tail (fd-stream-ibuf-tail stream))
1213 (available (- tail head))
1214 (copy (min available bytes)))
1215 (if (typep buffer 'system-area-pointer)
1216 (system-area-copy sap (* head vm:byte-bits)
1217 buffer (* offset vm:byte-bits)
1218 (* copy vm:byte-bits))
1219 (copy-from-system-area sap (* head vm:byte-bits)
1220 buffer (+ (* offset vm:byte-bits)
1221 (* vm:vector-data-offset
1222 vm:word-bits))
1223 (* copy vm:byte-bits)))
1224 (incf (fd-stream-ibuf-head stream) copy)
1225 (incf offset copy)
1226 (decf bytes copy))
1227 (when (zerop bytes)
1228 (return requested))))))
1229 (or result
1230 (eof-or-lose stream eof-error-p
1231 (- requested (/ bytes elsize))))))
1232 |#
1233
1234
1235 ;;; FD-STREAM-READ-N-BYTES -- internal
1236 ;;;
1237 ;;; The N-Bin method for FD-STREAMs. This doesn't use the SERVER; it blocks
1238 ;;; in UNIX-READ. This allows the method to be used to implement reading
1239 ;;; for CLX. It is generally used where there is a definite amount of reading
1240 ;;; to be done, so blocking isn't too problematical.
1241 ;;;
1242 ;;; We copy buffered data into the buffer. If there is enough, just return.
1243 ;;; Otherwise, we see if the amount of additional data needed will fit in the
1244 ;;; stream buffer. If not, inhibit GCing (so we can have a SAP into the Buffer
1245 ;;; argument), and read directly into the user supplied buffer. Otherwise,
1246 ;;; read a buffer-full into the stream buffer and then copy the amount we need
1247 ;;; out.
1248 ;;;
1249 ;;; We loop doing the reads until we either get enough bytes or hit EOF. We
1250 ;;; must loop when eof-errorp is T because some streams (like pipes) may return
1251 ;;; a partial amount without hitting EOF.
1252 ;;;
1253 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
1254 (declare (type stream stream) (type index start requested))
1255 (let* ((sap (fd-stream-ibuf-sap stream))
1256 (offset start)
1257 (head (fd-stream-ibuf-head stream))
1258 (tail (fd-stream-ibuf-tail stream))
1259 (available (- tail head))
1260 (copy (min requested available)))
1261 (declare (type index offset head tail available copy))
1262 ;;
1263 ;; If something has been unread, put that at buffer + start,
1264 ;; and read the rest to start + 1.
1265 (when (fd-stream-unread stream) ;;@@
1266 (etypecase buffer
1267 (system-area-pointer
1268 (assert (= 1 (fd-stream-element-size stream)))
1269 (setf (sap-ref-8 buffer start) (char-code (read-char stream))))
1270 (string
1271 (setf (aref buffer start) (read-char stream)))
1272 (vector
1273 (setf (aref buffer start) (char-code(read-char stream)))))
1274 (return-from fd-stream-read-n-bytes
1275 (1+ (fd-stream-read-n-bytes stream buffer (1+ start) (1- requested)
1276 eof-error-p))))
1277 ;;
1278 (unless (zerop copy)
1279 (if (typep buffer 'system-area-pointer)
1280 (system-area-copy sap (* head vm:byte-bits)
1281 buffer (* offset vm:byte-bits)
1282 (* copy vm:byte-bits))
1283 (copy-from-system-area sap (* head vm:byte-bits)
1284 buffer (+ (* offset vm:byte-bits)
1285 (* vm:vector-data-offset
1286 vm:word-bits))
1287 (* copy vm:byte-bits)))
1288 (incf (fd-stream-ibuf-head stream) copy))
1289 (cond
1290 ((or (= copy requested)
1291 (and (not eof-error-p) (/= copy 0)))
1292 copy)
1293 (t
1294 (setf (fd-stream-ibuf-head stream) 0)
1295 (setf (fd-stream-ibuf-tail stream) 0)
1296 (setf (fd-stream-listen stream) nil)
1297 (let ((now-needed (- requested copy))
1298 (len (fd-stream-ibuf-length stream)))
1299 (declare (type index now-needed len))
1300 (cond
1301 ((> now-needed len)
1302 ;;
1303 ;; If the desired amount is greater than the stream buffer size, then
1304 ;; read directly into the destination, incrementing the start
1305 ;; accordingly. In this case, we never leave anything in the stream
1306 ;; buffer.
1307 (system:without-gcing
1308 (loop
1309 (multiple-value-bind
1310 (count err)
1311 (unix:unix-read (fd-stream-fd stream)
1312 (sap+ (if (typep buffer 'system-area-pointer)
1313 buffer
1314 (vector-sap buffer))
1315 (+ offset copy))
1316 now-needed)
1317 (declare (type (or index null) count))
1318 (unless count
1319 (error (intl:gettext "Error reading ~S: ~A") stream
1320 (unix:get-unix-error-msg err)))
1321 (decf now-needed count)
1322 (if eof-error-p
1323 (when (zerop count)
1324 (error 'end-of-file :stream stream))
1325 (return (- requested now-needed)))
1326 (when (zerop now-needed) (return requested))
1327 (incf offset count)))))
1328 (t
1329 ;;
1330 ;; If we want less than the buffer size, then loop trying to fill the
1331 ;; stream buffer and copying what we get into the destination. When
1332 ;; we have enough, we leave what's left in the stream buffer.
1333 (loop
1334 (multiple-value-bind
1335 (count err)
1336 (unix:unix-read (fd-stream-fd stream) sap len)
1337 (declare (type (or index null) count))
1338 (unless count
1339 (error (intl:gettext "Error reading ~S: ~A") stream
1340 (unix:get-unix-error-msg err)))
1341 (when (and eof-error-p (zerop count))
1342 (error 'end-of-file :stream stream))
1343
1344 (let* ((copy (min now-needed count))
1345 (copy-bits (* copy vm:byte-bits))
1346 (buffer-start-bits
1347 (* (+ offset available) vm:byte-bits)))
1348 (declare (type index copy copy-bits buffer-start-bits))
1349 (if (typep buffer 'system-area-pointer)
1350 (system-area-copy sap 0
1351 buffer buffer-start-bits
1352 copy-bits)
1353 (copy-from-system-area sap 0
1354 buffer (+ buffer-start-bits
1355 (* vm:vector-data-offset
1356 vm:word-bits))
1357 copy-bits))
1358
1359 (decf now-needed copy)
1360 (when (or (zerop now-needed) (not eof-error-p))
1361 (setf (fd-stream-ibuf-head stream) copy)
1362 (setf (fd-stream-ibuf-tail stream) count)
1363 (return (- requested now-needed)))
1364 (incf offset copy)))))))))))
1365
1366
1367 ;;;; Utility functions (misc routines, etc)
1368
1369 ;;; SET-ROUTINES -- internal
1370 ;;;
1371 ;;; Fill in the various routine slots for the given type. Input-p and
1372 ;;; output-p indicate what slots to fill. The buffering slot must be set prior
1373 ;;; to calling this routine.
1374 ;;;
1375
1376 (defun set-routines (stream type input-p output-p buffer-p &key binary-stream-p)
1377 (let ((target-type (case type
1378 ((:default unsigned-byte)
1379 '(unsigned-byte 8))
1380 (signed-byte
1381 '(signed-byte 8))
1382 (t
1383 type)))
1384 (input-type nil)
1385 (output-type nil)
1386 (input-size nil)
1387 (output-size nil))
1388
1389 (when (fd-stream-obuf-sap stream)
1390 (push (fd-stream-obuf-sap stream) *available-buffers*)
1391 (setf (fd-stream-obuf-sap stream) nil))
1392 (when (fd-stream-ibuf-sap stream)
1393 (push (fd-stream-ibuf-sap stream) *available-buffers*)
1394 (setf (fd-stream-ibuf-sap stream) nil))
1395
1396 #+unicode
1397 (when (null (fd-stream-external-format stream))
1398 (setf (fd-stream-external-format stream) :default))
1399
1400 (when input-p
1401 (multiple-value-bind
1402 (routine type size)
1403 (pick-input-routine target-type)
1404 (unless routine
1405 (error (intl:gettext "Could not find any input routine for ~S") target-type))
1406 (setf (fd-stream-ibuf-sap stream) (next-available-buffer))
1407 (setf (fd-stream-ibuf-length stream) bytes-per-buffer)
1408 (setf (fd-stream-ibuf-tail stream) 0)
1409 (if (subtypep type 'character)
1410 (setf (fd-stream-in stream) routine
1411 (fd-stream-bin stream) #'ill-bin)
1412 (setf (fd-stream-in stream) (if (and binary-stream-p
1413 (eql size 1))
1414 (pick-input-routine 'character)
1415 #'ill-in)
1416 (fd-stream-bin stream) routine))
1417 (when (or (eql size 1)
1418 (eql size 2)
1419 (eql size 4))
1420 ;; Support for n-byte operations on 8-, 16-, and 32-bit streams
1421 (setf (fd-stream-n-bin stream) #'fd-stream-read-n-bytes)
1422 (when (and buffer-p (eql size 1)
1423 (or
1424 ;; FIXME: Do this better. We want to check for
1425 ;; (unsigned-byte 8). The 8 is unnecessary
1426 ;; since we already have size = 1.
1427 (or (eq 'unsigned-byte (and (consp type) (car type)))
1428 (eq type :default))
1429 (eq type 'character)))
1430 ;; We only create this buffer for streams of type
1431 ;; (unsigned-byte 8) or character streams with an external
1432 ;; format of :iso8859-1. Because there's no buffer, the
1433 ;; other element-types will dispatch to the appropriate
1434 ;; input (output) routine in fast-read-byte/fast-read-char.
1435 (when *enable-stream-buffer-p*
1436 (setf (lisp-stream-in-buffer stream)
1437 (make-array in-buffer-length
1438 :element-type '(unsigned-byte 8)))
1439 #+unicode
1440 (when (and (eq type 'character)
1441 (not (eq :iso8859-1 (fd-stream-external-format stream))))
1442 ;; For character streams, we create the string-buffer so
1443 ;; we can convert all available octets at once instead
1444 ;; of for each character. The string is one element
1445 ;; longer than in-buffer-length to leave room for
1446 ;; unreading.
1447 ;;
1448 ;; For ISO8859-1, we don't want this because it's very
1449 ;; easy and quick to convert octets to iso8859-1. (See
1450 ;; FAST-READ-CHAR.)
1451 (setf (lisp-stream-string-buffer stream)
1452 (make-string (1+ in-buffer-length)))
1453 (setf (fd-stream-octet-count stream)
1454 (make-array in-buffer-length :element-type '(unsigned-byte 8)))
1455 (setf (lisp-stream-string-buffer-len stream) 0)
1456 (setf (lisp-stream-string-index stream) 0)))))
1457 (setf input-size size)
1458 (setf input-type type)))
1459
1460 (when output-p
1461 (multiple-value-bind
1462 (routine type size)
1463 (pick-output-routine target-type (fd-stream-buffering stream))
1464 (unless routine
1465 (error (intl:gettext "Could not find any output routine for ~S buffered ~S.")
1466 (fd-stream-buffering stream)
1467 target-type))
1468 (setf (fd-stream-obuf-sap stream) (next-available-buffer))
1469 (setf (fd-stream-obuf-length stream) bytes-per-buffer)
1470 (setf (fd-stream-obuf-tail stream) 0)
1471 (if (subtypep type 'character)
1472 (setf (fd-stream-out stream) routine
1473 (fd-stream-bout stream) #'ill-bout)
1474 (setf (fd-stream-out stream)
1475 (or (if (eql size 1)
1476 (pick-output-routine 'base-char
1477 (fd-stream-buffering stream)))
1478 #'ill-out)
1479 (fd-stream-bout stream) routine))
1480 (setf (fd-stream-sout stream)
1481 (if (eql size 1) #'fd-sout #'ill-out))
1482 (setf (fd-stream-char-pos stream) 0)
1483 (setf output-size size)
1484 (setf output-type type)))
1485
1486 (when (and input-size output-size
1487 (not (eql input-size output-size)))
1488 (error (intl:gettext "Element sizes for input (~S:~S) and output (~S:~S) differ?")
1489 input-type input-size
1490 output-type output-size))
1491 (setf (fd-stream-element-size stream)
1492 (or input-size output-size))
1493
1494 (setf (fd-stream-element-type stream)
1495 (cond ((equal input-type output-type)
1496 input-type)
1497 ((null output-type)
1498 input-type)
1499 ((null input-type)
1500 output-type)
1501 ((subtypep input-type output-type)
1502 input-type)
1503 ((subtypep output-type input-type)
1504 output-type)
1505 (t
1506 (error (intl:gettext "Input type (~S) and output type (~S) are unrelated?")
1507 input-type
1508 output-type))))))
1509
1510 ;;; REVERT-FILE -- internal
1511 ;;;
1512 ;;; Revert a file, if possible; otherwise do nothing. Used during
1513 ;;; CLOSE when the abort flag is set.
1514 ;;;
1515 (defun revert-file (filename original)
1516 (declare (type simple-base-string filename)
1517 (type (or simple-base-string null) original))
1518 (when original
1519 (multiple-value-bind (okay err)
1520 (unix:unix-rename original filename)
1521 (unless okay
1522 (cerror (intl:gettext "Go on as if nothing bad happened.")
1523 (intl:gettext "Could not restore ~S to its original contents: ~A")
1524 filename (unix:get-unix-error-msg err))))))
1525
1526 ;;; DELETE-ORIGINAL -- internal
1527 ;;;
1528 ;;; Delete a backup file. Used during CLOSE.
1529 ;;;
1530 (defun delete-original (filename original)
1531 (declare (type simple-base-string filename)
1532 (type (or simple-base-string null) original))
1533 (when original
1534 (multiple-value-bind (okay err) (unix:unix-unlink original)
1535 (unless okay
1536 (cerror "Go on as if nothing bad happened."
1537 "Could not delete ~S during close of ~S: ~A"
1538 original filename (unix:get-unix-error-msg err))))))
1539
1540 ;;; FD-STREAM-MISC-ROUTINE -- input
1541 ;;;
1542 ;;; Handle the various misc operations on fd-stream.
1543 ;;;
1544 (defun fd-stream-misc-routine (stream operation &optional arg1 arg2)
1545 (declare (ignore arg2))
1546 (case operation
1547 (:listen
1548 (or (not (eql (fd-stream-ibuf-head stream)
1549 (fd-stream-ibuf-tail stream)))
1550 (fd-stream-listen stream)
1551 (setf (fd-stream-listen stream)
1552 (eql (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
1553 (unix:fd-zero read-fds)
1554 (unix:fd-set (fd-stream-fd stream) read-fds)
1555 (unix:unix-fast-select (1+ (fd-stream-fd stream))
1556 (alien:addr read-fds) nil nil
1557 0 0))
1558 1))))
1559 (:unread
1560 #-unicode
1561 (setf (fd-stream-unread stream) arg1)
1562 #+unicode
1563 (cond ((lisp-stream-string-buffer stream)
1564 (if (zerop (lisp-stream-string-index stream))
1565 (setf (fd-stream-unread stream) arg1)
1566 (decf (lisp-stream-string-index stream))))
1567 (t
1568 (if (zerop (fd-stream-last-char-read-size stream))
1569 (setf (fd-stream-unread stream) arg1)
1570 (decf (fd-stream-ibuf-head stream)
1571 (fd-stream-last-char-read-size stream)))))
1572 ;; Paul says:
1573 ;;
1574 ;; Not needed for unicode when unreading is implemented by backing up in
1575 ;; the buffer (e.g., with last-char-read-size...)
1576 ;;
1577 ;; (AFAICS there's nothing wrong with setting it there, but it
1578 ;; screws up read-interactive in my toplevel command thing -
1579 ;; leaves it expecting to read arguments when it shouldn't,
1580 ;; because LISTEN returns T when there's no input pending, but I
1581 ;; don't understand why...)
1582 #-unicode
1583 (setf (fd-stream-listen stream) t))
1584 (:close
1585 (cond (arg1
1586 ;; We got us an abort on our hands.
1587 (when (fd-stream-handler stream)
1588 (system:remove-fd-handler (fd-stream-handler stream))
1589 (setf (fd-stream-handler stream) nil))
1590 (when (and (fd-stream-file stream) (fd-stream-obuf-sap stream))
1591 (revert-file (fd-stream-file stream)
1592 (fd-stream-original stream))))
1593 (t
1594 #+(and unicode (not unicode-bootstrap))
1595 (when (and (output-stream-p stream)
1596 (eq (stream-element-type stream) 'character))
1597 ;; For output character streams, we need to flush out
1598 ;; any state that the external format might have.
1599 #+nil (format *debug-io* "state = ~S~%" (fd-stream-co-state stream))
1600 (funcall (ef-flush (fd-stream-external-format stream))
1601 stream))
1602 (fd-stream-misc-routine stream :finish-output)
1603 (when (fd-stream-delete-original stream)
1604 (delete-original (fd-stream-file stream)
1605 (fd-stream-original stream)))))
1606 (when (fboundp 'cancel-finalization)
1607 (cancel-finalization stream))
1608 (unix:unix-close (fd-stream-fd stream))
1609 (when (fd-stream-obuf-sap stream)
1610 (push (fd-stream-obuf-sap stream) *available-buffers*)
1611 (setf (fd-stream-obuf-sap stream) nil))
1612 (when (fd-stream-ibuf-sap stream)
1613 (push (fd-stream-ibuf-sap stream) *available-buffers*)
1614 (setf (fd-stream-ibuf-sap stream) nil))
1615 (lisp::set-closed-flame stream))
1616 (:clear-input
1617 (setf (fd-stream-unread stream) nil) ;;@@
1618 #+unicode (setf (fd-stream-last-char-read-size stream) 0)
1619 (setf (fd-stream-ibuf-head stream) 0)
1620 (setf (fd-stream-ibuf-tail stream) 0)
1621 (catch 'eof-input-catcher
1622 (loop
1623 (multiple-value-bind
1624 (count errno)
1625 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
1626 (unix:fd-zero read-fds)
1627 (unix:fd-set (fd-stream-fd stream) read-fds)
1628 (unix:unix-fast-select (1+ (fd-stream-fd stream))
1629 (alien:addr read-fds) nil nil 0 0))
1630 (cond ((eql count 1)
1631 (do-input stream)
1632 (setf (fd-stream-ibuf-head stream) 0)
1633 (setf (fd-stream-ibuf-tail stream) 0))
1634 ((and (not count) (eql errno unix:eintr)))
1635 (t
1636 (return t)))))))
1637 (:force-output
1638 (flush-output-buffer stream))
1639 (:finish-output
1640 (flush-output-buffer stream)
1641 (do ()
1642 ((null (fd-stream-output-later stream)))
1643 (system:serve-all-events)))
1644 (:element-type
1645 (fd-stream-element-type stream))
1646 (:interactive-p
1647 (unix:unix-isatty (fd-stream-fd stream)))
1648 (:line-length
1649 80)
1650 (:charpos
1651 (fd-stream-char-pos stream))
1652 (:file-length
1653 (unless (fd-stream-file stream)
1654 (error 'simple-type-error
1655 :datum stream
1656 :expected-type 'file-stream
1657 :format-control (intl:gettext "~s is not a stream associated with a file.")
1658 :format-arguments (list stream)))
1659 (multiple-value-bind
1660 (okay dev ino mode nlink uid gid rdev size
1661 atime mtime ctime blksize blocks)
1662 (unix:unix-fstat (fd-stream-fd stream))
1663 (declare (ignore ino nlink uid gid rdev
1664 atime mtime ctime blksize blocks))
1665 (unless okay
1666 (error 'simple-file-error
1667 :format-control (intl:gettext "Error fstating ~S: ~A")
1668 :format-arguments (list stream (unix:get-unix-error-msg dev))))
1669 (if (zerop mode)
1670 nil
1671 (values (truncate size (fd-stream-element-size stream))))))
1672 (:file-position
1673 (fd-stream-file-position stream arg1))))
1674
1675
1676 ;;; FD-STREAM-FILE-POSITION -- internal.
1677 ;;;
1678 (defun fd-stream-file-position (stream &optional newpos)
1679 (declare (type fd-stream stream)
1680 (type (or (integer 0) (member nil :start :end)) newpos))
1681 (if (null newpos)
1682 (system:without-interrupts
1683 ;; First, find the position of the UNIX file descriptor in the file.
1684 (multiple-value-bind
1685 (posn errno)
1686 (unix:unix-lseek (fd-stream-fd stream) 0 unix:l_incr)
1687 (declare (type (or (integer 0) null) posn))
1688 (cond (posn
1689 ;; Adjust for buffered output:
1690 ;; If there is any output buffered, the *real* file position
1691 ;; will be larger than reported by lseek because lseek
1692 ;; obviously cannot take into account output we have not
1693 ;; sent yet.
1694 (dolist (later (fd-stream-output-later stream))
1695 (incf posn (- (the index (caddr later))
1696 (the index (cadr later)))))
1697 (incf posn (fd-stream-obuf-tail stream))
1698 ;; Adjust for unread input:
1699 ;; If there is any input read from UNIX but not supplied to
1700 ;; the user of the stream, the *real* file position will
1701 ;; smaller than reported, because we want to look like the
1702 ;; unread stuff is still available.
1703 #-unicode
1704 (decf posn (- (fd-stream-ibuf-tail stream)
1705 (fd-stream-ibuf-head stream)))
1706 #+unicode
1707 (if (fd-stream-string-buffer stream)
1708 ;; The string buffer contains Lisp characters,
1709 ;; not octets! To figure out how many octets
1710 ;; have not been already supplied, we need to
1711 ;; count how many octets were consumed for all
1712 ;; the characters in the string bbuffer that have
1713 ;; not been supplied.
1714 (let ((ocount (fd-stream-octet-count stream)))
1715 (when ocount
1716 ;; Note: string-index starts at 1 (because
1717 ;; index 0 is for the unread-char), but
1718 ;; octet-count doesn't use that. Hence,
1719 ;; subtract one from string-index.
1720 (loop for k of-type fixnum from (1- (fd-stream-string-index stream))
1721 below (fd-stream-string-buffer-len stream)
1722 do (decf posn (aref ocount k)))))
1723 (decf posn (- (fd-stream-ibuf-tail stream)
1724 (fd-stream-ibuf-head stream))))
1725 (when (fd-stream-unread stream) ;;@@
1726 (decf posn))
1727 ;; Divide bytes by element size.
1728 (truncate posn (fd-stream-element-size stream)))
1729 ((eq errno unix:espipe)
1730 nil)
1731 (t
1732 (system:with-interrupts
1733 (error (intl:gettext "Error lseek'ing ~S: ~A")
1734 stream
1735 (unix:get-unix-error-msg errno)))))))
1736 (let ((offset 0)
1737 origin)
1738 (declare (type (integer 0) offset))
1739 ;; Make sure we don't have any output pending, because if we move the
1740 ;; file pointer before writing this stuff, it will be written in the
1741 ;; wrong location.
1742 (flush-output-buffer stream)
1743 (do ()
1744 ((null (fd-stream-output-later stream)))
1745 (system:serve-all-events))
1746 ;; Clear out any pending input to force the next read to go to the
1747 ;; disk.
1748 (setf (fd-stream-unread stream) nil) ;;@@
1749 #+unicode
1750 (progn
1751 (setf (fd-stream-last-char-read-size stream) 0)
1752 (setf (fd-stream-string-index stream)
1753 (fd-stream-string-buffer-len stream)))
1754 (setf (fd-stream-ibuf-head stream) 0)
1755 (setf (fd-stream-ibuf-tail stream) 0)
1756 ;; Trash cached value for listen, so that we check next time.
1757 (setf (fd-stream-listen stream) nil)
1758 ;; Now move it.
1759 (cond ((eq newpos :start)
1760 (setf offset 0
1761 origin unix:l_set))
1762 ((eq newpos :end)
1763 (setf offset 0
1764 origin unix:l_xtnd))
1765 ((typep newpos '(integer 0))
1766 (setf offset (* newpos (fd-stream-element-size stream))
1767 origin unix:l_set))
1768 (t
1769 (error (intl:gettext "Invalid position given to file-position: ~S") newpos)))
1770 (multiple-value-bind
1771 (posn errno)
1772 (unix:unix-lseek (fd-stream-fd stream) offset origin)
1773 (cond (posn
1774 t)
1775 ((eq errno unix:espipe)
1776 nil)
1777 (t
1778 (error (intl:gettext "Error lseek'ing ~S: ~A")
1779 stream
1780 (unix:get-unix-error-msg errno))))))))
1781
1782
1783
1784 ;;;; Creation routines (MAKE-FD-STREAM and OPEN)
1785
1786 ;;; MAKE-FD-STREAM -- Public.
1787 ;;;
1788 ;;; Returns a FD-STREAM on the given file.
1789 ;;;
1790 (defun make-fd-stream (fd
1791 &key
1792 (input nil input-p)
1793 (output nil output-p)
1794 (element-type 'base-char)
1795 (buffering :full)
1796 timeout
1797 file
1798 original
1799 delete-original
1800 pathname
1801 input-buffer-p
1802 ;; DO NOT translate these! It causes an
1803 ;; infinite loop. We need to open a file for
1804 ;; the translations, but if you translate
1805 ;; these, then we need to do a lookup which
1806 ;; wants to open the mo file which calls this
1807 ;; to name which causes a lookup ....
1808 (name (if file
1809 (format nil "file ~S" file)
1810 (format nil "descriptor ~D" fd)))
1811 auto-close
1812 (external-format :default)
1813 binary-stream-p
1814 decoding-error
1815 encoding-error)
1816 (declare (type index fd) (type (or index null) timeout)
1817 (type (member :none :line :full) buffering))
1818 "Create a stream for the given unix file descriptor.
1819 If input is non-nil, allow input operations.
1820 If output is non-nil, allow output operations.
1821 If neither input nor output are specified, default to allowing input.
1822 Element-type indicates the element type to use (as for open).
1823 Buffering indicates the kind of buffering to use.
1824 Timeout (if true) is the number of seconds to wait for input. If NIL (the
1825 default), then wait forever. When we time out, we signal IO-TIMEOUT.
1826 File is the name of the file (will be returned by PATHNAME).
1827 Name is used to identify the stream when printed.
1828 External-format is the external format to use for the stream.
1829 Decoding-error and Encoding-error indicate how decoding/encoding errors on
1830 the stream should be handled. The default is to use a replacement character."
1831 (cond ((not (or input-p output-p))
1832 (setf input t))
1833 ((not (or input output))
1834 (error (intl:gettext "File descriptor must be opened either for input or output."))))
1835 (let ((stream (if binary-stream-p
1836 (%make-binary-text-stream :fd fd
1837 :name name
1838 :file file
1839 :original original
1840 :delete-original delete-original
1841 :pathname pathname
1842 :buffering buffering
1843 :timeout timeout)
1844 (let ((e (cond ((characterp encoding-error)
1845 (constantly (char-code encoding-error)))
1846 (t
1847 encoding-error)))
1848 (d (cond ((characterp decoding-error)
1849 (constantly (char-code decoding-error)))
1850 ((eq t decoding-error)
1851 #+unicode
1852 #'(lambda (&rest args)
1853 (apply 'cerror
1854 (intl:gettext "Use Unicode replacement character instead")
1855 args)
1856 stream:+replacement-character-code+)
1857 #-unicode
1858 #'(lambda (&rest args)
1859 (apply 'cerror
1860 (intl:gettext "Use question mark character instead")
1861 args)
1862 #\?))
1863 (t
1864 decoding-error))))
1865 (%make-fd-stream :fd fd
1866 :name name
1867 :file file
1868 :original original
1869 :delete-original delete-original
1870 :pathname pathname
1871 :buffering buffering
1872 :timeout timeout
1873 :char-to-octets-error e
1874 :octets-to-char-error d)))))
1875 ;; FIXME: setting the external format here should be better
1876 ;; integrated into set-routines. We do it before so that
1877 ;; set-routines can create an in-buffer if appropriate. But we
1878 ;; need to do it after to put the correct input routines for the
1879 ;; external format.
1880 ;;
1881 ;;#-unicode-bootstrap ; fails in stream-reinit otherwise
1882 #+(and unicode (not unicode-bootstrap))
1883 (%set-fd-stream-external-format stream external-format nil)
1884 (set-routines stream element-type input output input-buffer-p
1885 :binary-stream-p binary-stream-p)
1886 #+(and unicode (not unicode-bootstrap))
1887 (%set-fd-stream-external-format stream external-format nil)
1888 (when (and auto-close (fboundp 'finalize))
1889 (finalize stream
1890 #'(lambda ()
1891 (unix:unix-close fd)
1892 (format *terminal-io* (intl:gettext "** Closed ~A~%") name)
1893 (when original
1894 (revert-file file original)))))
1895 stream))
1896
1897
1898 ;;; PICK-BACKUP-NAME -- internal
1899 ;;;
1900 ;;; Pick a name to use for the backup file.
1901 ;;;
1902 (defvar *backup-extension* ".BAK"
1903 "This is a string that OPEN tacks on the end of a file namestring to produce
1904 a name for the :if-exists :rename-and-delete and :rename options. Also,
1905 this can be a function that takes a namestring and returns a complete
1906 namestring.")
1907 ;;;
1908 (defun pick-backup-name (name)
1909 (declare (type simple-string name))
1910 (let ((ext *backup-extension*))
1911 (etypecase ext
1912 (simple-string (concatenate 'simple-string name ext))
1913 (function (funcall ext name)))))
1914
1915 ;;; NEXT-VERSION -- internal
1916 ;;;
1917 ;;; Find the next available versioned name for a file.
1918 ;;;
1919 (defun next-version (name)
1920 (declare (type simple-string name))
1921 (let* ((sep (position #\/ name :from-end t))
1922 (base (if sep (subseq name 0 (1+ sep)) ""))
1923 (dir (unix:open-dir base)))
1924 (multiple-value-bind (name type version)
1925 (extract-name-type-and-version name (if sep (1+ sep) 0) (length name))
1926 (let ((version (if (symbolp version) 1 (1+ version)))
1927 (match (if type
1928 (concatenate 'string name "." type ".~")
1929 (concatenate 'string name ".~"))))
1930 (when dir
1931 (unwind-protect
1932 (loop
1933 (let ((name (unix:read-dir dir)))
1934 (cond ((null name) (return))
1935 ((and (> (length name) (length match))
1936 (string= name match :end1 (length match)))
1937 (multiple-value-bind (v e)
1938 (parse-integer name :start (length match)
1939 :junk-allowed t)
1940 (when (and v
1941 (= (length name) (1+ e))
1942 (char= (schar name e) #\~))
1943 (setq version (max version (1+ v)))))))))
1944 (unix:close-dir dir)))
1945 (concatenate 'string base
1946 match (quick-integer-to-string version) "~")))))
1947
1948 ;;; ASSURE-ONE-OF -- internal
1949 ;;;
1950 ;;; Assure that the given arg is one of the given list of valid things.
1951 ;;; Allow the user to fix any problems.
1952 ;;;
1953 (defun assure-one-of (item list what)
1954 (unless (member item list)
1955 (loop
1956 (cerror (intl:gettext "Enter new value for ~*~S")
1957 (intl:gettext "~S is invalid for ~S. Must be one of~{ ~S~}")
1958 item
1959 what
1960 list)
1961 (format (the stream *query-io*) (intl:gettext "Enter new value for ~S: ") what)
1962 (force-output *query-io*)
1963 (setf item (read *query-io*))
1964 (when (member item list)
1965 (return))))
1966 item)
1967
1968 ;;; DO-OLD-RENAME -- Internal
1969 ;;;
1970 ;;; Rename Namestring to Original. First, check if we have write access,
1971 ;;; since we don't want to trash unwritable files even if we technically can.
1972 ;;; We return true if we succeed in renaming.
1973 ;;;
1974 (defun do-old-rename (namestring original)
1975 (unless (unix:unix-access namestring unix:w_ok)
1976 (cerror (intl:gettext "Try to rename it anyway.") (intl:gettext "File ~S is not writable.") namestring))
1977 (multiple-value-bind
1978 (okay err)
1979 (unix:unix-rename namestring original)
1980 (cond (okay t)
1981 (t
1982 (cerror (intl:gettext "Use :SUPERSEDE instead.")
1983 (intl:gettext "Could not rename ~S to ~S: ~A.")
1984 namestring
1985 original
1986 (unix:get-unix-error-msg err))
1987 nil))))
1988
1989 ;;; FD-OPEN -- Internal
1990 ;;;
1991 ;;; Open a file.
1992 ;;;
1993 (defun fd-open (pathname direction if-exists if-exists-given
1994 if-does-not-exist if-does-not-exist-given)
1995 (declare (type pathname pathname)
1996 (type (member :input :output :io :probe) direction)
1997 (type (member :error :new-version :rename :rename-and-delete
1998 :overwrite :append :supersede nil) if-exists)
1999 (type (member :error :create nil) if-does-not-exist))
2000 (multiple-value-bind (input output mask)
2001 (ecase direction
2002 (:input (values t nil unix:o_rdonly))
2003 (:output (values nil t unix:o_wronly))
2004 (:io (values t t unix:o_rdwr))
2005 (:probe (values t nil unix:o_rdonly)))
2006 (declare (type index mask))
2007 ;; Process if-exists argument if we are doing any output.
2008 (cond (output
2009 (unless if-exists-given
2010 (setf if-exists
2011 (if (eq (pathname-version pathname) :newest)
2012 :new-version
2013 :error)))
2014 (case if-exists
2015 ((:error nil)
2016 (setf mask (logior mask unix:o_excl)))
2017 ((:new-version :rename :rename-and-delete)
2018 (setf mask (logior mask unix:o_creat)))
2019 (:supersede
2020 (setf mask (logior mask unix:o_trunc)))))
2021 (t
2022 (setf if-exists nil))) ; :ignore-this-arg
2023
2024 (unless if-does-not-exist-given
2025 (setf if-does-not-exist
2026 (cond ((eq direction :input) :error)
2027 ((and output
2028 (member if-exists '(:overwrite :append)))
2029 :error)
2030 ((eq direction :probe)
2031 nil)
2032 (t
2033 :create))))
2034 (if (eq if-does-not-exist :create)
2035 (setf mask (logior mask unix:o_creat)))
2036
2037 (let ((name (cond ((unix-namestring pathname input))
2038 ((and input (eq if-does-not-exist :create))
2039 (unix-namestring pathname nil)))))
2040 (let ((original (cond ((eq if-exists :new-version)
2041 (next-version name))
2042 ((member if-exists '(:rename :rename-and-delete))
2043 (pick-backup-name name))))
2044 (delete-original (eq if-exists :rename-and-delete))
2045 (mode #o666))
2046 (when original
2047 ;; We are doing a :rename or :rename-and-delete.
2048 ;; Determine if the file already exists, make sure the original
2049 ;; file is not a directory and keep the mode
2050 (let ((exists
2051 (and name
2052 (multiple-value-bind
2053 (okay err/dev inode orig-mode)
2054 (unix:unix-stat name)
2055 (declare (ignore inode)
2056 (type (or index null) orig-mode))
2057 (cond
2058 (okay
2059 (when (and output (= (logand orig-mode #o170000)
2060 #o40000))
2061 (error 'simple-file-error
2062 :pathname pathname
2063 :format-control
2064 (intl:gettext "Cannot open ~S for output: Is a directory.")
2065 :format-arguments (list name)))
2066 (setf mode (logand orig-mode #o777))
2067 t)
2068 ((eql err/dev unix:enoent)
2069 nil)
2070 (t
2071 (error 'simple-file-error
2072 :pathname pathname
2073 :format-control (intl:gettext "Cannot find ~S: ~A")
2074 :format-arguments
2075 (list name
2076 (unix:get-unix-error-msg err/dev)))))))))
2077 (unless (and exists
2078 (do-old-rename name original))
2079 (setf original nil)
2080 (setf delete-original nil)
2081 ;; In order to use SUPERSEDE instead, we have
2082 ;; to make sure unix:o_creat corresponds to
2083 ;; if-does-not-exist. unix:o_creat was set
2084 ;; before because of if-exists being :rename.
2085 (unless (eq if-does-not-exist :create)
2086 (setf mask (logior (logandc2 mask unix:o_creat)
2087 unix:o_trunc)))
2088 (setf if-exists :supersede))))
2089
2090 ;; Okay, now we can try the actual open.
2091 (loop
2092 (multiple-value-bind (fd errno)
2093 (if name
2094 (unix:unix-open name mask mode)
2095 (values nil unix:enoent))
2096 (cond ((fixnump fd)
2097 (when (eq if-exists :append)
2098 (unix:unix-lseek fd 0 unix:l_xtnd)) ; SEEK_END
2099 (return (values fd name original delete-original)))
2100 ((eql errno unix:enoent)
2101 (case if-does-not-exist
2102 (:error
2103 (cerror (intl:gettext "Return NIL.")
2104 'simple-file-error
2105 :pathname pathname
2106 :format-control (intl:gettext "Error opening ~S, ~A.")
2107 :format-arguments
2108 (list pathname
2109 (unix:get-unix-error-msg errno))))
2110 (:create
2111 (cerror (intl:gettext "Return NIL.")
2112 'simple-file-error
2113 :pathname pathname
2114 :format-control
2115 (intl:gettext "Error creating ~S, path does not exist.")
2116 :format-arguments (list pathname))))
2117 (return nil))
2118 ((eql errno unix:eexist)
2119 (unless (eq nil if-exists)
2120 (cerror (intl:gettext "Return NIL.")
2121 'simple-file-error
2122 :pathname pathname
2123 :format-control (intl:gettext "Error opening ~S, ~A.")
2124 :format-arguments
2125 (list pathname
2126 (unix:get-unix-error-msg errno))))
2127 (return nil))
2128 ((eql errno unix:eacces)
2129 (cerror (intl:gettext "Try again.")
2130 'simple-file-error
2131 :pathname pathname
2132 :format-control (intl:gettext "Error opening ~S, ~A.")
2133 :format-arguments
2134 (list pathname
2135 (unix:get-unix-error-msg errno))))
2136 (t
2137 (cerror (intl:gettext "Return NIL.")
2138 'simple-file-error
2139 :pathname pathname
2140 :format-control (intl:gettext "Error opening ~S, ~A.")
2141 :format-arguments
2142 (list pathname
2143 (unix:get-unix-error-msg errno)))
2144 (return nil)))))))))
2145
2146 ;;; OPEN-FD-STREAM -- Internal
2147 ;;;
2148 ;;; Open an fd-stream connected to a file.
2149 ;;;
2150 (defun open-fd-stream (pathname &key (direction :input)
2151 (element-type 'base-char)
2152 (if-exists nil if-exists-given)
2153 (if-does-not-exist nil if-does-not-exist-given)
2154 (external-format :default)
2155 class
2156 decoding-error encoding-error)
2157 (declare (type pathname pathname)
2158 (type (member :input :output :io :probe) direction)
2159 (type (member :error :new-version :rename :rename-and-delete
2160 :overwrite :append :supersede nil) if-exists)
2161 (type (member :error :create nil) if-does-not-exist))
2162 (multiple-value-bind (fd namestring original delete-original)
2163 (fd-open pathname direction if-exists if-exists-given
2164 if-does-not-exist if-does-not-exist-given)
2165 (when fd
2166 (case direction
2167 ((:input :output :io)
2168 ;; We use the :class option to tell us if we want a
2169 ;; binary-text stream or not.
2170 (make-fd-stream fd
2171 :input (member direction '(:input :io))
2172 :output (member direction '(:output :io))
2173 :element-type element-type
2174 :file namestring
2175 :original original
2176 :delete-original delete-original
2177 :pathname pathname
2178 :input-buffer-p t
2179 :auto-close t
2180 :external-format external-format
2181 :binary-stream-p class
2182 :decoding-error decoding-error
2183 :encoding-error encoding-error))
2184 (:probe
2185 (let ((stream (%make-fd-stream :name namestring :fd fd
2186 :pathname pathname
2187 :element-type element-type)))
2188 (close stream)
2189 stream))))))
2190
2191 ;;; OPEN -- public
2192 ;;;
2193 ;;; Open the given file.
2194 ;;;
2195 (defun open (filename &rest options
2196 &key (direction :input)
2197 (element-type 'base-char element-type-given)
2198 (if-exists nil if-exists-given)
2199 (if-does-not-exist nil if-does-not-exist-given)
2200 (external-format :default)
2201 class mapped input-handle output-handle
2202 decoding-error encoding-error
2203 &allow-other-keys
2204 &aux ; Squelch assignment warning.
2205 (options options)
2206 (direction direction)
2207 (if-does-not-exist if-does-not-exist)
2208 (if-exists if-exists))
2209 "Return a stream which reads from or writes to Filename.
2210 Defined keywords:
2211 :direction - one of :input, :output, :io, or :probe
2212 :element-type - Type of object to read or write, default BASE-CHAR
2213 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
2214 :overwrite, :append, :supersede or nil
2215 :if-does-not-exist - one of :error, :create or nil
2216 :external-format - an external format name
2217 :decoding-error - How to handle decoding errors from the external format.
2218 If a character, then that character is used as
2219 the replacment character for all errors. If T,
2220 then a continuable error is signaled. If
2221 continued, the Unicode replacement character is
2222 used. Otherwise, it should be a symbol or
2223 function of 3 arguments. If it returns, it
2224 should return a code point to use as the
2225 replacment. The function arguments are a
2226 format message string, the offending octet, and
2227 the number of octets read in the current
2228 encoding.
2229 :encoding-error - Like :decoding-error, but for errors when encoding the
2230 stream. If a character, that character is used
2231 as the replacment code point. Otherwise, it
2232 should be a symbol or function oof two
2233 arguments: a format message string and the
2234 incorrect codepoint.
2235
2236 See the manual for details."
2237 (declare (ignore element-type external-format input-handle output-handle
2238 decoding-error encoding-error))
2239
2240 ;; OPEN signals a file-error if the filename is wild.
2241 (when (wild-pathname-p filename)
2242 (error 'file-error :pathname filename))
2243
2244 ;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
2245 (setq direction
2246 (assure-one-of direction
2247 '(:input :output :io :probe)
2248 :direction))
2249 (setf (getf options :direction) direction)
2250
2251 (when (and if-exists-given (member direction '(:output :io)))
2252 (setq if-exists
2253 (assure-one-of if-exists
2254 '(:error :new-version :rename
2255 :rename-and-delete :overwrite
2256 :append :supersede nil)
2257 :if-exists))
2258 (setf (getf options :if-exists) if-exists))
2259
2260 (when if-does-not-exist-given
2261 (setq if-does-not-exist
2262 (assure-one-of if-does-not-exist
2263 '(:error :create nil)
2264 :if-does-not-exist))
2265 (setf (getf options :if-does-not-exist) if-does-not-exist))
2266
2267 (let ((filespec (merge-pathnames filename))
2268 (options (copy-list options))
2269 (class (or class 'fd-stream)))
2270 (cond ((eq class 'fd-stream)
2271 (remf options :class)
2272 (remf options :mapped)
2273 (remf options :input-handle)
2274 (remf options :output-handle)
2275 (apply #'open-fd-stream filespec options))
2276 ((eq class 'binary-text-stream)
2277 ;; Like fd-stream, but binary and text allowed. This is
2278 ;; indicated by leaving the :class option around for
2279 ;; open-fd-stream to see.
2280 (remf options :mapped)
2281 (remf options :input-handle)
2282 (remf options :output-handle)
2283 (apply #'open-fd-stream filespec options))
2284 ((subtypep class 'stream:simple-stream)
2285 (when element-type-given
2286 (cerror (intl:gettext "Do it anyway.")
2287 (intl:gettext "Can't create simple-streams with an element-type.")))
2288 (when (and (eq class 'stream:file-simple-stream) mapped)
2289 (setq class 'stream:mapped-file-simple-stream)
2290 (setf (getf options :class) 'stream:mapped-file-simple-stream))
2291 (when (subtypep class 'stream:file-simple-stream)
2292 (when (eq direction :probe)
2293 (setq class 'stream:probe-simple-stream)))
2294 (apply #'make-instance class :filename filespec options))
2295 ((subtypep class 'ext:fundamental-stream)
2296 (remf options :class)
2297 (remf options :mapped)
2298 (remf options :input-handle)
2299 (remf options :output-handle)
2300 (let ((stream (apply #'open-fd-stream filespec options)))
2301 (when stream
2302 (make-instance class :lisp-stream stream))))
2303 (t
2304 (error (intl:gettext "Unable to open streams of class ~S.") class)))))
2305
2306 ;;;; Initialization.
2307
2308 (defvar *tty* nil
2309 "The stream connected to the controlling terminal or NIL if there is none.")
2310 (defvar *stdin* nil
2311 "The stream connected to the standard input (file descriptor 0).")
2312 (defvar *stdout* nil
2313 "The stream connected to the standard output (file descriptor 1).")
2314 (defvar *stderr* nil
2315 "The stream connected to the standard error output (file descriptor 2).")
2316
2317 ;;; STREAM-INIT -- internal interface
2318 ;;;
2319 ;;; Called when the cold load is first started up.
2320 ;;;
2321 (defun stream-init ()
2322 (stream-reinit)
2323 (setf *terminal-io* (make-synonym-stream '*tty*))
2324 (setf *standard-output* (make-synonym-stream '*stdout*))
2325 (setf *standard-input*
2326 (make-two-way-stream (make-synonym-stream '*stdin*)
2327 *standard-output*))
2328 (setf *error-output* (make-synonym-stream '*stderr*))
2329 (setf *query-io* (make-synonym-stream '*terminal-io*))
2330 (setf *debug-io* *query-io*)
2331 (setf *trace-output* *standard-output*)
2332 nil)
2333
2334 ;;; STREAM-REINIT -- internal interface
2335 ;;;
2336 ;;; Called whenever a saved core is restarted.
2337 ;;;
2338 (defun stream-reinit ()
2339 (setf *available-buffers* nil)
2340 (setf *stdin*
2341 (make-fd-stream 0 :name "Standard Input" :input t :buffering :line
2342 :external-format :iso8859-1))
2343 (setf *stdout*
2344 (make-fd-stream 1 :name "Standard Output" :output t :buffering :line
2345 :external-format :iso8859-1))
2346 (setf *stderr*
2347 (make-fd-stream 2 :name "Standard Error" :output t :buffering :line
2348 :external-format :iso8859-1))
2349 (let ((tty (and (not *batch-mode*)
2350 (unix:unix-open "/dev/tty" unix:o_rdwr #o666))))
2351 (setf *tty*
2352 (if tty
2353 (make-fd-stream tty :name "the Terminal" :input t :output t
2354 :buffering :line :auto-close t
2355 :external-format :iso8859-1)
2356 (make-two-way-stream *stdin* *stdout*))))
2357 nil)
2358
2359
2360 ;;;; Beeping.
2361
2362 (defun default-beep-function (stream)
2363 (write-char #\bell stream)
2364 (finish-output stream))
2365
2366 (defvar *beep-function* #'default-beep-function
2367 "This is called in BEEP to feep the user. It takes a stream.")
2368
2369 (defun beep (&optional (stream *terminal-io*))
2370 (funcall *beep-function* stream))
2371
2372
2373 ;;; File-Name -- internal interface
2374 ;;;
2375 ;;; Kind of like File-Position, but is an internal hack used by the filesys
2376 ;;; stuff to get and set the file name.
2377 ;;;
2378 (defun file-name (stream &optional new-name)
2379 (typecase stream
2380 (stream:simple-stream
2381 (if new-name
2382 (stream::%file-rename stream new-name)
2383 (stream::%file-name stream)))
2384 (fd-stream
2385 (cond (new-name
2386 (setf (fd-stream-pathname stream) new-name)
2387 (setf (fd-stream-file stream)
2388 (unix-namestring new-name nil))
2389 t)
2390 (t
2391 (fd-stream-pathname stream))))))
2392
2393
2394
2395 #+unicode
2396 (stream::def-ef-macro ef-strlen (extfmt lisp stream::+ef-max+ stream::+ef-str+)
2397 ;; While it would be nice not to have to call CHAR-TO-OCTETS to
2398 ;; figure out the length when the external format has fixed size
2399 ;; outputs, we can't. For example, utf16 will output a BOM, which
2400 ;; wouldn't be reflected in the count if we don't call
2401 ;; CHAR-TO-OCTETS.
2402 `(lambda (stream object &aux (count 0))
2403 (declare (type fd-stream stream)
2404 (type (or character string) object)
2405 (type (and fixnum unsigned-byte) count)
2406 #|(optimize (speed 3) (space 0) (debug 0) (safety 0))|#)
2407 (labels ((efstate (state)
2408 (stream::copy-state ,extfmt state))
2409 (eflen (char)
2410 (stream::char-to-octets ,extfmt char
2411 (fd-stream-co-state stream)
2412 (lambda (byte)
2413 (declare (ignore byte))
2414 (incf count))
2415 (fd-stream-char-to-octets-erroor stream))))
2416 (let* ((co-state (fd-stream-co-state stream))
2417 (old-ef-state (efstate (cdr (fd-stream-co-state stream))))
2418 (old-state (cons (car co-state) old-ef-state)))
2419 (etypecase object
2420 (character (eflen object))
2421 (string (dovector (ch object) (eflen ch))))
2422 ;; Restore state
2423 (setf (fd-stream-co-state stream) old-state)
2424 count))))
2425
2426
2427 (defun file-string-length (stream object)
2428 (declare (type (or string character) object)
2429 (type (or file-stream broadcast-stream stream:simple-stream) stream))
2430 "Return the delta in Stream's FILE-POSITION that would be caused by writing
2431 Object to Stream. Non-trivial only in implementations that support
2432 international character sets."
2433 (typecase stream
2434 (stream:simple-stream (stream::%file-string-length stream object))
2435 (broadcast-stream
2436 ;; CLHS says we must return 1 in this case
2437 1)
2438 #+unicode
2439 (t (funcall (ef-strlen (fd-stream-external-format stream))
2440 stream object))
2441 #-unicode
2442 (t (etypecase object
2443 (character 1)
2444 (string (length object))))))
2445
2446 #+unicode
2447 (stream::def-ef-macro ef-copy-state (extfmt lisp stream::+ef-max+ stream::+ef-copy-state+)
2448 ;; Return a copy of the state of an external format.
2449 `(lambda (state)
2450 (declare (ignorable state))
2451 (stream::copy-state ,extfmt state)))

  ViewVC Help
Powered by ViewVC 1.1.5