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