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