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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (hide annotations)
Tue Nov 1 17:57:46 1994 UTC (19 years, 5 months ago) by ram
Branch: MAIN
Changes since 1.23: +9 -4 lines
Clarify EOF-ERROR-P handling in read-n-bytes.
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 ram 1.24 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/stream.lisp,v 1.24 1994/11/01 17:57:46 ram 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     ;;;
15 ram 1.14 ;;; This file contains the OS-independent stream functions.
16 ram 1.1 ;;;
17 ram 1.2 (in-package "LISP")
18 ram 1.1
19 ram 1.14 (export '(broadcast-stream make-broadcast-stream broadcast-stream-streams
20     synonym-stream make-synonym-stream synonym-stream-symbol
21     concatenated-stream make-concatenated-stream
22     concatenated-stream-streams
23     two-way-stream make-two-way-stream two-way-stream-input-stream
24     two-way-stream-output-stream
25     echo-stream make-echo-stream echo-stream-input-stream
26     echo-stream-output-stream
27     make-string-input-stream make-string-output-stream
28 ram 1.1 get-output-stream-string stream-element-type input-stream-p
29 ram 1.14 output-stream-p open-stream-p interactive-stream-p
30 wlott 1.17 open-stream-p close read-line read-char
31 ram 1.1 unread-char peek-char listen read-char-no-hang clear-input read-byte
32     write-char write-string write-line terpri fresh-line
33     finish-output force-output clear-output write-byte
34 ram 1.14 stream streamp string-stream
35     *standard-input* *standard-output*
36 ram 1.1 *error-output* *query-io* *debug-io* *terminal-io* *trace-output*))
37    
38 ram 1.18 (in-package "SYSTEM")
39 ram 1.1 (export '(make-indenting-stream read-n-bytes))
40    
41 ram 1.18 (in-package "EXT")
42    
43     (export '(get-stream-command
44     stream-command stream-command-p stream-command-name
45     stream-command-args make-stream-command make-case-frob-stream))
46    
47     (in-package "LISP")
48    
49 ram 1.14 (deftype string-stream ()
50     '(or string-input-stream string-output-stream
51     fill-pointer-output-stream))
52    
53 ram 1.1 ;;;; Standard streams:
54     ;;;
55     ;;; The initialization of these streams is performed by Stream-Init,
56     ;;; which lives in the file of machine-specific stream functions.
57     ;;;
58     (defvar *terminal-io* () "Terminal I/O stream.")
59     (defvar *standard-input* () "Default input stream.")
60     (defvar *standard-output* () "Default output stream.")
61     (defvar *error-output* () "Error output stream.")
62     (defvar *query-io* () "Query I/O stream.")
63     (defvar *trace-output* () "Trace output stream.")
64     (defvar *debug-io* () "Interactive debugging stream.")
65    
66     (defun ill-in (stream &rest ignore)
67     (declare (ignore ignore))
68     (error "~S is not a character input stream." stream))
69     (defun ill-out (stream &rest ignore)
70     (declare (ignore ignore))
71     (error "~S is not a character output stream." stream))
72     (defun ill-bin (stream &rest ignore)
73     (declare (ignore ignore))
74     (error "~S is not a binary input stream." stream))
75     (defun ill-bout (stream &rest ignore)
76     (declare (ignore ignore))
77     (error "~S is not a binary output stream." stream))
78     (defun closed-flame (stream &rest ignore)
79     (declare (ignore ignore))
80     (error "~S is closed." stream))
81     (defun do-nothing (&rest ignore)
82     (declare (ignore ignore)))
83    
84     (defun %print-stream (structure stream d)
85     (declare (ignore d structure))
86     (write-string "#<Bare Stream>" stream))
87    
88     ;;; HOW THE STREAM STRUCTURE IS USED:
89     ;;;
90     ;;; Many of the slots of the stream structure contain functions
91     ;;; which are called to perform some operation on the stream. Closed
92     ;;; streams have #'Closed-Flame in all of their function slots. If
93     ;;; one side of an I/O or echo stream is closed, the whole stream is
94     ;;; considered closed. The functions in the operation slots take
95     ;;; arguments as follows:
96     ;;;
97     ;;; In: Stream, Eof-Errorp, Eof-Value
98     ;;; Bin: Stream, Eof-Errorp, Eof-Value
99     ;;; N-Bin: Stream, Buffer, Start, Numbytes, Eof-Errorp
100     ;;; Out: Stream, Character
101     ;;; Bout: Stream, Integer
102     ;;; Sout: Stream, String, Start, End
103     ;;; Misc: Stream, Operation, &Optional Arg1, Arg2
104     ;;;
105     ;;; In order to save space, some of the less common stream operations
106     ;;; are handled by just one function, the Misc method. This function
107     ;;; is passed a keyword which indicates the operation to perform.
108     ;;; The following keywords are used:
109 ram 1.10 ;;; :listen - Return the following values:
110     ;;; t if any input waiting.
111     ;;; :eof if at eof.
112     ;;; nil if no input is available and not at eof.
113 ram 1.1 ;;; :unread - Unread the character Arg.
114     ;;; :close - Do any stream specific stuff to close the stream.
115     ;;; The methods are set to closed-flame by the close
116     ;;; function, so that need not be done by this
117     ;;; function.
118     ;;; :clear-input - Clear any unread input
119     ;;; :finish-output,
120     ;;; :force-output - Cause output to happen
121     ;;; :clear-output - Clear any undone output
122 ram 1.18 ;;; :element-type - Return the type of element the stream deals wit<h.
123 ram 1.1 ;;; :line-length - Return the length of a line of output.
124     ;;; :charpos - Return current output position on the line.
125     ;;; :file-length - Return the file length of a file stream.
126     ;;; :file-position - Return or change the current position of a file stream.
127     ;;; :file-name - Return the name of an associated file.
128 ram 1.14 ;;; :interactive-p - Is this an interactive device?
129 ram 1.1 ;;;
130     ;;; In order to do almost anything useful, it is necessary to
131     ;;; define a new type of structure that includes stream, so that the
132     ;;; stream can have some state information.
133     ;;;
134     ;;; THE STREAM IN-BUFFER:
135     ;;;
136     ;;; The In-Buffer in the stream holds characters or bytes that
137     ;;; are ready to be read by some input function. If there is any
138     ;;; stuff in the In-Buffer, then the reading function can use it
139     ;;; without calling any stream method. Any stream may put stuff in
140     ;;; the In-Buffer, and may also assume that any input in the In-Buffer
141     ;;; has been consumed before any in-method is called. If a text
142     ;;; stream has in In-Buffer, then the first character should not be
143     ;;; used to buffer normal input so that it is free for unreading into.
144     ;;;
145     ;;; The In-Buffer slot is a vector In-Buffer-Length long. The
146     ;;; In-Index is the index in the In-Buffer of the first available
147     ;;; object. The available objects are thus between In-Index and the
148     ;;; length of the In-Buffer.
149     ;;;
150     ;;; When this buffer is only accessed by the normal stream
151     ;;; functions, the number of function calls is halved, thus
152     ;;; potentially doubling the speed of simple operations. If the
153     ;;; Fast-Read-Char and Fast-Read-Byte macros are used, nearly all
154     ;;; function call overhead is removed, vastly speeding up these
155     ;;; important operations.
156     ;;;
157     ;;; If a stream does not have an In-Buffer, then the In-Buffer slot
158     ;;; must be nil, and the In-Index must be In-Buffer-Length. These are
159     ;;; the default values for the slots.
160 ram 1.18
161 ram 1.1
162     ;;; Stream manipulation functions.
163    
164     (defun input-stream-p (stream)
165     "Returns non-nil if the given Stream can perform input operations."
166     (and (streamp stream)
167     (not (eq (stream-in stream) #'closed-flame))
168     (or (not (eq (stream-in stream) #'ill-in))
169     (not (eq (stream-bin stream) #'ill-bin)))))
170    
171     (defun output-stream-p (stream)
172     "Returns non-nil if the given Stream can perform output operations."
173     (and (streamp stream)
174     (not (eq (stream-in stream) #'closed-flame))
175     (or (not (eq (stream-out stream) #'ill-out))
176     (not (eq (stream-bout stream) #'ill-bout)))))
177    
178 ram 1.14 (defun open-stream-p (stream)
179     "Return true if Stream is not closed."
180     (declare (type stream stream))
181     (not (eq (stream-in stream) #'closed-flame)))
182    
183 ram 1.1 (defun stream-element-type (stream)
184     "Returns a type specifier for the kind of object returned by the Stream."
185 wlott 1.17 (declare (type stream stream))
186     (funcall (stream-misc stream) stream :element-type))
187 ram 1.1
188 ram 1.14 (defun interactive-stream-p (stream)
189     "Return true if Stream does I/O on a terminal or other interactive device."
190     (declare (type stream stream))
191     (funcall (stream-misc stream) stream :interactive-p))
192    
193 wlott 1.17 (defun open-stream-p (stream)
194     "Return true if and only if STREAM has not been closed."
195     (declare (type stream stream))
196     (not (eq (stream-in stream) #'closed-flame)))
197    
198 ram 1.1 (defun close (stream &key abort)
199     "Closes the given Stream. No more I/O may be performed, but inquiries
200     may still be made. If :Abort is non-nil, an attempt is made to clean
201     up the side effects of having created the stream."
202 wlott 1.17 (declare (type stream stream))
203     (when (open-stream-p stream)
204     (funcall (stream-misc stream) stream :close abort))
205 ram 1.1 t)
206    
207     (defun set-closed-flame (stream)
208     (setf (stream-in stream) #'closed-flame)
209     (setf (stream-bin stream) #'closed-flame)
210     (setf (stream-n-bin stream) #'closed-flame)
211     (setf (stream-in stream) #'closed-flame)
212     (setf (stream-out stream) #'closed-flame)
213     (setf (stream-bout stream) #'closed-flame)
214     (setf (stream-sout stream) #'closed-flame)
215     (setf (stream-misc stream) #'closed-flame))
216 ram 1.18
217 ram 1.1
218 ram 1.18 ;;;; File position and file length.
219    
220     ;;; File-Position -- Public
221     ;;;
222     ;;; Call the misc method with the :file-position operation.
223     ;;;
224     (defun file-position (stream &optional position)
225     "With one argument returns the current position within the file
226 wlott 1.19 File-Stream is open to. If the second argument is supplied, then
227     this becomes the new file position. The second argument may also
228     be :start or :end for the start and end of the file, respectively."
229     (declare (stream stream)
230     (type (or index (member nil :start :end)) position))
231 ram 1.18 (cond
232     (position
233     (setf (stream-in-index stream) in-buffer-length)
234     (funcall (stream-misc stream) stream :file-position position))
235     (t
236     (let ((res (funcall (stream-misc stream) stream :file-position nil)))
237     (when res (- res (- in-buffer-length (stream-in-index stream))))))))
238    
239    
240     ;;; File-Length -- Public
241     ;;;
242     ;;; Like File-Position, only use :file-length.
243     ;;;
244     (defun file-length (stream)
245     "This function returns the length of the file that File-Stream is open to."
246     (declare (stream stream))
247     (funcall (stream-misc stream) stream :file-length))
248    
249    
250 ram 1.1 ;;; Input functions:
251    
252     (defun read-line (&optional (stream *standard-input*) (eof-errorp t) eof-value
253     recursive-p)
254     "Returns a line of text read from the Stream as a string, discarding the
255     newline character."
256     (declare (ignore recursive-p))
257 ram 1.18 (prepare-for-fast-read-char stream
258     (let ((res (make-string 80))
259     (len 80)
260     (index 0))
261     (loop
262     (let ((ch (fast-read-char nil nil)))
263     (cond (ch
264     (when (char= ch #\newline)
265     (done-with-fast-read-char)
266     (return (values (shrink-vector res index) nil)))
267     (when (= index len)
268     (setq len (* len 2))
269     (let ((new (make-string len)))
270     (replace new res)
271     (setq res new)))
272     (setf (schar res index) ch)
273     (incf index))
274     ((zerop index)
275     (done-with-fast-read-char)
276 ram 1.22 (return (values (eof-or-lose (in-synonym-of stream)
277     eof-errorp eof-value)
278     t)))
279 ram 1.20 ;; since fast-read-char hit already the eof char, we
280     ;; shouldn't do another read-char
281 ram 1.18 (t
282     (done-with-fast-read-char)
283     (return (values (shrink-vector res index) t)))))))))
284 ram 1.1
285 ram 1.10
286 ram 1.1 ;;; We proclaim them inline here, then proclaim them notinline at EOF,
287     ;;; so, except in this file, they are not inline by default, but they can be.
288     ;;;
289     (proclaim '(inline read-char unread-char read-byte listen))
290     (defun read-char (&optional (stream *standard-input*) (eof-errorp t) eof-value
291     recursive-p)
292     "Inputs a character from Stream and returns it."
293     (declare (ignore recursive-p))
294 ram 1.18 (prepare-for-fast-read-char stream
295     (prog1
296     (fast-read-char eof-errorp eof-value)
297     (done-with-fast-read-char))))
298 ram 1.1
299     (defun unread-char (character &optional (stream *standard-input*))
300     "Puts the Character back on the front of the input Stream."
301     (let* ((stream (in-synonym-of stream))
302 ram 1.18 (index (1- (stream-in-index stream)))
303 ram 1.1 (buffer (stream-in-buffer stream)))
304     (declare (fixnum index))
305     (when (minusp index) (error "Nothing to unread."))
306 ram 1.18 (cond (buffer
307     (setf (aref buffer index) (char-code character))
308     (setf (stream-in-index stream) index))
309     (t
310     (funcall (stream-misc stream) stream :unread character))))
311 ram 1.1 nil)
312    
313     (defun peek-char (&optional (peek-type nil) (stream *standard-input*)
314     (eof-errorp t) eof-value recursive-p)
315     "Peeks at the next character in the input Stream. See manual for details."
316     (declare (ignore recursive-p))
317     (let* ((stream (in-synonym-of stream))
318     (char (read-char stream eof-errorp eof-value)))
319     (cond ((eq char eof-value) char)
320     ((characterp peek-type)
321     (do ((char char (read-char stream eof-errorp eof-value)))
322     ((or (eq char eof-value) (char= char peek-type))
323     (unless (eq char eof-value)
324     (unread-char char stream))
325     char)))
326     ((eq peek-type t)
327     (do ((char char (read-char stream eof-errorp eof-value)))
328     ((or (eq char eof-value) (not (whitespace-char-p char)))
329     (unless (eq char eof-value)
330     (unread-char char stream))
331     char)))
332     (t
333     (unread-char char stream)
334     char))))
335    
336     (defun listen (&optional (stream *standard-input*))
337     "Returns T if a character is availible on the given Stream."
338     (let ((stream (in-synonym-of stream)))
339     (or (/= (the fixnum (stream-in-index stream)) in-buffer-length)
340 ram 1.10 ;; Test for t explicitly since misc methods return :eof sometimes.
341     (eq (funcall (stream-misc stream) stream :listen) t))))
342 ram 1.1
343     (defun read-char-no-hang (&optional (stream *standard-input*)
344     (eof-errorp t) eof-value recursive-p)
345     "Returns the next character from the Stream if one is availible, or nil."
346     (declare (ignore recursive-p))
347 ram 1.10 (let ((stream (in-synonym-of stream)))
348     (if (funcall (stream-misc stream) stream :listen)
349     ;; On t or :eof get READ-CHAR to do the work.
350     (read-char stream eof-errorp eof-value)
351     nil)))
352 ram 1.1
353     (defun clear-input (&optional (stream *standard-input*))
354     "Clears any buffered input associated with the Stream."
355     (let ((stream (in-synonym-of stream)))
356     (setf (stream-in-index stream) in-buffer-length)
357 ram 1.2 (funcall (stream-misc stream) stream :clear-input)
358     nil))
359 ram 1.1
360     (defun read-byte (stream &optional (eof-errorp t) eof-value)
361     "Returns the next byte of the Stream."
362 ram 1.18 (prepare-for-fast-read-byte stream
363     (prog1
364     (fast-read-byte eof-errorp eof-value t)
365     (done-with-fast-read-byte))))
366 ram 1.1
367     (defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
368 ram 1.24 "Reads Numbytes bytes into the Buffer starting at Start, returning the number
369     of bytes read.
370     -- If EOF-ERROR-P is true, an END-OF-FILE condition is signalled if
371     end-of-file is encountered before Count bytes have been read.
372     -- If EOF-ERROR-P is false, READ-N-BYTES reads as much data is currently
373     available (up to count bytes.) On pipes or similar devices, this
374     function returns as soon as any adata is available, even if the amount
375     read is less than Count and eof has not been hit."
376 ram 1.18 (declare (type index numbytes start)
377     (type (or (simple-array * (*)) system-area-pointer) buffer))
378 ram 1.1 (let* ((stream (in-synonym-of stream))
379     (in-buffer (stream-in-buffer stream))
380     (index (stream-in-index stream))
381     (num-buffered (- in-buffer-length index)))
382     (declare (fixnum index num-buffered))
383     (cond
384 ram 1.2 ((not in-buffer)
385 ram 1.18 (funcall (stream-n-bin stream) stream buffer start numbytes eof-errorp))
386 ram 1.1 ((<= numbytes num-buffered)
387     (%primitive byte-blt in-buffer index buffer start (+ start numbytes))
388     (setf (stream-in-index stream) (+ index numbytes))
389     numbytes)
390     (t
391     (let ((end (+ start num-buffered)))
392     (%primitive byte-blt in-buffer index buffer start end)
393     (setf (stream-in-index stream) in-buffer-length)
394 ram 1.18 (+ (funcall (stream-n-bin stream) stream buffer end
395     (- numbytes num-buffered)
396     eof-errorp)
397 ram 1.1 num-buffered))))))
398 ram 1.18
399    
400     ;;; Amount of space we leave at the start of the in-buffer for unreading. 4
401     ;;; instead of 1 to allow word-aligned copies.
402     ;;;
403     (defconstant in-buffer-extra 4)
404    
405     ;;; FAST-READ-CHAR-REFILL -- Interface
406     ;;;
407     ;;; This function is called by the fast-read-char expansion to refill the
408     ;;; in-buffer for text streams. There is definitely an in-buffer, and hence
409     ;;; myst be an n-bin method.
410     ;;;
411     (defun fast-read-char-refill (stream eof-errorp eof-value)
412     (let* ((ibuf (stream-in-buffer stream))
413     (count (funcall (stream-n-bin stream) stream
414     ibuf in-buffer-extra
415     (- in-buffer-length in-buffer-extra)
416     nil))
417     (start (- in-buffer-length count)))
418     (declare (type index start count))
419     (cond ((zerop count)
420     (setf (stream-in-index stream) in-buffer-length)
421     (funcall (stream-in stream) stream eof-errorp eof-value))
422     (t
423     (when (/= start in-buffer-extra)
424     (bit-bash-copy ibuf (+ (* in-buffer-extra vm:byte-bits)
425     (* vm:vector-data-offset vm:word-bits))
426     ibuf (+ (the index (* start vm:byte-bits))
427     (* vm:vector-data-offset vm:word-bits))
428     (* count vm:byte-bits)))
429     (setf (stream-in-index stream) (1+ start))
430     (code-char (aref ibuf start))))))
431    
432    
433     ;;; FAST-READ-BYTE-REFILL -- Interface
434     ;;;
435     ;;; Similar to FAST-READ-CHAR-REFILL, but we don't have to leave room for
436     ;;; unreading.
437     ;;;
438     (defun fast-read-byte-refill (stream eof-errorp eof-value)
439     (let* ((ibuf (stream-in-buffer stream))
440     (count (funcall (stream-n-bin stream) stream
441     ibuf 0 in-buffer-length
442     nil))
443     (start (- in-buffer-length count)))
444     (declare (type index start count))
445     (cond ((zerop count)
446     (setf (stream-in-index stream) in-buffer-length)
447     (funcall (stream-bin stream) stream eof-errorp eof-value))
448     (t
449     (unless (zerop start)
450     (bit-bash-copy ibuf (* vm:vector-data-offset vm:word-bits)
451     ibuf (+ (the index (* start vm:byte-bits))
452     (* vm:vector-data-offset vm:word-bits))
453     (* count vm:byte-bits)))
454     (setf (stream-in-index stream) (1+ start))
455     (aref ibuf start)))))
456    
457 ram 1.1
458     ;;; Output functions:
459    
460     (defun write-char (character &optional (stream *standard-output*))
461     "Outputs the Character to the Stream."
462     (with-out-stream stream stream-out character)
463     character)
464    
465     (defun terpri (&optional (stream *standard-output*))
466     "Outputs a new line to the Stream."
467     (with-out-stream stream stream-out #\newline)
468     nil)
469    
470     (defun fresh-line (&optional (stream *standard-output*))
471     "Outputs a new line to the Stream if it is not positioned at the begining of
472     a line. Returns T if it output a new line, nil otherwise."
473     (let ((stream (out-synonym-of stream)))
474     (when (/= (or (charpos stream) 1) 0)
475     (funcall (stream-out stream) stream #\newline)
476     t)))
477    
478     (defun write-string (string &optional (stream *standard-output*)
479     &key (start 0) (end (length (the vector string))))
480     "Outputs the String to the given Stream."
481     (write-string* string stream start end))
482    
483     (defun write-string* (string &optional (stream *standard-output*)
484     (start 0) (end (length (the vector string))))
485     (declare (fixnum start end))
486     (if (array-header-p string)
487     (with-array-data ((data string) (offset-start start) (offset-end end))
488     (with-out-stream stream stream-sout data offset-start offset-end))
489     (with-out-stream stream stream-sout string start end))
490     string)
491    
492     (defun write-line (string &optional (stream *standard-output*)
493     &key (start 0) (end (length string)))
494     "Outputs the String to the given Stream, followed by a newline character."
495     (write-line* string stream start end))
496    
497     (defun write-line* (string &optional (stream *standard-output*)
498     (start 0) (end (length string)))
499     (declare (fixnum start end))
500     (let ((stream (out-synonym-of stream)))
501     (if (array-header-p string)
502     (with-array-data ((data string) (offset-start start) (offset-end end))
503     (with-out-stream stream stream-sout data offset-start offset-end))
504     (with-out-stream stream stream-sout string start end))
505     (funcall (stream-out stream) stream #\newline))
506     string)
507    
508     (defun charpos (&optional (stream *standard-output*))
509     "Returns the number of characters on the current line of output of the given
510     Stream, or Nil if that information is not availible."
511     (with-out-stream stream stream-misc :charpos))
512    
513     (defun line-length (&optional (stream *standard-output*))
514     "Returns the number of characters that will fit on a line of output on the
515     given Stream, or Nil if that information is not available."
516     (with-out-stream stream stream-misc :line-length))
517    
518     (defun finish-output (&optional (stream *standard-output*))
519     "Attempts to ensure that all output sent to the the Stream has reached its
520     destination, and only then returns."
521     (with-out-stream stream stream-misc :finish-output)
522     nil)
523    
524     (defun force-output (&optional (stream *standard-output*))
525     "Attempts to force any buffered output to be sent."
526     (with-out-stream stream stream-misc :force-output)
527     nil)
528    
529     (defun clear-output (&optional (stream *standard-output*))
530     "Clears the given output Stream."
531     (with-out-stream stream stream-misc :clear-output)
532     nil)
533    
534     (defun write-byte (integer stream)
535     "Outputs the Integer to the binary Stream."
536     (with-out-stream stream stream-bout integer)
537     integer)
538    
539     ;;;; Broadcast streams:
540    
541     (defstruct (broadcast-stream (:include stream
542     (out #'broadcast-out)
543     (bout #'broadcast-bout)
544     (sout #'broadcast-sout)
545     (misc #'broadcast-misc))
546     (:print-function %print-broadcast-stream)
547     (:constructor make-broadcast-stream (&rest streams)))
548     ;; This is a list of all the streams we broadcast to.
549 ram 1.14 (streams () :type list :read-only t))
550 ram 1.1
551     (setf (documentation 'make-broadcast-stream 'function)
552     "Returns an ouput stream which sends its output to all of the given streams.")
553    
554     (defun %print-broadcast-stream (s stream d)
555     (declare (ignore s d))
556     (write-string "#<Broadcast Stream>" stream))
557    
558     (macrolet ((out-fun (fun method &rest args)
559     `(defun ,fun (stream ,@args)
560     (dolist (stream (broadcast-stream-streams stream))
561     (funcall (,method stream) stream ,@args)))))
562     (out-fun broadcast-out stream-out char)
563     (out-fun broadcast-bout stream-bout byte)
564     (out-fun broadcast-sout stream-sout string start end))
565    
566     (defun broadcast-misc (stream operation &optional arg1 arg2)
567     (let ((streams (broadcast-stream-streams stream)))
568     (case operation
569     (:charpos
570     (dolist (stream streams)
571     (let ((charpos (funcall (stream-misc stream) stream :charpos)))
572     (if charpos (return charpos)))))
573     (:line-length
574     (let ((min nil))
575     (dolist (stream streams min)
576     (let ((res (funcall (stream-misc stream) stream :line-length)))
577     (when res (setq min (if min (min res min) res)))))))
578     (:element-type
579     (let (res)
580     (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
581     (pushnew (funcall (stream-misc stream) stream :element-type) res
582     :test #'equal))))
583     (t
584     (let ((res nil))
585     (dolist (stream streams res)
586     (setq res (funcall (stream-misc stream) stream operation
587     arg1 arg2))))))))
588    
589     ;;;; Synonym Streams:
590    
591     (defstruct (synonym-stream (:include stream
592     (in #'synonym-in)
593     (bin #'synonym-bin)
594     (n-bin #'synonym-n-bin)
595     (out #'synonym-out)
596     (bout #'synonym-bout)
597     (sout #'synonym-sout)
598     (misc #'synonym-misc))
599     (:print-function %print-synonym-stream)
600     (:constructor make-synonym-stream (symbol)))
601     ;; This is the symbol, the value of which is the stream we are synonym to.
602 wlott 1.15 (symbol nil :type symbol :read-only t))
603 ram 1.1
604     (defun %print-synonym-stream (s stream d)
605     (declare (ignore d))
606     (format stream "#<Synonym Stream to ~S>" (synonym-stream-symbol s)))
607    
608     (setf (documentation 'make-synonym-stream 'function)
609     "Returns a stream which performs its operations on the stream which is the
610     value of the dynamic variable named by Symbol.")
611    
612     ;;; The output simple output methods just call the corresponding method
613     ;;; in the synonymed stream.
614     ;;;
615     (macrolet ((out-fun (name slot &rest args)
616     `(defun ,name (stream ,@args)
617 ram 1.11 (declare (optimize (safety 1)))
618 ram 1.1 (let ((syn (symbol-value (synonym-stream-symbol stream))))
619     (funcall (,slot syn) syn ,@args)))))
620     (out-fun synonym-out stream-out ch)
621     (out-fun synonym-bout stream-bout n)
622     (out-fun synonym-sout stream-sout string start end))
623    
624    
625     ;;; Bind synonym stream to this so that SPIO can turn on the right frob in
626     ;;; the icon when we are in a terminal input wait.
627     ;;;
628     (defvar *previous-stream* nil)
629    
630     ;;; For the input methods, we just call the corresponding function on the
631     ;;; synonymed stream. These functions deal with getting input out of
632     ;;; the In-Buffer if there is any.
633     ;;;
634     (macrolet ((in-fun (name fun &rest args)
635     `(defun ,name (stream ,@args)
636 ram 1.11 (declare (optimize (safety 1)))
637 ram 1.1 (let ((*previous-stream* stream))
638     (,fun (symbol-value (synonym-stream-symbol stream)) ,@args)))))
639     (in-fun synonym-in read-char eof-errorp eof-value)
640     (in-fun synonym-bin read-byte eof-errorp eof-value)
641     (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-errorp))
642    
643    
644     ;;; Synonym-Misc -- Internal
645     ;;;
646     ;;; We have to special-case the operations which could look at stuff in
647     ;;; the in-buffer.
648     ;;;
649     (defun synonym-misc (stream operation &optional arg1 arg2)
650 ram 1.11 (declare (optimize (safety 1)))
651 ram 1.1 (let ((syn (symbol-value (synonym-stream-symbol stream)))
652     (*previous-stream* stream))
653     (case operation
654     (:listen (or (/= (the fixnum (stream-in-index syn)) in-buffer-length)
655     (funcall (stream-misc syn) syn :listen)))
656     (t
657     (funcall (stream-misc syn) syn operation arg1 arg2)))))
658    
659     ;;;; Two-Way streams:
660    
661     (defstruct (two-way-stream
662     (:include stream
663     (in #'two-way-in)
664     (bin #'two-way-bin)
665     (n-bin #'two-way-n-bin)
666     (out #'two-way-out)
667     (bout #'two-way-bout)
668     (sout #'two-way-sout)
669     (misc #'two-way-misc))
670     (:print-function %print-two-way-stream)
671     (:constructor make-two-way-stream (input-stream output-stream)))
672     ;; We read from this stream...
673 ram 1.14 (input-stream (required-argument) :type stream :read-only t)
674 ram 1.1 ;; And write to this one
675 ram 1.14 (output-stream (required-argument) :type stream :read-only t))
676 ram 1.1
677     (defun %print-two-way-stream (s stream d)
678     (declare (ignore d))
679     (format stream "#<Two-Way Stream, Input = ~S, Output = ~S>"
680     (two-way-stream-input-stream s)
681     (two-way-stream-output-stream s)))
682    
683     (setf (documentation 'make-two-way-stream 'function)
684     "Returns a bidirectional stream which gets its input from Input-Stream and
685     sends its output to Output-Stream.")
686    
687     (macrolet ((out-fun (name slot &rest args)
688     `(defun ,name (stream ,@args)
689     (let ((syn (two-way-stream-output-stream stream)))
690     (funcall (,slot syn) syn ,@args)))))
691     (out-fun two-way-out stream-out ch)
692     (out-fun two-way-bout stream-bout n)
693     (out-fun two-way-sout stream-sout string start end))
694    
695     (macrolet ((in-fun (name fun &rest args)
696     `(defun ,name (stream ,@args)
697 ram 1.10 (force-output (two-way-stream-output-stream stream))
698 ram 1.1 (,fun (two-way-stream-input-stream stream) ,@args))))
699     (in-fun two-way-in read-char eof-errorp eof-value)
700     (in-fun two-way-bin read-byte eof-errorp eof-value)
701     (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-errorp))
702    
703     (defun two-way-misc (stream operation &optional arg1 arg2)
704     (let* ((in (two-way-stream-input-stream stream))
705     (in-method (stream-misc in))
706     (out (two-way-stream-output-stream stream))
707     (out-method (stream-misc out)))
708     (case operation
709     (:listen (or (/= (the fixnum (stream-in-index in)) in-buffer-length)
710     (funcall in-method in :listen)))
711     ((:finish-output :force-output :clear-output)
712     (funcall out-method out operation arg1 arg2))
713     ((:clear-input :unread)
714     (funcall in-method in operation arg1 arg2))
715     (:element-type
716     (let ((in-type (funcall in-method in :element-type))
717     (out-type (funcall out-method out :element-type)))
718     (if (equal in-type out-type)
719     in-type `(and ,in-type ,out-type))))
720     (:close
721     (set-closed-flame stream))
722     (t
723     (or (funcall in-method in operation arg1 arg2)
724     (funcall out-method out operation arg1 arg2))))))
725    
726     ;;;; Concatenated Streams:
727    
728     (defstruct (concatenated-stream
729     (:include stream
730     (in #'concatenated-in)
731     (bin #'concatenated-bin)
732     (misc #'concatenated-misc))
733     (:print-function %print-concatenated-stream)
734     (:constructor
735     make-concatenated-stream (&rest streams &aux (current streams))))
736     ;; The car of this is the stream we are reading from now.
737     current
738     ;; This is a list of all the streams. We need to remember them so that
739     ;; we can close them.
740 ram 1.14 (streams nil :type list :read-only t))
741 ram 1.1
742     (defun %print-concatenated-stream (s stream d)
743     (declare (ignore d))
744     (format stream "#<Concatenated Stream, Streams = ~S>"
745     (concatenated-stream-streams s)))
746    
747     (setf (documentation 'make-concatenated-stream 'function)
748     "Returns a stream which takes its input from each of the Streams in turn,
749     going on to the next at EOF.")
750    
751     (macrolet ((in-fun (name fun)
752     `(defun ,name (stream eof-errorp eof-value)
753     (do ((current (concatenated-stream-current stream) (cdr current)))
754     ((null current)
755     (eof-or-lose stream eof-errorp eof-value))
756     (let* ((stream (car current))
757     (result (,fun stream nil nil)))
758     (when result (return result)))
759     (setf (concatenated-stream-current stream) current)))))
760     (in-fun concatenated-in read-char)
761     (in-fun concatenated-bin read-byte))
762    
763     (defun concatenated-misc (stream operation &optional arg1 arg2)
764 ram 1.18 (let ((left (concatenated-stream-current stream)))
765     (when left
766     (let* ((current (car left))
767     (misc (stream-misc current)))
768     (case operation
769     (:listen
770     (loop
771     (let ((stuff (funcall misc current :listen)))
772     (cond ((eq stuff :eof)
773     ;; Advance current, and try again.
774     (pop (concatenated-stream-current stream))
775     (setf current
776     (car (concatenated-stream-current stream)))
777     (unless current
778     ;; No further streams. EOF.
779     (return :eof))
780     (setf misc (stream-misc current)))
781     (stuff
782     ;; Stuff's available.
783     (return t))
784     (t
785     ;; Nothing available yet.
786     (return nil))))))
787     (:close
788     (set-closed-flame stream))
789     (t
790     (funcall misc current operation arg1 arg2)))))))
791 ram 1.1
792     ;;;; Echo Streams:
793    
794     (defstruct (echo-stream
795     (:include two-way-stream
796     (in #'echo-in)
797     (bin #'echo-bin)
798     (misc #'echo-misc)
799     (n-bin #'ill-bin))
800     (:print-function %print-echo-stream)
801 wlott 1.8 (:constructor make-echo-stream (input-stream output-stream)))
802     unread-stuff)
803 ram 1.1
804    
805     (macrolet ((in-fun (name fun out-slot &rest args)
806     `(defun ,name (stream ,@args)
807 wlott 1.8 (or (pop (echo-stream-unread-stuff stream))
808     (let* ((in (echo-stream-input-stream stream))
809     (out (echo-stream-output-stream stream))
810     (result (,fun in ,@args)))
811     (funcall (,out-slot out) out result)
812     result)))))
813 ram 1.1 (in-fun echo-in read-char stream-out eof-errorp eof-value)
814     (in-fun echo-bin read-byte stream-bout eof-errorp eof-value))
815    
816     (defun echo-misc (stream operation &optional arg1 arg2)
817     (let* ((in (two-way-stream-input-stream stream))
818     (in-method (stream-misc in))
819     (out (two-way-stream-output-stream stream))
820     (out-method (stream-misc out)))
821     (case operation
822 wlott 1.8 (:listen (or (not (null (echo-stream-unread-stuff stream)))
823     (/= (the fixnum (stream-in-index in)) in-buffer-length)
824 ram 1.1 (funcall in-method in :listen)))
825 wlott 1.8 (:unread (push arg1 (echo-stream-unread-stuff stream)))
826 ram 1.1 (:element-type
827     (let ((in-type (funcall in-method in :element-type))
828     (out-type (funcall out-method out :element-type)))
829     (if (equal in-type out-type)
830     in-type `(and ,in-type ,out-type))))
831     (:close
832     (set-closed-flame stream))
833     (t
834     (or (funcall in-method in operation arg1 arg2)
835     (funcall out-method out operation arg1 arg2))))))
836    
837     (defun %print-echo-stream (s stream d)
838     (declare (ignore d))
839     (format stream "#<Echo Stream, Input = ~S, Output = ~S>"
840     (two-way-stream-input-stream s)
841     (two-way-stream-output-stream s)))
842    
843     (setf (documentation 'make-echo-stream 'function)
844     "Returns a bidirectional stream which gets its input from Input-Stream and
845     sends its output to Output-Stream. In addition, all input is echoed to
846     the output stream")
847    
848     ;;;; String Input Streams:
849    
850     (defstruct (string-input-stream
851 ram 1.21 (:include stream
852     (in #'string-inch)
853     (bin #'string-binch)
854     (n-bin #'string-stream-read-n-bytes)
855     (misc #'string-in-misc))
856     (:print-function %print-string-input-stream)
857     ;(:constructor nil)
858     (:constructor internal-make-string-input-stream
859     (string current end)))
860 ram 1.1 (string nil :type simple-string)
861     (current nil :type fixnum)
862     (end nil :type fixnum))
863    
864     (defun %print-string-input-stream (s stream d)
865     (declare (ignore s d))
866     (write-string "#<String-Input Stream>" stream))
867    
868     (defun string-inch (stream eof-errorp eof-value)
869     (let ((string (string-input-stream-string stream))
870     (index (string-input-stream-current stream)))
871     (declare (simple-string string) (fixnum index))
872     (cond ((= index (the fixnum (string-input-stream-end stream)))
873     (eof-or-lose stream eof-errorp eof-value))
874     (t
875     (setf (string-input-stream-current stream) (1+ index))
876     (aref string index)))))
877 ram 1.21
878     (defun string-binch (stream eof-errorp eof-value)
879     (let ((string (string-input-stream-string stream))
880     (index (string-input-stream-current stream)))
881     (declare (simple-string string) (fixnum index))
882     (cond ((= index (the fixnum (string-input-stream-end stream)))
883     (eof-or-lose stream eof-errorp eof-value))
884     (t
885     (setf (string-input-stream-current stream) (1+ index))
886     (char-code (aref string index))))))
887    
888     (defun string-stream-read-n-bytes (stream buffer start requested eof-errorp)
889     (declare (type string-input-stream stream)
890     (type index start requested))
891     (let ((string (string-input-stream-string stream))
892     (index (string-input-stream-current stream))
893     (end (string-input-stream-end stream)))
894     (declare (simple-string string) (fixnum index end))
895     (cond ((>= (+ index requested) end)
896     (eof-or-lose stream eof-errorp nil))
897     (t
898     (setf (string-input-stream-current stream) (+ index requested))
899     (system:without-gcing
900     (system-area-copy (vector-sap string)
901     (* index vm:byte-bits)
902     (if (typep buffer 'system-area-pointer)
903     buffer
904     (vector-sap buffer))
905     (* start vm:byte-bits)
906     (* requested vm:byte-bits)))
907     requested))))
908 ram 1.1
909     (defun string-in-misc (stream operation &optional arg1 arg2)
910 ram 1.18 (declare (ignore arg2))
911 ram 1.1 (case operation
912 ram 1.2 (:file-position
913 ram 1.5 (if arg1
914     (setf (string-input-stream-current stream) arg1)
915     (string-input-stream-current stream)))
916     (:file-length (length (string-input-stream-string stream)))
917 ram 1.1 (:unread (decf (string-input-stream-current stream)))
918 ram 1.10 (:listen (or (/= (the fixnum (string-input-stream-current stream))
919     (the fixnum (string-input-stream-end stream)))
920     :eof))
921 ram 1.1 (:element-type 'string-char)))
922    
923     (defun make-string-input-stream (string &optional
924     (start 0) (end (length string)))
925     "Returns an input stream which will supply the characters of String between
926     Start and End in order."
927 wlott 1.17 (declare (type string string)
928     (type index start)
929 ram 1.18 (type (or index null) end))
930 wlott 1.17 (internal-make-string-input-stream (coerce string 'simple-string)
931     start end))
932 ram 1.1
933     ;;;; String Output Streams:
934    
935     (defstruct (string-output-stream
936     (:include stream
937     (out #'string-ouch)
938     (sout #'string-sout)
939     (misc #'string-out-misc))
940     (:print-function %print-string-output-stream)
941     (:constructor make-string-output-stream ()))
942     ;; The string we throw stuff in.
943     (string (make-string 40) :type simple-string)
944     ;; Index of the next location to use.
945     (index 0 :type fixnum))
946    
947     (defun %print-string-output-stream (s stream d)
948     (declare (ignore s d))
949     (write-string "#<String-Output Stream>" stream))
950    
951     (setf (documentation 'make-string-output-stream 'function)
952     "Returns an Output stream which will accumulate all output given it for
953     the benefit of the function Get-Output-Stream-String.")
954    
955     (defun string-ouch (stream character)
956     (let ((current (string-output-stream-index stream))
957     (workspace (string-output-stream-string stream)))
958     (declare (simple-string workspace) (fixnum current))
959     (if (= current (the fixnum (length workspace)))
960     (let ((new-workspace (make-string (* current 2))))
961 wlott 1.17 (replace new-workspace workspace)
962 ram 1.1 (setf (aref new-workspace current) character)
963     (setf (string-output-stream-string stream) new-workspace))
964     (setf (aref workspace current) character))
965     (setf (string-output-stream-index stream) (1+ current))))
966    
967     (defun string-sout (stream string start end)
968     (declare (simple-string string) (fixnum start end))
969     (let* ((current (string-output-stream-index stream))
970     (length (- end start))
971     (dst-end (+ length current))
972     (workspace (string-output-stream-string stream)))
973     (declare (simple-string workspace)
974     (fixnum current length dst-end))
975     (if (> dst-end (the fixnum (length workspace)))
976     (let ((new-workspace (make-string (+ (* current 2) length))))
977 wlott 1.17 (replace new-workspace workspace :end2 current)
978     (replace new-workspace string
979     :start1 current :end1 dst-end
980     :start2 start :end2 end)
981 ram 1.1 (setf (string-output-stream-string stream) new-workspace))
982 wlott 1.17 (replace workspace string
983     :start1 current :end1 dst-end
984     :start2 start :end2 end))
985 ram 1.1 (setf (string-output-stream-index stream) dst-end)))
986    
987     (defun string-out-misc (stream operation &optional arg1 arg2)
988 ram 1.3 (declare (ignore arg2))
989 ram 1.1 (case operation
990 ram 1.2 (:file-position
991     (if (null arg1)
992 ram 1.3 (string-output-stream-index stream)))
993 ram 1.1 (:charpos
994     (do ((index (1- (the fixnum (string-output-stream-index stream)))
995     (1- index))
996     (count 0 (1+ count))
997     (string (string-output-stream-string stream)))
998     ((< index 0) count)
999     (declare (simple-string string)
1000     (fixnum index count))
1001     (if (char= (schar string index) #\newline)
1002     (return count))))
1003     (:element-type 'string-char)))
1004    
1005     (defun get-output-stream-string (stream)
1006     "Returns a string of all the characters sent to a stream made by
1007     Make-String-Output-Stream since the last call to this function."
1008 wlott 1.17 (declare (type string-output-stream stream))
1009     (let* ((length (string-output-stream-index stream))
1010     (result (make-string length)))
1011     (replace result (string-output-stream-string stream))
1012     (setf (string-output-stream-index stream) 0)
1013     result))
1014 ram 1.1
1015     (defun dump-output-stream-string (in-stream out-stream)
1016     "Dumps the characters buffer up in the In-Stream to the Out-Stream as
1017     Get-Output-Stream-String would return them."
1018     (write-string (string-output-stream-string in-stream) out-stream
1019     :start 0 :end (string-output-stream-index in-stream))
1020     (setf (string-output-stream-index in-stream) 0))
1021    
1022     ;;;; Fill-pointer streams:
1023     ;;;
1024     ;;; Fill pointer string output streams are not explicitly mentioned in
1025     ;;; the CLM, but they are required for the implementation of With-Output-To-String.
1026    
1027     (defstruct (fill-pointer-output-stream
1028     (:include stream
1029     (out #'fill-pointer-ouch)
1030     (sout #'fill-pointer-sout)
1031     (misc #'fill-pointer-misc))
1032     (:print-function
1033     (lambda (s stream d)
1034     (declare (ignore s d))
1035     (write-string "#<Fill-Pointer String Output Stream>" stream)))
1036     (:constructor make-fill-pointer-output-stream (string)))
1037     ;; The string we throw stuff in.
1038     string)
1039    
1040    
1041     (defun fill-pointer-ouch (stream character)
1042     (let* ((buffer (fill-pointer-output-stream-string stream))
1043 wlott 1.4 (current (fill-pointer buffer))
1044 ram 1.1 (current+1 (1+ current)))
1045     (declare (fixnum current))
1046     (with-array-data ((workspace buffer) (start) (end))
1047     (declare (simple-string workspace))
1048     (let ((offset-current (+ start current)))
1049     (declare (fixnum offset-current))
1050     (if (= offset-current end)
1051     (let* ((new-length (* current 2))
1052     (new-workspace (make-string new-length)))
1053     (declare (simple-string new-workspace))
1054     (%primitive byte-blt workspace start new-workspace 0 current)
1055     (setf workspace new-workspace)
1056     (setf offset-current current)
1057     (set-array-header buffer workspace new-length
1058     current+1 0 new-length nil))
1059 wlott 1.4 (setf (fill-pointer buffer) current+1))
1060 ram 1.1 (setf (schar workspace offset-current) character)))
1061     current+1))
1062    
1063    
1064     (defun fill-pointer-sout (stream string start end)
1065     (declare (simple-string string) (fixnum start end))
1066     (let* ((buffer (fill-pointer-output-stream-string stream))
1067 wlott 1.4 (current (fill-pointer buffer))
1068 ram 1.1 (string-len (- end start))
1069     (dst-end (+ string-len current)))
1070     (declare (fixnum current dst-end string-len))
1071     (with-array-data ((workspace buffer) (dst-start) (dst-length))
1072     (declare (simple-string workspace))
1073     (let ((offset-dst-end (+ dst-start dst-end))
1074     (offset-current (+ dst-start current)))
1075     (declare (fixnum offset-dst-end offset-current))
1076     (if (> offset-dst-end dst-length)
1077     (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1078     (new-workspace (make-string new-length)))
1079     (declare (simple-string new-workspace))
1080     (%primitive byte-blt workspace dst-start new-workspace 0 current)
1081     (setf workspace new-workspace)
1082     (setf offset-current current)
1083     (setf offset-dst-end dst-end)
1084     (set-array-header buffer workspace new-length
1085     dst-end 0 new-length nil))
1086 wlott 1.4 (setf (fill-pointer buffer) dst-end))
1087 ram 1.1 (%primitive byte-blt string start
1088     workspace offset-current offset-dst-end)))
1089     dst-end))
1090    
1091    
1092     (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1093     (declare (ignore arg1 arg2))
1094     (case operation
1095     (:charpos
1096     (let* ((buffer (fill-pointer-output-stream-string stream))
1097 wlott 1.4 (current (fill-pointer buffer)))
1098 ram 1.1 (with-array-data ((string buffer) (start) (end current))
1099     (declare (simple-string string) (ignore start))
1100     (let ((found (position #\newline string :test #'char=
1101     :end end :from-end t)))
1102     (if found
1103     (- end (the fixnum found))
1104     current)))))
1105     (:element-type 'string-char)))
1106    
1107     ;;;; Indenting streams:
1108    
1109     (defstruct (indenting-stream (:include stream
1110     (out #'indenting-out)
1111     (sout #'indenting-sout)
1112     (misc #'indenting-misc))
1113     (:print-function %print-indenting-stream)
1114     (:constructor make-indenting-stream (stream)))
1115     ;; The stream we're based on:
1116     stream
1117     ;; How much we indent on each line:
1118     (indentation 0))
1119    
1120     (setf (documentation 'make-indenting-stream 'function)
1121     "Returns an ouput stream which indents its output by some amount.")
1122    
1123     (defun %print-indenting-stream (s stream d)
1124     (declare (ignore s d))
1125     (write-string "#<Indenting Stream>" stream))
1126    
1127     ;;; Indenting-Indent writes the right number of spaces needed to indent output on
1128     ;;; the given Stream based on the specified Sub-Stream.
1129    
1130     (defmacro indenting-indent (stream sub-stream)
1131     `(do ((i 0 (+ i 60))
1132     (indentation (indenting-stream-indentation ,stream)))
1133     ((>= i indentation))
1134     (funcall (stream-sout ,sub-stream) ,sub-stream
1135     " "
1136     0 (min 60 (- indentation i)))))
1137    
1138     ;;; Indenting-Out writes a character to an indenting stream.
1139    
1140     (defun indenting-out (stream char)
1141     (let ((sub-stream (indenting-stream-stream stream)))
1142     (funcall (stream-out sub-stream) sub-stream char)
1143     (if (char= char #\newline)
1144     (indenting-indent stream sub-stream))))
1145    
1146     ;;; Indenting-Sout writes a string to an indenting stream.
1147    
1148     (defun indenting-sout (stream string start end)
1149     (declare (simple-string string) (fixnum start end))
1150     (do ((i start)
1151     (sub-stream (indenting-stream-stream stream)))
1152     ((= i end))
1153     (let ((newline (position #\newline string :start i :end end)))
1154     (cond (newline
1155     (funcall (stream-sout sub-stream) sub-stream string i (1+ newline))
1156     (indenting-indent stream sub-stream)
1157     (setq i (+ newline 1)))
1158     (t
1159     (funcall (stream-sout sub-stream) sub-stream string i end)
1160     (setq i end))))))
1161    
1162     ;;; Indenting-Misc just treats just the :Line-Length message differently.
1163     ;;; Indenting-Charpos says the charpos is the charpos of the base stream minus
1164     ;;; the stream's indentation.
1165    
1166     (defun indenting-misc (stream operation &optional arg1 arg2)
1167     (let* ((sub-stream (indenting-stream-stream stream))
1168     (method (stream-misc sub-stream)))
1169     (case operation
1170     (:line-length
1171     (let ((line-length (funcall method sub-stream operation)))
1172     (if line-length
1173     (- line-length (indenting-stream-indentation stream)))))
1174     (:charpos
1175     (let* ((sub-stream (indenting-stream-stream stream))
1176     (charpos (funcall method sub-stream operation)))
1177     (if charpos
1178     (- charpos (indenting-stream-indentation stream)))))
1179     (t
1180     (funcall method sub-stream operation arg1 arg2)))))
1181    
1182 wlott 1.13 (proclaim '(maybe-inline read-char unread-char read-byte listen))
1183    
1184    
1185    
1186     ;;;; Case frobbing streams, used by format ~(...~).
1187    
1188     (defstruct (case-frob-stream
1189     (:include stream
1190     (:misc #'case-frob-misc))
1191     (:constructor %make-case-frob-stream (target out sout)))
1192     (target (required-argument) :type stream))
1193    
1194     (defun make-case-frob-stream (target kind)
1195     "Returns a stream that sends all output to the stream TARGET, but modifies
1196     the case of letters, depending on KIND, which should be one of:
1197     :upcase - convert to upper case.
1198     :downcase - convert to lower case.
1199     :capitalize - convert the first letter of words to upper case and the
1200     rest of the word to lower case.
1201     :capitalize-first - convert the first letter of the first word to upper
1202     case and everything else to lower case."
1203     (declare (type stream target)
1204     (type (member :upcase :downcase :capitalize :capitalize-first)
1205     kind)
1206     (values stream))
1207     (if (case-frob-stream-p target)
1208     ;; If we are going to be writing to a stream that already does case
1209     ;; frobbing, why bother frobbing the case just so it can frob it
1210     ;; again?
1211     target
1212     (multiple-value-bind
1213     (out sout)
1214     (ecase kind
1215     (:upcase
1216     (values #'case-frob-upcase-out
1217     #'case-frob-upcase-sout))
1218     (:downcase
1219     (values #'case-frob-downcase-out
1220     #'case-frob-downcase-sout))
1221     (:capitalize
1222     (values #'case-frob-capitalize-out
1223     #'case-frob-capitalize-sout))
1224     (:capitalize-first
1225     (values #'case-frob-capitalize-first-out
1226     #'case-frob-capitalize-first-sout)))
1227     (%make-case-frob-stream target out sout))))
1228    
1229     (defun case-frob-misc (stream op &optional arg1 arg2)
1230     (declare (type case-frob-stream stream))
1231     (case op
1232     (:close)
1233     (t
1234     (let ((target (case-frob-stream-target stream)))
1235     (funcall (stream-misc target) target op arg1 arg2)))))
1236    
1237    
1238     (defun case-frob-upcase-out (stream char)
1239     (declare (type case-frob-stream stream)
1240 ram 1.16 (type base-char char))
1241 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1242     (funcall (stream-out target) target (char-upcase char))))
1243    
1244     (defun case-frob-upcase-sout (stream str start end)
1245     (declare (type case-frob-stream stream)
1246     (type simple-base-string str)
1247     (type index start)
1248     (type (or index null) end))
1249     (let* ((target (case-frob-stream-target stream))
1250     (len (length str))
1251     (end (or end len)))
1252     (funcall (stream-sout target) target
1253     (if (and (zerop start) (= len end))
1254     (string-upcase str)
1255     (nstring-upcase (subseq str start end)))
1256     0
1257     (- end start))))
1258    
1259     (defun case-frob-downcase-out (stream char)
1260     (declare (type case-frob-stream stream)
1261 ram 1.16 (type base-char char))
1262 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1263     (funcall (stream-out target) target (char-downcase char))))
1264    
1265     (defun case-frob-downcase-sout (stream str start end)
1266     (declare (type case-frob-stream stream)
1267     (type simple-base-string str)
1268     (type index start)
1269     (type (or index null) end))
1270     (let* ((target (case-frob-stream-target stream))
1271     (len (length str))
1272     (end (or end len)))
1273     (funcall (stream-sout target) target
1274     (if (and (zerop start) (= len end))
1275     (string-downcase str)
1276     (nstring-downcase (subseq str start end)))
1277     0
1278     (- end start))))
1279    
1280     (defun case-frob-capitalize-out (stream char)
1281     (declare (type case-frob-stream stream)
1282 ram 1.16 (type base-char char))
1283 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1284     (cond ((alphanumericp char)
1285     (funcall (stream-out target) target (char-upcase char))
1286     (setf (case-frob-stream-out stream)
1287     #'case-frob-capitalize-aux-out)
1288     (setf (case-frob-stream-sout stream)
1289     #'case-frob-capitalize-aux-sout))
1290     (t
1291     (funcall (stream-out target) target char)))))
1292    
1293     (defun case-frob-capitalize-sout (stream str start end)
1294     (declare (type case-frob-stream stream)
1295     (type simple-base-string str)
1296     (type index start)
1297     (type (or index null) end))
1298     (let* ((target (case-frob-stream-target stream))
1299     (str (subseq str start end))
1300     (len (length str))
1301     (inside-word nil))
1302     (dotimes (i len)
1303     (let ((char (schar str i)))
1304     (cond ((not (alphanumericp char))
1305     (setf inside-word nil))
1306     (inside-word
1307     (setf (schar str i) (char-downcase char)))
1308     (t
1309     (setf inside-word t)
1310     (setf (schar str i) (char-upcase char))))))
1311     (when inside-word
1312     (setf (case-frob-stream-out stream)
1313     #'case-frob-capitalize-aux-out)
1314     (setf (case-frob-stream-sout stream)
1315     #'case-frob-capitalize-aux-sout))
1316     (funcall (stream-sout target) target str 0 len)))
1317    
1318     (defun case-frob-capitalize-aux-out (stream char)
1319     (declare (type case-frob-stream stream)
1320 ram 1.16 (type base-char char))
1321 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1322     (cond ((alphanumericp char)
1323     (funcall (stream-out target) target (char-downcase char)))
1324     (t
1325     (funcall (stream-out target) target char)
1326     (setf (case-frob-stream-out stream)
1327     #'case-frob-capitalize-out)
1328     (setf (case-frob-stream-sout stream)
1329     #'case-frob-capitalize-sout)))))
1330    
1331     (defun case-frob-capitalize-aux-sout (stream str start end)
1332     (declare (type case-frob-stream stream)
1333     (type simple-base-string str)
1334     (type index start)
1335     (type (or index null) end))
1336     (let* ((target (case-frob-stream-target stream))
1337     (str (subseq str start end))
1338     (len (length str))
1339     (inside-word t))
1340     (dotimes (i len)
1341     (let ((char (schar str i)))
1342     (cond ((not (alphanumericp char))
1343     (setf inside-word nil))
1344     (inside-word
1345     (setf (schar str i) (char-downcase char)))
1346     (t
1347     (setf inside-word t)
1348     (setf (schar str i) (char-upcase char))))))
1349     (unless inside-word
1350     (setf (case-frob-stream-out stream)
1351     #'case-frob-capitalize-out)
1352     (setf (case-frob-stream-sout stream)
1353     #'case-frob-capitalize-sout))
1354     (funcall (stream-sout target) target str 0 len)))
1355    
1356     (defun case-frob-capitalize-first-out (stream char)
1357     (declare (type case-frob-stream stream)
1358 ram 1.16 (type base-char char))
1359 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1360     (cond ((alphanumericp char)
1361     (funcall (stream-out target) target (char-upcase char))
1362     (setf (case-frob-stream-out stream)
1363     #'case-frob-downcase-out)
1364     (setf (case-frob-stream-sout stream)
1365     #'case-frob-downcase-sout))
1366     (t
1367     (funcall (stream-out target) target char)))))
1368    
1369     (defun case-frob-capitalize-first-sout (stream str start end)
1370     (declare (type case-frob-stream stream)
1371     (type simple-base-string str)
1372     (type index start)
1373     (type (or index null) end))
1374     (let* ((target (case-frob-stream-target stream))
1375     (str (subseq str start end))
1376     (len (length str)))
1377     (dotimes (i len)
1378     (let ((char (schar str i)))
1379     (when (alphanumericp char)
1380     (setf (schar str i) (char-upcase char))
1381     (do ((i (1+ i) (1+ i)))
1382     ((= i len))
1383     (setf (schar str i) (char-downcase (schar str i))))
1384     (setf (case-frob-stream-out stream)
1385     #'case-frob-downcase-out)
1386     (setf (case-frob-stream-sout stream)
1387     #'case-frob-downcase-sout)
1388     (return))))
1389     (funcall (stream-sout target) target str 0 len)))
1390 chiles 1.12
1391    
1392     ;;;; Public interface from "EXTENSIONS" package.
1393    
1394     (defstruct (stream-command (:print-function print-stream-command)
1395     (:constructor make-stream-command
1396     (name &optional args)))
1397     (name nil :type symbol)
1398     (args nil :type list))
1399    
1400     (defun print-stream-command (obj str n)
1401     (declare (ignore n))
1402     (format str "#<Stream-Cmd ~S>" (stream-command-name obj)))
1403    
1404    
1405     ;;; GET-STREAM-COMMAND -- Public.
1406     ;;;
1407     ;;; We can't simply call the stream's misc method because because nil is an
1408     ;;; ambiguous return value: does it mean text arrived, or does it mean the
1409     ;;; stream's misc method had no :get-command implementation. We can't return
1410     ;;; nil until there is text input. We don't need to loop because any stream
1411     ;;; implementing :get-command would wait until it had some input. If the
1412     ;;; LISTEN fails, then we have some random stream we must wait on.
1413     ;;;
1414     (defun get-stream-command (stream)
1415     "This takes a stream and waits for text or a command to appear on it. If
1416     text appears before a command, this returns nil, and otherwise it returns
1417     a command."
1418     (let ((cmdp (funcall (lisp::stream-misc stream) stream :get-command)))
1419     (cond (cmdp)
1420     ((listen stream)
1421     nil)
1422     (t
1423     ;; This waits for input and returns nil when it arrives.
1424     (unread-char (read-char stream) stream)))))

  ViewVC Help
Powered by ViewVC 1.1.5