%other-pointer-widetag derive-type: derive for simple-array.
[sbcl.git] / src / code / fd-stream.lisp
bloba4ec01c8986f07b4ab49f82dafbf09559a48fdd4
1 ;;;; streams for UNIX file descriptors
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB-IMPL")
14 ;;;; BUFFER
15 ;;;;
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.
20 ;;;;
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.
25 ;;;;
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:
31 ;;;;
32 ;;;; (let ((tail (buffer-tail buffer)))
33 ;;;; ...
34 ;;;; (setf (buffer-tail buffer) (+ tail n)))
35 ;;;;
36 ;;;; NOT
37 ;;;;
38 ;;;; (let ((tail (buffer-tail buffer)))
39 ;;;; ...
40 ;;;; (incf (buffer-tail buffer) n))
41 ;;;;
43 (defstruct (buffer (:constructor !make-buffer (sap length))
44 (:copier nil))
45 (sap (missing-arg) :type system-area-pointer :read-only t)
46 (length (missing-arg) :type index :read-only t)
47 (head 0 :type index)
48 (tail 0 :type index)
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.
63 (without-interrupts
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))
70 :dont-save t)
71 buffer)))
73 (defun get-buffer ()
74 (or (and *available-buffers* (atomic-pop *available-buffers*))
75 (alloc-buffer)))
77 (declaim (inline reset-buffer))
78 (defun reset-buffer (buffer)
79 (setf (buffer-head buffer) 0
80 (buffer-tail buffer) 0)
81 buffer)
83 (defun release-buffer (buffer)
84 (declare (sb-c::tlab :system))
85 (reset-buffer buffer)
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))
99 (defstruct (fd-stream
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))
107 (:copier nil))
109 ;; the name of this stream
110 (name nil)
111 ;; the file this stream is for
112 (file nil)
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.
134 (dual-channel-p nil)
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
138 #+64-bit index)
139 null))
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)
145 ;; the input buffer
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))
150 ;; the output buffer
151 (obuf nil :type (or buffer null))
153 ;; output flushed, but not written due to non-blocking io?
154 (output-queue nil)
155 (handler nil)
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)
180 (release-buffer it))
181 (awhen (fd-stream-obuf fd-stream)
182 (setf (fd-stream-obuf fd-stream) nil)
183 (release-buffer it))
184 (dolist (buf (fd-stream-output-queue fd-stream))
185 (when (buffer-p buf)
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)
201 (:include fd-stream
202 (misc #'tracking-stream-misc)
203 (input-char-pos 0))
204 (:copier nil))
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)))
219 (if charpos
220 (let ((index (position charpos newlines :test #'>= :from-end t)))
221 ;; Line numbers traditionally begin at 1, columns at 0.
222 (if index
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
226 (cons (+ index 2)
227 ;; 1 char after the newline = column 0
228 (- charpos (aref newlines index) 1))
229 ;; zero newlines were seen
230 (cons 1 charpos)))
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))
241 (when (< end start)
242 (error ":END before :START!"))
243 (when (> end start)
244 ;; Copy bytes from THING to buffers.
245 (flet ((copy-to-buffer (buffer tail count)
246 (declare (buffer buffer) (index tail count))
247 (aver (plusp count))
248 (let ((sap (buffer-sap buffer)))
249 (etypecase thing
250 (system-area-pointer
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))
258 (incf start count)))
259 (tagbody
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))
272 ((plusp space)
273 (copy-to-buffer obuf tail (min space length))
274 (go :more-output-p))))
275 :flush-and-fill
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))))
283 :more-output-p
284 (when (> 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)))
296 (when obuf
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.
302 (reset-buffer obuf))
303 ((fd-stream-output-queue stream)
304 ;; There is already stuff on the queue -- go directly
305 ;; there.
306 (aver (< head tail))
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.
311 (aver (< head tail))
312 (synchronize-stream-output stream)
313 (loop
314 (let ((length (- tail head)))
315 (multiple-value-bind (count errno)
316 (sb-unix:unix-write (fd-stream-fd stream) (buffer-sap obuf)
317 head length)
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)
323 nil)
324 (signal-timeout 'io-timeout
325 :stream stream
326 :direction :output
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)))
331 (count
332 ;; Partial write -- update buffer status and
333 ;; queue or wait.
334 (incf head count)
335 (setf (buffer-head obuf) head)
336 (queue-or-wait))
337 #-win32
338 ((eql errno sb-unix:ewouldblock)
339 ;; Blocking, queue or wair.
340 (queue-or-wait))
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)
349 (loop
350 (let ((length (- end start)))
351 (multiple-value-bind (count errno)
352 (sb-unix:unix-write (fd-stream-fd stream) sequence start length)
353 (flet ((wait ()
354 (or (wait-until-fd-usable (fd-stream-fd stream) :output
355 (fd-stream-timeout stream)
356 nil)
357 (signal-timeout 'io-timeout
358 :stream stream
359 :direction :output
360 :seconds (fd-stream-timeout stream)))))
361 (cond ((eql count length)
362 (return t))
363 (count
364 (incf start count)
365 (wait))
366 #-win32
367 ((eql errno sb-unix:ewouldblock)
368 (wait))
369 ;; if interrupted on win32, just try again
370 #+win32 ((eql errno sb-unix:eintr))
372 (simple-stream-perror +write-failed+
373 stream errno))))))))
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."))))
380 (new (get-buffer)))
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
384 ;; would be bad.
385 (setf (fd-stream-obuf stream) new)
386 (cond (queue
387 (nconc queue later))
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)
393 :output
394 (lambda (fd)
395 (declare (ignore fd))
396 (write-output-from-queue stream)))))
397 new))
399 ;;; This is called by the FD-HANDLER for the stream when output is
400 ;;; possible.
401 (defun write-output-from-queue (stream)
402 (aver (fd-stream-serve-events stream))
403 (synchronize-stream-output stream)
404 (let (not-first-p)
405 (tagbody
406 :pop-buffer
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))
411 (aver (>= length 0))
412 (multiple-value-bind (count errno)
413 (sb-unix:unix-write (fd-stream-fd stream) (buffer-sap buffer)
414 head length)
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)
420 (setf not-first-p t)
421 (go :pop-buffer))
423 (let ((handler (fd-stream-handler stream)))
424 (aver handler)
425 (setf (fd-stream-handler stream) nil)
426 (remove-fd-handler handler)))))
427 (count
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)))
433 (not-first-p
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!
439 #+win32
440 (simple-stream-perror +write-failed+ stream errno)
441 #-win32
442 (if (= errno sb-unix:ewouldblock)
443 (bug "Unexpected blocking in WRITE-OUTPUT-FROM-QUEUE.")
444 (simple-stream-perror +write-failed+
445 stream errno))))))))
446 nil)
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))
454 ((< end start)
455 (error ":END before :START!"))
456 ((> end 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!
464 (count
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.
470 #+win32
471 (simple-stream-perror +write-failed+ stream errno)
472 #-win32
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)
485 (case errno
486 (#-win32 #.sb-unix:epipe
487 #+win32 #.sb-win32::error-no-data
488 'broken-pipe)
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)
494 :stream stream
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)
503 (typecase datum
504 (format-control
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
510 arguments))))
512 (define-error-wrapper c-string-encoding-error (external-format code)
513 (error 'c-string-encoding-error
514 :external-format external-format
515 :code code))
517 (macrolet ((sap-ref-octets (sap offset count)
518 `(let ((.buffer.
519 (make-array (the fixnum ,count) :element-type '(unsigned-byte 8))))
520 (%byte-blt ,sap ,offset .buffer. 0 ,count)
521 .buffer.)))
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))
541 (if resyncp
542 (resync)
543 (advance))))
544 (resync ()
545 (fd-stream-resync stream)
546 nil)
547 (advance ()
548 (fd-stream-advance stream stream-unit-count)
549 nil))
550 (if replacement
551 (replacement replacement nil)
552 (restart-case
553 (error 'stream-decoding-error
554 :external-format external-format
555 :stream stream
556 :octets (let ((buffer (fd-stream-ibuf stream)))
557 (sap-ref-octets (buffer-sap buffer)
558 (buffer-head buffer)
559 octet-count)))
560 (attempt-resync ()
561 :report (lambda (stream)
562 (format stream
563 "~@<Attempt to resync the stream at a ~
564 character boundary and continue.~@:>"))
565 (resync))
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 ~
573 and continue.~@:>"))
574 :interactive (lambda ()
575 (read-evaluated-form
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)))))))
588 ) ; end MACROLET
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)))
596 (if newline-pos
597 (setf (fd-stream-output-column stream) (- (length replacement) newline-pos 1))
598 (incf (fd-stream-output-column stream) (length replacement)))))
599 ((unsigned-byte 8))
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))))
612 (if replacement
613 (replacement replacement)
614 (restart-case
615 (error 'stream-encoding-error
616 :external-format external-format
617 :stream stream
618 :code code)
619 (output-nothing ()
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 ()
626 (read-evaluated-form
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)
640 (if (streamp stream)
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)
651 (and
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)))
660 (or (not obuf)
661 (and (zerop (buffer-tail obuf))
662 (not (fd-stream-output-queue stream))))))
664 (defmacro output-wrapper/variable-width ((stream size buffering restart)
665 &body body)
666 (let ((stream-var '#:stream))
667 `(let* ((,stream-var ,stream)
668 (obuf (fd-stream-obuf ,stream-var))
669 (tail (buffer-tail obuf))
670 (size ,size))
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)))
678 ,(if restart
679 `(block output-nothing
680 ,@body
681 (setf (buffer-tail obuf) (+ tail size)))
682 `(progn
683 ,@body
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))))
689 (:full)))))
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)))
703 ,(if restart
704 `(block output-nothing
705 ,@body
706 (setf (buffer-tail obuf) (+ tail ,size)))
707 `(progn
708 ,@body
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))))
714 (:full)))))
716 (defmacro def-output-routines/variable-width
717 ((name-fmt size restart external-format &rest bufferings)
718 &body body)
719 (cons 'progn
720 (mapcan
721 (lambda (buffering)
722 (let ((function
723 (intern (format nil name-fmt (string (car buffering))))))
724 (list
725 `(defun ,function (stream |ch|)
726 (declare (optimize (sb-c:verify-arg-count 0)))
727 (output-wrapper/variable-width (stream ,size ,buffering ,restart)
728 ,@body)
729 ;; return char so WRITE-CHAR can tail-call the stream's output method
730 |ch|)
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*
735 ',(mapcar
736 (lambda (type)
737 (list type
738 (car buffering)
739 function
741 external-format))
742 (cdr buffering)))))))
743 bufferings)))
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)
748 &body body)
749 (cons 'progn
750 (mapcan
751 (lambda (buffering)
752 (let ((function
753 (intern (format nil name-fmt (string (car buffering))))))
754 (list
755 `(defun ,function (stream byte)
756 (declare (optimize (sb-c:verify-arg-count 0)))
757 (output-wrapper (stream ,size ,buffering ,restart)
758 ,@body)
759 ;; return byte so WRITE-BYTE can tail-call the stream's output method
760 byte)
761 `(setf *output-routines*
762 (nconc *output-routines*
763 ',(mapcar
764 (lambda (type)
765 (list type
766 (car buffering)
767 function
768 size
769 nil))
770 (cdr buffering)))))))
771 bufferings)))
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)
779 byte))
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)
787 byte))
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)
795 byte))
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)
803 byte))
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)
811 byte))
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)
819 byte))
821 #+64-bit
822 (progn
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)
829 byte))
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)
836 byte)))
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))
847 (let ((last-newline
848 (string-dispatch (simple-base-string
849 #+sb-unicode
850 (simple-array character (*))
851 string)
852 thing
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)
861 (:full
862 (buffer-output stream thing start end))
863 (:line
864 (buffer-output stream thing start end)
865 (when last-newline
866 (flush-output-buffer stream)))
867 (:none
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))))
876 (if last-newline
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)
882 (:conc-name ef-)
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))
914 (locally
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))
922 (locally
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
938 (keyword :lf)
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)))
956 'character
958 (ef-write-n-bytes-fun entry)
959 (ef-char-size entry)
960 (ef-replacement entry))))
961 (dolist (entry *output-routines*)
962 (when (and (subtypep type (first entry))
963 (eq buffering (second entry))
964 (not (fifth entry)))
965 (return-from pick-output-routine
966 (values (symbol-function (third entry))
967 (first entry)
968 (fourth entry)))))
969 ;; KLUDGE: dealing with the buffering here leads to excessive code
970 ;; explosion.
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
976 (values
977 (ecase buffering
978 (:none
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)
983 (+ j tail))
984 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
985 (:full
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)
990 (+ j tail))
991 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
992 `(unsigned-byte ,i)
993 (/ i 8))))
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
997 (values
998 (ecase buffering
999 (:none
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)
1004 (+ j tail))
1005 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
1006 (:full
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)
1011 (+ j tail))
1012 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
1013 `(signed-byte ,i)
1014 (/ i 8)))))
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
1020 ;;; per element.
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)
1031 #+win32
1032 ;; This answers T at EOF on win32.
1033 (not (sb-win32:handle-listen (fd-stream-fd stream)))
1034 #-win32
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))
1042 (errno 0)
1043 (count 0))
1044 (tagbody
1045 #+win32
1046 (go :main)
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)
1054 (go :main))
1055 ;; These (:CLOSED-FLAME and :READ-ERROR) tags are here so what
1056 ;; we can signal errors outside the WITHOUT-INTERRUPTS.
1057 :closed-flame
1058 (closed-flame stream)
1059 :read-error
1060 (simple-stream-perror "couldn't read from ~S" stream errno)
1061 :wait-for-input
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
1067 :stream stream
1068 :direction :input
1069 :seconds (fd-stream-timeout stream)))
1070 :main
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))))
1083 (without-interrupts
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.
1088 (block nil
1089 (prog1 nil
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.
1100 (setf head 0
1101 tail 0)
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)
1108 (setf head 0
1109 (buffer-head ibuf) head
1110 tail n
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)))
1115 (cond ((null count)
1116 (if (eql errno
1117 #+win32 sb-unix:eintr
1118 #-win32 sb-unix:ewouldblock)
1119 (return :wait-for-input)
1120 (return :read-error)))
1121 ((zerop count)
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))))))))))
1128 count))
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)
1137 (,bytes-var ,bytes)
1138 (,buffer-var (fd-stream-ibuf ,stream-var)))
1139 (loop
1140 (when (>= (- (buffer-tail ,buffer-var)
1141 (buffer-head ,buffer-var))
1142 ,bytes-var)
1143 (return))
1144 (refill-input-buffer ,stream-var)))))
1146 (defmacro input-wrapper/variable-width ((stream bytes eof-error eof-value)
1147 &body read-forms)
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))
1153 (size nil)
1154 (unit ,(if (consp bytes)
1155 (let ((size-info (car bytes)))
1156 (if (consp size-info)
1157 (cadr size-info)
1158 size-info))
1159 bytes)))
1160 (block use-instead
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))
1168 ((not ,retry-var))
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)))
1173 (when (= pointer 0)
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)))
1177 (unless
1178 (catch 'eof-input-catcher
1179 (setf decode-break-reason
1180 (block decode-break-reason
1181 ,(if (consp bytes)
1182 (let ((size-info (car bytes)))
1183 (if (consp size-info)
1184 `(progn
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))
1201 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))))))
1216 (cond (,element-var
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)
1231 (let ((,element-var
1232 (catch 'eof-input-catcher
1233 (input-at-least ,stream-var ,bytes)
1234 (locally ,@read-forms))))
1235 (cond (,element-var
1236 (incf (buffer-head (fd-stream-ibuf ,stream-var)) ,bytes)
1237 ,element-var)
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)
1243 &rest body)
1244 `(progn
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)))
1250 ,@body)))
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)
1257 &rest body)
1258 `(progn
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)))
1264 ,@body)))
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))
1304 #+64-bit
1305 (progn
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)
1320 'character
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))
1330 (first entry)
1331 (third 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
1338 (values
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)
1344 with result = 0
1345 do (setf result
1346 (+ (* 256 result)
1347 (sap-ref-8 sap (+ head j))))
1348 finally (return result)))))
1349 `(unsigned-byte ,i)
1350 (/ i 8))))
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
1354 (values
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)
1360 with result = 0
1361 do (setf result
1362 (+ (* 256 result)
1363 (sap-ref-8 sap (+ head j))))
1364 finally (return (if (logbitp (1- i) result)
1365 (dpb result (byte i 0) -1)
1366 result))))))
1367 `(signed-byte ,i)
1368 (/ i 8)))))
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))
1381 (do ()
1382 (nil)
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)))
1403 (if eof-error-p
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.
1408 ))))
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
1420 ;; deal with EOF.
1421 (setf (buffer-head buffer) (buffer-tail buffer))
1422 nil))
1424 (defun fd-stream-resync (stream)
1425 (let ((entry (get-external-format (fd-stream-external-format stream))))
1426 (when entry
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))))
1431 (when entry
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)))
1440 (when sizer
1441 (loop for char across string
1442 for size = (funcall sizer char)
1443 when (null size) do (return nil)
1444 summing size))))
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))))
1470 `(progn
1471 (define-unibyte-mapper ,->code-name ,code->-name
1472 ,@exceptions)
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)
1486 t #\? 1
1487 ,out-form
1489 ,in-form
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")))
1514 `(progn
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))
1527 (do ()
1528 ((= end start))
1529 (let ((obuf (fd-stream-obuf stream)))
1530 (string-dispatch (simple-base-string
1531 #+sb-unicode (simple-array character (*))
1532 string)
1533 string
1534 (let ((len (buffer-length obuf))
1535 (sap (buffer-sap obuf))
1536 ;; FIXME: Rename
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)
1543 `(progn))
1544 (do* ()
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))
1550 ,out-expr
1551 (incf tail size)
1552 (setf (buffer-tail obuf) tail)
1553 (incf start)))
1554 (go flush))
1555 ;; Exited via RETURN-FROM OUTPUT-NOTHING: skip the current character.
1556 (incf start))))
1557 flush
1558 (when (< start end)
1559 (flush-output-buffer stream)))
1560 (when flush-p
1561 (flush-output-buffer stream))))
1562 (def-output-routines/variable-width (,format
1563 ,out-size-expr
1564 ,output-restart
1565 ,external-format
1566 (:none character)
1567 (:line character)
1568 (:full character))
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)))
1576 ,out-expr))
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)
1594 (incf total-copied)
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
1619 ;; us.
1620 (requested-refill nil))
1621 (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)
1630 (cadr size-info)
1631 size-info))
1632 in-size-expr)))
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))
1643 size-info)))
1644 `((when (> ,want (- tail head))
1645 (setf requested-refill t)
1646 (return)))))
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))
1652 (return))
1653 (let ((index (+ start total-copied)))
1654 (setf (aref buffer index) ,in-expr)
1655 (setf (aref sbuffer index) size))
1656 (incf total-copied)
1657 (incf head size))
1658 nil))
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)
1671 (if eof-error-p
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))))
1687 (if eof-error-p
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.
1692 ))))))
1693 (def-input-routine/variable-width ,in-char-function (character
1694 ,external-format
1695 ,in-size-expr
1696 sap head)
1697 (let ((byte (sap-ref-8 sap head)))
1698 (declare (ignorable byte))
1699 ,in-expr))
1700 (defun ,resync-function (stream)
1701 (declare (optimize (sb-c:verify-arg-count 0)))
1702 (let ((ibuf (fd-stream-ibuf stream))
1703 size
1704 (unit ,(if (consp in-size-expr)
1705 (let ((size-info (car in-size-expr)))
1706 (if (consp size-info)
1707 (cadr size-info)
1708 size-info))
1709 in-size-expr)))
1710 (catch 'eof-input-catcher
1711 (loop
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)
1716 `(progn
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))
1732 ,in-expr)
1733 nil)
1734 (return))))))
1735 (defun ,read-c-string-function (sap element-type)
1736 (declare (type system-area-pointer sap)
1737 (optimize (sb-c:verify-arg-count 0)))
1738 (locally
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)
1748 (cadr in-size-expr)
1749 in-size-expr)
1750 |ch| ,in-expr)
1751 (incf head size)
1752 nil))
1753 (when decode-break-reason
1754 (c-string-decoding-error
1755 ,name sap head decode-break-reason))
1756 (when (zerop (char-code |ch|))
1757 (return count))))
1758 (string (case element-type
1759 (base-char
1760 (make-string length :element-type 'base-char))
1761 (character
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))
1770 (setf head 0)
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)
1776 (cadr in-size-expr)
1777 in-size-expr)
1778 |ch| ,in-expr)
1779 (incf head size)
1780 nil))
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)
1790 string)))
1792 (locally
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)))
1801 (buffer-length
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)
1806 null-size))
1807 (tail 0)
1808 (,n-buffer (make-array buffer-length
1809 :element-type '(unsigned-byte 8)))
1810 ;; For external-format-encoding-error
1811 (stream ',name))
1812 (declare (type index length buffer-length tail)
1813 (ignorable stream))
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
1821 do (prog1
1822 ,out-expr
1823 (incf tail size)))
1824 (let* ((bits 0)
1825 (|ch| (code-char bits)) ; more wtf
1826 (size null-size))
1827 (declare (ignorable bits |ch| size))
1828 ,out-expr)))
1829 ,n-buffer))))))
1831 (register-external-format
1832 ',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)
1864 (t element-type)))
1865 (character-stream-p (subtypep target-type 'character))
1866 (bivalent-stream-p (eq element-type :default))
1867 char-size
1868 replacement
1869 (bin-routine #'ill-bin)
1870 (bin-type nil)
1871 (bin-size nil)
1872 (cin-routine #'ill-in)
1873 (cin-type nil)
1874 (cin-size nil)
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)
1879 (bout-type nil)
1880 (bout-size nil)
1881 (cout-routine #'ill-out)
1882 (cout-type nil)
1883 (cout-size nil)
1884 (output-type nil)
1885 (output-size nil)
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)))
1891 (if output-p
1892 (if obuf
1893 (reset-buffer obuf)
1894 (setf (fd-stream-obuf fd-stream) (get-buffer)))
1895 (when obuf
1896 (setf (fd-stream-obuf fd-stream) nil)
1897 (release-buffer obuf))))
1899 (let ((ibuf (fd-stream-ibuf fd-stream)))
1900 (if input-p
1901 (if ibuf
1902 (reset-buffer ibuf)
1903 (setf (fd-stream-ibuf fd-stream) (get-buffer)))
1904 (when ibuf
1905 (setf (fd-stream-ibuf fd-stream) nil)
1906 (release-buffer ibuf))))
1908 ;; FIXME: Why only for output? Why unconditionally?
1909 (when output-p
1910 (setf (fd-stream-output-column fd-stream) 0))
1912 (when input-p
1913 (flet ((no-input-routine ()
1914 (error "could not find any input routine for ~
1915 ~/sb-impl:print-type-specifier/"
1916 target-type)))
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)
1921 target-type)
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))
1934 (when char-size
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))
1941 read-n-characters
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.
1949 (when (and buffer-p
1950 (not bivalent-stream-p)
1951 ;; temporary disable on :io streams
1952 (not output-p))
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))))))))
1965 (when output-p
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)
1973 :full
1974 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)
1979 target-type)))
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)
1989 target-type)))
1990 (when char-size
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)
2013 input-type)
2014 ((null output-type)
2015 input-type)
2016 ((null input-type)
2017 output-type)
2018 ((subtypep input-type output-type)
2019 input-type)
2020 ((subtypep output-type input-type)
2021 output-type)
2023 (error "Input type (~/sb-impl:print-type-specifier/) and ~
2024 output type (~/sb-impl:print-type-specifier/) ~
2025 are unrelated?"
2026 input-type output-type))))))
2028 ;;; Handles the resource-release aspects of stream closing, and marks
2029 ;;; it as closed.
2030 (defun release-fd-stream-resources (fd-stream)
2031 (handler-case
2032 (without-interrupts
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)
2049 (error 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
2056 ;;; bytes.
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)))
2063 (if ibuf
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)
2071 #+win32
2072 (progn
2073 (sb-win32:handle-clear-input (fd-stream-fd stream))
2074 (setf (fd-stream-listen stream) nil))
2075 #-win32
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)
2086 (:listen
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
2098 ;; at EOF.
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
2102 ;; blocking
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
2111 ;; applies.
2112 (do-listen)))))))
2113 (do-listen)))
2114 (:unread
2115 (let* ((ibuf (fd-stream-ibuf fd-stream))
2116 (head (buffer-head ibuf))
2117 (prev (buffer-prev-head ibuf)))
2118 (if (= head prev)
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)))))
2125 (:close
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)
2131 (cond (arg1
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)
2144 (if orig
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
2153 ;; all be the same?
2154 (unless okay
2155 (simple-stream-perror
2156 "~@<Couldn't restore ~S to its original ~
2157 contents from ~S while closing ~S~:>"
2158 fd-stream err
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
2170 ;; racy).
2171 (multiple-value-bind (okay err)
2172 (sb-unix:unix-unlink file)
2173 (unless okay
2174 (file-perror
2175 file err
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)
2182 (unless okay
2183 (file-perror
2184 orig err
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))))
2190 (:clear-input
2191 (fd-stream-clear-input fd-stream))
2192 (:force-output
2193 (flush-output-buffer fd-stream))
2194 (:finish-output
2195 (finish-fd-stream-output fd-stream))
2196 (:element-type
2197 (fd-stream-element-type fd-stream))
2198 (:element-mode
2199 (fd-stream-element-mode fd-stream))
2200 (:external-format
2201 (fd-stream-external-format fd-stream))
2202 (:interactive-p
2203 (plusp (the (integer 0)
2204 (sb-unix:unix-isatty (fd-stream-fd fd-stream)))))
2205 (:line-length
2207 (:charpos
2208 (fd-stream-output-column fd-stream))
2209 (:file-length
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
2216 :datum fd-stream
2217 :expected-type 'fd-stream
2218 :format-control "~S is not a stream associated with a file."
2219 :format-arguments (list fd-stream)))
2220 #-win32
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))
2226 (unless okay
2227 (simple-stream-perror "failed Unix fstat(2) on ~S" fd-stream dev))
2228 (if (zerop mode)
2230 (truncate size (fd-stream-element-size fd-stream))))
2231 #+win32
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)
2236 (if (zerop got)
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
2246 (etypecase arg1
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)
2267 (do ()
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))
2274 (without-interrupts
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
2285 ;; yet.
2286 (dolist (buffer (fd-stream-output-queue stream))
2287 (incf posn (- (buffer-tail buffer) (buffer-head buffer))))
2288 (let ((obuf (fd-stream-obuf stream)))
2289 (when obuf
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)))
2297 (when ibuf
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")
2307 (tagbody
2308 :again
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
2314 ;; won't screw us.
2315 (without-interrupts
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...
2321 (go :again))
2322 ;; Clear out any pending input to force the next read to go to
2323 ;; the disk.
2324 (flush-input-buffer stream)
2325 ;; Trash cached value for listen, so that we check next time.
2326 (setf (fd-stream-listen stream) nil)
2327 ;; Now move it.
2328 (multiple-value-bind (offset origin)
2329 (case position-spec
2330 (:start
2331 (values 0 sb-unix:l_set))
2332 (:end
2333 (values 0 sb-unix:l_xtnd))
2335 (values (* position-spec (fd-stream-element-size stream))
2336 sb-unix:l_set)))
2337 (declare (type (alien sb-unix:unix-offset) offset))
2338 (let ((posn (sb-unix:unix-lseek (fd-stream-fd stream)
2339 offset origin)))
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
2347 ;; output fails.
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
2366 ;;; IO-TIMEOUT.
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
2375 &key
2376 (class 'fd-stream)
2377 (input nil input-p)
2378 (output nil output-p)
2379 (element-type 'base-char)
2380 (buffering :full)
2381 (external-format :default)
2382 serve-events
2383 timeout
2384 file
2385 original
2386 delete-original
2387 pathname
2388 input-buffer-p
2389 dual-channel-p
2390 (name (if file
2391 (format nil "file ~A" file)
2392 (format nil "descriptor ~W" fd)))
2393 auto-close)
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
2401 ;; to close the FD.
2402 (let* ((defaulted-external-format (if (eql external-format :default)
2403 (default-external-format)
2404 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
2409 (unwind-protect
2410 (error "Undefined external-format: ~S" external-format)
2411 (sb-unix:unix-close fd)))
2413 (cond ((not (or input-p output-p))
2414 (setf input t))
2415 ((not (or input output))
2416 (unwind-protect
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
2424 :fd fd
2425 :fd-type
2426 #-win32 (sb-unix:fd-type fd)
2427 ;; KLUDGE.
2428 #+win32 (if serve-events
2429 :unknown
2430 :regular)
2431 :name name
2432 :file file
2433 :original original
2434 :delete-original delete-original
2435 :pathname pathname
2436 :buffering buffering
2437 :dual-channel-p dual-channel-p
2438 :element-mode element-mode
2439 :serve-events serve-events
2440 :timeout
2441 (if timeout
2442 (coerce timeout 'single-float)
2443 nil))))
2444 (set-fd-stream-routines stream element-type canonized-external-format external-format-entry
2445 input output input-buffer-p)
2446 (when auto-close
2447 (finalize stream
2448 (lambda ()
2449 (sb-unix:unix-close fd)
2450 #+sb-show
2451 (format *terminal-io* "** closed file descriptor ~W **~%"
2452 fd))
2453 :dont-save t))
2454 stream)))
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)
2469 (if okay
2471 (file-perror
2472 namestring err
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)))
2480 (restart-case
2481 (case errno
2482 (#-win32 #.sb-unix:enoent
2483 #+win32 #.sb-win32::error_file_not_found
2484 (case if-does-not-exist
2485 (:error
2486 (restart-case
2487 (signal-it 'file-does-not-exist)
2488 (create ()
2489 :report "Reopen with :if-does-not-exist :create"
2490 '(:new-if-does-not-exist :create))))
2491 (:create
2492 (sb-kernel::%file-error
2493 pathname
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)
2499 '(:return nil)
2500 (restart-case
2501 (signal-it 'file-exists)
2502 (supersede ()
2503 :report "Reopen with :if-exists :supersede"
2504 '(:new-if-exists :supersede))
2505 (overwrite ()
2506 :report "Reopen with :if-exists :overwrite"
2507 '(:new-if-exists :overwrite))
2508 (rename ()
2509 :report "Reopen with :if-exists :rename"
2510 '(:new-if-exists :rename))
2511 (append ()
2512 :report "Reopen with :if-exists :append"
2513 '(:new-if-exists :append)))))
2515 (signal-it "Error opening ~S" pathname)))
2516 (continue ()
2517 :report "Retry opening."
2518 '())
2519 (use-value (value)
2520 :report "Try opening a different file."
2521 :interactive read-evaluated-form
2522 (list :new-filename (the pathname-designator value))))))
2524 (defun open (filename
2525 &key
2526 (direction :input)
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
2532 (class 'fd-stream)
2533 #+win32
2534 (overlapped t)
2535 &aux
2536 ;; Squelch assignment warning.
2537 (filename filename)
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.
2542 Defined keywords:
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)
2551 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.
2558 (loop
2559 (multiple-value-bind (input output mask)
2560 (ecase direction
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
2574 (or (not input)
2575 (and input (eq if-does-not-exist :create))
2576 (and (eq direction :io)
2577 (not if-does-not-exist-given))))
2578 physical)))
2579 ;; Process if-exists argument if we are doing any output.
2580 (cond (output
2581 (unless if-exists-given
2582 (setf if-exists
2583 (if (eq (pathname-version pathname) :newest)
2584 :new-version
2585 :error)))
2586 (case if-exists
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)))
2591 ((:supersede)
2592 (setf mask (logior mask sb-unix:o_trunc)))
2593 (:append
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)
2601 ((and output
2602 (member if-exists '(:overwrite :append)))
2603 :error)
2604 ((eq direction :probe)
2605 nil)
2607 :create))))
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))
2619 (return-from open))
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))
2625 (existsp
2626 (if if-exists
2627 (sb-kernel::%file-error pathname 'file-exists)
2628 (return)))
2629 (if-does-not-exist
2630 (sb-kernel::%file-error pathname 'file-does-not-exist))
2632 (return)))
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
2639 namestring)))
2640 (delete-original (eq if-exists :rename-and-delete))
2641 (mode #o666))
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.
2646 (let ((exists
2647 (and namestring
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))
2652 (cond
2653 (okay
2654 (when (and output (= (logand orig-mode #o170000)
2655 #o40000))
2656 (file-perror
2657 pathname nil
2658 "Can't open ~S for output: is a directory"
2659 pathname))
2660 (setf mode (logand orig-mode #o777))
2662 ((eql err/dev sb-unix:enoent)
2663 nil)
2665 (file-perror namestring err/dev
2666 "Can't find ~S" namestring)))))))
2667 (unless (and exists
2668 (rename-the-old-one namestring original))
2669 (setf original nil)
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)
2676 (setf mask
2677 (logior (logandc2 mask sb-unix:o_creat)
2678 sb-unix:o_trunc)))
2679 (setf if-exists :supersede))))
2681 ;; Now we can try the actual Unix open(2).
2682 (multiple-value-bind (fd errno)
2683 (if namestring
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))
2688 (when (numberp fd)
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))
2695 (make-fd-stream fd
2696 :class class
2697 :input input
2698 :output output
2699 :element-type element-type
2700 :external-format canonized-external-format
2701 :file namestring
2702 :original original
2703 :delete-original delete-original
2704 :pathname pathname
2705 :dual-channel-p nil
2706 :serve-events nil
2707 :input-buffer-p t
2708 :auto-close t))
2709 (:probe
2710 (let ((stream
2711 (%make-fd-stream :name namestring
2712 :fd fd
2713 :pathname pathname
2714 :element-type element-type)))
2715 (close stream)
2716 stream)))))
2717 (destructuring-bind (&key (return nil returnp)
2718 new-filename
2719 new-if-exists
2720 new-if-does-not-exist)
2721 (%open-error pathname errno if-exists if-does-not-exist)
2722 (when returnp
2723 (return return))
2724 (when new-filename
2725 (setf filename new-filename))
2726 (when new-if-exists
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))))))))))
2731 ;;;; miscellany
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)
2744 :gray nil
2745 :native
2746 (when (typep stream 'fd-stream)
2747 (cond (new-name
2748 (setf (fd-stream-pathname stream) new-name)
2749 (setf (fd-stream-file stream)
2750 (native-namestring (physicalize-pathname new-name)
2751 :as-file t))
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)))
2764 ((>= i end)
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))
2772 (incf 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))))