1 ;;; esh-proc --- process management
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
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)
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.
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.
24 (eval-when-compile (require 'esh-maint))
26 (defgroup eshell-proc nil
27 "When Eshell invokes external commands, it always does so
28 asynchronously, so that Emacs isn't tied up waiting for the process to
30 :tag "Process management"
37 (defcustom eshell-proc-load-hook '(eshell-proc-initialize)
38 "*A hook that gets run when `eshell-proc' is loaded."
42 (defcustom eshell-process-wait-seconds 0
43 "*The number of seconds to delay waiting for a synchronous process."
47 (defcustom eshell-process-wait-milliseconds 50
48 "*The number of milliseconds to delay waiting for a synchronous process."
52 (defcustom eshell-done-messages-in-minibuffer t
53 "*If non-nil, subjob \"Done\" messages will display in minibuffer."
57 (defcustom eshell-delete-exited-processes t
58 "*If nil, process entries will stick around until `jobs' is run.
59 This variable sets the buffer-local value of `delete-exited-processes'
62 This variable causes Eshell to mimic the behavior of bash when set to
63 nil. It allows the user to view the exit status of a completed subjob
64 \(process) at their leisure, because the process entry remains in
65 memory until the user examines it using \\[list-processes].
67 Otherwise, if `eshell-done-messages-in-minibuffer' is nil, and this
68 variable is set to t, the only indication the user will have that a
69 subjob is done is that it will no longer appear in the
70 \\[list-processes\\] display.
72 Note that Eshell will have to be restarted for a change in this
73 variable's value to take effect."
77 (defcustom eshell-reset-signals
78 "^\\(interrupt\\|killed\\|quit\\|stopped\\)"
79 "*If a termination signal matches this regexp, the terminal will be reset."
83 (defcustom eshell-exec-hook nil
84 "*Called each time a process is exec'd by `eshell-gather-process-output'.
85 It is passed one argument, which is the process that was just started.
86 It is useful for things that must be done each time a process is
87 executed in a eshell mode buffer (e.g., `process-kill-without-query').
88 In contrast, `eshell-mode-hook' is only executed once when the buffer
93 (defcustom eshell-kill-hook '(eshell-reset-after-proc)
94 "*Called when a process run by `eshell-gather-process-output' has ended.
95 It is passed two arguments: the process that was just ended, and the
96 termination status (as a string). Note that the first argument may be
97 nil, in which case the user attempted to send a signal, but there was
98 no relevant process. This can be used for displaying help
99 information, for example."
103 ;;; Internal Variables:
105 (defvar eshell-current-subjob-p nil)
107 (defvar eshell-process-list nil
108 "A list of the current status of subprocesses.")
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))
124 (defun eshell-reset-after-proc (proc status)
125 "Reset the command input location after a process terminates.
126 The signals which will cause this to happen are matched by
127 `eshell-reset-signals'."
128 (if (and (stringp status)
129 (string-match eshell-reset-signals status))
132 (defun eshell-wait-for-process (&rest procs)
133 "Wait until PROC has successfully completed."
135 (let ((proc (car procs)))
136 (when (eshell-processp proc)
137 ;; NYI: If the process gets stopped here, that's bad.
138 (while (assq proc eshell-process-list)
139 (if (input-pending-p)
141 (sit-for eshell-process-wait-seconds
142 eshell-process-wait-milliseconds))))
143 (setq procs (cdr procs))))
145 (defalias 'eshell/wait 'eshell-wait-for-process)
147 (defun eshell/jobs (&rest args)
148 "List processes, if there are any."
149 (and (fboundp 'process-list)
153 (defun eshell/kill (&rest args)
154 "Kill processes, buffers, symbol or files."
158 (if (or (eshell-processp (car ptr))
159 (and (stringp (car ptr))
160 (string-match "^[A-Za-z/][A-Za-z0-9<>/]+$"
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)))
167 (let ((id (if (eshell-processp (car args))
168 (process-id (car args))
173 (error "kill: bad signal spec"))
174 ((and (numberp id) (= id 0))
175 (error "kill: bad signal spec `%d'" id))
177 (string-match "^-?[0-9]+$" id))
178 (setq signum (abs (string-to-number id))))
180 (let (case-fold-search)
181 (if (string-match "^-\\([A-Z]+\\)$" id)
183 (intern (concat "SIG" (match-string 1 id))))
184 (error "kill: bad signal spec `%s'" id))))
186 (setq signum (abs id)))
188 (signal-process id signum)))))
189 (setq args (cdr args)))
192 (defun eshell-read-process-name (prompt)
193 "Read the name of a process from the minibuffer, using completion.
194 The prompt will be set to PROMPT."
195 (completing-read prompt
199 (cons (process-name proc) t)))
200 (process-list)) nil t))
202 (defun eshell-insert-process (process)
203 "Insert the name of PROCESS into the current buffer at point."
206 (eshell-read-process-name "Name of process: "))))
207 (insert-and-inherit "#<process " (process-name process) ">"))
209 (defsubst eshell-record-process-object (object)
210 "Record OBJECT as now running."
211 (if (and (eshell-processp object)
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)))
220 (defun eshell-remove-process-entry (entry)
221 "Record the process ENTRY as fully completed."
222 (if (and (eshell-processp (car 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)))
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.
234 Used only on systems which do not support async subprocesses.")
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))
246 proc decoding encoding changed)
248 ((fboundp 'start-process)
250 (apply 'start-process
251 (file-name-nondirectory command) nil
252 ;; `start-process' can't deal with relative
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
270 (if (vectorp (coding-system-eol-type decoding))
271 (setq decoding (coding-system-change-eol-conversion decoding 'dos)
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)
281 (set-process-coding-system proc decoding encoding))))
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)))
290 (set-buffer (get-buffer-create eshell-scratch-buffer)))
293 (run-hook-with-args 'eshell-exec-hook command)
295 (apply 'call-process-region
296 (append (list eshell-last-sync-output-start (point)
298 eshell-scratch-buffer nil)
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))
309 (while (eq 0 (forward-line 1))
311 line (buffer-substring-no-properties lbeg lend))
314 (eshell-output-filter nil line)
315 (eshell-output-object line))
317 (set-buffer proc-buf))
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))
330 (defun eshell-insertion-filter (proc string)
331 "Insert a string into the eshell buffer, or a process/file/buffer.
332 PROC is the process for which we're inserting output. STRING is the
334 (when (buffer-live-p (process-buffer proc))
335 (set-buffer (process-buffer proc))
336 (let ((entry (assq proc eshell-process-list)))
338 (setcar (nthcdr 3 entry)
339 (concat (nth 3 entry) string))
340 (unless (nth 4 entry) ; already being handled?
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))))))))
348 (defun eshell-sentinel (proc string)
349 "Generic sentinel for command processes. Reports only signals.
350 PROC 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))
354 (let* ((entry (assq proc eshell-process-list)))
356 ; (error "Sentinel called for unowned process `%s'"
357 ; (process-name proc))
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
366 (eshell-remove-process-entry entry))))
367 (run-hook-with-args 'eshell-kill-hook proc string))))
369 (defun eshell-process-interact (func &optional all query)
370 "Interact with a process, using PROMPT if more than one, via FUNC.
371 If ALL is non-nil, background processes will be interacted with as well.
372 If 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))
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))
392 (defcustom eshell-kill-process-wait-time 5
393 "*Seconds to wait between sending termination signals to a subprocess."
397 (defcustom eshell-kill-process-signals '(SIGINT SIGQUIT SIGKILL)
398 "*Signals used to kill processes when an Eshell buffer exits.
399 Eshell calls each of these signals in order when an Eshell buffer is
400 killed; if the process is still alive afterwards, Eshell waits a
401 number of seconds defined by `eshell-kill-process-wait-time', and
402 tries the next signal in the list."
403 :type '(repeat symbol)
406 (defcustom eshell-kill-processes-on-exit nil
407 "*If non-nil, kill active processes when exiting an Eshell buffer.
408 Emacs will only kill processes owned by that Eshell buffer.
410 If nil, ownership of background and foreground processes reverts to
411 Emacs itself, and will die only if the user exits Emacs, calls
412 `kill-process', or terminates the processes externally.
414 If `ask', Emacs prompts the user before killing any processes.
416 If `every', it prompts once for every process.
418 If t, it kills all buffer-owned processes without asking.
420 Processes are first sent SIGHUP, then SIGINT, then SIGQUIT, then
421 SIGKILL. The variable `eshell-kill-process-wait-time' specifies how
422 long 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))
429 (defun eshell-round-robin-kill (&optional query)
430 "Kill current process by trying various signals in sequence.
431 See the variable `eshell-kill-processes-on-exit'."
432 (let ((sigs eshell-kill-process-signals))
434 (eshell-process-interact
437 (signal-process (process-id proc) (car sigs)))) t query)
439 (if (not eshell-process-list)
441 (sleep-for eshell-kill-process-wait-time)
442 (setq sigs (cdr sigs))))))
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
448 (save-window-excursion
450 (if (or (not (eq eshell-kill-processes-on-exit 'ask))
451 (y-or-n-p (format "Kill processes owned by `%s'? "
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))
461 (custom-add-option 'eshell-exit-hook 'eshell-query-kill-processes)
463 (defun eshell-interrupt-process ()
464 "Interrupt a process."
466 (unless (eshell-process-interact 'interrupt-process)
467 (run-hook-with-args 'eshell-kill-hook nil "interrupt")))
469 (defun eshell-kill-process ()
472 (unless (eshell-process-interact 'kill-process)
473 (run-hook-with-args 'eshell-kill-hook nil "killed")))
475 (defun eshell-quit-process ()
476 "Send quit signal to process."
478 (unless (eshell-process-interact 'quit-process)
479 (run-hook-with-args 'eshell-kill-hook nil "quit")))
481 (defun eshell-stop-process ()
482 "Send STOP signal to process."
484 (unless (eshell-process-interact 'stop-process)
485 (run-hook-with-args 'eshell-kill-hook nil "stopped")))
487 (defun eshell-continue-process ()
488 "Send CONTINUE signal to process."
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")))
496 (defun eshell-send-eof-to-process ()
497 "Send EOF to process."
499 (eshell-send-input nil nil t)
500 (eshell-process-interact 'process-send-eof))
504 ;;; esh-proc.el ends here