/[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.89 - (show annotations)
Mon Aug 10 16:47:41 2009 UTC (4 years, 8 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2009-08
Changes since 1.88: +83 -81 lines
Fixes from Paul Foley:

o Standard streams no longer change formats when
  *default-external-format* changes.  Use
  stream:set-system-external-format instead, or (setf
  external-format).
o char-to-octets properly handles surrogate characters being written.
o Makes simple-streams work again.

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

  ViewVC Help
Powered by ViewVC 1.1.5