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, 2006, 2007, 2008 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 3, 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 */
43 #ifdef HAVE_INTTYPES_H
50 #if defined(WINDOWSNT) || defined(UNIX98_PTYS)
53 #endif /* not WINDOWSNT */
55 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
56 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
61 /* Are local (unix) sockets supported? */
62 #if defined (HAVE_SYS_UN_H)
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 #if defined(BSD_SYSTEM)
79 #include <sys/ioctl.h>
80 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
82 #endif /* HAVE_PTYS and no O_NDELAY */
83 #endif /* BSD_SYSTEM */
85 #ifdef BROKEN_O_NONBLOCK
87 #endif /* BROKEN_O_NONBLOCK */
93 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
95 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
96 /* sys/ioctl.h may have been included already */
98 #include <sys/ioctl.h>
105 #include <sys/sysmacros.h> /* for "minor" */
106 #endif /* not IRIS */
109 #include <sys/wait.h>
113 #include <netinet/in.h>
114 #include <arpa/nameser.h>
124 #include "character.h"
128 #include "termhooks.h"
129 #include "termopts.h"
130 #include "commands.h"
131 #include "keyboard.h"
132 #include "blockinput.h"
133 #include "dispextern.h"
134 #include "composite.h"
137 Lisp_Object Qprocessp
;
138 Lisp_Object Qrun
, Qstop
, Qsignal
;
139 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
140 Lisp_Object Qlocal
, Qipv4
, 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) (CONSP (XPROCESS (p)->childp))
166 #define NETCONN1_P(p) (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
;
207 /* These next two vars are non-static since sysdep.c uses them in the
208 emulation of `select'. */
209 /* Number of events of change of status of a process. */
211 /* Number of events for which the user or sentinel has been notified. */
214 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
216 #ifdef BROKEN_NON_BLOCKING_CONNECT
217 #undef NON_BLOCKING_CONNECT
219 #ifndef NON_BLOCKING_CONNECT
222 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
223 #if defined (O_NONBLOCK) || defined (O_NDELAY)
224 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
225 #define NON_BLOCKING_CONNECT
226 #endif /* EWOULDBLOCK || EINPROGRESS */
227 #endif /* O_NONBLOCK || O_NDELAY */
228 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
229 #endif /* HAVE_SELECT */
230 #endif /* HAVE_SOCKETS */
231 #endif /* NON_BLOCKING_CONNECT */
232 #endif /* BROKEN_NON_BLOCKING_CONNECT */
234 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
235 this system. We need to read full packets, so we need a
236 "non-destructive" select. So we require either native select,
237 or emulation of select using FIONREAD. */
239 #ifdef BROKEN_DATAGRAM_SOCKETS
240 #undef DATAGRAM_SOCKETS
242 #ifndef DATAGRAM_SOCKETS
244 #if defined (HAVE_SELECT) || defined (FIONREAD)
245 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
246 #define DATAGRAM_SOCKETS
247 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
248 #endif /* HAVE_SELECT || FIONREAD */
249 #endif /* HAVE_SOCKETS */
250 #endif /* DATAGRAM_SOCKETS */
251 #endif /* BROKEN_DATAGRAM_SOCKETS */
254 #undef NON_BLOCKING_CONNECT
255 #undef DATAGRAM_SOCKETS
258 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
259 #ifdef EMACS_HAS_USECS
260 #define ADAPTIVE_READ_BUFFERING
264 #ifdef ADAPTIVE_READ_BUFFERING
265 #define READ_OUTPUT_DELAY_INCREMENT 10000
266 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
267 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
269 /* Number of processes which have a non-zero read_output_delay,
270 and therefore might be delayed for adaptive read buffering. */
272 static int process_output_delay_count
;
274 /* Non-zero if any process has non-nil read_output_skip. */
276 static int process_output_skip
;
278 /* Non-nil means to delay reading process output to improve buffering.
279 A value of t means that delay is reset after each send, any other
280 non-nil value does not reset the delay. A value of nil disables
281 adaptive read buffering completely. */
282 static Lisp_Object Vprocess_adaptive_read_buffering
;
284 #define process_output_delay_count 0
288 #include "sysselect.h"
290 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
291 static void deactivate_process
P_ ((Lisp_Object
));
292 static void status_notify
P_ ((struct Lisp_Process
*));
293 static int read_process_output
P_ ((Lisp_Object
, int));
295 /* If we support a window system, turn on the code to poll periodically
296 to detect C-g. It isn't actually used when doing interrupt input. */
297 #ifdef HAVE_WINDOW_SYSTEM
298 #define POLL_FOR_INPUT
301 static Lisp_Object
get_process ();
302 static void exec_sentinel ();
304 extern EMACS_TIME
timer_check ();
305 extern int timers_run
;
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 /* Mask for the gpm mouse input descriptor. */
321 static SELECT_TYPE gpm_wait_mask
;
323 #ifdef NON_BLOCKING_CONNECT
324 /* Mask of bits indicating the descriptors that we wait for connect to
325 complete on. Once they complete, they are removed from this mask
326 and added to the input_wait_mask and non_keyboard_wait_mask. */
328 static SELECT_TYPE connect_wait_mask
;
330 /* Number of bits set in connect_wait_mask. */
331 static int num_pending_connects
;
333 #define IF_NON_BLOCKING_CONNECT(s) s
335 #define IF_NON_BLOCKING_CONNECT(s)
338 /* The largest descriptor currently in use for a process object. */
339 static int max_process_desc
;
341 /* The largest descriptor currently in use for keyboard input. */
342 static int max_keyboard_desc
;
344 /* The largest descriptor currently in use for gpm mouse input. */
345 static int max_gpm_desc
;
347 /* Nonzero means delete a process right away if it exits. */
348 static int delete_exited_processes
;
350 /* Indexed by descriptor, gives the process (if any) for that descriptor */
351 Lisp_Object chan_process
[MAXDESC
];
353 /* Alist of elements (NAME . PROCESS) */
354 Lisp_Object Vprocess_alist
;
356 /* Buffered-ahead input char from process, indexed by channel.
357 -1 means empty (no char is buffered).
358 Used on sys V where the only way to tell if there is any
359 output from the process is to read at least one char.
360 Always -1 on systems that support FIONREAD. */
362 /* Don't make static; need to access externally. */
363 int proc_buffered_char
[MAXDESC
];
365 /* Table of `struct coding-system' for each process. */
366 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
367 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
369 #ifdef DATAGRAM_SOCKETS
370 /* Table of `partner address' for datagram sockets. */
371 struct sockaddr_and_len
{
374 } datagram_address
[MAXDESC
];
375 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
376 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
378 #define DATAGRAM_CHAN_P(chan) (0)
379 #define DATAGRAM_CONN_P(proc) (0)
382 /* Maximum number of bytes to send to a pty without an eof. */
383 static int pty_max_bytes
;
385 /* Nonzero means don't run process sentinels. This is used
387 int inhibit_sentinels
;
393 /* The file name of the pty opened by allocate_pty. */
395 static char pty_name
[24];
398 /* Compute the Lisp form of the process status, p->status, from
399 the numeric status that was returned by `wait'. */
401 static Lisp_Object
status_convert ();
405 struct Lisp_Process
*p
;
407 union { int i
; WAITTYPE wt
; } u
;
408 eassert (p
->raw_status_new
);
410 p
->status
= status_convert (u
.wt
);
411 p
->raw_status_new
= 0;
414 /* Convert a process status word in Unix format to
415 the list that we use internally. */
422 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
423 else if (WIFEXITED (w
))
424 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
425 WCOREDUMP (w
) ? Qt
: Qnil
));
426 else if (WIFSIGNALED (w
))
427 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
428 WCOREDUMP (w
) ? Qt
: Qnil
));
433 /* Given a status-list, extract the three pieces of information
434 and store them individually through the three pointers. */
437 decode_status (l
, symbol
, code
, coredump
)
455 *code
= XFASTINT (XCAR (tem
));
457 *coredump
= !NILP (tem
);
461 /* Return a string describing a process status list. */
465 struct Lisp_Process
*p
;
467 Lisp_Object status
= p
->status
;
470 Lisp_Object string
, string2
;
472 decode_status (status
, &symbol
, &code
, &coredump
);
474 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
477 synchronize_system_messages_locale ();
478 signame
= strsignal (code
);
481 string
= build_string (signame
);
482 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
483 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
484 return concat2 (string
, string2
);
486 else if (EQ (symbol
, Qexit
))
489 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
491 return build_string ("finished\n");
492 string
= Fnumber_to_string (make_number (code
));
493 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
494 return concat3 (build_string ("exited abnormally with code "),
497 else if (EQ (symbol
, Qfailed
))
499 string
= Fnumber_to_string (make_number (code
));
500 string2
= build_string ("\n");
501 return concat3 (build_string ("failed with code "),
505 return Fcopy_sequence (Fsymbol_name (symbol
));
510 /* Open an available pty, returning a file descriptor.
511 Return -1 on failure.
512 The file name of the terminal corresponding to the pty
513 is left in the variable pty_name. */
524 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
525 for (i
= 0; i
< 16; i
++)
528 struct stat stb
; /* Used in some PTY_OPEN. */
529 #ifdef PTY_NAME_SPRINTF
532 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
533 #endif /* no PTY_NAME_SPRINTF */
537 #else /* no PTY_OPEN */
540 /* Unusual IRIS code */
541 *ptyv
= emacs_open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
544 if (fstat (fd
, &stb
) < 0)
546 # else /* not IRIS */
547 { /* Some systems name their pseudoterminals so that there are gaps in
548 the usual sequence - for example, on HP9000/S700 systems, there
549 are no pseudoterminals with names ending in 'f'. So we wait for
550 three failures in a row before deciding that we've reached the
552 int failed_count
= 0;
554 if (stat (pty_name
, &stb
) < 0)
557 if (failed_count
>= 3)
564 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
566 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
568 # endif /* not IRIS */
570 #endif /* no PTY_OPEN */
574 /* check to make certain that both sides are available
575 this avoids a nasty yet stupid bug in rlogins */
576 #ifdef PTY_TTY_NAME_SPRINTF
579 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
580 #endif /* no PTY_TTY_NAME_SPRINTF */
581 if (access (pty_name
, 6) != 0)
584 # if !defined(IRIS) && !defined(__sgi)
596 #endif /* HAVE_PTYS */
602 register Lisp_Object val
, tem
, name1
;
603 register struct Lisp_Process
*p
;
607 p
= allocate_process ();
615 p
->raw_status_new
= 0;
617 p
->mark
= Fmake_marker ();
619 #ifdef ADAPTIVE_READ_BUFFERING
620 p
->adaptive_read_buffering
= 0;
621 p
->read_output_delay
= 0;
622 p
->read_output_skip
= 0;
625 /* If name is already in use, modify it until it is unused. */
630 tem
= Fget_process (name1
);
631 if (NILP (tem
)) break;
632 sprintf (suffix
, "<%d>", i
);
633 name1
= concat2 (name
, build_string (suffix
));
637 XSETPROCESS (val
, p
);
638 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
643 remove_process (proc
)
644 register Lisp_Object proc
;
646 register Lisp_Object pair
;
648 pair
= Frassq (proc
, Vprocess_alist
);
649 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
651 deactivate_process (proc
);
654 /* Setup coding systems of PROCESS. */
657 setup_process_coding_systems (process
)
660 struct Lisp_Process
*p
= XPROCESS (process
);
662 int outch
= p
->outfd
;
663 Lisp_Object coding_system
;
665 if (inch
< 0 || outch
< 0)
668 if (!proc_decode_coding_system
[inch
])
669 proc_decode_coding_system
[inch
]
670 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
671 coding_system
= p
->decode_coding_system
;
672 if (! NILP (p
->filter
))
674 if (!p
->filter_multibyte
)
675 coding_system
= raw_text_coding_system (coding_system
);
677 else if (BUFFERP (p
->buffer
))
679 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
680 coding_system
= raw_text_coding_system (coding_system
);
682 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
684 if (!proc_encode_coding_system
[outch
])
685 proc_encode_coding_system
[outch
]
686 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
687 setup_coding_system (p
->encode_coding_system
,
688 proc_encode_coding_system
[outch
]);
691 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
692 doc
: /* Return t if OBJECT is a process. */)
696 return PROCESSP (object
) ? Qt
: Qnil
;
699 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
700 doc
: /* Return the process named NAME, or nil if there is none. */)
702 register Lisp_Object name
;
707 return Fcdr (Fassoc (name
, Vprocess_alist
));
710 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
711 doc
: /* Return the (or a) process associated with BUFFER.
712 BUFFER may be a buffer or the name of one. */)
714 register Lisp_Object buffer
;
716 register Lisp_Object buf
, tail
, proc
;
718 if (NILP (buffer
)) return Qnil
;
719 buf
= Fget_buffer (buffer
);
720 if (NILP (buf
)) return Qnil
;
722 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
724 proc
= Fcdr (XCAR (tail
));
725 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
731 /* This is how commands for the user decode process arguments. It
732 accepts a process, a process name, a buffer, a buffer name, or nil.
733 Buffers denote the first process in the buffer, and nil denotes the
738 register Lisp_Object name
;
740 register Lisp_Object proc
, obj
;
743 obj
= Fget_process (name
);
745 obj
= Fget_buffer (name
);
747 error ("Process %s does not exist", SDATA (name
));
749 else if (NILP (name
))
750 obj
= Fcurrent_buffer ();
754 /* Now obj should be either a buffer object or a process object.
758 proc
= Fget_buffer_process (obj
);
760 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
772 /* Fdelete_process promises to immediately forget about the process, but in
773 reality, Emacs needs to remember those processes until they have been
774 treated by sigchld_handler; otherwise this handler would consider the
775 process as being synchronous and say that the synchronous process is
777 static Lisp_Object deleted_pid_list
;
780 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
781 doc
: /* Delete PROCESS: kill it and forget about it immediately.
782 PROCESS may be a process, a buffer, the name of a process or buffer, or
783 nil, indicating the current buffer's process. */)
785 register Lisp_Object process
;
787 register struct Lisp_Process
*p
;
789 process
= get_process (process
);
790 p
= XPROCESS (process
);
792 p
->raw_status_new
= 0;
795 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
796 p
->tick
= ++process_tick
;
799 else if (p
->infd
>= 0)
803 /* Assignment to EMACS_INT stops GCC whining about limited range
805 EMACS_INT pid
= p
->pid
;
807 /* No problem storing the pid here, as it is still in Vprocess_alist. */
808 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
809 /* GC treated elements set to nil. */
810 Fdelq (Qnil
, deleted_pid_list
));
811 /* If the process has already signaled, remove it from the list. */
812 if (p
->raw_status_new
)
815 if (CONSP (p
->status
))
816 symbol
= XCAR (p
->status
);
817 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
819 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
823 Fkill_process (process
, Qnil
);
824 /* Do this now, since remove_process will make sigchld_handler do nothing. */
826 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
827 p
->tick
= ++process_tick
;
831 remove_process (process
);
835 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
836 doc
: /* Return the status of PROCESS.
837 The returned value is one of the following symbols:
838 run -- for a process that is running.
839 stop -- for a process stopped but continuable.
840 exit -- for a process that has exited.
841 signal -- for a process that has got a fatal signal.
842 open -- for a network stream connection that is open.
843 listen -- for a network stream server that is listening.
844 closed -- for a network stream connection that is closed.
845 connect -- when waiting for a non-blocking connection to complete.
846 failed -- when a non-blocking connection has failed.
847 nil -- if arg is a process name and no such process exists.
848 PROCESS may be a process, a buffer, the name of a process, or
849 nil, indicating the current buffer's process. */)
851 register Lisp_Object process
;
853 register struct Lisp_Process
*p
;
854 register Lisp_Object status
;
856 if (STRINGP (process
))
857 process
= Fget_process (process
);
859 process
= get_process (process
);
864 p
= XPROCESS (process
);
865 if (p
->raw_status_new
)
869 status
= XCAR (status
);
872 if (EQ (status
, Qexit
))
874 else if (EQ (p
->command
, Qt
))
876 else if (EQ (status
, Qrun
))
882 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
884 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
885 If PROCESS has not yet exited or died, return 0. */)
887 register Lisp_Object process
;
889 CHECK_PROCESS (process
);
890 if (XPROCESS (process
)->raw_status_new
)
891 update_status (XPROCESS (process
));
892 if (CONSP (XPROCESS (process
)->status
))
893 return XCAR (XCDR (XPROCESS (process
)->status
));
894 return make_number (0);
897 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
898 doc
: /* Return the process id of PROCESS.
899 This is the pid of the external process which PROCESS uses or talks to.
900 For a network connection, this value is nil. */)
902 register Lisp_Object process
;
904 /* Assignment to EMACS_INT stops GCC whining about limited range of
908 CHECK_PROCESS (process
);
909 pid
= XPROCESS (process
)->pid
;
910 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
913 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
914 doc
: /* Return the name of PROCESS, as a string.
915 This is the name of the program invoked in PROCESS,
916 possibly modified to make it unique among process names. */)
918 register Lisp_Object process
;
920 CHECK_PROCESS (process
);
921 return XPROCESS (process
)->name
;
924 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
925 doc
: /* Return the command that was executed to start PROCESS.
926 This is a list of strings, the first string being the program executed
927 and the rest of the strings being the arguments given to it.
928 For a non-child channel, this is nil. */)
930 register Lisp_Object process
;
932 CHECK_PROCESS (process
);
933 return XPROCESS (process
)->command
;
936 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
937 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
938 This is the terminal that the process itself reads and writes on,
939 not the name of the pty that Emacs uses to talk with that terminal. */)
941 register Lisp_Object process
;
943 CHECK_PROCESS (process
);
944 return XPROCESS (process
)->tty_name
;
947 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
949 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
951 register Lisp_Object process
, buffer
;
953 struct Lisp_Process
*p
;
955 CHECK_PROCESS (process
);
957 CHECK_BUFFER (buffer
);
958 p
= XPROCESS (process
);
961 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
962 setup_process_coding_systems (process
);
966 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
968 doc
: /* Return the buffer PROCESS is associated with.
969 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
971 register Lisp_Object process
;
973 CHECK_PROCESS (process
);
974 return XPROCESS (process
)->buffer
;
977 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
979 doc
: /* Return the marker for the end of the last output from PROCESS. */)
981 register Lisp_Object process
;
983 CHECK_PROCESS (process
);
984 return XPROCESS (process
)->mark
;
987 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
989 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
990 A value of t means stop accepting output from the process.
992 When a process has a filter, its buffer is not used for output.
993 Instead, each time it does output, the entire string of output is
994 passed to the filter.
996 The filter gets two arguments: the process and the string of output.
997 The string argument is normally a multibyte string, except:
998 - if the process' input coding system is no-conversion or raw-text,
999 it is a unibyte string (the non-converted input), or else
1000 - if `default-enable-multibyte-characters' is nil, it is a unibyte
1001 string (the result of converting the decoded input multibyte
1002 string to unibyte with `string-make-unibyte'). */)
1004 register Lisp_Object process
, filter
;
1006 struct Lisp_Process
*p
;
1008 CHECK_PROCESS (process
);
1009 p
= XPROCESS (process
);
1011 /* Don't signal an error if the process' input file descriptor
1012 is closed. This could make debugging Lisp more difficult,
1013 for example when doing something like
1015 (setq process (start-process ...))
1017 (set-process-filter process ...) */
1021 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1023 FD_CLR (p
->infd
, &input_wait_mask
);
1024 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1026 else if (EQ (p
->filter
, Qt
)
1027 && !EQ (p
->command
, Qt
)) /* Network process not stopped. */
1029 FD_SET (p
->infd
, &input_wait_mask
);
1030 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1036 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1037 setup_process_coding_systems (process
);
1041 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1043 doc
: /* Returns the filter function of PROCESS; nil if none.
1044 See `set-process-filter' for more info on filter functions. */)
1046 register Lisp_Object process
;
1048 CHECK_PROCESS (process
);
1049 return XPROCESS (process
)->filter
;
1052 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1054 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1055 The sentinel is called as a function when the process changes state.
1056 It gets two arguments: the process, and a string describing the change. */)
1058 register Lisp_Object process
, sentinel
;
1060 struct Lisp_Process
*p
;
1062 CHECK_PROCESS (process
);
1063 p
= XPROCESS (process
);
1065 p
->sentinel
= sentinel
;
1067 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1071 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1073 doc
: /* Return the sentinel of PROCESS; nil if none.
1074 See `set-process-sentinel' for more info on sentinels. */)
1076 register Lisp_Object process
;
1078 CHECK_PROCESS (process
);
1079 return XPROCESS (process
)->sentinel
;
1082 DEFUN ("set-process-window-size", Fset_process_window_size
,
1083 Sset_process_window_size
, 3, 3, 0,
1084 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1085 (process
, height
, width
)
1086 register Lisp_Object process
, height
, width
;
1088 CHECK_PROCESS (process
);
1089 CHECK_NATNUM (height
);
1090 CHECK_NATNUM (width
);
1092 if (XPROCESS (process
)->infd
< 0
1093 || set_window_size (XPROCESS (process
)->infd
,
1094 XINT (height
), XINT (width
)) <= 0)
1100 DEFUN ("set-process-inherit-coding-system-flag",
1101 Fset_process_inherit_coding_system_flag
,
1102 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1103 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1104 If the second argument FLAG is non-nil, then the variable
1105 `buffer-file-coding-system' of the buffer associated with PROCESS
1106 will be bound to the value of the coding system used to decode
1109 This is useful when the coding system specified for the process buffer
1110 leaves either the character code conversion or the end-of-line conversion
1111 unspecified, or if the coding system used to decode the process output
1112 is more appropriate for saving the process buffer.
1114 Binding the variable `inherit-process-coding-system' to non-nil before
1115 starting the process is an alternative way of setting the inherit flag
1116 for the process which will run. */)
1118 register Lisp_Object process
, flag
;
1120 CHECK_PROCESS (process
);
1121 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1125 DEFUN ("process-inherit-coding-system-flag",
1126 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1128 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1129 If this flag is t, `buffer-file-coding-system' of the buffer
1130 associated with PROCESS will inherit the coding system used to decode
1131 the process output. */)
1133 register Lisp_Object process
;
1135 CHECK_PROCESS (process
);
1136 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
1139 DEFUN ("set-process-query-on-exit-flag",
1140 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1142 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1143 If the second argument FLAG is non-nil, Emacs will query the user before
1144 exiting if PROCESS is running. */)
1146 register Lisp_Object process
, flag
;
1148 CHECK_PROCESS (process
);
1149 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1153 DEFUN ("process-query-on-exit-flag",
1154 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1156 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1158 register Lisp_Object process
;
1160 CHECK_PROCESS (process
);
1161 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1164 #ifdef DATAGRAM_SOCKETS
1165 Lisp_Object
Fprocess_datagram_address ();
1168 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1170 doc
: /* Return the contact info of PROCESS; t for a real child.
1171 For a net connection, the value depends on the optional KEY arg.
1172 If KEY is nil, value is a cons cell of the form (HOST SERVICE),
1173 if KEY is t, the complete contact information for the connection is
1174 returned, else the specific value for the keyword KEY is returned.
1175 See `make-network-process' for a list of keywords. */)
1177 register Lisp_Object process
, key
;
1179 Lisp_Object contact
;
1181 CHECK_PROCESS (process
);
1182 contact
= XPROCESS (process
)->childp
;
1184 #ifdef DATAGRAM_SOCKETS
1185 if (DATAGRAM_CONN_P (process
)
1186 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1187 contact
= Fplist_put (contact
, QCremote
,
1188 Fprocess_datagram_address (process
));
1191 if (!NETCONN_P (process
) || EQ (key
, Qt
))
1194 return Fcons (Fplist_get (contact
, QChost
),
1195 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1196 return Fplist_get (contact
, key
);
1199 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1201 doc
: /* Return the plist of PROCESS. */)
1203 register Lisp_Object process
;
1205 CHECK_PROCESS (process
);
1206 return XPROCESS (process
)->plist
;
1209 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1211 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1213 register Lisp_Object process
, plist
;
1215 CHECK_PROCESS (process
);
1218 XPROCESS (process
)->plist
= plist
;
1222 #if 0 /* Turned off because we don't currently record this info
1223 in the process. Perhaps add it. */
1224 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1225 doc
: /* Return the connection type of PROCESS.
1226 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1227 a socket connection. */)
1229 Lisp_Object process
;
1231 return XPROCESS (process
)->type
;
1236 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1238 doc
: /* Convert network ADDRESS from internal format to a string.
1239 A 4 or 5 element vector represents an IPv4 address (with port number).
1240 An 8 or 9 element vector represents an IPv6 address (with port number).
1241 If optional second argument OMIT-PORT is non-nil, don't include a port
1242 number in the string, even when present in ADDRESS.
1243 Returns nil if format of ADDRESS is invalid. */)
1244 (address
, omit_port
)
1245 Lisp_Object address
, omit_port
;
1250 if (STRINGP (address
)) /* AF_LOCAL */
1253 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1255 register struct Lisp_Vector
*p
= XVECTOR (address
);
1256 Lisp_Object args
[10];
1259 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1261 args
[0] = build_string ("%d.%d.%d.%d");
1264 else if (p
->size
== 5)
1266 args
[0] = build_string ("%d.%d.%d.%d:%d");
1269 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1271 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1274 else if (p
->size
== 9)
1276 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1282 for (i
= 0; i
< nargs
; i
++)
1284 EMACS_INT element
= XINT (p
->contents
[i
]);
1286 if (element
< 0 || element
> 65535)
1289 if (nargs
<= 5 /* IPv4 */
1290 && i
< 4 /* host, not port */
1294 args
[i
+1] = p
->contents
[i
];
1297 return Fformat (nargs
+1, args
);
1300 if (CONSP (address
))
1302 Lisp_Object args
[2];
1303 args
[0] = build_string ("<Family %d>");
1304 args
[1] = Fcar (address
);
1305 return Fformat (2, args
);
1313 list_processes_1 (query_only
)
1314 Lisp_Object query_only
;
1316 register Lisp_Object tail
, tem
;
1317 Lisp_Object proc
, minspace
, tem1
;
1318 register struct Lisp_Process
*p
;
1320 int w_proc
, w_buffer
, w_tty
;
1322 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1324 w_proc
= 4; /* Proc */
1325 w_buffer
= 6; /* Buffer */
1326 w_tty
= 0; /* Omit if no ttys */
1328 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1332 proc
= Fcdr (XCAR (tail
));
1333 p
= XPROCESS (proc
);
1334 if (NILP (p
->childp
))
1336 if (!NILP (query_only
) && p
->kill_without_query
)
1338 if (STRINGP (p
->name
)
1339 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1341 if (!NILP (p
->buffer
))
1343 if (NILP (XBUFFER (p
->buffer
)->name
))
1346 w_buffer
= 8; /* (Killed) */
1348 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1351 if (STRINGP (p
->tty_name
)
1352 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1356 XSETFASTINT (i_status
, w_proc
+ 1);
1357 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1360 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1361 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1366 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1369 XSETFASTINT (minspace
, 1);
1371 set_buffer_internal (XBUFFER (Vstandard_output
));
1372 current_buffer
->undo_list
= Qt
;
1374 current_buffer
->truncate_lines
= Qt
;
1376 write_string ("Proc", -1);
1377 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1378 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1381 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1383 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1384 write_string ("\n", -1);
1386 write_string ("----", -1);
1387 Findent_to (i_status
, minspace
); write_string ("------", -1);
1388 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1391 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1393 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1394 write_string ("\n", -1);
1396 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1400 proc
= Fcdr (XCAR (tail
));
1401 p
= XPROCESS (proc
);
1402 if (NILP (p
->childp
))
1404 if (!NILP (query_only
) && p
->kill_without_query
)
1407 Finsert (1, &p
->name
);
1408 Findent_to (i_status
, minspace
);
1410 if (p
->raw_status_new
)
1413 if (CONSP (p
->status
))
1414 symbol
= XCAR (p
->status
);
1416 if (EQ (symbol
, Qsignal
))
1419 tem
= Fcar (Fcdr (p
->status
));
1421 if (XINT (tem
) < NSIG
)
1422 write_string (sys_errlist
[XINT (tem
)], -1);
1425 Fprinc (symbol
, Qnil
);
1427 else if (NETCONN1_P (p
))
1429 if (EQ (symbol
, Qexit
))
1430 write_string ("closed", -1);
1431 else if (EQ (p
->command
, Qt
))
1432 write_string ("stopped", -1);
1433 else if (EQ (symbol
, Qrun
))
1434 write_string ("open", -1);
1436 Fprinc (symbol
, Qnil
);
1439 Fprinc (symbol
, Qnil
);
1441 if (EQ (symbol
, Qexit
))
1444 tem
= Fcar (Fcdr (p
->status
));
1447 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1448 write_string (tembuf
, -1);
1452 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1455 Findent_to (i_buffer
, minspace
);
1456 if (NILP (p
->buffer
))
1457 insert_string ("(none)");
1458 else if (NILP (XBUFFER (p
->buffer
)->name
))
1459 insert_string ("(Killed)");
1461 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1465 Findent_to (i_tty
, minspace
);
1466 if (STRINGP (p
->tty_name
))
1467 Finsert (1, &p
->tty_name
);
1470 Findent_to (i_command
, minspace
);
1472 if (EQ (p
->status
, Qlisten
))
1474 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1475 if (INTEGERP (port
))
1476 port
= Fnumber_to_string (port
);
1478 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1479 sprintf (tembuf
, "(network %s server on %s)\n",
1480 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1481 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1482 insert_string (tembuf
);
1484 else if (NETCONN1_P (p
))
1486 /* For a local socket, there is no host name,
1487 so display service instead. */
1488 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1489 if (!STRINGP (host
))
1491 host
= Fplist_get (p
->childp
, QCservice
);
1492 if (INTEGERP (host
))
1493 host
= Fnumber_to_string (host
);
1496 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1497 sprintf (tembuf
, "(network %s connection to %s)\n",
1498 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1499 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1500 insert_string (tembuf
);
1512 insert_string (" ");
1514 insert_string ("\n");
1518 status_notify (NULL
);
1522 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1523 doc
: /* Display a list of all processes.
1524 If optional argument QUERY-ONLY is non-nil, only processes with
1525 the query-on-exit flag set will be listed.
1526 Any process listed as exited or signaled is actually eliminated
1527 after the listing is made. */)
1529 Lisp_Object query_only
;
1531 internal_with_output_to_temp_buffer ("*Process List*",
1532 list_processes_1
, query_only
);
1536 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1537 doc
: /* Return a list of all processes. */)
1540 return Fmapcar (Qcdr
, Vprocess_alist
);
1543 /* Starting asynchronous inferior processes. */
1545 static Lisp_Object
start_process_unwind ();
1547 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1548 doc
: /* Start a program in a subprocess. Return the process object for it.
1549 NAME is name for process. It is modified if necessary to make it unique.
1550 BUFFER is the buffer (or buffer name) to associate with the process.
1552 Process output (both standard output and standard error streams) goes
1553 at end of BUFFER, unless you specify an output stream or filter
1554 function to handle the output. BUFFER may also be nil, meaning that
1555 this process is not associated with any buffer.
1557 PROGRAM is the program file name. It is searched for in PATH.
1558 Remaining arguments are strings to give program as arguments.
1560 If you want to separate standard output from standard error, invoke
1561 the command through a shell and redirect one of them using the shell
1564 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1567 register Lisp_Object
*args
;
1569 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1571 register unsigned char *new_argv
;
1574 register unsigned char **new_argv
;
1577 int count
= SPECPDL_INDEX ();
1581 buffer
= Fget_buffer_create (buffer
);
1583 /* Make sure that the child will be able to chdir to the current
1584 buffer's current directory, or its unhandled equivalent. We
1585 can't just have the child check for an error when it does the
1586 chdir, since it's in a vfork.
1588 We have to GCPRO around this because Fexpand_file_name and
1589 Funhandled_file_name_directory might call a file name handling
1590 function. The argument list is protected by the caller, so all
1591 we really have to worry about is buffer. */
1593 struct gcpro gcpro1
, gcpro2
;
1595 current_dir
= current_buffer
->directory
;
1597 GCPRO2 (buffer
, current_dir
);
1599 current_dir
= Funhandled_file_name_directory (current_dir
);
1600 if (NILP (current_dir
))
1601 /* If the file name handler says that current_dir is unreachable, use
1602 a sensible default. */
1603 current_dir
= build_string ("~/");
1604 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1605 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1606 report_file_error ("Setting current directory",
1607 Fcons (current_buffer
->directory
, Qnil
));
1613 CHECK_STRING (name
);
1617 CHECK_STRING (program
);
1619 proc
= make_process (name
);
1620 /* If an error occurs and we can't start the process, we want to
1621 remove it from the process list. This means that each error
1622 check in create_process doesn't need to call remove_process
1623 itself; it's all taken care of here. */
1624 record_unwind_protect (start_process_unwind
, proc
);
1626 XPROCESS (proc
)->childp
= Qt
;
1627 XPROCESS (proc
)->plist
= Qnil
;
1628 XPROCESS (proc
)->buffer
= buffer
;
1629 XPROCESS (proc
)->sentinel
= Qnil
;
1630 XPROCESS (proc
)->filter
= Qnil
;
1631 XPROCESS (proc
)->filter_multibyte
1632 = !NILP (buffer_defaults
.enable_multibyte_characters
);
1633 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1635 #ifdef ADAPTIVE_READ_BUFFERING
1636 XPROCESS (proc
)->adaptive_read_buffering
1637 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1638 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1641 /* Make the process marker point into the process buffer (if any). */
1642 if (BUFFERP (buffer
))
1643 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1644 BUF_ZV (XBUFFER (buffer
)),
1645 BUF_ZV_BYTE (XBUFFER (buffer
)));
1648 /* Decide coding systems for communicating with the process. Here
1649 we don't setup the structure coding_system nor pay attention to
1650 unibyte mode. They are done in create_process. */
1652 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1653 Lisp_Object coding_systems
= Qt
;
1654 Lisp_Object val
, *args2
;
1655 struct gcpro gcpro1
, gcpro2
;
1657 val
= Vcoding_system_for_read
;
1660 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1661 args2
[0] = Qstart_process
;
1662 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1663 GCPRO2 (proc
, current_dir
);
1664 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1666 if (CONSP (coding_systems
))
1667 val
= XCAR (coding_systems
);
1668 else if (CONSP (Vdefault_process_coding_system
))
1669 val
= XCAR (Vdefault_process_coding_system
);
1671 XPROCESS (proc
)->decode_coding_system
= val
;
1673 val
= Vcoding_system_for_write
;
1676 if (EQ (coding_systems
, Qt
))
1678 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1679 args2
[0] = Qstart_process
;
1680 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1681 GCPRO2 (proc
, current_dir
);
1682 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1685 if (CONSP (coding_systems
))
1686 val
= XCDR (coding_systems
);
1687 else if (CONSP (Vdefault_process_coding_system
))
1688 val
= XCDR (Vdefault_process_coding_system
);
1690 XPROCESS (proc
)->encode_coding_system
= val
;
1694 /* Make a one member argv with all args concatenated
1695 together separated by a blank. */
1696 len
= SBYTES (program
) + 2;
1697 for (i
= 3; i
< nargs
; i
++)
1701 len
+= SBYTES (tem
) + 1; /* count the blank */
1703 new_argv
= (unsigned char *) alloca (len
);
1704 strcpy (new_argv
, SDATA (program
));
1705 for (i
= 3; i
< nargs
; i
++)
1709 strcat (new_argv
, " ");
1710 strcat (new_argv
, SDATA (tem
));
1712 /* Need to add code here to check for program existence on VMS */
1715 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1717 /* If program file name is not absolute, search our path for it.
1718 Put the name we will really use in TEM. */
1719 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1720 && !(SCHARS (program
) > 1
1721 && IS_DEVICE_SEP (SREF (program
, 1))))
1723 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1726 GCPRO4 (name
, program
, buffer
, current_dir
);
1727 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1730 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1731 tem
= Fexpand_file_name (tem
, Qnil
);
1735 if (!NILP (Ffile_directory_p (program
)))
1736 error ("Specified program for new process is a directory");
1740 /* If program file name starts with /: for quoting a magic name,
1742 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1743 && SREF (tem
, 1) == ':')
1744 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1746 /* Encode the file name and put it in NEW_ARGV.
1747 That's where the child will use it to execute the program. */
1748 tem
= ENCODE_FILE (tem
);
1749 new_argv
[0] = SDATA (tem
);
1751 /* Here we encode arguments by the coding system used for sending
1752 data to the process. We don't support using different coding
1753 systems for encoding arguments and for encoding data sent to the
1756 for (i
= 3; i
< nargs
; i
++)
1760 if (STRING_MULTIBYTE (tem
))
1761 tem
= (code_convert_string_norecord
1762 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1763 new_argv
[i
- 2] = SDATA (tem
);
1765 new_argv
[i
- 2] = 0;
1766 #endif /* not VMS */
1768 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1769 XPROCESS (proc
)->decoding_carryover
= 0;
1770 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1772 XPROCESS (proc
)->inherit_coding_system_flag
1773 = !(NILP (buffer
) || !inherit_process_coding_system
);
1775 create_process (proc
, (char **) new_argv
, current_dir
);
1777 return unbind_to (count
, proc
);
1780 /* This function is the unwind_protect form for Fstart_process. If
1781 PROC doesn't have its pid set, then we know someone has signaled
1782 an error and the process wasn't started successfully, so we should
1783 remove it from the process list. */
1785 start_process_unwind (proc
)
1788 if (!PROCESSP (proc
))
1791 /* Was PROC started successfully? */
1792 if (XPROCESS (proc
)->pid
<= 0)
1793 remove_process (proc
);
1799 create_process_1 (timer
)
1800 struct atimer
*timer
;
1802 /* Nothing to do. */
1806 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1809 /* Mimic blocking of signals on system V, which doesn't really have it. */
1811 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1812 int sigchld_deferred
;
1815 create_process_sigchld ()
1817 signal (SIGCHLD
, create_process_sigchld
);
1819 sigchld_deferred
= 1;
1825 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1827 create_process (process
, new_argv
, current_dir
)
1828 Lisp_Object process
;
1830 Lisp_Object current_dir
;
1832 int inchannel
, outchannel
;
1835 #ifdef POSIX_SIGNALS
1838 struct sigaction sigint_action
;
1839 struct sigaction sigquit_action
;
1841 struct sigaction sighup_action
;
1843 #else /* !POSIX_SIGNALS */
1846 SIGTYPE (*sigchld
)();
1849 #endif /* !POSIX_SIGNALS */
1850 /* Use volatile to protect variables from being clobbered by longjmp. */
1851 volatile int forkin
, forkout
;
1852 volatile int pty_flag
= 0;
1854 extern char **environ
;
1857 inchannel
= outchannel
= -1;
1860 if (!NILP (Vprocess_connection_type
))
1861 outchannel
= inchannel
= allocate_pty ();
1865 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1866 /* On most USG systems it does not work to open the pty's tty here,
1867 then close it and reopen it in the child. */
1869 /* Don't let this terminal become our controlling terminal
1870 (in case we don't have one). */
1871 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1873 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1876 report_file_error ("Opening pty", Qnil
);
1877 #if defined (DONT_REOPEN_PTY)
1878 /* In the case that vfork is defined as fork, the parent process
1879 (Emacs) may send some data before the child process completes
1880 tty options setup. So we setup tty before forking. */
1881 child_setup_tty (forkout
);
1882 #endif /* DONT_REOPEN_PTY */
1884 forkin
= forkout
= -1;
1885 #endif /* not USG, or USG_SUBTTY_WORKS */
1889 #endif /* HAVE_PTYS */
1894 report_file_error ("Creating pipe", Qnil
);
1900 emacs_close (inchannel
);
1901 emacs_close (forkout
);
1902 report_file_error ("Creating pipe", Qnil
);
1909 /* Replaced by close_process_descs */
1910 set_exclusive_use (inchannel
);
1911 set_exclusive_use (outchannel
);
1915 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1916 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1919 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1920 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1924 /* Record this as an active process, with its channels.
1925 As a result, child_setup will close Emacs's side of the pipes. */
1926 chan_process
[inchannel
] = process
;
1927 XPROCESS (process
)->infd
= inchannel
;
1928 XPROCESS (process
)->outfd
= outchannel
;
1930 /* Previously we recorded the tty descriptor used in the subprocess.
1931 It was only used for getting the foreground tty process, so now
1932 we just reopen the device (see emacs_get_tty_pgrp) as this is
1933 more portable (see USG_SUBTTY_WORKS above). */
1935 XPROCESS (process
)->pty_flag
= pty_flag
;
1936 XPROCESS (process
)->status
= Qrun
;
1937 setup_process_coding_systems (process
);
1939 /* Delay interrupts until we have a chance to store
1940 the new fork's pid in its process structure */
1941 #ifdef POSIX_SIGNALS
1942 sigemptyset (&blocked
);
1944 sigaddset (&blocked
, SIGCHLD
);
1946 #ifdef HAVE_WORKING_VFORK
1947 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1948 this sets the parent's signal handlers as well as the child's.
1949 So delay all interrupts whose handlers the child might munge,
1950 and record the current handlers so they can be restored later. */
1951 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1952 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1954 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1956 #endif /* HAVE_WORKING_VFORK */
1957 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1958 #else /* !POSIX_SIGNALS */
1962 #else /* not BSD4_1 */
1963 #if defined (BSD_SYSTEM) || defined (HPUX)
1964 sigsetmask (sigmask (SIGCHLD
));
1965 #else /* ordinary USG */
1967 sigchld_deferred
= 0;
1968 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1970 #endif /* ordinary USG */
1971 #endif /* not BSD4_1 */
1972 #endif /* SIGCHLD */
1973 #endif /* !POSIX_SIGNALS */
1975 FD_SET (inchannel
, &input_wait_mask
);
1976 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1977 if (inchannel
> max_process_desc
)
1978 max_process_desc
= inchannel
;
1980 /* Until we store the proper pid, enable sigchld_handler
1981 to recognize an unknown pid as standing for this process.
1982 It is very important not to let this `marker' value stay
1983 in the table after this function has returned; if it does
1984 it might cause call-process to hang and subsequent asynchronous
1985 processes to get their return values scrambled. */
1986 XPROCESS (process
)->pid
= -1;
1991 /* child_setup must clobber environ on systems with true vfork.
1992 Protect it from permanent change. */
1993 char **save_environ
= environ
;
1995 current_dir
= ENCODE_FILE (current_dir
);
2000 #endif /* not WINDOWSNT */
2002 int xforkin
= forkin
;
2003 int xforkout
= forkout
;
2005 #if 0 /* This was probably a mistake--it duplicates code later on,
2006 but fails to handle all the cases. */
2007 /* Make sure SIGCHLD is not blocked in the child. */
2008 sigsetmask (SIGEMPTYMASK
);
2011 /* Make the pty be the controlling terminal of the process. */
2013 /* First, disconnect its current controlling terminal. */
2015 /* We tried doing setsid only if pty_flag, but it caused
2016 process_set_signal to fail on SGI when using a pipe. */
2018 /* Make the pty's terminal the controlling terminal. */
2022 /* We ignore the return value
2023 because faith@cs.unc.edu says that is necessary on Linux. */
2024 ioctl (xforkin
, TIOCSCTTY
, 0);
2027 #else /* not HAVE_SETSID */
2029 /* It's very important to call setpgrp here and no time
2030 afterwards. Otherwise, we lose our controlling tty which
2031 is set when we open the pty. */
2034 #endif /* not HAVE_SETSID */
2035 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2036 if (pty_flag
&& xforkin
>= 0)
2039 tcgetattr (xforkin
, &t
);
2041 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2042 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2045 #if defined (NTTYDISC) && defined (TIOCSETD)
2046 if (pty_flag
&& xforkin
>= 0)
2048 /* Use new line discipline. */
2049 int ldisc
= NTTYDISC
;
2050 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2055 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2056 can do TIOCSPGRP only to the process's controlling tty. */
2059 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2060 I can't test it since I don't have 4.3. */
2061 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2062 ioctl (j
, TIOCNOTTY
, 0);
2065 /* In order to get a controlling terminal on some versions
2066 of BSD, it is necessary to put the process in pgrp 0
2067 before it opens the terminal. */
2075 #endif /* TIOCNOTTY */
2077 #if !defined (DONT_REOPEN_PTY)
2078 /*** There is a suggestion that this ought to be a
2079 conditional on TIOCSPGRP,
2080 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2081 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2082 that system does seem to need this code, even though
2083 both HAVE_SETSID and TIOCSCTTY are defined. */
2084 /* Now close the pty (if we had it open) and reopen it.
2085 This makes the pty the controlling terminal of the subprocess. */
2088 #ifdef SET_CHILD_PTY_PGRP
2089 int pgrp
= getpid ();
2092 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2095 emacs_close (xforkin
);
2096 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2100 emacs_write (1, "Couldn't open the pty terminal ", 31);
2101 emacs_write (1, pty_name
, strlen (pty_name
));
2102 emacs_write (1, "\n", 1);
2106 #ifdef SET_CHILD_PTY_PGRP
2107 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
2108 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
2111 #endif /* not DONT_REOPEN_PTY */
2113 #ifdef SETUP_SLAVE_PTY
2118 #endif /* SETUP_SLAVE_PTY */
2120 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2121 Now reenable it in the child, so it will die when we want it to. */
2123 signal (SIGHUP
, SIG_DFL
);
2125 #endif /* HAVE_PTYS */
2127 signal (SIGINT
, SIG_DFL
);
2128 signal (SIGQUIT
, SIG_DFL
);
2130 /* Stop blocking signals in the child. */
2131 #ifdef POSIX_SIGNALS
2132 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2133 #else /* !POSIX_SIGNALS */
2137 #else /* not BSD4_1 */
2138 #if defined (BSD_SYSTEM) || defined (HPUX)
2139 sigsetmask (SIGEMPTYMASK
);
2140 #else /* ordinary USG */
2142 signal (SIGCHLD
, sigchld
);
2144 #endif /* ordinary USG */
2145 #endif /* not BSD4_1 */
2146 #endif /* SIGCHLD */
2147 #endif /* !POSIX_SIGNALS */
2149 #if !defined (DONT_REOPEN_PTY)
2151 child_setup_tty (xforkout
);
2152 #endif /* not DONT_REOPEN_PTY */
2154 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2155 new_argv
, 1, current_dir
);
2156 #else /* not WINDOWSNT */
2157 child_setup (xforkin
, xforkout
, xforkout
,
2158 new_argv
, 1, current_dir
);
2159 #endif /* not WINDOWSNT */
2161 environ
= save_environ
;
2166 /* This runs in the Emacs process. */
2170 emacs_close (forkin
);
2171 if (forkin
!= forkout
&& forkout
>= 0)
2172 emacs_close (forkout
);
2176 /* vfork succeeded. */
2177 XPROCESS (process
)->pid
= pid
;
2180 register_child (pid
, inchannel
);
2181 #endif /* WINDOWSNT */
2183 /* If the subfork execv fails, and it exits,
2184 this close hangs. I don't know why.
2185 So have an interrupt jar it loose. */
2187 struct atimer
*timer
;
2191 EMACS_SET_SECS_USECS (offset
, 1, 0);
2192 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2195 emacs_close (forkin
);
2197 cancel_atimer (timer
);
2201 if (forkin
!= forkout
&& forkout
>= 0)
2202 emacs_close (forkout
);
2206 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2209 XPROCESS (process
)->tty_name
= Qnil
;
2212 /* Restore the signal state whether vfork succeeded or not.
2213 (We will signal an error, below, if it failed.) */
2214 #ifdef POSIX_SIGNALS
2215 #ifdef HAVE_WORKING_VFORK
2216 /* Restore the parent's signal handlers. */
2217 sigaction (SIGINT
, &sigint_action
, 0);
2218 sigaction (SIGQUIT
, &sigquit_action
, 0);
2220 sigaction (SIGHUP
, &sighup_action
, 0);
2222 #endif /* HAVE_WORKING_VFORK */
2223 /* Stop blocking signals in the parent. */
2224 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2225 #else /* !POSIX_SIGNALS */
2229 #else /* not BSD4_1 */
2230 #if defined (BSD_SYSTEM) || defined (HPUX)
2231 sigsetmask (SIGEMPTYMASK
);
2232 #else /* ordinary USG */
2234 signal (SIGCHLD
, sigchld
);
2235 /* Now really handle any of these signals
2236 that came in during this function. */
2237 if (sigchld_deferred
)
2238 kill (getpid (), SIGCHLD
);
2240 #endif /* ordinary USG */
2241 #endif /* not BSD4_1 */
2242 #endif /* SIGCHLD */
2243 #endif /* !POSIX_SIGNALS */
2245 /* Now generate the error if vfork failed. */
2247 report_file_error ("Doing vfork", Qnil
);
2249 #endif /* not VMS */
2254 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2255 The address family of sa is not included in the result. */
2258 conv_sockaddr_to_lisp (sa
, len
)
2259 struct sockaddr
*sa
;
2262 Lisp_Object address
;
2265 register struct Lisp_Vector
*p
;
2267 switch (sa
->sa_family
)
2271 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2272 len
= sizeof (sin
->sin_addr
) + 1;
2273 address
= Fmake_vector (make_number (len
), Qnil
);
2274 p
= XVECTOR (address
);
2275 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2276 cp
= (unsigned char *)&sin
->sin_addr
;
2282 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2283 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2284 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2285 address
= Fmake_vector (make_number (len
), Qnil
);
2286 p
= XVECTOR (address
);
2287 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2288 for (i
= 0; i
< len
; i
++)
2289 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2293 #ifdef HAVE_LOCAL_SOCKETS
2296 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2297 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2298 if (sockun
->sun_path
[i
] == 0)
2300 return make_unibyte_string (sockun
->sun_path
, i
);
2304 len
-= sizeof (sa
->sa_family
);
2305 address
= Fcons (make_number (sa
->sa_family
),
2306 Fmake_vector (make_number (len
), Qnil
));
2307 p
= XVECTOR (XCDR (address
));
2308 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2314 p
->contents
[i
++] = make_number (*cp
++);
2320 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2323 get_lisp_to_sockaddr_size (address
, familyp
)
2324 Lisp_Object address
;
2327 register struct Lisp_Vector
*p
;
2329 if (VECTORP (address
))
2331 p
= XVECTOR (address
);
2335 return sizeof (struct sockaddr_in
);
2338 else if (p
->size
== 9)
2340 *familyp
= AF_INET6
;
2341 return sizeof (struct sockaddr_in6
);
2345 #ifdef HAVE_LOCAL_SOCKETS
2346 else if (STRINGP (address
))
2348 *familyp
= AF_LOCAL
;
2349 return sizeof (struct sockaddr_un
);
2352 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2354 struct sockaddr
*sa
;
2355 *familyp
= XINT (XCAR (address
));
2356 p
= XVECTOR (XCDR (address
));
2357 return p
->size
+ sizeof (sa
->sa_family
);
2362 /* Convert an address object (vector or string) to an internal sockaddr.
2364 The address format has been basically validated by
2365 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2366 it could have come from user data. So if FAMILY is not valid,
2367 we return after zeroing *SA. */
2370 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2372 Lisp_Object address
;
2373 struct sockaddr
*sa
;
2376 register struct Lisp_Vector
*p
;
2377 register unsigned char *cp
= NULL
;
2382 if (VECTORP (address
))
2384 p
= XVECTOR (address
);
2385 if (family
== AF_INET
)
2387 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2388 len
= sizeof (sin
->sin_addr
) + 1;
2389 i
= XINT (p
->contents
[--len
]);
2390 sin
->sin_port
= htons (i
);
2391 cp
= (unsigned char *)&sin
->sin_addr
;
2392 sa
->sa_family
= family
;
2395 else if (family
== AF_INET6
)
2397 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2398 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2399 len
= sizeof (sin6
->sin6_addr
) + 1;
2400 i
= XINT (p
->contents
[--len
]);
2401 sin6
->sin6_port
= htons (i
);
2402 for (i
= 0; i
< len
; i
++)
2403 if (INTEGERP (p
->contents
[i
]))
2405 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2408 sa
->sa_family
= family
;
2413 else if (STRINGP (address
))
2415 #ifdef HAVE_LOCAL_SOCKETS
2416 if (family
== AF_LOCAL
)
2418 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2419 cp
= SDATA (address
);
2420 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2421 sockun
->sun_path
[i
] = *cp
++;
2422 sa
->sa_family
= family
;
2429 p
= XVECTOR (XCDR (address
));
2430 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2433 for (i
= 0; i
< len
; i
++)
2434 if (INTEGERP (p
->contents
[i
]))
2435 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2438 #ifdef DATAGRAM_SOCKETS
2439 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2441 doc
: /* Get the current datagram address associated with PROCESS. */)
2443 Lisp_Object process
;
2447 CHECK_PROCESS (process
);
2449 if (!DATAGRAM_CONN_P (process
))
2452 channel
= XPROCESS (process
)->infd
;
2453 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2454 datagram_address
[channel
].len
);
2457 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2459 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2460 Returns nil upon error setting address, ADDRESS otherwise. */)
2462 Lisp_Object process
, address
;
2467 CHECK_PROCESS (process
);
2469 if (!DATAGRAM_CONN_P (process
))
2472 channel
= XPROCESS (process
)->infd
;
2474 len
= get_lisp_to_sockaddr_size (address
, &family
);
2475 if (datagram_address
[channel
].len
!= len
)
2477 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2483 static struct socket_options
{
2484 /* The name of this option. Should be lowercase version of option
2485 name without SO_ prefix. */
2487 /* Option level SOL_... */
2489 /* Option number SO_... */
2491 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2492 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2493 } socket_options
[] =
2495 #ifdef SO_BINDTODEVICE
2496 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2499 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2502 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2505 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2508 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2511 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2514 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2517 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2519 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2522 /* Set option OPT to value VAL on socket S.
2524 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2525 Signals an error if setting a known option fails.
2529 set_socket_option (s
, opt
, val
)
2531 Lisp_Object opt
, val
;
2534 struct socket_options
*sopt
;
2539 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2540 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2541 if (strcmp (name
, sopt
->name
) == 0)
2544 switch (sopt
->opttype
)
2549 optval
= NILP (val
) ? 0 : 1;
2550 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2551 &optval
, sizeof (optval
));
2559 optval
= XINT (val
);
2561 error ("Bad option value for %s", name
);
2562 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2563 &optval
, sizeof (optval
));
2567 #ifdef SO_BINDTODEVICE
2570 char devname
[IFNAMSIZ
+1];
2572 /* This is broken, at least in the Linux 2.4 kernel.
2573 To unbind, the arg must be a zero integer, not the empty string.
2574 This should work on all systems. KFS. 2003-09-23. */
2575 bzero (devname
, sizeof devname
);
2578 char *arg
= (char *) SDATA (val
);
2579 int len
= min (strlen (arg
), IFNAMSIZ
);
2580 bcopy (arg
, devname
, len
);
2582 else if (!NILP (val
))
2583 error ("Bad option value for %s", name
);
2584 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2593 struct linger linger
;
2596 linger
.l_linger
= 0;
2598 linger
.l_linger
= XINT (val
);
2600 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2601 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2602 &linger
, sizeof (linger
));
2612 report_file_error ("Cannot set network option",
2613 Fcons (opt
, Fcons (val
, Qnil
)));
2614 return (1 << sopt
->optbit
);
2618 DEFUN ("set-network-process-option",
2619 Fset_network_process_option
, Sset_network_process_option
,
2621 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2622 See `make-network-process' for a list of options and values.
2623 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2624 OPTION is not a supported option, return nil instead; otherwise return t. */)
2625 (process
, option
, value
, no_error
)
2626 Lisp_Object process
, option
, value
;
2627 Lisp_Object no_error
;
2630 struct Lisp_Process
*p
;
2632 CHECK_PROCESS (process
);
2633 p
= XPROCESS (process
);
2634 if (!NETCONN1_P (p
))
2635 error ("Process is not a network process");
2639 error ("Process is not running");
2641 if (set_socket_option (s
, option
, value
))
2643 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2647 if (NILP (no_error
))
2648 error ("Unknown or unsupported option");
2654 /* A version of request_sigio suitable for a record_unwind_protect. */
2658 unwind_request_sigio (dummy
)
2661 if (interrupt_input
)
2667 /* Create a network stream/datagram client/server process. Treated
2668 exactly like a normal process when reading and writing. Primary
2669 differences are in status display and process deletion. A network
2670 connection has no PID; you cannot signal it. All you can do is
2671 stop/continue it and deactivate/close it via delete-process */
2673 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2675 doc
: /* Create and return a network server or client process.
2677 In Emacs, network connections are represented by process objects, so
2678 input and output work as for subprocesses and `delete-process' closes
2679 a network connection. However, a network process has no process id,
2680 it cannot be signaled, and the status codes are different from normal
2683 Arguments are specified as keyword/argument pairs. The following
2684 arguments are defined:
2686 :name NAME -- NAME is name for process. It is modified if necessary
2689 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2690 with the process. Process output goes at end of that buffer, unless
2691 you specify an output stream or filter function to handle the output.
2692 BUFFER may be also nil, meaning that this process is not associated
2695 :host HOST -- HOST is name of the host to connect to, or its IP
2696 address. The symbol `local' specifies the local host. If specified
2697 for a server process, it must be a valid name or address for the local
2698 host, and only clients connecting to that address will be accepted.
2700 :service SERVICE -- SERVICE is name of the service desired, or an
2701 integer specifying a port number to connect to. If SERVICE is t,
2702 a random port number is selected for the server. (If Emacs was
2703 compiled with getaddrinfo, a port number can also be specified as a
2704 string, e.g. "80", as well as an integer. This is not portable.)
2706 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2707 stream type connection, `datagram' creates a datagram type connection.
2709 :family FAMILY -- FAMILY is the address (and protocol) family for the
2710 service specified by HOST and SERVICE. The default (nil) is to use
2711 whatever address family (IPv4 or IPv6) that is defined for the host
2712 and port number specified by HOST and SERVICE. Other address families
2714 local -- for a local (i.e. UNIX) address specified by SERVICE.
2715 ipv4 -- use IPv4 address family only.
2716 ipv6 -- use IPv6 address family only.
2718 :local ADDRESS -- ADDRESS is the local address used for the connection.
2719 This parameter is ignored when opening a client process. When specified
2720 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2722 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2723 connection. This parameter is ignored when opening a stream server
2724 process. For a datagram server process, it specifies the initial
2725 setting of the remote datagram address. When specified for a client
2726 process, the FAMILY, HOST, and SERVICE args are ignored.
2728 The format of ADDRESS depends on the address family:
2729 - An IPv4 address is represented as an vector of integers [A B C D P]
2730 corresponding to numeric IP address A.B.C.D and port number P.
2731 - A local address is represented as a string with the address in the
2732 local address space.
2733 - An "unsupported family" address is represented by a cons (F . AV)
2734 where F is the family number and AV is a vector containing the socket
2735 address data with one element per address data byte. Do not rely on
2736 this format in portable code, as it may depend on implementation
2737 defined constants, data sizes, and data structure alignment.
2739 :coding CODING -- If CODING is a symbol, it specifies the coding
2740 system used for both reading and writing for this process. If CODING
2741 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2742 ENCODING is used for writing.
2744 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2745 return without waiting for the connection to complete; instead, the
2746 sentinel function will be called with second arg matching "open" (if
2747 successful) or "failed" when the connect completes. Default is to use
2748 a blocking connect (i.e. wait) for stream type connections.
2750 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2751 running when Emacs is exited.
2753 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2754 In the stopped state, a server process does not accept new
2755 connections, and a client process does not handle incoming traffic.
2756 The stopped state is cleared by `continue-process' and set by
2759 :filter FILTER -- Install FILTER as the process filter.
2761 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
2762 process filter are multibyte, otherwise they are unibyte.
2763 If this keyword is not specified, the strings are multibyte if
2764 `default-enable-multibyte-characters' is non-nil.
2766 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2768 :log LOG -- Install LOG as the server process log function. This
2769 function is called when the server accepts a network connection from a
2770 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2771 is the server process, CLIENT is the new process for the connection,
2772 and MESSAGE is a string.
2774 :plist PLIST -- Install PLIST as the new process' initial plist.
2776 :server QLEN -- if QLEN is non-nil, create a server process for the
2777 specified FAMILY, SERVICE, and connection type (stream or datagram).
2778 If QLEN is an integer, it is used as the max. length of the server's
2779 pending connection queue (also known as the backlog); the default
2780 queue length is 5. Default is to create a client process.
2782 The following network options can be specified for this connection:
2784 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2785 :dontroute BOOL -- Only send to directly connected hosts.
2786 :keepalive BOOL -- Send keep-alive messages on network stream.
2787 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2788 :oobinline BOOL -- Place out-of-band data in receive data stream.
2789 :priority INT -- Set protocol defined priority for sent packets.
2790 :reuseaddr BOOL -- Allow reusing a recently used local address
2791 (this is allowed by default for a server process).
2792 :bindtodevice NAME -- bind to interface NAME. Using this may require
2793 special privileges on some systems.
2795 Consult the relevant system programmer's manual pages for more
2796 information on using these options.
2799 A server process will listen for and accept connections from clients.
2800 When a client connection is accepted, a new network process is created
2801 for the connection with the following parameters:
2803 - The client's process name is constructed by concatenating the server
2804 process' NAME and a client identification string.
2805 - If the FILTER argument is non-nil, the client process will not get a
2806 separate process buffer; otherwise, the client's process buffer is a newly
2807 created buffer named after the server process' BUFFER name or process
2808 NAME concatenated with the client identification string.
2809 - The connection type and the process filter and sentinel parameters are
2810 inherited from the server process' TYPE, FILTER and SENTINEL.
2811 - The client process' contact info is set according to the client's
2812 addressing information (typically an IP address and a port number).
2813 - The client process' plist is initialized from the server's plist.
2815 Notice that the FILTER and SENTINEL args are never used directly by
2816 the server process. Also, the BUFFER argument is not used directly by
2817 the server process, but via the optional :log function, accepted (and
2818 failed) connections may be logged in the server process' buffer.
2820 The original argument list, modified with the actual connection
2821 information, is available via the `process-contact' function.
2823 usage: (make-network-process &rest ARGS) */)
2829 Lisp_Object contact
;
2830 struct Lisp_Process
*p
;
2831 #ifdef HAVE_GETADDRINFO
2832 struct addrinfo ai
, *res
, *lres
;
2833 struct addrinfo hints
;
2834 char *portstring
, portbuf
[128];
2835 #else /* HAVE_GETADDRINFO */
2836 struct _emacs_addrinfo
2842 struct sockaddr
*ai_addr
;
2843 struct _emacs_addrinfo
*ai_next
;
2845 #endif /* HAVE_GETADDRINFO */
2846 struct sockaddr_in address_in
;
2847 #ifdef HAVE_LOCAL_SOCKETS
2848 struct sockaddr_un address_un
;
2853 int s
= -1, outch
, inch
;
2854 struct gcpro gcpro1
;
2855 int count
= SPECPDL_INDEX ();
2857 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
2859 Lisp_Object name
, buffer
, host
, service
, address
;
2860 Lisp_Object filter
, sentinel
;
2861 int is_non_blocking_client
= 0;
2862 int is_server
= 0, backlog
= 5;
2869 /* Save arguments for process-contact and clone-process. */
2870 contact
= Flist (nargs
, args
);
2874 /* Ensure socket support is loaded if available. */
2875 init_winsock (TRUE
);
2878 /* :type TYPE (nil: stream, datagram */
2879 tem
= Fplist_get (contact
, QCtype
);
2881 socktype
= SOCK_STREAM
;
2882 #ifdef DATAGRAM_SOCKETS
2883 else if (EQ (tem
, Qdatagram
))
2884 socktype
= SOCK_DGRAM
;
2887 error ("Unsupported connection type");
2890 tem
= Fplist_get (contact
, QCserver
);
2893 /* Don't support network sockets when non-blocking mode is
2894 not available, since a blocked Emacs is not useful. */
2895 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY))
2896 error ("Network servers not supported");
2900 backlog
= XINT (tem
);
2904 /* Make QCaddress an alias for :local (server) or :remote (client). */
2905 QCaddress
= is_server
? QClocal
: QCremote
;
2908 if (!is_server
&& socktype
== SOCK_STREAM
2909 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
2911 #ifndef NON_BLOCKING_CONNECT
2912 error ("Non-blocking connect not supported");
2914 is_non_blocking_client
= 1;
2918 name
= Fplist_get (contact
, QCname
);
2919 buffer
= Fplist_get (contact
, QCbuffer
);
2920 filter
= Fplist_get (contact
, QCfilter
);
2921 sentinel
= Fplist_get (contact
, QCsentinel
);
2923 CHECK_STRING (name
);
2926 /* Let's handle TERM before things get complicated ... */
2927 host
= Fplist_get (contact
, QChost
);
2928 CHECK_STRING (host
);
2930 service
= Fplist_get (contact
, QCservice
);
2931 if (INTEGERP (service
))
2932 port
= htons ((unsigned short) XINT (service
));
2935 struct servent
*svc_info
;
2936 CHECK_STRING (service
);
2937 svc_info
= getservbyname (SDATA (service
), "tcp");
2939 error ("Unknown service: %s", SDATA (service
));
2940 port
= svc_info
->s_port
;
2943 s
= connect_server (0);
2945 report_file_error ("error creating socket", Fcons (name
, Qnil
));
2946 send_command (s
, C_PORT
, 0, "%s:%d", SDATA (host
), ntohs (port
));
2947 send_command (s
, C_DUMB
, 1, 0);
2949 #else /* not TERM */
2951 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
2952 ai
.ai_socktype
= socktype
;
2957 /* :local ADDRESS or :remote ADDRESS */
2958 address
= Fplist_get (contact
, QCaddress
);
2959 if (!NILP (address
))
2961 host
= service
= Qnil
;
2963 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
2964 error ("Malformed :address");
2965 ai
.ai_family
= family
;
2966 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
2967 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
2971 /* :family FAMILY -- nil (for Inet), local, or integer. */
2972 tem
= Fplist_get (contact
, QCfamily
);
2975 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
2981 #ifdef HAVE_LOCAL_SOCKETS
2982 else if (EQ (tem
, Qlocal
))
2986 else if (EQ (tem
, Qipv6
))
2989 else if (EQ (tem
, Qipv4
))
2991 else if (INTEGERP (tem
))
2992 family
= XINT (tem
);
2994 error ("Unknown address family");
2996 ai
.ai_family
= family
;
2998 /* :service SERVICE -- string, integer (port number), or t (random port). */
2999 service
= Fplist_get (contact
, QCservice
);
3001 #ifdef HAVE_LOCAL_SOCKETS
3002 if (family
== AF_LOCAL
)
3004 /* Host is not used. */
3006 CHECK_STRING (service
);
3007 bzero (&address_un
, sizeof address_un
);
3008 address_un
.sun_family
= AF_LOCAL
;
3009 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3010 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3011 ai
.ai_addrlen
= sizeof address_un
;
3016 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3017 host
= Fplist_get (contact
, QChost
);
3020 if (EQ (host
, Qlocal
))
3021 host
= build_string ("localhost");
3022 CHECK_STRING (host
);
3025 /* Slow down polling to every ten seconds.
3026 Some kernels have a bug which causes retrying connect to fail
3027 after a connect. Polling can interfere with gethostbyname too. */
3028 #ifdef POLL_FOR_INPUT
3029 if (socktype
== SOCK_STREAM
)
3031 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3032 bind_polling_period (10);
3036 #ifdef HAVE_GETADDRINFO
3037 /* If we have a host, use getaddrinfo to resolve both host and service.
3038 Otherwise, use getservbyname to lookup the service. */
3042 /* SERVICE can either be a string or int.
3043 Convert to a C string for later use by getaddrinfo. */
3044 if (EQ (service
, Qt
))
3046 else if (INTEGERP (service
))
3048 sprintf (portbuf
, "%ld", (long) XINT (service
));
3049 portstring
= portbuf
;
3053 CHECK_STRING (service
);
3054 portstring
= SDATA (service
);
3059 memset (&hints
, 0, sizeof (hints
));
3061 hints
.ai_family
= family
;
3062 hints
.ai_socktype
= socktype
;
3063 hints
.ai_protocol
= 0;
3065 #ifdef HAVE_RES_INIT
3069 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3071 #ifdef HAVE_GAI_STRERROR
3072 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3074 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3080 #endif /* HAVE_GETADDRINFO */
3082 /* We end up here if getaddrinfo is not defined, or in case no hostname
3083 has been specified (e.g. for a local server process). */
3085 if (EQ (service
, Qt
))
3087 else if (INTEGERP (service
))
3088 port
= htons ((unsigned short) XINT (service
));
3091 struct servent
*svc_info
;
3092 CHECK_STRING (service
);
3093 svc_info
= getservbyname (SDATA (service
),
3094 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3096 error ("Unknown service: %s", SDATA (service
));
3097 port
= svc_info
->s_port
;
3100 bzero (&address_in
, sizeof address_in
);
3101 address_in
.sin_family
= family
;
3102 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3103 address_in
.sin_port
= port
;
3105 #ifndef HAVE_GETADDRINFO
3108 struct hostent
*host_info_ptr
;
3110 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3111 as it may `hang' Emacs for a very long time. */
3115 #ifdef HAVE_RES_INIT
3119 host_info_ptr
= gethostbyname (SDATA (host
));
3124 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3125 host_info_ptr
->h_length
);
3126 family
= host_info_ptr
->h_addrtype
;
3127 address_in
.sin_family
= family
;
3130 /* Attempt to interpret host as numeric inet address */
3132 unsigned long numeric_addr
;
3133 numeric_addr
= inet_addr ((char *) SDATA (host
));
3134 if (numeric_addr
== -1)
3135 error ("Unknown host \"%s\"", SDATA (host
));
3137 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3138 sizeof (address_in
.sin_addr
));
3142 #endif /* not HAVE_GETADDRINFO */
3144 ai
.ai_family
= family
;
3145 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3146 ai
.ai_addrlen
= sizeof address_in
;
3151 /* Previously this was compiled unconditionally, but that seems
3152 unnecessary on modern systems, and `unrequest_sigio' was a noop
3153 under X anyway. --lorentey */
3154 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
3155 when connect is interrupted. So let's not let it get interrupted.
3156 Note we do not turn off polling, because polling is only used
3157 when not interrupt_input, and thus not normally used on the systems
3158 which have this bug. On systems which use polling, there's no way
3159 to quit if polling is turned off. */
3161 && !is_server
&& socktype
== SOCK_STREAM
)
3163 /* Comment from KFS: The original open-network-stream code
3164 didn't unwind protect this, but it seems like the proper
3165 thing to do. In any case, I don't see how it could harm to
3166 do this -- and it makes cleanup (using unbind_to) easier. */
3167 record_unwind_protect (unwind_request_sigio
, Qnil
);
3172 /* Do this in case we never enter the for-loop below. */
3173 count1
= SPECPDL_INDEX ();
3176 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3182 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3189 #ifdef DATAGRAM_SOCKETS
3190 if (!is_server
&& socktype
== SOCK_DGRAM
)
3192 #endif /* DATAGRAM_SOCKETS */
3194 #ifdef NON_BLOCKING_CONNECT
3195 if (is_non_blocking_client
)
3198 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3200 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3212 /* Make us close S if quit. */
3213 record_unwind_protect (close_file_unwind
, make_number (s
));
3215 /* Parse network options in the arg list.
3216 We simply ignore anything which isn't a known option (including other keywords).
3217 An error is signalled if setting a known option fails. */
3218 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3219 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3223 /* Configure as a server socket. */
3225 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3226 explicit :reuseaddr key to override this. */
3227 #ifdef HAVE_LOCAL_SOCKETS
3228 if (family
!= AF_LOCAL
)
3230 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3233 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3234 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3237 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3238 report_file_error ("Cannot bind server socket", Qnil
);
3240 #ifdef HAVE_GETSOCKNAME
3241 if (EQ (service
, Qt
))
3243 struct sockaddr_in sa1
;
3244 int len1
= sizeof (sa1
);
3245 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3247 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3248 service
= make_number (ntohs (sa1
.sin_port
));
3249 contact
= Fplist_put (contact
, QCservice
, service
);
3254 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3255 report_file_error ("Cannot listen on server socket", Qnil
);
3263 /* This turns off all alarm-based interrupts; the
3264 bind_polling_period call above doesn't always turn all the
3265 short-interval ones off, especially if interrupt_input is
3268 It'd be nice to be able to control the connect timeout
3269 though. Would non-blocking connect calls be portable?
3271 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3273 turn_on_atimers (0);
3275 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3278 turn_on_atimers (1);
3280 if (ret
== 0 || xerrno
== EISCONN
)
3282 /* The unwind-protect will be discarded afterwards.
3283 Likewise for immediate_quit. */
3287 #ifdef NON_BLOCKING_CONNECT
3289 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3293 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3301 /* Discard the unwind protect closing S. */
3302 specpdl_ptr
= specpdl
+ count1
;
3306 if (xerrno
== EINTR
)
3312 #ifdef DATAGRAM_SOCKETS
3313 if (socktype
== SOCK_DGRAM
)
3315 if (datagram_address
[s
].sa
)
3317 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3318 datagram_address
[s
].len
= lres
->ai_addrlen
;
3322 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3323 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3326 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3327 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3328 conv_lisp_to_sockaddr (rfamily
, remote
,
3329 datagram_address
[s
].sa
, rlen
);
3333 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3336 contact
= Fplist_put (contact
, QCaddress
,
3337 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3338 #ifdef HAVE_GETSOCKNAME
3341 struct sockaddr_in sa1
;
3342 int len1
= sizeof (sa1
);
3343 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3344 contact
= Fplist_put (contact
, QClocal
,
3345 conv_sockaddr_to_lisp (&sa1
, len1
));
3352 #ifdef HAVE_GETADDRINFO
3361 /* Discard the unwind protect for closing S, if any. */
3362 specpdl_ptr
= specpdl
+ count1
;
3364 /* Unwind bind_polling_period and request_sigio. */
3365 unbind_to (count
, Qnil
);
3369 /* If non-blocking got this far - and failed - assume non-blocking is
3370 not supported after all. This is probably a wrong assumption, but
3371 the normal blocking calls to open-network-stream handles this error
3373 if (is_non_blocking_client
)
3378 report_file_error ("make server process failed", contact
);
3380 report_file_error ("make client process failed", contact
);
3383 #endif /* not TERM */
3389 buffer
= Fget_buffer_create (buffer
);
3390 proc
= make_process (name
);
3392 chan_process
[inch
] = proc
;
3395 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3398 fcntl (inch
, F_SETFL
, O_NDELAY
);
3402 p
= XPROCESS (proc
);
3404 p
->childp
= contact
;
3405 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3408 p
->sentinel
= sentinel
;
3410 p
->filter_multibyte
= !NILP (buffer_defaults
.enable_multibyte_characters
);
3411 /* Override the above only if :filter-multibyte is specified. */
3412 if (! NILP (Fplist_member (contact
, QCfilter_multibyte
)))
3413 p
->filter_multibyte
= !NILP (Fplist_get (contact
, QCfilter_multibyte
));
3414 p
->log
= Fplist_get (contact
, QClog
);
3415 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3416 p
->kill_without_query
= 1;
3417 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3422 if (is_server
&& socktype
== SOCK_STREAM
)
3423 p
->status
= Qlisten
;
3425 /* Make the process marker point into the process buffer (if any). */
3426 if (BUFFERP (buffer
))
3427 set_marker_both (p
->mark
, buffer
,
3428 BUF_ZV (XBUFFER (buffer
)),
3429 BUF_ZV_BYTE (XBUFFER (buffer
)));
3431 #ifdef NON_BLOCKING_CONNECT
3432 if (is_non_blocking_client
)
3434 /* We may get here if connect did succeed immediately. However,
3435 in that case, we still need to signal this like a non-blocking
3437 p
->status
= Qconnect
;
3438 if (!FD_ISSET (inch
, &connect_wait_mask
))
3440 FD_SET (inch
, &connect_wait_mask
);
3441 num_pending_connects
++;
3446 /* A server may have a client filter setting of Qt, but it must
3447 still listen for incoming connects unless it is stopped. */
3448 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3449 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3451 FD_SET (inch
, &input_wait_mask
);
3452 FD_SET (inch
, &non_keyboard_wait_mask
);
3455 if (inch
> max_process_desc
)
3456 max_process_desc
= inch
;
3458 tem
= Fplist_member (contact
, QCcoding
);
3459 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3460 tem
= Qnil
; /* No error message (too late!). */
3463 /* Setup coding systems for communicating with the network stream. */
3464 struct gcpro gcpro1
;
3465 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3466 Lisp_Object coding_systems
= Qt
;
3467 Lisp_Object args
[5], val
;
3471 val
= XCAR (XCDR (tem
));
3475 else if (!NILP (Vcoding_system_for_read
))
3476 val
= Vcoding_system_for_read
;
3477 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3478 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3479 /* We dare not decode end-of-line format by setting VAL to
3480 Qraw_text, because the existing Emacs Lisp libraries
3481 assume that they receive bare code including a sequene of
3486 if (NILP (host
) || NILP (service
))
3487 coding_systems
= Qnil
;
3490 args
[0] = Qopen_network_stream
, args
[1] = name
,
3491 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3493 coding_systems
= Ffind_operation_coding_system (5, args
);
3496 if (CONSP (coding_systems
))
3497 val
= XCAR (coding_systems
);
3498 else if (CONSP (Vdefault_process_coding_system
))
3499 val
= XCAR (Vdefault_process_coding_system
);
3503 p
->decode_coding_system
= val
;
3507 val
= XCAR (XCDR (tem
));
3511 else if (!NILP (Vcoding_system_for_write
))
3512 val
= Vcoding_system_for_write
;
3513 else if (NILP (current_buffer
->enable_multibyte_characters
))
3517 if (EQ (coding_systems
, Qt
))
3519 if (NILP (host
) || NILP (service
))
3520 coding_systems
= Qnil
;
3523 args
[0] = Qopen_network_stream
, args
[1] = name
,
3524 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3526 coding_systems
= Ffind_operation_coding_system (5, args
);
3530 if (CONSP (coding_systems
))
3531 val
= XCDR (coding_systems
);
3532 else if (CONSP (Vdefault_process_coding_system
))
3533 val
= XCDR (Vdefault_process_coding_system
);
3537 p
->encode_coding_system
= val
;
3539 setup_process_coding_systems (proc
);
3541 p
->decoding_buf
= make_uninit_string (0);
3542 p
->decoding_carryover
= 0;
3543 p
->encoding_buf
= make_uninit_string (0);
3545 p
->inherit_coding_system_flag
3546 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3551 #endif /* HAVE_SOCKETS */
3554 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3557 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3558 doc
: /* Return an alist of all network interfaces and their network address.
3559 Each element is a cons, the car of which is a string containing the
3560 interface name, and the cdr is the network address in internal
3561 format; see the description of ADDRESS in `make-network-process'. */)
3564 struct ifconf ifconf
;
3565 struct ifreq
*ifreqs
= NULL
;
3570 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3576 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3577 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3584 ifconf
.ifc_len
= buf_size
;
3585 ifconf
.ifc_req
= ifreqs
;
3586 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3592 if (ifconf
.ifc_len
== buf_size
)
3596 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3599 while (--ifaces
>= 0)
3601 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3602 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3603 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3605 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3606 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3607 res
= Fcons (Fcons (build_string (namebuf
),
3608 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3609 sizeof (struct sockaddr
))),
3615 #endif /* SIOCGIFCONF */
3617 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3624 static struct ifflag_def ifflag_table
[] = {
3628 #ifdef IFF_BROADCAST
3629 { IFF_BROADCAST
, "broadcast" },
3632 { IFF_DEBUG
, "debug" },
3635 { IFF_LOOPBACK
, "loopback" },
3637 #ifdef IFF_POINTOPOINT
3638 { IFF_POINTOPOINT
, "pointopoint" },
3641 { IFF_RUNNING
, "running" },
3644 { IFF_NOARP
, "noarp" },
3647 { IFF_PROMISC
, "promisc" },
3649 #ifdef IFF_NOTRAILERS
3650 { IFF_NOTRAILERS
, "notrailers" },
3653 { IFF_ALLMULTI
, "allmulti" },
3656 { IFF_MASTER
, "master" },
3659 { IFF_SLAVE
, "slave" },
3661 #ifdef IFF_MULTICAST
3662 { IFF_MULTICAST
, "multicast" },
3665 { IFF_PORTSEL
, "portsel" },
3667 #ifdef IFF_AUTOMEDIA
3668 { IFF_AUTOMEDIA
, "automedia" },
3671 { IFF_DYNAMIC
, "dynamic" },
3674 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3677 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3680 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3683 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3686 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3691 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3692 doc
: /* Return information about network interface named IFNAME.
3693 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3694 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3695 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3696 FLAGS is the current flags of the interface. */)
3701 Lisp_Object res
= Qnil
;
3706 CHECK_STRING (ifname
);
3708 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
3709 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3711 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3716 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3717 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3719 int flags
= rq
.ifr_flags
;
3720 struct ifflag_def
*fp
;
3724 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3726 if (flags
& fp
->flag_bit
)
3728 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3729 flags
-= fp
->flag_bit
;
3732 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3734 if (flags
& (1 << fnum
))
3736 elt
= Fcons (make_number (fnum
), elt
);
3741 res
= Fcons (elt
, res
);
3744 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3745 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3747 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3748 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3752 for (n
= 0; n
< 6; n
++)
3753 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3754 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3757 res
= Fcons (elt
, res
);
3760 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
3761 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
3764 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
3765 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
3767 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3771 res
= Fcons (elt
, res
);
3774 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3775 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
3778 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
3781 res
= Fcons (elt
, res
);
3784 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
3785 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
3788 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3791 res
= Fcons (elt
, res
);
3795 return any
? res
: Qnil
;
3798 #endif /* HAVE_SOCKETS */
3800 /* Turn off input and output for process PROC. */
3803 deactivate_process (proc
)
3806 register int inchannel
, outchannel
;
3807 register struct Lisp_Process
*p
= XPROCESS (proc
);
3809 inchannel
= p
->infd
;
3810 outchannel
= p
->outfd
;
3812 #ifdef ADAPTIVE_READ_BUFFERING
3813 if (p
->read_output_delay
> 0)
3815 if (--process_output_delay_count
< 0)
3816 process_output_delay_count
= 0;
3817 p
->read_output_delay
= 0;
3818 p
->read_output_skip
= 0;
3824 /* Beware SIGCHLD hereabouts. */
3825 flush_pending_output (inchannel
);
3828 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
3829 sys$
dassgn (outchannel
);
3830 vs
= get_vms_process_pointer (p
->pid
);
3832 give_back_vms_process_stuff (vs
);
3835 emacs_close (inchannel
);
3836 if (outchannel
>= 0 && outchannel
!= inchannel
)
3837 emacs_close (outchannel
);
3842 #ifdef DATAGRAM_SOCKETS
3843 if (DATAGRAM_CHAN_P (inchannel
))
3845 xfree (datagram_address
[inchannel
].sa
);
3846 datagram_address
[inchannel
].sa
= 0;
3847 datagram_address
[inchannel
].len
= 0;
3850 chan_process
[inchannel
] = Qnil
;
3851 FD_CLR (inchannel
, &input_wait_mask
);
3852 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
3853 #ifdef NON_BLOCKING_CONNECT
3854 if (FD_ISSET (inchannel
, &connect_wait_mask
))
3856 FD_CLR (inchannel
, &connect_wait_mask
);
3857 if (--num_pending_connects
< 0)
3861 if (inchannel
== max_process_desc
)
3864 /* We just closed the highest-numbered process input descriptor,
3865 so recompute the highest-numbered one now. */
3866 max_process_desc
= 0;
3867 for (i
= 0; i
< MAXDESC
; i
++)
3868 if (!NILP (chan_process
[i
]))
3869 max_process_desc
= i
;
3874 /* Close all descriptors currently in use for communication
3875 with subprocess. This is used in a newly-forked subprocess
3876 to get rid of irrelevant descriptors. */
3879 close_process_descs ()
3883 for (i
= 0; i
< MAXDESC
; i
++)
3885 Lisp_Object process
;
3886 process
= chan_process
[i
];
3887 if (!NILP (process
))
3889 int in
= XPROCESS (process
)->infd
;
3890 int out
= XPROCESS (process
)->outfd
;
3893 if (out
>= 0 && in
!= out
)
3900 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
3902 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
3903 It is read into the process' buffers or given to their filter functions.
3904 Non-nil arg PROCESS means do not return until some output has been received
3907 Non-nil second arg SECONDS and third arg MILLISEC are number of
3908 seconds and milliseconds to wait; return after that much time whether
3909 or not there is input. If SECONDS is a floating point number,
3910 it specifies a fractional number of seconds to wait.
3912 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
3913 from PROCESS, suspending reading output from other processes.
3914 If JUST-THIS-ONE is an integer, don't run any timers either.
3915 Return non-nil if we received any output before the timeout expired. */)
3916 (process
, seconds
, millisec
, just_this_one
)
3917 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
3919 int secs
, usecs
= 0;
3921 if (! NILP (process
))
3922 CHECK_PROCESS (process
);
3924 just_this_one
= Qnil
;
3926 if (!NILP (seconds
))
3928 if (INTEGERP (seconds
))
3929 secs
= XINT (seconds
);
3930 else if (FLOATP (seconds
))
3932 double timeout
= XFLOAT_DATA (seconds
);
3933 secs
= (int) timeout
;
3934 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
3937 wrong_type_argument (Qnumberp
, seconds
);
3939 if (INTEGERP (millisec
))
3942 usecs
+= XINT (millisec
) * 1000;
3943 carry
= usecs
/ 1000000;
3945 if ((usecs
-= carry
* 1000000) < 0)
3952 if (secs
< 0 || (secs
== 0 && usecs
== 0))
3953 secs
= -1, usecs
= 0;
3956 secs
= NILP (process
) ? -1 : 0;
3959 (wait_reading_process_output (secs
, usecs
, 0, 0,
3961 !NILP (process
) ? XPROCESS (process
) : NULL
,
3962 NILP (just_this_one
) ? 0 :
3963 !INTEGERP (just_this_one
) ? 1 : -1)
3967 /* Accept a connection for server process SERVER on CHANNEL. */
3969 static int connect_counter
= 0;
3972 server_accept_connection (server
, channel
)
3976 Lisp_Object proc
, caller
, name
, buffer
;
3977 Lisp_Object contact
, host
, service
;
3978 struct Lisp_Process
*ps
= XPROCESS (server
);
3979 struct Lisp_Process
*p
;
3983 struct sockaddr_in in
;
3985 struct sockaddr_in6 in6
;
3987 #ifdef HAVE_LOCAL_SOCKETS
3988 struct sockaddr_un un
;
3991 int len
= sizeof saddr
;
3993 s
= accept (channel
, &saddr
.sa
, &len
);
4002 if (code
== EWOULDBLOCK
)
4006 if (!NILP (ps
->log
))
4007 call3 (ps
->log
, server
, Qnil
,
4008 concat3 (build_string ("accept failed with code"),
4009 Fnumber_to_string (make_number (code
)),
4010 build_string ("\n")));
4016 /* Setup a new process to handle the connection. */
4018 /* Generate a unique identification of the caller, and build contact
4019 information for this process. */
4022 switch (saddr
.sa
.sa_family
)
4026 Lisp_Object args
[5];
4027 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4028 args
[0] = build_string ("%d.%d.%d.%d");
4029 args
[1] = make_number (*ip
++);
4030 args
[2] = make_number (*ip
++);
4031 args
[3] = make_number (*ip
++);
4032 args
[4] = make_number (*ip
++);
4033 host
= Fformat (5, args
);
4034 service
= make_number (ntohs (saddr
.in
.sin_port
));
4036 args
[0] = build_string (" <%s:%d>");
4039 caller
= Fformat (3, args
);
4046 Lisp_Object args
[9];
4047 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4049 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4050 for (i
= 0; i
< 8; i
++)
4051 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4052 host
= Fformat (9, args
);
4053 service
= make_number (ntohs (saddr
.in
.sin_port
));
4055 args
[0] = build_string (" <[%s]:%d>");
4058 caller
= Fformat (3, args
);
4063 #ifdef HAVE_LOCAL_SOCKETS
4067 caller
= Fnumber_to_string (make_number (connect_counter
));
4068 caller
= concat3 (build_string (" <*"), caller
, build_string ("*>"));
4072 /* Create a new buffer name for this process if it doesn't have a
4073 filter. The new buffer name is based on the buffer name or
4074 process name of the server process concatenated with the caller
4077 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4081 buffer
= ps
->buffer
;
4083 buffer
= Fbuffer_name (buffer
);
4088 buffer
= concat2 (buffer
, caller
);
4089 buffer
= Fget_buffer_create (buffer
);
4093 /* Generate a unique name for the new server process. Combine the
4094 server process name with the caller identification. */
4096 name
= concat2 (ps
->name
, caller
);
4097 proc
= make_process (name
);
4099 chan_process
[s
] = proc
;
4102 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4105 fcntl (s
, F_SETFL
, O_NDELAY
);
4109 p
= XPROCESS (proc
);
4111 /* Build new contact information for this setup. */
4112 contact
= Fcopy_sequence (ps
->childp
);
4113 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4114 contact
= Fplist_put (contact
, QChost
, host
);
4115 if (!NILP (service
))
4116 contact
= Fplist_put (contact
, QCservice
, service
);
4117 contact
= Fplist_put (contact
, QCremote
,
4118 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4119 #ifdef HAVE_GETSOCKNAME
4121 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4122 contact
= Fplist_put (contact
, QClocal
,
4123 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4126 p
->childp
= contact
;
4127 p
->plist
= Fcopy_sequence (ps
->plist
);
4130 p
->sentinel
= ps
->sentinel
;
4131 p
->filter
= ps
->filter
;
4138 /* Client processes for accepted connections are not stopped initially. */
4139 if (!EQ (p
->filter
, Qt
))
4141 FD_SET (s
, &input_wait_mask
);
4142 FD_SET (s
, &non_keyboard_wait_mask
);
4145 if (s
> max_process_desc
)
4146 max_process_desc
= s
;
4148 /* Setup coding system for new process based on server process.
4149 This seems to be the proper thing to do, as the coding system
4150 of the new process should reflect the settings at the time the
4151 server socket was opened; not the current settings. */
4153 p
->decode_coding_system
= ps
->decode_coding_system
;
4154 p
->encode_coding_system
= ps
->encode_coding_system
;
4155 setup_process_coding_systems (proc
);
4157 p
->decoding_buf
= make_uninit_string (0);
4158 p
->decoding_carryover
= 0;
4159 p
->encoding_buf
= make_uninit_string (0);
4161 p
->inherit_coding_system_flag
4162 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4164 if (!NILP (ps
->log
))
4165 call3 (ps
->log
, server
, proc
,
4166 concat3 (build_string ("accept from "),
4167 (STRINGP (host
) ? host
: build_string ("-")),
4168 build_string ("\n")));
4170 if (!NILP (p
->sentinel
))
4171 exec_sentinel (proc
,
4172 concat3 (build_string ("open from "),
4173 (STRINGP (host
) ? host
: build_string ("-")),
4174 build_string ("\n")));
4177 /* This variable is different from waiting_for_input in keyboard.c.
4178 It is used to communicate to a lisp process-filter/sentinel (via the
4179 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4180 for user-input when that process-filter was called.
4181 waiting_for_input cannot be used as that is by definition 0 when
4182 lisp code is being evalled.
4183 This is also used in record_asynch_buffer_change.
4184 For that purpose, this must be 0
4185 when not inside wait_reading_process_output. */
4186 static int waiting_for_user_input_p
;
4189 wait_reading_process_output_unwind (data
)
4192 waiting_for_user_input_p
= XINT (data
);
4196 /* This is here so breakpoints can be put on it. */
4198 wait_reading_process_output_1 ()
4202 /* Use a wrapper around select to work around a bug in gdb 5.3.
4203 Normally, the wrapper is optimzed away by inlining.
4205 If emacs is stopped inside select, the gdb backtrace doesn't
4206 show the function which called select, so it is practically
4207 impossible to step through wait_reading_process_output. */
4211 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4213 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4216 return select (n
, rfd
, wfd
, xfd
, tmo
);
4218 #define select select_wrapper
4221 /* Read and dispose of subprocess output while waiting for timeout to
4222 elapse and/or keyboard input to be available.
4225 timeout in seconds, or
4226 zero for no limit, or
4227 -1 means gobble data immediately available but don't wait for any.
4230 an additional duration to wait, measured in microseconds.
4231 If this is nonzero and time_limit is 0, then the timeout
4232 consists of MICROSECS only.
4234 READ_KBD is a lisp value:
4235 0 to ignore keyboard input, or
4236 1 to return when input is available, or
4237 -1 meaning caller will actually read the input, so don't throw to
4238 the quit handler, or
4240 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4241 output that arrives.
4243 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4244 (and gobble terminal input into the buffer if any arrives).
4246 If WAIT_PROC is specified, wait until something arrives from that
4247 process. The return value is true if we read some input from
4250 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4251 (suspending output from other processes). A negative value
4252 means don't run any timers either.
4254 If WAIT_PROC is specified, then the function returns true if we
4255 received input from that process before the timeout elapsed.
4256 Otherwise, return true if we received input from any process. */
4259 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4260 wait_for_cell
, wait_proc
, just_wait_proc
)
4261 int time_limit
, microsecs
, read_kbd
, do_display
;
4262 Lisp_Object wait_for_cell
;
4263 struct Lisp_Process
*wait_proc
;
4266 register int channel
, nfds
;
4267 SELECT_TYPE Available
;
4268 #ifdef NON_BLOCKING_CONNECT
4269 SELECT_TYPE Connecting
;
4272 int check_delay
, no_avail
;
4275 EMACS_TIME timeout
, end_time
;
4276 int wait_channel
= -1;
4277 int got_some_input
= 0;
4278 int count
= SPECPDL_INDEX ();
4280 FD_ZERO (&Available
);
4281 #ifdef NON_BLOCKING_CONNECT
4282 FD_ZERO (&Connecting
);
4285 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4286 if (wait_proc
!= NULL
)
4287 wait_channel
= wait_proc
->infd
;
4289 record_unwind_protect (wait_reading_process_output_unwind
,
4290 make_number (waiting_for_user_input_p
));
4291 waiting_for_user_input_p
= read_kbd
;
4293 /* Since we may need to wait several times,
4294 compute the absolute time to return at. */
4295 if (time_limit
|| microsecs
)
4297 EMACS_GET_TIME (end_time
);
4298 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4299 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4301 #ifdef POLL_INTERRUPTED_SYS_CALL
4302 /* AlainF 5-Jul-1996
4303 HP-UX 10.10 seem to have problems with signals coming in
4304 Causes "poll: interrupted system call" messages when Emacs is run
4306 Turn off periodic alarms (in case they are in use),
4307 and then turn off any other atimers. */
4309 turn_on_atimers (0);
4310 #endif /* POLL_INTERRUPTED_SYS_CALL */
4314 int timeout_reduced_for_timers
= 0;
4316 /* If calling from keyboard input, do not quit
4317 since we want to return C-g as an input character.
4318 Otherwise, do pending quit if requested. */
4322 else if (interrupt_input_pending
)
4323 handle_async_input ();
4326 /* Exit now if the cell we're waiting for became non-nil. */
4327 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4330 /* Compute time from now till when time limit is up */
4331 /* Exit if already run out */
4332 if (time_limit
== -1)
4334 /* -1 specified for timeout means
4335 gobble output available now
4336 but don't wait at all. */
4338 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4340 else if (time_limit
|| microsecs
)
4342 EMACS_GET_TIME (timeout
);
4343 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4344 if (EMACS_TIME_NEG_P (timeout
))
4349 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4352 /* Normally we run timers here.
4353 But not if wait_for_cell; in those cases,
4354 the wait is supposed to be short,
4355 and those callers cannot handle running arbitrary Lisp code here. */
4356 if (NILP (wait_for_cell
)
4357 && just_wait_proc
>= 0)
4359 EMACS_TIME timer_delay
;
4363 int old_timers_run
= timers_run
;
4364 struct buffer
*old_buffer
= current_buffer
;
4365 Lisp_Object old_window
= selected_window
;
4367 timer_delay
= timer_check (1);
4369 /* If a timer has run, this might have changed buffers
4370 an alike. Make read_key_sequence aware of that. */
4371 if (timers_run
!= old_timers_run
4372 && (old_buffer
!= current_buffer
4373 || !EQ (old_window
, selected_window
))
4374 && waiting_for_user_input_p
== -1)
4375 record_asynch_buffer_change ();
4377 if (timers_run
!= old_timers_run
&& do_display
)
4378 /* We must retry, since a timer may have requeued itself
4379 and that could alter the time_delay. */
4380 redisplay_preserve_echo_area (9);
4384 while (!detect_input_pending ());
4386 /* If there is unread keyboard input, also return. */
4388 && requeued_events_pending_p ())
4391 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4393 EMACS_TIME difference
;
4394 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4395 if (EMACS_TIME_NEG_P (difference
))
4397 timeout
= timer_delay
;
4398 timeout_reduced_for_timers
= 1;
4401 /* If time_limit is -1, we are not going to wait at all. */
4402 else if (time_limit
!= -1)
4404 /* This is so a breakpoint can be put here. */
4405 wait_reading_process_output_1 ();
4409 /* Cause C-g and alarm signals to take immediate action,
4410 and cause input available signals to zero out timeout.
4412 It is important that we do this before checking for process
4413 activity. If we get a SIGCHLD after the explicit checks for
4414 process activity, timeout is the only way we will know. */
4416 set_waiting_for_input (&timeout
);
4418 /* If status of something has changed, and no input is
4419 available, notify the user of the change right away. After
4420 this explicit check, we'll let the SIGCHLD handler zap
4421 timeout to get our attention. */
4422 if (update_tick
!= process_tick
&& do_display
)
4425 #ifdef NON_BLOCKING_CONNECT
4429 Atemp
= input_wait_mask
;
4431 /* On Mac OS X 10.0, the SELECT system call always says input is
4432 present (for reading) at stdin, even when none is. This
4433 causes the call to SELECT below to return 1 and
4434 status_notify not to be called. As a result output of
4435 subprocesses are incorrectly discarded.
4439 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4441 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4442 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4445 #ifdef NON_BLOCKING_CONNECT
4446 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4450 (SELECT_TYPE
*)0, &timeout
)
4453 /* It's okay for us to do this and then continue with
4454 the loop, since timeout has already been zeroed out. */
4455 clear_waiting_for_input ();
4456 status_notify (NULL
);
4460 /* Don't wait for output from a non-running process. Just
4461 read whatever data has already been received. */
4462 if (wait_proc
&& wait_proc
->raw_status_new
)
4463 update_status (wait_proc
);
4465 && ! EQ (wait_proc
->status
, Qrun
)
4466 && ! EQ (wait_proc
->status
, Qconnect
))
4468 int nread
, total_nread
= 0;
4470 clear_waiting_for_input ();
4471 XSETPROCESS (proc
, wait_proc
);
4473 /* Read data from the process, until we exhaust it. */
4474 while (wait_proc
->infd
>= 0)
4476 nread
= read_process_output (proc
, wait_proc
->infd
);
4482 total_nread
+= nread
;
4484 else if (nread
== -1 && EIO
== errno
)
4488 else if (nread
== -1 && EAGAIN
== errno
)
4492 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4496 if (total_nread
> 0 && do_display
)
4497 redisplay_preserve_echo_area (10);
4502 /* Wait till there is something to do */
4504 if (wait_proc
&& just_wait_proc
)
4506 if (wait_proc
->infd
< 0) /* Terminated */
4508 FD_SET (wait_proc
->infd
, &Available
);
4510 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4512 else if (!NILP (wait_for_cell
))
4514 Available
= non_process_wait_mask
;
4516 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4521 Available
= non_keyboard_wait_mask
;
4523 Available
= input_wait_mask
;
4524 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4525 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4528 /* If frame size has changed or the window is newly mapped,
4529 redisplay now, before we start to wait. There is a race
4530 condition here; if a SIGIO arrives between now and the select
4531 and indicates that a frame is trashed, the select may block
4532 displaying a trashed screen. */
4533 if (frame_garbaged
&& do_display
)
4535 clear_waiting_for_input ();
4536 redisplay_preserve_echo_area (11);
4538 set_waiting_for_input (&timeout
);
4542 if (read_kbd
&& detect_input_pending ())
4549 #ifdef NON_BLOCKING_CONNECT
4551 Connecting
= connect_wait_mask
;
4554 #ifdef ADAPTIVE_READ_BUFFERING
4555 /* Set the timeout for adaptive read buffering if any
4556 process has non-zero read_output_skip and non-zero
4557 read_output_delay, and we are not reading output for a
4558 specific wait_channel. It is not executed if
4559 Vprocess_adaptive_read_buffering is nil. */
4560 if (process_output_skip
&& check_delay
> 0)
4562 int usecs
= EMACS_USECS (timeout
);
4563 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4564 usecs
= READ_OUTPUT_DELAY_MAX
;
4565 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4567 proc
= chan_process
[channel
];
4570 /* Find minimum non-zero read_output_delay among the
4571 processes with non-zero read_output_skip. */
4572 if (XPROCESS (proc
)->read_output_delay
> 0)
4575 if (!XPROCESS (proc
)->read_output_skip
)
4577 FD_CLR (channel
, &Available
);
4578 XPROCESS (proc
)->read_output_skip
= 0;
4579 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4580 usecs
= XPROCESS (proc
)->read_output_delay
;
4583 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4584 process_output_skip
= 0;
4588 nfds
= select (max (max (max_process_desc
, max_keyboard_desc
),
4591 #ifdef NON_BLOCKING_CONNECT
4592 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4596 (SELECT_TYPE
*)0, &timeout
);
4601 /* Make C-g and alarm signals set flags again */
4602 clear_waiting_for_input ();
4604 /* If we woke up due to SIGWINCH, actually change size now. */
4605 do_pending_window_change (0);
4607 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4608 /* We wanted the full specified time, so return now. */
4612 if (xerrno
== EINTR
)
4615 /* Ultrix select seems to return ENOMEM when it is
4616 interrupted. Treat it just like EINTR. Bleah. Note
4617 that we want to test for the "ultrix" CPP symbol, not
4618 "__ultrix__"; the latter is only defined under GCC, but
4619 not by DEC's bundled CC. -JimB */
4620 else if (xerrno
== ENOMEM
)
4623 else if (xerrno
== EBADF
)
4626 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4627 the child's closure of the pts gives the parent a SIGHUP, and
4628 the ptc file descriptor is automatically closed,
4629 yielding EBADF here or at select() call above.
4630 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4631 in m/ibmrt-aix.h), and here we just ignore the select error.
4632 Cleanup occurs c/o status_notify after SIGCLD. */
4633 no_avail
= 1; /* Cannot depend on values returned */
4639 error ("select error: %s", emacs_strerror (xerrno
));
4644 FD_ZERO (&Available
);
4645 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4648 #if defined(sun) && !defined(USG5_4)
4649 if (nfds
> 0 && keyboard_bit_set (&Available
)
4651 /* System sometimes fails to deliver SIGIO.
4653 David J. Mackenzie says that Emacs doesn't compile under
4654 Solaris if this code is enabled, thus the USG5_4 in the CPP
4655 conditional. "I haven't noticed any ill effects so far.
4656 If you find a Solaris expert somewhere, they might know
4658 kill (getpid (), SIGIO
);
4661 #if 0 /* When polling is used, interrupt_input is 0,
4662 so get_input_pending should read the input.
4663 So this should not be needed. */
4664 /* If we are using polling for input,
4665 and we see input available, make it get read now.
4666 Otherwise it might not actually get read for a second.
4667 And on hpux, since we turn off polling in wait_reading_process_output,
4668 it might never get read at all if we don't spend much time
4669 outside of wait_reading_process_output. */
4670 if (read_kbd
&& interrupt_input
4671 && keyboard_bit_set (&Available
)
4672 && input_polling_used ())
4673 kill (getpid (), SIGALRM
);
4676 /* Check for keyboard input */
4677 /* If there is any, return immediately
4678 to give it higher priority than subprocesses */
4682 int old_timers_run
= timers_run
;
4683 struct buffer
*old_buffer
= current_buffer
;
4684 Lisp_Object old_window
= selected_window
;
4687 if (detect_input_pending_run_timers (do_display
))
4689 swallow_events (do_display
);
4690 if (detect_input_pending_run_timers (do_display
))
4694 /* If a timer has run, this might have changed buffers
4695 an alike. Make read_key_sequence aware of that. */
4696 if (timers_run
!= old_timers_run
4697 && waiting_for_user_input_p
== -1
4698 && (old_buffer
!= current_buffer
4699 || !EQ (old_window
, selected_window
)))
4700 record_asynch_buffer_change ();
4706 /* If there is unread keyboard input, also return. */
4708 && requeued_events_pending_p ())
4711 /* If we are not checking for keyboard input now,
4712 do process events (but don't run any timers).
4713 This is so that X events will be processed.
4714 Otherwise they may have to wait until polling takes place.
4715 That would causes delays in pasting selections, for example.
4717 (We used to do this only if wait_for_cell.) */
4718 if (read_kbd
== 0 && detect_input_pending ())
4720 swallow_events (do_display
);
4721 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4722 if (detect_input_pending ())
4727 /* Exit now if the cell we're waiting for became non-nil. */
4728 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4732 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4733 go read it. This can happen with X on BSD after logging out.
4734 In that case, there really is no input and no SIGIO,
4735 but select says there is input. */
4737 if (read_kbd
&& interrupt_input
4738 && keyboard_bit_set (&Available
) && ! noninteractive
)
4739 kill (getpid (), SIGIO
);
4743 got_some_input
|= nfds
> 0;
4745 /* If checking input just got us a size-change event from X,
4746 obey it now if we should. */
4747 if (read_kbd
|| ! NILP (wait_for_cell
))
4748 do_pending_window_change (0);
4750 /* Check for data from a process. */
4751 if (no_avail
|| nfds
== 0)
4754 /* Really FIRST_PROC_DESC should be 0 on Unix,
4755 but this is safer in the short run. */
4756 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4758 if (FD_ISSET (channel
, &Available
)
4759 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
4763 /* If waiting for this channel, arrange to return as
4764 soon as no more input to be processed. No more
4766 if (wait_channel
== channel
)
4772 proc
= chan_process
[channel
];
4776 /* If this is a server stream socket, accept connection. */
4777 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4779 server_accept_connection (proc
, channel
);
4783 /* Read data from the process, starting with our
4784 buffered-ahead character if we have one. */
4786 nread
= read_process_output (proc
, channel
);
4789 /* Since read_process_output can run a filter,
4790 which can call accept-process-output,
4791 don't try to read from any other processes
4792 before doing the select again. */
4793 FD_ZERO (&Available
);
4796 redisplay_preserve_echo_area (12);
4799 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4802 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4803 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4805 else if (nread
== -1 && errno
== EAGAIN
)
4809 else if (nread
== -1 && errno
== EAGAIN
)
4811 /* Note that we cannot distinguish between no input
4812 available now and a closed pipe.
4813 With luck, a closed pipe will be accompanied by
4814 subprocess termination and SIGCHLD. */
4815 else if (nread
== 0 && !NETCONN_P (proc
))
4817 #endif /* O_NDELAY */
4818 #endif /* O_NONBLOCK */
4820 /* On some OSs with ptys, when the process on one end of
4821 a pty exits, the other end gets an error reading with
4822 errno = EIO instead of getting an EOF (0 bytes read).
4823 Therefore, if we get an error reading and errno =
4824 EIO, just continue, because the child process has
4825 exited and should clean itself up soon (e.g. when we
4828 However, it has been known to happen that the SIGCHLD
4829 got lost. So raise the signal again just in case.
4831 else if (nread
== -1 && errno
== EIO
)
4833 /* Clear the descriptor now, so we only raise the signal once. */
4834 FD_CLR (channel
, &input_wait_mask
);
4835 FD_CLR (channel
, &non_keyboard_wait_mask
);
4837 kill (getpid (), SIGCHLD
);
4839 #endif /* HAVE_PTYS */
4840 /* If we can detect process termination, don't consider the process
4841 gone just because its pipe is closed. */
4843 else if (nread
== 0 && !NETCONN_P (proc
))
4848 /* Preserve status of processes already terminated. */
4849 XPROCESS (proc
)->tick
= ++process_tick
;
4850 deactivate_process (proc
);
4851 if (XPROCESS (proc
)->raw_status_new
)
4852 update_status (XPROCESS (proc
));
4853 if (EQ (XPROCESS (proc
)->status
, Qrun
))
4854 XPROCESS (proc
)->status
4855 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
4858 #ifdef NON_BLOCKING_CONNECT
4859 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
4860 && FD_ISSET (channel
, &connect_wait_mask
))
4862 struct Lisp_Process
*p
;
4864 FD_CLR (channel
, &connect_wait_mask
);
4865 if (--num_pending_connects
< 0)
4868 proc
= chan_process
[channel
];
4872 p
= XPROCESS (proc
);
4875 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
4876 So only use it on systems where it is known to work. */
4878 int xlen
= sizeof(xerrno
);
4879 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
4884 struct sockaddr pname
;
4885 int pnamelen
= sizeof(pname
);
4887 /* If connection failed, getpeername will fail. */
4889 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
4891 /* Obtain connect failure code through error slippage. */
4894 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
4901 p
->tick
= ++process_tick
;
4902 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
4903 deactivate_process (proc
);
4908 /* Execute the sentinel here. If we had relied on
4909 status_notify to do it later, it will read input
4910 from the process before calling the sentinel. */
4911 exec_sentinel (proc
, build_string ("open\n"));
4912 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
4914 FD_SET (p
->infd
, &input_wait_mask
);
4915 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
4919 #endif /* NON_BLOCKING_CONNECT */
4920 } /* end for each file descriptor */
4921 } /* end while exit conditions not met */
4923 unbind_to (count
, Qnil
);
4925 /* If calling from keyboard input, do not quit
4926 since we want to return C-g as an input character.
4927 Otherwise, do pending quit if requested. */
4930 /* Prevent input_pending from remaining set if we quit. */
4931 clear_input_pending ();
4934 #ifdef POLL_INTERRUPTED_SYS_CALL
4935 /* AlainF 5-Jul-1996
4936 HP-UX 10.10 seems to have problems with signals coming in
4937 Causes "poll: interrupted system call" messages when Emacs is run
4939 Turn periodic alarms back on */
4941 #endif /* POLL_INTERRUPTED_SYS_CALL */
4943 return got_some_input
;
4946 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
4949 read_process_output_call (fun_and_args
)
4950 Lisp_Object fun_and_args
;
4952 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
4956 read_process_output_error_handler (error
)
4959 cmd_error_internal (error
, "error in process filter: ");
4961 update_echo_area ();
4962 Fsleep_for (make_number (2), Qnil
);
4966 /* Read pending output from the process channel,
4967 starting with our buffered-ahead character if we have one.
4968 Yield number of decoded characters read.
4970 This function reads at most 4096 characters.
4971 If you want to read all available subprocess output,
4972 you must call it repeatedly until it returns zero.
4974 The characters read are decoded according to PROC's coding-system
4978 read_process_output (proc
, channel
)
4980 register int channel
;
4982 register int nbytes
;
4984 register Lisp_Object outstream
;
4985 register struct buffer
*old
= current_buffer
;
4986 register struct Lisp_Process
*p
= XPROCESS (proc
);
4987 register int opoint
;
4988 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
4989 int carryover
= p
->decoding_carryover
;
4993 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
4995 vs
= get_vms_process_pointer (p
->pid
);
4999 return (0); /* Really weird if it does this */
5000 if (!(vs
->iosb
[0] & 1))
5001 return -1; /* I/O error */
5004 error ("Could not get VMS process pointer");
5005 chars
= vs
->inputBuffer
;
5006 nbytes
= clean_vms_buffer (chars
, vs
->iosb
[1]);
5009 start_vms_process_read (vs
); /* Crank up the next read on the process */
5010 return 1; /* Nothing worth printing, say we got 1 */
5014 /* The data carried over in the previous decoding (which are at
5015 the tail of decoding buffer) should be prepended to the new
5016 data read to decode all together. */
5017 chars
= (char *) alloca (nbytes
+ carryover
);
5018 bcopy (SDATA (p
->decoding_buf
), buf
, carryover
);
5019 bcopy (vs
->inputBuffer
, chars
+ carryover
, nbytes
);
5023 chars
= (char *) alloca (carryover
+ readmax
);
5025 /* See the comment above. */
5026 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5028 #ifdef DATAGRAM_SOCKETS
5029 /* We have a working select, so proc_buffered_char is always -1. */
5030 if (DATAGRAM_CHAN_P (channel
))
5032 int len
= datagram_address
[channel
].len
;
5033 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5034 0, datagram_address
[channel
].sa
, &len
);
5038 if (proc_buffered_char
[channel
] < 0)
5040 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5041 #ifdef ADAPTIVE_READ_BUFFERING
5042 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5044 int delay
= p
->read_output_delay
;
5047 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5050 process_output_delay_count
++;
5051 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5054 else if (delay
> 0 && (nbytes
== readmax
))
5056 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5058 process_output_delay_count
--;
5060 p
->read_output_delay
= delay
;
5063 p
->read_output_skip
= 1;
5064 process_output_skip
= 1;
5071 chars
[carryover
] = proc_buffered_char
[channel
];
5072 proc_buffered_char
[channel
] = -1;
5073 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5077 nbytes
= nbytes
+ 1;
5079 #endif /* not VMS */
5081 p
->decoding_carryover
= 0;
5083 /* At this point, NBYTES holds number of bytes just received
5084 (including the one in proc_buffered_char[channel]). */
5087 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5089 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5092 /* Now set NBYTES how many bytes we must decode. */
5093 nbytes
+= carryover
;
5095 /* Read and dispose of the process output. */
5096 outstream
= p
->filter
;
5097 if (!NILP (outstream
))
5099 /* We inhibit quit here instead of just catching it so that
5100 hitting ^G when a filter happens to be running won't screw
5102 int count
= SPECPDL_INDEX ();
5103 Lisp_Object odeactivate
;
5104 Lisp_Object obuffer
, okeymap
;
5106 int outer_running_asynch_code
= running_asynch_code
;
5107 int waiting
= waiting_for_user_input_p
;
5109 /* No need to gcpro these, because all we do with them later
5110 is test them for EQness, and none of them should be a string. */
5111 odeactivate
= Vdeactivate_mark
;
5112 XSETBUFFER (obuffer
, current_buffer
);
5113 okeymap
= current_buffer
->keymap
;
5115 specbind (Qinhibit_quit
, Qt
);
5116 specbind (Qlast_nonmenu_event
, Qt
);
5118 /* In case we get recursively called,
5119 and we already saved the match data nonrecursively,
5120 save the same match data in safely recursive fashion. */
5121 if (outer_running_asynch_code
)
5124 /* Don't clobber the CURRENT match data, either! */
5125 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5126 restore_search_regs ();
5127 record_unwind_save_match_data ();
5128 Fset_match_data (tem
, Qt
);
5131 /* For speed, if a search happens within this code,
5132 save the match data in a special nonrecursive fashion. */
5133 running_asynch_code
= 1;
5135 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5136 text
= coding
->dst_object
;
5137 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5138 /* A new coding system might be found. */
5139 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5141 p
->decode_coding_system
= Vlast_coding_system_used
;
5143 /* Don't call setup_coding_system for
5144 proc_decode_coding_system[channel] here. It is done in
5145 detect_coding called via decode_coding above. */
5147 /* If a coding system for encoding is not yet decided, we set
5148 it as the same as coding-system for decoding.
5150 But, before doing that we must check if
5151 proc_encode_coding_system[p->outfd] surely points to a
5152 valid memory because p->outfd will be changed once EOF is
5153 sent to the process. */
5154 if (NILP (p
->encode_coding_system
)
5155 && proc_encode_coding_system
[p
->outfd
])
5157 p
->encode_coding_system
5158 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5159 setup_coding_system (p
->encode_coding_system
,
5160 proc_encode_coding_system
[p
->outfd
]);
5164 if (coding
->carryover_bytes
> 0)
5166 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5167 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5168 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5169 coding
->carryover_bytes
);
5170 p
->decoding_carryover
= coding
->carryover_bytes
;
5172 /* Adjust the multibyteness of TEXT to that of the filter. */
5173 if (!p
->filter_multibyte
!= !STRING_MULTIBYTE (text
))
5174 text
= (STRING_MULTIBYTE (text
)
5175 ? Fstring_as_unibyte (text
)
5176 : Fstring_to_multibyte (text
));
5177 if (SBYTES (text
) > 0)
5178 internal_condition_case_1 (read_process_output_call
,
5180 Fcons (proc
, Fcons (text
, Qnil
))),
5181 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5182 read_process_output_error_handler
);
5184 /* If we saved the match data nonrecursively, restore it now. */
5185 restore_search_regs ();
5186 running_asynch_code
= outer_running_asynch_code
;
5188 /* Handling the process output should not deactivate the mark. */
5189 Vdeactivate_mark
= odeactivate
;
5191 /* Restore waiting_for_user_input_p as it was
5192 when we were called, in case the filter clobbered it. */
5193 waiting_for_user_input_p
= waiting
;
5195 #if 0 /* Call record_asynch_buffer_change unconditionally,
5196 because we might have changed minor modes or other things
5197 that affect key bindings. */
5198 if (! EQ (Fcurrent_buffer (), obuffer
)
5199 || ! EQ (current_buffer
->keymap
, okeymap
))
5201 /* But do it only if the caller is actually going to read events.
5202 Otherwise there's no need to make him wake up, and it could
5203 cause trouble (for example it would make sit_for return). */
5204 if (waiting_for_user_input_p
== -1)
5205 record_asynch_buffer_change ();
5208 start_vms_process_read (vs
);
5210 unbind_to (count
, Qnil
);
5214 /* If no filter, write into buffer if it isn't dead. */
5215 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5217 Lisp_Object old_read_only
;
5218 int old_begv
, old_zv
;
5219 int old_begv_byte
, old_zv_byte
;
5220 Lisp_Object odeactivate
;
5221 int before
, before_byte
;
5226 odeactivate
= Vdeactivate_mark
;
5228 Fset_buffer (p
->buffer
);
5230 opoint_byte
= PT_BYTE
;
5231 old_read_only
= current_buffer
->read_only
;
5234 old_begv_byte
= BEGV_BYTE
;
5235 old_zv_byte
= ZV_BYTE
;
5237 current_buffer
->read_only
= Qnil
;
5239 /* Insert new output into buffer
5240 at the current end-of-output marker,
5241 thus preserving logical ordering of input and output. */
5242 if (XMARKER (p
->mark
)->buffer
)
5243 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5244 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5247 SET_PT_BOTH (ZV
, ZV_BYTE
);
5249 before_byte
= PT_BYTE
;
5251 /* If the output marker is outside of the visible region, save
5252 the restriction and widen. */
5253 if (! (BEGV
<= PT
&& PT
<= ZV
))
5256 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5257 text
= coding
->dst_object
;
5258 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5259 /* A new coding system might be found. See the comment in the
5260 similar code in the previous `if' block. */
5261 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5263 p
->decode_coding_system
= Vlast_coding_system_used
;
5264 if (NILP (p
->encode_coding_system
)
5265 && proc_encode_coding_system
[p
->outfd
])
5267 p
->encode_coding_system
5268 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5269 setup_coding_system (p
->encode_coding_system
,
5270 proc_encode_coding_system
[p
->outfd
]);
5273 if (coding
->carryover_bytes
> 0)
5275 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5276 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5277 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5278 coding
->carryover_bytes
);
5279 p
->decoding_carryover
= coding
->carryover_bytes
;
5281 /* Adjust the multibyteness of TEXT to that of the buffer. */
5282 if (NILP (current_buffer
->enable_multibyte_characters
)
5283 != ! STRING_MULTIBYTE (text
))
5284 text
= (STRING_MULTIBYTE (text
)
5285 ? Fstring_as_unibyte (text
)
5286 : Fstring_to_multibyte (text
));
5287 /* Insert before markers in case we are inserting where
5288 the buffer's mark is, and the user's next command is Meta-y. */
5289 insert_from_string_before_markers (text
, 0, 0,
5290 SCHARS (text
), SBYTES (text
), 0);
5292 /* Make sure the process marker's position is valid when the
5293 process buffer is changed in the signal_after_change above.
5294 W3 is known to do that. */
5295 if (BUFFERP (p
->buffer
)
5296 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5297 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5299 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5301 update_mode_lines
++;
5303 /* Make sure opoint and the old restrictions
5304 float ahead of any new text just as point would. */
5305 if (opoint
>= before
)
5307 opoint
+= PT
- before
;
5308 opoint_byte
+= PT_BYTE
- before_byte
;
5310 if (old_begv
> before
)
5312 old_begv
+= PT
- before
;
5313 old_begv_byte
+= PT_BYTE
- before_byte
;
5315 if (old_zv
>= before
)
5317 old_zv
+= PT
- before
;
5318 old_zv_byte
+= PT_BYTE
- before_byte
;
5321 /* If the restriction isn't what it should be, set it. */
5322 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5323 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5325 /* Handling the process output should not deactivate the mark. */
5326 Vdeactivate_mark
= odeactivate
;
5328 current_buffer
->read_only
= old_read_only
;
5329 SET_PT_BOTH (opoint
, opoint_byte
);
5330 set_buffer_internal (old
);
5333 start_vms_process_read (vs
);
5338 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5340 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5341 This is intended for use by asynchronous process output filters and sentinels. */)
5344 return (waiting_for_user_input_p
? Qt
: Qnil
);
5347 /* Sending data to subprocess */
5349 jmp_buf send_process_frame
;
5350 Lisp_Object process_sent_to
;
5353 send_process_trap ()
5355 SIGNAL_THREAD_CHECK (SIGPIPE
);
5360 sigunblock (sigmask (SIGPIPE
));
5361 longjmp (send_process_frame
, 1);
5364 /* Send some data to process PROC.
5365 BUF is the beginning of the data; LEN is the number of characters.
5366 OBJECT is the Lisp object that the data comes from. If OBJECT is
5367 nil or t, it means that the data comes from C string.
5369 If OBJECT is not nil, the data is encoded by PROC's coding-system
5370 for encoding before it is sent.
5372 This function can evaluate Lisp code and can garbage collect. */
5375 send_process (proc
, buf
, len
, object
)
5376 volatile Lisp_Object proc
;
5377 unsigned char *volatile buf
;
5379 volatile Lisp_Object object
;
5381 /* Use volatile to protect variables from being clobbered by longjmp. */
5382 struct Lisp_Process
*p
= XPROCESS (proc
);
5384 struct coding_system
*coding
;
5385 struct gcpro gcpro1
;
5386 SIGTYPE (*volatile old_sigpipe
) ();
5391 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5394 if (p
->raw_status_new
)
5396 if (! EQ (p
->status
, Qrun
))
5397 error ("Process %s not running", SDATA (p
->name
));
5399 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5401 coding
= proc_encode_coding_system
[p
->outfd
];
5402 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5404 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5405 || (BUFFERP (object
)
5406 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5409 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5410 /* The coding system for encoding was changed to raw-text
5411 because we sent a unibyte text previously. Now we are
5412 sending a multibyte text, thus we must encode it by the
5413 original coding system specified for the current process. */
5414 setup_coding_system (p
->encode_coding_system
, coding
);
5415 coding
->src_multibyte
= 1;
5419 /* For sending a unibyte text, character code conversion should
5420 not take place but EOL conversion should. So, setup raw-text
5421 or one of the subsidiary if we have not yet done it. */
5422 if (CODING_REQUIRE_ENCODING (coding
))
5424 if (CODING_REQUIRE_FLUSHING (coding
))
5426 /* But, before changing the coding, we must flush out data. */
5427 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5428 send_process (proc
, "", 0, Qt
);
5429 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5431 setup_coding_system (raw_text_coding_system
5432 (Vlast_coding_system_used
),
5434 coding
->src_multibyte
= 0;
5437 coding
->dst_multibyte
= 0;
5439 if (CODING_REQUIRE_ENCODING (coding
))
5441 coding
->dst_object
= Qt
;
5442 if (BUFFERP (object
))
5444 int from_byte
, from
, to
;
5445 int save_pt
, save_pt_byte
;
5446 struct buffer
*cur
= current_buffer
;
5448 set_buffer_internal (XBUFFER (object
));
5449 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5451 from_byte
= PTR_BYTE_POS (buf
);
5452 from
= BYTE_TO_CHAR (from_byte
);
5453 to
= BYTE_TO_CHAR (from_byte
+ len
);
5454 TEMP_SET_PT_BOTH (from
, from_byte
);
5455 encode_coding_object (coding
, object
, from
, from_byte
,
5456 to
, from_byte
+ len
, Qt
);
5457 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5458 set_buffer_internal (cur
);
5460 else if (STRINGP (object
))
5462 encode_coding_string (coding
, object
, 1);
5466 coding
->dst_object
= make_unibyte_string (buf
, len
);
5467 coding
->produced
= len
;
5470 len
= coding
->produced
;
5471 buf
= SDATA (coding
->dst_object
);
5475 vs
= get_vms_process_pointer (p
->pid
);
5477 error ("Could not find this process: %x", p
->pid
);
5478 else if (write_to_vms_process (vs
, buf
, len
))
5482 if (pty_max_bytes
== 0)
5484 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5485 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5486 if (pty_max_bytes
< 0)
5487 pty_max_bytes
= 250;
5489 pty_max_bytes
= 250;
5491 /* Deduct one, to leave space for the eof. */
5495 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5496 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5497 when returning with longjmp despite being declared volatile. */
5498 if (!setjmp (send_process_frame
))
5500 process_sent_to
= proc
;
5505 /* Decide how much data we can send in one batch.
5506 Long lines need to be split into multiple batches. */
5509 /* Starting this at zero is always correct when not the first
5510 iteration because the previous iteration ended by sending C-d.
5511 It may not be correct for the first iteration
5512 if a partial line was sent in a separate send_process call.
5513 If that proves worth handling, we need to save linepos
5514 in the process object. */
5516 unsigned char *ptr
= (unsigned char *) buf
;
5517 unsigned char *end
= (unsigned char *) buf
+ len
;
5519 /* Scan through this text for a line that is too long. */
5520 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5528 /* If we found one, break the line there
5529 and put in a C-d to force the buffer through. */
5533 /* Send this batch, using one or more write calls. */
5536 int outfd
= p
->outfd
;
5537 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5538 #ifdef DATAGRAM_SOCKETS
5539 if (DATAGRAM_CHAN_P (outfd
))
5541 rv
= sendto (outfd
, (char *) buf
, this,
5542 0, datagram_address
[outfd
].sa
,
5543 datagram_address
[outfd
].len
);
5544 if (rv
< 0 && errno
== EMSGSIZE
)
5546 signal (SIGPIPE
, old_sigpipe
);
5547 report_file_error ("sending datagram",
5548 Fcons (proc
, Qnil
));
5554 rv
= emacs_write (outfd
, (char *) buf
, this);
5555 #ifdef ADAPTIVE_READ_BUFFERING
5556 if (p
->read_output_delay
> 0
5557 && p
->adaptive_read_buffering
== 1)
5559 p
->read_output_delay
= 0;
5560 process_output_delay_count
--;
5561 p
->read_output_skip
= 0;
5565 signal (SIGPIPE
, old_sigpipe
);
5571 || errno
== EWOULDBLOCK
5577 /* Buffer is full. Wait, accepting input;
5578 that may allow the program
5579 to finish doing output and read more. */
5583 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5584 /* A gross hack to work around a bug in FreeBSD.
5585 In the following sequence, read(2) returns
5589 write(2) 954 bytes, get EAGAIN
5590 read(2) 1024 bytes in process_read_output
5591 read(2) 11 bytes in process_read_output
5593 That is, read(2) returns more bytes than have
5594 ever been written successfully. The 1033 bytes
5595 read are the 1022 bytes written successfully
5596 after processing (for example with CRs added if
5597 the terminal is set up that way which it is
5598 here). The same bytes will be seen again in a
5599 later read(2), without the CRs. */
5601 if (errno
== EAGAIN
)
5604 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5606 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5608 /* Running filters might relocate buffers or strings.
5609 Arrange to relocate BUF. */
5610 if (BUFFERP (object
))
5611 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5612 else if (STRINGP (object
))
5613 offset
= buf
- SDATA (object
);
5615 #ifdef EMACS_HAS_USECS
5616 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5618 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5621 if (BUFFERP (object
))
5622 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5623 else if (STRINGP (object
))
5624 buf
= offset
+ SDATA (object
);
5629 /* This is a real error. */
5630 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5637 /* If we sent just part of the string, put in an EOF
5638 to force it through, before we send the rest. */
5640 Fprocess_send_eof (proc
);
5643 #endif /* not VMS */
5646 signal (SIGPIPE
, old_sigpipe
);
5648 proc
= process_sent_to
;
5649 p
= XPROCESS (proc
);
5651 p
->raw_status_new
= 0;
5652 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5653 p
->tick
= ++process_tick
;
5654 deactivate_process (proc
);
5656 error ("Error writing to process %s; closed it", SDATA (p
->name
));
5658 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5665 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5667 doc
: /* Send current contents of region as input to PROCESS.
5668 PROCESS may be a process, a buffer, the name of a process or buffer, or
5669 nil, indicating the current buffer's process.
5670 Called from program, takes three arguments, PROCESS, START and END.
5671 If the region is more than 500 characters long,
5672 it is sent in several bunches. This may happen even for shorter regions.
5673 Output from processes can arrive in between bunches. */)
5674 (process
, start
, end
)
5675 Lisp_Object process
, start
, end
;
5680 proc
= get_process (process
);
5681 validate_region (&start
, &end
);
5683 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5684 move_gap (XINT (start
));
5686 start1
= CHAR_TO_BYTE (XINT (start
));
5687 end1
= CHAR_TO_BYTE (XINT (end
));
5688 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5689 Fcurrent_buffer ());
5694 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5696 doc
: /* Send PROCESS the contents of STRING as input.
5697 PROCESS may be a process, a buffer, the name of a process or buffer, or
5698 nil, indicating the current buffer's process.
5699 If STRING is more than 500 characters long,
5700 it is sent in several bunches. This may happen even for shorter strings.
5701 Output from processes can arrive in between bunches. */)
5703 Lisp_Object process
, string
;
5706 CHECK_STRING (string
);
5707 proc
= get_process (process
);
5708 send_process (proc
, SDATA (string
),
5709 SBYTES (string
), string
);
5713 /* Return the foreground process group for the tty/pty that
5714 the process P uses. */
5716 emacs_get_tty_pgrp (p
)
5717 struct Lisp_Process
*p
;
5722 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5725 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5726 master side. Try the slave side. */
5727 fd
= emacs_open (XSTRING (p
->tty_name
)->data
, O_RDONLY
, 0);
5731 ioctl (fd
, TIOCGPGRP
, &gid
);
5735 #endif /* defined (TIOCGPGRP ) */
5740 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5741 Sprocess_running_child_p
, 0, 1, 0,
5742 doc
: /* Return t if PROCESS has given the terminal to a child.
5743 If the operating system does not make it possible to find out,
5744 return t unconditionally. */)
5746 Lisp_Object process
;
5748 /* Initialize in case ioctl doesn't exist or gives an error,
5749 in a way that will cause returning t. */
5752 struct Lisp_Process
*p
;
5754 proc
= get_process (process
);
5755 p
= XPROCESS (proc
);
5757 if (!EQ (p
->childp
, Qt
))
5758 error ("Process %s is not a subprocess",
5761 error ("Process %s is not active",
5764 gid
= emacs_get_tty_pgrp (p
);
5771 /* send a signal number SIGNO to PROCESS.
5772 If CURRENT_GROUP is t, that means send to the process group
5773 that currently owns the terminal being used to communicate with PROCESS.
5774 This is used for various commands in shell mode.
5775 If CURRENT_GROUP is lambda, that means send to the process group
5776 that currently owns the terminal, but only if it is NOT the shell itself.
5778 If NOMSG is zero, insert signal-announcements into process's buffers
5781 If we can, we try to signal PROCESS by sending control characters
5782 down the pty. This allows us to signal inferiors who have changed
5783 their uid, for which killpg would return an EPERM error. */
5786 process_send_signal (process
, signo
, current_group
, nomsg
)
5787 Lisp_Object process
;
5789 Lisp_Object current_group
;
5793 register struct Lisp_Process
*p
;
5797 proc
= get_process (process
);
5798 p
= XPROCESS (proc
);
5800 if (!EQ (p
->childp
, Qt
))
5801 error ("Process %s is not a subprocess",
5804 error ("Process %s is not active",
5808 current_group
= Qnil
;
5810 /* If we are using pgrps, get a pgrp number and make it negative. */
5811 if (NILP (current_group
))
5812 /* Send the signal to the shell's process group. */
5816 #ifdef SIGNALS_VIA_CHARACTERS
5817 /* If possible, send signals to the entire pgrp
5818 by sending an input character to it. */
5820 /* TERMIOS is the latest and bestest, and seems most likely to
5821 work. If the system has it, use it. */
5824 cc_t
*sig_char
= NULL
;
5826 tcgetattr (p
->infd
, &t
);
5831 sig_char
= &t
.c_cc
[VINTR
];
5835 sig_char
= &t
.c_cc
[VQUIT
];
5839 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5840 sig_char
= &t
.c_cc
[VSWTCH
];
5842 sig_char
= &t
.c_cc
[VSUSP
];
5847 if (sig_char
&& *sig_char
!= CDISABLE
)
5849 send_process (proc
, sig_char
, 1, Qnil
);
5852 /* If we can't send the signal with a character,
5853 fall through and send it another way. */
5854 #else /* ! HAVE_TERMIOS */
5856 /* On Berkeley descendants, the following IOCTL's retrieve the
5857 current control characters. */
5858 #if defined (TIOCGLTC) && defined (TIOCGETC)
5866 ioctl (p
->infd
, TIOCGETC
, &c
);
5867 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
5870 ioctl (p
->infd
, TIOCGETC
, &c
);
5871 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
5875 ioctl (p
->infd
, TIOCGLTC
, &lc
);
5876 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
5878 #endif /* ! defined (SIGTSTP) */
5881 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5883 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
5890 ioctl (p
->infd
, TCGETA
, &t
);
5891 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
5894 ioctl (p
->infd
, TCGETA
, &t
);
5895 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
5899 ioctl (p
->infd
, TCGETA
, &t
);
5900 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
5902 #endif /* ! defined (SIGTSTP) */
5904 #else /* ! defined (TCGETA) */
5905 Your configuration files are messed up
.
5906 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
5907 you'd better be using one of the alternatives above! */
5908 #endif /* ! defined (TCGETA) */
5909 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5910 /* In this case, the code above should alway returns. */
5912 #endif /* ! defined HAVE_TERMIOS */
5914 /* The code above may fall through if it can't
5915 handle the signal. */
5916 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5919 /* Get the current pgrp using the tty itself, if we have that.
5920 Otherwise, use the pty to get the pgrp.
5921 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5922 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5923 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5924 His patch indicates that if TIOCGPGRP returns an error, then
5925 we should just assume that p->pid is also the process group id. */
5927 gid
= emacs_get_tty_pgrp (p
);
5930 /* If we can't get the information, assume
5931 the shell owns the tty. */
5934 /* It is not clear whether anything really can set GID to -1.
5935 Perhaps on some system one of those ioctls can or could do so.
5936 Or perhaps this is vestigial. */
5939 #else /* ! defined (TIOCGPGRP ) */
5940 /* Can't select pgrps on this system, so we know that
5941 the child itself heads the pgrp. */
5943 #endif /* ! defined (TIOCGPGRP ) */
5945 /* If current_group is lambda, and the shell owns the terminal,
5946 don't send any signal. */
5947 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5955 p
->raw_status_new
= 0;
5957 p
->tick
= ++process_tick
;
5959 status_notify (NULL
);
5961 #endif /* ! defined (SIGCONT) */
5964 send_process (proc
, "\003", 1, Qnil
); /* ^C */
5969 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
5974 sys$
forcex (&(p
->pid
), 0, 1);
5977 flush_pending_output (p
->infd
);
5981 /* If we don't have process groups, send the signal to the immediate
5982 subprocess. That isn't really right, but it's better than any
5983 obvious alternative. */
5986 kill (p
->pid
, signo
);
5990 /* gid may be a pid, or minus a pgrp's number */
5992 if (!NILP (current_group
))
5994 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
5995 EMACS_KILLPG (gid
, signo
);
6002 #else /* ! defined (TIOCSIGSEND) */
6003 EMACS_KILLPG (gid
, signo
);
6004 #endif /* ! defined (TIOCSIGSEND) */
6007 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6008 doc
: /* Interrupt process PROCESS.
6009 PROCESS may be a process, a buffer, or the name of a process or buffer.
6010 No arg or nil means current buffer's process.
6011 Second arg CURRENT-GROUP non-nil means send signal to
6012 the current process-group of the process's controlling terminal
6013 rather than to the process's own process group.
6014 If the process is a shell, this means interrupt current subjob
6015 rather than the shell.
6017 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6018 don't send the signal. */)
6019 (process
, current_group
)
6020 Lisp_Object process
, current_group
;
6022 process_send_signal (process
, SIGINT
, current_group
, 0);
6026 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6027 doc
: /* Kill process PROCESS. May be process or name of one.
6028 See function `interrupt-process' for more details on usage. */)
6029 (process
, current_group
)
6030 Lisp_Object process
, current_group
;
6032 process_send_signal (process
, SIGKILL
, current_group
, 0);
6036 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6037 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6038 See function `interrupt-process' for more details on usage. */)
6039 (process
, current_group
)
6040 Lisp_Object process
, current_group
;
6042 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6046 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6047 doc
: /* Stop process PROCESS. May be process or name of one.
6048 See function `interrupt-process' for more details on usage.
6049 If PROCESS is a network process, inhibit handling of incoming traffic. */)
6050 (process
, current_group
)
6051 Lisp_Object process
, current_group
;
6054 if (PROCESSP (process
) && NETCONN_P (process
))
6056 struct Lisp_Process
*p
;
6058 p
= XPROCESS (process
);
6059 if (NILP (p
->command
)
6062 FD_CLR (p
->infd
, &input_wait_mask
);
6063 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6070 error ("No SIGTSTP support");
6072 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6077 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6078 doc
: /* Continue process PROCESS. May be process or name of one.
6079 See function `interrupt-process' for more details on usage.
6080 If PROCESS is a network process, resume handling of incoming traffic. */)
6081 (process
, current_group
)
6082 Lisp_Object process
, current_group
;
6085 if (PROCESSP (process
) && NETCONN_P (process
))
6087 struct Lisp_Process
*p
;
6089 p
= XPROCESS (process
);
6090 if (EQ (p
->command
, Qt
)
6092 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6094 FD_SET (p
->infd
, &input_wait_mask
);
6095 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6102 process_send_signal (process
, SIGCONT
, current_group
, 0);
6104 error ("No SIGCONT support");
6109 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6110 2, 2, "sProcess (name or number): \nnSignal code: ",
6111 doc
: /* Send PROCESS the signal with code SIGCODE.
6112 PROCESS may also be a number specifying the process id of the
6113 process to signal; in this case, the process need not be a child of
6115 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6117 Lisp_Object process
, sigcode
;
6121 if (INTEGERP (process
))
6123 pid
= XINT (process
);
6127 if (FLOATP (process
))
6129 pid
= (pid_t
) XFLOAT_DATA (process
);
6133 if (STRINGP (process
))
6136 if (tem
= Fget_process (process
), NILP (tem
))
6138 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6145 process
= get_process (process
);
6150 CHECK_PROCESS (process
);
6151 pid
= XPROCESS (process
)->pid
;
6153 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6157 #define parse_signal(NAME, VALUE) \
6158 else if (!xstricmp (name, NAME)) \
6159 XSETINT (sigcode, VALUE)
6161 if (INTEGERP (sigcode
))
6165 unsigned char *name
;
6167 CHECK_SYMBOL (sigcode
);
6168 name
= SDATA (SYMBOL_NAME (sigcode
));
6170 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6176 parse_signal ("usr1", SIGUSR1
);
6179 parse_signal ("usr2", SIGUSR2
);
6182 parse_signal ("term", SIGTERM
);
6185 parse_signal ("hup", SIGHUP
);
6188 parse_signal ("int", SIGINT
);
6191 parse_signal ("quit", SIGQUIT
);
6194 parse_signal ("ill", SIGILL
);
6197 parse_signal ("abrt", SIGABRT
);
6200 parse_signal ("emt", SIGEMT
);
6203 parse_signal ("kill", SIGKILL
);
6206 parse_signal ("fpe", SIGFPE
);
6209 parse_signal ("bus", SIGBUS
);
6212 parse_signal ("segv", SIGSEGV
);
6215 parse_signal ("sys", SIGSYS
);
6218 parse_signal ("pipe", SIGPIPE
);
6221 parse_signal ("alrm", SIGALRM
);
6224 parse_signal ("urg", SIGURG
);
6227 parse_signal ("stop", SIGSTOP
);
6230 parse_signal ("tstp", SIGTSTP
);
6233 parse_signal ("cont", SIGCONT
);
6236 parse_signal ("chld", SIGCHLD
);
6239 parse_signal ("ttin", SIGTTIN
);
6242 parse_signal ("ttou", SIGTTOU
);
6245 parse_signal ("io", SIGIO
);
6248 parse_signal ("xcpu", SIGXCPU
);
6251 parse_signal ("xfsz", SIGXFSZ
);
6254 parse_signal ("vtalrm", SIGVTALRM
);
6257 parse_signal ("prof", SIGPROF
);
6260 parse_signal ("winch", SIGWINCH
);
6263 parse_signal ("info", SIGINFO
);
6266 error ("Undefined signal name %s", name
);
6271 return make_number (kill (pid
, XINT (sigcode
)));
6274 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6275 doc
: /* Make PROCESS see end-of-file in its input.
6276 EOF comes after any text already sent to it.
6277 PROCESS may be a process, a buffer, the name of a process or buffer, or
6278 nil, indicating the current buffer's process.
6279 If PROCESS is a network connection, or is a process communicating
6280 through a pipe (as opposed to a pty), then you cannot send any more
6281 text to PROCESS after you call this function. */)
6283 Lisp_Object process
;
6286 struct coding_system
*coding
;
6288 if (DATAGRAM_CONN_P (process
))
6291 proc
= get_process (process
);
6292 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6294 /* Make sure the process is really alive. */
6295 if (XPROCESS (proc
)->raw_status_new
)
6296 update_status (XPROCESS (proc
));
6297 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6298 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6300 if (CODING_REQUIRE_FLUSHING (coding
))
6302 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6303 send_process (proc
, "", 0, Qnil
);
6307 send_process (proc
, "\032", 1, Qnil
); /* ^z */
6309 if (XPROCESS (proc
)->pty_flag
)
6310 send_process (proc
, "\004", 1, Qnil
);
6313 int old_outfd
, new_outfd
;
6315 #ifdef HAVE_SHUTDOWN
6316 /* If this is a network connection, or socketpair is used
6317 for communication with the subprocess, call shutdown to cause EOF.
6318 (In some old system, shutdown to socketpair doesn't work.
6319 Then we just can't win.) */
6320 if (XPROCESS (proc
)->pid
== 0
6321 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6322 shutdown (XPROCESS (proc
)->outfd
, 1);
6323 /* In case of socketpair, outfd == infd, so don't close it. */
6324 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6325 emacs_close (XPROCESS (proc
)->outfd
);
6326 #else /* not HAVE_SHUTDOWN */
6327 emacs_close (XPROCESS (proc
)->outfd
);
6328 #endif /* not HAVE_SHUTDOWN */
6329 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6332 old_outfd
= XPROCESS (proc
)->outfd
;
6334 if (!proc_encode_coding_system
[new_outfd
])
6335 proc_encode_coding_system
[new_outfd
]
6336 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6337 bcopy (proc_encode_coding_system
[old_outfd
],
6338 proc_encode_coding_system
[new_outfd
],
6339 sizeof (struct coding_system
));
6340 bzero (proc_encode_coding_system
[old_outfd
],
6341 sizeof (struct coding_system
));
6343 XPROCESS (proc
)->outfd
= new_outfd
;
6349 /* Kill all processes associated with `buffer'.
6350 If `buffer' is nil, kill all processes */
6353 kill_buffer_processes (buffer
)
6356 Lisp_Object tail
, proc
;
6358 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6360 proc
= XCDR (XCAR (tail
));
6362 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6364 if (NETCONN_P (proc
))
6365 Fdelete_process (proc
);
6366 else if (XPROCESS (proc
)->infd
>= 0)
6367 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6372 /* On receipt of a signal that a child status has changed, loop asking
6373 about children with changed statuses until the system says there
6376 All we do is change the status; we do not run sentinels or print
6377 notifications. That is saved for the next time keyboard input is
6378 done, in order to avoid timing errors.
6380 ** WARNING: this can be called during garbage collection.
6381 Therefore, it must not be fooled by the presence of mark bits in
6384 ** USG WARNING: Although it is not obvious from the documentation
6385 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6386 signal() before executing at least one wait(), otherwise the
6387 handler will be called again, resulting in an infinite loop. The
6388 relevant portion of the documentation reads "SIGCLD signals will be
6389 queued and the signal-catching function will be continually
6390 reentered until the queue is empty". Invoking signal() causes the
6391 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6394 ** Malloc WARNING: This should never call malloc either directly or
6395 indirectly; if it does, that is a bug */
6399 sigchld_handler (signo
)
6402 int old_errno
= errno
;
6404 register struct Lisp_Process
*p
;
6405 extern EMACS_TIME
*input_available_clear_time
;
6407 SIGNAL_THREAD_CHECK (signo
);
6411 sigheld
|= sigbit (SIGCHLD
);
6423 #endif /* no WUNTRACED */
6424 /* Keep trying to get a status until we get a definitive result. */
6428 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6430 while (pid
< 0 && errno
== EINTR
);
6434 /* PID == 0 means no processes found, PID == -1 means a real
6435 failure. We have done all our job, so return. */
6437 /* USG systems forget handlers when they are used;
6438 must reestablish each time */
6439 #if defined (USG) && !defined (POSIX_SIGNALS)
6440 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6443 sigheld
&= ~sigbit (SIGCHLD
);
6451 #endif /* no WNOHANG */
6453 /* Find the process that signaled us, and record its status. */
6455 /* The process can have been deleted by Fdelete_process. */
6456 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6458 Lisp_Object xpid
= XCAR (tail
);
6459 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6460 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6462 XSETCAR (tail
, Qnil
);
6463 goto sigchld_end_of_loop
;
6467 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6469 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6471 proc
= XCDR (XCAR (tail
));
6472 p
= XPROCESS (proc
);
6473 if (EQ (p
->childp
, Qt
) && p
->pid
== pid
)
6478 /* Look for an asynchronous process whose pid hasn't been filled
6481 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6483 proc
= XCDR (XCAR (tail
));
6484 p
= XPROCESS (proc
);
6490 /* Change the status of the process that was found. */
6493 union { int i
; WAITTYPE wt
; } u
;
6494 int clear_desc_flag
= 0;
6496 p
->tick
= ++process_tick
;
6498 p
->raw_status
= u
.i
;
6499 p
->raw_status_new
= 1;
6501 /* If process has terminated, stop waiting for its output. */
6502 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6504 clear_desc_flag
= 1;
6506 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6507 if (clear_desc_flag
)
6509 FD_CLR (p
->infd
, &input_wait_mask
);
6510 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6513 /* Tell wait_reading_process_output that it needs to wake up and
6515 if (input_available_clear_time
)
6516 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6519 /* There was no asynchronous process found for that pid: we have
6520 a synchronous process. */
6523 synch_process_alive
= 0;
6525 /* Report the status of the synchronous process. */
6527 synch_process_retcode
= WRETCODE (w
);
6528 else if (WIFSIGNALED (w
))
6529 synch_process_termsig
= WTERMSIG (w
);
6531 /* Tell wait_reading_process_output that it needs to wake up and
6533 if (input_available_clear_time
)
6534 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6537 sigchld_end_of_loop
:
6540 /* On some systems, we must return right away.
6541 If any more processes want to signal us, we will
6543 Otherwise (on systems that have WNOHANG), loop around
6544 to use up all the processes that have something to tell us. */
6545 #if (defined WINDOWSNT \
6546 || (defined USG && !defined GNU_LINUX \
6547 && !(defined HPUX && defined WNOHANG)))
6548 #if defined (USG) && ! defined (POSIX_SIGNALS)
6549 signal (signo
, sigchld_handler
);
6553 #endif /* USG, but not HPUX with WNOHANG */
6556 #endif /* SIGCHLD */
6560 exec_sentinel_unwind (data
)
6563 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6568 exec_sentinel_error_handler (error
)
6571 cmd_error_internal (error
, "error in process sentinel: ");
6573 update_echo_area ();
6574 Fsleep_for (make_number (2), Qnil
);
6579 exec_sentinel (proc
, reason
)
6580 Lisp_Object proc
, reason
;
6582 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6583 register struct Lisp_Process
*p
= XPROCESS (proc
);
6584 int count
= SPECPDL_INDEX ();
6585 int outer_running_asynch_code
= running_asynch_code
;
6586 int waiting
= waiting_for_user_input_p
;
6588 if (inhibit_sentinels
)
6591 /* No need to gcpro these, because all we do with them later
6592 is test them for EQness, and none of them should be a string. */
6593 odeactivate
= Vdeactivate_mark
;
6594 XSETBUFFER (obuffer
, current_buffer
);
6595 okeymap
= current_buffer
->keymap
;
6597 sentinel
= p
->sentinel
;
6598 if (NILP (sentinel
))
6601 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6602 assure that it gets restored no matter how the sentinel exits. */
6604 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6605 /* Inhibit quit so that random quits don't screw up a running filter. */
6606 specbind (Qinhibit_quit
, Qt
);
6607 specbind (Qlast_nonmenu_event
, Qt
);
6609 /* In case we get recursively called,
6610 and we already saved the match data nonrecursively,
6611 save the same match data in safely recursive fashion. */
6612 if (outer_running_asynch_code
)
6615 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6616 restore_search_regs ();
6617 record_unwind_save_match_data ();
6618 Fset_match_data (tem
, Qt
);
6621 /* For speed, if a search happens within this code,
6622 save the match data in a special nonrecursive fashion. */
6623 running_asynch_code
= 1;
6625 internal_condition_case_1 (read_process_output_call
,
6627 Fcons (proc
, Fcons (reason
, Qnil
))),
6628 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6629 exec_sentinel_error_handler
);
6631 /* If we saved the match data nonrecursively, restore it now. */
6632 restore_search_regs ();
6633 running_asynch_code
= outer_running_asynch_code
;
6635 Vdeactivate_mark
= odeactivate
;
6637 /* Restore waiting_for_user_input_p as it was
6638 when we were called, in case the filter clobbered it. */
6639 waiting_for_user_input_p
= waiting
;
6642 if (! EQ (Fcurrent_buffer (), obuffer
)
6643 || ! EQ (current_buffer
->keymap
, okeymap
))
6645 /* But do it only if the caller is actually going to read events.
6646 Otherwise there's no need to make him wake up, and it could
6647 cause trouble (for example it would make sit_for return). */
6648 if (waiting_for_user_input_p
== -1)
6649 record_asynch_buffer_change ();
6651 unbind_to (count
, Qnil
);
6654 /* Report all recent events of a change in process status
6655 (either run the sentinel or output a message).
6656 This is usually done while Emacs is waiting for keyboard input
6657 but can be done at other times. */
6660 status_notify (deleting_process
)
6661 struct Lisp_Process
*deleting_process
;
6663 register Lisp_Object proc
, buffer
;
6664 Lisp_Object tail
, msg
;
6665 struct gcpro gcpro1
, gcpro2
;
6669 /* We need to gcpro tail; if read_process_output calls a filter
6670 which deletes a process and removes the cons to which tail points
6671 from Vprocess_alist, and then causes a GC, tail is an unprotected
6675 /* Set this now, so that if new processes are created by sentinels
6676 that we run, we get called again to handle their status changes. */
6677 update_tick
= process_tick
;
6679 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6682 register struct Lisp_Process
*p
;
6684 proc
= Fcdr (XCAR (tail
));
6685 p
= XPROCESS (proc
);
6687 if (p
->tick
!= p
->update_tick
)
6689 p
->update_tick
= p
->tick
;
6691 /* If process is still active, read any output that remains. */
6692 while (! EQ (p
->filter
, Qt
)
6693 && ! EQ (p
->status
, Qconnect
)
6694 && ! EQ (p
->status
, Qlisten
)
6695 && ! EQ (p
->command
, Qt
) /* Network process not stopped. */
6697 && p
!= deleting_process
6698 && read_process_output (proc
, p
->infd
) > 0);
6702 /* Get the text to use for the message. */
6703 if (p
->raw_status_new
)
6705 msg
= status_message (p
);
6707 /* If process is terminated, deactivate it or delete it. */
6709 if (CONSP (p
->status
))
6710 symbol
= XCAR (p
->status
);
6712 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6713 || EQ (symbol
, Qclosed
))
6715 if (delete_exited_processes
)
6716 remove_process (proc
);
6718 deactivate_process (proc
);
6721 /* The actions above may have further incremented p->tick.
6722 So set p->update_tick again
6723 so that an error in the sentinel will not cause
6724 this code to be run again. */
6725 p
->update_tick
= p
->tick
;
6726 /* Now output the message suitably. */
6727 if (!NILP (p
->sentinel
))
6728 exec_sentinel (proc
, msg
);
6729 /* Don't bother with a message in the buffer
6730 when a process becomes runnable. */
6731 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6733 Lisp_Object ro
, tem
;
6734 struct buffer
*old
= current_buffer
;
6735 int opoint
, opoint_byte
;
6736 int before
, before_byte
;
6738 ro
= XBUFFER (buffer
)->read_only
;
6740 /* Avoid error if buffer is deleted
6741 (probably that's why the process is dead, too) */
6742 if (NILP (XBUFFER (buffer
)->name
))
6744 Fset_buffer (buffer
);
6747 opoint_byte
= PT_BYTE
;
6748 /* Insert new output into buffer
6749 at the current end-of-output marker,
6750 thus preserving logical ordering of input and output. */
6751 if (XMARKER (p
->mark
)->buffer
)
6752 Fgoto_char (p
->mark
);
6754 SET_PT_BOTH (ZV
, ZV_BYTE
);
6757 before_byte
= PT_BYTE
;
6759 tem
= current_buffer
->read_only
;
6760 current_buffer
->read_only
= Qnil
;
6761 insert_string ("\nProcess ");
6762 Finsert (1, &p
->name
);
6763 insert_string (" ");
6765 current_buffer
->read_only
= tem
;
6766 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6768 if (opoint
>= before
)
6769 SET_PT_BOTH (opoint
+ (PT
- before
),
6770 opoint_byte
+ (PT_BYTE
- before_byte
));
6772 SET_PT_BOTH (opoint
, opoint_byte
);
6774 set_buffer_internal (old
);
6779 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6780 redisplay_preserve_echo_area (13);
6786 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6787 Sset_process_coding_system
, 1, 3, 0,
6788 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6789 DECODING will be used to decode subprocess output and ENCODING to
6790 encode subprocess input. */)
6791 (process
, decoding
, encoding
)
6792 register Lisp_Object process
, decoding
, encoding
;
6794 register struct Lisp_Process
*p
;
6796 CHECK_PROCESS (process
);
6797 p
= XPROCESS (process
);
6799 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6801 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6802 Fcheck_coding_system (decoding
);
6803 Fcheck_coding_system (encoding
);
6804 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6805 p
->decode_coding_system
= decoding
;
6806 p
->encode_coding_system
= encoding
;
6807 setup_process_coding_systems (process
);
6812 DEFUN ("process-coding-system",
6813 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6814 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6816 register Lisp_Object process
;
6818 CHECK_PROCESS (process
);
6819 return Fcons (XPROCESS (process
)->decode_coding_system
,
6820 XPROCESS (process
)->encode_coding_system
);
6823 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6824 Sset_process_filter_multibyte
, 2, 2, 0,
6825 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6826 If FLAG is non-nil, the filter is given multibyte strings.
6827 If FLAG is nil, the filter is given unibyte strings. In this case,
6828 all character code conversion except for end-of-line conversion is
6831 Lisp_Object process
, flag
;
6833 register struct Lisp_Process
*p
;
6835 CHECK_PROCESS (process
);
6836 p
= XPROCESS (process
);
6837 p
->filter_multibyte
= !NILP (flag
);
6838 setup_process_coding_systems (process
);
6843 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6844 Sprocess_filter_multibyte_p
, 1, 1, 0,
6845 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6847 Lisp_Object process
;
6849 register struct Lisp_Process
*p
;
6851 CHECK_PROCESS (process
);
6852 p
= XPROCESS (process
);
6854 return (p
->filter_multibyte
? Qt
: Qnil
);
6859 /* Add DESC to the set of keyboard input descriptors. */
6862 add_keyboard_wait_descriptor (desc
)
6865 FD_SET (desc
, &input_wait_mask
);
6866 FD_SET (desc
, &non_process_wait_mask
);
6867 if (desc
> max_keyboard_desc
)
6868 max_keyboard_desc
= desc
;
6871 static int add_gpm_wait_descriptor_called_flag
;
6874 add_gpm_wait_descriptor (desc
)
6877 if (! add_gpm_wait_descriptor_called_flag
)
6878 FD_CLR (0, &input_wait_mask
);
6879 add_gpm_wait_descriptor_called_flag
= 1;
6880 FD_SET (desc
, &input_wait_mask
);
6881 FD_SET (desc
, &gpm_wait_mask
);
6882 if (desc
> max_gpm_desc
)
6883 max_gpm_desc
= desc
;
6886 /* From now on, do not expect DESC to give keyboard input. */
6889 delete_keyboard_wait_descriptor (desc
)
6893 int lim
= max_keyboard_desc
;
6895 FD_CLR (desc
, &input_wait_mask
);
6896 FD_CLR (desc
, &non_process_wait_mask
);
6898 if (desc
== max_keyboard_desc
)
6899 for (fd
= 0; fd
< lim
; fd
++)
6900 if (FD_ISSET (fd
, &input_wait_mask
)
6901 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
6902 && !FD_ISSET (fd
, &gpm_wait_mask
))
6903 max_keyboard_desc
= fd
;
6907 delete_gpm_wait_descriptor (desc
)
6911 int lim
= max_gpm_desc
;
6913 FD_CLR (desc
, &input_wait_mask
);
6914 FD_CLR (desc
, &non_process_wait_mask
);
6916 if (desc
== max_gpm_desc
)
6917 for (fd
= 0; fd
< lim
; fd
++)
6918 if (FD_ISSET (fd
, &input_wait_mask
)
6919 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
6920 && !FD_ISSET (fd
, &non_process_wait_mask
))
6924 /* Return nonzero if *MASK has a bit set
6925 that corresponds to one of the keyboard input descriptors. */
6928 keyboard_bit_set (mask
)
6933 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
6934 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6935 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6946 inhibit_sentinels
= 0;
6950 if (! noninteractive
|| initialized
)
6952 signal (SIGCHLD
, sigchld_handler
);
6955 FD_ZERO (&input_wait_mask
);
6956 FD_ZERO (&non_keyboard_wait_mask
);
6957 FD_ZERO (&non_process_wait_mask
);
6958 max_process_desc
= 0;
6960 #ifdef NON_BLOCKING_CONNECT
6961 FD_ZERO (&connect_wait_mask
);
6962 num_pending_connects
= 0;
6965 #ifdef ADAPTIVE_READ_BUFFERING
6966 process_output_delay_count
= 0;
6967 process_output_skip
= 0;
6970 /* Don't do this, it caused infinite select loops. The display
6971 method should call add_keyboard_wait_descriptor on stdin if it
6974 FD_SET (0, &input_wait_mask
);
6977 Vprocess_alist
= Qnil
;
6979 deleted_pid_list
= Qnil
;
6981 for (i
= 0; i
< MAXDESC
; i
++)
6983 chan_process
[i
] = Qnil
;
6984 proc_buffered_char
[i
] = -1;
6986 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
6987 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
6988 #ifdef DATAGRAM_SOCKETS
6989 bzero (datagram_address
, sizeof datagram_address
);
6994 Lisp_Object subfeatures
= Qnil
;
6995 struct socket_options
*sopt
;
6997 #define ADD_SUBFEATURE(key, val) \
6998 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7000 #ifdef NON_BLOCKING_CONNECT
7001 ADD_SUBFEATURE (QCnowait
, Qt
);
7003 #ifdef DATAGRAM_SOCKETS
7004 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7006 #ifdef HAVE_LOCAL_SOCKETS
7007 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7009 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7011 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7013 #ifdef HAVE_GETSOCKNAME
7014 ADD_SUBFEATURE (QCservice
, Qt
);
7016 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY))
7017 ADD_SUBFEATURE (QCserver
, Qt
);
7020 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7021 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7023 Fprovide (intern ("make-network-process"), subfeatures
);
7025 #endif /* HAVE_SOCKETS */
7027 #if defined (DARWIN) || defined (MAC_OSX)
7028 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7029 processes. As such, we only change the default value. */
7032 char *release
= get_operating_system_release();
7033 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7034 && release
[1] == '.')) {
7035 Vprocess_connection_type
= Qnil
;
7044 Qprocessp
= intern ("processp");
7045 staticpro (&Qprocessp
);
7046 Qrun
= intern ("run");
7048 Qstop
= intern ("stop");
7050 Qsignal
= intern ("signal");
7051 staticpro (&Qsignal
);
7053 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7056 Qexit = intern ("exit");
7057 staticpro (&Qexit); */
7059 Qopen
= intern ("open");
7061 Qclosed
= intern ("closed");
7062 staticpro (&Qclosed
);
7063 Qconnect
= intern ("connect");
7064 staticpro (&Qconnect
);
7065 Qfailed
= intern ("failed");
7066 staticpro (&Qfailed
);
7067 Qlisten
= intern ("listen");
7068 staticpro (&Qlisten
);
7069 Qlocal
= intern ("local");
7070 staticpro (&Qlocal
);
7071 Qipv4
= intern ("ipv4");
7074 Qipv6
= intern ("ipv6");
7077 Qdatagram
= intern ("datagram");
7078 staticpro (&Qdatagram
);
7080 QCname
= intern (":name");
7081 staticpro (&QCname
);
7082 QCbuffer
= intern (":buffer");
7083 staticpro (&QCbuffer
);
7084 QChost
= intern (":host");
7085 staticpro (&QChost
);
7086 QCservice
= intern (":service");
7087 staticpro (&QCservice
);
7088 QCtype
= intern (":type");
7089 staticpro (&QCtype
);
7090 QClocal
= intern (":local");
7091 staticpro (&QClocal
);
7092 QCremote
= intern (":remote");
7093 staticpro (&QCremote
);
7094 QCcoding
= intern (":coding");
7095 staticpro (&QCcoding
);
7096 QCserver
= intern (":server");
7097 staticpro (&QCserver
);
7098 QCnowait
= intern (":nowait");
7099 staticpro (&QCnowait
);
7100 QCsentinel
= intern (":sentinel");
7101 staticpro (&QCsentinel
);
7102 QClog
= intern (":log");
7104 QCnoquery
= intern (":noquery");
7105 staticpro (&QCnoquery
);
7106 QCstop
= intern (":stop");
7107 staticpro (&QCstop
);
7108 QCoptions
= intern (":options");
7109 staticpro (&QCoptions
);
7110 QCplist
= intern (":plist");
7111 staticpro (&QCplist
);
7112 QCfilter_multibyte
= intern (":filter-multibyte");
7113 staticpro (&QCfilter_multibyte
);
7115 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7116 staticpro (&Qlast_nonmenu_event
);
7118 staticpro (&Vprocess_alist
);
7120 staticpro (&deleted_pid_list
);
7123 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7124 doc
: /* *Non-nil means delete processes immediately when they exit.
7125 A value of nil means don't delete them until `list-processes' is run. */);
7127 delete_exited_processes
= 1;
7129 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7130 doc
: /* Control type of device used to communicate with subprocesses.
7131 Values are nil to use a pipe, or t or `pty' to use a pty.
7132 The value has no effect if the system has no ptys or if all ptys are busy:
7133 then a pipe is used in any case.
7134 The value takes effect when `start-process' is called. */);
7135 Vprocess_connection_type
= Qt
;
7137 #ifdef ADAPTIVE_READ_BUFFERING
7138 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7139 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7140 On some systems, when Emacs reads the output from a subprocess, the output data
7141 is read in very small blocks, potentially resulting in very poor performance.
7142 This behavior can be remedied to some extent by setting this variable to a
7143 non-nil value, as it will automatically delay reading from such processes, to
7144 allow them to produce more output before Emacs tries to read it.
7145 If the value is t, the delay is reset after each write to the process; any other
7146 non-nil value means that the delay is not reset on write.
7147 The variable takes effect when `start-process' is called. */);
7148 Vprocess_adaptive_read_buffering
= Qt
;
7151 defsubr (&Sprocessp
);
7152 defsubr (&Sget_process
);
7153 defsubr (&Sget_buffer_process
);
7154 defsubr (&Sdelete_process
);
7155 defsubr (&Sprocess_status
);
7156 defsubr (&Sprocess_exit_status
);
7157 defsubr (&Sprocess_id
);
7158 defsubr (&Sprocess_name
);
7159 defsubr (&Sprocess_tty_name
);
7160 defsubr (&Sprocess_command
);
7161 defsubr (&Sset_process_buffer
);
7162 defsubr (&Sprocess_buffer
);
7163 defsubr (&Sprocess_mark
);
7164 defsubr (&Sset_process_filter
);
7165 defsubr (&Sprocess_filter
);
7166 defsubr (&Sset_process_sentinel
);
7167 defsubr (&Sprocess_sentinel
);
7168 defsubr (&Sset_process_window_size
);
7169 defsubr (&Sset_process_inherit_coding_system_flag
);
7170 defsubr (&Sprocess_inherit_coding_system_flag
);
7171 defsubr (&Sset_process_query_on_exit_flag
);
7172 defsubr (&Sprocess_query_on_exit_flag
);
7173 defsubr (&Sprocess_contact
);
7174 defsubr (&Sprocess_plist
);
7175 defsubr (&Sset_process_plist
);
7176 defsubr (&Slist_processes
);
7177 defsubr (&Sprocess_list
);
7178 defsubr (&Sstart_process
);
7180 defsubr (&Sset_network_process_option
);
7181 defsubr (&Smake_network_process
);
7182 defsubr (&Sformat_network_address
);
7183 #endif /* HAVE_SOCKETS */
7184 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7186 defsubr (&Snetwork_interface_list
);
7188 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7189 defsubr (&Snetwork_interface_info
);
7191 #endif /* HAVE_SOCKETS ... */
7192 #ifdef DATAGRAM_SOCKETS
7193 defsubr (&Sprocess_datagram_address
);
7194 defsubr (&Sset_process_datagram_address
);
7196 defsubr (&Saccept_process_output
);
7197 defsubr (&Sprocess_send_region
);
7198 defsubr (&Sprocess_send_string
);
7199 defsubr (&Sinterrupt_process
);
7200 defsubr (&Skill_process
);
7201 defsubr (&Squit_process
);
7202 defsubr (&Sstop_process
);
7203 defsubr (&Scontinue_process
);
7204 defsubr (&Sprocess_running_child_p
);
7205 defsubr (&Sprocess_send_eof
);
7206 defsubr (&Ssignal_process
);
7207 defsubr (&Swaiting_for_user_input_p
);
7208 /* defsubr (&Sprocess_connection); */
7209 defsubr (&Sset_process_coding_system
);
7210 defsubr (&Sprocess_coding_system
);
7211 defsubr (&Sset_process_filter_multibyte
);
7212 defsubr (&Sprocess_filter_multibyte_p
);
7216 #else /* not subprocesses */
7218 #include <sys/types.h>
7222 #include "systime.h"
7223 #include "character.h"
7225 #include "termopts.h"
7226 #include "sysselect.h"
7228 extern int frame_garbaged
;
7230 extern EMACS_TIME
timer_check ();
7231 extern int timers_run
;
7235 /* As described above, except assuming that there are no subprocesses:
7237 Wait for timeout to elapse and/or keyboard input to be available.
7240 timeout in seconds, or
7241 zero for no limit, or
7242 -1 means gobble data immediately available but don't wait for any.
7244 read_kbd is a Lisp_Object:
7245 0 to ignore keyboard input, or
7246 1 to return when input is available, or
7247 -1 means caller will actually read the input, so don't throw to
7250 see full version for other parameters. We know that wait_proc will
7251 always be NULL, since `subprocesses' isn't defined.
7253 do_display != 0 means redisplay should be done to show subprocess
7254 output that arrives.
7256 Return true if we received input from any process. */
7259 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7260 wait_for_cell
, wait_proc
, just_wait_proc
)
7261 int time_limit
, microsecs
, read_kbd
, do_display
;
7262 Lisp_Object wait_for_cell
;
7263 struct Lisp_Process
*wait_proc
;
7267 EMACS_TIME end_time
, timeout
;
7268 SELECT_TYPE waitchannels
;
7271 /* What does time_limit really mean? */
7272 if (time_limit
|| microsecs
)
7274 EMACS_GET_TIME (end_time
);
7275 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7276 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7279 /* Turn off periodic alarms (in case they are in use)
7280 and then turn off any other atimers,
7281 because the select emulator uses alarms. */
7283 turn_on_atimers (0);
7287 int timeout_reduced_for_timers
= 0;
7289 /* If calling from keyboard input, do not quit
7290 since we want to return C-g as an input character.
7291 Otherwise, do pending quit if requested. */
7295 /* Exit now if the cell we're waiting for became non-nil. */
7296 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7299 /* Compute time from now till when time limit is up */
7300 /* Exit if already run out */
7301 if (time_limit
== -1)
7303 /* -1 specified for timeout means
7304 gobble output available now
7305 but don't wait at all. */
7307 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7309 else if (time_limit
|| microsecs
)
7311 EMACS_GET_TIME (timeout
);
7312 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7313 if (EMACS_TIME_NEG_P (timeout
))
7318 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7321 /* If our caller will not immediately handle keyboard events,
7322 run timer events directly.
7323 (Callers that will immediately read keyboard events
7324 call timer_delay on their own.) */
7325 if (NILP (wait_for_cell
))
7327 EMACS_TIME timer_delay
;
7331 int old_timers_run
= timers_run
;
7332 timer_delay
= timer_check (1);
7333 if (timers_run
!= old_timers_run
&& do_display
)
7334 /* We must retry, since a timer may have requeued itself
7335 and that could alter the time delay. */
7336 redisplay_preserve_echo_area (14);
7340 while (!detect_input_pending ());
7342 /* If there is unread keyboard input, also return. */
7344 && requeued_events_pending_p ())
7347 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7349 EMACS_TIME difference
;
7350 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7351 if (EMACS_TIME_NEG_P (difference
))
7353 timeout
= timer_delay
;
7354 timeout_reduced_for_timers
= 1;
7359 /* Cause C-g and alarm signals to take immediate action,
7360 and cause input available signals to zero out timeout. */
7362 set_waiting_for_input (&timeout
);
7364 /* Wait till there is something to do. */
7366 if (! read_kbd
&& NILP (wait_for_cell
))
7367 FD_ZERO (&waitchannels
);
7369 FD_SET (0, &waitchannels
);
7371 /* If a frame has been newly mapped and needs updating,
7372 reprocess its display stuff. */
7373 if (frame_garbaged
&& do_display
)
7375 clear_waiting_for_input ();
7376 redisplay_preserve_echo_area (15);
7378 set_waiting_for_input (&timeout
);
7381 if (read_kbd
&& detect_input_pending ())
7384 FD_ZERO (&waitchannels
);
7387 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7392 /* Make C-g and alarm signals set flags again */
7393 clear_waiting_for_input ();
7395 /* If we woke up due to SIGWINCH, actually change size now. */
7396 do_pending_window_change (0);
7398 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7399 /* We waited the full specified time, so return now. */
7404 /* If the system call was interrupted, then go around the
7406 if (xerrno
== EINTR
)
7407 FD_ZERO (&waitchannels
);
7409 error ("select error: %s", emacs_strerror (xerrno
));
7412 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7413 /* System sometimes fails to deliver SIGIO. */
7414 kill (getpid (), SIGIO
);
7417 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7418 kill (getpid (), SIGIO
);
7421 /* Check for keyboard input */
7424 && detect_input_pending_run_timers (do_display
))
7426 swallow_events (do_display
);
7427 if (detect_input_pending_run_timers (do_display
))
7431 /* If there is unread keyboard input, also return. */
7433 && requeued_events_pending_p ())
7436 /* If wait_for_cell. check for keyboard input
7437 but don't run any timers.
7438 ??? (It seems wrong to me to check for keyboard
7439 input at all when wait_for_cell, but the code
7440 has been this way since July 1994.
7441 Try changing this after version 19.31.) */
7442 if (! NILP (wait_for_cell
)
7443 && detect_input_pending ())
7445 swallow_events (do_display
);
7446 if (detect_input_pending ())
7450 /* Exit now if the cell we're waiting for became non-nil. */
7451 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7461 /* Don't confuse make-docfile by having two doc strings for this function.
7462 make-docfile does not pay attention to #if, for good reason! */
7463 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7466 register Lisp_Object name
;
7471 /* Don't confuse make-docfile by having two doc strings for this function.
7472 make-docfile does not pay attention to #if, for good reason! */
7473 DEFUN ("process-inherit-coding-system-flag",
7474 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7478 register Lisp_Object process
;
7480 /* Ignore the argument and return the value of
7481 inherit-process-coding-system. */
7482 return inherit_process_coding_system
? Qt
: Qnil
;
7485 /* Kill all processes associated with `buffer'.
7486 If `buffer' is nil, kill all processes.
7487 Since we have no subprocesses, this does nothing. */
7490 kill_buffer_processes (buffer
)
7503 QCtype
= intern (":type");
7504 staticpro (&QCtype
);
7506 defsubr (&Sget_buffer_process
);
7507 defsubr (&Sprocess_inherit_coding_system_flag
);
7511 #endif /* not subprocesses */
7513 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7514 (do not change this comment) */