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