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