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

  ViewVC Help
Powered by ViewVC 1.1.5