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

  ViewVC Help
Powered by ViewVC 1.1.5