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")
16 ;;;; Streams hold BUFFER objects, which contain a SAP, size of the
17 ;;;; memory area the SAP stands for (LENGTH bytes), and HEAD and TAIL
18 ;;;; indexes which delimit the "valid", or "active" area of the
19 ;;;; memory. HEAD is inclusive, TAIL is exclusive.
21 ;;;; Buffers get allocated lazily, and are recycled by returning them
22 ;;;; to the *AVAILABLE-BUFFERS* list. Every buffer has its own
23 ;;;; finalizer, to take care of releasing the SAP memory when a stream
24 ;;;; is not properly closed.
26 ;;;; The code aims to provide a limited form of thread and interrupt
27 ;;;; safety: parallel writes and reads may lose output or input, cause
28 ;;;; interleaved IO, etc -- but they should not corrupt memory. The
29 ;;;; key to doing this is to read buffer state once, and update the
30 ;;;; state based on the read state:
32 ;;;; (let ((tail (buffer-tail buffer)))
34 ;;;; (setf (buffer-tail buffer) (+ tail n)))
38 ;;;; (let ((tail (buffer-tail buffer)))
40 ;;;; (incf (buffer-tail buffer) n))
43 (defstruct (buffer (:constructor
!make-buffer
(sap length
))
45 (sap (missing-arg) :type system-area-pointer
:read-only t
)
46 (length (missing-arg) :type index
:read-only t
)
49 (prev-head 0 :type index
))
50 (declaim (freeze-type buffer
))
52 (define-load-time-global *available-buffers
* ()
53 "List of available buffers.")
55 (defconstant +bytes-per-buffer
+ (* 8 1024)
56 "Default number of bytes per buffer.")
58 (defun alloc-buffer (&optional
(size +bytes-per-buffer
+))
59 (declare (sb-c::tlab
:system
)
60 (inline allocate-system-memory
) ; so the SAP gets heap-consed
61 (inline !make-buffer
))
62 ;; Don't want to allocate & unwind before the finalizer is in place.
64 (let* ((sap (allocate-system-memory size
))
65 (buffer (!make-buffer sap size
)))
66 (when (zerop (sap-int sap
))
67 (error "Could not allocate ~D bytes for buffer." size
))
68 (finalize buffer
(lambda ()
69 (deallocate-system-memory sap size
))
74 (or (and *available-buffers
* (atomic-pop *available-buffers
*))
77 (declaim (inline reset-buffer
))
78 (defun reset-buffer (buffer)
79 (setf (buffer-head buffer
) 0
80 (buffer-tail buffer
) 0)
83 (defun release-buffer (buffer)
84 (declare (sb-c::tlab
:system
))
86 (atomic-push buffer
*available-buffers
*))
89 ;;;; the FD-STREAM structure
91 ;;; Coarsely characterizes the element type of an FD-STREAM w.r.t.
92 ;;; its SUBTYPEP relations to the relevant CHARACTER and
93 ;;; ([UN]SIGNED-BYTE 8) types. This coarse characterization enables
94 ;;; dispatching on the element type as needed by {READ,WRITE}-SEQUENCE
95 ;;; without calling SUBTYPEP.
96 (deftype stream-element-mode
()
97 '(member character unsigned-byte signed-byte
:bivalent
))
100 (:constructor %make-fd-stream
)
101 (:conc-name fd-stream-
)
102 (:predicate fd-stream-p
)
103 (:include ansi-stream
104 ;; FIXME: would a type constraint on IN-BUFFER
105 ;; and/or CIN-BUFFER improve anything?
106 (misc #'fd-stream-misc-routine
))
109 ;; the name of this stream
111 ;; the file this stream is for
113 ;; the backup file namestring for the old file, for :IF-EXISTS
114 ;; :RENAME or :RENAME-AND-DELETE.
115 (original nil
:type
(or simple-string null
))
116 (delete-original nil
) ; for :if-exists :rename-and-delete
117 ;;; the number of bytes per element
118 (element-size 1 :type index
)
119 ;; the type of element being transfered
120 (element-type 'base-char
)
121 ;; coarse characterization of the element type. see description of
122 ;; STREAM-ELEMENT-MODE type.
123 (element-mode :bivalent
:type stream-element-mode
)
124 ;; the Unix file descriptor
125 (fd -
1 :type
#-win32 fixnum
#+win32 sb-vm
:signed-word
)
126 ;; What do we know about the FD?
127 (fd-type :unknown
:type keyword
)
128 ;; controls when the output buffer is flushed
129 (buffering :full
:type
(member :full
:line
:none
))
130 ;; controls whether the input buffer must be cleared before output
131 ;; (must be done for files, not for sockets, pipes and other data
132 ;; sources where input and output aren't related). non-NIL means
133 ;; don't clear input buffer.
135 ;; character position if known -- this may run into bignums, but
136 ;; we probably should flip it into null then for efficiency's sake...
137 (output-column nil
:type
(or (and unsigned-byte
140 ;; T if input is waiting on FD. :EOF if we hit EOF.
141 (listen nil
:type
(member nil t
:eof
))
142 ;; T if serve-event is allowed when this stream blocks
143 (serve-events nil
:type boolean
)
146 (instead (make-array 0 :element-type
'character
:adjustable t
:fill-pointer t
) :type
(array character
(*)))
147 (ibuf nil
:type
(or buffer null
))
148 (eof-forced-p nil
:type
(member t nil
))
151 (obuf nil
:type
(or buffer null
))
153 ;; output flushed, but not written due to non-blocking io?
156 ;; timeout specified for this stream as seconds or NIL if none
157 (timeout nil
:type
(or single-float null
))
158 ;; pathname of the file this stream is opened to (returned by PATHNAME)
159 (pathname nil
:type
(or pathname null
))
160 ;; Not :DEFAULT, because we want to match CHAR-SIZE!
161 (external-format :latin-1
)
162 ;; fixed width, or function to call with a character
163 (char-size 1 :type
(or fixnum function
))
164 (replacement nil
:type
(or null character string
(simple-array (unsigned-byte 8) 1)))
165 (output-bytes #'ill-out
:type function
))
167 (defun fd-stream-bivalent-p (stream)
168 (eq (fd-stream-element-mode stream
) :bivalent
))
170 (defmethod print-object ((fd-stream fd-stream
) stream
)
171 (declare (type stream stream
))
172 (print-unreadable-object (fd-stream stream
:type t
:identity t
)
173 (format stream
"for ~S" (fd-stream-name fd-stream
))))
175 ;;; Release all of FD-STREAM's buffers. Originally the intent of this
176 ;;; was to grab a mutex once only, but the buffer pool is lock-free now.
177 (defun release-fd-stream-buffers (fd-stream)
178 (awhen (fd-stream-ibuf fd-stream
)
179 (setf (fd-stream-ibuf fd-stream
) nil
)
181 (awhen (fd-stream-obuf fd-stream
)
182 (setf (fd-stream-obuf fd-stream
) nil
)
184 (dolist (buf (fd-stream-output-queue fd-stream
))
186 (release-buffer buf
)))
187 (setf (fd-stream-output-queue fd-stream
) nil
))
189 ;;;; FORM-TRACKING-STREAM
191 ;; The compiler uses this to record for each input subform the start and
192 ;; end positions as character offsets. Measuring in characters rather than
193 ;; bytes is both better suited to the task - in that it is consistent with
194 ;; reporting of "point" by Emacs - and faster than querying FILE-POSITION.
195 ;; The slowness of FILE-POSITION on an FD-STREAM is due to making a system
196 ;; call every time, to ensure that the case of an append-only stream works
197 ;; correctly (where the OS forces all writes to the end), and other factors.
199 (defstruct (form-tracking-stream
200 (:constructor %make-form-tracking-stream
)
202 (misc #'tracking-stream-misc
)
205 ;; a function which is called for events on this stream.
206 (observer (lambda (x y z
) (declare (ignore x y z
))) :type function
)
207 ;; A vector of the character position of each #\Newline seen
208 (newlines (make-array 10 :fill-pointer
0 :adjustable t
))
209 (last-newline -
1 :type index-or-minus-1
)
210 ;; Better than reporting that a reader error occurred at a position
211 ;; before any whitespace (or equivalently, a macro producing no value),
212 ;; we can note the position at the first "good" character.
213 (form-start-byte-pos)
214 (form-start-char-pos))
216 (defun line/col-from-charpos
217 (stream &optional
(charpos (ansi-stream-input-char-pos stream
)))
218 (let ((newlines (form-tracking-stream-newlines stream
)))
220 (let ((index (position charpos newlines
:test
#'>= :from-end t
)))
221 ;; Line numbers traditionally begin at 1, columns at 0.
223 ;; INDEX is 1 less than the number of newlines seen
224 ;; up to and including this startpos.
225 ;; e.g. index=0 => 1 newline seen => line=2
227 ;; 1 char after the newline = column 0
228 (- charpos
(aref newlines index
) 1))
229 ;; zero newlines were seen
231 ;; No charpos means the error is before reading the first char
232 ;; e.g. an encoding error. Take the last Newline.
233 (cons (1+ (length newlines
)) 0))))
235 ;;;; CORE OUTPUT FUNCTIONS
237 ;;; Buffer the section of THING delimited by START and END by copying
238 ;;; to output buffer(s) of stream.
239 (defun buffer-output (stream thing start end
)
240 (declare (index start end
))
242 (error ":END before :START!"))
244 ;; Copy bytes from THING to buffers.
245 (flet ((copy-to-buffer (buffer tail count
)
246 (declare (buffer buffer
) (index tail count
))
248 (let ((sap (buffer-sap buffer
)))
251 (system-area-ub8-copy thing start sap tail count
))
252 ((simple-unboxed-array (*))
253 (copy-ub8-to-system-area thing start sap tail count
))))
254 ;; Not INCF! If another thread has moved tail from under
255 ;; us, we don't want to accidentally increment tail
256 ;; beyond buffer-length.
257 (setf (buffer-tail buffer
) (+ count tail
))
260 ;; First copy is special: the buffer may already contain
261 ;; something, or be even full.
262 (let* ((obuf (fd-stream-obuf stream
))
263 (tail (buffer-tail obuf
))
264 (buffer-length (buffer-length obuf
))
265 (space (- buffer-length tail
))
266 (length (- end start
)))
267 (cond ((and (not (fd-stream-serve-events stream
))
268 (>= length buffer-length
))
269 (flush-output-buffer stream
)
270 (finish-writing-sequence thing stream start end
)
271 (return-from buffer-output
))
273 (copy-to-buffer obuf tail
(min space length
))
274 (go :more-output-p
))))
276 ;; Later copies should always have an empty buffer, since
277 ;; they are freshly flushed, but if another thread is
278 ;; stomping on the same buffer that might not be the case.
279 (let* ((obuf (flush-output-buffer stream
))
280 (tail (buffer-tail obuf
))
281 (space (- (buffer-length obuf
) tail
)))
282 (copy-to-buffer obuf tail
(min space
(- end start
))))
285 (go :flush-and-fill
))))))
287 (define-symbol-macro +write-failed
+ "Couldn't write to ~S")
289 ;;; Flush the current output buffer of the stream, ensuring that the
290 ;;; new buffer is empty. Returns (for convenience) the new output
291 ;;; buffer -- which may or may not be EQ to the old one. If the is no
292 ;;; queued output we try to write the buffer immediately -- otherwise
293 ;;; we queue it for later.
294 (defun flush-output-buffer (stream)
295 (let ((obuf (fd-stream-obuf stream
)))
297 (let ((head (buffer-head obuf
))
298 (tail (buffer-tail obuf
)))
299 (cond ((eql head tail
)
300 ;; Buffer is already empty -- just ensure that is is
301 ;; set to zero as well.
303 ((fd-stream-output-queue stream
)
304 ;; There is already stuff on the queue -- go directly
307 (%queue-and-replace-output-buffer stream
))
309 ;; Try a non-blocking write, if SERVE-EVENT is allowed, queue
310 ;; whatever is left over. Otherwise wait until we can write.
312 (synchronize-stream-output stream
)
314 (let ((length (- tail head
)))
315 (multiple-value-bind (count errno
)
316 (sb-unix:unix-write
(fd-stream-fd stream
) (buffer-sap obuf
)
318 (flet ((queue-or-wait ()
319 (if (fd-stream-serve-events stream
)
320 (return (%queue-and-replace-output-buffer stream
))
321 (or (wait-until-fd-usable (fd-stream-fd stream
) :output
322 (fd-stream-timeout stream
)
324 (signal-timeout 'io-timeout
327 :seconds
(fd-stream-timeout stream
))))))
328 (cond ((eql count length
)
329 ;; Complete write -- we can use the same buffer.
330 (return (reset-buffer obuf
)))
332 ;; Partial write -- update buffer status and
335 (setf (buffer-head obuf
) head
)
338 ((eql errno sb-unix
:ewouldblock
)
339 ;; Blocking, queue or wair.
341 ;; if interrupted on win32, just try again
342 #+win32
((eql errno sb-unix
:eintr
))
344 (simple-stream-perror +write-failed
+
345 stream errno
)))))))))))))
347 (defun finish-writing-sequence (sequence stream start end
)
348 (synchronize-stream-output stream
)
350 (let ((length (- end start
)))
351 (multiple-value-bind (count errno
)
352 (sb-unix:unix-write
(fd-stream-fd stream
) sequence start length
)
354 (or (wait-until-fd-usable (fd-stream-fd stream
) :output
355 (fd-stream-timeout stream
)
357 (signal-timeout 'io-timeout
360 :seconds
(fd-stream-timeout stream
)))))
361 (cond ((eql count length
)
367 ((eql errno sb-unix
:ewouldblock
)
369 ;; if interrupted on win32, just try again
370 #+win32
((eql errno sb-unix
:eintr
))
372 (simple-stream-perror +write-failed
+
375 ;;; Helper for FLUSH-OUTPUT-BUFFER -- returns the new buffer.
376 (defun %queue-and-replace-output-buffer
(stream)
377 (aver (fd-stream-serve-events stream
))
378 (let ((queue (fd-stream-output-queue stream
))
379 (later (list (or (fd-stream-obuf stream
) (bug "Missing obuf."))))
381 ;; Important: before putting the buffer on queue, give the stream
382 ;; a new one. If we get an interrupt and unwind losing the buffer
383 ;; is relatively OK, but having the same buffer in two places
385 (setf (fd-stream-obuf stream
) new
)
389 (setf (fd-stream-output-queue stream
) later
)))
390 (unless (fd-stream-handler stream
)
391 (setf (fd-stream-handler stream
)
392 (add-fd-handler (fd-stream-fd stream
)
395 (declare (ignore fd
))
396 (write-output-from-queue stream
)))))
399 ;;; This is called by the FD-HANDLER for the stream when output is
401 (defun write-output-from-queue (stream)
402 (aver (fd-stream-serve-events stream
))
403 (synchronize-stream-output stream
)
407 (let* ((buffer (pop (fd-stream-output-queue stream
)))
408 (head (buffer-head buffer
))
409 (length (- (buffer-tail buffer
) head
)))
410 (declare (index head length
))
412 (multiple-value-bind (count errno
)
413 (sb-unix:unix-write
(fd-stream-fd stream
) (buffer-sap buffer
)
415 (cond ((eql count length
)
416 ;; Complete write, see if we can do another right
417 ;; away, or remove the handler if we're done.
418 (release-buffer buffer
)
419 (cond ((fd-stream-output-queue stream
)
423 (let ((handler (fd-stream-handler stream
)))
425 (setf (fd-stream-handler stream
) nil
)
426 (remove-fd-handler handler
)))))
428 ;; Partial write. Update buffer status and requeue.
429 (aver (< count length
))
430 ;; Do not use INCF! Another thread might have moved head.
431 (setf (buffer-head buffer
) (+ head count
))
432 (push buffer
(fd-stream-output-queue stream
)))
434 ;; We tried to do multiple writes, and finally our
435 ;; luck ran out. Requeue.
436 (push buffer
(fd-stream-output-queue stream
)))
438 ;; Could not write on the first try at all!
440 (simple-stream-perror +write-failed
+ stream errno
)
442 (if (= errno sb-unix
:ewouldblock
)
443 (bug "Unexpected blocking in WRITE-OUTPUT-FROM-QUEUE.")
444 (simple-stream-perror +write-failed
+
448 ;;; Try to write THING directly to STREAM without buffering, if
449 ;;; possible. If direct write doesn't happen, buffer.
450 (defun write-or-buffer-output (stream thing start end
)
451 (declare (index start end
))
452 (cond ((fd-stream-output-queue stream
)
453 (buffer-output stream thing start end
))
455 (error ":END before :START!"))
457 (let ((length (- end start
)))
458 (synchronize-stream-output stream
)
459 (multiple-value-bind (count errno
)
460 (sb-unix:unix-write
(fd-stream-fd stream
) thing start length
)
461 (cond ((eql count length
)
462 ;; Complete write -- done!
465 (aver (< count length
))
466 ;; Partial write -- buffer the rest.
467 (buffer-output stream thing
(+ start count
) end
))
469 ;; Could not write -- buffer or error.
471 (simple-stream-perror +write-failed
+ stream errno
)
473 (if (= errno sb-unix
:ewouldblock
)
474 (buffer-output stream thing start end
)
475 (simple-stream-perror +write-failed
+ stream errno
)))))))))
477 ;;;; output routines and related noise
479 (define-load-time-global *output-routines
* ()
480 "List of all available output routines. Each element is a list of the
481 element-type output, the kind of buffering, the function name, and the number
482 of bytes per element.")
484 (defun stream-errno-to-condition (errno)
486 (#-win32
#.sb-unix
:epipe
487 #+win32
#.sb-win32
::error-no-data
489 (t 'simple-stream-error
)))
491 ;;; common idioms for reporting low-level stream and file problems
492 (define-error-wrapper simple-stream-perror
(format-control stream
&optional errno
&rest format-arguments
)
493 (error (stream-errno-to-condition errno
)
495 :format-control
"~@<~?~@[: ~2I~_~A~]~:>"
496 :format-arguments
(list format-control
497 (list* stream format-arguments
)
498 (when errno
(strerror errno
)))))
500 (define-error-wrapper file-perror
(pathname errno
&optional datum
&rest arguments
)
501 (let ((message (when errno
(strerror errno
))))
502 (multiple-value-bind (condition-type arguments
)
505 (values 'simple-file-error
(list :format-control datum
506 :format-arguments arguments
)))
508 (values datum arguments
)))
509 (apply #'error condition-type
:pathname pathname
:message message
512 (define-error-wrapper c-string-encoding-error
(external-format code
)
513 (error 'c-string-encoding-error
514 :external-format external-format
517 (macrolet ((sap-ref-octets (sap offset count
)
519 (make-array (the fixnum
,count
) :element-type
'(unsigned-byte 8))))
520 (%byte-blt
,sap
,offset .buffer.
0 ,count
)
523 (define-error-wrapper c-string-decoding-error
(external-format sap offset count
)
524 (error 'c-string-decoding-error
525 :external-format external-format
526 :octets
(sap-ref-octets sap offset count
)))
528 ;;; Returning true goes into end of file handling, false will enter another
529 ;;; round of input buffer filling followed by re-entering character decode.
530 (defun stream-decoding-error-and-handle (stream octet-count stream-unit-count
)
531 (let ((external-format (stream-external-format stream
))
532 (replacement (fd-stream-replacement stream
)))
533 (labels ((replacement (thing resyncp
)
534 (let* ((string (decoding-replacement-stringify thing external-format
))
535 (reversed (reverse string
))
536 (instead (fd-stream-instead stream
)))
537 (dotimes (i (length reversed
))
538 (vector-push-extend (char reversed i
) instead
))
539 (when (> (length reversed
) 0)
540 (setf (fd-stream-listen stream
) t
))
545 (fd-stream-resync stream
)
548 (fd-stream-advance stream stream-unit-count
)
551 (replacement replacement nil
)
553 (error 'stream-decoding-error
554 :external-format external-format
556 :octets
(let ((buffer (fd-stream-ibuf stream
)))
557 (sap-ref-octets (buffer-sap buffer
)
561 :report
(lambda (stream)
563 "~@<Attempt to resync the stream at a ~
564 character boundary and continue.~@:>"))
566 (force-end-of-file ()
567 :report
(lambda (stream)
568 (format stream
"~@<Force an end of file.~@:>"))
569 (setf (fd-stream-eof-forced-p stream
) t
))
570 (use-value (replacement)
571 :report
(lambda (stream)
572 (format stream
"~@<Use datum as replacement input ~
574 :interactive
(lambda ()
576 "Replacement byte, bytes, character, or string (evaluated): "))
577 (replacement replacement nil
))
578 (input-replacement (thing)
579 :report
(lambda (stream)
580 (format stream
"~@<Use string as replacement input, ~
581 attempt to resync at a character ~
582 boundary and continue.~@:>"))
583 :interactive
(lambda ()
584 (format *query-io
* "~@<Enter a string: ~@:>")
585 (finish-output *query-io
*)
586 (list (read *query-io
*)))
587 (replacement thing t
)))))))
590 (defun encoding-replacement-adjust-charpos (replacement stream
)
591 (typecase replacement
592 (character (if (char= replacement
#\Newline
)
593 (setf (fd-stream-output-column stream
) 0)
594 (incf (fd-stream-output-column stream
) 1)))
595 (string (let ((newline-pos (position #\Newline replacement
:from-end t
)))
597 (setf (fd-stream-output-column stream
) (- (length replacement
) newline-pos
1))
598 (incf (fd-stream-output-column stream
) (length replacement
)))))
600 ((simple-array (unsigned-byte 8) 1))))
602 (defun stream-encoding-error-and-handle (stream code
)
603 (let ((external-format (stream-external-format stream
))
604 (replacement (fd-stream-replacement stream
)))
605 (labels ((replacement (thing)
606 (let ((octets (encoding-replacement-octetify thing external-format
)))
607 (ecase (fd-stream-buffering stream
)
608 (:full
(buffer-output stream octets
0 (length octets
)))
609 (:line
(buffer-output stream octets
0 (length octets
)))
610 (:none
(write-or-buffer-output stream octets
0 (length octets
))))
611 (encoding-replacement-adjust-charpos thing stream
))))
613 (replacement replacement
)
615 (error 'stream-encoding-error
616 :external-format external-format
620 :report
(lambda (stream)
621 (format stream
"~@<Skip output of this character.~@:>")))
622 (use-value (replacement)
623 :report
(lambda (stream)
624 (format stream
"~@<Use datum as replacement output.~@:>"))
625 :interactive
(lambda ()
627 "Replacement byte, bytes, character, or string (evaluated): "))
628 (replacement replacement
))
629 (output-replacement (string)
630 :report
(lambda (stream)
631 (format stream
"~@<Output replacement string.~@:>"))
632 :interactive
(lambda ()
633 (format *query-io
* "~@<Enter a string: ~@:>")
634 (finish-output *query-io
*)
635 (list (read *query-io
*)))
636 (let ((string (string string
)))
637 (fd-sout stream
(string string
) 0 (length string
)))))))))
639 (defun %external-format-encoding-error
(stream code
)
641 (stream-encoding-error-and-handle stream code
)
642 (c-string-encoding-error stream code
)))
644 (defmacro external-format-encoding-error
(stream code
)
645 `(return-from output-nothing
(%external-format-encoding-error
,stream
,code
)))
647 (defun synchronize-stream-output (stream)
648 ;; If we're reading and writing on the same file, flush buffered
649 ;; input and rewind file position accordingly.
650 (unless (or (fd-stream-dual-channel-p stream
)
652 (eq (fd-stream-in stream
) #'ill-in
)
653 (eq (fd-stream-bin stream
) #'ill-bin
)))
654 (let ((adjust (nth-value 1 (flush-input-buffer stream
))))
655 (unless (eql 0 adjust
)
656 (sb-unix:unix-lseek
(fd-stream-fd stream
) (- adjust
) sb-unix
:l_incr
)))))
658 (defun fd-stream-output-finished-p (stream)
659 (let ((obuf (fd-stream-obuf stream
)))
661 (and (zerop (buffer-tail obuf
))
662 (not (fd-stream-output-queue stream
))))))
664 (defmacro output-wrapper
/variable-width
((stream size buffering restart
)
666 (let ((stream-var '#:stream
))
667 `(let* ((,stream-var
,stream
)
668 (obuf (fd-stream-obuf ,stream-var
))
669 (tail (buffer-tail obuf
))
671 ,@(unless (eq (car buffering
) :none
)
672 `((when (< (buffer-length obuf
) (+ tail size
))
673 (setf obuf
(flush-output-buffer ,stream-var
)
674 tail
(buffer-tail obuf
)))))
675 ,@(unless (eq (car buffering
) :none
)
676 ;; FIXME: Why this here? Doesn't seem necessary.
677 `((synchronize-stream-output ,stream-var
)))
679 `(block output-nothing
681 (setf (buffer-tail obuf
) (+ tail size
)))
684 (setf (buffer-tail obuf
) (+ tail size
))))
685 ,@(ecase (car buffering
)
686 (:none
`((flush-output-buffer ,stream-var
)))
687 (:line
`((when (eql |ch|
#\Newline
)
688 (flush-output-buffer ,stream-var
))))
691 (defmacro output-wrapper
((stream size buffering restart
) &body body
)
692 (let ((stream-var '#:stream
))
693 `(let* ((,stream-var
,stream
)
694 (obuf (fd-stream-obuf ,stream-var
))
695 (tail (buffer-tail obuf
)))
696 ,@(unless (eq (car buffering
) :none
)
697 `((when (< (buffer-length obuf
) (+ tail
,size
))
698 (setf obuf
(flush-output-buffer ,stream-var
)
699 tail
(buffer-tail obuf
)))))
700 ;; FIXME: Why this here? Doesn't seem necessary.
701 ,@(unless (eq (car buffering
) :none
)
702 `((synchronize-stream-output ,stream-var
)))
704 `(block output-nothing
706 (setf (buffer-tail obuf
) (+ tail
,size
)))
709 (setf (buffer-tail obuf
) (+ tail
,size
))))
710 ,@(ecase (car buffering
)
711 (:none
`((flush-output-buffer ,stream-var
)))
712 (:line
`((when (eql |ch|
#\Newline
)
713 (flush-output-buffer ,stream-var
))))
716 (defmacro def-output-routines
/variable-width
717 ((name-fmt size restart external-format
&rest bufferings
)
723 (intern (format nil name-fmt
(string (car buffering
))))))
725 `(defun ,function
(stream |ch|
)
726 (declare (optimize (sb-c:verify-arg-count
0)))
727 (output-wrapper/variable-width
(stream ,size
,buffering
,restart
)
729 ;; return char so WRITE-CHAR can tail-call the stream's output method
731 ;; FIXME: technically illegal use of quoted constant, as we keep NCONCing
732 ;; onto the tail of a literal
733 `(setf *output-routines
*
734 (nconc *output-routines
*
742 (cdr buffering
)))))))
745 ;;; Define output routines that output numbers SIZE bytes long for the
746 ;;; given bufferings. Use BODY to do the actual output.
747 (defmacro def-output-routines
((name-fmt size restart
&rest bufferings
)
753 (intern (format nil name-fmt
(string (car buffering
))))))
755 `(defun ,function
(stream byte
)
756 (declare (optimize (sb-c:verify-arg-count
0)))
757 (output-wrapper (stream ,size
,buffering
,restart
)
759 ;; return byte so WRITE-BYTE can tail-call the stream's output method
761 `(setf *output-routines
*
762 (nconc *output-routines
*
770 (cdr buffering
)))))))
773 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
776 (:none
(unsigned-byte 8))
777 (:full
(unsigned-byte 8)))
778 (setf (sap-ref-8 (buffer-sap obuf
) tail
)
781 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
784 (:none
(signed-byte 8))
785 (:full
(signed-byte 8)))
786 (setf (signed-sap-ref-8 (buffer-sap obuf
) tail
)
789 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
792 (:none
(unsigned-byte 16))
793 (:full
(unsigned-byte 16)))
794 (setf (sap-ref-16 (buffer-sap obuf
) tail
)
797 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
800 (:none
(signed-byte 16))
801 (:full
(signed-byte 16)))
802 (setf (signed-sap-ref-16 (buffer-sap obuf
) tail
)
805 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
808 (:none
(unsigned-byte 32))
809 (:full
(unsigned-byte 32)))
810 (setf (sap-ref-32 (buffer-sap obuf
) tail
)
813 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
816 (:none
(signed-byte 32))
817 (:full
(signed-byte 32)))
818 (setf (signed-sap-ref-32 (buffer-sap obuf
) tail
)
823 (def-output-routines ("OUTPUT-UNSIGNED-LONG-LONG-~A-BUFFERED"
826 (:none
(unsigned-byte 64))
827 (:full
(unsigned-byte 64)))
828 (setf (sap-ref-64 (buffer-sap obuf
) tail
)
830 (def-output-routines ("OUTPUT-SIGNED-LONG-LONG-~A-BUFFERED"
833 (:none
(signed-byte 64))
834 (:full
(signed-byte 64)))
835 (setf (signed-sap-ref-64 (buffer-sap obuf
) tail
)
838 ;;; the routine to use to output a string. If the stream is
839 ;;; unbuffered, slam the string down the file descriptor, otherwise
840 ;;; use BUFFER-OUTPUT to buffer the string. Update charpos by
841 ;;; checking to see where the last newline was.
842 (defun fd-sout (stream thing start end
)
843 (declare (type fd-stream stream
) (type string thing
))
844 (let ((start (or start
0))
845 (end (or end
(length (the vector thing
)))))
846 (declare (fixnum start end
))
848 (string-dispatch (simple-base-string
850 (simple-array character
(*))
853 (position #\newline thing
:from-end t
854 :start start
:end end
))))
855 (if (and (typep thing
'base-string
)
856 (let ((external-format (fd-stream-external-format stream
)))
857 (and (eq (external-format-keyword external-format
) :latin-1
)
858 (or (null last-newline
)
859 (eq (external-format-newline-variant external-format
) :lf
)))))
860 (ecase (fd-stream-buffering stream
)
862 (buffer-output stream thing start end
))
864 (buffer-output stream thing start end
)
866 (flush-output-buffer stream
)))
868 (write-or-buffer-output stream thing start end
)))
869 (ecase (fd-stream-buffering stream
)
870 (:full
(funcall (fd-stream-output-bytes stream
)
871 stream thing nil start end
))
872 (:line
(funcall (fd-stream-output-bytes stream
)
873 stream thing last-newline start end
))
874 (:none
(funcall (fd-stream-output-bytes stream
)
875 stream thing t start end
))))
877 (setf (fd-stream-output-column stream
) (- end last-newline
1))
878 (incf (fd-stream-output-column stream
) (- end start
))))))
880 (defstruct (external-format
881 (:constructor %make-external-format
)
883 (:predicate external-format-p
)
884 (:copier %copy-external-format
))
885 ;; All the names that can refer to this external format. The first
886 ;; one is the canonical name.
887 (names (missing-arg) :type list
:read-only t
)
888 (newline-variant (missing-arg) :type
(member :crlf
:lf
:cr
) :read-only t
)
889 (default-replacement-character (missing-arg) :type character
)
890 (replacement nil
:type
(or null character string
(unsigned-byte 8) (simple-array (unsigned-byte 8) 1)))
891 (read-n-chars-fun (missing-arg) :type function
)
892 (read-char-fun (missing-arg) :type function
)
893 (write-n-bytes-fun (missing-arg) :type function
)
894 (write-char-none-buffered-fun (missing-arg) :type function
)
895 (write-char-line-buffered-fun (missing-arg) :type function
)
896 (write-char-full-buffered-fun (missing-arg) :type function
)
897 ;; Can be nil for fixed-width formats.
898 (resync-fun nil
:type
(or function null
))
899 (bytes-for-char-fun (missing-arg) :type function
)
900 (read-c-string-fun (missing-arg) :type function
)
901 (write-c-string-fun (missing-arg) :type function
)
902 (octets-to-string-fun (missing-arg) :type function
)
903 (string-to-octets-fun (missing-arg) :type function
))
904 (declaim (freeze-type external-format
))
906 (defun ef-char-size (ef-entry)
907 (if (variable-width-external-format-p ef-entry
)
908 (bytes-for-char-fun ef-entry
)
909 (funcall (bytes-for-char-fun ef-entry
) #\x
)))
911 (defun sb-alien::string-to-c-string
(string external-format
)
912 (declare (type simple-string string
)
913 (explicit-check :result
))
915 (declare (optimize (speed 3) (safety 0)))
916 (let ((external-format (get-external-format-or-lose external-format
)))
917 (funcall (ef-write-c-string-fun external-format
) string
))))
919 (defun sb-alien::c-string-to-string
(sap external-format element-type
)
920 (declare (type system-area-pointer sap
)
921 (explicit-check :result
))
923 (declare (optimize (speed 3) (safety 0)))
924 (let ((external-format (get-external-format-or-lose external-format
)))
925 (funcall (ef-read-c-string-fun external-format
) sap element-type
))))
927 (defun get-external-format-or-lose (external-format)
928 (or (get-external-format external-format
)
929 (error "Undefined external-format: ~S" external-format
)))
931 (defun external-format-keyword (external-format)
932 (typecase external-format
933 (keyword external-format
)
934 ((cons keyword
) (car external-format
))))
936 (defun external-format-newline-variant (external-format)
937 (typecase external-format
939 ((cons keyword
) (getf (cdr external-format
) :newline
:lf
))))
941 (defun canonize-external-format (external-format entry
)
942 (typecase external-format
943 (keyword (first (ef-names entry
)))
944 ((cons keyword
) (cons (first (ef-names entry
)) (rest external-format
)))))
946 ;;; Find an output routine to use given the type and buffering. Return
947 ;;; as multiple values the routine, the real type transfered, and the
948 ;;; number of bytes per element.
949 (defun pick-output-routine (type buffering
&optional entry
)
950 (when (subtypep type
'character
)
951 (return-from pick-output-routine
952 (values (ecase buffering
953 (:none
(ef-write-char-none-buffered-fun entry
))
954 (:line
(ef-write-char-line-buffered-fun entry
))
955 (:full
(ef-write-char-full-buffered-fun entry
)))
958 (ef-write-n-bytes-fun entry
)
960 (ef-replacement entry
))))
961 (dolist (entry *output-routines
*)
962 (when (and (subtypep type
(first entry
))
963 (eq buffering
(second entry
))
965 (return-from pick-output-routine
966 (values (symbol-function (third entry
))
969 ;; KLUDGE: dealing with the buffering here leads to excessive code
972 ;; KLUDGE: also see comments in PICK-INPUT-ROUTINE
973 (loop for i from
40 by
8 to
1024 ; ARB (KLUDGE)
974 if
(subtypep type
`(unsigned-byte ,i
))
975 do
(return-from pick-output-routine
979 (lambda (stream byte
)
980 (output-wrapper (stream (/ i
8) (:none
) nil
)
981 (loop for j from
0 below
(/ i
8)
982 do
(setf (sap-ref-8 (buffer-sap obuf
)
984 (ldb (byte 8 (- i
8 (* j
8))) byte
))))))
986 (lambda (stream byte
)
987 (output-wrapper (stream (/ i
8) (:full
) nil
)
988 (loop for j from
0 below
(/ i
8)
989 do
(setf (sap-ref-8 (buffer-sap obuf
)
991 (ldb (byte 8 (- i
8 (* j
8))) byte
)))))))
994 (loop for i from
40 by
8 to
1024 ; ARB (KLUDGE)
995 if
(subtypep type
`(signed-byte ,i
))
996 do
(return-from pick-output-routine
1000 (lambda (stream byte
)
1001 (output-wrapper (stream (/ i
8) (:none
) nil
)
1002 (loop for j from
0 below
(/ i
8)
1003 do
(setf (sap-ref-8 (buffer-sap obuf
)
1005 (ldb (byte 8 (- i
8 (* j
8))) byte
))))))
1007 (lambda (stream byte
)
1008 (output-wrapper (stream (/ i
8) (:full
) nil
)
1009 (loop for j from
0 below
(/ i
8)
1010 do
(setf (sap-ref-8 (buffer-sap obuf
)
1012 (ldb (byte 8 (- i
8 (* j
8))) byte
)))))))
1016 ;;;; input routines and related noise
1018 ;;; a list of all available input routines. Each element is a list of
1019 ;;; the element-type input, the function name, and the number of bytes
1021 (define-load-time-global *input-routines
* ())
1023 ;;; Return whether a primitive partial read operation on STREAM's FD
1024 ;;; would (probably) block. Signal a `simple-stream-error' if the
1025 ;;; system call implementing this operation fails.
1027 ;;; It is "may" instead of "would" because "would" is not quite
1028 ;;; correct on win32. However, none of the places that use it require
1029 ;;; further assurance than "may" versus "will definitely not".
1030 (defun sysread-may-block-p (stream)
1032 ;; This answers T at EOF on win32.
1033 (not (sb-win32:handle-listen
(fd-stream-fd stream
)))
1035 (not (sb-unix:unix-simple-poll
(fd-stream-fd stream
) :input
0)))
1037 ;;; If the read would block wait (using SERVE-EVENT) till input is available,
1038 ;;; then fill the input buffer, and return the number of bytes read. Throws
1039 ;;; to EOF-INPUT-CATCHER if the eof was reached.
1040 (defun refill-input-buffer (stream)
1041 (let ((fd (fd-stream-fd stream
))
1048 ;; Check for blocking input before touching the stream if we are to
1049 ;; serve events: if the FD is blocking, we don't want to try an uninterruptible
1050 ;; read(). Regular files should never block, so we can elide the check.
1051 (if (and (neq :regular
(fd-stream-fd-type stream
))
1052 (sysread-may-block-p stream
))
1053 (go :wait-for-input
)
1055 ;; These (:CLOSED-FLAME and :READ-ERROR) tags are here so what
1056 ;; we can signal errors outside the WITHOUT-INTERRUPTS.
1058 (closed-flame stream
)
1060 (simple-stream-perror "couldn't read from ~S" stream errno
)
1062 ;; This tag is here so we can unwind outside the WITHOUT-INTERRUPTS
1063 ;; to wait for input if read tells us EWOULDBLOCK.
1064 (unless (wait-until-fd-usable fd
:input
(fd-stream-timeout stream
)
1065 (fd-stream-serve-events stream
))
1066 (signal-timeout 'io-timeout
1069 :seconds
(fd-stream-timeout stream
)))
1071 ;; Since the read should not block, we'll disable the
1072 ;; interrupts here, so that we don't accidentally unwind and
1073 ;; leave the stream in an inconsistent state.
1075 ;; Execute the nlx outside without-interrupts to ensure the
1076 ;; resulting thunk is stack-allocatable.
1077 ((lambda (return-reason)
1078 (ecase return-reason
1079 ((nil)) ; fast path normal cases
1080 ((:wait-for-input
) (go #-win32
:wait-for-input
#+win32
:main
))
1081 ((:closed-flame
) (go :closed-flame
))
1082 ((:read-error
) (go :read-error
))))
1084 ;; Check the buffer: if it is null, then someone has closed
1085 ;; the stream from underneath us. This is not ment to fix
1086 ;; multithreaded races, but to deal with interrupt handlers
1087 ;; closing the stream.
1090 (let* ((ibuf (or (fd-stream-ibuf stream
) (return :closed-flame
)))
1091 (sap (buffer-sap ibuf
))
1092 (length (buffer-length ibuf
))
1093 (head (buffer-head ibuf
))
1094 (tail (buffer-tail ibuf
)))
1095 (declare (index length head tail
)
1096 (inline sb-unix
:unix-read
))
1097 (unless (zerop head
)
1098 (cond ((eql head tail
)
1099 ;; Buffer is empty, but not at yet reset -- make it so.
1102 (reset-buffer ibuf
))
1104 ;; Buffer has things in it, but they are not at the
1105 ;; head -- move them there.
1106 (let ((n (- tail head
)))
1107 (system-area-ub8-copy sap head sap
0 n
)
1109 (buffer-head ibuf
) head
1111 (buffer-tail ibuf
) tail
)))))
1112 (setf (fd-stream-listen stream
) nil
)
1113 (setf (values count errno
)
1114 (sb-unix:unix-read fd
(sap+ sap tail
) (- length tail
)))
1117 #+win32 sb-unix
:eintr
1118 #-win32 sb-unix
:ewouldblock
)
1119 (return :wait-for-input
)
1120 (return :read-error
)))
1122 (setf (fd-stream-listen stream
) :eof
)
1123 (/show0
"THROWing EOF-INPUT-CATCHER")
1124 (throw 'eof-input-catcher nil
))
1126 ;; Success! (Do not use INCF, for sake of other threads.)
1127 (setf (buffer-tail ibuf
) (+ count tail
))))))))))
1130 ;;; Make sure there are at least BYTES number of bytes in the input
1131 ;;; buffer. Keep calling REFILL-INPUT-BUFFER until that condition is met.
1132 (defmacro input-at-least
(stream bytes
)
1133 (let ((stream-var (gensym "STREAM"))
1134 (bytes-var (gensym "BYTES"))
1135 (buffer-var (gensym "IBUF")))
1136 `(let* ((,stream-var
,stream
)
1138 (,buffer-var
(fd-stream-ibuf ,stream-var
)))
1140 (when (>= (- (buffer-tail ,buffer-var
)
1141 (buffer-head ,buffer-var
))
1144 (refill-input-buffer ,stream-var
)))))
1146 (defmacro input-wrapper
/variable-width
((stream bytes eof-error eof-value
)
1148 (let ((stream-var (gensym "STREAM"))
1149 (retry-var (gensym "RETRY"))
1150 (element-var (gensym "ELT")))
1151 `(let* ((,stream-var
,stream
)
1152 (ibuf (fd-stream-ibuf ,stream-var
))
1154 (unit ,(if (consp bytes
)
1155 (let ((size-info (car bytes
)))
1156 (if (consp size-info
)
1161 (when (fd-stream-eof-forced-p ,stream-var
)
1162 (setf (fd-stream-eof-forced-p ,stream-var
) nil
)
1163 (return-from use-instead
1164 (eof-or-lose ,stream-var
,eof-error
,eof-value
)))
1165 (let ((,element-var nil
)
1166 (decode-break-reason nil
))
1167 (do ((,retry-var t
))
1169 (if (> (length (fd-stream-instead ,stream-var
)) 0)
1170 (let* ((instead (fd-stream-instead ,stream-var
))
1171 (result (vector-pop instead
))
1172 (pointer (fill-pointer instead
)))
1174 (setf (fd-stream-listen ,stream-var
) nil
))
1175 (setf (buffer-prev-head ibuf
) (buffer-head ibuf
))
1176 (return-from use-instead
(values result
0)))
1178 (catch 'eof-input-catcher
1179 (setf decode-break-reason
1180 (block decode-break-reason
1182 (let ((size-info (car bytes
)))
1183 (if (consp size-info
)
1185 (input-at-least ,stream-var
,(cadr size-info
))
1186 (catch 'eof-input-catcher
1187 (input-at-least ,stream-var
,(car size-info
))))
1188 `(input-at-least ,stream-var
,size-info
)))
1189 `(input-at-least ,stream-var
(setq size
,bytes
)))
1190 (let* ((byte (sap-ref-8 (buffer-sap ibuf
) (buffer-head ibuf
))))
1191 (declare (ignorable byte
))
1192 ,@(when (consp bytes
)
1193 `((let ((sap (buffer-sap ibuf
))
1194 (head (buffer-head ibuf
))
1195 (tail (buffer-tail ibuf
)))
1196 (declare (ignorable sap head tail
))
1197 (setq size
,(cadr bytes
))
1198 (input-at-least ,stream-var size
))))
1199 (setq ,element-var
(locally ,@read-forms
))
1200 (setq ,retry-var nil
))
1202 (when decode-break-reason
1203 (when (stream-decoding-error-and-handle
1204 stream decode-break-reason unit
)
1205 (setq ,retry-var nil
)
1206 (throw 'eof-input-catcher nil
)))
1208 (let ((octet-count (- (buffer-tail ibuf
)
1209 (buffer-head ibuf
))))
1210 (when (or (zerop octet-count
)
1211 (and (not ,element-var
)
1212 (not decode-break-reason
)
1213 (stream-decoding-error-and-handle
1214 stream octet-count unit
)))
1215 (setq ,retry-var nil
))))))
1217 (setf (buffer-prev-head ibuf
) (buffer-head ibuf
))
1218 (incf (buffer-head ibuf
) size
)
1219 (values ,element-var size
))
1221 (eof-or-lose ,stream-var
,eof-error
,eof-value
))))))))
1223 ;;; a macro to wrap around all input routines to handle EOF-ERROR noise
1224 (defmacro input-wrapper
((stream bytes eof-error eof-value
) &body read-forms
)
1225 (let ((stream-var (gensym "STREAM"))
1226 (element-var (gensym "ELT")))
1227 `(let* ((,stream-var
,stream
)
1228 (ibuf (fd-stream-ibuf ,stream-var
)))
1229 (if (> (length (fd-stream-instead ,stream-var
)) 0)
1230 (bug "INSTEAD not empty in INPUT-WRAPPER for ~S" ,stream-var
)
1232 (catch 'eof-input-catcher
1233 (input-at-least ,stream-var
,bytes
)
1234 (locally ,@read-forms
))))
1236 (incf (buffer-head (fd-stream-ibuf ,stream-var
)) ,bytes
)
1239 (eof-or-lose ,stream-var
,eof-error
,eof-value
))))))))
1241 (defmacro def-input-routine
/variable-width
(name
1242 (type external-format size sap head
)
1245 (defun ,name
(stream eof-error eof-value
)
1246 (declare (optimize (sb-c:verify-arg-count
0)))
1247 (input-wrapper/variable-width
(stream ,size eof-error eof-value
)
1248 (let ((,sap
(buffer-sap ibuf
))
1249 (,head
(buffer-head ibuf
)))
1251 (setf *input-routines
*
1252 (nconc *input-routines
*
1253 (list (list ',type
',name
1 ',external-format
))))))
1255 (defmacro def-input-routine
(name
1256 (type size sap head
)
1259 (defun ,name
(stream eof-error eof-value
)
1260 (declare (optimize (sb-c:verify-arg-count
0)))
1261 (input-wrapper (stream ,size eof-error eof-value
)
1262 (let ((,sap
(buffer-sap ibuf
))
1263 (,head
(buffer-head ibuf
)))
1265 (setf *input-routines
*
1266 (nconc *input-routines
*
1267 (list (list ',type
',name
',size nil
))))))
1269 ;;; STREAM-IN routine for reading a string char
1270 (def-input-routine input-character
1271 (character 1 sap head
)
1272 (code-char (sap-ref-8 sap head
)))
1274 ;;; STREAM-IN routine for reading an unsigned 8 bit number
1275 (def-input-routine input-unsigned-8bit-byte
1276 ((unsigned-byte 8) 1 sap head
)
1277 (sap-ref-8 sap head
))
1279 ;;; STREAM-IN routine for reading a signed 8 bit number
1280 (def-input-routine input-signed-8bit-number
1281 ((signed-byte 8) 1 sap head
)
1282 (signed-sap-ref-8 sap head
))
1284 ;;; STREAM-IN routine for reading an unsigned 16 bit number
1285 (def-input-routine input-unsigned-16bit-byte
1286 ((unsigned-byte 16) 2 sap head
)
1287 (sap-ref-16 sap head
))
1289 ;;; STREAM-IN routine for reading a signed 16 bit number
1290 (def-input-routine input-signed-16bit-byte
1291 ((signed-byte 16) 2 sap head
)
1292 (signed-sap-ref-16 sap head
))
1294 ;;; STREAM-IN routine for reading a unsigned 32 bit number
1295 (def-input-routine input-unsigned-32bit-byte
1296 ((unsigned-byte 32) 4 sap head
)
1297 (sap-ref-32 sap head
))
1299 ;;; STREAM-IN routine for reading a signed 32 bit number
1300 (def-input-routine input-signed-32bit-byte
1301 ((signed-byte 32) 4 sap head
)
1302 (signed-sap-ref-32 sap head
))
1306 (def-input-routine input-unsigned-64bit-byte
1307 ((unsigned-byte 64) 8 sap head
)
1308 (sap-ref-64 sap head
))
1309 (def-input-routine input-signed-64bit-byte
1310 ((signed-byte 64) 8 sap head
)
1311 (signed-sap-ref-64 sap head
)))
1313 ;;; Find an input routine to use given the type. Return as multiple
1314 ;;; values the routine, the real type transfered, and the number of
1315 ;;; bytes per element (and for character types string input routine).
1316 (defun pick-input-routine (type &optional entry
)
1317 (when (subtypep type
'character
)
1318 (return-from pick-input-routine
1319 (values (ef-read-char-fun entry
)
1322 (ef-read-n-chars-fun entry
)
1323 (ef-char-size entry
)
1324 (ef-replacement entry
))))
1325 (dolist (entry *input-routines
*)
1326 (when (and (subtypep type
(first entry
))
1327 (not (fourth entry
)))
1328 (return-from pick-input-routine
1329 (values (symbol-function (second entry
))
1332 ;; FIXME: let's do it the hard way, then (but ignore things like
1333 ;; endianness, efficiency, and the necessary coupling between these
1334 ;; and the output routines). -- CSR, 2004-02-09
1335 (loop for i from
40 by
8 to
1024 ; ARB (well, KLUDGE really)
1336 if
(subtypep type
`(unsigned-byte ,i
))
1337 do
(return-from pick-input-routine
1339 (lambda (stream eof-error eof-value
)
1340 (input-wrapper (stream (/ i
8) eof-error eof-value
)
1341 (let ((sap (buffer-sap ibuf
))
1342 (head (buffer-head ibuf
)))
1343 (loop for j from
0 below
(/ i
8)
1347 (sap-ref-8 sap
(+ head j
))))
1348 finally
(return result
)))))
1351 (loop for i from
40 by
8 to
1024 ; ARB (well, KLUDGE really)
1352 if
(subtypep type
`(signed-byte ,i
))
1353 do
(return-from pick-input-routine
1355 (lambda (stream eof-error eof-value
)
1356 (input-wrapper (stream (/ i
8) eof-error eof-value
)
1357 (let ((sap (buffer-sap ibuf
))
1358 (head (buffer-head ibuf
)))
1359 (loop for j from
0 below
(/ i
8)
1363 (sap-ref-8 sap
(+ head j
))))
1364 finally
(return (if (logbitp (1- i
) result
)
1365 (dpb result
(byte i
0) -
1)
1370 ;;; the N-BIN method for binary FD-STREAMs
1372 ;;; Note that this blocks in UNIX-READ. It is generally used where
1373 ;;; there is a definite amount of reading to be done, so blocking
1374 ;;; isn't too problematical.
1375 (defun fd-stream-read-n-bytes (stream buffer sbuffer start requested eof-error-p
1376 &aux
(total-copied 0))
1377 (declare (type fd-stream stream
))
1378 (declare (type index start requested total-copied
))
1379 (declare (ignore sbuffer
))
1380 (aver (= (length (fd-stream-instead stream
)) 0))
1383 (let* ((remaining-request (- requested total-copied
))
1384 (ibuf (fd-stream-ibuf stream
))
1385 (head (buffer-head ibuf
))
1386 (tail (buffer-tail ibuf
))
1387 (available (- tail head
))
1388 (n-this-copy (min remaining-request available
))
1389 (this-start (+ start total-copied
))
1390 (sap (buffer-sap ibuf
)))
1391 (declare (type index remaining-request head tail available
))
1392 (declare (type index n-this-copy
))
1393 ;; Copy data from stream buffer into user's buffer.
1394 (%byte-blt sap head buffer this-start n-this-copy
)
1395 (incf (buffer-head ibuf
) n-this-copy
)
1396 (incf total-copied n-this-copy
)
1397 ;; Maybe we need to refill the stream buffer.
1398 (cond (;; If there were enough data in the stream buffer, we're done.
1399 (eql total-copied requested
)
1400 (return total-copied
))
1401 (;; If EOF, we're done in another way.
1402 (null (catch 'eof-input-catcher
(refill-input-buffer stream
)))
1404 (error 'end-of-file
:stream stream
)
1405 (return total-copied
)))
1406 ;; Otherwise we refilled the stream buffer, so fall
1407 ;; through into another pass of the loop.
1410 (defun fd-stream-advance (stream unit
)
1411 (let* ((buffer (fd-stream-ibuf stream
))
1412 (head (buffer-head buffer
)))
1413 (catch 'eof-input-catcher
1414 (input-at-least stream unit
)
1415 ;; OK: we have another unit
1416 (setf (buffer-prev-head buffer
) head
)
1417 (setf (buffer-head buffer
) (+ head unit
))
1418 (return-from fd-stream-advance nil
))
1419 ;; we do not have enough input: set the HEAD to TAIL and let the next caller
1421 (setf (buffer-head buffer
) (buffer-tail buffer
))
1424 (defun fd-stream-resync (stream)
1425 (let ((entry (get-external-format (fd-stream-external-format stream
))))
1427 (funcall (ef-resync-fun entry
) stream
))))
1429 (defun get-fd-stream-character-sizer (stream)
1430 (let ((entry (get-external-format (fd-stream-external-format stream
))))
1432 (ef-bytes-for-char-fun entry
))))
1434 (defun fd-stream-character-size (stream char
)
1435 (let ((sizer (get-fd-stream-character-sizer stream
)))
1436 (when sizer
(funcall sizer char
))))
1438 (defun fd-stream-string-size (stream string
)
1439 (let ((sizer (get-fd-stream-character-sizer stream
)))
1441 (loop for char across string
1442 for size
= (funcall sizer char
)
1443 when
(null size
) do
(return nil
)
1446 (defun find-external-format (external-format)
1447 (when external-format
1448 (get-external-format external-format
)))
1450 (defun variable-width-external-format-p (ef-entry)
1451 ;; TODO: I'm pretty sure this is always true
1452 (and ef-entry
(not (null (ef-resync-fun ef-entry
)))))
1454 (defun bytes-for-char-fun (ef-entry)
1455 (if ef-entry
(ef-bytes-for-char-fun ef-entry
) (constantly 1)))
1457 (defmacro define-unibyte-mapping-external-format
1458 (canonical-name (&rest other-names
) &body exceptions
)
1459 (let ((->code-name
(symbolicate canonical-name
'-
>code-mapper
))
1460 (code->-name
(symbolicate 'code-
> canonical-name
'-mapper
))
1461 (get-bytes-name (symbolicate 'get- canonical-name
'-bytes
))
1462 (string->-name
(symbolicate 'string-
> canonical-name
))
1463 (string->/cr-name
(symbolicate 'string-
> canonical-name
'/cr
))
1464 (string->/crlf-name
(symbolicate 'string-
> canonical-name
'/crlf
))
1465 (string-name (symbolicate canonical-name
'-
>string
))
1466 (->string-aref-name
(symbolicate canonical-name
'-
>string-aref
))
1467 (->string-aref
/cr-name
(symbolicate canonical-name
'-
>string
/cr-aref
))
1468 (->string-aref
/crlf-name
(symbolicate canonical-name
'-
>string
/crlf-aref
))
1469 (invalid-bytes-p (loop for
(nil code
) in exceptions thereis
(null code
))))
1471 (define-unibyte-mapper ,-
>code-name
,code-
>-name
1473 (define-unibyte-to-octets-functions ,canonical-name
,get-bytes-name
,string-
>-name
,code-
>-name
)
1474 (define-unibyte-to-string-functions ,canonical-name
,string-name
,-
>code-name
,invalid-bytes-p
)
1475 (define-unibyte-external-format-with-newline-variants ,canonical-name
,other-names
1476 (,-
>code-name
,code-
>-name
)
1477 (,-
>string-aref-name
,string-
>-name
)
1478 (,-
>string-aref
/cr-name
,string-
>/cr-name
)
1479 (,-
>string-aref
/crlf-name
,string-
>/crlf-name
)
1480 ,invalid-bytes-p
))))
1482 (defmacro define-unibyte-external-format
1483 (canonical-name (&rest other-names
)
1484 char-encodable-p out-form in-form octets-to-string-symbol string-to-octets-symbol
)
1485 `(define-external-format/variable-width
(,canonical-name
,@other-names
)
1490 ,octets-to-string-symbol
1491 ,string-to-octets-symbol
1492 :char-encodable-p
,char-encodable-p
))
1494 (defmacro define-external-format
/variable-width
1495 (external-format output-restart replacement-character
1496 out-size-expr out-expr in-size-expr in-expr
1497 octets-to-string-sym string-to-octets-sym
1498 &key base-string-direct-mapping
1499 fd-stream-read-n-characters
1500 (newline-variant :lf
)
1501 (char-encodable-p t
))
1502 (let* ((name (first external-format
))
1503 (suffix (symbolicate name
'/ newline-variant
))
1504 (out-function (symbolicate "OUTPUT-BYTES/" suffix
))
1505 (format (format nil
"OUTPUT-CHAR-~A/~A-~~A-BUFFERED" (string name
) newline-variant
))
1506 (in-function (or fd-stream-read-n-characters
1507 (symbolicate "FD-STREAM-READ-N-CHARACTERS/" suffix
)))
1508 (in-char-function (symbolicate "INPUT-CHAR/" suffix
))
1509 (resync-function (symbolicate "RESYNC/" suffix
))
1510 (size-function (symbolicate "BYTES-FOR-CHAR/" suffix
))
1511 (read-c-string-function (symbolicate "READ-FROM-C-STRING/" suffix
))
1512 (output-c-string-function (symbolicate "OUTPUT-TO-C-STRING/" suffix
))
1513 (n-buffer (gensym "BUFFER")))
1515 (defun ,size-function
(|ch|
)
1516 (declare (ignorable |ch|
)
1517 (optimize (sb-c:verify-arg-count
0)))
1518 (and ,char-encodable-p
,out-size-expr
))
1519 (defun ,out-function
(stream string flush-p start end
)
1520 (declare (optimize (sb-c:verify-arg-count
0)))
1521 (let ((start (or start
0))
1522 (end (or end
(length string
))))
1523 (declare (type index start end
))
1524 (synchronize-stream-output stream
)
1525 (unless (<= 0 start end
(length string
))
1526 (sequence-bounding-indices-bad-error string start end
))
1529 (let ((obuf (fd-stream-obuf stream
)))
1530 (string-dispatch (simple-base-string
1531 #+sb-unicode
(simple-array character
(*))
1534 (let ((len (buffer-length obuf
))
1535 (sap (buffer-sap obuf
))
1537 (tail (buffer-tail obuf
)))
1538 (declare (type index tail
)
1539 ;; STRING bounds have already been checked.
1540 (optimize (safety 0)))
1541 (,@(if output-restart
1542 `(block output-nothing
)
1545 ((or (= start end
) (< (- len tail
) 4)))
1546 (let* ((|ch|
(aref string start
))
1547 (bits (char-code |ch|
))
1548 (size ,out-size-expr
))
1549 (declare (ignorable |ch| bits
))
1552 (setf (buffer-tail obuf
) tail
)
1555 ;; Exited via RETURN-FROM OUTPUT-NOTHING: skip the current character.
1559 (flush-output-buffer stream
)))
1561 (flush-output-buffer stream
))))
1562 (def-output-routines/variable-width
(,format
1569 (if (eql |ch|
#\Newline
)
1570 (setf (fd-stream-output-column stream
) 0)
1571 (setf (fd-stream-output-column stream
)
1572 (+ (truly-the unsigned-byte
(fd-stream-output-column stream
)) 1)))
1573 (let ((bits (char-code |ch|
))
1574 (sap (buffer-sap obuf
))
1575 (tail (buffer-tail obuf
)))
1577 ,@(unless fd-stream-read-n-characters
1578 `((defun ,in-function
(stream buffer sbuffer start requested eof-error-p
1579 &aux
(total-copied 0))
1580 (declare (type fd-stream stream
)
1581 (type index start requested total-copied
)
1582 (type ansi-stream-cin-buffer buffer
)
1583 (type ansi-stream-csize-buffer sbuffer
)
1584 (optimize (sb-c:verify-arg-count
0)))
1585 (when (fd-stream-eof-forced-p stream
)
1586 (setf (fd-stream-eof-forced-p stream
) nil
)
1587 (return-from ,in-function
0))
1588 (do ((instead (fd-stream-instead stream
))
1589 (index (+ start total-copied
) (1+ index
)))
1590 ((= (fill-pointer instead
) 0)
1591 (setf (fd-stream-listen stream
) nil
))
1592 (setf (aref buffer index
) (vector-pop instead
))
1593 (setf (aref sbuffer index
) 0)
1595 (when (= requested total-copied
)
1596 (when (= (fill-pointer instead
) 0)
1597 (setf (fd-stream-listen stream
) nil
))
1598 (return-from ,in-function total-copied
)))
1599 (do (;; external formats might wish for e.g. 2 octets
1600 ;; to be available, but still be able to handle a
1601 ;; single octet before end of file. This flag
1602 ;; lets the refilling be tracked so that we know
1603 ;; if we've run out of octets and just have to
1604 ;; make do with what we've got.
1606 ;; but actually I don't think this works.
1607 ;; Consider: a file which is exactly the size of
1608 ;; the buffer, and whose last character is a CR.
1609 ;; We will get to the last character and request a
1610 ;; refill of the buffer; however, the refill will
1611 ;; throw EOF. We need something like the
1612 ;; input-at-least logic here (where we catch EOF
1613 ;; ourselves for refills between what the external
1614 ;; format can handle if there is no more, and what
1615 ;; it wants if there is more.)
1617 ;; but actually actually: it might be that
1618 ;; FAST-READ-CHAR-REFILL is doing this work for
1620 (requested-refill nil
))
1622 (let* ((ibuf (fd-stream-ibuf stream
))
1623 (head (buffer-head ibuf
))
1624 (tail (buffer-tail ibuf
))
1625 (sap (buffer-sap ibuf
))
1626 (decode-break-reason nil
)
1627 (unit ,(if (consp in-size-expr
)
1628 (let ((size-info (car in-size-expr
)))
1629 (if (consp size-info
)
1633 (declare (type index head tail
))
1634 ;; Copy data from stream buffer into user's buffer.
1635 (do ((size nil nil
))
1636 ((or (= tail head
) (= requested total-copied
)))
1637 (setf decode-break-reason
1638 (block decode-break-reason
1639 ,@(when (consp in-size-expr
)
1640 (let* ((size-info (car in-size-expr
))
1641 (want (if (consp size-info
)
1642 `(if requested-refill
,(cadr size-info
) ,(car size-info
))
1644 `((when (> ,want
(- tail head
))
1645 (setf requested-refill t
)
1647 (let ((byte (sap-ref-8 sap head
)))
1648 (declare (ignorable byte
))
1649 (setf requested-refill nil
)
1650 (setq size
,(if (consp in-size-expr
) (cadr in-size-expr
) in-size-expr
))
1651 (when (> size
(- tail head
))
1653 (let ((index (+ start total-copied
)))
1654 (setf (aref buffer index
) ,in-expr
)
1655 (setf (aref sbuffer index
) size
))
1659 (setf (buffer-head ibuf
) head
)
1660 (when decode-break-reason
1661 ;; If we've already read some characters on when the invalid
1662 ;; code sequence is detected, we return immediately. The
1663 ;; handling of the error is deferred until the next call
1664 ;; (where this check will be false). This allows establishing
1665 ;; high-level handlers for decode errors (for example
1666 ;; automatically resyncing in Lisp comments).
1667 (when (plusp total-copied
)
1668 (return-from ,in-function total-copied
))
1669 (when (stream-decoding-error-and-handle
1670 stream decode-break-reason unit
)
1672 (error 'end-of-file
:stream stream
)
1673 (return-from ,in-function total-copied
)))
1674 ;; we might have been given stuff to use instead, so
1675 ;; we have to return (and trust our caller to know
1676 ;; what to do about TOTAL-COPIED being 0).
1677 (return-from ,in-function total-copied
)))
1678 (setf (buffer-head ibuf
) head
)
1679 ;; Maybe we need to refill the stream buffer.
1680 (cond (;; If was data in the stream buffer, we're done.
1681 (plusp total-copied
)
1682 (return total-copied
))
1683 (;; If EOF, we're done in another way.
1684 (or (eq decode-break-reason
'eof
)
1685 (null (catch 'eof-input-catcher
1686 (refill-input-buffer stream
))))
1688 (error 'end-of-file
:stream stream
)
1689 (return total-copied
)))
1690 ;; Otherwise we refilled the stream buffer, so fall
1691 ;; through into another pass of the loop.
1693 (def-input-routine/variable-width
,in-char-function
(character
1697 (let ((byte (sap-ref-8 sap head
)))
1698 (declare (ignorable byte
))
1700 (defun ,resync-function
(stream)
1701 (declare (optimize (sb-c:verify-arg-count
0)))
1702 (let ((ibuf (fd-stream-ibuf stream
))
1704 (unit ,(if (consp in-size-expr
)
1705 (let ((size-info (car in-size-expr
)))
1706 (if (consp size-info
)
1710 (catch 'eof-input-catcher
1712 (incf (buffer-head ibuf
) unit
)
1713 ,(if (consp in-size-expr
)
1714 (let ((size-info (car in-size-expr
)))
1715 (if (consp size-info
)
1717 (input-at-least stream
,(cadr size-info
))
1718 (catch 'eof-input-catcher
1719 (input-at-least stream
,(car size-info
))))
1720 `(input-at-least stream
,size-info
)))
1721 `(input-at-least stream
(setq size
,in-size-expr
)))
1722 (unless (block decode-break-reason
1723 (let* ((sap (buffer-sap ibuf
))
1724 (head (buffer-head ibuf
))
1725 (tail (buffer-tail ibuf
))
1726 (byte (sap-ref-8 sap head
)))
1727 (declare (ignorable byte tail
))
1728 ,@(when (consp in-size-expr
)
1729 `((setq size
,(cadr in-size-expr
))
1730 (input-at-least stream size
)))
1731 (setf head
(buffer-head ibuf
))
1735 (defun ,read-c-string-function
(sap element-type
)
1736 (declare (type system-area-pointer sap
)
1737 (optimize (sb-c:verify-arg-count
0)))
1739 (declare (optimize (speed 3) (safety 0)))
1740 (let* ((stream ,name
)
1741 (size 0) (head 0) (tail (1- array-dimension-limit
)) (byte 0) (|ch| nil
)
1742 (decode-break-reason nil
)
1743 (length (dotimes (count (1- array-dimension-limit
) count
)
1744 (setf decode-break-reason
1745 (block decode-break-reason
1746 (setf byte
(sap-ref-8 sap head
)
1747 size
,(if (consp in-size-expr
)
1753 (when decode-break-reason
1754 (c-string-decoding-error
1755 ,name sap head decode-break-reason
))
1756 (when (zerop (char-code |ch|
))
1758 (string (case element-type
1760 (make-string length
:element-type
'base-char
))
1762 (make-string length
:element-type
'character
))
1764 (make-string length
:element-type element-type
)))))
1765 (declare (ignorable stream byte tail
)
1766 (type index head length tail
) ;; size
1767 (type (unsigned-byte 8) byte
)
1768 (type (or null character
) |ch|
)
1769 (type string string
))
1771 (dotimes (index length string
)
1772 (setf decode-break-reason
1773 (block decode-break-reason
1774 (setf byte
(sap-ref-8 sap head
)
1775 size
,(if (consp in-size-expr
)
1781 (when decode-break-reason
1782 (c-string-decoding-error
1783 ,name sap head decode-break-reason
))
1784 (setf (aref string index
) |ch|
)))))
1786 (defun ,output-c-string-function
(string)
1787 (declare (type simple-string string
))
1788 (cond ,@(and base-string-direct-mapping
1789 `(((simple-base-string-p string
)
1793 (declare (optimize (speed 3) (safety 0)))
1794 (block output-nothing
1795 (let* ((length (length string
))
1796 ;; wtf? why not just "LET ((bits 0))" ?
1797 (null-size (let* ((|ch|
(code-char 0))
1798 (bits (char-code |ch|
)))
1799 (declare (ignorable |ch| bits
))
1800 (the index
,out-size-expr
)))
1802 (+ (loop for i of-type index below length
1803 for |ch| of-type character
= (aref string i
)
1804 for bits
= (char-code |ch|
)
1805 sum
(the index
,out-size-expr
) of-type index
)
1808 (,n-buffer
(make-array buffer-length
1809 :element-type
'(unsigned-byte 8)))
1810 ;; For external-format-encoding-error
1812 (declare (type index length buffer-length tail
)
1814 (with-pinned-objects (,n-buffer
)
1815 (let ((sap (vector-sap ,n-buffer
)))
1816 (declare (system-area-pointer sap
))
1817 (loop for i of-type index below length
1818 for |ch| of-type character
= (aref string i
)
1819 for bits
= (char-code |ch|
)
1820 for size of-type index
= ,out-size-expr
1825 (|ch|
(code-char bits
)) ; more wtf
1827 (declare (ignorable bits |ch| size
))
1831 (register-external-format
1833 :newline-variant
,newline-variant
1834 :default-replacement-character
,replacement-character
1835 :read-n-chars-fun
#',in-function
1836 :read-char-fun
#',in-char-function
1837 :write-n-bytes-fun
#',out-function
1838 ,@(mapcan #'(lambda (buffering)
1839 (list (intern (format nil
"WRITE-CHAR-~A-BUFFERED-FUN" buffering
) :keyword
)
1840 `#',(intern (format nil format
(string buffering
)))))
1841 '(:none
:line
:full
))
1842 :resync-fun
#',resync-function
1843 :bytes-for-char-fun
#',size-function
1844 :read-c-string-fun
#',read-c-string-function
1845 :write-c-string-fun
#',output-c-string-function
1846 :octets-to-string-fun
(lambda (&rest rest
)
1847 (declare (dynamic-extent rest
))
1848 (apply ',octets-to-string-sym rest
))
1849 :string-to-octets-fun
(lambda (&rest rest
)
1850 (declare (dynamic-extent rest
))
1851 (apply ',string-to-octets-sym rest
))))))
1853 ;;;; utility functions (misc routines, etc)
1855 ;;; Fill in the various routine slots for the given type. INPUT-P and
1856 ;;; OUTPUT-P indicate what slots to fill. The buffering slot must be
1857 ;;; set prior to calling this routine.
1858 (defun set-fd-stream-routines (fd-stream element-type canonized-external-format external-format-entry
1859 input-p output-p buffer-p
)
1860 (let* ((target-type (case element-type
1861 (unsigned-byte '(unsigned-byte 8))
1862 (signed-byte '(signed-byte 8))
1863 (:default
'character
)
1865 (character-stream-p (subtypep target-type
'character
))
1866 (bivalent-stream-p (eq element-type
:default
))
1869 (bin-routine #'ill-bin
)
1872 (cin-routine #'ill-in
)
1875 (input-type nil
) ;calculated from bin-type/cin-type
1876 (input-size nil
) ;calculated from bin-size/cin-size
1877 (read-n-characters #'ill-in
)
1878 (bout-routine #'ill-bout
)
1881 (cout-routine #'ill-out
)
1886 (output-bytes #'ill-bout
))
1888 ;; Ensure that we have buffers in the desired direction(s) only,
1889 ;; getting new ones and dropping/resetting old ones as necessary.
1890 (let ((obuf (fd-stream-obuf fd-stream
)))
1894 (setf (fd-stream-obuf fd-stream
) (get-buffer)))
1896 (setf (fd-stream-obuf fd-stream
) nil
)
1897 (release-buffer obuf
))))
1899 (let ((ibuf (fd-stream-ibuf fd-stream
)))
1903 (setf (fd-stream-ibuf fd-stream
) (get-buffer)))
1905 (setf (fd-stream-ibuf fd-stream
) nil
)
1906 (release-buffer ibuf
))))
1908 ;; FIXME: Why only for output? Why unconditionally?
1910 (setf (fd-stream-output-column fd-stream
) 0))
1913 (flet ((no-input-routine ()
1914 (error "could not find any input routine for ~
1915 ~/sb-impl:print-type-specifier/"
1917 (when (or (not character-stream-p
) bivalent-stream-p
)
1918 (setf (values bin-routine bin-type bin-size read-n-characters
1919 char-size replacement
)
1920 (pick-input-routine (if bivalent-stream-p
'(unsigned-byte 8)
1922 external-format-entry
))
1923 (unless bin-routine
(no-input-routine)))
1924 (when character-stream-p
1925 (setf (values cin-routine cin-type cin-size read-n-characters
1926 char-size replacement
)
1927 (pick-input-routine target-type external-format-entry
))
1928 (unless cin-routine
(no-input-routine))))
1929 (setf (fd-stream-in fd-stream
) cin-routine
1930 (fd-stream-bin fd-stream
) bin-routine
)
1931 ;; character type gets preferential treatment
1932 (setf input-size
(or cin-size bin-size
))
1933 (setf input-type
(or cin-type bin-type
))
1935 (setf (fd-stream-external-format fd-stream
) canonized-external-format
1936 (fd-stream-char-size fd-stream
) char-size
1937 (fd-stream-replacement fd-stream
) replacement
))
1938 (when (= (or cin-size
1) (or bin-size
1) 1)
1939 (setf (fd-stream-n-bin fd-stream
) ;XXX
1940 (if (and character-stream-p
(not bivalent-stream-p
))
1942 #'fd-stream-read-n-bytes
))
1943 ;; Sometimes turn on fast-read-char/fast-read-byte. Switch on
1944 ;; for character and (unsigned-byte 8) streams. In these
1945 ;; cases, fast-read-* will read from the
1946 ;; ansi-stream-(c)in-buffer, saving function calls.
1947 ;; Otherwise, the various data-reading functions in the stream
1948 ;; structure will be called.
1950 (not bivalent-stream-p
)
1951 ;; temporary disable on :io streams
1953 (cond (character-stream-p
1954 (setf (ansi-stream-cin-buffer fd-stream
)
1955 (make-array +ansi-stream-in-buffer-length
+
1956 :element-type
'character
))
1957 (setf (ansi-stream-csize-buffer fd-stream
)
1958 (make-array +ansi-stream-in-buffer-length
+
1959 :element-type
'(unsigned-byte 8))))
1960 ((equal target-type
'(unsigned-byte 8))
1961 (setf (ansi-stream-in-buffer fd-stream
)
1962 (make-array +ansi-stream-in-buffer-length
+
1963 :element-type
'(unsigned-byte 8))))))))
1966 (when (or (not character-stream-p
) bivalent-stream-p
)
1967 (setf (values bout-routine bout-type bout-size output-bytes
1968 char-size replacement
)
1969 (let ((buffering (fd-stream-buffering fd-stream
)))
1970 (if bivalent-stream-p
1971 (pick-output-routine '(unsigned-byte 8)
1972 (if (eq :line buffering
)
1975 (pick-output-routine target-type buffering external-format-entry
))))
1976 (unless bout-routine
1977 (error "could not find any output routine for ~S buffered ~S"
1978 (fd-stream-buffering fd-stream
)
1980 (when character-stream-p
1981 (setf (values cout-routine cout-type cout-size output-bytes
1982 char-size replacement
)
1983 (pick-output-routine target-type
1984 (fd-stream-buffering fd-stream
)
1985 external-format-entry
))
1986 (unless cout-routine
1987 (error "could not find any output routine for ~S buffered ~S"
1988 (fd-stream-buffering fd-stream
)
1991 (setf (fd-stream-external-format fd-stream
) canonized-external-format
1992 (fd-stream-replacement fd-stream
) replacement
1993 (fd-stream-char-size fd-stream
) char-size
))
1994 (when character-stream-p
1995 (setf (fd-stream-output-bytes fd-stream
) output-bytes
))
1996 (setf (fd-stream-cout fd-stream
) cout-routine
1997 (fd-stream-bout fd-stream
) bout-routine
1998 (fd-stream-sout fd-stream
) (if (eql cout-size
1)
1999 #'fd-sout
#'ill-out
))
2000 (setf output-size
(or cout-size bout-size
))
2001 (setf output-type
(or cout-type bout-type
)))
2003 (when (and input-size output-size
2004 (not (eq input-size output-size
)))
2005 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
2006 input-type input-size
2007 output-type output-size
))
2008 (setf (fd-stream-element-size fd-stream
)
2009 (or input-size output-size
))
2011 (setf (fd-stream-element-type fd-stream
)
2012 (cond ((equal input-type output-type
)
2018 ((subtypep input-type output-type
)
2020 ((subtypep output-type input-type
)
2023 (error "Input type (~/sb-impl:print-type-specifier/) and ~
2024 output type (~/sb-impl:print-type-specifier/) ~
2026 input-type output-type
))))))
2028 ;;; Handles the resource-release aspects of stream closing, and marks
2030 (defun release-fd-stream-resources (fd-stream)
2033 ;; Drop handlers first.
2034 (when (fd-stream-handler fd-stream
)
2035 (remove-fd-handler (fd-stream-handler fd-stream
))
2036 (setf (fd-stream-handler fd-stream
) nil
))
2037 ;; Disable interrupts so that a asynch unwind will not leave
2038 ;; us with a dangling finalizer (that would close the same
2039 ;; --possibly reassigned-- FD again), or a stream with a closed
2040 ;; FD that appears open.
2041 (cancel-finalization fd-stream
)
2042 (let ((fd (fd-stream-fd fd-stream
)))
2043 (when (and (/= fd -
1)
2044 (eq (cas (fd-stream-fd fd-stream
) fd -
1) fd
))
2045 (sb-unix:unix-close fd
)))
2046 (set-closed-flame fd-stream
))
2047 ;; On error unwind from WITHOUT-INTERRUPTS.
2048 (serious-condition (e)
2050 ;; Release all buffers. If this is undone, or interrupted,
2051 ;; we're still safe: buffers have finalizers of their own.
2052 (release-fd-stream-buffers fd-stream
))
2054 ;;; Flushes the current input buffer and any supplied replacements,
2055 ;;; and returns the input buffer, and the amount of flushed input in
2057 (defun flush-input-buffer (stream)
2058 (let* ((instead (fd-stream-instead stream
))
2059 (unread (length instead
)))
2060 ;; (setf fill-pointer) performs some checks and is slower
2061 (setf (%array-fill-pointer instead
) 0)
2062 (let ((ibuf (fd-stream-ibuf stream
)))
2064 (let ((head (buffer-head ibuf
))
2065 (tail (buffer-tail ibuf
)))
2066 (values (reset-buffer ibuf
) (- (+ unread tail
) head
)))
2067 (values nil unread
)))))
2069 (defun fd-stream-clear-input (stream)
2070 (flush-input-buffer stream
)
2073 (sb-win32:handle-clear-input
(fd-stream-fd stream
))
2074 (setf (fd-stream-listen stream
) nil
))
2076 (catch 'eof-input-catcher
2077 (loop until
(sysread-may-block-p stream
)
2079 (refill-input-buffer stream
)
2080 (reset-buffer (fd-stream-ibuf stream
)))
2083 ;;; Handle miscellaneous operations on FD-STREAM.
2084 (defun fd-stream-misc-routine (fd-stream operation arg1
)
2085 (stream-misc-case (operation :default nil
)
2087 (labels ((do-listen ()
2088 (let ((ibuf (fd-stream-ibuf fd-stream
)))
2089 (or (not (eql (buffer-head ibuf
) (buffer-tail ibuf
)))
2090 (fd-stream-listen fd-stream
)
2091 ;; If the read can block, LISTEN will certainly return NIL.
2092 (if (sysread-may-block-p fd-stream
)
2094 ;; Otherwise select(2) and CL:LISTEN have slightly
2095 ;; different semantics. The former returns that an FD
2096 ;; is readable when a read operation wouldn't block.
2097 ;; That includes EOF. However, LISTEN must return NIL
2099 (progn (catch 'eof-input-catcher
2100 ;; r-b/f too calls select, but it shouldn't
2101 ;; block as long as read can return once w/o
2103 (refill-input-buffer fd-stream
))
2104 ;; At this point either IBUF-HEAD != IBUF-TAIL
2105 ;; and FD-STREAM-LISTEN is NIL, in which case
2106 ;; we should return T, or IBUF-HEAD ==
2107 ;; IBUF-TAIL and FD-STREAM-LISTEN is :EOF, in
2108 ;; which case we should return :EOF for this
2109 ;; call and all future LISTEN call on this stream.
2110 ;; Call ourselves again to determine which case
2115 (let* ((ibuf (fd-stream-ibuf fd-stream
))
2116 (head (buffer-head ibuf
))
2117 (prev (buffer-prev-head ibuf
)))
2119 ;; we are unreading a character which we previously pulled
2120 ;; from INSTEAD; push it back there.
2121 (vector-push-extend arg1
(fd-stream-instead fd-stream
))
2122 ;; reset the buffer position to where it was before we read
2123 ;; the previous character.
2124 (setf (buffer-head ibuf
) (buffer-prev-head ibuf
)))))
2126 ;; Drop input buffers
2127 (setf (ansi-stream-in-index fd-stream
) +ansi-stream-in-buffer-length
+
2128 (ansi-stream-cin-buffer fd-stream
) nil
2129 (ansi-stream-csize-buffer fd-stream
) nil
2130 (ansi-stream-in-buffer fd-stream
) nil
)
2132 ;; We got us an abort on our hands.
2133 (let ((outputp (fd-stream-obuf fd-stream
))
2134 (file (fd-stream-file fd-stream
))
2135 (orig (fd-stream-original fd-stream
)))
2136 ;; This takes care of the important stuff -- everything
2137 ;; rest is cleaning up the file-system, which we cannot
2138 ;; do on some platforms as long as the file is open.
2139 (release-fd-stream-resources fd-stream
)
2140 ;; We can't do anything unless we know what file were
2141 ;; dealing with, and we don't want to do anything
2142 ;; strange unless we were writing to the file.
2143 (when (and outputp file
)
2145 ;; If the original is EQ to file we are appending to
2146 ;; and can just close the file without renaming.
2147 (unless (eq orig file
)
2148 ;; We have a handle on the original, just revert.
2149 (multiple-value-bind (okay err
)
2150 (sb-unix:unix-rename orig file
)
2151 ;; FIXME: Why is this a SIMPLE-STREAM-ERROR, and the
2152 ;; others are SIMPLE-FILE-ERRORS? Surely they should
2155 (simple-stream-perror
2156 "~@<Couldn't restore ~S to its original ~
2157 contents from ~S while closing ~S~:>"
2159 file orig fd-stream
))))
2160 ;; We can't restore the original, and aren't
2161 ;; appending, so nuke that puppy.
2163 ;; FIXME: This is currently the fate of superseded
2164 ;; files, and according to the CLOSE spec this is
2165 ;; wrong. However, there seems to be no clean way to
2166 ;; do that that doesn't involve either copying the
2167 ;; data (bad if the :abort resulted from a full
2168 ;; disk), or renaming the old file temporarily
2169 ;; (probably bad because stream opening becomes more
2171 (multiple-value-bind (okay err
)
2172 (sb-unix:unix-unlink file
)
2176 "~@<Couldn't remove ~S while closing ~S~:>" file fd-stream
)))))))
2178 (finish-fd-stream-output fd-stream
)
2179 (let ((orig (fd-stream-original fd-stream
)))
2180 (when (and orig
(fd-stream-delete-original fd-stream
))
2181 (multiple-value-bind (okay err
) (sb-unix:unix-unlink orig
)
2185 "~@<Couldn't delete ~S while closing ~S~:>" orig fd-stream
)))))
2186 ;; In case of no-abort close, don't *really* close the
2187 ;; stream until the last moment -- the cleaning up of the
2188 ;; original can be done first.
2189 (release-fd-stream-resources fd-stream
))))
2191 (fd-stream-clear-input fd-stream
))
2193 (flush-output-buffer fd-stream
))
2195 (finish-fd-stream-output fd-stream
))
2197 (fd-stream-element-type fd-stream
))
2199 (fd-stream-element-mode fd-stream
))
2201 (fd-stream-external-format fd-stream
))
2203 (plusp (the (integer 0)
2204 (sb-unix:unix-isatty
(fd-stream-fd fd-stream
)))))
2208 (fd-stream-output-column fd-stream
))
2210 (unless (fd-stream-file fd-stream
)
2211 ;; This is a TYPE-ERROR because ANSI's species FILE-LENGTH
2212 ;; "should signal an error of type TYPE-ERROR if stream is not
2213 ;; a stream associated with a file". Too bad there's no very
2214 ;; appropriate value for the EXPECTED-TYPE slot..
2215 (error 'simple-type-error
2217 :expected-type
'fd-stream
2218 :format-control
"~S is not a stream associated with a file."
2219 :format-arguments
(list fd-stream
)))
2221 (multiple-value-bind (okay dev ino mode nlink uid gid rdev size
2222 atime mtime ctime blksize blocks
)
2223 (sb-unix:unix-fstat
(fd-stream-fd fd-stream
))
2224 (declare (ignore ino nlink uid gid rdev
2225 atime mtime ctime blksize blocks
))
2227 (simple-stream-perror "failed Unix fstat(2) on ~S" fd-stream dev
))
2230 (truncate size
(fd-stream-element-size fd-stream
))))
2232 (let* ((handle (fd-stream-fd fd-stream
))
2233 (element-size (fd-stream-element-size fd-stream
)))
2234 (multiple-value-bind (got native-size
)
2235 (sb-win32:get-file-size-ex handle
0)
2237 ;; Might be a block device, in which case we fall back to
2238 ;; a non-atomic workaround:
2239 (let* ((here (sb-unix:unix-lseek handle
0 sb-unix
:l_incr
))
2240 (there (sb-unix:unix-lseek handle
0 sb-unix
:l_xtnd
)))
2241 (when (and here there
)
2242 (sb-unix:unix-lseek handle here sb-unix
:l_set
)
2243 (truncate there element-size
)))
2244 (truncate native-size element-size
)))))
2245 (:file-string-length
2247 (character (fd-stream-character-size fd-stream arg1
))
2248 (string (fd-stream-string-size fd-stream arg1
))))
2249 (:get-file-position
(fd-stream-get-file-position fd-stream
))
2250 (:set-file-position
(fd-stream-set-file-position fd-stream arg1
))))
2252 ;; FIXME: Think about this.
2254 ;; (defun finish-fd-stream-output (fd-stream)
2255 ;; (let ((timeout (fd-stream-timeout fd-stream)))
2256 ;; (loop while (fd-stream-output-queue fd-stream)
2257 ;; ;; FIXME: SIGINT while waiting for a timeout will
2258 ;; ;; cause a timeout here.
2259 ;; do (when (and (not (serve-event timeout)) timeout)
2260 ;; (signal-timeout 'io-timeout
2261 ;; :stream fd-stream
2262 ;; :direction :write
2263 ;; :seconds timeout)))))
2265 (defun finish-fd-stream-output (stream)
2266 (flush-output-buffer stream
)
2268 ((null (fd-stream-output-queue stream
)))
2269 (aver (fd-stream-serve-events stream
))
2270 (serve-all-events)))
2272 (defun fd-stream-get-file-position (stream)
2273 (declare (fd-stream stream
))
2275 (let ((posn (sb-unix:unix-lseek
(fd-stream-fd stream
) 0 sb-unix
:l_incr
)))
2276 (declare (type (or (alien sb-unix
:unix-offset
) null
) posn
))
2277 ;; We used to return NIL for errno==ESPIPE, and signal an error
2278 ;; in other failure cases. However, CLHS says to return NIL if
2279 ;; the position cannot be determined -- so that's what we do.
2280 (when (integerp posn
)
2281 ;; Adjust for buffered output: If there is any output
2282 ;; buffered, the *real* file position will be larger
2283 ;; than reported by lseek() because lseek() obviously
2284 ;; cannot take into account output we have not sent
2286 (dolist (buffer (fd-stream-output-queue stream
))
2287 (incf posn
(- (buffer-tail buffer
) (buffer-head buffer
))))
2288 (let ((obuf (fd-stream-obuf stream
)))
2290 (incf posn
(buffer-tail obuf
))))
2291 ;; Adjust for unread input: If there is any input
2292 ;; read from UNIX but not supplied to the user of the
2293 ;; stream, the *real* file position will smaller than
2294 ;; reported, because we want to look like the unread
2295 ;; stuff is still available.
2296 (let ((ibuf (fd-stream-ibuf stream
)))
2298 (decf posn
(- (buffer-tail ibuf
) (buffer-head ibuf
)))))
2299 ;; Divide bytes by element size.
2300 (truncate posn
(fd-stream-element-size stream
))))))
2302 (defun fd-stream-set-file-position (stream position-spec
)
2303 (declare (fd-stream stream
))
2304 (check-type position-spec
2305 (or (alien sb-unix
:unix-offset
) (member nil
:start
:end
))
2306 "valid file position designator")
2309 ;; Make sure we don't have any output pending, because if we
2310 ;; move the file pointer before writing this stuff, it will be
2311 ;; written in the wrong location.
2312 (finish-fd-stream-output stream
)
2313 ;; Disable interrupts so that interrupt handlers doing output
2316 (unless (fd-stream-output-finished-p stream
)
2317 ;; We got interrupted and more output came our way during
2318 ;; the interrupt. Wrapping the FINISH-FD-STREAM-OUTPUT in
2319 ;; WITHOUT-INTERRUPTS gets nasty as it can signal errors,
2320 ;; so we prefer to do things like this...
2322 ;; Clear out any pending input to force the next read to go to
2324 (flush-input-buffer stream
)
2325 ;; Trash cached value for listen, so that we check next time.
2326 (setf (fd-stream-listen stream
) nil
)
2328 (multiple-value-bind (offset origin
)
2331 (values 0 sb-unix
:l_set
))
2333 (values 0 sb-unix
:l_xtnd
))
2335 (values (* position-spec
(fd-stream-element-size stream
))
2337 (declare (type (alien sb-unix
:unix-offset
) offset
))
2338 (let ((posn (sb-unix:unix-lseek
(fd-stream-fd stream
)
2340 ;; CLHS says to return true if the file-position was set
2341 ;; successfully, and NIL otherwise. We are to signal an error
2342 ;; only if the given position was out of bounds, and that is
2343 ;; dealt with above. In times past we used to return NIL for
2344 ;; errno==ESPIPE, and signal an error in other cases.
2346 ;; FIXME: We are still liable to signal an error if flushing
2348 (return-from fd-stream-set-file-position
2349 (typep posn
'(alien sb-unix
:unix-offset
))))))))
2352 ;;;; creation routines (MAKE-FD-STREAM and OPEN)
2354 ;;; Create a stream for the given Unix file descriptor.
2356 ;;; If INPUT is non-NIL, allow input operations. If OUTPUT is non-nil,
2357 ;;; allow output operations. If neither INPUT nor OUTPUT is specified,
2358 ;;; default to allowing input.
2360 ;;; ELEMENT-TYPE indicates the element type to use (as for OPEN).
2362 ;;; BUFFERING indicates the kind of buffering to use.
2364 ;;; TIMEOUT (if true) is the number of seconds to wait for input. If
2365 ;;; NIL (the default), then wait forever. When we time out, we signal
2368 ;;; FILE is the name of the file (will be returned by PATHNAME).
2370 ;;; NAME is used to identify the stream when printed.
2372 ;;; If SERVE-EVENTS is true, SERVE-EVENT machinery is used to
2373 ;;; handle blocking IO on the stream.
2374 (defun make-fd-stream (fd
2378 (output nil output-p
)
2379 (element-type 'base-char
)
2381 (external-format :default
)
2391 (format nil
"file ~A" file
)
2392 (format nil
"descriptor ~W" fd
)))
2394 (declare (type index fd
) (type (or real null
) timeout
)
2395 (type (member :none
:line
:full
) buffering
))
2396 ;; OPEN ensures that the external-format argument is OK before
2397 ;; creating an FD and calling here. MAKE-FD-STREAM isn't really
2398 ;; part of a public interface but has numerous callers with an
2399 ;; :EXTERNAL-FORMAT argument, so we need to repeat the check and
2400 ;; canonization here, but if we detect a problem we must make sure
2402 (let* ((defaulted-external-format (if (eql external-format
:default
)
2403 (default-external-format)
2405 (external-format-entry (get-external-format defaulted-external-format
))
2406 (canonized-external-format
2407 (and external-format-entry
(canonize-external-format external-format external-format-entry
))))
2408 (unless external-format-entry
2410 (error "Undefined external-format: ~S" external-format
)
2411 (sb-unix:unix-close fd
)))
2413 (cond ((not (or input-p output-p
))
2415 ((not (or input output
))
2417 (error "File descriptor must be opened either for input or output.")
2418 (sb-unix:unix-close fd
))))
2419 (let* ((constructor (ecase class
2420 (fd-stream '%make-fd-stream
)
2421 (form-tracking-stream '%make-form-tracking-stream
)))
2422 (element-mode (stream-element-type-stream-element-mode element-type
))
2423 (stream (funcall constructor
2426 #-win32
(sb-unix:fd-type fd
)
2428 #+win32
(if serve-events
2434 :delete-original delete-original
2436 :buffering buffering
2437 :dual-channel-p dual-channel-p
2438 :element-mode element-mode
2439 :serve-events serve-events
2442 (coerce timeout
'single-float
)
2444 (set-fd-stream-routines stream element-type canonized-external-format external-format-entry
2445 input output input-buffer-p
)
2449 (sb-unix:unix-close fd
)
2451 (format *terminal-io
* "** closed file descriptor ~W **~%"
2456 ;;; Pick a name to use for the backup file for the :IF-EXISTS
2457 ;;; :RENAME-AND-DELETE and :RENAME options.
2458 (defun pick-backup-name (name)
2459 (declare (type simple-string name
))
2460 (concatenate 'simple-string name
".bak"))
2462 ;;; Rename NAMESTRING to ORIGINAL. First, check whether we have write
2463 ;;; access, since we don't want to trash unwritable files even if we
2464 ;;; technically can. We return true if we succeed in renaming.
2465 (defun rename-the-old-one (namestring original
)
2466 (unless (sb-unix:unix-access namestring sb-unix
:w_ok
)
2467 (error "~@<The file ~2I~_~S ~I~_is not writable.~:>" namestring
))
2468 (multiple-value-bind (okay err
) (sb-unix:unix-rename namestring original
)
2473 "~@<couldn't rename ~2I~_~S ~I~_to ~2I~_~S~:>" namestring original
))))
2475 #+unix
(defun file-exists-p (path) (sb-unix:unix-access path sb-unix
:f_ok
))
2477 (defun %open-error
(pathname errno if-exists if-does-not-exist
)
2478 (flet ((signal-it (&rest arguments
)
2479 (apply #'file-perror pathname errno arguments
)))
2482 (#-win32
#.sb-unix
:enoent
2483 #+win32
#.sb-win32
::error_file_not_found
2484 (case if-does-not-exist
2487 (signal-it 'file-does-not-exist
)
2489 :report
"Reopen with :if-does-not-exist :create"
2490 '(:new-if-does-not-exist
:create
))))
2492 (sb-kernel::%file-error
2494 "~@<The path ~2I~_~S ~I~_does not exist.~:>" pathname
))
2495 (t '(:return nil
))))
2496 (#-win32
#.sb-unix
:eexist
2497 #+win32
#.sb-win32
::error_file_exists
2498 (if (null if-exists
)
2501 (signal-it 'file-exists
)
2503 :report
"Reopen with :if-exists :supersede"
2504 '(:new-if-exists
:supersede
))
2506 :report
"Reopen with :if-exists :overwrite"
2507 '(:new-if-exists
:overwrite
))
2509 :report
"Reopen with :if-exists :rename"
2510 '(:new-if-exists
:rename
))
2512 :report
"Reopen with :if-exists :append"
2513 '(:new-if-exists
:append
)))))
2515 (signal-it "Error opening ~S" pathname
)))
2517 :report
"Retry opening."
2520 :report
"Try opening a different file."
2521 :interactive read-evaluated-form
2522 (list :new-filename
(the pathname-designator value
))))))
2524 (defun open (filename
2527 (element-type 'character
)
2528 (if-exists nil if-exists-given
)
2529 (if-does-not-exist nil if-does-not-exist-given
)
2530 (external-format :default
)
2531 ;; private options - use at your own risk
2536 ;; Squelch assignment warning.
2538 (direction direction
)
2539 (if-does-not-exist if-does-not-exist
)
2540 (if-exists if-exists
))
2541 "Return a stream which reads from or writes to FILENAME.
2543 :DIRECTION - one of :INPUT, :OUTPUT, :IO, or :PROBE
2544 :ELEMENT-TYPE - the type of object to read or write, default BASE-CHAR
2545 :IF-EXISTS - one of :ERROR, :NEW-VERSION, :RENAME, :RENAME-AND-DELETE,
2546 :OVERWRITE, :APPEND, :SUPERSEDE or NIL
2547 :IF-DOES-NOT-EXIST - one of :ERROR, :CREATE or NIL
2548 See the manual for details."
2549 (let* ((defaulted-external-format (if (eql external-format
:default
)
2550 (default-external-format)
2552 (external-format-entry (get-external-format defaulted-external-format
))
2553 (canonized-external-format
2554 (and external-format-entry
(canonize-external-format external-format external-format-entry
))))
2555 (unless external-format-entry
2556 (error "Undefined external-format: ~S" external-format
))
2557 ;; Calculate useful stuff.
2559 (multiple-value-bind (input output mask
)
2561 (:input
(values t nil sb-unix
:o_rdonly
))
2562 (:output
(values nil t sb-unix
:o_wronly
))
2563 (:io
(values t t sb-unix
:o_rdwr
))
2564 (:probe
(values t nil sb-unix
:o_rdonly
)))
2565 (declare (type index mask
))
2566 (let* (;; PATHNAME is the pathname we associate with the stream.
2567 (pathname (merge-pathnames filename
))
2568 (physical (native-namestring (physicalize-pathname pathname
) :as-file t
))
2569 ;; One call to access() is reasonable. 40 calls to lstat() is not.
2570 ;; So DO NOT CALL TRUENAME HERE.
2571 (existsp (file-exists-p physical
))
2572 ;; Leave NAMESTRING as NIL if nonexistent and not creating a file.
2573 (namestring (when (or existsp
2575 (and input
(eq if-does-not-exist
:create
))
2576 (and (eq direction
:io
)
2577 (not if-does-not-exist-given
))))
2579 ;; Process if-exists argument if we are doing any output.
2581 (unless if-exists-given
2583 (if (eq (pathname-version pathname
) :newest
)
2587 ((:new-version
:error nil
)
2588 (setf mask
(logior mask sb-unix
:o_excl
)))
2589 ((:rename
:rename-and-delete
)
2590 (setf mask
(logior mask sb-unix
:o_creat
)))
2592 (setf mask
(logior mask sb-unix
:o_trunc
)))
2594 (setf mask
(logior mask sb-unix
:o_append
)))))
2596 (setf if-exists
:ignore-this-arg
)))
2598 (unless if-does-not-exist-given
2599 (setf if-does-not-exist
2600 (cond ((eq direction
:input
) :error
)
2602 (member if-exists
'(:overwrite
:append
)))
2604 ((eq direction
:probe
)
2608 (cond ((and existsp if-exists-given
(eq if-exists
:new-version
))
2609 (sb-kernel::%file-error
2610 pathname
"OPEN :IF-EXISTS :NEW-VERSION is not supported ~
2611 when a new version must be created."))
2612 ((eq if-does-not-exist
:create
)
2613 (setf mask
(logior mask sb-unix
:o_creat
)))
2614 ((not (member if-exists
'(:error nil
))))
2615 ;; Both if-does-not-exist and if-exists now imply
2616 ;; that there will be no opening of files, and either
2617 ;; an error would be signalled, or NIL returned
2618 ((and (not if-exists
) (not if-does-not-exist
))
2620 ((and if-exists if-does-not-exist
)
2621 (sb-kernel::%file-error
2622 pathname
"OPEN :IF-DOES-NOT-EXIST ~s ~
2623 :IF-EXISTS ~s will always signal an error."
2624 if-does-not-exist if-exists
))
2627 (sb-kernel::%file-error pathname
'file-exists
)
2630 (sb-kernel::%file-error pathname
'file-does-not-exist
))
2633 (let ((original (case if-exists
2634 ((:rename
:rename-and-delete
)
2635 (pick-backup-name namestring
))
2636 ((:append
:overwrite
)
2637 ;; KLUDGE: Prevent CLOSE from deleting
2638 ;; appending streams when called with :ABORT T
2640 (delete-original (eq if-exists
:rename-and-delete
))
2642 (when (and original
(not (eq original namestring
)))
2643 ;; We are doing a :RENAME or :RENAME-AND-DELETE. Determine
2644 ;; whether the file already exists, make sure the original
2645 ;; file is not a directory, and keep the mode.
2648 (multiple-value-bind (okay err
/dev inode orig-mode
)
2649 (sb-unix:unix-stat namestring
)
2650 (declare (ignore inode
)
2651 (type (or index null
) orig-mode
))
2654 (when (and output
(= (logand orig-mode
#o170000
)
2658 "Can't open ~S for output: is a directory"
2660 (setf mode
(logand orig-mode
#o777
))
2662 ((eql err
/dev sb-unix
:enoent
)
2665 (file-perror namestring err
/dev
2666 "Can't find ~S" namestring
)))))))
2668 (rename-the-old-one namestring original
))
2670 (setf delete-original nil
)
2671 ;; In order to use :SUPERSEDE instead, we have to make
2672 ;; sure SB-UNIX:O_CREAT corresponds to
2673 ;; IF-DOES-NOT-EXIST. SB-UNIX:O_CREAT was set before
2674 ;; because of IF-EXISTS being :RENAME.
2675 (unless (eq if-does-not-exist
:create
)
2677 (logior (logandc2 mask sb-unix
:o_creat
)
2679 (setf if-exists
:supersede
))))
2681 ;; Now we can try the actual Unix open(2).
2682 (multiple-value-bind (fd errno
)
2684 (sb-unix:unix-open namestring mask mode
2685 #+win32
:overlapped
#+win32 overlapped
)
2686 (values nil
#-win32 sb-unix
:enoent
2687 #+win32 sb-win32
::error_file_not_found
))
2689 (return (case direction
2690 ((:input
:output
:io
)
2691 ;; For O_APPEND opened files, lseek returns 0 until first write.
2692 ;; So we jump ahead here.
2693 (when (eq if-exists
:append
)
2694 (sb-unix:unix-lseek fd
0 sb-unix
:l_xtnd
))
2699 :element-type element-type
2700 :external-format canonized-external-format
2703 :delete-original delete-original
2711 (%make-fd-stream
:name namestring
2714 :element-type element-type
)))
2717 (destructuring-bind (&key
(return nil returnp
)
2720 new-if-does-not-exist
)
2721 (%open-error pathname errno if-exists if-does-not-exist
)
2725 (setf filename new-filename
))
2727 (setf if-exists new-if-exists if-exists-given t
))
2728 (when new-if-does-not-exist
2729 (setf if-does-not-exist new-if-does-not-exist
2730 if-does-not-exist-given t
))))))))))
2733 ;;; the Unix way to beep
2734 (defun beep (stream)
2735 (write-char (code-char bell-char-code
) stream
)
2736 (finish-output stream
))
2738 ;;; This is kind of like FILE-POSITION, but is an internal hack used
2739 ;;; by the filesys stuff to get and set the file name.
2741 ;;; FIXME: misleading name, screwy interface
2742 (defun file-name (stream &optional new-name
)
2743 (stream-api-dispatch (stream)
2746 (when (typep stream
'fd-stream
)
2748 (setf (fd-stream-pathname stream
) new-name
)
2749 (setf (fd-stream-file stream
)
2750 (native-namestring (physicalize-pathname new-name
)
2754 (fd-stream-pathname stream
))))
2755 :simple
(s-%file-name stream new-name
)))
2757 ;; Fix the INPUT-CHAR-POS slot of STREAM after having consumed characters
2758 ;; from the CIN-BUFFER. This operation is done upon exit from a FAST-READ-CHAR
2759 ;; loop, and for each buffer refill inside the loop.
2760 (defun update-input-char-pos (stream &optional
(end +ansi-stream-in-buffer-length
+))
2761 (do ((chars (ansi-stream-cin-buffer stream
))
2762 (pos (form-tracking-stream-input-char-pos stream
))
2763 (i (ansi-stream-in-index stream
) (1+ i
)))
2765 (setf (form-tracking-stream-input-char-pos stream
) pos
))
2766 (let ((char (aref chars i
)))
2767 (when (and (eql char
#\Newline
)
2768 ;; record it only if it wasn't unread and re-read
2769 (> pos
(form-tracking-stream-last-newline stream
)))
2770 (vector-push-extend pos
(form-tracking-stream-newlines stream
))
2771 (setf (form-tracking-stream-last-newline stream
) pos
))
2774 (defun tracking-stream-misc (stream operation arg1
)
2775 ;; The :UNREAD operation will never be invoked because STREAM has a buffer,
2776 ;; so unreading is implemented entirely within ANSI-STREAM-UNREAD-CHAR.
2777 ;; But we do need to prevent attempts to change the absolute position.
2778 (stream-misc-case (operation)
2779 (:set-file-position
(simple-stream-perror "~S is not positionable" stream
))
2780 (t ; call next method
2781 (fd-stream-misc-routine stream operation arg1
))))