0
|
1 ;;; process.el --- commands for subprocesses; split out of simple.el
|
|
2
|
|
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Ben Wing.
|
|
5
|
|
6 ;; This file is part of XEmacs.
|
|
7
|
|
8 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
9 ;; under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
11 ;; any later version.
|
|
12
|
|
13 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 ;; General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
16
|
19 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 ;; Boston, MA 02111-1307, USA.
|
0
|
22
|
|
23 ;;; Synched up with: FSF 19.30.
|
|
24
|
|
25 ;;; Code:
|
|
26
|
|
27
|
|
28 (defvar shell-command-switch "-c"
|
|
29 "Switch used to have the shell execute its command line argument.")
|
|
30
|
|
31 (defun start-process-shell-command (name buffer &rest args)
|
|
32 "Start a program in a subprocess. Return the process object for it.
|
|
33 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
|
|
34 NAME is name for process. It is modified if necessary to make it unique.
|
|
35 BUFFER is the buffer or (buffer-name) to associate with the process.
|
|
36 Process output goes at end of that buffer, unless you specify
|
|
37 an output stream or filter function to handle the output.
|
|
38 BUFFER may be also nil, meaning that this process is not associated
|
|
39 with any buffer
|
|
40 Third arg is command name, the name of a shell command.
|
|
41 Remaining arguments are the arguments for the command.
|
|
42 Wildcards and redirection are handled as usual in the shell."
|
|
43 (cond
|
|
44 ((eq system-type 'vax-vms)
|
|
45 (apply 'start-process name buffer args))
|
|
46 ;; We used to use `exec' to replace the shell with the command,
|
|
47 ;; but that failed to handle (...) and semicolon, etc.
|
|
48 (t
|
|
49 (start-process name buffer shell-file-name shell-command-switch
|
|
50 (mapconcat 'identity args " ")))))
|
|
51
|
|
52 (defun call-process (program &optional infile buffer displayp &rest args)
|
|
53 "Call PROGRAM synchronously in separate process.
|
|
54 The program's input comes from file INFILE (nil means `/dev/null').
|
|
55 Insert output in BUFFER before point; t means current buffer;
|
|
56 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
57 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
58 REAL-BUFFER says what to do with standard output, as above,
|
|
59 while STDERR-FILE says what to do with standard error in the child.
|
|
60 STDERR-FILE may be nil (discard standard error output),
|
|
61 t (mix it with ordinary output), or a file name string.
|
|
62
|
|
63 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
64 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
65
|
|
66 If BUFFER is 0, `call-process' returns immediately with value nil.
|
|
67 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
|
|
68 or a signal description string.
|
|
69 If you quit, the process is killed with SIGINT, or SIGKILL if you
|
|
70 quit again."
|
|
71 (apply 'call-process-internal program infile buffer displayp args))
|
|
72
|
|
73 (defun call-process-region (start end program
|
|
74 &optional deletep buffer displayp
|
|
75 &rest args)
|
|
76 "Send text from START to END to a synchronous process running PROGRAM.
|
|
77 Delete the text if fourth arg DELETEP is non-nil.
|
|
78
|
|
79 Insert output in BUFFER before point; t means current buffer;
|
|
80 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
81 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
82 REAL-BUFFER says what to do with standard output, as above,
|
|
83 while STDERR-FILE says what to do with standard error in the child.
|
|
84 STDERR-FILE may be nil (discard standard error output),
|
|
85 t (mix it with ordinary output), or a file name string.
|
|
86
|
|
87 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
|
|
88 Remaining args are passed to PROGRAM at startup as command args.
|
|
89
|
|
90 If BUFFER is 0, returns immediately with value nil.
|
|
91 Otherwise waits for PROGRAM to terminate
|
|
92 and returns a numeric exit status or a signal description string.
|
|
93 If you quit, the process is first killed with SIGINT, then with SIGKILL if
|
|
94 you quit again before the process exits."
|
|
95 (let ((temp (cond ((eq system-type 'vax-vms)
|
|
96 (make-temp-name "tmp:emacs"))
|
|
97 ((or (eq system-type 'ms-dos)
|
|
98 (eq system-type 'windowsnt))
|
|
99 (make-temp-name
|
|
100 (concat (file-name-as-directory
|
|
101 (or (getenv "TMP")
|
|
102 (getenv "TEMP")
|
|
103 ""))
|
|
104 "em")))
|
|
105 (t
|
|
106 (make-temp-name "/tmp/emacs")))))
|
|
107 (unwind-protect
|
|
108 (progn
|
|
109 (if (or (eq system-type 'ms-dos)
|
|
110 (eq system-type 'windowsnt))
|
|
111 (let ((buffer-file-type binary-process-output))
|
|
112 (write-region start end temp nil 'silent))
|
|
113 (write-region start end temp nil 'silent))
|
|
114 (if deletep (delete-region start end))
|
|
115 (apply #'call-process program temp buffer displayp args))
|
|
116 (condition-case ()
|
|
117 (delete-file temp)
|
|
118 (file-error nil)))))
|
|
119
|
|
120
|
|
121 (defun shell-command (command &optional output-buffer)
|
|
122 "Execute string COMMAND in inferior shell; display output, if any.
|
|
123
|
|
124 If COMMAND ends in ampersand, execute it asynchronously.
|
|
125 The output appears in the buffer `*Async Shell Command*'.
|
|
126 That buffer is in shell mode.
|
|
127
|
|
128 Otherwise, COMMAND is executed synchronously. The output appears in the
|
|
129 buffer `*Shell Command Output*'.
|
|
130 If the output is one line, it is displayed in the echo area *as well*,
|
|
131 but it is nonetheless available in buffer `*Shell Command Output*',
|
|
132 even though that buffer is not automatically displayed.
|
|
133 If there is no output, or if output is inserted in the current buffer,
|
|
134 then `*Shell Command Output*' is deleted.
|
|
135
|
|
136 The optional second argument OUTPUT-BUFFER, if non-nil,
|
|
137 says to put the output in some other buffer.
|
|
138 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
|
|
139 If OUTPUT-BUFFER is not a buffer and not nil,
|
|
140 insert output in current buffer. (This cannot be done asynchronously.)
|
|
141 In either case, the output is inserted after point (leaving mark after it)."
|
|
142 (interactive (list (read-shell-command "Shell command: ")
|
|
143 current-prefix-arg))
|
|
144 (if (and output-buffer
|
|
145 (not (or (bufferp output-buffer) (stringp output-buffer))))
|
|
146 (progn (barf-if-buffer-read-only)
|
|
147 (push-mark)
|
|
148 ;; We do not use -f for csh; we will not support broken use of
|
|
149 ;; .cshrcs. Even the BSD csh manual says to use
|
|
150 ;; "if ($?prompt) exit" before things which are not useful
|
|
151 ;; non-interactively. Besides, if someone wants their other
|
|
152 ;; aliases for shell commands then they can still have them.
|
|
153 (call-process shell-file-name nil t nil
|
|
154 shell-command-switch command)
|
|
155 (exchange-point-and-mark t))
|
|
156 ;; Preserve the match data in case called from a program.
|
|
157 (save-match-data
|
|
158 (if (string-match "[ \t]*&[ \t]*$" command)
|
|
159 ;; Command ending with ampersand means asynchronous.
|
|
160 (progn
|
|
161 (require 'background) ; whizzy comint background code
|
|
162 (background (substring command 0 (match-beginning 0))))
|
|
163 (shell-command-on-region (point) (point) command nil)))))
|
|
164
|
|
165 ;; We have a sentinel to prevent insertion of a termination message
|
|
166 ;; in the buffer itself.
|
|
167 (defun shell-command-sentinel (process signal)
|
|
168 (if (memq (process-status process) '(exit signal))
|
|
169 (message "%s: %s."
|
|
170 (car (cdr (cdr (process-command process))))
|
|
171 (substring signal 0 -1))))
|
|
172
|
|
173 (defun shell-command-on-region (start end command
|
|
174 &optional output-buffer replace)
|
|
175 "Execute string COMMAND in inferior shell with region as input.
|
|
176 Normally display output (if any) in temp buffer `*Shell Command Output*';
|
|
177 Prefix arg means replace the region with it.
|
|
178
|
|
179 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
|
|
180 If REPLACE is non-nil, that means insert the output
|
|
181 in place of text from START to END, putting point and mark around it.
|
|
182
|
|
183 If the output is one line, it is displayed in the echo area,
|
|
184 but it is nonetheless available in buffer `*Shell Command Output*'
|
|
185 even though that buffer is not automatically displayed.
|
|
186 If there is no output, or if output is inserted in the current buffer,
|
|
187 then `*Shell Command Output*' is deleted.
|
|
188
|
|
189 If the optional fourth argument OUTPUT-BUFFER is non-nil,
|
|
190 that says to put the output in some other buffer.
|
|
191 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
|
|
192 If OUTPUT-BUFFER is not a buffer and not nil,
|
|
193 insert output in the current buffer.
|
|
194 In either case, the output is inserted after point (leaving mark after it)."
|
|
195 (interactive (if (not (region-exists-p))
|
|
196 (error "The region is not active now")
|
|
197 (let ((string
|
|
198 ;; Do this before calling region-beginning
|
|
199 ;; and region-end, in case subprocess output
|
|
200 ;; relocates them while we are in the minibuffer.
|
|
201 (read-shell-command "Shell command on region: ")))
|
|
202 ;; call-interactively recognizes region-beginning and
|
|
203 ;; region-end specially, leaving them in the history.
|
|
204 (list (region-beginning) (region-end)
|
|
205 string
|
|
206 current-prefix-arg
|
|
207 current-prefix-arg))))
|
|
208 (if (or replace
|
|
209 (and output-buffer
|
|
210 (not (or (bufferp output-buffer) (stringp output-buffer)))))
|
|
211 ;; Replace specified region with output from command.
|
|
212 (let ((swap (and replace (< start end))))
|
|
213 ;; Don't muck with mark unless REPLACE says we should.
|
|
214 (goto-char start)
|
|
215 (and replace (push-mark))
|
|
216 (call-process-region start end shell-file-name t t nil
|
|
217 shell-command-switch command)
|
|
218 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
|
|
219 (and shell-buffer (not (eq shell-buffer (current-buffer)))
|
|
220 (kill-buffer shell-buffer)))
|
|
221 ;; Don't muck with mark unless REPLACE says we should.
|
|
222 (and replace swap (exchange-point-and-mark t)))
|
|
223 ;; No prefix argument: put the output in a temp buffer,
|
|
224 ;; replacing its entire contents.
|
|
225 (let ((buffer (get-buffer-create
|
|
226 (or output-buffer "*Shell Command Output*")))
|
|
227 (success nil)
|
|
228 (directory default-directory))
|
|
229 (unwind-protect
|
|
230 (if (eq buffer (current-buffer))
|
|
231 ;; If the input is the same buffer as the output,
|
|
232 ;; delete everything but the specified region,
|
|
233 ;; then replace that region with the output.
|
|
234 (progn (setq buffer-read-only nil)
|
|
235 (delete-region (max start end) (point-max))
|
|
236 (delete-region (point-min) (max start end))
|
|
237 (call-process-region (point-min) (point-max)
|
|
238 shell-file-name t t nil
|
|
239 shell-command-switch command)
|
|
240 (setq success t))
|
|
241 ;; Clear the output buffer,
|
|
242 ;; then run the command with output there.
|
|
243 (save-excursion
|
|
244 (set-buffer buffer)
|
|
245 (setq buffer-read-only nil)
|
|
246 ;; XEmacs change
|
|
247 (setq default-directory directory)
|
|
248 (erase-buffer))
|
|
249 (call-process-region start end shell-file-name
|
|
250 nil buffer nil
|
|
251 shell-command-switch command)
|
|
252 (setq success t))
|
|
253 ;; Report the amount of output.
|
|
254 (let ((lines (save-excursion
|
|
255 (set-buffer buffer)
|
|
256 (if (= (buffer-size) 0)
|
|
257 0
|
|
258 (count-lines (point-min) (point-max))))))
|
|
259 (cond ((= lines 0)
|
|
260 (if success
|
|
261 (message
|
|
262 "(Shell command completed with no output)"))
|
|
263 (kill-buffer buffer))
|
|
264 ((and success (= lines 1))
|
|
265 (message "%s"
|
|
266 (save-excursion
|
|
267 (set-buffer buffer)
|
|
268 (goto-char (point-min))
|
|
269 (buffer-substring (point)
|
|
270 (progn (end-of-line)
|
|
271 (point))))))
|
|
272 (t
|
|
273 (set-window-start (display-buffer buffer) 1))))))))
|
|
274
|
|
275
|
|
276 (defun start-process (name buffer program &rest program-args)
|
|
277 "Start a program in a subprocess. Return the process object for it.
|
|
278 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
|
|
279 NAME is name for process. It is modified if necessary to make it unique.
|
|
280 BUFFER is the buffer or (buffer-name) to associate with the process.
|
|
281 Process output goes at end of that buffer, unless you specify
|
|
282 an output stream or filter function to handle the output.
|
|
283 BUFFER may be also nil, meaning that this process is not associated
|
|
284 with any buffer
|
|
285 Third arg is program file name. It is searched for as in the shell.
|
|
286 Remaining arguments are strings to give program as arguments.
|
|
287 INCODE and OUTCODE specify the coding-system objects used in input/output
|
|
288 from/to the process."
|
|
289 (apply 'start-process-internal name buffer program program-args))
|
|
290
|
|
291 (defun open-network-stream (name buffer host service)
|
|
292 "Open a TCP connection for a service to a host.
|
|
293 Returns a subprocess-object to represent the connection.
|
|
294 Input and output work as for subprocesses; `delete-process' closes it.
|
|
295 Args are NAME BUFFER HOST SERVICE.
|
|
296 NAME is name for process. It is modified if necessary to make it unique.
|
|
297 BUFFER is the buffer (or buffer-name) to associate with the process.
|
|
298 Process output goes at end of that buffer, unless you specify
|
|
299 an output stream or filter function to handle the output.
|
|
300 BUFFER may be also nil, meaning that this process is not associated
|
|
301 with any buffer
|
|
302 Third arg is name of the host to connect to, or its IP address.
|
|
303 Fourth arg SERVICE is name of the service desired, or an integer
|
|
304 specifying a port number to connect to."
|
|
305 (open-network-stream-internal name buffer host service))
|
|
306
|
|
307 (defun shell-quote-argument (argument)
|
|
308 "Quote an argument for passing as argument to an inferior shell."
|
|
309 (if (eq system-type 'ms-dos)
|
|
310 ;; MS-DOS shells don't have quoting, so don't do any.
|
|
311 argument
|
|
312 (if (eq system-type 'windows-nt)
|
|
313 (concat "\"" argument "\"")
|
|
314 ;; Quote everything except POSIX filename characters.
|
|
315 ;; This should be safe enough even for really weird shells.
|
|
316 (let ((result "") (start 0) end)
|
|
317 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
|
|
318 (setq end (match-beginning 0)
|
|
319 result (concat result (substring argument start end)
|
|
320 "\\" (substring argument end (1+ end)))
|
|
321 start (1+ end)))
|
|
322 (concat result (substring argument start))))))
|
|
323
|
|
324 (defun exec-to-string (command)
|
|
325 "Execute COMMAND as an external process and return the output of that
|
|
326 process as a string"
|
|
327 ;; by "William G. Dubuque" <wgd@zurich.ai.mit.edu>
|
|
328 (with-output-to-string
|
|
329 (call-process shell-file-name nil t nil "-c" command)))
|