/usr/share/common-lisp/source/metatilities-base/dev/l0-files.lisp is in cl-metatilities-base 20170403-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | (in-package #:metatilities)
(define-condition invalid-stream-specifier-error (error)
((stream-specifier
:initarg :stream-specifier
:reader stream-specifier)
(stream-specifier-direction
:initarg :stream-specifier-direction
:reader stream-specifier-direction)
(stream-specifier-args
:initform nil
:initarg :stream-specifier-args
:reader stream-specifier-args))
(:report
(lambda (condition stream)
(format stream "~&Unable to make an ~a stream with specifier ~s~@[ and arguments ~{~s~^, ~}~]"
(stream-specifier-direction condition)
(stream-specifier condition)
(stream-specifier-args condition)))))
#+(or)
(error 'invalid-stream-specifier-error
:stream-specifier nil
:stream-specifier-direction :input
:stream-specifier-args '(foo bar))
(defun invalid-stream-specifier-error (specifier direction &optional args)
(error 'invalid-stream-specifier-error
:stream-specifier specifier
:stream-specifier-direction direction
:stream-specifier-args args))
(defun pathname-name+type (pathname)
"Returns a new pathname consisting of only the name and type from
a non-wild pathname."
(make-pathname :name (pathname-name pathname)
:type (pathname-type pathname)))
(defun physical-pathname-directory-separator ()
"Returns a string representing the separator used to delimit directories
in a physical pathname. For example, Digitool's MCL would return \":\"
whereas OpenMCL would return \"/\"."
(let* ((directory-1 "foo")
(directory-2 "bar")
(pn (namestring
(translate-logical-pathname
(make-pathname
:host nil
:directory `(:absolute ,directory-1 ,directory-2)
:name nil
:type nil))))
(foo-pos (search directory-1 pn :test #'char-equal))
(bar-pos (search directory-2 pn :test #'char-equal)))
(subseq pn (+ foo-pos (length directory-1)) bar-pos)))
(defun relative-pathname (relative-to pathname &key name type)
(let ((directory (pathname-directory pathname)))
(when (eq (car directory) :absolute)
(setf (car directory) :relative))
(merge-pathnames
(make-pathname :name (or name (pathname-name pathname))
:type (or type (pathname-type pathname))
:directory directory
)
relative-to)))
(defun directory-pathname-p (pathname)
(and (member (pathname-name pathname) (list nil :unspecified))
(member (pathname-type pathname) (list nil :unspecified))))
(defun ensure-directory-pathname (pathname)
(if (directory-pathname-p pathname)
pathname
(make-pathname
:directory `(,@(pathname-directory pathname)
,(namestring (pathname-name+type pathname))))))
(defun pathname-samep (p1 p2)
"Returns true if the logical translations of `p1` and `p2` have
the same (`string=`) namestrings."
(and p1 p2
(typep p1 '(or string pathname))
(typep p2 '(or string pathname))
(string= (namestring (translate-logical-pathname p1))
(namestring (translate-logical-pathname p2)))))
(defgeneric make-stream-from-specifier (specifier direction &rest args)
(:documentation "Create and return a stream from specifier, direction and any other argsuments"))
(defgeneric close-stream-specifier (steam)
(:documentation "Close a stream and handle other bookkeeping as appropriate."))
(defmethod make-stream-from-specifier ((stream-specifier stream)
(direction symbol) &rest args)
(declare (ignore args))
(values stream-specifier nil))
(defmethod make-stream-from-specifier ((stream-specifier (eql t))
(direction symbol) &rest args)
(declare (ignore args))
(values *standard-output* nil))
(defmethod make-stream-from-specifier ((stream-specifier (eql nil))
(direction symbol) &rest args)
(declare (ignore args))
(values (make-string-output-stream) t))
(defmethod make-stream-from-specifier ((stream-specifier (eql nil))
(direction (eql :input)) &rest args)
(invalid-stream-specifier-error stream-specifier direction args))
(defmethod make-stream-from-specifier ((stream-specifier (eql t))
(direction (eql :input)) &rest args)
(invalid-stream-specifier-error stream-specifier direction args))
(defmethod make-stream-from-specifier ((stream-specifier (eql :none))
(direction symbol) &rest args)
(declare (ignore args))
(values nil nil))
(defmethod make-stream-from-specifier ((stream-specifier pathname)
(direction symbol) &rest args)
(values (apply #'open stream-specifier :direction direction args)
t))
(defmethod make-stream-from-specifier ((stream-specifier string)
(direction symbol) &rest args)
(declare (ignore args))
(values (make-string-input-stream stream-specifier) nil))
(defmethod make-stream-from-specifier ((stream-specifier string)
(direction (eql :output)) &rest args)
(apply #'make-stream-from-specifier
(pathname stream-specifier) direction args))
(defmethod close-stream-specifier (s)
(close s)
(values nil))
(defmethod close-stream-specifier ((s string-stream))
(prog1
(values (get-output-stream-string s))
(close s)))
;;;;
(defun map-forms (input fn &key (ignore-read-errors-p t))
(with-input (stream input)
(flet ((next ()
(if ignore-read-errors-p
(ignore-errors (read stream nil :eof))
(read stream nil :eof))))
(loop for f = (next) then (next)
until (eq f :eof) do
(handler-case
(funcall fn f)
(reader-error (c) (print c)))))))
(defun map-lines (input fn &key include-empty-lines-p filter)
(with-input (s input)
(loop for line = (read-line s nil :eof)
until (eq line :eof)
when (and
(or include-empty-lines-p
(some (complement #'whitespacep) line))
(or (not filter)
(funcall filter line)))
do (funcall fn line))))
(defun collect-forms (input &key filter transform)
(let ((result nil))
(map-forms input (lambda (form)
(when (or (not filter)
(funcall filter form))
(push (if transform (funcall transform form) form)
result))))
(nreverse result)))
(defun collect-lines (input &rest args &key
count-empty-lines-p filter transform)
(declare (ignore count-empty-lines-p filter))
(unless transform (setf transform #'identity))
(remf args :transform)
(let ((results nil))
(apply #'map-lines
input (lambda (line) (push (funcall transform line) results))
args)
(nreverse results)))
;;;;
;; find . -name "_darcs" -type d -maxdepth 2 -exec ...
(defun map-matching-files (root expression fn &key max-depth)
(let ((test (compile-expression expression)))
(labels ((make-wild (path)
(make-pathname
:name :wild
:type :wild
:directory
(if (directory-pathname-p path)
`(,@(pathname-directory path))
`(,@(pathname-directory path)
,(namestring (pathname-name+type path))))
:defaults path))
(do-it (root depth)
(when (and max-depth (>= depth max-depth))
(return-from do-it nil))
(dolist (match (directory (make-wild root)))
(when (funcall test match)
(funcall fn match))
(when (probe-file (ensure-directory-pathname match))
(do-it match (1+ depth))))))
(do-it root 0))))
(defun compile-expression (expression)
(if (functionp expression)
expression
;; not done
(constantly t)))
#+(or)
(map-matching-files
"~/darcs"
(lambda (pathname)
(and (probe-file (ensure-directory-pathname pathname))
(string= "_darcs" (namestring (pathname-name+type pathname)))))
#'print
:max-depth 2)
#+(or)
(map-matching-files
"~/darcs"
(lambda (pathname)
(and (not (probe-file (ensure-directory-pathname pathname)))
(string= "common-lisp.net" (namestring (pathname-name+type pathname)))))
#'print
:max-depth 2)
(defun collect-matching-files (root expression &key max-depth)
(let ((results nil))
(map-matching-files
root expression
(lambda (file)
(push file results))
:max-depth max-depth)
(nreverse results)))
(defun file-newer-than-file-p (file1 file2)
"Compares the write dates of `file1' and `file' and returns t
if `file' is newer than `file2' or if it cannot be determined.
`file1' is usually the source file and `file2' the object file."
;; File write dates default to 0 and 1 so that if they can't be
;; determined, the file is recompiled, just to be safe.
(< (or (file-write-date file2) 0)
(or (file-write-date file1) 1)))
(defun pathname-without-name+type (pathname)
"Chop off any name and type information from a pathname."
(make-pathname :name nil :type nil :defaults pathname)
#+(or)
(make-pathname :name :unspecific :type :unspecific :defaults pathname))
|