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.
|
771
|
4 ;; Copyright (C) 1995, 2000, 2001 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 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
|
|
65 NAME is name for process. It is modified if necessary to make it unique.
|
|
66 BUFFER is the buffer or (buffer-name) to associate with the process.
|
|
67 Process output goes at end of that buffer, unless you specify
|
|
68 an output stream or filter function to handle the output.
|
|
69 BUFFER may be also nil, meaning that this process is not associated
|
|
70 with any buffer
|
|
71 Third arg is command name, the name of a shell command.
|
|
72 Remaining arguments are the arguments for the command.
|
|
73 Wildcards and redirection are handled as usual in the shell."
|
|
74 ;; We used to use `exec' to replace the shell with the command,
|
|
75 ;; but that failed to handle (...) and semicolon, etc.
|
|
76 (start-process name buffer shell-file-name shell-command-switch
|
|
77 (mapconcat #'identity args " ")))
|
|
78
|
442
|
79 (defun call-process-internal (program &optional infile buffer display &rest args)
|
|
80 "Call PROGRAM synchronously in separate process, with coding-system specified.
|
|
81 Arguments are
|
|
82 (PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS).
|
|
83 The program's input comes from file INFILE (nil means `/dev/null').
|
|
84 Insert output in BUFFER before point; t means current buffer;
|
|
85 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
86 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
87 REAL-BUFFER says what to do with standard output, as above,
|
|
88 while STDERR-FILE says what to do with standard error in the child.
|
|
89 STDERR-FILE may be nil (discard standard error output),
|
|
90 t (mix it with ordinary output), or a file name string.
|
|
91
|
|
92 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
|
|
93 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
94
|
|
95 If BUFFER is 0, `call-process' returns immediately with value nil.
|
|
96 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
|
|
97 or a signal description string.
|
|
98 If you quit, the process is killed with SIGINT, or SIGKILL if you
|
|
99 quit again."
|
|
100 ;; #### remove windows-nt check when this is ready for prime time.
|
814
|
101 (if (not (eq 'windows-nt system-type))
|
442
|
102 (apply 'old-call-process-internal program infile buffer display args)
|
|
103 (let (proc inbuf errbuf discard)
|
|
104 (unwind-protect
|
|
105 (progn
|
|
106 (when infile
|
|
107 (setq infile (expand-file-name infile))
|
|
108 (setq inbuf (generate-new-buffer "*call-process*"))
|
|
109 (with-current-buffer inbuf
|
|
110 ;; Make sure this works with jka-compr
|
|
111 (let ((file-name-handler-alist nil))
|
|
112 (insert-file-contents-internal infile nil nil nil nil
|
|
113 'binary))))
|
|
114 (let ((stderr (if (consp buffer) (second buffer) t)))
|
|
115 (if (consp buffer) (setq buffer (car buffer)))
|
|
116 (setq buffer
|
|
117 (cond ((null buffer) nil)
|
|
118 ((eq buffer t) (current-buffer))
|
|
119 ;; use integerp for compatibility with existing
|
|
120 ;; call-process rmsism.
|
|
121 ((integerp buffer) (setq discard t) nil)
|
|
122 (t (get-buffer-create buffer))))
|
|
123 (when (and stderr (not (eq t stderr)))
|
|
124 (setq stderr (expand-file-name stderr))
|
|
125 (setq errbuf (generate-new-buffer "*call-process*")))
|
|
126 (setq proc
|
|
127 (apply 'start-process-internal "*call-process*"
|
|
128 buffer
|
|
129 ;#### not implemented until my new process
|
|
130 ;changes go in.
|
|
131 ;(if (eq t stderr) buffer (list buffer errbuf))
|
|
132 program args))
|
|
133 (if buffer
|
|
134 (set-marker (process-mark proc) (point buffer) buffer))
|
|
135 (unwind-protect
|
|
136 (prog1
|
|
137 (catch 'call-process-done
|
|
138 (when (not discard)
|
|
139 (set-process-sentinel
|
|
140 proc
|
|
141 #'(lambda (proc status)
|
|
142 (cond ((eq 'exit (process-status proc))
|
|
143 (set-process-sentinel proc nil)
|
|
144 (throw 'call-process-done
|
|
145 (process-exit-status proc)))
|
|
146 ((eq 'signal (process-status proc))
|
|
147 (set-process-sentinel proc nil)
|
|
148 (throw 'call-process-done status))))))
|
|
149 (when inbuf
|
|
150 (process-send-region proc 1
|
|
151 (1+ (buffer-size inbuf)) inbuf))
|
|
152 (process-send-eof proc)
|
|
153 (when discard
|
|
154 ;; we're trying really really hard to emulate
|
|
155 ;; the old call-process.
|
|
156 (if errbuf
|
|
157 (set-process-sentinel
|
|
158 proc
|
|
159 `(lambda (proc status)
|
|
160 (write-region-internal
|
|
161 1 (1+ (buffer-size))
|
|
162 ,stderr
|
|
163 nil 'major-rms-kludge-city nil
|
|
164 coding-system-for-write))))
|
|
165 (setq errbuf nil)
|
|
166 (setq proc nil)
|
|
167 (throw 'call-process-done nil))
|
|
168 (while t
|
|
169 (accept-process-output proc)
|
|
170 (if display (sit-for 0))))
|
|
171 (when errbuf
|
|
172 (with-current-buffer errbuf
|
|
173 (write-region-internal 1 (1+ (buffer-size)) stderr
|
|
174 nil 'major-rms-kludge-city nil
|
|
175 coding-system-for-write))))
|
|
176 (if proc (set-process-sentinel proc nil)))))
|
|
177 (if inbuf (kill-buffer inbuf))
|
|
178 (if errbuf (kill-buffer errbuf))
|
|
179 (condition-case nil
|
|
180 (if (and proc (process-live-p proc)) (kill-process proc))
|
|
181 (error nil))))))
|
|
182
|
428
|
183 (defun call-process (program &optional infile buffer displayp &rest args)
|
|
184 "Call PROGRAM synchronously in separate process.
|
|
185 The program's input comes from file INFILE (nil means `/dev/null').
|
|
186 Insert output in BUFFER before point; t means current buffer;
|
|
187 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
188 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
189 REAL-BUFFER says what to do with standard output, as above,
|
|
190 while STDERR-FILE says what to do with standard error in the child.
|
|
191 STDERR-FILE may be nil (discard standard error output),
|
|
192 t (mix it with ordinary output), or a file name string.
|
|
193
|
|
194 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
195 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
196
|
|
197 If BUFFER is 0, `call-process' returns immediately with value nil.
|
|
198 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
|
|
199 or a signal description string.
|
|
200 If you quit, the process is killed with SIGINT, or SIGKILL if you
|
|
201 quit again."
|
|
202 (apply 'call-process-internal program infile buffer displayp args))
|
|
203
|
|
204 (defun call-process-region (start end program
|
|
205 &optional deletep buffer displayp
|
|
206 &rest args)
|
|
207 "Send text from START to END to a synchronous process running PROGRAM.
|
|
208 Delete the text if fourth arg DELETEP is non-nil.
|
|
209
|
|
210 Insert output in BUFFER before point; t means current buffer;
|
|
211 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
212 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
213 REAL-BUFFER says what to do with standard output, as above,
|
|
214 while STDERR-FILE says what to do with standard error in the child.
|
|
215 STDERR-FILE may be nil (discard standard error output),
|
|
216 t (mix it with ordinary output), or a file name string.
|
|
217
|
|
218 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
219 Remaining args are passed to PROGRAM at startup as command args.
|
|
220
|
|
221 If BUFFER is 0, returns immediately with value nil.
|
|
222 Otherwise waits for PROGRAM to terminate
|
|
223 and returns a numeric exit status or a signal description string.
|
|
224 If you quit, the process is first killed with SIGINT, then with SIGKILL if
|
|
225 you quit again before the process exits."
|
|
226 (let ((temp
|
|
227 (make-temp-name
|
440
|
228 (concat (file-name-as-directory (temp-directory)) "emacs"))))
|
428
|
229 (unwind-protect
|
|
230 (progn
|
440
|
231 (write-region start end temp nil 'silent)
|
428
|
232 (if deletep (delete-region start end))
|
|
233 (apply #'call-process program temp buffer displayp args))
|
|
234 (ignore-file-errors (delete-file temp)))))
|
|
235
|
|
236
|
|
237 (defun shell-command (command &optional output-buffer)
|
|
238 "Execute string COMMAND in inferior shell; display output, if any.
|
|
239
|
|
240 If COMMAND ends in ampersand, execute it asynchronously.
|
|
241 The output appears in the buffer `*Async Shell Command*'.
|
|
242 That buffer is in shell mode.
|
|
243
|
|
244 Otherwise, COMMAND is executed synchronously. The output appears in the
|
|
245 buffer `*Shell Command Output*'.
|
|
246 If the output is one line, it is displayed in the echo area *as well*,
|
|
247 but it is nonetheless available in buffer `*Shell Command Output*',
|
|
248 even though that buffer is not automatically displayed.
|
|
249 If there is no output, or if output is inserted in the current buffer,
|
|
250 then `*Shell Command Output*' is deleted.
|
|
251
|
|
252 The optional second argument OUTPUT-BUFFER, if non-nil,
|
|
253 says to put the output in some other buffer.
|
|
254 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
|
|
255 If OUTPUT-BUFFER is not a buffer and not nil,
|
|
256 insert output in current buffer. (This cannot be done asynchronously.)
|
|
257 In either case, the output is inserted after point (leaving mark after it)."
|
|
258 (interactive (list (read-shell-command "Shell command: ")
|
|
259 current-prefix-arg))
|
|
260 (if (and output-buffer
|
|
261 (not (or (bufferp output-buffer) (stringp output-buffer))))
|
|
262 (progn (barf-if-buffer-read-only)
|
444
|
263 (push-mark nil (not (interactive-p)))
|
428
|
264 ;; We do not use -f for csh; we will not support broken use of
|
|
265 ;; .cshrcs. Even the BSD csh manual says to use
|
|
266 ;; "if ($?prompt) exit" before things which are not useful
|
|
267 ;; non-interactively. Besides, if someone wants their other
|
|
268 ;; aliases for shell commands then they can still have them.
|
|
269 (call-process shell-file-name nil t nil
|
|
270 shell-command-switch command)
|
|
271 (exchange-point-and-mark t))
|
|
272 ;; Preserve the match data in case called from a program.
|
|
273 (save-match-data
|
|
274 (if (string-match "[ \t]*&[ \t]*$" command)
|
|
275 ;; Command ending with ampersand means asynchronous.
|
|
276 (progn
|
776
|
277 (if-fboundp 'background
|
|
278 (background (substring command 0
|
|
279 (match-beginning 0)))
|
|
280 (error
|
|
281 'unimplemented
|
|
282 "backgrounding a shell command requires package `background'")))
|
|
283
|
428
|
284 (shell-command-on-region (point) (point) command output-buffer)))))
|
|
285
|
|
286 ;; We have a sentinel to prevent insertion of a termination message
|
|
287 ;; in the buffer itself.
|
|
288 (defun shell-command-sentinel (process signal)
|
|
289 (if (memq (process-status process) '(exit signal))
|
|
290 (message "%s: %s."
|
|
291 (car (cdr (cdr (process-command process))))
|
|
292 (substring signal 0 -1))))
|
|
293
|
|
294 (defun shell-command-on-region (start end command
|
|
295 &optional output-buffer replace)
|
|
296 "Execute string COMMAND in inferior shell with region as input.
|
|
297 Normally display output (if any) in temp buffer `*Shell Command Output*';
|
|
298 Prefix arg means replace the region with it.
|
|
299
|
|
300 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
|
|
301 If REPLACE is non-nil, that means insert the output
|
|
302 in place of text from START to END, putting point and mark around it.
|
|
303
|
|
304 If the output is one line, it is displayed in the echo area,
|
|
305 but it is nonetheless available in buffer `*Shell Command Output*'
|
|
306 even though that buffer is not automatically displayed.
|
|
307 If there is no output, or if output is inserted in the current buffer,
|
|
308 then `*Shell Command Output*' is deleted.
|
|
309
|
|
310 If the optional fourth argument OUTPUT-BUFFER is non-nil,
|
|
311 that says to put the output in some other buffer.
|
|
312 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
|
|
313 If OUTPUT-BUFFER is not a buffer and not nil,
|
|
314 insert output in the current buffer.
|
|
315 In either case, the output is inserted after point (leaving mark after it)."
|
|
316 (interactive (let ((string
|
|
317 ;; Do this before calling region-beginning
|
|
318 ;; and region-end, in case subprocess output
|
|
319 ;; relocates them while we are in the minibuffer.
|
|
320 (read-shell-command "Shell command on region: ")))
|
|
321 ;; call-interactively recognizes region-beginning and
|
|
322 ;; region-end specially, leaving them in the history.
|
|
323 (list (region-beginning) (region-end)
|
|
324 string
|
|
325 current-prefix-arg
|
|
326 current-prefix-arg)))
|
|
327 (if (or replace
|
|
328 (and output-buffer
|
|
329 (not (or (bufferp output-buffer) (stringp output-buffer)))))
|
|
330 ;; Replace specified region with output from command.
|
|
331 (let ((swap (and replace (< start end))))
|
|
332 ;; Don't muck with mark unless REPLACE says we should.
|
|
333 (goto-char start)
|
|
334 (and replace (push-mark))
|
|
335 (call-process-region start end shell-file-name t t nil
|
|
336 shell-command-switch command)
|
|
337 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
|
|
338 (and shell-buffer (not (eq shell-buffer (current-buffer)))
|
|
339 (kill-buffer shell-buffer)))
|
|
340 ;; Don't muck with mark unless REPLACE says we should.
|
|
341 (and replace swap (exchange-point-and-mark t)))
|
|
342 ;; No prefix argument: put the output in a temp buffer,
|
|
343 ;; replacing its entire contents.
|
|
344 (let ((buffer (get-buffer-create
|
|
345 (or output-buffer "*Shell Command Output*")))
|
|
346 (success nil)
|
|
347 (exit-status nil)
|
|
348 (directory default-directory))
|
|
349 (unwind-protect
|
|
350 (if (eq buffer (current-buffer))
|
|
351 ;; If the input is the same buffer as the output,
|
|
352 ;; delete everything but the specified region,
|
|
353 ;; then replace that region with the output.
|
|
354 (progn (setq buffer-read-only nil)
|
|
355 (delete-region (max start end) (point-max))
|
|
356 (delete-region (point-min) (max start end))
|
|
357 (setq exit-status
|
|
358 (call-process-region (point-min) (point-max)
|
|
359 shell-file-name t t nil
|
|
360 shell-command-switch command))
|
|
361 (setq success t))
|
|
362 ;; Clear the output buffer,
|
|
363 ;; then run the command with output there.
|
|
364 (save-excursion
|
|
365 (set-buffer buffer)
|
|
366 (setq buffer-read-only nil)
|
|
367 ;; XEmacs change
|
|
368 (setq default-directory directory)
|
|
369 (erase-buffer))
|
|
370 (setq exit-status
|
|
371 (call-process-region start end shell-file-name
|
|
372 nil buffer nil
|
|
373 shell-command-switch command))
|
|
374 (setq success t))
|
|
375 ;; Report the amount of output.
|
|
376 (let ((lines (save-excursion
|
|
377 (set-buffer buffer)
|
|
378 (if (= (buffer-size) 0)
|
|
379 0
|
|
380 (count-lines (point-min) (point-max))))))
|
|
381 (cond ((= lines 0)
|
|
382 (if success
|
|
383 (display-message
|
|
384 'command
|
|
385 (if (eql exit-status 0)
|
|
386 "(Shell command succeeded with no output)"
|
|
387 "(Shell command failed with no output)")))
|
|
388 (kill-buffer buffer))
|
|
389 ((and success (= lines 1))
|
|
390 (message "%s"
|
|
391 (save-excursion
|
|
392 (set-buffer buffer)
|
|
393 (goto-char (point-min))
|
|
394 (buffer-substring (point)
|
|
395 (progn (end-of-line)
|
|
396 (point))))))
|
|
397 (t
|
|
398 (set-window-start (display-buffer buffer) 1))))))))
|
|
399
|
|
400
|
|
401 (defun start-process (name buffer program &rest program-args)
|
|
402 "Start a program in a subprocess. Return the process object for it.
|
|
403 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
|
|
404 NAME is name for process. It is modified if necessary to make it unique.
|
|
405 BUFFER is the buffer or (buffer-name) to associate with the process.
|
|
406 Process output goes at end of that buffer, unless you specify
|
|
407 an output stream or filter function to handle the output.
|
|
408 BUFFER may be also nil, meaning that this process is not associated
|
|
409 with any buffer
|
|
410 Third arg is program file name. It is searched for as in the shell.
|
|
411 Remaining arguments are strings to give program as arguments."
|
|
412 (apply 'start-process-internal name buffer program program-args))
|
|
413
|
|
414 (defun open-network-stream (name buffer host service &optional protocol)
|
|
415 "Open a TCP connection for a service to a host.
|
444
|
416 Returns a process object to represent the connection.
|
428
|
417 Input and output work as for subprocesses; `delete-process' closes it.
|
|
418 Args are NAME BUFFER HOST SERVICE.
|
|
419 NAME is name for process. It is modified if necessary to make it unique.
|
|
420 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
421 Process output goes at end of that buffer, unless you specify
|
|
422 an output stream or filter function to handle the output.
|
|
423 BUFFER may be also nil, meaning that this process is not associated
|
|
424 with any buffer
|
|
425 Third arg is name of the host to connect to, or its IP address.
|
|
426 Fourth arg SERVICE is name of the service desired, or an integer
|
|
427 specifying a port number to connect to.
|
|
428 Fifth argument PROTOCOL is a network protocol. Currently 'tcp
|
|
429 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
|
|
430 supported. When omitted, 'tcp is assumed.
|
|
431
|
442
|
432 Output via `process-send-string' and input via buffer or filter (see
|
428
|
433 `set-process-filter') are stream-oriented. That means UDP datagrams are
|
|
434 not guaranteed to be sent and received in discrete packets. (But small
|
|
435 datagrams around 500 bytes that are not truncated by `process-send-string'
|
438
|
436 are usually fine.) Note further that UDP protocol does not guard against
|
428
|
437 lost packets."
|
|
438 (open-network-stream-internal name buffer host service protocol))
|
|
439
|
|
440 (defun shell-quote-argument (argument)
|
|
441 "Quote an argument for passing as argument to an inferior shell."
|
442
|
442 (if (and (eq system-type 'windows-nt)
|
|
443 (let ((progname (downcase (file-name-nondirectory
|
|
444 shell-file-name))))
|
|
445 (or (equal progname "command.com")
|
|
446 (equal progname "cmd.exe"))))
|
|
447 ;; the expectation is that you can take the result of
|
|
448 ;; shell-quote-argument and pass it to as an arg to
|
|
449 ;; (start-process shell-quote-argument ...) and have it end
|
|
450 ;; up as-is in the program's argv[] array. to do this, we
|
|
451 ;; need to protect against both the shell's and the program's
|
|
452 ;; quoting conventions (and our own conventions in
|
|
453 ;; mswindows-construct-process-command-line!). Putting quotes
|
|
454 ;; around shell metachars gets through the last two, and applying
|
|
455 ;; the normal VC runtime quoting works with practically all apps.
|
776
|
456 (declare-fboundp (mswindows-quote-one-vc-runtime-arg argument t))
|
611
|
457 (if (equal argument "")
|
|
458 "\"\""
|
|
459 ;; Quote everything except POSIX filename characters.
|
|
460 ;; This should be safe enough even for really weird shells.
|
|
461 (let ((result "") (start 0) end)
|
|
462 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
|
|
463 (setq end (match-beginning 0)
|
|
464 result (concat result (substring argument start end)
|
|
465 "\\" (substring argument end (1+ end)))
|
|
466 start (1+ end)))
|
|
467 (concat result (substring argument start))))))
|
428
|
468
|
438
|
469 (defun shell-command-to-string (command)
|
|
470 "Execute shell command COMMAND and return its output as a string."
|
428
|
471 (with-output-to-string
|
|
472 (call-process shell-file-name nil t nil shell-command-switch command)))
|
|
473
|
438
|
474 (defalias 'exec-to-string 'shell-command-to-string)
|
771
|
475
|
|
476
|
|
477 ;; History list for environment variable names.
|
|
478 (defvar read-envvar-name-history nil)
|
|
479
|
|
480 (defun read-envvar-name (prompt &optional mustmatch)
|
|
481 "Read environment variable name, prompting with PROMPT.
|
|
482 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
|
|
483 If it is also not t, RET does not exit if it does non-null completion."
|
|
484 (completing-read prompt
|
|
485 (mapcar (function
|
|
486 (lambda (enventry)
|
|
487 (list (substring enventry 0
|
|
488 (string-match "=" enventry)))))
|
|
489 process-environment)
|
|
490 nil mustmatch nil 'read-envvar-name-history))
|
|
491
|
|
492 ;; History list for VALUE argument to setenv.
|
|
493 (defvar setenv-history nil)
|
|
494
|
|
495 (defun setenv (variable &optional value unset)
|
|
496 "Set the value of the environment variable named VARIABLE to VALUE.
|
|
497 VARIABLE should be a string. VALUE is optional; if not provided or is
|
|
498 `nil', the environment variable VARIABLE will be removed.
|
|
499
|
|
500 Interactively, a prefix argument means to unset the variable.
|
|
501 Interactively, the current value (if any) of the variable
|
|
502 appears at the front of the history list when you type in the new value.
|
|
503
|
|
504 This function works by modifying `process-environment'."
|
|
505 (interactive
|
|
506 (if current-prefix-arg
|
|
507 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
|
|
508 (let ((var (read-envvar-name "Set environment variable: " nil)))
|
|
509 ;; Here finally we specify the args to call setenv with.
|
|
510 (list var (read-from-minibuffer (format "Set %s to value: " var)
|
|
511 nil nil nil 'setenv-history
|
|
512 (getenv var))))))
|
|
513 (if unset (setq value nil))
|
|
514 (if (string-match "=" variable)
|
|
515 (error "Environment variable name `%s' contains `='" variable)
|
|
516 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
|
|
517 (case-fold-search nil)
|
|
518 (scan process-environment)
|
|
519 found)
|
|
520 (if (string-equal "TZ" variable)
|
|
521 (set-time-zone-rule value))
|
|
522 (while scan
|
|
523 (cond ((string-match pattern (car scan))
|
|
524 (setq found t)
|
|
525 (if (eq nil value)
|
|
526 (setq process-environment (delq (car scan) process-environment))
|
|
527 (setcar scan (concat variable "=" value)))
|
|
528 (setq scan nil)))
|
|
529 (setq scan (cdr scan)))
|
|
530 (or found
|
|
531 (if value
|
|
532 (setq process-environment
|
|
533 (cons (concat variable "=" value)
|
|
534 process-environment)))))))
|
|
535
|
|
536 ;; already in C. Can't move it to Lisp too easily because it's needed
|
|
537 ;; extremely early in the Lisp loadup sequence.
|
|
538
|
|
539 ; (defun getenv (variable)
|
|
540 ; "Get the value of environment variable VARIABLE.
|
|
541 ; VARIABLE should be a string. Value is nil if VARIABLE is undefined in
|
|
542 ; the environment. Otherwise, value is a string.
|
|
543 ;
|
|
544 ; This function consults the variable `process-environment'
|
|
545 ; for its value."
|
|
546 ; (interactive (list (read-envvar-name "Get environment variable: " t)))
|
|
547 ; (let ((value (getenv-internal variable)))
|
|
548 ; (when (interactive-p)
|
|
549 ; (message "%s" (if value value "Not set")))
|
|
550 ; value))
|
|
551
|
|
552 (provide 'env) ;; Yuck. Formerly the above were in env.el, which did this
|
|
553 ;; provide.
|
428
|
554
|
|
555 ;;; process.el ends here
|