configuration.lisp 12.3 KB
Newer Older
1
2
3
;;;; ---------------------------------------------------------------------------
;;;; Generic support for configuration files

4
5
(asdf/package:define-package :asdf/configuration
  (:recycle :asdf/configuration :asdf)
6
7
  (:use :common-lisp :asdf/compatibility :asdf/utility
   :asdf/pathname :asdf/stream :asdf/os :asdf/image)
8
9
10
11
12
13
14
15
  (:export
   #:get-folder-path
   #:user-configuration-directories #:system-configuration-directories
   #:in-first-directory
   #:in-user-configuration-directory #:in-system-configuration-directory
   #:validate-configuration-form #:validate-configuration-file #:validate-configuration-directory
   #:configuration-inheritance-directive-p
   #:report-invalid-form #:invalid-configuration #:*ignored-configuration-form*
16
   #:*clear-configuration-hook* #:clear-configuration #:register-clear-configuration-hook
17
   #:resolve-location #:location-designator-p #:location-function-p #:*here-directory*
18
   #:resolve-relative-location-component #:resolve-absolute-location-component))
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
(in-package :asdf/configuration)

(define-condition invalid-configuration ()
  ((form :reader condition-form :initarg :form)
   (location :reader condition-location :initarg :location)
   (format :reader condition-format :initarg :format)
   (arguments :reader condition-arguments :initarg :arguments :initform nil))
  (:report (lambda (c s)
               (format s (compatfmt "~@<~? (will be skipped)~@:>")
                       (condition-format c)
                       (list* (condition-form c) (condition-location c)
                              (condition-arguments c))))))

(defun* get-folder-path (folder)
  (or ;; this semi-portably implements a subset of the functionality of lispworks' sys:get-folder-path
   #+(and lispworks mswindows) (sys:get-folder-path folder)
   ;; read-windows-registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData
   (ecase folder
    (:local-appdata (getenv-absolute-directory "LOCALAPPDATA"))
    (:appdata (getenv-absolute-directory "APPDATA"))
    (:common-appdata (or (getenv-absolute-directory "ALLUSERSAPPDATA")
                         (subpathname* (getenv-absolute-directory "ALLUSERSPROFILE") "Application Data/"))))))

(defun* user-configuration-directories ()
  (let ((dirs
         `(,@(when (os-unix-p)
               (cons
                (subpathname* (getenv-absolute-directory "XDG_CONFIG_HOME") "common-lisp/")
                (loop :for dir :in (getenv-absolute-directories "XDG_CONFIG_DIRS")
                  :collect (subpathname* dir "common-lisp/"))))
           ,@(when (os-windows-p)
               `(,(subpathname* (get-folder-path :local-appdata) "common-lisp/config/")
                 ,(subpathname* (get-folder-path :appdata) "common-lisp/config/")))
           ,(subpathname (user-homedir) ".config/common-lisp/"))))
    (remove-duplicates (remove-if-not #'absolute-pathname-p dirs)
                       :from-end t :test 'equal)))

(defun* system-configuration-directories ()
  (cond
    ((os-unix-p) '(#p"/etc/common-lisp/"))
    ((os-windows-p)
60
61
     (if-bind (it (subpathname* (get-folder-path :common-appdata) "common-lisp/config/"))
       (list it)))))
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

(defun* in-first-directory (dirs x &key (direction :input))
  (loop :with fun = (ecase direction
                      ((nil :input :probe) 'probe-file*)
                      ((:output :io) 'identity))
    :for dir :in dirs
    :thereis (and dir (funcall fun (merge-pathnames* x (ensure-directory-pathname dir))))))

(defun* in-user-configuration-directory (x &key (direction :input))
  (in-first-directory (user-configuration-directories) x :direction direction))
(defun* in-system-configuration-directory (x &key (direction :input))
  (in-first-directory (system-configuration-directories) x :direction direction))

(defun* configuration-inheritance-directive-p (x)
  (let ((kw '(:inherit-configuration :ignore-inherited-configuration)))
    (or (member x kw)
        (and (length=n-p x 1) (member (car x) kw)))))

(defun* report-invalid-form (reporter &rest args)
  (etypecase reporter
    (null
     (apply 'error 'invalid-configuration args))
    (function
     (apply reporter args))
    ((or symbol string)
     (apply 'error reporter args))
    (cons
     (apply 'apply (append reporter args)))))

(defvar *ignored-configuration-form* nil)

(defun* validate-configuration-form (form tag directive-validator
                                    &key location invalid-form-reporter)
  (unless (and (consp form) (eq (car form) tag))
    (setf *ignored-configuration-form* t)
    (report-invalid-form invalid-form-reporter :form form :location location)
    (return-from validate-configuration-form nil))
  (loop :with inherit = 0 :with ignore-invalid-p = nil :with x = (list tag)
    :for directive :in (cdr form)
    :when (cond
            ((configuration-inheritance-directive-p directive)
             (incf inherit) t)
            ((eq directive :ignore-invalid-entries)
             (setf ignore-invalid-p t) t)
            ((funcall directive-validator directive)
             t)
            (ignore-invalid-p
             nil)
            (t
             (setf *ignored-configuration-form* t)
             (report-invalid-form invalid-form-reporter :form directive :location location)
             nil))
    :do (push directive x)
    :finally
    (unless (= inherit 1)
      (report-invalid-form invalid-form-reporter
             :arguments (list (compatfmt "~@<One and only one of ~S or ~S is required.~@:>")
                              :inherit-configuration :ignore-inherited-configuration)))
    (return (nreverse x))))

(defun* validate-configuration-file (file validator &key description)
  (let ((forms (read-file-forms file)))
    (unless (length=n-p forms 1)
      (error (compatfmt "~@<One and only one form allowed for ~A. Got: ~3i~_~S~@:>~%")
             description forms))
    (funcall validator (car forms) :location file)))

(defun* validate-configuration-directory (directory tag validator &key invalid-form-reporter)
  "Map the VALIDATOR across the .conf files in DIRECTORY, the TAG will
be applied to the results to yield a configuration form.  Current
values of TAG include :source-registry and :output-translations."
  (let ((files (sort (ignore-errors
                       (remove-if
135
                        'hidden-pathname-p
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
136
                        (directory* (make-pathname :name *wild* :type "conf" :defaults directory))))
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                     #'string< :key #'namestring)))
    `(,tag
      ,@(loop :for file :in files :append
          (loop :with ignore-invalid-p = nil
            :for form :in (read-file-forms file)
            :when (eq form :ignore-invalid-entries)
              :do (setf ignore-invalid-p t)
            :else
              :when (funcall validator form)
                :collect form
              :else
                :when ignore-invalid-p
                  :do (setf *ignored-configuration-form* t)
                :else
                  :do (report-invalid-form invalid-form-reporter :form form :location file)))
      :inherit-configuration)))

154
(defun* resolve-relative-location-component (x &key want-directory wilden)
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
  (ensure-pathname
   (etypecase x
     (pathname x)
     (string (parse-unix-namestring
              x :want-directory want-directory))
     (cons
      (if (null (cdr x))
          (resolve-relative-location-component
           (car x) :want-directory want-directory :wilden wilden)
          (let* ((car (resolve-relative-location-component
                       (car x) :want-directory t :wilden nil)))
            (merge-pathnames*
             (resolve-relative-location-component
              (cdr x) :want-directory want-directory :wilden wilden)
             car))))
     ((eql :*/) *wild-directory*)
     ((eql :**/) *wild-inferiors*)
     ((eql :*.*.*) *wild-file*)
     ((eql :implementation)
      (parse-unix-namestring
       (implementation-identifier) :want-directory t))
     ((eql :implementation-type)
      (parse-unix-namestring
       (string-downcase (implementation-type)) :want-directory t))
     ((eql :hostname)
      (parse-unix-namestring (hostname) :want-directory t)))
  :wilden (and wilden (not (pathnamep x)) (not (member x '(:*/ :**/ :*.*.*))))
  :want-relative t))
183
184
185
186
187
188

(defvar *here-directory* nil
  "This special variable is bound to the currect directory during calls to
PROCESS-SOURCE-REGISTRY in order that we be able to interpret the :here
directive.")

189
190
(defvar *user-cache* nil
  "A specification as per RESOLVE-LOCATION of where the user keeps his FASL cache")
191

192
193
194
195
196
197
198
199
200
201
202
203
204
(defun compute-user-cache ()
  (setf *user-cache*
        (flet ((try (x &rest sub) (and x `(,x ,@sub))))
          (or
           (try (getenv-absolute-directory "XDG_CACHE_HOME") "common-lisp" :implementation)
           (when (os-windows-p)
             (try (or (get-folder-path :local-appdata)
                      (get-folder-path :appdata))
                  "common-lisp" "cache" :implementation))
           '(:home ".cache" "common-lisp" :implementation)))))
(register-image-restore-hook 'compute-user-cache)

(defun* resolve-absolute-location-component (x &key want-directory wilden)
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
  (ensure-pathname
   (etypecase x
     (pathname x)
     (string
      (let ((p #-mcl (parse-namestring x)
               #+mcl (probe-posix x)))
        #+mcl (unless p (error "POSIX pathname ~S does not exist" x))
        (if want-directory (ensure-directory-pathname p) p)))
     (cons
      (return-from resolve-absolute-location-component
        (if (null (cdr x))
            (resolve-absolute-location-component
             (car x) :want-directory want-directory :wilden wilden)
            (merge-pathnames*
             (resolve-relative-location-component
              (cdr x) :want-directory want-directory :wilden wilden)
             (resolve-absolute-location-component
              (car x) :want-directory t :wilden nil)))))
     ((eql :root)
      ;; special magic! we return a relative pathname,
      ;; but what it means to the output-translations is
      ;; "relative to the root of the source pathname's host and device".
      (return-from resolve-absolute-location-component
        (let ((p (make-pathname* :directory '(:relative))))
          (if wilden (wilden p) p))))
     ((eql :home) (user-homedir))
     ((eql :here) (resolve-absolute-location-component
                   *here-directory* :want-directory t :wilden nil))
     ((eql :user-cache) (resolve-absolute-location-component
                         *user-cache* :want-directory t :wilden nil)))
   :wilden (and wilden (not (pathnamep x)))
   :want-absolute t))
237

238
239
(defun* resolve-location (x &key want-directory wilden directory)
  (when directory (setf want-directory t)) ;; :directory backward compatibility, until 2014-01-16.
240
  (if (atom x)
241
      (resolve-absolute-location-component x :want-directory want-directory :wilden wilden)
242
      (loop :with path = (resolve-absolute-location-component
243
                          (car x) :want-directory (and (or want-directory (cdr x)) t)
244
245
                          :wilden (and wilden (null (cdr x))))
        :for (component . morep) :on (cdr x)
246
        :for dir = (and (or morep want-directory) t)
247
248
249
        :for wild = (and wilden (not morep))
        :do (setf path (merge-pathnames*
                        (resolve-relative-location-component
250
                         component :want-directory dir :wilden wild)
251
252
253
254
255
256
                        path))
        :finally (return path))))

(defun* location-designator-p (x)
  (flet ((absolute-component-p (c)
           (typep c '(or string pathname
257
                      (member :root :home :here :user-cache))))
258
259
         (relative-component-p (c)
           (typep c '(or string pathname
260
                      (member :*/ :**/ :*.*.* :implementation :implementation-type)))))
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    (or (typep x 'boolean)
        (absolute-component-p x)
        (and (consp x) (absolute-component-p (first x)) (every #'relative-component-p (rest x))))))

(defun* location-function-p (x)
  (and
   (length=n-p x 2)
   (eq (car x) :function)
   (or (symbolp (cadr x))
       (and (consp (cadr x))
            (eq (caadr x) 'lambda)
            (length=n-p (cadadr x) 2)))))

(defvar *clear-configuration-hook* '())

276
277
278
(defun* register-clear-configuration-hook (hook-function &optional call-now-p)
  (register-hook-function '*clear-configuration-hook* hook-function call-now-p))

279
(defun* clear-configuration ()
280
  (call-functions *clear-configuration-hook*))
281

282
(register-image-dump-hook 'clear-configuration)