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

  ViewVC Help
Powered by ViewVC 1.1.5