Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / eshell / esh-ext.el
blob2654bf21e6a425adbea12403d2904a65958b69de
1 ;;; esh-ext.el --- commands external to Eshell -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
22 ;;; Commentary:
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:
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
32 ;;; Code:
34 (provide 'esh-ext)
36 (require 'esh-util)
38 (eval-when-compile
39 (require 'cl-lib)
40 (require 'esh-io)
41 (require 'esh-cmd))
42 (require 'esh-opt)
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)
50 ;;; User Variables:
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)
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)
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)
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)))
95 ;; This file provides itself then eval-when-compile loads files that require it.
96 ;; This causes spurious "might not be defined at runtime" warnings.
97 (declare-function eshell-search-path "esh-ext" (name))
99 (defcustom eshell-windows-shell-file
100 (if (eshell-under-windows-p)
101 (if (string-match "\\(cmdproxy\\|sh\\)\\.\\(com\\|exe\\)"
102 shell-file-name)
103 (or (eshell-search-path "cmd.exe")
104 (eshell-search-path "command.com"))
105 shell-file-name))
106 "The name of the shell command to use for DOS/Windows batch files.
107 This defaults to nil on non-Windows systems, where this variable is
108 wholly ignored."
109 :type '(choice file (const nil))
110 :group 'eshell-ext)
112 (autoload 'eshell-parse-command "esh-cmd")
114 (defsubst eshell-invoke-batch-file (&rest args)
115 "Invoke a .BAT or .CMD file on DOS/Windows systems."
116 ;; since CMD.EXE can't handle forward slashes in the initial
117 ;; argument...
118 (setcar args (subst-char-in-string ?/ ?\\ (car args)))
119 (throw 'eshell-replace-command
120 (eshell-parse-command
121 (eshell-quote-argument eshell-windows-shell-file)
122 (cons "/c" args))))
124 (defcustom eshell-interpreter-alist
125 (if (eshell-under-windows-p)
126 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file)))
127 "An alist defining interpreter substitutions.
128 Each member is a cons cell of the form:
130 (MATCH . INTERPRETER)
132 MATCH should be a regexp, which is matched against the command
133 name, or a function of arity 2 receiving the COMMAND and its
134 ARGS (a list). If either returns a non-nil value, then
135 INTERPRETER will be used for that command.
137 If INTERPRETER is a string, it will be called as the command name,
138 with the original command name passed as the first argument, with all
139 subsequent arguments following. If INTERPRETER is a function, it will
140 be called with all of those arguments. Note that interpreter
141 functions should throw `eshell-replace-command' with the alternate
142 command form, or they should return a value compatible with the
143 possible return values of `eshell-external-command', which see."
144 :type '(repeat (cons (choice regexp (function :tag "Predicate"))
145 (choice string (function :tag "Interpreter"))))
146 :group 'eshell-ext)
148 (defcustom eshell-alternate-command-hook nil
149 "A hook run whenever external command lookup fails.
150 If a functions wishes to provide an alternate command, they must throw
151 it using the tag `eshell-replace-command'. This is done because the
152 substituted command need not be external at all, and therefore must be
153 passed up to a higher level for re-evaluation.
155 Or, if the function returns a filename, that filename will be invoked
156 with the current command arguments rather than the command specified
157 by the user on the command line."
158 :type 'hook
159 :group 'eshell-ext)
161 (defcustom eshell-command-interpreter-max-length 256
162 "The maximum length of any command interpreter string, plus args."
163 :type 'integer
164 :group 'eshell-ext)
166 (defcustom eshell-explicit-command-char ?*
167 "If this char occurs before a command name, call it externally.
168 That is, although `vi' may be an alias, `\vi' will always call the
169 external version."
170 :type 'character
171 :group 'eshell-ext)
173 ;;; Functions:
175 (defun eshell-ext-initialize ()
176 "Initialize the external command handling code."
177 (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t))
179 (defun eshell-explicit-command (command args)
180 "If a command name begins with `*', call it externally always.
181 This bypasses all Lisp functions and aliases."
182 (when (and (> (length command) 1)
183 (eq (aref command 0) eshell-explicit-command-char))
184 (let ((cmd (eshell-search-path (substring command 1))))
185 (if cmd
186 (or (eshell-external-command cmd args)
187 (error "%s: external command failed" cmd))
188 (error "%s: external command not found"
189 (substring command 1))))))
191 (autoload 'eshell-close-handles "esh-io")
193 (defun eshell-remote-command (command args)
194 "Insert output from a remote COMMAND, using ARGS.
195 A remote command is something that executes on a different machine.
196 An external command simply means external to Emacs.
198 Note that this function is very crude at the moment. It gathers up
199 all the output from the remote command, and sends it all at once,
200 causing the user to wonder if anything's really going on..."
201 (let ((outbuf (generate-new-buffer " *eshell remote output*"))
202 (errbuf (generate-new-buffer " *eshell remote error*"))
203 (command (or (file-remote-p command 'localname) command))
204 (exitcode 1))
205 (unwind-protect
206 (progn
207 (setq exitcode
208 (shell-command
209 (mapconcat 'shell-quote-argument
210 (append (list command) args) " ")
211 outbuf errbuf))
212 (eshell-print (with-current-buffer outbuf (buffer-string)))
213 (eshell-error (with-current-buffer errbuf (buffer-string))))
214 (eshell-close-handles exitcode 'nil)
215 (kill-buffer outbuf)
216 (kill-buffer errbuf))))
218 (defun eshell-external-command (command args)
219 "Insert output from an external COMMAND, using ARGS."
220 (setq args (eshell-stringify-list (eshell-flatten-list args)))
221 (let ((interp (eshell-find-interpreter
222 command
223 args
224 ;; `eshell-find-interpreter' does not work correctly
225 ;; for Tramp file name syntax. But we don't need to
226 ;; know the interpreter in that case, therefore the
227 ;; check is suppressed.
228 (or (and (stringp command) (file-remote-p command))
229 (file-remote-p default-directory)))))
230 (cl-assert interp)
231 (if (functionp (car interp))
232 (apply (car interp) (append (cdr interp) args))
233 (eshell-gather-process-output
234 (car interp) (append (cdr interp) args)))))
236 (defun eshell/addpath (&rest args)
237 "Add a set of paths to PATH."
238 (eshell-eval-using-options
239 "addpath" args
240 '((?b "begin" nil prepend "add path element at beginning")
241 (?h "help" nil nil "display this usage message")
242 :usage "[-b] PATH
243 Adds the given PATH to $PATH.")
244 (if args
245 (progn
246 (setq eshell-path-env (getenv "PATH")
247 args (mapconcat 'identity args path-separator)
248 eshell-path-env
249 (if prepend
250 (concat args path-separator eshell-path-env)
251 (concat eshell-path-env path-separator args)))
252 (setenv "PATH" eshell-path-env))
253 (dolist (dir (parse-colon-path (getenv "PATH")))
254 (eshell-printn dir)))))
256 (put 'eshell/addpath 'eshell-no-numeric-conversions t)
258 (defun eshell-script-interpreter (file)
259 "Extract the script to run from FILE, if it has #!<interp> in it.
260 Return nil, or a list of the form:
262 (INTERPRETER [ARGS] FILE)"
263 (let ((maxlen eshell-command-interpreter-max-length))
264 (if (and (file-readable-p file)
265 (file-regular-p file))
266 (with-temp-buffer
267 (insert-file-contents-literally file nil 0 maxlen)
268 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
269 (if (match-string 3)
270 (list (match-string 1)
271 (match-string 3)
272 file)
273 (list (match-string 1)
274 file)))))))
276 (defun eshell-find-interpreter (file args &optional no-examine-p)
277 "Find the command interpreter with which to execute FILE.
278 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
279 line of the form #!<interp>."
280 (let ((finterp
281 (catch 'found
282 (ignore
283 (dolist (possible eshell-interpreter-alist)
284 (cond
285 ((functionp (car possible))
286 (let ((fn (car possible)))
287 (and (funcall fn file args)
288 (throw 'found (cdr possible)))))
289 ((stringp (car possible))
290 (and (string-match (car possible) file)
291 (throw 'found (cdr possible))))
293 (error "Invalid interpreter-alist test"))))))))
294 (if finterp ; first check
295 (list finterp file)
296 (let ((fullname (if (file-name-directory file) file
297 (eshell-search-path file)))
298 (suffixes eshell-binary-suffixes))
299 (if (and fullname (not (or eshell-force-execution
300 (file-executable-p fullname))))
301 (while suffixes
302 (let ((try (concat fullname (car suffixes))))
303 (if (or (file-executable-p try)
304 (and eshell-force-execution
305 (file-readable-p try)))
306 (setq fullname try suffixes nil)
307 (setq suffixes (cdr suffixes))))))
308 (cond ((not (and fullname (file-exists-p fullname)))
309 (let ((name (or fullname file)))
310 (unless (setq fullname
311 (run-hook-with-args-until-success
312 'eshell-alternate-command-hook file))
313 (error "%s: command not found" name))))
314 ((not (or eshell-force-execution
315 (file-executable-p fullname)))
316 (error "%s: Permission denied" fullname)))
317 (let (interp)
318 (unless no-examine-p
319 (setq interp (eshell-script-interpreter fullname))
320 (if interp
321 (setq interp
322 (cons (car (eshell-find-interpreter (car interp) args t))
323 (cdr interp)))))
324 (or interp (list fullname)))))))
326 ;;; esh-ext.el ends here