annotate lisp/process.el @ 406:b8cc9ab3f761 r21-2-33

Import from CVS: tag r21-2-33
author cvs
date Mon, 13 Aug 2007 11:17:09 +0200
parents 74fd4e045ea6
children 501cfd01ee6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1 ;;; process.el --- commands for subprocesses; split out of simple.el
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
2
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc.
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
4 ;; Copyright (C) 1995, 2000 Ben Wing.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
5
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
6 ;; Author: Ben Wing
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
8 ;; Keywords: internal, processes, dumped
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
9
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
11
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
12 ;; XEmacs is free software; you can redistribute it and/or modify it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
15 ;; any later version.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
16
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
20 ;; General Public License for more details.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
21
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
23 ;; along with XEmacs; see the file COPYING. If not, write to the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
24 ;; Free Software Foundation, 59 Temple Place - Suite 330,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
26
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
27 ;;; Synched up with: FSF 19.30.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
28
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
29 ;;; Authorship:
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
30
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
31 ;; Created 1995 by Ben Wing during Mule work -- some commands split out
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
32 ;; of simple.el and wrappers of *-internal functions created so they could
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
33 ;; be redefined in a Mule world.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
34 ;; Lisp definition of call-process-internal added Mar. 2000 by Ben Wing.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
35
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
36 ;;; Commentary:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
37
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
38 ;; This file is dumped with XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
39
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
40 ;;; Code:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
41
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
42
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
43 (defgroup processes nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
44 "Process, subshell, compilation, and job control support."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
45 :group 'external
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
46 :group 'development)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
47
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
48 (defgroup processes-basics nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
49 "Basic stuff dealing with processes."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
50 :group 'processes)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
51
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
52 (defgroup execute nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
53 "Executing external commands."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
54 :group 'processes)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
55
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
56
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
57 (defvar shell-command-switch "-c"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
58 "Switch used to have the shell execute its command line argument.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
59
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
60 (defun start-process-shell-command (name buffer &rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
61 "Start a program in a subprocess. Return the process object for it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
62 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
63 NAME is name for process. It is modified if necessary to make it unique.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
64 BUFFER is the buffer or (buffer-name) to associate with the process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
65 Process output goes at end of that buffer, unless you specify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
66 an output stream or filter function to handle the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
67 BUFFER may be also nil, meaning that this process is not associated
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
68 with any buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
69 Third arg is command name, the name of a shell command.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
70 Remaining arguments are the arguments for the command.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
71 Wildcards and redirection are handled as usual in the shell."
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
72 ;; We used to use `exec' to replace the shell with the command,
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
73 ;; but that failed to handle (...) and semicolon, etc.
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
74 (start-process name buffer shell-file-name shell-command-switch
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
75 (mapconcat #'identity args " ")))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
76
406
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
77 (defun call-process-internal (program &optional infile buffer display &rest args)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
78 "Call PROGRAM synchronously in separate process, with coding-system specified.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
79 Arguments are
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
80 (PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS).
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
81 The program's input comes from file INFILE (nil means `/dev/null').
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
82 Insert output in BUFFER before point; t means current buffer;
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
83 nil for BUFFER means discard it; 0 means discard and don't wait.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
84 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
85 REAL-BUFFER says what to do with standard output, as above,
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
86 while STDERR-FILE says what to do with standard error in the child.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
87 STDERR-FILE may be nil (discard standard error output),
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
88 t (mix it with ordinary output), or a file name string.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
89
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
90 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
91 Remaining arguments are strings passed as command arguments to PROGRAM.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
92
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
93 If BUFFER is 0, `call-process' returns immediately with value nil.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
94 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
95 or a signal description string.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
96 If you quit, the process is killed with SIGINT, or SIGKILL if you
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
97 quit again."
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
98 ;; #### remove windows-nt check when this is ready for prime time.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
99 (if (or (noninteractive) (not (eq 'windows-nt system-type)))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
100 (apply 'old-call-process-internal program infile buffer display args)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
101 (let (proc inbuf errbuf discard)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
102 (unwind-protect
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
103 (progn
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
104 (when infile
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
105 (setq infile (expand-file-name infile))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
106 (setq inbuf (generate-new-buffer "*call-process*"))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
107 (with-current-buffer inbuf
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
108 (insert-file-contents-internal infile nil nil nil nil
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
109 coding-system-for-read)))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
110 (let ((stderr (if (consp buffer) (second buffer) t)))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
111 (if (consp buffer) (setq buffer (car buffer)))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
112 (setq buffer
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
113 (cond ((null buffer) nil)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
114 ((eq buffer t) (current-buffer))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
115 ;; use integerp for compatibility with existing
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
116 ;; call-process rmsism.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
117 ((integerp buffer) (setq discard t) nil)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
118 (t (get-buffer-create buffer))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
119 (when (and stderr (not (eq t stderr)))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
120 (setq stderr (expand-file-name stderr))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
121 (setq errbuf (generate-new-buffer "*call-process*")))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
122 (setq proc
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
123 (apply 'start-process-internal "*call-process*"
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
124 buffer
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
125 ;#### not implemented until my new process
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
126 ;changes go in.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
127 ;(if (eq t stderr) buffer (list buffer errbuf))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
128 program args))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
129 (if buffer
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
130 (set-marker (process-mark proc) (point buffer) buffer))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
131 (unwind-protect
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
132 (progn
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
133 (catch 'call-process-done
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
134 (when (not discard)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
135 (set-process-sentinel
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
136 proc
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
137 #'(lambda (proc status)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
138 (cond ((eq 'exit (process-status proc))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
139 (set-process-sentinel proc nil)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
140 (throw 'call-process-done
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
141 (process-exit-status proc)))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
142 ((eq 'signal (process-status proc))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
143 (set-process-sentinel proc nil)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
144 (throw 'call-process-done status))))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
145 (when inbuf
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
146 (process-send-region proc 1
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
147 (1+ (buffer-size inbuf)) inbuf))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
148 (process-send-eof proc)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
149 (when discard
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
150 ;; we're trying really really hard to emulate
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
151 ;; the old call-process.
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
152 (if errbuf
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
153 (set-process-sentinel
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
154 proc
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
155 `(lambda (proc status)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
156 (write-region-internal
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
157 1 (1+ (buffer-size))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
158 ,stderr
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
159 nil 'major-rms-kludge-city nil
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
160 coding-system-for-write))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
161 (setq errbuf nil)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
162 (setq proc nil)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
163 (throw 'call-process-done nil))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
164 (while t
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
165 (accept-process-output proc)
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
166 (if display (sit-for 0))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
167 (when errbuf
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
168 (with-current-buffer errbuf
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
169 (write-region-internal 1 (1+ (buffer-size)) stderr
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
170 nil 'major-rms-kludge-city nil
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
171 coding-system-for-write))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
172 (if proc (set-process-sentinel proc nil)))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
173 (if inbuf (kill-buffer inbuf))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
174 (if errbuf (kill-buffer errbuf))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
175 (condition-case nil
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
176 (if (and proc (process-live-p proc)) (kill-process proc))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
177 (error nil))))))
b8cc9ab3f761 Import from CVS: tag r21-2-33
cvs
parents: 398
diff changeset
178
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
179 (defun call-process (program &optional infile buffer displayp &rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
180 "Call PROGRAM synchronously in separate process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
181 The program's input comes from file INFILE (nil means `/dev/null').
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
182 Insert output in BUFFER before point; t means current buffer;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
183 nil for BUFFER means discard it; 0 means discard and don't wait.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
184 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
185 REAL-BUFFER says what to do with standard output, as above,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
186 while STDERR-FILE says what to do with standard error in the child.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
187 STDERR-FILE may be nil (discard standard error output),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
188 t (mix it with ordinary output), or a file name string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
189
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
190 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
191 Remaining arguments are strings passed as command arguments to PROGRAM.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
192
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
193 If BUFFER is 0, `call-process' returns immediately with value nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
194 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
195 or a signal description string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
196 If you quit, the process is killed with SIGINT, or SIGKILL if you
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
197 quit again."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
198 (apply 'call-process-internal program infile buffer displayp args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
199
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
200 (defun call-process-region (start end program
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
201 &optional deletep buffer displayp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
202 &rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
203 "Send text from START to END to a synchronous process running PROGRAM.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
204 Delete the text if fourth arg DELETEP is non-nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
205
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
206 Insert output in BUFFER before point; t means current buffer;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
207 nil for BUFFER means discard it; 0 means discard and don't wait.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
208 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
209 REAL-BUFFER says what to do with standard output, as above,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
210 while STDERR-FILE says what to do with standard error in the child.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
211 STDERR-FILE may be nil (discard standard error output),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
212 t (mix it with ordinary output), or a file name string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
213
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
214 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
215 Remaining args are passed to PROGRAM at startup as command args.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
216
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
217 If BUFFER is 0, returns immediately with value nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
218 Otherwise waits for PROGRAM to terminate
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
219 and returns a numeric exit status or a signal description string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
220 If you quit, the process is first killed with SIGINT, then with SIGKILL if
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
221 you quit again before the process exits."
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
222 (let ((temp
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
223 (make-temp-name
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
224 (concat (file-name-as-directory (temp-directory)) "emacs"))))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
225 (unwind-protect
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
226 (progn
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
227 (write-region start end temp nil 'silent)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
228 (if deletep (delete-region start end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
229 (apply #'call-process program temp buffer displayp args))
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
230 (ignore-file-errors (delete-file temp)))))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
231
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
232
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
233 (defun shell-command (command &optional output-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
234 "Execute string COMMAND in inferior shell; display output, if any.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
235
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
236 If COMMAND ends in ampersand, execute it asynchronously.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
237 The output appears in the buffer `*Async Shell Command*'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
238 That buffer is in shell mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
239
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
240 Otherwise, COMMAND is executed synchronously. The output appears in the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
241 buffer `*Shell Command Output*'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
242 If the output is one line, it is displayed in the echo area *as well*,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
243 but it is nonetheless available in buffer `*Shell Command Output*',
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
244 even though that buffer is not automatically displayed.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
245 If there is no output, or if output is inserted in the current buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
246 then `*Shell Command Output*' is deleted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
247
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
248 The optional second argument OUTPUT-BUFFER, if non-nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
249 says to put the output in some other buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
250 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
251 If OUTPUT-BUFFER is not a buffer and not nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
252 insert output in current buffer. (This cannot be done asynchronously.)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
253 In either case, the output is inserted after point (leaving mark after it)."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
254 (interactive (list (read-shell-command "Shell command: ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
255 current-prefix-arg))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
256 (if (and output-buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
257 (not (or (bufferp output-buffer) (stringp output-buffer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
258 (progn (barf-if-buffer-read-only)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
259 (push-mark)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
260 ;; We do not use -f for csh; we will not support broken use of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
261 ;; .cshrcs. Even the BSD csh manual says to use
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
262 ;; "if ($?prompt) exit" before things which are not useful
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
263 ;; non-interactively. Besides, if someone wants their other
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
264 ;; aliases for shell commands then they can still have them.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
265 (call-process shell-file-name nil t nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
266 shell-command-switch command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
267 (exchange-point-and-mark t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
268 ;; Preserve the match data in case called from a program.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
269 (save-match-data
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
270 (if (string-match "[ \t]*&[ \t]*$" command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
271 ;; Command ending with ampersand means asynchronous.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
272 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
273 (background (substring command 0 (match-beginning 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
274 (shell-command-on-region (point) (point) command output-buffer)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
275
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
276 ;; We have a sentinel to prevent insertion of a termination message
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
277 ;; in the buffer itself.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
278 (defun shell-command-sentinel (process signal)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
279 (if (memq (process-status process) '(exit signal))
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
280 (message "%s: %s."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
281 (car (cdr (cdr (process-command process))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
282 (substring signal 0 -1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
283
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
284 (defun shell-command-on-region (start end command
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
285 &optional output-buffer replace)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
286 "Execute string COMMAND in inferior shell with region as input.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
287 Normally display output (if any) in temp buffer `*Shell Command Output*';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
288 Prefix arg means replace the region with it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
289
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
290 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
291 If REPLACE is non-nil, that means insert the output
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
292 in place of text from START to END, putting point and mark around it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
293
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
294 If the output is one line, it is displayed in the echo area,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
295 but it is nonetheless available in buffer `*Shell Command Output*'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
296 even though that buffer is not automatically displayed.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
297 If there is no output, or if output is inserted in the current buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
298 then `*Shell Command Output*' is deleted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
299
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
300 If the optional fourth argument OUTPUT-BUFFER is non-nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
301 that says to put the output in some other buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
302 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
303 If OUTPUT-BUFFER is not a buffer and not nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
304 insert output in the current buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
305 In either case, the output is inserted after point (leaving mark after it)."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
306 (interactive (let ((string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
307 ;; Do this before calling region-beginning
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
308 ;; and region-end, in case subprocess output
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
309 ;; relocates them while we are in the minibuffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
310 (read-shell-command "Shell command on region: ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
311 ;; call-interactively recognizes region-beginning and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
312 ;; region-end specially, leaving them in the history.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
313 (list (region-beginning) (region-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
314 string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
315 current-prefix-arg
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
316 current-prefix-arg)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
317 (if (or replace
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
318 (and output-buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
319 (not (or (bufferp output-buffer) (stringp output-buffer)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
320 ;; Replace specified region with output from command.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
321 (let ((swap (and replace (< start end))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
322 ;; Don't muck with mark unless REPLACE says we should.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
323 (goto-char start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
324 (and replace (push-mark))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
325 (call-process-region start end shell-file-name t t nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
326 shell-command-switch command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
327 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
328 (and shell-buffer (not (eq shell-buffer (current-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
329 (kill-buffer shell-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
330 ;; Don't muck with mark unless REPLACE says we should.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
331 (and replace swap (exchange-point-and-mark t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
332 ;; No prefix argument: put the output in a temp buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
333 ;; replacing its entire contents.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
334 (let ((buffer (get-buffer-create
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
335 (or output-buffer "*Shell Command Output*")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
336 (success nil)
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
337 (exit-status nil)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
338 (directory default-directory))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
339 (unwind-protect
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
340 (if (eq buffer (current-buffer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
341 ;; If the input is the same buffer as the output,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
342 ;; delete everything but the specified region,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
343 ;; then replace that region with the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
344 (progn (setq buffer-read-only nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
345 (delete-region (max start end) (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
346 (delete-region (point-min) (max start end))
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
347 (setq exit-status
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
348 (call-process-region (point-min) (point-max)
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
349 shell-file-name t t nil
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
350 shell-command-switch command))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
351 (setq success t))
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
352 ;; Clear the output buffer,
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
353 ;; then run the command with output there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
354 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
355 (set-buffer buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
356 (setq buffer-read-only nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
357 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
358 (setq default-directory directory)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
359 (erase-buffer))
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
360 (setq exit-status
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
361 (call-process-region start end shell-file-name
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
362 nil buffer nil
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
363 shell-command-switch command))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
364 (setq success t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
365 ;; Report the amount of output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
366 (let ((lines (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
367 (set-buffer buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
368 (if (= (buffer-size) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
369 0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
370 (count-lines (point-min) (point-max))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
371 (cond ((= lines 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
372 (if success
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
373 (display-message
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
374 'command
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
375 (if (eql exit-status 0)
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
376 "(Shell command succeeded with no output)"
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
377 "(Shell command failed with no output)")))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
378 (kill-buffer buffer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
379 ((and success (= lines 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
380 (message "%s"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
381 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
382 (set-buffer buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
383 (goto-char (point-min))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
384 (buffer-substring (point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
385 (progn (end-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
386 (point))))))
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
387 (t
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
388 (set-window-start (display-buffer buffer) 1))))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
389
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
390
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
391 (defun start-process (name buffer program &rest program-args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
392 "Start a program in a subprocess. Return the process object for it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
393 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
394 NAME is name for process. It is modified if necessary to make it unique.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
395 BUFFER is the buffer or (buffer-name) to associate with the process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
396 Process output goes at end of that buffer, unless you specify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
397 an output stream or filter function to handle the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
398 BUFFER may be also nil, meaning that this process is not associated
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
399 with any buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
400 Third arg is program file name. It is searched for as in the shell.
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 263
diff changeset
401 Remaining arguments are strings to give program as arguments."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
402 (apply 'start-process-internal name buffer program program-args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
403
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
404 (defun open-network-stream (name buffer host service &optional protocol)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
405 "Open a TCP connection for a service to a host.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
406 Returns a subprocess-object to represent the connection.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
407 Input and output work as for subprocesses; `delete-process' closes it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
408 Args are NAME BUFFER HOST SERVICE.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
409 NAME is name for process. It is modified if necessary to make it unique.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
410 BUFFER is the buffer (or buffer-name) to associate with the process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
411 Process output goes at end of that buffer, unless you specify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
412 an output stream or filter function to handle the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
413 BUFFER may be also nil, meaning that this process is not associated
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
414 with any buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
415 Third arg is name of the host to connect to, or its IP address.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
416 Fourth arg SERVICE is name of the service desired, or an integer
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
417 specifying a port number to connect to.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
418 Fifth argument PROTOCOL is a network protocol. Currently 'tcp
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
419 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
420 supported. When omitted, 'tcp is assumed.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
421
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
422 Ouput via `process-send-string' and input via buffer or filter (see
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
423 `set-process-filter') are stream-oriented. That means UDP datagrams are
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
424 not guaranteed to be sent and received in discrete packets. (But small
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
425 datagrams around 500 bytes that are not truncated by `process-send-string'
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
426 are usually fine.) Note further that UDP protocol does not guard against
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
427 lost packets."
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
428 (open-network-stream-internal name buffer host service protocol))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
429
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
430 (defun shell-quote-argument (argument)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
431 "Quote an argument for passing as argument to an inferior shell."
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
432 (if (eq system-type 'windows-nt)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
433 (nt-quote-process-args (list shell-file-name argument))
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
434 ;; Quote everything except POSIX filename characters.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
435 ;; This should be safe enough even for really weird shells.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
436 (let ((result "") (start 0) end)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
437 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
438 (setq end (match-beginning 0)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
439 result (concat result (substring argument start end)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
440 "\\" (substring argument end (1+ end)))
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
441 start (1+ end)))
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
442 (concat result (substring argument start)))))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
443
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
444 (defun shell-command-to-string (command)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
445 "Execute shell command COMMAND and return its output as a string."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
446 (with-output-to-string
392
1f50e6fe4f3f Import from CVS: tag r21-2-11
cvs
parents: 380
diff changeset
447 (call-process shell-file-name nil t nil shell-command-switch command)))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
448
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 392
diff changeset
449 (defalias 'exec-to-string 'shell-command-to-string)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
450
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
451 ;;; process.el ends here