Commit | Line | Data |
---|---|---|
d0d6b7c5 | 1 | /* Asynchronous subprocess control for GNU Emacs. |
95df8112 | 2 | |
ba318903 PE |
3 | Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2014 Free Software |
4 | Foundation, Inc. | |
d0d6b7c5 JB |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
d0d6b7c5 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
d0d6b7c5 JB |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
d0d6b7c5 JB |
20 | |
21 | ||
18160b98 | 22 | #include <config.h> |
6a09a33b | 23 | |
d0d6b7c5 JB |
24 | #include <stdio.h> |
25 | #include <errno.h> | |
6c60eb9f | 26 | #include <sys/types.h> /* Some typedefs are used in sys/file.h. */ |
d0d6b7c5 JB |
27 | #include <sys/file.h> |
28 | #include <sys/stat.h> | |
93b4f699 | 29 | #include <unistd.h> |
e98d950b | 30 | #include <fcntl.h> |
e98d950b | 31 | |
0898ca10 JB |
32 | #include "lisp.h" |
33 | ||
f388c88a EZ |
34 | /* Only MS-DOS does not define `subprocesses'. */ |
35 | #ifdef subprocesses | |
36 | ||
d0d6b7c5 JB |
37 | #include <sys/socket.h> |
38 | #include <netdb.h> | |
39 | #include <netinet/in.h> | |
40 | #include <arpa/inet.h> | |
e690ca94 KS |
41 | |
42 | /* Are local (unix) sockets supported? */ | |
4624371d | 43 | #if defined (HAVE_SYS_UN_H) |
e690ca94 KS |
44 | #if !defined (AF_LOCAL) && defined (AF_UNIX) |
45 | #define AF_LOCAL AF_UNIX | |
46 | #endif | |
47 | #ifdef AF_LOCAL | |
48 | #define HAVE_LOCAL_SOCKETS | |
49 | #include <sys/un.h> | |
50 | #endif | |
51 | #endif | |
d0d6b7c5 | 52 | |
d0d6b7c5 | 53 | #include <sys/ioctl.h> |
5e617bc2 | 54 | #if defined (HAVE_NET_IF_H) |
32bc6709 DN |
55 | #include <net/if.h> |
56 | #endif /* HAVE_NET_IF_H */ | |
d0d6b7c5 | 57 | |
5e617bc2 | 58 | #if defined (HAVE_IFADDRS_H) |
377538cb JD |
59 | /* Must be after net/if.h */ |
60 | #include <ifaddrs.h> | |
61 | ||
62 | /* We only use structs from this header when we use getifaddrs. */ | |
5e617bc2 | 63 | #if defined (HAVE_NET_IF_DL_H) |
377538cb JD |
64 | #include <net/if_dl.h> |
65 | #endif | |
66 | ||
67 | #endif | |
68 | ||
d0d6b7c5 JB |
69 | #ifdef NEED_BSDTTY |
70 | #include <bsdtty.h> | |
71 | #endif | |
72 | ||
4a4bbad2 PE |
73 | #ifdef USG5_4 |
74 | # include <sys/stream.h> | |
75 | # include <sys/stropts.h> | |
76 | #endif | |
77 | ||
08116b48 | 78 | #ifdef HAVE_RES_INIT |
08116b48 CY |
79 | #include <arpa/nameser.h> |
80 | #include <resolv.h> | |
81 | #endif | |
82 | ||
3d608a86 J |
83 | #ifdef HAVE_UTIL_H |
84 | #include <util.h> | |
85 | #endif | |
86 | ||
9bf58201 DN |
87 | #ifdef HAVE_PTY_H |
88 | #include <pty.h> | |
89 | #endif | |
90 | ||
d983a10b PE |
91 | #include <c-ctype.h> |
92 | #include <sig2str.h> | |
94fcd171 | 93 | #include <verify.h> |
d983a10b | 94 | |
f388c88a EZ |
95 | #endif /* subprocesses */ |
96 | ||
d0d6b7c5 | 97 | #include "systime.h" |
36ebaafa | 98 | #include "systty.h" |
d0d6b7c5 | 99 | |
d0d6b7c5 | 100 | #include "window.h" |
5bbf78c1 | 101 | #include "character.h" |
e5560ff7 | 102 | #include "buffer.h" |
0fa1789e | 103 | #include "coding.h" |
d0d6b7c5 | 104 | #include "process.h" |
428a555e | 105 | #include "frame.h" |
d0d6b7c5 JB |
106 | #include "termhooks.h" |
107 | #include "termopts.h" | |
108 | #include "commands.h" | |
3ec68006 | 109 | #include "keyboard.h" |
ececcbec | 110 | #include "blockinput.h" |
dfcf069d | 111 | #include "dispextern.h" |
e0016554 | 112 | #include "composite.h" |
30904ab7 | 113 | #include "atimer.h" |
f388c88a EZ |
114 | #include "sysselect.h" |
115 | #include "syssignal.h" | |
116 | #include "syswait.h" | |
8af55556 TZ |
117 | #ifdef HAVE_GNUTLS |
118 | #include "gnutls.h" | |
119 | #endif | |
d0d6b7c5 | 120 | |
17a2cbbd DC |
121 | #ifdef HAVE_WINDOW_SYSTEM |
122 | #include TERM_HEADER | |
123 | #endif /* HAVE_WINDOW_SYSTEM */ | |
124 | ||
69f60cdc | 125 | #ifdef HAVE_GLIB |
872870b2 | 126 | #include "xgselect.h" |
69f60cdc JD |
127 | #ifndef WINDOWSNT |
128 | #include <glib.h> | |
129 | #endif | |
0949d2b6 | 130 | #endif |
57507bf8 | 131 | |
a68089e4 | 132 | #ifdef WINDOWSNT |
d486344e | 133 | extern int sys_select (int, fd_set *, fd_set *, fd_set *, |
43aac990 | 134 | struct timespec *, void *); |
a68089e4 EZ |
135 | #endif |
136 | ||
067428c1 PE |
137 | #ifndef SOCK_CLOEXEC |
138 | # define SOCK_CLOEXEC 0 | |
9caab067 PE |
139 | #endif |
140 | ||
141 | #ifndef HAVE_ACCEPT4 | |
067428c1 PE |
142 | |
143 | /* Emulate GNU/Linux accept4 and socket well enough for this module. */ | |
144 | ||
145 | static int | |
146 | close_on_exec (int fd) | |
147 | { | |
148 | if (0 <= fd) | |
149 | fcntl (fd, F_SETFD, FD_CLOEXEC); | |
150 | return fd; | |
151 | } | |
152 | ||
153 | static int | |
154 | accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) | |
155 | { | |
156 | return close_on_exec (accept (sockfd, addr, addrlen)); | |
157 | } | |
158 | ||
159 | static int | |
160 | process_socket (int domain, int type, int protocol) | |
161 | { | |
162 | return close_on_exec (socket (domain, type, protocol)); | |
163 | } | |
164 | # undef socket | |
165 | # define socket(domain, type, protocol) process_socket (domain, type, protocol) | |
166 | #endif | |
167 | ||
b8df54ff PE |
168 | /* Work around GCC 4.7.0 bug with strict overflow checking; see |
169 | <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>. | |
170 | These lines can be removed once the GCC bug is fixed. */ | |
908589fd | 171 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) |
b8df54ff PE |
172 | # pragma GCC diagnostic ignored "-Wstrict-overflow" |
173 | #endif | |
174 | ||
fdb183d6 DN |
175 | Lisp_Object Qeuid, Qegid, Qcomm, Qstate, Qppid, Qpgrp, Qsess, Qttname, Qtpgid; |
176 | Lisp_Object Qminflt, Qmajflt, Qcminflt, Qcmajflt, Qutime, Qstime, Qcstime; | |
177 | Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs; | |
178 | Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime; | |
179 | Lisp_Object QCname, QCtype; | |
180 | \f | |
0bce5d9e | 181 | /* True if keyboard input is on hold, zero otherwise. */ |
fdb183d6 | 182 | |
0bce5d9e | 183 | static bool kbd_is_on_hold; |
fdb183d6 | 184 | |
fdb183d6 DN |
185 | /* Nonzero means don't run process sentinels. This is used |
186 | when exiting. */ | |
0bce5d9e | 187 | bool inhibit_sentinels; |
fdb183d6 | 188 | |
f388c88a EZ |
189 | #ifdef subprocesses |
190 | ||
dd2281ae | 191 | Lisp_Object Qprocessp; |
955cbe7b PE |
192 | static Lisp_Object Qrun, Qstop, Qsignal; |
193 | static Lisp_Object Qopen, Qclosed, Qconnect, Qfailed, Qlisten; | |
194 | Lisp_Object Qlocal; | |
195 | static Lisp_Object Qipv4, Qdatagram, Qseqpacket; | |
196 | static Lisp_Object Qreal, Qnetwork, Qserial; | |
e1652a86 | 197 | #ifdef AF_INET6 |
955cbe7b | 198 | static Lisp_Object Qipv6; |
e1652a86 | 199 | #endif |
955cbe7b PE |
200 | static Lisp_Object QCport, QCprocess; |
201 | Lisp_Object QCspeed; | |
d888760c GM |
202 | Lisp_Object QCbytesize, QCstopbits, QCparity, Qodd, Qeven; |
203 | Lisp_Object QCflowcontrol, Qhw, Qsw, QCsummary; | |
955cbe7b PE |
204 | static Lisp_Object QCbuffer, QChost, QCservice; |
205 | static Lisp_Object QClocal, QCremote, QCcoding; | |
206 | static Lisp_Object QCserver, QCnowait, QCnoquery, QCstop; | |
207 | static Lisp_Object QCsentinel, QClog, QCoptions, QCplist; | |
208 | static Lisp_Object Qlast_nonmenu_event; | |
1aa8d505 SM |
209 | static Lisp_Object Qinternal_default_process_sentinel; |
210 | static Lisp_Object Qinternal_default_process_filter; | |
e0f712ba | 211 | |
4d2b044c DA |
212 | #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork)) |
213 | #define NETCONN1_P(p) (EQ (p->type, Qnetwork)) | |
214 | #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial)) | |
215 | #define SERIALCONN1_P(p) (EQ (p->type, Qserial)) | |
d0d6b7c5 | 216 | |
d0d6b7c5 | 217 | /* Number of events of change of status of a process. */ |
e4d81efc | 218 | static EMACS_INT process_tick; |
d0d6b7c5 | 219 | /* Number of events for which the user or sentinel has been notified. */ |
e4d81efc | 220 | static EMACS_INT update_tick; |
d0d6b7c5 | 221 | |
dd2a17ab KS |
222 | /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */ |
223 | ||
82d6e50b | 224 | /* Only W32 has this, it really means that select can't take write mask. */ |
dd2a17ab KS |
225 | #ifdef BROKEN_NON_BLOCKING_CONNECT |
226 | #undef NON_BLOCKING_CONNECT | |
82d6e50b | 227 | #define SELECT_CANT_DO_WRITE_MASK |
dd2a17ab KS |
228 | #else |
229 | #ifndef NON_BLOCKING_CONNECT | |
dd2a17ab KS |
230 | #ifdef HAVE_SELECT |
231 | #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX) | |
dd2a17ab KS |
232 | #if defined (EWOULDBLOCK) || defined (EINPROGRESS) |
233 | #define NON_BLOCKING_CONNECT | |
234 | #endif /* EWOULDBLOCK || EINPROGRESS */ | |
dd2a17ab KS |
235 | #endif /* HAVE_GETPEERNAME || GNU_LINUX */ |
236 | #endif /* HAVE_SELECT */ | |
dd2a17ab KS |
237 | #endif /* NON_BLOCKING_CONNECT */ |
238 | #endif /* BROKEN_NON_BLOCKING_CONNECT */ | |
239 | ||
e690ca94 KS |
240 | /* Define DATAGRAM_SOCKETS if datagrams can be used safely on |
241 | this system. We need to read full packets, so we need a | |
242 | "non-destructive" select. So we require either native select, | |
243 | or emulation of select using FIONREAD. */ | |
244 | ||
4a4bbad2 PE |
245 | #ifndef BROKEN_DATAGRAM_SOCKETS |
246 | # if defined HAVE_SELECT || defined USABLE_FIONREAD | |
247 | # if defined HAVE_SENDTO && defined HAVE_RECVFROM && defined EMSGSIZE | |
248 | # define DATAGRAM_SOCKETS | |
249 | # endif | |
250 | # endif | |
251 | #endif | |
e690ca94 | 252 | |
f00c449b SM |
253 | #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS |
254 | # define HAVE_SEQPACKET | |
255 | #endif | |
256 | ||
6b61353c | 257 | #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING) |
6b61353c KH |
258 | #define ADAPTIVE_READ_BUFFERING |
259 | #endif | |
6b61353c KH |
260 | |
261 | #ifdef ADAPTIVE_READ_BUFFERING | |
43aac990 | 262 | #define READ_OUTPUT_DELAY_INCREMENT (TIMESPEC_RESOLUTION / 100) |
6b61353c KH |
263 | #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5) |
264 | #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7) | |
265 | ||
db853b7a KS |
266 | /* Number of processes which have a non-zero read_output_delay, |
267 | and therefore might be delayed for adaptive read buffering. */ | |
6b61353c KH |
268 | |
269 | static int process_output_delay_count; | |
270 | ||
0bce5d9e | 271 | /* True if any process has non-nil read_output_skip. */ |
6b61353c | 272 | |
0bce5d9e | 273 | static bool process_output_skip; |
6b61353c | 274 | |
6b61353c KH |
275 | #else |
276 | #define process_output_delay_count 0 | |
277 | #endif | |
278 | ||
40ccffa6 | 279 | static void create_process (Lisp_Object, char **, Lisp_Object); |
4a4bbad2 | 280 | #ifdef USABLE_SIGIO |
d486344e | 281 | static bool keyboard_bit_set (fd_set *); |
006c5daa | 282 | #endif |
f57e2426 J |
283 | static void deactivate_process (Lisp_Object); |
284 | static void status_notify (struct Lisp_Process *); | |
285 | static int read_process_output (Lisp_Object, int); | |
4d7e6e51 | 286 | static void handle_child_signal (int); |
f57e2426 | 287 | static void create_pty (Lisp_Object); |
41d03b9a | 288 | |
583dcae4 | 289 | /* If we support a window system, turn on the code to poll periodically |
44ade2e9 | 290 | to detect C-g. It isn't actually used when doing interrupt input. */ |
4d7e6e51 | 291 | #ifdef HAVE_WINDOW_SYSTEM |
44ade2e9 RS |
292 | #define POLL_FOR_INPUT |
293 | #endif | |
294 | ||
d3da34e0 JB |
295 | static Lisp_Object get_process (register Lisp_Object name); |
296 | static void exec_sentinel (Lisp_Object proc, Lisp_Object reason); | |
84af9896 | 297 | |
f388c88a EZ |
298 | /* Mask of bits indicating the descriptors that we wait for input on. */ |
299 | ||
d486344e | 300 | static fd_set input_wait_mask; |
f388c88a | 301 | |
994d9841 | 302 | /* Mask that excludes keyboard input descriptor(s). */ |
a69281ff | 303 | |
d486344e | 304 | static fd_set non_keyboard_wait_mask; |
a69281ff | 305 | |
994d9841 | 306 | /* Mask that excludes process input descriptor(s). */ |
b5dc1c83 | 307 | |
d486344e | 308 | static fd_set non_process_wait_mask; |
b5dc1c83 | 309 | |
3fad2ad2 | 310 | /* Mask for selecting for write. */ |
994d9841 | 311 | |
d486344e | 312 | static fd_set write_mask; |
994d9841 | 313 | |
bad49fc7 | 314 | #ifdef NON_BLOCKING_CONNECT |
dd2a17ab KS |
315 | /* Mask of bits indicating the descriptors that we wait for connect to |
316 | complete on. Once they complete, they are removed from this mask | |
317 | and added to the input_wait_mask and non_keyboard_wait_mask. */ | |
318 | ||
d486344e | 319 | static fd_set connect_wait_mask; |
dd2a17ab KS |
320 | |
321 | /* Number of bits set in connect_wait_mask. */ | |
322 | static int num_pending_connects; | |
f388c88a | 323 | #endif /* NON_BLOCKING_CONNECT */ |
bad49fc7 | 324 | |
2878ba7e | 325 | /* The largest descriptor currently in use for a process object; -1 if none. */ |
7d0e672e RS |
326 | static int max_process_desc; |
327 | ||
2878ba7e | 328 | /* The largest descriptor currently in use for input; -1 if none. */ |
3fad2ad2 | 329 | static int max_input_desc; |
994d9841 | 330 | |
d0d6b7c5 | 331 | /* Indexed by descriptor, gives the process (if any) for that descriptor */ |
d486344e | 332 | static Lisp_Object chan_process[FD_SETSIZE]; |
d0d6b7c5 JB |
333 | |
334 | /* Alist of elements (NAME . PROCESS) */ | |
40ccffa6 | 335 | static Lisp_Object Vprocess_alist; |
d0d6b7c5 JB |
336 | |
337 | /* Buffered-ahead input char from process, indexed by channel. | |
338 | -1 means empty (no char is buffered). | |
339 | Used on sys V where the only way to tell if there is any | |
340 | output from the process is to read at least one char. | |
341 | Always -1 on systems that support FIONREAD. */ | |
342 | ||
d486344e | 343 | static int proc_buffered_char[FD_SETSIZE]; |
dd2281ae | 344 | |
0fa1789e | 345 | /* Table of `struct coding-system' for each process. */ |
d486344e PE |
346 | static struct coding_system *proc_decode_coding_system[FD_SETSIZE]; |
347 | static struct coding_system *proc_encode_coding_system[FD_SETSIZE]; | |
0fa1789e | 348 | |
e690ca94 KS |
349 | #ifdef DATAGRAM_SOCKETS |
350 | /* Table of `partner address' for datagram sockets. */ | |
40ccffa6 | 351 | static struct sockaddr_and_len { |
e690ca94 KS |
352 | struct sockaddr *sa; |
353 | int len; | |
d486344e | 354 | } datagram_address[FD_SETSIZE]; |
e690ca94 | 355 | #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0) |
60f0fb11 | 356 | #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0) |
e690ca94 KS |
357 | #else |
358 | #define DATAGRAM_CHAN_P(chan) (0) | |
359 | #define DATAGRAM_CONN_P(proc) (0) | |
360 | #endif | |
361 | ||
d5a1acfa DA |
362 | /* FOR_EACH_PROCESS (LIST_VAR, PROC_VAR) followed by a statement is |
363 | a `for' loop which iterates over processes from Vprocess_alist. */ | |
364 | ||
365 | #define FOR_EACH_PROCESS(list_var, proc_var) \ | |
366 | FOR_EACH_ALIST_VALUE (Vprocess_alist, list_var, proc_var) | |
367 | ||
6a09a33b | 368 | /* These setters are used only in this file, so they can be private. */ |
b0ab8123 | 369 | static void |
6a09a33b PE |
370 | pset_buffer (struct Lisp_Process *p, Lisp_Object val) |
371 | { | |
372 | p->buffer = val; | |
373 | } | |
b0ab8123 | 374 | static void |
6a09a33b PE |
375 | pset_command (struct Lisp_Process *p, Lisp_Object val) |
376 | { | |
377 | p->command = val; | |
378 | } | |
b0ab8123 | 379 | static void |
6a09a33b PE |
380 | pset_decode_coding_system (struct Lisp_Process *p, Lisp_Object val) |
381 | { | |
382 | p->decode_coding_system = val; | |
383 | } | |
b0ab8123 | 384 | static void |
6a09a33b PE |
385 | pset_decoding_buf (struct Lisp_Process *p, Lisp_Object val) |
386 | { | |
387 | p->decoding_buf = val; | |
388 | } | |
b0ab8123 | 389 | static void |
6a09a33b PE |
390 | pset_encode_coding_system (struct Lisp_Process *p, Lisp_Object val) |
391 | { | |
392 | p->encode_coding_system = val; | |
393 | } | |
b0ab8123 | 394 | static void |
6a09a33b PE |
395 | pset_encoding_buf (struct Lisp_Process *p, Lisp_Object val) |
396 | { | |
397 | p->encoding_buf = val; | |
398 | } | |
b0ab8123 | 399 | static void |
6a09a33b PE |
400 | pset_filter (struct Lisp_Process *p, Lisp_Object val) |
401 | { | |
1aa8d505 | 402 | p->filter = NILP (val) ? Qinternal_default_process_filter : val; |
6a09a33b | 403 | } |
b0ab8123 | 404 | static void |
6a09a33b PE |
405 | pset_log (struct Lisp_Process *p, Lisp_Object val) |
406 | { | |
407 | p->log = val; | |
408 | } | |
b0ab8123 | 409 | static void |
6a09a33b PE |
410 | pset_mark (struct Lisp_Process *p, Lisp_Object val) |
411 | { | |
412 | p->mark = val; | |
413 | } | |
b0ab8123 | 414 | static void |
6a09a33b PE |
415 | pset_name (struct Lisp_Process *p, Lisp_Object val) |
416 | { | |
417 | p->name = val; | |
418 | } | |
b0ab8123 | 419 | static void |
6a09a33b PE |
420 | pset_plist (struct Lisp_Process *p, Lisp_Object val) |
421 | { | |
422 | p->plist = val; | |
423 | } | |
b0ab8123 | 424 | static void |
6a09a33b PE |
425 | pset_sentinel (struct Lisp_Process *p, Lisp_Object val) |
426 | { | |
1aa8d505 | 427 | p->sentinel = NILP (val) ? Qinternal_default_process_sentinel : val; |
6a09a33b | 428 | } |
b0ab8123 | 429 | static void |
6a09a33b PE |
430 | pset_status (struct Lisp_Process *p, Lisp_Object val) |
431 | { | |
432 | p->status = val; | |
433 | } | |
b0ab8123 | 434 | static void |
6a09a33b PE |
435 | pset_tty_name (struct Lisp_Process *p, Lisp_Object val) |
436 | { | |
437 | p->tty_name = val; | |
438 | } | |
b0ab8123 | 439 | static void |
6a09a33b PE |
440 | pset_type (struct Lisp_Process *p, Lisp_Object val) |
441 | { | |
442 | p->type = val; | |
443 | } | |
b0ab8123 | 444 | static void |
6a09a33b PE |
445 | pset_write_queue (struct Lisp_Process *p, Lisp_Object val) |
446 | { | |
447 | p->write_queue = val; | |
448 | } | |
449 | ||
d0d6b7c5 | 450 | \f |
3fad2ad2 | 451 | |
40ccffa6 | 452 | static struct fd_callback_data |
3fad2ad2 J |
453 | { |
454 | fd_callback func; | |
455 | void *data; | |
456 | #define FOR_READ 1 | |
457 | #define FOR_WRITE 2 | |
458 | int condition; /* mask of the defines above. */ | |
d486344e | 459 | } fd_callback_info[FD_SETSIZE]; |
3fad2ad2 J |
460 | |
461 | ||
462 | /* Add a file descriptor FD to be monitored for when read is possible. | |
463 | When read is possible, call FUNC with argument DATA. */ | |
464 | ||
465 | void | |
466 | add_read_fd (int fd, fd_callback func, void *data) | |
467 | { | |
d486344e | 468 | eassert (fd < FD_SETSIZE); |
3fad2ad2 J |
469 | add_keyboard_wait_descriptor (fd); |
470 | ||
471 | fd_callback_info[fd].func = func; | |
472 | fd_callback_info[fd].data = data; | |
473 | fd_callback_info[fd].condition |= FOR_READ; | |
474 | } | |
475 | ||
476 | /* Stop monitoring file descriptor FD for when read is possible. */ | |
477 | ||
478 | void | |
479 | delete_read_fd (int fd) | |
480 | { | |
d486344e | 481 | eassert (fd < FD_SETSIZE); |
3fad2ad2 J |
482 | delete_keyboard_wait_descriptor (fd); |
483 | ||
484 | fd_callback_info[fd].condition &= ~FOR_READ; | |
485 | if (fd_callback_info[fd].condition == 0) | |
486 | { | |
487 | fd_callback_info[fd].func = 0; | |
488 | fd_callback_info[fd].data = 0; | |
489 | } | |
490 | } | |
491 | ||
492 | /* Add a file descriptor FD to be monitored for when write is possible. | |
493 | When write is possible, call FUNC with argument DATA. */ | |
494 | ||
495 | void | |
496 | add_write_fd (int fd, fd_callback func, void *data) | |
497 | { | |
d486344e | 498 | eassert (fd < FD_SETSIZE); |
3fad2ad2 J |
499 | FD_SET (fd, &write_mask); |
500 | if (fd > max_input_desc) | |
501 | max_input_desc = fd; | |
502 | ||
503 | fd_callback_info[fd].func = func; | |
504 | fd_callback_info[fd].data = data; | |
505 | fd_callback_info[fd].condition |= FOR_WRITE; | |
506 | } | |
507 | ||
2878ba7e PE |
508 | /* FD is no longer an input descriptor; update max_input_desc accordingly. */ |
509 | ||
510 | static void | |
511 | delete_input_desc (int fd) | |
512 | { | |
513 | if (fd == max_input_desc) | |
514 | { | |
515 | do | |
516 | fd--; | |
517 | while (0 <= fd && ! (FD_ISSET (fd, &input_wait_mask) | |
518 | || FD_ISSET (fd, &write_mask))); | |
519 | ||
520 | max_input_desc = fd; | |
521 | } | |
522 | } | |
523 | ||
3fad2ad2 J |
524 | /* Stop monitoring file descriptor FD for when write is possible. */ |
525 | ||
526 | void | |
527 | delete_write_fd (int fd) | |
528 | { | |
d486344e | 529 | eassert (fd < FD_SETSIZE); |
3fad2ad2 J |
530 | FD_CLR (fd, &write_mask); |
531 | fd_callback_info[fd].condition &= ~FOR_WRITE; | |
532 | if (fd_callback_info[fd].condition == 0) | |
533 | { | |
534 | fd_callback_info[fd].func = 0; | |
535 | fd_callback_info[fd].data = 0; | |
2878ba7e | 536 | delete_input_desc (fd); |
3fad2ad2 J |
537 | } |
538 | } | |
539 | ||
540 | \f | |
d0d6b7c5 JB |
541 | /* Compute the Lisp form of the process status, p->status, from |
542 | the numeric status that was returned by `wait'. */ | |
543 | ||
bba104c1 | 544 | static Lisp_Object status_convert (int); |
f9738840 | 545 | |
ff6daed3 | 546 | static void |
d3da34e0 | 547 | update_status (struct Lisp_Process *p) |
d0d6b7c5 | 548 | { |
6bfd98e7 | 549 | eassert (p->raw_status_new); |
6a09a33b | 550 | pset_status (p, status_convert (p->raw_status)); |
6bfd98e7 | 551 | p->raw_status_new = 0; |
d0d6b7c5 JB |
552 | } |
553 | ||
177c0ea7 | 554 | /* Convert a process status word in Unix format to |
d0d6b7c5 JB |
555 | the list that we use internally. */ |
556 | ||
ff6daed3 | 557 | static Lisp_Object |
bba104c1 | 558 | status_convert (int w) |
d0d6b7c5 JB |
559 | { |
560 | if (WIFSTOPPED (w)) | |
561 | return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil)); | |
562 | else if (WIFEXITED (w)) | |
13294f95 | 563 | return Fcons (Qexit, Fcons (make_number (WEXITSTATUS (w)), |
d0d6b7c5 JB |
564 | WCOREDUMP (w) ? Qt : Qnil)); |
565 | else if (WIFSIGNALED (w)) | |
566 | return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)), | |
567 | WCOREDUMP (w) ? Qt : Qnil)); | |
568 | else | |
569 | return Qrun; | |
570 | } | |
571 | ||
572 | /* Given a status-list, extract the three pieces of information | |
573 | and store them individually through the three pointers. */ | |
574 | ||
ff6daed3 | 575 | static void |
0bce5d9e | 576 | decode_status (Lisp_Object l, Lisp_Object *symbol, int *code, bool *coredump) |
d0d6b7c5 JB |
577 | { |
578 | Lisp_Object tem; | |
579 | ||
bcd69aea | 580 | if (SYMBOLP (l)) |
d0d6b7c5 JB |
581 | { |
582 | *symbol = l; | |
583 | *code = 0; | |
584 | *coredump = 0; | |
585 | } | |
586 | else | |
587 | { | |
70949dac KR |
588 | *symbol = XCAR (l); |
589 | tem = XCDR (l); | |
590 | *code = XFASTINT (XCAR (tem)); | |
591 | tem = XCDR (tem); | |
d0d6b7c5 JB |
592 | *coredump = !NILP (tem); |
593 | } | |
594 | } | |
595 | ||
596 | /* Return a string describing a process status list. */ | |
597 | ||
116c423c | 598 | static Lisp_Object |
d3da34e0 | 599 | status_message (struct Lisp_Process *p) |
d0d6b7c5 | 600 | { |
4d2b044c | 601 | Lisp_Object status = p->status; |
d0d6b7c5 | 602 | Lisp_Object symbol; |
0bce5d9e PE |
603 | int code; |
604 | bool coredump; | |
d0d6b7c5 JB |
605 | Lisp_Object string, string2; |
606 | ||
607 | decode_status (status, &symbol, &code, &coredump); | |
608 | ||
609 | if (EQ (symbol, Qsignal) || EQ (symbol, Qstop)) | |
610 | { | |
aa1ba90e | 611 | char const *signame; |
ca9c0567 | 612 | synchronize_system_messages_locale (); |
68c45bf0 | 613 | signame = strsignal (code); |
b97ad9ea | 614 | if (signame == 0) |
66bd43d1 KH |
615 | string = build_string ("unknown"); |
616 | else | |
617 | { | |
618 | int c1, c2; | |
619 | ||
d7ea76b4 | 620 | string = build_unibyte_string (signame); |
66bd43d1 KH |
621 | if (! NILP (Vlocale_coding_system)) |
622 | string = (code_convert_string_norecord | |
623 | (string, Vlocale_coding_system, 0)); | |
42a5b22f | 624 | c1 = STRING_CHAR (SDATA (string)); |
5da9919f | 625 | c2 = downcase (c1); |
66bd43d1 | 626 | if (c1 != c2) |
4735b74e | 627 | Faset (string, make_number (0), make_number (c2)); |
66bd43d1 | 628 | } |
d0d6b7c5 | 629 | string2 = build_string (coredump ? " (core dumped)\n" : "\n"); |
d0d6b7c5 JB |
630 | return concat2 (string, string2); |
631 | } | |
632 | else if (EQ (symbol, Qexit)) | |
633 | { | |
116c423c KS |
634 | if (NETCONN1_P (p)) |
635 | return build_string (code == 0 ? "deleted\n" : "connection broken by remote peer\n"); | |
d0d6b7c5 JB |
636 | if (code == 0) |
637 | return build_string ("finished\n"); | |
f2980264 | 638 | string = Fnumber_to_string (make_number (code)); |
d0d6b7c5 | 639 | string2 = build_string (coredump ? " (core dumped)\n" : "\n"); |
e690ca94 KS |
640 | return concat3 (build_string ("exited abnormally with code "), |
641 | string, string2); | |
d0d6b7c5 | 642 | } |
dd2a17ab KS |
643 | else if (EQ (symbol, Qfailed)) |
644 | { | |
645 | string = Fnumber_to_string (make_number (code)); | |
646 | string2 = build_string ("\n"); | |
e690ca94 KS |
647 | return concat3 (build_string ("failed with code "), |
648 | string, string2); | |
dd2a17ab | 649 | } |
d0d6b7c5 JB |
650 | else |
651 | return Fcopy_sequence (Fsymbol_name (symbol)); | |
652 | } | |
653 | \f | |
6496aec9 | 654 | enum { PTY_NAME_SIZE = 24 }; |
9bf58201 | 655 | |
875e6b94 | 656 | /* Open an available pty, returning a file descriptor. |
6496aec9 PE |
657 | Store into PTY_NAME the file name of the terminal corresponding to the pty. |
658 | Return -1 on failure. */ | |
875e6b94 | 659 | |
ff6daed3 | 660 | static int |
6496aec9 | 661 | allocate_pty (char pty_name[PTY_NAME_SIZE]) |
d0d6b7c5 | 662 | { |
6496aec9 | 663 | #ifdef HAVE_PTYS |
d0d6b7c5 JB |
664 | int fd; |
665 | ||
666 | #ifdef PTY_ITERATION | |
667 | PTY_ITERATION | |
668 | #else | |
be02381c | 669 | register int c, i; |
d0d6b7c5 JB |
670 | for (c = FIRST_PTY_LETTER; c <= 'z'; c++) |
671 | for (i = 0; i < 16; i++) | |
672 | #endif | |
673 | { | |
674 | #ifdef PTY_NAME_SPRINTF | |
675 | PTY_NAME_SPRINTF | |
d0d6b7c5 JB |
676 | #else |
677 | sprintf (pty_name, "/dev/pty%c%x", c, i); | |
d0d6b7c5 JB |
678 | #endif /* no PTY_NAME_SPRINTF */ |
679 | ||
4d7c105e RS |
680 | #ifdef PTY_OPEN |
681 | PTY_OPEN; | |
682 | #else /* no PTY_OPEN */ | |
49cdacda | 683 | fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0); |
4d7c105e | 684 | #endif /* no PTY_OPEN */ |
d0d6b7c5 JB |
685 | |
686 | if (fd >= 0) | |
687 | { | |
0d7c1e66 PE |
688 | #ifdef PTY_OPEN |
689 | /* Set FD's close-on-exec flag. This is needed even if | |
690 | PT_OPEN calls posix_openpt with O_CLOEXEC, since POSIX | |
691 | doesn't require support for that combination. | |
692 | Multithreaded platforms where posix_openpt ignores | |
693 | O_CLOEXEC (or where PTY_OPEN doesn't call posix_openpt) | |
694 | have a race condition between the PTY_OPEN and here. */ | |
695 | fcntl (fd, F_SETFD, FD_CLOEXEC); | |
696 | #endif | |
d0d6b7c5 JB |
697 | /* check to make certain that both sides are available |
698 | this avoids a nasty yet stupid bug in rlogins */ | |
699 | #ifdef PTY_TTY_NAME_SPRINTF | |
700 | PTY_TTY_NAME_SPRINTF | |
d0d6b7c5 | 701 | #else |
63136da6 | 702 | sprintf (pty_name, "/dev/tty%c%x", c, i); |
d0d6b7c5 | 703 | #endif /* no PTY_TTY_NAME_SPRINTF */ |
73dcdb9f | 704 | if (faccessat (AT_FDCWD, pty_name, R_OK | W_OK, AT_EACCESS) != 0) |
d0d6b7c5 | 705 | { |
68c45bf0 | 706 | emacs_close (fd); |
132d0475 | 707 | # ifndef __sgi |
d0d6b7c5 | 708 | continue; |
34967d0f | 709 | # else |
d0d6b7c5 | 710 | return -1; |
132d0475 | 711 | # endif /* __sgi */ |
d0d6b7c5 | 712 | } |
d0d6b7c5 JB |
713 | setup_pty (fd); |
714 | return fd; | |
715 | } | |
716 | } | |
6496aec9 | 717 | #endif /* HAVE_PTYS */ |
d0d6b7c5 JB |
718 | return -1; |
719 | } | |
d0d6b7c5 | 720 | \f |
ff6daed3 | 721 | static Lisp_Object |
d3da34e0 | 722 | make_process (Lisp_Object name) |
d0d6b7c5 JB |
723 | { |
724 | register Lisp_Object val, tem, name1; | |
725 | register struct Lisp_Process *p; | |
31c286f7 PE |
726 | char suffix[sizeof "<>" + INT_STRLEN_BOUND (printmax_t)]; |
727 | printmax_t i; | |
d0d6b7c5 | 728 | |
98423852 | 729 | p = allocate_process (); |
62efea5e DA |
730 | /* Initialize Lisp data. Note that allocate_process initializes all |
731 | Lisp data to nil, so do it only for slots which should not be nil. */ | |
6a09a33b PE |
732 | pset_status (p, Qrun); |
733 | pset_mark (p, Fmake_marker ()); | |
d0d6b7c5 | 734 | |
62efea5e DA |
735 | /* Initialize non-Lisp data. Note that allocate_process zeroes out all |
736 | non-Lisp data, so do it only for slots which should not be zero. */ | |
737 | p->infd = -1; | |
738 | p->outfd = -1; | |
94fcd171 PE |
739 | for (i = 0; i < PROCESS_OPEN_FDS; i++) |
740 | p->open_fd[i] = -1; | |
6b61353c | 741 | |
8af55556 TZ |
742 | #ifdef HAVE_GNUTLS |
743 | p->gnutls_initstage = GNUTLS_STAGE_EMPTY; | |
744 | #endif | |
745 | ||
d0d6b7c5 JB |
746 | /* If name is already in use, modify it until it is unused. */ |
747 | ||
748 | name1 = name; | |
749 | for (i = 1; ; i++) | |
750 | { | |
751 | tem = Fget_process (name1); | |
752 | if (NILP (tem)) break; | |
a8290ec3 | 753 | name1 = concat2 (name, make_formatted_string (suffix, "<%"pMd">", i)); |
d0d6b7c5 JB |
754 | } |
755 | name = name1; | |
6a09a33b | 756 | pset_name (p, name); |
1aa8d505 SM |
757 | pset_sentinel (p, Qinternal_default_process_sentinel); |
758 | pset_filter (p, Qinternal_default_process_filter); | |
23d6bb9c | 759 | XSETPROCESS (val, p); |
d0d6b7c5 JB |
760 | Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist); |
761 | return val; | |
762 | } | |
763 | ||
ff6daed3 | 764 | static void |
d3da34e0 | 765 | remove_process (register Lisp_Object proc) |
d0d6b7c5 JB |
766 | { |
767 | register Lisp_Object pair; | |
768 | ||
769 | pair = Frassq (proc, Vprocess_alist); | |
770 | Vprocess_alist = Fdelq (pair, Vprocess_alist); | |
d0d6b7c5 JB |
771 | |
772 | deactivate_process (proc); | |
773 | } | |
03f04413 | 774 | |
d0d6b7c5 JB |
775 | \f |
776 | DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0, | |
fdb82f93 | 777 | doc: /* Return t if OBJECT is a process. */) |
5842a27b | 778 | (Lisp_Object object) |
d0d6b7c5 | 779 | { |
4ee3e309 | 780 | return PROCESSP (object) ? Qt : Qnil; |
d0d6b7c5 JB |
781 | } |
782 | ||
783 | DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0, | |
fdb82f93 | 784 | doc: /* Return the process named NAME, or nil if there is none. */) |
5842a27b | 785 | (register Lisp_Object name) |
d0d6b7c5 | 786 | { |
bcd69aea | 787 | if (PROCESSP (name)) |
d0d6b7c5 | 788 | return name; |
b7826503 | 789 | CHECK_STRING (name); |
d0d6b7c5 JB |
790 | return Fcdr (Fassoc (name, Vprocess_alist)); |
791 | } | |
792 | ||
ebb9e16f JB |
793 | /* This is how commands for the user decode process arguments. It |
794 | accepts a process, a process name, a buffer, a buffer name, or nil. | |
795 | Buffers denote the first process in the buffer, and nil denotes the | |
796 | current buffer. */ | |
d0d6b7c5 | 797 | |
77b221b1 | 798 | static Lisp_Object |
d3da34e0 | 799 | get_process (register Lisp_Object name) |
d0d6b7c5 | 800 | { |
1619761d KH |
801 | register Lisp_Object proc, obj; |
802 | if (STRINGP (name)) | |
803 | { | |
804 | obj = Fget_process (name); | |
805 | if (NILP (obj)) | |
806 | obj = Fget_buffer (name); | |
807 | if (NILP (obj)) | |
d5db4077 | 808 | error ("Process %s does not exist", SDATA (name)); |
1619761d KH |
809 | } |
810 | else if (NILP (name)) | |
811 | obj = Fcurrent_buffer (); | |
d0d6b7c5 | 812 | else |
1619761d KH |
813 | obj = name; |
814 | ||
50dca5df GM |
815 | /* Now obj should be either a buffer object or a process object. */ |
816 | if (BUFFERP (obj)) | |
d0d6b7c5 | 817 | { |
50dca5df GM |
818 | if (NILP (BVAR (XBUFFER (obj), name))) |
819 | error ("Attempt to get process for a dead buffer"); | |
1619761d | 820 | proc = Fget_buffer_process (obj); |
d0d6b7c5 | 821 | if (NILP (proc)) |
50dca5df | 822 | error ("Buffer %s has no process", SDATA (BVAR (XBUFFER (obj), name))); |
d0d6b7c5 | 823 | } |
d0d6b7c5 | 824 | else |
1619761d | 825 | { |
b7826503 | 826 | CHECK_PROCESS (obj); |
1619761d KH |
827 | proc = obj; |
828 | } | |
829 | return proc; | |
d0d6b7c5 JB |
830 | } |
831 | ||
a5cfdda8 | 832 | |
a5cfdda8 KS |
833 | /* Fdelete_process promises to immediately forget about the process, but in |
834 | reality, Emacs needs to remember those processes until they have been | |
0b3d4a47 | 835 | treated by the SIGCHLD handler and waitpid has been invoked on them; |
bb5f74ee PE |
836 | otherwise they might fill up the kernel's process table. |
837 | ||
94fcd171 PE |
838 | Some processes created by call-process are also put onto this list. |
839 | ||
840 | Members of this list are (process-ID . filename) pairs. The | |
841 | process-ID is a number; the filename, if a string, is a file that | |
842 | needs to be removed after the process exits. */ | |
a5cfdda8 | 843 | static Lisp_Object deleted_pid_list; |
a5cfdda8 | 844 | |
bb5f74ee | 845 | void |
94fcd171 | 846 | record_deleted_pid (pid_t pid, Lisp_Object filename) |
bb5f74ee | 847 | { |
94fcd171 | 848 | deleted_pid_list = Fcons (Fcons (make_fixnum_or_float (pid), filename), |
bb5f74ee PE |
849 | /* GC treated elements set to nil. */ |
850 | Fdelq (Qnil, deleted_pid_list)); | |
851 | ||
bb5f74ee PE |
852 | } |
853 | ||
d0d6b7c5 | 854 | DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0, |
fdb82f93 PJ |
855 | doc: /* Delete PROCESS: kill it and forget about it immediately. |
856 | PROCESS may be a process, a buffer, the name of a process or buffer, or | |
857 | nil, indicating the current buffer's process. */) | |
5842a27b | 858 | (register Lisp_Object process) |
d0d6b7c5 | 859 | { |
ff6daed3 KS |
860 | register struct Lisp_Process *p; |
861 | ||
4ee3e309 | 862 | process = get_process (process); |
ff6daed3 KS |
863 | p = XPROCESS (process); |
864 | ||
6bfd98e7 | 865 | p->raw_status_new = 0; |
d888760c | 866 | if (NETCONN1_P (p) || SERIALCONN1_P (p)) |
d0d6b7c5 | 867 | { |
6c6f1994 | 868 | pset_status (p, list2 (Qexit, make_number (0))); |
60f0fb11 | 869 | p->tick = ++process_tick; |
ff6daed3 | 870 | status_notify (p); |
e42bdf01 | 871 | redisplay_preserve_echo_area (13); |
d0d6b7c5 | 872 | } |
35fb8050 | 873 | else |
d0d6b7c5 | 874 | { |
35fb8050 | 875 | if (p->alive) |
94fcd171 | 876 | record_kill_process (p, Qnil); |
35fb8050 PE |
877 | |
878 | if (p->infd >= 0) | |
a5cfdda8 | 879 | { |
35fb8050 PE |
880 | /* Update P's status, since record_kill_process will make the |
881 | SIGCHLD handler update deleted_pid_list, not *P. */ | |
882 | Lisp_Object symbol; | |
883 | if (p->raw_status_new) | |
884 | update_status (p); | |
885 | symbol = CONSP (p->status) ? XCAR (p->status) : p->status; | |
886 | if (! (EQ (symbol, Qsignal) || EQ (symbol, Qexit))) | |
887 | pset_status (p, list2 (Qsignal, make_number (SIGKILL))); | |
888 | ||
60f0fb11 | 889 | p->tick = ++process_tick; |
a5cfdda8 | 890 | status_notify (p); |
e42bdf01 | 891 | redisplay_preserve_echo_area (13); |
a5cfdda8 | 892 | } |
d0d6b7c5 | 893 | } |
4ee3e309 | 894 | remove_process (process); |
d0d6b7c5 JB |
895 | return Qnil; |
896 | } | |
897 | \f | |
898 | DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0, | |
fdb82f93 PJ |
899 | doc: /* Return the status of PROCESS. |
900 | The returned value is one of the following symbols: | |
901 | run -- for a process that is running. | |
902 | stop -- for a process stopped but continuable. | |
903 | exit -- for a process that has exited. | |
904 | signal -- for a process that has got a fatal signal. | |
905 | open -- for a network stream connection that is open. | |
e690ca94 | 906 | listen -- for a network stream server that is listening. |
fdb82f93 | 907 | closed -- for a network stream connection that is closed. |
e4984112 KS |
908 | connect -- when waiting for a non-blocking connection to complete. |
909 | failed -- when a non-blocking connection has failed. | |
fdb82f93 PJ |
910 | nil -- if arg is a process name and no such process exists. |
911 | PROCESS may be a process, a buffer, the name of a process, or | |
912 | nil, indicating the current buffer's process. */) | |
5842a27b | 913 | (register Lisp_Object process) |
d0d6b7c5 JB |
914 | { |
915 | register struct Lisp_Process *p; | |
916 | register Lisp_Object status; | |
343f4114 | 917 | |
4ee3e309 EN |
918 | if (STRINGP (process)) |
919 | process = Fget_process (process); | |
343f4114 | 920 | else |
4ee3e309 | 921 | process = get_process (process); |
343f4114 | 922 | |
4ee3e309 EN |
923 | if (NILP (process)) |
924 | return process; | |
343f4114 | 925 | |
4ee3e309 | 926 | p = XPROCESS (process); |
6bfd98e7 | 927 | if (p->raw_status_new) |
d0d6b7c5 | 928 | update_status (p); |
4d2b044c | 929 | status = p->status; |
bcd69aea | 930 | if (CONSP (status)) |
70949dac | 931 | status = XCAR (status); |
d888760c | 932 | if (NETCONN1_P (p) || SERIALCONN1_P (p)) |
d0d6b7c5 | 933 | { |
e690ca94 | 934 | if (EQ (status, Qexit)) |
d0d6b7c5 | 935 | status = Qclosed; |
4d2b044c | 936 | else if (EQ (p->command, Qt)) |
e690ca94 KS |
937 | status = Qstop; |
938 | else if (EQ (status, Qrun)) | |
939 | status = Qopen; | |
d0d6b7c5 JB |
940 | } |
941 | return status; | |
942 | } | |
943 | ||
944 | DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status, | |
945 | 1, 1, 0, | |
fdb82f93 PJ |
946 | doc: /* Return the exit status of PROCESS or the signal number that killed it. |
947 | If PROCESS has not yet exited or died, return 0. */) | |
5842a27b | 948 | (register Lisp_Object process) |
d0d6b7c5 | 949 | { |
b7826503 | 950 | CHECK_PROCESS (process); |
6bfd98e7 | 951 | if (XPROCESS (process)->raw_status_new) |
4ee3e309 | 952 | update_status (XPROCESS (process)); |
4d2b044c DA |
953 | if (CONSP (XPROCESS (process)->status)) |
954 | return XCAR (XCDR (XPROCESS (process)->status)); | |
d0d6b7c5 JB |
955 | return make_number (0); |
956 | } | |
957 | ||
958 | DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0, | |
fdb82f93 | 959 | doc: /* Return the process id of PROCESS. |
f3d9532b | 960 | This is the pid of the external process which PROCESS uses or talks to. |
fdb82f93 | 961 | For a network connection, this value is nil. */) |
5842a27b | 962 | (register Lisp_Object process) |
d0d6b7c5 | 963 | { |
d311d28c | 964 | pid_t pid; |
d30fe779 | 965 | |
b7826503 | 966 | CHECK_PROCESS (process); |
d30fe779 EZ |
967 | pid = XPROCESS (process)->pid; |
968 | return (pid ? make_fixnum_or_float (pid) : Qnil); | |
d0d6b7c5 JB |
969 | } |
970 | ||
971 | DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0, | |
fdb82f93 PJ |
972 | doc: /* Return the name of PROCESS, as a string. |
973 | This is the name of the program invoked in PROCESS, | |
974 | possibly modified to make it unique among process names. */) | |
5842a27b | 975 | (register Lisp_Object process) |
d0d6b7c5 | 976 | { |
b7826503 | 977 | CHECK_PROCESS (process); |
4d2b044c | 978 | return XPROCESS (process)->name; |
d0d6b7c5 JB |
979 | } |
980 | ||
981 | DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0, | |
fdb82f93 PJ |
982 | doc: /* Return the command that was executed to start PROCESS. |
983 | This is a list of strings, the first string being the program executed | |
984 | and the rest of the strings being the arguments given to it. | |
d888760c GM |
985 | For a network or serial process, this is nil (process is running) or t |
986 | \(process is stopped). */) | |
5842a27b | 987 | (register Lisp_Object process) |
d0d6b7c5 | 988 | { |
b7826503 | 989 | CHECK_PROCESS (process); |
4d2b044c | 990 | return XPROCESS (process)->command; |
d0d6b7c5 JB |
991 | } |
992 | ||
3b9a3dfa | 993 | DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0, |
fdb82f93 PJ |
994 | doc: /* Return the name of the terminal PROCESS uses, or nil if none. |
995 | This is the terminal that the process itself reads and writes on, | |
996 | not the name of the pty that Emacs uses to talk with that terminal. */) | |
5842a27b | 997 | (register Lisp_Object process) |
3b9a3dfa | 998 | { |
b7826503 | 999 | CHECK_PROCESS (process); |
4d2b044c | 1000 | return XPROCESS (process)->tty_name; |
3b9a3dfa RS |
1001 | } |
1002 | ||
d0d6b7c5 | 1003 | DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer, |
fdb82f93 | 1004 | 2, 2, 0, |
76147d94 DD |
1005 | doc: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). |
1006 | Return BUFFER. */) | |
5842a27b | 1007 | (register Lisp_Object process, Lisp_Object buffer) |
d0d6b7c5 | 1008 | { |
e690ca94 KS |
1009 | struct Lisp_Process *p; |
1010 | ||
b7826503 | 1011 | CHECK_PROCESS (process); |
d0d6b7c5 | 1012 | if (!NILP (buffer)) |
b7826503 | 1013 | CHECK_BUFFER (buffer); |
e690ca94 | 1014 | p = XPROCESS (process); |
6a09a33b | 1015 | pset_buffer (p, buffer); |
d888760c | 1016 | if (NETCONN1_P (p) || SERIALCONN1_P (p)) |
6a09a33b | 1017 | pset_childp (p, Fplist_put (p->childp, QCbuffer, buffer)); |
03f04413 | 1018 | setup_process_coding_systems (process); |
d0d6b7c5 JB |
1019 | return buffer; |
1020 | } | |
1021 | ||
1022 | DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer, | |
fdb82f93 PJ |
1023 | 1, 1, 0, |
1024 | doc: /* Return the buffer PROCESS is associated with. | |
1025 | Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */) | |
5842a27b | 1026 | (register Lisp_Object process) |
d0d6b7c5 | 1027 | { |
b7826503 | 1028 | CHECK_PROCESS (process); |
4d2b044c | 1029 | return XPROCESS (process)->buffer; |
d0d6b7c5 JB |
1030 | } |
1031 | ||
1032 | DEFUN ("process-mark", Fprocess_mark, Sprocess_mark, | |
fdb82f93 PJ |
1033 | 1, 1, 0, |
1034 | doc: /* Return the marker for the end of the last output from PROCESS. */) | |
5842a27b | 1035 | (register Lisp_Object process) |
d0d6b7c5 | 1036 | { |
b7826503 | 1037 | CHECK_PROCESS (process); |
4d2b044c | 1038 | return XPROCESS (process)->mark; |
d0d6b7c5 JB |
1039 | } |
1040 | ||
1041 | DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter, | |
fdb82f93 | 1042 | 2, 2, 0, |
1aa8d505 | 1043 | doc: /* Give PROCESS the filter function FILTER; nil means default. |
7b82c3b6 | 1044 | A value of t means stop accepting output from the process. |
3452df9e | 1045 | |
1aa8d505 | 1046 | When a process has a non-default filter, its buffer is not used for output. |
3452df9e | 1047 | Instead, each time it does output, the entire string of output is |
177c0ea7 | 1048 | passed to the filter. |
3452df9e | 1049 | |
fdb82f93 | 1050 | The filter gets two arguments: the process and the string of output. |
3452df9e KS |
1051 | The string argument is normally a multibyte string, except: |
1052 | - if the process' input coding system is no-conversion or raw-text, | |
1053 | it is a unibyte string (the non-converted input), or else | |
1054 | - if `default-enable-multibyte-characters' is nil, it is a unibyte | |
1055 | string (the result of converting the decoded input multibyte | |
1056 | string to unibyte with `string-make-unibyte'). */) | |
5842a27b | 1057 | (register Lisp_Object process, Lisp_Object filter) |
d0d6b7c5 | 1058 | { |
471f86b9 | 1059 | struct Lisp_Process *p; |
177c0ea7 | 1060 | |
b7826503 | 1061 | CHECK_PROCESS (process); |
471f86b9 GM |
1062 | p = XPROCESS (process); |
1063 | ||
1064 | /* Don't signal an error if the process' input file descriptor | |
1065 | is closed. This could make debugging Lisp more difficult, | |
1066 | for example when doing something like | |
1067 | ||
1068 | (setq process (start-process ...)) | |
1069 | (debug) | |
1070 | (set-process-filter process ...) */ | |
177c0ea7 | 1071 | |
1aa8d505 SM |
1072 | if (NILP (filter)) |
1073 | filter = Qinternal_default_process_filter; | |
1074 | ||
60f0fb11 | 1075 | if (p->infd >= 0) |
a69281ff | 1076 | { |
4d2b044c | 1077 | if (EQ (filter, Qt) && !EQ (p->status, Qlisten)) |
471f86b9 | 1078 | { |
60f0fb11 SM |
1079 | FD_CLR (p->infd, &input_wait_mask); |
1080 | FD_CLR (p->infd, &non_keyboard_wait_mask); | |
471f86b9 | 1081 | } |
4d2b044c | 1082 | else if (EQ (p->filter, Qt) |
d888760c | 1083 | /* Network or serial process not stopped: */ |
4d2b044c | 1084 | && !EQ (p->command, Qt)) |
471f86b9 | 1085 | { |
60f0fb11 SM |
1086 | FD_SET (p->infd, &input_wait_mask); |
1087 | FD_SET (p->infd, &non_keyboard_wait_mask); | |
471f86b9 | 1088 | } |
a69281ff | 1089 | } |
177c0ea7 | 1090 | |
6a09a33b | 1091 | pset_filter (p, filter); |
d888760c | 1092 | if (NETCONN1_P (p) || SERIALCONN1_P (p)) |
6a09a33b | 1093 | pset_childp (p, Fplist_put (p->childp, QCfilter, filter)); |
03f04413 | 1094 | setup_process_coding_systems (process); |
d0d6b7c5 JB |
1095 | return filter; |
1096 | } | |
1097 | ||
1098 | DEFUN ("process-filter", Fprocess_filter, Sprocess_filter, | |
fdb82f93 | 1099 | 1, 1, 0, |
1aa8d505 | 1100 | doc: /* Return the filter function of PROCESS. |
fdb82f93 | 1101 | See `set-process-filter' for more info on filter functions. */) |
5842a27b | 1102 | (register Lisp_Object process) |
d0d6b7c5 | 1103 | { |
b7826503 | 1104 | CHECK_PROCESS (process); |
4d2b044c | 1105 | return XPROCESS (process)->filter; |
d0d6b7c5 JB |
1106 | } |
1107 | ||
1108 | DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel, | |
fdb82f93 | 1109 | 2, 2, 0, |
1aa8d505 | 1110 | doc: /* Give PROCESS the sentinel SENTINEL; nil for default. |
fdb82f93 PJ |
1111 | The sentinel is called as a function when the process changes state. |
1112 | It gets two arguments: the process, and a string describing the change. */) | |
5842a27b | 1113 | (register Lisp_Object process, Lisp_Object sentinel) |
d0d6b7c5 | 1114 | { |
6b61353c KH |
1115 | struct Lisp_Process *p; |
1116 | ||
b7826503 | 1117 | CHECK_PROCESS (process); |
6b61353c KH |
1118 | p = XPROCESS (process); |
1119 | ||
1aa8d505 SM |
1120 | if (NILP (sentinel)) |
1121 | sentinel = Qinternal_default_process_sentinel; | |
1122 | ||
6a09a33b | 1123 | pset_sentinel (p, sentinel); |
d888760c | 1124 | if (NETCONN1_P (p) || SERIALCONN1_P (p)) |
6a09a33b | 1125 | pset_childp (p, Fplist_put (p->childp, QCsentinel, sentinel)); |
d0d6b7c5 JB |
1126 | return sentinel; |
1127 | } | |
1128 | ||
1129 | DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel, | |
fdb82f93 | 1130 | 1, 1, 0, |
1aa8d505 | 1131 | doc: /* Return the sentinel of PROCESS. |
fdb82f93 | 1132 | See `set-process-sentinel' for more info on sentinels. */) |
5842a27b | 1133 | (register Lisp_Object process) |
d0d6b7c5 | 1134 | { |
b7826503 | 1135 | CHECK_PROCESS (process); |
4d2b044c | 1136 | return XPROCESS (process)->sentinel; |
d0d6b7c5 JB |
1137 | } |
1138 | ||
396df322 | 1139 | DEFUN ("set-process-window-size", Fset_process_window_size, |
fdb82f93 PJ |
1140 | Sset_process_window_size, 3, 3, 0, |
1141 | doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */) | |
9c05bccf | 1142 | (Lisp_Object process, Lisp_Object height, Lisp_Object width) |
396df322 | 1143 | { |
b7826503 | 1144 | CHECK_PROCESS (process); |
9c05bccf PE |
1145 | |
1146 | /* All known platforms store window sizes as 'unsigned short'. */ | |
1147 | CHECK_RANGED_INTEGER (height, 0, USHRT_MAX); | |
1148 | CHECK_RANGED_INTEGER (width, 0, USHRT_MAX); | |
177c0ea7 | 1149 | |
60f0fb11 | 1150 | if (XPROCESS (process)->infd < 0 |
9c05bccf PE |
1151 | || (set_window_size (XPROCESS (process)->infd, |
1152 | XINT (height), XINT (width)) | |
1153 | < 0)) | |
396df322 RS |
1154 | return Qnil; |
1155 | else | |
1156 | return Qt; | |
1157 | } | |
1158 | ||
52a1b894 | 1159 | DEFUN ("set-process-inherit-coding-system-flag", |
fdb82f93 PJ |
1160 | Fset_process_inherit_coding_system_flag, |
1161 | Sset_process_inherit_coding_system_flag, 2, 2, 0, | |
1162 | doc: /* Determine whether buffer of PROCESS will inherit coding-system. | |
1163 | If the second argument FLAG is non-nil, then the variable | |
1164 | `buffer-file-coding-system' of the buffer associated with PROCESS | |
1165 | will be bound to the value of the coding system used to decode | |
1166 | the process output. | |
1167 | ||
1168 | This is useful when the coding system specified for the process buffer | |
1169 | leaves either the character code conversion or the end-of-line conversion | |
1170 | unspecified, or if the coding system used to decode the process output | |
1171 | is more appropriate for saving the process buffer. | |
1172 | ||
1173 | Binding the variable `inherit-process-coding-system' to non-nil before | |
1174 | starting the process is an alternative way of setting the inherit flag | |
016a35df GM |
1175 | for the process which will run. |
1176 | ||
1177 | This function returns FLAG. */) | |
5842a27b | 1178 | (register Lisp_Object process, Lisp_Object flag) |
52a1b894 | 1179 | { |
b7826503 | 1180 | CHECK_PROCESS (process); |
60f0fb11 | 1181 | XPROCESS (process)->inherit_coding_system_flag = !NILP (flag); |
52a1b894 EZ |
1182 | return flag; |
1183 | } | |
1184 | ||
e690ca94 KS |
1185 | DEFUN ("set-process-query-on-exit-flag", |
1186 | Fset_process_query_on_exit_flag, Sset_process_query_on_exit_flag, | |
1187 | 2, 2, 0, | |
1188 | doc: /* Specify if query is needed for PROCESS when Emacs is exited. | |
f3d9532b | 1189 | If the second argument FLAG is non-nil, Emacs will query the user before |
016a35df GM |
1190 | exiting or killing a buffer if PROCESS is running. This function |
1191 | returns FLAG. */) | |
5842a27b | 1192 | (register Lisp_Object process, Lisp_Object flag) |
d0d6b7c5 | 1193 | { |
b7826503 | 1194 | CHECK_PROCESS (process); |
60f0fb11 | 1195 | XPROCESS (process)->kill_without_query = NILP (flag); |
e690ca94 | 1196 | return flag; |
d0d6b7c5 | 1197 | } |
312c9964 | 1198 | |
e690ca94 KS |
1199 | DEFUN ("process-query-on-exit-flag", |
1200 | Fprocess_query_on_exit_flag, Sprocess_query_on_exit_flag, | |
fdb82f93 | 1201 | 1, 1, 0, |
f3d9532b | 1202 | doc: /* Return the current value of query-on-exit flag for PROCESS. */) |
5842a27b | 1203 | (register Lisp_Object process) |
de282a05 | 1204 | { |
b7826503 | 1205 | CHECK_PROCESS (process); |
60f0fb11 | 1206 | return (XPROCESS (process)->kill_without_query ? Qnil : Qt); |
e690ca94 KS |
1207 | } |
1208 | ||
e690ca94 KS |
1209 | DEFUN ("process-contact", Fprocess_contact, Sprocess_contact, |
1210 | 1, 2, 0, | |
1211 | doc: /* Return the contact info of PROCESS; t for a real child. | |
d888760c GM |
1212 | For a network or serial connection, the value depends on the optional |
1213 | KEY arg. If KEY is nil, value is a cons cell of the form (HOST | |
1214 | SERVICE) for a network connection or (PORT SPEED) for a serial | |
1215 | connection. If KEY is t, the complete contact information for the | |
1216 | connection is returned, else the specific value for the keyword KEY is | |
1217 | returned. See `make-network-process' or `make-serial-process' for a | |
1218 | list of keywords. */) | |
5842a27b | 1219 | (register Lisp_Object process, Lisp_Object key) |
e690ca94 KS |
1220 | { |
1221 | Lisp_Object contact; | |
1222 | ||
1223 | CHECK_PROCESS (process); | |
4d2b044c | 1224 | contact = XPROCESS (process)->childp; |
e690ca94 KS |
1225 | |
1226 | #ifdef DATAGRAM_SOCKETS | |
1227 | if (DATAGRAM_CONN_P (process) | |
1228 | && (EQ (key, Qt) || EQ (key, QCremote))) | |
177c0ea7 | 1229 | contact = Fplist_put (contact, QCremote, |
e690ca94 KS |
1230 | Fprocess_datagram_address (process)); |
1231 | #endif | |
1232 | ||
d888760c | 1233 | if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt)) |
e690ca94 | 1234 | return contact; |
d888760c | 1235 | if (NILP (key) && NETCONN_P (process)) |
6c6f1994 PE |
1236 | return list2 (Fplist_get (contact, QChost), |
1237 | Fplist_get (contact, QCservice)); | |
d888760c | 1238 | if (NILP (key) && SERIALCONN_P (process)) |
6c6f1994 PE |
1239 | return list2 (Fplist_get (contact, QCport), |
1240 | Fplist_get (contact, QCspeed)); | |
e690ca94 | 1241 | return Fplist_get (contact, key); |
de282a05 RS |
1242 | } |
1243 | ||
faa7db08 KS |
1244 | DEFUN ("process-plist", Fprocess_plist, Sprocess_plist, |
1245 | 1, 1, 0, | |
1246 | doc: /* Return the plist of PROCESS. */) | |
5842a27b | 1247 | (register Lisp_Object process) |
365aee82 | 1248 | { |
ac4a7584 | 1249 | CHECK_PROCESS (process); |
4d2b044c | 1250 | return XPROCESS (process)->plist; |
ac4a7584 | 1251 | } |
365aee82 | 1252 | |
faa7db08 KS |
1253 | DEFUN ("set-process-plist", Fset_process_plist, Sset_process_plist, |
1254 | 2, 2, 0, | |
26a086c6 | 1255 | doc: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */) |
5842a27b | 1256 | (register Lisp_Object process, Lisp_Object plist) |
ac4a7584 | 1257 | { |
365aee82 | 1258 | CHECK_PROCESS (process); |
faa7db08 | 1259 | CHECK_LIST (plist); |
365aee82 | 1260 | |
6a09a33b | 1261 | pset_plist (XPROCESS (process), plist); |
26a086c6 | 1262 | return plist; |
365aee82 KS |
1263 | } |
1264 | ||
312c9964 RS |
1265 | #if 0 /* Turned off because we don't currently record this info |
1266 | in the process. Perhaps add it. */ | |
1267 | DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0, | |
fdb82f93 PJ |
1268 | doc: /* Return the connection type of PROCESS. |
1269 | The value is nil for a pipe, t or `pty' for a pty, or `stream' for | |
1270 | a socket connection. */) | |
5842a27b | 1271 | (Lisp_Object process) |
312c9964 | 1272 | { |
4d2b044c | 1273 | return XPROCESS (process)->type; |
312c9964 RS |
1274 | } |
1275 | #endif | |
2a7bb882 | 1276 | |
d888760c GM |
1277 | DEFUN ("process-type", Fprocess_type, Sprocess_type, 1, 1, 0, |
1278 | doc: /* Return the connection type of PROCESS. | |
1279 | The value is either the symbol `real', `network', or `serial'. | |
1280 | PROCESS may be a process, a buffer, the name of a process or buffer, or | |
1281 | nil, indicating the current buffer's process. */) | |
5842a27b | 1282 | (Lisp_Object process) |
d888760c GM |
1283 | { |
1284 | Lisp_Object proc; | |
1285 | proc = get_process (process); | |
4d2b044c | 1286 | return XPROCESS (proc)->type; |
d888760c | 1287 | } |
991234f0 | 1288 | |
991234f0 | 1289 | DEFUN ("format-network-address", Fformat_network_address, Sformat_network_address, |
0dccee41 | 1290 | 1, 2, 0, |
991234f0 | 1291 | doc: /* Convert network ADDRESS from internal format to a string. |
e1652a86 KS |
1292 | A 4 or 5 element vector represents an IPv4 address (with port number). |
1293 | An 8 or 9 element vector represents an IPv6 address (with port number). | |
0dccee41 | 1294 | If optional second argument OMIT-PORT is non-nil, don't include a port |
e1652a86 KS |
1295 | number in the string, even when present in ADDRESS. |
1296 | Returns nil if format of ADDRESS is invalid. */) | |
5842a27b | 1297 | (Lisp_Object address, Lisp_Object omit_port) |
991234f0 | 1298 | { |
991234f0 KS |
1299 | if (NILP (address)) |
1300 | return Qnil; | |
1301 | ||
1302 | if (STRINGP (address)) /* AF_LOCAL */ | |
1303 | return address; | |
1304 | ||
e1652a86 | 1305 | if (VECTORP (address)) /* AF_INET or AF_INET6 */ |
991234f0 KS |
1306 | { |
1307 | register struct Lisp_Vector *p = XVECTOR (address); | |
d311d28c | 1308 | ptrdiff_t size = p->header.size; |
ded83665 | 1309 | Lisp_Object args[10]; |
0dccee41 | 1310 | int nargs, i; |
991234f0 | 1311 | |
eab3844f | 1312 | if (size == 4 || (size == 5 && !NILP (omit_port))) |
0dccee41 KS |
1313 | { |
1314 | args[0] = build_string ("%d.%d.%d.%d"); | |
1315 | nargs = 4; | |
1316 | } | |
eab3844f | 1317 | else if (size == 5) |
0dccee41 KS |
1318 | { |
1319 | args[0] = build_string ("%d.%d.%d.%d:%d"); | |
1320 | nargs = 5; | |
1321 | } | |
eab3844f | 1322 | else if (size == 8 || (size == 9 && !NILP (omit_port))) |
e1652a86 KS |
1323 | { |
1324 | args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x"); | |
1325 | nargs = 8; | |
1326 | } | |
eab3844f | 1327 | else if (size == 9) |
e1652a86 KS |
1328 | { |
1329 | args[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d"); | |
1330 | nargs = 9; | |
1331 | } | |
0dccee41 | 1332 | else |
991234f0 KS |
1333 | return Qnil; |
1334 | ||
0dccee41 | 1335 | for (i = 0; i < nargs; i++) |
cbf23adb | 1336 | { |
91f2d272 | 1337 | if (! RANGED_INTEGERP (0, p->contents[i], 65535)) |
cbf23adb JB |
1338 | return Qnil; |
1339 | ||
1340 | if (nargs <= 5 /* IPv4 */ | |
1341 | && i < 4 /* host, not port */ | |
91f2d272 | 1342 | && XINT (p->contents[i]) > 255) |
cbf23adb JB |
1343 | return Qnil; |
1344 | ||
91f2d272 | 1345 | args[i+1] = p->contents[i]; |
cbf23adb JB |
1346 | } |
1347 | ||
0dccee41 | 1348 | return Fformat (nargs+1, args); |
991234f0 KS |
1349 | } |
1350 | ||
1351 | if (CONSP (address)) | |
1352 | { | |
1353 | Lisp_Object args[2]; | |
1354 | args[0] = build_string ("<Family %d>"); | |
8d2ff840 | 1355 | args[1] = Fcar (address); |
991234f0 | 1356 | return Fformat (2, args); |
991234f0 KS |
1357 | } |
1358 | ||
1359 | return Qnil; | |
1360 | } | |
d0d6b7c5 JB |
1361 | |
1362 | DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0, | |
fec92060 | 1363 | doc: /* Return a list of all processes that are Emacs sub-processes. */) |
5842a27b | 1364 | (void) |
d0d6b7c5 JB |
1365 | { |
1366 | return Fmapcar (Qcdr, Vprocess_alist); | |
1367 | } | |
1368 | \f | |
b0310da4 JB |
1369 | /* Starting asynchronous inferior processes. */ |
1370 | ||
27e498e6 | 1371 | static void start_process_unwind (Lisp_Object proc); |
b0310da4 | 1372 | |
d0d6b7c5 | 1373 | DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0, |
fdb82f93 PJ |
1374 | doc: /* Start a program in a subprocess. Return the process object for it. |
1375 | NAME is name for process. It is modified if necessary to make it unique. | |
553acde5 | 1376 | BUFFER is the buffer (or buffer name) to associate with the process. |
4b6c5eee EZ |
1377 | |
1378 | Process output (both standard output and standard error streams) goes | |
1379 | at end of BUFFER, unless you specify an output stream or filter | |
1380 | function to handle the output. BUFFER may also be nil, meaning that | |
16fdeb17 RS |
1381 | this process is not associated with any buffer. |
1382 | ||
6b8bc570 CY |
1383 | PROGRAM is the program file name. It is searched for in `exec-path' |
1384 | (which see). If nil, just associate a pty with the buffer. Remaining | |
1385 | arguments are strings to give program as arguments. | |
320aebc9 | 1386 | |
4b6c5eee EZ |
1387 | If you want to separate standard output from standard error, invoke |
1388 | the command through a shell and redirect one of them using the shell | |
1389 | syntax. | |
320aebc9 | 1390 | |
3ecdf100 | 1391 | usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */) |
f66c7cf8 | 1392 | (ptrdiff_t nargs, Lisp_Object *args) |
d0d6b7c5 | 1393 | { |
1e30af70 | 1394 | Lisp_Object buffer, name, program, proc, current_dir, tem; |
d0d6b7c5 | 1395 | register unsigned char **new_argv; |
f66c7cf8 | 1396 | ptrdiff_t i; |
d311d28c | 1397 | ptrdiff_t count = SPECPDL_INDEX (); |
d0d6b7c5 JB |
1398 | |
1399 | buffer = args[1]; | |
1400 | if (!NILP (buffer)) | |
1401 | buffer = Fget_buffer_create (buffer); | |
1402 | ||
1e30af70 JB |
1403 | /* Make sure that the child will be able to chdir to the current |
1404 | buffer's current directory, or its unhandled equivalent. We | |
1405 | can't just have the child check for an error when it does the | |
1406 | chdir, since it's in a vfork. | |
1407 | ||
1408 | We have to GCPRO around this because Fexpand_file_name and | |
1409 | Funhandled_file_name_directory might call a file name handling | |
1410 | function. The argument list is protected by the caller, so all | |
1411 | we really have to worry about is buffer. */ | |
1412 | { | |
c365c355 PE |
1413 | struct gcpro gcpro1; |
1414 | GCPRO1 (buffer); | |
1415 | current_dir = encode_current_directory (); | |
1e30af70 JB |
1416 | UNGCPRO; |
1417 | } | |
1418 | ||
d0d6b7c5 | 1419 | name = args[0]; |
b7826503 | 1420 | CHECK_STRING (name); |
d0d6b7c5 JB |
1421 | |
1422 | program = args[2]; | |
1423 | ||
850d0752 NR |
1424 | if (!NILP (program)) |
1425 | CHECK_STRING (program); | |
d0d6b7c5 | 1426 | |
d0d6b7c5 | 1427 | proc = make_process (name); |
b0310da4 JB |
1428 | /* If an error occurs and we can't start the process, we want to |
1429 | remove it from the process list. This means that each error | |
1430 | check in create_process doesn't need to call remove_process | |
1431 | itself; it's all taken care of here. */ | |
1432 | record_unwind_protect (start_process_unwind, proc); | |
d0d6b7c5 | 1433 | |
6a09a33b PE |
1434 | pset_childp (XPROCESS (proc), Qt); |
1435 | pset_plist (XPROCESS (proc), Qnil); | |
1436 | pset_type (XPROCESS (proc), Qreal); | |
1437 | pset_buffer (XPROCESS (proc), buffer); | |
1aa8d505 SM |
1438 | pset_sentinel (XPROCESS (proc), Qinternal_default_process_sentinel); |
1439 | pset_filter (XPROCESS (proc), Qinternal_default_process_filter); | |
6a09a33b | 1440 | pset_command (XPROCESS (proc), Flist (nargs - 2, args + 2)); |
d0d6b7c5 | 1441 | |
8af55556 TZ |
1442 | #ifdef HAVE_GNUTLS |
1443 | /* AKA GNUTLS_INITSTAGE(proc). */ | |
1444 | XPROCESS (proc)->gnutls_initstage = GNUTLS_STAGE_EMPTY; | |
6a09a33b | 1445 | pset_gnutls_cred_type (XPROCESS (proc), Qnil); |
8af55556 TZ |
1446 | #endif |
1447 | ||
6b61353c | 1448 | #ifdef ADAPTIVE_READ_BUFFERING |
60f0fb11 SM |
1449 | XPROCESS (proc)->adaptive_read_buffering |
1450 | = (NILP (Vprocess_adaptive_read_buffering) ? 0 | |
1451 | : EQ (Vprocess_adaptive_read_buffering, Qt) ? 1 : 2); | |
6b61353c KH |
1452 | #endif |
1453 | ||
7af71e17 | 1454 | /* Make the process marker point into the process buffer (if any). */ |
f523e0c3 | 1455 | if (BUFFERP (buffer)) |
4d2b044c | 1456 | set_marker_both (XPROCESS (proc)->mark, buffer, |
d8a2934e RS |
1457 | BUF_ZV (XBUFFER (buffer)), |
1458 | BUF_ZV_BYTE (XBUFFER (buffer))); | |
7af71e17 | 1459 | |
67918941 | 1460 | { |
d5d4ae71 KH |
1461 | /* Decide coding systems for communicating with the process. Here |
1462 | we don't setup the structure coding_system nor pay attention to | |
1463 | unibyte mode. They are done in create_process. */ | |
1464 | ||
67918941 RS |
1465 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
1466 | Lisp_Object coding_systems = Qt; | |
1467 | Lisp_Object val, *args2; | |
a4a37e65 | 1468 | struct gcpro gcpro1, gcpro2; |
67918941 | 1469 | |
d5d4ae71 KH |
1470 | val = Vcoding_system_for_read; |
1471 | if (NILP (val)) | |
67918941 | 1472 | { |
38182d90 | 1473 | args2 = alloca ((nargs + 1) * sizeof *args2); |
67918941 RS |
1474 | args2[0] = Qstart_process; |
1475 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
a4a37e65 | 1476 | GCPRO2 (proc, current_dir); |
850d0752 NR |
1477 | if (!NILP (program)) |
1478 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); | |
67918941 RS |
1479 | UNGCPRO; |
1480 | if (CONSP (coding_systems)) | |
70949dac | 1481 | val = XCAR (coding_systems); |
67918941 | 1482 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 1483 | val = XCAR (Vdefault_process_coding_system); |
67918941 | 1484 | } |
6a09a33b | 1485 | pset_decode_coding_system (XPROCESS (proc), val); |
67918941 | 1486 | |
d5d4ae71 KH |
1487 | val = Vcoding_system_for_write; |
1488 | if (NILP (val)) | |
67918941 RS |
1489 | { |
1490 | if (EQ (coding_systems, Qt)) | |
1491 | { | |
38182d90 | 1492 | args2 = alloca ((nargs + 1) * sizeof *args2); |
67918941 RS |
1493 | args2[0] = Qstart_process; |
1494 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
a4a37e65 | 1495 | GCPRO2 (proc, current_dir); |
850d0752 NR |
1496 | if (!NILP (program)) |
1497 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); | |
67918941 RS |
1498 | UNGCPRO; |
1499 | } | |
1500 | if (CONSP (coding_systems)) | |
70949dac | 1501 | val = XCDR (coding_systems); |
67918941 | 1502 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 1503 | val = XCDR (Vdefault_process_coding_system); |
67918941 | 1504 | } |
6a09a33b | 1505 | pset_encode_coding_system (XPROCESS (proc), val); |
e1dbe924 | 1506 | /* Note: At this moment, the above coding system may leave |
fcaf8878 KH |
1507 | text-conversion or eol-conversion unspecified. They will be |
1508 | decided after we read output from the process and decode it by | |
1509 | some coding system, or just before we actually send a text to | |
1510 | the process. */ | |
67918941 | 1511 | } |
0fa1789e | 1512 | |
a4a37e65 | 1513 | |
6a09a33b | 1514 | pset_decoding_buf (XPROCESS (proc), empty_unibyte_string); |
850d0752 | 1515 | XPROCESS (proc)->decoding_carryover = 0; |
6a09a33b | 1516 | pset_encoding_buf (XPROCESS (proc), empty_unibyte_string); |
ed30cf85 | 1517 | |
850d0752 NR |
1518 | XPROCESS (proc)->inherit_coding_system_flag |
1519 | = !(NILP (buffer) || !inherit_process_coding_system); | |
77bf07e1 | 1520 | |
850d0752 NR |
1521 | if (!NILP (program)) |
1522 | { | |
1523 | /* If program file name is not absolute, search our path for it. | |
1524 | Put the name we will really use in TEM. */ | |
1525 | if (!IS_DIRECTORY_SEP (SREF (program, 0)) | |
1526 | && !(SCHARS (program) > 1 | |
1527 | && IS_DEVICE_SEP (SREF (program, 1)))) | |
1528 | { | |
1529 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
1530 | ||
1531 | tem = Qnil; | |
1532 | GCPRO4 (name, program, buffer, current_dir); | |
518c40a2 PE |
1533 | openp (Vexec_path, program, Vexec_suffixes, &tem, |
1534 | make_number (X_OK), false); | |
850d0752 NR |
1535 | UNGCPRO; |
1536 | if (NILP (tem)) | |
a9757f6a | 1537 | report_file_error ("Searching for program", program); |
850d0752 NR |
1538 | tem = Fexpand_file_name (tem, Qnil); |
1539 | } | |
1540 | else | |
1541 | { | |
1542 | if (!NILP (Ffile_directory_p (program))) | |
1543 | error ("Specified program for new process is a directory"); | |
1544 | tem = program; | |
1545 | } | |
ed30cf85 | 1546 | |
850d0752 NR |
1547 | /* If program file name starts with /: for quoting a magic name, |
1548 | discard that. */ | |
1549 | if (SBYTES (tem) > 2 && SREF (tem, 0) == '/' | |
1550 | && SREF (tem, 1) == ':') | |
1551 | tem = Fsubstring (tem, make_number (2), Qnil); | |
77bf07e1 | 1552 | |
77bf07e1 | 1553 | { |
fcaf8878 | 1554 | Lisp_Object arg_encoding = Qnil; |
850d0752 NR |
1555 | struct gcpro gcpro1; |
1556 | GCPRO1 (tem); | |
a4a37e65 | 1557 | |
850d0752 NR |
1558 | /* Encode the file name and put it in NEW_ARGV. |
1559 | That's where the child will use it to execute the program. */ | |
6c6f1994 | 1560 | tem = list1 (ENCODE_FILE (tem)); |
77bf07e1 | 1561 | |
850d0752 NR |
1562 | /* Here we encode arguments by the coding system used for sending |
1563 | data to the process. We don't support using different coding | |
1564 | systems for encoding arguments and for encoding data sent to the | |
1565 | process. */ | |
77bf07e1 | 1566 | |
850d0752 NR |
1567 | for (i = 3; i < nargs; i++) |
1568 | { | |
1569 | tem = Fcons (args[i], tem); | |
1570 | CHECK_STRING (XCAR (tem)); | |
1571 | if (STRING_MULTIBYTE (XCAR (tem))) | |
fcaf8878 KH |
1572 | { |
1573 | if (NILP (arg_encoding)) | |
1574 | arg_encoding = (complement_process_encoding_system | |
4d2b044c | 1575 | (XPROCESS (proc)->encode_coding_system)); |
fcaf8878 KH |
1576 | XSETCAR (tem, |
1577 | code_convert_string_norecord | |
1578 | (XCAR (tem), arg_encoding, 1)); | |
1579 | } | |
850d0752 | 1580 | } |
a4a37e65 | 1581 | |
850d0752 NR |
1582 | UNGCPRO; |
1583 | } | |
0fa1789e | 1584 | |
850d0752 NR |
1585 | /* Now that everything is encoded we can collect the strings into |
1586 | NEW_ARGV. */ | |
38182d90 | 1587 | new_argv = alloca ((nargs - 1) * sizeof *new_argv); |
850d0752 | 1588 | new_argv[nargs - 2] = 0; |
52a1b894 | 1589 | |
c5101a77 | 1590 | for (i = nargs - 2; i-- != 0; ) |
850d0752 NR |
1591 | { |
1592 | new_argv[i] = SDATA (XCAR (tem)); | |
1593 | tem = XCDR (tem); | |
1594 | } | |
1595 | ||
1596 | create_process (proc, (char **) new_argv, current_dir); | |
1597 | } | |
1598 | else | |
1599 | create_pty (proc); | |
d0d6b7c5 | 1600 | |
b0310da4 | 1601 | return unbind_to (count, proc); |
d0d6b7c5 JB |
1602 | } |
1603 | ||
b0310da4 | 1604 | /* This function is the unwind_protect form for Fstart_process. If |
8e6208c5 | 1605 | PROC doesn't have its pid set, then we know someone has signaled |
b0310da4 JB |
1606 | an error and the process wasn't started successfully, so we should |
1607 | remove it from the process list. */ | |
27e498e6 | 1608 | static void |
d3da34e0 | 1609 | start_process_unwind (Lisp_Object proc) |
b0310da4 | 1610 | { |
bcd69aea | 1611 | if (!PROCESSP (proc)) |
1088b922 | 1612 | emacs_abort (); |
b0310da4 | 1613 | |
2bf26180 GM |
1614 | /* Was PROC started successfully? |
1615 | -2 is used for a pty with no process, eg for gdb. */ | |
1616 | if (XPROCESS (proc)->pid <= 0 && XPROCESS (proc)->pid != -2) | |
b0310da4 | 1617 | remove_process (proc); |
b0310da4 JB |
1618 | } |
1619 | ||
94fcd171 PE |
1620 | /* If *FD_ADDR is nonnegative, close it, and mark it as closed. */ |
1621 | ||
1622 | static void | |
1623 | close_process_fd (int *fd_addr) | |
1624 | { | |
1625 | int fd = *fd_addr; | |
1626 | if (0 <= fd) | |
1627 | { | |
1628 | *fd_addr = -1; | |
1629 | emacs_close (fd); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | /* Indexes of file descriptors in open_fds. */ | |
1634 | enum | |
1635 | { | |
1636 | /* The pipe from Emacs to its subprocess. */ | |
1637 | SUBPROCESS_STDIN, | |
1638 | WRITE_TO_SUBPROCESS, | |
1639 | ||
1640 | /* The main pipe from the subprocess to Emacs. */ | |
1641 | READ_FROM_SUBPROCESS, | |
1642 | SUBPROCESS_STDOUT, | |
1643 | ||
1644 | /* The pipe from the subprocess to Emacs that is closed when the | |
1645 | subprocess execs. */ | |
1646 | READ_FROM_EXEC_MONITOR, | |
1647 | EXEC_MONITOR_OUTPUT | |
1648 | }; | |
1649 | ||
1650 | verify (PROCESS_OPEN_FDS == EXEC_MONITOR_OUTPUT + 1); | |
30904ab7 | 1651 | |
40ccffa6 | 1652 | static void |
cbd6509c | 1653 | create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir) |
d0d6b7c5 | 1654 | { |
94fcd171 | 1655 | struct Lisp_Process *p = XPROCESS (process); |
29ed4d51 CY |
1656 | int inchannel, outchannel; |
1657 | pid_t pid; | |
a773ed9a | 1658 | int vfork_errno; |
cbd6509c PE |
1659 | int forkin, forkout; |
1660 | bool pty_flag = 0; | |
6496aec9 | 1661 | char pty_name[PTY_NAME_SIZE]; |
cbd6509c | 1662 | Lisp_Object lisp_pty_name = Qnil; |
d0d6b7c5 | 1663 | |
d0d6b7c5 JB |
1664 | inchannel = outchannel = -1; |
1665 | ||
fe45da4e | 1666 | if (!NILP (Vprocess_connection_type)) |
6496aec9 | 1667 | outchannel = inchannel = allocate_pty (pty_name); |
d0d6b7c5 | 1668 | |
d0d6b7c5 JB |
1669 | if (inchannel >= 0) |
1670 | { | |
94fcd171 | 1671 | p->open_fd[READ_FROM_SUBPROCESS] = inchannel; |
2af70a0c RS |
1672 | #if ! defined (USG) || defined (USG_SUBTTY_WORKS) |
1673 | /* On most USG systems it does not work to open the pty's tty here, | |
1674 | then close it and reopen it in the child. */ | |
d0d6b7c5 JB |
1675 | /* Don't let this terminal become our controlling terminal |
1676 | (in case we don't have one). */ | |
68c45bf0 | 1677 | forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); |
d0d6b7c5 JB |
1678 | if (forkin < 0) |
1679 | report_file_error ("Opening pty", Qnil); | |
94fcd171 | 1680 | p->open_fd[SUBPROCESS_STDIN] = forkin; |
d0d6b7c5 JB |
1681 | #else |
1682 | forkin = forkout = -1; | |
2af70a0c | 1683 | #endif /* not USG, or USG_SUBTTY_WORKS */ |
d0d6b7c5 | 1684 | pty_flag = 1; |
4d7e6e51 | 1685 | lisp_pty_name = build_string (pty_name); |
d0d6b7c5 JB |
1686 | } |
1687 | else | |
d0d6b7c5 | 1688 | { |
94fcd171 PE |
1689 | if (emacs_pipe (p->open_fd + SUBPROCESS_STDIN) != 0 |
1690 | || emacs_pipe (p->open_fd + READ_FROM_SUBPROCESS) != 0) | |
fc14013c | 1691 | report_file_error ("Creating pipe", Qnil); |
94fcd171 PE |
1692 | forkin = p->open_fd[SUBPROCESS_STDIN]; |
1693 | outchannel = p->open_fd[WRITE_TO_SUBPROCESS]; | |
1694 | inchannel = p->open_fd[READ_FROM_SUBPROCESS]; | |
1695 | forkout = p->open_fd[SUBPROCESS_STDOUT]; | |
d0d6b7c5 | 1696 | } |
d0d6b7c5 | 1697 | |
49cdacda | 1698 | #ifndef WINDOWSNT |
94fcd171 | 1699 | if (emacs_pipe (p->open_fd + READ_FROM_EXEC_MONITOR) != 0) |
067428c1 | 1700 | report_file_error ("Creating pipe", Qnil); |
2c4df143 YM |
1701 | #endif |
1702 | ||
d0d6b7c5 | 1703 | fcntl (inchannel, F_SETFL, O_NONBLOCK); |
03832893 | 1704 | fcntl (outchannel, F_SETFL, O_NONBLOCK); |
d0d6b7c5 | 1705 | |
067428c1 | 1706 | /* Record this as an active process, with its channels. */ |
d0d6b7c5 | 1707 | chan_process[inchannel] = process; |
94fcd171 PE |
1708 | p->infd = inchannel; |
1709 | p->outfd = outchannel; | |
16782258 JD |
1710 | |
1711 | /* Previously we recorded the tty descriptor used in the subprocess. | |
1712 | It was only used for getting the foreground tty process, so now | |
1713 | we just reopen the device (see emacs_get_tty_pgrp) as this is | |
1714 | more portable (see USG_SUBTTY_WORKS above). */ | |
1715 | ||
94fcd171 PE |
1716 | p->pty_flag = pty_flag; |
1717 | pset_status (p, Qrun); | |
d0d6b7c5 | 1718 | |
3081bf8d | 1719 | FD_SET (inchannel, &input_wait_mask); |
a69281ff | 1720 | FD_SET (inchannel, &non_keyboard_wait_mask); |
3081bf8d KH |
1721 | if (inchannel > max_process_desc) |
1722 | max_process_desc = inchannel; | |
1723 | ||
0b3d4a47 | 1724 | /* This may signal an error. */ |
5cf2b69b KH |
1725 | setup_process_coding_systems (process); |
1726 | ||
4d7e6e51 | 1727 | block_input (); |
fa55d2aa | 1728 | block_child_signal (); |
4d7e6e51 | 1729 | |
e98d950b | 1730 | #ifndef WINDOWSNT |
cbd6509c PE |
1731 | /* vfork, and prevent local vars from being clobbered by the vfork. */ |
1732 | { | |
c365c355 | 1733 | Lisp_Object volatile current_dir_volatile = current_dir; |
cbd6509c | 1734 | Lisp_Object volatile lisp_pty_name_volatile = lisp_pty_name; |
cbd6509c PE |
1735 | char **volatile new_argv_volatile = new_argv; |
1736 | int volatile forkin_volatile = forkin; | |
1737 | int volatile forkout_volatile = forkout; | |
94fcd171 | 1738 | struct Lisp_Process *p_volatile = p; |
cbd6509c PE |
1739 | |
1740 | pid = vfork (); | |
1741 | ||
c365c355 | 1742 | current_dir = current_dir_volatile; |
cbd6509c | 1743 | lisp_pty_name = lisp_pty_name_volatile; |
cbd6509c PE |
1744 | new_argv = new_argv_volatile; |
1745 | forkin = forkin_volatile; | |
1746 | forkout = forkout_volatile; | |
94fcd171 | 1747 | p = p_volatile; |
6496aec9 | 1748 | |
94fcd171 | 1749 | pty_flag = p->pty_flag; |
cbd6509c PE |
1750 | } |
1751 | ||
4d7e6e51 | 1752 | if (pid == 0) |
e98d950b | 1753 | #endif /* not WINDOWSNT */ |
4d7e6e51 PE |
1754 | { |
1755 | int xforkin = forkin; | |
1756 | int xforkout = forkout; | |
d0d6b7c5 | 1757 | |
4d7e6e51 | 1758 | /* Make the pty be the controlling terminal of the process. */ |
d0d6b7c5 | 1759 | #ifdef HAVE_PTYS |
4d7e6e51 | 1760 | /* First, disconnect its current controlling terminal. */ |
4d7e6e51 PE |
1761 | /* We tried doing setsid only if pty_flag, but it caused |
1762 | process_set_signal to fail on SGI when using a pipe. */ | |
1763 | setsid (); | |
1764 | /* Make the pty's terminal the controlling terminal. */ | |
1765 | if (pty_flag && xforkin >= 0) | |
1766 | { | |
39e9ebcd | 1767 | #ifdef TIOCSCTTY |
4d7e6e51 PE |
1768 | /* We ignore the return value |
1769 | because faith@cs.unc.edu says that is necessary on Linux. */ | |
1770 | ioctl (xforkin, TIOCSCTTY, 0); | |
ce4c9c90 | 1771 | #endif |
4d7e6e51 | 1772 | } |
a7ebc409 | 1773 | #if defined (LDISC1) |
4d7e6e51 PE |
1774 | if (pty_flag && xforkin >= 0) |
1775 | { | |
1776 | struct termios t; | |
1777 | tcgetattr (xforkin, &t); | |
1778 | t.c_lflag = LDISC1; | |
1779 | if (tcsetattr (xforkin, TCSANOW, &t) < 0) | |
4ebbdd67 | 1780 | emacs_perror ("create_process/tcsetattr LDISC1"); |
4d7e6e51 | 1781 | } |
9bcf8ec6 | 1782 | #else |
aafadd9f | 1783 | #if defined (NTTYDISC) && defined (TIOCSETD) |
4d7e6e51 PE |
1784 | if (pty_flag && xforkin >= 0) |
1785 | { | |
1786 | /* Use new line discipline. */ | |
1787 | int ldisc = NTTYDISC; | |
1788 | ioctl (xforkin, TIOCSETD, &ldisc); | |
1789 | } | |
000ab717 | 1790 | #endif |
9bcf8ec6 | 1791 | #endif |
177c0ea7 | 1792 | #ifdef TIOCNOTTY |
4d7e6e51 PE |
1793 | /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you |
1794 | can do TIOCSPGRP only to the process's controlling tty. */ | |
1795 | if (pty_flag) | |
1796 | { | |
1797 | /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? | |
1798 | I can't test it since I don't have 4.3. */ | |
1799 | int j = emacs_open ("/dev/tty", O_RDWR, 0); | |
1800 | if (j >= 0) | |
1801 | { | |
1802 | ioctl (j, TIOCNOTTY, 0); | |
1803 | emacs_close (j); | |
1804 | } | |
4d7e6e51 | 1805 | } |
d0d6b7c5 JB |
1806 | #endif /* TIOCNOTTY */ |
1807 | ||
e39a993c | 1808 | #if !defined (DONT_REOPEN_PTY) |
d0d6b7c5 | 1809 | /*** There is a suggestion that this ought to be a |
dd0333b6 | 1810 | conditional on TIOCSPGRP, or !defined TIOCSCTTY. |
99153b9e RS |
1811 | Trying the latter gave the wrong results on Debian GNU/Linux 1.1; |
1812 | that system does seem to need this code, even though | |
dd0333b6 | 1813 | both TIOCSCTTY is defined. */ |
d0d6b7c5 JB |
1814 | /* Now close the pty (if we had it open) and reopen it. |
1815 | This makes the pty the controlling terminal of the subprocess. */ | |
4d7e6e51 PE |
1816 | if (pty_flag) |
1817 | { | |
99e3d726 | 1818 | |
6496aec9 | 1819 | /* I wonder if emacs_close (emacs_open (SSDATA (lisp_pty_name), ...)) |
4d7e6e51 PE |
1820 | would work? */ |
1821 | if (xforkin >= 0) | |
1822 | emacs_close (xforkin); | |
6496aec9 | 1823 | xforkout = xforkin = emacs_open (SSDATA (lisp_pty_name), O_RDWR, 0); |
d0d6b7c5 | 1824 | |
4d7e6e51 PE |
1825 | if (xforkin < 0) |
1826 | { | |
6496aec9 | 1827 | emacs_perror (SSDATA (lisp_pty_name)); |
4ebbdd67 | 1828 | _exit (EXIT_CANCELED); |
4d7e6e51 | 1829 | } |
4aa54ba8 | 1830 | |
4d7e6e51 | 1831 | } |
e39a993c | 1832 | #endif /* not DONT_REOPEN_PTY */ |
e9bf058b | 1833 | |
d0d6b7c5 | 1834 | #ifdef SETUP_SLAVE_PTY |
4d7e6e51 PE |
1835 | if (pty_flag) |
1836 | { | |
1837 | SETUP_SLAVE_PTY; | |
1838 | } | |
d0d6b7c5 | 1839 | #endif /* SETUP_SLAVE_PTY */ |
d0d6b7c5 JB |
1840 | #endif /* HAVE_PTYS */ |
1841 | ||
4d7e6e51 PE |
1842 | signal (SIGINT, SIG_DFL); |
1843 | signal (SIGQUIT, SIG_DFL); | |
1844 | ||
1845 | /* Emacs ignores SIGPIPE, but the child should not. */ | |
1846 | signal (SIGPIPE, SIG_DFL); | |
0dc70c33 | 1847 | |
fa55d2aa PE |
1848 | /* Stop blocking SIGCHLD in the child. */ |
1849 | unblock_child_signal (); | |
5e7e1da2 | 1850 | |
4d7e6e51 PE |
1851 | if (pty_flag) |
1852 | child_setup_tty (xforkout); | |
e98d950b | 1853 | #ifdef WINDOWSNT |
c365c355 | 1854 | pid = child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir); |
177c0ea7 | 1855 | #else /* not WINDOWSNT */ |
c365c355 | 1856 | child_setup (xforkin, xforkout, xforkout, new_argv, 1, current_dir); |
e98d950b | 1857 | #endif /* not WINDOWSNT */ |
4d7e6e51 | 1858 | } |
d0d6b7c5 | 1859 | |
4d7e6e51 PE |
1860 | /* Back in the parent process. */ |
1861 | ||
a773ed9a | 1862 | vfork_errno = errno; |
94fcd171 | 1863 | p->pid = pid; |
908589fd | 1864 | if (pid >= 0) |
94fcd171 | 1865 | p->alive = 1; |
d0d6b7c5 | 1866 | |
fa55d2aa PE |
1867 | /* Stop blocking in the parent. */ |
1868 | unblock_child_signal (); | |
4d7e6e51 | 1869 | unblock_input (); |
ececcbec | 1870 | |
d0d6b7c5 | 1871 | if (pid < 0) |
ee010797 | 1872 | report_file_errno ("Doing vfork", Qnil, vfork_errno); |
4a127b3b KH |
1873 | else |
1874 | { | |
1875 | /* vfork succeeded. */ | |
d0d6b7c5 | 1876 | |
94fcd171 PE |
1877 | /* Close the pipe ends that the child uses, or the child's pty. */ |
1878 | close_process_fd (&p->open_fd[SUBPROCESS_STDIN]); | |
1879 | close_process_fd (&p->open_fd[SUBPROCESS_STDOUT]); | |
1880 | ||
e98d950b | 1881 | #ifdef WINDOWSNT |
4a127b3b | 1882 | register_child (pid, inchannel); |
e98d950b RS |
1883 | #endif /* WINDOWSNT */ |
1884 | ||
94fcd171 | 1885 | pset_tty_name (p, lisp_pty_name); |
2c4df143 | 1886 | |
49cdacda | 1887 | #ifndef WINDOWSNT |
2c4df143 | 1888 | /* Wait for child_setup to complete in case that vfork is |
94fcd171 | 1889 | actually defined as fork. The descriptor |
526e5233 | 1890 | XPROCESS (proc)->open_fd[EXEC_MONITOR_OUTPUT] |
2c4df143 | 1891 | of a pipe is closed at the child side either by close-on-exec |
21e54a94 | 1892 | on successful execve or the _exit call in child_setup. */ |
2c4df143 YM |
1893 | { |
1894 | char dummy; | |
1895 | ||
94fcd171 PE |
1896 | close_process_fd (&p->open_fd[EXEC_MONITOR_OUTPUT]); |
1897 | emacs_read (p->open_fd[READ_FROM_EXEC_MONITOR], &dummy, 1); | |
1898 | close_process_fd (&p->open_fd[READ_FROM_EXEC_MONITOR]); | |
2c4df143 YM |
1899 | } |
1900 | #endif | |
4a127b3b | 1901 | } |
d0d6b7c5 | 1902 | } |
d0d6b7c5 | 1903 | |
6496aec9 | 1904 | static void |
d3da34e0 | 1905 | create_pty (Lisp_Object process) |
850d0752 | 1906 | { |
94fcd171 | 1907 | struct Lisp_Process *p = XPROCESS (process); |
6496aec9 | 1908 | char pty_name[PTY_NAME_SIZE]; |
94fcd171 | 1909 | int pty_fd = NILP (Vprocess_connection_type) ? -1 : allocate_pty (pty_name); |
850d0752 | 1910 | |
94fcd171 | 1911 | if (pty_fd >= 0) |
850d0752 | 1912 | { |
94fcd171 | 1913 | p->open_fd[SUBPROCESS_STDIN] = pty_fd; |
850d0752 NR |
1914 | #if ! defined (USG) || defined (USG_SUBTTY_WORKS) |
1915 | /* On most USG systems it does not work to open the pty's tty here, | |
1916 | then close it and reopen it in the child. */ | |
850d0752 NR |
1917 | /* Don't let this terminal become our controlling terminal |
1918 | (in case we don't have one). */ | |
afd4052b | 1919 | int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0); |
aba7731a | 1920 | if (forkout < 0) |
850d0752 | 1921 | report_file_error ("Opening pty", Qnil); |
94fcd171 | 1922 | p->open_fd[WRITE_TO_SUBPROCESS] = forkout; |
019d2c4c | 1923 | #if defined (DONT_REOPEN_PTY) |
850d0752 NR |
1924 | /* In the case that vfork is defined as fork, the parent process |
1925 | (Emacs) may send some data before the child process completes | |
1926 | tty options setup. So we setup tty before forking. */ | |
1927 | child_setup_tty (forkout); | |
019d2c4c | 1928 | #endif /* DONT_REOPEN_PTY */ |
850d0752 | 1929 | #endif /* not USG, or USG_SUBTTY_WORKS */ |
850d0752 | 1930 | |
94fcd171 | 1931 | fcntl (pty_fd, F_SETFL, O_NONBLOCK); |
850d0752 | 1932 | |
6496aec9 PE |
1933 | /* Record this as an active process, with its channels. |
1934 | As a result, child_setup will close Emacs's side of the pipes. */ | |
94fcd171 PE |
1935 | chan_process[pty_fd] = process; |
1936 | p->infd = pty_fd; | |
1937 | p->outfd = pty_fd; | |
850d0752 | 1938 | |
6496aec9 PE |
1939 | /* Previously we recorded the tty descriptor used in the subprocess. |
1940 | It was only used for getting the foreground tty process, so now | |
1941 | we just reopen the device (see emacs_get_tty_pgrp) as this is | |
1942 | more portable (see USG_SUBTTY_WORKS above). */ | |
850d0752 | 1943 | |
94fcd171 PE |
1944 | p->pty_flag = 1; |
1945 | pset_status (p, Qrun); | |
6496aec9 | 1946 | setup_process_coding_systems (process); |
850d0752 | 1947 | |
94fcd171 PE |
1948 | FD_SET (pty_fd, &input_wait_mask); |
1949 | FD_SET (pty_fd, &non_keyboard_wait_mask); | |
1950 | if (pty_fd > max_process_desc) | |
1951 | max_process_desc = pty_fd; | |
6496aec9 | 1952 | |
94fcd171 | 1953 | pset_tty_name (p, build_string (pty_name)); |
6496aec9 | 1954 | } |
850d0752 | 1955 | |
94fcd171 | 1956 | p->pid = -2; |
850d0752 NR |
1957 | } |
1958 | ||
e690ca94 | 1959 | \f |
e690ca94 KS |
1960 | /* Convert an internal struct sockaddr to a lisp object (vector or string). |
1961 | The address family of sa is not included in the result. */ | |
1962 | ||
3d798ba7 PE |
1963 | #ifndef WINDOWSNT |
1964 | static | |
1965 | #endif | |
3dffe395 | 1966 | Lisp_Object |
d3da34e0 | 1967 | conv_sockaddr_to_lisp (struct sockaddr *sa, int len) |
e690ca94 KS |
1968 | { |
1969 | Lisp_Object address; | |
1970 | int i; | |
1971 | unsigned char *cp; | |
1972 | register struct Lisp_Vector *p; | |
1973 | ||
63136da6 AS |
1974 | /* Workaround for a bug in getsockname on BSD: Names bound to |
1975 | sockets in the UNIX domain are inaccessible; getsockname returns | |
1976 | a zero length name. */ | |
89887d67 | 1977 | if (len < offsetof (struct sockaddr, sa_family) + sizeof (sa->sa_family)) |
fff4e459 | 1978 | return empty_unibyte_string; |
63136da6 | 1979 | |
e690ca94 KS |
1980 | switch (sa->sa_family) |
1981 | { | |
1982 | case AF_INET: | |
1983 | { | |
1984 | struct sockaddr_in *sin = (struct sockaddr_in *) sa; | |
1985 | len = sizeof (sin->sin_addr) + 1; | |
1986 | address = Fmake_vector (make_number (len), Qnil); | |
1987 | p = XVECTOR (address); | |
91f2d272 | 1988 | p->contents[--len] = make_number (ntohs (sin->sin_port)); |
63136da6 | 1989 | cp = (unsigned char *) &sin->sin_addr; |
e690ca94 KS |
1990 | break; |
1991 | } | |
e1652a86 KS |
1992 | #ifdef AF_INET6 |
1993 | case AF_INET6: | |
1994 | { | |
1995 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; | |
63136da6 | 1996 | uint16_t *ip6 = (uint16_t *) &sin6->sin6_addr; |
e1652a86 KS |
1997 | len = sizeof (sin6->sin6_addr)/2 + 1; |
1998 | address = Fmake_vector (make_number (len), Qnil); | |
1999 | p = XVECTOR (address); | |
91f2d272 | 2000 | p->contents[--len] = make_number (ntohs (sin6->sin6_port)); |
e1652a86 | 2001 | for (i = 0; i < len; i++) |
91f2d272 | 2002 | p->contents[i] = make_number (ntohs (ip6[i])); |
e1652a86 KS |
2003 | return address; |
2004 | } | |
2005 | #endif | |
e690ca94 KS |
2006 | #ifdef HAVE_LOCAL_SOCKETS |
2007 | case AF_LOCAL: | |
2008 | { | |
fb23673a EZ |
2009 | struct sockaddr_un *sockun = (struct sockaddr_un *) sa; |
2010 | for (i = 0; i < sizeof (sockun->sun_path); i++) | |
2011 | if (sockun->sun_path[i] == 0) | |
e690ca94 | 2012 | break; |
fb23673a | 2013 | return make_unibyte_string (sockun->sun_path, i); |
e690ca94 KS |
2014 | } |
2015 | #endif | |
2016 | default: | |
89887d67 | 2017 | len -= offsetof (struct sockaddr, sa_family) + sizeof (sa->sa_family); |
e690ca94 KS |
2018 | address = Fcons (make_number (sa->sa_family), |
2019 | Fmake_vector (make_number (len), Qnil)); | |
2020 | p = XVECTOR (XCDR (address)); | |
63136da6 | 2021 | cp = (unsigned char *) &sa->sa_family + sizeof (sa->sa_family); |
e690ca94 KS |
2022 | break; |
2023 | } | |
2024 | ||
2025 | i = 0; | |
2026 | while (i < len) | |
91f2d272 | 2027 | p->contents[i++] = make_number (*cp++); |
e690ca94 KS |
2028 | |
2029 | return address; | |
2030 | } | |
2031 | ||
2032 | ||
2033 | /* Get family and required size for sockaddr structure to hold ADDRESS. */ | |
2034 | ||
2035 | static int | |
d3da34e0 | 2036 | get_lisp_to_sockaddr_size (Lisp_Object address, int *familyp) |
e690ca94 KS |
2037 | { |
2038 | register struct Lisp_Vector *p; | |
2039 | ||
2040 | if (VECTORP (address)) | |
2041 | { | |
2042 | p = XVECTOR (address); | |
eab3844f | 2043 | if (p->header.size == 5) |
e690ca94 KS |
2044 | { |
2045 | *familyp = AF_INET; | |
2046 | return sizeof (struct sockaddr_in); | |
2047 | } | |
e1652a86 | 2048 | #ifdef AF_INET6 |
eab3844f | 2049 | else if (p->header.size == 9) |
e1652a86 KS |
2050 | { |
2051 | *familyp = AF_INET6; | |
2052 | return sizeof (struct sockaddr_in6); | |
2053 | } | |
2054 | #endif | |
e690ca94 KS |
2055 | } |
2056 | #ifdef HAVE_LOCAL_SOCKETS | |
2057 | else if (STRINGP (address)) | |
2058 | { | |
2059 | *familyp = AF_LOCAL; | |
2060 | return sizeof (struct sockaddr_un); | |
2061 | } | |
2062 | #endif | |
d311d28c PE |
2063 | else if (CONSP (address) && TYPE_RANGED_INTEGERP (int, XCAR (address)) |
2064 | && VECTORP (XCDR (address))) | |
e690ca94 KS |
2065 | { |
2066 | struct sockaddr *sa; | |
2067 | *familyp = XINT (XCAR (address)); | |
2068 | p = XVECTOR (XCDR (address)); | |
eab3844f | 2069 | return p->header.size + sizeof (sa->sa_family); |
e690ca94 KS |
2070 | } |
2071 | return 0; | |
2072 | } | |
2073 | ||
2074 | /* Convert an address object (vector or string) to an internal sockaddr. | |
9d1a8e5d RS |
2075 | |
2076 | The address format has been basically validated by | |
2077 | get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid; | |
2078 | it could have come from user data. So if FAMILY is not valid, | |
2079 | we return after zeroing *SA. */ | |
e690ca94 KS |
2080 | |
2081 | static void | |
d3da34e0 | 2082 | conv_lisp_to_sockaddr (int family, Lisp_Object address, struct sockaddr *sa, int len) |
e690ca94 KS |
2083 | { |
2084 | register struct Lisp_Vector *p; | |
34967d0f | 2085 | register unsigned char *cp = NULL; |
e690ca94 | 2086 | register int i; |
d311d28c | 2087 | EMACS_INT hostport; |
e690ca94 | 2088 | |
72af86bd | 2089 | memset (sa, 0, len); |
e690ca94 KS |
2090 | |
2091 | if (VECTORP (address)) | |
2092 | { | |
2093 | p = XVECTOR (address); | |
2094 | if (family == AF_INET) | |
2095 | { | |
2096 | struct sockaddr_in *sin = (struct sockaddr_in *) sa; | |
2097 | len = sizeof (sin->sin_addr) + 1; | |
91f2d272 | 2098 | hostport = XINT (p->contents[--len]); |
d311d28c | 2099 | sin->sin_port = htons (hostport); |
e690ca94 | 2100 | cp = (unsigned char *)&sin->sin_addr; |
9d1a8e5d | 2101 | sa->sa_family = family; |
e690ca94 | 2102 | } |
e1652a86 KS |
2103 | #ifdef AF_INET6 |
2104 | else if (family == AF_INET6) | |
2105 | { | |
2106 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; | |
2107 | uint16_t *ip6 = (uint16_t *)&sin6->sin6_addr; | |
2108 | len = sizeof (sin6->sin6_addr) + 1; | |
91f2d272 | 2109 | hostport = XINT (p->contents[--len]); |
d311d28c | 2110 | sin6->sin6_port = htons (hostport); |
e1652a86 | 2111 | for (i = 0; i < len; i++) |
91f2d272 | 2112 | if (INTEGERP (p->contents[i])) |
e1652a86 | 2113 | { |
91f2d272 | 2114 | int j = XFASTINT (p->contents[i]) & 0xffff; |
e1652a86 KS |
2115 | ip6[i] = ntohs (j); |
2116 | } | |
9d1a8e5d | 2117 | sa->sa_family = family; |
0eb7675e | 2118 | return; |
e1652a86 KS |
2119 | } |
2120 | #endif | |
0eb7675e AS |
2121 | else |
2122 | return; | |
e690ca94 KS |
2123 | } |
2124 | else if (STRINGP (address)) | |
2125 | { | |
2126 | #ifdef HAVE_LOCAL_SOCKETS | |
2127 | if (family == AF_LOCAL) | |
2128 | { | |
fb23673a | 2129 | struct sockaddr_un *sockun = (struct sockaddr_un *) sa; |
d5db4077 | 2130 | cp = SDATA (address); |
fb23673a EZ |
2131 | for (i = 0; i < sizeof (sockun->sun_path) && *cp; i++) |
2132 | sockun->sun_path[i] = *cp++; | |
9d1a8e5d | 2133 | sa->sa_family = family; |
e690ca94 KS |
2134 | } |
2135 | #endif | |
2136 | return; | |
2137 | } | |
2138 | else | |
2139 | { | |
2140 | p = XVECTOR (XCDR (address)); | |
2141 | cp = (unsigned char *)sa + sizeof (sa->sa_family); | |
2142 | } | |
2143 | ||
2144 | for (i = 0; i < len; i++) | |
91f2d272 PE |
2145 | if (INTEGERP (p->contents[i])) |
2146 | *cp++ = XFASTINT (p->contents[i]) & 0xff; | |
e690ca94 KS |
2147 | } |
2148 | ||
2149 | #ifdef DATAGRAM_SOCKETS | |
2150 | DEFUN ("process-datagram-address", Fprocess_datagram_address, Sprocess_datagram_address, | |
2151 | 1, 1, 0, | |
2152 | doc: /* Get the current datagram address associated with PROCESS. */) | |
5842a27b | 2153 | (Lisp_Object process) |
e690ca94 KS |
2154 | { |
2155 | int channel; | |
2156 | ||
2157 | CHECK_PROCESS (process); | |
2158 | ||
2159 | if (!DATAGRAM_CONN_P (process)) | |
2160 | return Qnil; | |
2161 | ||
60f0fb11 | 2162 | channel = XPROCESS (process)->infd; |
e690ca94 KS |
2163 | return conv_sockaddr_to_lisp (datagram_address[channel].sa, |
2164 | datagram_address[channel].len); | |
2165 | } | |
2166 | ||
2167 | DEFUN ("set-process-datagram-address", Fset_process_datagram_address, Sset_process_datagram_address, | |
2168 | 2, 2, 0, | |
2169 | doc: /* Set the datagram address for PROCESS to ADDRESS. | |
2170 | Returns nil upon error setting address, ADDRESS otherwise. */) | |
5842a27b | 2171 | (Lisp_Object process, Lisp_Object address) |
e690ca94 KS |
2172 | { |
2173 | int channel; | |
2174 | int family, len; | |
2175 | ||
2176 | CHECK_PROCESS (process); | |
2177 | ||
2178 | if (!DATAGRAM_CONN_P (process)) | |
2179 | return Qnil; | |
2180 | ||
60f0fb11 | 2181 | channel = XPROCESS (process)->infd; |
e690ca94 KS |
2182 | |
2183 | len = get_lisp_to_sockaddr_size (address, &family); | |
a9ebfa0b | 2184 | if (len == 0 || datagram_address[channel].len != len) |
e690ca94 KS |
2185 | return Qnil; |
2186 | conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len); | |
2187 | return address; | |
2188 | } | |
2189 | #endif | |
2190 | \f | |
2191 | ||
5e2327cf | 2192 | static const struct socket_options { |
e690ca94 | 2193 | /* The name of this option. Should be lowercase version of option |
177c0ea7 | 2194 | name without SO_ prefix. */ |
fce70521 | 2195 | const char *name; |
e690ca94 KS |
2196 | /* Option level SOL_... */ |
2197 | int optlevel; | |
2198 | /* Option number SO_... */ | |
2199 | int optnum; | |
6b61353c KH |
2200 | enum { SOPT_UNKNOWN, SOPT_BOOL, SOPT_INT, SOPT_IFNAME, SOPT_LINGER } opttype; |
2201 | enum { OPIX_NONE=0, OPIX_MISC=1, OPIX_REUSEADDR=2 } optbit; | |
e690ca94 KS |
2202 | } socket_options[] = |
2203 | { | |
2204 | #ifdef SO_BINDTODEVICE | |
6b61353c | 2205 | { ":bindtodevice", SOL_SOCKET, SO_BINDTODEVICE, SOPT_IFNAME, OPIX_MISC }, |
e690ca94 KS |
2206 | #endif |
2207 | #ifdef SO_BROADCAST | |
6b61353c | 2208 | { ":broadcast", SOL_SOCKET, SO_BROADCAST, SOPT_BOOL, OPIX_MISC }, |
e690ca94 KS |
2209 | #endif |
2210 | #ifdef SO_DONTROUTE | |
6b61353c | 2211 | { ":dontroute", SOL_SOCKET, SO_DONTROUTE, SOPT_BOOL, OPIX_MISC }, |
e690ca94 KS |
2212 | #endif |
2213 | #ifdef SO_KEEPALIVE | |
6b61353c | 2214 | { ":keepalive", SOL_SOCKET, SO_KEEPALIVE, SOPT_BOOL, OPIX_MISC }, |
e690ca94 KS |
2215 | #endif |
2216 | #ifdef SO_LINGER | |
6b61353c | 2217 | { ":linger", SOL_SOCKET, SO_LINGER, SOPT_LINGER, OPIX_MISC }, |
e690ca94 KS |
2218 | #endif |
2219 | #ifdef SO_OOBINLINE | |
6b61353c | 2220 | { ":oobinline", SOL_SOCKET, SO_OOBINLINE, SOPT_BOOL, OPIX_MISC }, |
e690ca94 KS |
2221 | #endif |
2222 | #ifdef SO_PRIORITY | |
6b61353c | 2223 | { ":priority", SOL_SOCKET, SO_PRIORITY, SOPT_INT, OPIX_MISC }, |
e690ca94 KS |
2224 | #endif |
2225 | #ifdef SO_REUSEADDR | |
6b61353c | 2226 | { ":reuseaddr", SOL_SOCKET, SO_REUSEADDR, SOPT_BOOL, OPIX_REUSEADDR }, |
e690ca94 | 2227 | #endif |
6b61353c | 2228 | { 0, 0, 0, SOPT_UNKNOWN, OPIX_NONE } |
e690ca94 KS |
2229 | }; |
2230 | ||
6b61353c | 2231 | /* Set option OPT to value VAL on socket S. |
e690ca94 | 2232 | |
6b61353c KH |
2233 | Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise. |
2234 | Signals an error if setting a known option fails. | |
2235 | */ | |
e690ca94 KS |
2236 | |
2237 | static int | |
d3da34e0 | 2238 | set_socket_option (int s, Lisp_Object opt, Lisp_Object val) |
e690ca94 | 2239 | { |
6b61353c | 2240 | char *name; |
5e2327cf | 2241 | const struct socket_options *sopt; |
6b61353c | 2242 | int ret = 0; |
e690ca94 | 2243 | |
6b61353c | 2244 | CHECK_SYMBOL (opt); |
e690ca94 | 2245 | |
51b59d79 | 2246 | name = SSDATA (SYMBOL_NAME (opt)); |
6b61353c KH |
2247 | for (sopt = socket_options; sopt->name; sopt++) |
2248 | if (strcmp (name, sopt->name) == 0) | |
2249 | break; | |
e690ca94 | 2250 | |
6b61353c KH |
2251 | switch (sopt->opttype) |
2252 | { | |
2253 | case SOPT_BOOL: | |
2254 | { | |
2255 | int optval; | |
2256 | optval = NILP (val) ? 0 : 1; | |
2257 | ret = setsockopt (s, sopt->optlevel, sopt->optnum, | |
2258 | &optval, sizeof (optval)); | |
2259 | break; | |
2260 | } | |
e690ca94 | 2261 | |
6b61353c KH |
2262 | case SOPT_INT: |
2263 | { | |
2264 | int optval; | |
d311d28c | 2265 | if (TYPE_RANGED_INTEGERP (int, val)) |
6b61353c KH |
2266 | optval = XINT (val); |
2267 | else | |
2268 | error ("Bad option value for %s", name); | |
2269 | ret = setsockopt (s, sopt->optlevel, sopt->optnum, | |
2270 | &optval, sizeof (optval)); | |
2271 | break; | |
2272 | } | |
e690ca94 | 2273 | |
6b61353c KH |
2274 | #ifdef SO_BINDTODEVICE |
2275 | case SOPT_IFNAME: | |
2276 | { | |
2277 | char devname[IFNAMSIZ+1]; | |
e690ca94 | 2278 | |
6b61353c KH |
2279 | /* This is broken, at least in the Linux 2.4 kernel. |
2280 | To unbind, the arg must be a zero integer, not the empty string. | |
2281 | This should work on all systems. KFS. 2003-09-23. */ | |
72af86bd | 2282 | memset (devname, 0, sizeof devname); |
6b61353c | 2283 | if (STRINGP (val)) |
e690ca94 | 2284 | { |
51b59d79 | 2285 | char *arg = SSDATA (val); |
6b61353c | 2286 | int len = min (strlen (arg), IFNAMSIZ); |
72af86bd | 2287 | memcpy (devname, arg, len); |
e690ca94 | 2288 | } |
6b61353c KH |
2289 | else if (!NILP (val)) |
2290 | error ("Bad option value for %s", name); | |
2291 | ret = setsockopt (s, sopt->optlevel, sopt->optnum, | |
2292 | devname, IFNAMSIZ); | |
2293 | break; | |
2294 | } | |
2295 | #endif | |
e690ca94 | 2296 | |
177c0ea7 | 2297 | #ifdef SO_LINGER |
6b61353c KH |
2298 | case SOPT_LINGER: |
2299 | { | |
2300 | struct linger linger; | |
e690ca94 | 2301 | |
6b61353c KH |
2302 | linger.l_onoff = 1; |
2303 | linger.l_linger = 0; | |
d311d28c | 2304 | if (TYPE_RANGED_INTEGERP (int, val)) |
6b61353c KH |
2305 | linger.l_linger = XINT (val); |
2306 | else | |
2307 | linger.l_onoff = NILP (val) ? 0 : 1; | |
2308 | ret = setsockopt (s, sopt->optlevel, sopt->optnum, | |
2309 | &linger, sizeof (linger)); | |
2310 | break; | |
2311 | } | |
e690ca94 | 2312 | #endif |
6b61353c KH |
2313 | |
2314 | default: | |
2315 | return 0; | |
e690ca94 | 2316 | } |
6b61353c KH |
2317 | |
2318 | if (ret < 0) | |
b648c163 PE |
2319 | { |
2320 | int setsockopt_errno = errno; | |
2321 | report_file_errno ("Cannot set network option", list2 (opt, val), | |
2322 | setsockopt_errno); | |
2323 | } | |
2324 | ||
6b61353c | 2325 | return (1 << sopt->optbit); |
e690ca94 KS |
2326 | } |
2327 | ||
6b61353c KH |
2328 | |
2329 | DEFUN ("set-network-process-option", | |
2330 | Fset_network_process_option, Sset_network_process_option, | |
2331 | 3, 4, 0, | |
2332 | doc: /* For network process PROCESS set option OPTION to value VALUE. | |
2333 | See `make-network-process' for a list of options and values. | |
2334 | If optional fourth arg NO-ERROR is non-nil, don't signal an error if | |
2335 | OPTION is not a supported option, return nil instead; otherwise return t. */) | |
5842a27b | 2336 | (Lisp_Object process, Lisp_Object option, Lisp_Object value, Lisp_Object no_error) |
e690ca94 | 2337 | { |
6b61353c KH |
2338 | int s; |
2339 | struct Lisp_Process *p; | |
e690ca94 | 2340 | |
e690ca94 | 2341 | CHECK_PROCESS (process); |
6b61353c KH |
2342 | p = XPROCESS (process); |
2343 | if (!NETCONN1_P (p)) | |
2344 | error ("Process is not a network process"); | |
2345 | ||
60f0fb11 | 2346 | s = p->infd; |
6b61353c KH |
2347 | if (s < 0) |
2348 | error ("Process is not running"); | |
2349 | ||
2350 | if (set_socket_option (s, option, value)) | |
e690ca94 | 2351 | { |
6a09a33b | 2352 | pset_childp (p, Fplist_put (p->childp, option, value)); |
6b61353c | 2353 | return Qt; |
e690ca94 | 2354 | } |
6b61353c KH |
2355 | |
2356 | if (NILP (no_error)) | |
2357 | error ("Unknown or unsupported option"); | |
2358 | ||
2359 | return Qnil; | |
e690ca94 | 2360 | } |
6b61353c | 2361 | |
e690ca94 | 2362 | \f |
d888760c GM |
2363 | DEFUN ("serial-process-configure", |
2364 | Fserial_process_configure, | |
2365 | Sserial_process_configure, | |
2366 | 0, MANY, 0, | |
2367 | doc: /* Configure speed, bytesize, etc. of a serial process. | |
2368 | ||
2369 | Arguments are specified as keyword/argument pairs. Attributes that | |
2370 | are not given are re-initialized from the process's current | |
2371 | configuration (available via the function `process-contact') or set to | |
2372 | reasonable default values. The following arguments are defined: | |
2373 | ||
2374 | :process PROCESS | |
2375 | :name NAME | |
2376 | :buffer BUFFER | |
2377 | :port PORT | |
2378 | -- Any of these arguments can be given to identify the process that is | |
2379 | to be configured. If none of these arguments is given, the current | |
2380 | buffer's process is used. | |
2381 | ||
2382 | :speed SPEED -- SPEED is the speed of the serial port in bits per | |
2383 | second, also called baud rate. Any value can be given for SPEED, but | |
2384 | most serial ports work only at a few defined values between 1200 and | |
2385 | 115200, with 9600 being the most common value. If SPEED is nil, the | |
2386 | serial port is not configured any further, i.e., all other arguments | |
2387 | are ignored. This may be useful for special serial ports such as | |
2388 | Bluetooth-to-serial converters which can only be configured through AT | |
2389 | commands. A value of nil for SPEED can be used only when passed | |
2390 | through `make-serial-process' or `serial-term'. | |
2391 | ||
2392 | :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which | |
2393 | can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used. | |
2394 | ||
2395 | :parity PARITY -- PARITY can be nil (don't use parity), the symbol | |
2396 | `odd' (use odd parity), or the symbol `even' (use even parity). If | |
2397 | PARITY is not given, no parity is used. | |
2398 | ||
2399 | :stopbits STOPBITS -- STOPBITS is the number of stopbits used to | |
2400 | terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS | |
2401 | is not given or nil, 1 stopbit is used. | |
2402 | ||
2403 | :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of | |
2404 | flowcontrol to be used, which is either nil (don't use flowcontrol), | |
2405 | the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw' | |
2406 | \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no | |
2407 | flowcontrol is used. | |
2408 | ||
2409 | `serial-process-configure' is called by `make-serial-process' for the | |
2410 | initial configuration of the serial port. | |
2411 | ||
2412 | Examples: | |
2413 | ||
2414 | \(serial-process-configure :process "/dev/ttyS0" :speed 1200) | |
2415 | ||
2416 | \(serial-process-configure | |
2417 | :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw) | |
2418 | ||
2419 | \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7) | |
2420 | ||
2421 | usage: (serial-process-configure &rest ARGS) */) | |
f66c7cf8 | 2422 | (ptrdiff_t nargs, Lisp_Object *args) |
d888760c GM |
2423 | { |
2424 | struct Lisp_Process *p; | |
2425 | Lisp_Object contact = Qnil; | |
2426 | Lisp_Object proc = Qnil; | |
2427 | struct gcpro gcpro1; | |
2428 | ||
2429 | contact = Flist (nargs, args); | |
2430 | GCPRO1 (contact); | |
2431 | ||
2432 | proc = Fplist_get (contact, QCprocess); | |
2433 | if (NILP (proc)) | |
2434 | proc = Fplist_get (contact, QCname); | |
2435 | if (NILP (proc)) | |
2436 | proc = Fplist_get (contact, QCbuffer); | |
2437 | if (NILP (proc)) | |
2438 | proc = Fplist_get (contact, QCport); | |
2439 | proc = get_process (proc); | |
2440 | p = XPROCESS (proc); | |
4d2b044c | 2441 | if (!EQ (p->type, Qserial)) |
d888760c GM |
2442 | error ("Not a serial process"); |
2443 | ||
4d2b044c | 2444 | if (NILP (Fplist_get (p->childp, QCspeed))) |
d888760c GM |
2445 | { |
2446 | UNGCPRO; | |
2447 | return Qnil; | |
2448 | } | |
2449 | ||
2450 | serial_configure (p, contact); | |
2451 | ||
2452 | UNGCPRO; | |
2453 | return Qnil; | |
2454 | } | |
d888760c | 2455 | |
d888760c GM |
2456 | DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process, |
2457 | 0, MANY, 0, | |
2458 | doc: /* Create and return a serial port process. | |
2459 | ||
2460 | In Emacs, serial port connections are represented by process objects, | |
2461 | so input and output work as for subprocesses, and `delete-process' | |
2462 | closes a serial port connection. However, a serial process has no | |
2463 | process id, it cannot be signaled, and the status codes are different | |
2464 | from normal processes. | |
2465 | ||
2466 | `make-serial-process' creates a process and a buffer, on which you | |
2467 | probably want to use `process-send-string'. Try \\[serial-term] for | |
2468 | an interactive terminal. See below for examples. | |
2469 | ||
2470 | Arguments are specified as keyword/argument pairs. The following | |
2471 | arguments are defined: | |
2472 | ||
2473 | :port PORT -- (mandatory) PORT is the path or name of the serial port. | |
2474 | For example, this could be "/dev/ttyS0" on Unix. On Windows, this | |
2475 | could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double | |
2476 | the backslashes in strings). | |
2477 | ||
2478 | :speed SPEED -- (mandatory) is handled by `serial-process-configure', | |
4373fd43 | 2479 | which this function calls. |
d888760c GM |
2480 | |
2481 | :name NAME -- NAME is the name of the process. If NAME is not given, | |
2482 | the value of PORT is used. | |
2483 | ||
2484 | :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate | |
2485 | with the process. Process output goes at the end of that buffer, | |
2486 | unless you specify an output stream or filter function to handle the | |
2487 | output. If BUFFER is not given, the value of NAME is used. | |
2488 | ||
2489 | :coding CODING -- If CODING is a symbol, it specifies the coding | |
2490 | system used for both reading and writing for this process. If CODING | |
2491 | is a cons (DECODING . ENCODING), DECODING is used for reading, and | |
2492 | ENCODING is used for writing. | |
2493 | ||
2494 | :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and | |
2495 | the process is running. If BOOL is not given, query before exiting. | |
2496 | ||
2497 | :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil. | |
2498 | In the stopped state, a serial process does not accept incoming data, | |
2499 | but you can send outgoing data. The stopped state is cleared by | |
2500 | `continue-process' and set by `stop-process'. | |
2501 | ||
2502 | :filter FILTER -- Install FILTER as the process filter. | |
2503 | ||
2504 | :sentinel SENTINEL -- Install SENTINEL as the process sentinel. | |
2505 | ||
2506 | :plist PLIST -- Install PLIST as the initial plist of the process. | |
2507 | ||
d888760c GM |
2508 | :bytesize |
2509 | :parity | |
2510 | :stopbits | |
2511 | :flowcontrol | |
4373fd43 GM |
2512 | -- This function calls `serial-process-configure' to handle these |
2513 | arguments. | |
d888760c GM |
2514 | |
2515 | The original argument list, possibly modified by later configuration, | |
2516 | is available via the function `process-contact'. | |
2517 | ||
2518 | Examples: | |
2519 | ||
2520 | \(make-serial-process :port "/dev/ttyS0" :speed 9600) | |
2521 | ||
2522 | \(make-serial-process :port "COM1" :speed 115200 :stopbits 2) | |
2523 | ||
2524 | \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd) | |
2525 | ||
2526 | \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil) | |
2527 | ||
2528 | usage: (make-serial-process &rest ARGS) */) | |
f66c7cf8 | 2529 | (ptrdiff_t nargs, Lisp_Object *args) |
d888760c GM |
2530 | { |
2531 | int fd = -1; | |
2532 | Lisp_Object proc, contact, port; | |
2533 | struct Lisp_Process *p; | |
2534 | struct gcpro gcpro1; | |
2535 | Lisp_Object name, buffer; | |
2536 | Lisp_Object tem, val; | |
9349e5f7 | 2537 | ptrdiff_t specpdl_count; |
d888760c GM |
2538 | |
2539 | if (nargs == 0) | |
2540 | return Qnil; | |
2541 | ||
2542 | contact = Flist (nargs, args); | |
2543 | GCPRO1 (contact); | |
2544 | ||
2545 | port = Fplist_get (contact, QCport); | |
2546 | if (NILP (port)) | |
2547 | error ("No port specified"); | |
2548 | CHECK_STRING (port); | |
2549 | ||
2550 | if (NILP (Fplist_member (contact, QCspeed))) | |
2551 | error (":speed not specified"); | |
2552 | if (!NILP (Fplist_get (contact, QCspeed))) | |
2553 | CHECK_NUMBER (Fplist_get (contact, QCspeed)); | |
2554 | ||
2555 | name = Fplist_get (contact, QCname); | |
2556 | if (NILP (name)) | |
2557 | name = port; | |
2558 | CHECK_STRING (name); | |
2559 | proc = make_process (name); | |
2560 | specpdl_count = SPECPDL_INDEX (); | |
27e498e6 | 2561 | record_unwind_protect (remove_process, proc); |
d888760c GM |
2562 | p = XPROCESS (proc); |
2563 | ||
4e604a5d | 2564 | fd = serial_open (port); |
94fcd171 | 2565 | p->open_fd[SUBPROCESS_STDIN] = fd; |
d888760c GM |
2566 | p->infd = fd; |
2567 | p->outfd = fd; | |
2568 | if (fd > max_process_desc) | |
2569 | max_process_desc = fd; | |
2570 | chan_process[fd] = proc; | |
2571 | ||
2572 | buffer = Fplist_get (contact, QCbuffer); | |
2573 | if (NILP (buffer)) | |
2574 | buffer = name; | |
2575 | buffer = Fget_buffer_create (buffer); | |
6a09a33b PE |
2576 | pset_buffer (p, buffer); |
2577 | ||
2578 | pset_childp (p, contact); | |
2579 | pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist))); | |
2580 | pset_type (p, Qserial); | |
2581 | pset_sentinel (p, Fplist_get (contact, QCsentinel)); | |
2582 | pset_filter (p, Fplist_get (contact, QCfilter)); | |
2583 | pset_log (p, Qnil); | |
d888760c GM |
2584 | if (tem = Fplist_get (contact, QCnoquery), !NILP (tem)) |
2585 | p->kill_without_query = 1; | |
2586 | if (tem = Fplist_get (contact, QCstop), !NILP (tem)) | |
6a09a33b | 2587 | pset_command (p, Qt); |
6496aec9 | 2588 | eassert (! p->pty_flag); |
d888760c | 2589 | |
4d2b044c | 2590 | if (!EQ (p->command, Qt)) |
d888760c GM |
2591 | { |
2592 | FD_SET (fd, &input_wait_mask); | |
2593 | FD_SET (fd, &non_keyboard_wait_mask); | |
2594 | } | |
2595 | ||
2596 | if (BUFFERP (buffer)) | |
2597 | { | |
4d2b044c | 2598 | set_marker_both (p->mark, buffer, |
d888760c GM |
2599 | BUF_ZV (XBUFFER (buffer)), |
2600 | BUF_ZV_BYTE (XBUFFER (buffer))); | |
2601 | } | |
2602 | ||
2603 | tem = Fplist_member (contact, QCcoding); | |
2604 | if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem)))) | |
2605 | tem = Qnil; | |
2606 | ||
2607 | val = Qnil; | |
2608 | if (!NILP (tem)) | |
2609 | { | |
2610 | val = XCAR (XCDR (tem)); | |
2611 | if (CONSP (val)) | |
2612 | val = XCAR (val); | |
2613 | } | |
2614 | else if (!NILP (Vcoding_system_for_read)) | |
2615 | val = Vcoding_system_for_read; | |
4b4deea2 TT |
2616 | else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters))) |
2617 | || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))) | |
d888760c | 2618 | val = Qnil; |
6a09a33b | 2619 | pset_decode_coding_system (p, val); |
d888760c GM |
2620 | |
2621 | val = Qnil; | |
2622 | if (!NILP (tem)) | |
2623 | { | |
2624 | val = XCAR (XCDR (tem)); | |
2625 | if (CONSP (val)) | |
2626 | val = XCDR (val); | |
2627 | } | |
2628 | else if (!NILP (Vcoding_system_for_write)) | |
2629 | val = Vcoding_system_for_write; | |
4b4deea2 TT |
2630 | else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters))) |
2631 | || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))) | |
d888760c | 2632 | val = Qnil; |
6a09a33b | 2633 | pset_encode_coding_system (p, val); |
d888760c GM |
2634 | |
2635 | setup_process_coding_systems (proc); | |
6a09a33b | 2636 | pset_decoding_buf (p, empty_unibyte_string); |
d888760c | 2637 | p->decoding_carryover = 0; |
6a09a33b | 2638 | pset_encoding_buf (p, empty_unibyte_string); |
d888760c GM |
2639 | p->inherit_coding_system_flag |
2640 | = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system); | |
2641 | ||
ed3751c8 | 2642 | Fserial_process_configure (nargs, args); |
d888760c GM |
2643 | |
2644 | specpdl_ptr = specpdl + specpdl_count; | |
2645 | ||
2646 | UNGCPRO; | |
2647 | return proc; | |
2648 | } | |
d888760c | 2649 | |
e690ca94 KS |
2650 | /* Create a network stream/datagram client/server process. Treated |
2651 | exactly like a normal process when reading and writing. Primary | |
d0d6b7c5 JB |
2652 | differences are in status display and process deletion. A network |
2653 | connection has no PID; you cannot signal it. All you can do is | |
e690ca94 KS |
2654 | stop/continue it and deactivate/close it via delete-process */ |
2655 | ||
177c0ea7 JB |
2656 | DEFUN ("make-network-process", Fmake_network_process, Smake_network_process, |
2657 | 0, MANY, 0, | |
e690ca94 KS |
2658 | doc: /* Create and return a network server or client process. |
2659 | ||
fa9d4315 | 2660 | In Emacs, network connections are represented by process objects, so |
e690ca94 KS |
2661 | input and output work as for subprocesses and `delete-process' closes |
2662 | a network connection. However, a network process has no process id, | |
d22426e1 | 2663 | it cannot be signaled, and the status codes are different from normal |
e690ca94 KS |
2664 | processes. |
2665 | ||
2666 | Arguments are specified as keyword/argument pairs. The following | |
2667 | arguments are defined: | |
2668 | ||
2669 | :name NAME -- NAME is name for process. It is modified if necessary | |
2670 | to make it unique. | |
2671 | ||
2672 | :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate | |
2673 | with the process. Process output goes at end of that buffer, unless | |
2674 | you specify an output stream or filter function to handle the output. | |
2675 | BUFFER may be also nil, meaning that this process is not associated | |
2676 | with any buffer. | |
2677 | ||
2678 | :host HOST -- HOST is name of the host to connect to, or its IP | |
2679 | address. The symbol `local' specifies the local host. If specified | |
2680 | for a server process, it must be a valid name or address for the local | |
2681 | host, and only clients connecting to that address will be accepted. | |
2682 | ||
2683 | :service SERVICE -- SERVICE is name of the service desired, or an | |
2684 | integer specifying a port number to connect to. If SERVICE is t, | |
eba25e7c GM |
2685 | a random port number is selected for the server. (If Emacs was |
2686 | compiled with getaddrinfo, a port number can also be specified as a | |
2687 | string, e.g. "80", as well as an integer. This is not portable.) | |
e690ca94 | 2688 | |
9057ff80 | 2689 | :type TYPE -- TYPE is the type of connection. The default (nil) is a |
f00c449b SM |
2690 | stream type connection, `datagram' creates a datagram type connection, |
2691 | `seqpacket' creates a reliable datagram connection. | |
9057ff80 | 2692 | |
e690ca94 | 2693 | :family FAMILY -- FAMILY is the address (and protocol) family for the |
e1652a86 KS |
2694 | service specified by HOST and SERVICE. The default (nil) is to use |
2695 | whatever address family (IPv4 or IPv6) that is defined for the host | |
2696 | and port number specified by HOST and SERVICE. Other address families | |
2697 | supported are: | |
e690ca94 | 2698 | local -- for a local (i.e. UNIX) address specified by SERVICE. |
e1652a86 KS |
2699 | ipv4 -- use IPv4 address family only. |
2700 | ipv6 -- use IPv6 address family only. | |
e690ca94 KS |
2701 | |
2702 | :local ADDRESS -- ADDRESS is the local address used for the connection. | |
2703 | This parameter is ignored when opening a client process. When specified | |
2704 | for a server process, the FAMILY, HOST and SERVICE args are ignored. | |
2705 | ||
2706 | :remote ADDRESS -- ADDRESS is the remote partner's address for the | |
2707 | connection. This parameter is ignored when opening a stream server | |
2708 | process. For a datagram server process, it specifies the initial | |
2709 | setting of the remote datagram address. When specified for a client | |
2710 | process, the FAMILY, HOST, and SERVICE args are ignored. | |
2711 | ||
2712 | The format of ADDRESS depends on the address family: | |
2713 | - An IPv4 address is represented as an vector of integers [A B C D P] | |
2714 | corresponding to numeric IP address A.B.C.D and port number P. | |
2715 | - A local address is represented as a string with the address in the | |
2716 | local address space. | |
2717 | - An "unsupported family" address is represented by a cons (F . AV) | |
2718 | where F is the family number and AV is a vector containing the socket | |
2719 | address data with one element per address data byte. Do not rely on | |
2720 | this format in portable code, as it may depend on implementation | |
2721 | defined constants, data sizes, and data structure alignment. | |
2722 | ||
6b61353c KH |
2723 | :coding CODING -- If CODING is a symbol, it specifies the coding |
2724 | system used for both reading and writing for this process. If CODING | |
2725 | is a cons (DECODING . ENCODING), DECODING is used for reading, and | |
2726 | ENCODING is used for writing. | |
e690ca94 KS |
2727 | |
2728 | :nowait BOOL -- If BOOL is non-nil for a stream type client process, | |
2729 | return without waiting for the connection to complete; instead, the | |
2730 | sentinel function will be called with second arg matching "open" (if | |
2731 | successful) or "failed" when the connect completes. Default is to use | |
2732 | a blocking connect (i.e. wait) for stream type connections. | |
2733 | ||
2734 | :noquery BOOL -- Query the user unless BOOL is non-nil, and process is | |
f3d9532b | 2735 | running when Emacs is exited. |
e690ca94 KS |
2736 | |
2737 | :stop BOOL -- Start process in the `stopped' state if BOOL non-nil. | |
2738 | In the stopped state, a server process does not accept new | |
2739 | connections, and a client process does not handle incoming traffic. | |
2740 | The stopped state is cleared by `continue-process' and set by | |
2741 | `stop-process'. | |
2742 | ||
2743 | :filter FILTER -- Install FILTER as the process filter. | |
2744 | ||
7392e23c KS |
2745 | :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the |
2746 | process filter are multibyte, otherwise they are unibyte. | |
e0f24100 | 2747 | If this keyword is not specified, the strings are multibyte if |
016a35df | 2748 | the default value of `enable-multibyte-characters' is non-nil. |
03f04413 | 2749 | |
e690ca94 KS |
2750 | :sentinel SENTINEL -- Install SENTINEL as the process sentinel. |
2751 | ||
2752 | :log LOG -- Install LOG as the server process log function. This | |
991234f0 | 2753 | function is called when the server accepts a network connection from a |
e690ca94 KS |
2754 | client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER |
2755 | is the server process, CLIENT is the new process for the connection, | |
2756 | and MESSAGE is a string. | |
2757 | ||
faa7db08 | 2758 | :plist PLIST -- Install PLIST as the new process' initial plist. |
177c0ea7 | 2759 | |
6b61353c | 2760 | :server QLEN -- if QLEN is non-nil, create a server process for the |
e690ca94 | 2761 | specified FAMILY, SERVICE, and connection type (stream or datagram). |
6b61353c KH |
2762 | If QLEN is an integer, it is used as the max. length of the server's |
2763 | pending connection queue (also known as the backlog); the default | |
2764 | queue length is 5. Default is to create a client process. | |
2765 | ||
2766 | The following network options can be specified for this connection: | |
2767 | ||
2768 | :broadcast BOOL -- Allow send and receive of datagram broadcasts. | |
2769 | :dontroute BOOL -- Only send to directly connected hosts. | |
2770 | :keepalive BOOL -- Send keep-alive messages on network stream. | |
2771 | :linger BOOL or TIMEOUT -- Send queued messages before closing. | |
2772 | :oobinline BOOL -- Place out-of-band data in receive data stream. | |
2773 | :priority INT -- Set protocol defined priority for sent packets. | |
2774 | :reuseaddr BOOL -- Allow reusing a recently used local address | |
2775 | (this is allowed by default for a server process). | |
2776 | :bindtodevice NAME -- bind to interface NAME. Using this may require | |
2777 | special privileges on some systems. | |
2778 | ||
2779 | Consult the relevant system programmer's manual pages for more | |
2780 | information on using these options. | |
2781 | ||
2782 | ||
2783 | A server process will listen for and accept connections from clients. | |
2784 | When a client connection is accepted, a new network process is created | |
2785 | for the connection with the following parameters: | |
e690ca94 | 2786 | |
e690ca94 KS |
2787 | - The client's process name is constructed by concatenating the server |
2788 | process' NAME and a client identification string. | |
2789 | - If the FILTER argument is non-nil, the client process will not get a | |
2790 | separate process buffer; otherwise, the client's process buffer is a newly | |
2791 | created buffer named after the server process' BUFFER name or process | |
177c0ea7 | 2792 | NAME concatenated with the client identification string. |
e690ca94 KS |
2793 | - The connection type and the process filter and sentinel parameters are |
2794 | inherited from the server process' TYPE, FILTER and SENTINEL. | |
2795 | - The client process' contact info is set according to the client's | |
2796 | addressing information (typically an IP address and a port number). | |
faa7db08 | 2797 | - The client process' plist is initialized from the server's plist. |
e690ca94 KS |
2798 | |
2799 | Notice that the FILTER and SENTINEL args are never used directly by | |
2800 | the server process. Also, the BUFFER argument is not used directly by | |
9e9a5792 KS |
2801 | the server process, but via the optional :log function, accepted (and |
2802 | failed) connections may be logged in the server process' buffer. | |
e690ca94 | 2803 | |
365aee82 KS |
2804 | The original argument list, modified with the actual connection |
2805 | information, is available via the `process-contact' function. | |
365aee82 | 2806 | |
fa9d4315 | 2807 | usage: (make-network-process &rest ARGS) */) |
f66c7cf8 | 2808 | (ptrdiff_t nargs, Lisp_Object *args) |
d0d6b7c5 JB |
2809 | { |
2810 | Lisp_Object proc; | |
e690ca94 KS |
2811 | Lisp_Object contact; |
2812 | struct Lisp_Process *p; | |
70dbdb36 | 2813 | #ifdef HAVE_GETADDRINFO |
e690ca94 | 2814 | struct addrinfo ai, *res, *lres; |
e1652a86 | 2815 | struct addrinfo hints; |
8ea90aa3 DN |
2816 | const char *portstring; |
2817 | char portbuf[128]; | |
70dbdb36 | 2818 | #else /* HAVE_GETADDRINFO */ |
dd2a17ab KS |
2819 | struct _emacs_addrinfo |
2820 | { | |
2821 | int ai_family; | |
2822 | int ai_socktype; | |
2823 | int ai_protocol; | |
2824 | int ai_addrlen; | |
2825 | struct sockaddr *ai_addr; | |
2826 | struct _emacs_addrinfo *ai_next; | |
2827 | } ai, *res, *lres; | |
418b48fd | 2828 | #endif /* HAVE_GETADDRINFO */ |
e690ca94 KS |
2829 | struct sockaddr_in address_in; |
2830 | #ifdef HAVE_LOCAL_SOCKETS | |
2831 | struct sockaddr_un address_un; | |
2832 | #endif | |
2833 | int port; | |
dd2a17ab KS |
2834 | int ret = 0; |
2835 | int xerrno = 0; | |
418b48fd | 2836 | int s = -1, outch, inch; |
e690ca94 | 2837 | struct gcpro gcpro1; |
d311d28c PE |
2838 | ptrdiff_t count = SPECPDL_INDEX (); |
2839 | ptrdiff_t count1; | |
e690ca94 KS |
2840 | Lisp_Object QCaddress; /* one of QClocal or QCremote */ |
2841 | Lisp_Object tem; | |
2842 | Lisp_Object name, buffer, host, service, address; | |
2843 | Lisp_Object filter, sentinel; | |
0bce5d9e PE |
2844 | bool is_non_blocking_client = 0; |
2845 | bool is_server = 0; | |
2846 | int backlog = 5; | |
9057ff80 | 2847 | int socktype; |
e690ca94 KS |
2848 | int family = -1; |
2849 | ||
2850 | if (nargs == 0) | |
2851 | return Qnil; | |
dd2a17ab | 2852 | |
e690ca94 KS |
2853 | /* Save arguments for process-contact and clone-process. */ |
2854 | contact = Flist (nargs, args); | |
2855 | GCPRO1 (contact); | |
2856 | ||
bff3ed0a RS |
2857 | #ifdef WINDOWSNT |
2858 | /* Ensure socket support is loaded if available. */ | |
2859 | init_winsock (TRUE); | |
2860 | #endif | |
2861 | ||
9057ff80 KS |
2862 | /* :type TYPE (nil: stream, datagram */ |
2863 | tem = Fplist_get (contact, QCtype); | |
2864 | if (NILP (tem)) | |
2865 | socktype = SOCK_STREAM; | |
2866 | #ifdef DATAGRAM_SOCKETS | |
2867 | else if (EQ (tem, Qdatagram)) | |
2868 | socktype = SOCK_DGRAM; | |
f00c449b SM |
2869 | #endif |
2870 | #ifdef HAVE_SEQPACKET | |
2871 | else if (EQ (tem, Qseqpacket)) | |
2872 | socktype = SOCK_SEQPACKET; | |
e690ca94 | 2873 | #endif |
9057ff80 KS |
2874 | else |
2875 | error ("Unsupported connection type"); | |
e690ca94 KS |
2876 | |
2877 | /* :server BOOL */ | |
2878 | tem = Fplist_get (contact, QCserver); | |
2879 | if (!NILP (tem)) | |
2880 | { | |
75728599 JR |
2881 | /* Don't support network sockets when non-blocking mode is |
2882 | not available, since a blocked Emacs is not useful. */ | |
e690ca94 | 2883 | is_server = 1; |
d311d28c | 2884 | if (TYPE_RANGED_INTEGERP (int, tem)) |
6b61353c | 2885 | backlog = XINT (tem); |
e690ca94 KS |
2886 | } |
2887 | ||
2888 | /* Make QCaddress an alias for :local (server) or :remote (client). */ | |
2889 | QCaddress = is_server ? QClocal : QCremote; | |
2890 | ||
d7e344cd | 2891 | /* :nowait BOOL */ |
84b31826 | 2892 | if (!is_server && socktype != SOCK_DGRAM |
e690ca94 KS |
2893 | && (tem = Fplist_get (contact, QCnowait), !NILP (tem))) |
2894 | { | |
2895 | #ifndef NON_BLOCKING_CONNECT | |
2896 | error ("Non-blocking connect not supported"); | |
2897 | #else | |
2898 | is_non_blocking_client = 1; | |
2899 | #endif | |
2900 | } | |
2901 | ||
2902 | name = Fplist_get (contact, QCname); | |
2903 | buffer = Fplist_get (contact, QCbuffer); | |
2904 | filter = Fplist_get (contact, QCfilter); | |
2905 | sentinel = Fplist_get (contact, QCsentinel); | |
2906 | ||
b7826503 | 2907 | CHECK_STRING (name); |
e690ca94 | 2908 | |
e690ca94 KS |
2909 | /* Initialize addrinfo structure in case we don't use getaddrinfo. */ |
2910 | ai.ai_socktype = socktype; | |
2911 | ai.ai_protocol = 0; | |
2912 | ai.ai_next = NULL; | |
2913 | res = &ai; | |
a319f7c1 | 2914 | |
e690ca94 KS |
2915 | /* :local ADDRESS or :remote ADDRESS */ |
2916 | address = Fplist_get (contact, QCaddress); | |
2917 | if (!NILP (address)) | |
a319f7c1 | 2918 | { |
e690ca94 KS |
2919 | host = service = Qnil; |
2920 | ||
2921 | if (!(ai.ai_addrlen = get_lisp_to_sockaddr_size (address, &family))) | |
2922 | error ("Malformed :address"); | |
2923 | ai.ai_family = family; | |
2924 | ai.ai_addr = alloca (ai.ai_addrlen); | |
2925 | conv_lisp_to_sockaddr (family, address, ai.ai_addr, ai.ai_addrlen); | |
2926 | goto open_socket; | |
a319f7c1 | 2927 | } |
e690ca94 KS |
2928 | |
2929 | /* :family FAMILY -- nil (for Inet), local, or integer. */ | |
2930 | tem = Fplist_get (contact, QCfamily); | |
e1652a86 | 2931 | if (NILP (tem)) |
a319f7c1 | 2932 | { |
5e617bc2 | 2933 | #if defined (HAVE_GETADDRINFO) && defined (AF_INET6) |
e1652a86 KS |
2934 | family = AF_UNSPEC; |
2935 | #else | |
2936 | family = AF_INET; | |
e690ca94 | 2937 | #endif |
a319f7c1 | 2938 | } |
e1652a86 KS |
2939 | #ifdef HAVE_LOCAL_SOCKETS |
2940 | else if (EQ (tem, Qlocal)) | |
2941 | family = AF_LOCAL; | |
2942 | #endif | |
2943 | #ifdef AF_INET6 | |
2944 | else if (EQ (tem, Qipv6)) | |
2945 | family = AF_INET6; | |
2946 | #endif | |
2947 | else if (EQ (tem, Qipv4)) | |
2948 | family = AF_INET; | |
d311d28c | 2949 | else if (TYPE_RANGED_INTEGERP (int, tem)) |
e1652a86 KS |
2950 | family = XINT (tem); |
2951 | else | |
e690ca94 | 2952 | error ("Unknown address family"); |
e1652a86 | 2953 | |
e690ca94 KS |
2954 | ai.ai_family = family; |
2955 | ||
2956 | /* :service SERVICE -- string, integer (port number), or t (random port). */ | |
2957 | service = Fplist_get (contact, QCservice); | |
2958 | ||
66a9f7f4 SM |
2959 | /* :host HOST -- hostname, ip address, or 'local for localhost. */ |
2960 | host = Fplist_get (contact, QChost); | |
2961 | if (!NILP (host)) | |
2962 | { | |
2963 | if (EQ (host, Qlocal)) | |
ba3033ee JB |
2964 | /* Depending on setup, "localhost" may map to different IPv4 and/or |
2965 | IPv6 addresses, so it's better to be explicit. (Bug#6781) */ | |
2966 | host = build_string ("127.0.0.1"); | |
66a9f7f4 SM |
2967 | CHECK_STRING (host); |
2968 | } | |
2969 | ||
e690ca94 KS |
2970 | #ifdef HAVE_LOCAL_SOCKETS |
2971 | if (family == AF_LOCAL) | |
d0d6b7c5 | 2972 | { |
66a9f7f4 SM |
2973 | if (!NILP (host)) |
2974 | { | |
2975 | message (":family local ignores the :host \"%s\" property", | |
2976 | SDATA (host)); | |
2977 | contact = Fplist_put (contact, QChost, Qnil); | |
2978 | host = Qnil; | |
2979 | } | |
b7826503 | 2980 | CHECK_STRING (service); |
72af86bd | 2981 | memset (&address_un, 0, sizeof address_un); |
e690ca94 | 2982 | address_un.sun_family = AF_LOCAL; |
e99a530f PE |
2983 | if (sizeof address_un.sun_path <= SBYTES (service)) |
2984 | error ("Service name too long"); | |
2985 | strcpy (address_un.sun_path, SSDATA (service)); | |
e690ca94 KS |
2986 | ai.ai_addr = (struct sockaddr *) &address_un; |
2987 | ai.ai_addrlen = sizeof address_un; | |
2988 | goto open_socket; | |
d0d6b7c5 | 2989 | } |
e690ca94 | 2990 | #endif |
a319f7c1 | 2991 | |
798b64bb KH |
2992 | /* Slow down polling to every ten seconds. |
2993 | Some kernels have a bug which causes retrying connect to fail | |
2994 | after a connect. Polling can interfere with gethostbyname too. */ | |
2995 | #ifdef POLL_FOR_INPUT | |
84b31826 | 2996 | if (socktype != SOCK_DGRAM) |
e690ca94 | 2997 | { |
27e498e6 | 2998 | record_unwind_protect_void (run_all_atimers); |
e690ca94 KS |
2999 | bind_polling_period (10); |
3000 | } | |
798b64bb KH |
3001 | #endif |
3002 | ||
a319f7c1 | 3003 | #ifdef HAVE_GETADDRINFO |
e690ca94 KS |
3004 | /* If we have a host, use getaddrinfo to resolve both host and service. |
3005 | Otherwise, use getservbyname to lookup the service. */ | |
3006 | if (!NILP (host)) | |
3007 | { | |
3008 | ||
3009 | /* SERVICE can either be a string or int. | |
3010 | Convert to a C string for later use by getaddrinfo. */ | |
3011 | if (EQ (service, Qt)) | |
3012 | portstring = "0"; | |
3013 | else if (INTEGERP (service)) | |
3014 | { | |
c2982e87 | 3015 | sprintf (portbuf, "%"pI"d", XINT (service)); |
e690ca94 KS |
3016 | portstring = portbuf; |
3017 | } | |
3018 | else | |
3019 | { | |
3020 | CHECK_STRING (service); | |
42a5b22f | 3021 | portstring = SSDATA (service); |
e690ca94 KS |
3022 | } |
3023 | ||
3024 | immediate_quit = 1; | |
3025 | QUIT; | |
3026 | memset (&hints, 0, sizeof (hints)); | |
3027 | hints.ai_flags = 0; | |
e1652a86 | 3028 | hints.ai_family = family; |
e690ca94 KS |
3029 | hints.ai_socktype = socktype; |
3030 | hints.ai_protocol = 0; | |
08116b48 CY |
3031 | |
3032 | #ifdef HAVE_RES_INIT | |
3033 | res_init (); | |
3034 | #endif | |
3035 | ||
42a5b22f | 3036 | ret = getaddrinfo (SSDATA (host), portstring, &hints, &res); |
e690ca94 | 3037 | if (ret) |
f6270f62 | 3038 | #ifdef HAVE_GAI_STRERROR |
42a5b22f | 3039 | error ("%s/%s %s", SSDATA (host), portstring, gai_strerror (ret)); |
f6270f62 | 3040 | #else |
42a5b22f | 3041 | error ("%s/%s getaddrinfo error %d", SSDATA (host), portstring, ret); |
f6270f62 | 3042 | #endif |
e690ca94 | 3043 | immediate_quit = 0; |
a319f7c1 | 3044 | |
e690ca94 KS |
3045 | goto open_socket; |
3046 | } | |
3047 | #endif /* HAVE_GETADDRINFO */ | |
a319f7c1 | 3048 | |
e690ca94 KS |
3049 | /* We end up here if getaddrinfo is not defined, or in case no hostname |
3050 | has been specified (e.g. for a local server process). */ | |
3051 | ||
3052 | if (EQ (service, Qt)) | |
3053 | port = 0; | |
3054 | else if (INTEGERP (service)) | |
3055 | port = htons ((unsigned short) XINT (service)); | |
3056 | else | |
616da37c | 3057 | { |
e690ca94 KS |
3058 | struct servent *svc_info; |
3059 | CHECK_STRING (service); | |
42a5b22f | 3060 | svc_info = getservbyname (SSDATA (service), |
e690ca94 KS |
3061 | (socktype == SOCK_DGRAM ? "udp" : "tcp")); |
3062 | if (svc_info == 0) | |
d5db4077 | 3063 | error ("Unknown service: %s", SDATA (service)); |
e690ca94 KS |
3064 | port = svc_info->s_port; |
3065 | } | |
3066 | ||
72af86bd | 3067 | memset (&address_in, 0, sizeof address_in); |
e690ca94 KS |
3068 | address_in.sin_family = family; |
3069 | address_in.sin_addr.s_addr = INADDR_ANY; | |
3070 | address_in.sin_port = port; | |
3071 | ||
3072 | #ifndef HAVE_GETADDRINFO | |
3073 | if (!NILP (host)) | |
3074 | { | |
3075 | struct hostent *host_info_ptr; | |
3076 | ||
c5e87d10 | 3077 | /* gethostbyname may fail with TRY_AGAIN, but we don't honor that, |
f3d9532b | 3078 | as it may `hang' Emacs for a very long time. */ |
5d6c2aa3 RS |
3079 | immediate_quit = 1; |
3080 | QUIT; | |
08116b48 CY |
3081 | |
3082 | #ifdef HAVE_RES_INIT | |
3083 | res_init (); | |
3084 | #endif | |
3085 | ||
d5db4077 | 3086 | host_info_ptr = gethostbyname (SDATA (host)); |
5d6c2aa3 | 3087 | immediate_quit = 0; |
177c0ea7 | 3088 | |
e690ca94 KS |
3089 | if (host_info_ptr) |
3090 | { | |
72af86bd AS |
3091 | memcpy (&address_in.sin_addr, host_info_ptr->h_addr, |
3092 | host_info_ptr->h_length); | |
e690ca94 KS |
3093 | family = host_info_ptr->h_addrtype; |
3094 | address_in.sin_family = family; | |
3095 | } | |
3096 | else | |
3097 | /* Attempt to interpret host as numeric inet address */ | |
3098 | { | |
4624371d | 3099 | unsigned long numeric_addr; |
51b59d79 | 3100 | numeric_addr = inet_addr (SSDATA (host)); |
4624371d | 3101 | if (numeric_addr == -1) |
d5db4077 | 3102 | error ("Unknown host \"%s\"", SDATA (host)); |
d0d6b7c5 | 3103 | |
72af86bd AS |
3104 | memcpy (&address_in.sin_addr, &numeric_addr, |
3105 | sizeof (address_in.sin_addr)); | |
e690ca94 | 3106 | } |
d0d6b7c5 | 3107 | |
e690ca94 | 3108 | } |
dd2a17ab | 3109 | #endif /* not HAVE_GETADDRINFO */ |
d0d6b7c5 | 3110 | |
e690ca94 KS |
3111 | ai.ai_family = family; |
3112 | ai.ai_addr = (struct sockaddr *) &address_in; | |
3113 | ai.ai_addrlen = sizeof address_in; | |
3114 | ||
3115 | open_socket: | |
3116 | ||
dd2a17ab | 3117 | /* Do this in case we never enter the for-loop below. */ |
aed13378 | 3118 | count1 = SPECPDL_INDEX (); |
dd2a17ab | 3119 | s = -1; |
457a9bee | 3120 | |
dd2a17ab KS |
3121 | for (lres = res; lres; lres = lres->ai_next) |
3122 | { | |
f66c7cf8 | 3123 | ptrdiff_t optn; |
c5101a77 | 3124 | int optbits; |
6b61353c | 3125 | |
4dc343ee | 3126 | #ifdef WINDOWSNT |
649dbf36 | 3127 | retry_connect: |
4dc343ee | 3128 | #endif |
649dbf36 | 3129 | |
067428c1 PE |
3130 | s = socket (lres->ai_family, lres->ai_socktype | SOCK_CLOEXEC, |
3131 | lres->ai_protocol); | |
dd2a17ab KS |
3132 | if (s < 0) |
3133 | { | |
3134 | xerrno = errno; | |
3135 | continue; | |
3136 | } | |
0f2ee0c1 | 3137 | |
e690ca94 KS |
3138 | #ifdef DATAGRAM_SOCKETS |
3139 | if (!is_server && socktype == SOCK_DGRAM) | |
3140 | break; | |
3141 | #endif /* DATAGRAM_SOCKETS */ | |
3142 | ||
dd2a17ab | 3143 | #ifdef NON_BLOCKING_CONNECT |
e690ca94 | 3144 | if (is_non_blocking_client) |
dd2a17ab | 3145 | { |
dd2a17ab | 3146 | ret = fcntl (s, F_SETFL, O_NONBLOCK); |
dd2a17ab KS |
3147 | if (ret < 0) |
3148 | { | |
3149 | xerrno = errno; | |
3150 | emacs_close (s); | |
3151 | s = -1; | |
3152 | continue; | |
3153 | } | |
3154 | } | |
3155 | #endif | |
177c0ea7 | 3156 | |
dd2a17ab | 3157 | /* Make us close S if quit. */ |
27e498e6 | 3158 | record_unwind_protect_int (close_file_unwind, s); |
dd2a17ab | 3159 | |
6b61353c KH |
3160 | /* Parse network options in the arg list. |
3161 | We simply ignore anything which isn't a known option (including other keywords). | |
63136da6 | 3162 | An error is signaled if setting a known option fails. */ |
6b61353c KH |
3163 | for (optn = optbits = 0; optn < nargs-1; optn += 2) |
3164 | optbits |= set_socket_option (s, args[optn], args[optn+1]); | |
3165 | ||
e690ca94 KS |
3166 | if (is_server) |
3167 | { | |
3168 | /* Configure as a server socket. */ | |
6b61353c KH |
3169 | |
3170 | /* SO_REUSEADDR = 1 is default for server sockets; must specify | |
3171 | explicit :reuseaddr key to override this. */ | |
e690ca94 KS |
3172 | #ifdef HAVE_LOCAL_SOCKETS |
3173 | if (family != AF_LOCAL) | |
3174 | #endif | |
6b61353c KH |
3175 | if (!(optbits & (1 << OPIX_REUSEADDR))) |
3176 | { | |
3177 | int optval = 1; | |
3178 | if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)) | |
3179 | report_file_error ("Cannot set reuse option on server socket", Qnil); | |
3180 | } | |
177c0ea7 | 3181 | |
e690ca94 KS |
3182 | if (bind (s, lres->ai_addr, lres->ai_addrlen)) |
3183 | report_file_error ("Cannot bind server socket", Qnil); | |
3184 | ||
3185 | #ifdef HAVE_GETSOCKNAME | |
3186 | if (EQ (service, Qt)) | |
3187 | { | |
3188 | struct sockaddr_in sa1; | |
401bf9b4 | 3189 | socklen_t len1 = sizeof (sa1); |
e690ca94 KS |
3190 | if (getsockname (s, (struct sockaddr *)&sa1, &len1) == 0) |
3191 | { | |
3192 | ((struct sockaddr_in *)(lres->ai_addr))->sin_port = sa1.sin_port; | |
21bd170d | 3193 | service = make_number (ntohs (sa1.sin_port)); |
e690ca94 KS |
3194 | contact = Fplist_put (contact, QCservice, service); |
3195 | } | |
3196 | } | |
3197 | #endif | |
3198 | ||
84b31826 | 3199 | if (socktype != SOCK_DGRAM && listen (s, backlog)) |
e690ca94 KS |
3200 | report_file_error ("Cannot listen on server socket", Qnil); |
3201 | ||
3202 | break; | |
3203 | } | |
3204 | ||
dd2a17ab KS |
3205 | immediate_quit = 1; |
3206 | QUIT; | |
3207 | ||
dd2a17ab KS |
3208 | ret = connect (s, lres->ai_addr, lres->ai_addrlen); |
3209 | xerrno = errno; | |
3210 | ||
dd2a17ab KS |
3211 | if (ret == 0 || xerrno == EISCONN) |
3212 | { | |
dd2a17ab KS |
3213 | /* The unwind-protect will be discarded afterwards. |
3214 | Likewise for immediate_quit. */ | |
3215 | break; | |
3216 | } | |
3217 | ||
3218 | #ifdef NON_BLOCKING_CONNECT | |
3219 | #ifdef EINPROGRESS | |
e690ca94 | 3220 | if (is_non_blocking_client && xerrno == EINPROGRESS) |
dd2a17ab KS |
3221 | break; |
3222 | #else | |
3223 | #ifdef EWOULDBLOCK | |
e690ca94 | 3224 | if (is_non_blocking_client && xerrno == EWOULDBLOCK) |
dd2a17ab KS |
3225 | break; |
3226 | #endif | |
3227 | #endif | |
3228 | #endif | |
649dbf36 CY |
3229 | |
3230 | #ifndef WINDOWSNT | |
cd591dbc HE |
3231 | if (xerrno == EINTR) |
3232 | { | |
3233 | /* Unlike most other syscalls connect() cannot be called | |
3234 | again. (That would return EALREADY.) The proper way to | |
d35af63c | 3235 | wait for completion is pselect(). */ |
401bf9b4 PE |
3236 | int sc; |
3237 | socklen_t len; | |
d486344e | 3238 | fd_set fdset; |
cd591dbc HE |
3239 | retry_select: |
3240 | FD_ZERO (&fdset); | |
3241 | FD_SET (s, &fdset); | |
3242 | QUIT; | |
d35af63c | 3243 | sc = pselect (s + 1, NULL, &fdset, NULL, NULL, NULL); |
cd591dbc HE |
3244 | if (sc == -1) |
3245 | { | |
649dbf36 | 3246 | if (errno == EINTR) |
cd591dbc | 3247 | goto retry_select; |
649dbf36 | 3248 | else |
4e604a5d | 3249 | report_file_error ("Failed select", Qnil); |
cd591dbc HE |
3250 | } |
3251 | eassert (sc > 0); | |
649dbf36 CY |
3252 | |
3253 | len = sizeof xerrno; | |
3254 | eassert (FD_ISSET (s, &fdset)); | |
a773ed9a | 3255 | if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) < 0) |
4e604a5d | 3256 | report_file_error ("Failed getsockopt", Qnil); |
649dbf36 | 3257 | if (xerrno) |
4e604a5d | 3258 | report_file_errno ("Failed connect", Qnil, xerrno); |
a773ed9a | 3259 | break; |
cd591dbc | 3260 | } |
649dbf36 | 3261 | #endif /* !WINDOWSNT */ |
e333e864 | 3262 | |
0f2ee0c1 RS |
3263 | immediate_quit = 0; |
3264 | ||
dd2a17ab | 3265 | /* Discard the unwind protect closing S. */ |
5684cd6e | 3266 | specpdl_ptr = specpdl + count1; |
68c45bf0 | 3267 | emacs_close (s); |
dd2a17ab | 3268 | s = -1; |
649dbf36 CY |
3269 | |
3270 | #ifdef WINDOWSNT | |
3271 | if (xerrno == EINTR) | |
3272 | goto retry_connect; | |
3273 | #endif | |
dd2a17ab | 3274 | } |
457a9bee | 3275 | |
e690ca94 KS |
3276 | if (s >= 0) |
3277 | { | |
3278 | #ifdef DATAGRAM_SOCKETS | |
3279 | if (socktype == SOCK_DGRAM) | |
3280 | { | |
3281 | if (datagram_address[s].sa) | |
1088b922 | 3282 | emacs_abort (); |
23f86fce | 3283 | datagram_address[s].sa = xmalloc (lres->ai_addrlen); |
e690ca94 KS |
3284 | datagram_address[s].len = lres->ai_addrlen; |
3285 | if (is_server) | |
3286 | { | |
3287 | Lisp_Object remote; | |
72af86bd | 3288 | memset (datagram_address[s].sa, 0, lres->ai_addrlen); |
e690ca94 KS |
3289 | if (remote = Fplist_get (contact, QCremote), !NILP (remote)) |
3290 | { | |
3291 | int rfamily, rlen; | |
3292 | rlen = get_lisp_to_sockaddr_size (remote, &rfamily); | |
a9ebfa0b PE |
3293 | if (rlen != 0 && rfamily == lres->ai_family |
3294 | && rlen == lres->ai_addrlen) | |
e690ca94 KS |
3295 | conv_lisp_to_sockaddr (rfamily, remote, |
3296 | datagram_address[s].sa, rlen); | |
3297 | } | |
3298 | } | |
3299 | else | |
72af86bd | 3300 | memcpy (datagram_address[s].sa, lres->ai_addr, lres->ai_addrlen); |
e690ca94 KS |
3301 | } |
3302 | #endif | |
177c0ea7 | 3303 | contact = Fplist_put (contact, QCaddress, |
e690ca94 | 3304 | conv_sockaddr_to_lisp (lres->ai_addr, lres->ai_addrlen)); |
2185db04 KS |
3305 | #ifdef HAVE_GETSOCKNAME |
3306 | if (!is_server) | |
3307 | { | |
3308 | struct sockaddr_in sa1; | |
401bf9b4 | 3309 | socklen_t len1 = sizeof (sa1); |
2185db04 KS |
3310 | if (getsockname (s, (struct sockaddr *)&sa1, &len1) == 0) |
3311 | contact = Fplist_put (contact, QClocal, | |
d3da34e0 | 3312 | conv_sockaddr_to_lisp ((struct sockaddr *)&sa1, len1)); |
2185db04 KS |
3313 | } |
3314 | #endif | |
e690ca94 KS |
3315 | } |
3316 | ||
54b3b62f YM |
3317 | immediate_quit = 0; |
3318 | ||
dd2a17ab | 3319 | #ifdef HAVE_GETADDRINFO |
e690ca94 | 3320 | if (res != &ai) |
54b3b62f | 3321 | { |
4d7e6e51 | 3322 | block_input (); |
54b3b62f | 3323 | freeaddrinfo (res); |
4d7e6e51 | 3324 | unblock_input (); |
54b3b62f | 3325 | } |
dd2a17ab KS |
3326 | #endif |
3327 | ||
3328 | if (s < 0) | |
3329 | { | |
dd2a17ab KS |
3330 | /* If non-blocking got this far - and failed - assume non-blocking is |
3331 | not supported after all. This is probably a wrong assumption, but | |
e690ca94 KS |
3332 | the normal blocking calls to open-network-stream handles this error |
3333 | better. */ | |
3334 | if (is_non_blocking_client) | |
dd2a17ab | 3335 | return Qnil; |
dd2a17ab | 3336 | |
a773ed9a PE |
3337 | report_file_errno ((is_server |
3338 | ? "make server process failed" | |
3339 | : "make client process failed"), | |
3340 | contact, xerrno); | |
d0d6b7c5 | 3341 | } |
44ade2e9 | 3342 | |
d0d6b7c5 | 3343 | inch = s; |
59f23005 | 3344 | outch = s; |
d0d6b7c5 JB |
3345 | |
3346 | if (!NILP (buffer)) | |
3347 | buffer = Fget_buffer_create (buffer); | |
3348 | proc = make_process (name); | |
3349 | ||
3350 | chan_process[inch] = proc; | |
3351 | ||
d0d6b7c5 | 3352 | fcntl (inch, F_SETFL, O_NONBLOCK); |
d0d6b7c5 | 3353 | |
e690ca94 KS |
3354 | p = XPROCESS (proc); |
3355 | ||
6a09a33b PE |
3356 | pset_childp (p, contact); |
3357 | pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist))); | |
3358 | pset_type (p, Qnetwork); | |
177c0ea7 | 3359 | |
6a09a33b PE |
3360 | pset_buffer (p, buffer); |
3361 | pset_sentinel (p, sentinel); | |
3362 | pset_filter (p, filter); | |
3363 | pset_log (p, Fplist_get (contact, QClog)); | |
e690ca94 | 3364 | if (tem = Fplist_get (contact, QCnoquery), !NILP (tem)) |
60f0fb11 | 3365 | p->kill_without_query = 1; |
e690ca94 | 3366 | if ((tem = Fplist_get (contact, QCstop), !NILP (tem))) |
6a09a33b | 3367 | pset_command (p, Qt); |
6bfd98e7 | 3368 | p->pid = 0; |
94fcd171 PE |
3369 | |
3370 | p->open_fd[SUBPROCESS_STDIN] = inch; | |
60f0fb11 SM |
3371 | p->infd = inch; |
3372 | p->outfd = outch; | |
94fcd171 PE |
3373 | |
3374 | /* Discard the unwind protect for closing S, if any. */ | |
3375 | specpdl_ptr = specpdl + count1; | |
3376 | ||
3377 | /* Unwind bind_polling_period and request_sigio. */ | |
3378 | unbind_to (count, Qnil); | |
3379 | ||
84b31826 | 3380 | if (is_server && socktype != SOCK_DGRAM) |
6a09a33b | 3381 | pset_status (p, Qlisten); |
dd2a17ab | 3382 | |
f523e0c3 SM |
3383 | /* Make the process marker point into the process buffer (if any). */ |
3384 | if (BUFFERP (buffer)) | |
4d2b044c | 3385 | set_marker_both (p->mark, buffer, |
f523e0c3 SM |
3386 | BUF_ZV (XBUFFER (buffer)), |
3387 | BUF_ZV_BYTE (XBUFFER (buffer))); | |
3388 | ||
dd2a17ab | 3389 | #ifdef NON_BLOCKING_CONNECT |
e690ca94 | 3390 | if (is_non_blocking_client) |
dd2a17ab KS |
3391 | { |
3392 | /* We may get here if connect did succeed immediately. However, | |
3393 | in that case, we still need to signal this like a non-blocking | |
3394 | connection. */ | |
6a09a33b | 3395 | pset_status (p, Qconnect); |
dd2a17ab KS |
3396 | if (!FD_ISSET (inch, &connect_wait_mask)) |
3397 | { | |
3398 | FD_SET (inch, &connect_wait_mask); | |
3fad2ad2 | 3399 | FD_SET (inch, &write_mask); |
dd2a17ab KS |
3400 | num_pending_connects++; |
3401 | } | |
3402 | } | |
3403 | else | |
3404 | #endif | |
e690ca94 KS |
3405 | /* A server may have a client filter setting of Qt, but it must |
3406 | still listen for incoming connects unless it is stopped. */ | |
4d2b044c DA |
3407 | if ((!EQ (p->filter, Qt) && !EQ (p->command, Qt)) |
3408 | || (EQ (p->status, Qlisten) && NILP (p->command))) | |
dd2a17ab KS |
3409 | { |
3410 | FD_SET (inch, &input_wait_mask); | |
3411 | FD_SET (inch, &non_keyboard_wait_mask); | |
3412 | } | |
3413 | ||
7d0e672e RS |
3414 | if (inch > max_process_desc) |
3415 | max_process_desc = inch; | |
d0d6b7c5 | 3416 | |
e690ca94 KS |
3417 | tem = Fplist_member (contact, QCcoding); |
3418 | if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem)))) | |
3419 | tem = Qnil; /* No error message (too late!). */ | |
3420 | ||
67918941 RS |
3421 | { |
3422 | /* Setup coding systems for communicating with the network stream. */ | |
dbf31225 | 3423 | struct gcpro gcpro1; |
67918941 RS |
3424 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
3425 | Lisp_Object coding_systems = Qt; | |
b93aacde | 3426 | Lisp_Object fargs[5], val; |
67918941 | 3427 | |
e690ca94 | 3428 | if (!NILP (tem)) |
6b61353c KH |
3429 | { |
3430 | val = XCAR (XCDR (tem)); | |
3431 | if (CONSP (val)) | |
3432 | val = XCAR (val); | |
3433 | } | |
e690ca94 | 3434 | else if (!NILP (Vcoding_system_for_read)) |
67918941 | 3435 | val = Vcoding_system_for_read; |
4b4deea2 TT |
3436 | else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters))) |
3437 | || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))) | |
67918941 RS |
3438 | /* We dare not decode end-of-line format by setting VAL to |
3439 | Qraw_text, because the existing Emacs Lisp libraries | |
9858f6c3 | 3440 | assume that they receive bare code including a sequence of |
67918941 RS |
3441 | CR LF. */ |
3442 | val = Qnil; | |
3443 | else | |
3444 | { | |
991234f0 KS |
3445 | if (NILP (host) || NILP (service)) |
3446 | coding_systems = Qnil; | |
3447 | else | |
3448 | { | |
b93aacde PE |
3449 | fargs[0] = Qopen_network_stream, fargs[1] = name, |
3450 | fargs[2] = buffer, fargs[3] = host, fargs[4] = service; | |
dbf31225 | 3451 | GCPRO1 (proc); |
b93aacde | 3452 | coding_systems = Ffind_operation_coding_system (5, fargs); |
dbf31225 | 3453 | UNGCPRO; |
991234f0 | 3454 | } |
67918941 | 3455 | if (CONSP (coding_systems)) |
70949dac | 3456 | val = XCAR (coding_systems); |
67918941 | 3457 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 3458 | val = XCAR (Vdefault_process_coding_system); |
67918941 RS |
3459 | else |
3460 | val = Qnil; | |
3461 | } | |
6a09a33b | 3462 | pset_decode_coding_system (p, val); |
0fa1789e | 3463 | |
e690ca94 | 3464 | if (!NILP (tem)) |
6b61353c KH |
3465 | { |
3466 | val = XCAR (XCDR (tem)); | |
3467 | if (CONSP (val)) | |
3468 | val = XCDR (val); | |
3469 | } | |
e690ca94 | 3470 | else if (!NILP (Vcoding_system_for_write)) |
67918941 | 3471 | val = Vcoding_system_for_write; |
4b4deea2 | 3472 | else if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
67918941 RS |
3473 | val = Qnil; |
3474 | else | |
3475 | { | |
3476 | if (EQ (coding_systems, Qt)) | |
3477 | { | |
991234f0 KS |
3478 | if (NILP (host) || NILP (service)) |
3479 | coding_systems = Qnil; | |
3480 | else | |
3481 | { | |
b93aacde PE |
3482 | fargs[0] = Qopen_network_stream, fargs[1] = name, |
3483 | fargs[2] = buffer, fargs[3] = host, fargs[4] = service; | |
dbf31225 | 3484 | GCPRO1 (proc); |
b93aacde | 3485 | coding_systems = Ffind_operation_coding_system (5, fargs); |
dbf31225 | 3486 | UNGCPRO; |
991234f0 | 3487 | } |
67918941 RS |
3488 | } |
3489 | if (CONSP (coding_systems)) | |
70949dac | 3490 | val = XCDR (coding_systems); |
67918941 | 3491 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 3492 | val = XCDR (Vdefault_process_coding_system); |
67918941 RS |
3493 | else |
3494 | val = Qnil; | |
3495 | } | |
6a09a33b | 3496 | pset_encode_coding_system (p, val); |
67918941 | 3497 | } |
03f04413 | 3498 | setup_process_coding_systems (proc); |
0fa1789e | 3499 | |
6a09a33b | 3500 | pset_decoding_buf (p, empty_unibyte_string); |
60f0fb11 | 3501 | p->decoding_carryover = 0; |
6a09a33b | 3502 | pset_encoding_buf (p, empty_unibyte_string); |
0fa1789e | 3503 | |
e690ca94 | 3504 | p->inherit_coding_system_flag |
e6fca843 | 3505 | = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system); |
52a1b894 | 3506 | |
d0d6b7c5 JB |
3507 | UNGCPRO; |
3508 | return proc; | |
3509 | } | |
d0d6b7c5 | 3510 | |
6b61353c | 3511 | \f |
3d798ba7 | 3512 | #ifdef HAVE_NET_IF_H |
3dffe395 EZ |
3513 | |
3514 | #ifdef SIOCGIFCONF | |
3d798ba7 | 3515 | static Lisp_Object |
3dffe395 | 3516 | network_interface_list (void) |
6b61353c KH |
3517 | { |
3518 | struct ifconf ifconf; | |
377538cb JD |
3519 | struct ifreq *ifreq; |
3520 | void *buf = NULL; | |
70c60eb2 | 3521 | ptrdiff_t buf_size = 512; |
0bce5d9e | 3522 | int s; |
6b61353c | 3523 | Lisp_Object res; |
3f5bef16 | 3524 | ptrdiff_t count; |
6b61353c | 3525 | |
067428c1 | 3526 | s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
6b61353c KH |
3527 | if (s < 0) |
3528 | return Qnil; | |
3f5bef16 PE |
3529 | count = SPECPDL_INDEX (); |
3530 | record_unwind_protect_int (close_file_unwind, s); | |
6b61353c | 3531 | |
76667214 | 3532 | do |
6b61353c | 3533 | { |
76667214 PE |
3534 | buf = xpalloc (buf, &buf_size, 1, INT_MAX, 1); |
3535 | ifconf.ifc_buf = buf; | |
3536 | ifconf.ifc_len = buf_size; | |
156bffbe AS |
3537 | if (ioctl (s, SIOCGIFCONF, &ifconf)) |
3538 | { | |
bacba3c2 | 3539 | emacs_close (s); |
76667214 | 3540 | xfree (buf); |
156bffbe AS |
3541 | return Qnil; |
3542 | } | |
6b61353c | 3543 | } |
76667214 | 3544 | while (ifconf.ifc_len == buf_size); |
6b61353c | 3545 | |
3f5bef16 | 3546 | res = unbind_to (count, Qnil); |
156bffbe AS |
3547 | ifreq = ifconf.ifc_req; |
3548 | while ((char *) ifreq < (char *) ifconf.ifc_req + ifconf.ifc_len) | |
6b61353c | 3549 | { |
377538cb JD |
3550 | struct ifreq *ifq = ifreq; |
3551 | #ifdef HAVE_STRUCT_IFREQ_IFR_ADDR_SA_LEN | |
156bffbe AS |
3552 | #define SIZEOF_IFREQ(sif) \ |
3553 | ((sif)->ifr_addr.sa_len < sizeof (struct sockaddr) \ | |
3554 | ? sizeof (*(sif)) : sizeof ((sif)->ifr_name) + (sif)->ifr_addr.sa_len) | |
377538cb JD |
3555 | |
3556 | int len = SIZEOF_IFREQ (ifq); | |
3557 | #else | |
3558 | int len = sizeof (*ifreq); | |
3559 | #endif | |
6b61353c | 3560 | char namebuf[sizeof (ifq->ifr_name) + 1]; |
156bffbe | 3561 | ifreq = (struct ifreq *) ((char *) ifreq + len); |
377538cb | 3562 | |
6b61353c KH |
3563 | if (ifq->ifr_addr.sa_family != AF_INET) |
3564 | continue; | |
377538cb | 3565 | |
72af86bd | 3566 | memcpy (namebuf, ifq->ifr_name, sizeof (ifq->ifr_name)); |
6b61353c KH |
3567 | namebuf[sizeof (ifq->ifr_name)] = 0; |
3568 | res = Fcons (Fcons (build_string (namebuf), | |
3569 | conv_sockaddr_to_lisp (&ifq->ifr_addr, | |
3570 | sizeof (struct sockaddr))), | |
3571 | res); | |
3572 | } | |
3573 | ||
377538cb | 3574 | xfree (buf); |
6b61353c KH |
3575 | return res; |
3576 | } | |
3577 | #endif /* SIOCGIFCONF */ | |
3578 | ||
5e617bc2 | 3579 | #if defined (SIOCGIFADDR) || defined (SIOCGIFHWADDR) || defined (SIOCGIFFLAGS) |
6b61353c KH |
3580 | |
3581 | struct ifflag_def { | |
3582 | int flag_bit; | |
91433552 | 3583 | const char *flag_sym; |
6b61353c KH |
3584 | }; |
3585 | ||
91433552 | 3586 | static const struct ifflag_def ifflag_table[] = { |
6b61353c KH |
3587 | #ifdef IFF_UP |
3588 | { IFF_UP, "up" }, | |
3589 | #endif | |
3590 | #ifdef IFF_BROADCAST | |
3591 | { IFF_BROADCAST, "broadcast" }, | |
3592 | #endif | |
3593 | #ifdef IFF_DEBUG | |
3594 | { IFF_DEBUG, "debug" }, | |
3595 | #endif | |
3596 | #ifdef IFF_LOOPBACK | |
3597 | { IFF_LOOPBACK, "loopback" }, | |
3598 | #endif | |
3599 | #ifdef IFF_POINTOPOINT | |
3600 | { IFF_POINTOPOINT, "pointopoint" }, | |
3601 | #endif | |
3602 | #ifdef IFF_RUNNING | |
3603 | { IFF_RUNNING, "running" }, | |
3604 | #endif | |
3605 | #ifdef IFF_NOARP | |
3606 | { IFF_NOARP, "noarp" }, | |
3607 | #endif | |
3608 | #ifdef IFF_PROMISC | |
3609 | { IFF_PROMISC, "promisc" }, | |
3610 | #endif | |
3611 | #ifdef IFF_NOTRAILERS | |
377538cb JD |
3612 | #ifdef NS_IMPL_COCOA |
3613 | /* Really means smart, notrailers is obsolete */ | |
3614 | { IFF_NOTRAILERS, "smart" }, | |
3615 | #else | |
6b61353c KH |
3616 | { IFF_NOTRAILERS, "notrailers" }, |
3617 | #endif | |
377538cb | 3618 | #endif |
6b61353c KH |
3619 | #ifdef IFF_ALLMULTI |
3620 | { IFF_ALLMULTI, "allmulti" }, | |
3621 | #endif | |
3622 | #ifdef IFF_MASTER | |
3623 | { IFF_MASTER, "master" }, | |
3624 | #endif | |
3625 | #ifdef IFF_SLAVE | |
3626 | { IFF_SLAVE, "slave" }, | |
3627 | #endif | |
3628 | #ifdef IFF_MULTICAST | |
3629 | { IFF_MULTICAST, "multicast" }, | |
3630 | #endif | |
3631 | #ifdef IFF_PORTSEL | |
3632 | { IFF_PORTSEL, "portsel" }, | |
3633 | #endif | |
3634 | #ifdef IFF_AUTOMEDIA | |
3635 | { IFF_AUTOMEDIA, "automedia" }, | |
3636 | #endif | |
3637 | #ifdef IFF_DYNAMIC | |
3638 | { IFF_DYNAMIC, "dynamic" }, | |
e1652a86 | 3639 | #endif |
2026418c YM |
3640 | #ifdef IFF_OACTIVE |
3641 | { IFF_OACTIVE, "oactive" }, /* OpenBSD: transmission in progress */ | |
e1652a86 KS |
3642 | #endif |
3643 | #ifdef IFF_SIMPLEX | |
3644 | { IFF_SIMPLEX, "simplex" }, /* OpenBSD: can't hear own transmissions */ | |
3645 | #endif | |
3646 | #ifdef IFF_LINK0 | |
3647 | { IFF_LINK0, "link0" }, /* OpenBSD: per link layer defined bit */ | |
3648 | #endif | |
3649 | #ifdef IFF_LINK1 | |
3650 | { IFF_LINK1, "link1" }, /* OpenBSD: per link layer defined bit */ | |
3651 | #endif | |
3652 | #ifdef IFF_LINK2 | |
3653 | { IFF_LINK2, "link2" }, /* OpenBSD: per link layer defined bit */ | |
6b61353c KH |
3654 | #endif |
3655 | { 0, 0 } | |
3656 | }; | |
3657 | ||
3d798ba7 | 3658 | static Lisp_Object |
3dffe395 | 3659 | network_interface_info (Lisp_Object ifname) |
6b61353c KH |
3660 | { |
3661 | struct ifreq rq; | |
3662 | Lisp_Object res = Qnil; | |
3663 | Lisp_Object elt; | |
3664 | int s; | |
0bce5d9e | 3665 | bool any = 0; |
3f5bef16 | 3666 | ptrdiff_t count; |
0a0d27fb PE |
3667 | #if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR) \ |
3668 | && defined HAVE_GETIFADDRS && defined LLADDR) | |
377538cb JD |
3669 | struct ifaddrs *ifap; |
3670 | #endif | |
6b61353c KH |
3671 | |
3672 | CHECK_STRING (ifname); | |
3673 | ||
e99a530f PE |
3674 | if (sizeof rq.ifr_name <= SBYTES (ifname)) |
3675 | error ("interface name too long"); | |
3676 | strcpy (rq.ifr_name, SSDATA (ifname)); | |
6b61353c | 3677 | |
067428c1 | 3678 | s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); |
6b61353c KH |
3679 | if (s < 0) |
3680 | return Qnil; | |
3f5bef16 PE |
3681 | count = SPECPDL_INDEX (); |
3682 | record_unwind_protect_int (close_file_unwind, s); | |
6b61353c KH |
3683 | |
3684 | elt = Qnil; | |
5e617bc2 | 3685 | #if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS) |
6b61353c KH |
3686 | if (ioctl (s, SIOCGIFFLAGS, &rq) == 0) |
3687 | { | |
3688 | int flags = rq.ifr_flags; | |
91433552 | 3689 | const struct ifflag_def *fp; |
6b61353c KH |
3690 | int fnum; |
3691 | ||
377538cb JD |
3692 | /* If flags is smaller than int (i.e. short) it may have the high bit set |
3693 | due to IFF_MULTICAST. In that case, sign extending it into | |
3694 | an int is wrong. */ | |
3695 | if (flags < 0 && sizeof (rq.ifr_flags) < sizeof (flags)) | |
3696 | flags = (unsigned short) rq.ifr_flags; | |
3697 | ||
bc57d757 | 3698 | any = 1; |
82a80058 | 3699 | for (fp = ifflag_table; flags != 0 && fp->flag_sym; fp++) |
6b61353c KH |
3700 | { |
3701 | if (flags & fp->flag_bit) | |
3702 | { | |
3703 | elt = Fcons (intern (fp->flag_sym), elt); | |
3704 | flags -= fp->flag_bit; | |
3705 | } | |
3706 | } | |
fa3c87e1 | 3707 | for (fnum = 0; flags && fnum < 32; flags >>= 1, fnum++) |
6b61353c | 3708 | { |
fa3c87e1 | 3709 | if (flags & 1) |
6b61353c KH |
3710 | { |
3711 | elt = Fcons (make_number (fnum), elt); | |
3712 | } | |
3713 | } | |
3714 | } | |
3715 | #endif | |
3716 | res = Fcons (elt, res); | |
3717 | ||
3718 | elt = Qnil; | |
5e617bc2 | 3719 | #if defined (SIOCGIFHWADDR) && defined (HAVE_STRUCT_IFREQ_IFR_HWADDR) |
6b61353c KH |
3720 | if (ioctl (s, SIOCGIFHWADDR, &rq) == 0) |
3721 | { | |
3722 | Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil); | |
3723 | register struct Lisp_Vector *p = XVECTOR (hwaddr); | |
3724 | int n; | |
3725 | ||
bc57d757 | 3726 | any = 1; |
6b61353c | 3727 | for (n = 0; n < 6; n++) |
91f2d272 PE |
3728 | p->contents[n] = make_number (((unsigned char *) |
3729 | &rq.ifr_hwaddr.sa_data[0]) | |
3730 | [n]); | |
6b61353c KH |
3731 | elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr); |
3732 | } | |
5e617bc2 | 3733 | #elif defined (HAVE_GETIFADDRS) && defined (LLADDR) |
377538cb JD |
3734 | if (getifaddrs (&ifap) != -1) |
3735 | { | |
3736 | Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil); | |
3737 | register struct Lisp_Vector *p = XVECTOR (hwaddr); | |
3738 | struct ifaddrs *it; | |
3739 | ||
3740 | for (it = ifap; it != NULL; it = it->ifa_next) | |
3741 | { | |
3742 | struct sockaddr_dl *sdl = (struct sockaddr_dl*) it->ifa_addr; | |
3743 | unsigned char linkaddr[6]; | |
3744 | int n; | |
3745 | ||
3746 | if (it->ifa_addr->sa_family != AF_LINK | |
3747 | || strcmp (it->ifa_name, SSDATA (ifname)) != 0 | |
3748 | || sdl->sdl_alen != 6) | |
3749 | continue; | |
3750 | ||
5e617bc2 | 3751 | memcpy (linkaddr, LLADDR (sdl), sdl->sdl_alen); |
377538cb | 3752 | for (n = 0; n < 6; n++) |
91f2d272 | 3753 | p->contents[n] = make_number (linkaddr[n]); |
377538cb JD |
3754 | |
3755 | elt = Fcons (make_number (it->ifa_addr->sa_family), hwaddr); | |
3756 | break; | |
3757 | } | |
3758 | } | |
3759 | #ifdef HAVE_FREEIFADDRS | |
3760 | freeifaddrs (ifap); | |
6b61353c | 3761 | #endif |
377538cb JD |
3762 | |
3763 | #endif /* HAVE_GETIFADDRS && LLADDR */ | |
3764 | ||
6b61353c KH |
3765 | res = Fcons (elt, res); |
3766 | ||
3767 | elt = Qnil; | |
5e617bc2 | 3768 | #if defined (SIOCGIFNETMASK) && (defined (HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined (HAVE_STRUCT_IFREQ_IFR_ADDR)) |
6b61353c KH |
3769 | if (ioctl (s, SIOCGIFNETMASK, &rq) == 0) |
3770 | { | |
bc57d757 | 3771 | any = 1; |
2026418c | 3772 | #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK |
6b61353c | 3773 | elt = conv_sockaddr_to_lisp (&rq.ifr_netmask, sizeof (rq.ifr_netmask)); |
2026418c YM |
3774 | #else |
3775 | elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr)); | |
3776 | #endif | |
6b61353c KH |
3777 | } |
3778 | #endif | |
3779 | res = Fcons (elt, res); | |
3780 | ||
3781 | elt = Qnil; | |
5e617bc2 | 3782 | #if defined (SIOCGIFBRDADDR) && defined (HAVE_STRUCT_IFREQ_IFR_BROADADDR) |
6b61353c KH |
3783 | if (ioctl (s, SIOCGIFBRDADDR, &rq) == 0) |
3784 | { | |
bc57d757 | 3785 | any = 1; |
6b61353c KH |
3786 | elt = conv_sockaddr_to_lisp (&rq.ifr_broadaddr, sizeof (rq.ifr_broadaddr)); |
3787 | } | |
3788 | #endif | |
3789 | res = Fcons (elt, res); | |
3790 | ||
3791 | elt = Qnil; | |
5e617bc2 | 3792 | #if defined (SIOCGIFADDR) && defined (HAVE_STRUCT_IFREQ_IFR_ADDR) |
6b61353c KH |
3793 | if (ioctl (s, SIOCGIFADDR, &rq) == 0) |
3794 | { | |
bc57d757 | 3795 | any = 1; |
6b61353c KH |
3796 | elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr)); |
3797 | } | |
3798 | #endif | |
3799 | res = Fcons (elt, res); | |
3800 | ||
3f5bef16 | 3801 | return unbind_to (count, any ? res : Qnil); |
6b61353c | 3802 | } |
3dffe395 | 3803 | #endif /* !SIOCGIFADDR && !SIOCGIFHWADDR && !SIOCGIFFLAGS */ |
5e617bc2 | 3804 | #endif /* defined (HAVE_NET_IF_H) */ |
6b61353c | 3805 | |
3d798ba7 PE |
3806 | DEFUN ("network-interface-list", Fnetwork_interface_list, |
3807 | Snetwork_interface_list, 0, 0, 0, | |
3808 | doc: /* Return an alist of all network interfaces and their network address. | |
3809 | Each element is a cons, the car of which is a string containing the | |
3810 | interface name, and the cdr is the network address in internal | |
3811 | format; see the description of ADDRESS in `make-network-process'. | |
3812 | ||
3813 | If the information is not available, return nil. */) | |
3814 | (void) | |
3815 | { | |
3816 | #if (defined HAVE_NET_IF_H && defined SIOCGIFCONF) || defined WINDOWSNT | |
3817 | return network_interface_list (); | |
3818 | #else | |
3819 | return Qnil; | |
3820 | #endif | |
3821 | } | |
3822 | ||
3823 | DEFUN ("network-interface-info", Fnetwork_interface_info, | |
3824 | Snetwork_interface_info, 1, 1, 0, | |
3825 | doc: /* Return information about network interface named IFNAME. | |
3826 | The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS), | |
3827 | where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address, | |
3828 | NETMASK is the layer 3 network mask, HWADDR is the layer 2 address, and | |
3829 | FLAGS is the current flags of the interface. | |
3830 | ||
3831 | Data that is unavailable is returned as nil. */) | |
3832 | (Lisp_Object ifname) | |
3833 | { | |
3834 | #if ((defined HAVE_NET_IF_H \ | |
3835 | && (defined SIOCGIFADDR || defined SIOCGIFHWADDR \ | |
3836 | || defined SIOCGIFFLAGS)) \ | |
3837 | || defined WINDOWSNT) | |
3838 | return network_interface_info (ifname); | |
3839 | #else | |
3840 | return Qnil; | |
3841 | #endif | |
3842 | } | |
3843 | ||
3844 | ||
04391069 RS |
3845 | /* Turn off input and output for process PROC. */ |
3846 | ||
40ccffa6 | 3847 | static void |
d3da34e0 | 3848 | deactivate_process (Lisp_Object proc) |
d0d6b7c5 | 3849 | { |
94fcd171 PE |
3850 | int inchannel; |
3851 | struct Lisp_Process *p = XPROCESS (proc); | |
3852 | int i; | |
d0d6b7c5 | 3853 | |
9c6c6f49 CY |
3854 | #ifdef HAVE_GNUTLS |
3855 | /* Delete GnuTLS structures in PROC, if any. */ | |
3856 | emacs_gnutls_deinit (proc); | |
3857 | #endif /* HAVE_GNUTLS */ | |
3858 | ||
6b61353c | 3859 | #ifdef ADAPTIVE_READ_BUFFERING |
60f0fb11 | 3860 | if (p->read_output_delay > 0) |
6b61353c KH |
3861 | { |
3862 | if (--process_output_delay_count < 0) | |
3863 | process_output_delay_count = 0; | |
60f0fb11 SM |
3864 | p->read_output_delay = 0; |
3865 | p->read_output_skip = 0; | |
6b61353c KH |
3866 | } |
3867 | #endif | |
f3d9532b | 3868 | |
94fcd171 | 3869 | /* Beware SIGCHLD hereabouts. */ |
94fcd171 PE |
3870 | |
3871 | for (i = 0; i < PROCESS_OPEN_FDS; i++) | |
3872 | close_process_fd (&p->open_fd[i]); | |
3873 | ||
26495794 | 3874 | inchannel = p->infd; |
a9f2c884 | 3875 | if (inchannel >= 0) |
d0d6b7c5 | 3876 | { |
297a545b PE |
3877 | p->infd = -1; |
3878 | p->outfd = -1; | |
e690ca94 KS |
3879 | #ifdef DATAGRAM_SOCKETS |
3880 | if (DATAGRAM_CHAN_P (inchannel)) | |
3881 | { | |
3882 | xfree (datagram_address[inchannel].sa); | |
3883 | datagram_address[inchannel].sa = 0; | |
3884 | datagram_address[inchannel].len = 0; | |
3885 | } | |
3886 | #endif | |
d0d6b7c5 JB |
3887 | chan_process[inchannel] = Qnil; |
3888 | FD_CLR (inchannel, &input_wait_mask); | |
a69281ff | 3889 | FD_CLR (inchannel, &non_keyboard_wait_mask); |
bad49fc7 | 3890 | #ifdef NON_BLOCKING_CONNECT |
dd2a17ab KS |
3891 | if (FD_ISSET (inchannel, &connect_wait_mask)) |
3892 | { | |
3893 | FD_CLR (inchannel, &connect_wait_mask); | |
3fad2ad2 | 3894 | FD_CLR (inchannel, &write_mask); |
dd2a17ab | 3895 | if (--num_pending_connects < 0) |
1088b922 | 3896 | emacs_abort (); |
dd2a17ab | 3897 | } |
bad49fc7 | 3898 | #endif |
7d0e672e RS |
3899 | if (inchannel == max_process_desc) |
3900 | { | |
7d0e672e RS |
3901 | /* We just closed the highest-numbered process input descriptor, |
3902 | so recompute the highest-numbered one now. */ | |
2878ba7e PE |
3903 | int i = inchannel; |
3904 | do | |
3905 | i--; | |
3906 | while (0 <= i && NILP (chan_process[i])); | |
3907 | ||
3908 | max_process_desc = i; | |
7d0e672e | 3909 | } |
d0d6b7c5 JB |
3910 | } |
3911 | } | |
3912 | ||
d0d6b7c5 JB |
3913 | \f |
3914 | DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output, | |
107ed38d | 3915 | 0, 4, 0, |
fdb82f93 PJ |
3916 | doc: /* Allow any pending output from subprocesses to be read by Emacs. |
3917 | It is read into the process' buffers or given to their filter functions. | |
3918 | Non-nil arg PROCESS means do not return until some output has been received | |
3919 | from PROCESS. | |
5088da42 | 3920 | |
2a7bb882 MR |
3921 | Non-nil second arg SECONDS and third arg MILLISEC are number of seconds |
3922 | and milliseconds to wait; return after that much time whether or not | |
3923 | there is any subprocess output. If SECONDS is a floating point number, | |
5088da42 | 3924 | it specifies a fractional number of seconds to wait. |
891bd142 | 3925 | The MILLISEC argument is obsolete and should be avoided. |
5088da42 | 3926 | |
107ed38d KS |
3927 | If optional fourth arg JUST-THIS-ONE is non-nil, only accept output |
3928 | from PROCESS, suspending reading output from other processes. | |
3929 | If JUST-THIS-ONE is an integer, don't run any timers either. | |
e0f24100 | 3930 | Return non-nil if we received any output before the timeout expired. */) |
5842a27b | 3931 | (register Lisp_Object process, Lisp_Object seconds, Lisp_Object millisec, Lisp_Object just_this_one) |
d0d6b7c5 | 3932 | { |
d35af63c PE |
3933 | intmax_t secs; |
3934 | int nsecs; | |
d0d6b7c5 | 3935 | |
0748d150 | 3936 | if (! NILP (process)) |
b7826503 | 3937 | CHECK_PROCESS (process); |
107ed38d KS |
3938 | else |
3939 | just_this_one = Qnil; | |
0748d150 | 3940 | |
891bd142 SM |
3941 | if (!NILP (millisec)) |
3942 | { /* Obsolete calling convention using integers rather than floats. */ | |
3943 | CHECK_NUMBER (millisec); | |
3944 | if (NILP (seconds)) | |
3945 | seconds = make_float (XINT (millisec) / 1000.0); | |
3946 | else | |
3947 | { | |
3948 | CHECK_NUMBER (seconds); | |
3949 | seconds = make_float (XINT (millisec) / 1000.0 + XINT (seconds)); | |
3950 | } | |
3951 | } | |
3952 | ||
d35af63c PE |
3953 | secs = 0; |
3954 | nsecs = -1; | |
3955 | ||
5088da42 | 3956 | if (!NILP (seconds)) |
d0d6b7c5 | 3957 | { |
d35af63c PE |
3958 | if (INTEGERP (seconds)) |
3959 | { | |
908589fd | 3960 | if (XINT (seconds) > 0) |
d35af63c PE |
3961 | { |
3962 | secs = XINT (seconds); | |
3963 | nsecs = 0; | |
3964 | } | |
3965 | } | |
3966 | else if (FLOATP (seconds)) | |
3967 | { | |
908589fd | 3968 | if (XFLOAT_DATA (seconds) > 0) |
d35af63c | 3969 | { |
43aac990 PE |
3970 | struct timespec t = dtotimespec (XFLOAT_DATA (seconds)); |
3971 | secs = min (t.tv_sec, WAIT_READING_MAX); | |
3972 | nsecs = t.tv_nsec; | |
d35af63c PE |
3973 | } |
3974 | } | |
3975 | else | |
3976 | wrong_type_argument (Qnumberp, seconds); | |
d0d6b7c5 | 3977 | } |
d35af63c PE |
3978 | else if (! NILP (process)) |
3979 | nsecs = 0; | |
d0d6b7c5 JB |
3980 | |
3981 | return | |
d35af63c | 3982 | (wait_reading_process_output (secs, nsecs, 0, 0, |
214d6069 KS |
3983 | Qnil, |
3984 | !NILP (process) ? XPROCESS (process) : NULL, | |
3985 | NILP (just_this_one) ? 0 : | |
3986 | !INTEGERP (just_this_one) ? 1 : -1) | |
d0d6b7c5 JB |
3987 | ? Qt : Qnil); |
3988 | } | |
3989 | ||
e690ca94 KS |
3990 | /* Accept a connection for server process SERVER on CHANNEL. */ |
3991 | ||
0bce5d9e | 3992 | static EMACS_INT connect_counter = 0; |
e690ca94 KS |
3993 | |
3994 | static void | |
d3da34e0 | 3995 | server_accept_connection (Lisp_Object server, int channel) |
e690ca94 KS |
3996 | { |
3997 | Lisp_Object proc, caller, name, buffer; | |
3998 | Lisp_Object contact, host, service; | |
3999 | struct Lisp_Process *ps= XPROCESS (server); | |
4000 | struct Lisp_Process *p; | |
4001 | int s; | |
4002 | union u_sockaddr { | |
4003 | struct sockaddr sa; | |
4004 | struct sockaddr_in in; | |
e1652a86 KS |
4005 | #ifdef AF_INET6 |
4006 | struct sockaddr_in6 in6; | |
4007 | #endif | |
e690ca94 KS |
4008 | #ifdef HAVE_LOCAL_SOCKETS |
4009 | struct sockaddr_un un; | |
4010 | #endif | |
4011 | } saddr; | |
401bf9b4 | 4012 | socklen_t len = sizeof saddr; |
3f5bef16 | 4013 | ptrdiff_t count; |
e690ca94 | 4014 | |
067428c1 | 4015 | s = accept4 (channel, &saddr.sa, &len, SOCK_CLOEXEC); |
e690ca94 KS |
4016 | |
4017 | if (s < 0) | |
4018 | { | |
4019 | int code = errno; | |
4020 | ||
4021 | if (code == EAGAIN) | |
4022 | return; | |
4023 | #ifdef EWOULDBLOCK | |
4024 | if (code == EWOULDBLOCK) | |
4025 | return; | |
4026 | #endif | |
4027 | ||
4d2b044c DA |
4028 | if (!NILP (ps->log)) |
4029 | call3 (ps->log, server, Qnil, | |
e690ca94 KS |
4030 | concat3 (build_string ("accept failed with code"), |
4031 | Fnumber_to_string (make_number (code)), | |
4032 | build_string ("\n"))); | |
4033 | return; | |
4034 | } | |
4035 | ||
3f5bef16 PE |
4036 | count = SPECPDL_INDEX (); |
4037 | record_unwind_protect_int (close_file_unwind, s); | |
4038 | ||
e690ca94 KS |
4039 | connect_counter++; |
4040 | ||
4041 | /* Setup a new process to handle the connection. */ | |
4042 | ||
4043 | /* Generate a unique identification of the caller, and build contact | |
4044 | information for this process. */ | |
4045 | host = Qt; | |
4046 | service = Qnil; | |
4047 | switch (saddr.sa.sa_family) | |
4048 | { | |
4049 | case AF_INET: | |
4050 | { | |
4051 | Lisp_Object args[5]; | |
4052 | unsigned char *ip = (unsigned char *)&saddr.in.sin_addr.s_addr; | |
4053 | args[0] = build_string ("%d.%d.%d.%d"); | |
4054 | args[1] = make_number (*ip++); | |
4055 | args[2] = make_number (*ip++); | |
4056 | args[3] = make_number (*ip++); | |
4057 | args[4] = make_number (*ip++); | |
4058 | host = Fformat (5, args); | |
4059 | service = make_number (ntohs (saddr.in.sin_port)); | |
4060 | ||
4061 | args[0] = build_string (" <%s:%d>"); | |
4062 | args[1] = host; | |
4063 | args[2] = service; | |
4064 | caller = Fformat (3, args); | |
4065 | } | |
4066 | break; | |
4067 | ||
e1652a86 KS |
4068 | #ifdef AF_INET6 |
4069 | case AF_INET6: | |
4070 | { | |
4071 | Lisp_Object args[9]; | |
4072 | uint16_t *ip6 = (uint16_t *)&saddr.in6.sin6_addr; | |
4073 | int i; | |
4074 | args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x"); | |
4075 | for (i = 0; i < 8; i++) | |
ed3751c8 | 4076 | args[i+1] = make_number (ntohs (ip6[i])); |
e1652a86 KS |
4077 | host = Fformat (9, args); |
4078 | service = make_number (ntohs (saddr.in.sin_port)); | |
4079 | ||
4080 | args[0] = build_string (" <[%s]:%d>"); | |
4081 | args[1] = host; | |
4082 | args[2] = service; | |
4083 | caller = Fformat (3, args); | |
4084 | } | |
4085 | break; | |
4086 | #endif | |
4087 | ||
e690ca94 KS |
4088 | #ifdef HAVE_LOCAL_SOCKETS |
4089 | case AF_LOCAL: | |
4090 | #endif | |
4091 | default: | |
4092 | caller = Fnumber_to_string (make_number (connect_counter)); | |
eef60308 | 4093 | caller = concat3 (build_string (" <"), caller, build_string (">")); |
e690ca94 KS |
4094 | break; |
4095 | } | |
4096 | ||
4097 | /* Create a new buffer name for this process if it doesn't have a | |
4098 | filter. The new buffer name is based on the buffer name or | |
4099 | process name of the server process concatenated with the caller | |
4100 | identification. */ | |
4101 | ||
1aa8d505 SM |
4102 | if (!(EQ (ps->filter, Qinternal_default_process_filter) |
4103 | || EQ (ps->filter, Qt))) | |
e690ca94 KS |
4104 | buffer = Qnil; |
4105 | else | |
4106 | { | |
4d2b044c | 4107 | buffer = ps->buffer; |
e690ca94 KS |
4108 | if (!NILP (buffer)) |
4109 | buffer = Fbuffer_name (buffer); | |
4110 | else | |
4d2b044c | 4111 | buffer = ps->name; |
e690ca94 KS |
4112 | if (!NILP (buffer)) |
4113 | { | |
4114 | buffer = concat2 (buffer, caller); | |
4115 | buffer = Fget_buffer_create (buffer); | |
4116 | } | |
4117 | } | |
4118 | ||
4119 | /* Generate a unique name for the new server process. Combine the | |
4120 | server process name with the caller identification. */ | |
4121 | ||
4d2b044c | 4122 | name = concat2 (ps->name, caller); |
e690ca94 KS |
4123 | proc = make_process (name); |
4124 | ||
4125 | chan_process[s] = proc; | |
4126 | ||
e690ca94 | 4127 | fcntl (s, F_SETFL, O_NONBLOCK); |
e690ca94 KS |
4128 | |
4129 | p = XPROCESS (proc); | |
4130 | ||
4131 | /* Build new contact information for this setup. */ | |
4d2b044c | 4132 | contact = Fcopy_sequence (ps->childp); |
e690ca94 KS |
4133 | contact = Fplist_put (contact, QCserver, Qnil); |
4134 | contact = Fplist_put (contact, QChost, host); | |
4135 | if (!NILP (service)) | |
4136 | contact = Fplist_put (contact, QCservice, service); | |
177c0ea7 | 4137 | contact = Fplist_put (contact, QCremote, |
e690ca94 KS |
4138 | conv_sockaddr_to_lisp (&saddr.sa, len)); |
4139 | #ifdef HAVE_GETSOCKNAME | |
4140 | len = sizeof saddr; | |
2185db04 | 4141 | if (getsockname (s, &saddr.sa, &len) == 0) |
177c0ea7 | 4142 | contact = Fplist_put (contact, QClocal, |
e690ca94 KS |
4143 | conv_sockaddr_to_lisp (&saddr.sa, len)); |
4144 | #endif | |
4145 | ||
6a09a33b PE |
4146 | pset_childp (p, contact); |
4147 | pset_plist (p, Fcopy_sequence (ps->plist)); | |
4148 | pset_type (p, Qnetwork); | |
ac4a7584 | 4149 | |
6a09a33b PE |
4150 | pset_buffer (p, buffer); |
4151 | pset_sentinel (p, ps->sentinel); | |
4152 | pset_filter (p, ps->filter); | |
4153 | pset_command (p, Qnil); | |
6bfd98e7 | 4154 | p->pid = 0; |
3f5bef16 PE |
4155 | |
4156 | /* Discard the unwind protect for closing S. */ | |
4157 | specpdl_ptr = specpdl + count; | |
4158 | ||
94fcd171 | 4159 | p->open_fd[SUBPROCESS_STDIN] = s; |
60f0fb11 SM |
4160 | p->infd = s; |
4161 | p->outfd = s; | |
6a09a33b | 4162 | pset_status (p, Qrun); |
e690ca94 KS |
4163 | |
4164 | /* Client processes for accepted connections are not stopped initially. */ | |
4d2b044c | 4165 | if (!EQ (p->filter, Qt)) |
e690ca94 KS |
4166 | { |
4167 | FD_SET (s, &input_wait_mask); | |
4168 | FD_SET (s, &non_keyboard_wait_mask); | |
4169 | } | |
4170 | ||
4171 | if (s > max_process_desc) | |
4172 | max_process_desc = s; | |
4173 | ||
177c0ea7 | 4174 | /* Setup coding system for new process based on server process. |
e690ca94 KS |
4175 | This seems to be the proper thing to do, as the coding system |
4176 | of the new process should reflect the settings at the time the | |
1aa8d505 | 4177 | server socket was opened; not the current settings. */ |
e690ca94 | 4178 | |
6a09a33b PE |
4179 | pset_decode_coding_system (p, ps->decode_coding_system); |
4180 | pset_encode_coding_system (p, ps->encode_coding_system); | |
03f04413 | 4181 | setup_process_coding_systems (proc); |
e690ca94 | 4182 | |
6a09a33b | 4183 | pset_decoding_buf (p, empty_unibyte_string); |
60f0fb11 | 4184 | p->decoding_carryover = 0; |
6a09a33b | 4185 | pset_encoding_buf (p, empty_unibyte_string); |
e690ca94 KS |
4186 | |
4187 | p->inherit_coding_system_flag | |
60f0fb11 | 4188 | = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag); |
e690ca94 | 4189 | |
4d2b044c DA |
4190 | if (!NILP (ps->log)) |
4191 | call3 (ps->log, server, proc, | |
e690ca94 KS |
4192 | concat3 (build_string ("accept from "), |
4193 | (STRINGP (host) ? host : build_string ("-")), | |
4194 | build_string ("\n"))); | |
4195 | ||
1aa8d505 SM |
4196 | exec_sentinel (proc, |
4197 | concat3 (build_string ("open from "), | |
4198 | (STRINGP (host) ? host : build_string ("-")), | |
4199 | build_string ("\n"))); | |
e690ca94 KS |
4200 | } |
4201 | ||
d0d6b7c5 JB |
4202 | /* This variable is different from waiting_for_input in keyboard.c. |
4203 | It is used to communicate to a lisp process-filter/sentinel (via the | |
f3d9532b | 4204 | function Fwaiting_for_user_input_p below) whether Emacs was waiting |
d0d6b7c5 JB |
4205 | for user-input when that process-filter was called. |
4206 | waiting_for_input cannot be used as that is by definition 0 when | |
d430ee71 RS |
4207 | lisp code is being evalled. |
4208 | This is also used in record_asynch_buffer_change. | |
4209 | For that purpose, this must be 0 | |
214d6069 | 4210 | when not inside wait_reading_process_output. */ |
d0d6b7c5 JB |
4211 | static int waiting_for_user_input_p; |
4212 | ||
27e498e6 PE |
4213 | static void |
4214 | wait_reading_process_output_unwind (int data) | |
2beb96f9 | 4215 | { |
27e498e6 | 4216 | waiting_for_user_input_p = data; |
2beb96f9 KS |
4217 | } |
4218 | ||
c573ae8e | 4219 | /* This is here so breakpoints can be put on it. */ |
dfcf069d | 4220 | static void |
d3da34e0 | 4221 | wait_reading_process_output_1 (void) |
c573ae8e RS |
4222 | { |
4223 | } | |
4224 | ||
d0d6b7c5 JB |
4225 | /* Read and dispose of subprocess output while waiting for timeout to |
4226 | elapse and/or keyboard input to be available. | |
4227 | ||
de6fd4b9 | 4228 | TIME_LIMIT is: |
d35af63c PE |
4229 | timeout in seconds |
4230 | If negative, gobble data immediately available but don't wait for any. | |
d0d6b7c5 | 4231 | |
d35af63c PE |
4232 | NSECS is: |
4233 | an additional duration to wait, measured in nanoseconds | |
4234 | If TIME_LIMIT is zero, then: | |
4235 | If NSECS == 0, there is no limit. | |
d188e26b | 4236 | If NSECS > 0, the timeout consists of NSECS only. |
d35af63c | 4237 | If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative. |
6e4f3667 | 4238 | |
a0bee46f | 4239 | READ_KBD is: |
d0d6b7c5 JB |
4240 | 0 to ignore keyboard input, or |
4241 | 1 to return when input is available, or | |
84aa3ace | 4242 | -1 meaning caller will actually read the input, so don't throw to |
d0d6b7c5 | 4243 | the quit handler, or |
d0d6b7c5 | 4244 | |
9fa1de30 | 4245 | DO_DISPLAY means redisplay should be done to show subprocess |
107ed38d | 4246 | output that arrives. |
d0d6b7c5 | 4247 | |
b89a415a KS |
4248 | If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil |
4249 | (and gobble terminal input into the buffer if any arrives). | |
d0d6b7c5 | 4250 | |
b89a415a | 4251 | If WAIT_PROC is specified, wait until something arrives from that |
e0f24100 | 4252 | process. The return value is true if we read some input from |
b89a415a KS |
4253 | that process. |
4254 | ||
0bce5d9e | 4255 | If JUST_WAIT_PROC is nonzero, handle only output from WAIT_PROC |
b89a415a KS |
4256 | (suspending output from other processes). A negative value |
4257 | means don't run any timers either. | |
4258 | ||
e0f24100 | 4259 | If WAIT_PROC is specified, then the function returns true if we |
b89a415a | 4260 | received input from that process before the timeout elapsed. |
e0f24100 | 4261 | Otherwise, return true if we received input from any process. */ |
d0d6b7c5 | 4262 | |
0bce5d9e | 4263 | bool |
d35af63c | 4264 | wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, |
9fa1de30 | 4265 | bool do_display, |
d5a3eaaf AS |
4266 | Lisp_Object wait_for_cell, |
4267 | struct Lisp_Process *wait_proc, int just_wait_proc) | |
d0d6b7c5 | 4268 | { |
0bce5d9e | 4269 | int channel, nfds; |
d486344e PE |
4270 | fd_set Available; |
4271 | fd_set Writeok; | |
0bce5d9e PE |
4272 | bool check_write; |
4273 | int check_delay; | |
4274 | bool no_avail; | |
d0d6b7c5 JB |
4275 | int xerrno; |
4276 | Lisp_Object proc; | |
43aac990 | 4277 | struct timespec timeout, end_time; |
a9f2c884 | 4278 | int wait_channel = -1; |
0bce5d9e | 4279 | bool got_some_input = 0; |
d311d28c | 4280 | ptrdiff_t count = SPECPDL_INDEX (); |
d0d6b7c5 JB |
4281 | |
4282 | FD_ZERO (&Available); | |
3fad2ad2 | 4283 | FD_ZERO (&Writeok); |
d0d6b7c5 | 4284 | |
d35af63c | 4285 | if (time_limit == 0 && nsecs == 0 && wait_proc && !NILP (Vinhibit_quit) |
4d2b044c DA |
4286 | && !(CONSP (wait_proc->status) |
4287 | && EQ (XCAR (wait_proc->status), Qexit))) | |
2f73da9c | 4288 | message1 ("Blocking call to accept-process-output with quit inhibited!!"); |
2b0a91e7 | 4289 | |
b89a415a KS |
4290 | /* If wait_proc is a process to watch, set wait_channel accordingly. */ |
4291 | if (wait_proc != NULL) | |
60f0fb11 | 4292 | wait_channel = wait_proc->infd; |
84aa3ace | 4293 | |
27e498e6 PE |
4294 | record_unwind_protect_int (wait_reading_process_output_unwind, |
4295 | waiting_for_user_input_p); | |
b89a415a | 4296 | waiting_for_user_input_p = read_kbd; |
d0d6b7c5 | 4297 | |
d35af63c PE |
4298 | if (time_limit < 0) |
4299 | { | |
4300 | time_limit = 0; | |
4301 | nsecs = -1; | |
4302 | } | |
7216e43b | 4303 | else if (TYPE_MAXIMUM (time_t) < time_limit) |
d35af63c PE |
4304 | time_limit = TYPE_MAXIMUM (time_t); |
4305 | ||
d0d6b7c5 JB |
4306 | /* Since we may need to wait several times, |
4307 | compute the absolute time to return at. */ | |
908589fd | 4308 | if (time_limit || nsecs > 0) |
d0d6b7c5 | 4309 | { |
43aac990 PE |
4310 | timeout = make_timespec (time_limit, nsecs); |
4311 | end_time = timespec_add (current_timespec (), timeout); | |
d0d6b7c5 JB |
4312 | } |
4313 | ||
d0d6b7c5 JB |
4314 | while (1) |
4315 | { | |
0bce5d9e | 4316 | bool timeout_reduced_for_timers = 0; |
209c6a58 | 4317 | |
d0d6b7c5 JB |
4318 | /* If calling from keyboard input, do not quit |
4319 | since we want to return C-g as an input character. | |
4320 | Otherwise, do pending quit if requested. */ | |
b89a415a | 4321 | if (read_kbd >= 0) |
d0d6b7c5 | 4322 | QUIT; |
4d7e6e51 | 4323 | else if (pending_signals) |
c3eff1b0 | 4324 | process_pending_signals (); |
d0d6b7c5 | 4325 | |
889255b4 | 4326 | /* Exit now if the cell we're waiting for became non-nil. */ |
f3fbd155 | 4327 | if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) |
889255b4 RS |
4328 | break; |
4329 | ||
1aa8d505 SM |
4330 | /* Compute time from now till when time limit is up. */ |
4331 | /* Exit if already run out. */ | |
d35af63c | 4332 | if (nsecs < 0) |
d0d6b7c5 | 4333 | { |
216ee680 | 4334 | /* A negative timeout means |
d0d6b7c5 JB |
4335 | gobble output available now |
4336 | but don't wait at all. */ | |
4337 | ||
43aac990 | 4338 | timeout = make_timespec (0, 0); |
d0d6b7c5 | 4339 | } |
908589fd | 4340 | else if (time_limit || nsecs > 0) |
d0d6b7c5 | 4341 | { |
43aac990 PE |
4342 | struct timespec now = current_timespec (); |
4343 | if (timespec_cmp (end_time, now) <= 0) | |
d0d6b7c5 | 4344 | break; |
43aac990 | 4345 | timeout = timespec_sub (end_time, now); |
d0d6b7c5 JB |
4346 | } |
4347 | else | |
4348 | { | |
43aac990 | 4349 | timeout = make_timespec (100000, 0); |
d0d6b7c5 JB |
4350 | } |
4351 | ||
f854a00b RS |
4352 | /* Normally we run timers here. |
4353 | But not if wait_for_cell; in those cases, | |
4354 | the wait is supposed to be short, | |
4355 | and those callers cannot handle running arbitrary Lisp code here. */ | |
107ed38d | 4356 | if (NILP (wait_for_cell) |
b89a415a | 4357 | && just_wait_proc >= 0) |
fb4c3627 | 4358 | { |
43aac990 | 4359 | struct timespec timer_delay; |
c573ae8e | 4360 | |
9baacf76 | 4361 | do |
c573ae8e | 4362 | { |
9fa1de30 | 4363 | unsigned old_timers_run = timers_run; |
c88164fe | 4364 | struct buffer *old_buffer = current_buffer; |
70bb1e5f | 4365 | Lisp_Object old_window = selected_window; |
177c0ea7 | 4366 | |
f868cd8a | 4367 | timer_delay = timer_check (); |
a2fab450 GM |
4368 | |
4369 | /* If a timer has run, this might have changed buffers | |
4370 | an alike. Make read_key_sequence aware of that. */ | |
4371 | if (timers_run != old_timers_run | |
70bb1e5f CY |
4372 | && (old_buffer != current_buffer |
4373 | || !EQ (old_window, selected_window)) | |
a2fab450 GM |
4374 | && waiting_for_user_input_p == -1) |
4375 | record_asynch_buffer_change (); | |
177c0ea7 | 4376 | |
9baacf76 GM |
4377 | if (timers_run != old_timers_run && do_display) |
4378 | /* We must retry, since a timer may have requeued itself | |
4379 | and that could alter the time_delay. */ | |
3007ebfb | 4380 | redisplay_preserve_echo_area (9); |
9baacf76 GM |
4381 | else |
4382 | break; | |
c573ae8e | 4383 | } |
9baacf76 | 4384 | while (!detect_input_pending ()); |
c573ae8e | 4385 | |
69645afc | 4386 | /* If there is unread keyboard input, also return. */ |
b89a415a | 4387 | if (read_kbd != 0 |
69645afc RS |
4388 | && requeued_events_pending_p ()) |
4389 | break; | |
4390 | ||
216ee680 | 4391 | /* A negative timeout means do not wait at all. */ |
908589fd | 4392 | if (nsecs >= 0) |
fb4c3627 | 4393 | { |
43aac990 | 4394 | if (timespec_valid_p (timer_delay)) |
c0239a0b | 4395 | { |
43aac990 | 4396 | if (timespec_cmp (timer_delay, timeout) < 0) |
209c6a58 CY |
4397 | { |
4398 | timeout = timer_delay; | |
4399 | timeout_reduced_for_timers = 1; | |
4400 | } | |
d35af63c PE |
4401 | } |
4402 | else | |
4403 | { | |
4404 | /* This is so a breakpoint can be put here. */ | |
4405 | wait_reading_process_output_1 (); | |
c0239a0b | 4406 | } |
c573ae8e | 4407 | } |
fb4c3627 RS |
4408 | } |
4409 | ||
90ab1a81 JB |
4410 | /* Cause C-g and alarm signals to take immediate action, |
4411 | and cause input available signals to zero out timeout. | |
4412 | ||
4413 | It is important that we do this before checking for process | |
4414 | activity. If we get a SIGCHLD after the explicit checks for | |
4415 | process activity, timeout is the only way we will know. */ | |
b89a415a | 4416 | if (read_kbd < 0) |
90ab1a81 JB |
4417 | set_waiting_for_input (&timeout); |
4418 | ||
6be429b1 JB |
4419 | /* If status of something has changed, and no input is |
4420 | available, notify the user of the change right away. After | |
4421 | this explicit check, we'll let the SIGCHLD handler zap | |
e42bdf01 CY |
4422 | timeout to get our attention. */ |
4423 | if (update_tick != process_tick) | |
6be429b1 | 4424 | { |
d486344e PE |
4425 | fd_set Atemp; |
4426 | fd_set Ctemp; | |
dd2a17ab | 4427 | |
cb768704 J |
4428 | if (kbd_on_hold_p ()) |
4429 | FD_ZERO (&Atemp); | |
4430 | else | |
4431 | Atemp = input_wait_mask; | |
3fad2ad2 | 4432 | Ctemp = write_mask; |
bad49fc7 | 4433 | |
43aac990 | 4434 | timeout = make_timespec (0, 0); |
d35af63c PE |
4435 | if ((pselect (max (max_process_desc, max_input_desc) + 1, |
4436 | &Atemp, | |
bad49fc7 | 4437 | #ifdef NON_BLOCKING_CONNECT |
d35af63c | 4438 | (num_pending_connects > 0 ? &Ctemp : NULL), |
bad49fc7 | 4439 | #else |
d35af63c | 4440 | NULL, |
bad49fc7 | 4441 | #endif |
d35af63c | 4442 | NULL, &timeout, NULL) |
ecd1f654 | 4443 | <= 0)) |
90ab1a81 JB |
4444 | { |
4445 | /* It's okay for us to do this and then continue with | |
a0e4d3f3 | 4446 | the loop, since timeout has already been zeroed out. */ |
90ab1a81 | 4447 | clear_waiting_for_input (); |
ff6daed3 | 4448 | status_notify (NULL); |
e42bdf01 | 4449 | if (do_display) redisplay_preserve_echo_area (13); |
90ab1a81 | 4450 | } |
6be429b1 JB |
4451 | } |
4452 | ||
dd2a17ab | 4453 | /* Don't wait for output from a non-running process. Just |
63136da6 | 4454 | read whatever data has already been received. */ |
6bfd98e7 | 4455 | if (wait_proc && wait_proc->raw_status_new) |
6be429b1 | 4456 | update_status (wait_proc); |
6bfd98e7 | 4457 | if (wait_proc |
4d2b044c DA |
4458 | && ! EQ (wait_proc->status, Qrun) |
4459 | && ! EQ (wait_proc->status, Qconnect)) | |
9aa2a7f4 | 4460 | { |
f86852b4 | 4461 | bool read_some_bytes = 0; |
7ce63188 | 4462 | |
9aa2a7f4 | 4463 | clear_waiting_for_input (); |
7ce63188 RS |
4464 | XSETPROCESS (proc, wait_proc); |
4465 | ||
4466 | /* Read data from the process, until we exhaust it. */ | |
60f0fb11 | 4467 | while (wait_proc->infd >= 0) |
215b45e9 | 4468 | { |
f86852b4 | 4469 | int nread = read_process_output (proc, wait_proc->infd); |
e1b37c34 GM |
4470 | |
4471 | if (nread == 0) | |
4472 | break; | |
4473 | ||
908589fd | 4474 | if (nread > 0) |
f86852b4 | 4475 | got_some_input = read_some_bytes = 1; |
22626a85 | 4476 | else if (nread == -1 && (errno == EIO || errno == EAGAIN)) |
63136da6 | 4477 | break; |
e1b37c34 GM |
4478 | #ifdef EWOULDBLOCK |
4479 | else if (nread == -1 && EWOULDBLOCK == errno) | |
63136da6 | 4480 | break; |
215b45e9 RS |
4481 | #endif |
4482 | } | |
f86852b4 | 4483 | if (read_some_bytes && do_display) |
3007ebfb | 4484 | redisplay_preserve_echo_area (10); |
7ce63188 | 4485 | |
9aa2a7f4 JB |
4486 | break; |
4487 | } | |
6be429b1 | 4488 | |
d0d6b7c5 JB |
4489 | /* Wait till there is something to do */ |
4490 | ||
b89a415a | 4491 | if (wait_proc && just_wait_proc) |
107ed38d | 4492 | { |
60f0fb11 | 4493 | if (wait_proc->infd < 0) /* Terminated */ |
b89a415a | 4494 | break; |
60f0fb11 | 4495 | FD_SET (wait_proc->infd, &Available); |
bad49fc7 | 4496 | check_delay = 0; |
3fad2ad2 | 4497 | check_write = 0; |
107ed38d KS |
4498 | } |
4499 | else if (!NILP (wait_for_cell)) | |
dd2a17ab KS |
4500 | { |
4501 | Available = non_process_wait_mask; | |
bad49fc7 | 4502 | check_delay = 0; |
3fad2ad2 | 4503 | check_write = 0; |
dd2a17ab | 4504 | } |
a69281ff | 4505 | else |
dd2a17ab | 4506 | { |
b89a415a | 4507 | if (! read_kbd) |
dd2a17ab KS |
4508 | Available = non_keyboard_wait_mask; |
4509 | else | |
4510 | Available = input_wait_mask; | |
3fad2ad2 | 4511 | Writeok = write_mask; |
82d6e50b J |
4512 | #ifdef SELECT_CANT_DO_WRITE_MASK |
4513 | check_write = 0; | |
4514 | #else | |
3fad2ad2 | 4515 | check_write = 1; |
82d6e50b | 4516 | #endif |
6b61353c | 4517 | check_delay = wait_channel >= 0 ? 0 : process_output_delay_count; |
dd2a17ab | 4518 | } |
d0d6b7c5 | 4519 | |
ff11dfa1 | 4520 | /* If frame size has changed or the window is newly mapped, |
ffd56f97 JB |
4521 | redisplay now, before we start to wait. There is a race |
4522 | condition here; if a SIGIO arrives between now and the select | |
016899c0 JB |
4523 | and indicates that a frame is trashed, the select may block |
4524 | displaying a trashed screen. */ | |
5164ee8e | 4525 | if (frame_garbaged && do_display) |
7286affd RS |
4526 | { |
4527 | clear_waiting_for_input (); | |
3007ebfb | 4528 | redisplay_preserve_echo_area (11); |
b89a415a | 4529 | if (read_kbd < 0) |
7efe788e | 4530 | set_waiting_for_input (&timeout); |
7286affd | 4531 | } |
ffd56f97 | 4532 | |
029529ac CY |
4533 | /* Skip the `select' call if input is available and we're |
4534 | waiting for keyboard input or a cell change (which can be | |
4535 | triggered by processing X events). In the latter case, set | |
4536 | nfds to 1 to avoid breaking the loop. */ | |
dd2a17ab | 4537 | no_avail = 0; |
029529ac CY |
4538 | if ((read_kbd || !NILP (wait_for_cell)) |
4539 | && detect_input_pending ()) | |
0a65b032 | 4540 | { |
029529ac | 4541 | nfds = read_kbd ? 0 : 1; |
dd2a17ab | 4542 | no_avail = 1; |
0a65b032 | 4543 | } |
029529ac CY |
4544 | |
4545 | if (!no_avail) | |
dd2a17ab | 4546 | { |
6b61353c KH |
4547 | |
4548 | #ifdef ADAPTIVE_READ_BUFFERING | |
db853b7a | 4549 | /* Set the timeout for adaptive read buffering if any |
60f0fb11 | 4550 | process has non-zero read_output_skip and non-zero |
db853b7a KS |
4551 | read_output_delay, and we are not reading output for a |
4552 | specific wait_channel. It is not executed if | |
4553 | Vprocess_adaptive_read_buffering is nil. */ | |
6b61353c KH |
4554 | if (process_output_skip && check_delay > 0) |
4555 | { | |
43aac990 PE |
4556 | int nsecs = timeout.tv_nsec; |
4557 | if (timeout.tv_sec > 0 || nsecs > READ_OUTPUT_DELAY_MAX) | |
d35af63c | 4558 | nsecs = READ_OUTPUT_DELAY_MAX; |
6b61353c KH |
4559 | for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++) |
4560 | { | |
4561 | proc = chan_process[channel]; | |
4562 | if (NILP (proc)) | |
4563 | continue; | |
db853b7a | 4564 | /* Find minimum non-zero read_output_delay among the |
60f0fb11 SM |
4565 | processes with non-zero read_output_skip. */ |
4566 | if (XPROCESS (proc)->read_output_delay > 0) | |
6b61353c KH |
4567 | { |
4568 | check_delay--; | |
60f0fb11 | 4569 | if (!XPROCESS (proc)->read_output_skip) |
6b61353c KH |
4570 | continue; |
4571 | FD_CLR (channel, &Available); | |
60f0fb11 | 4572 | XPROCESS (proc)->read_output_skip = 0; |
d35af63c PE |
4573 | if (XPROCESS (proc)->read_output_delay < nsecs) |
4574 | nsecs = XPROCESS (proc)->read_output_delay; | |
6b61353c KH |
4575 | } |
4576 | } | |
43aac990 | 4577 | timeout = make_timespec (0, nsecs); |
6b61353c KH |
4578 | process_output_skip = 0; |
4579 | } | |
4580 | #endif | |
0fda9b75 | 4581 | |
b33f93ee JD |
4582 | #if defined (HAVE_NS) |
4583 | nfds = ns_select | |
4584 | #elif defined (HAVE_GLIB) | |
4585 | nfds = xg_select | |
edfda783 | 4586 | #else |
d35af63c | 4587 | nfds = pselect |
edfda783 | 4588 | #endif |
3fad2ad2 J |
4589 | (max (max_process_desc, max_input_desc) + 1, |
4590 | &Available, | |
7d652d97 | 4591 | (check_write ? &Writeok : 0), |
d35af63c | 4592 | NULL, &timeout, NULL); |
e061a11b TZ |
4593 | |
4594 | #ifdef HAVE_GNUTLS | |
4595 | /* GnuTLS buffers data internally. In lowat mode it leaves | |
4596 | some data in the TCP buffers so that select works, but | |
4597 | with custom pull/push functions we need to check if some | |
4598 | data is available in the buffers manually. */ | |
261b6fd4 LMI |
4599 | if (nfds == 0) |
4600 | { | |
4601 | if (! wait_proc) | |
4602 | { | |
4603 | /* We're not waiting on a specific process, so loop | |
16c1ad08 LMI |
4604 | through all the channels and check for data. |
4605 | This is a workaround needed for some versions of | |
4606 | the gnutls library -- 2.12.14 has been confirmed | |
4607 | to need it. See | |
4608 | http://comments.gmane.org/gmane.emacs.devel/145074 */ | |
d486344e | 4609 | for (channel = 0; channel < FD_SETSIZE; ++channel) |
5b76caa4 PE |
4610 | if (! NILP (chan_process[channel])) |
4611 | { | |
4612 | struct Lisp_Process *p = | |
4613 | XPROCESS (chan_process[channel]); | |
194b4d9f | 4614 | if (p && p->gnutls_p && p->gnutls_state && p->infd |
5b76caa4 PE |
4615 | && ((emacs_gnutls_record_check_pending |
4616 | (p->gnutls_state)) | |
4617 | > 0)) | |
4618 | { | |
4619 | nfds++; | |
4620 | FD_SET (p->infd, &Available); | |
4621 | } | |
4622 | } | |
261b6fd4 LMI |
4623 | } |
4624 | else | |
4625 | { | |
4626 | /* Check this specific channel. */ | |
5b76caa4 | 4627 | if (wait_proc->gnutls_p /* Check for valid process. */ |
69369809 | 4628 | && wait_proc->gnutls_state |
261b6fd4 | 4629 | /* Do we have pending data? */ |
5b76caa4 PE |
4630 | && ((emacs_gnutls_record_check_pending |
4631 | (wait_proc->gnutls_state)) | |
4632 | > 0)) | |
261b6fd4 LMI |
4633 | { |
4634 | nfds = 1; | |
4635 | /* Set to Available. */ | |
4636 | FD_SET (wait_proc->infd, &Available); | |
4637 | } | |
4638 | } | |
4639 | } | |
e061a11b | 4640 | #endif |
dd2a17ab | 4641 | } |
6720a7fb | 4642 | |
d0d6b7c5 JB |
4643 | xerrno = errno; |
4644 | ||
4645 | /* Make C-g and alarm signals set flags again */ | |
4646 | clear_waiting_for_input (); | |
4647 | ||
4648 | /* If we woke up due to SIGWINCH, actually change size now. */ | |
2b653806 | 4649 | do_pending_window_change (0); |
d0d6b7c5 | 4650 | |
209c6a58 CY |
4651 | if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers) |
4652 | /* We waited the full specified time, so return now. */ | |
4653 | break; | |
d0d6b7c5 JB |
4654 | if (nfds < 0) |
4655 | { | |
4656 | if (xerrno == EINTR) | |
dd2a17ab | 4657 | no_avail = 1; |
d0d6b7c5 | 4658 | else if (xerrno == EBADF) |
7e649856 | 4659 | emacs_abort (); |
d0d6b7c5 | 4660 | else |
4e604a5d | 4661 | report_file_errno ("Failed select", Qnil, xerrno); |
d0d6b7c5 | 4662 | } |
dd2a17ab KS |
4663 | |
4664 | if (no_avail) | |
4665 | { | |
4666 | FD_ZERO (&Available); | |
3fad2ad2 | 4667 | check_write = 0; |
dd2a17ab KS |
4668 | } |
4669 | ||
d0d6b7c5 JB |
4670 | /* Check for keyboard input */ |
4671 | /* If there is any, return immediately | |
4672 | to give it higher priority than subprocesses */ | |
4673 | ||
b89a415a | 4674 | if (read_kbd != 0) |
6ed6233b | 4675 | { |
9fa1de30 | 4676 | unsigned old_timers_run = timers_run; |
c88164fe | 4677 | struct buffer *old_buffer = current_buffer; |
c0323133 | 4678 | Lisp_Object old_window = selected_window; |
0bce5d9e | 4679 | bool leave = 0; |
177c0ea7 | 4680 | |
5d6c2aa3 | 4681 | if (detect_input_pending_run_timers (do_display)) |
a2fab450 GM |
4682 | { |
4683 | swallow_events (do_display); | |
4684 | if (detect_input_pending_run_timers (do_display)) | |
4685 | leave = 1; | |
4686 | } | |
6ed6233b | 4687 | |
a2fab450 GM |
4688 | /* If a timer has run, this might have changed buffers |
4689 | an alike. Make read_key_sequence aware of that. */ | |
4690 | if (timers_run != old_timers_run | |
c88164fe | 4691 | && waiting_for_user_input_p == -1 |
c0323133 GM |
4692 | && (old_buffer != current_buffer |
4693 | || !EQ (old_window, selected_window))) | |
a2fab450 GM |
4694 | record_asynch_buffer_change (); |
4695 | ||
4696 | if (leave) | |
4697 | break; | |
177c0ea7 JB |
4698 | } |
4699 | ||
69645afc | 4700 | /* If there is unread keyboard input, also return. */ |
b89a415a | 4701 | if (read_kbd != 0 |
69645afc RS |
4702 | && requeued_events_pending_p ()) |
4703 | break; | |
4704 | ||
77e1b3d4 RS |
4705 | /* If we are not checking for keyboard input now, |
4706 | do process events (but don't run any timers). | |
4707 | This is so that X events will be processed. | |
0c9960e9 | 4708 | Otherwise they may have to wait until polling takes place. |
77e1b3d4 RS |
4709 | That would causes delays in pasting selections, for example. |
4710 | ||
4711 | (We used to do this only if wait_for_cell.) */ | |
b89a415a | 4712 | if (read_kbd == 0 && detect_input_pending ()) |
f854a00b RS |
4713 | { |
4714 | swallow_events (do_display); | |
0c9960e9 | 4715 | #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */ |
f854a00b RS |
4716 | if (detect_input_pending ()) |
4717 | break; | |
5d5beb62 | 4718 | #endif |
0c9960e9 | 4719 | } |
f854a00b | 4720 | |
84aa3ace | 4721 | /* Exit now if the cell we're waiting for became non-nil. */ |
f3fbd155 | 4722 | if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) |
84aa3ace RS |
4723 | break; |
4724 | ||
4a4bbad2 | 4725 | #ifdef USABLE_SIGIO |
5d5beb62 | 4726 | /* If we think we have keyboard input waiting, but didn't get SIGIO, |
d0d6b7c5 JB |
4727 | go read it. This can happen with X on BSD after logging out. |
4728 | In that case, there really is no input and no SIGIO, | |
4729 | but select says there is input. */ | |
4730 | ||
b89a415a | 4731 | if (read_kbd && interrupt_input |
2601f59e | 4732 | && keyboard_bit_set (&Available) && ! noninteractive) |
4d7e6e51 | 4733 | handle_input_available_signal (SIGIO); |
4746118a | 4734 | #endif |
d0d6b7c5 | 4735 | |
d0d6b7c5 JB |
4736 | if (! wait_proc) |
4737 | got_some_input |= nfds > 0; | |
4738 | ||
32676c08 JB |
4739 | /* If checking input just got us a size-change event from X, |
4740 | obey it now if we should. */ | |
b89a415a | 4741 | if (read_kbd || ! NILP (wait_for_cell)) |
2b653806 | 4742 | do_pending_window_change (0); |
32676c08 | 4743 | |
a9f2c884 | 4744 | /* Check for data from a process. */ |
dd2a17ab KS |
4745 | if (no_avail || nfds == 0) |
4746 | continue; | |
4747 | ||
3fad2ad2 J |
4748 | for (channel = 0; channel <= max_input_desc; ++channel) |
4749 | { | |
4750 | struct fd_callback_data *d = &fd_callback_info[channel]; | |
de1339b0 PE |
4751 | if (d->func |
4752 | && ((d->condition & FOR_READ | |
4753 | && FD_ISSET (channel, &Available)) | |
4754 | || (d->condition & FOR_WRITE | |
4755 | && FD_ISSET (channel, &write_mask)))) | |
4756 | d->func (channel, d->data); | |
4757 | } | |
3fad2ad2 | 4758 | |
a69281ff | 4759 | for (channel = 0; channel <= max_process_desc; channel++) |
d0d6b7c5 | 4760 | { |
a69281ff | 4761 | if (FD_ISSET (channel, &Available) |
3fad2ad2 J |
4762 | && FD_ISSET (channel, &non_keyboard_wait_mask) |
4763 | && !FD_ISSET (channel, &non_process_wait_mask)) | |
d0d6b7c5 JB |
4764 | { |
4765 | int nread; | |
4766 | ||
4767 | /* If waiting for this channel, arrange to return as | |
4768 | soon as no more input to be processed. No more | |
4769 | waiting. */ | |
4770 | if (wait_channel == channel) | |
4771 | { | |
a9f2c884 | 4772 | wait_channel = -1; |
d35af63c | 4773 | nsecs = -1; |
d0d6b7c5 JB |
4774 | got_some_input = 1; |
4775 | } | |
4776 | proc = chan_process[channel]; | |
4777 | if (NILP (proc)) | |
4778 | continue; | |
4779 | ||
e690ca94 | 4780 | /* If this is a server stream socket, accept connection. */ |
4d2b044c | 4781 | if (EQ (XPROCESS (proc)->status, Qlisten)) |
e690ca94 KS |
4782 | { |
4783 | server_accept_connection (proc, channel); | |
4784 | continue; | |
4785 | } | |
4786 | ||
d0d6b7c5 JB |
4787 | /* Read data from the process, starting with our |
4788 | buffered-ahead character if we have one. */ | |
4789 | ||
4790 | nread = read_process_output (proc, channel); | |
4791 | if (nread > 0) | |
4792 | { | |
4793 | /* Since read_process_output can run a filter, | |
4794 | which can call accept-process-output, | |
4795 | don't try to read from any other processes | |
4796 | before doing the select again. */ | |
4797 | FD_ZERO (&Available); | |
4798 | ||
4799 | if (do_display) | |
3007ebfb | 4800 | redisplay_preserve_echo_area (12); |
d0d6b7c5 JB |
4801 | } |
4802 | #ifdef EWOULDBLOCK | |
4803 | else if (nread == -1 && errno == EWOULDBLOCK) | |
4804 | ; | |
0b75e9a4 | 4805 | #endif |
d0d6b7c5 JB |
4806 | else if (nread == -1 && errno == EAGAIN) |
4807 | ; | |
49cdacda PE |
4808 | #ifdef WINDOWSNT |
4809 | /* FIXME: Is this special case still needed? */ | |
d0d6b7c5 JB |
4810 | /* Note that we cannot distinguish between no input |
4811 | available now and a closed pipe. | |
4812 | With luck, a closed pipe will be accompanied by | |
4813 | subprocess termination and SIGCHLD. */ | |
d888760c | 4814 | else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc)) |
d0d6b7c5 | 4815 | ; |
49cdacda | 4816 | #endif |
d0d6b7c5 JB |
4817 | #ifdef HAVE_PTYS |
4818 | /* On some OSs with ptys, when the process on one end of | |
4819 | a pty exits, the other end gets an error reading with | |
4820 | errno = EIO instead of getting an EOF (0 bytes read). | |
4821 | Therefore, if we get an error reading and errno = | |
4822 | EIO, just continue, because the child process has | |
4823 | exited and should clean itself up soon (e.g. when we | |
207a7ef0 | 4824 | get a SIGCHLD). */ |
d0d6b7c5 | 4825 | else if (nread == -1 && errno == EIO) |
4740825c | 4826 | { |
f30d612a CY |
4827 | struct Lisp_Process *p = XPROCESS (proc); |
4828 | ||
4829 | /* Clear the descriptor now, so we only raise the | |
4830 | signal once. */ | |
4831 | FD_CLR (channel, &input_wait_mask); | |
4832 | FD_CLR (channel, &non_keyboard_wait_mask); | |
4833 | ||
4834 | if (p->pid == -2) | |
4835 | { | |
20ef56db PE |
4836 | /* If the EIO occurs on a pty, the SIGCHLD handler's |
4837 | waitpid call will not find the process object to | |
f30d612a CY |
4838 | delete. Do it here. */ |
4839 | p->tick = ++process_tick; | |
6a09a33b | 4840 | pset_status (p, Qfailed); |
f30d612a | 4841 | } |
4740825c KS |
4842 | } |
4843 | #endif /* HAVE_PTYS */ | |
5063c0e1 TN |
4844 | /* If we can detect process termination, don't consider the |
4845 | process gone just because its pipe is closed. */ | |
d888760c | 4846 | else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc)) |
d0d6b7c5 | 4847 | ; |
d0d6b7c5 JB |
4848 | else |
4849 | { | |
4850 | /* Preserve status of processes already terminated. */ | |
60f0fb11 | 4851 | XPROCESS (proc)->tick = ++process_tick; |
d0d6b7c5 | 4852 | deactivate_process (proc); |
6bfd98e7 | 4853 | if (XPROCESS (proc)->raw_status_new) |
d0d6b7c5 | 4854 | update_status (XPROCESS (proc)); |
4d2b044c | 4855 | if (EQ (XPROCESS (proc)->status, Qrun)) |
6a09a33b PE |
4856 | pset_status (XPROCESS (proc), |
4857 | list2 (Qexit, make_number (256))); | |
d0d6b7c5 JB |
4858 | } |
4859 | } | |
177c0ea7 | 4860 | #ifdef NON_BLOCKING_CONNECT |
3fad2ad2 | 4861 | if (FD_ISSET (channel, &Writeok) |
1566f511 | 4862 | && FD_ISSET (channel, &connect_wait_mask)) |
dd2a17ab KS |
4863 | { |
4864 | struct Lisp_Process *p; | |
dd2a17ab KS |
4865 | |
4866 | FD_CLR (channel, &connect_wait_mask); | |
d8a4b27a | 4867 | FD_CLR (channel, &write_mask); |
dd2a17ab | 4868 | if (--num_pending_connects < 0) |
1088b922 | 4869 | emacs_abort (); |
dd2a17ab KS |
4870 | |
4871 | proc = chan_process[channel]; | |
4872 | if (NILP (proc)) | |
4873 | continue; | |
4874 | ||
4875 | p = XPROCESS (proc); | |
4876 | ||
4877 | #ifdef GNU_LINUX | |
4878 | /* getsockopt(,,SO_ERROR,,) is said to hang on some systems. | |
63136da6 | 4879 | So only use it on systems where it is known to work. */ |
dd2a17ab | 4880 | { |
401bf9b4 | 4881 | socklen_t xlen = sizeof (xerrno); |
ed3751c8 | 4882 | if (getsockopt (channel, SOL_SOCKET, SO_ERROR, &xerrno, &xlen)) |
dd2a17ab KS |
4883 | xerrno = errno; |
4884 | } | |
4885 | #else | |
44c887be PJ |
4886 | { |
4887 | struct sockaddr pname; | |
fb83ea63 | 4888 | socklen_t pnamelen = sizeof (pname); |
44c887be PJ |
4889 | |
4890 | /* If connection failed, getpeername will fail. */ | |
4891 | xerrno = 0; | |
ed3751c8 | 4892 | if (getpeername (channel, &pname, &pnamelen) < 0) |
44c887be PJ |
4893 | { |
4894 | /* Obtain connect failure code through error slippage. */ | |
4895 | char dummy; | |
dd2a17ab | 4896 | xerrno = errno; |
ed3751c8 | 4897 | if (errno == ENOTCONN && read (channel, &dummy, 1) < 0) |
44c887be PJ |
4898 | xerrno = errno; |
4899 | } | |
4900 | } | |
dd2a17ab KS |
4901 | #endif |
4902 | if (xerrno) | |
4903 | { | |
60f0fb11 | 4904 | p->tick = ++process_tick; |
6a09a33b | 4905 | pset_status (p, list2 (Qfailed, make_number (xerrno))); |
dd2a17ab KS |
4906 | deactivate_process (proc); |
4907 | } | |
4908 | else | |
4909 | { | |
6a09a33b | 4910 | pset_status (p, Qrun); |
dd2a17ab KS |
4911 | /* Execute the sentinel here. If we had relied on |
4912 | status_notify to do it later, it will read input | |
4913 | from the process before calling the sentinel. */ | |
4914 | exec_sentinel (proc, build_string ("open\n")); | |
4d2b044c | 4915 | if (!EQ (p->filter, Qt) && !EQ (p->command, Qt)) |
dd2a17ab | 4916 | { |
60f0fb11 SM |
4917 | FD_SET (p->infd, &input_wait_mask); |
4918 | FD_SET (p->infd, &non_keyboard_wait_mask); | |
dd2a17ab KS |
4919 | } |
4920 | } | |
4921 | } | |
4922 | #endif /* NON_BLOCKING_CONNECT */ | |
1aa8d505 SM |
4923 | } /* End for each file descriptor. */ |
4924 | } /* End while exit conditions not met. */ | |
d0d6b7c5 | 4925 | |
2beb96f9 | 4926 | unbind_to (count, Qnil); |
d430ee71 | 4927 | |
ffd56f97 JB |
4928 | /* If calling from keyboard input, do not quit |
4929 | since we want to return C-g as an input character. | |
4930 | Otherwise, do pending quit if requested. */ | |
b89a415a | 4931 | if (read_kbd >= 0) |
ffd56f97 JB |
4932 | { |
4933 | /* Prevent input_pending from remaining set if we quit. */ | |
4934 | clear_input_pending (); | |
4935 | QUIT; | |
4936 | } | |
efa2a55c | 4937 | |
d0d6b7c5 JB |
4938 | return got_some_input; |
4939 | } | |
4940 | \f | |
3b9a3dfa RS |
4941 | /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */ |
4942 | ||
4943 | static Lisp_Object | |
d3da34e0 | 4944 | read_process_output_call (Lisp_Object fun_and_args) |
3b9a3dfa | 4945 | { |
70949dac | 4946 | return apply1 (XCAR (fun_and_args), XCDR (fun_and_args)); |
3b9a3dfa RS |
4947 | } |
4948 | ||
4949 | static Lisp_Object | |
b93aacde | 4950 | read_process_output_error_handler (Lisp_Object error_val) |
3b9a3dfa | 4951 | { |
b93aacde | 4952 | cmd_error_internal (error_val, "error in process filter: "); |
3b9a3dfa RS |
4953 | Vinhibit_quit = Qt; |
4954 | update_echo_area (); | |
833ba342 | 4955 | Fsleep_for (make_number (2), Qnil); |
8c983bf2 | 4956 | return Qt; |
3b9a3dfa RS |
4957 | } |
4958 | ||
1aa8d505 SM |
4959 | static void |
4960 | read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars, | |
4961 | ssize_t nbytes, | |
4962 | struct coding_system *coding); | |
4963 | ||
d0d6b7c5 JB |
4964 | /* Read pending output from the process channel, |
4965 | starting with our buffered-ahead character if we have one. | |
0fa1789e | 4966 | Yield number of decoded characters read. |
d0d6b7c5 | 4967 | |
116c423c | 4968 | This function reads at most 4096 characters. |
d0d6b7c5 | 4969 | If you want to read all available subprocess output, |
0fa1789e KH |
4970 | you must call it repeatedly until it returns zero. |
4971 | ||
4972 | The characters read are decoded according to PROC's coding-system | |
4973 | for decoding. */ | |
d0d6b7c5 | 4974 | |
ff6daed3 | 4975 | static int |
d3da34e0 | 4976 | read_process_output (Lisp_Object proc, register int channel) |
d0d6b7c5 | 4977 | { |
368f4090 | 4978 | register ssize_t nbytes; |
d0d6b7c5 | 4979 | char *chars; |
d0d6b7c5 | 4980 | register struct Lisp_Process *p = XPROCESS (proc); |
c7580538 | 4981 | struct coding_system *coding = proc_decode_coding_system[channel]; |
60f0fb11 | 4982 | int carryover = p->decoding_carryover; |
116c423c | 4983 | int readmax = 4096; |
d311d28c | 4984 | ptrdiff_t count = SPECPDL_INDEX (); |
3a4fa2f2 | 4985 | Lisp_Object odeactivate; |
d0d6b7c5 | 4986 | |
38182d90 | 4987 | chars = alloca (carryover + readmax); |
e7fbaa65 KH |
4988 | if (carryover) |
4989 | /* See the comment above. */ | |
4d2b044c | 4990 | memcpy (chars, SDATA (p->decoding_buf), carryover); |
0fa1789e | 4991 | |
e690ca94 KS |
4992 | #ifdef DATAGRAM_SOCKETS |
4993 | /* We have a working select, so proc_buffered_char is always -1. */ | |
4994 | if (DATAGRAM_CHAN_P (channel)) | |
4995 | { | |
401bf9b4 | 4996 | socklen_t len = datagram_address[channel].len; |
39b1da20 | 4997 | nbytes = recvfrom (channel, chars + carryover, readmax, |
e690ca94 KS |
4998 | 0, datagram_address[channel].sa, &len); |
4999 | } | |
5000 | else | |
5001 | #endif | |
6b61353c | 5002 | { |
908589fd | 5003 | bool buffered = proc_buffered_char[channel] >= 0; |
82eaa333 PE |
5004 | if (buffered) |
5005 | { | |
5006 | chars[carryover] = proc_buffered_char[channel]; | |
5007 | proc_buffered_char[channel] = -1; | |
5008 | } | |
8af55556 | 5009 | #ifdef HAVE_GNUTLS |
194b4d9f | 5010 | if (p->gnutls_p && p->gnutls_state) |
959ad23f | 5011 | nbytes = emacs_gnutls_read (p, chars + carryover + buffered, |
82eaa333 | 5012 | readmax - buffered); |
8af55556 TZ |
5013 | else |
5014 | #endif | |
82eaa333 PE |
5015 | nbytes = emacs_read (channel, chars + carryover + buffered, |
5016 | readmax - buffered); | |
6b61353c | 5017 | #ifdef ADAPTIVE_READ_BUFFERING |
60f0fb11 | 5018 | if (nbytes > 0 && p->adaptive_read_buffering) |
6b61353c | 5019 | { |
60f0fb11 | 5020 | int delay = p->read_output_delay; |
6b61353c KH |
5021 | if (nbytes < 256) |
5022 | { | |
5023 | if (delay < READ_OUTPUT_DELAY_MAX_MAX) | |
5024 | { | |
5025 | if (delay == 0) | |
5026 | process_output_delay_count++; | |
5027 | delay += READ_OUTPUT_DELAY_INCREMENT * 2; | |
5028 | } | |
5029 | } | |
82eaa333 | 5030 | else if (delay > 0 && nbytes == readmax - buffered) |
6b61353c KH |
5031 | { |
5032 | delay -= READ_OUTPUT_DELAY_INCREMENT; | |
5033 | if (delay == 0) | |
5034 | process_output_delay_count--; | |
5035 | } | |
60f0fb11 | 5036 | p->read_output_delay = delay; |
6b61353c KH |
5037 | if (delay) |
5038 | { | |
60f0fb11 | 5039 | p->read_output_skip = 1; |
6b61353c KH |
5040 | process_output_skip = 1; |
5041 | } | |
5042 | } | |
5043 | #endif | |
fe07cdfa PE |
5044 | nbytes += buffered; |
5045 | nbytes += buffered && nbytes <= 0; | |
d0d6b7c5 | 5046 | } |
d0d6b7c5 | 5047 | |
60f0fb11 | 5048 | p->decoding_carryover = 0; |
ca65341e | 5049 | |
ed7a4b2d | 5050 | /* At this point, NBYTES holds number of bytes just received |
0fa1789e | 5051 | (including the one in proc_buffered_char[channel]). */ |
de7fbd09 KH |
5052 | if (nbytes <= 0) |
5053 | { | |
5054 | if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK) | |
5055 | return nbytes; | |
5056 | coding->mode |= CODING_MODE_LAST_BLOCK; | |
5057 | } | |
d0d6b7c5 | 5058 | |
1d2fc612 | 5059 | /* Now set NBYTES how many bytes we must decode. */ |
e7fbaa65 | 5060 | nbytes += carryover; |
0fa1789e | 5061 | |
3a4fa2f2 SM |
5062 | odeactivate = Vdeactivate_mark; |
5063 | /* There's no good reason to let process filters change the current | |
5064 | buffer, and many callers of accept-process-output, sit-for, and | |
5065 | friends don't expect current-buffer to be changed from under them. */ | |
66322887 | 5066 | record_unwind_current_buffer (); |
3a4fa2f2 | 5067 | |
1aa8d505 SM |
5068 | read_and_dispose_of_process_output (p, chars, nbytes, coding); |
5069 | ||
5070 | /* Handling the process output should not deactivate the mark. */ | |
5071 | Vdeactivate_mark = odeactivate; | |
5072 | ||
5073 | unbind_to (count, Qnil); | |
5074 | return nbytes; | |
5075 | } | |
5076 | ||
5077 | static void | |
5078 | read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars, | |
5079 | ssize_t nbytes, | |
5080 | struct coding_system *coding) | |
5081 | { | |
5082 | Lisp_Object outstream = p->filter; | |
5083 | Lisp_Object text; | |
5084 | bool outer_running_asynch_code = running_asynch_code; | |
5085 | int waiting = waiting_for_user_input_p; | |
30c78175 | 5086 | |
1aa8d505 SM |
5087 | /* No need to gcpro these, because all we do with them later |
5088 | is test them for EQness, and none of them should be a string. */ | |
aba7731a | 5089 | #if 0 |
1aa8d505 SM |
5090 | Lisp_Object obuffer, okeymap; |
5091 | XSETBUFFER (obuffer, current_buffer); | |
5092 | okeymap = BVAR (current_buffer, keymap); | |
aba7731a | 5093 | #endif |
30c78175 | 5094 | |
1aa8d505 SM |
5095 | /* We inhibit quit here instead of just catching it so that |
5096 | hitting ^G when a filter happens to be running won't screw | |
5097 | it up. */ | |
5098 | specbind (Qinhibit_quit, Qt); | |
5099 | specbind (Qlast_nonmenu_event, Qt); | |
4da2f5be | 5100 | |
1aa8d505 SM |
5101 | /* In case we get recursively called, |
5102 | and we already saved the match data nonrecursively, | |
5103 | save the same match data in safely recursive fashion. */ | |
5104 | if (outer_running_asynch_code) | |
5105 | { | |
5106 | Lisp_Object tem; | |
5107 | /* Don't clobber the CURRENT match data, either! */ | |
5108 | tem = Fmatch_data (Qnil, Qnil, Qnil); | |
5109 | restore_search_regs (); | |
5110 | record_unwind_save_match_data (); | |
5111 | Fset_match_data (tem, Qt); | |
5112 | } | |
4da2f5be | 5113 | |
1aa8d505 SM |
5114 | /* For speed, if a search happens within this code, |
5115 | save the match data in a special nonrecursive fashion. */ | |
5116 | running_asynch_code = 1; | |
ed7a4b2d | 5117 | |
1aa8d505 SM |
5118 | decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt); |
5119 | text = coding->dst_object; | |
5120 | Vlast_coding_system_used = CODING_ID_NAME (coding->id); | |
5121 | /* A new coding system might be found. */ | |
5122 | if (!EQ (p->decode_coding_system, Vlast_coding_system_used)) | |
5123 | { | |
5124 | pset_decode_coding_system (p, Vlast_coding_system_used); | |
ed7a4b2d | 5125 | |
1aa8d505 SM |
5126 | /* Don't call setup_coding_system for |
5127 | proc_decode_coding_system[channel] here. It is done in | |
5128 | detect_coding called via decode_coding above. */ | |
ed7a4b2d | 5129 | |
1aa8d505 SM |
5130 | /* If a coding system for encoding is not yet decided, we set |
5131 | it as the same as coding-system for decoding. | |
51c6067d | 5132 | |
1aa8d505 SM |
5133 | But, before doing that we must check if |
5134 | proc_encode_coding_system[p->outfd] surely points to a | |
5135 | valid memory because p->outfd will be changed once EOF is | |
5136 | sent to the process. */ | |
5137 | if (NILP (p->encode_coding_system) | |
5138 | && proc_encode_coding_system[p->outfd]) | |
dd8c7a53 | 5139 | { |
1aa8d505 SM |
5140 | pset_encode_coding_system |
5141 | (p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil)); | |
5142 | setup_coding_system (p->encode_coding_system, | |
5143 | proc_encode_coding_system[p->outfd]); | |
dd8c7a53 | 5144 | } |
1aa8d505 | 5145 | } |
d0d6b7c5 | 5146 | |
1aa8d505 SM |
5147 | if (coding->carryover_bytes > 0) |
5148 | { | |
5149 | if (SCHARS (p->decoding_buf) < coding->carryover_bytes) | |
5150 | pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes)); | |
5151 | memcpy (SDATA (p->decoding_buf), coding->carryover, | |
5152 | coding->carryover_bytes); | |
5153 | p->decoding_carryover = coding->carryover_bytes; | |
5154 | } | |
5155 | if (SBYTES (text) > 0) | |
5156 | /* FIXME: It's wrong to wrap or not based on debug-on-error, and | |
5157 | sometimes it's simply wrong to wrap (e.g. when called from | |
5158 | accept-process-output). */ | |
5159 | internal_condition_case_1 (read_process_output_call, | |
6c6f1994 | 5160 | list3 (outstream, make_lisp_proc (p), text), |
1aa8d505 SM |
5161 | !NILP (Vdebug_on_error) ? Qnil : Qerror, |
5162 | read_process_output_error_handler); | |
5163 | ||
5164 | /* If we saved the match data nonrecursively, restore it now. */ | |
5165 | restore_search_regs (); | |
5166 | running_asynch_code = outer_running_asynch_code; | |
5167 | ||
5168 | /* Restore waiting_for_user_input_p as it was | |
5169 | when we were called, in case the filter clobbered it. */ | |
5170 | waiting_for_user_input_p = waiting; | |
bbce7d72 | 5171 | |
7973cfa8 RS |
5172 | #if 0 /* Call record_asynch_buffer_change unconditionally, |
5173 | because we might have changed minor modes or other things | |
5174 | that affect key bindings. */ | |
1aa8d505 SM |
5175 | if (! EQ (Fcurrent_buffer (), obuffer) |
5176 | || ! EQ (current_buffer->keymap, okeymap)) | |
5177 | #endif | |
5178 | /* But do it only if the caller is actually going to read events. | |
5179 | Otherwise there's no need to make him wake up, and it could | |
5180 | cause trouble (for example it would make sit_for return). */ | |
5181 | if (waiting_for_user_input_p == -1) | |
5182 | record_asynch_buffer_change (); | |
5183 | } | |
5184 | ||
5185 | DEFUN ("internal-default-process-filter", Finternal_default_process_filter, | |
5186 | Sinternal_default_process_filter, 2, 2, 0, | |
5187 | doc: /* Function used as default process filter. */) | |
5188 | (Lisp_Object proc, Lisp_Object text) | |
5189 | { | |
5190 | struct Lisp_Process *p; | |
5191 | ptrdiff_t opoint; | |
d0d6b7c5 | 5192 | |
1aa8d505 SM |
5193 | CHECK_PROCESS (proc); |
5194 | p = XPROCESS (proc); | |
5195 | CHECK_STRING (text); | |
5196 | ||
5197 | if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer))) | |
d0d6b7c5 | 5198 | { |
b0310da4 | 5199 | Lisp_Object old_read_only; |
d311d28c PE |
5200 | ptrdiff_t old_begv, old_zv; |
5201 | ptrdiff_t old_begv_byte, old_zv_byte; | |
5202 | ptrdiff_t before, before_byte; | |
5203 | ptrdiff_t opoint_byte; | |
926b7e5e | 5204 | struct buffer *b; |
30c78175 | 5205 | |
4d2b044c | 5206 | Fset_buffer (p->buffer); |
6ec8bbd2 | 5207 | opoint = PT; |
d8a2934e | 5208 | opoint_byte = PT_BYTE; |
4b4deea2 | 5209 | old_read_only = BVAR (current_buffer, read_only); |
12ca5cdf RS |
5210 | old_begv = BEGV; |
5211 | old_zv = ZV; | |
d8a2934e RS |
5212 | old_begv_byte = BEGV_BYTE; |
5213 | old_zv_byte = ZV_BYTE; | |
b0310da4 | 5214 | |
39eb03f1 | 5215 | bset_read_only (current_buffer, Qnil); |
d0d6b7c5 | 5216 | |
032f7451 DA |
5217 | /* Insert new output into buffer at the current end-of-output |
5218 | marker, thus preserving logical ordering of input and output. */ | |
4d2b044c | 5219 | if (XMARKER (p->mark)->buffer) |
032f7451 | 5220 | set_point_from_marker (p->mark); |
d0d6b7c5 | 5221 | else |
d8a2934e | 5222 | SET_PT_BOTH (ZV, ZV_BYTE); |
12ca5cdf | 5223 | before = PT; |
d8a2934e | 5224 | before_byte = PT_BYTE; |
b0310da4 JB |
5225 | |
5226 | /* If the output marker is outside of the visible region, save | |
5227 | the restriction and widen. */ | |
6ec8bbd2 | 5228 | if (! (BEGV <= PT && PT <= ZV)) |
b0310da4 JB |
5229 | Fwiden (); |
5230 | ||
d69864bf | 5231 | /* Adjust the multibyteness of TEXT to that of the buffer. */ |
4b4deea2 | 5232 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
d69864bf KH |
5233 | != ! STRING_MULTIBYTE (text)) |
5234 | text = (STRING_MULTIBYTE (text) | |
57bb5c37 | 5235 | ? Fstring_as_unibyte (text) |
03f04413 | 5236 | : Fstring_to_multibyte (text)); |
57bb5c37 KH |
5237 | /* Insert before markers in case we are inserting where |
5238 | the buffer's mark is, and the user's next command is Meta-y. */ | |
e430e5ba KH |
5239 | insert_from_string_before_markers (text, 0, 0, |
5240 | SCHARS (text), SBYTES (text), 0); | |
0d023da1 | 5241 | |
926b7e5e GM |
5242 | /* Make sure the process marker's position is valid when the |
5243 | process buffer is changed in the signal_after_change above. | |
5244 | W3 is known to do that. */ | |
4d2b044c DA |
5245 | if (BUFFERP (p->buffer) |
5246 | && (b = XBUFFER (p->buffer), b != current_buffer)) | |
5247 | set_marker_both (p->mark, p->buffer, BUF_PT (b), BUF_PT_BYTE (b)); | |
926b7e5e | 5248 | else |
4d2b044c | 5249 | set_marker_both (p->mark, p->buffer, PT, PT_BYTE); |
b0310da4 | 5250 | |
2ec9db5d | 5251 | update_mode_lines = 23; |
d0d6b7c5 | 5252 | |
12ca5cdf RS |
5253 | /* Make sure opoint and the old restrictions |
5254 | float ahead of any new text just as point would. */ | |
5255 | if (opoint >= before) | |
d8a2934e RS |
5256 | { |
5257 | opoint += PT - before; | |
5258 | opoint_byte += PT_BYTE - before_byte; | |
5259 | } | |
12ca5cdf | 5260 | if (old_begv > before) |
d8a2934e RS |
5261 | { |
5262 | old_begv += PT - before; | |
5263 | old_begv_byte += PT_BYTE - before_byte; | |
5264 | } | |
12ca5cdf | 5265 | if (old_zv >= before) |
d8a2934e RS |
5266 | { |
5267 | old_zv += PT - before; | |
5268 | old_zv_byte += PT_BYTE - before_byte; | |
5269 | } | |
12ca5cdf | 5270 | |
b0310da4 | 5271 | /* If the restriction isn't what it should be, set it. */ |
12ca5cdf RS |
5272 | if (old_begv != BEGV || old_zv != ZV) |
5273 | Fnarrow_to_region (make_number (old_begv), make_number (old_zv)); | |
b0310da4 | 5274 | |
39eb03f1 | 5275 | bset_read_only (current_buffer, old_read_only); |
d8a2934e | 5276 | SET_PT_BOTH (opoint, opoint_byte); |
d0d6b7c5 | 5277 | } |
1aa8d505 | 5278 | return Qnil; |
d0d6b7c5 | 5279 | } |
d0d6b7c5 | 5280 | \f |
1aa8d505 | 5281 | /* Sending data to subprocess. */ |
d0d6b7c5 | 5282 | |
20ca2e94 TN |
5283 | /* In send_process, when a write fails temporarily, |
5284 | wait_reading_process_output is called. It may execute user code, | |
5285 | e.g. timers, that attempts to write new data to the same process. | |
5286 | We must ensure that data is sent in the right order, and not | |
5287 | interspersed half-completed with other writes (Bug#10815). This is | |
5288 | handled by the write_queue element of struct process. It is a list | |
5289 | with each entry having the form | |
5290 | ||
5291 | (string . (offset . length)) | |
5292 | ||
5293 | where STRING is a lisp string, OFFSET is the offset into the | |
5294 | string's byte sequence from which we should begin to send, and | |
5295 | LENGTH is the number of bytes left to send. */ | |
5296 | ||
5297 | /* Create a new entry in write_queue. | |
5298 | INPUT_OBJ should be a buffer, string Qt, or Qnil. | |
5299 | BUF is a pointer to the string sequence of the input_obj or a C | |
5300 | string in case of Qt or Qnil. */ | |
5301 | ||
5302 | static void | |
5303 | write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj, | |
0bce5d9e | 5304 | const char *buf, ptrdiff_t len, bool front) |
20ca2e94 | 5305 | { |
7ea2b339 | 5306 | ptrdiff_t offset; |
20ca2e94 TN |
5307 | Lisp_Object entry, obj; |
5308 | ||
5309 | if (STRINGP (input_obj)) | |
5310 | { | |
5311 | offset = buf - SSDATA (input_obj); | |
5312 | obj = input_obj; | |
5313 | } | |
5314 | else | |
5315 | { | |
5316 | offset = 0; | |
5317 | obj = make_unibyte_string (buf, len); | |
5318 | } | |
5319 | ||
5320 | entry = Fcons (obj, Fcons (make_number (offset), make_number (len))); | |
5321 | ||
5322 | if (front) | |
6a09a33b | 5323 | pset_write_queue (p, Fcons (entry, p->write_queue)); |
20ca2e94 | 5324 | else |
6c6f1994 | 5325 | pset_write_queue (p, nconc2 (p->write_queue, list1 (entry))); |
20ca2e94 TN |
5326 | } |
5327 | ||
5328 | /* Remove the first element in the write_queue of process P, put its | |
0bce5d9e PE |
5329 | contents in OBJ, BUF and LEN, and return true. If the |
5330 | write_queue is empty, return false. */ | |
20ca2e94 | 5331 | |
0bce5d9e | 5332 | static bool |
20ca2e94 | 5333 | write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj, |
7ea2b339 | 5334 | const char **buf, ptrdiff_t *len) |
20ca2e94 TN |
5335 | { |
5336 | Lisp_Object entry, offset_length; | |
7ea2b339 | 5337 | ptrdiff_t offset; |
20ca2e94 | 5338 | |
4d2b044c | 5339 | if (NILP (p->write_queue)) |
20ca2e94 TN |
5340 | return 0; |
5341 | ||
4d2b044c | 5342 | entry = XCAR (p->write_queue); |
6a09a33b | 5343 | pset_write_queue (p, XCDR (p->write_queue)); |
20ca2e94 TN |
5344 | |
5345 | *obj = XCAR (entry); | |
5346 | offset_length = XCDR (entry); | |
5347 | ||
5348 | *len = XINT (XCDR (offset_length)); | |
5349 | offset = XINT (XCAR (offset_length)); | |
7ea2b339 | 5350 | *buf = SSDATA (*obj) + offset; |
20ca2e94 TN |
5351 | |
5352 | return 1; | |
5353 | } | |
5354 | ||
4556b700 RS |
5355 | /* Send some data to process PROC. |
5356 | BUF is the beginning of the data; LEN is the number of characters. | |
a92e4183 KH |
5357 | OBJECT is the Lisp object that the data comes from. If OBJECT is |
5358 | nil or t, it means that the data comes from C string. | |
0fa1789e | 5359 | |
a92e4183 KH |
5360 | If OBJECT is not nil, the data is encoded by PROC's coding-system |
5361 | for encoding before it is sent. | |
1fb0098c GM |
5362 | |
5363 | This function can evaluate Lisp code and can garbage collect. */ | |
4556b700 | 5364 | |
ff6daed3 | 5365 | static void |
4d7e6e51 PE |
5366 | send_process (Lisp_Object proc, const char *buf, ptrdiff_t len, |
5367 | Lisp_Object object) | |
d0d6b7c5 | 5368 | { |
6b61353c | 5369 | struct Lisp_Process *p = XPROCESS (proc); |
368f4090 | 5370 | ssize_t rv; |
0fa1789e | 5371 | struct coding_system *coding; |
6044e593 | 5372 | |
6bfd98e7 | 5373 | if (p->raw_status_new) |
6b61353c | 5374 | update_status (p); |
4d2b044c DA |
5375 | if (! EQ (p->status, Qrun)) |
5376 | error ("Process %s not running", SDATA (p->name)); | |
60f0fb11 | 5377 | if (p->outfd < 0) |
4d2b044c | 5378 | error ("Output file descriptor of %s is closed", SDATA (p->name)); |
0fa1789e | 5379 | |
60f0fb11 | 5380 | coding = proc_encode_coding_system[p->outfd]; |
5bbf78c1 | 5381 | Vlast_coding_system_used = CODING_ID_NAME (coding->id); |
486b111b | 5382 | |
ed7a4b2d KH |
5383 | if ((STRINGP (object) && STRING_MULTIBYTE (object)) |
5384 | || (BUFFERP (object) | |
4b4deea2 | 5385 | && !NILP (BVAR (XBUFFER (object), enable_multibyte_characters))) |
a92e4183 | 5386 | || EQ (object, Qt)) |
278bfdd6 | 5387 | { |
6a09a33b PE |
5388 | pset_encode_coding_system |
5389 | (p, complement_process_encoding_system (p->encode_coding_system)); | |
4d2b044c | 5390 | if (!EQ (Vlast_coding_system_used, p->encode_coding_system)) |
fcaf8878 KH |
5391 | { |
5392 | /* The coding system for encoding was changed to raw-text | |
5393 | because we sent a unibyte text previously. Now we are | |
5394 | sending a multibyte text, thus we must encode it by the | |
5395 | original coding system specified for the current process. | |
5396 | ||
da6062e6 PE |
5397 | Another reason we come here is that the coding system |
5398 | was just complemented and a new one was returned by | |
fcaf8878 | 5399 | complement_process_encoding_system. */ |
4d2b044c DA |
5400 | setup_coding_system (p->encode_coding_system, coding); |
5401 | Vlast_coding_system_used = p->encode_coding_system; | |
fcaf8878 | 5402 | } |
e08bbb84 | 5403 | coding->src_multibyte = 1; |
278bfdd6 KH |
5404 | } |
5405 | else | |
5406 | { | |
a0241d01 | 5407 | coding->src_multibyte = 0; |
a92e4183 KH |
5408 | /* For sending a unibyte text, character code conversion should |
5409 | not take place but EOL conversion should. So, setup raw-text | |
5410 | or one of the subsidiary if we have not yet done it. */ | |
5bbf78c1 | 5411 | if (CODING_REQUIRE_ENCODING (coding)) |
a92e4183 KH |
5412 | { |
5413 | if (CODING_REQUIRE_FLUSHING (coding)) | |
5414 | { | |
5415 | /* But, before changing the coding, we must flush out data. */ | |
5416 | coding->mode |= CODING_MODE_LAST_BLOCK; | |
5417 | send_process (proc, "", 0, Qt); | |
8f924df7 | 5418 | coding->mode &= CODING_MODE_LAST_BLOCK; |
a92e4183 | 5419 | } |
5bbf78c1 KH |
5420 | setup_coding_system (raw_text_coding_system |
5421 | (Vlast_coding_system_used), | |
5422 | coding); | |
e08bbb84 | 5423 | coding->src_multibyte = 0; |
a92e4183 | 5424 | } |
278bfdd6 | 5425 | } |
a4a37e65 KH |
5426 | coding->dst_multibyte = 0; |
5427 | ||
ed7a4b2d | 5428 | if (CODING_REQUIRE_ENCODING (coding)) |
0fa1789e | 5429 | { |
5bbf78c1 | 5430 | coding->dst_object = Qt; |
ed7a4b2d | 5431 | if (BUFFERP (object)) |
0fa1789e | 5432 | { |
d311d28c PE |
5433 | ptrdiff_t from_byte, from, to; |
5434 | ptrdiff_t save_pt, save_pt_byte; | |
5bbf78c1 KH |
5435 | struct buffer *cur = current_buffer; |
5436 | ||
5437 | set_buffer_internal (XBUFFER (object)); | |
5438 | save_pt = PT, save_pt_byte = PT_BYTE; | |
5439 | ||
400a67ff | 5440 | from_byte = PTR_BYTE_POS ((unsigned char *) buf); |
5bbf78c1 KH |
5441 | from = BYTE_TO_CHAR (from_byte); |
5442 | to = BYTE_TO_CHAR (from_byte + len); | |
5443 | TEMP_SET_PT_BOTH (from, from_byte); | |
5444 | encode_coding_object (coding, object, from, from_byte, | |
5445 | to, from_byte + len, Qt); | |
5446 | TEMP_SET_PT_BOTH (save_pt, save_pt_byte); | |
5447 | set_buffer_internal (cur); | |
ed7a4b2d KH |
5448 | } |
5449 | else if (STRINGP (object)) | |
5450 | { | |
550c8289 KH |
5451 | encode_coding_object (coding, object, 0, 0, SCHARS (object), |
5452 | SBYTES (object), Qt); | |
0fa1789e | 5453 | } |
5bbf78c1 | 5454 | else |
452294c2 | 5455 | { |
5bbf78c1 KH |
5456 | coding->dst_object = make_unibyte_string (buf, len); |
5457 | coding->produced = len; | |
452294c2 | 5458 | } |
ed7a4b2d | 5459 | |
e7fbaa65 | 5460 | len = coding->produced; |
347f3cd3 | 5461 | object = coding->dst_object; |
400a67ff | 5462 | buf = SSDATA (object); |
0fa1789e | 5463 | } |
d0d6b7c5 | 5464 | |
4d7e6e51 PE |
5465 | /* If there is already data in the write_queue, put the new data |
5466 | in the back of queue. Otherwise, ignore it. */ | |
5467 | if (!NILP (p->write_queue)) | |
5468 | write_queue_push (p, object, buf, len, 0); | |
20ca2e94 | 5469 | |
4d7e6e51 PE |
5470 | do /* while !NILP (p->write_queue) */ |
5471 | { | |
5472 | ptrdiff_t cur_len = -1; | |
5473 | const char *cur_buf; | |
5474 | Lisp_Object cur_object; | |
20ca2e94 | 5475 | |
4d7e6e51 PE |
5476 | /* If write_queue is empty, ignore it. */ |
5477 | if (!write_queue_pop (p, &cur_object, &cur_buf, &cur_len)) | |
0daad115 | 5478 | { |
4d7e6e51 PE |
5479 | cur_len = len; |
5480 | cur_buf = buf; | |
5481 | cur_object = object; | |
5482 | } | |
20ca2e94 | 5483 | |
4d7e6e51 PE |
5484 | while (cur_len > 0) |
5485 | { | |
5486 | /* Send this batch, using one or more write calls. */ | |
5487 | ptrdiff_t written = 0; | |
5488 | int outfd = p->outfd; | |
5489 | #ifdef DATAGRAM_SOCKETS | |
5490 | if (DATAGRAM_CHAN_P (outfd)) | |
20ca2e94 | 5491 | { |
4d7e6e51 PE |
5492 | rv = sendto (outfd, cur_buf, cur_len, |
5493 | 0, datagram_address[outfd].sa, | |
5494 | datagram_address[outfd].len); | |
908589fd | 5495 | if (rv >= 0) |
4d7e6e51 PE |
5496 | written = rv; |
5497 | else if (errno == EMSGSIZE) | |
4e604a5d | 5498 | report_file_error ("Sending datagram", proc); |
20ca2e94 | 5499 | } |
4d7e6e51 | 5500 | else |
e690ca94 | 5501 | #endif |
4d7e6e51 | 5502 | { |
8af55556 | 5503 | #ifdef HAVE_GNUTLS |
194b4d9f | 5504 | if (p->gnutls_p && p->gnutls_state) |
4d7e6e51 PE |
5505 | written = emacs_gnutls_write (p, cur_buf, cur_len); |
5506 | else | |
8af55556 | 5507 | #endif |
4ebbdd67 | 5508 | written = emacs_write_sig (outfd, cur_buf, cur_len); |
4d7e6e51 | 5509 | rv = (written ? 0 : -1); |
6b61353c | 5510 | #ifdef ADAPTIVE_READ_BUFFERING |
4d7e6e51 PE |
5511 | if (p->read_output_delay > 0 |
5512 | && p->adaptive_read_buffering == 1) | |
5513 | { | |
5514 | p->read_output_delay = 0; | |
5515 | process_output_delay_count--; | |
5516 | p->read_output_skip = 0; | |
6b61353c | 5517 | } |
4d7e6e51 PE |
5518 | #endif |
5519 | } | |
4556b700 | 5520 | |
4d7e6e51 PE |
5521 | if (rv < 0) |
5522 | { | |
22626a85 | 5523 | if (errno == EAGAIN |
d0d6b7c5 | 5524 | #ifdef EWOULDBLOCK |
4d7e6e51 | 5525 | || errno == EWOULDBLOCK |
d0d6b7c5 | 5526 | #endif |
4d7e6e51 PE |
5527 | ) |
5528 | /* Buffer is full. Wait, accepting input; | |
5529 | that may allow the program | |
5530 | to finish doing output and read more. */ | |
5531 | { | |
3433b6bd | 5532 | #ifdef BROKEN_PTY_READ_AFTER_EAGAIN |
4d7e6e51 PE |
5533 | /* A gross hack to work around a bug in FreeBSD. |
5534 | In the following sequence, read(2) returns | |
5535 | bogus data: | |
5536 | ||
5537 | write(2) 1022 bytes | |
5538 | write(2) 954 bytes, get EAGAIN | |
5539 | read(2) 1024 bytes in process_read_output | |
5540 | read(2) 11 bytes in process_read_output | |
5541 | ||
5542 | That is, read(2) returns more bytes than have | |
5543 | ever been written successfully. The 1033 bytes | |
5544 | read are the 1022 bytes written successfully | |
5545 | after processing (for example with CRs added if | |
5546 | the terminal is set up that way which it is | |
5547 | here). The same bytes will be seen again in a | |
5548 | later read(2), without the CRs. */ | |
5549 | ||
5550 | if (errno == EAGAIN) | |
5551 | { | |
5552 | int flags = FWRITE; | |
5553 | ioctl (p->outfd, TIOCFLUSH, &flags); | |
5554 | } | |
3433b6bd | 5555 | #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */ |
177c0ea7 | 5556 | |
4d7e6e51 PE |
5557 | /* Put what we should have written in wait_queue. */ |
5558 | write_queue_push (p, cur_object, cur_buf, cur_len, 1); | |
5559 | wait_reading_process_output (0, 20 * 1000 * 1000, | |
5560 | 0, 0, Qnil, NULL, 0); | |
5561 | /* Reread queue, to see what is left. */ | |
5562 | break; | |
0daad115 | 5563 | } |
4d7e6e51 PE |
5564 | else if (errno == EPIPE) |
5565 | { | |
5566 | p->raw_status_new = 0; | |
5567 | pset_status (p, list2 (Qexit, make_number (256))); | |
5568 | p->tick = ++process_tick; | |
5569 | deactivate_process (proc); | |
5570 | error ("process %s no longer connected to pipe; closed it", | |
5571 | SDATA (p->name)); | |
5572 | } | |
5573 | else | |
5574 | /* This is a real error. */ | |
4e604a5d | 5575 | report_file_error ("Writing to process", proc); |
0daad115 | 5576 | } |
4d7e6e51 PE |
5577 | cur_buf += written; |
5578 | cur_len -= written; | |
0daad115 | 5579 | } |
d0d6b7c5 | 5580 | } |
4d7e6e51 | 5581 | while (!NILP (p->write_queue)); |
d0d6b7c5 JB |
5582 | } |
5583 | ||
5584 | DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region, | |
fdb82f93 PJ |
5585 | 3, 3, 0, |
5586 | doc: /* Send current contents of region as input to PROCESS. | |
5587 | PROCESS may be a process, a buffer, the name of a process or buffer, or | |
5588 | nil, indicating the current buffer's process. | |
5589 | Called from program, takes three arguments, PROCESS, START and END. | |
5590 | If the region is more than 500 characters long, | |
5591 | it is sent in several bunches. This may happen even for shorter regions. | |
5592 | Output from processes can arrive in between bunches. */) | |
5842a27b | 5593 | (Lisp_Object process, Lisp_Object start, Lisp_Object end) |
d0d6b7c5 | 5594 | { |
13002885 DA |
5595 | Lisp_Object proc = get_process (process); |
5596 | ptrdiff_t start_byte, end_byte; | |
d0d6b7c5 | 5597 | |
d0d6b7c5 | 5598 | validate_region (&start, &end); |
1b234f64 | 5599 | |
13002885 DA |
5600 | start_byte = CHAR_TO_BYTE (XINT (start)); |
5601 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
5602 | ||
1b234f64 | 5603 | if (XINT (start) < GPT && XINT (end) > GPT) |
13002885 | 5604 | move_gap_both (XINT (start), start_byte); |
1b234f64 | 5605 | |
e454145b | 5606 | send_process (proc, (char *) BYTE_POS_ADDR (start_byte), |
13002885 | 5607 | end_byte - start_byte, Fcurrent_buffer ()); |
1b234f64 | 5608 | |
d0d6b7c5 JB |
5609 | return Qnil; |
5610 | } | |
5611 | ||
5612 | DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string, | |
fdb82f93 PJ |
5613 | 2, 2, 0, |
5614 | doc: /* Send PROCESS the contents of STRING as input. | |
5615 | PROCESS may be a process, a buffer, the name of a process or buffer, or | |
5616 | nil, indicating the current buffer's process. | |
5617 | If STRING is more than 500 characters long, | |
5618 | it is sent in several bunches. This may happen even for shorter strings. | |
5619 | Output from processes can arrive in between bunches. */) | |
5842a27b | 5620 | (Lisp_Object process, Lisp_Object string) |
d0d6b7c5 JB |
5621 | { |
5622 | Lisp_Object proc; | |
b7826503 | 5623 | CHECK_STRING (string); |
d0d6b7c5 | 5624 | proc = get_process (process); |
400a67ff | 5625 | send_process (proc, SSDATA (string), |
1b234f64 | 5626 | SBYTES (string), string); |
d0d6b7c5 JB |
5627 | return Qnil; |
5628 | } | |
5629 | \f | |
16782258 JD |
5630 | /* Return the foreground process group for the tty/pty that |
5631 | the process P uses. */ | |
dde14581 | 5632 | static pid_t |
d3da34e0 | 5633 | emacs_get_tty_pgrp (struct Lisp_Process *p) |
16782258 | 5634 | { |
dde14581 | 5635 | pid_t gid = -1; |
16782258 | 5636 | |
f3d9532b | 5637 | #ifdef TIOCGPGRP |
4d2b044c | 5638 | if (ioctl (p->infd, TIOCGPGRP, &gid) == -1 && ! NILP (p->tty_name)) |
16782258 JD |
5639 | { |
5640 | int fd; | |
5641 | /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the | |
5642 | master side. Try the slave side. */ | |
4d2b044c | 5643 | fd = emacs_open (SSDATA (p->tty_name), O_RDONLY, 0); |
16782258 JD |
5644 | |
5645 | if (fd != -1) | |
5646 | { | |
5647 | ioctl (fd, TIOCGPGRP, &gid); | |
5648 | emacs_close (fd); | |
5649 | } | |
5650 | } | |
5651 | #endif /* defined (TIOCGPGRP ) */ | |
5652 | ||
5653 | return gid; | |
5654 | } | |
5655 | ||
b81ea5ef RS |
5656 | DEFUN ("process-running-child-p", Fprocess_running_child_p, |
5657 | Sprocess_running_child_p, 0, 1, 0, | |
fdb82f93 PJ |
5658 | doc: /* Return t if PROCESS has given the terminal to a child. |
5659 | If the operating system does not make it possible to find out, | |
5660 | return t unconditionally. */) | |
5842a27b | 5661 | (Lisp_Object process) |
b81ea5ef RS |
5662 | { |
5663 | /* Initialize in case ioctl doesn't exist or gives an error, | |
5664 | in a way that will cause returning t. */ | |
dde14581 | 5665 | pid_t gid; |
b81ea5ef RS |
5666 | Lisp_Object proc; |
5667 | struct Lisp_Process *p; | |
5668 | ||
5669 | proc = get_process (process); | |
5670 | p = XPROCESS (proc); | |
5671 | ||
4d2b044c | 5672 | if (!EQ (p->type, Qreal)) |
b81ea5ef | 5673 | error ("Process %s is not a subprocess", |
4d2b044c | 5674 | SDATA (p->name)); |
60f0fb11 | 5675 | if (p->infd < 0) |
b81ea5ef | 5676 | error ("Process %s is not active", |
4d2b044c | 5677 | SDATA (p->name)); |
b81ea5ef | 5678 | |
16782258 | 5679 | gid = emacs_get_tty_pgrp (p); |
b81ea5ef | 5680 | |
6bfd98e7 | 5681 | if (gid == p->pid) |
b81ea5ef RS |
5682 | return Qnil; |
5683 | return Qt; | |
5684 | } | |
5685 | \f | |
d0d6b7c5 | 5686 | /* send a signal number SIGNO to PROCESS. |
b81ea5ef RS |
5687 | If CURRENT_GROUP is t, that means send to the process group |
5688 | that currently owns the terminal being used to communicate with PROCESS. | |
d0d6b7c5 | 5689 | This is used for various commands in shell mode. |
b81ea5ef RS |
5690 | If CURRENT_GROUP is lambda, that means send to the process group |
5691 | that currently owns the terminal, but only if it is NOT the shell itself. | |
5692 | ||
0bce5d9e | 5693 | If NOMSG is false, insert signal-announcements into process's buffers |
b0310da4 JB |
5694 | right away. |
5695 | ||
5696 | If we can, we try to signal PROCESS by sending control characters | |
e333e864 | 5697 | down the pty. This allows us to signal inferiors who have changed |
d983a10b | 5698 | their uid, for which kill would return an EPERM error. */ |
d0d6b7c5 | 5699 | |
f9738840 | 5700 | static void |
d3da34e0 | 5701 | process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group, |
0bce5d9e | 5702 | bool nomsg) |
d0d6b7c5 JB |
5703 | { |
5704 | Lisp_Object proc; | |
0bce5d9e | 5705 | struct Lisp_Process *p; |
dde14581 | 5706 | pid_t gid; |
0bce5d9e | 5707 | bool no_pgrp = 0; |
d0d6b7c5 JB |
5708 | |
5709 | proc = get_process (process); | |
5710 | p = XPROCESS (proc); | |
5711 | ||
4d2b044c | 5712 | if (!EQ (p->type, Qreal)) |
d0d6b7c5 | 5713 | error ("Process %s is not a subprocess", |
4d2b044c | 5714 | SDATA (p->name)); |
60f0fb11 | 5715 | if (p->infd < 0) |
d0d6b7c5 | 5716 | error ("Process %s is not active", |
4d2b044c | 5717 | SDATA (p->name)); |
d0d6b7c5 | 5718 | |
60f0fb11 | 5719 | if (!p->pty_flag) |
d0d6b7c5 JB |
5720 | current_group = Qnil; |
5721 | ||
d0d6b7c5 | 5722 | /* If we are using pgrps, get a pgrp number and make it negative. */ |
2af70a0c RS |
5723 | if (NILP (current_group)) |
5724 | /* Send the signal to the shell's process group. */ | |
6bfd98e7 | 5725 | gid = p->pid; |
2af70a0c | 5726 | else |
d0d6b7c5 | 5727 | { |
b0310da4 | 5728 | #ifdef SIGNALS_VIA_CHARACTERS |
d0d6b7c5 JB |
5729 | /* If possible, send signals to the entire pgrp |
5730 | by sending an input character to it. */ | |
b0310da4 | 5731 | |
6be429b1 | 5732 | struct termios t; |
7f916a36 RS |
5733 | cc_t *sig_char = NULL; |
5734 | ||
60f0fb11 | 5735 | tcgetattr (p->infd, &t); |
6be429b1 JB |
5736 | |
5737 | switch (signo) | |
5738 | { | |
5739 | case SIGINT: | |
7f916a36 RS |
5740 | sig_char = &t.c_cc[VINTR]; |
5741 | break; | |
6be429b1 JB |
5742 | |
5743 | case SIGQUIT: | |
7f916a36 RS |
5744 | sig_char = &t.c_cc[VQUIT]; |
5745 | break; | |
6be429b1 JB |
5746 | |
5747 | case SIGTSTP: | |
d0adf46f | 5748 | #if defined (VSWTCH) && !defined (PREFER_VSUSP) |
7f916a36 | 5749 | sig_char = &t.c_cc[VSWTCH]; |
6be429b1 | 5750 | #else |
7f916a36 | 5751 | sig_char = &t.c_cc[VSUSP]; |
6be429b1 | 5752 | #endif |
7f916a36 | 5753 | break; |
6be429b1 JB |
5754 | } |
5755 | ||
ca4313d5 | 5756 | if (sig_char && *sig_char != CDISABLE) |
f1c206fc | 5757 | { |
f990b4e5 | 5758 | send_process (proc, (char *) sig_char, 1, Qnil); |
f1c206fc RS |
5759 | return; |
5760 | } | |
5761 | /* If we can't send the signal with a character, | |
5762 | fall through and send it another way. */ | |
f1c206fc RS |
5763 | |
5764 | /* The code above may fall through if it can't | |
5765 | handle the signal. */ | |
20505336 | 5766 | #endif /* defined (SIGNALS_VIA_CHARACTERS) */ |
d0d6b7c5 | 5767 | |
177c0ea7 | 5768 | #ifdef TIOCGPGRP |
2af70a0c | 5769 | /* Get the current pgrp using the tty itself, if we have that. |
d0d6b7c5 JB |
5770 | Otherwise, use the pty to get the pgrp. |
5771 | On pfa systems, saka@pfu.fujitsu.co.JP writes: | |
b0310da4 JB |
5772 | "TIOCGPGRP symbol defined in sys/ioctl.h at E50. |
5773 | But, TIOCGPGRP does not work on E50 ;-P works fine on E60" | |
d0d6b7c5 JB |
5774 | His patch indicates that if TIOCGPGRP returns an error, then |
5775 | we should just assume that p->pid is also the process group id. */ | |
d0d6b7c5 | 5776 | |
16782258 | 5777 | gid = emacs_get_tty_pgrp (p); |
f3d9532b | 5778 | |
16782258 JD |
5779 | if (gid == -1) |
5780 | /* If we can't get the information, assume | |
5781 | the shell owns the tty. */ | |
6bfd98e7 | 5782 | gid = p->pid; |
2af70a0c RS |
5783 | |
5784 | /* It is not clear whether anything really can set GID to -1. | |
5785 | Perhaps on some system one of those ioctls can or could do so. | |
5786 | Or perhaps this is vestigial. */ | |
d0d6b7c5 JB |
5787 | if (gid == -1) |
5788 | no_pgrp = 1; | |
b0310da4 | 5789 | #else /* ! defined (TIOCGPGRP ) */ |
301c3fe4 JB |
5790 | /* Can't select pgrps on this system, so we know that |
5791 | the child itself heads the pgrp. */ | |
6bfd98e7 | 5792 | gid = p->pid; |
301c3fe4 | 5793 | #endif /* ! defined (TIOCGPGRP ) */ |
b81ea5ef RS |
5794 | |
5795 | /* If current_group is lambda, and the shell owns the terminal, | |
5796 | don't send any signal. */ | |
6bfd98e7 | 5797 | if (EQ (current_group, Qlambda) && gid == p->pid) |
b81ea5ef | 5798 | return; |
d0d6b7c5 | 5799 | } |
d0d6b7c5 | 5800 | |
d0d6b7c5 | 5801 | #ifdef SIGCONT |
26495794 PE |
5802 | if (signo == SIGCONT) |
5803 | { | |
6bfd98e7 | 5804 | p->raw_status_new = 0; |
6a09a33b | 5805 | pset_status (p, Qrun); |
60f0fb11 | 5806 | p->tick = ++process_tick; |
d0d6b7c5 | 5807 | if (!nomsg) |
e42bdf01 CY |
5808 | { |
5809 | status_notify (NULL); | |
5810 | redisplay_preserve_echo_area (13); | |
5811 | } | |
d0d6b7c5 | 5812 | } |
26495794 | 5813 | #endif |
d0d6b7c5 JB |
5814 | |
5815 | /* If we don't have process groups, send the signal to the immediate | |
5816 | subprocess. That isn't really right, but it's better than any | |
5817 | obvious alternative. */ | |
5818 | if (no_pgrp) | |
5819 | { | |
6bfd98e7 | 5820 | kill (p->pid, signo); |
d0d6b7c5 JB |
5821 | return; |
5822 | } | |
5823 | ||
5824 | /* gid may be a pid, or minus a pgrp's number */ | |
5825 | #ifdef TIOCSIGSEND | |
5826 | if (!NILP (current_group)) | |
16782258 | 5827 | { |
60f0fb11 | 5828 | if (ioctl (p->infd, TIOCSIGSEND, signo) == -1) |
d983a10b | 5829 | kill (-gid, signo); |
16782258 | 5830 | } |
d0d6b7c5 JB |
5831 | else |
5832 | { | |
6bfd98e7 | 5833 | gid = - p->pid; |
d0d6b7c5 JB |
5834 | kill (gid, signo); |
5835 | } | |
301c3fe4 | 5836 | #else /* ! defined (TIOCSIGSEND) */ |
d983a10b | 5837 | kill (-gid, signo); |
301c3fe4 | 5838 | #endif /* ! defined (TIOCSIGSEND) */ |
d0d6b7c5 JB |
5839 | } |
5840 | ||
5841 | DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0, | |
fdb82f93 PJ |
5842 | doc: /* Interrupt process PROCESS. |
5843 | PROCESS may be a process, a buffer, or the name of a process or buffer. | |
f3d9532b | 5844 | No arg or nil means current buffer's process. |
fdb82f93 PJ |
5845 | Second arg CURRENT-GROUP non-nil means send signal to |
5846 | the current process-group of the process's controlling terminal | |
5847 | rather than to the process's own process group. | |
5848 | If the process is a shell, this means interrupt current subjob | |
5849 | rather than the shell. | |
5850 | ||
5851 | If CURRENT-GROUP is `lambda', and if the shell owns the terminal, | |
5852 | don't send the signal. */) | |
5842a27b | 5853 | (Lisp_Object process, Lisp_Object current_group) |
d0d6b7c5 JB |
5854 | { |
5855 | process_send_signal (process, SIGINT, current_group, 0); | |
5856 | return process; | |
5857 | } | |
5858 | ||
5859 | DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0, | |
fdb82f93 PJ |
5860 | doc: /* Kill process PROCESS. May be process or name of one. |
5861 | See function `interrupt-process' for more details on usage. */) | |
5842a27b | 5862 | (Lisp_Object process, Lisp_Object current_group) |
d0d6b7c5 JB |
5863 | { |
5864 | process_send_signal (process, SIGKILL, current_group, 0); | |
5865 | return process; | |
5866 | } | |
5867 | ||
5868 | DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0, | |
fdb82f93 PJ |
5869 | doc: /* Send QUIT signal to process PROCESS. May be process or name of one. |
5870 | See function `interrupt-process' for more details on usage. */) | |
5842a27b | 5871 | (Lisp_Object process, Lisp_Object current_group) |
d0d6b7c5 JB |
5872 | { |
5873 | process_send_signal (process, SIGQUIT, current_group, 0); | |
5874 | return process; | |
5875 | } | |
5876 | ||
5877 | DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0, | |
fdb82f93 | 5878 | doc: /* Stop process PROCESS. May be process or name of one. |
177c0ea7 | 5879 | See function `interrupt-process' for more details on usage. |
d888760c GM |
5880 | If PROCESS is a network or serial process, inhibit handling of incoming |
5881 | traffic. */) | |
5842a27b | 5882 | (Lisp_Object process, Lisp_Object current_group) |
d0d6b7c5 | 5883 | { |
d888760c | 5884 | if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process))) |
e690ca94 KS |
5885 | { |
5886 | struct Lisp_Process *p; | |
177c0ea7 | 5887 | |
e690ca94 | 5888 | p = XPROCESS (process); |
4d2b044c | 5889 | if (NILP (p->command) |
60f0fb11 | 5890 | && p->infd >= 0) |
e690ca94 | 5891 | { |
60f0fb11 SM |
5892 | FD_CLR (p->infd, &input_wait_mask); |
5893 | FD_CLR (p->infd, &non_keyboard_wait_mask); | |
e690ca94 | 5894 | } |
6a09a33b | 5895 | pset_command (p, Qt); |
e690ca94 KS |
5896 | return process; |
5897 | } | |
d0d6b7c5 | 5898 | #ifndef SIGTSTP |
a9a73d6c | 5899 | error ("No SIGTSTP support"); |
d0d6b7c5 JB |
5900 | #else |
5901 | process_send_signal (process, SIGTSTP, current_group, 0); | |
5902 | #endif | |
5903 | return process; | |
5904 | } | |
5905 | ||
5906 | DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0, | |
fdb82f93 | 5907 | doc: /* Continue process PROCESS. May be process or name of one. |
177c0ea7 | 5908 | See function `interrupt-process' for more details on usage. |
d888760c GM |
5909 | If PROCESS is a network or serial process, resume handling of incoming |
5910 | traffic. */) | |
5842a27b | 5911 | (Lisp_Object process, Lisp_Object current_group) |
d0d6b7c5 | 5912 | { |
d888760c | 5913 | if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process))) |
e690ca94 KS |
5914 | { |
5915 | struct Lisp_Process *p; | |
5916 | ||
5917 | p = XPROCESS (process); | |
4d2b044c | 5918 | if (EQ (p->command, Qt) |
60f0fb11 | 5919 | && p->infd >= 0 |
4d2b044c | 5920 | && (!EQ (p->filter, Qt) || EQ (p->status, Qlisten))) |
e690ca94 | 5921 | { |
60f0fb11 SM |
5922 | FD_SET (p->infd, &input_wait_mask); |
5923 | FD_SET (p->infd, &non_keyboard_wait_mask); | |
d888760c GM |
5924 | #ifdef WINDOWSNT |
5925 | if (fd_info[ p->infd ].flags & FILE_SERIAL) | |
5926 | PurgeComm (fd_info[ p->infd ].hnd, PURGE_RXABORT | PURGE_RXCLEAR); | |
a7ebc409 | 5927 | #else /* not WINDOWSNT */ |
d888760c | 5928 | tcflush (p->infd, TCIFLUSH); |
a7ebc409 | 5929 | #endif /* not WINDOWSNT */ |
e690ca94 | 5930 | } |
6a09a33b | 5931 | pset_command (p, Qnil); |
e690ca94 KS |
5932 | return process; |
5933 | } | |
d0d6b7c5 JB |
5934 | #ifdef SIGCONT |
5935 | process_send_signal (process, SIGCONT, current_group, 0); | |
5936 | #else | |
a9a73d6c | 5937 | error ("No SIGCONT support"); |
d0d6b7c5 JB |
5938 | #endif |
5939 | return process; | |
5940 | } | |
5941 | ||
d983a10b PE |
5942 | /* Return the integer value of the signal whose abbreviation is ABBR, |
5943 | or a negative number if there is no such signal. */ | |
5944 | static int | |
5945 | abbr_to_signal (char const *name) | |
5946 | { | |
5947 | int i, signo; | |
5948 | char sigbuf[20]; /* Large enough for all valid signal abbreviations. */ | |
5949 | ||
5950 | if (!strncmp (name, "SIG", 3) || !strncmp (name, "sig", 3)) | |
5951 | name += 3; | |
5952 | ||
5953 | for (i = 0; i < sizeof sigbuf; i++) | |
5954 | { | |
5955 | sigbuf[i] = c_toupper (name[i]); | |
5956 | if (! sigbuf[i]) | |
5957 | return str2sig (sigbuf, &signo) == 0 ? signo : -1; | |
5958 | } | |
5959 | ||
5960 | return -1; | |
5961 | } | |
5962 | ||
d0d6b7c5 | 5963 | DEFUN ("signal-process", Fsignal_process, Ssignal_process, |
cdd5ea86 KS |
5964 | 2, 2, "sProcess (name or number): \nnSignal code: ", |
5965 | doc: /* Send PROCESS the signal with code SIGCODE. | |
648f6edf | 5966 | PROCESS may also be a number specifying the process id of the |
cdd5ea86 KS |
5967 | process to signal; in this case, the process need not be a child of |
5968 | this Emacs. | |
fdb82f93 | 5969 | SIGCODE may be an integer, or a symbol whose name is a signal name. */) |
5842a27b | 5970 | (Lisp_Object process, Lisp_Object sigcode) |
d0d6b7c5 | 5971 | { |
6bfd98e7 | 5972 | pid_t pid; |
d983a10b | 5973 | int signo; |
cdd5ea86 | 5974 | |
cdd5ea86 KS |
5975 | if (STRINGP (process)) |
5976 | { | |
d311d28c PE |
5977 | Lisp_Object tem = Fget_process (process); |
5978 | if (NILP (tem)) | |
cdd5ea86 | 5979 | { |
d44287d4 PE |
5980 | Lisp_Object process_number = |
5981 | string_to_number (SSDATA (process), 10, 1); | |
5982 | if (INTEGERP (process_number) || FLOATP (process_number)) | |
5983 | tem = process_number; | |
cdd5ea86 KS |
5984 | } |
5985 | process = tem; | |
5986 | } | |
d44287d4 | 5987 | else if (!NUMBERP (process)) |
cdd5ea86 | 5988 | process = get_process (process); |
177c0ea7 | 5989 | |
cdd5ea86 KS |
5990 | if (NILP (process)) |
5991 | return process; | |
5992 | ||
d83cf4cc PE |
5993 | if (NUMBERP (process)) |
5994 | CONS_TO_INTEGER (process, pid_t, pid); | |
d44287d4 PE |
5995 | else |
5996 | { | |
5997 | CHECK_PROCESS (process); | |
5998 | pid = XPROCESS (process)->pid; | |
5999 | if (pid <= 0) | |
4d2b044c | 6000 | error ("Cannot signal process %s", SDATA (XPROCESS (process)->name)); |
d44287d4 | 6001 | } |
4766242d | 6002 | |
4766242d | 6003 | if (INTEGERP (sigcode)) |
d983a10b PE |
6004 | { |
6005 | CHECK_TYPE_RANGED_INTEGER (int, sigcode); | |
6006 | signo = XINT (sigcode); | |
6007 | } | |
4766242d RS |
6008 | else |
6009 | { | |
25a48bd0 | 6010 | char *name; |
4766242d | 6011 | |
b7826503 | 6012 | CHECK_SYMBOL (sigcode); |
25a48bd0 | 6013 | name = SSDATA (SYMBOL_NAME (sigcode)); |
4766242d | 6014 | |
d983a10b PE |
6015 | signo = abbr_to_signal (name); |
6016 | if (signo < 0) | |
9fa195a2 | 6017 | error ("Undefined signal name %s", name); |
4766242d RS |
6018 | } |
6019 | ||
d983a10b | 6020 | return make_number (kill (pid, signo)); |
d0d6b7c5 JB |
6021 | } |
6022 | ||
6023 | DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0, | |
fdb82f93 PJ |
6024 | doc: /* Make PROCESS see end-of-file in its input. |
6025 | EOF comes after any text already sent to it. | |
6026 | PROCESS may be a process, a buffer, the name of a process or buffer, or | |
6027 | nil, indicating the current buffer's process. | |
6028 | If PROCESS is a network connection, or is a process communicating | |
6029 | through a pipe (as opposed to a pty), then you cannot send any more | |
d888760c GM |
6030 | text to PROCESS after you call this function. |
6031 | If PROCESS is a serial process, wait until all output written to the | |
6032 | process has been transmitted to the serial port. */) | |
5842a27b | 6033 | (Lisp_Object process) |
d0d6b7c5 JB |
6034 | { |
6035 | Lisp_Object proc; | |
1014b1dc EZ |
6036 | struct coding_system *coding = NULL; |
6037 | int outfd; | |
d0d6b7c5 | 6038 | |
e690ca94 KS |
6039 | if (DATAGRAM_CONN_P (process)) |
6040 | return process; | |
6041 | ||
d0d6b7c5 | 6042 | proc = get_process (process); |
1014b1dc EZ |
6043 | outfd = XPROCESS (proc)->outfd; |
6044 | if (outfd >= 0) | |
6045 | coding = proc_encode_coding_system[outfd]; | |
577d03d5 RS |
6046 | |
6047 | /* Make sure the process is really alive. */ | |
6bfd98e7 | 6048 | if (XPROCESS (proc)->raw_status_new) |
577d03d5 | 6049 | update_status (XPROCESS (proc)); |
4d2b044c DA |
6050 | if (! EQ (XPROCESS (proc)->status, Qrun)) |
6051 | error ("Process %s not running", SDATA (XPROCESS (proc)->name)); | |
577d03d5 | 6052 | |
1014b1dc | 6053 | if (coding && CODING_REQUIRE_FLUSHING (coding)) |
de7fbd09 KH |
6054 | { |
6055 | coding->mode |= CODING_MODE_LAST_BLOCK; | |
6056 | send_process (proc, "", 0, Qnil); | |
6057 | } | |
6058 | ||
60f0fb11 | 6059 | if (XPROCESS (proc)->pty_flag) |
4556b700 | 6060 | send_process (proc, "\004", 1, Qnil); |
4d2b044c | 6061 | else if (EQ (XPROCESS (proc)->type, Qserial)) |
d888760c | 6062 | { |
a7ebc409 | 6063 | #ifndef WINDOWSNT |
d888760c | 6064 | if (tcdrain (XPROCESS (proc)->outfd) != 0) |
4e604a5d | 6065 | report_file_error ("Failed tcdrain", Qnil); |
a7ebc409 | 6066 | #endif /* not WINDOWSNT */ |
d888760c GM |
6067 | /* Do nothing on Windows because writes are blocking. */ |
6068 | } | |
d0d6b7c5 JB |
6069 | else |
6070 | { | |
1014b1dc EZ |
6071 | struct Lisp_Process *p = XPROCESS (proc); |
6072 | int old_outfd = p->outfd; | |
94fcd171 | 6073 | int new_outfd; |
4525f571 | 6074 | |
93853f3d | 6075 | #ifdef HAVE_SHUTDOWN |
02f55c4b RS |
6076 | /* If this is a network connection, or socketpair is used |
6077 | for communication with the subprocess, call shutdown to cause EOF. | |
6078 | (In some old system, shutdown to socketpair doesn't work. | |
6079 | Then we just can't win.) */ | |
1014b1dc EZ |
6080 | if (EQ (p->type, Qnetwork) |
6081 | || p->infd == old_outfd) | |
94fcd171 PE |
6082 | shutdown (old_outfd, 1); |
6083 | #endif | |
1014b1dc | 6084 | close_process_fd (&p->open_fd[WRITE_TO_SUBPROCESS]); |
68c45bf0 | 6085 | new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0); |
27448380 | 6086 | if (new_outfd < 0) |
94fcd171 | 6087 | report_file_error ("Opening null device", Qnil); |
1014b1dc EZ |
6088 | p->open_fd[WRITE_TO_SUBPROCESS] = new_outfd; |
6089 | p->outfd = new_outfd; | |
4525f571 RS |
6090 | |
6091 | if (!proc_encode_coding_system[new_outfd]) | |
6092 | proc_encode_coding_system[new_outfd] | |
23f86fce | 6093 | = xmalloc (sizeof (struct coding_system)); |
1014b1dc EZ |
6094 | if (old_outfd >= 0) |
6095 | { | |
6096 | *proc_encode_coding_system[new_outfd] | |
6097 | = *proc_encode_coding_system[old_outfd]; | |
6098 | memset (proc_encode_coding_system[old_outfd], 0, | |
6099 | sizeof (struct coding_system)); | |
6100 | } | |
6101 | else | |
6102 | setup_coding_system (p->encode_coding_system, | |
6103 | proc_encode_coding_system[new_outfd]); | |
d0d6b7c5 | 6104 | } |
d0d6b7c5 JB |
6105 | return process; |
6106 | } | |
d0d6b7c5 | 6107 | \f |
bb5f74ee | 6108 | /* The main Emacs thread records child processes in three places: |
0b3d4a47 | 6109 | |
bb5f74ee PE |
6110 | - Vprocess_alist, for asynchronous subprocesses, which are child |
6111 | processes visible to Lisp. | |
6112 | ||
6113 | - deleted_pid_list, for child processes invisible to Lisp, | |
6114 | typically because of delete-process. These are recorded so that | |
6115 | the processes can be reaped when they exit, so that the operating | |
6116 | system's process table is not cluttered by zombies. | |
0b3d4a47 | 6117 | |
bb5f74ee PE |
6118 | - the local variable PID in Fcall_process, call_process_cleanup and |
6119 | call_process_kill, for synchronous subprocesses. | |
6120 | record_unwind_protect is used to make sure this process is not | |
6121 | forgotten: if the user interrupts call-process and the child | |
6122 | process refuses to exit immediately even with two C-g's, | |
6123 | call_process_kill adds PID's contents to deleted_pid_list before | |
6124 | returning. | |
0b3d4a47 | 6125 | |
bb5f74ee PE |
6126 | The main Emacs thread invokes waitpid only on child processes that |
6127 | it creates and that have not been reaped. This avoid races on | |
6128 | platforms such as GTK, where other threads create their own | |
6129 | subprocesses which the main thread should not reap. For example, | |
6130 | if the main thread attempted to reap an already-reaped child, it | |
6131 | might inadvertently reap a GTK-created process that happened to | |
6132 | have the same process ID. */ | |
6133 | ||
f019a684 PE |
6134 | /* LIB_CHILD_HANDLER is a SIGCHLD handler that Emacs calls while doing |
6135 | its own SIGCHLD handling. On POSIXish systems, glib needs this to | |
fa55d2aa PE |
6136 | keep track of its own children. GNUstep is similar. */ |
6137 | ||
f019a684 | 6138 | static void dummy_handler (int sig) {} |
fa55d2aa | 6139 | static signal_handler_t volatile lib_child_handler; |
f019a684 | 6140 | |
bb5f74ee PE |
6141 | /* Handle a SIGCHLD signal by looking for known child processes of |
6142 | Emacs whose status have changed. For each one found, record its | |
6143 | new status. | |
177c0ea7 | 6144 | |
3fed8ad5 GM |
6145 | All we do is change the status; we do not run sentinels or print |
6146 | notifications. That is saved for the next time keyboard input is | |
6147 | done, in order to avoid timing errors. | |
6148 | ||
6149 | ** WARNING: this can be called during garbage collection. | |
6150 | Therefore, it must not be fooled by the presence of mark bits in | |
6151 | Lisp objects. | |
6152 | ||
6153 | ** USG WARNING: Although it is not obvious from the documentation | |
6154 | in signal(2), on a USG system the SIGCLD handler MUST NOT call | |
6155 | signal() before executing at least one wait(), otherwise the | |
6156 | handler will be called again, resulting in an infinite loop. The | |
6157 | relevant portion of the documentation reads "SIGCLD signals will be | |
6158 | queued and the signal-catching function will be continually | |
6159 | reentered until the queue is empty". Invoking signal() causes the | |
6160 | kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems | |
6b61353c KH |
6161 | Inc. |
6162 | ||
6163 | ** Malloc WARNING: This should never call malloc either directly or | |
6164 | indirectly; if it does, that is a bug */ | |
d0d6b7c5 | 6165 | |
bb5f74ee PE |
6166 | static void |
6167 | handle_child_signal (int sig) | |
d0d6b7c5 | 6168 | { |
d5a1acfa | 6169 | Lisp_Object tail, proc; |
d0d6b7c5 | 6170 | |
20ef56db | 6171 | /* Find the process that signaled us, and record its status. */ |
d0d6b7c5 | 6172 | |
bb5f74ee PE |
6173 | /* The process can have been deleted by Fdelete_process, or have |
6174 | been started asynchronously by Fcall_process. */ | |
20ef56db PE |
6175 | for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail)) |
6176 | { | |
0b3d4a47 PE |
6177 | bool all_pids_are_fixnums |
6178 | = (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t) | |
6179 | && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM); | |
94fcd171 | 6180 | Lisp_Object head = XCAR (tail); |
22490125 PE |
6181 | Lisp_Object xpid; |
6182 | if (! CONSP (head)) | |
6183 | continue; | |
6184 | xpid = XCAR (head); | |
0b3d4a47 | 6185 | if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid)) |
a5cfdda8 | 6186 | { |
0b3d4a47 PE |
6187 | pid_t deleted_pid; |
6188 | if (INTEGERP (xpid)) | |
6189 | deleted_pid = XINT (xpid); | |
6190 | else | |
6191 | deleted_pid = XFLOAT_DATA (xpid); | |
bb5f74ee | 6192 | if (child_status_changed (deleted_pid, 0, 0)) |
94fcd171 PE |
6193 | { |
6194 | if (STRINGP (XCDR (head))) | |
6195 | unlink (SSDATA (XCDR (head))); | |
6196 | XSETCAR (tail, Qnil); | |
6197 | } | |
a5cfdda8 | 6198 | } |
20ef56db | 6199 | } |
a5cfdda8 | 6200 | |
20ef56db | 6201 | /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */ |
d5a1acfa | 6202 | FOR_EACH_PROCESS (tail, proc) |
20ef56db | 6203 | { |
0b3d4a47 | 6204 | struct Lisp_Process *p = XPROCESS (proc); |
bb5f74ee PE |
6205 | int status; |
6206 | ||
da9aff11 PE |
6207 | if (p->alive |
6208 | && child_status_changed (p->pid, &status, WUNTRACED | WCONTINUED)) | |
0b3d4a47 PE |
6209 | { |
6210 | /* Change the status of the process that was found. */ | |
6211 | p->tick = ++process_tick; | |
bb5f74ee | 6212 | p->raw_status = status; |
0b3d4a47 | 6213 | p->raw_status_new = 1; |
177c0ea7 | 6214 | |
0b3d4a47 | 6215 | /* If process has terminated, stop waiting for its output. */ |
bb5f74ee | 6216 | if (WIFSIGNALED (status) || WIFEXITED (status)) |
0b3d4a47 | 6217 | { |
0bce5d9e | 6218 | bool clear_desc_flag = 0; |
0b3d4a47 PE |
6219 | p->alive = 0; |
6220 | if (p->infd >= 0) | |
6221 | clear_desc_flag = 1; | |
e98d950b | 6222 | |
0b3d4a47 PE |
6223 | /* clear_desc_flag avoids a compiler bug in Microsoft C. */ |
6224 | if (clear_desc_flag) | |
6225 | { | |
6226 | FD_CLR (p->infd, &input_wait_mask); | |
6227 | FD_CLR (p->infd, &non_keyboard_wait_mask); | |
6228 | } | |
6229 | } | |
d0d6b7c5 | 6230 | } |
20ef56db | 6231 | } |
f019a684 PE |
6232 | |
6233 | lib_child_handler (sig); | |
2c41e781 | 6234 | #ifdef NS_IMPL_GNUSTEP |
a6c4680a | 6235 | /* NSTask in GNUstep sets its child handler each time it is called. |
2c41e781 JD |
6236 | So we must re-set ours. */ |
6237 | catch_child_signal(); | |
6238 | #endif | |
d0d6b7c5 | 6239 | } |
20ef56db PE |
6240 | |
6241 | static void | |
6242 | deliver_child_signal (int sig) | |
6243 | { | |
4d7e6e51 | 6244 | deliver_process_signal (sig, handle_child_signal); |
20ef56db | 6245 | } |
d0d6b7c5 JB |
6246 | \f |
6247 | ||
3b9a3dfa | 6248 | static Lisp_Object |
b93aacde | 6249 | exec_sentinel_error_handler (Lisp_Object error_val) |
3b9a3dfa | 6250 | { |
b93aacde | 6251 | cmd_error_internal (error_val, "error in process sentinel: "); |
3b9a3dfa RS |
6252 | Vinhibit_quit = Qt; |
6253 | update_echo_area (); | |
833ba342 | 6254 | Fsleep_for (make_number (2), Qnil); |
8c983bf2 | 6255 | return Qt; |
3b9a3dfa RS |
6256 | } |
6257 | ||
d0d6b7c5 | 6258 | static void |
d3da34e0 | 6259 | exec_sentinel (Lisp_Object proc, Lisp_Object reason) |
d0d6b7c5 | 6260 | { |
aba7731a | 6261 | Lisp_Object sentinel, odeactivate; |
1882aa38 | 6262 | struct Lisp_Process *p = XPROCESS (proc); |
d311d28c | 6263 | ptrdiff_t count = SPECPDL_INDEX (); |
1882aa38 | 6264 | bool outer_running_asynch_code = running_asynch_code; |
bbce7d72 | 6265 | int waiting = waiting_for_user_input_p; |
d0d6b7c5 | 6266 | |
84af9896 RS |
6267 | if (inhibit_sentinels) |
6268 | return; | |
6269 | ||
dfc21838 RS |
6270 | /* No need to gcpro these, because all we do with them later |
6271 | is test them for EQness, and none of them should be a string. */ | |
8fb3cf64 | 6272 | odeactivate = Vdeactivate_mark; |
aba7731a PE |
6273 | #if 0 |
6274 | Lisp_Object obuffer, okeymap; | |
dfc21838 | 6275 | XSETBUFFER (obuffer, current_buffer); |
4b4deea2 | 6276 | okeymap = BVAR (current_buffer, keymap); |
aba7731a | 6277 | #endif |
dfc21838 | 6278 | |
97e53006 SM |
6279 | /* There's no good reason to let sentinels change the current |
6280 | buffer, and many callers of accept-process-output, sit-for, and | |
6281 | friends don't expect current-buffer to be changed from under them. */ | |
66322887 | 6282 | record_unwind_current_buffer (); |
97e53006 | 6283 | |
4d2b044c | 6284 | sentinel = p->sentinel; |
d0d6b7c5 | 6285 | |
d0d6b7c5 JB |
6286 | /* Inhibit quit so that random quits don't screw up a running filter. */ |
6287 | specbind (Qinhibit_quit, Qt); | |
f00c449b | 6288 | specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef */ |
3b9a3dfa | 6289 | |
4da2f5be RS |
6290 | /* In case we get recursively called, |
6291 | and we already saved the match data nonrecursively, | |
6292 | save the same match data in safely recursive fashion. */ | |
6293 | if (outer_running_asynch_code) | |
6294 | { | |
6295 | Lisp_Object tem; | |
89f2614d KS |
6296 | tem = Fmatch_data (Qnil, Qnil, Qnil); |
6297 | restore_search_regs (); | |
6298 | record_unwind_save_match_data (); | |
6299 | Fset_match_data (tem, Qt); | |
4da2f5be RS |
6300 | } |
6301 | ||
6302 | /* For speed, if a search happens within this code, | |
6303 | save the match data in a special nonrecursive fashion. */ | |
7074fde6 | 6304 | running_asynch_code = 1; |
4da2f5be | 6305 | |
3b9a3dfa | 6306 | internal_condition_case_1 (read_process_output_call, |
6c6f1994 | 6307 | list3 (sentinel, proc, reason), |
3b9a3dfa RS |
6308 | !NILP (Vdebug_on_error) ? Qnil : Qerror, |
6309 | exec_sentinel_error_handler); | |
4da2f5be RS |
6310 | |
6311 | /* If we saved the match data nonrecursively, restore it now. */ | |
89f2614d | 6312 | restore_search_regs (); |
4da2f5be | 6313 | running_asynch_code = outer_running_asynch_code; |
8fb3cf64 KH |
6314 | |
6315 | Vdeactivate_mark = odeactivate; | |
bbce7d72 RS |
6316 | |
6317 | /* Restore waiting_for_user_input_p as it was | |
6318 | when we were called, in case the filter clobbered it. */ | |
6319 | waiting_for_user_input_p = waiting; | |
6320 | ||
7973cfa8 | 6321 | #if 0 |
dfc21838 RS |
6322 | if (! EQ (Fcurrent_buffer (), obuffer) |
6323 | || ! EQ (current_buffer->keymap, okeymap)) | |
7973cfa8 | 6324 | #endif |
927e08be RS |
6325 | /* But do it only if the caller is actually going to read events. |
6326 | Otherwise there's no need to make him wake up, and it could | |
7157b8fe | 6327 | cause trouble (for example it would make sit_for return). */ |
927e08be RS |
6328 | if (waiting_for_user_input_p == -1) |
6329 | record_asynch_buffer_change (); | |
8fb3cf64 | 6330 | |
2ea6d561 | 6331 | unbind_to (count, Qnil); |
d0d6b7c5 JB |
6332 | } |
6333 | ||
6334 | /* Report all recent events of a change in process status | |
6335 | (either run the sentinel or output a message). | |
b50fe468 RS |
6336 | This is usually done while Emacs is waiting for keyboard input |
6337 | but can be done at other times. */ | |
d0d6b7c5 | 6338 | |
ff6daed3 | 6339 | static void |
d3da34e0 | 6340 | status_notify (struct Lisp_Process *deleting_process) |
d0d6b7c5 | 6341 | { |
1aa8d505 | 6342 | register Lisp_Object proc; |
2e4149a8 | 6343 | Lisp_Object tail, msg; |
d0d6b7c5 JB |
6344 | struct gcpro gcpro1, gcpro2; |
6345 | ||
2e4149a8 KH |
6346 | tail = Qnil; |
6347 | msg = Qnil; | |
d0d6b7c5 JB |
6348 | /* We need to gcpro tail; if read_process_output calls a filter |
6349 | which deletes a process and removes the cons to which tail points | |
6350 | from Vprocess_alist, and then causes a GC, tail is an unprotected | |
6351 | reference. */ | |
6352 | GCPRO2 (tail, msg); | |
6353 | ||
30623085 RS |
6354 | /* Set this now, so that if new processes are created by sentinels |
6355 | that we run, we get called again to handle their status changes. */ | |
6356 | update_tick = process_tick; | |
6357 | ||
d5a1acfa | 6358 | FOR_EACH_PROCESS (tail, proc) |
d0d6b7c5 | 6359 | { |
30623085 | 6360 | Lisp_Object symbol; |
d5a1acfa | 6361 | register struct Lisp_Process *p = XPROCESS (proc); |
30623085 | 6362 | |
60f0fb11 | 6363 | if (p->tick != p->update_tick) |
d0d6b7c5 | 6364 | { |
60f0fb11 | 6365 | p->update_tick = p->tick; |
d0d6b7c5 | 6366 | |
30623085 | 6367 | /* If process is still active, read any output that remains. */ |
4d2b044c DA |
6368 | while (! EQ (p->filter, Qt) |
6369 | && ! EQ (p->status, Qconnect) | |
6370 | && ! EQ (p->status, Qlisten) | |
d888760c | 6371 | /* Network or serial process not stopped: */ |
4d2b044c | 6372 | && ! EQ (p->command, Qt) |
60f0fb11 | 6373 | && p->infd >= 0 |
ff6daed3 | 6374 | && p != deleting_process |
60f0fb11 | 6375 | && read_process_output (proc, p->infd) > 0); |
d0d6b7c5 | 6376 | |
30623085 | 6377 | /* Get the text to use for the message. */ |
6bfd98e7 | 6378 | if (p->raw_status_new) |
30623085 | 6379 | update_status (p); |
116c423c | 6380 | msg = status_message (p); |
d0d6b7c5 | 6381 | |
30623085 | 6382 | /* If process is terminated, deactivate it or delete it. */ |
4d2b044c DA |
6383 | symbol = p->status; |
6384 | if (CONSP (p->status)) | |
6385 | symbol = XCAR (p->status); | |
d0d6b7c5 | 6386 | |
30623085 RS |
6387 | if (EQ (symbol, Qsignal) || EQ (symbol, Qexit) |
6388 | || EQ (symbol, Qclosed)) | |
6389 | { | |
6390 | if (delete_exited_processes) | |
6391 | remove_process (proc); | |
6392 | else | |
6393 | deactivate_process (proc); | |
6394 | } | |
d0d6b7c5 | 6395 | |
0ad61fe7 | 6396 | /* The actions above may have further incremented p->tick. |
1aa8d505 SM |
6397 | So set p->update_tick again so that an error in the sentinel will |
6398 | not cause this code to be run again. */ | |
60f0fb11 | 6399 | p->update_tick = p->tick; |
30623085 | 6400 | /* Now output the message suitably. */ |
1aa8d505 | 6401 | exec_sentinel (proc, msg); |
30623085 RS |
6402 | } |
6403 | } /* end for */ | |
d0d6b7c5 | 6404 | |
2ec9db5d | 6405 | update_mode_lines = 24; /* In case buffers use %s in mode-line-format. */ |
d0d6b7c5 JB |
6406 | UNGCPRO; |
6407 | } | |
0fa1789e | 6408 | |
1aa8d505 SM |
6409 | DEFUN ("internal-default-process-sentinel", Finternal_default_process_sentinel, |
6410 | Sinternal_default_process_sentinel, 2, 2, 0, | |
6411 | doc: /* Function used as default sentinel for processes. */) | |
6412 | (Lisp_Object proc, Lisp_Object msg) | |
6413 | { | |
6414 | Lisp_Object buffer, symbol; | |
6415 | struct Lisp_Process *p; | |
6416 | CHECK_PROCESS (proc); | |
6417 | p = XPROCESS (proc); | |
6418 | buffer = p->buffer; | |
6419 | symbol = p->status; | |
6420 | if (CONSP (symbol)) | |
6421 | symbol = XCAR (symbol); | |
6422 | ||
6423 | if (!EQ (symbol, Qrun) && !NILP (buffer)) | |
6424 | { | |
6425 | Lisp_Object tem; | |
6426 | struct buffer *old = current_buffer; | |
6427 | ptrdiff_t opoint, opoint_byte; | |
6428 | ptrdiff_t before, before_byte; | |
6429 | ||
6430 | /* Avoid error if buffer is deleted | |
6431 | (probably that's why the process is dead, too). */ | |
6432 | if (!BUFFER_LIVE_P (XBUFFER (buffer))) | |
6433 | return Qnil; | |
6434 | Fset_buffer (buffer); | |
6435 | ||
6436 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) | |
6437 | msg = (code_convert_string_norecord | |
6438 | (msg, Vlocale_coding_system, 1)); | |
6439 | ||
6440 | opoint = PT; | |
6441 | opoint_byte = PT_BYTE; | |
6442 | /* Insert new output into buffer | |
6443 | at the current end-of-output marker, | |
6444 | thus preserving logical ordering of input and output. */ | |
6445 | if (XMARKER (p->mark)->buffer) | |
6446 | Fgoto_char (p->mark); | |
6447 | else | |
6448 | SET_PT_BOTH (ZV, ZV_BYTE); | |
6449 | ||
6450 | before = PT; | |
6451 | before_byte = PT_BYTE; | |
6452 | ||
6453 | tem = BVAR (current_buffer, read_only); | |
6454 | bset_read_only (current_buffer, Qnil); | |
6455 | insert_string ("\nProcess "); | |
6456 | { /* FIXME: temporary kludge. */ | |
6457 | Lisp_Object tem2 = p->name; Finsert (1, &tem2); } | |
6458 | insert_string (" "); | |
6459 | Finsert (1, &msg); | |
6460 | bset_read_only (current_buffer, tem); | |
6461 | set_marker_both (p->mark, p->buffer, PT, PT_BYTE); | |
6462 | ||
6463 | if (opoint >= before) | |
6464 | SET_PT_BOTH (opoint + (PT - before), | |
6465 | opoint_byte + (PT_BYTE - before_byte)); | |
6466 | else | |
6467 | SET_PT_BOTH (opoint, opoint_byte); | |
6468 | ||
6469 | set_buffer_internal (old); | |
6470 | } | |
6471 | return Qnil; | |
6472 | } | |
6473 | ||
0fa1789e KH |
6474 | \f |
6475 | DEFUN ("set-process-coding-system", Fset_process_coding_system, | |
6476 | Sset_process_coding_system, 1, 3, 0, | |
fdb82f93 PJ |
6477 | doc: /* Set coding systems of PROCESS to DECODING and ENCODING. |
6478 | DECODING will be used to decode subprocess output and ENCODING to | |
6479 | encode subprocess input. */) | |
5842a27b | 6480 | (register Lisp_Object process, Lisp_Object decoding, Lisp_Object encoding) |
0fa1789e KH |
6481 | { |
6482 | register struct Lisp_Process *p; | |
6483 | ||
f3d9532b JB |
6484 | CHECK_PROCESS (process); |
6485 | p = XPROCESS (process); | |
60f0fb11 | 6486 | if (p->infd < 0) |
4d2b044c | 6487 | error ("Input file descriptor of %s closed", SDATA (p->name)); |
60f0fb11 | 6488 | if (p->outfd < 0) |
4d2b044c | 6489 | error ("Output file descriptor of %s closed", SDATA (p->name)); |
03f04413 KH |
6490 | Fcheck_coding_system (decoding); |
6491 | Fcheck_coding_system (encoding); | |
76d812bb | 6492 | encoding = coding_inherit_eol_type (encoding, Qnil); |
6a09a33b PE |
6493 | pset_decode_coding_system (p, decoding); |
6494 | pset_encode_coding_system (p, encoding); | |
f3d9532b | 6495 | setup_process_coding_systems (process); |
0fa1789e KH |
6496 | |
6497 | return Qnil; | |
6498 | } | |
6499 | ||
6500 | DEFUN ("process-coding-system", | |
6501 | Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0, | |
fdb82f93 | 6502 | doc: /* Return a cons of coding systems for decoding and encoding of PROCESS. */) |
5842a27b | 6503 | (register Lisp_Object process) |
0fa1789e | 6504 | { |
f3d9532b | 6505 | CHECK_PROCESS (process); |
4d2b044c DA |
6506 | return Fcons (XPROCESS (process)->decode_coding_system, |
6507 | XPROCESS (process)->encode_coding_system); | |
0fa1789e | 6508 | } |
03f04413 KH |
6509 | |
6510 | DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte, | |
6511 | Sset_process_filter_multibyte, 2, 2, 0, | |
7392e23c KS |
6512 | doc: /* Set multibyteness of the strings given to PROCESS's filter. |
6513 | If FLAG is non-nil, the filter is given multibyte strings. | |
6514 | If FLAG is nil, the filter is given unibyte strings. In this case, | |
03f04413 KH |
6515 | all character code conversion except for end-of-line conversion is |
6516 | suppressed. */) | |
5842a27b | 6517 | (Lisp_Object process, Lisp_Object flag) |
03f04413 KH |
6518 | { |
6519 | register struct Lisp_Process *p; | |
6520 | ||
f3d9532b JB |
6521 | CHECK_PROCESS (process); |
6522 | p = XPROCESS (process); | |
5431bd6a | 6523 | if (NILP (flag)) |
6a09a33b PE |
6524 | pset_decode_coding_system |
6525 | (p, raw_text_coding_system (p->decode_coding_system)); | |
f3d9532b | 6526 | setup_process_coding_systems (process); |
03f04413 KH |
6527 | |
6528 | return Qnil; | |
6529 | } | |
6530 | ||
6531 | DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p, | |
6532 | Sprocess_filter_multibyte_p, 1, 1, 0, | |
6533 | doc: /* Return t if a multibyte string is given to PROCESS's filter.*/) | |
5842a27b | 6534 | (Lisp_Object process) |
03f04413 KH |
6535 | { |
6536 | register struct Lisp_Process *p; | |
5431bd6a | 6537 | struct coding_system *coding; |
03f04413 | 6538 | |
f3d9532b JB |
6539 | CHECK_PROCESS (process); |
6540 | p = XPROCESS (process); | |
5431bd6a SM |
6541 | coding = proc_decode_coding_system[p->infd]; |
6542 | return (CODING_FOR_UNIBYTE (coding) ? Qnil : Qt); | |
03f04413 KH |
6543 | } |
6544 | ||
6545 | ||
d0d6b7c5 | 6546 | \f |
cb768704 | 6547 | |
4475bec4 PE |
6548 | # ifdef HAVE_GPM |
6549 | ||
994d9841 | 6550 | void |
d3da34e0 | 6551 | add_gpm_wait_descriptor (int desc) |
994d9841 | 6552 | { |
3fad2ad2 | 6553 | add_keyboard_wait_descriptor (desc); |
994d9841 NR |
6554 | } |
6555 | ||
994d9841 | 6556 | void |
d3da34e0 | 6557 | delete_gpm_wait_descriptor (int desc) |
994d9841 | 6558 | { |
3fad2ad2 | 6559 | delete_keyboard_wait_descriptor (desc); |
994d9841 NR |
6560 | } |
6561 | ||
4475bec4 PE |
6562 | # endif |
6563 | ||
4a4bbad2 | 6564 | # ifdef USABLE_SIGIO |
006c5daa | 6565 | |
0bce5d9e | 6566 | /* Return true if *MASK has a bit set |
a69281ff RS |
6567 | that corresponds to one of the keyboard input descriptors. */ |
6568 | ||
0bce5d9e | 6569 | static bool |
d3da34e0 | 6570 | keyboard_bit_set (fd_set *mask) |
a69281ff RS |
6571 | { |
6572 | int fd; | |
6573 | ||
3fad2ad2 | 6574 | for (fd = 0; fd <= max_input_desc; fd++) |
a69281ff RS |
6575 | if (FD_ISSET (fd, mask) && FD_ISSET (fd, &input_wait_mask) |
6576 | && !FD_ISSET (fd, &non_keyboard_wait_mask)) | |
6577 | return 1; | |
6578 | ||
6579 | return 0; | |
6580 | } | |
006c5daa | 6581 | # endif |
934e2a68 | 6582 | |
f388c88a | 6583 | #else /* not subprocesses */ |
934e2a68 | 6584 | |
f388c88a | 6585 | /* Defined on msdos.c. */ |
d486344e | 6586 | extern int sys_select (int, fd_set *, fd_set *, fd_set *, |
43aac990 | 6587 | struct timespec *, void *); |
934e2a68 | 6588 | |
f388c88a EZ |
6589 | /* Implementation of wait_reading_process_output, assuming that there |
6590 | are no subprocesses. Used only by the MS-DOS build. | |
934e2a68 | 6591 | |
f388c88a | 6592 | Wait for timeout to elapse and/or keyboard input to be available. |
934e2a68 | 6593 | |
d188e26b | 6594 | TIME_LIMIT is: |
d35af63c PE |
6595 | timeout in seconds |
6596 | If negative, gobble data immediately available but don't wait for any. | |
6597 | ||
d188e26b | 6598 | NSECS is: |
d35af63c PE |
6599 | an additional duration to wait, measured in nanoseconds |
6600 | If TIME_LIMIT is zero, then: | |
d188e26b PE |
6601 | If NSECS == 0, there is no limit. |
6602 | If NSECS > 0, the timeout consists of NSECS only. | |
d35af63c | 6603 | If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative. |
934e2a68 | 6604 | |
a0bee46f | 6605 | READ_KBD is: |
f388c88a EZ |
6606 | 0 to ignore keyboard input, or |
6607 | 1 to return when input is available, or | |
6608 | -1 means caller will actually read the input, so don't throw to | |
6609 | the quit handler. | |
934e2a68 | 6610 | |
f388c88a EZ |
6611 | see full version for other parameters. We know that wait_proc will |
6612 | always be NULL, since `subprocesses' isn't defined. | |
934e2a68 | 6613 | |
9fa1de30 | 6614 | DO_DISPLAY means redisplay should be done to show subprocess |
f388c88a | 6615 | output that arrives. |
934e2a68 | 6616 | |
f388c88a EZ |
6617 | Return true if we received input from any process. */ |
6618 | ||
0bce5d9e | 6619 | bool |
d35af63c | 6620 | wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, |
9fa1de30 | 6621 | bool do_display, |
1408902e EZ |
6622 | Lisp_Object wait_for_cell, |
6623 | struct Lisp_Process *wait_proc, int just_wait_proc) | |
d0d6b7c5 | 6624 | { |
f388c88a | 6625 | register int nfds; |
43aac990 | 6626 | struct timespec end_time, timeout; |
d0d6b7c5 | 6627 | |
d35af63c PE |
6628 | if (time_limit < 0) |
6629 | { | |
6630 | time_limit = 0; | |
6631 | nsecs = -1; | |
6632 | } | |
6633 | else if (TYPE_MAXIMUM (time_t) < time_limit) | |
6634 | time_limit = TYPE_MAXIMUM (time_t); | |
6635 | ||
f388c88a | 6636 | /* What does time_limit really mean? */ |
908589fd | 6637 | if (time_limit || nsecs > 0) |
f388c88a | 6638 | { |
43aac990 PE |
6639 | timeout = make_timespec (time_limit, nsecs); |
6640 | end_time = timespec_add (current_timespec (), timeout); | |
f388c88a | 6641 | } |
84af9896 | 6642 | |
f388c88a EZ |
6643 | /* Turn off periodic alarms (in case they are in use) |
6644 | and then turn off any other atimers, | |
6645 | because the select emulator uses alarms. */ | |
6646 | stop_polling (); | |
6647 | turn_on_atimers (0); | |
d0d6b7c5 | 6648 | |
f388c88a EZ |
6649 | while (1) |
6650 | { | |
0bce5d9e | 6651 | bool timeout_reduced_for_timers = 0; |
d486344e | 6652 | fd_set waitchannels; |
a0bee46f | 6653 | int xerrno; |
dd2281ae | 6654 | |
f388c88a EZ |
6655 | /* If calling from keyboard input, do not quit |
6656 | since we want to return C-g as an input character. | |
6657 | Otherwise, do pending quit if requested. */ | |
6658 | if (read_kbd >= 0) | |
6659 | QUIT; | |
bad49fc7 | 6660 | |
f388c88a EZ |
6661 | /* Exit now if the cell we're waiting for became non-nil. */ |
6662 | if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) | |
6663 | break; | |
0a65b032 | 6664 | |
1aa8d505 SM |
6665 | /* Compute time from now till when time limit is up. */ |
6666 | /* Exit if already run out. */ | |
d35af63c | 6667 | if (nsecs < 0) |
52fd88d3 | 6668 | { |
216ee680 | 6669 | /* A negative timeout means |
52fd88d3 | 6670 | gobble output available now |
1aa8d505 | 6671 | but don't wait at all. */ |
52fd88d3 | 6672 | |
43aac990 | 6673 | timeout = make_timespec (0, 0); |
52fd88d3 | 6674 | } |
908589fd | 6675 | else if (time_limit || nsecs > 0) |
6720a7fb | 6676 | { |
43aac990 PE |
6677 | struct timespec now = current_timespec (); |
6678 | if (timespec_cmp (end_time, now) <= 0) | |
6720a7fb | 6679 | break; |
43aac990 | 6680 | timeout = timespec_sub (end_time, now); |
6720a7fb | 6681 | } |
52fd88d3 EZ |
6682 | else |
6683 | { | |
43aac990 | 6684 | timeout = make_timespec (100000, 0); |
52fd88d3 | 6685 | } |
6720a7fb | 6686 | |
bae8d137 RS |
6687 | /* If our caller will not immediately handle keyboard events, |
6688 | run timer events directly. | |
6689 | (Callers that will immediately read keyboard events | |
6690 | call timer_delay on their own.) */ | |
f3fbd155 | 6691 | if (NILP (wait_for_cell)) |
bae8d137 | 6692 | { |
43aac990 | 6693 | struct timespec timer_delay; |
0a65b032 | 6694 | |
9baacf76 | 6695 | do |
0a65b032 | 6696 | { |
9fa1de30 | 6697 | unsigned old_timers_run = timers_run; |
f868cd8a | 6698 | timer_delay = timer_check (); |
9baacf76 GM |
6699 | if (timers_run != old_timers_run && do_display) |
6700 | /* We must retry, since a timer may have requeued itself | |
6701 | and that could alter the time delay. */ | |
3007ebfb | 6702 | redisplay_preserve_echo_area (14); |
9baacf76 GM |
6703 | else |
6704 | break; | |
0a65b032 | 6705 | } |
9baacf76 | 6706 | while (!detect_input_pending ()); |
0a65b032 | 6707 | |
52fd88d3 | 6708 | /* If there is unread keyboard input, also return. */ |
b89a415a | 6709 | if (read_kbd != 0 |
52fd88d3 EZ |
6710 | && requeued_events_pending_p ()) |
6711 | break; | |
6712 | ||
43aac990 | 6713 | if (timespec_valid_p (timer_delay) && nsecs >= 0) |
bae8d137 | 6714 | { |
43aac990 | 6715 | if (timespec_cmp (timer_delay, timeout) < 0) |
bae8d137 | 6716 | { |
f694e5d2 | 6717 | timeout = timer_delay; |
bae8d137 RS |
6718 | timeout_reduced_for_timers = 1; |
6719 | } | |
6720 | } | |
6721 | } | |
6722 | ||
6720a7fb JB |
6723 | /* Cause C-g and alarm signals to take immediate action, |
6724 | and cause input available signals to zero out timeout. */ | |
b89a415a | 6725 | if (read_kbd < 0) |
6720a7fb JB |
6726 | set_waiting_for_input (&timeout); |
6727 | ||
ff11dfa1 | 6728 | /* If a frame has been newly mapped and needs updating, |
6720a7fb | 6729 | reprocess its display stuff. */ |
5164ee8e | 6730 | if (frame_garbaged && do_display) |
0a65b032 RS |
6731 | { |
6732 | clear_waiting_for_input (); | |
3007ebfb | 6733 | redisplay_preserve_echo_area (15); |
b89a415a | 6734 | if (read_kbd < 0) |
0a65b032 RS |
6735 | set_waiting_for_input (&timeout); |
6736 | } | |
6720a7fb | 6737 | |
a0bee46f PE |
6738 | /* Wait till there is something to do. */ |
6739 | FD_ZERO (&waitchannels); | |
b89a415a | 6740 | if (read_kbd && detect_input_pending ()) |
a0bee46f PE |
6741 | nfds = 0; |
6742 | else | |
1861b214 | 6743 | { |
a0bee46f PE |
6744 | if (read_kbd || !NILP (wait_for_cell)) |
6745 | FD_SET (0, &waitchannels); | |
6746 | nfds = pselect (1, &waitchannels, NULL, NULL, &timeout, NULL); | |
1861b214 | 6747 | } |
f694e5d2 KH |
6748 | |
6749 | xerrno = errno; | |
6720a7fb JB |
6750 | |
6751 | /* Make C-g and alarm signals set flags again */ | |
6752 | clear_waiting_for_input (); | |
6753 | ||
6754 | /* If we woke up due to SIGWINCH, actually change size now. */ | |
2b653806 | 6755 | do_pending_window_change (0); |
6720a7fb | 6756 | |
d35af63c | 6757 | if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers) |
f694e5d2 KH |
6758 | /* We waited the full specified time, so return now. */ |
6759 | break; | |
6760 | ||
6720a7fb JB |
6761 | if (nfds == -1) |
6762 | { | |
6763 | /* If the system call was interrupted, then go around the | |
6764 | loop again. */ | |
f694e5d2 | 6765 | if (xerrno == EINTR) |
8db121c4 | 6766 | FD_ZERO (&waitchannels); |
f694e5d2 | 6767 | else |
4e604a5d | 6768 | report_file_errno ("Failed select", Qnil, xerrno); |
6720a7fb | 6769 | } |
6720a7fb | 6770 | |
f694e5d2 KH |
6771 | /* Check for keyboard input */ |
6772 | ||
b89a415a | 6773 | if (read_kbd |
78c1afb6 | 6774 | && detect_input_pending_run_timers (do_display)) |
f694e5d2 | 6775 | { |
78c1afb6 | 6776 | swallow_events (do_display); |
f694e5d2 | 6777 | if (detect_input_pending_run_timers (do_display)) |
a2fab450 | 6778 | break; |
78c1afb6 EZ |
6779 | } |
6780 | ||
52fd88d3 | 6781 | /* If there is unread keyboard input, also return. */ |
b89a415a | 6782 | if (read_kbd |
52fd88d3 EZ |
6783 | && requeued_events_pending_p ()) |
6784 | break; | |
6785 | ||
f854a00b RS |
6786 | /* If wait_for_cell. check for keyboard input |
6787 | but don't run any timers. | |
6788 | ??? (It seems wrong to me to check for keyboard | |
6789 | input at all when wait_for_cell, but the code | |
6790 | has been this way since July 1994. | |
6791 | Try changing this after version 19.31.) */ | |
f3fbd155 | 6792 | if (! NILP (wait_for_cell) |
f854a00b RS |
6793 | && detect_input_pending ()) |
6794 | { | |
6795 | swallow_events (do_display); | |
6796 | if (detect_input_pending ()) | |
6797 | break; | |
6798 | } | |
6799 | ||
0a65b032 | 6800 | /* Exit now if the cell we're waiting for became non-nil. */ |
f3fbd155 | 6801 | if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) |
0a65b032 | 6802 | break; |
6720a7fb JB |
6803 | } |
6804 | ||
a87b802f JB |
6805 | start_polling (); |
6806 | ||
6720a7fb JB |
6807 | return 0; |
6808 | } | |
6809 | ||
f388c88a EZ |
6810 | #endif /* not subprocesses */ |
6811 | ||
6812 | /* The following functions are needed even if async subprocesses are | |
6813 | not supported. Some of them are no-op stubs in that case. */ | |
6720a7fb | 6814 | |
01faa934 DN |
6815 | /* Add DESC to the set of keyboard input descriptors. */ |
6816 | ||
6817 | void | |
6818 | add_keyboard_wait_descriptor (int desc) | |
6819 | { | |
bf05eed6 | 6820 | #ifdef subprocesses /* actually means "not MSDOS" */ |
01faa934 DN |
6821 | FD_SET (desc, &input_wait_mask); |
6822 | FD_SET (desc, &non_process_wait_mask); | |
3fad2ad2 J |
6823 | if (desc > max_input_desc) |
6824 | max_input_desc = desc; | |
bf05eed6 | 6825 | #endif |
01faa934 DN |
6826 | } |
6827 | ||
6828 | /* From now on, do not expect DESC to give keyboard input. */ | |
6829 | ||
6830 | void | |
6831 | delete_keyboard_wait_descriptor (int desc) | |
6832 | { | |
bf05eed6 | 6833 | #ifdef subprocesses |
01faa934 DN |
6834 | FD_CLR (desc, &input_wait_mask); |
6835 | FD_CLR (desc, &non_process_wait_mask); | |
2878ba7e | 6836 | delete_input_desc (desc); |
bf05eed6 | 6837 | #endif |
01faa934 DN |
6838 | } |
6839 | ||
fc549af9 EZ |
6840 | /* Setup coding systems of PROCESS. */ |
6841 | ||
6842 | void | |
6843 | setup_process_coding_systems (Lisp_Object process) | |
6844 | { | |
6845 | #ifdef subprocesses | |
6846 | struct Lisp_Process *p = XPROCESS (process); | |
6847 | int inch = p->infd; | |
6848 | int outch = p->outfd; | |
6849 | Lisp_Object coding_system; | |
6850 | ||
6851 | if (inch < 0 || outch < 0) | |
6852 | return; | |
6853 | ||
6854 | if (!proc_decode_coding_system[inch]) | |
38182d90 | 6855 | proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system)); |
4d2b044c | 6856 | coding_system = p->decode_coding_system; |
1aa8d505 SM |
6857 | if (EQ (p->filter, Qinternal_default_process_filter) |
6858 | && BUFFERP (p->buffer)) | |
fc549af9 | 6859 | { |
4d2b044c | 6860 | if (NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters))) |
fc549af9 EZ |
6861 | coding_system = raw_text_coding_system (coding_system); |
6862 | } | |
6863 | setup_coding_system (coding_system, proc_decode_coding_system[inch]); | |
6864 | ||
6865 | if (!proc_encode_coding_system[outch]) | |
38182d90 | 6866 | proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system)); |
4d2b044c | 6867 | setup_coding_system (p->encode_coding_system, |
fc549af9 EZ |
6868 | proc_encode_coding_system[outch]); |
6869 | #endif | |
6870 | } | |
6871 | ||
6720a7fb | 6872 | DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0, |
f388c88a EZ |
6873 | doc: /* Return the (or a) process associated with BUFFER. |
6874 | BUFFER may be a buffer or the name of one. */) | |
6875 | (register Lisp_Object buffer) | |
6720a7fb | 6876 | { |
f388c88a EZ |
6877 | #ifdef subprocesses |
6878 | register Lisp_Object buf, tail, proc; | |
6879 | ||
6880 | if (NILP (buffer)) return Qnil; | |
6881 | buf = Fget_buffer (buffer); | |
6882 | if (NILP (buf)) return Qnil; | |
6883 | ||
d5a1acfa DA |
6884 | FOR_EACH_PROCESS (tail, proc) |
6885 | if (EQ (XPROCESS (proc)->buffer, buf)) | |
6886 | return proc; | |
f388c88a | 6887 | #endif /* subprocesses */ |
6720a7fb JB |
6888 | return Qnil; |
6889 | } | |
6890 | ||
52a1b894 | 6891 | DEFUN ("process-inherit-coding-system-flag", |
fdb82f93 PJ |
6892 | Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag, |
6893 | 1, 1, 0, | |
f388c88a EZ |
6894 | doc: /* Return the value of inherit-coding-system flag for PROCESS. |
6895 | If this flag is t, `buffer-file-coding-system' of the buffer | |
6896 | associated with PROCESS will inherit the coding system used to decode | |
6897 | the process output. */) | |
5842a27b | 6898 | (register Lisp_Object process) |
52a1b894 | 6899 | { |
f388c88a EZ |
6900 | #ifdef subprocesses |
6901 | CHECK_PROCESS (process); | |
6902 | return XPROCESS (process)->inherit_coding_system_flag ? Qt : Qnil; | |
6903 | #else | |
52a1b894 EZ |
6904 | /* Ignore the argument and return the value of |
6905 | inherit-process-coding-system. */ | |
6906 | return inherit_process_coding_system ? Qt : Qnil; | |
f388c88a | 6907 | #endif |
52a1b894 EZ |
6908 | } |
6909 | ||
6720a7fb | 6910 | /* Kill all processes associated with `buffer'. |
f388c88a | 6911 | If `buffer' is nil, kill all processes */ |
6720a7fb | 6912 | |
d9bb0c32 | 6913 | void |
f388c88a | 6914 | kill_buffer_processes (Lisp_Object buffer) |
6720a7fb | 6915 | { |
f388c88a EZ |
6916 | #ifdef subprocesses |
6917 | Lisp_Object tail, proc; | |
6918 | ||
d5a1acfa DA |
6919 | FOR_EACH_PROCESS (tail, proc) |
6920 | if (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)) | |
6921 | { | |
6922 | if (NETCONN_P (proc) || SERIALCONN_P (proc)) | |
6923 | Fdelete_process (proc); | |
6924 | else if (XPROCESS (proc)->infd >= 0) | |
6925 | process_send_signal (proc, SIGHUP, Qnil, 1); | |
6926 | } | |
f388c88a EZ |
6927 | #else /* subprocesses */ |
6928 | /* Since we have no subprocesses, this does nothing. */ | |
6929 | #endif /* subprocesses */ | |
6720a7fb JB |
6930 | } |
6931 | ||
a7ca3326 | 6932 | DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, |
16a97296 | 6933 | Swaiting_for_user_input_p, 0, 0, 0, |
1aa8d505 | 6934 | doc: /* Return non-nil if Emacs is waiting for input from the user. |
77defa9a EZ |
6935 | This is intended for use by asynchronous process output filters and sentinels. */) |
6936 | (void) | |
6937 | { | |
6938 | #ifdef subprocesses | |
6939 | return (waiting_for_user_input_p ? Qt : Qnil); | |
6940 | #else | |
6941 | return Qnil; | |
6942 | #endif | |
6943 | } | |
6944 | ||
d568829b EZ |
6945 | /* Stop reading input from keyboard sources. */ |
6946 | ||
6947 | void | |
6948 | hold_keyboard_input (void) | |
6949 | { | |
6950 | kbd_is_on_hold = 1; | |
6951 | } | |
6952 | ||
6953 | /* Resume reading input from keyboard sources. */ | |
6954 | ||
6955 | void | |
6956 | unhold_keyboard_input (void) | |
6957 | { | |
6958 | kbd_is_on_hold = 0; | |
6959 | } | |
6960 | ||
0bce5d9e | 6961 | /* Return true if keyboard input is on hold, zero otherwise. */ |
d568829b | 6962 | |
0bce5d9e | 6963 | bool |
d568829b EZ |
6964 | kbd_on_hold_p (void) |
6965 | { | |
6966 | return kbd_is_on_hold; | |
6967 | } | |
6968 | ||
f388c88a EZ |
6969 | \f |
6970 | /* Enumeration of and access to system processes a-la ps(1). */ | |
6971 | ||
934e2a68 EZ |
6972 | DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes, |
6973 | 0, 0, 0, | |
6974 | doc: /* Return a list of numerical process IDs of all running processes. | |
6975 | If this functionality is unsupported, return nil. | |
6976 | ||
f388c88a EZ |
6977 | See `process-attributes' for getting attributes of a process given its ID. */) |
6978 | (void) | |
6979 | { | |
6980 | return list_system_processes (); | |
6981 | } | |
6982 | ||
6983 | DEFUN ("process-attributes", Fprocess_attributes, | |
6984 | Sprocess_attributes, 1, 1, 0, | |
6985 | doc: /* Return attributes of the process given by its PID, a number. | |
6986 | ||
6987 | Value is an alist where each element is a cons cell of the form | |
6988 | ||
6989 | \(KEY . VALUE) | |
6990 | ||
6991 | If this functionality is unsupported, the value is nil. | |
6992 | ||
6993 | See `list-system-processes' for getting a list of all process IDs. | |
6994 | ||
6995 | The KEYs of the attributes that this function may return are listed | |
6996 | below, together with the type of the associated VALUE (in parentheses). | |
6997 | Not all platforms support all of these attributes; unsupported | |
6998 | attributes will not appear in the returned alist. | |
6999 | Unless explicitly indicated otherwise, numbers can have either | |
7000 | integer or floating point values. | |
7001 | ||
7002 | euid -- Effective user User ID of the process (number) | |
7003 | user -- User name corresponding to euid (string) | |
7004 | egid -- Effective user Group ID of the process (number) | |
7005 | group -- Group name corresponding to egid (string) | |
7006 | comm -- Command name (executable name only) (string) | |
7007 | state -- Process state code, such as "S", "R", or "T" (string) | |
7008 | ppid -- Parent process ID (number) | |
7009 | pgrp -- Process group ID (number) | |
7010 | sess -- Session ID, i.e. process ID of session leader (number) | |
7011 | ttname -- Controlling tty name (string) | |
7012 | tpgid -- ID of foreground process group on the process's tty (number) | |
7013 | minflt -- number of minor page faults (number) | |
7014 | majflt -- number of major page faults (number) | |
7015 | cminflt -- cumulative number of minor page faults (number) | |
7016 | cmajflt -- cumulative number of major page faults (number) | |
d35af63c PE |
7017 | utime -- user time used by the process, in (current-time) format, |
7018 | which is a list of integers (HIGH LOW USEC PSEC) | |
7019 | stime -- system time used by the process (current-time) | |
7020 | time -- sum of utime and stime (current-time) | |
7021 | cutime -- user time used by the process and its children (current-time) | |
7022 | cstime -- system time used by the process and its children (current-time) | |
7023 | ctime -- sum of cutime and cstime (current-time) | |
f388c88a EZ |
7024 | pri -- priority of the process (number) |
7025 | nice -- nice value of the process (number) | |
7026 | thcount -- process thread count (number) | |
d35af63c | 7027 | start -- time the process started (current-time) |
f388c88a EZ |
7028 | vsize -- virtual memory size of the process in KB's (number) |
7029 | rss -- resident set size of the process in KB's (number) | |
d35af63c | 7030 | etime -- elapsed time the process is running, in (HIGH LOW USEC PSEC) format |
f388c88a EZ |
7031 | pcpu -- percents of CPU time used by the process (floating-point number) |
7032 | pmem -- percents of total physical memory used by process's resident set | |
7033 | (floating-point number) | |
7034 | args -- command line which invoked the process (string). */) | |
7035 | ( Lisp_Object pid) | |
7036 | { | |
7037 | return system_process_attributes (pid); | |
7038 | } | |
7039 | ||
fa55d2aa PE |
7040 | /* Arrange to catch SIGCHLD if this hasn't already been arranged. |
7041 | Invoke this after init_process_emacs, and after glib and/or GNUstep | |
7042 | futz with the SIGCHLD handler, but before Emacs forks any children. | |
7043 | This function's caller should block SIGCHLD. */ | |
0dfeed58 | 7044 | |
dae2f5ef PE |
7045 | #ifndef NS_IMPL_GNUSTEP |
7046 | static | |
7047 | #endif | |
c0342369 JD |
7048 | void |
7049 | catch_child_signal (void) | |
7050 | { | |
f019a684 | 7051 | struct sigaction action, old_action; |
c0342369 | 7052 | emacs_sigaction_init (&action, deliver_child_signal); |
dae2f5ef | 7053 | block_child_signal (); |
f019a684 PE |
7054 | sigaction (SIGCHLD, &action, &old_action); |
7055 | eassert (! (old_action.sa_flags & SA_SIGINFO)); | |
fa55d2aa | 7056 | |
dae2f5ef PE |
7057 | if (old_action.sa_handler != deliver_child_signal) |
7058 | lib_child_handler | |
7059 | = (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN | |
7060 | ? dummy_handler | |
7061 | : old_action.sa_handler); | |
7062 | unblock_child_signal (); | |
c0342369 JD |
7063 | } |
7064 | ||
f388c88a | 7065 | \f |
b82da769 GM |
7066 | /* This is not called "init_process" because that is the name of a |
7067 | Mach system call, so it would cause problems on Darwin systems. */ | |
f388c88a | 7068 | void |
b82da769 | 7069 | init_process_emacs (void) |
f388c88a EZ |
7070 | { |
7071 | #ifdef subprocesses | |
7072 | register int i; | |
7073 | ||
7074 | inhibit_sentinels = 0; | |
7075 | ||
dae2f5ef PE |
7076 | #ifndef CANNOT_DUMP |
7077 | if (! noninteractive || initialized) | |
7078 | #endif | |
7079 | { | |
8f43ce49 | 7080 | #if defined HAVE_GLIB && !defined WINDOWSNT |
dae2f5ef PE |
7081 | /* Tickle glib's child-handling code. Ask glib to wait for Emacs itself; |
7082 | this should always fail, but is enough to initialize glib's | |
8f43ce49 PE |
7083 | private SIGCHLD handler, allowing catch_child_signal to copy |
7084 | it into lib_child_handler. */ | |
dae2f5ef PE |
7085 | g_source_unref (g_child_watch_source_new (getpid ())); |
7086 | #endif | |
7087 | catch_child_signal (); | |
7088 | } | |
7089 | ||
f388c88a EZ |
7090 | FD_ZERO (&input_wait_mask); |
7091 | FD_ZERO (&non_keyboard_wait_mask); | |
7092 | FD_ZERO (&non_process_wait_mask); | |
3fad2ad2 | 7093 | FD_ZERO (&write_mask); |
2878ba7e | 7094 | max_process_desc = max_input_desc = -1; |
3fad2ad2 | 7095 | memset (fd_callback_info, 0, sizeof (fd_callback_info)); |
f388c88a EZ |
7096 | |
7097 | #ifdef NON_BLOCKING_CONNECT | |
7098 | FD_ZERO (&connect_wait_mask); | |
7099 | num_pending_connects = 0; | |
7100 | #endif | |
7101 | ||
7102 | #ifdef ADAPTIVE_READ_BUFFERING | |
7103 | process_output_delay_count = 0; | |
7104 | process_output_skip = 0; | |
7105 | #endif | |
7106 | ||
7107 | /* Don't do this, it caused infinite select loops. The display | |
7108 | method should call add_keyboard_wait_descriptor on stdin if it | |
7109 | needs that. */ | |
7110 | #if 0 | |
7111 | FD_SET (0, &input_wait_mask); | |
7112 | #endif | |
7113 | ||
7114 | Vprocess_alist = Qnil; | |
f388c88a | 7115 | deleted_pid_list = Qnil; |
d486344e | 7116 | for (i = 0; i < FD_SETSIZE; i++) |
f388c88a EZ |
7117 | { |
7118 | chan_process[i] = Qnil; | |
7119 | proc_buffered_char[i] = -1; | |
7120 | } | |
7121 | memset (proc_decode_coding_system, 0, sizeof proc_decode_coding_system); | |
7122 | memset (proc_encode_coding_system, 0, sizeof proc_encode_coding_system); | |
7123 | #ifdef DATAGRAM_SOCKETS | |
7124 | memset (datagram_address, 0, sizeof datagram_address); | |
7125 | #endif | |
7126 | ||
f388c88a EZ |
7127 | { |
7128 | Lisp_Object subfeatures = Qnil; | |
7129 | const struct socket_options *sopt; | |
7130 | ||
7131 | #define ADD_SUBFEATURE(key, val) \ | |
7132 | subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures) | |
7133 | ||
7134 | #ifdef NON_BLOCKING_CONNECT | |
7135 | ADD_SUBFEATURE (QCnowait, Qt); | |
7136 | #endif | |
7137 | #ifdef DATAGRAM_SOCKETS | |
7138 | ADD_SUBFEATURE (QCtype, Qdatagram); | |
7139 | #endif | |
7140 | #ifdef HAVE_SEQPACKET | |
7141 | ADD_SUBFEATURE (QCtype, Qseqpacket); | |
7142 | #endif | |
7143 | #ifdef HAVE_LOCAL_SOCKETS | |
7144 | ADD_SUBFEATURE (QCfamily, Qlocal); | |
7145 | #endif | |
7146 | ADD_SUBFEATURE (QCfamily, Qipv4); | |
7147 | #ifdef AF_INET6 | |
7148 | ADD_SUBFEATURE (QCfamily, Qipv6); | |
7149 | #endif | |
7150 | #ifdef HAVE_GETSOCKNAME | |
7151 | ADD_SUBFEATURE (QCservice, Qt); | |
7152 | #endif | |
f388c88a | 7153 | ADD_SUBFEATURE (QCserver, Qt); |
f388c88a EZ |
7154 | |
7155 | for (sopt = socket_options; sopt->name; sopt++) | |
7156 | subfeatures = pure_cons (intern_c_string (sopt->name), subfeatures); | |
7157 | ||
7158 | Fprovide (intern_c_string ("make-network-process"), subfeatures); | |
7159 | } | |
f388c88a EZ |
7160 | |
7161 | #if defined (DARWIN_OS) | |
7162 | /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive | |
7163 | processes. As such, we only change the default value. */ | |
7164 | if (initialized) | |
7165 | { | |
f44bd759 PE |
7166 | char const *release = (STRINGP (Voperating_system_release) |
7167 | ? SSDATA (Voperating_system_release) | |
7168 | : 0); | |
6de0e799 | 7169 | if (!release || !release[0] || (release[0] < '7' && release[1] == '.')) { |
f388c88a EZ |
7170 | Vprocess_connection_type = Qnil; |
7171 | } | |
7172 | } | |
7173 | #endif | |
7174 | #endif /* subprocesses */ | |
7175 | kbd_is_on_hold = 0; | |
7176 | } | |
7177 | ||
7178 | void | |
7179 | syms_of_process (void) | |
7180 | { | |
7181 | #ifdef subprocesses | |
7182 | ||
cd3520a4 JB |
7183 | DEFSYM (Qprocessp, "processp"); |
7184 | DEFSYM (Qrun, "run"); | |
7185 | DEFSYM (Qstop, "stop"); | |
7186 | DEFSYM (Qsignal, "signal"); | |
f388c88a EZ |
7187 | |
7188 | /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it | |
7189 | here again. | |
934e2a68 | 7190 | |
f388c88a EZ |
7191 | Qexit = intern_c_string ("exit"); |
7192 | staticpro (&Qexit); */ | |
934e2a68 | 7193 | |
cd3520a4 JB |
7194 | DEFSYM (Qopen, "open"); |
7195 | DEFSYM (Qclosed, "closed"); | |
7196 | DEFSYM (Qconnect, "connect"); | |
7197 | DEFSYM (Qfailed, "failed"); | |
7198 | DEFSYM (Qlisten, "listen"); | |
7199 | DEFSYM (Qlocal, "local"); | |
7200 | DEFSYM (Qipv4, "ipv4"); | |
f388c88a | 7201 | #ifdef AF_INET6 |
cd3520a4 JB |
7202 | DEFSYM (Qipv6, "ipv6"); |
7203 | #endif | |
7204 | DEFSYM (Qdatagram, "datagram"); | |
7205 | DEFSYM (Qseqpacket, "seqpacket"); | |
7206 | ||
7207 | DEFSYM (QCport, ":port"); | |
7208 | DEFSYM (QCspeed, ":speed"); | |
7209 | DEFSYM (QCprocess, ":process"); | |
7210 | ||
7211 | DEFSYM (QCbytesize, ":bytesize"); | |
7212 | DEFSYM (QCstopbits, ":stopbits"); | |
7213 | DEFSYM (QCparity, ":parity"); | |
7214 | DEFSYM (Qodd, "odd"); | |
7215 | DEFSYM (Qeven, "even"); | |
7216 | DEFSYM (QCflowcontrol, ":flowcontrol"); | |
7217 | DEFSYM (Qhw, "hw"); | |
7218 | DEFSYM (Qsw, "sw"); | |
7219 | DEFSYM (QCsummary, ":summary"); | |
7220 | ||
7221 | DEFSYM (Qreal, "real"); | |
7222 | DEFSYM (Qnetwork, "network"); | |
7223 | DEFSYM (Qserial, "serial"); | |
7224 | DEFSYM (QCbuffer, ":buffer"); | |
7225 | DEFSYM (QChost, ":host"); | |
7226 | DEFSYM (QCservice, ":service"); | |
7227 | DEFSYM (QClocal, ":local"); | |
7228 | DEFSYM (QCremote, ":remote"); | |
7229 | DEFSYM (QCcoding, ":coding"); | |
7230 | DEFSYM (QCserver, ":server"); | |
7231 | DEFSYM (QCnowait, ":nowait"); | |
7232 | DEFSYM (QCsentinel, ":sentinel"); | |
7233 | DEFSYM (QClog, ":log"); | |
7234 | DEFSYM (QCnoquery, ":noquery"); | |
7235 | DEFSYM (QCstop, ":stop"); | |
7236 | DEFSYM (QCoptions, ":options"); | |
7237 | DEFSYM (QCplist, ":plist"); | |
7238 | ||
7239 | DEFSYM (Qlast_nonmenu_event, "last-nonmenu-event"); | |
934e2a68 | 7240 | |
f388c88a | 7241 | staticpro (&Vprocess_alist); |
f388c88a | 7242 | staticpro (&deleted_pid_list); |
934e2a68 | 7243 | |
f388c88a | 7244 | #endif /* subprocesses */ |
6720a7fb | 7245 | |
cd3520a4 JB |
7246 | DEFSYM (QCname, ":name"); |
7247 | DEFSYM (QCtype, ":type"); | |
7248 | ||
7249 | DEFSYM (Qeuid, "euid"); | |
7250 | DEFSYM (Qegid, "egid"); | |
7251 | DEFSYM (Quser, "user"); | |
7252 | DEFSYM (Qgroup, "group"); | |
7253 | DEFSYM (Qcomm, "comm"); | |
7254 | DEFSYM (Qstate, "state"); | |
7255 | DEFSYM (Qppid, "ppid"); | |
7256 | DEFSYM (Qpgrp, "pgrp"); | |
7257 | DEFSYM (Qsess, "sess"); | |
7258 | DEFSYM (Qttname, "ttname"); | |
7259 | DEFSYM (Qtpgid, "tpgid"); | |
7260 | DEFSYM (Qminflt, "minflt"); | |
7261 | DEFSYM (Qmajflt, "majflt"); | |
7262 | DEFSYM (Qcminflt, "cminflt"); | |
7263 | DEFSYM (Qcmajflt, "cmajflt"); | |
7264 | DEFSYM (Qutime, "utime"); | |
7265 | DEFSYM (Qstime, "stime"); | |
7266 | DEFSYM (Qtime, "time"); | |
7267 | DEFSYM (Qcutime, "cutime"); | |
7268 | DEFSYM (Qcstime, "cstime"); | |
7269 | DEFSYM (Qctime, "ctime"); | |
1aa8d505 SM |
7270 | DEFSYM (Qinternal_default_process_sentinel, |
7271 | "internal-default-process-sentinel"); | |
7272 | DEFSYM (Qinternal_default_process_filter, | |
7273 | "internal-default-process-filter"); | |
cd3520a4 JB |
7274 | DEFSYM (Qpri, "pri"); |
7275 | DEFSYM (Qnice, "nice"); | |
7276 | DEFSYM (Qthcount, "thcount"); | |
7277 | DEFSYM (Qstart, "start"); | |
7278 | DEFSYM (Qvsize, "vsize"); | |
7279 | DEFSYM (Qrss, "rss"); | |
7280 | DEFSYM (Qetime, "etime"); | |
7281 | DEFSYM (Qpcpu, "pcpu"); | |
7282 | DEFSYM (Qpmem, "pmem"); | |
7283 | DEFSYM (Qargs, "args"); | |
9057ff80 | 7284 | |
29208e82 | 7285 | DEFVAR_BOOL ("delete-exited-processes", delete_exited_processes, |
fb7ada5f | 7286 | doc: /* Non-nil means delete processes immediately when they exit. |
f388c88a EZ |
7287 | A value of nil means don't delete them until `list-processes' is run. */); |
7288 | ||
7289 | delete_exited_processes = 1; | |
7290 | ||
462aa963 | 7291 | #ifdef subprocesses |
29208e82 | 7292 | DEFVAR_LISP ("process-connection-type", Vprocess_connection_type, |
f388c88a EZ |
7293 | doc: /* Control type of device used to communicate with subprocesses. |
7294 | Values are nil to use a pipe, or t or `pty' to use a pty. | |
7295 | The value has no effect if the system has no ptys or if all ptys are busy: | |
7296 | then a pipe is used in any case. | |
7297 | The value takes effect when `start-process' is called. */); | |
7298 | Vprocess_connection_type = Qt; | |
7299 | ||
7300 | #ifdef ADAPTIVE_READ_BUFFERING | |
29208e82 | 7301 | DEFVAR_LISP ("process-adaptive-read-buffering", Vprocess_adaptive_read_buffering, |
f388c88a EZ |
7302 | doc: /* If non-nil, improve receive buffering by delaying after short reads. |
7303 | On some systems, when Emacs reads the output from a subprocess, the output data | |
7304 | is read in very small blocks, potentially resulting in very poor performance. | |
7305 | This behavior can be remedied to some extent by setting this variable to a | |
7306 | non-nil value, as it will automatically delay reading from such processes, to | |
7307 | allow them to produce more output before Emacs tries to read it. | |
7308 | If the value is t, the delay is reset after each write to the process; any other | |
7309 | non-nil value means that the delay is not reset on write. | |
7310 | The variable takes effect when `start-process' is called. */); | |
7311 | Vprocess_adaptive_read_buffering = Qt; | |
7312 | #endif | |
7313 | ||
7314 | defsubr (&Sprocessp); | |
7315 | defsubr (&Sget_process); | |
7316 | defsubr (&Sdelete_process); | |
7317 | defsubr (&Sprocess_status); | |
7318 | defsubr (&Sprocess_exit_status); | |
7319 | defsubr (&Sprocess_id); | |
7320 | defsubr (&Sprocess_name); | |
7321 | defsubr (&Sprocess_tty_name); | |
7322 | defsubr (&Sprocess_command); | |
7323 | defsubr (&Sset_process_buffer); | |
7324 | defsubr (&Sprocess_buffer); | |
7325 | defsubr (&Sprocess_mark); | |
7326 | defsubr (&Sset_process_filter); | |
7327 | defsubr (&Sprocess_filter); | |
7328 | defsubr (&Sset_process_sentinel); | |
7329 | defsubr (&Sprocess_sentinel); | |
7330 | defsubr (&Sset_process_window_size); | |
7331 | defsubr (&Sset_process_inherit_coding_system_flag); | |
7332 | defsubr (&Sset_process_query_on_exit_flag); | |
7333 | defsubr (&Sprocess_query_on_exit_flag); | |
7334 | defsubr (&Sprocess_contact); | |
7335 | defsubr (&Sprocess_plist); | |
7336 | defsubr (&Sset_process_plist); | |
f388c88a EZ |
7337 | defsubr (&Sprocess_list); |
7338 | defsubr (&Sstart_process); | |
f388c88a EZ |
7339 | defsubr (&Sserial_process_configure); |
7340 | defsubr (&Smake_serial_process); | |
f388c88a EZ |
7341 | defsubr (&Sset_network_process_option); |
7342 | defsubr (&Smake_network_process); | |
7343 | defsubr (&Sformat_network_address); | |
f388c88a | 7344 | defsubr (&Snetwork_interface_list); |
f388c88a | 7345 | defsubr (&Snetwork_interface_info); |
f388c88a EZ |
7346 | #ifdef DATAGRAM_SOCKETS |
7347 | defsubr (&Sprocess_datagram_address); | |
7348 | defsubr (&Sset_process_datagram_address); | |
7349 | #endif | |
7350 | defsubr (&Saccept_process_output); | |
7351 | defsubr (&Sprocess_send_region); | |
7352 | defsubr (&Sprocess_send_string); | |
7353 | defsubr (&Sinterrupt_process); | |
7354 | defsubr (&Skill_process); | |
7355 | defsubr (&Squit_process); | |
7356 | defsubr (&Sstop_process); | |
7357 | defsubr (&Scontinue_process); | |
7358 | defsubr (&Sprocess_running_child_p); | |
7359 | defsubr (&Sprocess_send_eof); | |
7360 | defsubr (&Ssignal_process); | |
7361 | defsubr (&Swaiting_for_user_input_p); | |
7362 | defsubr (&Sprocess_type); | |
1aa8d505 SM |
7363 | defsubr (&Sinternal_default_process_sentinel); |
7364 | defsubr (&Sinternal_default_process_filter); | |
f388c88a EZ |
7365 | defsubr (&Sset_process_coding_system); |
7366 | defsubr (&Sprocess_coding_system); | |
7367 | defsubr (&Sset_process_filter_multibyte); | |
7368 | defsubr (&Sprocess_filter_multibyte_p); | |
7369 | ||
7370 | #endif /* subprocesses */ | |
7371 | ||
6720a7fb | 7372 | defsubr (&Sget_buffer_process); |
52a1b894 | 7373 | defsubr (&Sprocess_inherit_coding_system_flag); |
934e2a68 | 7374 | defsubr (&Slist_system_processes); |
a20878b6 | 7375 | defsubr (&Sprocess_attributes); |
6720a7fb | 7376 | } |