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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.72 - (hide annotations)
Wed Apr 7 17:15:00 2004 UTC (10 years ago) by emarsden
Branch: MAIN
Changes since 1.71: +32 -33 lines
ANSI compliance fixes for CONCATENATED-STREAMS:

  - (read-char-no-hang (make-concatenated-stream) nil :eof) returns :eof
    instead of nil

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