Debian Codegraph @

comlib/fsys.lisp

raw
1 ;;;-*- Mode:Lisp; Syntax:Common-Lisp; Package:CHAOS -*-
2 ;;;
3 ;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :CHAOS)
30 #|==============================================================================
31 System: Chaos
32 Module: comlib
33 File: fsys.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 #+:SBCL
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42 (require 'sb-posix))
43
44 ;;; *****************
45 ;;; FILE SYSTEM UTILS___________________________________________________________
46 ;;; *****************
47
48 ;;; LOAD-FILE : filename
49 ;;; UNIX dependent.
50 (defun load-file (fname)
51 (declare (type simple-string fname)
52 (values t))
53 (when (and (eql #\~ (char fname 0)) (eql #\/ (char fname 1)))
54 (setq fname
55 (concatenate 'string
56 (namestring (user-homedir-pathname))
57 (subseq fname 2))))
58 (load fname))
59
60 #-GCL
61 (declaim (ftype (function ((or simple-string pathname)) simple-string)
62 expand-file-name))
63
64 (defun expand-file-name (fname)
65 (declare (type (or simple-string pathname) fname)
66 (values simple-string))
67 (if (pathnamep fname)
68 (namestring fname)
69 (progn
70 (setq fname (namestring fname))
71 (if (equal "~" fname)
72 (namestring (user-homedir-pathname))
73 (if (and (eql #\~ (char fname 0)) (eql #\/ (char fname 1)))
74 (concatenate 'string
75 (namestring (user-homedir-pathname)) (subseq fname 2))
76 fname)))))
77
78 ;;; DOFILE
79 ;;; Opens the specified file for input, reads successive lines
80 ;;; from the file, setting the specified variable <var> to
81 ;;; each line. When end of file is reached, the value of <return-form>
82 ;;; is returned.
83
84 (defmacro dofile ((var filename &optional return-form) &body body)
85 (let ((eof (gensym "EOF"))
86 (stream (gensym "STREAM")))
87 `(with-open-file (,stream ,filename :direction :input)
88 (do ((,var (read-line ,stream nil ,eof)
89 (read-line ,stream nil ,eof)))
90 ((eq ,var ,eof)
91 ,return-form)
92 ,@body))))
93
94 ;;;
95 ;;; IS-DIRECTORY? path -> (or null pathname)
96 ;;;
97 (defun is-directory? (path)
98 (declare (type (or pathname simple-string) path))
99 (let ((dpath (expand-file-name path)))
100 #+(or GCL CMU :openmcl)
101 (probe-file (concatenate 'string dpath "/"))
102 #+:SBCL
103 (let* ((p (probe-file dpath))
104 (nstr (if p
105 (namestring p)
106 "")))
107 (declare (type simple-string nstr))
108 (if (and p
109 (string-equal (subseq nstr
110 (1- (length nstr)))
111 ;; sbcl uses "/" on all platforms.
112 "/"))
113 p
114 nil))
115 #+:Allegro
116 (if (excl:file-directory-p dpath)
117 #-:mswindows (pathname (concatenate 'string dpath "/"))
118 #+:mswindows (pathname dpath)
119 nil)
120 #+(and :CCL (not :openmcl)) (if (directoryp dpath) dpath nil)
121 #+:CLISP
122 (let ((p (concatenate 'string dpath "/")))
123 (if (directory p) ;; (ext:probe-directory path)
124 p
125 nil))))
126
127 (defun is-relative-file-name? (path)
128 (declare (type (or simple-string pathname) path))
129 (let ((pn (if (pathnamep path)
130 path
131 (pathname path))))
132 (declare (type pathname pn))
133 (or (null (pathname-directory pn))
134 (member ':relative (pathname-directory pn) :test #'equal))))
135
136 (defun supply-suffixes (path suffixes)
137 (declare (type (or simple-string pathname) path)
138 (type list suffixes))
139 #+:SBCL
140 (let ((pn (if (pathnamep path)
141 (pathname-name path)
142 path)))
143 (declare (type simple-string pn))
144 (when (position #\. pn :from-end t)
145 (return-from supply-suffixes (list path))))
146 (mapcar #'pathname
147 (mapcar #'(lambda (x) (concatenate 'string
148 (namestring path)
149 (namestring x)))
150 suffixes)))
151
152 (defun chaos-get-relative-path (f-name)
153 (declare (type simple-string f-name)
154 (values pathname))
155 (flet ((chaos-relative-pathname? ()
156 (let ((fdp (pathname-directory (pathname f-name))))
157 (or (null fdp)
158 (and fdp ; not simple file name.
159 (not (eq (car (pathname-directory (pathname f-name)))
160 :root))))))
161 (chaos-get-directory (file-path)
162 (declare (type (or simple-string pathname)))
163 (unless (pathnamep file-path)
164 (setq file-path (pathname file-path)))
165 (let ((dir-path (make-pathname :host (pathname-host file-path)
166 :device (pathname-device file-path)
167 :directory (pathname-directory file-path))))
168 dir-path)))
169 (if (null *chaos-input-source*)
170 (pathname f-name)
171 (if (chaos-relative-pathname?)
172 (let ((f-path nil))
173 (unwind-protect
174 (let ((host (pathname-host (pathname f-name)))
175 (device (pathname-device (pathname f-name)))
176 (fd (pathname-directory (pathname f-name)))
177 (f (file-namestring (pathname f-name))))
178 (chaos-pushd (chaos-get-directory *chaos-input-source*))
179 #+GCL
180 (setq f-path (truename (make-pathname :directory fd :name f)))
181 #+:CLISP
182 (setq f-path (make-pathname
183 :host host
184 :device device
185 :directory fd ;; (pathname fd)
186 :name f))
187 #-(or GCL :CLISP)
188 (progn
189 (setq f-path (make-pathname
190 :host host
191 :device device
192 :directory fd
193 :name f))))
194 (chaos-popd))
195 f-path)
196 ;; absolute path or simple filename.
197 (pathname f-name)))))
198
199 (defun chaos-probe-file (file load-path suffixes)
200 (declare (type (or simple-string pathname) file)
201 (type list load-path suffixes))
202 (when (pathnamep file)
203 (if (is-directory? file)
204 (return-from chaos-probe-file nil)
205 (return-from chaos-probe-file (probe-file file))))
206 (let ((fp (expand-file-name file)))
207 (declare (type simple-string fp))
208 (cond ((is-relative-file-name? fp)
209 (let ((file-path (chaos-get-relative-path
210 (concatenate 'string "./" fp)))
211 (res nil))
212 (declare (type pathname file-path))
213 (setq res (probe-file file-path))
214 (when (and res (is-directory? res))
215 (setq res nil))
216 (unless res
217 (dolist (fx (supply-suffixes file-path suffixes))
218 (when (and (probe-file fx) (not (is-directory? fx)))
219 (setq res fx)
220 (return)))
221 ;; search through load paths
222 (unless res
223 (dolist (lpath load-path)
224 (declare (type pathname lpath))
225 (let ((libdir (is-directory? lpath)))
226 (declare (type (or null pathname) libdir))
227 (when libdir
228 (let ((f (make-pathname
229 :host (pathname-host libdir)
230 :device (pathname-device libdir)
231 :directory #+:CLISP libdir
232 #-:CLISP (pathname-directory libdir)
233 :name (namestring file))))
234 (declare (type pathname f))
235 (if (and (probe-file f) (not (is-directory? f)))
236 (progn (setq res f) (return))
237 (let ((x (supply-suffixes f suffixes)))
238 (dolist (fx x)
239 (when (and (probe-file fx) (not (is-directory? fx)))
240 (setq res fx)
241 (return)))))))))))
242 res))
243 (t (let ((file-path (chaos-get-relative-path file)))
244 (declare (type pathname file-path))
245 (if (and (probe-file file-path) (not (is-directory? file-path)))
246 file-path
247 (dolist (fx (supply-suffixes file-path suffixes))
248 (when (and (probe-file fx) (not (is-directory? fx)))
249 (return-from chaos-probe-file fx)))))))))
250
251 (defun bare-chaos-pwd ()
252 #+GCL (probe-file ".")
253 #+EXCL (excl::current-directory)
254 #+CMU (ext:default-directory)
255 #+:SBCL (car (directory "./"))
256 #+:CCL (mac-default-directory)
257 #+:CLISP (car (lisp:directory "./")))
258
259 (defun chaos-pwd ()
260 (declare (values simple-string))
261 (namestring (bare-chaos-pwd)))
262
263
264 #+(or (and CCL (not :openmcl)) :microsoft)
265 (defun chaos-ls (&optional (dir "**"))
266 (pprint (mapcar #'(lambda (x)
267 (file-namestring x))
268 (directory dir))))
269
270 #+(or GCL LUCID (and EXCL (not :microsoft)) CLISP :openmcl SBCL)
271 (defun chaos-ls (&optional dir)
272 (let ((comm '("ls"))
273 (args (if (and dir (atom dir))
274 (list dir)
275 dir)))
276 (setq comm (reduce #'(lambda (x y) (concatenate 'string x " " y))
277 (append comm args)))
278 #+GCL (system comm)
279 #+EXCL (excl:shell comm)
280 #+SBCL (apply #'sb-ext:run-program
281 #+win32 "CMD" #-win32 "/bin/sh"
282 #+win32 (list "/c" "dir") #-win32 (list "-c" comm)
283 :input nil :output *terminal-io*
284 #+win32 '(:search t) #-win32 nil)
285 #+LUCID (lucid::%execute-system-command comm)
286 #+CLISP (ext::shell comm)))
287
288 #+(or CMU :openmcl)
289 (defun chaos-ls (&optional args)
290 (if (and args (atom args))
291 #+CMU (ext::run-program "ls" (list args) :output t)
292 #+:openmcl (ccl::run-program "ls" (list args) :output t)
293 #+CMU
294 (if args
295 (ext::run-program "ls" args :output t)
296 (ext::run-program "ls" nil :output t)
297 )
298 #+:penmcl
299 (if args
300 (ccl::run-program "ls" args :output t)
301 (ccl::run-program "ls" nil :output t))))
302
303 (defvar *chaos-directory-stack* nil)
304
305 (defun chaos-print-directory-stack (&optional (stream *standard-output*))
306 (format stream "~%~a" *chaos-directory-stack*))
307
308 (defun fsys-parse-number (tok)
309 (declare (type (or simple-string pathname) tok))
310 (if (stringp tok)
311 (let ((minusp nil))
312 (if (char= (char tok 0) #\-)
313 (setq minusp t)
314 (unless (char= (char tok 0) #\+)
315 (return-from fsys-parse-number
316 (values tok nil))))
317 (let ((num (read-from-string tok)))
318 (if (numberp num)
319 (values num minusp)
320 (values tok nil))))
321 (values tok nil)))
322
323 (defun chaos-pushd (arg &optional (always-return nil))
324 (let ((path arg))
325 (cond (arg
326 (multiple-value-bind (num minusp)
327 (fsys-parse-number arg)
328 (cond ((numberp num)
329 (let ((new-stack (rotate-list *chaos-directory-stack*
330 num minusp)))
331 (if new-stack
332 (setq *chaos-directory-stack* new-stack)
333 (with-output-chaos-warning ()
334 (format t "directory stack is not so deep.")
335 (return-from chaos-pushd nil)))
336 (setq path (car *chaos-directory-stack*))))
337 (t (push :dymmy *chaos-directory-stack*)))
338 (if (chaos-cd path always-return)
339 t
340 (progn
341 (pop *chaos-directory-stack*)
342 nil))))
343 (t (if (<= (length *chaos-directory-stack*) 1)
344 (with-output-chaos-warning ()
345 (format t "No other directory.")
346 (return-from chaos-pushd nil))
347 (chaos-pushd "+1"))))))
348
349 (defun chaos-popd (&optional num)
350 (declare (ignore num))
351 (if (cdr *chaos-directory-stack*)
352 (let ((xd nil))
353 (pop *chaos-directory-stack*)
354 (setq xd (car *chaos-directory-stack*))
355 (chaos-cd xd))
356 ;; do nothing
357 ))
358
359 (defun chaos-cd (path &optional (always-return nil))
360 (let ((directory-path nil)
361 (ng nil))
362 (unless path
363 (setq path (user-homedir-pathname)))
364 #+GCL (let ((si::*break-enable* nil))
365 (if (setq directory-path (is-directory? path))
366 (system:chdir directory-path)
367 (setq ng t)))
368 #+CMU (if (setq directory-path (is-directory? path))
369 (progn (lisp::%set-default-directory directory-path))
370 (setq ng t))
371 #+EXCL (if (setq directory-path (is-directory? path))
372 (excl::chdir directory-path)
373 (setq ng t))
374 #+:openmcl (if (setq directory-path (is-directory? path))
375 (progn (ccl::%chdir (namestring directory-path)))
376 (setq ng t))
377 #+SBCL
378 (if (setq directory-path (is-directory? path))
379 (progn
380 (setq *default-pathname-defaults* directory-path)
381 (sb-posix:chdir directory-path))
382 (setq ng t))
383 #+(and :CCL (not :openmcl))
384 (if (setq directory-path (is-directory? path))
385 (set-mac-default-directory directory-path)
386 (setq ng t))
387 #+:CLISP (if (setq directory-path (is-directory? path))
388 (setq *default-pathname-defaults*
389 (ext:cd directory-path))
390 (setq ng t))
391 ;;
392 (cond (ng (with-output-chaos-warning ()
393 (format t "directory \"~a\" not found." path))
394 (if always-return
395 (return-from chaos-cd nil)
396 (chaos-error 'no-such-directory)))
397 (t (let ((cwd (chaos-pwd)))
398 #||
399 #-CLISP
400 (setq *default-pathname-defaults* cwd)
401 #+CLISP
402 ||#
403 (setq *default-pathname-defaults* (pathname cwd))
404 (if *chaos-directory-stack*
405 (setf (car *chaos-directory-stack*)
406 *default-pathname-defaults*)
407 (setf *chaos-directory-stack*
408 (list *default-pathname-defaults*))))))
409 *default-pathname-defaults*))
410
411 (defparameter *chaos-binary-magic* ";CHAOS_BINS_____")
412
413 (defun chaos-binary-file? (file)
414 (with-open-file (*standard-input* file :direction :input)
415 (let ((magic (read-line)))
416 (equal magic *chaos-binary-magic*))))
417
418 (defparameter *chaos-bin-suffix* '(".bin"))
419
420 (defun chaos-input-file (&key file ; input file name
421 proc ; procedure
422 load-path ; list of pathnames
423 (suffix '(".cafe" ".mod"))
424 args ; args to be passed to proc
425 (errorp t) ;
426 )
427 (let ((fname (chaos-probe-file file load-path suffix))
428 (bin-fname (chaos-probe-file file load-path *chaos-bin-suffix*)))
429 (when (and fname (is-directory? fname))
430 (with-output-chaos-error ('invalid-file)
431 (format t "~a is not a proper file." (namestring file)))
432 (return-from chaos-input-file nil))
433 (unless (or fname bin-fname)
434 (if errorp
435 (with-output-chaos-error ('no-such-file)
436 (format t "No such file: ~a" (namestring file)))
437 (return-from chaos-input-file nil)))
438 (when (and bin-fname fname (>= (file-write-date bin-fname)
439 (file-write-date fname)))
440 (setq fname bin-fname))
441 (unless fname
442 (setq fname bin-fname))
443
444 (when (equal *chaos-input-source* fname)
445 (return-from chaos-input-file t))
446
447 (let ((*chaos-input-source* fname)
448 (*chaos-input-level* (1+ *chaos-input-level*)))
449 (if (chaos-binary-file? fname)
450 (progn (load fname) fname)
451 (with-open-file (*standard-input* fname :direction :input)
452 (when (< *chaos-input-nesting-limit* *chaos-input-level*)
453 (with-output-chaos-warning ()
454 (format t "input nesting is ~d~%" *chaos-input-level*)
455 (print-next)
456 (princ "probable input loop (can increase *chaos-input-nesting-limit*)")))
457 (apply proc args)
458 fname)))))
459 ;;;
460 (defun set-search-path (paths)
461 (when (consp paths)
462 (setq paths (car paths)))
463 (let ((path nil))
464 (dolist (p (parse-with-delimiter paths #\:))
465 (push p path))
466 (setq *chaos-libpath* (nreverse path))))
467
468 (defun set-search-path-plus (paths)
469 (when (consp paths) (setq paths (car paths)))
470 (let ((path nil))
471 (dolist (p (parse-with-delimiter paths #\:))
472 (push p path))
473 (setq *chaos-libpath*
474 (append (nreverse path) *chaos-libpath*))))
475
476 (defun set-search-path-minus (paths)
477 (when (consp paths) (setq paths (car paths)))
478 (let ((path nil))
479 (dolist (p (parse-with-delimiter paths #\:))
480 (push p path))
481 (dolist (p path)
482 (if (not (member p *chaos-libpath* :test #'equal))
483 (with-output-chaos-warning ()
484 (format t "The path ~s is not in 'libpath'." p))
485 (setq *chaos-libpath* (remove p *chaos-libpath* :test #'equal))))
486 *chaos-libpath*))
487
488 (defun pr-search-path (&optional (stream *standard-output*))
489 (format stream "libpath = ~{~a~^:~}" *chaos-libpath*))
490
491 ;;;
492 ;;; INITIALIZATION
493 ;;;
494 (defun chaos-initialize-fsys ()
495 ;; any other?
496 (setq *chaos-directory-stack* nil)
497 (push (setq *default-pathname-defaults* (bare-chaos-pwd))
498 *chaos-directory-stack*)
499 ;; very old stuff
500 #+(and :ccl (not :openmcl))
501 (progn (setq *default-pathname-defaults*
502 (full-pathname (mac-default-directory)))
503 (set-mac-default-directory *default-pathname-defaults*)))
504
505 ;;; EOF