find-cmd.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ;;; find-cmd.el --- Build a valid find(1) command with sexps
  2. ;; Copyright (C) 2008-2012 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. (defconst find-constituents
  30. '((and . find-and)
  31. (not . find-not)
  32. (or . find-or)
  33. (a . find-and)
  34. (n . find-not)
  35. (o . find-or)
  36. (prune . find-prune)
  37. ;; switches
  38. (L . (0))
  39. (P . (0))
  40. (H . (0))
  41. ;; generic tests
  42. (amin . (1))
  43. (anewer . (1))
  44. (atime . (1))
  45. (cmin . (1))
  46. (cnewer . (1))
  47. (ctime . (1))
  48. (empty . (0))
  49. (false . (0))
  50. (fstype . (1))
  51. (gid . (1))
  52. (group . (1))
  53. (ilname . (1))
  54. (iname . (1))
  55. (inum . (1))
  56. (iwholename . (1))
  57. (iregex . (1))
  58. (links . (1))
  59. (lname . (1))
  60. (mmin . (1))
  61. (mtime . (1))
  62. (name . (1))
  63. (newer . (1))
  64. (nouser . (0))
  65. (nogroup . (0))
  66. (path . (1))
  67. (perm . (0))
  68. (regex . (1))
  69. (wholename . (1))
  70. (size . (1))
  71. (true . (0))
  72. (type . (1))
  73. (uid . (1))
  74. (used . (1))
  75. (user . (1))
  76. (xtype . (nil))
  77. ;; normal options (always true)
  78. (depth . (0))
  79. (maxdepth . (1))
  80. (mindepth . (1))
  81. (mount . (0))
  82. (noleaf . (0))
  83. (xdev . (0))
  84. (ignore_readdir_race . (0))
  85. (noignore_readdir_race . (0))
  86. ;; actions
  87. (delete . (0))
  88. (print0 . (0))
  89. (printf . (1))
  90. (fprintf . (2))
  91. (print . (0))
  92. (fprint0 . (1))
  93. (fprint . (1))
  94. (ls . (0))
  95. (fls . (1))
  96. (prune . (0))
  97. (quit . (0))
  98. ;; these need to be terminated with a ;
  99. (exec . (1 find-command t))
  100. (ok . (1 find-command t))
  101. (execdir . (1 find-command t))
  102. (okdir . (1 find-command t)))
  103. "Holds details of each of the find options.
  104. The car of each alist is the name. The cdr is minimum args, the
  105. function used to join many occurrences of the argument together,
  106. and whether or not to leave quotes off the string (non-nil means
  107. the string will be quoted).")
  108. ;;;###autoload
  109. (defun find-cmd (&rest subfinds)
  110. "Initiate the building of a find command.
  111. For example:
  112. \(find-cmd '\(prune \(name \".svn\" \".git\" \".CVS\"\)\)
  113. '\(and \(or \(name \"*.pl\" \"*.pm\" \"*.t\"\)
  114. \(mtime \"+1\"\)\)
  115. \(fstype \"nfs\" \"ufs\"\)\)\)\)
  116. `default-directory' is used as the initial search path. The
  117. result is a string that should be ready for the command line."
  118. (concat
  119. "find " (shell-quote-argument (expand-file-name default-directory)) " "
  120. (cond
  121. ((cdr subfinds)
  122. (mapconcat 'find-to-string subfinds ""))
  123. (t
  124. (find-to-string (car subfinds))))))
  125. (defun find-and (form)
  126. "And FORMs together, so:
  127. \(and \(mtime \"+1\"\) \(name \"something\"\)\)
  128. will produce:
  129. find . \\\( -mtime '+1' -and -name 'something' \\\)"
  130. (if (< (length form) 2)
  131. (find-to-string (car form))
  132. (concat "\\( "
  133. (mapconcat 'find-to-string form "-and ")
  134. "\\) ")))
  135. (defun find-or (form)
  136. "Or FORMs together, so:
  137. \(or \(mtime \"+1\"\) \(name \"something\"\)\)
  138. will produce:
  139. find . \\\( -mtime '+1' -or -name 'something' \\\)"
  140. (if (< (length form) 2)
  141. (find-to-string (car form))
  142. (concat "\\( "
  143. (mapconcat 'find-to-string form "-or ")
  144. "\\) ")))
  145. (defun find-not (form)
  146. "Or FORMs together and prefix with a -not, so:
  147. \(not \(mtime \"+1\"\) \(name \"something\"\)\)
  148. will produce:
  149. -not \\\( -mtime '+1' -or -name 'something' \\\)
  150. If you wanted the FORMs -and(ed) together instead then this would
  151. suffice:
  152. \(not \(and \(mtime \"+1\"\) \(name \"something\"\)\)\)"
  153. (concat "-not " (find-or (mapcar 'find-to-string form))))
  154. (defun find-prune (form)
  155. "-or together FORMs postfix '-prune' and then -or that with a
  156. -true, so:
  157. \(prune \(name \".svn\" \".git\"\)\) \(name \"*.pm\"\)
  158. will produce (unwrapped):
  159. \\\( \\\( \\\( -name '.svn' -or -name '.git' \\\) /
  160. -prune -or -true \\\) -and -name '*.pm' \\\)"
  161. (find-or
  162. (list
  163. (concat (find-or (mapcar 'find-to-string form)) (find-generic "prune"))
  164. (find-generic "true"))))
  165. (defun find-generic (option &optional oper argcount args dont-quote)
  166. "Allow an arbitrary string to be used as a form.
  167. OPTION is the name of the form, OPER is the function used to either
  168. OR or AND multiple results together. ARGCOUNT is the minimum of
  169. args that OPTION can receive and ARGS are the arguments for OPTION.
  170. If DONT-QUOTE is non-nil, arguments are quoted for passing them to
  171. the shell."
  172. (when (and (numberp argcount) (< (length args) argcount))
  173. (error "'%s' needs at least %d arguments" option argcount))
  174. (let ((oper (or oper 'find-or)))
  175. (if (and args (length args))
  176. (funcall oper (mapcar (lambda (x)
  177. (concat "-" option
  178. (if dont-quote
  179. (concat " " x " ")
  180. (concat " "
  181. (shell-quote-argument x)
  182. " "))))
  183. args))
  184. (concat "-" option " "))))
  185. (defun find-command (form)
  186. "For each item in FORM add a terminating semi-colon and turn
  187. them into valid switches. The result is -and(ed) together."
  188. (find-and (mapcar (lambda (x)
  189. (concat (find-to-string x) "\\; "))
  190. form)))
  191. (defun find-to-string (form)
  192. "Parse FORM to produce a set of valid find arguments."
  193. (cond
  194. ((stringp form)
  195. form)
  196. ((consp form)
  197. (let ((option (cdr (assoc (car form) find-constituents))))
  198. (cond
  199. ((and (symbolp option) (fboundp option))
  200. (funcall option (cdr form)))
  201. ((consp option)
  202. (let ((option (symbol-name (car form)))
  203. (argcnt (car option))
  204. (oper (cadr option))
  205. (dont-quote (car (cddr option))))
  206. (find-to-string
  207. (find-generic option oper argcnt (cdr form) dont-quote))))
  208. (t
  209. (error "Sorry I don't know how to handle '%s'" (car form))))))))
  210. (provide 'find-cmd)
  211. ;;; find-cmd.el ends here