0.6.11.37:
[sbcl.git] / src / code / pathname.lisp
1 ;;;; the known-to-the-cross-compiler part of PATHNAME logic
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!IMPL")
13 \f
14 ;;;; data types used by pathnames
15
16 ;;; The HOST structure holds the functions that both parse the
17 ;;; pathname information into structure slot entries, and after
18 ;;; translation the inverse (unparse) functions.
19 (def!struct (host (:constructor nil))
20   (parse (required-argument) :type function)
21   (unparse (required-argument) :type function)
22   (unparse-host (required-argument) :type function)
23   (unparse-directory (required-argument) :type function)
24   (unparse-file (required-argument) :type function)
25   (unparse-enough (required-argument) :type function)
26   (customary-case (required-argument) :type (member :upper :lower)))
27
28 (def!struct (logical-host
29              (:include host
30                        (:parse #'parse-logical-namestring)
31                        (:unparse #'unparse-logical-namestring)
32                        (:unparse-host
33                         (lambda (x)
34                           (logical-host-name (%pathname-host x))))
35                        (:unparse-directory #'unparse-logical-directory)
36                        (:unparse-file #'unparse-unix-file)
37                        (:unparse-enough #'unparse-enough-namestring)
38                        (:customary-case :upper)))
39   (name "" :type simple-base-string)
40   (translations nil :type list)
41   (canon-transls nil :type list))
42
43 (def!method print-object ((logical-host logical-host) stream)
44   (print-unreadable-object (logical-host stream :type t)
45     (prin1 (logical-host-name logical-host) stream)))
46
47 ;;; A PATTERN is a list of entries and wildcards used for pattern
48 ;;; matches of translations.
49 (sb!xc:defstruct (pattern (:constructor make-pattern (pieces)))
50   (pieces nil :type list))
51 \f
52 ;;;; PATHNAME structures
53
54 ;;; the various magic tokens that are allowed to appear in pretty much
55 ;;; all pathname components
56 (sb!xc:deftype pathname-component-tokens ()
57   '(member nil :unspecific :wild))
58
59 (sb!xc:defstruct (pathname (:conc-name %pathname-)
60                            (:constructor %make-pathname (host
61                                                          device
62                                                          directory
63                                                          name
64                                                          type
65                                                          version))
66                            (:predicate pathnamep))
67   ;; the host (at present either a UNIX or logical host)
68   (host nil :type (or host null))
69   ;; the name of a logical or physical device holding files
70   (device nil :type (or simple-string pathname-component-tokens))
71   ;; a list of strings that are the component subdirectory components
72   (directory nil :type list)
73   ;; the filename
74   (name nil :type (or simple-string pattern pathname-component-tokens))
75   ;; the type extension of the file
76   (type nil :type (or simple-string pattern pathname-component-tokens))
77   ;; the version number of the file, a positive integer (not supported
78   ;; on standard Unix filesystems)
79   (version nil :type (or integer pathname-component-tokens (member :newest))))
80
81 ;;; Logical pathnames have the following format:
82 ;;;
83 ;;; logical-namestring ::=
84 ;;;      [host ":"] [";"] {directory ";"}* [name] ["." type ["." version]]
85 ;;;
86 ;;; host ::= word
87 ;;; directory ::= word | wildcard-word | **
88 ;;; name ::= word | wildcard-word
89 ;;; type ::= word | wildcard-word
90 ;;; version ::= pos-int | newest | NEWEST | *
91 ;;; word ::= {uppercase-letter | digit | -}+
92 ;;; wildcard-word ::= [word] '* {word '*}* [word]
93 ;;; pos-int ::= integer > 0
94 ;;;
95 ;;; Physical pathnames include all these slots and a device slot.
96
97 ;;; Logical pathnames are a subclass of PATHNAME. Their class
98 ;;; relations are mimicked using structures for efficency.
99 (sb!xc:defstruct (logical-pathname (:conc-name %logical-pathname-)
100                                    (:include pathname)
101                                    (:constructor %make-logical-pathname
102                                                  (host
103                                                   device
104                                                   directory
105                                                   name
106                                                   type
107                                                   version))))
108 \f
109 (defmacro-mundanely enumerate-search-list ((var pathname &optional result)
110                                            &body body)
111   #!+sb-doc
112   "Execute BODY with VAR bound to each successive possible expansion for
113    PATHNAME and then return RESULT. Note: if PATHNAME does not contain a
114    search-list, then BODY is executed exactly once. Everything is wrapped
115    in a block named NIL, so RETURN can be used to terminate early. Note:
116    VAR is *not* bound inside of RESULT."
117   (let ((body-name (gensym)))
118     `(block nil
119        (flet ((,body-name (,var)
120                 ,@body))
121          (%enumerate-search-list ,pathname #',body-name)
122          ,result))))
123