File: pathname.lsp

package info (click to toggle)
xlispstat 3.52.0-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 7,472 kB
  • ctags: 12,480
  • sloc: ansic: 89,534; lisp: 21,690; sh: 1,525; makefile: 520; csh: 1
file content (341 lines) | stat: -rw-r--r-- 11,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
;;;;
;;;; Simplified version of some pathname functions.
;;;; XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney
;;;; Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz
;;;; You may give out copies of this software; for conditions see the file
;;;; COPYING included with this distribution.
;;;;

(in-package "XLISP")

(export '(namestring file-namestring directory-namestring
	 pathname pathname-host pathname-device pathname-version
	 parse-namestring pathname-directory pathname-name pathname-type
	 *default-pathname-defaults*
	 make-pathname merge-pathnames))

(defun namestring (x)
  (cond
   ((stringp x) x)
   ((streamp x) (truename x))
   (t (error "bad namestring - ~s" x))))

(defun file-namestring (pathname)
  (make-pathname :name (pathname-name pathname)
                 :type (pathname-type pathname)))

(defun directory-namestring (pathname)
  (make-pathname :directory (pathname-directory pathname)))

(defun parse-namestring (x &optional host defaults &key
			   (start 0)
			   end
			   junk-allowed)
  (cond
   ((stringp x) (subseq x start end))
   ((streamp x) (truename x))
   (junk-allowed nil)
   (t (error "bad namestring - ~s" x))))

(defun pathname (x) (namestring x))

(defun pathname-host (x) nil)
(defun pathname-version (x) nil)

(defvar *default-pathname-defaults* "")

#+(or unix macintosh)
(defun pathname-device (x) nil)
#+msdos
(defun pathname-device (x)
  (let ((d (position #\: x)))
    (if (and d (= d 1))
        (subseq x 0 1))))

(defconstant *wild-pathname-string*
  #+macintosh "*" ;; this isn't a good choice, but Mac CL uses it too.
  #-macintosh "*")

(defconstant *back-pathname-string*
  #+macintosh "::"
  #-macintosh "..")

(defun simplify-directory (dir)
  (cond
   ((null dir) dir)
   ((and (stringp (first dir)) (eq (second dir) :back))
    (simplify-directory (rest (rest dir))))
   (t (let ((ntail (simplify-directory (rest dir))))
	(if (eq ntail (rest dir))
	    dir
	  (cons (first dir) ntail))))))

;;**** should try to handle :back, :up, and OS equivalents (.., ::, etc.)
(defun make-pathname (&key (defaults "")
			   (host (pathname-host defaults))
			   (device (pathname-device defaults))
			   (directory (pathname-directory defaults))
			   (name (pathname-name defaults))
			   (type (pathname-type defaults))
			   (version (pathname-version defaults)))
  (when (stringp directory)
	(setf directory
	      (list :absolute
		    (string-trim #+unix "/" #+msdos "\\" #+macintosh ":"
				 directory))))
  (let ((nlist nil))
    #+msdos
    (when device (push (concatenate 'string device ":") nlist))
    (setf directory (subst *wild-pathname-string* :wild directory))
    (setf directory (simplify-directory directory))
    (setf directory (subst *back-pathname-string* :back directory))
    (setf directory (subst *back-pathname-string* :up directory))
    (when (eq name :wild) (setf name *wild-pathname-string*))
    (when (eq type :wild) (setf name *wild-pathname-string*))
    (cond
     ((eq (first directory) :absolute)
      #+unix (push "/" nlist)
      #+msdos (push "\\" nlist)
      (pop directory))
     ((eq (first directory) :relative)
      #+macintosh (push ":" nlist)
      (pop directory))
     ((null directory)
      #+macintosh (push ":" nlist)
      #+(or unix msdos) nil)
     (t (error "bad directory - ~s" directory)))
    (dolist (d directory)
      (push (if d (string d)) nlist)
      (push #+unix "/" #+macintosh ":" #+msdos "\\" nlist))
    (push (if name (string name)) nlist)
    (when type
	  (push "." nlist)
	  (push (if type (string type)) nlist))
    (apply #'concatenate 'string (nreverse nlist))))

(defun pathname-directory (x)
  (let ((dir nil)
	(pos 0))
     #+msdos
     (let ((d (position #\: x)))
       (if (and d (= d 1))
           (setf x (subseq x 2))))
     #+(or unix msdos)
     (cond
      ((and (< 0 (length x)) (char= (elt x 0) #+ unix #\/ #+msdos #\\))
       (setf pos 1)
       (push :absolute dir))
      (t (push :relative dir)))
     #+macintosh
     (cond
      ((and (< 0 (length x))
            (find #\: x :test #'char=)
            (not (char= #\: (elt x 0))))
       (push :absolute dir))
      (t (if (and (< 0 (length x)) (char= #\: (elt x 0))) (setf pos 1))
	 (push :relative dir)))
    (loop
     (let ((npos (position #+unix #\/ #+macintosh #\: #+msdos #\\ x :start pos)))
       (unless npos (return (nreverse dir)))
       (push (subseq x pos npos) dir)
       (setf pos (1+ npos))))))

(defun pathname-name (x)
  #+msdos
  (let ((d (position #\: x)))
    (if (and d (= d 1))
        (setf x (subseq x 2))))
  (let* ((start (position #+unix #\/ #+macintosh #\: #+msdos #\\ x :from-end t))
	 (end (position #\. x :from-end t :start (if start start 0)))
	 (name (subseq x (if start (1+ start) 0) end)))
    (if (< 0 (length name)) name)))

(defun pathname-type (x)
  (let* ((start (position #+unix #\/ #+macintosh #\: #+msdos #\\ x :from-end t))
	 (pos (position #\. x :from-end t :start (if start start 0)))
	 (typestr (if pos (subseq x (1+ pos)) "")))
    (if (< 0 (length typestr)) typestr)))

(defun merge-pathnames (path &optional (defaults "") version)
  (let ((dir (pathname-directory path))
	(name (pathname-name path))
	(type (pathname-type path))
	(device (pathname-device path)))
    (make-pathname :directory (if dir
				  (if (and (consp dir)
					   (eq (first dir) :relative))
				      (append (pathname-directory defaults)
					      (rest dir))
				      dir)
				  (pathname-directory defaults))
		   :name (if name name (pathname-name defaults))
		   :type (if type type (pathname-type defaults))
                   :device (if device device (pathname-device defaults)))))

;;;;
;;;;
;;;; Replacement for builtin LOAD
;;;;
;;;;

(export '(*load-print* *load-verbose* *load-pathname*
	  *load-pathname-defaults* *load-truename*))

(defvar *load-print* nil)
(defvar *load-verbose* t)
(defvar *load-pathname*)
(defvar *load-truename*)

(defvar *load-pathname-defaults* 
  nil 
  "list of additional defaults for load to try")

(defun do-load (path vflag pflag)
  (let ((*readtable* *readtable*)
	(*package* *package*)
	(*load-pathname* path)
	(*load-truename* (truename path))
	(eof (cons nil nil)))
     (with-open-file (stream path :if-does-not-exist nil)
       (when vflag (format t "~&; loading ~a~%" path))
       (loop
	(let ((expr (read stream nil eof)))
	  (when (eq expr eof) (return t))
	  (let ((val (eval expr)))
	    (when pflag (print val))))))))

(defun do-does-not-exist (file flag)
  (if flag (error "can't load file -- ~s" file)))

(defun load (file &key
		  (verbose *load-verbose*)
		  (print *load-print*)
		  (if-does-not-exist t))
  (let ((load-path (cons *default-pathname-defaults*
			 *load-pathname-defaults*)))
    (cond
     ((pathname-type file)
      (dolist (d load-path (do-does-not-exist file if-does-not-exist))
        (let ((path (merge-pathnames file d)))
	  (if (probe-file path)
	      (return (do-load path verbose print))))))
     (t
      (let ((lspfile (merge-pathnames file ".lsp"))
	    (fslfile (merge-pathnames file ".fsl")))
	(dolist (d load-path (do-does-not-exist file if-does-not-exist))
          (let* ((lsppath (merge-pathnames lspfile d))
		 (fslpath (merge-pathnames fslfile d))
		 (lsp-exists (probe-file lsppath))
		 (fsl-exists (probe-file fslpath)))
	    (cond
	     ((or (and fsl-exists
		       lsp-exists
		       (< (file-write-date lsppath) (file-write-date fslpath)))
		  (and fsl-exists (not lsp-exists)))
	      (return (do-load fslpath verbose print)))
	     (lsp-exists (return (do-load lsppath verbose print)))))))))))

(export '(wild-pathname-p pathname-match-p directory))

;;**** very minimal and very inefficient versions that are just adequate
;;**** to support DIRECTORY
(defun wild-pathname-p (pathname)
  (or (equal (pathname-name pathname) *wild-pathname-string*)
      (equal (pathname-type pathname) *wild-pathname-string*)
      (member *wild-pathname-string* (pathname-directory pathname)
	      :test #'equal)))

(defun pathname-match-p (pathname wildname)
  (and (equal (pathname-directory pathname) (pathname-directory wildname))
       (let ((wname (pathname-name wildname))
	     (wtype (pathname-type wildname))
	     (pname (pathname-name pathname))
	     (ptype (pathname-type pathname)))
	 (if (wild-pathname-p wname)
	     (or (null wtype)
		 (equal ptype wtype)
		 (and ptype (wild-pathname-p wtype)))
	   (and (equal pname wname)
		(or (equal ptype wtype)
		    (and ptype (wild-pathname-p wtype))))))))

;;**** needs to support wild cards in directories and  full pathname matching
(defun directory (arg &key all)
  (let* ((pattern (if (stringp arg) arg (truename arg)))
	 (dir (pathname-directory pattern))
	 (dev (pathname-device pattern))
	 (name (pathname-name pattern))
	 (type (pathname-type pattern))
	 (leafpat (make-pathname :name name :type type)))
    (let* ((dirname (make-pathname :directory dir :device dev))
	   (dirtruename (truename dirname))
	   (dlist (system::base-directory dirtruename))
	   (ndlist (remove-if-not #'(lambda (x) (pathname-match-p x leafpat))
				  dlist))
	   (tdlist (mapcar #'(lambda (x) (merge-pathnames x dirtruename))
			   ndlist)))
      (if all
	  tdlist
	(remove-if-not #'(lambda (x) (eq (system::file-type x) :regular))
		       tdlist)))))


;;;
;;; Autoloading
;;;

(in-package "SYSTEM")
(export '(define-autoload-module register-autoloads
          create-autoload-path))

(defun autoload-function (name)
  (let ((modpath (find-function-module-path name))
        (restart (find-restart 'continue))
        (*load-verbose* nil))
    (when (and modpath restart)
          (load modpath)
          (when (fboundp name)
                (invoke-restart restart)))))

(defun autoload-variable (name)
  (let ((modpath (find-variable-module-path name))
        (restart (find-restart 'continue))
        (*load-verbose* nil))
    (when (and modpath restart)
          (load modpath)
          (when (boundp name)
                (invoke-restart restart)))))

(let ((function-modules (make-hash-table))
      (variable-modules (make-hash-table)))
  (defun find-function-module-path (name)
    (gethash name function-modules))
  (defun find-variable-module-path (name)
    (gethash name variable-modules))
  (defun add-function-module (name module)
    (setf (gethash name function-modules) module))
  (defun add-variable-module (name module)
    (setf (gethash name variable-modules) module)))

(defmacro define-autoload-module (module &rest clauses)
  `(let* ((dir (pathname-directory *load-truename*))
          (mname (make-pathname :name ',module :directory dir))
         (clist ',clauses))
     (dolist (c clist)
       (ecase (first c)
         (variable (dolist (n (rest c)) (add-variable-module n mname)))
         (function (dolist (n (rest c)) (add-function-module n mname)))))))

(defun register-autoloads (dir)
  (let ((idx (merge-pathnames "_autoidx" dir))
        (dirlist (system::base-directory dir)))
    #+(or unix msdos) (setf dirlist (delete "." dirlist :test #'equal))
    #+(or unix msdos) (setf dirlist (delete ".." dirlist :test #'equal))
    (load idx :verbose nil :if-does-not-exist nil)
    (dolist (d dirlist)
      (let ((dpath (make-pathname :directory (list :relative d))))
        (register-autoloads (merge-pathnames dpath dir))))))

(defun create-autoload-path ()
  (list (merge-pathnames (make-pathname :directory '(:relative "Autoload"))
                         *default-path*)))