/[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.5 - (show annotations)
Sun Jul 23 14:59:20 2000 UTC (13 years, 8 months ago) by dtc
Branch: RELENG_18
Changes since 1.25.2.4: +7 -5 lines
Fix the handling of :unread and :clear-input in the stream
misc functions synonym-misc, two-way-misc, and concatenated-misc.
These streams encapsulate other input streams which may have an input
buffer so they need to call unread-char and clear-input on the
encapsulated stream rather than directly calling the encapsulated
streams misc method as the misc methods are below the layer of the
input buffer.
1 ;;; -*- Package: Lisp -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; 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 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/stream.lisp,v 1.25.2.5 2000/07/23 14:59:20 dtc Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Stream functions for Spice Lisp.
13 ;;; Written by Skef Wholey and Rob MacLachlan.
14 ;;; Gray streams support by Douglas Crosher, 1998.
15 ;;;
16 ;;; This file contains the OS-independent stream functions.
17 ;;;
18 (in-package "LISP")
19
20 (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 get-output-stream-string stream-element-type input-stream-p
30 output-stream-p open-stream-p interactive-stream-p
31 open-stream-p close read-line read-char
32 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 stream streamp string-stream
36 *standard-input* *standard-output*
37 *error-output* *query-io* *debug-io* *terminal-io* *trace-output*))
38
39 (in-package "SYSTEM")
40 (export '(make-indenting-stream read-n-bytes))
41
42 (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 (deftype string-stream ()
51 '(or string-input-stream string-output-stream
52 fill-pointer-output-stream))
53
54 ;;;; 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 (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 (defun ill-out (stream &rest ignore)
75 (declare (ignore ignore))
76 (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 (defun ill-bin (stream &rest ignore)
82 (declare (ignore ignore))
83 (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 (defun ill-bout (stream &rest ignore)
89 (declare (ignore ignore))
90 (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 (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 ;;; :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 ;;; :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 ;;; :element-type - Return the type of element the stream deals wit<h.
140 ;;; :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 ;;; :interactive-p - Is this an interactive device?
146 ;;;
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
178
179 ;;; Stream manipulation functions.
180
181 (defun input-stream-p (stream)
182 "Returns non-nil if the given Stream can perform input operations."
183 (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
188 (defun output-stream-p (stream)
189 "Returns non-nil if the given Stream can perform output operations."
190 (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
195 (defun open-stream-p (stream)
196 "Return true if Stream is not closed."
197 (declare (type stream stream))
198 (not (eq (lisp-stream-in stream) #'closed-flame)))
199
200 (defun stream-element-type (stream)
201 "Returns a type specifier for the kind of object returned by the Stream."
202 (declare (type stream stream))
203 (funcall (lisp-stream-misc stream) stream :element-type))
204
205 (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 (funcall (lisp-stream-misc stream) stream :interactive-p))
209
210 (defun open-stream-p (stream)
211 "Return true if and only if STREAM has not been closed."
212 (declare (type stream stream))
213 (not (eq (lisp-stream-in stream) #'closed-flame)))
214
215 (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 (declare (type stream stream))
220 (when (open-stream-p stream)
221 (funcall (lisp-stream-misc stream) stream :close abort))
222 t)
223
224 (defun set-closed-flame (stream)
225 (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
234
235 ;;;; 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 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 (cond
249 (position
250 (setf (lisp-stream-in-index stream) in-buffer-length)
251 (funcall (lisp-stream-misc stream) stream :file-position position))
252 (t
253 (let ((res (funcall (lisp-stream-misc stream) stream :file-position nil)))
254 (when res (- res (- in-buffer-length (lisp-stream-in-index stream))))))))
255
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 (funcall (lisp-stream-misc stream) stream :file-length))
265
266
267 ;;; 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 (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
309 ;;; 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 (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
329 (defun unread-char (character &optional (stream *standard-input*))
330 "Puts the Character back on the front of the input Stream."
331 (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 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 (let ((stream (in-synonym-of stream)))
352 (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
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 (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
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 (let ((stream (in-synonym-of stream)))
408 (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
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 (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
430 (defun read-byte (stream &optional (eof-errorp t) eof-value)
431 "Returns the next byte of the Stream."
432 (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
444 (defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
445 "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 (declare (type lisp-stream stream)
454 (type index numbytes start)
455 (type (or (simple-array * (*)) system-area-pointer) buffer))
456 (let* ((stream (in-synonym-of stream lisp-stream))
457 (in-buffer (lisp-stream-in-buffer stream))
458 (index (lisp-stream-in-index stream))
459 (num-buffered (- in-buffer-length index)))
460 (declare (fixnum index num-buffered))
461 (cond
462 ((not in-buffer)
463 (funcall (lisp-stream-n-bin stream) stream buffer start numbytes eof-errorp))
464 ((<= numbytes num-buffered)
465 (%primitive byte-blt in-buffer index buffer start (+ start numbytes))
466 (setf (lisp-stream-in-index stream) (+ index numbytes))
467 numbytes)
468 (t
469 (let ((end (+ start num-buffered)))
470 (%primitive byte-blt in-buffer index buffer start end)
471 (setf (lisp-stream-in-index stream) in-buffer-length)
472 (+ (funcall (lisp-stream-n-bin stream) stream buffer end
473 (- numbytes num-buffered)
474 eof-errorp)
475 num-buffered))))))
476
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 (let* ((ibuf (lisp-stream-in-buffer stream))
491 (count (funcall (lisp-stream-n-bin stream) stream
492 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 (setf (lisp-stream-in-index stream) in-buffer-length)
499 (funcall (lisp-stream-in stream) stream eof-errorp eof-value))
500 (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 (setf (lisp-stream-in-index stream) (1+ start))
508 (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 (let* ((ibuf (lisp-stream-in-buffer stream))
518 (count (funcall (lisp-stream-n-bin stream) stream
519 ibuf 0 in-buffer-length
520 nil))
521 (start (- in-buffer-length count)))
522 (declare (type index start count))
523 (cond ((zerop count)
524 (setf (lisp-stream-in-index stream) in-buffer-length)
525 (funcall (lisp-stream-bin stream) stream eof-errorp eof-value))
526 (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 (setf (lisp-stream-in-index stream) (1+ start))
533 (aref ibuf start)))))
534
535
536 ;;; Output functions:
537
538 (defun write-char (character &optional (stream *standard-output*))
539 "Outputs the Character to the Stream."
540 (with-out-stream stream (lisp-stream-out character)
541 (stream-write-char character))
542 character)
543
544 (defun terpri (&optional (stream *standard-output*))
545 "Outputs a new line to the Stream."
546 (with-out-stream stream (lisp-stream-out #\newline) (stream-terpri))
547 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 (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
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 (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
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 (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
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 (with-out-stream stream (lisp-stream-misc :charpos) (stream-line-column)))
606
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 (with-out-stream stream (lisp-stream-misc :line-length)
611 (stream-line-length)))
612
613 (defun finish-output (&optional (stream *standard-output*))
614 "Attempts to ensure that all output sent to the Stream has reached its
615 destination, and only then returns."
616 (with-out-stream stream (lisp-stream-misc :finish-output)
617 (stream-finish-output))
618 nil)
619
620 (defun force-output (&optional (stream *standard-output*))
621 "Attempts to force any buffered output to be sent."
622 (with-out-stream stream (lisp-stream-misc :force-output)
623 (stream-force-output))
624 nil)
625
626 (defun clear-output (&optional (stream *standard-output*))
627 "Clears the given output Stream."
628 (with-out-stream stream (lisp-stream-misc :clear-output)
629 (stream-force-output))
630 nil)
631
632 (defun write-byte (integer stream)
633 "Outputs the Integer to the binary Stream."
634 (with-out-stream stream (lisp-stream-bout integer)
635 (stream-write-byte integer))
636 integer)
637
638
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 ;;;; Broadcast streams:
680
681 (defstruct (broadcast-stream (:include lisp-stream
682 (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 (streams () :type list :read-only t))
690
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 (macrolet ((out-fun (fun method stream-method &rest args)
699 `(defun ,fun (stream ,@args)
700 (dolist (stream (broadcast-stream-streams stream))
701 (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
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 (let ((charpos (charpos stream)))
715 (if charpos (return charpos)))))
716 (:line-length
717 (let ((min nil))
718 (dolist (stream streams min)
719 (let ((res (line-length stream)))
720 (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 (pushnew (stream-element-type stream) res :test #'equal))))
725 (:close)
726 (t
727 (let ((res nil))
728 (dolist (stream streams res)
729 (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
736 ;;;; Synonym Streams:
737
738 (defstruct (synonym-stream (:include lisp-stream
739 (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 (symbol nil :type symbol :read-only t))
750
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 (macrolet ((out-fun (name slot stream-method &rest args)
763 `(defun ,name (stream ,@args)
764 (declare (optimize (safety 1)))
765 (let ((syn (symbol-value (synonym-stream-symbol stream))))
766 (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
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 (declare (optimize (safety 1)))
786 (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 (declare (optimize (safety 1)))
800 (let ((syn (symbol-value (synonym-stream-symbol stream)))
801 (*previous-stream* stream))
802 (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 (:clear-input (clear-input syn))
808 (:unread (unread-char arg1 syn))
809 (t
810 (funcall (lisp-stream-misc syn) syn operation arg1 arg2)))
811 (stream-misc-dispatch syn operation arg1 arg2))))
812
813 ;;;; Two-Way streams:
814
815 (defstruct (two-way-stream
816 (:include lisp-stream
817 (in #'two-way-in)
818 (bin #'two-way-bin)
819 (n-bin #'two-way-n-bin)
820 (out #'two-way-out)
821 (bout #'two-way-bout)
822 (sout #'two-way-sout)
823 (misc #'two-way-misc))
824 (:print-function %print-two-way-stream)
825 (:constructor make-two-way-stream (input-stream output-stream)))
826 ;; We read from this stream...
827 (input-stream (required-argument) :type stream :read-only t)
828 ;; And write to this one
829 (output-stream (required-argument) :type stream :read-only t))
830
831 (defun %print-two-way-stream (s stream d)
832 (declare (ignore d))
833 (format stream "#<Two-Way Stream, Input = ~S, Output = ~S>"
834 (two-way-stream-input-stream s)
835 (two-way-stream-output-stream s)))
836
837 (setf (documentation 'make-two-way-stream 'function)
838 "Returns a bidirectional stream which gets its input from Input-Stream and
839 sends its output to Output-Stream.")
840
841 (macrolet ((out-fun (name slot stream-method &rest args)
842 `(defun ,name (stream ,@args)
843 (let ((syn (two-way-stream-output-stream stream)))
844 (if (lisp-stream-p syn)
845 (funcall (,slot syn) syn ,@args)
846 (,stream-method syn ,@args))))))
847 (out-fun two-way-out lisp-stream-out stream-write-char ch)
848 (out-fun two-way-bout lisp-stream-bout stream-write-byte n)
849 (out-fun two-way-sout lisp-stream-sout stream-write-string string start end))
850
851 (macrolet ((in-fun (name fun &rest args)
852 `(defun ,name (stream ,@args)
853 (force-output (two-way-stream-output-stream stream))
854 (,fun (two-way-stream-input-stream stream) ,@args))))
855 (in-fun two-way-in read-char eof-errorp eof-value)
856 (in-fun two-way-bin read-byte eof-errorp eof-value)
857 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-errorp))
858
859 (defun two-way-misc (stream operation &optional arg1 arg2)
860 (let* ((in (two-way-stream-input-stream stream))
861 (out (two-way-stream-output-stream stream))
862 (in-lisp-stream-p (lisp-stream-p in))
863 (out-lisp-stream-p (lisp-stream-p out)))
864 (case operation
865 (:listen
866 (if in-lisp-stream-p
867 (or (/= (the fixnum (lisp-stream-in-index in)) in-buffer-length)
868 (funcall (lisp-stream-misc in) in :listen))
869 (stream-listen in)))
870 ((:finish-output :force-output :clear-output)
871 (if out-lisp-stream-p
872 (funcall (lisp-stream-misc out) out operation arg1 arg2)
873 (stream-misc-dispatch out operation arg1 arg2)))
874 (:clear-input (clear-input in))
875 (:unread (unread-char arg1 in))
876 (:element-type
877 (let ((in-type (stream-element-type in))
878 (out-type (stream-element-type out)))
879 (if (equal in-type out-type)
880 in-type `(and ,in-type ,out-type))))
881 (:close
882 (set-closed-flame stream))
883 (t
884 (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
892 ;;;; Concatenated Streams:
893
894 (defstruct (concatenated-stream
895 (:include lisp-stream
896 (in #'concatenated-in)
897 (bin #'concatenated-bin)
898 (misc #'concatenated-misc))
899 (:print-function %print-concatenated-stream)
900 (: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
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 (do ((current (concatenated-stream-streams stream)
917 (cdr current)))
918 ((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 (setf (concatenated-stream-streams stream) (cdr current))))))
924 (in-fun concatenated-in read-char)
925 (in-fun concatenated-bin read-byte))
926
927 (defun concatenated-misc (stream operation &optional arg1 arg2)
928 (let ((left (concatenated-stream-streams stream)))
929 (when left
930 (let* ((current (car left)))
931 (case operation
932 (:listen
933 (loop
934 (let ((stuff (if (lisp-stream-p current)
935 (funcall (lisp-stream-misc current) current
936 :listen)
937 (stream-misc-dispatch current :listen))))
938 (cond ((eq stuff :eof)
939 ;; Advance current, and try again.
940 (pop (concatenated-stream-streams stream))
941 (setf current
942 (car (concatenated-stream-streams stream)))
943 (unless current
944 ;; No further streams. EOF.
945 (return :eof)))
946 (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 (:clear-input (clear-input current))
955 (:unread (unread-char arg1 current))
956 (t
957 (if (lisp-stream-p current)
958 (funcall (lisp-stream-misc current) current operation arg1 arg2)
959 (stream-misc-dispatch current operation arg1 arg2))))))))
960
961
962 ;;;; Echo Streams:
963
964 (defstruct (echo-stream
965 (:include two-way-stream
966 (in #'echo-in)
967 (bin #'echo-bin)
968 (misc #'echo-misc)
969 (n-bin #'ill-bin))
970 (:print-function %print-echo-stream)
971 (:constructor make-echo-stream (input-stream output-stream)))
972 unread-stuff)
973
974
975 (macrolet ((in-fun (name fun out-slot stream-method)
976 `(defun ,name (stream eof-errorp eof-value)
977 (or (pop (echo-stream-unread-stuff stream))
978 (let* ((in (echo-stream-input-stream stream))
979 (out (echo-stream-output-stream stream))
980 (result (,fun in nil :eof)))
981 (cond ((eq result :eof)
982 (eof-or-lose stream eof-errorp eof-value))
983 (t
984 (if (lisp-stream-p out)
985 (funcall (,out-slot out) out result)
986 (,stream-method out result))
987 result)))))))
988 (in-fun echo-in read-char lisp-stream-out stream-write-char)
989 (in-fun echo-bin read-byte lisp-stream-bout stream-write-byte))
990
991 (defun echo-misc (stream operation &optional arg1 arg2)
992 (let* ((in (two-way-stream-input-stream stream))
993 (out (two-way-stream-output-stream stream)))
994 (case operation
995 (:listen
996 (or (not (null (echo-stream-unread-stuff stream)))
997 (if (lisp-stream-p in)
998 (or (/= (the fixnum (lisp-stream-in-index in)) in-buffer-length)
999 (funcall (lisp-stream-misc in) in :listen))
1000 (stream-misc-dispatch in :listen))))
1001 (:unread (push arg1 (echo-stream-unread-stuff stream)))
1002 (:element-type
1003 (let ((in-type (stream-element-type in))
1004 (out-type (stream-element-type out)))
1005 (if (equal in-type out-type)
1006 in-type `(and ,in-type ,out-type))))
1007 (:close
1008 (set-closed-flame stream))
1009 (t
1010 (or (if (lisp-stream-p in)
1011 (funcall (lisp-stream-misc in) in operation arg1 arg2)
1012 (stream-misc-dispatch in operation arg1 arg2))
1013 (if (lisp-stream-p out)
1014 (funcall (lisp-stream-misc out) out operation arg1 arg2)
1015 (stream-misc-dispatch out operation arg1 arg2)))))))
1016
1017 (defun %print-echo-stream (s stream d)
1018 (declare (ignore d))
1019 (format stream "#<Echo Stream, Input = ~S, Output = ~S>"
1020 (two-way-stream-input-stream s)
1021 (two-way-stream-output-stream s)))
1022
1023 (setf (documentation 'make-echo-stream 'function)
1024 "Returns a bidirectional stream which gets its input from Input-Stream and
1025 sends its output to Output-Stream. In addition, all input is echoed to
1026 the output stream")
1027
1028 ;;;; String Input Streams:
1029
1030 (defstruct (string-input-stream
1031 (:include lisp-stream
1032 (in #'string-inch)
1033 (bin #'string-binch)
1034 (n-bin #'string-stream-read-n-bytes)
1035 (misc #'string-in-misc))
1036 (:print-function %print-string-input-stream)
1037 ;(:constructor nil)
1038 (:constructor internal-make-string-input-stream
1039 (string current end)))
1040 (string nil :type simple-string)
1041 (current nil :type index)
1042 (end nil :type index))
1043
1044 (defun %print-string-input-stream (s stream d)
1045 (declare (ignore s d))
1046 (write-string "#<String-Input Stream>" stream))
1047
1048 (defun string-inch (stream eof-errorp eof-value)
1049 (let ((string (string-input-stream-string stream))
1050 (index (string-input-stream-current stream)))
1051 (declare (simple-string string) (fixnum index))
1052 (cond ((= index (the index (string-input-stream-end stream)))
1053 (eof-or-lose stream eof-errorp eof-value))
1054 (t
1055 (setf (string-input-stream-current stream) (1+ index))
1056 (aref string index)))))
1057
1058 (defun string-binch (stream eof-errorp eof-value)
1059 (let ((string (string-input-stream-string stream))
1060 (index (string-input-stream-current stream)))
1061 (declare (simple-string string)
1062 (type index index))
1063 (cond ((= index (the index (string-input-stream-end stream)))
1064 (eof-or-lose stream eof-errorp eof-value))
1065 (t
1066 (setf (string-input-stream-current stream) (1+ index))
1067 (char-code (aref string index))))))
1068
1069 (defun string-stream-read-n-bytes (stream buffer start requested eof-errorp)
1070 (declare (type string-input-stream stream)
1071 (type index start requested))
1072 (let* ((string (string-input-stream-string stream))
1073 (index (string-input-stream-current stream))
1074 (available (- (string-input-stream-end stream) index))
1075 (copy (min available requested)))
1076 (declare (simple-string string)
1077 (type index index available copy))
1078 (when (plusp copy)
1079 (setf (string-input-stream-current stream)
1080 (truly-the index (+ index copy)))
1081 (system:without-gcing
1082 (system-area-copy (vector-sap string)
1083 (* index vm:byte-bits)
1084 (if (typep buffer 'system-area-pointer)
1085 buffer
1086 (vector-sap buffer))
1087 (* start vm:byte-bits)
1088 (* copy vm:byte-bits))))
1089 (if (and (> requested copy) eof-errorp)
1090 (error 'end-of-file :stream stream)
1091 copy)))
1092
1093 (defun string-in-misc (stream operation &optional arg1 arg2)
1094 (declare (ignore arg2))
1095 (case operation
1096 (:file-position
1097 (if arg1
1098 (setf (string-input-stream-current stream) arg1)
1099 (string-input-stream-current stream)))
1100 (:file-length (length (string-input-stream-string stream)))
1101 (:unread (decf (string-input-stream-current stream)))
1102 (:listen (or (/= (the fixnum (string-input-stream-current stream))
1103 (the fixnum (string-input-stream-end stream)))
1104 :eof))
1105 (:element-type 'base-char)))
1106
1107 (defun make-string-input-stream (string &optional
1108 (start 0) (end (length string)))
1109 "Returns an input stream which will supply the characters of String between
1110 Start and End in order."
1111 (declare (type string string)
1112 (type index start)
1113 (type (or index null) end))
1114 (internal-make-string-input-stream (coerce string 'simple-string)
1115 start end))
1116
1117 ;;;; String Output Streams:
1118
1119 (defstruct (string-output-stream
1120 (:include lisp-stream
1121 (out #'string-ouch)
1122 (sout #'string-sout)
1123 (misc #'string-out-misc))
1124 (:print-function %print-string-output-stream)
1125 (:constructor make-string-output-stream ()))
1126 ;; The string we throw stuff in.
1127 (string (make-string 40) :type simple-string)
1128 ;; Index of the next location to use.
1129 (index 0 :type fixnum))
1130
1131 (defun %print-string-output-stream (s stream d)
1132 (declare (ignore s d))
1133 (write-string "#<String-Output Stream>" stream))
1134
1135 (setf (documentation 'make-string-output-stream 'function)
1136 "Returns an Output stream which will accumulate all output given it for
1137 the benefit of the function Get-Output-Stream-String.")
1138
1139 (defun string-ouch (stream character)
1140 (let ((current (string-output-stream-index stream))
1141 (workspace (string-output-stream-string stream)))
1142 (declare (simple-string workspace) (fixnum current))
1143 (if (= current (the fixnum (length workspace)))
1144 (let ((new-workspace (make-string (* current 2))))
1145 (replace new-workspace workspace)
1146 (setf (aref new-workspace current) character)
1147 (setf (string-output-stream-string stream) new-workspace))
1148 (setf (aref workspace current) character))
1149 (setf (string-output-stream-index stream) (1+ current))))
1150
1151 (defun string-sout (stream string start end)
1152 (declare (simple-string string) (fixnum start end))
1153 (let* ((current (string-output-stream-index stream))
1154 (length (- end start))
1155 (dst-end (+ length current))
1156 (workspace (string-output-stream-string stream)))
1157 (declare (simple-string workspace)
1158 (fixnum current length dst-end))
1159 (if (> dst-end (the fixnum (length workspace)))
1160 (let ((new-workspace (make-string (+ (* current 2) length))))
1161 (replace new-workspace workspace :end2 current)
1162 (replace new-workspace string
1163 :start1 current :end1 dst-end
1164 :start2 start :end2 end)
1165 (setf (string-output-stream-string stream) new-workspace))
1166 (replace workspace string
1167 :start1 current :end1 dst-end
1168 :start2 start :end2 end))
1169 (setf (string-output-stream-index stream) dst-end)))
1170
1171 (defun string-out-misc (stream operation &optional arg1 arg2)
1172 (declare (ignore arg2))
1173 (case operation
1174 (:file-position
1175 (if (null arg1)
1176 (string-output-stream-index stream)))
1177 (:charpos
1178 (do ((index (1- (the fixnum (string-output-stream-index stream)))
1179 (1- index))
1180 (count 0 (1+ count))
1181 (string (string-output-stream-string stream)))
1182 ((< index 0) count)
1183 (declare (simple-string string)
1184 (fixnum index count))
1185 (if (char= (schar string index) #\newline)
1186 (return count))))
1187 (:element-type 'base-char)))
1188
1189 (defun get-output-stream-string (stream)
1190 "Returns a string of all the characters sent to a stream made by
1191 Make-String-Output-Stream since the last call to this function."
1192 (declare (type string-output-stream stream))
1193 (let* ((length (string-output-stream-index stream))
1194 (result (make-string length)))
1195 (replace result (string-output-stream-string stream))
1196 (setf (string-output-stream-index stream) 0)
1197 result))
1198
1199 (defun dump-output-stream-string (in-stream out-stream)
1200 "Dumps the characters buffer up in the In-Stream to the Out-Stream as
1201 Get-Output-Stream-String would return them."
1202 (write-string* (string-output-stream-string in-stream) out-stream
1203 0 (string-output-stream-index in-stream))
1204 (setf (string-output-stream-index in-stream) 0))
1205
1206 ;;;; Fill-pointer streams:
1207 ;;;
1208 ;;; Fill pointer string output streams are not explicitly mentioned in
1209 ;;; the CLM, but they are required for the implementation of With-Output-To-String.
1210
1211 (defstruct (fill-pointer-output-stream
1212 (:include lisp-stream
1213 (out #'fill-pointer-ouch)
1214 (sout #'fill-pointer-sout)
1215 (misc #'fill-pointer-misc))
1216 (:print-function
1217 (lambda (s stream d)
1218 (declare (ignore s d))
1219 (write-string "#<Fill-Pointer String Output Stream>" stream)))
1220 (:constructor make-fill-pointer-output-stream (string)))
1221 ;; The string we throw stuff in.
1222 string)
1223
1224
1225 (defun fill-pointer-ouch (stream character)
1226 (let* ((buffer (fill-pointer-output-stream-string stream))
1227 (current (fill-pointer buffer))
1228 (current+1 (1+ current)))
1229 (declare (fixnum current))
1230 (with-array-data ((workspace buffer) (start) (end))
1231 (declare (simple-string workspace))
1232 (let ((offset-current (+ start current)))
1233 (declare (fixnum offset-current))
1234 (if (= offset-current end)
1235 (let* ((new-length (* current 2))
1236 (new-workspace (make-string new-length)))
1237 (declare (simple-string new-workspace))
1238 (%primitive byte-blt workspace start new-workspace 0 current)
1239 (setf workspace new-workspace)
1240 (setf offset-current current)
1241 (set-array-header buffer workspace new-length
1242 current+1 0 new-length nil))
1243 (setf (fill-pointer buffer) current+1))
1244 (setf (schar workspace offset-current) character)))
1245 current+1))
1246
1247
1248 (defun fill-pointer-sout (stream string start end)
1249 (declare (simple-string string) (fixnum start end))
1250 (let* ((buffer (fill-pointer-output-stream-string stream))
1251 (current (fill-pointer buffer))
1252 (string-len (- end start))
1253 (dst-end (+ string-len current)))
1254 (declare (fixnum current dst-end string-len))
1255 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1256 (declare (simple-string workspace))
1257 (let ((offset-dst-end (+ dst-start dst-end))
1258 (offset-current (+ dst-start current)))
1259 (declare (fixnum offset-dst-end offset-current))
1260 (if (> offset-dst-end dst-length)
1261 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1262 (new-workspace (make-string new-length)))
1263 (declare (simple-string new-workspace))
1264 (%primitive byte-blt workspace dst-start new-workspace 0 current)
1265 (setf workspace new-workspace)
1266 (setf offset-current current)
1267 (setf offset-dst-end dst-end)
1268 (set-array-header buffer workspace new-length
1269 dst-end 0 new-length nil))
1270 (setf (fill-pointer buffer) dst-end))
1271 (%primitive byte-blt string start
1272 workspace offset-current offset-dst-end)))
1273 dst-end))
1274
1275
1276 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1277 (declare (ignore arg1 arg2))
1278 (case operation
1279 (:charpos
1280 (let* ((buffer (fill-pointer-output-stream-string stream))
1281 (current (fill-pointer buffer)))
1282 (with-array-data ((string buffer) (start) (end current))
1283 (declare (simple-string string) (ignore start))
1284 (let ((found (position #\newline string :test #'char=
1285 :end end :from-end t)))
1286 (if found
1287 (- end (the fixnum found))
1288 current)))))
1289 (:element-type 'base-char)))
1290
1291 ;;;; Indenting streams:
1292
1293 (defstruct (indenting-stream (:include lisp-stream
1294 (out #'indenting-out)
1295 (sout #'indenting-sout)
1296 (misc #'indenting-misc))
1297 (:print-function %print-indenting-stream)
1298 (:constructor make-indenting-stream (stream)))
1299 ;; The stream we're based on:
1300 stream
1301 ;; How much we indent on each line:
1302 (indentation 0))
1303
1304 (setf (documentation 'make-indenting-stream 'function)
1305 "Returns an ouput stream which indents its output by some amount.")
1306
1307 (defun %print-indenting-stream (s stream d)
1308 (declare (ignore s d))
1309 (write-string "#<Indenting Stream>" stream))
1310
1311 ;;; Indenting-Indent writes the right number of spaces needed to indent output on
1312 ;;; the given Stream based on the specified Sub-Stream.
1313
1314 (defmacro indenting-indent (stream sub-stream)
1315 `(do ((i 0 (+ i 60))
1316 (indentation (indenting-stream-indentation ,stream)))
1317 ((>= i indentation))
1318 (write-string*
1319 " "
1320 ,sub-stream 0 (min 60 (- indentation i)))))
1321
1322 ;;; Indenting-Out writes a character to an indenting stream.
1323
1324 (defun indenting-out (stream char)
1325 (let ((sub-stream (indenting-stream-stream stream)))
1326 (write-char char sub-stream)
1327 (if (char= char #\newline)
1328 (indenting-indent stream sub-stream))))
1329
1330 ;;; Indenting-Sout writes a string to an indenting stream.
1331
1332 (defun indenting-sout (stream string start end)
1333 (declare (simple-string string) (fixnum start end))
1334 (do ((i start)
1335 (sub-stream (indenting-stream-stream stream)))
1336 ((= i end))
1337 (let ((newline (position #\newline string :start i :end end)))
1338 (cond (newline
1339 (write-string* string sub-stream i (1+ newline))
1340 (indenting-indent stream sub-stream)
1341 (setq i (+ newline 1)))
1342 (t
1343 (write-string* string sub-stream i end)
1344 (setq i end))))))
1345
1346 ;;; Indenting-Misc just treats just the :Line-Length message differently.
1347 ;;; Indenting-Charpos says the charpos is the charpos of the base stream minus
1348 ;;; the stream's indentation.
1349
1350 (defun indenting-misc (stream operation &optional arg1 arg2)
1351 (let ((sub-stream (indenting-stream-stream stream)))
1352 (if (lisp-stream-p sub-stream)
1353 (let ((method (lisp-stream-misc sub-stream)))
1354 (case operation
1355 (:line-length
1356 (let ((line-length (funcall method sub-stream operation)))
1357 (if line-length
1358 (- line-length (indenting-stream-indentation stream)))))
1359 (:charpos
1360 (let ((charpos (funcall method sub-stream operation)))
1361 (if charpos
1362 (- charpos (indenting-stream-indentation stream)))))
1363 (t
1364 (funcall method sub-stream operation arg1 arg2))))
1365 ;; Fundamental-stream.
1366 (case operation
1367 (:line-length
1368 (let ((line-length (stream-line-length sub-stream)))
1369 (if line-length
1370 (- line-length (indenting-stream-indentation stream)))))
1371 (:charpos
1372 (let ((charpos (stream-line-column sub-stream)))
1373 (if charpos
1374 (- charpos (indenting-stream-indentation stream)))))
1375 (t
1376 (stream-misc-dispatch sub-stream operation arg1 arg2))))))
1377
1378
1379 (proclaim '(maybe-inline read-char unread-char read-byte listen))
1380
1381
1382
1383 ;;;; Case frobbing streams, used by format ~(...~).
1384
1385 (defstruct (case-frob-stream
1386 (:include lisp-stream
1387 (:misc #'case-frob-misc))
1388 (:constructor %make-case-frob-stream (target out sout)))
1389 (target (required-argument) :type stream))
1390
1391 (defun make-case-frob-stream (target kind)
1392 "Returns a stream that sends all output to the stream TARGET, but modifies
1393 the case of letters, depending on KIND, which should be one of:
1394 :upcase - convert to upper case.
1395 :downcase - convert to lower case.
1396 :capitalize - convert the first letter of words to upper case and the
1397 rest of the word to lower case.
1398 :capitalize-first - convert the first letter of the first word to upper
1399 case and everything else to lower case."
1400 (declare (type stream target)
1401 (type (member :upcase :downcase :capitalize :capitalize-first)
1402 kind)
1403 (values stream))
1404 (if (case-frob-stream-p target)
1405 ;; If we are going to be writing to a stream that already does case
1406 ;; frobbing, why bother frobbing the case just so it can frob it
1407 ;; again?
1408 target
1409 (multiple-value-bind
1410 (out sout)
1411 (ecase kind
1412 (:upcase
1413 (values #'case-frob-upcase-out
1414 #'case-frob-upcase-sout))
1415 (:downcase
1416 (values #'case-frob-downcase-out
1417 #'case-frob-downcase-sout))
1418 (:capitalize
1419 (values #'case-frob-capitalize-out
1420 #'case-frob-capitalize-sout))
1421 (:capitalize-first
1422 (values #'case-frob-capitalize-first-out
1423 #'case-frob-capitalize-first-sout)))
1424 (%make-case-frob-stream target out sout))))
1425
1426 (defun case-frob-misc (stream op &optional arg1 arg2)
1427 (declare (type case-frob-stream stream))
1428 (case op
1429 (:close)
1430 (t
1431 (let ((target (case-frob-stream-target stream)))
1432 (if (lisp-stream-p target)
1433 (funcall (lisp-stream-misc target) target op arg1 arg2)
1434 (stream-misc-dispatch target op arg1 arg2))))))
1435
1436 (defun case-frob-upcase-out (stream char)
1437 (declare (type case-frob-stream stream)
1438 (type base-char char))
1439 (let ((target (case-frob-stream-target stream))
1440 (char (char-upcase char)))
1441 (if (lisp-stream-p target)
1442 (funcall (lisp-stream-out target) target char)
1443 (stream-write-char target char))))
1444
1445 (defun case-frob-upcase-sout (stream str start end)
1446 (declare (type case-frob-stream stream)
1447 (type simple-base-string str)
1448 (type index start)
1449 (type (or index null) end))
1450 (let* ((target (case-frob-stream-target stream))
1451 (len (length str))
1452 (end (or end len))
1453 (string (if (and (zerop start) (= len end))
1454 (string-upcase str)
1455 (nstring-upcase (subseq str start end))))
1456 (string-len (- end start)))
1457 (if (lisp-stream-p target)
1458 (funcall (lisp-stream-sout target) target string 0 string-len)
1459 (stream-write-string target string 0 string-len))))
1460
1461 (defun case-frob-downcase-out (stream char)
1462 (declare (type case-frob-stream stream)
1463 (type base-char char))
1464 (let ((target (case-frob-stream-target stream))
1465 (char (char-downcase char)))
1466 (if (lisp-stream-p target)
1467 (funcall (lisp-stream-out target) target char)
1468 (stream-write-char target char))))
1469
1470 (defun case-frob-downcase-sout (stream str start end)
1471 (declare (type case-frob-stream stream)
1472 (type simple-base-string str)
1473 (type index start)
1474 (type (or index null) end))
1475 (let* ((target (case-frob-stream-target stream))
1476 (len (length str))
1477 (end (or end len))
1478 (string (if (and (zerop start) (= len end))
1479 (string-downcase str)
1480 (nstring-downcase (subseq str start end))))
1481 (string-len (- end start)))
1482 (if (lisp-stream-p target)
1483 (funcall (lisp-stream-sout target) target string 0 string-len)
1484 (stream-write-string target string 0 string-len))))
1485
1486 (defun case-frob-capitalize-out (stream char)
1487 (declare (type case-frob-stream stream)
1488 (type base-char char))
1489 (let ((target (case-frob-stream-target stream)))
1490 (cond ((alphanumericp char)
1491 (let ((char (char-upcase char)))
1492 (if (lisp-stream-p target)
1493 (funcall (lisp-stream-out target) target char)
1494 (stream-write-char target char)))
1495 (setf (case-frob-stream-out stream) #'case-frob-capitalize-aux-out)
1496 (setf (case-frob-stream-sout stream)
1497 #'case-frob-capitalize-aux-sout))
1498 (t
1499 (if (lisp-stream-p target)
1500 (funcall (lisp-stream-out target) target char)
1501 (stream-write-char target char))))))
1502
1503 (defun case-frob-capitalize-sout (stream str start end)
1504 (declare (type case-frob-stream stream)
1505 (type simple-base-string str)
1506 (type index start)
1507 (type (or index null) end))
1508 (let* ((target (case-frob-stream-target stream))
1509 (str (subseq str start end))
1510 (len (length str))
1511 (inside-word nil))
1512 (dotimes (i len)
1513 (let ((char (schar str i)))
1514 (cond ((not (alphanumericp char))
1515 (setf inside-word nil))
1516 (inside-word
1517 (setf (schar str i) (char-downcase char)))
1518 (t
1519 (setf inside-word t)
1520 (setf (schar str i) (char-upcase char))))))
1521 (when inside-word
1522 (setf (case-frob-stream-out stream)
1523 #'case-frob-capitalize-aux-out)
1524 (setf (case-frob-stream-sout stream)
1525 #'case-frob-capitalize-aux-sout))
1526 (if (lisp-stream-p target)
1527 (funcall (lisp-stream-sout target) target str 0 len)
1528 (stream-write-string target str 0 len))))
1529
1530 (defun case-frob-capitalize-aux-out (stream char)
1531 (declare (type case-frob-stream stream)
1532 (type base-char char))
1533 (let ((target (case-frob-stream-target stream)))
1534 (cond ((alphanumericp char)
1535 (let ((char (char-downcase char)))
1536 (if (lisp-stream-p target)
1537 (funcall (lisp-stream-out target) target char)
1538 (stream-write-char target char))))
1539 (t
1540 (if (lisp-stream-p target)
1541 (funcall (lisp-stream-out target) target char)
1542 (stream-write-char target char))
1543 (setf (case-frob-stream-out stream)
1544 #'case-frob-capitalize-out)
1545 (setf (case-frob-stream-sout stream)
1546 #'case-frob-capitalize-sout)))))
1547
1548 (defun case-frob-capitalize-aux-sout (stream str start end)
1549 (declare (type case-frob-stream stream)
1550 (type simple-base-string str)
1551 (type index start)
1552 (type (or index null) end))
1553 (let* ((target (case-frob-stream-target stream))
1554 (str (subseq str start end))
1555 (len (length str))
1556 (inside-word t))
1557 (dotimes (i len)
1558 (let ((char (schar str i)))
1559 (cond ((not (alphanumericp char))
1560 (setf inside-word nil))
1561 (inside-word
1562 (setf (schar str i) (char-downcase char)))
1563 (t
1564 (setf inside-word t)
1565 (setf (schar str i) (char-upcase char))))))
1566 (unless inside-word
1567 (setf (case-frob-stream-out stream)
1568 #'case-frob-capitalize-out)
1569 (setf (case-frob-stream-sout stream)
1570 #'case-frob-capitalize-sout))
1571 (if (lisp-stream-p target)
1572 (funcall (lisp-stream-sout target) target str 0 len)
1573 (stream-write-string target str 0 len))))
1574
1575 (defun case-frob-capitalize-first-out (stream char)
1576 (declare (type case-frob-stream stream)
1577 (type base-char char))
1578 (let ((target (case-frob-stream-target stream)))
1579 (cond ((alphanumericp char)
1580 (let ((char (char-upcase char)))
1581 (if (lisp-stream-p target)
1582 (funcall (lisp-stream-out target) target char)
1583 (stream-write-char target char)))
1584 (setf (case-frob-stream-out stream)
1585 #'case-frob-downcase-out)
1586 (setf (case-frob-stream-sout stream)
1587 #'case-frob-downcase-sout))
1588 (t
1589 (if (lisp-stream-p target)
1590 (funcall (lisp-stream-out target) target char)
1591 (stream-write-char target char))))))
1592
1593 (defun case-frob-capitalize-first-sout (stream str start end)
1594 (declare (type case-frob-stream stream)
1595 (type simple-base-string str)
1596 (type index start)
1597 (type (or index null) end))
1598 (let* ((target (case-frob-stream-target stream))
1599 (str (subseq str start end))
1600 (len (length str)))
1601 (dotimes (i len)
1602 (let ((char (schar str i)))
1603 (when (alphanumericp char)
1604 (setf (schar str i) (char-upcase char))
1605 (do ((i (1+ i) (1+ i)))
1606 ((= i len))
1607 (setf (schar str i) (char-downcase (schar str i))))
1608 (setf (case-frob-stream-out stream)
1609 #'case-frob-downcase-out)
1610 (setf (case-frob-stream-sout stream)
1611 #'case-frob-downcase-sout)
1612 (return))))
1613 (if (lisp-stream-p target)
1614 (funcall (lisp-stream-sout target) target str 0 len)
1615 (stream-write-string target str 0 len))))
1616
1617
1618 ;;;; Public interface from "EXTENSIONS" package.
1619
1620 (defstruct (stream-command (:print-function print-stream-command)
1621 (:constructor make-stream-command
1622 (name &optional args)))
1623 (name nil :type symbol)
1624 (args nil :type list))
1625
1626 (defun print-stream-command (obj str n)
1627 (declare (ignore n))
1628 (format str "#<Stream-Cmd ~S>" (stream-command-name obj)))
1629
1630
1631 ;;; GET-STREAM-COMMAND -- Public.
1632 ;;;
1633 ;;; We can't simply call the stream's misc method because nil is an
1634 ;;; ambiguous return value: does it mean text arrived, or does it mean the
1635 ;;; stream's misc method had no :get-command implementation. We can't return
1636 ;;; nil until there is text input. We don't need to loop because any stream
1637 ;;; implementing :get-command would wait until it had some input. If the
1638 ;;; LISTEN fails, then we have some random stream we must wait on.
1639 ;;;
1640 (defun get-stream-command (stream)
1641 "This takes a stream and waits for text or a command to appear on it. If
1642 text appears before a command, this returns nil, and otherwise it returns
1643 a command."
1644 (let ((cmdp (funcall (lisp-stream-misc stream) stream :get-command)))
1645 (cond (cmdp)
1646 ((listen stream)
1647 nil)
1648 (t
1649 ;; This waits for input and returns nil when it arrives.
1650 (unread-char (read-char stream) stream)))))
1651
1652
1653 ;;; READ-SEQUENCE -- Public
1654 ;;;
1655 (defun read-sequence (seq stream &key (start 0) (end nil))
1656 "Destructively modify SEQ by reading elements from STREAM.
1657 SEQ is bounded by START and END. SEQ is destructively modified by
1658 copying successive elements into it from STREAM. If the end of file
1659 for STREAM is reached before copying all elements of the subsequence,
1660 then the extra elements near the end of sequence are not updated, and
1661 the index of the next element is returned."
1662 (declare (type sequence seq)
1663 (type stream stream)
1664 (type index start)
1665 (type sequence-end end)
1666 (values index))
1667 (let ((end (or end (length seq))))
1668 (declare (type index end))
1669 (etypecase seq
1670 (list
1671 (let ((read-function
1672 (if (subtypep (stream-element-type stream) 'character)
1673 #'read-char
1674 #'read-byte)))
1675 (do ((rem (nthcdr start seq) (rest rem))
1676 (i start (1+ i)))
1677 ((or (endp rem) (>= i end)) i)
1678 (declare (type list rem)
1679 (type index i))
1680 (let ((el (funcall read-function stream nil :eof)))
1681 (when (eq el :eof)
1682 (return i))
1683 (setf (first rem) el)))))
1684 (vector
1685 (with-array-data ((data seq) (offset-start start) (offset-end end))
1686 (typecase data
1687 ((or (simple-array (unsigned-byte 8) (*))
1688 (simple-array (signed-byte 8) (*))
1689 simple-string)
1690 (let* ((numbytes (- end start))
1691 (bytes-read (system:read-n-bytes
1692 stream data offset-start numbytes nil)))
1693 (if (< bytes-read numbytes)
1694 (+ start bytes-read)
1695 end)))
1696 (t
1697 (let ((read-function
1698 (if (subtypep (stream-element-type stream) 'character)
1699 #'read-char
1700 #'read-byte)))
1701 (do ((i offset-start (1+ i)))
1702 ((>= i offset-end) end)
1703 (declare (type index i))
1704 (let ((el (funcall read-function stream nil :eof)))
1705 (when (eq el :eof)
1706 (return (+ start (- i offset-start))))
1707 (setf (aref data i) el)))))))))))
1708
1709 ;;; WRITE-SEQUENCE -- Public
1710 ;;;
1711 (defun write-sequence (seq stream &key (start 0) (end nil))
1712 "Write the elements of SEQ bounded by START and END to STREAM."
1713 (declare (type sequence seq)
1714 (type stream stream)
1715 (type index start)
1716 (type sequence-end end)
1717 (values sequence))
1718 (let ((end (or end (length seq))))
1719 (declare (type index start end))
1720 (etypecase seq
1721 (list
1722 (let ((write-function
1723 (if (subtypep (stream-element-type stream) 'character)
1724 #'write-char
1725 #'write-byte)))
1726 (do ((rem (nthcdr start seq) (rest rem))
1727 (i start (1+ i)))
1728 ((or (endp rem) (>= i end)) seq)
1729 (declare (type list rem)
1730 (type index i))
1731 (funcall write-function (first rem) stream))))
1732 (string
1733 (write-string* seq stream start end))
1734 (vector
1735 (let ((write-function
1736 (if (subtypep (stream-element-type stream) 'character)
1737 #'write-char
1738 #'write-byte)))
1739 (do ((i start (1+ i)))
1740 ((>= i end) seq)
1741 (declare (type index i))
1742 (funcall write-function (aref seq i) stream)))))))

  ViewVC Help
Powered by ViewVC 1.1.5