Mercurial > emacs
annotate lisp/eshell/esh-proc.el @ 35210:9c0daf298e13
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Wed, 10 Jan 2001 17:01:22 +0000 |
parents | 8e57189d61b4 |
children | f9277090a94a |
rev | line source |
---|---|
29876 | 1 ;;; esh-proc --- process management |
2 | |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
32526 | 5 ;; Author: John Wiegley <johnw@gnu.org> |
6 | |
29876 | 7 ;; This file is part of GNU Emacs. |
8 | |
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) | |
12 ;; any later version. | |
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 | |
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. | |
23 | |
24 (provide 'esh-proc) | |
25 | |
26 (eval-when-compile (require 'esh-maint)) | |
27 | |
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 | |
31 finish." | |
32 :tag "Process management" | |
33 :group 'eshell) | |
34 | |
35 ;;; Commentary: | |
36 | |
37 ;;; User Variables: | |
38 | |
39 (defcustom eshell-proc-load-hook '(eshell-proc-initialize) | |
40 "*A hook that gets run when `eshell-proc' is loaded." | |
41 :type 'hook | |
42 :group 'eshell-proc) | |
43 | |
44 (defcustom eshell-process-wait-seconds 0 | |
45 "*The number of seconds to delay waiting for a synchronous process." | |
46 :type 'integer | |
47 :group 'eshell-proc) | |
48 | |
49 (defcustom eshell-process-wait-milliseconds 50 | |
50 "*The number of milliseconds to delay waiting for a synchronous process." | |
51 :type 'integer | |
52 :group 'eshell-proc) | |
53 | |
54 (defcustom eshell-done-messages-in-minibuffer t | |
55 "*If non-nil, subjob \"Done\" messages will display in minibuffer." | |
56 :type 'boolean | |
57 :group 'eshell-proc) | |
58 | |
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' | |
62 in Eshell buffers. | |
63 | |
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]. | |
68 | |
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. | |
73 | |
74 Note that Eshell will have to be restarted for a change in this | |
75 variable's value to take effect." | |
76 :type 'boolean | |
77 :group 'eshell-proc) | |
78 | |
79 (defcustom eshell-reset-signals | |
80 "^\\(interrupt\\|killed\\|quit\\|stopped\\)" | |
81 "*If a termination signal matches this regexp, the terminal will be reset." | |
82 :type 'regexp | |
83 :group 'eshell-proc) | |
84 | |
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 | |
91 is created." | |
92 :type 'hook | |
93 :group 'eshell-proc) | |
94 | |
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." | |
102 :type 'hook | |
103 :group 'eshell-proc) | |
104 | |
105 ;;; Internal Variables: | |
106 | |
107 (defvar eshell-current-subjob-p nil) | |
108 | |
109 (defvar eshell-process-list nil | |
110 "A list of the current status of subprocesses.") | |
111 | |
112 ;;; Functions: | |
113 | |
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)) | |
125 | |
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'." | |
31241 | 130 (if (and (stringp status) |
131 (string-match eshell-reset-signals status)) | |
29876 | 132 (eshell-reset))) |
133 | |
134 (defun eshell-wait-for-process (&rest procs) | |
135 "Wait until PROC has successfully completed." | |
136 (while procs | |
137 (let ((proc (car procs))) | |
31241 | 138 (when (eshell-processp proc) |
29876 | 139 ;; NYI: If the process gets stopped here, that's bad. |
140 (while (assq proc eshell-process-list) | |
141 (if (input-pending-p) | |
142 (discard-input)) | |
143 (sit-for eshell-process-wait-seconds | |
144 eshell-process-wait-milliseconds)))) | |
145 (setq procs (cdr procs)))) | |
146 | |
147 (defalias 'eshell/wait 'eshell-wait-for-process) | |
148 | |
149 (defun eshell/jobs (&rest args) | |
150 "List processes, if there are any." | |
31241 | 151 (and (fboundp 'process-list) |
152 (process-list) | |
29876 | 153 (list-processes))) |
154 | |
155 (defun eshell/kill (&rest args) | |
156 "Kill processes, buffers, symbol or files." | |
157 (let ((ptr args) | |
158 (signum 'SIGINT)) | |
159 (while ptr | |
31241 | 160 (if (or (eshell-processp (car ptr)) |
29876 | 161 (and (stringp (car ptr)) |
162 (string-match "^[A-Za-z/][A-Za-z0-9<>/]+$" | |
163 (car ptr)))) | |
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))) | |
168 (while args | |
31241 | 169 (let ((id (if (eshell-processp (car args)) |
29876 | 170 (process-id (car args)) |
171 (car args)))) | |
172 (when id | |
173 (cond | |
174 ((null id) | |
175 (error "kill: bad signal spec")) | |
176 ((and (numberp id) (= id 0)) | |
177 (error "kill: bad signal spec `%d'" id)) | |
178 ((and (stringp id) | |
179 (string-match "^-?[0-9]+$" id)) | |
180 (setq signum (abs (string-to-number id)))) | |
181 ((stringp id) | |
182 (let (case-fold-search) | |
183 (if (string-match "^-\\([A-Z]+\\)$" id) | |
184 (setq signum | |
185 (intern (concat "SIG" (match-string 1 id)))) | |
186 (error "kill: bad signal spec `%s'" id)))) | |
187 ((< id 0) | |
188 (setq signum (abs id))) | |
189 (t | |
190 (signal-process id signum))))) | |
191 (setq args (cdr args))) | |
192 nil)) | |
193 | |
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 | |
198 (mapcar | |
199 (function | |
200 (lambda (proc) | |
201 (cons (process-name proc) t))) | |
202 (process-list)) nil t)) | |
203 | |
204 (defun eshell-insert-process (process) | |
205 "Insert the name of PROCESS into the current buffer at point." | |
206 (interactive | |
207 (list (get-process | |
208 (eshell-read-process-name "Name of process: ")))) | |
209 (insert-and-inherit "#<process " (process-name process) ">")) | |
210 | |
211 (defsubst eshell-record-process-object (object) | |
212 "Record OBJECT as now running." | |
31241 | 213 (if (and (eshell-processp object) |
29876 | 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))) | |
221 | |
222 (defun eshell-remove-process-entry (entry) | |
223 "Record the process ENTRY as fully completed." | |
31241 | 224 (if (and (eshell-processp (car entry)) |
29876 | 225 (nth 2 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))) | |
231 | |
31241 | 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.") | |
237 | |
29876 | 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)) | |
31241 | 248 proc decoding encoding changed) |
249 (cond | |
250 ((fboundp 'start-process) | |
251 (setq proc | |
252 (apply 'start-process | |
253 (file-name-nondirectory command) nil | |
254 ;; `start-process' can't deal with relative | |
255 ;; filenames | |
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 | |
271 ;; to LF. | |
272 (if (vectorp (coding-system-eol-type decoding)) | |
273 (setq decoding (coding-system-change-eol-conversion decoding 'dos) | |
274 changed t)) | |
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) | |
281 changed t)) | |
282 (if changed | |
283 (set-process-coding-system proc decoding encoding)))) | |
284 (t | |
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))) | |
291 (setq proc-buf | |
292 (set-buffer (get-buffer-create eshell-scratch-buffer))) | |
293 (erase-buffer) | |
294 (set-buffer oldbuf) | |
295 (run-hook-with-args 'eshell-exec-hook command) | |
296 (setq exit-status | |
297 (apply 'call-process-region | |
298 (append (list eshell-last-sync-output-start (point) | |
299 command t | |
300 eshell-scratch-buffer nil) | |
301 args))) | |
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)) | |
310 (setq lbeg (point)) | |
311 (while (eq 0 (forward-line 1)) | |
312 (setq lend (point) | |
313 line (buffer-substring-no-properties lbeg lend)) | |
314 (set-buffer oldbuf) | |
315 (if interact-p | |
316 (eshell-output-filter nil line) | |
317 (eshell-output-object line)) | |
318 (setq lbeg lend) | |
319 (set-buffer proc-buf)) | |
320 (set-buffer oldbuf)) | |
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)) | |
329 (setq proc t)))) | |
29876 | 330 proc)) |
331 | |
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 | |
335 output." | |
336 (when (buffer-live-p (process-buffer proc)) | |
337 (set-buffer (process-buffer proc)) | |
338 (let ((entry (assq proc eshell-process-list))) | |
339 (when entry | |
340 (setcar (nthcdr 3 entry) | |
341 (concat (nth 3 entry) string)) | |
342 (unless (nth 4 entry) ; already being handled? | |
343 (while (nth 3 entry) | |
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)))))))) | |
349 | |
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)) | |
355 (unwind-protect | |
356 (let* ((entry (assq proc eshell-process-list))) | |
357 ; (if (not entry) | |
358 ; (error "Sentinel called for unowned process `%s'" | |
359 ; (process-name proc)) | |
360 (when entry | |
361 (unwind-protect | |
362 (progn | |
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 | |
367 (cadr entry)))) | |
368 (eshell-remove-process-entry entry)))) | |
369 (run-hook-with-args 'eshell-kill-hook proc string)))) | |
370 | |
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)) | |
379 (or all | |
380 (not (nth 2 entry))) | |
381 (or (not query) | |
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)) | |
392 result)) | |
393 | |
394 (defcustom eshell-kill-process-wait-time 5 | |
395 "*Seconds to wait between sending termination signals to a subprocess." | |
396 :type 'integer | |
397 :group 'eshell-proc) | |
398 | |
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) | |
406 :group 'eshell-proc) | |
407 | |
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. | |
411 | |
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. | |
415 | |
416 If `ask', Emacs prompts the user before killing any processes. | |
417 | |
418 If `every', it prompts once for every process. | |
419 | |
420 If t, it kills all buffer-owned processes without asking. | |
421 | |
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)) | |
429 :group 'eshell-proc) | |
430 | |
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)) | |
435 (while sigs | |
436 (eshell-process-interact | |
437 (function | |
438 (lambda (proc) | |
439 (signal-process (process-id proc) (car sigs)))) t query) | |
440 (setq query nil) | |
441 (if (not eshell-process-list) | |
442 (setq sigs nil) | |
443 (sleep-for eshell-kill-process-wait-time) | |
444 (setq sigs (cdr sigs)))))) | |
445 | |
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 | |
449 eshell-process-list) | |
450 (save-window-excursion | |
451 (list-processes) | |
452 (if (or (not (eq eshell-kill-processes-on-exit 'ask)) | |
453 (y-or-n-p (format "Kill processes owned by `%s'? " | |
454 (buffer-name)))) | |
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)) | |
460 (kill-buffer buf))) | |
461 (message nil)))) | |
462 | |
463 (custom-add-option 'eshell-exit-hook 'eshell-query-kill-processes) | |
464 | |
465 (defun eshell-interrupt-process () | |
466 "Interrupt a process." | |
467 (interactive) | |
468 (unless (eshell-process-interact 'interrupt-process) | |
469 (run-hook-with-args 'eshell-kill-hook nil "interrupt"))) | |
470 | |
471 (defun eshell-kill-process () | |
472 "Kill a process." | |
473 (interactive) | |
474 (unless (eshell-process-interact 'kill-process) | |
475 (run-hook-with-args 'eshell-kill-hook nil "killed"))) | |
476 | |
477 (defun eshell-quit-process () | |
478 "Send quit signal to process." | |
479 (interactive) | |
480 (unless (eshell-process-interact 'quit-process) | |
481 (run-hook-with-args 'eshell-kill-hook nil "quit"))) | |
482 | |
483 (defun eshell-stop-process () | |
484 "Send STOP signal to process." | |
485 (interactive) | |
486 (unless (eshell-process-interact 'stop-process) | |
487 (run-hook-with-args 'eshell-kill-hook nil "stopped"))) | |
488 | |
489 (defun eshell-continue-process () | |
490 "Send CONTINUE signal to process." | |
491 (interactive) | |
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"))) | |
497 | |
498 (defun eshell-send-eof-to-process () | |
499 "Send EOF to process." | |
500 (interactive) | |
501 (eshell-send-input nil nil t) | |
502 (eshell-process-interact 'process-send-eof)) | |
503 | |
504 ;;; Code: | |
505 | |
506 ;;; esh-proc.el ends here |