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