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