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