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

  ViewVC Help
Powered by ViewVC 1.1.5