1 ;;; find-cmd.el --- Build a valid find(1) command with sexps
3 ;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 ;; Author: Philip Jackson <phil@shellarchive.co.uk>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; With this module you can build up a (hopefully) valid find(1)
26 ;; string ready for the command line. For example:
28 ;; (find-cmd '(prune (name ".svn" ".git" ".CVS"))
29 ;; '(and (or (name "*.pl" "*.pm" "*.t")
31 ;; (fstype "nfs" "ufs"))))
33 ;; will become (un-wrapped):
35 ;; "find '/home/phil/' \\( \\( -name '.svn' -or -name '.git' -or
36 ;; -name '.CVS' \\) -prune -or -true \\) \\( \\( \\( -name '*.pl'
37 ;; -or -name '*.pm' -or -name '*.t' \\) -or -mtime '+1' \\) -and \\(
38 ;; -fstype 'nfs' -or -fstype 'ufs' \\) \\)"
42 (defconst find-constituents
100 ;; normal options (always true)
107 (ignore_readdir_race .
(0))
108 (noignore_readdir_race .
(0))
125 ;; these need to be terminated with a ;
126 (exec .
(1 find-command t
))
127 (ok .
(1 find-command t
))
128 (execdir .
(1 find-command t
))
129 (okdir .
(1 find-command t
)))
130 "Holds details of each of the find options.
131 The car of each alist is the name. The cdr is minimum args, the
132 function used to join many occurrences of the argument together,
133 and whether or not to leave quotes off the string (non-nil means
134 the string will be quoted).")
137 (defun find-cmd (&rest subfinds
)
138 "Initiate the building of a find command.
141 \(find-cmd '\(prune \(name \".svn\" \".git\" \".CVS\"\)\)
142 '\(and \(or \(name \"*.pl\" \"*.pm\" \"*.t\"\)
144 \(fstype \"nfs\" \"ufs\"\)\)\)\)
146 `default-directory' is used as the initial search path. The
147 result is a string that should be ready for the command line."
149 "find " (shell-quote-argument (expand-file-name default-directory
)) " "
152 (mapconcat 'find-to-string subfinds
""))
154 (find-to-string (car subfinds
))))))
156 (defun find-and (form)
157 "And FORMs together, so:
158 \(and \(mtime \"+1\"\) \(name \"something\"\)\)
160 find . \\\( -mtime '+1' -and -name 'something' \\\)"
161 (if (< (length form
) 2)
162 (find-to-string (car form
))
164 (mapconcat 'find-to-string form
"-and ")
167 (defun find-or (form)
168 "Or FORMs together, so:
169 \(or \(mtime \"+1\"\) \(name \"something\"\)\)
171 find . \\\( -mtime '+1' -or -name 'something' \\\)"
172 (if (< (length form
) 2)
173 (find-to-string (car form
))
175 (mapconcat 'find-to-string form
"-or ")
178 (defun find-not (form)
179 "Or FORMs together and prefix with a -not, so:
180 \(not \(mtime \"+1\"\) \(name \"something\"\)\)
182 -not \\\( -mtime '+1' -or -name 'something' \\\)
183 If you wanted the FORMs -and(ed) together instead then this would
185 \(not \(and \(mtime \"+1\"\) \(name \"something\"\)\)\)"
186 (concat "-not " (find-or (mapcar 'find-to-string form
))))
188 (defun find-prune (form)
189 "-or together FORMs postfix '-prune' and then -or that with a
191 \(prune \(name \".svn\" \".git\"\)\) \(name \"*.pm\"\)
192 will produce (unwrapped):
193 \\\( \\\( \\\( -name '.svn' -or -name '.git' \\\) /
194 -prune -or -true \\\) -and -name '*.pm' \\\)"
197 (concat (find-or (mapcar 'find-to-string form
)) (find-generic "prune"))
198 (find-generic "true"))))
200 (defun find-generic (option &optional oper argcount args dont-quote
)
201 "Allow an arbitrary string to be used as a form.
202 OPTION is the name of the form, OPER is the function used to either
203 OR or AND multiple results together. ARGCOUNT is the minimum of
204 args that OPTION can receive and ARGS are the arguments for OPTION.
205 If DONT-QUOTE is non-nil, arguments are quoted for passing them to
207 (when (and (numberp argcount
) (< (length args
) argcount
))
208 (error "'%s' needs at least %d arguments" option argcount
))
209 (let ((oper (or oper
'find-or
)))
210 (if (and args
(length args
))
211 (funcall oper
(mapcar (lambda (x)
216 (shell-quote-argument x
)
219 (concat "-" option
" "))))
221 (defun find-command (form)
222 "For each item in FORM add a terminating semi-colon and turn
223 them into valid switches. The result is -and(ed) together."
224 (find-and (mapcar (lambda (x)
225 (concat (find-to-string x
) "\\; "))
228 (defun find-to-string (form)
229 "Parse FORM to produce a set of valid find arguments."
234 (let ((option (cdr (assoc (car form
) find-constituents
))))
236 ((and (symbolp option
) (fboundp option
))
237 (funcall option
(cdr form
)))
239 (let ((option (symbol-name (car form
)))
240 (argcnt (car option
))
242 (dont-quote (car (cddr option
))))
244 (find-generic option oper argcnt
(cdr form
) dont-quote
))))
246 (error "Sorry I don't know how to handle '%s'" (car form
))))))))
250 ;;; find-cmd.el ends here