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

Contents of /src/code/fd-stream.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.60 - (show annotations)
Fri Aug 2 14:43:08 2002 UTC (11 years, 8 months ago) by toy
Branch: MAIN
Changes since 1.59: +8 -2 lines
Only create a buffer for streams of type (unsigned-byte 8).  Fixes the
bug where read-byte from (signed-byte 8) (or less) streams would
produce unsigned instead of signed numbers.
1 ;;; -*- Log: code.log; Package: LISP -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/fd-stream.lisp,v 1.60 2002/08/02 14:43:08 toy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Streams for UNIX file descriptors.
13 ;;;
14 ;;; Written by William Lott, July 1989 - January 1990.
15 ;;; Some tuning by Rob MacLachlan.
16 ;;;
17 ;;; **********************************************************************
18
19
20 (in-package "SYSTEM")
21
22 (export '(fd-stream fd-stream-p fd-stream-fd make-fd-stream
23 io-timeout beep *beep-function* output-raw-bytes
24 *tty* *stdin* *stdout* *stderr*))
25
26
27 (in-package "EXTENSIONS")
28
29 (export '(*backup-extension*))
30
31
32 (in-package "LISP")
33
34 (export '(file-stream file-string-length stream-external-format))
35 (deftype file-stream () 'fd-stream)
36
37
38 ;;;; Buffer manipulation routines.
39
40 (defvar *available-buffers* ()
41 "List of available buffers. Each buffer is an sap pointing to
42 bytes-per-buffer of memory.")
43
44 (defconstant bytes-per-buffer (* 4 1024)
45 "Number of bytes per buffer.")
46
47 ;;; NEXT-AVAILABLE-BUFFER -- Internal.
48 ;;;
49 ;;; Returns the next available buffer, creating one if necessary.
50 ;;;
51 (declaim (inline next-available-buffer))
52 ;;;
53 (defun next-available-buffer ()
54 (if *available-buffers*
55 (pop *available-buffers*)
56 (allocate-system-memory bytes-per-buffer)))
57
58
59 ;;;; The FD-STREAM structure.
60
61 (defstruct (fd-stream
62 (:print-function %print-fd-stream)
63 (:constructor %make-fd-stream)
64 (:include lisp-stream
65 (misc #'fd-stream-misc-routine)))
66
67 (name nil) ; The name of this stream
68 (file nil) ; The file this stream is for
69 ;;
70 ;; The backup file namestring for the old file, for :if-exists :rename or
71 ;; :rename-and-delete.
72 (original nil :type (or simple-string null))
73 (delete-original nil) ; for :if-exists :rename-and-delete
74 ;;
75 ;;; Number of bytes per element.
76 (element-size 1 :type index)
77 (element-type 'base-char) ; The type of element being transfered.
78 (fd -1 :type fixnum) ; The file descriptor
79 ;;
80 ;; Controls when the output buffer is flushed.
81 (buffering :full :type (member :full :line :none))
82 ;;
83 ;; Character position if known.
84 (char-pos nil :type (or index null))
85 ;;
86 ;; T if input is waiting on FD. :EOF if we hit EOF.
87 (listen nil :type (member nil t :eof))
88 ;;
89 ;; The input buffer.
90 (unread nil)
91 (ibuf-sap nil :type (or system-area-pointer null))
92 (ibuf-length nil :type (or index null))
93 (ibuf-head 0 :type index)
94 (ibuf-tail 0 :type index)
95
96 ;; The output buffer.
97 (obuf-sap nil :type (or system-area-pointer null))
98 (obuf-length nil :type (or index null))
99 (obuf-tail 0 :type index)
100
101 ;; Output flushed, but not written due to non-blocking io.
102 (output-later nil)
103 (handler nil)
104 ;;
105 ;; Timeout specified for this stream, or NIL if none.
106 (timeout nil :type (or index null))
107 ;;
108 ;; Pathname of the file this stream is opened to (returned by PATHNAME.)
109 (pathname nil :type (or pathname null)))
110
111 (defun %print-fd-stream (fd-stream stream depth)
112 (declare (ignore depth) (stream stream))
113 (format stream "#<Stream for ~A>"
114 (fd-stream-name fd-stream)))
115
116
117 (define-condition io-timeout (stream-error)
118 ((direction :reader io-timeout-direction :initarg :direction))
119 (:report
120 (lambda (condition stream)
121 (declare (stream stream))
122 (format stream "Timeout ~(~A~)ing ~S."
123 (io-timeout-direction condition)
124 (stream-error-stream condition)))))
125
126
127 ;;;; Output routines and related noise.
128
129 (defvar *output-routines* ()
130 "List of all available output routines. Each element is a list of the
131 element-type output, the kind of buffering, the function name, and the number
132 of bytes per element.")
133
134 ;;; DO-OUTPUT-LATER -- internal
135 ;;;
136 ;;; Called by the server when we can write to the given file descriptor.
137 ;;; Attemt to write the data again. If it worked, remove the data from the
138 ;;; output-later list. If it didn't work, something is wrong.
139 ;;;
140 (defun do-output-later (stream)
141 (let* ((stuff (pop (fd-stream-output-later stream)))
142 (base (car stuff))
143 (start (cadr stuff))
144 (end (caddr stuff))
145 (reuse-sap (cadddr stuff))
146 (length (- end start)))
147 (declare (type index start end length))
148 (multiple-value-bind
149 (count errno)
150 (unix:unix-write (fd-stream-fd stream)
151 base
152 start
153 length)
154 (cond ((not count)
155 (if (= errno unix:ewouldblock)
156 (error "Write would have blocked, but SERVER told us to go.")
157 (error "While writing ~S: ~A"
158 stream (unix:get-unix-error-msg errno))))
159 ((eql count length) ; Hot damn, it workded.
160 (when reuse-sap
161 (push base *available-buffers*)))
162 ((not (null count)) ; Sorta worked.
163 (push (list base
164 (the index (+ start count))
165 end)
166 (fd-stream-output-later stream))))))
167 (unless (fd-stream-output-later stream)
168 (system:remove-fd-handler (fd-stream-handler stream))
169 (setf (fd-stream-handler stream) nil)))
170
171 ;;; OUTPUT-LATER -- internal
172 ;;;
173 ;;; Arange to output the string when we can write on the file descriptor.
174 ;;;
175 (defun output-later (stream base start end reuse-sap)
176 (cond ((null (fd-stream-output-later stream))
177 (setf (fd-stream-output-later stream)
178 (list (list base start end reuse-sap)))
179 (setf (fd-stream-handler stream)
180 (system:add-fd-handler (fd-stream-fd stream)
181 :output
182 #'(lambda (fd)
183 (declare (ignore fd))
184 (do-output-later stream)))))
185 (t
186 (nconc (fd-stream-output-later stream)
187 (list (list base start end reuse-sap)))))
188 (when reuse-sap
189 (let ((new-buffer (next-available-buffer)))
190 (setf (fd-stream-obuf-sap stream) new-buffer)
191 (setf (fd-stream-obuf-length stream) bytes-per-buffer))))
192
193 ;;; DO-OUTPUT -- internal
194 ;;;
195 ;;; Output the given noise. Check to see if there are any pending writes. If
196 ;;; so, just queue this one. Otherwise, try to write it. If this would block,
197 ;;; queue it.
198 ;;;
199 (defun do-output (stream base start end reuse-sap)
200 (declare (type fd-stream stream)
201 (type (or system-area-pointer (simple-array * (*))) base)
202 (type index start end))
203 (if (not (null (fd-stream-output-later stream))) ; something buffered.
204 (progn
205 (output-later stream base start end reuse-sap)
206 ;; ### check to see if any of this noise can be output
207 )
208 (let ((length (- end start)))
209 (multiple-value-bind
210 (count errno)
211 (unix:unix-write (fd-stream-fd stream) base start length)
212 (cond ((not count)
213 (if (= errno unix:ewouldblock)
214 (output-later stream base start end reuse-sap)
215 (error "While writing ~S: ~A"
216 stream (unix:get-unix-error-msg errno))))
217 ((not (eql count length))
218 (output-later stream base (the index (+ start count))
219 end reuse-sap)))))))
220
221
222 ;;; FLUSH-OUTPUT-BUFFER -- internal
223 ;;;
224 ;;; Flush any data in the output buffer.
225 ;;;
226 (defun flush-output-buffer (stream)
227 (let ((length (fd-stream-obuf-tail stream)))
228 (unless (= length 0)
229 (do-output stream (fd-stream-obuf-sap stream) 0 length t)
230 (setf (fd-stream-obuf-tail stream) 0))))
231
232 ;;; DEF-OUTPUT-ROUTINES -- internal
233 ;;;
234 ;;; Define output routines that output numbers size bytes long for the
235 ;;; given bufferings. Use body to do the actual output.
236 ;;;
237 (defmacro def-output-routines ((name size &rest bufferings) &body body)
238 (declare (optimize (speed 1)))
239 (cons 'progn
240 (mapcar
241 #'(lambda (buffering)
242 (let ((function
243 (intern (let ((*print-case* :upcase))
244 (format nil name (car buffering))))))
245 `(progn
246 (defun ,function (stream byte)
247 ,(unless (eq (car buffering) :none)
248 `(when (< (fd-stream-obuf-length stream)
249 (+ (fd-stream-obuf-tail stream)
250 ,size))
251 (flush-output-buffer stream)))
252 ,@body
253 (incf (fd-stream-obuf-tail stream) ,size)
254 ,(ecase (car buffering)
255 (:none
256 `(flush-output-buffer stream))
257 (:line
258 `(when (eq (char-code byte) (char-code #\Newline))
259 (flush-output-buffer stream)))
260 (:full
261 ))
262 (values))
263 (setf *output-routines*
264 (nconc *output-routines*
265 ',(mapcar
266 #'(lambda (type)
267 (list type
268 (car buffering)
269 function
270 size))
271 (cdr buffering)))))))
272 bufferings)))
273
274 (def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
275 1
276 (:none character)
277 (:line character)
278 (:full character))
279 (if (char= byte #\Newline)
280 (setf (fd-stream-char-pos stream) 0)
281 (incf (fd-stream-char-pos stream)))
282 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
283 (char-code byte)))
284
285 (def-output-routines ("OUTPUT-UNSIGNED-BYTE-~A-BUFFERED"
286 1
287 (:none (unsigned-byte 8))
288 (:full (unsigned-byte 8)))
289 (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
290 byte))
291
292 (def-output-routines ("OUTPUT-SIGNED-BYTE-~A-BUFFERED"
293 1
294 (:none (signed-byte 8))
295 (:full (signed-byte 8)))
296 (setf (signed-sap-ref-8 (fd-stream-obuf-sap stream)
297 (fd-stream-obuf-tail stream))
298 byte))
299
300 (def-output-routines ("OUTPUT-UNSIGNED-SHORT-~A-BUFFERED"
301 2
302 (:none (unsigned-byte 16))
303 (:full (unsigned-byte 16)))
304 (setf (sap-ref-16 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
305 byte))
306
307 (def-output-routines ("OUTPUT-SIGNED-SHORT-~A-BUFFERED"
308 2
309 (:none (signed-byte 16))
310 (:full (signed-byte 16)))
311 (setf (signed-sap-ref-16 (fd-stream-obuf-sap stream)
312 (fd-stream-obuf-tail stream))
313 byte))
314
315 (def-output-routines ("OUTPUT-UNSIGNED-LONG-~A-BUFFERED"
316 4
317 (:none (unsigned-byte 32))
318 (:full (unsigned-byte 32)))
319 (setf (sap-ref-32 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
320 byte))
321
322 (def-output-routines ("OUTPUT-SIGNED-LONG-~A-BUFFERED"
323 4
324 (:none (signed-byte 32))
325 (:full (signed-byte 32)))
326 (setf (signed-sap-ref-32 (fd-stream-obuf-sap stream)
327 (fd-stream-obuf-tail stream))
328 byte))
329
330
331 ;;; OUTPUT-RAW-BYTES -- public
332 ;;;
333 ;;; Does the actual output. If there is space to buffer the string, buffer
334 ;;; it. If the string would normally fit in the buffer, but doesn't because
335 ;;; of other stuff in the buffer, flush the old noise out of the buffer and
336 ;;; put the string in it. Otherwise we have a very long string, so just
337 ;;; send it directly (after flushing the buffer, of course).
338 ;;;
339 (defun output-raw-bytes (stream thing &optional start end)
340 "Output THING to stream. THING can be any kind of vector or a sap. If THING
341 is a SAP, END must be supplied (as length won't work)."
342 (let ((start (or start 0))
343 (end (or end (length (the (simple-array * (*)) thing)))))
344 (declare (type index start end))
345 (let* ((len (fd-stream-obuf-length stream))
346 (tail (fd-stream-obuf-tail stream))
347 (space (- len tail))
348 (bytes (- end start))
349 (newtail (+ tail bytes)))
350 (cond ((minusp bytes) ; Error case
351 (cerror "Just go on as if nothing happened..."
352 "~S called with :END before :START!"
353 'output-raw-bytes))
354 ((zerop bytes)) ; Easy case
355 ((<= bytes space)
356 (if (system-area-pointer-p thing)
357 (system-area-copy thing
358 (* start vm:byte-bits)
359 (fd-stream-obuf-sap stream)
360 (* tail vm:byte-bits)
361 (* bytes vm:byte-bits))
362 (copy-to-system-area thing
363 (+ (* start vm:byte-bits)
364 (* vm:vector-data-offset vm:word-bits))
365 (fd-stream-obuf-sap stream)
366 (* tail vm:byte-bits)
367 (* bytes vm:byte-bits)))
368 (setf (fd-stream-obuf-tail stream) newtail))
369 ((<= bytes len)
370 (flush-output-buffer stream)
371 (if (system-area-pointer-p thing)
372 (system-area-copy thing
373 (* start vm:byte-bits)
374 (fd-stream-obuf-sap stream)
375 0
376 (* bytes vm:byte-bits))
377 (copy-to-system-area thing
378 (+ (* start vm:byte-bits)
379 (* vm:vector-data-offset vm:word-bits))
380 (fd-stream-obuf-sap stream)
381 0
382 (* bytes vm:byte-bits)))
383 (setf (fd-stream-obuf-tail stream) bytes))
384 (t
385 (flush-output-buffer stream)
386 (do-output stream thing start end nil))))))
387
388 ;;; FD-SOUT -- internal
389 ;;;
390 ;;; Routine to use to output a string. If the stream is unbuffered, slam
391 ;;; the string down the file descriptor, otherwise use OUTPUT-RAW-BYTES to
392 ;;; buffer the string. Update charpos by checking to see where the last newline
393 ;;; was.
394 ;;;
395 ;;; Note: some bozos (the FASL dumper) call write-string with things other
396 ;;; than strings. Therefore, we must make sure we have a string before calling
397 ;;; position on it.
398 ;;;
399 (defun fd-sout (stream thing start end)
400 (let ((start (or start 0))
401 (end (or end (length (the vector thing)))))
402 (declare (type index start end))
403 (if (stringp thing)
404 (let ((last-newline (and (find #\newline (the simple-string thing)
405 :start start :end end)
406 (position #\newline (the simple-string thing)
407 :from-end t
408 :start start
409 :end end))))
410 (ecase (fd-stream-buffering stream)
411 (:full
412 (output-raw-bytes stream thing start end))
413 (:line
414 (output-raw-bytes stream thing start end)
415 (when last-newline
416 (flush-output-buffer stream)))
417 (:none
418 (do-output stream thing start end nil)))
419 (if last-newline
420 (setf (fd-stream-char-pos stream)
421 (- end last-newline 1))
422 (incf (fd-stream-char-pos stream)
423 (- end start))))
424 (ecase (fd-stream-buffering stream)
425 ((:line :full)
426 (output-raw-bytes stream thing start end))
427 (:none
428 (do-output stream thing start end nil))))))
429
430 ;;; PICK-OUTPUT-ROUTINE -- internal
431 ;;;
432 ;;; Find an output routine to use given the type and buffering. Return as
433 ;;; multiple values the routine, the real type transfered, and the number of
434 ;;; bytes per element.
435 ;;;
436 (defun pick-output-routine (type buffering)
437 (dolist (entry *output-routines*)
438 (when (and (subtypep type (car entry))
439 (eq buffering (cadr entry)))
440 (return (values (symbol-function (caddr entry))
441 (car entry)
442 (cadddr entry))))))
443
444
445 ;;;; Input routines and related noise.
446
447 (defvar *input-routines* ()
448 "List of all available input routines. Each element is a list of the
449 element-type input, the function name, and the number of bytes per element.")
450
451 ;;; DO-INPUT -- internal
452 ;;;
453 ;;; Fills the input buffer, and returns the first character. Throws to
454 ;;; eof-input-catcher if the eof was reached. Drops into system:server if
455 ;;; necessary.
456 ;;;
457 (defun do-input (stream)
458 (let ((fd (fd-stream-fd stream))
459 (ibuf-sap (fd-stream-ibuf-sap stream))
460 (buflen (fd-stream-ibuf-length stream))
461 (head (fd-stream-ibuf-head stream))
462 (tail (fd-stream-ibuf-tail stream)))
463 (declare (type index head tail))
464 (unless (zerop head)
465 (cond ((eql head tail)
466 (setf head 0)
467 (setf tail 0)
468 (setf (fd-stream-ibuf-head stream) 0)
469 (setf (fd-stream-ibuf-tail stream) 0))
470 (t
471 (decf tail head)
472 (system-area-copy ibuf-sap (* head vm:byte-bits)
473 ibuf-sap 0 (* tail vm:byte-bits))
474 (setf head 0)
475 (setf (fd-stream-ibuf-head stream) 0)
476 (setf (fd-stream-ibuf-tail stream) tail))))
477 (setf (fd-stream-listen stream) nil)
478 (multiple-value-bind
479 (count errno)
480 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
481 (unix:fd-zero read-fds)
482 (unix:fd-set fd read-fds)
483 (unix:unix-fast-select (1+ fd) (alien:addr read-fds) nil nil 0 0))
484 ;; Wait if input is not available or if interrupted.
485 (when (or (eql count 0)
486 (and (not count) (eql errno unix:eintr)))
487 (unless #-mp (system:wait-until-fd-usable
488 fd :input (fd-stream-timeout stream))
489 #+mp (mp:process-wait-until-fd-usable
490 fd :input (fd-stream-timeout stream))
491 (error 'io-timeout :stream stream :direction :read))))
492 (multiple-value-bind
493 (count errno)
494 (unix:unix-read fd
495 (system:int-sap (+ (system:sap-int ibuf-sap) tail))
496 (- buflen tail))
497 (cond ((null count)
498 (if (eql errno unix:ewouldblock)
499 (progn
500 (unless #-mp (system:wait-until-fd-usable
501 fd :input (fd-stream-timeout stream))
502 #+mp (mp:process-wait-until-fd-usable
503 fd :input (fd-stream-timeout stream))
504 (error 'io-timeout :stream stream :direction :read))
505 (do-input stream))
506 (error "Error reading ~S: ~A"
507 stream
508 (unix:get-unix-error-msg errno))))
509 ((zerop count)
510 (setf (fd-stream-listen stream) :eof)
511 (throw 'eof-input-catcher nil))
512 (t
513 (incf (fd-stream-ibuf-tail stream) count))))))
514
515 ;;; INPUT-AT-LEAST -- internal
516 ;;;
517 ;;; Makes sure there are at least ``bytes'' number of bytes in the input
518 ;;; buffer. Keeps calling do-input until that condition is met.
519 ;;;
520 (defmacro input-at-least (stream bytes)
521 (let ((stream-var (gensym))
522 (bytes-var (gensym)))
523 `(let ((,stream-var ,stream)
524 (,bytes-var ,bytes))
525 (loop
526 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
527 (fd-stream-ibuf-head ,stream-var))
528 ,bytes-var)
529 (return))
530 (do-input ,stream-var)))))
531
532 ;;; INPUT-WRAPPER -- intenal
533 ;;;
534 ;;; Macro to wrap around all input routines to handle eof-error noise.
535 ;;;
536 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
537 (let ((stream-var (gensym))
538 (element-var (gensym)))
539 `(let ((,stream-var ,stream))
540 (if (fd-stream-unread ,stream-var)
541 (prog1
542 (fd-stream-unread ,stream-var)
543 (setf (fd-stream-unread ,stream-var) nil)
544 (setf (fd-stream-listen ,stream-var) nil))
545 (let ((,element-var
546 (catch 'eof-input-catcher
547 (input-at-least ,stream-var ,bytes)
548 ,@read-forms)))
549 (cond (,element-var
550 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
551 ,element-var)
552 (t
553 (eof-or-lose ,stream-var ,eof-error ,eof-value))))))))
554
555 ;;; DEF-INPUT-ROUTINE -- internal
556 ;;;
557 ;;; Defines an input routine.
558 ;;;
559 (defmacro def-input-routine (name
560 (type size sap head)
561 &rest body)
562 `(progn
563 (defun ,name (stream eof-error eof-value)
564 (input-wrapper (stream ,size eof-error eof-value)
565 (let ((,sap (fd-stream-ibuf-sap stream))
566 (,head (fd-stream-ibuf-head stream)))
567 ,@body)))
568 (setf *input-routines*
569 (nconc *input-routines*
570 (list (list ',type ',name ',size))))))
571
572 ;;; INPUT-CHARACTER -- internal
573 ;;;
574 ;;; Routine to use in stream-in slot for reading string chars.
575 ;;;
576 (def-input-routine input-character
577 (character 1 sap head)
578 (code-char (sap-ref-8 sap head)))
579
580 ;;; INPUT-UNSIGNED-8BIT-BYTE -- internal
581 ;;;
582 ;;; Routine to read in an unsigned 8 bit number.
583 ;;;
584 (def-input-routine input-unsigned-8bit-byte
585 ((unsigned-byte 8) 1 sap head)
586 (sap-ref-8 sap head))
587
588 ;;; INPUT-SIGNED-8BIT-BYTE -- internal
589 ;;;
590 ;;; Routine to read in a signed 8 bit number.
591 ;;;
592 (def-input-routine input-signed-8bit-number
593 ((signed-byte 8) 1 sap head)
594 (signed-sap-ref-8 sap head))
595
596 ;;; INPUT-UNSIGNED-16BIT-BYTE -- internal
597 ;;;
598 ;;; Routine to read in an unsigned 16 bit number.
599 ;;;
600 (def-input-routine input-unsigned-16bit-byte
601 ((unsigned-byte 16) 2 sap head)
602 (sap-ref-16 sap head))
603
604 ;;; INPUT-SIGNED-16BIT-BYTE -- internal
605 ;;;
606 ;;; Routine to read in a signed 16 bit number.
607 ;;;
608 (def-input-routine input-signed-16bit-byte
609 ((signed-byte 16) 2 sap head)
610 (signed-sap-ref-16 sap head))
611
612 ;;; INPUT-UNSIGNED-32BIT-BYTE -- internal
613 ;;;
614 ;;; Routine to read in a unsigned 32 bit number.
615 ;;;
616 (def-input-routine input-unsigned-32bit-byte
617 ((unsigned-byte 32) 4 sap head)
618 (sap-ref-32 sap head))
619
620 ;;; INPUT-SIGNED-32BIT-BYTE -- internal
621 ;;;
622 ;;; Routine to read in a signed 32 bit number.
623 ;;;
624 (def-input-routine input-signed-32bit-byte
625 ((signed-byte 32) 4 sap head)
626 (signed-sap-ref-32 sap head))
627
628 ;;; PICK-INPUT-ROUTINE -- internal
629 ;;;
630 ;;; Find an input routine to use given the type. Return as multiple values
631 ;;; the routine, the real type transfered, and the number of bytes per element.
632 ;;;
633 (defun pick-input-routine (type)
634 (dolist (entry *input-routines*)
635 (when (subtypep type (car entry))
636 (return (values (symbol-function (cadr entry))
637 (car entry)
638 (caddr entry))))))
639
640 ;;; STRING-FROM-SAP -- internal
641 ;;;
642 ;;; Returns a string constructed from the sap, start, and end.
643 ;;;
644 (defun string-from-sap (sap start end)
645 (declare (type index start end))
646 (let* ((length (- end start))
647 (string (make-string length)))
648 (copy-from-system-area sap (* start vm:byte-bits)
649 string (* vm:vector-data-offset vm:word-bits)
650 (* length vm:byte-bits))
651 string))
652
653 #|
654 ;;; FD-STREAM-READ-N-BYTES -- internal
655 ;;;
656 ;;; This version waits using server. I changed to the non-server version
657 ;;; because it allows this method to be used by CLX w/o confusing serve-event.
658 ;;; The non-server method is also significantly more efficient for large
659 ;;; reads. -- Ram
660 ;;;
661 ;;; The n-bin routine.
662 ;;;
663 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
664 (declare (type stream stream) (type index start requested))
665 (let* ((sap (fd-stream-ibuf-sap stream))
666 (elsize (fd-stream-element-size stream))
667 (offset (* elsize start))
668 (bytes (* elsize requested))
669 (result
670 (catch 'eof-input-catcher
671 (loop
672 (input-at-least stream 1)
673 (let* ((head (fd-stream-ibuf-head stream))
674 (tail (fd-stream-ibuf-tail stream))
675 (available (- tail head))
676 (copy (min available bytes)))
677 (if (typep buffer 'system-area-pointer)
678 (system-area-copy sap (* head vm:byte-bits)
679 buffer (* offset vm:byte-bits)
680 (* copy vm:byte-bits))
681 (copy-from-system-area sap (* head vm:byte-bits)
682 buffer (+ (* offset vm:byte-bits)
683 (* vm:vector-data-offset
684 vm:word-bits))
685 (* copy vm:byte-bits)))
686 (incf (fd-stream-ibuf-head stream) copy)
687 (incf offset copy)
688 (decf bytes copy))
689 (when (zerop bytes)
690 (return requested))))))
691 (or result
692 (eof-or-lose stream eof-error-p
693 (- requested (/ bytes elsize))))))
694 |#
695
696
697 ;;; FD-STREAM-READ-N-BYTES -- internal
698 ;;;
699 ;;; The N-Bin method for FD-STREAMs. This doesn't use the SERVER; it blocks
700 ;;; in UNIX-READ. This allows the method to be used to implementing reading
701 ;;; for CLX. It is generally used where there is a definite amount of reading
702 ;;; to be done, so blocking isn't too problematical.
703 ;;;
704 ;;; We copy buffered data into the buffer. If there is enough, just return.
705 ;;; Otherwise, we see if the amount of additional data needed will fit in the
706 ;;; stream buffer. If not, inhibit GCing (so we can have a SAP into the Buffer
707 ;;; argument), and read directly into the user supplied buffer. Otherwise,
708 ;;; read a buffer-full into the stream buffer and then copy the amount we need
709 ;;; out.
710 ;;;
711 ;;; We loop doing the reads until we either get enough bytes or hit EOF. We
712 ;;; must loop when eof-errorp is T because some streams (like pipes) may return
713 ;;; a partial amount without hitting EOF.
714 ;;;
715 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
716 (declare (type stream stream) (type index start requested))
717 (let* ((sap (fd-stream-ibuf-sap stream))
718 (offset start)
719 (head (fd-stream-ibuf-head stream))
720 (tail (fd-stream-ibuf-tail stream))
721 (available (- tail head))
722 (copy (min requested available)))
723 (declare (type index offset head tail available copy))
724 (unless (zerop copy)
725 (if (typep buffer 'system-area-pointer)
726 (system-area-copy sap (* head vm:byte-bits)
727 buffer (* offset vm:byte-bits)
728 (* copy vm:byte-bits))
729 (copy-from-system-area sap (* head vm:byte-bits)
730 buffer (+ (* offset vm:byte-bits)
731 (* vm:vector-data-offset
732 vm:word-bits))
733 (* copy vm:byte-bits)))
734 (incf (fd-stream-ibuf-head stream) copy))
735 (cond
736 ((or (= copy requested)
737 (and (not eof-error-p) (/= copy 0)))
738 copy)
739 (t
740 (setf (fd-stream-ibuf-head stream) 0)
741 (setf (fd-stream-ibuf-tail stream) 0)
742 (setf (fd-stream-listen stream) nil)
743 (let ((now-needed (- requested copy))
744 (len (fd-stream-ibuf-length stream)))
745 (declare (type index now-needed len))
746 (cond
747 ((> now-needed len)
748 ;;
749 ;; If the desired amount is greater than the stream buffer size, then
750 ;; read directly into the destination, incrementing the start
751 ;; accordingly. In this case, we never leave anything in the stream
752 ;; buffer.
753 (system:without-gcing
754 (loop
755 (multiple-value-bind
756 (count err)
757 (unix:unix-read (fd-stream-fd stream)
758 (sap+ (if (typep buffer 'system-area-pointer)
759 buffer
760 (vector-sap buffer))
761 (+ offset copy))
762 now-needed)
763 (declare (type (or index null) count))
764 (unless count
765 (error "Error reading ~S: ~A" stream
766 (unix:get-unix-error-msg err)))
767 (decf now-needed count)
768 (if eof-error-p
769 (when (zerop count)
770 (error 'end-of-file :stream stream))
771 (return (- requested now-needed)))
772 (when (zerop now-needed) (return requested))
773 (incf offset count)))))
774 (t
775 ;;
776 ;; If we want less than the buffer size, then loop trying to fill the
777 ;; stream buffer and copying what we get into the destination. When
778 ;; we have enough, we leave what's left in the stream buffer.
779 (loop
780 (multiple-value-bind
781 (count err)
782 (unix:unix-read (fd-stream-fd stream) sap len)
783 (declare (type (or index null) count))
784 (unless count
785 (error "Error reading ~S: ~A" stream
786 (unix:get-unix-error-msg err)))
787 (when (and eof-error-p (zerop count))
788 (error 'end-of-file :stream stream))
789
790 (let* ((copy (min now-needed count))
791 (copy-bits (* copy vm:byte-bits))
792 (buffer-start-bits
793 (* (+ offset available) vm:byte-bits)))
794 (declare (type index copy copy-bits buffer-start-bits))
795 (if (typep buffer 'system-area-pointer)
796 (system-area-copy sap 0
797 buffer buffer-start-bits
798 copy-bits)
799 (copy-from-system-area sap 0
800 buffer (+ buffer-start-bits
801 (* vm:vector-data-offset
802 vm:word-bits))
803 copy-bits))
804
805 (decf now-needed copy)
806 (when (or (zerop now-needed) (not eof-error-p))
807 (setf (fd-stream-ibuf-head stream) copy)
808 (setf (fd-stream-ibuf-tail stream) count)
809 (return (- requested now-needed)))
810 (incf offset copy)))))))))))
811
812
813 ;;;; Utility functions (misc routines, etc)
814
815 ;;; SET-ROUTINES -- internal
816 ;;;
817 ;;; Fill in the various routine slots for the given type. Input-p and
818 ;;; output-p indicate what slots to fill. The buffering slot must be set prior
819 ;;; to calling this routine.
820 ;;;
821 (defun set-routines (stream type input-p output-p buffer-p)
822 (let ((target-type (case type
823 ((:default unsigned-byte)
824 '(unsigned-byte 8))
825 (signed-byte
826 '(signed-byte 8))
827 (t
828 type)))
829 (input-type nil)
830 (output-type nil)
831 (input-size nil)
832 (output-size nil))
833
834 (when (fd-stream-obuf-sap stream)
835 (push (fd-stream-obuf-sap stream) *available-buffers*)
836 (setf (fd-stream-obuf-sap stream) nil))
837 (when (fd-stream-ibuf-sap stream)
838 (push (fd-stream-ibuf-sap stream) *available-buffers*)
839 (setf (fd-stream-ibuf-sap stream) nil))
840
841 (when input-p
842 (multiple-value-bind
843 (routine type size)
844 (pick-input-routine target-type)
845 (unless routine
846 (error "Could not find any input routine for ~S" target-type))
847 (setf (fd-stream-ibuf-sap stream) (next-available-buffer))
848 (setf (fd-stream-ibuf-length stream) bytes-per-buffer)
849 (setf (fd-stream-ibuf-tail stream) 0)
850 (if (subtypep type 'character)
851 (setf (fd-stream-in stream) routine
852 (fd-stream-bin stream) #'ill-bin)
853 (setf (fd-stream-in stream) #'ill-in
854 (fd-stream-bin stream) routine))
855 (when (or (eql size 1)
856 (eql size 2)
857 (eql size 4))
858 ;; Support for n-byte operations on 8-, 16-, and 32-bit streams
859 (setf (fd-stream-n-bin stream) #'fd-stream-read-n-bytes)
860 (when (and buffer-p (eql size 1)
861 (or (eq type 'unsigned-byte)
862 (eq type :default)))
863 ;; We only create this buffer for streams of type
864 ;; (unsigned-byte 8). Because there's no buffer, the
865 ;; other element-types will dispatch to the appropriate
866 ;; input (output) routine in fast-read-byte.
867 (setf (lisp-stream-in-buffer stream)
868 (make-array in-buffer-length
869 :element-type '(unsigned-byte 8)))))
870 (setf input-size size)
871 (setf input-type type)))
872
873 (when output-p
874 (multiple-value-bind
875 (routine type size)
876 (pick-output-routine target-type (fd-stream-buffering stream))
877 (unless routine
878 (error "Could not find any output routine for ~S buffered ~S."
879 (fd-stream-buffering stream)
880 target-type))
881 (setf (fd-stream-obuf-sap stream) (next-available-buffer))
882 (setf (fd-stream-obuf-length stream) bytes-per-buffer)
883 (setf (fd-stream-obuf-tail stream) 0)
884 (if (subtypep type 'character)
885 (setf (fd-stream-out stream) routine
886 (fd-stream-bout stream) #'ill-bout)
887 (setf (fd-stream-out stream)
888 (or (if (eql size 1)
889 (pick-output-routine 'base-char
890 (fd-stream-buffering stream)))
891 #'ill-out)
892 (fd-stream-bout stream) routine))
893 (setf (fd-stream-sout stream)
894 (if (eql size 1) #'fd-sout #'ill-out))
895 (setf (fd-stream-char-pos stream) 0)
896 (setf output-size size)
897 (setf output-type type)))
898
899 (when (and input-size output-size
900 (not (eq input-size output-size)))
901 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
902 input-type input-size
903 output-type output-size))
904 (setf (fd-stream-element-size stream)
905 (or input-size output-size))
906
907 (setf (fd-stream-element-type stream)
908 (cond ((equal input-type output-type)
909 input-type)
910 ((null output-type)
911 input-type)
912 ((null input-type)
913 output-type)
914 ((subtypep input-type output-type)
915 input-type)
916 ((subtypep output-type input-type)
917 output-type)
918 (t
919 (error "Input type (~S) and output type (~S) are unrelated?"
920 input-type
921 output-type))))))
922
923 ;;; FD-STREAM-MISC-ROUTINE -- input
924 ;;;
925 ;;; Handle the various misc operations on fd-stream.
926 ;;;
927 (defun fd-stream-misc-routine (stream operation &optional arg1 arg2)
928 (declare (ignore arg2))
929 (case operation
930 (:listen
931 (or (not (eql (fd-stream-ibuf-head stream)
932 (fd-stream-ibuf-tail stream)))
933 (fd-stream-listen stream)
934 (setf (fd-stream-listen stream)
935 (eql (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
936 (unix:fd-zero read-fds)
937 (unix:fd-set (fd-stream-fd stream) read-fds)
938 (unix:unix-fast-select (1+ (fd-stream-fd stream))
939 (alien:addr read-fds) nil nil
940 0 0))
941 1))))
942 (:unread
943 (setf (fd-stream-unread stream) arg1)
944 (setf (fd-stream-listen stream) t))
945 (:close
946 (cond (arg1
947 ;; We got us an abort on our hands.
948 (when (fd-stream-handler stream)
949 (system:remove-fd-handler (fd-stream-handler stream))
950 (setf (fd-stream-handler stream) nil))
951 (when (and (fd-stream-file stream)
952 (fd-stream-obuf-sap stream))
953 ;; Can't do anything unless we know what file were dealing with,
954 ;; and we don't want to do anything strange unless we were
955 ;; writing to the file.
956 (if (fd-stream-original stream)
957 ;; Have an handle on the original, just revert.
958 (multiple-value-bind
959 (okay err)
960 (unix:unix-rename (fd-stream-original stream)
961 (fd-stream-file stream))
962 (unless okay
963 (cerror "Go on as if nothing bad happened."
964 "Could not restore ~S to it's original contents: ~A"
965 (fd-stream-file stream)
966 (unix:get-unix-error-msg err))))
967 ;; Can't restore the orignal, so nuke that puppy.
968 (multiple-value-bind
969 (okay err)
970 (unix:unix-unlink (fd-stream-file stream))
971 (unless okay
972 (cerror "Go on as if nothing bad happened."
973 "Could not remove ~S: ~A"
974 (fd-stream-file stream)
975 (unix:get-unix-error-msg err)))))))
976 (t
977 (fd-stream-misc-routine stream :finish-output)
978 (when (and (fd-stream-original stream)
979 (fd-stream-delete-original stream))
980 (multiple-value-bind
981 (okay err)
982 (unix:unix-unlink (fd-stream-original stream))
983 (unless okay
984 (cerror "Go on as if nothing bad happened."
985 "Could not delete ~S during close of ~S: ~A"
986 (fd-stream-original stream)
987 stream
988 (unix:get-unix-error-msg err)))))))
989 (when (fboundp 'cancel-finalization)
990 (cancel-finalization stream))
991 (unix:unix-close (fd-stream-fd stream))
992 (when (fd-stream-obuf-sap stream)
993 (push (fd-stream-obuf-sap stream) *available-buffers*)
994 (setf (fd-stream-obuf-sap stream) nil))
995 (when (fd-stream-ibuf-sap stream)
996 (push (fd-stream-ibuf-sap stream) *available-buffers*)
997 (setf (fd-stream-ibuf-sap stream) nil))
998 (lisp::set-closed-flame stream))
999 (:clear-input
1000 (setf (fd-stream-unread stream) nil)
1001 (setf (fd-stream-ibuf-head stream) 0)
1002 (setf (fd-stream-ibuf-tail stream) 0)
1003 (catch 'eof-input-catcher
1004 (loop
1005 (multiple-value-bind
1006 (count errno)
1007 (alien:with-alien ((read-fds (alien:struct unix:fd-set)))
1008 (unix:fd-zero read-fds)
1009 (unix:fd-set (fd-stream-fd stream) read-fds)
1010 (unix:unix-fast-select (1+ (fd-stream-fd stream))
1011 (alien:addr read-fds) nil nil 0 0))
1012 (cond ((eql count 1)
1013 (do-input stream)
1014 (setf (fd-stream-ibuf-head stream) 0)
1015 (setf (fd-stream-ibuf-tail stream) 0))
1016 ((and (not count) (eql errno unix:eintr)))
1017 (t
1018 (return t)))))))
1019 (:force-output
1020 (flush-output-buffer stream))
1021 (:finish-output
1022 (flush-output-buffer stream)
1023 (do ()
1024 ((null (fd-stream-output-later stream)))
1025 (system:serve-all-events)))
1026 (:element-type
1027 (fd-stream-element-type stream))
1028 (:interactive-p
1029 (unix:unix-isatty (fd-stream-fd stream)))
1030 (:line-length
1031 80)
1032 (:charpos
1033 (fd-stream-char-pos stream))
1034 (:file-length
1035 (unless (fd-stream-file stream)
1036 (error 'simple-type-error
1037 :datum stream
1038 :expected-type 'file-stream
1039 :format-control "~s is not a stream associated with a file."
1040 :format-arguments (list stream)))
1041 (multiple-value-bind
1042 (okay dev ino mode nlink uid gid rdev size
1043 atime mtime ctime blksize blocks)
1044 (unix:unix-fstat (fd-stream-fd stream))
1045 (declare (ignore ino nlink uid gid rdev
1046 atime mtime ctime blksize blocks))
1047 (unless okay
1048 (error "Error fstating ~S: ~A"
1049 stream
1050 (unix:get-unix-error-msg dev)))
1051 (if (zerop mode)
1052 nil
1053 (truncate size (fd-stream-element-size stream)))))
1054 (:file-position
1055 (fd-stream-file-position stream arg1))))
1056
1057
1058 ;;; FD-STREAM-FILE-POSITION -- internal.
1059 ;;;
1060 (defun fd-stream-file-position (stream &optional newpos)
1061 (declare (type fd-stream stream)
1062 (type (or (integer 0) (member nil :start :end)) newpos))
1063 (if (null newpos)
1064 (system:without-interrupts
1065 ;; First, find the position of the UNIX file descriptor in the file.
1066 (multiple-value-bind
1067 (posn errno)
1068 (unix:unix-lseek (fd-stream-fd stream) 0 unix:l_incr)
1069 (declare (type (or (integer 0) null) posn))
1070 (cond (posn
1071 ;; Adjust for buffered output:
1072 ;; If there is any output buffered, the *real* file position
1073 ;; will be larger than reported by lseek because lseek
1074 ;; obviously cannot take< into account output we have not
1075 ;; sent yet.
1076 (dolist (later (fd-stream-output-later stream))
1077 (incf posn (- (the index (caddr later))
1078 (the index (cadr later)))))
1079 (incf posn (fd-stream-obuf-tail stream))
1080 ;; Adjust for unread input:
1081 ;; If there is any input read from UNIX but not supplied to
1082 ;; the user of the stream, the *real* file position will
1083 ;; smaller than reported, because we want to look like the
1084 ;; unread stuff is still available.
1085 (decf posn (- (fd-stream-ibuf-tail stream)
1086 (fd-stream-ibuf-head stream)))
1087 (when (fd-stream-unread stream)
1088 (decf posn))
1089 ;; Divide bytes by element size.
1090 (truncate posn (fd-stream-element-size stream)))
1091 ((eq errno unix:espipe)
1092 nil)
1093 (t
1094 (system:with-interrupts
1095 (error "Error lseek'ing ~S: ~A"
1096 stream
1097 (unix:get-unix-error-msg errno)))))))
1098 (let ((offset 0)
1099 origin)
1100 (declare (type (integer 0) offset))
1101 ;; Make sure we don't have any output pending, because if we move the
1102 ;; file pointer before writing this stuff, it will be written in the
1103 ;; wrong location.
1104 (flush-output-buffer stream)
1105 (do ()
1106 ((null (fd-stream-output-later stream)))
1107 (system:serve-all-events))
1108 ;; Clear out any pending input to force the next read to go to the
1109 ;; disk.
1110 (setf (fd-stream-unread stream) nil)
1111 (setf (fd-stream-ibuf-head stream) 0)
1112 (setf (fd-stream-ibuf-tail stream) 0)
1113 ;; Trash cached value for listen, so that we check next time.
1114 (setf (fd-stream-listen stream) nil)
1115 ;; Now move it.
1116 (cond ((eq newpos :start)
1117 (setf offset 0
1118 origin unix:l_set))
1119 ((eq newpos :end)
1120 (setf offset 0
1121 origin unix:l_xtnd))
1122 ((typep newpos '(integer 0))
1123 (setf offset (* newpos (fd-stream-element-size stream))
1124 origin unix:l_set))
1125 (t
1126 (error "Invalid position given to file-position: ~S" newpos)))
1127 (multiple-value-bind
1128 (posn errno)
1129 (unix:unix-lseek (fd-stream-fd stream) offset origin)
1130 (cond (posn
1131 t)
1132 ((eq errno unix:espipe)
1133 nil)
1134 (t
1135 (error "Error lseek'ing ~S: ~A"
1136 stream
1137 (unix:get-unix-error-msg errno))))))))
1138
1139
1140
1141 ;;;; Creation routines (MAKE-FD-STREAM and OPEN)
1142
1143 ;;; MAKE-FD-STREAM -- Public.
1144 ;;;
1145 ;;; Returns a FD-STREAM on the given file.
1146 ;;;
1147 (defun make-fd-stream (fd
1148 &key
1149 (input nil input-p)
1150 (output nil output-p)
1151 (element-type 'base-char)
1152 (buffering :full)
1153 timeout
1154 file
1155 original
1156 delete-original
1157 pathname
1158 input-buffer-p
1159 (name (if file
1160 (format nil "file ~S" file)
1161 (format nil "descriptor ~D" fd)))
1162 auto-close)
1163 (declare (type index fd) (type (or index null) timeout)
1164 (type (member :none :line :full) buffering))
1165 "Create a stream for the given unix file descriptor.
1166 If input is non-nil, allow input operations.
1167 If output is non-nil, allow output operations.
1168 If neither input nor output are specified, default to allowing input.
1169 Element-type indicates the element type to use (as for open).
1170 Buffering indicates the kind of buffering to use.
1171 Timeout (if true) is the number of seconds to wait for input. If NIL (the
1172 default), then wait forever. When we time out, we signal IO-TIMEOUT.
1173 File is the name of the file (will be returned by PATHNAME).
1174 Name is used to identify the stream when printed."
1175 (cond ((not (or input-p output-p))
1176 (setf input t))
1177 ((not (or input output))
1178 (error "File descriptor must be opened either for input or output.")))
1179 (let ((stream (%make-fd-stream :fd fd
1180 :name name
1181 :file file
1182 :original original
1183 :delete-original delete-original
1184 :pathname pathname
1185 :buffering buffering
1186 :timeout timeout)))
1187 (set-routines stream element-type input output input-buffer-p)
1188 (when (and auto-close (fboundp 'finalize))
1189 (finalize stream
1190 #'(lambda ()
1191 (unix:unix-close fd)
1192 (format *terminal-io* "** Closed file descriptor ~D~%"
1193 fd))))
1194 stream))
1195
1196
1197 ;;; PICK-PACKUP-NAME -- internal
1198 ;;;
1199 ;;; Pick a name to use for the backup file.
1200 ;;;
1201 (defvar *backup-extension* ".BAK"
1202 "This is a string that OPEN tacks on the end of a file namestring to produce
1203 a name for the :if-exists :rename-and-delete and :rename options. Also,
1204 this can be a function that takes a namestring and returns a complete
1205 namestring.")
1206 ;;;
1207 (defun pick-backup-name (name)
1208 (declare (type simple-string name))
1209 (let ((ext *backup-extension*))
1210 (etypecase ext
1211 (simple-string (concatenate 'simple-string name ext))
1212 (function (funcall ext name)))))
1213
1214 ;;; ASSURE-ONE-OF -- internal
1215 ;;;
1216 ;;; Assure that the given arg is one of the given list of valid things.
1217 ;;; Allow the user to fix any problems.
1218 ;;;
1219 (defun assure-one-of (item list what)
1220 (unless (member item list)
1221 (loop
1222 (cerror "Enter new value for ~*~S"
1223 "~S is invalid for ~S. Must be one of~{ ~S~}"
1224 item
1225 what
1226 list)
1227 (format (the stream *query-io*) "Enter new value for ~S: " what)
1228 (force-output *query-io*)
1229 (setf item (read *query-io*))
1230 (when (member item list)
1231 (return))))
1232 item)
1233
1234 ;;; DO-OLD-RENAME -- Internal
1235 ;;;
1236 ;;; Rename Namestring to Original. First, check if we have write access,
1237 ;;; since we don't want to trash unwritable files even if we technically can.
1238 ;;; We return true if we suceed in renaming.
1239 ;;;
1240 (defun do-old-rename (namestring original)
1241 (unless (unix:unix-access namestring unix:w_ok)
1242 (cerror "Try to rename it anyway." "File ~S is not writable." namestring))
1243 (multiple-value-bind
1244 (okay err)
1245 (unix:unix-rename namestring original)
1246 (cond (okay t)
1247 (t
1248 (cerror "Use :SUPERSEDE instead."
1249 "Could not rename ~S to ~S: ~A."
1250 namestring
1251 original
1252 (unix:get-unix-error-msg err))
1253 nil))))
1254
1255 ;;; RETURN-STREAM -- internal
1256 ;;;
1257 ;;; (this is just to save having to reindent the code in OPEN...move it there)
1258 ;;;
1259 (defmacro return-stream (class &body body)
1260 (let ((stream (gensym)))
1261 `(let ((,stream (progn ,@body)))
1262 (return (if ,class
1263 (make-instance ,class :lisp-stream ,stream)
1264 ,stream)))))
1265
1266 ;;; OPEN -- public
1267 ;;;
1268 ;;; Open the given file.
1269 ;;;
1270 (defun open (filename
1271 &key
1272 (direction :input)
1273 (element-type 'base-char)
1274 (if-exists nil if-exists-given)
1275 (if-does-not-exist nil if-does-not-exist-given)
1276 (external-format :default)
1277 class
1278 &aux ; Squelch assignment warning.
1279 (direction direction)
1280 (if-does-not-exist if-does-not-exist)
1281 (if-exists if-exists))
1282 "Return a stream which reads from or writes to Filename.
1283 Defined keywords:
1284 :direction - one of :input, :output, :io, or :probe
1285 :element-type - Type of object to read or write, default BASE-CHAR
1286 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
1287 :overwrite, :append, :supersede or nil
1288 :if-does-not-exist - one of :error, :create or nil
1289 See the manual for details."
1290 (declare (ignore external-format))
1291
1292 ;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
1293 (setf direction
1294 (assure-one-of direction
1295 '(:input :output :io :probe)
1296 :direction))
1297
1298 ;; Calculate useful stuff.
1299 (multiple-value-bind
1300 (input output mask)
1301 (case direction
1302 (:input (values t nil unix:o_rdonly))
1303 (:output (values nil t unix:o_wronly))
1304 (:io (values t t unix:o_rdwr))
1305 (:probe (values t nil unix:o_rdonly)))
1306 (declare (type index mask))
1307 (let* ((pathname (pathname filename))
1308 (namestring
1309 (cond ((unix-namestring pathname input))
1310 ((and input (eq if-does-not-exist :create))
1311 (unix-namestring pathname nil)))))
1312 ;; Process if-exists argument if we are doing any output.
1313 (cond (output
1314 (unless if-exists-given
1315 (setf if-exists
1316 (if (eq (pathname-version pathname) :newest)
1317 :new-version
1318 :error)))
1319 (setf if-exists
1320 (assure-one-of if-exists
1321 '(:error :new-version :rename
1322 :rename-and-delete :overwrite
1323 :append :supersede nil)
1324 :if-exists))
1325 (case if-exists
1326 ((:error nil)
1327 (setf mask (logior mask unix:o_excl)))
1328 ((:rename :rename-and-delete)
1329 (setf mask (logior mask unix:o_creat)))
1330 ((:new-version :supersede)
1331 (setf mask (logior mask unix:o_trunc)))
1332 (:append
1333 (setf mask (logior mask unix:o_append)))))
1334 (t
1335 (setf if-exists :ignore-this-arg)))
1336
1337 (unless if-does-not-exist-given
1338 (setf if-does-not-exist
1339 (cond ((eq direction :input) :error)
1340 ((and output
1341 (member if-exists '(:overwrite :append)))
1342 :error)
1343 ((eq direction :probe)
1344 nil)
1345 (t
1346 :create))))
1347 (setf if-does-not-exist
1348 (assure-one-of if-does-not-exist
1349 '(:error :create nil)
1350 :if-does-not-exist))
1351 (if (eq if-does-not-exist :create)
1352 (setf mask (logior mask unix:o_creat)))
1353
1354 (let ((original (if (member if-exists
1355 '(:rename :rename-and-delete))
1356 (pick-backup-name namestring)))
1357 (delete-original (eq if-exists :rename-and-delete))
1358 (mode #o666))
1359 (when original
1360 ;; We are doing a :rename or :rename-and-delete.
1361 ;; Determine if the file already exists, make sure the original
1362 ;; file is not a directory and keep the mode
1363 (let ((exists
1364 (and namestring
1365 (multiple-value-bind
1366 (okay err/dev inode orig-mode)
1367 (unix:unix-stat namestring)
1368 (declare (ignore inode)
1369 (type (or index null) orig-mode))
1370 (cond
1371 (okay
1372 (when (and output (= (logand orig-mode #o170000)
1373 #o40000))
1374 (error "Cannot open ~S for output: Is a directory."
1375 namestring))
1376 (setf mode (logand orig-mode #o777))
1377 t)
1378 ((eql err/dev unix:enoent)
1379 nil)
1380 (t
1381 (error "Cannot find ~S: ~A"
1382 namestring
1383 (unix:get-unix-error-msg err/dev))))))))
1384 (unless (and exists
1385 (do-old-rename namestring original))
1386 (setf original nil)
1387 (setf delete-original nil)
1388 ;; In order to use SUPERSEDE instead, we have
1389 ;; to make sure unix:o_creat corresponds to
1390 ;; if-does-not-exist. unix:o_creat was set
1391 ;; before because of if-exists being :rename.
1392 (unless (eq if-does-not-exist :create)
1393 (setf mask (logior (logandc2 mask unix:o_creat) unix:o_trunc)))
1394 (setf if-exists :supersede))))
1395
1396 ;; Okay, now we can try the actual open.
1397 (loop
1398 (multiple-value-bind
1399 (fd errno)
1400 (if namestring
1401 (unix:unix-open namestring mask mode)
1402 (values nil unix:enoent))
1403 (cond ((numberp fd)
1404 (return-stream class
1405 (case direction
1406 ((:input :output :io)
1407 (make-fd-stream fd
1408 :input input
1409 :output output
1410 :element-type element-type
1411 :file namestring
1412 :original original
1413 :delete-original delete-original
1414 :pathname pathname
1415 :input-buffer-p t
1416 :auto-close t))
1417 (:probe
1418 (let ((stream
1419 (%make-fd-stream :name namestring :fd fd
1420 :pathname pathname
1421 :element-type element-type)))
1422 (close stream)
1423 stream)))))
1424 ((eql errno unix:enoent)
1425 (case if-does-not-exist
1426 (:error
1427 (cerror "Return NIL."
1428 'simple-file-error
1429 :pathname pathname
1430 :format-control "Error opening ~S, ~A."
1431 :format-arguments
1432 (list pathname (unix:get-unix-error-msg errno))))
1433 (:create
1434 (cerror "Return NIL."
1435 "Error creating ~S, path does not exist."
1436 pathname)))
1437 (return nil))
1438 ((eql errno unix:eexist)
1439 (unless (eq nil if-exists)
1440 (cerror "Return NIL."
1441 'simple-file-error
1442 :pathname pathname
1443 :format-control "Error opening ~S, ~A."
1444 :format-arguments
1445 (list pathname (unix:get-unix-error-msg errno))))
1446 (return nil))
1447 ((eql errno unix:eacces)
1448 (cerror "Try again."
1449 "Error opening ~S, ~A."
1450 pathname
1451 (unix:get-unix-error-msg errno)))
1452 (t
1453 (cerror "Return NIL."
1454 "Error opening ~S, ~A."
1455 pathname
1456 (unix:get-unix-error-msg errno))
1457 (return nil)))))))))
1458
1459 ;;;; Initialization.
1460
1461 (defvar *tty* nil
1462 "The stream connected to the controlling terminal or NIL if there is none.")
1463 (defvar *stdin* nil
1464 "The stream connected to the standard input (file descriptor 0).")
1465 (defvar *stdout* nil
1466 "The stream connected to the standard output (file descriptor 1).")
1467 (defvar *stderr* nil
1468 "The stream connected to the standard error output (file descriptor 2).")
1469
1470 ;;; STREAM-INIT -- internal interface
1471 ;;;
1472 ;;; Called when the cold load is first started up.
1473 ;;;
1474 (defun stream-init ()
1475 (stream-reinit)
1476 (setf *terminal-io* (make-synonym-stream '*tty*))
1477 (setf *standard-output* (make-synonym-stream '*stdout*))
1478 (setf *standard-input*
1479 (make-two-way-stream (make-synonym-stream '*stdin*)
1480 *standard-output*))
1481 (setf *error-output* (make-synonym-stream '*stderr*))
1482 (setf *query-io* (make-synonym-stream '*terminal-io*))
1483 (setf *debug-io* *query-io*)
1484 (setf *trace-output* *standard-output*)
1485 nil)
1486
1487 ;;; STREAM-REINIT -- internal interface
1488 ;;;
1489 ;;; Called whenever a saved core is restarted.
1490 ;;;
1491 (defun stream-reinit ()
1492 (setf *available-buffers* nil)
1493 (setf *stdin*
1494 (make-fd-stream 0 :name "Standard Input" :input t :buffering :line))
1495 (setf *stdout*
1496 (make-fd-stream 1 :name "Standard Output" :output t :buffering :line))
1497 (setf *stderr*
1498 (make-fd-stream 2 :name "Standard Error" :output t :buffering :line))
1499 (let ((tty (and (not *batch-mode*)
1500 (unix:unix-open "/dev/tty" unix:o_rdwr #o666))))
1501 (setf *tty*
1502 (if tty
1503 (make-fd-stream tty :name "the Terminal" :input t :output t
1504 :buffering :line :auto-close t)
1505 (make-two-way-stream *stdin* *stdout*))))
1506 nil)
1507
1508
1509 ;;;; Beeping.
1510
1511 (defun default-beep-function (stream)
1512 (write-char #\bell stream)
1513 (finish-output stream))
1514
1515 (defvar *beep-function* #'default-beep-function
1516 "This is called in BEEP to feep the user. It takes a stream.")
1517
1518 (defun beep (&optional (stream *terminal-io*))
1519 (funcall *beep-function* stream))
1520
1521
1522 ;;; File-Name -- internal interface
1523 ;;;
1524 ;;; Kind of like File-Position, but is an internal hack used by the filesys
1525 ;;; stuff to get and set the file name.
1526 ;;;
1527 (defun file-name (stream &optional new-name)
1528 (when (typep stream 'fd-stream)
1529 (cond (new-name
1530 (setf (fd-stream-pathname stream) new-name)
1531 (setf (fd-stream-file stream)
1532 (unix-namestring new-name nil))
1533 t)
1534 (t
1535 (fd-stream-pathname stream)))))
1536
1537
1538 ;;;; Degenerate international character support:
1539
1540 (defun file-string-length (stream object)
1541 (declare (type (or string character) object) (type file-stream stream))
1542 "Return the delta in Stream's FILE-POSITION that would be caused by writing
1543 Object to Stream. Non-trivial only in implementations that support
1544 international character sets."
1545 (declare (ignore stream))
1546 (etypecase object
1547 (character 1)
1548 (string (length object))))
1549
1550 (defun stream-external-format (stream)
1551 (declare (type file-stream stream) (ignore stream))
1552 "Returns :DEFAULT."
1553 :default)

  ViewVC Help
Powered by ViewVC 1.1.5