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