Commit | Line | Data |
---|---|---|
d0d6b7c5 | 1 | /* Asynchronous subprocess control for GNU Emacs. |
c6c5df7f | 2 | Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc. |
d0d6b7c5 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
1dc77cc3 | 8 | the Free Software Foundation; either version 2, or (at your option) |
d0d6b7c5 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | ||
21 | #include <signal.h> | |
22 | ||
18160b98 | 23 | #include <config.h> |
d0d6b7c5 | 24 | |
6720a7fb JB |
25 | /* This file is split into two parts by the following preprocessor |
26 | conditional. The 'then' clause contains all of the support for | |
27 | asynchronous subprocesses. The 'else' clause contains stub | |
28 | versions of some of the asynchronous subprocess routines that are | |
29 | often called elsewhere in Emacs, so we don't have to #ifdef the | |
30 | sections that call them. */ | |
31 | ||
32 | \f | |
d0d6b7c5 | 33 | #ifdef subprocesses |
d0d6b7c5 JB |
34 | |
35 | #include <stdio.h> | |
36 | #include <errno.h> | |
37 | #include <setjmp.h> | |
38 | #include <sys/types.h> /* some typedefs are used in sys/file.h */ | |
39 | #include <sys/file.h> | |
40 | #include <sys/stat.h> | |
41 | ||
42 | #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */ | |
43 | #include <sys/socket.h> | |
44 | #include <netdb.h> | |
45 | #include <netinet/in.h> | |
46 | #include <arpa/inet.h> | |
47 | #endif /* HAVE_SOCKETS */ | |
48 | ||
1d2c16fa RS |
49 | /* TERM is a poor-man's SLIP, used on Linux. */ |
50 | #ifdef TERM | |
51 | #include <client.h> | |
52 | #endif | |
53 | ||
79967d5e RS |
54 | /* DGUX inet_addr returns a 'struct in_addr'. */ |
55 | #ifdef DGUX | |
56 | #define IN_ADDR struct in_addr | |
57 | #define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1) | |
58 | #else | |
59 | #define IN_ADDR unsigned long | |
60 | #define NUMERIC_ADDR_ERROR (numeric_addr == -1) | |
61 | #endif | |
62 | ||
d0d6b7c5 JB |
63 | #if defined(BSD) || defined(STRIDE) |
64 | #include <sys/ioctl.h> | |
0ad77c54 | 65 | #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5) |
d0d6b7c5 JB |
66 | #include <fcntl.h> |
67 | #endif /* HAVE_PTYS and no O_NDELAY */ | |
68 | #endif /* BSD or STRIDE */ | |
d0d6b7c5 JB |
69 | |
70 | #ifdef NEED_BSDTTY | |
71 | #include <bsdtty.h> | |
72 | #endif | |
73 | ||
d0d6b7c5 JB |
74 | #ifdef IRIS |
75 | #include <sys/sysmacros.h> /* for "minor" */ | |
76 | #endif /* not IRIS */ | |
77 | ||
78 | #include "systime.h" | |
36ebaafa | 79 | #include "systty.h" |
d0d6b7c5 JB |
80 | |
81 | #include "lisp.h" | |
82 | #include "window.h" | |
83 | #include "buffer.h" | |
84 | #include "process.h" | |
85 | #include "termhooks.h" | |
86 | #include "termopts.h" | |
87 | #include "commands.h" | |
1dc77cc3 | 88 | #include "frame.h" |
d0d6b7c5 | 89 | |
dd2281ae | 90 | Lisp_Object Qprocessp; |
d0d6b7c5 JB |
91 | Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed; |
92 | /* Qexit is declared and initialized in eval.c. */ | |
93 | ||
94 | /* a process object is a network connection when its childp field is neither | |
95 | Qt nor Qnil but is instead a string (name of foreign host we | |
96 | are connected to + name of port we are connected to) */ | |
97 | ||
98 | #ifdef HAVE_SOCKETS | |
99 | static Lisp_Object stream_process; | |
100 | ||
101 | #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String) | |
102 | #else | |
103 | #define NETCONN_P(p) 0 | |
104 | #endif /* HAVE_SOCKETS */ | |
105 | ||
106 | /* Define first descriptor number available for subprocesses. */ | |
107 | #ifdef VMS | |
108 | #define FIRST_PROC_DESC 1 | |
109 | #else /* Not VMS */ | |
110 | #define FIRST_PROC_DESC 3 | |
111 | #endif | |
112 | ||
113 | /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals | |
114 | testing SIGCHLD. */ | |
115 | ||
116 | #if !defined (SIGCHLD) && defined (SIGCLD) | |
117 | #define SIGCHLD SIGCLD | |
118 | #endif /* SIGCLD */ | |
119 | ||
120 | #include "syssignal.h" | |
121 | ||
889255b4 | 122 | #include "syswait.h" |
d0d6b7c5 JB |
123 | |
124 | extern errno; | |
125 | extern sys_nerr; | |
126 | extern char *sys_errlist[]; | |
127 | ||
128 | #ifndef VMS | |
129 | #ifndef BSD4_1 | |
084fd64a | 130 | #ifndef LINUX |
d0d6b7c5 | 131 | extern char *sys_siglist[]; |
084fd64a | 132 | #endif |
d0d6b7c5 JB |
133 | #else |
134 | char *sys_siglist[] = | |
135 | { | |
136 | "bum signal!!", | |
137 | "hangup", | |
138 | "interrupt", | |
139 | "quit", | |
140 | "illegal instruction", | |
141 | "trace trap", | |
142 | "iot instruction", | |
143 | "emt instruction", | |
144 | "floating point exception", | |
145 | "kill", | |
146 | "bus error", | |
147 | "segmentation violation", | |
148 | "bad argument to system call", | |
149 | "write on a pipe with no one to read it", | |
150 | "alarm clock", | |
151 | "software termination signal from kill", | |
152 | "status signal", | |
153 | "sendable stop signal not from tty", | |
154 | "stop signal from tty", | |
155 | "continue a stopped process", | |
156 | "child status has changed", | |
157 | "background read attempted from control tty", | |
158 | "background write attempted from control tty", | |
159 | "input record available at control tty", | |
160 | "exceeded CPU time limit", | |
161 | "exceeded file size limit" | |
162 | }; | |
163 | #endif | |
164 | #endif /* VMS */ | |
165 | ||
d0d6b7c5 JB |
166 | /* t means use pty, nil means use a pipe, |
167 | maybe other values to come. */ | |
dd2281ae | 168 | static Lisp_Object Vprocess_connection_type; |
d0d6b7c5 JB |
169 | |
170 | #ifdef SKTPAIR | |
171 | #ifndef HAVE_SOCKETS | |
172 | #include <sys/socket.h> | |
173 | #endif | |
174 | #endif /* SKTPAIR */ | |
175 | ||
176 | /* Number of events of change of status of a process. */ | |
dd2281ae | 177 | static int process_tick; |
d0d6b7c5 JB |
178 | |
179 | /* Number of events for which the user or sentinel has been notified. */ | |
dd2281ae | 180 | static int update_tick; |
d0d6b7c5 JB |
181 | |
182 | #ifdef FD_SET | |
183 | /* We could get this from param.h, but better not to depend on finding that. | |
184 | And better not to risk that it might define other symbols used in this | |
185 | file. */ | |
334c4f2f RS |
186 | #ifdef FD_SETSIZE |
187 | #define MAXDESC FD_SETSIZE | |
188 | #else | |
d0d6b7c5 | 189 | #define MAXDESC 64 |
334c4f2f | 190 | #endif |
d0d6b7c5 JB |
191 | #define SELECT_TYPE fd_set |
192 | #else /* no FD_SET */ | |
193 | #define MAXDESC 32 | |
194 | #define SELECT_TYPE int | |
195 | ||
196 | /* Define the macros to access a single-int bitmap of descriptors. */ | |
197 | #define FD_SET(n, p) (*(p) |= (1 << (n))) | |
198 | #define FD_CLR(n, p) (*(p) &= ~(1 << (n))) | |
199 | #define FD_ISSET(n, p) (*(p) & (1 << (n))) | |
200 | #define FD_ZERO(p) (*(p) = 0) | |
201 | #endif /* no FD_SET */ | |
202 | ||
203 | /* Mask of bits indicating the descriptors that we wait for input on */ | |
204 | ||
dd2281ae RS |
205 | static SELECT_TYPE input_wait_mask; |
206 | ||
207 | /* Descriptor to use for keyboard input. */ | |
208 | static int keyboard_descriptor; | |
d0d6b7c5 | 209 | |
dd2281ae RS |
210 | /* Nonzero means delete a process right away if it exits. */ |
211 | static int delete_exited_processes; | |
d0d6b7c5 JB |
212 | |
213 | /* Indexed by descriptor, gives the process (if any) for that descriptor */ | |
dd2281ae | 214 | static Lisp_Object chan_process[MAXDESC]; |
d0d6b7c5 JB |
215 | |
216 | /* Alist of elements (NAME . PROCESS) */ | |
dd2281ae | 217 | static Lisp_Object Vprocess_alist; |
d0d6b7c5 JB |
218 | |
219 | /* Buffered-ahead input char from process, indexed by channel. | |
220 | -1 means empty (no char is buffered). | |
221 | Used on sys V where the only way to tell if there is any | |
222 | output from the process is to read at least one char. | |
223 | Always -1 on systems that support FIONREAD. */ | |
224 | ||
dd2281ae RS |
225 | static int proc_buffered_char[MAXDESC]; |
226 | ||
227 | static Lisp_Object get_process (); | |
d0d6b7c5 JB |
228 | \f |
229 | /* Compute the Lisp form of the process status, p->status, from | |
230 | the numeric status that was returned by `wait'. */ | |
231 | ||
f9738840 JB |
232 | Lisp_Object status_convert (); |
233 | ||
d0d6b7c5 JB |
234 | update_status (p) |
235 | struct Lisp_Process *p; | |
236 | { | |
237 | union { int i; WAITTYPE wt; } u; | |
238 | u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16); | |
239 | p->status = status_convert (u.wt); | |
240 | p->raw_status_low = Qnil; | |
241 | p->raw_status_high = Qnil; | |
242 | } | |
243 | ||
91d10fa8 | 244 | /* Convert a process status word in Unix format to |
d0d6b7c5 JB |
245 | the list that we use internally. */ |
246 | ||
247 | Lisp_Object | |
248 | status_convert (w) | |
249 | WAITTYPE w; | |
250 | { | |
251 | if (WIFSTOPPED (w)) | |
252 | return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil)); | |
253 | else if (WIFEXITED (w)) | |
254 | return Fcons (Qexit, Fcons (make_number (WRETCODE (w)), | |
255 | WCOREDUMP (w) ? Qt : Qnil)); | |
256 | else if (WIFSIGNALED (w)) | |
257 | return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)), | |
258 | WCOREDUMP (w) ? Qt : Qnil)); | |
259 | else | |
260 | return Qrun; | |
261 | } | |
262 | ||
263 | /* Given a status-list, extract the three pieces of information | |
264 | and store them individually through the three pointers. */ | |
265 | ||
266 | void | |
267 | decode_status (l, symbol, code, coredump) | |
268 | Lisp_Object l; | |
269 | Lisp_Object *symbol; | |
270 | int *code; | |
271 | int *coredump; | |
272 | { | |
273 | Lisp_Object tem; | |
274 | ||
275 | if (XTYPE (l) == Lisp_Symbol) | |
276 | { | |
277 | *symbol = l; | |
278 | *code = 0; | |
279 | *coredump = 0; | |
280 | } | |
281 | else | |
282 | { | |
283 | *symbol = XCONS (l)->car; | |
284 | tem = XCONS (l)->cdr; | |
285 | *code = XFASTINT (XCONS (tem)->car); | |
f9738840 | 286 | tem = XCONS (tem)->cdr; |
d0d6b7c5 JB |
287 | *coredump = !NILP (tem); |
288 | } | |
289 | } | |
290 | ||
291 | /* Return a string describing a process status list. */ | |
292 | ||
293 | Lisp_Object | |
294 | status_message (status) | |
295 | Lisp_Object status; | |
296 | { | |
297 | Lisp_Object symbol; | |
298 | int code, coredump; | |
299 | Lisp_Object string, string2; | |
300 | ||
301 | decode_status (status, &symbol, &code, &coredump); | |
302 | ||
303 | if (EQ (symbol, Qsignal) || EQ (symbol, Qstop)) | |
304 | { | |
b0310da4 | 305 | #ifndef VMS |
d0d6b7c5 | 306 | string = build_string (code < NSIG ? sys_siglist[code] : "unknown"); |
b0310da4 JB |
307 | #else |
308 | string = build_string (code < NSIG ? sys_errlist[code] : "unknown"); | |
309 | #endif | |
d0d6b7c5 JB |
310 | string2 = build_string (coredump ? " (core dumped)\n" : "\n"); |
311 | XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]); | |
312 | return concat2 (string, string2); | |
313 | } | |
314 | else if (EQ (symbol, Qexit)) | |
315 | { | |
316 | if (code == 0) | |
317 | return build_string ("finished\n"); | |
f2980264 | 318 | string = Fnumber_to_string (make_number (code)); |
d0d6b7c5 JB |
319 | string2 = build_string (coredump ? " (core dumped)\n" : "\n"); |
320 | return concat2 (build_string ("exited abnormally with code "), | |
321 | concat2 (string, string2)); | |
322 | } | |
323 | else | |
324 | return Fcopy_sequence (Fsymbol_name (symbol)); | |
325 | } | |
326 | \f | |
327 | #ifdef HAVE_PTYS | |
d0d6b7c5 JB |
328 | |
329 | /* Open an available pty, returning a file descriptor. | |
330 | Return -1 on failure. | |
331 | The file name of the terminal corresponding to the pty | |
332 | is left in the variable pty_name. */ | |
333 | ||
334 | char pty_name[24]; | |
335 | ||
336 | int | |
337 | allocate_pty () | |
338 | { | |
339 | struct stat stb; | |
340 | register c, i; | |
341 | int fd; | |
342 | ||
32676c08 JB |
343 | /* Some systems name their pseudoterminals so that there are gaps in |
344 | the usual sequence - for example, on HP9000/S700 systems, there | |
345 | are no pseudoterminals with names ending in 'f'. So we wait for | |
346 | three failures in a row before deciding that we've reached the | |
347 | end of the ptys. */ | |
348 | int failed_count = 0; | |
349 | ||
d0d6b7c5 JB |
350 | #ifdef PTY_ITERATION |
351 | PTY_ITERATION | |
352 | #else | |
353 | for (c = FIRST_PTY_LETTER; c <= 'z'; c++) | |
354 | for (i = 0; i < 16; i++) | |
355 | #endif | |
356 | { | |
357 | #ifdef PTY_NAME_SPRINTF | |
358 | PTY_NAME_SPRINTF | |
d0d6b7c5 JB |
359 | #else |
360 | sprintf (pty_name, "/dev/pty%c%x", c, i); | |
d0d6b7c5 JB |
361 | #endif /* no PTY_NAME_SPRINTF */ |
362 | ||
4d7c105e RS |
363 | #ifdef PTY_OPEN |
364 | PTY_OPEN; | |
365 | #else /* no PTY_OPEN */ | |
32676c08 JB |
366 | #ifdef IRIS |
367 | /* Unusual IRIS code */ | |
368 | *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0); | |
369 | if (fd < 0) | |
370 | return -1; | |
371 | if (fstat (fd, &stb) < 0) | |
d0d6b7c5 | 372 | return -1; |
4d7c105e | 373 | #else /* not IRIS */ |
32676c08 JB |
374 | if (stat (pty_name, &stb) < 0) |
375 | { | |
376 | failed_count++; | |
377 | if (failed_count >= 3) | |
378 | return -1; | |
379 | } | |
380 | else | |
381 | failed_count = 0; | |
d0d6b7c5 JB |
382 | #ifdef O_NONBLOCK |
383 | fd = open (pty_name, O_RDWR | O_NONBLOCK, 0); | |
384 | #else | |
385 | fd = open (pty_name, O_RDWR | O_NDELAY, 0); | |
386 | #endif | |
4d7c105e RS |
387 | #endif /* not IRIS */ |
388 | #endif /* no PTY_OPEN */ | |
d0d6b7c5 JB |
389 | |
390 | if (fd >= 0) | |
391 | { | |
392 | /* check to make certain that both sides are available | |
393 | this avoids a nasty yet stupid bug in rlogins */ | |
394 | #ifdef PTY_TTY_NAME_SPRINTF | |
395 | PTY_TTY_NAME_SPRINTF | |
d0d6b7c5 JB |
396 | #else |
397 | sprintf (pty_name, "/dev/tty%c%x", c, i); | |
d0d6b7c5 JB |
398 | #endif /* no PTY_TTY_NAME_SPRINTF */ |
399 | #ifndef UNIPLUS | |
400 | if (access (pty_name, 6) != 0) | |
401 | { | |
402 | close (fd); | |
fad97cbe | 403 | #if !defined(IRIS) && !defined(__sgi) |
d0d6b7c5 JB |
404 | continue; |
405 | #else | |
406 | return -1; | |
407 | #endif /* IRIS */ | |
408 | } | |
409 | #endif /* not UNIPLUS */ | |
410 | setup_pty (fd); | |
411 | return fd; | |
412 | } | |
413 | } | |
414 | return -1; | |
415 | } | |
416 | #endif /* HAVE_PTYS */ | |
417 | \f | |
418 | Lisp_Object | |
419 | make_process (name) | |
420 | Lisp_Object name; | |
421 | { | |
422 | register Lisp_Object val, tem, name1; | |
423 | register struct Lisp_Process *p; | |
424 | char suffix[10]; | |
425 | register int i; | |
426 | ||
427 | /* size of process structure includes the vector header, | |
428 | so deduct for that. But struct Lisp_Vector includes the first | |
429 | element, thus deducts too much, so add it back. */ | |
430 | val = Fmake_vector (make_number ((sizeof (struct Lisp_Process) | |
431 | - sizeof (struct Lisp_Vector) | |
432 | + sizeof (Lisp_Object)) | |
433 | / sizeof (Lisp_Object)), | |
434 | Qnil); | |
435 | XSETTYPE (val, Lisp_Process); | |
436 | ||
437 | p = XPROCESS (val); | |
a9f2c884 RS |
438 | XSET (p->infd, Lisp_Int, -1); |
439 | XSET (p->outfd, Lisp_Int, -1); | |
d0d6b7c5 JB |
440 | XFASTINT (p->pid) = 0; |
441 | XFASTINT (p->tick) = 0; | |
442 | XFASTINT (p->update_tick) = 0; | |
443 | p->raw_status_low = Qnil; | |
444 | p->raw_status_high = Qnil; | |
445 | p->status = Qrun; | |
446 | p->mark = Fmake_marker (); | |
447 | ||
448 | /* If name is already in use, modify it until it is unused. */ | |
449 | ||
450 | name1 = name; | |
451 | for (i = 1; ; i++) | |
452 | { | |
453 | tem = Fget_process (name1); | |
454 | if (NILP (tem)) break; | |
455 | sprintf (suffix, "<%d>", i); | |
456 | name1 = concat2 (name, build_string (suffix)); | |
457 | } | |
458 | name = name1; | |
459 | p->name = name; | |
460 | Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist); | |
461 | return val; | |
462 | } | |
463 | ||
464 | remove_process (proc) | |
465 | register Lisp_Object proc; | |
466 | { | |
467 | register Lisp_Object pair; | |
468 | ||
469 | pair = Frassq (proc, Vprocess_alist); | |
470 | Vprocess_alist = Fdelq (pair, Vprocess_alist); | |
471 | Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil); | |
472 | ||
473 | deactivate_process (proc); | |
474 | } | |
475 | \f | |
476 | DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0, | |
477 | "Return t if OBJECT is a process.") | |
478 | (obj) | |
479 | Lisp_Object obj; | |
480 | { | |
481 | return XTYPE (obj) == Lisp_Process ? Qt : Qnil; | |
482 | } | |
483 | ||
484 | DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0, | |
485 | "Return the process named NAME, or nil if there is none.") | |
486 | (name) | |
487 | register Lisp_Object name; | |
488 | { | |
489 | if (XTYPE (name) == Lisp_Process) | |
490 | return name; | |
491 | CHECK_STRING (name, 0); | |
492 | return Fcdr (Fassoc (name, Vprocess_alist)); | |
493 | } | |
494 | ||
495 | DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0, | |
496 | "Return the (or, a) process associated with BUFFER.\n\ | |
497 | BUFFER may be a buffer or the name of one.") | |
498 | (name) | |
499 | register Lisp_Object name; | |
500 | { | |
501 | register Lisp_Object buf, tail, proc; | |
502 | ||
503 | if (NILP (name)) return Qnil; | |
504 | buf = Fget_buffer (name); | |
505 | if (NILP (buf)) return Qnil; | |
506 | ||
507 | for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail)) | |
508 | { | |
509 | proc = Fcdr (Fcar (tail)); | |
510 | if (XTYPE (proc) == Lisp_Process && EQ (XPROCESS (proc)->buffer, buf)) | |
511 | return proc; | |
512 | } | |
513 | return Qnil; | |
514 | } | |
515 | ||
ebb9e16f JB |
516 | /* This is how commands for the user decode process arguments. It |
517 | accepts a process, a process name, a buffer, a buffer name, or nil. | |
518 | Buffers denote the first process in the buffer, and nil denotes the | |
519 | current buffer. */ | |
d0d6b7c5 | 520 | |
77b221b1 | 521 | static Lisp_Object |
d0d6b7c5 JB |
522 | get_process (name) |
523 | register Lisp_Object name; | |
524 | { | |
525 | register Lisp_Object proc; | |
526 | if (NILP (name)) | |
527 | proc = Fget_buffer_process (Fcurrent_buffer ()); | |
528 | else | |
529 | { | |
530 | proc = Fget_process (name); | |
531 | if (NILP (proc)) | |
532 | proc = Fget_buffer_process (Fget_buffer (name)); | |
533 | } | |
534 | ||
535 | if (!NILP (proc)) | |
536 | return proc; | |
537 | ||
538 | if (NILP (name)) | |
539 | error ("Current buffer has no process"); | |
540 | else | |
541 | error ("Process %s does not exist", XSTRING (name)->data); | |
542 | /* NOTREACHED */ | |
543 | } | |
544 | ||
545 | DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0, | |
546 | "Delete PROCESS: kill it and forget about it immediately.\n\ | |
ebb9e16f JB |
547 | PROCESS may be a process, a buffer, the name of a process or buffer, or\n\ |
548 | nil, indicating the current buffer's process.") | |
d0d6b7c5 JB |
549 | (proc) |
550 | register Lisp_Object proc; | |
551 | { | |
552 | proc = get_process (proc); | |
553 | XPROCESS (proc)->raw_status_low = Qnil; | |
554 | XPROCESS (proc)->raw_status_high = Qnil; | |
555 | if (NETCONN_P (proc)) | |
556 | { | |
557 | XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil)); | |
558 | XSETINT (XPROCESS (proc)->tick, ++process_tick); | |
559 | } | |
a9f2c884 | 560 | else if (XINT (XPROCESS (proc)->infd >= 0)) |
d0d6b7c5 JB |
561 | { |
562 | Fkill_process (proc, Qnil); | |
563 | /* Do this now, since remove_process will make sigchld_handler do nothing. */ | |
564 | XPROCESS (proc)->status | |
565 | = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil)); | |
566 | XSETINT (XPROCESS (proc)->tick, ++process_tick); | |
567 | status_notify (); | |
568 | } | |
569 | remove_process (proc); | |
570 | return Qnil; | |
571 | } | |
572 | \f | |
573 | DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0, | |
574 | "Return the status of PROCESS: a symbol, one of these:\n\ | |
575 | run -- for a process that is running.\n\ | |
576 | stop -- for a process stopped but continuable.\n\ | |
577 | exit -- for a process that has exited.\n\ | |
578 | signal -- for a process that has got a fatal signal.\n\ | |
579 | open -- for a network stream connection that is open.\n\ | |
580 | closed -- for a network stream connection that is closed.\n\ | |
ebb9e16f JB |
581 | nil -- if arg is a process name and no such process exists.\n\ |
582 | PROCESS may be a process, a buffer, the name of a process or buffer, or\n\ | |
583 | nil, indicating the current buffer's process.") | |
d0d6b7c5 JB |
584 | (proc) |
585 | register Lisp_Object proc; | |
586 | { | |
587 | register struct Lisp_Process *p; | |
588 | register Lisp_Object status; | |
eb4686e4 | 589 | proc = Fget_process (proc); |
d0d6b7c5 JB |
590 | if (NILP (proc)) |
591 | return proc; | |
592 | p = XPROCESS (proc); | |
593 | if (!NILP (p->raw_status_low)) | |
594 | update_status (p); | |
595 | status = p->status; | |
596 | if (XTYPE (status) == Lisp_Cons) | |
597 | status = XCONS (status)->car; | |
598 | if (NETCONN_P (proc)) | |
599 | { | |
600 | if (EQ (status, Qrun)) | |
601 | status = Qopen; | |
602 | else if (EQ (status, Qexit)) | |
603 | status = Qclosed; | |
604 | } | |
605 | return status; | |
606 | } | |
607 | ||
608 | DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status, | |
609 | 1, 1, 0, | |
610 | "Return the exit status of PROCESS or the signal number that killed it.\n\ | |
611 | If PROCESS has not yet exited or died, return 0.") | |
612 | (proc) | |
613 | register Lisp_Object proc; | |
614 | { | |
615 | CHECK_PROCESS (proc, 0); | |
616 | if (!NILP (XPROCESS (proc)->raw_status_low)) | |
617 | update_status (XPROCESS (proc)); | |
618 | if (XTYPE (XPROCESS (proc)->status) == Lisp_Cons) | |
619 | return XCONS (XCONS (XPROCESS (proc)->status)->cdr)->car; | |
620 | return make_number (0); | |
621 | } | |
622 | ||
623 | DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0, | |
624 | "Return the process id of PROCESS.\n\ | |
625 | This is the pid of the Unix process which PROCESS uses or talks to.\n\ | |
626 | For a network connection, this value is nil.") | |
627 | (proc) | |
628 | register Lisp_Object proc; | |
629 | { | |
630 | CHECK_PROCESS (proc, 0); | |
631 | return XPROCESS (proc)->pid; | |
632 | } | |
633 | ||
634 | DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0, | |
635 | "Return the name of PROCESS, as a string.\n\ | |
636 | This is the name of the program invoked in PROCESS,\n\ | |
637 | possibly modified to make it unique among process names.") | |
638 | (proc) | |
639 | register Lisp_Object proc; | |
640 | { | |
641 | CHECK_PROCESS (proc, 0); | |
642 | return XPROCESS (proc)->name; | |
643 | } | |
644 | ||
645 | DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0, | |
646 | "Return the command that was executed to start PROCESS.\n\ | |
647 | This is a list of strings, the first string being the program executed\n\ | |
648 | and the rest of the strings being the arguments given to it.\n\ | |
649 | For a non-child channel, this is nil.") | |
650 | (proc) | |
651 | register Lisp_Object proc; | |
652 | { | |
653 | CHECK_PROCESS (proc, 0); | |
654 | return XPROCESS (proc)->command; | |
655 | } | |
656 | ||
657 | DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer, | |
658 | 2, 2, 0, | |
659 | "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).") | |
660 | (proc, buffer) | |
661 | register Lisp_Object proc, buffer; | |
662 | { | |
663 | CHECK_PROCESS (proc, 0); | |
664 | if (!NILP (buffer)) | |
665 | CHECK_BUFFER (buffer, 1); | |
666 | XPROCESS (proc)->buffer = buffer; | |
667 | return buffer; | |
668 | } | |
669 | ||
670 | DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer, | |
671 | 1, 1, 0, | |
672 | "Return the buffer PROCESS is associated with.\n\ | |
673 | Output from PROCESS is inserted in this buffer\n\ | |
674 | unless PROCESS has a filter.") | |
675 | (proc) | |
676 | register Lisp_Object proc; | |
677 | { | |
678 | CHECK_PROCESS (proc, 0); | |
679 | return XPROCESS (proc)->buffer; | |
680 | } | |
681 | ||
682 | DEFUN ("process-mark", Fprocess_mark, Sprocess_mark, | |
683 | 1, 1, 0, | |
684 | "Return the marker for the end of the last output from PROCESS.") | |
685 | (proc) | |
686 | register Lisp_Object proc; | |
687 | { | |
688 | CHECK_PROCESS (proc, 0); | |
689 | return XPROCESS (proc)->mark; | |
690 | } | |
691 | ||
692 | DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter, | |
693 | 2, 2, 0, | |
694 | "Give PROCESS the filter function FILTER; nil means no filter.\n\ | |
20ddfff7 | 695 | t means stop accepting output from the process.\n\ |
d0d6b7c5 JB |
696 | When a process has a filter, each time it does output\n\ |
697 | the entire string of output is passed to the filter.\n\ | |
698 | The filter gets two arguments: the process and the string of output.\n\ | |
699 | If the process has a filter, its buffer is not used for output.") | |
700 | (proc, filter) | |
701 | register Lisp_Object proc, filter; | |
702 | { | |
703 | CHECK_PROCESS (proc, 0); | |
20ddfff7 | 704 | if (EQ (filter, Qt)) |
a9f2c884 | 705 | FD_CLR (XINT (XPROCESS (proc)->infd), &input_wait_mask); |
20ddfff7 | 706 | else if (EQ (XPROCESS (proc)->filter, Qt)) |
a9f2c884 | 707 | FD_SET (XINT (XPROCESS (proc)->infd), &input_wait_mask); |
d0d6b7c5 JB |
708 | XPROCESS (proc)->filter = filter; |
709 | return filter; | |
710 | } | |
711 | ||
712 | DEFUN ("process-filter", Fprocess_filter, Sprocess_filter, | |
713 | 1, 1, 0, | |
714 | "Returns the filter function of PROCESS; nil if none.\n\ | |
715 | See `set-process-filter' for more info on filter functions.") | |
716 | (proc) | |
717 | register Lisp_Object proc; | |
718 | { | |
719 | CHECK_PROCESS (proc, 0); | |
720 | return XPROCESS (proc)->filter; | |
721 | } | |
722 | ||
723 | DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel, | |
724 | 2, 2, 0, | |
725 | "Give PROCESS the sentinel SENTINEL; nil for none.\n\ | |
726 | The sentinel is called as a function when the process changes state.\n\ | |
727 | It gets two arguments: the process, and a string describing the change.") | |
728 | (proc, sentinel) | |
729 | register Lisp_Object proc, sentinel; | |
730 | { | |
731 | CHECK_PROCESS (proc, 0); | |
732 | XPROCESS (proc)->sentinel = sentinel; | |
733 | return sentinel; | |
734 | } | |
735 | ||
736 | DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel, | |
737 | 1, 1, 0, | |
738 | "Return the sentinel of PROCESS; nil if none.\n\ | |
739 | See `set-process-sentinel' for more info on sentinels.") | |
740 | (proc) | |
741 | register Lisp_Object proc; | |
742 | { | |
743 | CHECK_PROCESS (proc, 0); | |
744 | return XPROCESS (proc)->sentinel; | |
745 | } | |
746 | ||
747 | DEFUN ("process-kill-without-query", Fprocess_kill_without_query, | |
748 | Sprocess_kill_without_query, 1, 2, 0, | |
749 | "Say no query needed if PROCESS is running when Emacs is exited.\n\ | |
750 | Optional second argument if non-nill says to require a query.\n\ | |
751 | Value is t if a query was formerly required.") | |
752 | (proc, value) | |
753 | register Lisp_Object proc, value; | |
754 | { | |
755 | Lisp_Object tem; | |
756 | ||
757 | CHECK_PROCESS (proc, 0); | |
758 | tem = XPROCESS (proc)->kill_without_query; | |
759 | XPROCESS (proc)->kill_without_query = Fnull (value); | |
760 | ||
761 | return Fnull (tem); | |
762 | } | |
312c9964 RS |
763 | |
764 | #if 0 /* Turned off because we don't currently record this info | |
765 | in the process. Perhaps add it. */ | |
766 | DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0, | |
767 | "Return the connection type of `PROCESS'.\n\ | |
768 | The value is `nil' for a pipe,\n\ | |
769 | `t' or `pty' for a pty, or `stream' for a socket connection.") | |
770 | (process) | |
771 | Lisp_Object process; | |
772 | { | |
773 | return XPROCESS (process)->type; | |
774 | } | |
775 | #endif | |
d0d6b7c5 JB |
776 | \f |
777 | Lisp_Object | |
778 | list_processes_1 () | |
779 | { | |
780 | register Lisp_Object tail, tem; | |
781 | Lisp_Object proc, minspace, tem1; | |
782 | register struct buffer *old = current_buffer; | |
783 | register struct Lisp_Process *p; | |
784 | register int state; | |
785 | char tembuf[80]; | |
786 | ||
787 | XFASTINT (minspace) = 1; | |
788 | ||
789 | set_buffer_internal (XBUFFER (Vstandard_output)); | |
790 | Fbuffer_disable_undo (Vstandard_output); | |
791 | ||
792 | current_buffer->truncate_lines = Qt; | |
793 | ||
794 | write_string ("\ | |
795 | Proc Status Buffer Command\n\ | |
796 | ---- ------ ------ -------\n", -1); | |
797 | ||
798 | for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail)) | |
799 | { | |
800 | Lisp_Object symbol; | |
801 | ||
802 | proc = Fcdr (Fcar (tail)); | |
803 | p = XPROCESS (proc); | |
804 | if (NILP (p->childp)) | |
805 | continue; | |
806 | ||
807 | Finsert (1, &p->name); | |
808 | Findent_to (make_number (13), minspace); | |
809 | ||
810 | if (!NILP (p->raw_status_low)) | |
811 | update_status (p); | |
812 | symbol = p->status; | |
813 | if (XTYPE (p->status) == Lisp_Cons) | |
814 | symbol = XCONS (p->status)->car; | |
815 | ||
816 | ||
817 | if (EQ (symbol, Qsignal)) | |
818 | { | |
819 | Lisp_Object tem; | |
820 | tem = Fcar (Fcdr (p->status)); | |
821 | #ifdef VMS | |
822 | if (XINT (tem) < NSIG) | |
b0310da4 | 823 | write_string (sys_errlist [XINT (tem)], -1); |
d0d6b7c5 JB |
824 | else |
825 | #endif | |
826 | Fprinc (symbol, Qnil); | |
827 | } | |
828 | else if (NETCONN_P (proc)) | |
829 | { | |
830 | if (EQ (symbol, Qrun)) | |
831 | write_string ("open", -1); | |
832 | else if (EQ (symbol, Qexit)) | |
833 | write_string ("closed", -1); | |
834 | else | |
835 | Fprinc (symbol, Qnil); | |
836 | } | |
837 | else | |
838 | Fprinc (symbol, Qnil); | |
839 | ||
840 | if (EQ (symbol, Qexit)) | |
841 | { | |
842 | Lisp_Object tem; | |
843 | tem = Fcar (Fcdr (p->status)); | |
844 | if (XFASTINT (tem)) | |
845 | { | |
846 | sprintf (tembuf, " %d", XFASTINT (tem)); | |
847 | write_string (tembuf, -1); | |
848 | } | |
849 | } | |
850 | ||
851 | if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)) | |
852 | remove_process (proc); | |
853 | ||
854 | Findent_to (make_number (22), minspace); | |
855 | if (NILP (p->buffer)) | |
856 | insert_string ("(none)"); | |
857 | else if (NILP (XBUFFER (p->buffer)->name)) | |
858 | insert_string ("(Killed)"); | |
859 | else | |
860 | Finsert (1, &XBUFFER (p->buffer)->name); | |
861 | ||
862 | Findent_to (make_number (37), minspace); | |
863 | ||
864 | if (NETCONN_P (proc)) | |
865 | { | |
866 | sprintf (tembuf, "(network stream connection to %s)\n", | |
867 | XSTRING (p->childp)->data); | |
868 | insert_string (tembuf); | |
869 | } | |
870 | else | |
871 | { | |
872 | tem = p->command; | |
873 | while (1) | |
874 | { | |
875 | tem1 = Fcar (tem); | |
876 | Finsert (1, &tem1); | |
877 | tem = Fcdr (tem); | |
878 | if (NILP (tem)) | |
879 | break; | |
880 | insert_string (" "); | |
881 | } | |
882 | insert_string ("\n"); | |
883 | } | |
884 | } | |
885 | return Qnil; | |
886 | } | |
887 | ||
888 | DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "", | |
889 | "Display a list of all processes.\n\ | |
890 | \(Any processes listed as Exited or Signaled are actually eliminated\n\ | |
891 | after the listing is made.)") | |
892 | () | |
893 | { | |
894 | internal_with_output_to_temp_buffer ("*Process List*", | |
895 | list_processes_1, Qnil); | |
896 | return Qnil; | |
897 | } | |
898 | ||
899 | DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0, | |
900 | "Return a list of all processes.") | |
901 | () | |
902 | { | |
903 | return Fmapcar (Qcdr, Vprocess_alist); | |
904 | } | |
905 | \f | |
b0310da4 JB |
906 | /* Starting asynchronous inferior processes. */ |
907 | ||
908 | static Lisp_Object start_process_unwind (); | |
909 | ||
d0d6b7c5 JB |
910 | DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0, |
911 | "Start a program in a subprocess. Return the process object for it.\n\ | |
912 | Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\ | |
913 | NAME is name for process. It is modified if necessary to make it unique.\n\ | |
914 | BUFFER is the buffer or (buffer-name) to associate with the process.\n\ | |
915 | Process output goes at end of that buffer, unless you specify\n\ | |
916 | an output stream or filter function to handle the output.\n\ | |
917 | BUFFER may be also nil, meaning that this process is not associated\n\ | |
918 | with any buffer\n\ | |
919 | Third arg is program file name. It is searched for as in the shell.\n\ | |
920 | Remaining arguments are strings to give program as arguments.") | |
921 | (nargs, args) | |
922 | int nargs; | |
923 | register Lisp_Object *args; | |
924 | { | |
1e30af70 | 925 | Lisp_Object buffer, name, program, proc, current_dir, tem; |
d0d6b7c5 JB |
926 | #ifdef VMS |
927 | register unsigned char *new_argv; | |
928 | int len; | |
929 | #else | |
930 | register unsigned char **new_argv; | |
931 | #endif | |
932 | register int i; | |
b0310da4 | 933 | int count = specpdl_ptr - specpdl; |
d0d6b7c5 JB |
934 | |
935 | buffer = args[1]; | |
936 | if (!NILP (buffer)) | |
937 | buffer = Fget_buffer_create (buffer); | |
938 | ||
1e30af70 JB |
939 | /* Make sure that the child will be able to chdir to the current |
940 | buffer's current directory, or its unhandled equivalent. We | |
941 | can't just have the child check for an error when it does the | |
942 | chdir, since it's in a vfork. | |
943 | ||
944 | We have to GCPRO around this because Fexpand_file_name and | |
945 | Funhandled_file_name_directory might call a file name handling | |
946 | function. The argument list is protected by the caller, so all | |
947 | we really have to worry about is buffer. */ | |
948 | { | |
949 | struct gcpro gcpro1, gcpro2; | |
950 | ||
951 | current_dir = current_buffer->directory; | |
952 | ||
953 | GCPRO2 (buffer, current_dir); | |
954 | ||
955 | current_dir = | |
956 | expand_and_dir_to_file | |
82df4891 | 957 | (Funhandled_file_name_directory (current_dir), Qnil); |
1e30af70 JB |
958 | if (NILP (Ffile_accessible_directory_p (current_dir))) |
959 | report_file_error ("Setting current directory", | |
960 | Fcons (current_buffer->directory, Qnil)); | |
961 | ||
962 | UNGCPRO; | |
963 | } | |
964 | ||
d0d6b7c5 JB |
965 | name = args[0]; |
966 | CHECK_STRING (name, 0); | |
967 | ||
968 | program = args[2]; | |
969 | ||
970 | CHECK_STRING (program, 2); | |
971 | ||
972 | #ifdef VMS | |
973 | /* Make a one member argv with all args concatenated | |
974 | together separated by a blank. */ | |
975 | len = XSTRING (program)->size + 2; | |
976 | for (i = 3; i < nargs; i++) | |
977 | { | |
978 | tem = args[i]; | |
979 | CHECK_STRING (tem, i); | |
980 | len += XSTRING (tem)->size + 1; /* count the blank */ | |
981 | } | |
982 | new_argv = (unsigned char *) alloca (len); | |
983 | strcpy (new_argv, XSTRING (program)->data); | |
984 | for (i = 3; i < nargs; i++) | |
985 | { | |
986 | tem = args[i]; | |
987 | CHECK_STRING (tem, i); | |
988 | strcat (new_argv, " "); | |
989 | strcat (new_argv, XSTRING (tem)->data); | |
990 | } | |
b0310da4 JB |
991 | /* Need to add code here to check for program existence on VMS */ |
992 | ||
d0d6b7c5 JB |
993 | #else /* not VMS */ |
994 | new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *)); | |
995 | ||
996 | for (i = 3; i < nargs; i++) | |
997 | { | |
998 | tem = args[i]; | |
999 | CHECK_STRING (tem, i); | |
1000 | new_argv[i - 2] = XSTRING (tem)->data; | |
1001 | } | |
1002 | new_argv[i - 2] = 0; | |
1003 | new_argv[0] = XSTRING (program)->data; | |
1004 | ||
1005 | /* If program file name is not absolute, search our path for it */ | |
1006 | if (new_argv[0][0] != '/') | |
1007 | { | |
1008 | tem = Qnil; | |
5437e9f9 | 1009 | openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1); |
d0d6b7c5 JB |
1010 | if (NILP (tem)) |
1011 | report_file_error ("Searching for program", Fcons (program, Qnil)); | |
1012 | new_argv[0] = XSTRING (tem)->data; | |
1013 | } | |
1014 | #endif /* not VMS */ | |
1015 | ||
1016 | proc = make_process (name); | |
b0310da4 JB |
1017 | /* If an error occurs and we can't start the process, we want to |
1018 | remove it from the process list. This means that each error | |
1019 | check in create_process doesn't need to call remove_process | |
1020 | itself; it's all taken care of here. */ | |
1021 | record_unwind_protect (start_process_unwind, proc); | |
d0d6b7c5 JB |
1022 | |
1023 | XPROCESS (proc)->childp = Qt; | |
1024 | XPROCESS (proc)->command_channel_p = Qnil; | |
1025 | XPROCESS (proc)->buffer = buffer; | |
1026 | XPROCESS (proc)->sentinel = Qnil; | |
1027 | XPROCESS (proc)->filter = Qnil; | |
1028 | XPROCESS (proc)->command = Flist (nargs - 2, args + 2); | |
1029 | ||
1e30af70 | 1030 | create_process (proc, new_argv, current_dir); |
d0d6b7c5 | 1031 | |
b0310da4 | 1032 | return unbind_to (count, proc); |
d0d6b7c5 JB |
1033 | } |
1034 | ||
b0310da4 JB |
1035 | /* This function is the unwind_protect form for Fstart_process. If |
1036 | PROC doesn't have its pid set, then we know someone has signalled | |
1037 | an error and the process wasn't started successfully, so we should | |
1038 | remove it from the process list. */ | |
1039 | static Lisp_Object | |
1040 | start_process_unwind (proc) | |
1041 | Lisp_Object proc; | |
1042 | { | |
1043 | if (XTYPE (proc) != Lisp_Process) | |
1044 | abort (); | |
1045 | ||
1046 | /* Was PROC started successfully? */ | |
188d6c4e | 1047 | if (XINT (XPROCESS (proc)->pid) <= 0) |
b0310da4 JB |
1048 | remove_process (proc); |
1049 | ||
1050 | return Qnil; | |
1051 | } | |
1052 | ||
1053 | ||
d0d6b7c5 JB |
1054 | SIGTYPE |
1055 | create_process_1 (signo) | |
1056 | int signo; | |
1057 | { | |
1058 | #ifdef USG | |
1059 | /* USG systems forget handlers when they are used; | |
1060 | must reestablish each time */ | |
1061 | signal (signo, create_process_1); | |
1062 | #endif /* USG */ | |
1063 | } | |
1064 | ||
1065 | #if 0 /* This doesn't work; see the note before sigchld_handler. */ | |
1066 | #ifdef USG | |
1067 | #ifdef SIGCHLD | |
1068 | /* Mimic blocking of signals on system V, which doesn't really have it. */ | |
1069 | ||
1070 | /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */ | |
1071 | int sigchld_deferred; | |
1072 | ||
1073 | SIGTYPE | |
1074 | create_process_sigchld () | |
1075 | { | |
1076 | signal (SIGCHLD, create_process_sigchld); | |
1077 | ||
1078 | sigchld_deferred = 1; | |
1079 | } | |
1080 | #endif | |
1081 | #endif | |
1082 | #endif | |
1083 | ||
1084 | #ifndef VMS /* VMS version of this function is in vmsproc.c. */ | |
1e30af70 | 1085 | create_process (process, new_argv, current_dir) |
d0d6b7c5 JB |
1086 | Lisp_Object process; |
1087 | char **new_argv; | |
1e30af70 | 1088 | Lisp_Object current_dir; |
d0d6b7c5 JB |
1089 | { |
1090 | int pid, inchannel, outchannel, forkin, forkout; | |
1091 | int sv[2]; | |
1092 | #ifdef SIGCHLD | |
1093 | SIGTYPE (*sigchld)(); | |
1094 | #endif | |
1095 | int pty_flag = 0; | |
d0d6b7c5 JB |
1096 | extern char **environ; |
1097 | ||
d0d6b7c5 JB |
1098 | inchannel = outchannel = -1; |
1099 | ||
1100 | #ifdef HAVE_PTYS | |
1101 | if (EQ (Vprocess_connection_type, Qt)) | |
1102 | outchannel = inchannel = allocate_pty (); | |
1103 | ||
d0d6b7c5 JB |
1104 | if (inchannel >= 0) |
1105 | { | |
1106 | #ifndef USG | |
1107 | /* On USG systems it does not work to open the pty's tty here | |
1108 | and then close and reopen it in the child. */ | |
1109 | #ifdef O_NOCTTY | |
1110 | /* Don't let this terminal become our controlling terminal | |
1111 | (in case we don't have one). */ | |
1112 | forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0); | |
1113 | #else | |
1114 | forkout = forkin = open (pty_name, O_RDWR, 0); | |
1115 | #endif | |
1116 | if (forkin < 0) | |
1117 | report_file_error ("Opening pty", Qnil); | |
1118 | #else | |
1119 | forkin = forkout = -1; | |
1120 | #endif /* not USG */ | |
1121 | pty_flag = 1; | |
1122 | } | |
1123 | else | |
1124 | #endif /* HAVE_PTYS */ | |
1125 | #ifdef SKTPAIR | |
1126 | { | |
1127 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0) | |
1128 | report_file_error ("Opening socketpair", Qnil); | |
1129 | outchannel = inchannel = sv[0]; | |
1130 | forkout = forkin = sv[1]; | |
1131 | } | |
1132 | #else /* not SKTPAIR */ | |
1133 | { | |
1134 | pipe (sv); | |
1135 | inchannel = sv[0]; | |
1136 | forkout = sv[1]; | |
1137 | pipe (sv); | |
1138 | outchannel = sv[1]; | |
1139 | forkin = sv[0]; | |
1140 | } | |
1141 | #endif /* not SKTPAIR */ | |
1142 | ||
1143 | #if 0 | |
1144 | /* Replaced by close_process_descs */ | |
1145 | set_exclusive_use (inchannel); | |
1146 | set_exclusive_use (outchannel); | |
1147 | #endif | |
1148 | ||
1149 | /* Stride people say it's a mystery why this is needed | |
1150 | as well as the O_NDELAY, but that it fails without this. */ | |
1151 | #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS)) | |
1152 | { | |
1153 | int one = 1; | |
1154 | ioctl (inchannel, FIONBIO, &one); | |
1155 | } | |
1156 | #endif | |
1157 | ||
1158 | #ifdef O_NONBLOCK | |
1159 | fcntl (inchannel, F_SETFL, O_NONBLOCK); | |
1160 | #else | |
1161 | #ifdef O_NDELAY | |
1162 | fcntl (inchannel, F_SETFL, O_NDELAY); | |
1163 | #endif | |
1164 | #endif | |
1165 | ||
1166 | /* Record this as an active process, with its channels. | |
1167 | As a result, child_setup will close Emacs's side of the pipes. */ | |
1168 | chan_process[inchannel] = process; | |
a9f2c884 RS |
1169 | XSET (XPROCESS (process)->infd, Lisp_Int, inchannel); |
1170 | XSET (XPROCESS (process)->outfd, Lisp_Int, outchannel); | |
d0d6b7c5 JB |
1171 | /* Record the tty descriptor used in the subprocess. */ |
1172 | if (forkin < 0) | |
1173 | XPROCESS (process)->subtty = Qnil; | |
1174 | else | |
1175 | XFASTINT (XPROCESS (process)->subtty) = forkin; | |
1176 | XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil); | |
1177 | XPROCESS (process)->status = Qrun; | |
1178 | ||
1179 | /* Delay interrupts until we have a chance to store | |
1180 | the new fork's pid in its process structure */ | |
1181 | #ifdef SIGCHLD | |
1182 | #ifdef BSD4_1 | |
1183 | sighold (SIGCHLD); | |
1184 | #else /* not BSD4_1 */ | |
1185 | #if defined (BSD) || defined (UNIPLUS) || defined (HPUX) | |
1186 | sigsetmask (sigmask (SIGCHLD)); | |
1187 | #else /* ordinary USG */ | |
1188 | #if 0 | |
1189 | sigchld_deferred = 0; | |
1190 | sigchld = signal (SIGCHLD, create_process_sigchld); | |
1191 | #endif | |
1192 | #endif /* ordinary USG */ | |
1193 | #endif /* not BSD4_1 */ | |
1194 | #endif /* SIGCHLD */ | |
1195 | ||
1196 | /* Until we store the proper pid, enable sigchld_handler | |
1197 | to recognize an unknown pid as standing for this process. | |
1198 | It is very important not to let this `marker' value stay | |
1199 | in the table after this function has returned; if it does | |
1200 | it might cause call-process to hang and subsequent asynchronous | |
1201 | processes to get their return values scrambled. */ | |
1202 | XSETINT (XPROCESS (process)->pid, -1); | |
1203 | ||
1204 | { | |
1205 | /* child_setup must clobber environ on systems with true vfork. | |
1206 | Protect it from permanent change. */ | |
1207 | char **save_environ = environ; | |
1208 | ||
1209 | pid = vfork (); | |
1210 | if (pid == 0) | |
1211 | { | |
1212 | int xforkin = forkin; | |
1213 | int xforkout = forkout; | |
1214 | ||
1215 | #if 0 /* This was probably a mistake--it duplicates code later on, | |
1216 | but fails to handle all the cases. */ | |
1217 | /* Make sure SIGCHLD is not blocked in the child. */ | |
1218 | sigsetmask (SIGEMPTYMASK); | |
1219 | #endif | |
1220 | ||
1221 | /* Make the pty be the controlling terminal of the process. */ | |
1222 | #ifdef HAVE_PTYS | |
1223 | /* First, disconnect its current controlling terminal. */ | |
1224 | #ifdef HAVE_SETSID | |
1225 | setsid (); | |
ce4c9c90 RS |
1226 | #ifdef TIOCSCTTY |
1227 | /* Make the pty's terminal the controlling terminal. */ | |
084fd64a RS |
1228 | if (pty_flag) |
1229 | /* We ignore the return value | |
1230 | because faith@cs.unc.edu says that is necessary on Linux. */ | |
1231 | ioctl (xforkin, TIOCSCTTY, 0); | |
ce4c9c90 | 1232 | #endif |
d0d6b7c5 | 1233 | #else /* not HAVE_SETSID */ |
b5aaed99 | 1234 | #if defined (USG) && !defined (IRIX) |
d0d6b7c5 JB |
1235 | /* It's very important to call setpgrp() here and no time |
1236 | afterwards. Otherwise, we lose our controlling tty which | |
1237 | is set when we open the pty. */ | |
1238 | setpgrp (); | |
1239 | #endif /* USG */ | |
1240 | #endif /* not HAVE_SETSID */ | |
1241 | #ifdef TIOCNOTTY | |
1242 | /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you | |
1243 | can do TIOCSPGRP only to the process's controlling tty. */ | |
1244 | if (pty_flag) | |
1245 | { | |
1246 | /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? | |
1247 | I can't test it since I don't have 4.3. */ | |
1248 | int j = open ("/dev/tty", O_RDWR, 0); | |
1249 | ioctl (j, TIOCNOTTY, 0); | |
1250 | close (j); | |
5a570e37 | 1251 | #ifndef USG |
d0d6b7c5 JB |
1252 | /* In order to get a controlling terminal on some versions |
1253 | of BSD, it is necessary to put the process in pgrp 0 | |
1254 | before it opens the terminal. */ | |
1255 | setpgrp (0, 0); | |
1256 | #endif | |
1257 | } | |
1258 | #endif /* TIOCNOTTY */ | |
1259 | ||
1260 | #if !defined (RTU) && !defined (UNIPLUS) | |
1261 | /*** There is a suggestion that this ought to be a | |
1262 | conditional on TIOCSPGRP. */ | |
1263 | /* Now close the pty (if we had it open) and reopen it. | |
1264 | This makes the pty the controlling terminal of the subprocess. */ | |
1265 | if (pty_flag) | |
1266 | { | |
1267 | /* I wonder if close (open (pty_name, ...)) would work? */ | |
1268 | if (xforkin >= 0) | |
1269 | close (xforkin); | |
1270 | xforkout = xforkin = open (pty_name, O_RDWR, 0); | |
1271 | ||
1272 | if (xforkin < 0) | |
1273 | abort (); | |
1274 | } | |
1275 | #endif /* not UNIPLUS and not RTU */ | |
1276 | #ifdef SETUP_SLAVE_PTY | |
1277 | SETUP_SLAVE_PTY; | |
1278 | #endif /* SETUP_SLAVE_PTY */ | |
1279 | #ifdef AIX | |
1280 | /* On AIX, we've disabled SIGHUP above once we start a child on a pty. | |
1281 | Now reenable it in the child, so it will die when we want it to. */ | |
1282 | if (pty_flag) | |
1283 | signal (SIGHUP, SIG_DFL); | |
1284 | #endif | |
1285 | #endif /* HAVE_PTYS */ | |
1286 | ||
1287 | #ifdef SIGCHLD | |
1288 | #ifdef BSD4_1 | |
1289 | sigrelse (SIGCHLD); | |
1290 | #else /* not BSD4_1 */ | |
1291 | #if defined (BSD) || defined (UNIPLUS) || defined (HPUX) | |
1292 | sigsetmask (SIGEMPTYMASK); | |
1293 | #else /* ordinary USG */ | |
63528b78 | 1294 | #if 0 |
d0d6b7c5 | 1295 | signal (SIGCHLD, sigchld); |
63528b78 | 1296 | #endif |
d0d6b7c5 JB |
1297 | #endif /* ordinary USG */ |
1298 | #endif /* not BSD4_1 */ | |
1299 | #endif /* SIGCHLD */ | |
1300 | ||
1301 | child_setup_tty (xforkout); | |
1302 | child_setup (xforkin, xforkout, xforkout, | |
e065a56e | 1303 | new_argv, 1, current_dir); |
d0d6b7c5 JB |
1304 | } |
1305 | environ = save_environ; | |
1306 | } | |
1307 | ||
1308 | if (pid < 0) | |
b0310da4 JB |
1309 | report_file_error ("Doing vfork", Qnil); |
1310 | ||
d0d6b7c5 JB |
1311 | XFASTINT (XPROCESS (process)->pid) = pid; |
1312 | ||
1313 | FD_SET (inchannel, &input_wait_mask); | |
1314 | ||
1315 | /* If the subfork execv fails, and it exits, | |
1316 | this close hangs. I don't know why. | |
1317 | So have an interrupt jar it loose. */ | |
1318 | stop_polling (); | |
1319 | signal (SIGALRM, create_process_1); | |
1320 | alarm (1); | |
1321 | #ifdef SYSV4_PTYS | |
1322 | /* OK to close only if it's not a pty. Otherwise we need to leave | |
1323 | it open for ioctl to get pgrp when signals are sent, or to send | |
1324 | the interrupt characters through if that's how we're signalling | |
1325 | subprocesses. Alternately if you are concerned about running out | |
1326 | of file descriptors, you could just save the tty name and open | |
1327 | just to do the ioctl. */ | |
1328 | if (NILP (XFASTINT (XPROCESS (process)->pty_flag))) | |
1329 | #endif | |
1330 | { | |
1331 | XPROCESS (process)->subtty = Qnil; | |
1332 | if (forkin >= 0) | |
1333 | close (forkin); | |
1334 | } | |
1335 | alarm (0); | |
1336 | start_polling (); | |
1337 | if (forkin != forkout && forkout >= 0) | |
1338 | close (forkout); | |
1339 | ||
1340 | #ifdef SIGCHLD | |
1341 | #ifdef BSD4_1 | |
1342 | sigrelse (SIGCHLD); | |
1343 | #else /* not BSD4_1 */ | |
1344 | #if defined (BSD) || defined (UNIPLUS) || defined (HPUX) | |
1345 | sigsetmask (SIGEMPTYMASK); | |
1346 | #else /* ordinary USG */ | |
1347 | #if 0 | |
1348 | signal (SIGCHLD, sigchld); | |
1349 | /* Now really handle any of these signals | |
1350 | that came in during this function. */ | |
1351 | if (sigchld_deferred) | |
1352 | kill (getpid (), SIGCHLD); | |
1353 | #endif | |
1354 | #endif /* ordinary USG */ | |
1355 | #endif /* not BSD4_1 */ | |
1356 | #endif /* SIGCHLD */ | |
1357 | } | |
1358 | #endif /* not VMS */ | |
1359 | ||
1360 | #ifdef HAVE_SOCKETS | |
1361 | ||
1362 | /* open a TCP network connection to a given HOST/SERVICE. Treated | |
1363 | exactly like a normal process when reading and writing. Only | |
1364 | differences are in status display and process deletion. A network | |
1365 | connection has no PID; you cannot signal it. All you can do is | |
1366 | deactivate and close it via delete-process */ | |
1367 | ||
1368 | DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, | |
1369 | 4, 4, 0, | |
1370 | "Open a TCP connection for a service to a host.\n\ | |
1371 | Returns a subprocess-object to represent the connection.\n\ | |
1372 | Input and output work as for subprocesses; `delete-process' closes it.\n\ | |
1373 | Args are NAME BUFFER HOST SERVICE.\n\ | |
1374 | NAME is name for process. It is modified if necessary to make it unique.\n\ | |
1375 | BUFFER is the buffer (or buffer-name) to associate with the process.\n\ | |
1376 | Process output goes at end of that buffer, unless you specify\n\ | |
1377 | an output stream or filter function to handle the output.\n\ | |
1378 | BUFFER may be also nil, meaning that this process is not associated\n\ | |
1379 | with any buffer\n\ | |
1380 | Third arg is name of the host to connect to, or its IP address.\n\ | |
1381 | Fourth arg SERVICE is name of the service desired, or an integer\n\ | |
1382 | specifying a port number to connect to.") | |
1383 | (name, buffer, host, service) | |
1384 | Lisp_Object name, buffer, host, service; | |
1385 | { | |
1386 | Lisp_Object proc; | |
1387 | register int i; | |
1388 | struct sockaddr_in address; | |
1389 | struct servent *svc_info; | |
1390 | struct hostent *host_info_ptr, host_info; | |
1391 | char *(addr_list[2]); | |
79967d5e | 1392 | IN_ADDR numeric_addr; |
d0d6b7c5 JB |
1393 | int s, outch, inch; |
1394 | char errstring[80]; | |
1395 | int port; | |
1396 | struct hostent host_info_fixed; | |
1397 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
1398 | ||
1399 | GCPRO4 (name, buffer, host, service); | |
1400 | CHECK_STRING (name, 0); | |
1401 | CHECK_STRING (host, 0); | |
1402 | if (XTYPE(service) == Lisp_Int) | |
1403 | port = htons ((unsigned short) XINT (service)); | |
1404 | else | |
1405 | { | |
1406 | CHECK_STRING (service, 0); | |
1407 | svc_info = getservbyname (XSTRING (service)->data, "tcp"); | |
1408 | if (svc_info == 0) | |
1409 | error ("Unknown service \"%s\"", XSTRING (service)->data); | |
1410 | port = svc_info->s_port; | |
1411 | } | |
1412 | ||
1d2c16fa | 1413 | #ifndef TERM |
d0d6b7c5 JB |
1414 | host_info_ptr = gethostbyname (XSTRING (host)->data); |
1415 | if (host_info_ptr == 0) | |
1416 | /* Attempt to interpret host as numeric inet address */ | |
1417 | { | |
393a9679 | 1418 | numeric_addr = inet_addr ((char *) XSTRING (host)->data); |
79967d5e | 1419 | if (NUMERIC_ADDR_ERROR) |
d0d6b7c5 JB |
1420 | error ("Unknown host \"%s\"", XSTRING (host)->data); |
1421 | ||
1422 | host_info_ptr = &host_info; | |
1423 | host_info.h_name = 0; | |
1424 | host_info.h_aliases = 0; | |
1425 | host_info.h_addrtype = AF_INET; | |
39a21be6 JB |
1426 | #ifdef h_addr |
1427 | /* Older machines have only one address slot called h_addr. | |
1428 | Newer machines have h_addr_list, but #define h_addr to | |
1429 | be its first element. */ | |
1430 | host_info.h_addr_list = &(addr_list[0]); | |
1431 | #endif | |
1432 | host_info.h_addr = (char*)(&numeric_addr); | |
d0d6b7c5 JB |
1433 | addr_list[1] = 0; |
1434 | host_info.h_length = strlen (addr_list[0]); | |
1435 | } | |
1436 | ||
1437 | bzero (&address, sizeof address); | |
1438 | bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr, | |
1439 | host_info_ptr->h_length); | |
1440 | address.sin_family = host_info_ptr->h_addrtype; | |
1441 | address.sin_port = port; | |
1442 | ||
1443 | s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0); | |
1444 | if (s < 0) | |
1445 | report_file_error ("error creating socket", Fcons (name, Qnil)); | |
1446 | ||
1447 | loop: | |
63826237 | 1448 | if (connect (s, (struct sockaddr *) &address, sizeof address) == -1) |
d0d6b7c5 JB |
1449 | { |
1450 | int xerrno = errno; | |
1451 | if (errno == EINTR) | |
1452 | goto loop; | |
1453 | close (s); | |
1454 | errno = xerrno; | |
1455 | report_file_error ("connection failed", | |
1456 | Fcons (host, Fcons (name, Qnil))); | |
1457 | } | |
1d2c16fa RS |
1458 | #else /* TERM */ |
1459 | s = connect_server (0); | |
1460 | if (s < 0) | |
1461 | report_file_error ("error creating socket", Fcons (name, Qnil)); | |
1462 | send_command (s, C_PORT, 0, "%s:%d", XSTRING (host)->data, ntohs (port)); | |
1463 | send_command (s, C_DUMB, 1, 0); | |
1464 | #endif /* TERM */ | |
d0d6b7c5 JB |
1465 | |
1466 | inch = s; | |
1467 | outch = dup (s); | |
1468 | if (outch < 0) | |
1469 | report_file_error ("error duplicating socket", Fcons (name, Qnil)); | |
1470 | ||
1471 | if (!NILP (buffer)) | |
1472 | buffer = Fget_buffer_create (buffer); | |
1473 | proc = make_process (name); | |
1474 | ||
1475 | chan_process[inch] = proc; | |
1476 | ||
1477 | #ifdef O_NONBLOCK | |
1478 | fcntl (inch, F_SETFL, O_NONBLOCK); | |
1479 | #else | |
1480 | #ifdef O_NDELAY | |
1481 | fcntl (inch, F_SETFL, O_NDELAY); | |
1482 | #endif | |
1483 | #endif | |
1484 | ||
1485 | XPROCESS (proc)->childp = host; | |
1486 | XPROCESS (proc)->command_channel_p = Qnil; | |
1487 | XPROCESS (proc)->buffer = buffer; | |
1488 | XPROCESS (proc)->sentinel = Qnil; | |
1489 | XPROCESS (proc)->filter = Qnil; | |
1490 | XPROCESS (proc)->command = Qnil; | |
1491 | XPROCESS (proc)->pid = Qnil; | |
a9f2c884 RS |
1492 | XSET (XPROCESS (proc)->infd, Lisp_Int, s); |
1493 | XSET (XPROCESS (proc)->outfd, Lisp_Int, outch); | |
d0d6b7c5 JB |
1494 | XPROCESS (proc)->status = Qrun; |
1495 | FD_SET (inch, &input_wait_mask); | |
1496 | ||
1497 | UNGCPRO; | |
1498 | return proc; | |
1499 | } | |
1500 | #endif /* HAVE_SOCKETS */ | |
1501 | ||
1502 | deactivate_process (proc) | |
1503 | Lisp_Object proc; | |
1504 | { | |
1505 | register int inchannel, outchannel; | |
1506 | register struct Lisp_Process *p = XPROCESS (proc); | |
1507 | ||
a9f2c884 RS |
1508 | inchannel = XINT (p->infd); |
1509 | outchannel = XINT (p->outfd); | |
d0d6b7c5 | 1510 | |
a9f2c884 | 1511 | if (inchannel >= 0) |
d0d6b7c5 JB |
1512 | { |
1513 | /* Beware SIGCHLD hereabouts. */ | |
1514 | flush_pending_output (inchannel); | |
1515 | #ifdef VMS | |
1516 | { | |
1517 | VMS_PROC_STUFF *get_vms_process_pointer (), *vs; | |
1518 | sys$dassgn (outchannel); | |
c6c6865d | 1519 | vs = get_vms_process_pointer (p->pid); |
d0d6b7c5 JB |
1520 | if (vs) |
1521 | give_back_vms_process_stuff (vs); | |
1522 | } | |
1523 | #else | |
1524 | close (inchannel); | |
a9f2c884 | 1525 | if (outchannel >= 0 && outchannel != inchannel) |
d0d6b7c5 JB |
1526 | close (outchannel); |
1527 | #endif | |
1528 | ||
a9f2c884 RS |
1529 | XSET (p->infd, Lisp_Int, -1); |
1530 | XSET (p->outfd, Lisp_Int, -1); | |
d0d6b7c5 JB |
1531 | chan_process[inchannel] = Qnil; |
1532 | FD_CLR (inchannel, &input_wait_mask); | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | /* Close all descriptors currently in use for communication | |
1537 | with subprocess. This is used in a newly-forked subprocess | |
1538 | to get rid of irrelevant descriptors. */ | |
1539 | ||
1540 | close_process_descs () | |
1541 | { | |
1542 | int i; | |
1543 | for (i = 0; i < MAXDESC; i++) | |
1544 | { | |
1545 | Lisp_Object process; | |
1546 | process = chan_process[i]; | |
1547 | if (!NILP (process)) | |
1548 | { | |
a9f2c884 RS |
1549 | int in = XINT (XPROCESS (process)->infd); |
1550 | int out = XINT (XPROCESS (process)->outfd); | |
1551 | if (in >= 0) | |
d0d6b7c5 | 1552 | close (in); |
a9f2c884 | 1553 | if (out >= 0 && in != out) |
d0d6b7c5 JB |
1554 | close (out); |
1555 | } | |
1556 | } | |
1557 | } | |
1558 | \f | |
1559 | DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output, | |
1560 | 0, 3, 0, | |
1561 | "Allow any pending output from subprocesses to be read by Emacs.\n\ | |
1562 | It is read into the process' buffers or given to their filter functions.\n\ | |
1563 | Non-nil arg PROCESS means do not return until some output has been received\n\ | |
1564 | from PROCESS.\n\ | |
1565 | Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\ | |
1566 | seconds and microseconds to wait; return after that much time whether\n\ | |
1567 | or not there is input.\n\ | |
1568 | Return non-nil iff we received any output before the timeout expired.") | |
1569 | (proc, timeout, timeout_msecs) | |
1570 | register Lisp_Object proc, timeout, timeout_msecs; | |
1571 | { | |
1572 | int seconds; | |
1573 | int useconds; | |
1574 | ||
1575 | if (! NILP (timeout_msecs)) | |
1576 | { | |
1577 | CHECK_NUMBER (timeout_msecs, 2); | |
1578 | useconds = XINT (timeout_msecs); | |
1579 | if (XTYPE (timeout) != Lisp_Int) | |
1580 | XSET (timeout, Lisp_Int, 0); | |
1581 | ||
1582 | { | |
1583 | int carry = useconds / 1000000; | |
1584 | ||
1585 | XSETINT (timeout, XINT (timeout) + carry); | |
1586 | useconds -= carry * 1000000; | |
1587 | ||
1588 | /* I think this clause is necessary because C doesn't | |
1589 | guarantee a particular rounding direction for negative | |
1590 | integers. */ | |
1591 | if (useconds < 0) | |
1592 | { | |
1593 | XSETINT (timeout, XINT (timeout) - 1); | |
1594 | useconds += 1000000; | |
1595 | } | |
1596 | } | |
1597 | } | |
de946e5a RS |
1598 | else |
1599 | useconds = 0; | |
d0d6b7c5 JB |
1600 | |
1601 | if (! NILP (timeout)) | |
1602 | { | |
1603 | CHECK_NUMBER (timeout, 1); | |
1604 | seconds = XINT (timeout); | |
1605 | if (seconds <= 0) | |
1606 | seconds = -1; | |
1607 | } | |
1608 | else | |
1609 | { | |
1610 | if (NILP (proc)) | |
1611 | seconds = -1; | |
1612 | else | |
1613 | seconds = 0; | |
1614 | } | |
1615 | ||
f76475ad JB |
1616 | if (NILP (proc)) |
1617 | XFASTINT (proc) = 0; | |
1618 | ||
d0d6b7c5 | 1619 | return |
f76475ad | 1620 | (wait_reading_process_input (seconds, useconds, proc, 0) |
d0d6b7c5 JB |
1621 | ? Qt : Qnil); |
1622 | } | |
1623 | ||
1624 | /* This variable is different from waiting_for_input in keyboard.c. | |
1625 | It is used to communicate to a lisp process-filter/sentinel (via the | |
1626 | function Fwaiting_for_user_input_p below) whether emacs was waiting | |
1627 | for user-input when that process-filter was called. | |
1628 | waiting_for_input cannot be used as that is by definition 0 when | |
1629 | lisp code is being evalled */ | |
1630 | static int waiting_for_user_input_p; | |
1631 | ||
1632 | /* Read and dispose of subprocess output while waiting for timeout to | |
1633 | elapse and/or keyboard input to be available. | |
1634 | ||
de6fd4b9 | 1635 | TIME_LIMIT is: |
d0d6b7c5 JB |
1636 | timeout in seconds, or |
1637 | zero for no limit, or | |
1638 | -1 means gobble data immediately available but don't wait for any. | |
1639 | ||
de6fd4b9 RS |
1640 | MICROSECS is: |
1641 | an additional duration to wait, measured in microseconds. | |
1642 | If this is nonzero and time_limit is 0, then the timeout | |
1643 | consists of MICROSECS only. | |
6e4f3667 | 1644 | |
de6fd4b9 | 1645 | READ_KBD is a lisp value: |
d0d6b7c5 JB |
1646 | 0 to ignore keyboard input, or |
1647 | 1 to return when input is available, or | |
84aa3ace | 1648 | -1 meaning caller will actually read the input, so don't throw to |
d0d6b7c5 | 1649 | the quit handler, or |
de6fd4b9 RS |
1650 | a cons cell, meaning wait wait until its car is non-nil |
1651 | (and gobble terminal input into the buffer if any arrives), or | |
f76475ad JB |
1652 | a process object, meaning wait until something arrives from that |
1653 | process. The return value is true iff we read some input from | |
1654 | that process. | |
d0d6b7c5 | 1655 | |
de6fd4b9 | 1656 | DO_DISPLAY != 0 means redisplay should be done to show subprocess |
d0d6b7c5 JB |
1657 | output that arrives. |
1658 | ||
de6fd4b9 | 1659 | If READ_KBD is a pointer to a struct Lisp_Process, then the |
d0d6b7c5 JB |
1660 | function returns true iff we received input from that process |
1661 | before the timeout elapsed. | |
eb8c3be9 | 1662 | Otherwise, return true iff we received input from any process. */ |
d0d6b7c5 JB |
1663 | |
1664 | wait_reading_process_input (time_limit, microsecs, read_kbd, do_display) | |
f76475ad JB |
1665 | int time_limit, microsecs; |
1666 | Lisp_Object read_kbd; | |
1667 | int do_display; | |
d0d6b7c5 JB |
1668 | { |
1669 | register int channel, nfds, m; | |
1670 | static SELECT_TYPE Available; | |
1671 | int xerrno; | |
1672 | Lisp_Object proc; | |
1673 | EMACS_TIME timeout, end_time, garbage; | |
1674 | SELECT_TYPE Atemp; | |
a9f2c884 | 1675 | int wait_channel = -1; |
d0d6b7c5 JB |
1676 | struct Lisp_Process *wait_proc = 0; |
1677 | int got_some_input = 0; | |
84aa3ace | 1678 | Lisp_Object *wait_for_cell = 0; |
d0d6b7c5 JB |
1679 | |
1680 | FD_ZERO (&Available); | |
1681 | ||
f76475ad JB |
1682 | /* If read_kbd is a process to watch, set wait_proc and wait_channel |
1683 | accordingly. */ | |
1684 | if (XTYPE (read_kbd) == Lisp_Process) | |
d0d6b7c5 | 1685 | { |
f76475ad | 1686 | wait_proc = XPROCESS (read_kbd); |
a9f2c884 | 1687 | wait_channel = XINT (wait_proc->infd); |
f76475ad | 1688 | XFASTINT (read_kbd) = 0; |
d0d6b7c5 JB |
1689 | } |
1690 | ||
84aa3ace RS |
1691 | /* If waiting for non-nil in a cell, record where. */ |
1692 | if (XTYPE (read_kbd) == Lisp_Cons) | |
1693 | { | |
1694 | wait_for_cell = &XCONS (read_kbd)->car; | |
1695 | XFASTINT (read_kbd) = 0; | |
1696 | } | |
1697 | ||
f76475ad | 1698 | waiting_for_user_input_p = XINT (read_kbd); |
d0d6b7c5 JB |
1699 | |
1700 | /* Since we may need to wait several times, | |
1701 | compute the absolute time to return at. */ | |
1702 | if (time_limit || microsecs) | |
1703 | { | |
1704 | EMACS_GET_TIME (end_time); | |
1705 | EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); | |
1706 | EMACS_ADD_TIME (end_time, end_time, timeout); | |
1707 | } | |
1708 | ||
d0d6b7c5 JB |
1709 | while (1) |
1710 | { | |
1711 | /* If calling from keyboard input, do not quit | |
1712 | since we want to return C-g as an input character. | |
1713 | Otherwise, do pending quit if requested. */ | |
f76475ad | 1714 | if (XINT (read_kbd) >= 0) |
d0d6b7c5 JB |
1715 | QUIT; |
1716 | ||
889255b4 RS |
1717 | /* Exit now if the cell we're waiting for became non-nil. */ |
1718 | if (wait_for_cell && ! NILP (*wait_for_cell)) | |
1719 | break; | |
1720 | ||
d0d6b7c5 JB |
1721 | /* Compute time from now till when time limit is up */ |
1722 | /* Exit if already run out */ | |
1723 | if (time_limit == -1) | |
1724 | { | |
1725 | /* -1 specified for timeout means | |
1726 | gobble output available now | |
1727 | but don't wait at all. */ | |
1728 | ||
1729 | EMACS_SET_SECS_USECS (timeout, 0, 0); | |
1730 | } | |
1731 | else if (time_limit || microsecs) | |
1732 | { | |
1733 | EMACS_GET_TIME (timeout); | |
1734 | EMACS_SUB_TIME (timeout, end_time, timeout); | |
1735 | if (EMACS_TIME_NEG_P (timeout)) | |
1736 | break; | |
1737 | } | |
1738 | else | |
1739 | { | |
1740 | EMACS_SET_SECS_USECS (timeout, 100000, 0); | |
1741 | } | |
1742 | ||
90ab1a81 JB |
1743 | /* Cause C-g and alarm signals to take immediate action, |
1744 | and cause input available signals to zero out timeout. | |
1745 | ||
1746 | It is important that we do this before checking for process | |
1747 | activity. If we get a SIGCHLD after the explicit checks for | |
1748 | process activity, timeout is the only way we will know. */ | |
1749 | if (XINT (read_kbd) < 0) | |
1750 | set_waiting_for_input (&timeout); | |
1751 | ||
6be429b1 JB |
1752 | /* If status of something has changed, and no input is |
1753 | available, notify the user of the change right away. After | |
1754 | this explicit check, we'll let the SIGCHLD handler zap | |
1755 | timeout to get our attention. */ | |
1756 | if (update_tick != process_tick && do_display) | |
1757 | { | |
1758 | Atemp = input_wait_mask; | |
1759 | EMACS_SET_SECS_USECS (timeout, 0, 0); | |
1760 | if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0) | |
90ab1a81 JB |
1761 | { |
1762 | /* It's okay for us to do this and then continue with | |
1763 | the loop, since timeout has already been zeroed out. */ | |
1764 | clear_waiting_for_input (); | |
1765 | status_notify (); | |
1766 | } | |
6be429b1 JB |
1767 | } |
1768 | ||
1769 | /* Don't wait for output from a non-running process. */ | |
1770 | if (wait_proc != 0 && !NILP (wait_proc->raw_status_low)) | |
1771 | update_status (wait_proc); | |
1772 | if (wait_proc != 0 | |
1773 | && ! EQ (wait_proc->status, Qrun)) | |
9aa2a7f4 JB |
1774 | { |
1775 | clear_waiting_for_input (); | |
1776 | break; | |
1777 | } | |
6be429b1 | 1778 | |
d0d6b7c5 JB |
1779 | /* Wait till there is something to do */ |
1780 | ||
1781 | Available = input_wait_mask; | |
6cbc22ed RS |
1782 | /* We used to have && wait_for_cell == 0 |
1783 | but that led to lossage handling selection_request events: | |
1784 | within one, we would start to handle another. */ | |
1785 | if (! XINT (read_kbd)) | |
dd2281ae | 1786 | FD_CLR (keyboard_descriptor, &Available); |
d0d6b7c5 | 1787 | |
ff11dfa1 | 1788 | /* If frame size has changed or the window is newly mapped, |
ffd56f97 JB |
1789 | redisplay now, before we start to wait. There is a race |
1790 | condition here; if a SIGIO arrives between now and the select | |
016899c0 JB |
1791 | and indicates that a frame is trashed, the select may block |
1792 | displaying a trashed screen. */ | |
ff11dfa1 | 1793 | if (frame_garbaged) |
ffd56f97 JB |
1794 | redisplay_preserve_echo_area (); |
1795 | ||
f76475ad | 1796 | if (XINT (read_kbd) && detect_input_pending ()) |
a9f2c884 RS |
1797 | { |
1798 | nfds = 0; | |
1799 | FD_ZERO (&Available); | |
1800 | } | |
d0d6b7c5 | 1801 | else |
d0d6b7c5 | 1802 | nfds = select (MAXDESC, &Available, 0, 0, &timeout); |
6720a7fb | 1803 | |
d0d6b7c5 JB |
1804 | xerrno = errno; |
1805 | ||
1806 | /* Make C-g and alarm signals set flags again */ | |
1807 | clear_waiting_for_input (); | |
1808 | ||
1809 | /* If we woke up due to SIGWINCH, actually change size now. */ | |
1810 | do_pending_window_change (); | |
1811 | ||
ffd56f97 | 1812 | if (time_limit && nfds == 0) /* timeout elapsed */ |
d0d6b7c5 JB |
1813 | break; |
1814 | if (nfds < 0) | |
1815 | { | |
1816 | if (xerrno == EINTR) | |
1817 | FD_ZERO (&Available); | |
b0310da4 JB |
1818 | #ifdef ultrix |
1819 | /* Ultrix select seems to return ENOMEM when it is | |
1820 | interrupted. Treat it just like EINTR. Bleah. Note | |
1821 | that we want to test for the "ultrix" CPP symbol, not | |
1822 | "__ultrix__"; the latter is only defined under GCC, but | |
1823 | not by DEC's bundled CC. -JimB */ | |
8058415c JB |
1824 | else if (xerrno == ENOMEM) |
1825 | FD_ZERO (&Available); | |
1826 | #endif | |
d0d6b7c5 JB |
1827 | #ifdef ALLIANT |
1828 | /* This happens for no known reason on ALLIANT. | |
1829 | I am guessing that this is the right response. -- RMS. */ | |
1830 | else if (xerrno == EFAULT) | |
1831 | FD_ZERO (&Available); | |
1832 | #endif | |
1833 | else if (xerrno == EBADF) | |
1834 | { | |
1835 | #ifdef AIX | |
1836 | /* AIX doesn't handle PTY closure the same way BSD does. On AIX, | |
1837 | the child's closure of the pts gives the parent a SIGHUP, and | |
1838 | the ptc file descriptor is automatically closed, | |
1839 | yielding EBADF here or at select() call above. | |
1840 | So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF | |
1841 | in m-ibmrt-aix.h), and here we just ignore the select error. | |
1842 | Cleanup occurs c/o status_notify after SIGCLD. */ | |
ffd56f97 | 1843 | FD_ZERO (&Available); /* Cannot depend on values returned */ |
d0d6b7c5 JB |
1844 | #else |
1845 | abort (); | |
1846 | #endif | |
1847 | } | |
1848 | else | |
1849 | error("select error: %s", sys_errlist[xerrno]); | |
1850 | } | |
26ec91de | 1851 | #if defined(sun) && !defined(USG5_4) |
dd2281ae RS |
1852 | else if (nfds > 0 && FD_ISSET (keyboard_descriptor, &Available) |
1853 | && interrupt_input) | |
e0109153 JB |
1854 | /* System sometimes fails to deliver SIGIO. |
1855 | ||
1856 | David J. Mackenzie says that Emacs doesn't compile under | |
1857 | Solaris if this code is enabled, thus the USG5_4 in the CPP | |
1858 | conditional. "I haven't noticed any ill effects so far. | |
1859 | If you find a Solaris expert somewhere, they might know | |
1860 | better." */ | |
d0d6b7c5 JB |
1861 | kill (getpid (), SIGIO); |
1862 | #endif | |
1863 | ||
1864 | /* Check for keyboard input */ | |
1865 | /* If there is any, return immediately | |
1866 | to give it higher priority than subprocesses */ | |
1867 | ||
6cbc22ed RS |
1868 | /* We used to do his if wait_for_cell, |
1869 | but that caused infinite recursion in selection request events. */ | |
1870 | if ((XINT (read_kbd)) | |
97f3b3d6 | 1871 | && detect_input_pending ()) |
6e103bac RS |
1872 | { |
1873 | swallow_events (); | |
1874 | if (detect_input_pending ()) | |
1875 | break; | |
1876 | } | |
d0d6b7c5 | 1877 | |
84aa3ace RS |
1878 | /* Exit now if the cell we're waiting for became non-nil. */ |
1879 | if (wait_for_cell && ! NILP (*wait_for_cell)) | |
1880 | break; | |
1881 | ||
4746118a | 1882 | #ifdef SIGIO |
d0d6b7c5 JB |
1883 | /* If we think we have keyboard input waiting, but didn't get SIGIO |
1884 | go read it. This can happen with X on BSD after logging out. | |
1885 | In that case, there really is no input and no SIGIO, | |
1886 | but select says there is input. */ | |
1887 | ||
dd2281ae RS |
1888 | if (XINT (read_kbd) && interrupt_input |
1889 | && (FD_ISSET (keyboard_descriptor, &Available))) | |
d0d6b7c5 | 1890 | kill (0, SIGIO); |
4746118a | 1891 | #endif |
d0d6b7c5 | 1892 | |
d0d6b7c5 JB |
1893 | if (! wait_proc) |
1894 | got_some_input |= nfds > 0; | |
1895 | ||
32676c08 JB |
1896 | /* If checking input just got us a size-change event from X, |
1897 | obey it now if we should. */ | |
97f3b3d6 | 1898 | if (XINT (read_kbd) || wait_for_cell) |
32676c08 JB |
1899 | do_pending_window_change (); |
1900 | ||
a9f2c884 RS |
1901 | /* Check for data from a process. */ |
1902 | /* Really FIRST_PROC_DESC should be 0 on Unix, | |
1903 | but this is safer in the short run. */ | |
1904 | for (channel = keyboard_descriptor == 0 ? FIRST_PROC_DESC : 0; | |
1905 | channel < MAXDESC; channel++) | |
d0d6b7c5 JB |
1906 | { |
1907 | if (FD_ISSET (channel, &Available)) | |
1908 | { | |
1909 | int nread; | |
1910 | ||
1911 | /* If waiting for this channel, arrange to return as | |
1912 | soon as no more input to be processed. No more | |
1913 | waiting. */ | |
1914 | if (wait_channel == channel) | |
1915 | { | |
a9f2c884 | 1916 | wait_channel = -1; |
d0d6b7c5 JB |
1917 | time_limit = -1; |
1918 | got_some_input = 1; | |
1919 | } | |
1920 | proc = chan_process[channel]; | |
1921 | if (NILP (proc)) | |
1922 | continue; | |
1923 | ||
d0d6b7c5 JB |
1924 | /* Read data from the process, starting with our |
1925 | buffered-ahead character if we have one. */ | |
1926 | ||
1927 | nread = read_process_output (proc, channel); | |
1928 | if (nread > 0) | |
1929 | { | |
1930 | /* Since read_process_output can run a filter, | |
1931 | which can call accept-process-output, | |
1932 | don't try to read from any other processes | |
1933 | before doing the select again. */ | |
1934 | FD_ZERO (&Available); | |
1935 | ||
1936 | if (do_display) | |
1937 | redisplay_preserve_echo_area (); | |
1938 | } | |
1939 | #ifdef EWOULDBLOCK | |
1940 | else if (nread == -1 && errno == EWOULDBLOCK) | |
1941 | ; | |
1942 | #else | |
1943 | #ifdef O_NONBLOCK | |
1944 | else if (nread == -1 && errno == EAGAIN) | |
1945 | ; | |
1946 | #else | |
1947 | #ifdef O_NDELAY | |
1948 | else if (nread == -1 && errno == EAGAIN) | |
1949 | ; | |
1950 | /* Note that we cannot distinguish between no input | |
1951 | available now and a closed pipe. | |
1952 | With luck, a closed pipe will be accompanied by | |
1953 | subprocess termination and SIGCHLD. */ | |
1954 | else if (nread == 0 && !NETCONN_P (proc)) | |
1955 | ; | |
ffd56f97 JB |
1956 | #endif /* O_NDELAY */ |
1957 | #endif /* O_NONBLOCK */ | |
1958 | #endif /* EWOULDBLOCK */ | |
d0d6b7c5 JB |
1959 | #ifdef HAVE_PTYS |
1960 | /* On some OSs with ptys, when the process on one end of | |
1961 | a pty exits, the other end gets an error reading with | |
1962 | errno = EIO instead of getting an EOF (0 bytes read). | |
1963 | Therefore, if we get an error reading and errno = | |
1964 | EIO, just continue, because the child process has | |
1965 | exited and should clean itself up soon (e.g. when we | |
1966 | get a SIGCHLD). */ | |
1967 | else if (nread == -1 && errno == EIO) | |
1968 | ; | |
ffd56f97 JB |
1969 | #endif /* HAVE_PTYS */ |
1970 | /* If we can detect process termination, don't consider the process | |
1971 | gone just because its pipe is closed. */ | |
d0d6b7c5 JB |
1972 | #ifdef SIGCHLD |
1973 | else if (nread == 0 && !NETCONN_P (proc)) | |
1974 | ; | |
1975 | #endif | |
1976 | else | |
1977 | { | |
1978 | /* Preserve status of processes already terminated. */ | |
1979 | XSETINT (XPROCESS (proc)->tick, ++process_tick); | |
1980 | deactivate_process (proc); | |
1981 | if (!NILP (XPROCESS (proc)->raw_status_low)) | |
1982 | update_status (XPROCESS (proc)); | |
1983 | if (EQ (XPROCESS (proc)->status, Qrun)) | |
1984 | XPROCESS (proc)->status | |
1985 | = Fcons (Qexit, Fcons (make_number (256), Qnil)); | |
1986 | } | |
1987 | } | |
ffd56f97 JB |
1988 | } /* end for each file descriptor */ |
1989 | } /* end while exit conditions not met */ | |
d0d6b7c5 | 1990 | |
ffd56f97 JB |
1991 | /* If calling from keyboard input, do not quit |
1992 | since we want to return C-g as an input character. | |
1993 | Otherwise, do pending quit if requested. */ | |
f76475ad | 1994 | if (XINT (read_kbd) >= 0) |
ffd56f97 JB |
1995 | { |
1996 | /* Prevent input_pending from remaining set if we quit. */ | |
1997 | clear_input_pending (); | |
1998 | QUIT; | |
1999 | } | |
d0d6b7c5 JB |
2000 | |
2001 | return got_some_input; | |
2002 | } | |
2003 | \f | |
2004 | /* Read pending output from the process channel, | |
2005 | starting with our buffered-ahead character if we have one. | |
2006 | Yield number of characters read. | |
2007 | ||
2008 | This function reads at most 1024 characters. | |
2009 | If you want to read all available subprocess output, | |
2010 | you must call it repeatedly until it returns zero. */ | |
2011 | ||
2012 | read_process_output (proc, channel) | |
2013 | Lisp_Object proc; | |
2014 | register int channel; | |
2015 | { | |
2016 | register int nchars; | |
2017 | #ifdef VMS | |
2018 | char *chars; | |
2019 | #else | |
2020 | char chars[1024]; | |
2021 | #endif | |
2022 | register Lisp_Object outstream; | |
2023 | register struct buffer *old = current_buffer; | |
2024 | register struct Lisp_Process *p = XPROCESS (proc); | |
2025 | register int opoint; | |
2026 | ||
2027 | #ifdef VMS | |
2028 | VMS_PROC_STUFF *vs, *get_vms_process_pointer(); | |
2029 | ||
2030 | vs = get_vms_process_pointer (p->pid); | |
2031 | if (vs) | |
2032 | { | |
2033 | if (!vs->iosb[0]) | |
2034 | return(0); /* Really weird if it does this */ | |
2035 | if (!(vs->iosb[0] & 1)) | |
2036 | return -1; /* I/O error */ | |
2037 | } | |
2038 | else | |
2039 | error ("Could not get VMS process pointer"); | |
2040 | chars = vs->inputBuffer; | |
2041 | nchars = clean_vms_buffer (chars, vs->iosb[1]); | |
2042 | if (nchars <= 0) | |
2043 | { | |
2044 | start_vms_process_read (vs); /* Crank up the next read on the process */ | |
2045 | return 1; /* Nothing worth printing, say we got 1 */ | |
2046 | } | |
2047 | #else /* not VMS */ | |
2048 | ||
2049 | if (proc_buffered_char[channel] < 0) | |
2050 | nchars = read (channel, chars, sizeof chars); | |
2051 | else | |
2052 | { | |
2053 | chars[0] = proc_buffered_char[channel]; | |
2054 | proc_buffered_char[channel] = -1; | |
2055 | nchars = read (channel, chars + 1, sizeof chars - 1); | |
2056 | if (nchars < 0) | |
2057 | nchars = 1; | |
2058 | else | |
2059 | nchars = nchars + 1; | |
2060 | } | |
2061 | #endif /* not VMS */ | |
2062 | ||
2063 | if (nchars <= 0) return nchars; | |
2064 | ||
2065 | outstream = p->filter; | |
2066 | if (!NILP (outstream)) | |
2067 | { | |
2068 | /* We inhibit quit here instead of just catching it so that | |
2069 | hitting ^G when a filter happens to be running won't screw | |
2070 | it up. */ | |
2071 | int count = specpdl_ptr - specpdl; | |
30c78175 RS |
2072 | Lisp_Object odeactivate; |
2073 | ||
2074 | odeactivate = Vdeactivate_mark; | |
2075 | ||
d0d6b7c5 JB |
2076 | specbind (Qinhibit_quit, Qt); |
2077 | call2 (outstream, proc, make_string (chars, nchars)); | |
2078 | ||
592ce97f | 2079 | /* Handling the process output should not deactivate the mark. */ |
30c78175 RS |
2080 | Vdeactivate_mark = odeactivate; |
2081 | ||
d0d6b7c5 JB |
2082 | #ifdef VMS |
2083 | start_vms_process_read (vs); | |
2084 | #endif | |
2085 | unbind_to (count); | |
2086 | return nchars; | |
2087 | } | |
2088 | ||
2089 | /* If no filter, write into buffer if it isn't dead. */ | |
2090 | if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name)) | |
2091 | { | |
b0310da4 JB |
2092 | Lisp_Object old_read_only; |
2093 | Lisp_Object old_begv, old_zv; | |
30c78175 RS |
2094 | Lisp_Object odeactivate; |
2095 | ||
2096 | odeactivate = Vdeactivate_mark; | |
d0d6b7c5 JB |
2097 | |
2098 | Fset_buffer (p->buffer); | |
2099 | opoint = point; | |
b0310da4 JB |
2100 | old_read_only = current_buffer->read_only; |
2101 | XFASTINT (old_begv) = BEGV; | |
2102 | XFASTINT (old_zv) = ZV; | |
2103 | ||
2104 | current_buffer->read_only = Qnil; | |
d0d6b7c5 JB |
2105 | |
2106 | /* Insert new output into buffer | |
2107 | at the current end-of-output marker, | |
2108 | thus preserving logical ordering of input and output. */ | |
2109 | if (XMARKER (p->mark)->buffer) | |
2110 | SET_PT (marker_position (p->mark)); | |
2111 | else | |
2112 | SET_PT (ZV); | |
b0310da4 JB |
2113 | |
2114 | /* If the output marker is outside of the visible region, save | |
2115 | the restriction and widen. */ | |
2116 | if (! (BEGV <= point && point <= ZV)) | |
2117 | Fwiden (); | |
2118 | ||
2119 | /* Make sure opoint floats ahead of any new text, just as point | |
2120 | would. */ | |
d0d6b7c5 JB |
2121 | if (point <= opoint) |
2122 | opoint += nchars; | |
2123 | ||
b0310da4 JB |
2124 | /* Insert after old_begv, but before old_zv. */ |
2125 | if (point < XFASTINT (old_begv)) | |
2126 | XFASTINT (old_begv) += nchars; | |
2127 | if (point <= XFASTINT (old_zv)) | |
2128 | XFASTINT (old_zv) += nchars; | |
2129 | ||
d0d6b7c5 JB |
2130 | /* Insert before markers in case we are inserting where |
2131 | the buffer's mark is, and the user's next command is Meta-y. */ | |
2132 | insert_before_markers (chars, nchars); | |
d0d6b7c5 | 2133 | Fset_marker (p->mark, make_number (point), p->buffer); |
b0310da4 | 2134 | |
d0d6b7c5 JB |
2135 | update_mode_lines++; |
2136 | ||
b0310da4 JB |
2137 | /* If the restriction isn't what it should be, set it. */ |
2138 | if (XFASTINT (old_begv) != BEGV || XFASTINT (old_zv) != ZV) | |
2139 | Fnarrow_to_region (old_begv, old_zv); | |
2140 | ||
592ce97f | 2141 | /* Handling the process output should not deactivate the mark. */ |
30c78175 RS |
2142 | Vdeactivate_mark = odeactivate; |
2143 | ||
b0310da4 | 2144 | current_buffer->read_only = old_read_only; |
d0d6b7c5 JB |
2145 | SET_PT (opoint); |
2146 | set_buffer_internal (old); | |
2147 | } | |
2148 | #ifdef VMS | |
2149 | start_vms_process_read (vs); | |
2150 | #endif | |
2151 | return nchars; | |
2152 | } | |
2153 | ||
2154 | DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p, | |
2155 | 0, 0, 0, | |
2156 | "Returns non-NIL if emacs is waiting for input from the user.\n\ | |
2157 | This is intended for use by asynchronous process output filters and sentinels.") | |
2158 | () | |
2159 | { | |
2160 | return ((waiting_for_user_input_p) ? Qt : Qnil); | |
2161 | } | |
2162 | \f | |
2163 | /* Sending data to subprocess */ | |
2164 | ||
2165 | jmp_buf send_process_frame; | |
2166 | ||
2167 | SIGTYPE | |
2168 | send_process_trap () | |
2169 | { | |
2170 | #ifdef BSD4_1 | |
2171 | sigrelse (SIGPIPE); | |
2172 | sigrelse (SIGALRM); | |
2173 | #endif /* BSD4_1 */ | |
2174 | longjmp (send_process_frame, 1); | |
2175 | } | |
2176 | ||
2177 | send_process (proc, buf, len) | |
2178 | Lisp_Object proc; | |
2179 | char *buf; | |
2180 | int len; | |
2181 | { | |
2182 | /* Don't use register vars; longjmp can lose them. */ | |
2183 | int rv; | |
2184 | unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data; | |
2185 | ||
2186 | ||
2187 | #ifdef VMS | |
2188 | struct Lisp_Process *p = XPROCESS (proc); | |
2189 | VMS_PROC_STUFF *vs, *get_vms_process_pointer(); | |
2190 | #endif /* VMS */ | |
2191 | ||
2192 | if (! NILP (XPROCESS (proc)->raw_status_low)) | |
2193 | update_status (XPROCESS (proc)); | |
2194 | if (! EQ (XPROCESS (proc)->status, Qrun)) | |
2195 | error ("Process %s not running", procname); | |
2196 | ||
2197 | #ifdef VMS | |
2198 | vs = get_vms_process_pointer (p->pid); | |
2199 | if (vs == 0) | |
2200 | error ("Could not find this process: %x", p->pid); | |
2201 | else if (write_to_vms_process (vs, buf, len)) | |
2202 | ; | |
2203 | #else | |
2204 | if (!setjmp (send_process_frame)) | |
2205 | while (len > 0) | |
2206 | { | |
2207 | int this = len; | |
4746118a JB |
2208 | SIGTYPE (*old_sigpipe)(); |
2209 | ||
d0d6b7c5 JB |
2210 | /* Don't send more than 500 bytes at a time. */ |
2211 | if (this > 500) | |
2212 | this = 500; | |
508b171c | 2213 | old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap); |
a9f2c884 | 2214 | rv = write (XINT (XPROCESS (proc)->outfd), buf, this); |
4746118a | 2215 | signal (SIGPIPE, old_sigpipe); |
d0d6b7c5 JB |
2216 | if (rv < 0) |
2217 | { | |
2218 | if (0 | |
2219 | #ifdef EWOULDBLOCK | |
2220 | || errno == EWOULDBLOCK | |
2221 | #endif | |
2222 | #ifdef EAGAIN | |
2223 | || errno == EAGAIN | |
2224 | #endif | |
2225 | ) | |
2226 | { | |
2227 | /* It would be nice to accept process output here, | |
2228 | but that is difficult. For example, it could | |
2229 | garbage what we are sending if that is from a buffer. */ | |
2230 | immediate_quit = 1; | |
2231 | QUIT; | |
2232 | sleep (1); | |
2233 | immediate_quit = 0; | |
2234 | continue; | |
2235 | } | |
2236 | report_file_error ("writing to process", Fcons (proc, Qnil)); | |
2237 | } | |
2238 | buf += rv; | |
2239 | len -= rv; | |
2240 | /* Allow input from processes between bursts of sending. | |
2241 | Otherwise things may get stopped up. */ | |
2242 | if (len > 0) | |
f76475ad JB |
2243 | { |
2244 | Lisp_Object zero; | |
2245 | ||
2246 | XFASTINT (zero) = 0; | |
2247 | wait_reading_process_input (-1, 0, zero, 0); | |
2248 | } | |
d0d6b7c5 JB |
2249 | } |
2250 | #endif | |
2251 | else | |
2252 | { | |
2253 | XPROCESS (proc)->raw_status_low = Qnil; | |
2254 | XPROCESS (proc)->raw_status_high = Qnil; | |
2255 | XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil)); | |
2256 | XSETINT (XPROCESS (proc)->tick, ++process_tick); | |
2257 | deactivate_process (proc); | |
2258 | #ifdef VMS | |
2259 | error ("Error writing to process %s; closed it", procname); | |
2260 | #else | |
2261 | error ("SIGPIPE raised on process %s; closed it", procname); | |
2262 | #endif | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region, | |
2267 | 3, 3, 0, | |
2268 | "Send current contents of region as input to PROCESS.\n\ | |
ebb9e16f JB |
2269 | PROCESS may be a process, a buffer, the name of a process or buffer, or\n\ |
2270 | nil, indicating the current buffer's process.\n\ | |
d0d6b7c5 JB |
2271 | Called from program, takes three arguments, PROCESS, START and END.\n\ |
2272 | If the region is more than 500 characters long,\n\ | |
2273 | it is sent in several bunches. This may happen even for shorter regions.\n\ | |
2274 | Output from processes can arrive in between bunches.") | |
2275 | (process, start, end) | |
2276 | Lisp_Object process, start, end; | |
2277 | { | |
2278 | Lisp_Object proc; | |
2279 | int start1; | |
2280 | ||
2281 | proc = get_process (process); | |
2282 | validate_region (&start, &end); | |
2283 | ||
2284 | if (XINT (start) < GPT && XINT (end) > GPT) | |
2285 | move_gap (start); | |
2286 | ||
2287 | start1 = XINT (start); | |
2288 | send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start)); | |
2289 | ||
2290 | return Qnil; | |
2291 | } | |
2292 | ||
2293 | DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string, | |
2294 | 2, 2, 0, | |
2295 | "Send PROCESS the contents of STRING as input.\n\ | |
ebb9e16f JB |
2296 | PROCESS may be a process, a buffer, the name of a process or buffer, or\n\ |
2297 | nil, indicating the current buffer's process.\n\ | |
d0d6b7c5 JB |
2298 | If STRING is more than 500 characters long,\n\ |
2299 | it is sent in several bunches. This may happen even for shorter strings.\n\ | |
2300 | Output from processes can arrive in between bunches.") | |
2301 | (process, string) | |
2302 | Lisp_Object process, string; | |
2303 | { | |
2304 | Lisp_Object proc; | |
2305 | CHECK_STRING (string, 1); | |
2306 | proc = get_process (process); | |
2307 | send_process (proc, XSTRING (string)->data, XSTRING (string)->size); | |
2308 | return Qnil; | |
2309 | } | |
2310 | \f | |
2311 | /* send a signal number SIGNO to PROCESS. | |
2312 | CURRENT_GROUP means send to the process group that currently owns | |
2313 | the terminal being used to communicate with PROCESS. | |
2314 | This is used for various commands in shell mode. | |
2315 | If NOMSG is zero, insert signal-announcements into process's buffers | |
b0310da4 JB |
2316 | right away. |
2317 | ||
2318 | If we can, we try to signal PROCESS by sending control characters | |
2319 | down the pipe. This allows us to signal inferiors who have changed | |
2320 | their uid, for which killpg would return an EPERM error. */ | |
d0d6b7c5 | 2321 | |
f9738840 | 2322 | static void |
d0d6b7c5 JB |
2323 | process_send_signal (process, signo, current_group, nomsg) |
2324 | Lisp_Object process; | |
2325 | int signo; | |
2326 | Lisp_Object current_group; | |
2327 | int nomsg; | |
2328 | { | |
2329 | Lisp_Object proc; | |
2330 | register struct Lisp_Process *p; | |
2331 | int gid; | |
2332 | int no_pgrp = 0; | |
2333 | ||
2334 | proc = get_process (process); | |
2335 | p = XPROCESS (proc); | |
2336 | ||
2337 | if (!EQ (p->childp, Qt)) | |
2338 | error ("Process %s is not a subprocess", | |
2339 | XSTRING (p->name)->data); | |
a9f2c884 | 2340 | if (XINT (p->infd) < 0) |
d0d6b7c5 JB |
2341 | error ("Process %s is not active", |
2342 | XSTRING (p->name)->data); | |
2343 | ||
2344 | if (NILP (p->pty_flag)) | |
2345 | current_group = Qnil; | |
2346 | ||
d0d6b7c5 JB |
2347 | /* If we are using pgrps, get a pgrp number and make it negative. */ |
2348 | if (!NILP (current_group)) | |
2349 | { | |
b0310da4 | 2350 | #ifdef SIGNALS_VIA_CHARACTERS |
d0d6b7c5 JB |
2351 | /* If possible, send signals to the entire pgrp |
2352 | by sending an input character to it. */ | |
b0310da4 | 2353 | |
6be429b1 JB |
2354 | /* TERMIOS is the latest and bestest, and seems most likely to |
2355 | work. If the system has it, use it. */ | |
2356 | #ifdef HAVE_TERMIOS | |
2357 | struct termios t; | |
2358 | ||
2359 | switch (signo) | |
2360 | { | |
2361 | case SIGINT: | |
a9f2c884 | 2362 | tcgetattr (XINT (p->infd), &t); |
6be429b1 | 2363 | send_process (proc, &t.c_cc[VINTR], 1); |
a87b802f | 2364 | return; |
6be429b1 JB |
2365 | |
2366 | case SIGQUIT: | |
a9f2c884 | 2367 | tcgetattr (XINT (p->infd), &t); |
6be429b1 | 2368 | send_process (proc, &t.c_cc[VQUIT], 1); |
a87b802f | 2369 | return; |
6be429b1 JB |
2370 | |
2371 | case SIGTSTP: | |
a9f2c884 | 2372 | tcgetattr (XINT (p->infd), &t); |
6be429b1 JB |
2373 | #ifdef VSWTCH |
2374 | send_process (proc, &t.c_cc[VSWTCH], 1); | |
2375 | #else | |
2376 | send_process (proc, &t.c_cc[VSUSP], 1); | |
2377 | #endif | |
a87b802f | 2378 | return; |
6be429b1 JB |
2379 | } |
2380 | ||
2381 | #else /* ! HAVE_TERMIOS */ | |
2382 | ||
b0310da4 JB |
2383 | /* On Berkeley descendants, the following IOCTL's retrieve the |
2384 | current control characters. */ | |
d0d6b7c5 | 2385 | #if defined (TIOCGLTC) && defined (TIOCGETC) |
b0310da4 | 2386 | |
d0d6b7c5 JB |
2387 | struct tchars c; |
2388 | struct ltchars lc; | |
2389 | ||
2390 | switch (signo) | |
2391 | { | |
2392 | case SIGINT: | |
a9f2c884 | 2393 | ioctl (XINT (p->infd), TIOCGETC, &c); |
d0d6b7c5 | 2394 | send_process (proc, &c.t_intrc, 1); |
f9738840 | 2395 | return; |
d0d6b7c5 | 2396 | case SIGQUIT: |
a9f2c884 | 2397 | ioctl (XINT (p->infd), TIOCGETC, &c); |
d0d6b7c5 | 2398 | send_process (proc, &c.t_quitc, 1); |
f9738840 | 2399 | return; |
0ad77c54 | 2400 | #ifdef SIGTSTP |
d0d6b7c5 | 2401 | case SIGTSTP: |
a9f2c884 | 2402 | ioctl (XINT (p->infd), TIOCGLTC, &lc); |
d0d6b7c5 | 2403 | send_process (proc, &lc.t_suspc, 1); |
f9738840 | 2404 | return; |
b0310da4 | 2405 | #endif /* ! defined (SIGTSTP) */ |
d0d6b7c5 | 2406 | } |
b0310da4 JB |
2407 | |
2408 | #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */ | |
2409 | ||
2410 | /* On SYSV descendants, the TCGETA ioctl retrieves the current control | |
2411 | characters. */ | |
2412 | #ifdef TCGETA | |
d0d6b7c5 JB |
2413 | struct termio t; |
2414 | switch (signo) | |
2415 | { | |
2416 | case SIGINT: | |
a9f2c884 | 2417 | ioctl (XINT (p->infd), TCGETA, &t); |
d0d6b7c5 | 2418 | send_process (proc, &t.c_cc[VINTR], 1); |
f9738840 | 2419 | return; |
d0d6b7c5 | 2420 | case SIGQUIT: |
a9f2c884 | 2421 | ioctl (XINT (p->infd), TCGETA, &t); |
d0d6b7c5 | 2422 | send_process (proc, &t.c_cc[VQUIT], 1); |
f9738840 | 2423 | return; |
7d79e3b4 | 2424 | #ifdef SIGTSTP |
d0d6b7c5 | 2425 | case SIGTSTP: |
a9f2c884 | 2426 | ioctl (XINT (p->infd), TCGETA, &t); |
d0d6b7c5 | 2427 | send_process (proc, &t.c_cc[VSWTCH], 1); |
f9738840 | 2428 | return; |
b0310da4 | 2429 | #endif /* ! defined (SIGTSTP) */ |
d0d6b7c5 | 2430 | } |
b0310da4 JB |
2431 | #else /* ! defined (TCGETA) */ |
2432 | Your configuration files are messed up. | |
2433 | /* If your system configuration files define SIGNALS_VIA_CHARACTERS, | |
2434 | you'd better be using one of the alternatives above! */ | |
2435 | #endif /* ! defined (TCGETA) */ | |
2436 | #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */ | |
6be429b1 | 2437 | #endif /* ! defined HAVE_TERMIOS */ |
b0310da4 | 2438 | #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */ |
d0d6b7c5 | 2439 | |
301c3fe4 | 2440 | #ifdef TIOCGPGRP |
d0d6b7c5 JB |
2441 | /* Get the pgrp using the tty itself, if we have that. |
2442 | Otherwise, use the pty to get the pgrp. | |
2443 | On pfa systems, saka@pfu.fujitsu.co.JP writes: | |
b0310da4 JB |
2444 | "TIOCGPGRP symbol defined in sys/ioctl.h at E50. |
2445 | But, TIOCGPGRP does not work on E50 ;-P works fine on E60" | |
d0d6b7c5 JB |
2446 | His patch indicates that if TIOCGPGRP returns an error, then |
2447 | we should just assume that p->pid is also the process group id. */ | |
2448 | { | |
2449 | int err; | |
2450 | ||
2451 | if (!NILP (p->subtty)) | |
2452 | err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid); | |
2453 | else | |
a9f2c884 | 2454 | err = ioctl (XINT (p->infd), TIOCGPGRP, &gid); |
d0d6b7c5 JB |
2455 | |
2456 | #ifdef pfa | |
2457 | if (err == -1) | |
2458 | gid = - XFASTINT (p->pid); | |
301c3fe4 | 2459 | #endif /* ! defined (pfa) */ |
d0d6b7c5 JB |
2460 | } |
2461 | if (gid == -1) | |
2462 | no_pgrp = 1; | |
2463 | else | |
2464 | gid = - gid; | |
b0310da4 | 2465 | #else /* ! defined (TIOCGPGRP ) */ |
301c3fe4 JB |
2466 | /* Can't select pgrps on this system, so we know that |
2467 | the child itself heads the pgrp. */ | |
2468 | gid = - XFASTINT (p->pid); | |
2469 | #endif /* ! defined (TIOCGPGRP ) */ | |
d0d6b7c5 JB |
2470 | } |
2471 | else | |
2472 | gid = - XFASTINT (p->pid); | |
d0d6b7c5 JB |
2473 | |
2474 | switch (signo) | |
2475 | { | |
2476 | #ifdef SIGCONT | |
2477 | case SIGCONT: | |
2478 | p->raw_status_low = Qnil; | |
2479 | p->raw_status_high = Qnil; | |
2480 | p->status = Qrun; | |
2481 | XSETINT (p->tick, ++process_tick); | |
2482 | if (!nomsg) | |
2483 | status_notify (); | |
2484 | break; | |
301c3fe4 | 2485 | #endif /* ! defined (SIGCONT) */ |
d0d6b7c5 JB |
2486 | case SIGINT: |
2487 | #ifdef VMS | |
2488 | send_process (proc, "\003", 1); /* ^C */ | |
2489 | goto whoosh; | |
2490 | #endif | |
2491 | case SIGQUIT: | |
2492 | #ifdef VMS | |
2493 | send_process (proc, "\031", 1); /* ^Y */ | |
2494 | goto whoosh; | |
2495 | #endif | |
2496 | case SIGKILL: | |
2497 | #ifdef VMS | |
2498 | sys$forcex (&(XFASTINT (p->pid)), 0, 1); | |
2499 | whoosh: | |
2500 | #endif | |
a9f2c884 | 2501 | flush_pending_output (XINT (p->infd)); |
d0d6b7c5 JB |
2502 | break; |
2503 | } | |
2504 | ||
2505 | /* If we don't have process groups, send the signal to the immediate | |
2506 | subprocess. That isn't really right, but it's better than any | |
2507 | obvious alternative. */ | |
2508 | if (no_pgrp) | |
2509 | { | |
2510 | kill (XFASTINT (p->pid), signo); | |
2511 | return; | |
2512 | } | |
2513 | ||
2514 | /* gid may be a pid, or minus a pgrp's number */ | |
2515 | #ifdef TIOCSIGSEND | |
2516 | if (!NILP (current_group)) | |
a9f2c884 | 2517 | ioctl (XINT (p->infd), TIOCSIGSEND, signo); |
d0d6b7c5 JB |
2518 | else |
2519 | { | |
2520 | gid = - XFASTINT (p->pid); | |
2521 | kill (gid, signo); | |
2522 | } | |
301c3fe4 | 2523 | #else /* ! defined (TIOCSIGSEND) */ |
d0d6b7c5 | 2524 | EMACS_KILLPG (-gid, signo); |
301c3fe4 | 2525 | #endif /* ! defined (TIOCSIGSEND) */ |
d0d6b7c5 JB |
2526 | } |
2527 | ||
2528 | DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0, | |
2529 | "Interrupt process PROCESS. May be process or name of one.\n\ | |
ebb9e16f | 2530 | PROCESS may be a process, a buffer, or the name of a process or buffer.\n\ |
d0d6b7c5 JB |
2531 | Nil or no arg means current buffer's process.\n\ |
2532 | Second arg CURRENT-GROUP non-nil means send signal to\n\ | |
2533 | the current process-group of the process's controlling terminal\n\ | |
2534 | rather than to the process's own process group.\n\ | |
2535 | If the process is a shell, this means interrupt current subjob\n\ | |
2536 | rather than the shell.") | |
2537 | (process, current_group) | |
2538 | Lisp_Object process, current_group; | |
2539 | { | |
2540 | process_send_signal (process, SIGINT, current_group, 0); | |
2541 | return process; | |
2542 | } | |
2543 | ||
2544 | DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0, | |
2545 | "Kill process PROCESS. May be process or name of one.\n\ | |
2546 | See function `interrupt-process' for more details on usage.") | |
2547 | (process, current_group) | |
2548 | Lisp_Object process, current_group; | |
2549 | { | |
2550 | process_send_signal (process, SIGKILL, current_group, 0); | |
2551 | return process; | |
2552 | } | |
2553 | ||
2554 | DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0, | |
2555 | "Send QUIT signal to process PROCESS. May be process or name of one.\n\ | |
2556 | See function `interrupt-process' for more details on usage.") | |
2557 | (process, current_group) | |
2558 | Lisp_Object process, current_group; | |
2559 | { | |
2560 | process_send_signal (process, SIGQUIT, current_group, 0); | |
2561 | return process; | |
2562 | } | |
2563 | ||
2564 | DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0, | |
2565 | "Stop process PROCESS. May be process or name of one.\n\ | |
2566 | See function `interrupt-process' for more details on usage.") | |
2567 | (process, current_group) | |
2568 | Lisp_Object process, current_group; | |
2569 | { | |
2570 | #ifndef SIGTSTP | |
2571 | error ("no SIGTSTP support"); | |
2572 | #else | |
2573 | process_send_signal (process, SIGTSTP, current_group, 0); | |
2574 | #endif | |
2575 | return process; | |
2576 | } | |
2577 | ||
2578 | DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0, | |
2579 | "Continue process PROCESS. May be process or name of one.\n\ | |
2580 | See function `interrupt-process' for more details on usage.") | |
2581 | (process, current_group) | |
2582 | Lisp_Object process, current_group; | |
2583 | { | |
2584 | #ifdef SIGCONT | |
2585 | process_send_signal (process, SIGCONT, current_group, 0); | |
2586 | #else | |
2587 | error ("no SIGCONT support"); | |
2588 | #endif | |
2589 | return process; | |
2590 | } | |
2591 | ||
2592 | DEFUN ("signal-process", Fsignal_process, Ssignal_process, | |
2593 | 2, 2, "nProcess number: \nnSignal code: ", | |
2594 | "Send the process with number PID the signal with code CODE.\n\ | |
2595 | Both PID and CODE are integers.") | |
2596 | (pid, sig) | |
2597 | Lisp_Object pid, sig; | |
2598 | { | |
2599 | CHECK_NUMBER (pid, 0); | |
2600 | CHECK_NUMBER (sig, 1); | |
2601 | return make_number (kill (XINT (pid), XINT (sig))); | |
2602 | } | |
2603 | ||
2604 | DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0, | |
2605 | "Make PROCESS see end-of-file in its input.\n\ | |
2606 | Eof comes after any text already sent to it.\n\ | |
ebb9e16f JB |
2607 | PROCESS may be a process, a buffer, the name of a process or buffer, or\n\ |
2608 | nil, indicating the current buffer's process.") | |
d0d6b7c5 JB |
2609 | (process) |
2610 | Lisp_Object process; | |
2611 | { | |
2612 | Lisp_Object proc; | |
2613 | ||
2614 | proc = get_process (process); | |
577d03d5 RS |
2615 | |
2616 | /* Make sure the process is really alive. */ | |
2617 | if (! NILP (XPROCESS (proc)->raw_status_low)) | |
2618 | update_status (XPROCESS (proc)); | |
2619 | if (! EQ (XPROCESS (proc)->status, Qrun)) | |
dcf970e6 | 2620 | error ("Process %s not running", XSTRING (XPROCESS (proc)->name)->data); |
577d03d5 | 2621 | |
d0d6b7c5 JB |
2622 | /* Sending a zero-length record is supposed to mean eof |
2623 | when TIOCREMOTE is turned on. */ | |
2624 | #ifdef DID_REMOTE | |
2625 | { | |
2626 | char buf[1]; | |
a9f2c884 | 2627 | write (XINT (XPROCESS (proc)->outfd), buf, 0); |
d0d6b7c5 JB |
2628 | } |
2629 | #else /* did not do TOICREMOTE */ | |
2630 | #ifdef VMS | |
2631 | send_process (proc, "\032", 1); /* ^z */ | |
2632 | #else | |
2633 | if (!NILP (XPROCESS (proc)->pty_flag)) | |
2634 | send_process (proc, "\004", 1); | |
2635 | else | |
2636 | { | |
a9f2c884 RS |
2637 | close (XINT (XPROCESS (proc)->outfd)); |
2638 | XSET (XPROCESS (proc)->outfd, Lisp_Int, open (NULL_DEVICE, O_WRONLY)); | |
d0d6b7c5 JB |
2639 | } |
2640 | #endif /* VMS */ | |
2641 | #endif /* did not do TOICREMOTE */ | |
2642 | return process; | |
2643 | } | |
2644 | ||
2645 | /* Kill all processes associated with `buffer'. | |
2646 | If `buffer' is nil, kill all processes */ | |
2647 | ||
2648 | kill_buffer_processes (buffer) | |
2649 | Lisp_Object buffer; | |
2650 | { | |
2651 | Lisp_Object tail, proc; | |
2652 | ||
2653 | for (tail = Vprocess_alist; XGCTYPE (tail) == Lisp_Cons; | |
2654 | tail = XCONS (tail)->cdr) | |
2655 | { | |
2656 | proc = XCONS (XCONS (tail)->car)->cdr; | |
2657 | if (XGCTYPE (proc) == Lisp_Process | |
2658 | && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))) | |
2659 | { | |
2660 | if (NETCONN_P (proc)) | |
2661 | deactivate_process (proc); | |
a9f2c884 | 2662 | else if (XINT (XPROCESS (proc)->infd) >= 0) |
d0d6b7c5 JB |
2663 | process_send_signal (proc, SIGHUP, Qnil, 1); |
2664 | } | |
2665 | } | |
2666 | } | |
2667 | \f | |
2668 | /* On receipt of a signal that a child status has changed, | |
2669 | loop asking about children with changed statuses until | |
2670 | the system says there are no more. | |
2671 | All we do is change the status; | |
2672 | we do not run sentinels or print notifications. | |
2673 | That is saved for the next time keyboard input is done, | |
2674 | in order to avoid timing errors. */ | |
2675 | ||
2676 | /** WARNING: this can be called during garbage collection. | |
2677 | Therefore, it must not be fooled by the presence of mark bits in | |
2678 | Lisp objects. */ | |
2679 | ||
2680 | /** USG WARNING: Although it is not obvious from the documentation | |
2681 | in signal(2), on a USG system the SIGCLD handler MUST NOT call | |
2682 | signal() before executing at least one wait(), otherwise the handler | |
2683 | will be called again, resulting in an infinite loop. The relevant | |
2684 | portion of the documentation reads "SIGCLD signals will be queued | |
2685 | and the signal-catching function will be continually reentered until | |
2686 | the queue is empty". Invoking signal() causes the kernel to reexamine | |
2687 | the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */ | |
2688 | ||
2689 | SIGTYPE | |
2690 | sigchld_handler (signo) | |
2691 | int signo; | |
2692 | { | |
2693 | int old_errno = errno; | |
2694 | Lisp_Object proc; | |
2695 | register struct Lisp_Process *p; | |
6be429b1 | 2696 | extern EMACS_TIME *input_available_clear_time; |
d0d6b7c5 JB |
2697 | |
2698 | #ifdef BSD4_1 | |
2699 | extern int sigheld; | |
2700 | sigheld |= sigbit (SIGCHLD); | |
2701 | #endif | |
2702 | ||
2703 | while (1) | |
2704 | { | |
2705 | register int pid; | |
2706 | WAITTYPE w; | |
2707 | Lisp_Object tail; | |
2708 | ||
2709 | #ifdef WNOHANG | |
2710 | #ifndef WUNTRACED | |
2711 | #define WUNTRACED 0 | |
2712 | #endif /* no WUNTRACED */ | |
2713 | /* Keep trying to get a status until we get a definitive result. */ | |
2714 | do | |
2715 | { | |
2716 | errno = 0; | |
2717 | pid = wait3 (&w, WNOHANG | WUNTRACED, 0); | |
2718 | } | |
2719 | while (pid <= 0 && errno == EINTR); | |
2720 | ||
2721 | if (pid <= 0) | |
2722 | { | |
2723 | /* A real failure. We have done all our job, so return. */ | |
2724 | ||
2725 | /* USG systems forget handlers when they are used; | |
2726 | must reestablish each time */ | |
2727 | #ifdef USG | |
2728 | signal (signo, sigchld_handler); /* WARNING - must come after wait3() */ | |
2729 | #endif | |
2730 | #ifdef BSD4_1 | |
2731 | sigheld &= ~sigbit (SIGCHLD); | |
2732 | sigrelse (SIGCHLD); | |
2733 | #endif | |
2734 | errno = old_errno; | |
2735 | return; | |
2736 | } | |
2737 | #else | |
2738 | pid = wait (&w); | |
2739 | #endif /* no WNOHANG */ | |
2740 | ||
2741 | /* Find the process that signaled us, and record its status. */ | |
2742 | ||
2743 | p = 0; | |
2744 | for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr) | |
2745 | { | |
2746 | proc = XCONS (XCONS (tail)->car)->cdr; | |
2747 | p = XPROCESS (proc); | |
2748 | if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid) | |
2749 | break; | |
2750 | p = 0; | |
2751 | } | |
2752 | ||
2753 | /* Look for an asynchronous process whose pid hasn't been filled | |
2754 | in yet. */ | |
2755 | if (p == 0) | |
2756 | for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr) | |
2757 | { | |
2758 | proc = XCONS (XCONS (tail)->car)->cdr; | |
2759 | p = XPROCESS (proc); | |
2760 | if (XTYPE (p->pid) == Lisp_Int && XINT (p->pid) == -1) | |
2761 | break; | |
2762 | p = 0; | |
2763 | } | |
2764 | ||
2765 | /* Change the status of the process that was found. */ | |
2766 | if (p != 0) | |
2767 | { | |
2768 | union { int i; WAITTYPE wt; } u; | |
2769 | ||
2770 | XSETINT (p->tick, ++process_tick); | |
2771 | u.wt = w; | |
2772 | XFASTINT (p->raw_status_low) = u.i & 0xffff; | |
2773 | XFASTINT (p->raw_status_high) = u.i >> 16; | |
2774 | ||
2775 | /* If process has terminated, stop waiting for its output. */ | |
2776 | if (WIFSIGNALED (w) || WIFEXITED (w)) | |
a9f2c884 RS |
2777 | if (XINT (p->infd) >= 0) |
2778 | FD_CLR (XINT (p->infd), &input_wait_mask); | |
6be429b1 JB |
2779 | |
2780 | /* Tell wait_reading_process_input that it needs to wake up and | |
2781 | look around. */ | |
2782 | if (input_available_clear_time) | |
2783 | EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); | |
d0d6b7c5 JB |
2784 | } |
2785 | ||
2786 | /* There was no asynchronous process found for that id. Check | |
2787 | if we have a synchronous process. */ | |
2788 | else | |
2789 | { | |
2790 | synch_process_alive = 0; | |
2791 | ||
2792 | /* Report the status of the synchronous process. */ | |
2793 | if (WIFEXITED (w)) | |
2794 | synch_process_retcode = WRETCODE (w); | |
2795 | else if (WIFSIGNALED (w)) | |
b0310da4 | 2796 | #ifndef VMS |
95e8068d | 2797 | synch_process_death = (char *) sys_siglist[WTERMSIG (w)]; |
b0310da4 JB |
2798 | #else |
2799 | synch_process_death = sys_errlist[WTERMSIG (w)]; | |
2800 | #endif | |
6be429b1 JB |
2801 | |
2802 | /* Tell wait_reading_process_input that it needs to wake up and | |
2803 | look around. */ | |
2804 | if (input_available_clear_time) | |
2805 | EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); | |
d0d6b7c5 JB |
2806 | } |
2807 | ||
2808 | /* On some systems, we must return right away. | |
2809 | If any more processes want to signal us, we will | |
2810 | get another signal. | |
2811 | Otherwise (on systems that have WNOHANG), loop around | |
2812 | to use up all the processes that have something to tell us. */ | |
2813 | #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG)) | |
2814 | #ifdef USG | |
2815 | signal (signo, sigchld_handler); | |
2816 | #endif | |
2817 | errno = old_errno; | |
2818 | return; | |
2819 | #endif /* USG, but not HPUX with WNOHANG */ | |
2820 | } | |
2821 | } | |
2822 | \f | |
2823 | ||
2824 | static Lisp_Object | |
2825 | exec_sentinel_unwind (data) | |
2826 | Lisp_Object data; | |
2827 | { | |
2828 | XPROCESS (XCONS (data)->car)->sentinel = XCONS (data)->cdr; | |
2829 | return Qnil; | |
2830 | } | |
2831 | ||
2832 | static void | |
2833 | exec_sentinel (proc, reason) | |
2834 | Lisp_Object proc, reason; | |
2835 | { | |
2836 | Lisp_Object sentinel; | |
2837 | register struct Lisp_Process *p = XPROCESS (proc); | |
2838 | int count = specpdl_ptr - specpdl; | |
2839 | ||
2840 | sentinel = p->sentinel; | |
2841 | if (NILP (sentinel)) | |
2842 | return; | |
2843 | ||
2844 | /* Zilch the sentinel while it's running, to avoid recursive invocations; | |
2845 | assure that it gets restored no matter how the sentinel exits. */ | |
2846 | p->sentinel = Qnil; | |
2847 | record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel)); | |
2848 | /* Inhibit quit so that random quits don't screw up a running filter. */ | |
2849 | specbind (Qinhibit_quit, Qt); | |
2850 | call2 (sentinel, proc, reason); | |
2851 | unbind_to (count); | |
2852 | } | |
2853 | ||
2854 | /* Report all recent events of a change in process status | |
2855 | (either run the sentinel or output a message). | |
2856 | This is done while Emacs is waiting for keyboard input. */ | |
2857 | ||
2858 | status_notify () | |
2859 | { | |
2860 | register Lisp_Object proc, buffer; | |
2861 | Lisp_Object tail = Qnil; | |
2862 | Lisp_Object msg = Qnil; | |
2863 | struct gcpro gcpro1, gcpro2; | |
2864 | ||
2865 | /* We need to gcpro tail; if read_process_output calls a filter | |
2866 | which deletes a process and removes the cons to which tail points | |
2867 | from Vprocess_alist, and then causes a GC, tail is an unprotected | |
2868 | reference. */ | |
2869 | GCPRO2 (tail, msg); | |
2870 | ||
2871 | for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail)) | |
2872 | { | |
2873 | Lisp_Object symbol; | |
2874 | register struct Lisp_Process *p; | |
2875 | ||
2876 | proc = Fcdr (Fcar (tail)); | |
2877 | p = XPROCESS (proc); | |
2878 | ||
2879 | if (XINT (p->tick) != XINT (p->update_tick)) | |
2880 | { | |
2881 | XSETINT (p->update_tick, XINT (p->tick)); | |
2882 | ||
2883 | /* If process is still active, read any output that remains. */ | |
a9f2c884 | 2884 | if (XINT (p->infd) >= 0) |
20ddfff7 | 2885 | while (! EQ (p->filter, Qt) |
a9f2c884 | 2886 | && read_process_output (proc, XINT (p->infd)) > 0); |
d0d6b7c5 JB |
2887 | |
2888 | buffer = p->buffer; | |
2889 | ||
2890 | /* Get the text to use for the message. */ | |
2891 | if (!NILP (p->raw_status_low)) | |
2892 | update_status (p); | |
2893 | msg = status_message (p->status); | |
2894 | ||
2895 | /* If process is terminated, deactivate it or delete it. */ | |
2896 | symbol = p->status; | |
2897 | if (XTYPE (p->status) == Lisp_Cons) | |
2898 | symbol = XCONS (p->status)->car; | |
2899 | ||
2900 | if (EQ (symbol, Qsignal) || EQ (symbol, Qexit) | |
2901 | || EQ (symbol, Qclosed)) | |
2902 | { | |
2903 | if (delete_exited_processes) | |
2904 | remove_process (proc); | |
2905 | else | |
2906 | deactivate_process (proc); | |
2907 | } | |
2908 | ||
2909 | /* Now output the message suitably. */ | |
2910 | if (!NILP (p->sentinel)) | |
2911 | exec_sentinel (proc, msg); | |
2912 | /* Don't bother with a message in the buffer | |
2913 | when a process becomes runnable. */ | |
2914 | else if (!EQ (symbol, Qrun) && !NILP (buffer)) | |
2915 | { | |
2916 | Lisp_Object ro = XBUFFER (buffer)->read_only; | |
2917 | Lisp_Object tem; | |
2918 | struct buffer *old = current_buffer; | |
2919 | int opoint; | |
2920 | ||
2921 | /* Avoid error if buffer is deleted | |
2922 | (probably that's why the process is dead, too) */ | |
2923 | if (NILP (XBUFFER (buffer)->name)) | |
2924 | continue; | |
2925 | Fset_buffer (buffer); | |
2926 | opoint = point; | |
2927 | /* Insert new output into buffer | |
2928 | at the current end-of-output marker, | |
2929 | thus preserving logical ordering of input and output. */ | |
2930 | if (XMARKER (p->mark)->buffer) | |
2931 | SET_PT (marker_position (p->mark)); | |
2932 | else | |
2933 | SET_PT (ZV); | |
2934 | if (point <= opoint) | |
2935 | opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10; | |
2936 | ||
2937 | tem = current_buffer->read_only; | |
2938 | current_buffer->read_only = Qnil; | |
2939 | insert_string ("\nProcess "); | |
2940 | Finsert (1, &p->name); | |
2941 | insert_string (" "); | |
2942 | Finsert (1, &msg); | |
2943 | current_buffer->read_only = tem; | |
2944 | Fset_marker (p->mark, make_number (point), p->buffer); | |
2945 | ||
2946 | SET_PT (opoint); | |
2947 | set_buffer_internal (old); | |
2948 | } | |
2949 | } | |
2950 | } /* end for */ | |
2951 | ||
2952 | update_mode_lines++; /* in case buffers use %s in mode-line-format */ | |
2953 | redisplay_preserve_echo_area (); | |
2954 | ||
2955 | update_tick = process_tick; | |
2956 | ||
2957 | UNGCPRO; | |
2958 | } | |
2959 | \f | |
2960 | init_process () | |
2961 | { | |
2962 | register int i; | |
2963 | ||
2964 | #ifdef SIGCHLD | |
2965 | #ifndef CANNOT_DUMP | |
2966 | if (! noninteractive || initialized) | |
2967 | #endif | |
2968 | signal (SIGCHLD, sigchld_handler); | |
2969 | #endif | |
2970 | ||
2971 | FD_ZERO (&input_wait_mask); | |
dd2281ae RS |
2972 | |
2973 | keyboard_descriptor = 0; | |
2974 | FD_SET (keyboard_descriptor, &input_wait_mask); | |
2975 | ||
d0d6b7c5 JB |
2976 | Vprocess_alist = Qnil; |
2977 | for (i = 0; i < MAXDESC; i++) | |
2978 | { | |
2979 | chan_process[i] = Qnil; | |
2980 | proc_buffered_char[i] = -1; | |
2981 | } | |
2982 | } | |
312c9964 | 2983 | |
dd2281ae RS |
2984 | /* From now on, assume keyboard input comes from descriptor DESC. */ |
2985 | ||
2986 | void | |
2987 | change_keyboard_wait_descriptor (desc) | |
2988 | int desc; | |
2989 | { | |
2990 | FD_CLR (keyboard_descriptor, &input_wait_mask); | |
2991 | keyboard_descriptor = desc; | |
2992 | FD_SET (keyboard_descriptor, &input_wait_mask); | |
2993 | } | |
2994 | ||
d0d6b7c5 JB |
2995 | syms_of_process () |
2996 | { | |
d0d6b7c5 JB |
2997 | #ifdef HAVE_SOCKETS |
2998 | stream_process = intern ("stream"); | |
2999 | #endif | |
3000 | Qprocessp = intern ("processp"); | |
3001 | staticpro (&Qprocessp); | |
3002 | Qrun = intern ("run"); | |
3003 | staticpro (&Qrun); | |
3004 | Qstop = intern ("stop"); | |
3005 | staticpro (&Qstop); | |
3006 | Qsignal = intern ("signal"); | |
3007 | staticpro (&Qsignal); | |
3008 | ||
3009 | /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it | |
3010 | here again. | |
3011 | ||
3012 | Qexit = intern ("exit"); | |
3013 | staticpro (&Qexit); */ | |
3014 | ||
3015 | Qopen = intern ("open"); | |
3016 | staticpro (&Qopen); | |
3017 | Qclosed = intern ("closed"); | |
3018 | staticpro (&Qclosed); | |
3019 | ||
3020 | staticpro (&Vprocess_alist); | |
3021 | ||
3022 | DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes, | |
3023 | "*Non-nil means delete processes immediately when they exit.\n\ | |
3024 | nil means don't delete them until `list-processes' is run."); | |
3025 | ||
3026 | delete_exited_processes = 1; | |
3027 | ||
3028 | DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type, | |
3029 | "Control type of device used to communicate with subprocesses.\n\ | |
3030 | Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\ | |
3031 | pty's are not available, this variable will be ignored. The value takes\n\ | |
3032 | effect when `start-process' is called."); | |
3033 | Vprocess_connection_type = Qt; | |
3034 | ||
3035 | defsubr (&Sprocessp); | |
3036 | defsubr (&Sget_process); | |
3037 | defsubr (&Sget_buffer_process); | |
3038 | defsubr (&Sdelete_process); | |
3039 | defsubr (&Sprocess_status); | |
3040 | defsubr (&Sprocess_exit_status); | |
3041 | defsubr (&Sprocess_id); | |
3042 | defsubr (&Sprocess_name); | |
3043 | defsubr (&Sprocess_command); | |
3044 | defsubr (&Sset_process_buffer); | |
3045 | defsubr (&Sprocess_buffer); | |
3046 | defsubr (&Sprocess_mark); | |
3047 | defsubr (&Sset_process_filter); | |
3048 | defsubr (&Sprocess_filter); | |
3049 | defsubr (&Sset_process_sentinel); | |
3050 | defsubr (&Sprocess_sentinel); | |
3051 | defsubr (&Sprocess_kill_without_query); | |
3052 | defsubr (&Slist_processes); | |
3053 | defsubr (&Sprocess_list); | |
3054 | defsubr (&Sstart_process); | |
3055 | #ifdef HAVE_SOCKETS | |
3056 | defsubr (&Sopen_network_stream); | |
3057 | #endif /* HAVE_SOCKETS */ | |
3058 | defsubr (&Saccept_process_output); | |
3059 | defsubr (&Sprocess_send_region); | |
3060 | defsubr (&Sprocess_send_string); | |
3061 | defsubr (&Sinterrupt_process); | |
3062 | defsubr (&Skill_process); | |
3063 | defsubr (&Squit_process); | |
3064 | defsubr (&Sstop_process); | |
3065 | defsubr (&Scontinue_process); | |
3066 | defsubr (&Sprocess_send_eof); | |
3067 | defsubr (&Ssignal_process); | |
3068 | defsubr (&Swaiting_for_user_input_p); | |
3069 | /* defsubr (&Sprocess_connection); */ | |
3070 | } | |
3071 | ||
6720a7fb JB |
3072 | \f |
3073 | #else /* not subprocesses */ | |
3074 | ||
3075 | #include <sys/types.h> | |
3076 | #include <errno.h> | |
3077 | ||
3078 | #include "lisp.h" | |
3079 | #include "systime.h" | |
3080 | #include "termopts.h" | |
3081 | ||
ff11dfa1 | 3082 | extern int frame_garbaged; |
6720a7fb JB |
3083 | |
3084 | ||
3085 | /* As described above, except assuming that there are no subprocesses: | |
3086 | ||
3087 | Wait for timeout to elapse and/or keyboard input to be available. | |
3088 | ||
3089 | time_limit is: | |
3090 | timeout in seconds, or | |
3091 | zero for no limit, or | |
3092 | -1 means gobble data immediately available but don't wait for any. | |
3093 | ||
f76475ad | 3094 | read_kbd is a Lisp_Object: |
6720a7fb JB |
3095 | 0 to ignore keyboard input, or |
3096 | 1 to return when input is available, or | |
3097 | -1 means caller will actually read the input, so don't throw to | |
3098 | the quit handler. | |
3099 | We know that read_kbd will never be a Lisp_Process, since | |
3100 | `subprocesses' isn't defined. | |
3101 | ||
3102 | do_display != 0 means redisplay should be done to show subprocess | |
3103 | output that arrives. This version of the function ignores it. | |
3104 | ||
eb8c3be9 | 3105 | Return true iff we received input from any process. */ |
6720a7fb JB |
3106 | |
3107 | int | |
3108 | wait_reading_process_input (time_limit, microsecs, read_kbd, do_display) | |
f76475ad JB |
3109 | int time_limit, microsecs; |
3110 | Lisp_Object read_kbd; | |
3111 | int do_display; | |
6720a7fb JB |
3112 | { |
3113 | EMACS_TIME end_time, timeout, *timeout_p; | |
3114 | int waitchannels; | |
3115 | ||
3116 | /* What does time_limit really mean? */ | |
3117 | if (time_limit || microsecs) | |
3118 | { | |
3119 | /* It's not infinite. */ | |
3120 | timeout_p = &timeout; | |
3121 | ||
3122 | if (time_limit == -1) | |
3123 | /* In fact, it's zero. */ | |
3124 | EMACS_SET_SECS_USECS (timeout, 0, 0); | |
3125 | else | |
3126 | EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); | |
3127 | ||
3128 | /* How far in the future is that? */ | |
3129 | EMACS_GET_TIME (end_time); | |
3130 | EMACS_ADD_TIME (end_time, end_time, timeout); | |
3131 | } | |
3132 | else | |
3133 | /* It's infinite. */ | |
3134 | timeout_p = 0; | |
3135 | ||
3136 | /* Turn off periodic alarms (in case they are in use) | |
3137 | because the select emulator uses alarms. */ | |
3138 | stop_polling (); | |
3139 | ||
3140 | for (;;) | |
3141 | { | |
3142 | int nfds; | |
3143 | ||
f76475ad | 3144 | waitchannels = XINT (read_kbd) ? 1 : 0; |
6720a7fb JB |
3145 | |
3146 | /* If calling from keyboard input, do not quit | |
3147 | since we want to return C-g as an input character. | |
3148 | Otherwise, do pending quit if requested. */ | |
f76475ad | 3149 | if (XINT (read_kbd) >= 0) |
6720a7fb JB |
3150 | QUIT; |
3151 | ||
3152 | if (timeout_p) | |
3153 | { | |
3154 | EMACS_GET_TIME (*timeout_p); | |
3155 | EMACS_SUB_TIME (*timeout_p, end_time, *timeout_p); | |
3156 | if (EMACS_TIME_NEG_P (*timeout_p)) | |
3157 | break; | |
3158 | } | |
3159 | ||
3160 | /* Cause C-g and alarm signals to take immediate action, | |
3161 | and cause input available signals to zero out timeout. */ | |
f76475ad | 3162 | if (XINT (read_kbd) < 0) |
6720a7fb JB |
3163 | set_waiting_for_input (&timeout); |
3164 | ||
ff11dfa1 | 3165 | /* If a frame has been newly mapped and needs updating, |
6720a7fb | 3166 | reprocess its display stuff. */ |
ff11dfa1 | 3167 | if (frame_garbaged) |
6720a7fb JB |
3168 | redisplay_preserve_echo_area (); |
3169 | ||
f76475ad | 3170 | if (XINT (read_kbd) && detect_input_pending ()) |
6720a7fb JB |
3171 | nfds = 0; |
3172 | else | |
3173 | nfds = select (1, &waitchannels, 0, 0, timeout_p); | |
3174 | ||
3175 | /* Make C-g and alarm signals set flags again */ | |
3176 | clear_waiting_for_input (); | |
3177 | ||
3178 | /* If we woke up due to SIGWINCH, actually change size now. */ | |
3179 | do_pending_window_change (); | |
3180 | ||
3181 | if (nfds == -1) | |
3182 | { | |
3183 | /* If the system call was interrupted, then go around the | |
3184 | loop again. */ | |
3185 | if (errno == EINTR) | |
3186 | waitchannels = 0; | |
3187 | } | |
3188 | #ifdef sun | |
3189 | else if (nfds > 0 && (waitchannels & 1) && interrupt_input) | |
3190 | /* System sometimes fails to deliver SIGIO. */ | |
3191 | kill (getpid (), SIGIO); | |
3192 | #endif | |
7324d660 | 3193 | #ifdef SIGIO |
f76475ad | 3194 | if (XINT (read_kbd) && interrupt_input && (waitchannels & 1)) |
6720a7fb | 3195 | kill (0, SIGIO); |
7324d660 | 3196 | #endif |
6720a7fb JB |
3197 | |
3198 | /* If we have timed out (nfds == 0) or found some input (nfds > 0), | |
3199 | we should exit. */ | |
3200 | if (nfds >= 0) | |
3201 | break; | |
3202 | } | |
3203 | ||
a87b802f JB |
3204 | start_polling (); |
3205 | ||
6720a7fb JB |
3206 | return 0; |
3207 | } | |
3208 | ||
3209 | ||
3210 | DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0, | |
312c9964 RS |
3211 | /* Don't confused make-docfile by having two doc strings for this function. |
3212 | make-docfile does not pay attention to #if, for good reason! */ | |
3213 | 0) | |
6720a7fb JB |
3214 | (name) |
3215 | register Lisp_Object name; | |
3216 | { | |
3217 | return Qnil; | |
3218 | } | |
3219 | ||
3220 | /* Kill all processes associated with `buffer'. | |
3221 | If `buffer' is nil, kill all processes. | |
3222 | Since we have no subprocesses, this does nothing. */ | |
3223 | ||
3224 | kill_buffer_processes (buffer) | |
3225 | Lisp_Object buffer; | |
3226 | { | |
3227 | } | |
3228 | ||
3229 | init_process () | |
3230 | { | |
3231 | } | |
3232 | ||
3233 | syms_of_process () | |
3234 | { | |
3235 | defsubr (&Sget_buffer_process); | |
3236 | } | |
3237 | ||
3238 | \f | |
3239 | #endif /* not subprocesses */ |