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