find-cmd.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ;;; find-cmd.el --- Build a valid find(1) command with sexps
  2. ;; Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. ;; Author: Philip Jackson <phil@shellarchive.co.uk>
  4. ;; Version: 0.6
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; With this module you can build up a (hopefully) valid find(1)
  18. ;; string ready for the command line. For example:
  19. ;; (find-cmd '(prune (name ".svn" ".git" ".CVS"))
  20. ;; '(and (or (name "*.pl" "*.pm" "*.t")
  21. ;; (mtime "+1"))
  22. ;; (fstype "nfs" "ufs"))))
  23. ;; will become (un-wrapped):
  24. ;; "find '/home/phil/' \\( \\( -name '.svn' -or -name '.git' -or
  25. ;; -name '.CVS' \\) -prune -or -true \\) \\( \\( \\( -name '*.pl'
  26. ;; -or -name '*.pm' -or -name '*.t' \\) -or -mtime '+1' \\) -and \\(
  27. ;; -fstype 'nfs' -or -fstype 'ufs' \\) \\)"
  28. ;;; Code:
  29. (require 'grep)
  30. (defconst find-constituents
  31. '((and . find-and)
  32. (not . find-not)
  33. (or . find-or)
  34. (a . find-and)
  35. (n . find-not)
  36. (o . find-or)
  37. (prune . find-prune)
  38. ;; switches
  39. (L . (0))
  40. (P . (0))
  41. (H . (0))
  42. ;; generic tests
  43. (amin . (1))
  44. (anewer . (1))
  45. (atime . (1))
  46. (cmin . (1))
  47. (cnewer . (1))
  48. (ctime . (1))
  49. (empty . (0))
  50. (executable . (0))
  51. (false . (0))
  52. (fstype . (1))
  53. (gid . (1))
  54. (group . (1))
  55. (ilname . (1))
  56. (iname . (1))
  57. (inum . (1))
  58. (ipath . (1))
  59. (iregex . (1))
  60. (iwholename . (1))
  61. (links . (1))
  62. (lname . (1))
  63. (mmin . (1))
  64. (mtime . (1))
  65. (name . (1))
  66. (newer . (1))
  67. (nogroup . (0))
  68. (nouser . (0))
  69. (path . (1))
  70. (perm . (0))
  71. (readable . (0))
  72. (regex . (1))
  73. (samefile . (1))
  74. (size . (1))
  75. (true . (0))
  76. (type . (1))
  77. (uid . (1))
  78. (used . (1))
  79. (user . (1))
  80. (wholename . (1))
  81. (writable . (0))
  82. (xtype . (nil))
  83. ;; normal options (always true)
  84. (daystart . (0))
  85. (depth . (0))
  86. (maxdepth . (1))
  87. (mindepth . (1))
  88. (mount . (0))
  89. (noleaf . (0))
  90. (ignore_readdir_race . (0))
  91. (noignore_readdir_race . (0))
  92. (regextype . (1))
  93. (xdev . (0))
  94. ;; actions
  95. (delete . (0))
  96. (print0 . (0))
  97. (printf . (1))
  98. (fprintf . (2))
  99. (print . (0))
  100. (fprint0 . (1))
  101. (fprint . (1))
  102. (ls . (0))
  103. (fls . (1))
  104. (prune . (0))
  105. (quit . (0))
  106. ;; these need to be terminated with a ;
  107. (exec . (1 find-command t))
  108. (ok . (1 find-command t))
  109. (execdir . (1 find-command t))
  110. (okdir . (1 find-command t)))
  111. "Holds details of each of the find options.
  112. The car of each alist is the name. The cdr is minimum args, the
  113. function used to join many occurrences of the argument together,
  114. and whether or not to leave quotes off the string (non-nil means
  115. the string will be quoted).")
  116. ;;;###autoload
  117. (defun find-cmd (&rest subfinds)
  118. "Initiate the building of a find command.
  119. For example:
  120. \(find-cmd \\='(prune (name \".svn\" \".git\" \".CVS\"))
  121. \\='(and (or (name \"*.pl\" \"*.pm\" \"*.t\")
  122. (mtime \"+1\"))
  123. (fstype \"nfs\" \"ufs\"))))
  124. `default-directory' is used as the initial search path. The
  125. result is a string that should be ready for the command line."
  126. ;; FIXME: Provide a version that returns a list of strings (ready to pass to
  127. ;; call-process).
  128. (concat find-program " "
  129. (shell-quote-argument (expand-file-name default-directory)) " "
  130. (cond
  131. ((cdr subfinds)
  132. (mapconcat #'find-to-string subfinds ""))
  133. (t
  134. (find-to-string (car subfinds))))))
  135. (defun find-and (form)
  136. "And FORMs together, so:
  137. (and (mtime \"+1\") (name \"something\"))
  138. will produce:
  139. find . \\( -mtime +1 -and -name something \\)"
  140. (if (< (length form) 2)
  141. (find-to-string (car form))
  142. (concat "\\( "
  143. (mapconcat #'find-to-string form "-and ")
  144. "\\) ")))
  145. (defun find-or (form)
  146. "Or FORMs together, so:
  147. (or (mtime \"+1\") (name \"something\"))
  148. will produce:
  149. find . \\( -mtime +1 -or -name something \\)"
  150. (if (< (length form) 2)
  151. (find-to-string (car form))
  152. (concat "\\( "
  153. (mapconcat #'find-to-string form "-or ")
  154. "\\) ")))
  155. (defun find-not (form)
  156. "Or FORMs together and prefix with a -not, so:
  157. (not (mtime \"+1\") (name \"something\"))
  158. will produce:
  159. -not \\( -mtime +1 -or -name something \\)
  160. If you wanted the FORMs -and(ed) together instead then this would
  161. suffice:
  162. (not (and (mtime \"+1\") (name \"something\")))"
  163. (concat "-not " (find-or (mapcar #'find-to-string form))))
  164. (defun find-prune (form)
  165. "-or together FORMs postfix `-prune' and then -or that with a
  166. -true, so:
  167. ((prune (name \".svn\" \".git\")) (name \"*.pm\"))
  168. will produce (unwrapped):
  169. \\( \\( \\( -name .svn -or -name .git \\) /
  170. -prune -or -true \\) -and -name *.pm \\)"
  171. (find-or
  172. (list
  173. (concat (find-or (mapcar #'find-to-string form)) (find-generic "prune"))
  174. (find-generic "true"))))
  175. (defun find-generic (option &optional oper argcount args dont-quote)
  176. "Allow an arbitrary string to be used as a form.
  177. OPTION is the name of the form, OPER is the function used to either
  178. OR or AND multiple results together. ARGCOUNT is the minimum of
  179. args that OPTION can receive and ARGS are the arguments for OPTION.
  180. If DONT-QUOTE is non-nil, arguments are quoted for passing them to
  181. the shell."
  182. (when (and (numberp argcount) (< (length args) argcount))
  183. (error "`%s' needs at least %d arguments" option argcount))
  184. (let ((oper (or oper 'find-or)))
  185. (if (and args (length args))
  186. (funcall oper (mapcar (lambda (x)
  187. (concat "-" option
  188. (if dont-quote
  189. (concat " " x " ")
  190. (concat " "
  191. (shell-quote-argument x)
  192. " "))))
  193. args))
  194. (concat "-" option " "))))
  195. (defun find-command (form)
  196. "For each item in FORM add a terminating semi-colon and turn
  197. them into valid switches. The result is -and(ed) together."
  198. (find-and (mapcar (lambda (x)
  199. (concat (find-to-string x) "\\; "))
  200. form)))
  201. (defun find-to-string (form)
  202. "Parse FORM to produce a set of valid find arguments."
  203. (cond
  204. ((stringp form)
  205. form)
  206. ((consp form)
  207. (let ((option (cdr (assoc (car form) find-constituents))))
  208. (cond
  209. ((and (symbolp option) (fboundp option))
  210. (funcall option (cdr form)))
  211. ((consp option)
  212. (let ((option (symbol-name (car form)))
  213. (argcnt (car option))
  214. (oper (cadr option))
  215. (dont-quote (car (cddr option))))
  216. (find-to-string
  217. (find-generic option oper argcnt (cdr form) dont-quote))))
  218. (t
  219. (error "Sorry I don't know how to handle `%s'" (car form))))))))
  220. (provide 'find-cmd)
  221. ;;; find-cmd.el ends here