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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5