/[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.16 - (show annotations)
Sat Nov 9 02:47:09 1991 UTC (22 years, 5 months ago) by wlott
Branch: MAIN
Changes since 1.15: +6 -6 lines
Changed BASE-CHARACTER to BASE-CHAR
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.16 1991/11/09 02:47:09 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 (mach:unix-write (fd-stream-fd stream)
142 base
143 start
144 length)
145 (cond ((not count)
146 (if (= errno mach:ewouldblock)
147 (error "Write would have blocked, but SERVER told us to go.")
148 (error "While writing ~S: ~A"
149 stream (mach: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 (mach:unix-write (fd-stream-fd stream) base start length)
203 (cond ((not count)
204 (if (= errno mach:ewouldblock)
205 (output-later stream base start end reuse-sap)
206 (error "While writing ~S: ~A"
207 stream (mach: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)
292 (truncate (fd-stream-obuf-tail stream) 2))
293 byte))
294
295 (def-output-routines ("OUTPUT-LONG-~A-BUFFERED"
296 4
297 (:none (signed-byte 32) (unsigned-byte 32))
298 (:full (signed-byte 32) (unsigned-byte 32)))
299 (setf (sap-ref-32 (fd-stream-obuf-sap stream)
300 (truncate (fd-stream-obuf-tail stream) 4))
301 byte))
302
303 ;;; OUTPUT-RAW-BYTES -- public
304 ;;;
305 ;;; Does the actual output. If there is space to buffer the string, buffer
306 ;;; it. If the string would normally fit in the buffer, but doesn't because
307 ;;; of other stuff in the buffer, flush the old noise out of the buffer and
308 ;;; put the string in it. Otherwise we have a very long string, so just
309 ;;; send it directly (after flushing the buffer, of course).
310 ;;;
311 (defun output-raw-bytes (stream thing &optional start end)
312 "Output THING to stream. THING can be any kind of vector or a sap. If THING
313 is a SAP, END must be supplied (as length won't work)."
314 (let ((start (or start 0))
315 (end (or end (length (the (simple-array * (*)) thing)))))
316 (declare (type index start end))
317 (let* ((len (fd-stream-obuf-length stream))
318 (tail (fd-stream-obuf-tail stream))
319 (space (- len tail))
320 (bytes (- end start))
321 (newtail (+ tail bytes)))
322 (cond ((minusp bytes) ; Error case
323 (cerror "Just go on as if nothing happened..."
324 "~S called with :END before :START!"
325 'output-raw-bytes))
326 ((zerop bytes)) ; Easy case
327 ((<= bytes space)
328 (if (system-area-pointer-p thing)
329 (system-area-copy thing
330 (* start vm:byte-bits)
331 (fd-stream-obuf-sap stream)
332 (* tail vm:byte-bits)
333 (* bytes vm:byte-bits))
334 (copy-to-system-area thing
335 (+ (* start vm:byte-bits)
336 (* vm:vector-data-offset vm:word-bits))
337 (fd-stream-obuf-sap stream)
338 (* tail vm:byte-bits)
339 (* bytes vm:byte-bits)))
340 (setf (fd-stream-obuf-tail stream) newtail))
341 ((<= bytes len)
342 (flush-output-buffer stream)
343 (if (system-area-pointer-p thing)
344 (system-area-copy thing
345 (* start vm:byte-bits)
346 (fd-stream-obuf-sap stream)
347 0
348 (* bytes vm:byte-bits))
349 (copy-to-system-area thing
350 (+ (* start vm:byte-bits)
351 (* vm:vector-data-offset vm:word-bits))
352 (fd-stream-obuf-sap stream)
353 0
354 (* bytes vm:byte-bits)))
355 (setf (fd-stream-obuf-tail stream) bytes))
356 (t
357 (flush-output-buffer stream)
358 (do-output stream thing start end nil))))))
359
360 ;;; FD-SOUT -- internal
361 ;;;
362 ;;; Routine to use to output a string. If the stream is unbuffered, slam
363 ;;; the string down the file descriptor, otherwise use OUTPUT-RAW-BYTES to
364 ;;; buffer the string. Update charpos by checking to see where the last newline
365 ;;; was.
366 ;;;
367 ;;; Note: some bozos (the FASL dumper) call write-string with things other
368 ;;; than strings. Therefore, we must make sure we have a string before calling
369 ;;; position on it.
370 ;;;
371 (defun fd-sout (stream thing start end)
372 (let ((start (or start 0))
373 (end (or end (length (the vector thing)))))
374 (declare (fixnum start end))
375 (if (stringp thing)
376 (let ((last-newline (and (find #\newline (the simple-string thing)
377 :start start :end end)
378 (position #\newline (the simple-string thing)
379 :from-end t
380 :start start
381 :end end))))
382 (ecase (fd-stream-buffering stream)
383 (:full
384 (output-raw-bytes stream thing start end))
385 (:line
386 (output-raw-bytes stream thing start end)
387 (when last-newline
388 (flush-output-buffer stream)))
389 (:none
390 (do-output stream thing start end nil)))
391 (if last-newline
392 (setf (fd-stream-char-pos stream)
393 (- end last-newline 1))
394 (incf (fd-stream-char-pos stream)
395 (- end start))))
396 (ecase (fd-stream-buffering stream)
397 ((:line :full)
398 (output-raw-bytes stream thing start end))
399 (:none
400 (do-output stream thing start end nil))))))
401
402 ;;; PICK-OUTPUT-ROUTINE -- internal
403 ;;;
404 ;;; Find an output routine to use given the type and buffering. Return as
405 ;;; multiple values the routine, the real type transfered, and the number of
406 ;;; bytes per element.
407 ;;;
408 (defun pick-output-routine (type buffering)
409 (dolist (entry *output-routines*)
410 (when (and (subtypep type (car entry))
411 (eq buffering (cadr entry)))
412 (return (values (symbol-function (caddr entry))
413 (car entry)
414 (cadddr entry))))))
415
416
417 ;;;; Input routines and related noise.
418
419 (defvar *input-routines* ()
420 "List of all available input routines. Each element is a list of the
421 element-type input, the function name, and the number of bytes per element.")
422
423 ;;; DO-INPUT -- internal
424 ;;;
425 ;;; Fills the input buffer, and returns the first character. Throws to
426 ;;; eof-input-catcher if the eof was reached. Drops into system:server if
427 ;;; necessary.
428 ;;;
429 (defun do-input (stream)
430 (let ((fd (fd-stream-fd stream))
431 (ibuf-sap (fd-stream-ibuf-sap stream))
432 (buflen (fd-stream-ibuf-length stream))
433 (head (fd-stream-ibuf-head stream))
434 (tail (fd-stream-ibuf-tail stream)))
435 (declare (type index head tail))
436 (unless (zerop head)
437 (cond ((eql head tail)
438 (setf head 0)
439 (setf tail 0)
440 (setf (fd-stream-ibuf-head stream) 0)
441 (setf (fd-stream-ibuf-tail stream) 0))
442 (t
443 (decf tail head)
444 (system-area-copy ibuf-sap (* head vm:byte-bits)
445 ibuf-sap 0 (* tail vm:byte-bits))
446 (setf head 0)
447 (setf (fd-stream-ibuf-head stream) 0)
448 (setf (fd-stream-ibuf-tail stream) tail))))
449 (setf (fd-stream-listen stream) nil)
450 #+serve-event
451 (multiple-value-bind
452 (count errno)
453 (mach:unix-select (1+ fd) (ash 1 fd) 0 0 0)
454 (case count
455 (1)
456 (0
457 (unless (system:wait-until-fd-usable
458 fd :input (fd-stream-timeout stream))
459 (error 'io-timeout :stream stream :direction :read)))
460 (t
461 (error "Problem checking to see if ~S is readable: ~A"
462 stream
463 (mach:get-unix-error-msg errno)))))
464 (multiple-value-bind
465 (count errno)
466 (mach:unix-read fd
467 (system:int-sap (+ (system:sap-int ibuf-sap) tail))
468 (- buflen tail))
469 (cond ((null count)
470 (if #+serve-event (eql errno mach:ewouldblock)
471 #-serve-event nil
472 (progn
473 (unless (system:wait-until-fd-usable
474 fd :input (fd-stream-timeout stream))
475 (error 'io-timeout :stream stream :direction :read))
476 (do-input stream))
477 (error "Error reading ~S: ~A"
478 stream
479 (mach:get-unix-error-msg errno))))
480 ((zerop count)
481 (setf (fd-stream-listen stream) :eof)
482 (throw 'eof-input-catcher nil))
483 (t
484 (incf (fd-stream-ibuf-tail stream) count))))))
485
486 ;;; INPUT-AT-LEAST -- internal
487 ;;;
488 ;;; Makes sure there are at least ``bytes'' number of bytes in the input
489 ;;; buffer. Keeps calling do-input until that condition is met.
490 ;;;
491 (defmacro input-at-least (stream bytes)
492 (let ((stream-var (gensym))
493 (bytes-var (gensym)))
494 `(let ((,stream-var ,stream)
495 (,bytes-var ,bytes))
496 (loop
497 (when (>= (- (fd-stream-ibuf-tail ,stream-var)
498 (fd-stream-ibuf-head ,stream-var))
499 ,bytes-var)
500 (return))
501 (do-input ,stream-var)))))
502
503 ;;; INPUT-WRAPPER -- intenal
504 ;;;
505 ;;; Macro to wrap around all input routines to handle eof-error noise. This
506 ;;; should make provisions for filling stream-in-buffer.
507 ;;;
508 (defmacro input-wrapper ((stream bytes eof-error eof-value) &body read-forms)
509 (let ((stream-var (gensym))
510 (element-var (gensym)))
511 `(let ((,stream-var ,stream))
512 (if (fd-stream-unread ,stream)
513 (prog1
514 (fd-stream-unread ,stream)
515 (setf (fd-stream-unread ,stream) nil)
516 (setf (fd-stream-listen ,stream) nil))
517 (let ((,element-var
518 (catch 'eof-input-catcher
519 (input-at-least ,stream-var ,bytes)
520 ,@read-forms)))
521 (cond (,element-var
522 (incf (fd-stream-ibuf-head ,stream-var) ,bytes)
523 ,element-var)
524 (,eof-error
525 (error "EOF while reading ~S" stream))
526 (t
527 ,eof-value)))))))
528
529 ;;; DEF-INPUT-ROUTINE -- internal
530 ;;;
531 ;;; Defines an input routine.
532 ;;;
533 (defmacro def-input-routine (name
534 (type size sap head)
535 &rest body)
536 `(progn
537 (defun ,name (stream eof-error eof-value)
538 (input-wrapper (stream ,size eof-error eof-value)
539 (let ((,sap (fd-stream-ibuf-sap stream))
540 (,head (fd-stream-ibuf-head stream)))
541 ,@body)))
542 (setf *input-routines*
543 (nconc *input-routines*
544 (list (list ',type ',name ',size))))))
545
546 ;;; INPUT-CHARACTER -- internal
547 ;;;
548 ;;; Routine to use in stream-in slot for reading string chars.
549 ;;;
550 (def-input-routine input-character
551 (character 1 sap head)
552 (code-char (sap-ref-8 sap head)))
553
554 ;;; INPUT-UNSIGNED-8BIT-BYTE -- internal
555 ;;;
556 ;;; Routine to read in an unsigned 8 bit number.
557 ;;;
558 (def-input-routine input-unsigned-8bit-byte
559 ((unsigned-byte 8) 1 sap head)
560 (sap-ref-8 sap head))
561
562 ;;; INPUT-SIGNED-8BIT-BYTE -- internal
563 ;;;
564 ;;; Routine to read in a signed 8 bit number.
565 ;;;
566 (def-input-routine input-signed-8bit-number
567 ((signed-byte 8) 1 sap head)
568 (signed-sap-ref-8 sap head))
569
570 ;;; INPUT-UNSIGNED-16BIT-BYTE -- internal
571 ;;;
572 ;;; Routine to read in an unsigned 16 bit number.
573 ;;;
574 (def-input-routine input-unsigned-16bit-byte
575 ((unsigned-byte 16) 2 sap head)
576 (sap-ref-16 sap (truncate head 2)))
577
578 ;;; INPUT-SIGNED-16BIT-BYTE -- internal
579 ;;;
580 ;;; Routine to read in a signed 16 bit number.
581 ;;;
582 (def-input-routine input-signed-16bit-byte
583 ((signed-byte 16) 2 sap head)
584 (signed-sap-ref-16 sap (truncate head 2)))
585
586 ;;; INPUT-UNSIGNED-32BIT-BYTE -- internal
587 ;;;
588 ;;; Routine to read in a unsigned 32 bit number.
589 ;;;
590 (def-input-routine input-unsigned-32bit-byte
591 ((unsigned-byte 32) 4 sap head)
592 (sap-ref-32 sap (truncate head 4)))
593
594 ;;; INPUT-SIGNED-32BIT-BYTE -- internal
595 ;;;
596 ;;; Routine to read in a signed 32 bit number.
597 ;;;
598 (def-input-routine input-signed-32bit-byte
599 ((signed-byte 32) 4 sap head)
600 (signed-sap-ref-32 sap (truncate head 2)))
601
602 ;;; PICK-INPUT-ROUTINE -- internal
603 ;;;
604 ;;; Find an input routine to use given the type. Return as multiple values
605 ;;; the routine, the real type transfered, and the number of bytes per element.
606 ;;;
607 (defun pick-input-routine (type)
608 (dolist (entry *input-routines*)
609 (when (subtypep type (car entry))
610 (return (values (symbol-function (cadr entry))
611 (car entry)
612 (caddr entry))))))
613
614 ;;; STRING-FROM-SAP -- internal
615 ;;;
616 ;;; Returns a string constructed from the sap, start, and end.
617 ;;;
618 (defun string-from-sap (sap start end)
619 (declare (type index start end))
620 (let* ((length (- end start))
621 (string (make-string length)))
622 (copy-from-system-area sap (* start vm:byte-bits)
623 string (* vm:vector-data-offset vm:word-bits)
624 (* length vm:byte-bits))
625 string))
626
627 ;;; FD-STREAM-READ-LINE -- internal
628 ;;;
629 ;;; Reads a line, returning a simple string. Note: this relies on the fact
630 ;;; that the input buffer does not change during do-input.
631 ;;;
632 (defun fd-stream-read-line (stream eof-error-p eof-value)
633 (let ((eof t))
634 (values
635 (or (let ((sap (fd-stream-ibuf-sap stream))
636 (results (when (fd-stream-unread stream)
637 (prog1
638 (list (string (fd-stream-unread stream)))
639 (setf (fd-stream-unread stream) nil)
640 (setf (fd-stream-listen stream) nil)))))
641 (catch 'eof-input-catcher
642 (loop
643 (input-at-least stream 1)
644 (let* ((head (fd-stream-ibuf-head stream))
645 (tail (fd-stream-ibuf-tail stream))
646 (newline (do ((index head (1+ index)))
647 ((= index tail) nil)
648 (when (= (sap-ref-8 sap index)
649 (char-code #\newline))
650 (return index))))
651 (end (or newline tail)))
652 (push (string-from-sap sap head end)
653 results)
654
655 (when newline
656 (setf eof nil)
657 (setf (fd-stream-ibuf-head stream)
658 (1+ newline))
659 (return))
660 (setf (fd-stream-ibuf-head stream) end))))
661 (cond ((null results)
662 nil)
663 ((null (cdr results))
664 (car results))
665 (t
666 (apply #'concatenate 'simple-string (nreverse results)))))
667 (if eof-error-p
668 (error "EOF while reading ~S" stream)
669 eof-value))
670 eof)))
671
672 #|
673 This version waits using server. I changed to the non-server version because
674 it allows this method to be used by CLX w/o confusing serve-event. The
675 non-server method is also significantly more efficient for large reads.
676 -- Ram
677
678 ;;; FD-STREAM-READ-N-BYTES -- internal
679 ;;;
680 ;;; The n-bin routine.
681 ;;;
682 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
683 (let* ((sap (fd-stream-ibuf-sap stream))
684 (elsize (fd-stream-element-size stream))
685 (offset (* elsize start))
686 (bytes (* elsize requested))
687 (result
688 (catch 'eof-input-catcher
689 (loop
690 (input-at-least stream 1)
691 (let* ((head (fd-stream-ibuf-head stream))
692 (tail (fd-stream-ibuf-tail stream))
693 (available (- tail head))
694 (copy (min available bytes)))
695 (if (typep buffer 'system-area-pointer)
696 (system-area-copy sap (* head vm:byte-bits)
697 buffer (* offset vm:byte-bits)
698 (* copy vm:byte-bits))
699 (copy-from-system-area sap (* head vm:byte-bits)
700 buffer (+ (* offset vm:byte-bits)
701 (* vm:vector-data-offset
702 vm:word-bits))
703 (* copy vm:byte-bits)))
704 (incf (fd-stream-ibuf-head stream) copy)
705 (incf offset copy)
706 (decf bytes copy))
707 (when (zerop bytes)
708 (return requested))))))
709 (cond (result)
710 ((not eof-error-p)
711 (- requested (/ bytes elsize)))
712 (t
713 (error "Hit eof on ~S after reading ~D ~D~2:*-bit byte~P~*, ~
714 but ~D~2:* ~D-bit byte~P~:* ~[were~;was~:;were~] requested."
715 stream
716 (- requested (/ bytes elsize))
717 (* elsize 8)
718 requested)))))
719 |#
720
721
722 ;;; FD-STREAM-READ-N-BYTES -- internal
723 ;;;
724 ;;; The N-Bin method for FD-STREAMs. This doesn't using SERVER; it blocks
725 ;;; in UNIX-READ. This allows the method to be used to implementing reading
726 ;;; for CLX. It is generally used where there is a definite amount of reading
727 ;;; to be done, so blocking isn't too problematical.
728 ;;;
729 ;;; We copy buffered data into the buffer. If there is enough, just return.
730 ;;; Otherwise, we see if the amount of additional data needed will fit in the
731 ;;; stream buffer. If not, inhibit GCing (so we can have a SAP into the Buffer
732 ;;; argument), and read directly into the user supplied buffer. Otherwise,
733 ;;; read a buffer-full into the stream buffer and then copy the amount we need
734 ;;; out.
735 ;;;
736 ;;; We loop doing the reads until we either get enough bytes or hit EOF. We
737 ;;; must loop because some streams (like pipes) may return a partial amount
738 ;;; without hitting EOF.
739 ;;;
740 (defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
741 (declare (type stream stream) (type index start requested))
742 (let* ((sap (fd-stream-ibuf-sap stream))
743 (elsize (fd-stream-element-size stream))
744 (offset (* elsize start))
745 (requested-bytes (* elsize requested))
746 (head (fd-stream-ibuf-head stream))
747 (tail (fd-stream-ibuf-tail stream))
748 (available (- tail head))
749 (copy (min requested-bytes available)))
750 (declare (type index elsize offset requested-bytes head tail available
751 copy))
752 (unless (zerop copy)
753 (if (typep buffer 'system-area-pointer)
754 (system-area-copy sap (* head vm:byte-bits)
755 buffer (* offset vm:byte-bits)
756 (* copy vm:byte-bits))
757 (copy-from-system-area sap (* head vm:byte-bits)
758 buffer (+ (* offset vm:byte-bits)
759 (* vm:vector-data-offset
760 vm:word-bits))
761 (* copy vm:byte-bits)))
762 (incf (fd-stream-ibuf-head stream) copy))
763 (cond
764 ((> requested-bytes available)
765 (setf (fd-stream-ibuf-head stream) 0)
766 (setf (fd-stream-ibuf-tail stream) 0)
767 (setf (fd-stream-listen stream) nil)
768 (let ((now-needed (- requested-bytes copy))
769 (len (fd-stream-ibuf-length stream)))
770 (declare (type index now-needed len))
771 (cond
772 ((> now-needed len)
773 (system:without-gcing
774 (loop
775 (multiple-value-bind
776 (count err)
777 (mach:unix-read (fd-stream-fd stream)
778 (sap+ (if (typep buffer 'system-area-pointer)
779 buffer
780 (vector-sap buffer))
781 (+ offset copy))
782 now-needed)
783 (declare (type (or index null) count))
784 (unless count
785 (error "Error reading ~S: ~A" stream
786 (mach:get-unix-error-msg err)))
787 (when (zerop count)
788 (if eof-error-p
789 (error "Unexpected eof on ~S." stream)
790 (return (- requested (truncate now-needed elsize)))))
791 (decf now-needed count)
792 (when (zerop now-needed) (return requested))
793 (incf offset count)))))
794 (t
795 (loop
796 (multiple-value-bind
797 (count err)
798 (mach:unix-read (fd-stream-fd stream) sap len)
799 (declare (type (or index null) count))
800 (unless count
801 (error "Error reading ~S: ~A" stream
802 (mach:get-unix-error-msg err)))
803 (incf (fd-stream-ibuf-tail stream) count)
804 (when (zerop count)
805 (if eof-error-p
806 (error "Unexpected eof on ~S." stream)
807 (return (- requested (truncate now-needed elsize)))))
808 (let* ((copy (min now-needed count))
809 (copy-bits (* copy vm:byte-bits))
810 (buffer-start-bits
811 (* (+ offset available) vm:byte-bits)))
812 (declare (type index copy copy-bits buffer-start-bits))
813 (if (typep buffer 'system-area-pointer)
814 (system-area-copy sap 0
815 buffer buffer-start-bits
816 copy-bits)
817 (copy-from-system-area sap 0
818 buffer (+ buffer-start-bits
819 (* vm:vector-data-offset
820 vm:word-bits))
821 copy-bits))
822 (incf (fd-stream-ibuf-head stream) copy)
823 (decf now-needed copy)
824 (when (zerop now-needed) (return requested))
825 (incf offset copy))))))))
826 (t
827 requested))))
828
829
830 ;;;; Utility functions (misc routines, etc)
831
832 ;;; SET-ROUTINES -- internal
833 ;;;
834 ;;; Fill in the various routine slots for the given type. Input-p and output-p
835 ;;; indicate what slots to fill. The buffering slot must be set prior to
836 ;;; calling this routine.
837 ;;;
838 (defun set-routines (stream type input-p output-p)
839 (let ((target-type (case type
840 ((:default unsigned-byte)
841 '(unsigned-byte 8))
842 (signed-byte
843 '(signed-byte 8))
844 (t
845 type)))
846 (input-type nil)
847 (output-type nil)
848 (input-size nil)
849 (output-size nil))
850
851 (when (fd-stream-obuf-sap stream)
852 (push (fd-stream-obuf-sap stream) *available-buffers*)
853 (setf (fd-stream-obuf-sap stream) nil))
854 (when (fd-stream-ibuf-sap stream)
855 (push (fd-stream-ibuf-sap stream) *available-buffers*)
856 (setf (fd-stream-ibuf-sap stream) nil))
857
858 (when input-p
859 (multiple-value-bind
860 (routine type size)
861 (pick-input-routine target-type)
862 (unless routine
863 (error "Could not find any input routine for ~S" target-type))
864 (setf (fd-stream-ibuf-sap stream) (next-available-buffer))
865 (setf (fd-stream-ibuf-length stream) bytes-per-buffer)
866 (setf (fd-stream-ibuf-tail stream) 0)
867 (if (subtypep type 'character)
868 (setf (fd-stream-in stream) routine
869 (fd-stream-bin stream) #'ill-bin
870 (fd-stream-n-bin stream) #'ill-bin)
871 (setf (fd-stream-in stream) #'ill-in
872 (fd-stream-bin stream) routine
873 (fd-stream-n-bin stream) #'fd-stream-read-n-bytes))
874 (setf input-size size)
875 (setf input-type type)))
876
877 (when output-p
878 (multiple-value-bind
879 (routine type size)
880 (pick-output-routine target-type (fd-stream-buffering stream))
881 (unless routine
882 (error "Could not find any output routine for ~S buffered ~S."
883 (fd-stream-buffering stream)
884 target-type))
885 (setf (fd-stream-obuf-sap stream) (next-available-buffer))
886 (setf (fd-stream-obuf-length stream) bytes-per-buffer)
887 (setf (fd-stream-obuf-tail stream) 0)
888 (if (subtypep type 'character)
889 (setf (fd-stream-out stream) routine
890 (fd-stream-bout stream) #'ill-bout)
891 (setf (fd-stream-out stream)
892 (or (if (eql size 1)
893 (pick-output-routine 'base-char
894 (fd-stream-buffering stream)))
895 #'ill-out)
896 (fd-stream-bout stream) routine))
897 (setf (fd-stream-sout stream)
898 (if (eql size 1) #'fd-sout #'ill-out))
899 (setf (fd-stream-char-pos stream) 0)
900 (setf output-size size)
901 (setf output-type type)))
902
903 (when (and input-size output-size
904 (not (eq input-size output-size)))
905 (error "Element sizes for input (~S:~S) and output (~S:~S) differ?"
906 input-type input-size
907 output-type output-size))
908 (setf (fd-stream-element-size stream)
909 (or input-size output-size))
910
911 (setf (fd-stream-element-type stream)
912 (cond ((equal input-type output-type)
913 input-type)
914 ((or (null output-type) (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 (case operation
929 (:read-line
930 (fd-stream-read-line stream arg1 arg2))
931 (:listen
932 (or (not (eql (fd-stream-ibuf-head stream)
933 (fd-stream-ibuf-tail stream)))
934 (fd-stream-listen stream)
935 (setf (fd-stream-listen stream)
936 (eql (mach:unix-select (1+ (fd-stream-fd stream))
937 (ash 1 (fd-stream-fd stream))
938 0
939 0
940 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 (and (fd-stream-file stream)
949 (fd-stream-obuf-sap stream))
950 ;; Can't do anything unless we know what file were dealing with,
951 ;; and we don't want to do anything strange unless we were
952 ;; writing to the file.
953 (if (fd-stream-original stream)
954 ;; Have an handle on the original, just revert.
955 (multiple-value-bind
956 (okay err)
957 (mach:unix-rename (fd-stream-original stream)
958 (fd-stream-file stream))
959 (unless okay
960 (cerror "Go on as if nothing bad happened."
961 "Could not restore ~S to it's original contents: ~A"
962 (fd-stream-file stream)
963 (mach:get-unix-error-msg err))))
964 ;; Can't restore the orignal, so nuke that puppy.
965 (multiple-value-bind
966 (okay err)
967 (mach:unix-unlink (fd-stream-file stream))
968 (unless okay
969 (cerror "Go on as if nothing bad happened."
970 "Could not remove ~S: ~A"
971 (fd-stream-file stream)
972 (mach:get-unix-error-msg err)))))))
973 (t
974 (fd-stream-misc-routine stream :finish-output)
975 (when (and (fd-stream-original stream)
976 (fd-stream-delete-original stream))
977 (multiple-value-bind
978 (okay err)
979 (mach:unix-unlink (fd-stream-original stream))
980 (unless okay
981 (cerror "Go on as if nothing bad happened."
982 "Could not delete ~S during close of ~S: ~A"
983 (fd-stream-original stream)
984 stream
985 (mach:get-unix-error-msg err)))))))
986 (mach:unix-close (fd-stream-fd stream))
987 (when (fd-stream-obuf-sap stream)
988 (push (fd-stream-obuf-sap stream) *available-buffers*)
989 (setf (fd-stream-obuf-sap stream) nil))
990 (when (fd-stream-ibuf-sap stream)
991 (push (fd-stream-ibuf-sap stream) *available-buffers*)
992 (setf (fd-stream-ibuf-sap stream) nil))
993 (lisp::set-closed-flame stream))
994 (:clear-input
995 (setf (fd-stream-ibuf-head stream) 0)
996 (setf (fd-stream-ibuf-tail stream) 0)
997 (loop
998 (let ((count (mach:unix-select (1+ (fd-stream-fd stream))
999 (ash 1 (fd-stream-fd stream))
1000 0 0 0)))
1001 (cond ((eql count 1)
1002 (do-input stream)
1003 (setf (fd-stream-ibuf-head stream) 0)
1004 (setf (fd-stream-ibuf-tail stream) 0))
1005 (t
1006 (return))))))
1007 (:force-output
1008 (flush-output-buffer stream))
1009 (:finish-output
1010 (flush-output-buffer stream)
1011 #+serve-event
1012 (do ()
1013 ((null (fd-stream-output-later stream)))
1014 (system:serve-all-events)))
1015 (:element-type
1016 (fd-stream-element-type stream))
1017 (:line-length
1018 80)
1019 (:charpos
1020 (fd-stream-char-pos stream))
1021 (:file-length
1022 (multiple-value-bind
1023 (okay dev ino mode nlink uid gid rdev size
1024 atime mtime ctime blksize blocks)
1025 (mach:unix-fstat (fd-stream-fd stream))
1026 (declare (ignore ino nlink uid gid rdev
1027 atime mtime ctime blksize blocks))
1028 (unless okay
1029 (error "Error fstating ~S: ~A"
1030 stream
1031 (mach:get-unix-error-msg dev)))
1032 (if (zerop mode)
1033 nil
1034 (truncate size (fd-stream-element-size stream)))))
1035 (:file-position
1036 (fd-stream-file-position stream arg1))
1037 (:file-name
1038 (fd-stream-file stream))))
1039
1040 ;;; FD-STREAM-FILE-POSITION -- internal.
1041 ;;;
1042 (defun fd-stream-file-position (stream &optional newpos)
1043 (declare (type fd-stream stream)
1044 (type (or index (member nil :start :end)) newpos))
1045 (if (null newpos)
1046 (system:without-interrupts
1047 ;; First, find the position of the UNIX file descriptor in the
1048 ;; file.
1049 (multiple-value-bind
1050 (posn errno)
1051 (mach:unix-lseek (fd-stream-fd stream) 0 mach:l_incr)
1052 (declare (type (or index null) posn))
1053 (cond ((fixnump posn)
1054 ;; Adjust for buffered output:
1055 ;; If there is any output buffered, the *real* file position
1056 ;; will be larger than reported by lseek because lseek
1057 ;; obviously cannot take into account output we have not
1058 ;; sent yet.
1059 (dolist (later (fd-stream-output-later stream))
1060 (incf posn (- (the index (caddr later))
1061 (the index (cadr later)))))
1062 (incf posn (fd-stream-obuf-tail stream))
1063 ;; Adjust for unread input:
1064 ;; If there is any input read from UNIX but not supplied to
1065 ;; the user of the stream, the *real* file position will
1066 ;; smaller than reported, because we want to look like the
1067 ;; unread stuff is still available.
1068 (decf posn (- (fd-stream-ibuf-tail stream)
1069 (fd-stream-ibuf-head stream)))
1070 (when (fd-stream-unread stream)
1071 (decf posn))
1072 ;; Divide bytes by element size.
1073 (truncate posn (fd-stream-element-size stream)))
1074 ((eq errno mach:espipe)
1075 nil)
1076 (t
1077 (system:with-interrupts
1078 (error "Error lseek'ing ~S: ~A"
1079 stream
1080 (mach:get-unix-error-msg errno)))))))
1081 (let (offset origin)
1082 ;; Make sure we don't have any output pending, because if we move the
1083 ;; file pointer before writing this stuff, it will be written in the
1084 ;; wrong location.
1085 (flush-output-buffer stream)
1086 (do ()
1087 ((null (fd-stream-output-later stream)))
1088 (system:serve-all-events))
1089 ;; Clear out any pending input to force the next read to go to the
1090 ;; disk.
1091 (setf (fd-stream-unread stream) nil)
1092 (setf (fd-stream-ibuf-head stream) 0)
1093 (setf (fd-stream-ibuf-tail stream) 0)
1094 ;; Trash cashed value for listen, so that we check next time.
1095 (setf (fd-stream-listen stream) nil)
1096 ;; Now move it.
1097 (cond ((eq newpos :start)
1098 (setf offset 0 origin mach:l_set))
1099 ((eq newpos :end)
1100 (setf offset 0 origin mach:l_xtnd))
1101 ((typep newpos 'index)
1102 (setf offset (* newpos (fd-stream-element-size stream))
1103 origin mach:l_set))
1104 (t
1105 (error "Invalid position given to file-position: ~S" newpos)))
1106 (multiple-value-bind
1107 (posn errno)
1108 (mach:unix-lseek (fd-stream-fd stream) offset origin)
1109 (cond ((typep posn 'fixnum)
1110 t)
1111 ((eq errno mach:espipe)
1112 nil)
1113 (t
1114 (error "Error lseek'ing ~S: ~A"
1115 stream
1116 (mach:get-unix-error-msg errno))))))))
1117
1118
1119
1120 ;;;; Creation routines (MAKE-FD-STREAM and OPEN)
1121
1122 ;;; MAKE-FD-STREAM -- Public.
1123 ;;;
1124 ;;; Returns a FD-STREAM on the given file.
1125 ;;;
1126 (defun make-fd-stream (fd
1127 &key
1128 (input nil input-p)
1129 (output nil output-p)
1130 (element-type 'base-char)
1131 (buffering :full)
1132 timeout
1133 file
1134 original
1135 delete-original
1136 (name (if file
1137 (format nil "file ~S" file)
1138 (format nil "descriptor ~D" fd))))
1139 (declare (type index fd) (type (or index null) timeout)
1140 (type (member :none :line :full) buffering))
1141 "Create a stream for the given unix file descriptor.
1142 If input is non-nil, allow input operations.
1143 If output is non-nil, allow output operations.
1144 If neither input nor output are specified, default to allowing input.
1145 Element-type indicates the element type to use (as for open).
1146 Buffering indicates the kind of buffering to use.
1147 Timeout (if true) is the number of seconds to wait for input. If NIL (the
1148 default), then wait forever. When we time out, we signal IO-TIMEOUT.
1149 File is the name of the file (will be returned by PATHNAME).
1150 Name is used to identify the stream when printed."
1151 (cond ((not (or input-p output-p))
1152 (setf input t))
1153 ((not (or input output))
1154 (error "File descriptor must be opened either for input or output.")))
1155 (let ((stream (%make-fd-stream :fd fd
1156 :name name
1157 :file file
1158 :original original
1159 :delete-original delete-original
1160 :buffering buffering
1161 :timeout timeout)))
1162 (set-routines stream element-type input output)
1163 stream))
1164
1165 ;;; PICK-PACKUP-NAME -- internal
1166 ;;;
1167 ;;; Pick a name to use for the backup file.
1168 ;;;
1169 (defvar *backup-extension* ".BAK"
1170 "This is a string that OPEN tacks on the end of a file namestring to produce
1171 a name for the :if-exists :rename-and-delete and :rename options. Also,
1172 this can be a function that takes a namestring and returns a complete
1173 namestring.")
1174 ;;;
1175 (defun pick-backup-name (name)
1176 (declare (type simple-string name))
1177 (let ((ext *backup-extension*))
1178 (etypecase ext
1179 (simple-string (concatenate 'simple-string name ext))
1180 (function (funcall ext name)))))
1181
1182 ;;; ASSURE-ONE-OF -- internal
1183 ;;;
1184 ;;; Assure that the given arg is one of the given list of valid things.
1185 ;;; Allow the user to fix any problems.
1186 ;;;
1187 (defun assure-one-of (item list what)
1188 (unless (member item list)
1189 (loop
1190 (cerror "Enter new value for ~*~S"
1191 "~S is invalid for ~S. Must be one of~{ ~S~}"
1192 item
1193 what
1194 list)
1195 (format (the stream *query-io*) "Enter new value for ~S: " what)
1196 (force-output *query-io*)
1197 (setf item (read *query-io*))
1198 (when (member item list)
1199 (return))))
1200 item)
1201
1202 ;;; DO-OLD-RENAME -- Internal
1203 ;;;
1204 ;;; Rename Namestring to Original. First, check if we have write access,
1205 ;;; since we don't want to trash unwritable files even if we technically can.
1206 ;;; We return true if we suceed in renaming.
1207 ;;;
1208 (defun do-old-rename (namestring original)
1209 (unless (mach:unix-access namestring mach:w_ok)
1210 (cerror "Try to rename it anyway." "File ~S is not writable." namestring))
1211 (multiple-value-bind
1212 (okay err)
1213 (mach:unix-rename namestring original)
1214 (cond (okay t)
1215 (t
1216 (cerror "Use :SUPERSEDE instead."
1217 "Could not rename ~S to ~S: ~A."
1218 namestring
1219 original
1220 (mach:get-unix-error-msg err))
1221 nil))))
1222
1223
1224 ;;; OPEN -- public
1225 ;;;
1226 ;;; Open the given file.
1227 ;;;
1228 (defun open (filename
1229 &key
1230 (direction :input)
1231 (element-type 'base-char)
1232 (if-exists nil if-exists-given)
1233 (if-does-not-exist nil if-does-not-exist-given))
1234 "Return a stream which reads from or writes to Filename.
1235 Defined keywords:
1236 :direction - one of :input, :output, :io, or :probe
1237 :element-type - Type of object to read or write, default BASE-CHAR
1238 :if-exists - one of :error, :new-version, :rename, :rename-and-delete,
1239 :overwrite, :append, :supersede or nil
1240 :if-does-not-exist - one of :error, :create or nil
1241 See the manual for details."
1242 ;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
1243 (setf direction
1244 (assure-one-of direction
1245 '(:input :output :io :probe)
1246 :direction))
1247
1248 ;; Calculate useful stuff.
1249 (multiple-value-bind
1250 (input output mask)
1251 (case direction
1252 (:input (values t nil mach:o_rdonly))
1253 (:output (values nil t mach:o_wronly))
1254 (:io (values t t mach:o_rdwr))
1255 (:probe (values t nil mach:o_rdonly)))
1256 (declare (type index mask))
1257 (let* ((pathname (pathname filename))
1258 (namestring (unix-namestring pathname input)))
1259 ;; Process if-exists argument if we are doing any output.
1260 (cond (output
1261 (unless if-exists-given
1262 (setf if-exists
1263 (if (eq (pathname-version pathname) :newest)
1264 :new-version
1265 :error)))
1266 (setf if-exists
1267 (assure-one-of if-exists
1268 '(:error :new-version :rename
1269 :rename-and-delete :overwrite
1270 :append :supersede nil)
1271 :if-exists))
1272 (case if-exists
1273 ((:error nil)
1274 (setf mask (logior mask mach:o_excl)))
1275 ((:rename :rename-and-delete)
1276 (setf mask (logior mask mach:o_creat)))
1277 ((:new-version :supersede)
1278 (setf mask (logior mask mach:o_trunc)))
1279 (:append
1280 (setf mask (logior mask mach:o_append)))))
1281 (t
1282 (setf if-exists :ignore-this-arg)))
1283
1284 (unless if-does-not-exist-given
1285 (setf if-does-not-exist
1286 (cond ((eq direction :input) :error)
1287 ((and output
1288 (member if-exists '(:overwrite :append)))
1289 :error)
1290 ((eq direction :probe)
1291 nil)
1292 (t
1293 :create))))
1294 (setf if-does-not-exist
1295 (assure-one-of if-does-not-exist
1296 '(:error :create nil)
1297 :if-does-not-exist))
1298 (if (eq if-does-not-exist :create)
1299 (setf mask (logior mask mach:o_creat)))
1300
1301 (let ((original (if (member if-exists
1302 '(:rename :rename-and-delete))
1303 (pick-backup-name namestring)))
1304 (delete-original (eq if-exists :rename-and-delete))
1305 (mode #o666))
1306 (when original
1307 ;; We are doing a :rename or :rename-and-delete.
1308 ;; Determine if the file already exists, make sure the original
1309 ;; file is not a directory and keep the mode
1310 (let ((exists
1311 (multiple-value-bind
1312 (okay err/dev inode orig-mode)
1313 (mach:unix-stat namestring)
1314 (declare (ignore inode) (type (or index null) orig-mode))
1315 (cond (okay
1316 (when (and output (= (logand orig-mode #o170000)
1317 #o40000))
1318 (error "Cannot open ~S for output: Is a directory."
1319 namestring))
1320 (setf mode (logand orig-mode #o777))
1321 t)
1322 ((eql err/dev mach:enoent)
1323 nil)
1324 (t
1325 (error "Cannot find ~S: ~A"
1326 namestring
1327 (mach:get-unix-error-msg err/dev)))))))
1328 (unless (and exists
1329 (do-old-rename namestring original))
1330 (setf original nil)
1331 (setf delete-original nil)
1332 ;; In order to use SUPERSEDE instead, we have
1333 ;; to make sure mach:o_creat corresponds to
1334 ;; if-does-not-exist. mach:o_creat was set
1335 ;; before because of if-exists being :rename.
1336 (unless (eq if-does-not-exist :create)
1337 (setf mask (logior (logandc2 mask mach:o_creat) mach:o_trunc)))
1338 (setf if-exists :supersede))))
1339
1340 ;; Okay, now we can try the actual open.
1341 (loop
1342 (multiple-value-bind
1343 (fd errno)
1344 (mach:unix-open namestring mask mode)
1345 (cond ((numberp fd)
1346 (return
1347 (case direction
1348 ((:input :output :io)
1349 (make-fd-stream fd
1350 :input input
1351 :output output
1352 :element-type element-type
1353 :file namestring
1354 :original original
1355 :delete-original delete-original))
1356 (:probe
1357 (let ((stream
1358 (%make-fd-stream :name namestring :fd fd
1359 :element-type element-type)))
1360 (close stream)
1361 stream)))))
1362 ((eql errno mach:enoent)
1363 (case if-does-not-exist
1364 (:error
1365 (cerror "Return NIL."
1366 "Error opening ~S, ~A."
1367 pathname
1368 (mach:get-unix-error-msg errno)))
1369 (:create
1370 (cerror "Return NIL."
1371 "Error creating ~S, path does not exist."
1372 pathname)))
1373 (return nil))
1374 ((eql errno mach:eexist)
1375 (unless (eq nil if-exists)
1376 (cerror "Return NIL."
1377 "Error opening ~S, ~A."
1378 pathname
1379 (mach:get-unix-error-msg errno)))
1380 (return nil))
1381 ((eql errno mach:eacces)
1382 (cerror "Try again."
1383 "Error opening ~S, ~A."
1384 pathname
1385 (mach:get-unix-error-msg errno)))
1386 (t
1387 (cerror "Return NIL."
1388 "Error opening ~S, ~A."
1389 pathname
1390 (mach:get-unix-error-msg errno))
1391 (return nil)))))))))
1392
1393 ;;;; Initialization.
1394
1395 (defvar *tty* nil
1396 "The stream connected to the controlling terminal or NIL if there is none.")
1397 (defvar *stdin* nil
1398 "The stream connected to the standard input (file descriptor 0).")
1399 (defvar *stdout* nil
1400 "The stream connected to the standard output (file descriptor 1).")
1401 (defvar *stderr* nil
1402 "The stream connected to the standard error output (file descriptor 2).")
1403
1404 ;;; STREAM-INIT -- internal interface
1405 ;;;
1406 ;;; Called when the cold load is first started up.
1407 ;;;
1408 (defun stream-init ()
1409 (stream-reinit)
1410 (setf *terminal-io* (make-synonym-stream '*tty*))
1411 (setf *standard-output* (make-synonym-stream '*stdout*))
1412 (setf *standard-input*
1413 (make-two-way-stream (make-synonym-stream '*stdin*)
1414 *standard-output*))
1415 (setf *error-output* (make-synonym-stream '*stderr*))
1416 (setf *query-io* (make-synonym-stream '*terminal-io*))
1417 (setf *debug-io* *query-io*)
1418 (setf *trace-output* *standard-output*)
1419 nil)
1420
1421 ;;; STREAM-REINIT -- internal interface
1422 ;;;
1423 ;;; Called whenever a saved core is restarted.
1424 ;;;
1425 (defun stream-reinit ()
1426 (setf *available-buffers* nil)
1427 (setf *stdin*
1428 (make-fd-stream 0 :name "Standard Input" :input t :buffering :line))
1429 (setf *stdout*
1430 (make-fd-stream 1 :name "Standard Output" :output t :buffering :line))
1431 (setf *stderr*
1432 (make-fd-stream 2 :name "Standard Error" :output t :buffering :line))
1433 (let ((tty (mach:unix-open "/dev/tty" mach:o_rdwr #o666)))
1434 (if tty
1435 (setf *tty*
1436 (make-fd-stream tty :name "the Terminal" :input t :output t
1437 :buffering :line))
1438 (setf *tty* (make-two-way-stream *stdin* *stdout*))))
1439 nil)
1440
1441
1442 ;;;; Beeping.
1443
1444 (defun default-beep-function (stream)
1445 (write-char #\bell stream)
1446 (finish-output stream))
1447
1448 (defvar *beep-function* #'default-beep-function
1449 "This is called in BEEP to feep the user. It takes a stream.")
1450
1451 (defun beep (&optional (stream *terminal-io*))
1452 (funcall *beep-function* stream))
1453
1454
1455 ;;;; File position and file length.
1456
1457 ;;; File-Position -- Public
1458 ;;;
1459 ;;; Call the misc method with the :file-position operation.
1460 ;;;
1461 (defun file-position (stream &optional position)
1462 "With one argument returns the current position within the file
1463 File-Stream is open to. If the second argument is supplied, then
1464 this becomes the new file position. The second argument may also
1465 be :start or :end for the start and end of the file, respectively."
1466 (unless (streamp stream)
1467 (error "Argument ~S is not a stream." stream))
1468 (funcall (stream-misc stream) stream :file-position position))
1469
1470 ;;; File-Length -- Public
1471 ;;;
1472 ;;; Like File-Position, only use :file-length.
1473 ;;;
1474 (defun file-length (stream)
1475 "This function returns the length of the file that File-Stream is open to."
1476 (unless (streamp stream)
1477 (error "Argument ~S is not a stream." stream))
1478 (funcall (stream-misc stream) stream :file-length))
1479
1480 ;;; File-Name -- internal interface
1481 ;;;
1482 ;;; Kind of like File-Position, but is an internal hack used by the filesys
1483 ;;; stuff to get and set the file name.
1484 ;;;
1485 (defun file-name (stream &optional new-name)
1486 (when (fd-stream-p stream)
1487 (if new-name
1488 (setf (fd-stream-file stream) new-name)
1489 (fd-stream-file stream))))

  ViewVC Help
Powered by ViewVC 1.1.5