38414
|
1 ;;; esh-ext.el --- commands external to Eshell
|
29876
|
2
|
74509
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
|
79707
|
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
|
29876
|
5
|
32526
|
6 ;; Author: John Wiegley <johnw@gnu.org>
|
|
7
|
29876
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
94661
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
29876
|
11 ;; it under the terms of the GNU General Public License as published by
|
94661
|
12 ;; the Free Software Foundation, either version 3 of the License, or
|
|
13 ;; (at your option) any later version.
|
29876
|
14
|
|
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.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
94661
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
29876
|
22
|
|
23 ;;; Commentary:
|
|
24
|
|
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:
|
|
28 ;;
|
|
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
|
|
32
|
87084
|
33 (provide 'esh-ext)
|
|
34
|
|
35 (eval-when-compile
|
|
36 (require 'esh-cmd))
|
|
37 (require 'esh-util)
|
|
38
|
|
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"
|
|
43 :group 'eshell)
|
|
44
|
29876
|
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
|
44002
|
52 (defcustom eshell-binary-suffixes exec-suffixes
|
29876
|
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.
|
|
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'."
|
|
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")
|
65169
|
94 (eshell-search-path "command.com"))
|
29876
|
95 shell-file-name))
|
|
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
|
|
98 wholly ignored."
|
35717
|
99 :type '(choice file (const nil))
|
29876
|
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...
|
62915
b89e30bcd2bb
Changed all uses of `directory-sep-char' to ?/, and all uses of
John Wiegley <johnw@newartisans.com>
diff
changeset
|
106 (setcar args (subst-char-in-string ?/ ?\\ (car args)))
|
29876
|
107 (throw 'eshell-replace-command
|
31241
|
108 (eshell-parse-command eshell-windows-shell-file (cons "/c" args))))
|
29876
|
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.
|
|
114 Each member is a cons cell of the form:
|
|
115
|
|
116 (MATCH . INTERPRETER)
|
|
117
|
|
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.
|
|
121
|
|
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"))))
|
|
131 :group 'eshell-ext)
|
|
132
|
|
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.
|
|
139
|
|
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."
|
|
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
|
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
151 (defcustom eshell-explicit-command-char ?*
|
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
152 "*If this char occurs before a command name, call it externally.
|
57162
|
153 That is, although `vi' may be an alias, `\vi' will always call the
|
|
154 external version."
|
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
155 :type 'character
|
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
156 :group 'eshell-ext)
|
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
157
|
29876
|
158 ;;; Functions:
|
|
159
|
|
160 (defun eshell-ext-initialize ()
|
|
161 "Initialize the external command handling code."
|
|
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.
|
|
166 This bypasses all Lisp functions and aliases."
|
|
167 (when (and (> (length command) 1)
|
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
168 (eq (aref command 0) eshell-explicit-command-char))
|
29876
|
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.
|
|
178 A remote command is something that executes on a different machine.
|
|
179 An external command simply means external to Emacs.
|
|
180
|
|
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*"))
|
|
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))))
|
43319
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
212 (if (and handler
|
86202
|
213 (not (and (featurep 'xemacs)
|
43319
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
diff
changeset
|
214 (eq handler 'dired-handler-fn))))
|
29876
|
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
|
|
230 Adds 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
|
37661
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
diff
changeset
|
248 (put 'eshell/addpath 'eshell-no-numeric-conversions t)
|
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
diff
changeset
|
249
|
29876
|
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:
|
|
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)
|
44553
536e5590a586
(eshell-script-interpreter): Fix for CRLF operating systems to the
John Wiegley <johnw@newartisans.com>
diff
changeset
|
260 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
|
29876
|
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.
|
|
270 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
|
|
271 line 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
|
93975
|
319 ;; arch-tag: 178d4064-7e60-4745-b81f-bab5d8d7c40f
|
29876
|
320 ;;; esh-ext.el ends here
|