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

  ViewVC Help
Powered by ViewVC 1.1.5