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 Qlast_nonmenu_event
;
149 /* QCfamily is declared and initialized in xfaces.c,
150 QCfilter in keyboard.c. */
151 extern Lisp_Object QCfamily
, QCfilter
;
153 /* Qexit is declared and initialized in eval.c. */
155 /* QCfamily is defined in xfaces.c. */
156 extern Lisp_Object QCfamily
;
157 /* QCfilter is defined in keyboard.c. */
158 extern Lisp_Object QCfilter
;
160 /* a process object is a network connection when its childp field is neither
161 Qt nor Qnil but is instead a property list (KEY VAL ...). */
164 #define NETCONN_P(p) (CONSP (XPROCESS (p)->childp))
165 #define NETCONN1_P(p) (CONSP ((p)->childp))
167 #define NETCONN_P(p) 0
168 #define NETCONN1_P(p) 0
169 #endif /* HAVE_SOCKETS */
171 /* Define first descriptor number available for subprocesses. */
173 #define FIRST_PROC_DESC 1
175 #define FIRST_PROC_DESC 3
178 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
181 #if !defined (SIGCHLD) && defined (SIGCLD)
182 #define SIGCHLD SIGCLD
185 #include "syssignal.h"
189 extern char *get_operating_system_release ();
195 extern char *sys_errlist
[];
202 /* t means use pty, nil means use a pipe,
203 maybe other values to come. */
204 static Lisp_Object Vprocess_connection_type
;
206 /* These next two vars are non-static since sysdep.c uses them in the
207 emulation of `select'. */
208 /* Number of events of change of status of a process. */
210 /* Number of events for which the user or sentinel has been notified. */
213 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
215 #ifdef BROKEN_NON_BLOCKING_CONNECT
216 #undef NON_BLOCKING_CONNECT
218 #ifndef NON_BLOCKING_CONNECT
221 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
222 #if defined (O_NONBLOCK) || defined (O_NDELAY)
223 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
224 #define NON_BLOCKING_CONNECT
225 #endif /* EWOULDBLOCK || EINPROGRESS */
226 #endif /* O_NONBLOCK || O_NDELAY */
227 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
228 #endif /* HAVE_SELECT */
229 #endif /* HAVE_SOCKETS */
230 #endif /* NON_BLOCKING_CONNECT */
231 #endif /* BROKEN_NON_BLOCKING_CONNECT */
233 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
234 this system. We need to read full packets, so we need a
235 "non-destructive" select. So we require either native select,
236 or emulation of select using FIONREAD. */
238 #ifdef BROKEN_DATAGRAM_SOCKETS
239 #undef DATAGRAM_SOCKETS
241 #ifndef DATAGRAM_SOCKETS
243 #if defined (HAVE_SELECT) || defined (FIONREAD)
244 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
245 #define DATAGRAM_SOCKETS
246 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
247 #endif /* HAVE_SELECT || FIONREAD */
248 #endif /* HAVE_SOCKETS */
249 #endif /* DATAGRAM_SOCKETS */
250 #endif /* BROKEN_DATAGRAM_SOCKETS */
253 #undef NON_BLOCKING_CONNECT
254 #undef DATAGRAM_SOCKETS
257 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
258 #ifdef EMACS_HAS_USECS
259 #define ADAPTIVE_READ_BUFFERING
263 #ifdef ADAPTIVE_READ_BUFFERING
264 #define READ_OUTPUT_DELAY_INCREMENT 10000
265 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
266 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
268 /* Number of processes which have a non-zero read_output_delay,
269 and therefore might be delayed for adaptive read buffering. */
271 static int process_output_delay_count
;
273 /* Non-zero if any process has non-nil read_output_skip. */
275 static int process_output_skip
;
277 /* Non-nil means to delay reading process output to improve buffering.
278 A value of t means that delay is reset after each send, any other
279 non-nil value does not reset the delay. A value of nil disables
280 adaptive read buffering completely. */
281 static Lisp_Object Vprocess_adaptive_read_buffering
;
283 #define process_output_delay_count 0
287 #include "sysselect.h"
289 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
290 static void deactivate_process
P_ ((Lisp_Object
));
291 static void status_notify
P_ ((struct Lisp_Process
*));
292 static int read_process_output
P_ ((Lisp_Object
, int));
294 /* If we support a window system, turn on the code to poll periodically
295 to detect C-g. It isn't actually used when doing interrupt input. */
296 #ifdef HAVE_WINDOW_SYSTEM
297 #define POLL_FOR_INPUT
300 static Lisp_Object
get_process ();
301 static void exec_sentinel ();
303 extern EMACS_TIME
timer_check ();
304 extern int timers_run
;
306 /* Mask of bits indicating the descriptors that we wait for input on. */
308 static SELECT_TYPE input_wait_mask
;
310 /* Mask that excludes keyboard input descriptor(s). */
312 static SELECT_TYPE non_keyboard_wait_mask
;
314 /* Mask that excludes process input descriptor(s). */
316 static SELECT_TYPE non_process_wait_mask
;
318 /* Mask for the gpm mouse input descriptor. */
320 static SELECT_TYPE gpm_wait_mask
;
322 #ifdef NON_BLOCKING_CONNECT
323 /* Mask of bits indicating the descriptors that we wait for connect to
324 complete on. Once they complete, they are removed from this mask
325 and added to the input_wait_mask and non_keyboard_wait_mask. */
327 static SELECT_TYPE connect_wait_mask
;
329 /* Number of bits set in connect_wait_mask. */
330 static int num_pending_connects
;
332 #define IF_NON_BLOCKING_CONNECT(s) s
334 #define IF_NON_BLOCKING_CONNECT(s)
337 /* The largest descriptor currently in use for a process object. */
338 static int max_process_desc
;
340 /* The largest descriptor currently in use for keyboard input. */
341 static int max_keyboard_desc
;
343 /* The largest descriptor currently in use for gpm mouse input. */
344 static int max_gpm_desc
;
346 /* Nonzero means delete a process right away if it exits. */
347 static int delete_exited_processes
;
349 /* Indexed by descriptor, gives the process (if any) for that descriptor */
350 Lisp_Object chan_process
[MAXDESC
];
352 /* Alist of elements (NAME . PROCESS) */
353 Lisp_Object Vprocess_alist
;
355 /* Buffered-ahead input char from process, indexed by channel.
356 -1 means empty (no char is buffered).
357 Used on sys V where the only way to tell if there is any
358 output from the process is to read at least one char.
359 Always -1 on systems that support FIONREAD. */
361 /* Don't make static; need to access externally. */
362 int proc_buffered_char
[MAXDESC
];
364 /* Table of `struct coding-system' for each process. */
365 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
366 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
368 #ifdef DATAGRAM_SOCKETS
369 /* Table of `partner address' for datagram sockets. */
370 struct sockaddr_and_len
{
373 } datagram_address
[MAXDESC
];
374 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
375 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
377 #define DATAGRAM_CHAN_P(chan) (0)
378 #define DATAGRAM_CONN_P(proc) (0)
381 /* Maximum number of bytes to send to a pty without an eof. */
382 static int pty_max_bytes
;
384 /* Nonzero means don't run process sentinels. This is used
386 int inhibit_sentinels
;
392 /* The file name of the pty opened by allocate_pty. */
394 static char pty_name
[24];
397 /* Compute the Lisp form of the process status, p->status, from
398 the numeric status that was returned by `wait'. */
400 static Lisp_Object
status_convert ();
404 struct Lisp_Process
*p
;
406 union { int i
; WAITTYPE wt
; } u
;
407 eassert (p
->raw_status_new
);
409 p
->status
= status_convert (u
.wt
);
410 p
->raw_status_new
= 0;
413 /* Convert a process status word in Unix format to
414 the list that we use internally. */
421 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
422 else if (WIFEXITED (w
))
423 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
424 WCOREDUMP (w
) ? Qt
: Qnil
));
425 else if (WIFSIGNALED (w
))
426 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
427 WCOREDUMP (w
) ? Qt
: Qnil
));
432 /* Given a status-list, extract the three pieces of information
433 and store them individually through the three pointers. */
436 decode_status (l
, symbol
, code
, coredump
)
454 *code
= XFASTINT (XCAR (tem
));
456 *coredump
= !NILP (tem
);
460 /* Return a string describing a process status list. */
464 struct Lisp_Process
*p
;
466 Lisp_Object status
= p
->status
;
469 Lisp_Object string
, string2
;
471 decode_status (status
, &symbol
, &code
, &coredump
);
473 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
476 synchronize_system_messages_locale ();
477 signame
= strsignal (code
);
480 string
= build_string (signame
);
481 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
482 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
483 return concat2 (string
, string2
);
485 else if (EQ (symbol
, Qexit
))
488 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
490 return build_string ("finished\n");
491 string
= Fnumber_to_string (make_number (code
));
492 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
493 return concat3 (build_string ("exited abnormally with code "),
496 else if (EQ (symbol
, Qfailed
))
498 string
= Fnumber_to_string (make_number (code
));
499 string2
= build_string ("\n");
500 return concat3 (build_string ("failed with code "),
504 return Fcopy_sequence (Fsymbol_name (symbol
));
509 /* Open an available pty, returning a file descriptor.
510 Return -1 on failure.
511 The file name of the terminal corresponding to the pty
512 is left in the variable pty_name. */
523 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
524 for (i
= 0; i
< 16; i
++)
527 struct stat stb
; /* Used in some PTY_OPEN. */
528 #ifdef PTY_NAME_SPRINTF
531 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
532 #endif /* no PTY_NAME_SPRINTF */
536 #else /* no PTY_OPEN */
539 /* Unusual IRIS code */
540 *ptyv
= emacs_open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
543 if (fstat (fd
, &stb
) < 0)
545 # else /* not IRIS */
546 { /* Some systems name their pseudoterminals so that there are gaps in
547 the usual sequence - for example, on HP9000/S700 systems, there
548 are no pseudoterminals with names ending in 'f'. So we wait for
549 three failures in a row before deciding that we've reached the
551 int failed_count
= 0;
553 if (stat (pty_name
, &stb
) < 0)
556 if (failed_count
>= 3)
563 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
565 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
567 # endif /* not IRIS */
569 #endif /* no PTY_OPEN */
573 /* check to make certain that both sides are available
574 this avoids a nasty yet stupid bug in rlogins */
575 #ifdef PTY_TTY_NAME_SPRINTF
578 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
579 #endif /* no PTY_TTY_NAME_SPRINTF */
580 if (access (pty_name
, 6) != 0)
583 # if !defined(IRIS) && !defined(__sgi)
595 #endif /* HAVE_PTYS */
601 register Lisp_Object val
, tem
, name1
;
602 register struct Lisp_Process
*p
;
606 p
= allocate_process ();
614 p
->raw_status_new
= 0;
616 p
->mark
= Fmake_marker ();
618 #ifdef ADAPTIVE_READ_BUFFERING
619 p
->adaptive_read_buffering
= 0;
620 p
->read_output_delay
= 0;
621 p
->read_output_skip
= 0;
624 /* If name is already in use, modify it until it is unused. */
629 tem
= Fget_process (name1
);
630 if (NILP (tem
)) break;
631 sprintf (suffix
, "<%d>", i
);
632 name1
= concat2 (name
, build_string (suffix
));
636 XSETPROCESS (val
, p
);
637 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
642 remove_process (proc
)
643 register Lisp_Object proc
;
645 register Lisp_Object pair
;
647 pair
= Frassq (proc
, Vprocess_alist
);
648 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
650 deactivate_process (proc
);
653 /* Setup coding systems of PROCESS. */
656 setup_process_coding_systems (process
)
659 struct Lisp_Process
*p
= XPROCESS (process
);
661 int outch
= p
->outfd
;
662 Lisp_Object coding_system
;
664 if (inch
< 0 || outch
< 0)
667 if (!proc_decode_coding_system
[inch
])
668 proc_decode_coding_system
[inch
]
669 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
670 coding_system
= p
->decode_coding_system
;
671 if (! NILP (p
->filter
))
673 else if (BUFFERP (p
->buffer
))
675 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
676 coding_system
= raw_text_coding_system (coding_system
);
678 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
680 if (!proc_encode_coding_system
[outch
])
681 proc_encode_coding_system
[outch
]
682 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
683 setup_coding_system (p
->encode_coding_system
,
684 proc_encode_coding_system
[outch
]);
687 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
688 doc
: /* Return t if OBJECT is a process. */)
692 return PROCESSP (object
) ? Qt
: Qnil
;
695 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
696 doc
: /* Return the process named NAME, or nil if there is none. */)
698 register Lisp_Object name
;
703 return Fcdr (Fassoc (name
, Vprocess_alist
));
706 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
707 doc
: /* Return the (or a) process associated with BUFFER.
708 BUFFER may be a buffer or the name of one. */)
710 register Lisp_Object buffer
;
712 register Lisp_Object buf
, tail
, proc
;
714 if (NILP (buffer
)) return Qnil
;
715 buf
= Fget_buffer (buffer
);
716 if (NILP (buf
)) return Qnil
;
718 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
720 proc
= Fcdr (XCAR (tail
));
721 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
727 /* This is how commands for the user decode process arguments. It
728 accepts a process, a process name, a buffer, a buffer name, or nil.
729 Buffers denote the first process in the buffer, and nil denotes the
734 register Lisp_Object name
;
736 register Lisp_Object proc
, obj
;
739 obj
= Fget_process (name
);
741 obj
= Fget_buffer (name
);
743 error ("Process %s does not exist", SDATA (name
));
745 else if (NILP (name
))
746 obj
= Fcurrent_buffer ();
750 /* Now obj should be either a buffer object or a process object.
754 proc
= Fget_buffer_process (obj
);
756 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
768 /* Fdelete_process promises to immediately forget about the process, but in
769 reality, Emacs needs to remember those processes until they have been
770 treated by sigchld_handler; otherwise this handler would consider the
771 process as being synchronous and say that the synchronous process is
773 static Lisp_Object deleted_pid_list
;
776 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
777 doc
: /* Delete PROCESS: kill it and forget about it immediately.
778 PROCESS may be a process, a buffer, the name of a process or buffer, or
779 nil, indicating the current buffer's process. */)
781 register Lisp_Object process
;
783 register struct Lisp_Process
*p
;
785 process
= get_process (process
);
786 p
= XPROCESS (process
);
788 p
->raw_status_new
= 0;
791 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
792 p
->tick
= ++process_tick
;
795 else if (p
->infd
>= 0)
799 /* Assignment to EMACS_INT stops GCC whining about limited range
801 EMACS_INT pid
= p
->pid
;
803 /* No problem storing the pid here, as it is still in Vprocess_alist. */
804 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
805 /* GC treated elements set to nil. */
806 Fdelq (Qnil
, deleted_pid_list
));
807 /* If the process has already signaled, remove it from the list. */
808 if (p
->raw_status_new
)
811 if (CONSP (p
->status
))
812 symbol
= XCAR (p
->status
);
813 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
815 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
819 Fkill_process (process
, Qnil
);
820 /* Do this now, since remove_process will make sigchld_handler do nothing. */
822 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
823 p
->tick
= ++process_tick
;
827 remove_process (process
);
831 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
832 doc
: /* Return the status of PROCESS.
833 The returned value is one of the following symbols:
834 run -- for a process that is running.
835 stop -- for a process stopped but continuable.
836 exit -- for a process that has exited.
837 signal -- for a process that has got a fatal signal.
838 open -- for a network stream connection that is open.
839 listen -- for a network stream server that is listening.
840 closed -- for a network stream connection that is closed.
841 connect -- when waiting for a non-blocking connection to complete.
842 failed -- when a non-blocking connection has failed.
843 nil -- if arg is a process name and no such process exists.
844 PROCESS may be a process, a buffer, the name of a process, or
845 nil, indicating the current buffer's process. */)
847 register Lisp_Object process
;
849 register struct Lisp_Process
*p
;
850 register Lisp_Object status
;
852 if (STRINGP (process
))
853 process
= Fget_process (process
);
855 process
= get_process (process
);
860 p
= XPROCESS (process
);
861 if (p
->raw_status_new
)
865 status
= XCAR (status
);
868 if (EQ (status
, Qexit
))
870 else if (EQ (p
->command
, Qt
))
872 else if (EQ (status
, Qrun
))
878 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
880 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
881 If PROCESS has not yet exited or died, return 0. */)
883 register Lisp_Object process
;
885 CHECK_PROCESS (process
);
886 if (XPROCESS (process
)->raw_status_new
)
887 update_status (XPROCESS (process
));
888 if (CONSP (XPROCESS (process
)->status
))
889 return XCAR (XCDR (XPROCESS (process
)->status
));
890 return make_number (0);
893 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
894 doc
: /* Return the process id of PROCESS.
895 This is the pid of the external process which PROCESS uses or talks to.
896 For a network connection, this value is nil. */)
898 register Lisp_Object process
;
900 /* Assignment to EMACS_INT stops GCC whining about limited range of
904 CHECK_PROCESS (process
);
905 pid
= XPROCESS (process
)->pid
;
906 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
909 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
910 doc
: /* Return the name of PROCESS, as a string.
911 This is the name of the program invoked in PROCESS,
912 possibly modified to make it unique among process names. */)
914 register Lisp_Object process
;
916 CHECK_PROCESS (process
);
917 return XPROCESS (process
)->name
;
920 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
921 doc
: /* Return the command that was executed to start PROCESS.
922 This is a list of strings, the first string being the program executed
923 and the rest of the strings being the arguments given to it.
924 For a non-child channel, this is nil. */)
926 register Lisp_Object process
;
928 CHECK_PROCESS (process
);
929 return XPROCESS (process
)->command
;
932 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
933 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
934 This is the terminal that the process itself reads and writes on,
935 not the name of the pty that Emacs uses to talk with that terminal. */)
937 register Lisp_Object process
;
939 CHECK_PROCESS (process
);
940 return XPROCESS (process
)->tty_name
;
943 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
945 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
947 register Lisp_Object process
, buffer
;
949 struct Lisp_Process
*p
;
951 CHECK_PROCESS (process
);
953 CHECK_BUFFER (buffer
);
954 p
= XPROCESS (process
);
957 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
958 setup_process_coding_systems (process
);
962 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
964 doc
: /* Return the buffer PROCESS is associated with.
965 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
967 register Lisp_Object process
;
969 CHECK_PROCESS (process
);
970 return XPROCESS (process
)->buffer
;
973 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
975 doc
: /* Return the marker for the end of the last output from PROCESS. */)
977 register Lisp_Object process
;
979 CHECK_PROCESS (process
);
980 return XPROCESS (process
)->mark
;
983 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
985 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
986 A value of t means stop accepting output from the process.
988 When a process has a filter, its buffer is not used for output.
989 Instead, each time it does output, the entire string of output is
990 passed to the filter.
992 The filter gets two arguments: the process and the string of output.
993 The string argument is normally a multibyte string, except:
994 - if the process' input coding system is no-conversion or raw-text,
995 it is a unibyte string (the non-converted input), or else
996 - if `default-enable-multibyte-characters' is nil, it is a unibyte
997 string (the result of converting the decoded input multibyte
998 string to unibyte with `string-make-unibyte'). */)
1000 register Lisp_Object process
, filter
;
1002 struct Lisp_Process
*p
;
1004 CHECK_PROCESS (process
);
1005 p
= XPROCESS (process
);
1007 /* Don't signal an error if the process' input file descriptor
1008 is closed. This could make debugging Lisp more difficult,
1009 for example when doing something like
1011 (setq process (start-process ...))
1013 (set-process-filter process ...) */
1017 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1019 FD_CLR (p
->infd
, &input_wait_mask
);
1020 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1022 else if (EQ (p
->filter
, Qt
)
1023 && !EQ (p
->command
, Qt
)) /* Network process not stopped. */
1025 FD_SET (p
->infd
, &input_wait_mask
);
1026 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1032 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1033 setup_process_coding_systems (process
);
1037 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1039 doc
: /* Returns the filter function of PROCESS; nil if none.
1040 See `set-process-filter' for more info on filter functions. */)
1042 register Lisp_Object process
;
1044 CHECK_PROCESS (process
);
1045 return XPROCESS (process
)->filter
;
1048 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1050 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1051 The sentinel is called as a function when the process changes state.
1052 It gets two arguments: the process, and a string describing the change. */)
1054 register Lisp_Object process
, sentinel
;
1056 struct Lisp_Process
*p
;
1058 CHECK_PROCESS (process
);
1059 p
= XPROCESS (process
);
1061 p
->sentinel
= sentinel
;
1063 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1067 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1069 doc
: /* Return the sentinel of PROCESS; nil if none.
1070 See `set-process-sentinel' for more info on sentinels. */)
1072 register Lisp_Object process
;
1074 CHECK_PROCESS (process
);
1075 return XPROCESS (process
)->sentinel
;
1078 DEFUN ("set-process-window-size", Fset_process_window_size
,
1079 Sset_process_window_size
, 3, 3, 0,
1080 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1081 (process
, height
, width
)
1082 register Lisp_Object process
, height
, width
;
1084 CHECK_PROCESS (process
);
1085 CHECK_NATNUM (height
);
1086 CHECK_NATNUM (width
);
1088 if (XPROCESS (process
)->infd
< 0
1089 || set_window_size (XPROCESS (process
)->infd
,
1090 XINT (height
), XINT (width
)) <= 0)
1096 DEFUN ("set-process-inherit-coding-system-flag",
1097 Fset_process_inherit_coding_system_flag
,
1098 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1099 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1100 If the second argument FLAG is non-nil, then the variable
1101 `buffer-file-coding-system' of the buffer associated with PROCESS
1102 will be bound to the value of the coding system used to decode
1105 This is useful when the coding system specified for the process buffer
1106 leaves either the character code conversion or the end-of-line conversion
1107 unspecified, or if the coding system used to decode the process output
1108 is more appropriate for saving the process buffer.
1110 Binding the variable `inherit-process-coding-system' to non-nil before
1111 starting the process is an alternative way of setting the inherit flag
1112 for the process which will run. */)
1114 register Lisp_Object process
, flag
;
1116 CHECK_PROCESS (process
);
1117 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1121 DEFUN ("process-inherit-coding-system-flag",
1122 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1124 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1125 If this flag is t, `buffer-file-coding-system' of the buffer
1126 associated with PROCESS will inherit the coding system used to decode
1127 the process output. */)
1129 register Lisp_Object process
;
1131 CHECK_PROCESS (process
);
1132 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
1135 DEFUN ("set-process-query-on-exit-flag",
1136 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1138 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1139 If the second argument FLAG is non-nil, Emacs will query the user before
1140 exiting if PROCESS is running. */)
1142 register Lisp_Object process
, flag
;
1144 CHECK_PROCESS (process
);
1145 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1149 DEFUN ("process-query-on-exit-flag",
1150 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1152 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1154 register Lisp_Object process
;
1156 CHECK_PROCESS (process
);
1157 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1160 #ifdef DATAGRAM_SOCKETS
1161 Lisp_Object
Fprocess_datagram_address ();
1164 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1166 doc
: /* Return the contact info of PROCESS; t for a real child.
1167 For a net connection, the value depends on the optional KEY arg.
1168 If KEY is nil, value is a cons cell of the form (HOST SERVICE),
1169 if KEY is t, the complete contact information for the connection is
1170 returned, else the specific value for the keyword KEY is returned.
1171 See `make-network-process' for a list of keywords. */)
1173 register Lisp_Object process
, key
;
1175 Lisp_Object contact
;
1177 CHECK_PROCESS (process
);
1178 contact
= XPROCESS (process
)->childp
;
1180 #ifdef DATAGRAM_SOCKETS
1181 if (DATAGRAM_CONN_P (process
)
1182 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1183 contact
= Fplist_put (contact
, QCremote
,
1184 Fprocess_datagram_address (process
));
1187 if (!NETCONN_P (process
) || EQ (key
, Qt
))
1190 return Fcons (Fplist_get (contact
, QChost
),
1191 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1192 return Fplist_get (contact
, key
);
1195 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1197 doc
: /* Return the plist of PROCESS. */)
1199 register Lisp_Object process
;
1201 CHECK_PROCESS (process
);
1202 return XPROCESS (process
)->plist
;
1205 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1207 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1209 register Lisp_Object process
, plist
;
1211 CHECK_PROCESS (process
);
1214 XPROCESS (process
)->plist
= plist
;
1218 #if 0 /* Turned off because we don't currently record this info
1219 in the process. Perhaps add it. */
1220 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1221 doc
: /* Return the connection type of PROCESS.
1222 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1223 a socket connection. */)
1225 Lisp_Object process
;
1227 return XPROCESS (process
)->type
;
1232 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1234 doc
: /* Convert network ADDRESS from internal format to a string.
1235 A 4 or 5 element vector represents an IPv4 address (with port number).
1236 An 8 or 9 element vector represents an IPv6 address (with port number).
1237 If optional second argument OMIT-PORT is non-nil, don't include a port
1238 number in the string, even when present in ADDRESS.
1239 Returns nil if format of ADDRESS is invalid. */)
1240 (address
, omit_port
)
1241 Lisp_Object address
, omit_port
;
1246 if (STRINGP (address
)) /* AF_LOCAL */
1249 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1251 register struct Lisp_Vector
*p
= XVECTOR (address
);
1252 Lisp_Object args
[10];
1255 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1257 args
[0] = build_string ("%d.%d.%d.%d");
1260 else if (p
->size
== 5)
1262 args
[0] = build_string ("%d.%d.%d.%d:%d");
1265 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1267 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1270 else if (p
->size
== 9)
1272 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1278 for (i
= 0; i
< nargs
; i
++)
1280 EMACS_INT element
= XINT (p
->contents
[i
]);
1282 if (element
< 0 || element
> 65535)
1285 if (nargs
<= 5 /* IPv4 */
1286 && i
< 4 /* host, not port */
1290 args
[i
+1] = p
->contents
[i
];
1293 return Fformat (nargs
+1, args
);
1296 if (CONSP (address
))
1298 Lisp_Object args
[2];
1299 args
[0] = build_string ("<Family %d>");
1300 args
[1] = Fcar (address
);
1301 return Fformat (2, args
);
1309 list_processes_1 (query_only
)
1310 Lisp_Object query_only
;
1312 register Lisp_Object tail
, tem
;
1313 Lisp_Object proc
, minspace
, tem1
;
1314 register struct Lisp_Process
*p
;
1316 int w_proc
, w_buffer
, w_tty
;
1318 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1320 w_proc
= 4; /* Proc */
1321 w_buffer
= 6; /* Buffer */
1322 w_tty
= 0; /* Omit if no ttys */
1324 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1328 proc
= Fcdr (XCAR (tail
));
1329 p
= XPROCESS (proc
);
1330 if (NILP (p
->childp
))
1332 if (!NILP (query_only
) && p
->kill_without_query
)
1334 if (STRINGP (p
->name
)
1335 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1337 if (!NILP (p
->buffer
))
1339 if (NILP (XBUFFER (p
->buffer
)->name
))
1342 w_buffer
= 8; /* (Killed) */
1344 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1347 if (STRINGP (p
->tty_name
)
1348 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1352 XSETFASTINT (i_status
, w_proc
+ 1);
1353 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1356 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1357 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1362 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1365 XSETFASTINT (minspace
, 1);
1367 set_buffer_internal (XBUFFER (Vstandard_output
));
1368 current_buffer
->undo_list
= Qt
;
1370 current_buffer
->truncate_lines
= Qt
;
1372 write_string ("Proc", -1);
1373 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1374 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1377 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1379 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1380 write_string ("\n", -1);
1382 write_string ("----", -1);
1383 Findent_to (i_status
, minspace
); write_string ("------", -1);
1384 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1387 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1389 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1390 write_string ("\n", -1);
1392 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1396 proc
= Fcdr (XCAR (tail
));
1397 p
= XPROCESS (proc
);
1398 if (NILP (p
->childp
))
1400 if (!NILP (query_only
) && p
->kill_without_query
)
1403 Finsert (1, &p
->name
);
1404 Findent_to (i_status
, minspace
);
1406 if (p
->raw_status_new
)
1409 if (CONSP (p
->status
))
1410 symbol
= XCAR (p
->status
);
1412 if (EQ (symbol
, Qsignal
))
1415 tem
= Fcar (Fcdr (p
->status
));
1417 if (XINT (tem
) < NSIG
)
1418 write_string (sys_errlist
[XINT (tem
)], -1);
1421 Fprinc (symbol
, Qnil
);
1423 else if (NETCONN1_P (p
))
1425 if (EQ (symbol
, Qexit
))
1426 write_string ("closed", -1);
1427 else if (EQ (p
->command
, Qt
))
1428 write_string ("stopped", -1);
1429 else if (EQ (symbol
, Qrun
))
1430 write_string ("open", -1);
1432 Fprinc (symbol
, Qnil
);
1435 Fprinc (symbol
, Qnil
);
1437 if (EQ (symbol
, Qexit
))
1440 tem
= Fcar (Fcdr (p
->status
));
1443 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1444 write_string (tembuf
, -1);
1448 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1451 Findent_to (i_buffer
, minspace
);
1452 if (NILP (p
->buffer
))
1453 insert_string ("(none)");
1454 else if (NILP (XBUFFER (p
->buffer
)->name
))
1455 insert_string ("(Killed)");
1457 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1461 Findent_to (i_tty
, minspace
);
1462 if (STRINGP (p
->tty_name
))
1463 Finsert (1, &p
->tty_name
);
1466 Findent_to (i_command
, minspace
);
1468 if (EQ (p
->status
, Qlisten
))
1470 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1471 if (INTEGERP (port
))
1472 port
= Fnumber_to_string (port
);
1474 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1475 sprintf (tembuf
, "(network %s server on %s)\n",
1476 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1477 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1478 insert_string (tembuf
);
1480 else if (NETCONN1_P (p
))
1482 /* For a local socket, there is no host name,
1483 so display service instead. */
1484 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1485 if (!STRINGP (host
))
1487 host
= Fplist_get (p
->childp
, QCservice
);
1488 if (INTEGERP (host
))
1489 host
= Fnumber_to_string (host
);
1492 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1493 sprintf (tembuf
, "(network %s connection to %s)\n",
1494 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1495 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1496 insert_string (tembuf
);
1508 insert_string (" ");
1510 insert_string ("\n");
1514 status_notify (NULL
);
1518 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1519 doc
: /* Display a list of all processes.
1520 If optional argument QUERY-ONLY is non-nil, only processes with
1521 the query-on-exit flag set will be listed.
1522 Any process listed as exited or signaled is actually eliminated
1523 after the listing is made. */)
1525 Lisp_Object query_only
;
1527 internal_with_output_to_temp_buffer ("*Process List*",
1528 list_processes_1
, query_only
);
1532 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1533 doc
: /* Return a list of all processes. */)
1536 return Fmapcar (Qcdr
, Vprocess_alist
);
1539 /* Starting asynchronous inferior processes. */
1541 static Lisp_Object
start_process_unwind ();
1543 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1544 doc
: /* Start a program in a subprocess. Return the process object for it.
1545 NAME is name for process. It is modified if necessary to make it unique.
1546 BUFFER is the buffer (or buffer name) to associate with the process.
1548 Process output (both standard output and standard error streams) goes
1549 at end of BUFFER, unless you specify an output stream or filter
1550 function to handle the output. BUFFER may also be nil, meaning that
1551 this process is not associated with any buffer.
1553 PROGRAM is the program file name. It is searched for in PATH.
1554 Remaining arguments are strings to give program as arguments.
1556 If you want to separate standard output from standard error, invoke
1557 the command through a shell and redirect one of them using the shell
1560 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1563 register Lisp_Object
*args
;
1565 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1567 register unsigned char *new_argv
;
1570 register unsigned char **new_argv
;
1573 int count
= SPECPDL_INDEX ();
1577 buffer
= Fget_buffer_create (buffer
);
1579 /* Make sure that the child will be able to chdir to the current
1580 buffer's current directory, or its unhandled equivalent. We
1581 can't just have the child check for an error when it does the
1582 chdir, since it's in a vfork.
1584 We have to GCPRO around this because Fexpand_file_name and
1585 Funhandled_file_name_directory might call a file name handling
1586 function. The argument list is protected by the caller, so all
1587 we really have to worry about is buffer. */
1589 struct gcpro gcpro1
, gcpro2
;
1591 current_dir
= current_buffer
->directory
;
1593 GCPRO2 (buffer
, current_dir
);
1595 current_dir
= Funhandled_file_name_directory (current_dir
);
1596 if (NILP (current_dir
))
1597 /* If the file name handler says that current_dir is unreachable, use
1598 a sensible default. */
1599 current_dir
= build_string ("~/");
1600 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1601 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1602 report_file_error ("Setting current directory",
1603 Fcons (current_buffer
->directory
, Qnil
));
1609 CHECK_STRING (name
);
1613 CHECK_STRING (program
);
1615 proc
= make_process (name
);
1616 /* If an error occurs and we can't start the process, we want to
1617 remove it from the process list. This means that each error
1618 check in create_process doesn't need to call remove_process
1619 itself; it's all taken care of here. */
1620 record_unwind_protect (start_process_unwind
, proc
);
1622 XPROCESS (proc
)->childp
= Qt
;
1623 XPROCESS (proc
)->plist
= Qnil
;
1624 XPROCESS (proc
)->buffer
= buffer
;
1625 XPROCESS (proc
)->sentinel
= Qnil
;
1626 XPROCESS (proc
)->filter
= Qnil
;
1627 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1629 #ifdef ADAPTIVE_READ_BUFFERING
1630 XPROCESS (proc
)->adaptive_read_buffering
1631 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1632 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1635 /* Make the process marker point into the process buffer (if any). */
1636 if (BUFFERP (buffer
))
1637 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1638 BUF_ZV (XBUFFER (buffer
)),
1639 BUF_ZV_BYTE (XBUFFER (buffer
)));
1642 /* Decide coding systems for communicating with the process. Here
1643 we don't setup the structure coding_system nor pay attention to
1644 unibyte mode. They are done in create_process. */
1646 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1647 Lisp_Object coding_systems
= Qt
;
1648 Lisp_Object val
, *args2
;
1649 struct gcpro gcpro1
, gcpro2
;
1651 val
= Vcoding_system_for_read
;
1654 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1655 args2
[0] = Qstart_process
;
1656 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1657 GCPRO2 (proc
, current_dir
);
1658 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1660 if (CONSP (coding_systems
))
1661 val
= XCAR (coding_systems
);
1662 else if (CONSP (Vdefault_process_coding_system
))
1663 val
= XCAR (Vdefault_process_coding_system
);
1665 XPROCESS (proc
)->decode_coding_system
= val
;
1667 val
= Vcoding_system_for_write
;
1670 if (EQ (coding_systems
, Qt
))
1672 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1673 args2
[0] = Qstart_process
;
1674 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1675 GCPRO2 (proc
, current_dir
);
1676 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1679 if (CONSP (coding_systems
))
1680 val
= XCDR (coding_systems
);
1681 else if (CONSP (Vdefault_process_coding_system
))
1682 val
= XCDR (Vdefault_process_coding_system
);
1684 XPROCESS (proc
)->encode_coding_system
= val
;
1688 /* Make a one member argv with all args concatenated
1689 together separated by a blank. */
1690 len
= SBYTES (program
) + 2;
1691 for (i
= 3; i
< nargs
; i
++)
1695 len
+= SBYTES (tem
) + 1; /* count the blank */
1697 new_argv
= (unsigned char *) alloca (len
);
1698 strcpy (new_argv
, SDATA (program
));
1699 for (i
= 3; i
< nargs
; i
++)
1703 strcat (new_argv
, " ");
1704 strcat (new_argv
, SDATA (tem
));
1706 /* Need to add code here to check for program existence on VMS */
1709 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1711 /* If program file name is not absolute, search our path for it.
1712 Put the name we will really use in TEM. */
1713 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1714 && !(SCHARS (program
) > 1
1715 && IS_DEVICE_SEP (SREF (program
, 1))))
1717 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1720 GCPRO4 (name
, program
, buffer
, current_dir
);
1721 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1724 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1725 tem
= Fexpand_file_name (tem
, Qnil
);
1729 if (!NILP (Ffile_directory_p (program
)))
1730 error ("Specified program for new process is a directory");
1734 /* If program file name starts with /: for quoting a magic name,
1736 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1737 && SREF (tem
, 1) == ':')
1738 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1740 /* Encode the file name and put it in NEW_ARGV.
1741 That's where the child will use it to execute the program. */
1742 tem
= ENCODE_FILE (tem
);
1743 new_argv
[0] = SDATA (tem
);
1745 /* Here we encode arguments by the coding system used for sending
1746 data to the process. We don't support using different coding
1747 systems for encoding arguments and for encoding data sent to the
1750 for (i
= 3; i
< nargs
; i
++)
1754 if (STRING_MULTIBYTE (tem
))
1755 tem
= (code_convert_string_norecord
1756 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1757 new_argv
[i
- 2] = SDATA (tem
);
1759 new_argv
[i
- 2] = 0;
1760 #endif /* not VMS */
1762 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1763 XPROCESS (proc
)->decoding_carryover
= 0;
1764 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1766 XPROCESS (proc
)->inherit_coding_system_flag
1767 = !(NILP (buffer
) || !inherit_process_coding_system
);
1769 create_process (proc
, (char **) new_argv
, current_dir
);
1771 return unbind_to (count
, proc
);
1774 /* This function is the unwind_protect form for Fstart_process. If
1775 PROC doesn't have its pid set, then we know someone has signaled
1776 an error and the process wasn't started successfully, so we should
1777 remove it from the process list. */
1779 start_process_unwind (proc
)
1782 if (!PROCESSP (proc
))
1785 /* Was PROC started successfully? */
1786 if (XPROCESS (proc
)->pid
<= 0)
1787 remove_process (proc
);
1793 create_process_1 (timer
)
1794 struct atimer
*timer
;
1796 /* Nothing to do. */
1800 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1803 /* Mimic blocking of signals on system V, which doesn't really have it. */
1805 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1806 int sigchld_deferred
;
1809 create_process_sigchld ()
1811 signal (SIGCHLD
, create_process_sigchld
);
1813 sigchld_deferred
= 1;
1819 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1821 create_process (process
, new_argv
, current_dir
)
1822 Lisp_Object process
;
1824 Lisp_Object current_dir
;
1826 int inchannel
, outchannel
;
1829 #ifdef POSIX_SIGNALS
1832 struct sigaction sigint_action
;
1833 struct sigaction sigquit_action
;
1835 struct sigaction sighup_action
;
1837 #else /* !POSIX_SIGNALS */
1840 SIGTYPE (*sigchld
)();
1843 #endif /* !POSIX_SIGNALS */
1844 /* Use volatile to protect variables from being clobbered by longjmp. */
1845 volatile int forkin
, forkout
;
1846 volatile int pty_flag
= 0;
1848 extern char **environ
;
1851 inchannel
= outchannel
= -1;
1854 if (!NILP (Vprocess_connection_type
))
1855 outchannel
= inchannel
= allocate_pty ();
1859 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1860 /* On most USG systems it does not work to open the pty's tty here,
1861 then close it and reopen it in the child. */
1863 /* Don't let this terminal become our controlling terminal
1864 (in case we don't have one). */
1865 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1867 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1870 report_file_error ("Opening pty", Qnil
);
1871 #if defined (DONT_REOPEN_PTY)
1872 /* In the case that vfork is defined as fork, the parent process
1873 (Emacs) may send some data before the child process completes
1874 tty options setup. So we setup tty before forking. */
1875 child_setup_tty (forkout
);
1876 #endif /* DONT_REOPEN_PTY */
1878 forkin
= forkout
= -1;
1879 #endif /* not USG, or USG_SUBTTY_WORKS */
1883 #endif /* HAVE_PTYS */
1888 report_file_error ("Creating pipe", Qnil
);
1894 emacs_close (inchannel
);
1895 emacs_close (forkout
);
1896 report_file_error ("Creating pipe", Qnil
);
1903 /* Replaced by close_process_descs */
1904 set_exclusive_use (inchannel
);
1905 set_exclusive_use (outchannel
);
1909 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1910 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1913 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1914 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1918 /* Record this as an active process, with its channels.
1919 As a result, child_setup will close Emacs's side of the pipes. */
1920 chan_process
[inchannel
] = process
;
1921 XPROCESS (process
)->infd
= inchannel
;
1922 XPROCESS (process
)->outfd
= outchannel
;
1924 /* Previously we recorded the tty descriptor used in the subprocess.
1925 It was only used for getting the foreground tty process, so now
1926 we just reopen the device (see emacs_get_tty_pgrp) as this is
1927 more portable (see USG_SUBTTY_WORKS above). */
1929 XPROCESS (process
)->pty_flag
= pty_flag
;
1930 XPROCESS (process
)->status
= Qrun
;
1931 setup_process_coding_systems (process
);
1933 /* Delay interrupts until we have a chance to store
1934 the new fork's pid in its process structure */
1935 #ifdef POSIX_SIGNALS
1936 sigemptyset (&blocked
);
1938 sigaddset (&blocked
, SIGCHLD
);
1940 #ifdef HAVE_WORKING_VFORK
1941 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1942 this sets the parent's signal handlers as well as the child's.
1943 So delay all interrupts whose handlers the child might munge,
1944 and record the current handlers so they can be restored later. */
1945 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1946 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1948 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1950 #endif /* HAVE_WORKING_VFORK */
1951 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1952 #else /* !POSIX_SIGNALS */
1956 #else /* not BSD4_1 */
1957 #if defined (BSD_SYSTEM) || defined (HPUX)
1958 sigsetmask (sigmask (SIGCHLD
));
1959 #else /* ordinary USG */
1961 sigchld_deferred
= 0;
1962 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1964 #endif /* ordinary USG */
1965 #endif /* not BSD4_1 */
1966 #endif /* SIGCHLD */
1967 #endif /* !POSIX_SIGNALS */
1969 FD_SET (inchannel
, &input_wait_mask
);
1970 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1971 if (inchannel
> max_process_desc
)
1972 max_process_desc
= inchannel
;
1974 /* Until we store the proper pid, enable sigchld_handler
1975 to recognize an unknown pid as standing for this process.
1976 It is very important not to let this `marker' value stay
1977 in the table after this function has returned; if it does
1978 it might cause call-process to hang and subsequent asynchronous
1979 processes to get their return values scrambled. */
1980 XPROCESS (process
)->pid
= -1;
1985 /* child_setup must clobber environ on systems with true vfork.
1986 Protect it from permanent change. */
1987 char **save_environ
= environ
;
1989 current_dir
= ENCODE_FILE (current_dir
);
1994 #endif /* not WINDOWSNT */
1996 int xforkin
= forkin
;
1997 int xforkout
= forkout
;
1999 #if 0 /* This was probably a mistake--it duplicates code later on,
2000 but fails to handle all the cases. */
2001 /* Make sure SIGCHLD is not blocked in the child. */
2002 sigsetmask (SIGEMPTYMASK
);
2005 /* Make the pty be the controlling terminal of the process. */
2007 /* First, disconnect its current controlling terminal. */
2009 /* We tried doing setsid only if pty_flag, but it caused
2010 process_set_signal to fail on SGI when using a pipe. */
2012 /* Make the pty's terminal the controlling terminal. */
2016 /* We ignore the return value
2017 because faith@cs.unc.edu says that is necessary on Linux. */
2018 ioctl (xforkin
, TIOCSCTTY
, 0);
2021 #else /* not HAVE_SETSID */
2023 /* It's very important to call setpgrp here and no time
2024 afterwards. Otherwise, we lose our controlling tty which
2025 is set when we open the pty. */
2028 #endif /* not HAVE_SETSID */
2029 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2030 if (pty_flag
&& xforkin
>= 0)
2033 tcgetattr (xforkin
, &t
);
2035 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2036 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2039 #if defined (NTTYDISC) && defined (TIOCSETD)
2040 if (pty_flag
&& xforkin
>= 0)
2042 /* Use new line discipline. */
2043 int ldisc
= NTTYDISC
;
2044 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2049 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2050 can do TIOCSPGRP only to the process's controlling tty. */
2053 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2054 I can't test it since I don't have 4.3. */
2055 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2056 ioctl (j
, TIOCNOTTY
, 0);
2059 /* In order to get a controlling terminal on some versions
2060 of BSD, it is necessary to put the process in pgrp 0
2061 before it opens the terminal. */
2069 #endif /* TIOCNOTTY */
2071 #if !defined (DONT_REOPEN_PTY)
2072 /*** There is a suggestion that this ought to be a
2073 conditional on TIOCSPGRP,
2074 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2075 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2076 that system does seem to need this code, even though
2077 both HAVE_SETSID and TIOCSCTTY are defined. */
2078 /* Now close the pty (if we had it open) and reopen it.
2079 This makes the pty the controlling terminal of the subprocess. */
2082 #ifdef SET_CHILD_PTY_PGRP
2083 int pgrp
= getpid ();
2086 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2089 emacs_close (xforkin
);
2090 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2094 emacs_write (1, "Couldn't open the pty terminal ", 31);
2095 emacs_write (1, pty_name
, strlen (pty_name
));
2096 emacs_write (1, "\n", 1);
2100 #ifdef SET_CHILD_PTY_PGRP
2101 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
2102 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
2105 #endif /* not DONT_REOPEN_PTY */
2107 #ifdef SETUP_SLAVE_PTY
2112 #endif /* SETUP_SLAVE_PTY */
2114 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2115 Now reenable it in the child, so it will die when we want it to. */
2117 signal (SIGHUP
, SIG_DFL
);
2119 #endif /* HAVE_PTYS */
2121 signal (SIGINT
, SIG_DFL
);
2122 signal (SIGQUIT
, SIG_DFL
);
2124 /* Stop blocking signals in the child. */
2125 #ifdef POSIX_SIGNALS
2126 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2127 #else /* !POSIX_SIGNALS */
2131 #else /* not BSD4_1 */
2132 #if defined (BSD_SYSTEM) || defined (HPUX)
2133 sigsetmask (SIGEMPTYMASK
);
2134 #else /* ordinary USG */
2136 signal (SIGCHLD
, sigchld
);
2138 #endif /* ordinary USG */
2139 #endif /* not BSD4_1 */
2140 #endif /* SIGCHLD */
2141 #endif /* !POSIX_SIGNALS */
2143 #if !defined (DONT_REOPEN_PTY)
2145 child_setup_tty (xforkout
);
2146 #endif /* not DONT_REOPEN_PTY */
2148 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2149 new_argv
, 1, current_dir
);
2150 #else /* not WINDOWSNT */
2151 child_setup (xforkin
, xforkout
, xforkout
,
2152 new_argv
, 1, current_dir
);
2153 #endif /* not WINDOWSNT */
2155 environ
= save_environ
;
2160 /* This runs in the Emacs process. */
2164 emacs_close (forkin
);
2165 if (forkin
!= forkout
&& forkout
>= 0)
2166 emacs_close (forkout
);
2170 /* vfork succeeded. */
2171 XPROCESS (process
)->pid
= pid
;
2174 register_child (pid
, inchannel
);
2175 #endif /* WINDOWSNT */
2177 /* If the subfork execv fails, and it exits,
2178 this close hangs. I don't know why.
2179 So have an interrupt jar it loose. */
2181 struct atimer
*timer
;
2185 EMACS_SET_SECS_USECS (offset
, 1, 0);
2186 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2189 emacs_close (forkin
);
2191 cancel_atimer (timer
);
2195 if (forkin
!= forkout
&& forkout
>= 0)
2196 emacs_close (forkout
);
2200 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2203 XPROCESS (process
)->tty_name
= Qnil
;
2206 /* Restore the signal state whether vfork succeeded or not.
2207 (We will signal an error, below, if it failed.) */
2208 #ifdef POSIX_SIGNALS
2209 #ifdef HAVE_WORKING_VFORK
2210 /* Restore the parent's signal handlers. */
2211 sigaction (SIGINT
, &sigint_action
, 0);
2212 sigaction (SIGQUIT
, &sigquit_action
, 0);
2214 sigaction (SIGHUP
, &sighup_action
, 0);
2216 #endif /* HAVE_WORKING_VFORK */
2217 /* Stop blocking signals in the parent. */
2218 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2219 #else /* !POSIX_SIGNALS */
2223 #else /* not BSD4_1 */
2224 #if defined (BSD_SYSTEM) || defined (HPUX)
2225 sigsetmask (SIGEMPTYMASK
);
2226 #else /* ordinary USG */
2228 signal (SIGCHLD
, sigchld
);
2229 /* Now really handle any of these signals
2230 that came in during this function. */
2231 if (sigchld_deferred
)
2232 kill (getpid (), SIGCHLD
);
2234 #endif /* ordinary USG */
2235 #endif /* not BSD4_1 */
2236 #endif /* SIGCHLD */
2237 #endif /* !POSIX_SIGNALS */
2239 /* Now generate the error if vfork failed. */
2241 report_file_error ("Doing vfork", Qnil
);
2243 #endif /* not VMS */
2248 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2249 The address family of sa is not included in the result. */
2252 conv_sockaddr_to_lisp (sa
, len
)
2253 struct sockaddr
*sa
;
2256 Lisp_Object address
;
2259 register struct Lisp_Vector
*p
;
2261 switch (sa
->sa_family
)
2265 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2266 len
= sizeof (sin
->sin_addr
) + 1;
2267 address
= Fmake_vector (make_number (len
), Qnil
);
2268 p
= XVECTOR (address
);
2269 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2270 cp
= (unsigned char *)&sin
->sin_addr
;
2276 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2277 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2278 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2279 address
= Fmake_vector (make_number (len
), Qnil
);
2280 p
= XVECTOR (address
);
2281 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2282 for (i
= 0; i
< len
; i
++)
2283 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2287 #ifdef HAVE_LOCAL_SOCKETS
2290 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2291 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2292 if (sockun
->sun_path
[i
] == 0)
2294 return make_unibyte_string (sockun
->sun_path
, i
);
2298 len
-= sizeof (sa
->sa_family
);
2299 address
= Fcons (make_number (sa
->sa_family
),
2300 Fmake_vector (make_number (len
), Qnil
));
2301 p
= XVECTOR (XCDR (address
));
2302 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2308 p
->contents
[i
++] = make_number (*cp
++);
2314 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2317 get_lisp_to_sockaddr_size (address
, familyp
)
2318 Lisp_Object address
;
2321 register struct Lisp_Vector
*p
;
2323 if (VECTORP (address
))
2325 p
= XVECTOR (address
);
2329 return sizeof (struct sockaddr_in
);
2332 else if (p
->size
== 9)
2334 *familyp
= AF_INET6
;
2335 return sizeof (struct sockaddr_in6
);
2339 #ifdef HAVE_LOCAL_SOCKETS
2340 else if (STRINGP (address
))
2342 *familyp
= AF_LOCAL
;
2343 return sizeof (struct sockaddr_un
);
2346 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2348 struct sockaddr
*sa
;
2349 *familyp
= XINT (XCAR (address
));
2350 p
= XVECTOR (XCDR (address
));
2351 return p
->size
+ sizeof (sa
->sa_family
);
2356 /* Convert an address object (vector or string) to an internal sockaddr.
2358 The address format has been basically validated by
2359 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2360 it could have come from user data. So if FAMILY is not valid,
2361 we return after zeroing *SA. */
2364 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2366 Lisp_Object address
;
2367 struct sockaddr
*sa
;
2370 register struct Lisp_Vector
*p
;
2371 register unsigned char *cp
= NULL
;
2376 if (VECTORP (address
))
2378 p
= XVECTOR (address
);
2379 if (family
== AF_INET
)
2381 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2382 len
= sizeof (sin
->sin_addr
) + 1;
2383 i
= XINT (p
->contents
[--len
]);
2384 sin
->sin_port
= htons (i
);
2385 cp
= (unsigned char *)&sin
->sin_addr
;
2386 sa
->sa_family
= family
;
2389 else if (family
== AF_INET6
)
2391 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2392 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2393 len
= sizeof (sin6
->sin6_addr
) + 1;
2394 i
= XINT (p
->contents
[--len
]);
2395 sin6
->sin6_port
= htons (i
);
2396 for (i
= 0; i
< len
; i
++)
2397 if (INTEGERP (p
->contents
[i
]))
2399 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2402 sa
->sa_family
= family
;
2407 else if (STRINGP (address
))
2409 #ifdef HAVE_LOCAL_SOCKETS
2410 if (family
== AF_LOCAL
)
2412 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2413 cp
= SDATA (address
);
2414 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2415 sockun
->sun_path
[i
] = *cp
++;
2416 sa
->sa_family
= family
;
2423 p
= XVECTOR (XCDR (address
));
2424 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2427 for (i
= 0; i
< len
; i
++)
2428 if (INTEGERP (p
->contents
[i
]))
2429 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2432 #ifdef DATAGRAM_SOCKETS
2433 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2435 doc
: /* Get the current datagram address associated with PROCESS. */)
2437 Lisp_Object process
;
2441 CHECK_PROCESS (process
);
2443 if (!DATAGRAM_CONN_P (process
))
2446 channel
= XPROCESS (process
)->infd
;
2447 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2448 datagram_address
[channel
].len
);
2451 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2453 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2454 Returns nil upon error setting address, ADDRESS otherwise. */)
2456 Lisp_Object process
, address
;
2461 CHECK_PROCESS (process
);
2463 if (!DATAGRAM_CONN_P (process
))
2466 channel
= XPROCESS (process
)->infd
;
2468 len
= get_lisp_to_sockaddr_size (address
, &family
);
2469 if (datagram_address
[channel
].len
!= len
)
2471 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2477 static struct socket_options
{
2478 /* The name of this option. Should be lowercase version of option
2479 name without SO_ prefix. */
2481 /* Option level SOL_... */
2483 /* Option number SO_... */
2485 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2486 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2487 } socket_options
[] =
2489 #ifdef SO_BINDTODEVICE
2490 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2493 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2496 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2499 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2502 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2505 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2508 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2511 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2513 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2516 /* Set option OPT to value VAL on socket S.
2518 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2519 Signals an error if setting a known option fails.
2523 set_socket_option (s
, opt
, val
)
2525 Lisp_Object opt
, val
;
2528 struct socket_options
*sopt
;
2533 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2534 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2535 if (strcmp (name
, sopt
->name
) == 0)
2538 switch (sopt
->opttype
)
2543 optval
= NILP (val
) ? 0 : 1;
2544 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2545 &optval
, sizeof (optval
));
2553 optval
= XINT (val
);
2555 error ("Bad option value for %s", name
);
2556 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2557 &optval
, sizeof (optval
));
2561 #ifdef SO_BINDTODEVICE
2564 char devname
[IFNAMSIZ
+1];
2566 /* This is broken, at least in the Linux 2.4 kernel.
2567 To unbind, the arg must be a zero integer, not the empty string.
2568 This should work on all systems. KFS. 2003-09-23. */
2569 bzero (devname
, sizeof devname
);
2572 char *arg
= (char *) SDATA (val
);
2573 int len
= min (strlen (arg
), IFNAMSIZ
);
2574 bcopy (arg
, devname
, len
);
2576 else if (!NILP (val
))
2577 error ("Bad option value for %s", name
);
2578 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2587 struct linger linger
;
2590 linger
.l_linger
= 0;
2592 linger
.l_linger
= XINT (val
);
2594 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2595 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2596 &linger
, sizeof (linger
));
2606 report_file_error ("Cannot set network option",
2607 Fcons (opt
, Fcons (val
, Qnil
)));
2608 return (1 << sopt
->optbit
);
2612 DEFUN ("set-network-process-option",
2613 Fset_network_process_option
, Sset_network_process_option
,
2615 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2616 See `make-network-process' for a list of options and values.
2617 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2618 OPTION is not a supported option, return nil instead; otherwise return t. */)
2619 (process
, option
, value
, no_error
)
2620 Lisp_Object process
, option
, value
;
2621 Lisp_Object no_error
;
2624 struct Lisp_Process
*p
;
2626 CHECK_PROCESS (process
);
2627 p
= XPROCESS (process
);
2628 if (!NETCONN1_P (p
))
2629 error ("Process is not a network process");
2633 error ("Process is not running");
2635 if (set_socket_option (s
, option
, value
))
2637 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2641 if (NILP (no_error
))
2642 error ("Unknown or unsupported option");
2648 /* A version of request_sigio suitable for a record_unwind_protect. */
2652 unwind_request_sigio (dummy
)
2655 if (interrupt_input
)
2661 /* Create a network stream/datagram client/server process. Treated
2662 exactly like a normal process when reading and writing. Primary
2663 differences are in status display and process deletion. A network
2664 connection has no PID; you cannot signal it. All you can do is
2665 stop/continue it and deactivate/close it via delete-process */
2667 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2669 doc
: /* Create and return a network server or client process.
2671 In Emacs, network connections are represented by process objects, so
2672 input and output work as for subprocesses and `delete-process' closes
2673 a network connection. However, a network process has no process id,
2674 it cannot be signaled, and the status codes are different from normal
2677 Arguments are specified as keyword/argument pairs. The following
2678 arguments are defined:
2680 :name NAME -- NAME is name for process. It is modified if necessary
2683 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2684 with the process. Process output goes at end of that buffer, unless
2685 you specify an output stream or filter function to handle the output.
2686 BUFFER may be also nil, meaning that this process is not associated
2689 :host HOST -- HOST is name of the host to connect to, or its IP
2690 address. The symbol `local' specifies the local host. If specified
2691 for a server process, it must be a valid name or address for the local
2692 host, and only clients connecting to that address will be accepted.
2694 :service SERVICE -- SERVICE is name of the service desired, or an
2695 integer specifying a port number to connect to. If SERVICE is t,
2696 a random port number is selected for the server. (If Emacs was
2697 compiled with getaddrinfo, a port number can also be specified as a
2698 string, e.g. "80", as well as an integer. This is not portable.)
2700 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2701 stream type connection, `datagram' creates a datagram type connection.
2703 :family FAMILY -- FAMILY is the address (and protocol) family for the
2704 service specified by HOST and SERVICE. The default (nil) is to use
2705 whatever address family (IPv4 or IPv6) that is defined for the host
2706 and port number specified by HOST and SERVICE. Other address families
2708 local -- for a local (i.e. UNIX) address specified by SERVICE.
2709 ipv4 -- use IPv4 address family only.
2710 ipv6 -- use IPv6 address family only.
2712 :local ADDRESS -- ADDRESS is the local address used for the connection.
2713 This parameter is ignored when opening a client process. When specified
2714 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2716 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2717 connection. This parameter is ignored when opening a stream server
2718 process. For a datagram server process, it specifies the initial
2719 setting of the remote datagram address. When specified for a client
2720 process, the FAMILY, HOST, and SERVICE args are ignored.
2722 The format of ADDRESS depends on the address family:
2723 - An IPv4 address is represented as an vector of integers [A B C D P]
2724 corresponding to numeric IP address A.B.C.D and port number P.
2725 - A local address is represented as a string with the address in the
2726 local address space.
2727 - An "unsupported family" address is represented by a cons (F . AV)
2728 where F is the family number and AV is a vector containing the socket
2729 address data with one element per address data byte. Do not rely on
2730 this format in portable code, as it may depend on implementation
2731 defined constants, data sizes, and data structure alignment.
2733 :coding CODING -- If CODING is a symbol, it specifies the coding
2734 system used for both reading and writing for this process. If CODING
2735 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2736 ENCODING is used for writing.
2738 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2739 return without waiting for the connection to complete; instead, the
2740 sentinel function will be called with second arg matching "open" (if
2741 successful) or "failed" when the connect completes. Default is to use
2742 a blocking connect (i.e. wait) for stream type connections.
2744 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2745 running when Emacs is exited.
2747 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2748 In the stopped state, a server process does not accept new
2749 connections, and a client process does not handle incoming traffic.
2750 The stopped state is cleared by `continue-process' and set by
2753 :filter FILTER -- Install FILTER as the process filter.
2755 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
2756 process filter are multibyte, otherwise they are unibyte.
2757 If this keyword is not specified, the strings are multibyte if
2758 `default-enable-multibyte-characters' is non-nil.
2760 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2762 :log LOG -- Install LOG as the server process log function. This
2763 function is called when the server accepts a network connection from a
2764 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2765 is the server process, CLIENT is the new process for the connection,
2766 and MESSAGE is a string.
2768 :plist PLIST -- Install PLIST as the new process' initial plist.
2770 :server QLEN -- if QLEN is non-nil, create a server process for the
2771 specified FAMILY, SERVICE, and connection type (stream or datagram).
2772 If QLEN is an integer, it is used as the max. length of the server's
2773 pending connection queue (also known as the backlog); the default
2774 queue length is 5. Default is to create a client process.
2776 The following network options can be specified for this connection:
2778 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2779 :dontroute BOOL -- Only send to directly connected hosts.
2780 :keepalive BOOL -- Send keep-alive messages on network stream.
2781 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2782 :oobinline BOOL -- Place out-of-band data in receive data stream.
2783 :priority INT -- Set protocol defined priority for sent packets.
2784 :reuseaddr BOOL -- Allow reusing a recently used local address
2785 (this is allowed by default for a server process).
2786 :bindtodevice NAME -- bind to interface NAME. Using this may require
2787 special privileges on some systems.
2789 Consult the relevant system programmer's manual pages for more
2790 information on using these options.
2793 A server process will listen for and accept connections from clients.
2794 When a client connection is accepted, a new network process is created
2795 for the connection with the following parameters:
2797 - The client's process name is constructed by concatenating the server
2798 process' NAME and a client identification string.
2799 - If the FILTER argument is non-nil, the client process will not get a
2800 separate process buffer; otherwise, the client's process buffer is a newly
2801 created buffer named after the server process' BUFFER name or process
2802 NAME concatenated with the client identification string.
2803 - The connection type and the process filter and sentinel parameters are
2804 inherited from the server process' TYPE, FILTER and SENTINEL.
2805 - The client process' contact info is set according to the client's
2806 addressing information (typically an IP address and a port number).
2807 - The client process' plist is initialized from the server's plist.
2809 Notice that the FILTER and SENTINEL args are never used directly by
2810 the server process. Also, the BUFFER argument is not used directly by
2811 the server process, but via the optional :log function, accepted (and
2812 failed) connections may be logged in the server process' buffer.
2814 The original argument list, modified with the actual connection
2815 information, is available via the `process-contact' function.
2817 usage: (make-network-process &rest ARGS) */)
2823 Lisp_Object contact
;
2824 struct Lisp_Process
*p
;
2825 #ifdef HAVE_GETADDRINFO
2826 struct addrinfo ai
, *res
, *lres
;
2827 struct addrinfo hints
;
2828 char *portstring
, portbuf
[128];
2829 #else /* HAVE_GETADDRINFO */
2830 struct _emacs_addrinfo
2836 struct sockaddr
*ai_addr
;
2837 struct _emacs_addrinfo
*ai_next
;
2839 #endif /* HAVE_GETADDRINFO */
2840 struct sockaddr_in address_in
;
2841 #ifdef HAVE_LOCAL_SOCKETS
2842 struct sockaddr_un address_un
;
2847 int s
= -1, outch
, inch
;
2848 struct gcpro gcpro1
;
2849 int count
= SPECPDL_INDEX ();
2851 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
2853 Lisp_Object name
, buffer
, host
, service
, address
;
2854 Lisp_Object filter
, sentinel
;
2855 int is_non_blocking_client
= 0;
2856 int is_server
= 0, backlog
= 5;
2863 /* Save arguments for process-contact and clone-process. */
2864 contact
= Flist (nargs
, args
);
2868 /* Ensure socket support is loaded if available. */
2869 init_winsock (TRUE
);
2872 /* :type TYPE (nil: stream, datagram */
2873 tem
= Fplist_get (contact
, QCtype
);
2875 socktype
= SOCK_STREAM
;
2876 #ifdef DATAGRAM_SOCKETS
2877 else if (EQ (tem
, Qdatagram
))
2878 socktype
= SOCK_DGRAM
;
2881 error ("Unsupported connection type");
2884 tem
= Fplist_get (contact
, QCserver
);
2887 /* Don't support network sockets when non-blocking mode is
2888 not available, since a blocked Emacs is not useful. */
2889 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY))
2890 error ("Network servers not supported");
2894 backlog
= XINT (tem
);
2898 /* Make QCaddress an alias for :local (server) or :remote (client). */
2899 QCaddress
= is_server
? QClocal
: QCremote
;
2902 if (!is_server
&& socktype
== SOCK_STREAM
2903 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
2905 #ifndef NON_BLOCKING_CONNECT
2906 error ("Non-blocking connect not supported");
2908 is_non_blocking_client
= 1;
2912 name
= Fplist_get (contact
, QCname
);
2913 buffer
= Fplist_get (contact
, QCbuffer
);
2914 filter
= Fplist_get (contact
, QCfilter
);
2915 sentinel
= Fplist_get (contact
, QCsentinel
);
2917 CHECK_STRING (name
);
2920 /* Let's handle TERM before things get complicated ... */
2921 host
= Fplist_get (contact
, QChost
);
2922 CHECK_STRING (host
);
2924 service
= Fplist_get (contact
, QCservice
);
2925 if (INTEGERP (service
))
2926 port
= htons ((unsigned short) XINT (service
));
2929 struct servent
*svc_info
;
2930 CHECK_STRING (service
);
2931 svc_info
= getservbyname (SDATA (service
), "tcp");
2933 error ("Unknown service: %s", SDATA (service
));
2934 port
= svc_info
->s_port
;
2937 s
= connect_server (0);
2939 report_file_error ("error creating socket", Fcons (name
, Qnil
));
2940 send_command (s
, C_PORT
, 0, "%s:%d", SDATA (host
), ntohs (port
));
2941 send_command (s
, C_DUMB
, 1, 0);
2943 #else /* not TERM */
2945 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
2946 ai
.ai_socktype
= socktype
;
2951 /* :local ADDRESS or :remote ADDRESS */
2952 address
= Fplist_get (contact
, QCaddress
);
2953 if (!NILP (address
))
2955 host
= service
= Qnil
;
2957 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
2958 error ("Malformed :address");
2959 ai
.ai_family
= family
;
2960 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
2961 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
2965 /* :family FAMILY -- nil (for Inet), local, or integer. */
2966 tem
= Fplist_get (contact
, QCfamily
);
2969 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
2975 #ifdef HAVE_LOCAL_SOCKETS
2976 else if (EQ (tem
, Qlocal
))
2980 else if (EQ (tem
, Qipv6
))
2983 else if (EQ (tem
, Qipv4
))
2985 else if (INTEGERP (tem
))
2986 family
= XINT (tem
);
2988 error ("Unknown address family");
2990 ai
.ai_family
= family
;
2992 /* :service SERVICE -- string, integer (port number), or t (random port). */
2993 service
= Fplist_get (contact
, QCservice
);
2995 #ifdef HAVE_LOCAL_SOCKETS
2996 if (family
== AF_LOCAL
)
2998 /* Host is not used. */
3000 CHECK_STRING (service
);
3001 bzero (&address_un
, sizeof address_un
);
3002 address_un
.sun_family
= AF_LOCAL
;
3003 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3004 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3005 ai
.ai_addrlen
= sizeof address_un
;
3010 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3011 host
= Fplist_get (contact
, QChost
);
3014 if (EQ (host
, Qlocal
))
3015 host
= build_string ("localhost");
3016 CHECK_STRING (host
);
3019 /* Slow down polling to every ten seconds.
3020 Some kernels have a bug which causes retrying connect to fail
3021 after a connect. Polling can interfere with gethostbyname too. */
3022 #ifdef POLL_FOR_INPUT
3023 if (socktype
== SOCK_STREAM
)
3025 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3026 bind_polling_period (10);
3030 #ifdef HAVE_GETADDRINFO
3031 /* If we have a host, use getaddrinfo to resolve both host and service.
3032 Otherwise, use getservbyname to lookup the service. */
3036 /* SERVICE can either be a string or int.
3037 Convert to a C string for later use by getaddrinfo. */
3038 if (EQ (service
, Qt
))
3040 else if (INTEGERP (service
))
3042 sprintf (portbuf
, "%ld", (long) XINT (service
));
3043 portstring
= portbuf
;
3047 CHECK_STRING (service
);
3048 portstring
= SDATA (service
);
3053 memset (&hints
, 0, sizeof (hints
));
3055 hints
.ai_family
= family
;
3056 hints
.ai_socktype
= socktype
;
3057 hints
.ai_protocol
= 0;
3059 #ifdef HAVE_RES_INIT
3063 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3065 #ifdef HAVE_GAI_STRERROR
3066 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3068 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3074 #endif /* HAVE_GETADDRINFO */
3076 /* We end up here if getaddrinfo is not defined, or in case no hostname
3077 has been specified (e.g. for a local server process). */
3079 if (EQ (service
, Qt
))
3081 else if (INTEGERP (service
))
3082 port
= htons ((unsigned short) XINT (service
));
3085 struct servent
*svc_info
;
3086 CHECK_STRING (service
);
3087 svc_info
= getservbyname (SDATA (service
),
3088 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3090 error ("Unknown service: %s", SDATA (service
));
3091 port
= svc_info
->s_port
;
3094 bzero (&address_in
, sizeof address_in
);
3095 address_in
.sin_family
= family
;
3096 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3097 address_in
.sin_port
= port
;
3099 #ifndef HAVE_GETADDRINFO
3102 struct hostent
*host_info_ptr
;
3104 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3105 as it may `hang' Emacs for a very long time. */
3109 #ifdef HAVE_RES_INIT
3113 host_info_ptr
= gethostbyname (SDATA (host
));
3118 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3119 host_info_ptr
->h_length
);
3120 family
= host_info_ptr
->h_addrtype
;
3121 address_in
.sin_family
= family
;
3124 /* Attempt to interpret host as numeric inet address */
3126 unsigned long numeric_addr
;
3127 numeric_addr
= inet_addr ((char *) SDATA (host
));
3128 if (numeric_addr
== -1)
3129 error ("Unknown host \"%s\"", SDATA (host
));
3131 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3132 sizeof (address_in
.sin_addr
));
3136 #endif /* not HAVE_GETADDRINFO */
3138 ai
.ai_family
= family
;
3139 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3140 ai
.ai_addrlen
= sizeof address_in
;
3145 /* Previously this was compiled unconditionally, but that seems
3146 unnecessary on modern systems, and `unrequest_sigio' was a noop
3147 under X anyway. --lorentey */
3148 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
3149 when connect is interrupted. So let's not let it get interrupted.
3150 Note we do not turn off polling, because polling is only used
3151 when not interrupt_input, and thus not normally used on the systems
3152 which have this bug. On systems which use polling, there's no way
3153 to quit if polling is turned off. */
3155 && !is_server
&& socktype
== SOCK_STREAM
)
3157 /* Comment from KFS: The original open-network-stream code
3158 didn't unwind protect this, but it seems like the proper
3159 thing to do. In any case, I don't see how it could harm to
3160 do this -- and it makes cleanup (using unbind_to) easier. */
3161 record_unwind_protect (unwind_request_sigio
, Qnil
);
3166 /* Do this in case we never enter the for-loop below. */
3167 count1
= SPECPDL_INDEX ();
3170 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3176 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3183 #ifdef DATAGRAM_SOCKETS
3184 if (!is_server
&& socktype
== SOCK_DGRAM
)
3186 #endif /* DATAGRAM_SOCKETS */
3188 #ifdef NON_BLOCKING_CONNECT
3189 if (is_non_blocking_client
)
3192 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3194 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3206 /* Make us close S if quit. */
3207 record_unwind_protect (close_file_unwind
, make_number (s
));
3209 /* Parse network options in the arg list.
3210 We simply ignore anything which isn't a known option (including other keywords).
3211 An error is signalled if setting a known option fails. */
3212 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3213 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3217 /* Configure as a server socket. */
3219 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3220 explicit :reuseaddr key to override this. */
3221 #ifdef HAVE_LOCAL_SOCKETS
3222 if (family
!= AF_LOCAL
)
3224 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3227 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3228 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3231 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3232 report_file_error ("Cannot bind server socket", Qnil
);
3234 #ifdef HAVE_GETSOCKNAME
3235 if (EQ (service
, Qt
))
3237 struct sockaddr_in sa1
;
3238 int len1
= sizeof (sa1
);
3239 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3241 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3242 service
= make_number (ntohs (sa1
.sin_port
));
3243 contact
= Fplist_put (contact
, QCservice
, service
);
3248 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3249 report_file_error ("Cannot listen on server socket", Qnil
);
3257 /* This turns off all alarm-based interrupts; the
3258 bind_polling_period call above doesn't always turn all the
3259 short-interval ones off, especially if interrupt_input is
3262 It'd be nice to be able to control the connect timeout
3263 though. Would non-blocking connect calls be portable?
3265 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3267 turn_on_atimers (0);
3269 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3272 turn_on_atimers (1);
3274 if (ret
== 0 || xerrno
== EISCONN
)
3276 /* The unwind-protect will be discarded afterwards.
3277 Likewise for immediate_quit. */
3281 #ifdef NON_BLOCKING_CONNECT
3283 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3287 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3295 /* Discard the unwind protect closing S. */
3296 specpdl_ptr
= specpdl
+ count1
;
3300 if (xerrno
== EINTR
)
3306 #ifdef DATAGRAM_SOCKETS
3307 if (socktype
== SOCK_DGRAM
)
3309 if (datagram_address
[s
].sa
)
3311 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3312 datagram_address
[s
].len
= lres
->ai_addrlen
;
3316 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3317 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3320 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3321 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3322 conv_lisp_to_sockaddr (rfamily
, remote
,
3323 datagram_address
[s
].sa
, rlen
);
3327 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3330 contact
= Fplist_put (contact
, QCaddress
,
3331 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3332 #ifdef HAVE_GETSOCKNAME
3335 struct sockaddr_in sa1
;
3336 int len1
= sizeof (sa1
);
3337 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3338 contact
= Fplist_put (contact
, QClocal
,
3339 conv_sockaddr_to_lisp (&sa1
, len1
));
3346 #ifdef HAVE_GETADDRINFO
3355 /* Discard the unwind protect for closing S, if any. */
3356 specpdl_ptr
= specpdl
+ count1
;
3358 /* Unwind bind_polling_period and request_sigio. */
3359 unbind_to (count
, Qnil
);
3363 /* If non-blocking got this far - and failed - assume non-blocking is
3364 not supported after all. This is probably a wrong assumption, but
3365 the normal blocking calls to open-network-stream handles this error
3367 if (is_non_blocking_client
)
3372 report_file_error ("make server process failed", contact
);
3374 report_file_error ("make client process failed", contact
);
3377 #endif /* not TERM */
3383 buffer
= Fget_buffer_create (buffer
);
3384 proc
= make_process (name
);
3386 chan_process
[inch
] = proc
;
3389 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3392 fcntl (inch
, F_SETFL
, O_NDELAY
);
3396 p
= XPROCESS (proc
);
3398 p
->childp
= contact
;
3399 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3402 p
->sentinel
= sentinel
;
3404 p
->log
= Fplist_get (contact
, QClog
);
3405 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3406 p
->kill_without_query
= 1;
3407 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3412 if (is_server
&& socktype
== SOCK_STREAM
)
3413 p
->status
= Qlisten
;
3415 /* Make the process marker point into the process buffer (if any). */
3416 if (BUFFERP (buffer
))
3417 set_marker_both (p
->mark
, buffer
,
3418 BUF_ZV (XBUFFER (buffer
)),
3419 BUF_ZV_BYTE (XBUFFER (buffer
)));
3421 #ifdef NON_BLOCKING_CONNECT
3422 if (is_non_blocking_client
)
3424 /* We may get here if connect did succeed immediately. However,
3425 in that case, we still need to signal this like a non-blocking
3427 p
->status
= Qconnect
;
3428 if (!FD_ISSET (inch
, &connect_wait_mask
))
3430 FD_SET (inch
, &connect_wait_mask
);
3431 num_pending_connects
++;
3436 /* A server may have a client filter setting of Qt, but it must
3437 still listen for incoming connects unless it is stopped. */
3438 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3439 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3441 FD_SET (inch
, &input_wait_mask
);
3442 FD_SET (inch
, &non_keyboard_wait_mask
);
3445 if (inch
> max_process_desc
)
3446 max_process_desc
= inch
;
3448 tem
= Fplist_member (contact
, QCcoding
);
3449 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3450 tem
= Qnil
; /* No error message (too late!). */
3453 /* Setup coding systems for communicating with the network stream. */
3454 struct gcpro gcpro1
;
3455 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3456 Lisp_Object coding_systems
= Qt
;
3457 Lisp_Object args
[5], val
;
3461 val
= XCAR (XCDR (tem
));
3465 else if (!NILP (Vcoding_system_for_read
))
3466 val
= Vcoding_system_for_read
;
3467 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3468 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3469 /* We dare not decode end-of-line format by setting VAL to
3470 Qraw_text, because the existing Emacs Lisp libraries
3471 assume that they receive bare code including a sequene of
3476 if (NILP (host
) || NILP (service
))
3477 coding_systems
= Qnil
;
3480 args
[0] = Qopen_network_stream
, args
[1] = name
,
3481 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3483 coding_systems
= Ffind_operation_coding_system (5, args
);
3486 if (CONSP (coding_systems
))
3487 val
= XCAR (coding_systems
);
3488 else if (CONSP (Vdefault_process_coding_system
))
3489 val
= XCAR (Vdefault_process_coding_system
);
3493 p
->decode_coding_system
= val
;
3497 val
= XCAR (XCDR (tem
));
3501 else if (!NILP (Vcoding_system_for_write
))
3502 val
= Vcoding_system_for_write
;
3503 else if (NILP (current_buffer
->enable_multibyte_characters
))
3507 if (EQ (coding_systems
, Qt
))
3509 if (NILP (host
) || NILP (service
))
3510 coding_systems
= Qnil
;
3513 args
[0] = Qopen_network_stream
, args
[1] = name
,
3514 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3516 coding_systems
= Ffind_operation_coding_system (5, args
);
3520 if (CONSP (coding_systems
))
3521 val
= XCDR (coding_systems
);
3522 else if (CONSP (Vdefault_process_coding_system
))
3523 val
= XCDR (Vdefault_process_coding_system
);
3527 p
->encode_coding_system
= val
;
3529 setup_process_coding_systems (proc
);
3531 p
->decoding_buf
= make_uninit_string (0);
3532 p
->decoding_carryover
= 0;
3533 p
->encoding_buf
= make_uninit_string (0);
3535 p
->inherit_coding_system_flag
3536 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3541 #endif /* HAVE_SOCKETS */
3544 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3547 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3548 doc
: /* Return an alist of all network interfaces and their network address.
3549 Each element is a cons, the car of which is a string containing the
3550 interface name, and the cdr is the network address in internal
3551 format; see the description of ADDRESS in `make-network-process'. */)
3554 struct ifconf ifconf
;
3555 struct ifreq
*ifreqs
= NULL
;
3560 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3566 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3567 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3574 ifconf
.ifc_len
= buf_size
;
3575 ifconf
.ifc_req
= ifreqs
;
3576 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3582 if (ifconf
.ifc_len
== buf_size
)
3586 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3589 while (--ifaces
>= 0)
3591 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3592 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3593 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3595 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3596 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3597 res
= Fcons (Fcons (build_string (namebuf
),
3598 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3599 sizeof (struct sockaddr
))),
3605 #endif /* SIOCGIFCONF */
3607 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3614 static struct ifflag_def ifflag_table
[] = {
3618 #ifdef IFF_BROADCAST
3619 { IFF_BROADCAST
, "broadcast" },
3622 { IFF_DEBUG
, "debug" },
3625 { IFF_LOOPBACK
, "loopback" },
3627 #ifdef IFF_POINTOPOINT
3628 { IFF_POINTOPOINT
, "pointopoint" },
3631 { IFF_RUNNING
, "running" },
3634 { IFF_NOARP
, "noarp" },
3637 { IFF_PROMISC
, "promisc" },
3639 #ifdef IFF_NOTRAILERS
3640 { IFF_NOTRAILERS
, "notrailers" },
3643 { IFF_ALLMULTI
, "allmulti" },
3646 { IFF_MASTER
, "master" },
3649 { IFF_SLAVE
, "slave" },
3651 #ifdef IFF_MULTICAST
3652 { IFF_MULTICAST
, "multicast" },
3655 { IFF_PORTSEL
, "portsel" },
3657 #ifdef IFF_AUTOMEDIA
3658 { IFF_AUTOMEDIA
, "automedia" },
3661 { IFF_DYNAMIC
, "dynamic" },
3664 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3667 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3670 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3673 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3676 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3681 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3682 doc
: /* Return information about network interface named IFNAME.
3683 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3684 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3685 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3686 FLAGS is the current flags of the interface. */)
3691 Lisp_Object res
= Qnil
;
3696 CHECK_STRING (ifname
);
3698 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
3699 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3701 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3706 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3707 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3709 int flags
= rq
.ifr_flags
;
3710 struct ifflag_def
*fp
;
3714 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3716 if (flags
& fp
->flag_bit
)
3718 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3719 flags
-= fp
->flag_bit
;
3722 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3724 if (flags
& (1 << fnum
))
3726 elt
= Fcons (make_number (fnum
), elt
);
3731 res
= Fcons (elt
, res
);
3734 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3735 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3737 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3738 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3742 for (n
= 0; n
< 6; n
++)
3743 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3744 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3747 res
= Fcons (elt
, res
);
3750 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
3751 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
3754 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
3755 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
3757 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3761 res
= Fcons (elt
, res
);
3764 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3765 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
3768 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
3771 res
= Fcons (elt
, res
);
3774 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
3775 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
3778 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3781 res
= Fcons (elt
, res
);
3785 return any
? res
: Qnil
;
3788 #endif /* HAVE_SOCKETS */
3790 /* Turn off input and output for process PROC. */
3793 deactivate_process (proc
)
3796 register int inchannel
, outchannel
;
3797 register struct Lisp_Process
*p
= XPROCESS (proc
);
3799 inchannel
= p
->infd
;
3800 outchannel
= p
->outfd
;
3802 #ifdef ADAPTIVE_READ_BUFFERING
3803 if (p
->read_output_delay
> 0)
3805 if (--process_output_delay_count
< 0)
3806 process_output_delay_count
= 0;
3807 p
->read_output_delay
= 0;
3808 p
->read_output_skip
= 0;
3814 /* Beware SIGCHLD hereabouts. */
3815 flush_pending_output (inchannel
);
3818 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
3819 sys$
dassgn (outchannel
);
3820 vs
= get_vms_process_pointer (p
->pid
);
3822 give_back_vms_process_stuff (vs
);
3825 emacs_close (inchannel
);
3826 if (outchannel
>= 0 && outchannel
!= inchannel
)
3827 emacs_close (outchannel
);
3832 #ifdef DATAGRAM_SOCKETS
3833 if (DATAGRAM_CHAN_P (inchannel
))
3835 xfree (datagram_address
[inchannel
].sa
);
3836 datagram_address
[inchannel
].sa
= 0;
3837 datagram_address
[inchannel
].len
= 0;
3840 chan_process
[inchannel
] = Qnil
;
3841 FD_CLR (inchannel
, &input_wait_mask
);
3842 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
3843 #ifdef NON_BLOCKING_CONNECT
3844 if (FD_ISSET (inchannel
, &connect_wait_mask
))
3846 FD_CLR (inchannel
, &connect_wait_mask
);
3847 if (--num_pending_connects
< 0)
3851 if (inchannel
== max_process_desc
)
3854 /* We just closed the highest-numbered process input descriptor,
3855 so recompute the highest-numbered one now. */
3856 max_process_desc
= 0;
3857 for (i
= 0; i
< MAXDESC
; i
++)
3858 if (!NILP (chan_process
[i
]))
3859 max_process_desc
= i
;
3864 /* Close all descriptors currently in use for communication
3865 with subprocess. This is used in a newly-forked subprocess
3866 to get rid of irrelevant descriptors. */
3869 close_process_descs ()
3873 for (i
= 0; i
< MAXDESC
; i
++)
3875 Lisp_Object process
;
3876 process
= chan_process
[i
];
3877 if (!NILP (process
))
3879 int in
= XPROCESS (process
)->infd
;
3880 int out
= XPROCESS (process
)->outfd
;
3883 if (out
>= 0 && in
!= out
)
3890 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
3892 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
3893 It is read into the process' buffers or given to their filter functions.
3894 Non-nil arg PROCESS means do not return until some output has been received
3897 Non-nil second arg SECONDS and third arg MILLISEC are number of
3898 seconds and milliseconds to wait; return after that much time whether
3899 or not there is input. If SECONDS is a floating point number,
3900 it specifies a fractional number of seconds to wait.
3902 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
3903 from PROCESS, suspending reading output from other processes.
3904 If JUST-THIS-ONE is an integer, don't run any timers either.
3905 Return non-nil if we received any output before the timeout expired. */)
3906 (process
, seconds
, millisec
, just_this_one
)
3907 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
3909 int secs
, usecs
= 0;
3911 if (! NILP (process
))
3912 CHECK_PROCESS (process
);
3914 just_this_one
= Qnil
;
3916 if (!NILP (seconds
))
3918 if (INTEGERP (seconds
))
3919 secs
= XINT (seconds
);
3920 else if (FLOATP (seconds
))
3922 double timeout
= XFLOAT_DATA (seconds
);
3923 secs
= (int) timeout
;
3924 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
3927 wrong_type_argument (Qnumberp
, seconds
);
3929 if (INTEGERP (millisec
))
3932 usecs
+= XINT (millisec
) * 1000;
3933 carry
= usecs
/ 1000000;
3935 if ((usecs
-= carry
* 1000000) < 0)
3942 if (secs
< 0 || (secs
== 0 && usecs
== 0))
3943 secs
= -1, usecs
= 0;
3946 secs
= NILP (process
) ? -1 : 0;
3949 (wait_reading_process_output (secs
, usecs
, 0, 0,
3951 !NILP (process
) ? XPROCESS (process
) : NULL
,
3952 NILP (just_this_one
) ? 0 :
3953 !INTEGERP (just_this_one
) ? 1 : -1)
3957 /* Accept a connection for server process SERVER on CHANNEL. */
3959 static int connect_counter
= 0;
3962 server_accept_connection (server
, channel
)
3966 Lisp_Object proc
, caller
, name
, buffer
;
3967 Lisp_Object contact
, host
, service
;
3968 struct Lisp_Process
*ps
= XPROCESS (server
);
3969 struct Lisp_Process
*p
;
3973 struct sockaddr_in in
;
3975 struct sockaddr_in6 in6
;
3977 #ifdef HAVE_LOCAL_SOCKETS
3978 struct sockaddr_un un
;
3981 int len
= sizeof saddr
;
3983 s
= accept (channel
, &saddr
.sa
, &len
);
3992 if (code
== EWOULDBLOCK
)
3996 if (!NILP (ps
->log
))
3997 call3 (ps
->log
, server
, Qnil
,
3998 concat3 (build_string ("accept failed with code"),
3999 Fnumber_to_string (make_number (code
)),
4000 build_string ("\n")));
4006 /* Setup a new process to handle the connection. */
4008 /* Generate a unique identification of the caller, and build contact
4009 information for this process. */
4012 switch (saddr
.sa
.sa_family
)
4016 Lisp_Object args
[5];
4017 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4018 args
[0] = build_string ("%d.%d.%d.%d");
4019 args
[1] = make_number (*ip
++);
4020 args
[2] = make_number (*ip
++);
4021 args
[3] = make_number (*ip
++);
4022 args
[4] = make_number (*ip
++);
4023 host
= Fformat (5, args
);
4024 service
= make_number (ntohs (saddr
.in
.sin_port
));
4026 args
[0] = build_string (" <%s:%d>");
4029 caller
= Fformat (3, args
);
4036 Lisp_Object args
[9];
4037 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4039 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4040 for (i
= 0; i
< 8; i
++)
4041 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4042 host
= Fformat (9, args
);
4043 service
= make_number (ntohs (saddr
.in
.sin_port
));
4045 args
[0] = build_string (" <[%s]:%d>");
4048 caller
= Fformat (3, args
);
4053 #ifdef HAVE_LOCAL_SOCKETS
4057 caller
= Fnumber_to_string (make_number (connect_counter
));
4058 caller
= concat3 (build_string (" <*"), caller
, build_string ("*>"));
4062 /* Create a new buffer name for this process if it doesn't have a
4063 filter. The new buffer name is based on the buffer name or
4064 process name of the server process concatenated with the caller
4067 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4071 buffer
= ps
->buffer
;
4073 buffer
= Fbuffer_name (buffer
);
4078 buffer
= concat2 (buffer
, caller
);
4079 buffer
= Fget_buffer_create (buffer
);
4083 /* Generate a unique name for the new server process. Combine the
4084 server process name with the caller identification. */
4086 name
= concat2 (ps
->name
, caller
);
4087 proc
= make_process (name
);
4089 chan_process
[s
] = proc
;
4092 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4095 fcntl (s
, F_SETFL
, O_NDELAY
);
4099 p
= XPROCESS (proc
);
4101 /* Build new contact information for this setup. */
4102 contact
= Fcopy_sequence (ps
->childp
);
4103 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4104 contact
= Fplist_put (contact
, QChost
, host
);
4105 if (!NILP (service
))
4106 contact
= Fplist_put (contact
, QCservice
, service
);
4107 contact
= Fplist_put (contact
, QCremote
,
4108 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4109 #ifdef HAVE_GETSOCKNAME
4111 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4112 contact
= Fplist_put (contact
, QClocal
,
4113 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4116 p
->childp
= contact
;
4117 p
->plist
= Fcopy_sequence (ps
->plist
);
4120 p
->sentinel
= ps
->sentinel
;
4121 p
->filter
= ps
->filter
;
4128 /* Client processes for accepted connections are not stopped initially. */
4129 if (!EQ (p
->filter
, Qt
))
4131 FD_SET (s
, &input_wait_mask
);
4132 FD_SET (s
, &non_keyboard_wait_mask
);
4135 if (s
> max_process_desc
)
4136 max_process_desc
= s
;
4138 /* Setup coding system for new process based on server process.
4139 This seems to be the proper thing to do, as the coding system
4140 of the new process should reflect the settings at the time the
4141 server socket was opened; not the current settings. */
4143 p
->decode_coding_system
= ps
->decode_coding_system
;
4144 p
->encode_coding_system
= ps
->encode_coding_system
;
4145 setup_process_coding_systems (proc
);
4147 p
->decoding_buf
= make_uninit_string (0);
4148 p
->decoding_carryover
= 0;
4149 p
->encoding_buf
= make_uninit_string (0);
4151 p
->inherit_coding_system_flag
4152 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4154 if (!NILP (ps
->log
))
4155 call3 (ps
->log
, server
, proc
,
4156 concat3 (build_string ("accept from "),
4157 (STRINGP (host
) ? host
: build_string ("-")),
4158 build_string ("\n")));
4160 if (!NILP (p
->sentinel
))
4161 exec_sentinel (proc
,
4162 concat3 (build_string ("open from "),
4163 (STRINGP (host
) ? host
: build_string ("-")),
4164 build_string ("\n")));
4167 /* This variable is different from waiting_for_input in keyboard.c.
4168 It is used to communicate to a lisp process-filter/sentinel (via the
4169 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4170 for user-input when that process-filter was called.
4171 waiting_for_input cannot be used as that is by definition 0 when
4172 lisp code is being evalled.
4173 This is also used in record_asynch_buffer_change.
4174 For that purpose, this must be 0
4175 when not inside wait_reading_process_output. */
4176 static int waiting_for_user_input_p
;
4179 wait_reading_process_output_unwind (data
)
4182 waiting_for_user_input_p
= XINT (data
);
4186 /* This is here so breakpoints can be put on it. */
4188 wait_reading_process_output_1 ()
4192 /* Use a wrapper around select to work around a bug in gdb 5.3.
4193 Normally, the wrapper is optimzed away by inlining.
4195 If emacs is stopped inside select, the gdb backtrace doesn't
4196 show the function which called select, so it is practically
4197 impossible to step through wait_reading_process_output. */
4201 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4203 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4206 return select (n
, rfd
, wfd
, xfd
, tmo
);
4208 #define select select_wrapper
4211 /* Read and dispose of subprocess output while waiting for timeout to
4212 elapse and/or keyboard input to be available.
4215 timeout in seconds, or
4216 zero for no limit, or
4217 -1 means gobble data immediately available but don't wait for any.
4220 an additional duration to wait, measured in microseconds.
4221 If this is nonzero and time_limit is 0, then the timeout
4222 consists of MICROSECS only.
4224 READ_KBD is a lisp value:
4225 0 to ignore keyboard input, or
4226 1 to return when input is available, or
4227 -1 meaning caller will actually read the input, so don't throw to
4228 the quit handler, or
4230 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4231 output that arrives.
4233 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4234 (and gobble terminal input into the buffer if any arrives).
4236 If WAIT_PROC is specified, wait until something arrives from that
4237 process. The return value is true if we read some input from
4240 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4241 (suspending output from other processes). A negative value
4242 means don't run any timers either.
4244 If WAIT_PROC is specified, then the function returns true if we
4245 received input from that process before the timeout elapsed.
4246 Otherwise, return true if we received input from any process. */
4249 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4250 wait_for_cell
, wait_proc
, just_wait_proc
)
4251 int time_limit
, microsecs
, read_kbd
, do_display
;
4252 Lisp_Object wait_for_cell
;
4253 struct Lisp_Process
*wait_proc
;
4256 register int channel
, nfds
;
4257 SELECT_TYPE Available
;
4258 #ifdef NON_BLOCKING_CONNECT
4259 SELECT_TYPE Connecting
;
4262 int check_delay
, no_avail
;
4265 EMACS_TIME timeout
, end_time
;
4266 int wait_channel
= -1;
4267 int got_some_input
= 0;
4268 int count
= SPECPDL_INDEX ();
4270 FD_ZERO (&Available
);
4271 #ifdef NON_BLOCKING_CONNECT
4272 FD_ZERO (&Connecting
);
4275 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4276 if (wait_proc
!= NULL
)
4277 wait_channel
= wait_proc
->infd
;
4279 record_unwind_protect (wait_reading_process_output_unwind
,
4280 make_number (waiting_for_user_input_p
));
4281 waiting_for_user_input_p
= read_kbd
;
4283 /* Since we may need to wait several times,
4284 compute the absolute time to return at. */
4285 if (time_limit
|| microsecs
)
4287 EMACS_GET_TIME (end_time
);
4288 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4289 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4291 #ifdef POLL_INTERRUPTED_SYS_CALL
4292 /* AlainF 5-Jul-1996
4293 HP-UX 10.10 seem to have problems with signals coming in
4294 Causes "poll: interrupted system call" messages when Emacs is run
4296 Turn off periodic alarms (in case they are in use),
4297 and then turn off any other atimers. */
4299 turn_on_atimers (0);
4300 #endif /* POLL_INTERRUPTED_SYS_CALL */
4304 int timeout_reduced_for_timers
= 0;
4306 /* If calling from keyboard input, do not quit
4307 since we want to return C-g as an input character.
4308 Otherwise, do pending quit if requested. */
4312 else if (interrupt_input_pending
)
4313 handle_async_input ();
4316 /* Exit now if the cell we're waiting for became non-nil. */
4317 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4320 /* Compute time from now till when time limit is up */
4321 /* Exit if already run out */
4322 if (time_limit
== -1)
4324 /* -1 specified for timeout means
4325 gobble output available now
4326 but don't wait at all. */
4328 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4330 else if (time_limit
|| microsecs
)
4332 EMACS_GET_TIME (timeout
);
4333 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4334 if (EMACS_TIME_NEG_P (timeout
))
4339 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4342 /* Normally we run timers here.
4343 But not if wait_for_cell; in those cases,
4344 the wait is supposed to be short,
4345 and those callers cannot handle running arbitrary Lisp code here. */
4346 if (NILP (wait_for_cell
)
4347 && just_wait_proc
>= 0)
4349 EMACS_TIME timer_delay
;
4353 int old_timers_run
= timers_run
;
4354 struct buffer
*old_buffer
= current_buffer
;
4355 Lisp_Object old_window
= selected_window
;
4357 timer_delay
= timer_check (1);
4359 /* If a timer has run, this might have changed buffers
4360 an alike. Make read_key_sequence aware of that. */
4361 if (timers_run
!= old_timers_run
4362 && (old_buffer
!= current_buffer
4363 || !EQ (old_window
, selected_window
))
4364 && waiting_for_user_input_p
== -1)
4365 record_asynch_buffer_change ();
4367 if (timers_run
!= old_timers_run
&& do_display
)
4368 /* We must retry, since a timer may have requeued itself
4369 and that could alter the time_delay. */
4370 redisplay_preserve_echo_area (9);
4374 while (!detect_input_pending ());
4376 /* If there is unread keyboard input, also return. */
4378 && requeued_events_pending_p ())
4381 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4383 EMACS_TIME difference
;
4384 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4385 if (EMACS_TIME_NEG_P (difference
))
4387 timeout
= timer_delay
;
4388 timeout_reduced_for_timers
= 1;
4391 /* If time_limit is -1, we are not going to wait at all. */
4392 else if (time_limit
!= -1)
4394 /* This is so a breakpoint can be put here. */
4395 wait_reading_process_output_1 ();
4399 /* Cause C-g and alarm signals to take immediate action,
4400 and cause input available signals to zero out timeout.
4402 It is important that we do this before checking for process
4403 activity. If we get a SIGCHLD after the explicit checks for
4404 process activity, timeout is the only way we will know. */
4406 set_waiting_for_input (&timeout
);
4408 /* If status of something has changed, and no input is
4409 available, notify the user of the change right away. After
4410 this explicit check, we'll let the SIGCHLD handler zap
4411 timeout to get our attention. */
4412 if (update_tick
!= process_tick
&& do_display
)
4415 #ifdef NON_BLOCKING_CONNECT
4419 Atemp
= input_wait_mask
;
4421 /* On Mac OS X 10.0, the SELECT system call always says input is
4422 present (for reading) at stdin, even when none is. This
4423 causes the call to SELECT below to return 1 and
4424 status_notify not to be called. As a result output of
4425 subprocesses are incorrectly discarded.
4429 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4431 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4432 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4435 #ifdef NON_BLOCKING_CONNECT
4436 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4440 (SELECT_TYPE
*)0, &timeout
)
4443 /* It's okay for us to do this and then continue with
4444 the loop, since timeout has already been zeroed out. */
4445 clear_waiting_for_input ();
4446 status_notify (NULL
);
4450 /* Don't wait for output from a non-running process. Just
4451 read whatever data has already been received. */
4452 if (wait_proc
&& wait_proc
->raw_status_new
)
4453 update_status (wait_proc
);
4455 && ! EQ (wait_proc
->status
, Qrun
)
4456 && ! EQ (wait_proc
->status
, Qconnect
))
4458 int nread
, total_nread
= 0;
4460 clear_waiting_for_input ();
4461 XSETPROCESS (proc
, wait_proc
);
4463 /* Read data from the process, until we exhaust it. */
4464 while (wait_proc
->infd
>= 0)
4466 nread
= read_process_output (proc
, wait_proc
->infd
);
4472 total_nread
+= nread
;
4474 else if (nread
== -1 && EIO
== errno
)
4478 else if (nread
== -1 && EAGAIN
== errno
)
4482 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4486 if (total_nread
> 0 && do_display
)
4487 redisplay_preserve_echo_area (10);
4492 /* Wait till there is something to do */
4494 if (wait_proc
&& just_wait_proc
)
4496 if (wait_proc
->infd
< 0) /* Terminated */
4498 FD_SET (wait_proc
->infd
, &Available
);
4500 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4502 else if (!NILP (wait_for_cell
))
4504 Available
= non_process_wait_mask
;
4506 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4511 Available
= non_keyboard_wait_mask
;
4513 Available
= input_wait_mask
;
4514 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4515 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4518 /* If frame size has changed or the window is newly mapped,
4519 redisplay now, before we start to wait. There is a race
4520 condition here; if a SIGIO arrives between now and the select
4521 and indicates that a frame is trashed, the select may block
4522 displaying a trashed screen. */
4523 if (frame_garbaged
&& do_display
)
4525 clear_waiting_for_input ();
4526 redisplay_preserve_echo_area (11);
4528 set_waiting_for_input (&timeout
);
4532 if (read_kbd
&& detect_input_pending ())
4539 #ifdef NON_BLOCKING_CONNECT
4541 Connecting
= connect_wait_mask
;
4544 #ifdef ADAPTIVE_READ_BUFFERING
4545 /* Set the timeout for adaptive read buffering if any
4546 process has non-zero read_output_skip and non-zero
4547 read_output_delay, and we are not reading output for a
4548 specific wait_channel. It is not executed if
4549 Vprocess_adaptive_read_buffering is nil. */
4550 if (process_output_skip
&& check_delay
> 0)
4552 int usecs
= EMACS_USECS (timeout
);
4553 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4554 usecs
= READ_OUTPUT_DELAY_MAX
;
4555 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4557 proc
= chan_process
[channel
];
4560 /* Find minimum non-zero read_output_delay among the
4561 processes with non-zero read_output_skip. */
4562 if (XPROCESS (proc
)->read_output_delay
> 0)
4565 if (!XPROCESS (proc
)->read_output_skip
)
4567 FD_CLR (channel
, &Available
);
4568 XPROCESS (proc
)->read_output_skip
= 0;
4569 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4570 usecs
= XPROCESS (proc
)->read_output_delay
;
4573 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4574 process_output_skip
= 0;
4578 nfds
= select (max (max (max_process_desc
, max_keyboard_desc
),
4581 #ifdef NON_BLOCKING_CONNECT
4582 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4586 (SELECT_TYPE
*)0, &timeout
);
4591 /* Make C-g and alarm signals set flags again */
4592 clear_waiting_for_input ();
4594 /* If we woke up due to SIGWINCH, actually change size now. */
4595 do_pending_window_change (0);
4597 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4598 /* We wanted the full specified time, so return now. */
4602 if (xerrno
== EINTR
)
4605 /* Ultrix select seems to return ENOMEM when it is
4606 interrupted. Treat it just like EINTR. Bleah. Note
4607 that we want to test for the "ultrix" CPP symbol, not
4608 "__ultrix__"; the latter is only defined under GCC, but
4609 not by DEC's bundled CC. -JimB */
4610 else if (xerrno
== ENOMEM
)
4613 else if (xerrno
== EBADF
)
4616 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4617 the child's closure of the pts gives the parent a SIGHUP, and
4618 the ptc file descriptor is automatically closed,
4619 yielding EBADF here or at select() call above.
4620 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4621 in m/ibmrt-aix.h), and here we just ignore the select error.
4622 Cleanup occurs c/o status_notify after SIGCLD. */
4623 no_avail
= 1; /* Cannot depend on values returned */
4629 error ("select error: %s", emacs_strerror (xerrno
));
4634 FD_ZERO (&Available
);
4635 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4638 #if defined(sun) && !defined(USG5_4)
4639 if (nfds
> 0 && keyboard_bit_set (&Available
)
4641 /* System sometimes fails to deliver SIGIO.
4643 David J. Mackenzie says that Emacs doesn't compile under
4644 Solaris if this code is enabled, thus the USG5_4 in the CPP
4645 conditional. "I haven't noticed any ill effects so far.
4646 If you find a Solaris expert somewhere, they might know
4648 kill (getpid (), SIGIO
);
4651 #if 0 /* When polling is used, interrupt_input is 0,
4652 so get_input_pending should read the input.
4653 So this should not be needed. */
4654 /* If we are using polling for input,
4655 and we see input available, make it get read now.
4656 Otherwise it might not actually get read for a second.
4657 And on hpux, since we turn off polling in wait_reading_process_output,
4658 it might never get read at all if we don't spend much time
4659 outside of wait_reading_process_output. */
4660 if (read_kbd
&& interrupt_input
4661 && keyboard_bit_set (&Available
)
4662 && input_polling_used ())
4663 kill (getpid (), SIGALRM
);
4666 /* Check for keyboard input */
4667 /* If there is any, return immediately
4668 to give it higher priority than subprocesses */
4672 int old_timers_run
= timers_run
;
4673 struct buffer
*old_buffer
= current_buffer
;
4674 Lisp_Object old_window
= selected_window
;
4677 if (detect_input_pending_run_timers (do_display
))
4679 swallow_events (do_display
);
4680 if (detect_input_pending_run_timers (do_display
))
4684 /* If a timer has run, this might have changed buffers
4685 an alike. Make read_key_sequence aware of that. */
4686 if (timers_run
!= old_timers_run
4687 && waiting_for_user_input_p
== -1
4688 && (old_buffer
!= current_buffer
4689 || !EQ (old_window
, selected_window
)))
4690 record_asynch_buffer_change ();
4696 /* If there is unread keyboard input, also return. */
4698 && requeued_events_pending_p ())
4701 /* If we are not checking for keyboard input now,
4702 do process events (but don't run any timers).
4703 This is so that X events will be processed.
4704 Otherwise they may have to wait until polling takes place.
4705 That would causes delays in pasting selections, for example.
4707 (We used to do this only if wait_for_cell.) */
4708 if (read_kbd
== 0 && detect_input_pending ())
4710 swallow_events (do_display
);
4711 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4712 if (detect_input_pending ())
4717 /* Exit now if the cell we're waiting for became non-nil. */
4718 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4722 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4723 go read it. This can happen with X on BSD after logging out.
4724 In that case, there really is no input and no SIGIO,
4725 but select says there is input. */
4727 if (read_kbd
&& interrupt_input
4728 && keyboard_bit_set (&Available
) && ! noninteractive
)
4729 kill (getpid (), SIGIO
);
4733 got_some_input
|= nfds
> 0;
4735 /* If checking input just got us a size-change event from X,
4736 obey it now if we should. */
4737 if (read_kbd
|| ! NILP (wait_for_cell
))
4738 do_pending_window_change (0);
4740 /* Check for data from a process. */
4741 if (no_avail
|| nfds
== 0)
4744 /* Really FIRST_PROC_DESC should be 0 on Unix,
4745 but this is safer in the short run. */
4746 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4748 if (FD_ISSET (channel
, &Available
)
4749 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
4753 /* If waiting for this channel, arrange to return as
4754 soon as no more input to be processed. No more
4756 if (wait_channel
== channel
)
4762 proc
= chan_process
[channel
];
4766 /* If this is a server stream socket, accept connection. */
4767 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4769 server_accept_connection (proc
, channel
);
4773 /* Read data from the process, starting with our
4774 buffered-ahead character if we have one. */
4776 nread
= read_process_output (proc
, channel
);
4779 /* Since read_process_output can run a filter,
4780 which can call accept-process-output,
4781 don't try to read from any other processes
4782 before doing the select again. */
4783 FD_ZERO (&Available
);
4786 redisplay_preserve_echo_area (12);
4789 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4792 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4793 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4795 else if (nread
== -1 && errno
== EAGAIN
)
4799 else if (nread
== -1 && errno
== EAGAIN
)
4801 /* Note that we cannot distinguish between no input
4802 available now and a closed pipe.
4803 With luck, a closed pipe will be accompanied by
4804 subprocess termination and SIGCHLD. */
4805 else if (nread
== 0 && !NETCONN_P (proc
))
4807 #endif /* O_NDELAY */
4808 #endif /* O_NONBLOCK */
4810 /* On some OSs with ptys, when the process on one end of
4811 a pty exits, the other end gets an error reading with
4812 errno = EIO instead of getting an EOF (0 bytes read).
4813 Therefore, if we get an error reading and errno =
4814 EIO, just continue, because the child process has
4815 exited and should clean itself up soon (e.g. when we
4818 However, it has been known to happen that the SIGCHLD
4819 got lost. So raise the signal again just in case.
4821 else if (nread
== -1 && errno
== EIO
)
4823 /* Clear the descriptor now, so we only raise the signal once. */
4824 FD_CLR (channel
, &input_wait_mask
);
4825 FD_CLR (channel
, &non_keyboard_wait_mask
);
4827 kill (getpid (), SIGCHLD
);
4829 #endif /* HAVE_PTYS */
4830 /* If we can detect process termination, don't consider the process
4831 gone just because its pipe is closed. */
4833 else if (nread
== 0 && !NETCONN_P (proc
))
4838 /* Preserve status of processes already terminated. */
4839 XPROCESS (proc
)->tick
= ++process_tick
;
4840 deactivate_process (proc
);
4841 if (XPROCESS (proc
)->raw_status_new
)
4842 update_status (XPROCESS (proc
));
4843 if (EQ (XPROCESS (proc
)->status
, Qrun
))
4844 XPROCESS (proc
)->status
4845 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
4848 #ifdef NON_BLOCKING_CONNECT
4849 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
4850 && FD_ISSET (channel
, &connect_wait_mask
))
4852 struct Lisp_Process
*p
;
4854 FD_CLR (channel
, &connect_wait_mask
);
4855 if (--num_pending_connects
< 0)
4858 proc
= chan_process
[channel
];
4862 p
= XPROCESS (proc
);
4865 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
4866 So only use it on systems where it is known to work. */
4868 int xlen
= sizeof(xerrno
);
4869 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
4874 struct sockaddr pname
;
4875 int pnamelen
= sizeof(pname
);
4877 /* If connection failed, getpeername will fail. */
4879 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
4881 /* Obtain connect failure code through error slippage. */
4884 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
4891 p
->tick
= ++process_tick
;
4892 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
4893 deactivate_process (proc
);
4898 /* Execute the sentinel here. If we had relied on
4899 status_notify to do it later, it will read input
4900 from the process before calling the sentinel. */
4901 exec_sentinel (proc
, build_string ("open\n"));
4902 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
4904 FD_SET (p
->infd
, &input_wait_mask
);
4905 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
4909 #endif /* NON_BLOCKING_CONNECT */
4910 } /* end for each file descriptor */
4911 } /* end while exit conditions not met */
4913 unbind_to (count
, Qnil
);
4915 /* If calling from keyboard input, do not quit
4916 since we want to return C-g as an input character.
4917 Otherwise, do pending quit if requested. */
4920 /* Prevent input_pending from remaining set if we quit. */
4921 clear_input_pending ();
4924 #ifdef POLL_INTERRUPTED_SYS_CALL
4925 /* AlainF 5-Jul-1996
4926 HP-UX 10.10 seems to have problems with signals coming in
4927 Causes "poll: interrupted system call" messages when Emacs is run
4929 Turn periodic alarms back on */
4931 #endif /* POLL_INTERRUPTED_SYS_CALL */
4933 return got_some_input
;
4936 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
4939 read_process_output_call (fun_and_args
)
4940 Lisp_Object fun_and_args
;
4942 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
4946 read_process_output_error_handler (error
)
4949 cmd_error_internal (error
, "error in process filter: ");
4951 update_echo_area ();
4952 Fsleep_for (make_number (2), Qnil
);
4956 /* Read pending output from the process channel,
4957 starting with our buffered-ahead character if we have one.
4958 Yield number of decoded characters read.
4960 This function reads at most 4096 characters.
4961 If you want to read all available subprocess output,
4962 you must call it repeatedly until it returns zero.
4964 The characters read are decoded according to PROC's coding-system
4968 read_process_output (proc
, channel
)
4970 register int channel
;
4972 register int nbytes
;
4974 register Lisp_Object outstream
;
4975 register struct buffer
*old
= current_buffer
;
4976 register struct Lisp_Process
*p
= XPROCESS (proc
);
4977 register int opoint
;
4978 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
4979 int carryover
= p
->decoding_carryover
;
4983 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
4985 vs
= get_vms_process_pointer (p
->pid
);
4989 return (0); /* Really weird if it does this */
4990 if (!(vs
->iosb
[0] & 1))
4991 return -1; /* I/O error */
4994 error ("Could not get VMS process pointer");
4995 chars
= vs
->inputBuffer
;
4996 nbytes
= clean_vms_buffer (chars
, vs
->iosb
[1]);
4999 start_vms_process_read (vs
); /* Crank up the next read on the process */
5000 return 1; /* Nothing worth printing, say we got 1 */
5004 /* The data carried over in the previous decoding (which are at
5005 the tail of decoding buffer) should be prepended to the new
5006 data read to decode all together. */
5007 chars
= (char *) alloca (nbytes
+ carryover
);
5008 bcopy (SDATA (p
->decoding_buf
), buf
, carryover
);
5009 bcopy (vs
->inputBuffer
, chars
+ carryover
, nbytes
);
5013 chars
= (char *) alloca (carryover
+ readmax
);
5015 /* See the comment above. */
5016 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5018 #ifdef DATAGRAM_SOCKETS
5019 /* We have a working select, so proc_buffered_char is always -1. */
5020 if (DATAGRAM_CHAN_P (channel
))
5022 int len
= datagram_address
[channel
].len
;
5023 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5024 0, datagram_address
[channel
].sa
, &len
);
5028 if (proc_buffered_char
[channel
] < 0)
5030 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5031 #ifdef ADAPTIVE_READ_BUFFERING
5032 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5034 int delay
= p
->read_output_delay
;
5037 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5040 process_output_delay_count
++;
5041 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5044 else if (delay
> 0 && (nbytes
== readmax
))
5046 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5048 process_output_delay_count
--;
5050 p
->read_output_delay
= delay
;
5053 p
->read_output_skip
= 1;
5054 process_output_skip
= 1;
5061 chars
[carryover
] = proc_buffered_char
[channel
];
5062 proc_buffered_char
[channel
] = -1;
5063 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5067 nbytes
= nbytes
+ 1;
5069 #endif /* not VMS */
5071 p
->decoding_carryover
= 0;
5073 /* At this point, NBYTES holds number of bytes just received
5074 (including the one in proc_buffered_char[channel]). */
5077 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5079 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5082 /* Now set NBYTES how many bytes we must decode. */
5083 nbytes
+= carryover
;
5085 /* Read and dispose of the process output. */
5086 outstream
= p
->filter
;
5087 if (!NILP (outstream
))
5089 /* We inhibit quit here instead of just catching it so that
5090 hitting ^G when a filter happens to be running won't screw
5092 int count
= SPECPDL_INDEX ();
5093 Lisp_Object odeactivate
;
5094 Lisp_Object obuffer
, okeymap
;
5096 int outer_running_asynch_code
= running_asynch_code
;
5097 int waiting
= waiting_for_user_input_p
;
5099 /* No need to gcpro these, because all we do with them later
5100 is test them for EQness, and none of them should be a string. */
5101 odeactivate
= Vdeactivate_mark
;
5102 XSETBUFFER (obuffer
, current_buffer
);
5103 okeymap
= current_buffer
->keymap
;
5105 specbind (Qinhibit_quit
, Qt
);
5106 specbind (Qlast_nonmenu_event
, Qt
);
5108 /* In case we get recursively called,
5109 and we already saved the match data nonrecursively,
5110 save the same match data in safely recursive fashion. */
5111 if (outer_running_asynch_code
)
5114 /* Don't clobber the CURRENT match data, either! */
5115 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5116 restore_search_regs ();
5117 record_unwind_save_match_data ();
5118 Fset_match_data (tem
, Qt
);
5121 /* For speed, if a search happens within this code,
5122 save the match data in a special nonrecursive fashion. */
5123 running_asynch_code
= 1;
5125 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5126 text
= coding
->dst_object
;
5127 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5128 /* A new coding system might be found. */
5129 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5131 p
->decode_coding_system
= Vlast_coding_system_used
;
5133 /* Don't call setup_coding_system for
5134 proc_decode_coding_system[channel] here. It is done in
5135 detect_coding called via decode_coding above. */
5137 /* If a coding system for encoding is not yet decided, we set
5138 it as the same as coding-system for decoding.
5140 But, before doing that we must check if
5141 proc_encode_coding_system[p->outfd] surely points to a
5142 valid memory because p->outfd will be changed once EOF is
5143 sent to the process. */
5144 if (NILP (p
->encode_coding_system
)
5145 && proc_encode_coding_system
[p
->outfd
])
5147 p
->encode_coding_system
5148 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5149 setup_coding_system (p
->encode_coding_system
,
5150 proc_encode_coding_system
[p
->outfd
]);
5154 if (coding
->carryover_bytes
> 0)
5156 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5157 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5158 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5159 coding
->carryover_bytes
);
5160 p
->decoding_carryover
= coding
->carryover_bytes
;
5162 if (SBYTES (text
) > 0)
5163 internal_condition_case_1 (read_process_output_call
,
5165 Fcons (proc
, Fcons (text
, Qnil
))),
5166 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5167 read_process_output_error_handler
);
5169 /* If we saved the match data nonrecursively, restore it now. */
5170 restore_search_regs ();
5171 running_asynch_code
= outer_running_asynch_code
;
5173 /* Handling the process output should not deactivate the mark. */
5174 Vdeactivate_mark
= odeactivate
;
5176 /* Restore waiting_for_user_input_p as it was
5177 when we were called, in case the filter clobbered it. */
5178 waiting_for_user_input_p
= waiting
;
5180 #if 0 /* Call record_asynch_buffer_change unconditionally,
5181 because we might have changed minor modes or other things
5182 that affect key bindings. */
5183 if (! EQ (Fcurrent_buffer (), obuffer
)
5184 || ! EQ (current_buffer
->keymap
, okeymap
))
5186 /* But do it only if the caller is actually going to read events.
5187 Otherwise there's no need to make him wake up, and it could
5188 cause trouble (for example it would make sit_for return). */
5189 if (waiting_for_user_input_p
== -1)
5190 record_asynch_buffer_change ();
5193 start_vms_process_read (vs
);
5195 unbind_to (count
, Qnil
);
5199 /* If no filter, write into buffer if it isn't dead. */
5200 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5202 Lisp_Object old_read_only
;
5203 int old_begv
, old_zv
;
5204 int old_begv_byte
, old_zv_byte
;
5205 Lisp_Object odeactivate
;
5206 int before
, before_byte
;
5211 odeactivate
= Vdeactivate_mark
;
5213 Fset_buffer (p
->buffer
);
5215 opoint_byte
= PT_BYTE
;
5216 old_read_only
= current_buffer
->read_only
;
5219 old_begv_byte
= BEGV_BYTE
;
5220 old_zv_byte
= ZV_BYTE
;
5222 current_buffer
->read_only
= Qnil
;
5224 /* Insert new output into buffer
5225 at the current end-of-output marker,
5226 thus preserving logical ordering of input and output. */
5227 if (XMARKER (p
->mark
)->buffer
)
5228 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5229 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5232 SET_PT_BOTH (ZV
, ZV_BYTE
);
5234 before_byte
= PT_BYTE
;
5236 /* If the output marker is outside of the visible region, save
5237 the restriction and widen. */
5238 if (! (BEGV
<= PT
&& PT
<= ZV
))
5241 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5242 text
= coding
->dst_object
;
5243 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5244 /* A new coding system might be found. See the comment in the
5245 similar code in the previous `if' block. */
5246 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5248 p
->decode_coding_system
= Vlast_coding_system_used
;
5249 if (NILP (p
->encode_coding_system
)
5250 && proc_encode_coding_system
[p
->outfd
])
5252 p
->encode_coding_system
5253 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5254 setup_coding_system (p
->encode_coding_system
,
5255 proc_encode_coding_system
[p
->outfd
]);
5258 if (coding
->carryover_bytes
> 0)
5260 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5261 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5262 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5263 coding
->carryover_bytes
);
5264 p
->decoding_carryover
= coding
->carryover_bytes
;
5266 /* Adjust the multibyteness of TEXT to that of the buffer. */
5267 if (NILP (current_buffer
->enable_multibyte_characters
)
5268 != ! STRING_MULTIBYTE (text
))
5269 text
= (STRING_MULTIBYTE (text
)
5270 ? Fstring_as_unibyte (text
)
5271 : Fstring_to_multibyte (text
));
5272 /* Insert before markers in case we are inserting where
5273 the buffer's mark is, and the user's next command is Meta-y. */
5274 insert_from_string_before_markers (text
, 0, 0,
5275 SCHARS (text
), SBYTES (text
), 0);
5277 /* Make sure the process marker's position is valid when the
5278 process buffer is changed in the signal_after_change above.
5279 W3 is known to do that. */
5280 if (BUFFERP (p
->buffer
)
5281 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5282 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5284 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5286 update_mode_lines
++;
5288 /* Make sure opoint and the old restrictions
5289 float ahead of any new text just as point would. */
5290 if (opoint
>= before
)
5292 opoint
+= PT
- before
;
5293 opoint_byte
+= PT_BYTE
- before_byte
;
5295 if (old_begv
> before
)
5297 old_begv
+= PT
- before
;
5298 old_begv_byte
+= PT_BYTE
- before_byte
;
5300 if (old_zv
>= before
)
5302 old_zv
+= PT
- before
;
5303 old_zv_byte
+= PT_BYTE
- before_byte
;
5306 /* If the restriction isn't what it should be, set it. */
5307 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5308 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5310 /* Handling the process output should not deactivate the mark. */
5311 Vdeactivate_mark
= odeactivate
;
5313 current_buffer
->read_only
= old_read_only
;
5314 SET_PT_BOTH (opoint
, opoint_byte
);
5315 set_buffer_internal (old
);
5318 start_vms_process_read (vs
);
5323 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5325 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5326 This is intended for use by asynchronous process output filters and sentinels. */)
5329 return (waiting_for_user_input_p
? Qt
: Qnil
);
5332 /* Sending data to subprocess */
5334 jmp_buf send_process_frame
;
5335 Lisp_Object process_sent_to
;
5338 send_process_trap ()
5340 SIGNAL_THREAD_CHECK (SIGPIPE
);
5345 sigunblock (sigmask (SIGPIPE
));
5346 longjmp (send_process_frame
, 1);
5349 /* Send some data to process PROC.
5350 BUF is the beginning of the data; LEN is the number of characters.
5351 OBJECT is the Lisp object that the data comes from. If OBJECT is
5352 nil or t, it means that the data comes from C string.
5354 If OBJECT is not nil, the data is encoded by PROC's coding-system
5355 for encoding before it is sent.
5357 This function can evaluate Lisp code and can garbage collect. */
5360 send_process (proc
, buf
, len
, object
)
5361 volatile Lisp_Object proc
;
5362 unsigned char *volatile buf
;
5364 volatile Lisp_Object object
;
5366 /* Use volatile to protect variables from being clobbered by longjmp. */
5367 struct Lisp_Process
*p
= XPROCESS (proc
);
5369 struct coding_system
*coding
;
5370 struct gcpro gcpro1
;
5371 SIGTYPE (*volatile old_sigpipe
) ();
5376 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5379 if (p
->raw_status_new
)
5381 if (! EQ (p
->status
, Qrun
))
5382 error ("Process %s not running", SDATA (p
->name
));
5384 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5386 coding
= proc_encode_coding_system
[p
->outfd
];
5387 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5389 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5390 || (BUFFERP (object
)
5391 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5394 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5395 /* The coding system for encoding was changed to raw-text
5396 because we sent a unibyte text previously. Now we are
5397 sending a multibyte text, thus we must encode it by the
5398 original coding system specified for the current process. */
5399 setup_coding_system (p
->encode_coding_system
, coding
);
5400 coding
->src_multibyte
= 1;
5404 /* For sending a unibyte text, character code conversion should
5405 not take place but EOL conversion should. So, setup raw-text
5406 or one of the subsidiary if we have not yet done it. */
5407 if (CODING_REQUIRE_ENCODING (coding
))
5409 if (CODING_REQUIRE_FLUSHING (coding
))
5411 /* But, before changing the coding, we must flush out data. */
5412 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5413 send_process (proc
, "", 0, Qt
);
5414 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5416 setup_coding_system (raw_text_coding_system
5417 (Vlast_coding_system_used
),
5419 coding
->src_multibyte
= 0;
5422 coding
->dst_multibyte
= 0;
5424 if (CODING_REQUIRE_ENCODING (coding
))
5426 coding
->dst_object
= Qt
;
5427 if (BUFFERP (object
))
5429 int from_byte
, from
, to
;
5430 int save_pt
, save_pt_byte
;
5431 struct buffer
*cur
= current_buffer
;
5433 set_buffer_internal (XBUFFER (object
));
5434 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5436 from_byte
= PTR_BYTE_POS (buf
);
5437 from
= BYTE_TO_CHAR (from_byte
);
5438 to
= BYTE_TO_CHAR (from_byte
+ len
);
5439 TEMP_SET_PT_BOTH (from
, from_byte
);
5440 encode_coding_object (coding
, object
, from
, from_byte
,
5441 to
, from_byte
+ len
, Qt
);
5442 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5443 set_buffer_internal (cur
);
5445 else if (STRINGP (object
))
5447 encode_coding_string (coding
, object
, 1);
5451 coding
->dst_object
= make_unibyte_string (buf
, len
);
5452 coding
->produced
= len
;
5455 len
= coding
->produced
;
5456 buf
= SDATA (coding
->dst_object
);
5460 vs
= get_vms_process_pointer (p
->pid
);
5462 error ("Could not find this process: %x", p
->pid
);
5463 else if (write_to_vms_process (vs
, buf
, len
))
5467 if (pty_max_bytes
== 0)
5469 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5470 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5471 if (pty_max_bytes
< 0)
5472 pty_max_bytes
= 250;
5474 pty_max_bytes
= 250;
5476 /* Deduct one, to leave space for the eof. */
5480 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5481 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5482 when returning with longjmp despite being declared volatile. */
5483 if (!setjmp (send_process_frame
))
5485 process_sent_to
= proc
;
5490 /* Decide how much data we can send in one batch.
5491 Long lines need to be split into multiple batches. */
5494 /* Starting this at zero is always correct when not the first
5495 iteration because the previous iteration ended by sending C-d.
5496 It may not be correct for the first iteration
5497 if a partial line was sent in a separate send_process call.
5498 If that proves worth handling, we need to save linepos
5499 in the process object. */
5501 unsigned char *ptr
= (unsigned char *) buf
;
5502 unsigned char *end
= (unsigned char *) buf
+ len
;
5504 /* Scan through this text for a line that is too long. */
5505 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5513 /* If we found one, break the line there
5514 and put in a C-d to force the buffer through. */
5518 /* Send this batch, using one or more write calls. */
5521 int outfd
= p
->outfd
;
5522 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5523 #ifdef DATAGRAM_SOCKETS
5524 if (DATAGRAM_CHAN_P (outfd
))
5526 rv
= sendto (outfd
, (char *) buf
, this,
5527 0, datagram_address
[outfd
].sa
,
5528 datagram_address
[outfd
].len
);
5529 if (rv
< 0 && errno
== EMSGSIZE
)
5531 signal (SIGPIPE
, old_sigpipe
);
5532 report_file_error ("sending datagram",
5533 Fcons (proc
, Qnil
));
5539 rv
= emacs_write (outfd
, (char *) buf
, this);
5540 #ifdef ADAPTIVE_READ_BUFFERING
5541 if (p
->read_output_delay
> 0
5542 && p
->adaptive_read_buffering
== 1)
5544 p
->read_output_delay
= 0;
5545 process_output_delay_count
--;
5546 p
->read_output_skip
= 0;
5550 signal (SIGPIPE
, old_sigpipe
);
5556 || errno
== EWOULDBLOCK
5562 /* Buffer is full. Wait, accepting input;
5563 that may allow the program
5564 to finish doing output and read more. */
5568 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5569 /* A gross hack to work around a bug in FreeBSD.
5570 In the following sequence, read(2) returns
5574 write(2) 954 bytes, get EAGAIN
5575 read(2) 1024 bytes in process_read_output
5576 read(2) 11 bytes in process_read_output
5578 That is, read(2) returns more bytes than have
5579 ever been written successfully. The 1033 bytes
5580 read are the 1022 bytes written successfully
5581 after processing (for example with CRs added if
5582 the terminal is set up that way which it is
5583 here). The same bytes will be seen again in a
5584 later read(2), without the CRs. */
5586 if (errno
== EAGAIN
)
5589 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5591 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5593 /* Running filters might relocate buffers or strings.
5594 Arrange to relocate BUF. */
5595 if (BUFFERP (object
))
5596 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5597 else if (STRINGP (object
))
5598 offset
= buf
- SDATA (object
);
5600 #ifdef EMACS_HAS_USECS
5601 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5603 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5606 if (BUFFERP (object
))
5607 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5608 else if (STRINGP (object
))
5609 buf
= offset
+ SDATA (object
);
5614 /* This is a real error. */
5615 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5622 /* If we sent just part of the string, put in an EOF
5623 to force it through, before we send the rest. */
5625 Fprocess_send_eof (proc
);
5628 #endif /* not VMS */
5631 signal (SIGPIPE
, old_sigpipe
);
5633 proc
= process_sent_to
;
5634 p
= XPROCESS (proc
);
5636 p
->raw_status_new
= 0;
5637 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5638 p
->tick
= ++process_tick
;
5639 deactivate_process (proc
);
5641 error ("Error writing to process %s; closed it", SDATA (p
->name
));
5643 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5650 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5652 doc
: /* Send current contents of region as input to PROCESS.
5653 PROCESS may be a process, a buffer, the name of a process or buffer, or
5654 nil, indicating the current buffer's process.
5655 Called from program, takes three arguments, PROCESS, START and END.
5656 If the region is more than 500 characters long,
5657 it is sent in several bunches. This may happen even for shorter regions.
5658 Output from processes can arrive in between bunches. */)
5659 (process
, start
, end
)
5660 Lisp_Object process
, start
, end
;
5665 proc
= get_process (process
);
5666 validate_region (&start
, &end
);
5668 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5669 move_gap (XINT (start
));
5671 start1
= CHAR_TO_BYTE (XINT (start
));
5672 end1
= CHAR_TO_BYTE (XINT (end
));
5673 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5674 Fcurrent_buffer ());
5679 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5681 doc
: /* Send PROCESS the contents of STRING as input.
5682 PROCESS may be a process, a buffer, the name of a process or buffer, or
5683 nil, indicating the current buffer's process.
5684 If STRING is more than 500 characters long,
5685 it is sent in several bunches. This may happen even for shorter strings.
5686 Output from processes can arrive in between bunches. */)
5688 Lisp_Object process
, string
;
5691 CHECK_STRING (string
);
5692 proc
= get_process (process
);
5693 send_process (proc
, SDATA (string
),
5694 SBYTES (string
), string
);
5698 /* Return the foreground process group for the tty/pty that
5699 the process P uses. */
5701 emacs_get_tty_pgrp (p
)
5702 struct Lisp_Process
*p
;
5707 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5710 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5711 master side. Try the slave side. */
5712 fd
= emacs_open (XSTRING (p
->tty_name
)->data
, O_RDONLY
, 0);
5716 ioctl (fd
, TIOCGPGRP
, &gid
);
5720 #endif /* defined (TIOCGPGRP ) */
5725 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5726 Sprocess_running_child_p
, 0, 1, 0,
5727 doc
: /* Return t if PROCESS has given the terminal to a child.
5728 If the operating system does not make it possible to find out,
5729 return t unconditionally. */)
5731 Lisp_Object process
;
5733 /* Initialize in case ioctl doesn't exist or gives an error,
5734 in a way that will cause returning t. */
5737 struct Lisp_Process
*p
;
5739 proc
= get_process (process
);
5740 p
= XPROCESS (proc
);
5742 if (!EQ (p
->childp
, Qt
))
5743 error ("Process %s is not a subprocess",
5746 error ("Process %s is not active",
5749 gid
= emacs_get_tty_pgrp (p
);
5756 /* send a signal number SIGNO to PROCESS.
5757 If CURRENT_GROUP is t, that means send to the process group
5758 that currently owns the terminal being used to communicate with PROCESS.
5759 This is used for various commands in shell mode.
5760 If CURRENT_GROUP is lambda, that means send to the process group
5761 that currently owns the terminal, but only if it is NOT the shell itself.
5763 If NOMSG is zero, insert signal-announcements into process's buffers
5766 If we can, we try to signal PROCESS by sending control characters
5767 down the pty. This allows us to signal inferiors who have changed
5768 their uid, for which killpg would return an EPERM error. */
5771 process_send_signal (process
, signo
, current_group
, nomsg
)
5772 Lisp_Object process
;
5774 Lisp_Object current_group
;
5778 register struct Lisp_Process
*p
;
5782 proc
= get_process (process
);
5783 p
= XPROCESS (proc
);
5785 if (!EQ (p
->childp
, Qt
))
5786 error ("Process %s is not a subprocess",
5789 error ("Process %s is not active",
5793 current_group
= Qnil
;
5795 /* If we are using pgrps, get a pgrp number and make it negative. */
5796 if (NILP (current_group
))
5797 /* Send the signal to the shell's process group. */
5801 #ifdef SIGNALS_VIA_CHARACTERS
5802 /* If possible, send signals to the entire pgrp
5803 by sending an input character to it. */
5805 /* TERMIOS is the latest and bestest, and seems most likely to
5806 work. If the system has it, use it. */
5809 cc_t
*sig_char
= NULL
;
5811 tcgetattr (p
->infd
, &t
);
5816 sig_char
= &t
.c_cc
[VINTR
];
5820 sig_char
= &t
.c_cc
[VQUIT
];
5824 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5825 sig_char
= &t
.c_cc
[VSWTCH
];
5827 sig_char
= &t
.c_cc
[VSUSP
];
5832 if (sig_char
&& *sig_char
!= CDISABLE
)
5834 send_process (proc
, sig_char
, 1, Qnil
);
5837 /* If we can't send the signal with a character,
5838 fall through and send it another way. */
5839 #else /* ! HAVE_TERMIOS */
5841 /* On Berkeley descendants, the following IOCTL's retrieve the
5842 current control characters. */
5843 #if defined (TIOCGLTC) && defined (TIOCGETC)
5851 ioctl (p
->infd
, TIOCGETC
, &c
);
5852 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
5855 ioctl (p
->infd
, TIOCGETC
, &c
);
5856 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
5860 ioctl (p
->infd
, TIOCGLTC
, &lc
);
5861 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
5863 #endif /* ! defined (SIGTSTP) */
5866 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5868 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
5875 ioctl (p
->infd
, TCGETA
, &t
);
5876 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
5879 ioctl (p
->infd
, TCGETA
, &t
);
5880 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
5884 ioctl (p
->infd
, TCGETA
, &t
);
5885 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
5887 #endif /* ! defined (SIGTSTP) */
5889 #else /* ! defined (TCGETA) */
5890 Your configuration files are messed up
.
5891 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
5892 you'd better be using one of the alternatives above! */
5893 #endif /* ! defined (TCGETA) */
5894 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5895 /* In this case, the code above should alway returns. */
5897 #endif /* ! defined HAVE_TERMIOS */
5899 /* The code above may fall through if it can't
5900 handle the signal. */
5901 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5904 /* Get the current pgrp using the tty itself, if we have that.
5905 Otherwise, use the pty to get the pgrp.
5906 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5907 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5908 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5909 His patch indicates that if TIOCGPGRP returns an error, then
5910 we should just assume that p->pid is also the process group id. */
5912 gid
= emacs_get_tty_pgrp (p
);
5915 /* If we can't get the information, assume
5916 the shell owns the tty. */
5919 /* It is not clear whether anything really can set GID to -1.
5920 Perhaps on some system one of those ioctls can or could do so.
5921 Or perhaps this is vestigial. */
5924 #else /* ! defined (TIOCGPGRP ) */
5925 /* Can't select pgrps on this system, so we know that
5926 the child itself heads the pgrp. */
5928 #endif /* ! defined (TIOCGPGRP ) */
5930 /* If current_group is lambda, and the shell owns the terminal,
5931 don't send any signal. */
5932 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5940 p
->raw_status_new
= 0;
5942 p
->tick
= ++process_tick
;
5944 status_notify (NULL
);
5946 #endif /* ! defined (SIGCONT) */
5949 send_process (proc
, "\003", 1, Qnil
); /* ^C */
5954 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
5959 sys$
forcex (&(p
->pid
), 0, 1);
5962 flush_pending_output (p
->infd
);
5966 /* If we don't have process groups, send the signal to the immediate
5967 subprocess. That isn't really right, but it's better than any
5968 obvious alternative. */
5971 kill (p
->pid
, signo
);
5975 /* gid may be a pid, or minus a pgrp's number */
5977 if (!NILP (current_group
))
5979 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
5980 EMACS_KILLPG (gid
, signo
);
5987 #else /* ! defined (TIOCSIGSEND) */
5988 EMACS_KILLPG (gid
, signo
);
5989 #endif /* ! defined (TIOCSIGSEND) */
5992 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
5993 doc
: /* Interrupt process PROCESS.
5994 PROCESS may be a process, a buffer, or the name of a process or buffer.
5995 No arg or nil means current buffer's process.
5996 Second arg CURRENT-GROUP non-nil means send signal to
5997 the current process-group of the process's controlling terminal
5998 rather than to the process's own process group.
5999 If the process is a shell, this means interrupt current subjob
6000 rather than the shell.
6002 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6003 don't send the signal. */)
6004 (process
, current_group
)
6005 Lisp_Object process
, current_group
;
6007 process_send_signal (process
, SIGINT
, current_group
, 0);
6011 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6012 doc
: /* Kill process PROCESS. May be process or name of one.
6013 See function `interrupt-process' for more details on usage. */)
6014 (process
, current_group
)
6015 Lisp_Object process
, current_group
;
6017 process_send_signal (process
, SIGKILL
, current_group
, 0);
6021 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6022 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6023 See function `interrupt-process' for more details on usage. */)
6024 (process
, current_group
)
6025 Lisp_Object process
, current_group
;
6027 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6031 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6032 doc
: /* Stop process PROCESS. May be process or name of one.
6033 See function `interrupt-process' for more details on usage.
6034 If PROCESS is a network process, inhibit handling of incoming traffic. */)
6035 (process
, current_group
)
6036 Lisp_Object process
, current_group
;
6039 if (PROCESSP (process
) && NETCONN_P (process
))
6041 struct Lisp_Process
*p
;
6043 p
= XPROCESS (process
);
6044 if (NILP (p
->command
)
6047 FD_CLR (p
->infd
, &input_wait_mask
);
6048 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6055 error ("No SIGTSTP support");
6057 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6062 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6063 doc
: /* Continue process PROCESS. May be process or name of one.
6064 See function `interrupt-process' for more details on usage.
6065 If PROCESS is a network process, resume handling of incoming traffic. */)
6066 (process
, current_group
)
6067 Lisp_Object process
, current_group
;
6070 if (PROCESSP (process
) && NETCONN_P (process
))
6072 struct Lisp_Process
*p
;
6074 p
= XPROCESS (process
);
6075 if (EQ (p
->command
, Qt
)
6077 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6079 FD_SET (p
->infd
, &input_wait_mask
);
6080 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6087 process_send_signal (process
, SIGCONT
, current_group
, 0);
6089 error ("No SIGCONT support");
6094 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6095 2, 2, "sProcess (name or number): \nnSignal code: ",
6096 doc
: /* Send PROCESS the signal with code SIGCODE.
6097 PROCESS may also be a number specifying the process id of the
6098 process to signal; in this case, the process need not be a child of
6100 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6102 Lisp_Object process
, sigcode
;
6106 if (INTEGERP (process
))
6108 pid
= XINT (process
);
6112 if (FLOATP (process
))
6114 pid
= (pid_t
) XFLOAT_DATA (process
);
6118 if (STRINGP (process
))
6121 if (tem
= Fget_process (process
), NILP (tem
))
6123 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6130 process
= get_process (process
);
6135 CHECK_PROCESS (process
);
6136 pid
= XPROCESS (process
)->pid
;
6138 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6142 #define parse_signal(NAME, VALUE) \
6143 else if (!xstricmp (name, NAME)) \
6144 XSETINT (sigcode, VALUE)
6146 if (INTEGERP (sigcode
))
6150 unsigned char *name
;
6152 CHECK_SYMBOL (sigcode
);
6153 name
= SDATA (SYMBOL_NAME (sigcode
));
6155 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6161 parse_signal ("usr1", SIGUSR1
);
6164 parse_signal ("usr2", SIGUSR2
);
6167 parse_signal ("term", SIGTERM
);
6170 parse_signal ("hup", SIGHUP
);
6173 parse_signal ("int", SIGINT
);
6176 parse_signal ("quit", SIGQUIT
);
6179 parse_signal ("ill", SIGILL
);
6182 parse_signal ("abrt", SIGABRT
);
6185 parse_signal ("emt", SIGEMT
);
6188 parse_signal ("kill", SIGKILL
);
6191 parse_signal ("fpe", SIGFPE
);
6194 parse_signal ("bus", SIGBUS
);
6197 parse_signal ("segv", SIGSEGV
);
6200 parse_signal ("sys", SIGSYS
);
6203 parse_signal ("pipe", SIGPIPE
);
6206 parse_signal ("alrm", SIGALRM
);
6209 parse_signal ("urg", SIGURG
);
6212 parse_signal ("stop", SIGSTOP
);
6215 parse_signal ("tstp", SIGTSTP
);
6218 parse_signal ("cont", SIGCONT
);
6221 parse_signal ("chld", SIGCHLD
);
6224 parse_signal ("ttin", SIGTTIN
);
6227 parse_signal ("ttou", SIGTTOU
);
6230 parse_signal ("io", SIGIO
);
6233 parse_signal ("xcpu", SIGXCPU
);
6236 parse_signal ("xfsz", SIGXFSZ
);
6239 parse_signal ("vtalrm", SIGVTALRM
);
6242 parse_signal ("prof", SIGPROF
);
6245 parse_signal ("winch", SIGWINCH
);
6248 parse_signal ("info", SIGINFO
);
6251 error ("Undefined signal name %s", name
);
6256 return make_number (kill (pid
, XINT (sigcode
)));
6259 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6260 doc
: /* Make PROCESS see end-of-file in its input.
6261 EOF comes after any text already sent to it.
6262 PROCESS may be a process, a buffer, the name of a process or buffer, or
6263 nil, indicating the current buffer's process.
6264 If PROCESS is a network connection, or is a process communicating
6265 through a pipe (as opposed to a pty), then you cannot send any more
6266 text to PROCESS after you call this function. */)
6268 Lisp_Object process
;
6271 struct coding_system
*coding
;
6273 if (DATAGRAM_CONN_P (process
))
6276 proc
= get_process (process
);
6277 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6279 /* Make sure the process is really alive. */
6280 if (XPROCESS (proc
)->raw_status_new
)
6281 update_status (XPROCESS (proc
));
6282 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6283 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6285 if (CODING_REQUIRE_FLUSHING (coding
))
6287 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6288 send_process (proc
, "", 0, Qnil
);
6292 send_process (proc
, "\032", 1, Qnil
); /* ^z */
6294 if (XPROCESS (proc
)->pty_flag
)
6295 send_process (proc
, "\004", 1, Qnil
);
6298 int old_outfd
, new_outfd
;
6300 #ifdef HAVE_SHUTDOWN
6301 /* If this is a network connection, or socketpair is used
6302 for communication with the subprocess, call shutdown to cause EOF.
6303 (In some old system, shutdown to socketpair doesn't work.
6304 Then we just can't win.) */
6305 if (XPROCESS (proc
)->pid
== 0
6306 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6307 shutdown (XPROCESS (proc
)->outfd
, 1);
6308 /* In case of socketpair, outfd == infd, so don't close it. */
6309 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6310 emacs_close (XPROCESS (proc
)->outfd
);
6311 #else /* not HAVE_SHUTDOWN */
6312 emacs_close (XPROCESS (proc
)->outfd
);
6313 #endif /* not HAVE_SHUTDOWN */
6314 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6317 old_outfd
= XPROCESS (proc
)->outfd
;
6319 if (!proc_encode_coding_system
[new_outfd
])
6320 proc_encode_coding_system
[new_outfd
]
6321 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6322 bcopy (proc_encode_coding_system
[old_outfd
],
6323 proc_encode_coding_system
[new_outfd
],
6324 sizeof (struct coding_system
));
6325 bzero (proc_encode_coding_system
[old_outfd
],
6326 sizeof (struct coding_system
));
6328 XPROCESS (proc
)->outfd
= new_outfd
;
6334 /* Kill all processes associated with `buffer'.
6335 If `buffer' is nil, kill all processes */
6338 kill_buffer_processes (buffer
)
6341 Lisp_Object tail
, proc
;
6343 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6345 proc
= XCDR (XCAR (tail
));
6347 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6349 if (NETCONN_P (proc
))
6350 Fdelete_process (proc
);
6351 else if (XPROCESS (proc
)->infd
>= 0)
6352 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6357 /* On receipt of a signal that a child status has changed, loop asking
6358 about children with changed statuses until the system says there
6361 All we do is change the status; we do not run sentinels or print
6362 notifications. That is saved for the next time keyboard input is
6363 done, in order to avoid timing errors.
6365 ** WARNING: this can be called during garbage collection.
6366 Therefore, it must not be fooled by the presence of mark bits in
6369 ** USG WARNING: Although it is not obvious from the documentation
6370 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6371 signal() before executing at least one wait(), otherwise the
6372 handler will be called again, resulting in an infinite loop. The
6373 relevant portion of the documentation reads "SIGCLD signals will be
6374 queued and the signal-catching function will be continually
6375 reentered until the queue is empty". Invoking signal() causes the
6376 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6379 ** Malloc WARNING: This should never call malloc either directly or
6380 indirectly; if it does, that is a bug */
6384 sigchld_handler (signo
)
6387 int old_errno
= errno
;
6389 register struct Lisp_Process
*p
;
6390 extern EMACS_TIME
*input_available_clear_time
;
6392 SIGNAL_THREAD_CHECK (signo
);
6396 sigheld
|= sigbit (SIGCHLD
);
6408 #endif /* no WUNTRACED */
6409 /* Keep trying to get a status until we get a definitive result. */
6413 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6415 while (pid
< 0 && errno
== EINTR
);
6419 /* PID == 0 means no processes found, PID == -1 means a real
6420 failure. We have done all our job, so return. */
6422 /* USG systems forget handlers when they are used;
6423 must reestablish each time */
6424 #if defined (USG) && !defined (POSIX_SIGNALS)
6425 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6428 sigheld
&= ~sigbit (SIGCHLD
);
6436 #endif /* no WNOHANG */
6438 /* Find the process that signaled us, and record its status. */
6440 /* The process can have been deleted by Fdelete_process. */
6441 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6443 Lisp_Object xpid
= XCAR (tail
);
6444 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6445 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6447 XSETCAR (tail
, Qnil
);
6448 goto sigchld_end_of_loop
;
6452 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6454 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6456 proc
= XCDR (XCAR (tail
));
6457 p
= XPROCESS (proc
);
6458 if (EQ (p
->childp
, Qt
) && p
->pid
== pid
)
6463 /* Look for an asynchronous process whose pid hasn't been filled
6466 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6468 proc
= XCDR (XCAR (tail
));
6469 p
= XPROCESS (proc
);
6475 /* Change the status of the process that was found. */
6478 union { int i
; WAITTYPE wt
; } u
;
6479 int clear_desc_flag
= 0;
6481 p
->tick
= ++process_tick
;
6483 p
->raw_status
= u
.i
;
6484 p
->raw_status_new
= 1;
6486 /* If process has terminated, stop waiting for its output. */
6487 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6489 clear_desc_flag
= 1;
6491 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6492 if (clear_desc_flag
)
6494 FD_CLR (p
->infd
, &input_wait_mask
);
6495 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6498 /* Tell wait_reading_process_output that it needs to wake up and
6500 if (input_available_clear_time
)
6501 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6504 /* There was no asynchronous process found for that pid: we have
6505 a synchronous process. */
6508 synch_process_alive
= 0;
6510 /* Report the status of the synchronous process. */
6512 synch_process_retcode
= WRETCODE (w
);
6513 else if (WIFSIGNALED (w
))
6514 synch_process_termsig
= WTERMSIG (w
);
6516 /* Tell wait_reading_process_output that it needs to wake up and
6518 if (input_available_clear_time
)
6519 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6522 sigchld_end_of_loop
:
6525 /* On some systems, we must return right away.
6526 If any more processes want to signal us, we will
6528 Otherwise (on systems that have WNOHANG), loop around
6529 to use up all the processes that have something to tell us. */
6530 #if (defined WINDOWSNT \
6531 || (defined USG && !defined GNU_LINUX \
6532 && !(defined HPUX && defined WNOHANG)))
6533 #if defined (USG) && ! defined (POSIX_SIGNALS)
6534 signal (signo
, sigchld_handler
);
6538 #endif /* USG, but not HPUX with WNOHANG */
6541 #endif /* SIGCHLD */
6545 exec_sentinel_unwind (data
)
6548 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6553 exec_sentinel_error_handler (error
)
6556 cmd_error_internal (error
, "error in process sentinel: ");
6558 update_echo_area ();
6559 Fsleep_for (make_number (2), Qnil
);
6564 exec_sentinel (proc
, reason
)
6565 Lisp_Object proc
, reason
;
6567 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6568 register struct Lisp_Process
*p
= XPROCESS (proc
);
6569 int count
= SPECPDL_INDEX ();
6570 int outer_running_asynch_code
= running_asynch_code
;
6571 int waiting
= waiting_for_user_input_p
;
6573 if (inhibit_sentinels
)
6576 /* No need to gcpro these, because all we do with them later
6577 is test them for EQness, and none of them should be a string. */
6578 odeactivate
= Vdeactivate_mark
;
6579 XSETBUFFER (obuffer
, current_buffer
);
6580 okeymap
= current_buffer
->keymap
;
6582 sentinel
= p
->sentinel
;
6583 if (NILP (sentinel
))
6586 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6587 assure that it gets restored no matter how the sentinel exits. */
6589 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6590 /* Inhibit quit so that random quits don't screw up a running filter. */
6591 specbind (Qinhibit_quit
, Qt
);
6592 specbind (Qlast_nonmenu_event
, Qt
);
6594 /* In case we get recursively called,
6595 and we already saved the match data nonrecursively,
6596 save the same match data in safely recursive fashion. */
6597 if (outer_running_asynch_code
)
6600 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6601 restore_search_regs ();
6602 record_unwind_save_match_data ();
6603 Fset_match_data (tem
, Qt
);
6606 /* For speed, if a search happens within this code,
6607 save the match data in a special nonrecursive fashion. */
6608 running_asynch_code
= 1;
6610 internal_condition_case_1 (read_process_output_call
,
6612 Fcons (proc
, Fcons (reason
, Qnil
))),
6613 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6614 exec_sentinel_error_handler
);
6616 /* If we saved the match data nonrecursively, restore it now. */
6617 restore_search_regs ();
6618 running_asynch_code
= outer_running_asynch_code
;
6620 Vdeactivate_mark
= odeactivate
;
6622 /* Restore waiting_for_user_input_p as it was
6623 when we were called, in case the filter clobbered it. */
6624 waiting_for_user_input_p
= waiting
;
6627 if (! EQ (Fcurrent_buffer (), obuffer
)
6628 || ! EQ (current_buffer
->keymap
, okeymap
))
6630 /* But do it only if the caller is actually going to read events.
6631 Otherwise there's no need to make him wake up, and it could
6632 cause trouble (for example it would make sit_for return). */
6633 if (waiting_for_user_input_p
== -1)
6634 record_asynch_buffer_change ();
6636 unbind_to (count
, Qnil
);
6639 /* Report all recent events of a change in process status
6640 (either run the sentinel or output a message).
6641 This is usually done while Emacs is waiting for keyboard input
6642 but can be done at other times. */
6645 status_notify (deleting_process
)
6646 struct Lisp_Process
*deleting_process
;
6648 register Lisp_Object proc
, buffer
;
6649 Lisp_Object tail
, msg
;
6650 struct gcpro gcpro1
, gcpro2
;
6654 /* We need to gcpro tail; if read_process_output calls a filter
6655 which deletes a process and removes the cons to which tail points
6656 from Vprocess_alist, and then causes a GC, tail is an unprotected
6660 /* Set this now, so that if new processes are created by sentinels
6661 that we run, we get called again to handle their status changes. */
6662 update_tick
= process_tick
;
6664 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6667 register struct Lisp_Process
*p
;
6669 proc
= Fcdr (XCAR (tail
));
6670 p
= XPROCESS (proc
);
6672 if (p
->tick
!= p
->update_tick
)
6674 p
->update_tick
= p
->tick
;
6676 /* If process is still active, read any output that remains. */
6677 while (! EQ (p
->filter
, Qt
)
6678 && ! EQ (p
->status
, Qconnect
)
6679 && ! EQ (p
->status
, Qlisten
)
6680 && ! EQ (p
->command
, Qt
) /* Network process not stopped. */
6682 && p
!= deleting_process
6683 && read_process_output (proc
, p
->infd
) > 0);
6687 /* Get the text to use for the message. */
6688 if (p
->raw_status_new
)
6690 msg
= status_message (p
);
6692 /* If process is terminated, deactivate it or delete it. */
6694 if (CONSP (p
->status
))
6695 symbol
= XCAR (p
->status
);
6697 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6698 || EQ (symbol
, Qclosed
))
6700 if (delete_exited_processes
)
6701 remove_process (proc
);
6703 deactivate_process (proc
);
6706 /* The actions above may have further incremented p->tick.
6707 So set p->update_tick again
6708 so that an error in the sentinel will not cause
6709 this code to be run again. */
6710 p
->update_tick
= p
->tick
;
6711 /* Now output the message suitably. */
6712 if (!NILP (p
->sentinel
))
6713 exec_sentinel (proc
, msg
);
6714 /* Don't bother with a message in the buffer
6715 when a process becomes runnable. */
6716 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6718 Lisp_Object ro
, tem
;
6719 struct buffer
*old
= current_buffer
;
6720 int opoint
, opoint_byte
;
6721 int before
, before_byte
;
6723 ro
= XBUFFER (buffer
)->read_only
;
6725 /* Avoid error if buffer is deleted
6726 (probably that's why the process is dead, too) */
6727 if (NILP (XBUFFER (buffer
)->name
))
6729 Fset_buffer (buffer
);
6732 opoint_byte
= PT_BYTE
;
6733 /* Insert new output into buffer
6734 at the current end-of-output marker,
6735 thus preserving logical ordering of input and output. */
6736 if (XMARKER (p
->mark
)->buffer
)
6737 Fgoto_char (p
->mark
);
6739 SET_PT_BOTH (ZV
, ZV_BYTE
);
6742 before_byte
= PT_BYTE
;
6744 tem
= current_buffer
->read_only
;
6745 current_buffer
->read_only
= Qnil
;
6746 insert_string ("\nProcess ");
6747 Finsert (1, &p
->name
);
6748 insert_string (" ");
6750 current_buffer
->read_only
= tem
;
6751 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6753 if (opoint
>= before
)
6754 SET_PT_BOTH (opoint
+ (PT
- before
),
6755 opoint_byte
+ (PT_BYTE
- before_byte
));
6757 SET_PT_BOTH (opoint
, opoint_byte
);
6759 set_buffer_internal (old
);
6764 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6765 redisplay_preserve_echo_area (13);
6771 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6772 Sset_process_coding_system
, 1, 3, 0,
6773 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6774 DECODING will be used to decode subprocess output and ENCODING to
6775 encode subprocess input. */)
6776 (process
, decoding
, encoding
)
6777 register Lisp_Object process
, decoding
, encoding
;
6779 register struct Lisp_Process
*p
;
6781 CHECK_PROCESS (process
);
6782 p
= XPROCESS (process
);
6784 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6786 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6787 Fcheck_coding_system (decoding
);
6788 Fcheck_coding_system (encoding
);
6789 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6790 p
->decode_coding_system
= decoding
;
6791 p
->encode_coding_system
= encoding
;
6792 setup_process_coding_systems (process
);
6797 DEFUN ("process-coding-system",
6798 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6799 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6801 register Lisp_Object process
;
6803 CHECK_PROCESS (process
);
6804 return Fcons (XPROCESS (process
)->decode_coding_system
,
6805 XPROCESS (process
)->encode_coding_system
);
6808 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6809 Sset_process_filter_multibyte
, 2, 2, 0,
6810 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6811 If FLAG is non-nil, the filter is given multibyte strings.
6812 If FLAG is nil, the filter is given unibyte strings. In this case,
6813 all character code conversion except for end-of-line conversion is
6816 Lisp_Object process
, flag
;
6818 register struct Lisp_Process
*p
;
6820 CHECK_PROCESS (process
);
6821 p
= XPROCESS (process
);
6823 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
6824 setup_process_coding_systems (process
);
6829 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6830 Sprocess_filter_multibyte_p
, 1, 1, 0,
6831 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6833 Lisp_Object process
;
6835 register struct Lisp_Process
*p
;
6836 struct coding_system
*coding
;
6838 CHECK_PROCESS (process
);
6839 p
= XPROCESS (process
);
6840 coding
= proc_decode_coding_system
[p
->infd
];
6841 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6846 /* Add DESC to the set of keyboard input descriptors. */
6849 add_keyboard_wait_descriptor (desc
)
6852 FD_SET (desc
, &input_wait_mask
);
6853 FD_SET (desc
, &non_process_wait_mask
);
6854 if (desc
> max_keyboard_desc
)
6855 max_keyboard_desc
= desc
;
6858 static int add_gpm_wait_descriptor_called_flag
;
6861 add_gpm_wait_descriptor (desc
)
6864 if (! add_gpm_wait_descriptor_called_flag
)
6865 FD_CLR (0, &input_wait_mask
);
6866 add_gpm_wait_descriptor_called_flag
= 1;
6867 FD_SET (desc
, &input_wait_mask
);
6868 FD_SET (desc
, &gpm_wait_mask
);
6869 if (desc
> max_gpm_desc
)
6870 max_gpm_desc
= desc
;
6873 /* From now on, do not expect DESC to give keyboard input. */
6876 delete_keyboard_wait_descriptor (desc
)
6880 int lim
= max_keyboard_desc
;
6882 FD_CLR (desc
, &input_wait_mask
);
6883 FD_CLR (desc
, &non_process_wait_mask
);
6885 if (desc
== max_keyboard_desc
)
6886 for (fd
= 0; fd
< lim
; fd
++)
6887 if (FD_ISSET (fd
, &input_wait_mask
)
6888 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
6889 && !FD_ISSET (fd
, &gpm_wait_mask
))
6890 max_keyboard_desc
= fd
;
6894 delete_gpm_wait_descriptor (desc
)
6898 int lim
= max_gpm_desc
;
6900 FD_CLR (desc
, &input_wait_mask
);
6901 FD_CLR (desc
, &non_process_wait_mask
);
6903 if (desc
== max_gpm_desc
)
6904 for (fd
= 0; fd
< lim
; fd
++)
6905 if (FD_ISSET (fd
, &input_wait_mask
)
6906 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
6907 && !FD_ISSET (fd
, &non_process_wait_mask
))
6911 /* Return nonzero if *MASK has a bit set
6912 that corresponds to one of the keyboard input descriptors. */
6915 keyboard_bit_set (mask
)
6920 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
6921 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6922 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6933 inhibit_sentinels
= 0;
6937 if (! noninteractive
|| initialized
)
6939 signal (SIGCHLD
, sigchld_handler
);
6942 FD_ZERO (&input_wait_mask
);
6943 FD_ZERO (&non_keyboard_wait_mask
);
6944 FD_ZERO (&non_process_wait_mask
);
6945 max_process_desc
= 0;
6947 #ifdef NON_BLOCKING_CONNECT
6948 FD_ZERO (&connect_wait_mask
);
6949 num_pending_connects
= 0;
6952 #ifdef ADAPTIVE_READ_BUFFERING
6953 process_output_delay_count
= 0;
6954 process_output_skip
= 0;
6957 /* Don't do this, it caused infinite select loops. The display
6958 method should call add_keyboard_wait_descriptor on stdin if it
6961 FD_SET (0, &input_wait_mask
);
6964 Vprocess_alist
= Qnil
;
6966 deleted_pid_list
= Qnil
;
6968 for (i
= 0; i
< MAXDESC
; i
++)
6970 chan_process
[i
] = Qnil
;
6971 proc_buffered_char
[i
] = -1;
6973 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
6974 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
6975 #ifdef DATAGRAM_SOCKETS
6976 bzero (datagram_address
, sizeof datagram_address
);
6981 Lisp_Object subfeatures
= Qnil
;
6982 struct socket_options
*sopt
;
6984 #define ADD_SUBFEATURE(key, val) \
6985 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
6987 #ifdef NON_BLOCKING_CONNECT
6988 ADD_SUBFEATURE (QCnowait
, Qt
);
6990 #ifdef DATAGRAM_SOCKETS
6991 ADD_SUBFEATURE (QCtype
, Qdatagram
);
6993 #ifdef HAVE_LOCAL_SOCKETS
6994 ADD_SUBFEATURE (QCfamily
, Qlocal
);
6996 ADD_SUBFEATURE (QCfamily
, Qipv4
);
6998 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7000 #ifdef HAVE_GETSOCKNAME
7001 ADD_SUBFEATURE (QCservice
, Qt
);
7003 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY))
7004 ADD_SUBFEATURE (QCserver
, Qt
);
7007 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7008 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7010 Fprovide (intern ("make-network-process"), subfeatures
);
7012 #endif /* HAVE_SOCKETS */
7014 #if defined (DARWIN) || defined (MAC_OSX)
7015 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7016 processes. As such, we only change the default value. */
7019 char *release
= get_operating_system_release();
7020 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7021 && release
[1] == '.')) {
7022 Vprocess_connection_type
= Qnil
;
7031 Qprocessp
= intern ("processp");
7032 staticpro (&Qprocessp
);
7033 Qrun
= intern ("run");
7035 Qstop
= intern ("stop");
7037 Qsignal
= intern ("signal");
7038 staticpro (&Qsignal
);
7040 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7043 Qexit = intern ("exit");
7044 staticpro (&Qexit); */
7046 Qopen
= intern ("open");
7048 Qclosed
= intern ("closed");
7049 staticpro (&Qclosed
);
7050 Qconnect
= intern ("connect");
7051 staticpro (&Qconnect
);
7052 Qfailed
= intern ("failed");
7053 staticpro (&Qfailed
);
7054 Qlisten
= intern ("listen");
7055 staticpro (&Qlisten
);
7056 Qlocal
= intern ("local");
7057 staticpro (&Qlocal
);
7058 Qipv4
= intern ("ipv4");
7061 Qipv6
= intern ("ipv6");
7064 Qdatagram
= intern ("datagram");
7065 staticpro (&Qdatagram
);
7067 QCname
= intern (":name");
7068 staticpro (&QCname
);
7069 QCbuffer
= intern (":buffer");
7070 staticpro (&QCbuffer
);
7071 QChost
= intern (":host");
7072 staticpro (&QChost
);
7073 QCservice
= intern (":service");
7074 staticpro (&QCservice
);
7075 QCtype
= intern (":type");
7076 staticpro (&QCtype
);
7077 QClocal
= intern (":local");
7078 staticpro (&QClocal
);
7079 QCremote
= intern (":remote");
7080 staticpro (&QCremote
);
7081 QCcoding
= intern (":coding");
7082 staticpro (&QCcoding
);
7083 QCserver
= intern (":server");
7084 staticpro (&QCserver
);
7085 QCnowait
= intern (":nowait");
7086 staticpro (&QCnowait
);
7087 QCsentinel
= intern (":sentinel");
7088 staticpro (&QCsentinel
);
7089 QClog
= intern (":log");
7091 QCnoquery
= intern (":noquery");
7092 staticpro (&QCnoquery
);
7093 QCstop
= intern (":stop");
7094 staticpro (&QCstop
);
7095 QCoptions
= intern (":options");
7096 staticpro (&QCoptions
);
7097 QCplist
= intern (":plist");
7098 staticpro (&QCplist
);
7100 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7101 staticpro (&Qlast_nonmenu_event
);
7103 staticpro (&Vprocess_alist
);
7105 staticpro (&deleted_pid_list
);
7108 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7109 doc
: /* *Non-nil means delete processes immediately when they exit.
7110 A value of nil means don't delete them until `list-processes' is run. */);
7112 delete_exited_processes
= 1;
7114 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7115 doc
: /* Control type of device used to communicate with subprocesses.
7116 Values are nil to use a pipe, or t or `pty' to use a pty.
7117 The value has no effect if the system has no ptys or if all ptys are busy:
7118 then a pipe is used in any case.
7119 The value takes effect when `start-process' is called. */);
7120 Vprocess_connection_type
= Qt
;
7122 #ifdef ADAPTIVE_READ_BUFFERING
7123 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7124 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7125 On some systems, when Emacs reads the output from a subprocess, the output data
7126 is read in very small blocks, potentially resulting in very poor performance.
7127 This behavior can be remedied to some extent by setting this variable to a
7128 non-nil value, as it will automatically delay reading from such processes, to
7129 allow them to produce more output before Emacs tries to read it.
7130 If the value is t, the delay is reset after each write to the process; any other
7131 non-nil value means that the delay is not reset on write.
7132 The variable takes effect when `start-process' is called. */);
7133 Vprocess_adaptive_read_buffering
= Qt
;
7136 defsubr (&Sprocessp
);
7137 defsubr (&Sget_process
);
7138 defsubr (&Sget_buffer_process
);
7139 defsubr (&Sdelete_process
);
7140 defsubr (&Sprocess_status
);
7141 defsubr (&Sprocess_exit_status
);
7142 defsubr (&Sprocess_id
);
7143 defsubr (&Sprocess_name
);
7144 defsubr (&Sprocess_tty_name
);
7145 defsubr (&Sprocess_command
);
7146 defsubr (&Sset_process_buffer
);
7147 defsubr (&Sprocess_buffer
);
7148 defsubr (&Sprocess_mark
);
7149 defsubr (&Sset_process_filter
);
7150 defsubr (&Sprocess_filter
);
7151 defsubr (&Sset_process_sentinel
);
7152 defsubr (&Sprocess_sentinel
);
7153 defsubr (&Sset_process_window_size
);
7154 defsubr (&Sset_process_inherit_coding_system_flag
);
7155 defsubr (&Sprocess_inherit_coding_system_flag
);
7156 defsubr (&Sset_process_query_on_exit_flag
);
7157 defsubr (&Sprocess_query_on_exit_flag
);
7158 defsubr (&Sprocess_contact
);
7159 defsubr (&Sprocess_plist
);
7160 defsubr (&Sset_process_plist
);
7161 defsubr (&Slist_processes
);
7162 defsubr (&Sprocess_list
);
7163 defsubr (&Sstart_process
);
7165 defsubr (&Sset_network_process_option
);
7166 defsubr (&Smake_network_process
);
7167 defsubr (&Sformat_network_address
);
7168 #endif /* HAVE_SOCKETS */
7169 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7171 defsubr (&Snetwork_interface_list
);
7173 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7174 defsubr (&Snetwork_interface_info
);
7176 #endif /* HAVE_SOCKETS ... */
7177 #ifdef DATAGRAM_SOCKETS
7178 defsubr (&Sprocess_datagram_address
);
7179 defsubr (&Sset_process_datagram_address
);
7181 defsubr (&Saccept_process_output
);
7182 defsubr (&Sprocess_send_region
);
7183 defsubr (&Sprocess_send_string
);
7184 defsubr (&Sinterrupt_process
);
7185 defsubr (&Skill_process
);
7186 defsubr (&Squit_process
);
7187 defsubr (&Sstop_process
);
7188 defsubr (&Scontinue_process
);
7189 defsubr (&Sprocess_running_child_p
);
7190 defsubr (&Sprocess_send_eof
);
7191 defsubr (&Ssignal_process
);
7192 defsubr (&Swaiting_for_user_input_p
);
7193 /* defsubr (&Sprocess_connection); */
7194 defsubr (&Sset_process_coding_system
);
7195 defsubr (&Sprocess_coding_system
);
7196 defsubr (&Sset_process_filter_multibyte
);
7197 defsubr (&Sprocess_filter_multibyte_p
);
7201 #else /* not subprocesses */
7203 #include <sys/types.h>
7207 #include "systime.h"
7208 #include "character.h"
7210 #include "termopts.h"
7211 #include "sysselect.h"
7213 extern int frame_garbaged
;
7215 extern EMACS_TIME
timer_check ();
7216 extern int timers_run
;
7220 /* As described above, except assuming that there are no subprocesses:
7222 Wait for timeout to elapse and/or keyboard input to be available.
7225 timeout in seconds, or
7226 zero for no limit, or
7227 -1 means gobble data immediately available but don't wait for any.
7229 read_kbd is a Lisp_Object:
7230 0 to ignore keyboard input, or
7231 1 to return when input is available, or
7232 -1 means caller will actually read the input, so don't throw to
7235 see full version for other parameters. We know that wait_proc will
7236 always be NULL, since `subprocesses' isn't defined.
7238 do_display != 0 means redisplay should be done to show subprocess
7239 output that arrives.
7241 Return true if we received input from any process. */
7244 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7245 wait_for_cell
, wait_proc
, just_wait_proc
)
7246 int time_limit
, microsecs
, read_kbd
, do_display
;
7247 Lisp_Object wait_for_cell
;
7248 struct Lisp_Process
*wait_proc
;
7252 EMACS_TIME end_time
, timeout
;
7253 SELECT_TYPE waitchannels
;
7256 /* What does time_limit really mean? */
7257 if (time_limit
|| microsecs
)
7259 EMACS_GET_TIME (end_time
);
7260 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7261 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7264 /* Turn off periodic alarms (in case they are in use)
7265 and then turn off any other atimers,
7266 because the select emulator uses alarms. */
7268 turn_on_atimers (0);
7272 int timeout_reduced_for_timers
= 0;
7274 /* If calling from keyboard input, do not quit
7275 since we want to return C-g as an input character.
7276 Otherwise, do pending quit if requested. */
7280 /* Exit now if the cell we're waiting for became non-nil. */
7281 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7284 /* Compute time from now till when time limit is up */
7285 /* Exit if already run out */
7286 if (time_limit
== -1)
7288 /* -1 specified for timeout means
7289 gobble output available now
7290 but don't wait at all. */
7292 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7294 else if (time_limit
|| microsecs
)
7296 EMACS_GET_TIME (timeout
);
7297 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7298 if (EMACS_TIME_NEG_P (timeout
))
7303 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7306 /* If our caller will not immediately handle keyboard events,
7307 run timer events directly.
7308 (Callers that will immediately read keyboard events
7309 call timer_delay on their own.) */
7310 if (NILP (wait_for_cell
))
7312 EMACS_TIME timer_delay
;
7316 int old_timers_run
= timers_run
;
7317 timer_delay
= timer_check (1);
7318 if (timers_run
!= old_timers_run
&& do_display
)
7319 /* We must retry, since a timer may have requeued itself
7320 and that could alter the time delay. */
7321 redisplay_preserve_echo_area (14);
7325 while (!detect_input_pending ());
7327 /* If there is unread keyboard input, also return. */
7329 && requeued_events_pending_p ())
7332 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7334 EMACS_TIME difference
;
7335 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7336 if (EMACS_TIME_NEG_P (difference
))
7338 timeout
= timer_delay
;
7339 timeout_reduced_for_timers
= 1;
7344 /* Cause C-g and alarm signals to take immediate action,
7345 and cause input available signals to zero out timeout. */
7347 set_waiting_for_input (&timeout
);
7349 /* Wait till there is something to do. */
7351 if (! read_kbd
&& NILP (wait_for_cell
))
7352 FD_ZERO (&waitchannels
);
7354 FD_SET (0, &waitchannels
);
7356 /* If a frame has been newly mapped and needs updating,
7357 reprocess its display stuff. */
7358 if (frame_garbaged
&& do_display
)
7360 clear_waiting_for_input ();
7361 redisplay_preserve_echo_area (15);
7363 set_waiting_for_input (&timeout
);
7366 if (read_kbd
&& detect_input_pending ())
7369 FD_ZERO (&waitchannels
);
7372 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7377 /* Make C-g and alarm signals set flags again */
7378 clear_waiting_for_input ();
7380 /* If we woke up due to SIGWINCH, actually change size now. */
7381 do_pending_window_change (0);
7383 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7384 /* We waited the full specified time, so return now. */
7389 /* If the system call was interrupted, then go around the
7391 if (xerrno
== EINTR
)
7392 FD_ZERO (&waitchannels
);
7394 error ("select error: %s", emacs_strerror (xerrno
));
7397 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7398 /* System sometimes fails to deliver SIGIO. */
7399 kill (getpid (), SIGIO
);
7402 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7403 kill (getpid (), SIGIO
);
7406 /* Check for keyboard input */
7409 && detect_input_pending_run_timers (do_display
))
7411 swallow_events (do_display
);
7412 if (detect_input_pending_run_timers (do_display
))
7416 /* If there is unread keyboard input, also return. */
7418 && requeued_events_pending_p ())
7421 /* If wait_for_cell. check for keyboard input
7422 but don't run any timers.
7423 ??? (It seems wrong to me to check for keyboard
7424 input at all when wait_for_cell, but the code
7425 has been this way since July 1994.
7426 Try changing this after version 19.31.) */
7427 if (! NILP (wait_for_cell
)
7428 && detect_input_pending ())
7430 swallow_events (do_display
);
7431 if (detect_input_pending ())
7435 /* Exit now if the cell we're waiting for became non-nil. */
7436 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7446 /* Don't confuse make-docfile by having two doc strings for this function.
7447 make-docfile does not pay attention to #if, for good reason! */
7448 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7451 register Lisp_Object name
;
7456 /* Don't confuse make-docfile by having two doc strings for this function.
7457 make-docfile does not pay attention to #if, for good reason! */
7458 DEFUN ("process-inherit-coding-system-flag",
7459 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7463 register Lisp_Object process
;
7465 /* Ignore the argument and return the value of
7466 inherit-process-coding-system. */
7467 return inherit_process_coding_system
? Qt
: Qnil
;
7470 /* Kill all processes associated with `buffer'.
7471 If `buffer' is nil, kill all processes.
7472 Since we have no subprocesses, this does nothing. */
7475 kill_buffer_processes (buffer
)
7488 QCtype
= intern (":type");
7489 staticpro (&QCtype
);
7491 defsubr (&Sget_buffer_process
);
7492 defsubr (&Sprocess_inherit_coding_system_flag
);
7496 #endif /* not subprocesses */
7498 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7499 (do not change this comment) */