Newer
Older
(ash 1 (fd-stream-fd stream))
0 0 0)))
(cond ((eql count 1)
(do-input stream)
(setf (fd-stream-ibuf-head stream) 0)
(setf (fd-stream-ibuf-tail stream) 0))
(t
(return))))))
(:force-output
(flush-output-buffer stream))
(:finish-output
(flush-output-buffer stream)
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
(do ()
((null (fd-stream-output-later stream)))
(system:serve-all-events)))
(:element-type
(fd-stream-element-type stream))
(:line-length
80)
(:charpos
(fd-stream-char-pos stream))
(:file-length
(multiple-value-bind
(okay dev ino mode nlink uid gid rdev size
atime mtime ctime blksize blocks)
(mach:unix-fstat (fd-stream-fd stream))
(declare (ignore ino nlink uid gid rdev
atime mtime ctime blksize blocks))
(unless okay
(error "Error fstating ~S: ~A"
stream
(mach:get-unix-error-msg dev)))
(if (zerop mode)
nil
(truncate size (fd-stream-element-size stream)))))
(:file-position
(fd-stream-file-position stream arg1))
(:file-name
(fd-stream-file stream))))
;;; FD-STREAM-FILE-POSITION -- internal.
;;;
(defun fd-stream-file-position (stream &optional newpos)
(declare (type fd-stream stream)
(type (or index (member nil :start :end)) newpos))
(if (null newpos)
(system:without-interrupts
;; First, find the position of the UNIX file descriptor in the
;; file.
(multiple-value-bind
(posn errno)
(mach:unix-lseek (fd-stream-fd stream) 0 mach:l_incr)
(declare (type (or index null) posn))
(cond ((fixnump posn)
;; Adjust for buffered output:
;; If there is any output buffered, the *real* file position
;; will be larger than reported by lseek because lseek
;; obviously cannot take into account output we have not
;; sent yet.
(dolist (later (fd-stream-output-later stream))
(incf posn (- (the index (caddr later))
(the index (cadr later)))))
(incf posn (fd-stream-obuf-tail stream))
;; Adjust for unread input:
;; If there is any input read from UNIX but not supplied to
;; the user of the stream, the *real* file position will
;; smaller than reported, because we want to look like the
;; unread stuff is still available.
(decf posn (- (fd-stream-ibuf-tail stream)
(fd-stream-ibuf-head stream)))
(when (fd-stream-unread stream)
(decf posn))
;; Divide bytes by element size.
(truncate posn (fd-stream-element-size stream)))
((eq errno mach:espipe)
nil)
(t
(system:with-interrupts
(error "Error lseek'ing ~S: ~A"
stream
(mach:get-unix-error-msg errno)))))))
(let (offset origin)
;; Make sure we don't have any output pending, because if we move the
;; file pointer before writing this stuff, it will be written in the
;; wrong location.
(flush-output-buffer stream)
(do ()
((null (fd-stream-output-later stream)))
(system:serve-all-events))
;; Clear out any pending input to force the next read to go to the
;; disk.
(setf (fd-stream-unread stream) nil)
(setf (fd-stream-ibuf-head stream) 0)
(setf (fd-stream-ibuf-tail stream) 0)
;; Trash cashed value for listen, so that we check next time.
(setf (fd-stream-listen stream) nil)
(cond ((eq newpos :start)
(setf offset 0 origin mach:l_set))
((eq newpos :end)
(setf offset 0 origin mach:l_xtnd))
(setf offset (* newpos (fd-stream-element-size stream))
origin mach:l_set))
(t
(error "Invalid position given to file-position: ~S" newpos)))
(multiple-value-bind
(posn errno)
(mach:unix-lseek (fd-stream-fd stream) offset origin)
(cond ((typep posn 'fixnum)
t)
((eq errno mach:espipe)
nil)
(t
(error "Error lseek'ing ~S: ~A"
stream
(mach:get-unix-error-msg errno))))))))
;;;; Creation routines (MAKE-FD-STREAM and OPEN)
;;; MAKE-FD-STREAM -- Public.
;;;
;;; Returns a FD-STREAM on the given file.
;;;
(defun make-fd-stream (fd
&key
(input nil input-p)
(output nil output-p)
timeout
file
original
delete-original
(name (if file
(format nil "file ~S" file)
(format nil "descriptor ~D" fd)))
auto-close)
(declare (type index fd) (type (or index null) timeout)
(type (member :none :line :full) buffering))
"Create a stream for the given unix file descriptor.
If input is non-nil, allow input operations.
If output is non-nil, allow output operations.
If neither input nor output are specified, default to allowing input.
Element-type indicates the element type to use (as for open).
Buffering indicates the kind of buffering to use.
Timeout (if true) is the number of seconds to wait for input. If NIL (the
default), then wait forever. When we time out, we signal IO-TIMEOUT.
File is the name of the file (will be returned by PATHNAME).
Name is used to identify the stream when printed."
(cond ((not (or input-p output-p))
(setf input t))
((not (or input output))
(error "File descriptor must be opened either for input or output.")))
(let ((stream (%make-fd-stream :fd fd
:name name
:file file
:original original
:delete-original delete-original
:buffering buffering
:timeout timeout)))
(when (and auto-close (fboundp 'finalize))
(finalize stream
#'(lambda ()
(mach:unix-close fd)
(format *terminal-io* "** Closed file descriptor ~D~%"
fd))))
stream))
;;; PICK-PACKUP-NAME -- internal
;;;
;;; Pick a name to use for the backup file.
;;;
(defvar *backup-extension* ".BAK"
"This is a string that OPEN tacks on the end of a file namestring to produce
a name for the :if-exists :rename-and-delete and :rename options. Also,
this can be a function that takes a namestring and returns a complete
namestring.")
;;;
(defun pick-backup-name (name)
(declare (type simple-string name))
(let ((ext *backup-extension*))
(etypecase ext
(simple-string (concatenate 'simple-string name ext))
(function (funcall ext name)))))
;;; ASSURE-ONE-OF -- internal
;;;
;;; Assure that the given arg is one of the given list of valid things.
;;; Allow the user to fix any problems.
;;;
(defun assure-one-of (item list what)
(unless (member item list)
(loop
(cerror "Enter new value for ~*~S"
"~S is invalid for ~S. Must be one of~{ ~S~}"
item
what
list)
(format (the stream *query-io*) "Enter new value for ~S: " what)
(force-output *query-io*)
(setf item (read *query-io*))
(when (member item list)
(return))))
item)
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
;;; DO-OLD-RENAME -- Internal
;;;
;;; Rename Namestring to Original. First, check if we have write access,
;;; since we don't want to trash unwritable files even if we technically can.
;;; We return true if we suceed in renaming.
;;;
(defun do-old-rename (namestring original)
(unless (mach:unix-access namestring mach:w_ok)
(cerror "Try to rename it anyway." "File ~S is not writable." namestring))
(multiple-value-bind
(okay err)
(mach:unix-rename namestring original)
(cond (okay t)
(t
(cerror "Use :SUPERSEDE instead."
"Could not rename ~S to ~S: ~A."
namestring
original
(mach:get-unix-error-msg err))
nil))))
;;; OPEN -- public
;;;
;;; Open the given file.
;;;
(defun open (filename
&key
(direction :input)
(if-exists nil if-exists-given)
(if-does-not-exist nil if-does-not-exist-given))
"Return a stream which reads from or writes to Filename.
Defined keywords:
:direction - one of :input, :output, :io, or :probe
:element-type - Type of object to read or write, default BASE-CHAR
:if-exists - one of :error, :new-version, :rename, :rename-and-delete,
:overwrite, :append, :supersede or nil
:if-does-not-exist - one of :error, :create or nil
See the manual for details."
;; First, make sure that DIRECTION is valid. Allow it to be changed if not.
(setf direction
(assure-one-of direction
'(:input :output :io :probe)
:direction))
;; Calculate useful stuff.
(multiple-value-bind
(input output mask)
(case direction
(:input (values t nil mach:o_rdonly))
(:output (values nil t mach:o_wronly))
(:io (values t t mach:o_rdwr))
(:probe (values t nil mach:o_rdonly)))
(declare (type index mask))
(namestring (unix-namestring pathname input)))
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
;; Process if-exists argument if we are doing any output.
(cond (output
(unless if-exists-given
(setf if-exists
(if (eq (pathname-version pathname) :newest)
:new-version
:error)))
(setf if-exists
(assure-one-of if-exists
'(:error :new-version :rename
:rename-and-delete :overwrite
:append :supersede nil)
:if-exists))
(case if-exists
((:error nil)
(setf mask (logior mask mach:o_excl)))
((:rename :rename-and-delete)
(setf mask (logior mask mach:o_creat)))
((:new-version :supersede)
(setf mask (logior mask mach:o_trunc)))
(:append
(setf mask (logior mask mach:o_append)))))
(t
(setf if-exists :ignore-this-arg)))
(unless if-does-not-exist-given
(setf if-does-not-exist
(cond ((eq direction :input) :error)
((and output
(member if-exists '(:overwrite :append)))
:error)
((eq direction :probe)
nil)
(t
:create))))
(setf if-does-not-exist
(assure-one-of if-does-not-exist
'(:error :create nil)
:if-does-not-exist))
(if (eq if-does-not-exist :create)
(setf mask (logior mask mach:o_creat)))
(let ((original (if (member if-exists
'(:rename :rename-and-delete))
(pick-backup-name namestring)))
(delete-original (eq if-exists :rename-and-delete))
(mode #o666))
(when original
;; We are doing a :rename or :rename-and-delete.
;; Determine if the file already exists, make sure the original
;; file is not a directory and keep the mode
(let ((exists
(multiple-value-bind
(okay err/dev inode orig-mode)
(mach:unix-stat namestring)
(declare (ignore inode) (type (or index null) orig-mode))
(cond (okay
(when (and output (= (logand orig-mode #o170000)
#o40000))
(error "Cannot open ~S for output: Is a directory."
namestring))
(setf mode (logand orig-mode #o777))
t)
((eql err/dev mach:enoent)
nil)
(t
(error "Cannot find ~S: ~A"
namestring
(mach:get-unix-error-msg err/dev)))))))
(unless (and exists
(do-old-rename namestring original))
(setf original nil)
(setf delete-original nil)
;; In order to use SUPERSEDE instead, we have
;; to make sure mach:o_creat corresponds to
;; if-does-not-exist. mach:o_creat was set
;; before because of if-exists being :rename.
(unless (eq if-does-not-exist :create)
(setf mask (logior (logandc2 mask mach:o_creat) mach:o_trunc)))
(setf if-exists :supersede))))
;; Okay, now we can try the actual open.
(loop
(multiple-value-bind
(fd errno)
(mach:unix-open namestring mask mode)
(cond ((numberp fd)
(return
(case direction
((:input :output :io)
(make-fd-stream fd
:input input
:output output
:element-type element-type
:file namestring
:original original
:delete-original delete-original
:auto-close t))
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
(:probe
(let ((stream
(%make-fd-stream :name namestring :fd fd
:element-type element-type)))
(close stream)
stream)))))
((eql errno mach:enoent)
(case if-does-not-exist
(:error
(cerror "Return NIL."
"Error opening ~S, ~A."
pathname
(mach:get-unix-error-msg errno)))
(:create
(cerror "Return NIL."
"Error creating ~S, path does not exist."
pathname)))
(return nil))
((eql errno mach:eexist)
(unless (eq nil if-exists)
(cerror "Return NIL."
"Error opening ~S, ~A."
pathname
(mach:get-unix-error-msg errno)))
(return nil))
((eql errno mach:eacces)
(cerror "Try again."
"Error opening ~S, ~A."
pathname
(mach:get-unix-error-msg errno)))
(t
(cerror "Return NIL."
"Error opening ~S, ~A."
pathname
(mach:get-unix-error-msg errno))
(return nil)))))))))
;;;; Initialization.
(defvar *tty* nil
"The stream connected to the controlling terminal or NIL if there is none.")
(defvar *stdin* nil
"The stream connected to the standard input (file descriptor 0).")
(defvar *stdout* nil
"The stream connected to the standard output (file descriptor 1).")
(defvar *stderr* nil
"The stream connected to the standard error output (file descriptor 2).")
;;; STREAM-INIT -- internal interface
;;;
;;; Called when the cold load is first started up.
;;;
(defun stream-init ()
(stream-reinit)
(setf *terminal-io* (make-synonym-stream '*tty*))
(setf *standard-output* (make-synonym-stream '*stdout*))
(setf *standard-input*
(make-two-way-stream (make-synonym-stream '*stdin*)
*standard-output*))
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
(setf *error-output* (make-synonym-stream '*stderr*))
(setf *query-io* (make-synonym-stream '*terminal-io*))
(setf *debug-io* *query-io*)
(setf *trace-output* *standard-output*)
nil)
;;; STREAM-REINIT -- internal interface
;;;
;;; Called whenever a saved core is restarted.
;;;
(defun stream-reinit ()
(setf *available-buffers* nil)
(setf *stdin*
(make-fd-stream 0 :name "Standard Input" :input t :buffering :line))
(setf *stdout*
(make-fd-stream 1 :name "Standard Output" :output t :buffering :line))
(setf *stderr*
(make-fd-stream 2 :name "Standard Error" :output t :buffering :line))
(let ((tty (mach:unix-open "/dev/tty" mach:o_rdwr #o666)))
(if tty
(setf *tty*
(make-fd-stream tty :name "the Terminal" :input t :output t
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
(setf *tty* (make-two-way-stream *stdin* *stdout*))))
nil)
;;;; Beeping.
(defun default-beep-function (stream)
(write-char #\bell stream)
(finish-output stream))
(defvar *beep-function* #'default-beep-function
"This is called in BEEP to feep the user. It takes a stream.")
(defun beep (&optional (stream *terminal-io*))
(funcall *beep-function* stream))
;;;; File position and file length.
;;; File-Position -- Public
;;;
;;; Call the misc method with the :file-position operation.
;;;
(defun file-position (stream &optional position)
"With one argument returns the current position within the file
File-Stream is open to. If the second argument is supplied, then
this becomes the new file position. The second argument may also
be :start or :end for the start and end of the file, respectively."
(unless (streamp stream)
(error "Argument ~S is not a stream." stream))
(funcall (stream-misc stream) stream :file-position position))
;;; File-Length -- Public
;;;
;;; Like File-Position, only use :file-length.
;;;
(defun file-length (stream)
"This function returns the length of the file that File-Stream is open to."
(unless (streamp stream)
(error "Argument ~S is not a stream." stream))
(funcall (stream-misc stream) stream :file-length))
;;; File-Name -- internal interface
;;;
;;; Kind of like File-Position, but is an internal hack used by the filesys
;;; stuff to get and set the file name.
;;;
(defun file-name (stream &optional new-name)
(when (fd-stream-p stream)
(if new-name
(setf (fd-stream-file stream) new-name)
(fd-stream-file stream))))