/[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.23.1.3 - (hide annotations) (vendor branch)
Tue Feb 23 23:25:12 1993 UTC (21 years, 1 month ago) by ram
Branch: new_struct
Changes since 1.23.1.2: +24 -22 lines
Added INPUT-BUFFER-P arg to MAKE-FD-STREAM which controls whether we 
use the IN-BUFFER or not, because the point is to work on real files,
and it requires extra syscalls to make sure that we don't spuriously
block on TTYs.
Removed vestigial support for non-8 byte sizes in n-bin.
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.23.1.3 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/fd-stream.lisp,v 1.23.1.3 1993/02/23 23:25:12 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.22 (export '(file-stream))
37     (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     (original nil) ; The original file (for :if-exists :rename)
72     (delete-original nil) ; for :if-exists :rename-and-delete
73 ram 1.14 ;;
74     ;;; Number of bytes per element.
75     (element-size 1 :type index)
76 wlott 1.16 (element-type 'base-char) ; The type of element being transfered.
77 ram 1.1 (fd -1 :type fixnum) ; The file descriptor
78 ram 1.14 ;;
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 ram 1.1 ;; The input buffer.
89     (unread nil)
90 ram 1.14 (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 ram 1.1
95     ;; The output buffer.
96 ram 1.14 (obuf-sap nil :type (or system-area-pointer null))
97     (obuf-length nil :type (or index null))
98     (obuf-tail 0 :type index)
99 ram 1.1
100     ;; Output flushed, but not written due to non-blocking io.
101     (output-later nil)
102 ram 1.15 (handler nil)
103     ;;
104     ;; Timeout specified for this stream, or NIL if none.
105     (timeout nil :type (or index null)))
106 ram 1.1
107     (defun %print-fd-stream (fd-stream stream depth)
108 ram 1.14 (declare (ignore depth) (stream stream))
109 ram 1.1 (format stream "#<Stream for ~A>"
110     (fd-stream-name fd-stream)))
111    
112    
113 ram 1.15 (define-condition io-timeout (stream-error) (direction)
114     (:report
115     (lambda (condition stream)
116 ram 1.23 (declare (stream stream))
117 ram 1.15 (format stream "Timeout ~(~A~)ing ~S."
118     (io-timeout-direction condition)
119     (stream-error-stream condition)))))
120    
121 ram 1.1
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 wlott 1.5 (reuse-sap (cadddr stuff))
141 ram 1.1 (length (- end start)))
142 ram 1.14 (declare (type index start end length))
143 ram 1.1 (multiple-value-bind
144     (count errno)
145 wlott 1.19 (unix:unix-write (fd-stream-fd stream)
146 ram 1.1 base
147     start
148     length)
149 ram 1.14 (cond ((not count)
150 wlott 1.19 (if (= errno unix:ewouldblock)
151 ram 1.14 (error "Write would have blocked, but SERVER told us to go.")
152     (error "While writing ~S: ~A"
153 wlott 1.19 stream (unix:get-unix-error-msg errno))))
154 ram 1.14 ((eql count length) ; Hot damn, it workded.
155 wlott 1.5 (when reuse-sap
156     (push base *available-buffers*)))
157 ram 1.1 ((not (null count)) ; Sorta worked.
158     (push (list base
159 ram 1.23 (the index (+ start count))
160 wlott 1.5 end)
161 ram 1.14 (fd-stream-output-later stream))))))
162 ram 1.1 (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 wlott 1.5 (defun output-later (stream base start end reuse-sap)
171 ram 1.1 (cond ((null (fd-stream-output-later stream))
172     (setf (fd-stream-output-later stream)
173 wlott 1.5 (list (list base start end reuse-sap)))
174 ram 1.1 (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 wlott 1.5 (list (list base start end reuse-sap)))))
183     (when reuse-sap
184 ram 1.1 (let ((new-buffer (next-available-buffer)))
185 wlott 1.5 (setf (fd-stream-obuf-sap stream) new-buffer)
186 ram 1.14 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
187 ram 1.1
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 wlott 1.5 (defun do-output (stream base start end reuse-sap)
195 ram 1.14 (declare (type fd-stream stream)
196     (type (or system-area-pointer (simple-array * (*))) base)
197     (type index start end))
198 ram 1.1 (if (not (null (fd-stream-output-later stream))) ; something buffered.
199 ram 1.14 (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 wlott 1.19 (unix:unix-write (fd-stream-fd stream) base start length)
207 ram 1.14 (cond ((not count)
208 wlott 1.19 (if (= errno unix:ewouldblock)
209 ram 1.14 (output-later stream base start end reuse-sap)
210     (error "While writing ~S: ~A"
211 wlott 1.19 stream (unix:get-unix-error-msg errno))))
212 ram 1.14 ((not (eql count length))
213 ram 1.23 (output-later stream base (the index (+ start count))
214     end reuse-sap)))))))
215 ram 1.1
216 ram 1.14
217 ram 1.1 ;;; 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 wlott 1.5 (do-output stream (fd-stream-obuf-sap stream) 0 length t)
225 ram 1.1 (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 ram 1.23 (declare (optimize (speed 1)))
234 ram 1.1 (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 wlott 1.5 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
270     1
271 wlott 1.6 (:none character)
272     (:line character)
273     (:full character))
274 wlott 1.5 (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 ram 1.1 (def-output-routines ("OUTPUT-BYTE-~A-BUFFERED"
282     1
283 wlott 1.5 (:none (signed-byte 8) (unsigned-byte 8))
284     (:full (signed-byte 8) (unsigned-byte 8)))
285 ram 1.1 (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 wlott 1.5 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
291     byte))
292 ram 1.1
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 wlott 1.20 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
298 wlott 1.5 byte))
299    
300     (def-output-routines ("OUTPUT-LONG-~A-BUFFERED"
301 ram 1.1 4
302 wlott 1.5 (:none (signed-byte 32) (unsigned-byte 32))
303     (:full (signed-byte 32) (unsigned-byte 32)))
304 wlott 1.20 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
305 wlott 1.5 byte))
306 ram 1.1
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 ram 1.14 (end (or end (length (the (simple-array * (*)) thing)))))
320     (declare (type index start end))
321 ram 1.1 (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 wlott 1.5 (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 ram 1.1 (setf (fd-stream-obuf-tail stream) newtail))
345     ((<= bytes len)
346     (flush-output-buffer stream)
347 wlott 1.5 (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 ram 1.1 (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 ram 1.14 (end (or end (length (the vector thing)))))
378 ram 1.1 (declare (fixnum start end))
379     (if (stringp thing)
380 wlott 1.5 (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 ram 1.1 (ecase (fd-stream-buffering stream)
401 wlott 1.5 ((:line :full)
402 ram 1.1 (output-raw-bytes stream thing start end))
403     (:none
404 wlott 1.5 (do-output stream thing start end nil))))))
405 ram 1.1
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 ram 1.14 (declare (type index head tail))
440 ram 1.1 (unless (zerop head)
441 ram 1.14 (cond ((eql head tail)
442 ram 1.1 (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 wlott 1.5 (system-area-copy ibuf-sap (* head vm:byte-bits)
449     ibuf-sap 0 (* tail vm:byte-bits))
450 ram 1.1 (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 ram 1.23 (unix:unix-select (1+ fd) (the (unsigned-byte 32) (ash 1 fd)) 0 0 0)
457 ram 1.1 (case count
458     (1)
459     (0
460 ram 1.15 (unless (system:wait-until-fd-usable
461     fd :input (fd-stream-timeout stream))
462     (error 'io-timeout :stream stream :direction :read)))
463 ram 1.1 (t
464     (error "Problem checking to see if ~S is readable: ~A"
465     stream
466 wlott 1.19 (unix:get-unix-error-msg errno)))))
467 ram 1.1 (multiple-value-bind
468     (count errno)
469 wlott 1.19 (unix:unix-read fd
470 ram 1.1 (system:int-sap (+ (system:sap-int ibuf-sap) tail))
471     (- buflen tail))
472     (cond ((null count)
473 wlott 1.21 (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 ram 1.1 ((zerop count)
483 ram 1.12 (setf (fd-stream-listen stream) :eof)
484 ram 1.1 (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 ram 1.23.1.3 ;;; Macro to wrap around all input routines to handle eof-error noise.
508 ram 1.1 ;;;
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 wlott 1.5 (setf (fd-stream-unread ,stream) nil)
517     (setf (fd-stream-listen ,stream) nil))
518 ram 1.1 (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 wlott 1.6 ;;; INPUT-CHARACTER -- internal
548 ram 1.1 ;;;
549     ;;; Routine to use in stream-in slot for reading string chars.
550     ;;;
551 wlott 1.6 (def-input-routine input-character
552     (character 1 sap head)
553 wlott 1.5 (code-char (sap-ref-8 sap head)))
554 ram 1.1
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 wlott 1.5 (sap-ref-8 sap head))
562 ram 1.1
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 wlott 1.5 (signed-sap-ref-8 sap head))
570 ram 1.1
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 wlott 1.20 (sap-ref-16 sap head))
578 ram 1.1
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 wlott 1.20 (signed-sap-ref-16 sap head))
586 ram 1.1
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 wlott 1.20 (sap-ref-32 sap head))
594 ram 1.1
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 wlott 1.20 (signed-sap-ref-32 sap head))
602 ram 1.1
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 ram 1.14 (declare (type index start end))
621 ram 1.1 (let* ((length (- end start))
622     (string (make-string length)))
623 wlott 1.5 (copy-from-system-area sap (* start vm:byte-bits)
624     string (* vm:vector-data-offset vm:word-bits)
625     (* length vm:byte-bits))
626 ram 1.1 string))
627    
628 ram 1.13 #|
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 ram 1.1
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 wlott 1.5 (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 ram 1.1 (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 ram 1.13 |#
676 ram 1.1
677 ram 1.13
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 ram 1.14 ;;; to be done, so blocking isn't too problematical.
684 ram 1.13 ;;;
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 ram 1.23.1.1 ;;; must loop when eof-errorp is T because some streams (like pipes) may return
694     ;;; a partial amount without hitting EOF.
695     ;;;
696 ram 1.13 (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 ram 1.23.1.3 (offset start)
700 ram 1.13 (head (fd-stream-ibuf-head stream))
701     (tail (fd-stream-ibuf-tail stream))
702     (available (- tail head))
703 ram 1.23.1.3 (copy (min requested available)))
704     (declare (type index offset head tail available copy))
705 ram 1.13 (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 ram 1.23.1.3 ((or (= copy requested)
718     (and (not eof-error-p) (/= copy 0)))
719     copy)
720     (t
721 ram 1.13 (setf (fd-stream-ibuf-head stream) 0)
722     (setf (fd-stream-ibuf-tail stream) 0)
723     (setf (fd-stream-listen stream) nil)
724 ram 1.23.1.3 (let ((now-needed (- requested copy))
725 ram 1.13 (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 wlott 1.19 (unix:unix-read (fd-stream-fd stream)
734 ram 1.13 (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 wlott 1.19 (unix:get-unix-error-msg err)))
743 ram 1.23.1.1 (when (< count now-needed)
744 ram 1.13 (if eof-error-p
745 ram 1.23.1.1 (when (zerop count)
746     (error "Unexpected eof on ~S." stream))
747 ram 1.23.1.3 (return (- requested now-needed))))
748 ram 1.13 (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 wlott 1.19 (unix:unix-read (fd-stream-fd stream) sap len)
756 ram 1.13 (declare (type (or index null) count))
757     (unless count
758     (error "Error reading ~S: ~A" stream
759 wlott 1.19 (unix:get-unix-error-msg err)))
760 ram 1.13 (incf (fd-stream-ibuf-tail stream) count)
761 ram 1.23.1.1 (when (< count now-needed)
762 ram 1.13 (if eof-error-p
763 ram 1.23.1.1 (when (zerop count)
764     (error "Unexpected eof on ~S." stream))
765 ram 1.23.1.3 (return (- requested now-needed))))
766 ram 1.13 (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 ram 1.23.1.3 (incf offset copy)))))))))))
784 ram 1.13
785 ram 1.1
786     ;;;; Utility functions (misc routines, etc)
787    
788     ;;; SET-ROUTINES -- internal
789     ;;;
790 ram 1.23.1.1 ;;; 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 ram 1.1 ;;;
794 ram 1.23.1.3 (defun set-routines (stream type input-p output-p buffer-p)
795 ram 1.1 (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 wlott 1.5 (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 ram 1.1
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 wlott 1.5 (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 ram 1.1 (if (subtypep type 'character)
824 ram 1.14 (setf (fd-stream-in stream) routine
825 ram 1.23.1.1 (fd-stream-bin stream) #'ill-bin)
826 ram 1.14 (setf (fd-stream-in stream) #'ill-in
827 ram 1.23.1.1 (fd-stream-bin stream) routine))
828     (when (eql size 1)
829     (setf (fd-stream-n-bin stream) #'fd-stream-read-n-bytes)
830 ram 1.23.1.3 (when buffer-p
831     (setf (stream-in-buffer stream)
832     (make-array in-buffer-length
833     :element-type '(unsigned-byte 8)))))
834 ram 1.1 (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 wlott 1.5 (setf (fd-stream-obuf-sap stream) (next-available-buffer))
846     (setf (fd-stream-obuf-length stream) bytes-per-buffer)
847 ram 1.1 (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 wlott 1.16 (pick-output-routine 'base-char
854 ram 1.1 (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 wlott 1.5 ((or (null output-type) (subtypep input-type output-type))
875 ram 1.1 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 ram 1.23.1.3 (declare (ignore arg2))
889 ram 1.1 (case operation
890 ram 1.23.1.3 (:listen
891 ram 1.1 (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 wlott 1.19 (eql (unix:unix-select (1+ (fd-stream-fd stream))
896 ram 1.23 (the (unsigned-byte 32)
897     (ash 1 (fd-stream-fd stream)))
898 ram 1.13 0
899     0
900     0)
901     1))))
902 ram 1.1 (:unread
903 wlott 1.5 (setf (fd-stream-unread stream) arg1)
904     (setf (fd-stream-listen stream) t))
905 ram 1.1 (:close
906     (cond (arg1
907     ;; We got us an abort on our hands.
908     (when (and (fd-stream-file stream)
909 wlott 1.5 (fd-stream-obuf-sap stream))
910 ram 1.1 ;; 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 wlott 1.19 (unix:unix-rename (fd-stream-original stream)
918 ram 1.1 (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 wlott 1.19 (unix:get-unix-error-msg err))))
924 ram 1.1 ;; Can't restore the orignal, so nuke that puppy.
925     (multiple-value-bind
926     (okay err)
927 wlott 1.19 (unix:unix-unlink (fd-stream-file stream))
928 ram 1.1 (unless okay
929     (cerror "Go on as if nothing bad happened."
930     "Could not remove ~S: ~A"
931     (fd-stream-file stream)
932 wlott 1.19 (unix:get-unix-error-msg err)))))))
933 ram 1.1 (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 wlott 1.19 (unix:unix-unlink (fd-stream-original stream))
940 ram 1.1 (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 wlott 1.19 (unix:get-unix-error-msg err)))))))
946 wlott 1.17 (when (fboundp 'cancel-finalization)
947     (cancel-finalization stream))
948 wlott 1.19 (unix:unix-close (fd-stream-fd stream))
949 wlott 1.5 (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 ram 1.1 (lisp::set-closed-flame stream))
956 wlott 1.6 (:clear-input
957     (setf (fd-stream-ibuf-head stream) 0)
958     (setf (fd-stream-ibuf-tail stream) 0)
959     (loop
960 wlott 1.19 (let ((count (unix:unix-select (1+ (fd-stream-fd stream))
961 ram 1.23 (the (unsigned-byte 32)
962     (ash 1 (fd-stream-fd stream)))
963 ram 1.13 0 0 0)))
964 wlott 1.6 (cond ((eql count 1)
965     (do-input stream)
966     (setf (fd-stream-ibuf-head stream) 0)
967     (setf (fd-stream-ibuf-tail stream) 0))
968     (t
969     (return))))))
970 ram 1.1 (:force-output
971     (flush-output-buffer stream))
972     (:finish-output
973     (flush-output-buffer stream)
974     (do ()
975     ((null (fd-stream-output-later stream)))
976     (system:serve-all-events)))
977     (:element-type
978     (fd-stream-element-type stream))
979 ram 1.22 (:interactive-p
980     (unix:unix-isatty (fd-stream-fd stream)))
981 ram 1.1 (:line-length
982     80)
983     (:charpos
984     (fd-stream-char-pos stream))
985     (:file-length
986     (multiple-value-bind
987     (okay dev ino mode nlink uid gid rdev size
988     atime mtime ctime blksize blocks)
989 wlott 1.19 (unix:unix-fstat (fd-stream-fd stream))
990 ram 1.1 (declare (ignore ino nlink uid gid rdev
991     atime mtime ctime blksize blocks))
992     (unless okay
993     (error "Error fstating ~S: ~A"
994     stream
995 wlott 1.19 (unix:get-unix-error-msg dev)))
996 ram 1.23 (if (zerop (the index mode))
997     nil
998     (truncate (the index size) (fd-stream-element-size stream)))))
999 ram 1.1 (:file-position
1000     (fd-stream-file-position stream arg1))
1001     (:file-name
1002     (fd-stream-file stream))))
1003    
1004     ;;; FD-STREAM-FILE-POSITION -- internal.
1005     ;;;
1006     (defun fd-stream-file-position (stream &optional newpos)
1007 ram 1.14 (declare (type fd-stream stream)
1008     (type (or index (member nil :start :end)) newpos))
1009 ram 1.1 (if (null newpos)
1010     (system:without-interrupts
1011     ;; First, find the position of the UNIX file descriptor in the
1012     ;; file.
1013     (multiple-value-bind
1014     (posn errno)
1015 wlott 1.19 (unix:unix-lseek (fd-stream-fd stream) 0 unix:l_incr)
1016 ram 1.14 (declare (type (or index null) posn))
1017     (cond ((fixnump posn)
1018 ram 1.1 ;; Adjust for buffered output:
1019     ;; If there is any output buffered, the *real* file position
1020     ;; will be larger than reported by lseek because lseek
1021     ;; obviously cannot take into account output we have not
1022     ;; sent yet.
1023     (dolist (later (fd-stream-output-later stream))
1024 ram 1.14 (incf posn (- (the index (caddr later))
1025     (the index (cadr later)))))
1026 ram 1.1 (incf posn (fd-stream-obuf-tail stream))
1027     ;; Adjust for unread input:
1028     ;; If there is any input read from UNIX but not supplied to
1029     ;; the user of the stream, the *real* file position will
1030     ;; smaller than reported, because we want to look like the
1031     ;; unread stuff is still available.
1032     (decf posn (- (fd-stream-ibuf-tail stream)
1033     (fd-stream-ibuf-head stream)))
1034     (when (fd-stream-unread stream)
1035     (decf posn))
1036     ;; Divide bytes by element size.
1037 ram 1.14 (truncate posn (fd-stream-element-size stream)))
1038 wlott 1.19 ((eq errno unix:espipe)
1039 ram 1.1 nil)
1040     (t
1041     (system:with-interrupts
1042     (error "Error lseek'ing ~S: ~A"
1043     stream
1044 wlott 1.19 (unix:get-unix-error-msg errno)))))))
1045 ram 1.23 (let ((offset 0) origin)
1046     (declare (type index offset))
1047 ram 1.1 ;; Make sure we don't have any output pending, because if we move the
1048     ;; file pointer before writing this stuff, it will be written in the
1049     ;; wrong location.
1050     (flush-output-buffer stream)
1051     (do ()
1052     ((null (fd-stream-output-later stream)))
1053     (system:serve-all-events))
1054 ram 1.4 ;; Clear out any pending input to force the next read to go to the
1055     ;; disk.
1056     (setf (fd-stream-unread stream) nil)
1057     (setf (fd-stream-ibuf-head stream) 0)
1058     (setf (fd-stream-ibuf-tail stream) 0)
1059 wlott 1.5 ;; Trash cashed value for listen, so that we check next time.
1060     (setf (fd-stream-listen stream) nil)
1061 ram 1.4 ;; Now move it.
1062 ram 1.1 (cond ((eq newpos :start)
1063 wlott 1.19 (setf offset 0 origin unix:l_set))
1064 ram 1.1 ((eq newpos :end)
1065 wlott 1.19 (setf offset 0 origin unix:l_xtnd))
1066 ram 1.14 ((typep newpos 'index)
1067 ram 1.1 (setf offset (* newpos (fd-stream-element-size stream))
1068 wlott 1.19 origin unix:l_set))
1069 ram 1.1 (t
1070     (error "Invalid position given to file-position: ~S" newpos)))
1071     (multiple-value-bind
1072     (posn errno)
1073 wlott 1.19 (unix:unix-lseek (fd-stream-fd stream) offset origin)
1074 ram 1.14 (cond ((typep posn 'fixnum)
1075 ram 1.1 t)
1076 wlott 1.19 ((eq errno unix:espipe)
1077 ram 1.1 nil)
1078     (t
1079     (error "Error lseek'ing ~S: ~A"
1080     stream
1081 wlott 1.19 (unix:get-unix-error-msg errno))))))))
1082 ram 1.1
1083    
1084    
1085     ;;;; Creation routines (MAKE-FD-STREAM and OPEN)
1086    
1087     ;;; MAKE-FD-STREAM -- Public.
1088     ;;;
1089     ;;; Returns a FD-STREAM on the given file.
1090     ;;;
1091     (defun make-fd-stream (fd
1092     &key
1093     (input nil input-p)
1094     (output nil output-p)
1095 wlott 1.16 (element-type 'base-char)
1096 ram 1.1 (buffering :full)
1097 ram 1.15 timeout
1098 ram 1.1 file
1099     original
1100     delete-original
1101 ram 1.23.1.3 input-buffer-p
1102 ram 1.1 (name (if file
1103     (format nil "file ~S" file)
1104 wlott 1.17 (format nil "descriptor ~D" fd)))
1105     auto-close)
1106 ram 1.15 (declare (type index fd) (type (or index null) timeout)
1107     (type (member :none :line :full) buffering))
1108     "Create a stream for the given unix file descriptor.
1109     If input is non-nil, allow input operations.
1110     If output is non-nil, allow output operations.
1111     If neither input nor output are specified, default to allowing input.
1112     Element-type indicates the element type to use (as for open).
1113     Buffering indicates the kind of buffering to use.
1114     Timeout (if true) is the number of seconds to wait for input. If NIL (the
1115     default), then wait forever. When we time out, we signal IO-TIMEOUT.
1116     File is the name of the file (will be returned by PATHNAME).
1117     Name is used to identify the stream when printed."
1118 ram 1.1 (cond ((not (or input-p output-p))
1119     (setf input t))
1120     ((not (or input output))
1121     (error "File descriptor must be opened either for input or output.")))
1122     (let ((stream (%make-fd-stream :fd fd
1123     :name name
1124     :file file
1125     :original original
1126     :delete-original delete-original
1127 ram 1.15 :buffering buffering
1128     :timeout timeout)))
1129 ram 1.23.1.3 (set-routines stream element-type input output input-buffer-p)
1130 wlott 1.17 (when (and auto-close (fboundp 'finalize))
1131     (finalize stream
1132     #'(lambda ()
1133 wlott 1.19 (unix:unix-close fd)
1134 wlott 1.17 (format *terminal-io* "** Closed file descriptor ~D~%"
1135     fd))))
1136 ram 1.1 stream))
1137    
1138 ram 1.23.1.3
1139 ram 1.1 ;;; PICK-PACKUP-NAME -- internal
1140     ;;;
1141     ;;; Pick a name to use for the backup file.
1142     ;;;
1143     (defvar *backup-extension* ".BAK"
1144     "This is a string that OPEN tacks on the end of a file namestring to produce
1145     a name for the :if-exists :rename-and-delete and :rename options. Also,
1146     this can be a function that takes a namestring and returns a complete
1147     namestring.")
1148     ;;;
1149     (defun pick-backup-name (name)
1150 ram 1.14 (declare (type simple-string name))
1151     (let ((ext *backup-extension*))
1152     (etypecase ext
1153     (simple-string (concatenate 'simple-string name ext))
1154     (function (funcall ext name)))))
1155 ram 1.1
1156     ;;; ASSURE-ONE-OF -- internal
1157     ;;;
1158     ;;; Assure that the given arg is one of the given list of valid things.
1159     ;;; Allow the user to fix any problems.
1160     ;;;
1161     (defun assure-one-of (item list what)
1162     (unless (member item list)
1163     (loop
1164     (cerror "Enter new value for ~*~S"
1165     "~S is invalid for ~S. Must be one of~{ ~S~}"
1166     item
1167     what
1168     list)
1169 ram 1.14 (format (the stream *query-io*) "Enter new value for ~S: " what)
1170 ram 1.1 (force-output *query-io*)
1171     (setf item (read *query-io*))
1172     (when (member item list)
1173     (return))))
1174     item)
1175    
1176 ram 1.14 ;;; DO-OLD-RENAME -- Internal
1177     ;;;
1178     ;;; Rename Namestring to Original. First, check if we have write access,
1179     ;;; since we don't want to trash unwritable files even if we technically can.
1180     ;;; We return true if we suceed in renaming.
1181     ;;;
1182     (defun do-old-rename (namestring original)
1183 wlott 1.19 (unless (unix:unix-access namestring unix:w_ok)
1184 ram 1.14 (cerror "Try to rename it anyway." "File ~S is not writable." namestring))
1185     (multiple-value-bind
1186     (okay err)
1187 wlott 1.19 (unix:unix-rename namestring original)
1188 ram 1.14 (cond (okay t)
1189     (t
1190     (cerror "Use :SUPERSEDE instead."
1191     "Could not rename ~S to ~S: ~A."
1192     namestring
1193     original
1194 wlott 1.19 (unix:get-unix-error-msg err))
1195 ram 1.14 nil))))
1196    
1197    
1198 ram 1.1 ;;; OPEN -- public
1199     ;;;
1200     ;;; Open the given file.
1201     ;;;
1202     (defun open (filename
1203     &key
1204     (direction :input)
1205 wlott 1.16 (element-type 'base-char)
1206 ram 1.1 (if-exists nil if-exists-given)
1207     (if-does-not-exist nil if-does-not-exist-given))
1208     "Return a stream which reads from or writes to Filename.
1209     Defined keywords:
1210     :direction - one of :input, :output, :io, or :probe
1211 wlott 1.16 :element-type - Type of object to read or write, default BASE-CHAR
1212 ram 1.1 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
1213     :overwrite, :append, :supersede or nil
1214     :if-does-not-exist - one of :error, :create or nil
1215     See the manual for details."
1216     ;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
1217     (setf direction
1218     (assure-one-of direction
1219     '(:input :output :io :probe)
1220     :direction))
1221    
1222     ;; Calculate useful stuff.
1223     (multiple-value-bind
1224     (input output mask)
1225     (case direction
1226 wlott 1.19 (:input (values t nil unix:o_rdonly))
1227     (:output (values nil t unix:o_wronly))
1228     (:io (values t t unix:o_rdwr))
1229     (:probe (values t nil unix:o_rdonly)))
1230 ram 1.14 (declare (type index mask))
1231 ram 1.1 (let* ((pathname (pathname filename))
1232 wlott 1.8 (namestring (unix-namestring pathname input)))
1233 ram 1.1 ;; Process if-exists argument if we are doing any output.
1234     (cond (output
1235     (unless if-exists-given
1236     (setf if-exists
1237     (if (eq (pathname-version pathname) :newest)
1238     :new-version
1239     :error)))
1240     (setf if-exists
1241     (assure-one-of if-exists
1242     '(:error :new-version :rename
1243     :rename-and-delete :overwrite
1244     :append :supersede nil)
1245     :if-exists))
1246     (case if-exists
1247     ((:error nil)
1248 wlott 1.19 (setf mask (logior mask unix:o_excl)))
1249 ram 1.1 ((:rename :rename-and-delete)
1250 wlott 1.19 (setf mask (logior mask unix:o_creat)))
1251 ram 1.1 ((:new-version :supersede)
1252 wlott 1.19 (setf mask (logior mask unix:o_trunc)))
1253 ram 1.1 (:append
1254 wlott 1.19 (setf mask (logior mask unix:o_append)))))
1255 ram 1.1 (t
1256     (setf if-exists :ignore-this-arg)))
1257    
1258     (unless if-does-not-exist-given
1259     (setf if-does-not-exist
1260     (cond ((eq direction :input) :error)
1261     ((and output
1262     (member if-exists '(:overwrite :append)))
1263     :error)
1264     ((eq direction :probe)
1265     nil)
1266     (t
1267     :create))))
1268     (setf if-does-not-exist
1269     (assure-one-of if-does-not-exist
1270     '(:error :create nil)
1271     :if-does-not-exist))
1272     (if (eq if-does-not-exist :create)
1273 wlott 1.19 (setf mask (logior mask unix:o_creat)))
1274 ram 1.1
1275     (let ((original (if (member if-exists
1276     '(:rename :rename-and-delete))
1277 ram 1.14 (pick-backup-name namestring)))
1278 ram 1.1 (delete-original (eq if-exists :rename-and-delete))
1279     (mode #o666))
1280     (when original
1281     ;; We are doing a :rename or :rename-and-delete.
1282     ;; Determine if the file already exists, make sure the original
1283     ;; file is not a directory and keep the mode
1284     (let ((exists
1285 wlott 1.18 (and namestring
1286     (multiple-value-bind
1287     (okay err/dev inode orig-mode)
1288 wlott 1.19 (unix:unix-stat namestring)
1289 wlott 1.18 (declare (ignore inode)
1290     (type (or index null) orig-mode))
1291     (cond
1292     (okay
1293 ram 1.1 (when (and output (= (logand orig-mode #o170000)
1294     #o40000))
1295     (error "Cannot open ~S for output: Is a directory."
1296     namestring))
1297     (setf mode (logand orig-mode #o777))
1298     t)
1299 wlott 1.19 ((eql err/dev unix:enoent)
1300 ram 1.1 nil)
1301     (t
1302     (error "Cannot find ~S: ~A"
1303     namestring
1304 wlott 1.19 (unix:get-unix-error-msg err/dev))))))))
1305 ram 1.14 (unless (and exists
1306     (do-old-rename namestring original))
1307 ram 1.1 (setf original nil)
1308     (setf delete-original nil)
1309     ;; In order to use SUPERSEDE instead, we have
1310 wlott 1.19 ;; to make sure unix:o_creat corresponds to
1311     ;; if-does-not-exist. unix:o_creat was set
1312 ram 1.1 ;; before because of if-exists being :rename.
1313     (unless (eq if-does-not-exist :create)
1314 wlott 1.19 (setf mask (logior (logandc2 mask unix:o_creat) unix:o_trunc)))
1315 ram 1.1 (setf if-exists :supersede))))
1316    
1317     ;; Okay, now we can try the actual open.
1318 wlott 1.9 (loop
1319     (multiple-value-bind
1320     (fd errno)
1321 wlott 1.18 (if namestring
1322 wlott 1.19 (unix:unix-open namestring mask mode)
1323     (values nil unix:enoent))
1324 wlott 1.9 (cond ((numberp fd)
1325     (return
1326     (case direction
1327     ((:input :output :io)
1328     (make-fd-stream fd
1329     :input input
1330     :output output
1331     :element-type element-type
1332     :file namestring
1333     :original original
1334 wlott 1.17 :delete-original delete-original
1335 ram 1.23.1.3 :input-buffer-p t
1336 wlott 1.17 :auto-close t))
1337 wlott 1.9 (:probe
1338     (let ((stream
1339     (%make-fd-stream :name namestring :fd fd
1340     :element-type element-type)))
1341     (close stream)
1342     stream)))))
1343 wlott 1.19 ((eql errno unix:enoent)
1344 wlott 1.9 (case if-does-not-exist
1345     (:error
1346     (cerror "Return NIL."
1347     "Error opening ~S, ~A."
1348     pathname
1349 wlott 1.19 (unix:get-unix-error-msg errno)))
1350 wlott 1.9 (:create
1351     (cerror "Return NIL."
1352     "Error creating ~S, path does not exist."
1353     pathname)))
1354     (return nil))
1355 wlott 1.19 ((eql errno unix:eexist)
1356 wlott 1.9 (unless (eq nil if-exists)
1357     (cerror "Return NIL."
1358     "Error opening ~S, ~A."
1359     pathname
1360 wlott 1.19 (unix:get-unix-error-msg errno)))
1361 wlott 1.9 (return nil))
1362 wlott 1.19 ((eql errno unix:eacces)
1363 wlott 1.9 (cerror "Try again."
1364     "Error opening ~S, ~A."
1365     pathname
1366 wlott 1.19 (unix:get-unix-error-msg errno)))
1367 wlott 1.9 (t
1368 ram 1.1 (cerror "Return NIL."
1369     "Error opening ~S, ~A."
1370     pathname
1371 wlott 1.19 (unix:get-unix-error-msg errno))
1372 wlott 1.9 (return nil)))))))))
1373 ram 1.1
1374     ;;;; Initialization.
1375    
1376     (defvar *tty* nil
1377     "The stream connected to the controlling terminal or NIL if there is none.")
1378     (defvar *stdin* nil
1379     "The stream connected to the standard input (file descriptor 0).")
1380     (defvar *stdout* nil
1381     "The stream connected to the standard output (file descriptor 1).")
1382     (defvar *stderr* nil
1383     "The stream connected to the standard error output (file descriptor 2).")
1384    
1385     ;;; STREAM-INIT -- internal interface
1386     ;;;
1387     ;;; Called when the cold load is first started up.
1388     ;;;
1389     (defun stream-init ()
1390     (stream-reinit)
1391     (setf *terminal-io* (make-synonym-stream '*tty*))
1392     (setf *standard-output* (make-synonym-stream '*stdout*))
1393 ram 1.11 (setf *standard-input*
1394     (make-two-way-stream (make-synonym-stream '*stdin*)
1395     *standard-output*))
1396 ram 1.1 (setf *error-output* (make-synonym-stream '*stderr*))
1397     (setf *query-io* (make-synonym-stream '*terminal-io*))
1398     (setf *debug-io* *query-io*)
1399     (setf *trace-output* *standard-output*)
1400     nil)
1401    
1402     ;;; STREAM-REINIT -- internal interface
1403     ;;;
1404     ;;; Called whenever a saved core is restarted.
1405     ;;;
1406     (defun stream-reinit ()
1407     (setf *available-buffers* nil)
1408     (setf *stdin*
1409     (make-fd-stream 0 :name "Standard Input" :input t :buffering :line))
1410     (setf *stdout*
1411     (make-fd-stream 1 :name "Standard Output" :output t :buffering :line))
1412     (setf *stderr*
1413     (make-fd-stream 2 :name "Standard Error" :output t :buffering :line))
1414 wlott 1.19 (let ((tty (unix:unix-open "/dev/tty" unix:o_rdwr #o666)))
1415 ram 1.1 (if tty
1416     (setf *tty*
1417     (make-fd-stream tty :name "the Terminal" :input t :output t
1418 wlott 1.17 :buffering :line :auto-close t))
1419 ram 1.1 (setf *tty* (make-two-way-stream *stdin* *stdout*))))
1420     nil)
1421    
1422    
1423     ;;;; Beeping.
1424    
1425     (defun default-beep-function (stream)
1426     (write-char #\bell stream)
1427     (finish-output stream))
1428    
1429     (defvar *beep-function* #'default-beep-function
1430     "This is called in BEEP to feep the user. It takes a stream.")
1431    
1432     (defun beep (&optional (stream *terminal-io*))
1433     (funcall *beep-function* stream))
1434    
1435    
1436     ;;; File-Name -- internal interface
1437     ;;;
1438     ;;; Kind of like File-Position, but is an internal hack used by the filesys
1439     ;;; stuff to get and set the file name.
1440     ;;;
1441     (defun file-name (stream &optional new-name)
1442     (when (fd-stream-p stream)
1443     (if new-name
1444     (setf (fd-stream-file stream) new-name)
1445     (fd-stream-file stream))))

  ViewVC Help
Powered by ViewVC 1.1.5