Commit | Line | Data |
---|---|---|
affbf647 GM |
1 | ;;; esh-ext --- commands external to Eshell |
2 | ||
faadfb0a | 3 | ;; Copyright (C) 1999, 2000 Free Software Foundation |
affbf647 GM |
4 | |
5 | ;; This file is part of GNU Emacs. | |
6 | ||
7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 | ;; it under the terms of the GNU General Public License as published by | |
9 | ;; the Free Software Foundation; either version 2, or (at your option) | |
10 | ;; any later version. | |
11 | ||
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;; GNU General Public License for more details. | |
16 | ||
17 | ;; You should have received a copy of the GNU General Public License | |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
19 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | ;; Boston, MA 02111-1307, USA. | |
21 | ||
22 | (provide 'esh-ext) | |
23 | ||
24 | (eval-when-compile (require 'esh-maint)) | |
25 | ||
26 | (defgroup eshell-ext nil | |
27 | "External commands are invoked when operating system executables are | |
28 | loaded into memory, thus beginning a new process." | |
29 | :tag "External commands" | |
30 | :group 'eshell) | |
31 | ||
32 | ;;; Commentary: | |
33 | ||
34 | ;; To force a command to invoked external, either provide an explicit | |
35 | ;; pathname for the command argument, or prefix the command name with | |
36 | ;; an asterix character. Example: | |
37 | ;; | |
38 | ;; grep ; make invoke `grep' Lisp function, or `eshell/grep' | |
39 | ;; /bin/grep ; will definitely invoke /bin/grep | |
40 | ;; *grep ; will also invoke /bin/grep | |
41 | ||
42 | ;;; User Variables: | |
43 | ||
44 | (defcustom eshell-ext-load-hook '(eshell-ext-initialize) | |
45 | "*A hook that gets run when `eshell-ext' is loaded." | |
46 | :type 'hook | |
47 | :group 'eshell-ext) | |
48 | ||
49 | (defcustom eshell-binary-suffixes | |
50 | (if (eshell-under-windows-p) | |
51 | '(".exe" ".com" ".bat" ".cmd" "") | |
52 | '("")) | |
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") | |
94 | (eshell-search-path "command.exe")) | |
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." | |
99 | :type 'file | |
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. | |
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 | ||
151 | ;;; Functions: | |
152 | ||
153 | (defun eshell-ext-initialize () | |
154 | "Initialize the external command handling code." | |
155 | (make-local-hook 'eshell-named-command-hook) | |
156 | (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t)) | |
157 | ||
158 | (defun eshell-explicit-command (command args) | |
159 | "If a command name begins with `*', call it externally always. | |
160 | This bypasses all Lisp functions and aliases." | |
161 | (when (and (> (length command) 1) | |
162 | (eq (aref command 0) ?*)) | |
163 | (let ((cmd (eshell-search-path (substring command 1)))) | |
164 | (if cmd | |
165 | (or (eshell-external-command cmd args) | |
166 | (error "%s: external command failed" cmd)) | |
167 | (error "%s: external command not found" | |
168 | (substring command 1)))))) | |
169 | ||
170 | (defun eshell-remote-command (handler command args) | |
171 | "Insert output from a remote COMMAND, using ARGS. | |
172 | A remote command is something that executes on a different machine. | |
173 | An external command simply means external to Emacs. | |
174 | ||
175 | Note that this function is very crude at the moment. It gathers up | |
176 | all the output from the remote command, and sends it all at once, | |
177 | causing the user to wonder if anything's really going on..." | |
178 | (let ((outbuf (generate-new-buffer " *eshell remote output*")) | |
179 | (errbuf (generate-new-buffer " *eshell remote error*")) | |
180 | (exitcode 1)) | |
181 | (unwind-protect | |
182 | (progn | |
183 | (setq exitcode | |
184 | (funcall handler 'shell-command | |
185 | (mapconcat 'shell-quote-argument | |
186 | (append (list command) args) " ") | |
187 | outbuf errbuf)) | |
188 | (eshell-print (save-excursion (set-buffer outbuf) | |
189 | (buffer-string))) | |
190 | (eshell-error (save-excursion (set-buffer errbuf) | |
191 | (buffer-string)))) | |
192 | (eshell-close-handles exitcode 'nil) | |
193 | (kill-buffer outbuf) | |
194 | (kill-buffer errbuf)))) | |
195 | ||
196 | (defun eshell-external-command (command args) | |
197 | "Insert output from an external COMMAND, using ARGS." | |
198 | (setq args (eshell-stringify-list (eshell-flatten-list args))) | |
199 | (let ((handler | |
200 | (unless (or (equal default-directory "/") | |
201 | (and (eshell-under-windows-p) | |
202 | (string-match "\\`[A-Za-z]:[/\\\\]\\'" | |
203 | default-directory))) | |
204 | (find-file-name-handler default-directory | |
205 | 'shell-command)))) | |
206 | (if handler | |
207 | (eshell-remote-command handler command args)) | |
208 | (let ((interp (eshell-find-interpreter command))) | |
209 | (assert interp) | |
210 | (if (functionp (car interp)) | |
211 | (apply (car interp) (append (cdr interp) args)) | |
212 | (eshell-gather-process-output | |
213 | (car interp) (append (cdr interp) args)))))) | |
214 | ||
215 | (defun eshell/addpath (&rest args) | |
216 | "Add a set of paths to PATH." | |
217 | (eshell-eval-using-options | |
218 | "addpath" args | |
219 | '((?b "begin" nil prepend "add path element at beginning") | |
220 | (?h "help" nil nil "display this usage message") | |
221 | :usage "[-b] PATH | |
222 | Adds the given PATH to $PATH.") | |
223 | (if args | |
224 | (progn | |
225 | (if prepend | |
226 | (setq args (nreverse args))) | |
227 | (while args | |
228 | (setenv "PATH" | |
229 | (if prepend | |
230 | (concat (car args) path-separator | |
231 | (getenv "PATH")) | |
232 | (concat (getenv "PATH") path-separator | |
233 | (car args)))) | |
234 | (setq args (cdr args)))) | |
235 | (let ((paths (parse-colon-path (getenv "PATH")))) | |
236 | (while paths | |
237 | (eshell-printn (car paths)) | |
238 | (setq paths (cdr paths))))))) | |
239 | ||
240 | (defun eshell-script-interpreter (file) | |
241 | "Extract the script to run from FILE, if it has #!<interp> in it. | |
242 | Return nil, or a list of the form: | |
243 | ||
244 | (INTERPRETER [ARGS] FILE)" | |
245 | (let ((maxlen eshell-command-interpreter-max-length)) | |
246 | (if (and (file-readable-p file) | |
247 | (file-regular-p file)) | |
248 | (with-temp-buffer | |
249 | (insert-file-contents-literally file nil 0 maxlen) | |
250 | (if (looking-at "#!\\([^ \t\n]+\\)\\([ \t]+\\(.+\\)\\)?") | |
251 | (if (match-string 3) | |
252 | (list (match-string 1) | |
253 | (match-string 3) | |
254 | file) | |
255 | (list (match-string 1) | |
256 | file))))))) | |
257 | ||
258 | (defun eshell-find-interpreter (file &optional no-examine-p) | |
259 | "Find the command interpreter with which to execute FILE. | |
260 | If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script | |
261 | line of the form #!<interp>." | |
262 | (let ((finterp | |
263 | (catch 'found | |
264 | (ignore | |
265 | (eshell-for possible eshell-interpreter-alist | |
266 | (cond | |
267 | ((functionp (car possible)) | |
268 | (and (funcall (car possible) file) | |
269 | (throw 'found (cdr possible)))) | |
270 | ((stringp (car possible)) | |
271 | (and (string-match (car possible) file) | |
272 | (throw 'found (cdr possible)))) | |
273 | (t | |
274 | (error "Invalid interpreter-alist test")))))))) | |
275 | (if finterp ; first check | |
276 | (list finterp file) | |
277 | (let ((fullname (if (file-name-directory file) file | |
278 | (eshell-search-path file))) | |
279 | (suffixes eshell-binary-suffixes)) | |
280 | (if (and fullname (not (or eshell-force-execution | |
281 | (file-executable-p fullname)))) | |
282 | (while suffixes | |
283 | (let ((try (concat fullname (car suffixes)))) | |
284 | (if (or (file-executable-p try) | |
285 | (and eshell-force-execution | |
286 | (file-readable-p try))) | |
287 | (setq fullname try suffixes nil) | |
288 | (setq suffixes (cdr suffixes)))))) | |
289 | (cond ((not (and fullname (file-exists-p fullname))) | |
290 | (let ((name (or fullname file))) | |
291 | (unless (setq fullname | |
292 | (run-hook-with-args-until-success | |
293 | 'eshell-alternate-command-hook file)) | |
294 | (error "%s: command not found" name)))) | |
295 | ((not (or eshell-force-execution | |
296 | (file-executable-p fullname))) | |
297 | (error "%s: Permission denied" fullname))) | |
298 | (let (interp) | |
299 | (unless no-examine-p | |
300 | (setq interp (eshell-script-interpreter fullname)) | |
301 | (if interp | |
302 | (setq interp | |
303 | (cons (car (eshell-find-interpreter (car interp) t)) | |
304 | (cdr interp))))) | |
305 | (or interp (list fullname))))))) | |
306 | ||
307 | ;;; Code: | |
308 | ||
309 | ;;; esh-ext.el ends here |