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

  ViewVC Help
Powered by ViewVC 1.1.5