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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations)
Thu Jul 16 13:30:52 1998 UTC (15 years, 9 months ago) by pw
Branch: MAIN
Changes since 1.33: +21 -5 lines
Fixes and improvements to July-14 commit dealing with condition types
in calls to error. From Douglas.
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 pw 1.34 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/stream.lisp,v 1.34 1998/07/16 13:30:52 pw 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.32 (let ((stream (in-synonym-of stream)))
352 dtc 1.33 (if (lisp-stream-p stream)
353     (let ((char (read-char stream eof-errorp eof-value)))
354     (cond ((eq char eof-value) char)
355     ((characterp peek-type)
356     (do ((char char (read-char stream eof-errorp eof-value)))
357     ((or (eq char eof-value) (char= char peek-type))
358     (unless (eq char eof-value)
359     (unread-char char stream))
360     char)))
361     ((eq peek-type t)
362     (do ((char char (read-char stream eof-errorp eof-value)))
363     ((or (eq char eof-value) (not (whitespace-char-p char)))
364     (unless (eq char eof-value)
365     (unread-char char stream))
366     char)))
367     (t
368     (unread-char char stream)
369     char)))
370     ;; Fundamental-stream.
371     (cond ((characterp peek-type)
372     (do ((char (stream-read-char stream) (stream-read-char stream)))
373     ((or (eq char :eof) (char= char peek-type))
374     (cond ((eq char :eof)
375     (eof-or-lose stream eof-errorp eof-value))
376     (t
377     (stream-unread-char stream char)
378     char)))))
379     ((eq peek-type t)
380     (do ((char (stream-read-char stream) (stream-read-char stream)))
381     ((or (eq char :eof) (not (whitespace-char-p char)))
382     (cond ((eq char :eof)
383     (eof-or-lose stream eof-errorp eof-value))
384     (t
385     (stream-unread-char stream char)
386     char)))))
387     (t
388     (let ((char (stream-peek-char stream)))
389     (if (eq char :eof)
390     (eof-or-lose stream eof-errorp eof-value)
391     char)))))))
392 ram 1.1
393     (defun listen (&optional (stream *standard-input*))
394     "Returns T if a character is availible on the given Stream."
395 dtc 1.32 (let ((stream (in-synonym-of stream)))
396 dtc 1.33 (if (lisp-stream-p stream)
397     (or (/= (the fixnum (lisp-stream-in-index stream)) in-buffer-length)
398     ;; Test for t explicitly since misc methods return :eof sometimes.
399     (eq (funcall (lisp-stream-misc stream) stream :listen) t))
400     ;; Fundamental-stream.
401     (stream-listen stream))))
402 ram 1.1
403     (defun read-char-no-hang (&optional (stream *standard-input*)
404     (eof-errorp t) eof-value recursive-p)
405     "Returns the next character from the Stream if one is availible, or nil."
406     (declare (ignore recursive-p))
407 dtc 1.32 (let ((stream (in-synonym-of stream)))
408 dtc 1.33 (if (lisp-stream-p stream)
409     (if (funcall (lisp-stream-misc stream) stream :listen)
410     ;; On t or :eof get READ-CHAR to do the work.
411     (read-char stream eof-errorp eof-value)
412     nil)
413     ;; Fundamental-stream.
414     (let ((char (stream-read-char-no-hang stream)))
415     (if (eq char :eof)
416     (eof-or-lose stream eof-errorp eof-value)
417     char)))))
418 dtc 1.30
419 ram 1.1
420     (defun clear-input (&optional (stream *standard-input*))
421     "Clears any buffered input associated with the Stream."
422 dtc 1.32 (let ((stream (in-synonym-of stream)))
423 dtc 1.33 (cond ((lisp-stream-p stream)
424     (setf (lisp-stream-in-index stream) in-buffer-length)
425     (funcall (lisp-stream-misc stream) stream :clear-input))
426     (t
427     (stream-clear-input stream))))
428 dtc 1.30 nil)
429 ram 1.1
430     (defun read-byte (stream &optional (eof-errorp t) eof-value)
431     "Returns the next byte of the Stream."
432 dtc 1.32 (let ((stream (in-synonym-of stream)))
433 dtc 1.33 (if (lisp-stream-p stream)
434     (prepare-for-fast-read-byte stream
435     (prog1
436     (fast-read-byte eof-errorp eof-value t)
437     (done-with-fast-read-byte)))
438     ;; Fundamental-stream.
439     (let ((char (stream-read-byte stream)))
440     (if (eq char :eof)
441     (eof-or-lose stream eof-errorp eof-value)
442     char)))))
443 ram 1.1
444     (defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
445 ram 1.24 "Reads Numbytes bytes into the Buffer starting at Start, returning the number
446     of bytes read.
447     -- If EOF-ERROR-P is true, an END-OF-FILE condition is signalled if
448     end-of-file is encountered before Count bytes have been read.
449     -- If EOF-ERROR-P is false, READ-N-BYTES reads as much data is currently
450     available (up to count bytes.) On pipes or similar devices, this
451     function returns as soon as any adata is available, even if the amount
452     read is less than Count and eof has not been hit."
453 dtc 1.30 (declare (type lisp-stream stream)
454     (type index numbytes start)
455 ram 1.18 (type (or (simple-array * (*)) system-area-pointer) buffer))
456 dtc 1.32 (let* ((stream (in-synonym-of stream lisp-stream))
457 dtc 1.30 (in-buffer (lisp-stream-in-buffer stream))
458     (index (lisp-stream-in-index stream))
459 ram 1.1 (num-buffered (- in-buffer-length index)))
460     (declare (fixnum index num-buffered))
461     (cond
462 ram 1.2 ((not in-buffer)
463 dtc 1.30 (funcall (lisp-stream-n-bin stream) stream buffer start numbytes eof-errorp))
464 ram 1.1 ((<= numbytes num-buffered)
465     (%primitive byte-blt in-buffer index buffer start (+ start numbytes))
466 dtc 1.30 (setf (lisp-stream-in-index stream) (+ index numbytes))
467 ram 1.1 numbytes)
468     (t
469     (let ((end (+ start num-buffered)))
470     (%primitive byte-blt in-buffer index buffer start end)
471 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
472     (+ (funcall (lisp-stream-n-bin stream) stream buffer end
473 ram 1.18 (- numbytes num-buffered)
474     eof-errorp)
475 ram 1.1 num-buffered))))))
476 ram 1.18
477    
478     ;;; Amount of space we leave at the start of the in-buffer for unreading. 4
479     ;;; instead of 1 to allow word-aligned copies.
480     ;;;
481     (defconstant in-buffer-extra 4)
482    
483     ;;; FAST-READ-CHAR-REFILL -- Interface
484     ;;;
485     ;;; This function is called by the fast-read-char expansion to refill the
486     ;;; in-buffer for text streams. There is definitely an in-buffer, and hence
487     ;;; myst be an n-bin method.
488     ;;;
489     (defun fast-read-char-refill (stream eof-errorp eof-value)
490 dtc 1.30 (let* ((ibuf (lisp-stream-in-buffer stream))
491     (count (funcall (lisp-stream-n-bin stream) stream
492 ram 1.18 ibuf in-buffer-extra
493     (- in-buffer-length in-buffer-extra)
494     nil))
495     (start (- in-buffer-length count)))
496     (declare (type index start count))
497     (cond ((zerop count)
498 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
499     (funcall (lisp-stream-in stream) stream eof-errorp eof-value))
500 ram 1.18 (t
501     (when (/= start in-buffer-extra)
502     (bit-bash-copy ibuf (+ (* in-buffer-extra vm:byte-bits)
503     (* vm:vector-data-offset vm:word-bits))
504     ibuf (+ (the index (* start vm:byte-bits))
505     (* vm:vector-data-offset vm:word-bits))
506     (* count vm:byte-bits)))
507 dtc 1.30 (setf (lisp-stream-in-index stream) (1+ start))
508 ram 1.18 (code-char (aref ibuf start))))))
509    
510    
511     ;;; FAST-READ-BYTE-REFILL -- Interface
512     ;;;
513     ;;; Similar to FAST-READ-CHAR-REFILL, but we don't have to leave room for
514     ;;; unreading.
515     ;;;
516     (defun fast-read-byte-refill (stream eof-errorp eof-value)
517 dtc 1.30 (let* ((ibuf (lisp-stream-in-buffer stream))
518     (count (funcall (lisp-stream-n-bin stream) stream
519 ram 1.18 ibuf 0 in-buffer-length
520     nil))
521     (start (- in-buffer-length count)))
522     (declare (type index start count))
523     (cond ((zerop count)
524 dtc 1.30 (setf (lisp-stream-in-index stream) in-buffer-length)
525     (funcall (lisp-stream-bin stream) stream eof-errorp eof-value))
526 ram 1.18 (t
527     (unless (zerop start)
528     (bit-bash-copy ibuf (* vm:vector-data-offset vm:word-bits)
529     ibuf (+ (the index (* start vm:byte-bits))
530     (* vm:vector-data-offset vm:word-bits))
531     (* count vm:byte-bits)))
532 dtc 1.30 (setf (lisp-stream-in-index stream) (1+ start))
533 ram 1.18 (aref ibuf start)))))
534    
535 ram 1.1
536     ;;; Output functions:
537    
538     (defun write-char (character &optional (stream *standard-output*))
539     "Outputs the Character to the Stream."
540 dtc 1.32 (with-out-stream stream (lisp-stream-out character)
541     (stream-write-char character))
542 ram 1.1 character)
543    
544     (defun terpri (&optional (stream *standard-output*))
545     "Outputs a new line to the Stream."
546 dtc 1.32 (with-out-stream stream (lisp-stream-out #\newline) (stream-terpri))
547 ram 1.1 nil)
548    
549     (defun fresh-line (&optional (stream *standard-output*))
550     "Outputs a new line to the Stream if it is not positioned at the begining of
551     a line. Returns T if it output a new line, nil otherwise."
552 dtc 1.32 (let ((stream (out-synonym-of stream)))
553 dtc 1.33 (if (lisp-stream-p stream)
554     (when (/= (or (charpos stream) 1) 0)
555     (funcall (lisp-stream-out stream) stream #\newline)
556     t)
557     ;; Fundamental-stream.
558     (stream-fresh-line stream))))
559 ram 1.1
560     (defun write-string (string &optional (stream *standard-output*)
561     &key (start 0) (end (length (the vector string))))
562     "Outputs the String to the given Stream."
563     (write-string* string stream start end))
564    
565     (defun write-string* (string &optional (stream *standard-output*)
566     (start 0) (end (length (the vector string))))
567     (declare (fixnum start end))
568 dtc 1.32 (let ((stream (out-synonym-of stream)))
569 dtc 1.33 (cond ((lisp-stream-p stream)
570     (if (array-header-p string)
571     (with-array-data ((data string) (offset-start start)
572     (offset-end end))
573     (funcall (lisp-stream-sout stream)
574     stream data offset-start offset-end))
575     (funcall (lisp-stream-sout stream) stream string start end))
576     string)
577     (t ; Fundamental-stream.
578     (stream-write-string stream string start end)))))
579 ram 1.1
580     (defun write-line (string &optional (stream *standard-output*)
581     &key (start 0) (end (length string)))
582     "Outputs the String to the given Stream, followed by a newline character."
583     (write-line* string stream start end))
584    
585     (defun write-line* (string &optional (stream *standard-output*)
586     (start 0) (end (length string)))
587     (declare (fixnum start end))
588 dtc 1.32 (let ((stream (out-synonym-of stream)))
589 dtc 1.33 (cond ((lisp-stream-p stream)
590     (if (array-header-p string)
591     (with-array-data ((data string) (offset-start start)
592     (offset-end end))
593     (with-out-stream stream (lisp-stream-sout data offset-start
594     offset-end)))
595     (with-out-stream stream (lisp-stream-sout string start end)))
596     (funcall (lisp-stream-out stream) stream #\newline))
597     (t ; Fundamental-stream.
598     (stream-write-string stream string start end)
599     (stream-write-char stream #\Newline)))
600 dtc 1.30 string))
601 ram 1.1
602     (defun charpos (&optional (stream *standard-output*))
603     "Returns the number of characters on the current line of output of the given
604     Stream, or Nil if that information is not availible."
605 dtc 1.32 (with-out-stream stream (lisp-stream-misc :charpos) (stream-line-column)))
606 ram 1.1
607     (defun line-length (&optional (stream *standard-output*))
608     "Returns the number of characters that will fit on a line of output on the
609     given Stream, or Nil if that information is not available."
610 dtc 1.32 (with-out-stream stream (lisp-stream-misc :line-length)
611     (stream-line-length)))
612 ram 1.1
613     (defun finish-output (&optional (stream *standard-output*))
614 dtc 1.28 "Attempts to ensure that all output sent to the Stream has reached its
615 ram 1.1 destination, and only then returns."
616 dtc 1.32 (with-out-stream stream (lisp-stream-misc :finish-output)
617     (stream-finish-output))
618 ram 1.1 nil)
619    
620     (defun force-output (&optional (stream *standard-output*))
621     "Attempts to force any buffered output to be sent."
622 dtc 1.32 (with-out-stream stream (lisp-stream-misc :force-output)
623     (stream-force-output))
624 ram 1.1 nil)
625    
626     (defun clear-output (&optional (stream *standard-output*))
627     "Clears the given output Stream."
628 dtc 1.32 (with-out-stream stream (lisp-stream-misc :clear-output)
629     (stream-force-output))
630 ram 1.1 nil)
631    
632     (defun write-byte (integer stream)
633     "Outputs the Integer to the binary Stream."
634 dtc 1.32 (with-out-stream stream (lisp-stream-bout integer) (stream-write-byte))
635 ram 1.1 integer)
636    
637 dtc 1.33
638     ;;; Stream-misc-dispatch
639     ;;;
640     ;;; Called from lisp-steam routines that encapsulate CLOS streams to
641     ;;; handle the misc routines and dispatch to the appropriate Gray
642     ;;; stream functions.
643     ;;;
644     (defun stream-misc-dispatch (stream operation &optional arg1 arg2)
645     (declare (type fundamental-stream stream)
646     (ignore arg2))
647     (case operation
648     (:listen
649     ;; Return true is input available, :eof for eof-of-file, otherwise Nil.
650     (let ((char (stream-read-char-no-hang stream)))
651     (when (characterp char)
652     (stream-unread-char stream char))
653     char))
654     (:unread
655     (stream-unread-char stream arg1))
656     (:close
657     (close stream))
658     (:clear-input
659     (stream-clear-input stream))
660     (:force-output
661     (stream-force-output stream))
662     (:finish-output
663     (stream-finish-output stream))
664     (:element-type
665     (stream-element-type stream))
666     (:interactive-p
667     (interactive-stream-p stream))
668     (:line-length
669     (stream-line-length stream))
670     (:charpos
671     (stream-line-column stream))
672     (:file-length
673     (file-length stream))
674     (:file-position
675     (file-position stream arg1))))
676    
677    
678 ram 1.1 ;;;; Broadcast streams:
679    
680 dtc 1.30 (defstruct (broadcast-stream (:include lisp-stream
681 ram 1.1 (out #'broadcast-out)
682     (bout #'broadcast-bout)
683     (sout #'broadcast-sout)
684     (misc #'broadcast-misc))
685     (:print-function %print-broadcast-stream)
686     (:constructor make-broadcast-stream (&rest streams)))
687     ;; This is a list of all the streams we broadcast to.
688 ram 1.14 (streams () :type list :read-only t))
689 ram 1.1
690     (setf (documentation 'make-broadcast-stream 'function)
691     "Returns an ouput stream which sends its output to all of the given streams.")
692    
693     (defun %print-broadcast-stream (s stream d)
694     (declare (ignore s d))
695     (write-string "#<Broadcast Stream>" stream))
696    
697 dtc 1.33 (macrolet ((out-fun (fun method stream-method &rest args)
698 ram 1.1 `(defun ,fun (stream ,@args)
699     (dolist (stream (broadcast-stream-streams stream))
700 dtc 1.33 (if (lisp-stream-p stream)
701     (funcall (,method stream) stream ,@args)
702     (,stream-method stream ,@args))))))
703     (out-fun broadcast-out lisp-stream-out stream-write-char char)
704     (out-fun broadcast-bout lisp-stream-bout stream-write-byte byte)
705     (out-fun broadcast-sout lisp-stream-sout stream-write-string
706     string start end))
707 ram 1.1
708     (defun broadcast-misc (stream operation &optional arg1 arg2)
709     (let ((streams (broadcast-stream-streams stream)))
710     (case operation
711     (:charpos
712     (dolist (stream streams)
713 dtc 1.33 (let ((charpos (charpos stream)))
714 ram 1.1 (if charpos (return charpos)))))
715     (:line-length
716     (let ((min nil))
717     (dolist (stream streams min)
718 dtc 1.33 (let ((res (line-length stream)))
719 ram 1.1 (when res (setq min (if min (min res min) res)))))))
720     (:element-type
721     (let (res)
722     (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
723 dtc 1.33 (pushnew (stream-element-type stream) res :test #'equal))))
724 wlott 1.25 (:close)
725 ram 1.1 (t
726     (let ((res nil))
727     (dolist (stream streams res)
728 dtc 1.33 (setq res
729     (if (lisp-stream-p stream)
730     (funcall (lisp-stream-misc stream) stream operation
731     arg1 arg2)
732     (stream-misc-dispatch stream operation arg1 arg2)))))))))
733    
734 ram 1.1
735     ;;;; Synonym Streams:
736    
737 dtc 1.30 (defstruct (synonym-stream (:include lisp-stream
738 ram 1.1 (in #'synonym-in)
739     (bin #'synonym-bin)
740     (n-bin #'synonym-n-bin)
741     (out #'synonym-out)
742     (bout #'synonym-bout)
743     (sout #'synonym-sout)
744     (misc #'synonym-misc))
745     (:print-function %print-synonym-stream)
746     (:constructor make-synonym-stream (symbol)))
747     ;; This is the symbol, the value of which is the stream we are synonym to.
748 wlott 1.15 (symbol nil :type symbol :read-only t))
749 ram 1.1
750     (defun %print-synonym-stream (s stream d)
751     (declare (ignore d))
752     (format stream "#<Synonym Stream to ~S>" (synonym-stream-symbol s)))
753    
754     (setf (documentation 'make-synonym-stream 'function)
755     "Returns a stream which performs its operations on the stream which is the
756     value of the dynamic variable named by Symbol.")
757    
758     ;;; The output simple output methods just call the corresponding method
759     ;;; in the synonymed stream.
760     ;;;
761 dtc 1.33 (macrolet ((out-fun (name slot stream-method &rest args)
762 ram 1.1 `(defun ,name (stream ,@args)
763 ram 1.11 (declare (optimize (safety 1)))
764 ram 1.1 (let ((syn (symbol-value (synonym-stream-symbol stream))))
765 dtc 1.33 (if (lisp-stream-p syn)
766     (funcall (,slot syn) syn ,@args)
767     (,stream-method syn ,@args))))))
768     (out-fun synonym-out lisp-stream-out stream-write-char ch)
769     (out-fun synonym-bout lisp-stream-bout stream-write-byte n)
770     (out-fun synonym-sout lisp-stream-sout stream-write-string string start end))
771 ram 1.1
772    
773     ;;; Bind synonym stream to this so that SPIO can turn on the right frob in
774     ;;; the icon when we are in a terminal input wait.
775     ;;;
776     (defvar *previous-stream* nil)
777    
778     ;;; For the input methods, we just call the corresponding function on the
779     ;;; synonymed stream. These functions deal with getting input out of
780     ;;; the In-Buffer if there is any.
781     ;;;
782     (macrolet ((in-fun (name fun &rest args)
783     `(defun ,name (stream ,@args)
784 ram 1.11 (declare (optimize (safety 1)))
785 ram 1.1 (let ((*previous-stream* stream))
786     (,fun (symbol-value (synonym-stream-symbol stream)) ,@args)))))
787     (in-fun synonym-in read-char eof-errorp eof-value)
788     (in-fun synonym-bin read-byte eof-errorp eof-value)
789     (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-errorp))
790    
791    
792     ;;; Synonym-Misc -- Internal
793     ;;;
794     ;;; We have to special-case the operations which could look at stuff in
795     ;;; the in-buffer.
796     ;;;
797     (defun synonym-misc (stream operation &optional arg1 arg2)
798 ram 1.11 (declare (optimize (safety 1)))
799 ram 1.1 (let ((syn (symbol-value (synonym-stream-symbol stream)))
800     (*previous-stream* stream))
801 dtc 1.33 (if (lisp-stream-p syn)
802     (case operation
803     (:listen (or (/= (the fixnum (lisp-stream-in-index syn))
804     in-buffer-length)
805     (funcall (lisp-stream-misc syn) syn :listen)))
806     (t
807     (funcall (lisp-stream-misc syn) syn operation arg1 arg2)))
808     (stream-misc-dispatch syn operation arg1 arg2))))
809 ram 1.1
810     ;;;; Two-Way streams:
811    
812     (defstruct (two-way-stream
813 dtc 1.30 (:include lisp-stream
814 ram 1.1 (in #'two-way-in)
815     (bin #'two-way-bin)
816     (n-bin #'two-way-n-bin)
817     (out #'two-way-out)
818     (bout #'two-way-bout)
819     (sout #'two-way-sout)
820     (misc #'two-way-misc))
821     (:print-function %print-two-way-stream)
822     (:constructor make-two-way-stream (input-stream output-stream)))
823     ;; We read from this stream...
824 ram 1.14 (input-stream (required-argument) :type stream :read-only t)
825 ram 1.1 ;; And write to this one
826 ram 1.14 (output-stream (required-argument) :type stream :read-only t))
827 ram 1.1
828     (defun %print-two-way-stream (s stream d)
829     (declare (ignore d))
830     (format stream "#<Two-Way Stream, Input = ~S, Output = ~S>"
831     (two-way-stream-input-stream s)
832     (two-way-stream-output-stream s)))
833    
834     (setf (documentation 'make-two-way-stream 'function)
835     "Returns a bidirectional stream which gets its input from Input-Stream and
836     sends its output to Output-Stream.")
837    
838 dtc 1.33 (macrolet ((out-fun (name slot stream-method &rest args)
839 ram 1.1 `(defun ,name (stream ,@args)
840     (let ((syn (two-way-stream-output-stream stream)))
841 dtc 1.33 (if (lisp-stream-p syn)
842     (funcall (,slot syn) syn ,@args)
843     (,stream-method syn ,@args))))))
844     (out-fun two-way-out lisp-stream-out stream-write-char ch)
845     (out-fun two-way-bout lisp-stream-bout stream-write-byte n)
846     (out-fun two-way-sout lisp-stream-sout stream-write-string string start end))
847 ram 1.1
848     (macrolet ((in-fun (name fun &rest args)
849     `(defun ,name (stream ,@args)
850 ram 1.10 (force-output (two-way-stream-output-stream stream))
851 ram 1.1 (,fun (two-way-stream-input-stream stream) ,@args))))
852     (in-fun two-way-in read-char eof-errorp eof-value)
853     (in-fun two-way-bin read-byte eof-errorp eof-value)
854     (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-errorp))
855    
856     (defun two-way-misc (stream operation &optional arg1 arg2)
857     (let* ((in (two-way-stream-input-stream stream))
858     (out (two-way-stream-output-stream stream))
859 dtc 1.33 (in-lisp-stream-p (lisp-stream-p in))
860     (out-lisp-stream-p (lisp-stream-p out)))
861 ram 1.1 (case operation
862 dtc 1.33 (:listen
863     (if in-lisp-stream-p
864     (or (/= (the fixnum (lisp-stream-in-index in)) in-buffer-length)
865     (funcall (lisp-stream-misc in) in :listen))
866     (stream-listen in)))
867 ram 1.1 ((:finish-output :force-output :clear-output)
868 dtc 1.33 (if out-lisp-stream-p
869     (funcall (lisp-stream-misc out) out operation arg1 arg2)
870     (stream-misc-dispatch out operation arg1 arg2)))
871 ram 1.1 ((:clear-input :unread)
872 dtc 1.33 (if in-lisp-stream-p
873     (funcall (lisp-stream-misc in) in operation arg1 arg2)
874     (stream-misc-dispatch in operation arg1 arg2)))
875 ram 1.1 (:element-type
876 dtc 1.33 (let ((in-type (stream-element-type in))
877     (out-type (stream-element-type out)))
878 ram 1.1 (if (equal in-type out-type)
879     in-type `(and ,in-type ,out-type))))
880 dtc 1.33 (:close
881 ram 1.1 (set-closed-flame stream))
882     (t
883 dtc 1.33 (or (if in-lisp-stream-p
884     (funcall (lisp-stream-misc in) in operation arg1 arg2)
885     (stream-misc-dispatch in operation arg1 arg2))
886     (if out-lisp-stream-p
887     (funcall (lisp-stream-misc out) out operation arg1 arg2)
888     (stream-misc-dispatch out operation arg1 arg2)))))))
889    
890 ram 1.1
891     ;;;; Concatenated Streams:
892    
893     (defstruct (concatenated-stream
894 dtc 1.30 (:include lisp-stream
895 ram 1.1 (in #'concatenated-in)
896     (bin #'concatenated-bin)
897     (misc #'concatenated-misc))
898     (:print-function %print-concatenated-stream)
899     (:constructor
900     make-concatenated-stream (&rest streams &aux (current streams))))
901     ;; The car of this is the stream we are reading from now.
902     current
903     ;; This is a list of all the streams. We need to remember them so that
904     ;; we can close them.
905 ram 1.14 (streams nil :type list :read-only t))
906 ram 1.1
907     (defun %print-concatenated-stream (s stream d)
908     (declare (ignore d))
909     (format stream "#<Concatenated Stream, Streams = ~S>"
910     (concatenated-stream-streams s)))
911    
912     (setf (documentation 'make-concatenated-stream 'function)
913     "Returns a stream which takes its input from each of the Streams in turn,
914     going on to the next at EOF.")
915    
916     (macrolet ((in-fun (name fun)
917     `(defun ,name (stream eof-errorp eof-value)
918     (do ((current (concatenated-stream-current stream) (cdr current)))
919     ((null current)
920     (eof-or-lose stream eof-errorp eof-value))
921     (let* ((stream (car current))
922     (result (,fun stream nil nil)))
923     (when result (return result)))
924     (setf (concatenated-stream-current stream) current)))))
925     (in-fun concatenated-in read-char)
926     (in-fun concatenated-bin read-byte))
927    
928     (defun concatenated-misc (stream operation &optional arg1 arg2)
929 ram 1.18 (let ((left (concatenated-stream-current stream)))
930     (when left
931 dtc 1.33 (let* ((current (car left)))
932 ram 1.18 (case operation
933     (:listen
934     (loop
935 dtc 1.33 (let ((stuff (if (lisp-stream-p current)
936     (funcall (lisp-stream-misc current) current
937     :listen)
938     (stream-misc-dispatch current :listen))))
939 ram 1.18 (cond ((eq stuff :eof)
940     ;; Advance current, and try again.
941     (pop (concatenated-stream-current stream))
942     (setf current
943     (car (concatenated-stream-current stream)))
944     (unless current
945     ;; No further streams. EOF.
946 dtc 1.33 (return :eof)))
947 ram 1.18 (stuff
948     ;; Stuff's available.
949     (return t))
950     (t
951     ;; Nothing available yet.
952     (return nil))))))
953     (:close
954     (set-closed-flame stream))
955     (t
956 dtc 1.33 (if (lisp-stream-p current)
957     (funcall (lisp-stream-misc current) current operation arg1 arg2)
958     (stream-misc-dispatch current operation arg1 arg2))))))))
959    
960 ram 1.1
961     ;;;; Echo Streams:
962    
963     (defstruct (echo-stream
964     (:include two-way-stream
965     (in #'echo-in)
966     (bin #'echo-bin)
967     (misc #'echo-misc)
968     (n-bin #'ill-bin))
969     (:print-function %print-echo-stream)
970 wlott 1.8 (:constructor make-echo-stream (input-stream output-stream)))
971     unread-stuff)
972 ram 1.1
973    
974 dtc 1.33 (macrolet ((in-fun (name fun out-slot stream-method &rest args)
975 ram 1.1 `(defun ,name (stream ,@args)
976 wlott 1.8 (or (pop (echo-stream-unread-stuff stream))
977     (let* ((in (echo-stream-input-stream stream))
978     (out (echo-stream-output-stream stream))
979     (result (,fun in ,@args)))
980 dtc 1.33 (if (lisp-stream-p out)
981     (funcall (,out-slot out) out result)
982     (,stream-method out result))
983 wlott 1.8 result)))))
984 dtc 1.33 (in-fun echo-in read-char lisp-stream-out stream-write-char
985     eof-errorp eof-value)
986     (in-fun echo-bin read-byte lisp-stream-bout stream-write-byte
987     eof-errorp eof-value))
988 ram 1.1
989     (defun echo-misc (stream operation &optional arg1 arg2)
990     (let* ((in (two-way-stream-input-stream stream))
991 dtc 1.33 (out (two-way-stream-output-stream stream)))
992 ram 1.1 (case operation
993 dtc 1.33 (:listen
994     (or (not (null (echo-stream-unread-stuff stream)))
995     (if (lisp-stream-p in)
996     (or (/= (the fixnum (lisp-stream-in-index in)) in-buffer-length)
997     (funcall (lisp-stream-misc in) in :listen))
998     (stream-misc-dispatch in :listen))))
999 wlott 1.8 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1000 ram 1.1 (:element-type
1001 dtc 1.33 (let ((in-type (stream-element-type in))
1002     (out-type (stream-element-type out)))
1003 ram 1.1 (if (equal in-type out-type)
1004     in-type `(and ,in-type ,out-type))))
1005     (:close
1006     (set-closed-flame stream))
1007     (t
1008 dtc 1.33 (or (if (lisp-stream-p in)
1009     (funcall (lisp-stream-misc in) in operation arg1 arg2)
1010     (stream-misc-dispatch in operation arg1 arg2))
1011     (if (lisp-stream-p out)
1012     (funcall (lisp-stream-misc out) out operation arg1 arg2)
1013     (stream-misc-dispatch out operation arg1 arg2)))))))
1014 ram 1.1
1015     (defun %print-echo-stream (s stream d)
1016     (declare (ignore d))
1017     (format stream "#<Echo Stream, Input = ~S, Output = ~S>"
1018     (two-way-stream-input-stream s)
1019     (two-way-stream-output-stream s)))
1020    
1021     (setf (documentation 'make-echo-stream 'function)
1022     "Returns a bidirectional stream which gets its input from Input-Stream and
1023     sends its output to Output-Stream. In addition, all input is echoed to
1024     the output stream")
1025    
1026     ;;;; String Input Streams:
1027    
1028     (defstruct (string-input-stream
1029 dtc 1.30 (:include lisp-stream
1030 ram 1.21 (in #'string-inch)
1031     (bin #'string-binch)
1032     (n-bin #'string-stream-read-n-bytes)
1033     (misc #'string-in-misc))
1034     (:print-function %print-string-input-stream)
1035     ;(:constructor nil)
1036     (:constructor internal-make-string-input-stream
1037     (string current end)))
1038 ram 1.1 (string nil :type simple-string)
1039 dtc 1.31 (current nil :type index)
1040     (end nil :type index))
1041 ram 1.1
1042     (defun %print-string-input-stream (s stream d)
1043     (declare (ignore s d))
1044     (write-string "#<String-Input Stream>" stream))
1045    
1046     (defun string-inch (stream eof-errorp eof-value)
1047     (let ((string (string-input-stream-string stream))
1048     (index (string-input-stream-current stream)))
1049     (declare (simple-string string) (fixnum index))
1050 dtc 1.31 (cond ((= index (the index (string-input-stream-end stream)))
1051 ram 1.1 (eof-or-lose stream eof-errorp eof-value))
1052     (t
1053     (setf (string-input-stream-current stream) (1+ index))
1054     (aref string index)))))
1055 ram 1.21
1056     (defun string-binch (stream eof-errorp eof-value)
1057     (let ((string (string-input-stream-string stream))
1058     (index (string-input-stream-current stream)))
1059 dtc 1.31 (declare (simple-string string)
1060     (type index index))
1061     (cond ((= index (the index (string-input-stream-end stream)))
1062 ram 1.21 (eof-or-lose stream eof-errorp eof-value))
1063     (t
1064     (setf (string-input-stream-current stream) (1+ index))
1065     (char-code (aref string index))))))
1066    
1067     (defun string-stream-read-n-bytes (stream buffer start requested eof-errorp)
1068     (declare (type string-input-stream stream)
1069     (type index start requested))
1070 dtc 1.31 (let* ((string (string-input-stream-string stream))
1071     (index (string-input-stream-current stream))
1072     (available (- (string-input-stream-end stream) index))
1073     (copy (min available requested)))
1074     (declare (simple-string string)
1075     (type index index available copy))
1076     (when (plusp copy)
1077     (setf (string-input-stream-current stream)
1078     (truly-the index (+ index copy)))
1079     (system:without-gcing
1080     (system-area-copy (vector-sap string)
1081     (* index vm:byte-bits)
1082     (if (typep buffer 'system-area-pointer)
1083     buffer
1084     (vector-sap buffer))
1085     (* start vm:byte-bits)
1086     (* copy vm:byte-bits))))
1087     (if (and (> requested copy) eof-errorp)
1088     (error 'end-of-file :stream stream)
1089     copy)))
1090 ram 1.1
1091     (defun string-in-misc (stream operation &optional arg1 arg2)
1092 ram 1.18 (declare (ignore arg2))
1093 ram 1.1 (case operation
1094 ram 1.2 (:file-position
1095 ram 1.5 (if arg1
1096     (setf (string-input-stream-current stream) arg1)
1097     (string-input-stream-current stream)))
1098     (:file-length (length (string-input-stream-string stream)))
1099 ram 1.1 (:unread (decf (string-input-stream-current stream)))
1100 ram 1.10 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1101     (the fixnum (string-input-stream-end stream)))
1102     :eof))
1103 pw 1.27 (:element-type 'base-char)))
1104 ram 1.1
1105     (defun make-string-input-stream (string &optional
1106     (start 0) (end (length string)))
1107     "Returns an input stream which will supply the characters of String between
1108     Start and End in order."
1109 wlott 1.17 (declare (type string string)
1110     (type index start)
1111 ram 1.18 (type (or index null) end))
1112 wlott 1.17 (internal-make-string-input-stream (coerce string 'simple-string)
1113     start end))
1114 ram 1.1
1115     ;;;; String Output Streams:
1116    
1117     (defstruct (string-output-stream
1118 dtc 1.30 (:include lisp-stream
1119 ram 1.1 (out #'string-ouch)
1120     (sout #'string-sout)
1121     (misc #'string-out-misc))
1122     (:print-function %print-string-output-stream)
1123     (:constructor make-string-output-stream ()))
1124     ;; The string we throw stuff in.
1125     (string (make-string 40) :type simple-string)
1126     ;; Index of the next location to use.
1127     (index 0 :type fixnum))
1128    
1129     (defun %print-string-output-stream (s stream d)
1130     (declare (ignore s d))
1131     (write-string "#<String-Output Stream>" stream))
1132    
1133     (setf (documentation 'make-string-output-stream 'function)
1134     "Returns an Output stream which will accumulate all output given it for
1135     the benefit of the function Get-Output-Stream-String.")
1136    
1137     (defun string-ouch (stream character)
1138     (let ((current (string-output-stream-index stream))
1139     (workspace (string-output-stream-string stream)))
1140     (declare (simple-string workspace) (fixnum current))
1141     (if (= current (the fixnum (length workspace)))
1142     (let ((new-workspace (make-string (* current 2))))
1143 wlott 1.17 (replace new-workspace workspace)
1144 ram 1.1 (setf (aref new-workspace current) character)
1145     (setf (string-output-stream-string stream) new-workspace))
1146     (setf (aref workspace current) character))
1147     (setf (string-output-stream-index stream) (1+ current))))
1148    
1149     (defun string-sout (stream string start end)
1150     (declare (simple-string string) (fixnum start end))
1151     (let* ((current (string-output-stream-index stream))
1152     (length (- end start))
1153     (dst-end (+ length current))
1154     (workspace (string-output-stream-string stream)))
1155     (declare (simple-string workspace)
1156     (fixnum current length dst-end))
1157     (if (> dst-end (the fixnum (length workspace)))
1158     (let ((new-workspace (make-string (+ (* current 2) length))))
1159 wlott 1.17 (replace new-workspace workspace :end2 current)
1160     (replace new-workspace string
1161     :start1 current :end1 dst-end
1162     :start2 start :end2 end)
1163 ram 1.1 (setf (string-output-stream-string stream) new-workspace))
1164 wlott 1.17 (replace workspace string
1165     :start1 current :end1 dst-end
1166     :start2 start :end2 end))
1167 ram 1.1 (setf (string-output-stream-index stream) dst-end)))
1168    
1169     (defun string-out-misc (stream operation &optional arg1 arg2)
1170 ram 1.3 (declare (ignore arg2))
1171 ram 1.1 (case operation
1172 ram 1.2 (:file-position
1173     (if (null arg1)
1174 ram 1.3 (string-output-stream-index stream)))
1175 ram 1.1 (:charpos
1176     (do ((index (1- (the fixnum (string-output-stream-index stream)))
1177     (1- index))
1178     (count 0 (1+ count))
1179     (string (string-output-stream-string stream)))
1180     ((< index 0) count)
1181     (declare (simple-string string)
1182     (fixnum index count))
1183     (if (char= (schar string index) #\newline)
1184     (return count))))
1185 pw 1.27 (:element-type 'base-char)))
1186 ram 1.1
1187     (defun get-output-stream-string (stream)
1188     "Returns a string of all the characters sent to a stream made by
1189     Make-String-Output-Stream since the last call to this function."
1190 wlott 1.17 (declare (type string-output-stream stream))
1191     (let* ((length (string-output-stream-index stream))
1192     (result (make-string length)))
1193     (replace result (string-output-stream-string stream))
1194     (setf (string-output-stream-index stream) 0)
1195     result))
1196 ram 1.1
1197     (defun dump-output-stream-string (in-stream out-stream)
1198     "Dumps the characters buffer up in the In-Stream to the Out-Stream as
1199     Get-Output-Stream-String would return them."
1200 dtc 1.30 (write-string* (string-output-stream-string in-stream) out-stream
1201     0 (string-output-stream-index in-stream))
1202 ram 1.1 (setf (string-output-stream-index in-stream) 0))
1203    
1204     ;;;; Fill-pointer streams:
1205     ;;;
1206     ;;; Fill pointer string output streams are not explicitly mentioned in
1207     ;;; the CLM, but they are required for the implementation of With-Output-To-String.
1208    
1209     (defstruct (fill-pointer-output-stream
1210 dtc 1.30 (:include lisp-stream
1211 ram 1.1 (out #'fill-pointer-ouch)
1212     (sout #'fill-pointer-sout)
1213     (misc #'fill-pointer-misc))
1214     (:print-function
1215     (lambda (s stream d)
1216     (declare (ignore s d))
1217     (write-string "#<Fill-Pointer String Output Stream>" stream)))
1218     (:constructor make-fill-pointer-output-stream (string)))
1219     ;; The string we throw stuff in.
1220     string)
1221    
1222    
1223     (defun fill-pointer-ouch (stream character)
1224     (let* ((buffer (fill-pointer-output-stream-string stream))
1225 wlott 1.4 (current (fill-pointer buffer))
1226 ram 1.1 (current+1 (1+ current)))
1227     (declare (fixnum current))
1228     (with-array-data ((workspace buffer) (start) (end))
1229     (declare (simple-string workspace))
1230     (let ((offset-current (+ start current)))
1231     (declare (fixnum offset-current))
1232     (if (= offset-current end)
1233     (let* ((new-length (* current 2))
1234     (new-workspace (make-string new-length)))
1235     (declare (simple-string new-workspace))
1236     (%primitive byte-blt workspace start new-workspace 0 current)
1237     (setf workspace new-workspace)
1238     (setf offset-current current)
1239     (set-array-header buffer workspace new-length
1240     current+1 0 new-length nil))
1241 wlott 1.4 (setf (fill-pointer buffer) current+1))
1242 ram 1.1 (setf (schar workspace offset-current) character)))
1243     current+1))
1244    
1245    
1246     (defun fill-pointer-sout (stream string start end)
1247     (declare (simple-string string) (fixnum start end))
1248     (let* ((buffer (fill-pointer-output-stream-string stream))
1249 wlott 1.4 (current (fill-pointer buffer))
1250 ram 1.1 (string-len (- end start))
1251     (dst-end (+ string-len current)))
1252     (declare (fixnum current dst-end string-len))
1253     (with-array-data ((workspace buffer) (dst-start) (dst-length))
1254     (declare (simple-string workspace))
1255     (let ((offset-dst-end (+ dst-start dst-end))
1256     (offset-current (+ dst-start current)))
1257     (declare (fixnum offset-dst-end offset-current))
1258     (if (> offset-dst-end dst-length)
1259     (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1260     (new-workspace (make-string new-length)))
1261     (declare (simple-string new-workspace))
1262     (%primitive byte-blt workspace dst-start new-workspace 0 current)
1263     (setf workspace new-workspace)
1264     (setf offset-current current)
1265     (setf offset-dst-end dst-end)
1266     (set-array-header buffer workspace new-length
1267     dst-end 0 new-length nil))
1268 wlott 1.4 (setf (fill-pointer buffer) dst-end))
1269 ram 1.1 (%primitive byte-blt string start
1270     workspace offset-current offset-dst-end)))
1271     dst-end))
1272    
1273    
1274     (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1275     (declare (ignore arg1 arg2))
1276     (case operation
1277     (:charpos
1278     (let* ((buffer (fill-pointer-output-stream-string stream))
1279 wlott 1.4 (current (fill-pointer buffer)))
1280 ram 1.1 (with-array-data ((string buffer) (start) (end current))
1281     (declare (simple-string string) (ignore start))
1282     (let ((found (position #\newline string :test #'char=
1283     :end end :from-end t)))
1284     (if found
1285     (- end (the fixnum found))
1286     current)))))
1287 pw 1.27 (:element-type 'base-char)))
1288 ram 1.1
1289     ;;;; Indenting streams:
1290    
1291 dtc 1.30 (defstruct (indenting-stream (:include lisp-stream
1292 ram 1.1 (out #'indenting-out)
1293     (sout #'indenting-sout)
1294     (misc #'indenting-misc))
1295     (:print-function %print-indenting-stream)
1296     (:constructor make-indenting-stream (stream)))
1297     ;; The stream we're based on:
1298     stream
1299     ;; How much we indent on each line:
1300     (indentation 0))
1301    
1302     (setf (documentation 'make-indenting-stream 'function)
1303     "Returns an ouput stream which indents its output by some amount.")
1304    
1305     (defun %print-indenting-stream (s stream d)
1306     (declare (ignore s d))
1307     (write-string "#<Indenting Stream>" stream))
1308    
1309     ;;; Indenting-Indent writes the right number of spaces needed to indent output on
1310     ;;; the given Stream based on the specified Sub-Stream.
1311    
1312     (defmacro indenting-indent (stream sub-stream)
1313     `(do ((i 0 (+ i 60))
1314     (indentation (indenting-stream-indentation ,stream)))
1315     ((>= i indentation))
1316 dtc 1.30 (write-string*
1317     " "
1318     ,sub-stream 0 (min 60 (- indentation i)))))
1319 ram 1.1
1320     ;;; Indenting-Out writes a character to an indenting stream.
1321    
1322     (defun indenting-out (stream char)
1323     (let ((sub-stream (indenting-stream-stream stream)))
1324 dtc 1.30 (write-char char sub-stream)
1325 ram 1.1 (if (char= char #\newline)
1326     (indenting-indent stream sub-stream))))
1327    
1328     ;;; Indenting-Sout writes a string to an indenting stream.
1329    
1330     (defun indenting-sout (stream string start end)
1331     (declare (simple-string string) (fixnum start end))
1332     (do ((i start)
1333     (sub-stream (indenting-stream-stream stream)))
1334     ((= i end))
1335     (let ((newline (position #\newline string :start i :end end)))
1336     (cond (newline
1337 dtc 1.30 (write-string* string sub-stream i (1+ newline))
1338 ram 1.1 (indenting-indent stream sub-stream)
1339     (setq i (+ newline 1)))
1340     (t
1341 dtc 1.30 (write-string* string sub-stream i end)
1342 ram 1.1 (setq i end))))))
1343    
1344     ;;; Indenting-Misc just treats just the :Line-Length message differently.
1345     ;;; Indenting-Charpos says the charpos is the charpos of the base stream minus
1346     ;;; the stream's indentation.
1347    
1348     (defun indenting-misc (stream operation &optional arg1 arg2)
1349 dtc 1.30 (let ((sub-stream (indenting-stream-stream stream)))
1350 dtc 1.33 (if (lisp-stream-p sub-stream)
1351     (let ((method (lisp-stream-misc sub-stream)))
1352     (case operation
1353     (:line-length
1354     (let ((line-length (funcall method sub-stream operation)))
1355     (if line-length
1356     (- line-length (indenting-stream-indentation stream)))))
1357     (:charpos
1358     (let ((charpos (funcall method sub-stream operation)))
1359     (if charpos
1360     (- charpos (indenting-stream-indentation stream)))))
1361     (t
1362     (funcall method sub-stream operation arg1 arg2))))
1363     ;; Fundamental-stream.
1364     (case operation
1365     (:line-length
1366     (let ((line-length (stream-line-length sub-stream)))
1367     (if line-length
1368     (- line-length (indenting-stream-indentation stream)))))
1369     (:charpos
1370     (let ((charpos (stream-line-column sub-stream)))
1371     (if charpos
1372     (- charpos (indenting-stream-indentation stream)))))
1373     (t
1374     (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1375 dtc 1.30
1376 ram 1.1
1377 wlott 1.13 (proclaim '(maybe-inline read-char unread-char read-byte listen))
1378    
1379    
1380    
1381     ;;;; Case frobbing streams, used by format ~(...~).
1382    
1383     (defstruct (case-frob-stream
1384 dtc 1.30 (:include lisp-stream
1385 wlott 1.13 (:misc #'case-frob-misc))
1386     (:constructor %make-case-frob-stream (target out sout)))
1387     (target (required-argument) :type stream))
1388    
1389     (defun make-case-frob-stream (target kind)
1390     "Returns a stream that sends all output to the stream TARGET, but modifies
1391     the case of letters, depending on KIND, which should be one of:
1392     :upcase - convert to upper case.
1393     :downcase - convert to lower case.
1394     :capitalize - convert the first letter of words to upper case and the
1395     rest of the word to lower case.
1396     :capitalize-first - convert the first letter of the first word to upper
1397     case and everything else to lower case."
1398     (declare (type stream target)
1399     (type (member :upcase :downcase :capitalize :capitalize-first)
1400     kind)
1401     (values stream))
1402     (if (case-frob-stream-p target)
1403     ;; If we are going to be writing to a stream that already does case
1404     ;; frobbing, why bother frobbing the case just so it can frob it
1405     ;; again?
1406     target
1407     (multiple-value-bind
1408     (out sout)
1409     (ecase kind
1410     (:upcase
1411     (values #'case-frob-upcase-out
1412     #'case-frob-upcase-sout))
1413     (:downcase
1414     (values #'case-frob-downcase-out
1415     #'case-frob-downcase-sout))
1416     (:capitalize
1417     (values #'case-frob-capitalize-out
1418     #'case-frob-capitalize-sout))
1419     (:capitalize-first
1420     (values #'case-frob-capitalize-first-out
1421     #'case-frob-capitalize-first-sout)))
1422     (%make-case-frob-stream target out sout))))
1423    
1424     (defun case-frob-misc (stream op &optional arg1 arg2)
1425     (declare (type case-frob-stream stream))
1426     (case op
1427     (:close)
1428     (t
1429     (let ((target (case-frob-stream-target stream)))
1430 dtc 1.33 (if (lisp-stream-p target)
1431     (funcall (lisp-stream-misc target) target op arg1 arg2)
1432     (stream-misc-dispatch target op arg1 arg2))))))
1433 wlott 1.13
1434     (defun case-frob-upcase-out (stream char)
1435     (declare (type case-frob-stream stream)
1436 ram 1.16 (type base-char char))
1437 dtc 1.33 (let ((target (case-frob-stream-target stream))
1438     (char (char-upcase char)))
1439     (if (lisp-stream-p target)
1440     (funcall (lisp-stream-out target) target char)
1441     (stream-write-char target char))))
1442 wlott 1.13
1443     (defun case-frob-upcase-sout (stream str start end)
1444     (declare (type case-frob-stream stream)
1445     (type simple-base-string str)
1446     (type index start)
1447     (type (or index null) end))
1448     (let* ((target (case-frob-stream-target stream))
1449     (len (length str))
1450 dtc 1.33 (end (or end len))
1451     (string (if (and (zerop start) (= len end))
1452     (string-upcase str)
1453     (nstring-upcase (subseq str start end))))
1454     (string-len (- end start)))
1455     (if (lisp-stream-p target)
1456     (funcall (lisp-stream-sout target) target string 0 string-len)
1457     (stream-write-string target string 0 string-len))))
1458 wlott 1.13
1459     (defun case-frob-downcase-out (stream char)
1460     (declare (type case-frob-stream stream)
1461 ram 1.16 (type base-char char))
1462 dtc 1.33 (let ((target (case-frob-stream-target stream))
1463     (char (char-downcase char)))
1464     (if (lisp-stream-p target)
1465     (funcall (lisp-stream-out target) target char)
1466     (stream-write-char target char))))
1467 wlott 1.13
1468     (defun case-frob-downcase-sout (stream str start end)
1469     (declare (type case-frob-stream stream)
1470     (type simple-base-string str)
1471     (type index start)
1472     (type (or index null) end))
1473     (let* ((target (case-frob-stream-target stream))
1474     (len (length str))
1475 dtc 1.33 (end (or end len))
1476     (string (if (and (zerop start) (= len end))
1477     (string-downcase str)
1478     (nstring-downcase (subseq str start end))))
1479     (string-len (- end start)))
1480     (if (lisp-stream-p target)
1481     (funcall (lisp-stream-sout target) target string 0 string-len)
1482     (stream-write-string target string 0 string-len))))
1483 wlott 1.13
1484     (defun case-frob-capitalize-out (stream char)
1485     (declare (type case-frob-stream stream)
1486 ram 1.16 (type base-char char))
1487 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1488     (cond ((alphanumericp char)
1489 dtc 1.33 (let ((char (char-upcase char)))
1490     (if (lisp-stream-p target)
1491     (funcall (lisp-stream-out target) target char)
1492     (stream-write-char target char)))
1493     (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1494 wlott 1.13 (setf (case-frob-stream-sout stream)
1495     #'case-frob-capitalize-aux-sout))
1496     (t
1497 dtc 1.33 (if (lisp-stream-p target)
1498     (funcall (lisp-stream-out target) target char)
1499     (stream-write-char target char))))))
1500 wlott 1.13
1501     (defun case-frob-capitalize-sout (stream str start end)
1502     (declare (type case-frob-stream stream)
1503     (type simple-base-string str)
1504     (type index start)
1505     (type (or index null) end))
1506     (let* ((target (case-frob-stream-target stream))
1507     (str (subseq str start end))
1508     (len (length str))
1509     (inside-word nil))
1510     (dotimes (i len)
1511     (let ((char (schar str i)))
1512     (cond ((not (alphanumericp char))
1513     (setf inside-word nil))
1514     (inside-word
1515     (setf (schar str i) (char-downcase char)))
1516     (t
1517     (setf inside-word t)
1518     (setf (schar str i) (char-upcase char))))))
1519     (when inside-word
1520     (setf (case-frob-stream-out stream)
1521     #'case-frob-capitalize-aux-out)
1522     (setf (case-frob-stream-sout stream)
1523     #'case-frob-capitalize-aux-sout))
1524 dtc 1.33 (if (lisp-stream-p target)
1525     (funcall (lisp-stream-sout target) target str 0 len)
1526     (stream-write-string target str 0 len))))
1527 wlott 1.13
1528     (defun case-frob-capitalize-aux-out (stream char)
1529     (declare (type case-frob-stream stream)
1530 ram 1.16 (type base-char char))
1531 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1532     (cond ((alphanumericp char)
1533 dtc 1.33 (let ((char (char-downcase char)))
1534     (if (lisp-stream-p target)
1535     (funcall (lisp-stream-out target) target char)
1536     (stream-write-char target char))))
1537 wlott 1.13 (t
1538 dtc 1.33 (if (lisp-stream-p target)
1539     (funcall (lisp-stream-out target) target char)
1540     (stream-write-char target char))
1541 wlott 1.13 (setf (case-frob-stream-out stream)
1542     #'case-frob-capitalize-out)
1543     (setf (case-frob-stream-sout stream)
1544     #'case-frob-capitalize-sout)))))
1545    
1546     (defun case-frob-capitalize-aux-sout (stream str start end)
1547     (declare (type case-frob-stream stream)
1548     (type simple-base-string str)
1549     (type index start)
1550     (type (or index null) end))
1551     (let* ((target (case-frob-stream-target stream))
1552     (str (subseq str start end))
1553     (len (length str))
1554     (inside-word t))
1555     (dotimes (i len)
1556     (let ((char (schar str i)))
1557     (cond ((not (alphanumericp char))
1558     (setf inside-word nil))
1559     (inside-word
1560     (setf (schar str i) (char-downcase char)))
1561     (t
1562     (setf inside-word t)
1563     (setf (schar str i) (char-upcase char))))))
1564     (unless inside-word
1565     (setf (case-frob-stream-out stream)
1566     #'case-frob-capitalize-out)
1567     (setf (case-frob-stream-sout stream)
1568     #'case-frob-capitalize-sout))
1569 dtc 1.33 (if (lisp-stream-p target)
1570     (funcall (lisp-stream-sout target) target str 0 len)
1571     (stream-write-string target str 0 len))))
1572 wlott 1.13
1573     (defun case-frob-capitalize-first-out (stream char)
1574     (declare (type case-frob-stream stream)
1575 ram 1.16 (type base-char char))
1576 wlott 1.13 (let ((target (case-frob-stream-target stream)))
1577     (cond ((alphanumericp char)
1578 dtc 1.33 (let ((char (char-upcase char)))
1579     (if (lisp-stream-p target)
1580     (funcall (lisp-stream-out target) target char)
1581     (stream-write-char target char)))
1582 wlott 1.13 (setf (case-frob-stream-out stream)
1583     #'case-frob-downcase-out)
1584     (setf (case-frob-stream-sout stream)
1585     #'case-frob-downcase-sout))
1586     (t
1587 dtc 1.33 (if (lisp-stream-p target)
1588     (funcall (lisp-stream-out target) target char)
1589     (stream-write-char target char))))))
1590 wlott 1.13
1591     (defun case-frob-capitalize-first-sout (stream str start end)
1592     (declare (type case-frob-stream stream)
1593     (type simple-base-string str)
1594     (type index start)
1595     (type (or index null) end))
1596     (let* ((target (case-frob-stream-target stream))
1597     (str (subseq str start end))
1598     (len (length str)))
1599     (dotimes (i len)
1600     (let ((char (schar str i)))
1601     (when (alphanumericp char)
1602     (setf (schar str i) (char-upcase char))
1603     (do ((i (1+ i) (1+ i)))
1604     ((= i len))
1605     (setf (schar str i) (char-downcase (schar str i))))
1606     (setf (case-frob-stream-out stream)
1607     #'case-frob-downcase-out)
1608     (setf (case-frob-stream-sout stream)
1609     #'case-frob-downcase-sout)
1610     (return))))
1611 dtc 1.33 (if (lisp-stream-p target)
1612     (funcall (lisp-stream-sout target) target str 0 len)
1613     (stream-write-string target str 0 len))))
1614 chiles 1.12
1615    
1616     ;;;; Public interface from "EXTENSIONS" package.
1617    
1618     (defstruct (stream-command (:print-function print-stream-command)
1619     (:constructor make-stream-command
1620     (name &optional args)))
1621     (name nil :type symbol)
1622     (args nil :type list))
1623    
1624     (defun print-stream-command (obj str n)
1625     (declare (ignore n))
1626     (format str "#<Stream-Cmd ~S>" (stream-command-name obj)))
1627    
1628    
1629     ;;; GET-STREAM-COMMAND -- Public.
1630     ;;;
1631 dtc 1.26 ;;; We can't simply call the stream's misc method because nil is an
1632 chiles 1.12 ;;; ambiguous return value: does it mean text arrived, or does it mean the
1633     ;;; stream's misc method had no :get-command implementation. We can't return
1634     ;;; nil until there is text input. We don't need to loop because any stream
1635     ;;; implementing :get-command would wait until it had some input. If the
1636     ;;; LISTEN fails, then we have some random stream we must wait on.
1637     ;;;
1638     (defun get-stream-command (stream)
1639     "This takes a stream and waits for text or a command to appear on it. If
1640     text appears before a command, this returns nil, and otherwise it returns
1641     a command."
1642 dtc 1.30 (let ((cmdp (funcall (lisp-stream-misc stream) stream :get-command)))
1643 chiles 1.12 (cond (cmdp)
1644     ((listen stream)
1645     nil)
1646     (t
1647     ;; This waits for input and returns nil when it arrives.
1648     (unread-char (read-char stream) stream)))))
1649 dtc 1.26
1650    
1651     ;;; READ-SEQUENCE -- Public
1652     ;;;
1653     (defun read-sequence (seq stream &key (start 0) (end nil))
1654     "Destructively modify SEQ by reading elements from STREAM.
1655     SEQ is bounded by START and END. SEQ is destructively modified by
1656     copying successive elements into it from STREAM. If the end of file
1657     for STREAM is reached before copying all elements of the subsequence,
1658     then the extra elements near the end of sequence are not updated, and
1659     the index of the next element is returned."
1660     (declare (type sequence seq)
1661     (type stream stream)
1662     (type index start)
1663     (type sequence-end end)
1664     (values index))
1665     (let ((end (or end (length seq))))
1666     (declare (type index end))
1667     (etypecase seq
1668     (list
1669     (let ((read-function
1670     (if (subtypep (stream-element-type stream) 'character)
1671     #'read-char
1672     #'read-byte)))
1673     (do ((rem (nthcdr start seq) (rest rem))
1674     (i start (1+ i)))
1675     ((or (endp rem) (>= i end)) i)
1676     (declare (type list rem)
1677     (type index i))
1678 dtc 1.31 (let ((el (funcall read-function stream nil :eof)))
1679     (when (eq el :eof)
1680 dtc 1.26 (return i))
1681     (setf (first rem) el)))))
1682     (vector
1683     (with-array-data ((data seq) (offset-start start) (offset-end end))
1684 dtc 1.31 (typecase data
1685     ((or (simple-array (unsigned-byte 8) (*))
1686     #+signed-array (simple-array (signed-byte 8) (*))
1687     simple-string)
1688     (let* ((numbytes (- end start))
1689     (bytes-read (system:read-n-bytes
1690     stream data offset-start numbytes nil)))
1691     (if (< bytes-read numbytes)
1692     (+ start bytes-read)
1693     end)))
1694     (t
1695     (let ((read-function
1696     (if (subtypep (stream-element-type stream) 'character)
1697     #'read-char
1698     #'read-byte)))
1699     (do ((i offset-start (1+ i)))
1700     ((>= i offset-end) end)
1701     (declare (type index i))
1702     (let ((el (funcall read-function stream nil :eof)))
1703     (when (eq el :eof)
1704     (return (+ start (- i offset-start))))
1705     (setf (aref data i) el)))))))))))
1706 dtc 1.26
1707     ;;; WRITE-SEQUENCE -- Public
1708     ;;;
1709     (defun write-sequence (seq stream &key (start 0) (end nil))
1710     "Write the elements of SEQ bounded by START and END to STREAM."
1711     (declare (type sequence seq)
1712     (type stream stream)
1713     (type index start)
1714     (type sequence-end end)
1715     (values sequence))
1716     (let ((end (or end (length seq))))
1717     (declare (type index start end))
1718     (etypecase seq
1719     (list
1720     (let ((write-function
1721     (if (subtypep (stream-element-type stream) 'character)
1722     #'write-char
1723     #'write-byte)))
1724     (do ((rem (nthcdr start seq) (rest rem))
1725     (i start (1+ i)))
1726     ((or (endp rem) (>= i end)) seq)
1727     (declare (type list rem)
1728     (type index i))
1729     (funcall write-function (first rem) stream))))
1730     (string
1731 dtc 1.30 (write-string* seq stream start end))
1732 dtc 1.26 (vector
1733     (let ((write-function
1734     (if (subtypep (stream-element-type stream) 'character)
1735     #'write-char
1736     #'write-byte)))
1737     (do ((i start (1+ i)))
1738     ((>= i end) seq)
1739     (declare (type index i))
1740     (funcall write-function (aref seq i) stream)))))))

  ViewVC Help
Powered by ViewVC 1.1.5