/[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.64 - (show annotations)
Mon Jun 2 14:42:50 2003 UTC (10 years, 10 months ago) by toy
Branch: MAIN
Changes since 1.63: +2 -2 lines
file-length returns two values instead of one, as required.  Bug and
solution from Wolfhard Buss.
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.64 2003/06/02 14:42:50 toy 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
26
27 (in-package "EXTENSIONS")
28
29 (export '(*backup-extension*))
30
31
32 (in-package "LISP")
33
34 (export '(file-stream file-string-length stream-external-format))
35
36
37 ;;;; Buffer manipulation routines.
38
39 (defvar *available-buffers* ()
40 "List of available buffers. Each buffer is an sap pointing to
41 bytes-per-buffer of memory.")
42
43 (defconstant bytes-per-buffer (* 4 1024)
44 "Number of bytes per buffer.")
45
46 ;;; NEXT-AVAILABLE-BUFFER -- Internal.
47 ;;;
48 ;;; Returns the next available buffer, creating one if necessary.
49 ;;;
50 (declaim (inline next-available-buffer))
51 ;;;
52 (defun next-available-buffer ()
53 (if *available-buffers*
54 (pop *available-buffers*)
55 (allocate-system-memory bytes-per-buffer)))
56
57
58 ;;;; The FD-STREAM structure.
59
60 ;;;; Superclass defined by the ANSI Spec
61 (defstruct (file-stream
62 (:include lisp-stream)
63 (:constructor nil)
64 (:copier nil)))
65
66 (defstruct (fd-stream
67 (:print-function %print-fd-stream)
68 (:constructor %make-fd-stream)
69 (:include file-stream
70 (misc #'fd-stream-misc-routine)))
71
72 (name nil) ; The name of this stream
73 (file nil) ; The file this stream is for
74 ;;
75 ;; The backup file namestring for the old file, for :if-exists :rename or
76 ;; :rename-and-delete.
77 (original nil :type (or simple-string null))
78 (delete-original nil) ; for :if-exists :rename-and-delete
79 ;;
80 ;;; Number of bytes per element.
81 (element-size 1 :type index)
82 (element-type 'base-char) ; The type of element being transfered.
83 (fd -1 :type fixnum) ; The file descriptor
84 ;;
85 ;; Controls when the output buffer is flushed.
86 (buffering :full :type (member :full :line :none))
87 ;;
88 ;; Character position if known.
89 (char-pos nil :type (or index null))
90 ;;
91 ;; T if input is waiting on FD. :EOF if we hit EOF.
92 (listen nil :type (member nil t :eof))
93 ;;
94 ;; The input buffer.
95 (unread nil)
96 (ibuf-sap nil :type (or system-area-pointer null))
97 (ibuf-length nil :type (or index null))
98 (ibuf-head 0 :type index)
99 (ibuf-tail 0 :type index)
100
101 ;; The output buffer.
102 (obuf-sap nil :type (or system-area-pointer null))
103 (obuf-length nil :type (or index null))
104 (obuf-tail 0 :type index)
105
106 ;; Output flushed, but not written due to non-blocking io.
107 (output-later nil)
108 (handler nil)
109 ;;
110 ;; Timeout specified for this stream, or NIL if none.
111 (timeout nil :type (or index null))
112 ;;
113 ;; Pathname of the file this stream is opened to (returned by PATHNAME.)
114 (pathname nil :type (or pathname null)))
115
116 (defun %print-fd-stream (fd-stream stream depth)
117 (declare (ignore depth) (stream stream))
118 (format stream "#<Stream for ~A>"
119 (fd-stream-name fd-stream)))
120
121
122 (define-condition io-timeout (stream-error)
123 ((direction :reader io-timeout-direction :initarg :direction))
124 (:report
125 (lambda (condition stream)
126 (declare (stream stream))
127 (format stream "Timeout ~(~A~)ing ~S."
128 (io-timeout-direction condition)
129 (stream-error-stream condition)))))
130
131
132 ;;;; Output routines and related noise.
133
134 (defvar *output-routines* ()
135 "List of all available output routines. Each element is a list of the
136 element-type output, the kind of buffering, the function name, and the number
137 of bytes per element.")
138
139 ;;; DO-OUTPUT-LATER -- internal
140 ;;;
141 ;;; Called by the server when we can write to the given file descriptor.
142 ;;; Attemt to write the data again. If it worked, remove the data from the
143 ;;; output-later list. If it didn't work, something is wrong.
144 ;;;
145 (defun do-output-later (stream)
146 (let* ((stuff (pop (fd-stream-output-later stream)))
147 (base (car stuff))
148 (start (cadr stuff))
149 (end (caddr stuff))
150 (reuse-sap (cadddr stuff))
151 (length (- end start)))
152 (declare (type index start end length))
153 (multiple-value-bind
154 (count errno)
155 (unix:unix-write (fd-stream-fd stream)
156 base
157 start
158 length)
159 (cond ((not count)
160 (if (= errno unix:ewouldblock)
161 (error "Write would have blocked, but SERVER told us to go.")
162 (error "While writing ~S: ~A"
163 stream (unix:get-unix-error-msg errno))))
164 ((eql count length) ; Hot damn, it workded.
165 (when reuse-sap
166 (push base *available-buffers*)))
167 ((not (null count)) ; Sorta worked.
168 (push (list base
169 (the index (+ start count))
170 end)
171 (fd-stream-output-later stream))))))
172 (unless (fd-stream-output-later stream)
173 (system:remove-fd-handler (fd-stream-handler stream))
174 (setf (fd-stream-handler stream) nil)))
175
176 ;;; OUTPUT-LATER -- internal
177 ;;;
178 ;;; Arange to output the string when we can write on the file descriptor.
179 ;;;
180 (defun output-later (stream base start end reuse-sap)
181 (cond ((null (fd-stream-output-later stream))
182 (setf (fd-stream-output-later stream)
183 (list (list base start end reuse-sap)))
184 (setf (fd-stream-handler stream)
185 (system:add-fd-handler (fd-stream-fd stream)
186 :output
187 #'(lambda (fd)
188 (declare (ignore fd))
189 (do-output-later stream)))))
190 (t
191 (nconc (fd-stream-output-later stream)
192 (list (list base start end reuse-sap)))))
193 (when reuse-sap
194 (let ((new-buffer (next-available-buffer)))
195 (setf (fd-stream-obuf-sap stream) new-buffer)
196 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
197
198 ;;; DO-OUTPUT -- internal
199 ;;;
200 ;;; Output the given noise. Check to see if there are any pending writes. If
201 ;;; so, just queue this one. Otherwise, try to write it. If this would block,
202 ;;; queue it.
203 ;;;
204 (defun do-output (stream base start end reuse-sap)
205 (declare (type fd-stream stream)
206 (type (or system-area-pointer (simple-array * (*))) base)
207 (type index start end))
208 (if (not (null (fd-stream-output-later stream))) ; something buffered.
209 (progn
210 (output-later stream base start end reuse-sap)
211 ;; ### check to see if any of this noise can be output
212 )
213 (let ((length (- end start)))
214 (multiple-value-bind
215 (count errno)
216 (unix:unix-write (fd-stream-fd stream) base start length)
217 (cond ((not count)
218 (if (= errno unix:ewouldblock)
219 (output-later stream base start end reuse-sap)
220 (error "While writing ~S: ~A"
221 stream (unix:get-unix-error-msg errno))))
222 ((not (eql count length))
223 (output-later stream base (the index (+ start count))
224 end reuse-sap)))))))
225
226
227 ;;; FLUSH-OUTPUT-BUFFER -- internal
228 ;;;
229 ;;; Flush any data in the output buffer.
230 ;;;
231 (defun flush-output-buffer (stream)
232 (let ((length (fd-stream-obuf-tail stream)))
233 (unless (= length 0)
234 (do-output stream (fd-stream-obuf-sap stream) 0 length t)
235 (setf (fd-stream-obuf-tail stream) 0))))
236
237 ;;; DEF-OUTPUT-ROUTINES -- internal
238 ;;;
239 ;;; Define output routines that output numbers size bytes long for the
240 ;;; given bufferings. Use body to do the actual output.
241 ;;;
242 (defmacro def-output-routines ((name size &rest bufferings) &body body)
243 (declare (optimize (speed 1)))
244 (cons 'progn
245 (mapcar
246 #'(lambda (buffering)
247 (let ((function
248 (intern (let ((*print-case* :upcase))
249 (format nil name (car buffering))))))
250 `(progn
251 (defun ,function (stream byte)
252 ,(unless (eq (car buffering) :none)
253 `(when (< (fd-stream-obuf-length stream)
254 (+ (fd-stream-obuf-tail stream)
255 ,size))
256 (flush-output-buffer stream)))
257 ,@body
258 (incf (fd-stream-obuf-tail stream) ,size)
259 ,(ecase (car buffering)
260 (:none
261 `(flush-output-buffer stream))
262 (:line
263 `(when (eq (char-code byte) (char-code #\Newline))
264 (flush-output-buffer stream)))
265 (:full
266 ))
267 (values))
268 (setf *output-routines*
269 (nconc *output-routines*
270 ',(mapcar
271 #'(lambda (type)
272 (list type
273 (car buffering)
274 function
275 size))
276 (cdr buffering)))))))
277 bufferings)))
278
279 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
280 1
281 (:none character)
282 (:line character)
283 (:full character))
284 (if (char= byte #\Newline)
285 (setf (fd-stream-char-pos stream) 0)
286 (incf (fd-stream-char-pos stream)))
287 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
288 (char-code byte)))
289
290 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
291 1
292 (:none (unsigned-byte 8))
293 (:full (unsigned-byte 8)))
294 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
295 byte))
296
297 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
298 1
299 (:none (signed-byte 8))
300 (:full (signed-byte 8)))
301 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
302 (fd-stream-obuf-tail stream))
303 byte))
304
305 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
306 2
307 (:none (unsigned-byte 16))
308 (:full (unsigned-byte 16)))
309 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
310 byte))
311
312 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
313 2
314 (:none (signed-byte 16))
315 (:full (signed-byte 16)))
316 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
317 (fd-stream-obuf-tail stream))
318 byte))
319
320 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
321 4
322 (:none (unsigned-byte 32))
323 (:full (unsigned-byte 32)))
324 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
325 byte))
326
327 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
328 4
329 (:none (signed-byte 32))
330 (:full (signed-byte 32)))
331 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
332 (fd-stream-obuf-tail stream))
333 byte))
334
335
336 ;;; OUTPUT-RAW-BYTES -- public
337 ;;;
338 ;;; Does the actual output. If there is space to buffer the string, buffer
339 ;;; it. If the string would normally fit in the buffer, but doesn't because
340 ;;; of other stuff in the buffer, flush the old noise out of the buffer and
341 ;;; put the string in it. Otherwise we have a very long string, so just
342 ;;; send it directly (after flushing the buffer, of course).
343 ;;;
344 (defun output-raw-bytes (stream thing &optional start end)
345 "Output THING to stream. THING can be any kind of vector or a sap. If THING
346 is a SAP, END must be supplied (as length won't work)."
347 (let ((start (or start 0))
348 (end (or end (length (the (simple-array * (*)) thing)))))
349 (declare (type index start end))
350 (let* ((len (fd-stream-obuf-length stream))
351 (tail (fd-stream-obuf-tail stream))
352 (space (- len tail))
353 (bytes (- end start))
354 (newtail (+ tail bytes)))
355 (cond ((minusp bytes) ; Error case
356 (cerror "Just go on as if nothing happened..."
357 "~S called with :END before :START!"
358 'output-raw-bytes))
359 ((zerop bytes)) ; Easy case
360 ((<= bytes space)
361 (if (system-area-pointer-p thing)
362 (system-area-copy thing
363 (* start vm:byte-bits)
364 (fd-stream-obuf-sap stream)
365 (* tail vm:byte-bits)
366 (* bytes vm:byte-bits))
367 (copy-to-system-area thing
368 (+ (* start vm:byte-bits)
369 (* vm:vector-data-offset vm:word-bits))
370 (fd-stream-obuf-sap stream)
371 (* tail vm:byte-bits)
372 (* bytes vm:byte-bits)))
373 (setf (fd-stream-obuf-tail stream) newtail))
374 ((<= bytes len)
375 (flush-output-buffer stream)
376 (if (system-area-pointer-p thing)
377 (system-area-copy thing
378 (* start vm:byte-bits)
379 (fd-stream-obuf-sap stream)
380 0
381 (* bytes vm:byte-bits))
382 (copy-to-system-area thing
383 (+ (* start vm:byte-bits)
384 (* vm:vector-data-offset vm:word-bits))
385 (fd-stream-obuf-sap stream)
386 0
387 (* bytes vm:byte-bits)))
388 (setf (fd-stream-obuf-tail stream) bytes))
389 (t
390 (flush-output-buffer stream)
391 (do-output stream thing start end nil))))))
392
393 ;;; FD-SOUT -- internal
394 ;;;
395 ;;; Routine to use to output a string. If the stream is unbuffered, slam
396 ;;; the string down the file descriptor, otherwise use OUTPUT-RAW-BYTES to
397 ;;; buffer the string. Update charpos by checking to see where the last newline
398 ;;; was.
399 ;;;
400 ;;; Note: some bozos (the FASL dumper) call write-string with things other
401 ;;; than strings. Therefore, we must make sure we have a string before calling
402 ;;; position on it.
403 ;;;
404 (defun fd-sout (stream thing start end)
405 (let ((start (or start 0))
406 (end (or end (length (the vector thing)))))
407 (declare (type index start end))
408 (if (stringp thing)
409 (let ((last-newline (and (find #\newline (the simple-string thing)
410 :start start :end end)
411 (position #\newline (the simple-string thing)
412 :from-end t
413 :start start
414 :end end))))
415 (ecase (fd-stream-buffering stream)
416 (:full
417 (output-raw-bytes stream thing start end))
418 (:line
419 (output-raw-bytes stream thing start end)
420 (when last-newline
421 (flush-output-buffer stream)))
422 (:none
423 (do-output stream thing start end nil)))
424 (if last-newline
425 (setf (fd-stream-char-pos stream)
426 (- end last-newline 1))
427 (incf (fd-stream-char-pos stream)
428 (- end start))))
429 (ecase (fd-stream-buffering stream)
430 ((:line :full)
431 (output-raw-bytes stream thing start end))
432 (:none
433 (do-output stream thing start end nil))))))
434
435 ;;; PICK-OUTPUT-ROUTINE -- internal
436 ;;;
437 ;;; Find an output routine to use given the type and buffering. Return as
438 ;;; multiple values the routine, the real type transfered, and the number of
439 ;;; bytes per element.
440 ;;;
441 (defun pick-output-routine (type buffering)
442 (dolist (entry *output-routines*)
443 (when (and (subtypep type (car entry))
444 (eq buffering (cadr entry)))
445 (return (values (symbol-function (caddr entry))
446 (car entry)
447 (cadddr entry))))))
448
449
450 ;;;; Input routines and related noise.
451
452 (defvar *input-routines* ()
453 "List of all available input routines. Each element is a list of the
454 element-type input, the function name, and the number of bytes per element.")
455
456 ;;; DO-INPUT -- internal
457 ;;;
458 ;;; Fills the input buffer, and returns the first character. Throws to
459 ;;; eof-input-catcher if the eof was reached. Drops into system:server if
460 ;;; necessary.
461 ;;;
462 (defun do-input (stream)
463 (let ((fd (fd-stream-fd stream))
464 (ibuf-sap (fd-stream-ibuf-sap stream))
465 (buflen (fd-stream-ibuf-length stream))
466 (head (fd-stream-ibuf-head stream))
467 (tail (fd-stream-ibuf-tail stream)))
468 (declare (type index head tail))
469 (unless (zerop head)
470 (cond ((eql head tail)
471 (setf head 0)
472 (setf tail 0)
473 (setf (fd-stream-ibuf-head stream) 0)
474 (setf (fd-stream-ibuf-tail stream) 0))
475 (t
476 (decf tail head)
477 (system-area-copy ibuf-sap (* head vm:byte-bits)
478 ibuf-sap 0 (* tail vm:byte-bits))
479 (setf head 0)
480 (setf (fd-stream-ibuf-head stream) 0)
481 (setf (fd-stream-ibuf-tail stream) tail))))
482 (setf (fd-stream-listen stream) nil)
483 (multiple-value-bind
484 (count errno)
485 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
486 (unix:fd-zero read-fds)
487 (unix:fd-set fd read-fds)
488 (unix:unix-fast-select (1+ fd) (alien:addr read-fds) nil nil 0 0))
489 ;; Wait if input is not available or if interrupted.
490 (when (or (eql count 0)
491 (and (not count) (eql errno unix:eintr)))
492 (unless #-mp (system:wait-until-fd-usable
493 fd :input (fd-stream-timeout stream))
494 #+mp (mp:process-wait-until-fd-usable
495 fd :input (fd-stream-timeout stream))
496 (error 'io-timeout :stream stream :direction :read))))
497 (multiple-value-bind
498 (count errno)
499 (unix:unix-read fd
500 (system:int-sap (+ (system:sap-int ibuf-sap) tail))
501 (- buflen tail))
502 (cond ((null count)
503 (if (eql errno unix:ewouldblock)
504 (progn
505 (unless #-mp (system:wait-until-fd-usable
506 fd :input (fd-stream-timeout stream))
507 #+mp (mp:process-wait-until-fd-usable
508 fd :input (fd-stream-timeout stream))
509 (error 'io-timeout :stream stream :direction :read))
510 (do-input stream))
511 (error "Error reading ~S: ~A"
512 stream
513 (unix:get-unix-error-msg errno))))
514 ((zerop count)
515 (setf (fd-stream-listen stream) :eof)
516 (throw 'eof-input-catcher nil))
517 (t
518 (incf (fd-stream-ibuf-tail stream) count))))))
519
520 ;;; INPUT-AT-LEAST -- internal
521 ;;;
522 ;;; Makes sure there are at least ``bytes'' number of bytes in the input
523 ;;; buffer. Keeps calling do-input until that condition is met.
524 ;;;
525 (defmacro input-at-least (stream bytes)
526 (let ((stream-var (gensym))
527 (bytes-var (gensym)))
528 `(let ((,stream-var ,stream)
529 (,bytes-var ,bytes))
530 (loop
531 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
532 (fd-stream-ibuf-head ,stream-var))
533 ,bytes-var)
534 (return))
535 (do-input ,stream-var)))))
536
537 ;;; INPUT-WRAPPER -- intenal
538 ;;;
539 ;;; Macro to wrap around all input routines to handle eof-error noise.
540 ;;;
541 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
542 (let ((stream-var (gensym))
543 (element-var (gensym)))
544 `(let ((,stream-var ,stream))
545 (if (fd-stream-unread ,stream-var)
546 (prog1
547 (fd-stream-unread ,stream-var)
548 (setf (fd-stream-unread ,stream-var) nil)
549 (setf (fd-stream-listen ,stream-var) nil))
550 (let ((,element-var
551 (catch 'eof-input-catcher
552 (input-at-least ,stream-var ,bytes)
553 ,@read-forms)))
554 (cond (,element-var
555 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
556 ,element-var)
557 (t
558 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
559
560 ;;; DEF-INPUT-ROUTINE -- internal
561 ;;;
562 ;;; Defines an input routine.
563 ;;;
564 (defmacro def-input-routine (name
565 (type size sap head)
566 &rest body)
567 `(progn
568 (defun ,name (stream eof-error eof-value)
569 (input-wrapper (stream ,size eof-error eof-value)
570 (let ((,sap (fd-stream-ibuf-sap stream))
571 (,head (fd-stream-ibuf-head stream)))
572 ,@body)))
573 (setf *input-routines*
574 (nconc *input-routines*
575 (list (list ',type ',name ',size))))))
576
577 ;;; INPUT-CHARACTER -- internal
578 ;;;
579 ;;; Routine to use in stream-in slot for reading string chars.
580 ;;;
581 (def-input-routine input-character
582 (character 1 sap head)
583 (code-char (sap-ref-8 sap head)))
584
585 ;;; INPUT-UNSIGNED-8BIT-BYTE -- internal
586 ;;;
587 ;;; Routine to read in an unsigned 8 bit number.
588 ;;;
589 (def-input-routine input-unsigned-8bit-byte
590 ((unsigned-byte 8) 1 sap head)
591 (sap-ref-8 sap head))
592
593 ;;; INPUT-SIGNED-8BIT-BYTE -- internal
594 ;;;
595 ;;; Routine to read in a signed 8 bit number.
596 ;;;
597 (def-input-routine input-signed-8bit-number
598 ((signed-byte 8) 1 sap head)
599 (signed-sap-ref-8 sap head))
600
601 ;;; INPUT-UNSIGNED-16BIT-BYTE -- internal
602 ;;;
603 ;;; Routine to read in an unsigned 16 bit number.
604 ;;;
605 (def-input-routine input-unsigned-16bit-byte
606 ((unsigned-byte 16) 2 sap head)
607 (sap-ref-16 sap head))
608
609 ;;; INPUT-SIGNED-16BIT-BYTE -- internal
610 ;;;
611 ;;; Routine to read in a signed 16 bit number.
612 ;;;
613 (def-input-routine input-signed-16bit-byte
614 ((signed-byte 16) 2 sap head)
615 (signed-sap-ref-16 sap head))
616
617 ;;; INPUT-UNSIGNED-32BIT-BYTE -- internal
618 ;;;
619 ;;; Routine to read in a unsigned 32 bit number.
620 ;;;
621 (def-input-routine input-unsigned-32bit-byte
622 ((unsigned-byte 32) 4 sap head)
623 (sap-ref-32 sap head))
624
625 ;;; INPUT-SIGNED-32BIT-BYTE -- internal
626 ;;;
627 ;;; Routine to read in a signed 32 bit number.
628 ;;;
629 (def-input-routine input-signed-32bit-byte
630 ((signed-byte 32) 4 sap head)
631 (signed-sap-ref-32 sap head))
632
633 ;;; PICK-INPUT-ROUTINE -- internal
634 ;;;
635 ;;; Find an input routine to use given the type. Return as multiple values
636 ;;; the routine, the real type transfered, and the number of bytes per element.
637 ;;;
638 (defun pick-input-routine (type)
639 (dolist (entry *input-routines*)
640 (when (subtypep type (car entry))
641 (return (values (symbol-function (cadr entry))
642 (car entry)
643 (caddr entry))))))
644
645 ;;; STRING-FROM-SAP -- internal
646 ;;;
647 ;;; Returns a string constructed from the sap, start, and end.
648 ;;;
649 (defun string-from-sap (sap start end)
650 (declare (type index start end))
651 (let* ((length (- end start))
652 (string (make-string length)))
653 (copy-from-system-area sap (* start vm:byte-bits)
654 string (* vm:vector-data-offset vm:word-bits)
655 (* length vm:byte-bits))
656 string))
657
658 #|
659 ;;; FD-STREAM-READ-N-BYTES -- internal
660 ;;;
661 ;;; This version waits using server. I changed to the non-server version
662 ;;; because it allows this method to be used by CLX w/o confusing serve-event.
663 ;;; The non-server method is also significantly more efficient for large
664 ;;; reads. -- Ram
665 ;;;
666 ;;; The n-bin routine.
667 ;;;
668 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
669 (declare (type stream stream) (type index start requested))
670 (let* ((sap (fd-stream-ibuf-sap stream))
671 (elsize (fd-stream-element-size stream))
672 (offset (* elsize start))
673 (bytes (* elsize requested))
674 (result
675 (catch 'eof-input-catcher
676 (loop
677 (input-at-least stream 1)
678 (let* ((head (fd-stream-ibuf-head stream))
679 (tail (fd-stream-ibuf-tail stream))
680 (available (- tail head))
681 (copy (min available bytes)))
682 (if (typep buffer 'system-area-pointer)
683 (system-area-copy sap (* head vm:byte-bits)
684 buffer (* offset vm:byte-bits)
685 (* copy vm:byte-bits))
686 (copy-from-system-area sap (* head vm:byte-bits)
687 buffer (+ (* offset vm:byte-bits)
688 (* vm:vector-data-offset
689 vm:word-bits))
690 (* copy vm:byte-bits)))
691 (incf (fd-stream-ibuf-head stream) copy)
692 (incf offset copy)
693 (decf bytes copy))
694 (when (zerop bytes)
695 (return requested))))))
696 (or result
697 (eof-or-lose stream eof-error-p
698 (- requested (/ bytes elsize))))))
699 |#
700
701
702 ;;; FD-STREAM-READ-N-BYTES -- internal
703 ;;;
704 ;;; The N-Bin method for FD-STREAMs. This doesn't use the SERVER; it blocks
705 ;;; in UNIX-READ. This allows the method to be used to implementing reading
706 ;;; for CLX. It is generally used where there is a definite amount of reading
707 ;;; to be done, so blocking isn't too problematical.
708 ;;;
709 ;;; We copy buffered data into the buffer. If there is enough, just return.
710 ;;; Otherwise, we see if the amount of additional data needed will fit in the
711 ;;; stream buffer. If not, inhibit GCing (so we can have a SAP into the Buffer
712 ;;; argument), and read directly into the user supplied buffer. Otherwise,
713 ;;; read a buffer-full into the stream buffer and then copy the amount we need
714 ;;; out.
715 ;;;
716 ;;; We loop doing the reads until we either get enough bytes or hit EOF. We
717 ;;; must loop when eof-errorp is T because some streams (like pipes) may return
718 ;;; a partial amount without hitting EOF.
719 ;;;
720 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
721 (declare (type stream stream) (type index start requested))
722 (let* ((sap (fd-stream-ibuf-sap stream))
723 (offset start)
724 (head (fd-stream-ibuf-head stream))
725 (tail (fd-stream-ibuf-tail stream))
726 (available (- tail head))
727 (copy (min requested available)))
728 (declare (type index offset head tail available copy))
729 (unless (zerop copy)
730 (if (typep buffer 'system-area-pointer)
731 (system-area-copy sap (* head vm:byte-bits)
732 buffer (* offset vm:byte-bits)
733 (* copy vm:byte-bits))
734 (copy-from-system-area sap (* head vm:byte-bits)
735 buffer (+ (* offset vm:byte-bits)
736 (* vm:vector-data-offset
737 vm:word-bits))
738 (* copy vm:byte-bits)))
739 (incf (fd-stream-ibuf-head stream) copy))
740 (cond
741 ((or (= copy requested)
742 (and (not eof-error-p) (/= copy 0)))
743 copy)
744 (t
745 (setf (fd-stream-ibuf-head stream) 0)
746 (setf (fd-stream-ibuf-tail stream) 0)
747 (setf (fd-stream-listen stream) nil)
748 (let ((now-needed (- requested copy))
749 (len (fd-stream-ibuf-length stream)))
750 (declare (type index now-needed len))
751 (cond
752 ((> now-needed len)
753 ;;
754 ;; If the desired amount is greater than the stream buffer size, then
755 ;; read directly into the destination, incrementing the start
756 ;; accordingly. In this case, we never leave anything in the stream
757 ;; buffer.
758 (system:without-gcing
759 (loop
760 (multiple-value-bind
761 (count err)
762 (unix:unix-read (fd-stream-fd stream)
763 (sap+ (if (typep buffer 'system-area-pointer)
764 buffer
765 (vector-sap buffer))
766 (+ offset copy))
767 now-needed)
768 (declare (type (or index null) count))
769 (unless count
770 (error "Error reading ~S: ~A" stream
771 (unix:get-unix-error-msg err)))
772 (decf now-needed count)
773 (if eof-error-p
774 (when (zerop count)
775 (error 'end-of-file :stream stream))
776 (return (- requested now-needed)))
777 (when (zerop now-needed) (return requested))
778 (incf offset count)))))
779 (t
780 ;;
781 ;; If we want less than the buffer size, then loop trying to fill the
782 ;; stream buffer and copying what we get into the destination. When
783 ;; we have enough, we leave what's left in the stream buffer.
784 (loop
785 (multiple-value-bind
786 (count err)
787 (unix:unix-read (fd-stream-fd stream) sap len)
788 (declare (type (or index null) count))
789 (unless count
790 (error "Error reading ~S: ~A" stream
791 (unix:get-unix-error-msg err)))
792 (when (and eof-error-p (zerop count))
793 (error 'end-of-file :stream stream))
794
795 (let* ((copy (min now-needed count))
796 (copy-bits (* copy vm:byte-bits))
797 (buffer-start-bits
798 (* (+ offset available) vm:byte-bits)))
799 (declare (type index copy copy-bits buffer-start-bits))
800 (if (typep buffer 'system-area-pointer)
801 (system-area-copy sap 0
802 buffer buffer-start-bits
803 copy-bits)
804 (copy-from-system-area sap 0
805 buffer (+ buffer-start-bits
806 (* vm:vector-data-offset
807 vm:word-bits))
808 copy-bits))
809
810 (decf now-needed copy)
811 (when (or (zerop now-needed) (not eof-error-p))
812 (setf (fd-stream-ibuf-head stream) copy)
813 (setf (fd-stream-ibuf-tail stream) count)
814 (return (- requested now-needed)))
815 (incf offset copy)))))))))))
816
817
818 ;;;; Utility functions (misc routines, etc)
819
820 ;;; SET-ROUTINES -- internal
821 ;;;
822 ;;; Fill in the various routine slots for the given type. Input-p and
823 ;;; output-p indicate what slots to fill. The buffering slot must be set prior
824 ;;; to calling this routine.
825 ;;;
826 (defun set-routines (stream type input-p output-p buffer-p)
827 (let ((target-type (case type
828 ((:default unsigned-byte)
829 '(unsigned-byte 8))
830 (signed-byte
831 '(signed-byte 8))
832 (t
833 type)))
834 (input-type nil)
835 (output-type nil)
836 (input-size nil)
837 (output-size nil))
838
839 (when (fd-stream-obuf-sap stream)
840 (push (fd-stream-obuf-sap stream) *available-buffers*)
841 (setf (fd-stream-obuf-sap stream) nil))
842 (when (fd-stream-ibuf-sap stream)
843 (push (fd-stream-ibuf-sap stream) *available-buffers*)
844 (setf (fd-stream-ibuf-sap stream) nil))
845
846 (when input-p
847 (multiple-value-bind
848 (routine type size)
849 (pick-input-routine target-type)
850 (unless routine
851 (error "Could not find any input routine for ~S" target-type))
852 (setf (fd-stream-ibuf-sap stream) (next-available-buffer))
853 (setf (fd-stream-ibuf-length stream) bytes-per-buffer)
854 (setf (fd-stream-ibuf-tail stream) 0)
855 (if (subtypep type 'character)
856 (setf (fd-stream-in stream) routine
857 (fd-stream-bin stream) #'ill-bin)
858 (setf (fd-stream-in stream) #'ill-in
859 (fd-stream-bin stream) routine))
860 (when (or (eql size 1)
861 (eql size 2)
862 (eql size 4))
863 ;; Support for n-byte operations on 8-, 16-, and 32-bit streams
864 (setf (fd-stream-n-bin stream) #'fd-stream-read-n-bytes)
865 (when (and buffer-p (eql size 1)
866 (or (eq type 'unsigned-byte)
867 (eq type :default)))
868 ;; We only create this buffer for streams of type
869 ;; (unsigned-byte 8). Because there's no buffer, the
870 ;; other element-types will dispatch to the appropriate
871 ;; input (output) routine in fast-read-byte.
872 (setf (lisp-stream-in-buffer stream)
873 (make-array in-buffer-length
874 :element-type '(unsigned-byte 8)))))
875 (setf input-size size)
876 (setf input-type type)))
877
878 (when output-p
879 (multiple-value-bind
880 (routine type size)
881 (pick-output-routine target-type (fd-stream-buffering stream))
882 (unless routine
883 (error "Could not find any output routine for ~S buffered ~S."
884 (fd-stream-buffering stream)
885 target-type))
886 (setf (fd-stream-obuf-sap stream) (next-available-buffer))
887 (setf (fd-stream-obuf-length stream) bytes-per-buffer)
888 (setf (fd-stream-obuf-tail stream) 0)
889 (if (subtypep type 'character)
890 (setf (fd-stream-out stream) routine
891 (fd-stream-bout stream) #'ill-bout)
892 (setf (fd-stream-out stream)
893 (or (if (eql size 1)
894 (pick-output-routine 'base-char
895 (fd-stream-buffering stream)))
896 #'ill-out)
897 (fd-stream-bout stream) routine))
898 (setf (fd-stream-sout stream)
899 (if (eql size 1) #'fd-sout #'ill-out))
900 (setf (fd-stream-char-pos stream) 0)
901 (setf output-size size)
902 (setf output-type type)))
903
904 (when (and input-size output-size
905 (not (eq input-size output-size)))
906 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
907 input-type input-size
908 output-type output-size))
909 (setf (fd-stream-element-size stream)
910 (or input-size output-size))
911
912 (setf (fd-stream-element-type stream)
913 (cond ((equal input-type output-type)
914 input-type)
915 ((null output-type)
916 input-type)
917 ((null input-type)
918 output-type)
919 ((subtypep input-type output-type)
920 input-type)
921 ((subtypep output-type input-type)
922 output-type)
923 (t
924 (error "Input type (~S) and output type (~S) are unrelated?"
925 input-type
926 output-type))))))
927
928 ;;; FD-STREAM-MISC-ROUTINE -- input
929 ;;;
930 ;;; Handle the various misc operations on fd-stream.
931 ;;;
932 (defun fd-stream-misc-routine (stream operation &optional arg1 arg2)
933 (declare (ignore arg2))
934 (case operation
935 (:listen
936 (or (not (eql (fd-stream-ibuf-head stream)
937 (fd-stream-ibuf-tail stream)))
938 (fd-stream-listen stream)
939 (setf (fd-stream-listen stream)
940 (eql (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
941 (unix:fd-zero read-fds)
942 (unix:fd-set (fd-stream-fd stream) read-fds)
943 (unix:unix-fast-select (1+ (fd-stream-fd stream))
944 (alien:addr read-fds) nil nil
945 0 0))
946 1))))
947 (:unread
948 (setf (fd-stream-unread stream) arg1)
949 (setf (fd-stream-listen stream) t))
950 (:close
951 (cond (arg1
952 ;; We got us an abort on our hands.
953 (when (fd-stream-handler stream)
954 (system:remove-fd-handler (fd-stream-handler stream))
955 (setf (fd-stream-handler stream) nil))
956 (when (and (fd-stream-file stream)
957 (fd-stream-obuf-sap stream))
958 ;; Can't do anything unless we know what file were dealing with,
959 ;; and we don't want to do anything strange unless we were
960 ;; writing to the file.
961 (if (fd-stream-original stream)
962 ;; Have an handle on the original, just revert.
963 (multiple-value-bind
964 (okay err)
965 (unix:unix-rename (fd-stream-original stream)
966 (fd-stream-file stream))
967 (unless okay
968 (cerror "Go on as if nothing bad happened."
969 "Could not restore ~S to it's original contents: ~A"
970 (fd-stream-file stream)
971 (unix:get-unix-error-msg err))))
972 ;; Can't restore the orignal, so nuke that puppy.
973 (multiple-value-bind
974 (okay err)
975 (unix:unix-unlink (fd-stream-file stream))
976 (unless okay
977 (cerror "Go on as if nothing bad happened."
978 "Could not remove ~S: ~A"
979 (fd-stream-file stream)
980 (unix:get-unix-error-msg err)))))))
981 (t
982 (fd-stream-misc-routine stream :finish-output)
983 (when (and (fd-stream-original stream)
984 (fd-stream-delete-original stream))
985 (multiple-value-bind
986 (okay err)
987 (unix:unix-unlink (fd-stream-original stream))
988 (unless okay
989 (cerror "Go on as if nothing bad happened."
990 "Could not delete ~S during close of ~S: ~A"
991 (fd-stream-original stream)
992 stream
993 (unix:get-unix-error-msg err)))))))
994 (when (fboundp 'cancel-finalization)
995 (cancel-finalization stream))
996 (unix:unix-close (fd-stream-fd stream))
997 (when (fd-stream-obuf-sap stream)
998 (push (fd-stream-obuf-sap stream) *available-buffers*)
999 (setf (fd-stream-obuf-sap stream) nil))
1000 (when (fd-stream-ibuf-sap stream)
1001 (push (fd-stream-ibuf-sap stream) *available-buffers*)
1002 (setf (fd-stream-ibuf-sap stream) nil))
1003 (lisp::set-closed-flame stream))
1004 (:clear-input
1005 (setf (fd-stream-unread stream) nil)
1006 (setf (fd-stream-ibuf-head stream) 0)
1007 (setf (fd-stream-ibuf-tail stream) 0)
1008 (catch 'eof-input-catcher
1009 (loop
1010 (multiple-value-bind
1011 (count errno)
1012 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
1013 (unix:fd-zero read-fds)
1014 (unix:fd-set (fd-stream-fd stream) read-fds)
1015 (unix:unix-fast-select (1+ (fd-stream-fd stream))
1016 (alien:addr read-fds) nil nil 0 0))
1017 (cond ((eql count 1)
1018 (do-input stream)
1019 (setf (fd-stream-ibuf-head stream) 0)
1020 (setf (fd-stream-ibuf-tail stream) 0))
1021 ((and (not count) (eql errno unix:eintr)))
1022 (t
1023 (return t)))))))
1024 (:force-output
1025 (flush-output-buffer stream))
1026 (:finish-output
1027 (flush-output-buffer stream)
1028 (do ()
1029 ((null (fd-stream-output-later stream)))
1030 (system:serve-all-events)))
1031 (:element-type
1032 (fd-stream-element-type stream))
1033 (:interactive-p
1034 (unix:unix-isatty (fd-stream-fd stream)))
1035 (:line-length
1036 80)
1037 (:charpos
1038 (fd-stream-char-pos stream))
1039 (:file-length
1040 (unless (fd-stream-file stream)
1041 (error 'simple-type-error
1042 :datum stream
1043 :expected-type 'file-stream
1044 :format-control "~s is not a stream associated with a file."
1045 :format-arguments (list stream)))
1046 (multiple-value-bind
1047 (okay dev ino mode nlink uid gid rdev size
1048 atime mtime ctime blksize blocks)
1049 (unix:unix-fstat (fd-stream-fd stream))
1050 (declare (ignore ino nlink uid gid rdev
1051 atime mtime ctime blksize blocks))
1052 (unless okay
1053 (error 'simple-file-error
1054 :format-control "Error fstating ~S: ~A"
1055 :format-arguments (list stream (unix:get-unix-error-msg dev))))
1056 (if (zerop mode)
1057 nil
1058 (values (truncate size (fd-stream-element-size stream))))))
1059 (:file-position
1060 (fd-stream-file-position stream arg1))))
1061
1062
1063 ;;; FD-STREAM-FILE-POSITION -- internal.
1064 ;;;
1065 (defun fd-stream-file-position (stream &optional newpos)
1066 (declare (type fd-stream stream)
1067 (type (or (integer 0) (member nil :start :end)) newpos))
1068 (if (null newpos)
1069 (system:without-interrupts
1070 ;; First, find the position of the UNIX file descriptor in the file.
1071 (multiple-value-bind
1072 (posn errno)
1073 (unix:unix-lseek (fd-stream-fd stream) 0 unix:l_incr)
1074 (declare (type (or (integer 0) null) posn))
1075 (cond (posn
1076 ;; Adjust for buffered output:
1077 ;; If there is any output buffered, the *real* file position
1078 ;; will be larger than reported by lseek because lseek
1079 ;; obviously cannot take< into account output we have not
1080 ;; sent yet.
1081 (dolist (later (fd-stream-output-later stream))
1082 (incf posn (- (the index (caddr later))
1083 (the index (cadr later)))))
1084 (incf posn (fd-stream-obuf-tail stream))
1085 ;; Adjust for unread input:
1086 ;; If there is any input read from UNIX but not supplied to
1087 ;; the user of the stream, the *real* file position will
1088 ;; smaller than reported, because we want to look like the
1089 ;; unread stuff is still available.
1090 (decf posn (- (fd-stream-ibuf-tail stream)
1091 (fd-stream-ibuf-head stream)))
1092 (when (fd-stream-unread stream)
1093 (decf posn))
1094 ;; Divide bytes by element size.
1095 (truncate posn (fd-stream-element-size stream)))
1096 ((eq errno unix:espipe)
1097 nil)
1098 (t
1099 (system:with-interrupts
1100 (error "Error lseek'ing ~S: ~A"
1101 stream
1102 (unix:get-unix-error-msg errno)))))))
1103 (let ((offset 0)
1104 origin)
1105 (declare (type (integer 0) offset))
1106 ;; Make sure we don't have any output pending, because if we move the
1107 ;; file pointer before writing this stuff, it will be written in the
1108 ;; wrong location.
1109 (flush-output-buffer stream)
1110 (do ()
1111 ((null (fd-stream-output-later stream)))
1112 (system:serve-all-events))
1113 ;; Clear out any pending input to force the next read to go to the
1114 ;; disk.
1115 (setf (fd-stream-unread stream) nil)
1116 (setf (fd-stream-ibuf-head stream) 0)
1117 (setf (fd-stream-ibuf-tail stream) 0)
1118 ;; Trash cached value for listen, so that we check next time.
1119 (setf (fd-stream-listen stream) nil)
1120 ;; Now move it.
1121 (cond ((eq newpos :start)
1122 (setf offset 0
1123 origin unix:l_set))
1124 ((eq newpos :end)
1125 (setf offset 0
1126 origin unix:l_xtnd))
1127 ((typep newpos '(integer 0))
1128 (setf offset (* newpos (fd-stream-element-size stream))
1129 origin unix:l_set))
1130 (t
1131 (error "Invalid position given to file-position: ~S" newpos)))
1132 (multiple-value-bind
1133 (posn errno)
1134 (unix:unix-lseek (fd-stream-fd stream) offset origin)
1135 (cond (posn
1136 t)
1137 ((eq errno unix:espipe)
1138 nil)
1139 (t
1140 (error "Error lseek'ing ~S: ~A"
1141 stream
1142 (unix:get-unix-error-msg errno))))))))
1143
1144
1145
1146 ;;;; Creation routines (MAKE-FD-STREAM and OPEN)
1147
1148 ;;; MAKE-FD-STREAM -- Public.
1149 ;;;
1150 ;;; Returns a FD-STREAM on the given file.
1151 ;;;
1152 (defun make-fd-stream (fd
1153 &key
1154 (input nil input-p)
1155 (output nil output-p)
1156 (element-type 'base-char)
1157 (buffering :full)
1158 timeout
1159 file
1160 original
1161 delete-original
1162 pathname
1163 input-buffer-p
1164 (name (if file
1165 (format nil "file ~S" file)
1166 (format nil "descriptor ~D" fd)))
1167 auto-close)
1168 (declare (type index fd) (type (or index null) timeout)
1169 (type (member :none :line :full) buffering))
1170 "Create a stream for the given unix file descriptor.
1171 If input is non-nil, allow input operations.
1172 If output is non-nil, allow output operations.
1173 If neither input nor output are specified, default to allowing input.
1174 Element-type indicates the element type to use (as for open).
1175 Buffering indicates the kind of buffering to use.
1176 Timeout (if true) is the number of seconds to wait for input. If NIL (the
1177 default), then wait forever. When we time out, we signal IO-TIMEOUT.
1178 File is the name of the file (will be returned by PATHNAME).
1179 Name is used to identify the stream when printed."
1180 (cond ((not (or input-p output-p))
1181 (setf input t))
1182 ((not (or input output))
1183 (error "File descriptor must be opened either for input or output.")))
1184 (let ((stream (%make-fd-stream :fd fd
1185 :name name
1186 :file file
1187 :original original
1188 :delete-original delete-original
1189 :pathname pathname
1190 :buffering buffering
1191 :timeout timeout)))
1192 (set-routines stream element-type input output input-buffer-p)
1193 (when (and auto-close (fboundp 'finalize))
1194 (finalize stream
1195 #'(lambda ()
1196 (unix:unix-close fd)
1197 (format *terminal-io* "** Closed file descriptor ~D~%"
1198 fd))))
1199 stream))
1200
1201
1202 ;;; PICK-PACKUP-NAME -- internal
1203 ;;;
1204 ;;; Pick a name to use for the backup file.
1205 ;;;
1206 (defvar *backup-extension* ".BAK"
1207 "This is a string that OPEN tacks on the end of a file namestring to produce
1208 a name for the :if-exists :rename-and-delete and :rename options. Also,
1209 this can be a function that takes a namestring and returns a complete
1210 namestring.")
1211 ;;;
1212 (defun pick-backup-name (name)
1213 (declare (type simple-string name))
1214 (let ((ext *backup-extension*))
1215 (etypecase ext
1216 (simple-string (concatenate 'simple-string name ext))
1217 (function (funcall ext name)))))
1218
1219 ;;; ASSURE-ONE-OF -- internal
1220 ;;;
1221 ;;; Assure that the given arg is one of the given list of valid things.
1222 ;;; Allow the user to fix any problems.
1223 ;;;
1224 (defun assure-one-of (item list what)
1225 (unless (member item list)
1226 (loop
1227 (cerror "Enter new value for ~*~S"
1228 "~S is invalid for ~S. Must be one of~{ ~S~}"
1229 item
1230 what
1231 list)
1232 (format (the stream *query-io*) "Enter new value for ~S: " what)
1233 (force-output *query-io*)
1234 (setf item (read *query-io*))
1235 (when (member item list)
1236 (return))))
1237 item)
1238
1239 ;;; DO-OLD-RENAME -- Internal
1240 ;;;
1241 ;;; Rename Namestring to Original. First, check if we have write access,
1242 ;;; since we don't want to trash unwritable files even if we technically can.
1243 ;;; We return true if we suceed in renaming.
1244 ;;;
1245 (defun do-old-rename (namestring original)
1246 (unless (unix:unix-access namestring unix:w_ok)
1247 (cerror "Try to rename it anyway." "File ~S is not writable." namestring))
1248 (multiple-value-bind
1249 (okay err)
1250 (unix:unix-rename namestring original)
1251 (cond (okay t)
1252 (t
1253 (cerror "Use :SUPERSEDE instead."
1254 "Could not rename ~S to ~S: ~A."
1255 namestring
1256 original
1257 (unix:get-unix-error-msg err))
1258 nil))))
1259
1260 ;;; RETURN-STREAM -- internal
1261 ;;;
1262 ;;; (this is just to save having to reindent the code in OPEN...move it there)
1263 ;;;
1264 (defmacro return-stream (class &body body)
1265 (let ((stream (gensym)))
1266 `(let ((,stream (progn ,@body)))
1267 (return (if ,class
1268 (make-instance ,class :lisp-stream ,stream)
1269 ,stream)))))
1270
1271 ;;; OPEN -- public
1272 ;;;
1273 ;;; Open the given file.
1274 ;;;
1275 (defun open (filename
1276 &key
1277 (direction :input)
1278 (element-type 'base-char)
1279 (if-exists nil if-exists-given)
1280 (if-does-not-exist nil if-does-not-exist-given)
1281 (external-format :default)
1282 class
1283 &aux ; Squelch assignment warning.
1284 (direction direction)
1285 (if-does-not-exist if-does-not-exist)
1286 (if-exists if-exists))
1287 "Return a stream which reads from or writes to Filename.
1288 Defined keywords:
1289 :direction - one of :input, :output, :io, or :probe
1290 :element-type - Type of object to read or write, default BASE-CHAR
1291 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
1292 :overwrite, :append, :supersede or nil
1293 :if-does-not-exist - one of :error, :create or nil
1294 See the manual for details."
1295 (declare (ignore external-format))
1296
1297 ;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
1298 (setf direction
1299 (assure-one-of direction
1300 '(:input :output :io :probe)
1301 :direction))
1302
1303 ;; Calculate useful stuff.
1304 (multiple-value-bind
1305 (input output mask)
1306 (case direction
1307 (:input (values t nil unix:o_rdonly))
1308 (:output (values nil t unix:o_wronly))
1309 (:io (values t t unix:o_rdwr))
1310 (:probe (values t nil unix:o_rdonly)))
1311 (declare (type index mask))
1312 (let* ((pathname (pathname filename))
1313 (namestring
1314 (cond ((unix-namestring pathname input))
1315 ((and input (eq if-does-not-exist :create))
1316 (unix-namestring pathname nil)))))
1317 ;; Process if-exists argument if we are doing any output.
1318 (cond (output
1319 (unless if-exists-given
1320 (setf if-exists
1321 (if (eq (pathname-version pathname) :newest)
1322 :new-version
1323 :error)))
1324 (setf if-exists
1325 (assure-one-of if-exists
1326 '(:error :new-version :rename
1327 :rename-and-delete :overwrite
1328 :append :supersede nil)
1329 :if-exists))
1330 (case if-exists
1331 ((:error nil)
1332 (setf mask (logior mask unix:o_excl)))
1333 ((:rename :rename-and-delete)
1334 (setf mask (logior mask unix:o_creat)))
1335 ((:new-version :supersede)
1336 (setf mask (logior mask unix:o_trunc)))
1337 (:append
1338 (setf mask (logior mask unix:o_append)))))
1339 (t
1340 (setf if-exists :ignore-this-arg)))
1341
1342 (unless if-does-not-exist-given
1343 (setf if-does-not-exist
1344 (cond ((eq direction :input) :error)
1345 ((and output
1346 (member if-exists '(:overwrite :append)))
1347 :error)
1348 ((eq direction :probe)
1349 nil)
1350 (t
1351 :create))))
1352 (setf if-does-not-exist
1353 (assure-one-of if-does-not-exist
1354 '(:error :create nil)
1355 :if-does-not-exist))
1356 (if (eq if-does-not-exist :create)
1357 (setf mask (logior mask unix:o_creat)))
1358
1359 (let ((original (if (member if-exists
1360 '(:rename :rename-and-delete))
1361 (pick-backup-name namestring)))
1362 (delete-original (eq if-exists :rename-and-delete))
1363 (mode #o666))
1364 (when original
1365 ;; We are doing a :rename or :rename-and-delete.
1366 ;; Determine if the file already exists, make sure the original
1367 ;; file is not a directory and keep the mode
1368 (let ((exists
1369 (and namestring
1370 (multiple-value-bind
1371 (okay err/dev inode orig-mode)
1372 (unix:unix-stat namestring)
1373 (declare (ignore inode)
1374 (type (or index null) orig-mode))
1375 (cond
1376 (okay
1377 (when (and output (= (logand orig-mode #o170000)
1378 #o40000))
1379 (error 'simple-file-error
1380 :format-control "Cannot open ~S for output: Is a directory."
1381 :format-arguments (list namestring)))
1382 (setf mode (logand orig-mode #o777))
1383 t)
1384 ((eql err/dev unix:enoent)
1385 nil)
1386 (t
1387 (error 'simple-file-error
1388 :format-control "Cannot find ~S: ~A"
1389 :format-arguments
1390 (list namestring (unix:get-unix-error-msg err/dev)))))))))
1391 (unless (and exists
1392 (do-old-rename namestring original))
1393 (setf original nil)
1394 (setf delete-original nil)
1395 ;; In order to use SUPERSEDE instead, we have
1396 ;; to make sure unix:o_creat corresponds to
1397 ;; if-does-not-exist. unix:o_creat was set
1398 ;; before because of if-exists being :rename.
1399 (unless (eq if-does-not-exist :create)
1400 (setf mask (logior (logandc2 mask unix:o_creat) unix:o_trunc)))
1401 (setf if-exists :supersede))))
1402
1403 ;; Okay, now we can try the actual open.
1404 (loop
1405 (multiple-value-bind
1406 (fd errno)
1407 (if namestring
1408 (unix:unix-open namestring mask mode)
1409 (values nil unix:enoent))
1410 (cond ((numberp fd)
1411 (return-stream class
1412 (case direction
1413 ((:input :output :io)
1414 (make-fd-stream fd
1415 :input input
1416 :output output
1417 :element-type element-type
1418 :file namestring
1419 :original original
1420 :delete-original delete-original
1421 :pathname pathname
1422 :input-buffer-p t
1423 :auto-close t))
1424 (:probe
1425 (let ((stream
1426 (%make-fd-stream :name namestring :fd fd
1427 :pathname pathname
1428 :element-type element-type)))
1429 (close stream)
1430 stream)))))
1431 ((eql errno unix:enoent)
1432 (case if-does-not-exist
1433 (:error
1434 (cerror "Return NIL."
1435 'simple-file-error
1436 :pathname pathname
1437 :format-control "Error opening ~S, ~A."
1438 :format-arguments
1439 (list pathname (unix:get-unix-error-msg errno))))
1440 (:create
1441 (cerror "Return NIL."
1442 "Error creating ~S, path does not exist."
1443 pathname)))
1444 (return nil))
1445 ((eql errno unix:eexist)
1446 (unless (eq nil if-exists)
1447 (cerror "Return NIL."
1448 'simple-file-error
1449 :pathname pathname
1450 :format-control "Error opening ~S, ~A."
1451 :format-arguments
1452 (list pathname (unix:get-unix-error-msg errno))))
1453 (return nil))
1454 ((eql errno unix:eacces)
1455 (cerror "Try again."
1456 "Error opening ~S, ~A."
1457 pathname
1458 (unix:get-unix-error-msg errno)))
1459 (t
1460 (cerror "Return NIL."
1461 "Error opening ~S, ~A."
1462 pathname
1463 (unix:get-unix-error-msg errno))
1464 (return nil)))))))))
1465
1466 ;;;; Initialization.
1467
1468 (defvar *tty* nil
1469 "The stream connected to the controlling terminal or NIL if there is none.")
1470 (defvar *stdin* nil
1471 "The stream connected to the standard input (file descriptor 0).")
1472 (defvar *stdout* nil
1473 "The stream connected to the standard output (file descriptor 1).")
1474 (defvar *stderr* nil
1475 "The stream connected to the standard error output (file descriptor 2).")
1476
1477 ;;; STREAM-INIT -- internal interface
1478 ;;;
1479 ;;; Called when the cold load is first started up.
1480 ;;;
1481 (defun stream-init ()
1482 (stream-reinit)
1483 (setf *terminal-io* (make-synonym-stream '*tty*))
1484 (setf *standard-output* (make-synonym-stream '*stdout*))
1485 (setf *standard-input*
1486 (make-two-way-stream (make-synonym-stream '*stdin*)
1487 *standard-output*))
1488 (setf *error-output* (make-synonym-stream '*stderr*))
1489 (setf *query-io* (make-synonym-stream '*terminal-io*))
1490 (setf *debug-io* *query-io*)
1491 (setf *trace-output* *standard-output*)
1492 nil)
1493
1494 ;;; STREAM-REINIT -- internal interface
1495 ;;;
1496 ;;; Called whenever a saved core is restarted.
1497 ;;;
1498 (defun stream-reinit ()
1499 (setf *available-buffers* nil)
1500 (setf *stdin*
1501 (make-fd-stream 0 :name "Standard Input" :input t :buffering :line))
1502 (setf *stdout*
1503 (make-fd-stream 1 :name "Standard Output" :output t :buffering :line))
1504 (setf *stderr*
1505 (make-fd-stream 2 :name "Standard Error" :output t :buffering :line))
1506 (let ((tty (and (not *batch-mode*)
1507 (unix:unix-open "/dev/tty" unix:o_rdwr #o666))))
1508 (setf *tty*
1509 (if tty
1510 (make-fd-stream tty :name "the Terminal" :input t :output t
1511 :buffering :line :auto-close t)
1512 (make-two-way-stream *stdin* *stdout*))))
1513 nil)
1514
1515
1516 ;;;; Beeping.
1517
1518 (defun default-beep-function (stream)
1519 (write-char #\bell stream)
1520 (finish-output stream))
1521
1522 (defvar *beep-function* #'default-beep-function
1523 "This is called in BEEP to feep the user. It takes a stream.")
1524
1525 (defun beep (&optional (stream *terminal-io*))
1526 (funcall *beep-function* stream))
1527
1528
1529 ;;; File-Name -- internal interface
1530 ;;;
1531 ;;; Kind of like File-Position, but is an internal hack used by the filesys
1532 ;;; stuff to get and set the file name.
1533 ;;;
1534 (defun file-name (stream &optional new-name)
1535 (when (typep stream 'fd-stream)
1536 (cond (new-name
1537 (setf (fd-stream-pathname stream) new-name)
1538 (setf (fd-stream-file stream)
1539 (unix-namestring new-name nil))
1540 t)
1541 (t
1542 (fd-stream-pathname stream)))))
1543
1544
1545 ;;;; Degenerate international character support:
1546
1547 (defun file-string-length (stream object)
1548 (declare (type (or string character) object) (type file-stream stream))
1549 "Return the delta in Stream's FILE-POSITION that would be caused by writing
1550 Object to Stream. Non-trivial only in implementations that support
1551 international character sets."
1552 (declare (ignore stream))
1553 (etypecase object
1554 (character 1)
1555 (string (length object))))
1556
1557 (defun stream-external-format (stream)
1558 (declare (type file-stream stream) (ignore stream))
1559 "Returns :DEFAULT."
1560 :default)

  ViewVC Help
Powered by ViewVC 1.1.5