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