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