1 ;;; esh-ext.el --- commands external to Eshell -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2016 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/>.
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
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"
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
58 (defcustom eshell-binary-suffixes exec-suffixes
59 "A list of suffixes used when searching for executable files."
60 :type
'(repeat string
)
63 (defcustom eshell-force-execution
64 (not (null (memq system-type
'(windows-nt ms-dos
))))
65 "If non-nil, try to execute files regardless of execute permissions.
66 This can be useful on systems like Windows, where the operating system
67 doesn't support the execution bit for shell scripts; or in cases where
68 you want to associate an interpreter with a particular kind of script
69 file, but the language won't let you but a `#!' interpreter line in
70 the file, and you don't want to make it executable since nothing else
71 but Eshell will be able to understand
72 `eshell-interpreter-alist'."
76 (defun eshell-search-path (name)
77 "Search the environment path for NAME."
78 (if (file-name-absolute-p name
)
80 (let ((list (eshell-parse-colon-path eshell-path-env
))
82 (if (eshell-under-windows-p)
85 (setq n1
(concat (car list
) name
))
86 (setq suffixes eshell-binary-suffixes
)
88 (setq n2
(concat n1
(car suffixes
)))
89 (if (and (or (file-executable-p n2
)
90 (and eshell-force-execution
91 (file-readable-p n2
)))
92 (not (file-directory-p n2
)))
93 (setq file n2 suffixes nil list nil
))
94 (setq suffixes
(cdr suffixes
)))
95 (setq list
(cdr list
)))
98 ;; This file provides itself then eval-when-compile loads files that require it.
99 ;; This causes spurious "might not be defined at runtime" warnings.
100 (declare-function eshell-search-path
"esh-ext" (name))
102 (defcustom eshell-windows-shell-file
103 (if (eshell-under-windows-p)
104 (if (string-match "\\(cmdproxy\\|sh\\)\\.\\(com\\|exe\\)"
106 (or (eshell-search-path "cmd.exe")
107 (eshell-search-path "command.com"))
109 "The name of the shell command to use for DOS/Windows batch files.
110 This defaults to nil on non-Windows systems, where this variable is
112 :type
'(choice file
(const nil
))
115 (autoload 'eshell-parse-command
"esh-cmd")
117 (defsubst eshell-invoke-batch-file
(&rest args
)
118 "Invoke a .BAT or .CMD file on DOS/Windows systems."
119 ;; since CMD.EXE can't handle forward slashes in the initial
121 (setcar args
(subst-char-in-string ?
/ ?
\\ (car args
)))
122 (throw 'eshell-replace-command
123 (eshell-parse-command
124 (eshell-quote-argument eshell-windows-shell-file
)
127 (defcustom eshell-interpreter-alist
128 (if (eshell-under-windows-p)
129 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file
)))
130 "An alist defining interpreter substitutions.
131 Each member is a cons cell of the form:
133 (MATCH . INTERPRETER)
135 MATCH should be a regexp, which is matched against the command
136 name, or a function of arity 2 receiving the COMMAND and its
137 ARGS (a list). If either returns a non-nil value, then
138 INTERPRETER will be used for that command.
140 If INTERPRETER is a string, it will be called as the command name,
141 with the original command name passed as the first argument, with all
142 subsequent arguments following. If INTERPRETER is a function, it will
143 be called with all of those arguments. Note that interpreter
144 functions should throw `eshell-replace-command' with the alternate
145 command form, or they should return a value compatible with the
146 possible return values of `eshell-external-command', which see."
147 :type
'(repeat (cons (choice regexp
(function :tag
"Predicate"))
148 (choice string
(function :tag
"Interpreter"))))
151 (defcustom eshell-alternate-command-hook nil
152 "A hook run whenever external command lookup fails.
153 If a functions wishes to provide an alternate command, they must throw
154 it using the tag `eshell-replace-command'. This is done because the
155 substituted command need not be external at all, and therefore must be
156 passed up to a higher level for re-evaluation.
158 Or, if the function returns a filename, that filename will be invoked
159 with the current command arguments rather than the command specified
160 by the user on the command line."
164 (defcustom eshell-command-interpreter-max-length
256
165 "The maximum length of any command interpreter string, plus args."
169 (defcustom eshell-explicit-command-char ?
*
170 "If this char occurs before a command name, call it externally.
171 That is, although `vi' may be an alias, `\vi' will always call the
178 (defun eshell-ext-initialize ()
179 "Initialize the external command handling code."
180 (add-hook 'eshell-named-command-hook
'eshell-explicit-command nil t
))
182 (defun eshell-explicit-command (command args
)
183 "If a command name begins with `*', call it externally always.
184 This bypasses all Lisp functions and aliases."
185 (when (and (> (length command
) 1)
186 (eq (aref command
0) eshell-explicit-command-char
))
187 (let ((cmd (eshell-search-path (substring command
1))))
189 (or (eshell-external-command cmd args
)
190 (error "%s: external command failed" cmd
))
191 (error "%s: external command not found"
192 (substring command
1))))))
194 (autoload 'eshell-close-handles
"esh-io")
196 (defun eshell-remote-command (command args
)
197 "Insert output from a remote COMMAND, using ARGS.
198 A remote command is something that executes on a different machine.
199 An external command simply means external to Emacs.
201 Note that this function is very crude at the moment. It gathers up
202 all the output from the remote command, and sends it all at once,
203 causing the user to wonder if anything's really going on..."
204 (let ((outbuf (generate-new-buffer " *eshell remote output*"))
205 (errbuf (generate-new-buffer " *eshell remote error*"))
206 (command (or (file-remote-p command
'localname
) command
))
212 (mapconcat 'shell-quote-argument
213 (append (list command
) args
) " ")
215 (eshell-print (with-current-buffer outbuf
(buffer-string)))
216 (eshell-error (with-current-buffer errbuf
(buffer-string))))
217 (eshell-close-handles exitcode
'nil
)
219 (kill-buffer errbuf
))))
221 (defun eshell-external-command (command args
)
222 "Insert output from an external COMMAND, using ARGS."
223 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
224 (let ((interp (eshell-find-interpreter
227 ;; `eshell-find-interpreter' does not work correctly
228 ;; for Tramp file name syntax. But we don't need to
229 ;; know the interpreter in that case, therefore the
230 ;; check is suppressed.
231 (or (and (stringp command
) (file-remote-p command
))
232 (file-remote-p default-directory
)))))
234 (if (functionp (car interp
))
235 (apply (car interp
) (append (cdr interp
) args
))
236 (eshell-gather-process-output
237 (car interp
) (append (cdr interp
) args
)))))
239 (defun eshell/addpath
(&rest args
)
240 "Add a set of paths to PATH."
241 (eshell-eval-using-options
243 '((?b
"begin" nil prepend
"add path element at beginning")
244 (?h
"help" nil nil
"display this usage message")
246 Adds the given PATH to $PATH.")
249 (setq eshell-path-env
(getenv "PATH")
250 args
(mapconcat 'identity args path-separator
)
253 (concat args path-separator eshell-path-env
)
254 (concat eshell-path-env path-separator args
)))
255 (setenv "PATH" eshell-path-env
))
256 (dolist (dir (parse-colon-path (getenv "PATH")))
257 (eshell-printn dir
)))))
259 (put 'eshell
/addpath
'eshell-no-numeric-conversions t
)
261 (defun eshell-script-interpreter (file)
262 "Extract the script to run from FILE, if it has #!<interp> in it.
263 Return nil, or a list of the form:
265 (INTERPRETER [ARGS] FILE)"
266 (let ((maxlen eshell-command-interpreter-max-length
))
267 (if (and (file-readable-p file
)
268 (file-regular-p file
))
270 (insert-file-contents-literally file nil
0 maxlen
)
271 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
273 (list (match-string 1)
276 (list (match-string 1)
279 (defun eshell-find-interpreter (file args
&optional no-examine-p
)
280 "Find the command interpreter with which to execute FILE.
281 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
282 line of the form #!<interp>."
286 (dolist (possible eshell-interpreter-alist
)
288 ((functionp (car possible
))
289 (let ((fn (car possible
)))
290 (and (funcall fn file args
)
291 (throw 'found
(cdr possible
)))))
292 ((stringp (car possible
))
293 (and (string-match (car possible
) file
)
294 (throw 'found
(cdr possible
))))
296 (error "Invalid interpreter-alist test"))))))))
297 (if finterp
; first check
299 (let ((fullname (if (file-name-directory file
) file
300 (eshell-search-path file
)))
301 (suffixes eshell-binary-suffixes
))
303 (not (file-remote-p fullname
))
304 (file-remote-p default-directory
))
305 (setq fullname
(expand-file-name
306 (concat "./" fullname
) default-directory
)))
307 (if (and fullname
(not (or eshell-force-execution
308 (file-executable-p fullname
))))
310 (let ((try (concat fullname
(car suffixes
))))
311 (if (or (file-executable-p try
)
312 (and eshell-force-execution
313 (file-readable-p try
)))
314 (setq fullname try suffixes nil
)
315 (setq suffixes
(cdr suffixes
))))))
316 (cond ((not (and fullname
(file-exists-p fullname
)))
317 (let ((name (or fullname file
)))
318 (unless (setq fullname
319 (run-hook-with-args-until-success
320 'eshell-alternate-command-hook file
))
321 (error "%s: command not found" name
))))
322 ((not (or eshell-force-execution
323 (file-executable-p fullname
)))
324 (error "%s: Permission denied" fullname
)))
327 (setq interp
(eshell-script-interpreter fullname
))
330 (cons (car (eshell-find-interpreter (car interp
) args t
))
332 (or interp
(list fullname
)))))))
334 ;;; esh-ext.el ends here