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