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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25.2.3 - (hide annotations)
Tue May 23 16:36:50 2000 UTC (13 years, 10 months ago) by pw
Branch: RELENG_18
Changes since 1.25.2.2: +28 -28 lines
This set of revisions brings the RELENG_18 branch up to HEAD in preparation
for an 18c release.
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.25.2.3 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/stream.lisp,v 1.25.2.3 2000/05/23 16:36:50 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 pw 1.25.2.1 ;;; 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 dtc 1.25.2.2 (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 dtc 1.25.2.2 (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 dtc 1.25.2.2 (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 dtc 1.25.2.2 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (funcall (lisp-stream-misc stream) stream :close abort))
222 ram 1.1 t)
223    
224     (defun set-closed-flame (stream)
225 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (let ((stream (in-synonym-of stream)))
275     (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 pw 1.25.2.1 (let ((stream (in-synonym-of stream)))
318     (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 pw 1.25.2.1 (let ((stream (in-synonym-of stream)))
332     (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 pw 1.25.2.3 (let ((stream (in-synonym-of stream))
351     (eof-errorp (or eof-errorp recursive-p)))
352 pw 1.25.2.1 (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     (let ((stream (in-synonym-of stream)))
396 pw 1.25.2.1 (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 ram 1.10 (let ((stream (in-synonym-of stream)))
408 pw 1.25.2.1 (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    
419 ram 1.1
420     (defun clear-input (&optional (stream *standard-input*))
421     "Clears any buffered input associated with the Stream."
422     (let ((stream (in-synonym-of stream)))
423 pw 1.25.2.1 (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     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 pw 1.25.2.1 (let ((stream (in-synonym-of stream)))
433     (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 pw 1.25.2.1 (declare (type lisp-stream stream)
454     (type index numbytes start)
455 ram 1.18 (type (or (simple-array * (*)) system-area-pointer) buffer))
456 pw 1.25.2.1 (let* ((stream (in-synonym-of stream lisp-stream))
457     (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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     (let ((stream (out-synonym-of stream)))
553 pw 1.25.2.1 (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 pw 1.25.2.1 (let ((stream (out-synonym-of stream)))
569     (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     (let ((stream (out-synonym-of stream)))
589 pw 1.25.2.1 (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     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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 "Attempts to ensure that all output sent to the Stream has reached its
615 ram 1.1 destination, and only then returns."
616 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.3 (with-out-stream stream (lisp-stream-bout integer)
635     (stream-write-byte integer))
636 ram 1.1 integer)
637    
638 pw 1.25.2.1
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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (: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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (in-lisp-stream-p (lisp-stream-p in))
861     (out-lisp-stream-p (lisp-stream-p out)))
862 ram 1.1 (case operation
863 pw 1.25.2.1 (: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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (:close
882 ram 1.1 (set-closed-flame stream))
883     (t
884 pw 1.25.2.1 (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 pw 1.25.2.1 (: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 pw 1.25.2.3 (:constructor make-concatenated-stream (&rest streams)))
901     ;; This is a list of all the streams. The car of this is the stream
902     ;; we are reading from now.
903     (streams nil :type list))
904 ram 1.1
905     (defun %print-concatenated-stream (s stream d)
906     (declare (ignore d))
907     (format stream "#<Concatenated Stream, Streams = ~S>"
908     (concatenated-stream-streams s)))
909    
910     (setf (documentation 'make-concatenated-stream 'function)
911     "Returns a stream which takes its input from each of the Streams in turn,
912     going on to the next at EOF.")
913    
914     (macrolet ((in-fun (name fun)
915     `(defun ,name (stream eof-errorp eof-value)
916 pw 1.25.2.3 (do ((current (concatenated-stream-streams stream)
917     (cdr current)))
918 ram 1.1 ((null current)
919     (eof-or-lose stream eof-errorp eof-value))
920     (let* ((stream (car current))
921     (result (,fun stream nil nil)))
922     (when result (return result)))
923 pw 1.25.2.3 (setf (concatenated-stream-streams stream) (cdr current))))))
924 ram 1.1 (in-fun concatenated-in read-char)
925     (in-fun concatenated-bin read-byte))
926    
927     (defun concatenated-misc (stream operation &optional arg1 arg2)
928 pw 1.25.2.3 (let ((left (concatenated-stream-streams stream)))
929 ram 1.18 (when left
930 pw 1.25.2.1 (let* ((current (car left)))
931 ram 1.18 (case operation
932     (:listen
933     (loop
934 pw 1.25.2.1 (let ((stuff (if (lisp-stream-p current)
935     (funcall (lisp-stream-misc current) current
936     :listen)
937     (stream-misc-dispatch current :listen))))
938 ram 1.18 (cond ((eq stuff :eof)
939     ;; Advance current, and try again.
940 pw 1.25.2.3 (pop (concatenated-stream-streams stream))
941 ram 1.18 (setf current
942 pw 1.25.2.3 (car (concatenated-stream-streams stream)))
943 ram 1.18 (unless current
944     ;; No further streams. EOF.
945 pw 1.25.2.1 (return :eof)))
946 ram 1.18 (stuff
947     ;; Stuff's available.
948     (return t))
949     (t
950     ;; Nothing available yet.
951     (return nil))))))
952     (:close
953     (set-closed-flame stream))
954     (t
955 pw 1.25.2.1 (if (lisp-stream-p current)
956     (funcall (lisp-stream-misc current) current operation arg1 arg2)
957     (stream-misc-dispatch current operation arg1 arg2))))))))
958    
959 ram 1.1
960     ;;;; Echo Streams:
961    
962     (defstruct (echo-stream
963     (:include two-way-stream
964     (in #'echo-in)
965     (bin #'echo-bin)
966     (misc #'echo-misc)
967     (n-bin #'ill-bin))
968     (:print-function %print-echo-stream)
969 wlott 1.8 (:constructor make-echo-stream (input-stream output-stream)))
970     unread-stuff)
971 ram 1.1
972    
973 pw 1.25.2.3 (macrolet ((in-fun (name fun out-slot stream-method)
974     `(defun ,name (stream eof-errorp eof-value)
975 wlott 1.8 (or (pop (echo-stream-unread-stuff stream))
976     (let* ((in (echo-stream-input-stream stream))
977     (out (echo-stream-output-stream stream))
978 pw 1.25.2.3 (result (,fun in nil :eof)))
979     (cond ((eq result :eof)
980     (eof-or-lose stream eof-errorp eof-value))
981     (t
982     (if (lisp-stream-p out)
983     (funcall (,out-slot out) out result)
984     (,stream-method out result))
985     result)))))))
986     (in-fun echo-in read-char lisp-stream-out stream-write-char)
987     (in-fun echo-bin read-byte lisp-stream-bout stream-write-byte))
988 ram 1.1
989     (defun echo-misc (stream operation &optional arg1 arg2)
990     (let* ((in (two-way-stream-input-stream stream))
991 pw 1.25.2.1 (out (two-way-stream-output-stream stream)))
992 ram 1.1 (case operation
993 pw 1.25.2.1 (: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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (: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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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.25.2.1 (: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 pw 1.25.2.1 (: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.25.2.1 (: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 pw 1.25.2.1 (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 pw 1.25.2.1 (: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.25.2.1 (:element-type 'base-char)))
1288 ram 1.1
1289     ;;;; Indenting streams:
1290    
1291 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (let ((sub-stream (indenting-stream-stream stream)))
1350     (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    
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 pw 1.25.2.1 (: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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 (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 pw 1.25.2.1 ;;; 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 pw 1.25.2.1 (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 pw 1.25.2.1
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     (let ((el (funcall read-function stream nil :eof)))
1679     (when (eq el :eof)
1680     (return i))
1681     (setf (first rem) el)))))
1682     (vector
1683     (with-array-data ((data seq) (offset-start start) (offset-end end))
1684     (typecase data
1685     ((or (simple-array (unsigned-byte 8) (*))
1686 pw 1.25.2.3 (simple-array (signed-byte 8) (*))
1687 pw 1.25.2.1 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    
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     (write-string* seq stream start end))
1732     (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