annotate lisp/process.el @ 444:576fb035e263 r21-2-37

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