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

  ViewVC Help
Powered by ViewVC 1.1.5