Fix bug #7576 with lack of index entries for character syntax.
[bpt/emacs.git] / doc / lispref / processes.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001,
d24880de
GM
4@c 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5@c Free Software Foundation, Inc.
b8d4c8d0 6@c See the file elisp.texi for copying conditions.
6336d8c3 7@setfilename ../../info/processes
b8d4c8d0
GM
8@node Processes, Display, Abbrevs, Top
9@chapter Processes
10@cindex child process
11@cindex parent process
12@cindex subprocess
13@cindex process
14
15 In the terminology of operating systems, a @dfn{process} is a space in
16which a program can execute. Emacs runs in a process. Emacs Lisp
17programs can invoke other programs in processes of their own. These are
18called @dfn{subprocesses} or @dfn{child processes} of the Emacs process,
19which is their @dfn{parent process}.
20
21 A subprocess of Emacs may be @dfn{synchronous} or @dfn{asynchronous},
22depending on how it is created. When you create a synchronous
23subprocess, the Lisp program waits for the subprocess to terminate
24before continuing execution. When you create an asynchronous
25subprocess, it can run in parallel with the Lisp program. This kind of
26subprocess is represented within Emacs by a Lisp object which is also
27called a ``process.'' Lisp programs can use this object to communicate
28with the subprocess or to control it. For example, you can send
29signals, obtain status information, receive output from the process, or
30send input to it.
31
32@defun processp object
23dd4ecd
EZ
33This function returns @code{t} if @var{object} represents an Emacs
34subprocess, @code{nil} otherwise.
b8d4c8d0
GM
35@end defun
36
23dd4ecd
EZ
37 In addition to subprocesses of the current Emacs session, you can
38also access other processes running on your machine. @xref{System
39Processes}.
40
b8d4c8d0
GM
41@menu
42* Subprocess Creation:: Functions that start subprocesses.
43* Shell Arguments:: Quoting an argument to pass it to a shell.
44* Synchronous Processes:: Details of using synchronous subprocesses.
45* Asynchronous Processes:: Starting up an asynchronous subprocess.
46* Deleting Processes:: Eliminating an asynchronous subprocess.
47* Process Information:: Accessing run-status and other attributes.
48* Input to Processes:: Sending input to an asynchronous subprocess.
49* Signals to Processes:: Stopping, continuing or interrupting
50 an asynchronous subprocess.
51* Output from Processes:: Collecting output from an asynchronous subprocess.
52* Sentinels:: Sentinels run when process run-status changes.
53* Query Before Exit:: Whether to query if exiting will kill a process.
23dd4ecd 54* System Processes:: Accessing other processes running on your system.
d24880de 55* Transaction Queues:: Transaction-based communication with subprocesses.
b8d4c8d0
GM
56* Network:: Opening network connections.
57* Network Servers:: Network servers let Emacs accept net connections.
58* Datagrams:: UDP network connections.
59* Low-Level Network:: Lower-level but more general function
60 to create connections and servers.
d24880de 61* Misc Network:: Additional relevant functions for net connections.
c73e02fa 62* Serial Ports:: Communicating with serial ports.
b8d4c8d0
GM
63* Byte Packing:: Using bindat to pack and unpack binary data.
64@end menu
65
66@node Subprocess Creation
67@section Functions that Create Subprocesses
68
583d8b3c 69 There are three primitives that create a new subprocess in which to run
b8d4c8d0
GM
70a program. One of them, @code{start-process}, creates an asynchronous
71process and returns a process object (@pxref{Asynchronous Processes}).
72The other two, @code{call-process} and @code{call-process-region},
73create a synchronous process and do not return a process object
74(@pxref{Synchronous Processes}).
75
76 Synchronous and asynchronous processes are explained in the following
77sections. Since the three functions are all called in a similar
78fashion, their common arguments are described here.
79
80@cindex execute program
81@cindex @code{PATH} environment variable
82@cindex @code{HOME} environment variable
83 In all cases, the function's @var{program} argument specifies the
84program to be run. An error is signaled if the file is not found or
85cannot be executed. If the file name is relative, the variable
86@code{exec-path} contains a list of directories to search. Emacs
87initializes @code{exec-path} when it starts up, based on the value of
88the environment variable @code{PATH}. The standard file name
89constructs, @samp{~}, @samp{.}, and @samp{..}, are interpreted as
90usual in @code{exec-path}, but environment variable substitutions
91(@samp{$HOME}, etc.) are not recognized; use
92@code{substitute-in-file-name} to perform them (@pxref{File Name
93Expansion}). @code{nil} in this list refers to
94@code{default-directory}.
95
96 Executing a program can also try adding suffixes to the specified
97name:
98
99@defvar exec-suffixes
100This variable is a list of suffixes (strings) to try adding to the
101specified program file name. The list should include @code{""} if you
102want the name to be tried exactly as specified. The default value is
103system-dependent.
104@end defvar
105
106 @strong{Please note:} The argument @var{program} contains only the
107name of the program; it may not contain any command-line arguments. You
108must use @var{args} to provide those.
109
110 Each of the subprocess-creating functions has a @var{buffer-or-name}
111argument which specifies where the standard output from the program will
112go. It should be a buffer or a buffer name; if it is a buffer name,
113that will create the buffer if it does not already exist. It can also
114be @code{nil}, which says to discard the output unless a filter function
115handles it. (@xref{Filter Functions}, and @ref{Read and Print}.)
116Normally, you should avoid having multiple processes send output to the
117same buffer because their output would be intermixed randomly.
118
119@cindex program arguments
120 All three of the subprocess-creating functions have a @code{&rest}
121argument, @var{args}. The @var{args} must all be strings, and they are
122supplied to @var{program} as separate command line arguments. Wildcard
123characters and other shell constructs have no special meanings in these
124strings, since the strings are passed directly to the specified program.
125
126 The subprocess gets its current directory from the value of
127@code{default-directory} (@pxref{File Name Expansion}).
128
129@cindex environment variables, subprocesses
130 The subprocess inherits its environment from Emacs, but you can
131specify overrides for it with @code{process-environment}. @xref{System
132Environment}.
133
134@defvar exec-directory
135@pindex movemail
136The value of this variable is a string, the name of a directory that
137contains programs that come with GNU Emacs, programs intended for Emacs
138to invoke. The program @code{movemail} is an example of such a program;
139Rmail uses it to fetch new mail from an inbox.
140@end defvar
141
142@defopt exec-path
143The value of this variable is a list of directories to search for
144programs to run in subprocesses. Each element is either the name of a
145directory (i.e., a string), or @code{nil}, which stands for the default
146directory (which is the value of @code{default-directory}).
147@cindex program directories
148
149The value of @code{exec-path} is used by @code{call-process} and
150@code{start-process} when the @var{program} argument is not an absolute
151file name.
152@end defopt
153
154@node Shell Arguments
155@section Shell Arguments
156@cindex arguments for shell commands
157@cindex shell command arguments
158
159 Lisp programs sometimes need to run a shell and give it a command
160that contains file names that were specified by the user. These
161programs ought to be able to support any valid file name. But the shell
162gives special treatment to certain characters, and if these characters
163occur in the file name, they will confuse the shell. To handle these
164characters, use the function @code{shell-quote-argument}:
165
166@defun shell-quote-argument argument
167This function returns a string which represents, in shell syntax,
168an argument whose actual contents are @var{argument}. It should
169work reliably to concatenate the return value into a shell command
170and then pass it to a shell for execution.
171
172Precisely what this function does depends on your operating system. The
173function is designed to work with the syntax of your system's standard
174shell; if you use an unusual shell, you will need to redefine this
175function.
176
177@example
178;; @r{This example shows the behavior on GNU and Unix systems.}
179(shell-quote-argument "foo > bar")
180 @result{} "foo\\ \\>\\ bar"
181
182;; @r{This example shows the behavior on MS-DOS and MS-Windows.}
183(shell-quote-argument "foo > bar")
184 @result{} "\"foo > bar\""
185@end example
186
187Here's an example of using @code{shell-quote-argument} to construct
188a shell command:
189
190@example
191(concat "diff -c "
192 (shell-quote-argument oldfile)
193 " "
194 (shell-quote-argument newfile))
195@end example
196@end defun
197
a873ee3d 198@cindex quoting and unquoting shell command line
0999039c
CY
199 The following two functions are useful for creating shell commands
200from individual argument strings, and taking shell command lines apart
201into individual arguments.
a873ee3d
EZ
202
203@defun split-string-and-unquote string &optional separators
204This function splits @var{string} into substrings at matches for the
205regular expression @var{separators}, like @code{split-string} does
0999039c
CY
206(@pxref{Creating Strings}); in addition, it removes quoting from the
207substrings. It then makes a list of the substrings and returns it.
a873ee3d 208
4185820c
CY
209If @var{separators} is omitted or @code{nil}, it defaults to
210@code{"\\s-+"}, which is a regular expression that matches one or more
211characters with whitespace syntax (@pxref{Syntax Class Table}).
a873ee3d 212
0999039c
CY
213This function performs two types of quoting: enclosing a whole string
214in double quotes @code{"@dots{}"}, and quoting individual characters
215with a backslash escape @samp{\}. The latter is also used in Lisp
216strings, so this function can handle those as well.
a873ee3d
EZ
217@end defun
218
219@defun combine-and-quote-strings list-of-strings &optional separator
220This function concatenates @var{list-of-strings} into a single string,
0999039c
CY
221quoting each string as necessary. It also sticks the @var{separator}
222string between each pair of strings; if @var{separator} is omitted or
223@code{nil}, it defaults to @code{" "}. The return value is the
224resulting string.
a873ee3d
EZ
225
226The strings in @var{list-of-strings} that need quoting are those that
227include @var{separator} as their substring. Quoting a string encloses
228it in double quotes @code{"@dots{}"}. In the simplest case, if you
229are consing a shell command from the individual command-line
230arguments, every argument that includes embedded blanks will be
231quoted.
232@end defun
233
b8d4c8d0
GM
234@node Synchronous Processes
235@section Creating a Synchronous Process
236@cindex synchronous subprocess
237
238 After a @dfn{synchronous process} is created, Emacs waits for the
239process to terminate before continuing. Starting Dired on GNU or
240Unix@footnote{On other systems, Emacs uses a Lisp emulation of
241@code{ls}; see @ref{Contents of Directories}.} is an example of this: it
242runs @code{ls} in a synchronous process, then modifies the output
243slightly. Because the process is synchronous, the entire directory
244listing arrives in the buffer before Emacs tries to do anything with it.
245
246 While Emacs waits for the synchronous subprocess to terminate, the
247user can quit by typing @kbd{C-g}. The first @kbd{C-g} tries to kill
248the subprocess with a @code{SIGINT} signal; but it waits until the
249subprocess actually terminates before quitting. If during that time the
250user types another @kbd{C-g}, that kills the subprocess instantly with
251@code{SIGKILL} and quits immediately (except on MS-DOS, where killing
252other processes doesn't work). @xref{Quitting}.
253
254 The synchronous subprocess functions return an indication of how the
255process terminated.
256
257 The output from a synchronous subprocess is generally decoded using a
258coding system, much like text read from a file. The input sent to a
259subprocess by @code{call-process-region} is encoded using a coding
260system, much like text written into a file. @xref{Coding Systems}.
261
262@defun call-process program &optional infile destination display &rest args
263This function calls @var{program} in a separate process and waits for
264it to finish.
265
266The standard input for the process comes from file @var{infile} if
267@var{infile} is not @code{nil}, and from the null device otherwise.
268The argument @var{destination} says where to put the process output.
269Here are the possibilities:
270
271@table @asis
272@item a buffer
273Insert the output in that buffer, before point. This includes both the
274standard output stream and the standard error stream of the process.
275
276@item a string
277Insert the output in a buffer with that name, before point.
278
279@item @code{t}
280Insert the output in the current buffer, before point.
281
282@item @code{nil}
283Discard the output.
284
285@item 0
286Discard the output, and return @code{nil} immediately without waiting
287for the subprocess to finish.
288
289In this case, the process is not truly synchronous, since it can run in
290parallel with Emacs; but you can think of it as synchronous in that
291Emacs is essentially finished with the subprocess as soon as this
292function returns.
293
294MS-DOS doesn't support asynchronous subprocesses, so this option doesn't
295work there.
296
297@item @code{(@var{real-destination} @var{error-destination})}
298Keep the standard output stream separate from the standard error stream;
299deal with the ordinary output as specified by @var{real-destination},
300and dispose of the error output according to @var{error-destination}.
301If @var{error-destination} is @code{nil}, that means to discard the
302error output, @code{t} means mix it with the ordinary output, and a
303string specifies a file name to redirect error output into.
304
305You can't directly specify a buffer to put the error output in; that is
306too difficult to implement. But you can achieve this result by sending
307the error output to a temporary file and then inserting the file into a
308buffer.
309@end table
310
311If @var{display} is non-@code{nil}, then @code{call-process} redisplays
312the buffer as output is inserted. (However, if the coding system chosen
313for decoding output is @code{undecided}, meaning deduce the encoding
314from the actual data, then redisplay sometimes cannot continue once
315non-@acronym{ASCII} characters are encountered. There are fundamental
316reasons why it is hard to fix this; see @ref{Output from Processes}.)
317
318Otherwise the function @code{call-process} does no redisplay, and the
319results become visible on the screen only when Emacs redisplays that
320buffer in the normal course of events.
321
322The remaining arguments, @var{args}, are strings that specify command
323line arguments for the program.
324
325The value returned by @code{call-process} (unless you told it not to
326wait) indicates the reason for process termination. A number gives the
327exit status of the subprocess; 0 means success, and any other value
328means failure. If the process terminated with a signal,
329@code{call-process} returns a string describing the signal.
330
331In the examples below, the buffer @samp{foo} is current.
332
333@smallexample
334@group
335(call-process "pwd" nil t)
336 @result{} 0
337
338---------- Buffer: foo ----------
339/usr/user/lewis/manual
340---------- Buffer: foo ----------
341@end group
342
343@group
344(call-process "grep" nil "bar" nil "lewis" "/etc/passwd")
345 @result{} 0
346
347---------- Buffer: bar ----------
348lewis:5LTsHm66CSWKg:398:21:Bil Lewis:/user/lewis:/bin/csh
349
350---------- Buffer: bar ----------
351@end group
352@end smallexample
353
354Here is a good example of the use of @code{call-process}, which used to
355be found in the definition of @code{insert-directory}:
356
357@smallexample
358@group
359(call-process insert-directory-program nil t nil @var{switches}
360 (if full-directory-p
361 (concat (file-name-as-directory file) ".")
362 file))
363@end group
364@end smallexample
365@end defun
366
367@defun process-file program &optional infile buffer display &rest args
368This function processes files synchronously in a separate process. It
369is similar to @code{call-process} but may invoke a file handler based
370on the value of the variable @code{default-directory}. The current
371working directory of the subprocess is @code{default-directory}.
372
373The arguments are handled in almost the same way as for
374@code{call-process}, with the following differences:
375
376Some file handlers may not support all combinations and forms of the
377arguments @var{infile}, @var{buffer}, and @var{display}. For example,
378some file handlers might behave as if @var{display} were @code{nil},
379regardless of the value actually passed. As another example, some
380file handlers might not support separating standard output and error
381output by way of the @var{buffer} argument.
382
383If a file handler is invoked, it determines the program to run based
384on the first argument @var{program}. For instance, consider that a
385handler for remote files is invoked. Then the path that is used for
386searching the program might be different than @code{exec-path}.
387
388The second argument @var{infile} may invoke a file handler. The file
389handler could be different from the handler chosen for the
390@code{process-file} function itself. (For example,
391@code{default-directory} could be on a remote host, whereas
392@var{infile} is on another remote host. Or @code{default-directory}
393could be non-special, whereas @var{infile} is on a remote host.)
394
395If @var{buffer} is a list of the form @code{(@var{real-destination}
396@var{error-destination})}, and @var{error-destination} names a file,
397then the same remarks as for @var{infile} apply.
398
399The remaining arguments (@var{args}) will be passed to the process
400verbatim. Emacs is not involved in processing file names that are
401present in @var{args}. To avoid confusion, it may be best to avoid
402absolute file names in @var{args}, but rather to specify all file
403names as relative to @code{default-directory}. The function
404@code{file-relative-name} is useful for constructing such relative
405file names.
406@end defun
407
3691a0a6
MA
408@defvar process-file-side-effects
409This variable indicates, whether a call of @code{process-file} changes
410remote files.
411
412Per default, this variable is always set to @code{t}, meaning that a
413call of @code{process-file} could potentially change any file on a
414remote host. When set to @code{nil}, a file handler could optimize
415its behaviour with respect to remote file attributes caching.
416
417This variable should never be changed by @code{setq}. Instead of, it
418shall be set only by let-binding.
419@end defvar
420
b8d4c8d0
GM
421@defun call-process-region start end program &optional delete destination display &rest args
422This function sends the text from @var{start} to @var{end} as
423standard input to a process running @var{program}. It deletes the text
424sent if @var{delete} is non-@code{nil}; this is useful when
425@var{destination} is @code{t}, to insert the output in the current
426buffer in place of the input.
427
428The arguments @var{destination} and @var{display} control what to do
429with the output from the subprocess, and whether to update the display
430as it comes in. For details, see the description of
431@code{call-process}, above. If @var{destination} is the integer 0,
432@code{call-process-region} discards the output and returns @code{nil}
433immediately, without waiting for the subprocess to finish (this only
434works if asynchronous subprocesses are supported).
435
436The remaining arguments, @var{args}, are strings that specify command
437line arguments for the program.
438
439The return value of @code{call-process-region} is just like that of
440@code{call-process}: @code{nil} if you told it to return without
441waiting; otherwise, a number or string which indicates how the
442subprocess terminated.
443
444In the following example, we use @code{call-process-region} to run the
445@code{cat} utility, with standard input being the first five characters
446in buffer @samp{foo} (the word @samp{input}). @code{cat} copies its
447standard input into its standard output. Since the argument
448@var{destination} is @code{t}, this output is inserted in the current
449buffer.
450
451@smallexample
452@group
453---------- Buffer: foo ----------
454input@point{}
455---------- Buffer: foo ----------
456@end group
457
458@group
459(call-process-region 1 6 "cat" nil t)
460 @result{} 0
461
462---------- Buffer: foo ----------
463inputinput@point{}
464---------- Buffer: foo ----------
465@end group
466@end smallexample
467
468 The @code{shell-command-on-region} command uses
469@code{call-process-region} like this:
470
471@smallexample
472@group
473(call-process-region
474 start end
475 shell-file-name ; @r{Name of program.}
476 nil ; @r{Do not delete region.}
477 buffer ; @r{Send output to @code{buffer}.}
478 nil ; @r{No redisplay during output.}
479 "-c" command) ; @r{Arguments for the shell.}
480@end group
481@end smallexample
482@end defun
483
484@defun call-process-shell-command command &optional infile destination display &rest args
485This function executes the shell command @var{command} synchronously
486in a separate process. The final arguments @var{args} are additional
487arguments to add at the end of @var{command}. The other arguments
488are handled as in @code{call-process}.
489@end defun
490
491@defun process-file-shell-command command &optional infile destination display &rest args
492This function is like @code{call-process-shell-command}, but uses
493@code{process-file} internally. Depending on @code{default-directory},
494@var{command} can be executed also on remote hosts.
495@end defun
496
497@defun shell-command-to-string command
498This function executes @var{command} (a string) as a shell command,
499then returns the command's output as a string.
500@end defun
501
583d8b3c
EZ
502@defun process-lines program &rest args
503This function runs @var{program} in a separate process, waits for it
504to finish, and returns its output as a list of strings. Each string
505in the list holds a single line of text output by the program; the
506end-of-line characters are stripped from each line. The arguments
507beyond @var{program}, @var{args}, are strings that specify
508command-line arguments with which to run the program.
509
510If @var{program} exits with a non-zero exit status, this function
511signals an error.
512
513This function works by calling @code{call-process}, so program output
514is decoded in the same way as for @code{call-process}.
515@end defun
516
b8d4c8d0
GM
517@node Asynchronous Processes
518@section Creating an Asynchronous Process
519@cindex asynchronous subprocess
520
521 After an @dfn{asynchronous process} is created, Emacs and the subprocess
522both continue running immediately. The process thereafter runs
523in parallel with Emacs, and the two can communicate with each other
524using the functions described in the following sections. However,
525communication is only partially asynchronous: Emacs sends data to the
526process only when certain functions are called, and Emacs accepts data
527from the process only when Emacs is waiting for input or for a time
528delay.
529
530 Here we describe how to create an asynchronous process.
531
532@defun start-process name buffer-or-name program &rest args
533This function creates a new asynchronous subprocess and starts the
534program @var{program} running in it. It returns a process object that
535stands for the new subprocess in Lisp. The argument @var{name}
536specifies the name for the process object; if a process with this name
537already exists, then @var{name} is modified (by appending @samp{<1>},
538etc.) to be unique. The buffer @var{buffer-or-name} is the buffer to
539associate with the process.
540
541The remaining arguments, @var{args}, are strings that specify command
542line arguments for the program.
543
544In the example below, the first process is started and runs (rather,
545sleeps) for 100 seconds. Meanwhile, the second process is started, and
546given the name @samp{my-process<1>} for the sake of uniqueness. It
547inserts the directory listing at the end of the buffer @samp{foo},
548before the first process finishes. Then it finishes, and a message to
549that effect is inserted in the buffer. Much later, the first process
550finishes, and another message is inserted in the buffer for it.
551
552@smallexample
553@group
554(start-process "my-process" "foo" "sleep" "100")
555 @result{} #<process my-process>
556@end group
557
558@group
559(start-process "my-process" "foo" "ls" "-l" "/user/lewis/bin")
560 @result{} #<process my-process<1>>
561
562---------- Buffer: foo ----------
563total 2
564lrwxrwxrwx 1 lewis 14 Jul 22 10:12 gnuemacs --> /emacs
565-rwxrwxrwx 1 lewis 19 Jul 30 21:02 lemon
566
567Process my-process<1> finished
568
569Process my-process finished
570---------- Buffer: foo ----------
571@end group
572@end smallexample
573@end defun
574
575@defun start-file-process name buffer-or-name program &rest args
576Like @code{start-process}, this function starts a new asynchronous
577subprocess running @var{program} in it, and returns its process
578object---when @code{default-directory} is not a magic file name.
579
580If @code{default-directory} is magic, the function invokes its file
581handler instead. This handler ought to run @var{program}, perhaps on
582the local host, perhaps on a remote host that corresponds to
583@code{default-directory}. In the latter case, the local part of
584@code{default-directory} becomes the working directory of the process.
585
586This function does not try to invoke file name handlers for
587@var{program} or for the @var{program-args}.
588
589Depending on the implementation of the file handler, it might not be
590possible to apply @code{process-filter} or @code{process-sentinel} to
591the resulting process object (@pxref{Filter Functions}, @pxref{Sentinels}).
592
593Some file handlers may not support @code{start-file-process} (for
594example @code{ange-ftp-hook-function}). In such cases, the function
595does nothing and returns @code{nil}.
596@end defun
597
03a74b84 598@defun start-process-shell-command name buffer-or-name command
b8d4c8d0
GM
599This function is like @code{start-process} except that it uses a shell
600to execute the specified command. The argument @var{command} is a shell
03a74b84 601command name. The variable @code{shell-file-name} specifies which shell to
b8d4c8d0
GM
602use.
603
604The point of running a program through the shell, rather than directly
605with @code{start-process}, is so that you can employ shell features such
606as wildcards in the arguments. It follows that if you include an
607arbitrary user-specified arguments in the command, you should quote it
608with @code{shell-quote-argument} first, so that any special shell
609characters do @emph{not} have their special shell meanings. @xref{Shell
610Arguments}.
611@end defun
612
03a74b84 613@defun start-file-process-shell-command name buffer-or-name command
b8d4c8d0
GM
614This function is like @code{start-process-shell-command}, but uses
615@code{start-file-process} internally. By this, @var{command} can be
616executed also on remote hosts, depending on @code{default-directory}.
617@end defun
618
619@defvar process-connection-type
620@cindex pipes
621@cindex @acronym{PTY}s
622This variable controls the type of device used to communicate with
623asynchronous subprocesses. If it is non-@code{nil}, then @acronym{PTY}s are
624used, when available. Otherwise, pipes are used.
625
626@acronym{PTY}s are usually preferable for processes visible to the user, as
627in Shell mode, because they allow job control (@kbd{C-c}, @kbd{C-z},
628etc.) to work between the process and its children, whereas pipes do
629not. For subprocesses used for internal purposes by programs, it is
630often better to use a pipe, because they are more efficient. In
631addition, the total number of @acronym{PTY}s is limited on many systems and
632it is good not to waste them.
633
634The value of @code{process-connection-type} takes effect when
635@code{start-process} is called. So you can specify how to communicate
636with one subprocess by binding the variable around the call to
637@code{start-process}.
638
639@smallexample
640@group
641(let ((process-connection-type nil)) ; @r{Use a pipe.}
642 (start-process @dots{}))
643@end group
644@end smallexample
645
646To determine whether a given subprocess actually got a pipe or a
647@acronym{PTY}, use the function @code{process-tty-name} (@pxref{Process
648Information}).
649@end defvar
650
651@node Deleting Processes
652@section Deleting Processes
653@cindex deleting processes
654
655 @dfn{Deleting a process} disconnects Emacs immediately from the
656subprocess. Processes are deleted automatically after they terminate,
657but not necessarily right away. You can delete a process explicitly
658at any time. If you delete a terminated process explicitly before it
659is deleted automatically, no harm results. Deleting a running
660process sends a signal to terminate it (and its child processes if
661any), and calls the process sentinel if it has one. @xref{Sentinels}.
662
663 When a process is deleted, the process object itself continues to
664exist as long as other Lisp objects point to it. All the Lisp
665primitives that work on process objects accept deleted processes, but
666those that do I/O or send signals will report an error. The process
667mark continues to point to the same place as before, usually into a
668buffer where output from the process was being inserted.
669
670@defopt delete-exited-processes
671This variable controls automatic deletion of processes that have
672terminated (due to calling @code{exit} or to a signal). If it is
673@code{nil}, then they continue to exist until the user runs
674@code{list-processes}. Otherwise, they are deleted immediately after
675they exit.
676@end defopt
677
678@defun delete-process process
679This function deletes a process, killing it with a @code{SIGKILL}
680signal. The argument may be a process, the name of a process, a
681buffer, or the name of a buffer. (A buffer or buffer-name stands for
682the process that @code{get-buffer-process} returns.) Calling
683@code{delete-process} on a running process terminates it, updates the
684process status, and runs the sentinel (if any) immediately. If the
685process has already terminated, calling @code{delete-process} has no
686effect on its status, or on the running of its sentinel (which will
687happen sooner or later).
688
689@smallexample
690@group
691(delete-process "*shell*")
692 @result{} nil
693@end group
694@end smallexample
695@end defun
696
697@node Process Information
698@section Process Information
699
700 Several functions return information about processes.
701@code{list-processes} is provided for interactive use.
702
703@deffn Command list-processes &optional query-only
704This command displays a listing of all living processes. In addition,
705it finally deletes any process whose status was @samp{Exited} or
706@samp{Signaled}. It returns @code{nil}.
707
708If @var{query-only} is non-@code{nil} then it lists only processes
709whose query flag is non-@code{nil}. @xref{Query Before Exit}.
710@end deffn
711
712@defun process-list
713This function returns a list of all processes that have not been deleted.
714
715@smallexample
716@group
717(process-list)
718 @result{} (#<process display-time> #<process shell>)
719@end group
720@end smallexample
721@end defun
722
723@defun get-process name
724This function returns the process named @var{name}, or @code{nil} if
725there is none. An error is signaled if @var{name} is not a string.
726
727@smallexample
728@group
729(get-process "shell")
730 @result{} #<process shell>
731@end group
732@end smallexample
733@end defun
734
735@defun process-command process
736This function returns the command that was executed to start
737@var{process}. This is a list of strings, the first string being the
738program executed and the rest of the strings being the arguments that
739were given to the program.
740
741@smallexample
742@group
743(process-command (get-process "shell"))
744 @result{} ("/bin/csh" "-i")
745@end group
746@end smallexample
747@end defun
748
c73e02fa
GM
749@defun process-contact process &optional key
750
751This function returns information about how a network or serial
752process was set up. For a network process, when @var{key} is
753@code{nil}, it returns @code{(@var{hostname} @var{service})} which
754specifies what you connected to. For a serial process, when @var{key}
755is @code{nil}, it returns @code{(@var{port} @var{speed})}. For an
756ordinary child process, this function always returns @code{t}.
757
758If @var{key} is @code{t}, the value is the complete status information
759for the connection, server, or serial port; that is, the list of
760keywords and values specified in @code{make-network-process} or
761@code{make-serial-process}, except that some of the values represent
762the current status instead of what you specified.
763
764For a network process:
765
766@table @code
767@item :buffer
768The associated value is the process buffer.
769@item :filter
770The associated value is the process filter function.
771@item :sentinel
772The associated value is the process sentinel function.
773@item :remote
774In a connection, the address in internal format of the remote peer.
775@item :local
776The local address, in internal format.
777@item :service
778In a server, if you specified @code{t} for @var{service},
779this value is the actual port number.
780@end table
781
782@code{:local} and @code{:remote} are included even if they were not
783specified explicitly in @code{make-network-process}.
784
785For a serial process, see @code{make-serial-process} and
786@code{serial-process-configure} for a list of keys.
787
788If @var{key} is a keyword, the function returns the value corresponding
789to that keyword.
790@end defun
791
b8d4c8d0
GM
792@defun process-id process
793This function returns the @acronym{PID} of @var{process}. This is an
794integer that distinguishes the process @var{process} from all other
795processes running on the same computer at the current time. The
796@acronym{PID} of a process is chosen by the operating system kernel when the
797process is started and remains constant as long as the process exists.
798@end defun
799
800@defun process-name process
801This function returns the name of @var{process}.
802@end defun
803
804@defun process-status process-name
805This function returns the status of @var{process-name} as a symbol.
170ddd97
CY
806The argument @var{process-name} must be a process, a buffer, or a
807process name (a string).
b8d4c8d0
GM
808
809The possible values for an actual subprocess are:
810
811@table @code
812@item run
813for a process that is running.
814@item stop
815for a process that is stopped but continuable.
816@item exit
817for a process that has exited.
818@item signal
819for a process that has received a fatal signal.
820@item open
821for a network connection that is open.
822@item closed
823for a network connection that is closed. Once a connection
824is closed, you cannot reopen it, though you might be able to open
825a new connection to the same place.
826@item connect
827for a non-blocking connection that is waiting to complete.
828@item failed
829for a non-blocking connection that has failed to complete.
830@item listen
831for a network server that is listening.
832@item nil
833if @var{process-name} is not the name of an existing process.
834@end table
835
836@smallexample
837@group
b8d4c8d0
GM
838(process-status (get-buffer "*shell*"))
839 @result{} run
840@end group
841@group
842x
843 @result{} #<process xx<1>>
844(process-status x)
845 @result{} exit
846@end group
847@end smallexample
848
849For a network connection, @code{process-status} returns one of the symbols
850@code{open} or @code{closed}. The latter means that the other side
851closed the connection, or Emacs did @code{delete-process}.
852@end defun
853
c73e02fa
GM
854@defun process-type process
855This function returns the symbol @code{network} for a network
856connection or server, @code{serial} for a serial port connection, or
857@code{real} for a real subprocess.
858@end defun
859
b8d4c8d0
GM
860@defun process-exit-status process
861This function returns the exit status of @var{process} or the signal
862number that killed it. (Use the result of @code{process-status} to
863determine which of those it is.) If @var{process} has not yet
864terminated, the value is 0.
865@end defun
866
867@defun process-tty-name process
868This function returns the terminal name that @var{process} is using for
869its communication with Emacs---or @code{nil} if it is using pipes
870instead of a terminal (see @code{process-connection-type} in
871@ref{Asynchronous Processes}).
872@end defun
873
874@defun process-coding-system process
875@anchor{Coding systems for a subprocess}
876This function returns a cons cell describing the coding systems in use
877for decoding output from @var{process} and for encoding input to
878@var{process} (@pxref{Coding Systems}). The value has this form:
879
880@example
881(@var{coding-system-for-decoding} . @var{coding-system-for-encoding})
882@end example
883@end defun
884
885@defun set-process-coding-system process &optional decoding-system encoding-system
886This function specifies the coding systems to use for subsequent output
887from and input to @var{process}. It will use @var{decoding-system} to
888decode subprocess output, and @var{encoding-system} to encode subprocess
889input.
890@end defun
891
892 Every process also has a property list that you can use to store
893miscellaneous values associated with the process.
894
895@defun process-get process propname
896This function returns the value of the @var{propname} property
897of @var{process}.
898@end defun
899
900@defun process-put process propname value
901This function sets the value of the @var{propname} property
902of @var{process} to @var{value}.
903@end defun
904
905@defun process-plist process
906This function returns the process plist of @var{process}.
907@end defun
908
909@defun set-process-plist process plist
910This function sets the process plist of @var{process} to @var{plist}.
911@end defun
912
913@node Input to Processes
914@section Sending Input to Processes
915@cindex process input
916
917 Asynchronous subprocesses receive input when it is sent to them by
918Emacs, which is done with the functions in this section. You must
919specify the process to send input to, and the input data to send. The
920data appears on the ``standard input'' of the subprocess.
921
922 Some operating systems have limited space for buffered input in a
923@acronym{PTY}. On these systems, Emacs sends an @acronym{EOF}
924periodically amidst the other characters, to force them through. For
925most programs, these @acronym{EOF}s do no harm.
926
927 Subprocess input is normally encoded using a coding system before the
928subprocess receives it, much like text written into a file. You can use
929@code{set-process-coding-system} to specify which coding system to use
930(@pxref{Process Information}). Otherwise, the coding system comes from
931@code{coding-system-for-write}, if that is non-@code{nil}; or else from
932the defaulting mechanism (@pxref{Default Coding Systems}).
933
934 Sometimes the system is unable to accept input for that process,
935because the input buffer is full. When this happens, the send functions
936wait a short while, accepting output from subprocesses, and then try
937again. This gives the subprocess a chance to read more of its pending
938input and make space in the buffer. It also allows filters, sentinels
939and timers to run---so take account of that in writing your code.
940
941 In these functions, the @var{process} argument can be a process or
942the name of a process, or a buffer or buffer name (which stands
943for a process via @code{get-buffer-process}). @code{nil} means
944the current buffer's process.
945
946@defun process-send-string process string
947This function sends @var{process} the contents of @var{string} as
948standard input. If it is @code{nil}, the current buffer's process is used.
949
950 The function returns @code{nil}.
951
952@smallexample
953@group
954(process-send-string "shell<1>" "ls\n")
955 @result{} nil
956@end group
957
958
959@group
960---------- Buffer: *shell* ----------
961...
962introduction.texi syntax-tables.texi~
963introduction.texi~ text.texi
964introduction.txt text.texi~
965...
966---------- Buffer: *shell* ----------
967@end group
968@end smallexample
969@end defun
970
971@defun process-send-region process start end
972This function sends the text in the region defined by @var{start} and
973@var{end} as standard input to @var{process}.
974
975An error is signaled unless both @var{start} and @var{end} are
976integers or markers that indicate positions in the current buffer. (It
977is unimportant which number is larger.)
978@end defun
979
980@defun process-send-eof &optional process
981This function makes @var{process} see an end-of-file in its
982input. The @acronym{EOF} comes after any text already sent to it.
983
984The function returns @var{process}.
985
986@smallexample
987@group
988(process-send-eof "shell")
989 @result{} "shell"
990@end group
991@end smallexample
992@end defun
993
106e6894
CY
994@defun process-running-child-p &optional process
995This function will tell you whether a @var{process} has given control of
b8d4c8d0
GM
996its terminal to its own child process. The value is @code{t} if this is
997true, or if Emacs cannot tell; it is @code{nil} if Emacs can be certain
998that this is not so.
999@end defun
1000
1001@node Signals to Processes
1002@section Sending Signals to Processes
1003@cindex process signals
1004@cindex sending signals
1005@cindex signals
1006
1007 @dfn{Sending a signal} to a subprocess is a way of interrupting its
1008activities. There are several different signals, each with its own
1009meaning. The set of signals and their names is defined by the operating
1010system. For example, the signal @code{SIGINT} means that the user has
1011typed @kbd{C-c}, or that some analogous thing has happened.
1012
1013 Each signal has a standard effect on the subprocess. Most signals
1014kill the subprocess, but some stop or resume execution instead. Most
1015signals can optionally be handled by programs; if the program handles
1016the signal, then we can say nothing in general about its effects.
1017
1018 You can send signals explicitly by calling the functions in this
1019section. Emacs also sends signals automatically at certain times:
1020killing a buffer sends a @code{SIGHUP} signal to all its associated
1021processes; killing Emacs sends a @code{SIGHUP} signal to all remaining
1022processes. (@code{SIGHUP} is a signal that usually indicates that the
1023user hung up the phone.)
1024
1025 Each of the signal-sending functions takes two optional arguments:
1026@var{process} and @var{current-group}.
1027
1028 The argument @var{process} must be either a process, a process
1029name, a buffer, a buffer name, or @code{nil}. A buffer or buffer name
1030stands for a process through @code{get-buffer-process}. @code{nil}
1031stands for the process associated with the current buffer. An error
1032is signaled if @var{process} does not identify a process.
1033
1034 The argument @var{current-group} is a flag that makes a difference
1035when you are running a job-control shell as an Emacs subprocess. If it
1036is non-@code{nil}, then the signal is sent to the current process-group
1037of the terminal that Emacs uses to communicate with the subprocess. If
1038the process is a job-control shell, this means the shell's current
1039subjob. If it is @code{nil}, the signal is sent to the process group of
1040the immediate subprocess of Emacs. If the subprocess is a job-control
1041shell, this is the shell itself.
1042
1043 The flag @var{current-group} has no effect when a pipe is used to
1044communicate with the subprocess, because the operating system does not
1045support the distinction in the case of pipes. For the same reason,
1046job-control shells won't work when a pipe is used. See
1047@code{process-connection-type} in @ref{Asynchronous Processes}.
1048
1049@defun interrupt-process &optional process current-group
1050This function interrupts the process @var{process} by sending the
1051signal @code{SIGINT}. Outside of Emacs, typing the ``interrupt
1052character'' (normally @kbd{C-c} on some systems, and @code{DEL} on
1053others) sends this signal. When the argument @var{current-group} is
1054non-@code{nil}, you can think of this function as ``typing @kbd{C-c}''
1055on the terminal by which Emacs talks to the subprocess.
1056@end defun
1057
1058@defun kill-process &optional process current-group
1059This function kills the process @var{process} by sending the
1060signal @code{SIGKILL}. This signal kills the subprocess immediately,
1061and cannot be handled by the subprocess.
1062@end defun
1063
1064@defun quit-process &optional process current-group
1065This function sends the signal @code{SIGQUIT} to the process
1066@var{process}. This signal is the one sent by the ``quit
1067character'' (usually @kbd{C-b} or @kbd{C-\}) when you are not inside
1068Emacs.
1069@end defun
1070
1071@defun stop-process &optional process current-group
1072This function stops the process @var{process} by sending the
1073signal @code{SIGTSTP}. Use @code{continue-process} to resume its
1074execution.
1075
1076Outside of Emacs, on systems with job control, the ``stop character''
1077(usually @kbd{C-z}) normally sends this signal. When
1078@var{current-group} is non-@code{nil}, you can think of this function as
1079``typing @kbd{C-z}'' on the terminal Emacs uses to communicate with the
1080subprocess.
1081@end defun
1082
1083@defun continue-process &optional process current-group
1084This function resumes execution of the process @var{process} by sending
1085it the signal @code{SIGCONT}. This presumes that @var{process} was
1086stopped previously.
1087@end defun
1088
b8d4c8d0
GM
1089@defun signal-process process signal
1090This function sends a signal to process @var{process}. The argument
1091@var{signal} specifies which signal to send; it should be an integer.
1092
1093The @var{process} argument can be a system process @acronym{ID}; that
1094allows you to send signals to processes that are not children of
23dd4ecd 1095Emacs. @xref{System Processes}.
b8d4c8d0
GM
1096@end defun
1097
1098@node Output from Processes
1099@section Receiving Output from Processes
1100@cindex process output
1101@cindex output from processes
1102
1103 There are two ways to receive the output that a subprocess writes to
1104its standard output stream. The output can be inserted in a buffer,
1105which is called the associated buffer of the process, or a function
1106called the @dfn{filter function} can be called to act on the output. If
1107the process has no buffer and no filter function, its output is
1108discarded.
1109
1110 When a subprocess terminates, Emacs reads any pending output,
1111then stops reading output from that subprocess. Therefore, if the
1112subprocess has children that are still live and still producing
1113output, Emacs won't receive that output.
1114
1115 Output from a subprocess can arrive only while Emacs is waiting: when
1116reading terminal input, in @code{sit-for} and @code{sleep-for}
1117(@pxref{Waiting}), and in @code{accept-process-output} (@pxref{Accepting
1118Output}). This minimizes the problem of timing errors that usually
1119plague parallel programming. For example, you can safely create a
1120process and only then specify its buffer or filter function; no output
1121can arrive before you finish, if the code in between does not call any
1122primitive that waits.
1123
1124@defvar process-adaptive-read-buffering
1125On some systems, when Emacs reads the output from a subprocess, the
1126output data is read in very small blocks, potentially resulting in
1127very poor performance. This behavior can be remedied to some extent
1128by setting the variable @var{process-adaptive-read-buffering} to a
1129non-@code{nil} value (the default), as it will automatically delay reading
1130from such processes, thus allowing them to produce more output before
1131Emacs tries to read it.
1132@end defvar
1133
1134 It is impossible to separate the standard output and standard error
1135streams of the subprocess, because Emacs normally spawns the subprocess
1136inside a pseudo-TTY, and a pseudo-TTY has only one output channel. If
1137you want to keep the output to those streams separate, you should
1138redirect one of them to a file---for example, by using an appropriate
1139shell command.
1140
1141@menu
1142* Process Buffers:: If no filter, output is put in a buffer.
1143* Filter Functions:: Filter functions accept output from the process.
1144* Decoding Output:: Filters can get unibyte or multibyte strings.
1145* Accepting Output:: How to wait until process output arrives.
1146@end menu
1147
1148@node Process Buffers
1149@subsection Process Buffers
1150
1151 A process can (and usually does) have an @dfn{associated buffer},
1152which is an ordinary Emacs buffer that is used for two purposes: storing
1153the output from the process, and deciding when to kill the process. You
1154can also use the buffer to identify a process to operate on, since in
1155normal practice only one process is associated with any given buffer.
1156Many applications of processes also use the buffer for editing input to
1157be sent to the process, but this is not built into Emacs Lisp.
1158
1159 Unless the process has a filter function (@pxref{Filter Functions}),
1160its output is inserted in the associated buffer. The position to insert
1161the output is determined by the @code{process-mark}, which is then
1162updated to point to the end of the text just inserted. Usually, but not
1163always, the @code{process-mark} is at the end of the buffer.
1164
16d1ff5f
CY
1165@findex process-kill-buffer-query-function
1166 Killing the associated buffer of a process also kills the process.
1167Emacs asks for confirmation first, if the process's
1168@code{process-query-on-exit-flag} is non-@code{nil} (@pxref{Query
1169Before Exit}). This confirmation is done by the function
1170@code{process-kill-buffer-query-function}, which is run from
1171@code{kill-buffer-query-functions} (@pxref{Killing Buffers}).
1172
b8d4c8d0
GM
1173@defun process-buffer process
1174This function returns the associated buffer of the process
1175@var{process}.
1176
1177@smallexample
1178@group
1179(process-buffer (get-process "shell"))
1180 @result{} #<buffer *shell*>
1181@end group
1182@end smallexample
1183@end defun
1184
1185@defun process-mark process
1186This function returns the process marker for @var{process}, which is the
1187marker that says where to insert output from the process.
1188
1189If @var{process} does not have a buffer, @code{process-mark} returns a
1190marker that points nowhere.
1191
1192Insertion of process output in a buffer uses this marker to decide where
1193to insert, and updates it to point after the inserted text. That is why
1194successive batches of output are inserted consecutively.
1195
1196Filter functions normally should use this marker in the same fashion
1197as is done by direct insertion of output in the buffer. A good
1198example of a filter function that uses @code{process-mark} is found at
1199the end of the following section.
1200
1201When the user is expected to enter input in the process buffer for
1202transmission to the process, the process marker separates the new input
1203from previous output.
1204@end defun
1205
1206@defun set-process-buffer process buffer
1207This function sets the buffer associated with @var{process} to
1208@var{buffer}. If @var{buffer} is @code{nil}, the process becomes
1209associated with no buffer.
1210@end defun
1211
1212@defun get-buffer-process buffer-or-name
1213This function returns a nondeleted process associated with the buffer
1214specified by @var{buffer-or-name}. If there are several processes
1215associated with it, this function chooses one (currently, the one most
1216recently created, but don't count on that). Deletion of a process
1217(see @code{delete-process}) makes it ineligible for this function to
1218return.
1219
1220It is usually a bad idea to have more than one process associated with
1221the same buffer.
1222
1223@smallexample
1224@group
1225(get-buffer-process "*shell*")
1226 @result{} #<process shell>
1227@end group
1228@end smallexample
1229
1230Killing the process's buffer deletes the process, which kills the
1231subprocess with a @code{SIGHUP} signal (@pxref{Signals to Processes}).
1232@end defun
1233
1234@node Filter Functions
1235@subsection Process Filter Functions
1236@cindex filter function
1237@cindex process filter
1238
1239 A process @dfn{filter function} is a function that receives the
1240standard output from the associated process. If a process has a filter,
1241then @emph{all} output from that process is passed to the filter. The
1242process buffer is used directly for output from the process only when
1243there is no filter.
1244
1245 The filter function can only be called when Emacs is waiting for
1246something, because process output arrives only at such times. Emacs
1247waits when reading terminal input, in @code{sit-for} and
1248@code{sleep-for} (@pxref{Waiting}), and in @code{accept-process-output}
1249(@pxref{Accepting Output}).
1250
1251 A filter function must accept two arguments: the associated process
1252and a string, which is output just received from it. The function is
1253then free to do whatever it chooses with the output.
1254
1255 Quitting is normally inhibited within a filter function---otherwise,
1256the effect of typing @kbd{C-g} at command level or to quit a user
1257command would be unpredictable. If you want to permit quitting inside
1258a filter function, bind @code{inhibit-quit} to @code{nil}. In most
1259cases, the right way to do this is with the macro
1260@code{with-local-quit}. @xref{Quitting}.
1261
1262 If an error happens during execution of a filter function, it is
1263caught automatically, so that it doesn't stop the execution of whatever
1264program was running when the filter function was started. However, if
1265@code{debug-on-error} is non-@code{nil}, the error-catching is turned
1266off. This makes it possible to use the Lisp debugger to debug the
1267filter function. @xref{Debugger}.
1268
1269 Many filter functions sometimes or always insert the text in the
1270process's buffer, mimicking the actions of Emacs when there is no
1271filter. Such filter functions need to use @code{set-buffer} in order to
1272be sure to insert in that buffer. To avoid setting the current buffer
1273semipermanently, these filter functions must save and restore the
1274current buffer. They should also update the process marker, and in some
1275cases update the value of point. Here is how to do these things:
1276
1277@smallexample
1278@group
1279(defun ordinary-insertion-filter (proc string)
1280 (with-current-buffer (process-buffer proc)
1281 (let ((moving (= (point) (process-mark proc))))
1282@end group
1283@group
1284 (save-excursion
1285 ;; @r{Insert the text, advancing the process marker.}
1286 (goto-char (process-mark proc))
1287 (insert string)
1288 (set-marker (process-mark proc) (point)))
1289 (if moving (goto-char (process-mark proc))))))
1290@end group
1291@end smallexample
1292
1293@noindent
1294The reason to use @code{with-current-buffer}, rather than using
1295@code{save-excursion} to save and restore the current buffer, is so as
1296to preserve the change in point made by the second call to
1297@code{goto-char}.
1298
1299 To make the filter force the process buffer to be visible whenever new
1300text arrives, insert the following line just before the
1301@code{with-current-buffer} construct:
1302
1303@smallexample
1304(display-buffer (process-buffer proc))
1305@end smallexample
1306
1307 To force point to the end of the new output, no matter where it was
1308previously, eliminate the variable @code{moving} and call
1309@code{goto-char} unconditionally.
1310
1311 In earlier Emacs versions, every filter function that did regular
1312expression searching or matching had to explicitly save and restore the
1313match data. Now Emacs does this automatically for filter functions;
1314they never need to do it explicitly. @xref{Match Data}.
1315
1316 A filter function that writes the output into the buffer of the
1317process should check whether the buffer is still alive. If it tries to
1318insert into a dead buffer, it will get an error. The expression
1319@code{(buffer-name (process-buffer @var{process}))} returns @code{nil}
1320if the buffer is dead.
1321
1322 The output to the function may come in chunks of any size. A program
1323that produces the same output twice in a row may send it as one batch of
1324200 characters one time, and five batches of 40 characters the next. If
1325the filter looks for certain text strings in the subprocess output, make
1326sure to handle the case where one of these strings is split across two
fd211f0b
CY
1327or more batches of output; one way to do this is to insert the
1328received text into a temporary buffer, which can then be searched.
b8d4c8d0
GM
1329
1330@defun set-process-filter process filter
1331This function gives @var{process} the filter function @var{filter}. If
1332@var{filter} is @code{nil}, it gives the process no filter.
1333@end defun
1334
1335@defun process-filter process
1336This function returns the filter function of @var{process}, or @code{nil}
1337if it has none.
1338@end defun
1339
1340 Here is an example of use of a filter function:
1341
1342@smallexample
1343@group
1344(defun keep-output (process output)
1345 (setq kept (cons output kept)))
1346 @result{} keep-output
1347@end group
1348@group
1349(setq kept nil)
1350 @result{} nil
1351@end group
1352@group
1353(set-process-filter (get-process "shell") 'keep-output)
1354 @result{} keep-output
1355@end group
1356@group
1357(process-send-string "shell" "ls ~/other\n")
1358 @result{} nil
1359kept
1360 @result{} ("lewis@@slug[8] % "
1361@end group
1362@group
1363"FINAL-W87-SHORT.MSS backup.otl kolstad.mss~
1364address.txt backup.psf kolstad.psf
1365backup.bib~ david.mss resume-Dec-86.mss~
1366backup.err david.psf resume-Dec.psf
1367backup.mss dland syllabus.mss
1368"
1369"#backups.mss# backup.mss~ kolstad.mss
1370")
1371@end group
1372@end smallexample
1373
1374@ignore @c The code in this example doesn't show the right way to do things.
1375Here is another, more realistic example, which demonstrates how to use
1376the process mark to do insertion in the same fashion as is done when
1377there is no filter function:
1378
1379@smallexample
1380@group
1381;; @r{Insert input in the buffer specified by @code{my-shell-buffer}}
1382;; @r{and make sure that buffer is shown in some window.}
1383(defun my-process-filter (proc str)
1384 (let ((cur (selected-window))
1385 (pop-up-windows t))
1386 (pop-to-buffer my-shell-buffer)
1387@end group
1388@group
1389 (goto-char (point-max))
1390 (insert str)
1391 (set-marker (process-mark proc) (point-max))
1392 (select-window cur)))
1393@end group
1394@end smallexample
1395@end ignore
1396
1397@node Decoding Output
1398@subsection Decoding Process Output
1399@cindex decode process output
1400
1401 When Emacs writes process output directly into a multibyte buffer,
1402it decodes the output according to the process output coding system.
1403If the coding system is @code{raw-text} or @code{no-conversion}, Emacs
1404converts the unibyte output to multibyte using
1405@code{string-to-multibyte}, and inserts the resulting multibyte text.
1406
1407 You can use @code{set-process-coding-system} to specify which coding
1408system to use (@pxref{Process Information}). Otherwise, the coding
1409system comes from @code{coding-system-for-read}, if that is
1410non-@code{nil}; or else from the defaulting mechanism (@pxref{Default
3355f04d
EZ
1411Coding Systems}). If the text output by a process contains null
1412bytes, Emacs by default uses @code{no-conversion} for it; see
1413@ref{Lisp and Coding Systems, inhibit-null-byte-detection}, for how to
1414control this behavior.
b8d4c8d0
GM
1415
1416 @strong{Warning:} Coding systems such as @code{undecided} which
1417determine the coding system from the data do not work entirely
1418reliably with asynchronous subprocess output. This is because Emacs
1419has to process asynchronous subprocess output in batches, as it
1420arrives. Emacs must try to detect the proper coding system from one
1421batch at a time, and this does not always work. Therefore, if at all
1422possible, specify a coding system that determines both the character
1423code conversion and the end of line conversion---that is, one like
1424@code{latin-1-unix}, rather than @code{undecided} or @code{latin-1}.
1425
4972c361
SM
1426@c Let's keep the index entries that were there for
1427@c set-process-filter-multibyte and process-filter-multibyte-p,
b8d4c8d0
GM
1428@cindex filter multibyte flag, of process
1429@cindex process filter multibyte flag
1430 When Emacs calls a process filter function, it provides the process
1431output as a multibyte string or as a unibyte string according to the
4972c361
SM
1432process's filter coding system. Emacs
1433decodes the output according to the process output coding system,
1434which usually produces a multibyte string, except for coding systems
1435such as @code{binary} and @code{raw-text}
b8d4c8d0
GM
1436
1437@node Accepting Output
1438@subsection Accepting Output from Processes
1439@cindex accept input from processes
1440
1441 Output from asynchronous subprocesses normally arrives only while
1442Emacs is waiting for some sort of external event, such as elapsed time
1443or terminal input. Occasionally it is useful in a Lisp program to
1444explicitly permit output to arrive at a specific point, or even to wait
1445until output arrives from a process.
1446
1447@defun accept-process-output &optional process seconds millisec just-this-one
1448This function allows Emacs to read pending output from processes. The
1449output is inserted in the associated buffers or given to their filter
1450functions. If @var{process} is non-@code{nil} then this function does
1451not return until some output has been received from @var{process}.
1452
1453@c Emacs 19 feature
1454The arguments @var{seconds} and @var{millisec} let you specify timeout
1455periods. The former specifies a period measured in seconds and the
1456latter specifies one measured in milliseconds. The two time periods
1457thus specified are added together, and @code{accept-process-output}
1458returns after that much time, whether or not there has been any
1459subprocess output.
1460
1461The argument @var{millisec} is semi-obsolete nowadays because
1462@var{seconds} can be a floating point number to specify waiting a
1463fractional number of seconds. If @var{seconds} is 0, the function
1464accepts whatever output is pending but does not wait.
1465
1466@c Emacs 22.1 feature
1467If @var{process} is a process, and the argument @var{just-this-one} is
1468non-@code{nil}, only output from that process is handled, suspending output
1469from other processes until some output has been received from that
1470process or the timeout expires. If @var{just-this-one} is an integer,
1471also inhibit running timers. This feature is generally not
1472recommended, but may be necessary for specific applications, such as
1473speech synthesis.
1474
1475The function @code{accept-process-output} returns non-@code{nil} if it
1476did get some output, or @code{nil} if the timeout expired before output
1477arrived.
1478@end defun
1479
1480@node Sentinels
1481@section Sentinels: Detecting Process Status Changes
1482@cindex process sentinel
1483@cindex sentinel (of process)
1484
1485 A @dfn{process sentinel} is a function that is called whenever the
1486associated process changes status for any reason, including signals
1487(whether sent by Emacs or caused by the process's own actions) that
1488terminate, stop, or continue the process. The process sentinel is
1489also called if the process exits. The sentinel receives two
1490arguments: the process for which the event occurred, and a string
1491describing the type of event.
1492
1493 The string describing the event looks like one of the following:
1494
1495@itemize @bullet
1496@item
1497@code{"finished\n"}.
1498
1499@item
1500@code{"exited abnormally with code @var{exitcode}\n"}.
1501
1502@item
1503@code{"@var{name-of-signal}\n"}.
1504
1505@item
1506@code{"@var{name-of-signal} (core dumped)\n"}.
1507@end itemize
1508
1509 A sentinel runs only while Emacs is waiting (e.g., for terminal
1510input, or for time to elapse, or for process output). This avoids the
1511timing errors that could result from running them at random places in
1512the middle of other Lisp programs. A program can wait, so that
1513sentinels will run, by calling @code{sit-for} or @code{sleep-for}
1514(@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting
1515Output}). Emacs also allows sentinels to run when the command loop is
1516reading input. @code{delete-process} calls the sentinel when it
1517terminates a running process.
1518
1519 Emacs does not keep a queue of multiple reasons to call the sentinel
1520of one process; it records just the current status and the fact that
1521there has been a change. Therefore two changes in status, coming in
1522quick succession, can call the sentinel just once. However, process
1523termination will always run the sentinel exactly once. This is
1524because the process status can't change again after termination.
1525
1526 Emacs explicitly checks for output from the process before running
1527the process sentinel. Once the sentinel runs due to process
1528termination, no further output can arrive from the process.
1529
1530 A sentinel that writes the output into the buffer of the process
1531should check whether the buffer is still alive. If it tries to insert
1532into a dead buffer, it will get an error. If the buffer is dead,
1533@code{(buffer-name (process-buffer @var{process}))} returns @code{nil}.
1534
1535 Quitting is normally inhibited within a sentinel---otherwise, the
1536effect of typing @kbd{C-g} at command level or to quit a user command
1537would be unpredictable. If you want to permit quitting inside a
1538sentinel, bind @code{inhibit-quit} to @code{nil}. In most cases, the
1539right way to do this is with the macro @code{with-local-quit}.
1540@xref{Quitting}.
1541
1542 If an error happens during execution of a sentinel, it is caught
1543automatically, so that it doesn't stop the execution of whatever
1544programs was running when the sentinel was started. However, if
1545@code{debug-on-error} is non-@code{nil}, the error-catching is turned
1546off. This makes it possible to use the Lisp debugger to debug the
1547sentinel. @xref{Debugger}.
1548
1549 While a sentinel is running, the process sentinel is temporarily
1550set to @code{nil} so that the sentinel won't run recursively.
1551For this reason it is not possible for a sentinel to specify
1552a new sentinel.
1553
1554 In earlier Emacs versions, every sentinel that did regular expression
1555searching or matching had to explicitly save and restore the match data.
1556Now Emacs does this automatically for sentinels; they never need to do
1557it explicitly. @xref{Match Data}.
1558
1559@defun set-process-sentinel process sentinel
1560This function associates @var{sentinel} with @var{process}. If
1561@var{sentinel} is @code{nil}, then the process will have no sentinel.
1562The default behavior when there is no sentinel is to insert a message in
1563the process's buffer when the process status changes.
1564
1565Changes in process sentinel take effect immediately---if the sentinel
1566is slated to be run but has not been called yet, and you specify a new
1567sentinel, the eventual call to the sentinel will use the new one.
1568
1569@smallexample
1570@group
1571(defun msg-me (process event)
1572 (princ
1573 (format "Process: %s had the event `%s'" process event)))
1574(set-process-sentinel (get-process "shell") 'msg-me)
1575 @result{} msg-me
1576@end group
1577@group
1578(kill-process (get-process "shell"))
1579 @print{} Process: #<process shell> had the event `killed'
1580 @result{} #<process shell>
1581@end group
1582@end smallexample
1583@end defun
1584
1585@defun process-sentinel process
1586This function returns the sentinel of @var{process}, or @code{nil} if it
1587has none.
1588@end defun
1589
1590@defun waiting-for-user-input-p
1591While a sentinel or filter function is running, this function returns
1592non-@code{nil} if Emacs was waiting for keyboard input from the user at
1593the time the sentinel or filter function was called, @code{nil} if it
1594was not.
1595@end defun
1596
1597@node Query Before Exit
1598@section Querying Before Exit
1599
1600 When Emacs exits, it terminates all its subprocesses by sending them
1601the @code{SIGHUP} signal. Because subprocesses may be doing
1602valuable work, Emacs normally asks the user to confirm that it is ok
1603to terminate them. Each process has a query flag which, if
1604non-@code{nil}, says that Emacs should ask for confirmation before
1605exiting and thus killing that process. The default for the query flag
1606is @code{t}, meaning @emph{do} query.
1607
1608@defun process-query-on-exit-flag process
1609This returns the query flag of @var{process}.
1610@end defun
1611
1612@defun set-process-query-on-exit-flag process flag
1613This function sets the query flag of @var{process} to @var{flag}. It
1614returns @var{flag}.
1615
1616@smallexample
1617@group
1618;; @r{Don't query about the shell process}
1619(set-process-query-on-exit-flag (get-process "shell") nil)
1620 @result{} t
1621@end group
1622@end smallexample
1623@end defun
1624
1625@defun process-kill-without-query process &optional do-query
1626This function clears the query flag of @var{process}, so that
1627Emacs will not query the user on account of that process.
1628
1629Actually, the function does more than that: it returns the old value of
1630the process's query flag, and sets the query flag to @var{do-query}.
1631Please don't use this function to do those things any more---please
1632use the newer, cleaner functions @code{process-query-on-exit-flag} and
1633@code{set-process-query-on-exit-flag} in all but the simplest cases.
1634The only way you should use @code{process-kill-without-query} nowadays
1635is like this:
1636
1637@smallexample
1638@group
1639;; @r{Don't query about the shell process}
1640(process-kill-without-query (get-process "shell"))
1641@end group
1642@end smallexample
1643@end defun
1644
23dd4ecd
EZ
1645@node System Processes
1646@section Accessing Other Processes
1647@cindex system processes
1648
1649 In addition to accessing and manipulating processes that are
1650subprocesses of the current Emacs session, Emacs Lisp programs can
1651also access other processes running on the same machine. We call
1652these @dfn{system processes}, to distinguish between them and Emacs
1653subprocesses.
1654
1655 Emacs provides several primitives for accessing system processes.
1656Not all platforms support these primitives; on those which don't,
1657these primitives return @code{nil}.
1658
1659@defun list-system-processes
1660This function returns a list of all the processes running on the
1661system. Each process is identified by its @acronym{PID}, a numerical
1662process ID that is assigned by the OS and distinguishes the process
1663from all the other processes running on the same machine at the same
1664time.
1665@end defun
1666
a20878b6 1667@defun process-attributes pid
23dd4ecd
EZ
1668This function returns an alist of attributes for the process specified
1669by its process ID @var{pid}. Each association in the alist is of the
1670form @code{(@var{key} . @var{value})}, where @var{key} designates the
1671attribute and @var{value} is the value of that attribute. The various
1672attribute @var{key}'s that this function can return are listed below.
1673Not all platforms support all of these attributes; if an attribute is
1674not supported, its association will not appear in the returned alist.
1675Values that are numbers can be either integer or floating-point,
1676depending on the magnitude of the value.
1677
1678@table @code
1679@item euid
1680The effective user ID of the user who invoked the process. The
1681corresponding @var{value} is a number. If the process was invoked by
1682the same user who runs the current Emacs session, the value is
1683identical to what @code{user-uid} returns (@pxref{User
1684Identification}).
1685
1686@item user
1687User name corresponding to the process's effective user ID, a string.
1688
1689@item egid
1690The group ID of the effective user ID, a number.
1691
1692@item group
1693Group name corresponding to the effective user's group ID, a string.
1694
1695@item comm
1696The name of the command that runs in the process. This is a string
1697that usually specifies the name of the executable file of the process,
1698without the leading directories. However, some special system
1699processes can report strings that do not correspond to an executable
1700file of a program.
1701
1702@item state
1703The state code of the process. This is a short string that encodes
1704the scheduling state of the process. Here's a list of the most
1705frequently seen codes:
1706
1707@table @code
1dca458f 1708@item "D"
23dd4ecd 1709uninterruptible sleep (usually I/O)
1dca458f 1710@item "R"
23dd4ecd 1711running
1dca458f 1712@item "S"
23dd4ecd 1713interruptible sleep (waiting for some event)
1dca458f 1714@item "T"
23dd4ecd 1715stopped, e.g., by a job control signal
1dca458f
EZ
1716@item "Z"
1717``zombie'': a process that terminated, but was not reaped by its parent
23dd4ecd
EZ
1718@end table
1719
1720@noindent
1721For the full list of the possible states, see the manual page of the
1722@command{ps} command.
1723
1724@item ppid
1725The process ID of the parent process, a number.
1726
1727@item pgrp
1728The process group ID of the process, a number.
1729
1730@item sess
1731The session ID of the process. This is a number that is the process
1732ID of the process's @dfn{session leader}.
1733
1734@item ttname
1735A string that is the name of the process's controlling terminal. On
1736Unix and GNU systems, this is normally the file name of the
1737corresponding terminal device, such as @file{/dev/pts65}.
1738
1739@item tpgid
1740The numerical process group ID of the foreground process group that
1741uses the process's terminal.
1742
1743@item minflt
1744The number of minor page faults caused by the process since its
1745beginning. (Minor page faults are those that don't involve reading
1746from disk.)
1747
1748@item majflt
1749The number of major page faults caused by the process since its
1750beginning. (Major page faults require a disk to be read, and are thus
1751more expensive than minor page faults.)
1752
1753@item cminflt
1754@itemx cmajflt
1755Like @code{minflt} and @code{majflt}, but include the number of page
1756faults for all the child processes of the given process.
1757
1758@item utime
1759Time spent by the process in the user context, for running the
1760application's code. The corresponding @var{value} is in the
1761@w{@code{(@var{high} @var{low} @var{microsec})}} format, the same
1762format used by functions @code{current-time} (@pxref{Time of Day,
1763current-time}) and @code{file-attributes} (@pxref{File Attributes}).
1764
1765@item stime
1766Time spent by the process in the system (kernel) context, for
1767processing system calls. The corresponding @var{value} is in the same
1768format as for @code{utime}.
1769
af34ad36
EZ
1770@item time
1771The sum of @code{utime} and @code{stime}. The corresponding
1772@var{value} is in the same format as for @code{utime}.
1773
23dd4ecd
EZ
1774@item cutime
1775@itemx cstime
af34ad36
EZ
1776@itemx ctime
1777Like @code{utime}, @code{stime}, and @code{time}, but include the
1778times of all the child processes of the given process.
23dd4ecd
EZ
1779
1780@item pri
1781The numerical priority of the process.
1782
1783@item nice
1dca458f
EZ
1784The @dfn{nice value} of the process, a number. (Processes with smaller
1785nice values get scheduled more favorably.)
23dd4ecd
EZ
1786
1787@item thcount
1788The number of threads in the process.
1789
1790@item start
1791The time the process was started, in the @w{@code{(@var{high}
1792@var{low} @var{microsec})}} format used by @code{current-time} and
1793@code{file-attributes}.
1794
1795@item etime
1796The time elapsed since the process started, in the @w{@code{(@var{high}
1797@var{low} @var{microsec})}} format.
1798
1799@item vsize
1800The virtual memory size of the process, measured in kilobytes.
1801
1802@item rss
1803The size of the process's @dfn{resident set}, the number of kilobytes
1804occupied by the process in the machine's physical memory.
1805
1806@item pcpu
1807The percentage of the CPU time used by the process since it started.
1808The corresponding @var{value} is a floating-point number between 0 and
1809100.
1810
1811@item pmem
1812The percentage of the total physical memory installed on the machine
1813used by the process's resident set. The value is a floating-point
1814number between 0 and 100.
1815
1816@item args
1817The command-line with which the process was invoked. This is a string
1818in which individual command-line arguments are separated by blanks;
1819whitespace characters that are embedded in the arguments are quoted as
1820appropriate for the system's shell: escaped by backslash characters on
1821GNU and Unix, and enclosed in double quote characters on Windows.
1822Thus, this command-line string can be directly used in primitives such
1823as @code{shell-command}.
1824@end table
1825
1826@end defun
1827
1828
b8d4c8d0
GM
1829@node Transaction Queues
1830@section Transaction Queues
1831@cindex transaction queue
1832
1833You can use a @dfn{transaction queue} to communicate with a subprocess
1834using transactions. First use @code{tq-create} to create a transaction
1835queue communicating with a specified process. Then you can call
1836@code{tq-enqueue} to send a transaction.
1837
1838@defun tq-create process
1839This function creates and returns a transaction queue communicating with
1840@var{process}. The argument @var{process} should be a subprocess
1841capable of sending and receiving streams of bytes. It may be a child
1842process, or it may be a TCP connection to a server, possibly on another
1843machine.
1844@end defun
1845
1846@defun tq-enqueue queue question regexp closure fn &optional delay-question
1847This function sends a transaction to queue @var{queue}. Specifying the
1848queue has the effect of specifying the subprocess to talk to.
1849
1850The argument @var{question} is the outgoing message that starts the
1851transaction. The argument @var{fn} is the function to call when the
1852corresponding answer comes back; it is called with two arguments:
1853@var{closure}, and the answer received.
1854
1855The argument @var{regexp} is a regular expression that should match
1856text at the end of the entire answer, but nothing before; that's how
1857@code{tq-enqueue} determines where the answer ends.
1858
4185820c
CY
1859If the argument @var{delay-question} is non-@code{nil}, delay sending
1860this question until the process has finished replying to any previous
b8d4c8d0
GM
1861questions. This produces more reliable results with some processes.
1862
1863The return value of @code{tq-enqueue} itself is not meaningful.
1864@end defun
1865
1866@defun tq-close queue
1867Shut down transaction queue @var{queue}, waiting for all pending transactions
1868to complete, and then terminate the connection or child process.
1869@end defun
1870
1871Transaction queues are implemented by means of a filter function.
1872@xref{Filter Functions}.
1873
1874@node Network
1875@section Network Connections
1876@cindex network connection
1877@cindex TCP
1878@cindex UDP
1879
1880 Emacs Lisp programs can open stream (TCP) and datagram (UDP) network
1881connections to other processes on the same machine or other machines.
1882A network connection is handled by Lisp much like a subprocess, and is
1883represented by a process object. However, the process you are
1884communicating with is not a child of the Emacs process, so it has no
1885process @acronym{ID}, and you can't kill it or send it signals. All you
1886can do is send and receive data. @code{delete-process} closes the
1887connection, but does not kill the program at the other end; that
1888program must decide what to do about closure of the connection.
1889
1890 Lisp programs can listen for connections by creating network
1891servers. A network server is also represented by a kind of process
1892object, but unlike a network connection, the network server never
1893transfers data itself. When it receives a connection request, it
1894creates a new network connection to represent the connection just
1895made. (The network connection inherits certain information, including
1896the process plist, from the server.) The network server then goes
1897back to listening for more connection requests.
1898
1899 Network connections and servers are created by calling
1900@code{make-network-process} with an argument list consisting of
1901keyword/argument pairs, for example @code{:server t} to create a
1902server process, or @code{:type 'datagram} to create a datagram
1903connection. @xref{Low-Level Network}, for details. You can also use
1904the @code{open-network-stream} function described below.
1905
c73e02fa
GM
1906 To distinguish the different types of processes, the
1907@code{process-type} function returns the symbol @code{network} for a
1908network connection or server, @code{serial} for a serial port
1909connection, or @code{real} for a real subprocess.
1910
1911 The @code{process-status} function returns @code{open},
1912@code{closed}, @code{connect}, and @code{failed} for network
1913connections. For a network server, the status is always
b8d4c8d0
GM
1914@code{listen}. None of those values is possible for a real
1915subprocess. @xref{Process Information}.
1916
1917 You can stop and resume operation of a network process by calling
1918@code{stop-process} and @code{continue-process}. For a server
1919process, being stopped means not accepting new connections. (Up to 5
1920connection requests will be queued for when you resume the server; you
1921can increase this limit, unless it is imposed by the operating
1922system.) For a network stream connection, being stopped means not
1923processing input (any arriving input waits until you resume the
1924connection). For a datagram connection, some number of packets may be
1925queued but input may be lost. You can use the function
1926@code{process-command} to determine whether a network connection or
1927server is stopped; a non-@code{nil} value means yes.
1928
1929@defun open-network-stream name buffer-or-name host service
1930This function opens a TCP connection, and returns a process object
1931that represents the connection.
1932
1933The @var{name} argument specifies the name for the process object. It
1934is modified as necessary to make it unique.
1935
1936The @var{buffer-or-name} argument is the buffer to associate with the
1937connection. Output from the connection is inserted in the buffer,
1938unless you specify a filter function to handle the output. If
1939@var{buffer-or-name} is @code{nil}, it means that the connection is not
1940associated with any buffer.
1941
1942The arguments @var{host} and @var{service} specify where to connect to;
1943@var{host} is the host name (a string), and @var{service} is the name of
1944a defined network service (a string) or a port number (an integer).
1945@end defun
1946
b8d4c8d0
GM
1947@node Network Servers
1948@section Network Servers
1949@cindex network servers
1950
1951 You create a server by calling @code{make-network-process} with
1952@code{:server t}. The server will listen for connection requests from
1953clients. When it accepts a client connection request, that creates a
1954new network connection, itself a process object, with the following
1955parameters:
1956
1957@itemize @bullet
1958@item
1959The connection's process name is constructed by concatenating the
1960server process' @var{name} with a client identification string. The
1961client identification string for an IPv4 connection looks like
1962@samp{<@var{a}.@var{b}.@var{c}.@var{d}:@var{p}>}. Otherwise, it is a
1963unique number in brackets, as in @samp{<@var{nnn}>}. The number
1964is unique for each connection in the Emacs session.
1965
1966@item
1967If the server's filter is non-@code{nil}, the connection process does
1968not get a separate process buffer; otherwise, Emacs creates a new
1969buffer for the purpose. The buffer name is the server's buffer name
1970or process name, concatenated with the client identification string.
1971
1972The server's process buffer value is never used directly by Emacs, but
1973it is passed to the log function, which can log connections by
1974inserting text there.
1975
1976@item
1977The communication type and the process filter and sentinel are
1978inherited from those of the server. The server never directly
1979uses its filter and sentinel; their sole purpose is to initialize
1980connections made to the server.
1981
1982@item
1983The connection's process contact info is set according to the client's
1984addressing information (typically an IP address and a port number).
1985This information is associated with the @code{process-contact}
1986keywords @code{:host}, @code{:service}, @code{:remote}.
1987
1988@item
1989The connection's local address is set up according to the port
1990number used for the connection.
1991
1992@item
1993The client process' plist is initialized from the server's plist.
1994@end itemize
1995
1996@node Datagrams
1997@section Datagrams
1998@cindex datagrams
1999
2000 A datagram connection communicates with individual packets rather
2001than streams of data. Each call to @code{process-send} sends one
2002datagram packet (@pxref{Input to Processes}), and each datagram
2003received results in one call to the filter function.
2004
2005 The datagram connection doesn't have to talk with the same remote
2006peer all the time. It has a @dfn{remote peer address} which specifies
2007where to send datagrams to. Each time an incoming datagram is passed
2008to the filter function, the peer address is set to the address that
2009datagram came from; that way, if the filter function sends a datagram,
2010it will go back to that place. You can specify the remote peer
2011address when you create the datagram connection using the
2012@code{:remote} keyword. You can change it later on by calling
2013@code{set-process-datagram-address}.
2014
2015@defun process-datagram-address process
2016If @var{process} is a datagram connection or server, this function
2017returns its remote peer address.
2018@end defun
2019
2020@defun set-process-datagram-address process address
2021If @var{process} is a datagram connection or server, this function
2022sets its remote peer address to @var{address}.
2023@end defun
2024
2025@node Low-Level Network
2026@section Low-Level Network Access
2027
2028 You can also create network connections by operating at a lower
2029level than that of @code{open-network-stream}, using
2030@code{make-network-process}.
2031
2032@menu
2033* Proc: Network Processes. Using @code{make-network-process}.
2034* Options: Network Options. Further control over network connections.
2035* Features: Network Feature Testing.
2036 Determining which network features work on
2037 the machine you are using.
2038@end menu
2039
2040@node Network Processes
2041@subsection @code{make-network-process}
2042
2043 The basic function for creating network connections and network
2044servers is @code{make-network-process}. It can do either of those
2045jobs, depending on the arguments you give it.
2046
2047@defun make-network-process &rest args
2048This function creates a network connection or server and returns the
2049process object that represents it. The arguments @var{args} are a
2050list of keyword/argument pairs. Omitting a keyword is always
2051equivalent to specifying it with value @code{nil}, except for
2052@code{:coding}, @code{:filter-multibyte}, and @code{:reuseaddr}. Here
2053are the meaningful keywords:
2054
2055@table @asis
2056@item :name @var{name}
2057Use the string @var{name} as the process name. It is modified if
2058necessary to make it unique.
2059
2060@item :type @var{type}
2061Specify the communication type. A value of @code{nil} specifies a
2062stream connection (the default); @code{datagram} specifies a datagram
f7704e26
CY
2063connection; @code{seqpacket} specifies a ``sequenced packet stream''
2064connection. Both connections and servers can be of these types.
b8d4c8d0
GM
2065
2066@item :server @var{server-flag}
2067If @var{server-flag} is non-@code{nil}, create a server. Otherwise,
2068create a connection. For a stream type server, @var{server-flag} may
2069be an integer which then specifies the length of the queue of pending
2070connections to the server. The default queue length is 5.
2071
2072@item :host @var{host}
2073Specify the host to connect to. @var{host} should be a host name or
2074Internet address, as a string, or the symbol @code{local} to specify
2075the local host. If you specify @var{host} for a server, it must
2076specify a valid address for the local host, and only clients
2077connecting to that address will be accepted.
2078
2079@item :service @var{service}
2080@var{service} specifies a port number to connect to, or, for a server,
2081the port number to listen on. It should be a service name that
2082translates to a port number, or an integer specifying the port number
2083directly. For a server, it can also be @code{t}, which means to let
2084the system select an unused port number.
2085
2086@item :family @var{family}
2087@var{family} specifies the address (and protocol) family for
2088communication. @code{nil} means determine the proper address family
2089automatically for the given @var{host} and @var{service}.
2090@code{local} specifies a Unix socket, in which case @var{host} is
2091ignored. @code{ipv4} and @code{ipv6} specify to use IPv4 and IPv6
2092respectively.
2093
2094@item :local @var{local-address}
2095For a server process, @var{local-address} is the address to listen on.
2096It overrides @var{family}, @var{host} and @var{service}, and you
2097may as well not specify them.
2098
2099@item :remote @var{remote-address}
2100For a connection, @var{remote-address} is the address to connect to.
2101It overrides @var{family}, @var{host} and @var{service}, and you
2102may as well not specify them.
2103
2104For a datagram server, @var{remote-address} specifies the initial
2105setting of the remote datagram address.
2106
2107The format of @var{local-address} or @var{remote-address} depends on
2108the address family:
2109
2110@itemize -
2111@item
2112An IPv4 address is represented as a five-element vector of four 8-bit
2113integers and one 16-bit integer
2114@code{[@var{a} @var{b} @var{c} @var{d} @var{p}]} corresponding to
2115numeric IPv4 address @var{a}.@var{b}.@var{c}.@var{d} and port number
2116@var{p}.
2117
2118@item
2119An IPv6 address is represented as a nine-element vector of 16-bit
2120integers @code{[@var{a} @var{b} @var{c} @var{d} @var{e} @var{f}
2121@var{g} @var{h} @var{p}]} corresponding to numeric IPv6 address
2122@var{a}:@var{b}:@var{c}:@var{d}:@var{e}:@var{f}:@var{g}:@var{h} and
2123port number @var{p}.
2124
2125@item
2126A local address is represented as a string which specifies the address
2127in the local address space.
2128
2129@item
2130An ``unsupported family'' address is represented by a cons
2131@code{(@var{f} . @var{av})}, where @var{f} is the family number and
2132@var{av} is a vector specifying the socket address using one element
2133per address data byte. Do not rely on this format in portable code,
2134as it may depend on implementation defined constants, data sizes, and
2135data structure alignment.
2136@end itemize
2137
2138@item :nowait @var{bool}
2139If @var{bool} is non-@code{nil} for a stream connection, return
2140without waiting for the connection to complete. When the connection
2141succeeds or fails, Emacs will call the sentinel function, with a
2142second argument matching @code{"open"} (if successful) or
2143@code{"failed"}. The default is to block, so that
2144@code{make-network-process} does not return until the connection
2145has succeeded or failed.
2146
2147@item :stop @var{stopped}
2148Start the network connection or server in the `stopped' state if
2149@var{stopped} is non-@code{nil}.
2150
2151@item :buffer @var{buffer}
2152Use @var{buffer} as the process buffer.
2153
2154@item :coding @var{coding}
2155Use @var{coding} as the coding system for this process. To specify
2156different coding systems for decoding data from the connection and for
2157encoding data sent to it, specify @code{(@var{decoding} .
2158@var{encoding})} for @var{coding}.
2159
2160If you don't specify this keyword at all, the default
2161is to determine the coding systems from the data.
2162
2163@item :noquery @var{query-flag}
2164Initialize the process query flag to @var{query-flag}.
2165@xref{Query Before Exit}.
2166
2167@item :filter @var{filter}
2168Initialize the process filter to @var{filter}.
2169
b8d4c8d0
GM
2170@item :sentinel @var{sentinel}
2171Initialize the process sentinel to @var{sentinel}.
2172
2173@item :log @var{log}
2174Initialize the log function of a server process to @var{log}. The log
2175function is called each time the server accepts a network connection
2176from a client. The arguments passed to the log function are
2177@var{server}, @var{connection}, and @var{message}, where @var{server}
2178is the server process, @var{connection} is the new process for the
2179connection, and @var{message} is a string describing what has
2180happened.
2181
2182@item :plist @var{plist}
2183Initialize the process plist to @var{plist}.
2184@end table
2185
2186The original argument list, modified with the actual connection
2187information, is available via the @code{process-contact} function.
2188@end defun
2189
2190@node Network Options
2191@subsection Network Options
2192
2193 The following network options can be specified when you create a
2194network process. Except for @code{:reuseaddr}, you can also set or
2195modify these options later, using @code{set-network-process-option}.
2196
2197 For a server process, the options specified with
2198@code{make-network-process} are not inherited by the client
2199connections, so you will need to set the necessary options for each
2200child connection as it is created.
2201
2202@table @asis
2203@item :bindtodevice @var{device-name}
2204If @var{device-name} is a non-empty string identifying a network
2205interface name (see @code{network-interface-list}), only handle
2206packets received on that interface. If @var{device-name} is @code{nil}
2207(the default), handle packets received on any interface.
2208
2209Using this option may require special privileges on some systems.
2210
2211@item :broadcast @var{broadcast-flag}
2212If @var{broadcast-flag} is non-@code{nil} for a datagram process, the
2213process will receive datagram packet sent to a broadcast address, and
2214be able to send packets to a broadcast address. Ignored for a stream
2215connection.
2216
2217@item :dontroute @var{dontroute-flag}
2218If @var{dontroute-flag} is non-@code{nil}, the process can only send
2219to hosts on the same network as the local host.
2220
2221@item :keepalive @var{keepalive-flag}
2222If @var{keepalive-flag} is non-@code{nil} for a stream connection,
2223enable exchange of low-level keep-alive messages.
2224
2225@item :linger @var{linger-arg}
2226If @var{linger-arg} is non-@code{nil}, wait for successful
2227transmission of all queued packets on the connection before it is
2228deleted (see @code{delete-process}). If @var{linger-arg} is an
2229integer, it specifies the maximum time in seconds to wait for queued
2230packets to be sent before closing the connection. Default is
2231@code{nil} which means to discard unsent queued packets when the
2232process is deleted.
2233
2234@item :oobinline @var{oobinline-flag}
2235If @var{oobinline-flag} is non-@code{nil} for a stream connection,
2236receive out-of-band data in the normal data stream. Otherwise, ignore
2237out-of-band data.
2238
2239@item :priority @var{priority}
2240Set the priority for packets sent on this connection to the integer
2241@var{priority}. The interpretation of this number is protocol
2242specific, such as setting the TOS (type of service) field on IP
2243packets sent on this connection. It may also have system dependent
2244effects, such as selecting a specific output queue on the network
2245interface.
2246
2247@item :reuseaddr @var{reuseaddr-flag}
2248If @var{reuseaddr-flag} is non-@code{nil} (the default) for a stream
2249server process, allow this server to reuse a specific port number (see
2250@code{:service}) unless another process on this host is already
2251listening on that port. If @var{reuseaddr-flag} is @code{nil}, there
2252may be a period of time after the last use of that port (by any
2253process on the host), where it is not possible to make a new server on
2254that port.
2255@end table
2256
106e6894 2257@defun set-network-process-option process option value &optional no-error
b8d4c8d0
GM
2258This function sets or modifies a network option for network process
2259@var{process}. See @code{make-network-process} for details of options
106e6894
CY
2260@var{option} and their corresponding values @var{value}. If
2261@var{no-error} is non-@code{nil}, this function returns @code{nil}
2262instead of signaling an error if @var{option} is not a supported
2263option. If the function successfully completes, it returns @code{t}.
b8d4c8d0
GM
2264
2265The current setting of an option is available via the
2266@code{process-contact} function.
2267@end defun
2268
2269@node Network Feature Testing
2270@subsection Testing Availability of Network Features
2271
2272 To test for the availability of a given network feature, use
2273@code{featurep} like this:
2274
2275@example
2276(featurep 'make-network-process '(@var{keyword} @var{value}))
2277@end example
2278
2279@noindent
2280The result of the first form is @code{t} if it works to specify
2281@var{keyword} with value @var{value} in @code{make-network-process}.
2282The result of the second form is @code{t} if @var{keyword} is
2283supported by @code{make-network-process}. Here are some of the
2284@var{keyword}---@var{value} pairs you can test in
2285this way.
2286
2287@table @code
2288@item (:nowait t)
2289Non-@code{nil} if non-blocking connect is supported.
2290@item (:type datagram)
2291Non-@code{nil} if datagrams are supported.
2292@item (:family local)
2293Non-@code{nil} if local (a.k.a.@: ``UNIX domain'') sockets are supported.
2294@item (:family ipv6)
2295Non-@code{nil} if IPv6 is supported.
2296@item (:service t)
2297Non-@code{nil} if the system can select the port for a server.
2298@end table
2299
2300 To test for the availability of a given network option, use
2301@code{featurep} like this:
2302
2303@example
2304(featurep 'make-network-process '@var{keyword})
2305@end example
2306
2307@noindent
2308Here are some of the options you can test in this way.
2309
2310@table @code
2311@item :bindtodevice
2312@itemx :broadcast
2313@itemx :dontroute
2314@itemx :keepalive
2315@itemx :linger
2316@itemx :oobinline
2317@itemx :priority
2318@itemx :reuseaddr
2319That particular network option is supported by
2320@code{make-network-process} and @code{set-network-process-option}.
2321@end table
2322
2323@node Misc Network
2324@section Misc Network Facilities
2325
2326 These additional functions are useful for creating and operating
305a7ef2
EZ
2327on network connections. Note that they are supported only on some
2328systems.
b8d4c8d0
GM
2329
2330@defun network-interface-list
2331This function returns a list describing the network interfaces
2332of the machine you are using. The value is an alist whose
2333elements have the form @code{(@var{name} . @var{address})}.
2334@var{address} has the same form as the @var{local-address}
2335and @var{remote-address} arguments to @code{make-network-process}.
2336@end defun
2337
2338@defun network-interface-info ifname
2339This function returns information about the network interface named
2340@var{ifname}. The value is a list of the form
2341@code{(@var{addr} @var{bcast} @var{netmask} @var{hwaddr} @var{flags})}.
2342
2343@table @var
2344@item addr
2345The Internet protocol address.
2346@item bcast
2347The broadcast address.
2348@item netmask
2349The network mask.
2350@item hwaddr
2351The layer 2 address (Ethernet MAC address, for instance).
2352@item flags
2353The current flags of the interface.
2354@end table
2355@end defun
2356
2357@defun format-network-address address &optional omit-port
2358This function converts the Lisp representation of a network address to
2359a string.
2360
2361A five-element vector @code{[@var{a} @var{b} @var{c} @var{d} @var{p}]}
2362represents an IPv4 address @var{a}.@var{b}.@var{c}.@var{d} and port
2363number @var{p}. @code{format-network-address} converts that to the
2364string @code{"@var{a}.@var{b}.@var{c}.@var{d}:@var{p}"}.
2365
2366A nine-element vector @code{[@var{a} @var{b} @var{c} @var{d} @var{e}
2367@var{f} @var{g} @var{h} @var{p}]} represents an IPv6 address along
2368with a port number. @code{format-network-address} converts that to
2369the string
2370@code{"[@var{a}:@var{b}:@var{c}:@var{d}:@var{e}:@var{f}:@var{g}:@var{h}]:@var{p}"}.
2371
2372If the vector does not include the port number, @var{p}, or if
2373@var{omit-port} is non-@code{nil}, the result does not include the
2374@code{:@var{p}} suffix.
2375@end defun
2376
c73e02fa
GM
2377@node Serial Ports
2378@section Communicating with Serial Ports
2379@cindex @file{/dev/tty}
2380@cindex @file{COM1}
545c2782 2381@cindex serial connections
c73e02fa
GM
2382
2383 Emacs can communicate with serial ports. For interactive use,
2384@kbd{M-x serial-term} opens a terminal window. In a Lisp program,
2385@code{make-serial-process} creates a process object.
2386
2387 The serial port can be configured at run-time, without having to
2388close and re-open it. The function @code{serial-process-configure}
2389lets you change the speed, bytesize, and other parameters. In a
2390terminal window created by @code{serial-term}, you can click on the
2391mode line for configuration.
2392
2393 A serial connection is represented by a process object which can be
2394used similar to a subprocess or network process. You can send and
2395receive data and configure the serial port. A serial process object
80e6b6df
EZ
2396has no process ID, you can't send signals to it, and the status codes
2397are different from other types of processes.
c73e02fa
GM
2398@code{delete-process} on the process object or @code{kill-buffer} on
2399the process buffer close the connection, but this does not affect the
2400device connected to the serial port.
2401
2402 The function @code{process-type} returns the symbol @code{serial}
80e6b6df 2403for a process object representing a serial port connection.
c73e02fa
GM
2404
2405 Serial ports are available on GNU/Linux, Unix, and Windows systems.
2406
80e6b6df 2407@deffn Command serial-term port speed
c73e02fa 2408Start a terminal-emulator for a serial port in a new buffer.
80e6b6df
EZ
2409@var{port} is the name of the serial port to which to connect. For
2410example, this could be @file{/dev/ttyS0} on Unix. On Windows, this
2411could be @file{COM1}, or @file{\\.\COM10} (double the backslashes in
2412Lisp strings).
c73e02fa
GM
2413
2414@var{speed} is the speed of the serial port in bits per second. 9600
80e6b6df
EZ
2415is a common value. The buffer is in Term mode; see @ref{Term Mode,,,
2416emacs, The GNU Emacs Manual}, for the commands to use in that buffer.
2417You can change the speed and the configuration in the mode line menu.
2418@end deffn
c73e02fa
GM
2419
2420@defun make-serial-process &rest args
80e6b6df
EZ
2421This function creates a process and a buffer. Arguments are specified
2422as keyword/argument pairs. Here's the list of the meaningful keywords:
c73e02fa
GM
2423
2424@table @code
80e6b6df
EZ
2425@item :port @var{port}@r{ (mandatory)}
2426This is the name of the serial port. On Unix and GNU systems, this is
2427a file name such as @file{/dev/ttyS0}. On Windows, this could be
2428@file{COM1}, or @file{\\.\COM10} for ports higher than @file{COM9}
2429(double the backslashes in Lisp strings).
2430
2431@item :speed @var{speed}@r{ (mandatory)}
2432The speed of the serial port in bits per second. This function calls
2433@code{serial-process-configure} to handle the speed.
2434
2435@item :name @var{name}
2436The name of the process. If @var{name} is not given, @var{port} will
2437serve as the process name as well.
2438
2439@item :buffer @var{buffer}
2440The buffer to associate with the process. The value could be either a
2441buffer or a string that names a buffer. Process output goes at the
2442end of that buffer, unless you specify an output stream or filter
2443function to handle the output. If @var{buffer} is not given, the
2444process buffer's name is taken from the value of the @code{:name}
2445keyword.
2446
2447@item :coding @var{coding}
c73e02fa
GM
2448If @var{coding} is a symbol, it specifies the coding system used for
2449both reading and writing for this process. If @var{coding} is a cons
2450@code{(decoding . encoding)}, @var{decoding} is used for reading, and
80e6b6df
EZ
2451@var{encoding} is used for writing. If not specified, the default is
2452to determine the coding systems from data itself.
c73e02fa 2453
80e6b6df
EZ
2454@item :noquery @var{query-flag}
2455Initialize the process query flag to @var{query-flag}. @xref{Query
2456Before Exit}. The flags defaults to @code{nil} if unspecified.
c73e02fa 2457
80e6b6df 2458@item :stop @var{bool}
c73e02fa
GM
2459Start process in the @code{stopped} state if @var{bool} is
2460non-@code{nil}. In the stopped state, a serial process does not
2461accept incoming data, but you can send outgoing data. The stopped
2462state is cleared by @code{continue-process} and set by
2463@code{stop-process}.
2464
80e6b6df 2465@item :filter @var{filter}
c73e02fa
GM
2466Install @var{filter} as the process filter.
2467
80e6b6df 2468@item :sentinel @var{sentinel}
c73e02fa
GM
2469Install @var{sentinel} as the process sentinel.
2470
80e6b6df 2471@item :plist @var{plist}
c73e02fa
GM
2472Install @var{plist} as the initial plist of the process.
2473
2474@item :speed
2475@itemx :bytesize
2476@itemx :parity
2477@itemx :stopbits
2478@itemx :flowcontrol
2479These arguments are handled by @code{serial-process-configure}, which
2480is called by @code{make-serial-process}.
2481@end table
2482
2483The original argument list, possibly modified by later configuration,
2484is available via the function @code{process-contact}.
2485
2486Examples:
2487
2488@example
2489(make-serial-process :port "/dev/ttyS0" :speed 9600)
2490
2491(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2492
80e6b6df
EZ
2493(make-serial-process :port "\\\\.\\COM13" :speed 1200
2494 :bytesize 7 :parity 'odd)
c73e02fa 2495
57eedec1
GM
2496(make-serial-process :port "/dev/tty.BlueConsole-SPP-1"
2497 :speed nil)
c73e02fa
GM
2498@end example
2499@end defun
2500
2501@defun serial-process-configure &rest args
80e6b6df
EZ
2502@cindex baud, in serial connections
2503@cindex bytesize, in serial connections
2504@cindex parity, in serial connections
2505@cindex stopbits, in serial connections
2506@cindex flowcontrol, in serial connections
2507
2508This functions configures a serial port connection. Arguments are
2509specified as keyword/argument pairs. Attributes that are not given
2510are re-initialized from the process's current configuration (available
2511via the function @code{process-contact}) or set to reasonable default
2512values. The following arguments are defined:
c73e02fa
GM
2513
2514@table @code
80e6b6df
EZ
2515@item :process @var{process}
2516@itemx :name @var{name}
2517@itemx :buffer @var{buffer}
2518@itemx :port @var{port}
c73e02fa
GM
2519Any of these arguments can be given to identify the process that is to
2520be configured. If none of these arguments is given, the current
2521buffer's process is used.
2522
2523@item :speed @var{speed}
545c2782
EZ
2524The speed of the serial port in bits per second, a.k.a.@: @dfn{baud
2525rate}. The value can be any number, but most serial ports work only
2526at a few defined values between 1200 and 115200, with 9600 being the
2527most common value. If @var{speed} is @code{nil}, the function ignores
2528all other arguments and does not configure the port. This may be
2529useful for special serial ports such as Bluetooth-to-serial converters
2530which can only be configured through AT commands sent through the
538395d9
EZ
2531connection. The value of @code{nil} for @var{speed} is valid only for
2532connections that were already opened by a previous call to
80e6b6df 2533@code{make-serial-process} or @code{serial-term}.
c73e02fa
GM
2534
2535@item :bytesize @var{bytesize}
80e6b6df
EZ
2536The number of bits per byte, which can be 7 or 8. If @var{bytesize}
2537is not given or @code{nil}, it defaults to 8.
c73e02fa
GM
2538
2539@item :parity @var{parity}
80e6b6df 2540The value can be @code{nil} (don't use parity), the symbol
c73e02fa 2541@code{odd} (use odd parity), or the symbol @code{even} (use even
80e6b6df 2542parity). If @var{parity} is not given, it defaults to no parity.
c73e02fa
GM
2543
2544@item :stopbits @var{stopbits}
80e6b6df
EZ
2545The number of stopbits used to terminate a transmission
2546of each byte. @var{stopbits} can be 1 or 2. If @var{stopbits} is not
2547given or @code{nil}, it defaults to 1.
c73e02fa
GM
2548
2549@item :flowcontrol @var{flowcontrol}
80e6b6df
EZ
2550The type of flow control to use for this connection, which is either
2551@code{nil} (don't use flow control), the symbol @code{hw} (use RTS/CTS
2552hardware flow control), or the symbol @code{sw} (use XON/XOFF software
2553flow control). If @var{flowcontrol} is not given, it defaults to no
2554flow control.
c73e02fa
GM
2555@end table
2556
2557@code{serial-process-configure} is called by @code{make-serial-process} for the
2558initial configuration of the serial port.
2559
2560Examples:
2561
2562@example
2563(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2564
57eedec1
GM
2565(serial-process-configure :buffer "COM1" :stopbits 1
2566 :parity 'odd :flowcontrol 'hw)
c73e02fa
GM
2567
2568(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2569@end example
2570@end defun
2571
b8d4c8d0
GM
2572@node Byte Packing
2573@section Packing and Unpacking Byte Arrays
2574@cindex byte packing and unpacking
2575
2576 This section describes how to pack and unpack arrays of bytes,
2577usually for binary network protocols. These functions convert byte arrays
2578to alists, and vice versa. The byte array can be represented as a
2579unibyte string or as a vector of integers, while the alist associates
2580symbols either with fixed-size objects or with recursive sub-alists.
2581
2582@cindex serializing
2583@cindex deserializing
2584@cindex packing
2585@cindex unpacking
2586 Conversion from byte arrays to nested alists is also known as
2587@dfn{deserializing} or @dfn{unpacking}, while going in the opposite
2588direction is also known as @dfn{serializing} or @dfn{packing}.
2589
2590@menu
2591* Bindat Spec:: Describing data layout.
2592* Bindat Functions:: Doing the unpacking and packing.
2593* Bindat Examples:: Samples of what bindat.el can do for you!
2594@end menu
2595
2596@node Bindat Spec
2597@subsection Describing Data Layout
2598
2599 To control unpacking and packing, you write a @dfn{data layout
2600specification}, a special nested list describing named and typed
2601@dfn{fields}. This specification controls length of each field to be
2602processed, and how to pack or unpack it. We normally keep bindat specs
2603in variables whose names end in @samp{-bindat-spec}; that kind of name
2604is automatically recognized as ``risky.''
2605
2606@cindex endianness
2607@cindex big endian
2608@cindex little endian
2609@cindex network byte ordering
2610 A field's @dfn{type} describes the size (in bytes) of the object
2611that the field represents and, in the case of multibyte fields, how
2612the bytes are ordered within the field. The two possible orderings
2613are ``big endian'' (also known as ``network byte ordering'') and
2614``little endian.'' For instance, the number @code{#x23cd} (decimal
26159165) in big endian would be the two bytes @code{#x23} @code{#xcd};
2616and in little endian, @code{#xcd} @code{#x23}. Here are the possible
2617type values:
2618
2619@table @code
2620@item u8
2621@itemx byte
2622Unsigned byte, with length 1.
2623
2624@item u16
2625@itemx word
2626@itemx short
2627Unsigned integer in network byte order, with length 2.
2628
2629@item u24
2630Unsigned integer in network byte order, with length 3.
2631
2632@item u32
2633@itemx dword
2634@itemx long
2635Unsigned integer in network byte order, with length 4.
2636Note: These values may be limited by Emacs' integer implementation limits.
2637
2638@item u16r
2639@itemx u24r
2640@itemx u32r
2641Unsigned integer in little endian order, with length 2, 3 and 4, respectively.
2642
2643@item str @var{len}
2644String of length @var{len}.
2645
2646@item strz @var{len}
2647Zero-terminated string, in a fixed-size field with length @var{len}.
2648
2649@item vec @var{len} [@var{type}]
2650Vector of @var{len} elements of type @var{type}, or bytes if not
2651@var{type} is specified.
2652The @var{type} is any of the simple types above, or another vector
2653specified as a list @code{(vec @var{len} [@var{type}])}.
2654
2655@item ip
2656Four-byte vector representing an Internet address. For example:
2657@code{[127 0 0 1]} for localhost.
2658
2659@item bits @var{len}
2660List of set bits in @var{len} bytes. The bytes are taken in big
2661endian order and the bits are numbered starting with @code{8 *
2662@var{len} @minus{} 1} and ending with zero. For example: @code{bits
26632} unpacks @code{#x28} @code{#x1c} to @code{(2 3 4 11 13)} and
2664@code{#x1c} @code{#x28} to @code{(3 5 10 11 12)}.
2665
2666@item (eval @var{form})
2667@var{form} is a Lisp expression evaluated at the moment the field is
2668unpacked or packed. The result of the evaluation should be one of the
2669above-listed type specifications.
2670@end table
2671
2672For a fixed-size field, the length @var{len} is given as an integer
2673specifying the number of bytes in the field.
2674
2675When the length of a field is not fixed, it typically depends on the
2676value of a preceding field. In this case, the length @var{len} can be
2677given either as a list @code{(@var{name} ...)} identifying a
2678@dfn{field name} in the format specified for @code{bindat-get-field}
2679below, or by an expression @code{(eval @var{form})} where @var{form}
2680should evaluate to an integer, specifying the field length.
2681
2682A field specification generally has the form @code{([@var{name}]
2683@var{handler})}. The square braces indicate that @var{name} is
2684optional. (Don't use names that are symbols meaningful as type
2685specifications (above) or handler specifications (below), since that
2686would be ambiguous.) @var{name} can be a symbol or the expression
2687@code{(eval @var{form})}, in which case @var{form} should evaluate to
2688a symbol.
2689
2690@var{handler} describes how to unpack or pack the field and can be one
2691of the following:
2692
2693@table @code
2694@item @var{type}
2695Unpack/pack this field according to the type specification @var{type}.
2696
2697@item eval @var{form}
2698Evaluate @var{form}, a Lisp expression, for side-effect only. If the
2699field name is specified, the value is bound to that field name.
2700
2701@item fill @var{len}
2702Skip @var{len} bytes. In packing, this leaves them unchanged,
2703which normally means they remain zero. In unpacking, this means
2704they are ignored.
2705
2706@item align @var{len}
2707Skip to the next multiple of @var{len} bytes.
2708
2709@item struct @var{spec-name}
2710Process @var{spec-name} as a sub-specification. This describes a
2711structure nested within another structure.
2712
2713@item union @var{form} (@var{tag} @var{spec})@dots{}
2714@c ??? I don't see how one would actually use this.
2715@c ??? what kind of expression would be useful for @var{form}?
2716Evaluate @var{form}, a Lisp expression, find the first @var{tag}
2717that matches it, and process its associated data layout specification
2718@var{spec}. Matching can occur in one of three ways:
2719
2720@itemize
2721@item
2722If a @var{tag} has the form @code{(eval @var{expr})}, evaluate
2723@var{expr} with the variable @code{tag} dynamically bound to the value
2724of @var{form}. A non-@code{nil} result indicates a match.
2725
2726@item
2727@var{tag} matches if it is @code{equal} to the value of @var{form}.
2728
2729@item
2730@var{tag} matches unconditionally if it is @code{t}.
2731@end itemize
2732
2733@item repeat @var{count} @var{field-specs}@dots{}
2734Process the @var{field-specs} recursively, in order, then repeat
2735starting from the first one, processing all the specs @var{count}
2736times overall. The @var{count} is given using the same formats as a
2737field length---if an @code{eval} form is used, it is evaluated just once.
2738For correct operation, each spec in @var{field-specs} must include a name.
2739@end table
2740
2741For the @code{(eval @var{form})} forms used in a bindat specification,
2742the @var{form} can access and update these dynamically bound variables
2743during evaluation:
2744
2745@table @code
2746@item last
2747Value of the last field processed.
2748
2749@item bindat-raw
2750The data as a byte array.
2751
2752@item bindat-idx
2753Current index (within @code{bindat-raw}) for unpacking or packing.
2754
2755@item struct
2756The alist containing the structured data that have been unpacked so
2757far, or the entire structure being packed. You can use
2758@code{bindat-get-field} to access specific fields of this structure.
2759
2760@item count
2761@itemx index
2762Inside a @code{repeat} block, these contain the maximum number of
2763repetitions (as specified by the @var{count} parameter), and the
2764current repetition number (counting from 0). Setting @code{count} to
2765zero will terminate the inner-most repeat block after the current
2766repetition has completed.
2767@end table
2768
2769@node Bindat Functions
2770@subsection Functions to Unpack and Pack Bytes
2771
2772 In the following documentation, @var{spec} refers to a data layout
2773specification, @code{bindat-raw} to a byte array, and @var{struct} to an
2774alist representing unpacked field data.
2775
2776@defun bindat-unpack spec bindat-raw &optional bindat-idx
2777This function unpacks data from the unibyte string or byte
2778array @code{bindat-raw}
2779according to @var{spec}. Normally this starts unpacking at the
2780beginning of the byte array, but if @var{bindat-idx} is non-@code{nil}, it
2781specifies a zero-based starting position to use instead.
2782
2783The value is an alist or nested alist in which each element describes
2784one unpacked field.
2785@end defun
2786
2787@defun bindat-get-field struct &rest name
2788This function selects a field's data from the nested alist
2789@var{struct}. Usually @var{struct} was returned by
2790@code{bindat-unpack}. If @var{name} corresponds to just one argument,
2791that means to extract a top-level field value. Multiple @var{name}
2792arguments specify repeated lookup of sub-structures. An integer name
2793acts as an array index.
2794
2795For example, if @var{name} is @code{(a b 2 c)}, that means to find
2796field @code{c} in the third element of subfield @code{b} of field
2797@code{a}. (This corresponds to @code{struct.a.b[2].c} in C.)
2798@end defun
2799
2800 Although packing and unpacking operations change the organization of
2801data (in memory), they preserve the data's @dfn{total length}, which is
2802the sum of all the fields' lengths, in bytes. This value is not
2803generally inherent in either the specification or alist alone; instead,
2804both pieces of information contribute to its calculation. Likewise, the
2805length of a string or array being unpacked may be longer than the data's
2806total length as described by the specification.
2807
2808@defun bindat-length spec struct
2809This function returns the total length of the data in @var{struct},
2810according to @var{spec}.
2811@end defun
2812
2813@defun bindat-pack spec struct &optional bindat-raw bindat-idx
2814This function returns a byte array packed according to @var{spec} from
2815the data in the alist @var{struct}. Normally it creates and fills a
2816new byte array starting at the beginning. However, if @var{bindat-raw}
2817is non-@code{nil}, it specifies a pre-allocated unibyte string or vector to
2818pack into. If @var{bindat-idx} is non-@code{nil}, it specifies the starting
2819offset for packing into @code{bindat-raw}.
2820
2821When pre-allocating, you should make sure @code{(length @var{bindat-raw})}
2822meets or exceeds the total length to avoid an out-of-range error.
2823@end defun
2824
2825@defun bindat-ip-to-string ip
2826Convert the Internet address vector @var{ip} to a string in the usual
2827dotted notation.
2828
2829@example
2830(bindat-ip-to-string [127 0 0 1])
2831 @result{} "127.0.0.1"
2832@end example
2833@end defun
2834
2835@node Bindat Examples
2836@subsection Examples of Byte Unpacking and Packing
2837
2838 Here is a complete example of byte unpacking and packing:
2839
2840@lisp
2841(defvar fcookie-index-spec
2842 '((:version u32)
2843 (:count u32)
2844 (:longest u32)
2845 (:shortest u32)
2846 (:flags u32)
2847 (:delim u8)
2848 (:ignored fill 3)
2849 (:offset repeat (:count)
2850 (:foo u32)))
2851 "Description of a fortune cookie index file's contents.")
2852
2853(defun fcookie (cookies &optional index)
2854 "Display a random fortune cookie from file COOKIES.
2855Optional second arg INDEX specifies the associated index
2856filename, which is by default constructed by appending
2857\".dat\" to COOKIES. Display cookie text in possibly
2858new buffer \"*Fortune Cookie: BASENAME*\" where BASENAME
2859is COOKIES without the directory part."
2860 (interactive "fCookies file: ")
2861 (let* ((info (with-temp-buffer
2862 (insert-file-contents-literally
2863 (or index (concat cookies ".dat")))
2864 (bindat-unpack fcookie-index-spec
2865 (buffer-string))))
2866 (sel (random (bindat-get-field info :count)))
2867 (beg (cdar (bindat-get-field info :offset sel)))
2868 (end (or (cdar (bindat-get-field info
2869 :offset (1+ sel)))
2870 (nth 7 (file-attributes cookies)))))
2871 (switch-to-buffer
2872 (get-buffer-create
2873 (format "*Fortune Cookie: %s*"
2874 (file-name-nondirectory cookies))))
2875 (erase-buffer)
2876 (insert-file-contents-literally
2877 cookies nil beg (- end 3))))
2878
2879(defun fcookie-create-index (cookies &optional index delim)
2880 "Scan file COOKIES, and write out its index file.
2881Optional second arg INDEX specifies the index filename,
2882which is by default constructed by appending \".dat\" to
2883COOKIES. Optional third arg DELIM specifies the unibyte
2884character which, when found on a line of its own in
2885COOKIES, indicates the border between entries."
2886 (interactive "fCookies file: ")
2887 (setq delim (or delim ?%))
2888 (let ((delim-line (format "\n%c\n" delim))
2889 (count 0)
2890 (max 0)
2891 min p q len offsets)
2892 (unless (= 3 (string-bytes delim-line))
2893 (error "Delimiter cannot be represented in one byte"))
2894 (with-temp-buffer
2895 (insert-file-contents-literally cookies)
2896 (while (and (setq p (point))
2897 (search-forward delim-line (point-max) t)
2898 (setq len (- (point) 3 p)))
2899 (setq count (1+ count)
2900 max (max max len)
2901 min (min (or min max) len)
2902 offsets (cons (1- p) offsets))))
2903 (with-temp-buffer
2904 (set-buffer-multibyte nil)
2905 (insert
2906 (bindat-pack
2907 fcookie-index-spec
2908 `((:version . 2)
2909 (:count . ,count)
2910 (:longest . ,max)
2911 (:shortest . ,min)
2912 (:flags . 0)
2913 (:delim . ,delim)
2914 (:offset . ,(mapcar (lambda (o)
2915 (list (cons :foo o)))
2916 (nreverse offsets))))))
2917 (let ((coding-system-for-write 'raw-text-unix))
2918 (write-file (or index (concat cookies ".dat")))))))
2919@end lisp
2920
2921Following is an example of defining and unpacking a complex structure.
2922Consider the following C structures:
2923
2924@example
2925struct header @{
2926 unsigned long dest_ip;
2927 unsigned long src_ip;
2928 unsigned short dest_port;
2929 unsigned short src_port;
2930@};
2931
2932struct data @{
2933 unsigned char type;
2934 unsigned char opcode;
2935 unsigned short length; /* In network byte order */
2936 unsigned char id[8]; /* null-terminated string */
2937 unsigned char data[/* (length + 3) & ~3 */];
2938@};
2939
2940struct packet @{
2941 struct header header;
2942 unsigned long counters[2]; /* In little endian order */
2943 unsigned char items;
2944 unsigned char filler[3];
2945 struct data item[/* items */];
2946
2947@};
2948@end example
2949
2950The corresponding data layout specification:
2951
2952@lisp
2953(setq header-spec
2954 '((dest-ip ip)
2955 (src-ip ip)
2956 (dest-port u16)
2957 (src-port u16)))
2958
2959(setq data-spec
2960 '((type u8)
2961 (opcode u8)
2962 (length u16) ;; network byte order
2963 (id strz 8)
2964 (data vec (length))
2965 (align 4)))
2966
2967(setq packet-spec
2968 '((header struct header-spec)
2969 (counters vec 2 u32r) ;; little endian order
2970 (items u8)
2971 (fill 3)
2972 (item repeat (items)
2973 (struct data-spec))))
2974@end lisp
2975
2976A binary data representation:
2977
2978@lisp
2979(setq binary-data
2980 [ 192 168 1 100 192 168 1 101 01 28 21 32
2981 160 134 1 0 5 1 0 0 2 0 0 0
2982 2 3 0 5 ?A ?B ?C ?D ?E ?F 0 0 1 2 3 4 5 0 0 0
2983 1 4 0 7 ?B ?C ?D ?E ?F ?G 0 0 6 7 8 9 10 11 12 0 ])
2984@end lisp
2985
2986The corresponding decoded structure:
2987
2988@lisp
2989(setq decoded (bindat-unpack packet-spec binary-data))
2990 @result{}
2991((header
2992 (dest-ip . [192 168 1 100])
2993 (src-ip . [192 168 1 101])
2994 (dest-port . 284)
2995 (src-port . 5408))
2996 (counters . [100000 261])
2997 (items . 2)
2998 (item ((data . [1 2 3 4 5])
2999 (id . "ABCDEF")
3000 (length . 5)
3001 (opcode . 3)
3002 (type . 2))
3003 ((data . [6 7 8 9 10 11 12])
3004 (id . "BCDEFG")
3005 (length . 7)
3006 (opcode . 4)
3007 (type . 1))))
3008@end lisp
3009
3010Fetching data from this structure:
3011
3012@lisp
3013(bindat-get-field decoded 'item 1 'id)
3014 @result{} "BCDEFG"
3015@end lisp
3016
3017@ignore
3018 arch-tag: ba9da253-e65f-4e7f-b727-08fba0a1df7a
3019@end ignore