comparison src/process.c @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 7c27c9efd353
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 /* Asynchronous subprocess control for GNU Emacs. 1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 96, 98, 1999, 2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 2001, 2002 Free Software Foundation, Inc. 3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005 Free Software Foundation, Inc.
4 5
5 This file is part of GNU Emacs. 6 This file is part of GNU Emacs.
6 7
7 GNU Emacs is free software; you can redistribute it and/or modify 8 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 it under the terms of the GNU General Public License as published by
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 GNU General Public License for more details.
16 17
17 You should have received a copy of the GNU General Public License 18 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to 19 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02111-1307, USA. */ 21 Boston, MA 02110-1301, USA. */
21 22
22 23
23 #include <config.h> 24 #include <config.h>
24 #include <signal.h> 25 #include <signal.h>
25 26
37 #include <errno.h> 38 #include <errno.h>
38 #include <setjmp.h> 39 #include <setjmp.h>
39 #include <sys/types.h> /* some typedefs are used in sys/file.h */ 40 #include <sys/types.h> /* some typedefs are used in sys/file.h */
40 #include <sys/file.h> 41 #include <sys/file.h>
41 #include <sys/stat.h> 42 #include <sys/stat.h>
43 #ifdef HAVE_INTTYPES_H
44 #include <inttypes.h>
45 #endif
42 #ifdef HAVE_UNISTD_H 46 #ifdef HAVE_UNISTD_H
43 #include <unistd.h> 47 #include <unistd.h>
44 #endif 48 #endif
45 49
46 #if defined(WINDOWSNT) || defined(UNIX98_PTYS) 50 #if defined(WINDOWSNT) || defined(UNIX98_PTYS)
96 100
97 #ifdef NEED_BSDTTY 101 #ifdef NEED_BSDTTY
98 #include <bsdtty.h> 102 #include <bsdtty.h>
99 #endif 103 #endif
100 104
105 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
106 #ifdef HAVE_SOCKETS
107 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
108 /* sys/ioctl.h may have been included already */
109 #ifndef SIOCGIFADDR
110 #include <sys/ioctl.h>
111 #endif
112 #include <net/if.h>
113 #endif
114 #endif
115
101 #ifdef IRIS 116 #ifdef IRIS
102 #include <sys/sysmacros.h> /* for "minor" */ 117 #include <sys/sysmacros.h> /* for "minor" */
103 #endif /* not IRIS */ 118 #endif /* not IRIS */
104 119
105 #ifdef HAVE_SYS_WAIT 120 #ifdef HAVE_SYS_WAIT
106 #include <sys/wait.h> 121 #include <sys/wait.h>
107 #endif 122 #endif
108 123
124 /* Disable IPv6 support for w32 until someone figures out how to do it
125 properly. */
126 #ifdef WINDOWSNT
127 # ifdef AF_INET6
128 # undef AF_INET6
129 # endif
130 #endif
131
132 #include "lisp.h"
109 #include "systime.h" 133 #include "systime.h"
110 #include "systty.h" 134 #include "systty.h"
111 135
112 #include "lisp.h"
113 #include "window.h" 136 #include "window.h"
114 #include "buffer.h" 137 #include "buffer.h"
115 #include "charset.h" 138 #include "charset.h"
116 #include "coding.h" 139 #include "coding.h"
117 #include "process.h" 140 #include "process.h"
126 #include "atimer.h" 149 #include "atimer.h"
127 150
128 Lisp_Object Qprocessp; 151 Lisp_Object Qprocessp;
129 Lisp_Object Qrun, Qstop, Qsignal; 152 Lisp_Object Qrun, Qstop, Qsignal;
130 Lisp_Object Qopen, Qclosed, Qconnect, Qfailed, Qlisten; 153 Lisp_Object Qopen, Qclosed, Qconnect, Qfailed, Qlisten;
131 Lisp_Object Qlocal, Qdatagram; 154 Lisp_Object Qlocal, Qipv4, Qdatagram;
155 #ifdef AF_INET6
156 Lisp_Object Qipv6;
157 #endif
132 Lisp_Object QCname, QCbuffer, QChost, QCservice, QCtype; 158 Lisp_Object QCname, QCbuffer, QChost, QCservice, QCtype;
133 Lisp_Object QClocal, QCremote, QCcoding; 159 Lisp_Object QClocal, QCremote, QCcoding;
134 Lisp_Object QCserver, QCnowait, QCnoquery, QCstop; 160 Lisp_Object QCserver, QCnowait, QCnoquery, QCstop;
135 Lisp_Object QCsentinel, QClog, QCoptions, QCplist; 161 Lisp_Object QCsentinel, QClog, QCoptions, QCplist;
136 Lisp_Object QCfilter_multibyte; 162 Lisp_Object QCfilter_multibyte;
173 199
174 #include "syssignal.h" 200 #include "syssignal.h"
175 201
176 #include "syswait.h" 202 #include "syswait.h"
177 203
178 extern void set_waiting_for_input P_ ((EMACS_TIME *)); 204 extern char *get_operating_system_release ();
179 205
180 #ifndef USE_CRT_DLL 206 #ifndef USE_CRT_DLL
181 extern int errno; 207 extern int errno;
182 #endif 208 #endif
183 #ifdef VMS 209 #ifdef VMS
247 #ifdef TERM 273 #ifdef TERM
248 #undef NON_BLOCKING_CONNECT 274 #undef NON_BLOCKING_CONNECT
249 #undef DATAGRAM_SOCKETS 275 #undef DATAGRAM_SOCKETS
250 #endif 276 #endif
251 277
278 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
279 #ifdef EMACS_HAS_USECS
280 #define ADAPTIVE_READ_BUFFERING
281 #endif
282 #endif
283
284 #ifdef ADAPTIVE_READ_BUFFERING
285 #define READ_OUTPUT_DELAY_INCREMENT 10000
286 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
287 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
288
289 /* Number of processes which have a non-zero read_output_delay,
290 and therefore might be delayed for adaptive read buffering. */
291
292 static int process_output_delay_count;
293
294 /* Non-zero if any process has non-nil read_output_skip. */
295
296 static int process_output_skip;
297
298 /* Non-nil means to delay reading process output to improve buffering.
299 A value of t means that delay is reset after each send, any other
300 non-nil value does not reset the delay. A value of nil disables
301 adaptive read buffering completely. */
302 static Lisp_Object Vprocess_adaptive_read_buffering;
303 #else
304 #define process_output_delay_count 0
305 #endif
306
252 307
253 #include "sysselect.h" 308 #include "sysselect.h"
254 309
255 extern int keyboard_bit_set P_ ((SELECT_TYPE *)); 310 static int keyboard_bit_set P_ ((SELECT_TYPE *));
311 static void deactivate_process P_ ((Lisp_Object));
312 static void status_notify P_ ((struct Lisp_Process *));
313 static int read_process_output P_ ((Lisp_Object, int));
256 314
257 /* If we support a window system, turn on the code to poll periodically 315 /* If we support a window system, turn on the code to poll periodically
258 to detect C-g. It isn't actually used when doing interrupt input. */ 316 to detect C-g. It isn't actually used when doing interrupt input. */
259 #ifdef HAVE_WINDOW_SYSTEM 317 #ifdef HAVE_WINDOW_SYSTEM
260 #define POLL_FOR_INPUT 318 #define POLL_FOR_INPUT
270 328
271 /* Mask that excludes process input descriptor (s). */ 329 /* Mask that excludes process input descriptor (s). */
272 330
273 static SELECT_TYPE non_process_wait_mask; 331 static SELECT_TYPE non_process_wait_mask;
274 332
333 #ifdef NON_BLOCKING_CONNECT
275 /* Mask of bits indicating the descriptors that we wait for connect to 334 /* Mask of bits indicating the descriptors that we wait for connect to
276 complete on. Once they complete, they are removed from this mask 335 complete on. Once they complete, they are removed from this mask
277 and added to the input_wait_mask and non_keyboard_wait_mask. */ 336 and added to the input_wait_mask and non_keyboard_wait_mask. */
278 337
279 static SELECT_TYPE connect_wait_mask; 338 static SELECT_TYPE connect_wait_mask;
280 339
281 /* Number of bits set in connect_wait_mask. */ 340 /* Number of bits set in connect_wait_mask. */
282 static int num_pending_connects; 341 static int num_pending_connects;
342
343 #define IF_NON_BLOCKING_CONNECT(s) s
344 #else
345 #define IF_NON_BLOCKING_CONNECT(s)
346 #endif
283 347
284 /* The largest descriptor currently in use for a process object. */ 348 /* The largest descriptor currently in use for a process object. */
285 static int max_process_desc; 349 static int max_process_desc;
286 350
287 /* The largest descriptor currently in use for keyboard input. */ 351 /* The largest descriptor currently in use for keyboard input. */
329 extern int timers_run; 393 extern int timers_run;
330 394
331 /* Maximum number of bytes to send to a pty without an eof. */ 395 /* Maximum number of bytes to send to a pty without an eof. */
332 static int pty_max_bytes; 396 static int pty_max_bytes;
333 397
334 extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
335
336 #ifdef HAVE_PTYS 398 #ifdef HAVE_PTYS
337 #ifdef HAVE_PTY_H 399 #ifdef HAVE_PTY_H
338 #include <pty.h> 400 #include <pty.h>
339 #endif 401 #endif
340 /* The file name of the pty opened by allocate_pty. */ 402 /* The file name of the pty opened by allocate_pty. */
343 #endif 405 #endif
344 406
345 /* Compute the Lisp form of the process status, p->status, from 407 /* Compute the Lisp form of the process status, p->status, from
346 the numeric status that was returned by `wait'. */ 408 the numeric status that was returned by `wait'. */
347 409
348 Lisp_Object status_convert (); 410 static Lisp_Object status_convert ();
349 411
350 void 412 static void
351 update_status (p) 413 update_status (p)
352 struct Lisp_Process *p; 414 struct Lisp_Process *p;
353 { 415 {
354 union { int i; WAITTYPE wt; } u; 416 union { int i; WAITTYPE wt; } u;
355 u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16); 417 u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
359 } 421 }
360 422
361 /* Convert a process status word in Unix format to 423 /* Convert a process status word in Unix format to
362 the list that we use internally. */ 424 the list that we use internally. */
363 425
364 Lisp_Object 426 static Lisp_Object
365 status_convert (w) 427 status_convert (w)
366 WAITTYPE w; 428 WAITTYPE w;
367 { 429 {
368 if (WIFSTOPPED (w)) 430 if (WIFSTOPPED (w))
369 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil)); 431 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
378 } 440 }
379 441
380 /* Given a status-list, extract the three pieces of information 442 /* Given a status-list, extract the three pieces of information
381 and store them individually through the three pointers. */ 443 and store them individually through the three pointers. */
382 444
383 void 445 static void
384 decode_status (l, symbol, code, coredump) 446 decode_status (l, symbol, code, coredump)
385 Lisp_Object l; 447 Lisp_Object l;
386 Lisp_Object *symbol; 448 Lisp_Object *symbol;
387 int *code; 449 int *code;
388 int *coredump; 450 int *coredump;
405 } 467 }
406 } 468 }
407 469
408 /* Return a string describing a process status list. */ 470 /* Return a string describing a process status list. */
409 471
410 Lisp_Object 472 static Lisp_Object
411 status_message (status) 473 status_message (p)
412 Lisp_Object status; 474 struct Lisp_Process *p;
413 { 475 {
476 Lisp_Object status = p->status;
414 Lisp_Object symbol; 477 Lisp_Object symbol;
415 int code, coredump; 478 int code, coredump;
416 Lisp_Object string, string2; 479 Lisp_Object string, string2;
417 480
418 decode_status (status, &symbol, &code, &coredump); 481 decode_status (status, &symbol, &code, &coredump);
429 SSET (string, 0, DOWNCASE (SREF (string, 0))); 492 SSET (string, 0, DOWNCASE (SREF (string, 0)));
430 return concat2 (string, string2); 493 return concat2 (string, string2);
431 } 494 }
432 else if (EQ (symbol, Qexit)) 495 else if (EQ (symbol, Qexit))
433 { 496 {
497 if (NETCONN1_P (p))
498 return build_string (code == 0 ? "deleted\n" : "connection broken by remote peer\n");
434 if (code == 0) 499 if (code == 0)
435 return build_string ("finished\n"); 500 return build_string ("finished\n");
436 string = Fnumber_to_string (make_number (code)); 501 string = Fnumber_to_string (make_number (code));
437 string2 = build_string (coredump ? " (core dumped)\n" : "\n"); 502 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
438 return concat3 (build_string ("exited abnormally with code "), 503 return concat3 (build_string ("exited abnormally with code "),
454 /* Open an available pty, returning a file descriptor. 519 /* Open an available pty, returning a file descriptor.
455 Return -1 on failure. 520 Return -1 on failure.
456 The file name of the terminal corresponding to the pty 521 The file name of the terminal corresponding to the pty
457 is left in the variable pty_name. */ 522 is left in the variable pty_name. */
458 523
459 int 524 static int
460 allocate_pty () 525 allocate_pty ()
461 { 526 {
462 struct stat stb;
463 register int c, i; 527 register int c, i;
464 int fd; 528 int fd;
465
466 /* Some systems name their pseudoterminals so that there are gaps in
467 the usual sequence - for example, on HP9000/S700 systems, there
468 are no pseudoterminals with names ending in 'f'. So we wait for
469 three failures in a row before deciding that we've reached the
470 end of the ptys. */
471 int failed_count = 0;
472 529
473 #ifdef PTY_ITERATION 530 #ifdef PTY_ITERATION
474 PTY_ITERATION 531 PTY_ITERATION
475 #else 532 #else
476 for (c = FIRST_PTY_LETTER; c <= 'z'; c++) 533 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
477 for (i = 0; i < 16; i++) 534 for (i = 0; i < 16; i++)
478 #endif 535 #endif
479 { 536 {
537 struct stat stb; /* Used in some PTY_OPEN. */
480 #ifdef PTY_NAME_SPRINTF 538 #ifdef PTY_NAME_SPRINTF
481 PTY_NAME_SPRINTF 539 PTY_NAME_SPRINTF
482 #else 540 #else
483 sprintf (pty_name, "/dev/pty%c%x", c, i); 541 sprintf (pty_name, "/dev/pty%c%x", c, i);
484 #endif /* no PTY_NAME_SPRINTF */ 542 #endif /* no PTY_NAME_SPRINTF */
485 543
486 #ifdef PTY_OPEN 544 #ifdef PTY_OPEN
487 PTY_OPEN; 545 PTY_OPEN;
488 #else /* no PTY_OPEN */ 546 #else /* no PTY_OPEN */
489 #ifdef IRIS 547 {
490 /* Unusual IRIS code */ 548 # ifdef IRIS
491 *ptyv = emacs_open ("/dev/ptc", O_RDWR | O_NDELAY, 0); 549 /* Unusual IRIS code */
492 if (fd < 0) 550 *ptyv = emacs_open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
493 return -1; 551 if (fd < 0)
494 if (fstat (fd, &stb) < 0) 552 return -1;
495 return -1; 553 if (fstat (fd, &stb) < 0)
496 #else /* not IRIS */ 554 return -1;
497 if (stat (pty_name, &stb) < 0) 555 # else /* not IRIS */
498 { 556 { /* Some systems name their pseudoterminals so that there are gaps in
499 failed_count++; 557 the usual sequence - for example, on HP9000/S700 systems, there
500 if (failed_count >= 3) 558 are no pseudoterminals with names ending in 'f'. So we wait for
501 return -1; 559 three failures in a row before deciding that we've reached the
560 end of the ptys. */
561 int failed_count = 0;
562
563 if (stat (pty_name, &stb) < 0)
564 {
565 failed_count++;
566 if (failed_count >= 3)
567 return -1;
568 }
569 else
570 failed_count = 0;
502 } 571 }
503 else 572 # ifdef O_NONBLOCK
504 failed_count = 0; 573 fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
505 #ifdef O_NONBLOCK 574 # else
506 fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0); 575 fd = emacs_open (pty_name, O_RDWR | O_NDELAY, 0);
507 #else 576 # endif
508 fd = emacs_open (pty_name, O_RDWR | O_NDELAY, 0); 577 # endif /* not IRIS */
509 #endif 578 }
510 #endif /* not IRIS */
511 #endif /* no PTY_OPEN */ 579 #endif /* no PTY_OPEN */
512 580
513 if (fd >= 0) 581 if (fd >= 0)
514 { 582 {
515 /* check to make certain that both sides are available 583 /* check to make certain that both sides are available
521 #endif /* no PTY_TTY_NAME_SPRINTF */ 589 #endif /* no PTY_TTY_NAME_SPRINTF */
522 #ifndef UNIPLUS 590 #ifndef UNIPLUS
523 if (access (pty_name, 6) != 0) 591 if (access (pty_name, 6) != 0)
524 { 592 {
525 emacs_close (fd); 593 emacs_close (fd);
526 #if !defined(IRIS) && !defined(__sgi) 594 # if !defined(IRIS) && !defined(__sgi)
527 continue; 595 continue;
528 #else 596 # else
529 return -1; 597 return -1;
530 #endif /* IRIS */ 598 # endif /* IRIS */
531 } 599 }
532 #endif /* not UNIPLUS */ 600 #endif /* not UNIPLUS */
533 setup_pty (fd); 601 setup_pty (fd);
534 return fd; 602 return fd;
535 } 603 }
536 } 604 }
537 return -1; 605 return -1;
538 } 606 }
539 #endif /* HAVE_PTYS */ 607 #endif /* HAVE_PTYS */
540 608
541 Lisp_Object 609 static Lisp_Object
542 make_process (name) 610 make_process (name)
543 Lisp_Object name; 611 Lisp_Object name;
544 { 612 {
545 register Lisp_Object val, tem, name1; 613 register Lisp_Object val, tem, name1;
546 register struct Lisp_Process *p; 614 register struct Lisp_Process *p;
557 p->raw_status_low = Qnil; 625 p->raw_status_low = Qnil;
558 p->raw_status_high = Qnil; 626 p->raw_status_high = Qnil;
559 p->status = Qrun; 627 p->status = Qrun;
560 p->mark = Fmake_marker (); 628 p->mark = Fmake_marker ();
561 629
630 #ifdef ADAPTIVE_READ_BUFFERING
631 p->adaptive_read_buffering = Qnil;
632 XSETFASTINT (p->read_output_delay, 0);
633 p->read_output_skip = Qnil;
634 #endif
635
562 /* If name is already in use, modify it until it is unused. */ 636 /* If name is already in use, modify it until it is unused. */
563 637
564 name1 = name; 638 name1 = name;
565 for (i = 1; ; i++) 639 for (i = 1; ; i++)
566 { 640 {
574 XSETPROCESS (val, p); 648 XSETPROCESS (val, p);
575 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist); 649 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
576 return val; 650 return val;
577 } 651 }
578 652
579 void 653 static void
580 remove_process (proc) 654 remove_process (proc)
581 register Lisp_Object proc; 655 register Lisp_Object proc;
582 { 656 {
583 register Lisp_Object pair; 657 register Lisp_Object pair;
584 658
595 Lisp_Object process; 669 Lisp_Object process;
596 { 670 {
597 struct Lisp_Process *p = XPROCESS (process); 671 struct Lisp_Process *p = XPROCESS (process);
598 int inch = XINT (p->infd); 672 int inch = XINT (p->infd);
599 int outch = XINT (p->outfd); 673 int outch = XINT (p->outfd);
674
675 if (inch < 0 || outch < 0)
676 return;
600 677
601 if (!proc_decode_coding_system[inch]) 678 if (!proc_decode_coding_system[inch])
602 proc_decode_coding_system[inch] 679 proc_decode_coding_system[inch]
603 = (struct coding_system *) xmalloc (sizeof (struct coding_system)); 680 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
604 setup_coding_system (p->decode_coding_system, 681 setup_coding_system (p->decode_coding_system,
705 PROCESS may be a process, a buffer, the name of a process or buffer, or 782 PROCESS may be a process, a buffer, the name of a process or buffer, or
706 nil, indicating the current buffer's process. */) 783 nil, indicating the current buffer's process. */)
707 (process) 784 (process)
708 register Lisp_Object process; 785 register Lisp_Object process;
709 { 786 {
787 register struct Lisp_Process *p;
788
710 process = get_process (process); 789 process = get_process (process);
711 XPROCESS (process)->raw_status_low = Qnil; 790 p = XPROCESS (process);
712 XPROCESS (process)->raw_status_high = Qnil; 791
713 if (NETCONN_P (process)) 792 p->raw_status_low = Qnil;
714 { 793 p->raw_status_high = Qnil;
715 XPROCESS (process)->status = Fcons (Qexit, Fcons (make_number (0), Qnil)); 794 if (NETCONN1_P (p))
716 XSETINT (XPROCESS (process)->tick, ++process_tick); 795 {
717 } 796 p->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
718 else if (XINT (XPROCESS (process)->infd) >= 0) 797 XSETINT (p->tick, ++process_tick);
798 status_notify (p);
799 }
800 else if (XINT (p->infd) >= 0)
719 { 801 {
720 Fkill_process (process, Qnil); 802 Fkill_process (process, Qnil);
721 /* Do this now, since remove_process will make sigchld_handler do nothing. */ 803 /* Do this now, since remove_process will make sigchld_handler do nothing. */
722 XPROCESS (process)->status 804 p->status
723 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil)); 805 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
724 XSETINT (XPROCESS (process)->tick, ++process_tick); 806 XSETINT (p->tick, ++process_tick);
725 status_notify (); 807 status_notify (p);
726 } 808 }
727 remove_process (process); 809 remove_process (process);
728 return Qnil; 810 return Qnil;
729 } 811 }
730 812
790 return make_number (0); 872 return make_number (0);
791 } 873 }
792 874
793 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0, 875 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
794 doc: /* Return the process id of PROCESS. 876 doc: /* Return the process id of PROCESS.
795 This is the pid of the Unix process which PROCESS uses or talks to. 877 This is the pid of the external process which PROCESS uses or talks to.
796 For a network connection, this value is nil. */) 878 For a network connection, this value is nil. */)
797 (process) 879 (process)
798 register Lisp_Object process; 880 register Lisp_Object process;
799 { 881 {
800 CHECK_PROCESS (process); 882 CHECK_PROCESS (process);
946 The sentinel is called as a function when the process changes state. 1028 The sentinel is called as a function when the process changes state.
947 It gets two arguments: the process, and a string describing the change. */) 1029 It gets two arguments: the process, and a string describing the change. */)
948 (process, sentinel) 1030 (process, sentinel)
949 register Lisp_Object process, sentinel; 1031 register Lisp_Object process, sentinel;
950 { 1032 {
1033 struct Lisp_Process *p;
1034
951 CHECK_PROCESS (process); 1035 CHECK_PROCESS (process);
952 XPROCESS (process)->sentinel = sentinel; 1036 p = XPROCESS (process);
1037
1038 p->sentinel = sentinel;
1039 if (NETCONN1_P (p))
1040 p->childp = Fplist_put (p->childp, QCsentinel, sentinel);
953 return sentinel; 1041 return sentinel;
954 } 1042 }
955 1043
956 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel, 1044 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
957 1, 1, 0, 1045 1, 1, 0,
1023 1111
1024 DEFUN ("set-process-query-on-exit-flag", 1112 DEFUN ("set-process-query-on-exit-flag",
1025 Fset_process_query_on_exit_flag, Sset_process_query_on_exit_flag, 1113 Fset_process_query_on_exit_flag, Sset_process_query_on_exit_flag,
1026 2, 2, 0, 1114 2, 2, 0,
1027 doc: /* Specify if query is needed for PROCESS when Emacs is exited. 1115 doc: /* Specify if query is needed for PROCESS when Emacs is exited.
1028 If the second argument FLAG is non-nil, emacs will query the user before 1116 If the second argument FLAG is non-nil, Emacs will query the user before
1029 exiting if PROCESS is running. */) 1117 exiting if PROCESS is running. */)
1030 (process, flag) 1118 (process, flag)
1031 register Lisp_Object process, flag; 1119 register Lisp_Object process, flag;
1032 { 1120 {
1033 CHECK_PROCESS (process); 1121 CHECK_PROCESS (process);
1036 } 1124 }
1037 1125
1038 DEFUN ("process-query-on-exit-flag", 1126 DEFUN ("process-query-on-exit-flag",
1039 Fprocess_query_on_exit_flag, Sprocess_query_on_exit_flag, 1127 Fprocess_query_on_exit_flag, Sprocess_query_on_exit_flag,
1040 1, 1, 0, 1128 1, 1, 0,
1041 doc: /* Return the current value of query on exit flag for PROCESS. */) 1129 doc: /* Return the current value of query-on-exit flag for PROCESS. */)
1042 (process) 1130 (process)
1043 register Lisp_Object process; 1131 register Lisp_Object process;
1044 { 1132 {
1045 CHECK_PROCESS (process); 1133 CHECK_PROCESS (process);
1046 return Fnull (XPROCESS (process)->kill_without_query); 1134 return Fnull (XPROCESS (process)->kill_without_query);
1119 1207
1120 #ifdef HAVE_SOCKETS 1208 #ifdef HAVE_SOCKETS
1121 DEFUN ("format-network-address", Fformat_network_address, Sformat_network_address, 1209 DEFUN ("format-network-address", Fformat_network_address, Sformat_network_address,
1122 1, 2, 0, 1210 1, 2, 0,
1123 doc: /* Convert network ADDRESS from internal format to a string. 1211 doc: /* Convert network ADDRESS from internal format to a string.
1212 A 4 or 5 element vector represents an IPv4 address (with port number).
1213 An 8 or 9 element vector represents an IPv6 address (with port number).
1124 If optional second argument OMIT-PORT is non-nil, don't include a port 1214 If optional second argument OMIT-PORT is non-nil, don't include a port
1125 number in the string; in this case, interpret a 4 element vector as an 1215 number in the string, even when present in ADDRESS.
1126 IP address. Returns nil if format of ADDRESS is invalid. */) 1216 Returns nil if format of ADDRESS is invalid. */)
1127 (address, omit_port) 1217 (address, omit_port)
1128 Lisp_Object address, omit_port; 1218 Lisp_Object address, omit_port;
1129 { 1219 {
1130 if (NILP (address)) 1220 if (NILP (address))
1131 return Qnil; 1221 return Qnil;
1132 1222
1133 if (STRINGP (address)) /* AF_LOCAL */ 1223 if (STRINGP (address)) /* AF_LOCAL */
1134 return address; 1224 return address;
1135 1225
1136 if (VECTORP (address)) /* AF_INET */ 1226 if (VECTORP (address)) /* AF_INET or AF_INET6 */
1137 { 1227 {
1138 register struct Lisp_Vector *p = XVECTOR (address); 1228 register struct Lisp_Vector *p = XVECTOR (address);
1139 Lisp_Object args[6]; 1229 Lisp_Object args[6];
1140 int nargs, i; 1230 int nargs, i;
1141 1231
1142 if (!NILP (omit_port) && (p->size == 4 || p->size == 5)) 1232 if (p->size == 4 || (p->size == 5 && !NILP (omit_port)))
1143 { 1233 {
1144 args[0] = build_string ("%d.%d.%d.%d"); 1234 args[0] = build_string ("%d.%d.%d.%d");
1145 nargs = 4; 1235 nargs = 4;
1146 } 1236 }
1147 else if (p->size == 5) 1237 else if (p->size == 5)
1148 { 1238 {
1149 args[0] = build_string ("%d.%d.%d.%d:%d"); 1239 args[0] = build_string ("%d.%d.%d.%d:%d");
1150 nargs = 5; 1240 nargs = 5;
1241 }
1242 else if (p->size == 8 || (p->size == 9 && !NILP (omit_port)))
1243 {
1244 args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1245 nargs = 8;
1246 }
1247 else if (p->size == 9)
1248 {
1249 args[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1250 nargs = 9;
1151 } 1251 }
1152 else 1252 else
1153 return Qnil; 1253 return Qnil;
1154 1254
1155 for (i = 0; i < nargs; i++) 1255 for (i = 0; i < nargs; i++)
1168 1268
1169 return Qnil; 1269 return Qnil;
1170 } 1270 }
1171 #endif 1271 #endif
1172 1272
1173 Lisp_Object 1273 static Lisp_Object
1174 list_processes_1 (query_only) 1274 list_processes_1 (query_only)
1175 Lisp_Object query_only; 1275 Lisp_Object query_only;
1176 { 1276 {
1177 register Lisp_Object tail, tem; 1277 register Lisp_Object tail, tem;
1178 Lisp_Object proc, minspace, tem1; 1278 Lisp_Object proc, minspace, tem1;
1222 } 1322 }
1223 1323
1224 XSETFASTINT (minspace, 1); 1324 XSETFASTINT (minspace, 1);
1225 1325
1226 set_buffer_internal (XBUFFER (Vstandard_output)); 1326 set_buffer_internal (XBUFFER (Vstandard_output));
1227 Fbuffer_disable_undo (Vstandard_output); 1327 current_buffer->undo_list = Qt;
1228 1328
1229 current_buffer->truncate_lines = Qt; 1329 current_buffer->truncate_lines = Qt;
1230 1330
1231 write_string ("Proc", -1); 1331 write_string ("Proc", -1);
1232 Findent_to (i_status, minspace); write_string ("Status", -1); 1332 Findent_to (i_status, minspace); write_string ("Status", -1);
1399 static Lisp_Object start_process_unwind (); 1499 static Lisp_Object start_process_unwind ();
1400 1500
1401 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0, 1501 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
1402 doc: /* Start a program in a subprocess. Return the process object for it. 1502 doc: /* Start a program in a subprocess. Return the process object for it.
1403 NAME is name for process. It is modified if necessary to make it unique. 1503 NAME is name for process. It is modified if necessary to make it unique.
1404 BUFFER is the buffer or (buffer-name) to associate with the process. 1504 BUFFER is the buffer (or buffer name) to associate with the process.
1405 Process output goes at end of that buffer, unless you specify 1505 Process output goes at end of that buffer, unless you specify
1406 an output stream or filter function to handle the output. 1506 an output stream or filter function to handle the output.
1407 BUFFER may be also nil, meaning that this process is not associated 1507 BUFFER may be also nil, meaning that this process is not associated
1408 with any buffer. 1508 with any buffer.
1409 Third arg is program file name. It is searched for in PATH. 1509 PROGRAM is the program file name. It is searched for in PATH.
1410 Remaining arguments are strings to give program as arguments. 1510 Remaining arguments are strings to give program as arguments.
1411 1511
1412 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */) 1512 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1413 (nargs, args) 1513 (nargs, args)
1414 int nargs; 1514 int nargs;
1468 itself; it's all taken care of here. */ 1568 itself; it's all taken care of here. */
1469 record_unwind_protect (start_process_unwind, proc); 1569 record_unwind_protect (start_process_unwind, proc);
1470 1570
1471 XPROCESS (proc)->childp = Qt; 1571 XPROCESS (proc)->childp = Qt;
1472 XPROCESS (proc)->plist = Qnil; 1572 XPROCESS (proc)->plist = Qnil;
1473 XPROCESS (proc)->command_channel_p = Qnil;
1474 XPROCESS (proc)->buffer = buffer; 1573 XPROCESS (proc)->buffer = buffer;
1475 XPROCESS (proc)->sentinel = Qnil; 1574 XPROCESS (proc)->sentinel = Qnil;
1476 XPROCESS (proc)->filter = Qnil; 1575 XPROCESS (proc)->filter = Qnil;
1477 XPROCESS (proc)->filter_multibyte 1576 XPROCESS (proc)->filter_multibyte
1478 = buffer_defaults.enable_multibyte_characters; 1577 = buffer_defaults.enable_multibyte_characters;
1479 XPROCESS (proc)->command = Flist (nargs - 2, args + 2); 1578 XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
1579
1580 #ifdef ADAPTIVE_READ_BUFFERING
1581 XPROCESS (proc)->adaptive_read_buffering = Vprocess_adaptive_read_buffering;
1582 #endif
1480 1583
1481 /* Make the process marker point into the process buffer (if any). */ 1584 /* Make the process marker point into the process buffer (if any). */
1482 if (!NILP (buffer)) 1585 if (!NILP (buffer))
1483 set_marker_both (XPROCESS (proc)->mark, buffer, 1586 set_marker_both (XPROCESS (proc)->mark, buffer,
1484 BUF_ZV (XBUFFER (buffer)), 1587 BUF_ZV (XBUFFER (buffer)),
1635 remove_process (proc); 1738 remove_process (proc);
1636 1739
1637 return Qnil; 1740 return Qnil;
1638 } 1741 }
1639 1742
1640 void 1743 static void
1641 create_process_1 (timer) 1744 create_process_1 (timer)
1642 struct atimer *timer; 1745 struct atimer *timer;
1643 { 1746 {
1644 /* Nothing to do. */ 1747 /* Nothing to do. */
1645 } 1748 }
1713 #else 1816 #else
1714 forkout = forkin = emacs_open (pty_name, O_RDWR, 0); 1817 forkout = forkin = emacs_open (pty_name, O_RDWR, 0);
1715 #endif 1818 #endif
1716 if (forkin < 0) 1819 if (forkin < 0)
1717 report_file_error ("Opening pty", Qnil); 1820 report_file_error ("Opening pty", Qnil);
1821 #if defined (RTU) || defined (UNIPLUS) || defined (DONT_REOPEN_PTY)
1822 /* In the case that vfork is defined as fork, the parent process
1823 (Emacs) may send some data before the child process completes
1824 tty options setup. So we setup tty before forking. */
1825 child_setup_tty (forkout);
1826 #endif /* RTU or UNIPLUS or DONT_REOPEN_PTY */
1718 #else 1827 #else
1719 forkin = forkout = -1; 1828 forkin = forkout = -1;
1720 #endif /* not USG, or USG_SUBTTY_WORKS */ 1829 #endif /* not USG, or USG_SUBTTY_WORKS */
1721 pty_flag = 1; 1830 pty_flag = 1;
1722 } 1831 }
1777 /* Record this as an active process, with its channels. 1886 /* Record this as an active process, with its channels.
1778 As a result, child_setup will close Emacs's side of the pipes. */ 1887 As a result, child_setup will close Emacs's side of the pipes. */
1779 chan_process[inchannel] = process; 1888 chan_process[inchannel] = process;
1780 XSETINT (XPROCESS (process)->infd, inchannel); 1889 XSETINT (XPROCESS (process)->infd, inchannel);
1781 XSETINT (XPROCESS (process)->outfd, outchannel); 1890 XSETINT (XPROCESS (process)->outfd, outchannel);
1782 /* Record the tty descriptor used in the subprocess. */ 1891
1783 if (forkin < 0) 1892 /* Previously we recorded the tty descriptor used in the subprocess.
1784 XPROCESS (process)->subtty = Qnil; 1893 It was only used for getting the foreground tty process, so now
1785 else 1894 we just reopen the device (see emacs_get_tty_pgrp) as this is
1786 XSETFASTINT (XPROCESS (process)->subtty, forkin); 1895 more portable (see USG_SUBTTY_WORKS above). */
1896
1787 XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil); 1897 XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
1788 XPROCESS (process)->status = Qrun; 1898 XPROCESS (process)->status = Qrun;
1789 setup_process_coding_systems (process); 1899 setup_process_coding_systems (process);
1790 1900
1791 /* Delay interrupts until we have a chance to store 1901 /* Delay interrupts until we have a chance to store
1996 #endif /* ordinary USG */ 2106 #endif /* ordinary USG */
1997 #endif /* not BSD4_1 */ 2107 #endif /* not BSD4_1 */
1998 #endif /* SIGCHLD */ 2108 #endif /* SIGCHLD */
1999 #endif /* !POSIX_SIGNALS */ 2109 #endif /* !POSIX_SIGNALS */
2000 2110
2111 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
2001 if (pty_flag) 2112 if (pty_flag)
2002 child_setup_tty (xforkout); 2113 child_setup_tty (xforkout);
2114 #endif /* not RTU and not UNIPLUS and not DONT_REOPEN_PTY */
2003 #ifdef WINDOWSNT 2115 #ifdef WINDOWSNT
2004 pid = child_setup (xforkin, xforkout, xforkout, 2116 pid = child_setup (xforkin, xforkout, xforkout,
2005 new_argv, 1, current_dir); 2117 new_argv, 1, current_dir);
2006 #else /* not WINDOWSNT */ 2118 #else /* not WINDOWSNT */
2007 child_setup (xforkin, xforkout, xforkout, 2119 child_setup (xforkin, xforkout, xforkout,
2039 2151
2040 stop_polling (); 2152 stop_polling ();
2041 EMACS_SET_SECS_USECS (offset, 1, 0); 2153 EMACS_SET_SECS_USECS (offset, 1, 0);
2042 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0); 2154 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
2043 2155
2044 XPROCESS (process)->subtty = Qnil;
2045 if (forkin >= 0) 2156 if (forkin >= 0)
2046 emacs_close (forkin); 2157 emacs_close (forkin);
2047 2158
2048 cancel_atimer (timer); 2159 cancel_atimer (timer);
2049 start_polling (); 2160 start_polling ();
2125 p = XVECTOR (address); 2236 p = XVECTOR (address);
2126 p->contents[--len] = make_number (ntohs (sin->sin_port)); 2237 p->contents[--len] = make_number (ntohs (sin->sin_port));
2127 cp = (unsigned char *)&sin->sin_addr; 2238 cp = (unsigned char *)&sin->sin_addr;
2128 break; 2239 break;
2129 } 2240 }
2241 #ifdef AF_INET6
2242 case AF_INET6:
2243 {
2244 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
2245 uint16_t *ip6 = (uint16_t *)&sin6->sin6_addr;
2246 len = sizeof (sin6->sin6_addr)/2 + 1;
2247 address = Fmake_vector (make_number (len), Qnil);
2248 p = XVECTOR (address);
2249 p->contents[--len] = make_number (ntohs (sin6->sin6_port));
2250 for (i = 0; i < len; i++)
2251 p->contents[i] = make_number (ntohs (ip6[i]));
2252 return address;
2253 }
2254 #endif
2130 #ifdef HAVE_LOCAL_SOCKETS 2255 #ifdef HAVE_LOCAL_SOCKETS
2131 case AF_LOCAL: 2256 case AF_LOCAL:
2132 { 2257 {
2133 struct sockaddr_un *sockun = (struct sockaddr_un *) sa; 2258 struct sockaddr_un *sockun = (struct sockaddr_un *) sa;
2134 for (i = 0; i < sizeof (sockun->sun_path); i++) 2259 for (i = 0; i < sizeof (sockun->sun_path); i++)
2169 if (p->size == 5) 2294 if (p->size == 5)
2170 { 2295 {
2171 *familyp = AF_INET; 2296 *familyp = AF_INET;
2172 return sizeof (struct sockaddr_in); 2297 return sizeof (struct sockaddr_in);
2173 } 2298 }
2299 #ifdef AF_INET6
2300 else if (p->size == 9)
2301 {
2302 *familyp = AF_INET6;
2303 return sizeof (struct sockaddr_in6);
2304 }
2305 #endif
2174 } 2306 }
2175 #ifdef HAVE_LOCAL_SOCKETS 2307 #ifdef HAVE_LOCAL_SOCKETS
2176 else if (STRINGP (address)) 2308 else if (STRINGP (address))
2177 { 2309 {
2178 *familyp = AF_LOCAL; 2310 *familyp = AF_LOCAL;
2198 Lisp_Object address; 2330 Lisp_Object address;
2199 struct sockaddr *sa; 2331 struct sockaddr *sa;
2200 int len; 2332 int len;
2201 { 2333 {
2202 register struct Lisp_Vector *p; 2334 register struct Lisp_Vector *p;
2203 register unsigned char *cp; 2335 register unsigned char *cp = NULL;
2204 register int i; 2336 register int i;
2205 2337
2206 bzero (sa, len); 2338 bzero (sa, len);
2207 sa->sa_family = family; 2339 sa->sa_family = family;
2208 2340
2215 len = sizeof (sin->sin_addr) + 1; 2347 len = sizeof (sin->sin_addr) + 1;
2216 i = XINT (p->contents[--len]); 2348 i = XINT (p->contents[--len]);
2217 sin->sin_port = htons (i); 2349 sin->sin_port = htons (i);
2218 cp = (unsigned char *)&sin->sin_addr; 2350 cp = (unsigned char *)&sin->sin_addr;
2219 } 2351 }
2352 #ifdef AF_INET6
2353 else if (family == AF_INET6)
2354 {
2355 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
2356 uint16_t *ip6 = (uint16_t *)&sin6->sin6_addr;
2357 len = sizeof (sin6->sin6_addr) + 1;
2358 i = XINT (p->contents[--len]);
2359 sin6->sin6_port = htons (i);
2360 for (i = 0; i < len; i++)
2361 if (INTEGERP (p->contents[i]))
2362 {
2363 int j = XFASTINT (p->contents[i]) & 0xffff;
2364 ip6[i] = ntohs (j);
2365 }
2366 return;
2367 }
2368 #endif
2220 } 2369 }
2221 else if (STRINGP (address)) 2370 else if (STRINGP (address))
2222 { 2371 {
2223 #ifdef HAVE_LOCAL_SOCKETS 2372 #ifdef HAVE_LOCAL_SOCKETS
2224 if (family == AF_LOCAL) 2373 if (family == AF_LOCAL)
2289 2438
2290 static struct socket_options { 2439 static struct socket_options {
2291 /* The name of this option. Should be lowercase version of option 2440 /* The name of this option. Should be lowercase version of option
2292 name without SO_ prefix. */ 2441 name without SO_ prefix. */
2293 char *name; 2442 char *name;
2294 /* Length of name. */
2295 int nlen;
2296 /* Option level SOL_... */ 2443 /* Option level SOL_... */
2297 int optlevel; 2444 int optlevel;
2298 /* Option number SO_... */ 2445 /* Option number SO_... */
2299 int optnum; 2446 int optnum;
2300 enum { SOPT_UNKNOWN, SOPT_BOOL, SOPT_INT, SOPT_STR, SOPT_LINGER } opttype; 2447 enum { SOPT_UNKNOWN, SOPT_BOOL, SOPT_INT, SOPT_IFNAME, SOPT_LINGER } opttype;
2448 enum { OPIX_NONE=0, OPIX_MISC=1, OPIX_REUSEADDR=2 } optbit;
2301 } socket_options[] = 2449 } socket_options[] =
2302 { 2450 {
2303 #ifdef SO_BINDTODEVICE 2451 #ifdef SO_BINDTODEVICE
2304 { "bindtodevice", 12, SOL_SOCKET, SO_BINDTODEVICE, SOPT_STR }, 2452 { ":bindtodevice", SOL_SOCKET, SO_BINDTODEVICE, SOPT_IFNAME, OPIX_MISC },
2305 #endif 2453 #endif
2306 #ifdef SO_BROADCAST 2454 #ifdef SO_BROADCAST
2307 { "broadcast", 9, SOL_SOCKET, SO_BROADCAST, SOPT_BOOL }, 2455 { ":broadcast", SOL_SOCKET, SO_BROADCAST, SOPT_BOOL, OPIX_MISC },
2308 #endif 2456 #endif
2309 #ifdef SO_DONTROUTE 2457 #ifdef SO_DONTROUTE
2310 { "dontroute", 9, SOL_SOCKET, SO_DONTROUTE, SOPT_BOOL }, 2458 { ":dontroute", SOL_SOCKET, SO_DONTROUTE, SOPT_BOOL, OPIX_MISC },
2311 #endif 2459 #endif
2312 #ifdef SO_KEEPALIVE 2460 #ifdef SO_KEEPALIVE
2313 { "keepalive", 9, SOL_SOCKET, SO_KEEPALIVE, SOPT_BOOL }, 2461 { ":keepalive", SOL_SOCKET, SO_KEEPALIVE, SOPT_BOOL, OPIX_MISC },
2314 #endif 2462 #endif
2315 #ifdef SO_LINGER 2463 #ifdef SO_LINGER
2316 { "linger", 6, SOL_SOCKET, SO_LINGER, SOPT_LINGER }, 2464 { ":linger", SOL_SOCKET, SO_LINGER, SOPT_LINGER, OPIX_MISC },
2317 #endif 2465 #endif
2318 #ifdef SO_OOBINLINE 2466 #ifdef SO_OOBINLINE
2319 { "oobinline", 9, SOL_SOCKET, SO_OOBINLINE, SOPT_BOOL }, 2467 { ":oobinline", SOL_SOCKET, SO_OOBINLINE, SOPT_BOOL, OPIX_MISC },
2320 #endif 2468 #endif
2321 #ifdef SO_PRIORITY 2469 #ifdef SO_PRIORITY
2322 { "priority", 8, SOL_SOCKET, SO_PRIORITY, SOPT_INT }, 2470 { ":priority", SOL_SOCKET, SO_PRIORITY, SOPT_INT, OPIX_MISC },
2323 #endif 2471 #endif
2324 #ifdef SO_REUSEADDR 2472 #ifdef SO_REUSEADDR
2325 { "reuseaddr", 9, SOL_SOCKET, SO_REUSEADDR, SOPT_BOOL }, 2473 { ":reuseaddr", SOL_SOCKET, SO_REUSEADDR, SOPT_BOOL, OPIX_REUSEADDR },
2326 #endif 2474 #endif
2327 { 0, 0, 0, 0, SOPT_UNKNOWN } 2475 { 0, 0, 0, SOPT_UNKNOWN, OPIX_NONE }
2328 }; 2476 };
2329 2477
2330 /* Process list of socket options OPTS on socket S. 2478 /* Set option OPT to value VAL on socket S.
2331 Only check if options are supported is S < 0. 2479
2332 If NO_ERROR is non-zero, continue silently if an option 2480 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2333 cannot be set. 2481 Signals an error if setting a known option fails.
2334 2482 */
2335 Each element specifies one option. An element is either a string
2336 "OPTION=VALUE" or a cons (OPTION . VALUE) where OPTION is a string
2337 or a symbol. */
2338 2483
2339 static int 2484 static int
2340 set_socket_options (s, opts, no_error) 2485 set_socket_option (s, opt, val)
2341 int s; 2486 int s;
2342 Lisp_Object opts; 2487 Lisp_Object opt, val;
2343 int no_error; 2488 {
2344 { 2489 char *name;
2345 if (!CONSP (opts)) 2490 struct socket_options *sopt;
2346 opts = Fcons (opts, Qnil); 2491 int ret = 0;
2347 2492
2348 while (CONSP (opts)) 2493 CHECK_SYMBOL (opt);
2349 { 2494
2350 Lisp_Object opt; 2495 name = (char *) SDATA (SYMBOL_NAME (opt));
2351 Lisp_Object val; 2496 for (sopt = socket_options; sopt->name; sopt++)
2352 char *name, *arg; 2497 if (strcmp (name, sopt->name) == 0)
2353 struct socket_options *sopt; 2498 break;
2354 int ret = 0; 2499
2355 2500 switch (sopt->opttype)
2356 opt = XCAR (opts); 2501 {
2357 opts = XCDR (opts); 2502 case SOPT_BOOL:
2358 2503 {
2359 name = 0; 2504 int optval;
2360 val = Qt; 2505 optval = NILP (val) ? 0 : 1;
2361 if (CONSP (opt)) 2506 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2362 { 2507 &optval, sizeof (optval));
2363 val = XCDR (opt); 2508 break;
2364 opt = XCAR (opt);
2365 }
2366 if (STRINGP (opt))
2367 name = (char *) SDATA (opt);
2368 else if (SYMBOLP (opt))
2369 name = (char *) SDATA (SYMBOL_NAME (opt));
2370 else {
2371 error ("Mal-formed option list");
2372 return 0;
2373 } 2509 }
2374 2510
2375 if (strncmp (name, "no", 2) == 0) 2511 case SOPT_INT:
2376 { 2512 {
2377 val = Qnil; 2513 int optval;
2378 name += 2; 2514 if (INTEGERP (val))
2379 } 2515 optval = XINT (val);
2380 2516 else
2381 arg = 0; 2517 error ("Bad option value for %s", name);
2382 for (sopt = socket_options; sopt->name; sopt++) 2518 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2383 if (strncmp (name, sopt->name, sopt->nlen) == 0) 2519 &optval, sizeof (optval));
2520 break;
2521 }
2522
2523 #ifdef SO_BINDTODEVICE
2524 case SOPT_IFNAME:
2525 {
2526 char devname[IFNAMSIZ+1];
2527
2528 /* This is broken, at least in the Linux 2.4 kernel.
2529 To unbind, the arg must be a zero integer, not the empty string.
2530 This should work on all systems. KFS. 2003-09-23. */
2531 bzero (devname, sizeof devname);
2532 if (STRINGP (val))
2384 { 2533 {
2385 if (name[sopt->nlen] == 0) 2534 char *arg = (char *) SDATA (val);
2386 break; 2535 int len = min (strlen (arg), IFNAMSIZ);
2387 if (name[sopt->nlen] == '=') 2536 bcopy (arg, devname, len);
2388 {
2389 arg = name + sopt->nlen + 1;
2390 break;
2391 }
2392 } 2537 }
2393 2538 else if (!NILP (val))
2394 switch (sopt->opttype) 2539 error ("Bad option value for %s", name);
2395 { 2540 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2396 case SOPT_BOOL: 2541 devname, IFNAMSIZ);
2397 { 2542 break;
2398 int optval; 2543 }
2399 if (s < 0) 2544 #endif
2400 return 1;
2401 if (arg)
2402 optval = (*arg == '0' || *arg == 'n') ? 0 : 1;
2403 else if (INTEGERP (val))
2404 optval = XINT (val) == 0 ? 0 : 1;
2405 else
2406 optval = NILP (val) ? 0 : 1;
2407 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2408 &optval, sizeof (optval));
2409 break;
2410 }
2411
2412 case SOPT_INT:
2413 {
2414 int optval;
2415 if (arg)
2416 optval = atoi(arg);
2417 else if (INTEGERP (val))
2418 optval = XINT (val);
2419 else
2420 error ("Bad option argument for %s", name);
2421 if (s < 0)
2422 return 1;
2423 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2424 &optval, sizeof (optval));
2425 break;
2426 }
2427
2428 case SOPT_STR:
2429 {
2430 if (!arg)
2431 {
2432 if (NILP (val))
2433 arg = "";
2434 else if (STRINGP (val))
2435 arg = (char *) SDATA (val);
2436 else if (XSYMBOL (val))
2437 arg = (char *) SDATA (SYMBOL_NAME (val));
2438 else
2439 error ("Invalid argument to %s option", name);
2440 }
2441 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2442 arg, strlen (arg));
2443 }
2444 2545
2445 #ifdef SO_LINGER 2546 #ifdef SO_LINGER
2446 case SOPT_LINGER: 2547 case SOPT_LINGER:
2447 { 2548 {
2448 struct linger linger; 2549 struct linger linger;
2449 2550
2450 linger.l_onoff = 1; 2551 linger.l_onoff = 1;
2451 linger.l_linger = 0; 2552 linger.l_linger = 0;
2452 2553 if (INTEGERP (val))
2453 if (s < 0) 2554 linger.l_linger = XINT (val);
2454 return 1; 2555 else
2455 2556 linger.l_onoff = NILP (val) ? 0 : 1;
2456 if (arg) 2557 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2457 { 2558 &linger, sizeof (linger));
2458 if (*arg == 'n' || *arg == 't' || *arg == 'y') 2559 break;
2459 linger.l_onoff = (*arg == 'n') ? 0 : 1; 2560 }
2460 else 2561 #endif
2461 linger.l_linger = atoi(arg); 2562
2462 } 2563 default:
2463 else if (INTEGERP (val)) 2564 return 0;
2464 linger.l_linger = XINT (val); 2565 }
2465 else 2566
2466 linger.l_onoff = NILP (val) ? 0 : 1; 2567 if (ret < 0)
2467 ret = setsockopt (s, sopt->optlevel, sopt->optnum, 2568 report_file_error ("Cannot set network option",
2468 &linger, sizeof (linger)); 2569 Fcons (opt, Fcons (val, Qnil)));
2469 break; 2570 return (1 << sopt->optbit);
2470 } 2571 }
2471 #endif 2572
2472 default: 2573
2473 if (s < 0) 2574 DEFUN ("set-network-process-option",
2474 return 0; 2575 Fset_network_process_option, Sset_network_process_option,
2475 if (no_error) 2576 3, 4, 0,
2476 continue; 2577 doc: /* For network process PROCESS set option OPTION to value VALUE.
2477 error ("Unsupported option: %s", name); 2578 See `make-network-process' for a list of options and values.
2478 } 2579 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2479 if (ret < 0 && ! no_error) 2580 OPTION is not a supported option, return nil instead; otherwise return t. */)
2480 report_file_error ("Cannot set network option: %s", opt); 2581 (process, option, value, no_error)
2481 } 2582 Lisp_Object process, option, value;
2482 return 1; 2583 Lisp_Object no_error;
2483 } 2584 {
2484 2585 int s;
2485 DEFUN ("set-network-process-options", 2586 struct Lisp_Process *p;
2486 Fset_network_process_options, Sset_network_process_options, 2587
2487 1, MANY, 0,
2488 doc: /* Set one or more options for network process PROCESS.
2489 Each option is either a string "OPT=VALUE" or a cons (OPT . VALUE).
2490 A boolean value is false if it either zero or nil, true otherwise.
2491
2492 The following options are known. Consult the relevant system manual
2493 pages for more information.
2494
2495 bindtodevice=NAME -- bind to interface NAME, or remove binding if nil.
2496 broadcast=BOOL -- Allow send and receive of datagram broadcasts.
2497 dontroute=BOOL -- Only send to directly connected hosts.
2498 keepalive=BOOL -- Send keep-alive messages on network stream.
2499 linger=BOOL or TIMEOUT -- Send queued messages before closing.
2500 oobinline=BOOL -- Place out-of-band data in receive data stream.
2501 priority=INT -- Set protocol defined priority for sent packets.
2502 reuseaddr=BOOL -- Allow reusing a recently used address.
2503
2504 usage: (set-network-process-options PROCESS &rest OPTIONS) */)
2505 (nargs, args)
2506 int nargs;
2507 Lisp_Object *args;
2508 {
2509 Lisp_Object process;
2510 Lisp_Object opts;
2511
2512 process = args[0];
2513 CHECK_PROCESS (process); 2588 CHECK_PROCESS (process);
2514 if (nargs > 1 && XINT (XPROCESS (process)->infd) >= 0) 2589 p = XPROCESS (process);
2515 { 2590 if (!NETCONN1_P (p))
2516 opts = Flist (nargs, args); 2591 error ("Process is not a network process");
2517 set_socket_options (XINT (XPROCESS (process)->infd), opts, 0); 2592
2518 } 2593 s = XINT (p->infd);
2519 return process; 2594 if (s < 0)
2520 } 2595 error ("Process is not running");
2596
2597 if (set_socket_option (s, option, value))
2598 {
2599 p->childp = Fplist_put (p->childp, option, value);
2600 return Qt;
2601 }
2602
2603 if (NILP (no_error))
2604 error ("Unknown or unsupported option");
2605
2606 return Qnil;
2607 }
2608
2521 2609
2522 /* A version of request_sigio suitable for a record_unwind_protect. */ 2610 /* A version of request_sigio suitable for a record_unwind_protect. */
2523 2611
2524 Lisp_Object 2612 static Lisp_Object
2525 unwind_request_sigio (dummy) 2613 unwind_request_sigio (dummy)
2526 Lisp_Object dummy; 2614 Lisp_Object dummy;
2527 { 2615 {
2528 if (interrupt_input) 2616 if (interrupt_input)
2529 request_sigio (); 2617 request_sigio ();
2541 doc: /* Create and return a network server or client process. 2629 doc: /* Create and return a network server or client process.
2542 2630
2543 In Emacs, network connections are represented by process objects, so 2631 In Emacs, network connections are represented by process objects, so
2544 input and output work as for subprocesses and `delete-process' closes 2632 input and output work as for subprocesses and `delete-process' closes
2545 a network connection. However, a network process has no process id, 2633 a network connection. However, a network process has no process id,
2546 it cannot be signalled, and the status codes are different from normal 2634 it cannot be signaled, and the status codes are different from normal
2547 processes. 2635 processes.
2548 2636
2549 Arguments are specified as keyword/argument pairs. The following 2637 Arguments are specified as keyword/argument pairs. The following
2550 arguments are defined: 2638 arguments are defined:
2551 2639
2569 2657
2570 :type TYPE -- TYPE is the type of connection. The default (nil) is a 2658 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2571 stream type connection, `datagram' creates a datagram type connection. 2659 stream type connection, `datagram' creates a datagram type connection.
2572 2660
2573 :family FAMILY -- FAMILY is the address (and protocol) family for the 2661 :family FAMILY -- FAMILY is the address (and protocol) family for the
2574 service specified by HOST and SERVICE. The default address family is 2662 service specified by HOST and SERVICE. The default (nil) is to use
2575 Inet (or IPv4) for the host and port number specified by HOST and 2663 whatever address family (IPv4 or IPv6) that is defined for the host
2576 SERVICE. Other address families supported are: 2664 and port number specified by HOST and SERVICE. Other address families
2665 supported are:
2577 local -- for a local (i.e. UNIX) address specified by SERVICE. 2666 local -- for a local (i.e. UNIX) address specified by SERVICE.
2667 ipv4 -- use IPv4 address family only.
2668 ipv6 -- use IPv6 address family only.
2578 2669
2579 :local ADDRESS -- ADDRESS is the local address used for the connection. 2670 :local ADDRESS -- ADDRESS is the local address used for the connection.
2580 This parameter is ignored when opening a client process. When specified 2671 This parameter is ignored when opening a client process. When specified
2581 for a server process, the FAMILY, HOST and SERVICE args are ignored. 2672 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2582 2673
2595 where F is the family number and AV is a vector containing the socket 2686 where F is the family number and AV is a vector containing the socket
2596 address data with one element per address data byte. Do not rely on 2687 address data with one element per address data byte. Do not rely on
2597 this format in portable code, as it may depend on implementation 2688 this format in portable code, as it may depend on implementation
2598 defined constants, data sizes, and data structure alignment. 2689 defined constants, data sizes, and data structure alignment.
2599 2690
2600 :coding CODING -- CODING is coding system for this process. 2691 :coding CODING -- If CODING is a symbol, it specifies the coding
2601 2692 system used for both reading and writing for this process. If CODING
2602 :options OPTIONS -- Set the specified options for the network process. 2693 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2603 See `set-network-process-options' for details. 2694 ENCODING is used for writing.
2604 2695
2605 :nowait BOOL -- If BOOL is non-nil for a stream type client process, 2696 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2606 return without waiting for the connection to complete; instead, the 2697 return without waiting for the connection to complete; instead, the
2607 sentinel function will be called with second arg matching "open" (if 2698 sentinel function will be called with second arg matching "open" (if
2608 successful) or "failed" when the connect completes. Default is to use 2699 successful) or "failed" when the connect completes. Default is to use
2609 a blocking connect (i.e. wait) for stream type connections. 2700 a blocking connect (i.e. wait) for stream type connections.
2610 2701
2611 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is 2702 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2612 running when emacs is exited. 2703 running when Emacs is exited.
2613 2704
2614 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil. 2705 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2615 In the stopped state, a server process does not accept new 2706 In the stopped state, a server process does not accept new
2616 connections, and a client process does not handle incoming traffic. 2707 connections, and a client process does not handle incoming traffic.
2617 The stopped state is cleared by `continue-process' and set by 2708 The stopped state is cleared by `continue-process' and set by
2632 is the server process, CLIENT is the new process for the connection, 2723 is the server process, CLIENT is the new process for the connection,
2633 and MESSAGE is a string. 2724 and MESSAGE is a string.
2634 2725
2635 :plist PLIST -- Install PLIST as the new process' initial plist. 2726 :plist PLIST -- Install PLIST as the new process' initial plist.
2636 2727
2637 :server BOOL -- if BOOL is non-nil, create a server process for the 2728 :server QLEN -- if QLEN is non-nil, create a server process for the
2638 specified FAMILY, SERVICE, and connection type (stream or datagram). 2729 specified FAMILY, SERVICE, and connection type (stream or datagram).
2639 Default is a client process. 2730 If QLEN is an integer, it is used as the max. length of the server's
2640 2731 pending connection queue (also known as the backlog); the default
2641 A server process will listen for and accept connections from 2732 queue length is 5. Default is to create a client process.
2642 clients. When a client connection is accepted, a new network process 2733
2643 is created for the connection with the following parameters: 2734 The following network options can be specified for this connection:
2735
2736 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2737 :dontroute BOOL -- Only send to directly connected hosts.
2738 :keepalive BOOL -- Send keep-alive messages on network stream.
2739 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2740 :oobinline BOOL -- Place out-of-band data in receive data stream.
2741 :priority INT -- Set protocol defined priority for sent packets.
2742 :reuseaddr BOOL -- Allow reusing a recently used local address
2743 (this is allowed by default for a server process).
2744 :bindtodevice NAME -- bind to interface NAME. Using this may require
2745 special privileges on some systems.
2746
2747 Consult the relevant system programmer's manual pages for more
2748 information on using these options.
2749
2750
2751 A server process will listen for and accept connections from clients.
2752 When a client connection is accepted, a new network process is created
2753 for the connection with the following parameters:
2754
2644 - The client's process name is constructed by concatenating the server 2755 - The client's process name is constructed by concatenating the server
2645 process' NAME and a client identification string. 2756 process' NAME and a client identification string.
2646 - If the FILTER argument is non-nil, the client process will not get a 2757 - If the FILTER argument is non-nil, the client process will not get a
2647 separate process buffer; otherwise, the client's process buffer is a newly 2758 separate process buffer; otherwise, the client's process buffer is a newly
2648 created buffer named after the server process' BUFFER name or process 2759 created buffer named after the server process' BUFFER name or process
2669 Lisp_Object proc; 2780 Lisp_Object proc;
2670 Lisp_Object contact; 2781 Lisp_Object contact;
2671 struct Lisp_Process *p; 2782 struct Lisp_Process *p;
2672 #ifdef HAVE_GETADDRINFO 2783 #ifdef HAVE_GETADDRINFO
2673 struct addrinfo ai, *res, *lres; 2784 struct addrinfo ai, *res, *lres;
2674 struct addrinfo hints; 2785 struct addrinfo hints;
2675 char *portstring, portbuf[128]; 2786 char *portstring, portbuf[128];
2676 #else /* HAVE_GETADDRINFO */ 2787 #else /* HAVE_GETADDRINFO */
2677 struct _emacs_addrinfo 2788 struct _emacs_addrinfo
2678 { 2789 {
2679 int ai_family; 2790 int ai_family;
2680 int ai_socktype; 2791 int ai_socktype;
2691 int port; 2802 int port;
2692 int ret = 0; 2803 int ret = 0;
2693 int xerrno = 0; 2804 int xerrno = 0;
2694 int s = -1, outch, inch; 2805 int s = -1, outch, inch;
2695 struct gcpro gcpro1; 2806 struct gcpro gcpro1;
2696 int retry = 0;
2697 int count = SPECPDL_INDEX (); 2807 int count = SPECPDL_INDEX ();
2698 int count1; 2808 int count1;
2699 Lisp_Object QCaddress; /* one of QClocal or QCremote */ 2809 Lisp_Object QCaddress; /* one of QClocal or QCremote */
2700 Lisp_Object tem; 2810 Lisp_Object tem;
2701 Lisp_Object name, buffer, host, service, address; 2811 Lisp_Object name, buffer, host, service, address;
2702 Lisp_Object filter, sentinel; 2812 Lisp_Object filter, sentinel;
2703 int is_non_blocking_client = 0; 2813 int is_non_blocking_client = 0;
2704 int is_server = 0; 2814 int is_server = 0, backlog = 5;
2705 int socktype; 2815 int socktype;
2706 int family = -1; 2816 int family = -1;
2707 2817
2708 if (nargs == 0) 2818 if (nargs == 0)
2709 return Qnil; 2819 return Qnil;
2736 not available, since a blocked Emacs is not useful. */ 2846 not available, since a blocked Emacs is not useful. */
2737 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY)) 2847 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY))
2738 error ("Network servers not supported"); 2848 error ("Network servers not supported");
2739 #else 2849 #else
2740 is_server = 1; 2850 is_server = 1;
2851 if (INTEGERP (tem))
2852 backlog = XINT (tem);
2741 #endif 2853 #endif
2742 } 2854 }
2743 2855
2744 /* Make QCaddress an alias for :local (server) or :remote (client). */ 2856 /* Make QCaddress an alias for :local (server) or :remote (client). */
2745 QCaddress = is_server ? QClocal : QCremote; 2857 QCaddress = is_server ? QClocal : QCremote;
2808 goto open_socket; 2920 goto open_socket;
2809 } 2921 }
2810 2922
2811 /* :family FAMILY -- nil (for Inet), local, or integer. */ 2923 /* :family FAMILY -- nil (for Inet), local, or integer. */
2812 tem = Fplist_get (contact, QCfamily); 2924 tem = Fplist_get (contact, QCfamily);
2813 if (INTEGERP (tem)) 2925 if (NILP (tem))
2926 {
2927 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
2928 family = AF_UNSPEC;
2929 #else
2930 family = AF_INET;
2931 #endif
2932 }
2933 #ifdef HAVE_LOCAL_SOCKETS
2934 else if (EQ (tem, Qlocal))
2935 family = AF_LOCAL;
2936 #endif
2937 #ifdef AF_INET6
2938 else if (EQ (tem, Qipv6))
2939 family = AF_INET6;
2940 #endif
2941 else if (EQ (tem, Qipv4))
2942 family = AF_INET;
2943 else if (INTEGERP (tem))
2814 family = XINT (tem); 2944 family = XINT (tem);
2815 else 2945 else
2816 {
2817 if (NILP (tem))
2818 family = AF_INET;
2819 #ifdef HAVE_LOCAL_SOCKETS
2820 else if (EQ (tem, Qlocal))
2821 family = AF_LOCAL;
2822 #endif
2823 }
2824 if (family < 0)
2825 error ("Unknown address family"); 2946 error ("Unknown address family");
2947
2826 ai.ai_family = family; 2948 ai.ai_family = family;
2827 2949
2828 /* :service SERVICE -- string, integer (port number), or t (random port). */ 2950 /* :service SERVICE -- string, integer (port number), or t (random port). */
2829 service = Fplist_get (contact, QCservice); 2951 service = Fplist_get (contact, QCservice);
2830 2952
2886 3008
2887 immediate_quit = 1; 3009 immediate_quit = 1;
2888 QUIT; 3010 QUIT;
2889 memset (&hints, 0, sizeof (hints)); 3011 memset (&hints, 0, sizeof (hints));
2890 hints.ai_flags = 0; 3012 hints.ai_flags = 0;
2891 hints.ai_family = NILP (Fplist_member (contact, QCfamily)) ? AF_UNSPEC : family; 3013 hints.ai_family = family;
2892 hints.ai_socktype = socktype; 3014 hints.ai_socktype = socktype;
2893 hints.ai_protocol = 0; 3015 hints.ai_protocol = 0;
2894 ret = getaddrinfo (SDATA (host), portstring, &hints, &res); 3016 ret = getaddrinfo (SDATA (host), portstring, &hints, &res);
2895 if (ret) 3017 if (ret)
2896 #ifdef HAVE_GAI_STRERROR 3018 #ifdef HAVE_GAI_STRERROR
2931 if (!NILP (host)) 3053 if (!NILP (host))
2932 { 3054 {
2933 struct hostent *host_info_ptr; 3055 struct hostent *host_info_ptr;
2934 3056
2935 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that, 3057 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
2936 as it may `hang' emacs for a very long time. */ 3058 as it may `hang' Emacs for a very long time. */
2937 immediate_quit = 1; 3059 immediate_quit = 1;
2938 QUIT; 3060 QUIT;
2939 host_info_ptr = gethostbyname (SDATA (host)); 3061 host_info_ptr = gethostbyname (SDATA (host));
2940 immediate_quit = 0; 3062 immediate_quit = 0;
2941 3063
2988 count1 = SPECPDL_INDEX (); 3110 count1 = SPECPDL_INDEX ();
2989 s = -1; 3111 s = -1;
2990 3112
2991 for (lres = res; lres; lres = lres->ai_next) 3113 for (lres = res; lres; lres = lres->ai_next)
2992 { 3114 {
3115 int optn, optbits;
3116
3117 retry_connect:
3118
2993 s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol); 3119 s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol);
2994 if (s < 0) 3120 if (s < 0)
2995 { 3121 {
2996 xerrno = errno; 3122 xerrno = errno;
2997 continue; 3123 continue;
3021 #endif 3147 #endif
3022 3148
3023 /* Make us close S if quit. */ 3149 /* Make us close S if quit. */
3024 record_unwind_protect (close_file_unwind, make_number (s)); 3150 record_unwind_protect (close_file_unwind, make_number (s));
3025 3151
3152 /* Parse network options in the arg list.
3153 We simply ignore anything which isn't a known option (including other keywords).
3154 An error is signalled if setting a known option fails. */
3155 for (optn = optbits = 0; optn < nargs-1; optn += 2)
3156 optbits |= set_socket_option (s, args[optn], args[optn+1]);
3157
3026 if (is_server) 3158 if (is_server)
3027 { 3159 {
3028 /* Configure as a server socket. */ 3160 /* Configure as a server socket. */
3161
3162 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3163 explicit :reuseaddr key to override this. */
3029 #ifdef HAVE_LOCAL_SOCKETS 3164 #ifdef HAVE_LOCAL_SOCKETS
3030 if (family != AF_LOCAL) 3165 if (family != AF_LOCAL)
3031 #endif 3166 #endif
3032 { 3167 if (!(optbits & (1 << OPIX_REUSEADDR)))
3033 int optval = 1; 3168 {
3034 if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)) 3169 int optval = 1;
3035 report_file_error ("Cannot set reuse option on server socket.", Qnil); 3170 if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval))
3036 } 3171 report_file_error ("Cannot set reuse option on server socket", Qnil);
3172 }
3037 3173
3038 if (bind (s, lres->ai_addr, lres->ai_addrlen)) 3174 if (bind (s, lres->ai_addr, lres->ai_addrlen))
3039 report_file_error ("Cannot bind server socket", Qnil); 3175 report_file_error ("Cannot bind server socket", Qnil);
3040 3176
3041 #ifdef HAVE_GETSOCKNAME 3177 #ifdef HAVE_GETSOCKNAME
3050 contact = Fplist_put (contact, QCservice, service); 3186 contact = Fplist_put (contact, QCservice, service);
3051 } 3187 }
3052 } 3188 }
3053 #endif 3189 #endif
3054 3190
3055 if (socktype == SOCK_STREAM && listen (s, 5)) 3191 if (socktype == SOCK_STREAM && listen (s, backlog))
3056 report_file_error ("Cannot listen on server socket", Qnil); 3192 report_file_error ("Cannot listen on server socket", Qnil);
3057 3193
3058 break; 3194 break;
3059 } 3195 }
3060
3061 retry_connect:
3062 3196
3063 immediate_quit = 1; 3197 immediate_quit = 1;
3064 QUIT; 3198 QUIT;
3065 3199
3066 /* This turns off all alarm-based interrupts; the 3200 /* This turns off all alarm-based interrupts; the
3099 #endif 3233 #endif
3100 #endif 3234 #endif
3101 3235
3102 immediate_quit = 0; 3236 immediate_quit = 0;
3103 3237
3104 if (xerrno == EINTR)
3105 goto retry_connect;
3106 if (xerrno == EADDRINUSE && retry < 20)
3107 {
3108 /* A delay here is needed on some FreeBSD systems,
3109 and it is harmless, since this retrying takes time anyway
3110 and should be infrequent. */
3111 Fsleep_for (make_number (1), Qnil);
3112 retry++;
3113 goto retry_connect;
3114 }
3115
3116 /* Discard the unwind protect closing S. */ 3238 /* Discard the unwind protect closing S. */
3117 specpdl_ptr = specpdl + count1; 3239 specpdl_ptr = specpdl + count1;
3118 emacs_close (s); 3240 emacs_close (s);
3119 s = -1; 3241 s = -1;
3242
3243 if (xerrno == EINTR)
3244 goto retry_connect;
3120 } 3245 }
3121 3246
3122 if (s >= 0) 3247 if (s >= 0)
3123 { 3248 {
3124 #ifdef DATAGRAM_SOCKETS 3249 #ifdef DATAGRAM_SOCKETS
3186 report_file_error ("make server process failed", contact); 3311 report_file_error ("make server process failed", contact);
3187 else 3312 else
3188 report_file_error ("make client process failed", contact); 3313 report_file_error ("make client process failed", contact);
3189 } 3314 }
3190 3315
3191 tem = Fplist_get (contact, QCoptions);
3192 if (!NILP (tem))
3193 set_socket_options (s, tem, 1);
3194
3195 #endif /* not TERM */ 3316 #endif /* not TERM */
3196 3317
3197 inch = s; 3318 inch = s;
3198 outch = s; 3319 outch = s;
3199 3320
3271 /* Qt denotes we have not yet called Ffind_operation_coding_system. */ 3392 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3272 Lisp_Object coding_systems = Qt; 3393 Lisp_Object coding_systems = Qt;
3273 Lisp_Object args[5], val; 3394 Lisp_Object args[5], val;
3274 3395
3275 if (!NILP (tem)) 3396 if (!NILP (tem))
3276 val = XCAR (XCDR (tem)); 3397 {
3398 val = XCAR (XCDR (tem));
3399 if (CONSP (val))
3400 val = XCAR (val);
3401 }
3277 else if (!NILP (Vcoding_system_for_read)) 3402 else if (!NILP (Vcoding_system_for_read))
3278 val = Vcoding_system_for_read; 3403 val = Vcoding_system_for_read;
3279 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters)) 3404 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
3280 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters))) 3405 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
3281 /* We dare not decode end-of-line format by setting VAL to 3406 /* We dare not decode end-of-line format by setting VAL to
3303 val = Qnil; 3428 val = Qnil;
3304 } 3429 }
3305 p->decode_coding_system = val; 3430 p->decode_coding_system = val;
3306 3431
3307 if (!NILP (tem)) 3432 if (!NILP (tem))
3308 val = XCAR (XCDR (tem)); 3433 {
3434 val = XCAR (XCDR (tem));
3435 if (CONSP (val))
3436 val = XCDR (val);
3437 }
3309 else if (!NILP (Vcoding_system_for_write)) 3438 else if (!NILP (Vcoding_system_for_write))
3310 val = Vcoding_system_for_write; 3439 val = Vcoding_system_for_write;
3311 else if (NILP (current_buffer->enable_multibyte_characters)) 3440 else if (NILP (current_buffer->enable_multibyte_characters))
3312 val = Qnil; 3441 val = Qnil;
3313 else 3442 else
3348 UNGCPRO; 3477 UNGCPRO;
3349 return proc; 3478 return proc;
3350 } 3479 }
3351 #endif /* HAVE_SOCKETS */ 3480 #endif /* HAVE_SOCKETS */
3352 3481
3482
3483 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3484
3485 #ifdef SIOCGIFCONF
3486 DEFUN ("network-interface-list", Fnetwork_interface_list, Snetwork_interface_list, 0, 0, 0,
3487 doc: /* Return an alist of all network interfaces and their network address.
3488 Each element is a cons, the car of which is a string containing the
3489 interface name, and the cdr is the network address in internal
3490 format; see the description of ADDRESS in `make-network-process'. */)
3491 ()
3492 {
3493 struct ifconf ifconf;
3494 struct ifreq *ifreqs = NULL;
3495 int ifaces = 0;
3496 int buf_size, s;
3497 Lisp_Object res;
3498
3499 s = socket (AF_INET, SOCK_STREAM, 0);
3500 if (s < 0)
3501 return Qnil;
3502
3503 again:
3504 ifaces += 25;
3505 buf_size = ifaces * sizeof(ifreqs[0]);
3506 ifreqs = (struct ifreq *)xrealloc(ifreqs, buf_size);
3507 if (!ifreqs)
3508 {
3509 close (s);
3510 return Qnil;
3511 }
3512
3513 ifconf.ifc_len = buf_size;
3514 ifconf.ifc_req = ifreqs;
3515 if (ioctl (s, SIOCGIFCONF, &ifconf))
3516 {
3517 close (s);
3518 return Qnil;
3519 }
3520
3521 if (ifconf.ifc_len == buf_size)
3522 goto again;
3523
3524 close (s);
3525 ifaces = ifconf.ifc_len / sizeof (ifreqs[0]);
3526
3527 res = Qnil;
3528 while (--ifaces >= 0)
3529 {
3530 struct ifreq *ifq = &ifreqs[ifaces];
3531 char namebuf[sizeof (ifq->ifr_name) + 1];
3532 if (ifq->ifr_addr.sa_family != AF_INET)
3533 continue;
3534 bcopy (ifq->ifr_name, namebuf, sizeof (ifq->ifr_name));
3535 namebuf[sizeof (ifq->ifr_name)] = 0;
3536 res = Fcons (Fcons (build_string (namebuf),
3537 conv_sockaddr_to_lisp (&ifq->ifr_addr,
3538 sizeof (struct sockaddr))),
3539 res);
3540 }
3541
3542 return res;
3543 }
3544 #endif /* SIOCGIFCONF */
3545
3546 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3547
3548 struct ifflag_def {
3549 int flag_bit;
3550 char *flag_sym;
3551 };
3552
3553 static struct ifflag_def ifflag_table[] = {
3554 #ifdef IFF_UP
3555 { IFF_UP, "up" },
3556 #endif
3557 #ifdef IFF_BROADCAST
3558 { IFF_BROADCAST, "broadcast" },
3559 #endif
3560 #ifdef IFF_DEBUG
3561 { IFF_DEBUG, "debug" },
3562 #endif
3563 #ifdef IFF_LOOPBACK
3564 { IFF_LOOPBACK, "loopback" },
3565 #endif
3566 #ifdef IFF_POINTOPOINT
3567 { IFF_POINTOPOINT, "pointopoint" },
3568 #endif
3569 #ifdef IFF_RUNNING
3570 { IFF_RUNNING, "running" },
3571 #endif
3572 #ifdef IFF_NOARP
3573 { IFF_NOARP, "noarp" },
3574 #endif
3575 #ifdef IFF_PROMISC
3576 { IFF_PROMISC, "promisc" },
3577 #endif
3578 #ifdef IFF_NOTRAILERS
3579 { IFF_NOTRAILERS, "notrailers" },
3580 #endif
3581 #ifdef IFF_ALLMULTI
3582 { IFF_ALLMULTI, "allmulti" },
3583 #endif
3584 #ifdef IFF_MASTER
3585 { IFF_MASTER, "master" },
3586 #endif
3587 #ifdef IFF_SLAVE
3588 { IFF_SLAVE, "slave" },
3589 #endif
3590 #ifdef IFF_MULTICAST
3591 { IFF_MULTICAST, "multicast" },
3592 #endif
3593 #ifdef IFF_PORTSEL
3594 { IFF_PORTSEL, "portsel" },
3595 #endif
3596 #ifdef IFF_AUTOMEDIA
3597 { IFF_AUTOMEDIA, "automedia" },
3598 #endif
3599 #ifdef IFF_DYNAMIC
3600 { IFF_DYNAMIC, "dynamic" },
3601 #endif
3602 #ifdef IFF_OACTIVE
3603 { IFF_OACTIVE, "oactive" }, /* OpenBSD: transmission in progress */
3604 #endif
3605 #ifdef IFF_SIMPLEX
3606 { IFF_SIMPLEX, "simplex" }, /* OpenBSD: can't hear own transmissions */
3607 #endif
3608 #ifdef IFF_LINK0
3609 { IFF_LINK0, "link0" }, /* OpenBSD: per link layer defined bit */
3610 #endif
3611 #ifdef IFF_LINK1
3612 { IFF_LINK1, "link1" }, /* OpenBSD: per link layer defined bit */
3613 #endif
3614 #ifdef IFF_LINK2
3615 { IFF_LINK2, "link2" }, /* OpenBSD: per link layer defined bit */
3616 #endif
3617 { 0, 0 }
3618 };
3619
3620 DEFUN ("network-interface-info", Fnetwork_interface_info, Snetwork_interface_info, 1, 1, 0,
3621 doc: /* Return information about network interface named IFNAME.
3622 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3623 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3624 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3625 FLAGS is the current flags of the interface. */)
3626 (ifname)
3627 Lisp_Object ifname;
3628 {
3629 struct ifreq rq;
3630 Lisp_Object res = Qnil;
3631 Lisp_Object elt;
3632 int s;
3633 int any = 0;
3634
3635 CHECK_STRING (ifname);
3636
3637 bzero (rq.ifr_name, sizeof rq.ifr_name);
3638 strncpy (rq.ifr_name, SDATA (ifname), sizeof (rq.ifr_name));
3639
3640 s = socket (AF_INET, SOCK_STREAM, 0);
3641 if (s < 0)
3642 return Qnil;
3643
3644 elt = Qnil;
3645 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3646 if (ioctl (s, SIOCGIFFLAGS, &rq) == 0)
3647 {
3648 int flags = rq.ifr_flags;
3649 struct ifflag_def *fp;
3650 int fnum;
3651
3652 any++;
3653 for (fp = ifflag_table; flags != 0 && fp->flag_sym; fp++)
3654 {
3655 if (flags & fp->flag_bit)
3656 {
3657 elt = Fcons (intern (fp->flag_sym), elt);
3658 flags -= fp->flag_bit;
3659 }
3660 }
3661 for (fnum = 0; flags && fnum < 32; fnum++)
3662 {
3663 if (flags & (1 << fnum))
3664 {
3665 elt = Fcons (make_number (fnum), elt);
3666 }
3667 }
3668 }
3669 #endif
3670 res = Fcons (elt, res);
3671
3672 elt = Qnil;
3673 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3674 if (ioctl (s, SIOCGIFHWADDR, &rq) == 0)
3675 {
3676 Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil);
3677 register struct Lisp_Vector *p = XVECTOR (hwaddr);
3678 int n;
3679
3680 any++;
3681 for (n = 0; n < 6; n++)
3682 p->contents[n] = make_number (((unsigned char *)&rq.ifr_hwaddr.sa_data[0])[n]);
3683 elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr);
3684 }
3685 #endif
3686 res = Fcons (elt, res);
3687
3688 elt = Qnil;
3689 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
3690 if (ioctl (s, SIOCGIFNETMASK, &rq) == 0)
3691 {
3692 any++;
3693 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
3694 elt = conv_sockaddr_to_lisp (&rq.ifr_netmask, sizeof (rq.ifr_netmask));
3695 #else
3696 elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
3697 #endif
3698 }
3699 #endif
3700 res = Fcons (elt, res);
3701
3702 elt = Qnil;
3703 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3704 if (ioctl (s, SIOCGIFBRDADDR, &rq) == 0)
3705 {
3706 any++;
3707 elt = conv_sockaddr_to_lisp (&rq.ifr_broadaddr, sizeof (rq.ifr_broadaddr));
3708 }
3709 #endif
3710 res = Fcons (elt, res);
3711
3712 elt = Qnil;
3713 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
3714 if (ioctl (s, SIOCGIFADDR, &rq) == 0)
3715 {
3716 any++;
3717 elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
3718 }
3719 #endif
3720 res = Fcons (elt, res);
3721
3722 close (s);
3723
3724 return any ? res : Qnil;
3725 }
3726 #endif
3727 #endif /* HAVE_SOCKETS */
3728
3729 /* Turn off input and output for process PROC. */
3730
3353 void 3731 void
3354 deactivate_process (proc) 3732 deactivate_process (proc)
3355 Lisp_Object proc; 3733 Lisp_Object proc;
3356 { 3734 {
3357 register int inchannel, outchannel; 3735 register int inchannel, outchannel;
3358 register struct Lisp_Process *p = XPROCESS (proc); 3736 register struct Lisp_Process *p = XPROCESS (proc);
3359 3737
3360 inchannel = XINT (p->infd); 3738 inchannel = XINT (p->infd);
3361 outchannel = XINT (p->outfd); 3739 outchannel = XINT (p->outfd);
3740
3741 #ifdef ADAPTIVE_READ_BUFFERING
3742 if (XINT (p->read_output_delay) > 0)
3743 {
3744 if (--process_output_delay_count < 0)
3745 process_output_delay_count = 0;
3746 XSETINT (p->read_output_delay, 0);
3747 p->read_output_skip = Qnil;
3748 }
3749 #endif
3362 3750
3363 if (inchannel >= 0) 3751 if (inchannel >= 0)
3364 { 3752 {
3365 /* Beware SIGCHLD hereabouts. */ 3753 /* Beware SIGCHLD hereabouts. */
3366 flush_pending_output (inchannel); 3754 flush_pending_output (inchannel);
3389 } 3777 }
3390 #endif 3778 #endif
3391 chan_process[inchannel] = Qnil; 3779 chan_process[inchannel] = Qnil;
3392 FD_CLR (inchannel, &input_wait_mask); 3780 FD_CLR (inchannel, &input_wait_mask);
3393 FD_CLR (inchannel, &non_keyboard_wait_mask); 3781 FD_CLR (inchannel, &non_keyboard_wait_mask);
3782 #ifdef NON_BLOCKING_CONNECT
3394 if (FD_ISSET (inchannel, &connect_wait_mask)) 3783 if (FD_ISSET (inchannel, &connect_wait_mask))
3395 { 3784 {
3396 FD_CLR (inchannel, &connect_wait_mask); 3785 FD_CLR (inchannel, &connect_wait_mask);
3397 if (--num_pending_connects < 0) 3786 if (--num_pending_connects < 0)
3398 abort (); 3787 abort ();
3399 } 3788 }
3789 #endif
3400 if (inchannel == max_process_desc) 3790 if (inchannel == max_process_desc)
3401 { 3791 {
3402 int i; 3792 int i;
3403 /* We just closed the highest-numbered process input descriptor, 3793 /* We just closed the highest-numbered process input descriptor,
3404 so recompute the highest-numbered one now. */ 3794 so recompute the highest-numbered one now. */
3435 } 3825 }
3436 #endif 3826 #endif
3437 } 3827 }
3438 3828
3439 DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output, 3829 DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
3440 0, 3, 0, 3830 0, 4, 0,
3441 doc: /* Allow any pending output from subprocesses to be read by Emacs. 3831 doc: /* Allow any pending output from subprocesses to be read by Emacs.
3442 It is read into the process' buffers or given to their filter functions. 3832 It is read into the process' buffers or given to their filter functions.
3443 Non-nil arg PROCESS means do not return until some output has been received 3833 Non-nil arg PROCESS means do not return until some output has been received
3444 from PROCESS. 3834 from PROCESS.
3445 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of 3835 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of
3446 seconds and microseconds to wait; return after that much time whether 3836 seconds and microseconds to wait; return after that much time whether
3447 or not there is input. 3837 or not there is input.
3838 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
3839 from PROCESS, suspending reading output from other processes.
3840 If JUST-THIS-ONE is an integer, don't run any timers either.
3448 Return non-nil iff we received any output before the timeout expired. */) 3841 Return non-nil iff we received any output before the timeout expired. */)
3449 (process, timeout, timeout_msecs) 3842 (process, timeout, timeout_msecs, just_this_one)
3450 register Lisp_Object process, timeout, timeout_msecs; 3843 register Lisp_Object process, timeout, timeout_msecs, just_this_one;
3451 { 3844 {
3452 int seconds; 3845 int seconds;
3453 int useconds; 3846 int useconds;
3454 3847
3455 if (! NILP (process)) 3848 if (! NILP (process))
3456 CHECK_PROCESS (process); 3849 CHECK_PROCESS (process);
3850 else
3851 just_this_one = Qnil;
3457 3852
3458 if (! NILP (timeout_msecs)) 3853 if (! NILP (timeout_msecs))
3459 { 3854 {
3460 CHECK_NUMBER (timeout_msecs); 3855 CHECK_NUMBER (timeout_msecs);
3461 useconds = XINT (timeout_msecs); 3856 useconds = XINT (timeout_msecs);
3487 seconds = XINT (timeout); 3882 seconds = XINT (timeout);
3488 if (seconds < 0 || (seconds == 0 && useconds == 0)) 3883 if (seconds < 0 || (seconds == 0 && useconds == 0))
3489 seconds = -1; 3884 seconds = -1;
3490 } 3885 }
3491 else 3886 else
3492 { 3887 seconds = NILP (process) ? -1 : 0;
3493 if (NILP (process))
3494 seconds = -1;
3495 else
3496 seconds = 0;
3497 }
3498
3499 if (NILP (process))
3500 XSETFASTINT (process, 0);
3501 3888
3502 return 3889 return
3503 (wait_reading_process_input (seconds, useconds, process, 0) 3890 (wait_reading_process_output (seconds, useconds, 0, 0,
3891 Qnil,
3892 !NILP (process) ? XPROCESS (process) : NULL,
3893 NILP (just_this_one) ? 0 :
3894 !INTEGERP (just_this_one) ? 1 : -1)
3504 ? Qt : Qnil); 3895 ? Qt : Qnil);
3505 } 3896 }
3506 3897
3507 /* Accept a connection for server process SERVER on CHANNEL. */ 3898 /* Accept a connection for server process SERVER on CHANNEL. */
3508 3899
3519 struct Lisp_Process *p; 3910 struct Lisp_Process *p;
3520 int s; 3911 int s;
3521 union u_sockaddr { 3912 union u_sockaddr {
3522 struct sockaddr sa; 3913 struct sockaddr sa;
3523 struct sockaddr_in in; 3914 struct sockaddr_in in;
3915 #ifdef AF_INET6
3916 struct sockaddr_in6 in6;
3917 #endif
3524 #ifdef HAVE_LOCAL_SOCKETS 3918 #ifdef HAVE_LOCAL_SOCKETS
3525 struct sockaddr_un un; 3919 struct sockaddr_un un;
3526 #endif 3920 #endif
3527 } saddr; 3921 } saddr;
3528 int len = sizeof saddr; 3922 int len = sizeof saddr;
3574 args[1] = host; 3968 args[1] = host;
3575 args[2] = service; 3969 args[2] = service;
3576 caller = Fformat (3, args); 3970 caller = Fformat (3, args);
3577 } 3971 }
3578 break; 3972 break;
3973
3974 #ifdef AF_INET6
3975 case AF_INET6:
3976 {
3977 Lisp_Object args[9];
3978 uint16_t *ip6 = (uint16_t *)&saddr.in6.sin6_addr;
3979 int i;
3980 args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
3981 for (i = 0; i < 8; i++)
3982 args[i+1] = make_number (ntohs(ip6[i]));
3983 host = Fformat (9, args);
3984 service = make_number (ntohs (saddr.in.sin_port));
3985
3986 args[0] = build_string (" <[%s]:%d>");
3987 args[1] = host;
3988 args[2] = service;
3989 caller = Fformat (3, args);
3990 }
3991 break;
3992 #endif
3579 3993
3580 #ifdef HAVE_LOCAL_SOCKETS 3994 #ifdef HAVE_LOCAL_SOCKETS
3581 case AF_LOCAL: 3995 case AF_LOCAL:
3582 #endif 3996 #endif
3583 default: 3997 default:
3692 build_string ("\n"))); 4106 build_string ("\n")));
3693 } 4107 }
3694 4108
3695 /* This variable is different from waiting_for_input in keyboard.c. 4109 /* This variable is different from waiting_for_input in keyboard.c.
3696 It is used to communicate to a lisp process-filter/sentinel (via the 4110 It is used to communicate to a lisp process-filter/sentinel (via the
3697 function Fwaiting_for_user_input_p below) whether emacs was waiting 4111 function Fwaiting_for_user_input_p below) whether Emacs was waiting
3698 for user-input when that process-filter was called. 4112 for user-input when that process-filter was called.
3699 waiting_for_input cannot be used as that is by definition 0 when 4113 waiting_for_input cannot be used as that is by definition 0 when
3700 lisp code is being evalled. 4114 lisp code is being evalled.
3701 This is also used in record_asynch_buffer_change. 4115 This is also used in record_asynch_buffer_change.
3702 For that purpose, this must be 0 4116 For that purpose, this must be 0
3703 when not inside wait_reading_process_input. */ 4117 when not inside wait_reading_process_output. */
3704 static int waiting_for_user_input_p; 4118 static int waiting_for_user_input_p;
3705 4119
3706 /* This is here so breakpoints can be put on it. */ 4120 /* This is here so breakpoints can be put on it. */
3707 static void 4121 static void
3708 wait_reading_process_input_1 () 4122 wait_reading_process_output_1 ()
3709 { 4123 {
3710 } 4124 }
3711 4125
3712 /* Read and dispose of subprocess output while waiting for timeout to 4126 /* Read and dispose of subprocess output while waiting for timeout to
3713 elapse and/or keyboard input to be available. 4127 elapse and/or keyboard input to be available.
3725 READ_KBD is a lisp value: 4139 READ_KBD is a lisp value:
3726 0 to ignore keyboard input, or 4140 0 to ignore keyboard input, or
3727 1 to return when input is available, or 4141 1 to return when input is available, or
3728 -1 meaning caller will actually read the input, so don't throw to 4142 -1 meaning caller will actually read the input, so don't throw to
3729 the quit handler, or 4143 the quit handler, or
3730 a cons cell, meaning wait until its car is non-nil
3731 (and gobble terminal input into the buffer if any arrives), or
3732 a process object, meaning wait until something arrives from that
3733 process. The return value is true iff we read some input from
3734 that process.
3735 4144
3736 DO_DISPLAY != 0 means redisplay should be done to show subprocess 4145 DO_DISPLAY != 0 means redisplay should be done to show subprocess
3737 output that arrives. 4146 output that arrives.
3738 4147
3739 If READ_KBD is a pointer to a struct Lisp_Process, then the 4148 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
3740 function returns true iff we received input from that process 4149 (and gobble terminal input into the buffer if any arrives).
3741 before the timeout elapsed. 4150
4151 If WAIT_PROC is specified, wait until something arrives from that
4152 process. The return value is true iff we read some input from
4153 that process.
4154
4155 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4156 (suspending output from other processes). A negative value
4157 means don't run any timers either.
4158
4159 If WAIT_PROC is specified, then the function returns true iff we
4160 received input from that process before the timeout elapsed.
3742 Otherwise, return true iff we received input from any process. */ 4161 Otherwise, return true iff we received input from any process. */
3743 4162
3744 int 4163 int
3745 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display) 4164 wait_reading_process_output (time_limit, microsecs, read_kbd, do_display,
3746 int time_limit, microsecs; 4165 wait_for_cell, wait_proc, just_wait_proc)
3747 Lisp_Object read_kbd; 4166 int time_limit, microsecs, read_kbd, do_display;
3748 int do_display; 4167 Lisp_Object wait_for_cell;
4168 struct Lisp_Process *wait_proc;
4169 int just_wait_proc;
3749 { 4170 {
3750 register int channel, nfds; 4171 register int channel, nfds;
3751 static SELECT_TYPE Available; 4172 SELECT_TYPE Available;
3752 static SELECT_TYPE Connecting; 4173 #ifdef NON_BLOCKING_CONNECT
3753 int check_connect, no_avail; 4174 SELECT_TYPE Connecting;
4175 int check_connect;
4176 #endif
4177 int check_delay, no_avail;
3754 int xerrno; 4178 int xerrno;
3755 Lisp_Object proc; 4179 Lisp_Object proc;
3756 EMACS_TIME timeout, end_time; 4180 EMACS_TIME timeout, end_time;
3757 int wait_channel = -1; 4181 int wait_channel = -1;
3758 struct Lisp_Process *wait_proc = 0;
3759 int got_some_input = 0; 4182 int got_some_input = 0;
3760 /* Either nil or a cons cell, the car of which is of interest and 4183 /* Either nil or a cons cell, the car of which is of interest and
3761 may be changed outside of this routine. */ 4184 may be changed outside of this routine. */
3762 Lisp_Object wait_for_cell = Qnil; 4185 int saved_waiting_for_user_input_p = waiting_for_user_input_p;
3763 4186
3764 FD_ZERO (&Available); 4187 FD_ZERO (&Available);
4188 #ifdef NON_BLOCKING_CONNECT
3765 FD_ZERO (&Connecting); 4189 FD_ZERO (&Connecting);
3766 4190 #endif
3767 /* If read_kbd is a process to watch, set wait_proc and wait_channel 4191
3768 accordingly. */ 4192 /* If wait_proc is a process to watch, set wait_channel accordingly. */
3769 if (PROCESSP (read_kbd)) 4193 if (wait_proc != NULL)
3770 { 4194 wait_channel = XINT (wait_proc->infd);
3771 wait_proc = XPROCESS (read_kbd); 4195
3772 wait_channel = XINT (wait_proc->infd); 4196 waiting_for_user_input_p = read_kbd;
3773 XSETFASTINT (read_kbd, 0);
3774 }
3775
3776 /* If waiting for non-nil in a cell, record where. */
3777 if (CONSP (read_kbd))
3778 {
3779 wait_for_cell = read_kbd;
3780 XSETFASTINT (read_kbd, 0);
3781 }
3782
3783 waiting_for_user_input_p = XINT (read_kbd);
3784 4197
3785 /* Since we may need to wait several times, 4198 /* Since we may need to wait several times,
3786 compute the absolute time to return at. */ 4199 compute the absolute time to return at. */
3787 if (time_limit || microsecs) 4200 if (time_limit || microsecs)
3788 { 4201 {
3789 EMACS_GET_TIME (end_time); 4202 EMACS_GET_TIME (end_time);
3790 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); 4203 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
3791 EMACS_ADD_TIME (end_time, end_time, timeout); 4204 EMACS_ADD_TIME (end_time, end_time, timeout);
3792 } 4205 }
3793 #ifdef POLLING_PROBLEM_IN_SELECT 4206 #ifdef POLL_INTERRUPTED_SYS_CALL
3794 /* AlainF 5-Jul-1996 4207 /* AlainF 5-Jul-1996
3795 HP-UX 10.10 seem to have problems with signals coming in 4208 HP-UX 10.10 seem to have problems with signals coming in
3796 Causes "poll: interrupted system call" messages when Emacs is run 4209 Causes "poll: interrupted system call" messages when Emacs is run
3797 in an X window 4210 in an X window
3798 Turn off periodic alarms (in case they are in use), 4211 Turn off periodic alarms (in case they are in use),
3799 and then turn off any other atimers. */ 4212 and then turn off any other atimers. */
3800 stop_polling (); 4213 stop_polling ();
3801 turn_on_atimers (0); 4214 turn_on_atimers (0);
3802 #endif 4215 #endif /* POLL_INTERRUPTED_SYS_CALL */
3803 4216
3804 while (1) 4217 while (1)
3805 { 4218 {
3806 int timeout_reduced_for_timers = 0; 4219 int timeout_reduced_for_timers = 0;
3807 4220
3808 /* If calling from keyboard input, do not quit 4221 /* If calling from keyboard input, do not quit
3809 since we want to return C-g as an input character. 4222 since we want to return C-g as an input character.
3810 Otherwise, do pending quit if requested. */ 4223 Otherwise, do pending quit if requested. */
3811 if (XINT (read_kbd) >= 0) 4224 if (read_kbd >= 0)
3812 QUIT; 4225 QUIT;
4226 #ifdef SYNC_INPUT
4227 else if (interrupt_input_pending)
4228 handle_async_input ();
4229 #endif
3813 4230
3814 /* Exit now if the cell we're waiting for became non-nil. */ 4231 /* Exit now if the cell we're waiting for became non-nil. */
3815 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 4232 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
3816 break; 4233 break;
3817 4234
3839 4256
3840 /* Normally we run timers here. 4257 /* Normally we run timers here.
3841 But not if wait_for_cell; in those cases, 4258 But not if wait_for_cell; in those cases,
3842 the wait is supposed to be short, 4259 the wait is supposed to be short,
3843 and those callers cannot handle running arbitrary Lisp code here. */ 4260 and those callers cannot handle running arbitrary Lisp code here. */
3844 if (NILP (wait_for_cell)) 4261 if (NILP (wait_for_cell)
4262 && just_wait_proc >= 0)
3845 { 4263 {
3846 EMACS_TIME timer_delay; 4264 EMACS_TIME timer_delay;
3847 4265
3848 do 4266 do
3849 { 4267 {
3867 break; 4285 break;
3868 } 4286 }
3869 while (!detect_input_pending ()); 4287 while (!detect_input_pending ());
3870 4288
3871 /* If there is unread keyboard input, also return. */ 4289 /* If there is unread keyboard input, also return. */
3872 if (XINT (read_kbd) != 0 4290 if (read_kbd != 0
3873 && requeued_events_pending_p ()) 4291 && requeued_events_pending_p ())
3874 break; 4292 break;
3875 4293
3876 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1) 4294 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
3877 { 4295 {
3885 } 4303 }
3886 /* If time_limit is -1, we are not going to wait at all. */ 4304 /* If time_limit is -1, we are not going to wait at all. */
3887 else if (time_limit != -1) 4305 else if (time_limit != -1)
3888 { 4306 {
3889 /* This is so a breakpoint can be put here. */ 4307 /* This is so a breakpoint can be put here. */
3890 wait_reading_process_input_1 (); 4308 wait_reading_process_output_1 ();
3891 } 4309 }
3892 } 4310 }
3893 4311
3894 /* Cause C-g and alarm signals to take immediate action, 4312 /* Cause C-g and alarm signals to take immediate action,
3895 and cause input available signals to zero out timeout. 4313 and cause input available signals to zero out timeout.
3896 4314
3897 It is important that we do this before checking for process 4315 It is important that we do this before checking for process
3898 activity. If we get a SIGCHLD after the explicit checks for 4316 activity. If we get a SIGCHLD after the explicit checks for
3899 process activity, timeout is the only way we will know. */ 4317 process activity, timeout is the only way we will know. */
3900 if (XINT (read_kbd) < 0) 4318 if (read_kbd < 0)
3901 set_waiting_for_input (&timeout); 4319 set_waiting_for_input (&timeout);
3902 4320
3903 /* If status of something has changed, and no input is 4321 /* If status of something has changed, and no input is
3904 available, notify the user of the change right away. After 4322 available, notify the user of the change right away. After
3905 this explicit check, we'll let the SIGCHLD handler zap 4323 this explicit check, we'll let the SIGCHLD handler zap
3906 timeout to get our attention. */ 4324 timeout to get our attention. */
3907 if (update_tick != process_tick && do_display) 4325 if (update_tick != process_tick && do_display)
3908 { 4326 {
3909 SELECT_TYPE Atemp, Ctemp; 4327 SELECT_TYPE Atemp;
4328 #ifdef NON_BLOCKING_CONNECT
4329 SELECT_TYPE Ctemp;
4330 #endif
3910 4331
3911 Atemp = input_wait_mask; 4332 Atemp = input_wait_mask;
3912 #ifdef MAC_OSX 4333 #if 0
3913 /* On Mac OS X, the SELECT system call always says input is 4334 /* On Mac OS X 10.0, the SELECT system call always says input is
3914 present (for reading) at stdin, even when none is. This 4335 present (for reading) at stdin, even when none is. This
3915 causes the call to SELECT below to return 1 and 4336 causes the call to SELECT below to return 1 and
3916 status_notify not to be called. As a result output of 4337 status_notify not to be called. As a result output of
3917 subprocesses are incorrectly discarded. */ 4338 subprocesses are incorrectly discarded.
4339 */
3918 FD_CLR (0, &Atemp); 4340 FD_CLR (0, &Atemp);
3919 #endif 4341 #endif
3920 Ctemp = connect_wait_mask; 4342 IF_NON_BLOCKING_CONNECT (Ctemp = connect_wait_mask);
4343
3921 EMACS_SET_SECS_USECS (timeout, 0, 0); 4344 EMACS_SET_SECS_USECS (timeout, 0, 0);
3922 if ((select (max (max_process_desc, max_keyboard_desc) + 1, 4345 if ((select (max (max_process_desc, max_keyboard_desc) + 1,
3923 &Atemp, 4346 &Atemp,
4347 #ifdef NON_BLOCKING_CONNECT
3924 (num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0), 4348 (num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0),
4349 #else
4350 (SELECT_TYPE *)0,
4351 #endif
3925 (SELECT_TYPE *)0, &timeout) 4352 (SELECT_TYPE *)0, &timeout)
3926 <= 0)) 4353 <= 0))
3927 { 4354 {
3928 /* It's okay for us to do this and then continue with 4355 /* It's okay for us to do this and then continue with
3929 the loop, since timeout has already been zeroed out. */ 4356 the loop, since timeout has already been zeroed out. */
3930 clear_waiting_for_input (); 4357 clear_waiting_for_input ();
3931 status_notify (); 4358 status_notify (NULL);
3932 } 4359 }
3933 } 4360 }
3934 4361
3935 /* Don't wait for output from a non-running process. Just 4362 /* Don't wait for output from a non-running process. Just
3936 read whatever data has already been received. */ 4363 read whatever data has already been received. */
3974 break; 4401 break;
3975 } 4402 }
3976 4403
3977 /* Wait till there is something to do */ 4404 /* Wait till there is something to do */
3978 4405
3979 if (!NILP (wait_for_cell)) 4406 if (wait_proc && just_wait_proc)
4407 {
4408 if (XINT (wait_proc->infd) < 0) /* Terminated */
4409 break;
4410 FD_SET (XINT (wait_proc->infd), &Available);
4411 check_delay = 0;
4412 IF_NON_BLOCKING_CONNECT (check_connect = 0);
4413 }
4414 else if (!NILP (wait_for_cell))
3980 { 4415 {
3981 Available = non_process_wait_mask; 4416 Available = non_process_wait_mask;
3982 check_connect = 0; 4417 check_delay = 0;
4418 IF_NON_BLOCKING_CONNECT (check_connect = 0);
3983 } 4419 }
3984 else 4420 else
3985 { 4421 {
3986 if (! XINT (read_kbd)) 4422 if (! read_kbd)
3987 Available = non_keyboard_wait_mask; 4423 Available = non_keyboard_wait_mask;
3988 else 4424 else
3989 Available = input_wait_mask; 4425 Available = input_wait_mask;
3990 check_connect = (num_pending_connects > 0); 4426 IF_NON_BLOCKING_CONNECT (check_connect = (num_pending_connects > 0));
4427 check_delay = wait_channel >= 0 ? 0 : process_output_delay_count;
3991 } 4428 }
3992 4429
3993 /* If frame size has changed or the window is newly mapped, 4430 /* If frame size has changed or the window is newly mapped,
3994 redisplay now, before we start to wait. There is a race 4431 redisplay now, before we start to wait. There is a race
3995 condition here; if a SIGIO arrives between now and the select 4432 condition here; if a SIGIO arrives between now and the select
3997 displaying a trashed screen. */ 4434 displaying a trashed screen. */
3998 if (frame_garbaged && do_display) 4435 if (frame_garbaged && do_display)
3999 { 4436 {
4000 clear_waiting_for_input (); 4437 clear_waiting_for_input ();
4001 redisplay_preserve_echo_area (11); 4438 redisplay_preserve_echo_area (11);
4002 if (XINT (read_kbd) < 0) 4439 if (read_kbd < 0)
4003 set_waiting_for_input (&timeout); 4440 set_waiting_for_input (&timeout);
4004 } 4441 }
4005 4442
4006 no_avail = 0; 4443 no_avail = 0;
4007 if (XINT (read_kbd) && detect_input_pending ()) 4444 if (read_kbd && detect_input_pending ())
4008 { 4445 {
4009 nfds = 0; 4446 nfds = 0;
4010 no_avail = 1; 4447 no_avail = 1;
4011 } 4448 }
4012 else 4449 else
4013 { 4450 {
4451 #ifdef NON_BLOCKING_CONNECT
4014 if (check_connect) 4452 if (check_connect)
4015 Connecting = connect_wait_mask; 4453 Connecting = connect_wait_mask;
4454 #endif
4455
4456 #ifdef ADAPTIVE_READ_BUFFERING
4457 /* Set the timeout for adaptive read buffering if any
4458 process has non-nil read_output_skip and non-zero
4459 read_output_delay, and we are not reading output for a
4460 specific wait_channel. It is not executed if
4461 Vprocess_adaptive_read_buffering is nil. */
4462 if (process_output_skip && check_delay > 0)
4463 {
4464 int usecs = EMACS_USECS (timeout);
4465 if (EMACS_SECS (timeout) > 0 || usecs > READ_OUTPUT_DELAY_MAX)
4466 usecs = READ_OUTPUT_DELAY_MAX;
4467 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4468 {
4469 proc = chan_process[channel];
4470 if (NILP (proc))
4471 continue;
4472 /* Find minimum non-zero read_output_delay among the
4473 processes with non-nil read_output_skip. */
4474 if (XINT (XPROCESS (proc)->read_output_delay) > 0)
4475 {
4476 check_delay--;
4477 if (NILP (XPROCESS (proc)->read_output_skip))
4478 continue;
4479 FD_CLR (channel, &Available);
4480 XPROCESS (proc)->read_output_skip = Qnil;
4481 if (XINT (XPROCESS (proc)->read_output_delay) < usecs)
4482 usecs = XINT (XPROCESS (proc)->read_output_delay);
4483 }
4484 }
4485 EMACS_SET_SECS_USECS (timeout, 0, usecs);
4486 process_output_skip = 0;
4487 }
4488 #endif
4489
4016 nfds = select (max (max_process_desc, max_keyboard_desc) + 1, 4490 nfds = select (max (max_process_desc, max_keyboard_desc) + 1,
4017 &Available, 4491 &Available,
4492 #ifdef NON_BLOCKING_CONNECT
4018 (check_connect ? &Connecting : (SELECT_TYPE *)0), 4493 (check_connect ? &Connecting : (SELECT_TYPE *)0),
4494 #else
4495 (SELECT_TYPE *)0,
4496 #endif
4019 (SELECT_TYPE *)0, &timeout); 4497 (SELECT_TYPE *)0, &timeout);
4020 } 4498 }
4021 4499
4022 xerrno = errno; 4500 xerrno = errno;
4023 4501
4069 } 4547 }
4070 4548
4071 if (no_avail) 4549 if (no_avail)
4072 { 4550 {
4073 FD_ZERO (&Available); 4551 FD_ZERO (&Available);
4074 check_connect = 0; 4552 IF_NON_BLOCKING_CONNECT (check_connect = 0);
4075 } 4553 }
4076 4554
4077 #if defined(sun) && !defined(USG5_4) 4555 #if defined(sun) && !defined(USG5_4)
4078 if (nfds > 0 && keyboard_bit_set (&Available) 4556 if (nfds > 0 && keyboard_bit_set (&Available)
4079 && interrupt_input) 4557 && interrupt_input)
4091 so get_input_pending should read the input. 4569 so get_input_pending should read the input.
4092 So this should not be needed. */ 4570 So this should not be needed. */
4093 /* If we are using polling for input, 4571 /* If we are using polling for input,
4094 and we see input available, make it get read now. 4572 and we see input available, make it get read now.
4095 Otherwise it might not actually get read for a second. 4573 Otherwise it might not actually get read for a second.
4096 And on hpux, since we turn off polling in wait_reading_process_input, 4574 And on hpux, since we turn off polling in wait_reading_process_output,
4097 it might never get read at all if we don't spend much time 4575 it might never get read at all if we don't spend much time
4098 outside of wait_reading_process_input. */ 4576 outside of wait_reading_process_output. */
4099 if (XINT (read_kbd) && interrupt_input 4577 if (read_kbd && interrupt_input
4100 && keyboard_bit_set (&Available) 4578 && keyboard_bit_set (&Available)
4101 && input_polling_used ()) 4579 && input_polling_used ())
4102 kill (getpid (), SIGALRM); 4580 kill (getpid (), SIGALRM);
4103 #endif 4581 #endif
4104 4582
4105 /* Check for keyboard input */ 4583 /* Check for keyboard input */
4106 /* If there is any, return immediately 4584 /* If there is any, return immediately
4107 to give it higher priority than subprocesses */ 4585 to give it higher priority than subprocesses */
4108 4586
4109 if (XINT (read_kbd) != 0) 4587 if (read_kbd != 0)
4110 { 4588 {
4111 int old_timers_run = timers_run; 4589 int old_timers_run = timers_run;
4112 struct buffer *old_buffer = current_buffer; 4590 struct buffer *old_buffer = current_buffer;
4113 int leave = 0; 4591 int leave = 0;
4114 4592
4129 if (leave) 4607 if (leave)
4130 break; 4608 break;
4131 } 4609 }
4132 4610
4133 /* If there is unread keyboard input, also return. */ 4611 /* If there is unread keyboard input, also return. */
4134 if (XINT (read_kbd) != 0 4612 if (read_kbd != 0
4135 && requeued_events_pending_p ()) 4613 && requeued_events_pending_p ())
4136 break; 4614 break;
4137 4615
4138 /* If we are not checking for keyboard input now, 4616 /* If we are not checking for keyboard input now,
4139 do process events (but don't run any timers). 4617 do process events (but don't run any timers).
4140 This is so that X events will be processed. 4618 This is so that X events will be processed.
4141 Otherwise they may have to wait until polling takes place. 4619 Otherwise they may have to wait until polling takes place.
4142 That would causes delays in pasting selections, for example. 4620 That would causes delays in pasting selections, for example.
4143 4621
4144 (We used to do this only if wait_for_cell.) */ 4622 (We used to do this only if wait_for_cell.) */
4145 if (XINT (read_kbd) == 0 && detect_input_pending ()) 4623 if (read_kbd == 0 && detect_input_pending ())
4146 { 4624 {
4147 swallow_events (do_display); 4625 swallow_events (do_display);
4148 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */ 4626 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4149 if (detect_input_pending ()) 4627 if (detect_input_pending ())
4150 break; 4628 break;
4159 /* If we think we have keyboard input waiting, but didn't get SIGIO, 4637 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4160 go read it. This can happen with X on BSD after logging out. 4638 go read it. This can happen with X on BSD after logging out.
4161 In that case, there really is no input and no SIGIO, 4639 In that case, there really is no input and no SIGIO,
4162 but select says there is input. */ 4640 but select says there is input. */
4163 4641
4164 if (XINT (read_kbd) && interrupt_input 4642 if (read_kbd && interrupt_input
4165 && keyboard_bit_set (&Available)) 4643 && keyboard_bit_set (&Available) && ! noninteractive)
4166 kill (getpid (), SIGIO); 4644 kill (getpid (), SIGIO);
4167 #endif 4645 #endif
4168 4646
4169 if (! wait_proc) 4647 if (! wait_proc)
4170 got_some_input |= nfds > 0; 4648 got_some_input |= nfds > 0;
4171 4649
4172 /* If checking input just got us a size-change event from X, 4650 /* If checking input just got us a size-change event from X,
4173 obey it now if we should. */ 4651 obey it now if we should. */
4174 if (XINT (read_kbd) || ! NILP (wait_for_cell)) 4652 if (read_kbd || ! NILP (wait_for_cell))
4175 do_pending_window_change (0); 4653 do_pending_window_change (0);
4176 4654
4177 /* Check for data from a process. */ 4655 /* Check for data from a process. */
4178 if (no_avail || nfds == 0) 4656 if (no_avail || nfds == 0)
4179 continue; 4657 continue;
4275 XPROCESS (proc)->status 4753 XPROCESS (proc)->status
4276 = Fcons (Qexit, Fcons (make_number (256), Qnil)); 4754 = Fcons (Qexit, Fcons (make_number (256), Qnil));
4277 } 4755 }
4278 } 4756 }
4279 #ifdef NON_BLOCKING_CONNECT 4757 #ifdef NON_BLOCKING_CONNECT
4280 if (check_connect && FD_ISSET (channel, &Connecting)) 4758 if (check_connect && FD_ISSET (channel, &Connecting)
4759 && FD_ISSET (channel, &connect_wait_mask))
4281 { 4760 {
4282 struct Lisp_Process *p; 4761 struct Lisp_Process *p;
4283 4762
4284 FD_CLR (channel, &connect_wait_mask); 4763 FD_CLR (channel, &connect_wait_mask);
4285 if (--num_pending_connects < 0) 4764 if (--num_pending_connects < 0)
4338 } 4817 }
4339 #endif /* NON_BLOCKING_CONNECT */ 4818 #endif /* NON_BLOCKING_CONNECT */
4340 } /* end for each file descriptor */ 4819 } /* end for each file descriptor */
4341 } /* end while exit conditions not met */ 4820 } /* end while exit conditions not met */
4342 4821
4343 waiting_for_user_input_p = 0; 4822 waiting_for_user_input_p = saved_waiting_for_user_input_p;
4344 4823
4345 /* If calling from keyboard input, do not quit 4824 /* If calling from keyboard input, do not quit
4346 since we want to return C-g as an input character. 4825 since we want to return C-g as an input character.
4347 Otherwise, do pending quit if requested. */ 4826 Otherwise, do pending quit if requested. */
4348 if (XINT (read_kbd) >= 0) 4827 if (read_kbd >= 0)
4349 { 4828 {
4350 /* Prevent input_pending from remaining set if we quit. */ 4829 /* Prevent input_pending from remaining set if we quit. */
4351 clear_input_pending (); 4830 clear_input_pending ();
4352 QUIT; 4831 QUIT;
4353 } 4832 }
4354 #ifdef hpux 4833 #ifdef POLL_INTERRUPTED_SYS_CALL
4355 /* AlainF 5-Jul-1996 4834 /* AlainF 5-Jul-1996
4356 HP-UX 10.10 seems to have problems with signals coming in 4835 HP-UX 10.10 seems to have problems with signals coming in
4357 Causes "poll: interrupted system call" messages when Emacs is run 4836 Causes "poll: interrupted system call" messages when Emacs is run
4358 in an X window 4837 in an X window
4359 Turn periodic alarms back on */ 4838 Turn periodic alarms back on */
4360 start_polling (); 4839 start_polling ();
4361 #endif 4840 #endif /* POLL_INTERRUPTED_SYS_CALL */
4362 4841
4363 return got_some_input; 4842 return got_some_input;
4364 } 4843 }
4365 4844
4366 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */ 4845 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
4385 4864
4386 /* Read pending output from the process channel, 4865 /* Read pending output from the process channel,
4387 starting with our buffered-ahead character if we have one. 4866 starting with our buffered-ahead character if we have one.
4388 Yield number of decoded characters read. 4867 Yield number of decoded characters read.
4389 4868
4390 This function reads at most 1024 characters. 4869 This function reads at most 4096 characters.
4391 If you want to read all available subprocess output, 4870 If you want to read all available subprocess output,
4392 you must call it repeatedly until it returns zero. 4871 you must call it repeatedly until it returns zero.
4393 4872
4394 The characters read are decoded according to PROC's coding-system 4873 The characters read are decoded according to PROC's coding-system
4395 for decoding. */ 4874 for decoding. */
4396 4875
4397 int 4876 static int
4398 read_process_output (proc, channel) 4877 read_process_output (proc, channel)
4399 Lisp_Object proc; 4878 Lisp_Object proc;
4400 register int channel; 4879 register int channel;
4401 { 4880 {
4402 register int nchars, nbytes; 4881 register int nbytes;
4403 char *chars; 4882 char *chars;
4404 register Lisp_Object outstream; 4883 register Lisp_Object outstream;
4405 register struct buffer *old = current_buffer; 4884 register struct buffer *old = current_buffer;
4406 register struct Lisp_Process *p = XPROCESS (proc); 4885 register struct Lisp_Process *p = XPROCESS (proc);
4407 register int opoint; 4886 register int opoint;
4408 struct coding_system *coding = proc_decode_coding_system[channel]; 4887 struct coding_system *coding = proc_decode_coding_system[channel];
4409 int carryover = XINT (p->decoding_carryover); 4888 int carryover = XINT (p->decoding_carryover);
4410 int readmax = 1024; 4889 int readmax = 4096;
4411 4890
4412 #ifdef VMS 4891 #ifdef VMS
4413 VMS_PROC_STUFF *vs, *get_vms_process_pointer(); 4892 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
4414 4893
4415 vs = get_vms_process_pointer (p->pid); 4894 vs = get_vms_process_pointer (p->pid);
4438 bcopy (SDATA (p->decoding_buf), buf, carryover); 4917 bcopy (SDATA (p->decoding_buf), buf, carryover);
4439 bcopy (vs->inputBuffer, chars + carryover, nbytes); 4918 bcopy (vs->inputBuffer, chars + carryover, nbytes);
4440 } 4919 }
4441 #else /* not VMS */ 4920 #else /* not VMS */
4442 4921
4443 #ifdef DATAGRAM_SOCKETS
4444 /* A datagram is one packet; allow at least 1500+ bytes of data
4445 corresponding to the typical Ethernet frame size. */
4446 if (DATAGRAM_CHAN_P (channel))
4447 {
4448 /* carryover = 0; */ /* Does carryover make sense for datagrams? */
4449 readmax += 1024;
4450 }
4451 #endif
4452
4453 chars = (char *) alloca (carryover + readmax); 4922 chars = (char *) alloca (carryover + readmax);
4454 if (carryover) 4923 if (carryover)
4455 /* See the comment above. */ 4924 /* See the comment above. */
4456 bcopy (SDATA (p->decoding_buf), chars, carryover); 4925 bcopy (SDATA (p->decoding_buf), chars, carryover);
4457 4926
4458 #ifdef DATAGRAM_SOCKETS 4927 #ifdef DATAGRAM_SOCKETS
4459 /* We have a working select, so proc_buffered_char is always -1. */ 4928 /* We have a working select, so proc_buffered_char is always -1. */
4460 if (DATAGRAM_CHAN_P (channel)) 4929 if (DATAGRAM_CHAN_P (channel))
4461 { 4930 {
4462 int len = datagram_address[channel].len; 4931 int len = datagram_address[channel].len;
4463 nbytes = recvfrom (channel, chars + carryover, readmax - carryover, 4932 nbytes = recvfrom (channel, chars + carryover, readmax,
4464 0, datagram_address[channel].sa, &len); 4933 0, datagram_address[channel].sa, &len);
4465 } 4934 }
4466 else 4935 else
4467 #endif 4936 #endif
4468 if (proc_buffered_char[channel] < 0) 4937 if (proc_buffered_char[channel] < 0)
4469 nbytes = emacs_read (channel, chars + carryover, readmax - carryover); 4938 {
4939 nbytes = emacs_read (channel, chars + carryover, readmax);
4940 #ifdef ADAPTIVE_READ_BUFFERING
4941 if (nbytes > 0 && !NILP (p->adaptive_read_buffering))
4942 {
4943 int delay = XINT (p->read_output_delay);
4944 if (nbytes < 256)
4945 {
4946 if (delay < READ_OUTPUT_DELAY_MAX_MAX)
4947 {
4948 if (delay == 0)
4949 process_output_delay_count++;
4950 delay += READ_OUTPUT_DELAY_INCREMENT * 2;
4951 }
4952 }
4953 else if (delay > 0 && (nbytes == readmax))
4954 {
4955 delay -= READ_OUTPUT_DELAY_INCREMENT;
4956 if (delay == 0)
4957 process_output_delay_count--;
4958 }
4959 XSETINT (p->read_output_delay, delay);
4960 if (delay)
4961 {
4962 p->read_output_skip = Qt;
4963 process_output_skip = 1;
4964 }
4965 }
4966 #endif
4967 }
4470 else 4968 else
4471 { 4969 {
4472 chars[carryover] = proc_buffered_char[channel]; 4970 chars[carryover] = proc_buffered_char[channel];
4473 proc_buffered_char[channel] = -1; 4971 proc_buffered_char[channel] = -1;
4474 nbytes = emacs_read (channel, chars + carryover + 1, readmax - 1 - carryover); 4972 nbytes = emacs_read (channel, chars + carryover + 1, readmax - 1);
4475 if (nbytes < 0) 4973 if (nbytes < 0)
4476 nbytes = 1; 4974 nbytes = 1;
4477 else 4975 else
4478 nbytes = nbytes + 1; 4976 nbytes = nbytes + 1;
4479 } 4977 }
4521 save the same match data in safely recursive fashion. */ 5019 save the same match data in safely recursive fashion. */
4522 if (outer_running_asynch_code) 5020 if (outer_running_asynch_code)
4523 { 5021 {
4524 Lisp_Object tem; 5022 Lisp_Object tem;
4525 /* Don't clobber the CURRENT match data, either! */ 5023 /* Don't clobber the CURRENT match data, either! */
4526 tem = Fmatch_data (Qnil, Qnil); 5024 tem = Fmatch_data (Qnil, Qnil, Qnil);
4527 restore_match_data (); 5025 restore_search_regs ();
4528 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil)); 5026 record_unwind_save_match_data ();
4529 Fset_match_data (tem); 5027 Fset_match_data (tem, Qt);
4530 } 5028 }
4531 5029
4532 /* For speed, if a search happens within this code, 5030 /* For speed, if a search happens within this code,
4533 save the match data in a special nonrecursive fashion. */ 5031 save the match data in a special nonrecursive fashion. */
4534 running_asynch_code = 1; 5032 running_asynch_code = 1;
4560 proc_encode_coding_system[XINT (p->outfd)]); 5058 proc_encode_coding_system[XINT (p->outfd)]);
4561 } 5059 }
4562 } 5060 }
4563 5061
4564 carryover = nbytes - coding->consumed; 5062 carryover = nbytes - coding->consumed;
5063 if (SCHARS (p->decoding_buf) < carryover)
5064 p->decoding_buf = make_uninit_string (carryover);
4565 bcopy (chars + coding->consumed, SDATA (p->decoding_buf), 5065 bcopy (chars + coding->consumed, SDATA (p->decoding_buf),
4566 carryover); 5066 carryover);
4567 XSETINT (p->decoding_carryover, carryover); 5067 XSETINT (p->decoding_carryover, carryover);
4568 /* Adjust the multibyteness of TEXT to that of the filter. */ 5068 /* Adjust the multibyteness of TEXT to that of the filter. */
4569 if (NILP (p->filter_multibyte) != ! STRING_MULTIBYTE (text)) 5069 if (NILP (p->filter_multibyte) != ! STRING_MULTIBYTE (text))
4570 text = (STRING_MULTIBYTE (text) 5070 text = (STRING_MULTIBYTE (text)
4571 ? Fstring_as_unibyte (text) 5071 ? Fstring_as_unibyte (text)
4572 : Fstring_to_multibyte (text)); 5072 : Fstring_to_multibyte (text));
4573 nbytes = SBYTES (text); 5073 if (SBYTES (text) > 0)
4574 nchars = SCHARS (text);
4575 if (nbytes > 0)
4576 internal_condition_case_1 (read_process_output_call, 5074 internal_condition_case_1 (read_process_output_call,
4577 Fcons (outstream, 5075 Fcons (outstream,
4578 Fcons (proc, Fcons (text, Qnil))), 5076 Fcons (proc, Fcons (text, Qnil))),
4579 !NILP (Vdebug_on_error) ? Qnil : Qerror, 5077 !NILP (Vdebug_on_error) ? Qnil : Qerror,
4580 read_process_output_error_handler); 5078 read_process_output_error_handler);
4581 5079
4582 /* If we saved the match data nonrecursively, restore it now. */ 5080 /* If we saved the match data nonrecursively, restore it now. */
4583 restore_match_data (); 5081 restore_search_regs ();
4584 running_asynch_code = outer_running_asynch_code; 5082 running_asynch_code = outer_running_asynch_code;
4585 5083
4586 /* Handling the process output should not deactivate the mark. */ 5084 /* Handling the process output should not deactivate the mark. */
4587 Vdeactivate_mark = odeactivate; 5085 Vdeactivate_mark = odeactivate;
4588 5086
4604 5102
4605 #ifdef VMS 5103 #ifdef VMS
4606 start_vms_process_read (vs); 5104 start_vms_process_read (vs);
4607 #endif 5105 #endif
4608 unbind_to (count, Qnil); 5106 unbind_to (count, Qnil);
4609 return nchars; 5107 return nbytes;
4610 } 5108 }
4611 5109
4612 /* If no filter, write into buffer if it isn't dead. */ 5110 /* If no filter, write into buffer if it isn't dead. */
4613 if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name)) 5111 if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
4614 { 5112 {
4666 setup_coding_system (coding->symbol, 5164 setup_coding_system (coding->symbol,
4667 proc_encode_coding_system[XINT (p->outfd)]); 5165 proc_encode_coding_system[XINT (p->outfd)]);
4668 } 5166 }
4669 } 5167 }
4670 carryover = nbytes - coding->consumed; 5168 carryover = nbytes - coding->consumed;
5169 if (SCHARS (p->decoding_buf) < carryover)
5170 p->decoding_buf = make_uninit_string (carryover);
4671 bcopy (chars + coding->consumed, SDATA (p->decoding_buf), 5171 bcopy (chars + coding->consumed, SDATA (p->decoding_buf),
4672 carryover); 5172 carryover);
4673 XSETINT (p->decoding_carryover, carryover); 5173 XSETINT (p->decoding_carryover, carryover);
4674 /* Adjust the multibyteness of TEXT to that of the buffer. */ 5174 /* Adjust the multibyteness of TEXT to that of the buffer. */
4675 if (NILP (current_buffer->enable_multibyte_characters) 5175 if (NILP (current_buffer->enable_multibyte_characters)
4676 != ! STRING_MULTIBYTE (text)) 5176 != ! STRING_MULTIBYTE (text))
4677 text = (STRING_MULTIBYTE (text) 5177 text = (STRING_MULTIBYTE (text)
4678 ? Fstring_as_unibyte (text) 5178 ? Fstring_as_unibyte (text)
4679 : Fstring_to_multibyte (text)); 5179 : Fstring_to_multibyte (text));
4680 nbytes = SBYTES (text);
4681 nchars = SCHARS (text);
4682 /* Insert before markers in case we are inserting where 5180 /* Insert before markers in case we are inserting where
4683 the buffer's mark is, and the user's next command is Meta-y. */ 5181 the buffer's mark is, and the user's next command is Meta-y. */
4684 insert_from_string_before_markers (text, 0, 0, nchars, nbytes, 0); 5182 insert_from_string_before_markers (text, 0, 0,
5183 SCHARS (text), SBYTES (text), 0);
4685 5184
4686 /* Make sure the process marker's position is valid when the 5185 /* Make sure the process marker's position is valid when the
4687 process buffer is changed in the signal_after_change above. 5186 process buffer is changed in the signal_after_change above.
4688 W3 is known to do that. */ 5187 W3 is known to do that. */
4689 if (BUFFERP (p->buffer) 5188 if (BUFFERP (p->buffer)
4729 return nbytes; 5228 return nbytes;
4730 } 5229 }
4731 5230
4732 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p, 5231 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
4733 0, 0, 0, 5232 0, 0, 0,
4734 doc: /* Returns non-nil if emacs is waiting for input from the user. 5233 doc: /* Returns non-nil if Emacs is waiting for input from the user.
4735 This is intended for use by asynchronous process output filters and sentinels. */) 5234 This is intended for use by asynchronous process output filters and sentinels. */)
4736 () 5235 ()
4737 { 5236 {
4738 return (waiting_for_user_input_p ? Qt : Qnil); 5237 return (waiting_for_user_input_p ? Qt : Qnil);
4739 } 5238 }
4744 Lisp_Object process_sent_to; 5243 Lisp_Object process_sent_to;
4745 5244
4746 SIGTYPE 5245 SIGTYPE
4747 send_process_trap () 5246 send_process_trap ()
4748 { 5247 {
5248 SIGNAL_THREAD_CHECK (SIGPIPE);
4749 #ifdef BSD4_1 5249 #ifdef BSD4_1
4750 sigrelse (SIGPIPE); 5250 sigrelse (SIGPIPE);
4751 sigrelse (SIGALRM); 5251 sigrelse (SIGALRM);
4752 #endif /* BSD4_1 */ 5252 #endif /* BSD4_1 */
5253 sigunblock (sigmask (SIGPIPE));
4753 longjmp (send_process_frame, 1); 5254 longjmp (send_process_frame, 1);
4754 } 5255 }
4755 5256
4756 /* Send some data to process PROC. 5257 /* Send some data to process PROC.
4757 BUF is the beginning of the data; LEN is the number of characters. 5258 BUF is the beginning of the data; LEN is the number of characters.
4761 If OBJECT is not nil, the data is encoded by PROC's coding-system 5262 If OBJECT is not nil, the data is encoded by PROC's coding-system
4762 for encoding before it is sent. 5263 for encoding before it is sent.
4763 5264
4764 This function can evaluate Lisp code and can garbage collect. */ 5265 This function can evaluate Lisp code and can garbage collect. */
4765 5266
4766 void 5267 static void
4767 send_process (proc, buf, len, object) 5268 send_process (proc, buf, len, object)
4768 volatile Lisp_Object proc; 5269 volatile Lisp_Object proc;
4769 unsigned char *volatile buf; 5270 unsigned char *volatile buf;
4770 volatile int len; 5271 volatile int len;
4771 volatile Lisp_Object object; 5272 volatile Lisp_Object object;
4772 { 5273 {
4773 /* Use volatile to protect variables from being clobbered by longjmp. */ 5274 /* Use volatile to protect variables from being clobbered by longjmp. */
5275 struct Lisp_Process *p = XPROCESS (proc);
4774 int rv; 5276 int rv;
4775 struct coding_system *coding; 5277 struct coding_system *coding;
4776 struct gcpro gcpro1; 5278 struct gcpro gcpro1;
5279 SIGTYPE (*volatile old_sigpipe) ();
4777 5280
4778 GCPRO1 (object); 5281 GCPRO1 (object);
4779 5282
4780 #ifdef VMS 5283 #ifdef VMS
4781 struct Lisp_Process *p = XPROCESS (proc);
4782 VMS_PROC_STUFF *vs, *get_vms_process_pointer(); 5284 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
4783 #endif /* VMS */ 5285 #endif /* VMS */
4784 5286
4785 if (! NILP (XPROCESS (proc)->raw_status_low)) 5287 if (! NILP (p->raw_status_low))
4786 update_status (XPROCESS (proc)); 5288 update_status (p);
4787 if (! EQ (XPROCESS (proc)->status, Qrun)) 5289 if (! EQ (p->status, Qrun))
4788 error ("Process %s not running", 5290 error ("Process %s not running", SDATA (p->name));
4789 SDATA (XPROCESS (proc)->name)); 5291 if (XINT (p->outfd) < 0)
4790 if (XINT (XPROCESS (proc)->outfd) < 0) 5292 error ("Output file descriptor of %s is closed", SDATA (p->name));
4791 error ("Output file descriptor of %s is closed", 5293
4792 SDATA (XPROCESS (proc)->name)); 5294 coding = proc_encode_coding_system[XINT (p->outfd)];
4793
4794 coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
4795 Vlast_coding_system_used = coding->symbol; 5295 Vlast_coding_system_used = coding->symbol;
4796 5296
4797 if ((STRINGP (object) && STRING_MULTIBYTE (object)) 5297 if ((STRINGP (object) && STRING_MULTIBYTE (object))
4798 || (BUFFERP (object) 5298 || (BUFFERP (object)
4799 && !NILP (XBUFFER (object)->enable_multibyte_characters)) 5299 && !NILP (XBUFFER (object)->enable_multibyte_characters))
4800 || EQ (object, Qt)) 5300 || EQ (object, Qt))
4801 { 5301 {
4802 if (!EQ (coding->symbol, XPROCESS (proc)->encode_coding_system)) 5302 if (!EQ (coding->symbol, p->encode_coding_system))
4803 /* The coding system for encoding was changed to raw-text 5303 /* The coding system for encoding was changed to raw-text
4804 because we sent a unibyte text previously. Now we are 5304 because we sent a unibyte text previously. Now we are
4805 sending a multibyte text, thus we must encode it by the 5305 sending a multibyte text, thus we must encode it by the
4806 original coding system specified for the current 5306 original coding system specified for the current process. */
4807 process. */ 5307 setup_coding_system (p->encode_coding_system, coding);
4808 setup_coding_system (XPROCESS (proc)->encode_coding_system, coding);
4809 /* src_multibyte should be set to 1 _after_ a call to 5308 /* src_multibyte should be set to 1 _after_ a call to
4810 setup_coding_system, since it resets src_multibyte to 5309 setup_coding_system, since it resets src_multibyte to
4811 zero. */ 5310 zero. */
4812 coding->src_multibyte = 1; 5311 coding->src_multibyte = 1;
4813 } 5312 }
4832 5331
4833 if (CODING_REQUIRE_ENCODING (coding)) 5332 if (CODING_REQUIRE_ENCODING (coding))
4834 { 5333 {
4835 int require = encoding_buffer_size (coding, len); 5334 int require = encoding_buffer_size (coding, len);
4836 int from_byte = -1, from = -1, to = -1; 5335 int from_byte = -1, from = -1, to = -1;
4837 unsigned char *temp_buf = NULL;
4838 5336
4839 if (BUFFERP (object)) 5337 if (BUFFERP (object))
4840 { 5338 {
4841 from_byte = BUF_PTR_BYTE_POS (XBUFFER (object), buf); 5339 from_byte = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
4842 from = buf_bytepos_to_charpos (XBUFFER (object), from_byte); 5340 from = buf_bytepos_to_charpos (XBUFFER (object), from_byte);
4855 coding_save_composition (coding, from, to, object); 5353 coding_save_composition (coding, from, to, object);
4856 else 5354 else
4857 coding->composing = COMPOSITION_DISABLED; 5355 coding->composing = COMPOSITION_DISABLED;
4858 } 5356 }
4859 5357
4860 if (SBYTES (XPROCESS (proc)->encoding_buf) < require) 5358 if (SBYTES (p->encoding_buf) < require)
4861 XPROCESS (proc)->encoding_buf = make_uninit_string (require); 5359 p->encoding_buf = make_uninit_string (require);
4862 5360
4863 if (from_byte >= 0) 5361 if (from_byte >= 0)
4864 buf = (BUFFERP (object) 5362 buf = (BUFFERP (object)
4865 ? BUF_BYTE_ADDRESS (XBUFFER (object), from_byte) 5363 ? BUF_BYTE_ADDRESS (XBUFFER (object), from_byte)
4866 : SDATA (object) + from_byte); 5364 : SDATA (object) + from_byte);
4867 5365
4868 object = XPROCESS (proc)->encoding_buf; 5366 object = p->encoding_buf;
4869 encode_coding (coding, (char *) buf, SDATA (object), 5367 encode_coding (coding, (char *) buf, SDATA (object),
4870 len, SBYTES (object)); 5368 len, SBYTES (object));
5369 coding_free_composition_data (coding);
4871 len = coding->produced; 5370 len = coding->produced;
4872 buf = SDATA (object); 5371 buf = SDATA (object);
4873 if (temp_buf)
4874 xfree (temp_buf);
4875 } 5372 }
4876 5373
4877 #ifdef VMS 5374 #ifdef VMS
4878 vs = get_vms_process_pointer (p->pid); 5375 vs = get_vms_process_pointer (p->pid);
4879 if (vs == 0) 5376 if (vs == 0)
4883 #else /* not VMS */ 5380 #else /* not VMS */
4884 5381
4885 if (pty_max_bytes == 0) 5382 if (pty_max_bytes == 0)
4886 { 5383 {
4887 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON) 5384 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
4888 pty_max_bytes = fpathconf (XFASTINT (XPROCESS (proc)->outfd), 5385 pty_max_bytes = fpathconf (XFASTINT (p->outfd), _PC_MAX_CANON);
4889 _PC_MAX_CANON);
4890 if (pty_max_bytes < 0) 5386 if (pty_max_bytes < 0)
4891 pty_max_bytes = 250; 5387 pty_max_bytes = 250;
4892 #else 5388 #else
4893 pty_max_bytes = 250; 5389 pty_max_bytes = 250;
4894 #endif 5390 #endif
4903 { 5399 {
4904 process_sent_to = proc; 5400 process_sent_to = proc;
4905 while (len > 0) 5401 while (len > 0)
4906 { 5402 {
4907 int this = len; 5403 int this = len;
4908 SIGTYPE (*old_sigpipe)();
4909 5404
4910 /* Decide how much data we can send in one batch. 5405 /* Decide how much data we can send in one batch.
4911 Long lines need to be split into multiple batches. */ 5406 Long lines need to be split into multiple batches. */
4912 if (!NILP (XPROCESS (proc)->pty_flag)) 5407 if (!NILP (p->pty_flag))
4913 { 5408 {
4914 /* Starting this at zero is always correct when not the first 5409 /* Starting this at zero is always correct when not the first
4915 iteration because the previous iteration ended by sending C-d. 5410 iteration because the previous iteration ended by sending C-d.
4916 It may not be correct for the first iteration 5411 It may not be correct for the first iteration
4917 if a partial line was sent in a separate send_process call. 5412 if a partial line was sent in a separate send_process call.
4936 } 5431 }
4937 5432
4938 /* Send this batch, using one or more write calls. */ 5433 /* Send this batch, using one or more write calls. */
4939 while (this > 0) 5434 while (this > 0)
4940 { 5435 {
4941 int outfd = XINT (XPROCESS (proc)->outfd); 5436 int outfd = XINT (p->outfd);
4942 old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap); 5437 old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
4943 #ifdef DATAGRAM_SOCKETS 5438 #ifdef DATAGRAM_SOCKETS
4944 if (DATAGRAM_CHAN_P (outfd)) 5439 if (DATAGRAM_CHAN_P (outfd))
4945 { 5440 {
4946 rv = sendto (outfd, (char *) buf, this, 5441 rv = sendto (outfd, (char *) buf, this,
4947 0, datagram_address[outfd].sa, 5442 0, datagram_address[outfd].sa,
4948 datagram_address[outfd].len); 5443 datagram_address[outfd].len);
4949 if (rv < 0 && errno == EMSGSIZE) 5444 if (rv < 0 && errno == EMSGSIZE)
4950 report_file_error ("sending datagram", Fcons (proc, Qnil)); 5445 {
5446 signal (SIGPIPE, old_sigpipe);
5447 report_file_error ("sending datagram",
5448 Fcons (proc, Qnil));
5449 }
4951 } 5450 }
4952 else 5451 else
4953 #endif 5452 #endif
4954 rv = emacs_write (outfd, (char *) buf, this); 5453 {
5454 rv = emacs_write (outfd, (char *) buf, this);
5455 #ifdef ADAPTIVE_READ_BUFFERING
5456 if (XINT (p->read_output_delay) > 0
5457 && EQ (p->adaptive_read_buffering, Qt))
5458 {
5459 XSETFASTINT (p->read_output_delay, 0);
5460 process_output_delay_count--;
5461 p->read_output_skip = Qnil;
5462 }
5463 #endif
5464 }
4955 signal (SIGPIPE, old_sigpipe); 5465 signal (SIGPIPE, old_sigpipe);
4956 5466
4957 if (rv < 0) 5467 if (rv < 0)
4958 { 5468 {
4959 if (0 5469 if (0
4966 ) 5476 )
4967 /* Buffer is full. Wait, accepting input; 5477 /* Buffer is full. Wait, accepting input;
4968 that may allow the program 5478 that may allow the program
4969 to finish doing output and read more. */ 5479 to finish doing output and read more. */
4970 { 5480 {
4971 Lisp_Object zero;
4972 int offset = 0; 5481 int offset = 0;
4973 5482
4974 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN 5483 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
4975 /* A gross hack to work around a bug in FreeBSD. 5484 /* A gross hack to work around a bug in FreeBSD.
4976 In the following sequence, read(2) returns 5485 In the following sequence, read(2) returns
4990 later read(2), without the CRs. */ 5499 later read(2), without the CRs. */
4991 5500
4992 if (errno == EAGAIN) 5501 if (errno == EAGAIN)
4993 { 5502 {
4994 int flags = FWRITE; 5503 int flags = FWRITE;
4995 ioctl (XINT (XPROCESS (proc)->outfd), TIOCFLUSH, 5504 ioctl (XINT (p->outfd), TIOCFLUSH, &flags);
4996 &flags);
4997 } 5505 }
4998 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */ 5506 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
4999 5507
5000 /* Running filters might relocate buffers or strings. 5508 /* Running filters might relocate buffers or strings.
5001 Arrange to relocate BUF. */ 5509 Arrange to relocate BUF. */
5002 if (BUFFERP (object)) 5510 if (BUFFERP (object))
5003 offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf); 5511 offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
5004 else if (STRINGP (object)) 5512 else if (STRINGP (object))
5005 offset = buf - SDATA (object); 5513 offset = buf - SDATA (object);
5006 5514
5007 XSETFASTINT (zero, 0);
5008 #ifdef EMACS_HAS_USECS 5515 #ifdef EMACS_HAS_USECS
5009 wait_reading_process_input (0, 20000, zero, 0); 5516 wait_reading_process_output (0, 20000, 0, 0, Qnil, NULL, 0);
5010 #else 5517 #else
5011 wait_reading_process_input (1, 0, zero, 0); 5518 wait_reading_process_output (1, 0, 0, 0, Qnil, NULL, 0);
5012 #endif 5519 #endif
5013 5520
5014 if (BUFFERP (object)) 5521 if (BUFFERP (object))
5015 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset); 5522 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
5016 else if (STRINGP (object)) 5523 else if (STRINGP (object))
5034 } 5541 }
5035 } 5542 }
5036 #endif /* not VMS */ 5543 #endif /* not VMS */
5037 else 5544 else
5038 { 5545 {
5546 signal (SIGPIPE, old_sigpipe);
5039 #ifndef VMS 5547 #ifndef VMS
5040 proc = process_sent_to; 5548 proc = process_sent_to;
5041 #endif 5549 p = XPROCESS (proc);
5042 XPROCESS (proc)->raw_status_low = Qnil; 5550 #endif
5043 XPROCESS (proc)->raw_status_high = Qnil; 5551 p->raw_status_low = Qnil;
5044 XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil)); 5552 p->raw_status_high = Qnil;
5045 XSETINT (XPROCESS (proc)->tick, ++process_tick); 5553 p->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
5554 XSETINT (p->tick, ++process_tick);
5046 deactivate_process (proc); 5555 deactivate_process (proc);
5047 #ifdef VMS 5556 #ifdef VMS
5048 error ("Error writing to process %s; closed it", 5557 error ("Error writing to process %s; closed it", SDATA (p->name));
5049 SDATA (XPROCESS (proc)->name));
5050 #else 5558 #else
5051 error ("SIGPIPE raised on process %s; closed it", 5559 error ("SIGPIPE raised on process %s; closed it", SDATA (p->name));
5052 SDATA (XPROCESS (proc)->name));
5053 #endif 5560 #endif
5054 } 5561 }
5055 5562
5056 UNGCPRO; 5563 UNGCPRO;
5057 } 5564 }
5102 send_process (proc, SDATA (string), 5609 send_process (proc, SDATA (string),
5103 SBYTES (string), string); 5610 SBYTES (string), string);
5104 return Qnil; 5611 return Qnil;
5105 } 5612 }
5106 5613
5614 /* Return the foreground process group for the tty/pty that
5615 the process P uses. */
5616 static int
5617 emacs_get_tty_pgrp (p)
5618 struct Lisp_Process *p;
5619 {
5620 int gid = -1;
5621
5622 #ifdef TIOCGPGRP
5623 if (ioctl (XINT (p->infd), TIOCGPGRP, &gid) == -1 && ! NILP (p->tty_name))
5624 {
5625 int fd;
5626 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5627 master side. Try the slave side. */
5628 fd = emacs_open (XSTRING (p->tty_name)->data, O_RDONLY, 0);
5629
5630 if (fd != -1)
5631 {
5632 ioctl (fd, TIOCGPGRP, &gid);
5633 emacs_close (fd);
5634 }
5635 }
5636 #endif /* defined (TIOCGPGRP ) */
5637
5638 return gid;
5639 }
5640
5107 DEFUN ("process-running-child-p", Fprocess_running_child_p, 5641 DEFUN ("process-running-child-p", Fprocess_running_child_p,
5108 Sprocess_running_child_p, 0, 1, 0, 5642 Sprocess_running_child_p, 0, 1, 0,
5109 doc: /* Return t if PROCESS has given the terminal to a child. 5643 doc: /* Return t if PROCESS has given the terminal to a child.
5110 If the operating system does not make it possible to find out, 5644 If the operating system does not make it possible to find out,
5111 return t unconditionally. */) 5645 return t unconditionally. */)
5112 (process) 5646 (process)
5113 Lisp_Object process; 5647 Lisp_Object process;
5114 { 5648 {
5115 /* Initialize in case ioctl doesn't exist or gives an error, 5649 /* Initialize in case ioctl doesn't exist or gives an error,
5116 in a way that will cause returning t. */ 5650 in a way that will cause returning t. */
5117 int gid = 0; 5651 int gid;
5118 Lisp_Object proc; 5652 Lisp_Object proc;
5119 struct Lisp_Process *p; 5653 struct Lisp_Process *p;
5120 5654
5121 proc = get_process (process); 5655 proc = get_process (process);
5122 p = XPROCESS (proc); 5656 p = XPROCESS (proc);
5126 SDATA (p->name)); 5660 SDATA (p->name));
5127 if (XINT (p->infd) < 0) 5661 if (XINT (p->infd) < 0)
5128 error ("Process %s is not active", 5662 error ("Process %s is not active",
5129 SDATA (p->name)); 5663 SDATA (p->name));
5130 5664
5131 #ifdef TIOCGPGRP 5665 gid = emacs_get_tty_pgrp (p);
5132 if (!NILP (p->subtty))
5133 ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
5134 else
5135 ioctl (XINT (p->infd), TIOCGPGRP, &gid);
5136 #endif /* defined (TIOCGPGRP ) */
5137 5666
5138 if (gid == XFASTINT (p->pid)) 5667 if (gid == XFASTINT (p->pid))
5139 return Qnil; 5668 return Qnil;
5140 return Qt; 5669 return Qt;
5141 } 5670 }
5191 5720
5192 /* TERMIOS is the latest and bestest, and seems most likely to 5721 /* TERMIOS is the latest and bestest, and seems most likely to
5193 work. If the system has it, use it. */ 5722 work. If the system has it, use it. */
5194 #ifdef HAVE_TERMIOS 5723 #ifdef HAVE_TERMIOS
5195 struct termios t; 5724 struct termios t;
5725 cc_t *sig_char = NULL;
5726
5727 tcgetattr (XINT (p->infd), &t);
5196 5728
5197 switch (signo) 5729 switch (signo)
5198 { 5730 {
5199 case SIGINT: 5731 case SIGINT:
5200 tcgetattr (XINT (p->infd), &t); 5732 sig_char = &t.c_cc[VINTR];
5201 send_process (proc, &t.c_cc[VINTR], 1, Qnil); 5733 break;
5734
5735 case SIGQUIT:
5736 sig_char = &t.c_cc[VQUIT];
5737 break;
5738
5739 case SIGTSTP:
5740 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5741 sig_char = &t.c_cc[VSWTCH];
5742 #else
5743 sig_char = &t.c_cc[VSUSP];
5744 #endif
5745 break;
5746 }
5747
5748 if (sig_char && *sig_char != CDISABLE)
5749 {
5750 send_process (proc, sig_char, 1, Qnil);
5202 return; 5751 return;
5203 5752 }
5204 case SIGQUIT: 5753 /* If we can't send the signal with a character,
5205 tcgetattr (XINT (p->infd), &t); 5754 fall through and send it another way. */
5206 send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
5207 return;
5208
5209 case SIGTSTP:
5210 tcgetattr (XINT (p->infd), &t);
5211 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5212 send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
5213 #else
5214 send_process (proc, &t.c_cc[VSUSP], 1, Qnil);
5215 #endif
5216 return;
5217 }
5218
5219 #else /* ! HAVE_TERMIOS */ 5755 #else /* ! HAVE_TERMIOS */
5220 5756
5221 /* On Berkeley descendants, the following IOCTL's retrieve the 5757 /* On Berkeley descendants, the following IOCTL's retrieve the
5222 current control characters. */ 5758 current control characters. */
5223 #if defined (TIOCGLTC) && defined (TIOCGETC) 5759 #if defined (TIOCGLTC) && defined (TIOCGETC)
5270 Your configuration files are messed up. 5806 Your configuration files are messed up.
5271 /* If your system configuration files define SIGNALS_VIA_CHARACTERS, 5807 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
5272 you'd better be using one of the alternatives above! */ 5808 you'd better be using one of the alternatives above! */
5273 #endif /* ! defined (TCGETA) */ 5809 #endif /* ! defined (TCGETA) */
5274 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */ 5810 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5811 /* In this case, the code above should alway returns. */
5812 abort ();
5275 #endif /* ! defined HAVE_TERMIOS */ 5813 #endif /* ! defined HAVE_TERMIOS */
5276 abort (); 5814
5277 /* The code above always returns from the function. */ 5815 /* The code above may fall through if it can't
5816 handle the signal. */
5278 #endif /* defined (SIGNALS_VIA_CHARACTERS) */ 5817 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5279 5818
5280 #ifdef TIOCGPGRP 5819 #ifdef TIOCGPGRP
5281 /* Get the current pgrp using the tty itself, if we have that. 5820 /* Get the current pgrp using the tty itself, if we have that.
5282 Otherwise, use the pty to get the pgrp. 5821 Otherwise, use the pty to get the pgrp.
5283 On pfa systems, saka@pfu.fujitsu.co.JP writes: 5822 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5284 "TIOCGPGRP symbol defined in sys/ioctl.h at E50. 5823 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5285 But, TIOCGPGRP does not work on E50 ;-P works fine on E60" 5824 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5286 His patch indicates that if TIOCGPGRP returns an error, then 5825 His patch indicates that if TIOCGPGRP returns an error, then
5287 we should just assume that p->pid is also the process group id. */ 5826 we should just assume that p->pid is also the process group id. */
5288 { 5827
5289 int err; 5828 gid = emacs_get_tty_pgrp (p);
5290 5829
5291 if (!NILP (p->subtty)) 5830 if (gid == -1)
5292 err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid); 5831 /* If we can't get the information, assume
5293 else 5832 the shell owns the tty. */
5294 err = ioctl (XINT (p->infd), TIOCGPGRP, &gid); 5833 gid = XFASTINT (p->pid);
5295
5296 if (err == -1)
5297 /* If we can't get the information, assume
5298 the shell owns the tty. */
5299 gid = XFASTINT (p->pid);
5300 }
5301 5834
5302 /* It is not clear whether anything really can set GID to -1. 5835 /* It is not clear whether anything really can set GID to -1.
5303 Perhaps on some system one of those ioctls can or could do so. 5836 Perhaps on some system one of those ioctls can or could do so.
5304 Or perhaps this is vestigial. */ 5837 Or perhaps this is vestigial. */
5305 if (gid == -1) 5838 if (gid == -1)
5323 p->raw_status_low = Qnil; 5856 p->raw_status_low = Qnil;
5324 p->raw_status_high = Qnil; 5857 p->raw_status_high = Qnil;
5325 p->status = Qrun; 5858 p->status = Qrun;
5326 XSETINT (p->tick, ++process_tick); 5859 XSETINT (p->tick, ++process_tick);
5327 if (!nomsg) 5860 if (!nomsg)
5328 status_notify (); 5861 status_notify (NULL);
5329 break; 5862 break;
5330 #endif /* ! defined (SIGCONT) */ 5863 #endif /* ! defined (SIGCONT) */
5331 case SIGINT: 5864 case SIGINT:
5332 #ifdef VMS 5865 #ifdef VMS
5333 send_process (proc, "\003", 1, Qnil); /* ^C */ 5866 send_process (proc, "\003", 1, Qnil); /* ^C */
5357 } 5890 }
5358 5891
5359 /* gid may be a pid, or minus a pgrp's number */ 5892 /* gid may be a pid, or minus a pgrp's number */
5360 #ifdef TIOCSIGSEND 5893 #ifdef TIOCSIGSEND
5361 if (!NILP (current_group)) 5894 if (!NILP (current_group))
5362 ioctl (XINT (p->infd), TIOCSIGSEND, signo); 5895 {
5896 if (ioctl (XINT (p->infd), TIOCSIGSEND, signo) == -1)
5897 EMACS_KILLPG (gid, signo);
5898 }
5363 else 5899 else
5364 { 5900 {
5365 gid = - XFASTINT (p->pid); 5901 gid = - XFASTINT (p->pid);
5366 kill (gid, signo); 5902 kill (gid, signo);
5367 } 5903 }
5371 } 5907 }
5372 5908
5373 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0, 5909 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
5374 doc: /* Interrupt process PROCESS. 5910 doc: /* Interrupt process PROCESS.
5375 PROCESS may be a process, a buffer, or the name of a process or buffer. 5911 PROCESS may be a process, a buffer, or the name of a process or buffer.
5376 nil or no arg means current buffer's process. 5912 No arg or nil means current buffer's process.
5377 Second arg CURRENT-GROUP non-nil means send signal to 5913 Second arg CURRENT-GROUP non-nil means send signal to
5378 the current process-group of the process's controlling terminal 5914 the current process-group of the process's controlling terminal
5379 rather than to the process's own process group. 5915 rather than to the process's own process group.
5380 If the process is a shell, this means interrupt current subjob 5916 If the process is a shell, this means interrupt current subjob
5381 rather than the shell. 5917 rather than the shell.
5431 p->command = Qt; 5967 p->command = Qt;
5432 return process; 5968 return process;
5433 } 5969 }
5434 #endif 5970 #endif
5435 #ifndef SIGTSTP 5971 #ifndef SIGTSTP
5436 error ("no SIGTSTP support"); 5972 error ("No SIGTSTP support");
5437 #else 5973 #else
5438 process_send_signal (process, SIGTSTP, current_group, 0); 5974 process_send_signal (process, SIGTSTP, current_group, 0);
5439 #endif 5975 #endif
5440 return process; 5976 return process;
5441 } 5977 }
5465 } 6001 }
5466 #endif 6002 #endif
5467 #ifdef SIGCONT 6003 #ifdef SIGCONT
5468 process_send_signal (process, SIGCONT, current_group, 0); 6004 process_send_signal (process, SIGCONT, current_group, 0);
5469 #else 6005 #else
5470 error ("no SIGCONT support"); 6006 error ("No SIGCONT support");
5471 #endif 6007 #endif
5472 return process; 6008 return process;
5473 } 6009 }
5474 6010
5475 DEFUN ("signal-process", Fsignal_process, Ssignal_process, 6011 DEFUN ("signal-process", Fsignal_process, Ssignal_process,
5525 unsigned char *name; 6061 unsigned char *name;
5526 6062
5527 CHECK_SYMBOL (sigcode); 6063 CHECK_SYMBOL (sigcode);
5528 name = SDATA (SYMBOL_NAME (sigcode)); 6064 name = SDATA (SYMBOL_NAME (sigcode));
5529 6065
6066 if (!strncmp(name, "SIG", 3))
6067 name += 3;
6068
5530 if (0) 6069 if (0)
5531 ; 6070 ;
5532 #ifdef SIGHUP 6071 #ifdef SIGHUP
5533 handle_signal ("SIGHUP", SIGHUP); 6072 handle_signal ("HUP", SIGHUP);
5534 #endif 6073 #endif
5535 #ifdef SIGINT 6074 #ifdef SIGINT
5536 handle_signal ("SIGINT", SIGINT); 6075 handle_signal ("INT", SIGINT);
5537 #endif 6076 #endif
5538 #ifdef SIGQUIT 6077 #ifdef SIGQUIT
5539 handle_signal ("SIGQUIT", SIGQUIT); 6078 handle_signal ("QUIT", SIGQUIT);
5540 #endif 6079 #endif
5541 #ifdef SIGILL 6080 #ifdef SIGILL
5542 handle_signal ("SIGILL", SIGILL); 6081 handle_signal ("ILL", SIGILL);
5543 #endif 6082 #endif
5544 #ifdef SIGABRT 6083 #ifdef SIGABRT
5545 handle_signal ("SIGABRT", SIGABRT); 6084 handle_signal ("ABRT", SIGABRT);
5546 #endif 6085 #endif
5547 #ifdef SIGEMT 6086 #ifdef SIGEMT
5548 handle_signal ("SIGEMT", SIGEMT); 6087 handle_signal ("EMT", SIGEMT);
5549 #endif 6088 #endif
5550 #ifdef SIGKILL 6089 #ifdef SIGKILL
5551 handle_signal ("SIGKILL", SIGKILL); 6090 handle_signal ("KILL", SIGKILL);
5552 #endif 6091 #endif
5553 #ifdef SIGFPE 6092 #ifdef SIGFPE
5554 handle_signal ("SIGFPE", SIGFPE); 6093 handle_signal ("FPE", SIGFPE);
5555 #endif 6094 #endif
5556 #ifdef SIGBUS 6095 #ifdef SIGBUS
5557 handle_signal ("SIGBUS", SIGBUS); 6096 handle_signal ("BUS", SIGBUS);
5558 #endif 6097 #endif
5559 #ifdef SIGSEGV 6098 #ifdef SIGSEGV
5560 handle_signal ("SIGSEGV", SIGSEGV); 6099 handle_signal ("SEGV", SIGSEGV);
5561 #endif 6100 #endif
5562 #ifdef SIGSYS 6101 #ifdef SIGSYS
5563 handle_signal ("SIGSYS", SIGSYS); 6102 handle_signal ("SYS", SIGSYS);
5564 #endif 6103 #endif
5565 #ifdef SIGPIPE 6104 #ifdef SIGPIPE
5566 handle_signal ("SIGPIPE", SIGPIPE); 6105 handle_signal ("PIPE", SIGPIPE);
5567 #endif 6106 #endif
5568 #ifdef SIGALRM 6107 #ifdef SIGALRM
5569 handle_signal ("SIGALRM", SIGALRM); 6108 handle_signal ("ALRM", SIGALRM);
5570 #endif 6109 #endif
5571 #ifdef SIGTERM 6110 #ifdef SIGTERM
5572 handle_signal ("SIGTERM", SIGTERM); 6111 handle_signal ("TERM", SIGTERM);
5573 #endif 6112 #endif
5574 #ifdef SIGURG 6113 #ifdef SIGURG
5575 handle_signal ("SIGURG", SIGURG); 6114 handle_signal ("URG", SIGURG);
5576 #endif 6115 #endif
5577 #ifdef SIGSTOP 6116 #ifdef SIGSTOP
5578 handle_signal ("SIGSTOP", SIGSTOP); 6117 handle_signal ("STOP", SIGSTOP);
5579 #endif 6118 #endif
5580 #ifdef SIGTSTP 6119 #ifdef SIGTSTP
5581 handle_signal ("SIGTSTP", SIGTSTP); 6120 handle_signal ("TSTP", SIGTSTP);
5582 #endif 6121 #endif
5583 #ifdef SIGCONT 6122 #ifdef SIGCONT
5584 handle_signal ("SIGCONT", SIGCONT); 6123 handle_signal ("CONT", SIGCONT);
5585 #endif 6124 #endif
5586 #ifdef SIGCHLD 6125 #ifdef SIGCHLD
5587 handle_signal ("SIGCHLD", SIGCHLD); 6126 handle_signal ("CHLD", SIGCHLD);
5588 #endif 6127 #endif
5589 #ifdef SIGTTIN 6128 #ifdef SIGTTIN
5590 handle_signal ("SIGTTIN", SIGTTIN); 6129 handle_signal ("TTIN", SIGTTIN);
5591 #endif 6130 #endif
5592 #ifdef SIGTTOU 6131 #ifdef SIGTTOU
5593 handle_signal ("SIGTTOU", SIGTTOU); 6132 handle_signal ("TTOU", SIGTTOU);
5594 #endif 6133 #endif
5595 #ifdef SIGIO 6134 #ifdef SIGIO
5596 handle_signal ("SIGIO", SIGIO); 6135 handle_signal ("IO", SIGIO);
5597 #endif 6136 #endif
5598 #ifdef SIGXCPU 6137 #ifdef SIGXCPU
5599 handle_signal ("SIGXCPU", SIGXCPU); 6138 handle_signal ("XCPU", SIGXCPU);
5600 #endif 6139 #endif
5601 #ifdef SIGXFSZ 6140 #ifdef SIGXFSZ
5602 handle_signal ("SIGXFSZ", SIGXFSZ); 6141 handle_signal ("XFSZ", SIGXFSZ);
5603 #endif 6142 #endif
5604 #ifdef SIGVTALRM 6143 #ifdef SIGVTALRM
5605 handle_signal ("SIGVTALRM", SIGVTALRM); 6144 handle_signal ("VTALRM", SIGVTALRM);
5606 #endif 6145 #endif
5607 #ifdef SIGPROF 6146 #ifdef SIGPROF
5608 handle_signal ("SIGPROF", SIGPROF); 6147 handle_signal ("PROF", SIGPROF);
5609 #endif 6148 #endif
5610 #ifdef SIGWINCH 6149 #ifdef SIGWINCH
5611 handle_signal ("SIGWINCH", SIGWINCH); 6150 handle_signal ("WINCH", SIGWINCH);
5612 #endif 6151 #endif
5613 #ifdef SIGINFO 6152 #ifdef SIGINFO
5614 handle_signal ("SIGINFO", SIGINFO); 6153 handle_signal ("INFO", SIGINFO);
5615 #endif 6154 #endif
5616 #ifdef SIGUSR1 6155 #ifdef SIGUSR1
5617 handle_signal ("SIGUSR1", SIGUSR1); 6156 handle_signal ("USR1", SIGUSR1);
5618 #endif 6157 #endif
5619 #ifdef SIGUSR2 6158 #ifdef SIGUSR2
5620 handle_signal ("SIGUSR2", SIGUSR2); 6159 handle_signal ("USR2", SIGUSR2);
5621 #endif 6160 #endif
5622 else 6161 else
5623 error ("Undefined signal name %s", name); 6162 error ("Undefined signal name %s", name);
5624 } 6163 }
5625 6164
5742 handler will be called again, resulting in an infinite loop. The 6281 handler will be called again, resulting in an infinite loop. The
5743 relevant portion of the documentation reads "SIGCLD signals will be 6282 relevant portion of the documentation reads "SIGCLD signals will be
5744 queued and the signal-catching function will be continually 6283 queued and the signal-catching function will be continually
5745 reentered until the queue is empty". Invoking signal() causes the 6284 reentered until the queue is empty". Invoking signal() causes the
5746 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems 6285 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
5747 Inc. */ 6286 Inc.
6287
6288 ** Malloc WARNING: This should never call malloc either directly or
6289 indirectly; if it does, that is a bug */
5748 6290
5749 SIGTYPE 6291 SIGTYPE
5750 sigchld_handler (signo) 6292 sigchld_handler (signo)
5751 int signo; 6293 int signo;
5752 { 6294 {
5753 int old_errno = errno; 6295 int old_errno = errno;
5754 Lisp_Object proc; 6296 Lisp_Object proc;
5755 register struct Lisp_Process *p; 6297 register struct Lisp_Process *p;
5756 extern EMACS_TIME *input_available_clear_time; 6298 extern EMACS_TIME *input_available_clear_time;
6299
6300 SIGNAL_THREAD_CHECK (signo);
5757 6301
5758 #ifdef BSD4_1 6302 #ifdef BSD4_1
5759 extern int sigheld; 6303 extern int sigheld;
5760 sigheld |= sigbit (SIGCHLD); 6304 sigheld |= sigbit (SIGCHLD);
5761 #endif 6305 #endif
5844 { 6388 {
5845 FD_CLR (XINT (p->infd), &input_wait_mask); 6389 FD_CLR (XINT (p->infd), &input_wait_mask);
5846 FD_CLR (XINT (p->infd), &non_keyboard_wait_mask); 6390 FD_CLR (XINT (p->infd), &non_keyboard_wait_mask);
5847 } 6391 }
5848 6392
5849 /* Tell wait_reading_process_input that it needs to wake up and 6393 /* Tell wait_reading_process_output that it needs to wake up and
5850 look around. */ 6394 look around. */
5851 if (input_available_clear_time) 6395 if (input_available_clear_time)
5852 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); 6396 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5853 } 6397 }
5854 6398
5860 6404
5861 /* Report the status of the synchronous process. */ 6405 /* Report the status of the synchronous process. */
5862 if (WIFEXITED (w)) 6406 if (WIFEXITED (w))
5863 synch_process_retcode = WRETCODE (w); 6407 synch_process_retcode = WRETCODE (w);
5864 else if (WIFSIGNALED (w)) 6408 else if (WIFSIGNALED (w))
5865 { 6409 synch_process_termsig = WTERMSIG (w);
5866 int code = WTERMSIG (w); 6410
5867 char *signame; 6411 /* Tell wait_reading_process_output that it needs to wake up and
5868
5869 synchronize_system_messages_locale ();
5870 signame = strsignal (code);
5871
5872 if (signame == 0)
5873 signame = "unknown";
5874
5875 synch_process_death = signame;
5876 }
5877
5878 /* Tell wait_reading_process_input that it needs to wake up and
5879 look around. */ 6412 look around. */
5880 if (input_available_clear_time) 6413 if (input_available_clear_time)
5881 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); 6414 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
5882 } 6415 }
5883 6416
5950 and we already saved the match data nonrecursively, 6483 and we already saved the match data nonrecursively,
5951 save the same match data in safely recursive fashion. */ 6484 save the same match data in safely recursive fashion. */
5952 if (outer_running_asynch_code) 6485 if (outer_running_asynch_code)
5953 { 6486 {
5954 Lisp_Object tem; 6487 Lisp_Object tem;
5955 tem = Fmatch_data (Qnil, Qnil); 6488 tem = Fmatch_data (Qnil, Qnil, Qnil);
5956 restore_match_data (); 6489 restore_search_regs ();
5957 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil)); 6490 record_unwind_save_match_data ();
5958 Fset_match_data (tem); 6491 Fset_match_data (tem, Qt);
5959 } 6492 }
5960 6493
5961 /* For speed, if a search happens within this code, 6494 /* For speed, if a search happens within this code,
5962 save the match data in a special nonrecursive fashion. */ 6495 save the match data in a special nonrecursive fashion. */
5963 running_asynch_code = 1; 6496 running_asynch_code = 1;
5967 Fcons (proc, Fcons (reason, Qnil))), 6500 Fcons (proc, Fcons (reason, Qnil))),
5968 !NILP (Vdebug_on_error) ? Qnil : Qerror, 6501 !NILP (Vdebug_on_error) ? Qnil : Qerror,
5969 exec_sentinel_error_handler); 6502 exec_sentinel_error_handler);
5970 6503
5971 /* If we saved the match data nonrecursively, restore it now. */ 6504 /* If we saved the match data nonrecursively, restore it now. */
5972 restore_match_data (); 6505 restore_search_regs ();
5973 running_asynch_code = outer_running_asynch_code; 6506 running_asynch_code = outer_running_asynch_code;
5974 6507
5975 Vdeactivate_mark = odeactivate; 6508 Vdeactivate_mark = odeactivate;
5976 6509
5977 /* Restore waiting_for_user_input_p as it was 6510 /* Restore waiting_for_user_input_p as it was
5994 /* Report all recent events of a change in process status 6527 /* Report all recent events of a change in process status
5995 (either run the sentinel or output a message). 6528 (either run the sentinel or output a message).
5996 This is usually done while Emacs is waiting for keyboard input 6529 This is usually done while Emacs is waiting for keyboard input
5997 but can be done at other times. */ 6530 but can be done at other times. */
5998 6531
5999 void 6532 static void
6000 status_notify () 6533 status_notify (deleting_process)
6534 struct Lisp_Process *deleting_process;
6001 { 6535 {
6002 register Lisp_Object proc, buffer; 6536 register Lisp_Object proc, buffer;
6003 Lisp_Object tail, msg; 6537 Lisp_Object tail, msg;
6004 struct gcpro gcpro1, gcpro2; 6538 struct gcpro gcpro1, gcpro2;
6005 6539
6031 while (! EQ (p->filter, Qt) 6565 while (! EQ (p->filter, Qt)
6032 && ! EQ (p->status, Qconnect) 6566 && ! EQ (p->status, Qconnect)
6033 && ! EQ (p->status, Qlisten) 6567 && ! EQ (p->status, Qlisten)
6034 && ! EQ (p->command, Qt) /* Network process not stopped. */ 6568 && ! EQ (p->command, Qt) /* Network process not stopped. */
6035 && XINT (p->infd) >= 0 6569 && XINT (p->infd) >= 0
6570 && p != deleting_process
6036 && read_process_output (proc, XINT (p->infd)) > 0); 6571 && read_process_output (proc, XINT (p->infd)) > 0);
6037 6572
6038 buffer = p->buffer; 6573 buffer = p->buffer;
6039 6574
6040 /* Get the text to use for the message. */ 6575 /* Get the text to use for the message. */
6041 if (!NILP (p->raw_status_low)) 6576 if (!NILP (p->raw_status_low))
6042 update_status (p); 6577 update_status (p);
6043 msg = status_message (p->status); 6578 msg = status_message (p);
6044 6579
6045 /* If process is terminated, deactivate it or delete it. */ 6580 /* If process is terminated, deactivate it or delete it. */
6046 symbol = p->status; 6581 symbol = p->status;
6047 if (CONSP (p->status)) 6582 if (CONSP (p->status))
6048 symbol = XCAR (p->status); 6583 symbol = XCAR (p->status);
6124 DEFUN ("set-process-coding-system", Fset_process_coding_system, 6659 DEFUN ("set-process-coding-system", Fset_process_coding_system,
6125 Sset_process_coding_system, 1, 3, 0, 6660 Sset_process_coding_system, 1, 3, 0,
6126 doc: /* Set coding systems of PROCESS to DECODING and ENCODING. 6661 doc: /* Set coding systems of PROCESS to DECODING and ENCODING.
6127 DECODING will be used to decode subprocess output and ENCODING to 6662 DECODING will be used to decode subprocess output and ENCODING to
6128 encode subprocess input. */) 6663 encode subprocess input. */)
6129 (proc, decoding, encoding) 6664 (process, decoding, encoding)
6130 register Lisp_Object proc, decoding, encoding; 6665 register Lisp_Object process, decoding, encoding;
6131 { 6666 {
6132 register struct Lisp_Process *p; 6667 register struct Lisp_Process *p;
6133 6668
6134 CHECK_PROCESS (proc); 6669 CHECK_PROCESS (process);
6135 p = XPROCESS (proc); 6670 p = XPROCESS (process);
6136 if (XINT (p->infd) < 0) 6671 if (XINT (p->infd) < 0)
6137 error ("Input file descriptor of %s closed", SDATA (p->name)); 6672 error ("Input file descriptor of %s closed", SDATA (p->name));
6138 if (XINT (p->outfd) < 0) 6673 if (XINT (p->outfd) < 0)
6139 error ("Output file descriptor of %s closed", SDATA (p->name)); 6674 error ("Output file descriptor of %s closed", SDATA (p->name));
6140 Fcheck_coding_system (decoding); 6675 Fcheck_coding_system (decoding);
6141 Fcheck_coding_system (encoding); 6676 Fcheck_coding_system (encoding);
6142 6677
6143 p->decode_coding_system = decoding; 6678 p->decode_coding_system = decoding;
6144 p->encode_coding_system = encoding; 6679 p->encode_coding_system = encoding;
6145 setup_process_coding_systems (proc); 6680 setup_process_coding_systems (process);
6146 6681
6147 return Qnil; 6682 return Qnil;
6148 } 6683 }
6149 6684
6150 DEFUN ("process-coding-system", 6685 DEFUN ("process-coding-system",
6151 Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0, 6686 Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
6152 doc: /* Return a cons of coding systems for decoding and encoding of PROCESS. */) 6687 doc: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6153 (proc) 6688 (process)
6154 register Lisp_Object proc; 6689 register Lisp_Object process;
6155 { 6690 {
6156 CHECK_PROCESS (proc); 6691 CHECK_PROCESS (process);
6157 return Fcons (XPROCESS (proc)->decode_coding_system, 6692 return Fcons (XPROCESS (process)->decode_coding_system,
6158 XPROCESS (proc)->encode_coding_system); 6693 XPROCESS (process)->encode_coding_system);
6159 } 6694 }
6160 6695
6161 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte, 6696 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte,
6162 Sset_process_filter_multibyte, 2, 2, 0, 6697 Sset_process_filter_multibyte, 2, 2, 0,
6163 doc: /* Set multibyteness of the strings given to PROCESS's filter. 6698 doc: /* Set multibyteness of the strings given to PROCESS's filter.
6164 If FLAG is non-nil, the filter is given multibyte strings. 6699 If FLAG is non-nil, the filter is given multibyte strings.
6165 If FLAG is nil, the filter is given unibyte strings. In this case, 6700 If FLAG is nil, the filter is given unibyte strings. In this case,
6166 all character code conversion except for end-of-line conversion is 6701 all character code conversion except for end-of-line conversion is
6167 suppressed. */) 6702 suppressed. */)
6168 (proc, flag) 6703 (process, flag)
6169 Lisp_Object proc, flag; 6704 Lisp_Object process, flag;
6170 { 6705 {
6171 register struct Lisp_Process *p; 6706 register struct Lisp_Process *p;
6172 6707
6173 CHECK_PROCESS (proc); 6708 CHECK_PROCESS (process);
6174 p = XPROCESS (proc); 6709 p = XPROCESS (process);
6175 p->filter_multibyte = flag; 6710 p->filter_multibyte = flag;
6176 setup_process_coding_systems (proc); 6711 setup_process_coding_systems (process);
6177 6712
6178 return Qnil; 6713 return Qnil;
6179 } 6714 }
6180 6715
6181 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p, 6716 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p,
6182 Sprocess_filter_multibyte_p, 1, 1, 0, 6717 Sprocess_filter_multibyte_p, 1, 1, 0,
6183 doc: /* Return t if a multibyte string is given to PROCESS's filter.*/) 6718 doc: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6184 (proc) 6719 (process)
6185 Lisp_Object proc; 6720 Lisp_Object process;
6186 { 6721 {
6187 register struct Lisp_Process *p; 6722 register struct Lisp_Process *p;
6188 6723
6189 CHECK_PROCESS (proc); 6724 CHECK_PROCESS (process);
6190 p = XPROCESS (proc); 6725 p = XPROCESS (process);
6191 6726
6192 return (NILP (p->filter_multibyte) ? Qnil : Qt); 6727 return (NILP (p->filter_multibyte) ? Qnil : Qt);
6193 } 6728 }
6194 6729
6195 6730
6234 } 6769 }
6235 6770
6236 /* Return nonzero if *MASK has a bit set 6771 /* Return nonzero if *MASK has a bit set
6237 that corresponds to one of the keyboard input descriptors. */ 6772 that corresponds to one of the keyboard input descriptors. */
6238 6773
6239 int 6774 static int
6240 keyboard_bit_set (mask) 6775 keyboard_bit_set (mask)
6241 SELECT_TYPE *mask; 6776 SELECT_TYPE *mask;
6242 { 6777 {
6243 int fd; 6778 int fd;
6244 6779
6265 FD_ZERO (&input_wait_mask); 6800 FD_ZERO (&input_wait_mask);
6266 FD_ZERO (&non_keyboard_wait_mask); 6801 FD_ZERO (&non_keyboard_wait_mask);
6267 FD_ZERO (&non_process_wait_mask); 6802 FD_ZERO (&non_process_wait_mask);
6268 max_process_desc = 0; 6803 max_process_desc = 0;
6269 6804
6805 #ifdef NON_BLOCKING_CONNECT
6806 FD_ZERO (&connect_wait_mask);
6807 num_pending_connects = 0;
6808 #endif
6809
6810 #ifdef ADAPTIVE_READ_BUFFERING
6811 process_output_delay_count = 0;
6812 process_output_skip = 0;
6813 #endif
6814
6270 FD_SET (0, &input_wait_mask); 6815 FD_SET (0, &input_wait_mask);
6271 6816
6272 Vprocess_alist = Qnil; 6817 Vprocess_alist = Qnil;
6273 for (i = 0; i < MAXDESC; i++) 6818 for (i = 0; i < MAXDESC; i++)
6274 { 6819 {
6282 #endif 6827 #endif
6283 6828
6284 #ifdef HAVE_SOCKETS 6829 #ifdef HAVE_SOCKETS
6285 { 6830 {
6286 Lisp_Object subfeatures = Qnil; 6831 Lisp_Object subfeatures = Qnil;
6832 struct socket_options *sopt;
6833
6287 #define ADD_SUBFEATURE(key, val) \ 6834 #define ADD_SUBFEATURE(key, val) \
6288 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures) 6835 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
6289 6836
6290 #ifdef NON_BLOCKING_CONNECT 6837 #ifdef NON_BLOCKING_CONNECT
6291 ADD_SUBFEATURE (QCnowait, Qt); 6838 ADD_SUBFEATURE (QCnowait, Qt);
6294 ADD_SUBFEATURE (QCtype, Qdatagram); 6841 ADD_SUBFEATURE (QCtype, Qdatagram);
6295 #endif 6842 #endif
6296 #ifdef HAVE_LOCAL_SOCKETS 6843 #ifdef HAVE_LOCAL_SOCKETS
6297 ADD_SUBFEATURE (QCfamily, Qlocal); 6844 ADD_SUBFEATURE (QCfamily, Qlocal);
6298 #endif 6845 #endif
6846 ADD_SUBFEATURE (QCfamily, Qipv4);
6847 #ifdef AF_INET6
6848 ADD_SUBFEATURE (QCfamily, Qipv6);
6849 #endif
6299 #ifdef HAVE_GETSOCKNAME 6850 #ifdef HAVE_GETSOCKNAME
6300 ADD_SUBFEATURE (QCservice, Qt); 6851 ADD_SUBFEATURE (QCservice, Qt);
6301 #endif 6852 #endif
6302 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY)) 6853 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY))
6303 ADD_SUBFEATURE (QCserver, Qt); 6854 ADD_SUBFEATURE (QCserver, Qt);
6304 #endif 6855 #endif
6305 #ifdef SO_BINDTODEVICE 6856
6306 ADD_SUBFEATURE (QCoptions, intern ("bindtodevice")); 6857 for (sopt = socket_options; sopt->name; sopt++)
6307 #endif 6858 subfeatures = Fcons (intern (sopt->name), subfeatures);
6308 #ifdef SO_BROADCAST 6859
6309 ADD_SUBFEATURE (QCoptions, intern ("broadcast"));
6310 #endif
6311 #ifdef SO_DONTROUTE
6312 ADD_SUBFEATURE (QCoptions, intern ("dontroute"));
6313 #endif
6314 #ifdef SO_KEEPALIVE
6315 ADD_SUBFEATURE (QCoptions, intern ("keepalive"));
6316 #endif
6317 #ifdef SO_LINGER
6318 ADD_SUBFEATURE (QCoptions, intern ("linger"));
6319 #endif
6320 #ifdef SO_OOBINLINE
6321 ADD_SUBFEATURE (QCoptions, intern ("oobinline"));
6322 #endif
6323 #ifdef SO_PRIORITY
6324 ADD_SUBFEATURE (QCoptions, intern ("priority"));
6325 #endif
6326 #ifdef SO_REUSEADDR
6327 ADD_SUBFEATURE (QCoptions, intern ("reuseaddr"));
6328 #endif
6329 Fprovide (intern ("make-network-process"), subfeatures); 6860 Fprovide (intern ("make-network-process"), subfeatures);
6330 } 6861 }
6331 #endif /* HAVE_SOCKETS */ 6862 #endif /* HAVE_SOCKETS */
6863
6864 #if defined (DARWIN) || defined (MAC_OSX)
6865 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
6866 processes. As such, we only change the default value. */
6867 if (initialized)
6868 {
6869 char *release = get_operating_system_release();
6870 if (!release || !release[0] || (release[0] < MIN_PTY_KERNEL_VERSION
6871 && release[1] == '.')) {
6872 Vprocess_connection_type = Qnil;
6873 }
6874 }
6875 #endif
6332 } 6876 }
6333 6877
6334 void 6878 void
6335 syms_of_process () 6879 syms_of_process ()
6336 { 6880 {
6359 staticpro (&Qfailed); 6903 staticpro (&Qfailed);
6360 Qlisten = intern ("listen"); 6904 Qlisten = intern ("listen");
6361 staticpro (&Qlisten); 6905 staticpro (&Qlisten);
6362 Qlocal = intern ("local"); 6906 Qlocal = intern ("local");
6363 staticpro (&Qlocal); 6907 staticpro (&Qlocal);
6908 Qipv4 = intern ("ipv4");
6909 staticpro (&Qipv4);
6910 #ifdef AF_INET6
6911 Qipv6 = intern ("ipv6");
6912 staticpro (&Qipv6);
6913 #endif
6364 Qdatagram = intern ("datagram"); 6914 Qdatagram = intern ("datagram");
6365 staticpro (&Qdatagram); 6915 staticpro (&Qdatagram);
6366 6916
6367 QCname = intern (":name"); 6917 QCname = intern (":name");
6368 staticpro (&QCname); 6918 staticpro (&QCname);
6416 The value has no effect if the system has no ptys or if all ptys are busy: 6966 The value has no effect if the system has no ptys or if all ptys are busy:
6417 then a pipe is used in any case. 6967 then a pipe is used in any case.
6418 The value takes effect when `start-process' is called. */); 6968 The value takes effect when `start-process' is called. */);
6419 Vprocess_connection_type = Qt; 6969 Vprocess_connection_type = Qt;
6420 6970
6971 #ifdef ADAPTIVE_READ_BUFFERING
6972 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering,
6973 doc: /* If non-nil, improve receive buffering by delaying after short reads.
6974 On some systems, when Emacs reads the output from a subprocess, the output data
6975 is read in very small blocks, potentially resulting in very poor performance.
6976 This behavior can be remedied to some extent by setting this variable to a
6977 non-nil value, as it will automatically delay reading from such processes, to
6978 allowing them to produce more output before Emacs tries to read it.
6979 If the value is t, the delay is reset after each write to the process; any other
6980 non-nil value means that the delay is not reset on write.
6981 The variable takes effect when `start-process' is called. */);
6982 Vprocess_adaptive_read_buffering = Qt;
6983 #endif
6984
6421 defsubr (&Sprocessp); 6985 defsubr (&Sprocessp);
6422 defsubr (&Sget_process); 6986 defsubr (&Sget_process);
6423 defsubr (&Sget_buffer_process); 6987 defsubr (&Sget_buffer_process);
6424 defsubr (&Sdelete_process); 6988 defsubr (&Sdelete_process);
6425 defsubr (&Sprocess_status); 6989 defsubr (&Sprocess_status);
6445 defsubr (&Sset_process_plist); 7009 defsubr (&Sset_process_plist);
6446 defsubr (&Slist_processes); 7010 defsubr (&Slist_processes);
6447 defsubr (&Sprocess_list); 7011 defsubr (&Sprocess_list);
6448 defsubr (&Sstart_process); 7012 defsubr (&Sstart_process);
6449 #ifdef HAVE_SOCKETS 7013 #ifdef HAVE_SOCKETS
6450 defsubr (&Sset_network_process_options); 7014 defsubr (&Sset_network_process_option);
6451 defsubr (&Smake_network_process); 7015 defsubr (&Smake_network_process);
6452 defsubr (&Sformat_network_address); 7016 defsubr (&Sformat_network_address);
6453 #endif /* HAVE_SOCKETS */ 7017 #endif /* HAVE_SOCKETS */
7018 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7019 #ifdef SIOCGIFCONF
7020 defsubr (&Snetwork_interface_list);
7021 #endif
7022 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7023 defsubr (&Snetwork_interface_info);
7024 #endif
7025 #endif /* HAVE_SOCKETS ... */
6454 #ifdef DATAGRAM_SOCKETS 7026 #ifdef DATAGRAM_SOCKETS
6455 defsubr (&Sprocess_datagram_address); 7027 defsubr (&Sprocess_datagram_address);
6456 defsubr (&Sset_process_datagram_address); 7028 defsubr (&Sset_process_datagram_address);
6457 #endif 7029 #endif
6458 defsubr (&Saccept_process_output); 7030 defsubr (&Saccept_process_output);
6506 read_kbd is a Lisp_Object: 7078 read_kbd is a Lisp_Object:
6507 0 to ignore keyboard input, or 7079 0 to ignore keyboard input, or
6508 1 to return when input is available, or 7080 1 to return when input is available, or
6509 -1 means caller will actually read the input, so don't throw to 7081 -1 means caller will actually read the input, so don't throw to
6510 the quit handler. 7082 the quit handler.
6511 a cons cell, meaning wait until its car is non-nil 7083
6512 (and gobble terminal input into the buffer if any arrives), or 7084 see full version for other parameters. We know that wait_proc will
6513 We know that read_kbd will never be a Lisp_Process, since 7085 always be NULL, since `subprocesses' isn't defined.
6514 `subprocesses' isn't defined.
6515 7086
6516 do_display != 0 means redisplay should be done to show subprocess 7087 do_display != 0 means redisplay should be done to show subprocess
6517 output that arrives. 7088 output that arrives.
6518 7089
6519 Return true iff we received input from any process. */ 7090 Return true iff we received input from any process. */
6520 7091
6521 int 7092 int
6522 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display) 7093 wait_reading_process_output (time_limit, microsecs, read_kbd, do_display,
6523 int time_limit, microsecs; 7094 wait_for_cell, wait_proc, just_wait_proc)
6524 Lisp_Object read_kbd; 7095 int time_limit, microsecs, read_kbd, do_display;
6525 int do_display; 7096 Lisp_Object wait_for_cell;
7097 struct Lisp_Process *wait_proc;
7098 int just_wait_proc;
6526 { 7099 {
6527 register int nfds; 7100 register int nfds;
6528 EMACS_TIME end_time, timeout; 7101 EMACS_TIME end_time, timeout;
6529 SELECT_TYPE waitchannels; 7102 SELECT_TYPE waitchannels;
6530 int xerrno; 7103 int xerrno;
6531 /* Either nil or a cons cell, the car of which is of interest and
6532 may be changed outside of this routine. */
6533 Lisp_Object wait_for_cell;
6534
6535 wait_for_cell = Qnil;
6536
6537 /* If waiting for non-nil in a cell, record where. */
6538 if (CONSP (read_kbd))
6539 {
6540 wait_for_cell = read_kbd;
6541 XSETFASTINT (read_kbd, 0);
6542 }
6543 7104
6544 /* What does time_limit really mean? */ 7105 /* What does time_limit really mean? */
6545 if (time_limit || microsecs) 7106 if (time_limit || microsecs)
6546 { 7107 {
6547 EMACS_GET_TIME (end_time); 7108 EMACS_GET_TIME (end_time);
6560 int timeout_reduced_for_timers = 0; 7121 int timeout_reduced_for_timers = 0;
6561 7122
6562 /* If calling from keyboard input, do not quit 7123 /* If calling from keyboard input, do not quit
6563 since we want to return C-g as an input character. 7124 since we want to return C-g as an input character.
6564 Otherwise, do pending quit if requested. */ 7125 Otherwise, do pending quit if requested. */
6565 if (XINT (read_kbd) >= 0) 7126 if (read_kbd >= 0)
6566 QUIT; 7127 QUIT;
6567 7128
6568 /* Exit now if the cell we're waiting for became non-nil. */ 7129 /* Exit now if the cell we're waiting for became non-nil. */
6569 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 7130 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
6570 break; 7131 break;
6611 break; 7172 break;
6612 } 7173 }
6613 while (!detect_input_pending ()); 7174 while (!detect_input_pending ());
6614 7175
6615 /* If there is unread keyboard input, also return. */ 7176 /* If there is unread keyboard input, also return. */
6616 if (XINT (read_kbd) != 0 7177 if (read_kbd != 0
6617 && requeued_events_pending_p ()) 7178 && requeued_events_pending_p ())
6618 break; 7179 break;
6619 7180
6620 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1) 7181 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
6621 { 7182 {
6629 } 7190 }
6630 } 7191 }
6631 7192
6632 /* Cause C-g and alarm signals to take immediate action, 7193 /* Cause C-g and alarm signals to take immediate action,
6633 and cause input available signals to zero out timeout. */ 7194 and cause input available signals to zero out timeout. */
6634 if (XINT (read_kbd) < 0) 7195 if (read_kbd < 0)
6635 set_waiting_for_input (&timeout); 7196 set_waiting_for_input (&timeout);
6636 7197
6637 /* Wait till there is something to do. */ 7198 /* Wait till there is something to do. */
6638 7199
6639 if (! XINT (read_kbd) && NILP (wait_for_cell)) 7200 if (! read_kbd && NILP (wait_for_cell))
6640 FD_ZERO (&waitchannels); 7201 FD_ZERO (&waitchannels);
6641 else 7202 else
6642 FD_SET (0, &waitchannels); 7203 FD_SET (0, &waitchannels);
6643 7204
6644 /* If a frame has been newly mapped and needs updating, 7205 /* If a frame has been newly mapped and needs updating,
6645 reprocess its display stuff. */ 7206 reprocess its display stuff. */
6646 if (frame_garbaged && do_display) 7207 if (frame_garbaged && do_display)
6647 { 7208 {
6648 clear_waiting_for_input (); 7209 clear_waiting_for_input ();
6649 redisplay_preserve_echo_area (15); 7210 redisplay_preserve_echo_area (15);
6650 if (XINT (read_kbd) < 0) 7211 if (read_kbd < 0)
6651 set_waiting_for_input (&timeout); 7212 set_waiting_for_input (&timeout);
6652 } 7213 }
6653 7214
6654 if (XINT (read_kbd) && detect_input_pending ()) 7215 if (read_kbd && detect_input_pending ())
6655 { 7216 {
6656 nfds = 0; 7217 nfds = 0;
6657 FD_ZERO (&waitchannels); 7218 FD_ZERO (&waitchannels);
6658 } 7219 }
6659 else 7220 else
6685 else if (nfds > 0 && (waitchannels & 1) && interrupt_input) 7246 else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
6686 /* System sometimes fails to deliver SIGIO. */ 7247 /* System sometimes fails to deliver SIGIO. */
6687 kill (getpid (), SIGIO); 7248 kill (getpid (), SIGIO);
6688 #endif 7249 #endif
6689 #ifdef SIGIO 7250 #ifdef SIGIO
6690 if (XINT (read_kbd) && interrupt_input && (waitchannels & 1)) 7251 if (read_kbd && interrupt_input && (waitchannels & 1))
6691 kill (getpid (), SIGIO); 7252 kill (getpid (), SIGIO);
6692 #endif 7253 #endif
6693 7254
6694 /* Check for keyboard input */ 7255 /* Check for keyboard input */
6695 7256
6696 if ((XINT (read_kbd) != 0) 7257 if (read_kbd
6697 && detect_input_pending_run_timers (do_display)) 7258 && detect_input_pending_run_timers (do_display))
6698 { 7259 {
6699 swallow_events (do_display); 7260 swallow_events (do_display);
6700 if (detect_input_pending_run_timers (do_display)) 7261 if (detect_input_pending_run_timers (do_display))
6701 break; 7262 break;
6702 } 7263 }
6703 7264
6704 /* If there is unread keyboard input, also return. */ 7265 /* If there is unread keyboard input, also return. */
6705 if (XINT (read_kbd) != 0 7266 if (read_kbd
6706 && requeued_events_pending_p ()) 7267 && requeued_events_pending_p ())
6707 break; 7268 break;
6708 7269
6709 /* If wait_for_cell. check for keyboard input 7270 /* If wait_for_cell. check for keyboard input
6710 but don't run any timers. 7271 but don't run any timers.
6780 defsubr (&Sprocess_inherit_coding_system_flag); 7341 defsubr (&Sprocess_inherit_coding_system_flag);
6781 } 7342 }
6782 7343
6783 7344
6784 #endif /* not subprocesses */ 7345 #endif /* not subprocesses */
7346
7347 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7348 (do not change this comment) */