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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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