/[cmucl]/src/code/stream.lisp
ViewVC logotype

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56 - (hide annotations)
Tue Nov 19 14:30:57 2002 UTC (11 years, 5 months ago) by toy
Branch: MAIN
Changes since 1.55: +112 -41 lines
Entomotomy: peek-char-wrongly-echos-to-echo-stream

Port Matthew Danish's fix for this bug for SBCL.
1 ram 1.14 ;;; -*- Package: Lisp -*-
2 ram 1.1 ;;;
3     ;;; **********************************************************************
4 ram 1.9 ;;; 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 toy 1.56 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/stream.lisp,v 1.56 2002/11/19 14:30:57 toy Exp $")
9 ram 1.9 ;;;
10 ram 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; Stream functions for Spice Lisp.
13     ;;; Written by Skef Wholey and Rob MacLachlan.
14 dtc 1.33 ;;; Gray streams support by Douglas Crosher, 1998.
15 ram 1.1 ;;;
16 ram 1.14 ;;; This file contains the OS-independent stream functions.
17 ram 1.1 ;;;
18 ram 1.2 (in-package "LISP")
19 ram 1.1
20 ram 1.14 (export '(broadcast-stream make-broadcast-stream broadcast-stream-streams
21     synonym-stream make-synonym-stream synonym-stream-symbol
22     concatenated-stream make-concatenated-stream
23     concatenated-stream-streams
24     two-way-stream make-two-way-stream two-way-stream-input-stream
25     two-way-stream-output-stream
26     echo-stream make-echo-stream echo-stream-input-stream
27     echo-stream-output-stream
28     make-string-input-stream make-string-output-stream
29 ram 1.1 get-output-stream-string stream-element-type input-stream-p
30 ram 1.14 output-stream-p open-stream-p interactive-stream-p
31 wlott 1.17 open-stream-p close read-line read-char
32 ram 1.1 unread-char peek-char listen read-char-no-hang clear-input read-byte
33     write-char write-string write-line terpri fresh-line
34     finish-output force-output clear-output write-byte
35 ram 1.14 stream streamp string-stream
36     *standard-input* *standard-output*
37 ram 1.1 *error-output* *query-io* *debug-io* *terminal-io* *trace-output*))
38    
39 ram 1.18 (in-package "SYSTEM")
40 ram 1.1 (export '(make-indenting-stream read-n-bytes))
41    
42 ram 1.18 (in-package "EXT")
43    
44     (export '(get-stream-command
45     stream-command stream-command-p stream-command-name
46     stream-command-args make-stream-command make-case-frob-stream))
47    
48     (in-package "LISP")
49    
50 ram 1.14 (deftype string-stream ()
51     '(or string-input-stream string-output-stream
52     fill-pointer-output-stream))
53    
54 ram 1.1 ;;;; Standard streams:
55     ;;;
56     ;;; The initialization of these streams is performed by Stream-Init,
57     ;;; which lives in the file of machine-specific stream functions.
58     ;;;
59     (defvar *terminal-io* () "Terminal I/O stream.")
60     (defvar *standard-input* () "Default input stream.")
61     (defvar *standard-output* () "Default output stream.")
62     (defvar *error-output* () "Error output stream.")
63     (defvar *query-io* () "Query I/O stream.")
64     (defvar *trace-output* () "Trace output stream.")
65     (defvar *debug-io* () "Interactive debugging stream.")
66    
67 toy 1.52 (defun ill-in-any (stream &rest ignore)
68     (declare (ignore ignore))
69     (error 'simple-type-error
70     :datum stream
71     :expected-type '(satisfies input-stream-p)
72     :format-control "~S is not an input stream."
73     :format-arguments (list stream)))
74     (defun ill-out-any (stream &rest ignore)
75     (declare (ignore ignore))
76     (error 'simple-type-error
77     :datum stream
78     :expected-type '(satisfies output-stream-p)
79     :format-control "~S is not an output stream."
80     :format-arguments (list stream)))
81 ram 1.1 (defun ill-in (stream &rest ignore)
82     (declare (ignore ignore))
83 pw 1.34 (error 'simple-type-error
84     :datum stream
85     :expected-type '(satisfies input-stream-p)
86     :format-control "~S is not a character input stream."
87     :format-arguments (list stream)))
88 ram 1.1 (defun ill-out (stream &rest ignore)
89     (declare (ignore ignore))
90 pw 1.34 (error 'simple-type-error
91     :datum stream
92     :expected-type '(satisfies output-stream-p)
93     :format-control "~S is not a character output stream."
94     :format-arguments (list stream)))
95 ram 1.1 (defun ill-bin (stream &rest ignore)
96     (declare (ignore ignore))
97 pw 1.34 (error 'simple-type-error
98     :datum stream
99     :expected-type '(satisfies input-stream-p)
100     :format-control "~S is not a binary input stream."
101     :format-arguments (list stream)))
102 toy 1.49 (defun ill-n-bin (stream &rest ignore)
103     (declare (ignore ignore))
104     (error 'simple-type-error
105     :datum stream
106     :expected-type '(satisfies input-stream-p)
107     :format-control "~S is not a binary input stream or does not support multi-byte read operations."
108     :format-arguments (list stream)))
109 ram 1.1 (defun ill-bout (stream &rest ignore)
110     (declare (ignore ignore))
111 pw 1.34 (error 'simple-type-error
112     :datum stream
113     :expected-type '(satisfies output-stream-p)
114     :format-control "~S is not a binary output stream."
115     :format-arguments (list stream)))
116 ram 1.1 (defun closed-flame (stream &rest ignore)
117     (declare (ignore ignore))
118     (error "~S is closed." stream))
119     (defun do-nothing (&rest ignore)
120     (declare (ignore ignore)))
121    
122     (defun %print-stream (structure stream d)
123     (declare (ignore d structure))
124     (write-string "#<Bare Stream>" stream))
125    
126     ;;; HOW THE STREAM STRUCTURE IS USED:
127     ;;;
128     ;;; Many of the slots of the stream structure contain functions
129     ;;; which are called to perform some operation on the stream. Closed
130     ;;; streams have #'Closed-Flame in all of their function slots. If
131     ;;; one side of an I/O or echo stream is closed, the whole stream is
132     ;;; considered closed. The functions in the operation slots take
133     ;;; arguments as follows:
134     ;;;
135     ;;; In: Stream, Eof-Errorp, Eof-Value
136     ;;; Bin: Stream, Eof-Errorp, Eof-Value
137     ;;; N-Bin: Stream, Buffer, Start, Numbytes, Eof-Errorp
138     ;;; Out: Stream, Character
139     ;;; Bout: Stream, Integer
140     ;;; Sout: Stream, String, Start, End
141     ;;; Misc: Stream, Operation, &Optional Arg1, Arg2
142     ;;;
143     ;;; In order to save space, some of the less common stream operations
144     ;;; are handled by just one function, the Misc method. This function
145     ;;; is passed a keyword which indicates the operation to perform.
146     ;;; The following keywords are used:
147 ram 1.10 ;;; :listen - Return the following values:
148     ;;; t if any input waiting.
149     ;;; :eof if at eof.
150     ;;; nil if no input is available and not at eof.
151 ram 1.1 ;;; :unread - Unread the character Arg.
152     ;;; :close - Do any stream specific stuff to close the stream.
153     ;;; The methods are set to closed-flame by the close
154     ;;; function, so that need not be done by this
155     ;;; function.
156     ;;; :clear-input - Clear any unread input
157     ;;; :finish-output,
158     ;;; :force-output - Cause output to happen
159     ;;; :clear-output - Clear any undone output
160 ram 1.18 ;;; :element-type - Return the type of element the stream deals wit<h.
161 ram 1.1 ;;; :line-length - Return the length of a line of output.
162     ;;; :charpos - Return current output position on the line.
163     ;;; :file-length - Return the file length of a file stream.
164     ;;; :file-position - Return or change the current position of a file stream.
165     ;;; :file-name - Return the name of an associated file.
166 ram 1.14 ;;; :interactive-p - Is this an interactive device?
167 ram 1.1 ;;;
168     ;;; In order to do almost anything useful, it is necessary to
169     ;;; define a new type of structure that includes stream, so that the
170     ;;; stream can have some state information.
171     ;;;
172     ;;; THE STREAM IN-BUFFER:
173     ;;;
174     ;;; The In-Buffer in the stream holds characters or bytes that
175     ;;; are ready to be read by some input function. If there is any
176     ;;; stuff in the In-Buffer, then the reading function can use it
177     ;;; without calling any stream method. Any stream may put stuff in
178     ;;; the In-Buffer, and may also assume that any input in the In-Buffer
179     ;;; has been consumed before any in-method is called. If a text
180     ;;; stream has in In-Buffer, then the first character should not be
181     ;;; used to buffer normal input so that it is free for unreading into.
182     ;;;
183     ;;; The In-Buffer slot is a vector In-Buffer-Length long. The
184     ;;; In-Index is the index in the In-Buffer of the first available
185     ;;; object. The available objects are thus between In-Index and the
186     ;;; length of the In-Buffer.
187     ;;;
188     ;;; When this buffer is only accessed by the normal stream
189     ;;; functions, the number of function calls is halved, thus
190     ;;; potentially doubling the speed of simple operations. If the
191     ;;; Fast-Read-Char and Fast-Read-Byte macros are used, nearly all
192     ;;; function call overhead is removed, vastly speeding up these
193     ;;; important operations.
194     ;;;
195     ;;; If a stream does not have an In-Buffer, then the In-Buffer slot
196     ;;; must be nil, and the In-Index must be In-Buffer-Length. These are
197     ;;; the default values for the slots.
198 ram 1.18
199 ram 1.1
200     ;;; Stream manipulation functions.
201    
202     (defun input-stream-p (stream)
203     "Returns non-nil if the given Stream can perform input operations."
204 dtc 1.30 (and (lisp-stream-p stream)
205     (not (eq (lisp-stream-in stream) #'closed-flame))
206     (or (not (eq (lisp-stream-in stream) #'ill-in))
207 toy 1.49 (not (eq (lisp-stream-bin stream) #'ill-bin))
208     (not (eq (lisp-stream-n-bin stream) #'ill-n-bin)))))
209 ram 1.1
210     (defun output-stream-p (stream)
211     "Returns non-nil if the given Stream can perform output operations."
212 dtc 1.30 (and (lisp-stream-p stream)
213     (not (eq (lisp-stream-in stream) #'closed-flame))
214     (or (not (eq (lisp-stream-out stream) #'ill-out))
215     (not (eq (lisp-stream-bout stream) #'ill-bout)))))
216 ram 1.1
217 ram 1.14 (defun open-stream-p (stream)
218     "Return true if Stream is not closed."
219     (declare (type stream stream))
220 dtc 1.30 (not (eq (lisp-stream-in stream) #'closed-flame)))
221 ram 1.14
222 ram 1.1 (defun stream-element-type (stream)
223     "Returns a type specifier for the kind of object returned by the Stream."
224 wlott 1.17 (declare (type stream stream))
225 dtc 1.30 (funcall (lisp-stream-misc stream) stream :element-type))
226 ram 1.1
227 ram 1.14 (defun interactive-stream-p (stream)
228     "Return true if Stream does I/O on a terminal or other interactive device."
229     (declare (type stream stream))
230 dtc 1.30 (funcall (lisp-stream-misc stream) stream :interactive-p))
231 ram 1.14
232 wlott 1.17 (defun open-stream-p (stream)
233     "Return true if and only if STREAM has not been closed."
234     (declare (type stream stream))
235 dtc 1.30 (not (eq (lisp-stream-in stream) #'closed-flame)))
236 wlott 1.17
237 ram 1.1 (defun close (stream &key abort)
238     "Closes the given Stream. No more I/O may be performed, but inquiries
239     may still be made. If :Abort is non-nil, an attempt is made to clean
240     up the side effects of having created the stream."
241 wlott 1.17 (declare (type stream stream))
242     (when (open-stream-p stream)
243 dtc 1.30 (funcall (lisp-stream-misc stream) stream :close abort))
244 ram 1.1 t)
245    
246     (defun set-closed-flame (stream)
247 dtc 1.30 (setf (lisp-stream-in stream) #'closed-flame)
248     (setf (lisp-stream-bin stream) #'closed-flame)
249     (setf (lisp-stream-n-bin stream) #'closed-flame)
250     (setf (lisp-stream-in stream) #'closed-flame)
251     (setf (lisp-stream-out stream) #'closed-flame)
252     (setf (lisp-stream-bout stream) #'closed-flame)
253     (setf (lisp-stream-sout stream) #'closed-flame)
254     (setf (lisp-stream-misc stream) #'closed-flame))
255 ram 1.18
256 ram 1.1
257 ram 1.18 ;;;; File position and file length.
258    
259     ;;; File-Position -- Public
260     ;;;
261     ;;; Call the misc method with the :file-position operation.
262     ;;;
263     (defun file-position (stream &optional position)
264     "With one argument returns the current position within the file
265 wlott 1.19 File-Stream is open to. If the second argument is supplied, then
266     this becomes the new file position. The second argument may also
267     be :start or :end for the start and end of the file, respectively."
268     (declare (stream stream)
269     (type (or index (member nil :start :end)) position))
270 ram 1.18 (cond
271     (position
272 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
273     (funcall (lisp-stream-misc stream) stream :file-position position))
274 ram 1.18 (t
275 dtc 1.30 (let ((res (funcall (lisp-stream-misc stream) stream :file-position nil)))
276     (when res (- res (- in-buffer-length (lisp-stream-in-index stream))))))))
277 ram 1.18
278    
279     ;;; File-Length -- Public
280     ;;;
281     ;;; Like File-Position, only use :file-length.
282     ;;;
283     (defun file-length (stream)
284     "This function returns the length of the file that File-Stream is open to."
285     (declare (stream stream))
286 dtc 1.30 (funcall (lisp-stream-misc stream) stream :file-length))
287 ram 1.18
288    
289 ram 1.1 ;;; Input functions:
290    
291     (defun read-line (&optional (stream *standard-input*) (eof-errorp t) eof-value
292     recursive-p)
293     "Returns a line of text read from the Stream as a string, discarding the
294     newline character."
295     (declare (ignore recursive-p))
296 dtc 1.32 (let ((stream (in-synonym-of stream)))
297 dtc 1.33 (if (lisp-stream-p stream)
298     (prepare-for-fast-read-char stream
299     (let ((res (make-string 80))
300     (len 80)
301     (index 0))
302     (loop
303     (let ((ch (fast-read-char nil nil)))
304     (cond (ch
305     (when (char= ch #\newline)
306     (done-with-fast-read-char)
307     (return (values (shrink-vector res index) nil)))
308     (when (= index len)
309     (setq len (* len 2))
310     (let ((new (make-string len)))
311     (replace new res)
312     (setq res new)))
313     (setf (schar res index) ch)
314     (incf index))
315     ((zerop index)
316     (done-with-fast-read-char)
317     (return (values (eof-or-lose stream eof-errorp eof-value)
318     t)))
319     ;; since fast-read-char hit already the eof char, we
320     ;; shouldn't do another read-char
321     (t
322     (done-with-fast-read-char)
323     (return (values (shrink-vector res index) t))))))))
324     ;; Fundamental-stream.
325     (multiple-value-bind (string eof)
326     (stream-read-line stream)
327     (if (and eof (zerop (length string)))
328     (values (eof-or-lose stream eof-errorp eof-value) t)
329     (values string eof))))))
330 ram 1.10
331 ram 1.1 ;;; We proclaim them inline here, then proclaim them notinline at EOF,
332     ;;; so, except in this file, they are not inline by default, but they can be.
333     ;;;
334 pw 1.45 (declaim (inline read-char unread-char read-byte listen))
335 ram 1.1 (defun read-char (&optional (stream *standard-input*) (eof-errorp t) eof-value
336     recursive-p)
337     "Inputs a character from Stream and returns it."
338     (declare (ignore recursive-p))
339 dtc 1.32 (let ((stream (in-synonym-of stream)))
340 dtc 1.33 (if (lisp-stream-p stream)
341     (prepare-for-fast-read-char stream
342     (prog1
343     (fast-read-char eof-errorp eof-value)
344     (done-with-fast-read-char)))
345     ;; Fundamental-stream.
346     (let ((char (stream-read-char stream)))
347     (if (eq char :eof)
348     (eof-or-lose stream eof-errorp eof-value)
349     char)))))
350 ram 1.1
351     (defun unread-char (character &optional (stream *standard-input*))
352     "Puts the Character back on the front of the input Stream."
353 dtc 1.32 (let ((stream (in-synonym-of stream)))
354 dtc 1.33 (if (lisp-stream-p stream)
355     (let ((index (1- (lisp-stream-in-index stream)))
356     (buffer (lisp-stream-in-buffer stream)))
357     (declare (fixnum index))
358     (when (minusp index) (error "Nothing to unread."))
359     (cond (buffer
360     (setf (aref buffer index) (char-code character))
361     (setf (lisp-stream-in-index stream) index))
362     (t
363     (funcall (lisp-stream-misc stream) stream
364     :unread character))))
365     ;; Fundamental-stream
366     (stream-unread-char stream character)))
367 ram 1.1 nil)
368    
369 toy 1.56 ;;; In the interest of ``once and only once'' this macro contains the
370     ;;; framework necessary to implement a peek-char function, which has
371     ;;; two special-cases (one for gray streams and one for echo streams)
372     ;;; in addition to the normal case.
373     ;;;
374     ;;; All arguments are forms which will be used for a specific purpose
375     ;;; PEEK-TYPE - the current peek-type as defined by ANSI CL
376     ;;; EOF-VALUE - the eof-value argument to peek-char
377     ;;; CHAR-VAR - the variable which will be used to store the current character
378     ;;; READ-FORM - the form which will be used to read a character
379     ;;; UNREAD-FORM - ditto for unread-char
380     ;;; SKIPPED-CHAR-FORM - the form to execute when skipping a character
381     ;;; EOF-DETECTED-FORM - the form to execute when EOF has been detected
382     ;;; (this will default to CHAR-VAR)
383     (defmacro generalized-peeking-mechanism (peek-type eof-value char-var read-form unread-form
384     &optional (skipped-char-form nil)
385     (eof-detected-form nil))
386     `(let ((,char-var ,read-form))
387     (cond ((eql ,char-var ,eof-value)
388     ,(if eof-detected-form
389     eof-detected-form
390     char-var))
391     ((characterp ,peek-type)
392     (do ((,char-var ,char-var ,read-form))
393     ((or (eql ,char-var ,eof-value)
394     (char= ,char-var ,peek-type))
395     (cond ((eql ,char-var ,eof-value)
396     ,(if eof-detected-form
397     eof-detected-form
398     char-var))
399     (t ,unread-form
400     ,char-var)))
401     ,skipped-char-form))
402     ((eql ,peek-type t)
403     (do ((,char-var ,char-var ,read-form))
404     ((or (eql ,char-var ,eof-value)
405     (not (whitespace-char-p ,char-var)))
406     (cond ((eql ,char-var ,eof-value)
407     ,(if eof-detected-form
408     eof-detected-form
409     char-var))
410     (t ,unread-form
411     ,char-var)))
412     ,skipped-char-form))
413     ((null ,peek-type)
414     ,unread-form
415     ,char-var)
416     (t
417     (error "Impossible case reached in PEEK-CHAR")))))
418    
419 ram 1.1 (defun peek-char (&optional (peek-type nil) (stream *standard-input*)
420     (eof-errorp t) eof-value recursive-p)
421     "Peeks at the next character in the input Stream. See manual for details."
422 dtc 1.41 (declare (ignore recursive-p))
423 toy 1.56 ;; FIXME: The type of PEEK-TYPE is also declared in a DEFKNOWN, but
424     ;; the compiler doesn't seem to be smart enough to go from there to
425     ;; imposing a type check. Figure out why (because PEEK-TYPE is an
426     ;; &OPTIONAL argument?) and fix it, and then this explicit type
427     ;; check can go away.
428     (unless (typep peek-type '(or character boolean))
429     (error 'simple-type-error
430     :datum peek-type
431     :expected-type '(or character boolean)
432     :format-control "~@<bad PEEK-TYPE=~S, ~_expected ~S~:>"
433     :format-arguments (list peek-type '(or character boolean))))
434 dtc 1.41 (let ((stream (in-synonym-of stream)))
435 toy 1.56 (cond ((typep stream 'echo-stream)
436     (echo-misc stream
437     :peek-char
438     peek-type
439     (list eof-errorp eof-value)))
440     ((lisp-stream-p stream)
441     (generalized-peeking-mechanism
442     peek-type eof-value char
443     (read-char stream eof-errorp eof-value)
444     (unread-char char stream)))
445     (t
446     ;; by elimination, must be Gray streams FUNDAMENTAL-STREAM
447     (generalized-peeking-mechanism
448     peek-type :eof char
449     (if (null peek-type)
450     (stream-peek-char stream)
451     (stream-read-char stream))
452     (if (null peek-type)
453     ()
454     (stream-unread-char stream char))
455     ()
456     (eof-or-lose stream eof-errorp eof-value))))))
457 ram 1.1
458     (defun listen (&optional (stream *standard-input*))
459     "Returns T if a character is availible on the given Stream."
460 dtc 1.32 (let ((stream (in-synonym-of stream)))
461 dtc 1.33 (if (lisp-stream-p stream)
462     (or (/= (the fixnum (lisp-stream-in-index stream)) in-buffer-length)
463     ;; Test for t explicitly since misc methods return :eof sometimes.
464     (eq (funcall (lisp-stream-misc stream) stream :listen) t))
465     ;; Fundamental-stream.
466     (stream-listen stream))))
467 ram 1.1
468     (defun read-char-no-hang (&optional (stream *standard-input*)
469     (eof-errorp t) eof-value recursive-p)
470     "Returns the next character from the Stream if one is availible, or nil."
471     (declare (ignore recursive-p))
472 dtc 1.32 (let ((stream (in-synonym-of stream)))
473 dtc 1.33 (if (lisp-stream-p stream)
474     (if (funcall (lisp-stream-misc stream) stream :listen)
475     ;; On t or :eof get READ-CHAR to do the work.
476     (read-char stream eof-errorp eof-value)
477     nil)
478     ;; Fundamental-stream.
479     (let ((char (stream-read-char-no-hang stream)))
480     (if (eq char :eof)
481     (eof-or-lose stream eof-errorp eof-value)
482     char)))))
483 dtc 1.30
484 ram 1.1
485     (defun clear-input (&optional (stream *standard-input*))
486     "Clears any buffered input associated with the Stream."
487 toy 1.52 (declare (type streamlike stream))
488 dtc 1.32 (let ((stream (in-synonym-of stream)))
489 dtc 1.33 (cond ((lisp-stream-p stream)
490     (setf (lisp-stream-in-index stream) in-buffer-length)
491     (funcall (lisp-stream-misc stream) stream :clear-input))
492     (t
493     (stream-clear-input stream))))
494 dtc 1.30 nil)
495 ram 1.1
496     (defun read-byte (stream &optional (eof-errorp t) eof-value)
497     "Returns the next byte of the Stream."
498 toy 1.52 (declare (type stream stream))
499 dtc 1.32 (let ((stream (in-synonym-of stream)))
500 dtc 1.33 (if (lisp-stream-p stream)
501     (prepare-for-fast-read-byte stream
502     (prog1
503     (fast-read-byte eof-errorp eof-value t)
504     (done-with-fast-read-byte)))
505     ;; Fundamental-stream.
506     (let ((char (stream-read-byte stream)))
507     (if (eq char :eof)
508     (eof-or-lose stream eof-errorp eof-value)
509     char)))))
510 ram 1.1
511     (defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
512 ram 1.24 "Reads Numbytes bytes into the Buffer starting at Start, returning the number
513     of bytes read.
514     -- If EOF-ERROR-P is true, an END-OF-FILE condition is signalled if
515     end-of-file is encountered before Count bytes have been read.
516     -- If EOF-ERROR-P is false, READ-N-BYTES reads as much data is currently
517     available (up to count bytes.) On pipes or similar devices, this
518     function returns as soon as any adata is available, even if the amount
519     read is less than Count and eof has not been hit."
520 dtc 1.30 (declare (type lisp-stream stream)
521     (type index numbytes start)
522 ram 1.18 (type (or (simple-array * (*)) system-area-pointer) buffer))
523 dtc 1.32 (let* ((stream (in-synonym-of stream lisp-stream))
524 dtc 1.30 (in-buffer (lisp-stream-in-buffer stream))
525     (index (lisp-stream-in-index stream))
526 ram 1.1 (num-buffered (- in-buffer-length index)))
527     (declare (fixnum index num-buffered))
528     (cond
529 ram 1.2 ((not in-buffer)
530 dtc 1.30 (funcall (lisp-stream-n-bin stream) stream buffer start numbytes eof-errorp))
531 ram 1.1 ((<= numbytes num-buffered)
532     (%primitive byte-blt in-buffer index buffer start (+ start numbytes))
533 dtc 1.30 (setf (lisp-stream-in-index stream) (+ index numbytes))
534 ram 1.1 numbytes)
535     (t
536     (let ((end (+ start num-buffered)))
537     (%primitive byte-blt in-buffer index buffer start end)
538 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
539     (+ (funcall (lisp-stream-n-bin stream) stream buffer end
540 ram 1.18 (- numbytes num-buffered)
541     eof-errorp)
542 ram 1.1 num-buffered))))))
543 ram 1.18
544    
545     ;;; Amount of space we leave at the start of the in-buffer for unreading. 4
546     ;;; instead of 1 to allow word-aligned copies.
547     ;;;
548     (defconstant in-buffer-extra 4)
549    
550     ;;; FAST-READ-CHAR-REFILL -- Interface
551     ;;;
552     ;;; This function is called by the fast-read-char expansion to refill the
553     ;;; in-buffer for text streams. There is definitely an in-buffer, and hence
554     ;;; myst be an n-bin method.
555     ;;;
556     (defun fast-read-char-refill (stream eof-errorp eof-value)
557 dtc 1.30 (let* ((ibuf (lisp-stream-in-buffer stream))
558     (count (funcall (lisp-stream-n-bin stream) stream
559 ram 1.18 ibuf in-buffer-extra
560     (- in-buffer-length in-buffer-extra)
561     nil))
562     (start (- in-buffer-length count)))
563     (declare (type index start count))
564     (cond ((zerop count)
565 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
566     (funcall (lisp-stream-in stream) stream eof-errorp eof-value))
567 ram 1.18 (t
568     (when (/= start in-buffer-extra)
569     (bit-bash-copy ibuf (+ (* in-buffer-extra vm:byte-bits)
570     (* vm:vector-data-offset vm:word-bits))
571     ibuf (+ (the index (* start vm:byte-bits))
572     (* vm:vector-data-offset vm:word-bits))
573     (* count vm:byte-bits)))
574 dtc 1.30 (setf (lisp-stream-in-index stream) (1+ start))
575 ram 1.18 (code-char (aref ibuf start))))))
576    
577    
578     ;;; FAST-READ-BYTE-REFILL -- Interface
579     ;;;
580     ;;; Similar to FAST-READ-CHAR-REFILL, but we don't have to leave room for
581     ;;; unreading.
582     ;;;
583     (defun fast-read-byte-refill (stream eof-errorp eof-value)
584 dtc 1.30 (let* ((ibuf (lisp-stream-in-buffer stream))
585     (count (funcall (lisp-stream-n-bin stream) stream
586 ram 1.18 ibuf 0 in-buffer-length
587     nil))
588     (start (- in-buffer-length count)))
589     (declare (type index start count))
590     (cond ((zerop count)
591 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
592     (funcall (lisp-stream-bin stream) stream eof-errorp eof-value))
593 ram 1.18 (t
594     (unless (zerop start)
595     (bit-bash-copy ibuf (* vm:vector-data-offset vm:word-bits)
596     ibuf (+ (the index (* start vm:byte-bits))
597     (* vm:vector-data-offset vm:word-bits))
598     (* count vm:byte-bits)))
599 dtc 1.30 (setf (lisp-stream-in-index stream) (1+ start))
600 ram 1.18 (aref ibuf start)))))
601    
602 ram 1.1
603     ;;; Output functions:
604    
605     (defun write-char (character &optional (stream *standard-output*))
606     "Outputs the Character to the Stream."
607 toy 1.52 (declare (type streamlike stream))
608 dtc 1.32 (with-out-stream stream (lisp-stream-out character)
609     (stream-write-char character))
610 ram 1.1 character)
611    
612     (defun terpri (&optional (stream *standard-output*))
613     "Outputs a new line to the Stream."
614 toy 1.52 (declare (type streamlike stream))
615 dtc 1.32 (with-out-stream stream (lisp-stream-out #\newline) (stream-terpri))
616 ram 1.1 nil)
617    
618     (defun fresh-line (&optional (stream *standard-output*))
619     "Outputs a new line to the Stream if it is not positioned at the begining of
620     a line. Returns T if it output a new line, nil otherwise."
621 toy 1.52 (declare (type streamlike stream))
622 dtc 1.32 (let ((stream (out-synonym-of stream)))
623 dtc 1.33 (if (lisp-stream-p stream)
624     (when (/= (or (charpos stream) 1) 0)
625     (funcall (lisp-stream-out stream) stream #\newline)
626     t)
627     ;; Fundamental-stream.
628     (stream-fresh-line stream))))
629 ram 1.1
630     (defun write-string (string &optional (stream *standard-output*)
631 toy 1.50 &key (start 0) end)
632 ram 1.1 "Outputs the String to the given Stream."
633 toy 1.50 (write-string* string stream start (or end (length (the vector string)))))
634 ram 1.1
635     (defun write-string* (string &optional (stream *standard-output*)
636     (start 0) (end (length (the vector string))))
637     (declare (fixnum start end))
638 dtc 1.32 (let ((stream (out-synonym-of stream)))
639 dtc 1.33 (cond ((lisp-stream-p stream)
640     (if (array-header-p string)
641     (with-array-data ((data string) (offset-start start)
642     (offset-end end))
643     (funcall (lisp-stream-sout stream)
644     stream data offset-start offset-end))
645     (funcall (lisp-stream-sout stream) stream string start end))
646     string)
647     (t ; Fundamental-stream.
648     (stream-write-string stream string start end)))))
649 ram 1.1
650     (defun write-line (string &optional (stream *standard-output*)
651     &key (start 0) (end (length string)))
652     "Outputs the String to the given Stream, followed by a newline character."
653     (write-line* string stream start end))
654    
655     (defun write-line* (string &optional (stream *standard-output*)
656     (start 0) (end (length string)))
657     (declare (fixnum start end))
658 dtc 1.32 (let ((stream (out-synonym-of stream)))
659 dtc 1.33 (cond ((lisp-stream-p stream)
660     (if (array-header-p string)
661     (with-array-data ((data string) (offset-start start)
662     (offset-end end))
663     (with-out-stream stream (lisp-stream-sout data offset-start
664     offset-end)))
665     (with-out-stream stream (lisp-stream-sout string start end)))
666     (funcall (lisp-stream-out stream) stream #\newline))
667     (t ; Fundamental-stream.
668     (stream-write-string stream string start end)
669     (stream-write-char stream #\Newline)))
670 dtc 1.30 string))
671 ram 1.1
672     (defun charpos (&optional (stream *standard-output*))
673     "Returns the number of characters on the current line of output of the given
674     Stream, or Nil if that information is not availible."
675 dtc 1.32 (with-out-stream stream (lisp-stream-misc :charpos) (stream-line-column)))
676 ram 1.1
677     (defun line-length (&optional (stream *standard-output*))
678     "Returns the number of characters that will fit on a line of output on the
679     given Stream, or Nil if that information is not available."
680 dtc 1.32 (with-out-stream stream (lisp-stream-misc :line-length)
681     (stream-line-length)))
682 ram 1.1
683     (defun finish-output (&optional (stream *standard-output*))
684 dtc 1.28 "Attempts to ensure that all output sent to the Stream has reached its
685 ram 1.1 destination, and only then returns."
686 toy 1.52 (declare (type streamlike stream))
687 dtc 1.32 (with-out-stream stream (lisp-stream-misc :finish-output)
688     (stream-finish-output))
689 ram 1.1 nil)
690    
691     (defun force-output (&optional (stream *standard-output*))
692     "Attempts to force any buffered output to be sent."
693 toy 1.52 (declare (type streamlike stream))
694 dtc 1.32 (with-out-stream stream (lisp-stream-misc :force-output)
695     (stream-force-output))
696 ram 1.1 nil)
697    
698     (defun clear-output (&optional (stream *standard-output*))
699     "Clears the given output Stream."
700 toy 1.52 (declare (type streamlike stream))
701 dtc 1.32 (with-out-stream stream (lisp-stream-misc :clear-output)
702     (stream-force-output))
703 ram 1.1 nil)
704    
705     (defun write-byte (integer stream)
706     "Outputs the Integer to the binary Stream."
707 toy 1.52 (declare (type stream stream))
708 dtc 1.39 (with-out-stream stream (lisp-stream-bout integer)
709     (stream-write-byte integer))
710 ram 1.1 integer)
711    
712 dtc 1.33
713     ;;; Stream-misc-dispatch
714     ;;;
715     ;;; Called from lisp-steam routines that encapsulate CLOS streams to
716     ;;; handle the misc routines and dispatch to the appropriate Gray
717     ;;; stream functions.
718     ;;;
719     (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
720     (declare (type fundamental-stream stream)
721     (ignore arg2))
722     (case operation
723     (:listen
724     ;; Return true is input available, :eof for eof-of-file, otherwise Nil.
725     (let ((char (stream-read-char-no-hang stream)))
726     (when (characterp char)
727     (stream-unread-char stream char))
728     char))
729     (:unread
730     (stream-unread-char stream arg1))
731     (:close
732     (close stream))
733     (:clear-input
734     (stream-clear-input stream))
735     (:force-output
736     (stream-force-output stream))
737     (:finish-output
738     (stream-finish-output stream))
739     (:element-type
740     (stream-element-type stream))
741     (:interactive-p
742     (interactive-stream-p stream))
743     (:line-length
744     (stream-line-length stream))
745     (:charpos
746     (stream-line-column stream))
747     (:file-length
748     (file-length stream))
749     (:file-position
750     (file-position stream arg1))))
751    
752    
753 ram 1.1 ;;;; Broadcast streams:
754    
755 dtc 1.30 (defstruct (broadcast-stream (:include lisp-stream
756 ram 1.1 (out #'broadcast-out)
757     (bout #'broadcast-bout)
758     (sout #'broadcast-sout)
759     (misc #'broadcast-misc))
760     (:print-function %print-broadcast-stream)
761 toy 1.52 (:constructor %make-broadcast-stream (&rest streams)))
762 ram 1.1 ;; This is a list of all the streams we broadcast to.
763 ram 1.14 (streams () :type list :read-only t))
764 ram 1.1
765 toy 1.52 (defun make-broadcast-stream (&rest streams)
766     "Returns an output stream which sends its output to all of the given
767     streams."
768     (dolist (s streams)
769     (unless (output-stream-p s)
770     (ill-out-any s)))
771    
772     (apply #'%make-broadcast-stream streams))
773 ram 1.1
774     (defun %print-broadcast-stream (s stream d)
775     (declare (ignore s d))
776     (write-string "#<Broadcast Stream>" stream))
777    
778 dtc 1.33 (macrolet ((out-fun (fun method stream-method &rest args)
779 ram 1.1 `(defun ,fun (stream ,@args)
780     (dolist (stream (broadcast-stream-streams stream))
781 dtc 1.33 (if (lisp-stream-p stream)
782     (funcall (,method stream) stream ,@args)
783     (,stream-method stream ,@args))))))
784     (out-fun broadcast-out lisp-stream-out stream-write-char char)
785     (out-fun broadcast-bout lisp-stream-bout stream-write-byte byte)
786     (out-fun broadcast-sout lisp-stream-sout stream-write-string
787     string start end))
788 ram 1.1
789     (defun broadcast-misc (stream operation &optional arg1 arg2)
790     (let ((streams (broadcast-stream-streams stream)))
791     (case operation
792     (:charpos
793     (dolist (stream streams)
794 dtc 1.33 (let ((charpos (charpos stream)))
795 ram 1.1 (if charpos (return charpos)))))
796     (:line-length
797     (let ((min nil))
798     (dolist (stream streams min)
799 dtc 1.33 (let ((res (line-length stream)))
800 ram 1.1 (when res (setq min (if min (min res min) res)))))))
801     (:element-type
802     (let (res)
803     (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
804 dtc 1.33 (pushnew (stream-element-type stream) res :test #'equal))))
805 wlott 1.25 (:close)
806 ram 1.1 (t
807     (let ((res nil))
808     (dolist (stream streams res)
809 dtc 1.33 (setq res
810     (if (lisp-stream-p stream)
811     (funcall (lisp-stream-misc stream) stream operation
812     arg1 arg2)
813     (stream-misc-dispatch stream operation arg1 arg2)))))))))
814    
815 ram 1.1
816     ;;;; Synonym Streams:
817    
818 dtc 1.30 (defstruct (synonym-stream (:include lisp-stream
819 ram 1.1 (in #'synonym-in)
820     (bin #'synonym-bin)
821     (n-bin #'synonym-n-bin)
822     (out #'synonym-out)
823     (bout #'synonym-bout)
824     (sout #'synonym-sout)
825     (misc #'synonym-misc))
826     (:print-function %print-synonym-stream)
827     (:constructor make-synonym-stream (symbol)))
828     ;; This is the symbol, the value of which is the stream we are synonym to.
829 wlott 1.15 (symbol nil :type symbol :read-only t))
830 ram 1.1
831     (defun %print-synonym-stream (s stream d)
832     (declare (ignore d))
833     (format stream "#<Synonym Stream to ~S>" (synonym-stream-symbol s)))
834    
835     (setf (documentation 'make-synonym-stream 'function)
836     "Returns a stream which performs its operations on the stream which is the
837     value of the dynamic variable named by Symbol.")
838    
839     ;;; The output simple output methods just call the corresponding method
840     ;;; in the synonymed stream.
841     ;;;
842 dtc 1.33 (macrolet ((out-fun (name slot stream-method &rest args)
843 ram 1.1 `(defun ,name (stream ,@args)
844 ram 1.11 (declare (optimize (safety 1)))
845 ram 1.1 (let ((syn (symbol-value (synonym-stream-symbol stream))))
846 dtc 1.33 (if (lisp-stream-p syn)
847     (funcall (,slot syn) syn ,@args)
848     (,stream-method syn ,@args))))))
849     (out-fun synonym-out lisp-stream-out stream-write-char ch)
850     (out-fun synonym-bout lisp-stream-bout stream-write-byte n)
851     (out-fun synonym-sout lisp-stream-sout stream-write-string string start end))
852 ram 1.1
853    
854     ;;; Bind synonym stream to this so that SPIO can turn on the right frob in
855     ;;; the icon when we are in a terminal input wait.
856     ;;;
857     (defvar *previous-stream* nil)
858    
859     ;;; For the input methods, we just call the corresponding function on the
860     ;;; synonymed stream. These functions deal with getting input out of
861     ;;; the In-Buffer if there is any.
862     ;;;
863     (macrolet ((in-fun (name fun &rest args)
864     `(defun ,name (stream ,@args)
865 ram 1.11 (declare (optimize (safety 1)))
866 ram 1.1 (let ((*previous-stream* stream))
867     (,fun (symbol-value (synonym-stream-symbol stream)) ,@args)))))
868     (in-fun synonym-in read-char eof-errorp eof-value)
869     (in-fun synonym-bin read-byte eof-errorp eof-value)
870     (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-errorp))
871    
872    
873     ;;; Synonym-Misc -- Internal
874     ;;;
875     ;;; We have to special-case the operations which could look at stuff in
876     ;;; the in-buffer.
877     ;;;
878     (defun synonym-misc (stream operation &optional arg1 arg2)
879 ram 1.11 (declare (optimize (safety 1)))
880 ram 1.1 (let ((syn (symbol-value (synonym-stream-symbol stream)))
881     (*previous-stream* stream))
882 dtc 1.33 (if (lisp-stream-p syn)
883     (case operation
884     (:listen (or (/= (the fixnum (lisp-stream-in-index syn))
885     in-buffer-length)
886     (funcall (lisp-stream-misc syn) syn :listen)))
887 dtc 1.42 (:clear-input (clear-input syn))
888     (:unread (unread-char arg1 syn))
889 dtc 1.33 (t
890     (funcall (lisp-stream-misc syn) syn operation arg1 arg2)))
891     (stream-misc-dispatch syn operation arg1 arg2))))
892 ram 1.1
893     ;;;; Two-Way streams:
894    
895     (defstruct (two-way-stream
896 dtc 1.30 (:include lisp-stream
897 ram 1.1 (in #'two-way-in)
898     (bin #'two-way-bin)
899     (n-bin #'two-way-n-bin)
900     (out #'two-way-out)
901     (bout #'two-way-bout)
902     (sout #'two-way-sout)
903     (misc #'two-way-misc))
904     (:print-function %print-two-way-stream)
905 toy 1.52 (:constructor %make-two-way-stream (input-stream output-stream)))
906 ram 1.1 ;; We read from this stream...
907 ram 1.14 (input-stream (required-argument) :type stream :read-only t)
908 ram 1.1 ;; And write to this one
909 ram 1.14 (output-stream (required-argument) :type stream :read-only t))
910 ram 1.1
911     (defun %print-two-way-stream (s stream d)
912     (declare (ignore d))
913     (format stream "#<Two-Way Stream, Input = ~S, Output = ~S>"
914     (two-way-stream-input-stream s)
915     (two-way-stream-output-stream s)))
916    
917 toy 1.52 (defun make-two-way-stream (input-stream output-stream)
918 ram 1.1 "Returns a bidirectional stream which gets its input from Input-Stream and
919 toy 1.52 sends its output to Output-Stream."
920     (unless (input-stream-p input-stream)
921     (ill-in-any input-stream))
922     (unless (output-stream-p output-stream)
923     (ill-out-any output-stream))
924    
925     (%make-two-way-stream input-stream output-stream))
926 ram 1.1
927 dtc 1.33 (macrolet ((out-fun (name slot stream-method &rest args)
928 ram 1.1 `(defun ,name (stream ,@args)
929     (let ((syn (two-way-stream-output-stream stream)))
930 dtc 1.33 (if (lisp-stream-p syn)
931     (funcall (,slot syn) syn ,@args)
932     (,stream-method syn ,@args))))))
933     (out-fun two-way-out lisp-stream-out stream-write-char ch)
934     (out-fun two-way-bout lisp-stream-bout stream-write-byte n)
935     (out-fun two-way-sout lisp-stream-sout stream-write-string string start end))
936 ram 1.1
937     (macrolet ((in-fun (name fun &rest args)
938     `(defun ,name (stream ,@args)
939 ram 1.10 (force-output (two-way-stream-output-stream stream))
940 ram 1.1 (,fun (two-way-stream-input-stream stream) ,@args))))
941     (in-fun two-way-in read-char eof-errorp eof-value)
942     (in-fun two-way-bin read-byte eof-errorp eof-value)
943     (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-errorp))
944    
945     (defun two-way-misc (stream operation &optional arg1 arg2)
946     (let* ((in (two-way-stream-input-stream stream))
947     (out (two-way-stream-output-stream stream))
948 dtc 1.33 (in-lisp-stream-p (lisp-stream-p in))
949     (out-lisp-stream-p (lisp-stream-p out)))
950 ram 1.1 (case operation
951 dtc 1.33 (:listen
952     (if in-lisp-stream-p
953     (or (/= (the fixnum (lisp-stream-in-index in)) in-buffer-length)
954     (funcall (lisp-stream-misc in) in :listen))
955     (stream-listen in)))
956 ram 1.1 ((:finish-output :force-output :clear-output)
957 dtc 1.33 (if out-lisp-stream-p
958     (funcall (lisp-stream-misc out) out operation arg1 arg2)
959     (stream-misc-dispatch out operation arg1 arg2)))
960 dtc 1.42 (:clear-input (clear-input in))
961     (:unread (unread-char arg1 in))
962 ram 1.1 (:element-type
963 dtc 1.33 (let ((in-type (stream-element-type in))
964     (out-type (stream-element-type out)))
965 ram 1.1 (if (equal in-type out-type)
966     in-type `(and ,in-type ,out-type))))
967 dtc 1.33 (:close
968 ram 1.1 (set-closed-flame stream))
969     (t
970 dtc 1.33 (or (if in-lisp-stream-p
971     (funcall (lisp-stream-misc in) in operation arg1 arg2)
972     (stream-misc-dispatch in operation arg1 arg2))
973     (if out-lisp-stream-p
974     (funcall (lisp-stream-misc out) out operation arg1 arg2)
975     (stream-misc-dispatch out operation arg1 arg2)))))))
976    
977 ram 1.1
978     ;;;; Concatenated Streams:
979    
980     (defstruct (concatenated-stream
981 dtc 1.30 (:include lisp-stream
982 ram 1.1 (in #'concatenated-in)
983     (bin #'concatenated-bin)
984 pw 1.46 (n-bin #'concatenated-n-bin)
985 ram 1.1 (misc #'concatenated-misc))
986     (:print-function %print-concatenated-stream)
987 toy 1.52 (:constructor %make-concatenated-stream (&rest streams)))
988 dtc 1.40 ;; This is a list of all the streams. The car of this is the stream
989     ;; we are reading from now.
990     (streams nil :type list))
991 ram 1.1
992     (defun %print-concatenated-stream (s stream d)
993     (declare (ignore d))
994     (format stream "#<Concatenated Stream, Streams = ~S>"
995     (concatenated-stream-streams s)))
996    
997 toy 1.52 (defun make-concatenated-stream (&rest streams)
998 ram 1.1 "Returns a stream which takes its input from each of the Streams in turn,
999 toy 1.52 going on to the next at EOF."
1000     (dolist (s streams)
1001     (unless (input-stream-p s)
1002     (ill-in-any s)))
1003     (apply #'%make-concatenated-stream streams))
1004 ram 1.1
1005     (macrolet ((in-fun (name fun)
1006     `(defun ,name (stream eof-errorp eof-value)
1007 dtc 1.40 (do ((current (concatenated-stream-streams stream)
1008     (cdr current)))
1009 ram 1.1 ((null current)
1010     (eof-or-lose stream eof-errorp eof-value))
1011     (let* ((stream (car current))
1012     (result (,fun stream nil nil)))
1013     (when result (return result)))
1014 dtc 1.40 (setf (concatenated-stream-streams stream) (cdr current))))))
1015 ram 1.1 (in-fun concatenated-in read-char)
1016     (in-fun concatenated-bin read-byte))
1017 pw 1.46
1018     (defun concatenated-n-bin (stream buffer start numbytes eof-errorp)
1019     (do ((current (concatenated-stream-streams stream) (cdr current))
1020     (current-start start)
1021     (remaining-bytes numbytes))
1022     ((null current)
1023     (if eof-errorp
1024     (error 'end-of-file :stream stream)
1025     (- numbytes remaining-bytes)))
1026     (let* ((stream (car current))
1027     (bytes-read (read-n-bytes stream buffer current-start
1028     remaining-bytes nil)))
1029     (incf current-start bytes-read)
1030     (decf remaining-bytes bytes-read)
1031     (when (zerop remaining-bytes) (return numbytes)))
1032     (setf (concatenated-stream-streams stream) (cdr current))))
1033 ram 1.1
1034     (defun concatenated-misc (stream operation &optional arg1 arg2)
1035 dtc 1.40 (let ((left (concatenated-stream-streams stream)))
1036 ram 1.18 (when left
1037 dtc 1.33 (let* ((current (car left)))
1038 ram 1.18 (case operation
1039     (:listen
1040     (loop
1041 dtc 1.33 (let ((stuff (if (lisp-stream-p current)
1042     (funcall (lisp-stream-misc current) current
1043     :listen)
1044     (stream-misc-dispatch current :listen))))
1045 ram 1.18 (cond ((eq stuff :eof)
1046     ;; Advance current, and try again.
1047 dtc 1.40 (pop (concatenated-stream-streams stream))
1048 ram 1.18 (setf current
1049 dtc 1.40 (car (concatenated-stream-streams stream)))
1050 ram 1.18 (unless current
1051     ;; No further streams. EOF.
1052 dtc 1.33 (return :eof)))
1053 ram 1.18 (stuff
1054     ;; Stuff's available.
1055     (return t))
1056     (t
1057     ;; Nothing available yet.
1058     (return nil))))))
1059     (:close
1060     (set-closed-flame stream))
1061 dtc 1.42 (:clear-input (clear-input current))
1062     (:unread (unread-char arg1 current))
1063 ram 1.18 (t
1064 dtc 1.33 (if (lisp-stream-p current)
1065     (funcall (lisp-stream-misc current) current operation arg1 arg2)
1066     (stream-misc-dispatch current operation arg1 arg2))))))))
1067    
1068 ram 1.1
1069     ;;;; Echo Streams:
1070    
1071     (defstruct (echo-stream
1072     (:include two-way-stream
1073     (in #'echo-in)
1074     (bin #'echo-bin)
1075     (misc #'echo-misc)
1076 toy 1.49 (n-bin #'ill-n-bin))
1077 ram 1.1 (:print-function %print-echo-stream)
1078 toy 1.52 (:constructor %make-echo-stream (input-stream output-stream)))
1079 wlott 1.8 unread-stuff)
1080 ram 1.1
1081 toy 1.52 (defun make-echo-stream (input-stream output-stream)
1082     "Returns an echo stream that takes input from Input-stream and sends
1083     output to Output-stream"
1084     (unless (input-stream-p input-stream)
1085     (ill-in-any input-stream))
1086     (unless (output-stream-p output-stream)
1087     (ill-out-any output-stream))
1088     (%make-echo-stream input-stream output-stream))
1089 ram 1.1
1090 dtc 1.38 (macrolet ((in-fun (name fun out-slot stream-method)
1091     `(defun ,name (stream eof-errorp eof-value)
1092 wlott 1.8 (or (pop (echo-stream-unread-stuff stream))
1093     (let* ((in (echo-stream-input-stream stream))
1094     (out (echo-stream-output-stream stream))
1095 dtc 1.38 (result (,fun in nil :eof)))
1096     (cond ((eq result :eof)
1097     (eof-or-lose stream eof-errorp eof-value))
1098     (t
1099     (if (lisp-stream-p out)
1100     (funcall (,out-slot out) out result)
1101     (,stream-method out result))
1102     result)))))))
1103     (in-fun echo-in read-char lisp-stream-out stream-write-char)
1104     (in-fun echo-bin read-byte lisp-stream-bout stream-write-byte))
1105 ram 1.1
1106 toy 1.56
1107 ram 1.1 (defun echo-misc (stream operation &optional arg1 arg2)
1108     (let* ((in (two-way-stream-input-stream stream))
1109 dtc 1.33 (out (two-way-stream-output-stream stream)))
1110 ram 1.1 (case operation
1111 dtc 1.33 (:listen
1112     (or (not (null (echo-stream-unread-stuff stream)))
1113     (if (lisp-stream-p in)
1114     (or (/= (the fixnum (lisp-stream-in-index in)) in-buffer-length)
1115     (funcall (lisp-stream-misc in) in :listen))
1116     (stream-misc-dispatch in :listen))))
1117 wlott 1.8 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1118 ram 1.1 (:element-type
1119 dtc 1.33 (let ((in-type (stream-element-type in))
1120     (out-type (stream-element-type out)))
1121 ram 1.1 (if (equal in-type out-type)
1122     in-type `(and ,in-type ,out-type))))
1123     (:close
1124     (set-closed-flame stream))
1125 toy 1.56 (:peek-char
1126     ;; For the special case of peeking into an echo-stream
1127     ;; arg1 is peek-type, arg2 is (eof-errorp eof-value)
1128     ;; returns peeked-char, eof-value, or errors end-of-file
1129     (let ((unread-char-p nil))
1130     (destructuring-bind (eof-errorp eof-value)
1131     arg2
1132     (flet ((outfn (c)
1133     (unless unread-char-p
1134     (if (lisp-stream-p out)
1135     (funcall (lisp-stream-out out) out c)
1136     ;; gray-stream
1137     (stream-write-char out c))))
1138     (infn ()
1139     ;; Obtain input from unread buffer or input stream,
1140     ;; and set the flag appropriately.
1141     (cond ((not (null (echo-stream-unread-stuff stream)))
1142     (setf unread-char-p t)
1143     (pop (echo-stream-unread-stuff stream)))
1144     (t
1145     (setf unread-char-p nil)
1146     (read-char in eof-errorp eof-value)))))
1147     (generalized-peeking-mechanism
1148     arg1 eof-value char
1149     (infn)
1150     (unread-char char in)
1151     (outfn char))))))
1152 ram 1.1 (t
1153 dtc 1.33 (or (if (lisp-stream-p in)
1154     (funcall (lisp-stream-misc in) in operation arg1 arg2)
1155     (stream-misc-dispatch in operation arg1 arg2))
1156     (if (lisp-stream-p out)
1157     (funcall (lisp-stream-misc out) out operation arg1 arg2)
1158     (stream-misc-dispatch out operation arg1 arg2)))))))
1159 ram 1.1
1160     (defun %print-echo-stream (s stream d)
1161     (declare (ignore d))
1162     (format stream "#<Echo Stream, Input = ~S, Output = ~S>"
1163     (two-way-stream-input-stream s)
1164     (two-way-stream-output-stream s)))
1165    
1166     (setf (documentation 'make-echo-stream 'function)
1167     "Returns a bidirectional stream which gets its input from Input-Stream and
1168     sends its output to Output-Stream. In addition, all input is echoed to
1169     the output stream")
1170    
1171     ;;;; String Input Streams:
1172    
1173     (defstruct (string-input-stream
1174 dtc 1.30 (:include lisp-stream
1175 ram 1.21 (in #'string-inch)
1176     (bin #'string-binch)
1177     (n-bin #'string-stream-read-n-bytes)
1178     (misc #'string-in-misc))
1179     (:print-function %print-string-input-stream)
1180     ;(:constructor nil)
1181     (:constructor internal-make-string-input-stream
1182     (string current end)))
1183 ram 1.1 (string nil :type simple-string)
1184 dtc 1.31 (current nil :type index)
1185     (end nil :type index))
1186 ram 1.1
1187     (defun %print-string-input-stream (s stream d)
1188     (declare (ignore s d))
1189     (write-string "#<String-Input Stream>" stream))
1190    
1191     (defun string-inch (stream eof-errorp eof-value)
1192     (let ((string (string-input-stream-string stream))
1193     (index (string-input-stream-current stream)))
1194     (declare (simple-string string) (fixnum index))
1195 dtc 1.31 (cond ((= index (the index (string-input-stream-end stream)))
1196 ram 1.1 (eof-or-lose stream eof-errorp eof-value))
1197     (t
1198     (setf (string-input-stream-current stream) (1+ index))
1199     (aref string index)))))
1200 ram 1.21
1201     (defun string-binch (stream eof-errorp eof-value)
1202     (let ((string (string-input-stream-string stream))
1203     (index (string-input-stream-current stream)))
1204 dtc 1.31 (declare (simple-string string)
1205     (type index index))
1206     (cond ((= index (the index (string-input-stream-end stream)))
1207 ram 1.21 (eof-or-lose stream eof-errorp eof-value))
1208     (t
1209     (setf (string-input-stream-current stream) (1+ index))
1210     (char-code (aref string index))))))
1211    
1212     (defun string-stream-read-n-bytes (stream buffer start requested eof-errorp)
1213     (declare (type string-input-stream stream)
1214     (type index start requested))
1215 dtc 1.31 (let* ((string (string-input-stream-string stream))
1216     (index (string-input-stream-current stream))
1217     (available (- (string-input-stream-end stream) index))
1218     (copy (min available requested)))
1219     (declare (simple-string string)
1220     (type index index available copy))
1221     (when (plusp copy)
1222     (setf (string-input-stream-current stream)
1223     (truly-the index (+ index copy)))
1224     (system:without-gcing
1225     (system-area-copy (vector-sap string)
1226     (* index vm:byte-bits)
1227     (if (typep buffer 'system-area-pointer)
1228     buffer
1229     (vector-sap buffer))
1230     (* start vm:byte-bits)
1231     (* copy vm:byte-bits))))
1232     (if (and (> requested copy) eof-errorp)
1233     (error 'end-of-file :stream stream)
1234     copy)))
1235 ram 1.1
1236     (defun string-in-misc (stream operation &optional arg1 arg2)
1237 ram 1.18 (declare (ignore arg2))
1238 ram 1.1 (case operation
1239 ram 1.2 (:file-position
1240 ram 1.5 (if arg1
1241     (setf (string-input-stream-current stream) arg1)
1242     (string-input-stream-current stream)))
1243     (:file-length (length (string-input-stream-string stream)))
1244 ram 1.1 (:unread (decf (string-input-stream-current stream)))
1245 ram 1.10 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1246     (the fixnum (string-input-stream-end stream)))
1247     :eof))
1248 pw 1.27 (:element-type 'base-char)))
1249 ram 1.1
1250     (defun make-string-input-stream (string &optional
1251     (start 0) (end (length string)))
1252     "Returns an input stream which will supply the characters of String between
1253     Start and End in order."
1254 wlott 1.17 (declare (type string string)
1255     (type index start)
1256 ram 1.18 (type (or index null) end))
1257 wlott 1.17 (internal-make-string-input-stream (coerce string 'simple-string)
1258     start end))
1259 ram 1.1
1260     ;;;; String Output Streams:
1261    
1262     (defstruct (string-output-stream
1263 dtc 1.30 (:include lisp-stream
1264 ram 1.1 (out #'string-ouch)
1265     (sout #'string-sout)
1266     (misc #'string-out-misc))
1267     (:print-function %print-string-output-stream)
1268     (:constructor make-string-output-stream ()))
1269     ;; The string we throw stuff in.
1270     (string (make-string 40) :type simple-string)
1271     ;; Index of the next location to use.
1272     (index 0 :type fixnum))
1273    
1274     (defun %print-string-output-stream (s stream d)
1275     (declare (ignore s d))
1276     (write-string "#<String-Output Stream>" stream))
1277    
1278     (setf (documentation 'make-string-output-stream 'function)
1279     "Returns an Output stream which will accumulate all output given it for
1280     the benefit of the function Get-Output-Stream-String.")
1281    
1282     (defun string-ouch (stream character)
1283     (let ((current (string-output-stream-index stream))
1284     (workspace (string-output-stream-string stream)))
1285     (declare (simple-string workspace) (fixnum current))
1286     (if (= current (the fixnum (length workspace)))
1287     (let ((new-workspace (make-string (* current 2))))
1288 wlott 1.17 (replace new-workspace workspace)
1289 ram 1.1 (setf (aref new-workspace current) character)
1290     (setf (string-output-stream-string stream) new-workspace))
1291     (setf (aref workspace current) character))
1292     (setf (string-output-stream-index stream) (1+ current))))
1293    
1294     (defun string-sout (stream string start end)
1295     (declare (simple-string string) (fixnum start end))
1296     (let* ((current (string-output-stream-index stream))
1297     (length (- end start))
1298     (dst-end (+ length current))
1299     (workspace (string-output-stream-string stream)))
1300     (declare (simple-string workspace)
1301     (fixnum current length dst-end))
1302     (if (> dst-end (the fixnum (length workspace)))
1303     (let ((new-workspace (make-string (+ (* current 2) length))))
1304 wlott 1.17 (replace new-workspace workspace :end2 current)
1305     (replace new-workspace string
1306     :start1 current :end1 dst-end
1307     :start2 start :end2 end)
1308 ram 1.1 (setf (string-output-stream-string stream) new-workspace))
1309 wlott 1.17 (replace workspace string
1310     :start1 current :end1 dst-end
1311     :start2 start :end2 end))
1312 ram 1.1 (setf (string-output-stream-index stream) dst-end)))
1313    
1314     (defun string-out-misc (stream operation &optional arg1 arg2)
1315 ram 1.3 (declare (ignore arg2))
1316 ram 1.1 (case operation
1317 ram 1.2 (:file-position
1318     (if (null arg1)
1319 ram 1.3 (string-output-stream-index stream)))
1320 ram 1.1 (:charpos
1321     (do ((index (1- (the fixnum (string-output-stream-index stream)))
1322     (1- index))
1323     (count 0 (1+ count))
1324     (string (string-output-stream-string stream)))
1325     ((< index 0) count)
1326     (declare (simple-string string)
1327     (fixnum index count))
1328     (if (char= (schar string index) #\newline)
1329     (return count))))
1330 pw 1.27 (:element-type 'base-char)))
1331 ram 1.1
1332     (defun get-output-stream-string (stream)
1333     "Returns a string of all the characters sent to a stream made by
1334     Make-String-Output-Stream since the last call to this function."
1335 wlott 1.17 (declare (type string-output-stream stream))
1336     (let* ((length (string-output-stream-index stream))
1337     (result (make-string length)))
1338     (replace result (string-output-stream-string stream))
1339     (setf (string-output-stream-index stream) 0)
1340     result))
1341 ram 1.1
1342     (defun dump-output-stream-string (in-stream out-stream)
1343     "Dumps the characters buffer up in the In-Stream to the Out-Stream as
1344     Get-Output-Stream-String would return them."
1345 dtc 1.30 (write-string* (string-output-stream-string in-stream) out-stream
1346     0 (string-output-stream-index in-stream))
1347 ram 1.1 (setf (string-output-stream-index in-stream) 0))
1348    
1349     ;;;; Fill-pointer streams:
1350     ;;;
1351     ;;; Fill pointer string output streams are not explicitly mentioned in
1352     ;;; the CLM, but they are required for the implementation of With-Output-To-String.
1353    
1354     (defstruct (fill-pointer-output-stream
1355 dtc 1.30 (:include lisp-stream
1356 ram 1.1 (out #'fill-pointer-ouch)
1357     (sout #'fill-pointer-sout)
1358     (misc #'fill-pointer-misc))
1359     (:print-function
1360     (lambda (s stream d)
1361     (declare (ignore s d))
1362     (write-string "#<Fill-Pointer String Output Stream>" stream)))
1363     (:constructor make-fill-pointer-output-stream (string)))
1364     ;; The string we throw stuff in.
1365     string)
1366    
1367    
1368     (defun fill-pointer-ouch (stream character)
1369     (let* ((buffer (fill-pointer-output-stream-string stream))
1370 wlott 1.4 (current (fill-pointer buffer))
1371 ram 1.1 (current+1 (1+ current)))
1372     (declare (fixnum current))
1373     (with-array-data ((workspace buffer) (start) (end))
1374     (declare (simple-string workspace))
1375     (let ((offset-current (+ start current)))
1376     (declare (fixnum offset-current))
1377     (if (= offset-current end)
1378 pw 1.48 (let* ((new-length (if (zerop current) 1 (* current 2)))
1379 ram 1.1 (new-workspace (make-string new-length)))
1380     (declare (simple-string new-workspace))
1381     (%primitive byte-blt workspace start new-workspace 0 current)
1382     (setf workspace new-workspace)
1383     (setf offset-current current)
1384     (set-array-header buffer workspace new-length
1385     current+1 0 new-length nil))
1386 wlott 1.4 (setf (fill-pointer buffer) current+1))
1387 ram 1.1 (setf (schar workspace offset-current) character)))
1388     current+1))
1389    
1390    
1391     (defun fill-pointer-sout (stream string start end)
1392     (declare (simple-string string) (fixnum start end))
1393     (let* ((buffer (fill-pointer-output-stream-string stream))
1394 wlott 1.4 (current (fill-pointer buffer))
1395 ram 1.1 (string-len (- end start))
1396     (dst-end (+ string-len current)))
1397     (declare (fixnum current dst-end string-len))
1398     (with-array-data ((workspace buffer) (dst-start) (dst-length))
1399     (declare (simple-string workspace))
1400     (let ((offset-dst-end (+ dst-start dst-end))
1401     (offset-current (+ dst-start current)))
1402     (declare (fixnum offset-dst-end offset-current))
1403     (if (> offset-dst-end dst-length)
1404     (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1405     (new-workspace (make-string new-length)))
1406     (declare (simple-string new-workspace))
1407     (%primitive byte-blt workspace dst-start new-workspace 0 current)
1408     (setf workspace new-workspace)
1409     (setf offset-current current)
1410     (setf offset-dst-end dst-end)
1411     (set-array-header buffer workspace new-length
1412     dst-end 0 new-length nil))
1413 wlott 1.4 (setf (fill-pointer buffer) dst-end))
1414 ram 1.1 (%primitive byte-blt string start
1415     workspace offset-current offset-dst-end)))
1416     dst-end))
1417    
1418    
1419     (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1420     (declare (ignore arg1 arg2))
1421     (case operation
1422     (:charpos
1423     (let* ((buffer (fill-pointer-output-stream-string stream))
1424 wlott 1.4 (current (fill-pointer buffer)))
1425 ram 1.1 (with-array-data ((string buffer) (start) (end current))
1426     (declare (simple-string string) (ignore start))
1427     (let ((found (position #\newline string :test #'char=
1428     :end end :from-end t)))
1429     (if found
1430     (- end (the fixnum found))
1431     current)))))
1432 pw 1.27 (:element-type 'base-char)))
1433 ram 1.1
1434     ;;;; Indenting streams:
1435    
1436 dtc 1.30 (defstruct (indenting-stream (:include lisp-stream
1437 ram 1.1 (out #'indenting-out)
1438     (sout #'indenting-sout)
1439     (misc #'indenting-misc))
1440     (:print-function %print-indenting-stream)
1441     (:constructor make-indenting-stream (stream)))
1442     ;; The stream we're based on:
1443     stream
1444     ;; How much we indent on each line:
1445     (indentation 0))
1446    
1447     (setf (documentation 'make-indenting-stream 'function)
1448 pw 1.47 "Returns an output stream which indents its output by some amount.")
1449 ram 1.1
1450     (defun %print-indenting-stream (s stream d)
1451     (declare (ignore s d))
1452     (write-string "#<Indenting Stream>" stream))
1453    
1454     ;;; Indenting-Indent writes the right number of spaces needed to indent output on
1455     ;;; the given Stream based on the specified Sub-Stream.
1456    
1457     (defmacro indenting-indent (stream sub-stream)
1458     `(do ((i 0 (+ i 60))
1459     (indentation (indenting-stream-indentation ,stream)))
1460     ((>= i indentation))
1461 dtc 1.30 (write-string*
1462     " "
1463     ,sub-stream 0 (min 60 (- indentation i)))))
1464 ram 1.1
1465     ;;; Indenting-Out writes a character to an indenting stream.
1466    
1467     (defun indenting-out (stream char)
1468     (let ((sub-stream (indenting-stream-stream stream)))
1469 dtc 1.30 (write-char char sub-stream)
1470 ram 1.1 (if (char= char #\newline)
1471     (indenting-indent stream sub-stream))))
1472    
1473     ;;; Indenting-Sout writes a string to an indenting stream.
1474    
1475     (defun indenting-sout (stream string start end)
1476     (declare (simple-string string) (fixnum start end))
1477     (do ((i start)
1478     (sub-stream (indenting-stream-stream stream)))
1479     ((= i end))
1480     (let ((newline (position #\newline string :start i :end end)))
1481     (cond (newline
1482 dtc 1.30 (write-string* string sub-stream i (1+ newline))
1483 ram 1.1 (indenting-indent stream sub-stream)
1484     (setq i (+ newline 1)))
1485     (t
1486 dtc 1.30 (write-string* string sub-stream i end)
1487 ram 1.1 (setq i end))))))
1488    
1489     ;;; Indenting-Misc just treats just the :Line-Length message differently.
1490     ;;; Indenting-Charpos says the charpos is the charpos of the base stream minus
1491     ;;; the stream's indentation.
1492    
1493     (defun indenting-misc (stream operation &optional arg1 arg2)
1494 dtc 1.30 (let ((sub-stream (indenting-stream-stream stream)))
1495 dtc 1.33 (if (lisp-stream-p sub-stream)
1496     (let ((method (lisp-stream-misc sub-stream)))
1497     (case operation
1498     (:line-length
1499     (let ((line-length (funcall method sub-stream operation)))
1500     (if line-length
1501     (- line-length (indenting-stream-indentation stream)))))
1502     (:charpos
1503     (let ((charpos (funcall method sub-stream operation)))
1504     (if charpos
1505     (- charpos (indenting-stream-indentation stream)))))
1506     (t
1507     (funcall method sub-stream operation arg1 arg2))))
1508     ;; Fundamental-stream.
1509     (case operation
1510     (:line-length
1511     (let ((line-length (stream-line-length sub-stream)))
1512     (if line-length
1513     (- line-length (indenting-stream-indentation stream)))))
1514     (:charpos
1515     (let ((charpos (stream-line-column sub-stream)))
1516     (if charpos
1517     (- charpos (indenting-stream-indentation stream)))))
1518     (t
1519     (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1520 dtc 1.30
1521 ram 1.1
1522 pw 1.45 (declaim (maybe-inline read-char unread-char read-byte listen))
1523 wlott 1.13
1524    
1525    
1526     ;;;; Case frobbing streams, used by format ~(...~).
1527    
1528     (defstruct (case-frob-stream
1529 dtc 1.30 (:include lisp-stream
1530 wlott 1.13 (:misc #'case-frob-misc))
1531     (:constructor %make-case-frob-stream (target out sout)))
1532     (target (required-argument) :type stream))
1533    
1534     (defun make-case-frob-stream (target kind)
1535     "Returns a stream that sends all output to the stream TARGET, but modifies
1536     the case of letters, depending on KIND, which should be one of:
1537     :upcase - convert to upper case.
1538     :downcase - convert to lower case.
1539     :capitalize - convert the first letter of words to upper case and the
1540     rest of the word to lower case.
1541     :capitalize-first - convert the first letter of the first word to upper
1542     case and everything else to lower case."
1543     (declare (type stream target)
1544     (type (member :upcase :downcase :capitalize :capitalize-first)
1545     kind)
1546     (values stream))
1547     (if (case-frob-stream-p target)
1548     ;; If we are going to be writing to a stream that already does case
1549     ;; frobbing, why bother frobbing the case just so it can frob it
1550     ;; again?
1551     target
1552     (multiple-value-bind
1553     (out sout)
1554     (ecase kind
1555     (:upcase
1556     (values #'case-frob-upcase-out
1557     #'case-frob-upcase-sout))
1558     (:downcase
1559     (values #'case-frob-downcase-out
1560     #'case-frob-downcase-sout))
1561     (:capitalize
1562     (values #'case-frob-capitalize-out
1563     #'case-frob-capitalize-sout))
1564     (:capitalize-first
1565     (values #'case-frob-capitalize-first-out
1566     #'case-frob-capitalize-first-sout)))
1567     (%make-case-frob-stream target out sout))))
1568    
1569     (defun case-frob-misc (stream op &optional arg1 arg2)
1570     (declare (type case-frob-stream stream))
1571     (case op
1572     (:close)
1573     (t
1574     (let ((target (case-frob-stream-target stream)))
1575 dtc 1.33 (if (lisp-stream-p target)
1576     (funcall (lisp-stream-misc target) target op arg1 arg2)
1577     (stream-misc-dispatch target op arg1 arg2))))))
1578 wlott 1.13
1579     (defun case-frob-upcase-out (stream char)
1580     (declare (type case-frob-stream stream)
1581 ram 1.16 (type base-char char))
1582 dtc 1.33 (let ((target (case-frob-stream-target stream))
1583     (char (char-upcase char)))
1584     (if (lisp-stream-p target)
1585     (funcall (lisp-stream-out target) target char)
1586     (stream-write-char target char))))
1587 wlott 1.13
1588     (defun case-frob-upcase-sout (stream str start end)
1589     (declare (type case-frob-stream stream)
1590     (type simple-base-string str)
1591     (type index start)
1592     (type (or index null) end))
1593     (let* ((target (case-frob-stream-target stream))
1594     (len (length str))
1595 dtc 1.33 (end (or end len))
1596     (string (if (and (zerop start) (= len end))
1597     (string-upcase str)
1598     (nstring-upcase (subseq str start end))))
1599     (string-len (- end start)))
1600     (if (lisp-stream-p target)
1601     (funcall (lisp-stream-sout target) target string 0 string-len)
1602     (stream-write-string target string 0 string-len))))
1603 wlott 1.13
1604     (defun case-frob-downcase-out (stream char)
1605     (declare (type case-frob-stream stream)
1606 ram 1.16 (type base-char char))
1607 dtc 1.33 (let ((target (case-frob-stream-target stream))
1608     (char (char-downcase char)))
1609     (if (lisp-stream-p target)
1610     (funcall (lisp-stream-out target) target char)
1611     (stream-write-char target char))))
1612 wlott 1.13
1613     (defun case-frob-downcase-sout (stream str start end)
1614     (declare (type case-frob-stream stream)
1615     (type simple-base-string str)
1616     (type index start)
1617     (type (or index null) end))
1618     (let* ((target (case-frob-stream-target stream))
1619     (len (length str))
1620 dtc 1.33 (end (or end len))
1621     (string (if (and (zerop start) (= len end))
1622     (string-downcase str)
1623     (nstring-downcase (subseq str start end))))
1624     (string-len (- end start)))
1625     (if (lisp-stream-p target)
1626     (funcall (lisp-stream-sout target) target string 0 string-len)
1627     (stream-write-string target string 0 string-len))))
1628 wlott 1.13
1629     (defun case-frob-capitalize-out (stream char)
1630     (declare (type case-frob-stream stream)
1631 ram 1.16 (type base-char char))
1632 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1633     (cond ((alphanumericp char)
1634 dtc 1.33 (let ((char (char-upcase char)))
1635     (if (lisp-stream-p target)
1636     (funcall (lisp-stream-out target) target char)
1637     (stream-write-char target char)))
1638     (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1639 wlott 1.13 (setf (case-frob-stream-sout stream)
1640     #'case-frob-capitalize-aux-sout))
1641     (t
1642 dtc 1.33 (if (lisp-stream-p target)
1643     (funcall (lisp-stream-out target) target char)
1644     (stream-write-char target char))))))
1645 wlott 1.13
1646     (defun case-frob-capitalize-sout (stream str start end)
1647     (declare (type case-frob-stream stream)
1648     (type simple-base-string str)
1649     (type index start)
1650     (type (or index null) end))
1651     (let* ((target (case-frob-stream-target stream))
1652     (str (subseq str start end))
1653     (len (length str))
1654     (inside-word nil))
1655     (dotimes (i len)
1656     (let ((char (schar str i)))
1657     (cond ((not (alphanumericp char))
1658     (setf inside-word nil))
1659     (inside-word
1660     (setf (schar str i) (char-downcase char)))
1661     (t
1662     (setf inside-word t)
1663     (setf (schar str i) (char-upcase char))))))
1664     (when inside-word
1665     (setf (case-frob-stream-out stream)
1666     #'case-frob-capitalize-aux-out)
1667     (setf (case-frob-stream-sout stream)
1668     #'case-frob-capitalize-aux-sout))
1669 dtc 1.33 (if (lisp-stream-p target)
1670     (funcall (lisp-stream-sout target) target str 0 len)
1671     (stream-write-string target str 0 len))))
1672 wlott 1.13
1673     (defun case-frob-capitalize-aux-out (stream char)
1674     (declare (type case-frob-stream stream)
1675 ram 1.16 (type base-char char))
1676 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1677     (cond ((alphanumericp char)
1678 dtc 1.33 (let ((char (char-downcase char)))
1679     (if (lisp-stream-p target)
1680     (funcall (lisp-stream-out target) target char)
1681     (stream-write-char target char))))
1682 wlott 1.13 (t
1683 dtc 1.33 (if (lisp-stream-p target)
1684     (funcall (lisp-stream-out target) target char)
1685     (stream-write-char target char))
1686 wlott 1.13 (setf (case-frob-stream-out stream)
1687     #'case-frob-capitalize-out)
1688     (setf (case-frob-stream-sout stream)
1689     #'case-frob-capitalize-sout)))))
1690    
1691     (defun case-frob-capitalize-aux-sout (stream str start end)
1692     (declare (type case-frob-stream stream)
1693     (type simple-base-string str)
1694     (type index start)
1695     (type (or index null) end))
1696     (let* ((target (case-frob-stream-target stream))
1697     (str (subseq str start end))
1698     (len (length str))
1699     (inside-word t))
1700     (dotimes (i len)
1701     (let ((char (schar str i)))
1702     (cond ((not (alphanumericp char))
1703     (setf inside-word nil))
1704     (inside-word
1705     (setf (schar str i) (char-downcase char)))
1706     (t
1707     (setf inside-word t)
1708     (setf (schar str i) (char-upcase char))))))
1709     (unless inside-word
1710     (setf (case-frob-stream-out stream)
1711     #'case-frob-capitalize-out)
1712     (setf (case-frob-stream-sout stream)
1713     #'case-frob-capitalize-sout))
1714 dtc 1.33 (if (lisp-stream-p target)
1715     (funcall (lisp-stream-sout target) target str 0 len)
1716     (stream-write-string target str 0 len))))
1717 wlott 1.13
1718     (defun case-frob-capitalize-first-out (stream char)
1719     (declare (type case-frob-stream stream)
1720 ram 1.16 (type base-char char))
1721 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1722     (cond ((alphanumericp char)
1723 dtc 1.33 (let ((char (char-upcase char)))
1724     (if (lisp-stream-p target)
1725     (funcall (lisp-stream-out target) target char)
1726     (stream-write-char target char)))
1727 wlott 1.13 (setf (case-frob-stream-out stream)
1728     #'case-frob-downcase-out)
1729     (setf (case-frob-stream-sout stream)
1730     #'case-frob-downcase-sout))
1731     (t
1732 dtc 1.33 (if (lisp-stream-p target)
1733     (funcall (lisp-stream-out target) target char)
1734     (stream-write-char target char))))))
1735 wlott 1.13
1736     (defun case-frob-capitalize-first-sout (stream str start end)
1737     (declare (type case-frob-stream stream)
1738     (type simple-base-string str)
1739     (type index start)
1740     (type (or index null) end))
1741     (let* ((target (case-frob-stream-target stream))
1742     (str (subseq str start end))
1743     (len (length str)))
1744     (dotimes (i len)
1745     (let ((char (schar str i)))
1746     (when (alphanumericp char)
1747     (setf (schar str i) (char-upcase char))
1748     (do ((i (1+ i) (1+ i)))
1749     ((= i len))
1750     (setf (schar str i) (char-downcase (schar str i))))
1751     (setf (case-frob-stream-out stream)
1752     #'case-frob-downcase-out)
1753     (setf (case-frob-stream-sout stream)
1754     #'case-frob-downcase-sout)
1755     (return))))
1756 dtc 1.33 (if (lisp-stream-p target)
1757     (funcall (lisp-stream-sout target) target str 0 len)
1758     (stream-write-string target str 0 len))))
1759 chiles 1.12
1760    
1761     ;;;; Public interface from "EXTENSIONS" package.
1762    
1763     (defstruct (stream-command (:print-function print-stream-command)
1764     (:constructor make-stream-command
1765     (name &optional args)))
1766     (name nil :type symbol)
1767     (args nil :type list))
1768    
1769     (defun print-stream-command (obj str n)
1770     (declare (ignore n))
1771     (format str "#<Stream-Cmd ~S>" (stream-command-name obj)))
1772    
1773    
1774     ;;; GET-STREAM-COMMAND -- Public.
1775     ;;;
1776 dtc 1.26 ;;; We can't simply call the stream's misc method because nil is an
1777 chiles 1.12 ;;; ambiguous return value: does it mean text arrived, or does it mean the
1778     ;;; stream's misc method had no :get-command implementation. We can't return
1779     ;;; nil until there is text input. We don't need to loop because any stream
1780     ;;; implementing :get-command would wait until it had some input. If the
1781     ;;; LISTEN fails, then we have some random stream we must wait on.
1782     ;;;
1783     (defun get-stream-command (stream)
1784     "This takes a stream and waits for text or a command to appear on it. If
1785     text appears before a command, this returns nil, and otherwise it returns
1786     a command."
1787 dtc 1.30 (let ((cmdp (funcall (lisp-stream-misc stream) stream :get-command)))
1788 chiles 1.12 (cond (cmdp)
1789     ((listen stream)
1790     nil)
1791     (t
1792     ;; This waits for input and returns nil when it arrives.
1793     (unread-char (read-char stream) stream)))))
1794 dtc 1.26
1795    
1796 toy 1.49 ;;; READ-SEQUENCE --
1797     ;;; Note: the multi-byte operation SYSTEM:READ-N-BYTES operates on
1798     ;;; subtypes of SIMPLE-ARRAY. Hence the distinction between simple
1799     ;;; and non simple input functions.
1800    
1801     (defun read-sequence (seq stream &key (start 0) (end nil))
1802     "Destructively modify SEQ by reading elements from STREAM.
1803     SEQ is bounded by START and END. SEQ is destructively modified by
1804     copying successive elements into it from STREAM. If the end of file
1805     for STREAM is reached before copying all elements of the subsequence,
1806     then the extra elements near the end of sequence are not updated.
1807    
1808     Argument(s):
1809     SEQ: a proper SEQUENCE
1810     STREAM: an input STREAM
1811     START: a bounding index designator of type '(INTEGER 0 *)' (default 0)
1812     END: a bounding index designator which be NIL or an INTEGER of
1813     type '(INTEGER 0 *)' (default NIL)
1814    
1815     Value(s):
1816     POSITION: an INTEGER greater than or equal to zero, and less than or
1817     equal to the length of the SEQ. POSITION is the index of
1818     the first element of SEQ that was not updated, which might be
1819     less than END because the end of file was reached."
1820    
1821     (declare (type (or list vector) seq)) ; could be (type sequence seq)
1822     (declare (type stream stream))
1823     (declare (type (integer 0 *) start)) ; a list does not have a limit
1824     (declare (type (or null (integer 0 *)) end))
1825     (declare (values (integer 0 *)))
1826    
1827 toy 1.54 (when (not (cl::lisp-stream-p stream))
1828 toy 1.55 (return-from read-sequence (stream-read-sequence stream seq start end)))
1829 toy 1.54
1830 toy 1.49 (let ((end (or end (length seq))))
1831     (declare (type (integer 0 *) start end))
1832    
1833     ;; Just catch some errors earlier than it would be necessary.
1834     (cond ((not (open-stream-p stream))
1835     (error 'stream-error
1836     :stream stream
1837     :format-control "The stream is not open."))
1838     ((not (input-stream-p stream))
1839     (error 'stream-error
1840     :stream stream
1841     :format-control "The stream is not open for input."))
1842     ((and seq (>= start end) 0))
1843     (t
1844     ;; So much for object-oriented programming!
1845     (etypecase seq
1846     (list
1847     (read-into-list seq stream start end))
1848     (simple-string
1849     (read-into-simple-string seq stream start end))
1850     (string
1851     (read-into-string seq stream start end))
1852     (simple-array ; We also know that it is a 'vector'.
1853     (read-into-simple-array seq stream start end))
1854     (vector
1855     (read-into-vector seq stream start end)))
1856     ))))
1857    
1858    
1859     ;;; READ-INTO-LIST, READ-INTO-LIST-1
1860     ;;; Auxiliary functions for READ-SEQUENCE. Their semantics is pretty
1861     ;;; obvious. Since lists do not have an attached element type, and
1862     ;;; since we cannot do a low-level multi-byte read operation on them,
1863     ;;; I simply dispatch on the element type of the stream and then rely
1864     ;;; on READ-BYTE and READ-CHAR to to the input.
1865     ;;;
1866     ;;; NOTE: the use of 'endp' will generate a (desired)
1867     ;;; 'type-error' if the sequence is not a "proper list".
1868    
1869     (defun read-into-list (l stream start end)
1870     (let ((read-function (if (subtypep (stream-element-type stream) 'character)
1871     #'read-char
1872     #'read-byte)))
1873     (read-into-list-1 (nthcdr start l) start end stream read-function)))
1874    
1875     #+:recursive
1876     (defun read-into-list-1 (l start end stream read-function)
1877     (declare (type list l))
1878     (declare (type stream stream))
1879     (declare (type (integer 0 *) start end))
1880     (if (or (endp l) (= start end))
1881     start
1882 toy 1.53 (let* ((eof-marker (load-time-value (list 'eof-marker)))
1883     (el (funcall read-function stream nil eof-marker)))
1884     (cond ((eq el eof-marker) start)
1885 toy 1.49 (t (setf (first l) el)
1886     (read-into-list-1 (rest l)
1887     (1+ start)
1888     end
1889     stream
1890     read-function))))
1891     ))
1892    
1893    
1894     #-:iterative
1895     (defun read-into-list-1 (l start end stream read-function)
1896     (declare (type list l))
1897     (declare (type stream stream))
1898     (declare (type (integer 0 *) start end))
1899    
1900     ;; The declaration for I may be too restrictive in the case of
1901     ;; lists. But then again, it is still a huge number.
1902     (do ((lis l (rest lis))
1903     (i start (1+ i))
1904     )
1905     ((or (endp lis) (>= i end)) i)
1906     (declare (type list lis))
1907     (declare (type index i))
1908 toy 1.53 (let* ((eof-marker (load-time-value (list 'eof-marker)))
1909     (el (funcall read-function stream nil eof-marker)))
1910     (when (eq el eof-marker)
1911 toy 1.49 (return i))
1912 toy 1.53 (setf (first lis) el))))
1913 toy 1.49
1914    
1915     ;;; READ-INTO-SIMPLE-STRING --
1916    
1917     (defun read-into-simple-string (s stream start end)
1918     (declare (type simple-string s))
1919     (declare (type stream stream))
1920     (declare (type index start end))
1921     (unless (subtypep (stream-element-type stream) 'character)
1922     (error 'type-error
1923     :datum (read-char stream nil #\Null)
1924     :expected-type (stream-element-type stream)
1925     :format-control "Trying to read characters from a binary stream."))
1926     ;; Let's go as low level as it seems reasonable.
1927     (let* ((numbytes (- end start))
1928     (bytes-read (system:read-n-bytes stream s start numbytes nil))
1929     )
1930     (if (< bytes-read numbytes)
1931     (+ start bytes-read)
1932     end)))
1933    
1934    
1935     (defun read-into-string (s stream start end)
1936     (declare (type string s))
1937     (declare (type stream stream))
1938     (declare (type index start end))
1939     (unless (subtypep (stream-element-type stream) 'character)
1940     (error 'type-error
1941     :datum (read-char stream nil #\Null)
1942     :expected-type (stream-element-type stream)
1943     :format-control "Trying to read characters from a binary stream."))
1944     (do ((i start (1+ i))
1945     (s-len (length s))
1946     )
1947     ((or (>= i s-len) (>= i end)) i)
1948     (declare (type index i s-len))
1949 toy 1.53 (let* ((eof-marker (load-time-value (list 'eof-marker)))
1950     (el (read-char stream nil eof-marker)))
1951     (declare (type (or character stream) el))
1952     (when (eq el eof-marker)
1953 toy 1.49 (return i))
1954 toy 1.53 (setf (char s i) (the character el)))))
1955 toy 1.49
1956    
1957     ;;; READ-INTO-SIMPLE-ARRAY --
1958     ;;; We definitively know that we are really reading into a vector.
1959    
1960     ;;; *read-into-simple-array-recognized-types* --
1961     ;;;
1962     ;;; Note the new feature :extended-binary-streams.
1963     ;;; Up to 18a, CMUCL has a wired in limitation to treat only 8-bits
1964     ;;; word binary streams. 'fd-read-n-bytes' is associated to a binary
1965     ;;; stream only if the fd-stream has binary type size of 1 (i.e. 1
1966     ;;; 8-bits byte). This is reflected also in the size of the input
1967     ;;; buffers.
1968    
1969     (defparameter *read-into-simple-array-recognized-types*
1970     '((unsigned-byte 8)
1971     (unsigned-byte 16)
1972     (unsigned-byte 32)
1973     (signed-byte 8)
1974     (signed-byte 16)
1975    
1976     (signed-byte 32)
1977     ))
1978    
1979     (defun read-into-simple-array (s stream start end)
1980     ;; The complex declaration is needed to make Python behave.
1981     ;; The first declaration does not work because of type promotion
1982     ;; which effectively excises the etypecase below.
1983     ;; The second declaration does not quite work because it does not
1984     ;; quite constrain the array element type.
1985     ;; (declare (type (simple-array (or unsigned-byte signed-byte) (*)) s))
1986     ;; (declare (type (simple-array * (*)) s))
1987     (declare (type (or (simple-array (unsigned-byte 8) (*))
1988     (simple-array (signed-byte 8) (*))
1989     (simple-array (unsigned-byte 16) (*))
1990     (simple-array (signed-byte 16) (*))
1991     (simple-array (unsigned-byte 32) (*))
1992     (simple-array (signed-byte 32) (*))
1993     (simple-array (unsigned-byte *) (*))
1994     (simple-array (signed-byte *) (*))
1995     )
1996     s))
1997    
1998     (declare (type stream stream))
1999     (declare (type index start end))
2000     (let ((stream-et (stream-element-type stream)))
2001     (cond ((subtypep (stream-element-type stream) 'character)
2002     (error 'type-error
2003     :datum (read-byte stream nil 0)
2004     :expected-type (stream-element-type stream) ; Bogus?!?
2005     :format-control
2006     "Trying to read binary data from a text stream."))
2007    
2008     ;; Let's go as low level as it seems reasonable.
2009     ((not (member stream-et
2010     *read-into-simple-array-recognized-types*
2011     :test #'equal))
2012     ;; (format t ">>> Reading vector from binary stream of type ~S~%"
2013     ;; stream-et)
2014    
2015     ;; We resort to the READ-BYTE based operation.
2016     (read-into-vector s stream start end