Newer
Older
;;; -*- Package: Lisp -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of CMU Common Lisp, please contact
;;; Scott Fahlman or slisp-group@cs.cmu.edu.
;;;
(ext:file-comment
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/stream.lisp,v 1.17 1993/02/12 20:21:44 wlott Exp $")
;;; **********************************************************************
;;;
;;; Stream functions for Spice Lisp.
;;; Written by Skef Wholey and Rob MacLachlan.
;;;
;;; This file contains the OS-independent stream functions.
(in-package "LISP")
(export '(broadcast-stream make-broadcast-stream broadcast-stream-streams
synonym-stream make-synonym-stream synonym-stream-symbol
concatenated-stream make-concatenated-stream
concatenated-stream-streams
two-way-stream make-two-way-stream two-way-stream-input-stream
two-way-stream-output-stream
echo-stream make-echo-stream echo-stream-input-stream
echo-stream-output-stream
make-string-input-stream make-string-output-stream
output-stream-p open-stream-p interactive-stream-p
open-stream-p close read-line read-char
unread-char peek-char listen read-char-no-hang clear-input read-byte
write-char write-string write-line terpri fresh-line
finish-output force-output clear-output write-byte
stream streamp string-stream
*standard-input* *standard-output*
*error-output* *query-io* *debug-io* *terminal-io* *trace-output*))
(in-package 'system)
(export '(make-indenting-stream read-n-bytes))
(in-package 'lisp)
(deftype string-stream ()
'(or string-input-stream string-output-stream
fill-pointer-output-stream))
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;;;; Standard streams:
;;;
;;; The initialization of these streams is performed by Stream-Init,
;;; which lives in the file of machine-specific stream functions.
;;;
(defvar *terminal-io* () "Terminal I/O stream.")
(defvar *standard-input* () "Default input stream.")
(defvar *standard-output* () "Default output stream.")
(defvar *error-output* () "Error output stream.")
(defvar *query-io* () "Query I/O stream.")
(defvar *trace-output* () "Trace output stream.")
(defvar *debug-io* () "Interactive debugging stream.")
(defun ill-in (stream &rest ignore)
(declare (ignore ignore))
(error "~S is not a character input stream." stream))
(defun ill-out (stream &rest ignore)
(declare (ignore ignore))
(error "~S is not a character output stream." stream))
(defun ill-bin (stream &rest ignore)
(declare (ignore ignore))
(error "~S is not a binary input stream." stream))
(defun ill-bout (stream &rest ignore)
(declare (ignore ignore))
(error "~S is not a binary output stream." stream))
(defun closed-flame (stream &rest ignore)
(declare (ignore ignore))
(error "~S is closed." stream))
(defun do-nothing (&rest ignore)
(declare (ignore ignore)))
(defun %print-stream (structure stream d)
(declare (ignore d structure))
(write-string "#<Bare Stream>" stream))
;;; HOW THE STREAM STRUCTURE IS USED:
;;;
;;; Many of the slots of the stream structure contain functions
;;; which are called to perform some operation on the stream. Closed
;;; streams have #'Closed-Flame in all of their function slots. If
;;; one side of an I/O or echo stream is closed, the whole stream is
;;; considered closed. The functions in the operation slots take
;;; arguments as follows:
;;;
;;; In: Stream, Eof-Errorp, Eof-Value
;;; Bin: Stream, Eof-Errorp, Eof-Value
;;; N-Bin: Stream, Buffer, Start, Numbytes, Eof-Errorp
;;; Out: Stream, Character
;;; Bout: Stream, Integer
;;; Sout: Stream, String, Start, End
;;; Misc: Stream, Operation, &Optional Arg1, Arg2
;;;
;;; In order to save space, some of the less common stream operations
;;; are handled by just one function, the Misc method. This function
;;; is passed a keyword which indicates the operation to perform.
;;; The following keywords are used:
;;; :read-line - Do a read-line.
;;; :listen - Return the following values:
;;; t if any input waiting.
;;; :eof if at eof.
;;; nil if no input is available and not at eof.
;;; :unread - Unread the character Arg.
;;; :close - Do any stream specific stuff to close the stream.
;;; The methods are set to closed-flame by the close
;;; function, so that need not be done by this
;;; function.
;;; :clear-input - Clear any unread input
;;; :finish-output,
;;; :force-output - Cause output to happen
;;; :clear-output - Clear any undone output
;;; :element-type - Return the type of element the stream deals with.
;;; :line-length - Return the length of a line of output.
;;; :charpos - Return current output position on the line.
;;; :file-length - Return the file length of a file stream.
;;; :file-position - Return or change the current position of a file stream.
;;; :file-name - Return the name of an associated file.
;;; :interactive-p - Is this an interactive device?
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;;;
;;; In order to do almost anything useful, it is necessary to
;;; define a new type of structure that includes stream, so that the
;;; stream can have some state information.
;;;
;;; THE STREAM IN-BUFFER:
;;;
;;; The In-Buffer in the stream holds characters or bytes that
;;; are ready to be read by some input function. If there is any
;;; stuff in the In-Buffer, then the reading function can use it
;;; without calling any stream method. Any stream may put stuff in
;;; the In-Buffer, and may also assume that any input in the In-Buffer
;;; has been consumed before any in-method is called. If a text
;;; stream has in In-Buffer, then the first character should not be
;;; used to buffer normal input so that it is free for unreading into.
;;;
;;; The In-Buffer slot is a vector In-Buffer-Length long. The
;;; In-Index is the index in the In-Buffer of the first available
;;; object. The available objects are thus between In-Index and the
;;; length of the In-Buffer.
;;;
;;; When this buffer is only accessed by the normal stream
;;; functions, the number of function calls is halved, thus
;;; potentially doubling the speed of simple operations. If the
;;; Fast-Read-Char and Fast-Read-Byte macros are used, nearly all
;;; function call overhead is removed, vastly speeding up these
;;; important operations.
;;;
;;; If a stream does not have an In-Buffer, then the In-Buffer slot
;;; must be nil, and the In-Index must be In-Buffer-Length. These are
;;; the default values for the slots.
;;; Stream manipulation functions.
(defun input-stream-p (stream)
"Returns non-nil if the given Stream can perform input operations."
(and (streamp stream)
(not (eq (stream-in stream) #'closed-flame))
(or (not (eq (stream-in stream) #'ill-in))
(not (eq (stream-bin stream) #'ill-bin)))))
(defun output-stream-p (stream)
"Returns non-nil if the given Stream can perform output operations."
(and (streamp stream)
(not (eq (stream-in stream) #'closed-flame))
(or (not (eq (stream-out stream) #'ill-out))
(not (eq (stream-bout stream) #'ill-bout)))))
(defun open-stream-p (stream)
"Return true if Stream is not closed."
(declare (type stream stream))
(not (eq (stream-in stream) #'closed-flame)))
(defun stream-element-type (stream)
"Returns a type specifier for the kind of object returned by the Stream."
(declare (type stream stream))
(funcall (stream-misc stream) stream :element-type))
(defun interactive-stream-p (stream)
"Return true if Stream does I/O on a terminal or other interactive device."
(declare (type stream stream))
(funcall (stream-misc stream) stream :interactive-p))
(defun open-stream-p (stream)
"Return true if and only if STREAM has not been closed."
(declare (type stream stream))
(not (eq (stream-in stream) #'closed-flame)))
(defun close (stream &key abort)
"Closes the given Stream. No more I/O may be performed, but inquiries
may still be made. If :Abort is non-nil, an attempt is made to clean
up the side effects of having created the stream."
(declare (type stream stream))
(when (open-stream-p stream)
(funcall (stream-misc stream) stream :close abort))
(defun set-closed-flame (stream)
(setf (stream-in stream) #'closed-flame)
(setf (stream-bin stream) #'closed-flame)
(setf (stream-n-bin stream) #'closed-flame)
(setf (stream-in stream) #'closed-flame)
(setf (stream-out stream) #'closed-flame)
(setf (stream-bout stream) #'closed-flame)
(setf (stream-sout stream) #'closed-flame)
(setf (stream-misc stream) #'closed-flame))
;;; Input functions:
(defun read-line (&optional (stream *standard-input*) (eof-errorp t) eof-value
recursive-p)
"Returns a line of text read from the Stream as a string, discarding the
newline character."
(declare (ignore recursive-p))
(let* ((stream (in-synonym-of stream))
(buffer (stream-in-buffer stream))
(index (stream-in-index stream)))
(declare (fixnum index))
(if (simple-string-p buffer)
(let ((nl (%sp-find-character buffer index in-buffer-length
#\newline)))
(if nl
(values (prog1 (subseq (the simple-string buffer) index nl)
(setf (stream-in-index stream)
(1+ (the fixnum nl))))
nil)
(multiple-value-bind (str eofp)
(funcall (stream-misc stream) stream
:read-line eof-errorp eof-value)
(if (= index in-buffer-length)
(values str eofp)
(let ((first (subseq buffer index in-buffer-length)))
(setf (stream-in-index stream) in-buffer-length)
(if (eq str eof-value)
(values first t)
(values (concatenate 'simple-string first
(the simple-string str))
eofp)))))))
(funcall (stream-misc stream) stream :read-line eof-errorp
eof-value))))
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;;; We proclaim them inline here, then proclaim them notinline at EOF,
;;; so, except in this file, they are not inline by default, but they can be.
;;;
(proclaim '(inline read-char unread-char read-byte listen))
(defun read-char (&optional (stream *standard-input*) (eof-errorp t) eof-value
recursive-p)
"Inputs a character from Stream and returns it."
(declare (ignore recursive-p))
(let* ((stream (in-synonym-of stream))
(index (stream-in-index stream)))
(declare (fixnum index))
(if (eql index in-buffer-length)
(funcall (stream-in stream) stream eof-errorp eof-value)
(prog1 (aref (stream-in-buffer stream) index)
(setf (stream-in-index stream) (1+ index))))))
(defun unread-char (character &optional (stream *standard-input*))
"Puts the Character back on the front of the input Stream."
(let* ((stream (in-synonym-of stream))
(index (1- (the fixnum (stream-in-index stream))))
(buffer (stream-in-buffer stream)))
(declare (fixnum index))
(when (minusp index) (error "Nothing to unread."))
(if buffer
(setf (aref (the simple-array buffer) index) character
(stream-in-index stream) index)
(funcall (stream-misc stream) stream :unread character)))
nil)
(defun peek-char (&optional (peek-type nil) (stream *standard-input*)
(eof-errorp t) eof-value recursive-p)
"Peeks at the next character in the input Stream. See manual for details."
(declare (ignore recursive-p))
(let* ((stream (in-synonym-of stream))
(char (read-char stream eof-errorp eof-value)))
(cond ((eq char eof-value) char)
((characterp peek-type)
(do ((char char (read-char stream eof-errorp eof-value)))
((or (eq char eof-value) (char= char peek-type))
(unless (eq char eof-value)
(unread-char char stream))
char)))
((eq peek-type t)
(do ((char char (read-char stream eof-errorp eof-value)))
((or (eq char eof-value) (not (whitespace-char-p char)))
(unless (eq char eof-value)
(unread-char char stream))
char)))
(t
(unread-char char stream)
char))))
(defun listen (&optional (stream *standard-input*))
"Returns T if a character is availible on the given Stream."
(let ((stream (in-synonym-of stream)))
(or (/= (the fixnum (stream-in-index stream)) in-buffer-length)
;; Test for t explicitly since misc methods return :eof sometimes.
(eq (funcall (stream-misc stream) stream :listen) t))))
(defun read-char-no-hang (&optional (stream *standard-input*)
(eof-errorp t) eof-value recursive-p)
"Returns the next character from the Stream if one is availible, or nil."
(declare (ignore recursive-p))
(let ((stream (in-synonym-of stream)))
(if (funcall (stream-misc stream) stream :listen)
;; On t or :eof get READ-CHAR to do the work.
(read-char stream eof-errorp eof-value)
nil)))
(defun clear-input (&optional (stream *standard-input*))
"Clears any buffered input associated with the Stream."
(let ((stream (in-synonym-of stream)))
(setf (stream-in-index stream) in-buffer-length)
(funcall (stream-misc stream) stream :clear-input)
nil))
(defun read-byte (stream &optional (eof-errorp t) eof-value)
"Returns the next byte of the Stream."
(let* ((stream (in-synonym-of stream))
(index (stream-in-index stream)))
(declare (fixnum index))
(if (eql index in-buffer-length)
(funcall (stream-bin stream) stream eof-errorp eof-value)
(prog1 (aref (stream-in-buffer stream) index)
(setf (stream-in-index stream) (1+ index))))))
(defun read-n-bytes (stream buffer start numbytes &optional (eof-errorp t))
"Reads Numbytes bytes into the Buffer starting at Start, and returns
the number of bytes actually read if the end of file was hit before Numbytes
bytes were read (and Eof-Errorp is false)."
(declare (fixnum numbytes))
(let* ((stream (in-synonym-of stream))
(in-buffer (stream-in-buffer stream))
(index (stream-in-index stream))
(num-buffered (- in-buffer-length index)))
(declare (fixnum index num-buffered))
(cond
((not in-buffer)
(with-in-stream stream stream-n-bin buffer start numbytes eof-errorp))
((not (typep in-buffer
'(or simple-string (simple-array (unsigned-byte 8) (*)))))
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
(error "N-Bin only works on 8-bit-like streams."))
((<= numbytes num-buffered)
(%primitive byte-blt in-buffer index buffer start (+ start numbytes))
(setf (stream-in-index stream) (+ index numbytes))
numbytes)
(t
(let ((end (+ start num-buffered)))
(%primitive byte-blt in-buffer index buffer start end)
(setf (stream-in-index stream) in-buffer-length)
(+ (with-in-stream stream stream-n-bin buffer end
(- numbytes num-buffered)
eof-errorp)
num-buffered))))))
;;; Output functions:
(defun write-char (character &optional (stream *standard-output*))
"Outputs the Character to the Stream."
(with-out-stream stream stream-out character)
character)
(defun terpri (&optional (stream *standard-output*))
"Outputs a new line to the Stream."
(with-out-stream stream stream-out #\newline)
nil)
(defun fresh-line (&optional (stream *standard-output*))
"Outputs a new line to the Stream if it is not positioned at the begining of
a line. Returns T if it output a new line, nil otherwise."
(let ((stream (out-synonym-of stream)))
(when (/= (or (charpos stream) 1) 0)
(funcall (stream-out stream) stream #\newline)
t)))
(defun write-string (string &optional (stream *standard-output*)
&key (start 0) (end (length (the vector string))))
"Outputs the String to the given Stream."
(write-string* string stream start end))
(defun write-string* (string &optional (stream *standard-output*)
(start 0) (end (length (the vector string))))
(declare (fixnum start end))
(if (array-header-p string)
(with-array-data ((data string) (offset-start start) (offset-end end))
(with-out-stream stream stream-sout data offset-start offset-end))
(with-out-stream stream stream-sout string start end))
string)
(defun write-line (string &optional (stream *standard-output*)
&key (start 0) (end (length string)))
"Outputs the String to the given Stream, followed by a newline character."
(write-line* string stream start end))
(defun write-line* (string &optional (stream *standard-output*)
(start 0) (end (length string)))
(declare (fixnum start end))
(let ((stream (out-synonym-of stream)))
(if (array-header-p string)
(with-array-data ((data string) (offset-start start) (offset-end end))
(with-out-stream stream stream-sout data offset-start offset-end))
(with-out-stream stream stream-sout string start end))
(funcall (stream-out stream) stream #\newline))
string)
(defun charpos (&optional (stream *standard-output*))
"Returns the number of characters on the current line of output of the given
Stream, or Nil if that information is not availible."
(with-out-stream stream stream-misc :charpos))
(defun line-length (&optional (stream *standard-output*))
"Returns the number of characters that will fit on a line of output on the
given Stream, or Nil if that information is not available."
(with-out-stream stream stream-misc :line-length))
(defun finish-output (&optional (stream *standard-output*))
"Attempts to ensure that all output sent to the the Stream has reached its
destination, and only then returns."
(with-out-stream stream stream-misc :finish-output)
nil)
(defun force-output (&optional (stream *standard-output*))
"Attempts to force any buffered output to be sent."
(with-out-stream stream stream-misc :force-output)
nil)
(defun clear-output (&optional (stream *standard-output*))
"Clears the given output Stream."
(with-out-stream stream stream-misc :clear-output)
nil)
(defun write-byte (integer stream)
"Outputs the Integer to the binary Stream."
(with-out-stream stream stream-bout integer)
integer)
;;;; Broadcast streams:
(defstruct (broadcast-stream (:include stream
(out #'broadcast-out)
(bout #'broadcast-bout)
(sout #'broadcast-sout)
(misc #'broadcast-misc))
(:print-function %print-broadcast-stream)
(:constructor make-broadcast-stream (&rest streams)))
;; This is a list of all the streams we broadcast to.
(streams () :type list :read-only t))
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
(setf (documentation 'make-broadcast-stream 'function)
"Returns an ouput stream which sends its output to all of the given streams.")
(defun %print-broadcast-stream (s stream d)
(declare (ignore s d))
(write-string "#<Broadcast Stream>" stream))
(macrolet ((out-fun (fun method &rest args)
`(defun ,fun (stream ,@args)
(dolist (stream (broadcast-stream-streams stream))
(funcall (,method stream) stream ,@args)))))
(out-fun broadcast-out stream-out char)
(out-fun broadcast-bout stream-bout byte)
(out-fun broadcast-sout stream-sout string start end))
(defun broadcast-misc (stream operation &optional arg1 arg2)
(let ((streams (broadcast-stream-streams stream)))
(case operation
(:charpos
(dolist (stream streams)
(let ((charpos (funcall (stream-misc stream) stream :charpos)))
(if charpos (return charpos)))))
(:line-length
(let ((min nil))
(dolist (stream streams min)
(let ((res (funcall (stream-misc stream) stream :line-length)))
(when res (setq min (if min (min res min) res)))))))
(:element-type
(let (res)
(dolist (stream streams (if (> (length res) 1) `(and ,@res) res))
(pushnew (funcall (stream-misc stream) stream :element-type) res
:test #'equal))))
(t
(let ((res nil))
(dolist (stream streams res)
(setq res (funcall (stream-misc stream) stream operation
arg1 arg2))))))))
;;;; Synonym Streams:
(defstruct (synonym-stream (:include stream
(in #'synonym-in)
(bin #'synonym-bin)
(n-bin #'synonym-n-bin)
(out #'synonym-out)
(bout #'synonym-bout)
(sout #'synonym-sout)
(misc #'synonym-misc))
(:print-function %print-synonym-stream)
(:constructor make-synonym-stream (symbol)))
;; This is the symbol, the value of which is the stream we are synonym to.
(symbol nil :type symbol :read-only t))
(defun %print-synonym-stream (s stream d)
(declare (ignore d))
(format stream "#<Synonym Stream to ~S>" (synonym-stream-symbol s)))
(setf (documentation 'make-synonym-stream 'function)
"Returns a stream which performs its operations on the stream which is the
value of the dynamic variable named by Symbol.")
;;; The output simple output methods just call the corresponding method
;;; in the synonymed stream.
;;;
(macrolet ((out-fun (name slot &rest args)
`(defun ,name (stream ,@args)
(declare (optimize (safety 1)))
(let ((syn (symbol-value (synonym-stream-symbol stream))))
(funcall (,slot syn) syn ,@args)))))
(out-fun synonym-out stream-out ch)
(out-fun synonym-bout stream-bout n)
(out-fun synonym-sout stream-sout string start end))
;;; Bind synonym stream to this so that SPIO can turn on the right frob in
;;; the icon when we are in a terminal input wait.
;;;
(defvar *previous-stream* nil)
;;; For the input methods, we just call the corresponding function on the
;;; synonymed stream. These functions deal with getting input out of
;;; the In-Buffer if there is any.
;;;
(macrolet ((in-fun (name fun &rest args)
`(defun ,name (stream ,@args)
(declare (optimize (safety 1)))
(let ((*previous-stream* stream))
(,fun (symbol-value (synonym-stream-symbol stream)) ,@args)))))
(in-fun synonym-in read-char eof-errorp eof-value)
(in-fun synonym-bin read-byte eof-errorp eof-value)
(in-fun synonym-n-bin read-n-bytes buffer start numbytes eof-errorp))
;;; Synonym-Misc -- Internal
;;;
;;; We have to special-case the operations which could look at stuff in
;;; the in-buffer.
;;;
(defun synonym-misc (stream operation &optional arg1 arg2)
(declare (optimize (safety 1)))
(let ((syn (symbol-value (synonym-stream-symbol stream)))
(*previous-stream* stream))
(case operation
(:read-line (read-line syn))
(:listen (or (/= (the fixnum (stream-in-index syn)) in-buffer-length)
(funcall (stream-misc syn) syn :listen)))
(t
(funcall (stream-misc syn) syn operation arg1 arg2)))))
;;;; Two-Way streams:
(defstruct (two-way-stream
(:include stream
(in #'two-way-in)
(bin #'two-way-bin)
(n-bin #'two-way-n-bin)
(out #'two-way-out)
(bout #'two-way-bout)
(sout #'two-way-sout)
(misc #'two-way-misc))
(:print-function %print-two-way-stream)
(:constructor make-two-way-stream (input-stream output-stream)))
;; We read from this stream...
(input-stream (required-argument) :type stream :read-only t)
(output-stream (required-argument) :type stream :read-only t))
(defun %print-two-way-stream (s stream d)
(declare (ignore d))
(format stream "#<Two-Way Stream, Input = ~S, Output = ~S>"
(two-way-stream-input-stream s)
(two-way-stream-output-stream s)))
(setf (documentation 'make-two-way-stream 'function)
"Returns a bidirectional stream which gets its input from Input-Stream and
sends its output to Output-Stream.")
(macrolet ((out-fun (name slot &rest args)
`(defun ,name (stream ,@args)
(let ((syn (two-way-stream-output-stream stream)))
(funcall (,slot syn) syn ,@args)))))
(out-fun two-way-out stream-out ch)
(out-fun two-way-bout stream-bout n)
(out-fun two-way-sout stream-sout string start end))
(macrolet ((in-fun (name fun &rest args)
`(defun ,name (stream ,@args)
(force-output (two-way-stream-output-stream stream))
(,fun (two-way-stream-input-stream stream) ,@args))))
(in-fun two-way-in read-char eof-errorp eof-value)
(in-fun two-way-bin read-byte eof-errorp eof-value)
(in-fun two-way-n-bin read-n-bytes buffer start numbytes eof-errorp))
(defun two-way-misc (stream operation &optional arg1 arg2)
(let* ((in (two-way-stream-input-stream stream))
(in-method (stream-misc in))
(out (two-way-stream-output-stream stream))
(out-method (stream-misc out)))
(case operation
(:listen (or (/= (the fixnum (stream-in-index in)) in-buffer-length)
(funcall in-method in :listen)))
(:read-line
(force-output out)
(read-line in arg1 arg2))
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
((:finish-output :force-output :clear-output)
(funcall out-method out operation arg1 arg2))
((:clear-input :unread)
(funcall in-method in operation arg1 arg2))
(:element-type
(let ((in-type (funcall in-method in :element-type))
(out-type (funcall out-method out :element-type)))
(if (equal in-type out-type)
in-type `(and ,in-type ,out-type))))
(:close
(funcall in-method in :close arg1)
(funcall out-method out :close arg1)
(set-closed-flame stream))
(t
(or (funcall in-method in operation arg1 arg2)
(funcall out-method out operation arg1 arg2))))))
;;;; Concatenated Streams:
(defstruct (concatenated-stream
(:include stream
(in #'concatenated-in)
(bin #'concatenated-bin)
(misc #'concatenated-misc))
(:print-function %print-concatenated-stream)
(:constructor
make-concatenated-stream (&rest streams &aux (current streams))))
;; The car of this is the stream we are reading from now.
current
;; This is a list of all the streams. We need to remember them so that
;; we can close them.
(streams nil :type list :read-only t))
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
(defun %print-concatenated-stream (s stream d)
(declare (ignore d))
(format stream "#<Concatenated Stream, Streams = ~S>"
(concatenated-stream-streams s)))
(setf (documentation 'make-concatenated-stream 'function)
"Returns a stream which takes its input from each of the Streams in turn,
going on to the next at EOF.")
(macrolet ((in-fun (name fun)
`(defun ,name (stream eof-errorp eof-value)
(do ((current (concatenated-stream-current stream) (cdr current)))
((null current)
(eof-or-lose stream eof-errorp eof-value))
(let* ((stream (car current))
(result (,fun stream nil nil)))
(when result (return result)))
(setf (concatenated-stream-current stream) current)))))
(in-fun concatenated-in read-char)
(in-fun concatenated-bin read-byte))
;;; Concatenated-Readline is somewhat hairy, since we may need to
;;; do several readlines and concatenate the result if the lines are
;;; terminated by eof.
;;;
(defun concatenated-readline (stream eof-errorp eof-value)
;; Loop until we find a stream that will give us something or we error
;; out.
(do ((current (concatenated-stream-current stream) (cdr current)))
((null current)
(eof-or-lose stream eof-errorp eof-value))
(setf (concatenated-stream-current stream) current)
(let ((this (car current)))
(multiple-value-bind (result eofp)
(read-line this nil :eof)
(declare (type (or simple-string (member :eof)) result))
;; Once we have found some input, we loop until we either find a
;; line not terminated by eof or hit eof on the last stream.
(do ((current (cdr current) (cdr current))
(new ""))
((or (not eofp) (null current))
(return-from concatenated-readline (values result eofp)))
(declare (type (or simple-string (member :eof)) new))
(setf (concatenated-stream-current stream) current)
(let ((this (car current)))
(multiple-value-setq (new eofp)
(read-line this nil :eof))
(if (eq new :eof)
(setq eofp t)
(setq result (concatenate 'simple-string result new))))))))))
(defun concatenated-misc (stream operation &optional arg1 arg2)
(if (eq operation :read-line)
(concatenated-readline stream arg1 arg2)
(let ((left (concatenated-stream-current stream)))
(when left
(let* ((current (car left))
(misc (stream-misc current)))
(case operation
(:listen
(loop
(let ((stuff (funcall misc current :listen)))
(cond ((eq stuff :eof)
;; Advance current, and try again.
(pop (concatenated-stream-current stream))
(setf current
(car (concatenated-stream-current stream)))
(unless current
;; No further streams. EOF.
(return :eof))
(setf misc (stream-misc current)))
(stuff
;; Stuff's available.
(return t))
(t
;; Nothing available yet.
(return nil))))))
(:close
(dolist (stream (concatenated-stream-streams stream))
(funcall (stream-misc stream) stream :close arg1))
(set-closed-flame stream))
(t
(funcall misc current operation arg1 arg2))))))))
;;;; Echo Streams:
(defstruct (echo-stream
(:include two-way-stream
(in #'echo-in)
(bin #'echo-bin)
(misc #'echo-misc)
(n-bin #'ill-bin))
(:print-function %print-echo-stream)
(:constructor make-echo-stream (input-stream output-stream)))
unread-stuff)
(macrolet ((in-fun (name fun out-slot &rest args)
`(defun ,name (stream ,@args)
(or (pop (echo-stream-unread-stuff stream))
(let* ((in (echo-stream-input-stream stream))
(out (echo-stream-output-stream stream))
(result (,fun in ,@args)))
(funcall (,out-slot out) out result)
result)))))
(in-fun echo-in read-char stream-out eof-errorp eof-value)
(in-fun echo-bin read-byte stream-bout eof-errorp eof-value))
(defun echo-misc (stream operation &optional arg1 arg2)
(let* ((in (two-way-stream-input-stream stream))
(in-method (stream-misc in))
(out (two-way-stream-output-stream stream))
(out-method (stream-misc out)))
(case operation
(:listen (or (not (null (echo-stream-unread-stuff stream)))
(/= (the fixnum (stream-in-index in)) in-buffer-length)
(:unread (push arg1 (echo-stream-unread-stuff stream)))
(let* ((stuff (echo-stream-unread-stuff stream))
(newline-pos (position #\newline stuff)))
(if newline-pos
(progn
(setf (echo-stream-unread-stuff stream)
(subseq stuff (1+ newline-pos)))
(values (coerce (subseq stuff 0 newline-pos) 'simple-string)
nil))
(multiple-value-bind (result eofp)
(read-line in arg1 arg2)
(if eofp
(write-string result out)
(write-line result out))
(setf (echo-stream-unread-stuff stream) nil)
(values (if stuff
(concatenate 'simple-string stuff result)
result)
eofp)))))
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
(:element-type
(let ((in-type (funcall in-method in :element-type))
(out-type (funcall out-method out :element-type)))
(if (equal in-type out-type)
in-type `(and ,in-type ,out-type))))
(:close
(funcall in-method in :close arg1)
(funcall out-method out :close arg1)
(set-closed-flame stream))
(t
(or (funcall in-method in operation arg1 arg2)
(funcall out-method out operation arg1 arg2))))))
(defun %print-echo-stream (s stream d)
(declare (ignore d))
(format stream "#<Echo Stream, Input = ~S, Output = ~S>"
(two-way-stream-input-stream s)
(two-way-stream-output-stream s)))
(setf (documentation 'make-echo-stream 'function)
"Returns a bidirectional stream which gets its input from Input-Stream and
sends its output to Output-Stream. In addition, all input is echoed to
the output stream")
;;;; String Input Streams:
(defstruct (string-input-stream
(:include stream
(in #'string-inch)
(misc #'string-in-misc))
(:print-function %print-string-input-stream)
(:constructor internal-make-string-input-stream
(string current end)))
(string nil :type simple-string)
(current nil :type fixnum)
(end nil :type fixnum))
(defun %print-string-input-stream (s stream d)
(declare (ignore s d))
(write-string "#<String-Input Stream>" stream))
(defun string-inch (stream eof-errorp eof-value)
(let ((string (string-input-stream-string stream))
(index (string-input-stream-current stream)))
(declare (simple-string string) (fixnum index))
(cond ((= index (the fixnum (string-input-stream-end stream)))
(eof-or-lose stream eof-errorp eof-value))
(t
(setf (string-input-stream-current stream) (1+ index))
(aref string index)))))
(defun string-in-misc (stream operation &optional arg1 arg2)
(case operation
(:file-position
(if arg1
(setf (string-input-stream-current stream) arg1)
(string-input-stream-current stream)))
(:file-length (length (string-input-stream-string stream)))
(:read-line
(let ((string (string-input-stream-string stream))
(current (string-input-stream-current stream))
(end (string-input-stream-end stream)))
(declare (simple-string string) (fixnum current end))
(if (= current end)
(eof-or-lose stream arg1 arg2)
(let ((pos (position #\newline string :start current :end end)))
(if pos
(let* ((res-length (- (the fixnum pos) current))
(result (make-string res-length)))
(%primitive byte-blt string current result 0 res-length)
(setf (string-input-stream-current stream)
(1+ (the fixnum pos)))
(values result nil))
(let* ((res-length (- end current))
(result (make-string res-length)))
(%primitive byte-blt string current result 0 res-length)
(setf (string-input-stream-current stream) end)
(values result t)))))))
(:unread (decf (string-input-stream-current stream)))
(:listen (or (/= (the fixnum (string-input-stream-current stream))
(the fixnum (string-input-stream-end stream)))
:eof))
(:element-type 'string-char)))
(defun make-string-input-stream (string &optional
(start 0) (end (length string)))
"Returns an input stream which will supply the characters of String between
Start and End in order."
(declare (type string string)
(type index start)
(or (index null) end))
(internal-make-string-input-stream (coerce string 'simple-string)
start end))
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
;;;; String Output Streams:
(defstruct (string-output-stream
(:include stream
(out #'string-ouch)
(sout #'string-sout)
(misc #'string-out-misc))
(:print-function %print-string-output-stream)
(:constructor make-string-output-stream ()))
;; The string we throw stuff in.
(string (make-string 40) :type simple-string)
;; Index of the next location to use.
(index 0 :type fixnum))
(defun %print-string-output-stream (s stream d)
(declare (ignore s d))
(write-string "#<String-Output Stream>" stream))
(setf (documentation 'make-string-output-stream 'function)
"Returns an Output stream which will accumulate all output given it for
the benefit of the function Get-Output-Stream-String.")
(defun string-ouch (stream character)
(let ((current (string-output-stream-index stream))
(workspace (string-output-stream-string stream)))
(declare (simple-string workspace) (fixnum current))
(if (= current (the fixnum (length workspace)))
(let ((new-workspace (make-string (* current 2))))
(replace new-workspace workspace)
(setf (aref new-workspace current) character)
(setf (string-output-stream-string stream) new-workspace))
(setf (aref workspace current) character))
(setf (string-output-stream-index stream) (1+ current))))
(defun string-sout (stream string start end)
(declare (simple-string string) (fixnum start end))
(let* ((current (string-output-stream-index stream))
(length (- end start))
(dst-end (+ length current))
(workspace (string-output-stream-string stream)))
(declare (simple-string workspace)
(fixnum current length dst-end))
(if (> dst-end (the fixnum (length workspace)))
(let ((new-workspace (make-string (+ (* current 2) length))))
(replace new-workspace workspace :end2 current)
(replace new-workspace string
:start1 current :end1 dst-end
:start2 start :end2 end)
(replace workspace string
:start1 current :end1 dst-end
:start2 start :end2 end))
(setf (string-output-stream-index stream) dst-end)))
(defun string-out-misc (stream operation &optional arg1 arg2)
(:file-position
(if (null arg1)
(:charpos
(do ((index (1- (the fixnum (string-output-stream-index stream)))
(1- index))
(count 0 (1+ count))
(string (string-output-stream-string stream)))
((< index 0) count)
(declare (simple-string string)
(fixnum index count))
(if (char= (schar string index) #\newline)
(return count))))
(:element-type 'string-char)))
(defun get-output-stream-string (stream)
"Returns a string of all the characters sent to a stream made by
Make-String-Output-Stream since the last call to this function."
(declare (type string-output-stream stream))
(let* ((length (string-output-stream-index stream))
(result (make-string length)))
(replace result (string-output-stream-string stream))
(setf (string-output-stream-index stream) 0)
result))
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
(defun dump-output-stream-string (in-stream out-stream)
"Dumps the characters buffer up in the In-Stream to the Out-Stream as
Get-Output-Stream-String would return them."
(write-string (string-output-stream-string in-stream) out-stream
:start 0 :end (string-output-stream-index in-stream))
(setf (string-output-stream-index in-stream) 0))
;;;; Fill-pointer streams:
;;;
;;; Fill pointer string output streams are not explicitly mentioned in
;;; the CLM, but they are required for the implementation of With-Output-To-String.
(defstruct (fill-pointer-output-stream
(:include stream
(out #'fill-pointer-ouch)
(sout #'fill-pointer-sout)
(misc #'fill-pointer-misc))
(:print-function
(lambda (s stream d)
(declare (ignore s d))
(write-string "#<Fill-Pointer String Output Stream>" stream)))
(:constructor make-fill-pointer-output-stream (string)))
;; The string we throw stuff in.
string)
(defun fill-pointer-ouch (stream character)
(let* ((buffer (fill-pointer-output-stream-string stream))
(current (fill-pointer buffer))
(current+1 (1+ current)))
(declare (fixnum current))
(with-array-data ((workspace buffer) (start) (end))
(declare (simple-string workspace))
(let ((offset-current (+ start current)))
(declare (fixnum offset-current))
(if (= offset-current end)
(let* ((new-length (* current 2))
(new-workspace (make-string new-length)))
(declare (simple-string new-workspace))
(%primitive byte-blt workspace start new-workspace 0 current)
(setf workspace new-workspace)
(setf offset-current current)
(set-array-header buffer workspace new-length
current+1 0 new-length nil))
(setf (fill-pointer buffer) current+1))
(setf (schar workspace offset-current) character)))
current+1))
(defun fill-pointer-sout (stream string start end)
(declare (simple-string string) (fixnum start end))
(let* ((buffer (fill-pointer-output-stream-string stream))
(current (fill-pointer buffer))
(string-len (- end start))
(dst-end (+ string-len current)))
(declare (fixnum current dst-end string-len))
(with-array-data ((workspace buffer) (dst-start) (dst-length))
(declare (simple-string workspace))
(let ((offset-dst-end (+ dst-start dst-end))
(offset-current (+ dst-start current)))
(declare (fixnum offset-dst-end offset-current))
(if (> offset-dst-end dst-length)
(let* ((new-length (+ (the fixnum (* current 2)) string-len))
(new-workspace (make-string new-length)))
(declare (simple-string new-workspace))
(%primitive byte-blt workspace dst-start new-workspace 0 current)
(setf workspace new-workspace)
(setf offset-current current)
(setf offset-dst-end dst-end)
(set-array-header buffer workspace new-length
dst-end 0 new-length nil))
(setf (fill-pointer buffer) dst-end))
(%primitive byte-blt string start
workspace offset-current offset-dst-end)))
dst-end))
(defun fill-pointer-misc (stream operation &optional arg1 arg2)
(declare (ignore arg1 arg2))
(case operation
(:charpos
(let* ((buffer (fill-pointer-output-stream-string stream))
(current (fill-pointer buffer)))
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
(with-array-data ((string buffer) (start) (end current))
(declare (simple-string string) (ignore start))
(let ((found (position #\newline string :test #'char=
:end end :from-end t)))
(if found
(- end (the fixnum found))
current)))))
(:element-type 'string-char)))
;;;; Indenting streams:
(defstruct (indenting-stream (:include stream
(out #'indenting-out)
(sout #'indenting-sout)
(misc #'indenting-misc))
(:print-function %print-indenting-stream)
(:constructor make-indenting-stream (stream)))
;; The stream we're based on:
stream
;; How much we indent on each line:
(indentation 0))
(setf (documentation 'make-indenting-stream 'function)
"Returns an ouput stream which indents its output by some amount.")
(defun %print-indenting-stream (s stream d)
(declare (ignore s d))
(write-string "#<Indenting Stream>" stream))
;;; Indenting-Indent writes the right number of spaces needed to indent output on
;;; the given Stream based on the specified Sub-Stream.
(defmacro indenting-indent (stream sub-stream)
`(do ((i 0 (+ i 60))
(indentation (indenting-stream-indentation ,stream)))
((>= i indentation))
(funcall (stream-sout ,sub-stream) ,sub-stream
" "
0 (min 60 (- indentation i)))))
;;; Indenting-Out writes a character to an indenting stream.
(defun indenting-out (stream char)
(let ((sub-stream (indenting-stream-stream stream)))
(funcall (stream-out sub-stream) sub-stream char)
(if (char= char #\newline)
(indenting-indent stream sub-stream))))
;;; Indenting-Sout writes a string to an indenting stream.
(defun indenting-sout (stream string start end)
(declare (simple-string string) (fixnum start end))
(do ((i start)
(sub-stream (indenting-stream-stream stream)))
((= i end))
(let ((newline (position #\newline string :start i :end end)))
(cond (newline
(funcall (stream-sout sub-stream) sub-stream string i (1+ newline))
(indenting-indent stream sub-stream)
(setq i (+ newline 1)))
(t
(funcall (stream-sout sub-stream) sub-stream string i end)
(setq i end))))))
;;; Indenting-Misc just treats just the :Line-Length message differently.
;;; Indenting-Charpos says the charpos is the charpos of the base stream minus
;;; the stream's indentation.
(defun indenting-misc (stream operation &optional arg1 arg2)
(let* ((sub-stream (indenting-stream-stream stream))
(method (stream-misc sub-stream)))
(case operation
(:line-length
(let ((line-length (funcall method sub-stream operation)))
(if line-length
(- line-length (indenting-stream-indentation stream)))))
(:charpos
(let* ((sub-stream (indenting-stream-stream stream))
(charpos (funcall method sub-stream operation)))
(if charpos
(- charpos (indenting-stream-indentation stream)))))
(t
(funcall method sub-stream operation arg1 arg2)))))
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
(proclaim '(maybe-inline read-char unread-char read-byte listen))
;;;; Case frobbing streams, used by format ~(...~).
(in-package "EXT")
(export '(make-case-frob-stream))
(in-package "LISP")
(defstruct (case-frob-stream
(:include stream
(:misc #'case-frob-misc))
(:constructor %make-case-frob-stream (target out sout)))
(target (required-argument) :type stream))
(defun make-case-frob-stream (target kind)
"Returns a stream that sends all output to the stream TARGET, but modifies
the case of letters, depending on KIND, which should be one of:
:upcase - convert to upper case.
:downcase - convert to lower case.
:capitalize - convert the first letter of words to upper case and the
rest of the word to lower case.
:capitalize-first - convert the first letter of the first word to upper
case and everything else to lower case."
(declare (type stream target)
(type (member :upcase :downcase :capitalize :capitalize-first)
kind)
(values stream))
(if (case-frob-stream-p target)
;; If we are going to be writing to a stream that already does case
;; frobbing, why bother frobbing the case just so it can frob it
;; again?
target
(multiple-value-bind
(out sout)
(ecase kind
(:upcase
(values #'case-frob-upcase-out
#'case-frob-upcase-sout))
(:downcase
(values #'case-frob-downcase-out
#'case-frob-downcase-sout))
(:capitalize
(values #'case-frob-capitalize-out
#'case-frob-capitalize-sout))
(:capitalize-first
(values #'case-frob-capitalize-first-out
#'case-frob-capitalize-first-sout)))
(%make-case-frob-stream target out sout))))
(defun case-frob-misc (stream op &optional arg1 arg2)
(declare (type case-frob-stream stream))
(case op
(:close)
(t
(let ((target (case-frob-stream-target stream)))
(funcall (stream-misc target) target op arg1 arg2)))))
(defun case-frob-upcase-out (stream char)
(declare (type case-frob-stream stream)
(let ((target (case-frob-stream-target stream)))
(funcall (stream-out target) target (char-upcase char))))
(defun case-frob-upcase-sout (stream str start end)
(declare (type case-frob-stream stream)
(type simple-base-string str)
(type index start)
(type (or index null) end))
(let* ((target (case-frob-stream-target stream))
(len (length str))
(end (or end len)))
(funcall (stream-sout target) target
(if (and (zerop start) (= len end))
(string-upcase str)
(nstring-upcase (subseq str start end)))
0
(- end start))))
(defun case-frob-downcase-out (stream char)
(declare (type case-frob-stream stream)
(let ((target (case-frob-stream-target stream)))
(funcall (stream-out target) target (char-downcase char))))
(defun case-frob-downcase-sout (stream str start end)
(declare (type case-frob-stream stream)
(type simple-base-string str)
(type index start)
(type (or index null) end))
(let* ((target (case-frob-stream-target stream))
(len (length str))
(end (or end len)))
(funcall (stream-sout target) target
(if (and (zerop start) (= len end))
(string-downcase str)
(nstring-downcase (subseq str start end)))
0
(- end start))))
(defun case-frob-capitalize-out (stream char)
(declare (type case-frob-stream stream)
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
(let ((target (case-frob-stream-target stream)))
(cond ((alphanumericp char)
(funcall (stream-out target) target (char-upcase char))
(setf (case-frob-stream-out stream)
#'case-frob-capitalize-aux-out)
(setf (case-frob-stream-sout stream)
#'case-frob-capitalize-aux-sout))
(t
(funcall (stream-out target) target char)))))
(defun case-frob-capitalize-sout (stream str start end)
(declare (type case-frob-stream stream)
(type simple-base-string str)
(type index start)
(type (or index null) end))
(let* ((target (case-frob-stream-target stream))
(str (subseq str start end))
(len (length str))
(inside-word nil))
(dotimes (i len)
(let ((char (schar str i)))
(cond ((not (alphanumericp char))
(setf inside-word nil))
(inside-word
(setf (schar str i) (char-downcase char)))
(t
(setf inside-word t)
(setf (schar str i) (char-upcase char))))))
(when inside-word
(setf (case-frob-stream-out stream)
#'case-frob-capitalize-aux-out)
(setf (case-frob-stream-sout stream)
#'case-frob-capitalize-aux-sout))
(funcall (stream-sout target) target str 0 len)))
(defun case-frob-capitalize-aux-out (stream char)
(declare (type case-frob-stream stream)
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
(let ((target (case-frob-stream-target stream)))
(cond ((alphanumericp char)
(funcall (stream-out target) target (char-downcase char)))
(t
(funcall (stream-out target) target char)
(setf (case-frob-stream-out stream)
#'case-frob-capitalize-out)
(setf (case-frob-stream-sout stream)
#'case-frob-capitalize-sout)))))
(defun case-frob-capitalize-aux-sout (stream str start end)
(declare (type case-frob-stream stream)
(type simple-base-string str)
(type index start)
(type (or index null) end))
(let* ((target (case-frob-stream-target stream))
(str (subseq str start end))
(len (length str))
(inside-word t))
(dotimes (i len)
(let ((char (schar str i)))
(cond ((not (alphanumericp char))
(setf inside-word nil))
(inside-word
(setf (schar str i) (char-downcase char)))
(t
(setf inside-word t)
(setf (schar str i) (char-upcase char))))))
(unless inside-word
(setf (case-frob-stream-out stream)
#'case-frob-capitalize-out)
(setf (case-frob-stream-sout stream)
#'case-frob-capitalize-sout))
(funcall (stream-sout target) target str 0 len)))
(defun case-frob-capitalize-first-out (stream char)
(declare (type case-frob-stream stream)
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
(let ((target (case-frob-stream-target stream)))
(cond ((alphanumericp char)
(funcall (stream-out target) target (char-upcase char))
(setf (case-frob-stream-out stream)
#'case-frob-downcase-out)
(setf (case-frob-stream-sout stream)
#'case-frob-downcase-sout))
(t
(funcall (stream-out target) target char)))))
(defun case-frob-capitalize-first-sout (stream str start end)
(declare (type case-frob-stream stream)
(type simple-base-string str)
(type index start)
(type (or index null) end))
(let* ((target (case-frob-stream-target stream))
(str (subseq str start end))
(len (length str)))
(dotimes (i len)
(let ((char (schar str i)))
(when (alphanumericp char)
(setf (schar str i) (char-upcase char))
(do ((i (1+ i) (1+ i)))
((= i len))
(setf (schar str i) (char-downcase (schar str i))))
(setf (case-frob-stream-out stream)
#'case-frob-downcase-out)
(setf (case-frob-stream-sout stream)
#'case-frob-downcase-sout)
(return))))
(funcall (stream-sout target) target str 0 len)))
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
;;;; Public interface from "EXTENSIONS" package.
(in-package "EXT")
(export '(get-stream-command stream-command stream-command-p stream-command-name
stream-command-args make-stream-command))
(defstruct (stream-command (:print-function print-stream-command)
(:constructor make-stream-command
(name &optional args)))
(name nil :type symbol)
(args nil :type list))
(defun print-stream-command (obj str n)
(declare (ignore n))
(format str "#<Stream-Cmd ~S>" (stream-command-name obj)))
;;; GET-STREAM-COMMAND -- Public.
;;;
;;; We can't simply call the stream's misc method because because nil is an
;;; ambiguous return value: does it mean text arrived, or does it mean the
;;; stream's misc method had no :get-command implementation. We can't return
;;; nil until there is text input. We don't need to loop because any stream
;;; implementing :get-command would wait until it had some input. If the
;;; LISTEN fails, then we have some random stream we must wait on.
;;;
(defun get-stream-command (stream)
"This takes a stream and waits for text or a command to appear on it. If
text appears before a command, this returns nil, and otherwise it returns
a command."
(let ((cmdp (funcall (lisp::stream-misc stream) stream :get-command)))
(cond (cmdp)
((listen stream)
nil)
(t
;; This waits for input and returns nil when it arrives.
(unread-char (read-char stream) stream)))))