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