* lisp/eshell: Minor fixes.
[emacs.git] / lisp / eshell / esh-proc.el
blob81ca2182488c2bd5fd31ccd92a5137b4fa6071ca
1 ;;; esh-proc.el --- process management
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 (provide 'esh-proc)
28 (eval-when-compile
29 (require 'eshell)
30 (require 'esh-util))
32 (defgroup eshell-proc nil
33 "When Eshell invokes external commands, it always does so
34 asynchronously, so that Emacs isn't tied up waiting for the process to
35 finish."
36 :tag "Process management"
37 :group 'eshell)
39 ;;; User Variables:
41 (defcustom eshell-proc-load-hook nil
42 "A hook that gets run when `eshell-proc' is loaded."
43 :version "24.1" ; removed eshell-proc-initialize
44 :type 'hook
45 :group 'eshell-proc)
47 (defcustom eshell-process-wait-seconds 0
48 "The number of seconds to delay waiting for a synchronous process."
49 :type 'integer
50 :group 'eshell-proc)
52 (defcustom eshell-process-wait-milliseconds 50
53 "The number of milliseconds to delay waiting for a synchronous process."
54 :type 'integer
55 :group 'eshell-proc)
57 (defcustom eshell-done-messages-in-minibuffer t
58 "If non-nil, subjob \"Done\" messages will display in minibuffer."
59 :type 'boolean
60 :group 'eshell-proc)
62 (defcustom eshell-delete-exited-processes t
63 "If nil, process entries will stick around until `jobs' is run.
64 This variable sets the buffer-local value of `delete-exited-processes'
65 in Eshell buffers.
67 This variable causes Eshell to mimic the behavior of bash when set to
68 nil. It allows the user to view the exit status of a completed subjob
69 \(process) at their leisure, because the process entry remains in
70 memory until the user examines it using \\[list-processes].
72 Otherwise, if `eshell-done-messages-in-minibuffer' is nil, and this
73 variable is set to t, the only indication the user will have that a
74 subjob is done is that it will no longer appear in the
75 \\[list-processes\\] display.
77 Note that Eshell will have to be restarted for a change in this
78 variable's value to take effect."
79 :type 'boolean
80 :group 'eshell-proc)
82 (defcustom eshell-reset-signals
83 "^\\(interrupt\\|killed\\|quit\\|stopped\\)"
84 "If a termination signal matches this regexp, the terminal will be reset."
85 :type 'regexp
86 :group 'eshell-proc)
88 (defcustom eshell-exec-hook nil
89 "Called each time a process is exec'd by `eshell-gather-process-output'.
90 It is passed one argument, which is the process that was just started.
91 It is useful for things that must be done each time a process is
92 executed in a eshell mode buffer (e.g., `process-kill-without-query').
93 In contrast, `eshell-mode-hook' is only executed once when the buffer
94 is created."
95 :type 'hook
96 :group 'eshell-proc)
98 (defcustom eshell-kill-hook nil
99 "Called when a process run by `eshell-gather-process-output' has ended.
100 It is passed two arguments: the process that was just ended, and the
101 termination status (as a string). Note that the first argument may be
102 nil, in which case the user attempted to send a signal, but there was
103 no relevant process. This can be used for displaying help
104 information, for example."
105 :version "24.1" ; removed eshell-reset-after-proc
106 :type 'hook
107 :group 'eshell-proc)
109 ;;; Internal Variables:
111 (defvar eshell-current-subjob-p nil)
113 (defvar eshell-process-list nil
114 "A list of the current status of subprocesses.")
116 ;;; Functions:
118 (defun eshell-kill-process-function (proc status)
119 "Function run when killing a process.
120 Runs `eshell-reset-after-proc' and `eshell-kill-hook', passing arguments
121 PROC and STATUS to both."
122 (or (memq 'eshell-reset-after-proc eshell-kill-hook)
123 (eshell-reset-after-proc proc status))
124 (run-hook-with-args 'eshell-kill-hook proc status))
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)
133 ; (define-key eshell-command-map [(control ?q)] 'eshell-continue-process)
134 (define-key eshell-command-map [(control ?s)] 'list-processes)
135 ; (define-key eshell-command-map [(control ?z)] 'eshell-stop-process)
136 (define-key eshell-command-map [(control ?\\)] 'eshell-quit-process))
138 (defun eshell-reset-after-proc (proc status)
139 "Reset the command input location after a process terminates.
140 The signals which will cause this to happen are matched by
141 `eshell-reset-signals'."
142 (if (and (stringp status)
143 (string-match eshell-reset-signals status))
144 (eshell-reset)))
146 (defun eshell-wait-for-process (&rest procs)
147 "Wait until PROC has successfully completed."
148 (while procs
149 (let ((proc (car procs)))
150 (when (eshell-processp proc)
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))))
159 (defalias 'eshell/wait 'eshell-wait-for-process)
161 (defun eshell/jobs (&rest args)
162 "List processes, if there are any."
163 (and (fboundp 'process-list)
164 (process-list)
165 (list-processes)))
167 (defun eshell/kill (&rest args)
168 "Kill processes.
169 Usage: kill [-<signal>] <pid>|<process> ...
170 Accepts 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))
198 (signal-process arg signum)))))
199 (setq args (cdr args))))
200 nil)
202 (defun eshell-read-process-name (prompt)
203 "Read the name of a process from the minibuffer, using completion.
204 The 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))
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) ">"))
219 (defsubst eshell-record-process-object (object)
220 "Record OBJECT as now running."
221 (if (and (eshell-processp object)
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)))
230 (defun eshell-remove-process-entry (entry)
231 "Record the process ENTRY as fully completed."
232 (if (and (eshell-processp (car entry))
233 (nth 2 entry)
234 eshell-done-messages-in-minibuffer)
235 (message "[%s]+ Done %s" (process-name (car entry))
236 (process-command (car entry))))
237 (setq eshell-process-list
238 (delq entry eshell-process-list)))
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.
244 Used only on systems which do not support async subprocesses.")
246 (defvar eshell-needs-pipe '("bc")
247 "List of commands which need `process-connection-type' to be nil.
248 Currently only affects commands in pipelines, and not those at
249 the front. If an element contains a directory part it must match
250 the full name of a command, otherwise just the nondirectory part must match.")
252 (defun eshell-needs-pipe-p (command)
253 "Return non-nil if COMMAND needs `process-connection-type' to be nil.
254 See `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))))))
266 (defun eshell-gather-process-output (command args)
267 "Gather the output from COMMAND + ARGS."
268 (unless (and (file-executable-p command)
269 (file-regular-p (file-truename command)))
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))
276 proc decoding encoding changed)
277 (cond
278 ((fboundp 'start-file-process)
279 (setq proc
280 (let ((process-connection-type
281 (unless (eshell-needs-pipe-p command)
282 process-connection-type))
283 (command (or (file-remote-p command 'localname) command)))
284 (apply 'start-file-process
285 (file-name-nondirectory command) nil
286 ;; `start-process' can't deal with relative filenames.
287 (append (list (expand-file-name command)) args))))
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))))
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))
355 (eshell-kill-process-function command exit-status)
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))))
361 proc))
363 (defun eshell-insertion-filter (proc string)
364 "Insert a string into the eshell buffer, or a process/file/buffer.
365 PROC is the process for which we're inserting output. STRING is the
366 output."
367 (when (buffer-live-p (process-buffer proc))
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)))))))))
381 (defun eshell-sentinel (proc string)
382 "Generic sentinel for command processes. Reports only signals.
383 PROC is the process that's exiting. STRING is the exit message."
384 (when (buffer-live-p (process-buffer proc))
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))))
400 (eshell-kill-process-function proc string)))))
402 (defun eshell-process-interact (func &optional all query)
403 "Interact with a process, using PROMPT if more than one, via FUNC.
404 If ALL is non-nil, background processes will be interacted with as well.
405 If QUERY is non-nil, query the user with QUERY before calling FUNC."
406 (let (defunct result)
407 (dolist (entry eshell-process-list)
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.
421 (dolist (d defunct)
422 (eshell-remove-process-entry d))
423 result))
425 (defcustom eshell-kill-process-wait-time 5
426 "Seconds to wait between sending termination signals to a subprocess."
427 :type 'integer
428 :group 'eshell-proc)
430 (defcustom eshell-kill-process-signals '(SIGINT SIGQUIT SIGKILL)
431 "Signals used to kill processes when an Eshell buffer exits.
432 Eshell calls each of these signals in order when an Eshell buffer is
433 killed; if the process is still alive afterwards, Eshell waits a
434 number of seconds defined by `eshell-kill-process-wait-time', and
435 tries the next signal in the list."
436 :type '(repeat symbol)
437 :group 'eshell-proc)
439 (defcustom eshell-kill-processes-on-exit nil
440 "If non-nil, kill active processes when exiting an Eshell buffer.
441 Emacs will only kill processes owned by that Eshell buffer.
443 If nil, ownership of background and foreground processes reverts to
444 Emacs itself, and will die only if the user exits Emacs, calls
445 `kill-process', or terminates the processes externally.
447 If `ask', Emacs prompts the user before killing any processes.
449 If `every', it prompts once for every process.
451 If t, it kills all buffer-owned processes without asking.
453 Processes are first sent SIGHUP, then SIGINT, then SIGQUIT, then
454 SIGKILL. The variable `eshell-kill-process-wait-time' specifies how
455 long 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)
462 (defun eshell-round-robin-kill (&optional query)
463 "Kill current process by trying various signals in sequence.
464 See 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))))))
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))))
494 (defun eshell-interrupt-process ()
495 "Interrupt a process."
496 (interactive)
497 (unless (eshell-process-interact 'interrupt-process)
498 (eshell-kill-process-function nil "interrupt")))
500 (defun eshell-kill-process ()
501 "Kill a process."
502 (interactive)
503 (unless (eshell-process-interact 'kill-process)
504 (eshell-kill-process-function nil "killed")))
506 (defun eshell-quit-process ()
507 "Send quit signal to process."
508 (interactive)
509 (unless (eshell-process-interact 'quit-process)
510 (eshell-kill-process-function nil "quit")))
512 ;(defun eshell-stop-process ()
513 ; "Send STOP signal to process."
514 ; (interactive)
515 ; (unless (eshell-process-interact 'stop-process)
516 ; (eshell-kill-process-function nil "stopped")))
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'.
525 ; (eshell-kill-process-function nil "continue")))
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))
533 ;;; esh-proc.el ends here