comparison lispref/processes.texi @ 6558:fa8ff07eaafc

Initial revision
author Richard M. Stallman <rms@gnu.org>
date Mon, 28 Mar 1994 20:21:44 +0000
parents
children 9a9e88e65617
comparison
equal deleted inserted replaced
6557:74758cf67338 6558:fa8ff07eaafc
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/processes
6 @node Processes, System Interface, Abbrevs, Top
7 @chapter Processes
8 @cindex child process
9 @cindex parent process
10 @cindex subprocess
11 @cindex process
12
13 In the terminology of operating systems, a @dfn{process} is a space in
14 which a program can execute. Emacs runs in a process. Emacs Lisp
15 programs can invoke other programs in processes of their own. These are
16 called @dfn{subprocesses} or @dfn{child processes} of the Emacs process,
17 which is their @dfn{parent process}.
18
19 A subprocess of Emacs may be @dfn{synchronous} or @dfn{asynchronous},
20 depending on how it is created. When you create a synchronous
21 subprocess, the Lisp program waits for the subprocess to terminate
22 before continuing execution. When you create an asynchronous
23 subprocess, it can run in parallel with the Lisp program. This kind of
24 subprocess is represented within Emacs by a Lisp object which is also
25 called a ``process''. Lisp programs can use this object to communicate
26 with the subprocess or to control it. For example, you can send
27 signals, obtain status information, receive output from the process, or
28 send input to it.
29
30 @defun processp object
31 This function returns @code{t} if @var{object} is a process,
32 @code{nil} otherwise.
33 @end defun
34
35 @menu
36 * Subprocess Creation:: Functions that start subprocesses.
37 * Synchronous Processes:: Details of using synchronous subprocesses.
38 * Asynchronous Processes:: Starting up an asynchronous subprocess.
39 * Deleting Processes:: Eliminating an asynchronous subprocess.
40 * Process Information:: Accessing run-status and other attributes.
41 * Input to Processes:: Sending input to an asynchronous subprocess.
42 * Signals to Processes:: Stopping, continuing or interrupting
43 an asynchronous subprocess.
44 * Output from Processes:: Collecting output from an asynchronous subprocess.
45 * Sentinels:: Sentinels run when process run-status changes.
46 * Transaction Queues:: Transaction-based communication with subprocesses.
47 * TCP:: Opening network connections.
48 @end menu
49
50 @node Subprocess Creation
51 @section Functions that Create Subprocesses
52
53 There are three functions that create a new subprocess in which to run
54 a program. One of them, @code{start-process}, creates an asynchronous
55 process and returns a process object (@pxref{Asynchronous Processes}).
56 The other two, @code{call-process} and @code{call-process-region},
57 create a synchronous process and do not return a process object
58 (@pxref{Synchronous Processes}).
59
60 Synchronous and asynchronous processes are explained in following
61 sections. Since the three functions are all called in a similar
62 fashion, their common arguments are described here.
63
64 @cindex execute program
65 @cindex @code{PATH} environment variable
66 @cindex @code{HOME} environment variable
67 In all cases, the function's @var{program} argument specifies the
68 program to be run. An error is signaled if the file is not found or
69 cannot be executed. If the file name is relative, the variable
70 @code{exec-path} contains a list of directories to search. Emacs
71 initializes @code{exec-path} when it starts up, based on the value of
72 the environment variable @code{PATH}. The standard file name
73 constructs, @samp{~}, @samp{.}, and @samp{..}, are interpreted as usual
74 in @code{exec-path}, but environment variable substitutions
75 (@samp{$HOME}, etc.) are not recognized; use
76 @code{substitute-in-file-name} to perform them (@pxref{File Name
77 Expansion}).
78
79 Each of the subprocess-creating functions has a @var{buffer-or-name}
80 argument which specifies where the standard output from the program will
81 go. If @var{buffer-or-name} is @code{nil}, that says to discard the
82 output unless a filter function handles it. (@xref{Filter Functions},
83 and @ref{Streams}.) Normally, you should avoid having multiple
84 processes send output to the same buffer because their output would be
85 intermixed randomly.
86
87 @cindex program arguments
88 All three of the subprocess-creating functions have a @code{&rest}
89 argument, @var{args}. The @var{args} must all be strings, and they are
90 supplied to @var{program} as separate command line arguments. Wildcard
91 characters and other shell constructs are not allowed in these strings,
92 since they are passed directly to the specified program.
93
94 @strong{Please note:} the argument @var{program} contains only the
95 name of the program; it may not contain any command-line arguments. You
96 must use @var{args} to provide those.
97
98 The subprocess gets its current directory from the value of
99 @code{default-directory} (@pxref{File Name Expansion}).
100
101 @cindex environment variables, subprocesses
102 The subprocess inherits its environment from Emacs; but you can
103 specify overrides for it with @code{process-environment}. @xref{System
104 Environment}.
105
106 @defvar exec-directory
107 @pindex wakeup
108 The value of this variable is the name of a directory (a string) that
109 contains programs that come with GNU Emacs, that are intended for Emacs
110 to invoke. The program @code{wakeup} is an example of such a program;
111 the @code{display-time} command uses it to get a reminder once per
112 minute.
113 @end defvar
114
115 @defopt exec-path
116 The value of this variable is a list of directories to search for
117 programs to run in subprocesses. Each element is either the name of a
118 directory (i.e., a string), or @code{nil}, which stands for the default
119 directory (which is the value of @code{default-directory}).
120 @cindex program directories
121
122 The value of @code{exec-path} is used by @code{call-process} and
123 @code{start-process} when the @var{program} argument is not an absolute
124 file name.
125 @end defopt
126
127 @node Synchronous Processes
128 @section Creating a Synchronous Process
129 @cindex synchronous subprocess
130
131 After a @dfn{synchronous process} is created, Emacs waits for the
132 process to terminate before continuing. Starting Dired is an example of
133 this: it runs @code{ls} in a synchronous process, then modifies the
134 output slightly. Because the process is synchronous, the entire
135 directory listing arrives in the buffer before Emacs tries to do
136 anything with it.
137
138 While Emacs waits for the synchronous subprocess to terminate, the
139 user can quit by typing @kbd{C-g}. The first @kbd{C-g} tries to kill
140 the subprocess with a @code{SIGINT} signal; but it waits until the
141 subprocess actually terminates before quitting. If during that time the
142 user types another @kbd{C-g}, that kills the subprocess instantly with
143 @code{SIGKILL} and quits immediately. @xref{Quitting}.
144
145 The synchronous subprocess functions returned @code{nil} in version
146 18. In version 19, they return an indication of how the process
147 terminated.
148
149 @defun call-process program &optional infile buffer-or-name display &rest args
150 This function calls @var{program} in a separate process and waits for
151 it to finish.
152
153 The standard input for the process comes from file @var{infile} if
154 @var{infile} is not @code{nil} and from @file{/dev/null} otherwise. The
155 process output gets inserted in buffer @var{buffer-or-name} before point,
156 if that argument names a buffer. If @var{buffer-or-name} is @code{t},
157 output is sent to the current buffer; if @var{buffer-or-name} is
158 @code{nil}, output is discarded.
159
160 If @var{buffer-or-name} is the integer 0, @code{call-process} returns
161 @code{nil} immediately and discards any output. In this case, the
162 process is not truly synchronous, since it can run in parallel with
163 Emacs; but you can think of it as synchronous in that Emacs is
164 essentially finished with the subprocess as soon as this function
165 returns.
166
167 If @var{display} is non-@code{nil}, then @code{call-process} redisplays
168 the buffer as output is inserted. Otherwise the function does no
169 redisplay, and the results become visible on the screen only when Emacs
170 redisplays that buffer in the normal course of events.
171
172 The remaining arguments, @var{args}, are strings that specify command
173 line arguments for the program.
174
175 The value returned by @code{call-process} (unless you told it not to
176 wait) indicates the reason for process termination. A number gives the
177 exit status of the subprocess; 0 means success, and any other value
178 means failure. If the process terminated with a signal,
179 @code{call-process} returns a string describing the signal.
180
181 In the examples below, the buffer @samp{foo} is current.
182
183 @smallexample
184 @group
185 (call-process "pwd" nil t)
186 @result{} nil
187
188 ---------- Buffer: foo ----------
189 /usr/user/lewis/manual
190 ---------- Buffer: foo ----------
191 @end group
192
193 @group
194 (call-process "grep" nil "bar" nil "lewis" "/etc/passwd")
195 @result{} nil
196
197 ---------- Buffer: bar ----------
198 lewis:5LTsHm66CSWKg:398:21:Bil Lewis:/user/lewis:/bin/csh
199
200 ---------- Buffer: bar ----------
201 @end group
202 @end smallexample
203
204 The @code{insert-directory} function contains a good example of the use
205 of @code{call-process}:
206
207 @smallexample
208 @group
209 (call-process insert-directory-program nil t nil switches
210 (if full-directory-p
211 (concat (file-name-as-directory file) ".")
212 file))
213 @end group
214 @end smallexample
215 @end defun
216
217 @defun call-process-region start end program &optional delete buffer-or-name display &rest args
218 This function sends the text between @var{start} to @var{end} as
219 standard input to a process running @var{program}. It deletes the text
220 sent if @var{delete} is non-@code{nil}; this is useful when @var{buffer}
221 is @code{t}, to insert the output in the current buffer.
222
223 The arguments @var{buffer-or-name} and @var{display} control what to do
224 with the output from the subprocess, and whether to update the display
225 as it comes in. For details, see the description of
226 @code{call-process}, above. If @var{buffer-or-name} is the integer 0,
227 @code{call-process-region} discards the output and returns @code{nil}
228 immediately, without waiting for the subprocess to finish.
229
230 The remaining arguments, @var{args}, are strings that specify command
231 line arguments for the program.
232
233 The return value of @code{call-process-region} is just like that of
234 @code{call-process}: @code{nil} if you told it to return without
235 waiting; otherwise, a number or string which indicates how the
236 subprocess terminated.
237
238 In the following example, we use @code{call-process-region} to run the
239 @code{cat} utility, with standard input being the first five characters
240 in buffer @samp{foo} (the word @samp{input}). @code{cat} copies its
241 standard input into its standard output. Since the argument
242 @var{buffer-or-name} is @code{t}, this output is inserted in the current
243 buffer.
244
245 @smallexample
246 @group
247 ---------- Buffer: foo ----------
248 input@point{}
249 ---------- Buffer: foo ----------
250 @end group
251
252 @group
253 (call-process-region 1 6 "cat" nil t)
254 @result{} nil
255
256 ---------- Buffer: foo ----------
257 inputinput@point{}
258 ---------- Buffer: foo ----------
259 @end group
260 @end smallexample
261
262 The @code{shell-command-on-region} command uses
263 @code{call-process-region} like this:
264
265 @smallexample
266 @group
267 (call-process-region
268 start end
269 shell-file-name ; @r{Name of program.}
270 nil ; @r{Do not delete region.}
271 buffer ; @r{Send output to @code{buffer}.}
272 nil ; @r{No redisplay during output.}
273 "-c" command) ; @r{Arguments for the shell.}
274 @end group
275 @end smallexample
276 @end defun
277
278 @node Asynchronous Processes
279 @section Creating an Asynchronous Process
280 @cindex asynchronous subprocess
281
282 After an @dfn{asynchronous process} is created, Emacs and the Lisp
283 program both continue running immediately. The process may thereafter
284 run in parallel with Emacs, and the two may communicate with each other
285 using the functions described in following sections. Here we describe
286 how to create an asynchronous process with @code{start-process}.
287
288 @defun start-process name buffer-or-name program &rest args
289 This function creates a new asynchronous subprocess and starts the
290 program @var{program} running in it. It returns a process object that
291 stands for the new subprocess in Lisp. The argument @var{name}
292 specifies the name for the process object; if a process with this name
293 already exists, then @var{name} is modified (by adding @samp{<1>}, etc.)
294 to be unique. The buffer @var{buffer-or-name} is the buffer to
295 associate with the process.
296
297 The remaining arguments, @var{args}, are strings that specify command
298 line arguments for the program.
299
300 In the example below, the first process is started and runs (rather,
301 sleeps) for 100 seconds. Meanwhile, the second process is started, and
302 given the name @samp{my-process<1>} for the sake of uniqueness. It
303 inserts the directory listing at the end of the buffer @samp{foo},
304 before the first process finishes. Then it finishes, and a message to
305 that effect is inserted in the buffer. Much later, the first process
306 finishes, and another message is inserted in the buffer for it.
307
308 @smallexample
309 @group
310 (start-process "my-process" "foo" "sleep" "100")
311 @result{} #<process my-process>
312 @end group
313
314 @group
315 (start-process "my-process" "foo" "ls" "-l" "/user/lewis/bin")
316 @result{} #<process my-process<1>>
317
318 ---------- Buffer: foo ----------
319 total 2
320 lrwxrwxrwx 1 lewis 14 Jul 22 10:12 gnuemacs --> /emacs
321 -rwxrwxrwx 1 lewis 19 Jul 30 21:02 lemon
322
323 Process my-process<1> finished
324
325 Process my-process finished
326 ---------- Buffer: foo ----------
327 @end group
328 @end smallexample
329 @end defun
330
331 @defun start-process-shell-command name buffer-or-name command &rest command-args
332 This function is like @code{start-process} except that it uses a shell
333 to execute the specified command. The argument @var{command} is a shell
334 command name, and @var{command-args} are the arguments for the shell
335 command.
336 @end defun
337
338 @defvar process-connection-type
339 @cindex pipes
340 @cindex @sc{pty}s
341 This variable controls the type of device used to communicate with
342 asynchronous subprocesses. If it is @code{nil}, then pipes are used.
343 If it is @code{t}, then @sc{pty}s are used (or pipes if @sc{pty}s are
344 not supported).
345
346 @sc{pty}s are usually preferable for processes visible to the user, as
347 in Shell mode, because they allow job control (@kbd{C-c}, @kbd{C-z},
348 etc.) to work between the process and its children whereas pipes do not.
349 For subprocesses used for internal purposes by programs, it is often
350 better to use a pipe, because they are more efficient. In addition, the
351 total number of @sc{pty}s is limited on many systems and it is good not
352 to waste them.
353
354 The value @code{process-connection-type} is used when
355 @code{start-process} is called. So you can specify how to communicate
356 with one subprocess by binding the variable around the call to
357 @code{start-process}.
358
359 @smallexample
360 @group
361 (let ((process-connection-type nil)) ; @r{Use a pipe.}
362 (start-process @dots{}))
363 @end group
364 @end smallexample
365 @end defvar
366
367 @node Deleting Processes
368 @section Deleting Processes
369 @cindex deleting processes
370
371 @dfn{Deleting a process} disconnects Emacs immediately from the
372 subprocess, and removes it from the list of active processes. It sends
373 a signal to the subprocess to make the subprocess terminate, but this is
374 not guaranteed to happen immediately. The process object itself
375 continues to exist as long as other Lisp objects point to it.
376
377 You can delete a process explicitly at any time. Processes are
378 deleted automatically after they terminate, but not necessarily right
379 away. If you delete a terminated process explicitly before it is
380 deleted automatically, no harm results.
381
382 @defvar delete-exited-processes
383 This variable controls automatic deletion of processes that have
384 terminated (due to calling @code{exit} or to a signal). If it is
385 @code{nil}, then they continue to exist until the user runs
386 @code{list-processes}. Otherwise, they are deleted immediately after
387 they exit.
388 @end defvar
389
390 @defun delete-process name
391 This function deletes the process associated with @var{name}, killing it
392 with a @code{SIGHUP} signal. The argument @var{name} may be a process,
393 the name of a process, a buffer, or the name of a buffer.
394
395 @smallexample
396 @group
397 (delete-process "*shell*")
398 @result{} nil
399 @end group
400 @end smallexample
401 @end defun
402
403 @defun process-kill-without-query process
404 This function declares that Emacs need not query the user if
405 @var{process} is still running when Emacs is exited. The process will
406 be deleted silently. The value is @code{t}.
407
408 @smallexample
409 @group
410 (process-kill-without-query (get-process "shell"))
411 @result{} t
412 @end group
413 @end smallexample
414 @end defun
415
416 @node Process Information
417 @section Process Information
418
419 Several functions return information about processes.
420 @code{list-processes} is provided for interactive use.
421
422 @deffn Command list-processes
423 This command displays a listing of all living processes. In addition,
424 it finally deletes any process whose status was @samp{Exited} or
425 @samp{Signaled}. It returns @code{nil}.
426 @end deffn
427
428 @defun process-list
429 This function returns a list of all processes that have not been deleted.
430
431 @smallexample
432 @group
433 (process-list)
434 @result{} (#<process display-time> #<process shell>)
435 @end group
436 @end smallexample
437 @end defun
438
439 @defun get-process name
440 This function returns the process named @var{name}, or @code{nil} if
441 there is none. An error is signaled if @var{name} is not a string.
442
443 @smallexample
444 @group
445 (get-process "shell")
446 @result{} #<process shell>
447 @end group
448 @end smallexample
449 @end defun
450
451 @defun process-command process
452 This function returns the command that was executed to start
453 @var{process}. This is a list of strings, the first string being the
454 program executed and the rest of the strings being the arguments that
455 were given to the program.
456
457 @smallexample
458 @group
459 (process-command (get-process "shell"))
460 @result{} ("/bin/csh" "-i")
461 @end group
462 @end smallexample
463 @end defun
464
465 @defun process-id process
466 This function returns the @sc{pid} of @var{process}. This is an
467 integer which distinguishes the process @var{process} from all other
468 processes running on the same computer at the current time. The
469 @sc{pid} of a process is chosen by the operating system kernel when the
470 process is started and remains constant as long as the process exists.
471 @end defun
472
473 @defun process-name process
474 This function returns the name of @var{process}.
475 @end defun
476
477 @defun process-status process-name
478 This function returns the status of @var{process-name} as a symbol.
479 The argument @var{process-name} must be a process, a buffer, a
480 process name (string) or a buffer name (string).
481
482 The possible values for an actual subprocess are:
483
484 @table @code
485 @item run
486 for a process that is running.
487 @item stop
488 for a process that is stopped but continuable.
489 @item exit
490 for a process that has exited.
491 @item signal
492 for a process that has received a fatal signal.
493 @item open
494 for a network connection that is open.
495 @item closed
496 for a network connection that is closed. Once a connection
497 is closed, you cannot reopen it, though you might be able to open
498 a new connection to the same place.
499 @item nil
500 if @var{process-name} is not the name of an existing process.
501 @end table
502
503 @smallexample
504 @group
505 (process-status "shell")
506 @result{} run
507 @end group
508 @group
509 (process-status (get-buffer "*shell*"))
510 @result{} run
511 @end group
512 @group
513 x
514 @result{} #<process xx<1>>
515 (process-status x)
516 @result{} exit
517 @end group
518 @end smallexample
519
520 For a network connection, @code{process-status} returns one of the symbols
521 @code{open} or @code{closed}. The latter means that the other side
522 closed the connection, or Emacs did @code{delete-process}.
523
524 In earlier Emacs versions (prior to version 19), the status of a network
525 connection was @code{run} if open, and @code{exit} if closed.
526 @end defun
527
528 @defun process-exit-status process
529 This function returns the exit status of @var{process} or the signal
530 number that killed it. (Use the result of @code{process-status} to
531 determine which of those it is.) If @var{process} has not yet
532 terminated, the value is 0.
533 @end defun
534
535 @node Input to Processes
536 @section Sending Input to Processes
537 @cindex process input
538
539 Asynchronous subprocesses receive input when it is sent to them by
540 Emacs, which is done with the functions in this section. You must
541 specify the process to send input to, and the input data to send. The
542 data appears on the ``standard input'' of the subprocess.
543
544 Some operating systems have limited space for buffered input in a
545 @sc{pty}. On these systems, Emacs sends an @sc{eof} periodically amidst
546 the other characters, to force them through. For most programs,
547 these @sc{eof}s do no harm.
548
549 @defun process-send-string process-name string
550 This function sends @var{process-name} the contents of @var{string} as
551 standard input. The argument @var{process-name} must be a process or
552 the name of a process. If it is @code{nil}, the current buffer's
553 process is used.
554
555 The function returns @code{nil}.
556
557 @smallexample
558 @group
559 (process-send-string "shell<1>" "ls\n")
560 @result{} nil
561 @end group
562
563
564 @group
565 ---------- Buffer: *shell* ----------
566 ...
567 introduction.texi syntax-tables.texi~
568 introduction.texi~ text.texi
569 introduction.txt text.texi~
570 ...
571 ---------- Buffer: *shell* ----------
572 @end group
573 @end smallexample
574 @end defun
575
576 @deffn Command process-send-region process-name start end
577 This function sends the text in the region defined by @var{start} and
578 @var{end} as standard input to @var{process-name}, which is a process or
579 a process name. (If it is @code{nil}, the current buffer's process is
580 used.)
581
582 An error is signaled unless both @var{start} and @var{end} are
583 integers or markers that indicate positions in the current buffer. (It
584 is unimportant which number is larger.)
585 @end deffn
586
587 @defun process-send-eof &optional process-name
588 This function makes @var{process-name} see an end-of-file in its
589 input. The @sc{eof} comes after any text already sent to it.
590
591 If @var{process-name} is not supplied, or if it is @code{nil}, then
592 this function sends the @sc{eof} to the current buffer's process. An
593 error is signaled if the current buffer has no process.
594
595 The function returns @var{process-name}.
596
597 @smallexample
598 @group
599 (process-send-eof "shell")
600 @result{} "shell"
601 @end group
602 @end smallexample
603 @end defun
604
605 @node Signals to Processes
606 @section Sending Signals to Processes
607 @cindex process signals
608 @cindex sending signals
609 @cindex signals
610
611 @dfn{Sending a signal} to a subprocess is a way of interrupting its
612 activities. There are several different signals, each with its own
613 meaning. The set of signals and their names is defined by the operating
614 system. For example, the signal @code{SIGINT} means that the user has
615 typed @kbd{C-c}, or that some analogous thing has happened.
616
617 Each signal has a standard effect on the subprocess. Most signals
618 kill the subprocess, but some stop or resume execution instead. Most
619 signals can optionally be handled by programs; if the program handles
620 the signal, then we can say nothing in general about its effects.
621
622 You can send signals explicitly by calling the functions in this
623 section. Emacs also sends signals automatically at certain times:
624 killing a buffer sends a @code{SIGHUP} signal to all its associated
625 processes; killing Emacs sends a @code{SIGHUP} signal to all remaining
626 processes. (@code{SIGHUP} is a signal that usually indicates that the
627 user hung up the phone.)
628
629 Each of the signal-sending functions takes two optional arguments:
630 @var{process-name} and @var{current-group}.
631
632 The argument @var{process-name} must be either a process, the name of
633 one, or @code{nil}. If it is @code{nil}, the process defaults to the
634 process associated with the current buffer. An error is signaled if
635 @var{process-name} does not identify a process.
636
637 The argument @var{current-group} is a flag that makes a difference
638 when you are running a job-control shell as an Emacs subprocess. If it
639 is non-@code{nil}, then the signal is sent to the current process-group
640 of the terminal which Emacs uses to communicate with the subprocess. If
641 the process is a job-control shell, this means the shell's current
642 subjob. If it is @code{nil}, the signal is sent to the process group of
643 the immediate subprocess of Emacs. If the subprocess is a job-control
644 shell, this is the shell itself.
645
646 The flag @var{current-group} has no effect when a pipe is used to
647 communicate with the subprocess, because the operating system does not
648 support the distinction in the case of pipes. For the same reason,
649 job-control shells won't work when a pipe is used. See
650 @code{process-connection-type} in @ref{Asynchronous Processes}.
651
652 @defun interrupt-process &optional process-name current-group
653 This function interrupts the process @var{process-name} by sending the
654 signal @code{SIGINT}. Outside of Emacs, typing the ``interrupt
655 character'' (normally @kbd{C-c} on some systems, and @code{DEL} on
656 others) sends this signal. When the argument @var{current-group} is
657 non-@code{nil}, you can think of this function as ``typing @kbd{C-c}''
658 on the terminal by which Emacs talks to the subprocess.
659 @end defun
660
661 @defun kill-process &optional process-name current-group
662 This function kills the process @var{process-name} by sending the
663 signal @code{SIGKILL}. This signal kills the subprocess immediately,
664 and cannot be handled by the subprocess.
665 @end defun
666
667 @defun quit-process &optional process-name current-group
668 This function sends the signal @code{SIGQUIT} to the process
669 @var{process-name}. This signal is the one sent by the ``quit
670 character'' (usually @kbd{C-b} or @kbd{C-\}) when you are not inside
671 Emacs.
672 @end defun
673
674 @defun stop-process &optional process-name current-group
675 This function stops the process @var{process-name} by sending the
676 signal @code{SIGTSTP}. Use @code{continue-process} to resume its
677 execution.
678
679 On systems with job control, the ``stop character'' (usually @kbd{C-z})
680 sends this signal (outside of Emacs). When @var{current-group} is
681 non-@code{nil}, you can think of this function as ``typing @kbd{C-z}''
682 on the terminal Emacs uses to communicate with the subprocess.
683 @end defun
684
685 @defun continue-process &optional process-name current-group
686 This function resumes execution of the process @var{process} by sending
687 it the signal @code{SIGCONT}. This presumes that @var{process-name} was
688 stopped previously.
689 @end defun
690
691 @c Emacs 19 feature
692 @defun signal-process pid signal
693 This function sends a signal to process @var{pid}, which need not be
694 a child of Emacs. The argument @var{signal} specifies which signal
695 to send; it should be an integer.
696 @end defun
697
698 @node Output from Processes
699 @section Receiving Output from Processes
700 @cindex process output
701 @cindex output from processes
702
703 There are two ways to receive the output that a subprocess writes to
704 its standard output stream. The output can be inserted in a buffer,
705 which is called the associated buffer of the process, or a function
706 called the @dfn{filter function} can be called to act on the output.
707
708 @menu
709 * Process Buffers:: If no filter, output is put in a buffer.
710 * Filter Functions:: Filter functions accept output from the process.
711 * Accepting Output:: Explicitly permitting subprocess output.
712 Waiting for subprocess output.
713 @end menu
714
715 @node Process Buffers
716 @subsection Process Buffers
717
718 A process can (and usually does) have an @dfn{associated buffer},
719 which is an ordinary Emacs buffer that is used for two purposes: storing
720 the output from the process, and deciding when to kill the process. You
721 can also use the buffer to identify a process to operate on, since in
722 normal practice only one process is associated with any given buffer.
723 Many applications of processes also use the buffer for editing input to
724 be sent to the process, but this is not built into Emacs Lisp.
725
726 Unless the process has a filter function (@pxref{Filter Functions}),
727 its output is inserted in the associated buffer. The position to insert
728 the output is determined by the @code{process-mark} (@pxref{Process
729 Information}), which is then updated to point to the end of the text
730 just inserted. Usually, but not always, the @code{process-mark} is at
731 the end of the buffer. If the process has no buffer and no filter
732 function, its output is discarded.
733
734 @defun process-buffer process
735 This function returns the associated buffer of the process
736 @var{process}.
737
738 @smallexample
739 @group
740 (process-buffer (get-process "shell"))
741 @result{} #<buffer *shell*>
742 @end group
743 @end smallexample
744 @end defun
745
746 @defun process-mark process
747 This function returns the process marker for @var{process}, which is the
748 marker that says where to insert output from the process.
749
750 If @var{process} does not have a buffer, @code{process-mark} returns a
751 marker that points nowhere.
752
753 Insertion of process output in a buffer uses this marker to decide where
754 to insert, and updates it to point after the inserted text. That is why
755 successive batches of output are inserted consecutively.
756
757 Filter functions normally should use this marker in the same fashion
758 as is done by direct insertion of output in the buffer. A good
759 example of a filter function that uses @code{process-mark} is found at
760 the end of the following section.
761
762 When the user is expected to enter input in the process buffer for
763 transmission to the process, the process marker is useful for
764 distinguishing the new input from previous output.
765 @end defun
766
767 @defun set-process-buffer process buffer
768 This function sets the buffer associated with @var{process} to
769 @var{buffer}. If @var{buffer} is @code{nil}, the process becomes
770 associated with no buffer.
771 @end defun
772
773 @defun get-buffer-process buffer-or-name
774 This function returns the process associated with @var{buffer-or-name}.
775 If there are several processes associated with it, then one is chosen.
776 (Presently, the one chosen is the one most recently created.) It is
777 usually a bad idea to have more than one process associated with the
778 same buffer.
779
780 @smallexample
781 @group
782 (get-buffer-process "*shell*")
783 @result{} #<process shell>
784 @end group
785 @end smallexample
786
787 Killing the process's buffer deletes the process, which kills the
788 subprocess with a @code{SIGHUP} signal (@pxref{Signals to Processes}).
789 @end defun
790
791 @node Filter Functions
792 @subsection Process Filter Functions
793 @cindex filter function
794 @cindex process filter
795
796 A process @dfn{filter function} is a function that receives the
797 standard output from the associated process. If a process has a filter,
798 then @emph{all} output from that process, that would otherwise have been
799 in a buffer, is passed to the filter. The process buffer is used
800 directly for output from the process only when there is no filter.
801
802 A filter function must accept two arguments: the associated process and
803 a string, which is the output. The function is then free to do whatever it
804 chooses with the output.
805
806 A filter function runs only while Emacs is waiting (e.g., for terminal
807 input, or for time to elapse, or for process output). This avoids the
808 timing errors that could result from running filters at random places in
809 the middle of other Lisp programs. You may explicitly cause Emacs to
810 wait, so that filter functions will run, by calling @code{sit-for},
811 @code{sleep-for} or @code{accept-process-output} (@pxref{Accepting
812 Output}). Emacs is also waiting when the command loop is reading input.
813
814 Quitting is normally inhibited within a filter function---otherwise,
815 the effect of typing @kbd{C-g} at command level or to quit a user
816 command would be unpredictable. If you want to permit quitting inside a
817 filter function, bind @code{inhibit-quit} to @code{nil}.
818 @xref{Quitting}.
819
820 Many filter functions sometimes or always insert the text in the
821 process's buffer, mimicking the actions of Emacs when there is no
822 filter. Such filter functions need to use @code{set-buffer} in order to
823 be sure to insert in that buffer. To avoid setting the current buffer
824 semipermanently, these filter functions must use @code{unwind-protect}
825 to make sure to restore the previous current buffer. They should also
826 update the process marker, and in some cases update the value of point.
827 Here is how to do these things:
828
829 @smallexample
830 @group
831 (defun ordinary-insertion-filter (proc string)
832 (let ((old-buffer (current-buffer)))
833 (unwind-protect
834 (let (moving)
835 (set-buffer (process-buffer proc))
836 (setq moving (= (point) (process-mark proc)))
837 @end group
838 @group
839 (save-excursion
840 ;; @r{Insert the text, moving the process-marker.}
841 (goto-char (process-mark proc))
842 (insert string)
843 (set-marker (process-mark proc) (point)))
844 (if moving (goto-char (process-mark proc))))
845 (set-buffer old-buffer))))
846 @end group
847 @end smallexample
848
849 @noindent
850 The reason to use an explicit @code{unwind-protect} rather than letting
851 @code{save-excursion} restore the current buffer is so as to preserve
852 the change in point made by @code{goto-char}.
853
854 To make the filter force the process buffer to be visible whenever new
855 text arrives, insert the following line just before the
856 @code{unwind-protect}:
857
858 @smallexample
859 (display-buffer (process-buffer proc))
860 @end smallexample
861
862 To force point to move to the end of the new output no matter where
863 it was previously, eliminate the variable @code{moving} and call
864 @code{goto-char} unconditionally.
865
866 All filter functions that do regexp searching or matching should save
867 and restore the match data. Otherwise, a filter function that runs
868 during a call to @code{sit-for} might clobber the match data of the
869 program that called @code{sit-for}. @xref{Match Data}.
870
871 A filter function that writes the output into the buffer of the
872 process should check whether the process is still alive. If it tries to
873 insert into a dead buffer, it will get an error. If the buffer is dead,
874 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil}.
875
876 The output to the function may come in chunks of any size. A program
877 that produces the same output twice in a row may send it as one batch
878 of 200 characters one time, and five batches of 40 characters the next.
879
880 @defun set-process-filter process filter
881 This function gives @var{process} the filter function @var{filter}. If
882 @var{filter} is @code{nil}, it gives the process no filter.
883 @end defun
884
885 @defun process-filter process
886 This function returns the filter function of @var{process}, or @code{nil}
887 if it has none.
888 @end defun
889
890 Here is an example of use of a filter function:
891
892 @smallexample
893 @group
894 (defun keep-output (process output)
895 (setq kept (cons output kept)))
896 @result{} keep-output
897 @end group
898 @group
899 (setq kept nil)
900 @result{} nil
901 @end group
902 @group
903 (set-process-filter (get-process "shell") 'keep-output)
904 @result{} keep-output
905 @end group
906 @group
907 (process-send-string "shell" "ls ~/other\n")
908 @result{} nil
909 kept
910 @result{} ("lewis@@slug[8] % "
911 @end group
912 @group
913 "FINAL-W87-SHORT.MSS backup.otl kolstad.mss~
914 address.txt backup.psf kolstad.psf
915 backup.bib~ david.mss resume-Dec-86.mss~
916 backup.err david.psf resume-Dec.psf
917 backup.mss dland syllabus.mss
918 "
919 "#backups.mss# backup.mss~ kolstad.mss
920 ")
921 @end group
922 @end smallexample
923
924 @ignore @c The code in this example doesn't show the right way to do things.
925 Here is another, more realistic example, which demonstrates how to use
926 the process mark to do insertion in the same fashion as is done when
927 there is no filter function:
928
929 @smallexample
930 @group
931 ;; @r{Insert input in the buffer specified by @code{my-shell-buffer}}
932 ;; @r{and make sure that buffer is shown in some window.}
933 (defun my-process-filter (proc str)
934 (let ((cur (selected-window))
935 (pop-up-windows t))
936 (pop-to-buffer my-shell-buffer)
937 @end group
938 @group
939 (goto-char (point-max))
940 (insert str)
941 (set-marker (process-mark proc) (point-max))
942 (select-window cur)))
943 @end group
944 @end smallexample
945 @end ignore
946
947 @node Accepting Output
948 @subsection Accepting Output from Processes
949
950 Output from asynchronous subprocesses normally arrives only while
951 Emacs is waiting for some sort of external event, such as elapsed time
952 or terminal input. Occasionally it is useful in a Lisp program to
953 explicitly permit output to arrive at a specific point, or even to wait
954 until output arrives from a process.
955
956 @defun accept-process-output &optional process seconds millisec
957 This function allows Emacs to read pending output from processes. The
958 output is inserted in the associated buffers or given to their filter
959 functions. If @var{process} is non-@code{nil} then this function does
960 not return until some output has been received from @var{process}.
961
962 @c Emacs 19 feature
963 The arguments @var{seconds} and @var{millisec} let you specify timeout
964 periods. The former specifies a period measured in seconds and the
965 latter specifies one measured in milliseconds. The two time periods
966 thus specified are added together, and @code{accept-process-output}
967 returns after that much time whether or not there has been any
968 subprocess output.
969
970 Not all operating systems support waiting periods other than multiples
971 of a second; on those that do not, you get an error if you specify
972 nonzero @var{millisec}.
973
974 The function @code{accept-process-output} returns non-@code{nil} if it
975 did get some output, or @code{nil} if the timeout expired before output
976 arrived.
977 @end defun
978
979 @node Sentinels
980 @section Sentinels: Detecting Process Status Changes
981 @cindex process sentinel
982 @cindex sentinel
983
984 A @dfn{process sentinel} is a function that is called whenever the
985 associated process changes status for any reason, including signals
986 (whether sent by Emacs or caused by the process's own actions) that
987 terminate, stop, or continue the process. The process sentinel is also
988 called if the process exits. The sentinel receives two arguments: the
989 process for which the event occurred, and a string describing the type
990 of event.
991
992 The string describing the event looks like one of the following:
993
994 @itemize @bullet
995 @item
996 @code{"finished\n"}.
997
998 @item
999 @code{"exited abnormally with code @var{exitcode}\n"}.
1000
1001 @item
1002 @code{"@var{name-of-signal}\n"}.
1003
1004 @item
1005 @code{"@var{name-of-signal} (core dumped)\n"}.
1006 @end itemize
1007
1008 A sentinel runs only while Emacs is waiting (e.g., for terminal input,
1009 or for time to elapse, or for process output). This avoids the timing
1010 errors that could result from running them at random places in the
1011 middle of other Lisp programs. A program can wait, so that sentinels
1012 will run, by calling @code{sit-for}, @code{sleep-for} or
1013 @code{accept-process-output} (@pxref{Accepting Output}). Emacs is also
1014 waiting when the command loop is reading input.
1015
1016 Quitting is normally inhibited within a sentinel---otherwise, the
1017 effect of typing @kbd{C-g} at command level or to quit a user command
1018 would be unpredictable. If you want to permit quitting inside a
1019 sentinel, bind @code{inhibit-quit} to @code{nil}. @xref{Quitting}.
1020
1021 A sentinel that writes the output into the buffer of the process
1022 should check whether the process is still alive. If it tries to insert
1023 into a dead buffer, it will get an error. If the buffer is dead,
1024 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil}.
1025
1026 All sentinels that do regexp searching or matching should save and
1027 restore the match data. Otherwise, a sentinel that runs during a call
1028 to @code{sit-for} might clobber the match data of the program that
1029 called @code{sit-for}. @xref{Match Data}.
1030
1031 @defun set-process-sentinel process sentinel
1032 This function associates @var{sentinel} with @var{process}. If
1033 @var{sentinel} is @code{nil}, then the process will have no sentinel.
1034 The default behavior when there is no sentinel is to insert a message in
1035 the process's buffer when the process status changes.
1036
1037 @smallexample
1038 @group
1039 (defun msg-me (process event)
1040 (princ
1041 (format "Process: %s had the event `%s'" process event)))
1042 (set-process-sentinel (get-process "shell") 'msg-me)
1043 @result{} msg-me
1044 @end group
1045 @group
1046 (kill-process (get-process "shell"))
1047 @print{} Process: #<process shell> had the event `killed'
1048 @result{} #<process shell>
1049 @end group
1050 @end smallexample
1051 @end defun
1052
1053 @defun process-sentinel process
1054 This function returns the sentinel of @var{process}, or @code{nil} if it
1055 has none.
1056 @end defun
1057
1058 @defun waiting-for-user-input-p
1059 While a sentinel or filter function is running, this function returns
1060 non-@code{nil} if Emacs was waiting for keyboard input from the user at
1061 the time the sentinel or filter function was called, @code{nil} if it
1062 was not.
1063 @end defun
1064
1065 @node Transaction Queues
1066 @section Transaction Queues
1067 @cindex transaction queue
1068
1069 You can use a @dfn{transaction queue} for more convenient communication
1070 with subprocesses using transactions. First use @code{tq-create} to
1071 create a transaction queue communicating with a specified process. Then
1072 you can call @code{tq-enqueue} to send a transaction.
1073
1074 @defun tq-create process
1075 This function creates and returns a transaction queue communicating with
1076 @var{process}. The argument @var{process} should be a subprocess
1077 capable of sending and receiving streams of bytes. It may be a child
1078 process, or it may be a TCP connection to a server possibly on another
1079 machine.
1080 @end defun
1081
1082 @defun tq-enqueue queue question regexp closure fn
1083 This function sends a transaction to queue @var{queue}. Specifying the
1084 queue has the effect of specifying the subprocess to talk to.
1085
1086 The argument @var{question} is the outgoing message which starts the
1087 transaction. The argument @var{fn} is the function to call when the
1088 corresponding answer comes back; it is called with two arguments:
1089 @var{closure}, and the answer received.
1090
1091 The argument @var{regexp} is a regular expression that should match the
1092 entire answer, but nothing less; that's how @code{tq-enqueue} determines
1093 where the answer ends.
1094
1095 The return value of @code{tq-enqueue} itself is not meaningful.
1096 @end defun
1097
1098 @defun tq-close queue
1099 Shut down transaction queue @var{queue}, waiting for all pending transactions
1100 to complete, and then terminate the connection or child process.
1101 @end defun
1102
1103 Transaction queues are implemented by means of a filter function.
1104 @xref{Filter Functions}.
1105
1106 @node TCP
1107 @section TCP
1108 @cindex TCP
1109
1110 Emacs Lisp programs can open TCP connections to other processes on the
1111 same machine or other machines. A network connection is handled by Lisp
1112 much like a subprocess, and is represented by a process object.
1113 However, the process you are communicating with is not a child of the
1114 Emacs process, so you can't kill it or send it signals. All you can do
1115 is send and receive data. @code{delete-process} closes the connection,
1116 but does not kill the process at the other end; that process must decide
1117 what to do about closure of the connection.
1118
1119 You can distinguish process objects representing network connections
1120 from those representing subprocesses with the @code{process-status}
1121 function. @xref{Process Information}.
1122
1123 @defun open-network-stream name buffer-or-name host service
1124 This function opens a TCP connection for a service to a host. It
1125 returns a process object to represent the connection.
1126
1127 The @var{name} argument specifies the name for the process object. It
1128 is modified as necessary to make it unique.
1129
1130 The @var{buffer-or-name} argument is the buffer to associate with the
1131 connection. Output from the connection is inserted in the buffer,
1132 unless you specify a filter function to handle the output. If
1133 @var{buffer-or-name} is @code{nil}, it means that the connection is not
1134 associated with any buffer.
1135
1136 The arguments @var{host} and @var{service} specify where to connect to;
1137 @var{host} is the host name (a string), and @var{service} is the name of
1138 a defined network service (a string) or a port number (an integer).
1139 @end defun