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