annotate lisp/process.el @ 786:381a409ad3c3

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