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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Tue Aug 23 18:36:22 1994 UTC (19 years, 7 months ago) by ram
Branch: MAIN
Changes since 1.20: +41 -14 lines
When closing composite streams (broadcast, etc.), don't close component
streams.  ANSI sez.

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

  ViewVC Help
Powered by ViewVC 1.1.5