processes.texi 140 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1 2
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
Paul Eggert's avatar
Paul Eggert committed
3
@c Copyright (C) 1990-1995, 1998-1999, 2001-2019 Free Software
4
@c Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
5
@c See the file elisp.texi for copying conditions.
6
@node Processes
Glenn Morris's avatar
Glenn Morris committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
@chapter Processes
@cindex child process
@cindex parent process
@cindex subprocess
@cindex process

  In the terminology of operating systems, a @dfn{process} is a space in
which a program can execute.  Emacs runs in a process.  Emacs Lisp
programs can invoke other programs in processes of their own.  These are
called @dfn{subprocesses} or @dfn{child processes} of the Emacs process,
which is their @dfn{parent process}.

  A subprocess of Emacs may be @dfn{synchronous} or @dfn{asynchronous},
depending on how it is created.  When you create a synchronous
subprocess, the Lisp program waits for the subprocess to terminate
before continuing execution.  When you create an asynchronous
subprocess, it can run in parallel with the Lisp program.  This kind of
subprocess is represented within Emacs by a Lisp object which is also
25
called a ``process''.  Lisp programs can use this object to communicate
Glenn Morris's avatar
Glenn Morris committed
26 27 28 29
with the subprocess or to control it.  For example, you can send
signals, obtain status information, receive output from the process, or
send input to it.

30 31 32 33 34 35 36
  In addition to processes that run programs, Lisp programs can open
connections of several types to devices or processes running on the
same machine or on other machines.  The supported connection types
are: TCP and UDP network connections, serial port connections, and
pipe connections.  Each such connection is also represented by a
process object.

Glenn Morris's avatar
Glenn Morris committed
37
@defun processp object
38
This function returns @code{t} if @var{object} represents an Emacs
39 40 41
process object, @code{nil} otherwise.  The process object can
represent a subprocess running a program or a connection of any
supported type.
Glenn Morris's avatar
Glenn Morris committed
42 43
@end defun

44 45 46 47
  In addition to subprocesses of the current Emacs session, you can
also access other processes running on your machine.  @xref{System
Processes}.

Glenn Morris's avatar
Glenn Morris committed
48 49 50 51 52 53 54 55 56 57 58 59 60
@menu
* Subprocess Creation::      Functions that start subprocesses.
* Shell Arguments::          Quoting an argument to pass it to a shell.
* Synchronous Processes::    Details of using synchronous subprocesses.
* Asynchronous Processes::   Starting up an asynchronous subprocess.
* Deleting Processes::       Eliminating an asynchronous subprocess.
* Process Information::      Accessing run-status and other attributes.
* Input to Processes::       Sending input to an asynchronous subprocess.
* Signals to Processes::     Stopping, continuing or interrupting
                               an asynchronous subprocess.
* Output from Processes::    Collecting output from an asynchronous subprocess.
* Sentinels::                Sentinels run when process run-status changes.
* Query Before Exit::        Whether to query if exiting will kill a process.
61
* System Processes::         Accessing other processes running on your system.
Glenn Morris's avatar
Glenn Morris committed
62
* Transaction Queues::       Transaction-based communication with subprocesses.
Glenn Morris's avatar
Glenn Morris committed
63 64 65 66 67
* Network::                  Opening network connections.
* Network Servers::          Network servers let Emacs accept net connections.
* Datagrams::                UDP network connections.
* Low-Level Network::        Lower-level but more general function
                               to create connections and servers.
Glenn Morris's avatar
Glenn Morris committed
68
* Misc Network::             Additional relevant functions for net connections.
69
* Serial Ports::             Communicating with serial ports.
Glenn Morris's avatar
Glenn Morris committed
70 71 72 73 74
* Byte Packing::             Using bindat to pack and unpack binary data.
@end menu

@node Subprocess Creation
@section Functions that Create Subprocesses
75 76
@cindex create subprocess
@cindex process creation
Glenn Morris's avatar
Glenn Morris committed
77

78
  There are three primitives that create a new subprocess in which to run
79
a program.  One of them, @code{make-process}, creates an asynchronous
Glenn Morris's avatar
Glenn Morris committed
80 81 82
process and returns a process object (@pxref{Asynchronous Processes}).
The other two, @code{call-process} and @code{call-process-region},
create a synchronous process and do not return a process object
83 84 85
(@pxref{Synchronous Processes}).  There are various higher-level
functions that make use of these primitives to run particular types of
process.
Glenn Morris's avatar
Glenn Morris committed
86 87 88 89 90 91

  Synchronous and asynchronous processes are explained in the following
sections.  Since the three functions are all called in a similar
fashion, their common arguments are described here.

@cindex execute program
92 93
@cindex @env{PATH} environment variable
@cindex @env{HOME} environment variable
94 95 96 97 98 99 100 101
  In all cases, the functions specify the program to be run.  An error
is signaled if the file is not found or cannot be executed.  If the
file name is relative, the variable @code{exec-path} contains a list
of directories to search.  Emacs initializes @code{exec-path} when it
starts up, based on the value of the environment variable @env{PATH}.
The standard file name constructs, @samp{~}, @samp{.}, and @samp{..},
are interpreted as usual in @code{exec-path}, but environment variable
substitutions (@samp{$HOME}, etc.)@: are not recognized; use
Glenn Morris's avatar
Glenn Morris committed
102 103 104 105 106 107 108
@code{substitute-in-file-name} to perform them (@pxref{File Name
Expansion}).  @code{nil} in this list refers to
@code{default-directory}.

  Executing a program can also try adding suffixes to the specified
name:

109
@defopt exec-suffixes
Glenn Morris's avatar
Glenn Morris committed
110 111 112 113
This variable is a list of suffixes (strings) to try adding to the
specified program file name.  The list should include @code{""} if you
want the name to be tried exactly as specified.  The default value is
system-dependent.
114
@end defopt
Glenn Morris's avatar
Glenn Morris committed
115 116

  @strong{Please note:} The argument @var{program} contains only the
117 118 119
name of the program file; it may not contain any command-line
arguments.  You must use a separate argument, @var{args}, to provide
those, as described below.
Glenn Morris's avatar
Glenn Morris committed
120 121

  Each of the subprocess-creating functions has a @var{buffer-or-name}
122 123 124 125 126 127 128 129 130 131 132 133 134
argument that specifies where the output from the program will go.  It
should be a buffer or a buffer name; if it is a buffer name, that will
create the buffer if it does not already exist.  It can also be
@code{nil}, which says to discard the output, unless a custom filter
function handles it.  (@xref{Filter Functions}, and @ref{Read and
Print}.)  Normally, you should avoid having multiple processes send
output to the same buffer because their output would be intermixed
randomly.  For synchronous processes, you can send the output to a
file instead of a buffer (and the corresponding argument is therefore
more appropriately called @var{destination}).  By default, both
standard output and standard error streams go to the same destination,
but all the 3 primitives allow optionally to direct the standard error
stream to a different destination.
Glenn Morris's avatar
Glenn Morris committed
135 136

@cindex program arguments
137 138 139 140 141 142 143 144 145 146
  All three of the subprocess-creating functions allow to specify
command-line arguments for the process to run. For @code{call-process}
and @code{call-process-region}, these come in the form of a
@code{&rest} argument, @var{args}.  For @code{make-process}, both the
program to run and its command-line arguments are specified as a list
of strings.  The command-line arguments must all be strings, and they
are supplied to the program as separate argument strings.  Wildcard
characters and other shell constructs have no special meanings in
these strings, since the strings are passed directly to the specified
program.
Glenn Morris's avatar
Glenn Morris committed
147 148 149 150

@cindex environment variables, subprocesses
  The subprocess inherits its environment from Emacs, but you can
specify overrides for it with @code{process-environment}.  @xref{System
151 152
Environment}.  The subprocess gets its current directory from the
value of @code{default-directory}.
Glenn Morris's avatar
Glenn Morris committed
153 154 155 156

@defvar exec-directory
@pindex movemail
The value of this variable is a string, the name of a directory that
157
contains programs that come with GNU Emacs and are intended for Emacs
Glenn Morris's avatar
Glenn Morris committed
158 159 160 161 162 163 164 165 166
to invoke.  The program @code{movemail} is an example of such a program;
Rmail uses it to fetch new mail from an inbox.
@end defvar

@defopt exec-path
The value of this variable is a list of directories to search for
programs to run in subprocesses.  Each element is either the name of a
directory (i.e., a string), or @code{nil}, which stands for the default
directory (which is the value of @code{default-directory}).
167
@xref{Locating Files, executable-find}, for the details of this search.
Glenn Morris's avatar
Glenn Morris committed
168 169 170 171 172
@cindex program directories

The value of @code{exec-path} is used by @code{call-process} and
@code{start-process} when the @var{program} argument is not an absolute
file name.
173 174 175 176 177

Generally, you should not modify @code{exec-path} directly.  Instead,
ensure that your @env{PATH} environment variable is set appropriately
before starting Emacs.  Trying to modify @code{exec-path}
independently of @env{PATH} can lead to confusing results.
Glenn Morris's avatar
Glenn Morris committed
178 179
@end defopt

180
@defun exec-path
181 182 183
This function is an extension of the variable @code{exec-path}.  If
@code{default-directory} indicates a remote directory, this function
returns a list of directories used for searching programs on the
184 185 186 187
respective remote host.  In case of a local @code{default-directory},
the function returns just the value of the variable @code{exec-path}.
@end defun

Glenn Morris's avatar
Glenn Morris committed
188 189 190 191 192 193 194 195 196 197 198 199 200
@node Shell Arguments
@section Shell Arguments
@cindex arguments for shell commands
@cindex shell command arguments

  Lisp programs sometimes need to run a shell and give it a command
that contains file names that were specified by the user.  These
programs ought to be able to support any valid file name.  But the shell
gives special treatment to certain characters, and if these characters
occur in the file name, they will confuse the shell.  To handle these
characters, use the function @code{shell-quote-argument}:

@defun shell-quote-argument argument
201
This function returns a string that represents, in shell syntax,
Glenn Morris's avatar
Glenn Morris committed
202 203 204 205 206 207 208
an argument whose actual contents are @var{argument}.  It should
work reliably to concatenate the return value into a shell command
and then pass it to a shell for execution.

Precisely what this function does depends on your operating system.  The
function is designed to work with the syntax of your system's standard
shell; if you use an unusual shell, you will need to redefine this
209
function.  @xref{Security Considerations}.
Glenn Morris's avatar
Glenn Morris committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

@example
;; @r{This example shows the behavior on GNU and Unix systems.}
(shell-quote-argument "foo > bar")
     @result{} "foo\\ \\>\\ bar"

;; @r{This example shows the behavior on MS-DOS and MS-Windows.}
(shell-quote-argument "foo > bar")
     @result{} "\"foo > bar\""
@end example

Here's an example of using @code{shell-quote-argument} to construct
a shell command:

@example
225
(concat "diff -u "
Glenn Morris's avatar
Glenn Morris committed
226 227 228 229 230 231
        (shell-quote-argument oldfile)
        " "
        (shell-quote-argument newfile))
@end example
@end defun

232 233 234 235
@cindex quoting and unquoting command-line arguments
@cindex minibuffer input, and command-line arguments
@cindex @code{call-process}, command-line arguments from minibuffer
@cindex @code{start-process}, command-line arguments from minibuffer
Eli Zaretskii's avatar
Eli Zaretskii committed
236 237 238
  The following two functions are useful for combining a list of
individual command-line argument strings into a single string, and
taking a string apart into a list of individual command-line
239 240 241 242 243
arguments.  These functions are mainly intended for converting user
input in the minibuffer, a Lisp string, into a list of string
arguments to be passed to @code{make-process}, @code{call-process} or
@code{start-process}, or for converting such lists of arguments into a
single Lisp string to be presented in the minibuffer or echo area.
244 245 246 247 248
Note that if a shell is involved (e.g., if using
@code{call-process-shell-command}), arguments should still be
protected by @code{shell-quote-argument};
@code{combine-and-quote-strings} is @emph{not} intended to protect
special characters from shell evaluation.
249 250 251 252

@defun split-string-and-unquote string &optional separators
This function splits @var{string} into substrings at matches for the
regular expression @var{separators}, like @code{split-string} does
253 254
(@pxref{Creating Strings}); in addition, it removes quoting from the
substrings.  It then makes a list of the substrings and returns it.
255

Chong Yidong's avatar
Chong Yidong committed
256 257 258
If @var{separators} is omitted or @code{nil}, it defaults to
@code{"\\s-+"}, which is a regular expression that matches one or more
characters with whitespace syntax (@pxref{Syntax Class Table}).
259

260
This function supports two types of quoting: enclosing a whole string
261 262 263
in double quotes @code{"@dots{}"}, and quoting individual characters
with a backslash escape @samp{\}.  The latter is also used in Lisp
strings, so this function can handle those as well.
264 265 266 267
@end defun

@defun combine-and-quote-strings list-of-strings &optional separator
This function concatenates @var{list-of-strings} into a single string,
268 269 270 271
quoting each string as necessary.  It also sticks the @var{separator}
string between each pair of strings; if @var{separator} is omitted or
@code{nil}, it defaults to @code{" "}.  The return value is the
resulting string.
272 273 274 275

The strings in @var{list-of-strings} that need quoting are those that
include @var{separator} as their substring.  Quoting a string encloses
it in double quotes @code{"@dots{}"}.  In the simplest case, if you
Eli Zaretskii's avatar
Eli Zaretskii committed
276 277
are consing a command from the individual command-line arguments,
every argument that includes embedded blanks will be quoted.
278 279
@end defun

Glenn Morris's avatar
Glenn Morris committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
@node Synchronous Processes
@section Creating a Synchronous Process
@cindex synchronous subprocess

  After a @dfn{synchronous process} is created, Emacs waits for the
process to terminate before continuing.  Starting Dired on GNU or
Unix@footnote{On other systems, Emacs uses a Lisp emulation of
@code{ls}; see @ref{Contents of Directories}.} is an example of this: it
runs @code{ls} in a synchronous process, then modifies the output
slightly.  Because the process is synchronous, the entire directory
listing arrives in the buffer before Emacs tries to do anything with it.

  While Emacs waits for the synchronous subprocess to terminate, the
user can quit by typing @kbd{C-g}.  The first @kbd{C-g} tries to kill
the subprocess with a @code{SIGINT} signal; but it waits until the
subprocess actually terminates before quitting.  If during that time the
user types another @kbd{C-g}, that kills the subprocess instantly with
@code{SIGKILL} and quits immediately (except on MS-DOS, where killing
other processes doesn't work).  @xref{Quitting}.

  The synchronous subprocess functions return an indication of how the
process terminated.

  The output from a synchronous subprocess is generally decoded using a
coding system, much like text read from a file.  The input sent to a
subprocess by @code{call-process-region} is encoded using a coding
system, much like text written into a file.  @xref{Coding Systems}.

@defun call-process program &optional infile destination display &rest args
309
This function calls @var{program} and waits for it to finish.
Glenn Morris's avatar
Glenn Morris committed
310

311 312 313 314 315
The current working directory of the subprocess is set to the current
buffer's value of @code{default-directory} if that is local (as
determined by @code{unhandled-file-name-directory}), or "~" otherwise.
If you want to run a process in a remote directory use
@code{process-file}.
316

317
The standard input for the new process comes from file @var{infile} if
Glenn Morris's avatar
Glenn Morris committed
318 319 320 321 322 323 324 325 326
@var{infile} is not @code{nil}, and from the null device otherwise.
The argument @var{destination} says where to put the process output.
Here are the possibilities:

@table @asis
@item a buffer
Insert the output in that buffer, before point.  This includes both the
standard output stream and the standard error stream of the process.

327
@item a buffer name (a string)
Glenn Morris's avatar
Glenn Morris committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
Insert the output in a buffer with that name, before point.

@item @code{t}
Insert the output in the current buffer, before point.

@item @code{nil}
Discard the output.

@item 0
Discard the output, and return @code{nil} immediately without waiting
for the subprocess to finish.

In this case, the process is not truly synchronous, since it can run in
parallel with Emacs; but you can think of it as synchronous in that
Emacs is essentially finished with the subprocess as soon as this
function returns.

MS-DOS doesn't support asynchronous subprocesses, so this option doesn't
work there.

348
@item @code{(:file @var{file-name})}
349 350
Send the output to the file name specified, overwriting it if it
already exists.
351

Glenn Morris's avatar
Glenn Morris committed
352 353 354 355 356 357 358 359 360 361 362
@item @code{(@var{real-destination} @var{error-destination})}
Keep the standard output stream separate from the standard error stream;
deal with the ordinary output as specified by @var{real-destination},
and dispose of the error output according to @var{error-destination}.
If @var{error-destination} is @code{nil}, that means to discard the
error output, @code{t} means mix it with the ordinary output, and a
string specifies a file name to redirect error output into.

You can't directly specify a buffer to put the error output in; that is
too difficult to implement.  But you can achieve this result by sending
the error output to a temporary file and then inserting the file into a
363
buffer when the subprocess finishes.
Glenn Morris's avatar
Glenn Morris committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377
@end table

If @var{display} is non-@code{nil}, then @code{call-process} redisplays
the buffer as output is inserted.  (However, if the coding system chosen
for decoding output is @code{undecided}, meaning deduce the encoding
from the actual data, then redisplay sometimes cannot continue once
non-@acronym{ASCII} characters are encountered.  There are fundamental
reasons why it is hard to fix this; see @ref{Output from Processes}.)

Otherwise the function @code{call-process} does no redisplay, and the
results become visible on the screen only when Emacs redisplays that
buffer in the normal course of events.

The remaining arguments, @var{args}, are strings that specify command
378 379
line arguments for the program.  Each string is passed to
@var{program} as a separate argument.
Glenn Morris's avatar
Glenn Morris committed
380 381 382 383 384

The value returned by @code{call-process} (unless you told it not to
wait) indicates the reason for process termination.  A number gives the
exit status of the subprocess; 0 means success, and any other value
means failure.  If the process terminated with a signal,
385 386
@code{call-process} returns a string describing the signal.  If you
told @code{call-process} not to wait, it returns @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
387 388 389 390 391 392 393 394 395

In the examples below, the buffer @samp{foo} is current.

@smallexample
@group
(call-process "pwd" nil t)
     @result{} 0

---------- Buffer: foo ----------
396
/home/lewis/manual
Glenn Morris's avatar
Glenn Morris committed
397 398 399 400 401 402 403 404
---------- Buffer: foo ----------
@end group

@group
(call-process "grep" nil "bar" nil "lewis" "/etc/passwd")
     @result{} 0

---------- Buffer: bar ----------
405
lewis:x:1001:1001:Bil Lewis,,,,:/home/lewis:/bin/bash
Glenn Morris's avatar
Glenn Morris committed
406 407 408 409 410

---------- Buffer: bar ----------
@end group
@end smallexample

411 412
Here is an example of the use of @code{call-process}, as used to
be found in the definition of the @code{insert-directory} function:
Glenn Morris's avatar
Glenn Morris committed
413 414 415

@smallexample
@group
416
(call-process insert-directory-program nil t nil switches
Glenn Morris's avatar
Glenn Morris committed
417 418 419 420 421 422 423 424 425
              (if full-directory-p
                  (concat (file-name-as-directory file) ".")
                file))
@end group
@end smallexample
@end defun

@defun process-file program &optional infile buffer display &rest args
This function processes files synchronously in a separate process.  It
426 427 428
is similar to @code{call-process}, but may invoke a file name handler
based on the value of the variable @code{default-directory}, which
specifies the current working directory of the subprocess.
Glenn Morris's avatar
Glenn Morris committed
429 430 431 432

The arguments are handled in almost the same way as for
@code{call-process}, with the following differences:

433
Some file name handlers may not support all combinations and forms of the
Glenn Morris's avatar
Glenn Morris committed
434
arguments @var{infile}, @var{buffer}, and @var{display}.  For example,
435
some file name handlers might behave as if @var{display} were @code{nil},
Glenn Morris's avatar
Glenn Morris committed
436
regardless of the value actually passed.  As another example, some
437
file name handlers might not support separating standard output and error
Glenn Morris's avatar
Glenn Morris committed
438 439
output by way of the @var{buffer} argument.

440
If a file name handler is invoked, it determines the program to run based
441
on the first argument @var{program}.  For instance, suppose that a
Glenn Morris's avatar
Glenn Morris committed
442
handler for remote files is invoked.  Then the path that is used for
443
searching for the program might be different from @code{exec-path}.
Glenn Morris's avatar
Glenn Morris committed
444

445 446
The second argument @var{infile} may invoke a file name handler.  The file
name handler could be different from the handler chosen for the
Glenn Morris's avatar
Glenn Morris committed
447
@code{process-file} function itself.  (For example,
448 449
@code{default-directory} could be on one remote host, and
@var{infile} on a different remote host.  Or @code{default-directory}
Glenn Morris's avatar
Glenn Morris committed
450 451 452 453 454 455 456 457 458 459 460 461
could be non-special, whereas @var{infile} is on a remote host.)

If @var{buffer} is a list of the form @code{(@var{real-destination}
@var{error-destination})}, and @var{error-destination} names a file,
then the same remarks as for @var{infile} apply.

The remaining arguments (@var{args}) will be passed to the process
verbatim.  Emacs is not involved in processing file names that are
present in @var{args}.  To avoid confusion, it may be best to avoid
absolute file names in @var{args}, but rather to specify all file
names as relative to @code{default-directory}.  The function
@code{file-relative-name} is useful for constructing such relative
462 463 464
file names.  Alternatively, you can use @code{file-local-name}
(@pxref{Magic File Names}) to obtain an absolute file name as seen
from the remote host's perspective.
Glenn Morris's avatar
Glenn Morris committed
465 466
@end defun

467
@defvar process-file-side-effects
468
This variable indicates whether a call of @code{process-file} changes
469 470
remote files.

471
By default, this variable is always set to @code{t}, meaning that a
472
call of @code{process-file} could potentially change any file on a
473
remote host.  When set to @code{nil}, a file name handler could optimize
474
its behavior with respect to remote file attribute caching.
475

476 477
You should only ever change this variable with a let-binding; never
with @code{setq}.
478 479
@end defvar

Glenn Morris's avatar
Glenn Morris committed
480 481 482 483 484 485 486 487 488 489 490 491 492
@defun call-process-region start end program &optional delete destination display &rest args
This function sends the text from @var{start} to @var{end} as
standard input to a process running @var{program}.  It deletes the text
sent if @var{delete} is non-@code{nil}; this is useful when
@var{destination} is @code{t}, to insert the output in the current
buffer in place of the input.

The arguments @var{destination} and @var{display} control what to do
with the output from the subprocess, and whether to update the display
as it comes in.  For details, see the description of
@code{call-process}, above.  If @var{destination} is the integer 0,
@code{call-process-region} discards the output and returns @code{nil}
immediately, without waiting for the subprocess to finish (this only
493
works if asynchronous subprocesses are supported; i.e., not on MS-DOS).
Glenn Morris's avatar
Glenn Morris committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

The remaining arguments, @var{args}, are strings that specify command
line arguments for the program.

The return value of @code{call-process-region} is just like that of
@code{call-process}: @code{nil} if you told it to return without
waiting; otherwise, a number or string which indicates how the
subprocess terminated.

In the following example, we use @code{call-process-region} to run the
@code{cat} utility, with standard input being the first five characters
in buffer @samp{foo} (the word @samp{input}).  @code{cat} copies its
standard input into its standard output.  Since the argument
@var{destination} is @code{t}, this output is inserted in the current
buffer.

@smallexample
@group
---------- Buffer: foo ----------
input@point{}
---------- Buffer: foo ----------
@end group

@group
(call-process-region 1 6 "cat" nil t)
     @result{} 0

---------- Buffer: foo ----------
inputinput@point{}
---------- Buffer: foo ----------
@end group
@end smallexample

527
  For example, the @code{shell-command-on-region} command uses
Tino Calancha's avatar
Tino Calancha committed
528
@code{call-shell-region} in a manner similar to this:
Glenn Morris's avatar
Glenn Morris committed
529 530 531

@smallexample
@group
Tino Calancha's avatar
Tino Calancha committed
532
(call-shell-region
Glenn Morris's avatar
Glenn Morris committed
533
 start end
Tino Calancha's avatar
Tino Calancha committed
534
 command              ; @r{shell command}
535
 nil                  ; @r{do not delete region}
Tino Calancha's avatar
Tino Calancha committed
536
 buffer)              ; @r{send output to @code{buffer}}
Glenn Morris's avatar
Glenn Morris committed
537 538 539 540
@end group
@end smallexample
@end defun

541
@defun call-process-shell-command command &optional infile destination display
542
This function executes the shell command @var{command} synchronously.
543 544 545 546
The other arguments are handled as in @code{call-process}.  An old
calling convention allowed passing any number of additional arguments
after @var{display}, which were concatenated to @var{command}; this is
still supported, but strongly discouraged.
Glenn Morris's avatar
Glenn Morris committed
547 548
@end defun

549
@defun process-file-shell-command command &optional infile destination display
Glenn Morris's avatar
Glenn Morris committed
550 551
This function is like @code{call-process-shell-command}, but uses
@code{process-file} internally.  Depending on @code{default-directory},
552
@var{command} can be executed also on remote hosts.  An old calling
Stephen Berman's avatar
Stephen Berman committed
553
convention allowed passing any number of additional arguments after
554 555
@var{display}, which were concatenated to @var{command}; this is still
supported, but strongly discouraged.
Glenn Morris's avatar
Glenn Morris committed
556 557
@end defun

Tino Calancha's avatar
Tino Calancha committed
558 559 560 561 562 563
@defun call-shell-region start end command &optional delete destination
This function sends the text from @var{start} to @var{end} as
standard input to an inferior shell running @var{command}.  This function
is similar than @code{call-process-region}, with process being a shell.
The arguments @code{delete}, @code{destination} and the return value
are like in @code{call-process-region}.
Paul Eggert's avatar
Paul Eggert committed
564
Note that this function doesn't accept additional arguments.
Tino Calancha's avatar
Tino Calancha committed
565 566
@end defun

Glenn Morris's avatar
Glenn Morris committed
567 568 569 570 571
@defun shell-command-to-string command
This function executes @var{command} (a string) as a shell command,
then returns the command's output as a string.
@end defun

572 573 574
@c There is also shell-command-on-region, but that is more of a user
@c command, not something to use in programs.

575
@defun process-lines program &rest args
576 577 578 579 580 581
This function runs @var{program}, waits for it to finish, and returns
its output as a list of strings.  Each string in the list holds a
single line of text output by the program; the end-of-line characters
are stripped from each line.  The arguments beyond @var{program},
@var{args}, are strings that specify command-line arguments with which
to run the program.
582 583 584 585 586 587 588 589

If @var{program} exits with a non-zero exit status, this function
signals an error.

This function works by calling @code{call-process}, so program output
is decoded in the same way as for @code{call-process}.
@end defun

Glenn Morris's avatar
Glenn Morris committed
590 591 592 593
@node Asynchronous Processes
@section Creating an Asynchronous Process
@cindex asynchronous subprocess

594 595 596 597 598
  In this section, we describe how to create an @dfn{asynchronous
process}.  After an asynchronous process is created, it runs in
parallel with Emacs, and Emacs can communicate with it using the
functions described in the following sections (@pxref{Input to
Processes}, and @pxref{Output from Processes}).  Note that process
599 600
communication is only partially asynchronous: Emacs sends and receives
data to and from a process only when those functions are called.
601

602 603
@cindex pty, when to use for subprocess communications
@cindex pipe, when to use for subprocess communications
604 605
  An asynchronous process is controlled either via a @dfn{pty}
(pseudo-terminal) or a @dfn{pipe}.  The choice of pty or pipe is made
606 607 608 609 610 611 612
when creating the process, by default based on the value of the
variable @code{process-connection-type} (see below).  If available,
ptys are usually preferable for processes visible to the user, as in
Shell mode, because they allow for job control (@kbd{C-c}, @kbd{C-z},
etc.)@: between the process and its children, and because interactive
programs treat ptys as terminal devices, whereas pipes don't support
these features.  However, for subprocesses used by Lisp programs for
613 614 615
internal purposes (i.e., no user interaction with the subprocess is
required), where significant amounts of data need to be exchanged
between the subprocess and the Lisp program, it is often better to use
616 617 618
a pipe, because pipes are more efficient.  Also, the total number of
ptys is limited on many systems, and it is good not to waste them
unnecessarily.
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

@defun make-process &rest args
This function is the basic low-level primitive for starting
asynchronous subprocesses.  It returns a process object representing
the subprocess.  Compared to the more high-level @code{start-process},
described below, it takes keyword arguments, is more flexible, and
allows to specify process filters and sentinels in a single call.

The arguments @var{args} are a list of keyword/argument pairs.
Omitting a keyword is always equivalent to specifying it with value
@code{nil}.  Here are the meaningful keywords:

@table @asis
@item :name @var{name}
Use the string @var{name} as the process name; if a process with this
name already exists, then @var{name} is modified (by appending
@samp{<1>}, etc.)@: to be unique.

@item :buffer @var{buffer}
Use @var{buffer} as the process buffer.  If the value is @code{nil},
the subprocess is not associated with any buffer.

@item :command @var{command}
Use @var{command} as the command line of the process.  The value
should be a list starting with the program's executable file name,
followed by strings to give to the program as its arguments.  If
the first element of the list is @code{nil}, Emacs opens a new
pseudoterminal (pty) and associates its input and output with
@var{buffer}, without actually running any program; the rest of the
list elements are ignored in that case.

@item :coding @var{coding}
If @var{coding} is a symbol, it specifies the coding system to be
used for both reading and writing of data from and to the
connection.  If @var{coding} is a cons cell
@w{@code{(@var{decoding} . @var{encoding})}}, then @var{decoding}
will be used for reading and @var{encoding} for writing.  The coding
system used for encoding the data written to the program is also used
for encoding the command-line arguments (but not the program itself,
whose file name is encoded as any other file name; @pxref{Encoding and
I/O, file-name-coding-system}).

If @var{coding} is @code{nil}, the default rules for finding the
coding system will apply.  @xref{Default Coding Systems}.

664
@item :connection-type @var{type}
665 666 667 668 669 670
Initialize the type of device used to communicate with the subprocess.
Possible values are @code{pty} to use a pty, @code{pipe} to use a
pipe, or @code{nil} to use the default derived from the value of the
@code{process-connection-type} variable.  This parameter and the value
of @code{process-connection-type} are ignored if a non-@code{nil}
value is specified for the @code{:stderr} parameter; in that case, the
671 672 673
type will always be @code{pipe}.  On systems where ptys are not
available (MS-Windows), this parameter is likewise ignored, and pipes
are used unconditionally.
674 675 676 677 678 679

@item :noquery @var{query-flag}
Initialize the process query flag to @var{query-flag}.
@xref{Query Before Exit}.

@item :stop @var{stopped}
680 681 682 683 684
If provided, @var{stopped} must be @code{nil}; it is an error to use
any non-@code{nil} value.  The @code{:stop} key is ignored otherwise
and is retained for compatibility with other process types such as
pipe processes.  Asynchronous subprocesses never start in the stopped
state.
685 686 687 688 689 690 691 692 693 694 695 696 697 698

@item :filter @var{filter}
Initialize the process filter to @var{filter}.  If not specified, a
default filter will be provided, which can be overridden later.
@xref{Filter Functions}.

@item :sentinel @var{sentinel}
Initialize the process sentinel to @var{sentinel}.  If not specified,
a default sentinel will be used, which can be overridden later.
@xref{Sentinels}.

@item :stderr @var{stderr}
Associate @var{stderr} with the standard error of the process.  A
non-@code{nil} value should be either a buffer or a pipe process
699 700 701
created with @code{make-pipe-process}, described below.  If
@var{stderr} is @code{nil}, standard error is mixed with standard
output, and both are sent to @var{buffer} or @var{filter}.
702

703 704 705 706 707 708 709 710 711 712 713 714 715
@cindex standard error process
If @var{stderr} is a buffer, Emacs will create a pipe process, the
@dfn{standard error process}.  This process will have the default
filter (@pxref{Filter Functions}), sentinel (@pxref{Sentinels}), and
coding systems (@pxref{Default Coding Systems}).  On the other hand,
it will use @var{query-flag} as its query-on-exit flag (@pxref{Query
Before Exit}).  It will be associated with the @var{stderr} buffer
(@pxref{Process Buffers}) and send its output (which is the standard
error of the main process) there.

If @var{stderr} is a pipe process, Emacs will use it as standard error
process for the new process.

716 717 718
@item :file-handler @var{file-handler}
If @var{file-handler} is non-@code{nil}, then look for a file name
handler for the current buffer's @code{default-directory}, and invoke
719 720
that file name handler to make the process.  If there is no such
handler, proceed as if @var{file-handler} were @code{nil}.
721 722 723 724
@end table

The original argument list, modified with the actual connection
information, is available via the @code{process-contact} function.
725 726 727

The current working directory of the subprocess is set to the current
buffer's value of @code{default-directory} if that is local (as
728 729 730 731 732 733 734 735
determined by @code{unhandled-file-name-directory}), or @file{~}
otherwise.  If you want to run a process in a remote directory, pass
@code{:file-handler t} to @code{make-process}.  In that case, the
current working directory is the local name component of
@code{default-directory} (as determined by @code{file-local-name}).

Depending on the implementation of the file name handler, it might not
be possible to apply @var{filter} or @var{sentinel} to the resulting
736 737 738 739 740
process object.  The @code{:stderr} argument cannot be a pipe process,
file name handlers do not support pipe processes for this.  A buffer
as @code{:stderr} argument is accepted, its contents is shown without
the use of pipe processes.  @xref{Filter Functions}, @ref{Sentinels},
and @ref{Accepting Output}.
741 742 743

Some file name handlers may not support @code{make-process}.  In such
cases, this function does nothing and returns @code{nil}.
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
@end defun

@defun make-pipe-process &rest args
This function creates a bidirectional pipe which can be attached to a
child process.  This is useful with the @code{:stderr} keyword of
@code{make-process}.  The function returns a process object.

The arguments @var{args} are a list of keyword/argument pairs.
Omitting a keyword is always equivalent to specifying it with value
@code{nil}.

Here are the meaningful keywords:

@table @asis
@item :name @var{name}
Use the string @var{name} as the process name.  As with
@code{make-process}, it is modified if necessary to make it unique.

@item :buffer @var{buffer}
Use @var{buffer} as the process buffer.

@item :coding @var{coding}
If @var{coding} is a symbol, it specifies the coding system to be
used for both reading and writing of data from and to the
connection.  If @var{coding} is a cons cell
@w{@code{(@var{decoding} . @var{encoding})}}, then @var{decoding}
will be used for reading and @var{encoding} for writing.

If @var{coding} is @code{nil}, the default rules for finding the
coding system will apply.  @xref{Default Coding Systems}.

@item :noquery @var{query-flag}
Initialize the process query flag to @var{query-flag}.
@xref{Query Before Exit}.

@item :stop @var{stopped}
780 781 782 783 784
If @var{stopped} is non-@code{nil}, start the process in the stopped
state.  In the stopped state, a pipe process does not accept incoming
data, but you can send outgoing data.  The stopped state is set by
@code{stop-process} and cleared by @code{continue-process}
(@pxref{Signals to Processes}).
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

@item :filter @var{filter}
Initialize the process filter to @var{filter}.  If not specified, a
default filter will be provided, which can be changed later.
@xref{Filter Functions}.

@item :sentinel @var{sentinel}
Initialize the process sentinel to @var{sentinel}.  If not specified,
a default sentinel will be used, which can be changed later.
@xref{Sentinels}.
@end table

The original argument list, modified with the actual connection
information, is available via the @code{process-contact} function.
@end defun
Glenn Morris's avatar
Glenn Morris committed
800 801

@defun start-process name buffer-or-name program &rest args
802 803 804 805 806 807 808 809
This function is a higher-level wrapper around @code{make-process},
exposing an interface that is similar to @code{call-process}.  It
creates a new asynchronous subprocess and starts the specified
@var{program} running in it.  It returns a process object that stands
for the new subprocess in Lisp.  The argument @var{name} specifies the
name for the process object; as with @code{make-process}, it is
modified if necessary to make it unique.  The buffer
@var{buffer-or-name} is the buffer to associate with the process.
Glenn Morris's avatar
Glenn Morris committed
810

811 812 813 814 815
If @var{program} is @code{nil}, Emacs opens a new pseudoterminal (pty)
and associates its input and output with @var{buffer-or-name}, without
creating a subprocess.  In that case, the remaining arguments
@var{args} are ignored.

816 817
The rest of @var{args} are strings that specify command line arguments
for the subprocess.
Glenn Morris's avatar
Glenn Morris committed
818 819

In the example below, the first process is started and runs (rather,
820 821
sleeps) for 100 seconds (the output buffer @samp{foo} is created
immediately).  Meanwhile, the second process is started, and
Glenn Morris's avatar
Glenn Morris committed
822 823 824 825 826 827 828 829 830 831 832 833 834
given the name @samp{my-process<1>} for the sake of uniqueness.  It
inserts the directory listing at the end of the buffer @samp{foo},
before the first process finishes.  Then it finishes, and a message to
that effect is inserted in the buffer.  Much later, the first process
finishes, and another message is inserted in the buffer for it.

@smallexample
@group
(start-process "my-process" "foo" "sleep" "100")
     @result{} #<process my-process>
@end group

@group
835
(start-process "my-process" "foo" "ls" "-l" "/bin")
Glenn Morris's avatar
Glenn Morris committed
836 837 838
     @result{} #<process my-process<1>>

---------- Buffer: foo ----------
839 840 841 842 843
total 8336
-rwxr-xr-x 1 root root 971384 Mar 30 10:14 bash
-rwxr-xr-x 1 root root 146920 Jul  5  2011 bsd-csh
@dots{}
-rwxr-xr-x 1 root root 696880 Feb 28 15:55 zsh4
Glenn Morris's avatar
Glenn Morris committed
844 845 846 847 848 849 850 851 852 853 854 855

Process my-process<1> finished

Process my-process finished
---------- Buffer: foo ----------
@end group
@end smallexample
@end defun

@defun start-file-process name buffer-or-name program &rest args
Like @code{start-process}, this function starts a new asynchronous
subprocess running @var{program} in it, and returns its process
856
object.
Glenn Morris's avatar
Glenn Morris committed
857

858
The difference from @code{start-process} is that this function may
859
invoke a file name handler based on the value of @code{default-directory}.
860 861 862 863
This handler ought to run @var{program}, perhaps on the local host,
perhaps on a remote host that corresponds to @code{default-directory}.
In the latter case, the local part of @code{default-directory} becomes
the working directory of the process.
Glenn Morris's avatar
Glenn Morris committed
864 865

This function does not try to invoke file name handlers for
866 867 868 869 870 871
@var{program} or for the rest of @var{args}.  For that reason, if
@var{program} or any of @var{args} use the remote-file syntax
(@pxref{Magic File Names}), they must be converted either to file
names relative to @code{default-directory}, or to names that identify
the files locally on the remote host, by running them through
@code{file-local-name}.
Glenn Morris's avatar
Glenn Morris committed
872

873
Depending on the implementation of the file name handler, it might not be
Glenn Morris's avatar
Glenn Morris committed
874
possible to apply @code{process-filter} or @code{process-sentinel} to
875
the resulting process object.  @xref{Filter Functions}, and @ref{Sentinels}.
Glenn Morris's avatar
Glenn Morris committed
876

877
@c FIXME  Can we find a better example (i.e., a more modern function
878
@c that is actually documented).
879
Some file name handlers may not support @code{start-file-process} (for
880 881
example the function @code{ange-ftp-hook-function}).  In such cases,
this function does nothing and returns @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
882 883
@end defun

884
@defun start-process-shell-command name buffer-or-name command
885 886 887 888
This function is like @code{start-process}, except that it uses a
shell to execute the specified @var{command}.  The argument
@var{command} is a shell command string.  The variable
@code{shell-file-name} specifies which shell to use.
Glenn Morris's avatar
Glenn Morris committed
889 890

The point of running a program through the shell, rather than directly
891 892 893 894 895 896 897 898
with @code{make-process} or @code{start-process}, is so that you can
employ shell features such as wildcards in the arguments.  It follows
that if you include any arbitrary user-specified arguments in the
command, you should quote them with @code{shell-quote-argument} first,
so that any special shell characters do @emph{not} have their special
shell meanings.  @xref{Shell Arguments}.  Of course, when executing
commands based on user input you should also consider the security
implications.
Glenn Morris's avatar
Glenn Morris committed
899 900
@end defun

901
@defun start-file-process-shell-command name buffer-or-name command
Glenn Morris's avatar
Glenn Morris committed
902
This function is like @code{start-process-shell-command}, but uses
903 904
@code{start-file-process} internally.  Because of this, @var{command}
can also be executed on remote hosts, depending on @code{default-directory}.
Glenn Morris's avatar
Glenn Morris committed
905 906 907 908
@end defun

@defvar process-connection-type
This variable controls the type of device used to communicate with
909
asynchronous subprocesses.  If it is non-@code{nil}, then ptys are
Glenn Morris's avatar
Glenn Morris committed
910 911 912
used, when available.  Otherwise, pipes are used.

The value of @code{process-connection-type} takes effect when
913 914 915 916 917 918 919
@code{make-process} or @code{start-process} is called.  So you can
specify how to communicate with one subprocess by binding the variable
around the call to these functions.

Note that the value of this variable is ignored when
@code{make-process} is called with a non-@code{nil} value of the
@code{:stderr} parameter; in that case, Emacs will communicate with
920 921
the process using pipes.  It is also ignored if ptys are unavailable
(MS-Windows).
Glenn Morris's avatar
Glenn Morris committed
922 923 924

@smallexample
@group
925
(let ((process-connection-type nil))  ; @r{use a pipe}
Glenn Morris's avatar
Glenn Morris committed
926 927 928 929
  (start-process @dots{}))
@end group
@end smallexample

930 931
To determine whether a given subprocess actually got a pipe or a pty,
use the function @code{process-tty-name} (@pxref{Process
Glenn Morris's avatar
Glenn Morris committed
932 933 934 935 936 937 938 939 940 941
Information}).
@end defvar

@node Deleting Processes
@section Deleting Processes
@cindex deleting processes

  @dfn{Deleting a process} disconnects Emacs immediately from the
subprocess.  Processes are deleted automatically after they terminate,
but not necessarily right away.  You can delete a process explicitly
942
at any time.  If you explicitly delete a terminated process before it
Glenn Morris's avatar
Glenn Morris committed
943
is deleted automatically, no harm results.  Deleting a running
944
process sends a signal to terminate it (and its child processes, if
945
any), and calls the process sentinel.  @xref{Sentinels}.
Glenn Morris's avatar
Glenn Morris committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

  When a process is deleted, the process object itself continues to
exist as long as other Lisp objects point to it.  All the Lisp
primitives that work on process objects accept deleted processes, but
those that do I/O or send signals will report an error.  The process
mark continues to point to the same place as before, usually into a
buffer where output from the process was being inserted.

@defopt delete-exited-processes
This variable controls automatic deletion of processes that have
terminated (due to calling @code{exit} or to a signal).  If it is
@code{nil}, then they continue to exist until the user runs
@code{list-processes}.  Otherwise, they are deleted immediately after
they exit.
@end defopt

@defun delete-process process
This function deletes a process, killing it with a @code{SIGKILL}
964 965 966 967 968 969 970 971 972 973 974 975 976
signal if the process was running a program.  The argument may be a
process, the name of a process, a buffer, or the name of a buffer.  (A
buffer or buffer-name stands for the process that
@code{get-buffer-process} returns.)  Calling @code{delete-process} on
a running process terminates it, updates the process status, and runs
the sentinel immediately.  If the process has already terminated,
calling @code{delete-process} has no effect on its status, or on the
running of its sentinel (which will happen sooner or later).

If the process object represents a network, serial, or pipe
connection, its status changes to @code{closed}; otherwise, it changes
to @code{signal}, unless the process already exited.  @xref{Process
Information, process-status}.
Glenn Morris's avatar
Glenn Morris committed
977 978 979 980 981 982 983 984 985 986 987

@smallexample
@group
(delete-process "*shell*")
     @result{} nil
@end group
@end smallexample
@end defun

@node Process Information
@section Process Information
988
@cindex process information
Glenn Morris's avatar
Glenn Morris committed
989 990 991

  Several functions return information about processes.

992
@deffn Command list-processes &optional query-only buffer
Glenn Morris's avatar
Glenn Morris committed
993 994 995 996
This command displays a listing of all living processes.  In addition,
it finally deletes any process whose status was @samp{Exited} or
@samp{Signaled}.  It returns @code{nil}.

997 998 999
The processes are shown in a buffer named @file{*Process List*}
(unless you specify otherwise using the optional argument @var{buffer}),
whose major mode is Process Menu mode.
1000

1001
If @var{query-only} is non-@code{nil}, it only lists processes
Glenn Morris's avatar
Glenn Morris committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
whose query flag is non-@code{nil}.  @xref{Query Before Exit}.
@end deffn

@defun process-list
This function returns a list of all processes that have not been deleted.

@smallexample
@group
(process-list)
     @result{} (#<process display-time> #<process shell>)
@end group
@end smallexample
@end defun

@defun get-process name
1017
This function returns the process named @var{name} (a string), or
1018 1019
@code{nil} if there is none.  The argument @var{name} can also be a
process object, in which case it is returned.
Glenn Morris's avatar
Glenn Morris committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

@smallexample
@group
(get-process "shell")
     @result{} #<process shell>
@end group
@end smallexample
@end defun

@defun process-command process
This function returns the command that was executed to start
@var{process}.  This is a list of strings, the first string being the
program executed and the rest of the strings being the arguments that
1033 1034 1035
were given to the program.  For a network, serial, or pipe connection,
this is either @code{nil}, which means the process is running or
@code{t} (process is stopped).
Glenn Morris's avatar
Glenn Morris committed
1036 1037 1038 1039

@smallexample
@group
(process-command (get-process "shell"))
1040
     @result{} ("bash" "-i")
Glenn Morris's avatar
Glenn Morris committed
1041 1042 1043 1044
@end group
@end smallexample
@end defun

1045
@defun process-contact process &optional key
1046 1047 1048 1049 1050 1051
This function returns information about how a network, a serial, or a
pipe connection was set up.  When @var{key} is @code{nil}, it returns
@code{(@var{hostname} @var{service})} for a network connection,
@code{(@var{port} @var{speed})} for a serial connection, and @code{t}
for a pipe connection.  For an ordinary child process, this function
always returns @code{t} when called with a @code{nil} @var{key}.
1052 1053

If @var{key} is @code{t}, the value is the complete status information
1054 1055 1056 1057 1058
for the connection, server, serial port, or pipe; that is, the list of
keywords and values specified in @code{make-network-process},
@code{make-serial-process}, or @code{make-pipe-process}, except that
some of the values represent the current status instead of what you
specified.
1059

1060 1061
For a network process, the values include (see
@code{make-network-process} for a complete list):
1062 1063 1064 1065 1066

@table @code
@item :buffer
The associated value is the process buffer.
@item :filter
1067 1068
The associated value is the process filter function.  @xref{Filter
Functions}.
1069
@item :sentinel
1070
The associated value is the process sentinel function.  @xref{Sentinels}.
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
@item :remote
In a connection, the address in internal format of the remote peer.
@item :local
The local address, in internal format.
@item :service
In a server, if you specified @code{t} for @var{service},
this value is the actual port number.
@end table

@code{:local} and @code{:remote} are included even if they were not
specified explicitly in @code{make-network-process}.

1083 1084 1085
For a serial connection, see @code{make-serial-process} and
@code{serial-process-configure} for the list of keys.  For a pipe
connection, see @code{make-pipe-process} for the list of keys.
1086 1087 1088 1089 1090

If @var{key} is a keyword, the function returns the value corresponding
to that keyword.
@end defun

Glenn Morris's avatar
Glenn Morris committed
1091 1092
@defun process-id process
This function returns the @acronym{PID} of @var{process}.  This is an
1093 1094 1095 1096 1097 1098
integral number that distinguishes the process @var{process} from all
other processes running on the same computer at the current time.  The
@acronym{PID} of a process is chosen by the operating system kernel
when the process is started and remains constant as long as the
process exists.  For network, serial, and pipe connections, this
function returns @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
1099 1100 1101
@end defun

@defun process-name process
1102
This function returns the name of @var{process}, as a string.
Glenn Morris's avatar
Glenn Morris committed
1103 1104 1105 1106
@end defun

@defun process-status process-name
This function returns the status of @var{process-name} as a symbol.
1107 1108
The argument @var{process-name} must be a process, a buffer, or a
process name (a string).
Glenn Morris's avatar
Glenn Morris committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

The possible values for an actual subprocess are:

@table @code
@item run
for a process that is running.
@item stop
for a process that is stopped but continuable.
@item exit
for a process that has exited.
@item signal
for a process that has received a fatal signal.
@item open
1122
for a network, serial, or pipe connection that is open.
Glenn Morris's avatar
Glenn Morris committed
1123
@item closed
1124 1125 1126
for a network, serial, or pipe connection that is closed.  Once a
connection is closed, you cannot reopen it, though you might be able
to open a new connection to the same place.
Glenn Morris's avatar
Glenn Morris committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
@item connect
for a non-blocking connection that is waiting to complete.
@item failed
for a non-blocking connection that has failed to complete.
@item listen
for a network server that is listening.
@item nil
if @var{process-name} is not the name of an existing process.
@end table

@smallexample
@group
(process-status (get-buffer "*shell*"))
     @result{} run
@end group
@end smallexample

1144 1145 1146 1147 1148
For a network, serial, or pipe connection, @code{process-status}
returns one of the symbols @code{open}, @code{stop}, or @code{closed}.
The latter means that the other side closed the connection, or Emacs
did @code{delete-process}.  The value @code{stop} means that
@code{stop-process} was called on the connection.
Glenn Morris's avatar
Glenn Morris committed
1149 1150
@end defun

1151
@defun process-live-p process
1152
This function returns non-@code{nil} if @var{process} is alive.  A
1153 1154 1155 1156
process is considered alive if its status is @code{run}, @code{open},
@code{listen}, @code{connect} or @code{stop}.
@end defun

1157 1158
@defun process-type process
This function returns the symbol @code{network} for a network
1159 1160 1161
connection or server, @code{serial} for a serial port connection,
@code{pipe} for a pipe connection, or @code{real} for a subprocess
created for running a program.
1162 1163
@end defun

Glenn Morris's avatar
Glenn Morris committed
1164 1165 1166 1167
@defun process-exit-status process
This function returns the exit status of @var{process} or the signal
number that killed it.  (Use the result of @code{process-status} to
determine which of those it is.)  If @var{process} has not yet
1168 1169 1170
terminated, the value is 0.  For network, serial, and pipe connections
that are already closed, the value is either 0 or 256, depending on
whether the connection was closed normally or abnormally.
Glenn Morris's avatar
Glenn Morris committed
1171 1172 1173 1174 1175
@end defun

@defun process-tty-name process
This function returns the terminal name that @var{process} is using for
its communication with Emacs---or @code{nil} if it is using pipes
1176
instead of a pty (see @code{process-connection-type} in
1177 1178
@ref{Asynchronous Processes}).  If @var{process} represents a program
running on a remote host, the terminal name used by that program on
1179 1180 1181
the remote host is provided as process property @code{remote-tty}.  If
@var{process} represents a network, serial, or pipe connection, the
value is @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
1182 1183 1184 1185
@end defun

@defun process-coding-system process
@anchor{Coding systems for a subprocess}
1186 1187 1188
This function returns a cons cell @code{(@var{decode} . @var{encode})},
describing the coding systems in use for decoding output from, and
encoding input to, @var{process} (@pxref{Coding Systems}).
Glenn Morris's avatar
Glenn Morris committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
@end defun

@defun set-process-coding-system process &optional decoding-system encoding-system
This function specifies the coding systems to use for subsequent output
from and input to @var{process}.  It will use @var{decoding-system} to
decode subprocess output, and @var{encoding-system} to encode subprocess
input.
@end defun

  Every process also has a property list that you can use to store
miscellaneous values associated with the process.

@defun process-get process propname
This function returns the value of the @var{propname} property
of @var{process}.
@end defun

@defun process-put process propname value
This function sets the value of the @var{propname} property
of @var{process} to @var{value}.
@end defun

@defun process-plist process
This function returns the process plist of @var{process}.
@end defun

@defun set-process-plist process plist
This function sets the process plist of @var{process} to @var{plist}.
@end defun

@node Input to Processes
@section Sending Input to Processes
@cindex process input

  Asynchronous subprocesses receive input when it is sent to them by
Emacs, which is done with the functions in this section.  You must
1225 1226 1227 1228
specify the process to send input to, and the input data to send.  If
the subprocess runs a program, the data appears on the standard input
of that program; for connections, the data is sent to the connected
device or program.
Glenn Morris's avatar
Glenn Morris committed
1229

1230
@c FIXME which?
Glenn Morris's avatar
Glenn Morris committed
1231
  Some operating systems have limited space for buffered input in a
1232 1233 1234
pty.  On these systems, Emacs sends an @acronym{EOF} periodically
amidst the other characters, to force them through.  For most
programs, these @acronym{EOF}s do no harm.
Glenn Morris's avatar
Glenn Morris committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

  Subprocess input is normally encoded using a coding system before the
subprocess receives it, much like text written into a file.  You can use
@code{set-process-coding-system} to specify which coding system to use
(@pxref{Process Information}).  Otherwise, the coding system comes from
@code{coding-system-for-write}, if that is non-@code{nil}; or else from
the defaulting mechanism (@pxref{Default Coding Systems}).

  Sometimes the system is unable to accept input for that process,
because the input buffer is full.  When this happens, the send functions
wait a short while, accepting output from subprocesses, and then try
again.  This gives the subprocess a chance to read more of its pending
1247 1248 1249
input and make space in the buffer.  It also allows filters (including
the one currently running), sentinels and timers to run---so take
account of that in writing your code.
Glenn Morris's avatar
Glenn Morris committed
1250 1251 1252 1253 1254 1255 1256 1257

  In these functions, the @var{process} argument can be a process or
the name of a process, or a buffer or buffer name (which stands
for a process via @code{get-buffer-process}).  @code{nil} means
the current buffer's process.

@defun process-send-string process string
This function sends @var{process} the contents of @var{string} as
1258 1259
standard input.  It returns @code{nil}.  For example, to make a
Shell buffer list files:
Glenn Morris's avatar
Glenn Morris committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

@smallexample
@group
(process-send-string "shell<1>" "ls\n")
     @result{} nil
@end group
@end smallexample
@end defun

@defun process-send-region process start end
This function sends the text in the region defined by @var{start} and
@var{end} as standard input to @var{process}.

An error is signaled unless both @var{start} and @var{end} are
integers or markers that indicate positions in the current buffer.  (It
is unimportant which number is larger.)
@end defun

@defun process-send-eof &optional process
This function makes @var{process} see an end-of-file in its
input.  The @acronym{EOF} comes after any text already sent to it.
The function returns @var{process}.

@smallexample
@group
(process-send-eof "shell")
     @result{} "shell"
@end group
@end smallexample
@end defun

1291
@defun process-running-child-p &optional process
1292 1293 1294 1295 1296 1297 1298 1299
This function will tell you whether a @var{process}, which must not be
a connection but a real subprocess, has given control of its terminal
to a child process of its own.  If this is true, the function returns
the numeric ID of the foreground process group of @var{process}; it
returns @code{nil} if Emacs can be certain that this is not so.  The
value is @code{t} if Emacs cannot tell whether this is true.  This
function signals an error if @var{process} is a network, serial, or
pipe connection, or is the subprocess is not active.
Glenn Morris's avatar
Glenn Morris committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
@end defun

@node Signals to Processes
@section Sending Signals to Processes
@cindex process signals
@cindex sending signals
@cindex signals

  @dfn{Sending a signal} to a subprocess is a way of interrupting its
activities.  There are several different signals, each with its own
meaning.  The set of signals and their names is defined by the operating
system.  For example, the signal @code{SIGINT} means that the user has
typed @kbd{C-c}, or that some analogous thing has happened.

  Each signal has a standard effect on the subprocess.  Most signals
1315
kill the subprocess, but some stop (or resume) execution instead.  Most
Glenn Morris's avatar
Glenn Morris committed
1316 1317 1318 1319 1320 1321 1322 1323
signals can optionally be handled by programs; if the program handles
the signal, then we can say nothing in general about its effects.

  You can send signals explicitly by calling the functions in this
section.  Emacs also sends signals automatically at certain times:
killing a buffer sends a @code{SIGHUP} signal to all its associated
processes; killing Emacs sends a @code{SIGHUP} signal to all remaining
processes.  (@code{SIGHUP} is a signal that usually indicates that the
1324
user ``hung up the phone'', i.e., disconnected.)
Glenn Morris's avatar
Glenn Morris committed
1325 1326 1327 1328 1329 1330 1331

  Each of the signal-sending functions takes two optional arguments:
@var{process} and @var{current-group}.

  The argument @var{process} must be either a process, a process
name, a buffer, a buffer name, or @code{nil}.  A buffer or buffer name
stands for a process through @code{get-buffer-process}.  @code{nil}
1332 1333 1334 1335
stands for the process associated with the current buffer.  Except
with @code{stop-process} and @code{continue-process}, an error is
signaled if @var{process} does not identify an active process, or if
it represents a network, serial, or pipe connection.
Glenn Morris's avatar
Glenn Morris committed
1336 1337 1338 1339 1340 1341

  The argument @var{current-group} is a flag that makes a difference
when you are running a job-control shell as an Emacs subprocess.  If it
is non-@code{nil}, then the signal is sent to the current process-group
of the terminal that Emacs uses to communicate with the subprocess.  If
the process is a job-control shell, this means the shell's current
1342 1343 1344 1345 1346 1347
subjob.  If @var{current-group} is @code{nil}, the signal is
sent to the process group of the immediate subprocess of Emacs.  If
the subprocess is a job-control shell, this is the shell itself.  If
@var{current-group} is @code{lambda}, the signal is sent to the
process-group that owns the terminal, but only if it is not the shell
itself.
Glenn Morris's avatar
Glenn Morris committed
1348 1349 1350 1351 1352 1353 1354 1355 1356

  The flag @var{current-group} has no effect when a pipe is used to
communicate with the subprocess, because the operating system does not
support the distinction in the case of pipes.  For the same reason,
job-control shells won't work when a pipe is used.  See
@code{process-connection-type} in @ref{Asynchronous Processes}.

@defun interrupt-process &optional process current-group
This function interrupts the process @var{process} by sending the
Paul Eggert's avatar
Paul Eggert committed
1357 1358
signal @code{SIGINT}.  Outside of Emacs, typing the interrupt
character (normally @kbd{C-c} on some systems, and @key{DEL} on
Glenn Morris's avatar
Glenn Morris committed
1359
others) sends this signal.  When the argument @var{current-group} is
Paul Eggert's avatar
Paul Eggert committed
1360
non-@code{nil}, you can think of this function as typing @kbd{C-c}
Glenn Morris's avatar
Glenn Morris committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
on the terminal by which Emacs talks to the subprocess.
@end defun

@defun kill-process &optional process current-group
This function kills the process @var{process} by sending the
signal @code{SIGKILL}.  This signal kills the subprocess immediately,
and cannot be handled by the subprocess.
@end defun

@defun quit-process &optional process current-group
This function sends the signal @code{SIGQUIT} to the process
Paul Eggert's avatar
Paul Eggert committed
1372 1373
@var{process}.  This signal is the one sent by the quit
character (usually @kbd{C-\}) when you are not inside
Glenn Morris's avatar
Glenn Morris committed
1374 1375 1376 1377
Emacs.
@end defun

@defun stop-process &optional process current-group
1378 1379 1380 1381 1382 1383
This function stops the specified @var{process}.  If it is a real
subprocess running a program, it sends the signal @code{SIGTSTP} to
that subprocess.  If @var{process} represents a network, serial, or
pipe connection, this function inhibits handling of the incoming data
from the connection; for a network server, this means not accepting
new connections.  Use @code{continue-process} to resume normal
Glenn Morris's avatar
Glenn Morris committed
1384 1385
execution.

Paul Eggert's avatar
Paul Eggert committed
1386
Outside of Emacs, on systems with job control, the stop character
1387 1388 1389 1390
(usually @kbd{C-z}) normally sends the @code{SIGTSTP} signal to a
subprocess.  When @var{current-group} is non-@code{nil}, you can think
of this function as typing @kbd{C-z} on the terminal Emacs uses to
communicate with the subprocess.
Glenn Morris's avatar
Glenn Morris committed
1391 1392 1393
@end defun

@defun continue-process &optional process current-group
1394 1395 1396 1397 1398 1399 1400
This function resumes execution of the process @var{process}.  If it
is a real subprocess running a program, it sends the signal
@code{SIGCONT} to that subprocess; this presumes that @var{process}
was stopped previously.  If @var{process} represents a network,
serial, or pipe connection, this function resumes handling of the
incoming data from the connection.  For serial connections, data that
arrived during the time the process was stopped might be lost.
Glenn Morris's avatar
Glenn Morris committed
1401 1402
@end defun

1403
@deffn Command signal-process process signal
Glenn Morris's avatar
Glenn Morris committed
1404
This function sends a signal to process @var{process}.  The argument
1405 1406
@var{signal} specifies which signal to send; it should be an integer,
or a symbol whose name is a signal.
Glenn Morris's avatar
Glenn Morris committed
1407

1408 1409 1410
The @var{process} argument can be a system process @acronym{ID} (an
integer); that allows you to send signals to processes that are not
children of Emacs.  @xref{System Processes}.
<