Mercurial > hg > xemacs-beta
annotate lisp/process.el @ 5442:8d3d05ffbb43
Copyright and short license added to etc/gtkrc.
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Sun, 12 Dec 2010 00:38:54 +0100 |
parents | 308d34e9f07d |
children | 11da5b828d10 |
rev | line source |
---|---|
428 | 1 ;;; process.el --- commands for subprocesses; split out of simple.el |
2 | |
3 ;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc. | |
853 | 4 ;; Copyright (C) 1995, 2000, 2001, 2002 Ben Wing. |
428 | 5 |
6 ;; Author: Ben Wing | |
7 ;; Maintainer: XEmacs Development Team | |
8 ;; Keywords: internal, processes, dumped | |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
15 ;; option) any later version. |
428 | 16 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
20 ;; for more details. |
428 | 21 |
22 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4611
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 24 |
771 | 25 ;;; Synched up with: FSF 19.30, except for setenv/getenv (synched with FSF |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
26 ;;; 21.2.1). |
428 | 27 |
442 | 28 ;;; Authorship: |
29 | |
30 ;; Created 1995 by Ben Wing during Mule work -- some commands split out | |
31 ;; of simple.el and wrappers of *-internal functions created so they could | |
32 ;; be redefined in a Mule world. | |
33 ;; Lisp definition of call-process-internal added Mar. 2000 by Ben Wing. | |
34 | |
428 | 35 ;;; Commentary: |
36 | |
37 ;; This file is dumped with XEmacs. | |
38 | |
39 ;;; Code: | |
40 | |
41 | |
42 (defgroup processes nil | |
43 "Process, subshell, compilation, and job control support." | |
44 :group 'external | |
45 :group 'development) | |
46 | |
47 (defgroup processes-basics nil | |
48 "Basic stuff dealing with processes." | |
49 :group 'processes) | |
50 | |
51 (defgroup execute nil | |
52 "Executing external commands." | |
53 :group 'processes) | |
54 | |
611 | 55 ;; This may be changed to "/c" in win32-native.el. |
428 | 56 |
57 (defvar shell-command-switch "-c" | |
58 "Switch used to have the shell execute its command line argument.") | |
59 | |
60 (defun start-process-shell-command (name buffer &rest args) | |
61 "Start a program in a subprocess. Return the process object for it. | |
62 NAME is name for process. It is modified if necessary to make it unique. | |
63 BUFFER is the buffer or (buffer-name) to associate with the process. | |
64 Process output goes at end of that buffer, unless you specify | |
65 an output stream or filter function to handle the output. | |
66 BUFFER may be also nil, meaning that this process is not associated | |
2473 | 67 with any buffer. |
68 Variables `shell-file-name' and `shell-command-switch' are used to | |
69 start the process. | |
428 | 70 Remaining arguments are the arguments for the command. |
71 Wildcards and redirection are handled as usual in the shell." | |
72 ;; We used to use `exec' to replace the shell with the command, | |
73 ;; but that failed to handle (...) and semicolon, etc. | |
74 (start-process name buffer shell-file-name shell-command-switch | |
75 (mapconcat #'identity args " "))) | |
76 | |
862 | 77 (defun process-synchronize-point (proc) |
78 "Set the point(s) in buffer and stderr-buffer according to the process mark." | |
79 ;; We need this because the documentation says to insert *BEFORE* point, | |
80 ;; but we end up inserting after because only the process mark moves | |
81 ;; forward, not point. We synchronize after every place output might | |
82 ;; happen, in sentinels, and in an unwind-protect, to make *SURE* that | |
83 ;; point is correct. (We could do this more easily and perhaps more | |
84 ;; safely using a process filter, but that would create a LOT of garbage | |
85 ;; since all the data would get sent in strings.) We make this a separate | |
86 ;; function, not an flet, due to dynamic binding problems -- the flet may | |
87 ;; not still be in scope when the sentinel is called. | |
88 (let ((pb (process-buffer proc)) | |
89 (pm (process-mark proc))) | |
90 (if (and pb (buffer-live-p pb) (marker-buffer pm)) | |
91 (goto-char pm pb)) | |
92 (if (process-has-separate-stderr-p proc) | |
93 (let ((pseb (process-stderr-buffer proc)) | |
94 (psem (process-stderr-mark proc))) | |
95 (if (and pseb (not (eq pb pseb)) | |
96 (buffer-live-p pseb) | |
97 (marker-buffer psem)) | |
98 (goto-char psem pseb)))))) | |
99 | |
853 | 100 (defun call-process-internal (program &optional infile buffer display |
101 &rest args) | |
102 "Internal function to call PROGRAM synchronously in separate process. | |
103 Lisp callers should use `call-process' or `call-process-region'. | |
104 | |
442 | 105 The program's input comes from file INFILE (nil means `/dev/null'). |
853 | 106 XEmacs feature: INFILE can also be a list of (BUFFER [START [END]]), i.e. |
107 a list of one to three elements, consisting of a buffer and optionally | |
108 a start position or start and end position. In this case, input comes | |
109 from the buffer, starting from START (defaults to the beginning of the | |
110 buffer) and ending at END (defaults to the end of the buffer). | |
111 | |
442 | 112 Insert output in BUFFER before point; t means current buffer; |
113 nil for BUFFER means discard it; 0 means discard and don't wait. | |
853 | 114 If BUFFER is a string, then find or create a buffer with that name, |
115 then insert the output in that buffer, before point. | |
442 | 116 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, |
117 REAL-BUFFER says what to do with standard output, as above, | |
118 while STDERR-FILE says what to do with standard error in the child. | |
119 STDERR-FILE may be nil (discard standard error output), | |
853 | 120 t (mix it with ordinary output), a file name string, or (XEmacs feature) |
121 a buffer object. If STDERR-FILE is a buffer object (but not the name of | |
122 a buffer, since that would be interpreted as a file), the standard error | |
123 output will be inserted into the buffer before point. | |
442 | 124 |
125 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted. | |
126 Remaining arguments are strings passed as command arguments to PROGRAM. | |
127 | |
853 | 128 If BUFFER is 0, returns immediately with value nil. |
129 Otherwise waits for PROGRAM to terminate and returns a numeric exit status | |
130 or a signal description string. If you quit, the process is first killed | |
131 with SIGINT, then with SIGKILL if you quit again before the process exits. | |
132 | |
133 Coding systems for the process are the same as for `start-process-internal'." | |
134 (let (proc inbuf errbuf kill-inbuf kill-errbuf no-wait start end) | |
135 ;; first set up an unwind-protect to clean everything up. this will: | |
136 ;; | |
137 ;; -- kill the process. (when we're not waiting for it to finish, we | |
138 ;; set PROC to nil when we're ready to exit so this doesn't happen -- | |
139 ;; if we're interrupted before we're ready to exit, we should still | |
140 ;; kill the process) | |
141 ;; -- kill temporary buffers created to handle I/O to or from a file. | |
142 ;; KILL-INBUF/KILL-ERRBUF tell us if we should do so. | |
143 ;; | |
144 ;; note that we need to be *very* careful in this code to handle C-g | |
145 ;; at any point. | |
146 (unwind-protect | |
147 (progn | |
148 ;; first handle INFILE. | |
149 (cond ((stringp infile) | |
150 (setq infile (expand-file-name infile)) | |
151 (setq kill-inbuf t) | |
152 (setq inbuf (generate-new-buffer "*call-process*")) | |
153 ;; transfer the exact contents of the file to the process. | |
154 ;; we do that by reading in and writing out in | |
155 ;; binary. #### is this even correct? should we be doing | |
156 ;; the same thing with stderr? if so we'd need a way of | |
157 ;; controlling the stderr coding system separate from | |
158 ;; everything else. | |
159 (with-current-buffer inbuf | |
160 ;; Make sure this works with jka-compr | |
161 (let ((file-name-handler-alist nil)) | |
162 (insert-file-contents-internal infile nil nil nil nil | |
163 'binary)) | |
164 (setq start (point-min) end (point-max)))) | |
165 ((consp infile) | |
166 (setq inbuf (get-buffer (car infile))) | |
167 (setq start (or (nth 1 infile) (point-min inbuf))) | |
168 (setq end (or (nth 2 infile) (point-max inbuf)))) | |
169 ((null infile) nil) | |
170 (t | |
171 (error 'wrong-type-argument | |
172 "Must be filename or (BUFFER [START [END]])" | |
173 infile))) | |
174 ;; now handle BUFFER | |
175 (let ((stderr (if (consp buffer) (second buffer) t))) | |
176 (if (consp buffer) (setq buffer (car buffer))) | |
177 (setq buffer | |
178 (cond ((null buffer) nil) | |
179 ((eq buffer t) (current-buffer)) | |
180 ;; use integerp for compatibility with existing | |
181 ;; call-process rmsism. | |
182 ((integerp buffer) (setq no-wait t) nil) | |
183 (t (get-buffer-create buffer)))) | |
184 (when (and stderr (not (eq t stderr))) | |
185 ;; both ERRBUF and STDERR being non-nil indicates to the | |
186 ;; code below that STDERR is a file and we should write | |
187 ;; ERRBUF to it; so clear out STDERR if we don't want this. | |
188 (if (bufferp stderr) (setq errbuf stderr stderr nil) | |
442 | 189 (setq stderr (expand-file-name stderr)) |
853 | 190 (setq kill-errbuf t) |
191 (setq errbuf (generate-new-buffer "*call-process*")))) | |
192 ;; now start process. using a pty causes all sorts of | |
193 ;; weirdness, at least under cygwin, when there's input. #### i | |
194 ;; don't know what's going wrong and whether it's a cygwin-only | |
195 ;; problem. suffice to say that there were NO pty connections | |
196 ;; in the old version. | |
197 (let ((process-connection-type nil)) | |
442 | 198 (setq proc |
199 (apply 'start-process-internal "*call-process*" | |
853 | 200 (if (eq t stderr) buffer (list buffer errbuf)) |
201 program args))) | |
202 ;; see comment above where the data was read from the file. | |
203 (if kill-inbuf | |
204 (set-process-output-coding-system proc 'binary)) | |
205 ;; point mark/stderr-mark at the right place (by default it's | |
206 ;; end of buffer). | |
207 (if buffer | |
208 (set-marker (process-mark proc) (point buffer) buffer)) | |
209 (if errbuf | |
210 (set-marker (process-stderr-mark proc) (point errbuf) errbuf)) | |
859 | 211 ;; now do I/O, very carefully! the unwind-protect makes sure |
212 ;; to clear out the sentinel, since it does a `throw', which | |
213 ;; would have no catch (or writes to a file -- we only want | |
214 ;; this on normal exit) | |
862 | 215 (unwind-protect |
216 ;; if not NO-WAIT, set a sentinel to return the exit | |
217 ;; status. it will throw to this catch so we can exit | |
218 ;; properly. | |
219 (catch 'call-process-done | |
220 (set-process-sentinel | |
221 proc | |
222 (cond | |
223 ((and no-wait errbuf stderr) | |
224 ;; we're trying really really hard to emulate | |
225 ;; the old call-process, which would save the | |
226 ;; stderr to a file even if discarding output. so | |
227 ;; we set a sentinel to save the output when | |
228 ;; we finish. | |
229 ;; | |
230 ;; #### not clear if we should be doing this. | |
231 ;; | |
232 ;; NOTE NOTE NOTE: Due to the total bogosity of | |
233 ;; dynamic scoping, and the lack of closures, we | |
234 ;; have to be careful how we write the first | |
235 ;; sentinel below since it may be executed after | |
236 ;; this function has returned -- thus we fake a | |
237 ;; closure. (This doesn't apply to the second one, | |
238 ;; which only gets executed within the | |
239 ;; unwind-protect.) | |
240 `(lambda (proc status) | |
241 (set-process-sentinel proc nil) | |
242 (process-synchronize-point proc) | |
243 (with-current-buffer ,errbuf | |
244 (write-region-internal | |
245 1 (1+ (buffer-size)) | |
246 ,stderr | |
247 nil 'major-rms-kludge-city nil | |
248 coding-system-for-write)) | |
249 (kill-buffer ,errbuf))) | |
250 (no-wait nil) | |
251 (t | |
252 ;; normal sentinel: maybe write out stderr and return | |
253 ;; status. | |
254 #'(lambda (proc status) | |
255 (process-synchronize-point proc) | |
256 (when (and errbuf stderr) | |
257 (with-current-buffer errbuf | |
258 (write-region-internal | |
259 1 (1+ (buffer-size)) stderr | |
260 nil 'major-rms-kludge-city nil | |
261 coding-system-for-write))) | |
262 (cond ((eq 'exit (process-status proc)) | |
263 (set-process-sentinel proc nil) | |
264 (throw 'call-process-done | |
265 (process-exit-status proc))) | |
266 ((eq 'signal (process-status proc)) | |
267 (set-process-sentinel proc nil) | |
268 (throw 'call-process-done status))))))) | |
269 (if (not no-wait) | |
270 ;; we're waiting. send the input and loop forever, | |
271 ;; handling process output and maybe redisplaying. | |
272 ;; exit happens through the sentinel or C-g. if | |
273 ;; C-g, send SIGINT the first time, EOF if not | |
274 ;; already done so (might make the process exit), | |
275 ;; and keep waiting. Another C-g will exit the | |
276 ;; whole function, and the unwind-protect will | |
277 ;; kill the process. (Hence the documented semantics | |
278 ;; of SIGINT/SIGKILL.) | |
279 (let (eof-sent) | |
280 (condition-case nil | |
281 (progn | |
282 (when inbuf | |
283 (process-send-region proc start end inbuf)) | |
284 (process-send-eof proc) | |
285 (setq eof-sent t) | |
286 (while t | |
287 (accept-process-output proc) | |
288 (process-synchronize-point proc) | |
289 (if display (sit-for 0)))) | |
290 (quit | |
291 (process-send-signal 'SIGINT proc) | |
292 (unless eof-sent | |
293 (process-send-eof proc)) | |
294 (while t | |
295 (accept-process-output proc) | |
296 (process-synchronize-point proc) | |
297 (if display (sit-for 0)))))) | |
298 ;; discard and no wait: send the input, set PROC | |
299 ;; and ERRBUF to nil so that the unwind-protect | |
300 ;; forms don't erase the sentinel, kill the process, | |
301 ;; or kill ERRBUF (the sentinel does that), and exit. | |
302 (when inbuf | |
303 (process-send-region proc start end inbuf)) | |
304 (process-send-eof proc) | |
305 (setq errbuf nil) | |
306 (setq proc nil))) | |
307 ;; inner unwind-protect, once we're ready to do I/O. | |
308 (when proc | |
309 (set-process-sentinel proc nil) | |
310 (process-synchronize-point proc))))) | |
859 | 311 ;; outer unwind-protect forms, to make sure we always clean up. |
853 | 312 (if (and inbuf kill-inbuf) (kill-buffer inbuf)) |
313 (if (and errbuf kill-errbuf) (kill-buffer errbuf)) | |
314 (condition-case nil | |
315 (if (and proc (process-live-p proc)) (kill-process proc)) | |
316 (error nil))))) | |
428 | 317 |
318 | |
319 (defun shell-command (command &optional output-buffer) | |
320 "Execute string COMMAND in inferior shell; display output, if any. | |
321 | |
322 If COMMAND ends in ampersand, execute it asynchronously. | |
323 The output appears in the buffer `*Async Shell Command*'. | |
324 That buffer is in shell mode. | |
325 | |
326 Otherwise, COMMAND is executed synchronously. The output appears in the | |
327 buffer `*Shell Command Output*'. | |
328 If the output is one line, it is displayed in the echo area *as well*, | |
329 but it is nonetheless available in buffer `*Shell Command Output*', | |
330 even though that buffer is not automatically displayed. | |
331 If there is no output, or if output is inserted in the current buffer, | |
332 then `*Shell Command Output*' is deleted. | |
333 | |
334 The optional second argument OUTPUT-BUFFER, if non-nil, | |
335 says to put the output in some other buffer. | |
336 If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
337 If OUTPUT-BUFFER is not a buffer and not nil, | |
338 insert output in current buffer. (This cannot be done asynchronously.) | |
339 In either case, the output is inserted after point (leaving mark after it)." | |
340 (interactive (list (read-shell-command "Shell command: ") | |
341 current-prefix-arg)) | |
342 (if (and output-buffer | |
343 (not (or (bufferp output-buffer) (stringp output-buffer)))) | |
344 (progn (barf-if-buffer-read-only) | |
444 | 345 (push-mark nil (not (interactive-p))) |
428 | 346 ;; We do not use -f for csh; we will not support broken use of |
347 ;; .cshrcs. Even the BSD csh manual says to use | |
348 ;; "if ($?prompt) exit" before things which are not useful | |
349 ;; non-interactively. Besides, if someone wants their other | |
350 ;; aliases for shell commands then they can still have them. | |
351 (call-process shell-file-name nil t nil | |
352 shell-command-switch command) | |
353 (exchange-point-and-mark t)) | |
354 ;; Preserve the match data in case called from a program. | |
355 (save-match-data | |
356 (if (string-match "[ \t]*&[ \t]*$" command) | |
357 ;; Command ending with ampersand means asynchronous. | |
358 (progn | |
776 | 359 (if-fboundp 'background |
3491 | 360 (background (substring command 0 (match-beginning 0)) |
361 output-buffer) | |
776 | 362 (error |
363 'unimplemented | |
364 "backgrounding a shell command requires package `background'"))) | |
365 | |
428 | 366 (shell-command-on-region (point) (point) command output-buffer))))) |
367 | |
368 ;; We have a sentinel to prevent insertion of a termination message | |
369 ;; in the buffer itself. | |
370 (defun shell-command-sentinel (process signal) | |
371 (if (memq (process-status process) '(exit signal)) | |
372 (message "%s: %s." | |
373 (car (cdr (cdr (process-command process)))) | |
374 (substring signal 0 -1)))) | |
375 | |
376 (defun shell-command-on-region (start end command | |
377 &optional output-buffer replace) | |
378 "Execute string COMMAND in inferior shell with region as input. | |
379 Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
380 Prefix arg means replace the region with it. | |
381 | |
382 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE. | |
383 If REPLACE is non-nil, that means insert the output | |
384 in place of text from START to END, putting point and mark around it. | |
385 | |
386 If the output is one line, it is displayed in the echo area, | |
387 but it is nonetheless available in buffer `*Shell Command Output*' | |
388 even though that buffer is not automatically displayed. | |
389 If there is no output, or if output is inserted in the current buffer, | |
390 then `*Shell Command Output*' is deleted. | |
391 | |
392 If the optional fourth argument OUTPUT-BUFFER is non-nil, | |
393 that says to put the output in some other buffer. | |
394 If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
395 If OUTPUT-BUFFER is not a buffer and not nil, | |
396 insert output in the current buffer. | |
397 In either case, the output is inserted after point (leaving mark after it)." | |
398 (interactive (let ((string | |
399 ;; Do this before calling region-beginning | |
400 ;; and region-end, in case subprocess output | |
401 ;; relocates them while we are in the minibuffer. | |
402 (read-shell-command "Shell command on region: "))) | |
403 ;; call-interactively recognizes region-beginning and | |
404 ;; region-end specially, leaving them in the history. | |
405 (list (region-beginning) (region-end) | |
406 string | |
407 current-prefix-arg | |
408 current-prefix-arg))) | |
409 (if (or replace | |
410 (and output-buffer | |
411 (not (or (bufferp output-buffer) (stringp output-buffer))))) | |
412 ;; Replace specified region with output from command. | |
413 (let ((swap (and replace (< start end)))) | |
414 ;; Don't muck with mark unless REPLACE says we should. | |
415 (goto-char start) | |
416 (and replace (push-mark)) | |
417 (call-process-region start end shell-file-name t t nil | |
418 shell-command-switch command) | |
419 (let ((shell-buffer (get-buffer "*Shell Command Output*"))) | |
420 (and shell-buffer (not (eq shell-buffer (current-buffer))) | |
421 (kill-buffer shell-buffer))) | |
422 ;; Don't muck with mark unless REPLACE says we should. | |
423 (and replace swap (exchange-point-and-mark t))) | |
424 ;; No prefix argument: put the output in a temp buffer, | |
425 ;; replacing its entire contents. | |
426 (let ((buffer (get-buffer-create | |
427 (or output-buffer "*Shell Command Output*"))) | |
428 (success nil) | |
429 (exit-status nil) | |
430 (directory default-directory)) | |
431 (unwind-protect | |
432 (if (eq buffer (current-buffer)) | |
433 ;; If the input is the same buffer as the output, | |
434 ;; delete everything but the specified region, | |
435 ;; then replace that region with the output. | |
436 (progn (setq buffer-read-only nil) | |
437 (delete-region (max start end) (point-max)) | |
921 | 438 (delete-region (point-min) (min start end)) |
428 | 439 (setq exit-status |
440 (call-process-region (point-min) (point-max) | |
441 shell-file-name t t nil | |
442 shell-command-switch command)) | |
443 (setq success t)) | |
444 ;; Clear the output buffer, | |
445 ;; then run the command with output there. | |
446 (save-excursion | |
447 (set-buffer buffer) | |
448 (setq buffer-read-only nil) | |
449 ;; XEmacs change | |
450 (setq default-directory directory) | |
451 (erase-buffer)) | |
452 (setq exit-status | |
453 (call-process-region start end shell-file-name | |
454 nil buffer nil | |
455 shell-command-switch command)) | |
456 (setq success t)) | |
457 ;; Report the amount of output. | |
458 (let ((lines (save-excursion | |
459 (set-buffer buffer) | |
460 (if (= (buffer-size) 0) | |
461 0 | |
462 (count-lines (point-min) (point-max)))))) | |
463 (cond ((= lines 0) | |
464 (if success | |
465 (display-message | |
466 'command | |
467 (if (eql exit-status 0) | |
468 "(Shell command succeeded with no output)" | |
469 "(Shell command failed with no output)"))) | |
470 (kill-buffer buffer)) | |
471 ((and success (= lines 1)) | |
472 (message "%s" | |
473 (save-excursion | |
474 (set-buffer buffer) | |
475 (goto-char (point-min)) | |
476 (buffer-substring (point) | |
477 (progn (end-of-line) | |
478 (point)))))) | |
479 (t | |
480 (set-window-start (display-buffer buffer) 1)))))))) | |
481 | |
482 (defun shell-quote-argument (argument) | |
483 "Quote an argument for passing as argument to an inferior shell." | |
442 | 484 (if (and (eq system-type 'windows-nt) |
485 (let ((progname (downcase (file-name-nondirectory | |
486 shell-file-name)))) | |
487 (or (equal progname "command.com") | |
488 (equal progname "cmd.exe")))) | |
489 ;; the expectation is that you can take the result of | |
490 ;; shell-quote-argument and pass it to as an arg to | |
491 ;; (start-process shell-quote-argument ...) and have it end | |
492 ;; up as-is in the program's argv[] array. to do this, we | |
493 ;; need to protect against both the shell's and the program's | |
494 ;; quoting conventions (and our own conventions in | |
495 ;; mswindows-construct-process-command-line!). Putting quotes | |
496 ;; around shell metachars gets through the last two, and applying | |
497 ;; the normal VC runtime quoting works with practically all apps. | |
776 | 498 (declare-fboundp (mswindows-quote-one-vc-runtime-arg argument t)) |
611 | 499 (if (equal argument "") |
500 "\"\"" | |
501 ;; Quote everything except POSIX filename characters. | |
502 ;; This should be safe enough even for really weird shells. | |
503 (let ((result "") (start 0) end) | |
504 (while (string-match "[^-0-9a-zA-Z_./]" argument start) | |
505 (setq end (match-beginning 0) | |
506 result (concat result (substring argument start end) | |
507 "\\" (substring argument end (1+ end))) | |
508 start (1+ end))) | |
509 (concat result (substring argument start)))))) | |
428 | 510 |
438 | 511 (defun shell-command-to-string (command) |
512 "Execute shell command COMMAND and return its output as a string." | |
428 | 513 (with-output-to-string |
1189 | 514 (with-current-buffer standard-output |
515 (call-process shell-file-name nil t nil shell-command-switch command)))) | |
428 | 516 |
438 | 517 (defalias 'exec-to-string 'shell-command-to-string) |
771 | 518 |
519 | |
520 ;; History list for environment variable names. | |
521 (defvar read-envvar-name-history nil) | |
522 | |
523 (defun read-envvar-name (prompt &optional mustmatch) | |
524 "Read environment variable name, prompting with PROMPT. | |
525 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. | |
526 If it is also not t, RET does not exit if it does non-null completion." | |
527 (completing-read prompt | |
528 (mapcar (function | |
529 (lambda (enventry) | |
530 (list (substring enventry 0 | |
531 (string-match "=" enventry))))) | |
532 process-environment) | |
533 nil mustmatch nil 'read-envvar-name-history)) | |
534 | |
535 ;; History list for VALUE argument to setenv. | |
536 (defvar setenv-history nil) | |
537 | |
930 | 538 (defun substitute-env-vars (string) |
539 "Substitute environment variables referred to in STRING. | |
540 `$FOO' where FOO is an environment variable name means to substitute | |
541 the value of that variable. The variable name should be terminated | |
542 with a character not a letter, digit or underscore; otherwise, enclose | |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
543 the entire variable name in braces. For instance, in `ab$cd-x', |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
544 `$cd' is treated as an environment variable. |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
545 |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
546 Use `$$' to insert a single dollar sign." |
930 | 547 (let ((start 0)) |
548 (while (string-match | |
549 ;; XEmacs change - FSF use their rx macro to generate this regexp | |
550 "\\(?:\\$\\(\\(?:[a-zA-Z0-9_]\\)+\\)\\)\\|\\(?:\\${\\(\\(?:.\\|\n\\)*?\\)}\\)\\|\\$\\$" | |
551 string start) | |
552 (cond ((match-beginning 1) | |
553 (let ((value (getenv (match-string 1 string)))) | |
554 (setq string (replace-match (or value "") t t string) | |
555 start (+ (match-beginning 0) (length value))))) | |
556 ((match-beginning 2) | |
557 (let ((value (getenv (match-string 2 string)))) | |
558 (setq string (replace-match (or value "") t t string) | |
559 start (+ (match-beginning 0) (length value))))) | |
560 (t | |
561 (setq string (replace-match "$" t t string) | |
562 start (+ (match-beginning 0) 1))))) | |
563 string)) | |
564 | |
565 (defun setenv (variable &optional value unset substitute-env-vars) | |
771 | 566 "Set the value of the environment variable named VARIABLE to VALUE. |
567 VARIABLE should be a string. VALUE is optional; if not provided or is | |
568 `nil', the environment variable VARIABLE will be removed. | |
569 | |
930 | 570 UNSET, if non-nil, means to remove VARIABLE from the environment. |
571 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment | |
572 variables in VALUE using `substitute-env-vars'. | |
573 | |
771 | 574 Interactively, a prefix argument means to unset the variable. |
575 Interactively, the current value (if any) of the variable | |
576 appears at the front of the history list when you type in the new value. | |
577 | |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
578 This function works by modifying `process-environment'. |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
579 |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
580 As a special case, setting variable `TZ' calls `set-time-zone-rule' as |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
581 a side-effect." |
771 | 582 (interactive |
583 (if current-prefix-arg | |
584 (list (read-envvar-name "Clear environment variable: " 'exact) nil t) | |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
585 (let* ((var (read-envvar-name "Set environment variable: " nil)) |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
586 (value (getenv var))) |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
587 (when value |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
588 (push value setenv-history)) |
771 | 589 ;; Here finally we specify the args to call setenv with. |
590 (list var (read-from-minibuffer (format "Set %s to value: " var) | |
591 nil nil nil 'setenv-history | |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
592 ;; XEmacs change; don't specify a |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
593 ;; default. (Nor an abbrev table.) |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
594 ))))) |
930 | 595 (if unset |
596 (setq value nil) | |
597 (if substitute-env-vars | |
598 (setq value (substitute-env-vars value)))) | |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
599 |
4611
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
600 ;; GNU fuck around with coding systems here. We do it at a much lower |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
601 ;; level; an equivalent of the following code of Handa's would be |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
602 ;; worthwhile here, though: |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
603 |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
604 ; (let ((codings (find-coding-systems-string (concat variable value)))) |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
605 ; (unless (or (eq 'undecided (car codings)) |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
606 ; (memq (coding-system-base locale-coding-system) codings)) |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
607 ; (error "Can't encode `%s=%s' with `locale-coding-system'" |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
608 ; variable (or value ""))))) |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
609 |
9c97a5a8c241
Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents:
4610
diff
changeset
|
610 ;; But then right now our find-coding-systems analogue is in packages. |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
611 |
771 | 612 (if (string-match "=" variable) |
613 (error "Environment variable name `%s' contains `='" variable) | |
614 (let ((pattern (concat "\\`" (regexp-quote (concat variable "=")))) | |
615 (case-fold-search nil) | |
616 (scan process-environment) | |
617 found) | |
618 (if (string-equal "TZ" variable) | |
619 (set-time-zone-rule value)) | |
620 (while scan | |
621 (cond ((string-match pattern (car scan)) | |
622 (setq found t) | |
623 (if (eq nil value) | |
624 (setq process-environment (delq (car scan) process-environment)) | |
625 (setcar scan (concat variable "=" value))) | |
626 (setq scan nil))) | |
627 (setq scan (cdr scan))) | |
628 (or found | |
629 (if value | |
630 (setq process-environment | |
631 (cons (concat variable "=" value) | |
632 process-environment))))))) | |
633 | |
634 ;; already in C. Can't move it to Lisp too easily because it's needed | |
635 ;; extremely early in the Lisp loadup sequence. | |
636 | |
637 ; (defun getenv (variable) | |
638 ; "Get the value of environment variable VARIABLE. | |
639 ; VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
640 ; the environment. Otherwise, value is a string. | |
641 ; | |
642 ; This function consults the variable `process-environment' | |
643 ; for its value." | |
644 ; (interactive (list (read-envvar-name "Get environment variable: " t))) | |
645 ; (let ((value (getenv-internal variable))) | |
646 ; (when (interactive-p) | |
647 ; (message "%s" (if value value "Not set"))) | |
648 ; value)) | |
649 | |
4327
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
650 |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
651 ;; GNU have an #'environment function here, as of 2007-12-14. If someone |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
652 ;; actually uses it in the wild, this would suffice as an implementation: |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
653 |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
654 ; (defun environment (&optional frame) |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
655 ; "Return a list of environment variables with their values. |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
656 ; Each entry in the list is a string of the form NAME=VALUE. |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
657 ; |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
658 ; Optional argument FRAME is ignored, for GNU compatibility. |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
659 ; |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
660 ; Non-ASCII characters look like Mojibake (that is, they are unreadable.)" |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
661 ; (loop |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
662 ; for entry in process-environment |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
663 ; collect (encode-coding-string entry 'native))) |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
664 ; |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
665 |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
666 ;; but we shouldn't encourage that sort of ugliness and needless backwards |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
667 ;; incompatibility. |
466ad8ad5f13
Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3491
diff
changeset
|
668 |
771 | 669 (provide 'env) ;; Yuck. Formerly the above were in env.el, which did this |
670 ;; provide. | |
428 | 671 |
672 ;;; process.el ends here |