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

  ViewVC Help
Powered by ViewVC 1.1.5