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