/[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.123 - (hide annotations)
Thu Feb 17 02:55:45 2011 UTC (3 years, 2 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2011-04, snapshot-2011-03
Changes since 1.122: +2 -2 lines
Fix ansi-test failure OPEN.IO.28:

(OPEN PN :DIRECTION :IO :IF-DOES-NOT-EXIST NIL)

no longer signals an error.

code/fd-stream.lisp:
o Don't try to create a new version if there's no existing file

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