Misc manual updates.
[bpt/emacs.git] / lisp / eshell / esh-proc.el
CommitLineData
60370d40 1;;; esh-proc.el --- process management
affbf647 2
acaf905b 3;; Copyright (C) 1999-2012 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)
168 "Kill processes, buffers, symbol or files."
169 (let ((ptr args)
170 (signum 'SIGINT))
171 (while ptr
ca7aae91 172 (if (or (eshell-processp (car ptr))
affbf647
GM
173 (and (stringp (car ptr))
174 (string-match "^[A-Za-z/][A-Za-z0-9<>/]+$"
175 (car ptr))))
176 ;; What about when $lisp-variable is possible here?
177 ;; It could very well name a process.
178 (setcar ptr (get-process (car ptr))))
179 (setq ptr (cdr ptr)))
180 (while args
ca7aae91 181 (let ((id (if (eshell-processp (car args))
affbf647
GM
182 (process-id (car args))
183 (car args))))
184 (when id
185 (cond
186 ((null id)
187 (error "kill: bad signal spec"))
188 ((and (numberp id) (= id 0))
189 (error "kill: bad signal spec `%d'" id))
190 ((and (stringp id)
191 (string-match "^-?[0-9]+$" id))
192 (setq signum (abs (string-to-number id))))
193 ((stringp id)
194 (let (case-fold-search)
be6e5004 195 (if (string-match "^-\\([A-Z]+[12]?\\)$" id)
affbf647
GM
196 (setq signum
197 (intern (concat "SIG" (match-string 1 id))))
198 (error "kill: bad signal spec `%s'" id))))
199 ((< id 0)
200 (setq signum (abs id)))
201 (t
202 (signal-process id signum)))))
203 (setq args (cdr args)))
204 nil))
205
206(defun eshell-read-process-name (prompt)
207 "Read the name of a process from the minibuffer, using completion.
208The prompt will be set to PROMPT."
209 (completing-read prompt
210 (mapcar
211 (function
212 (lambda (proc)
213 (cons (process-name proc) t)))
214 (process-list)) nil t))
215
216(defun eshell-insert-process (process)
217 "Insert the name of PROCESS into the current buffer at point."
218 (interactive
219 (list (get-process
220 (eshell-read-process-name "Name of process: "))))
221 (insert-and-inherit "#<process " (process-name process) ">"))
222
223(defsubst eshell-record-process-object (object)
224 "Record OBJECT as now running."
ca7aae91 225 (if (and (eshell-processp object)
affbf647
GM
226 eshell-current-subjob-p)
227 (eshell-interactive-print
228 (format "[%s] %d\n" (process-name object) (process-id object))))
229 (setq eshell-process-list
230 (cons (list object eshell-current-handles
231 eshell-current-subjob-p nil nil)
232 eshell-process-list)))
233
234(defun eshell-remove-process-entry (entry)
235 "Record the process ENTRY as fully completed."
ca7aae91 236 (if (and (eshell-processp (car entry))
affbf647
GM
237 (nth 2 entry)
238 eshell-done-messages-in-minibuffer)
b2a9524a
DG
239 (message "[%s]+ Done %s" (process-name (car entry))
240 (process-command (car entry))))
affbf647
GM
241 (setq eshell-process-list
242 (delq entry eshell-process-list)))
243
ca7aae91
JW
244(defvar eshell-scratch-buffer " *eshell-scratch*"
245 "Scratch buffer for holding Eshell's input/output.")
246(defvar eshell-last-sync-output-start nil
247 "A marker that tracks the beginning of output of the last subprocess.
248Used only on systems which do not support async subprocesses.")
249
1e262c45
GM
250(defvar eshell-needs-pipe '("bc")
251 "List of commands which need `process-connection-type' to be nil.
252Currently only affects commands in pipelines, and not those at
253the front. If an element contains a directory part it must match
254the full name of a command, otherwise just the nondirectory part must match.")
255
256(defun eshell-needs-pipe-p (command)
257 "Return non-nil if COMMAND needs `process-connection-type' to be nil.
258See `eshell-needs-pipe'."
259 (and eshell-in-pipeline-p
260 (not (eq eshell-in-pipeline-p 'first))
261 ;; FIXME should this return non-nil for anything that is
262 ;; neither 'first nor 'last? See bug#1388 discussion.
263 (catch 'found
264 (dolist (exe eshell-needs-pipe)
265 (if (string-equal exe (if (string-match "/" exe)
266 command
267 (file-name-nondirectory command)))
268 (throw 'found t))))))
269
affbf647
GM
270(defun eshell-gather-process-output (command args)
271 "Gather the output from COMMAND + ARGS."
272 (unless (and (file-executable-p command)
605a20a9 273 (file-regular-p (file-truename command)))
affbf647
GM
274 (error "%s: not an executable file" command))
275 (let* ((delete-exited-processes
276 (if eshell-current-subjob-p
277 eshell-delete-exited-processes
278 delete-exited-processes))
279 (process-environment (eshell-environment-variables))
ca7aae91
JW
280 proc decoding encoding changed)
281 (cond
605a20a9 282 ((fboundp 'start-file-process)
ca7aae91 283 (setq proc
1e262c45
GM
284 (let ((process-connection-type
285 (unless (eshell-needs-pipe-p command)
605a20a9
MA
286 process-connection-type))
287 (command (or (file-remote-p command 'localname) command)))
288 (apply 'start-file-process
1e262c45
GM
289 (file-name-nondirectory command) nil
290 ;; `start-process' can't deal with relative filenames.
291 (append (list (expand-file-name command)) args))))
ca7aae91
JW
292 (eshell-record-process-object proc)
293 (set-process-buffer proc (current-buffer))
294 (if (eshell-interactive-output-p)
295 (set-process-filter proc 'eshell-output-filter)
296 (set-process-filter proc 'eshell-insertion-filter))
297 (set-process-sentinel proc 'eshell-sentinel)
298 (run-hook-with-args 'eshell-exec-hook proc)
299 (when (fboundp 'process-coding-system)
300 (let ((coding-systems (process-coding-system proc)))
301 (setq decoding (car coding-systems)
302 encoding (cdr coding-systems)))
303 ;; If start-process decided to use some coding system for
304 ;; decoding data sent from the process and the coding system
305 ;; doesn't specify EOL conversion, we had better convert CRLF
306 ;; to LF.
307 (if (vectorp (coding-system-eol-type decoding))
308 (setq decoding (coding-system-change-eol-conversion decoding 'dos)
309 changed t))
310 ;; Even if start-process left the coding system for encoding
311 ;; data sent from the process undecided, we had better use the
312 ;; same one as what we use for decoding. But, we should
313 ;; suppress EOL conversion.
314 (if (and decoding (not encoding))
315 (setq encoding (coding-system-change-eol-conversion decoding 'unix)
316 changed t))
317 (if changed
318 (set-process-coding-system proc decoding encoding))))
319 (t
320 ;; No async subprocesses...
321 (let ((oldbuf (current-buffer))
322 (interact-p (eshell-interactive-output-p))
323 lbeg lend line proc-buf exit-status)
324 (and (not (markerp eshell-last-sync-output-start))
325 (setq eshell-last-sync-output-start (point-marker)))
326 (setq proc-buf
327 (set-buffer (get-buffer-create eshell-scratch-buffer)))
328 (erase-buffer)
329 (set-buffer oldbuf)
330 (run-hook-with-args 'eshell-exec-hook command)
331 (setq exit-status
332 (apply 'call-process-region
333 (append (list eshell-last-sync-output-start (point)
334 command t
335 eshell-scratch-buffer nil)
336 args)))
337 ;; When in a pipeline, record the place where the output of
338 ;; this process will begin.
339 (and eshell-in-pipeline-p
340 (set-marker eshell-last-sync-output-start (point)))
341 ;; Simulate the effect of the process filter.
342 (when (numberp exit-status)
343 (set-buffer proc-buf)
344 (goto-char (point-min))
345 (setq lbeg (point))
346 (while (eq 0 (forward-line 1))
347 (setq lend (point)
348 line (buffer-substring-no-properties lbeg lend))
349 (set-buffer oldbuf)
350 (if interact-p
351 (eshell-output-filter nil line)
352 (eshell-output-object line))
353 (setq lbeg lend)
354 (set-buffer proc-buf))
355 (set-buffer oldbuf))
356 (eshell-update-markers eshell-last-output-end)
357 ;; Simulate the effect of eshell-sentinel.
358 (eshell-close-handles (if (numberp exit-status) exit-status -1))
d783d303 359 (eshell-kill-process-function command exit-status)
ca7aae91
JW
360 (or eshell-in-pipeline-p
361 (setq eshell-last-sync-output-start nil))
362 (if (not (numberp exit-status))
363 (error "%s: external command failed: %s" command exit-status))
364 (setq proc t))))
affbf647
GM
365 proc))
366
367(defun eshell-insertion-filter (proc string)
368 "Insert a string into the eshell buffer, or a process/file/buffer.
369PROC is the process for which we're inserting output. STRING is the
370output."
371 (when (buffer-live-p (process-buffer proc))
dafac6f3
GM
372 (with-current-buffer (process-buffer proc)
373 (let ((entry (assq proc eshell-process-list)))
374 (when entry
375 (setcar (nthcdr 3 entry)
376 (concat (nth 3 entry) string))
377 (unless (nth 4 entry) ; already being handled?
378 (while (nth 3 entry)
379 (let ((data (nth 3 entry)))
380 (setcar (nthcdr 3 entry) nil)
381 (setcar (nthcdr 4 entry) t)
382 (eshell-output-object data nil (cadr entry))
383 (setcar (nthcdr 4 entry) nil)))))))))
affbf647
GM
384
385(defun eshell-sentinel (proc string)
386 "Generic sentinel for command processes. Reports only signals.
387PROC is the process that's exiting. STRING is the exit message."
388 (when (buffer-live-p (process-buffer proc))
dafac6f3
GM
389 (with-current-buffer (process-buffer proc)
390 (unwind-protect
391 (let* ((entry (assq proc eshell-process-list)))
392; (if (not entry)
393; (error "Sentinel called for unowned process `%s'"
394; (process-name proc))
395 (when entry
396 (unwind-protect
397 (progn
398 (unless (string= string "run")
399 (unless (string-match "^\\(finished\\|exited\\)" string)
400 (eshell-insertion-filter proc string))
401 (eshell-close-handles (process-exit-status proc) 'nil
402 (cadr entry))))
403 (eshell-remove-process-entry entry))))
d783d303 404 (eshell-kill-process-function proc string)))))
affbf647
GM
405
406(defun eshell-process-interact (func &optional all query)
407 "Interact with a process, using PROMPT if more than one, via FUNC.
408If ALL is non-nil, background processes will be interacted with as well.
409If QUERY is non-nil, query the user with QUERY before calling FUNC."
410 (let (defunct result)
a9eeff78 411 (dolist (entry eshell-process-list)
affbf647
GM
412 (if (and (memq (process-status (car entry))
413 '(run stop open closed))
414 (or all
415 (not (nth 2 entry)))
416 (or (not query)
417 (y-or-n-p (format query (process-name (car entry))))))
418 (setq result (funcall func (car entry))))
419 (unless (memq (process-status (car entry))
420 '(run stop open closed))
421 (setq defunct (cons entry defunct))))
422 ;; clean up the process list; this can get dirty if an error
423 ;; occurred that brought the user into the debugger, and then they
424 ;; quit, so that the sentinel was never called.
a9eeff78 425 (dolist (d defunct)
affbf647
GM
426 (eshell-remove-process-entry d))
427 result))
428
429(defcustom eshell-kill-process-wait-time 5
ec60da52 430 "Seconds to wait between sending termination signals to a subprocess."
affbf647
GM
431 :type 'integer
432 :group 'eshell-proc)
433
434(defcustom eshell-kill-process-signals '(SIGINT SIGQUIT SIGKILL)
ec60da52 435 "Signals used to kill processes when an Eshell buffer exits.
affbf647
GM
436Eshell calls each of these signals in order when an Eshell buffer is
437killed; if the process is still alive afterwards, Eshell waits a
438number of seconds defined by `eshell-kill-process-wait-time', and
439tries the next signal in the list."
440 :type '(repeat symbol)
441 :group 'eshell-proc)
442
443(defcustom eshell-kill-processes-on-exit nil
ec60da52 444 "If non-nil, kill active processes when exiting an Eshell buffer.
affbf647
GM
445Emacs will only kill processes owned by that Eshell buffer.
446
447If nil, ownership of background and foreground processes reverts to
448Emacs itself, and will die only if the user exits Emacs, calls
449`kill-process', or terminates the processes externally.
450
451If `ask', Emacs prompts the user before killing any processes.
452
453If `every', it prompts once for every process.
454
455If t, it kills all buffer-owned processes without asking.
456
457Processes are first sent SIGHUP, then SIGINT, then SIGQUIT, then
458SIGKILL. The variable `eshell-kill-process-wait-time' specifies how
459long to delay between signals."
460 :type '(choice (const :tag "Kill all, don't ask" t)
461 (const :tag "Ask before killing" ask)
462 (const :tag "Ask for each process" every)
463 (const :tag "Don't kill subprocesses" nil))
464 :group 'eshell-proc)
465
466(defun eshell-round-robin-kill (&optional query)
467 "Kill current process by trying various signals in sequence.
468See the variable `eshell-kill-processes-on-exit'."
469 (let ((sigs eshell-kill-process-signals))
470 (while sigs
471 (eshell-process-interact
472 (function
473 (lambda (proc)
474 (signal-process (process-id proc) (car sigs)))) t query)
475 (setq query nil)
476 (if (not eshell-process-list)
477 (setq sigs nil)
478 (sleep-for eshell-kill-process-wait-time)
479 (setq sigs (cdr sigs))))))
480
481(defun eshell-query-kill-processes ()
482 "Kill processes belonging to the current Eshell buffer, possibly w/ query."
483 (when (and eshell-kill-processes-on-exit
484 eshell-process-list)
485 (save-window-excursion
486 (list-processes)
487 (if (or (not (eq eshell-kill-processes-on-exit 'ask))
488 (y-or-n-p (format "Kill processes owned by `%s'? "
489 (buffer-name))))
490 (eshell-round-robin-kill
491 (if (eq eshell-kill-processes-on-exit 'every)
492 "Kill Eshell child process `%s'? ")))
493 (let ((buf (get-buffer "*Process List*")))
494 (if (and buf (buffer-live-p buf))
495 (kill-buffer buf)))
496 (message nil))))
497
affbf647
GM
498(defun eshell-interrupt-process ()
499 "Interrupt a process."
500 (interactive)
501 (unless (eshell-process-interact 'interrupt-process)
d783d303 502 (eshell-kill-process-function nil "interrupt")))
affbf647
GM
503
504(defun eshell-kill-process ()
505 "Kill a process."
506 (interactive)
507 (unless (eshell-process-interact 'kill-process)
d783d303 508 (eshell-kill-process-function nil "killed")))
affbf647
GM
509
510(defun eshell-quit-process ()
511 "Send quit signal to process."
512 (interactive)
513 (unless (eshell-process-interact 'quit-process)
d783d303 514 (eshell-kill-process-function nil "quit")))
affbf647 515
36e81327
JW
516;(defun eshell-stop-process ()
517; "Send STOP signal to process."
518; (interactive)
519; (unless (eshell-process-interact 'stop-process)
d783d303 520; (eshell-kill-process-function nil "stopped")))
36e81327
JW
521
522;(defun eshell-continue-process ()
523; "Send CONTINUE signal to process."
524; (interactive)
525; (unless (eshell-process-interact 'continue-process)
526; ;; jww (1999-09-17): this signal is not dealt with yet. For
527; ;; example, `eshell-reset' will be called, and so will
528; ;; `eshell-resume-eval'.
d783d303 529; (eshell-kill-process-function nil "continue")))
affbf647
GM
530
531(defun eshell-send-eof-to-process ()
532 "Send EOF to process."
533 (interactive)
534 (eshell-send-input nil nil t)
535 (eshell-process-interact 'process-send-eof))
536
affbf647 537;;; esh-proc.el ends here