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 FILE-STREAM structure
36 (defstruct (file-stream
37 (:constructor %make-fd-stream)
38 ;; KLUDGE: in an ideal world, maybe we'd rewrite
39 ;; everything to use FILE-STREAM rather than simply
40 ;; providing this hack for compatibility with the old
41 ;; code. However, CVS doesn't deal terribly well with
42 ;; file renaming, so for now we use this
43 ;; backward-compatibility feature.
44 (:conc-name fd-stream-)
45 (:predicate fd-stream-p)
47 (misc #'fd-stream-misc-routine))
50 ;; the name of this stream
52 ;; the file this stream is for
54 ;; the backup file namestring for the old file, for :IF-EXISTS
55 ;; :RENAME or :RENAME-AND-DELETE.
56 (original nil :type (or simple-string null))
57 (delete-original nil) ; for :if-exists :rename-and-delete
58 ;;; the number of bytes per element
59 (element-size 1 :type index)
60 ;; the type of element being transfered
61 (element-type 'base-char)
62 ;; the Unix file descriptor
64 ;; controls when the output buffer is flushed
65 (buffering :full :type (member :full :line :none))
66 ;; character position (if known)
67 (char-pos nil :type (or index null))
68 ;; T if input is waiting on FD. :EOF if we hit EOF.
69 (listen nil :type (member nil t :eof))
73 (ibuf-sap nil :type (or system-area-pointer null))
74 (ibuf-length nil :type (or index null))
75 (ibuf-head 0 :type index)
76 (ibuf-tail 0 :type index)
79 (obuf-sap nil :type (or system-area-pointer null))
80 (obuf-length nil :type (or index null))
81 (obuf-tail 0 :type index)
83 ;; output flushed, but not written due to non-blocking io?
86 ;; timeout specified for this stream, or NIL if none
87 (timeout nil :type (or index null))
88 ;; pathname of the file this stream is opened to (returned by PATHNAME)
89 (pathname nil :type (or pathname null))
90 (external-format :default)
91 (output-bytes #'ill-out :type function))
92 (def!method print-object ((fd-stream file-stream) stream)
93 (declare (type stream stream))
94 (print-unreadable-object (fd-stream stream :type t :identity t)
95 (format stream "for ~S" (fd-stream-name fd-stream))))
97 ;;;; output routines and related noise
99 (defvar *output-routines* ()
101 "List of all available output routines. Each element is a list of the
102 element-type output, the kind of buffering, the function name, and the number
103 of bytes per element.")
105 ;;; common idioms for reporting low-level stream and file problems
106 (defun simple-stream-perror (note-format stream errno)
107 (error 'simple-stream-error
109 :format-control "~@<~?: ~2I~_~A~:>"
110 :format-arguments (list note-format (list stream) (strerror errno))))
111 (defun simple-file-perror (note-format pathname errno)
112 (error 'simple-file-error
114 :format-control "~@<~?: ~2I~_~A~:>"
116 (list note-format (list pathname) (strerror errno))))
118 ;;; This is called by the server when we can write to the given file
119 ;;; descriptor. Attempt to write the data again. If it worked, remove
120 ;;; the data from the OUTPUT-LATER list. If it didn't work, something
122 (defun frob-output-later (stream)
123 (let* ((stuff (pop (fd-stream-output-later stream)))
127 (reuse-sap (cadddr stuff))
128 (length (- end start)))
129 (declare (type index start end length))
130 (multiple-value-bind (count errno)
131 (sb!unix:unix-write (fd-stream-fd stream)
136 (if (= errno sb!unix:ewouldblock)
137 (error "Write would have blocked, but SERVER told us to go.")
138 (simple-stream-perror "couldn't write to ~S" stream errno)))
139 ((eql count length) ; Hot damn, it worked.
141 (push base *available-buffers*)))
142 ((not (null count)) ; sorta worked..
144 (the index (+ start count))
146 (fd-stream-output-later stream))))))
147 (unless (fd-stream-output-later stream)
148 (sb!sys:remove-fd-handler (fd-stream-handler stream))
149 (setf (fd-stream-handler stream) nil)))
151 ;;; Arange to output the string when we can write on the file descriptor.
152 (defun output-later (stream base start end reuse-sap)
153 (cond ((null (fd-stream-output-later stream))
154 (setf (fd-stream-output-later stream)
155 (list (list base start end reuse-sap)))
156 (setf (fd-stream-handler stream)
157 (sb!sys:add-fd-handler (fd-stream-fd stream)
160 (declare (ignore fd))
161 (frob-output-later stream)))))
163 (nconc (fd-stream-output-later stream)
164 (list (list base start end reuse-sap)))))
166 (let ((new-buffer (next-available-buffer)))
167 (setf (fd-stream-obuf-sap stream) new-buffer)
168 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
170 ;;; Output the given noise. Check to see whether there are any pending
171 ;;; writes. If so, just queue this one. Otherwise, try to write it. If
172 ;;; this would block, queue it.
173 (defun frob-output (stream base start end reuse-sap)
174 (declare (type file-stream stream)
175 (type (or system-area-pointer (simple-array * (*))) base)
176 (type index start end))
177 (if (not (null (fd-stream-output-later stream))) ; something buffered.
179 (output-later stream base start end reuse-sap)
180 ;; ### check to see whether any of this noise can be output
182 (let ((length (- end start)))
183 (multiple-value-bind (count errno)
184 (sb!unix:unix-write (fd-stream-fd stream) base start length)
186 (if (= errno sb!unix:ewouldblock)
187 (output-later stream base start end reuse-sap)
188 (simple-stream-perror "couldn't write to ~S"
191 ((not (eql count length))
192 (output-later stream base (the index (+ start count))
195 ;;; Flush any data in the output buffer.
196 (defun flush-output-buffer (stream)
197 (let ((length (fd-stream-obuf-tail stream)))
199 (frob-output stream (fd-stream-obuf-sap stream) 0 length t)
200 (setf (fd-stream-obuf-tail stream) 0))))
202 (defmacro output-wrapper/variable-width ((stream size buffering)
204 (let ((stream-var (gensym)))
205 `(let ((,stream-var ,stream)
207 ,(unless (eq (car buffering) :none)
208 `(when (< (fd-stream-obuf-length ,stream-var)
209 (+ (fd-stream-obuf-tail ,stream-var)
211 (flush-output-buffer ,stream-var)))
212 ,(unless (eq (car buffering) :none)
213 `(when (> (fd-stream-ibuf-tail ,stream-var)
214 (fd-stream-ibuf-head ,stream-var))
215 (file-position ,stream-var (file-position ,stream-var))))
218 (incf (fd-stream-obuf-tail ,stream-var) size)
219 ,(ecase (car buffering)
221 `(flush-output-buffer ,stream-var))
223 `(when (eq (char-code byte) (char-code #\Newline))
224 (flush-output-buffer ,stream-var)))
228 (defmacro output-wrapper ((stream size buffering) &body body)
229 (let ((stream-var (gensym)))
230 `(let ((,stream-var ,stream))
231 ,(unless (eq (car buffering) :none)
232 `(when (< (fd-stream-obuf-length ,stream-var)
233 (+ (fd-stream-obuf-tail ,stream-var)
235 (flush-output-buffer ,stream-var)))
236 ,(unless (eq (car buffering) :none)
237 `(when (> (fd-stream-ibuf-tail ,stream-var)
238 (fd-stream-ibuf-head ,stream-var))
239 (file-position ,stream-var (file-position ,stream-var))))
242 (incf (fd-stream-obuf-tail ,stream-var) ,size)
243 ,(ecase (car buffering)
245 `(flush-output-buffer ,stream-var))
247 `(when (eq (char-code byte) (char-code #\Newline))
248 (flush-output-buffer ,stream-var)))
252 (defmacro def-output-routines/variable-width ((name-fmt size external-format
255 (declare (optimize (speed 1)))
260 (intern (let ((*print-case* :upcase))
261 (format nil name-fmt (car buffering))))))
263 (defun ,function (stream byte)
264 (output-wrapper/variable-width (stream ,size ,buffering)
266 (setf *output-routines*
267 (nconc *output-routines*
275 (cdr buffering)))))))
278 ;;; Define output routines that output numbers SIZE bytes long for the
279 ;;; given bufferings. Use BODY to do the actual output.
280 (defmacro def-output-routines ((name-fmt size &rest bufferings) &body body)
281 (declare (optimize (speed 1)))
286 (intern (let ((*print-case* :upcase))
287 (format nil name-fmt (car buffering))))))
289 (defun ,function (stream byte)
290 (output-wrapper (stream ,size ,buffering)
292 (setf *output-routines*
293 (nconc *output-routines*
301 (cdr buffering)))))))
304 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
309 (if (char= byte #\Newline)
310 (setf (fd-stream-char-pos stream) 0)
311 (incf (fd-stream-char-pos stream)))
312 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
315 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
317 (:none (unsigned-byte 8))
318 (:full (unsigned-byte 8)))
319 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
322 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
324 (:none (signed-byte 8))
325 (:full (signed-byte 8)))
326 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
327 (fd-stream-obuf-tail stream))
330 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
332 (:none (unsigned-byte 16))
333 (:full (unsigned-byte 16)))
334 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
337 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
339 (:none (signed-byte 16))
340 (:full (signed-byte 16)))
341 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
342 (fd-stream-obuf-tail stream))
345 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
347 (:none (unsigned-byte 32))
348 (:full (unsigned-byte 32)))
349 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
352 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
354 (:none (signed-byte 32))
355 (:full (signed-byte 32)))
356 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
357 (fd-stream-obuf-tail stream))
360 ;;; Do the actual output. If there is space to buffer the string,
361 ;;; buffer it. If the string would normally fit in the buffer, but
362 ;;; doesn't because of other stuff in the buffer, flush the old noise
363 ;;; out of the buffer and put the string in it. Otherwise we have a
364 ;;; very long string, so just send it directly (after flushing the
365 ;;; buffer, of course).
366 (defun output-raw-bytes (fd-stream thing &optional start end)
368 "Output THING to FD-STREAM. THING can be any kind of vector or a SAP. If
369 THING is a SAP, END must be supplied (as length won't work)."
370 (let ((start (or start 0))
371 (end (or end (length (the (simple-array * (*)) thing)))))
372 (declare (type index start end))
373 (when (> (fd-stream-ibuf-tail fd-stream)
374 (fd-stream-ibuf-head fd-stream))
375 (file-position fd-stream (file-position fd-stream)))
376 (let* ((len (fd-stream-obuf-length fd-stream))
377 (tail (fd-stream-obuf-tail fd-stream))
379 (bytes (- end start))
380 (newtail (+ tail bytes)))
381 (cond ((minusp bytes) ; error case
382 (error ":END before :START!"))
383 ((zerop bytes)) ; easy case
385 (if (system-area-pointer-p thing)
386 (system-area-copy thing
387 (* start sb!vm:n-byte-bits)
388 (fd-stream-obuf-sap fd-stream)
389 (* tail sb!vm:n-byte-bits)
390 (* bytes sb!vm:n-byte-bits))
391 ;; FIXME: There should be some type checking somewhere to
392 ;; verify that THING here is a vector, not just <not a SAP>.
393 (copy-to-system-area thing
394 (+ (* start sb!vm:n-byte-bits)
395 (* sb!vm:vector-data-offset
397 (fd-stream-obuf-sap fd-stream)
398 (* tail sb!vm:n-byte-bits)
399 (* bytes sb!vm:n-byte-bits)))
400 (setf (fd-stream-obuf-tail fd-stream) newtail))
402 (flush-output-buffer fd-stream)
403 (if (system-area-pointer-p thing)
404 (system-area-copy thing
405 (* start sb!vm:n-byte-bits)
406 (fd-stream-obuf-sap fd-stream)
408 (* bytes sb!vm:n-byte-bits))
409 ;; FIXME: There should be some type checking somewhere to
410 ;; verify that THING here is a vector, not just <not a SAP>.
411 (copy-to-system-area thing
412 (+ (* start sb!vm:n-byte-bits)
413 (* sb!vm:vector-data-offset
415 (fd-stream-obuf-sap fd-stream)
417 (* bytes sb!vm:n-byte-bits)))
418 (setf (fd-stream-obuf-tail fd-stream) bytes))
420 (flush-output-buffer fd-stream)
421 (frob-output fd-stream thing start end nil))))))
423 ;;; the routine to use to output a string. If the stream is
424 ;;; unbuffered, slam the string down the file descriptor, otherwise
425 ;;; use OUTPUT-RAW-BYTES to buffer the string. Update charpos by
426 ;;; checking to see where the last newline was.
428 ;;; Note: some bozos (the FASL dumper) call write-string with things
429 ;;; other than strings. Therefore, we must make sure we have a string
430 ;;; before calling POSITION on it.
431 ;;; KLUDGE: It would be better to fix the bozos instead of trying to
432 ;;; cover for them here. -- WHN 20000203
433 (defun fd-sout (stream thing start end)
434 (let ((start (or start 0))
435 (end (or end (length (the vector thing)))))
436 (declare (fixnum start end))
438 (let ((last-newline (and (find #\newline (the simple-string thing)
439 :start start :end end)
440 ;; FIXME why do we need both calls?
441 ;; Is find faster forwards than
442 ;; position is backwards?
443 (position #\newline (the simple-string thing)
447 (if (and (typep thing 'base-string)
448 (eq (fd-stream-external-format stream) :latin-1))
449 (ecase (fd-stream-buffering stream)
451 (output-raw-bytes stream thing start end))
453 (output-raw-bytes stream thing start end)
455 (flush-output-buffer stream)))
457 (frob-output stream thing start end nil)))
458 (ecase (fd-stream-buffering stream)
459 (:full (funcall (fd-stream-output-bytes stream)
460 stream thing nil start end))
461 (:line (funcall (fd-stream-output-bytes stream)
462 stream thing last-newline start end))
463 (:none (funcall (fd-stream-output-bytes stream)
464 stream thing t start end))))
466 (setf (fd-stream-char-pos stream)
467 (- end last-newline 1))
468 (incf (fd-stream-char-pos stream)
470 (ecase (fd-stream-buffering stream)
472 (output-raw-bytes stream thing start end))
474 (frob-output stream thing start end nil))))))
476 (defvar *external-formats* ()
478 "List of all available external formats. Each element is a list of the
479 element-type, string input function name, character input function name,
480 and string output function name.")
482 ;;; Find an output routine to use given the type and buffering. Return
483 ;;; as multiple values the routine, the real type transfered, and the
484 ;;; number of bytes per element.
485 (defun pick-output-routine (type buffering &optional external-format)
486 (when (subtypep type 'character)
487 (dolist (entry *external-formats*)
488 (when (member external-format (first entry))
489 (return-from pick-output-routine
490 (values (symbol-function (nth (ecase buffering
497 (symbol-function (fourth entry))
498 (first (first entry)))))))
499 (dolist (entry *output-routines*)
500 (when (and (subtypep type (first entry))
501 (eq buffering (second entry))
502 (or (not (fifth entry))
503 (eq external-format (fifth entry))))
504 (return-from pick-output-routine
505 (values (symbol-function (third entry))
508 ;; KLUDGE: dealing with the buffering here leads to excessive code
511 ;; KLUDGE: also see comments in PICK-INPUT-ROUTINE
512 (loop for i from 40 by 8 to 1024 ; ARB (KLUDGE)
513 if (subtypep type `(unsigned-byte ,i))
514 do (return-from pick-output-routine
518 (lambda (stream byte)
519 (output-wrapper (stream (/ i 8) (:none))
520 (loop for j from 0 below (/ i 8)
522 (fd-stream-obuf-sap stream)
523 (+ j (fd-stream-obuf-tail stream)))
524 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
526 (lambda (stream byte)
527 (output-wrapper (stream (/ i 8) (:full))
528 (loop for j from 0 below (/ i 8)
530 (fd-stream-obuf-sap stream)
531 (+ j (fd-stream-obuf-tail stream)))
532 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
535 (loop for i from 40 by 8 to 1024 ; ARB (KLUDGE)
536 if (subtypep type `(signed-byte ,i))
537 do (return-from pick-output-routine
541 (lambda (stream byte)
542 (output-wrapper (stream (/ i 8) (:none))
543 (loop for j from 0 below (/ i 8)
545 (fd-stream-obuf-sap stream)
546 (+ j (fd-stream-obuf-tail stream)))
547 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
549 (lambda (stream byte)
550 (output-wrapper (stream (/ i 8) (:full))
551 (loop for j from 0 below (/ i 8)
553 (fd-stream-obuf-sap stream)
554 (+ j (fd-stream-obuf-tail stream)))
555 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
559 ;;;; input routines and related noise
561 ;;; a list of all available input routines. Each element is a list of
562 ;;; the element-type input, the function name, and the number of bytes
564 (defvar *input-routines* ())
566 ;;; Fill the input buffer, and return the first character. Throw to
567 ;;; EOF-INPUT-CATCHER if the eof was reached. Drop into SYSTEM:SERVER
569 (defun frob-input (stream)
570 (let ((fd (fd-stream-fd stream))
571 (ibuf-sap (fd-stream-ibuf-sap stream))
572 (buflen (fd-stream-ibuf-length stream))
573 (head (fd-stream-ibuf-head stream))
574 (tail (fd-stream-ibuf-tail stream)))
575 (declare (type index head tail))
577 (cond ((eql head tail)
580 (setf (fd-stream-ibuf-head stream) 0)
581 (setf (fd-stream-ibuf-tail stream) 0))
584 (system-area-copy ibuf-sap (* head sb!vm:n-byte-bits)
585 ibuf-sap 0 (* tail sb!vm:n-byte-bits))
587 (setf (fd-stream-ibuf-head stream) 0)
588 (setf (fd-stream-ibuf-tail stream) tail))))
589 (setf (fd-stream-listen stream) nil)
590 (multiple-value-bind (count errno)
591 ;; FIXME: Judging from compiler warnings, this WITH-ALIEN form expands
592 ;; into something which uses the not-yet-defined type
593 ;; (SB!ALIEN-INTERNALS:ALIEN (* (SB!ALIEN:STRUCT SB!UNIX:FD-SET))).
594 ;; This is probably inefficient and unsafe and generally bad, so
595 ;; try to find some way to make that type known before
597 (sb!alien:with-alien ((read-fds (sb!alien:struct sb!unix:fd-set)))
598 (sb!unix:fd-zero read-fds)
599 (sb!unix:fd-set fd read-fds)
600 (sb!unix:unix-fast-select (1+ fd)
601 (sb!alien:addr read-fds)
609 (unless (sb!sys:wait-until-fd-usable
610 fd :input (fd-stream-timeout stream))
611 (error 'io-timeout :stream stream :direction :read)))
613 (simple-stream-perror "couldn't check whether ~S is readable"
616 (multiple-value-bind (count errno)
617 (sb!unix:unix-read fd
618 (sb!sys:int-sap (+ (sb!sys:sap-int ibuf-sap) tail))
621 (if (eql errno sb!unix:ewouldblock)
623 (unless (sb!sys:wait-until-fd-usable
624 fd :input (fd-stream-timeout stream))
625 (error 'io-timeout :stream stream :direction :read))
627 (simple-stream-perror "couldn't read from ~S" stream errno)))
629 (setf (fd-stream-listen stream) :eof)
630 (/show0 "THROWing EOF-INPUT-CATCHER")
631 (throw 'eof-input-catcher nil))
633 (incf (fd-stream-ibuf-tail stream) count))))))
635 ;;; Make sure there are at least BYTES number of bytes in the input
636 ;;; buffer. Keep calling FROB-INPUT until that condition is met.
637 (defmacro input-at-least (stream bytes)
638 (let ((stream-var (gensym))
639 (bytes-var (gensym)))
640 `(let ((,stream-var ,stream)
643 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
644 (fd-stream-ibuf-head ,stream-var))
647 (frob-input ,stream-var)))))
649 (defmacro input-wrapper/variable-width ((stream bytes eof-error eof-value)
651 (let ((stream-var (gensym))
652 (element-var (gensym)))
653 `(let ((,stream-var ,stream)
655 (if (fd-stream-unread ,stream-var)
657 (fd-stream-unread ,stream-var)
658 (setf (fd-stream-unread ,stream-var) nil)
659 (setf (fd-stream-listen ,stream-var) nil))
661 (catch 'eof-input-catcher
662 (input-at-least ,stream-var 1)
663 (let* ((byte (sap-ref-8 (fd-stream-ibuf-sap ,stream-var)
664 (fd-stream-ibuf-head ,stream-var))))
666 (input-at-least ,stream-var size)
667 (locally ,@read-forms)))))
669 (incf (fd-stream-ibuf-head ,stream-var) size)
672 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
674 ;;; a macro to wrap around all input routines to handle EOF-ERROR noise
675 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
676 (let ((stream-var (gensym))
677 (element-var (gensym)))
678 `(let ((,stream-var ,stream))
679 (if (fd-stream-unread ,stream-var)
681 (fd-stream-unread ,stream-var)
682 (setf (fd-stream-unread ,stream-var) nil)
683 (setf (fd-stream-listen ,stream-var) nil))
685 (catch 'eof-input-catcher
686 (input-at-least ,stream-var ,bytes)
687 (locally ,@read-forms))))
689 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
692 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
694 (defmacro def-input-routine/variable-width (name
695 (type external-format size sap head)
698 (defun ,name (stream eof-error eof-value)
699 (input-wrapper/variable-width (stream ,size eof-error eof-value)
700 (let ((,sap (fd-stream-ibuf-sap stream))
701 (,head (fd-stream-ibuf-head stream)))
703 (setf *input-routines*
704 (nconc *input-routines*
705 (list (list ',type ',name 1 ',external-format))))))
707 (defmacro def-input-routine (name
711 (defun ,name (stream eof-error eof-value)
712 (input-wrapper (stream ,size eof-error eof-value)
713 (let ((,sap (fd-stream-ibuf-sap stream))
714 (,head (fd-stream-ibuf-head stream)))
716 (setf *input-routines*
717 (nconc *input-routines*
718 (list (list ',type ',name ',size nil))))))
720 ;;; STREAM-IN routine for reading a string char
721 (def-input-routine input-character
722 (character 1 sap head)
723 (code-char (sap-ref-8 sap head)))
725 ;;; STREAM-IN routine for reading an unsigned 8 bit number
726 (def-input-routine input-unsigned-8bit-byte
727 ((unsigned-byte 8) 1 sap head)
728 (sap-ref-8 sap head))
730 ;;; STREAM-IN routine for reading a signed 8 bit number
731 (def-input-routine input-signed-8bit-number
732 ((signed-byte 8) 1 sap head)
733 (signed-sap-ref-8 sap head))
735 ;;; STREAM-IN routine for reading an unsigned 16 bit number
736 (def-input-routine input-unsigned-16bit-byte
737 ((unsigned-byte 16) 2 sap head)
738 (sap-ref-16 sap head))
740 ;;; STREAM-IN routine for reading a signed 16 bit number
741 (def-input-routine input-signed-16bit-byte
742 ((signed-byte 16) 2 sap head)
743 (signed-sap-ref-16 sap head))
745 ;;; STREAM-IN routine for reading a unsigned 32 bit number
746 (def-input-routine input-unsigned-32bit-byte
747 ((unsigned-byte 32) 4 sap head)
748 (sap-ref-32 sap head))
750 ;;; STREAM-IN routine for reading a signed 32 bit number
751 (def-input-routine input-signed-32bit-byte
752 ((signed-byte 32) 4 sap head)
753 (signed-sap-ref-32 sap head))
757 ;;; Find an input routine to use given the type. Return as multiple
758 ;;; values the routine, the real type transfered, and the number of
759 ;;; bytes per element (and for character types string input routine).
760 (defun pick-input-routine (type &optional external-format)
761 (when (subtypep type 'character)
762 (dolist (entry *external-formats*)
763 (when (member external-format (first entry))
764 (return-from pick-input-routine
765 (values (symbol-function (third entry))
768 (symbol-function (second entry))
769 (first (first entry)))))))
770 (dolist (entry *input-routines*)
771 (when (and (subtypep type (first entry))
772 (or (not (fourth entry))
773 (eq external-format (fourth entry))))
774 (return-from pick-input-routine
775 (values (symbol-function (second entry))
778 ;; FIXME: let's do it the hard way, then (but ignore things like
779 ;; endianness, efficiency, and the necessary coupling between these
780 ;; and the output routines). -- CSR, 2004-02-09
781 (loop for i from 40 by 8 to 1024 ; ARB (well, KLUDGE really)
782 if (subtypep type `(unsigned-byte ,i))
783 do (return-from pick-input-routine
785 (lambda (stream eof-error eof-value)
786 (input-wrapper (stream (/ i 8) eof-error eof-value)
787 (let ((sap (fd-stream-ibuf-sap stream))
788 (head (fd-stream-ibuf-head stream)))
789 (loop for j from 0 below (/ i 8)
793 (sap-ref-8 sap (+ head j))))
794 finally (return result)))))
797 (loop for i from 40 by 8 to 1024 ; ARB (well, KLUDGE really)
798 if (subtypep type `(signed-byte ,i))
799 do (return-from pick-input-routine
801 (lambda (stream eof-error eof-value)
802 (input-wrapper (stream (/ i 8) eof-error eof-value)
803 (let ((sap (fd-stream-ibuf-sap stream))
804 (head (fd-stream-ibuf-head stream)))
805 (loop for j from 0 below (/ i 8)
809 (sap-ref-8 sap (+ head j))))
810 finally (return (if (logbitp (1- i) result)
811 (dpb result (byte i 0) -1)
816 ;;; Return a string constructed from SAP, START, and END.
817 (defun string-from-sap (sap start end)
818 (declare (type index start end))
819 (let* ((length (- end start))
820 (string (make-string length)))
821 (copy-from-system-area sap (* start sb!vm:n-byte-bits)
822 string (* sb!vm:vector-data-offset
824 (* length sb!vm:n-byte-bits))
827 ;;; the N-BIN method for FD-STREAMs
829 ;;; Note that this blocks in UNIX-READ. It is generally used where
830 ;;; there is a definite amount of reading to be done, so blocking
831 ;;; isn't too problematical.
832 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p
833 &aux (total-copied 0))
834 (declare (type file-stream stream))
835 (declare (type index start requested total-copied))
836 (let ((unread (fd-stream-unread stream)))
838 ;; AVERs designed to fail when we have more complicated
839 ;; character representations.
840 (aver (typep unread 'base-char))
841 (aver (= (fd-stream-element-size stream) 1))
842 ;; KLUDGE: this is a slightly-unrolled-and-inlined version of
846 (setf (sap-ref-8 buffer start) (char-code unread)))
847 ((simple-unboxed-array (*))
848 (setf (aref buffer start) unread)))
849 (setf (fd-stream-unread stream) nil)
850 (setf (fd-stream-listen stream) nil)
851 (incf total-copied)))
854 (let* ((remaining-request (- requested total-copied))
855 (head (fd-stream-ibuf-head stream))
856 (tail (fd-stream-ibuf-tail stream))
857 (available (- tail head))
858 (n-this-copy (min remaining-request available))
859 (this-start (+ start total-copied))
860 (this-end (+ this-start n-this-copy))
861 (sap (fd-stream-ibuf-sap stream)))
862 (declare (type index remaining-request head tail available))
863 (declare (type index n-this-copy))
864 ;; Copy data from stream buffer into user's buffer.
865 (%byte-blt sap head buffer this-start this-end)
866 (incf (fd-stream-ibuf-head stream) n-this-copy)
867 (incf total-copied n-this-copy)
868 ;; Maybe we need to refill the stream buffer.
869 (cond (;; If there were enough data in the stream buffer, we're done.
870 (= total-copied requested)
871 (return total-copied))
872 (;; If EOF, we're done in another way.
873 (zerop (refill-fd-stream-buffer stream))
875 (error 'end-of-file :stream stream)
876 (return total-copied)))
877 ;; Otherwise we refilled the stream buffer, so fall
878 ;; through into another pass of the loop.
881 ;;; Try to refill the stream buffer. Return the number of bytes read.
882 ;;; (For EOF, the return value will be zero, otherwise positive.)
883 (defun refill-fd-stream-buffer (stream)
884 ;; We don't have any logic to preserve leftover bytes in the buffer,
885 ;; so we should only be called when the buffer is empty.
886 ;; FIXME: can have three bytes in buffer because of UTF-8
888 (sap (fd-stream-ibuf-sap stream)))
889 (do ((head (fd-stream-ibuf-head stream) (1+ head))
890 (tail (fd-stream-ibuf-tail stream)))
892 (setf (sap-ref-8 sap new-head) (sap-ref-8 sap head))
894 (multiple-value-bind (count err)
895 (sb!unix:unix-read (fd-stream-fd stream)
897 (- (fd-stream-ibuf-length stream) new-head))
898 (declare (type (or index null) count))
900 (simple-stream-perror "couldn't read from ~S" stream err))
901 (setf (fd-stream-listen stream) nil
902 (fd-stream-ibuf-head stream) new-head
903 (fd-stream-ibuf-tail stream) (+ count new-head))
906 (defmacro define-external-format (external-format size out-expr in-expr)
907 (let* ((name (first external-format))
908 (out-function (intern (let ((*print-case* :upcase))
909 (format nil "OUTPUT-BYTES/~A" name))))
910 (format (format nil "OUTPUT-CHAR-~A-~~A-BUFFERED" name))
911 (in-function (intern (let ((*print-case* :upcase))
912 (format nil "FD-STREAM-READ-N-CHARACTERS/~A"
914 (in-char-function (intern (let ((*print-case* :upcase))
915 (format nil "INPUT-CHAR/~A" name)))))
917 (defun ,out-function (fd-stream string flush-p start end)
918 (let ((start (or start 0))
919 (end (or end (length string))))
920 (declare (type index start end))
921 (when (> (fd-stream-ibuf-tail fd-stream)
922 (fd-stream-ibuf-head fd-stream))
923 (file-position fd-stream (file-position fd-stream)))
925 (error ":END before :START!"))
928 (setf (fd-stream-obuf-tail fd-stream)
929 (do* ((len (fd-stream-obuf-length fd-stream))
930 (sap (fd-stream-obuf-sap fd-stream))
931 (tail (fd-stream-obuf-tail fd-stream)))
932 ((or (= start end) (< (- len tail) 4)) tail)
933 (let* ((byte (aref string start))
934 (bits (char-code byte)))
939 (flush-output-buffer fd-stream)))
941 (flush-output-buffer fd-stream))))
942 (def-output-routines (,format
947 (if (char= byte #\Newline)
948 (setf (fd-stream-char-pos stream) 0)
949 (incf (fd-stream-char-pos stream)))
950 (let ((bits (char-code byte))
951 (sap (fd-stream-obuf-sap stream))
952 (tail (fd-stream-obuf-tail stream)))
954 (defun ,in-function (stream buffer start requested eof-error-p
955 &aux (total-copied 0))
956 (declare (type file-stream stream))
957 (declare (type index start requested total-copied))
958 (let ((unread (fd-stream-unread stream)))
960 (setf (aref buffer start) unread)
961 (setf (fd-stream-unread stream) nil)
962 (setf (fd-stream-listen stream) nil)
963 (incf total-copied)))
966 (let* ((head (fd-stream-ibuf-head stream))
967 (tail (fd-stream-ibuf-tail stream))
968 (sap (fd-stream-ibuf-sap stream)))
969 (declare (type index head tail))
970 ;; Copy data from stream buffer into user's buffer.
972 ((or (= tail head) (= requested total-copied)))
973 (let* ((byte (sap-ref-8 sap head)))
974 (when (> ,size (- tail head))
976 (setf (aref buffer (+ start total-copied)) ,in-expr)
979 (setf (fd-stream-ibuf-head stream) head)
980 ;; Maybe we need to refill the stream buffer.
981 (cond ( ;; If there were enough data in the stream buffer, we're done.
982 (= total-copied requested)
983 (return total-copied))
984 ( ;; If EOF, we're done in another way.
985 (zerop (refill-fd-stream-buffer stream))
987 (error 'end-of-file :stream stream)
988 (return total-copied)))
989 ;; Otherwise we refilled the stream buffer, so fall
990 ;; through into another pass of the loop.
992 (def-input-routine ,in-char-function (character ,size sap head)
993 (let ((byte (sap-ref-8 sap head)))
995 (setf *external-formats*
996 (cons '(,external-format ,in-function ,in-char-function ,out-function
997 ,@(mapcar #'(lambda (buffering)
998 (intern (let ((*print-case* :upcase))
999 (format nil format buffering))))
1000 '(:none :line :full)))
1001 *external-formats*)))))
1003 (defmacro define-external-format/variable-width (external-format out-size-expr
1004 out-expr in-size-expr in-expr)
1005 (let* ((name (first external-format))
1006 (out-function (intern (let ((*print-case* :upcase))
1007 (format nil "OUTPUT-BYTES/~A" name))))
1008 (format (format nil "OUTPUT-CHAR-~A-~~A-BUFFERED" name))
1009 (in-function (intern (let ((*print-case* :upcase))
1010 (format nil "FD-STREAM-READ-N-CHARACTERS/~A"
1012 (in-char-function (intern (let ((*print-case* :upcase))
1013 (format nil "INPUT-CHAR/~A" name)))))
1015 (defun ,out-function (fd-stream string flush-p start end)
1016 (let ((start (or start 0))
1017 (end (or end (length string))))
1018 (declare (type index start end))
1019 (when (> (fd-stream-ibuf-tail fd-stream)
1020 (fd-stream-ibuf-head fd-stream))
1021 (file-position fd-stream (file-position fd-stream)))
1023 (error ":END before :START!"))
1026 (setf (fd-stream-obuf-tail fd-stream)
1027 (do* ((len (fd-stream-obuf-length fd-stream))
1028 (sap (fd-stream-obuf-sap fd-stream))
1029 (tail (fd-stream-obuf-tail fd-stream)))
1030 ((or (= start end) (< (- len tail) 4)) tail)
1031 (let* ((byte (aref string start))
1032 (bits (char-code byte))
1033 (size ,out-size-expr))
1038 (flush-output-buffer fd-stream)))
1040 (flush-output-buffer fd-stream))))
1041 (def-output-routines/variable-width (,format
1047 (if (char= byte #\Newline)
1048 (setf (fd-stream-char-pos stream) 0)
1049 (incf (fd-stream-char-pos stream)))
1050 (let ((bits (char-code byte))
1051 (sap (fd-stream-obuf-sap stream))
1052 (tail (fd-stream-obuf-tail stream)))
1054 (defun ,in-function (stream buffer start requested eof-error-p
1055 &aux (total-copied 0))
1056 (declare (type file-stream stream))
1057 (declare (type index start requested total-copied))
1058 (let ((unread (fd-stream-unread stream)))
1060 (setf (aref buffer start) unread)
1061 (setf (fd-stream-unread stream) nil)
1062 (setf (fd-stream-listen stream) nil)
1063 (incf total-copied)))
1066 (let* ((head (fd-stream-ibuf-head stream))
1067 (tail (fd-stream-ibuf-tail stream))
1068 (sap (fd-stream-ibuf-sap stream)))
1069 (declare (type index head tail))
1070 ;; Copy data from stream buffer into user's buffer.
1072 ((or (= tail head) (= requested total-copied)))
1073 (let* ((byte (sap-ref-8 sap head))
1074 (size ,in-size-expr))
1075 (when (> size (- tail head))
1077 (setf (aref buffer (+ start total-copied)) ,in-expr)
1080 (setf (fd-stream-ibuf-head stream) head)
1081 ;; Maybe we need to refill the stream buffer.
1082 (cond ( ;; If there were enough data in the stream buffer, we're done.
1083 (= total-copied requested)
1084 (return total-copied))
1085 ( ;; If EOF, we're done in another way.
1086 (zerop (refill-fd-stream-buffer stream))
1088 (error 'end-of-file :stream stream)
1089 (return total-copied)))
1090 ;; Otherwise we refilled the stream buffer, so fall
1091 ;; through into another pass of the loop.
1093 (def-input-routine/variable-width ,in-char-function (character
1097 (let ((byte (sap-ref-8 sap head)))
1099 (setf *external-formats*
1100 (cons '(,external-format ,in-function ,in-char-function ,out-function
1101 ,@(mapcar #'(lambda (buffering)
1102 (intern (let ((*print-case* :upcase))
1103 (format nil format buffering))))
1104 '(:none :line :full)))
1105 *external-formats*)))))
1107 (define-external-format (:latin-1 :latin1 :iso-8859-1
1108 ;; FIXME: shouldn't ASCII-like things have an
1109 ;; extra typecheck for 7-bitness?
1110 :ascii :us-ascii :ansi_x3.4-1968)
1112 (setf (sap-ref-8 sap tail) bits)
1115 (define-external-format/variable-width (:utf-8 :utf8)
1116 (let ((bits (char-code byte)))
1117 (cond ((< bits #x80) 1)
1119 ((< bits #x10000) 3)
1122 (1 (setf (sap-ref-8 sap tail) bits))
1123 (2 (setf (sap-ref-8 sap tail) (logior #xc0 (ldb (byte 5 6) bits))
1124 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 0) bits))))
1125 (3 (setf (sap-ref-8 sap tail) (logior #xe0 (ldb (byte 4 12) bits))
1126 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 6) bits))
1127 (sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 0) bits))))
1128 (4 (setf (sap-ref-8 sap tail) (logior #xf0 (ldb (byte 3 18) bits))
1129 (sap-ref-8 sap (1+ tail)) (logior #x80 (ldb (byte 6 12) bits))
1130 (sap-ref-8 sap (+ 2 tail)) (logior #x80 (ldb (byte 6 6) bits))
1131 (sap-ref-8 sap (+ 3 tail)) (logior #x80 (ldb (byte 6 0) bits)))))
1132 (cond ((< byte #x80) 1)
1136 (code-char (ecase size
1138 (2 (dpb byte (byte 5 6) (sap-ref-8 sap (1+ head))))
1139 (3 (dpb byte (byte 4 12)
1140 (dpb (sap-ref-8 sap (1+ head)) (byte 6 6)
1141 (sap-ref-8 sap (+ 2 head)))))
1142 (4 (dpb byte (byte 3 18)
1143 (dpb (sap-ref-8 sap (1+ head)) (byte 6 12)
1144 (dpb (sap-ref-8 sap (+ 2 head)) (byte 6 6)
1145 (sap-ref-8 sap (+ 3 head)))))))))
1147 ;;;; utility functions (misc routines, etc)
1149 ;;; Fill in the various routine slots for the given type. INPUT-P and
1150 ;;; OUTPUT-P indicate what slots to fill. The buffering slot must be
1151 ;;; set prior to calling this routine.
1152 (defun set-fd-stream-routines (fd-stream type input-p output-p buffer-p)
1153 (let ((target-type (case type
1154 ((:default unsigned-byte)
1164 (character-stream-p (subtypep type 'character)))
1166 (when (fd-stream-obuf-sap fd-stream)
1167 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
1168 (setf (fd-stream-obuf-sap fd-stream) nil))
1169 (when (fd-stream-ibuf-sap fd-stream)
1170 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
1171 (setf (fd-stream-ibuf-sap fd-stream) nil))
1173 (when (and character-stream-p
1174 (eq (fd-stream-external-format fd-stream) :default))
1175 (setf (fd-stream-external-format fd-stream)
1176 (intern (or (alien-funcall
1177 (extern-alien "nl_langinfo"
1178 (function c-string int))
1182 (dolist (entry *external-formats*
1183 (setf (fd-stream-external-format fd-stream) :latin-1))
1184 (when (member (fd-stream-external-format fd-stream) (first entry))
1188 (multiple-value-bind (routine type size read-n-characters
1189 normalized-external-format)
1190 (pick-input-routine target-type
1191 (fd-stream-external-format fd-stream))
1192 (when normalized-external-format
1193 (setf (fd-stream-external-format fd-stream)
1194 normalized-external-format))
1196 (error "could not find any input routine for ~S" target-type))
1197 (setf (fd-stream-ibuf-sap fd-stream) (next-available-buffer))
1198 (setf (fd-stream-ibuf-length fd-stream) bytes-per-buffer)
1199 (setf (fd-stream-ibuf-tail fd-stream) 0)
1200 (if character-stream-p
1201 (setf (fd-stream-in fd-stream) routine
1202 (fd-stream-bin fd-stream) #'ill-bin)
1203 (setf (fd-stream-in fd-stream) #'ill-in
1204 (fd-stream-bin fd-stream) routine))
1206 (setf (fd-stream-n-bin fd-stream)
1207 (if character-stream-p
1209 #'fd-stream-read-n-bytes))
1211 ;; We only create this buffer for streams of type
1212 ;; (unsigned-byte 8). Because there's no buffer, the
1213 ;; other element-types will dispatch to the appropriate
1214 ;; input (output) routine in fast-read-byte.
1215 (or character-stream-p
1216 (equal target-type '(unsigned-byte 8)))
1217 (not output-p) ; temporary disable on :io streams
1219 (or (eq type 'unsigned-byte)
1220 (eq type :default)))
1221 (if character-stream-p
1222 (setf (ansi-stream-cin-buffer fd-stream)
1223 (make-array +ansi-stream-in-buffer-length+
1224 :element-type 'character))
1225 (setf (ansi-stream-in-buffer fd-stream)
1226 (make-array +ansi-stream-in-buffer-length+
1227 :element-type '(unsigned-byte 8))))))
1228 (setf input-size size)
1229 (setf input-type type)))
1232 (multiple-value-bind (routine type size output-bytes
1233 normalized-external-format)
1234 (pick-output-routine target-type
1235 (fd-stream-buffering fd-stream)
1236 (fd-stream-external-format fd-stream))
1237 (when normalized-external-format
1238 (setf (fd-stream-external-format fd-stream)
1239 normalized-external-format))
1241 (error "could not find any output routine for ~S buffered ~S"
1242 (fd-stream-buffering fd-stream)
1244 (setf (fd-stream-obuf-sap fd-stream) (next-available-buffer))
1245 (setf (fd-stream-obuf-length fd-stream) bytes-per-buffer)
1246 (setf (fd-stream-obuf-tail fd-stream) 0)
1247 (when character-stream-p
1248 (setf (fd-stream-output-bytes fd-stream) output-bytes))
1249 (if character-stream-p
1250 (setf (fd-stream-out fd-stream) routine
1251 (fd-stream-bout fd-stream) #'ill-bout)
1252 (setf (fd-stream-out fd-stream)
1253 (or (if (eql size 1)
1254 (pick-output-routine
1255 'base-char (fd-stream-buffering fd-stream)))
1257 (fd-stream-bout fd-stream) routine))
1258 (setf (fd-stream-sout fd-stream)
1259 (if (eql size 1) #'fd-sout #'ill-out))
1260 (setf (fd-stream-char-pos fd-stream) 0)
1261 (setf output-size size)
1262 (setf output-type type)))
1264 (when (and input-size output-size
1265 (not (eq input-size output-size)))
1266 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
1267 input-type input-size
1268 output-type output-size))
1269 (setf (fd-stream-element-size fd-stream)
1270 (or input-size output-size))
1272 (setf (fd-stream-element-type fd-stream)
1273 (cond ((equal input-type output-type)
1279 ((subtypep input-type output-type)
1281 ((subtypep output-type input-type)
1284 (error "Input type (~S) and output type (~S) are unrelated?"
1288 ;;; Handle miscellaneous operations on FD-STREAM.
1289 (defun fd-stream-misc-routine (fd-stream operation &optional arg1 arg2)
1290 (declare (ignore arg2))
1293 (or (not (eql (fd-stream-ibuf-head fd-stream)
1294 (fd-stream-ibuf-tail fd-stream)))
1295 (fd-stream-listen fd-stream)
1296 (setf (fd-stream-listen fd-stream)
1297 (eql (sb!alien:with-alien ((read-fds (sb!alien:struct
1299 (sb!unix:fd-zero read-fds)
1300 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
1301 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
1302 (sb!alien:addr read-fds)
1306 (setf (fd-stream-unread fd-stream) arg1)
1307 (setf (fd-stream-listen fd-stream) t))
1309 (cond (arg1 ; We got us an abort on our hands.
1310 (when (fd-stream-handler fd-stream)
1311 (sb!sys:remove-fd-handler (fd-stream-handler fd-stream))
1312 (setf (fd-stream-handler fd-stream) nil))
1313 ;; We can't do anything unless we know what file were
1314 ;; dealing with, and we don't want to do anything
1315 ;; strange unless we were writing to the file.
1316 (when (and (fd-stream-file fd-stream)
1317 (fd-stream-obuf-sap fd-stream))
1318 (if (fd-stream-original fd-stream)
1319 ;; If the original is EQ to file we are appending
1320 ;; and can just close the file without renaming.
1321 (unless (eq (fd-stream-original fd-stream)
1322 (fd-stream-file fd-stream))
1323 ;; We have a handle on the original, just revert.
1324 (multiple-value-bind (okay err)
1325 (sb!unix:unix-rename (fd-stream-original fd-stream)
1326 (fd-stream-file fd-stream))
1328 (simple-stream-perror
1329 "couldn't restore ~S to its original contents"
1332 ;; We can't restore the original, and aren't
1333 ;; appending, so nuke that puppy.
1335 ;; FIXME: This is currently the fate of superseded
1336 ;; files, and according to the CLOSE spec this is
1337 ;; wrong. However, there seems to be no clean way to
1338 ;; do that that doesn't involve either copying the
1339 ;; data (bad if the :abort resulted from a full
1340 ;; disk), or renaming the old file temporarily
1341 ;; (probably bad because stream opening becomes more
1343 (multiple-value-bind (okay err)
1344 (sb!unix:unix-unlink (fd-stream-file fd-stream))
1346 (error 'simple-file-error
1347 :pathname (fd-stream-file fd-stream)
1349 "~@<couldn't remove ~S: ~2I~_~A~:>"
1350 :format-arguments (list (fd-stream-file fd-stream)
1351 (strerror err))))))))
1353 (fd-stream-misc-routine fd-stream :finish-output)
1354 (when (and (fd-stream-original fd-stream)
1355 (fd-stream-delete-original fd-stream))
1356 (multiple-value-bind (okay err)
1357 (sb!unix:unix-unlink (fd-stream-original fd-stream))
1359 (error 'simple-file-error
1360 :pathname (fd-stream-original fd-stream)
1362 "~@<couldn't delete ~S during close of ~S: ~
1365 (list (fd-stream-original fd-stream)
1367 (strerror err))))))))
1368 (when (fboundp 'cancel-finalization)
1369 (cancel-finalization fd-stream))
1370 (sb!unix:unix-close (fd-stream-fd fd-stream))
1371 (when (fd-stream-obuf-sap fd-stream)
1372 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
1373 (setf (fd-stream-obuf-sap fd-stream) nil))
1374 (when (fd-stream-ibuf-sap fd-stream)
1375 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
1376 (setf (fd-stream-ibuf-sap fd-stream) nil))
1377 (sb!impl::set-closed-flame fd-stream))
1379 (setf (fd-stream-unread fd-stream) nil)
1380 (setf (fd-stream-ibuf-head fd-stream) 0)
1381 (setf (fd-stream-ibuf-tail fd-stream) 0)
1382 (catch 'eof-input-catcher
1384 (let ((count (sb!alien:with-alien ((read-fds (sb!alien:struct
1386 (sb!unix:fd-zero read-fds)
1387 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
1388 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
1389 (sb!alien:addr read-fds)
1394 (cond ((eql count 1)
1395 (frob-input fd-stream)
1396 (setf (fd-stream-ibuf-head fd-stream) 0)
1397 (setf (fd-stream-ibuf-tail fd-stream) 0))
1401 (flush-output-buffer fd-stream))
1403 (flush-output-buffer fd-stream)
1405 ((null (fd-stream-output-later fd-stream)))
1406 (sb!sys:serve-all-events)))
1408 (fd-stream-element-type fd-stream))
1410 (= 1 (the (member 0 1)
1411 (sb!unix:unix-isatty (fd-stream-fd fd-stream)))))
1415 (fd-stream-char-pos fd-stream))
1417 (unless (fd-stream-file fd-stream)
1418 ;; This is a TYPE-ERROR because ANSI's species FILE-LENGTH
1419 ;; "should signal an error of type TYPE-ERROR if stream is not
1420 ;; a stream associated with a file". Too bad there's no very
1421 ;; appropriate value for the EXPECTED-TYPE slot..
1422 (error 'simple-type-error
1424 :expected-type 'file-stream
1425 :format-control "~S is not a stream associated with a file."
1426 :format-arguments (list fd-stream)))
1427 (multiple-value-bind (okay dev ino mode nlink uid gid rdev size
1428 atime mtime ctime blksize blocks)
1429 (sb!unix:unix-fstat (fd-stream-fd fd-stream))
1430 (declare (ignore ino nlink uid gid rdev
1431 atime mtime ctime blksize blocks))
1433 (simple-stream-perror "failed Unix fstat(2) on ~S" fd-stream dev))
1436 (truncate size (fd-stream-element-size fd-stream)))))
1438 (fd-stream-file-position fd-stream arg1))))
1440 (defun fd-stream-file-position (stream &optional newpos)
1441 (declare (type file-stream stream)
1442 (type (or (alien sb!unix:off-t) (member nil :start :end)) newpos))
1444 (sb!sys:without-interrupts
1445 ;; First, find the position of the UNIX file descriptor in the file.
1446 (multiple-value-bind (posn errno)
1447 (sb!unix:unix-lseek (fd-stream-fd stream) 0 sb!unix:l_incr)
1448 (declare (type (or (alien sb!unix:off-t) null) posn))
1449 (cond ((integerp posn)
1450 ;; Adjust for buffered output: If there is any output
1451 ;; buffered, the *real* file position will be larger
1452 ;; than reported by lseek() because lseek() obviously
1453 ;; cannot take into account output we have not sent
1455 (dolist (later (fd-stream-output-later stream))
1456 (incf posn (- (caddr later)
1458 (incf posn (fd-stream-obuf-tail stream))
1459 ;; Adjust for unread input: If there is any input
1460 ;; read from UNIX but not supplied to the user of the
1461 ;; stream, the *real* file position will smaller than
1462 ;; reported, because we want to look like the unread
1463 ;; stuff is still available.
1464 (decf posn (- (fd-stream-ibuf-tail stream)
1465 (fd-stream-ibuf-head stream)))
1466 (when (fd-stream-unread stream)
1468 ;; Divide bytes by element size.
1469 (truncate posn (fd-stream-element-size stream)))
1470 ((eq errno sb!unix:espipe)
1473 (sb!sys:with-interrupts
1474 (simple-stream-perror "failure in Unix lseek() on ~S"
1477 (let ((offset 0) origin)
1478 (declare (type (alien sb!unix:off-t) offset))
1479 ;; Make sure we don't have any output pending, because if we
1480 ;; move the file pointer before writing this stuff, it will be
1481 ;; written in the wrong location.
1482 (flush-output-buffer stream)
1484 ((null (fd-stream-output-later stream)))
1485 (sb!sys:serve-all-events))
1486 ;; Clear out any pending input to force the next read to go to
1488 (setf (fd-stream-unread stream) nil)
1489 (setf (fd-stream-ibuf-head stream) 0)
1490 (setf (fd-stream-ibuf-tail stream) 0)
1491 ;; Trash cached value for listen, so that we check next time.
1492 (setf (fd-stream-listen stream) nil)
1494 (cond ((eq newpos :start)
1495 (setf offset 0 origin sb!unix:l_set))
1497 (setf offset 0 origin sb!unix:l_xtnd))
1498 ((typep newpos '(alien sb!unix:off-t))
1499 (setf offset (* newpos (fd-stream-element-size stream))
1500 origin sb!unix:l_set))
1502 (error "invalid position given to FILE-POSITION: ~S" newpos)))
1503 (multiple-value-bind (posn errno)
1504 (sb!unix:unix-lseek (fd-stream-fd stream) offset origin)
1505 (cond ((typep posn '(alien sb!unix:off-t))
1507 ((eq errno sb!unix:espipe)
1510 (simple-stream-perror "error in Unix lseek() on ~S"
1514 ;;;; creation routines (MAKE-FD-STREAM and OPEN)
1516 ;;; Create a stream for the given Unix file descriptor.
1518 ;;; If INPUT is non-NIL, allow input operations. If OUTPUT is non-nil,
1519 ;;; allow output operations. If neither INPUT nor OUTPUT is specified,
1520 ;;; default to allowing input.
1522 ;;; ELEMENT-TYPE indicates the element type to use (as for OPEN).
1524 ;;; BUFFERING indicates the kind of buffering to use.
1526 ;;; TIMEOUT (if true) is the number of seconds to wait for input. If
1527 ;;; NIL (the default), then wait forever. When we time out, we signal
1530 ;;; FILE is the name of the file (will be returned by PATHNAME).
1532 ;;; NAME is used to identify the stream when printed.
1533 (defun make-fd-stream (fd
1536 (output nil output-p)
1537 (element-type 'base-char)
1539 (external-format :default)
1547 (format nil "file ~S" file)
1548 (format nil "descriptor ~W" fd)))
1550 (declare (type index fd) (type (or index null) timeout)
1551 (type (member :none :line :full) buffering))
1552 (cond ((not (or input-p output-p))
1554 ((not (or input output))
1555 (error "File descriptor must be opened either for input or output.")))
1556 (let ((stream (%make-fd-stream :fd fd
1560 :delete-original delete-original
1562 :buffering buffering
1563 :external-format external-format
1565 (set-fd-stream-routines stream element-type input output input-buffer-p)
1566 (when (and auto-close (fboundp 'finalize))
1569 (sb!unix:unix-close fd)
1571 (format *terminal-io* "** closed file descriptor ~W **~%"
1575 ;;; Pick a name to use for the backup file for the :IF-EXISTS
1576 ;;; :RENAME-AND-DELETE and :RENAME options.
1577 (defun pick-backup-name (name)
1578 (declare (type simple-base-string name))
1579 (concatenate 'simple-base-string name ".bak"))
1581 ;;; Ensure that the given arg is one of the given list of valid
1582 ;;; things. Allow the user to fix any problems.
1583 (defun ensure-one-of (item list what)
1584 (unless (member item list)
1585 (error 'simple-type-error
1587 :expected-type `(member ,@list)
1588 :format-control "~@<~S is ~_invalid for ~S; ~_need one of~{ ~S~}~:>"
1589 :format-arguments (list item what list))))
1591 ;;; Rename NAMESTRING to ORIGINAL. First, check whether we have write
1592 ;;; access, since we don't want to trash unwritable files even if we
1593 ;;; technically can. We return true if we succeed in renaming.
1594 (defun rename-the-old-one (namestring original)
1595 (unless (sb!unix:unix-access namestring sb!unix:w_ok)
1596 (error "~@<The file ~2I~_~S ~I~_is not writable.~:>" namestring))
1597 (multiple-value-bind (okay err) (sb!unix:unix-rename namestring original)
1600 (error 'simple-file-error
1601 :pathname namestring
1603 "~@<couldn't rename ~2I~_~S ~I~_to ~2I~_~S: ~4I~_~A~:>"
1604 :format-arguments (list namestring original (strerror err))))))
1606 (defun open (filename
1609 (element-type 'base-char)
1610 (if-exists nil if-exists-given)
1611 (if-does-not-exist nil if-does-not-exist-given)
1612 (external-format :default)
1613 &aux ; Squelch assignment warning.
1614 (direction direction)
1615 (if-does-not-exist if-does-not-exist)
1616 (if-exists if-exists))
1618 "Return a stream which reads from or writes to FILENAME.
1620 :DIRECTION - one of :INPUT, :OUTPUT, :IO, or :PROBE
1621 :ELEMENT-TYPE - the type of object to read or write, default BASE-CHAR
1622 :IF-EXISTS - one of :ERROR, :NEW-VERSION, :RENAME, :RENAME-AND-DELETE,
1623 :OVERWRITE, :APPEND, :SUPERSEDE or NIL
1624 :IF-DOES-NOT-EXIST - one of :ERROR, :CREATE or NIL
1625 See the manual for details."
1627 ;; Calculate useful stuff.
1628 (multiple-value-bind (input output mask)
1630 (:input (values t nil sb!unix:o_rdonly))
1631 (:output (values nil t sb!unix:o_wronly))
1632 (:io (values t t sb!unix:o_rdwr))
1633 (:probe (values t nil sb!unix:o_rdonly)))
1634 (declare (type index mask))
1635 (let* ((pathname (merge-pathnames filename))
1637 (cond ((unix-namestring pathname input))
1638 ((and input (eq if-does-not-exist :create))
1639 (unix-namestring pathname nil))
1640 ((and (eq direction :io) (not if-does-not-exist-given))
1641 (unix-namestring pathname nil)))))
1642 ;; Process if-exists argument if we are doing any output.
1644 (unless if-exists-given
1646 (if (eq (pathname-version pathname) :newest)
1649 (ensure-one-of if-exists
1650 '(:error :new-version :rename
1651 :rename-and-delete :overwrite
1652 :append :supersede nil)
1655 ((:new-version :error nil)
1656 (setf mask (logior mask sb!unix:o_excl)))
1657 ((:rename :rename-and-delete)
1658 (setf mask (logior mask sb!unix:o_creat)))
1660 (setf mask (logior mask sb!unix:o_trunc)))
1662 (setf mask (logior mask sb!unix:o_append)))))
1664 (setf if-exists :ignore-this-arg)))
1666 (unless if-does-not-exist-given
1667 (setf if-does-not-exist
1668 (cond ((eq direction :input) :error)
1670 (member if-exists '(:overwrite :append)))
1672 ((eq direction :probe)
1676 (ensure-one-of if-does-not-exist
1677 '(:error :create nil)
1679 (if (eq if-does-not-exist :create)
1680 (setf mask (logior mask sb!unix:o_creat)))
1682 (let ((original (case if-exists
1683 ((:rename :rename-and-delete)
1684 (pick-backup-name namestring))
1686 ;; KLUDGE: Provent CLOSE from deleting
1687 ;; appending streams when called with :ABORT T
1689 (delete-original (eq if-exists :rename-and-delete))
1691 (when (and original (not (eq original namestring)))
1692 ;; We are doing a :RENAME or :RENAME-AND-DELETE. Determine
1693 ;; whether the file already exists, make sure the original
1694 ;; file is not a directory, and keep the mode.
1697 (multiple-value-bind (okay err/dev inode orig-mode)
1698 (sb!unix:unix-stat namestring)
1699 (declare (ignore inode)
1700 (type (or index null) orig-mode))
1703 (when (and output (= (logand orig-mode #o170000)
1705 (error 'simple-file-error
1706 :pathname namestring
1708 "can't open ~S for output: is a directory"
1709 :format-arguments (list namestring)))
1710 (setf mode (logand orig-mode #o777))
1712 ((eql err/dev sb!unix:enoent)
1715 (simple-file-perror "can't find ~S"
1719 (rename-the-old-one namestring original))
1721 (setf delete-original nil)
1722 ;; In order to use :SUPERSEDE instead, we have to make
1723 ;; sure SB!UNIX:O_CREAT corresponds to
1724 ;; IF-DOES-NOT-EXIST. SB!UNIX:O_CREAT was set before
1725 ;; because of IF-EXISTS being :RENAME.
1726 (unless (eq if-does-not-exist :create)
1728 (logior (logandc2 mask sb!unix:o_creat)
1730 (setf if-exists :supersede))))
1732 ;; Now we can try the actual Unix open(2).
1733 (multiple-value-bind (fd errno)
1735 (sb!unix:unix-open namestring mask mode)
1736 (values nil sb!unix:enoent))
1737 (labels ((open-error (format-control &rest format-arguments)
1738 (error 'simple-file-error
1740 :format-control format-control
1741 :format-arguments format-arguments))
1742 (vanilla-open-error ()
1743 (simple-file-perror "error opening ~S" pathname errno)))
1746 ((:input :output :io)
1750 :element-type element-type
1751 :external-format external-format
1754 :delete-original delete-original
1760 (%make-fd-stream :name namestring
1763 :element-type element-type)))
1766 ((eql errno sb!unix:enoent)
1767 (case if-does-not-exist
1768 (:error (vanilla-open-error))
1770 (open-error "~@<The path ~2I~_~S ~I~_does not exist.~:>"
1773 ((and (eql errno sb!unix:eexist) (null if-exists))
1776 (vanilla-open-error)))))))))
1780 ;;; the stream connected to the controlling terminal, or NIL if there is none
1783 ;;; the stream connected to the standard input (file descriptor 0)
1786 ;;; the stream connected to the standard output (file descriptor 1)
1789 ;;; the stream connected to the standard error output (file descriptor 2)
1792 ;;; This is called when the cold load is first started up, and may also
1793 ;;; be called in an attempt to recover from nested errors.
1794 (defun stream-cold-init-or-reset ()
1796 (setf *terminal-io* (make-synonym-stream '*tty*))
1797 (setf *standard-output* (make-synonym-stream '*stdout*))
1798 (setf *standard-input* (make-synonym-stream '*stdin*))
1799 (setf *error-output* (make-synonym-stream '*stderr*))
1800 (setf *query-io* (make-synonym-stream '*terminal-io*))
1801 (setf *debug-io* *query-io*)
1802 (setf *trace-output* *standard-output*)
1805 ;;; This is called whenever a saved core is restarted.
1806 (defun stream-reinit ()
1807 (setf *available-buffers* nil)
1809 (make-fd-stream 0 :name "standard input" :input t :buffering :line))
1811 (make-fd-stream 1 :name "standard output" :output t :buffering :line))
1813 (make-fd-stream 2 :name "standard error" :output t :buffering :line))
1814 (let* ((ttyname #.(coerce "/dev/tty" 'simple-base-string))
1815 (tty (sb!unix:unix-open ttyname sb!unix:o_rdwr #o666)))
1819 :name "the terminal"
1824 (setf *tty* (make-two-way-stream *stdin* *stdout*))))
1829 ;;; the Unix way to beep
1830 (defun beep (stream)
1831 (write-char (code-char bell-char-code) stream)
1832 (finish-output stream))
1834 ;;; This is kind of like FILE-POSITION, but is an internal hack used
1835 ;;; by the filesys stuff to get and set the file name.
1837 ;;; FIXME: misleading name, screwy interface
1838 (defun file-name (stream &optional new-name)
1839 (when (typep stream 'file-stream)
1841 (setf (fd-stream-pathname stream) new-name)
1842 (setf (fd-stream-file stream)
1843 (unix-namestring new-name nil))
1846 (fd-stream-pathname stream)))))
1848 ;;;; international character support (which is trivial for our simple
1849 ;;;; character sets)
1851 ;;;; (Those who do Lisp only in English might not remember that ANSI
1852 ;;;; requires these functions to be exported from package
1855 (defun file-string-length (stream object)
1856 (declare (type (or string character) object) (type file-stream stream))
1858 "Return the delta in STREAM's FILE-POSITION that would be caused by writing
1859 OBJECT to STREAM. Non-trivial only in implementations that support
1860 international character sets."
1861 (declare (ignore stream))
1864 (string (length object))))
1866 (defun stream-external-format (stream)
1867 (declare (type file-stream stream))
1869 "Return the actual external format for file-streams, otherwise :DEFAULT."
1870 (if (typep stream 'file-stream)
1871 (fd-stream-external-format stream)