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