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