esh-ext.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;;; esh-ext.el --- commands external to Eshell
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; To force a command to invoked external, either provide an explicit
  17. ;; pathname for the command argument, or prefix the command name with
  18. ;; an asterix character. Example:
  19. ;;
  20. ;; grep ; make invoke `grep' Lisp function, or `eshell/grep'
  21. ;; /bin/grep ; will definitely invoke /bin/grep
  22. ;; *grep ; will also invoke /bin/grep
  23. ;;; Code:
  24. (provide 'esh-ext)
  25. (eval-when-compile
  26. (require 'cl)
  27. (require 'esh-cmd))
  28. (require 'esh-util)
  29. (defgroup eshell-ext nil
  30. "External commands are invoked when operating system executables are
  31. loaded into memory, thus beginning a new process."
  32. :tag "External commands"
  33. :group 'eshell)
  34. ;;; User Variables:
  35. (defcustom eshell-ext-load-hook nil
  36. "A hook that gets run when `eshell-ext' is loaded."
  37. :version "24.1" ; removed eshell-ext-initialize
  38. :type 'hook
  39. :group 'eshell-ext)
  40. (defcustom eshell-binary-suffixes exec-suffixes
  41. "A list of suffixes used when searching for executable files."
  42. :type '(repeat string)
  43. :group 'eshell-ext)
  44. (defcustom eshell-force-execution nil
  45. "If non-nil, try to execute binary files regardless of permissions.
  46. This can be useful on systems like Windows, where the operating system
  47. doesn't happen to honor the permission bits in certain cases; or in
  48. cases where you want to associate an interpreter with a particular
  49. kind of script file, but the language won't let you but a '#!'
  50. interpreter line in the file, and you don't want to make it executable
  51. since nothing else but Eshell will be able to understand
  52. `eshell-interpreter-alist'."
  53. :type 'boolean
  54. :group 'eshell-ext)
  55. (defun eshell-search-path (name)
  56. "Search the environment path for NAME."
  57. (if (file-name-absolute-p name)
  58. name
  59. (let ((list (eshell-parse-colon-path eshell-path-env))
  60. suffixes n1 n2 file)
  61. (while list
  62. (setq n1 (concat (car list) name))
  63. (setq suffixes eshell-binary-suffixes)
  64. (while suffixes
  65. (setq n2 (concat n1 (car suffixes)))
  66. (if (and (or (file-executable-p n2)
  67. (and eshell-force-execution
  68. (file-readable-p n2)))
  69. (not (file-directory-p n2)))
  70. (setq file n2 suffixes nil list nil))
  71. (setq suffixes (cdr suffixes)))
  72. (setq list (cdr list)))
  73. file)))
  74. (defcustom eshell-windows-shell-file
  75. (if (eshell-under-windows-p)
  76. (if (string-match "\\(cmdproxy\\|sh\\)\\.\\(com\\|exe\\)"
  77. shell-file-name)
  78. (or (eshell-search-path "cmd.exe")
  79. (eshell-search-path "command.com"))
  80. shell-file-name))
  81. "The name of the shell command to use for DOS/Windows batch files.
  82. This defaults to nil on non-Windows systems, where this variable is
  83. wholly ignored."
  84. :type '(choice file (const nil))
  85. :group 'eshell-ext)
  86. (defsubst eshell-invoke-batch-file (&rest args)
  87. "Invoke a .BAT or .CMD file on DOS/Windows systems."
  88. ;; since CMD.EXE can't handle forward slashes in the initial
  89. ;; argument...
  90. (setcar args (subst-char-in-string ?/ ?\\ (car args)))
  91. (throw 'eshell-replace-command
  92. (eshell-parse-command
  93. (eshell-quote-argument eshell-windows-shell-file)
  94. (cons "/c" args))))
  95. (defcustom eshell-interpreter-alist
  96. (if (eshell-under-windows-p)
  97. '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file)))
  98. "An alist defining interpreter substitutions.
  99. Each member is a cons cell of the form:
  100. (MATCH . INTERPRETER)
  101. MATCH should be a regexp, which is matched against the command name,
  102. or a function. If either returns a non-nil value, then INTERPRETER
  103. will be used for that command.
  104. If INTERPRETER is a string, it will be called as the command name,
  105. with the original command name passed as the first argument, with all
  106. subsequent arguments following. If INTERPRETER is a function, it will
  107. be called with all of those arguments. Note that interpreter
  108. functions should throw `eshell-replace-command' with the alternate
  109. command form, or they should return a value compatible with the
  110. possible return values of `eshell-external-command', which see."
  111. :type '(repeat (cons (choice regexp (function :tag "Predicate"))
  112. (choice string (function :tag "Interpreter"))))
  113. :group 'eshell-ext)
  114. (defcustom eshell-alternate-command-hook nil
  115. "A hook run whenever external command lookup fails.
  116. If a functions wishes to provide an alternate command, they must throw
  117. it using the tag `eshell-replace-command'. This is done because the
  118. substituted command need not be external at all, and therefore must be
  119. passed up to a higher level for re-evaluation.
  120. Or, if the function returns a filename, that filename will be invoked
  121. with the current command arguments rather than the command specified
  122. by the user on the command line."
  123. :type 'hook
  124. :group 'eshell-ext)
  125. (defcustom eshell-command-interpreter-max-length 256
  126. "The maximum length of any command interpreter string, plus args."
  127. :type 'integer
  128. :group 'eshell-ext)
  129. (defcustom eshell-explicit-command-char ?*
  130. "If this char occurs before a command name, call it externally.
  131. That is, although `vi' may be an alias, `\vi' will always call the
  132. external version."
  133. :type 'character
  134. :group 'eshell-ext)
  135. ;;; Functions:
  136. (defun eshell-ext-initialize ()
  137. "Initialize the external command handling code."
  138. (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t))
  139. (defun eshell-explicit-command (command args)
  140. "If a command name begins with `*', call it externally always.
  141. This bypasses all Lisp functions and aliases."
  142. (when (and (> (length command) 1)
  143. (eq (aref command 0) eshell-explicit-command-char))
  144. (let ((cmd (eshell-search-path (substring command 1))))
  145. (if cmd
  146. (or (eshell-external-command cmd args)
  147. (error "%s: external command failed" cmd))
  148. (error "%s: external command not found"
  149. (substring command 1))))))
  150. (defun eshell-remote-command (command args)
  151. "Insert output from a remote COMMAND, using ARGS.
  152. A remote command is something that executes on a different machine.
  153. An external command simply means external to Emacs.
  154. Note that this function is very crude at the moment. It gathers up
  155. all the output from the remote command, and sends it all at once,
  156. causing the user to wonder if anything's really going on..."
  157. (let ((outbuf (generate-new-buffer " *eshell remote output*"))
  158. (errbuf (generate-new-buffer " *eshell remote error*"))
  159. (exitcode 1))
  160. (unwind-protect
  161. (progn
  162. (setq exitcode
  163. (shell-command
  164. (mapconcat 'shell-quote-argument
  165. (append (list command) args) " ")
  166. outbuf errbuf))
  167. (eshell-print (with-current-buffer outbuf (buffer-string)))
  168. (eshell-error (with-current-buffer errbuf (buffer-string))))
  169. (eshell-close-handles exitcode 'nil)
  170. (kill-buffer outbuf)
  171. (kill-buffer errbuf))))
  172. (defun eshell-external-command (command args)
  173. "Insert output from an external COMMAND, using ARGS."
  174. (setq args (eshell-stringify-list (eshell-flatten-list args)))
  175. (if (file-remote-p default-directory)
  176. (eshell-remote-command command args))
  177. (let ((interp (eshell-find-interpreter command)))
  178. (assert interp)
  179. (if (functionp (car interp))
  180. (apply (car interp) (append (cdr interp) args))
  181. (eshell-gather-process-output
  182. (car interp) (append (cdr interp) args)))))
  183. (defun eshell/addpath (&rest args)
  184. "Add a set of paths to PATH."
  185. (eshell-eval-using-options
  186. "addpath" args
  187. '((?b "begin" nil prepend "add path element at beginning")
  188. (?h "help" nil nil "display this usage message")
  189. :usage "[-b] PATH
  190. Adds the given PATH to $PATH.")
  191. (if args
  192. (progn
  193. (if prepend
  194. (setq args (nreverse args)))
  195. (while args
  196. (setenv "PATH"
  197. (if prepend
  198. (concat (car args) path-separator
  199. (getenv "PATH"))
  200. (concat (getenv "PATH") path-separator
  201. (car args))))
  202. (setq args (cdr args))))
  203. (let ((paths (parse-colon-path (getenv "PATH"))))
  204. (while paths
  205. (eshell-printn (car paths))
  206. (setq paths (cdr paths)))))))
  207. (put 'eshell/addpath 'eshell-no-numeric-conversions t)
  208. (defun eshell-script-interpreter (file)
  209. "Extract the script to run from FILE, if it has #!<interp> in it.
  210. Return nil, or a list of the form:
  211. (INTERPRETER [ARGS] FILE)"
  212. (let ((maxlen eshell-command-interpreter-max-length))
  213. (if (and (file-readable-p file)
  214. (file-regular-p file))
  215. (with-temp-buffer
  216. (insert-file-contents-literally file nil 0 maxlen)
  217. (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
  218. (if (match-string 3)
  219. (list (match-string 1)
  220. (match-string 3)
  221. file)
  222. (list (match-string 1)
  223. file)))))))
  224. (defun eshell-find-interpreter (file &optional no-examine-p)
  225. "Find the command interpreter with which to execute FILE.
  226. If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
  227. line of the form #!<interp>."
  228. (let ((finterp
  229. (catch 'found
  230. (ignore
  231. (dolist (possible eshell-interpreter-alist)
  232. (cond
  233. ((functionp (car possible))
  234. (and (funcall (car possible) file)
  235. (throw 'found (cdr possible))))
  236. ((stringp (car possible))
  237. (and (string-match (car possible) file)
  238. (throw 'found (cdr possible))))
  239. (t
  240. (error "Invalid interpreter-alist test"))))))))
  241. (if finterp ; first check
  242. (list finterp file)
  243. (let ((fullname (if (file-name-directory file) file
  244. (eshell-search-path file)))
  245. (suffixes eshell-binary-suffixes))
  246. (if (and fullname (not (or eshell-force-execution
  247. (file-executable-p fullname))))
  248. (while suffixes
  249. (let ((try (concat fullname (car suffixes))))
  250. (if (or (file-executable-p try)
  251. (and eshell-force-execution
  252. (file-readable-p try)))
  253. (setq fullname try suffixes nil)
  254. (setq suffixes (cdr suffixes))))))
  255. (cond ((not (and fullname (file-exists-p fullname)))
  256. (let ((name (or fullname file)))
  257. (unless (setq fullname
  258. (run-hook-with-args-until-success
  259. 'eshell-alternate-command-hook file))
  260. (error "%s: command not found" name))))
  261. ((not (or eshell-force-execution
  262. (file-executable-p fullname)))
  263. (error "%s: Permission denied" fullname)))
  264. (let (interp)
  265. (unless no-examine-p
  266. (setq interp (eshell-script-interpreter fullname))
  267. (if interp
  268. (setq interp
  269. (cons (car (eshell-find-interpreter (car interp) t))
  270. (cdr interp)))))
  271. (or interp (list fullname)))))))
  272. ;;; esh-ext.el ends here