/[cmucl]/src/code/filesys.lisp
ViewVC logotype

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations)
Tue Oct 22 16:31:32 1991 UTC (22 years, 6 months ago) by chiles
Branch: MAIN
Changes since 1.12: +52 -22 lines
Modified COMPLETE-FILE to correctly complete files relative to the defaults.
It was previously relying on MERGE-PATHNAMES solely, but MERGE-PATHNAMES cannot
do what COMPLETE-FILE requires when presented with a partial (non-absolute)
pathname that includes some directories.  What the user wants is the partial
pathname to be merged with the absolute defaults including any directories
specified in pathname.  MERGE-PATHNAMES cannot do this since partial pathnames
have independent uses, and it cannot clobber a non-nil slot in the partial
pathname structure.  However, COMPLETE-FILE obtains its partial pathnames from
users specifying against defaults with the intent of deriving an absolute name,
so when the defaults contain an absolute directory slot, the partial directory
spec of the supplied pathname (if any) gets tacked onto the end of the defaults
directory spec.

This fixes a long standing Hemlock problem: you could find files relative to
the defaults with subdirectory specs, but you could not complete them.

Fixed DIRECTORY to no longer signal errors.  It's job is to return a list of
files matching its argument, and it should return nil when the spec is in
accurate.
1 ram 1.1 ;;; -*- Log: code.log; Package: Lisp -*-
2     ;;; ### Some day fix to accept :wild in any pathname component.
3     ;;; **********************************************************************
4 ram 1.8 ;;; 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 chiles 1.13 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.13 1991/10/22 16:31:32 chiles Exp $")
11 ram 1.8 ;;;
12 ram 1.1 ;;; **********************************************************************
13     ;;;
14     ;;; Ugly pathname functions for Spice Lisp.
15     ;;; these functions are part of the standard Spice Lisp environment.
16     ;;;
17     ;;; Written by Jim Large and Rob MacLachlan
18     ;;;
19     ;;; **********************************************************************
20 ram 1.2
21     (in-package "LISP")
22    
23 ram 1.1 (export '(pathname pathnamep *default-pathname-defaults* truename
24     parse-namestring merge-pathnames make-pathname
25     pathname-host pathname-device pathname-directory
26     pathname-name pathname-type pathname-version
27     namestring file-namestring directory-namestring
28     host-namestring enough-namestring user-homedir-pathname
29     probe-file rename-file delete-file file-write-date
30     file-author directory))
31    
32     (use-package "EXTENSIONS")
33    
34     (in-package "EXTENSIONS")
35     (export '(print-directory complete-file ambiguous-files default-directory
36 wlott 1.5 file-writable unix-namestring))
37 ram 1.2 (in-package "LISP")
38 ram 1.1
39    
40 wlott 1.5
41     ;;; Pathname structure
42 ram 1.1
43    
44     ;;; *Default-Pathname-defaults* has all values unspecified except for the
45     ;;; host. All pathnames must have a host.
46     (defvar *default-pathname-defaults* ()
47     "Set to the default pathname-defaults pathname (Got that?)")
48    
49     (defun filesys-init ()
50 wlott 1.3 (setq *default-pathname-defaults*
51     (%make-pathname "Mach" nil nil nil nil nil))
52     (multiple-value-bind (won dir)
53     (mach:unix-current-directory)
54     (when won
55     (setf (search-list "default:") (list dir)))))
56 ram 1.1
57    
58     ;;; The pathname type is defined with a defstruct.
59     ;;; This declaration implicitly defines the common lisp function Pathnamep
60     (defstruct (pathname
61     (:conc-name %pathname-)
62     (:print-function %print-pathname)
63     (:constructor
64     %make-pathname (host device directory name type version))
65     (:predicate pathnamep))
66     "Pathname is the structure of the file pathname. It consists of a
67     host, a device, a directory, a name, and a type."
68     (host nil :type (or simple-string null))
69     (device nil :type (or simple-string (member nil :absolute)))
70     (directory nil :type (or simple-vector null))
71     (name nil :type (or simple-string null))
72     (type nil :type (or simple-string null))
73     (version nil :type (or integer (member nil :newest))))
74    
75     (defun %print-pathname (s stream d)
76     (declare (ignore d))
77 ram 1.11 (format stream "#p~S" (namestring s)))
78 ram 1.1
79     (defun make-pathname (&key defaults (host nil hostp) (device nil devicep)
80     (directory nil directoryp) (name nil namep)
81     (type nil typep) (version nil versionp))
82     "Create a pathname from :host, :device, :directory, :name, :type and :version.
83     If any field is ommitted, it is obtained from :defaults as though by
84     merge-pathnames."
85 ram 1.10 (flet ((make-it (host device directory name type version)
86     (%make-pathname
87     (if host
88     (if (stringp host) (coerce host 'simple-string) host)
89     (%pathname-host *default-pathname-defaults*))
90     (if (stringp device) (coerce device 'simple-string) device)
91     (if (stringp directory)
92     (%pathname-directory (parse-namestring directory))
93     directory)
94     (if (stringp name) (coerce name 'simple-string) name)
95     (if (stringp type) (coerce type 'simple-string) type)
96     version)))
97 ram 1.1 (if defaults
98     (let ((defaults (pathname defaults)))
99 ram 1.10 (make-it (if hostp host (%pathname-host defaults))
100     (if devicep device (%pathname-device defaults))
101     (if directoryp directory (%pathname-directory defaults))
102     (if namep name (%pathname-name defaults))
103     (if typep type (%pathname-type defaults))
104     (if versionp version (%pathname-version defaults))))
105     (make-it host device directory name type version))))
106 ram 1.1
107    
108     ;;; These can not be done by the accessors because the pathname arg may be
109     ;;; a string or a symbol or etc.
110    
111     (defun pathname-host (pathname)
112     "Returns the host slot of pathname. Pathname may be a string, symbol,
113     or stream."
114     (%pathname-host (if (pathnamep pathname) pathname (pathname pathname))))
115    
116     (defun pathname-device (pathname)
117     "Returns the device slot of pathname. Pathname may be a string, symbol,
118     or stream."
119     (%pathname-device (if (pathnamep pathname) pathname (pathname pathname))))
120    
121     (defun pathname-directory (pathname)
122     "Returns the directory slot of pathname. Pathname may be a string,
123     symbol, or stream."
124     (%pathname-directory (if (pathnamep pathname) pathname (pathname pathname))))
125    
126     (defun pathname-name (pathname)
127     "Returns the name slot of pathname. Pathname may be a string,
128     symbol, or stream."
129     (%pathname-name (if (pathnamep pathname) pathname (pathname pathname))))
130    
131     (defun pathname-type (pathname)
132     "Returns the type slot of pathname. Pathname may be a string,
133     symbol, or stream."
134     (%pathname-type (if (pathnamep pathname) pathname (pathname pathname))))
135    
136     (defun pathname-version (pathname)
137     "Returns the version slot of pathname. Pathname may be a string,
138     symbol, or stream."
139     (%pathname-version (if (pathnamep pathname) pathname (pathname pathname))))
140    
141    
142    
143 ram 1.2 ;;;; PARSE-NAMESTRING and PATHNAME.
144 ram 1.1
145 ram 1.2 ;;; SPLIT-FILENAME -- internal
146     ;;;
147     ;;; Splits the filename into the name and type. If someone wants to change
148     ;;; this yet again, just change this.
149     ;;;
150     (defun split-filename (filename)
151     (declare (simple-string filename))
152     (let ((posn (position #\. filename :from-end t)))
153     (cond ((null posn)
154     (values filename nil))
155     ((or (zerop posn) (= posn (1- (length filename))))
156     (values filename ""))
157     (t
158     (values (subseq filename 0 posn)
159     (subseq filename (1+ posn)))))))
160 ram 1.1
161 ram 1.2 ;;; DO-FILENAME-PARSE -- internal
162 ram 1.1 ;;;
163 ram 1.2 ;;; Split string into a logical name, a vector of directories, a file name and
164     ;;; a file type.
165 ram 1.1 ;;;
166 ram 1.2 (defun do-filename-parse (string &optional (start 0) end)
167     (declare (simple-string string))
168     (let ((end (or end (length string))))
169     (let* ((directories nil)
170     (filename nil)
171     (absolutep (and (> end start) (eql (schar string start) #\/)))
172     (logical-name
173     (cond (absolutep
174     (setf start (position #\/ string :start start :end end
175     :test-not #'char=))
176     :absolute)
177     ((find #\: string
178     :start start
179     :end (or (position #\/ string :start start :end end)
180     end))
181     (let ((posn (position #\: string :start start)))
182     (prog1
183     (subseq string start posn)
184     (setf start (1+ posn))))))))
185     (loop
186     (unless (and start (> end start))
187     (return))
188     (let ((next-slash (position #\/ string :start start :end end)))
189     (cond (next-slash
190     (push (subseq string start next-slash) directories)
191     (setf start
192     (position #\/ string :start next-slash :end end
193     :test-not #'char=)))
194     (t
195     (setf filename (subseq string start end))
196     (return)))))
197     (multiple-value-bind (name type)
198     (if filename (split-filename filename))
199     (values (cond (logical-name logical-name)
200     (directories "Default"))
201     (if (or logical-name directories)
202     (coerce (nreverse directories) 'vector))
203     name
204     type)))))
205 ram 1.1
206 ram 1.2 (defun parse-namestring (thing &optional host
207     (defaults *default-pathname-defaults*)
208     &key (start 0) end junk-allowed)
209     "Convert THING (string, symbol, pathname, or stream) into a pathname."
210     (declare (ignore junk-allowed))
211     (let* ((host (or host (pathname-host defaults)))
212     (pathname
213     (etypecase thing
214     ((or string symbol)
215 ram 1.9 (with-array-data ((string (string thing))
216     (start start)
217     (end end))
218 ram 1.2 (multiple-value-bind (device directories name type)
219     (do-filename-parse string start end)
220     (make-pathname :host host
221     :device device
222     :directory directories
223     :name name
224     :type type))))
225     (pathname
226     thing)
227     (stream
228     (pathname (file-name thing))))))
229     (unless (or (null host)
230     (null (pathname-host pathname))
231     (string-equal host (pathname-host pathname)))
232     (cerror "Ignore it."
233     "Host mismatch in ~S: ~S isn't ~S"
234     'parse-namestring
235     (pathname-host pathname)
236     host))
237 ram 1.10 ;;
238     ;; ### ??? what should the second value be???
239     (values pathname (or end start))))
240 ram 1.1
241    
242     (defun pathname (thing)
243     "Turns thing into a pathname. Thing may be a string, symbol, stream, or
244     pathname."
245     (values (parse-namestring thing)))
246    
247    
248    
249     ;;; Merge-Pathnames -- Public
250     ;;;
251     ;;; Returns a new pathname whose fields are the same as the fields in PATHNAME
252     ;;; except that () fields are filled in from defaults. Type and Version field
253     ;;; are only done if name field has to be done (see manual for explanation).
254     ;;;
255     (defun merge-pathnames (pathname &optional
256     (defaults *default-pathname-defaults*)
257     default-version)
258     "Fills in unspecified slots of Pathname from Defaults (defaults to
259 chiles 1.13 *default-pathname-defaults*). If the version remains unspecified,
260     gets it from Default-Version."
261 ram 1.1 ;;
262     ;; finish hairy argument defaulting
263 ram 1.10 (let ((pathname (pathname pathname))
264     (defaults (pathname defaults)))
265     ;;
266     ;; make a new pathname
267 chiles 1.13 (let ((name (%pathname-name pathname)))
268 ram 1.10 (%make-pathname
269     (or (%pathname-host pathname) (%pathname-host defaults))
270 chiles 1.13 (or (%pathname-device pathname) (%pathname-device defaults))
271 ram 1.10 (or (%pathname-directory pathname) (%pathname-directory defaults))
272     (or name (%pathname-name defaults))
273     (or (%pathname-type pathname) (%pathname-type defaults))
274     (or (%pathname-version pathname)
275     (if name
276     default-version
277     (or (%pathname-version defaults) default-version)))))))
278 ram 1.1
279    
280     ;;;; NAMESTRING and other stringification stuff.
281    
282     ;;; %Dirstring -- Internal
283     ;;;
284     ;;; %Dirstring converts a vector of the form #("foo" "bar" ... "baz") into a
285     ;;; string of the form "foo/bar/ ... /baz/"
286    
287     (defun %dirstring (dirlist)
288     (declare (simple-vector dirlist))
289     (let* ((numdirs (length dirlist))
290     (length numdirs))
291     (declare (fixnum numdirs length))
292     (dotimes (i numdirs)
293     (incf length (the fixnum (length (svref dirlist i)))))
294     (do ((result (make-string length))
295     (index 0 (1+ index))
296     (position 0))
297     ((= index numdirs) result)
298     (declare (simple-string result))
299     (let* ((string (svref dirlist index))
300     (len (length string))
301     (end (+ position len)))
302     (declare (simple-string string)
303     (fixnum len end))
304     (replace result string :start1 position :end1 end :end2 len)
305     (setf (schar result end) #\/)
306     (setq position (+ end 1))))))
307    
308     (defun quick-integer-to-string (n)
309     (cond ((zerop n) "0")
310     ((eql n 1) "1")
311     ((minusp n)
312     (concatenate 'simple-string "-"
313     (the simple-string (quick-integer-to-string (- n)))))
314     (t
315     (do* ((len (1+ (truncate (integer-length n) 3)))
316     (res (make-string len))
317     (i (1- len) (1- i))
318     (q n)
319     (r 0))
320     ((zerop q)
321     (incf i)
322     (replace res res :start2 i :end2 len)
323 wlott 1.3 (shrink-vector res (- len i)))
324 ram 1.1 (declare (simple-string res)
325     (fixnum len i r))
326     (multiple-value-setq (q r) (truncate q 10))
327     (setf (schar res i) (schar "0123456789" r))))))
328    
329     (defun %device-string (device)
330     (cond ((eq device :absolute) "/")
331     (device
332     (if (string-equal device "Default")
333     ""
334     (concatenate 'simple-string (the simple-string device) ":")))
335     (T "")))
336    
337     (defun namestring (pathname)
338     "Returns the full form of PATHNAME as a string."
339 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
340     (directory (%pathname-directory pathname))
341 ram 1.1 (name (%pathname-name pathname))
342     (type (%pathname-type pathname))
343     (result (%device-string (%pathname-device pathname))))
344     (declare (simple-string result))
345     (when directory
346     (setq result (concatenate 'simple-string result
347     (the simple-string (%dirstring directory)))))
348     (when name
349     (setq result (concatenate 'simple-string result
350     (the simple-string name))))
351 ram 1.2 (when (and type (not (zerop (length type))))
352 ram 1.1 (setq result (concatenate 'simple-string result "."
353     (the simple-string type))))
354     result))
355    
356 wlott 1.4 (defun namestring-without-device (pathname)
357 ram 1.1 "NAMESTRING of pathname ignoring the device slot."
358 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
359     (directory (%pathname-directory pathname))
360 ram 1.1 (name (%pathname-name pathname))
361     (type (%pathname-type pathname))
362     (result ""))
363     (declare (simple-string result))
364     (when directory
365     (setq result (concatenate 'simple-string result
366     (the simple-string (%dirstring directory)))))
367     (when name
368     (setq result (concatenate 'simple-string result
369     (the simple-string name))))
370 ram 1.2 (when (and type (not (zerop (length type))))
371 ram 1.1 (setq result (concatenate 'simple-string result "."
372     (the simple-string type))))
373     result))
374    
375     ;;; This function is somewhat bummed to make the Hemlock directory command
376     ;;; is fast.
377     ;;;
378     (defun file-namestring (pathname)
379     "Returns the name, type, and version of PATHNAME as a string."
380 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
381     (name (%pathname-name pathname))
382 ram 1.1 (type (%pathname-type pathname))
383     (result (or name "")))
384     (declare (simple-string result))
385 ram 1.2 (if (and type (not (zerop (length type))))
386 ram 1.1 (concatenate 'simple-string result "." type)
387     result)))
388    
389     (defun directory-namestring (pathname)
390     "Returns the device & directory parts of PATHNAME as a string."
391 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
392     (directory (%pathname-directory pathname))
393 ram 1.1 (result (%device-string (%pathname-device pathname))))
394     (declare (simple-string result))
395     (when directory
396     (setq result (concatenate 'simple-string result
397     (the simple-string (%dirstring directory)))))
398     result))
399    
400     (defun host-namestring (pathname)
401     "Returns the host part of PATHNAME as a string."
402 wlott 1.4 (%pathname-host (if (pathnamep pathname) pathname (pathname pathname))))
403 ram 1.1
404    
405 wlott 1.4 ;;; Do-Search-List -- Internal
406     ;;;
407     ;;; Bind var in turn to each element of search list with the specifed
408     ;;; name.
409     ;;;
410     (defmacro do-search-list ((var name &optional exit-form) . body)
411     "Do-Search-List (Var Name [Exit-Form]) {Form}*"
412     `(dolist (,var (resolve-search-list ,name nil) ,exit-form)
413     (declare (simple-string ,var))
414     ,@body))
415    
416 wlott 1.5 ;;; UNIX-NAMESTRING -- public
417     ;;;
418     (defun unix-namestring (pathname &optional (for-input t))
419 wlott 1.4 "Convert PATHNAME into a string that can be used with UNIX system calls."
420     (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
421     (device (%pathname-device pathname)))
422     (cond ((or (eq device :absolute)
423     (null device)
424     (string= device "Default"))
425     (namestring pathname))
426     (for-input
427     (let ((remainder (namestring-without-device pathname))
428     (first nil))
429     (do-search-list (entry device first)
430     (let ((name (concatenate 'simple-string entry remainder)))
431     (unless first
432     (setf first name))
433     (when (mach:unix-file-kind name)
434     (return name))))))
435     (t
436     (concatenate 'simple-string
437     (car (resolve-search-list device t))
438     (namestring-without-device pathname))))))
439    
440    
441 ram 1.1
442     ;;;; ENOUGH-NAMESTRING
443    
444     (defun enough-namestring (pathname &optional
445     (defaults *default-pathname-defaults*))
446     "Returns a string which uniquely identifies PATHNAME w.r.t. DEFAULTS."
447 ram 1.10 (let* ((pathname (pathname pathname))
448     (defaults (pathname defaults))
449     (device (%pathname-device pathname))
450 ram 1.1 (directory (%pathname-directory pathname))
451     (name (%pathname-name pathname))
452     (type (%pathname-type pathname))
453     (result "")
454     (need-name nil))
455     (declare (simple-string result))
456     (when (and device (string-not-equal device (%pathname-device defaults)))
457     (setq result (%device-string device)))
458     (when (and directory
459     (not (equalp directory (%pathname-directory defaults))))
460     (setq result (concatenate 'simple-string result
461     (the simple-string (%dirstring directory)))))
462     (when (and name (string-not-equal name (%pathname-name defaults)))
463     (setq result (concatenate 'simple-string result
464     (the simple-string name))
465     need-name t))
466     (when (and type (or need-name
467     (string-not-equal type (%pathname-type defaults))))
468     (setq result (concatenate 'simple-string result "."
469     (the simple-string type))))
470     result))
471    
472    
473    
474     ;;;; TRUENAME and other stuff probing stuff.
475    
476     ;;; Truename -- Public
477     ;;;
478 wlott 1.4 ;;; Another silly file function trivially different from another function.
479 ram 1.1 ;;;
480     (defun truename (pathname)
481     "Return the pathname for the actual file described by the pathname
482     An error is signalled if no such file exists."
483     (let ((result (probe-file pathname)))
484     (unless result
485     (error "The file ~S does not exist." (namestring pathname)))
486     result))
487    
488     ;;; Probe-File -- Public
489     ;;;
490 wlott 1.4 ;;; If PATHNAME exists, return it's truename, otherwise NIL.
491 ram 1.1 ;;;
492     (defun probe-file (pathname)
493     "Return a pathname which is the truename of the file if it exists, NIL
494     otherwise. Returns NIL for directories and other non-file entries."
495 wlott 1.4 (let ((namestring (unix-namestring pathname t)))
496     (when (mach:unix-file-kind namestring)
497     (let ((truename (mach:unix-resolve-links
498     (mach:unix-maybe-prepend-current-directory
499     namestring))))
500     (when truename
501     (pathname (mach:unix-simplify-pathname truename)))))))
502 ram 1.1
503    
504 wlott 1.4 ;;;; Other random operations.
505 ram 1.1
506     ;;; Rename-File -- Public
507     ;;;
508     ;;; If File is a File-Stream, then rename the associated file if it exists,
509     ;;; otherwise just change the name in the stream. If not a file stream, then
510     ;;; just rename the file.
511     ;;;
512     (defun rename-file (file new-name)
513     "Rename File to have the specified New-Name. If file is a stream open to a
514     file, then the associated file is renamed. If the file does not yet exist
515     then the file is created with the New-Name when the stream is closed."
516 wlott 1.4 (let* ((original (truename file))
517     (original-namestring (namestring original))
518     (new-name (merge-pathnames new-name original))
519     (new-namestring (unix-namestring new-name nil)))
520     (multiple-value-bind (res error)
521     (mach:unix-rename original-namestring
522     new-namestring)
523     (unless res
524     (error "Failed to rename ~A to ~A: ~A"
525     original new-name (mach:get-unix-error-msg error)))
526     (when (streamp file)
527     (file-name file new-namestring))
528     (values new-name original (truename new-namestring)))))
529 ram 1.1
530     ;;; Delete-File -- Public
531     ;;;
532     ;;; Delete the file, Man.
533     ;;;
534     (defun delete-file (file)
535     "Delete the specified file."
536 wlott 1.4 (let ((namestring (unix-namestring file t)))
537 ram 1.1 (when (streamp file)
538     (close file :abort t))
539 wlott 1.4 (when namestring
540     (multiple-value-bind (res err) (mach:unix-unlink namestring)
541     (unless res
542     (error "Could not delete ~A: ~A."
543     namestring
544     (mach:get-unix-error-msg err))))))
545 ram 1.1 t)
546 wlott 1.4
547 ram 1.1
548     ;;; User-Homedir-Pathname -- Public
549     ;;;
550 ram 1.12 ;;; Return Home:, which is set up for us at initialization time.
551 ram 1.1 ;;;
552     (defun user-homedir-pathname (&optional host)
553     "Returns the home directory of the logged in user as a pathname.
554 ram 1.12 This is obtained from the logical name \"home:\"."
555 ram 1.1 (declare (ignore host))
556 ram 1.12 #p"home:")
557 ram 1.1
558     ;;; File-Write-Date -- Public
559     ;;;
560     (defun file-write-date (file)
561     "Return file's creation date, or NIL if it doesn't exist."
562 wlott 1.4 (multiple-value-bind (res dev ino mode nlink uid gid
563     rdev size atime mtime)
564     (mach:unix-stat (unix-namestring file t))
565     (declare (ignore dev ino mode nlink uid gid rdev size atime))
566     (when res
567     (+ unix-to-universal-time mtime))))
568 ram 1.1
569     ;;; File-Author -- Public
570     ;;;
571     (defun file-author (file)
572     "Returns the file author as a string, or nil if the author cannot be
573     determined. Signals an error if file doesn't exist."
574 wlott 1.4 (multiple-value-bind (winp dev ino mode nlink uid)
575     (mach:unix-stat (unix-namestring (pathname file) t))
576     (declare (ignore dev ino mode nlink))
577     (if winp (lookup-login-name uid))))
578 ram 1.1
579    
580    
581     ;;;; DIRECTORY.
582    
583 wlott 1.4 ;;; PARSE-PATTERN -- internal.
584 ram 1.1 ;;;
585 wlott 1.4 ;;; Parse-pattern extracts the name portion of pathname and converts it into
586     ;;; a pattern usable in match-pattern-p.
587     ;;;
588     (defun parse-pattern (pathname)
589     (let ((string (file-namestring pathname))
590     (pattern nil)
591     (last-regular-char nil)
592     (index 0))
593     (flet ((flush-pending-regulars ()
594     (when last-regular-char
595     (push (subseq string last-regular-char index) pattern))
596     (setf last-regular-char nil)))
597     (loop
598     (when (>= index (length string))
599     (return))
600     (let ((char (schar string index)))
601     (cond ((char= char #\?)
602     (flush-pending-regulars)
603     (push :single-char-wild pattern)
604     (incf index))
605     ((char= char #\*)
606     (flush-pending-regulars)
607     (push :multi-char-wild pattern)
608     (incf index))
609     ((char= char #\[)
610     (flush-pending-regulars)
611     (let ((close-bracket (position #\] string :start index)))
612     (unless close-bracket
613     (error "``['' with no corresponding ``]'': ~S" string))
614     (push :character-set pattern)
615     (push (subseq string (1+ index) close-bracket)
616     pattern)
617     (setf index (1+ close-bracket))))
618     (t
619     (unless last-regular-char
620     (setf last-regular-char index))
621     (incf index)))))
622     (flush-pending-regulars))
623     (nreverse pattern)))
624 ram 1.1
625 wlott 1.4 ;;; MATCH-PATTERN-P -- internal.
626     ;;;
627     ;;; Determine if string (starting at start) matches pattern.
628     ;;;
629     (defun match-pattern-p (string pattern &optional (start 0))
630     (cond ((null pattern)
631     (= start (length string)))
632     ((eq (car pattern) :single-char-wild)
633     (and (> (length string) start)
634     (match-pattern-p string (cdr pattern) (1+ start))))
635     ((eq (car pattern) :character-set)
636     (and (> (length string) start)
637     (find (schar string start) (cadr pattern))
638     (match-pattern-p string (cddr pattern) (1+ start))))
639     ((eq (car pattern) :multi-char-wild)
640     (do ((new-start (length string) (1- new-start)))
641     ((< new-start start) nil)
642     (when (match-pattern-p string (cdr pattern) new-start)
643     (return t))))
644     ((stringp (car pattern))
645     (let* ((expected (car pattern))
646     (len (length expected))
647     (new-start (+ start len)))
648     (and (>= (length string) new-start)
649     (string= string expected :start1 start :end1 new-start)
650     (match-pattern-p string (cdr pattern) new-start))))
651     (t
652     (error "Bogus thing in pattern: ~S" (car pattern)))))
653    
654     ;;; MATCHING-FILES-IN-DIR -- internal
655     ;;;
656     ;;; Return a list of all the files in the directory dirname that match
657     ;;; pattern. If all is nil, ignore files starting with a ``.''.
658     ;;;
659     (defun matching-files-in-dir (dirname pattern all)
660     (let ((dir (mach:open-dir dirname)))
661     (if dir
662     (unwind-protect
663     (let ((results nil))
664     (loop
665     (let ((name (mach:read-dir dir)))
666     (cond ((null name)
667     (return))
668     ((or (string= name ".")
669     (string= name "..")
670     (and (not all)
671     (char= (schar name 0) #\.))))
672     ((or (null pattern)
673     (match-pattern-p name pattern))
674     (if (zerop (length dirname))
675     (push name results)
676     (push (concatenate 'string dirname name)
677     results))))))
678     (values results t))
679     (mach:close-dir dir))
680     (values nil nil))))
681    
682     ;;; DIRECTORY -- public.
683     ;;;
684     (defun directory (pathname &key (all t) (check-for-subdirs t))
685 ram 1.1 "Returns a list of pathnames, one for each file that matches the given
686     pathname. Supplying :all as nil causes this to ignore Unix dot files. This
687     never includes Unix dot and dot-dot in the result."
688 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
689     (device (%pathname-device pathname))
690     (pattern (parse-pattern pathname))
691 chiles 1.13 (results nil))
692 wlott 1.4 (if (or (eq device :absolute)
693     (null device)
694     (string= device "Default"))
695 chiles 1.13 (setf results
696     (matching-files-in-dir (directory-namestring pathname)
697     pattern all))
698 wlott 1.4 (let ((remainder (namestring-without-device
699     (directory-namestring pathname))))
700     (do-search-list (dir device)
701     (multiple-value-bind
702     (files won)
703     (matching-files-in-dir (concatenate 'simple-string
704     dir
705     remainder)
706     pattern
707     all)
708     (when won
709     (setf really-won t))
710     (setf results (append results files))))))
711 chiles 1.13 (setf results (sort (delete-duplicates results :test #'string=) #'string<))
712 wlott 1.4 (mapcar #'(lambda (name)
713     (if (and check-for-subdirs
714     (eq (mach:unix-file-kind name) :directory))
715     (pathname (concatenate 'string name "/"))
716     (pathname name)))
717     results)))
718 ram 1.1
719 wlott 1.4
720     ;;;; Printing directories.
721 ram 1.1
722     ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
723     ;;;
724     (defun print-directory (pathname &optional stream &key all verbose return-list)
725     "Like Directory, but prints a terse, multi-coloumn directory listing
726     instead of returning a list of pathnames. When :all is supplied and
727     non-nil, then Unix dot files are included too (as ls -a). When :vervose
728     is supplied and non-nil, then a long listing of miscellaneous
729     information is output one file per line."
730     (setf pathname (pathname pathname))
731     (let ((*standard-output* (out-synonym-of stream)))
732     (if verbose
733     (print-directory-verbose pathname all return-list)
734     (print-directory-formatted pathname all return-list))))
735    
736     (defun print-directory-verbose (pathname all return-list)
737 wlott 1.7 (let ((contents (directory pathname :all all :check-for-subdirs nil))
738     (result nil))
739     (format t "Directory of ~A :~%" (namestring pathname))
740     (dolist (file contents)
741     (let* ((namestring (unix-namestring file))
742     (tail (subseq namestring
743     (1+ (or (position #\/ namestring
744     :from-end t
745     :test #'char=)
746     -1)))))
747     (multiple-value-bind
748     (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
749     (mach:unix-stat namestring)
750     (declare (ignore ino gid rdev atime)
751     (fixnum uid mode))
752     (cond (reslt
753     ;;
754     ;; Print characters for file modes.
755     (macrolet ((frob (bit name &optional sbit sname negate)
756     `(if ,(if negate
757     `(not (logbitp ,bit mode))
758     `(logbitp ,bit mode))
759     ,(if sbit
760     `(if (logbitp ,sbit mode)
761     (write-char ,sname)
762     (write-char ,name))
763     `(write-char ,name))
764     (write-char #\-))))
765     (frob 15 #\d nil nil t)
766     (frob 8 #\r)
767     (frob 7 #\w)
768     (frob 6 #\x 11 #\s)
769     (frob 5 #\r)
770     (frob 4 #\w)
771     (frob 3 #\x 10 #\s)
772     (frob 2 #\r)
773     (frob 1 #\w)
774     (frob 0 #\x))
775     ;;
776     ;; Print the rest.
777     (multiple-value-bind (sec min hour date month year)
778     (get-decoded-time)
779     (declare (ignore sec min hour date month))
780     (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
781     nlink
782     (or (lookup-login-name uid) uid)
783     size
784     (decode-universal-time-for-files mtime year)
785     tail
786     (= (logand mode mach::s_ifmt) mach::s_ifdir))))
787     (t (format t "Couldn't stat ~A -- ~A.~%"
788     tail
789     (mach:get-unix-error-msg dev-or-err))))
790 ram 1.1 (when return-list
791 wlott 1.7 (push (if (= (logand mode mach::s_ifmt) mach::s_ifdir)
792     (pathname (concatenate 'string namestring "/"))
793     file)
794     result)))))
795     (nreverse result)))
796 ram 1.1
797     (defun decode-universal-time-for-files (time current-year)
798     (multiple-value-bind (sec min hour day month year)
799     (decode-universal-time (+ time unix-to-universal-time))
800     (declare (ignore sec))
801     (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
802     (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
803     "Sep" "Oct" "Nov" "Dec")
804     (1- month))
805     day (= current-year year) year hour min)))
806    
807     (defun print-directory-formatted (pathname all return-list)
808     (let ((width (or (line-length *standard-output*) 80))
809     (names ())
810     (cnt 0)
811     (max-len 0)
812 wlott 1.7 (result (directory pathname :all all)))
813 ram 1.1 (declare (list names) (fixnum max-len cnt))
814     ;;
815     ;; Get the data.
816 wlott 1.7 (dolist (file result)
817     (let* ((name (unix-namestring file))
818     (length (length name))
819     (end (if (and (plusp length)
820     (char= (schar name (1- length)) #\/))
821     (1- length)
822     length))
823     (slash-name (subseq name
824     (1+ (or (position #\/ name
825     :from-end t
826     :end end
827     :test #'char=)
828     -1))))
829     (len (length slash-name)))
830     (declare (simple-string slash-name)
831     (fixnum len))
832     (if (> len max-len) (setq max-len len))
833     (incf cnt)
834     (push slash-name names)))
835     (setq names (nreverse names))
836     ;;
837     ;; Do the output.
838     (let* ((col-width (1+ max-len))
839     (cols (max (truncate width col-width) 1))
840     (lines (ceiling cnt cols)))
841     (declare (fixnum cols lines))
842     (format t "Directory of ~A :~%" (namestring pathname))
843     (dotimes (i lines)
844     (declare (fixnum i))
845     (dotimes (j cols)
846     (declare (fixnum j))
847     (let ((name (nth (+ i (the fixnum (* j lines))) names)))
848     (when name
849     (write-string name)
850     (unless (eql j (1- cols))
851     (tab-over
852     (- col-width (length (the simple-string name))))))))
853     (terpri)))
854     (when return-list
855     result)))
856 ram 1.1
857    
858    
859     ;;;; Translating uid's and gid's.
860    
861     (defvar *uid-hash-table* (make-hash-table)
862     "Hash table for keeping track of uid's and login names.")
863    
864     ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
865     ;;; lookups are cached in a hash table since groveling the passwd(s) files
866     ;;; is somewhat expensive. The table may hold nil for id's that cannot
867     ;;; be looked up since this means the files are searched in their entirety
868     ;;; each time this id is translated.
869     ;;;
870     (defun lookup-login-name (uid)
871     (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
872     (if foundp
873     login-name
874     (setf (gethash uid *uid-hash-table*)
875     (get-group-or-user-name :user uid)))))
876    
877     (defvar *gid-hash-table* (make-hash-table)
878     "Hash table for keeping track of gid's and group names.")
879    
880     ;;; LOOKUP-GROUP-NAME translates a group id into a group name. Previous
881     ;;; lookups are cached in a hash table since groveling the group(s) files
882     ;;; is somewhat expensive. The table may hold nil for id's that cannot
883     ;;; be looked up since this means the files are searched in their entirety
884     ;;; each time this id is translated.
885     ;;;
886     (defun lookup-group-name (gid)
887     (multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
888     (if foundp
889     group-name
890     (setf (gethash gid *gid-hash-table*)
891     (get-group-or-user-name :group gid)))))
892    
893    
894     ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
895     ;;; a much smaller file, contains all the local id's, and most uses probably
896     ;;; involve id's on machines one would login into. Then if necessary, we look
897     ;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
898     ;;; fetched over the net.
899     ;;;
900     (defun get-group-or-user-name (group-or-user id)
901     "Returns the simple-string user or group name of the user whose uid or gid
902     is id, or NIL if no such user or group exists. Group-or-user is either
903     :group or :user."
904     (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
905     (declare (simple-string id-string))
906     (multiple-value-bind (file1 file2)
907     (ecase group-or-user
908     (:group (values "/etc/group" "/etc/groups"))
909     (:user (values "/etc/passwd" "/etc/passwd")))
910     (or (get-group-or-user-name-aux id-string file1)
911     (get-group-or-user-name-aux id-string file2)))))
912    
913     (defun get-group-or-user-name-aux (id-string passwd-file)
914     (with-open-file (stream passwd-file)
915     (loop
916     (let ((entry (read-line stream nil)))
917     (unless entry (return nil))
918     (let ((name-end (position #\: (the simple-string entry)
919     :test #'char=)))
920     (when name-end
921     (let ((id-start (position #\: (the simple-string entry)
922     :start (1+ name-end) :test #'char=)))
923     (when id-start
924     (incf id-start)
925     (let ((id-end (position #\: (the simple-string entry)
926     :start id-start :test #'char=)))
927     (when (and id-end
928     (string= id-string entry
929     :start2 id-start :end2 id-end))
930     (return (subseq entry 0 name-end))))))))))))
931    
932    
933 wlott 1.4 ;;;; File completion.
934 ram 1.1
935 chiles 1.13 ;;; COMPLETE-FILE -- Public
936     ;;;
937 wlott 1.4 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
938     ignore-types)
939     (let ((files
940 chiles 1.13 (directory (complete-file-directory-arg pathname defaults)
941 wlott 1.4 :check-for-subdirs nil)))
942     (cond ((null files)
943     (values nil nil))
944     ((null (cdr files))
945     (values (merge-pathnames (file-namestring (car files))
946     pathname)
947     t))
948     (t
949     (let ((good-files
950     (delete-if #'(lambda (pathname)
951     (and (pathname-type pathname)
952     (member (pathname-type pathname)
953     ignore-types
954     :test #'string=)))
955     files)))
956     (cond ((null good-files))
957     ((null (cdr good-files))
958     (return-from complete-file
959     (values (merge-pathnames (file-namestring
960     (car good-files))
961     pathname)
962     t)))
963     (t
964     (setf files good-files)))
965     (let ((common (file-namestring (car files))))
966     (dolist (file (cdr files))
967     (let ((name (file-namestring file)))
968     (dotimes (i (min (length common) (length name))
969     (when (< (length name) (length common))
970     (setf common name)))
971     (unless (char= (schar common i) (schar name i))
972     (setf common (subseq common 0 i))
973     (return)))))
974     (values (merge-pathnames common pathname)
975     nil)))))))
976 ram 1.1
977 chiles 1.13 ;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
978     ;;;
979     ;;; This is necessary to make COMPLETE-FILE reasonable. The problem is that
980     ;;; MERGE-PATHNAMES of pathname and defaults fails us when pathname has a
981     ;;; non-nil directory slot, and pathname is relative to the "default" device.
982     ;;; There is no good argument for making MERGE-PATHNAMES do what COMPLETE-FILE
983     ;;; wants, but since COMPLETE-FILE is used in the context of the defaults
984     ;;; argument, it should always complete relative to the defaults, not relative
985     ;;; to the "default" logical name. That is, what users want when pathname is
986     ;;; not absolute and specifies directories is something relative to the
987     ;;; absolute defaults supplied.
988     ;;;
989     (defun complete-file-directory-arg (pathname defaults)
990     (let ((pathname (pathname pathname))
991     (defaults (pathname defaults)))
992     (concatenate 'simple-string
993     (namestring
994     (cond
995     ((not (and (string= (pathname-device pathname) "Default")
996     (pathname-directory pathname)))
997     (merge-pathnames pathname defaults))
998     ((eq (pathname-device defaults) :absolute)
999     ;;
1000     (make-pathname
1001     :host (or (pathname-host pathname) (pathname-host defaults))
1002     :device :absolute
1003     :directory (concatenate 'simple-vector
1004     (pathname-directory defaults)
1005     (pathname-directory pathname))
1006     :name (or (pathname-name pathname) (pathname-name defaults))
1007     :type (or (pathname-type pathname)
1008     (pathname-type defaults))))
1009     (t
1010     ;; This branch should never fire, but just in case ...
1011     (merge-pathnames pathname defaults))))
1012     "*")))
1013    
1014 wlott 1.4 ;;; Ambiguous-Files -- Public
1015 ram 1.1 ;;;
1016 wlott 1.4 (defun ambiguous-files (pathname &optional defaults)
1017     "Return a list of all files which are possible completions of Pathname.
1018 chiles 1.13 We look in the directory specified by Defaults as well as looking down
1019     the search list."
1020 wlott 1.4 (directory (concatenate 'string
1021 wlott 1.6 (namestring
1022     (merge-pathnames pathname
1023     (make-pathname :defaults defaults
1024     :name nil
1025     :type nil)))
1026     "*")))
1027 wlott 1.4
1028 ram 1.1
1029     ;;; File-writable -- exported from extensions.
1030     ;;;
1031     ;;; Determines whether the single argument (which should be a pathname)
1032     ;;; can be written by the the current task.
1033    
1034     (defun file-writable (name)
1035     "File-writable accepts a pathname and returns T if the current
1036     process can write it, and NIL otherwise."
1037 wlott 1.4 (let ((truename (probe-file name)))
1038     (values
1039     (mach:unix-access
1040     (unix-namestring (or truename (directory-namestring name)) t)
1041     (if truename mach:w_ok (logior mach:w_ok mach:x_ok))))))
1042 ram 1.1
1043    
1044     ;;; Pathname-Order -- Internal
1045     ;;;
1046     ;;; Predicate to order pathnames by. Goes by name.
1047     ;;;
1048     (defun pathname-order (x y)
1049     (let ((xn (%pathname-name x))
1050     (yn (%pathname-name y)))
1051     (if (and xn yn)
1052     (let ((res (string-lessp xn yn)))
1053     (cond ((not res) nil)
1054     ((= res (length (the simple-string xn))) t)
1055     ((= res (length (the simple-string yn))) nil)
1056     (t t)))
1057     xn)))
1058    
1059    
1060     ;;; Default-Directory -- Public
1061     ;;;
1062     ;;; This fills in a hole in Common Lisp. We return the first thing we
1063     ;;; find by doing a ResolveSearchList on Default.
1064     ;;;
1065     (defun default-directory ()
1066     "Returns the pathname for the default directory. This is the place where
1067     a file will be written if no directory is specified. This may be changed
1068     with setf."
1069     (multiple-value-bind (gr dir-or-error)
1070     (mach:unix-current-directory)
1071     (if gr
1072 wlott 1.3 (pathname (concatenate 'simple-string dir-or-error "/"))
1073     (error dir-or-error))))
1074 ram 1.1
1075     ;;;
1076     ;;; Maybe this shouldn't go here...
1077     (defsetf default-directory %set-default-directory)
1078    
1079     ;;; %Set-Default-Directory -- Internal
1080     ;;;
1081     ;;; The setf method for Default-Directory. We actually set the environment
1082     ;;; variable Current which is by convention the head of the search list.
1083     ;;;
1084     (defun %set-default-directory (new-val)
1085     (multiple-value-bind (gr error)
1086 wlott 1.4 (mach:unix-chdir (unix-namestring new-val t))
1087 ram 1.1 (if gr
1088     (car (setf (search-list "default:")
1089 wlott 1.3 (list (default-directory))))
1090 ram 1.1 (error (mach:get-unix-error-msg error)))))

  ViewVC Help
Powered by ViewVC 1.1.5