1 ;;; esh-proc.el --- process management
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'esh-maint
))
28 (defgroup eshell-proc nil
29 "When Eshell invokes external commands, it always does so
30 asynchronously, so that Emacs isn't tied up waiting for the process to
32 :tag
"Process management"
39 (defcustom eshell-proc-load-hook
'(eshell-proc-initialize)
40 "*A hook that gets run when `eshell-proc' is loaded."
44 (defcustom eshell-process-wait-seconds
0
45 "*The number of seconds to delay waiting for a synchronous process."
49 (defcustom eshell-process-wait-milliseconds
50
50 "*The number of milliseconds to delay waiting for a synchronous process."
54 (defcustom eshell-done-messages-in-minibuffer t
55 "*If non-nil, subjob \"Done\" messages will display in minibuffer."
59 (defcustom eshell-delete-exited-processes t
60 "*If nil, process entries will stick around until `jobs' is run.
61 This variable sets the buffer-local value of `delete-exited-processes'
64 This variable causes Eshell to mimic the behavior of bash when set to
65 nil. It allows the user to view the exit status of a completed subjob
66 \(process) at their leisure, because the process entry remains in
67 memory until the user examines it using \\[list-processes].
69 Otherwise, if `eshell-done-messages-in-minibuffer' is nil, and this
70 variable is set to t, the only indication the user will have that a
71 subjob is done is that it will no longer appear in the
72 \\[list-processes\\] display.
74 Note that Eshell will have to be restarted for a change in this
75 variable's value to take effect."
79 (defcustom eshell-reset-signals
80 "^\\(interrupt\\|killed\\|quit\\|stopped\\)"
81 "*If a termination signal matches this regexp, the terminal will be reset."
85 (defcustom eshell-exec-hook nil
86 "*Called each time a process is exec'd by `eshell-gather-process-output'.
87 It is passed one argument, which is the process that was just started.
88 It is useful for things that must be done each time a process is
89 executed in a eshell mode buffer (e.g., `process-kill-without-query').
90 In contrast, `eshell-mode-hook' is only executed once when the buffer
95 (defcustom eshell-kill-hook
'(eshell-reset-after-proc)
96 "*Called when a process run by `eshell-gather-process-output' has ended.
97 It is passed two arguments: the process that was just ended, and the
98 termination status (as a string). Note that the first argument may be
99 nil, in which case the user attempted to send a signal, but there was
100 no relevant process. This can be used for displaying help
101 information, for example."
105 ;;; Internal Variables:
107 (defvar eshell-current-subjob-p nil
)
109 (defvar eshell-process-list nil
110 "A list of the current status of subprocesses.")
114 (defun eshell-proc-initialize ()
115 "Initialize the process handling code."
116 (make-local-variable 'eshell-process-list
)
117 (define-key eshell-command-map
[(meta ?i
)] 'eshell-insert-process
)
118 (define-key eshell-command-map
[(control ?c
)] 'eshell-interrupt-process
)
119 (define-key eshell-command-map
[(control ?k
)] 'eshell-kill-process
)
120 (define-key eshell-command-map
[(control ?d
)] 'eshell-send-eof-to-process
)
121 ; (define-key eshell-command-map [(control ?q)] 'eshell-continue-process)
122 (define-key eshell-command-map
[(control ?s
)] 'list-processes
)
123 ; (define-key eshell-command-map [(control ?z)] 'eshell-stop-process)
124 (define-key eshell-command-map
[(control ?
\\)] 'eshell-quit-process
))
126 (defun eshell-reset-after-proc (proc status
)
127 "Reset the command input location after a process terminates.
128 The signals which will cause this to happen are matched by
129 `eshell-reset-signals'."
130 (if (and (stringp status
)
131 (string-match eshell-reset-signals status
))
134 (defun eshell-wait-for-process (&rest procs
)
135 "Wait until PROC has successfully completed."
137 (let ((proc (car procs
)))
138 (when (eshell-processp proc
)
139 ;; NYI: If the process gets stopped here, that's bad.
140 (while (assq proc eshell-process-list
)
141 (if (input-pending-p)
143 (sit-for eshell-process-wait-seconds
144 eshell-process-wait-milliseconds
))))
145 (setq procs
(cdr procs
))))
147 (defalias 'eshell
/wait
'eshell-wait-for-process
)
149 (defun eshell/jobs
(&rest args
)
150 "List processes, if there are any."
151 (and (fboundp 'process-list
)
155 (defun eshell/kill
(&rest args
)
156 "Kill processes, buffers, symbol or files."
160 (if (or (eshell-processp (car ptr
))
161 (and (stringp (car ptr
))
162 (string-match "^[A-Za-z/][A-Za-z0-9<>/]+$"
164 ;; What about when $lisp-variable is possible here?
165 ;; It could very well name a process.
166 (setcar ptr
(get-process (car ptr
))))
167 (setq ptr
(cdr ptr
)))
169 (let ((id (if (eshell-processp (car args
))
170 (process-id (car args
))
175 (error "kill: bad signal spec"))
176 ((and (numberp id
) (= id
0))
177 (error "kill: bad signal spec `%d'" id
))
179 (string-match "^-?[0-9]+$" id
))
180 (setq signum
(abs (string-to-number id
))))
182 (let (case-fold-search)
183 (if (string-match "^-\\([A-Z]+\\)$" id
)
185 (intern (concat "SIG" (match-string 1 id
))))
186 (error "kill: bad signal spec `%s'" id
))))
188 (setq signum
(abs id
)))
190 (signal-process id signum
)))))
191 (setq args
(cdr args
)))
194 (defun eshell-read-process-name (prompt)
195 "Read the name of a process from the minibuffer, using completion.
196 The prompt will be set to PROMPT."
197 (completing-read prompt
201 (cons (process-name proc
) t
)))
202 (process-list)) nil t
))
204 (defun eshell-insert-process (process)
205 "Insert the name of PROCESS into the current buffer at point."
208 (eshell-read-process-name "Name of process: "))))
209 (insert-and-inherit "#<process " (process-name process
) ">"))
211 (defsubst eshell-record-process-object
(object)
212 "Record OBJECT as now running."
213 (if (and (eshell-processp object
)
214 eshell-current-subjob-p
)
215 (eshell-interactive-print
216 (format "[%s] %d\n" (process-name object
) (process-id object
))))
217 (setq eshell-process-list
218 (cons (list object eshell-current-handles
219 eshell-current-subjob-p nil nil
)
220 eshell-process-list
)))
222 (defun eshell-remove-process-entry (entry)
223 "Record the process ENTRY as fully completed."
224 (if (and (eshell-processp (car entry
))
226 eshell-done-messages-in-minibuffer
)
227 (message (format "[%s]+ Done %s" (process-name (car entry
))
228 (process-command (car entry
)))))
229 (setq eshell-process-list
230 (delq entry eshell-process-list
)))
232 (defvar eshell-scratch-buffer
" *eshell-scratch*"
233 "Scratch buffer for holding Eshell's input/output.")
234 (defvar eshell-last-sync-output-start nil
235 "A marker that tracks the beginning of output of the last subprocess.
236 Used only on systems which do not support async subprocesses.")
238 (defun eshell-gather-process-output (command args
)
239 "Gather the output from COMMAND + ARGS."
240 (unless (and (file-executable-p command
)
241 (file-regular-p command
))
242 (error "%s: not an executable file" command
))
243 (let* ((delete-exited-processes
244 (if eshell-current-subjob-p
245 eshell-delete-exited-processes
246 delete-exited-processes
))
247 (process-environment (eshell-environment-variables))
248 proc decoding encoding changed
)
250 ((fboundp 'start-process
)
252 (apply 'start-process
253 (file-name-nondirectory command
) nil
254 ;; `start-process' can't deal with relative
256 (append (list (expand-file-name command
)) args
)))
257 (eshell-record-process-object proc
)
258 (set-process-buffer proc
(current-buffer))
259 (if (eshell-interactive-output-p)
260 (set-process-filter proc
'eshell-output-filter
)
261 (set-process-filter proc
'eshell-insertion-filter
))
262 (set-process-sentinel proc
'eshell-sentinel
)
263 (run-hook-with-args 'eshell-exec-hook proc
)
264 (when (fboundp 'process-coding-system
)
265 (let ((coding-systems (process-coding-system proc
)))
266 (setq decoding
(car coding-systems
)
267 encoding
(cdr coding-systems
)))
268 ;; If start-process decided to use some coding system for
269 ;; decoding data sent from the process and the coding system
270 ;; doesn't specify EOL conversion, we had better convert CRLF
272 (if (vectorp (coding-system-eol-type decoding
))
273 (setq decoding
(coding-system-change-eol-conversion decoding
'dos
)
275 ;; Even if start-process left the coding system for encoding
276 ;; data sent from the process undecided, we had better use the
277 ;; same one as what we use for decoding. But, we should
278 ;; suppress EOL conversion.
279 (if (and decoding
(not encoding
))
280 (setq encoding
(coding-system-change-eol-conversion decoding
'unix
)
283 (set-process-coding-system proc decoding encoding
))))
285 ;; No async subprocesses...
286 (let ((oldbuf (current-buffer))
287 (interact-p (eshell-interactive-output-p))
288 lbeg lend line proc-buf exit-status
)
289 (and (not (markerp eshell-last-sync-output-start
))
290 (setq eshell-last-sync-output-start
(point-marker)))
292 (set-buffer (get-buffer-create eshell-scratch-buffer
)))
295 (run-hook-with-args 'eshell-exec-hook command
)
297 (apply 'call-process-region
298 (append (list eshell-last-sync-output-start
(point)
300 eshell-scratch-buffer nil
)
302 ;; When in a pipeline, record the place where the output of
303 ;; this process will begin.
304 (and eshell-in-pipeline-p
305 (set-marker eshell-last-sync-output-start
(point)))
306 ;; Simulate the effect of the process filter.
307 (when (numberp exit-status
)
308 (set-buffer proc-buf
)
309 (goto-char (point-min))
311 (while (eq 0 (forward-line 1))
313 line
(buffer-substring-no-properties lbeg lend
))
316 (eshell-output-filter nil line
)
317 (eshell-output-object line
))
319 (set-buffer proc-buf
))
321 (eshell-update-markers eshell-last-output-end
)
322 ;; Simulate the effect of eshell-sentinel.
323 (eshell-close-handles (if (numberp exit-status
) exit-status -
1))
324 (run-hook-with-args 'eshell-kill-hook command exit-status
)
325 (or eshell-in-pipeline-p
326 (setq eshell-last-sync-output-start nil
))
327 (if (not (numberp exit-status
))
328 (error "%s: external command failed: %s" command exit-status
))
332 (defun eshell-insertion-filter (proc string
)
333 "Insert a string into the eshell buffer, or a process/file/buffer.
334 PROC is the process for which we're inserting output. STRING is the
336 (when (buffer-live-p (process-buffer proc
))
337 (set-buffer (process-buffer proc
))
338 (let ((entry (assq proc eshell-process-list
)))
340 (setcar (nthcdr 3 entry
)
341 (concat (nth 3 entry
) string
))
342 (unless (nth 4 entry
) ; already being handled?
344 (let ((data (nth 3 entry
)))
345 (setcar (nthcdr 3 entry
) nil
)
346 (setcar (nthcdr 4 entry
) t
)
347 (eshell-output-object data nil
(cadr entry
))
348 (setcar (nthcdr 4 entry
) nil
))))))))
350 (defun eshell-sentinel (proc string
)
351 "Generic sentinel for command processes. Reports only signals.
352 PROC is the process that's exiting. STRING is the exit message."
353 (when (buffer-live-p (process-buffer proc
))
354 (set-buffer (process-buffer proc
))
356 (let* ((entry (assq proc eshell-process-list
)))
358 ; (error "Sentinel called for unowned process `%s'"
359 ; (process-name proc))
363 (unless (string= string
"run")
364 (unless (string-match "^\\(finished\\|exited\\)" string
)
365 (eshell-insertion-filter proc string
))
366 (eshell-close-handles (process-exit-status proc
) 'nil
368 (eshell-remove-process-entry entry
))))
369 (run-hook-with-args 'eshell-kill-hook proc string
))))
371 (defun eshell-process-interact (func &optional all query
)
372 "Interact with a process, using PROMPT if more than one, via FUNC.
373 If ALL is non-nil, background processes will be interacted with as well.
374 If QUERY is non-nil, query the user with QUERY before calling FUNC."
375 (let (defunct result
)
376 (eshell-for entry eshell-process-list
377 (if (and (memq (process-status (car entry
))
378 '(run stop open closed
))
382 (y-or-n-p (format query
(process-name (car entry
))))))
383 (setq result
(funcall func
(car entry
))))
384 (unless (memq (process-status (car entry
))
385 '(run stop open closed
))
386 (setq defunct
(cons entry defunct
))))
387 ;; clean up the process list; this can get dirty if an error
388 ;; occurred that brought the user into the debugger, and then they
389 ;; quit, so that the sentinel was never called.
390 (eshell-for d defunct
391 (eshell-remove-process-entry d
))
394 (defcustom eshell-kill-process-wait-time
5
395 "*Seconds to wait between sending termination signals to a subprocess."
399 (defcustom eshell-kill-process-signals
'(SIGINT SIGQUIT SIGKILL
)
400 "*Signals used to kill processes when an Eshell buffer exits.
401 Eshell calls each of these signals in order when an Eshell buffer is
402 killed; if the process is still alive afterwards, Eshell waits a
403 number of seconds defined by `eshell-kill-process-wait-time', and
404 tries the next signal in the list."
405 :type
'(repeat symbol
)
408 (defcustom eshell-kill-processes-on-exit nil
409 "*If non-nil, kill active processes when exiting an Eshell buffer.
410 Emacs will only kill processes owned by that Eshell buffer.
412 If nil, ownership of background and foreground processes reverts to
413 Emacs itself, and will die only if the user exits Emacs, calls
414 `kill-process', or terminates the processes externally.
416 If `ask', Emacs prompts the user before killing any processes.
418 If `every', it prompts once for every process.
420 If t, it kills all buffer-owned processes without asking.
422 Processes are first sent SIGHUP, then SIGINT, then SIGQUIT, then
423 SIGKILL. The variable `eshell-kill-process-wait-time' specifies how
424 long to delay between signals."
425 :type
'(choice (const :tag
"Kill all, don't ask" t
)
426 (const :tag
"Ask before killing" ask
)
427 (const :tag
"Ask for each process" every
)
428 (const :tag
"Don't kill subprocesses" nil
))
431 (defun eshell-round-robin-kill (&optional query
)
432 "Kill current process by trying various signals in sequence.
433 See the variable `eshell-kill-processes-on-exit'."
434 (let ((sigs eshell-kill-process-signals
))
436 (eshell-process-interact
439 (signal-process (process-id proc
) (car sigs
)))) t query
)
441 (if (not eshell-process-list
)
443 (sleep-for eshell-kill-process-wait-time
)
444 (setq sigs
(cdr sigs
))))))
446 (defun eshell-query-kill-processes ()
447 "Kill processes belonging to the current Eshell buffer, possibly w/ query."
448 (when (and eshell-kill-processes-on-exit
450 (save-window-excursion
452 (if (or (not (eq eshell-kill-processes-on-exit
'ask
))
453 (y-or-n-p (format "Kill processes owned by `%s'? "
455 (eshell-round-robin-kill
456 (if (eq eshell-kill-processes-on-exit
'every
)
457 "Kill Eshell child process `%s'? ")))
458 (let ((buf (get-buffer "*Process List*")))
459 (if (and buf
(buffer-live-p buf
))
463 (custom-add-option 'eshell-exit-hook
'eshell-query-kill-processes
)
465 (defun eshell-interrupt-process ()
466 "Interrupt a process."
468 (unless (eshell-process-interact 'interrupt-process
)
469 (run-hook-with-args 'eshell-kill-hook nil
"interrupt")))
471 (defun eshell-kill-process ()
474 (unless (eshell-process-interact 'kill-process
)
475 (run-hook-with-args 'eshell-kill-hook nil
"killed")))
477 (defun eshell-quit-process ()
478 "Send quit signal to process."
480 (unless (eshell-process-interact 'quit-process
)
481 (run-hook-with-args 'eshell-kill-hook nil
"quit")))
483 ;(defun eshell-stop-process ()
484 ; "Send STOP signal to process."
486 ; (unless (eshell-process-interact 'stop-process)
487 ; (run-hook-with-args 'eshell-kill-hook nil "stopped")))
489 ;(defun eshell-continue-process ()
490 ; "Send CONTINUE signal to process."
492 ; (unless (eshell-process-interact 'continue-process)
493 ; ;; jww (1999-09-17): this signal is not dealt with yet. For
494 ; ;; example, `eshell-reset' will be called, and so will
495 ; ;; `eshell-resume-eval'.
496 ; (run-hook-with-args 'eshell-kill-hook nil "continue")))
498 (defun eshell-send-eof-to-process ()
499 "Send EOF to process."
501 (eshell-send-input nil nil t
)
502 (eshell-process-interact 'process-send-eof
))
506 ;;; arch-tag: ac477a3e-ee4d-4b44-8ec6-212010e607bb
507 ;;; esh-proc.el ends here