/[cmucl]/src/code/fd-stream.lisp
ViewVC logotype

Contents of /src/code/fd-stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.121 - (show annotations)
Tue Oct 12 21:52:44 2010 UTC (3 years, 6 months ago) by rtoy
Branch: MAIN
Changes since 1.120: +43 -18 lines
Make read-char and read-byte signal errors when given the wrong kind
of streams.  This is a change from current 20a and 20b behavior which
didn't signal errors, but matches the behavior for releases 19f and
earlier.

But allow them to work on binary-text-streams.  This is the same
behavior as before for binary-text-stream streams.

However, read-sequence no longer allows reading from streams into
arbitrary objects, unless the stream is a binary-text-stream stream.

code/fd-stream-extfmt.lisp:
o In %SET-FD-STREAM-EXTERNAL-FORMAT, only update
  fd-stream-in/fd-stream-out if we have a character or
  binary-text-stream stream.
o Don't update the fd-stream-string-buffer or lisp-stream-in-buffer if
  we have a binary-text-stream because that will mess up how
  fast-read-char and fast-read-byte dispatch to do the right thing for
  binary-text-stream streams.

code/fd-stream.lisp:
o Set the fd-stream-in and fd-stream-bin slots appropriately depending
  on whether we have a character, binary, or binary-text-stream
  stream.
o Only create the lisp-stream-in-buffer if we do NOT have a
  binary-text-stream.  (Binary streams didn't use the
  lisp-stream-buffer previously, so no change there.  Character
  streams use the lisp-stream-buffer and/or lisp-string-buffer.)
o Set the fd-stream-flags appropriately for the kind of stream this
  is.  Checking a fixnum is faster than checking the type of a stream.

code/struct.lisp:
o Add FLAGS slot to LISP-STREAM so we can tell what kind of stream
  (character, binary, binary-text-stream) we have.

code/sysmacs.lisp:
o Change FAST-READ-CHAR so that if we have a have a binary or
  binary-text-stream stream, we dispatch to the fast-read-char methods
  to do the right thing, including signaling an error for the wrong
  kind of stream.
o Change FAST-READ-BYTE so that if we do not have a binary stream, we
  dispatch to the fast-read-char method to do the right thing.

compiler/dump.lisp:
o With the above changes, we can no longer write characters to a
  binary stream, like a FASL file.  Make the fasl file a
  binary-text-stream so that we can.  (Alternatively, we could create
  the FASL header as a string, convert to octets and dump the octest
  to the file.  This is easier, and should still be fast for writing
  fasls.)
1 ;;; -*- Log: code.log; Package: LISP -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/fd-stream.lisp,v 1.121 2010/10/12 21:52:44 rtoy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Streams for UNIX file descriptors.
13 ;;;
14 ;;; Written by William Lott, July 1989 - January 1990.
15 ;;; Some tuning by Rob MacLachlan.
16 ;;;
17 ;;; **********************************************************************
18
19
20 (in-package "SYSTEM")
21
22 (intl:textdomain "cmucl")
23
24 (export '(fd-stream fd-stream-p fd-stream-fd make-fd-stream
25 io-timeout beep *beep-function* output-raw-bytes
26 *tty* *stdin* *stdout* *stderr*
27 binary-text-stream))
28
29
30 (in-package "EXTENSIONS")
31
32 (export '(*backup-extension*))
33
34
35 (in-package "LISP")
36
37 (export '(file-stream file-string-length))
38
39
40 ;;;; Buffer manipulation routines.
41
42 (defvar *available-buffers* ()
43 "List of available buffers. Each buffer is an sap pointing to
44 bytes-per-buffer of memory.")
45
46 (defvar lisp::*enable-stream-buffer-p* nil)
47
48 (defconstant bytes-per-buffer (* 4 1024)
49 "Number of bytes per buffer.")
50
51 ;; This limit is rather arbitrary
52 (defconstant max-stream-element-size 1024
53 "The maximum supported byte size for a stream element-type.")
54
55 ;;; NEXT-AVAILABLE-BUFFER -- Internal.
56 ;;;
57 ;;; Returns the next available buffer, creating one if necessary.
58 ;;;
59 (declaim (inline next-available-buffer))
60 ;;;
61 (defun next-available-buffer ()
62 (if *available-buffers*
63 (pop *available-buffers*)
64 (allocate-system-memory bytes-per-buffer)))
65
66 (declaim (inline buffer-sap bref (setf bref) buffer-copy))
67
68 (defun buffer-sap (thing &optional offset)
69 (declare (type simple-stream-buffer thing) (type (or fixnum null) offset)
70 (optimize (speed 3) (space 2) (debug 0) (safety 0)
71 ;; Suppress the note about having to box up the return:
72 (ext:inhibit-warnings 3)))
73 (let ((sap (if (vectorp thing) (sys:vector-sap thing) thing)))
74 (if offset (sys:sap+ sap offset) sap)))
75
76 (defun bref (buffer index)
77 (declare (type simple-stream-buffer buffer)
78 (type (integer 0 #.most-positive-fixnum) index))
79 (sys:sap-ref-8 (buffer-sap buffer) index))
80
81 (defun (setf bref) (octet buffer index)
82 (declare (type (unsigned-byte 8) octet)
83 (type simple-stream-buffer buffer)
84 (type (integer 0 #.most-positive-fixnum) index))
85 (setf (sys:sap-ref-8 (buffer-sap buffer) index) octet))
86
87 (defun buffer-copy (src soff dst doff length)
88 (declare (type simple-stream-buffer src dst)
89 (type fixnum soff doff length))
90 (sys:without-gcing ;; is this necessary??
91 (kernel:system-area-copy (buffer-sap src) (* soff 8)
92 (buffer-sap dst) (* doff 8)
93 (* length 8))))
94
95 #-(or big-endian little-endian)
96 (eval-when (:compile-toplevel)
97 (push (c::backend-byte-order c::*target-backend*) *features*))
98
99 (defun vector-elt-width (vector)
100 ;; Return octet-width of vector elements
101 (etypecase vector
102 ;; (simple-array fixnum (*)) not supported
103 ;; (simple-array base-char (*)) treated specially; don't call this
104 ((simple-array bit (*)) 1/8)
105 ((simple-array (unsigned-byte 2) (*)) 1/4)
106 ((simple-array (unsigned-byte 4) (*)) 1/2)
107 ((simple-array (signed-byte 8) (*)) 1)
108 ((simple-array (unsigned-byte 8) (*)) 1)
109 ((simple-array (signed-byte 16) (*)) 2)
110 ((simple-array (unsigned-byte 16) (*)) 2)
111 ((simple-array (signed-byte 32) (*)) 4)
112 ((simple-array (unsigned-byte 32) (*)) 4)
113 ((simple-array single-float (*)) 4)
114 ((simple-array double-float (*)) 8)
115 ((simple-array (complex single-float) (*)) 8)
116 ((simple-array (complex double-float) (*)) 16)
117 #+long-float
118 ((simple-array long-float (*)) 10)
119 #+long-float
120 ((simple-array (complex long-float) (*)) 20)
121 #+double-double
122 ((simple-array double-double-float (*)) 16)
123 #+double-double
124 ((simple-array (complex double-double-float) (*)) 32)))
125
126 (defun endian-swap-value (vector endian-swap)
127 (case endian-swap
128 (:network-order
129 #+big-endian 0
130 ;; This is needed because the little-endian (x86) architectures
131 ;; store the lowest indexed element in the least significant part
132 ;; of a byte. On a big-endian machine (sparc, ppc), the lowest
133 ;; indexed element is at the most significant part of a byte.
134 #+little-endian
135 (typecase vector
136 ((array (unsigned-byte 4) (*))
137 -1)
138 ((array (unsigned-byte 2) (*))
139 -2)
140 ((array (unsigned-byte 1) (*))
141 -8)
142 (t
143 (1- (vector-elt-width vector)))))
144 (:byte-8 0)
145 (:byte-16 1)
146 (:byte-32 3)
147 (:byte-64 7)
148 (:byte-128 15)
149 ;; additions by Lynn Quam
150 (:machine-endian 0)
151 (:big-endian
152 #+big-endian 0
153 #+little-endian
154 (typecase vector
155 ((array (unsigned-byte 4) (*))
156 -1)
157 ((array (unsigned-byte 2) (*))
158 -2)
159 ((array (unsigned-byte 1) (*))
160 -8)
161 (t
162 (1- (vector-elt-width vector)))))
163 (:little-endian
164 #+big-endian
165 (typecase vector
166 ((array (unsigned-byte 4) (*))
167 -1)
168 ((array (unsigned-byte 2) (*))
169 -2)
170 ((array (unsigned-byte 1) (*))
171 -8)
172 (t
173 (1- (vector-elt-width vector))))
174 #+little-endian 0)
175 (otherwise endian-swap)))
176
177
178 ;;;; The FD-STREAM structure.
179
180 ;;;; Superclass defined by the ANSI Spec
181 (defstruct (file-stream
182 (:include lisp-stream)
183 (:constructor nil)
184 (:copier nil)))
185
186 (defstruct (fd-stream
187 (:print-function %print-fd-stream)
188 (:constructor %make-fd-stream)
189 (:include file-stream
190 (misc #'fd-stream-misc-routine)))
191
192 (name nil) ; The name of this stream
193 (file nil) ; The file this stream is for
194 ;;
195 ;; The backup file namestring for the old file, for :if-exists :rename or
196 ;; :rename-and-delete.
197 (original nil :type (or simple-string null))
198 (delete-original nil) ; for :if-exists :rename-and-delete
199 ;;
200 ;;; Number of bytes per element.
201 (element-size 1 :type index)
202 (element-type 'base-char) ; The type of element being transfered.
203 (fd -1 :type fixnum) ; The file descriptor
204 ;;
205 ;; Controls when the output buffer is flushed.
206 (buffering :full :type (member :full :line :none))
207 ;;
208 ;; Character position if known.
209 (char-pos nil :type (or index null))
210 ;;
211 ;; T if input is waiting on FD. :EOF if we hit EOF.
212 (listen nil :type (member nil t :eof))
213 ;;
214 ;; The input buffer.
215 (unread nil)
216 (ibuf-sap nil :type (or system-area-pointer null))
217 (ibuf-length nil :type (or index null))
218 (ibuf-head 0 :type index)
219 (ibuf-tail 0 :type index)
220
221 ;; The output buffer.
222 (obuf-sap nil :type (or system-area-pointer null))
223 (obuf-length nil :type (or index null))
224 (obuf-tail 0 :type index)
225
226 ;; Output flushed, but not written due to non-blocking io.
227 (output-later nil)
228 (handler nil)
229 ;;
230 ;; Timeout specified for this stream, or NIL if none.
231 (timeout nil :type (or index null))
232 ;;
233 ;; Pathname of the file this stream is opened to (returned by PATHNAME.)
234 (pathname nil :type (or pathname null))
235 ;;
236 ;; External format support
237 ;;
238 ;; @@ I want to use :default here, but keyword pkg isn't set up yet at boot
239 ;; so initialize to NIL and fix it in SET-ROUTINES
240 #+unicode
241 (external-format nil :type (or null keyword cons))
242 ;;
243 ;; State for octets-to-char (for reading from a stream). The
244 ;; contents of the state can be anything and is defined by the
245 ;; external format.
246 #+unicode
247 (oc-state nil)
248 ;;
249 ;; State for char-to-octets (for writing to a stream). The contents
250 ;; of the state can be anything and is defined by the external
251 ;; format. If not NIL, then the CAR is used by char-to-octets to
252 ;; hold some state information, and the CDR is available to the
253 ;; external format to hold whatever state information is needed.
254 #+unicode
255 (co-state nil)
256 #+unicode
257 (last-char-read-size 0 :type index)
258 ;;
259 ;; The number of octets in in-buffer. Normally equal to
260 ;; in-buffer-length, but could be less if we reached the
261 ;; end-of-file.
262 #+unicode
263 (in-length 0 :type index)
264 ;;
265 ;; Indicates how to handle errors when converting octets to
266 ;; characters. If NIL, then the external format should handle it
267 ;; itself, doing whatever is deemed appropriate. If non-NIL, this
268 ;; should be a function (or symbol) that the external format can
269 ;; funcall to deal with the error. The function should take three
270 ;; arguments: a message string, the offending octet, and the number
271 ;; of octets read so far in decoding; if the function returns it
272 ;; should return the codepoint of the desired replacement character.
273 (octets-to-char-error nil :type (or null symbol function))
274 ;;
275 ;; Like OCTETS-TO-CHAR-ERROR, but for converting characters to
276 ;; octets for output. The function takes two arguments: a message
277 ;; string and the codepoint that cannot be converted. The function
278 ;; should return the octet that should be output.
279 (char-to-octets-error nil :type (or null symbol function)))
280
281 (defun %print-fd-stream (fd-stream stream depth)
282 (declare (ignore depth) (stream stream))
283 (format stream "#<Stream for ~A>"
284 (fd-stream-name fd-stream)))
285
286 ;; CMUCL extension. This is a FD-STREAM, but it allows reading and
287 ;; writing of 8-bit characters and unsigned bytes from the stream.
288 (defstruct (binary-text-stream
289 (:print-function %print-binary-text-stream)
290 (:constructor %make-binary-text-stream)
291 (:include fd-stream)))
292
293 (defun %print-binary-text-stream (fd-stream stream depth)
294 (declare (ignore depth) (stream stream))
295 (format stream "#<Binary-text Stream for ~A>"
296 (fd-stream-name fd-stream)))
297
298 (define-condition io-timeout (stream-error)
299 ((direction :reader io-timeout-direction :initarg :direction))
300 (:report
301 (lambda (condition stream)
302 (declare (stream stream))
303 (format stream (intl:gettext "Timeout ~(~A~)ing ~S.")
304 (io-timeout-direction condition)
305 (stream-error-stream condition)))))
306
307
308 ;;;; Output routines and related noise.
309
310 (defvar *output-routines* ()
311 "List of all available output routines. Each element is a list of the
312 element-type output, the kind of buffering, the function name, and the number
313 of bytes per element.")
314
315 ;;; DO-OUTPUT-LATER -- internal
316 ;;;
317 ;;; Called by the server when we can write to the given file descriptor.
318 ;;; Attempt to write the data again. If it worked, remove the data from the
319 ;;; output-later list. If it didn't work, something is wrong.
320 ;;;
321 (defun do-output-later (stream)
322 (let* ((stuff (pop (fd-stream-output-later stream)))
323 (base (car stuff))
324 (start (cadr stuff))
325 (end (caddr stuff))
326 (reuse-sap (cadddr stuff))
327 (length (- end start)))
328 (declare (type index start end length))
329 (multiple-value-bind
330 (count errno)
331 (unix:unix-write (fd-stream-fd stream)
332 base
333 start
334 length)
335 (cond ((not count)
336 (if (= errno unix:ewouldblock)
337 (error (intl:gettext "Write would have blocked, but SERVER told us to go."))
338 (error (intl:gettext "While writing ~S: ~A")
339 stream (unix:get-unix-error-msg errno))))
340 ((eql count length) ; Hot damn, it worked.
341 (when reuse-sap
342 (push base *available-buffers*)))
343 ((not (null count)) ; Sorta worked.
344 (push (list base
345 (the index (+ start count))
346 end)
347 (fd-stream-output-later stream))))))
348 (unless (fd-stream-output-later stream)
349 (system:remove-fd-handler (fd-stream-handler stream))
350 (setf (fd-stream-handler stream) nil)))
351
352 ;;; OUTPUT-LATER -- internal
353 ;;;
354 ;;; Arrange to output the string when we can write on the file descriptor.
355 ;;;
356 (defun output-later (stream base start end reuse-sap)
357 (cond ((null (fd-stream-output-later stream))
358 (setf (fd-stream-output-later stream)
359 (list (list base start end reuse-sap)))
360 (setf (fd-stream-handler stream)
361 (system:add-fd-handler (fd-stream-fd stream)
362 :output
363 #'(lambda (fd)
364 (declare (ignore fd))
365 (do-output-later stream)))))
366 (t
367 (nconc (fd-stream-output-later stream)
368 (list (list base start end reuse-sap)))))
369 (when reuse-sap
370 (let ((new-buffer (next-available-buffer)))
371 (setf (fd-stream-obuf-sap stream) new-buffer)
372 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
373
374 ;;; DO-OUTPUT -- internal
375 ;;;
376 ;;; Output the given noise. Check to see if there are any pending writes. If
377 ;;; so, just queue this one. Otherwise, try to write it. If this would block,
378 ;;; queue it.
379 ;;;
380 (defun do-output (stream base start end reuse-sap)
381 (declare (type fd-stream stream)
382 (type (or system-area-pointer (simple-array * (*))) base)
383 (type index start end))
384 (if (not (null (fd-stream-output-later stream))) ; something buffered.
385 (progn
386 (output-later stream base start end reuse-sap)
387 ;; ### check to see if any of this noise can be output
388 )
389 (let ((length (- end start)))
390 (multiple-value-bind
391 (count errno)
392 (unix:unix-write (fd-stream-fd stream) base start length)
393 (cond ((not count)
394 (if (= errno unix:ewouldblock)
395 (output-later stream base start end reuse-sap)
396 (error 'simple-stream-error
397 :stream stream
398 :format-control "while writing: ~A"
399 :format-arguments (list (unix:get-unix-error-msg errno)))))
400 ((not (eql count length))
401 (output-later stream base (the index (+ start count))
402 end reuse-sap)))))))
403
404 #+unicode
405 (stream::def-ef-macro ef-flush (extfmt lisp stream::+ef-max+ stream::+ef-flush+)
406 `(lambda (stream)
407 (declare (type fd-stream stream))
408 (let* ((tail (fd-stream-obuf-tail stream)))
409 (declare (type index tail))
410 (cond
411 ((stream::ef-flush-state ,(stream::find-external-format extfmt))
412 (let* ((sap (fd-stream-obuf-sap stream))
413 (len (fd-stream-obuf-length stream)))
414 (declare (type sys:system-area-pointer sap)
415 (type index len)
416 (ignorable sap len))
417 (stream::flush-state ,extfmt
418 (fd-stream-co-state stream)
419 (lambda (byte)
420 (when (= tail len)
421 (do-output stream sap 0 tail t)
422 (setq sap (fd-stream-obuf-sap stream)
423 tail 0))
424 (setf (bref sap (1- (incf tail))) byte))
425 (fd-stream-char-to-octets-error stream))
426 (setf (fd-stream-obuf-tail stream) tail)))
427 (t
428 ;; No flush-state function, so just output a replacement
429 ;; character (or signal an error). We hack the co-state to
430 ;; what we need for this to work. This should be ok because
431 ;; we're closing the file anyway.
432 (let* ((state (fd-stream-co-state stream))
433 (c (car state)))
434 (when (and state c)
435 (setf (fd-stream-co-state stream)
436 (cons nil (cdr state)))
437 (funcall (ef-cout (fd-stream-external-format stream))
438 stream
439 ;; Handle bare surrogates or use the
440 ;; replacement character.
441 (if (lisp::surrogatep c)
442 (if (fd-stream-char-to-octets-error stream)
443 (funcall (fd-stream-char-to-octets-error stream)
444 "Flushing bare surrogate #x~4,0X is illegal"
445 (char-code c))
446 (code-char stream:+replacement-character-code+))
447 c))))))
448 (values))))
449
450 ;;; FLUSH-OUTPUT-BUFFER -- internal
451 ;;;
452 ;;; Flush any data in the output buffer.
453 ;;;
454 (defun flush-output-buffer (stream)
455 (let ((length (fd-stream-obuf-tail stream)))
456 (unless (= length 0)
457 (do-output stream (fd-stream-obuf-sap stream) 0 length t)
458 (setf (fd-stream-obuf-tail stream) 0))))
459
460 ;;; DEF-OUTPUT-ROUTINES -- internal
461 ;;;
462 ;;; Define output routines that output numbers size bytes long for the
463 ;;; given bufferings. Use body to do the actual output.
464 ;;;
465 (defmacro def-output-routines ((name size &rest bufferings) &body body)
466 (declare (optimize (speed 1)))
467 (cons 'progn
468 (mapcar
469 #'(lambda (buffering)
470 (let ((function
471 (intern (let ((*print-case* :upcase))
472 (format nil name (car buffering))))))
473 `(progn
474 (defun ,function (stream byte)
475 ,(unless (eq (car buffering) :none)
476 `(when (< (fd-stream-obuf-length stream)
477 (+ (fd-stream-obuf-tail stream)
478 ,size))
479 (flush-output-buffer stream)))
480 ;;
481 ;; If there is any input read from UNIX but not
482 ;; supplied to the user of the stream, reposition
483 ;; to the real file position as seen from Lisp.
484 ,(unless (eq (car buffering) :none)
485 `(when (> (fd-stream-ibuf-tail stream)
486 (fd-stream-ibuf-head stream))
487 (file-position stream (file-position stream))))
488 ,@body
489 (incf (fd-stream-obuf-tail stream) ,size)
490 ,(ecase (car buffering)
491 (:none
492 `(flush-output-buffer stream))
493 (:line
494 `(when (eql (char-code byte) (char-code #\Newline))
495 (flush-output-buffer stream)))
496 (:full
497 ))
498 (values))
499 (setf *output-routines*
500 (nconc *output-routines*
501 ',(mapcar
502 #'(lambda (type)
503 (list type
504 (car buffering)
505 function
506 size))
507 (cdr buffering)))))))
508 bufferings)))
509
510 #-unicode
511 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
512 1
513 (:none character)
514 (:line character)
515 (:full character))
516 (if (char= byte #\Newline)
517 (setf (fd-stream-char-pos stream) 0)
518 (incf (fd-stream-char-pos stream)))
519 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
520 (char-code byte)))
521
522 #+unicode
523 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
524 1
525 (:none character)
526 (:line character)
527 (:full character))
528 (if (char= byte #\Newline)
529 (setf (fd-stream-char-pos stream) 0)
530 (incf (fd-stream-char-pos stream)))
531 ;; FIXME! We only use the low 8 bits of a character!
532 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
533 (logand #xff (char-code byte))))
534
535 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
536 1
537 (:none (unsigned-byte 8))
538 (:full (unsigned-byte 8)))
539 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
540 byte))
541
542 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
543 1
544 (:none (signed-byte 8))
545 (:full (signed-byte 8)))
546 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
547 (fd-stream-obuf-tail stream))
548 byte))
549
550 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
551 2
552 (:none (unsigned-byte 16))
553 (:full (unsigned-byte 16)))
554 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
555 byte))
556
557 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
558 2
559 (:none (signed-byte 16))
560 (:full (signed-byte 16)))
561 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
562 (fd-stream-obuf-tail stream))
563 byte))
564
565 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
566 4
567 (:none (unsigned-byte 32))
568 (:full (unsigned-byte 32)))
569 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
570 byte))
571
572 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
573 4
574 (:none (signed-byte 32))
575 (:full (signed-byte 32)))
576 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
577 (fd-stream-obuf-tail stream))
578 byte))
579
580 (stream::def-ef-macro ef-cout (extfmt lisp stream::+ef-max+ stream::+ef-cout+)
581 `(lambda (stream char)
582 (declare (type fd-stream stream)
583 (type character char)
584 (optimize (speed 3) (space 0) (debug 0) (safety 0)))
585 ;; If there is any input read from UNIX but not
586 ;; supplied to the user of the stream, reposition
587 ;; to the real file position as seen from Lisp.
588 (when (> (fd-stream-ibuf-tail stream)
589 (fd-stream-ibuf-head stream))
590 (file-position stream (file-position stream)))
591 (let* ((sap (fd-stream-obuf-sap stream))
592 (len (fd-stream-obuf-length stream))
593 (tail (fd-stream-obuf-tail stream)))
594 (declare (type sys:system-area-pointer sap) (type index len tail))
595 (stream::char-to-octets ,extfmt
596 char
597 (fd-stream-co-state stream)
598 (lambda (byte)
599 (when (= tail len)
600 (do-output stream sap 0 tail t)
601 (setq sap (fd-stream-obuf-sap stream)
602 tail 0))
603 (setf (bref sap (1- (incf tail))) byte))
604 (fd-stream-char-to-octets-error stream))
605 (setf (fd-stream-obuf-tail stream) tail))
606 (if (char= char #\Newline)
607 (setf (fd-stream-char-pos stream) 0)
608 (incf (fd-stream-char-pos stream)))
609 (ecase (fd-stream-buffering stream)
610 (:none (flush-output-buffer stream))
611 (:line (when (char= char #\Newline) (flush-output-buffer stream)))
612 (:full #| do nothing |#))
613 (values)))
614
615
616 ;;; OUTPUT-RAW-BYTES -- public
617 ;;;
618 ;;; Does the actual output. If there is space to buffer the string, buffer
619 ;;; it. If the string would normally fit in the buffer, but doesn't because
620 ;;; of other stuff in the buffer, flush the old noise out of the buffer and
621 ;;; put the string in it. Otherwise we have a very long string, so just
622 ;;; send it directly (after flushing the buffer, of course).
623 ;;;
624 (defun output-raw-bytes (stream thing &optional start end)
625 "Output THING to stream. THING can be any kind of vector or a sap. If THING
626 is a SAP, END must be supplied (as length won't work)."
627 (let ((start (or start 0))
628 (end (or end (length (the (simple-array * (*)) thing)))))
629 (declare (type index start end))
630 ;;
631 ;; If there is any input read from UNIX but not
632 ;; supplied to the user of the stream, reposition
633 ;; to the real file position as seen from Lisp.
634 (when (> (fd-stream-ibuf-tail stream)
635 (fd-stream-ibuf-head stream))
636 (file-position stream (file-position stream)))
637 (let* ((len (fd-stream-obuf-length stream))
638 (tail (fd-stream-obuf-tail stream))
639 (space (- len tail))
640 (bytes (- end start))
641 (newtail (+ tail bytes)))
642 (cond ((minusp bytes) ; Error case
643 (cerror (intl:gettext "Just go on as if nothing happened...")
644 (intl:gettext "~S called with :END before :START!")
645 'output-raw-bytes))
646 ((zerop bytes)) ; Easy case
647 ((<= bytes space)
648 (if (system-area-pointer-p thing)
649 (system-area-copy thing
650 (* start vm:byte-bits)
651 (fd-stream-obuf-sap stream)
652 (* tail vm:byte-bits)
653 (* bytes vm:byte-bits))
654 (copy-to-system-area thing
655 (+ (* start vm:byte-bits)
656 (* vm:vector-data-offset vm:word-bits))
657 (fd-stream-obuf-sap stream)
658 (* tail vm:byte-bits)
659 (* bytes vm:byte-bits)))
660 (setf (fd-stream-obuf-tail stream) newtail))
661 ((<= bytes len)
662 (flush-output-buffer stream)
663 (if (system-area-pointer-p thing)
664 (system-area-copy thing
665 (* start vm:byte-bits)
666 (fd-stream-obuf-sap stream)
667 0
668 (* bytes vm:byte-bits))
669 (copy-to-system-area thing
670 (+ (* start vm:byte-bits)
671 (* vm:vector-data-offset vm:word-bits))
672 (fd-stream-obuf-sap stream)
673 0
674 (* bytes vm:byte-bits)))
675 (setf (fd-stream-obuf-tail stream) bytes))
676 (t
677 (flush-output-buffer stream)
678 (do-output stream thing start end nil))))))
679
680 ;;; FD-SOUT -- internal
681 ;;;
682 ;;; Routine to use to output a string. If the stream is unbuffered, slam
683 ;;; the string down the file descriptor, otherwise use OUTPUT-RAW-BYTES to
684 ;;; buffer the string. Update charpos by checking to see where the last newline
685 ;;; was.
686 ;;;
687 ;;; Note: some bozos (the FASL dumper) call write-string with things other
688 ;;; than strings. Therefore, we must make sure we have a string before calling
689 ;;; position on it.
690 ;;;
691
692 (stream::def-ef-macro ef-sout (extfmt lisp stream::+ef-max+ stream::+ef-sout+)
693 `(lambda (stream string start end)
694 (declare (type fd-stream stream)
695 (type simple-string string)
696 (type index start end)
697 (optimize (speed 3) (space 0) (safety 0) (debug 0)))
698 ;; If there is any input read from UNIX but not
699 ;; supplied to the user of the stream, reposition
700 ;; to the real file position as seen from Lisp.
701 ;; (maybe the caller should do this?)
702 (when (> (fd-stream-ibuf-tail stream)
703 (fd-stream-ibuf-head stream))
704 (file-position stream (file-position stream)))
705 (let* ((sap (fd-stream-obuf-sap stream))
706 (len (fd-stream-obuf-length stream))
707 (tail (fd-stream-obuf-tail stream)))
708 (declare (type sys:system-area-pointer sap) (type index len tail))
709 (dotimes (i (- end start))
710 (stream::char-to-octets ,extfmt
711 (schar string (+ i start))
712 (fd-stream-co-state stream)
713 (lambda (byte)
714 (when (= tail len)
715 (do-output stream sap 0 tail t)
716 (setq sap (fd-stream-obuf-sap stream)
717 tail 0))
718 (setf (bref sap (1- (incf tail))) byte))
719 (fd-stream-char-to-octets-error stream)))
720 (setf (fd-stream-obuf-tail stream) tail))))
721
722
723 #-unicode
724 (defun fd-sout (stream thing start end)
725 (let ((start (or start 0))
726 (end (or end (length (the vector thing)))))
727 (declare (type index start end))
728 (if (stringp thing)
729 (let ((last-newline (and (find #\newline (the simple-string thing)
730 :start start :end end)
731 (position #\newline (the simple-string thing)
732 :from-end t
733 :start start
734 :end end))))
735 (ecase (fd-stream-buffering stream)
736 (:full
737 (output-raw-bytes stream thing start end))
738 (:line
739 (output-raw-bytes stream thing start end)
740 (when last-newline
741 (flush-output-buffer stream)))
742 (:none
743 (do-output stream thing start end nil)))
744 (if last-newline
745 (setf (fd-stream-char-pos stream)
746 (- end last-newline 1))
747 (incf (fd-stream-char-pos stream)
748 (- end start))))
749 (ecase (fd-stream-buffering stream)
750 ((:line :full)
751 (output-raw-bytes stream thing start end))
752 (:none
753 (do-output stream thing start end nil))))))
754
755 #+unicode
756 ;; Temporary. The final version is defined in fd-stream-extfmt.lisp
757 (defun fd-sout (stream thing start end)
758 (declare (type string thing))
759 (let ((start (or start 0))
760 (end (or end (length (the vector thing)))))
761 (declare (type index start end))
762 (cond
763 ((stringp thing) ; FIXME - remove this test
764 (let ((out (fd-stream-out stream)))
765 (do ((index start (+ index 1)))
766 ((>= index end))
767 (funcall out stream (elt thing index))))))))
768
769 (defmacro output-wrapper ((stream size buffering) &body body)
770 (let ((stream-var (gensym)))
771 `(let ((,stream-var ,stream))
772 ,(unless (eq (car buffering) :none)
773 `(when (< (fd-stream-obuf-length ,stream-var)
774 (+ (fd-stream-obuf-tail ,stream-var)
775 ,size))
776 (flush-output-buffer ,stream-var)))
777 ,(unless (eq (car buffering) :none)
778 `(when (> (fd-stream-ibuf-tail ,stream-var)
779 (fd-stream-ibuf-head ,stream-var))
780 (file-position ,stream-var (file-position ,stream-var))))
781
782 ,@body
783 (incf (fd-stream-obuf-tail ,stream-var) ,size)
784 ,(ecase (car buffering)
785 (:none
786 `(flush-output-buffer ,stream-var))
787 (:line
788 `(when (eq (char-code byte) (char-code #\Newline))
789 (flush-output-buffer ,stream-var)))
790 (:full))
791 (values))))
792
793 ;;; PICK-OUTPUT-ROUTINE -- internal
794 ;;;
795 ;;; Find an output routine to use given the type and buffering. Return as
796 ;;; multiple values the routine, the real type transfered, and the number of
797 ;;; bytes per element.
798 ;;;
799 (defun pick-output-routine (type buffering)
800 (dolist (entry *output-routines*)
801 (when (and (subtypep type (car entry))
802 (eq buffering (cadr entry)))
803 (return-from pick-output-routine
804 (values (symbol-function (caddr entry))
805 (car entry)
806 (cadddr entry)))))
807 ;; KLUDGE: also see comments in PICK-INPUT-ROUTINE
808 (loop for i from 40 by 8 to max-stream-element-size ; ARB (KLUDGE)
809 if (subtypep type `(unsigned-byte ,i))
810 do (return-from pick-output-routine
811 (values
812 (ecase buffering
813 (:none
814 (lambda (stream byte)
815 (output-wrapper (stream (/ i 8) (:none))
816 (loop for j from 0 below (/ i 8)
817 do (setf (sap-ref-8
818 (fd-stream-obuf-sap stream)
819 (+ j (fd-stream-obuf-tail stream)))
820 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
821 (:full
822 (lambda (stream byte)
823 (output-wrapper (stream (/ i 8) (:full))
824 (loop for j from 0 below (/ i 8)
825 do (setf (sap-ref-8
826 (fd-stream-obuf-sap stream)
827 (+ j (fd-stream-obuf-tail stream)))
828 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
829 `(unsigned-byte ,i)
830 (/ i 8))))
831 (loop for i from 40 by 8 to max-stream-element-size ; ARB (KLUDGE)
832 if (subtypep type `(signed-byte ,i))
833 do (return-from pick-output-routine
834 (values
835 (ecase buffering
836 (:none
837 (lambda (stream byte)
838 (output-wrapper (stream (/ i 8) (:none))
839 (loop for j from 0 below (/ i 8)
840 do (setf (sap-ref-8
841 (fd-stream-obuf-sap stream)
842 (+ j (fd-stream-obuf-tail stream)))
843 (ldb (byte 8 (- i 8 (* j 8))) byte))))))
844 (:full
845 (lambda (stream byte)
846 (output-wrapper (stream (/ i 8) (:full))
847 (loop for j from 0 below (/ i 8)
848 do (setf (sap-ref-8
849 (fd-stream-obuf-sap stream)
850 (+ j (fd-stream-obuf-tail stream)))
851 (ldb (byte 8 (- i 8 (* j 8))) byte)))))))
852 `(signed-byte ,i)
853 (/ i 8)))))
854
855 ;;;; Input routines and related noise.
856
857 (defvar *input-routines* ()
858 "List of all available input routines. Each element is a list of the
859 element-type input, the function name, and the number of bytes per element.")
860
861 ;;; DO-INPUT -- internal
862 ;;;
863 ;;; Fills the input buffer, and returns the first character. Throws to
864 ;;; eof-input-catcher if the eof was reached. Drops into system:server if
865 ;;; necessary.
866 ;;;
867 (defun do-input (stream)
868 (let ((fd (fd-stream-fd stream))
869 (ibuf-sap (fd-stream-ibuf-sap stream))
870 (buflen (fd-stream-ibuf-length stream))
871 (head (fd-stream-ibuf-head stream))
872 (lcrs #-unicode 0
873 #+unicode (fd-stream-last-char-read-size stream))
874 (tail (fd-stream-ibuf-tail stream)))
875 (declare (type index head lcrs tail))
876 (unless (zerop head)
877 (cond ((eql head tail)
878 (setf head lcrs)
879 (setf tail lcrs)
880 (setf (fd-stream-ibuf-head stream) lcrs)
881 (setf (fd-stream-ibuf-tail stream) lcrs))
882 (t
883 (decf tail (- head lcrs))
884 (system-area-copy ibuf-sap (* (- head lcrs) vm:byte-bits)
885 ibuf-sap 0 (* tail vm:byte-bits))
886 (setf head lcrs)
887 (setf (fd-stream-ibuf-head stream) lcrs)
888 (setf (fd-stream-ibuf-tail stream) tail))))
889 (setf (fd-stream-listen stream) nil)
890 (multiple-value-bind
891 (count errno)
892 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
893 (unix:fd-zero read-fds)
894 (unix:fd-set fd read-fds)
895 (unix:unix-fast-select (1+ fd) (alien:addr read-fds) nil nil 0 0))
896 ;; Wait if input is not available or if interrupted.
897 (when (or (eql count 0)
898 (and (not count) (eql errno unix:eintr)))
899 (unless #-mp (system:wait-until-fd-usable
900 fd :input (fd-stream-timeout stream))
901 #+mp (mp:process-wait-until-fd-usable
902 fd :input (fd-stream-timeout stream))
903 (error 'io-timeout :stream stream :direction :read))))
904 (multiple-value-bind
905 (count errno)
906 (unix:unix-read fd
907 (system:int-sap (+ (system:sap-int ibuf-sap) tail))
908 (- buflen tail))
909 (cond ((null count)
910 ;; What kinds of errors do we want to look at and what do
911 ;; we want them to do?
912 (cond ((eql errno unix:ewouldblock)
913 (unless #-mp (system:wait-until-fd-usable
914 fd :input (fd-stream-timeout stream))
915 #+mp (mp:process-wait-until-fd-usable
916 fd :input (fd-stream-timeout stream))
917 (error 'io-timeout :stream stream :direction :read))
918 (do-input stream))
919 ((eql errno unix:econnreset)
920 (error 'socket-error
921 :format-control "Socket connection reset: ~A"
922 :format-arguments (list (unix:get-unix-error-msg errno))
923 :errno errno))
924 (t
925 (error (intl:gettext "Error reading ~S: ~A")
926 stream
927 (unix:get-unix-error-msg errno)))))
928 ((zerop count)
929 (setf (fd-stream-listen stream) :eof)
930 (throw 'eof-input-catcher nil))
931 (t
932 (incf (fd-stream-ibuf-tail stream) count))))))
933
934 ;;; INPUT-AT-LEAST -- internal
935 ;;;
936 ;;; Makes sure there are at least ``bytes'' number of bytes in the input
937 ;;; buffer. Keeps calling do-input until that condition is met.
938 ;;;
939 (defmacro input-at-least (stream bytes)
940 (let ((stream-var (gensym))
941 (bytes-var (gensym)))
942 `(let ((,stream-var ,stream)
943 (,bytes-var ,bytes))
944 (loop
945 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
946 (fd-stream-ibuf-head ,stream-var))
947 ,bytes-var)
948 (return))
949 (do-input ,stream-var)))))
950
951 ;;; INPUT-WRAPPER -- internal
952 ;;;
953 ;;; Macro to wrap around all input routines to handle eof-error noise.
954 ;;;
955 (defmacro input-wrapper ((stream bytes eof-error eof-value &optional type) &body read-forms)
956 (let ((stream-var (gensym))
957 (element-var (gensym)))
958 `(let ((,stream-var ,stream))
959 (if (fd-stream-unread ,stream-var) ;;@@
960 (prog1
961 ,(if (eq type 'character)
962 `(fd-stream-unread ,stream-var)
963 `(char-code (fd-stream-unread ,stream-var)))
964 (setf (fd-stream-unread ,stream-var) nil)
965 (setf (fd-stream-listen ,stream-var) nil))
966 (let ((,element-var
967 (catch 'eof-input-catcher
968 (input-at-least ,stream-var ,bytes)
969 ,@read-forms)))
970 (cond (,element-var
971 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
972 ,element-var)
973 (t
974 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
975
976 ;;; DEF-INPUT-ROUTINE -- internal
977 ;;;
978 ;;; Defines an input routine.
979 ;;;
980 (defmacro def-input-routine (name
981 (type size sap head)
982 &rest body)
983 `(progn
984 (defun ,name (stream eof-error eof-value)
985 (input-wrapper (stream ,size eof-error eof-value ,type)
986 (let ((,sap (fd-stream-ibuf-sap stream))
987 (,head (fd-stream-ibuf-head stream)))
988 ,@body)))
989 (setf *input-routines*
990 (nconc *input-routines*
991 (list (list ',type ',name ',size))))))
992
993 ;;; INPUT-CHARACTER -- internal
994 ;;;
995 ;;; Routine to use in stream-in slot for reading string chars.
996 ;;;
997 (def-input-routine input-character
998 (character 1 sap head)
999 (code-char (sap-ref-8 sap head)))
1000
1001 ;;; INPUT-UNSIGNED-8BIT-BYTE -- internal
1002 ;;;
1003 ;;; Routine to read in an unsigned 8 bit number.
1004 ;;;
1005 (def-input-routine input-unsigned-8bit-byte
1006 ((unsigned-byte 8) 1 sap head)
1007 (sap-ref-8 sap head))
1008
1009 ;;; INPUT-SIGNED-8BIT-BYTE -- internal
1010 ;;;
1011 ;;; Routine to read in a signed 8 bit number.
1012 ;;;
1013 (def-input-routine input-signed-8bit-number
1014 ((signed-byte 8) 1 sap head)
1015 (signed-sap-ref-8 sap head))
1016
1017 ;;; INPUT-UNSIGNED-16BIT-BYTE -- internal
1018 ;;;
1019 ;;; Routine to read in an unsigned 16 bit number.
1020 ;;;
1021 (def-input-routine input-unsigned-16bit-byte
1022 ((unsigned-byte 16) 2 sap head)
1023 (sap-ref-16 sap head))
1024
1025 ;;; INPUT-SIGNED-16BIT-BYTE -- internal
1026 ;;;
1027 ;;; Routine to read in a signed 16 bit number.
1028 ;;;
1029 (def-input-routine input-signed-16bit-byte
1030 ((signed-byte 16) 2 sap head)
1031 (signed-sap-ref-16 sap head))
1032
1033 ;;; INPUT-UNSIGNED-32BIT-BYTE -- internal
1034 ;;;
1035 ;;; Routine to read in a unsigned 32 bit number.
1036 ;;;
1037 (def-input-routine input-unsigned-32bit-byte
1038 ((unsigned-byte 32) 4 sap head)
1039 (sap-ref-32 sap head))
1040
1041 ;;; INPUT-SIGNED-32BIT-BYTE -- internal
1042 ;;;
1043 ;;; Routine to read in a signed 32 bit number.
1044 ;;;
1045 (def-input-routine input-signed-32bit-byte
1046 ((signed-byte 32) 4 sap head)
1047 (signed-sap-ref-32 sap head))
1048
1049 (stream::def-ef-macro ef-cin (extfmt lisp stream::+ef-max+ stream::+ef-cin+)
1050 `(lambda (stream eof-error-p eof-value)
1051 (declare (type fd-stream stream)
1052 #|(optimize (speed 3) (space 0) (debug 0) (safety 0))|#)
1053 (let* ((head (fd-stream-ibuf-head stream))
1054 (ch (catch 'eof-input-catcher
1055 (stream::octets-to-char ,extfmt
1056 (fd-stream-oc-state stream)
1057 (fd-stream-last-char-read-size stream)
1058 ;;@@ Note: need proper EOF handling...
1059 (progn
1060 (when (= head (fd-stream-ibuf-tail stream))
1061 (let ((sofar (- head (fd-stream-ibuf-head
1062 stream))))
1063 (do-input stream)
1064 (setf head (+ (fd-stream-ibuf-head stream)
1065 sofar))))
1066 (bref (fd-stream-ibuf-sap stream)
1067 (1- (incf head))))
1068 (lambda (n) (decf head n))))))
1069 (declare (type index head))
1070 (if ch
1071 (progn
1072 (setf (fd-stream-ibuf-head stream) head)
1073 ch)
1074 (eof-or-lose stream eof-error-p eof-value)))))
1075
1076 #+(or)
1077 (stream::def-ef-macro ef-sin (extfmt lisp stream::+ef-max+ stream::+ef-sin+)
1078 `(lambda (stream string char start end)
1079 (declare (type fd-stream stream)
1080 (type simple-string string)
1081 (type (or character null) char)
1082 (type index start end)
1083 (optimize (speed 3) (space 0) (debug 0) (safety 0)))
1084 (let ((sap (fd-stream-ibuf-sap stream))
1085 (head (fd-stream-ibuf-head stream))
1086 (tail (fd-stream-ibuf-tail stream))
1087 (curr start))
1088 (declare (type sys:system-area-pointer sap)
1089 (type index head tail curr))
1090 (loop
1091 ;;@@ Fix EOF handling
1092 (let* ((sz 0)
1093 (ch (catch 'eof-input-catcher
1094 (stream::octets-to-char ,extfmt
1095 (fd-stream-oc-state stream)
1096 sz
1097 (progn
1098 (when (= head tail)
1099 (let ((sofar (- head
1100 (fd-stream-ibuf-head
1101 stream))))
1102 (do-input stream)
1103 (setq head
1104 (+ (fd-stream-ibuf-head
1105 stream)
1106 sofar)
1107 tail
1108 (fd-stream-ibuf-tail
1109 stream))))
1110 (bref sap (1- (incf head))))
1111 (lambda (n) (decf head n))))))
1112 (declare (type index sz)
1113 (type (or null character) ch))
1114 (when (null ch)
1115 (return (values (- curr start) :eof)))
1116 (setf (fd-stream-last-char-read-size stream) sz)
1117 (incf (fd-stream-ibuf-head stream) sz)
1118 (when (and char (char= ch char))
1119 (return (values (- curr start) t)))
1120 (setf (schar string (1- (incf curr))) ch)
1121 (when (= curr end)
1122 (return (values (- curr start) nil))))))))
1123
1124 ;;; PICK-INPUT-ROUTINE -- internal
1125 ;;;
1126 ;;; Find an input routine to use given the type. Return as multiple values
1127 ;;; the routine, the real type transfered, and the number of bytes per element.
1128 ;;;
1129 (defun pick-input-routine (type)
1130 (dolist (entry *input-routines*)
1131 (when (subtypep type (car entry))
1132 (return-from pick-input-routine
1133 (values (symbol-function (cadr entry))
1134 (car entry)
1135 (caddr entry)))))
1136 ;; FIXME: let's do it the hard way, then (but ignore things like
1137 ;; endianness, efficiency, and the necessary coupling between these
1138 ;; and the output routines). -- CSR, 2004-02-09
1139 (loop for i from 40 by 8 to max-stream-element-size ; ARB (well, KLUDGE really)
1140 if (subtypep type `(unsigned-byte ,i))
1141 do (return-from pick-input-routine
1142 (values
1143 (lambda (stream eof-error eof-value)
1144 (input-wrapper (stream (/ i 8) eof-error eof-value)
1145 (let ((sap (fd-stream-ibuf-sap stream))
1146 (head (fd-stream-ibuf-head stream)))
1147 (loop for j from 0 below (/ i 8)
1148 with result = 0
1149 do (setf result
1150 (+ (* 256 result)
1151 (sap-ref-8 sap (+ head j))))
1152 finally (return result)))))
1153 `(unsigned-byte ,i)
1154 (/ i 8))))
1155 (loop for i from 40 by 8 to max-stream-element-size ; ARB (well, KLUDGE really)
1156 if (subtypep type `(signed-byte ,i))
1157 do (return-from pick-input-routine
1158 (values
1159 (lambda (stream eof-error eof-value)
1160 (input-wrapper (stream (/ i 8) eof-error eof-value)
1161 (let ((sap (fd-stream-ibuf-sap stream))
1162 (head (fd-stream-ibuf-head stream)))
1163 (loop for j from 0 below (/ i 8)
1164 with result = 0
1165 do (setf result
1166 (+ (* 256 result)
1167 (sap-ref-8 sap (+ head j))))
1168 finally (return (if (logbitp (1- i) result)
1169 (dpb result (byte i 0) -1)
1170 result))))))
1171 `(signed-byte ,i)
1172 (/ i 8)))))
1173
1174 ;;; STRING-FROM-SAP -- internal
1175 ;;;
1176 ;;; Returns a string constructed from the sap, start, and end.
1177 ;;;
1178 (defun string-from-sap (sap start end)
1179 (declare (type index start end))
1180 (let* ((length (- end start))
1181 (string (make-string length)))
1182 (copy-from-system-area sap (* start vm:byte-bits)
1183 string (* vm:vector-data-offset vm:word-bits)
1184 (* length vm:byte-bits))
1185 string))
1186
1187 #|
1188 ;;; FD-STREAM-READ-N-BYTES -- internal
1189 ;;;
1190 ;;; This version waits using server. I changed to the non-server version
1191 ;;; because it allows this method to be used by CLX w/o confusing serve-event.
1192 ;;; The non-server method is also significantly more efficient for large
1193 ;;; reads. -- Ram
1194 ;;;
1195 ;;; The n-bin routine.
1196 ;;;
1197 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
1198 (declare (type stream stream) (type index start requested))
1199 (let* ((sap (fd-stream-ibuf-sap stream))
1200 (elsize (fd-stream-element-size stream))
1201 (offset (* elsize start))
1202 (bytes (* elsize requested))
1203 (result
1204 (catch 'eof-input-catcher
1205 (loop
1206 (input-at-least stream 1)
1207 (let* ((head (fd-stream-ibuf-head stream))
1208 (tail (fd-stream-ibuf-tail stream))
1209 (available (- tail head))
1210 (copy (min available bytes)))
1211 (if (typep buffer 'system-area-pointer)
1212 (system-area-copy sap (* head vm:byte-bits)
1213 buffer (* offset vm:byte-bits)
1214 (* copy vm:byte-bits))
1215 (copy-from-system-area sap (* head vm:byte-bits)
1216 buffer (+ (* offset vm:byte-bits)
1217 (* vm:vector-data-offset
1218 vm:word-bits))
1219 (* copy vm:byte-bits)))
1220 (incf (fd-stream-ibuf-head stream) copy)
1221 (incf offset copy)
1222 (decf bytes copy))
1223 (when (zerop bytes)
1224 (return requested))))))
1225 (or result
1226 (eof-or-lose stream eof-error-p
1227 (- requested (/ bytes elsize))))))
1228 |#
1229
1230
1231 ;;; FD-STREAM-READ-N-BYTES -- internal
1232 ;;;
1233 ;;; The N-Bin method for FD-STREAMs. This doesn't use the SERVER; it blocks
1234 ;;; in UNIX-READ. This allows the method to be used to implement reading
1235 ;;; for CLX. It is generally used where there is a definite amount of reading
1236 ;;; to be done, so blocking isn't too problematical.
1237 ;;;
1238 ;;; We copy buffered data into the buffer. If there is enough, just return.
1239 ;;; Otherwise, we see if the amount of additional data needed will fit in the
1240 ;;; stream buffer. If not, inhibit GCing (so we can have a SAP into the Buffer
1241 ;;; argument), and read directly into the user supplied buffer. Otherwise,
1242 ;;; read a buffer-full into the stream buffer and then copy the amount we need
1243 ;;; out.
1244 ;;;
1245 ;;; We loop doing the reads until we either get enough bytes or hit EOF. We
1246 ;;; must loop when eof-errorp is T because some streams (like pipes) may return
1247 ;;; a partial amount without hitting EOF.
1248 ;;;
1249 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
1250 (declare (type stream stream) (type index start requested))
1251 (let* ((sap (fd-stream-ibuf-sap stream))
1252 (offset start)
1253 (head (fd-stream-ibuf-head stream))
1254 (tail (fd-stream-ibuf-tail stream))
1255 (available (- tail head))
1256 (copy (min requested available)))
1257 (declare (type index offset head tail available copy))
1258 ;;
1259 ;; If something has been unread, put that at buffer + start,
1260 ;; and read the rest to start + 1.
1261 (when (fd-stream-unread stream) ;;@@
1262 (etypecase buffer
1263 (system-area-pointer
1264 (assert (= 1 (fd-stream-element-size stream)))
1265 (setf (sap-ref-8 buffer start) (char-code (read-char stream))))
1266 (string
1267 (setf (aref buffer start) (read-char stream)))
1268 (vector
1269 (setf (aref buffer start) (char-code(read-char stream)))))
1270 (return-from fd-stream-read-n-bytes
1271 (1+ (fd-stream-read-n-bytes stream buffer (1+ start) (1- requested)
1272 eof-error-p))))
1273 ;;
1274 (unless (zerop copy)
1275 (if (typep buffer 'system-area-pointer)
1276 (system-area-copy sap (* head vm:byte-bits)
1277 buffer (* offset vm:byte-bits)
1278 (* copy vm:byte-bits))
1279 (copy-from-system-area sap (* head vm:byte-bits)
1280 buffer (+ (* offset vm:byte-bits)
1281 (* vm:vector-data-offset
1282 vm:word-bits))
1283 (* copy vm:byte-bits)))
1284 (incf (fd-stream-ibuf-head stream) copy))
1285 (cond
1286 ((or (= copy requested)
1287 (and (not eof-error-p) (/= copy 0)))
1288 copy)
1289 (t
1290 (setf (fd-stream-ibuf-head stream) 0)
1291 (setf (fd-stream-ibuf-tail stream) 0)
1292 (setf (fd-stream-listen stream) nil)
1293 (let ((now-needed (- requested copy))
1294 (len (fd-stream-ibuf-length stream)))
1295 (declare (type index now-needed len))
1296 (cond
1297 ((> now-needed len)
1298 ;;
1299 ;; If the desired amount is greater than the stream buffer size, then
1300 ;; read directly into the destination, incrementing the start
1301 ;; accordingly. In this case, we never leave anything in the stream
1302 ;; buffer.
1303 (system:without-gcing
1304 (loop
1305 (multiple-value-bind
1306 (count err)
1307 (unix:unix-read (fd-stream-fd stream)
1308 (sap+ (if (typep buffer 'system-area-pointer)
1309 buffer
1310 (vector-sap buffer))
1311 (+ offset copy))
1312 now-needed)
1313 (declare (type (or index null) count))
1314 (unless count
1315 (error (intl:gettext "Error reading ~S: ~A") stream
1316 (unix:get-unix-error-msg err)))
1317 (decf now-needed count)
1318 (if eof-error-p
1319 (when (zerop count)
1320 (error 'end-of-file :stream stream))
1321 (return (- requested now-needed)))
1322 (when (zerop now-needed) (return requested))
1323 (incf offset count)))))
1324 (t
1325 ;;
1326 ;; If we want less than the buffer size, then loop trying to fill the
1327 ;; stream buffer and copying what we get into the destination. When
1328 ;; we have enough, we leave what's left in the stream buffer.
1329 (loop
1330 (multiple-value-bind
1331 (count err)
1332 (unix:unix-read (fd-stream-fd stream) sap len)
1333 (declare (type (or index null) count))
1334 (unless count
1335 (error (intl:gettext "Error reading ~S: ~A") stream
1336 (unix:get-unix-error-msg err)))
1337 (when (and eof-error-p (zerop count))
1338 (error 'end-of-file :stream stream))
1339
1340 (let* ((copy (min now-needed count))
1341 (copy-bits (* copy vm:byte-bits))
1342 (buffer-start-bits
1343 (* (+ offset available) vm:byte-bits)))
1344 (declare (type index copy copy-bits buffer-start-bits))
1345 (if (typep buffer 'system-area-pointer)
1346 (system-area-copy sap 0
1347 buffer buffer-start-bits
1348 copy-bits)
1349 (copy-from-system-area sap 0
1350 buffer (+ buffer-start-bits
1351 (* vm:vector-data-offset
1352 vm:word-bits))
1353 copy-bits))
1354
1355 (decf now-needed copy)
1356 (when (or (zerop now-needed) (not eof-error-p))
1357 (setf (fd-stream-ibuf-head stream) copy)
1358 (setf (fd-stream-ibuf-tail stream) count)
1359 (return (- requested now-needed)))
1360 (incf offset copy)))))))))))
1361
1362
1363 ;;;; Utility functions (misc routines, etc)
1364
1365 ;;; SET-ROUTINES -- internal
1366 ;;;
1367 ;;; Fill in the various routine slots for the given type. Input-p and
1368 ;;; output-p indicate what slots to fill. The buffering slot must be set prior
1369 ;;; to calling this routine.
1370 ;;;
1371
1372 (defun set-routines (stream type input-p output-p buffer-p &key binary-stream-p)
1373 (let ((target-type (case type
1374 ((:default unsigned-byte)
1375 '(unsigned-byte 8))
1376 (signed-byte
1377 '(signed-byte 8))
1378 (t
1379 type)))
1380 (input-type nil)
1381 (output-type nil)
1382 (input-size nil)
1383 (output-size nil))
1384
1385 (when (fd-stream-obuf-sap stream)
1386 (push (fd-stream-obuf-sap stream) *available-buffers*)
1387 (setf (fd-stream-obuf-sap stream) nil))
1388 (when (fd-stream-ibuf-sap stream)
1389 (push (fd-stream-ibuf-sap stream) *available-buffers*)
1390 (setf (fd-stream-ibuf-sap stream) nil))
1391
1392 #+unicode
1393 (when (null (fd-stream-external-format stream))
1394 (setf (fd-stream-external-format stream) :default))
1395
1396 (when input-p
1397 (multiple-value-bind
1398 (routine type size)
1399 (pick-input-routine target-type)
1400 (unless routine
1401 (error (intl:gettext "Could not find any input routine for ~S") target-type))
1402 (setf (fd-stream-ibuf-sap stream) (next-available-buffer))
1403 (setf (fd-stream-ibuf-length stream) bytes-per-buffer)
1404 (setf (fd-stream-ibuf-tail stream) 0)
1405
1406 ;; Set the in and bin methods. Normally put an illegal input
1407 ;; function in, but if we have a binary text stream, pick an
1408 ;; appropriate input routine.
1409 (if (subtypep type 'character)
1410 (setf (fd-stream-in stream) routine
1411 (fd-stream-bin stream) (if (and binary-stream-p
1412 (eql size 1))
1413 (pick-input-routine '(unsigned-byte 8))
1414 #'ill-bin))
1415 (setf (fd-stream-in stream) (if (and binary-stream-p
1416 (eql size 1))
1417 (pick-input-routine 'character)
1418 #'ill-in)
1419 (fd-stream-bin stream) routine))
1420 (when (or (eql size 1)
1421 (eql size 2)
1422 (eql size 4))
1423 ;; Support for n-byte operations on 8-, 16-, and 32-bit streams
1424 (setf (fd-stream-n-bin stream) #'fd-stream-read-n-bytes)
1425 (when (and buffer-p (eql size 1)
1426 (or
1427 ;; FIXME: Do this better. We want to check for
1428 ;; (unsigned-byte 8). The 8 is unnecessary
1429 ;; since we already have size = 1.
1430 (or (eq 'unsigned-byte (and (consp type) (car type)))
1431 (eq type :default))
1432 (eq type 'character)))
1433 (when *enable-stream-buffer-p*
1434 (when (and (not binary-stream-p)
1435 (eq type 'character))
1436 ;; Create the in-buffer for any character (only)
1437 ;; stream. Don't want one for binary-text-streams!
1438 (setf (lisp-stream-in-buffer stream)
1439 (make-array in-buffer-length
1440 :element-type '(unsigned-byte 8))))
1441 #+unicode
1442 (when (and (not binary-stream-p)
1443 (eq type 'character)
1444 (not (eq :iso8859-1 (fd-stream-external-format stream))))
1445 ;; For character streams, we create the string-buffer so
1446 ;; we can convert all available octets at once instead
1447 ;; of for each character. The string is one element
1448 ;; longer than in-buffer-length to leave room for
1449 ;; unreading.
1450 ;;
1451 ;; For ISO8859-1, we don't want this because it's very
1452 ;; easy and quick to convert octets to iso8859-1. (See
1453 ;; FAST-READ-CHAR.)
1454
1455 (setf (lisp-stream-string-buffer stream)
1456 (make-string (1+ in-buffer-length)))
1457 (setf (fd-stream-octet-count stream)
1458 (make-array in-buffer-length :element-type '(unsigned-byte 8)))
1459 (setf (lisp-stream-string-buffer-len stream) 0)
1460 (setf (lisp-stream-string-index stream) 0)))))
1461 (setf input-size size)
1462 (setf input-type type)))
1463
1464 (when output-p
1465 (multiple-value-bind
1466 (routine type size)
1467 (pick-output-routine target-type (fd-stream-buffering stream))
1468 (unless routine
1469 (error (intl:gettext "Could not find any output routine for ~S buffered ~S.")
1470 (fd-stream-buffering stream)
1471 target-type))
1472 (setf (fd-stream-obuf-sap stream) (next-available-buffer))
1473 (setf (fd-stream-obuf-length stream) bytes-per-buffer)
1474 (setf (fd-stream-obuf-tail stream) 0)
1475 ;; Normally signal errors for reading from a stream with the
1476 ;; wrong element type, but allow binary-text-streams to read
1477 ;; from either.
1478 (if (subtypep type 'character)
1479 (setf (fd-stream-out stream) routine
1480 (fd-stream-bout stream)
1481 (if (and binary-stream-p
1482 (eql size 1))
1483 (pick-output-routine '(unsigned-byte 8)
1484 (fd-stream-buffering stream))
1485 #'ill-bout))
1486 (setf (fd-stream-out stream)
1487 (if (and binary-stream-p (eql size 1))
1488 (pick-output-routine 'base-char
1489 (fd-stream-buffering stream))
1490 #'ill-out)
1491 (fd-stream-bout stream) routine))
1492 (setf (fd-stream-sout stream)
1493 (if (eql size 1) #'fd-sout #'ill-out))
1494 (setf (fd-stream-char-pos stream) 0)
1495 (setf output-size size)
1496 (setf output-type type)))
1497
1498 (when (and input-size output-size
1499 (not (eql input-size output-size)))
1500 (error (intl:gettext "Element sizes for input (~S:~S) and output (~S:~S) differ?")
1501 input-type input-size
1502 output-type output-size))
1503 (setf (fd-stream-element-size stream)
1504 (or input-size output-size))
1505
1506 (setf (fd-stream-element-type stream)
1507 (cond ((equal input-type output-type)
1508 input-type)
1509 ((null output-type)
1510 input-type)
1511 ((null input-type)
1512 output-type)
1513 ((subtypep input-type output-type)
1514 input-type)
1515 ((subtypep output-type input-type)
1516 output-type)
1517 (t
1518 (error (intl:gettext "Input type (~S) and output type (~S) are unrelated?")
1519 input-type
1520 output-type))))))
1521
1522 ;;; REVERT-FILE -- internal
1523 ;;;
1524 ;;; Revert a file, if possible; otherwise do nothing. Used during
1525 ;;; CLOSE when the abort flag is set.
1526 ;;;
1527 (defun revert-file (filename original)
1528 (declare (type simple-base-string filename)
1529 (type (or simple-base-string null) original))
1530 (when original
1531 (multiple-value-bind (okay err)
1532 (unix:unix-rename original filename)
1533 (unless okay
1534 (cerror (intl:gettext "Go on as if nothing bad happened.")
1535 (intl:gettext "Could not restore ~S to its original contents: ~A")
1536 filename (unix:get-unix-error-msg err))))))
1537
1538 ;;; DELETE-ORIGINAL -- internal
1539 ;;;
1540 ;;; Delete a backup file. Used during CLOSE.
1541 ;;;
1542 (defun delete-original (filename original)
1543 (declare (type simple-base-string filename)
1544 (type (or simple-base-string null) original))
1545 (when original
1546 (multiple-value-bind (okay err) (unix:unix-unlink original)
1547 (unless okay
1548 (cerror "Go on as if nothing bad happened."
1549 "Could not delete ~S during close of ~S: ~A"
1550 original filename (unix:get-unix-error-msg err))))))
1551
1552 ;;; FD-STREAM-MISC-ROUTINE -- input
1553 ;;;
1554 ;;; Handle the various misc operations on fd-stream.
1555 ;;;
1556 (defun fd-stream-misc-routine (stream operation &optional arg1 arg2)
1557 (declare (ignore arg2))
1558 (case operation
1559 (:listen
1560 (or (not (eql (fd-stream-ibuf-head stream)
1561 (fd-stream-ibuf-tail stream)))
1562 (fd-stream-listen stream)
1563 (setf (fd-stream-listen stream)
1564 (eql (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
1565 (unix:fd-zero read-fds)
1566 (unix:fd-set (fd-stream-fd stream) read-fds)
1567 (unix:unix-fast-select (1+ (fd-stream-fd stream))
1568 (alien:addr read-fds) nil nil
1569 0 0))
1570 1))))
1571 (:unread
1572 #-unicode
1573 (setf (fd-stream-unread stream) arg1)
1574 #+unicode
1575 (cond ((lisp-stream-string-buffer stream)
1576 (if (zerop (lisp-stream-string-index stream))
1577 (setf (fd-stream-unread stream) arg1)
1578 (decf (lisp-stream-string-index stream))))
1579 (t
1580 (if (zerop (fd-stream-last-char-read-size stream))
1581 (setf (fd-stream-unread stream) arg1)
1582 (decf (fd-stream-ibuf-head stream)
1583 (fd-stream-last-char-read-size stream)))))
1584 ;; Paul says:
1585 ;;
1586 ;; Not needed for unicode when unreading is implemented by backing up in
1587 ;; the buffer (e.g., with last-char-read-size...)
1588 ;;
1589 ;; (AFAICS there's nothing wrong with setting it there, but it
1590 ;; screws up read-interactive in my toplevel command thing -
1591 ;; leaves it expecting to read arguments when it shouldn't,
1592 ;; because LISTEN returns T when there's no input pending, but I
1593 ;; don't understand why...)
1594 #-unicode
1595 (setf (fd-stream-listen stream) t))
1596 (:close
1597 (cond (arg1
1598 ;; We got us an abort on our hands.
1599 (when (fd-stream-handler stream)
1600 (system:remove-fd-handler (fd-stream-handler stream))
1601 (setf (fd-stream-handler stream) nil))
1602 (when (and (fd-stream-file stream) (fd-stream-obuf-sap stream))
1603 (revert-file (fd-stream-file stream)
1604 (fd-stream-original stream))))
1605 (t
1606 #+(and unicode (not unicode-bootstrap))
1607 (when (and (output-stream-p stream)
1608 (eq (stream-element-type stream) 'character))
1609 ;; For output character streams, we need to flush out
1610 ;; any state that the external format might have.
1611 #+nil (format *debug-io* "state = ~S~%" (fd-stream-co-state stream))
1612 (funcall (ef-flush (fd-stream-external-format stream))
1613 stream))
1614 (fd-stream-misc-routine stream :finish-output)
1615 (when (fd-stream-delete-original stream)
1616 (delete-original (fd-stream-file stream)
1617 (fd-stream-original stream)))))
1618 (when (fboundp 'cancel-finalization)
1619 (cancel-finalization stream))
1620 (unix:unix-close (fd-stream-fd stream))
1621 (when (fd-stream-obuf-sap stream)
1622 (push (fd-stream-obuf-sap stream) *available-buffers*)
1623 (setf (fd-stream-obuf-sap stream) nil))
1624 (when (fd-stream-ibuf-sap stream)
1625 (push (fd-stream-ibuf-sap stream) *available-buffers*)
1626 (setf (fd-stream-ibuf-sap stream) nil))
1627 (lisp::set-closed-flame stream))
1628 (:clear-input
1629 (setf (fd-stream-unread stream) nil) ;;@@
1630 #+unicode (setf (fd-stream-last-char-read-size stream) 0)
1631 (setf (fd-stream-ibuf-head stream) 0)
1632 (setf (fd-stream-ibuf-tail stream) 0)
1633 (catch 'eof-input-catcher
1634 (loop
1635 (multiple-value-bind
1636 (count errno)
1637 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
1638 (unix:fd-zero read-fds)
1639 (unix:fd-set (fd-stream-fd stream) read-fds)
1640 (unix:unix-fast-select (1+ (fd-stream-fd stream))
1641 (alien:addr read-fds) nil nil 0 0))
1642 (cond ((eql count 1)
1643 (do-input stream)
1644 (setf (fd-stream-ibuf-head stream) 0)
1645 (setf (fd-stream-ibuf-tail stream) 0))
1646 ((and (not count) (eql errno unix:eintr)))
1647 (t
1648 (return t)))))))
1649 (:force-output
1650 (flush-output-buffer stream))
1651 (:finish-output
1652 (flush-output-buffer stream)
1653 (do ()
1654 ((null (fd-stream-output-later stream)))
1655 (system:serve-all-events)))
1656 (:element-type
1657 (fd-stream-element-type stream))
1658 (:interactive-p
1659 (unix:unix-isatty (fd-stream-fd stream)))
1660 (:line-length
1661 80)
1662 (:charpos
1663 (fd-stream-char-pos stream))
1664 (:file-length
1665 (unless (fd-stream-file stream)
1666 (error 'simple-type-error
1667 :datum stream
1668 :expected-type 'file-stream
1669 :format-control (intl:gettext "~s is not a stream associated with a file.")
1670 :format-arguments (list stream)))
1671 (multiple-value-bind
1672 (okay dev ino mode nlink uid gid rdev size
1673 atime mtime ctime blksize blocks)
1674 (unix:unix-fstat (fd-stream-fd stream))
1675 (declare (ignore ino nlink uid gid rdev
1676 atime mtime ctime blksize blocks))
1677 (unless okay
1678 (error 'simple-file-error
1679 :format-control (intl:gettext "Error fstating ~S: ~A")
1680 :format-arguments (list stream (unix:get-unix-error-msg dev))))
1681 (if (zerop mode)
1682 nil
1683 (values (truncate size (fd-stream-element-size stream))))))
1684 (:file-position
1685 (fd-stream-file-position stream arg1))))
1686
1687
1688 ;;; FD-STREAM-FILE-POSITION -- internal.
1689 ;;;
1690 (defun fd-stream-file-position (stream &optional newpos)
1691 (declare (type fd-stream stream)
1692 (type (or (integer 0) (member nil :start :end)) newpos))
1693 (if (null newpos)
1694 (system:without-interrupts
1695 ;; First, find the position of the UNIX file descriptor in the file.
1696 (multiple-value-bind
1697 (posn errno)
1698 (unix:unix-lseek (fd-stream-fd stream) 0 unix:l_incr)
1699 (declare (type (or (integer 0) null) posn))
1700 (cond (posn
1701 ;; Adjust for buffered output:
1702 ;; If there is any output buffered, the *real* file position
1703 ;; will be larger than reported by lseek because lseek
1704 ;; obviously cannot take into account output we have not
1705 ;; sent yet.
1706 (dolist (later (fd-stream-output-later stream))
1707 (incf posn (- (the index (caddr later))
1708 (the index (cadr later)))))
1709 (incf posn (fd-stream-obuf-tail stream))
1710
1711 ;; Adjust for unread input:
1712 ;; If there is any input read from UNIX but not supplied to
1713 ;; the user of the stream, the *real* file position will
1714 ;; smaller than reported, because we want to look like the
1715 ;; unread stuff is still available.
1716 (decf posn (- (fd-stream-ibuf-tail stream)
1717 (fd-stream-ibuf-head stream)))
1718
1719 #+unicode
1720 (if (fd-stream-string-buffer stream)
1721 ;; The string buffer contains Lisp characters,
1722 ;; not octets! To figure out how many octets
1723 ;; have not been already supplied, we need to
1724 ;; count how many octets were consumed for all
1725 ;; the characters in the string bbuffer that have
1726 ;; not been supplied.
1727 (let ((ocount (fd-stream-octet-count stream)))
1728 (when ocount
1729 ;; Note: string-index starts at 1 (because
1730 ;; index 0 is for the unread-char), but
1731 ;; octet-count doesn't use that. Hence,
1732 ;; subtract one from string-index and
1733 ;; string-buffer-len.
1734 (loop for k of-type fixnum from (1- (fd-stream-string-index stream))
1735 below (1- (fd-stream-string-buffer-len stream))
1736 do (decf posn (aref ocount k)))))
1737 (when (fd-stream-in-buffer stream)
1738 ;; When we have an in-buffer (but no
1739 ;; string-buffer!), we need to adjust for the
1740 ;; octets that have not yet been supplied.
1741 ;; This situation (should!) only happens for an
1742 ;; external-format of ISO-8859-1. If there's
1743 ;; no string-buffer and no in-buffer, then the
1744 ;; ibuf tail and head pointers contain all the
1745 ;; information needed.
1746 (decf posn (- in-buffer-length
1747 (fd-stream-in-index stream)))))
1748
1749 (when (fd-stream-unread stream) ;;@@
1750 (decf posn))
1751 ;; Divide bytes by element size.
1752 (truncate posn (fd-stream-element-size stream)))
1753 ((eq errno unix:espipe)
1754 nil)
1755 (t
1756 (system:with-interrupts
1757 (error (intl:gettext "Error lseek'ing ~S: ~A")
1758 stream
1759 (unix:get-unix-error-msg errno)))))))
1760 (let ((offset 0)
1761 origin)
1762 (declare (type (integer 0) offset))
1763 ;; Make sure we don't have any output pending, because if we move the
1764 ;; file pointer before writing this stuff, it will be written in the
1765 ;; wrong location.
1766 (flush-output-buffer stream)
1767 (do ()
1768 ((null (fd-stream-output-later stream)))
1769 (system:serve-all-events))
1770 ;; Clear out any pending input to force the next read to go to the
1771 ;; disk.
1772 (setf (fd-stream-unread stream) nil) ;;@@
1773 #+unicode
1774 (progn
1775 (setf (fd-stream-last-char-read-size stream) 0)
1776 (setf (fd-stream-string-index stream)
1777 (fd-stream-string-buffer-len stream)))
1778 (setf (fd-stream-ibuf-head stream) 0)
1779 (setf (fd-stream-ibuf-tail stream) 0)
1780 ;; Trash cached value for listen, so that we check next time.
1781 (setf (fd-stream-listen stream) nil)
1782 ;; Now move it.
1783 (cond ((eq newpos :start)
1784 (setf offset 0
1785 origin unix:l_set))
1786 ((eq newpos :end)
1787 (setf offset 0
1788 origin unix:l_xtnd))
1789 ((typep newpos '(integer 0))
1790 (setf offset (* newpos (fd-stream-element-size stream))
1791 origin unix:l_set))
1792 (t
1793 (error (intl:gettext "Invalid position given to file-position: ~S") newpos)))
1794 (multiple-value-bind
1795 (posn errno)
1796 (unix:unix-lseek (fd-stream-fd stream) offset origin)
1797 (cond (posn
1798 t)
1799 ((eq errno unix:espipe)
1800 nil)
1801 (t
1802 (error (intl:gettext "Error lseek'ing ~S: ~A")
1803 stream
1804 (unix:get-unix-error-msg errno))))))))
1805
1806
1807
1808 ;;;; Creation routines (MAKE-FD-STREAM and OPEN)
1809
1810 ;;; MAKE-FD-STREAM -- Public.
1811 ;;;
1812 ;;; Returns a FD-STREAM on the given file.
1813 ;;;
1814 (defun make-fd-stream (fd
1815 &key
1816 (input nil input-p)
1817 (output nil output-p)
1818 (element-type 'base-char)
1819 (buffering :full)
1820 timeout
1821 file
1822 original
1823 delete-original
1824 pathname
1825 input-buffer-p
1826 ;; DO NOT translate these! It causes an
1827 ;; infinite loop. We need to open a file for
1828 ;; the translations, but if you translate
1829 ;; these, then we need to do a lookup which
1830 ;; wants to open the mo file which calls this
1831 ;; to name which causes a lookup ....
1832 (name (if file
1833 (format nil "file ~S" file)
1834 (format nil "descriptor ~D" fd)))
1835 auto-close
1836 (external-format :default)
1837 binary-stream-p
1838 decoding-error
1839 encoding-error)
1840 (declare (type index fd) (type (or index null) timeout)
1841 (type (member :none :line :full) buffering))
1842 "Create a stream for the given unix file descriptor.
1843 If input is non-nil, allow input operations.
1844 If output is non-nil, allow output operations.
1845 If neither input nor output are specified, default to allowing input.
1846 Element-type indicates the element type to use (as for open).
1847 Buffering indicates the kind of buffering to use.
1848 Timeout (if true) is the number of seconds to wait for input. If NIL (the
1849 default), then wait forever. When we time out, we signal IO-TIMEOUT.
1850 File is the name of the file (will be returned by PATHNAME).
1851 Name is used to identify the stream when printed.
1852 External-format is the external format to use for the stream.
1853 Decoding-error and Encoding-error indicate how decoding/encoding errors on
1854 the stream should be handled. The default is to use a replacement character."
1855 (cond ((not (or input-p output-p))
1856 (setf input t))
1857 ((not (or input output))
1858 (error (intl:gettext "File descriptor must be opened either for input or output."))))
1859 (let ((stream (if binary-stream-p
1860 (%make-binary-text-stream :fd fd
1861 :name name
1862 :file file
1863 :original original
1864 :delete-original delete-original
1865 :pathname pathname
1866 :buffering buffering
1867 :timeout timeout)
1868 (let ((e (cond ((characterp encoding-error)
1869 (constantly (char-code encoding-error)))
1870 (t
1871 encoding-error)))
1872 (d (cond ((characterp decoding-error)
1873 (constantly (char-code decoding-error)))
1874 ((eq t decoding-error)
1875 #+unicode
1876 #'(lambda (&rest args)
1877 (apply 'cerror
1878 (intl:gettext "Use Unicode replacement character instead")
1879 args)
1880 stream:+replacement-character-code+)
1881 #-unicode
1882 #'(lambda (&rest args)
1883 (apply 'cerror
1884 (intl:gettext "Use question mark character instead")
1885 args)
1886 #\?))
1887 (t
1888 decoding-error))))
1889 (%make-fd-stream :fd fd
1890 :name name
1891 :file file
1892 :original original
1893 :delete-original delete-original
1894 :pathname pathname
1895 :buffering buffering
1896 :timeout timeout
1897 :char-to-octets-error e
1898 :octets-to-char-error d)))))
1899 ;; Set the lisp-stream flags appropriately for the kind of stream
1900 ;; we have (character, binary, binary-text-stream).
1901 (cond ((typep stream 'binary-text-stream)
1902 (setf (fd-stream-flags stream) #b100))
1903 ((subtypep element-type 'character)
1904 (setf (fd-stream-flags stream) #b001))
1905 (t
1906 (setf (fd-stream-flags stream) #b010)))
1907
1908 ;; FIXME: setting the external format here should be better
1909 ;; integrated into set-routines. We do it before so that
1910 ;; set-routines can create an in-buffer if appropriate. But we
1911 ;; need to do it after to put the correct input routines for the
1912 ;; external format.
1913 ;;
1914 ;;#-unicode-bootstrap ; fails in stream-reinit otherwise
1915 #+(and unicode (not unicode-bootstrap))
1916 (%set-fd-stream-external-format stream external-format nil)
1917 (set-routines stream element-type input output input-buffer-p
1918 :binary-stream-p binary-stream-p)
1919 #+(and unicode (not unicode-bootstrap))
1920 (%set-fd-stream-external-format stream external-format nil)
1921 (when (and auto-close (fboundp 'finalize))
1922 (finalize stream
1923 #'(lambda ()
1924 (unix:unix-close fd)
1925 (format *terminal-io* (intl:gettext "** Closed ~A~%") name)
1926 (when original
1927 (revert-file file original)))))
1928 stream))
1929
1930
1931 ;;; PICK-BACKUP-NAME -- internal
1932 ;;;
1933 ;;; Pick a name to use for the backup file.
1934 ;;;
1935 (defvar *backup-extension* ".BAK"
1936 "This is a string that OPEN tacks on the end of a file namestring to produce
1937 a name for the :if-exists :rename-and-delete and :rename options. Also,
1938 this can be a function that takes a namestring and returns a complete
1939 namestring.")
1940 ;;;
1941 (defun pick-backup-name (name)
1942 (declare (type simple-string name))
1943 (let ((ext *backup-extension*))
1944 (etypecase ext
1945 (simple-string (concatenate 'simple-string name ext))
1946 (function (funcall ext name)))))
1947
1948 ;;; NEXT-VERSION -- internal
1949 ;;;
1950 ;;; Find the next available versioned name for a file.
1951 ;;;
1952 (defun next-version (name)
1953 (declare (type simple-string name))
1954 (let* ((sep (position #\/ name :from-end t))
1955 (base (if sep (subseq name 0 (1+ sep)) ""))
1956 (dir (unix:open-dir base)))
1957 (multiple-value-bind (name type version)
1958 (extract-name-type-and-version name (if sep (1+ sep) 0) (length name))
1959 (let ((version (if (symbolp version) 1 (1+ version)))
1960 (match (if type
1961 (concatenate 'string name "." type ".~")
1962 (concatenate 'string name ".~"))))
1963 (when dir
1964 (unwind-protect
1965 (loop
1966 (let ((name (unix:read-dir dir)))
1967 (cond ((null name) (return))
1968 ((and (> (length name) (length match))
1969 (string= name match :end1 (length match)))
1970 (multiple-value-bind (v e)
1971 (parse-integer name :start (length match)
1972 :junk-allowed t)
1973 (when (and v
1974 (= (length name) (1+ e))
1975 (char= (schar name e) #\~))
1976 (setq version (max version (1+ v)))))))))
1977 (unix:close-dir dir)))
1978 (concatenate 'string base
1979 match (quick-integer-to-string version) "~")))))
1980
1981 ;;; ASSURE-ONE-OF -- internal
1982 ;;;
1983 ;;; Assure that the given arg is one of the given list of valid things.
1984 ;;; Allow the user to fix any problems.
1985 ;;;
1986 (defun assure-one-of (item list what)
1987 (unless (member item list)
1988 (loop
1989 (cerror (intl:gettext "Enter new value for ~*~S")
1990 (intl:gettext "~S is invalid for ~S. Must be one of~{ ~S~}")
1991 item
1992 what
1993 list)
1994 (format (the stream *query-io*) (intl:gettext "Enter new value for ~S: ") what)
1995 (force-output *query-io*)
1996 (setf item (read *query-io*))
1997 (when (member item list)
1998 (return))))
1999 item)
2000
2001 ;;; DO-OLD-RENAME -- Internal
2002 ;;;
2003 ;;; Rename Namestring to Original. First, check if we have write access,
2004 ;;; since we don't want to trash unwritable files even if we technically can.
2005 ;;; We return true if we succeed in renaming.
2006 ;;;
2007 (defun do-old-rename (namestring original)
2008 (unless (unix:unix-access namestring unix:w_ok)
2009 (cerror (intl:gettext "Try to rename it anyway.") (intl:gettext "File ~S is not writable.") namestring))
2010 (multiple-value-bind
2011 (okay err)
2012 (unix:unix-rename namestring original)
2013 (cond (okay t)
2014 (t
2015 (cerror (intl:gettext "Use :SUPERSEDE instead.")
2016 (intl:gettext "Could not rename ~S to ~S: ~A.")
2017 namestring
2018 original
2019 (unix:get-unix-error-msg err))
2020 nil))))
2021
2022 ;;; FD-OPEN -- Internal
2023 ;;;
2024 ;;; Open a file.
2025 ;;;
2026 (defun fd-open (pathname direction if-exists if-exists-given
2027 if-does-not-exist if-does-not-exist-given)
2028 (declare (type pathname pathname)
2029 (type (member :input :output :io :probe) direction)
2030 (type (member :error :new-version :rename :rename-and-delete
2031 :overwrite :append :supersede nil) if-exists)
2032 (type (member :error :create nil) if-does-not-exist))
2033 (multiple-value-bind (input output mask)
2034 (ecase direction
2035 (:input (values t nil unix:o_rdonly))
2036 (:output (values nil t unix:o_wronly))
2037 (:io (values t t unix:o_rdwr))
2038 (:probe (values t nil unix:o_rdonly)))
2039 (declare (type index mask))
2040 ;; Process if-exists argument if we are doing any output.
2041 (cond (output
2042 (unless if-exists-given
2043 (setf if-exists
2044 (if (eq (pathname-version pathname) :newest)
2045 :new-version
2046 :error)))
2047 (case if-exists
2048 ((:error nil)
2049 (setf mask (logior mask unix:o_excl)))
2050 ((:new-version :rename :rename-and-delete)
2051 (setf mask (logior mask unix:o_creat)))
2052 (:supersede
2053 (setf mask (logior mask unix:o_trunc)))))
2054 (t
2055 (setf if-exists nil))) ; :ignore-this-arg
2056
2057 (unless if-does-not-exist-given
2058 (setf if-does-not-exist
2059 (cond ((eq direction :input) :error)
2060 ((and output
2061 (member if-exists '(:overwrite :append)))
2062 :error)
2063 ((eq direction :probe)
2064 nil)
2065 (t
2066 :create))))
2067 (if (eq if-does-not-exist :create)
2068 (setf mask (logior mask unix:o_creat)))
2069
2070 (let ((name (cond ((unix-namestring pathname input))
2071 ((and input (eq if-does-not-exist :create))
2072 (unix-namestring pathname nil)))))
2073 (let ((original (cond ((eq if-exists :new-version)
2074 (next-version name))
2075 ((member if-exists '(:rename :rename-and-delete))
2076 (pick-backup-name name))))
2077 (delete-original (eq if-exists :rename-and-delete))
2078 (mode #o666))
2079 (when original
2080 ;; We are doing a :rename or :rename-and-delete.
2081 ;; Determine if the file already exists, make sure the original
2082 ;; file is not a directory and keep the mode
2083 (let ((exists
2084 (and name
2085 (multiple-value-bind
2086 (okay err/dev inode orig-mode)
2087 (unix:unix-stat name)
2088 (declare (ignore inode)
2089 (type (or index null) orig-mode))
2090 (cond
2091 (okay
2092 (when (and output (= (logand orig-mode #o170000)
2093 #o40000))
2094 (error 'simple-file-error
2095 :pathname pathname
2096 :format-control
2097 (intl:gettext "Cannot open ~S for output: Is a directory.")
2098 :format-arguments (list name)))
2099 (setf mode (logand orig-mode #o777))
2100 t)
2101 ((eql err/dev unix:enoent)
2102 nil)
2103 (t
2104 (error 'simple-file-error
2105 :pathname pathname
2106 :format-control (intl:gettext "Cannot find ~S: ~A")
2107 :format-arguments
2108 (list name
2109 (unix:get-unix-error-msg err/dev)))))))))
2110 (unless (and exists
2111 (do-old-rename name original))
2112 (setf original nil)
2113 (setf delete-original nil)
2114 ;; In order to use SUPERSEDE instead, we have
2115 ;; to make sure unix:o_creat corresponds to
2116 ;; if-does-not-exist. unix:o_creat was set
2117 ;; before because of if-exists being :rename.
2118 (unless (eq if-does-not-exist :create)
2119 (setf mask (logior (logandc2 mask unix:o_creat)
2120 unix:o_trunc)))
2121 (setf if-exists :supersede))))
2122
2123 ;; Okay, now we can try the actual open.
2124 (loop
2125 (multiple-value-bind (fd errno)
2126 (if name
2127 (unix:unix-open name mask mode)
2128 (values nil unix:enoent))
2129 (cond ((fixnump fd)
2130 (when (eq if-exists :append)
2131 (unix:unix-lseek fd 0 unix:l_xtnd)) ; SEEK_END
2132 (return (values fd name original delete-original)))
2133 ((eql errno unix:enoent)
2134 (case if-does-not-exist
2135 (:error
2136 (cerror (intl:gettext "Return NIL.")
2137 'simple-file-error
2138 :pathname pathname
2139 :format-control (intl:gettext "Error opening ~S, ~A.")
2140 :format-arguments
2141 (list pathname
2142 (unix:get-unix-error-msg errno))))
2143 (:create
2144 (cerror (intl:gettext "Return NIL.")
2145 'simple-file-error
2146 :pathname pathname
2147 :format-control
2148 (intl:gettext "Error creating ~S, path does not exist.")
2149 :format-arguments (list pathname))))
2150 (return nil))
2151 ((eql errno unix:eexist)
2152 (unless (eq nil if-exists)
2153 (cerror (intl:gettext "Return NIL.")
2154 'simple-file-error
2155 :pathname pathname
2156 :format-control (intl:gettext "Error opening ~S, ~A.")
2157 :format-arguments
2158 (list pathname
2159 (unix:get-unix-error-msg errno))))
2160 (return nil))
2161 ((eql errno unix:eacces)
2162 (cerror (intl:gettext "Try again.")
2163 'simple-file-error
2164 :pathname pathname
2165 :format-control (intl:gettext "Error opening ~S, ~A.")
2166 :format-arguments
2167 (list pathname
2168 (unix:get-unix-error-msg errno))))
2169 (t
2170 (cerror (intl:gettext "Return NIL.")
2171 'simple-file-error
2172 :pathname pathname
2173 :format-control (intl:gettext "Error opening ~S, ~A.")
2174 :format-arguments
2175 (list pathname
2176 (unix:get-unix-error-msg errno)))
2177 (return nil)))))))))
2178
2179 ;;; OPEN-FD-STREAM -- Internal
2180 ;;;
2181 ;;; Open an fd-stream connected to a file.
2182 ;;;
2183 (defun open-fd-stream (pathname &key (direction :input)
2184 (element-type 'base-char)
2185 (if-exists nil if-exists-given)
2186 (if-does-not-exist nil if-does-not-exist-given)
2187 (external-format :default)
2188 class
2189 decoding-error encoding-error)
2190 (declare (type pathname pathname)
2191 (type (member :input :output :io :probe) direction)
2192 (type (member :error :new-version :rename :rename-and-delete
2193 :overwrite :append :supersede nil) if-exists)
2194 (type (member :error :create nil) if-does-not-exist))
2195 (multiple-value-bind (fd namestring original delete-original)
2196 (fd-open pathname direction if-exists if-exists-given
2197 if-does-not-exist if-does-not-exist-given)
2198 (when fd
2199 (case direction
2200 ((:input :output :io)
2201 ;; We use the :class option to tell us if we want a
2202 ;; binary-text stream or not.
2203 (make-fd-stream fd
2204 :input (member direction '(:input :io))
2205 :output (member direction '(:output :io))
2206 :element-type element-type
2207 :file namestring
2208 :original original
2209 :delete-original delete-original
2210 :pathname pathname
2211 :input-buffer-p t
2212 :auto-close t
2213 :external-format external-format
2214 :binary-stream-p class
2215 :decoding-error decoding-error
2216 :encoding-error encoding-error))
2217 (:probe
2218 (let ((stream (%make-fd-stream :name namestring :fd fd
2219 :pathname pathname
2220 :element-type element-type)))
2221 (close stream)
2222 stream))))))
2223
2224 ;;; OPEN -- public
2225 ;;;
2226 ;;; Open the given file.
2227 ;;;
2228 (defun open (filename &rest options
2229 &key (direction :input)
2230 (element-type 'base-char element-type-given)
2231 (if-exists nil if-exists-given)
2232 (if-does-not-exist nil if-does-not-exist-given)
2233 (external-format :default)
2234 class mapped input-handle output-handle
2235 decoding-error encoding-error
2236 &allow-other-keys
2237 &aux ; Squelch assignment warning.
2238 (options options)
2239 (direction direction)
2240 (if-does-not-exist if-does-not-exist)
2241 (if-exists if-exists))
2242 "Return a stream which reads from or writes to Filename.
2243 Defined keywords:
2244 :direction - one of :input, :output, :io, or :probe
2245 :element-type - Type of object to read or write, default BASE-CHAR
2246 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
2247 :overwrite, :append, :supersede or nil
2248 :if-does-not-exist - one of :error, :create or nil
2249 :external-format - an external format name
2250 :decoding-error - How to handle decoding errors from the external format.
2251 If a character, then that character is used as
2252 the replacment character for all errors. If T,
2253 then a continuable error is signaled. If
2254 continued, the Unicode replacement character is
2255 used. Otherwise, it should be a symbol or
2256 function of 3 arguments. If it returns, it
2257 should return a code point to use as the
2258 replacment. The function arguments are a
2259 format message string, the offending octet, and
2260 the number of octets read in the current
2261 encoding.
2262 :encoding-error - Like :decoding-error, but for errors when encoding the
2263 stream. If a character, that character is used
2264 as the replacment code point. Otherwise, it
2265 should be a symbol or function oof two
2266 arguments: a format message string and the
2267 incorrect codepoint.
2268
2269 See the manual for details."
2270 (declare (ignore element-type external-format input-handle output-handle
2271 decoding-error encoding-error))
2272
2273 ;; OPEN signals a file-error if the filename is wild.
2274 (when (wild-pathname-p filename)
2275 (error 'file-error :pathname filename))
2276
2277 ;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
2278 (setq direction
2279 (assure-one-of direction
2280 '(:input :output :io :probe)
2281 :direction))
2282 (setf (getf options :direction) direction)
2283
2284 (when (and if-exists-given (member direction '(:output :io)))
2285 (setq if-exists
2286 (assure-one-of if-exists
2287 '(:error :new-version :rename
2288 :rename-and-delete :overwrite
2289 :append :supersede nil)
2290 :if-exists))
2291 (setf (getf options :if-exists) if-exists))
2292
2293 (when if-does-not-exist-given
2294 (setq if-does-not-exist
2295 (assure-one-of if-does-not-exist
2296 '(:error :create nil)
2297 :if-does-not-exist))
2298 (setf (getf options :if-does-not-exist) if-does-not-exist))
2299
2300 (let ((filespec (merge-pathnames filename))
2301 (options (copy-list options))
2302 (class (or class 'fd-stream)))
2303 (cond ((eq class 'fd-stream)
2304 (remf options :class)
2305 (remf options :mapped)
2306 (remf options :input-handle)
2307 (remf options :output-handle)
2308 (apply #'open-fd-stream filespec options))
2309 ((eq class 'binary-text-stream)
2310 ;; Like fd-stream, but binary and text allowed. This is
2311 ;; indicated by leaving the :class option around for
2312 ;; open-fd-stream to see.
2313 (remf options :mapped)
2314 (remf options :input-handle)
2315 (remf options :output-handle)
2316 (apply #'open-fd-stream filespec options))
2317 ((subtypep class 'stream:simple-stream)
2318 (when element-type-given
2319 (cerror (intl:gettext "Do it anyway.")
2320 (intl:gettext "Can't create simple-streams with an element-type.")))
2321 (when (and (eq class 'stream:file-simple-stream) mapped)
2322 (setq class 'stream:mapped-file-simple-stream)
2323 (setf (getf options :class) 'stream:mapped-file-simple-stream))
2324 (when (subtypep class 'stream:file-simple-stream)
2325 (when (eq direction :probe)
2326 (setq class 'stream:probe-simple-stream)))
2327 (apply #'make-instance class :filename filespec options))
2328 ((subtypep class 'ext:fundamental-stream)
2329 (remf options :class)
2330 (remf options :mapped)
2331 (remf options :input-handle)
2332 (remf options :output-handle)
2333 (let ((stream (apply #'open-fd-stream filespec options)))
2334 (when stream
2335 (make-instance class :lisp-stream stream))))
2336 (t
2337 (error (intl:gettext "Unable to open streams of class ~S.") class)))))
2338
2339 ;;;; Initialization.
2340
2341 (defvar *tty* nil
2342 "The stream connected to the controlling terminal or NIL if there is none.")
2343 (defvar *stdin* nil
2344 "The stream connected to the standard input (file descriptor 0).")
2345 (defvar *stdout* nil
2346 "The stream connected to the standard output (file descriptor 1).")
2347 (defvar *stderr* nil
2348 "The stream connected to the standard error output (file descriptor 2).")
2349
2350 ;;; STREAM-INIT -- internal interface
2351 ;;;
2352 ;;; Called when the cold load is first started up.
2353 ;;;
2354 (defun stream-init ()
2355 (stream-reinit)
2356 (setf *terminal-io* (make-synonym-stream '*tty*))
2357 (setf *standard-output* (make-synonym-stream '*stdout*))
2358 (setf *standard-input*
2359 (make-two-way-stream (make-synonym-stream '*stdin*)
2360 *standard-output*))
2361 (setf *error-output* (make-synonym-stream '*stderr*))
2362 (setf *query-io* (make-synonym-stream '*terminal-io*))
2363 (setf *debug-io* *query-io*)
2364 (setf *trace-output* *standard-output*)
2365 nil)
2366
2367 ;;; STREAM-REINIT -- internal interface
2368 ;;;
2369 ;;; Called whenever a saved core is restarted.
2370 ;;;
2371 (defun stream-reinit ()
2372 (setf *available-buffers* nil)
2373 (setf *stdin*
2374 (make-fd-stream 0 :name "Standard Input" :input t :buffering :line
2375 :external-format :iso8859-1))
2376 (setf *stdout*
2377 (make-fd-stream 1 :name "Standard Output" :output t :buffering :line
2378 :external-format :iso8859-1))
2379 (setf *stderr*
2380 (make-fd-stream 2 :name "Standard Error" :output t :buffering :line
2381 :external-format :iso8859-1))
2382 (let ((tty (and (not *batch-mode*)
2383 (unix:unix-open "/dev/tty" unix:o_rdwr #o666))))
2384 (setf *tty*
2385 (if tty
2386 (make-fd-stream tty :name "the Terminal" :input t :output t
2387 :buffering :line :auto-close t
2388 :external-format :iso8859-1)
2389 (make-two-way-stream *stdin* *stdout*))))
2390 nil)
2391
2392
2393 ;;;; Beeping.
2394
2395 (defun default-beep-function (stream)
2396 (write-char #\bell stream)
2397 (finish-output stream))
2398
2399 (defvar *beep-function* #'default-beep-function
2400 "This is called in BEEP to feep the user. It takes a stream.")
2401
2402 (defun beep (&optional (stream *terminal-io*))
2403 (funcall *beep-function* stream))
2404
2405
2406 ;;; File-Name -- internal interface
2407 ;;;
2408 ;;; Kind of like File-Position, but is an internal hack used by the filesys
2409 ;;; stuff to get and set the file name.
2410 ;;;
2411 (defun file-name (stream &optional new-name)
2412 (typecase stream
2413 (stream:simple-stream
2414 (if new-name
2415 (stream::%file-rename stream new-name)
2416 (stream::%file-name stream)))
2417 (fd-stream
2418 (cond (new-name
2419 (setf (fd-stream-pathname stream) new-name)
2420 (setf (fd-stream-file stream)
2421 (unix-namestring new-name nil))
2422 t)
2423 (t
2424 (fd-stream-pathname stream))))))
2425
2426
2427
2428 #+unicode
2429 (stream::def-ef-macro ef-strlen (extfmt lisp stream::+ef-max+ stream::+ef-str+)
2430 ;; While it would be nice not to have to call CHAR-TO-OCTETS to
2431 ;; figure out the length when the external format has fixed size
2432 ;; outputs, we can't. For example, utf16 will output a BOM, which
2433 ;; wouldn't be reflected in the count if we don't call
2434 ;; CHAR-TO-OCTETS.
2435 `(lambda (stream object &aux (count 0))
2436 (declare (type fd-stream stream)
2437 (type (or character string) object)
2438 (type (and fixnum unsigned-byte) count)
2439 #|(optimize (speed 3) (space 0) (debug 0) (safety 0))|#)
2440 (labels ((efstate (state)
2441 (stream::copy-state ,extfmt state))
2442 (eflen (char)
2443 (stream::char-to-octets ,extfmt char
2444 (fd-stream-co-state stream)
2445 (lambda (byte)
2446 (declare (ignore byte))
2447 (incf count))
2448 (fd-stream-char-to-octets-erroor stream))))
2449 (let* ((co-state (fd-stream-co-state stream))
2450 (old-ef-state (efstate (cdr (fd-stream-co-state stream))))
2451 (old-state (cons (car co-state) old-ef-state)))
2452 (etypecase object
2453 (character (eflen object))
2454 (string (dovector (ch object) (eflen ch))))
2455 ;; Restore state
2456 (setf (fd-stream-co-state stream) old-state)
2457 count))))
2458
2459
2460 (defun file-string-length (stream object)
2461 (declare (type (or string character) object)
2462 (type (or file-stream broadcast-stream stream:simple-stream) stream))
2463 "Return the delta in Stream's FILE-POSITION that would be caused by writing
2464 Object to Stream. Non-trivial only in implementations that support
2465 international character sets."
2466 (typecase stream
2467 (stream:simple-stream (stream::%file-string-length stream object))
2468 (broadcast-stream
2469 ;; CLHS says we must return 1 in this case
2470 1)
2471 #+unicode
2472 (t (funcall (ef-strlen (fd-stream-external-format stream))
2473 stream object))
2474 #-unicode
2475 (t (etypecase object
2476 (character 1)
2477 (string (length object))))))
2478
2479 #+unicode
2480 (stream::def-ef-macro ef-copy-state (extfmt lisp stream::+ef-max+ stream::+ef-copy-state+)
2481 ;; Return a copy of the state of an external format.
2482 `(lambda (state)
2483 (declare (ignorable state))
2484 (stream::copy-state ,extfmt state)))

  ViewVC Help
Powered by ViewVC 1.1.5