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