1 ;;;; streams for UNIX file descriptors
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; buffer manipulation routines
16 ;;; FIXME: Is it really good to maintain this pool separate from the
17 ;;; GC and the C malloc logic?
18 (defvar *available-buffers* ()
20 "List of available buffers. Each buffer is an sap pointing to
21 bytes-per-buffer of memory.")
23 (defconstant bytes-per-buffer (* 4 1024)
25 "Number of bytes per buffer.")
27 ;;; Return the next available buffer, creating one if necessary.
28 #!-sb-fluid (declaim (inline next-available-buffer))
29 (defun next-available-buffer ()
30 (if *available-buffers*
31 (pop *available-buffers*)
32 (allocate-system-memory bytes-per-buffer)))
34 ;;;; the FD-STREAM structure
37 (:constructor %make-fd-stream)
38 (:conc-name fd-stream-)
39 (:predicate fd-stream-p)
41 (misc #'fd-stream-misc-routine))
44 ;; the name of this stream
46 ;; the file this stream is for
48 ;; the backup file namestring for the old file, for :IF-EXISTS
49 ;; :RENAME or :RENAME-AND-DELETE.
50 (original nil :type (or simple-string null))
51 (delete-original nil) ; for :if-exists :rename-and-delete
52 ;;; the number of bytes per element
53 (element-size 1 :type index)
54 ;; the type of element being transfered
55 (element-type 'base-char)
56 ;; the Unix file descriptor
58 ;; controls when the output buffer is flushed
59 (buffering :full :type (member :full :line :none))
60 ;; character position (if known)
61 (char-pos nil :type (or index null))
62 ;; T if input is waiting on FD. :EOF if we hit EOF.
63 (listen nil :type (member nil t :eof))
67 (ibuf-sap nil :type (or system-area-pointer null))
68 (ibuf-length nil :type (or index null))
69 (ibuf-head 0 :type index)
70 (ibuf-tail 0 :type index)
73 (obuf-sap nil :type (or system-area-pointer null))
74 (obuf-length nil :type (or index null))
75 (obuf-tail 0 :type index)
77 ;; output flushed, but not written due to non-blocking io?
80 ;; timeout specified for this stream, or NIL if none
81 (timeout nil :type (or index null))
82 ;; pathname of the file this stream is opened to (returned by PATHNAME)
83 (pathname nil :type (or pathname null))
84 (external-format :default)
85 (output-bytes #'ill-out :type function))
86 (def!method print-object ((fd-stream fd-stream) stream)
87 (declare (type stream stream))
88 (print-unreadable-object (fd-stream stream :type t :identity t)
89 (format stream "for ~S" (fd-stream-name fd-stream))))
91 ;;;; output routines and related noise
93 (defvar *output-routines* ()
95 "List of all available output routines. Each element is a list of the
96 element-type output, the kind of buffering, the function name, and the number
97 of bytes per element.")
99 ;;; common idioms for reporting low-level stream and file problems
100 (defun simple-stream-perror (note-format stream errno)
101 (error 'simple-stream-error
103 :format-control "~@<~?: ~2I~_~A~:>"
104 :format-arguments (list note-format (list stream) (strerror errno))))
105 (defun simple-file-perror (note-format pathname errno)
106 (error 'simple-file-error
108 :format-control "~@<~?: ~2I~_~A~:>"
110 (list note-format (list pathname) (strerror errno))))
112 (defun stream-decoding-error (stream octets)
113 (error 'stream-decoding-error
115 ;; FIXME: dunno how to get at OCTETS currently, or even if
116 ;; that's the right thing to report.
118 (defun stream-encoding-error (stream code)
119 (error 'stream-encoding-error
123 ;;; This is called by the server when we can write to the given file
124 ;;; descriptor. Attempt to write the data again. If it worked, remove
125 ;;; the data from the OUTPUT-LATER list. If it didn't work, something
127 (defun frob-output-later (stream)
128 (let* ((stuff (pop (fd-stream-output-later stream)))
132 (reuse-sap (cadddr stuff))
133 (length (- end start)))
134 (declare (type index start end length))
135 (multiple-value-bind (count errno)
136 (sb!unix:unix-write (fd-stream-fd stream)
141 (if (= errno sb!unix:ewouldblock)
142 (error "Write would have blocked, but SERVER told us to go.")
143 (simple-stream-perror "couldn't write to ~S" stream errno)))
144 ((eql count length) ; Hot damn, it worked.
146 (push base *available-buffers*)))
147 ((not (null count)) ; sorta worked..
149 (the index (+ start count))
151 (fd-stream-output-later stream))))))
152 (unless (fd-stream-output-later stream)
153 (sb!sys:remove-fd-handler (fd-stream-handler stream))
154 (setf (fd-stream-handler stream) nil)))
156 ;;; Arange to output the string when we can write on the file descriptor.
157 (defun output-later (stream base start end reuse-sap)
158 (cond ((null (fd-stream-output-later stream))
159 (setf (fd-stream-output-later stream)
160 (list (list base start end reuse-sap)))
161 (setf (fd-stream-handler stream)
162 (sb!sys:add-fd-handler (fd-stream-fd stream)
165 (declare (ignore fd))
166 (frob-output-later stream)))))
168 (nconc (fd-stream-output-later stream)
169 (list (list base start end reuse-sap)))))
171 (let ((new-buffer (next-available-buffer)))
172 (setf (fd-stream-obuf-sap stream) new-buffer)
173 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
175 ;;; Output the given noise. Check to see whether there are any pending
176 ;;; writes. If so, just queue this one. Otherwise, try to write it. If
177 ;;; this would block, queue it.
178 (defun frob-output (stream base start end reuse-sap)
179 (declare (type fd-stream stream)
180 (type (or system-area-pointer (simple-array * (*))) base)
181 (type index start end))
182 (if (not (null (fd-stream-output-later stream))) ; something buffered.
184 (output-later stream base start end reuse-sap)
185 ;; ### check to see whether any of this noise can be output
187 (let ((length (- end start)))
188 (multiple-value-bind (count errno)
189 (sb!unix:unix-write (fd-stream-fd stream) base start length)
191 (if (= errno sb!unix:ewouldblock)
192 (output-later stream base start end reuse-sap)
193 (simple-stream-perror "couldn't write to ~S"
196 ((not (eql count length))
197 (output-later stream base (the index (+ start count))
200 ;;; Flush any data in the output buffer.
201 (defun flush-output-buffer (stream)
202 (let ((length (fd-stream-obuf-tail stream)))
204 (frob-output stream (fd-stream-obuf-sap stream) 0 length t)
205 (setf (fd-stream-obuf-tail stream) 0))))
207 (defmacro output-wrapper/variable-width ((stream size buffering restart)
209 (let ((stream-var (gensym)))
210 `(let ((,stream-var ,stream)
212 ,(unless (eq (car buffering) :none)
213 `(when (< (fd-stream-obuf-length ,stream-var)
214 (+ (fd-stream-obuf-tail ,stream-var)
216 (flush-output-buffer ,stream-var)))
217 ,(unless (eq (car buffering) :none)
218 `(when (> (fd-stream-ibuf-tail ,stream-var)
219 (fd-stream-ibuf-head ,stream-var))
220 (file-position ,stream-var (file-position ,stream-var))))
223 `(with-simple-restart (output-nothing
224 "~@<Skip output of this character.~@:>")
226 (incf (fd-stream-obuf-tail ,stream-var) size))
229 (incf (fd-stream-obuf-tail ,stream-var) size)))
230 ,(ecase (car buffering)
232 `(flush-output-buffer ,stream-var))
234 `(when (eq (char-code byte) (char-code #\Newline))
235 (flush-output-buffer ,stream-var)))
239 (defmacro output-wrapper ((stream size buffering restart) &body body)
240 (let ((stream-var (gensym)))
241 `(let ((,stream-var ,stream))
242 ,(unless (eq (car buffering) :none)
243 `(when (< (fd-stream-obuf-length ,stream-var)
244 (+ (fd-stream-obuf-tail ,stream-var)
246 (flush-output-buffer ,stream-var)))
247 ,(unless (eq (car buffering) :none)
248 `(when (> (fd-stream-ibuf-tail ,stream-var)
249 (fd-stream-ibuf-head ,stream-var))
250 (file-position ,stream-var (file-position ,stream-var))))
252 `(with-simple-restart (output-nothing
253 "~@<Skip output of this character.~@:>")
255 (incf (fd-stream-obuf-tail ,stream-var) ,size))
258 (incf (fd-stream-obuf-tail ,stream-var) ,size)))
259 ,(ecase (car buffering)
261 `(flush-output-buffer ,stream-var))
263 `(when (eq (char-code byte) (char-code #\Newline))
264 (flush-output-buffer ,stream-var)))
268 (defmacro def-output-routines/variable-width
269 ((name-fmt size restart external-format &rest bufferings)
271 (declare (optimize (speed 1)))
276 (intern (let ((*print-case* :upcase))
277 (format nil name-fmt (car buffering))))))
279 (defun ,function (stream byte)
280 (output-wrapper/variable-width (stream ,size ,buffering ,restart)
282 (setf *output-routines*
283 (nconc *output-routines*
291 (cdr buffering)))))))
294 ;;; Define output routines that output numbers SIZE bytes long for the
295 ;;; given bufferings. Use BODY to do the actual output.
296 (defmacro def-output-routines ((name-fmt size restart &rest bufferings)
298 (declare (optimize (speed 1)))
303 (intern (let ((*print-case* :upcase))
304 (format nil name-fmt (car buffering))))))
306 (defun ,function (stream byte)
307 (output-wrapper (stream ,size ,buffering ,restart)
309 (setf *output-routines*
310 (nconc *output-routines*
318 (cdr buffering)))))))
321 ;;; FIXME: is this used anywhere any more?
322 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
328 (if (char= byte #\Newline)
329 (setf (fd-stream-char-pos stream) 0)
330 (incf (fd-stream-char-pos stream)))
331 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
334 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
337 (:none (unsigned-byte 8))
338 (:full (unsigned-byte 8)))
339 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
342 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
345 (:none (signed-byte 8))
346 (:full (signed-byte 8)))
347 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
348 (fd-stream-obuf-tail stream))
351 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
354 (:none (unsigned-byte 16))
355 (:full (unsigned-byte 16)))
356 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
359 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
362 (:none (signed-byte 16))
363 (:full (signed-byte 16)))
364 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
365 (fd-stream-obuf-tail stream))
368 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
371 (:none (unsigned-byte 32))
372 (:full (unsigned-byte 32)))
373 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
376 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
379 (:none (signed-byte 32))
380 (:full (signed-byte 32)))
381 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
382 (fd-stream-obuf-tail stream))
385 ;;; Do the actual output. If there is space to buffer the string,
386 ;;; buffer it. If the string would normally fit in the buffer, but
387 ;;; doesn't because of other stuff in the buffer, flush the old noise
388 ;;; out of the buffer and put the string in it. Otherwise we have a
389 ;;; very long string, so just send it directly (after flushing the
390 ;;; buffer, of course).
391 (defun output-raw-bytes (fd-stream thing &optional start end)
393 "Output THING to FD-STREAM. THING can be any kind of vector or a SAP. If
394 THING is a SAP, END must be supplied (as length won't work)."
395 (let ((start (or start 0))
396 (end (or end (length (the (simple-array * (*)) thing)))))
397 (declare (type index start end))
398 (when (> (fd-stream-ibuf-tail fd-stream)
399 (fd-stream-ibuf-head fd-stream))
400 (file-position fd-stream (file-position fd-stream)))
401 (let* ((len (fd-stream-obuf-length fd-stream))
402 (tail (fd-stream-obuf-tail fd-stream))
404 (bytes (- end start))
405 (newtail (+ tail bytes)))
406 (cond ((minusp bytes) ; error case
407 (error ":END before :START!"))
408 ((zerop bytes)) ; easy case
410 (if (system-area-pointer-p thing)
411 (system-area-copy thing
412 (* start sb!vm:n-byte-bits)
413 (fd-stream-obuf-sap fd-stream)
414 (* tail sb!vm:n-byte-bits)
415 (* bytes sb!vm:n-byte-bits))
416 ;; FIXME: There should be some type checking somewhere to
417 ;; verify that THING here is a vector, not just <not a SAP>.
418 (copy-to-system-area thing
419 (+ (* start sb!vm:n-byte-bits)
420 (* sb!vm:vector-data-offset
422 (fd-stream-obuf-sap fd-stream)
423 (* tail sb!vm:n-byte-bits)
424 (* bytes sb!vm:n-byte-bits)))
425 (setf (fd-stream-obuf-tail fd-stream) newtail))
427 (flush-output-buffer fd-stream)
428 (if (system-area-pointer-p thing)
429 (system-area-copy thing
430 (* start sb!vm:n-byte-bits)
431 (fd-stream-obuf-sap fd-stream)
433 (* bytes sb!vm:n-byte-bits))
434 ;; FIXME: There should be some type checking somewhere to
435 ;; verify that THING here is a vector, not just <not a SAP>.
436 (copy-to-system-area thing
437 (+ (* start sb!vm:n-byte-bits)
438 (* sb!vm:vector-data-offset
440 (fd-stream-obuf-sap fd-stream)
442 (* bytes sb!vm:n-byte-bits)))
443 (setf (fd-stream-obuf-tail fd-stream) bytes))
445 (flush-output-buffer fd-stream)
446 (frob-output fd-stream thing start end nil))))))
448 ;;; the routine to use to output a string. If the stream is
449 ;;; unbuffered, slam the string down the file descriptor, otherwise
450 ;;; use OUTPUT-RAW-BYTES to buffer the string. Update charpos by
451 ;;; checking to see where the last newline was.
453 ;;; Note: some bozos (the FASL dumper) call write-string with things
454 ;;; other than strings. Therefore, we must make sure we have a string
455 ;;; before calling POSITION on it.
456 ;;; KLUDGE: It would be better to fix the bozos instead of trying to
457 ;;; cover for them here. -- WHN 20000203
458 (defun fd-sout (stream thing start end)
459 (let ((start (or start 0))
460 (end (or end (length (the vector thing)))))
461 (declare (fixnum start end))
463 (let ((last-newline (and (find #\newline (the simple-string thing)
464 :start start :end end)
465 ;; FIXME why do we need both calls?
466 ;; Is find faster forwards than
467 ;; position is backwards?
468 (position #\newline (the simple-string thing)
472 (if (and (typep thing 'base-string)
473 (eq (fd-stream-external-format stream) :latin-1))
474 (ecase (fd-stream-buffering stream)
476 (output-raw-bytes stream thing start end))
478 (output-raw-bytes stream thing start end)
480 (flush-output-buffer stream)))
482 (frob-output stream thing start end nil)))
483 (ecase (fd-stream-buffering stream)
484 (:full (funcall (fd-stream-output-bytes stream)
485 stream thing nil start end))
486 (:line (funcall (fd-stream-output-bytes stream)
487 stream thing last-newline start end))
488 (:none (funcall (fd-stream-output-bytes stream)
489 stream thing t start end))))
491 (setf (fd-stream-char-pos stream)
492 (- end last-newline 1))
493 (incf (fd-stream-char-pos stream)
495 (ecase (fd-stream-buffering stream)
497 (output-raw-bytes stream thing start end))
499 (frob-output stream thing start end nil))))))
501 (defvar *external-formats* ()
503 "List of all available external formats. Each element is a list of the
504 element-type, string input function name, character input function name,
505 and string output function name.")
507 ;;; Find an output routine to use given the type and buffering. Return
508 ;;; as multiple values the routine, the real type transfered, and the
509 ;;; number of bytes per element.
510 (defun pick-output-routine (type buffering &optional external-format)
511 (when (subtypep type 'character)
512 (dolist (entry *external-formats*)
513 (when (member external-format (first entry))
514 (return-from pick-output-routine
515 (values (symbol-function (nth (ecase buffering
522 (symbol-function (fourth entry))
523 (first (first entry)))))))
524 (dolist (entry *output-routines*)
525 (when (and (subtypep type (first entry))
526 (eq buffering (second entry))
527 (or (not (fifth entry))
528 (eq external-format (fifth entry))))
529 (return-from pick-output-routine
530 (values (symbol-function (third entry))
533 ;; KLUDGE: dealing with the buffering here leads to excessive code
536 ;; KLUDGE: also see comments in PICK-INPUT-ROUTINE
537 (loop for i from 40 by 8 to 1024 ; ARB (KLUDGE)
538 if (subtypep type `(unsigned-byte ,i))
539 do (return-from pick-output-routine
543 (lambda (stream byte)
544 (output-wrapper (stream (/ i 8) (:none) nil)
545 (loop for j from 0 below (/ i 8)
547 (fd-stream-obuf-sap stream)
548 (+ j (fd-stream-obuf-tail stream)))
549 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
551 (lambda (stream byte)
552 (output-wrapper (stream (/ i 8) (:full) nil)
553 (loop for j from 0 below (/ i 8)
555 (fd-stream-obuf-sap stream)
556 (+ j (fd-stream-obuf-tail stream)))
557 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
560 (loop for i from 40 by 8 to 1024 ; ARB (KLUDGE)
561 if (subtypep type `(signed-byte ,i))
562 do (return-from pick-output-routine
566 (lambda (stream byte)
567 (output-wrapper (stream (/ i 8) (:none) nil)
568 (loop for j from 0 below (/ i 8)
570 (fd-stream-obuf-sap stream)
571 (+ j (fd-stream-obuf-tail stream)))
572 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
574 (lambda (stream byte)
575 (output-wrapper (stream (/ i 8) (:full) nil)
576 (loop for j from 0 below (/ i 8)
578 (fd-stream-obuf-sap stream)
579 (+ j (fd-stream-obuf-tail stream)))
580 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
584 ;;;; input routines and related noise
586 ;;; a list of all available input routines. Each element is a list of
587 ;;; the element-type input, the function name, and the number of bytes
589 (defvar *input-routines* ())
591 ;;; Fill the input buffer, and return the first character. Throw to
592 ;;; EOF-INPUT-CATCHER if the eof was reached. Drop into SYSTEM:SERVER
594 (defun frob-input (stream)
595 (let ((fd (fd-stream-fd stream))
596 (ibuf-sap (fd-stream-ibuf-sap stream))
597 (buflen (fd-stream-ibuf-length stream))
598 (head (fd-stream-ibuf-head stream))
599 (tail (fd-stream-ibuf-tail stream)))
600 (declare (type index head tail))
602 (cond ((eql head tail)
605 (setf (fd-stream-ibuf-head stream) 0)
606 (setf (fd-stream-ibuf-tail stream) 0))
609 (system-area-copy ibuf-sap (* head sb!vm:n-byte-bits)
610 ibuf-sap 0 (* tail sb!vm:n-byte-bits))
612 (setf (fd-stream-ibuf-head stream) 0)
613 (setf (fd-stream-ibuf-tail stream) tail))))
614 (setf (fd-stream-listen stream) nil)
615 (multiple-value-bind (count errno)
616 ;; FIXME: Judging from compiler warnings, this WITH-ALIEN form expands
617 ;; into something which uses the not-yet-defined type
618 ;; (SB!ALIEN-INTERNALS:ALIEN (* (SB!ALIEN:STRUCT SB!UNIX:FD-SET))).
619 ;; This is probably inefficient and unsafe and generally bad, so
620 ;; try to find some way to make that type known before
622 (sb!alien:with-alien ((read-fds (sb!alien:struct sb!unix:fd-set)))
623 (sb!unix:fd-zero read-fds)
624 (sb!unix:fd-set fd read-fds)
625 (sb!unix:unix-fast-select (1+ fd)
626 (sb!alien:addr read-fds)
634 (unless (sb!sys:wait-until-fd-usable
635 fd :input (fd-stream-timeout stream))
636 (error 'io-timeout :stream stream :direction :read)))
638 (simple-stream-perror "couldn't check whether ~S is readable"
641 (multiple-value-bind (count errno)
642 (sb!unix:unix-read fd
643 (sb!sys:int-sap (+ (sb!sys:sap-int ibuf-sap) tail))
646 (if (eql errno sb!unix:ewouldblock)
648 (unless (sb!sys:wait-until-fd-usable
649 fd :input (fd-stream-timeout stream))
650 (error 'io-timeout :stream stream :direction :read))
652 (simple-stream-perror "couldn't read from ~S" stream errno)))
654 (setf (fd-stream-listen stream) :eof)
655 (/show0 "THROWing EOF-INPUT-CATCHER")
656 (throw 'eof-input-catcher nil))
658 (incf (fd-stream-ibuf-tail stream) count))))))
660 ;;; Make sure there are at least BYTES number of bytes in the input
661 ;;; buffer. Keep calling FROB-INPUT until that condition is met.
662 (defmacro input-at-least (stream bytes)
663 (let ((stream-var (gensym))
664 (bytes-var (gensym)))
665 `(let ((,stream-var ,stream)
668 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
669 (fd-stream-ibuf-head ,stream-var))
672 (frob-input ,stream-var)))))
674 (defmacro input-wrapper/variable-width ((stream bytes eof-error eof-value
677 (let ((stream-var (gensym))
679 (element-var (gensym)))
680 `(let ((,stream-var ,stream)
682 (if (fd-stream-unread ,stream-var)
684 (fd-stream-unread ,stream-var)
685 (setf (fd-stream-unread ,stream-var) nil)
686 (setf (fd-stream-listen ,stream-var) nil))
687 (let ((,element-var nil))
690 (setq ,retry-var nil)
692 (catch 'eof-input-catcher
694 (block character-decode
695 (input-at-least ,stream-var 1)
696 (let* ((byte (sap-ref-8 (fd-stream-ibuf-sap
701 (input-at-least ,stream-var size)
702 (setq ,element-var (locally ,@read-forms))))
703 (stream-decoding-error
706 (loop for i from 0 below size
707 collect (sap-ref-8 (fd-stream-ibuf-sap
709 (+ (fd-stream-ibuf-head
712 (list (sap-ref-8 (fd-stream-ibuf-sap
717 :report (lambda (stream)
719 "~@<Attempt to resync the stream at a ~
720 character boundary and continue.~@:>"))
721 (,resync-function ,stream-var)
723 (force-end-of-file ()
724 :report (lambda (stream)
726 "~@<Force an end of file.~@:>"))
729 (incf (fd-stream-ibuf-head ,stream-var) size)
732 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
734 ;;; a macro to wrap around all input routines to handle EOF-ERROR noise
735 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
736 (let ((stream-var (gensym))
737 (element-var (gensym)))
738 `(let ((,stream-var ,stream))
739 (if (fd-stream-unread ,stream-var)
741 (fd-stream-unread ,stream-var)
742 (setf (fd-stream-unread ,stream-var) nil)
743 (setf (fd-stream-listen ,stream-var) nil))
745 (catch 'eof-input-catcher
746 (input-at-least ,stream-var ,bytes)
747 (locally ,@read-forms))))
749 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
752 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
754 (defmacro def-input-routine/variable-width (name
755 (type external-format size sap head
759 (defun ,name (stream eof-error eof-value)
760 (input-wrapper/variable-width (stream ,size eof-error eof-value
762 (let ((,sap (fd-stream-ibuf-sap stream))
763 (,head (fd-stream-ibuf-head stream)))
765 (setf *input-routines*
766 (nconc *input-routines*
767 (list (list ',type ',name 1 ',external-format))))))
769 (defmacro def-input-routine (name
773 (defun ,name (stream eof-error eof-value)
774 (input-wrapper (stream ,size eof-error eof-value)
775 (let ((,sap (fd-stream-ibuf-sap stream))
776 (,head (fd-stream-ibuf-head stream)))
778 (setf *input-routines*
779 (nconc *input-routines*
780 (list (list ',type ',name ',size nil))))))
782 ;;; STREAM-IN routine for reading a string char
783 (def-input-routine input-character
784 (character 1 sap head)
785 (code-char (sap-ref-8 sap head)))
787 ;;; STREAM-IN routine for reading an unsigned 8 bit number
788 (def-input-routine input-unsigned-8bit-byte
789 ((unsigned-byte 8) 1 sap head)
790 (sap-ref-8 sap head))
792 ;;; STREAM-IN routine for reading a signed 8 bit number
793 (def-input-routine input-signed-8bit-number
794 ((signed-byte 8) 1 sap head)
795 (signed-sap-ref-8 sap head))
797 ;;; STREAM-IN routine for reading an unsigned 16 bit number
798 (def-input-routine input-unsigned-16bit-byte
799 ((unsigned-byte 16) 2 sap head)
800 (sap-ref-16 sap head))
802 ;;; STREAM-IN routine for reading a signed 16 bit number
803 (def-input-routine input-signed-16bit-byte
804 ((signed-byte 16) 2 sap head)
805 (signed-sap-ref-16 sap head))
807 ;;; STREAM-IN routine for reading a unsigned 32 bit number
808 (def-input-routine input-unsigned-32bit-byte
809 ((unsigned-byte 32) 4 sap head)
810 (sap-ref-32 sap head))
812 ;;; STREAM-IN routine for reading a signed 32 bit number
813 (def-input-routine input-signed-32bit-byte
814 ((signed-byte 32) 4 sap head)
815 (signed-sap-ref-32 sap head))
819 ;;; Find an input routine to use given the type. Return as multiple
820 ;;; values the routine, the real type transfered, and the number of
821 ;;; bytes per element (and for character types string input routine).
822 (defun pick-input-routine (type &optional external-format)
823 (when (subtypep type 'character)
824 (dolist (entry *external-formats*)
825 (when (member external-format (first entry))
826 (return-from pick-input-routine
827 (values (symbol-function (third entry))
830 (symbol-function (second entry))
831 (first (first entry)))))))
832 (dolist (entry *input-routines*)
833 (when (and (subtypep type (first entry))
834 (or (not (fourth entry))
835 (eq external-format (fourth entry))))
836 (return-from pick-input-routine
837 (values (symbol-function (second entry))
840 ;; FIXME: let's do it the hard way, then (but ignore things like
841 ;; endianness, efficiency, and the necessary coupling between these
842 ;; and the output routines). -- CSR, 2004-02-09
843 (loop for i from 40 by 8 to 1024 ; ARB (well, KLUDGE really)
844 if (subtypep type `(unsigned-byte ,i))
845 do (return-from pick-input-routine
847 (lambda (stream eof-error eof-value)
848 (input-wrapper (stream (/ i 8) eof-error eof-value)
849 (let ((sap (fd-stream-ibuf-sap stream))
850 (head (fd-stream-ibuf-head stream)))
851 (loop for j from 0 below (/ i 8)
855 (sap-ref-8 sap (+ head j))))
856 finally (return result)))))
859 (loop for i from 40 by 8 to 1024 ; ARB (well, KLUDGE really)
860 if (subtypep type `(signed-byte ,i))
861 do (return-from pick-input-routine
863 (lambda (stream eof-error eof-value)
864 (input-wrapper (stream (/ i 8) eof-error eof-value)
865 (let ((sap (fd-stream-ibuf-sap stream))
866 (head (fd-stream-ibuf-head stream)))
867 (loop for j from 0 below (/ i 8)
871 (sap-ref-8 sap (+ head j))))
872 finally (return (if (logbitp (1- i) result)
873 (dpb result (byte i 0) -1)
878 ;;; Return a string constructed from SAP, START, and END.
879 (defun string-from-sap (sap start end)
880 (declare (type index start end))
881 (let* ((length (- end start))
882 (string (make-string length)))
883 (copy-from-system-area sap (* start sb!vm:n-byte-bits)
884 string (* sb!vm:vector-data-offset
886 (* length sb!vm:n-byte-bits))
889 ;;; the N-BIN method for FD-STREAMs
891 ;;; Note that this blocks in UNIX-READ. It is generally used where
892 ;;; there is a definite amount of reading to be done, so blocking
893 ;;; isn't too problematical.
894 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p
895 &aux (total-copied 0))
896 (declare (type fd-stream stream))
897 (declare (type index start requested total-copied))
898 (let ((unread (fd-stream-unread stream)))
900 ;; AVERs designed to fail when we have more complicated
901 ;; character representations.
902 (aver (typep unread 'base-char))
903 (aver (= (fd-stream-element-size stream) 1))
904 ;; KLUDGE: this is a slightly-unrolled-and-inlined version of
908 (setf (sap-ref-8 buffer start) (char-code unread)))
909 ((simple-unboxed-array (*))
910 (setf (aref buffer start) unread)))
911 (setf (fd-stream-unread stream) nil)
912 (setf (fd-stream-listen stream) nil)
913 (incf total-copied)))
916 (let* ((remaining-request (- requested total-copied))
917 (head (fd-stream-ibuf-head stream))
918 (tail (fd-stream-ibuf-tail stream))
919 (available (- tail head))
920 (n-this-copy (min remaining-request available))
921 (this-start (+ start total-copied))
922 (this-end (+ this-start n-this-copy))
923 (sap (fd-stream-ibuf-sap stream)))
924 (declare (type index remaining-request head tail available))
925 (declare (type index n-this-copy))
926 ;; Copy data from stream buffer into user's buffer.
927 (%byte-blt sap head buffer this-start this-end)
928 (incf (fd-stream-ibuf-head stream) n-this-copy)
929 (incf total-copied n-this-copy)
930 ;; Maybe we need to refill the stream buffer.
931 (cond (;; If there were enough data in the stream buffer, we're done.
932 (= total-copied requested)
933 (return total-copied))
934 (;; If EOF, we're done in another way.
935 (zerop (refill-fd-stream-buffer stream))
937 (error 'end-of-file :stream stream)
938 (return total-copied)))
939 ;; Otherwise we refilled the stream buffer, so fall
940 ;; through into another pass of the loop.
943 ;;; Try to refill the stream buffer. Return the number of bytes read.
944 ;;; (For EOF, the return value will be zero, otherwise positive.)
945 (defun refill-fd-stream-buffer (stream)
946 ;; We don't have any logic to preserve leftover bytes in the buffer,
947 ;; so we should only be called when the buffer is empty.
948 ;; FIXME: can have three bytes in buffer because of UTF-8
950 (sap (fd-stream-ibuf-sap stream)))
951 (do ((head (fd-stream-ibuf-head stream) (1+ head))
952 (tail (fd-stream-ibuf-tail stream)))
954 (setf (sap-ref-8 sap new-head) (sap-ref-8 sap head))
956 (multiple-value-bind (count err)
957 (sb!unix:unix-read (fd-stream-fd stream)
959 (- (fd-stream-ibuf-length stream) new-head))
960 (declare (type (or index null) count))
962 (simple-stream-perror "couldn't read from ~S" stream err))
963 (setf (fd-stream-listen stream) nil
964 (fd-stream-ibuf-head stream) 0
965 (fd-stream-ibuf-tail stream) (+ count new-head))
968 (defmacro define-external-format (external-format size output-restart
970 (let* ((name (first external-format))
971 (out-function (intern (let ((*print-case* :upcase))
972 (format nil "OUTPUT-BYTES/~A" name))))
973 (format (format nil "OUTPUT-CHAR-~A-~~A-BUFFERED" name))
974 (in-function (intern (let ((*print-case* :upcase))
975 (format nil "FD-STREAM-READ-N-CHARACTERS/~A"
977 (in-char-function (intern (let ((*print-case* :upcase))
978 (format nil "INPUT-CHAR/~A" name)))))
980 (defun ,out-function (stream string flush-p start end)
981 (let ((start (or start 0))
982 (end (or end (length string))))
983 (declare (type index start end))
984 (when (> (fd-stream-ibuf-tail stream)
985 (fd-stream-ibuf-head stream))
986 (file-position stream (file-position stream)))
988 (error ":END before :START!"))
991 (setf (fd-stream-obuf-tail stream)
992 (do* ((len (fd-stream-obuf-length stream))
993 (sap (fd-stream-obuf-sap stream))
994 (tail (fd-stream-obuf-tail stream)))
995 ((or (= start end) (< (- len tail) 4)) tail)
997 `(with-simple-restart (output-nothing
998 "~@<Skip output of this character.~@:>")
999 (let* ((byte (aref string start))
1000 (bits (char-code byte)))
1003 `(let* ((byte (aref string start))
1004 (bits (char-code byte)))
1009 (flush-output-buffer stream)))
1011 (flush-output-buffer stream))))
1012 (def-output-routines (,format
1018 (if (char= byte #\Newline)
1019 (setf (fd-stream-char-pos stream) 0)
1020 (incf (fd-stream-char-pos stream)))
1021 (let ((bits (char-code byte))
1022 (sap (fd-stream-obuf-sap stream))
1023 (tail (fd-stream-obuf-tail stream)))
1025 (defun ,in-function (stream buffer start requested eof-error-p
1026 &aux (total-copied 0))
1027 (declare (type fd-stream stream))
1028 (declare (type index start requested total-copied))
1029 (let ((unread (fd-stream-unread stream)))
1031 (setf (aref buffer start) unread)
1032 (setf (fd-stream-unread stream) nil)
1033 (setf (fd-stream-listen stream) nil)
1034 (incf total-copied)))
1037 (let* ((head (fd-stream-ibuf-head stream))
1038 (tail (fd-stream-ibuf-tail stream))
1039 (sap (fd-stream-ibuf-sap stream)))
1040 (declare (type index head tail))
1041 ;; Copy data from stream buffer into user's buffer.
1043 ((or (= tail head) (= requested total-copied)))
1044 (let* ((byte (sap-ref-8 sap head)))
1045 (when (> ,size (- tail head))
1047 (setf (aref buffer (+ start total-copied)) ,in-expr)
1050 (setf (fd-stream-ibuf-head stream) head)
1051 ;; Maybe we need to refill the stream buffer.
1052 (cond ( ;; If there were enough data in the stream buffer, we're done.
1053 (= total-copied requested)
1054 (return total-copied))
1055 ( ;; If EOF, we're done in another way.
1056 (zerop (refill-fd-stream-buffer stream))
1058 (error 'end-of-file :stream stream)
1059 (return total-copied)))
1060 ;; Otherwise we refilled the stream buffer, so fall
1061 ;; through into another pass of the loop.
1063 (def-input-routine ,in-char-function (character ,size sap head)
1064 (let ((byte (sap-ref-8 sap head)))
1066 (setf *external-formats*
1067 (cons '(,external-format ,in-function ,in-char-function ,out-function
1068 ,@(mapcar #'(lambda (buffering)
1069 (intern (let ((*print-case* :upcase))
1070 (format nil format buffering))))
1071 '(:none :line :full)))
1072 *external-formats*)))))
1074 (defmacro define-external-format/variable-width
1075 (external-format output-restart out-size-expr
1076 out-expr in-size-expr in-expr)
1077 (let* ((name (first external-format))
1078 (out-function (intern (let ((*print-case* :upcase))
1079 (format nil "OUTPUT-BYTES/~A" name))))
1080 (format (format nil "OUTPUT-CHAR-~A-~~A-BUFFERED" name))
1081 (in-function (intern (let ((*print-case* :upcase))
1082 (format nil "FD-STREAM-READ-N-CHARACTERS/~A"
1084 (in-char-function (intern (let ((*print-case* :upcase))
1085 (format nil "INPUT-CHAR/~A" name))))
1086 (resync-function (intern (let ((*print-case* :upcase))
1087 (format nil "RESYNC/~A" name)))))
1089 (defun ,out-function (fd-stream string flush-p start end)
1090 (let ((start (or start 0))
1091 (end (or end (length string))))
1092 (declare (type index start end))
1093 (when (> (fd-stream-ibuf-tail fd-stream)
1094 (fd-stream-ibuf-head fd-stream))
1095 (file-position fd-stream (file-position fd-stream)))
1097 (error ":END before :START!"))
1100 (setf (fd-stream-obuf-tail fd-stream)
1101 (do* ((len (fd-stream-obuf-length fd-stream))
1102 (sap (fd-stream-obuf-sap fd-stream))
1103 (tail (fd-stream-obuf-tail fd-stream)))
1104 ((or (= start end) (< (- len tail) 4)) tail)
1105 (let* ((byte (aref string start))
1106 (bits (char-code byte))
1107 (size ,out-size-expr))
1112 (flush-output-buffer fd-stream)))
1114 (flush-output-buffer fd-stream))))
1115 (def-output-routines/variable-width (,format
1122 (if (char= byte #\Newline)
1123 (setf (fd-stream-char-pos stream) 0)
1124 (incf (fd-stream-char-pos stream)))
1125 (let ((bits (char-code byte))
1126 (sap (fd-stream-obuf-sap stream))
1127 (tail (fd-stream-obuf-tail stream)))
1129 (defun ,in-function (stream buffer start requested eof-error-p
1130 &aux (total-copied 0))
1131 (declare (type fd-stream stream))
1132 (declare (type index start requested total-copied))
1133 (let ((unread (fd-stream-unread stream)))
1135 (setf (aref buffer start) unread)
1136 (setf (fd-stream-unread stream) nil)
1137 (setf (fd-stream-listen stream) nil)
1138 (incf total-copied)))
1141 (let* ((head (fd-stream-ibuf-head stream))
1142 (tail (fd-stream-ibuf-tail stream))
1143 (sap (fd-stream-ibuf-sap stream)))
1144 (declare (type index head tail))
1145 ;; Copy data from stream buffer into user's buffer.
1146 (do ((size nil nil))
1147 ((or (= tail head) (= requested total-copied)))
1149 (unless (block character-decode
1150 (let ((byte (sap-ref-8 sap head)))
1151 (setq size ,in-size-expr)
1152 (when (> size (- tail head))
1154 (setf (aref buffer (+ start total-copied))
1158 (setf (fd-stream-ibuf-head stream) head)
1159 (if (plusp total-copied)
1160 (return-from ,in-function total-copied)
1161 (stream-decoding-error
1164 (loop for i from 0 below size
1165 collect (sap-ref-8 (fd-stream-ibuf-sap
1167 (+ (fd-stream-ibuf-head
1170 (list (sap-ref-8 (fd-stream-ibuf-sap stream)
1171 (fd-stream-ibuf-head stream)))))))
1173 :report (lambda (stream)
1175 "~@<Attempt to resync the stream at a ~
1176 character boundary and continue.~@:>"))
1177 (,resync-function stream)
1178 (setf head (fd-stream-ibuf-head stream)))
1179 (force-end-of-file ()
1180 :report (lambda (stream)
1181 (format stream "~@<Force an end of file.~@:>"))
1183 (error 'end-of-file :stream stream)
1184 (return-from ,in-function total-copied)))))
1185 (setf (fd-stream-ibuf-head stream) head)
1186 ;; Maybe we need to refill the stream buffer.
1187 (cond ( ;; If there were enough data in the stream buffer, we're done.
1188 (= total-copied requested)
1189 (return total-copied))
1190 ( ;; If EOF, we're done in another way.
1191 (zerop (refill-fd-stream-buffer stream))
1193 (error 'end-of-file :stream stream)
1194 (return total-copied)))
1195 ;; Otherwise we refilled the stream buffer, so fall
1196 ;; through into another pass of the loop.
1198 (def-input-routine/variable-width ,in-char-function (character
1203 (let ((byte (sap-ref-8 sap head)))
1205 (defun ,resync-function (stream)
1206 (loop (input-at-least stream 1)
1207 (incf (fd-stream-ibuf-head stream))
1208 (when (block character-decode
1209 (let* ((sap (fd-stream-ibuf-sap stream))
1210 (head (fd-stream-ibuf-head stream))
1211 (byte (sap-ref-8 sap head))
1212 (size ,in-size-expr))
1215 (setf *external-formats*
1216 (cons '(,external-format ,in-function ,in-char-function ,out-function
1217 ,@(mapcar #'(lambda (buffering)
1218 (intern (let ((*print-case* :upcase))
1219 (format nil format buffering))))
1220 '(:none :line :full))
1222 *external-formats*)))))
1224 (define-external-format (:latin-1 :latin1 :iso-8859-1)
1227 (stream-encoding-error stream bits)
1228 (setf (sap-ref-8 sap tail) bits))
1231 (define-external-format (:ascii :us-ascii :ansi_x3.4-1968)
1234 (stream-encoding-error stream bits)
1235 (setf (sap-ref-8 sap tail) bits))
1238 (let* ((table (let ((s (make-string 256)))
1239 (map-into s #'code-char
1240 '(#x00 #x01 #x02 #x03 #x9c #x09 #x86 #x7f #x97 #x8d #x8e #x0b #x0c #x0d #x0e #x0f
1241 #x10 #x11 #x12 #x13 #x9d #x85 #x08 #x87 #x18 #x19 #x92 #x8f #x1c #x1d #x1e #x1f
1242 #x80 #x81 #x82 #x83 #x84 #x0a #x17 #x1b #x88 #x89 #x8a #x8b #x8c #x05 #x06 #x07
1243 #x90 #x91 #x16 #x93 #x94 #x95 #x96 #x04 #x98 #x99 #x9a #x9b #x14 #x15 #x9e #x1a
1244 #x20 #xa0 #xe2 #xe4 #xe0 #xe1 #xe3 #xe5 #xe7 #xf1 #xa2 #x2e #x3c #x28 #x2b #x7c
1245 #x26 #xe9 #xea #xeb #xe8 #xed #xee #xef #xec #xdf #x21 #x24 #x2a #x29 #x3b #xac
1246 #x2d #x2f #xc2 #xc4 #xc0 #xc1 #xc3 #xc5 #xc7 #xd1 #xa6 #x2c #x25 #x5f #x3e #x3f
1247 #xf8 #xc9 #xca #xcb #xc8 #xcd #xce #xcf #xcc #x60 #x3a #x23 #x40 #x27 #x3d #x22
1248 #xd8 #x61 #x62 #x63 #x64 #x65 #x66 #x67 #x68 #x69 #xab #xbb #xf0 #xfd #xfe #xb1
1249 #xb0 #x6a #x6b #x6c #x6d #x6e #x6f #x70 #x71 #x72 #xaa #xba #xe6 #xb8 #xc6 #xa4
1250 #xb5 #x7e #x73 #x74 #x75 #x76 #x77 #x78 #x79 #x7a #xa1 #xbf #xd0 #xdd #xde #xae
1251 #x5e #xa3 #xa5 #xb7 #xa9 #xa7 #xb6 #xbc #xbd #xbe #x5b #x5d #xaf #xa8 #xb4 #xd7
1252 #x7b #x41 #x42 #x43 #x44 #x45 #x46 #x47 #x48 #x49 #xad #xf4 #xf6 #xf2 #xf3 #xf5
1253 #x7d #x4a #x4b #x4c #x4d #x4e #x4f #x50 #x51 #x52 #xb9 #xfb #xfc #xf9 #xfa #xff
1254 #x5c #xf7 #x53 #x54 #x55 #x56 #x57 #x58 #x59 #x5a #xb2 #xd4 #xd6 #xd2 #xd3 #xd5
1255 #x30 #x31 #x32 #x33 #x34 #x35 #x36 #x37 #x38 #x39 #xb3 #xdb #xdc #xd9 #xda #x9f))
1257 (reverse-table (let ((rt (make-array 256 :element-type '(unsigned-byte 8) :initial-element 0)))
1258 (loop for char across table for i from 0
1259 do (aver (= 0 (aref rt (char-code char))))
1260 do (setf (aref rt (char-code char)) i))
1262 (define-external-format (:ebcdic-us :ibm-037 :ibm037)
1265 (stream-encoding-error stream bits)
1266 (setf (sap-ref-8 sap tail) (aref reverse-table bits)))
1271 (let ((latin-9-table (let ((table (make-string 256)))
1274 (setf (aref table i) (code-char i)))
1275 (setf (aref table #xa4) (code-char #x20ac))
1276 (setf (aref table #xa6) (code-char #x0160))
1277 (setf (aref table #xa8) (code-char #x0161))
1278 (setf (aref table #xb4) (code-char #x017d))
1279 (setf (aref table #xb8) (code-char #x017e))
1280 (setf (aref table #xbc) (code-char #x0152))
1281 (setf (aref table #xbd) (code-char #x0153))
1282 (setf (aref table #xbe) (code-char #x0178))
1284 (latin-9-reverse-1 (make-array 16
1285 :element-type '(unsigned-byte 21)
1286 :initial-contents '(#x0160 #x0161 #x0152 #x0153 0 0 0 0 #x0178 0 0 0 #x20ac #x017d #x017e 0)))
1287 (latin-9-reverse-2 (make-array 16
1288 :element-type '(unsigned-byte 8)
1289 :initial-contents '(#xa6 #xa8 #xbc #xbd 0 0 0 0 #xbe 0 0 0 #xa4 #xb4 #xb8 0))))
1290 (define-external-format (:latin-9 :latin9 :iso-8859-15)
1292 (setf (sap-ref-8 sap tail)
1294 (if (= bits (char-code (aref latin-9-table bits)))
1296 (stream-encoding-error stream byte))
1297 (if (= (aref latin-9-reverse-1 (logand bits 15)) bits)
1298 (aref latin-9-reverse-2 (logand bits 15))
1299 (stream-encoding-error stream byte))))
1300 (aref latin-9-table byte)))
1302 (define-external-format/variable-width (:utf-8 :utf8) nil
1303 (let ((bits (char-code byte)))
1304 (cond ((< bits #x80) 1)
1306 ((< bits #x10000) 3)
1309 (1 (setf (sap-ref-8 sap tail) bits))
1310 (2 (setf (sap-ref-8 sap tail) (logior #xc0 (ldb (byte 5 6) bits))
1311 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 0) bits))))
1312 (3 (setf (sap-ref-8 sap tail) (logior #xe0 (ldb (byte 4 12) bits))
1313 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 6) bits))
1314 (sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 0) bits))))
1315 (4 (setf (sap-ref-8 sap tail) (logior #xf0 (ldb (byte 3 18) bits))
1316 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 12) bits))
1317 (sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 6) bits))
1318 (sap-ref-8 sap (+ 3 tail)) (logior #x80 (ldb (byte 6 0) bits)))))
1319 (cond ((< byte #x80) 1)
1320 ((< byte #xc2) (return-from character-decode))
1324 (code-char (ecase size
1326 (2 (let ((byte2 (sap-ref-8 sap (1+ head))))
1327 (unless (<= #x80 byte2 #xbf)
1328 (return-from character-decode))
1329 (dpb byte (byte 5 6) byte2)))
1330 (3 (let ((byte2 (sap-ref-8 sap (1+ head)))
1331 (byte3 (sap-ref-8 sap (+ 2 head))))
1332 (unless (and (<= #x80 byte2 #xbf)
1333 (<= #x80 byte3 #xbf))
1334 (return-from character-decode))
1335 (dpb byte (byte 4 12) (dpb byte2 (byte 6 6) byte3))))
1336 (4 (let ((byte2 (sap-ref-8 sap (1+ head)))
1337 (byte3 (sap-ref-8 sap (+ 2 head)))
1338 (byte4 (sap-ref-8 sap (+ 3 head))))
1339 (unless (and (<= #x80 byte2 #xbf)
1340 (<= #x80 byte3 #xbf)
1341 (<= #x80 byte4 #xbf))
1342 (return-from character-decode))
1343 (dpb byte (byte 3 18)
1344 (dpb byte2 (byte 6 12)
1345 (dpb byte3 (byte 6 6) byte4))))))))
1347 ;;;; utility functions (misc routines, etc)
1349 ;;; Fill in the various routine slots for the given type. INPUT-P and
1350 ;;; OUTPUT-P indicate what slots to fill. The buffering slot must be
1351 ;;; set prior to calling this routine.
1352 (defun set-fd-stream-routines (fd-stream type input-p output-p buffer-p)
1353 (let ((target-type (case type
1354 ((:default unsigned-byte)
1364 (character-stream-p (subtypep type 'character)))
1366 (when (fd-stream-obuf-sap fd-stream)
1367 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
1368 (setf (fd-stream-obuf-sap fd-stream) nil))
1369 (when (fd-stream-ibuf-sap fd-stream)
1370 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
1371 (setf (fd-stream-ibuf-sap fd-stream) nil))
1373 (when (and character-stream-p
1374 (eq (fd-stream-external-format fd-stream) :default))
1375 (setf (fd-stream-external-format fd-stream)
1376 (intern (or (alien-funcall
1377 (extern-alien "nl_langinfo"
1378 (function c-string int))
1382 (dolist (entry *external-formats*
1383 (setf (fd-stream-external-format fd-stream) :latin-1))
1384 (when (member (fd-stream-external-format fd-stream) (first entry))
1388 (multiple-value-bind (routine type size read-n-characters
1389 normalized-external-format)
1390 (pick-input-routine target-type
1391 (fd-stream-external-format fd-stream))
1392 (when normalized-external-format
1393 (setf (fd-stream-external-format fd-stream)
1394 normalized-external-format))
1396 (error "could not find any input routine for ~S" target-type))
1397 (setf (fd-stream-ibuf-sap fd-stream) (next-available-buffer))
1398 (setf (fd-stream-ibuf-length fd-stream) bytes-per-buffer)
1399 (setf (fd-stream-ibuf-tail fd-stream) 0)
1400 (if character-stream-p
1401 (setf (fd-stream-in fd-stream) routine
1402 (fd-stream-bin fd-stream) #'ill-bin)
1403 (setf (fd-stream-in fd-stream) #'ill-in
1404 (fd-stream-bin fd-stream) routine))
1406 (setf (fd-stream-n-bin fd-stream)
1407 (if character-stream-p
1409 #'fd-stream-read-n-bytes))
1411 ;; We only create this buffer for streams of type
1412 ;; (unsigned-byte 8). Because there's no buffer, the
1413 ;; other element-types will dispatch to the appropriate
1414 ;; input (output) routine in fast-read-byte.
1415 (or character-stream-p
1416 (equal target-type '(unsigned-byte 8)))
1417 (not output-p) ; temporary disable on :io streams
1419 (or (eq type 'unsigned-byte)
1420 (eq type :default)))
1421 (if character-stream-p
1422 (setf (ansi-stream-cin-buffer fd-stream)
1423 (make-array +ansi-stream-in-buffer-length+
1424 :element-type 'character))
1425 (setf (ansi-stream-in-buffer fd-stream)
1426 (make-array +ansi-stream-in-buffer-length+
1427 :element-type '(unsigned-byte 8))))))
1428 (setf input-size size)
1429 (setf input-type type)))
1432 (multiple-value-bind (routine type size output-bytes
1433 normalized-external-format)
1434 (pick-output-routine target-type
1435 (fd-stream-buffering fd-stream)
1436 (fd-stream-external-format fd-stream))
1437 (when normalized-external-format
1438 (setf (fd-stream-external-format fd-stream)
1439 normalized-external-format))
1441 (error "could not find any output routine for ~S buffered ~S"
1442 (fd-stream-buffering fd-stream)
1444 (setf (fd-stream-obuf-sap fd-stream) (next-available-buffer))
1445 (setf (fd-stream-obuf-length fd-stream) bytes-per-buffer)
1446 (setf (fd-stream-obuf-tail fd-stream) 0)
1447 (when character-stream-p
1448 (setf (fd-stream-output-bytes fd-stream) output-bytes))
1449 (if character-stream-p
1450 (setf (fd-stream-out fd-stream) routine
1451 (fd-stream-bout fd-stream) #'ill-bout)
1452 (setf (fd-stream-out fd-stream)
1453 (or (if (eql size 1)
1454 (pick-output-routine
1455 'base-char (fd-stream-buffering fd-stream)))
1457 (fd-stream-bout fd-stream) routine))
1458 (setf (fd-stream-sout fd-stream)
1459 (if (eql size 1) #'fd-sout #'ill-out))
1460 (setf (fd-stream-char-pos fd-stream) 0)
1461 (setf output-size size)
1462 (setf output-type type)))
1464 (when (and input-size output-size
1465 (not (eq input-size output-size)))
1466 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
1467 input-type input-size
1468 output-type output-size))
1469 (setf (fd-stream-element-size fd-stream)
1470 (or input-size output-size))
1472 (setf (fd-stream-element-type fd-stream)
1473 (cond ((equal input-type output-type)
1479 ((subtypep input-type output-type)
1481 ((subtypep output-type input-type)
1484 (error "Input type (~S) and output type (~S) are unrelated?"
1488 ;;; Handle miscellaneous operations on FD-STREAM.
1489 (defun fd-stream-misc-routine (fd-stream operation &optional arg1 arg2)
1490 (declare (ignore arg2))
1493 (or (not (eql (fd-stream-ibuf-head fd-stream)
1494 (fd-stream-ibuf-tail fd-stream)))
1495 (fd-stream-listen fd-stream)
1496 (setf (fd-stream-listen fd-stream)
1497 (eql (sb!alien:with-alien ((read-fds (sb!alien:struct
1499 (sb!unix:fd-zero read-fds)
1500 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
1501 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
1502 (sb!alien:addr read-fds)
1506 (setf (fd-stream-unread fd-stream) arg1)
1507 (setf (fd-stream-listen fd-stream) t))
1509 (cond (arg1 ; We got us an abort on our hands.
1510 (when (fd-stream-handler fd-stream)
1511 (sb!sys:remove-fd-handler (fd-stream-handler fd-stream))
1512 (setf (fd-stream-handler fd-stream) nil))
1513 ;; We can't do anything unless we know what file were
1514 ;; dealing with, and we don't want to do anything
1515 ;; strange unless we were writing to the file.
1516 (when (and (fd-stream-file fd-stream)
1517 (fd-stream-obuf-sap fd-stream))
1518 (if (fd-stream-original fd-stream)
1519 ;; If the original is EQ to file we are appending
1520 ;; and can just close the file without renaming.
1521 (unless (eq (fd-stream-original fd-stream)
1522 (fd-stream-file fd-stream))
1523 ;; We have a handle on the original, just revert.
1524 (multiple-value-bind (okay err)
1525 (sb!unix:unix-rename (fd-stream-original fd-stream)
1526 (fd-stream-file fd-stream))
1528 (simple-stream-perror
1529 "couldn't restore ~S to its original contents"
1532 ;; We can't restore the original, and aren't
1533 ;; appending, so nuke that puppy.
1535 ;; FIXME: This is currently the fate of superseded
1536 ;; files, and according to the CLOSE spec this is
1537 ;; wrong. However, there seems to be no clean way to
1538 ;; do that that doesn't involve either copying the
1539 ;; data (bad if the :abort resulted from a full
1540 ;; disk), or renaming the old file temporarily
1541 ;; (probably bad because stream opening becomes more
1543 (multiple-value-bind (okay err)
1544 (sb!unix:unix-unlink (fd-stream-file fd-stream))
1546 (error 'simple-file-error
1547 :pathname (fd-stream-file fd-stream)
1549 "~@<couldn't remove ~S: ~2I~_~A~:>"
1550 :format-arguments (list (fd-stream-file fd-stream)
1551 (strerror err))))))))
1553 (fd-stream-misc-routine fd-stream :finish-output)
1554 (when (and (fd-stream-original fd-stream)
1555 (fd-stream-delete-original fd-stream))
1556 (multiple-value-bind (okay err)
1557 (sb!unix:unix-unlink (fd-stream-original fd-stream))
1559 (error 'simple-file-error
1560 :pathname (fd-stream-original fd-stream)
1562 "~@<couldn't delete ~S during close of ~S: ~
1565 (list (fd-stream-original fd-stream)
1567 (strerror err))))))))
1568 (when (fboundp 'cancel-finalization)
1569 (cancel-finalization fd-stream))
1570 (sb!unix:unix-close (fd-stream-fd fd-stream))
1571 (when (fd-stream-obuf-sap fd-stream)
1572 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
1573 (setf (fd-stream-obuf-sap fd-stream) nil))
1574 (when (fd-stream-ibuf-sap fd-stream)
1575 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
1576 (setf (fd-stream-ibuf-sap fd-stream) nil))
1577 (sb!impl::set-closed-flame fd-stream))
1579 (setf (fd-stream-unread fd-stream) nil)
1580 (setf (fd-stream-ibuf-head fd-stream) 0)
1581 (setf (fd-stream-ibuf-tail fd-stream) 0)
1582 (catch 'eof-input-catcher
1584 (let ((count (sb!alien:with-alien ((read-fds (sb!alien:struct
1586 (sb!unix:fd-zero read-fds)
1587 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
1588 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
1589 (sb!alien:addr read-fds)
1594 (cond ((eql count 1)
1595 (frob-input fd-stream)
1596 (setf (fd-stream-ibuf-head fd-stream) 0)
1597 (setf (fd-stream-ibuf-tail fd-stream) 0))
1601 (flush-output-buffer fd-stream))
1603 (flush-output-buffer fd-stream)
1605 ((null (fd-stream-output-later fd-stream)))
1606 (sb!sys:serve-all-events)))
1608 (fd-stream-element-type fd-stream))
1610 (= 1 (the (member 0 1)
1611 (sb!unix:unix-isatty (fd-stream-fd fd-stream)))))
1615 (fd-stream-char-pos fd-stream))
1617 (unless (fd-stream-file fd-stream)
1618 ;; This is a TYPE-ERROR because ANSI's species FILE-LENGTH
1619 ;; "should signal an error of type TYPE-ERROR if stream is not
1620 ;; a stream associated with a file". Too bad there's no very
1621 ;; appropriate value for the EXPECTED-TYPE slot..
1622 (error 'simple-type-error
1624 :expected-type 'fd-stream
1625 :format-control "~S is not a stream associated with a file."
1626 :format-arguments (list fd-stream)))
1627 (multiple-value-bind (okay dev ino mode nlink uid gid rdev size
1628 atime mtime ctime blksize blocks)
1629 (sb!unix:unix-fstat (fd-stream-fd fd-stream))
1630 (declare (ignore ino nlink uid gid rdev
1631 atime mtime ctime blksize blocks))
1633 (simple-stream-perror "failed Unix fstat(2) on ~S" fd-stream dev))
1636 (truncate size (fd-stream-element-size fd-stream)))))
1638 (fd-stream-file-position fd-stream arg1))))
1640 (defun fd-stream-file-position (stream &optional newpos)
1641 (declare (type fd-stream stream)
1642 (type (or (alien sb!unix:off-t) (member nil :start :end)) newpos))
1644 (sb!sys:without-interrupts
1645 ;; First, find the position of the UNIX file descriptor in the file.
1646 (multiple-value-bind (posn errno)
1647 (sb!unix:unix-lseek (fd-stream-fd stream) 0 sb!unix:l_incr)
1648 (declare (type (or (alien sb!unix:off-t) null) posn))
1649 (cond ((integerp posn)
1650 ;; Adjust for buffered output: If there is any output
1651 ;; buffered, the *real* file position will be larger
1652 ;; than reported by lseek() because lseek() obviously
1653 ;; cannot take into account output we have not sent
1655 (dolist (later (fd-stream-output-later stream))
1656 (incf posn (- (caddr later)
1658 (incf posn (fd-stream-obuf-tail stream))
1659 ;; Adjust for unread input: If there is any input
1660 ;; read from UNIX but not supplied to the user of the
1661 ;; stream, the *real* file position will smaller than
1662 ;; reported, because we want to look like the unread
1663 ;; stuff is still available.
1664 (decf posn (- (fd-stream-ibuf-tail stream)
1665 (fd-stream-ibuf-head stream)))
1666 (when (fd-stream-unread stream)
1668 ;; Divide bytes by element size.
1669 (truncate posn (fd-stream-element-size stream)))
1670 ((eq errno sb!unix:espipe)
1673 (sb!sys:with-interrupts
1674 (simple-stream-perror "failure in Unix lseek() on ~S"
1677 (let ((offset 0) origin)
1678 (declare (type (alien sb!unix:off-t) offset))
1679 ;; Make sure we don't have any output pending, because if we
1680 ;; move the file pointer before writing this stuff, it will be
1681 ;; written in the wrong location.
1682 (flush-output-buffer stream)
1684 ((null (fd-stream-output-later stream)))
1685 (sb!sys:serve-all-events))
1686 ;; Clear out any pending input to force the next read to go to
1688 (setf (fd-stream-unread stream) nil)
1689 (setf (fd-stream-ibuf-head stream) 0)
1690 (setf (fd-stream-ibuf-tail stream) 0)
1691 ;; Trash cached value for listen, so that we check next time.
1692 (setf (fd-stream-listen stream) nil)
1694 (cond ((eq newpos :start)
1695 (setf offset 0 origin sb!unix:l_set))
1697 (setf offset 0 origin sb!unix:l_xtnd))
1698 ((typep newpos '(alien sb!unix:off-t))
1699 (setf offset (* newpos (fd-stream-element-size stream))
1700 origin sb!unix:l_set))
1702 (error "invalid position given to FILE-POSITION: ~S" newpos)))
1703 (multiple-value-bind (posn errno)
1704 (sb!unix:unix-lseek (fd-stream-fd stream) offset origin)
1705 (cond ((typep posn '(alien sb!unix:off-t))
1707 ((eq errno sb!unix:espipe)
1710 (simple-stream-perror "error in Unix lseek() on ~S"
1714 ;;;; creation routines (MAKE-FD-STREAM and OPEN)
1716 ;;; Create a stream for the given Unix file descriptor.
1718 ;;; If INPUT is non-NIL, allow input operations. If OUTPUT is non-nil,
1719 ;;; allow output operations. If neither INPUT nor OUTPUT is specified,
1720 ;;; default to allowing input.
1722 ;;; ELEMENT-TYPE indicates the element type to use (as for OPEN).
1724 ;;; BUFFERING indicates the kind of buffering to use.
1726 ;;; TIMEOUT (if true) is the number of seconds to wait for input. If
1727 ;;; NIL (the default), then wait forever. When we time out, we signal
1730 ;;; FILE is the name of the file (will be returned by PATHNAME).
1732 ;;; NAME is used to identify the stream when printed.
1733 (defun make-fd-stream (fd
1736 (output nil output-p)
1737 (element-type 'base-char)
1739 (external-format :default)
1747 (format nil "file ~S" file)
1748 (format nil "descriptor ~W" fd)))
1750 (declare (type index fd) (type (or index null) timeout)
1751 (type (member :none :line :full) buffering))
1752 (cond ((not (or input-p output-p))
1754 ((not (or input output))
1755 (error "File descriptor must be opened either for input or output.")))
1756 (let ((stream (%make-fd-stream :fd fd
1760 :delete-original delete-original
1762 :buffering buffering
1763 :external-format external-format
1765 (set-fd-stream-routines stream element-type input output input-buffer-p)
1766 (when (and auto-close (fboundp 'finalize))
1769 (sb!unix:unix-close fd)
1771 (format *terminal-io* "** closed file descriptor ~W **~%"
1775 ;;; Pick a name to use for the backup file for the :IF-EXISTS
1776 ;;; :RENAME-AND-DELETE and :RENAME options.
1777 (defun pick-backup-name (name)
1778 (declare (type simple-base-string name))
1779 (concatenate 'simple-base-string name ".bak"))
1781 ;;; Ensure that the given arg is one of the given list of valid
1782 ;;; things. Allow the user to fix any problems.
1783 (defun ensure-one-of (item list what)
1784 (unless (member item list)
1785 (error 'simple-type-error
1787 :expected-type `(member ,@list)
1788 :format-control "~@<~S is ~_invalid for ~S; ~_need one of~{ ~S~}~:>"
1789 :format-arguments (list item what list))))
1791 ;;; Rename NAMESTRING to ORIGINAL. First, check whether we have write
1792 ;;; access, since we don't want to trash unwritable files even if we
1793 ;;; technically can. We return true if we succeed in renaming.
1794 (defun rename-the-old-one (namestring original)
1795 (unless (sb!unix:unix-access namestring sb!unix:w_ok)
1796 (error "~@<The file ~2I~_~S ~I~_is not writable.~:>" namestring))
1797 (multiple-value-bind (okay err) (sb!unix:unix-rename namestring original)
1800 (error 'simple-file-error
1801 :pathname namestring
1803 "~@<couldn't rename ~2I~_~S ~I~_to ~2I~_~S: ~4I~_~A~:>"
1804 :format-arguments (list namestring original (strerror err))))))
1806 (defun open (filename
1809 (element-type 'base-char)
1810 (if-exists nil if-exists-given)
1811 (if-does-not-exist nil if-does-not-exist-given)
1812 (external-format :default)
1813 &aux ; Squelch assignment warning.
1814 (direction direction)
1815 (if-does-not-exist if-does-not-exist)
1816 (if-exists if-exists))
1818 "Return a stream which reads from or writes to FILENAME.
1820 :DIRECTION - one of :INPUT, :OUTPUT, :IO, or :PROBE
1821 :ELEMENT-TYPE - the type of object to read or write, default BASE-CHAR
1822 :IF-EXISTS - one of :ERROR, :NEW-VERSION, :RENAME, :RENAME-AND-DELETE,
1823 :OVERWRITE, :APPEND, :SUPERSEDE or NIL
1824 :IF-DOES-NOT-EXIST - one of :ERROR, :CREATE or NIL
1825 See the manual for details."
1827 ;; Calculate useful stuff.
1828 (multiple-value-bind (input output mask)
1830 (:input (values t nil sb!unix:o_rdonly))
1831 (:output (values nil t sb!unix:o_wronly))
1832 (:io (values t t sb!unix:o_rdwr))
1833 (:probe (values t nil sb!unix:o_rdonly)))
1834 (declare (type index mask))
1835 (let* ((pathname (merge-pathnames filename))
1837 (cond ((unix-namestring pathname input))
1838 ((and input (eq if-does-not-exist :create))
1839 (unix-namestring pathname nil))
1840 ((and (eq direction :io) (not if-does-not-exist-given))
1841 (unix-namestring pathname nil)))))
1842 ;; Process if-exists argument if we are doing any output.
1844 (unless if-exists-given
1846 (if (eq (pathname-version pathname) :newest)
1849 (ensure-one-of if-exists
1850 '(:error :new-version :rename
1851 :rename-and-delete :overwrite
1852 :append :supersede nil)
1855 ((:new-version :error nil)
1856 (setf mask (logior mask sb!unix:o_excl)))
1857 ((:rename :rename-and-delete)
1858 (setf mask (logior mask sb!unix:o_creat)))
1860 (setf mask (logior mask sb!unix:o_trunc)))
1862 (setf mask (logior mask sb!unix:o_append)))))
1864 (setf if-exists :ignore-this-arg)))
1866 (unless if-does-not-exist-given
1867 (setf if-does-not-exist
1868 (cond ((eq direction :input) :error)
1870 (member if-exists '(:overwrite :append)))
1872 ((eq direction :probe)
1876 (ensure-one-of if-does-not-exist
1877 '(:error :create nil)
1879 (if (eq if-does-not-exist :create)
1880 (setf mask (logior mask sb!unix:o_creat)))
1882 (let ((original (case if-exists
1883 ((:rename :rename-and-delete)
1884 (pick-backup-name namestring))
1886 ;; KLUDGE: Provent CLOSE from deleting
1887 ;; appending streams when called with :ABORT T
1889 (delete-original (eq if-exists :rename-and-delete))
1891 (when (and original (not (eq original namestring)))
1892 ;; We are doing a :RENAME or :RENAME-AND-DELETE. Determine
1893 ;; whether the file already exists, make sure the original
1894 ;; file is not a directory, and keep the mode.
1897 (multiple-value-bind (okay err/dev inode orig-mode)
1898 (sb!unix:unix-stat namestring)
1899 (declare (ignore inode)
1900 (type (or index null) orig-mode))
1903 (when (and output (= (logand orig-mode #o170000)
1905 (error 'simple-file-error
1906 :pathname namestring
1908 "can't open ~S for output: is a directory"
1909 :format-arguments (list namestring)))
1910 (setf mode (logand orig-mode #o777))
1912 ((eql err/dev sb!unix:enoent)
1915 (simple-file-perror "can't find ~S"
1919 (rename-the-old-one namestring original))
1921 (setf delete-original nil)
1922 ;; In order to use :SUPERSEDE instead, we have to make
1923 ;; sure SB!UNIX:O_CREAT corresponds to
1924 ;; IF-DOES-NOT-EXIST. SB!UNIX:O_CREAT was set before
1925 ;; because of IF-EXISTS being :RENAME.
1926 (unless (eq if-does-not-exist :create)
1928 (logior (logandc2 mask sb!unix:o_creat)
1930 (setf if-exists :supersede))))
1932 ;; Now we can try the actual Unix open(2).
1933 (multiple-value-bind (fd errno)
1935 (sb!unix:unix-open namestring mask mode)
1936 (values nil sb!unix:enoent))
1937 (labels ((open-error (format-control &rest format-arguments)
1938 (error 'simple-file-error
1940 :format-control format-control
1941 :format-arguments format-arguments))
1942 (vanilla-open-error ()
1943 (simple-file-perror "error opening ~S" pathname errno)))
1946 ((:input :output :io)
1950 :element-type element-type
1951 :external-format external-format
1954 :delete-original delete-original
1960 (%make-fd-stream :name namestring
1963 :element-type element-type)))
1966 ((eql errno sb!unix:enoent)
1967 (case if-does-not-exist
1968 (:error (vanilla-open-error))
1970 (open-error "~@<The path ~2I~_~S ~I~_does not exist.~:>"
1973 ((and (eql errno sb!unix:eexist) (null if-exists))
1976 (vanilla-open-error)))))))))
1980 ;;; the stream connected to the controlling terminal, or NIL if there is none
1983 ;;; the stream connected to the standard input (file descriptor 0)
1986 ;;; the stream connected to the standard output (file descriptor 1)
1989 ;;; the stream connected to the standard error output (file descriptor 2)
1992 ;;; This is called when the cold load is first started up, and may also
1993 ;;; be called in an attempt to recover from nested errors.
1994 (defun stream-cold-init-or-reset ()
1996 (setf *terminal-io* (make-synonym-stream '*tty*))
1997 (setf *standard-output* (make-synonym-stream '*stdout*))
1998 (setf *standard-input* (make-synonym-stream '*stdin*))
1999 (setf *error-output* (make-synonym-stream '*stderr*))
2000 (setf *query-io* (make-synonym-stream '*terminal-io*))
2001 (setf *debug-io* *query-io*)
2002 (setf *trace-output* *standard-output*)
2005 ;;; This is called whenever a saved core is restarted.
2006 (defun stream-reinit ()
2007 (setf *available-buffers* nil)
2009 (make-fd-stream 0 :name "standard input" :input t :buffering :line))
2011 (make-fd-stream 1 :name "standard output" :output t :buffering :line))
2013 (make-fd-stream 2 :name "standard error" :output t :buffering :line))
2014 (let* ((ttyname #.(coerce "/dev/tty" 'simple-base-string))
2015 (tty (sb!unix:unix-open ttyname sb!unix:o_rdwr #o666)))
2019 :name "the terminal"
2024 (setf *tty* (make-two-way-stream *stdin* *stdout*))))
2029 ;;; the Unix way to beep
2030 (defun beep (stream)
2031 (write-char (code-char bell-char-code) stream)
2032 (finish-output stream))
2034 ;;; This is kind of like FILE-POSITION, but is an internal hack used
2035 ;;; by the filesys stuff to get and set the file name.
2037 ;;; FIXME: misleading name, screwy interface
2038 (defun file-name (stream &optional new-name)
2039 (when (typep stream 'fd-stream)
2041 (setf (fd-stream-pathname stream) new-name)
2042 (setf (fd-stream-file stream)
2043 (unix-namestring new-name nil))
2046 (fd-stream-pathname stream)))))
2048 ;;;; international character support (which is trivial for our simple
2049 ;;;; character sets)
2051 ;;;; (Those who do Lisp only in English might not remember that ANSI
2052 ;;;; requires these functions to be exported from package
2055 (defun file-string-length (stream object)
2056 (declare (type (or string character) object) (type fd-stream stream))
2058 "Return the delta in STREAM's FILE-POSITION that would be caused by writing
2059 OBJECT to STREAM. Non-trivial only in implementations that support
2060 international character sets."
2061 (declare (ignore stream))
2064 (string (length object))))
2066 (defun stream-external-format (stream)
2067 (declare (type fd-stream stream))
2069 "Return the actual external format for fd-streams, otherwise :DEFAULT."
2070 (if (typep stream 'fd-stream)
2071 (fd-stream-external-format stream)