(xpm_lookup_color):
[bpt/emacs.git] / lisp / eshell / esh-proc.el
CommitLineData
affbf647
GM
1;;; esh-proc --- process management
2
faadfb0a 3;; Copyright (C) 1999, 2000 Free Software Foundation
affbf647
GM
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 2, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to the
19;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20;; Boston, MA 02111-1307, USA.
21
22(provide 'esh-proc)
23
24(eval-when-compile (require 'esh-maint))
25
26(defgroup eshell-proc nil
27 "When Eshell invokes external commands, it always does so
28asynchronously, so that Emacs isn't tied up waiting for the process to
29finish."
30 :tag "Process management"
31 :group 'eshell)
32
33;;; Commentary:
34
35;;; User Variables:
36
37(defcustom eshell-proc-load-hook '(eshell-proc-initialize)
38 "*A hook that gets run when `eshell-proc' is loaded."
39 :type 'hook
40 :group 'eshell-proc)
41
42(defcustom eshell-process-wait-seconds 0
43 "*The number of seconds to delay waiting for a synchronous process."
44 :type 'integer
45 :group 'eshell-proc)
46
47(defcustom eshell-process-wait-milliseconds 50
48 "*The number of milliseconds to delay waiting for a synchronous process."
49 :type 'integer
50 :group 'eshell-proc)
51
52(defcustom eshell-done-messages-in-minibuffer t
53 "*If non-nil, subjob \"Done\" messages will display in minibuffer."
54 :type 'boolean
55 :group 'eshell-proc)
56
57(defcustom eshell-delete-exited-processes t
58 "*If nil, process entries will stick around until `jobs' is run.
59This variable sets the buffer-local value of `delete-exited-processes'
60in Eshell buffers.
61
62This variable causes Eshell to mimic the behavior of bash when set to
63nil. It allows the user to view the exit status of a completed subjob
64\(process) at their leisure, because the process entry remains in
65memory until the user examines it using \\[list-processes].
66
67Otherwise, if `eshell-done-messages-in-minibuffer' is nil, and this
68variable is set to t, the only indication the user will have that a
69subjob is done is that it will no longer appear in the
70\\[list-processes\\] display.
71
72Note that Eshell will have to be restarted for a change in this
73variable's value to take effect."
74 :type 'boolean
75 :group 'eshell-proc)
76
77(defcustom eshell-reset-signals
78 "^\\(interrupt\\|killed\\|quit\\|stopped\\)"
79 "*If a termination signal matches this regexp, the terminal will be reset."
80 :type 'regexp
81 :group 'eshell-proc)
82
83(defcustom eshell-exec-hook nil
84 "*Called each time a process is exec'd by `eshell-gather-process-output'.
85It is passed one argument, which is the process that was just started.
86It is useful for things that must be done each time a process is
87executed in a eshell mode buffer (e.g., `process-kill-without-query').
88In contrast, `eshell-mode-hook' is only executed once when the buffer
89is created."
90 :type 'hook
91 :group 'eshell-proc)
92
93(defcustom eshell-kill-hook '(eshell-reset-after-proc)
94 "*Called when a process run by `eshell-gather-process-output' has ended.
95It is passed two arguments: the process that was just ended, and the
96termination status (as a string). Note that the first argument may be
97nil, in which case the user attempted to send a signal, but there was
98no relevant process. This can be used for displaying help
99information, for example."
100 :type 'hook
101 :group 'eshell-proc)
102
103;;; Internal Variables:
104
105(defvar eshell-current-subjob-p nil)
106
107(defvar eshell-process-list nil
108 "A list of the current status of subprocesses.")
109
110;;; Functions:
111
112(defun eshell-proc-initialize ()
113 "Initialize the process handling code."
114 (make-local-variable 'eshell-process-list)
115 (define-key eshell-command-map [(meta ?i)] 'eshell-insert-process)
116 (define-key eshell-command-map [(control ?c)] 'eshell-interrupt-process)
117 (define-key eshell-command-map [(control ?k)] 'eshell-kill-process)
118 (define-key eshell-command-map [(control ?d)] 'eshell-send-eof-to-process)
119 (define-key eshell-command-map [(control ?q)] 'eshell-continue-process)
120 (define-key eshell-command-map [(control ?s)] 'list-processes)
121 (define-key eshell-command-map [(control ?z)] 'eshell-stop-process)
122 (define-key eshell-command-map [(control ?\\)] 'eshell-quit-process))
123
124(defun eshell-reset-after-proc (proc status)
125 "Reset the command input location after a process terminates.
126The signals which will cause this to happen are matched by
127`eshell-reset-signals'."
ca7aae91
JW
128 (if (and (stringp status)
129 (string-match eshell-reset-signals status))
affbf647
GM
130 (eshell-reset)))
131
132(defun eshell-wait-for-process (&rest procs)
133 "Wait until PROC has successfully completed."
134 (while procs
135 (let ((proc (car procs)))
ca7aae91 136 (when (eshell-processp proc)
affbf647
GM
137 ;; NYI: If the process gets stopped here, that's bad.
138 (while (assq proc eshell-process-list)
139 (if (input-pending-p)
140 (discard-input))
141 (sit-for eshell-process-wait-seconds
142 eshell-process-wait-milliseconds))))
143 (setq procs (cdr procs))))
144
145(defalias 'eshell/wait 'eshell-wait-for-process)
146
147(defun eshell/jobs (&rest args)
148 "List processes, if there are any."
ca7aae91
JW
149 (and (fboundp 'process-list)
150 (process-list)
affbf647
GM
151 (list-processes)))
152
153(defun eshell/kill (&rest args)
154 "Kill processes, buffers, symbol or files."
155 (let ((ptr args)
156 (signum 'SIGINT))
157 (while ptr
ca7aae91 158 (if (or (eshell-processp (car ptr))
affbf647
GM
159 (and (stringp (car ptr))
160 (string-match "^[A-Za-z/][A-Za-z0-9<>/]+$"
161 (car ptr))))
162 ;; What about when $lisp-variable is possible here?
163 ;; It could very well name a process.
164 (setcar ptr (get-process (car ptr))))
165 (setq ptr (cdr ptr)))
166 (while args
ca7aae91 167 (let ((id (if (eshell-processp (car args))
affbf647
GM
168 (process-id (car args))
169 (car args))))
170 (when id
171 (cond
172 ((null id)
173 (error "kill: bad signal spec"))
174 ((and (numberp id) (= id 0))
175 (error "kill: bad signal spec `%d'" id))
176 ((and (stringp id)
177 (string-match "^-?[0-9]+$" id))
178 (setq signum (abs (string-to-number id))))
179 ((stringp id)
180 (let (case-fold-search)
181 (if (string-match "^-\\([A-Z]+\\)$" id)
182 (setq signum
183 (intern (concat "SIG" (match-string 1 id))))
184 (error "kill: bad signal spec `%s'" id))))
185 ((< id 0)
186 (setq signum (abs id)))
187 (t
188 (signal-process id signum)))))
189 (setq args (cdr args)))
190 nil))
191
192(defun eshell-read-process-name (prompt)
193 "Read the name of a process from the minibuffer, using completion.
194The prompt will be set to PROMPT."
195 (completing-read prompt
196 (mapcar
197 (function
198 (lambda (proc)
199 (cons (process-name proc) t)))
200 (process-list)) nil t))
201
202(defun eshell-insert-process (process)
203 "Insert the name of PROCESS into the current buffer at point."
204 (interactive
205 (list (get-process
206 (eshell-read-process-name "Name of process: "))))
207 (insert-and-inherit "#<process " (process-name process) ">"))
208
209(defsubst eshell-record-process-object (object)
210 "Record OBJECT as now running."
ca7aae91 211 (if (and (eshell-processp object)
affbf647
GM
212 eshell-current-subjob-p)
213 (eshell-interactive-print
214 (format "[%s] %d\n" (process-name object) (process-id object))))
215 (setq eshell-process-list
216 (cons (list object eshell-current-handles
217 eshell-current-subjob-p nil nil)
218 eshell-process-list)))
219
220(defun eshell-remove-process-entry (entry)
221 "Record the process ENTRY as fully completed."
ca7aae91 222 (if (and (eshell-processp (car entry))
affbf647
GM
223 (nth 2 entry)
224 eshell-done-messages-in-minibuffer)
225 (message (format "[%s]+ Done %s" (process-name (car entry))
226 (process-command (car entry)))))
227 (setq eshell-process-list
228 (delq entry eshell-process-list)))
229
ca7aae91
JW
230(defvar eshell-scratch-buffer " *eshell-scratch*"
231 "Scratch buffer for holding Eshell's input/output.")
232(defvar eshell-last-sync-output-start nil
233 "A marker that tracks the beginning of output of the last subprocess.
234Used only on systems which do not support async subprocesses.")
235
affbf647
GM
236(defun eshell-gather-process-output (command args)
237 "Gather the output from COMMAND + ARGS."
238 (unless (and (file-executable-p command)
239 (file-regular-p command))
240 (error "%s: not an executable file" command))
241 (let* ((delete-exited-processes
242 (if eshell-current-subjob-p
243 eshell-delete-exited-processes
244 delete-exited-processes))
245 (process-environment (eshell-environment-variables))
ca7aae91
JW
246 proc decoding encoding changed)
247 (cond
248 ((fboundp 'start-process)
249 (setq proc
250 (apply 'start-process
251 (file-name-nondirectory command) nil
252 ;; `start-process' can't deal with relative
253 ;; filenames
254 (append (list (expand-file-name command)) args)))
255 (eshell-record-process-object proc)
256 (set-process-buffer proc (current-buffer))
257 (if (eshell-interactive-output-p)
258 (set-process-filter proc 'eshell-output-filter)
259 (set-process-filter proc 'eshell-insertion-filter))
260 (set-process-sentinel proc 'eshell-sentinel)
261 (run-hook-with-args 'eshell-exec-hook proc)
262 (when (fboundp 'process-coding-system)
263 (let ((coding-systems (process-coding-system proc)))
264 (setq decoding (car coding-systems)
265 encoding (cdr coding-systems)))
266 ;; If start-process decided to use some coding system for
267 ;; decoding data sent from the process and the coding system
268 ;; doesn't specify EOL conversion, we had better convert CRLF
269 ;; to LF.
270 (if (vectorp (coding-system-eol-type decoding))
271 (setq decoding (coding-system-change-eol-conversion decoding 'dos)
272 changed t))
273 ;; Even if start-process left the coding system for encoding
274 ;; data sent from the process undecided, we had better use the
275 ;; same one as what we use for decoding. But, we should
276 ;; suppress EOL conversion.
277 (if (and decoding (not encoding))
278 (setq encoding (coding-system-change-eol-conversion decoding 'unix)
279 changed t))
280 (if changed
281 (set-process-coding-system proc decoding encoding))))
282 (t
283 ;; No async subprocesses...
284 (let ((oldbuf (current-buffer))
285 (interact-p (eshell-interactive-output-p))
286 lbeg lend line proc-buf exit-status)
287 (and (not (markerp eshell-last-sync-output-start))
288 (setq eshell-last-sync-output-start (point-marker)))
289 (setq proc-buf
290 (set-buffer (get-buffer-create eshell-scratch-buffer)))
291 (erase-buffer)
292 (set-buffer oldbuf)
293 (run-hook-with-args 'eshell-exec-hook command)
294 (setq exit-status
295 (apply 'call-process-region
296 (append (list eshell-last-sync-output-start (point)
297 command t
298 eshell-scratch-buffer nil)
299 args)))
300 ;; When in a pipeline, record the place where the output of
301 ;; this process will begin.
302 (and eshell-in-pipeline-p
303 (set-marker eshell-last-sync-output-start (point)))
304 ;; Simulate the effect of the process filter.
305 (when (numberp exit-status)
306 (set-buffer proc-buf)
307 (goto-char (point-min))
308 (setq lbeg (point))
309 (while (eq 0 (forward-line 1))
310 (setq lend (point)
311 line (buffer-substring-no-properties lbeg lend))
312 (set-buffer oldbuf)
313 (if interact-p
314 (eshell-output-filter nil line)
315 (eshell-output-object line))
316 (setq lbeg lend)
317 (set-buffer proc-buf))
318 (set-buffer oldbuf))
319 (eshell-update-markers eshell-last-output-end)
320 ;; Simulate the effect of eshell-sentinel.
321 (eshell-close-handles (if (numberp exit-status) exit-status -1))
322 (run-hook-with-args 'eshell-kill-hook command exit-status)
323 (or eshell-in-pipeline-p
324 (setq eshell-last-sync-output-start nil))
325 (if (not (numberp exit-status))
326 (error "%s: external command failed: %s" command exit-status))
327 (setq proc t))))
affbf647
GM
328 proc))
329
330(defun eshell-insertion-filter (proc string)
331 "Insert a string into the eshell buffer, or a process/file/buffer.
332PROC is the process for which we're inserting output. STRING is the
333output."
334 (when (buffer-live-p (process-buffer proc))
335 (set-buffer (process-buffer proc))
336 (let ((entry (assq proc eshell-process-list)))
337 (when entry
338 (setcar (nthcdr 3 entry)
339 (concat (nth 3 entry) string))
340 (unless (nth 4 entry) ; already being handled?
341 (while (nth 3 entry)
342 (let ((data (nth 3 entry)))
343 (setcar (nthcdr 3 entry) nil)
344 (setcar (nthcdr 4 entry) t)
345 (eshell-output-object data nil (cadr entry))
346 (setcar (nthcdr 4 entry) nil))))))))
347
348(defun eshell-sentinel (proc string)
349 "Generic sentinel for command processes. Reports only signals.
350PROC is the process that's exiting. STRING is the exit message."
351 (when (buffer-live-p (process-buffer proc))
352 (set-buffer (process-buffer proc))
353 (unwind-protect
354 (let* ((entry (assq proc eshell-process-list)))
355; (if (not entry)
356; (error "Sentinel called for unowned process `%s'"
357; (process-name proc))
358 (when entry
359 (unwind-protect
360 (progn
361 (unless (string= string "run")
362 (unless (string-match "^\\(finished\\|exited\\)" string)
363 (eshell-insertion-filter proc string))
364 (eshell-close-handles (process-exit-status proc) 'nil
365 (cadr entry))))
366 (eshell-remove-process-entry entry))))
367 (run-hook-with-args 'eshell-kill-hook proc string))))
368
369(defun eshell-process-interact (func &optional all query)
370 "Interact with a process, using PROMPT if more than one, via FUNC.
371If ALL is non-nil, background processes will be interacted with as well.
372If QUERY is non-nil, query the user with QUERY before calling FUNC."
373 (let (defunct result)
374 (eshell-for entry eshell-process-list
375 (if (and (memq (process-status (car entry))
376 '(run stop open closed))
377 (or all
378 (not (nth 2 entry)))
379 (or (not query)
380 (y-or-n-p (format query (process-name (car entry))))))
381 (setq result (funcall func (car entry))))
382 (unless (memq (process-status (car entry))
383 '(run stop open closed))
384 (setq defunct (cons entry defunct))))
385 ;; clean up the process list; this can get dirty if an error
386 ;; occurred that brought the user into the debugger, and then they
387 ;; quit, so that the sentinel was never called.
388 (eshell-for d defunct
389 (eshell-remove-process-entry d))
390 result))
391
392(defcustom eshell-kill-process-wait-time 5
393 "*Seconds to wait between sending termination signals to a subprocess."
394 :type 'integer
395 :group 'eshell-proc)
396
397(defcustom eshell-kill-process-signals '(SIGINT SIGQUIT SIGKILL)
398 "*Signals used to kill processes when an Eshell buffer exits.
399Eshell calls each of these signals in order when an Eshell buffer is
400killed; if the process is still alive afterwards, Eshell waits a
401number of seconds defined by `eshell-kill-process-wait-time', and
402tries the next signal in the list."
403 :type '(repeat symbol)
404 :group 'eshell-proc)
405
406(defcustom eshell-kill-processes-on-exit nil
407 "*If non-nil, kill active processes when exiting an Eshell buffer.
408Emacs will only kill processes owned by that Eshell buffer.
409
410If nil, ownership of background and foreground processes reverts to
411Emacs itself, and will die only if the user exits Emacs, calls
412`kill-process', or terminates the processes externally.
413
414If `ask', Emacs prompts the user before killing any processes.
415
416If `every', it prompts once for every process.
417
418If t, it kills all buffer-owned processes without asking.
419
420Processes are first sent SIGHUP, then SIGINT, then SIGQUIT, then
421SIGKILL. The variable `eshell-kill-process-wait-time' specifies how
422long to delay between signals."
423 :type '(choice (const :tag "Kill all, don't ask" t)
424 (const :tag "Ask before killing" ask)
425 (const :tag "Ask for each process" every)
426 (const :tag "Don't kill subprocesses" nil))
427 :group 'eshell-proc)
428
429(defun eshell-round-robin-kill (&optional query)
430 "Kill current process by trying various signals in sequence.
431See the variable `eshell-kill-processes-on-exit'."
432 (let ((sigs eshell-kill-process-signals))
433 (while sigs
434 (eshell-process-interact
435 (function
436 (lambda (proc)
437 (signal-process (process-id proc) (car sigs)))) t query)
438 (setq query nil)
439 (if (not eshell-process-list)
440 (setq sigs nil)
441 (sleep-for eshell-kill-process-wait-time)
442 (setq sigs (cdr sigs))))))
443
444(defun eshell-query-kill-processes ()
445 "Kill processes belonging to the current Eshell buffer, possibly w/ query."
446 (when (and eshell-kill-processes-on-exit
447 eshell-process-list)
448 (save-window-excursion
449 (list-processes)
450 (if (or (not (eq eshell-kill-processes-on-exit 'ask))
451 (y-or-n-p (format "Kill processes owned by `%s'? "
452 (buffer-name))))
453 (eshell-round-robin-kill
454 (if (eq eshell-kill-processes-on-exit 'every)
455 "Kill Eshell child process `%s'? ")))
456 (let ((buf (get-buffer "*Process List*")))
457 (if (and buf (buffer-live-p buf))
458 (kill-buffer buf)))
459 (message nil))))
460
461(custom-add-option 'eshell-exit-hook 'eshell-query-kill-processes)
462
463(defun eshell-interrupt-process ()
464 "Interrupt a process."
465 (interactive)
466 (unless (eshell-process-interact 'interrupt-process)
467 (run-hook-with-args 'eshell-kill-hook nil "interrupt")))
468
469(defun eshell-kill-process ()
470 "Kill a process."
471 (interactive)
472 (unless (eshell-process-interact 'kill-process)
473 (run-hook-with-args 'eshell-kill-hook nil "killed")))
474
475(defun eshell-quit-process ()
476 "Send quit signal to process."
477 (interactive)
478 (unless (eshell-process-interact 'quit-process)
479 (run-hook-with-args 'eshell-kill-hook nil "quit")))
480
481(defun eshell-stop-process ()
482 "Send STOP signal to process."
483 (interactive)
484 (unless (eshell-process-interact 'stop-process)
485 (run-hook-with-args 'eshell-kill-hook nil "stopped")))
486
487(defun eshell-continue-process ()
488 "Send CONTINUE signal to process."
489 (interactive)
490 (unless (eshell-process-interact 'continue-process)
491 ;; jww (1999-09-17): this signal is not dealt with yet. For
492 ;; example, `eshell-reset' will be called, and so will
493 ;; `eshell-resume-eval'.
494 (run-hook-with-args 'eshell-kill-hook nil "continue")))
495
496(defun eshell-send-eof-to-process ()
497 "Send EOF to process."
498 (interactive)
499 (eshell-send-input nil nil t)
500 (eshell-process-interact 'process-send-eof))
501
502;;; Code:
503
504;;; esh-proc.el ends here