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

Contents of /src/code/stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations)
Fri Mar 12 21:05:38 1993 UTC (21 years, 1 month ago) by wlott
Branch: MAIN
Branch point for: solaris_patch
Changes since 1.18: +6 -5 lines
Fixed type decls in FILE-POSITION to allow :START and :END for positions.
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.19 1993/03/12 21:05:38 wlott 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 (funcall (stream-in stream) stream
279 eof-errorp eof-value)))
280 (t
281 (done-with-fast-read-char)
282 (return (values (shrink-vector res index) t)))))))))
283
284
285 ;;; We proclaim them inline here, then proclaim them notinline at EOF,
286 ;;; so, except in this file, they are not inline by default, but they can be.
287 ;;;
288 (proclaim '(inline read-char unread-char read-byte listen))
289 (defun read-char (&optional (stream *standard-input*) (eof-errorp t) eof-value
290 recursive-p)
291 "Inputs a character from Stream and returns it."
292 (declare (ignore recursive-p))
293 (prepare-for-fast-read-char stream
294 (prog1
295 (fast-read-char eof-errorp eof-value)
296 (done-with-fast-read-char))))
297
298 (defun unread-char (character &optional (stream *standard-input*))
299 "Puts the Character back on the front of the input Stream."
300 (let* ((stream (in-synonym-of stream))
301 (index (1- (stream-in-index stream)))
302 (buffer (stream-in-buffer stream)))
303 (declare (fixnum index))
304 (when (minusp index) (error "Nothing to unread."))
305 (cond (buffer
306 (setf (aref buffer index) (char-code character))
307 (setf (stream-in-index stream) index))
308 (t
309 (funcall (stream-misc stream) stream :unread character))))
310 nil)
311
312 (defun peek-char (&optional (peek-type nil) (stream *standard-input*)
313 (eof-errorp t) eof-value recursive-p)
314 "Peeks at the next character in the input Stream. See manual for details."
315 (declare (ignore recursive-p))
316 (let* ((stream (in-synonym-of stream))
317 (char (read-char stream eof-errorp eof-value)))
318 (cond ((eq char eof-value) char)
319 ((characterp peek-type)
320 (do ((char char (read-char stream eof-errorp eof-value)))
321 ((or (eq char eof-value) (char= char peek-type))
322 (unless (eq char eof-value)
323 (unread-char char stream))
324 char)))
325 ((eq peek-type t)
326 (do ((char char (read-char stream eof-errorp eof-value)))
327 ((or (eq char eof-value) (not (whitespace-char-p char)))
328 (unless (eq char eof-value)
329 (unread-char char stream))
330 char)))
331 (t
332 (unread-char char stream)
333 char))))
334
335 (defun listen (&optional (stream *standard-input*))
336 "Returns T if a character is availible on the given Stream."
337 (let ((stream (in-synonym-of stream)))
338 (or (/= (the fixnum (stream-in-index stream)) in-buffer-length)
339 ;; Test for t explicitly since misc methods return :eof sometimes.
340 (eq (funcall (stream-misc stream) stream :listen) t))))
341
342 (defun read-char-no-hang (&optional (stream *standard-input*)
343 (eof-errorp t) eof-value recursive-p)
344 "Returns the next character from the Stream if one is availible, or nil."
345 (declare (ignore recursive-p))
346 (let ((stream (in-synonym-of stream)))
347 (if (funcall (stream-misc stream) stream :listen)
348 ;; On t or :eof get READ-CHAR to do the work.
349 (read-char stream eof-errorp eof-value)
350 nil)))
351
352 (defun clear-input (&optional (stream *standard-input*))
353 "Clears any buffered input associated with the Stream."
354 (let ((stream (in-synonym-of stream)))
355 (setf (stream-in-index stream) in-buffer-length)
356 (funcall (stream-misc stream) stream :clear-input)
357 nil))
358
359 (defun read-byte (stream &optional (eof-errorp t) eof-value)
360 "Returns the next byte of the Stream."
361 (prepare-for-fast-read-byte stream
362 (prog1
363 (fast-read-byte eof-errorp eof-value t)
364 (done-with-fast-read-byte))))
365
366 (defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
367 "Reads Numbytes bytes into the Buffer starting at Start, and returns
368 the number of bytes actually read if the end of file was hit before Numbytes
369 bytes were read (and Eof-Errorp is false)."
370 (declare (type index numbytes start)
371 (type (or (simple-array * (*)) system-area-pointer) buffer))
372 (let* ((stream (in-synonym-of stream))
373 (in-buffer (stream-in-buffer stream))
374 (index (stream-in-index stream))
375 (num-buffered (- in-buffer-length index)))
376 (declare (fixnum index num-buffered))
377 (cond
378 ((not in-buffer)
379 (funcall (stream-n-bin stream) stream buffer start numbytes eof-errorp))
380 ((<= numbytes num-buffered)
381 (%primitive byte-blt in-buffer index buffer start (+ start numbytes))
382 (setf (stream-in-index stream) (+ index numbytes))
383 numbytes)
384 (t
385 (let ((end (+ start num-buffered)))
386 (%primitive byte-blt in-buffer index buffer start end)
387 (setf (stream-in-index stream) in-buffer-length)
388 (+ (funcall (stream-n-bin stream) stream buffer end
389 (- numbytes num-buffered)
390 eof-errorp)
391 num-buffered))))))
392
393
394 ;;; Amount of space we leave at the start of the in-buffer for unreading. 4
395 ;;; instead of 1 to allow word-aligned copies.
396 ;;;
397 (defconstant in-buffer-extra 4)
398
399 ;;; FAST-READ-CHAR-REFILL -- Interface
400 ;;;
401 ;;; This function is called by the fast-read-char expansion to refill the
402 ;;; in-buffer for text streams. There is definitely an in-buffer, and hence
403 ;;; myst be an n-bin method.
404 ;;;
405 (defun fast-read-char-refill (stream eof-errorp eof-value)
406 (let* ((ibuf (stream-in-buffer stream))
407 (count (funcall (stream-n-bin stream) stream
408 ibuf in-buffer-extra
409 (- in-buffer-length in-buffer-extra)
410 nil))
411 (start (- in-buffer-length count)))
412 (declare (type index start count))
413 (cond ((zerop count)
414 (setf (stream-in-index stream) in-buffer-length)
415 (funcall (stream-in stream) stream eof-errorp eof-value))
416 (t
417 (when (/= start in-buffer-extra)
418 (bit-bash-copy ibuf (+ (* in-buffer-extra vm:byte-bits)
419 (* vm:vector-data-offset vm:word-bits))
420 ibuf (+ (the index (* start vm:byte-bits))
421 (* vm:vector-data-offset vm:word-bits))
422 (* count vm:byte-bits)))
423 (setf (stream-in-index stream) (1+ start))
424 (code-char (aref ibuf start))))))
425
426
427 ;;; FAST-READ-BYTE-REFILL -- Interface
428 ;;;
429 ;;; Similar to FAST-READ-CHAR-REFILL, but we don't have to leave room for
430 ;;; unreading.
431 ;;;
432 (defun fast-read-byte-refill (stream eof-errorp eof-value)
433 (let* ((ibuf (stream-in-buffer stream))
434 (count (funcall (stream-n-bin stream) stream
435 ibuf 0 in-buffer-length
436 nil))
437 (start (- in-buffer-length count)))
438 (declare (type index start count))
439 (cond ((zerop count)
440 (setf (stream-in-index stream) in-buffer-length)
441 (funcall (stream-bin stream) stream eof-errorp eof-value))
442 (t
443 (unless (zerop start)
444 (bit-bash-copy ibuf (* vm:vector-data-offset vm:word-bits)
445 ibuf (+ (the index (* start vm:byte-bits))
446 (* vm:vector-data-offset vm:word-bits))
447 (* count vm:byte-bits)))
448 (setf (stream-in-index stream) (1+ start))
449 (aref ibuf start)))))
450
451
452 ;;; Output functions:
453
454 (defun write-char (character &optional (stream *standard-output*))
455 "Outputs the Character to the Stream."
456 (with-out-stream stream stream-out character)
457 character)
458
459 (defun terpri (&optional (stream *standard-output*))
460 "Outputs a new line to the Stream."
461 (with-out-stream stream stream-out #\newline)
462 nil)
463
464 (defun fresh-line (&optional (stream *standard-output*))
465 "Outputs a new line to the Stream if it is not positioned at the begining of
466 a line. Returns T if it output a new line, nil otherwise."
467 (let ((stream (out-synonym-of stream)))
468 (when (/= (or (charpos stream) 1) 0)
469 (funcall (stream-out stream) stream #\newline)
470 t)))
471
472 (defun write-string (string &optional (stream *standard-output*)
473 &key (start 0) (end (length (the vector string))))
474 "Outputs the String to the given Stream."
475 (write-string* string stream start end))
476
477 (defun write-string* (string &optional (stream *standard-output*)
478 (start 0) (end (length (the vector string))))
479 (declare (fixnum start end))
480 (if (array-header-p string)
481 (with-array-data ((data string) (offset-start start) (offset-end end))
482 (with-out-stream stream stream-sout data offset-start offset-end))
483 (with-out-stream stream stream-sout string start end))
484 string)
485
486 (defun write-line (string &optional (stream *standard-output*)
487 &key (start 0) (end (length string)))
488 "Outputs the String to the given Stream, followed by a newline character."
489 (write-line* string stream start end))
490
491 (defun write-line* (string &optional (stream *standard-output*)
492 (start 0) (end (length string)))
493 (declare (fixnum start end))
494 (let ((stream (out-synonym-of stream)))
495 (if (array-header-p string)
496 (with-array-data ((data string) (offset-start start) (offset-end end))
497 (with-out-stream stream stream-sout data offset-start offset-end))
498 (with-out-stream stream stream-sout string start end))
499 (funcall (stream-out stream) stream #\newline))
500 string)
501
502 (defun charpos (&optional (stream *standard-output*))
503 "Returns the number of characters on the current line of output of the given
504 Stream, or Nil if that information is not availible."
505 (with-out-stream stream stream-misc :charpos))
506
507 (defun line-length (&optional (stream *standard-output*))
508 "Returns the number of characters that will fit on a line of output on the
509 given Stream, or Nil if that information is not available."
510 (with-out-stream stream stream-misc :line-length))
511
512 (defun finish-output (&optional (stream *standard-output*))
513 "Attempts to ensure that all output sent to the the Stream has reached its
514 destination, and only then returns."
515 (with-out-stream stream stream-misc :finish-output)
516 nil)
517
518 (defun force-output (&optional (stream *standard-output*))
519 "Attempts to force any buffered output to be sent."
520 (with-out-stream stream stream-misc :force-output)
521 nil)
522
523 (defun clear-output (&optional (stream *standard-output*))
524 "Clears the given output Stream."
525 (with-out-stream stream stream-misc :clear-output)
526 nil)
527
528 (defun write-byte (integer stream)
529 "Outputs the Integer to the binary Stream."
530 (with-out-stream stream stream-bout integer)
531 integer)
532
533 ;;;; Broadcast streams:
534
535 (defstruct (broadcast-stream (:include stream
536 (out #'broadcast-out)
537 (bout #'broadcast-bout)
538 (sout #'broadcast-sout)
539 (misc #'broadcast-misc))
540 (:print-function %print-broadcast-stream)
541 (:constructor make-broadcast-stream (&rest streams)))
542 ;; This is a list of all the streams we broadcast to.
543 (streams () :type list :read-only t))
544
545 (setf (documentation 'make-broadcast-stream 'function)
546 "Returns an ouput stream which sends its output to all of the given streams.")
547
548 (defun %print-broadcast-stream (s stream d)
549 (declare (ignore s d))
550 (write-string "#<Broadcast Stream>" stream))
551
552 (macrolet ((out-fun (fun method &rest args)
553 `(defun ,fun (stream ,@args)
554 (dolist (stream (broadcast-stream-streams stream))
555 (funcall (,method stream) stream ,@args)))))
556 (out-fun broadcast-out stream-out char)
557 (out-fun broadcast-bout stream-bout byte)
558 (out-fun broadcast-sout stream-sout string start end))
559
560 (defun broadcast-misc (stream operation &optional arg1 arg2)
561 (let ((streams (broadcast-stream-streams stream)))
562 (case operation
563 (:charpos
564 (dolist (stream streams)
565 (let ((charpos (funcall (stream-misc stream) stream :charpos)))
566 (if charpos (return charpos)))))
567 (:line-length
568 (let ((min nil))
569 (dolist (stream streams min)
570 (let ((res (funcall (stream-misc stream) stream :line-length)))
571 (when res (setq min (if min (min res min) res)))))))
572 (:element-type
573 (let (res)
574 (dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
575 (pushnew (funcall (stream-misc stream) stream :element-type) res
576 :test #'equal))))
577 (t
578 (let ((res nil))
579 (dolist (stream streams res)
580 (setq res (funcall (stream-misc stream) stream operation
581 arg1 arg2))))))))
582
583 ;;;; Synonym Streams:
584
585 (defstruct (synonym-stream (:include stream
586 (in #'synonym-in)
587 (bin #'synonym-bin)
588 (n-bin #'synonym-n-bin)
589 (out #'synonym-out)
590 (bout #'synonym-bout)
591 (sout #'synonym-sout)
592 (misc #'synonym-misc))
593 (:print-function %print-synonym-stream)
594 (:constructor make-synonym-stream (symbol)))
595 ;; This is the symbol, the value of which is the stream we are synonym to.
596 (symbol nil :type symbol :read-only t))
597
598 (defun %print-synonym-stream (s stream d)
599 (declare (ignore d))
600 (format stream "#<Synonym Stream to ~S>" (synonym-stream-symbol s)))
601
602 (setf (documentation 'make-synonym-stream 'function)
603 "Returns a stream which performs its operations on the stream which is the
604 value of the dynamic variable named by Symbol.")
605
606 ;;; The output simple output methods just call the corresponding method
607 ;;; in the synonymed stream.
608 ;;;
609 (macrolet ((out-fun (name slot &rest args)
610 `(defun ,name (stream ,@args)
611 (declare (optimize (safety 1)))
612 (let ((syn (symbol-value (synonym-stream-symbol stream))))
613 (funcall (,slot syn) syn ,@args)))))
614 (out-fun synonym-out stream-out ch)
615 (out-fun synonym-bout stream-bout n)
616 (out-fun synonym-sout stream-sout string start end))
617
618
619 ;;; Bind synonym stream to this so that SPIO can turn on the right frob in
620 ;;; the icon when we are in a terminal input wait.
621 ;;;
622 (defvar *previous-stream* nil)
623
624 ;;; For the input methods, we just call the corresponding function on the
625 ;;; synonymed stream. These functions deal with getting input out of
626 ;;; the In-Buffer if there is any.
627 ;;;
628 (macrolet ((in-fun (name fun &rest args)
629 `(defun ,name (stream ,@args)
630 (declare (optimize (safety 1)))
631 (let ((*previous-stream* stream))
632 (,fun (symbol-value (synonym-stream-symbol stream)) ,@args)))))
633 (in-fun synonym-in read-char eof-errorp eof-value)
634 (in-fun synonym-bin read-byte eof-errorp eof-value)
635 (in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-errorp))
636
637
638 ;;; Synonym-Misc -- Internal
639 ;;;
640 ;;; We have to special-case the operations which could look at stuff in
641 ;;; the in-buffer.
642 ;;;
643 (defun synonym-misc (stream operation &optional arg1 arg2)
644 (declare (optimize (safety 1)))
645 (let ((syn (symbol-value (synonym-stream-symbol stream)))
646 (*previous-stream* stream))
647 (case operation
648 (:listen (or (/= (the fixnum (stream-in-index syn)) in-buffer-length)
649 (funcall (stream-misc syn) syn :listen)))
650 (t
651 (funcall (stream-misc syn) syn operation arg1 arg2)))))
652
653 ;;;; Two-Way streams:
654
655 (defstruct (two-way-stream
656 (:include stream
657 (in #'two-way-in)
658 (bin #'two-way-bin)
659 (n-bin #'two-way-n-bin)
660 (out #'two-way-out)
661 (bout #'two-way-bout)
662 (sout #'two-way-sout)
663 (misc #'two-way-misc))
664 (:print-function %print-two-way-stream)
665 (:constructor make-two-way-stream (input-stream output-stream)))
666 ;; We read from this stream...
667 (input-stream (required-argument) :type stream :read-only t)
668 ;; And write to this one
669 (output-stream (required-argument) :type stream :read-only t))
670
671 (defun %print-two-way-stream (s stream d)
672 (declare (ignore d))
673 (format stream "#<Two-Way Stream, Input = ~S, Output = ~S>"
674 (two-way-stream-input-stream s)
675 (two-way-stream-output-stream s)))
676
677 (setf (documentation 'make-two-way-stream 'function)
678 "Returns a bidirectional stream which gets its input from Input-Stream and
679 sends its output to Output-Stream.")
680
681 (macrolet ((out-fun (name slot &rest args)
682 `(defun ,name (stream ,@args)
683 (let ((syn (two-way-stream-output-stream stream)))
684 (funcall (,slot syn) syn ,@args)))))
685 (out-fun two-way-out stream-out ch)
686 (out-fun two-way-bout stream-bout n)
687 (out-fun two-way-sout stream-sout string start end))
688
689 (macrolet ((in-fun (name fun &rest args)
690 `(defun ,name (stream ,@args)
691 (force-output (two-way-stream-output-stream stream))
692 (,fun (two-way-stream-input-stream stream) ,@args))))
693 (in-fun two-way-in read-char eof-errorp eof-value)
694 (in-fun two-way-bin read-byte eof-errorp eof-value)
695 (in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-errorp))
696
697 (defun two-way-misc (stream operation &optional arg1 arg2)
698 (let* ((in (two-way-stream-input-stream stream))
699 (in-method (stream-misc in))
700 (out (two-way-stream-output-stream stream))
701 (out-method (stream-misc out)))
702 (case operation
703 (:listen (or (/= (the fixnum (stream-in-index in)) in-buffer-length)
704 (funcall in-method in :listen)))
705 ((:finish-output :force-output :clear-output)
706 (funcall out-method out operation arg1 arg2))
707 ((:clear-input :unread)
708 (funcall in-method in operation arg1 arg2))
709 (:element-type
710 (let ((in-type (funcall in-method in :element-type))
711 (out-type (funcall out-method out :element-type)))
712 (if (equal in-type out-type)
713 in-type `(and ,in-type ,out-type))))
714 (:close
715 (funcall in-method in :close arg1)
716 (funcall out-method out :close arg1)
717 (set-closed-flame stream))
718 (t
719 (or (funcall in-method in operation arg1 arg2)
720 (funcall out-method out operation arg1 arg2))))))
721
722 ;;;; Concatenated Streams:
723
724 (defstruct (concatenated-stream
725 (:include stream
726 (in #'concatenated-in)
727 (bin #'concatenated-bin)
728 (misc #'concatenated-misc))
729 (:print-function %print-concatenated-stream)
730 (:constructor
731 make-concatenated-stream (&rest streams &aux (current streams))))
732 ;; The car of this is the stream we are reading from now.
733 current
734 ;; This is a list of all the streams. We need to remember them so that
735 ;; we can close them.
736 (streams nil :type list :read-only t))
737
738 (defun %print-concatenated-stream (s stream d)
739 (declare (ignore d))
740 (format stream "#<Concatenated Stream, Streams = ~S>"
741 (concatenated-stream-streams s)))
742
743 (setf (documentation 'make-concatenated-stream 'function)
744 "Returns a stream which takes its input from each of the Streams in turn,
745 going on to the next at EOF.")
746
747 (macrolet ((in-fun (name fun)
748 `(defun ,name (stream eof-errorp eof-value)
749 (do ((current (concatenated-stream-current stream) (cdr current)))
750 ((null current)
751 (eof-or-lose stream eof-errorp eof-value))
752 (let* ((stream (car current))
753 (result (,fun stream nil nil)))
754 (when result (return result)))
755 (setf (concatenated-stream-current stream) current)))))
756 (in-fun concatenated-in read-char)
757 (in-fun concatenated-bin read-byte))
758
759 (defun concatenated-misc (stream operation &optional arg1 arg2)
760 (let ((left (concatenated-stream-current stream)))
761 (when left
762 (let* ((current (car left))
763 (misc (stream-misc current)))
764 (case operation
765 (:listen
766 (loop
767 (let ((stuff (funcall misc current :listen)))
768 (cond ((eq stuff :eof)
769 ;; Advance current, and try again.
770 (pop (concatenated-stream-current stream))
771 (setf current
772 (car (concatenated-stream-current stream)))
773 (unless current
774 ;; No further streams. EOF.
775 (return :eof))
776 (setf misc (stream-misc current)))
777 (stuff
778 ;; Stuff's available.
779 (return t))
780 (t
781 ;; Nothing available yet.
782 (return nil))))))
783 (:close
784 (dolist (stream (concatenated-stream-streams stream))
785 (funcall (stream-misc stream) stream :close arg1))
786 (set-closed-flame stream))
787 (t
788 (funcall misc current operation arg1 arg2)))))))
789
790 ;;;; Echo Streams:
791
792 (defstruct (echo-stream
793 (:include two-way-stream
794 (in #'echo-in)
795 (bin #'echo-bin)
796 (misc #'echo-misc)
797 (n-bin #'ill-bin))
798 (:print-function %print-echo-stream)
799 (:constructor make-echo-stream (input-stream output-stream)))
800 unread-stuff)
801
802
803 (macrolet ((in-fun (name fun out-slot &rest args)
804 `(defun ,name (stream ,@args)
805 (or (pop (echo-stream-unread-stuff stream))
806 (let* ((in (echo-stream-input-stream stream))
807 (out (echo-stream-output-stream stream))
808 (result (,fun in ,@args)))
809 (funcall (,out-slot out) out result)
810 result)))))
811 (in-fun echo-in read-char stream-out eof-errorp eof-value)
812 (in-fun echo-bin read-byte stream-bout eof-errorp eof-value))
813
814 (defun echo-misc (stream operation &optional arg1 arg2)
815 (let* ((in (two-way-stream-input-stream stream))
816 (in-method (stream-misc in))
817 (out (two-way-stream-output-stream stream))
818 (out-method (stream-misc out)))
819 (case operation
820 (:listen (or (not (null (echo-stream-unread-stuff stream)))
821 (/= (the fixnum (stream-in-index in)) in-buffer-length)
822 (funcall in-method in :listen)))
823 (:unread (push arg1 (echo-stream-unread-stuff stream)))
824 (:element-type
825 (let ((in-type (funcall in-method in :element-type))
826 (out-type (funcall out-method out :element-type)))
827 (if (equal in-type out-type)
828 in-type `(and ,in-type ,out-type))))
829 (:close
830 (funcall in-method in :close arg1)
831 (funcall out-method out :close arg1)
832 (set-closed-flame stream))
833 (t
834 (or (funcall in-method in operation arg1 arg2)
835 (funcall out-method out operation arg1 arg2))))))
836
837 (defun %print-echo-stream (s stream d)
838 (declare (ignore d))
839 (format stream "#<Echo Stream, Input = ~S, Output = ~S>"
840 (two-way-stream-input-stream s)
841 (two-way-stream-output-stream s)))
842
843 (setf (documentation 'make-echo-stream 'function)
844 "Returns a bidirectional stream which gets its input from Input-Stream and
845 sends its output to Output-Stream. In addition, all input is echoed to
846 the output stream")
847
848 ;;;; String Input Streams:
849
850 (defstruct (string-input-stream
851 (:include stream
852 (in #'string-inch)
853 (misc #'string-in-misc))
854 (:print-function %print-string-input-stream)
855 ;(:constructor nil)
856 (:constructor internal-make-string-input-stream
857 (string current end)))
858 (string nil :type simple-string)
859 (current nil :type fixnum)
860 (end nil :type fixnum))
861
862 (defun %print-string-input-stream (s stream d)
863 (declare (ignore s d))
864 (write-string "#<String-Input Stream>" stream))
865
866 (defun string-inch (stream eof-errorp eof-value)
867 (let ((string (string-input-stream-string stream))
868 (index (string-input-stream-current stream)))
869 (declare (simple-string string) (fixnum index))
870 (cond ((= index (the fixnum (string-input-stream-end stream)))
871 (eof-or-lose stream eof-errorp eof-value))
872 (t
873 (setf (string-input-stream-current stream) (1+ index))
874 (aref string index)))))
875
876 (defun string-in-misc (stream operation &optional arg1 arg2)
877 (declare (ignore arg2))
878 (case operation
879 (:file-position
880 (if arg1
881 (setf (string-input-stream-current stream) arg1)
882 (string-input-stream-current stream)))
883 (:file-length (length (string-input-stream-string stream)))
884 (:unread (decf (string-input-stream-current stream)))
885 (:listen (or (/= (the fixnum (string-input-stream-current stream))
886 (the fixnum (string-input-stream-end stream)))
887 :eof))
888 (:element-type 'string-char)))
889
890 (defun make-string-input-stream (string &optional
891 (start 0) (end (length string)))
892 "Returns an input stream which will supply the characters of String between
893 Start and End in order."
894 (declare (type string string)
895 (type index start)
896 (type (or index null) end))
897 (internal-make-string-input-stream (coerce string 'simple-string)
898 start end))
899
900 ;;;; String Output Streams:
901
902 (defstruct (string-output-stream
903 (:include stream
904 (out #'string-ouch)
905 (sout #'string-sout)
906 (misc #'string-out-misc))
907 (:print-function %print-string-output-stream)
908 (:constructor make-string-output-stream ()))
909 ;; The string we throw stuff in.
910 (string (make-string 40) :type simple-string)
911 ;; Index of the next location to use.
912 (index 0 :type fixnum))
913
914 (defun %print-string-output-stream (s stream d)
915 (declare (ignore s d))
916 (write-string "#<String-Output Stream>" stream))
917
918 (setf (documentation 'make-string-output-stream 'function)
919 "Returns an Output stream which will accumulate all output given it for
920 the benefit of the function Get-Output-Stream-String.")
921
922 (defun string-ouch (stream character)
923 (let ((current (string-output-stream-index stream))
924 (workspace (string-output-stream-string stream)))
925 (declare (simple-string workspace) (fixnum current))
926 (if (= current (the fixnum (length workspace)))
927 (let ((new-workspace (make-string (* current 2))))
928 (replace new-workspace workspace)
929 (setf (aref new-workspace current) character)
930 (setf (string-output-stream-string stream) new-workspace))
931 (setf (aref workspace current) character))
932 (setf (string-output-stream-index stream) (1+ current))))
933
934 (defun string-sout (stream string start end)
935 (declare (simple-string string) (fixnum start end))
936 (let* ((current (string-output-stream-index stream))
937 (length (- end start))
938 (dst-end (+ length current))
939 (workspace (string-output-stream-string stream)))
940 (declare (simple-string workspace)
941 (fixnum current length dst-end))
942 (if (> dst-end (the fixnum (length workspace)))
943 (let ((new-workspace (make-string (+ (* current 2) length))))
944 (replace new-workspace workspace :end2 current)
945 (replace new-workspace string
946 :start1 current :end1 dst-end
947 :start2 start :end2 end)
948 (setf (string-output-stream-string stream) new-workspace))
949 (replace workspace string
950 :start1 current :end1 dst-end
951 :start2 start :end2 end))
952 (setf (string-output-stream-index stream) dst-end)))
953
954 (defun string-out-misc (stream operation &optional arg1 arg2)
955 (declare (ignore arg2))
956 (case operation
957 (:file-position
958 (if (null arg1)
959 (string-output-stream-index stream)))
960 (:charpos
961 (do ((index (1- (the fixnum (string-output-stream-index stream)))
962 (1- index))
963 (count 0 (1+ count))
964 (string (string-output-stream-string stream)))
965 ((< index 0) count)
966 (declare (simple-string string)
967 (fixnum index count))
968 (if (char= (schar string index) #\newline)
969 (return count))))
970 (:element-type 'string-char)))
971
972 (defun get-output-stream-string (stream)
973 "Returns a string of all the characters sent to a stream made by
974 Make-String-Output-Stream since the last call to this function."
975 (declare (type string-output-stream stream))
976 (let* ((length (string-output-stream-index stream))
977 (result (make-string length)))
978 (replace result (string-output-stream-string stream))
979 (setf (string-output-stream-index stream) 0)
980 result))
981
982 (defun dump-output-stream-string (in-stream out-stream)
983 "Dumps the characters buffer up in the In-Stream to the Out-Stream as
984 Get-Output-Stream-String would return them."
985 (write-string (string-output-stream-string in-stream) out-stream
986 :start 0 :end (string-output-stream-index in-stream))
987 (setf (string-output-stream-index in-stream) 0))
988
989 ;;;; Fill-pointer streams:
990 ;;;
991 ;;; Fill pointer string output streams are not explicitly mentioned in
992 ;;; the CLM, but they are required for the implementation of With-Output-To-String.
993
994 (defstruct (fill-pointer-output-stream
995 (:include stream
996 (out #'fill-pointer-ouch)
997 (sout #'fill-pointer-sout)
998 (misc #'fill-pointer-misc))
999 (:print-function
1000 (lambda (s stream d)
1001 (declare (ignore s d))
1002 (write-string "#<Fill-Pointer String Output Stream>" stream)))
1003 (:constructor make-fill-pointer-output-stream (string)))
1004 ;; The string we throw stuff in.
1005 string)
1006
1007
1008 (defun fill-pointer-ouch (stream character)
1009 (let* ((buffer (fill-pointer-output-stream-string stream))
1010 (current (fill-pointer buffer))
1011 (current+1 (1+ current)))
1012 (declare (fixnum current))
1013 (with-array-data ((workspace buffer) (start) (end))
1014 (declare (simple-string workspace))
1015 (let ((offset-current (+ start current)))
1016 (declare (fixnum offset-current))
1017 (if (= offset-current end)
1018 (let* ((new-length (* current 2))
1019 (new-workspace (make-string new-length)))
1020 (declare (simple-string new-workspace))
1021 (%primitive byte-blt workspace start new-workspace 0 current)
1022 (setf workspace new-workspace)
1023 (setf offset-current current)
1024 (set-array-header buffer workspace new-length
1025 current+1 0 new-length nil))
1026 (setf (fill-pointer buffer) current+1))
1027 (setf (schar workspace offset-current) character)))
1028 current+1))
1029
1030
1031 (defun fill-pointer-sout (stream string start end)
1032 (declare (simple-string string) (fixnum start end))
1033 (let* ((buffer (fill-pointer-output-stream-string stream))
1034 (current (fill-pointer buffer))
1035 (string-len (- end start))
1036 (dst-end (+ string-len current)))
1037 (declare (fixnum current dst-end string-len))
1038 (with-array-data ((workspace buffer) (dst-start) (dst-length))
1039 (declare (simple-string workspace))
1040 (let ((offset-dst-end (+ dst-start dst-end))
1041 (offset-current (+ dst-start current)))
1042 (declare (fixnum offset-dst-end offset-current))
1043 (if (> offset-dst-end dst-length)
1044 (let* ((new-length (+ (the fixnum (* current 2)) string-len))
1045 (new-workspace (make-string new-length)))
1046 (declare (simple-string new-workspace))
1047 (%primitive byte-blt workspace dst-start new-workspace 0 current)
1048 (setf workspace new-workspace)
1049 (setf offset-current current)
1050 (setf offset-dst-end dst-end)
1051 (set-array-header buffer workspace new-length
1052 dst-end 0 new-length nil))
1053 (setf (fill-pointer buffer) dst-end))
1054 (%primitive byte-blt string start
1055 workspace offset-current offset-dst-end)))
1056 dst-end))
1057
1058
1059 (defun fill-pointer-misc (stream operation &optional arg1 arg2)
1060 (declare (ignore arg1 arg2))
1061 (case operation
1062 (:charpos
1063 (let* ((buffer (fill-pointer-output-stream-string stream))
1064 (current (fill-pointer buffer)))
1065 (with-array-data ((string buffer) (start) (end current))
1066 (declare (simple-string string) (ignore start))
1067 (let ((found (position #\newline string :test #'char=
1068 :end end :from-end t)))
1069 (if found
1070 (- end (the fixnum found))
1071 current)))))
1072 (:element-type 'string-char)))
1073
1074 ;;;; Indenting streams:
1075
1076 (defstruct (indenting-stream (:include stream
1077 (out #'indenting-out)
1078 (sout #'indenting-sout)
1079 (misc #'indenting-misc))
1080 (:print-function %print-indenting-stream)
1081 (:constructor make-indenting-stream (stream)))
1082 ;; The stream we're based on:
1083 stream
1084 ;; How much we indent on each line:
1085 (indentation 0))
1086
1087 (setf (documentation 'make-indenting-stream 'function)
1088 "Returns an ouput stream which indents its output by some amount.")
1089
1090 (defun %print-indenting-stream (s stream d)
1091 (declare (ignore s d))
1092 (write-string "#<Indenting Stream>" stream))
1093
1094 ;;; Indenting-Indent writes the right number of spaces needed to indent output on
1095 ;;; the given Stream based on the specified Sub-Stream.
1096
1097 (defmacro indenting-indent (stream sub-stream)
1098 `(do ((i 0 (+ i 60))
1099 (indentation (indenting-stream-indentation ,stream)))
1100 ((>= i indentation))
1101 (funcall (stream-sout ,sub-stream) ,sub-stream
1102 " "
1103 0 (min 60 (- indentation i)))))
1104
1105 ;;; Indenting-Out writes a character to an indenting stream.
1106
1107 (defun indenting-out (stream char)
1108 (let ((sub-stream (indenting-stream-stream stream)))
1109 (funcall (stream-out sub-stream) sub-stream char)
1110 (if (char= char #\newline)
1111 (indenting-indent stream sub-stream))))
1112
1113 ;;; Indenting-Sout writes a string to an indenting stream.
1114
1115 (defun indenting-sout (stream string start end)
1116 (declare (simple-string string) (fixnum start end))
1117 (do ((i start)
1118 (sub-stream (indenting-stream-stream stream)))
1119 ((= i end))
1120 (let ((newline (position #\newline string :start i :end end)))
1121 (cond (newline
1122 (funcall (stream-sout sub-stream) sub-stream string i (1+ newline))
1123 (indenting-indent stream sub-stream)
1124 (setq i (+ newline 1)))
1125 (t
1126 (funcall (stream-sout sub-stream) sub-stream string i end)
1127 (setq i end))))))
1128
1129 ;;; Indenting-Misc just treats just the :Line-Length message differently.
1130 ;;; Indenting-Charpos says the charpos is the charpos of the base stream minus
1131 ;;; the stream's indentation.
1132
1133 (defun indenting-misc (stream operation &optional arg1 arg2)
1134 (let* ((sub-stream (indenting-stream-stream stream))
1135 (method (stream-misc sub-stream)))
1136 (case operation
1137 (:line-length
1138 (let ((line-length (funcall method sub-stream operation)))
1139 (if line-length
1140 (- line-length (indenting-stream-indentation stream)))))
1141 (:charpos
1142 (let* ((sub-stream (indenting-stream-stream stream))
1143 (charpos (funcall method sub-stream operation)))
1144 (if charpos
1145 (- charpos (indenting-stream-indentation stream)))))
1146 (t
1147 (funcall method sub-stream operation arg1 arg2)))))
1148
1149 (proclaim '(maybe-inline read-char unread-char read-byte listen))
1150
1151
1152
1153 ;;;; Case frobbing streams, used by format ~(...~).
1154
1155 (defstruct (case-frob-stream
1156 (:include stream
1157 (:misc #'case-frob-misc))
1158 (:constructor %make-case-frob-stream (target out sout)))
1159 (target (required-argument) :type stream))
1160
1161 (defun make-case-frob-stream (target kind)
1162 "Returns a stream that sends all output to the stream TARGET, but modifies
1163 the case of letters, depending on KIND, which should be one of:
1164 :upcase - convert to upper case.
1165 :downcase - convert to lower case.
1166 :capitalize - convert the first letter of words to upper case and the
1167 rest of the word to lower case.
1168 :capitalize-first - convert the first letter of the first word to upper
1169 case and everything else to lower case."
1170 (declare (type stream target)
1171 (type (member :upcase :downcase :capitalize :capitalize-first)
1172 kind)
1173 (values stream))
1174 (if (case-frob-stream-p target)
1175 ;; If we are going to be writing to a stream that already does case
1176 ;; frobbing, why bother frobbing the case just so it can frob it
1177 ;; again?
1178 target
1179 (multiple-value-bind
1180 (out sout)
1181 (ecase kind
1182 (:upcase
1183 (values #'case-frob-upcase-out
1184 #'case-frob-upcase-sout))
1185 (:downcase
1186 (values #'case-frob-downcase-out
1187 #'case-frob-downcase-sout))
1188 (:capitalize
1189 (values #'case-frob-capitalize-out
1190 #'case-frob-capitalize-sout))
1191 (:capitalize-first
1192 (values #'case-frob-capitalize-first-out
1193 #'case-frob-capitalize-first-sout)))
1194 (%make-case-frob-stream target out sout))))
1195
1196 (defun case-frob-misc (stream op &optional arg1 arg2)
1197 (declare (type case-frob-stream stream))
1198 (case op
1199 (:close)
1200 (t
1201 (let ((target (case-frob-stream-target stream)))
1202 (funcall (stream-misc target) target op arg1 arg2)))))
1203
1204
1205 (defun case-frob-upcase-out (stream char)
1206 (declare (type case-frob-stream stream)
1207 (type base-char char))
1208 (let ((target (case-frob-stream-target stream)))
1209 (funcall (stream-out target) target (char-upcase char))))
1210
1211 (defun case-frob-upcase-sout (stream str start end)
1212 (declare (type case-frob-stream stream)
1213 (type simple-base-string str)
1214 (type index start)
1215 (type (or index null) end))
1216 (let* ((target (case-frob-stream-target stream))
1217 (len (length str))
1218 (end (or end len)))
1219 (funcall (stream-sout target) target
1220 (if (and (zerop start) (= len end))
1221 (string-upcase str)
1222 (nstring-upcase (subseq str start end)))
1223 0
1224 (- end start))))
1225
1226 (defun case-frob-downcase-out (stream char)
1227 (declare (type case-frob-stream stream)
1228 (type base-char char))
1229 (let ((target (case-frob-stream-target stream)))
1230 (funcall (stream-out target) target (char-downcase char))))
1231
1232 (defun case-frob-downcase-sout (stream str start end)
1233 (declare (type case-frob-stream stream)
1234 (type simple-base-string str)
1235 (type index start)
1236 (type (or index null) end))
1237 (let* ((target (case-frob-stream-target stream))
1238 (len (length str))
1239 (end (or end len)))
1240 (funcall (stream-sout target) target
1241 (if (and (zerop start) (= len end))
1242 (string-downcase str)
1243 (nstring-downcase (subseq str start end)))
1244 0
1245 (- end start))))
1246
1247 (defun case-frob-capitalize-out (stream char)
1248 (declare (type case-frob-stream stream)
1249 (type base-char char))
1250 (let ((target (case-frob-stream-target stream)))
1251 (cond ((alphanumericp char)
1252 (funcall (stream-out target) target (char-upcase char))
1253 (setf (case-frob-stream-out stream)
1254 #'case-frob-capitalize-aux-out)
1255 (setf (case-frob-stream-sout stream)
1256 #'case-frob-capitalize-aux-sout))
1257 (t
1258 (funcall (stream-out target) target char)))))
1259
1260 (defun case-frob-capitalize-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 (str (subseq str start end))
1267 (len (length str))
1268 (inside-word nil))
1269 (dotimes (i len)
1270 (let ((char (schar str i)))
1271 (cond ((not (alphanumericp char))
1272 (setf inside-word nil))
1273 (inside-word
1274 (setf (schar str i) (char-downcase char)))
1275 (t
1276 (setf inside-word t)
1277 (setf (schar str i) (char-upcase char))))))
1278 (when inside-word
1279 (setf (case-frob-stream-out stream)
1280 #'case-frob-capitalize-aux-out)
1281 (setf (case-frob-stream-sout stream)
1282 #'case-frob-capitalize-aux-sout))
1283 (funcall (stream-sout target) target str 0 len)))
1284
1285 (defun case-frob-capitalize-aux-out (stream char)
1286 (declare (type case-frob-stream stream)
1287 (type base-char char))
1288 (let ((target (case-frob-stream-target stream)))
1289 (cond ((alphanumericp char)
1290 (funcall (stream-out target) target (char-downcase char)))
1291 (t
1292 (funcall (stream-out target) target char)
1293 (setf (case-frob-stream-out stream)
1294 #'case-frob-capitalize-out)
1295 (setf (case-frob-stream-sout stream)
1296 #'case-frob-capitalize-sout)))))
1297
1298 (defun case-frob-capitalize-aux-sout (stream str start end)
1299 (declare (type case-frob-stream stream)
1300 (type simple-base-string str)
1301 (type index start)
1302 (type (or index null) end))
1303 (let* ((target (case-frob-stream-target stream))
1304 (str (subseq str start end))
1305 (len (length str))
1306 (inside-word t))
1307 (dotimes (i len)
1308 (let ((char (schar str i)))
1309 (cond ((not (alphanumericp char))
1310 (setf inside-word nil))
1311 (inside-word
1312 (setf (schar str i) (char-downcase char)))
1313 (t
1314 (setf inside-word t)
1315 (setf (schar str i) (char-upcase char))))))
1316 (unless inside-word
1317 (setf (case-frob-stream-out stream)
1318 #'case-frob-capitalize-out)
1319 (setf (case-frob-stream-sout stream)
1320 #'case-frob-capitalize-sout))
1321 (funcall (stream-sout target) target str 0 len)))
1322
1323 (defun case-frob-capitalize-first-out (stream char)
1324 (declare (type case-frob-stream stream)
1325 (type base-char char))
1326 (let ((target (case-frob-stream-target stream)))
1327 (cond ((alphanumericp char)
1328 (funcall (stream-out target) target (char-upcase char))
1329 (setf (case-frob-stream-out stream)
1330 #'case-frob-downcase-out)
1331 (setf (case-frob-stream-sout stream)
1332 #'case-frob-downcase-sout))
1333 (t
1334 (funcall (stream-out target) target char)))))
1335
1336 (defun case-frob-capitalize-first-sout (stream str start end)
1337 (declare (type case-frob-stream stream)
1338 (type simple-base-string str)
1339 (type index start)
1340 (type (or index null) end))
1341 (let* ((target (case-frob-stream-target stream))
1342 (str (subseq str start end))
1343 (len (length str)))
1344 (dotimes (i len)
1345 (let ((char (schar str i)))
1346 (when (alphanumericp char)
1347 (setf (schar str i) (char-upcase char))
1348 (do ((i (1+ i) (1+ i)))
1349 ((= i len))
1350 (setf (schar str i) (char-downcase (schar str i))))
1351 (setf (case-frob-stream-out stream)
1352 #'case-frob-downcase-out)
1353 (setf (case-frob-stream-sout stream)
1354 #'case-frob-downcase-sout)
1355 (return))))
1356 (funcall (stream-sout target) target str 0 len)))
1357
1358
1359 ;;;; Public interface from "EXTENSIONS" package.
1360
1361 (defstruct (stream-command (:print-function print-stream-command)
1362 (:constructor make-stream-command
1363 (name &optional args)))
1364 (name nil :type symbol)
1365 (args nil :type list))
1366
1367 (defun print-stream-command (obj str n)
1368 (declare (ignore n))
1369 (format str "#<Stream-Cmd ~S>" (stream-command-name obj)))
1370
1371
1372 ;;; GET-STREAM-COMMAND -- Public.
1373 ;;;
1374 ;;; We can't simply call the stream's misc method because because nil is an
1375 ;;; ambiguous return value: does it mean text arrived, or does it mean the
1376 ;;; stream's misc method had no :get-command implementation. We can't return
1377 ;;; nil until there is text input. We don't need to loop because any stream
1378 ;;; implementing :get-command would wait until it had some input. If the
1379 ;;; LISTEN fails, then we have some random stream we must wait on.
1380 ;;;
1381 (defun get-stream-command (stream)
1382 "This takes a stream and waits for text or a command to appear on it. If
1383 text appears before a command, this returns nil, and otherwise it returns
1384 a command."
1385 (let ((cmdp (funcall (lisp::stream-misc stream) stream :get-command)))
1386 (cond (cmdp)
1387 ((listen stream)
1388 nil)
1389 (t
1390 ;; This waits for input and returns nil when it arrives.
1391 (unread-char (read-char stream) stream)))))

  ViewVC Help
Powered by ViewVC 1.1.5