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

  ViewVC Help
Powered by ViewVC 1.1.5