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

  ViewVC Help
Powered by ViewVC 1.1.5