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 ;;; FIXME: Wouldn't it be clearer to just have the structure
15 ;;; definition be DEFSTRUCT FILE-STREAM (instead of DEFSTRUCT
16 ;;; FD-STREAM)? That way we'd have TYPE-OF and PRINT-OBJECT refer to
17 ;;; these objects as FILE-STREAMs (the ANSI name) instead of the
18 ;;; internal implementation name FD-STREAM, and there might be other
20 (deftype file-stream () 'fd-stream)
22 ;;;; buffer manipulation routines
24 ;;; FIXME: Is it really good to maintain this pool separate from the
25 ;;; GC and the C malloc logic?
26 (defvar *available-buffers* ()
28 "List of available buffers. Each buffer is an sap pointing to
29 bytes-per-buffer of memory.")
31 (defconstant bytes-per-buffer (* 4 1024)
33 "Number of bytes per buffer.")
35 ;;; Return the next available buffer, creating one if necessary.
36 #!-sb-fluid (declaim (inline next-available-buffer))
37 (defun next-available-buffer ()
38 (if *available-buffers*
39 (pop *available-buffers*)
40 (allocate-system-memory bytes-per-buffer)))
42 ;;;; the FD-STREAM structure
45 (:constructor %make-fd-stream)
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 (def!method print-object ((fd-stream fd-stream) stream)
91 (declare (type stream stream))
92 (print-unreadable-object (fd-stream stream :type t :identity t)
93 (format stream "for ~S" (fd-stream-name fd-stream))))
95 ;;;; output routines and related noise
97 (defvar *output-routines* ()
99 "List of all available output routines. Each element is a list of the
100 element-type output, the kind of buffering, the function name, and the number
101 of bytes per element.")
103 ;;; This is called by the server when we can write to the given file
104 ;;; descriptor. Attempt to write the data again. If it worked, remove
105 ;;; the data from the OUTPUT-LATER list. If it didn't work, something
107 (defun do-output-later (stream)
108 (let* ((stuff (pop (fd-stream-output-later stream)))
112 (reuse-sap (cadddr stuff))
113 (length (- end start)))
114 (declare (type index start end length))
115 (multiple-value-bind (count errno)
116 (sb!unix:unix-write (fd-stream-fd stream)
121 (if (= errno sb!unix:ewouldblock)
122 (error "Write would have blocked, but SERVER told us to go.")
123 (error "while writing ~S: ~A"
125 (sb!unix:get-unix-error-msg errno))))
126 ((eql count length) ; Hot damn, it worked.
128 (push base *available-buffers*)))
129 ((not (null count)) ; Sorta worked.
131 (the index (+ start count))
133 (fd-stream-output-later stream))))))
134 (unless (fd-stream-output-later stream)
135 (sb!sys:remove-fd-handler (fd-stream-handler stream))
136 (setf (fd-stream-handler stream) nil)))
138 ;;; Arange to output the string when we can write on the file descriptor.
139 (defun output-later (stream base start end reuse-sap)
140 (cond ((null (fd-stream-output-later stream))
141 (setf (fd-stream-output-later stream)
142 (list (list base start end reuse-sap)))
143 (setf (fd-stream-handler stream)
144 (sb!sys:add-fd-handler (fd-stream-fd stream)
147 (declare (ignore fd))
148 (do-output-later stream)))))
150 (nconc (fd-stream-output-later stream)
151 (list (list base start end reuse-sap)))))
153 (let ((new-buffer (next-available-buffer)))
154 (setf (fd-stream-obuf-sap stream) new-buffer)
155 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
157 ;;; Output the given noise. Check to see whether there are any pending
158 ;;; writes. If so, just queue this one. Otherwise, try to write it. If
159 ;;; this would block, queue it.
160 (defun do-output (stream base start end reuse-sap)
161 (declare (type fd-stream stream)
162 (type (or system-area-pointer (simple-array * (*))) base)
163 (type index start end))
164 (if (not (null (fd-stream-output-later stream))) ; something buffered.
166 (output-later stream base start end reuse-sap)
167 ;; ### check to see whether any of this noise can be output
169 (let ((length (- end start)))
170 (multiple-value-bind (count errno)
171 (sb!unix:unix-write (fd-stream-fd stream) base start length)
173 (if (= errno sb!unix:ewouldblock)
174 (output-later stream base start end reuse-sap)
175 ;; FIXME: This and various other errors in this file
176 ;; should probably be STREAM-ERROR.
177 (error "while writing ~S: ~A"
179 (sb!unix:get-unix-error-msg errno))))
180 ((not (eql count length))
181 (output-later stream base (the index (+ start count))
184 ;;; Flush any data in the output buffer.
185 (defun flush-output-buffer (stream)
186 (let ((length (fd-stream-obuf-tail stream)))
188 (do-output stream (fd-stream-obuf-sap stream) 0 length t)
189 (setf (fd-stream-obuf-tail stream) 0))))
191 ;;; Define output routines that output numbers SIZE bytes long for the
192 ;;; given bufferings. Use BODY to do the actual output.
193 (defmacro def-output-routines ((name-fmt size &rest bufferings) &body body)
194 (declare (optimize (speed 1)))
197 #'(lambda (buffering)
199 (intern (let ((*print-case* :upcase))
200 (format nil name-fmt (car buffering))))))
202 (defun ,function (stream byte)
203 ,(unless (eq (car buffering) :none)
204 `(when (< (fd-stream-obuf-length stream)
205 (+ (fd-stream-obuf-tail stream)
207 (flush-output-buffer stream)))
209 (incf (fd-stream-obuf-tail stream) ,size)
210 ,(ecase (car buffering)
212 `(flush-output-buffer stream))
214 `(when (eq (char-code byte) (char-code #\Newline))
215 (flush-output-buffer stream)))
219 (setf *output-routines*
220 (nconc *output-routines*
227 (cdr buffering)))))))
230 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
235 (if (and (base-char-p byte) (char= byte #\Newline))
236 (setf (fd-stream-char-pos stream) 0)
237 (incf (fd-stream-char-pos stream)))
238 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
241 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
243 (:none (unsigned-byte 8))
244 (:full (unsigned-byte 8)))
245 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
248 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
250 (:none (signed-byte 8))
251 (:full (signed-byte 8)))
252 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
253 (fd-stream-obuf-tail stream))
256 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
258 (:none (unsigned-byte 16))
259 (:full (unsigned-byte 16)))
260 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
263 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
265 (:none (signed-byte 16))
266 (:full (signed-byte 16)))
267 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
268 (fd-stream-obuf-tail stream))
271 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
273 (:none (unsigned-byte 32))
274 (:full (unsigned-byte 32)))
275 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
278 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
280 (:none (signed-byte 32))
281 (:full (signed-byte 32)))
282 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
283 (fd-stream-obuf-tail stream))
286 ;;; Do the actual output. If there is space to buffer the string,
287 ;;; buffer it. If the string would normally fit in the buffer, but
288 ;;; doesn't because of other stuff in the buffer, flush the old noise
289 ;;; out of the buffer and put the string in it. Otherwise we have a
290 ;;; very long string, so just send it directly (after flushing the
291 ;;; buffer, of course).
292 (defun output-raw-bytes (fd-stream thing &optional start end)
294 "Output THING to FD-STREAM. THING can be any kind of vector or a SAP. If
295 THING is a SAP, END must be supplied (as length won't work)."
296 (let ((start (or start 0))
297 (end (or end (length (the (simple-array * (*)) thing)))))
298 (declare (type index start end))
299 (let* ((len (fd-stream-obuf-length fd-stream))
300 (tail (fd-stream-obuf-tail fd-stream))
302 (bytes (- end start))
303 (newtail (+ tail bytes)))
304 (cond ((minusp bytes) ; error case
305 (error ":END before :START!"))
306 ((zerop bytes)) ; easy case
308 (if (system-area-pointer-p thing)
309 (system-area-copy thing
310 (* start sb!vm:byte-bits)
311 (fd-stream-obuf-sap fd-stream)
312 (* tail sb!vm:byte-bits)
313 (* bytes sb!vm:byte-bits))
314 ;; FIXME: There should be some type checking somewhere to
315 ;; verify that THING here is a vector, not just <not a SAP>.
316 (copy-to-system-area thing
317 (+ (* start sb!vm:byte-bits)
318 (* sb!vm:vector-data-offset
320 (fd-stream-obuf-sap fd-stream)
321 (* tail sb!vm:byte-bits)
322 (* bytes sb!vm:byte-bits)))
323 (setf (fd-stream-obuf-tail fd-stream) newtail))
325 (flush-output-buffer fd-stream)
326 (if (system-area-pointer-p thing)
327 (system-area-copy thing
328 (* start sb!vm:byte-bits)
329 (fd-stream-obuf-sap fd-stream)
331 (* bytes sb!vm:byte-bits))
332 ;; FIXME: There should be some type checking somewhere to
333 ;; verify that THING here is a vector, not just <not a SAP>.
334 (copy-to-system-area thing
335 (+ (* start sb!vm:byte-bits)
336 (* sb!vm:vector-data-offset
338 (fd-stream-obuf-sap fd-stream)
340 (* bytes sb!vm:byte-bits)))
341 (setf (fd-stream-obuf-tail fd-stream) bytes))
343 (flush-output-buffer fd-stream)
344 (do-output fd-stream thing start end nil))))))
346 ;;; the routine to use to output a string. If the stream is
347 ;;; unbuffered, slam the string down the file descriptor, otherwise
348 ;;; use OUTPUT-RAW-BYTES to buffer the string. Update charpos by
349 ;;; checking to see where the last newline was.
351 ;;; Note: some bozos (the FASL dumper) call write-string with things
352 ;;; other than strings. Therefore, we must make sure we have a string
353 ;;; before calling POSITION on it.
354 ;;; KLUDGE: It would be better to fix the bozos instead of trying to
355 ;;; cover for them here. -- WHN 20000203
356 (defun fd-sout (stream thing start end)
357 (let ((start (or start 0))
358 (end (or end (length (the vector thing)))))
359 (declare (fixnum start end))
361 (let ((last-newline (and (find #\newline (the simple-string thing)
362 :start start :end end)
363 (position #\newline (the simple-string thing)
367 (ecase (fd-stream-buffering stream)
369 (output-raw-bytes stream thing start end))
371 (output-raw-bytes stream thing start end)
373 (flush-output-buffer stream)))
375 (do-output stream thing start end nil)))
377 (setf (fd-stream-char-pos stream)
378 (- end last-newline 1))
379 (incf (fd-stream-char-pos stream)
381 (ecase (fd-stream-buffering stream)
383 (output-raw-bytes stream thing start end))
385 (do-output stream thing start end nil))))))
387 ;;; Find an output routine to use given the type and buffering. Return
388 ;;; as multiple values the routine, the real type transfered, and the
389 ;;; number of bytes per element.
390 (defun pick-output-routine (type buffering)
391 (dolist (entry *output-routines*)
392 (when (and (subtypep type (car entry))
393 (eq buffering (cadr entry)))
394 (return (values (symbol-function (caddr entry))
398 ;;;; input routines and related noise
400 ;;; a list of all available input routines. Each element is a list of
401 ;;; the element-type input, the function name, and the number of bytes
403 (defvar *input-routines* ())
405 ;;; Fill the input buffer, and return the first character. Throw to
406 ;;; EOF-INPUT-CATCHER if the eof was reached. Drop into SYSTEM:SERVER
408 (defun do-input (stream)
409 (let ((fd (fd-stream-fd stream))
410 (ibuf-sap (fd-stream-ibuf-sap stream))
411 (buflen (fd-stream-ibuf-length stream))
412 (head (fd-stream-ibuf-head stream))
413 (tail (fd-stream-ibuf-tail stream)))
414 (declare (type index head tail))
416 (cond ((eql head tail)
419 (setf (fd-stream-ibuf-head stream) 0)
420 (setf (fd-stream-ibuf-tail stream) 0))
423 (system-area-copy ibuf-sap (* head sb!vm:byte-bits)
424 ibuf-sap 0 (* tail sb!vm:byte-bits))
426 (setf (fd-stream-ibuf-head stream) 0)
427 (setf (fd-stream-ibuf-tail stream) tail))))
428 (setf (fd-stream-listen stream) nil)
429 (multiple-value-bind (count errno)
430 ;; FIXME: Judging from compiler warnings, this WITH-ALIEN form expands
431 ;; into something which uses the not-yet-defined type
432 ;; (SB!ALIEN-INTERNALS:ALIEN (* (SB!ALIEN:STRUCT SB!UNIX:FD-SET))).
433 ;; This is probably inefficient and unsafe and generally bad, so
434 ;; try to find some way to make that type known before
436 (sb!alien:with-alien ((read-fds (sb!alien:struct sb!unix:fd-set)))
437 (sb!unix:fd-zero read-fds)
438 (sb!unix:fd-set fd read-fds)
439 (sb!unix:unix-fast-select (1+ fd)
440 (sb!alien:addr read-fds)
448 (unless #!-mp (sb!sys:wait-until-fd-usable
449 fd :input (fd-stream-timeout stream))
450 #!+mp (sb!mp:process-wait-until-fd-usable
451 fd :input (fd-stream-timeout stream))
452 (error 'io-timeout :stream stream :direction :read)))
454 (error "problem checking to see whether ~S is readable: ~A"
456 (sb!unix:get-unix-error-msg errno)))))
457 (multiple-value-bind (count errno)
458 (sb!unix:unix-read fd
459 (sb!sys:int-sap (+ (sb!sys:sap-int ibuf-sap) tail))
462 (if (eql errno sb!unix:ewouldblock)
464 (unless #!-mp (sb!sys:wait-until-fd-usable
465 fd :input (fd-stream-timeout stream))
466 #!+mp (sb!mp:process-wait-until-fd-usable
467 fd :input (fd-stream-timeout stream))
468 (error 'io-timeout :stream stream :direction :read))
470 (error "error reading ~S: ~A"
472 (sb!unix:get-unix-error-msg errno))))
474 (setf (fd-stream-listen stream) :eof)
475 (throw 'eof-input-catcher nil))
477 (incf (fd-stream-ibuf-tail stream) count))))))
479 ;;; Make sure there are at least BYTES number of bytes in the input
480 ;;; buffer. Keep calling DO-INPUT until that condition is met.
481 (defmacro input-at-least (stream bytes)
482 (let ((stream-var (gensym))
483 (bytes-var (gensym)))
484 `(let ((,stream-var ,stream)
487 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
488 (fd-stream-ibuf-head ,stream-var))
491 (do-input ,stream-var)))))
493 ;;; a macro to wrap around all input routines to handle EOF-ERROR noise
494 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
495 (let ((stream-var (gensym))
496 (element-var (gensym)))
497 `(let ((,stream-var ,stream))
498 (if (fd-stream-unread ,stream-var)
500 (fd-stream-unread ,stream-var)
501 (setf (fd-stream-unread ,stream-var) nil)
502 (setf (fd-stream-listen ,stream-var) nil))
504 (catch 'eof-input-catcher
505 (input-at-least ,stream-var ,bytes)
508 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
511 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
513 (defmacro def-input-routine (name
517 (defun ,name (stream eof-error eof-value)
518 (input-wrapper (stream ,size eof-error eof-value)
519 (let ((,sap (fd-stream-ibuf-sap stream))
520 (,head (fd-stream-ibuf-head stream)))
522 (setf *input-routines*
523 (nconc *input-routines*
524 (list (list ',type ',name ',size))))))
526 ;;; STREAM-IN routine for reading a string char
527 (def-input-routine input-character
528 (character 1 sap head)
529 (code-char (sap-ref-8 sap head)))
531 ;;; STREAM-IN routine for reading an unsigned 8 bit number
532 (def-input-routine input-unsigned-8bit-byte
533 ((unsigned-byte 8) 1 sap head)
534 (sap-ref-8 sap head))
536 ;;; STREAM-IN routine for reading a signed 8 bit number
537 (def-input-routine input-signed-8bit-number
538 ((signed-byte 8) 1 sap head)
539 (signed-sap-ref-8 sap head))
541 ;;; STREAM-IN routine for reading an unsigned 16 bit number
542 (def-input-routine input-unsigned-16bit-byte
543 ((unsigned-byte 16) 2 sap head)
544 (sap-ref-16 sap head))
546 ;;; STREAM-IN routine for reading a signed 16 bit number
547 (def-input-routine input-signed-16bit-byte
548 ((signed-byte 16) 2 sap head)
549 (signed-sap-ref-16 sap head))
551 ;;; STREAM-IN routine for reading a unsigned 32 bit number
552 (def-input-routine input-unsigned-32bit-byte
553 ((unsigned-byte 32) 4 sap head)
554 (sap-ref-32 sap head))
556 ;;; STREAM-IN routine for reading a signed 32 bit number
557 (def-input-routine input-signed-32bit-byte
558 ((signed-byte 32) 4 sap head)
559 (signed-sap-ref-32 sap head))
561 ;;; Find an input routine to use given the type. Return as multiple
562 ;;; values the routine, the real type transfered, and the number of
563 ;;; bytes per element.
564 (defun pick-input-routine (type)
565 (dolist (entry *input-routines*)
566 (when (subtypep type (car entry))
567 (return (values (symbol-function (cadr entry))
571 ;;; Returns a string constructed from the sap, start, and end.
572 (defun string-from-sap (sap start end)
573 (declare (type index start end))
574 (let* ((length (- end start))
575 (string (make-string length)))
576 (copy-from-system-area sap (* start sb!vm:byte-bits)
577 string (* sb!vm:vector-data-offset sb!vm:word-bits)
578 (* length sb!vm:byte-bits))
581 ;;; the N-BIN method for FD-STREAMs. This blocks in UNIX-READ. It is
582 ;;; generally used where there is a definite amount of reading to be
583 ;;; done, so blocking isn't too problematical.
584 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
585 (declare (type fd-stream stream))
586 (declare (type index start requested))
587 (do ((total-copied 0))
589 (declare (type index total-copied))
590 (let* ((remaining-request (- requested total-copied))
591 (head (fd-stream-ibuf-head stream))
592 (tail (fd-stream-ibuf-tail stream))
593 (available (- tail head))
594 (this-copy (min remaining-request available))
595 (this-start (+ start total-copied))
596 (sap (fd-stream-ibuf-sap stream)))
597 (declare (type index remaining-request head tail available))
598 (declare (type index this-copy))
599 ;; Copy data from stream buffer into user's buffer.
600 (if (typep buffer 'system-area-pointer)
601 (system-area-copy sap (* head sb!vm:byte-bits)
602 buffer (* this-start sb!vm:byte-bits)
603 (* this-copy sb!vm:byte-bits))
604 (copy-from-system-area sap (* head sb!vm:byte-bits)
605 buffer (+ (* this-start sb!vm:byte-bits)
606 (* sb!vm:vector-data-offset
608 (* this-copy sb!vm:byte-bits)))
609 (incf (fd-stream-ibuf-head stream) this-copy)
610 (incf total-copied this-copy)
611 ;; Maybe we need to refill the stream buffer.
612 (cond (;; If there were enough data in the stream buffer, we're done.
613 (= total-copied requested)
614 (return total-copied))
615 (;; If EOF, we're done in another way.
616 (zerop (refill-fd-stream-buffer stream))
618 (error 'end-of-file :stream stream)
619 (return total-copied)))
620 ;; Otherwise we refilled the stream buffer, so fall
621 ;; through into another pass of the loop.
624 ;;; Try to refill the stream buffer. Return the number of bytes read.
625 ;;; (For EOF, the return value will be zero, otherwise positive.)
626 (defun refill-fd-stream-buffer (stream)
627 ;; We don't have any logic to preserve leftover bytes in the buffer,
628 ;; so we should only be called when the buffer is empty.
629 (aver (= (fd-stream-ibuf-head stream) (fd-stream-ibuf-tail stream)))
630 (multiple-value-bind (count err)
631 (sb!unix:unix-read (fd-stream-fd stream)
632 (fd-stream-ibuf-sap stream)
633 (fd-stream-ibuf-length stream))
634 (declare (type (or index null) count))
636 (error "error reading ~S: ~A"
638 (sb!unix:get-unix-error-msg err)))
639 (setf (fd-stream-listen stream) nil
640 (fd-stream-ibuf-head stream) 0
641 (fd-stream-ibuf-tail stream) count)
642 ; (format t "~%buffer=~%--~%")
644 ; (write-char (code-char (sap-ref-8 (fd-stream-ibuf-sap stream) i))))
645 ; (format t "~%--~%")
647 (format t "/REFILL-FD-STREAM-BUFFER = ~D~%" count)
650 ;;;; utility functions (misc routines, etc)
652 ;;; Fill in the various routine slots for the given type. INPUT-P and
653 ;;; OUTPUT-P indicate what slots to fill. The buffering slot must be
654 ;;; set prior to calling this routine.
655 (defun set-fd-stream-routines (fd-stream type input-p output-p buffer-p)
656 (let ((target-type (case type
657 ((:default unsigned-byte)
668 (when (fd-stream-obuf-sap fd-stream)
669 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
670 (setf (fd-stream-obuf-sap fd-stream) nil))
671 (when (fd-stream-ibuf-sap fd-stream)
672 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
673 (setf (fd-stream-ibuf-sap fd-stream) nil))
676 (multiple-value-bind (routine type size)
677 (pick-input-routine target-type)
679 (error "could not find any input routine for ~S" target-type))
680 (setf (fd-stream-ibuf-sap fd-stream) (next-available-buffer))
681 (setf (fd-stream-ibuf-length fd-stream) bytes-per-buffer)
682 (setf (fd-stream-ibuf-tail fd-stream) 0)
683 (if (subtypep type 'character)
684 (setf (fd-stream-in fd-stream) routine
685 (fd-stream-bin fd-stream) #'ill-bin)
686 (setf (fd-stream-in fd-stream) #'ill-in
687 (fd-stream-bin fd-stream) routine))
689 (setf (fd-stream-n-bin fd-stream) #'fd-stream-read-n-bytes)
691 (setf (lisp-stream-in-buffer fd-stream)
692 (make-array +in-buffer-length+
693 :element-type '(unsigned-byte 8)))))
694 (setf input-size size)
695 (setf input-type type)))
698 (multiple-value-bind (routine type size)
699 (pick-output-routine target-type (fd-stream-buffering fd-stream))
701 (error "could not find any output routine for ~S buffered ~S"
702 (fd-stream-buffering fd-stream)
704 (setf (fd-stream-obuf-sap fd-stream) (next-available-buffer))
705 (setf (fd-stream-obuf-length fd-stream) bytes-per-buffer)
706 (setf (fd-stream-obuf-tail fd-stream) 0)
707 (if (subtypep type 'character)
708 (setf (fd-stream-out fd-stream) routine
709 (fd-stream-bout fd-stream) #'ill-bout)
710 (setf (fd-stream-out fd-stream)
712 (pick-output-routine 'base-char
713 (fd-stream-buffering fd-stream)))
715 (fd-stream-bout fd-stream) routine))
716 (setf (fd-stream-sout fd-stream)
717 (if (eql size 1) #'fd-sout #'ill-out))
718 (setf (fd-stream-char-pos fd-stream) 0)
719 (setf output-size size)
720 (setf output-type type)))
722 (when (and input-size output-size
723 (not (eq input-size output-size)))
724 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
725 input-type input-size
726 output-type output-size))
727 (setf (fd-stream-element-size fd-stream)
728 (or input-size output-size))
730 (setf (fd-stream-element-type fd-stream)
731 (cond ((equal input-type output-type)
737 ((subtypep input-type output-type)
739 ((subtypep output-type input-type)
742 (error "Input type (~S) and output type (~S) are unrelated?"
746 ;;; Handle miscellaneous operations on FD-STREAM.
747 (defun fd-stream-misc-routine (fd-stream operation &optional arg1 arg2)
748 (declare (ignore arg2))
751 (or (not (eql (fd-stream-ibuf-head fd-stream)
752 (fd-stream-ibuf-tail fd-stream)))
753 (fd-stream-listen fd-stream)
754 (setf (fd-stream-listen fd-stream)
755 (eql (sb!alien:with-alien ((read-fds (sb!alien:struct
757 (sb!unix:fd-zero read-fds)
758 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
759 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
760 (sb!alien:addr read-fds)
764 (setf (fd-stream-unread fd-stream) arg1)
765 (setf (fd-stream-listen fd-stream) t))
768 ;; We got us an abort on our hands.
769 (when (fd-stream-handler fd-stream)
770 (sb!sys:remove-fd-handler (fd-stream-handler fd-stream))
771 (setf (fd-stream-handler fd-stream) nil))
772 (when (and (fd-stream-file fd-stream)
773 (fd-stream-obuf-sap fd-stream))
774 ;; We can't do anything unless we know what file were
775 ;; dealing with, and we don't want to do anything
776 ;; strange unless we were writing to the file.
777 (if (fd-stream-original fd-stream)
778 ;; We have a handle on the original, just revert.
779 (multiple-value-bind (okay err)
780 (sb!unix:unix-rename (fd-stream-original fd-stream)
781 (fd-stream-file fd-stream))
783 (error "~@<could not restore ~S to its original ~
784 contents: ~2I~_~A~:>"
785 (fd-stream-file fd-stream)
786 (sb!unix:get-unix-error-msg err))))
787 ;; We can't restore the original, so nuke that puppy.
788 (multiple-value-bind (okay err)
789 (sb!unix:unix-unlink (fd-stream-file fd-stream))
791 (error "~@<could not remove ~S: ~2I~_~A~:>"
792 (fd-stream-file fd-stream)
793 (sb!unix:get-unix-error-msg err)))))))
795 (fd-stream-misc-routine fd-stream :finish-output)
796 (when (and (fd-stream-original fd-stream)
797 (fd-stream-delete-original fd-stream))
798 (multiple-value-bind (okay err)
799 (sb!unix:unix-unlink (fd-stream-original fd-stream))
801 (error "~@<could not delete ~S during close ~
803 (fd-stream-original fd-stream)
805 (sb!unix:get-unix-error-msg err)))))))
806 (when (fboundp 'cancel-finalization)
807 (cancel-finalization fd-stream))
808 (sb!unix:unix-close (fd-stream-fd fd-stream))
809 (when (fd-stream-obuf-sap fd-stream)
810 (push (fd-stream-obuf-sap fd-stream) *available-buffers*)
811 (setf (fd-stream-obuf-sap fd-stream) nil))
812 (when (fd-stream-ibuf-sap fd-stream)
813 (push (fd-stream-ibuf-sap fd-stream) *available-buffers*)
814 (setf (fd-stream-ibuf-sap fd-stream) nil))
815 (sb!impl::set-closed-flame fd-stream))
817 (setf (fd-stream-unread fd-stream) nil)
818 (setf (fd-stream-ibuf-head fd-stream) 0)
819 (setf (fd-stream-ibuf-tail fd-stream) 0)
820 (catch 'eof-input-catcher
822 (let ((count (sb!alien:with-alien ((read-fds (sb!alien:struct
824 (sb!unix:fd-zero read-fds)
825 (sb!unix:fd-set (fd-stream-fd fd-stream) read-fds)
826 (sb!unix:unix-fast-select (1+ (fd-stream-fd fd-stream))
827 (sb!alien:addr read-fds)
834 (setf (fd-stream-ibuf-head fd-stream) 0)
835 (setf (fd-stream-ibuf-tail fd-stream) 0))
839 (flush-output-buffer fd-stream))
841 (flush-output-buffer fd-stream)
843 ((null (fd-stream-output-later fd-stream)))
844 (sb!sys:serve-all-events)))
846 (fd-stream-element-type fd-stream))
848 ;; FIXME: sb!unix:unix-isatty is undefined.
849 (sb!unix:unix-isatty (fd-stream-fd fd-stream)))
853 (fd-stream-char-pos fd-stream))
855 (multiple-value-bind (okay dev ino mode nlink uid gid rdev size
856 atime mtime ctime blksize blocks)
857 (sb!unix:unix-fstat (fd-stream-fd fd-stream))
858 (declare (ignore ino nlink uid gid rdev
859 atime mtime ctime blksize blocks))
861 (error "error in Unix fstat(2) on ~S: ~A"
863 (sb!unix:get-unix-error-msg dev)))
866 (truncate size (fd-stream-element-size fd-stream)))))
868 (fd-stream-file-position fd-stream arg1))))
870 (defun fd-stream-file-position (stream &optional newpos)
871 (declare (type fd-stream stream)
872 (type (or index (member nil :start :end)) newpos))
874 (sb!sys:without-interrupts
875 ;; First, find the position of the UNIX file descriptor in the
877 (multiple-value-bind (posn errno)
878 (sb!unix:unix-lseek (fd-stream-fd stream) 0 sb!unix:l_incr)
879 (declare (type (or index null) posn))
880 (cond ((fixnump posn)
881 ;; Adjust for buffered output: If there is any output
882 ;; buffered, the *real* file position will be larger
883 ;; than reported by lseek because lseek obviously
884 ;; cannot take into account output we have not sent
886 (dolist (later (fd-stream-output-later stream))
887 (incf posn (- (the index (caddr later))
888 (the index (cadr later)))))
889 (incf posn (fd-stream-obuf-tail stream))
890 ;; Adjust for unread input: If there is any input
891 ;; read from UNIX but not supplied to the user of the
892 ;; stream, the *real* file position will smaller than
893 ;; reported, because we want to look like the unread
894 ;; stuff is still available.
895 (decf posn (- (fd-stream-ibuf-tail stream)
896 (fd-stream-ibuf-head stream)))
897 (when (fd-stream-unread stream)
899 ;; Divide bytes by element size.
900 (truncate posn (fd-stream-element-size stream)))
901 ((eq errno sb!unix:espipe)
904 (sb!sys:with-interrupts
905 (error "error LSEEK'ing ~S: ~A"
907 (sb!unix:get-unix-error-msg errno)))))))
908 (let ((offset 0) origin)
909 (declare (type index offset))
910 ;; Make sure we don't have any output pending, because if we
911 ;; move the file pointer before writing this stuff, it will be
912 ;; written in the wrong location.
913 (flush-output-buffer stream)
915 ((null (fd-stream-output-later stream)))
916 (sb!sys:serve-all-events))
917 ;; Clear out any pending input to force the next read to go to
919 (setf (fd-stream-unread stream) nil)
920 (setf (fd-stream-ibuf-head stream) 0)
921 (setf (fd-stream-ibuf-tail stream) 0)
922 ;; Trash cached value for listen, so that we check next time.
923 (setf (fd-stream-listen stream) nil)
925 (cond ((eq newpos :start)
926 (setf offset 0 origin sb!unix:l_set))
928 (setf offset 0 origin sb!unix:l_xtnd))
929 ((typep newpos 'index)
930 (setf offset (* newpos (fd-stream-element-size stream))
931 origin sb!unix:l_set))
933 (error "invalid position given to file-position: ~S" newpos)))
934 (multiple-value-bind (posn errno)
935 (sb!unix:unix-lseek (fd-stream-fd stream) offset origin)
936 (cond ((typep posn 'fixnum)
938 ((eq errno sb!unix:espipe)
941 (error "error lseek'ing ~S: ~A"
943 (sb!unix:get-unix-error-msg errno))))))))
945 ;;;; creation routines (MAKE-FD-STREAM and OPEN)
947 ;;; Create a stream for the given Unix file descriptor.
949 ;;; If INPUT is non-NIL, allow input operations. If OUTPUT is non-nil,
950 ;;; allow output operations. If neither INPUT nor OUTPUT is specified,
951 ;;; default to allowing input.
953 ;;; ELEMENT-TYPE indicates the element type to use (as for OPEN).
955 ;;; BUFFERING indicates the kind of buffering to use.
957 ;;; TIMEOUT (if true) is the number of seconds to wait for input. If
958 ;;; NIL (the default), then wait forever. When we time out, we signal
961 ;;; FILE is the name of the file (will be returned by PATHNAME).
963 ;;; NAME is used to identify the stream when printed.
964 (defun make-fd-stream (fd
967 (output nil output-p)
968 (element-type 'base-char)
977 (format nil "file ~S" file)
978 (format nil "descriptor ~D" fd)))
980 (declare (type index fd) (type (or index null) timeout)
981 (type (member :none :line :full) buffering))
982 (cond ((not (or input-p output-p))
984 ((not (or input output))
985 (error "File descriptor must be opened either for input or output.")))
986 (let ((stream (%make-fd-stream :fd fd
990 :delete-original delete-original
994 (set-fd-stream-routines stream element-type input output input-buffer-p)
995 (when (and auto-close (fboundp 'finalize))
998 (sb!unix:unix-close fd)
1000 (format *terminal-io* "** closed file descriptor ~D **~%"
1004 ;;; Pick a name to use for the backup file for the :IF-EXISTS
1005 ;;; :RENAME-AND-DELETE and :RENAME options.
1006 (defun pick-backup-name (name)
1007 (declare (type simple-string name))
1008 (concatenate 'simple-string name ".bak"))
1010 ;;; Ensure that the given arg is one of the given list of valid
1011 ;;; things. Allow the user to fix any problems.
1012 (defun ensure-one-of (item list what)
1013 (unless (member item list)
1014 (error 'simple-type-error
1016 :expected-type `(member ,@list)
1017 :format-control "~@<~S is ~_invalid for ~S; ~_need one of~{ ~S~}~:>"
1018 :format-arguments (list item what list))))
1020 ;;; Rename NAMESTRING to ORIGINAL. First, check whether we have write
1021 ;;; access, since we don't want to trash unwritable files even if we
1022 ;;; technically can. We return true if we succeed in renaming.
1023 (defun do-old-rename (namestring original)
1024 (unless (sb!unix:unix-access namestring sb!unix:w_ok)
1025 (error "~@<The file ~2I~_~S ~I~_is not writable.~:>" namestring))
1026 (multiple-value-bind (okay err) (sb!unix:unix-rename namestring original)
1029 (error "~@<could not rename ~2I~_~S ~I~_to ~2I~_~S: ~4I~_~A~:>"
1032 (sb!unix:get-unix-error-msg err))
1035 (defun open (filename
1038 (element-type 'base-char)
1039 (if-exists nil if-exists-given)
1040 (if-does-not-exist nil if-does-not-exist-given)
1041 (external-format :default)
1042 &aux ; Squelch assignment warning.
1043 (direction direction)
1044 (if-does-not-exist if-does-not-exist)
1045 (if-exists if-exists))
1047 "Return a stream which reads from or writes to FILENAME.
1049 :DIRECTION - one of :INPUT, :OUTPUT, :IO, or :PROBE
1050 :ELEMENT-TYPE - the type of object to read or write, default BASE-CHAR
1051 :IF-EXISTS - one of :ERROR, :NEW-VERSION, :RENAME, :RENAME-AND-DELETE,
1052 :OVERWRITE, :APPEND, :SUPERSEDE or NIL
1053 :IF-DOES-NOT-EXIST - one of :ERROR, :CREATE or nil
1054 See the manual for details."
1056 (unless (eq external-format :default)
1057 (error "Any external format other than :DEFAULT isn't recognized."))
1059 ;; First, make sure that DIRECTION is valid.
1060 (ensure-one-of direction
1061 '(:input :output :io :probe)
1064 ;; Calculate useful stuff.
1065 (multiple-value-bind (input output mask)
1067 (:input (values t nil sb!unix:o_rdonly))
1068 (:output (values nil t sb!unix:o_wronly))
1069 (:io (values t t sb!unix:o_rdwr))
1070 (:probe (values t nil sb!unix:o_rdonly)))
1071 (declare (type index mask))
1072 (let* ((pathname (pathname filename))
1074 (cond ((unix-namestring pathname input))
1075 ((and input (eq if-does-not-exist :create))
1076 (unix-namestring pathname nil)))))
1077 ;; Process if-exists argument if we are doing any output.
1079 (unless if-exists-given
1081 (if (eq (pathname-version pathname) :newest)
1084 (ensure-one-of if-exists
1085 '(:error :new-version :rename
1086 :rename-and-delete :overwrite
1087 :append :supersede nil)
1091 (setf mask (logior mask sb!unix:o_excl)))
1092 ((:rename :rename-and-delete)
1093 (setf mask (logior mask sb!unix:o_creat)))
1094 ((:new-version :supersede)
1095 (setf mask (logior mask sb!unix:o_trunc)))
1097 (setf mask (logior mask sb!unix:o_append)))))
1099 (setf if-exists :ignore-this-arg)))
1101 (unless if-does-not-exist-given
1102 (setf if-does-not-exist
1103 (cond ((eq direction :input) :error)
1105 (member if-exists '(:overwrite :append)))
1107 ((eq direction :probe)
1111 (ensure-one-of if-does-not-exist
1112 '(:error :create nil)
1114 (if (eq if-does-not-exist :create)
1115 (setf mask (logior mask sb!unix:o_creat)))
1117 (let ((original (if (member if-exists
1118 '(:rename :rename-and-delete))
1119 (pick-backup-name namestring)))
1120 (delete-original (eq if-exists :rename-and-delete))
1123 ;; We are doing a :RENAME or :RENAME-AND-DELETE.
1124 ;; Determine whether the file already exists, make sure the original
1125 ;; file is not a directory, and keep the mode.
1128 (multiple-value-bind (okay err/dev inode orig-mode)
1129 (sb!unix:unix-stat namestring)
1130 (declare (ignore inode)
1131 (type (or index null) orig-mode))
1134 (when (and output (= (logand orig-mode #o170000)
1136 (error "cannot open ~S for output: is a directory"
1138 (setf mode (logand orig-mode #o777))
1140 ((eql err/dev sb!unix:enoent)
1143 (error "cannot find ~S: ~A"
1145 (sb!unix:get-unix-error-msg err/dev))))))))
1147 (do-old-rename namestring original))
1149 (setf delete-original nil)
1150 ;; In order to use :SUPERSEDE instead, we have to make sure
1151 ;; SB!UNIX:O_CREAT corresponds to IF-DOES-NOT-EXIST.
1152 ;; SB!UNIX:O_CREAT was set before because of IF-EXISTS being
1154 (unless (eq if-does-not-exist :create)
1156 (logior (logandc2 mask sb!unix:o_creat)
1158 (setf if-exists :supersede))))
1160 ;; Now we can try the actual Unix open(2).
1161 (multiple-value-bind (fd errno)
1163 (sb!unix:unix-open namestring mask mode)
1164 (values nil sb!unix:enoent))
1165 (labels ((open-error (format-control &rest format-arguments)
1166 (error 'simple-file-error
1168 :format-control format-control
1169 :format-arguments format-arguments))
1170 (vanilla-open-error ()
1171 (open-error "~@<error opening ~S: ~2I~_~A~:>"
1173 (sb!unix:get-unix-error-msg errno))))
1176 ((:input :output :io)
1180 :element-type element-type
1183 :delete-original delete-original
1189 (%make-fd-stream :name namestring
1192 :element-type element-type)))
1195 ((eql errno sb!unix:enoent)
1196 (case if-does-not-exist
1197 (:error (vanilla-open-error))
1200 "~@<The path ~2I~_~S ~I~_does not exist.~:>"
1203 ((and (eql errno sb!unix:eexist) if-exists)
1206 (vanilla-open-error)))))))))
1210 ;;; the stream connected to the controlling terminal, or NIL if there is none
1213 ;;; the stream connected to the standard input (file descriptor 0)
1216 ;;; the stream connected to the standard output (file descriptor 1)
1219 ;;; the stream connected to the standard error output (file descriptor 2)
1222 ;;; This is called when the cold load is first started up, and may also
1223 ;;; be called in an attempt to recover from nested errors.
1224 (defun stream-cold-init-or-reset ()
1226 (setf *terminal-io* (make-synonym-stream '*tty*))
1227 (setf *standard-output* (make-synonym-stream '*stdout*))
1228 (setf *standard-input*
1230 ;; FIXME: Why is *STANDARD-INPUT* a TWO-WAY-STREAM? ANSI says
1231 ;; it's an input stream.
1234 %make-two-way-stream (make-synonym-stream '*stdin*)
1236 (setf *error-output* (make-synonym-stream '*stderr*))
1237 (setf *query-io* (make-synonym-stream '*terminal-io*))
1238 (setf *debug-io* *query-io*)
1239 (setf *trace-output* *standard-output*)
1242 ;;; This is called whenever a saved core is restarted.
1243 (defun stream-reinit ()
1244 (setf *available-buffers* nil)
1246 (make-fd-stream 0 :name "standard input" :input t :buffering :line))
1248 (make-fd-stream 1 :name "standard output" :output t :buffering :line))
1250 (make-fd-stream 2 :name "standard error" :output t :buffering :line))
1251 (let ((tty (sb!unix:unix-open "/dev/tty" sb!unix:o_rdwr #o666)))
1255 :name "the terminal"
1260 (setf *tty* (make-two-way-stream *stdin* *stdout*))))
1265 ;;; the Unix way to beep
1266 (defun beep (stream)
1267 (write-char (code-char bell-char-code) stream)
1268 (finish-output stream))
1270 ;;; This is kind of like FILE-POSITION, but is an internal hack used
1271 ;;; by the filesys stuff to get and set the file name.
1272 (defun file-name (stream &optional new-name)
1273 (when (typep stream 'fd-stream)
1275 (setf (fd-stream-pathname stream) new-name)
1276 (setf (fd-stream-file stream)
1277 (unix-namestring new-name nil))
1280 (fd-stream-pathname stream)))))
1282 ;;;; international character support (which is trivial for our simple
1283 ;;;; character sets)
1285 ;;;; (Those who do Lisp only in English might not remember that ANSI
1286 ;;;; requires these functions to be exported from package
1289 (defun file-string-length (stream object)
1290 (declare (type (or string character) object) (type file-stream stream))
1292 "Return the delta in STREAM's FILE-POSITION that would be caused by writing
1293 OBJECT to STREAM. Non-trivial only in implementations that support
1294 international character sets."
1295 (declare (ignore stream))
1298 (string (length object))))
1300 (defun stream-external-format (stream)
1301 (declare (type file-stream stream) (ignore stream))