Mercurial > emacs
annotate lisp/eshell/esh-ext.el @ 40281:ec4224073236
*** empty log message ***
author | Miles Bader <miles@gnu.org> |
---|---|
date | Thu, 25 Oct 2001 01:09:20 +0000 |
parents | 67b464da13ec |
children | 539d3657c52c |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37818
diff
changeset
|
1 ;;; esh-ext.el --- commands external to Eshell |
29876 | 2 |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
32526 | 5 ;; Author: John Wiegley <johnw@gnu.org> |
6 | |
29876 | 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)) | |
27 | |
28 (defgroup eshell-ext nil | |
29 "External commands are invoked when operating system executables are | |
30 loaded into memory, thus beginning a new process." | |
31 :tag "External commands" | |
32 :group 'eshell) | |
33 | |
34 ;;; Commentary: | |
35 | |
36 ;; To force a command to invoked external, either provide an explicit | |
37 ;; pathname for the command argument, or prefix the command name with | |
38 ;; an asterix character. Example: | |
39 ;; | |
40 ;; grep ; make invoke `grep' Lisp function, or `eshell/grep' | |
41 ;; /bin/grep ; will definitely invoke /bin/grep | |
42 ;; *grep ; will also invoke /bin/grep | |
43 | |
44 ;;; User Variables: | |
45 | |
46 (defcustom eshell-ext-load-hook '(eshell-ext-initialize) | |
47 "*A hook that gets run when `eshell-ext' is loaded." | |
48 :type 'hook | |
49 :group 'eshell-ext) | |
50 | |
51 (defcustom eshell-binary-suffixes | |
52 (if (eshell-under-windows-p) | |
53 '(".exe" ".com" ".bat" ".cmd" "") | |
54 '("")) | |
55 "*A list of suffixes used when searching for executable files." | |
56 :type '(repeat string) | |
57 :group 'eshell-ext) | |
58 | |
59 (defcustom eshell-force-execution nil | |
60 "*If non-nil, try to execute binary files regardless of permissions. | |
61 This can be useful on systems like Windows, where the operating system | |
62 doesn't happen to honor the permission bits in certain cases; or in | |
63 cases where you want to associate an interpreter with a particular | |
64 kind of script file, but the language won't let you but a '#!' | |
65 interpreter line in the file, and you don't want to make it executable | |
66 since 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 | |
75 (let ((list (parse-colon-path (getenv "PATH"))) | |
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") | |
96 (eshell-search-path "command.exe")) | |
97 shell-file-name)) | |
98 "*The name of the shell command to use for DOS/Windows batch files. | |
99 This defaults to nil on non-Windows systems, where this variable is | |
100 wholly ignored." | |
35717
a16b7aced16a
(eshell-windows-shell-file): Fix :type.
Dave Love <fx@gnu.org>
parents:
32526
diff
changeset
|
101 :type '(choice file (const nil)) |
29876 | 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... | |
31241 | 108 (setcar args (subst-char-in-string directory-sep-char ?\\ (car args))) |
29876 | 109 (throw 'eshell-replace-command |
31241 | 110 (eshell-parse-command eshell-windows-shell-file (cons "/c" args)))) |
29876 | 111 |
112 (defcustom eshell-interpreter-alist | |
113 (if (eshell-under-windows-p) | |
114 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file))) | |
115 "*An alist defining interpreter substitutions. | |
116 Each member is a cons cell of the form: | |
117 | |
118 (MATCH . INTERPRETER) | |
119 | |
120 MATCH should be a regexp, which is matched against the command name, | |
121 or a function. If either returns a non-nil value, then INTERPRETER | |
122 will be used for that command. | |
123 | |
124 If INTERPRETER is a string, it will be called as the command name, | |
125 with the original command name passed as the first argument, with all | |
126 subsequent arguments following. If INTERPRETER is a function, it will | |
127 be called with all of those arguments. Note that interpreter | |
128 functions should throw `eshell-replace-command' with the alternate | |
129 command form, or they should return a value compatible with the | |
130 possible 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 | |
136 "*A hook run whenever external command lookup fails. | |
137 If a functions wishes to provide an alternate command, they must throw | |
138 it using the tag `eshell-replace-command'. This is done because the | |
139 substituted command need not be external at all, and therefore must be | |
140 passed up to a higher level for re-evaluation. | |
141 | |
142 Or, if the function returns a filename, that filename will be invoked | |
143 with the current command arguments rather than the command specified | |
144 by the user on the command line." | |
145 :type 'hook | |
146 :group 'eshell-ext) | |
147 | |
148 (defcustom eshell-command-interpreter-max-length 256 | |
149 "*The maximum length of any command interpreter string, plus args." | |
150 :type 'integer | |
151 :group 'eshell-ext) | |
152 | |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
153 (defcustom eshell-explicit-command-char ?* |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
154 "*If this char occurs before a command name, call it externally. |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
155 That is, although vi may be an alias, *vi will always call the |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
156 external version. UNIX users may prefer this variable to be \." |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
157 :type 'character |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
158 :group 'eshell-ext) |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
159 |
29876 | 160 ;;; Functions: |
161 | |
162 (defun eshell-ext-initialize () | |
163 "Initialize the external command handling code." | |
164 (make-local-hook 'eshell-named-command-hook) | |
165 (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t)) | |
166 | |
167 (defun eshell-explicit-command (command args) | |
168 "If a command name begins with `*', call it externally always. | |
169 This bypasses all Lisp functions and aliases." | |
170 (when (and (> (length command) 1) | |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
171 (eq (aref command 0) eshell-explicit-command-char)) |
29876 | 172 (let ((cmd (eshell-search-path (substring command 1)))) |
173 (if cmd | |
174 (or (eshell-external-command cmd args) | |
175 (error "%s: external command failed" cmd)) | |
176 (error "%s: external command not found" | |
177 (substring command 1)))))) | |
178 | |
179 (defun eshell-remote-command (handler command args) | |
180 "Insert output from a remote COMMAND, using ARGS. | |
181 A remote command is something that executes on a different machine. | |
182 An external command simply means external to Emacs. | |
183 | |
184 Note that this function is very crude at the moment. It gathers up | |
185 all the output from the remote command, and sends it all at once, | |
186 causing the user to wonder if anything's really going on..." | |
187 (let ((outbuf (generate-new-buffer " *eshell remote output*")) | |
188 (errbuf (generate-new-buffer " *eshell remote error*")) | |
189 (exitcode 1)) | |
190 (unwind-protect | |
191 (progn | |
192 (setq exitcode | |
193 (funcall handler 'shell-command | |
194 (mapconcat 'shell-quote-argument | |
195 (append (list command) args) " ") | |
196 outbuf errbuf)) | |
197 (eshell-print (save-excursion (set-buffer outbuf) | |
198 (buffer-string))) | |
199 (eshell-error (save-excursion (set-buffer errbuf) | |
200 (buffer-string)))) | |
201 (eshell-close-handles exitcode 'nil) | |
202 (kill-buffer outbuf) | |
203 (kill-buffer errbuf)))) | |
204 | |
205 (defun eshell-external-command (command args) | |
206 "Insert output from an external COMMAND, using ARGS." | |
207 (setq args (eshell-stringify-list (eshell-flatten-list args))) | |
208 (let ((handler | |
209 (unless (or (equal default-directory "/") | |
210 (and (eshell-under-windows-p) | |
211 (string-match "\\`[A-Za-z]:[/\\\\]\\'" | |
212 default-directory))) | |
213 (find-file-name-handler default-directory | |
214 'shell-command)))) | |
215 (if handler | |
216 (eshell-remote-command handler command args)) | |
217 (let ((interp (eshell-find-interpreter command))) | |
218 (assert interp) | |
219 (if (functionp (car interp)) | |
220 (apply (car interp) (append (cdr interp) args)) | |
221 (eshell-gather-process-output | |
222 (car interp) (append (cdr interp) args)))))) | |
223 | |
224 (defun eshell/addpath (&rest args) | |
225 "Add a set of paths to PATH." | |
226 (eshell-eval-using-options | |
227 "addpath" args | |
228 '((?b "begin" nil prepend "add path element at beginning") | |
229 (?h "help" nil nil "display this usage message") | |
230 :usage "[-b] PATH | |
231 Adds the given PATH to $PATH.") | |
232 (if args | |
233 (progn | |
234 (if prepend | |
235 (setq args (nreverse args))) | |
236 (while args | |
237 (setenv "PATH" | |
238 (if prepend | |
239 (concat (car args) path-separator | |
240 (getenv "PATH")) | |
241 (concat (getenv "PATH") path-separator | |
242 (car args)))) | |
243 (setq args (cdr args)))) | |
244 (let ((paths (parse-colon-path (getenv "PATH")))) | |
245 (while paths | |
246 (eshell-printn (car paths)) | |
247 (setq paths (cdr paths))))))) | |
248 | |
37661
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
parents:
35717
diff
changeset
|
249 (put 'eshell/addpath 'eshell-no-numeric-conversions t) |
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
parents:
35717
diff
changeset
|
250 |
29876 | 251 (defun eshell-script-interpreter (file) |
252 "Extract the script to run from FILE, if it has #!<interp> in it. | |
253 Return nil, or a list of the form: | |
254 | |
255 (INTERPRETER [ARGS] FILE)" | |
256 (let ((maxlen eshell-command-interpreter-max-length)) | |
257 (if (and (file-readable-p file) | |
258 (file-regular-p file)) | |
259 (with-temp-buffer | |
260 (insert-file-contents-literally file nil 0 maxlen) | |
261 (if (looking-at "#!\\([^ \t\n]+\\)\\([ \t]+\\(.+\\)\\)?") | |
262 (if (match-string 3) | |
263 (list (match-string 1) | |
264 (match-string 3) | |
265 file) | |
266 (list (match-string 1) | |
267 file))))))) | |
268 | |
269 (defun eshell-find-interpreter (file &optional no-examine-p) | |
270 "Find the command interpreter with which to execute FILE. | |
271 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script | |
272 line of the form #!<interp>." | |
273 (let ((finterp | |
274 (catch 'found | |
275 (ignore | |
276 (eshell-for possible eshell-interpreter-alist | |
277 (cond | |
278 ((functionp (car possible)) | |
279 (and (funcall (car possible) file) | |
280 (throw 'found (cdr possible)))) | |
281 ((stringp (car possible)) | |
282 (and (string-match (car possible) file) | |
283 (throw 'found (cdr possible)))) | |
284 (t | |
285 (error "Invalid interpreter-alist test")))))))) | |
286 (if finterp ; first check | |
287 (list finterp file) | |
288 (let ((fullname (if (file-name-directory file) file | |
289 (eshell-search-path file))) | |
290 (suffixes eshell-binary-suffixes)) | |
291 (if (and fullname (not (or eshell-force-execution | |
292 (file-executable-p fullname)))) | |
293 (while suffixes | |
294 (let ((try (concat fullname (car suffixes)))) | |
295 (if (or (file-executable-p try) | |
296 (and eshell-force-execution | |
297 (file-readable-p try))) | |
298 (setq fullname try suffixes nil) | |
299 (setq suffixes (cdr suffixes)))))) | |
300 (cond ((not (and fullname (file-exists-p fullname))) | |
301 (let ((name (or fullname file))) | |
302 (unless (setq fullname | |
303 (run-hook-with-args-until-success | |
304 'eshell-alternate-command-hook file)) | |
305 (error "%s: command not found" name)))) | |
306 ((not (or eshell-force-execution | |
307 (file-executable-p fullname))) | |
308 (error "%s: Permission denied" fullname))) | |
309 (let (interp) | |
310 (unless no-examine-p | |
311 (setq interp (eshell-script-interpreter fullname)) | |
312 (if interp | |
313 (setq interp | |
314 (cons (car (eshell-find-interpreter (car interp) t)) | |
315 (cdr interp))))) | |
316 (or interp (list fullname))))))) | |
317 | |
318 ;;; Code: | |
319 | |
320 ;;; esh-ext.el ends here |