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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5