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 ;;; Returning true goes into end of file handling, false will enter another
124 ;;; round of input buffer filling followed by re-entering character decode.
125 (defun stream-decoding-error-and-handle (stream octet-count)
127 (stream-decoding-error stream
128 (let ((sap (fd-stream-ibuf-sap stream))
129 (head (fd-stream-ibuf-head stream)))
130 (loop for i from 0 below octet-count
131 collect (sap-ref-8 sap (+ head i)))))
133 :report (lambda (stream)
135 "~@<Attempt to resync the stream at a character ~
136 character boundary and continue.~@:>"))
137 (fd-stream-resync stream)
139 (force-end-of-file ()
140 :report (lambda (stream)
141 (format stream "~@<Force an end of file.~@:>"))
144 (defun stream-encoding-error-and-handle (stream code)
146 (stream-encoding-error stream code)
148 :report (lambda (stream)
149 (format stream "~@<Skip output of this character.~@:>"))
150 (throw 'output-nothing nil))))
152 ;;; This is called by the server when we can write to the given file
153 ;;; descriptor. Attempt to write the data again. If it worked, remove
154 ;;; the data from the OUTPUT-LATER list. If it didn't work, something
156 (defun frob-output-later (stream)
157 (let* ((stuff (pop (fd-stream-output-later stream)))
161 (reuse-sap (cadddr stuff))
162 (length (- end start)))
163 (declare (type index start end length))
164 (multiple-value-bind (count errno)
165 (sb!unix:unix-write (fd-stream-fd stream)
170 (if (= errno sb!unix:ewouldblock)
171 (error "Write would have blocked, but SERVER told us to go.")
172 (simple-stream-perror "couldn't write to ~S" stream errno)))
173 ((eql count length) ; Hot damn, it worked.
175 (push base *available-buffers*)))
176 ((not (null count)) ; sorta worked..
178 (the index (+ start count))
180 (fd-stream-output-later stream))))))
181 (unless (fd-stream-output-later stream)
182 (sb!sys:remove-fd-handler (fd-stream-handler stream))
183 (setf (fd-stream-handler stream) nil)))
185 ;;; Arange to output the string when we can write on the file descriptor.
186 (defun output-later (stream base start end reuse-sap)
187 (cond ((null (fd-stream-output-later stream))
188 (setf (fd-stream-output-later stream)
189 (list (list base start end reuse-sap)))
190 (setf (fd-stream-handler stream)
191 (sb!sys:add-fd-handler (fd-stream-fd stream)
194 (declare (ignore fd))
195 (frob-output-later stream)))))
197 (nconc (fd-stream-output-later stream)
198 (list (list base start end reuse-sap)))))
200 (let ((new-buffer (next-available-buffer)))
201 (setf (fd-stream-obuf-sap stream) new-buffer)
202 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
204 ;;; Output the given noise. Check to see whether there are any pending
205 ;;; writes. If so, just queue this one. Otherwise, try to write it. If
206 ;;; this would block, queue it.
207 (defun frob-output (stream base start end reuse-sap)
208 (declare (type fd-stream stream)
209 (type (or system-area-pointer (simple-array * (*))) base)
210 (type index start end))
211 (if (not (null (fd-stream-output-later stream))) ; something buffered.
213 (output-later stream base start end reuse-sap)
214 ;; ### check to see whether any of this noise can be output
216 (let ((length (- end start)))
217 (multiple-value-bind (count errno)
218 (sb!unix:unix-write (fd-stream-fd stream) base start length)
220 (if (= errno sb!unix:ewouldblock)
221 (output-later stream base start end reuse-sap)
222 (simple-stream-perror "couldn't write to ~S"
225 ((not (eql count length))
226 (output-later stream base (the index (+ start count))
229 ;;; Flush any data in the output buffer.
230 (defun flush-output-buffer (stream)
231 (let ((length (fd-stream-obuf-tail stream)))
233 (frob-output stream (fd-stream-obuf-sap stream) 0 length t)
234 (setf (fd-stream-obuf-tail stream) 0))))
236 (defmacro output-wrapper/variable-width ((stream size buffering restart)
238 (let ((stream-var (gensym)))
239 `(let ((,stream-var ,stream)
241 ,(unless (eq (car buffering) :none)
242 `(when (< (fd-stream-obuf-length ,stream-var)
243 (+ (fd-stream-obuf-tail ,stream-var)
245 (flush-output-buffer ,stream-var)))
246 ,(unless (eq (car buffering) :none)
247 `(when (> (fd-stream-ibuf-tail ,stream-var)
248 (fd-stream-ibuf-head ,stream-var))
249 (file-position ,stream-var (file-position ,stream-var))))
251 `(catch 'output-nothing
253 (incf (fd-stream-obuf-tail ,stream-var) size))
256 (incf (fd-stream-obuf-tail ,stream-var) size)))
257 ,(ecase (car buffering)
259 `(flush-output-buffer ,stream-var))
261 `(when (eq (char-code byte) (char-code #\Newline))
262 (flush-output-buffer ,stream-var)))
266 (defmacro output-wrapper ((stream size buffering restart) &body body)
267 (let ((stream-var (gensym)))
268 `(let ((,stream-var ,stream))
269 ,(unless (eq (car buffering) :none)
270 `(when (< (fd-stream-obuf-length ,stream-var)
271 (+ (fd-stream-obuf-tail ,stream-var)
273 (flush-output-buffer ,stream-var)))
274 ,(unless (eq (car buffering) :none)
275 `(when (> (fd-stream-ibuf-tail ,stream-var)
276 (fd-stream-ibuf-head ,stream-var))
277 (file-position ,stream-var (file-position ,stream-var))))
279 `(catch 'output-nothing
281 (incf (fd-stream-obuf-tail ,stream-var) ,size))
284 (incf (fd-stream-obuf-tail ,stream-var) ,size)))
285 ,(ecase (car buffering)
287 `(flush-output-buffer ,stream-var))
289 `(when (eq (char-code byte) (char-code #\Newline))
290 (flush-output-buffer ,stream-var)))
294 (defmacro def-output-routines/variable-width
295 ((name-fmt size restart external-format &rest bufferings)
297 (declare (optimize (speed 1)))
302 (intern (let ((*print-case* :upcase))
303 (format nil name-fmt (car buffering))))))
305 (defun ,function (stream byte)
306 (output-wrapper/variable-width (stream ,size ,buffering ,restart)
308 (setf *output-routines*
309 (nconc *output-routines*
317 (cdr buffering)))))))
320 ;;; Define output routines that output numbers SIZE bytes long for the
321 ;;; given bufferings. Use BODY to do the actual output.
322 (defmacro def-output-routines ((name-fmt size restart &rest bufferings)
324 (declare (optimize (speed 1)))
329 (intern (let ((*print-case* :upcase))
330 (format nil name-fmt (car buffering))))))
332 (defun ,function (stream byte)
333 (output-wrapper (stream ,size ,buffering ,restart)
335 (setf *output-routines*
336 (nconc *output-routines*
344 (cdr buffering)))))))
347 ;;; FIXME: is this used anywhere any more?
348 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
354 (if (char= byte #\Newline)
355 (setf (fd-stream-char-pos stream) 0)
356 (incf (fd-stream-char-pos stream)))
357 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
360 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
363 (:none (unsigned-byte 8))
364 (:full (unsigned-byte 8)))
365 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
368 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
371 (:none (signed-byte 8))
372 (:full (signed-byte 8)))
373 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
374 (fd-stream-obuf-tail stream))
377 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
380 (:none (unsigned-byte 16))
381 (:full (unsigned-byte 16)))
382 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
385 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
388 (:none (signed-byte 16))
389 (:full (signed-byte 16)))
390 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
391 (fd-stream-obuf-tail stream))
394 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
397 (:none (unsigned-byte 32))
398 (:full (unsigned-byte 32)))
399 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
402 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
405 (:none (signed-byte 32))
406 (:full (signed-byte 32)))
407 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
408 (fd-stream-obuf-tail stream))
411 ;;; Do the actual output. If there is space to buffer the string,
412 ;;; buffer it. If the string would normally fit in the buffer, but
413 ;;; doesn't because of other stuff in the buffer, flush the old noise
414 ;;; out of the buffer and put the string in it. Otherwise we have a
415 ;;; very long string, so just send it directly (after flushing the
416 ;;; buffer, of course).
417 (defun output-raw-bytes (fd-stream thing &optional start end)
419 "Output THING to FD-STREAM. THING can be any kind of vector or a SAP. If
420 THING is a SAP, END must be supplied (as length won't work)."
421 (let ((start (or start 0))
422 (end (or end (length (the (simple-array * (*)) thing)))))
423 (declare (type index start end))
424 (when (> (fd-stream-ibuf-tail fd-stream)
425 (fd-stream-ibuf-head fd-stream))
426 (file-position fd-stream (file-position fd-stream)))
427 (let* ((len (fd-stream-obuf-length fd-stream))
428 (tail (fd-stream-obuf-tail fd-stream))
430 (bytes (- end start))
431 (newtail (+ tail bytes)))
432 (cond ((minusp bytes) ; error case
433 (error ":END before :START!"))
434 ((zerop bytes)) ; easy case
436 (if (system-area-pointer-p thing)
437 (system-area-ub8-copy thing start
438 (fd-stream-obuf-sap fd-stream)
441 ;; FIXME: There should be some type checking somewhere to
442 ;; verify that THING here is a vector, not just <not a SAP>.
443 (copy-ub8-to-system-area thing start
444 (fd-stream-obuf-sap fd-stream)
447 (setf (fd-stream-obuf-tail fd-stream) newtail))
449 (flush-output-buffer fd-stream)
450 (if (system-area-pointer-p thing)
451 (system-area-ub8-copy thing
453 (fd-stream-obuf-sap fd-stream)
456 ;; FIXME: There should be some type checking somewhere to
457 ;; verify that THING here is a vector, not just <not a SAP>.
458 (copy-ub8-to-system-area thing
460 (fd-stream-obuf-sap fd-stream)
463 (setf (fd-stream-obuf-tail fd-stream) bytes))
465 (flush-output-buffer fd-stream)
466 (frob-output fd-stream thing start end nil))))))
468 ;;; the routine to use to output a string. If the stream is
469 ;;; unbuffered, slam the string down the file descriptor, otherwise
470 ;;; use OUTPUT-RAW-BYTES to buffer the string. Update charpos by
471 ;;; checking to see where the last newline was.
473 ;;; Note: some bozos (the FASL dumper) call write-string with things
474 ;;; other than strings. Therefore, we must make sure we have a string
475 ;;; before calling POSITION on it.
476 ;;; KLUDGE: It would be better to fix the bozos instead of trying to
477 ;;; cover for them here. -- WHN 20000203
478 (defun fd-sout (stream thing start end)
479 (let ((start (or start 0))
480 (end (or end (length (the vector thing)))))
481 (declare (fixnum start end))
483 (let ((last-newline (and (find #\newline (the simple-string thing)
484 :start start :end end)
485 ;; FIXME why do we need both calls?
486 ;; Is find faster forwards than
487 ;; position is backwards?
488 (position #\newline (the simple-string thing)
492 (if (and (typep thing 'base-string)
493 (eq (fd-stream-external-format stream) :latin-1))
494 (ecase (fd-stream-buffering stream)
496 (output-raw-bytes stream thing start end))
498 (output-raw-bytes stream thing start end)
500 (flush-output-buffer stream)))
502 (frob-output stream thing start end nil)))
503 (ecase (fd-stream-buffering stream)
504 (:full (funcall (fd-stream-output-bytes stream)
505 stream thing nil start end))
506 (:line (funcall (fd-stream-output-bytes stream)
507 stream thing last-newline start end))
508 (:none (funcall (fd-stream-output-bytes stream)
509 stream thing t start end))))
511 (setf (fd-stream-char-pos stream)
512 (- end last-newline 1))
513 (incf (fd-stream-char-pos stream)
515 (ecase (fd-stream-buffering stream)
517 (output-raw-bytes stream thing start end))
519 (frob-output stream thing start end nil))))))
521 (defvar *external-formats* ()
523 "List of all available external formats. Each element is a list of the
524 element-type, string input function name, character input function name,
525 and string output function name.")
527 ;;; Find an output routine to use given the type and buffering. Return
528 ;;; as multiple values the routine, the real type transfered, and the
529 ;;; number of bytes per element.
530 (defun pick-output-routine (type buffering &optional external-format)
531 (when (subtypep type 'character)
532 (dolist (entry *external-formats*)
533 (when (member external-format (first entry))
534 (return-from pick-output-routine
535 (values (symbol-function (nth (ecase buffering
542 (symbol-function (fourth entry))
543 (first (first entry)))))))
544 (dolist (entry *output-routines*)
545 (when (and (subtypep type (first entry))
546 (eq buffering (second entry))
547 (or (not (fifth entry))
548 (eq external-format (fifth entry))))
549 (return-from pick-output-routine
550 (values (symbol-function (third entry))
553 ;; KLUDGE: dealing with the buffering here leads to excessive code
556 ;; KLUDGE: also see comments in PICK-INPUT-ROUTINE
557 (loop for i from 40 by 8 to 1024 ; ARB (KLUDGE)
558 if (subtypep type `(unsigned-byte ,i))
559 do (return-from pick-output-routine
563 (lambda (stream byte)
564 (output-wrapper (stream (/ i 8) (:none) nil)
565 (loop for j from 0 below (/ i 8)
567 (fd-stream-obuf-sap stream)
568 (+ j (fd-stream-obuf-tail stream)))
569 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
571 (lambda (stream byte)
572 (output-wrapper (stream (/ i 8) (:full) nil)
573 (loop for j from 0 below (/ i 8)
575 (fd-stream-obuf-sap stream)
576 (+ j (fd-stream-obuf-tail stream)))
577 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
580 (loop for i from 40 by 8 to 1024 ; ARB (KLUDGE)
581 if (subtypep type `(signed-byte ,i))
582 do (return-from pick-output-routine
586 (lambda (stream byte)
587 (output-wrapper (stream (/ i 8) (:none) nil)
588 (loop for j from 0 below (/ i 8)
590 (fd-stream-obuf-sap stream)
591 (+ j (fd-stream-obuf-tail stream)))
592 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
594 (lambda (stream byte)
595 (output-wrapper (stream (/ i 8) (:full) nil)
596 (loop for j from 0 below (/ i 8)
598 (fd-stream-obuf-sap stream)
599 (+ j (fd-stream-obuf-tail stream)))
600 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
604 ;;;; input routines and related noise
606 ;;; a list of all available input routines. Each element is a list of
607 ;;; the element-type input, the function name, and the number of bytes
609 (defvar *input-routines* ())
611 ;;; Fill the input buffer, and return the first character. Throw to
612 ;;; EOF-INPUT-CATCHER if the eof was reached. Drop into SYSTEM:SERVER
614 (defun frob-input (stream)
615 (let ((fd (fd-stream-fd stream))
616 (ibuf-sap (fd-stream-ibuf-sap stream))
617 (buflen (fd-stream-ibuf-length stream))
618 (head (fd-stream-ibuf-head stream))
619 (tail (fd-stream-ibuf-tail stream)))
620 (declare (type index head tail))
622 (cond ((eql head tail)
625 (setf (fd-stream-ibuf-head stream) 0)
626 (setf (fd-stream-ibuf-tail stream) 0))
629 (system-area-ub8-copy ibuf-sap head
632 (setf (fd-stream-ibuf-head stream) 0)
633 (setf (fd-stream-ibuf-tail stream) tail))))
634 (setf (fd-stream-listen stream) nil)
635 (multiple-value-bind (count errno)
636 ;; FIXME: Judging from compiler warnings, this WITH-ALIEN form expands
637 ;; into something which uses the not-yet-defined type
638 ;; (SB!ALIEN-INTERNALS:ALIEN (* (SB!ALIEN:STRUCT SB!UNIX:FD-SET))).
639 ;; This is probably inefficient and unsafe and generally bad, so
640 ;; try to find some way to make that type known before
642 (sb!alien:with-alien ((read-fds (sb!alien:struct sb!unix:fd-set)))
643 (sb!unix:fd-zero read-fds)
644 (sb!unix:fd-set fd read-fds)
645 (sb!unix:unix-fast-select (1+ fd)
646 (sb!alien:addr read-fds)
654 (unless (sb!sys:wait-until-fd-usable
655 fd :input (fd-stream-timeout stream))
656 (error 'io-timeout :stream stream :direction :read)))
658 (simple-stream-perror "couldn't check whether ~S is readable"
661 (multiple-value-bind (count errno)
662 (sb!unix:unix-read fd
663 (sb!sys:int-sap (+ (sb!sys:sap-int ibuf-sap) tail))
666 (if (eql errno sb!unix:ewouldblock)
668 (unless (sb!sys:wait-until-fd-usable
669 fd :input (fd-stream-timeout stream))
670 (error 'io-timeout :stream stream :direction :read))
672 (simple-stream-perror "couldn't read from ~S" stream errno)))
674 (setf (fd-stream-listen stream) :eof)
675 (/show0 "THROWing EOF-INPUT-CATCHER")
676 (throw 'eof-input-catcher nil))
678 (incf (fd-stream-ibuf-tail stream) count))))))
680 ;;; Make sure there are at least BYTES number of bytes in the input
681 ;;; buffer. Keep calling FROB-INPUT until that condition is met.
682 (defmacro input-at-least (stream bytes)
683 (let ((stream-var (gensym))
684 (bytes-var (gensym)))
685 `(let ((,stream-var ,stream)
688 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
689 (fd-stream-ibuf-head ,stream-var))
692 (frob-input ,stream-var)))))
694 (defmacro input-wrapper/variable-width ((stream bytes eof-error eof-value)
696 (let ((stream-var (gensym))
698 (element-var (gensym)))
699 `(let ((,stream-var ,stream)
701 (if (fd-stream-unread ,stream-var)
703 (fd-stream-unread ,stream-var)
704 (setf (fd-stream-unread ,stream-var) nil)
705 (setf (fd-stream-listen ,stream-var) nil))
706 (let ((,element-var nil)
707 (decode-break-reason nil))
711 (catch 'eof-input-catcher
712 (setf decode-break-reason
713 (block decode-break-reason
714 (input-at-least ,stream-var 1)
715 (let* ((byte (sap-ref-8 (fd-stream-ibuf-sap
720 (input-at-least ,stream-var size)
721 (setq ,element-var (locally ,@read-forms))
722 (setq ,retry-var nil))
724 (when decode-break-reason
725 (stream-decoding-error-and-handle stream
726 decode-break-reason))
728 (let ((octet-count (- (fd-stream-ibuf-tail ,stream-var)
729 (fd-stream-ibuf-head ,stream-var))))
730 (when (or (zerop octet-count)
731 (and (not ,element-var)
732 (not decode-break-reason)
733 (stream-decoding-error-and-handle
734 stream octet-count)))
735 (setq ,retry-var nil)))))
737 (incf (fd-stream-ibuf-head ,stream-var) size)
740 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
742 ;;; a macro to wrap around all input routines to handle EOF-ERROR noise
743 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
744 (let ((stream-var (gensym))
745 (element-var (gensym)))
746 `(let ((,stream-var ,stream))
747 (if (fd-stream-unread ,stream-var)
749 (fd-stream-unread ,stream-var)
750 (setf (fd-stream-unread ,stream-var) nil)
751 (setf (fd-stream-listen ,stream-var) nil))
753 (catch 'eof-input-catcher
754 (input-at-least ,stream-var ,bytes)
755 (locally ,@read-forms))))
757 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
760 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
762 (defmacro def-input-routine/variable-width (name
763 (type external-format size sap head)
766 (defun ,name (stream eof-error eof-value)
767 (input-wrapper/variable-width (stream ,size eof-error eof-value)
768 (let ((,sap (fd-stream-ibuf-sap stream))
769 (,head (fd-stream-ibuf-head stream)))
771 (setf *input-routines*
772 (nconc *input-routines*
773 (list (list ',type ',name 1 ',external-format))))))
775 (defmacro def-input-routine (name
779 (defun ,name (stream eof-error eof-value)
780 (input-wrapper (stream ,size eof-error eof-value)
781 (let ((,sap (fd-stream-ibuf-sap stream))
782 (,head (fd-stream-ibuf-head stream)))
784 (setf *input-routines*
785 (nconc *input-routines*
786 (list (list ',type ',name ',size nil))))))
788 ;;; STREAM-IN routine for reading a string char
789 (def-input-routine input-character
790 (character 1 sap head)
791 (code-char (sap-ref-8 sap head)))
793 ;;; STREAM-IN routine for reading an unsigned 8 bit number
794 (def-input-routine input-unsigned-8bit-byte
795 ((unsigned-byte 8) 1 sap head)
796 (sap-ref-8 sap head))
798 ;;; STREAM-IN routine for reading a signed 8 bit number
799 (def-input-routine input-signed-8bit-number
800 ((signed-byte 8) 1 sap head)
801 (signed-sap-ref-8 sap head))
803 ;;; STREAM-IN routine for reading an unsigned 16 bit number
804 (def-input-routine input-unsigned-16bit-byte
805 ((unsigned-byte 16) 2 sap head)
806 (sap-ref-16 sap head))
808 ;;; STREAM-IN routine for reading a signed 16 bit number
809 (def-input-routine input-signed-16bit-byte
810 ((signed-byte 16) 2 sap head)
811 (signed-sap-ref-16 sap head))
813 ;;; STREAM-IN routine for reading a unsigned 32 bit number
814 (def-input-routine input-unsigned-32bit-byte
815 ((unsigned-byte 32) 4 sap head)
816 (sap-ref-32 sap head))
818 ;;; STREAM-IN routine for reading a signed 32 bit number
819 (def-input-routine input-signed-32bit-byte
820 ((signed-byte 32) 4 sap head)
821 (signed-sap-ref-32 sap head))
825 ;;; Find an input routine to use given the type. Return as multiple
826 ;;; values the routine, the real type transfered, and the number of
827 ;;; bytes per element (and for character types string input routine).
828 (defun pick-input-routine (type &optional external-format)
829 (when (subtypep type 'character)
830 (dolist (entry *external-formats*)
831 (when (member external-format (first entry))
832 (return-from pick-input-routine
833 (values (symbol-function (third entry))
836 (symbol-function (second entry))
837 (first (first entry)))))))
838 (dolist (entry *input-routines*)
839 (when (and (subtypep type (first entry))
840 (or (not (fourth entry))
841 (eq external-format (fourth entry))))
842 (return-from pick-input-routine
843 (values (symbol-function (second entry))
846 ;; FIXME: let's do it the hard way, then (but ignore things like
847 ;; endianness, efficiency, and the necessary coupling between these
848 ;; and the output routines). -- CSR, 2004-02-09
849 (loop for i from 40 by 8 to 1024 ; ARB (well, KLUDGE really)
850 if (subtypep type `(unsigned-byte ,i))
851 do (return-from pick-input-routine
853 (lambda (stream eof-error eof-value)
854 (input-wrapper (stream (/ i 8) eof-error eof-value)
855 (let ((sap (fd-stream-ibuf-sap stream))
856 (head (fd-stream-ibuf-head stream)))
857 (loop for j from 0 below (/ i 8)
861 (sap-ref-8 sap (+ head j))))
862 finally (return result)))))
865 (loop for i from 40 by 8 to 1024 ; ARB (well, KLUDGE really)
866 if (subtypep type `(signed-byte ,i))
867 do (return-from pick-input-routine
869 (lambda (stream eof-error eof-value)
870 (input-wrapper (stream (/ i 8) eof-error eof-value)
871 (let ((sap (fd-stream-ibuf-sap stream))
872 (head (fd-stream-ibuf-head stream)))
873 (loop for j from 0 below (/ i 8)
877 (sap-ref-8 sap (+ head j))))
878 finally (return (if (logbitp (1- i) result)
879 (dpb result (byte i 0) -1)
884 ;;; Return a string constructed from SAP, START, and END.
885 (defun string-from-sap (sap start end)
886 (declare (type index start end))
887 (let* ((length (- end start))
888 (string (make-string length)))
889 (copy-ub8-from-system-area sap start
894 ;;; the N-BIN method for FD-STREAMs
896 ;;; Note that this blocks in UNIX-READ. It is generally used where
897 ;;; there is a definite amount of reading to be done, so blocking
898 ;;; isn't too problematical.
899 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p
900 &aux (total-copied 0))
901 (declare (type fd-stream stream))
902 (declare (type index start requested total-copied))
903 (let ((unread (fd-stream-unread stream)))
905 ;; AVERs designed to fail when we have more complicated
906 ;; character representations.
907 (aver (typep unread 'base-char))
908 (aver (= (fd-stream-element-size stream) 1))
909 ;; KLUDGE: this is a slightly-unrolled-and-inlined version of
913 (setf (sap-ref-8 buffer start) (char-code unread)))
914 ((simple-unboxed-array (*))
915 (setf (aref buffer start) unread)))
916 (setf (fd-stream-unread stream) nil)
917 (setf (fd-stream-listen stream) nil)
918 (incf total-copied)))
921 (let* ((remaining-request (- requested total-copied))
922 (head (fd-stream-ibuf-head stream))
923 (tail (fd-stream-ibuf-tail stream))
924 (available (- tail head))
925 (n-this-copy (min remaining-request available))
926 (this-start (+ start total-copied))
927 (this-end (+ this-start n-this-copy))
928 (sap (fd-stream-ibuf-sap stream)))
929 (declare (type index remaining-request head tail available))
930 (declare (type index n-this-copy))
931 ;; Copy data from stream buffer into user's buffer.
932 (%byte-blt sap head buffer this-start this-end)
933 (incf (fd-stream-ibuf-head stream) n-this-copy)
934 (incf total-copied n-this-copy)
935 ;; Maybe we need to refill the stream buffer.
936 (cond (;; If there were enough data in the stream buffer, we're done.
937 (= total-copied requested)
938 (return total-copied))
939 (;; If EOF, we're done in another way.
940 (zerop (refill-fd-stream-buffer stream))
942 (error 'end-of-file :stream stream)
943 (return total-copied)))
944 ;; Otherwise we refilled the stream buffer, so fall
945 ;; through into another pass of the loop.
948 ;;; Try to refill the stream buffer. Return the number of bytes read.
949 ;;; (For EOF, the return value will be zero, otherwise positive.)
950 (defun refill-fd-stream-buffer (stream)
951 ;; We don't have any logic to preserve leftover bytes in the buffer,
952 ;; so we should only be called when the buffer is empty.
953 ;; FIXME: can have three bytes in buffer because of UTF-8
955 (sap (fd-stream-ibuf-sap stream)))
956 (do ((head (fd-stream-ibuf-head stream) (1+ head))
957 (tail (fd-stream-ibuf-tail stream)))
959 (setf (sap-ref-8 sap new-head) (sap-ref-8 sap head))
961 (multiple-value-bind (count err)
962 (sb!unix:unix-read (fd-stream-fd stream)
964 (- (fd-stream-ibuf-length stream) new-head))
965 (declare (type (or index null) count))
967 (simple-stream-perror "couldn't read from ~S" stream err))
968 (setf (fd-stream-listen stream) nil
969 (fd-stream-ibuf-head stream) 0
970 (fd-stream-ibuf-tail stream) (+ count new-head))
973 (defun fd-stream-resync (stream)
974 (dolist (entry *external-formats*)
975 (when (member (fd-stream-external-format stream) (first entry))
976 (return-from fd-stream-resync
977 (funcall (symbol-function (eighth entry)) stream)))))
979 (defmacro define-external-format (external-format size output-restart
981 (let* ((name (first external-format))
982 (out-function (intern (let ((*print-case* :upcase))
983 (format nil "OUTPUT-BYTES/~A" name))))
984 (format (format nil "OUTPUT-CHAR-~A-~~A-BUFFERED" name))
985 (in-function (intern (let ((*print-case* :upcase))
986 (format nil "FD-STREAM-READ-N-CHARACTERS/~A"
988 (in-char-function (intern (let ((*print-case* :upcase))
989 (format nil "INPUT-CHAR/~A" name)))))
991 (defun ,out-function (stream string flush-p start end)
992 (let ((start (or start 0))
993 (end (or end (length string))))
994 (declare (type index start end))
995 (when (> (fd-stream-ibuf-tail stream)
996 (fd-stream-ibuf-head stream))
997 (file-position stream (file-position stream)))
999 (error ":END before :START!"))
1002 (setf (fd-stream-obuf-tail stream)
1003 (do* ((len (fd-stream-obuf-length stream))
1004 (sap (fd-stream-obuf-sap stream))
1005 (tail (fd-stream-obuf-tail stream)))
1006 ((or (= start end) (< (- len tail) 4)) tail)
1008 `(catch 'output-nothing
1009 (let* ((byte (aref string start))
1010 (bits (char-code byte)))
1013 `(let* ((byte (aref string start))
1014 (bits (char-code byte)))
1019 (flush-output-buffer stream)))
1021 (flush-output-buffer stream))))
1022 (def-output-routines (,format
1028 (if (char= byte #\Newline)
1029 (setf (fd-stream-char-pos stream) 0)
1030 (incf (fd-stream-char-pos stream)))
1031 (let ((bits (char-code byte))
1032 (sap (fd-stream-obuf-sap stream))
1033 (tail (fd-stream-obuf-tail stream)))
1035 (defun ,in-function (stream buffer start requested eof-error-p
1036 &aux (total-copied 0))
1037 (declare (type fd-stream stream))
1038 (declare (type index start requested total-copied))
1039 (let ((unread (fd-stream-unread stream)))
1041 (setf (aref buffer start) unread)
1042 (setf (fd-stream-unread stream) nil)
1043 (setf (fd-stream-listen stream) nil)
1044 (incf total-copied)))
1047 (let* ((head (fd-stream-ibuf-head stream))
1048 (tail (fd-stream-ibuf-tail stream))
1049 (sap (fd-stream-ibuf-sap stream)))
1050 (declare (type index head tail))
1051 ;; Copy data from stream buffer into user's buffer.
1053 ((or (= tail head) (= requested total-copied)))
1054 (let* ((byte (sap-ref-8 sap head)))
1055 (when (> ,size (- tail head))
1057 (setf (aref buffer (+ start total-copied)) ,in-expr)
1060 (setf (fd-stream-ibuf-head stream) head)
1061 ;; Maybe we need to refill the stream buffer.
1062 (cond ( ;; If there were enough data in the stream buffer, we're done.
1063 (= total-copied requested)
1064 (return total-copied))
1065 ( ;; If EOF, we're done in another way.
1066 (zerop (refill-fd-stream-buffer stream))
1068 (error 'end-of-file :stream stream)
1069 (return total-copied)))
1070 ;; Otherwise we refilled the stream buffer, so fall
1071 ;; through into another pass of the loop.
1073 (def-input-routine ,in-char-function (character ,size sap head)
1074 (let ((byte (sap-ref-8 sap head)))
1076 (setf *external-formats*
1077 (cons '(,external-format ,in-function ,in-char-function ,out-function
1078 ,@(mapcar #'(lambda (buffering)
1079 (intern (let ((*print-case* :upcase))
1080 (format nil format buffering))))
1081 '(:none :line :full)))
1082 *external-formats*)))))
1084 (defmacro define-external-format/variable-width
1085 (external-format output-restart out-size-expr
1086 out-expr in-size-expr in-expr)
1087 (let* ((name (first external-format))
1088 (out-function (intern (let ((*print-case* :upcase))
1089 (format nil "OUTPUT-BYTES/~A" name))))
1090 (format (format nil "OUTPUT-CHAR-~A-~~A-BUFFERED" name))
1091 (in-function (intern (let ((*print-case* :upcase))
1092 (format nil "FD-STREAM-READ-N-CHARACTERS/~A"
1094 (in-char-function (intern (let ((*print-case* :upcase))
1095 (format nil "INPUT-CHAR/~A" name))))
1096 (resync-function (intern (let ((*print-case* :upcase))
1097 (format nil "RESYNC/~A" name)))))
1099 (defun ,out-function (fd-stream string flush-p start end)
1100 (let ((start (or start 0))
1101 (end (or end (length string))))
1102 (declare (type index start end))
1103 (when (> (fd-stream-ibuf-tail fd-stream)
1104 (fd-stream-ibuf-head fd-stream))
1105 (file-position fd-stream (file-position fd-stream)))
1107 (error ":END before :START!"))
1110 (setf (fd-stream-obuf-tail fd-stream)
1111 (do* ((len (fd-stream-obuf-length fd-stream))
1112 (sap (fd-stream-obuf-sap fd-stream))
1113 (tail (fd-stream-obuf-tail fd-stream)))
1114 ((or (= start end) (< (- len tail) 4)) tail)
1116 `(catch 'output-nothing
1117 (let* ((byte (aref string start))
1118 (bits (char-code byte))
1119 (size ,out-size-expr))
1123 `(let* ((byte (aref string start))
1124 (bits (char-code byte))
1125 (size ,out-size-expr))
1130 (flush-output-buffer fd-stream)))
1132 (flush-output-buffer fd-stream))))
1133 (def-output-routines/variable-width (,format
1140 (if (char= byte #\Newline)
1141 (setf (fd-stream-char-pos stream) 0)
1142 (incf (fd-stream-char-pos stream)))
1143 (let ((bits (char-code byte))
1144 (sap (fd-stream-obuf-sap stream))
1145 (tail (fd-stream-obuf-tail stream)))
1147 (defun ,in-function (stream buffer start requested eof-error-p
1148 &aux (total-copied 0))
1149 (declare (type fd-stream stream))
1150 (declare (type index start requested total-copied))
1151 (let ((unread (fd-stream-unread stream)))
1153 (setf (aref buffer start) unread)
1154 (setf (fd-stream-unread stream) nil)
1155 (setf (fd-stream-listen stream) nil)
1156 (incf total-copied)))
1159 (let* ((head (fd-stream-ibuf-head stream))
1160 (tail (fd-stream-ibuf-tail stream))
1161 (sap (fd-stream-ibuf-sap stream))
1163 (decode-break-reason nil))
1164 (declare (type index head tail))
1165 ;; Copy data from stream buffer into user's buffer.
1166 (do ((size nil nil))
1167 ((or (= tail head) (= requested total-copied)))
1168 (setf decode-break-reason
1169 (block decode-break-reason
1170 (let ((byte (sap-ref-8 sap head)))
1171 (setq size ,in-size-expr)
1172 (when (> size (- tail head))
1174 (setf (aref buffer (+ start total-copied)) ,in-expr)
1178 (setf (fd-stream-ibuf-head stream) head)
1179 (when (and decode-break-reason
1181 (stream-decoding-error-and-handle
1182 stream decode-break-reason))
1184 (error 'end-of-file :stream stream)
1185 (return-from ,in-function total-copied)))
1186 (when (plusp total-copied)
1187 (return-from ,in-function total-copied)))
1188 (setf (fd-stream-ibuf-head stream) head)
1189 ;; Maybe we need to refill the stream buffer.
1190 (cond ( ;; If there were enough data in the stream buffer, we're done.
1191 (= total-copied requested)
1192 (return total-copied))
1193 ( ;; If EOF, we're done in another way.
1194 (or (eq decode-break-reason 'eof)
1195 (zerop (refill-fd-stream-buffer stream)))
1197 (error 'end-of-file :stream stream)
1198 (return total-copied)))
1199 ;; Otherwise we refilled the stream buffer, so fall
1200 ;; through into another pass of the loop.
1202 (def-input-routine/variable-width ,in-char-function (character
1206 (let ((byte (sap-ref-8 sap head)))
1208 (defun ,resync-function (stream)
1209 (loop (input-at-least stream 1)
1210 (incf (fd-stream-ibuf-head stream))
1211 (unless (block decode-break-reason
1212 (let* ((sap (fd-stream-ibuf-sap stream))
1213 (head (fd-stream-ibuf-head stream))
1214 (byte (sap-ref-8 sap head))
1215 (size ,in-size-expr))
1219 (setf *external-formats*
1220 (cons '(,external-format ,in-function ,in-char-function ,out-function
1221 ,@(mapcar #'(lambda (buffering)
1222 (intern (let ((*print-case* :upcase))
1223 (format nil format buffering))))
1224 '(:none :line :full))
1226 *external-formats*)))))
1228 (define-external-format (:latin-1 :latin1 :iso-8859-1)
1231 (stream-encoding-error-and-handle stream bits)
1232 (setf (sap-ref-8 sap tail) bits))
1235 (define-external-format (:ascii :us-ascii :ansi_x3.4-1968)
1238 (stream-encoding-error-and-handle stream bits)
1239 (setf (sap-ref-8 sap tail) bits))
1242 (let* ((table (let ((s (make-string 256)))
1243 (map-into s #'code-char
1244 '(#x00 #x01 #x02 #x03 #x9c #x09 #x86 #x7f #x97 #x8d #x8e #x0b #x0c #x0d #x0e #x0f
1245 #x10 #x11 #x12 #x13 #x9d #x85 #x08 #x87 #x18 #x19 #x92 #x8f #x1c #x1d #x1e #x1f
1246 #x80 #x81 #x82 #x83 #x84 #x0a #x17 #x1b #x88 #x89 #x8a #x8b #x8c #x05 #x06 #x07
1247 #x90 #x91 #x16 #x93 #x94 #x95 #x96 #x04 #x98 #x99 #x9a #x9b #x14 #x15 #x9e #x1a
1248 #x20 #xa0 #xe2 #xe4 #xe0 #xe1 #xe3 #xe5 #xe7 #xf1 #xa2 #x2e #x3c #x28 #x2b #x7c
1249 #x26 #xe9 #xea #xeb #xe8 #xed #xee #xef #xec #xdf #x21 #x24 #x2a #x29 #x3b #xac
1250 #x2d #x2f #xc2 #xc4 #xc0 #xc1 #xc3 #xc5 #xc7 #xd1 #xa6 #x2c #x25 #x5f #x3e #x3f
1251 #xf8 #xc9 #xca #xcb #xc8 #xcd #xce #xcf #xcc #x60 #x3a #x23 #x40 #x27 #x3d #x22
1252 #xd8 #x61 #x62 #x63 #x64 #x65 #x66 #x67 #x68 #x69 #xab #xbb #xf0 #xfd #xfe #xb1
1253 #xb0 #x6a #x6b #x6c #x6d #x6e #x6f #x70 #x71 #x72 #xaa #xba #xe6 #xb8 #xc6 #xa4
1254 #xb5 #x7e #x73 #x74 #x75 #x76 #x77 #x78 #x79 #x7a #xa1 #xbf #xd0 #xdd #xde #xae
1255 #x5e #xa3 #xa5 #xb7 #xa9 #xa7 #xb6 #xbc #xbd #xbe #x5b #x5d #xaf #xa8 #xb4 #xd7
1256 #x7b #x41 #x42 #x43 #x44 #x45 #x46 #x47 #x48 #x49 #xad #xf4 #xf6 #xf2 #xf3 #xf5
1257 #x7d #x4a #x4b #x4c #x4d #x4e #x4f #x50 #x51 #x52 #xb9 #xfb #xfc #xf9 #xfa #xff
1258 #x5c #xf7 #x53 #x54 #x55 #x56 #x57 #x58 #x59 #x5a #xb2 #xd4 #xd6 #xd2 #xd3 #xd5
1259 #x30 #x31 #x32 #x33 #x34 #x35 #x36 #x37 #x38 #x39 #xb3 #xdb #xdc #xd9 #xda #x9f))
1261 (reverse-table (let ((rt (make-array 256 :element-type '(unsigned-byte 8) :initial-element 0)))
1262 (loop for char across table for i from 0
1263 do (aver (= 0 (aref rt (char-code char))))
1264 do (setf (aref rt (char-code char)) i))
1266 (define-external-format (:ebcdic-us :ibm-037 :ibm037)
1269 (stream-encoding-error-and-handle stream bits)
1270 (setf (sap-ref-8 sap tail) (aref reverse-table bits)))
1275 (let ((latin-9-table (let ((table (make-string 256)))
1278 (setf (aref table i) (code-char i)))
1279 (setf (aref table #xa4) (code-char #x20ac))
1280 (setf (aref table #xa6) (code-char #x0160))
1281 (setf (aref table #xa8) (code-char #x0161))
1282 (setf (aref table #xb4) (code-char #x017d))
1283 (setf (aref table #xb8) (code-char #x017e))
1284 (setf (aref table #xbc) (code-char #x0152))
1285 (setf (aref table #xbd) (code-char #x0153))
1286 (setf (aref table #xbe) (code-char #x0178))
1288 (latin-9-reverse-1 (make-array 16
1289 :element-type '(unsigned-byte 21)
1290 :initial-contents '(#x0160 #x0161 #x0152 #x0153 0 0 0 0 #x0178 0 0 0 #x20ac #x017d #x017e 0)))
1291 (latin-9-reverse-2 (make-array 16
1292 :element-type '(unsigned-byte 8)
1293 :initial-contents '(#xa6 #xa8 #xbc #xbd 0 0 0 0 #xbe 0 0 0 #xa4 #xb4 #xb8 0))))
1294 (define-external-format (:latin-9 :latin9 :iso-8859-15)
1296 (setf (sap-ref-8 sap tail)
1298 (if (= bits (char-code (aref latin-9-table bits)))
1300 (stream-encoding-error-and-handle stream byte))
1301 (if (= (aref latin-9-reverse-1 (logand bits 15)) bits)
1302 (aref latin-9-reverse-2 (logand bits 15))
1303 (stream-encoding-error-and-handle stream byte))))
1304 (aref latin-9-table byte)))
1306 (define-external-format/variable-width (:utf-8 :utf8) nil
1307 (let ((bits (char-code byte)))
1308 (cond ((< bits #x80) 1)
1310 ((< bits #x10000) 3)
1313 (1 (setf (sap-ref-8 sap tail) bits))
1314 (2 (setf (sap-ref-8 sap tail) (logior #xc0 (ldb (byte 5 6) bits))
1315 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 0) bits))))
1316 (3 (setf (sap-ref-8 sap tail) (logior #xe0 (ldb (byte 4 12) bits))
1317 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 6) bits))
1318 (sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 0) bits))))
1319 (4 (setf (sap-ref-8 sap tail) (logior #xf0 (ldb (byte 3 18) bits))
1320 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 12) bits))
1321 (sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 6) bits))
1322 (sap-ref-8 sap (+ 3 tail)) (logior #x80 (ldb (byte 6 0) bits)))))
1323 (cond ((< byte #x80) 1)
1324 ((< byte #xc2) (return-from decode-break-reason 1))
1328 (code-char (ecase size
1330 (2 (let ((byte2 (sap-ref-8 sap (1+ head))))
1331 (unless (<= #x80 byte2 #xbf)
1332 (return-from decode-break-reason 2))
1333 (dpb byte (byte 5 6) byte2)))
1334 (3 (let ((byte2 (sap-ref-8 sap (1+ head)))
1335 (byte3 (sap-ref-8 sap (+ 2 head))))
1336 (unless (and (<= #x80 byte2 #xbf)
1337 (<= #x80 byte3 #xbf))
1338 (return-from decode-break-reason 3))
1339 (dpb byte (byte 4 12) (dpb byte2 (byte 6 6) byte3))))
1340 (4 (let ((byte2 (sap-ref-8 sap (1+ head)))
1341 (byte3 (sap-ref-8 sap (+ 2 head)))
1342 (byte4 (sap-ref-8 sap (+ 3 head))))
1343 (unless (and (<= #x80 byte2 #xbf)
1344 (<= #x80 byte3 #xbf)
1345 (<= #x80 byte4 #xbf))
1346 (return-from decode-break-reason 4))
1347 (dpb byte (byte 3 18)
1348 (dpb byte2 (byte 6 12)
1349 (dpb byte3 (byte 6 6) byte4))))))))
1351 ;;;; utility functions (misc routines, etc)
1353 ;;; Fill in the various routine slots for the given type. INPUT-P and
1354 ;;; OUTPUT-P indicate what slots to fill. The buffering slot must be
1355 ;;; set prior to calling this routine.
1356 (defun set-fd-stream-routines (fd-stream type input-p output-p buffer-p)
1357 (let ((target-type (case type
1358 ((:default unsigned-byte)
1368 (character-stream-p (subtypep type 'character)))
1370 (when (fd-stream-obuf-sap fd-stream)
1371 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
1372 (setf (fd-stream-obuf-sap fd-stream) nil))
1373 (when (fd-stream-ibuf-sap fd-stream)
1374 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
1375 (setf (fd-stream-ibuf-sap fd-stream) nil))
1377 (when (and character-stream-p
1378 (eq (fd-stream-external-format fd-stream) :default))
1379 (setf (fd-stream-external-format fd-stream)
1380 (intern (or (alien-funcall
1381 (extern-alien "nl_langinfo"
1382 (function c-string int))
1386 (dolist (entry *external-formats*
1387 (setf (fd-stream-external-format fd-stream) :latin-1))
1388 (when (member (fd-stream-external-format fd-stream) (first entry))
1392 (multiple-value-bind (routine type size read-n-characters
1393 normalized-external-format)
1394 (pick-input-routine target-type
1395 (fd-stream-external-format fd-stream))
1396 (when normalized-external-format
1397 (setf (fd-stream-external-format fd-stream)
1398 normalized-external-format))
1400 (error "could not find any input routine for ~S" target-type))
1401 (setf (fd-stream-ibuf-sap fd-stream) (next-available-buffer))
1402 (setf (fd-stream-ibuf-length fd-stream) bytes-per-buffer)
1403 (setf (fd-stream-ibuf-tail fd-stream) 0)
1404 (if character-stream-p
1405 (setf (fd-stream-in fd-stream) routine
1406 (fd-stream-bin fd-stream) #'ill-bin)
1407 (setf (fd-stream-in fd-stream) #'ill-in
1408 (fd-stream-bin fd-stream) routine))
1410 (setf (fd-stream-n-bin fd-stream)
1411 (if character-stream-p
1413 #'fd-stream-read-n-bytes))
1415 ;; We only create this buffer for streams of type
1416 ;; (unsigned-byte 8). Because there's no buffer, the
1417 ;; other element-types will dispatch to the appropriate
1418 ;; input (output) routine in fast-read-byte.
1419 (or character-stream-p
1420 (equal target-type '(unsigned-byte 8)))
1421 (not output-p) ; temporary disable on :io streams
1423 (or (eq type 'unsigned-byte)
1424 (eq type :default)))
1425 (if character-stream-p
1426 (setf (ansi-stream-cin-buffer fd-stream)
1427 (make-array +ansi-stream-in-buffer-length+
1428 :element-type 'character))
1429 (setf (ansi-stream-in-buffer fd-stream)
1430 (make-array +ansi-stream-in-buffer-length+
1431 :element-type '(unsigned-byte 8))))))
1432 (setf input-size size)
1433 (setf input-type type)))
1436 (multiple-value-bind (routine type size output-bytes
1437 normalized-external-format)
1438 (pick-output-routine target-type
1439 (fd-stream-buffering fd-stream)
1440 (fd-stream-external-format fd-stream))
1441 (when normalized-external-format
1442 (setf (fd-stream-external-format fd-stream)
1443 normalized-external-format))
1445 (error "could not find any output routine for ~S buffered ~S"
1446 (fd-stream-buffering fd-stream)
1448 (setf (fd-stream-obuf-sap fd-stream) (next-available-buffer))
1449 (setf (fd-stream-obuf-length fd-stream) bytes-per-buffer)
1450 (setf (fd-stream-obuf-tail fd-stream) 0)
1451 (when character-stream-p
1452 (setf (fd-stream-output-bytes fd-stream) output-bytes))
1453 (if character-stream-p
1454 (setf (fd-stream-out fd-stream) routine
1455 (fd-stream-bout fd-stream) #'ill-bout)
1456 (setf (fd-stream-out fd-stream)
1457 (or (if (eql size 1)
1458 (pick-output-routine
1459 'base-char (fd-stream-buffering fd-stream)))
1461 (fd-stream-bout fd-stream) routine))
1462 (setf (fd-stream-sout fd-stream)
1463 (if (eql size 1) #'fd-sout #'ill-out))
1464 (setf (fd-stream-char-pos fd-stream) 0)
1465 (setf output-size size)
1466 (setf output-type type)))
1468 (when (and input-size output-size
1469 (not (eq input-size output-size)))
1470 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
1471 input-type input-size
1472 output-type output-size))
1473 (setf (fd-stream-element-size fd-stream)
1474 (or input-size output-size))
1476 (setf (fd-stream-element-type fd-stream)
1477 (cond ((equal input-type output-type)
1483 ((subtypep input-type output-type)
1485 ((subtypep output-type input-type)
1488 (error "Input type (~S) and output type (~S) are unrelated?"
1492 ;;; Handle miscellaneous operations on FD-STREAM.
1493 (defun fd-stream-misc-routine (fd-stream operation &optional arg1 arg2)
1494 (declare (ignore arg2))
1497 (or (not (eql (fd-stream-ibuf-head fd-stream)
1498 (fd-stream-ibuf-tail fd-stream)))
1499 (fd-stream-listen fd-stream)
1500 (setf (fd-stream-listen fd-stream)
1501 (eql (sb!alien:with-alien ((read-fds (sb!alien:struct
1503 (sb!unix:fd-zero read-fds)
1504 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
1505 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
1506 (sb!alien:addr read-fds)
1510 (setf (fd-stream-unread fd-stream) arg1)
1511 (setf (fd-stream-listen fd-stream) t))
1513 (cond (arg1 ; We got us an abort on our hands.
1514 (when (fd-stream-handler fd-stream)
1515 (sb!sys:remove-fd-handler (fd-stream-handler fd-stream))
1516 (setf (fd-stream-handler fd-stream) nil))
1517 ;; We can't do anything unless we know what file were
1518 ;; dealing with, and we don't want to do anything
1519 ;; strange unless we were writing to the file.
1520 (when (and (fd-stream-file fd-stream)
1521 (fd-stream-obuf-sap fd-stream))
1522 (if (fd-stream-original fd-stream)
1523 ;; If the original is EQ to file we are appending
1524 ;; and can just close the file without renaming.
1525 (unless (eq (fd-stream-original fd-stream)
1526 (fd-stream-file fd-stream))
1527 ;; We have a handle on the original, just revert.
1528 (multiple-value-bind (okay err)
1529 (sb!unix:unix-rename (fd-stream-original fd-stream)
1530 (fd-stream-file fd-stream))
1532 (simple-stream-perror
1533 "couldn't restore ~S to its original contents"
1536 ;; We can't restore the original, and aren't
1537 ;; appending, so nuke that puppy.
1539 ;; FIXME: This is currently the fate of superseded
1540 ;; files, and according to the CLOSE spec this is
1541 ;; wrong. However, there seems to be no clean way to
1542 ;; do that that doesn't involve either copying the
1543 ;; data (bad if the :abort resulted from a full
1544 ;; disk), or renaming the old file temporarily
1545 ;; (probably bad because stream opening becomes more
1547 (multiple-value-bind (okay err)
1548 (sb!unix:unix-unlink (fd-stream-file fd-stream))
1550 (error 'simple-file-error
1551 :pathname (fd-stream-file fd-stream)
1553 "~@<couldn't remove ~S: ~2I~_~A~:>"
1554 :format-arguments (list (fd-stream-file fd-stream)
1555 (strerror err))))))))
1557 (fd-stream-misc-routine fd-stream :finish-output)
1558 (when (and (fd-stream-original fd-stream)
1559 (fd-stream-delete-original fd-stream))
1560 (multiple-value-bind (okay err)
1561 (sb!unix:unix-unlink (fd-stream-original fd-stream))
1563 (error 'simple-file-error
1564 :pathname (fd-stream-original fd-stream)
1566 "~@<couldn't delete ~S during close of ~S: ~
1569 (list (fd-stream-original fd-stream)
1571 (strerror err))))))))
1572 (when (fboundp 'cancel-finalization)
1573 (cancel-finalization fd-stream))
1574 (sb!unix:unix-close (fd-stream-fd fd-stream))
1575 (when (fd-stream-obuf-sap fd-stream)
1576 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
1577 (setf (fd-stream-obuf-sap fd-stream) nil))
1578 (when (fd-stream-ibuf-sap fd-stream)
1579 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
1580 (setf (fd-stream-ibuf-sap fd-stream) nil))
1581 (sb!impl::set-closed-flame fd-stream))
1583 (setf (fd-stream-unread fd-stream) nil)
1584 (setf (fd-stream-ibuf-head fd-stream) 0)
1585 (setf (fd-stream-ibuf-tail fd-stream) 0)
1586 (catch 'eof-input-catcher
1588 (let ((count (sb!alien:with-alien ((read-fds (sb!alien:struct
1590 (sb!unix:fd-zero read-fds)
1591 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
1592 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
1593 (sb!alien:addr read-fds)
1598 (cond ((eql count 1)
1599 (frob-input fd-stream)
1600 (setf (fd-stream-ibuf-head fd-stream) 0)
1601 (setf (fd-stream-ibuf-tail fd-stream) 0))
1605 (flush-output-buffer fd-stream))
1607 (flush-output-buffer fd-stream)
1609 ((null (fd-stream-output-later fd-stream)))
1610 (sb!sys:serve-all-events)))
1612 (fd-stream-element-type fd-stream))
1614 (= 1 (the (member 0 1)
1615 (sb!unix:unix-isatty (fd-stream-fd fd-stream)))))
1619 (fd-stream-char-pos fd-stream))
1621 (unless (fd-stream-file fd-stream)
1622 ;; This is a TYPE-ERROR because ANSI's species FILE-LENGTH
1623 ;; "should signal an error of type TYPE-ERROR if stream is not
1624 ;; a stream associated with a file". Too bad there's no very
1625 ;; appropriate value for the EXPECTED-TYPE slot..
1626 (error 'simple-type-error
1628 :expected-type 'fd-stream
1629 :format-control "~S is not a stream associated with a file."
1630 :format-arguments (list fd-stream)))
1631 (multiple-value-bind (okay dev ino mode nlink uid gid rdev size
1632 atime mtime ctime blksize blocks)
1633 (sb!unix:unix-fstat (fd-stream-fd fd-stream))
1634 (declare (ignore ino nlink uid gid rdev
1635 atime mtime ctime blksize blocks))
1637 (simple-stream-perror "failed Unix fstat(2) on ~S" fd-stream dev))
1640 (truncate size (fd-stream-element-size fd-stream)))))
1642 (fd-stream-file-position fd-stream arg1))))
1644 (defun fd-stream-file-position (stream &optional newpos)
1645 (declare (type fd-stream stream)
1646 (type (or (alien sb!unix:off-t) (member nil :start :end)) newpos))
1648 (sb!sys:without-interrupts
1649 ;; First, find the position of the UNIX file descriptor in the file.
1650 (multiple-value-bind (posn errno)
1651 (sb!unix:unix-lseek (fd-stream-fd stream) 0 sb!unix:l_incr)
1652 (declare (type (or (alien sb!unix:off-t) null) posn))
1653 (cond ((integerp posn)
1654 ;; Adjust for buffered output: If there is any output
1655 ;; buffered, the *real* file position will be larger
1656 ;; than reported by lseek() because lseek() obviously
1657 ;; cannot take into account output we have not sent
1659 (dolist (later (fd-stream-output-later stream))
1660 (incf posn (- (caddr later)
1662 (incf posn (fd-stream-obuf-tail stream))
1663 ;; Adjust for unread input: If there is any input
1664 ;; read from UNIX but not supplied to the user of the
1665 ;; stream, the *real* file position will smaller than
1666 ;; reported, because we want to look like the unread
1667 ;; stuff is still available.
1668 (decf posn (- (fd-stream-ibuf-tail stream)
1669 (fd-stream-ibuf-head stream)))
1670 (when (fd-stream-unread stream)
1672 ;; Divide bytes by element size.
1673 (truncate posn (fd-stream-element-size stream)))
1674 ((eq errno sb!unix:espipe)
1677 (sb!sys:with-interrupts
1678 (simple-stream-perror "failure in Unix lseek() on ~S"
1681 (let ((offset 0) origin)
1682 (declare (type (alien sb!unix:off-t) offset))
1683 ;; Make sure we don't have any output pending, because if we
1684 ;; move the file pointer before writing this stuff, it will be
1685 ;; written in the wrong location.
1686 (flush-output-buffer stream)
1688 ((null (fd-stream-output-later stream)))
1689 (sb!sys:serve-all-events))
1690 ;; Clear out any pending input to force the next read to go to
1692 (setf (fd-stream-unread stream) nil)
1693 (setf (fd-stream-ibuf-head stream) 0)
1694 (setf (fd-stream-ibuf-tail stream) 0)
1695 ;; Trash cached value for listen, so that we check next time.
1696 (setf (fd-stream-listen stream) nil)
1698 (cond ((eq newpos :start)
1699 (setf offset 0 origin sb!unix:l_set))
1701 (setf offset 0 origin sb!unix:l_xtnd))
1702 ((typep newpos '(alien sb!unix:off-t))
1703 (setf offset (* newpos (fd-stream-element-size stream))
1704 origin sb!unix:l_set))
1706 (error "invalid position given to FILE-POSITION: ~S" newpos)))
1707 (multiple-value-bind (posn errno)
1708 (sb!unix:unix-lseek (fd-stream-fd stream) offset origin)
1709 (cond ((typep posn '(alien sb!unix:off-t))
1711 ((eq errno sb!unix:espipe)
1714 (simple-stream-perror "error in Unix lseek() on ~S"
1718 ;;;; creation routines (MAKE-FD-STREAM and OPEN)
1720 ;;; Create a stream for the given Unix file descriptor.
1722 ;;; If INPUT is non-NIL, allow input operations. If OUTPUT is non-nil,
1723 ;;; allow output operations. If neither INPUT nor OUTPUT is specified,
1724 ;;; default to allowing input.
1726 ;;; ELEMENT-TYPE indicates the element type to use (as for OPEN).
1728 ;;; BUFFERING indicates the kind of buffering to use.
1730 ;;; TIMEOUT (if true) is the number of seconds to wait for input. If
1731 ;;; NIL (the default), then wait forever. When we time out, we signal
1734 ;;; FILE is the name of the file (will be returned by PATHNAME).
1736 ;;; NAME is used to identify the stream when printed.
1737 (defun make-fd-stream (fd
1740 (output nil output-p)
1741 (element-type 'base-char)
1743 (external-format :default)
1751 (format nil "file ~S" file)
1752 (format nil "descriptor ~W" fd)))
1754 (declare (type index fd) (type (or index null) timeout)
1755 (type (member :none :line :full) buffering))
1756 (cond ((not (or input-p output-p))
1758 ((not (or input output))
1759 (error "File descriptor must be opened either for input or output.")))
1760 (let ((stream (%make-fd-stream :fd fd
1764 :delete-original delete-original
1766 :buffering buffering
1767 :external-format external-format
1769 (set-fd-stream-routines stream element-type input output input-buffer-p)
1770 (when (and auto-close (fboundp 'finalize))
1773 (sb!unix:unix-close fd)
1775 (format *terminal-io* "** closed file descriptor ~W **~%"
1779 ;;; Pick a name to use for the backup file for the :IF-EXISTS
1780 ;;; :RENAME-AND-DELETE and :RENAME options.
1781 (defun pick-backup-name (name)
1782 (declare (type simple-base-string name))
1783 (concatenate 'simple-base-string name ".bak"))
1785 ;;; Ensure that the given arg is one of the given list of valid
1786 ;;; things. Allow the user to fix any problems.
1787 (defun ensure-one-of (item list what)
1788 (unless (member item list)
1789 (error 'simple-type-error
1791 :expected-type `(member ,@list)
1792 :format-control "~@<~S is ~_invalid for ~S; ~_need one of~{ ~S~}~:>"
1793 :format-arguments (list item what list))))
1795 ;;; Rename NAMESTRING to ORIGINAL. First, check whether we have write
1796 ;;; access, since we don't want to trash unwritable files even if we
1797 ;;; technically can. We return true if we succeed in renaming.
1798 (defun rename-the-old-one (namestring original)
1799 (unless (sb!unix:unix-access namestring sb!unix:w_ok)
1800 (error "~@<The file ~2I~_~S ~I~_is not writable.~:>" namestring))
1801 (multiple-value-bind (okay err) (sb!unix:unix-rename namestring original)
1804 (error 'simple-file-error
1805 :pathname namestring
1807 "~@<couldn't rename ~2I~_~S ~I~_to ~2I~_~S: ~4I~_~A~:>"
1808 :format-arguments (list namestring original (strerror err))))))
1810 (defun open (filename
1813 (element-type 'base-char)
1814 (if-exists nil if-exists-given)
1815 (if-does-not-exist nil if-does-not-exist-given)
1816 (external-format :default)
1817 &aux ; Squelch assignment warning.
1818 (direction direction)
1819 (if-does-not-exist if-does-not-exist)
1820 (if-exists if-exists))
1822 "Return a stream which reads from or writes to FILENAME.
1824 :DIRECTION - one of :INPUT, :OUTPUT, :IO, or :PROBE
1825 :ELEMENT-TYPE - the type of object to read or write, default BASE-CHAR
1826 :IF-EXISTS - one of :ERROR, :NEW-VERSION, :RENAME, :RENAME-AND-DELETE,
1827 :OVERWRITE, :APPEND, :SUPERSEDE or NIL
1828 :IF-DOES-NOT-EXIST - one of :ERROR, :CREATE or NIL
1829 See the manual for details."
1831 ;; Calculate useful stuff.
1832 (multiple-value-bind (input output mask)
1834 (:input (values t nil sb!unix:o_rdonly))
1835 (:output (values nil t sb!unix:o_wronly))
1836 (:io (values t t sb!unix:o_rdwr))
1837 (:probe (values t nil sb!unix:o_rdonly)))
1838 (declare (type index mask))
1839 (let* ((pathname (merge-pathnames filename))
1841 (cond ((unix-namestring pathname input))
1842 ((and input (eq if-does-not-exist :create))
1843 (unix-namestring pathname nil))
1844 ((and (eq direction :io) (not if-does-not-exist-given))
1845 (unix-namestring pathname nil)))))
1846 ;; Process if-exists argument if we are doing any output.
1848 (unless if-exists-given
1850 (if (eq (pathname-version pathname) :newest)
1853 (ensure-one-of if-exists
1854 '(:error :new-version :rename
1855 :rename-and-delete :overwrite
1856 :append :supersede nil)
1859 ((:new-version :error nil)
1860 (setf mask (logior mask sb!unix:o_excl)))
1861 ((:rename :rename-and-delete)
1862 (setf mask (logior mask sb!unix:o_creat)))
1864 (setf mask (logior mask sb!unix:o_trunc)))
1866 (setf mask (logior mask sb!unix:o_append)))))
1868 (setf if-exists :ignore-this-arg)))
1870 (unless if-does-not-exist-given
1871 (setf if-does-not-exist
1872 (cond ((eq direction :input) :error)
1874 (member if-exists '(:overwrite :append)))
1876 ((eq direction :probe)
1880 (ensure-one-of if-does-not-exist
1881 '(:error :create nil)
1883 (if (eq if-does-not-exist :create)
1884 (setf mask (logior mask sb!unix:o_creat)))
1886 (let ((original (case if-exists
1887 ((:rename :rename-and-delete)
1888 (pick-backup-name namestring))
1890 ;; KLUDGE: Provent CLOSE from deleting
1891 ;; appending streams when called with :ABORT T
1893 (delete-original (eq if-exists :rename-and-delete))
1895 (when (and original (not (eq original namestring)))
1896 ;; We are doing a :RENAME or :RENAME-AND-DELETE. Determine
1897 ;; whether the file already exists, make sure the original
1898 ;; file is not a directory, and keep the mode.
1901 (multiple-value-bind (okay err/dev inode orig-mode)
1902 (sb!unix:unix-stat namestring)
1903 (declare (ignore inode)
1904 (type (or index null) orig-mode))
1907 (when (and output (= (logand orig-mode #o170000)
1909 (error 'simple-file-error
1910 :pathname namestring
1912 "can't open ~S for output: is a directory"
1913 :format-arguments (list namestring)))
1914 (setf mode (logand orig-mode #o777))
1916 ((eql err/dev sb!unix:enoent)
1919 (simple-file-perror "can't find ~S"
1923 (rename-the-old-one namestring original))
1925 (setf delete-original nil)
1926 ;; In order to use :SUPERSEDE instead, we have to make
1927 ;; sure SB!UNIX:O_CREAT corresponds to
1928 ;; IF-DOES-NOT-EXIST. SB!UNIX:O_CREAT was set before
1929 ;; because of IF-EXISTS being :RENAME.
1930 (unless (eq if-does-not-exist :create)
1932 (logior (logandc2 mask sb!unix:o_creat)
1934 (setf if-exists :supersede))))
1936 ;; Now we can try the actual Unix open(2).
1937 (multiple-value-bind (fd errno)
1939 (sb!unix:unix-open namestring mask mode)
1940 (values nil sb!unix:enoent))
1941 (labels ((open-error (format-control &rest format-arguments)
1942 (error 'simple-file-error
1944 :format-control format-control
1945 :format-arguments format-arguments))
1946 (vanilla-open-error ()
1947 (simple-file-perror "error opening ~S" pathname errno)))
1950 ((:input :output :io)
1954 :element-type element-type
1955 :external-format external-format
1958 :delete-original delete-original
1964 (%make-fd-stream :name namestring
1967 :element-type element-type)))
1970 ((eql errno sb!unix:enoent)
1971 (case if-does-not-exist
1972 (:error (vanilla-open-error))
1974 (open-error "~@<The path ~2I~_~S ~I~_does not exist.~:>"
1977 ((and (eql errno sb!unix:eexist) (null if-exists))
1980 (vanilla-open-error)))))))))
1984 ;;; the stream connected to the controlling terminal, or NIL if there is none
1987 ;;; the stream connected to the standard input (file descriptor 0)
1990 ;;; the stream connected to the standard output (file descriptor 1)
1993 ;;; the stream connected to the standard error output (file descriptor 2)
1996 ;;; This is called when the cold load is first started up, and may also
1997 ;;; be called in an attempt to recover from nested errors.
1998 (defun stream-cold-init-or-reset ()
2000 (setf *terminal-io* (make-synonym-stream '*tty*))
2001 (setf *standard-output* (make-synonym-stream '*stdout*))
2002 (setf *standard-input* (make-synonym-stream '*stdin*))
2003 (setf *error-output* (make-synonym-stream '*stderr*))
2004 (setf *query-io* (make-synonym-stream '*terminal-io*))
2005 (setf *debug-io* *query-io*)
2006 (setf *trace-output* *standard-output*)
2009 ;;; This is called whenever a saved core is restarted.
2010 (defun stream-reinit ()
2011 (setf *available-buffers* nil)
2013 (make-fd-stream 0 :name "standard input" :input t :buffering :line))
2015 (make-fd-stream 1 :name "standard output" :output t :buffering :line))
2017 (make-fd-stream 2 :name "standard error" :output t :buffering :line))
2018 (let* ((ttyname #.(coerce "/dev/tty" 'simple-base-string))
2019 (tty (sb!unix:unix-open ttyname sb!unix:o_rdwr #o666)))
2023 :name "the terminal"
2028 (setf *tty* (make-two-way-stream *stdin* *stdout*))))
2033 ;;; the Unix way to beep
2034 (defun beep (stream)
2035 (write-char (code-char bell-char-code) stream)
2036 (finish-output stream))
2038 ;;; This is kind of like FILE-POSITION, but is an internal hack used
2039 ;;; by the filesys stuff to get and set the file name.
2041 ;;; FIXME: misleading name, screwy interface
2042 (defun file-name (stream &optional new-name)
2043 (when (typep stream 'fd-stream)
2045 (setf (fd-stream-pathname stream) new-name)
2046 (setf (fd-stream-file stream)
2047 (unix-namestring new-name nil))
2050 (fd-stream-pathname stream)))))
2052 ;;;; international character support (which is trivial for our simple
2053 ;;;; character sets)
2055 ;;;; (Those who do Lisp only in English might not remember that ANSI
2056 ;;;; requires these functions to be exported from package
2059 (defun file-string-length (stream object)
2060 (declare (type (or string character) object) (type fd-stream stream))
2062 "Return the delta in STREAM's FILE-POSITION that would be caused by writing
2063 OBJECT to STREAM. Non-trivial only in implementations that support
2064 international character sets."
2065 (declare (ignore stream))
2068 (string (length object))))
2070 (defun stream-external-format (stream)
2071 (declare (type fd-stream stream))
2073 "Return the actual external format for fd-streams, otherwise :DEFAULT."
2074 (if (typep stream 'fd-stream)
2075 (fd-stream-external-format stream)