1 ;;; esh-ext.el --- commands external to Eshell
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; To force a command to invoked external, either provide an explicit
26 ;; pathname for the command argument, or prefix the command name with
27 ;; an asterix character. Example:
29 ;; grep ; make invoke `grep' Lisp function, or `eshell/grep'
30 ;; /bin/grep ; will definitely invoke /bin/grep
31 ;; *grep ; will also invoke /bin/grep
39 (defgroup eshell-ext nil
40 "External commands are invoked when operating system executables are
41 loaded into memory, thus beginning a new process."
42 :tag
"External commands"
47 (defcustom eshell-ext-load-hook
'(eshell-ext-initialize)
48 "*A hook that gets run when `eshell-ext' is loaded."
52 (defcustom eshell-binary-suffixes exec-suffixes
53 "*A list of suffixes used when searching for executable files."
54 :type
'(repeat string
)
57 (defcustom eshell-force-execution nil
58 "*If non-nil, try to execute binary files regardless of permissions.
59 This can be useful on systems like Windows, where the operating system
60 doesn't happen to honor the permission bits in certain cases; or in
61 cases where you want to associate an interpreter with a particular
62 kind of script file, but the language won't let you but a '#!'
63 interpreter line in the file, and you don't want to make it executable
64 since nothing else but Eshell will be able to understand
65 `eshell-interpreter-alist'."
69 (defun eshell-search-path (name)
70 "Search the environment path for NAME."
71 (if (file-name-absolute-p name
)
73 (let ((list (parse-colon-path (getenv "PATH")))
76 (setq n1
(concat (car list
) name
))
77 (setq suffixes eshell-binary-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
)))
89 (defcustom eshell-windows-shell-file
90 (if (eshell-under-windows-p)
91 (if (string-match "\\(\\`cmdproxy\\|sh\\)\\.\\(com\\|exe\\)"
93 (or (eshell-search-path "cmd.exe")
94 (eshell-search-path "command.com"))
96 "*The name of the shell command to use for DOS/Windows batch files.
97 This defaults to nil on non-Windows systems, where this variable is
99 :type
'(choice file
(const nil
))
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
106 (setcar args
(subst-char-in-string ?
/ ?
\\ (car args
)))
107 (throw 'eshell-replace-command
108 (eshell-parse-command eshell-windows-shell-file
(cons "/c" args
))))
110 (defcustom eshell-interpreter-alist
111 (if (eshell-under-windows-p)
112 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file
)))
113 "*An alist defining interpreter substitutions.
114 Each member is a cons cell of the form:
116 (MATCH . INTERPRETER)
118 MATCH should be a regexp, which is matched against the command name,
119 or a function. If either returns a non-nil value, then INTERPRETER
120 will be used for that command.
122 If INTERPRETER is a string, it will be called as the command name,
123 with the original command name passed as the first argument, with all
124 subsequent arguments following. If INTERPRETER is a function, it will
125 be called with all of those arguments. Note that interpreter
126 functions should throw `eshell-replace-command' with the alternate
127 command form, or they should return a value compatible with the
128 possible return values of `eshell-external-command', which see."
129 :type
'(repeat (cons (choice regexp
(function :tag
"Predicate"))
130 (choice string
(function :tag
"Interpreter"))))
133 (defcustom eshell-alternate-command-hook nil
134 "*A hook run whenever external command lookup fails.
135 If a functions wishes to provide an alternate command, they must throw
136 it using the tag `eshell-replace-command'. This is done because the
137 substituted command need not be external at all, and therefore must be
138 passed up to a higher level for re-evaluation.
140 Or, if the function returns a filename, that filename will be invoked
141 with the current command arguments rather than the command specified
142 by the user on the command line."
146 (defcustom eshell-command-interpreter-max-length
256
147 "*The maximum length of any command interpreter string, plus args."
151 (defcustom eshell-explicit-command-char ?
*
152 "*If this char occurs before a command name, call it externally.
153 That is, although `vi' may be an alias, `\vi' will always call the
160 (defun eshell-ext-initialize ()
161 "Initialize the external command handling code."
162 (add-hook 'eshell-named-command-hook
'eshell-explicit-command nil t
))
164 (defun eshell-explicit-command (command args
)
165 "If a command name begins with `*', call it externally always.
166 This bypasses all Lisp functions and aliases."
167 (when (and (> (length command
) 1)
168 (eq (aref command
0) eshell-explicit-command-char
))
169 (let ((cmd (eshell-search-path (substring command
1))))
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))))))
176 (defun eshell-remote-command (handler command args
)
177 "Insert output from a remote COMMAND, using ARGS.
178 A remote command is something that executes on a different machine.
179 An external command simply means external to Emacs.
181 Note that this function is very crude at the moment. It gathers up
182 all the output from the remote command, and sends it all at once,
183 causing 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*"))
190 (funcall handler
'shell-command
191 (mapconcat 'shell-quote-argument
192 (append (list command
) args
) " ")
194 (eshell-print (save-excursion (set-buffer outbuf
)
196 (eshell-error (save-excursion (set-buffer errbuf
)
198 (eshell-close-handles exitcode
'nil
)
200 (kill-buffer errbuf
))))
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
)))
206 (unless (or (equal default-directory
"/")
207 (and (eshell-under-windows-p)
208 (string-match "\\`[A-Za-z]:[/\\\\]\\'"
210 (find-file-name-handler default-directory
213 (not (and (featurep 'xemacs
)
214 (eq handler
'dired-handler-fn
))))
215 (eshell-remote-command handler command args
))
216 (let ((interp (eshell-find-interpreter command
)))
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
))))))
223 (defun eshell/addpath
(&rest args
)
224 "Add a set of paths to PATH."
225 (eshell-eval-using-options
227 '((?b
"begin" nil prepend
"add path element at beginning")
228 (?h
"help" nil nil
"display this usage message")
230 Adds the given PATH to $PATH.")
234 (setq args
(nreverse args
)))
238 (concat (car args
) path-separator
240 (concat (getenv "PATH") path-separator
242 (setq args
(cdr args
))))
243 (let ((paths (parse-colon-path (getenv "PATH"))))
245 (eshell-printn (car paths
))
246 (setq paths
(cdr paths
)))))))
248 (put 'eshell
/addpath
'eshell-no-numeric-conversions t
)
250 (defun eshell-script-interpreter (file)
251 "Extract the script to run from FILE, if it has #!<interp> in it.
252 Return nil, or a list of the form:
254 (INTERPRETER [ARGS] FILE)"
255 (let ((maxlen eshell-command-interpreter-max-length
))
256 (if (and (file-readable-p file
)
257 (file-regular-p file
))
259 (insert-file-contents-literally file nil
0 maxlen
)
260 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
262 (list (match-string 1)
265 (list (match-string 1)
268 (defun eshell-find-interpreter (file &optional no-examine-p
)
269 "Find the command interpreter with which to execute FILE.
270 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
271 line of the form #!<interp>."
275 (eshell-for possible eshell-interpreter-alist
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
))))
284 (error "Invalid interpreter-alist test"))))))))
285 (if finterp
; first check
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
))))
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
)))
310 (setq interp
(eshell-script-interpreter fullname
))
313 (cons (car (eshell-find-interpreter (car interp
) t
))
315 (or interp
(list fullname
)))))))
319 ;; arch-tag: 178d4064-7e60-4745-b81f-bab5d8d7c40f
320 ;;; esh-ext.el ends here