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