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>
60 #ifdef NEED_NET_ERRNO_H
61 #include <net/errno.h>
62 #endif /* NEED_NET_ERRNO_H */
64 /* Are local (unix) sockets supported? */
65 #if defined (HAVE_SYS_UN_H) && !defined (NO_SOCKETS_IN_FILE_SYSTEM)
66 #if !defined (AF_LOCAL) && defined (AF_UNIX)
67 #define AF_LOCAL AF_UNIX
70 #define HAVE_LOCAL_SOCKETS
74 #endif /* HAVE_SOCKETS */
76 /* TERM is a poor-man's SLIP, used on GNU/Linux. */
81 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
82 #ifdef HAVE_BROKEN_INET_ADDR
83 #define IN_ADDR struct in_addr
84 #define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
86 #define IN_ADDR unsigned long
87 #define NUMERIC_ADDR_ERROR (numeric_addr == -1)
90 #if defined(BSD_SYSTEM) || defined(STRIDE)
91 #include <sys/ioctl.h>
92 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
94 #endif /* HAVE_PTYS and no O_NDELAY */
95 #endif /* BSD_SYSTEM || STRIDE */
97 #ifdef BROKEN_O_NONBLOCK
99 #endif /* BROKEN_O_NONBLOCK */
105 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
107 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
108 /* sys/ioctl.h may have been included already */
110 #include <sys/ioctl.h>
117 #include <sys/sysmacros.h> /* for "minor" */
118 #endif /* not IRIS */
121 #include <sys/wait.h>
124 /* Disable IPv6 support for w32 until someone figures out how to do it
141 #include "termhooks.h"
142 #include "termopts.h"
143 #include "commands.h"
144 #include "keyboard.h"
146 #include "blockinput.h"
147 #include "dispextern.h"
148 #include "composite.h"
151 Lisp_Object Qprocessp
;
152 Lisp_Object Qrun
, Qstop
, Qsignal
;
153 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
154 Lisp_Object Qlocal
, Qipv4
, Qdatagram
;
158 Lisp_Object QCname
, QCbuffer
, QChost
, QCservice
, QCtype
;
159 Lisp_Object QClocal
, QCremote
, QCcoding
;
160 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
161 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
162 Lisp_Object QCfilter_multibyte
;
163 Lisp_Object Qlast_nonmenu_event
;
164 /* QCfamily is declared and initialized in xfaces.c,
165 QCfilter in keyboard.c. */
166 extern Lisp_Object QCfamily
, QCfilter
;
168 /* Qexit is declared and initialized in eval.c. */
170 /* QCfamily is defined in xfaces.c. */
171 extern Lisp_Object QCfamily
;
172 /* QCfilter is defined in keyboard.c. */
173 extern Lisp_Object QCfilter
;
175 /* a process object is a network connection when its childp field is neither
176 Qt nor Qnil but is instead a property list (KEY VAL ...). */
179 #define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
180 #define NETCONN1_P(p) (GC_CONSP ((p)->childp))
182 #define NETCONN_P(p) 0
183 #define NETCONN1_P(p) 0
184 #endif /* HAVE_SOCKETS */
186 /* Define first descriptor number available for subprocesses. */
188 #define FIRST_PROC_DESC 1
190 #define FIRST_PROC_DESC 3
193 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
196 #if !defined (SIGCHLD) && defined (SIGCLD)
197 #define SIGCHLD SIGCLD
200 #include "syssignal.h"
204 extern char *get_operating_system_release ();
210 extern char *sys_errlist
[];
217 /* t means use pty, nil means use a pipe,
218 maybe other values to come. */
219 static Lisp_Object Vprocess_connection_type
;
223 #include <sys/socket.h>
227 /* These next two vars are non-static since sysdep.c uses them in the
228 emulation of `select'. */
229 /* Number of events of change of status of a process. */
231 /* Number of events for which the user or sentinel has been notified. */
234 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
236 #ifdef BROKEN_NON_BLOCKING_CONNECT
237 #undef NON_BLOCKING_CONNECT
239 #ifndef NON_BLOCKING_CONNECT
242 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
243 #if defined (O_NONBLOCK) || defined (O_NDELAY)
244 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
245 #define NON_BLOCKING_CONNECT
246 #endif /* EWOULDBLOCK || EINPROGRESS */
247 #endif /* O_NONBLOCK || O_NDELAY */
248 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
249 #endif /* HAVE_SELECT */
250 #endif /* HAVE_SOCKETS */
251 #endif /* NON_BLOCKING_CONNECT */
252 #endif /* BROKEN_NON_BLOCKING_CONNECT */
254 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
255 this system. We need to read full packets, so we need a
256 "non-destructive" select. So we require either native select,
257 or emulation of select using FIONREAD. */
259 #ifdef BROKEN_DATAGRAM_SOCKETS
260 #undef DATAGRAM_SOCKETS
262 #ifndef DATAGRAM_SOCKETS
264 #if defined (HAVE_SELECT) || defined (FIONREAD)
265 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
266 #define DATAGRAM_SOCKETS
267 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
268 #endif /* HAVE_SELECT || FIONREAD */
269 #endif /* HAVE_SOCKETS */
270 #endif /* DATAGRAM_SOCKETS */
271 #endif /* BROKEN_DATAGRAM_SOCKETS */
274 #undef NON_BLOCKING_CONNECT
275 #undef DATAGRAM_SOCKETS
278 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
279 #ifdef EMACS_HAS_USECS
280 #define ADAPTIVE_READ_BUFFERING
284 #ifdef ADAPTIVE_READ_BUFFERING
285 #define READ_OUTPUT_DELAY_INCREMENT 10000
286 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
287 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
289 /* Number of processes which have a non-zero read_output_delay,
290 and therefore might be delayed for adaptive read buffering. */
292 static int process_output_delay_count
;
294 /* Non-zero if any process has non-nil read_output_skip. */
296 static int process_output_skip
;
298 /* Non-nil means to delay reading process output to improve buffering.
299 A value of t means that delay is reset after each send, any other
300 non-nil value does not reset the delay. A value of nil disables
301 adaptive read buffering completely. */
302 static Lisp_Object Vprocess_adaptive_read_buffering
;
304 #define process_output_delay_count 0
308 #include "sysselect.h"
310 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
311 static void deactivate_process
P_ ((Lisp_Object
));
312 static void status_notify
P_ ((struct Lisp_Process
*));
313 static int read_process_output
P_ ((Lisp_Object
, int));
315 /* If we support a window system, turn on the code to poll periodically
316 to detect C-g. It isn't actually used when doing interrupt input. */
317 #ifdef HAVE_WINDOW_SYSTEM
318 #define POLL_FOR_INPUT
321 static Lisp_Object
get_process ();
322 static void exec_sentinel ();
324 extern EMACS_TIME
timer_check ();
325 extern int timers_run
;
327 /* Mask of bits indicating the descriptors that we wait for input on. */
329 static SELECT_TYPE input_wait_mask
;
331 /* Mask that excludes keyboard input descriptor (s). */
333 static SELECT_TYPE non_keyboard_wait_mask
;
335 /* Mask that excludes process input descriptor (s). */
337 static SELECT_TYPE non_process_wait_mask
;
339 #ifdef NON_BLOCKING_CONNECT
340 /* Mask of bits indicating the descriptors that we wait for connect to
341 complete on. Once they complete, they are removed from this mask
342 and added to the input_wait_mask and non_keyboard_wait_mask. */
344 static SELECT_TYPE connect_wait_mask
;
346 /* Number of bits set in connect_wait_mask. */
347 static int num_pending_connects
;
349 #define IF_NON_BLOCKING_CONNECT(s) s
351 #define IF_NON_BLOCKING_CONNECT(s)
354 /* The largest descriptor currently in use for a process object. */
355 static int max_process_desc
;
357 /* The largest descriptor currently in use for keyboard input. */
358 static int max_keyboard_desc
;
360 /* Nonzero means delete a process right away if it exits. */
361 static int delete_exited_processes
;
363 /* Indexed by descriptor, gives the process (if any) for that descriptor */
364 Lisp_Object chan_process
[MAXDESC
];
366 /* Alist of elements (NAME . PROCESS) */
367 Lisp_Object Vprocess_alist
;
369 /* Buffered-ahead input char from process, indexed by channel.
370 -1 means empty (no char is buffered).
371 Used on sys V where the only way to tell if there is any
372 output from the process is to read at least one char.
373 Always -1 on systems that support FIONREAD. */
375 /* Don't make static; need to access externally. */
376 int proc_buffered_char
[MAXDESC
];
378 /* Table of `struct coding-system' for each process. */
379 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
380 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
382 #ifdef DATAGRAM_SOCKETS
383 /* Table of `partner address' for datagram sockets. */
384 struct sockaddr_and_len
{
387 } datagram_address
[MAXDESC
];
388 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
389 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XINT (XPROCESS (proc)->infd)].sa != 0)
391 #define DATAGRAM_CHAN_P(chan) (0)
392 #define DATAGRAM_CONN_P(proc) (0)
395 /* Maximum number of bytes to send to a pty without an eof. */
396 static int pty_max_bytes
;
398 /* Nonzero means don't run process sentinels. This is used
400 int inhibit_sentinels
;
406 /* The file name of the pty opened by allocate_pty. */
408 static char pty_name
[24];
411 /* Compute the Lisp form of the process status, p->status, from
412 the numeric status that was returned by `wait'. */
414 static Lisp_Object
status_convert ();
418 struct Lisp_Process
*p
;
420 union { int i
; WAITTYPE wt
; } u
;
421 eassert (p
->raw_status_new
);
423 p
->status
= status_convert (u
.wt
);
424 p
->raw_status_new
= 0;
427 /* Convert a process status word in Unix format to
428 the list that we use internally. */
435 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
436 else if (WIFEXITED (w
))
437 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
438 WCOREDUMP (w
) ? Qt
: Qnil
));
439 else if (WIFSIGNALED (w
))
440 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
441 WCOREDUMP (w
) ? Qt
: Qnil
));
446 /* Given a status-list, extract the three pieces of information
447 and store them individually through the three pointers. */
450 decode_status (l
, symbol
, code
, coredump
)
468 *code
= XFASTINT (XCAR (tem
));
470 *coredump
= !NILP (tem
);
474 /* Return a string describing a process status list. */
478 struct Lisp_Process
*p
;
480 Lisp_Object status
= p
->status
;
483 Lisp_Object string
, string2
;
485 decode_status (status
, &symbol
, &code
, &coredump
);
487 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
490 synchronize_system_messages_locale ();
491 signame
= strsignal (code
);
494 string
= build_string (signame
);
495 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
496 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
497 return concat2 (string
, string2
);
499 else if (EQ (symbol
, Qexit
))
502 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
504 return build_string ("finished\n");
505 string
= Fnumber_to_string (make_number (code
));
506 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
507 return concat3 (build_string ("exited abnormally with code "),
510 else if (EQ (symbol
, Qfailed
))
512 string
= Fnumber_to_string (make_number (code
));
513 string2
= build_string ("\n");
514 return concat3 (build_string ("failed with code "),
518 return Fcopy_sequence (Fsymbol_name (symbol
));
523 /* Open an available pty, returning a file descriptor.
524 Return -1 on failure.
525 The file name of the terminal corresponding to the pty
526 is left in the variable pty_name. */
537 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
538 for (i
= 0; i
< 16; i
++)
541 struct stat stb
; /* Used in some PTY_OPEN. */
542 #ifdef PTY_NAME_SPRINTF
545 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
546 #endif /* no PTY_NAME_SPRINTF */
550 #else /* no PTY_OPEN */
553 /* Unusual IRIS code */
554 *ptyv
= emacs_open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
557 if (fstat (fd
, &stb
) < 0)
559 # else /* not IRIS */
560 { /* Some systems name their pseudoterminals so that there are gaps in
561 the usual sequence - for example, on HP9000/S700 systems, there
562 are no pseudoterminals with names ending in 'f'. So we wait for
563 three failures in a row before deciding that we've reached the
565 int failed_count
= 0;
567 if (stat (pty_name
, &stb
) < 0)
570 if (failed_count
>= 3)
577 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
579 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
581 # endif /* not IRIS */
583 #endif /* no PTY_OPEN */
587 /* check to make certain that both sides are available
588 this avoids a nasty yet stupid bug in rlogins */
589 #ifdef PTY_TTY_NAME_SPRINTF
592 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
593 #endif /* no PTY_TTY_NAME_SPRINTF */
595 if (access (pty_name
, 6) != 0)
598 # if !defined(IRIS) && !defined(__sgi)
604 #endif /* not UNIPLUS */
611 #endif /* HAVE_PTYS */
617 register Lisp_Object val
, tem
, name1
;
618 register struct Lisp_Process
*p
;
622 p
= allocate_process ();
624 XSETINT (p
->infd
, -1);
625 XSETINT (p
->outfd
, -1);
626 XSETFASTINT (p
->tick
, 0);
627 XSETFASTINT (p
->update_tick
, 0);
630 p
->raw_status_new
= 0;
632 p
->mark
= Fmake_marker ();
633 p
->kill_without_query
= Qnil
;
635 #ifdef ADAPTIVE_READ_BUFFERING
636 p
->adaptive_read_buffering
= Qnil
;
637 XSETFASTINT (p
->read_output_delay
, 0);
638 p
->read_output_skip
= Qnil
;
641 /* If name is already in use, modify it until it is unused. */
646 tem
= Fget_process (name1
);
647 if (NILP (tem
)) break;
648 sprintf (suffix
, "<%d>", i
);
649 name1
= concat2 (name
, build_string (suffix
));
653 XSETPROCESS (val
, p
);
654 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
659 remove_process (proc
)
660 register Lisp_Object proc
;
662 register Lisp_Object pair
;
664 pair
= Frassq (proc
, Vprocess_alist
);
665 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
667 deactivate_process (proc
);
670 /* Setup coding systems of PROCESS. */
673 setup_process_coding_systems (process
)
676 struct Lisp_Process
*p
= XPROCESS (process
);
677 int inch
= XINT (p
->infd
);
678 int outch
= XINT (p
->outfd
);
680 if (inch
< 0 || outch
< 0)
683 if (!proc_decode_coding_system
[inch
])
684 proc_decode_coding_system
[inch
]
685 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
686 setup_coding_system (p
->decode_coding_system
,
687 proc_decode_coding_system
[inch
]);
688 if (! NILP (p
->filter
))
690 if (NILP (p
->filter_multibyte
))
691 setup_raw_text_coding_system (proc_decode_coding_system
[inch
]);
693 else if (BUFFERP (p
->buffer
))
695 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
696 setup_raw_text_coding_system (proc_decode_coding_system
[inch
]);
699 if (!proc_encode_coding_system
[outch
])
700 proc_encode_coding_system
[outch
]
701 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
702 setup_coding_system (p
->encode_coding_system
,
703 proc_encode_coding_system
[outch
]);
704 if (proc_encode_coding_system
[outch
]->eol_type
== CODING_EOL_UNDECIDED
)
705 proc_encode_coding_system
[outch
]->eol_type
= system_eol_type
;
708 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
709 doc
: /* Return t if OBJECT is a process. */)
713 return PROCESSP (object
) ? Qt
: Qnil
;
716 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
717 doc
: /* Return the process named NAME, or nil if there is none. */)
719 register Lisp_Object name
;
724 return Fcdr (Fassoc (name
, Vprocess_alist
));
727 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
728 doc
: /* Return the (or a) process associated with BUFFER.
729 BUFFER may be a buffer or the name of one. */)
731 register Lisp_Object buffer
;
733 register Lisp_Object buf
, tail
, proc
;
735 if (NILP (buffer
)) return Qnil
;
736 buf
= Fget_buffer (buffer
);
737 if (NILP (buf
)) return Qnil
;
739 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
741 proc
= Fcdr (Fcar (tail
));
742 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
748 /* This is how commands for the user decode process arguments. It
749 accepts a process, a process name, a buffer, a buffer name, or nil.
750 Buffers denote the first process in the buffer, and nil denotes the
755 register Lisp_Object name
;
757 register Lisp_Object proc
, obj
;
760 obj
= Fget_process (name
);
762 obj
= Fget_buffer (name
);
764 error ("Process %s does not exist", SDATA (name
));
766 else if (NILP (name
))
767 obj
= Fcurrent_buffer ();
771 /* Now obj should be either a buffer object or a process object.
775 proc
= Fget_buffer_process (obj
);
777 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
789 /* Fdelete_process promises to immediately forget about the process, but in
790 reality, Emacs needs to remember those processes until they have been
791 treated by sigchld_handler; otherwise this handler would consider the
792 process as being synchronous and say that the synchronous process is
794 static Lisp_Object deleted_pid_list
;
797 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
798 doc
: /* Delete PROCESS: kill it and forget about it immediately.
799 PROCESS may be a process, a buffer, the name of a process or buffer, or
800 nil, indicating the current buffer's process. */)
802 register Lisp_Object process
;
804 register struct Lisp_Process
*p
;
806 process
= get_process (process
);
807 p
= XPROCESS (process
);
809 p
->raw_status_new
= 0;
812 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
813 XSETINT (p
->tick
, ++process_tick
);
816 else if (XINT (p
->infd
) >= 0)
820 /* Assignment to EMACS_INT stops GCC whining about limited range
822 EMACS_INT pid
= p
->pid
;
824 /* No problem storing the pid here, as it is still in Vprocess_alist. */
825 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
826 /* GC treated elements set to nil. */
827 Fdelq (Qnil
, deleted_pid_list
));
828 /* If the process has already signaled, remove it from the list. */
829 if (p
->raw_status_new
)
832 if (CONSP (p
->status
))
833 symbol
= XCAR (p
->status
);
834 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
836 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
840 Fkill_process (process
, Qnil
);
841 /* Do this now, since remove_process will make sigchld_handler do nothing. */
843 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
844 XSETINT (p
->tick
, ++process_tick
);
848 remove_process (process
);
852 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
853 doc
: /* Return the status of PROCESS.
854 The returned value is one of the following symbols:
855 run -- for a process that is running.
856 stop -- for a process stopped but continuable.
857 exit -- for a process that has exited.
858 signal -- for a process that has got a fatal signal.
859 open -- for a network stream connection that is open.
860 listen -- for a network stream server that is listening.
861 closed -- for a network stream connection that is closed.
862 connect -- when waiting for a non-blocking connection to complete.
863 failed -- when a non-blocking connection has failed.
864 nil -- if arg is a process name and no such process exists.
865 PROCESS may be a process, a buffer, the name of a process, or
866 nil, indicating the current buffer's process. */)
868 register Lisp_Object process
;
870 register struct Lisp_Process
*p
;
871 register Lisp_Object status
;
873 if (STRINGP (process
))
874 process
= Fget_process (process
);
876 process
= get_process (process
);
881 p
= XPROCESS (process
);
882 if (p
->raw_status_new
)
886 status
= XCAR (status
);
889 if (EQ (status
, Qexit
))
891 else if (EQ (p
->command
, Qt
))
893 else if (EQ (status
, Qrun
))
899 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
901 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
902 If PROCESS has not yet exited or died, return 0. */)
904 register Lisp_Object process
;
906 CHECK_PROCESS (process
);
907 if (XPROCESS (process
)->raw_status_new
)
908 update_status (XPROCESS (process
));
909 if (CONSP (XPROCESS (process
)->status
))
910 return XCAR (XCDR (XPROCESS (process
)->status
));
911 return make_number (0);
914 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
915 doc
: /* Return the process id of PROCESS.
916 This is the pid of the external process which PROCESS uses or talks to.
917 For a network connection, this value is nil. */)
919 register Lisp_Object process
;
921 /* Assignment to EMACS_INT stops GCC whining about limited range of
925 CHECK_PROCESS (process
);
926 pid
= XPROCESS (process
)->pid
;
927 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
930 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
931 doc
: /* Return the name of PROCESS, as a string.
932 This is the name of the program invoked in PROCESS,
933 possibly modified to make it unique among process names. */)
935 register Lisp_Object process
;
937 CHECK_PROCESS (process
);
938 return XPROCESS (process
)->name
;
941 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
942 doc
: /* Return the command that was executed to start PROCESS.
943 This is a list of strings, the first string being the program executed
944 and the rest of the strings being the arguments given to it.
945 For a non-child channel, this is nil. */)
947 register Lisp_Object process
;
949 CHECK_PROCESS (process
);
950 return XPROCESS (process
)->command
;
953 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
954 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
955 This is the terminal that the process itself reads and writes on,
956 not the name of the pty that Emacs uses to talk with that terminal. */)
958 register Lisp_Object process
;
960 CHECK_PROCESS (process
);
961 return XPROCESS (process
)->tty_name
;
964 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
966 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
968 register Lisp_Object process
, buffer
;
970 struct Lisp_Process
*p
;
972 CHECK_PROCESS (process
);
974 CHECK_BUFFER (buffer
);
975 p
= XPROCESS (process
);
978 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
979 setup_process_coding_systems (process
);
983 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
985 doc
: /* Return the buffer PROCESS is associated with.
986 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
988 register Lisp_Object process
;
990 CHECK_PROCESS (process
);
991 return XPROCESS (process
)->buffer
;
994 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
996 doc
: /* Return the marker for the end of the last output from PROCESS. */)
998 register Lisp_Object process
;
1000 CHECK_PROCESS (process
);
1001 return XPROCESS (process
)->mark
;
1004 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
1006 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
1007 A value of t means stop accepting output from the process.
1009 When a process has a filter, its buffer is not used for output.
1010 Instead, each time it does output, the entire string of output is
1011 passed to the filter.
1013 The filter gets two arguments: the process and the string of output.
1014 The string argument is normally a multibyte string, except:
1015 - if the process' input coding system is no-conversion or raw-text,
1016 it is a unibyte string (the non-converted input), or else
1017 - if `default-enable-multibyte-characters' is nil, it is a unibyte
1018 string (the result of converting the decoded input multibyte
1019 string to unibyte with `string-make-unibyte'). */)
1021 register Lisp_Object process
, filter
;
1023 struct Lisp_Process
*p
;
1025 CHECK_PROCESS (process
);
1026 p
= XPROCESS (process
);
1028 /* Don't signal an error if the process' input file descriptor
1029 is closed. This could make debugging Lisp more difficult,
1030 for example when doing something like
1032 (setq process (start-process ...))
1034 (set-process-filter process ...) */
1036 if (XINT (p
->infd
) >= 0)
1038 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1040 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
1041 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
1043 else if (EQ (p
->filter
, Qt
)
1044 && !EQ (p
->command
, Qt
)) /* Network process not stopped. */
1046 FD_SET (XINT (p
->infd
), &input_wait_mask
);
1047 FD_SET (XINT (p
->infd
), &non_keyboard_wait_mask
);
1053 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1054 setup_process_coding_systems (process
);
1058 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1060 doc
: /* Returns the filter function of PROCESS; nil if none.
1061 See `set-process-filter' for more info on filter functions. */)
1063 register Lisp_Object process
;
1065 CHECK_PROCESS (process
);
1066 return XPROCESS (process
)->filter
;
1069 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1071 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1072 The sentinel is called as a function when the process changes state.
1073 It gets two arguments: the process, and a string describing the change. */)
1075 register Lisp_Object process
, sentinel
;
1077 struct Lisp_Process
*p
;
1079 CHECK_PROCESS (process
);
1080 p
= XPROCESS (process
);
1082 p
->sentinel
= sentinel
;
1084 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1088 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1090 doc
: /* Return the sentinel of PROCESS; nil if none.
1091 See `set-process-sentinel' for more info on sentinels. */)
1093 register Lisp_Object process
;
1095 CHECK_PROCESS (process
);
1096 return XPROCESS (process
)->sentinel
;
1099 DEFUN ("set-process-window-size", Fset_process_window_size
,
1100 Sset_process_window_size
, 3, 3, 0,
1101 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1102 (process
, height
, width
)
1103 register Lisp_Object process
, height
, width
;
1105 CHECK_PROCESS (process
);
1106 CHECK_NATNUM (height
);
1107 CHECK_NATNUM (width
);
1109 if (XINT (XPROCESS (process
)->infd
) < 0
1110 || set_window_size (XINT (XPROCESS (process
)->infd
),
1111 XINT (height
), XINT (width
)) <= 0)
1117 DEFUN ("set-process-inherit-coding-system-flag",
1118 Fset_process_inherit_coding_system_flag
,
1119 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1120 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1121 If the second argument FLAG is non-nil, then the variable
1122 `buffer-file-coding-system' of the buffer associated with PROCESS
1123 will be bound to the value of the coding system used to decode
1126 This is useful when the coding system specified for the process buffer
1127 leaves either the character code conversion or the end-of-line conversion
1128 unspecified, or if the coding system used to decode the process output
1129 is more appropriate for saving the process buffer.
1131 Binding the variable `inherit-process-coding-system' to non-nil before
1132 starting the process is an alternative way of setting the inherit flag
1133 for the process which will run. */)
1135 register Lisp_Object process
, flag
;
1137 CHECK_PROCESS (process
);
1138 XPROCESS (process
)->inherit_coding_system_flag
= flag
;
1142 DEFUN ("process-inherit-coding-system-flag",
1143 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1145 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1146 If this flag is t, `buffer-file-coding-system' of the buffer
1147 associated with PROCESS will inherit the coding system used to decode
1148 the process output. */)
1150 register Lisp_Object process
;
1152 CHECK_PROCESS (process
);
1153 return XPROCESS (process
)->inherit_coding_system_flag
;
1156 DEFUN ("set-process-query-on-exit-flag",
1157 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1159 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1160 If the second argument FLAG is non-nil, Emacs will query the user before
1161 exiting if PROCESS is running. */)
1163 register Lisp_Object process
, flag
;
1165 CHECK_PROCESS (process
);
1166 XPROCESS (process
)->kill_without_query
= Fnull (flag
);
1170 DEFUN ("process-query-on-exit-flag",
1171 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1173 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1175 register Lisp_Object process
;
1177 CHECK_PROCESS (process
);
1178 return Fnull (XPROCESS (process
)->kill_without_query
);
1181 #ifdef DATAGRAM_SOCKETS
1182 Lisp_Object
Fprocess_datagram_address ();
1185 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1187 doc
: /* Return the contact info of PROCESS; t for a real child.
1188 For a net connection, the value depends on the optional KEY arg.
1189 If KEY is nil, value is a cons cell of the form (HOST SERVICE),
1190 if KEY is t, the complete contact information for the connection is
1191 returned, else the specific value for the keyword KEY is returned.
1192 See `make-network-process' for a list of keywords. */)
1194 register Lisp_Object process
, key
;
1196 Lisp_Object contact
;
1198 CHECK_PROCESS (process
);
1199 contact
= XPROCESS (process
)->childp
;
1201 #ifdef DATAGRAM_SOCKETS
1202 if (DATAGRAM_CONN_P (process
)
1203 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1204 contact
= Fplist_put (contact
, QCremote
,
1205 Fprocess_datagram_address (process
));
1208 if (!NETCONN_P (process
) || EQ (key
, Qt
))
1211 return Fcons (Fplist_get (contact
, QChost
),
1212 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1213 return Fplist_get (contact
, key
);
1216 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1218 doc
: /* Return the plist of PROCESS. */)
1220 register Lisp_Object process
;
1222 CHECK_PROCESS (process
);
1223 return XPROCESS (process
)->plist
;
1226 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1228 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1230 register Lisp_Object process
, plist
;
1232 CHECK_PROCESS (process
);
1235 XPROCESS (process
)->plist
= plist
;
1239 #if 0 /* Turned off because we don't currently record this info
1240 in the process. Perhaps add it. */
1241 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1242 doc
: /* Return the connection type of PROCESS.
1243 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1244 a socket connection. */)
1246 Lisp_Object process
;
1248 return XPROCESS (process
)->type
;
1253 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1255 doc
: /* Convert network ADDRESS from internal format to a string.
1256 A 4 or 5 element vector represents an IPv4 address (with port number).
1257 An 8 or 9 element vector represents an IPv6 address (with port number).
1258 If optional second argument OMIT-PORT is non-nil, don't include a port
1259 number in the string, even when present in ADDRESS.
1260 Returns nil if format of ADDRESS is invalid. */)
1261 (address
, omit_port
)
1262 Lisp_Object address
, omit_port
;
1267 if (STRINGP (address
)) /* AF_LOCAL */
1270 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1272 register struct Lisp_Vector
*p
= XVECTOR (address
);
1273 Lisp_Object args
[10];
1276 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1278 args
[0] = build_string ("%d.%d.%d.%d");
1281 else if (p
->size
== 5)
1283 args
[0] = build_string ("%d.%d.%d.%d:%d");
1286 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1288 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1291 else if (p
->size
== 9)
1293 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1299 for (i
= 0; i
< nargs
; i
++)
1301 EMACS_INT element
= XINT (p
->contents
[i
]);
1303 if (element
< 0 || element
> 65535)
1306 if (nargs
<= 5 /* IPv4 */
1307 && i
< 4 /* host, not port */
1311 args
[i
+1] = p
->contents
[i
];
1314 return Fformat (nargs
+1, args
);
1317 if (CONSP (address
))
1319 Lisp_Object args
[2];
1320 args
[0] = build_string ("<Family %d>");
1321 args
[1] = Fcar (address
);
1322 return Fformat (2, args
);
1330 list_processes_1 (query_only
)
1331 Lisp_Object query_only
;
1333 register Lisp_Object tail
, tem
;
1334 Lisp_Object proc
, minspace
, tem1
;
1335 register struct Lisp_Process
*p
;
1337 int w_proc
, w_buffer
, w_tty
;
1339 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1341 w_proc
= 4; /* Proc */
1342 w_buffer
= 6; /* Buffer */
1343 w_tty
= 0; /* Omit if no ttys */
1345 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
1349 proc
= Fcdr (Fcar (tail
));
1350 p
= XPROCESS (proc
);
1351 if (NILP (p
->childp
))
1353 if (!NILP (query_only
) && !NILP (p
->kill_without_query
))
1355 if (STRINGP (p
->name
)
1356 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1358 if (!NILP (p
->buffer
))
1360 if (NILP (XBUFFER (p
->buffer
)->name
) && w_buffer
< 8)
1361 w_buffer
= 8; /* (Killed) */
1362 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1365 if (STRINGP (p
->tty_name
)
1366 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1370 XSETFASTINT (i_status
, w_proc
+ 1);
1371 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1374 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1375 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1380 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1383 XSETFASTINT (minspace
, 1);
1385 set_buffer_internal (XBUFFER (Vstandard_output
));
1386 current_buffer
->undo_list
= Qt
;
1388 current_buffer
->truncate_lines
= Qt
;
1390 write_string ("Proc", -1);
1391 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1392 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1395 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1397 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1398 write_string ("\n", -1);
1400 write_string ("----", -1);
1401 Findent_to (i_status
, minspace
); write_string ("------", -1);
1402 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1405 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1407 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1408 write_string ("\n", -1);
1410 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
1414 proc
= Fcdr (Fcar (tail
));
1415 p
= XPROCESS (proc
);
1416 if (NILP (p
->childp
))
1418 if (!NILP (query_only
) && !NILP (p
->kill_without_query
))
1421 Finsert (1, &p
->name
);
1422 Findent_to (i_status
, minspace
);
1424 if (p
->raw_status_new
)
1427 if (CONSP (p
->status
))
1428 symbol
= XCAR (p
->status
);
1430 if (EQ (symbol
, Qsignal
))
1433 tem
= Fcar (Fcdr (p
->status
));
1435 if (XINT (tem
) < NSIG
)
1436 write_string (sys_errlist
[XINT (tem
)], -1);
1439 Fprinc (symbol
, Qnil
);
1441 else if (NETCONN1_P (p
))
1443 if (EQ (symbol
, Qexit
))
1444 write_string ("closed", -1);
1445 else if (EQ (p
->command
, Qt
))
1446 write_string ("stopped", -1);
1447 else if (EQ (symbol
, Qrun
))
1448 write_string ("open", -1);
1450 Fprinc (symbol
, Qnil
);
1453 Fprinc (symbol
, Qnil
);
1455 if (EQ (symbol
, Qexit
))
1458 tem
= Fcar (Fcdr (p
->status
));
1461 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1462 write_string (tembuf
, -1);
1466 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1469 Findent_to (i_buffer
, minspace
);
1470 if (NILP (p
->buffer
))
1471 insert_string ("(none)");
1472 else if (NILP (XBUFFER (p
->buffer
)->name
))
1473 insert_string ("(Killed)");
1475 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1479 Findent_to (i_tty
, minspace
);
1480 if (STRINGP (p
->tty_name
))
1481 Finsert (1, &p
->tty_name
);
1484 Findent_to (i_command
, minspace
);
1486 if (EQ (p
->status
, Qlisten
))
1488 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1489 if (INTEGERP (port
))
1490 port
= Fnumber_to_string (port
);
1492 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1493 sprintf (tembuf
, "(network %s server on %s)\n",
1494 (DATAGRAM_CHAN_P (XINT (p
->infd
)) ? "datagram" : "stream"),
1495 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1496 insert_string (tembuf
);
1498 else if (NETCONN1_P (p
))
1500 /* For a local socket, there is no host name,
1501 so display service instead. */
1502 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1503 if (!STRINGP (host
))
1505 host
= Fplist_get (p
->childp
, QCservice
);
1506 if (INTEGERP (host
))
1507 host
= Fnumber_to_string (host
);
1510 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1511 sprintf (tembuf
, "(network %s connection to %s)\n",
1512 (DATAGRAM_CHAN_P (XINT (p
->infd
)) ? "datagram" : "stream"),
1513 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1514 insert_string (tembuf
);
1526 insert_string (" ");
1528 insert_string ("\n");
1532 status_notify (NULL
);
1536 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1537 doc
: /* Display a list of all processes.
1538 If optional argument QUERY-ONLY is non-nil, only processes with
1539 the query-on-exit flag set will be listed.
1540 Any process listed as exited or signaled is actually eliminated
1541 after the listing is made. */)
1543 Lisp_Object query_only
;
1545 internal_with_output_to_temp_buffer ("*Process List*",
1546 list_processes_1
, query_only
);
1550 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1551 doc
: /* Return a list of all processes. */)
1554 return Fmapcar (Qcdr
, Vprocess_alist
);
1557 /* Starting asynchronous inferior processes. */
1559 static Lisp_Object
start_process_unwind ();
1561 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1562 doc
: /* Start a program in a subprocess. Return the process object for it.
1563 NAME is name for process. It is modified if necessary to make it unique.
1564 BUFFER is the buffer (or buffer name) to associate with the process.
1566 Process output (both standard output and standard error streams) goes
1567 at end of BUFFER, unless you specify an output stream or filter
1568 function to handle the output. BUFFER may also be nil, meaning that
1569 this process is not associated with any buffer.
1571 PROGRAM is the program file name. It is searched for in PATH.
1572 Remaining arguments are strings to give program as arguments.
1574 If you want to separate standard output from standard error, invoke
1575 the command through a shell and redirect one of them using the shell
1578 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1581 register Lisp_Object
*args
;
1583 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1585 register unsigned char *new_argv
;
1588 register unsigned char **new_argv
;
1591 int count
= SPECPDL_INDEX ();
1595 buffer
= Fget_buffer_create (buffer
);
1597 /* Make sure that the child will be able to chdir to the current
1598 buffer's current directory, or its unhandled equivalent. We
1599 can't just have the child check for an error when it does the
1600 chdir, since it's in a vfork.
1602 We have to GCPRO around this because Fexpand_file_name and
1603 Funhandled_file_name_directory might call a file name handling
1604 function. The argument list is protected by the caller, so all
1605 we really have to worry about is buffer. */
1607 struct gcpro gcpro1
, gcpro2
;
1609 current_dir
= current_buffer
->directory
;
1611 GCPRO2 (buffer
, current_dir
);
1614 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
1616 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1617 report_file_error ("Setting current directory",
1618 Fcons (current_buffer
->directory
, Qnil
));
1624 CHECK_STRING (name
);
1628 CHECK_STRING (program
);
1630 proc
= make_process (name
);
1631 /* If an error occurs and we can't start the process, we want to
1632 remove it from the process list. This means that each error
1633 check in create_process doesn't need to call remove_process
1634 itself; it's all taken care of here. */
1635 record_unwind_protect (start_process_unwind
, proc
);
1637 XPROCESS (proc
)->childp
= Qt
;
1638 XPROCESS (proc
)->plist
= Qnil
;
1639 XPROCESS (proc
)->buffer
= buffer
;
1640 XPROCESS (proc
)->sentinel
= Qnil
;
1641 XPROCESS (proc
)->filter
= Qnil
;
1642 XPROCESS (proc
)->filter_multibyte
1643 = buffer_defaults
.enable_multibyte_characters
;
1644 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1646 #ifdef ADAPTIVE_READ_BUFFERING
1647 XPROCESS (proc
)->adaptive_read_buffering
= Vprocess_adaptive_read_buffering
;
1650 /* Make the process marker point into the process buffer (if any). */
1651 if (BUFFERP (buffer
))
1652 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1653 BUF_ZV (XBUFFER (buffer
)),
1654 BUF_ZV_BYTE (XBUFFER (buffer
)));
1657 /* Decide coding systems for communicating with the process. Here
1658 we don't setup the structure coding_system nor pay attention to
1659 unibyte mode. They are done in create_process. */
1661 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1662 Lisp_Object coding_systems
= Qt
;
1663 Lisp_Object val
, *args2
;
1664 struct gcpro gcpro1
, gcpro2
;
1666 val
= Vcoding_system_for_read
;
1669 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1670 args2
[0] = Qstart_process
;
1671 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1672 GCPRO2 (proc
, current_dir
);
1673 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1675 if (CONSP (coding_systems
))
1676 val
= XCAR (coding_systems
);
1677 else if (CONSP (Vdefault_process_coding_system
))
1678 val
= XCAR (Vdefault_process_coding_system
);
1680 XPROCESS (proc
)->decode_coding_system
= val
;
1682 val
= Vcoding_system_for_write
;
1685 if (EQ (coding_systems
, Qt
))
1687 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1688 args2
[0] = Qstart_process
;
1689 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1690 GCPRO2 (proc
, current_dir
);
1691 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1694 if (CONSP (coding_systems
))
1695 val
= XCDR (coding_systems
);
1696 else if (CONSP (Vdefault_process_coding_system
))
1697 val
= XCDR (Vdefault_process_coding_system
);
1699 XPROCESS (proc
)->encode_coding_system
= val
;
1703 /* Make a one member argv with all args concatenated
1704 together separated by a blank. */
1705 len
= SBYTES (program
) + 2;
1706 for (i
= 3; i
< nargs
; i
++)
1710 len
+= SBYTES (tem
) + 1; /* count the blank */
1712 new_argv
= (unsigned char *) alloca (len
);
1713 strcpy (new_argv
, SDATA (program
));
1714 for (i
= 3; i
< nargs
; i
++)
1718 strcat (new_argv
, " ");
1719 strcat (new_argv
, SDATA (tem
));
1721 /* Need to add code here to check for program existence on VMS */
1724 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1726 /* If program file name is not absolute, search our path for it.
1727 Put the name we will really use in TEM. */
1728 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1729 && !(SCHARS (program
) > 1
1730 && IS_DEVICE_SEP (SREF (program
, 1))))
1732 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1735 GCPRO4 (name
, program
, buffer
, current_dir
);
1736 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1739 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1740 tem
= Fexpand_file_name (tem
, Qnil
);
1744 if (!NILP (Ffile_directory_p (program
)))
1745 error ("Specified program for new process is a directory");
1749 /* If program file name starts with /: for quoting a magic name,
1751 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1752 && SREF (tem
, 1) == ':')
1753 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1755 /* Encode the file name and put it in NEW_ARGV.
1756 That's where the child will use it to execute the program. */
1757 tem
= ENCODE_FILE (tem
);
1758 new_argv
[0] = SDATA (tem
);
1760 /* Here we encode arguments by the coding system used for sending
1761 data to the process. We don't support using different coding
1762 systems for encoding arguments and for encoding data sent to the
1765 for (i
= 3; i
< nargs
; i
++)
1769 if (STRING_MULTIBYTE (tem
))
1770 tem
= (code_convert_string_norecord
1771 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1772 new_argv
[i
- 2] = SDATA (tem
);
1774 new_argv
[i
- 2] = 0;
1775 #endif /* not VMS */
1777 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1778 XPROCESS (proc
)->decoding_carryover
= make_number (0);
1779 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1780 XPROCESS (proc
)->encoding_carryover
= make_number (0);
1782 XPROCESS (proc
)->inherit_coding_system_flag
1783 = (NILP (buffer
) || !inherit_process_coding_system
1786 create_process (proc
, (char **) new_argv
, current_dir
);
1788 return unbind_to (count
, proc
);
1791 /* This function is the unwind_protect form for Fstart_process. If
1792 PROC doesn't have its pid set, then we know someone has signaled
1793 an error and the process wasn't started successfully, so we should
1794 remove it from the process list. */
1796 start_process_unwind (proc
)
1799 if (!PROCESSP (proc
))
1802 /* Was PROC started successfully? */
1803 if (XPROCESS (proc
)->pid
<= 0)
1804 remove_process (proc
);
1810 create_process_1 (timer
)
1811 struct atimer
*timer
;
1813 /* Nothing to do. */
1817 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1820 /* Mimic blocking of signals on system V, which doesn't really have it. */
1822 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1823 int sigchld_deferred
;
1826 create_process_sigchld ()
1828 signal (SIGCHLD
, create_process_sigchld
);
1830 sigchld_deferred
= 1;
1836 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1838 create_process (process
, new_argv
, current_dir
)
1839 Lisp_Object process
;
1841 Lisp_Object current_dir
;
1843 int inchannel
, outchannel
;
1846 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1847 int wait_child_setup
[2];
1849 #ifdef POSIX_SIGNALS
1852 struct sigaction sigint_action
;
1853 struct sigaction sigquit_action
;
1855 struct sigaction sighup_action
;
1857 #else /* !POSIX_SIGNALS */
1860 SIGTYPE (*sigchld
)();
1863 #endif /* !POSIX_SIGNALS */
1864 /* Use volatile to protect variables from being clobbered by longjmp. */
1865 volatile int forkin
, forkout
;
1866 volatile int pty_flag
= 0;
1868 extern char **environ
;
1871 inchannel
= outchannel
= -1;
1874 if (!NILP (Vprocess_connection_type
))
1875 outchannel
= inchannel
= allocate_pty ();
1879 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1880 /* On most USG systems it does not work to open the pty's tty here,
1881 then close it and reopen it in the child. */
1883 /* Don't let this terminal become our controlling terminal
1884 (in case we don't have one). */
1885 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1887 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1890 report_file_error ("Opening pty", Qnil
);
1892 forkin
= forkout
= -1;
1893 #endif /* not USG, or USG_SUBTTY_WORKS */
1897 #endif /* HAVE_PTYS */
1900 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1901 report_file_error ("Opening socketpair", Qnil
);
1902 outchannel
= inchannel
= sv
[0];
1903 forkout
= forkin
= sv
[1];
1905 #else /* not SKTPAIR */
1910 report_file_error ("Creating pipe", Qnil
);
1916 emacs_close (inchannel
);
1917 emacs_close (forkout
);
1918 report_file_error ("Creating pipe", Qnil
);
1923 #endif /* not SKTPAIR */
1925 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1929 tem
= pipe (wait_child_setup
);
1931 report_file_error ("Creating pipe", Qnil
);
1932 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1934 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1937 emacs_close (wait_child_setup
[0]);
1938 emacs_close (wait_child_setup
[1]);
1939 report_file_error ("Setting file descriptor flags", Qnil
);
1945 /* Replaced by close_process_descs */
1946 set_exclusive_use (inchannel
);
1947 set_exclusive_use (outchannel
);
1950 /* Stride people say it's a mystery why this is needed
1951 as well as the O_NDELAY, but that it fails without this. */
1952 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1955 ioctl (inchannel
, FIONBIO
, &one
);
1960 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1961 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1964 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1965 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1969 /* Record this as an active process, with its channels.
1970 As a result, child_setup will close Emacs's side of the pipes. */
1971 chan_process
[inchannel
] = process
;
1972 XSETINT (XPROCESS (process
)->infd
, inchannel
);
1973 XSETINT (XPROCESS (process
)->outfd
, outchannel
);
1975 /* Previously we recorded the tty descriptor used in the subprocess.
1976 It was only used for getting the foreground tty process, so now
1977 we just reopen the device (see emacs_get_tty_pgrp) as this is
1978 more portable (see USG_SUBTTY_WORKS above). */
1980 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1981 XPROCESS (process
)->status
= Qrun
;
1982 setup_process_coding_systems (process
);
1984 /* Delay interrupts until we have a chance to store
1985 the new fork's pid in its process structure */
1986 #ifdef POSIX_SIGNALS
1987 sigemptyset (&blocked
);
1989 sigaddset (&blocked
, SIGCHLD
);
1991 #ifdef HAVE_WORKING_VFORK
1992 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1993 this sets the parent's signal handlers as well as the child's.
1994 So delay all interrupts whose handlers the child might munge,
1995 and record the current handlers so they can be restored later. */
1996 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1997 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1999 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
2001 #endif /* HAVE_WORKING_VFORK */
2002 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
2003 #else /* !POSIX_SIGNALS */
2007 #else /* not BSD4_1 */
2008 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
2009 sigsetmask (sigmask (SIGCHLD
));
2010 #else /* ordinary USG */
2012 sigchld_deferred
= 0;
2013 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
2015 #endif /* ordinary USG */
2016 #endif /* not BSD4_1 */
2017 #endif /* SIGCHLD */
2018 #endif /* !POSIX_SIGNALS */
2020 FD_SET (inchannel
, &input_wait_mask
);
2021 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2022 if (inchannel
> max_process_desc
)
2023 max_process_desc
= inchannel
;
2025 /* Until we store the proper pid, enable sigchld_handler
2026 to recognize an unknown pid as standing for this process.
2027 It is very important not to let this `marker' value stay
2028 in the table after this function has returned; if it does
2029 it might cause call-process to hang and subsequent asynchronous
2030 processes to get their return values scrambled. */
2031 XPROCESS (process
)->pid
= -1;
2036 /* child_setup must clobber environ on systems with true vfork.
2037 Protect it from permanent change. */
2038 char **save_environ
= environ
;
2040 current_dir
= ENCODE_FILE (current_dir
);
2045 #endif /* not WINDOWSNT */
2047 int xforkin
= forkin
;
2048 int xforkout
= forkout
;
2050 #if 0 /* This was probably a mistake--it duplicates code later on,
2051 but fails to handle all the cases. */
2052 /* Make sure SIGCHLD is not blocked in the child. */
2053 sigsetmask (SIGEMPTYMASK
);
2056 /* Make the pty be the controlling terminal of the process. */
2058 /* First, disconnect its current controlling terminal. */
2060 /* We tried doing setsid only if pty_flag, but it caused
2061 process_set_signal to fail on SGI when using a pipe. */
2063 /* Make the pty's terminal the controlling terminal. */
2067 /* We ignore the return value
2068 because faith@cs.unc.edu says that is necessary on Linux. */
2069 ioctl (xforkin
, TIOCSCTTY
, 0);
2072 #else /* not HAVE_SETSID */
2074 /* It's very important to call setpgrp here and no time
2075 afterwards. Otherwise, we lose our controlling tty which
2076 is set when we open the pty. */
2079 #endif /* not HAVE_SETSID */
2080 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2081 if (pty_flag
&& xforkin
>= 0)
2084 tcgetattr (xforkin
, &t
);
2086 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2087 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2090 #if defined (NTTYDISC) && defined (TIOCSETD)
2091 if (pty_flag
&& xforkin
>= 0)
2093 /* Use new line discipline. */
2094 int ldisc
= NTTYDISC
;
2095 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2100 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2101 can do TIOCSPGRP only to the process's controlling tty. */
2104 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2105 I can't test it since I don't have 4.3. */
2106 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2107 ioctl (j
, TIOCNOTTY
, 0);
2110 /* In order to get a controlling terminal on some versions
2111 of BSD, it is necessary to put the process in pgrp 0
2112 before it opens the terminal. */
2120 #endif /* TIOCNOTTY */
2122 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
2123 /*** There is a suggestion that this ought to be a
2124 conditional on TIOCSPGRP,
2125 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2126 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2127 that system does seem to need this code, even though
2128 both HAVE_SETSID and TIOCSCTTY are defined. */
2129 /* Now close the pty (if we had it open) and reopen it.
2130 This makes the pty the controlling terminal of the subprocess. */
2133 #ifdef SET_CHILD_PTY_PGRP
2134 int pgrp
= getpid ();
2137 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2140 emacs_close (xforkin
);
2141 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2145 emacs_write (1, "Couldn't open the pty terminal ", 31);
2146 emacs_write (1, pty_name
, strlen (pty_name
));
2147 emacs_write (1, "\n", 1);
2151 #ifdef SET_CHILD_PTY_PGRP
2152 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
2153 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
2156 #endif /* not UNIPLUS and not RTU and not DONT_REOPEN_PTY */
2158 #ifdef SETUP_SLAVE_PTY
2163 #endif /* SETUP_SLAVE_PTY */
2165 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2166 Now reenable it in the child, so it will die when we want it to. */
2168 signal (SIGHUP
, SIG_DFL
);
2170 #endif /* HAVE_PTYS */
2172 signal (SIGINT
, SIG_DFL
);
2173 signal (SIGQUIT
, SIG_DFL
);
2175 /* Stop blocking signals in the child. */
2176 #ifdef POSIX_SIGNALS
2177 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2178 #else /* !POSIX_SIGNALS */
2182 #else /* not BSD4_1 */
2183 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
2184 sigsetmask (SIGEMPTYMASK
);
2185 #else /* ordinary USG */
2187 signal (SIGCHLD
, sigchld
);
2189 #endif /* ordinary USG */
2190 #endif /* not BSD4_1 */
2191 #endif /* SIGCHLD */
2192 #endif /* !POSIX_SIGNALS */
2195 child_setup_tty (xforkout
);
2197 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2198 new_argv
, 1, current_dir
);
2199 #else /* not WINDOWSNT */
2201 emacs_close (wait_child_setup
[0]);
2203 child_setup (xforkin
, xforkout
, xforkout
,
2204 new_argv
, 1, current_dir
);
2205 #endif /* not WINDOWSNT */
2207 environ
= save_environ
;
2212 /* This runs in the Emacs process. */
2216 emacs_close (forkin
);
2217 if (forkin
!= forkout
&& forkout
>= 0)
2218 emacs_close (forkout
);
2222 /* vfork succeeded. */
2223 XPROCESS (process
)->pid
= pid
;
2226 register_child (pid
, inchannel
);
2227 #endif /* WINDOWSNT */
2229 /* If the subfork execv fails, and it exits,
2230 this close hangs. I don't know why.
2231 So have an interrupt jar it loose. */
2233 struct atimer
*timer
;
2237 EMACS_SET_SECS_USECS (offset
, 1, 0);
2238 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2241 emacs_close (forkin
);
2243 cancel_atimer (timer
);
2247 if (forkin
!= forkout
&& forkout
>= 0)
2248 emacs_close (forkout
);
2252 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2255 XPROCESS (process
)->tty_name
= Qnil
;
2257 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2258 /* Wait for child_setup to complete in case that vfork is
2259 actually defined as fork. The descriptor wait_child_setup[1]
2260 of a pipe is closed at the child side either by close-on-exec
2261 on successful execvp or the _exit call in child_setup. */
2265 emacs_close (wait_child_setup
[1]);
2266 emacs_read (wait_child_setup
[0], &dummy
, 1);
2267 emacs_close (wait_child_setup
[0]);
2272 /* Restore the signal state whether vfork succeeded or not.
2273 (We will signal an error, below, if it failed.) */
2274 #ifdef POSIX_SIGNALS
2275 #ifdef HAVE_WORKING_VFORK
2276 /* Restore the parent's signal handlers. */
2277 sigaction (SIGINT
, &sigint_action
, 0);
2278 sigaction (SIGQUIT
, &sigquit_action
, 0);
2280 sigaction (SIGHUP
, &sighup_action
, 0);
2282 #endif /* HAVE_WORKING_VFORK */
2283 /* Stop blocking signals in the parent. */
2284 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2285 #else /* !POSIX_SIGNALS */
2289 #else /* not BSD4_1 */
2290 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
2291 sigsetmask (SIGEMPTYMASK
);
2292 #else /* ordinary USG */
2294 signal (SIGCHLD
, sigchld
);
2295 /* Now really handle any of these signals
2296 that came in during this function. */
2297 if (sigchld_deferred
)
2298 kill (getpid (), SIGCHLD
);
2300 #endif /* ordinary USG */
2301 #endif /* not BSD4_1 */
2302 #endif /* SIGCHLD */
2303 #endif /* !POSIX_SIGNALS */
2305 /* Now generate the error if vfork failed. */
2307 report_file_error ("Doing vfork", Qnil
);
2309 #endif /* not VMS */
2314 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2315 The address family of sa is not included in the result. */
2318 conv_sockaddr_to_lisp (sa
, len
)
2319 struct sockaddr
*sa
;
2322 Lisp_Object address
;
2325 register struct Lisp_Vector
*p
;
2327 switch (sa
->sa_family
)
2331 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2332 len
= sizeof (sin
->sin_addr
) + 1;
2333 address
= Fmake_vector (make_number (len
), Qnil
);
2334 p
= XVECTOR (address
);
2335 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2336 cp
= (unsigned char *)&sin
->sin_addr
;
2342 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2343 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2344 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2345 address
= Fmake_vector (make_number (len
), Qnil
);
2346 p
= XVECTOR (address
);
2347 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2348 for (i
= 0; i
< len
; i
++)
2349 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2353 #ifdef HAVE_LOCAL_SOCKETS
2356 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2357 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2358 if (sockun
->sun_path
[i
] == 0)
2360 return make_unibyte_string (sockun
->sun_path
, i
);
2364 len
-= sizeof (sa
->sa_family
);
2365 address
= Fcons (make_number (sa
->sa_family
),
2366 Fmake_vector (make_number (len
), Qnil
));
2367 p
= XVECTOR (XCDR (address
));
2368 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2374 p
->contents
[i
++] = make_number (*cp
++);
2380 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2383 get_lisp_to_sockaddr_size (address
, familyp
)
2384 Lisp_Object address
;
2387 register struct Lisp_Vector
*p
;
2389 if (VECTORP (address
))
2391 p
= XVECTOR (address
);
2395 return sizeof (struct sockaddr_in
);
2398 else if (p
->size
== 9)
2400 *familyp
= AF_INET6
;
2401 return sizeof (struct sockaddr_in6
);
2405 #ifdef HAVE_LOCAL_SOCKETS
2406 else if (STRINGP (address
))
2408 *familyp
= AF_LOCAL
;
2409 return sizeof (struct sockaddr_un
);
2412 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2414 struct sockaddr
*sa
;
2415 *familyp
= XINT (XCAR (address
));
2416 p
= XVECTOR (XCDR (address
));
2417 return p
->size
+ sizeof (sa
->sa_family
);
2422 /* Convert an address object (vector or string) to an internal sockaddr.
2424 The address format has been basically validated by
2425 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2426 it could have come from user data. So if FAMILY is not valid,
2427 we return after zeroing *SA. */
2430 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2432 Lisp_Object address
;
2433 struct sockaddr
*sa
;
2436 register struct Lisp_Vector
*p
;
2437 register unsigned char *cp
= NULL
;
2442 if (VECTORP (address
))
2444 p
= XVECTOR (address
);
2445 if (family
== AF_INET
)
2447 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2448 len
= sizeof (sin
->sin_addr
) + 1;
2449 i
= XINT (p
->contents
[--len
]);
2450 sin
->sin_port
= htons (i
);
2451 cp
= (unsigned char *)&sin
->sin_addr
;
2452 sa
->sa_family
= family
;
2455 else if (family
== AF_INET6
)
2457 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2458 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2459 len
= sizeof (sin6
->sin6_addr
) + 1;
2460 i
= XINT (p
->contents
[--len
]);
2461 sin6
->sin6_port
= htons (i
);
2462 for (i
= 0; i
< len
; i
++)
2463 if (INTEGERP (p
->contents
[i
]))
2465 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2468 sa
->sa_family
= family
;
2473 else if (STRINGP (address
))
2475 #ifdef HAVE_LOCAL_SOCKETS
2476 if (family
== AF_LOCAL
)
2478 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2479 cp
= SDATA (address
);
2480 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2481 sockun
->sun_path
[i
] = *cp
++;
2482 sa
->sa_family
= family
;
2489 p
= XVECTOR (XCDR (address
));
2490 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2493 for (i
= 0; i
< len
; i
++)
2494 if (INTEGERP (p
->contents
[i
]))
2495 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2498 #ifdef DATAGRAM_SOCKETS
2499 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2501 doc
: /* Get the current datagram address associated with PROCESS. */)
2503 Lisp_Object process
;
2507 CHECK_PROCESS (process
);
2509 if (!DATAGRAM_CONN_P (process
))
2512 channel
= XINT (XPROCESS (process
)->infd
);
2513 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2514 datagram_address
[channel
].len
);
2517 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2519 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2520 Returns nil upon error setting address, ADDRESS otherwise. */)
2522 Lisp_Object process
, address
;
2527 CHECK_PROCESS (process
);
2529 if (!DATAGRAM_CONN_P (process
))
2532 channel
= XINT (XPROCESS (process
)->infd
);
2534 len
= get_lisp_to_sockaddr_size (address
, &family
);
2535 if (datagram_address
[channel
].len
!= len
)
2537 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2543 static struct socket_options
{
2544 /* The name of this option. Should be lowercase version of option
2545 name without SO_ prefix. */
2547 /* Option level SOL_... */
2549 /* Option number SO_... */
2551 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2552 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2553 } socket_options
[] =
2555 #ifdef SO_BINDTODEVICE
2556 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2559 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2562 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2565 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2568 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2571 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2574 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2577 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2579 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2582 /* Set option OPT to value VAL on socket S.
2584 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2585 Signals an error if setting a known option fails.
2589 set_socket_option (s
, opt
, val
)
2591 Lisp_Object opt
, val
;
2594 struct socket_options
*sopt
;
2599 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2600 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2601 if (strcmp (name
, sopt
->name
) == 0)
2604 switch (sopt
->opttype
)
2609 optval
= NILP (val
) ? 0 : 1;
2610 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2611 &optval
, sizeof (optval
));
2619 optval
= XINT (val
);
2621 error ("Bad option value for %s", name
);
2622 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2623 &optval
, sizeof (optval
));
2627 #ifdef SO_BINDTODEVICE
2630 char devname
[IFNAMSIZ
+1];
2632 /* This is broken, at least in the Linux 2.4 kernel.
2633 To unbind, the arg must be a zero integer, not the empty string.
2634 This should work on all systems. KFS. 2003-09-23. */
2635 bzero (devname
, sizeof devname
);
2638 char *arg
= (char *) SDATA (val
);
2639 int len
= min (strlen (arg
), IFNAMSIZ
);
2640 bcopy (arg
, devname
, len
);
2642 else if (!NILP (val
))
2643 error ("Bad option value for %s", name
);
2644 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2653 struct linger linger
;
2656 linger
.l_linger
= 0;
2658 linger
.l_linger
= XINT (val
);
2660 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2661 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2662 &linger
, sizeof (linger
));
2672 report_file_error ("Cannot set network option",
2673 Fcons (opt
, Fcons (val
, Qnil
)));
2674 return (1 << sopt
->optbit
);
2678 DEFUN ("set-network-process-option",
2679 Fset_network_process_option
, Sset_network_process_option
,
2681 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2682 See `make-network-process' for a list of options and values.
2683 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2684 OPTION is not a supported option, return nil instead; otherwise return t. */)
2685 (process
, option
, value
, no_error
)
2686 Lisp_Object process
, option
, value
;
2687 Lisp_Object no_error
;
2690 struct Lisp_Process
*p
;
2692 CHECK_PROCESS (process
);
2693 p
= XPROCESS (process
);
2694 if (!NETCONN1_P (p
))
2695 error ("Process is not a network process");
2699 error ("Process is not running");
2701 if (set_socket_option (s
, option
, value
))
2703 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2707 if (NILP (no_error
))
2708 error ("Unknown or unsupported option");
2714 /* A version of request_sigio suitable for a record_unwind_protect. */
2717 unwind_request_sigio (dummy
)
2720 if (interrupt_input
)
2725 /* Create a network stream/datagram client/server process. Treated
2726 exactly like a normal process when reading and writing. Primary
2727 differences are in status display and process deletion. A network
2728 connection has no PID; you cannot signal it. All you can do is
2729 stop/continue it and deactivate/close it via delete-process */
2731 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2733 doc
: /* Create and return a network server or client process.
2735 In Emacs, network connections are represented by process objects, so
2736 input and output work as for subprocesses and `delete-process' closes
2737 a network connection. However, a network process has no process id,
2738 it cannot be signaled, and the status codes are different from normal
2741 Arguments are specified as keyword/argument pairs. The following
2742 arguments are defined:
2744 :name NAME -- NAME is name for process. It is modified if necessary
2747 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2748 with the process. Process output goes at end of that buffer, unless
2749 you specify an output stream or filter function to handle the output.
2750 BUFFER may be also nil, meaning that this process is not associated
2753 :host HOST -- HOST is name of the host to connect to, or its IP
2754 address. The symbol `local' specifies the local host. If specified
2755 for a server process, it must be a valid name or address for the local
2756 host, and only clients connecting to that address will be accepted.
2758 :service SERVICE -- SERVICE is name of the service desired, or an
2759 integer specifying a port number to connect to. If SERVICE is t,
2760 a random port number is selected for the server. (If Emacs was
2761 compiled with getaddrinfo, a port number can also be specified as a
2762 string, e.g. "80", as well as an integer. This is not portable.)
2764 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2765 stream type connection, `datagram' creates a datagram type connection.
2767 :family FAMILY -- FAMILY is the address (and protocol) family for the
2768 service specified by HOST and SERVICE. The default (nil) is to use
2769 whatever address family (IPv4 or IPv6) that is defined for the host
2770 and port number specified by HOST and SERVICE. Other address families
2772 local -- for a local (i.e. UNIX) address specified by SERVICE.
2773 ipv4 -- use IPv4 address family only.
2774 ipv6 -- use IPv6 address family only.
2776 :local ADDRESS -- ADDRESS is the local address used for the connection.
2777 This parameter is ignored when opening a client process. When specified
2778 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2780 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2781 connection. This parameter is ignored when opening a stream server
2782 process. For a datagram server process, it specifies the initial
2783 setting of the remote datagram address. When specified for a client
2784 process, the FAMILY, HOST, and SERVICE args are ignored.
2786 The format of ADDRESS depends on the address family:
2787 - An IPv4 address is represented as an vector of integers [A B C D P]
2788 corresponding to numeric IP address A.B.C.D and port number P.
2789 - A local address is represented as a string with the address in the
2790 local address space.
2791 - An "unsupported family" address is represented by a cons (F . AV)
2792 where F is the family number and AV is a vector containing the socket
2793 address data with one element per address data byte. Do not rely on
2794 this format in portable code, as it may depend on implementation
2795 defined constants, data sizes, and data structure alignment.
2797 :coding CODING -- If CODING is a symbol, it specifies the coding
2798 system used for both reading and writing for this process. If CODING
2799 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2800 ENCODING is used for writing.
2802 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2803 return without waiting for the connection to complete; instead, the
2804 sentinel function will be called with second arg matching "open" (if
2805 successful) or "failed" when the connect completes. Default is to use
2806 a blocking connect (i.e. wait) for stream type connections.
2808 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2809 running when Emacs is exited.
2811 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2812 In the stopped state, a server process does not accept new
2813 connections, and a client process does not handle incoming traffic.
2814 The stopped state is cleared by `continue-process' and set by
2817 :filter FILTER -- Install FILTER as the process filter.
2819 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
2820 process filter are multibyte, otherwise they are unibyte.
2821 If this keyword is not specified, the strings are multibyte if
2822 `default-enable-multibyte-characters' is non-nil.
2824 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2826 :log LOG -- Install LOG as the server process log function. This
2827 function is called when the server accepts a network connection from a
2828 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2829 is the server process, CLIENT is the new process for the connection,
2830 and MESSAGE is a string.
2832 :plist PLIST -- Install PLIST as the new process' initial plist.
2834 :server QLEN -- if QLEN is non-nil, create a server process for the
2835 specified FAMILY, SERVICE, and connection type (stream or datagram).
2836 If QLEN is an integer, it is used as the max. length of the server's
2837 pending connection queue (also known as the backlog); the default
2838 queue length is 5. Default is to create a client process.
2840 The following network options can be specified for this connection:
2842 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2843 :dontroute BOOL -- Only send to directly connected hosts.
2844 :keepalive BOOL -- Send keep-alive messages on network stream.
2845 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2846 :oobinline BOOL -- Place out-of-band data in receive data stream.
2847 :priority INT -- Set protocol defined priority for sent packets.
2848 :reuseaddr BOOL -- Allow reusing a recently used local address
2849 (this is allowed by default for a server process).
2850 :bindtodevice NAME -- bind to interface NAME. Using this may require
2851 special privileges on some systems.
2853 Consult the relevant system programmer's manual pages for more
2854 information on using these options.
2857 A server process will listen for and accept connections from clients.
2858 When a client connection is accepted, a new network process is created
2859 for the connection with the following parameters:
2861 - The client's process name is constructed by concatenating the server
2862 process' NAME and a client identification string.
2863 - If the FILTER argument is non-nil, the client process will not get a
2864 separate process buffer; otherwise, the client's process buffer is a newly
2865 created buffer named after the server process' BUFFER name or process
2866 NAME concatenated with the client identification string.
2867 - The connection type and the process filter and sentinel parameters are
2868 inherited from the server process' TYPE, FILTER and SENTINEL.
2869 - The client process' contact info is set according to the client's
2870 addressing information (typically an IP address and a port number).
2871 - The client process' plist is initialized from the server's plist.
2873 Notice that the FILTER and SENTINEL args are never used directly by
2874 the server process. Also, the BUFFER argument is not used directly by
2875 the server process, but via the optional :log function, accepted (and
2876 failed) connections may be logged in the server process' buffer.
2878 The original argument list, modified with the actual connection
2879 information, is available via the `process-contact' function.
2881 usage: (make-network-process &rest ARGS) */)
2887 Lisp_Object contact
;
2888 struct Lisp_Process
*p
;
2889 #ifdef HAVE_GETADDRINFO
2890 struct addrinfo ai
, *res
, *lres
;
2891 struct addrinfo hints
;
2892 char *portstring
, portbuf
[128];
2893 #else /* HAVE_GETADDRINFO */
2894 struct _emacs_addrinfo
2900 struct sockaddr
*ai_addr
;
2901 struct _emacs_addrinfo
*ai_next
;
2903 #endif /* HAVE_GETADDRINFO */
2904 struct sockaddr_in address_in
;
2905 #ifdef HAVE_LOCAL_SOCKETS
2906 struct sockaddr_un address_un
;
2911 int s
= -1, outch
, inch
;
2912 struct gcpro gcpro1
;
2913 int count
= SPECPDL_INDEX ();
2915 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
2917 Lisp_Object name
, buffer
, host
, service
, address
;
2918 Lisp_Object filter
, sentinel
;
2919 int is_non_blocking_client
= 0;
2920 int is_server
= 0, backlog
= 5;
2927 /* Save arguments for process-contact and clone-process. */
2928 contact
= Flist (nargs
, args
);
2932 /* Ensure socket support is loaded if available. */
2933 init_winsock (TRUE
);
2936 /* :type TYPE (nil: stream, datagram */
2937 tem
= Fplist_get (contact
, QCtype
);
2939 socktype
= SOCK_STREAM
;
2940 #ifdef DATAGRAM_SOCKETS
2941 else if (EQ (tem
, Qdatagram
))
2942 socktype
= SOCK_DGRAM
;
2945 error ("Unsupported connection type");
2948 tem
= Fplist_get (contact
, QCserver
);
2951 /* Don't support network sockets when non-blocking mode is
2952 not available, since a blocked Emacs is not useful. */
2953 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY))
2954 error ("Network servers not supported");
2958 backlog
= XINT (tem
);
2962 /* Make QCaddress an alias for :local (server) or :remote (client). */
2963 QCaddress
= is_server
? QClocal
: QCremote
;
2966 if (!is_server
&& socktype
== SOCK_STREAM
2967 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
2969 #ifndef NON_BLOCKING_CONNECT
2970 error ("Non-blocking connect not supported");
2972 is_non_blocking_client
= 1;
2976 name
= Fplist_get (contact
, QCname
);
2977 buffer
= Fplist_get (contact
, QCbuffer
);
2978 filter
= Fplist_get (contact
, QCfilter
);
2979 sentinel
= Fplist_get (contact
, QCsentinel
);
2981 CHECK_STRING (name
);
2984 /* Let's handle TERM before things get complicated ... */
2985 host
= Fplist_get (contact
, QChost
);
2986 CHECK_STRING (host
);
2988 service
= Fplist_get (contact
, QCservice
);
2989 if (INTEGERP (service
))
2990 port
= htons ((unsigned short) XINT (service
));
2993 struct servent
*svc_info
;
2994 CHECK_STRING (service
);
2995 svc_info
= getservbyname (SDATA (service
), "tcp");
2997 error ("Unknown service: %s", SDATA (service
));
2998 port
= svc_info
->s_port
;
3001 s
= connect_server (0);
3003 report_file_error ("error creating socket", Fcons (name
, Qnil
));
3004 send_command (s
, C_PORT
, 0, "%s:%d", SDATA (host
), ntohs (port
));
3005 send_command (s
, C_DUMB
, 1, 0);
3007 #else /* not TERM */
3009 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3010 ai
.ai_socktype
= socktype
;
3015 /* :local ADDRESS or :remote ADDRESS */
3016 address
= Fplist_get (contact
, QCaddress
);
3017 if (!NILP (address
))
3019 host
= service
= Qnil
;
3021 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3022 error ("Malformed :address");
3023 ai
.ai_family
= family
;
3024 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3025 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3029 /* :family FAMILY -- nil (for Inet), local, or integer. */
3030 tem
= Fplist_get (contact
, QCfamily
);
3033 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3039 #ifdef HAVE_LOCAL_SOCKETS
3040 else if (EQ (tem
, Qlocal
))
3044 else if (EQ (tem
, Qipv6
))
3047 else if (EQ (tem
, Qipv4
))
3049 else if (INTEGERP (tem
))
3050 family
= XINT (tem
);
3052 error ("Unknown address family");
3054 ai
.ai_family
= family
;
3056 /* :service SERVICE -- string, integer (port number), or t (random port). */
3057 service
= Fplist_get (contact
, QCservice
);
3059 #ifdef HAVE_LOCAL_SOCKETS
3060 if (family
== AF_LOCAL
)
3062 /* Host is not used. */
3064 CHECK_STRING (service
);
3065 bzero (&address_un
, sizeof address_un
);
3066 address_un
.sun_family
= AF_LOCAL
;
3067 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3068 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3069 ai
.ai_addrlen
= sizeof address_un
;
3074 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3075 host
= Fplist_get (contact
, QChost
);
3078 if (EQ (host
, Qlocal
))
3079 host
= build_string ("localhost");
3080 CHECK_STRING (host
);
3083 /* Slow down polling to every ten seconds.
3084 Some kernels have a bug which causes retrying connect to fail
3085 after a connect. Polling can interfere with gethostbyname too. */
3086 #ifdef POLL_FOR_INPUT
3087 if (socktype
== SOCK_STREAM
)
3089 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3090 bind_polling_period (10);
3094 #ifdef HAVE_GETADDRINFO
3095 /* If we have a host, use getaddrinfo to resolve both host and service.
3096 Otherwise, use getservbyname to lookup the service. */
3100 /* SERVICE can either be a string or int.
3101 Convert to a C string for later use by getaddrinfo. */
3102 if (EQ (service
, Qt
))
3104 else if (INTEGERP (service
))
3106 sprintf (portbuf
, "%ld", (long) XINT (service
));
3107 portstring
= portbuf
;
3111 CHECK_STRING (service
);
3112 portstring
= SDATA (service
);
3117 memset (&hints
, 0, sizeof (hints
));
3119 hints
.ai_family
= family
;
3120 hints
.ai_socktype
= socktype
;
3121 hints
.ai_protocol
= 0;
3122 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3124 #ifdef HAVE_GAI_STRERROR
3125 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3127 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3133 #endif /* HAVE_GETADDRINFO */
3135 /* We end up here if getaddrinfo is not defined, or in case no hostname
3136 has been specified (e.g. for a local server process). */
3138 if (EQ (service
, Qt
))
3140 else if (INTEGERP (service
))
3141 port
= htons ((unsigned short) XINT (service
));
3144 struct servent
*svc_info
;
3145 CHECK_STRING (service
);
3146 svc_info
= getservbyname (SDATA (service
),
3147 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3149 error ("Unknown service: %s", SDATA (service
));
3150 port
= svc_info
->s_port
;
3153 bzero (&address_in
, sizeof address_in
);
3154 address_in
.sin_family
= family
;
3155 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3156 address_in
.sin_port
= port
;
3158 #ifndef HAVE_GETADDRINFO
3161 struct hostent
*host_info_ptr
;
3163 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3164 as it may `hang' Emacs for a very long time. */
3167 host_info_ptr
= gethostbyname (SDATA (host
));
3172 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3173 host_info_ptr
->h_length
);
3174 family
= host_info_ptr
->h_addrtype
;
3175 address_in
.sin_family
= family
;
3178 /* Attempt to interpret host as numeric inet address */
3180 IN_ADDR numeric_addr
;
3181 numeric_addr
= inet_addr ((char *) SDATA (host
));
3182 if (NUMERIC_ADDR_ERROR
)
3183 error ("Unknown host \"%s\"", SDATA (host
));
3185 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3186 sizeof (address_in
.sin_addr
));
3190 #endif /* not HAVE_GETADDRINFO */
3192 ai
.ai_family
= family
;
3193 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3194 ai
.ai_addrlen
= sizeof address_in
;
3198 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
3199 when connect is interrupted. So let's not let it get interrupted.
3200 Note we do not turn off polling, because polling is only used
3201 when not interrupt_input, and thus not normally used on the systems
3202 which have this bug. On systems which use polling, there's no way
3203 to quit if polling is turned off. */
3205 && !is_server
&& socktype
== SOCK_STREAM
)
3207 /* Comment from KFS: The original open-network-stream code
3208 didn't unwind protect this, but it seems like the proper
3209 thing to do. In any case, I don't see how it could harm to
3210 do this -- and it makes cleanup (using unbind_to) easier. */
3211 record_unwind_protect (unwind_request_sigio
, Qnil
);
3215 /* Do this in case we never enter the for-loop below. */
3216 count1
= SPECPDL_INDEX ();
3219 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3225 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3232 #ifdef DATAGRAM_SOCKETS
3233 if (!is_server
&& socktype
== SOCK_DGRAM
)
3235 #endif /* DATAGRAM_SOCKETS */
3237 #ifdef NON_BLOCKING_CONNECT
3238 if (is_non_blocking_client
)
3241 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3243 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3255 /* Make us close S if quit. */
3256 record_unwind_protect (close_file_unwind
, make_number (s
));
3258 /* Parse network options in the arg list.
3259 We simply ignore anything which isn't a known option (including other keywords).
3260 An error is signalled if setting a known option fails. */
3261 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3262 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3266 /* Configure as a server socket. */
3268 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3269 explicit :reuseaddr key to override this. */
3270 #ifdef HAVE_LOCAL_SOCKETS
3271 if (family
!= AF_LOCAL
)
3273 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3276 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3277 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3280 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3281 report_file_error ("Cannot bind server socket", Qnil
);
3283 #ifdef HAVE_GETSOCKNAME
3284 if (EQ (service
, Qt
))
3286 struct sockaddr_in sa1
;
3287 int len1
= sizeof (sa1
);
3288 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3290 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3291 service
= make_number (ntohs (sa1
.sin_port
));
3292 contact
= Fplist_put (contact
, QCservice
, service
);
3297 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3298 report_file_error ("Cannot listen on server socket", Qnil
);
3306 /* This turns off all alarm-based interrupts; the
3307 bind_polling_period call above doesn't always turn all the
3308 short-interval ones off, especially if interrupt_input is
3311 It'd be nice to be able to control the connect timeout
3312 though. Would non-blocking connect calls be portable?
3314 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3316 turn_on_atimers (0);
3318 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3321 turn_on_atimers (1);
3323 if (ret
== 0 || xerrno
== EISCONN
)
3325 /* The unwind-protect will be discarded afterwards.
3326 Likewise for immediate_quit. */
3330 #ifdef NON_BLOCKING_CONNECT
3332 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3336 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3344 /* Discard the unwind protect closing S. */
3345 specpdl_ptr
= specpdl
+ count1
;
3349 if (xerrno
== EINTR
)
3355 #ifdef DATAGRAM_SOCKETS
3356 if (socktype
== SOCK_DGRAM
)
3358 if (datagram_address
[s
].sa
)
3360 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3361 datagram_address
[s
].len
= lres
->ai_addrlen
;
3365 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3366 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3369 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3370 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3371 conv_lisp_to_sockaddr (rfamily
, remote
,
3372 datagram_address
[s
].sa
, rlen
);
3376 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3379 contact
= Fplist_put (contact
, QCaddress
,
3380 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3381 #ifdef HAVE_GETSOCKNAME
3384 struct sockaddr_in sa1
;
3385 int len1
= sizeof (sa1
);
3386 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3387 contact
= Fplist_put (contact
, QClocal
,
3388 conv_sockaddr_to_lisp (&sa1
, len1
));
3395 #ifdef HAVE_GETADDRINFO
3404 /* Discard the unwind protect for closing S, if any. */
3405 specpdl_ptr
= specpdl
+ count1
;
3407 /* Unwind bind_polling_period and request_sigio. */
3408 unbind_to (count
, Qnil
);
3412 /* If non-blocking got this far - and failed - assume non-blocking is
3413 not supported after all. This is probably a wrong assumption, but
3414 the normal blocking calls to open-network-stream handles this error
3416 if (is_non_blocking_client
)
3421 report_file_error ("make server process failed", contact
);
3423 report_file_error ("make client process failed", contact
);
3426 #endif /* not TERM */
3432 buffer
= Fget_buffer_create (buffer
);
3433 proc
= make_process (name
);
3435 chan_process
[inch
] = proc
;
3438 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3441 fcntl (inch
, F_SETFL
, O_NDELAY
);
3445 p
= XPROCESS (proc
);
3447 p
->childp
= contact
;
3448 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3451 p
->sentinel
= sentinel
;
3453 p
->filter_multibyte
= buffer_defaults
.enable_multibyte_characters
;
3454 /* Override the above only if :filter-multibyte is specified. */
3455 if (! NILP (Fplist_member (contact
, QCfilter_multibyte
)))
3456 p
->filter_multibyte
= Fplist_get (contact
, QCfilter_multibyte
);
3457 p
->log
= Fplist_get (contact
, QClog
);
3458 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3459 p
->kill_without_query
= Qt
;
3460 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3463 XSETINT (p
->infd
, inch
);
3464 XSETINT (p
->outfd
, outch
);
3465 if (is_server
&& socktype
== SOCK_STREAM
)
3466 p
->status
= Qlisten
;
3468 /* Make the process marker point into the process buffer (if any). */
3469 if (BUFFERP (buffer
))
3470 set_marker_both (p
->mark
, buffer
,
3471 BUF_ZV (XBUFFER (buffer
)),
3472 BUF_ZV_BYTE (XBUFFER (buffer
)));
3474 #ifdef NON_BLOCKING_CONNECT
3475 if (is_non_blocking_client
)
3477 /* We may get here if connect did succeed immediately. However,
3478 in that case, we still need to signal this like a non-blocking
3480 p
->status
= Qconnect
;
3481 if (!FD_ISSET (inch
, &connect_wait_mask
))
3483 FD_SET (inch
, &connect_wait_mask
);
3484 num_pending_connects
++;
3489 /* A server may have a client filter setting of Qt, but it must
3490 still listen for incoming connects unless it is stopped. */
3491 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3492 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3494 FD_SET (inch
, &input_wait_mask
);
3495 FD_SET (inch
, &non_keyboard_wait_mask
);
3498 if (inch
> max_process_desc
)
3499 max_process_desc
= inch
;
3501 tem
= Fplist_member (contact
, QCcoding
);
3502 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3503 tem
= Qnil
; /* No error message (too late!). */
3506 /* Setup coding systems for communicating with the network stream. */
3507 struct gcpro gcpro1
;
3508 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3509 Lisp_Object coding_systems
= Qt
;
3510 Lisp_Object args
[5], val
;
3514 val
= XCAR (XCDR (tem
));
3518 else if (!NILP (Vcoding_system_for_read
))
3519 val
= Vcoding_system_for_read
;
3520 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3521 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3522 /* We dare not decode end-of-line format by setting VAL to
3523 Qraw_text, because the existing Emacs Lisp libraries
3524 assume that they receive bare code including a sequene of
3529 if (NILP (host
) || NILP (service
))
3530 coding_systems
= Qnil
;
3533 args
[0] = Qopen_network_stream
, args
[1] = name
,
3534 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3536 coding_systems
= Ffind_operation_coding_system (5, args
);
3539 if (CONSP (coding_systems
))
3540 val
= XCAR (coding_systems
);
3541 else if (CONSP (Vdefault_process_coding_system
))
3542 val
= XCAR (Vdefault_process_coding_system
);
3546 p
->decode_coding_system
= val
;
3550 val
= XCAR (XCDR (tem
));
3554 else if (!NILP (Vcoding_system_for_write
))
3555 val
= Vcoding_system_for_write
;
3556 else if (NILP (current_buffer
->enable_multibyte_characters
))
3560 if (EQ (coding_systems
, Qt
))
3562 if (NILP (host
) || NILP (service
))
3563 coding_systems
= Qnil
;
3566 args
[0] = Qopen_network_stream
, args
[1] = name
,
3567 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3569 coding_systems
= Ffind_operation_coding_system (5, args
);
3573 if (CONSP (coding_systems
))
3574 val
= XCDR (coding_systems
);
3575 else if (CONSP (Vdefault_process_coding_system
))
3576 val
= XCDR (Vdefault_process_coding_system
);
3580 p
->encode_coding_system
= val
;
3582 setup_process_coding_systems (proc
);
3584 p
->decoding_buf
= make_uninit_string (0);
3585 p
->decoding_carryover
= make_number (0);
3586 p
->encoding_buf
= make_uninit_string (0);
3587 p
->encoding_carryover
= make_number (0);
3589 p
->inherit_coding_system_flag
3590 = (!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
3596 #endif /* HAVE_SOCKETS */
3599 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3602 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3603 doc
: /* Return an alist of all network interfaces and their network address.
3604 Each element is a cons, the car of which is a string containing the
3605 interface name, and the cdr is the network address in internal
3606 format; see the description of ADDRESS in `make-network-process'. */)
3609 struct ifconf ifconf
;
3610 struct ifreq
*ifreqs
= NULL
;
3615 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3621 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3622 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3629 ifconf
.ifc_len
= buf_size
;
3630 ifconf
.ifc_req
= ifreqs
;
3631 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3637 if (ifconf
.ifc_len
== buf_size
)
3641 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3644 while (--ifaces
>= 0)
3646 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3647 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3648 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3650 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3651 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3652 res
= Fcons (Fcons (build_string (namebuf
),
3653 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3654 sizeof (struct sockaddr
))),
3660 #endif /* SIOCGIFCONF */
3662 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3669 static struct ifflag_def ifflag_table
[] = {
3673 #ifdef IFF_BROADCAST
3674 { IFF_BROADCAST
, "broadcast" },
3677 { IFF_DEBUG
, "debug" },
3680 { IFF_LOOPBACK
, "loopback" },
3682 #ifdef IFF_POINTOPOINT
3683 { IFF_POINTOPOINT
, "pointopoint" },
3686 { IFF_RUNNING
, "running" },
3689 { IFF_NOARP
, "noarp" },
3692 { IFF_PROMISC
, "promisc" },
3694 #ifdef IFF_NOTRAILERS
3695 { IFF_NOTRAILERS
, "notrailers" },
3698 { IFF_ALLMULTI
, "allmulti" },
3701 { IFF_MASTER
, "master" },
3704 { IFF_SLAVE
, "slave" },
3706 #ifdef IFF_MULTICAST
3707 { IFF_MULTICAST
, "multicast" },
3710 { IFF_PORTSEL
, "portsel" },
3712 #ifdef IFF_AUTOMEDIA
3713 { IFF_AUTOMEDIA
, "automedia" },
3716 { IFF_DYNAMIC
, "dynamic" },
3719 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3722 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3725 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3728 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3731 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3736 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3737 doc
: /* Return information about network interface named IFNAME.
3738 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3739 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3740 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3741 FLAGS is the current flags of the interface. */)
3746 Lisp_Object res
= Qnil
;
3751 CHECK_STRING (ifname
);
3753 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
3754 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3756 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3761 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3762 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3764 int flags
= rq
.ifr_flags
;
3765 struct ifflag_def
*fp
;
3769 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3771 if (flags
& fp
->flag_bit
)
3773 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3774 flags
-= fp
->flag_bit
;
3777 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3779 if (flags
& (1 << fnum
))
3781 elt
= Fcons (make_number (fnum
), elt
);
3786 res
= Fcons (elt
, res
);
3789 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3790 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3792 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3793 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3797 for (n
= 0; n
< 6; n
++)
3798 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3799 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3802 res
= Fcons (elt
, res
);
3805 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
3806 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
3809 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
3810 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
3812 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3816 res
= Fcons (elt
, res
);
3819 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3820 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
3823 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
3826 res
= Fcons (elt
, res
);
3829 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
3830 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
3833 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3836 res
= Fcons (elt
, res
);
3840 return any
? res
: Qnil
;
3843 #endif /* HAVE_SOCKETS */
3845 /* Turn off input and output for process PROC. */
3848 deactivate_process (proc
)
3851 register int inchannel
, outchannel
;
3852 register struct Lisp_Process
*p
= XPROCESS (proc
);
3854 inchannel
= XINT (p
->infd
);
3855 outchannel
= XINT (p
->outfd
);
3857 #ifdef ADAPTIVE_READ_BUFFERING
3858 if (XINT (p
->read_output_delay
) > 0)
3860 if (--process_output_delay_count
< 0)
3861 process_output_delay_count
= 0;
3862 XSETINT (p
->read_output_delay
, 0);
3863 p
->read_output_skip
= Qnil
;
3869 /* Beware SIGCHLD hereabouts. */
3870 flush_pending_output (inchannel
);
3873 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
3874 sys$
dassgn (outchannel
);
3875 vs
= get_vms_process_pointer (p
->pid
);
3877 give_back_vms_process_stuff (vs
);
3880 emacs_close (inchannel
);
3881 if (outchannel
>= 0 && outchannel
!= inchannel
)
3882 emacs_close (outchannel
);
3885 XSETINT (p
->infd
, -1);
3886 XSETINT (p
->outfd
, -1);
3887 #ifdef DATAGRAM_SOCKETS
3888 if (DATAGRAM_CHAN_P (inchannel
))
3890 xfree (datagram_address
[inchannel
].sa
);
3891 datagram_address
[inchannel
].sa
= 0;
3892 datagram_address
[inchannel
].len
= 0;
3895 chan_process
[inchannel
] = Qnil
;
3896 FD_CLR (inchannel
, &input_wait_mask
);
3897 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
3898 #ifdef NON_BLOCKING_CONNECT
3899 if (FD_ISSET (inchannel
, &connect_wait_mask
))
3901 FD_CLR (inchannel
, &connect_wait_mask
);
3902 if (--num_pending_connects
< 0)
3906 if (inchannel
== max_process_desc
)
3909 /* We just closed the highest-numbered process input descriptor,
3910 so recompute the highest-numbered one now. */
3911 max_process_desc
= 0;
3912 for (i
= 0; i
< MAXDESC
; i
++)
3913 if (!NILP (chan_process
[i
]))
3914 max_process_desc
= i
;
3919 /* Close all descriptors currently in use for communication
3920 with subprocess. This is used in a newly-forked subprocess
3921 to get rid of irrelevant descriptors. */
3924 close_process_descs ()
3928 for (i
= 0; i
< MAXDESC
; i
++)
3930 Lisp_Object process
;
3931 process
= chan_process
[i
];
3932 if (!NILP (process
))
3934 int in
= XINT (XPROCESS (process
)->infd
);
3935 int out
= XINT (XPROCESS (process
)->outfd
);
3938 if (out
>= 0 && in
!= out
)
3945 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
3947 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
3948 It is read into the process' buffers or given to their filter functions.
3949 Non-nil arg PROCESS means do not return until some output has been received
3952 Non-nil second arg SECONDS and third arg MILLISEC are number of
3953 seconds and milliseconds to wait; return after that much time whether
3954 or not there is input. If SECONDS is a floating point number,
3955 it specifies a fractional number of seconds to wait.
3957 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
3958 from PROCESS, suspending reading output from other processes.
3959 If JUST-THIS-ONE is an integer, don't run any timers either.
3960 Return non-nil if we received any output before the timeout expired. */)
3961 (process
, seconds
, millisec
, just_this_one
)
3962 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
3964 int secs
, usecs
= 0;
3966 if (! NILP (process
))
3967 CHECK_PROCESS (process
);
3969 just_this_one
= Qnil
;
3971 if (!NILP (seconds
))
3973 if (INTEGERP (seconds
))
3974 secs
= XINT (seconds
);
3975 else if (FLOATP (seconds
))
3977 double timeout
= XFLOAT_DATA (seconds
);
3978 secs
= (int) timeout
;
3979 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
3982 wrong_type_argument (Qnumberp
, seconds
);
3984 if (INTEGERP (millisec
))
3987 usecs
+= XINT (millisec
) * 1000;
3988 carry
= usecs
/ 1000000;
3990 if ((usecs
-= carry
* 1000000) < 0)
3997 if (secs
< 0 || (secs
== 0 && usecs
== 0))
3998 secs
= -1, usecs
= 0;
4001 secs
= NILP (process
) ? -1 : 0;
4004 (wait_reading_process_output (secs
, usecs
, 0, 0,
4006 !NILP (process
) ? XPROCESS (process
) : NULL
,
4007 NILP (just_this_one
) ? 0 :
4008 !INTEGERP (just_this_one
) ? 1 : -1)
4012 /* Accept a connection for server process SERVER on CHANNEL. */
4014 static int connect_counter
= 0;
4017 server_accept_connection (server
, channel
)
4021 Lisp_Object proc
, caller
, name
, buffer
;
4022 Lisp_Object contact
, host
, service
;
4023 struct Lisp_Process
*ps
= XPROCESS (server
);
4024 struct Lisp_Process
*p
;
4028 struct sockaddr_in in
;
4030 struct sockaddr_in6 in6
;
4032 #ifdef HAVE_LOCAL_SOCKETS
4033 struct sockaddr_un un
;
4036 int len
= sizeof saddr
;
4038 s
= accept (channel
, &saddr
.sa
, &len
);
4047 if (code
== EWOULDBLOCK
)
4051 if (!NILP (ps
->log
))
4052 call3 (ps
->log
, server
, Qnil
,
4053 concat3 (build_string ("accept failed with code"),
4054 Fnumber_to_string (make_number (code
)),
4055 build_string ("\n")));
4061 /* Setup a new process to handle the connection. */
4063 /* Generate a unique identification of the caller, and build contact
4064 information for this process. */
4067 switch (saddr
.sa
.sa_family
)
4071 Lisp_Object args
[5];
4072 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4073 args
[0] = build_string ("%d.%d.%d.%d");
4074 args
[1] = make_number (*ip
++);
4075 args
[2] = make_number (*ip
++);
4076 args
[3] = make_number (*ip
++);
4077 args
[4] = make_number (*ip
++);
4078 host
= Fformat (5, args
);
4079 service
= make_number (ntohs (saddr
.in
.sin_port
));
4081 args
[0] = build_string (" <%s:%d>");
4084 caller
= Fformat (3, args
);
4091 Lisp_Object args
[9];
4092 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4094 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4095 for (i
= 0; i
< 8; i
++)
4096 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4097 host
= Fformat (9, args
);
4098 service
= make_number (ntohs (saddr
.in
.sin_port
));
4100 args
[0] = build_string (" <[%s]:%d>");
4103 caller
= Fformat (3, args
);
4108 #ifdef HAVE_LOCAL_SOCKETS
4112 caller
= Fnumber_to_string (make_number (connect_counter
));
4113 caller
= concat3 (build_string (" <*"), caller
, build_string ("*>"));
4117 /* Create a new buffer name for this process if it doesn't have a
4118 filter. The new buffer name is based on the buffer name or
4119 process name of the server process concatenated with the caller
4122 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4126 buffer
= ps
->buffer
;
4128 buffer
= Fbuffer_name (buffer
);
4133 buffer
= concat2 (buffer
, caller
);
4134 buffer
= Fget_buffer_create (buffer
);
4138 /* Generate a unique name for the new server process. Combine the
4139 server process name with the caller identification. */
4141 name
= concat2 (ps
->name
, caller
);
4142 proc
= make_process (name
);
4144 chan_process
[s
] = proc
;
4147 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4150 fcntl (s
, F_SETFL
, O_NDELAY
);
4154 p
= XPROCESS (proc
);
4156 /* Build new contact information for this setup. */
4157 contact
= Fcopy_sequence (ps
->childp
);
4158 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4159 contact
= Fplist_put (contact
, QChost
, host
);
4160 if (!NILP (service
))
4161 contact
= Fplist_put (contact
, QCservice
, service
);
4162 contact
= Fplist_put (contact
, QCremote
,
4163 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4164 #ifdef HAVE_GETSOCKNAME
4166 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4167 contact
= Fplist_put (contact
, QClocal
,
4168 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4171 p
->childp
= contact
;
4172 p
->plist
= Fcopy_sequence (ps
->plist
);
4175 p
->sentinel
= ps
->sentinel
;
4176 p
->filter
= ps
->filter
;
4179 XSETINT (p
->infd
, s
);
4180 XSETINT (p
->outfd
, s
);
4183 /* Client processes for accepted connections are not stopped initially. */
4184 if (!EQ (p
->filter
, Qt
))
4186 FD_SET (s
, &input_wait_mask
);
4187 FD_SET (s
, &non_keyboard_wait_mask
);
4190 if (s
> max_process_desc
)
4191 max_process_desc
= s
;
4193 /* Setup coding system for new process based on server process.
4194 This seems to be the proper thing to do, as the coding system
4195 of the new process should reflect the settings at the time the
4196 server socket was opened; not the current settings. */
4198 p
->decode_coding_system
= ps
->decode_coding_system
;
4199 p
->encode_coding_system
= ps
->encode_coding_system
;
4200 setup_process_coding_systems (proc
);
4202 p
->decoding_buf
= make_uninit_string (0);
4203 p
->decoding_carryover
= make_number (0);
4204 p
->encoding_buf
= make_uninit_string (0);
4205 p
->encoding_carryover
= make_number (0);
4207 p
->inherit_coding_system_flag
4208 = (NILP (buffer
) ? Qnil
: ps
->inherit_coding_system_flag
);
4210 if (!NILP (ps
->log
))
4211 call3 (ps
->log
, server
, proc
,
4212 concat3 (build_string ("accept from "),
4213 (STRINGP (host
) ? host
: build_string ("-")),
4214 build_string ("\n")));
4216 if (!NILP (p
->sentinel
))
4217 exec_sentinel (proc
,
4218 concat3 (build_string ("open from "),
4219 (STRINGP (host
) ? host
: build_string ("-")),
4220 build_string ("\n")));
4223 /* This variable is different from waiting_for_input in keyboard.c.
4224 It is used to communicate to a lisp process-filter/sentinel (via the
4225 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4226 for user-input when that process-filter was called.
4227 waiting_for_input cannot be used as that is by definition 0 when
4228 lisp code is being evalled.
4229 This is also used in record_asynch_buffer_change.
4230 For that purpose, this must be 0
4231 when not inside wait_reading_process_output. */
4232 static int waiting_for_user_input_p
;
4235 wait_reading_process_output_unwind (data
)
4238 waiting_for_user_input_p
= XINT (data
);
4242 /* This is here so breakpoints can be put on it. */
4244 wait_reading_process_output_1 ()
4248 /* Use a wrapper around select to work around a bug in gdb 5.3.
4249 Normally, the wrapper is optimzed away by inlining.
4251 If emacs is stopped inside select, the gdb backtrace doesn't
4252 show the function which called select, so it is practically
4253 impossible to step through wait_reading_process_output. */
4257 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4259 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4262 return select (n
, rfd
, wfd
, xfd
, tmo
);
4264 #define select select_wrapper
4267 /* Read and dispose of subprocess output while waiting for timeout to
4268 elapse and/or keyboard input to be available.
4271 timeout in seconds, or
4272 zero for no limit, or
4273 -1 means gobble data immediately available but don't wait for any.
4276 an additional duration to wait, measured in microseconds.
4277 If this is nonzero and time_limit is 0, then the timeout
4278 consists of MICROSECS only.
4280 READ_KBD is a lisp value:
4281 0 to ignore keyboard input, or
4282 1 to return when input is available, or
4283 -1 meaning caller will actually read the input, so don't throw to
4284 the quit handler, or
4286 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4287 output that arrives.
4289 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4290 (and gobble terminal input into the buffer if any arrives).
4292 If WAIT_PROC is specified, wait until something arrives from that
4293 process. The return value is true if we read some input from
4296 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4297 (suspending output from other processes). A negative value
4298 means don't run any timers either.
4300 If WAIT_PROC is specified, then the function returns true if we
4301 received input from that process before the timeout elapsed.
4302 Otherwise, return true if we received input from any process. */
4305 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4306 wait_for_cell
, wait_proc
, just_wait_proc
)
4307 int time_limit
, microsecs
, read_kbd
, do_display
;
4308 Lisp_Object wait_for_cell
;
4309 struct Lisp_Process
*wait_proc
;
4312 register int channel
, nfds
;
4313 SELECT_TYPE Available
;
4314 #ifdef NON_BLOCKING_CONNECT
4315 SELECT_TYPE Connecting
;
4318 int check_delay
, no_avail
;
4321 EMACS_TIME timeout
, end_time
;
4322 int wait_channel
= -1;
4323 int got_some_input
= 0;
4324 int count
= SPECPDL_INDEX ();
4326 FD_ZERO (&Available
);
4327 #ifdef NON_BLOCKING_CONNECT
4328 FD_ZERO (&Connecting
);
4331 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4332 if (wait_proc
!= NULL
)
4333 wait_channel
= XINT (wait_proc
->infd
);
4335 record_unwind_protect (wait_reading_process_output_unwind
,
4336 make_number (waiting_for_user_input_p
));
4337 waiting_for_user_input_p
= read_kbd
;
4339 /* Since we may need to wait several times,
4340 compute the absolute time to return at. */
4341 if (time_limit
|| microsecs
)
4343 EMACS_GET_TIME (end_time
);
4344 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4345 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4347 #ifdef POLL_INTERRUPTED_SYS_CALL
4348 /* AlainF 5-Jul-1996
4349 HP-UX 10.10 seem to have problems with signals coming in
4350 Causes "poll: interrupted system call" messages when Emacs is run
4352 Turn off periodic alarms (in case they are in use),
4353 and then turn off any other atimers. */
4355 turn_on_atimers (0);
4356 #endif /* POLL_INTERRUPTED_SYS_CALL */
4360 int timeout_reduced_for_timers
= 0;
4362 /* If calling from keyboard input, do not quit
4363 since we want to return C-g as an input character.
4364 Otherwise, do pending quit if requested. */
4368 else if (interrupt_input_pending
)
4369 handle_async_input ();
4372 /* Exit now if the cell we're waiting for became non-nil. */
4373 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4376 /* Compute time from now till when time limit is up */
4377 /* Exit if already run out */
4378 if (time_limit
== -1)
4380 /* -1 specified for timeout means
4381 gobble output available now
4382 but don't wait at all. */
4384 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4386 else if (time_limit
|| microsecs
)
4388 EMACS_GET_TIME (timeout
);
4389 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4390 if (EMACS_TIME_NEG_P (timeout
))
4395 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4398 /* Normally we run timers here.
4399 But not if wait_for_cell; in those cases,
4400 the wait is supposed to be short,
4401 and those callers cannot handle running arbitrary Lisp code here. */
4402 if (NILP (wait_for_cell
)
4403 && just_wait_proc
>= 0)
4405 EMACS_TIME timer_delay
;
4409 int old_timers_run
= timers_run
;
4410 struct buffer
*old_buffer
= current_buffer
;
4411 Lisp_Object old_window
= selected_window
;
4413 timer_delay
= timer_check (1);
4415 /* If a timer has run, this might have changed buffers
4416 an alike. Make read_key_sequence aware of that. */
4417 if (timers_run
!= old_timers_run
4418 && (old_buffer
!= current_buffer
4419 || !EQ (old_window
, selected_window
))
4420 && waiting_for_user_input_p
== -1)
4421 record_asynch_buffer_change ();
4423 if (timers_run
!= old_timers_run
&& do_display
)
4424 /* We must retry, since a timer may have requeued itself
4425 and that could alter the time_delay. */
4426 redisplay_preserve_echo_area (9);
4430 while (!detect_input_pending ());
4432 /* If there is unread keyboard input, also return. */
4434 && requeued_events_pending_p ())
4437 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4439 EMACS_TIME difference
;
4440 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4441 if (EMACS_TIME_NEG_P (difference
))
4443 timeout
= timer_delay
;
4444 timeout_reduced_for_timers
= 1;
4447 /* If time_limit is -1, we are not going to wait at all. */
4448 else if (time_limit
!= -1)
4450 /* This is so a breakpoint can be put here. */
4451 wait_reading_process_output_1 ();
4455 /* Cause C-g and alarm signals to take immediate action,
4456 and cause input available signals to zero out timeout.
4458 It is important that we do this before checking for process
4459 activity. If we get a SIGCHLD after the explicit checks for
4460 process activity, timeout is the only way we will know. */
4462 set_waiting_for_input (&timeout
);
4464 /* If status of something has changed, and no input is
4465 available, notify the user of the change right away. After
4466 this explicit check, we'll let the SIGCHLD handler zap
4467 timeout to get our attention. */
4468 if (update_tick
!= process_tick
&& do_display
)
4471 #ifdef NON_BLOCKING_CONNECT
4475 Atemp
= input_wait_mask
;
4476 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4478 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4479 if ((select (max (max_process_desc
, max_keyboard_desc
) + 1,
4481 #ifdef NON_BLOCKING_CONNECT
4482 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4486 (SELECT_TYPE
*)0, &timeout
)
4489 /* It's okay for us to do this and then continue with
4490 the loop, since timeout has already been zeroed out. */
4491 clear_waiting_for_input ();
4492 status_notify (NULL
);
4496 /* Don't wait for output from a non-running process. Just
4497 read whatever data has already been received. */
4498 if (wait_proc
&& wait_proc
->raw_status_new
)
4499 update_status (wait_proc
);
4501 && ! EQ (wait_proc
->status
, Qrun
)
4502 && ! EQ (wait_proc
->status
, Qconnect
))
4504 int nread
, total_nread
= 0;
4506 clear_waiting_for_input ();
4507 XSETPROCESS (proc
, wait_proc
);
4509 /* Read data from the process, until we exhaust it. */
4510 while (XINT (wait_proc
->infd
) >= 0)
4512 nread
= read_process_output (proc
, XINT (wait_proc
->infd
));
4518 total_nread
+= nread
;
4520 else if (nread
== -1 && EIO
== errno
)
4524 else if (nread
== -1 && EAGAIN
== errno
)
4528 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4532 if (total_nread
> 0 && do_display
)
4533 redisplay_preserve_echo_area (10);
4538 /* Wait till there is something to do */
4540 if (wait_proc
&& just_wait_proc
)
4542 if (XINT (wait_proc
->infd
) < 0) /* Terminated */
4544 FD_SET (XINT (wait_proc
->infd
), &Available
);
4546 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4548 else if (!NILP (wait_for_cell
))
4550 Available
= non_process_wait_mask
;
4552 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4557 Available
= non_keyboard_wait_mask
;
4559 Available
= input_wait_mask
;
4560 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4561 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4564 /* If frame size has changed or the window is newly mapped,
4565 redisplay now, before we start to wait. There is a race
4566 condition here; if a SIGIO arrives between now and the select
4567 and indicates that a frame is trashed, the select may block
4568 displaying a trashed screen. */
4569 if (frame_garbaged
&& do_display
)
4571 clear_waiting_for_input ();
4572 redisplay_preserve_echo_area (11);
4574 set_waiting_for_input (&timeout
);
4578 if (read_kbd
&& detect_input_pending ())
4585 #ifdef NON_BLOCKING_CONNECT
4587 Connecting
= connect_wait_mask
;
4590 #ifdef ADAPTIVE_READ_BUFFERING
4591 /* Set the timeout for adaptive read buffering if any
4592 process has non-nil read_output_skip and non-zero
4593 read_output_delay, and we are not reading output for a
4594 specific wait_channel. It is not executed if
4595 Vprocess_adaptive_read_buffering is nil. */
4596 if (process_output_skip
&& check_delay
> 0)
4598 int usecs
= EMACS_USECS (timeout
);
4599 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4600 usecs
= READ_OUTPUT_DELAY_MAX
;
4601 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4603 proc
= chan_process
[channel
];
4606 /* Find minimum non-zero read_output_delay among the
4607 processes with non-nil read_output_skip. */
4608 if (XINT (XPROCESS (proc
)->read_output_delay
) > 0)
4611 if (NILP (XPROCESS (proc
)->read_output_skip
))
4613 FD_CLR (channel
, &Available
);
4614 XPROCESS (proc
)->read_output_skip
= Qnil
;
4615 if (XINT (XPROCESS (proc
)->read_output_delay
) < usecs
)
4616 usecs
= XINT (XPROCESS (proc
)->read_output_delay
);
4619 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4620 process_output_skip
= 0;
4624 nfds
= select (max (max_process_desc
, max_keyboard_desc
) + 1,
4626 #ifdef NON_BLOCKING_CONNECT
4627 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4631 (SELECT_TYPE
*)0, &timeout
);
4636 /* Make C-g and alarm signals set flags again */
4637 clear_waiting_for_input ();
4639 /* If we woke up due to SIGWINCH, actually change size now. */
4640 do_pending_window_change (0);
4642 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4643 /* We wanted the full specified time, so return now. */
4647 if (xerrno
== EINTR
)
4650 /* Ultrix select seems to return ENOMEM when it is
4651 interrupted. Treat it just like EINTR. Bleah. Note
4652 that we want to test for the "ultrix" CPP symbol, not
4653 "__ultrix__"; the latter is only defined under GCC, but
4654 not by DEC's bundled CC. -JimB */
4655 else if (xerrno
== ENOMEM
)
4659 /* This happens for no known reason on ALLIANT.
4660 I am guessing that this is the right response. -- RMS. */
4661 else if (xerrno
== EFAULT
)
4664 else if (xerrno
== EBADF
)
4667 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4668 the child's closure of the pts gives the parent a SIGHUP, and
4669 the ptc file descriptor is automatically closed,
4670 yielding EBADF here or at select() call above.
4671 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4672 in m/ibmrt-aix.h), and here we just ignore the select error.
4673 Cleanup occurs c/o status_notify after SIGCLD. */
4674 no_avail
= 1; /* Cannot depend on values returned */
4680 error ("select error: %s", emacs_strerror (xerrno
));
4685 FD_ZERO (&Available
);
4686 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4689 #if defined(sun) && !defined(USG5_4)
4690 if (nfds
> 0 && keyboard_bit_set (&Available
)
4692 /* System sometimes fails to deliver SIGIO.
4694 David J. Mackenzie says that Emacs doesn't compile under
4695 Solaris if this code is enabled, thus the USG5_4 in the CPP
4696 conditional. "I haven't noticed any ill effects so far.
4697 If you find a Solaris expert somewhere, they might know
4699 kill (getpid (), SIGIO
);
4702 #if 0 /* When polling is used, interrupt_input is 0,
4703 so get_input_pending should read the input.
4704 So this should not be needed. */
4705 /* If we are using polling for input,
4706 and we see input available, make it get read now.
4707 Otherwise it might not actually get read for a second.
4708 And on hpux, since we turn off polling in wait_reading_process_output,
4709 it might never get read at all if we don't spend much time
4710 outside of wait_reading_process_output. */
4711 if (read_kbd
&& interrupt_input
4712 && keyboard_bit_set (&Available
)
4713 && input_polling_used ())
4714 kill (getpid (), SIGALRM
);
4717 /* Check for keyboard input */
4718 /* If there is any, return immediately
4719 to give it higher priority than subprocesses */
4723 int old_timers_run
= timers_run
;
4724 struct buffer
*old_buffer
= current_buffer
;
4725 Lisp_Object old_window
= selected_window
;
4728 if (detect_input_pending_run_timers (do_display
))
4730 swallow_events (do_display
);
4731 if (detect_input_pending_run_timers (do_display
))
4735 /* If a timer has run, this might have changed buffers
4736 an alike. Make read_key_sequence aware of that. */
4737 if (timers_run
!= old_timers_run
4738 && waiting_for_user_input_p
== -1
4739 && (old_buffer
!= current_buffer
4740 || !EQ (old_window
, selected_window
)))
4741 record_asynch_buffer_change ();
4747 /* If there is unread keyboard input, also return. */
4749 && requeued_events_pending_p ())
4752 /* If we are not checking for keyboard input now,
4753 do process events (but don't run any timers).
4754 This is so that X events will be processed.
4755 Otherwise they may have to wait until polling takes place.
4756 That would causes delays in pasting selections, for example.
4758 (We used to do this only if wait_for_cell.) */
4759 if (read_kbd
== 0 && detect_input_pending ())
4761 swallow_events (do_display
);
4762 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4763 if (detect_input_pending ())
4768 /* Exit now if the cell we're waiting for became non-nil. */
4769 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4773 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4774 go read it. This can happen with X on BSD after logging out.
4775 In that case, there really is no input and no SIGIO,
4776 but select says there is input. */
4778 if (read_kbd
&& interrupt_input
4779 && keyboard_bit_set (&Available
) && ! noninteractive
)
4780 kill (getpid (), SIGIO
);
4784 got_some_input
|= nfds
> 0;
4786 /* If checking input just got us a size-change event from X,
4787 obey it now if we should. */
4788 if (read_kbd
|| ! NILP (wait_for_cell
))
4789 do_pending_window_change (0);
4791 /* Check for data from a process. */
4792 if (no_avail
|| nfds
== 0)
4795 /* Really FIRST_PROC_DESC should be 0 on Unix,
4796 but this is safer in the short run. */
4797 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4799 if (FD_ISSET (channel
, &Available
)
4800 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
4804 /* If waiting for this channel, arrange to return as
4805 soon as no more input to be processed. No more
4807 if (wait_channel
== channel
)
4813 proc
= chan_process
[channel
];
4817 /* If this is a server stream socket, accept connection. */
4818 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4820 server_accept_connection (proc
, channel
);
4824 /* Read data from the process, starting with our
4825 buffered-ahead character if we have one. */
4827 nread
= read_process_output (proc
, channel
);
4830 /* Since read_process_output can run a filter,
4831 which can call accept-process-output,
4832 don't try to read from any other processes
4833 before doing the select again. */
4834 FD_ZERO (&Available
);
4837 redisplay_preserve_echo_area (12);
4840 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4843 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4844 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4846 else if (nread
== -1 && errno
== EAGAIN
)
4850 else if (nread
== -1 && errno
== EAGAIN
)
4852 /* Note that we cannot distinguish between no input
4853 available now and a closed pipe.
4854 With luck, a closed pipe will be accompanied by
4855 subprocess termination and SIGCHLD. */
4856 else if (nread
== 0 && !NETCONN_P (proc
))
4858 #endif /* O_NDELAY */
4859 #endif /* O_NONBLOCK */
4861 /* On some OSs with ptys, when the process on one end of
4862 a pty exits, the other end gets an error reading with
4863 errno = EIO instead of getting an EOF (0 bytes read).
4864 Therefore, if we get an error reading and errno =
4865 EIO, just continue, because the child process has
4866 exited and should clean itself up soon (e.g. when we
4869 However, it has been known to happen that the SIGCHLD
4870 got lost. So raise the signal again just in case.
4872 else if (nread
== -1 && errno
== EIO
)
4874 /* Clear the descriptor now, so we only raise the signal once. */
4875 FD_CLR (channel
, &input_wait_mask
);
4876 FD_CLR (channel
, &non_keyboard_wait_mask
);
4878 kill (getpid (), SIGCHLD
);
4880 #endif /* HAVE_PTYS */
4881 /* If we can detect process termination, don't consider the process
4882 gone just because its pipe is closed. */
4884 else if (nread
== 0 && !NETCONN_P (proc
))
4889 /* Preserve status of processes already terminated. */
4890 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
4891 deactivate_process (proc
);
4892 if (XPROCESS (proc
)->raw_status_new
)
4893 update_status (XPROCESS (proc
));
4894 if (EQ (XPROCESS (proc
)->status
, Qrun
))
4895 XPROCESS (proc
)->status
4896 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
4899 #ifdef NON_BLOCKING_CONNECT
4900 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
4901 && FD_ISSET (channel
, &connect_wait_mask
))
4903 struct Lisp_Process
*p
;
4905 FD_CLR (channel
, &connect_wait_mask
);
4906 if (--num_pending_connects
< 0)
4909 proc
= chan_process
[channel
];
4913 p
= XPROCESS (proc
);
4916 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
4917 So only use it on systems where it is known to work. */
4919 int xlen
= sizeof(xerrno
);
4920 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
4925 struct sockaddr pname
;
4926 int pnamelen
= sizeof(pname
);
4928 /* If connection failed, getpeername will fail. */
4930 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
4932 /* Obtain connect failure code through error slippage. */
4935 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
4942 XSETINT (p
->tick
, ++process_tick
);
4943 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
4944 deactivate_process (proc
);
4949 /* Execute the sentinel here. If we had relied on
4950 status_notify to do it later, it will read input
4951 from the process before calling the sentinel. */
4952 exec_sentinel (proc
, build_string ("open\n"));
4953 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
4955 FD_SET (XINT (p
->infd
), &input_wait_mask
);
4956 FD_SET (XINT (p
->infd
), &non_keyboard_wait_mask
);
4960 #endif /* NON_BLOCKING_CONNECT */
4961 } /* end for each file descriptor */
4962 } /* end while exit conditions not met */
4964 unbind_to (count
, Qnil
);
4966 /* If calling from keyboard input, do not quit
4967 since we want to return C-g as an input character.
4968 Otherwise, do pending quit if requested. */
4971 /* Prevent input_pending from remaining set if we quit. */
4972 clear_input_pending ();
4975 #ifdef POLL_INTERRUPTED_SYS_CALL
4976 /* AlainF 5-Jul-1996
4977 HP-UX 10.10 seems to have problems with signals coming in
4978 Causes "poll: interrupted system call" messages when Emacs is run
4980 Turn periodic alarms back on */
4982 #endif /* POLL_INTERRUPTED_SYS_CALL */
4984 return got_some_input
;
4987 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
4990 read_process_output_call (fun_and_args
)
4991 Lisp_Object fun_and_args
;
4993 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
4997 read_process_output_error_handler (error
)
5000 cmd_error_internal (error
, "error in process filter: ");
5002 update_echo_area ();
5003 Fsleep_for (make_number (2), Qnil
);
5007 /* Read pending output from the process channel,
5008 starting with our buffered-ahead character if we have one.
5009 Yield number of decoded characters read.
5011 This function reads at most 4096 characters.
5012 If you want to read all available subprocess output,
5013 you must call it repeatedly until it returns zero.
5015 The characters read are decoded according to PROC's coding-system
5019 read_process_output (proc
, channel
)
5021 register int channel
;
5023 register int nbytes
;
5025 register Lisp_Object outstream
;
5026 register struct buffer
*old
= current_buffer
;
5027 register struct Lisp_Process
*p
= XPROCESS (proc
);
5028 register int opoint
;
5029 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5030 int carryover
= XINT (p
->decoding_carryover
);
5034 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5036 vs
= get_vms_process_pointer (p
->pid
);
5040 return (0); /* Really weird if it does this */
5041 if (!(vs
->iosb
[0] & 1))
5042 return -1; /* I/O error */
5045 error ("Could not get VMS process pointer");
5046 chars
= vs
->inputBuffer
;
5047 nbytes
= clean_vms_buffer (chars
, vs
->iosb
[1]);
5050 start_vms_process_read (vs
); /* Crank up the next read on the process */
5051 return 1; /* Nothing worth printing, say we got 1 */
5055 /* The data carried over in the previous decoding (which are at
5056 the tail of decoding buffer) should be prepended to the new
5057 data read to decode all together. */
5058 chars
= (char *) alloca (nbytes
+ carryover
);
5059 bcopy (SDATA (p
->decoding_buf
), buf
, carryover
);
5060 bcopy (vs
->inputBuffer
, chars
+ carryover
, nbytes
);
5064 chars
= (char *) alloca (carryover
+ readmax
);
5066 /* See the comment above. */
5067 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5069 #ifdef DATAGRAM_SOCKETS
5070 /* We have a working select, so proc_buffered_char is always -1. */
5071 if (DATAGRAM_CHAN_P (channel
))
5073 int len
= datagram_address
[channel
].len
;
5074 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5075 0, datagram_address
[channel
].sa
, &len
);
5079 if (proc_buffered_char
[channel
] < 0)
5081 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5082 #ifdef ADAPTIVE_READ_BUFFERING
5083 if (nbytes
> 0 && !NILP (p
->adaptive_read_buffering
))
5085 int delay
= XINT (p
->read_output_delay
);
5088 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5091 process_output_delay_count
++;
5092 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5095 else if (delay
> 0 && (nbytes
== readmax
))
5097 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5099 process_output_delay_count
--;
5101 XSETINT (p
->read_output_delay
, delay
);
5104 p
->read_output_skip
= Qt
;
5105 process_output_skip
= 1;
5112 chars
[carryover
] = proc_buffered_char
[channel
];
5113 proc_buffered_char
[channel
] = -1;
5114 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5118 nbytes
= nbytes
+ 1;
5120 #endif /* not VMS */
5122 XSETINT (p
->decoding_carryover
, 0);
5124 /* At this point, NBYTES holds number of bytes just received
5125 (including the one in proc_buffered_char[channel]). */
5128 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5130 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5133 /* Now set NBYTES how many bytes we must decode. */
5134 nbytes
+= carryover
;
5136 /* Read and dispose of the process output. */
5137 outstream
= p
->filter
;
5138 if (!NILP (outstream
))
5140 /* We inhibit quit here instead of just catching it so that
5141 hitting ^G when a filter happens to be running won't screw
5143 int count
= SPECPDL_INDEX ();
5144 Lisp_Object odeactivate
;
5145 Lisp_Object obuffer
, okeymap
;
5147 int outer_running_asynch_code
= running_asynch_code
;
5148 int waiting
= waiting_for_user_input_p
;
5150 /* No need to gcpro these, because all we do with them later
5151 is test them for EQness, and none of them should be a string. */
5152 odeactivate
= Vdeactivate_mark
;
5153 XSETBUFFER (obuffer
, current_buffer
);
5154 okeymap
= current_buffer
->keymap
;
5156 specbind (Qinhibit_quit
, Qt
);
5157 specbind (Qlast_nonmenu_event
, Qt
);
5159 /* In case we get recursively called,
5160 and we already saved the match data nonrecursively,
5161 save the same match data in safely recursive fashion. */
5162 if (outer_running_asynch_code
)
5165 /* Don't clobber the CURRENT match data, either! */
5166 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5167 restore_search_regs ();
5168 record_unwind_save_match_data ();
5169 Fset_match_data (tem
, Qt
);
5172 /* For speed, if a search happens within this code,
5173 save the match data in a special nonrecursive fashion. */
5174 running_asynch_code
= 1;
5176 text
= decode_coding_string (make_unibyte_string (chars
, nbytes
),
5178 Vlast_coding_system_used
= coding
->symbol
;
5179 /* A new coding system might be found. */
5180 if (!EQ (p
->decode_coding_system
, coding
->symbol
))
5182 p
->decode_coding_system
= coding
->symbol
;
5184 /* Don't call setup_coding_system for
5185 proc_decode_coding_system[channel] here. It is done in
5186 detect_coding called via decode_coding above. */
5188 /* If a coding system for encoding is not yet decided, we set
5189 it as the same as coding-system for decoding.
5191 But, before doing that we must check if
5192 proc_encode_coding_system[p->outfd] surely points to a
5193 valid memory because p->outfd will be changed once EOF is
5194 sent to the process. */
5195 if (NILP (p
->encode_coding_system
)
5196 && proc_encode_coding_system
[XINT (p
->outfd
)])
5198 p
->encode_coding_system
= coding
->symbol
;
5199 setup_coding_system (coding
->symbol
,
5200 proc_encode_coding_system
[XINT (p
->outfd
)]);
5201 if (proc_encode_coding_system
[XINT (p
->outfd
)]->eol_type
5202 == CODING_EOL_UNDECIDED
)
5203 proc_encode_coding_system
[XINT (p
->outfd
)]->eol_type
5208 carryover
= nbytes
- coding
->consumed
;
5212 if (SCHARS (p
->decoding_buf
) < carryover
)
5213 p
->decoding_buf
= make_uninit_string (carryover
);
5214 bcopy (chars
+ coding
->consumed
, SDATA (p
->decoding_buf
),
5216 XSETINT (p
->decoding_carryover
, carryover
);
5217 /* Adjust the multibyteness of TEXT to that of the filter. */
5218 if (NILP (p
->filter_multibyte
) != ! STRING_MULTIBYTE (text
))
5219 text
= (STRING_MULTIBYTE (text
)
5220 ? Fstring_as_unibyte (text
)
5221 : Fstring_to_multibyte (text
));
5222 if (SBYTES (text
) > 0)
5223 internal_condition_case_1 (read_process_output_call
,
5225 Fcons (proc
, Fcons (text
, Qnil
))),
5226 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5227 read_process_output_error_handler
);
5229 /* If we saved the match data nonrecursively, restore it now. */
5230 restore_search_regs ();
5231 running_asynch_code
= outer_running_asynch_code
;
5233 /* Handling the process output should not deactivate the mark. */
5234 Vdeactivate_mark
= odeactivate
;
5236 /* Restore waiting_for_user_input_p as it was
5237 when we were called, in case the filter clobbered it. */
5238 waiting_for_user_input_p
= waiting
;
5240 #if 0 /* Call record_asynch_buffer_change unconditionally,
5241 because we might have changed minor modes or other things
5242 that affect key bindings. */
5243 if (! EQ (Fcurrent_buffer (), obuffer
)
5244 || ! EQ (current_buffer
->keymap
, okeymap
))
5246 /* But do it only if the caller is actually going to read events.
5247 Otherwise there's no need to make him wake up, and it could
5248 cause trouble (for example it would make sit_for return). */
5249 if (waiting_for_user_input_p
== -1)
5250 record_asynch_buffer_change ();
5253 start_vms_process_read (vs
);
5255 unbind_to (count
, Qnil
);
5259 /* If no filter, write into buffer if it isn't dead. */
5260 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5262 Lisp_Object old_read_only
;
5263 int old_begv
, old_zv
;
5264 int old_begv_byte
, old_zv_byte
;
5265 Lisp_Object odeactivate
;
5266 int before
, before_byte
;
5271 odeactivate
= Vdeactivate_mark
;
5273 Fset_buffer (p
->buffer
);
5275 opoint_byte
= PT_BYTE
;
5276 old_read_only
= current_buffer
->read_only
;
5279 old_begv_byte
= BEGV_BYTE
;
5280 old_zv_byte
= ZV_BYTE
;
5282 current_buffer
->read_only
= Qnil
;
5284 /* Insert new output into buffer
5285 at the current end-of-output marker,
5286 thus preserving logical ordering of input and output. */
5287 if (XMARKER (p
->mark
)->buffer
)
5288 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5289 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5292 SET_PT_BOTH (ZV
, ZV_BYTE
);
5294 before_byte
= PT_BYTE
;
5296 /* If the output marker is outside of the visible region, save
5297 the restriction and widen. */
5298 if (! (BEGV
<= PT
&& PT
<= ZV
))
5301 text
= decode_coding_string (make_unibyte_string (chars
, nbytes
),
5303 Vlast_coding_system_used
= coding
->symbol
;
5304 /* A new coding system might be found. See the comment in the
5305 similar code in the previous `if' block. */
5306 if (!EQ (p
->decode_coding_system
, coding
->symbol
))
5308 p
->decode_coding_system
= coding
->symbol
;
5309 if (NILP (p
->encode_coding_system
)
5310 && proc_encode_coding_system
[XINT (p
->outfd
)])
5312 p
->encode_coding_system
= coding
->symbol
;
5313 setup_coding_system (coding
->symbol
,
5314 proc_encode_coding_system
[XINT (p
->outfd
)]);
5315 if (proc_encode_coding_system
[XINT (p
->outfd
)]->eol_type
5316 == CODING_EOL_UNDECIDED
)
5317 proc_encode_coding_system
[XINT (p
->outfd
)]->eol_type
5321 carryover
= nbytes
- coding
->consumed
;
5325 if (SCHARS (p
->decoding_buf
) < carryover
)
5326 p
->decoding_buf
= make_uninit_string (carryover
);
5327 bcopy (chars
+ coding
->consumed
, SDATA (p
->decoding_buf
),
5329 XSETINT (p
->decoding_carryover
, carryover
);
5331 /* Adjust the multibyteness of TEXT to that of the buffer. */
5332 if (NILP (current_buffer
->enable_multibyte_characters
)
5333 != ! STRING_MULTIBYTE (text
))
5334 text
= (STRING_MULTIBYTE (text
)
5335 ? Fstring_as_unibyte (text
)
5336 : Fstring_to_multibyte (text
));
5337 /* Insert before markers in case we are inserting where
5338 the buffer's mark is, and the user's next command is Meta-y. */
5339 insert_from_string_before_markers (text
, 0, 0,
5340 SCHARS (text
), SBYTES (text
), 0);
5342 /* Make sure the process marker's position is valid when the
5343 process buffer is changed in the signal_after_change above.
5344 W3 is known to do that. */
5345 if (BUFFERP (p
->buffer
)
5346 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5347 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5349 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5351 update_mode_lines
++;
5353 /* Make sure opoint and the old restrictions
5354 float ahead of any new text just as point would. */
5355 if (opoint
>= before
)
5357 opoint
+= PT
- before
;
5358 opoint_byte
+= PT_BYTE
- before_byte
;
5360 if (old_begv
> before
)
5362 old_begv
+= PT
- before
;
5363 old_begv_byte
+= PT_BYTE
- before_byte
;
5365 if (old_zv
>= before
)
5367 old_zv
+= PT
- before
;
5368 old_zv_byte
+= PT_BYTE
- before_byte
;
5371 /* If the restriction isn't what it should be, set it. */
5372 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5373 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5375 /* Handling the process output should not deactivate the mark. */
5376 Vdeactivate_mark
= odeactivate
;
5378 current_buffer
->read_only
= old_read_only
;
5379 SET_PT_BOTH (opoint
, opoint_byte
);
5380 set_buffer_internal (old
);
5383 start_vms_process_read (vs
);
5388 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5390 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5391 This is intended for use by asynchronous process output filters and sentinels. */)
5394 return (waiting_for_user_input_p
? Qt
: Qnil
);
5397 /* Sending data to subprocess */
5399 jmp_buf send_process_frame
;
5400 Lisp_Object process_sent_to
;
5403 send_process_trap ()
5405 SIGNAL_THREAD_CHECK (SIGPIPE
);
5410 sigunblock (sigmask (SIGPIPE
));
5411 longjmp (send_process_frame
, 1);
5414 /* Send some data to process PROC.
5415 BUF is the beginning of the data; LEN is the number of characters.
5416 OBJECT is the Lisp object that the data comes from. If OBJECT is
5417 nil or t, it means that the data comes from C string.
5419 If OBJECT is not nil, the data is encoded by PROC's coding-system
5420 for encoding before it is sent.
5422 This function can evaluate Lisp code and can garbage collect. */
5425 send_process (proc
, buf
, len
, object
)
5426 volatile Lisp_Object proc
;
5427 unsigned char *volatile buf
;
5429 volatile Lisp_Object object
;
5431 /* Use volatile to protect variables from being clobbered by longjmp. */
5432 struct Lisp_Process
*p
= XPROCESS (proc
);
5434 struct coding_system
*coding
;
5435 struct gcpro gcpro1
;
5436 SIGTYPE (*volatile old_sigpipe
) ();
5441 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5444 if (p
->raw_status_new
)
5446 if (! EQ (p
->status
, Qrun
))
5447 error ("Process %s not running", SDATA (p
->name
));
5448 if (XINT (p
->outfd
) < 0)
5449 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5451 coding
= proc_encode_coding_system
[XINT (p
->outfd
)];
5452 Vlast_coding_system_used
= coding
->symbol
;
5454 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5455 || (BUFFERP (object
)
5456 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5459 if (!EQ (coding
->symbol
, p
->encode_coding_system
))
5460 /* The coding system for encoding was changed to raw-text
5461 because we sent a unibyte text previously. Now we are
5462 sending a multibyte text, thus we must encode it by the
5463 original coding system specified for the current process. */
5464 setup_coding_system (p
->encode_coding_system
, coding
);
5465 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
5466 coding
->eol_type
= system_eol_type
;
5467 /* src_multibyte should be set to 1 _after_ a call to
5468 setup_coding_system, since it resets src_multibyte to
5470 coding
->src_multibyte
= 1;
5474 /* For sending a unibyte text, character code conversion should
5475 not take place but EOL conversion should. So, setup raw-text
5476 or one of the subsidiary if we have not yet done it. */
5477 if (coding
->type
!= coding_type_raw_text
)
5479 if (CODING_REQUIRE_FLUSHING (coding
))
5481 /* But, before changing the coding, we must flush out data. */
5482 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5483 send_process (proc
, "", 0, Qt
);
5485 coding
->src_multibyte
= 0;
5486 setup_raw_text_coding_system (coding
);
5489 coding
->dst_multibyte
= 0;
5491 if (CODING_REQUIRE_ENCODING (coding
))
5493 int require
= encoding_buffer_size (coding
, len
);
5494 int from_byte
= -1, from
= -1, to
= -1;
5496 if (BUFFERP (object
))
5498 from_byte
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5499 from
= buf_bytepos_to_charpos (XBUFFER (object
), from_byte
);
5500 to
= buf_bytepos_to_charpos (XBUFFER (object
), from_byte
+ len
);
5502 else if (STRINGP (object
))
5504 from_byte
= buf
- SDATA (object
);
5505 from
= string_byte_to_char (object
, from_byte
);
5506 to
= string_byte_to_char (object
, from_byte
+ len
);
5509 if (coding
->composing
!= COMPOSITION_DISABLED
)
5512 coding_save_composition (coding
, from
, to
, object
);
5514 coding
->composing
= COMPOSITION_DISABLED
;
5517 if (SBYTES (p
->encoding_buf
) < require
)
5518 p
->encoding_buf
= make_uninit_string (require
);
5521 buf
= (BUFFERP (object
)
5522 ? BUF_BYTE_ADDRESS (XBUFFER (object
), from_byte
)
5523 : SDATA (object
) + from_byte
);
5525 object
= p
->encoding_buf
;
5526 encode_coding (coding
, (char *) buf
, SDATA (object
),
5527 len
, SBYTES (object
));
5528 coding_free_composition_data (coding
);
5529 len
= coding
->produced
;
5530 buf
= SDATA (object
);
5534 vs
= get_vms_process_pointer (p
->pid
);
5536 error ("Could not find this process: %x", p
->pid
);
5537 else if (write_to_vms_process (vs
, buf
, len
))
5541 if (pty_max_bytes
== 0)
5543 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5544 pty_max_bytes
= fpathconf (XFASTINT (p
->outfd
), _PC_MAX_CANON
);
5545 if (pty_max_bytes
< 0)
5546 pty_max_bytes
= 250;
5548 pty_max_bytes
= 250;
5550 /* Deduct one, to leave space for the eof. */
5554 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5555 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5556 when returning with longjmp despite being declared volatile. */
5557 if (!setjmp (send_process_frame
))
5559 process_sent_to
= proc
;
5564 /* Decide how much data we can send in one batch.
5565 Long lines need to be split into multiple batches. */
5566 if (!NILP (p
->pty_flag
))
5568 /* Starting this at zero is always correct when not the first
5569 iteration because the previous iteration ended by sending C-d.
5570 It may not be correct for the first iteration
5571 if a partial line was sent in a separate send_process call.
5572 If that proves worth handling, we need to save linepos
5573 in the process object. */
5575 unsigned char *ptr
= (unsigned char *) buf
;
5576 unsigned char *end
= (unsigned char *) buf
+ len
;
5578 /* Scan through this text for a line that is too long. */
5579 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5587 /* If we found one, break the line there
5588 and put in a C-d to force the buffer through. */
5592 /* Send this batch, using one or more write calls. */
5595 int outfd
= XINT (p
->outfd
);
5596 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5597 #ifdef DATAGRAM_SOCKETS
5598 if (DATAGRAM_CHAN_P (outfd
))
5600 rv
= sendto (outfd
, (char *) buf
, this,
5601 0, datagram_address
[outfd
].sa
,
5602 datagram_address
[outfd
].len
);
5603 if (rv
< 0 && errno
== EMSGSIZE
)
5605 signal (SIGPIPE
, old_sigpipe
);
5606 report_file_error ("sending datagram",
5607 Fcons (proc
, Qnil
));
5613 rv
= emacs_write (outfd
, (char *) buf
, this);
5614 #ifdef ADAPTIVE_READ_BUFFERING
5615 if (XINT (p
->read_output_delay
) > 0
5616 && EQ (p
->adaptive_read_buffering
, Qt
))
5618 XSETFASTINT (p
->read_output_delay
, 0);
5619 process_output_delay_count
--;
5620 p
->read_output_skip
= Qnil
;
5624 signal (SIGPIPE
, old_sigpipe
);
5630 || errno
== EWOULDBLOCK
5636 /* Buffer is full. Wait, accepting input;
5637 that may allow the program
5638 to finish doing output and read more. */
5642 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5643 /* A gross hack to work around a bug in FreeBSD.
5644 In the following sequence, read(2) returns
5648 write(2) 954 bytes, get EAGAIN
5649 read(2) 1024 bytes in process_read_output
5650 read(2) 11 bytes in process_read_output
5652 That is, read(2) returns more bytes than have
5653 ever been written successfully. The 1033 bytes
5654 read are the 1022 bytes written successfully
5655 after processing (for example with CRs added if
5656 the terminal is set up that way which it is
5657 here). The same bytes will be seen again in a
5658 later read(2), without the CRs. */
5660 if (errno
== EAGAIN
)
5663 ioctl (XINT (p
->outfd
), TIOCFLUSH
, &flags
);
5665 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5667 /* Running filters might relocate buffers or strings.
5668 Arrange to relocate BUF. */
5669 if (BUFFERP (object
))
5670 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5671 else if (STRINGP (object
))
5672 offset
= buf
- SDATA (object
);
5674 #ifdef EMACS_HAS_USECS
5675 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5677 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5680 if (BUFFERP (object
))
5681 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5682 else if (STRINGP (object
))
5683 buf
= offset
+ SDATA (object
);
5688 /* This is a real error. */
5689 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5696 /* If we sent just part of the string, put in an EOF
5697 to force it through, before we send the rest. */
5699 Fprocess_send_eof (proc
);
5702 #endif /* not VMS */
5705 signal (SIGPIPE
, old_sigpipe
);
5707 proc
= process_sent_to
;
5708 p
= XPROCESS (proc
);
5710 p
->raw_status_new
= 0;
5711 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5712 XSETINT (p
->tick
, ++process_tick
);
5713 deactivate_process (proc
);
5715 error ("Error writing to process %s; closed it", SDATA (p
->name
));
5717 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5725 send_process_object_unwind (buf
)
5730 if (XBUFFER (buf
) == current_buffer
)
5732 tembuf
= Fcurrent_buffer ();
5734 Fkill_buffer (tembuf
);
5738 /* Send current contents of region between START and END to PROC.
5739 If START is a string, send it instead.
5740 This function can evaluate Lisp code and can garbage collect. */
5743 send_process_object (proc
, start
, end
)
5744 Lisp_Object proc
, start
, end
;
5746 int count
= SPECPDL_INDEX ();
5747 Lisp_Object object
= STRINGP (start
) ? start
: Fcurrent_buffer ();
5748 struct buffer
*given_buffer
= current_buffer
;
5752 record_unwind_protect (send_process_object_unwind
, Fcurrent_buffer ());
5754 if (STRINGP (object
) ? STRING_MULTIBYTE (object
)
5755 : ! NILP (XBUFFER (object
)->enable_multibyte_characters
))
5757 struct Lisp_Process
*p
= XPROCESS (proc
);
5758 struct coding_system
*coding
;
5760 if (p
->raw_status_new
)
5762 if (! EQ (p
->status
, Qrun
))
5763 error ("Process %s not running", SDATA (p
->name
));
5764 if (XINT (p
->outfd
) < 0)
5765 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5767 coding
= proc_encode_coding_system
[XINT (p
->outfd
)];
5768 if (! EQ (coding
->symbol
, p
->encode_coding_system
))
5769 /* The coding system for encoding was changed to raw-text
5770 because we sent a unibyte text previously. Now we are
5771 sending a multibyte text, thus we must encode it by the
5772 original coding system specified for the current process. */
5773 setup_coding_system (p
->encode_coding_system
, coding
);
5774 if (! NILP (coding
->pre_write_conversion
))
5776 struct gcpro gcpro1
, gcpro2
;
5778 GCPRO2 (proc
, object
);
5779 call2 (coding
->pre_write_conversion
, start
, end
);
5781 if (given_buffer
!= current_buffer
)
5783 start
= make_number (BEGV
), end
= make_number (ZV
);
5784 object
= Fcurrent_buffer ();
5789 if (BUFFERP (object
))
5791 EMACS_INT start_byte
;
5793 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5794 move_gap (XINT (end
));
5795 start_byte
= CHAR_TO_BYTE (XINT (start
));
5796 buf
= BYTE_POS_ADDR (start_byte
);
5797 len
= CHAR_TO_BYTE (XINT (end
)) - start_byte
;
5801 buf
= SDATA (object
);
5802 len
= SBYTES (object
);
5804 send_process (proc
, buf
, len
, object
);
5806 unbind_to (count
, Qnil
);
5809 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5811 doc
: /* Send current contents of region as input to PROCESS.
5812 PROCESS may be a process, a buffer, the name of a process or buffer, or
5813 nil, indicating the current buffer's process.
5814 Called from program, takes three arguments, PROCESS, START and END.
5815 If the region is more than 500 characters long,
5816 it is sent in several bunches. This may happen even for shorter regions.
5817 Output from processes can arrive in between bunches. */)
5818 (process
, start
, end
)
5819 Lisp_Object process
, start
, end
;
5823 proc
= get_process (process
);
5824 validate_region (&start
, &end
);
5825 send_process_object (proc
, start
, end
);
5829 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5831 doc
: /* Send PROCESS the contents of STRING as input.
5832 PROCESS may be a process, a buffer, the name of a process or buffer, or
5833 nil, indicating the current buffer's process.
5834 If STRING is more than 500 characters long,
5835 it is sent in several bunches. This may happen even for shorter strings.
5836 Output from processes can arrive in between bunches. */)
5838 Lisp_Object process
, string
;
5841 CHECK_STRING (string
);
5842 proc
= get_process (process
);
5843 send_process_object (proc
, string
, Qnil
);
5847 /* Return the foreground process group for the tty/pty that
5848 the process P uses. */
5850 emacs_get_tty_pgrp (p
)
5851 struct Lisp_Process
*p
;
5856 if (ioctl (XINT (p
->infd
), TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5859 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5860 master side. Try the slave side. */
5861 fd
= emacs_open (XSTRING (p
->tty_name
)->data
, O_RDONLY
, 0);
5865 ioctl (fd
, TIOCGPGRP
, &gid
);
5869 #endif /* defined (TIOCGPGRP ) */
5874 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5875 Sprocess_running_child_p
, 0, 1, 0,
5876 doc
: /* Return t if PROCESS has given the terminal to a child.
5877 If the operating system does not make it possible to find out,
5878 return t unconditionally. */)
5880 Lisp_Object process
;
5882 /* Initialize in case ioctl doesn't exist or gives an error,
5883 in a way that will cause returning t. */
5886 struct Lisp_Process
*p
;
5888 proc
= get_process (process
);
5889 p
= XPROCESS (proc
);
5891 if (!EQ (p
->childp
, Qt
))
5892 error ("Process %s is not a subprocess",
5894 if (XINT (p
->infd
) < 0)
5895 error ("Process %s is not active",
5898 gid
= emacs_get_tty_pgrp (p
);
5905 /* send a signal number SIGNO to PROCESS.
5906 If CURRENT_GROUP is t, that means send to the process group
5907 that currently owns the terminal being used to communicate with PROCESS.
5908 This is used for various commands in shell mode.
5909 If CURRENT_GROUP is lambda, that means send to the process group
5910 that currently owns the terminal, but only if it is NOT the shell itself.
5912 If NOMSG is zero, insert signal-announcements into process's buffers
5915 If we can, we try to signal PROCESS by sending control characters
5916 down the pty. This allows us to signal inferiors who have changed
5917 their uid, for which killpg would return an EPERM error. */
5920 process_send_signal (process
, signo
, current_group
, nomsg
)
5921 Lisp_Object process
;
5923 Lisp_Object current_group
;
5927 register struct Lisp_Process
*p
;
5931 proc
= get_process (process
);
5932 p
= XPROCESS (proc
);
5934 if (!EQ (p
->childp
, Qt
))
5935 error ("Process %s is not a subprocess",
5937 if (XINT (p
->infd
) < 0)
5938 error ("Process %s is not active",
5941 if (NILP (p
->pty_flag
))
5942 current_group
= Qnil
;
5944 /* If we are using pgrps, get a pgrp number and make it negative. */
5945 if (NILP (current_group
))
5946 /* Send the signal to the shell's process group. */
5950 #ifdef SIGNALS_VIA_CHARACTERS
5951 /* If possible, send signals to the entire pgrp
5952 by sending an input character to it. */
5954 /* TERMIOS is the latest and bestest, and seems most likely to
5955 work. If the system has it, use it. */
5958 cc_t
*sig_char
= NULL
;
5960 tcgetattr (XINT (p
->infd
), &t
);
5965 sig_char
= &t
.c_cc
[VINTR
];
5969 sig_char
= &t
.c_cc
[VQUIT
];
5973 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5974 sig_char
= &t
.c_cc
[VSWTCH
];
5976 sig_char
= &t
.c_cc
[VSUSP
];
5981 if (sig_char
&& *sig_char
!= CDISABLE
)
5983 send_process (proc
, sig_char
, 1, Qnil
);
5986 /* If we can't send the signal with a character,
5987 fall through and send it another way. */
5988 #else /* ! HAVE_TERMIOS */
5990 /* On Berkeley descendants, the following IOCTL's retrieve the
5991 current control characters. */
5992 #if defined (TIOCGLTC) && defined (TIOCGETC)
6000 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
6001 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
6004 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
6005 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
6009 ioctl (XINT (p
->infd
), TIOCGLTC
, &lc
);
6010 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
6012 #endif /* ! defined (SIGTSTP) */
6015 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6017 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6024 ioctl (XINT (p
->infd
), TCGETA
, &t
);
6025 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
6028 ioctl (XINT (p
->infd
), TCGETA
, &t
);
6029 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
6033 ioctl (XINT (p
->infd
), TCGETA
, &t
);
6034 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
6036 #endif /* ! defined (SIGTSTP) */
6038 #else /* ! defined (TCGETA) */
6039 Your configuration files are messed up
.
6040 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6041 you'd better be using one of the alternatives above! */
6042 #endif /* ! defined (TCGETA) */
6043 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6044 /* In this case, the code above should alway returns. */
6046 #endif /* ! defined HAVE_TERMIOS */
6048 /* The code above may fall through if it can't
6049 handle the signal. */
6050 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
6053 /* Get the current pgrp using the tty itself, if we have that.
6054 Otherwise, use the pty to get the pgrp.
6055 On pfa systems, saka@pfu.fujitsu.co.JP writes:
6056 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6057 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
6058 His patch indicates that if TIOCGPGRP returns an error, then
6059 we should just assume that p->pid is also the process group id. */
6061 gid
= emacs_get_tty_pgrp (p
);
6064 /* If we can't get the information, assume
6065 the shell owns the tty. */
6068 /* It is not clear whether anything really can set GID to -1.
6069 Perhaps on some system one of those ioctls can or could do so.
6070 Or perhaps this is vestigial. */
6073 #else /* ! defined (TIOCGPGRP ) */
6074 /* Can't select pgrps on this system, so we know that
6075 the child itself heads the pgrp. */
6077 #endif /* ! defined (TIOCGPGRP ) */
6079 /* If current_group is lambda, and the shell owns the terminal,
6080 don't send any signal. */
6081 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
6089 p
->raw_status_new
= 0;
6091 XSETINT (p
->tick
, ++process_tick
);
6093 status_notify (NULL
);
6095 #endif /* ! defined (SIGCONT) */
6098 send_process (proc
, "\003", 1, Qnil
); /* ^C */
6103 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
6108 sys$
forcex (&(p
->pid
), 0, 1);
6111 flush_pending_output (XINT (p
->infd
));
6115 /* If we don't have process groups, send the signal to the immediate
6116 subprocess. That isn't really right, but it's better than any
6117 obvious alternative. */
6120 kill (p
->pid
, signo
);
6124 /* gid may be a pid, or minus a pgrp's number */
6126 if (!NILP (current_group
))
6128 if (ioctl (XINT (p
->infd
), TIOCSIGSEND
, signo
) == -1)
6129 EMACS_KILLPG (gid
, signo
);
6136 #else /* ! defined (TIOCSIGSEND) */
6137 EMACS_KILLPG (gid
, signo
);
6138 #endif /* ! defined (TIOCSIGSEND) */
6141 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6142 doc
: /* Interrupt process PROCESS.
6143 PROCESS may be a process, a buffer, or the name of a process or buffer.
6144 No arg or nil means current buffer's process.
6145 Second arg CURRENT-GROUP non-nil means send signal to
6146 the current process-group of the process's controlling terminal
6147 rather than to the process's own process group.
6148 If the process is a shell, this means interrupt current subjob
6149 rather than the shell.
6151 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6152 don't send the signal. */)
6153 (process
, current_group
)
6154 Lisp_Object process
, current_group
;
6156 process_send_signal (process
, SIGINT
, current_group
, 0);
6160 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6161 doc
: /* Kill process PROCESS. May be process or name of one.
6162 See function `interrupt-process' for more details on usage. */)
6163 (process
, current_group
)
6164 Lisp_Object process
, current_group
;
6166 process_send_signal (process
, SIGKILL
, current_group
, 0);
6170 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6171 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6172 See function `interrupt-process' for more details on usage. */)
6173 (process
, current_group
)
6174 Lisp_Object process
, current_group
;
6176 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6180 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6181 doc
: /* Stop process PROCESS. May be process or name of one.
6182 See function `interrupt-process' for more details on usage.
6183 If PROCESS is a network process, inhibit handling of incoming traffic. */)
6184 (process
, current_group
)
6185 Lisp_Object process
, current_group
;
6188 if (PROCESSP (process
) && NETCONN_P (process
))
6190 struct Lisp_Process
*p
;
6192 p
= XPROCESS (process
);
6193 if (NILP (p
->command
)
6194 && XINT (p
->infd
) >= 0)
6196 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
6197 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
6204 error ("No SIGTSTP support");
6206 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6211 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6212 doc
: /* Continue process PROCESS. May be process or name of one.
6213 See function `interrupt-process' for more details on usage.
6214 If PROCESS is a network process, resume handling of incoming traffic. */)
6215 (process
, current_group
)
6216 Lisp_Object process
, current_group
;
6219 if (PROCESSP (process
) && NETCONN_P (process
))
6221 struct Lisp_Process
*p
;
6223 p
= XPROCESS (process
);
6224 if (EQ (p
->command
, Qt
)
6225 && XINT (p
->infd
) >= 0
6226 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6228 FD_SET (XINT (p
->infd
), &input_wait_mask
);
6229 FD_SET (XINT (p
->infd
), &non_keyboard_wait_mask
);
6236 process_send_signal (process
, SIGCONT
, current_group
, 0);
6238 error ("No SIGCONT support");
6243 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6244 2, 2, "sProcess (name or number): \nnSignal code: ",
6245 doc
: /* Send PROCESS the signal with code SIGCODE.
6246 PROCESS may also be a number specifying the process id of the
6247 process to signal; in this case, the process need not be a child of
6249 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6251 Lisp_Object process
, sigcode
;
6255 if (INTEGERP (process
))
6257 pid
= XINT (process
);
6261 if (FLOATP (process
))
6263 pid
= (pid_t
) XFLOAT_DATA (process
);
6267 if (STRINGP (process
))
6270 if (tem
= Fget_process (process
), NILP (tem
))
6272 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6279 process
= get_process (process
);
6284 CHECK_PROCESS (process
);
6285 pid
= XPROCESS (process
)->pid
;
6287 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6291 #define parse_signal(NAME, VALUE) \
6292 else if (!xstricmp (name, NAME)) \
6293 XSETINT (sigcode, VALUE)
6295 if (INTEGERP (sigcode
))
6299 unsigned char *name
;
6301 CHECK_SYMBOL (sigcode
);
6302 name
= SDATA (SYMBOL_NAME (sigcode
));
6304 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6310 parse_signal ("usr1", SIGUSR1
);
6313 parse_signal ("usr2", SIGUSR2
);
6316 parse_signal ("term", SIGTERM
);
6319 parse_signal ("hup", SIGHUP
);
6322 parse_signal ("int", SIGINT
);
6325 parse_signal ("quit", SIGQUIT
);
6328 parse_signal ("ill", SIGILL
);
6331 parse_signal ("abrt", SIGABRT
);
6334 parse_signal ("emt", SIGEMT
);
6337 parse_signal ("kill", SIGKILL
);
6340 parse_signal ("fpe", SIGFPE
);
6343 parse_signal ("bus", SIGBUS
);
6346 parse_signal ("segv", SIGSEGV
);
6349 parse_signal ("sys", SIGSYS
);
6352 parse_signal ("pipe", SIGPIPE
);
6355 parse_signal ("alrm", SIGALRM
);
6358 parse_signal ("urg", SIGURG
);
6361 parse_signal ("stop", SIGSTOP
);
6364 parse_signal ("tstp", SIGTSTP
);
6367 parse_signal ("cont", SIGCONT
);
6370 parse_signal ("chld", SIGCHLD
);
6373 parse_signal ("ttin", SIGTTIN
);
6376 parse_signal ("ttou", SIGTTOU
);
6379 parse_signal ("io", SIGIO
);
6382 parse_signal ("xcpu", SIGXCPU
);
6385 parse_signal ("xfsz", SIGXFSZ
);
6388 parse_signal ("vtalrm", SIGVTALRM
);
6391 parse_signal ("prof", SIGPROF
);
6394 parse_signal ("winch", SIGWINCH
);
6397 parse_signal ("info", SIGINFO
);
6400 error ("Undefined signal name %s", name
);
6405 return make_number (kill (pid
, XINT (sigcode
)));
6408 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6409 doc
: /* Make PROCESS see end-of-file in its input.
6410 EOF comes after any text already sent to it.
6411 PROCESS may be a process, a buffer, the name of a process or buffer, or
6412 nil, indicating the current buffer's process.
6413 If PROCESS is a network connection, or is a process communicating
6414 through a pipe (as opposed to a pty), then you cannot send any more
6415 text to PROCESS after you call this function. */)
6417 Lisp_Object process
;
6420 struct coding_system
*coding
;
6422 if (DATAGRAM_CONN_P (process
))
6425 proc
= get_process (process
);
6426 coding
= proc_encode_coding_system
[XINT (XPROCESS (proc
)->outfd
)];
6428 /* Make sure the process is really alive. */
6429 if (XPROCESS (proc
)->raw_status_new
)
6430 update_status (XPROCESS (proc
));
6431 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6432 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6434 if (CODING_REQUIRE_FLUSHING (coding
))
6436 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6437 send_process (proc
, "", 0, Qnil
);
6441 send_process (proc
, "\032", 1, Qnil
); /* ^z */
6443 if (!NILP (XPROCESS (proc
)->pty_flag
))
6444 send_process (proc
, "\004", 1, Qnil
);
6447 int old_outfd
, new_outfd
;
6449 #ifdef HAVE_SHUTDOWN
6450 /* If this is a network connection, or socketpair is used
6451 for communication with the subprocess, call shutdown to cause EOF.
6452 (In some old system, shutdown to socketpair doesn't work.
6453 Then we just can't win.) */
6454 if (XPROCESS (proc
)->pid
== 0
6455 || XINT (XPROCESS (proc
)->outfd
) == XINT (XPROCESS (proc
)->infd
))
6456 shutdown (XINT (XPROCESS (proc
)->outfd
), 1);
6457 /* In case of socketpair, outfd == infd, so don't close it. */
6458 if (XINT (XPROCESS (proc
)->outfd
) != XINT (XPROCESS (proc
)->infd
))
6459 emacs_close (XINT (XPROCESS (proc
)->outfd
));
6460 #else /* not HAVE_SHUTDOWN */
6461 emacs_close (XINT (XPROCESS (proc
)->outfd
));
6462 #endif /* not HAVE_SHUTDOWN */
6463 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6466 old_outfd
= XINT (XPROCESS (proc
)->outfd
);
6468 if (!proc_encode_coding_system
[new_outfd
])
6469 proc_encode_coding_system
[new_outfd
]
6470 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6471 bcopy (proc_encode_coding_system
[old_outfd
],
6472 proc_encode_coding_system
[new_outfd
],
6473 sizeof (struct coding_system
));
6474 bzero (proc_encode_coding_system
[old_outfd
],
6475 sizeof (struct coding_system
));
6477 XSETINT (XPROCESS (proc
)->outfd
, new_outfd
);
6483 /* Kill all processes associated with `buffer'.
6484 If `buffer' is nil, kill all processes */
6487 kill_buffer_processes (buffer
)
6490 Lisp_Object tail
, proc
;
6492 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
6494 proc
= XCDR (XCAR (tail
));
6495 if (GC_PROCESSP (proc
)
6496 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6498 if (NETCONN_P (proc
))
6499 Fdelete_process (proc
);
6500 else if (XINT (XPROCESS (proc
)->infd
) >= 0)
6501 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6506 /* On receipt of a signal that a child status has changed, loop asking
6507 about children with changed statuses until the system says there
6510 All we do is change the status; we do not run sentinels or print
6511 notifications. That is saved for the next time keyboard input is
6512 done, in order to avoid timing errors.
6514 ** WARNING: this can be called during garbage collection.
6515 Therefore, it must not be fooled by the presence of mark bits in
6518 ** USG WARNING: Although it is not obvious from the documentation
6519 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6520 signal() before executing at least one wait(), otherwise the
6521 handler will be called again, resulting in an infinite loop. The
6522 relevant portion of the documentation reads "SIGCLD signals will be
6523 queued and the signal-catching function will be continually
6524 reentered until the queue is empty". Invoking signal() causes the
6525 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6528 ** Malloc WARNING: This should never call malloc either directly or
6529 indirectly; if it does, that is a bug */
6533 sigchld_handler (signo
)
6536 int old_errno
= errno
;
6538 register struct Lisp_Process
*p
;
6539 extern EMACS_TIME
*input_available_clear_time
;
6541 SIGNAL_THREAD_CHECK (signo
);
6545 sigheld
|= sigbit (SIGCHLD
);
6557 #endif /* no WUNTRACED */
6558 /* Keep trying to get a status until we get a definitive result. */
6562 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6564 while (pid
< 0 && errno
== EINTR
);
6568 /* PID == 0 means no processes found, PID == -1 means a real
6569 failure. We have done all our job, so return. */
6571 /* USG systems forget handlers when they are used;
6572 must reestablish each time */
6573 #if defined (USG) && !defined (POSIX_SIGNALS)
6574 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6577 sigheld
&= ~sigbit (SIGCHLD
);
6585 #endif /* no WNOHANG */
6587 /* Find the process that signaled us, and record its status. */
6589 /* The process can have been deleted by Fdelete_process. */
6590 for (tail
= deleted_pid_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
6592 Lisp_Object xpid
= XCAR (tail
);
6593 if ((GC_INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6594 || (GC_FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6596 XSETCAR (tail
, Qnil
);
6597 goto sigchld_end_of_loop
;
6601 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6603 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
6605 proc
= XCDR (XCAR (tail
));
6606 p
= XPROCESS (proc
);
6607 if (GC_EQ (p
->childp
, Qt
) && p
->pid
== pid
)
6612 /* Look for an asynchronous process whose pid hasn't been filled
6615 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCDR (tail
))
6617 proc
= XCDR (XCAR (tail
));
6618 p
= XPROCESS (proc
);
6624 /* Change the status of the process that was found. */
6627 union { int i
; WAITTYPE wt
; } u
;
6628 int clear_desc_flag
= 0;
6630 XSETINT (p
->tick
, ++process_tick
);
6632 p
->raw_status
= u
.i
;
6633 p
->raw_status_new
= 1;
6635 /* If process has terminated, stop waiting for its output. */
6636 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6637 && XINT (p
->infd
) >= 0)
6638 clear_desc_flag
= 1;
6640 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6641 if (clear_desc_flag
)
6643 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
6644 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
6647 /* Tell wait_reading_process_output that it needs to wake up and
6649 if (input_available_clear_time
)
6650 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6653 /* There was no asynchronous process found for that pid: we have
6654 a synchronous process. */
6657 synch_process_alive
= 0;
6659 /* Report the status of the synchronous process. */
6661 synch_process_retcode
= WRETCODE (w
);
6662 else if (WIFSIGNALED (w
))
6663 synch_process_termsig
= WTERMSIG (w
);
6665 /* Tell wait_reading_process_output that it needs to wake up and
6667 if (input_available_clear_time
)
6668 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6671 sigchld_end_of_loop
:
6674 /* On some systems, we must return right away.
6675 If any more processes want to signal us, we will
6677 Otherwise (on systems that have WNOHANG), loop around
6678 to use up all the processes that have something to tell us. */
6679 #if (defined WINDOWSNT \
6680 || (defined USG && !defined GNU_LINUX \
6681 && !(defined HPUX && defined WNOHANG)))
6682 #if defined (USG) && ! defined (POSIX_SIGNALS)
6683 signal (signo
, sigchld_handler
);
6687 #endif /* USG, but not HPUX with WNOHANG */
6690 #endif /* SIGCHLD */
6694 exec_sentinel_unwind (data
)
6697 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6702 exec_sentinel_error_handler (error
)
6705 cmd_error_internal (error
, "error in process sentinel: ");
6707 update_echo_area ();
6708 Fsleep_for (make_number (2), Qnil
);
6713 exec_sentinel (proc
, reason
)
6714 Lisp_Object proc
, reason
;
6716 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6717 register struct Lisp_Process
*p
= XPROCESS (proc
);
6718 int count
= SPECPDL_INDEX ();
6719 int outer_running_asynch_code
= running_asynch_code
;
6720 int waiting
= waiting_for_user_input_p
;
6722 if (inhibit_sentinels
)
6725 /* No need to gcpro these, because all we do with them later
6726 is test them for EQness, and none of them should be a string. */
6727 odeactivate
= Vdeactivate_mark
;
6728 XSETBUFFER (obuffer
, current_buffer
);
6729 okeymap
= current_buffer
->keymap
;
6731 sentinel
= p
->sentinel
;
6732 if (NILP (sentinel
))
6735 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6736 assure that it gets restored no matter how the sentinel exits. */
6738 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6739 /* Inhibit quit so that random quits don't screw up a running filter. */
6740 specbind (Qinhibit_quit
, Qt
);
6741 specbind (Qlast_nonmenu_event
, Qt
);
6743 /* In case we get recursively called,
6744 and we already saved the match data nonrecursively,
6745 save the same match data in safely recursive fashion. */
6746 if (outer_running_asynch_code
)
6749 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6750 restore_search_regs ();
6751 record_unwind_save_match_data ();
6752 Fset_match_data (tem
, Qt
);
6755 /* For speed, if a search happens within this code,
6756 save the match data in a special nonrecursive fashion. */
6757 running_asynch_code
= 1;
6759 internal_condition_case_1 (read_process_output_call
,
6761 Fcons (proc
, Fcons (reason
, Qnil
))),
6762 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6763 exec_sentinel_error_handler
);
6765 /* If we saved the match data nonrecursively, restore it now. */
6766 restore_search_regs ();
6767 running_asynch_code
= outer_running_asynch_code
;
6769 Vdeactivate_mark
= odeactivate
;
6771 /* Restore waiting_for_user_input_p as it was
6772 when we were called, in case the filter clobbered it. */
6773 waiting_for_user_input_p
= waiting
;
6776 if (! EQ (Fcurrent_buffer (), obuffer
)
6777 || ! EQ (current_buffer
->keymap
, okeymap
))
6779 /* But do it only if the caller is actually going to read events.
6780 Otherwise there's no need to make him wake up, and it could
6781 cause trouble (for example it would make sit_for return). */
6782 if (waiting_for_user_input_p
== -1)
6783 record_asynch_buffer_change ();
6785 unbind_to (count
, Qnil
);
6788 /* Report all recent events of a change in process status
6789 (either run the sentinel or output a message).
6790 This is usually done while Emacs is waiting for keyboard input
6791 but can be done at other times. */
6794 status_notify (deleting_process
)
6795 struct Lisp_Process
*deleting_process
;
6797 register Lisp_Object proc
, buffer
;
6798 Lisp_Object tail
, msg
;
6799 struct gcpro gcpro1
, gcpro2
;
6803 /* We need to gcpro tail; if read_process_output calls a filter
6804 which deletes a process and removes the cons to which tail points
6805 from Vprocess_alist, and then causes a GC, tail is an unprotected
6809 /* Set this now, so that if new processes are created by sentinels
6810 that we run, we get called again to handle their status changes. */
6811 update_tick
= process_tick
;
6813 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
6816 register struct Lisp_Process
*p
;
6818 proc
= Fcdr (Fcar (tail
));
6819 p
= XPROCESS (proc
);
6821 if (XINT (p
->tick
) != XINT (p
->update_tick
))
6823 XSETINT (p
->update_tick
, XINT (p
->tick
));
6825 /* If process is still active, read any output that remains. */
6826 while (! EQ (p
->filter
, Qt
)
6827 && ! EQ (p
->status
, Qconnect
)
6828 && ! EQ (p
->status
, Qlisten
)
6829 && ! EQ (p
->command
, Qt
) /* Network process not stopped. */
6830 && XINT (p
->infd
) >= 0
6831 && p
!= deleting_process
6832 && read_process_output (proc
, XINT (p
->infd
)) > 0);
6836 /* Get the text to use for the message. */
6837 if (p
->raw_status_new
)
6839 msg
= status_message (p
);
6841 /* If process is terminated, deactivate it or delete it. */
6843 if (CONSP (p
->status
))
6844 symbol
= XCAR (p
->status
);
6846 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6847 || EQ (symbol
, Qclosed
))
6849 if (delete_exited_processes
)
6850 remove_process (proc
);
6852 deactivate_process (proc
);
6855 /* The actions above may have further incremented p->tick.
6856 So set p->update_tick again
6857 so that an error in the sentinel will not cause
6858 this code to be run again. */
6859 XSETINT (p
->update_tick
, XINT (p
->tick
));
6860 /* Now output the message suitably. */
6861 if (!NILP (p
->sentinel
))
6862 exec_sentinel (proc
, msg
);
6863 /* Don't bother with a message in the buffer
6864 when a process becomes runnable. */
6865 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6867 Lisp_Object ro
, tem
;
6868 struct buffer
*old
= current_buffer
;
6869 int opoint
, opoint_byte
;
6870 int before
, before_byte
;
6872 ro
= XBUFFER (buffer
)->read_only
;
6874 /* Avoid error if buffer is deleted
6875 (probably that's why the process is dead, too) */
6876 if (NILP (XBUFFER (buffer
)->name
))
6878 Fset_buffer (buffer
);
6881 opoint_byte
= PT_BYTE
;
6882 /* Insert new output into buffer
6883 at the current end-of-output marker,
6884 thus preserving logical ordering of input and output. */
6885 if (XMARKER (p
->mark
)->buffer
)
6886 Fgoto_char (p
->mark
);
6888 SET_PT_BOTH (ZV
, ZV_BYTE
);
6891 before_byte
= PT_BYTE
;
6893 tem
= current_buffer
->read_only
;
6894 current_buffer
->read_only
= Qnil
;
6895 insert_string ("\nProcess ");
6896 Finsert (1, &p
->name
);
6897 insert_string (" ");
6899 current_buffer
->read_only
= tem
;
6900 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6902 if (opoint
>= before
)
6903 SET_PT_BOTH (opoint
+ (PT
- before
),
6904 opoint_byte
+ (PT_BYTE
- before_byte
));
6906 SET_PT_BOTH (opoint
, opoint_byte
);
6908 set_buffer_internal (old
);
6913 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6914 redisplay_preserve_echo_area (13);
6920 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6921 Sset_process_coding_system
, 1, 3, 0,
6922 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6923 DECODING will be used to decode subprocess output and ENCODING to
6924 encode subprocess input. */)
6925 (process
, decoding
, encoding
)
6926 register Lisp_Object process
, decoding
, encoding
;
6928 register struct Lisp_Process
*p
;
6930 CHECK_PROCESS (process
);
6931 p
= XPROCESS (process
);
6932 if (XINT (p
->infd
) < 0)
6933 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6934 if (XINT (p
->outfd
) < 0)
6935 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6936 Fcheck_coding_system (decoding
);
6937 Fcheck_coding_system (encoding
);
6939 p
->decode_coding_system
= decoding
;
6940 p
->encode_coding_system
= encoding
;
6941 setup_process_coding_systems (process
);
6946 DEFUN ("process-coding-system",
6947 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6948 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6950 register Lisp_Object process
;
6952 CHECK_PROCESS (process
);
6953 return Fcons (XPROCESS (process
)->decode_coding_system
,
6954 XPROCESS (process
)->encode_coding_system
);
6957 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6958 Sset_process_filter_multibyte
, 2, 2, 0,
6959 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6960 If FLAG is non-nil, the filter is given multibyte strings.
6961 If FLAG is nil, the filter is given unibyte strings. In this case,
6962 all character code conversion except for end-of-line conversion is
6965 Lisp_Object process
, flag
;
6967 register struct Lisp_Process
*p
;
6969 CHECK_PROCESS (process
);
6970 p
= XPROCESS (process
);
6971 p
->filter_multibyte
= flag
;
6972 setup_process_coding_systems (process
);
6977 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6978 Sprocess_filter_multibyte_p
, 1, 1, 0,
6979 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6981 Lisp_Object process
;
6983 register struct Lisp_Process
*p
;
6985 CHECK_PROCESS (process
);
6986 p
= XPROCESS (process
);
6988 return (NILP (p
->filter_multibyte
) ? Qnil
: Qt
);
6993 /* The first time this is called, assume keyboard input comes from DESC
6994 instead of from where we used to expect it.
6995 Subsequent calls mean assume input keyboard can come from DESC
6996 in addition to other places. */
6998 static int add_keyboard_wait_descriptor_called_flag
;
7001 add_keyboard_wait_descriptor (desc
)
7004 if (! add_keyboard_wait_descriptor_called_flag
)
7005 FD_CLR (0, &input_wait_mask
);
7006 add_keyboard_wait_descriptor_called_flag
= 1;
7007 FD_SET (desc
, &input_wait_mask
);
7008 FD_SET (desc
, &non_process_wait_mask
);
7009 if (desc
> max_keyboard_desc
)
7010 max_keyboard_desc
= desc
;
7013 /* From now on, do not expect DESC to give keyboard input. */
7016 delete_keyboard_wait_descriptor (desc
)
7020 int lim
= max_keyboard_desc
;
7022 FD_CLR (desc
, &input_wait_mask
);
7023 FD_CLR (desc
, &non_process_wait_mask
);
7025 if (desc
== max_keyboard_desc
)
7026 for (fd
= 0; fd
< lim
; fd
++)
7027 if (FD_ISSET (fd
, &input_wait_mask
)
7028 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
7029 max_keyboard_desc
= fd
;
7032 /* Return nonzero if *MASK has a bit set
7033 that corresponds to one of the keyboard input descriptors. */
7036 keyboard_bit_set (mask
)
7041 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
7042 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
7043 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
7054 inhibit_sentinels
= 0;
7058 if (! noninteractive
|| initialized
)
7060 signal (SIGCHLD
, sigchld_handler
);
7063 FD_ZERO (&input_wait_mask
);
7064 FD_ZERO (&non_keyboard_wait_mask
);
7065 FD_ZERO (&non_process_wait_mask
);
7066 max_process_desc
= 0;
7068 #ifdef NON_BLOCKING_CONNECT
7069 FD_ZERO (&connect_wait_mask
);
7070 num_pending_connects
= 0;
7073 #ifdef ADAPTIVE_READ_BUFFERING
7074 process_output_delay_count
= 0;
7075 process_output_skip
= 0;
7078 FD_SET (0, &input_wait_mask
);
7080 Vprocess_alist
= Qnil
;
7082 deleted_pid_list
= Qnil
;
7084 for (i
= 0; i
< MAXDESC
; i
++)
7086 chan_process
[i
] = Qnil
;
7087 proc_buffered_char
[i
] = -1;
7089 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
7090 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
7091 #ifdef DATAGRAM_SOCKETS
7092 bzero (datagram_address
, sizeof datagram_address
);
7097 Lisp_Object subfeatures
= Qnil
;
7098 struct socket_options
*sopt
;
7100 #define ADD_SUBFEATURE(key, val) \
7101 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7103 #ifdef NON_BLOCKING_CONNECT
7104 ADD_SUBFEATURE (QCnowait
, Qt
);
7106 #ifdef DATAGRAM_SOCKETS
7107 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7109 #ifdef HAVE_LOCAL_SOCKETS
7110 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7112 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7114 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7116 #ifdef HAVE_GETSOCKNAME
7117 ADD_SUBFEATURE (QCservice
, Qt
);
7119 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY))
7120 ADD_SUBFEATURE (QCserver
, Qt
);
7123 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7124 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7126 Fprovide (intern ("make-network-process"), subfeatures
);
7128 #endif /* HAVE_SOCKETS */
7130 #if defined (DARWIN) || defined (MAC_OSX)
7131 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7132 processes. As such, we only change the default value. */
7135 char *release
= get_operating_system_release();
7136 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7137 && release
[1] == '.')) {
7138 Vprocess_connection_type
= Qnil
;
7147 Qprocessp
= intern ("processp");
7148 staticpro (&Qprocessp
);
7149 Qrun
= intern ("run");
7151 Qstop
= intern ("stop");
7153 Qsignal
= intern ("signal");
7154 staticpro (&Qsignal
);
7156 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7159 Qexit = intern ("exit");
7160 staticpro (&Qexit); */
7162 Qopen
= intern ("open");
7164 Qclosed
= intern ("closed");
7165 staticpro (&Qclosed
);
7166 Qconnect
= intern ("connect");
7167 staticpro (&Qconnect
);
7168 Qfailed
= intern ("failed");
7169 staticpro (&Qfailed
);
7170 Qlisten
= intern ("listen");
7171 staticpro (&Qlisten
);
7172 Qlocal
= intern ("local");
7173 staticpro (&Qlocal
);
7174 Qipv4
= intern ("ipv4");
7177 Qipv6
= intern ("ipv6");
7180 Qdatagram
= intern ("datagram");
7181 staticpro (&Qdatagram
);
7183 QCname
= intern (":name");
7184 staticpro (&QCname
);
7185 QCbuffer
= intern (":buffer");
7186 staticpro (&QCbuffer
);
7187 QChost
= intern (":host");
7188 staticpro (&QChost
);
7189 QCservice
= intern (":service");
7190 staticpro (&QCservice
);
7191 QCtype
= intern (":type");
7192 staticpro (&QCtype
);
7193 QClocal
= intern (":local");
7194 staticpro (&QClocal
);
7195 QCremote
= intern (":remote");
7196 staticpro (&QCremote
);
7197 QCcoding
= intern (":coding");
7198 staticpro (&QCcoding
);
7199 QCserver
= intern (":server");
7200 staticpro (&QCserver
);
7201 QCnowait
= intern (":nowait");
7202 staticpro (&QCnowait
);
7203 QCsentinel
= intern (":sentinel");
7204 staticpro (&QCsentinel
);
7205 QClog
= intern (":log");
7207 QCnoquery
= intern (":noquery");
7208 staticpro (&QCnoquery
);
7209 QCstop
= intern (":stop");
7210 staticpro (&QCstop
);
7211 QCoptions
= intern (":options");
7212 staticpro (&QCoptions
);
7213 QCplist
= intern (":plist");
7214 staticpro (&QCplist
);
7215 QCfilter_multibyte
= intern (":filter-multibyte");
7216 staticpro (&QCfilter_multibyte
);
7218 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7219 staticpro (&Qlast_nonmenu_event
);
7221 staticpro (&Vprocess_alist
);
7223 staticpro (&deleted_pid_list
);
7226 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7227 doc
: /* *Non-nil means delete processes immediately when they exit.
7228 A value of nil means don't delete them until `list-processes' is run. */);
7230 delete_exited_processes
= 1;
7232 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7233 doc
: /* Control type of device used to communicate with subprocesses.
7234 Values are nil to use a pipe, or t or `pty' to use a pty.
7235 The value has no effect if the system has no ptys or if all ptys are busy:
7236 then a pipe is used in any case.
7237 The value takes effect when `start-process' is called. */);
7238 Vprocess_connection_type
= Qt
;
7240 #ifdef ADAPTIVE_READ_BUFFERING
7241 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7242 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7243 On some systems, when Emacs reads the output from a subprocess, the output data
7244 is read in very small blocks, potentially resulting in very poor performance.
7245 This behavior can be remedied to some extent by setting this variable to a
7246 non-nil value, as it will automatically delay reading from such processes, to
7247 allow them to produce more output before Emacs tries to read it.
7248 If the value is t, the delay is reset after each write to the process; any other
7249 non-nil value means that the delay is not reset on write.
7250 The variable takes effect when `start-process' is called. */);
7251 Vprocess_adaptive_read_buffering
= Qt
;
7254 defsubr (&Sprocessp
);
7255 defsubr (&Sget_process
);
7256 defsubr (&Sget_buffer_process
);
7257 defsubr (&Sdelete_process
);
7258 defsubr (&Sprocess_status
);
7259 defsubr (&Sprocess_exit_status
);
7260 defsubr (&Sprocess_id
);
7261 defsubr (&Sprocess_name
);
7262 defsubr (&Sprocess_tty_name
);
7263 defsubr (&Sprocess_command
);
7264 defsubr (&Sset_process_buffer
);
7265 defsubr (&Sprocess_buffer
);
7266 defsubr (&Sprocess_mark
);
7267 defsubr (&Sset_process_filter
);
7268 defsubr (&Sprocess_filter
);
7269 defsubr (&Sset_process_sentinel
);
7270 defsubr (&Sprocess_sentinel
);
7271 defsubr (&Sset_process_window_size
);
7272 defsubr (&Sset_process_inherit_coding_system_flag
);
7273 defsubr (&Sprocess_inherit_coding_system_flag
);
7274 defsubr (&Sset_process_query_on_exit_flag
);
7275 defsubr (&Sprocess_query_on_exit_flag
);
7276 defsubr (&Sprocess_contact
);
7277 defsubr (&Sprocess_plist
);
7278 defsubr (&Sset_process_plist
);
7279 defsubr (&Slist_processes
);
7280 defsubr (&Sprocess_list
);
7281 defsubr (&Sstart_process
);
7283 defsubr (&Sset_network_process_option
);
7284 defsubr (&Smake_network_process
);
7285 defsubr (&Sformat_network_address
);
7286 #endif /* HAVE_SOCKETS */
7287 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7289 defsubr (&Snetwork_interface_list
);
7291 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7292 defsubr (&Snetwork_interface_info
);
7294 #endif /* HAVE_SOCKETS ... */
7295 #ifdef DATAGRAM_SOCKETS
7296 defsubr (&Sprocess_datagram_address
);
7297 defsubr (&Sset_process_datagram_address
);
7299 defsubr (&Saccept_process_output
);
7300 defsubr (&Sprocess_send_region
);
7301 defsubr (&Sprocess_send_string
);
7302 defsubr (&Sinterrupt_process
);
7303 defsubr (&Skill_process
);
7304 defsubr (&Squit_process
);
7305 defsubr (&Sstop_process
);
7306 defsubr (&Scontinue_process
);
7307 defsubr (&Sprocess_running_child_p
);
7308 defsubr (&Sprocess_send_eof
);
7309 defsubr (&Ssignal_process
);
7310 defsubr (&Swaiting_for_user_input_p
);
7311 /* defsubr (&Sprocess_connection); */
7312 defsubr (&Sset_process_coding_system
);
7313 defsubr (&Sprocess_coding_system
);
7314 defsubr (&Sset_process_filter_multibyte
);
7315 defsubr (&Sprocess_filter_multibyte_p
);
7319 #else /* not subprocesses */
7321 #include <sys/types.h>
7325 #include "systime.h"
7326 #include "charset.h"
7328 #include "termopts.h"
7329 #include "sysselect.h"
7331 extern int frame_garbaged
;
7333 extern EMACS_TIME
timer_check ();
7334 extern int timers_run
;
7338 /* As described above, except assuming that there are no subprocesses:
7340 Wait for timeout to elapse and/or keyboard input to be available.
7343 timeout in seconds, or
7344 zero for no limit, or
7345 -1 means gobble data immediately available but don't wait for any.
7347 read_kbd is a Lisp_Object:
7348 0 to ignore keyboard input, or
7349 1 to return when input is available, or
7350 -1 means caller will actually read the input, so don't throw to
7353 see full version for other parameters. We know that wait_proc will
7354 always be NULL, since `subprocesses' isn't defined.
7356 do_display != 0 means redisplay should be done to show subprocess
7357 output that arrives.
7359 Return true if we received input from any process. */
7362 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7363 wait_for_cell
, wait_proc
, just_wait_proc
)
7364 int time_limit
, microsecs
, read_kbd
, do_display
;
7365 Lisp_Object wait_for_cell
;
7366 struct Lisp_Process
*wait_proc
;
7370 EMACS_TIME end_time
, timeout
;
7371 SELECT_TYPE waitchannels
;
7374 /* What does time_limit really mean? */
7375 if (time_limit
|| microsecs
)
7377 EMACS_GET_TIME (end_time
);
7378 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7379 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7382 /* Turn off periodic alarms (in case they are in use)
7383 and then turn off any other atimers,
7384 because the select emulator uses alarms. */
7386 turn_on_atimers (0);
7390 int timeout_reduced_for_timers
= 0;
7392 /* If calling from keyboard input, do not quit
7393 since we want to return C-g as an input character.
7394 Otherwise, do pending quit if requested. */
7398 /* Exit now if the cell we're waiting for became non-nil. */
7399 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7402 /* Compute time from now till when time limit is up */
7403 /* Exit if already run out */
7404 if (time_limit
== -1)
7406 /* -1 specified for timeout means
7407 gobble output available now
7408 but don't wait at all. */
7410 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7412 else if (time_limit
|| microsecs
)
7414 EMACS_GET_TIME (timeout
);
7415 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7416 if (EMACS_TIME_NEG_P (timeout
))
7421 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7424 /* If our caller will not immediately handle keyboard events,
7425 run timer events directly.
7426 (Callers that will immediately read keyboard events
7427 call timer_delay on their own.) */
7428 if (NILP (wait_for_cell
))
7430 EMACS_TIME timer_delay
;
7434 int old_timers_run
= timers_run
;
7435 timer_delay
= timer_check (1);
7436 if (timers_run
!= old_timers_run
&& do_display
)
7437 /* We must retry, since a timer may have requeued itself
7438 and that could alter the time delay. */
7439 redisplay_preserve_echo_area (14);
7443 while (!detect_input_pending ());
7445 /* If there is unread keyboard input, also return. */
7447 && requeued_events_pending_p ())
7450 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7452 EMACS_TIME difference
;
7453 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7454 if (EMACS_TIME_NEG_P (difference
))
7456 timeout
= timer_delay
;
7457 timeout_reduced_for_timers
= 1;
7462 /* Cause C-g and alarm signals to take immediate action,
7463 and cause input available signals to zero out timeout. */
7465 set_waiting_for_input (&timeout
);
7467 /* Wait till there is something to do. */
7469 if (! read_kbd
&& NILP (wait_for_cell
))
7470 FD_ZERO (&waitchannels
);
7472 FD_SET (0, &waitchannels
);
7474 /* If a frame has been newly mapped and needs updating,
7475 reprocess its display stuff. */
7476 if (frame_garbaged
&& do_display
)
7478 clear_waiting_for_input ();
7479 redisplay_preserve_echo_area (15);
7481 set_waiting_for_input (&timeout
);
7484 if (read_kbd
&& detect_input_pending ())
7487 FD_ZERO (&waitchannels
);
7490 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7495 /* Make C-g and alarm signals set flags again */
7496 clear_waiting_for_input ();
7498 /* If we woke up due to SIGWINCH, actually change size now. */
7499 do_pending_window_change (0);
7501 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7502 /* We waited the full specified time, so return now. */
7507 /* If the system call was interrupted, then go around the
7509 if (xerrno
== EINTR
)
7510 FD_ZERO (&waitchannels
);
7512 error ("select error: %s", emacs_strerror (xerrno
));
7515 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7516 /* System sometimes fails to deliver SIGIO. */
7517 kill (getpid (), SIGIO
);
7520 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7521 kill (getpid (), SIGIO
);
7524 /* Check for keyboard input */
7527 && detect_input_pending_run_timers (do_display
))
7529 swallow_events (do_display
);
7530 if (detect_input_pending_run_timers (do_display
))
7534 /* If there is unread keyboard input, also return. */
7536 && requeued_events_pending_p ())
7539 /* If wait_for_cell. check for keyboard input
7540 but don't run any timers.
7541 ??? (It seems wrong to me to check for keyboard
7542 input at all when wait_for_cell, but the code
7543 has been this way since July 1994.
7544 Try changing this after version 19.31.) */
7545 if (! NILP (wait_for_cell
)
7546 && detect_input_pending ())
7548 swallow_events (do_display
);
7549 if (detect_input_pending ())
7553 /* Exit now if the cell we're waiting for became non-nil. */
7554 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7564 /* Don't confuse make-docfile by having two doc strings for this function.
7565 make-docfile does not pay attention to #if, for good reason! */
7566 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7569 register Lisp_Object name
;
7574 /* Don't confuse make-docfile by having two doc strings for this function.
7575 make-docfile does not pay attention to #if, for good reason! */
7576 DEFUN ("process-inherit-coding-system-flag",
7577 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7581 register Lisp_Object process
;
7583 /* Ignore the argument and return the value of
7584 inherit-process-coding-system. */
7585 return inherit_process_coding_system
? Qt
: Qnil
;
7588 /* Kill all processes associated with `buffer'.
7589 If `buffer' is nil, kill all processes.
7590 Since we have no subprocesses, this does nothing. */
7593 kill_buffer_processes (buffer
)
7606 QCtype
= intern (":type");
7607 staticpro (&QCtype
);
7609 defsubr (&Sget_buffer_process
);
7610 defsubr (&Sprocess_inherit_coding_system_flag
);
7614 #endif /* not subprocesses */
7616 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7617 (do not change this comment) */