1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
27 /* This file is split into two parts by the following preprocessor
28 conditional. The 'then' clause contains all of the support for
29 asynchronous subprocesses. The 'else' clause contains stub
30 versions of some of the asynchronous subprocess routines that are
31 often called elsewhere in Emacs, so we don't have to #ifdef the
32 sections that call them. */
40 #include <sys/types.h> /* some typedefs are used in sys/file.h */
47 #if defined(WINDOWSNT) || defined(UNIX98_PTYS)
50 #endif /* not WINDOWSNT */
52 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
53 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #ifdef NEED_NET_ERRNO_H
58 #include <net/errno.h>
59 #endif /* NEED_NET_ERRNO_H */
61 /* Are local (unix) sockets supported? */
62 #if defined (HAVE_SYS_UN_H) && !defined (NO_SOCKETS_IN_FILE_SYSTEM)
63 #if !defined (AF_LOCAL) && defined (AF_UNIX)
64 #define AF_LOCAL AF_UNIX
67 #define HAVE_LOCAL_SOCKETS
71 #endif /* HAVE_SOCKETS */
73 /* TERM is a poor-man's SLIP, used on GNU/Linux. */
78 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
79 #ifdef HAVE_BROKEN_INET_ADDR
80 #define IN_ADDR struct in_addr
81 #define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
83 #define IN_ADDR unsigned long
84 #define NUMERIC_ADDR_ERROR (numeric_addr == -1)
87 #if defined(BSD_SYSTEM) || defined(STRIDE)
88 #include <sys/ioctl.h>
89 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
91 #endif /* HAVE_PTYS and no O_NDELAY */
92 #endif /* BSD_SYSTEM || STRIDE */
94 #ifdef BROKEN_O_NONBLOCK
96 #endif /* BROKEN_O_NONBLOCK */
102 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
104 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
105 /* sys/ioctl.h may have been included already */
107 #include <sys/ioctl.h>
114 #include <sys/sysmacros.h> /* for "minor" */
115 #endif /* not IRIS */
118 #include <sys/wait.h>
127 #include "character.h"
130 #include "termhooks.h"
131 #include "termopts.h"
132 #include "commands.h"
133 #include "keyboard.h"
135 #include "blockinput.h"
136 #include "dispextern.h"
137 #include "composite.h"
140 Lisp_Object Qprocessp
;
141 Lisp_Object Qrun
, Qstop
, Qsignal
;
142 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
143 Lisp_Object Qlocal
, Qdatagram
;
144 Lisp_Object QCname
, QCbuffer
, QChost
, QCservice
, QCtype
;
145 Lisp_Object QClocal
, QCremote
, QCcoding
;
146 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
147 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
148 Lisp_Object QCfilter_multibyte
;
149 Lisp_Object Qlast_nonmenu_event
;
150 /* QCfamily is declared and initialized in xfaces.c,
151 QCfilter in keyboard.c. */
152 extern Lisp_Object QCfamily
, QCfilter
;
154 /* Qexit is declared and initialized in eval.c. */
156 /* QCfamily is defined in xfaces.c. */
157 extern Lisp_Object QCfamily
;
158 /* QCfilter is defined in keyboard.c. */
159 extern Lisp_Object QCfilter
;
161 /* a process object is a network connection when its childp field is neither
162 Qt nor Qnil but is instead a property list (KEY VAL ...). */
165 #define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
166 #define NETCONN1_P(p) (GC_CONSP ((p)->childp))
168 #define NETCONN_P(p) 0
169 #define NETCONN1_P(p) 0
170 #endif /* HAVE_SOCKETS */
172 /* Define first descriptor number available for subprocesses. */
174 #define FIRST_PROC_DESC 1
176 #define FIRST_PROC_DESC 3
179 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
182 #if !defined (SIGCHLD) && defined (SIGCLD)
183 #define SIGCHLD SIGCLD
186 #include "syssignal.h"
190 extern char *get_operating_system_release ();
196 extern char *sys_errlist
[];
203 /* t means use pty, nil means use a pipe,
204 maybe other values to come. */
205 static Lisp_Object Vprocess_connection_type
;
209 #include <sys/socket.h>
213 /* These next two vars are non-static since sysdep.c uses them in the
214 emulation of `select'. */
215 /* Number of events of change of status of a process. */
217 /* Number of events for which the user or sentinel has been notified. */
220 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
222 #ifdef BROKEN_NON_BLOCKING_CONNECT
223 #undef NON_BLOCKING_CONNECT
225 #ifndef NON_BLOCKING_CONNECT
228 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
229 #if defined (O_NONBLOCK) || defined (O_NDELAY)
230 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
231 #define NON_BLOCKING_CONNECT
232 #endif /* EWOULDBLOCK || EINPROGRESS */
233 #endif /* O_NONBLOCK || O_NDELAY */
234 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
235 #endif /* HAVE_SELECT */
236 #endif /* HAVE_SOCKETS */
237 #endif /* NON_BLOCKING_CONNECT */
238 #endif /* BROKEN_NON_BLOCKING_CONNECT */
240 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
241 this system. We need to read full packets, so we need a
242 "non-destructive" select. So we require either native select,
243 or emulation of select using FIONREAD. */
245 #ifdef BROKEN_DATAGRAM_SOCKETS
246 #undef DATAGRAM_SOCKETS
248 #ifndef DATAGRAM_SOCKETS
250 #if defined (HAVE_SELECT) || defined (FIONREAD)
251 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
252 #define DATAGRAM_SOCKETS
253 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
254 #endif /* HAVE_SELECT || FIONREAD */
255 #endif /* HAVE_SOCKETS */
256 #endif /* DATAGRAM_SOCKETS */
257 #endif /* BROKEN_DATAGRAM_SOCKETS */
260 #undef NON_BLOCKING_CONNECT
261 #undef DATAGRAM_SOCKETS
264 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
265 #ifdef EMACS_HAS_USECS
266 #define ADAPTIVE_READ_BUFFERING
270 #ifdef ADAPTIVE_READ_BUFFERING
271 #define READ_OUTPUT_DELAY_INCREMENT 10000
272 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
273 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
275 /* Number of processes which have a non-zero read_output_delay,
276 and therefore might be delayed for adaptive read buffering. */
278 static int process_output_delay_count
;
280 /* Non-zero if any process has non-nil read_output_skip. */
282 static int process_output_skip
;
284 /* Non-nil means to delay reading process output to improve buffering.
285 A value of t means that delay is reset after each send, any other
286 non-nil value does not reset the delay. A value of nil disables
287 adaptive read buffering completely. */
288 static Lisp_Object Vprocess_adaptive_read_buffering
;
290 #define process_output_delay_count 0
294 #include "sysselect.h"
296 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
297 static void deactivate_process
P_ ((Lisp_Object
));
298 static void status_notify
P_ ((struct Lisp_Process
*));
299 static int read_process_output
P_ ((Lisp_Object
, int));
301 /* If we support a window system, turn on the code to poll periodically
302 to detect C-g. It isn't actually used when doing interrupt input. */
303 #ifdef HAVE_WINDOW_SYSTEM
304 #define POLL_FOR_INPUT
307 /* Mask of bits indicating the descriptors that we wait for input on. */
309 static SELECT_TYPE input_wait_mask
;
311 /* Mask that excludes keyboard input descriptor (s). */
313 static SELECT_TYPE non_keyboard_wait_mask
;
315 /* Mask that excludes process input descriptor (s). */
317 static SELECT_TYPE non_process_wait_mask
;
319 #ifdef NON_BLOCKING_CONNECT
320 /* Mask of bits indicating the descriptors that we wait for connect to
321 complete on. Once they complete, they are removed from this mask
322 and added to the input_wait_mask and non_keyboard_wait_mask. */
324 static SELECT_TYPE connect_wait_mask
;
326 /* Number of bits set in connect_wait_mask. */
327 static int num_pending_connects
;
329 #define IF_NON_BLOCKING_CONNECT(s) s
331 #define IF_NON_BLOCKING_CONNECT(s)
334 /* The largest descriptor currently in use for a process object. */
335 static int max_process_desc
;
337 /* The largest descriptor currently in use for keyboard input. */
338 static int max_keyboard_desc
;
340 /* Nonzero means delete a process right away if it exits. */
341 static int delete_exited_processes
;
343 /* Indexed by descriptor, gives the process (if any) for that descriptor */
344 Lisp_Object chan_process
[MAXDESC
];
346 /* Alist of elements (NAME . PROCESS) */
347 Lisp_Object Vprocess_alist
;
349 /* Buffered-ahead input char from process, indexed by channel.
350 -1 means empty (no char is buffered).
351 Used on sys V where the only way to tell if there is any
352 output from the process is to read at least one char.
353 Always -1 on systems that support FIONREAD. */
355 /* Don't make static; need to access externally. */
356 int proc_buffered_char
[MAXDESC
];
358 /* Table of `struct coding-system' for each process. */
359 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
360 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
362 #ifdef DATAGRAM_SOCKETS
363 /* Table of `partner address' for datagram sockets. */
364 struct sockaddr_and_len
{
367 } datagram_address
[MAXDESC
];
368 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
369 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XINT (XPROCESS (proc)->infd)].sa != 0)
371 #define DATAGRAM_CHAN_P(chan) (0)
372 #define DATAGRAM_CONN_P(proc) (0)
375 static Lisp_Object
get_process ();
376 static void exec_sentinel ();
378 extern EMACS_TIME
timer_check ();
379 extern int timers_run
;
381 /* Maximum number of bytes to send to a pty without an eof. */
382 static int pty_max_bytes
;
388 /* The file name of the pty opened by allocate_pty. */
390 static char pty_name
[24];
393 /* Compute the Lisp form of the process status, p->status, from
394 the numeric status that was returned by `wait'. */
396 static Lisp_Object
status_convert ();
400 struct Lisp_Process
*p
;
402 union { int i
; WAITTYPE wt
; } u
;
403 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
404 p
->status
= status_convert (u
.wt
);
405 p
->raw_status_low
= Qnil
;
406 p
->raw_status_high
= Qnil
;
409 /* Convert a process status word in Unix format to
410 the list that we use internally. */
417 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
418 else if (WIFEXITED (w
))
419 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
420 WCOREDUMP (w
) ? Qt
: Qnil
));
421 else if (WIFSIGNALED (w
))
422 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
423 WCOREDUMP (w
) ? Qt
: Qnil
));
428 /* Given a status-list, extract the three pieces of information
429 and store them individually through the three pointers. */
432 decode_status (l
, symbol
, code
, coredump
)
450 *code
= XFASTINT (XCAR (tem
));
452 *coredump
= !NILP (tem
);
456 /* Return a string describing a process status list. */
460 struct Lisp_Process
*p
;
462 Lisp_Object status
= p
->status
;
465 Lisp_Object string
, string2
;
467 decode_status (status
, &symbol
, &code
, &coredump
);
469 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
472 synchronize_system_messages_locale ();
473 signame
= strsignal (code
);
476 string
= build_string (signame
);
477 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
478 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
479 return concat2 (string
, string2
);
481 else if (EQ (symbol
, Qexit
))
484 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
486 return build_string ("finished\n");
487 string
= Fnumber_to_string (make_number (code
));
488 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
489 return concat3 (build_string ("exited abnormally with code "),
492 else if (EQ (symbol
, Qfailed
))
494 string
= Fnumber_to_string (make_number (code
));
495 string2
= build_string ("\n");
496 return concat3 (build_string ("failed with code "),
500 return Fcopy_sequence (Fsymbol_name (symbol
));
505 /* Open an available pty, returning a file descriptor.
506 Return -1 on failure.
507 The file name of the terminal corresponding to the pty
508 is left in the variable pty_name. */
519 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
520 for (i
= 0; i
< 16; i
++)
523 struct stat stb
; /* Used in some PTY_OPEN. */
524 #ifdef PTY_NAME_SPRINTF
527 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
528 #endif /* no PTY_NAME_SPRINTF */
532 #else /* no PTY_OPEN */
535 /* Unusual IRIS code */
536 *ptyv
= emacs_open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
539 if (fstat (fd
, &stb
) < 0)
541 # else /* not IRIS */
542 { /* Some systems name their pseudoterminals so that there are gaps in
543 the usual sequence - for example, on HP9000/S700 systems, there
544 are no pseudoterminals with names ending in 'f'. So we wait for
545 three failures in a row before deciding that we've reached the
547 int failed_count
= 0;
549 if (stat (pty_name
, &stb
) < 0)
552 if (failed_count
>= 3)
559 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
561 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
563 # endif /* not IRIS */
565 #endif /* no PTY_OPEN */
569 /* check to make certain that both sides are available
570 this avoids a nasty yet stupid bug in rlogins */
571 #ifdef PTY_TTY_NAME_SPRINTF
574 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
575 #endif /* no PTY_TTY_NAME_SPRINTF */
577 if (access (pty_name
, 6) != 0)
580 # if !defined(IRIS) && !defined(__sgi)
586 #endif /* not UNIPLUS */
593 #endif /* HAVE_PTYS */
599 register Lisp_Object val
, tem
, name1
;
600 register struct Lisp_Process
*p
;
604 p
= allocate_process ();
606 XSETINT (p
->infd
, -1);
607 XSETINT (p
->outfd
, -1);
608 XSETFASTINT (p
->pid
, 0);
609 XSETFASTINT (p
->tick
, 0);
610 XSETFASTINT (p
->update_tick
, 0);
611 p
->raw_status_low
= Qnil
;
612 p
->raw_status_high
= Qnil
;
614 p
->mark
= Fmake_marker ();
616 #ifdef ADAPTIVE_READ_BUFFERING
617 p
->adaptive_read_buffering
= Qnil
;
618 XSETFASTINT (p
->read_output_delay
, 0);
619 p
->read_output_skip
= Qnil
;
622 /* If name is already in use, modify it until it is unused. */
627 tem
= Fget_process (name1
);
628 if (NILP (tem
)) break;
629 sprintf (suffix
, "<%d>", i
);
630 name1
= concat2 (name
, build_string (suffix
));
634 XSETPROCESS (val
, p
);
635 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
640 remove_process (proc
)
641 register Lisp_Object proc
;
643 register Lisp_Object pair
;
645 pair
= Frassq (proc
, Vprocess_alist
);
646 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
648 deactivate_process (proc
);
651 /* Setup coding systems of PROCESS. */
654 setup_process_coding_systems (process
)
657 struct Lisp_Process
*p
= XPROCESS (process
);
658 int inch
= XINT (p
->infd
);
659 int outch
= XINT (p
->outfd
);
660 Lisp_Object coding_system
;
662 if (inch
< 0 || outch
< 0)
665 if (!proc_decode_coding_system
[inch
])
666 proc_decode_coding_system
[inch
]
667 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
668 coding_system
= p
->decode_coding_system
;
669 if (! NILP (p
->filter
))
671 if (NILP (p
->filter_multibyte
))
672 coding_system
= raw_text_coding_system (coding_system
);
674 else if (BUFFERP (p
->buffer
))
676 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
677 coding_system
= raw_text_coding_system (coding_system
);
679 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
681 if (!proc_encode_coding_system
[outch
])
682 proc_encode_coding_system
[outch
]
683 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
684 setup_coding_system (p
->encode_coding_system
,
685 proc_encode_coding_system
[outch
]);
688 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
689 doc
: /* Return t if OBJECT is a process. */)
693 return PROCESSP (object
) ? Qt
: Qnil
;
696 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
697 doc
: /* Return the process named NAME, or nil if there is none. */)
699 register Lisp_Object name
;
704 return Fcdr (Fassoc (name
, Vprocess_alist
));
707 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
708 doc
: /* Return the (or a) process associated with BUFFER.
709 BUFFER may be a buffer or the name of one. */)
711 register Lisp_Object buffer
;
713 register Lisp_Object buf
, tail
, proc
;
715 if (NILP (buffer
)) return Qnil
;
716 buf
= Fget_buffer (buffer
);
717 if (NILP (buf
)) return Qnil
;
719 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
721 proc
= Fcdr (Fcar (tail
));
722 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
728 /* This is how commands for the user decode process arguments. It
729 accepts a process, a process name, a buffer, a buffer name, or nil.
730 Buffers denote the first process in the buffer, and nil denotes the
735 register Lisp_Object name
;
737 register Lisp_Object proc
, obj
;
740 obj
= Fget_process (name
);
742 obj
= Fget_buffer (name
);
744 error ("Process %s does not exist", SDATA (name
));
746 else if (NILP (name
))
747 obj
= Fcurrent_buffer ();
751 /* Now obj should be either a buffer object or a process object.
755 proc
= Fget_buffer_process (obj
);
757 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
767 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
768 doc
: /* Delete PROCESS: kill it and forget about it immediately.
769 PROCESS may be a process, a buffer, the name of a process or buffer, or
770 nil, indicating the current buffer's process. */)
772 register Lisp_Object process
;
774 register struct Lisp_Process
*p
;
776 process
= get_process (process
);
777 p
= XPROCESS (process
);
779 p
->raw_status_low
= Qnil
;
780 p
->raw_status_high
= Qnil
;
783 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
784 XSETINT (p
->tick
, ++process_tick
);
787 else if (XINT (p
->infd
) >= 0)
789 Fkill_process (process
, Qnil
);
790 /* Do this now, since remove_process will make sigchld_handler do nothing. */
792 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
793 XSETINT (p
->tick
, ++process_tick
);
796 remove_process (process
);
800 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
801 doc
: /* Return the status of PROCESS.
802 The returned value is one of the following symbols:
803 run -- for a process that is running.
804 stop -- for a process stopped but continuable.
805 exit -- for a process that has exited.
806 signal -- for a process that has got a fatal signal.
807 open -- for a network stream connection that is open.
808 listen -- for a network stream server that is listening.
809 closed -- for a network stream connection that is closed.
810 connect -- when waiting for a non-blocking connection to complete.
811 failed -- when a non-blocking connection has failed.
812 nil -- if arg is a process name and no such process exists.
813 PROCESS may be a process, a buffer, the name of a process, or
814 nil, indicating the current buffer's process. */)
816 register Lisp_Object process
;
818 register struct Lisp_Process
*p
;
819 register Lisp_Object status
;
821 if (STRINGP (process
))
822 process
= Fget_process (process
);
824 process
= get_process (process
);
829 p
= XPROCESS (process
);
830 if (!NILP (p
->raw_status_low
))
834 status
= XCAR (status
);
837 if (EQ (status
, Qexit
))
839 else if (EQ (p
->command
, Qt
))
841 else if (EQ (status
, Qrun
))
847 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
849 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
850 If PROCESS has not yet exited or died, return 0. */)
852 register Lisp_Object process
;
854 CHECK_PROCESS (process
);
855 if (!NILP (XPROCESS (process
)->raw_status_low
))
856 update_status (XPROCESS (process
));
857 if (CONSP (XPROCESS (process
)->status
))
858 return XCAR (XCDR (XPROCESS (process
)->status
));
859 return make_number (0);
862 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
863 doc
: /* Return the process id of PROCESS.
864 This is the pid of the external process which PROCESS uses or talks to.
865 For a network connection, this value is nil. */)
867 register Lisp_Object process
;
869 CHECK_PROCESS (process
);
870 return XPROCESS (process
)->pid
;
873 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
874 doc
: /* Return the name of PROCESS, as a string.
875 This is the name of the program invoked in PROCESS,
876 possibly modified to make it unique among process names. */)
878 register Lisp_Object process
;
880 CHECK_PROCESS (process
);
881 return XPROCESS (process
)->name
;
884 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
885 doc
: /* Return the command that was executed to start PROCESS.
886 This is a list of strings, the first string being the program executed
887 and the rest of the strings being the arguments given to it.
888 For a non-child channel, this is nil. */)
890 register Lisp_Object process
;
892 CHECK_PROCESS (process
);
893 return XPROCESS (process
)->command
;
896 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
897 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
898 This is the terminal that the process itself reads and writes on,
899 not the name of the pty that Emacs uses to talk with that terminal. */)
901 register Lisp_Object process
;
903 CHECK_PROCESS (process
);
904 return XPROCESS (process
)->tty_name
;
907 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
909 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
911 register Lisp_Object process
, buffer
;
913 struct Lisp_Process
*p
;
915 CHECK_PROCESS (process
);
917 CHECK_BUFFER (buffer
);
918 p
= XPROCESS (process
);
921 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
922 setup_process_coding_systems (process
);
926 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
928 doc
: /* Return the buffer PROCESS is associated with.
929 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
931 register Lisp_Object process
;
933 CHECK_PROCESS (process
);
934 return XPROCESS (process
)->buffer
;
937 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
939 doc
: /* Return the marker for the end of the last output from PROCESS. */)
941 register Lisp_Object process
;
943 CHECK_PROCESS (process
);
944 return XPROCESS (process
)->mark
;
947 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
949 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
950 t means stop accepting output from the process.
952 When a process has a filter, its buffer is not used for output.
953 Instead, each time it does output, the entire string of output is
954 passed to the filter.
956 The filter gets two arguments: the process and the string of output.
957 The string argument is normally a multibyte string, except:
958 - if the process' input coding system is no-conversion or raw-text,
959 it is a unibyte string (the non-converted input), or else
960 - if `default-enable-multibyte-characters' is nil, it is a unibyte
961 string (the result of converting the decoded input multibyte
962 string to unibyte with `string-make-unibyte'). */)
964 register Lisp_Object process
, filter
;
966 struct Lisp_Process
*p
;
968 CHECK_PROCESS (process
);
969 p
= XPROCESS (process
);
971 /* Don't signal an error if the process' input file descriptor
972 is closed. This could make debugging Lisp more difficult,
973 for example when doing something like
975 (setq process (start-process ...))
977 (set-process-filter process ...) */
979 if (XINT (p
->infd
) >= 0)
981 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
983 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
984 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
986 else if (EQ (p
->filter
, Qt
)
987 && !EQ (p
->command
, Qt
)) /* Network process not stopped. */
989 FD_SET (XINT (p
->infd
), &input_wait_mask
);
990 FD_SET (XINT (p
->infd
), &non_keyboard_wait_mask
);
996 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
997 setup_process_coding_systems (process
);
1001 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1003 doc
: /* Returns the filter function of PROCESS; nil if none.
1004 See `set-process-filter' for more info on filter functions. */)
1006 register Lisp_Object process
;
1008 CHECK_PROCESS (process
);
1009 return XPROCESS (process
)->filter
;
1012 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1014 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1015 The sentinel is called as a function when the process changes state.
1016 It gets two arguments: the process, and a string describing the change. */)
1018 register Lisp_Object process
, sentinel
;
1020 struct Lisp_Process
*p
;
1022 CHECK_PROCESS (process
);
1023 p
= XPROCESS (process
);
1025 p
->sentinel
= sentinel
;
1027 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1031 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1033 doc
: /* Return the sentinel of PROCESS; nil if none.
1034 See `set-process-sentinel' for more info on sentinels. */)
1036 register Lisp_Object process
;
1038 CHECK_PROCESS (process
);
1039 return XPROCESS (process
)->sentinel
;
1042 DEFUN ("set-process-window-size", Fset_process_window_size
,
1043 Sset_process_window_size
, 3, 3, 0,
1044 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1045 (process
, height
, width
)
1046 register Lisp_Object process
, height
, width
;
1048 CHECK_PROCESS (process
);
1049 CHECK_NATNUM (height
);
1050 CHECK_NATNUM (width
);
1052 if (XINT (XPROCESS (process
)->infd
) < 0
1053 || set_window_size (XINT (XPROCESS (process
)->infd
),
1054 XINT (height
), XINT (width
)) <= 0)
1060 DEFUN ("set-process-inherit-coding-system-flag",
1061 Fset_process_inherit_coding_system_flag
,
1062 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1063 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1064 If the second argument FLAG is non-nil, then the variable
1065 `buffer-file-coding-system' of the buffer associated with PROCESS
1066 will be bound to the value of the coding system used to decode
1069 This is useful when the coding system specified for the process buffer
1070 leaves either the character code conversion or the end-of-line conversion
1071 unspecified, or if the coding system used to decode the process output
1072 is more appropriate for saving the process buffer.
1074 Binding the variable `inherit-process-coding-system' to non-nil before
1075 starting the process is an alternative way of setting the inherit flag
1076 for the process which will run. */)
1078 register Lisp_Object process
, flag
;
1080 CHECK_PROCESS (process
);
1081 XPROCESS (process
)->inherit_coding_system_flag
= flag
;
1085 DEFUN ("process-inherit-coding-system-flag",
1086 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1088 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1089 If this flag is t, `buffer-file-coding-system' of the buffer
1090 associated with PROCESS will inherit the coding system used to decode
1091 the process output. */)
1093 register Lisp_Object process
;
1095 CHECK_PROCESS (process
);
1096 return XPROCESS (process
)->inherit_coding_system_flag
;
1099 DEFUN ("set-process-query-on-exit-flag",
1100 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1102 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1103 If the second argument FLAG is non-nil, Emacs will query the user before
1104 exiting if PROCESS is running. */)
1106 register Lisp_Object process
, flag
;
1108 CHECK_PROCESS (process
);
1109 XPROCESS (process
)->kill_without_query
= Fnull (flag
);
1113 DEFUN ("process-query-on-exit-flag",
1114 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1116 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1118 register Lisp_Object process
;
1120 CHECK_PROCESS (process
);
1121 return Fnull (XPROCESS (process
)->kill_without_query
);
1124 #ifdef DATAGRAM_SOCKETS
1125 Lisp_Object
Fprocess_datagram_address ();
1128 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1130 doc
: /* Return the contact info of PROCESS; t for a real child.
1131 For a net connection, the value depends on the optional KEY arg.
1132 If KEY is nil, value is a cons cell of the form (HOST SERVICE),
1133 if KEY is t, the complete contact information for the connection is
1134 returned, else the specific value for the keyword KEY is returned.
1135 See `make-network-process' for a list of keywords. */)
1137 register Lisp_Object process
, key
;
1139 Lisp_Object contact
;
1141 CHECK_PROCESS (process
);
1142 contact
= XPROCESS (process
)->childp
;
1144 #ifdef DATAGRAM_SOCKETS
1145 if (DATAGRAM_CONN_P (process
)
1146 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1147 contact
= Fplist_put (contact
, QCremote
,
1148 Fprocess_datagram_address (process
));
1151 if (!NETCONN_P (process
) || EQ (key
, Qt
))
1154 return Fcons (Fplist_get (contact
, QChost
),
1155 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1156 return Fplist_get (contact
, key
);
1159 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1161 doc
: /* Return the plist of PROCESS. */)
1163 register Lisp_Object process
;
1165 CHECK_PROCESS (process
);
1166 return XPROCESS (process
)->plist
;
1169 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1171 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1173 register Lisp_Object process
, plist
;
1175 CHECK_PROCESS (process
);
1178 XPROCESS (process
)->plist
= plist
;
1182 #if 0 /* Turned off because we don't currently record this info
1183 in the process. Perhaps add it. */
1184 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1185 doc
: /* Return the connection type of PROCESS.
1186 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1187 a socket connection. */)
1189 Lisp_Object process
;
1191 return XPROCESS (process
)->type
;
1196 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1198 doc
: /* Convert network ADDRESS from internal format to a string.
1199 If optional second argument OMIT-PORT is non-nil, don't include a port
1200 number in the string; in this case, interpret a 4 element vector as an
1201 IP address. Returns nil if format of ADDRESS is invalid. */)
1202 (address
, omit_port
)
1203 Lisp_Object address
, omit_port
;
1208 if (STRINGP (address
)) /* AF_LOCAL */
1211 if (VECTORP (address
)) /* AF_INET */
1213 register struct Lisp_Vector
*p
= XVECTOR (address
);
1214 Lisp_Object args
[6];
1217 if (!NILP (omit_port
) && (p
->size
== 4 || p
->size
== 5))
1219 args
[0] = build_string ("%d.%d.%d.%d");
1222 else if (p
->size
== 5)
1224 args
[0] = build_string ("%d.%d.%d.%d:%d");
1230 for (i
= 0; i
< nargs
; i
++)
1231 args
[i
+1] = p
->contents
[i
];
1232 return Fformat (nargs
+1, args
);
1235 if (CONSP (address
))
1237 Lisp_Object args
[2];
1238 args
[0] = build_string ("<Family %d>");
1239 args
[1] = Fcar (address
);
1240 return Fformat (2, args
);
1249 list_processes_1 (query_only
)
1250 Lisp_Object query_only
;
1252 register Lisp_Object tail
, tem
;
1253 Lisp_Object proc
, minspace
, tem1
;
1254 register struct Lisp_Process
*p
;
1256 int w_proc
, w_buffer
, w_tty
;
1257 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1259 w_proc
= 4; /* Proc */
1260 w_buffer
= 6; /* Buffer */
1261 w_tty
= 0; /* Omit if no ttys */
1263 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
1267 proc
= Fcdr (Fcar (tail
));
1268 p
= XPROCESS (proc
);
1269 if (NILP (p
->childp
))
1271 if (!NILP (query_only
) && !NILP (p
->kill_without_query
))
1273 if (STRINGP (p
->name
)
1274 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1276 if (!NILP (p
->buffer
))
1278 if (NILP (XBUFFER (p
->buffer
)->name
) && w_buffer
< 8)
1279 w_buffer
= 8; /* (Killed) */
1280 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1283 if (STRINGP (p
->tty_name
)
1284 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1288 XSETFASTINT (i_status
, w_proc
+ 1);
1289 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1292 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1293 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_tty
+ 1);
1296 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1299 XSETFASTINT (minspace
, 1);
1301 set_buffer_internal (XBUFFER (Vstandard_output
));
1302 current_buffer
->undo_list
= Qt
;
1304 current_buffer
->truncate_lines
= Qt
;
1306 write_string ("Proc", -1);
1307 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1308 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1311 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1313 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1314 write_string ("\n", -1);
1316 write_string ("----", -1);
1317 Findent_to (i_status
, minspace
); write_string ("------", -1);
1318 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1321 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1323 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1324 write_string ("\n", -1);
1326 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
1330 proc
= Fcdr (Fcar (tail
));
1331 p
= XPROCESS (proc
);
1332 if (NILP (p
->childp
))
1334 if (!NILP (query_only
) && !NILP (p
->kill_without_query
))
1337 Finsert (1, &p
->name
);
1338 Findent_to (i_status
, minspace
);
1340 if (!NILP (p
->raw_status_low
))
1343 if (CONSP (p
->status
))
1344 symbol
= XCAR (p
->status
);
1347 if (EQ (symbol
, Qsignal
))
1350 tem
= Fcar (Fcdr (p
->status
));
1352 if (XINT (tem
) < NSIG
)
1353 write_string (sys_errlist
[XINT (tem
)], -1);
1356 Fprinc (symbol
, Qnil
);
1358 else if (NETCONN1_P (p
))
1360 if (EQ (symbol
, Qexit
))
1361 write_string ("closed", -1);
1362 else if (EQ (p
->command
, Qt
))
1363 write_string ("stopped", -1);
1364 else if (EQ (symbol
, Qrun
))
1365 write_string ("open", -1);
1367 Fprinc (symbol
, Qnil
);
1370 Fprinc (symbol
, Qnil
);
1372 if (EQ (symbol
, Qexit
))
1375 tem
= Fcar (Fcdr (p
->status
));
1378 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1379 write_string (tembuf
, -1);
1383 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
1384 remove_process (proc
);
1386 Findent_to (i_buffer
, minspace
);
1387 if (NILP (p
->buffer
))
1388 insert_string ("(none)");
1389 else if (NILP (XBUFFER (p
->buffer
)->name
))
1390 insert_string ("(Killed)");
1392 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1396 Findent_to (i_tty
, minspace
);
1397 if (STRINGP (p
->tty_name
))
1398 Finsert (1, &p
->tty_name
);
1401 Findent_to (i_command
, minspace
);
1403 if (EQ (p
->status
, Qlisten
))
1405 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1406 if (INTEGERP (port
))
1407 port
= Fnumber_to_string (port
);
1409 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1410 sprintf (tembuf
, "(network %s server on %s)\n",
1411 (DATAGRAM_CHAN_P (XINT (p
->infd
)) ? "datagram" : "stream"),
1412 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1413 insert_string (tembuf
);
1415 else if (NETCONN1_P (p
))
1417 /* For a local socket, there is no host name,
1418 so display service instead. */
1419 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1420 if (!STRINGP (host
))
1422 host
= Fplist_get (p
->childp
, QCservice
);
1423 if (INTEGERP (host
))
1424 host
= Fnumber_to_string (host
);
1427 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1428 sprintf (tembuf
, "(network %s connection to %s)\n",
1429 (DATAGRAM_CHAN_P (XINT (p
->infd
)) ? "datagram" : "stream"),
1430 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1431 insert_string (tembuf
);
1443 insert_string (" ");
1445 insert_string ("\n");
1451 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1452 doc
: /* Display a list of all processes.
1453 If optional argument QUERY-ONLY is non-nil, only processes with
1454 the query-on-exit flag set will be listed.
1455 Any process listed as exited or signaled is actually eliminated
1456 after the listing is made. */)
1458 Lisp_Object query_only
;
1460 internal_with_output_to_temp_buffer ("*Process List*",
1461 list_processes_1
, query_only
);
1465 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1466 doc
: /* Return a list of all processes. */)
1469 return Fmapcar (Qcdr
, Vprocess_alist
);
1472 /* Starting asynchronous inferior processes. */
1474 static Lisp_Object
start_process_unwind ();
1476 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1477 doc
: /* Start a program in a subprocess. Return the process object for it.
1478 NAME is name for process. It is modified if necessary to make it unique.
1479 BUFFER is the buffer (or buffer name) to associate with the process.
1480 Process output goes at end of that buffer, unless you specify
1481 an output stream or filter function to handle the output.
1482 BUFFER may be also nil, meaning that this process is not associated
1484 PROGRAM is the program file name. It is searched for in PATH.
1485 Remaining arguments are strings to give program as arguments.
1487 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1490 register Lisp_Object
*args
;
1492 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1494 register unsigned char *new_argv
;
1497 register unsigned char **new_argv
;
1500 int count
= SPECPDL_INDEX ();
1504 buffer
= Fget_buffer_create (buffer
);
1506 /* Make sure that the child will be able to chdir to the current
1507 buffer's current directory, or its unhandled equivalent. We
1508 can't just have the child check for an error when it does the
1509 chdir, since it's in a vfork.
1511 We have to GCPRO around this because Fexpand_file_name and
1512 Funhandled_file_name_directory might call a file name handling
1513 function. The argument list is protected by the caller, so all
1514 we really have to worry about is buffer. */
1516 struct gcpro gcpro1
, gcpro2
;
1518 current_dir
= current_buffer
->directory
;
1520 GCPRO2 (buffer
, current_dir
);
1523 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
1525 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1526 report_file_error ("Setting current directory",
1527 Fcons (current_buffer
->directory
, Qnil
));
1533 CHECK_STRING (name
);
1537 CHECK_STRING (program
);
1539 proc
= make_process (name
);
1540 /* If an error occurs and we can't start the process, we want to
1541 remove it from the process list. This means that each error
1542 check in create_process doesn't need to call remove_process
1543 itself; it's all taken care of here. */
1544 record_unwind_protect (start_process_unwind
, proc
);
1546 XPROCESS (proc
)->childp
= Qt
;
1547 XPROCESS (proc
)->plist
= Qnil
;
1548 XPROCESS (proc
)->buffer
= buffer
;
1549 XPROCESS (proc
)->sentinel
= Qnil
;
1550 XPROCESS (proc
)->filter
= Qnil
;
1551 XPROCESS (proc
)->filter_multibyte
1552 = buffer_defaults
.enable_multibyte_characters
;
1553 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1555 #ifdef ADAPTIVE_READ_BUFFERING
1556 XPROCESS (proc
)->adaptive_read_buffering
= Vprocess_adaptive_read_buffering
;
1559 /* Make the process marker point into the process buffer (if any). */
1561 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1562 BUF_ZV (XBUFFER (buffer
)),
1563 BUF_ZV_BYTE (XBUFFER (buffer
)));
1566 /* Decide coding systems for communicating with the process. Here
1567 we don't setup the structure coding_system nor pay attention to
1568 unibyte mode. They are done in create_process. */
1570 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1571 Lisp_Object coding_systems
= Qt
;
1572 Lisp_Object val
, *args2
;
1573 struct gcpro gcpro1
, gcpro2
;
1575 val
= Vcoding_system_for_read
;
1578 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1579 args2
[0] = Qstart_process
;
1580 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1581 GCPRO2 (proc
, current_dir
);
1582 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1584 if (CONSP (coding_systems
))
1585 val
= XCAR (coding_systems
);
1586 else if (CONSP (Vdefault_process_coding_system
))
1587 val
= XCAR (Vdefault_process_coding_system
);
1589 XPROCESS (proc
)->decode_coding_system
= val
;
1591 val
= Vcoding_system_for_write
;
1594 if (EQ (coding_systems
, Qt
))
1596 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1597 args2
[0] = Qstart_process
;
1598 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1599 GCPRO2 (proc
, current_dir
);
1600 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1603 if (CONSP (coding_systems
))
1604 val
= XCDR (coding_systems
);
1605 else if (CONSP (Vdefault_process_coding_system
))
1606 val
= XCDR (Vdefault_process_coding_system
);
1608 XPROCESS (proc
)->encode_coding_system
= val
;
1612 /* Make a one member argv with all args concatenated
1613 together separated by a blank. */
1614 len
= SBYTES (program
) + 2;
1615 for (i
= 3; i
< nargs
; i
++)
1619 len
+= SBYTES (tem
) + 1; /* count the blank */
1621 new_argv
= (unsigned char *) alloca (len
);
1622 strcpy (new_argv
, SDATA (program
));
1623 for (i
= 3; i
< nargs
; i
++)
1627 strcat (new_argv
, " ");
1628 strcat (new_argv
, SDATA (tem
));
1630 /* Need to add code here to check for program existence on VMS */
1633 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1635 /* If program file name is not absolute, search our path for it.
1636 Put the name we will really use in TEM. */
1637 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1638 && !(SCHARS (program
) > 1
1639 && IS_DEVICE_SEP (SREF (program
, 1))))
1641 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1644 GCPRO4 (name
, program
, buffer
, current_dir
);
1645 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1648 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1649 tem
= Fexpand_file_name (tem
, Qnil
);
1653 if (!NILP (Ffile_directory_p (program
)))
1654 error ("Specified program for new process is a directory");
1658 /* If program file name starts with /: for quoting a magic name,
1660 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1661 && SREF (tem
, 1) == ':')
1662 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1664 /* Encode the file name and put it in NEW_ARGV.
1665 That's where the child will use it to execute the program. */
1666 tem
= ENCODE_FILE (tem
);
1667 new_argv
[0] = SDATA (tem
);
1669 /* Here we encode arguments by the coding system used for sending
1670 data to the process. We don't support using different coding
1671 systems for encoding arguments and for encoding data sent to the
1674 for (i
= 3; i
< nargs
; i
++)
1678 if (STRING_MULTIBYTE (tem
))
1679 tem
= (code_convert_string_norecord
1680 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1681 new_argv
[i
- 2] = SDATA (tem
);
1683 new_argv
[i
- 2] = 0;
1684 #endif /* not VMS */
1686 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1687 XPROCESS (proc
)->decoding_carryover
= make_number (0);
1688 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1689 XPROCESS (proc
)->encoding_carryover
= make_number (0);
1691 XPROCESS (proc
)->inherit_coding_system_flag
1692 = (NILP (buffer
) || !inherit_process_coding_system
1695 create_process (proc
, (char **) new_argv
, current_dir
);
1697 return unbind_to (count
, proc
);
1700 /* This function is the unwind_protect form for Fstart_process. If
1701 PROC doesn't have its pid set, then we know someone has signaled
1702 an error and the process wasn't started successfully, so we should
1703 remove it from the process list. */
1705 start_process_unwind (proc
)
1708 if (!PROCESSP (proc
))
1711 /* Was PROC started successfully? */
1712 if (XINT (XPROCESS (proc
)->pid
) <= 0)
1713 remove_process (proc
);
1719 create_process_1 (timer
)
1720 struct atimer
*timer
;
1722 /* Nothing to do. */
1726 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1729 /* Mimic blocking of signals on system V, which doesn't really have it. */
1731 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1732 int sigchld_deferred
;
1735 create_process_sigchld ()
1737 signal (SIGCHLD
, create_process_sigchld
);
1739 sigchld_deferred
= 1;
1745 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1747 create_process (process
, new_argv
, current_dir
)
1748 Lisp_Object process
;
1750 Lisp_Object current_dir
;
1752 int pid
, inchannel
, outchannel
;
1754 #ifdef POSIX_SIGNALS
1757 struct sigaction sigint_action
;
1758 struct sigaction sigquit_action
;
1760 struct sigaction sighup_action
;
1762 #else /* !POSIX_SIGNALS */
1765 SIGTYPE (*sigchld
)();
1768 #endif /* !POSIX_SIGNALS */
1769 /* Use volatile to protect variables from being clobbered by longjmp. */
1770 volatile int forkin
, forkout
;
1771 volatile int pty_flag
= 0;
1773 extern char **environ
;
1776 inchannel
= outchannel
= -1;
1779 if (!NILP (Vprocess_connection_type
))
1780 outchannel
= inchannel
= allocate_pty ();
1784 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1785 /* On most USG systems it does not work to open the pty's tty here,
1786 then close it and reopen it in the child. */
1788 /* Don't let this terminal become our controlling terminal
1789 (in case we don't have one). */
1790 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1792 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1795 report_file_error ("Opening pty", Qnil
);
1796 #if defined (RTU) || defined (UNIPLUS) || defined (DONT_REOPEN_PTY)
1797 /* In the case that vfork is defined as fork, the parent process
1798 (Emacs) may send some data before the child process completes
1799 tty options setup. So we setup tty before forking. */
1800 child_setup_tty (forkout
);
1801 #endif /* RTU or UNIPLUS or DONT_REOPEN_PTY */
1803 forkin
= forkout
= -1;
1804 #endif /* not USG, or USG_SUBTTY_WORKS */
1808 #endif /* HAVE_PTYS */
1811 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1812 report_file_error ("Opening socketpair", Qnil
);
1813 outchannel
= inchannel
= sv
[0];
1814 forkout
= forkin
= sv
[1];
1816 #else /* not SKTPAIR */
1821 report_file_error ("Creating pipe", Qnil
);
1827 emacs_close (inchannel
);
1828 emacs_close (forkout
);
1829 report_file_error ("Creating pipe", Qnil
);
1834 #endif /* not SKTPAIR */
1837 /* Replaced by close_process_descs */
1838 set_exclusive_use (inchannel
);
1839 set_exclusive_use (outchannel
);
1842 /* Stride people say it's a mystery why this is needed
1843 as well as the O_NDELAY, but that it fails without this. */
1844 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1847 ioctl (inchannel
, FIONBIO
, &one
);
1852 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1853 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1856 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1857 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1861 /* Record this as an active process, with its channels.
1862 As a result, child_setup will close Emacs's side of the pipes. */
1863 chan_process
[inchannel
] = process
;
1864 XSETINT (XPROCESS (process
)->infd
, inchannel
);
1865 XSETINT (XPROCESS (process
)->outfd
, outchannel
);
1867 /* Previously we recorded the tty descriptor used in the subprocess.
1868 It was only used for getting the foreground tty process, so now
1869 we just reopen the device (see emacs_get_tty_pgrp) as this is
1870 more portable (see USG_SUBTTY_WORKS above). */
1872 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1873 XPROCESS (process
)->status
= Qrun
;
1874 setup_process_coding_systems (process
);
1876 /* Delay interrupts until we have a chance to store
1877 the new fork's pid in its process structure */
1878 #ifdef POSIX_SIGNALS
1879 sigemptyset (&blocked
);
1881 sigaddset (&blocked
, SIGCHLD
);
1883 #ifdef HAVE_WORKING_VFORK
1884 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1885 this sets the parent's signal handlers as well as the child's.
1886 So delay all interrupts whose handlers the child might munge,
1887 and record the current handlers so they can be restored later. */
1888 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1889 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1891 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1893 #endif /* HAVE_WORKING_VFORK */
1894 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1895 #else /* !POSIX_SIGNALS */
1899 #else /* not BSD4_1 */
1900 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1901 sigsetmask (sigmask (SIGCHLD
));
1902 #else /* ordinary USG */
1904 sigchld_deferred
= 0;
1905 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1907 #endif /* ordinary USG */
1908 #endif /* not BSD4_1 */
1909 #endif /* SIGCHLD */
1910 #endif /* !POSIX_SIGNALS */
1912 FD_SET (inchannel
, &input_wait_mask
);
1913 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1914 if (inchannel
> max_process_desc
)
1915 max_process_desc
= inchannel
;
1917 /* Until we store the proper pid, enable sigchld_handler
1918 to recognize an unknown pid as standing for this process.
1919 It is very important not to let this `marker' value stay
1920 in the table after this function has returned; if it does
1921 it might cause call-process to hang and subsequent asynchronous
1922 processes to get their return values scrambled. */
1923 XSETINT (XPROCESS (process
)->pid
, -1);
1928 /* child_setup must clobber environ on systems with true vfork.
1929 Protect it from permanent change. */
1930 char **save_environ
= environ
;
1932 current_dir
= ENCODE_FILE (current_dir
);
1937 #endif /* not WINDOWSNT */
1939 int xforkin
= forkin
;
1940 int xforkout
= forkout
;
1942 #if 0 /* This was probably a mistake--it duplicates code later on,
1943 but fails to handle all the cases. */
1944 /* Make sure SIGCHLD is not blocked in the child. */
1945 sigsetmask (SIGEMPTYMASK
);
1948 /* Make the pty be the controlling terminal of the process. */
1950 /* First, disconnect its current controlling terminal. */
1952 /* We tried doing setsid only if pty_flag, but it caused
1953 process_set_signal to fail on SGI when using a pipe. */
1955 /* Make the pty's terminal the controlling terminal. */
1959 /* We ignore the return value
1960 because faith@cs.unc.edu says that is necessary on Linux. */
1961 ioctl (xforkin
, TIOCSCTTY
, 0);
1964 #else /* not HAVE_SETSID */
1966 /* It's very important to call setpgrp here and no time
1967 afterwards. Otherwise, we lose our controlling tty which
1968 is set when we open the pty. */
1971 #endif /* not HAVE_SETSID */
1972 #if defined (HAVE_TERMIOS) && defined (LDISC1)
1973 if (pty_flag
&& xforkin
>= 0)
1976 tcgetattr (xforkin
, &t
);
1978 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1979 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1982 #if defined (NTTYDISC) && defined (TIOCSETD)
1983 if (pty_flag
&& xforkin
>= 0)
1985 /* Use new line discipline. */
1986 int ldisc
= NTTYDISC
;
1987 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1992 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1993 can do TIOCSPGRP only to the process's controlling tty. */
1996 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1997 I can't test it since I don't have 4.3. */
1998 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
1999 ioctl (j
, TIOCNOTTY
, 0);
2002 /* In order to get a controlling terminal on some versions
2003 of BSD, it is necessary to put the process in pgrp 0
2004 before it opens the terminal. */
2012 #endif /* TIOCNOTTY */
2014 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
2015 /*** There is a suggestion that this ought to be a
2016 conditional on TIOCSPGRP,
2017 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2018 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2019 that system does seem to need this code, even though
2020 both HAVE_SETSID and TIOCSCTTY are defined. */
2021 /* Now close the pty (if we had it open) and reopen it.
2022 This makes the pty the controlling terminal of the subprocess. */
2025 #ifdef SET_CHILD_PTY_PGRP
2026 int pgrp
= getpid ();
2029 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2032 emacs_close (xforkin
);
2033 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2037 emacs_write (1, "Couldn't open the pty terminal ", 31);
2038 emacs_write (1, pty_name
, strlen (pty_name
));
2039 emacs_write (1, "\n", 1);
2043 #ifdef SET_CHILD_PTY_PGRP
2044 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
2045 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
2048 #endif /* not UNIPLUS and not RTU and not DONT_REOPEN_PTY */
2050 #ifdef SETUP_SLAVE_PTY
2055 #endif /* SETUP_SLAVE_PTY */
2057 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2058 Now reenable it in the child, so it will die when we want it to. */
2060 signal (SIGHUP
, SIG_DFL
);
2062 #endif /* HAVE_PTYS */
2064 signal (SIGINT
, SIG_DFL
);
2065 signal (SIGQUIT
, SIG_DFL
);
2067 /* Stop blocking signals in the child. */
2068 #ifdef POSIX_SIGNALS
2069 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2070 #else /* !POSIX_SIGNALS */
2074 #else /* not BSD4_1 */
2075 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
2076 sigsetmask (SIGEMPTYMASK
);
2077 #else /* ordinary USG */
2079 signal (SIGCHLD
, sigchld
);
2081 #endif /* ordinary USG */
2082 #endif /* not BSD4_1 */
2083 #endif /* SIGCHLD */
2084 #endif /* !POSIX_SIGNALS */
2086 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
2088 child_setup_tty (xforkout
);
2089 #endif /* not RTU and not UNIPLUS and not DONT_REOPEN_PTY */
2091 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2092 new_argv
, 1, current_dir
);
2093 #else /* not WINDOWSNT */
2094 child_setup (xforkin
, xforkout
, xforkout
,
2095 new_argv
, 1, current_dir
);
2096 #endif /* not WINDOWSNT */
2098 environ
= save_environ
;
2103 /* This runs in the Emacs process. */
2107 emacs_close (forkin
);
2108 if (forkin
!= forkout
&& forkout
>= 0)
2109 emacs_close (forkout
);
2113 /* vfork succeeded. */
2114 XSETFASTINT (XPROCESS (process
)->pid
, pid
);
2117 register_child (pid
, inchannel
);
2118 #endif /* WINDOWSNT */
2120 /* If the subfork execv fails, and it exits,
2121 this close hangs. I don't know why.
2122 So have an interrupt jar it loose. */
2124 struct atimer
*timer
;
2128 EMACS_SET_SECS_USECS (offset
, 1, 0);
2129 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2132 emacs_close (forkin
);
2134 cancel_atimer (timer
);
2138 if (forkin
!= forkout
&& forkout
>= 0)
2139 emacs_close (forkout
);
2143 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2146 XPROCESS (process
)->tty_name
= Qnil
;
2149 /* Restore the signal state whether vfork succeeded or not.
2150 (We will signal an error, below, if it failed.) */
2151 #ifdef POSIX_SIGNALS
2152 #ifdef HAVE_WORKING_VFORK
2153 /* Restore the parent's signal handlers. */
2154 sigaction (SIGINT
, &sigint_action
, 0);
2155 sigaction (SIGQUIT
, &sigquit_action
, 0);
2157 sigaction (SIGHUP
, &sighup_action
, 0);
2159 #endif /* HAVE_WORKING_VFORK */
2160 /* Stop blocking signals in the parent. */
2161 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2162 #else /* !POSIX_SIGNALS */
2166 #else /* not BSD4_1 */
2167 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
2168 sigsetmask (SIGEMPTYMASK
);
2169 #else /* ordinary USG */
2171 signal (SIGCHLD
, sigchld
);
2172 /* Now really handle any of these signals
2173 that came in during this function. */
2174 if (sigchld_deferred
)
2175 kill (getpid (), SIGCHLD
);
2177 #endif /* ordinary USG */
2178 #endif /* not BSD4_1 */
2179 #endif /* SIGCHLD */
2180 #endif /* !POSIX_SIGNALS */
2182 /* Now generate the error if vfork failed. */
2184 report_file_error ("Doing vfork", Qnil
);
2186 #endif /* not VMS */
2191 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2192 The address family of sa is not included in the result. */
2195 conv_sockaddr_to_lisp (sa
, len
)
2196 struct sockaddr
*sa
;
2199 Lisp_Object address
;
2202 register struct Lisp_Vector
*p
;
2204 switch (sa
->sa_family
)
2208 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2209 len
= sizeof (sin
->sin_addr
) + 1;
2210 address
= Fmake_vector (make_number (len
), Qnil
);
2211 p
= XVECTOR (address
);
2212 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2213 cp
= (unsigned char *)&sin
->sin_addr
;
2216 #ifdef HAVE_LOCAL_SOCKETS
2219 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2220 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2221 if (sockun
->sun_path
[i
] == 0)
2223 return make_unibyte_string (sockun
->sun_path
, i
);
2227 len
-= sizeof (sa
->sa_family
);
2228 address
= Fcons (make_number (sa
->sa_family
),
2229 Fmake_vector (make_number (len
), Qnil
));
2230 p
= XVECTOR (XCDR (address
));
2231 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2237 p
->contents
[i
++] = make_number (*cp
++);
2243 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2246 get_lisp_to_sockaddr_size (address
, familyp
)
2247 Lisp_Object address
;
2250 register struct Lisp_Vector
*p
;
2252 if (VECTORP (address
))
2254 p
= XVECTOR (address
);
2258 return sizeof (struct sockaddr_in
);
2261 #ifdef HAVE_LOCAL_SOCKETS
2262 else if (STRINGP (address
))
2264 *familyp
= AF_LOCAL
;
2265 return sizeof (struct sockaddr_un
);
2268 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2270 struct sockaddr
*sa
;
2271 *familyp
= XINT (XCAR (address
));
2272 p
= XVECTOR (XCDR (address
));
2273 return p
->size
+ sizeof (sa
->sa_family
);
2278 /* Convert an address object (vector or string) to an internal sockaddr.
2279 Format of address has already been validated by size_lisp_to_sockaddr. */
2282 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2284 Lisp_Object address
;
2285 struct sockaddr
*sa
;
2288 register struct Lisp_Vector
*p
;
2289 register unsigned char *cp
= NULL
;
2293 sa
->sa_family
= family
;
2295 if (VECTORP (address
))
2297 p
= XVECTOR (address
);
2298 if (family
== AF_INET
)
2300 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2301 len
= sizeof (sin
->sin_addr
) + 1;
2302 i
= XINT (p
->contents
[--len
]);
2303 sin
->sin_port
= htons (i
);
2304 cp
= (unsigned char *)&sin
->sin_addr
;
2307 else if (STRINGP (address
))
2309 #ifdef HAVE_LOCAL_SOCKETS
2310 if (family
== AF_LOCAL
)
2312 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2313 cp
= SDATA (address
);
2314 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2315 sockun
->sun_path
[i
] = *cp
++;
2322 p
= XVECTOR (XCDR (address
));
2323 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2326 for (i
= 0; i
< len
; i
++)
2327 if (INTEGERP (p
->contents
[i
]))
2328 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2331 #ifdef DATAGRAM_SOCKETS
2332 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2334 doc
: /* Get the current datagram address associated with PROCESS. */)
2336 Lisp_Object process
;
2340 CHECK_PROCESS (process
);
2342 if (!DATAGRAM_CONN_P (process
))
2345 channel
= XINT (XPROCESS (process
)->infd
);
2346 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2347 datagram_address
[channel
].len
);
2350 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2352 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2353 Returns nil upon error setting address, ADDRESS otherwise. */)
2355 Lisp_Object process
, address
;
2360 CHECK_PROCESS (process
);
2362 if (!DATAGRAM_CONN_P (process
))
2365 channel
= XINT (XPROCESS (process
)->infd
);
2367 len
= get_lisp_to_sockaddr_size (address
, &family
);
2368 if (datagram_address
[channel
].len
!= len
)
2370 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2376 static struct socket_options
{
2377 /* The name of this option. Should be lowercase version of option
2378 name without SO_ prefix. */
2380 /* Option level SOL_... */
2382 /* Option number SO_... */
2384 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2385 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2386 } socket_options
[] =
2388 #ifdef SO_BINDTODEVICE
2389 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2392 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2395 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2398 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2401 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2404 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2407 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2410 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2412 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2415 /* Set option OPT to value VAL on socket S.
2417 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2418 Signals an error if setting a known option fails.
2422 set_socket_option (s
, opt
, val
)
2424 Lisp_Object opt
, val
;
2427 struct socket_options
*sopt
;
2432 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2433 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2434 if (strcmp (name
, sopt
->name
) == 0)
2437 switch (sopt
->opttype
)
2442 optval
= NILP (val
) ? 0 : 1;
2443 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2444 &optval
, sizeof (optval
));
2452 optval
= XINT (val
);
2454 error ("Bad option value for %s", name
);
2455 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2456 &optval
, sizeof (optval
));
2460 #ifdef SO_BINDTODEVICE
2463 char devname
[IFNAMSIZ
+1];
2465 /* This is broken, at least in the Linux 2.4 kernel.
2466 To unbind, the arg must be a zero integer, not the empty string.
2467 This should work on all systems. KFS. 2003-09-23. */
2468 bzero (devname
, sizeof devname
);
2471 char *arg
= (char *) SDATA (val
);
2472 int len
= min (strlen (arg
), IFNAMSIZ
);
2473 bcopy (arg
, devname
, len
);
2475 else if (!NILP (val
))
2476 error ("Bad option value for %s", name
);
2477 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2486 struct linger linger
;
2489 linger
.l_linger
= 0;
2491 linger
.l_linger
= XINT (val
);
2493 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2494 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2495 &linger
, sizeof (linger
));
2505 report_file_error ("Cannot set network option",
2506 Fcons (opt
, Fcons (val
, Qnil
)));
2507 return (1 << sopt
->optbit
);
2511 DEFUN ("set-network-process-option",
2512 Fset_network_process_option
, Sset_network_process_option
,
2514 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2515 See `make-network-process' for a list of options and values.
2516 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2517 OPTION is not a supported option, return nil instead; otherwise return t. */)
2518 (process
, option
, value
, no_error
)
2519 Lisp_Object process
, option
, value
;
2520 Lisp_Object no_error
;
2523 struct Lisp_Process
*p
;
2525 CHECK_PROCESS (process
);
2526 p
= XPROCESS (process
);
2527 if (!NETCONN1_P (p
))
2528 error ("Process is not a network process");
2532 error ("Process is not running");
2534 if (set_socket_option (s
, option
, value
))
2536 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2540 if (NILP (no_error
))
2541 error ("Unknown or unsupported option");
2547 /* A version of request_sigio suitable for a record_unwind_protect. */
2550 unwind_request_sigio (dummy
)
2553 if (interrupt_input
)
2558 /* Create a network stream/datagram client/server process. Treated
2559 exactly like a normal process when reading and writing. Primary
2560 differences are in status display and process deletion. A network
2561 connection has no PID; you cannot signal it. All you can do is
2562 stop/continue it and deactivate/close it via delete-process */
2564 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2566 doc
: /* Create and return a network server or client process.
2568 In Emacs, network connections are represented by process objects, so
2569 input and output work as for subprocesses and `delete-process' closes
2570 a network connection. However, a network process has no process id,
2571 it cannot be signaled, and the status codes are different from normal
2574 Arguments are specified as keyword/argument pairs. The following
2575 arguments are defined:
2577 :name NAME -- NAME is name for process. It is modified if necessary
2580 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2581 with the process. Process output goes at end of that buffer, unless
2582 you specify an output stream or filter function to handle the output.
2583 BUFFER may be also nil, meaning that this process is not associated
2586 :host HOST -- HOST is name of the host to connect to, or its IP
2587 address. The symbol `local' specifies the local host. If specified
2588 for a server process, it must be a valid name or address for the local
2589 host, and only clients connecting to that address will be accepted.
2591 :service SERVICE -- SERVICE is name of the service desired, or an
2592 integer specifying a port number to connect to. If SERVICE is t,
2593 a random port number is selected for the server.
2595 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2596 stream type connection, `datagram' creates a datagram type connection.
2598 :family FAMILY -- FAMILY is the address (and protocol) family for the
2599 service specified by HOST and SERVICE. The default address family is
2600 Inet (or IPv4) for the host and port number specified by HOST and
2601 SERVICE. Other address families supported are:
2602 local -- for a local (i.e. UNIX) address specified by SERVICE.
2604 :local ADDRESS -- ADDRESS is the local address used for the connection.
2605 This parameter is ignored when opening a client process. When specified
2606 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2608 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2609 connection. This parameter is ignored when opening a stream server
2610 process. For a datagram server process, it specifies the initial
2611 setting of the remote datagram address. When specified for a client
2612 process, the FAMILY, HOST, and SERVICE args are ignored.
2614 The format of ADDRESS depends on the address family:
2615 - An IPv4 address is represented as an vector of integers [A B C D P]
2616 corresponding to numeric IP address A.B.C.D and port number P.
2617 - A local address is represented as a string with the address in the
2618 local address space.
2619 - An "unsupported family" address is represented by a cons (F . AV)
2620 where F is the family number and AV is a vector containing the socket
2621 address data with one element per address data byte. Do not rely on
2622 this format in portable code, as it may depend on implementation
2623 defined constants, data sizes, and data structure alignment.
2625 :coding CODING -- If CODING is a symbol, it specifies the coding
2626 system used for both reading and writing for this process. If CODING
2627 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2628 ENCODING is used for writing.
2630 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2631 return without waiting for the connection to complete; instead, the
2632 sentinel function will be called with second arg matching "open" (if
2633 successful) or "failed" when the connect completes. Default is to use
2634 a blocking connect (i.e. wait) for stream type connections.
2636 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2637 running when Emacs is exited.
2639 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2640 In the stopped state, a server process does not accept new
2641 connections, and a client process does not handle incoming traffic.
2642 The stopped state is cleared by `continue-process' and set by
2645 :filter FILTER -- Install FILTER as the process filter.
2647 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
2648 process filter are multibyte, otherwise they are unibyte.
2649 If this keyword is not specified, the strings are multibyte iff
2650 `default-enable-multibyte-characters' is non-nil.
2652 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2654 :log LOG -- Install LOG as the server process log function. This
2655 function is called when the server accepts a network connection from a
2656 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2657 is the server process, CLIENT is the new process for the connection,
2658 and MESSAGE is a string.
2660 :plist PLIST -- Install PLIST as the new process' initial plist.
2662 :server QLEN -- if QLEN is non-nil, create a server process for the
2663 specified FAMILY, SERVICE, and connection type (stream or datagram).
2664 If QLEN is an integer, it is used as the max. length of the server's
2665 pending connection queue (also known as the backlog); the default
2666 queue length is 5. Default is to create a client process.
2668 The following network options can be specified for this connection:
2670 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2671 :dontroute BOOL -- Only send to directly connected hosts.
2672 :keepalive BOOL -- Send keep-alive messages on network stream.
2673 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2674 :oobinline BOOL -- Place out-of-band data in receive data stream.
2675 :priority INT -- Set protocol defined priority for sent packets.
2676 :reuseaddr BOOL -- Allow reusing a recently used local address
2677 (this is allowed by default for a server process).
2678 :bindtodevice NAME -- bind to interface NAME. Using this may require
2679 special privileges on some systems.
2681 Consult the relevant system programmer's manual pages for more
2682 information on using these options.
2685 A server process will listen for and accept connections from clients.
2686 When a client connection is accepted, a new network process is created
2687 for the connection with the following parameters:
2689 - The client's process name is constructed by concatenating the server
2690 process' NAME and a client identification string.
2691 - If the FILTER argument is non-nil, the client process will not get a
2692 separate process buffer; otherwise, the client's process buffer is a newly
2693 created buffer named after the server process' BUFFER name or process
2694 NAME concatenated with the client identification string.
2695 - The connection type and the process filter and sentinel parameters are
2696 inherited from the server process' TYPE, FILTER and SENTINEL.
2697 - The client process' contact info is set according to the client's
2698 addressing information (typically an IP address and a port number).
2699 - The client process' plist is initialized from the server's plist.
2701 Notice that the FILTER and SENTINEL args are never used directly by
2702 the server process. Also, the BUFFER argument is not used directly by
2703 the server process, but via the optional :log function, accepted (and
2704 failed) connections may be logged in the server process' buffer.
2706 The original argument list, modified with the actual connection
2707 information, is available via the `process-contact' function.
2709 usage: (make-network-process &rest ARGS) */)
2715 Lisp_Object contact
;
2716 struct Lisp_Process
*p
;
2717 #ifdef HAVE_GETADDRINFO
2718 struct addrinfo ai
, *res
, *lres
;
2719 struct addrinfo hints
;
2720 char *portstring
, portbuf
[128];
2721 #else /* HAVE_GETADDRINFO */
2722 struct _emacs_addrinfo
2728 struct sockaddr
*ai_addr
;
2729 struct _emacs_addrinfo
*ai_next
;
2731 #endif /* HAVE_GETADDRINFO */
2732 struct sockaddr_in address_in
;
2733 #ifdef HAVE_LOCAL_SOCKETS
2734 struct sockaddr_un address_un
;
2739 int s
= -1, outch
, inch
;
2740 struct gcpro gcpro1
;
2741 int count
= SPECPDL_INDEX ();
2743 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
2745 Lisp_Object name
, buffer
, host
, service
, address
;
2746 Lisp_Object filter
, sentinel
;
2747 int is_non_blocking_client
= 0;
2748 int is_server
= 0, backlog
= 5;
2755 /* Save arguments for process-contact and clone-process. */
2756 contact
= Flist (nargs
, args
);
2760 /* Ensure socket support is loaded if available. */
2761 init_winsock (TRUE
);
2764 /* :type TYPE (nil: stream, datagram */
2765 tem
= Fplist_get (contact
, QCtype
);
2767 socktype
= SOCK_STREAM
;
2768 #ifdef DATAGRAM_SOCKETS
2769 else if (EQ (tem
, Qdatagram
))
2770 socktype
= SOCK_DGRAM
;
2773 error ("Unsupported connection type");
2776 tem
= Fplist_get (contact
, QCserver
);
2779 /* Don't support network sockets when non-blocking mode is
2780 not available, since a blocked Emacs is not useful. */
2781 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY))
2782 error ("Network servers not supported");
2786 backlog
= XINT (tem
);
2790 /* Make QCaddress an alias for :local (server) or :remote (client). */
2791 QCaddress
= is_server
? QClocal
: QCremote
;
2794 if (!is_server
&& socktype
== SOCK_STREAM
2795 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
2797 #ifndef NON_BLOCKING_CONNECT
2798 error ("Non-blocking connect not supported");
2800 is_non_blocking_client
= 1;
2804 name
= Fplist_get (contact
, QCname
);
2805 buffer
= Fplist_get (contact
, QCbuffer
);
2806 filter
= Fplist_get (contact
, QCfilter
);
2807 sentinel
= Fplist_get (contact
, QCsentinel
);
2809 CHECK_STRING (name
);
2812 /* Let's handle TERM before things get complicated ... */
2813 host
= Fplist_get (contact
, QChost
);
2814 CHECK_STRING (host
);
2816 service
= Fplist_get (contact
, QCservice
);
2817 if (INTEGERP (service
))
2818 port
= htons ((unsigned short) XINT (service
));
2821 struct servent
*svc_info
;
2822 CHECK_STRING (service
);
2823 svc_info
= getservbyname (SDATA (service
), "tcp");
2825 error ("Unknown service: %s", SDATA (service
));
2826 port
= svc_info
->s_port
;
2829 s
= connect_server (0);
2831 report_file_error ("error creating socket", Fcons (name
, Qnil
));
2832 send_command (s
, C_PORT
, 0, "%s:%d", SDATA (host
), ntohs (port
));
2833 send_command (s
, C_DUMB
, 1, 0);
2835 #else /* not TERM */
2837 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
2838 ai
.ai_socktype
= socktype
;
2843 /* :local ADDRESS or :remote ADDRESS */
2844 address
= Fplist_get (contact
, QCaddress
);
2845 if (!NILP (address
))
2847 host
= service
= Qnil
;
2849 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
2850 error ("Malformed :address");
2851 ai
.ai_family
= family
;
2852 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
2853 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
2857 /* :family FAMILY -- nil (for Inet), local, or integer. */
2858 tem
= Fplist_get (contact
, QCfamily
);
2860 family
= XINT (tem
);
2865 #ifdef HAVE_LOCAL_SOCKETS
2866 else if (EQ (tem
, Qlocal
))
2871 error ("Unknown address family");
2872 ai
.ai_family
= family
;
2874 /* :service SERVICE -- string, integer (port number), or t (random port). */
2875 service
= Fplist_get (contact
, QCservice
);
2877 #ifdef HAVE_LOCAL_SOCKETS
2878 if (family
== AF_LOCAL
)
2880 /* Host is not used. */
2882 CHECK_STRING (service
);
2883 bzero (&address_un
, sizeof address_un
);
2884 address_un
.sun_family
= AF_LOCAL
;
2885 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
2886 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
2887 ai
.ai_addrlen
= sizeof address_un
;
2892 /* :host HOST -- hostname, ip address, or 'local for localhost. */
2893 host
= Fplist_get (contact
, QChost
);
2896 if (EQ (host
, Qlocal
))
2897 host
= build_string ("localhost");
2898 CHECK_STRING (host
);
2901 /* Slow down polling to every ten seconds.
2902 Some kernels have a bug which causes retrying connect to fail
2903 after a connect. Polling can interfere with gethostbyname too. */
2904 #ifdef POLL_FOR_INPUT
2905 if (socktype
== SOCK_STREAM
)
2907 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
2908 bind_polling_period (10);
2912 #ifdef HAVE_GETADDRINFO
2913 /* If we have a host, use getaddrinfo to resolve both host and service.
2914 Otherwise, use getservbyname to lookup the service. */
2918 /* SERVICE can either be a string or int.
2919 Convert to a C string for later use by getaddrinfo. */
2920 if (EQ (service
, Qt
))
2922 else if (INTEGERP (service
))
2924 sprintf (portbuf
, "%ld", (long) XINT (service
));
2925 portstring
= portbuf
;
2929 CHECK_STRING (service
);
2930 portstring
= SDATA (service
);
2935 memset (&hints
, 0, sizeof (hints
));
2937 hints
.ai_family
= NILP (Fplist_member (contact
, QCfamily
)) ? AF_UNSPEC
: family
;
2938 hints
.ai_socktype
= socktype
;
2939 hints
.ai_protocol
= 0;
2940 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
2942 #ifdef HAVE_GAI_STRERROR
2943 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
2945 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
2951 #endif /* HAVE_GETADDRINFO */
2953 /* We end up here if getaddrinfo is not defined, or in case no hostname
2954 has been specified (e.g. for a local server process). */
2956 if (EQ (service
, Qt
))
2958 else if (INTEGERP (service
))
2959 port
= htons ((unsigned short) XINT (service
));
2962 struct servent
*svc_info
;
2963 CHECK_STRING (service
);
2964 svc_info
= getservbyname (SDATA (service
),
2965 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
2967 error ("Unknown service: %s", SDATA (service
));
2968 port
= svc_info
->s_port
;
2971 bzero (&address_in
, sizeof address_in
);
2972 address_in
.sin_family
= family
;
2973 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
2974 address_in
.sin_port
= port
;
2976 #ifndef HAVE_GETADDRINFO
2979 struct hostent
*host_info_ptr
;
2981 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
2982 as it may `hang' Emacs for a very long time. */
2985 host_info_ptr
= gethostbyname (SDATA (host
));
2990 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
2991 host_info_ptr
->h_length
);
2992 family
= host_info_ptr
->h_addrtype
;
2993 address_in
.sin_family
= family
;
2996 /* Attempt to interpret host as numeric inet address */
2998 IN_ADDR numeric_addr
;
2999 numeric_addr
= inet_addr ((char *) SDATA (host
));
3000 if (NUMERIC_ADDR_ERROR
)
3001 error ("Unknown host \"%s\"", SDATA (host
));
3003 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3004 sizeof (address_in
.sin_addr
));
3008 #endif /* not HAVE_GETADDRINFO */
3010 ai
.ai_family
= family
;
3011 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3012 ai
.ai_addrlen
= sizeof address_in
;
3016 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
3017 when connect is interrupted. So let's not let it get interrupted.
3018 Note we do not turn off polling, because polling is only used
3019 when not interrupt_input, and thus not normally used on the systems
3020 which have this bug. On systems which use polling, there's no way
3021 to quit if polling is turned off. */
3023 && !is_server
&& socktype
== SOCK_STREAM
)
3025 /* Comment from KFS: The original open-network-stream code
3026 didn't unwind protect this, but it seems like the proper
3027 thing to do. In any case, I don't see how it could harm to
3028 do this -- and it makes cleanup (using unbind_to) easier. */
3029 record_unwind_protect (unwind_request_sigio
, Qnil
);
3033 /* Do this in case we never enter the for-loop below. */
3034 count1
= SPECPDL_INDEX ();
3037 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3043 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3050 #ifdef DATAGRAM_SOCKETS
3051 if (!is_server
&& socktype
== SOCK_DGRAM
)
3053 #endif /* DATAGRAM_SOCKETS */
3055 #ifdef NON_BLOCKING_CONNECT
3056 if (is_non_blocking_client
)
3059 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3061 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3073 /* Make us close S if quit. */
3074 record_unwind_protect (close_file_unwind
, make_number (s
));
3076 /* Parse network options in the arg list.
3077 We simply ignore anything which isn't a known option (including other keywords).
3078 An error is signalled if setting a known option fails. */
3079 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3080 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3084 /* Configure as a server socket. */
3086 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3087 explicit :reuseaddr key to override this. */
3088 #ifdef HAVE_LOCAL_SOCKETS
3089 if (family
!= AF_LOCAL
)
3091 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3094 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3095 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3098 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3099 report_file_error ("Cannot bind server socket", Qnil
);
3101 #ifdef HAVE_GETSOCKNAME
3102 if (EQ (service
, Qt
))
3104 struct sockaddr_in sa1
;
3105 int len1
= sizeof (sa1
);
3106 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3108 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3109 service
= make_number (ntohs (sa1
.sin_port
));
3110 contact
= Fplist_put (contact
, QCservice
, service
);
3115 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3116 report_file_error ("Cannot listen on server socket", Qnil
);
3124 /* This turns off all alarm-based interrupts; the
3125 bind_polling_period call above doesn't always turn all the
3126 short-interval ones off, especially if interrupt_input is
3129 It'd be nice to be able to control the connect timeout
3130 though. Would non-blocking connect calls be portable?
3132 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3134 turn_on_atimers (0);
3136 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3139 turn_on_atimers (1);
3141 if (ret
== 0 || xerrno
== EISCONN
)
3143 /* The unwind-protect will be discarded afterwards.
3144 Likewise for immediate_quit. */
3148 #ifdef NON_BLOCKING_CONNECT
3150 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3154 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3162 /* Discard the unwind protect closing S. */
3163 specpdl_ptr
= specpdl
+ count1
;
3167 if (xerrno
== EINTR
)
3173 #ifdef DATAGRAM_SOCKETS
3174 if (socktype
== SOCK_DGRAM
)
3176 if (datagram_address
[s
].sa
)
3178 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3179 datagram_address
[s
].len
= lres
->ai_addrlen
;
3183 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3184 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3187 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3188 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3189 conv_lisp_to_sockaddr (rfamily
, remote
,
3190 datagram_address
[s
].sa
, rlen
);
3194 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3197 contact
= Fplist_put (contact
, QCaddress
,
3198 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3199 #ifdef HAVE_GETSOCKNAME
3202 struct sockaddr_in sa1
;
3203 int len1
= sizeof (sa1
);
3204 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3205 contact
= Fplist_put (contact
, QClocal
,
3206 conv_sockaddr_to_lisp (&sa1
, len1
));
3211 #ifdef HAVE_GETADDRINFO
3218 /* Discard the unwind protect for closing S, if any. */
3219 specpdl_ptr
= specpdl
+ count1
;
3221 /* Unwind bind_polling_period and request_sigio. */
3222 unbind_to (count
, Qnil
);
3226 /* If non-blocking got this far - and failed - assume non-blocking is
3227 not supported after all. This is probably a wrong assumption, but
3228 the normal blocking calls to open-network-stream handles this error
3230 if (is_non_blocking_client
)
3235 report_file_error ("make server process failed", contact
);
3237 report_file_error ("make client process failed", contact
);
3240 #endif /* not TERM */
3246 buffer
= Fget_buffer_create (buffer
);
3247 proc
= make_process (name
);
3249 chan_process
[inch
] = proc
;
3252 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3255 fcntl (inch
, F_SETFL
, O_NDELAY
);
3259 p
= XPROCESS (proc
);
3261 p
->childp
= contact
;
3262 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3265 p
->sentinel
= sentinel
;
3267 p
->filter_multibyte
= buffer_defaults
.enable_multibyte_characters
;
3268 /* Override the above only if :filter-multibyte is specified. */
3269 if (! NILP (Fplist_member (contact
, QCfilter_multibyte
)))
3270 p
->filter_multibyte
= Fplist_get (contact
, QCfilter_multibyte
);
3271 p
->log
= Fplist_get (contact
, QClog
);
3272 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3273 p
->kill_without_query
= Qt
;
3274 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3277 XSETINT (p
->infd
, inch
);
3278 XSETINT (p
->outfd
, outch
);
3279 if (is_server
&& socktype
== SOCK_STREAM
)
3280 p
->status
= Qlisten
;
3282 #ifdef NON_BLOCKING_CONNECT
3283 if (is_non_blocking_client
)
3285 /* We may get here if connect did succeed immediately. However,
3286 in that case, we still need to signal this like a non-blocking
3288 p
->status
= Qconnect
;
3289 if (!FD_ISSET (inch
, &connect_wait_mask
))
3291 FD_SET (inch
, &connect_wait_mask
);
3292 num_pending_connects
++;
3297 /* A server may have a client filter setting of Qt, but it must
3298 still listen for incoming connects unless it is stopped. */
3299 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3300 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3302 FD_SET (inch
, &input_wait_mask
);
3303 FD_SET (inch
, &non_keyboard_wait_mask
);
3306 if (inch
> max_process_desc
)
3307 max_process_desc
= inch
;
3309 tem
= Fplist_member (contact
, QCcoding
);
3310 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3311 tem
= Qnil
; /* No error message (too late!). */
3314 /* Setup coding systems for communicating with the network stream. */
3315 struct gcpro gcpro1
;
3316 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3317 Lisp_Object coding_systems
= Qt
;
3318 Lisp_Object args
[5], val
;
3322 val
= XCAR (XCDR (tem
));
3326 else if (!NILP (Vcoding_system_for_read
))
3327 val
= Vcoding_system_for_read
;
3328 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3329 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3330 /* We dare not decode end-of-line format by setting VAL to
3331 Qraw_text, because the existing Emacs Lisp libraries
3332 assume that they receive bare code including a sequene of
3337 if (NILP (host
) || NILP (service
))
3338 coding_systems
= Qnil
;
3341 args
[0] = Qopen_network_stream
, args
[1] = name
,
3342 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3344 coding_systems
= Ffind_operation_coding_system (5, args
);
3347 if (CONSP (coding_systems
))
3348 val
= XCAR (coding_systems
);
3349 else if (CONSP (Vdefault_process_coding_system
))
3350 val
= XCAR (Vdefault_process_coding_system
);
3354 p
->decode_coding_system
= val
;
3358 val
= XCAR (XCDR (tem
));
3362 else if (!NILP (Vcoding_system_for_write
))
3363 val
= Vcoding_system_for_write
;
3364 else if (NILP (current_buffer
->enable_multibyte_characters
))
3368 if (EQ (coding_systems
, Qt
))
3370 if (NILP (host
) || NILP (service
))
3371 coding_systems
= Qnil
;
3374 args
[0] = Qopen_network_stream
, args
[1] = name
,
3375 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3377 coding_systems
= Ffind_operation_coding_system (5, args
);
3381 if (CONSP (coding_systems
))
3382 val
= XCDR (coding_systems
);
3383 else if (CONSP (Vdefault_process_coding_system
))
3384 val
= XCDR (Vdefault_process_coding_system
);
3388 p
->encode_coding_system
= val
;
3390 setup_process_coding_systems (proc
);
3392 p
->decoding_buf
= make_uninit_string (0);
3393 p
->decoding_carryover
= make_number (0);
3394 p
->encoding_buf
= make_uninit_string (0);
3395 p
->encoding_carryover
= make_number (0);
3397 p
->inherit_coding_system_flag
3398 = (!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
3404 #endif /* HAVE_SOCKETS */
3407 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3410 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3411 doc
: /* Return an alist of all network interfaces and their network address.
3412 Each element is a cons, the car of which is a string containing the
3413 interface name, and the cdr is the network address in internal
3414 format; see the description of ADDRESS in `make-network-process'. */)
3417 struct ifconf ifconf
;
3418 struct ifreq
*ifreqs
= NULL
;
3423 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3429 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3430 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3437 ifconf
.ifc_len
= buf_size
;
3438 ifconf
.ifc_req
= ifreqs
;
3439 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3445 if (ifconf
.ifc_len
== buf_size
)
3449 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3452 while (--ifaces
>= 0)
3454 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3455 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3456 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3458 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3459 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3460 res
= Fcons (Fcons (build_string (namebuf
),
3461 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3462 sizeof (struct sockaddr
))),
3468 #endif /* SIOCGIFCONF */
3470 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3477 static struct ifflag_def ifflag_table
[] = {
3481 #ifdef IFF_BROADCAST
3482 { IFF_BROADCAST
, "broadcast" },
3485 { IFF_DEBUG
, "debug" },
3488 { IFF_LOOPBACK
, "loopback" },
3490 #ifdef IFF_POINTOPOINT
3491 { IFF_POINTOPOINT
, "pointopoint" },
3494 { IFF_RUNNING
, "running" },
3497 { IFF_NOARP
, "noarp" },
3500 { IFF_PROMISC
, "promisc" },
3502 #ifdef IFF_NOTRAILERS
3503 { IFF_NOTRAILERS
, "notrailers" },
3506 { IFF_ALLMULTI
, "allmulti" },
3509 { IFF_MASTER
, "master" },
3512 { IFF_SLAVE
, "slave" },
3514 #ifdef IFF_MULTICAST
3515 { IFF_MULTICAST
, "multicast" },
3518 { IFF_PORTSEL
, "portsel" },
3520 #ifdef IFF_AUTOMEDIA
3521 { IFF_AUTOMEDIA
, "automedia" },
3524 { IFF_DYNAMIC
, "dynamic" },
3529 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3530 doc
: /* Return information about network interface named IFNAME.
3531 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3532 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3533 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3534 FLAGS is the current flags of the interface. */)
3539 Lisp_Object res
= Qnil
;
3544 CHECK_STRING (ifname
);
3546 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
3547 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3549 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3554 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3555 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3557 int flags
= rq
.ifr_flags
;
3558 struct ifflag_def
*fp
;
3562 for (fp
= ifflag_table
; flags
!= 0 && fp
; fp
++)
3564 if (flags
& fp
->flag_bit
)
3566 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3567 flags
-= fp
->flag_bit
;
3570 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3572 if (flags
& (1 << fnum
))
3574 elt
= Fcons (make_number (fnum
), elt
);
3579 res
= Fcons (elt
, res
);
3582 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3583 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3585 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3586 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3590 for (n
= 0; n
< 6; n
++)
3591 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3592 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3595 res
= Fcons (elt
, res
);
3598 #if defined(SIOCGIFNETMASK) && defined(ifr_netmask)
3599 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
3602 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
3605 res
= Fcons (elt
, res
);
3608 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3609 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
3612 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
3615 res
= Fcons (elt
, res
);
3618 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
3619 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
3622 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3625 res
= Fcons (elt
, res
);
3629 return any
? res
: Qnil
;
3632 #endif /* HAVE_SOCKETS */
3634 /* Turn off input and output for process PROC. */
3637 deactivate_process (proc
)
3640 register int inchannel
, outchannel
;
3641 register struct Lisp_Process
*p
= XPROCESS (proc
);
3643 inchannel
= XINT (p
->infd
);
3644 outchannel
= XINT (p
->outfd
);
3646 #ifdef ADAPTIVE_READ_BUFFERING
3647 if (XINT (p
->read_output_delay
) > 0)
3649 if (--process_output_delay_count
< 0)
3650 process_output_delay_count
= 0;
3651 XSETINT (p
->read_output_delay
, 0);
3652 p
->read_output_skip
= Qnil
;
3658 /* Beware SIGCHLD hereabouts. */
3659 flush_pending_output (inchannel
);
3662 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
3663 sys$
dassgn (outchannel
);
3664 vs
= get_vms_process_pointer (p
->pid
);
3666 give_back_vms_process_stuff (vs
);
3669 emacs_close (inchannel
);
3670 if (outchannel
>= 0 && outchannel
!= inchannel
)
3671 emacs_close (outchannel
);
3674 XSETINT (p
->infd
, -1);
3675 XSETINT (p
->outfd
, -1);
3676 #ifdef DATAGRAM_SOCKETS
3677 if (DATAGRAM_CHAN_P (inchannel
))
3679 xfree (datagram_address
[inchannel
].sa
);
3680 datagram_address
[inchannel
].sa
= 0;
3681 datagram_address
[inchannel
].len
= 0;
3684 chan_process
[inchannel
] = Qnil
;
3685 FD_CLR (inchannel
, &input_wait_mask
);
3686 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
3687 #ifdef NON_BLOCKING_CONNECT
3688 if (FD_ISSET (inchannel
, &connect_wait_mask
))
3690 FD_CLR (inchannel
, &connect_wait_mask
);
3691 if (--num_pending_connects
< 0)
3695 if (inchannel
== max_process_desc
)
3698 /* We just closed the highest-numbered process input descriptor,
3699 so recompute the highest-numbered one now. */
3700 max_process_desc
= 0;
3701 for (i
= 0; i
< MAXDESC
; i
++)
3702 if (!NILP (chan_process
[i
]))
3703 max_process_desc
= i
;
3708 /* Close all descriptors currently in use for communication
3709 with subprocess. This is used in a newly-forked subprocess
3710 to get rid of irrelevant descriptors. */
3713 close_process_descs ()
3717 for (i
= 0; i
< MAXDESC
; i
++)
3719 Lisp_Object process
;
3720 process
= chan_process
[i
];
3721 if (!NILP (process
))
3723 int in
= XINT (XPROCESS (process
)->infd
);
3724 int out
= XINT (XPROCESS (process
)->outfd
);
3727 if (out
>= 0 && in
!= out
)
3734 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
3736 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
3737 It is read into the process' buffers or given to their filter functions.
3738 Non-nil arg PROCESS means do not return until some output has been received
3740 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of
3741 seconds and microseconds to wait; return after that much time whether
3742 or not there is input.
3743 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
3744 from PROCESS, suspending reading output from other processes.
3745 If JUST-THIS-ONE is an integer, don't run any timers either.
3746 Return non-nil iff we received any output before the timeout expired. */)
3747 (process
, timeout
, timeout_msecs
, just_this_one
)
3748 register Lisp_Object process
, timeout
, timeout_msecs
, just_this_one
;
3753 if (! NILP (process
))
3754 CHECK_PROCESS (process
);
3756 just_this_one
= Qnil
;
3758 if (! NILP (timeout_msecs
))
3760 CHECK_NUMBER (timeout_msecs
);
3761 useconds
= XINT (timeout_msecs
);
3762 if (!INTEGERP (timeout
))
3763 XSETINT (timeout
, 0);
3766 int carry
= useconds
/ 1000000;
3768 XSETINT (timeout
, XINT (timeout
) + carry
);
3769 useconds
-= carry
* 1000000;
3771 /* I think this clause is necessary because C doesn't
3772 guarantee a particular rounding direction for negative
3776 XSETINT (timeout
, XINT (timeout
) - 1);
3777 useconds
+= 1000000;
3784 if (! NILP (timeout
))
3786 CHECK_NUMBER (timeout
);
3787 seconds
= XINT (timeout
);
3788 if (seconds
< 0 || (seconds
== 0 && useconds
== 0))
3792 seconds
= NILP (process
) ? -1 : 0;
3795 (wait_reading_process_output (seconds
, useconds
, 0, 0,
3797 !NILP (process
) ? XPROCESS (process
) : NULL
,
3798 NILP (just_this_one
) ? 0 :
3799 !INTEGERP (just_this_one
) ? 1 : -1)
3803 /* Accept a connection for server process SERVER on CHANNEL. */
3805 static int connect_counter
= 0;
3808 server_accept_connection (server
, channel
)
3812 Lisp_Object proc
, caller
, name
, buffer
;
3813 Lisp_Object contact
, host
, service
;
3814 struct Lisp_Process
*ps
= XPROCESS (server
);
3815 struct Lisp_Process
*p
;
3819 struct sockaddr_in in
;
3820 #ifdef HAVE_LOCAL_SOCKETS
3821 struct sockaddr_un un
;
3824 int len
= sizeof saddr
;
3826 s
= accept (channel
, &saddr
.sa
, &len
);
3835 if (code
== EWOULDBLOCK
)
3839 if (!NILP (ps
->log
))
3840 call3 (ps
->log
, server
, Qnil
,
3841 concat3 (build_string ("accept failed with code"),
3842 Fnumber_to_string (make_number (code
)),
3843 build_string ("\n")));
3849 /* Setup a new process to handle the connection. */
3851 /* Generate a unique identification of the caller, and build contact
3852 information for this process. */
3855 switch (saddr
.sa
.sa_family
)
3859 Lisp_Object args
[5];
3860 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
3861 args
[0] = build_string ("%d.%d.%d.%d");
3862 args
[1] = make_number (*ip
++);
3863 args
[2] = make_number (*ip
++);
3864 args
[3] = make_number (*ip
++);
3865 args
[4] = make_number (*ip
++);
3866 host
= Fformat (5, args
);
3867 service
= make_number (ntohs (saddr
.in
.sin_port
));
3869 args
[0] = build_string (" <%s:%d>");
3872 caller
= Fformat (3, args
);
3876 #ifdef HAVE_LOCAL_SOCKETS
3880 caller
= Fnumber_to_string (make_number (connect_counter
));
3881 caller
= concat3 (build_string (" <*"), caller
, build_string ("*>"));
3885 /* Create a new buffer name for this process if it doesn't have a
3886 filter. The new buffer name is based on the buffer name or
3887 process name of the server process concatenated with the caller
3890 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
3894 buffer
= ps
->buffer
;
3896 buffer
= Fbuffer_name (buffer
);
3901 buffer
= concat2 (buffer
, caller
);
3902 buffer
= Fget_buffer_create (buffer
);
3906 /* Generate a unique name for the new server process. Combine the
3907 server process name with the caller identification. */
3909 name
= concat2 (ps
->name
, caller
);
3910 proc
= make_process (name
);
3912 chan_process
[s
] = proc
;
3915 fcntl (s
, F_SETFL
, O_NONBLOCK
);
3918 fcntl (s
, F_SETFL
, O_NDELAY
);
3922 p
= XPROCESS (proc
);
3924 /* Build new contact information for this setup. */
3925 contact
= Fcopy_sequence (ps
->childp
);
3926 contact
= Fplist_put (contact
, QCserver
, Qnil
);
3927 contact
= Fplist_put (contact
, QChost
, host
);
3928 if (!NILP (service
))
3929 contact
= Fplist_put (contact
, QCservice
, service
);
3930 contact
= Fplist_put (contact
, QCremote
,
3931 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
3932 #ifdef HAVE_GETSOCKNAME
3934 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
3935 contact
= Fplist_put (contact
, QClocal
,
3936 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
3939 p
->childp
= contact
;
3940 p
->plist
= Fcopy_sequence (ps
->plist
);
3943 p
->sentinel
= ps
->sentinel
;
3944 p
->filter
= ps
->filter
;
3947 XSETINT (p
->infd
, s
);
3948 XSETINT (p
->outfd
, s
);
3951 /* Client processes for accepted connections are not stopped initially. */
3952 if (!EQ (p
->filter
, Qt
))
3954 FD_SET (s
, &input_wait_mask
);
3955 FD_SET (s
, &non_keyboard_wait_mask
);
3958 if (s
> max_process_desc
)
3959 max_process_desc
= s
;
3961 /* Setup coding system for new process based on server process.
3962 This seems to be the proper thing to do, as the coding system
3963 of the new process should reflect the settings at the time the
3964 server socket was opened; not the current settings. */
3966 p
->decode_coding_system
= ps
->decode_coding_system
;
3967 p
->encode_coding_system
= ps
->encode_coding_system
;
3968 setup_process_coding_systems (proc
);
3970 p
->decoding_buf
= make_uninit_string (0);
3971 p
->decoding_carryover
= make_number (0);
3972 p
->encoding_buf
= make_uninit_string (0);
3973 p
->encoding_carryover
= make_number (0);
3975 p
->inherit_coding_system_flag
3976 = (NILP (buffer
) ? Qnil
: ps
->inherit_coding_system_flag
);
3978 if (!NILP (ps
->log
))
3979 call3 (ps
->log
, server
, proc
,
3980 concat3 (build_string ("accept from "),
3981 (STRINGP (host
) ? host
: build_string ("-")),
3982 build_string ("\n")));
3984 if (!NILP (p
->sentinel
))
3985 exec_sentinel (proc
,
3986 concat3 (build_string ("open from "),
3987 (STRINGP (host
) ? host
: build_string ("-")),
3988 build_string ("\n")));
3991 /* This variable is different from waiting_for_input in keyboard.c.
3992 It is used to communicate to a lisp process-filter/sentinel (via the
3993 function Fwaiting_for_user_input_p below) whether Emacs was waiting
3994 for user-input when that process-filter was called.
3995 waiting_for_input cannot be used as that is by definition 0 when
3996 lisp code is being evalled.
3997 This is also used in record_asynch_buffer_change.
3998 For that purpose, this must be 0
3999 when not inside wait_reading_process_output. */
4000 static int waiting_for_user_input_p
;
4002 /* This is here so breakpoints can be put on it. */
4004 wait_reading_process_output_1 ()
4008 /* Read and dispose of subprocess output while waiting for timeout to
4009 elapse and/or keyboard input to be available.
4012 timeout in seconds, or
4013 zero for no limit, or
4014 -1 means gobble data immediately available but don't wait for any.
4017 an additional duration to wait, measured in microseconds.
4018 If this is nonzero and time_limit is 0, then the timeout
4019 consists of MICROSECS only.
4021 READ_KBD is a lisp value:
4022 0 to ignore keyboard input, or
4023 1 to return when input is available, or
4024 -1 meaning caller will actually read the input, so don't throw to
4025 the quit handler, or
4027 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4028 output that arrives.
4030 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4031 (and gobble terminal input into the buffer if any arrives).
4033 If WAIT_PROC is specified, wait until something arrives from that
4034 process. The return value is true iff we read some input from
4037 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4038 (suspending output from other processes). A negative value
4039 means don't run any timers either.
4041 If WAIT_PROC is specified, then the function returns true iff we
4042 received input from that process before the timeout elapsed.
4043 Otherwise, return true iff we received input from any process. */
4046 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4047 wait_for_cell
, wait_proc
, just_wait_proc
)
4048 int time_limit
, microsecs
, read_kbd
, do_display
;
4049 Lisp_Object wait_for_cell
;
4050 struct Lisp_Process
*wait_proc
;
4053 register int channel
, nfds
;
4054 SELECT_TYPE Available
;
4055 #ifdef NON_BLOCKING_CONNECT
4056 SELECT_TYPE Connecting
;
4059 int check_delay
, no_avail
;
4062 EMACS_TIME timeout
, end_time
;
4063 int wait_channel
= -1;
4064 int got_some_input
= 0;
4065 /* Either nil or a cons cell, the car of which is of interest and
4066 may be changed outside of this routine. */
4067 int saved_waiting_for_user_input_p
= waiting_for_user_input_p
;
4069 FD_ZERO (&Available
);
4070 #ifdef NON_BLOCKING_CONNECT
4071 FD_ZERO (&Connecting
);
4074 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4075 if (wait_proc
!= NULL
)
4076 wait_channel
= XINT (wait_proc
->infd
);
4078 waiting_for_user_input_p
= read_kbd
;
4080 /* Since we may need to wait several times,
4081 compute the absolute time to return at. */
4082 if (time_limit
|| microsecs
)
4084 EMACS_GET_TIME (end_time
);
4085 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4086 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4088 #ifdef POLL_INTERRUPTED_SYS_CALL
4089 /* AlainF 5-Jul-1996
4090 HP-UX 10.10 seem to have problems with signals coming in
4091 Causes "poll: interrupted system call" messages when Emacs is run
4093 Turn off periodic alarms (in case they are in use),
4094 and then turn off any other atimers. */
4096 turn_on_atimers (0);
4097 #endif /* POLL_INTERRUPTED_SYS_CALL */
4101 int timeout_reduced_for_timers
= 0;
4103 /* If calling from keyboard input, do not quit
4104 since we want to return C-g as an input character.
4105 Otherwise, do pending quit if requested. */
4109 else if (interrupt_input_pending
)
4110 handle_async_input ();
4113 /* Exit now if the cell we're waiting for became non-nil. */
4114 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4117 /* Compute time from now till when time limit is up */
4118 /* Exit if already run out */
4119 if (time_limit
== -1)
4121 /* -1 specified for timeout means
4122 gobble output available now
4123 but don't wait at all. */
4125 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4127 else if (time_limit
|| microsecs
)
4129 EMACS_GET_TIME (timeout
);
4130 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4131 if (EMACS_TIME_NEG_P (timeout
))
4136 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4139 /* Normally we run timers here.
4140 But not if wait_for_cell; in those cases,
4141 the wait is supposed to be short,
4142 and those callers cannot handle running arbitrary Lisp code here. */
4143 if (NILP (wait_for_cell
)
4144 && just_wait_proc
>= 0)
4146 EMACS_TIME timer_delay
;
4150 int old_timers_run
= timers_run
;
4151 struct buffer
*old_buffer
= current_buffer
;
4153 timer_delay
= timer_check (1);
4155 /* If a timer has run, this might have changed buffers
4156 an alike. Make read_key_sequence aware of that. */
4157 if (timers_run
!= old_timers_run
4158 && old_buffer
!= current_buffer
4159 && waiting_for_user_input_p
== -1)
4160 record_asynch_buffer_change ();
4162 if (timers_run
!= old_timers_run
&& do_display
)
4163 /* We must retry, since a timer may have requeued itself
4164 and that could alter the time_delay. */
4165 redisplay_preserve_echo_area (9);
4169 while (!detect_input_pending ());
4171 /* If there is unread keyboard input, also return. */
4173 && requeued_events_pending_p ())
4176 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4178 EMACS_TIME difference
;
4179 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4180 if (EMACS_TIME_NEG_P (difference
))
4182 timeout
= timer_delay
;
4183 timeout_reduced_for_timers
= 1;
4186 /* If time_limit is -1, we are not going to wait at all. */
4187 else if (time_limit
!= -1)
4189 /* This is so a breakpoint can be put here. */
4190 wait_reading_process_output_1 ();
4194 /* Cause C-g and alarm signals to take immediate action,
4195 and cause input available signals to zero out timeout.
4197 It is important that we do this before checking for process
4198 activity. If we get a SIGCHLD after the explicit checks for
4199 process activity, timeout is the only way we will know. */
4201 set_waiting_for_input (&timeout
);
4203 /* If status of something has changed, and no input is
4204 available, notify the user of the change right away. After
4205 this explicit check, we'll let the SIGCHLD handler zap
4206 timeout to get our attention. */
4207 if (update_tick
!= process_tick
&& do_display
)
4210 #ifdef NON_BLOCKING_CONNECT
4214 Atemp
= input_wait_mask
;
4216 /* On Mac OS X 10.0, the SELECT system call always says input is
4217 present (for reading) at stdin, even when none is. This
4218 causes the call to SELECT below to return 1 and
4219 status_notify not to be called. As a result output of
4220 subprocesses are incorrectly discarded.
4224 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4226 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4227 if ((select (max (max_process_desc
, max_keyboard_desc
) + 1,
4229 #ifdef NON_BLOCKING_CONNECT
4230 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4234 (SELECT_TYPE
*)0, &timeout
)
4237 /* It's okay for us to do this and then continue with
4238 the loop, since timeout has already been zeroed out. */
4239 clear_waiting_for_input ();
4240 status_notify (NULL
);
4244 /* Don't wait for output from a non-running process. Just
4245 read whatever data has already been received. */
4246 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
4247 update_status (wait_proc
);
4249 && ! EQ (wait_proc
->status
, Qrun
)
4250 && ! EQ (wait_proc
->status
, Qconnect
))
4252 int nread
, total_nread
= 0;
4254 clear_waiting_for_input ();
4255 XSETPROCESS (proc
, wait_proc
);
4257 /* Read data from the process, until we exhaust it. */
4258 while (XINT (wait_proc
->infd
) >= 0)
4260 nread
= read_process_output (proc
, XINT (wait_proc
->infd
));
4266 total_nread
+= nread
;
4268 else if (nread
== -1 && EIO
== errno
)
4272 else if (nread
== -1 && EAGAIN
== errno
)
4276 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4280 if (total_nread
> 0 && do_display
)
4281 redisplay_preserve_echo_area (10);
4286 /* Wait till there is something to do */
4288 if (wait_proc
&& just_wait_proc
)
4290 if (XINT (wait_proc
->infd
) < 0) /* Terminated */
4292 FD_SET (XINT (wait_proc
->infd
), &Available
);
4294 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4296 else if (!NILP (wait_for_cell
))
4298 Available
= non_process_wait_mask
;
4300 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4305 Available
= non_keyboard_wait_mask
;
4307 Available
= input_wait_mask
;
4308 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4309 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4312 /* If frame size has changed or the window is newly mapped,
4313 redisplay now, before we start to wait. There is a race
4314 condition here; if a SIGIO arrives between now and the select
4315 and indicates that a frame is trashed, the select may block
4316 displaying a trashed screen. */
4317 if (frame_garbaged
&& do_display
)
4319 clear_waiting_for_input ();
4320 redisplay_preserve_echo_area (11);
4322 set_waiting_for_input (&timeout
);
4326 if (read_kbd
&& detect_input_pending ())
4333 #ifdef NON_BLOCKING_CONNECT
4335 Connecting
= connect_wait_mask
;
4338 #ifdef ADAPTIVE_READ_BUFFERING
4339 /* Set the timeout for adaptive read buffering if any
4340 process has non-nil read_output_skip and non-zero
4341 read_output_delay, and we are not reading output for a
4342 specific wait_channel. It is not executed if
4343 Vprocess_adaptive_read_buffering is nil. */
4344 if (process_output_skip
&& check_delay
> 0)
4346 int usecs
= EMACS_USECS (timeout
);
4347 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4348 usecs
= READ_OUTPUT_DELAY_MAX
;
4349 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4351 proc
= chan_process
[channel
];
4354 /* Find minimum non-zero read_output_delay among the
4355 processes with non-nil read_output_skip. */
4356 if (XINT (XPROCESS (proc
)->read_output_delay
) > 0)
4359 if (NILP (XPROCESS (proc
)->read_output_skip
))
4361 FD_CLR (channel
, &Available
);
4362 XPROCESS (proc
)->read_output_skip
= Qnil
;
4363 if (XINT (XPROCESS (proc
)->read_output_delay
) < usecs
)
4364 usecs
= XINT (XPROCESS (proc
)->read_output_delay
);
4367 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4368 process_output_skip
= 0;
4372 nfds
= select (max (max_process_desc
, max_keyboard_desc
) + 1,
4374 #ifdef NON_BLOCKING_CONNECT
4375 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4379 (SELECT_TYPE
*)0, &timeout
);
4384 /* Make C-g and alarm signals set flags again */
4385 clear_waiting_for_input ();
4387 /* If we woke up due to SIGWINCH, actually change size now. */
4388 do_pending_window_change (0);
4390 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4391 /* We wanted the full specified time, so return now. */
4395 if (xerrno
== EINTR
)
4398 /* Ultrix select seems to return ENOMEM when it is
4399 interrupted. Treat it just like EINTR. Bleah. Note
4400 that we want to test for the "ultrix" CPP symbol, not
4401 "__ultrix__"; the latter is only defined under GCC, but
4402 not by DEC's bundled CC. -JimB */
4403 else if (xerrno
== ENOMEM
)
4407 /* This happens for no known reason on ALLIANT.
4408 I am guessing that this is the right response. -- RMS. */
4409 else if (xerrno
== EFAULT
)
4412 else if (xerrno
== EBADF
)
4415 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4416 the child's closure of the pts gives the parent a SIGHUP, and
4417 the ptc file descriptor is automatically closed,
4418 yielding EBADF here or at select() call above.
4419 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4420 in m/ibmrt-aix.h), and here we just ignore the select error.
4421 Cleanup occurs c/o status_notify after SIGCLD. */
4422 no_avail
= 1; /* Cannot depend on values returned */
4428 error ("select error: %s", emacs_strerror (xerrno
));
4433 FD_ZERO (&Available
);
4434 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4437 #if defined(sun) && !defined(USG5_4)
4438 if (nfds
> 0 && keyboard_bit_set (&Available
)
4440 /* System sometimes fails to deliver SIGIO.
4442 David J. Mackenzie says that Emacs doesn't compile under
4443 Solaris if this code is enabled, thus the USG5_4 in the CPP
4444 conditional. "I haven't noticed any ill effects so far.
4445 If you find a Solaris expert somewhere, they might know
4447 kill (getpid (), SIGIO
);
4450 #if 0 /* When polling is used, interrupt_input is 0,
4451 so get_input_pending should read the input.
4452 So this should not be needed. */
4453 /* If we are using polling for input,
4454 and we see input available, make it get read now.
4455 Otherwise it might not actually get read for a second.
4456 And on hpux, since we turn off polling in wait_reading_process_output,
4457 it might never get read at all if we don't spend much time
4458 outside of wait_reading_process_output. */
4459 if (read_kbd
&& interrupt_input
4460 && keyboard_bit_set (&Available
)
4461 && input_polling_used ())
4462 kill (getpid (), SIGALRM
);
4465 /* Check for keyboard input */
4466 /* If there is any, return immediately
4467 to give it higher priority than subprocesses */
4471 int old_timers_run
= timers_run
;
4472 struct buffer
*old_buffer
= current_buffer
;
4475 if (detect_input_pending_run_timers (do_display
))
4477 swallow_events (do_display
);
4478 if (detect_input_pending_run_timers (do_display
))
4482 /* If a timer has run, this might have changed buffers
4483 an alike. Make read_key_sequence aware of that. */
4484 if (timers_run
!= old_timers_run
4485 && waiting_for_user_input_p
== -1
4486 && old_buffer
!= current_buffer
)
4487 record_asynch_buffer_change ();
4493 /* If there is unread keyboard input, also return. */
4495 && requeued_events_pending_p ())
4498 /* If we are not checking for keyboard input now,
4499 do process events (but don't run any timers).
4500 This is so that X events will be processed.
4501 Otherwise they may have to wait until polling takes place.
4502 That would causes delays in pasting selections, for example.
4504 (We used to do this only if wait_for_cell.) */
4505 if (read_kbd
== 0 && detect_input_pending ())
4507 swallow_events (do_display
);
4508 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4509 if (detect_input_pending ())
4514 /* Exit now if the cell we're waiting for became non-nil. */
4515 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4519 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4520 go read it. This can happen with X on BSD after logging out.
4521 In that case, there really is no input and no SIGIO,
4522 but select says there is input. */
4524 if (read_kbd
&& interrupt_input
4525 && keyboard_bit_set (&Available
) && ! noninteractive
)
4526 kill (getpid (), SIGIO
);
4530 got_some_input
|= nfds
> 0;
4532 /* If checking input just got us a size-change event from X,
4533 obey it now if we should. */
4534 if (read_kbd
|| ! NILP (wait_for_cell
))
4535 do_pending_window_change (0);
4537 /* Check for data from a process. */
4538 if (no_avail
|| nfds
== 0)
4541 /* Really FIRST_PROC_DESC should be 0 on Unix,
4542 but this is safer in the short run. */
4543 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4545 if (FD_ISSET (channel
, &Available
)
4546 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
4550 /* If waiting for this channel, arrange to return as
4551 soon as no more input to be processed. No more
4553 if (wait_channel
== channel
)
4559 proc
= chan_process
[channel
];
4563 /* If this is a server stream socket, accept connection. */
4564 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4566 server_accept_connection (proc
, channel
);
4570 /* Read data from the process, starting with our
4571 buffered-ahead character if we have one. */
4573 nread
= read_process_output (proc
, channel
);
4576 /* Since read_process_output can run a filter,
4577 which can call accept-process-output,
4578 don't try to read from any other processes
4579 before doing the select again. */
4580 FD_ZERO (&Available
);
4583 redisplay_preserve_echo_area (12);
4586 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4589 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4590 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4592 else if (nread
== -1 && errno
== EAGAIN
)
4596 else if (nread
== -1 && errno
== EAGAIN
)
4598 /* Note that we cannot distinguish between no input
4599 available now and a closed pipe.
4600 With luck, a closed pipe will be accompanied by
4601 subprocess termination and SIGCHLD. */
4602 else if (nread
== 0 && !NETCONN_P (proc
))
4604 #endif /* O_NDELAY */
4605 #endif /* O_NONBLOCK */
4607 /* On some OSs with ptys, when the process on one end of
4608 a pty exits, the other end gets an error reading with
4609 errno = EIO instead of getting an EOF (0 bytes read).
4610 Therefore, if we get an error reading and errno =
4611 EIO, just continue, because the child process has
4612 exited and should clean itself up soon (e.g. when we
4615 However, it has been known to happen that the SIGCHLD
4616 got lost. So raise the signl again just in case.
4618 else if (nread
== -1 && errno
== EIO
)
4619 kill (getpid (), SIGCHLD
);
4620 #endif /* HAVE_PTYS */
4621 /* If we can detect process termination, don't consider the process
4622 gone just because its pipe is closed. */
4624 else if (nread
== 0 && !NETCONN_P (proc
))
4629 /* Preserve status of processes already terminated. */
4630 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
4631 deactivate_process (proc
);
4632 if (!NILP (XPROCESS (proc
)->raw_status_low
))
4633 update_status (XPROCESS (proc
));
4634 if (EQ (XPROCESS (proc
)->status
, Qrun
))
4635 XPROCESS (proc
)->status
4636 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
4639 #ifdef NON_BLOCKING_CONNECT
4640 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
4641 && FD_ISSET (channel
, &connect_wait_mask
))
4643 struct Lisp_Process
*p
;
4645 FD_CLR (channel
, &connect_wait_mask
);
4646 if (--num_pending_connects
< 0)
4649 proc
= chan_process
[channel
];
4653 p
= XPROCESS (proc
);
4656 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
4657 So only use it on systems where it is known to work. */
4659 int xlen
= sizeof(xerrno
);
4660 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
4665 struct sockaddr pname
;
4666 int pnamelen
= sizeof(pname
);
4668 /* If connection failed, getpeername will fail. */
4670 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
4672 /* Obtain connect failure code through error slippage. */
4675 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
4682 XSETINT (p
->tick
, ++process_tick
);
4683 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
4684 deactivate_process (proc
);
4689 /* Execute the sentinel here. If we had relied on
4690 status_notify to do it later, it will read input
4691 from the process before calling the sentinel. */
4692 exec_sentinel (proc
, build_string ("open\n"));
4693 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
4695 FD_SET (XINT (p
->infd
), &input_wait_mask
);
4696 FD_SET (XINT (p
->infd
), &non_keyboard_wait_mask
);
4700 #endif /* NON_BLOCKING_CONNECT */
4701 } /* end for each file descriptor */
4702 } /* end while exit conditions not met */
4704 waiting_for_user_input_p
= saved_waiting_for_user_input_p
;
4706 /* If calling from keyboard input, do not quit
4707 since we want to return C-g as an input character.
4708 Otherwise, do pending quit if requested. */
4711 /* Prevent input_pending from remaining set if we quit. */
4712 clear_input_pending ();
4715 #ifdef POLL_INTERRUPTED_SYS_CALL
4716 /* AlainF 5-Jul-1996
4717 HP-UX 10.10 seems to have problems with signals coming in
4718 Causes "poll: interrupted system call" messages when Emacs is run
4720 Turn periodic alarms back on */
4722 #endif /* POLL_INTERRUPTED_SYS_CALL */
4724 return got_some_input
;
4727 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
4730 read_process_output_call (fun_and_args
)
4731 Lisp_Object fun_and_args
;
4733 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
4737 read_process_output_error_handler (error
)
4740 cmd_error_internal (error
, "error in process filter: ");
4742 update_echo_area ();
4743 Fsleep_for (make_number (2), Qnil
);
4747 /* Read pending output from the process channel,
4748 starting with our buffered-ahead character if we have one.
4749 Yield number of decoded characters read.
4751 This function reads at most 4096 characters.
4752 If you want to read all available subprocess output,
4753 you must call it repeatedly until it returns zero.
4755 The characters read are decoded according to PROC's coding-system
4759 read_process_output (proc
, channel
)
4761 register int channel
;
4763 register int nbytes
;
4765 register Lisp_Object outstream
;
4766 register struct buffer
*old
= current_buffer
;
4767 register struct Lisp_Process
*p
= XPROCESS (proc
);
4768 register int opoint
;
4769 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
4770 int carryover
= XINT (p
->decoding_carryover
);
4774 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
4776 vs
= get_vms_process_pointer (p
->pid
);
4780 return (0); /* Really weird if it does this */
4781 if (!(vs
->iosb
[0] & 1))
4782 return -1; /* I/O error */
4785 error ("Could not get VMS process pointer");
4786 chars
= vs
->inputBuffer
;
4787 nbytes
= clean_vms_buffer (chars
, vs
->iosb
[1]);
4790 start_vms_process_read (vs
); /* Crank up the next read on the process */
4791 return 1; /* Nothing worth printing, say we got 1 */
4795 /* The data carried over in the previous decoding (which are at
4796 the tail of decoding buffer) should be prepended to the new
4797 data read to decode all together. */
4798 chars
= (char *) alloca (nbytes
+ carryover
);
4799 bcopy (SDATA (p
->decoding_buf
), buf
, carryover
);
4800 bcopy (vs
->inputBuffer
, chars
+ carryover
, nbytes
);
4804 chars
= (char *) alloca (carryover
+ readmax
);
4806 /* See the comment above. */
4807 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
4809 #ifdef DATAGRAM_SOCKETS
4810 /* We have a working select, so proc_buffered_char is always -1. */
4811 if (DATAGRAM_CHAN_P (channel
))
4813 int len
= datagram_address
[channel
].len
;
4814 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
4815 0, datagram_address
[channel
].sa
, &len
);
4819 if (proc_buffered_char
[channel
] < 0)
4821 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
4822 #ifdef ADAPTIVE_READ_BUFFERING
4823 if (nbytes
> 0 && !NILP (p
->adaptive_read_buffering
))
4825 int delay
= XINT (p
->read_output_delay
);
4828 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
4831 process_output_delay_count
++;
4832 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
4835 else if (delay
> 0 && (nbytes
== readmax
))
4837 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
4839 process_output_delay_count
--;
4841 XSETINT (p
->read_output_delay
, delay
);
4844 p
->read_output_skip
= Qt
;
4845 process_output_skip
= 1;
4852 chars
[carryover
] = proc_buffered_char
[channel
];
4853 proc_buffered_char
[channel
] = -1;
4854 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
4858 nbytes
= nbytes
+ 1;
4860 #endif /* not VMS */
4862 XSETINT (p
->decoding_carryover
, 0);
4864 /* At this point, NBYTES holds number of bytes just received
4865 (including the one in proc_buffered_char[channel]). */
4868 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
4870 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
4873 /* Now set NBYTES how many bytes we must decode. */
4874 nbytes
+= carryover
;
4876 /* Read and dispose of the process output. */
4877 outstream
= p
->filter
;
4878 if (!NILP (outstream
))
4880 /* We inhibit quit here instead of just catching it so that
4881 hitting ^G when a filter happens to be running won't screw
4883 int count
= SPECPDL_INDEX ();
4884 Lisp_Object odeactivate
;
4885 Lisp_Object obuffer
, okeymap
;
4887 int outer_running_asynch_code
= running_asynch_code
;
4888 int waiting
= waiting_for_user_input_p
;
4890 /* No need to gcpro these, because all we do with them later
4891 is test them for EQness, and none of them should be a string. */
4892 odeactivate
= Vdeactivate_mark
;
4893 XSETBUFFER (obuffer
, current_buffer
);
4894 okeymap
= current_buffer
->keymap
;
4896 specbind (Qinhibit_quit
, Qt
);
4897 specbind (Qlast_nonmenu_event
, Qt
);
4899 /* In case we get recursively called,
4900 and we already saved the match data nonrecursively,
4901 save the same match data in safely recursive fashion. */
4902 if (outer_running_asynch_code
)
4905 /* Don't clobber the CURRENT match data, either! */
4906 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
4907 restore_search_regs ();
4908 record_unwind_save_match_data ();
4909 Fset_match_data (tem
, Qt
);
4912 /* For speed, if a search happens within this code,
4913 save the match data in a special nonrecursive fashion. */
4914 running_asynch_code
= 1;
4916 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
4917 text
= coding
->dst_object
;
4918 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
4919 /* A new coding system might be found. */
4920 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
4922 p
->decode_coding_system
= Vlast_coding_system_used
;
4924 /* Don't call setup_coding_system for
4925 proc_decode_coding_system[channel] here. It is done in
4926 detect_coding called via decode_coding above. */
4928 /* If a coding system for encoding is not yet decided, we set
4929 it as the same as coding-system for decoding.
4931 But, before doing that we must check if
4932 proc_encode_coding_system[p->outfd] surely points to a
4933 valid memory because p->outfd will be changed once EOF is
4934 sent to the process. */
4935 if (NILP (p
->encode_coding_system
)
4936 && proc_encode_coding_system
[XINT (p
->outfd
)])
4938 p
->encode_coding_system
= Vlast_coding_system_used
;
4939 setup_coding_system (p
->encode_coding_system
,
4940 proc_encode_coding_system
[XINT (p
->outfd
)]);
4944 if (coding
->carryover_bytes
> 0)
4946 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
4947 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
4948 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
4949 coding
->carryover_bytes
);
4950 XSETINT (p
->decoding_carryover
, coding
->carryover_bytes
);
4952 /* Adjust the multibyteness of TEXT to that of the filter. */
4953 if (NILP (p
->filter_multibyte
) != ! STRING_MULTIBYTE (text
))
4954 text
= (STRING_MULTIBYTE (text
)
4955 ? Fstring_as_unibyte (text
)
4956 : Fstring_to_multibyte (text
));
4957 if (SBYTES (text
) > 0)
4958 internal_condition_case_1 (read_process_output_call
,
4960 Fcons (proc
, Fcons (text
, Qnil
))),
4961 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
4962 read_process_output_error_handler
);
4964 /* If we saved the match data nonrecursively, restore it now. */
4965 restore_search_regs ();
4966 running_asynch_code
= outer_running_asynch_code
;
4968 /* Handling the process output should not deactivate the mark. */
4969 Vdeactivate_mark
= odeactivate
;
4971 /* Restore waiting_for_user_input_p as it was
4972 when we were called, in case the filter clobbered it. */
4973 waiting_for_user_input_p
= waiting
;
4975 #if 0 /* Call record_asynch_buffer_change unconditionally,
4976 because we might have changed minor modes or other things
4977 that affect key bindings. */
4978 if (! EQ (Fcurrent_buffer (), obuffer
)
4979 || ! EQ (current_buffer
->keymap
, okeymap
))
4981 /* But do it only if the caller is actually going to read events.
4982 Otherwise there's no need to make him wake up, and it could
4983 cause trouble (for example it would make Fsit_for return). */
4984 if (waiting_for_user_input_p
== -1)
4985 record_asynch_buffer_change ();
4988 start_vms_process_read (vs
);
4990 unbind_to (count
, Qnil
);
4994 /* If no filter, write into buffer if it isn't dead. */
4995 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
4997 Lisp_Object old_read_only
;
4998 int old_begv
, old_zv
;
4999 int old_begv_byte
, old_zv_byte
;
5000 Lisp_Object odeactivate
;
5001 int before
, before_byte
;
5006 odeactivate
= Vdeactivate_mark
;
5008 Fset_buffer (p
->buffer
);
5010 opoint_byte
= PT_BYTE
;
5011 old_read_only
= current_buffer
->read_only
;
5014 old_begv_byte
= BEGV_BYTE
;
5015 old_zv_byte
= ZV_BYTE
;
5017 current_buffer
->read_only
= Qnil
;
5019 /* Insert new output into buffer
5020 at the current end-of-output marker,
5021 thus preserving logical ordering of input and output. */
5022 if (XMARKER (p
->mark
)->buffer
)
5023 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5024 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5027 SET_PT_BOTH (ZV
, ZV_BYTE
);
5029 before_byte
= PT_BYTE
;
5031 /* If the output marker is outside of the visible region, save
5032 the restriction and widen. */
5033 if (! (BEGV
<= PT
&& PT
<= ZV
))
5036 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5037 text
= coding
->dst_object
;
5038 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5039 /* A new coding system might be found. See the comment in the
5040 similar code in the previous `if' block. */
5041 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5043 p
->decode_coding_system
= Vlast_coding_system_used
;
5044 if (NILP (p
->encode_coding_system
)
5045 && proc_encode_coding_system
[XINT (p
->outfd
)])
5047 p
->encode_coding_system
= Vlast_coding_system_used
;
5048 setup_coding_system (p
->encode_coding_system
,
5049 proc_encode_coding_system
[XINT (p
->outfd
)]);
5052 if (coding
->carryover_bytes
> 0)
5054 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5055 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5056 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5057 coding
->carryover_bytes
);
5058 XSETINT (p
->decoding_carryover
, coding
->carryover_bytes
);
5060 /* Adjust the multibyteness of TEXT to that of the buffer. */
5061 if (NILP (current_buffer
->enable_multibyte_characters
)
5062 != ! STRING_MULTIBYTE (text
))
5063 text
= (STRING_MULTIBYTE (text
)
5064 ? Fstring_as_unibyte (text
)
5065 : Fstring_to_multibyte (text
));
5066 /* Insert before markers in case we are inserting where
5067 the buffer's mark is, and the user's next command is Meta-y. */
5068 insert_from_string_before_markers (text
, 0, 0,
5069 SCHARS (text
), SBYTES (text
), 0);
5071 /* Make sure the process marker's position is valid when the
5072 process buffer is changed in the signal_after_change above.
5073 W3 is known to do that. */
5074 if (BUFFERP (p
->buffer
)
5075 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5076 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5078 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5080 update_mode_lines
++;
5082 /* Make sure opoint and the old restrictions
5083 float ahead of any new text just as point would. */
5084 if (opoint
>= before
)
5086 opoint
+= PT
- before
;
5087 opoint_byte
+= PT_BYTE
- before_byte
;
5089 if (old_begv
> before
)
5091 old_begv
+= PT
- before
;
5092 old_begv_byte
+= PT_BYTE
- before_byte
;
5094 if (old_zv
>= before
)
5096 old_zv
+= PT
- before
;
5097 old_zv_byte
+= PT_BYTE
- before_byte
;
5100 /* If the restriction isn't what it should be, set it. */
5101 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5102 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5104 /* Handling the process output should not deactivate the mark. */
5105 Vdeactivate_mark
= odeactivate
;
5107 current_buffer
->read_only
= old_read_only
;
5108 SET_PT_BOTH (opoint
, opoint_byte
);
5109 set_buffer_internal (old
);
5112 start_vms_process_read (vs
);
5117 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5119 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5120 This is intended for use by asynchronous process output filters and sentinels. */)
5123 return (waiting_for_user_input_p
? Qt
: Qnil
);
5126 /* Sending data to subprocess */
5128 jmp_buf send_process_frame
;
5129 Lisp_Object process_sent_to
;
5132 send_process_trap ()
5134 SIGNAL_THREAD_CHECK (SIGPIPE
);
5139 sigunblock (sigmask (SIGPIPE
));
5140 longjmp (send_process_frame
, 1);
5143 /* Send some data to process PROC.
5144 BUF is the beginning of the data; LEN is the number of characters.
5145 OBJECT is the Lisp object that the data comes from. If OBJECT is
5146 nil or t, it means that the data comes from C string.
5148 If OBJECT is not nil, the data is encoded by PROC's coding-system
5149 for encoding before it is sent.
5151 This function can evaluate Lisp code and can garbage collect. */
5154 send_process (proc
, buf
, len
, object
)
5155 volatile Lisp_Object proc
;
5156 unsigned char *volatile buf
;
5158 volatile Lisp_Object object
;
5160 /* Use volatile to protect variables from being clobbered by longjmp. */
5161 struct Lisp_Process
*p
= XPROCESS (proc
);
5163 struct coding_system
*coding
;
5164 struct gcpro gcpro1
;
5165 SIGTYPE (*volatile old_sigpipe
) ();
5170 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5173 if (! NILP (p
->raw_status_low
))
5175 if (! EQ (p
->status
, Qrun
))
5176 error ("Process %s not running", SDATA (p
->name
));
5177 if (XINT (p
->outfd
) < 0)
5178 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5180 coding
= proc_encode_coding_system
[XINT (p
->outfd
)];
5181 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5183 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5184 || (BUFFERP (object
)
5185 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5188 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5189 /* The coding system for encoding was changed to raw-text
5190 because we sent a unibyte text previously. Now we are
5191 sending a multibyte text, thus we must encode it by the
5192 original coding system specified for the current process. */
5193 setup_coding_system (p
->encode_coding_system
, coding
);
5194 coding
->src_multibyte
= 1;
5198 /* For sending a unibyte text, character code conversion should
5199 not take place but EOL conversion should. So, setup raw-text
5200 or one of the subsidiary if we have not yet done it. */
5201 if (CODING_REQUIRE_ENCODING (coding
))
5203 if (CODING_REQUIRE_FLUSHING (coding
))
5205 /* But, before changing the coding, we must flush out data. */
5206 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5207 send_process (proc
, "", 0, Qt
);
5208 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5210 setup_coding_system (raw_text_coding_system
5211 (Vlast_coding_system_used
),
5213 coding
->src_multibyte
= 0;
5216 coding
->dst_multibyte
= 0;
5218 if (CODING_REQUIRE_ENCODING (coding
))
5220 coding
->dst_object
= Qt
;
5221 if (BUFFERP (object
))
5223 int from_byte
, from
, to
;
5224 int save_pt
, save_pt_byte
;
5225 struct buffer
*cur
= current_buffer
;
5227 set_buffer_internal (XBUFFER (object
));
5228 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5230 from_byte
= PTR_BYTE_POS (buf
);
5231 from
= BYTE_TO_CHAR (from_byte
);
5232 to
= BYTE_TO_CHAR (from_byte
+ len
);
5233 TEMP_SET_PT_BOTH (from
, from_byte
);
5234 encode_coding_object (coding
, object
, from
, from_byte
,
5235 to
, from_byte
+ len
, Qt
);
5236 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5237 set_buffer_internal (cur
);
5239 else if (STRINGP (object
))
5241 encode_coding_string (coding
, object
, 1);
5245 coding
->dst_object
= make_unibyte_string (buf
, len
);
5246 coding
->produced
= len
;
5249 len
= coding
->produced
;
5250 buf
= SDATA (coding
->dst_object
);
5254 vs
= get_vms_process_pointer (p
->pid
);
5256 error ("Could not find this process: %x", p
->pid
);
5257 else if (write_to_vms_process (vs
, buf
, len
))
5261 if (pty_max_bytes
== 0)
5263 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5264 pty_max_bytes
= fpathconf (XFASTINT (p
->outfd
), _PC_MAX_CANON
);
5265 if (pty_max_bytes
< 0)
5266 pty_max_bytes
= 250;
5268 pty_max_bytes
= 250;
5270 /* Deduct one, to leave space for the eof. */
5274 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5275 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5276 when returning with longjmp despite being declared volatile. */
5277 if (!setjmp (send_process_frame
))
5279 process_sent_to
= proc
;
5284 /* Decide how much data we can send in one batch.
5285 Long lines need to be split into multiple batches. */
5286 if (!NILP (p
->pty_flag
))
5288 /* Starting this at zero is always correct when not the first
5289 iteration because the previous iteration ended by sending C-d.
5290 It may not be correct for the first iteration
5291 if a partial line was sent in a separate send_process call.
5292 If that proves worth handling, we need to save linepos
5293 in the process object. */
5295 unsigned char *ptr
= (unsigned char *) buf
;
5296 unsigned char *end
= (unsigned char *) buf
+ len
;
5298 /* Scan through this text for a line that is too long. */
5299 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5307 /* If we found one, break the line there
5308 and put in a C-d to force the buffer through. */
5312 /* Send this batch, using one or more write calls. */
5315 int outfd
= XINT (p
->outfd
);
5316 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5317 #ifdef DATAGRAM_SOCKETS
5318 if (DATAGRAM_CHAN_P (outfd
))
5320 rv
= sendto (outfd
, (char *) buf
, this,
5321 0, datagram_address
[outfd
].sa
,
5322 datagram_address
[outfd
].len
);
5323 if (rv
< 0 && errno
== EMSGSIZE
)
5325 signal (SIGPIPE
, old_sigpipe
);
5326 report_file_error ("sending datagram",
5327 Fcons (proc
, Qnil
));
5333 rv
= emacs_write (outfd
, (char *) buf
, this);
5334 #ifdef ADAPTIVE_READ_BUFFERING
5335 if (XINT (p
->read_output_delay
) > 0
5336 && EQ (p
->adaptive_read_buffering
, Qt
))
5338 XSETFASTINT (p
->read_output_delay
, 0);
5339 process_output_delay_count
--;
5340 p
->read_output_skip
= Qnil
;
5344 signal (SIGPIPE
, old_sigpipe
);
5350 || errno
== EWOULDBLOCK
5356 /* Buffer is full. Wait, accepting input;
5357 that may allow the program
5358 to finish doing output and read more. */
5362 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5363 /* A gross hack to work around a bug in FreeBSD.
5364 In the following sequence, read(2) returns
5368 write(2) 954 bytes, get EAGAIN
5369 read(2) 1024 bytes in process_read_output
5370 read(2) 11 bytes in process_read_output
5372 That is, read(2) returns more bytes than have
5373 ever been written successfully. The 1033 bytes
5374 read are the 1022 bytes written successfully
5375 after processing (for example with CRs added if
5376 the terminal is set up that way which it is
5377 here). The same bytes will be seen again in a
5378 later read(2), without the CRs. */
5380 if (errno
== EAGAIN
)
5383 ioctl (XINT (p
->outfd
), TIOCFLUSH
, &flags
);
5385 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5387 /* Running filters might relocate buffers or strings.
5388 Arrange to relocate BUF. */
5389 if (BUFFERP (object
))
5390 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5391 else if (STRINGP (object
))
5392 offset
= buf
- SDATA (object
);
5394 #ifdef EMACS_HAS_USECS
5395 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5397 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5400 if (BUFFERP (object
))
5401 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5402 else if (STRINGP (object
))
5403 buf
= offset
+ SDATA (object
);
5408 /* This is a real error. */
5409 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5416 /* If we sent just part of the string, put in an EOF
5417 to force it through, before we send the rest. */
5419 Fprocess_send_eof (proc
);
5422 #endif /* not VMS */
5425 signal (SIGPIPE
, old_sigpipe
);
5427 proc
= process_sent_to
;
5428 p
= XPROCESS (proc
);
5430 p
->raw_status_low
= Qnil
;
5431 p
->raw_status_high
= Qnil
;
5432 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5433 XSETINT (p
->tick
, ++process_tick
);
5434 deactivate_process (proc
);
5436 error ("Error writing to process %s; closed it", SDATA (p
->name
));
5438 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5445 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5447 doc
: /* Send current contents of region as input to PROCESS.
5448 PROCESS may be a process, a buffer, the name of a process or buffer, or
5449 nil, indicating the current buffer's process.
5450 Called from program, takes three arguments, PROCESS, START and END.
5451 If the region is more than 500 characters long,
5452 it is sent in several bunches. This may happen even for shorter regions.
5453 Output from processes can arrive in between bunches. */)
5454 (process
, start
, end
)
5455 Lisp_Object process
, start
, end
;
5460 proc
= get_process (process
);
5461 validate_region (&start
, &end
);
5463 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5464 move_gap (XINT (start
));
5466 start1
= CHAR_TO_BYTE (XINT (start
));
5467 end1
= CHAR_TO_BYTE (XINT (end
));
5468 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5469 Fcurrent_buffer ());
5474 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5476 doc
: /* Send PROCESS the contents of STRING as input.
5477 PROCESS may be a process, a buffer, the name of a process or buffer, or
5478 nil, indicating the current buffer's process.
5479 If STRING is more than 500 characters long,
5480 it is sent in several bunches. This may happen even for shorter strings.
5481 Output from processes can arrive in between bunches. */)
5483 Lisp_Object process
, string
;
5486 CHECK_STRING (string
);
5487 proc
= get_process (process
);
5488 send_process (proc
, SDATA (string
),
5489 SBYTES (string
), string
);
5493 /* Return the foreground process group for the tty/pty that
5494 the process P uses. */
5496 emacs_get_tty_pgrp (p
)
5497 struct Lisp_Process
*p
;
5502 if (ioctl (XINT (p
->infd
), TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5505 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5506 master side. Try the slave side. */
5507 fd
= emacs_open (XSTRING (p
->tty_name
)->data
, O_RDONLY
, 0);
5511 ioctl (fd
, TIOCGPGRP
, &gid
);
5515 #endif /* defined (TIOCGPGRP ) */
5520 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5521 Sprocess_running_child_p
, 0, 1, 0,
5522 doc
: /* Return t if PROCESS has given the terminal to a child.
5523 If the operating system does not make it possible to find out,
5524 return t unconditionally. */)
5526 Lisp_Object process
;
5528 /* Initialize in case ioctl doesn't exist or gives an error,
5529 in a way that will cause returning t. */
5532 struct Lisp_Process
*p
;
5534 proc
= get_process (process
);
5535 p
= XPROCESS (proc
);
5537 if (!EQ (p
->childp
, Qt
))
5538 error ("Process %s is not a subprocess",
5540 if (XINT (p
->infd
) < 0)
5541 error ("Process %s is not active",
5544 gid
= emacs_get_tty_pgrp (p
);
5546 if (gid
== XFASTINT (p
->pid
))
5551 /* send a signal number SIGNO to PROCESS.
5552 If CURRENT_GROUP is t, that means send to the process group
5553 that currently owns the terminal being used to communicate with PROCESS.
5554 This is used for various commands in shell mode.
5555 If CURRENT_GROUP is lambda, that means send to the process group
5556 that currently owns the terminal, but only if it is NOT the shell itself.
5558 If NOMSG is zero, insert signal-announcements into process's buffers
5561 If we can, we try to signal PROCESS by sending control characters
5562 down the pty. This allows us to signal inferiors who have changed
5563 their uid, for which killpg would return an EPERM error. */
5566 process_send_signal (process
, signo
, current_group
, nomsg
)
5567 Lisp_Object process
;
5569 Lisp_Object current_group
;
5573 register struct Lisp_Process
*p
;
5577 proc
= get_process (process
);
5578 p
= XPROCESS (proc
);
5580 if (!EQ (p
->childp
, Qt
))
5581 error ("Process %s is not a subprocess",
5583 if (XINT (p
->infd
) < 0)
5584 error ("Process %s is not active",
5587 if (NILP (p
->pty_flag
))
5588 current_group
= Qnil
;
5590 /* If we are using pgrps, get a pgrp number and make it negative. */
5591 if (NILP (current_group
))
5592 /* Send the signal to the shell's process group. */
5593 gid
= XFASTINT (p
->pid
);
5596 #ifdef SIGNALS_VIA_CHARACTERS
5597 /* If possible, send signals to the entire pgrp
5598 by sending an input character to it. */
5600 /* TERMIOS is the latest and bestest, and seems most likely to
5601 work. If the system has it, use it. */
5604 cc_t
*sig_char
= NULL
;
5606 tcgetattr (XINT (p
->infd
), &t
);
5611 sig_char
= &t
.c_cc
[VINTR
];
5615 sig_char
= &t
.c_cc
[VQUIT
];
5619 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5620 sig_char
= &t
.c_cc
[VSWTCH
];
5622 sig_char
= &t
.c_cc
[VSUSP
];
5627 if (sig_char
&& *sig_char
!= CDISABLE
)
5629 send_process (proc
, sig_char
, 1, Qnil
);
5632 /* If we can't send the signal with a character,
5633 fall through and send it another way. */
5634 #else /* ! HAVE_TERMIOS */
5636 /* On Berkeley descendants, the following IOCTL's retrieve the
5637 current control characters. */
5638 #if defined (TIOCGLTC) && defined (TIOCGETC)
5646 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
5647 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
5650 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
5651 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
5655 ioctl (XINT (p
->infd
), TIOCGLTC
, &lc
);
5656 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
5658 #endif /* ! defined (SIGTSTP) */
5661 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5663 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
5670 ioctl (XINT (p
->infd
), TCGETA
, &t
);
5671 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
5674 ioctl (XINT (p
->infd
), TCGETA
, &t
);
5675 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
5679 ioctl (XINT (p
->infd
), TCGETA
, &t
);
5680 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
5682 #endif /* ! defined (SIGTSTP) */
5684 #else /* ! defined (TCGETA) */
5685 Your configuration files are messed up
.
5686 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
5687 you'd better be using one of the alternatives above! */
5688 #endif /* ! defined (TCGETA) */
5689 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5690 /* In this case, the code above should alway returns. */
5692 #endif /* ! defined HAVE_TERMIOS */
5694 /* The code above may fall through if it can't
5695 handle the signal. */
5696 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5699 /* Get the current pgrp using the tty itself, if we have that.
5700 Otherwise, use the pty to get the pgrp.
5701 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5702 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5703 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5704 His patch indicates that if TIOCGPGRP returns an error, then
5705 we should just assume that p->pid is also the process group id. */
5707 gid
= emacs_get_tty_pgrp (p
);
5710 /* If we can't get the information, assume
5711 the shell owns the tty. */
5712 gid
= XFASTINT (p
->pid
);
5714 /* It is not clear whether anything really can set GID to -1.
5715 Perhaps on some system one of those ioctls can or could do so.
5716 Or perhaps this is vestigial. */
5719 #else /* ! defined (TIOCGPGRP ) */
5720 /* Can't select pgrps on this system, so we know that
5721 the child itself heads the pgrp. */
5722 gid
= XFASTINT (p
->pid
);
5723 #endif /* ! defined (TIOCGPGRP ) */
5725 /* If current_group is lambda, and the shell owns the terminal,
5726 don't send any signal. */
5727 if (EQ (current_group
, Qlambda
) && gid
== XFASTINT (p
->pid
))
5735 p
->raw_status_low
= Qnil
;
5736 p
->raw_status_high
= Qnil
;
5738 XSETINT (p
->tick
, ++process_tick
);
5740 status_notify (NULL
);
5742 #endif /* ! defined (SIGCONT) */
5745 send_process (proc
, "\003", 1, Qnil
); /* ^C */
5750 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
5755 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
5758 flush_pending_output (XINT (p
->infd
));
5762 /* If we don't have process groups, send the signal to the immediate
5763 subprocess. That isn't really right, but it's better than any
5764 obvious alternative. */
5767 kill (XFASTINT (p
->pid
), signo
);
5771 /* gid may be a pid, or minus a pgrp's number */
5773 if (!NILP (current_group
))
5775 if (ioctl (XINT (p
->infd
), TIOCSIGSEND
, signo
) == -1)
5776 EMACS_KILLPG (gid
, signo
);
5780 gid
= - XFASTINT (p
->pid
);
5783 #else /* ! defined (TIOCSIGSEND) */
5784 EMACS_KILLPG (gid
, signo
);
5785 #endif /* ! defined (TIOCSIGSEND) */
5788 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
5789 doc
: /* Interrupt process PROCESS.
5790 PROCESS may be a process, a buffer, or the name of a process or buffer.
5791 No arg or nil means current buffer's process.
5792 Second arg CURRENT-GROUP non-nil means send signal to
5793 the current process-group of the process's controlling terminal
5794 rather than to the process's own process group.
5795 If the process is a shell, this means interrupt current subjob
5796 rather than the shell.
5798 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
5799 don't send the signal. */)
5800 (process
, current_group
)
5801 Lisp_Object process
, current_group
;
5803 process_send_signal (process
, SIGINT
, current_group
, 0);
5807 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
5808 doc
: /* Kill process PROCESS. May be process or name of one.
5809 See function `interrupt-process' for more details on usage. */)
5810 (process
, current_group
)
5811 Lisp_Object process
, current_group
;
5813 process_send_signal (process
, SIGKILL
, current_group
, 0);
5817 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
5818 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
5819 See function `interrupt-process' for more details on usage. */)
5820 (process
, current_group
)
5821 Lisp_Object process
, current_group
;
5823 process_send_signal (process
, SIGQUIT
, current_group
, 0);
5827 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
5828 doc
: /* Stop process PROCESS. May be process or name of one.
5829 See function `interrupt-process' for more details on usage.
5830 If PROCESS is a network process, inhibit handling of incoming traffic. */)
5831 (process
, current_group
)
5832 Lisp_Object process
, current_group
;
5835 if (PROCESSP (process
) && NETCONN_P (process
))
5837 struct Lisp_Process
*p
;
5839 p
= XPROCESS (process
);
5840 if (NILP (p
->command
)
5841 && XINT (p
->infd
) >= 0)
5843 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
5844 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
5851 error ("No SIGTSTP support");
5853 process_send_signal (process
, SIGTSTP
, current_group
, 0);
5858 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
5859 doc
: /* Continue process PROCESS. May be process or name of one.
5860 See function `interrupt-process' for more details on usage.
5861 If PROCESS is a network process, resume handling of incoming traffic. */)
5862 (process
, current_group
)
5863 Lisp_Object process
, current_group
;
5866 if (PROCESSP (process
) && NETCONN_P (process
))
5868 struct Lisp_Process
*p
;
5870 p
= XPROCESS (process
);
5871 if (EQ (p
->command
, Qt
)
5872 && XINT (p
->infd
) >= 0
5873 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
5875 FD_SET (XINT (p
->infd
), &input_wait_mask
);
5876 FD_SET (XINT (p
->infd
), &non_keyboard_wait_mask
);
5883 process_send_signal (process
, SIGCONT
, current_group
, 0);
5885 error ("No SIGCONT support");
5890 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
5891 2, 2, "sProcess (name or number): \nnSignal code: ",
5892 doc
: /* Send PROCESS the signal with code SIGCODE.
5893 PROCESS may also be an integer specifying the process id of the
5894 process to signal; in this case, the process need not be a child of
5896 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
5898 Lisp_Object process
, sigcode
;
5902 if (INTEGERP (process
))
5908 if (STRINGP (process
))
5911 if (tem
= Fget_process (process
), NILP (tem
))
5913 pid
= Fstring_to_number (process
, make_number (10));
5914 if (XINT (pid
) != 0)
5920 process
= get_process (process
);
5925 CHECK_PROCESS (process
);
5926 pid
= XPROCESS (process
)->pid
;
5927 if (!INTEGERP (pid
) || XINT (pid
) <= 0)
5928 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
5932 #define handle_signal(NAME, VALUE) \
5933 else if (!strcmp (name, NAME)) \
5934 XSETINT (sigcode, VALUE)
5936 if (INTEGERP (sigcode
))
5940 unsigned char *name
;
5942 CHECK_SYMBOL (sigcode
);
5943 name
= SDATA (SYMBOL_NAME (sigcode
));
5948 handle_signal ("SIGHUP", SIGHUP
);
5951 handle_signal ("SIGINT", SIGINT
);
5954 handle_signal ("SIGQUIT", SIGQUIT
);
5957 handle_signal ("SIGILL", SIGILL
);
5960 handle_signal ("SIGABRT", SIGABRT
);
5963 handle_signal ("SIGEMT", SIGEMT
);
5966 handle_signal ("SIGKILL", SIGKILL
);
5969 handle_signal ("SIGFPE", SIGFPE
);
5972 handle_signal ("SIGBUS", SIGBUS
);
5975 handle_signal ("SIGSEGV", SIGSEGV
);
5978 handle_signal ("SIGSYS", SIGSYS
);
5981 handle_signal ("SIGPIPE", SIGPIPE
);
5984 handle_signal ("SIGALRM", SIGALRM
);
5987 handle_signal ("SIGTERM", SIGTERM
);
5990 handle_signal ("SIGURG", SIGURG
);
5993 handle_signal ("SIGSTOP", SIGSTOP
);
5996 handle_signal ("SIGTSTP", SIGTSTP
);
5999 handle_signal ("SIGCONT", SIGCONT
);
6002 handle_signal ("SIGCHLD", SIGCHLD
);
6005 handle_signal ("SIGTTIN", SIGTTIN
);
6008 handle_signal ("SIGTTOU", SIGTTOU
);
6011 handle_signal ("SIGIO", SIGIO
);
6014 handle_signal ("SIGXCPU", SIGXCPU
);
6017 handle_signal ("SIGXFSZ", SIGXFSZ
);
6020 handle_signal ("SIGVTALRM", SIGVTALRM
);
6023 handle_signal ("SIGPROF", SIGPROF
);
6026 handle_signal ("SIGWINCH", SIGWINCH
);
6029 handle_signal ("SIGINFO", SIGINFO
);
6032 handle_signal ("SIGUSR1", SIGUSR1
);
6035 handle_signal ("SIGUSR2", SIGUSR2
);
6038 error ("Undefined signal name %s", name
);
6041 #undef handle_signal
6043 return make_number (kill (XINT (pid
), XINT (sigcode
)));
6046 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6047 doc
: /* Make PROCESS see end-of-file in its input.
6048 EOF comes after any text already sent to it.
6049 PROCESS may be a process, a buffer, the name of a process or buffer, or
6050 nil, indicating the current buffer's process.
6051 If PROCESS is a network connection, or is a process communicating
6052 through a pipe (as opposed to a pty), then you cannot send any more
6053 text to PROCESS after you call this function. */)
6055 Lisp_Object process
;
6058 struct coding_system
*coding
;
6060 if (DATAGRAM_CONN_P (process
))
6063 proc
= get_process (process
);
6064 coding
= proc_encode_coding_system
[XINT (XPROCESS (proc
)->outfd
)];
6066 /* Make sure the process is really alive. */
6067 if (! NILP (XPROCESS (proc
)->raw_status_low
))
6068 update_status (XPROCESS (proc
));
6069 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6070 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6072 if (CODING_REQUIRE_FLUSHING (coding
))
6074 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6075 send_process (proc
, "", 0, Qnil
);
6079 send_process (proc
, "\032", 1, Qnil
); /* ^z */
6081 if (!NILP (XPROCESS (proc
)->pty_flag
))
6082 send_process (proc
, "\004", 1, Qnil
);
6085 int old_outfd
, new_outfd
;
6087 #ifdef HAVE_SHUTDOWN
6088 /* If this is a network connection, or socketpair is used
6089 for communication with the subprocess, call shutdown to cause EOF.
6090 (In some old system, shutdown to socketpair doesn't work.
6091 Then we just can't win.) */
6092 if (NILP (XPROCESS (proc
)->pid
)
6093 || XINT (XPROCESS (proc
)->outfd
) == XINT (XPROCESS (proc
)->infd
))
6094 shutdown (XINT (XPROCESS (proc
)->outfd
), 1);
6095 /* In case of socketpair, outfd == infd, so don't close it. */
6096 if (XINT (XPROCESS (proc
)->outfd
) != XINT (XPROCESS (proc
)->infd
))
6097 emacs_close (XINT (XPROCESS (proc
)->outfd
));
6098 #else /* not HAVE_SHUTDOWN */
6099 emacs_close (XINT (XPROCESS (proc
)->outfd
));
6100 #endif /* not HAVE_SHUTDOWN */
6101 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6102 old_outfd
= XINT (XPROCESS (proc
)->outfd
);
6104 if (!proc_encode_coding_system
[new_outfd
])
6105 proc_encode_coding_system
[new_outfd
]
6106 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6107 bcopy (proc_encode_coding_system
[old_outfd
],
6108 proc_encode_coding_system
[new_outfd
],
6109 sizeof (struct coding_system
));
6110 bzero (proc_encode_coding_system
[old_outfd
],
6111 sizeof (struct coding_system
));
6113 XSETINT (XPROCESS (proc
)->outfd
, new_outfd
);
6119 /* Kill all processes associated with `buffer'.
6120 If `buffer' is nil, kill all processes */
6123 kill_buffer_processes (buffer
)
6126 Lisp_Object tail
, proc
;
6128 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
6130 proc
= XCDR (XCAR (tail
));
6131 if (GC_PROCESSP (proc
)
6132 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6134 if (NETCONN_P (proc
))
6135 Fdelete_process (proc
);
6136 else if (XINT (XPROCESS (proc
)->infd
) >= 0)
6137 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6142 /* On receipt of a signal that a child status has changed, loop asking
6143 about children with changed statuses until the system says there
6146 All we do is change the status; we do not run sentinels or print
6147 notifications. That is saved for the next time keyboard input is
6148 done, in order to avoid timing errors.
6150 ** WARNING: this can be called during garbage collection.
6151 Therefore, it must not be fooled by the presence of mark bits in
6154 ** USG WARNING: Although it is not obvious from the documentation
6155 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6156 signal() before executing at least one wait(), otherwise the
6157 handler will be called again, resulting in an infinite loop. The
6158 relevant portion of the documentation reads "SIGCLD signals will be
6159 queued and the signal-catching function will be continually
6160 reentered until the queue is empty". Invoking signal() causes the
6161 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6164 ** Malloc WARNING: This should never call malloc either directly or
6165 indirectly; if it does, that is a bug */
6168 sigchld_handler (signo
)
6171 int old_errno
= errno
;
6173 register struct Lisp_Process
*p
;
6174 extern EMACS_TIME
*input_available_clear_time
;
6176 SIGNAL_THREAD_CHECK (signo
);
6180 sigheld
|= sigbit (SIGCHLD
);
6192 #endif /* no WUNTRACED */
6193 /* Keep trying to get a status until we get a definitive result. */
6197 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6199 while (pid
< 0 && errno
== EINTR
);
6203 /* PID == 0 means no processes found, PID == -1 means a real
6204 failure. We have done all our job, so return. */
6206 /* USG systems forget handlers when they are used;
6207 must reestablish each time */
6208 #if defined (USG) && !defined (POSIX_SIGNALS)
6209 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6212 sigheld
&= ~sigbit (SIGCHLD
);
6220 #endif /* no WNOHANG */
6222 /* Find the process that signaled us, and record its status. */
6225 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
6227 proc
= XCDR (XCAR (tail
));
6228 p
= XPROCESS (proc
);
6229 if (GC_EQ (p
->childp
, Qt
) && XINT (p
->pid
) == pid
)
6234 /* Look for an asynchronous process whose pid hasn't been filled
6237 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
6239 proc
= XCDR (XCAR (tail
));
6240 p
= XPROCESS (proc
);
6241 if (GC_INTEGERP (p
->pid
) && XINT (p
->pid
) == -1)
6246 /* Change the status of the process that was found. */
6249 union { int i
; WAITTYPE wt
; } u
;
6250 int clear_desc_flag
= 0;
6252 XSETINT (p
->tick
, ++process_tick
);
6254 XSETINT (p
->raw_status_low
, u
.i
& 0xffff);
6255 XSETINT (p
->raw_status_high
, u
.i
>> 16);
6257 /* If process has terminated, stop waiting for its output. */
6258 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6259 && XINT (p
->infd
) >= 0)
6260 clear_desc_flag
= 1;
6262 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6263 if (clear_desc_flag
)
6265 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
6266 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
6269 /* Tell wait_reading_process_output that it needs to wake up and
6271 if (input_available_clear_time
)
6272 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6275 /* There was no asynchronous process found for that id. Check
6276 if we have a synchronous process. */
6279 synch_process_alive
= 0;
6281 /* Report the status of the synchronous process. */
6283 synch_process_retcode
= WRETCODE (w
);
6284 else if (WIFSIGNALED (w
))
6285 synch_process_termsig
= WTERMSIG (w
);
6287 /* Tell wait_reading_process_output that it needs to wake up and
6289 if (input_available_clear_time
)
6290 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6293 /* On some systems, we must return right away.
6294 If any more processes want to signal us, we will
6296 Otherwise (on systems that have WNOHANG), loop around
6297 to use up all the processes that have something to tell us. */
6298 #if (defined WINDOWSNT \
6299 || (defined USG && !defined GNU_LINUX \
6300 && !(defined HPUX && defined WNOHANG)))
6301 #if defined (USG) && ! defined (POSIX_SIGNALS)
6302 signal (signo
, sigchld_handler
);
6306 #endif /* USG, but not HPUX with WNOHANG */
6312 exec_sentinel_unwind (data
)
6315 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6320 exec_sentinel_error_handler (error
)
6323 cmd_error_internal (error
, "error in process sentinel: ");
6325 update_echo_area ();
6326 Fsleep_for (make_number (2), Qnil
);
6331 exec_sentinel (proc
, reason
)
6332 Lisp_Object proc
, reason
;
6334 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6335 register struct Lisp_Process
*p
= XPROCESS (proc
);
6336 int count
= SPECPDL_INDEX ();
6337 int outer_running_asynch_code
= running_asynch_code
;
6338 int waiting
= waiting_for_user_input_p
;
6340 /* No need to gcpro these, because all we do with them later
6341 is test them for EQness, and none of them should be a string. */
6342 odeactivate
= Vdeactivate_mark
;
6343 XSETBUFFER (obuffer
, current_buffer
);
6344 okeymap
= current_buffer
->keymap
;
6346 sentinel
= p
->sentinel
;
6347 if (NILP (sentinel
))
6350 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6351 assure that it gets restored no matter how the sentinel exits. */
6353 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6354 /* Inhibit quit so that random quits don't screw up a running filter. */
6355 specbind (Qinhibit_quit
, Qt
);
6356 specbind (Qlast_nonmenu_event
, Qt
);
6358 /* In case we get recursively called,
6359 and we already saved the match data nonrecursively,
6360 save the same match data in safely recursive fashion. */
6361 if (outer_running_asynch_code
)
6364 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6365 restore_search_regs ();
6366 record_unwind_save_match_data ();
6367 Fset_match_data (tem
, Qt
);
6370 /* For speed, if a search happens within this code,
6371 save the match data in a special nonrecursive fashion. */
6372 running_asynch_code
= 1;
6374 internal_condition_case_1 (read_process_output_call
,
6376 Fcons (proc
, Fcons (reason
, Qnil
))),
6377 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6378 exec_sentinel_error_handler
);
6380 /* If we saved the match data nonrecursively, restore it now. */
6381 restore_search_regs ();
6382 running_asynch_code
= outer_running_asynch_code
;
6384 Vdeactivate_mark
= odeactivate
;
6386 /* Restore waiting_for_user_input_p as it was
6387 when we were called, in case the filter clobbered it. */
6388 waiting_for_user_input_p
= waiting
;
6391 if (! EQ (Fcurrent_buffer (), obuffer
)
6392 || ! EQ (current_buffer
->keymap
, okeymap
))
6394 /* But do it only if the caller is actually going to read events.
6395 Otherwise there's no need to make him wake up, and it could
6396 cause trouble (for example it would make Fsit_for return). */
6397 if (waiting_for_user_input_p
== -1)
6398 record_asynch_buffer_change ();
6400 unbind_to (count
, Qnil
);
6403 /* Report all recent events of a change in process status
6404 (either run the sentinel or output a message).
6405 This is usually done while Emacs is waiting for keyboard input
6406 but can be done at other times. */
6409 status_notify (deleting_process
)
6410 struct Lisp_Process
*deleting_process
;
6412 register Lisp_Object proc
, buffer
;
6413 Lisp_Object tail
, msg
;
6414 struct gcpro gcpro1
, gcpro2
;
6418 /* We need to gcpro tail; if read_process_output calls a filter
6419 which deletes a process and removes the cons to which tail points
6420 from Vprocess_alist, and then causes a GC, tail is an unprotected
6424 /* Set this now, so that if new processes are created by sentinels
6425 that we run, we get called again to handle their status changes. */
6426 update_tick
= process_tick
;
6428 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
6431 register struct Lisp_Process
*p
;
6433 proc
= Fcdr (Fcar (tail
));
6434 p
= XPROCESS (proc
);
6436 if (XINT (p
->tick
) != XINT (p
->update_tick
))
6438 XSETINT (p
->update_tick
, XINT (p
->tick
));
6440 /* If process is still active, read any output that remains. */
6441 while (! EQ (p
->filter
, Qt
)
6442 && ! EQ (p
->status
, Qconnect
)
6443 && ! EQ (p
->status
, Qlisten
)
6444 && ! EQ (p
->command
, Qt
) /* Network process not stopped. */
6445 && XINT (p
->infd
) >= 0
6446 && p
!= deleting_process
6447 && read_process_output (proc
, XINT (p
->infd
)) > 0);
6451 /* Get the text to use for the message. */
6452 if (!NILP (p
->raw_status_low
))
6454 msg
= status_message (p
);
6456 /* If process is terminated, deactivate it or delete it. */
6458 if (CONSP (p
->status
))
6459 symbol
= XCAR (p
->status
);
6461 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6462 || EQ (symbol
, Qclosed
))
6464 if (delete_exited_processes
)
6465 remove_process (proc
);
6467 deactivate_process (proc
);
6470 /* The actions above may have further incremented p->tick.
6471 So set p->update_tick again
6472 so that an error in the sentinel will not cause
6473 this code to be run again. */
6474 XSETINT (p
->update_tick
, XINT (p
->tick
));
6475 /* Now output the message suitably. */
6476 if (!NILP (p
->sentinel
))
6477 exec_sentinel (proc
, msg
);
6478 /* Don't bother with a message in the buffer
6479 when a process becomes runnable. */
6480 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6482 Lisp_Object ro
, tem
;
6483 struct buffer
*old
= current_buffer
;
6484 int opoint
, opoint_byte
;
6485 int before
, before_byte
;
6487 ro
= XBUFFER (buffer
)->read_only
;
6489 /* Avoid error if buffer is deleted
6490 (probably that's why the process is dead, too) */
6491 if (NILP (XBUFFER (buffer
)->name
))
6493 Fset_buffer (buffer
);
6496 opoint_byte
= PT_BYTE
;
6497 /* Insert new output into buffer
6498 at the current end-of-output marker,
6499 thus preserving logical ordering of input and output. */
6500 if (XMARKER (p
->mark
)->buffer
)
6501 Fgoto_char (p
->mark
);
6503 SET_PT_BOTH (ZV
, ZV_BYTE
);
6506 before_byte
= PT_BYTE
;
6508 tem
= current_buffer
->read_only
;
6509 current_buffer
->read_only
= Qnil
;
6510 insert_string ("\nProcess ");
6511 Finsert (1, &p
->name
);
6512 insert_string (" ");
6514 current_buffer
->read_only
= tem
;
6515 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6517 if (opoint
>= before
)
6518 SET_PT_BOTH (opoint
+ (PT
- before
),
6519 opoint_byte
+ (PT_BYTE
- before_byte
));
6521 SET_PT_BOTH (opoint
, opoint_byte
);
6523 set_buffer_internal (old
);
6528 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6529 redisplay_preserve_echo_area (13);
6535 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6536 Sset_process_coding_system
, 1, 3, 0,
6537 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6538 DECODING will be used to decode subprocess output and ENCODING to
6539 encode subprocess input. */)
6540 (process
, decoding
, encoding
)
6541 register Lisp_Object process
, decoding
, encoding
;
6543 register struct Lisp_Process
*p
;
6545 CHECK_PROCESS (process
);
6546 p
= XPROCESS (process
);
6547 if (XINT (p
->infd
) < 0)
6548 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6549 if (XINT (p
->outfd
) < 0)
6550 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6551 Fcheck_coding_system (decoding
);
6552 Fcheck_coding_system (encoding
);
6554 p
->decode_coding_system
= decoding
;
6555 p
->encode_coding_system
= encoding
;
6556 setup_process_coding_systems (process
);
6561 DEFUN ("process-coding-system",
6562 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6563 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6565 register Lisp_Object process
;
6567 CHECK_PROCESS (process
);
6568 return Fcons (XPROCESS (process
)->decode_coding_system
,
6569 XPROCESS (process
)->encode_coding_system
);
6572 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6573 Sset_process_filter_multibyte
, 2, 2, 0,
6574 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6575 If FLAG is non-nil, the filter is given multibyte strings.
6576 If FLAG is nil, the filter is given unibyte strings. In this case,
6577 all character code conversion except for end-of-line conversion is
6580 Lisp_Object process
, flag
;
6582 register struct Lisp_Process
*p
;
6584 CHECK_PROCESS (process
);
6585 p
= XPROCESS (process
);
6586 p
->filter_multibyte
= flag
;
6587 setup_process_coding_systems (process
);
6592 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6593 Sprocess_filter_multibyte_p
, 1, 1, 0,
6594 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6596 Lisp_Object process
;
6598 register struct Lisp_Process
*p
;
6600 CHECK_PROCESS (process
);
6601 p
= XPROCESS (process
);
6603 return (NILP (p
->filter_multibyte
) ? Qnil
: Qt
);
6608 /* The first time this is called, assume keyboard input comes from DESC
6609 instead of from where we used to expect it.
6610 Subsequent calls mean assume input keyboard can come from DESC
6611 in addition to other places. */
6613 static int add_keyboard_wait_descriptor_called_flag
;
6616 add_keyboard_wait_descriptor (desc
)
6619 if (! add_keyboard_wait_descriptor_called_flag
)
6620 FD_CLR (0, &input_wait_mask
);
6621 add_keyboard_wait_descriptor_called_flag
= 1;
6622 FD_SET (desc
, &input_wait_mask
);
6623 FD_SET (desc
, &non_process_wait_mask
);
6624 if (desc
> max_keyboard_desc
)
6625 max_keyboard_desc
= desc
;
6628 /* From now on, do not expect DESC to give keyboard input. */
6631 delete_keyboard_wait_descriptor (desc
)
6635 int lim
= max_keyboard_desc
;
6637 FD_CLR (desc
, &input_wait_mask
);
6638 FD_CLR (desc
, &non_process_wait_mask
);
6640 if (desc
== max_keyboard_desc
)
6641 for (fd
= 0; fd
< lim
; fd
++)
6642 if (FD_ISSET (fd
, &input_wait_mask
)
6643 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6644 max_keyboard_desc
= fd
;
6647 /* Return nonzero if *MASK has a bit set
6648 that corresponds to one of the keyboard input descriptors. */
6651 keyboard_bit_set (mask
)
6656 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
6657 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6658 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6671 if (! noninteractive
|| initialized
)
6673 signal (SIGCHLD
, sigchld_handler
);
6676 FD_ZERO (&input_wait_mask
);
6677 FD_ZERO (&non_keyboard_wait_mask
);
6678 FD_ZERO (&non_process_wait_mask
);
6679 max_process_desc
= 0;
6681 #ifdef NON_BLOCKING_CONNECT
6682 FD_ZERO (&connect_wait_mask
);
6683 num_pending_connects
= 0;
6686 #ifdef ADAPTIVE_READ_BUFFERING
6687 process_output_delay_count
= 0;
6688 process_output_skip
= 0;
6691 FD_SET (0, &input_wait_mask
);
6693 Vprocess_alist
= Qnil
;
6694 for (i
= 0; i
< MAXDESC
; i
++)
6696 chan_process
[i
] = Qnil
;
6697 proc_buffered_char
[i
] = -1;
6699 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
6700 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
6701 #ifdef DATAGRAM_SOCKETS
6702 bzero (datagram_address
, sizeof datagram_address
);
6707 Lisp_Object subfeatures
= Qnil
;
6708 struct socket_options
*sopt
;
6710 #define ADD_SUBFEATURE(key, val) \
6711 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
6713 #ifdef NON_BLOCKING_CONNECT
6714 ADD_SUBFEATURE (QCnowait
, Qt
);
6716 #ifdef DATAGRAM_SOCKETS
6717 ADD_SUBFEATURE (QCtype
, Qdatagram
);
6719 #ifdef HAVE_LOCAL_SOCKETS
6720 ADD_SUBFEATURE (QCfamily
, Qlocal
);
6722 #ifdef HAVE_GETSOCKNAME
6723 ADD_SUBFEATURE (QCservice
, Qt
);
6725 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY))
6726 ADD_SUBFEATURE (QCserver
, Qt
);
6729 for (sopt
= socket_options
; sopt
->name
; sopt
++)
6730 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
6732 Fprovide (intern ("make-network-process"), subfeatures
);
6734 #endif /* HAVE_SOCKETS */
6736 #if defined (DARWIN) || defined (MAC_OSX)
6737 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
6738 processes. As such, we only change the default value. */
6741 char *release
= get_operating_system_release();
6742 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
6743 && release
[1] == '.')) {
6744 Vprocess_connection_type
= Qnil
;
6753 Qprocessp
= intern ("processp");
6754 staticpro (&Qprocessp
);
6755 Qrun
= intern ("run");
6757 Qstop
= intern ("stop");
6759 Qsignal
= intern ("signal");
6760 staticpro (&Qsignal
);
6762 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
6765 Qexit = intern ("exit");
6766 staticpro (&Qexit); */
6768 Qopen
= intern ("open");
6770 Qclosed
= intern ("closed");
6771 staticpro (&Qclosed
);
6772 Qconnect
= intern ("connect");
6773 staticpro (&Qconnect
);
6774 Qfailed
= intern ("failed");
6775 staticpro (&Qfailed
);
6776 Qlisten
= intern ("listen");
6777 staticpro (&Qlisten
);
6778 Qlocal
= intern ("local");
6779 staticpro (&Qlocal
);
6780 Qdatagram
= intern ("datagram");
6781 staticpro (&Qdatagram
);
6783 QCname
= intern (":name");
6784 staticpro (&QCname
);
6785 QCbuffer
= intern (":buffer");
6786 staticpro (&QCbuffer
);
6787 QChost
= intern (":host");
6788 staticpro (&QChost
);
6789 QCservice
= intern (":service");
6790 staticpro (&QCservice
);
6791 QCtype
= intern (":type");
6792 staticpro (&QCtype
);
6793 QClocal
= intern (":local");
6794 staticpro (&QClocal
);
6795 QCremote
= intern (":remote");
6796 staticpro (&QCremote
);
6797 QCcoding
= intern (":coding");
6798 staticpro (&QCcoding
);
6799 QCserver
= intern (":server");
6800 staticpro (&QCserver
);
6801 QCnowait
= intern (":nowait");
6802 staticpro (&QCnowait
);
6803 QCsentinel
= intern (":sentinel");
6804 staticpro (&QCsentinel
);
6805 QClog
= intern (":log");
6807 QCnoquery
= intern (":noquery");
6808 staticpro (&QCnoquery
);
6809 QCstop
= intern (":stop");
6810 staticpro (&QCstop
);
6811 QCoptions
= intern (":options");
6812 staticpro (&QCoptions
);
6813 QCplist
= intern (":plist");
6814 staticpro (&QCplist
);
6815 QCfilter_multibyte
= intern (":filter-multibyte");
6816 staticpro (&QCfilter_multibyte
);
6818 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
6819 staticpro (&Qlast_nonmenu_event
);
6821 staticpro (&Vprocess_alist
);
6823 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
6824 doc
: /* *Non-nil means delete processes immediately when they exit.
6825 nil means don't delete them until `list-processes' is run. */);
6827 delete_exited_processes
= 1;
6829 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
6830 doc
: /* Control type of device used to communicate with subprocesses.
6831 Values are nil to use a pipe, or t or `pty' to use a pty.
6832 The value has no effect if the system has no ptys or if all ptys are busy:
6833 then a pipe is used in any case.
6834 The value takes effect when `start-process' is called. */);
6835 Vprocess_connection_type
= Qt
;
6837 #ifdef ADAPTIVE_READ_BUFFERING
6838 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
6839 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
6840 On some systems, when Emacs reads the output from a subprocess, the output data
6841 is read in very small blocks, potentially resulting in very poor performance.
6842 This behavior can be remedied to some extent by setting this variable to a
6843 non-nil value, as it will automatically delay reading from such processes, to
6844 allowing them to produce more output before Emacs tries to read it.
6845 If the value is t, the delay is reset after each write to the process; any other
6846 non-nil value means that the delay is not reset on write.
6847 The variable takes effect when `start-process' is called. */);
6848 Vprocess_adaptive_read_buffering
= Qt
;
6851 defsubr (&Sprocessp
);
6852 defsubr (&Sget_process
);
6853 defsubr (&Sget_buffer_process
);
6854 defsubr (&Sdelete_process
);
6855 defsubr (&Sprocess_status
);
6856 defsubr (&Sprocess_exit_status
);
6857 defsubr (&Sprocess_id
);
6858 defsubr (&Sprocess_name
);
6859 defsubr (&Sprocess_tty_name
);
6860 defsubr (&Sprocess_command
);
6861 defsubr (&Sset_process_buffer
);
6862 defsubr (&Sprocess_buffer
);
6863 defsubr (&Sprocess_mark
);
6864 defsubr (&Sset_process_filter
);
6865 defsubr (&Sprocess_filter
);
6866 defsubr (&Sset_process_sentinel
);
6867 defsubr (&Sprocess_sentinel
);
6868 defsubr (&Sset_process_window_size
);
6869 defsubr (&Sset_process_inherit_coding_system_flag
);
6870 defsubr (&Sprocess_inherit_coding_system_flag
);
6871 defsubr (&Sset_process_query_on_exit_flag
);
6872 defsubr (&Sprocess_query_on_exit_flag
);
6873 defsubr (&Sprocess_contact
);
6874 defsubr (&Sprocess_plist
);
6875 defsubr (&Sset_process_plist
);
6876 defsubr (&Slist_processes
);
6877 defsubr (&Sprocess_list
);
6878 defsubr (&Sstart_process
);
6880 defsubr (&Sset_network_process_option
);
6881 defsubr (&Smake_network_process
);
6882 defsubr (&Sformat_network_address
);
6883 #endif /* HAVE_SOCKETS */
6884 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
6886 defsubr (&Snetwork_interface_list
);
6888 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
6889 defsubr (&Snetwork_interface_info
);
6891 #endif /* HAVE_SOCKETS ... */
6892 #ifdef DATAGRAM_SOCKETS
6893 defsubr (&Sprocess_datagram_address
);
6894 defsubr (&Sset_process_datagram_address
);
6896 defsubr (&Saccept_process_output
);
6897 defsubr (&Sprocess_send_region
);
6898 defsubr (&Sprocess_send_string
);
6899 defsubr (&Sinterrupt_process
);
6900 defsubr (&Skill_process
);
6901 defsubr (&Squit_process
);
6902 defsubr (&Sstop_process
);
6903 defsubr (&Scontinue_process
);
6904 defsubr (&Sprocess_running_child_p
);
6905 defsubr (&Sprocess_send_eof
);
6906 defsubr (&Ssignal_process
);
6907 defsubr (&Swaiting_for_user_input_p
);
6908 /* defsubr (&Sprocess_connection); */
6909 defsubr (&Sset_process_coding_system
);
6910 defsubr (&Sprocess_coding_system
);
6911 defsubr (&Sset_process_filter_multibyte
);
6912 defsubr (&Sprocess_filter_multibyte_p
);
6916 #else /* not subprocesses */
6918 #include <sys/types.h>
6922 #include "systime.h"
6923 #include "character.h"
6925 #include "termopts.h"
6926 #include "sysselect.h"
6928 extern int frame_garbaged
;
6930 extern EMACS_TIME
timer_check ();
6931 extern int timers_run
;
6935 /* As described above, except assuming that there are no subprocesses:
6937 Wait for timeout to elapse and/or keyboard input to be available.
6940 timeout in seconds, or
6941 zero for no limit, or
6942 -1 means gobble data immediately available but don't wait for any.
6944 read_kbd is a Lisp_Object:
6945 0 to ignore keyboard input, or
6946 1 to return when input is available, or
6947 -1 means caller will actually read the input, so don't throw to
6950 see full version for other parameters. We know that wait_proc will
6951 always be NULL, since `subprocesses' isn't defined.
6953 do_display != 0 means redisplay should be done to show subprocess
6954 output that arrives.
6956 Return true iff we received input from any process. */
6959 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
6960 wait_for_cell
, wait_proc
, just_wait_proc
)
6961 int time_limit
, microsecs
, read_kbd
, do_display
;
6962 Lisp_Object wait_for_cell
;
6963 struct Lisp_Process
*wait_proc
;
6967 EMACS_TIME end_time
, timeout
;
6968 SELECT_TYPE waitchannels
;
6971 /* What does time_limit really mean? */
6972 if (time_limit
|| microsecs
)
6974 EMACS_GET_TIME (end_time
);
6975 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
6976 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
6979 /* Turn off periodic alarms (in case they are in use)
6980 and then turn off any other atimers,
6981 because the select emulator uses alarms. */
6983 turn_on_atimers (0);
6987 int timeout_reduced_for_timers
= 0;
6989 /* If calling from keyboard input, do not quit
6990 since we want to return C-g as an input character.
6991 Otherwise, do pending quit if requested. */
6995 /* Exit now if the cell we're waiting for became non-nil. */
6996 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6999 /* Compute time from now till when time limit is up */
7000 /* Exit if already run out */
7001 if (time_limit
== -1)
7003 /* -1 specified for timeout means
7004 gobble output available now
7005 but don't wait at all. */
7007 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7009 else if (time_limit
|| microsecs
)
7011 EMACS_GET_TIME (timeout
);
7012 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7013 if (EMACS_TIME_NEG_P (timeout
))
7018 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7021 /* If our caller will not immediately handle keyboard events,
7022 run timer events directly.
7023 (Callers that will immediately read keyboard events
7024 call timer_delay on their own.) */
7025 if (NILP (wait_for_cell
))
7027 EMACS_TIME timer_delay
;
7031 int old_timers_run
= timers_run
;
7032 timer_delay
= timer_check (1);
7033 if (timers_run
!= old_timers_run
&& do_display
)
7034 /* We must retry, since a timer may have requeued itself
7035 and that could alter the time delay. */
7036 redisplay_preserve_echo_area (14);
7040 while (!detect_input_pending ());
7042 /* If there is unread keyboard input, also return. */
7044 && requeued_events_pending_p ())
7047 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7049 EMACS_TIME difference
;
7050 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7051 if (EMACS_TIME_NEG_P (difference
))
7053 timeout
= timer_delay
;
7054 timeout_reduced_for_timers
= 1;
7059 /* Cause C-g and alarm signals to take immediate action,
7060 and cause input available signals to zero out timeout. */
7062 set_waiting_for_input (&timeout
);
7064 /* Wait till there is something to do. */
7066 if (! read_kbd
&& NILP (wait_for_cell
))
7067 FD_ZERO (&waitchannels
);
7069 FD_SET (0, &waitchannels
);
7071 /* If a frame has been newly mapped and needs updating,
7072 reprocess its display stuff. */
7073 if (frame_garbaged
&& do_display
)
7075 clear_waiting_for_input ();
7076 redisplay_preserve_echo_area (15);
7078 set_waiting_for_input (&timeout
);
7081 if (read_kbd
&& detect_input_pending ())
7084 FD_ZERO (&waitchannels
);
7087 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7092 /* Make C-g and alarm signals set flags again */
7093 clear_waiting_for_input ();
7095 /* If we woke up due to SIGWINCH, actually change size now. */
7096 do_pending_window_change (0);
7098 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7099 /* We waited the full specified time, so return now. */
7104 /* If the system call was interrupted, then go around the
7106 if (xerrno
== EINTR
)
7107 FD_ZERO (&waitchannels
);
7109 error ("select error: %s", emacs_strerror (xerrno
));
7112 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7113 /* System sometimes fails to deliver SIGIO. */
7114 kill (getpid (), SIGIO
);
7117 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7118 kill (getpid (), SIGIO
);
7121 /* Check for keyboard input */
7124 && detect_input_pending_run_timers (do_display
))
7126 swallow_events (do_display
);
7127 if (detect_input_pending_run_timers (do_display
))
7131 /* If there is unread keyboard input, also return. */
7133 && requeued_events_pending_p ())
7136 /* If wait_for_cell. check for keyboard input
7137 but don't run any timers.
7138 ??? (It seems wrong to me to check for keyboard
7139 input at all when wait_for_cell, but the code
7140 has been this way since July 1994.
7141 Try changing this after version 19.31.) */
7142 if (! NILP (wait_for_cell
)
7143 && detect_input_pending ())
7145 swallow_events (do_display
);
7146 if (detect_input_pending ())
7150 /* Exit now if the cell we're waiting for became non-nil. */
7151 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7161 /* Don't confuse make-docfile by having two doc strings for this function.
7162 make-docfile does not pay attention to #if, for good reason! */
7163 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7166 register Lisp_Object name
;
7171 /* Don't confuse make-docfile by having two doc strings for this function.
7172 make-docfile does not pay attention to #if, for good reason! */
7173 DEFUN ("process-inherit-coding-system-flag",
7174 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7178 register Lisp_Object process
;
7180 /* Ignore the argument and return the value of
7181 inherit-process-coding-system. */
7182 return inherit_process_coding_system
? Qt
: Qnil
;
7185 /* Kill all processes associated with `buffer'.
7186 If `buffer' is nil, kill all processes.
7187 Since we have no subprocesses, this does nothing. */
7190 kill_buffer_processes (buffer
)
7203 QCtype
= intern (":type");
7204 staticpro (&QCtype
);
7206 defsubr (&Sget_buffer_process
);
7207 defsubr (&Sprocess_inherit_coding_system_flag
);
7211 #endif /* not subprocesses */
7213 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7214 (do not change this comment) */