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, 2009, 2010 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 of the License, or
11 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h> /* some typedefs are used in sys/file.h */
31 #ifdef HAVE_INTTYPES_H
40 /* Only MS-DOS does not define `subprocesses'. */
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 /* Are local (unix) sockets supported? */
49 #if defined (HAVE_SYS_UN_H)
50 #if !defined (AF_LOCAL) && defined (AF_UNIX)
51 #define AF_LOCAL AF_UNIX
54 #define HAVE_LOCAL_SOCKETS
59 #if defined(HAVE_SYS_IOCTL_H)
60 #include <sys/ioctl.h>
61 #if defined(HAVE_NET_IF_H)
63 #endif /* HAVE_NET_IF_H */
64 #endif /* HAVE_SYS_IOCTL_H */
75 #include <netinet/in.h>
76 #include <arpa/nameser.h>
88 #endif /* subprocesses */
96 #include "character.h"
100 #include "termhooks.h"
101 #include "termopts.h"
102 #include "commands.h"
103 #include "keyboard.h"
104 #include "blockinput.h"
105 #include "dispextern.h"
106 #include "composite.h"
108 #include "sysselect.h"
109 #include "syssignal.h"
115 #if defined (USE_GTK) || defined (HAVE_GCONF)
116 #include "xgselect.h"
117 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
122 extern int timers_run
;
124 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
125 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
126 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
127 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
128 Lisp_Object QCname
, QCtype
;
130 /* Non-zero if keyboard input is on hold, zero otherwise. */
132 static int kbd_is_on_hold
;
134 /* Nonzero means delete a process right away if it exits. */
135 static int delete_exited_processes
;
137 /* Nonzero means don't run process sentinels. This is used
139 int inhibit_sentinels
;
143 Lisp_Object Qprocessp
;
144 Lisp_Object Qrun
, Qstop
, Qsignal
;
145 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
146 Lisp_Object Qlocal
, Qipv4
, Qdatagram
, Qseqpacket
;
147 Lisp_Object Qreal
, Qnetwork
, Qserial
;
151 Lisp_Object QCport
, QCspeed
, QCprocess
;
152 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
153 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
154 Lisp_Object QCbuffer
, QChost
, QCservice
;
155 Lisp_Object QClocal
, QCremote
, QCcoding
;
156 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
157 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
158 Lisp_Object Qlast_nonmenu_event
;
159 /* QCfamily is declared and initialized in xfaces.c,
160 QCfilter in keyboard.c. */
161 extern Lisp_Object QCfamily
, QCfilter
;
163 /* Qexit is declared and initialized in eval.c. */
165 /* QCfamily is defined in xfaces.c. */
166 extern Lisp_Object QCfamily
;
167 /* QCfilter is defined in keyboard.c. */
168 extern Lisp_Object QCfilter
;
170 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
171 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
172 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
173 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
175 /* Define first descriptor number available for subprocesses. */
176 #define FIRST_PROC_DESC 3
178 extern const char *get_operating_system_release (void);
180 /* From sysdep.c or w32.c */
181 extern int serial_open (char *port
);
182 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
188 /* t means use pty, nil means use a pipe,
189 maybe other values to come. */
190 static Lisp_Object Vprocess_connection_type
;
192 /* These next two vars are non-static since sysdep.c uses them in the
193 emulation of `select'. */
194 /* Number of events of change of status of a process. */
196 /* Number of events for which the user or sentinel has been notified. */
199 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
201 /* Only W32 has this, it really means that select can't take write mask. */
202 #ifdef BROKEN_NON_BLOCKING_CONNECT
203 #undef NON_BLOCKING_CONNECT
204 #define SELECT_CANT_DO_WRITE_MASK
206 #ifndef NON_BLOCKING_CONNECT
208 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
209 #if defined (O_NONBLOCK) || defined (O_NDELAY)
210 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
211 #define NON_BLOCKING_CONNECT
212 #endif /* EWOULDBLOCK || EINPROGRESS */
213 #endif /* O_NONBLOCK || O_NDELAY */
214 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
215 #endif /* HAVE_SELECT */
216 #endif /* NON_BLOCKING_CONNECT */
217 #endif /* BROKEN_NON_BLOCKING_CONNECT */
219 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
220 this system. We need to read full packets, so we need a
221 "non-destructive" select. So we require either native select,
222 or emulation of select using FIONREAD. */
224 #ifdef BROKEN_DATAGRAM_SOCKETS
225 #undef DATAGRAM_SOCKETS
227 #ifndef DATAGRAM_SOCKETS
228 #if defined (HAVE_SELECT) || defined (FIONREAD)
229 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
230 #define DATAGRAM_SOCKETS
231 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
232 #endif /* HAVE_SELECT || FIONREAD */
233 #endif /* DATAGRAM_SOCKETS */
234 #endif /* BROKEN_DATAGRAM_SOCKETS */
236 #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
237 # define HAVE_SEQPACKET
240 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
241 #ifdef EMACS_HAS_USECS
242 #define ADAPTIVE_READ_BUFFERING
246 #ifdef ADAPTIVE_READ_BUFFERING
247 #define READ_OUTPUT_DELAY_INCREMENT 10000
248 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
249 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
251 /* Number of processes which have a non-zero read_output_delay,
252 and therefore might be delayed for adaptive read buffering. */
254 static int process_output_delay_count
;
256 /* Non-zero if any process has non-nil read_output_skip. */
258 static int process_output_skip
;
260 /* Non-nil means to delay reading process output to improve buffering.
261 A value of t means that delay is reset after each send, any other
262 non-nil value does not reset the delay. A value of nil disables
263 adaptive read buffering completely. */
264 static Lisp_Object Vprocess_adaptive_read_buffering
;
266 #define process_output_delay_count 0
269 static int keyboard_bit_set (SELECT_TYPE
*);
270 static void deactivate_process (Lisp_Object
);
271 static void status_notify (struct Lisp_Process
*);
272 static int read_process_output (Lisp_Object
, int);
273 static void create_pty (Lisp_Object
);
275 /* If we support a window system, turn on the code to poll periodically
276 to detect C-g. It isn't actually used when doing interrupt input. */
277 #ifdef HAVE_WINDOW_SYSTEM
278 #define POLL_FOR_INPUT
281 static Lisp_Object
get_process (register Lisp_Object name
);
282 static void exec_sentinel (Lisp_Object proc
, Lisp_Object reason
);
284 /* Mask of bits indicating the descriptors that we wait for input on. */
286 static SELECT_TYPE input_wait_mask
;
288 /* Mask that excludes keyboard input descriptor(s). */
290 static SELECT_TYPE non_keyboard_wait_mask
;
292 /* Mask that excludes process input descriptor(s). */
294 static SELECT_TYPE non_process_wait_mask
;
296 /* Mask for selecting for write. */
298 static SELECT_TYPE write_mask
;
300 #ifdef NON_BLOCKING_CONNECT
301 /* Mask of bits indicating the descriptors that we wait for connect to
302 complete on. Once they complete, they are removed from this mask
303 and added to the input_wait_mask and non_keyboard_wait_mask. */
305 static SELECT_TYPE connect_wait_mask
;
307 /* Number of bits set in connect_wait_mask. */
308 static int num_pending_connects
;
310 #define IF_NON_BLOCKING_CONNECT(s) s
311 #else /* NON_BLOCKING_CONNECT */
312 #define IF_NON_BLOCKING_CONNECT(s)
313 #endif /* NON_BLOCKING_CONNECT */
315 /* The largest descriptor currently in use for a process object. */
316 static int max_process_desc
;
318 /* The largest descriptor currently in use for input. */
319 static int max_input_desc
;
321 /* Indexed by descriptor, gives the process (if any) for that descriptor */
322 Lisp_Object chan_process
[MAXDESC
];
324 /* Alist of elements (NAME . PROCESS) */
325 Lisp_Object Vprocess_alist
;
327 /* Buffered-ahead input char from process, indexed by channel.
328 -1 means empty (no char is buffered).
329 Used on sys V where the only way to tell if there is any
330 output from the process is to read at least one char.
331 Always -1 on systems that support FIONREAD. */
333 /* Don't make static; need to access externally. */
334 int proc_buffered_char
[MAXDESC
];
336 /* Table of `struct coding-system' for each process. */
337 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
338 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
340 #ifdef DATAGRAM_SOCKETS
341 /* Table of `partner address' for datagram sockets. */
342 struct sockaddr_and_len
{
345 } datagram_address
[MAXDESC
];
346 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
347 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
349 #define DATAGRAM_CHAN_P(chan) (0)
350 #define DATAGRAM_CONN_P(proc) (0)
353 /* Maximum number of bytes to send to a pty without an eof. */
354 static int pty_max_bytes
;
358 struct fd_callback_data
364 int condition
; /* mask of the defines above. */
365 } fd_callback_info
[MAXDESC
];
368 /* Add a file descriptor FD to be monitored for when read is possible.
369 When read is possible, call FUNC with argument DATA. */
372 add_read_fd (int fd
, fd_callback func
, void *data
)
374 xassert (fd
< MAXDESC
);
375 add_keyboard_wait_descriptor (fd
);
377 fd_callback_info
[fd
].func
= func
;
378 fd_callback_info
[fd
].data
= data
;
379 fd_callback_info
[fd
].condition
|= FOR_READ
;
382 /* Stop monitoring file descriptor FD for when read is possible. */
385 delete_read_fd (int fd
)
387 xassert (fd
< MAXDESC
);
388 delete_keyboard_wait_descriptor (fd
);
390 fd_callback_info
[fd
].condition
&= ~FOR_READ
;
391 if (fd_callback_info
[fd
].condition
== 0)
393 fd_callback_info
[fd
].func
= 0;
394 fd_callback_info
[fd
].data
= 0;
398 /* Add a file descriptor FD to be monitored for when write is possible.
399 When write is possible, call FUNC with argument DATA. */
402 add_write_fd (int fd
, fd_callback func
, void *data
)
404 xassert (fd
< MAXDESC
);
405 FD_SET (fd
, &write_mask
);
406 if (fd
> max_input_desc
)
409 fd_callback_info
[fd
].func
= func
;
410 fd_callback_info
[fd
].data
= data
;
411 fd_callback_info
[fd
].condition
|= FOR_WRITE
;
414 /* Stop monitoring file descriptor FD for when write is possible. */
417 delete_write_fd (int fd
)
419 int lim
= max_input_desc
;
421 xassert (fd
< MAXDESC
);
422 FD_CLR (fd
, &write_mask
);
423 fd_callback_info
[fd
].condition
&= ~FOR_WRITE
;
424 if (fd_callback_info
[fd
].condition
== 0)
426 fd_callback_info
[fd
].func
= 0;
427 fd_callback_info
[fd
].data
= 0;
429 if (fd
== max_input_desc
)
430 for (fd
= lim
; fd
>= 0; fd
--)
431 if (FD_ISSET (fd
, &input_wait_mask
) || FD_ISSET (fd
, &write_mask
))
441 /* Compute the Lisp form of the process status, p->status, from
442 the numeric status that was returned by `wait'. */
444 static Lisp_Object
status_convert (int);
447 update_status (struct Lisp_Process
*p
)
449 eassert (p
->raw_status_new
);
450 p
->status
= status_convert (p
->raw_status
);
451 p
->raw_status_new
= 0;
454 /* Convert a process status word in Unix format to
455 the list that we use internally. */
458 status_convert (int w
)
461 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
462 else if (WIFEXITED (w
))
463 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
464 WCOREDUMP (w
) ? Qt
: Qnil
));
465 else if (WIFSIGNALED (w
))
466 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
467 WCOREDUMP (w
) ? Qt
: Qnil
));
472 /* Given a status-list, extract the three pieces of information
473 and store them individually through the three pointers. */
476 decode_status (Lisp_Object l
, Lisp_Object
*symbol
, int *code
, int *coredump
)
490 *code
= XFASTINT (XCAR (tem
));
492 *coredump
= !NILP (tem
);
496 /* Return a string describing a process status list. */
499 status_message (struct Lisp_Process
*p
)
501 Lisp_Object status
= p
->status
;
504 Lisp_Object string
, string2
;
506 decode_status (status
, &symbol
, &code
, &coredump
);
508 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
511 synchronize_system_messages_locale ();
512 signame
= strsignal (code
);
514 string
= build_string ("unknown");
519 string
= make_unibyte_string (signame
, strlen (signame
));
520 if (! NILP (Vlocale_coding_system
))
521 string
= (code_convert_string_norecord
522 (string
, Vlocale_coding_system
, 0));
523 c1
= STRING_CHAR ((char *) SDATA (string
));
526 Faset (string
, make_number (0), make_number (c2
));
528 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
529 return concat2 (string
, string2
);
531 else if (EQ (symbol
, Qexit
))
534 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
536 return build_string ("finished\n");
537 string
= Fnumber_to_string (make_number (code
));
538 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
539 return concat3 (build_string ("exited abnormally with code "),
542 else if (EQ (symbol
, Qfailed
))
544 string
= Fnumber_to_string (make_number (code
));
545 string2
= build_string ("\n");
546 return concat3 (build_string ("failed with code "),
550 return Fcopy_sequence (Fsymbol_name (symbol
));
555 /* The file name of the pty opened by allocate_pty. */
556 static char pty_name
[24];
558 /* Open an available pty, returning a file descriptor.
559 Return -1 on failure.
560 The file name of the terminal corresponding to the pty
561 is left in the variable pty_name. */
572 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
573 for (i
= 0; i
< 16; i
++)
576 struct stat stb
; /* Used in some PTY_OPEN. */
577 #ifdef PTY_NAME_SPRINTF
580 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
581 #endif /* no PTY_NAME_SPRINTF */
585 #else /* no PTY_OPEN */
587 { /* Some systems name their pseudoterminals so that there are gaps in
588 the usual sequence - for example, on HP9000/S700 systems, there
589 are no pseudoterminals with names ending in 'f'. So we wait for
590 three failures in a row before deciding that we've reached the
592 int failed_count
= 0;
594 if (stat (pty_name
, &stb
) < 0)
597 if (failed_count
>= 3)
604 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
606 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
609 #endif /* no PTY_OPEN */
613 /* check to make certain that both sides are available
614 this avoids a nasty yet stupid bug in rlogins */
615 #ifdef PTY_TTY_NAME_SPRINTF
618 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
619 #endif /* no PTY_TTY_NAME_SPRINTF */
620 if (access (pty_name
, 6) != 0)
635 #endif /* HAVE_PTYS */
638 make_process (Lisp_Object name
)
640 register Lisp_Object val
, tem
, name1
;
641 register struct Lisp_Process
*p
;
645 p
= allocate_process ();
653 p
->raw_status_new
= 0;
655 p
->mark
= Fmake_marker ();
656 p
->kill_without_query
= 0;
658 #ifdef ADAPTIVE_READ_BUFFERING
659 p
->adaptive_read_buffering
= 0;
660 p
->read_output_delay
= 0;
661 p
->read_output_skip
= 0;
665 p
->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
666 p
->gnutls_log_level
= 0;
670 /* If name is already in use, modify it until it is unused. */
675 tem
= Fget_process (name1
);
676 if (NILP (tem
)) break;
677 sprintf (suffix
, "<%d>", i
);
678 name1
= concat2 (name
, build_string (suffix
));
682 XSETPROCESS (val
, p
);
683 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
688 remove_process (register Lisp_Object proc
)
690 register Lisp_Object pair
;
692 pair
= Frassq (proc
, Vprocess_alist
);
693 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
695 deactivate_process (proc
);
699 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
700 doc
: /* Return t if OBJECT is a process. */)
703 return PROCESSP (object
) ? Qt
: Qnil
;
706 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
707 doc
: /* Return the process named NAME, or nil if there is none. */)
708 (register Lisp_Object name
)
713 return Fcdr (Fassoc (name
, Vprocess_alist
));
716 /* This is how commands for the user decode process arguments. It
717 accepts a process, a process name, a buffer, a buffer name, or nil.
718 Buffers denote the first process in the buffer, and nil denotes the
722 get_process (register Lisp_Object name
)
724 register Lisp_Object proc
, obj
;
727 obj
= Fget_process (name
);
729 obj
= Fget_buffer (name
);
731 error ("Process %s does not exist", SDATA (name
));
733 else if (NILP (name
))
734 obj
= Fcurrent_buffer ();
738 /* Now obj should be either a buffer object or a process object.
742 proc
= Fget_buffer_process (obj
);
744 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
756 /* Fdelete_process promises to immediately forget about the process, but in
757 reality, Emacs needs to remember those processes until they have been
758 treated by sigchld_handler; otherwise this handler would consider the
759 process as being synchronous and say that the synchronous process is
761 static Lisp_Object deleted_pid_list
;
764 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
765 doc
: /* Delete PROCESS: kill it and forget about it immediately.
766 PROCESS may be a process, a buffer, the name of a process or buffer, or
767 nil, indicating the current buffer's process. */)
768 (register Lisp_Object process
)
770 register struct Lisp_Process
*p
;
772 process
= get_process (process
);
773 p
= XPROCESS (process
);
775 p
->raw_status_new
= 0;
776 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
778 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
779 p
->tick
= ++process_tick
;
781 redisplay_preserve_echo_area (13);
783 else if (p
->infd
>= 0)
787 /* Assignment to EMACS_INT stops GCC whining about limited range
789 EMACS_INT pid
= p
->pid
;
791 /* No problem storing the pid here, as it is still in Vprocess_alist. */
792 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
793 /* GC treated elements set to nil. */
794 Fdelq (Qnil
, deleted_pid_list
));
795 /* If the process has already signaled, remove it from the list. */
796 if (p
->raw_status_new
)
799 if (CONSP (p
->status
))
800 symbol
= XCAR (p
->status
);
801 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
803 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
807 Fkill_process (process
, Qnil
);
808 /* Do this now, since remove_process will make sigchld_handler do nothing. */
810 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
811 p
->tick
= ++process_tick
;
813 redisplay_preserve_echo_area (13);
816 remove_process (process
);
820 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
821 doc
: /* Return the status of PROCESS.
822 The returned value is one of the following symbols:
823 run -- for a process that is running.
824 stop -- for a process stopped but continuable.
825 exit -- for a process that has exited.
826 signal -- for a process that has got a fatal signal.
827 open -- for a network stream connection that is open.
828 listen -- for a network stream server that is listening.
829 closed -- for a network stream connection that is closed.
830 connect -- when waiting for a non-blocking connection to complete.
831 failed -- when a non-blocking connection has failed.
832 nil -- if arg is a process name and no such process exists.
833 PROCESS may be a process, a buffer, the name of a process, or
834 nil, indicating the current buffer's process. */)
835 (register Lisp_Object process
)
837 register struct Lisp_Process
*p
;
838 register Lisp_Object status
;
840 if (STRINGP (process
))
841 process
= Fget_process (process
);
843 process
= get_process (process
);
848 p
= XPROCESS (process
);
849 if (p
->raw_status_new
)
853 status
= XCAR (status
);
854 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
856 if (EQ (status
, Qexit
))
858 else if (EQ (p
->command
, Qt
))
860 else if (EQ (status
, Qrun
))
866 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
868 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
869 If PROCESS has not yet exited or died, return 0. */)
870 (register Lisp_Object process
)
872 CHECK_PROCESS (process
);
873 if (XPROCESS (process
)->raw_status_new
)
874 update_status (XPROCESS (process
));
875 if (CONSP (XPROCESS (process
)->status
))
876 return XCAR (XCDR (XPROCESS (process
)->status
));
877 return make_number (0);
880 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
881 doc
: /* Return the process id of PROCESS.
882 This is the pid of the external process which PROCESS uses or talks to.
883 For a network connection, this value is nil. */)
884 (register Lisp_Object process
)
886 /* Assignment to EMACS_INT stops GCC whining about limited range of
890 CHECK_PROCESS (process
);
891 pid
= XPROCESS (process
)->pid
;
892 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
895 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
896 doc
: /* Return the name of PROCESS, as a string.
897 This is the name of the program invoked in PROCESS,
898 possibly modified to make it unique among process names. */)
899 (register Lisp_Object process
)
901 CHECK_PROCESS (process
);
902 return XPROCESS (process
)->name
;
905 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
906 doc
: /* Return the command that was executed to start PROCESS.
907 This is a list of strings, the first string being the program executed
908 and the rest of the strings being the arguments given to it.
909 For a network or serial process, this is nil (process is running) or t
910 \(process is stopped). */)
911 (register Lisp_Object process
)
913 CHECK_PROCESS (process
);
914 return XPROCESS (process
)->command
;
917 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
918 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
919 This is the terminal that the process itself reads and writes on,
920 not the name of the pty that Emacs uses to talk with that terminal. */)
921 (register Lisp_Object process
)
923 CHECK_PROCESS (process
);
924 return XPROCESS (process
)->tty_name
;
927 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
929 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
930 (register Lisp_Object process
, Lisp_Object buffer
)
932 struct Lisp_Process
*p
;
934 CHECK_PROCESS (process
);
936 CHECK_BUFFER (buffer
);
937 p
= XPROCESS (process
);
939 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
940 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
941 setup_process_coding_systems (process
);
945 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
947 doc
: /* Return the buffer PROCESS is associated with.
948 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
949 (register Lisp_Object process
)
951 CHECK_PROCESS (process
);
952 return XPROCESS (process
)->buffer
;
955 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
957 doc
: /* Return the marker for the end of the last output from PROCESS. */)
958 (register Lisp_Object process
)
960 CHECK_PROCESS (process
);
961 return XPROCESS (process
)->mark
;
964 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
966 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
967 A value of t means stop accepting output from the process.
969 When a process has a filter, its buffer is not used for output.
970 Instead, each time it does output, the entire string of output is
971 passed to the filter.
973 The filter gets two arguments: the process and the string of output.
974 The string argument is normally a multibyte string, except:
975 - if the process' input coding system is no-conversion or raw-text,
976 it is a unibyte string (the non-converted input), or else
977 - if `default-enable-multibyte-characters' is nil, it is a unibyte
978 string (the result of converting the decoded input multibyte
979 string to unibyte with `string-make-unibyte'). */)
980 (register Lisp_Object process
, Lisp_Object filter
)
982 struct Lisp_Process
*p
;
984 CHECK_PROCESS (process
);
985 p
= XPROCESS (process
);
987 /* Don't signal an error if the process' input file descriptor
988 is closed. This could make debugging Lisp more difficult,
989 for example when doing something like
991 (setq process (start-process ...))
993 (set-process-filter process ...) */
997 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
999 FD_CLR (p
->infd
, &input_wait_mask
);
1000 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1002 else if (EQ (p
->filter
, Qt
)
1003 /* Network or serial process not stopped: */
1004 && !EQ (p
->command
, Qt
))
1006 FD_SET (p
->infd
, &input_wait_mask
);
1007 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1012 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1013 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1014 setup_process_coding_systems (process
);
1018 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1020 doc
: /* Returns the filter function of PROCESS; nil if none.
1021 See `set-process-filter' for more info on filter functions. */)
1022 (register Lisp_Object process
)
1024 CHECK_PROCESS (process
);
1025 return XPROCESS (process
)->filter
;
1028 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1030 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1031 The sentinel is called as a function when the process changes state.
1032 It gets two arguments: the process, and a string describing the change. */)
1033 (register Lisp_Object process
, Lisp_Object sentinel
)
1035 struct Lisp_Process
*p
;
1037 CHECK_PROCESS (process
);
1038 p
= XPROCESS (process
);
1040 p
->sentinel
= sentinel
;
1041 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1042 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1046 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1048 doc
: /* Return the sentinel of PROCESS; nil if none.
1049 See `set-process-sentinel' for more info on sentinels. */)
1050 (register Lisp_Object process
)
1052 CHECK_PROCESS (process
);
1053 return XPROCESS (process
)->sentinel
;
1056 DEFUN ("set-process-window-size", Fset_process_window_size
,
1057 Sset_process_window_size
, 3, 3, 0,
1058 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1059 (register Lisp_Object process
, Lisp_Object height
, Lisp_Object width
)
1061 CHECK_PROCESS (process
);
1062 CHECK_NATNUM (height
);
1063 CHECK_NATNUM (width
);
1065 if (XPROCESS (process
)->infd
< 0
1066 || set_window_size (XPROCESS (process
)->infd
,
1067 XINT (height
), XINT (width
)) <= 0)
1073 DEFUN ("set-process-inherit-coding-system-flag",
1074 Fset_process_inherit_coding_system_flag
,
1075 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1076 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1077 If the second argument FLAG is non-nil, then the variable
1078 `buffer-file-coding-system' of the buffer associated with PROCESS
1079 will be bound to the value of the coding system used to decode
1082 This is useful when the coding system specified for the process buffer
1083 leaves either the character code conversion or the end-of-line conversion
1084 unspecified, or if the coding system used to decode the process output
1085 is more appropriate for saving the process buffer.
1087 Binding the variable `inherit-process-coding-system' to non-nil before
1088 starting the process is an alternative way of setting the inherit flag
1089 for the process which will run. */)
1090 (register Lisp_Object process
, Lisp_Object flag
)
1092 CHECK_PROCESS (process
);
1093 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1097 DEFUN ("set-process-query-on-exit-flag",
1098 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1100 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1101 If the second argument FLAG is non-nil, Emacs will query the user before
1102 exiting or killing a buffer if PROCESS is running. */)
1103 (register Lisp_Object process
, Lisp_Object flag
)
1105 CHECK_PROCESS (process
);
1106 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1110 DEFUN ("process-query-on-exit-flag",
1111 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1113 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1114 (register Lisp_Object process
)
1116 CHECK_PROCESS (process
);
1117 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1120 #ifdef DATAGRAM_SOCKETS
1121 Lisp_Object
Fprocess_datagram_address (Lisp_Object process
);
1124 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1126 doc
: /* Return the contact info of PROCESS; t for a real child.
1127 For a network or serial connection, the value depends on the optional
1128 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1129 SERVICE) for a network connection or (PORT SPEED) for a serial
1130 connection. If KEY is t, the complete contact information for the
1131 connection is returned, else the specific value for the keyword KEY is
1132 returned. See `make-network-process' or `make-serial-process' for a
1133 list of keywords. */)
1134 (register Lisp_Object process
, Lisp_Object key
)
1136 Lisp_Object contact
;
1138 CHECK_PROCESS (process
);
1139 contact
= XPROCESS (process
)->childp
;
1141 #ifdef DATAGRAM_SOCKETS
1142 if (DATAGRAM_CONN_P (process
)
1143 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1144 contact
= Fplist_put (contact
, QCremote
,
1145 Fprocess_datagram_address (process
));
1148 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1150 if (NILP (key
) && NETCONN_P (process
))
1151 return Fcons (Fplist_get (contact
, QChost
),
1152 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1153 if (NILP (key
) && SERIALCONN_P (process
))
1154 return Fcons (Fplist_get (contact
, QCport
),
1155 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1156 return Fplist_get (contact
, key
);
1159 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1161 doc
: /* Return the plist of PROCESS. */)
1162 (register Lisp_Object process
)
1164 CHECK_PROCESS (process
);
1165 return XPROCESS (process
)->plist
;
1168 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1170 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1171 (register Lisp_Object process
, Lisp_Object plist
)
1173 CHECK_PROCESS (process
);
1176 XPROCESS (process
)->plist
= plist
;
1180 #if 0 /* Turned off because we don't currently record this info
1181 in the process. Perhaps add it. */
1182 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1183 doc
: /* Return the connection type of PROCESS.
1184 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1185 a socket connection. */)
1186 (Lisp_Object process
)
1188 return XPROCESS (process
)->type
;
1192 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1193 doc
: /* Return the connection type of PROCESS.
1194 The value is either the symbol `real', `network', or `serial'.
1195 PROCESS may be a process, a buffer, the name of a process or buffer, or
1196 nil, indicating the current buffer's process. */)
1197 (Lisp_Object process
)
1200 proc
= get_process (process
);
1201 return XPROCESS (proc
)->type
;
1204 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1206 doc
: /* Convert network ADDRESS from internal format to a string.
1207 A 4 or 5 element vector represents an IPv4 address (with port number).
1208 An 8 or 9 element vector represents an IPv6 address (with port number).
1209 If optional second argument OMIT-PORT is non-nil, don't include a port
1210 number in the string, even when present in ADDRESS.
1211 Returns nil if format of ADDRESS is invalid. */)
1212 (Lisp_Object address
, Lisp_Object omit_port
)
1217 if (STRINGP (address
)) /* AF_LOCAL */
1220 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1222 register struct Lisp_Vector
*p
= XVECTOR (address
);
1223 Lisp_Object args
[10];
1226 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1228 args
[0] = build_string ("%d.%d.%d.%d");
1231 else if (p
->size
== 5)
1233 args
[0] = build_string ("%d.%d.%d.%d:%d");
1236 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1238 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1241 else if (p
->size
== 9)
1243 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1249 for (i
= 0; i
< nargs
; i
++)
1251 EMACS_INT element
= XINT (p
->contents
[i
]);
1253 if (element
< 0 || element
> 65535)
1256 if (nargs
<= 5 /* IPv4 */
1257 && i
< 4 /* host, not port */
1261 args
[i
+1] = p
->contents
[i
];
1264 return Fformat (nargs
+1, args
);
1267 if (CONSP (address
))
1269 Lisp_Object args
[2];
1270 args
[0] = build_string ("<Family %d>");
1271 args
[1] = Fcar (address
);
1272 return Fformat (2, args
);
1279 list_processes_1 (Lisp_Object query_only
)
1281 register Lisp_Object tail
, tem
;
1282 Lisp_Object proc
, minspace
, tem1
;
1283 register struct Lisp_Process
*p
;
1285 int w_proc
, w_buffer
, w_tty
;
1287 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1289 w_proc
= 4; /* Proc */
1290 w_buffer
= 6; /* Buffer */
1291 w_tty
= 0; /* Omit if no ttys */
1293 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1297 proc
= Fcdr (XCAR (tail
));
1298 p
= XPROCESS (proc
);
1301 if (!NILP (query_only
) && p
->kill_without_query
)
1303 if (STRINGP (p
->name
)
1304 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1306 if (!NILP (p
->buffer
))
1308 if (NILP (XBUFFER (p
->buffer
)->name
))
1311 w_buffer
= 8; /* (Killed) */
1313 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1316 if (STRINGP (p
->tty_name
)
1317 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1321 XSETFASTINT (i_status
, w_proc
+ 1);
1322 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1325 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1326 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1331 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1334 XSETFASTINT (minspace
, 1);
1336 set_buffer_internal (XBUFFER (Vstandard_output
));
1337 current_buffer
->undo_list
= Qt
;
1339 current_buffer
->truncate_lines
= Qt
;
1341 write_string ("Proc", -1);
1342 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1343 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1346 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1348 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1349 write_string ("\n", -1);
1351 write_string ("----", -1);
1352 Findent_to (i_status
, minspace
); write_string ("------", -1);
1353 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1356 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1358 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1359 write_string ("\n", -1);
1361 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1365 proc
= Fcdr (XCAR (tail
));
1366 p
= XPROCESS (proc
);
1369 if (!NILP (query_only
) && p
->kill_without_query
)
1372 Finsert (1, &p
->name
);
1373 Findent_to (i_status
, minspace
);
1375 if (p
->raw_status_new
)
1378 if (CONSP (p
->status
))
1379 symbol
= XCAR (p
->status
);
1381 if (EQ (symbol
, Qsignal
))
1384 tem
= Fcar (Fcdr (p
->status
));
1385 Fprinc (symbol
, Qnil
);
1387 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1389 if (EQ (symbol
, Qexit
))
1390 write_string ("closed", -1);
1391 else if (EQ (p
->command
, Qt
))
1392 write_string ("stopped", -1);
1393 else if (EQ (symbol
, Qrun
))
1394 write_string ("open", -1);
1396 Fprinc (symbol
, Qnil
);
1398 else if (SERIALCONN1_P (p
))
1400 write_string ("running", -1);
1403 Fprinc (symbol
, Qnil
);
1405 if (EQ (symbol
, Qexit
))
1408 tem
= Fcar (Fcdr (p
->status
));
1411 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1412 write_string (tembuf
, -1);
1416 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1419 Findent_to (i_buffer
, minspace
);
1420 if (NILP (p
->buffer
))
1421 insert_string ("(none)");
1422 else if (NILP (XBUFFER (p
->buffer
)->name
))
1423 insert_string ("(Killed)");
1425 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1429 Findent_to (i_tty
, minspace
);
1430 if (STRINGP (p
->tty_name
))
1431 Finsert (1, &p
->tty_name
);
1434 Findent_to (i_command
, minspace
);
1436 if (EQ (p
->status
, Qlisten
))
1438 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1439 if (INTEGERP (port
))
1440 port
= Fnumber_to_string (port
);
1442 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1443 sprintf (tembuf
, "(network %s server on %s)\n",
1444 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1445 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1446 insert_string (tembuf
);
1448 else if (NETCONN1_P (p
))
1450 /* For a local socket, there is no host name,
1451 so display service instead. */
1452 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1453 if (!STRINGP (host
))
1455 host
= Fplist_get (p
->childp
, QCservice
);
1456 if (INTEGERP (host
))
1457 host
= Fnumber_to_string (host
);
1460 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1461 sprintf (tembuf
, "(network %s connection to %s)\n",
1462 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1463 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1464 insert_string (tembuf
);
1466 else if (SERIALCONN1_P (p
))
1468 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1469 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1470 insert_string ("(serial port ");
1472 insert_string (SDATA (port
));
1474 insert_string ("?");
1475 if (INTEGERP (speed
))
1477 sprintf (tembuf
, " at %ld b/s", (long) XINT (speed
));
1478 insert_string (tembuf
);
1480 insert_string (")\n");
1494 insert_string (" ");
1496 insert_string ("\n");
1501 status_notify (NULL
);
1502 redisplay_preserve_echo_area (13);
1507 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1508 doc
: /* Display a list of all processes.
1509 If optional argument QUERY-ONLY is non-nil, only processes with
1510 the query-on-exit flag set will be listed.
1511 Any process listed as exited or signaled is actually eliminated
1512 after the listing is made. */)
1513 (Lisp_Object query_only
)
1515 internal_with_output_to_temp_buffer ("*Process List*",
1516 list_processes_1
, query_only
);
1520 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1521 doc
: /* Return a list of all processes. */)
1524 return Fmapcar (Qcdr
, Vprocess_alist
);
1527 /* Starting asynchronous inferior processes. */
1529 static Lisp_Object
start_process_unwind (Lisp_Object proc
);
1531 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1532 doc
: /* Start a program in a subprocess. Return the process object for it.
1533 NAME is name for process. It is modified if necessary to make it unique.
1534 BUFFER is the buffer (or buffer name) to associate with the process.
1536 Process output (both standard output and standard error streams) goes
1537 at end of BUFFER, unless you specify an output stream or filter
1538 function to handle the output. BUFFER may also be nil, meaning that
1539 this process is not associated with any buffer.
1541 PROGRAM is the program file name. It is searched for in PATH. If
1542 nil, just associate a pty with the buffer. Remaining arguments are
1543 strings to give program as arguments.
1545 If you want to separate standard output from standard error, invoke
1546 the command through a shell and redirect one of them using the shell
1549 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1550 (int nargs
, register Lisp_Object
*args
)
1552 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1553 register unsigned char **new_argv
;
1555 int count
= SPECPDL_INDEX ();
1559 buffer
= Fget_buffer_create (buffer
);
1561 /* Make sure that the child will be able to chdir to the current
1562 buffer's current directory, or its unhandled equivalent. We
1563 can't just have the child check for an error when it does the
1564 chdir, since it's in a vfork.
1566 We have to GCPRO around this because Fexpand_file_name and
1567 Funhandled_file_name_directory might call a file name handling
1568 function. The argument list is protected by the caller, so all
1569 we really have to worry about is buffer. */
1571 struct gcpro gcpro1
, gcpro2
;
1573 current_dir
= current_buffer
->directory
;
1575 GCPRO2 (buffer
, current_dir
);
1577 current_dir
= Funhandled_file_name_directory (current_dir
);
1578 if (NILP (current_dir
))
1579 /* If the file name handler says that current_dir is unreachable, use
1580 a sensible default. */
1581 current_dir
= build_string ("~/");
1582 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1583 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1584 report_file_error ("Setting current directory",
1585 Fcons (current_buffer
->directory
, Qnil
));
1591 CHECK_STRING (name
);
1595 if (!NILP (program
))
1596 CHECK_STRING (program
);
1598 proc
= make_process (name
);
1599 /* If an error occurs and we can't start the process, we want to
1600 remove it from the process list. This means that each error
1601 check in create_process doesn't need to call remove_process
1602 itself; it's all taken care of here. */
1603 record_unwind_protect (start_process_unwind
, proc
);
1605 XPROCESS (proc
)->childp
= Qt
;
1606 XPROCESS (proc
)->plist
= Qnil
;
1607 XPROCESS (proc
)->type
= Qreal
;
1608 XPROCESS (proc
)->buffer
= buffer
;
1609 XPROCESS (proc
)->sentinel
= Qnil
;
1610 XPROCESS (proc
)->filter
= Qnil
;
1611 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1614 /* AKA GNUTLS_INITSTAGE(proc). */
1615 XPROCESS (proc
)->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
1616 XPROCESS (proc
)->gnutls_cred_type
= Qnil
;
1619 #ifdef ADAPTIVE_READ_BUFFERING
1620 XPROCESS (proc
)->adaptive_read_buffering
1621 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1622 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1625 /* Make the process marker point into the process buffer (if any). */
1626 if (BUFFERP (buffer
))
1627 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1628 BUF_ZV (XBUFFER (buffer
)),
1629 BUF_ZV_BYTE (XBUFFER (buffer
)));
1632 /* Decide coding systems for communicating with the process. Here
1633 we don't setup the structure coding_system nor pay attention to
1634 unibyte mode. They are done in create_process. */
1636 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1637 Lisp_Object coding_systems
= Qt
;
1638 Lisp_Object val
, *args2
;
1639 struct gcpro gcpro1
, gcpro2
;
1641 val
= Vcoding_system_for_read
;
1644 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1645 args2
[0] = Qstart_process
;
1646 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1647 GCPRO2 (proc
, current_dir
);
1648 if (!NILP (program
))
1649 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1651 if (CONSP (coding_systems
))
1652 val
= XCAR (coding_systems
);
1653 else if (CONSP (Vdefault_process_coding_system
))
1654 val
= XCAR (Vdefault_process_coding_system
);
1656 XPROCESS (proc
)->decode_coding_system
= val
;
1658 val
= Vcoding_system_for_write
;
1661 if (EQ (coding_systems
, Qt
))
1663 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1664 args2
[0] = Qstart_process
;
1665 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1666 GCPRO2 (proc
, current_dir
);
1667 if (!NILP (program
))
1668 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1671 if (CONSP (coding_systems
))
1672 val
= XCDR (coding_systems
);
1673 else if (CONSP (Vdefault_process_coding_system
))
1674 val
= XCDR (Vdefault_process_coding_system
);
1676 XPROCESS (proc
)->encode_coding_system
= val
;
1680 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1681 XPROCESS (proc
)->decoding_carryover
= 0;
1682 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1684 XPROCESS (proc
)->inherit_coding_system_flag
1685 = !(NILP (buffer
) || !inherit_process_coding_system
);
1687 if (!NILP (program
))
1689 /* If program file name is not absolute, search our path for it.
1690 Put the name we will really use in TEM. */
1691 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1692 && !(SCHARS (program
) > 1
1693 && IS_DEVICE_SEP (SREF (program
, 1))))
1695 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1698 GCPRO4 (name
, program
, buffer
, current_dir
);
1699 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1702 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1703 tem
= Fexpand_file_name (tem
, Qnil
);
1707 if (!NILP (Ffile_directory_p (program
)))
1708 error ("Specified program for new process is a directory");
1712 /* If program file name starts with /: for quoting a magic name,
1714 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1715 && SREF (tem
, 1) == ':')
1716 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1719 struct gcpro gcpro1
;
1722 /* Encode the file name and put it in NEW_ARGV.
1723 That's where the child will use it to execute the program. */
1724 tem
= Fcons (ENCODE_FILE (tem
), Qnil
);
1726 /* Here we encode arguments by the coding system used for sending
1727 data to the process. We don't support using different coding
1728 systems for encoding arguments and for encoding data sent to the
1731 for (i
= 3; i
< nargs
; i
++)
1733 tem
= Fcons (args
[i
], tem
);
1734 CHECK_STRING (XCAR (tem
));
1735 if (STRING_MULTIBYTE (XCAR (tem
)))
1737 code_convert_string_norecord
1738 (XCAR (tem
), XPROCESS (proc
)->encode_coding_system
, 1));
1744 /* Now that everything is encoded we can collect the strings into
1746 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1747 new_argv
[nargs
- 2] = 0;
1749 for (i
= nargs
- 3; i
>= 0; i
--)
1751 new_argv
[i
] = SDATA (XCAR (tem
));
1755 create_process (proc
, (char **) new_argv
, current_dir
);
1760 return unbind_to (count
, proc
);
1763 /* This function is the unwind_protect form for Fstart_process. If
1764 PROC doesn't have its pid set, then we know someone has signaled
1765 an error and the process wasn't started successfully, so we should
1766 remove it from the process list. */
1768 start_process_unwind (Lisp_Object proc
)
1770 if (!PROCESSP (proc
))
1773 /* Was PROC started successfully? */
1774 if (XPROCESS (proc
)->pid
== -1)
1775 remove_process (proc
);
1781 create_process_1 (struct atimer
*timer
)
1783 /* Nothing to do. */
1788 create_process (Lisp_Object process
, char **new_argv
, Lisp_Object current_dir
)
1790 int inchannel
, outchannel
;
1793 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1794 int wait_child_setup
[2];
1798 struct sigaction sigint_action
;
1799 struct sigaction sigquit_action
;
1801 struct sigaction sighup_action
;
1803 /* Use volatile to protect variables from being clobbered by longjmp. */
1804 volatile int forkin
, forkout
;
1805 volatile int pty_flag
= 0;
1807 extern char **environ
;
1810 inchannel
= outchannel
= -1;
1813 if (!NILP (Vprocess_connection_type
))
1814 outchannel
= inchannel
= allocate_pty ();
1818 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1819 /* On most USG systems it does not work to open the pty's tty here,
1820 then close it and reopen it in the child. */
1822 /* Don't let this terminal become our controlling terminal
1823 (in case we don't have one). */
1824 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1826 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1829 report_file_error ("Opening pty", Qnil
);
1831 forkin
= forkout
= -1;
1832 #endif /* not USG, or USG_SUBTTY_WORKS */
1836 #endif /* HAVE_PTYS */
1841 report_file_error ("Creating pipe", Qnil
);
1847 emacs_close (inchannel
);
1848 emacs_close (forkout
);
1849 report_file_error ("Creating pipe", Qnil
);
1855 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1859 tem
= pipe (wait_child_setup
);
1861 report_file_error ("Creating pipe", Qnil
);
1862 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1864 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1867 emacs_close (wait_child_setup
[0]);
1868 emacs_close (wait_child_setup
[1]);
1869 report_file_error ("Setting file descriptor flags", Qnil
);
1875 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1876 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1879 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1880 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1884 /* Record this as an active process, with its channels.
1885 As a result, child_setup will close Emacs's side of the pipes. */
1886 chan_process
[inchannel
] = process
;
1887 XPROCESS (process
)->infd
= inchannel
;
1888 XPROCESS (process
)->outfd
= outchannel
;
1890 /* Previously we recorded the tty descriptor used in the subprocess.
1891 It was only used for getting the foreground tty process, so now
1892 we just reopen the device (see emacs_get_tty_pgrp) as this is
1893 more portable (see USG_SUBTTY_WORKS above). */
1895 XPROCESS (process
)->pty_flag
= pty_flag
;
1896 XPROCESS (process
)->status
= Qrun
;
1897 setup_process_coding_systems (process
);
1899 /* Delay interrupts until we have a chance to store
1900 the new fork's pid in its process structure */
1901 sigemptyset (&blocked
);
1903 sigaddset (&blocked
, SIGCHLD
);
1905 #ifdef HAVE_WORKING_VFORK
1906 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1907 this sets the parent's signal handlers as well as the child's.
1908 So delay all interrupts whose handlers the child might munge,
1909 and record the current handlers so they can be restored later. */
1910 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1911 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1913 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1915 #endif /* HAVE_WORKING_VFORK */
1916 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1918 FD_SET (inchannel
, &input_wait_mask
);
1919 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1920 if (inchannel
> max_process_desc
)
1921 max_process_desc
= inchannel
;
1923 /* Until we store the proper pid, enable sigchld_handler
1924 to recognize an unknown pid as standing for this process.
1925 It is very important not to let this `marker' value stay
1926 in the table after this function has returned; if it does
1927 it might cause call-process to hang and subsequent asynchronous
1928 processes to get their return values scrambled. */
1929 XPROCESS (process
)->pid
= -1;
1934 /* child_setup must clobber environ on systems with true vfork.
1935 Protect it from permanent change. */
1936 char **save_environ
= environ
;
1938 current_dir
= ENCODE_FILE (current_dir
);
1943 #endif /* not WINDOWSNT */
1945 int xforkin
= forkin
;
1946 int xforkout
= forkout
;
1948 #if 0 /* This was probably a mistake--it duplicates code later on,
1949 but fails to handle all the cases. */
1950 /* Make sure SIGCHLD is not blocked in the child. */
1951 sigsetmask (SIGEMPTYMASK
);
1954 /* Make the pty be the controlling terminal of the process. */
1956 /* First, disconnect its current controlling terminal. */
1958 /* We tried doing setsid only if pty_flag, but it caused
1959 process_set_signal to fail on SGI when using a pipe. */
1961 /* Make the pty's terminal the controlling terminal. */
1962 if (pty_flag
&& xforkin
>= 0)
1965 /* We ignore the return value
1966 because faith@cs.unc.edu says that is necessary on Linux. */
1967 ioctl (xforkin
, TIOCSCTTY
, 0);
1970 #else /* not HAVE_SETSID */
1972 /* It's very important to call setpgrp here and no time
1973 afterwards. Otherwise, we lose our controlling tty which
1974 is set when we open the pty. */
1977 #endif /* not HAVE_SETSID */
1978 #if defined (LDISC1)
1979 if (pty_flag
&& xforkin
>= 0)
1982 tcgetattr (xforkin
, &t
);
1984 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1985 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1988 #if defined (NTTYDISC) && defined (TIOCSETD)
1989 if (pty_flag
&& xforkin
>= 0)
1991 /* Use new line discipline. */
1992 int ldisc
= NTTYDISC
;
1993 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1998 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1999 can do TIOCSPGRP only to the process's controlling tty. */
2002 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2003 I can't test it since I don't have 4.3. */
2004 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2007 ioctl (j
, TIOCNOTTY
, 0);
2011 /* In order to get a controlling terminal on some versions
2012 of BSD, it is necessary to put the process in pgrp 0
2013 before it opens the terminal. */
2021 #endif /* TIOCNOTTY */
2023 #if !defined (DONT_REOPEN_PTY)
2024 /*** There is a suggestion that this ought to be a
2025 conditional on TIOCSPGRP,
2026 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2027 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2028 that system does seem to need this code, even though
2029 both HAVE_SETSID and TIOCSCTTY are defined. */
2030 /* Now close the pty (if we had it open) and reopen it.
2031 This makes the pty the controlling terminal of the subprocess. */
2035 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2038 emacs_close (xforkin
);
2039 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2043 emacs_write (1, "Couldn't open the pty terminal ", 31);
2044 emacs_write (1, pty_name
, strlen (pty_name
));
2045 emacs_write (1, "\n", 1);
2050 #endif /* not DONT_REOPEN_PTY */
2052 #ifdef SETUP_SLAVE_PTY
2057 #endif /* SETUP_SLAVE_PTY */
2059 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2060 Now reenable it in the child, so it will die when we want it to. */
2062 signal (SIGHUP
, SIG_DFL
);
2064 #endif /* HAVE_PTYS */
2066 signal (SIGINT
, SIG_DFL
);
2067 signal (SIGQUIT
, SIG_DFL
);
2069 /* Stop blocking signals in the child. */
2070 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2073 child_setup_tty (xforkout
);
2075 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2076 new_argv
, 1, current_dir
);
2077 #else /* not WINDOWSNT */
2079 emacs_close (wait_child_setup
[0]);
2081 child_setup (xforkin
, xforkout
, xforkout
,
2082 new_argv
, 1, current_dir
);
2083 #endif /* not WINDOWSNT */
2085 environ
= save_environ
;
2090 /* This runs in the Emacs process. */
2094 emacs_close (forkin
);
2095 if (forkin
!= forkout
&& forkout
>= 0)
2096 emacs_close (forkout
);
2100 /* vfork succeeded. */
2101 XPROCESS (process
)->pid
= pid
;
2104 register_child (pid
, inchannel
);
2105 #endif /* WINDOWSNT */
2107 /* If the subfork execv fails, and it exits,
2108 this close hangs. I don't know why.
2109 So have an interrupt jar it loose. */
2111 struct atimer
*timer
;
2115 EMACS_SET_SECS_USECS (offset
, 1, 0);
2116 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2119 emacs_close (forkin
);
2121 cancel_atimer (timer
);
2125 if (forkin
!= forkout
&& forkout
>= 0)
2126 emacs_close (forkout
);
2130 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2133 XPROCESS (process
)->tty_name
= Qnil
;
2135 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2136 /* Wait for child_setup to complete in case that vfork is
2137 actually defined as fork. The descriptor wait_child_setup[1]
2138 of a pipe is closed at the child side either by close-on-exec
2139 on successful execvp or the _exit call in child_setup. */
2143 emacs_close (wait_child_setup
[1]);
2144 emacs_read (wait_child_setup
[0], &dummy
, 1);
2145 emacs_close (wait_child_setup
[0]);
2150 /* Restore the signal state whether vfork succeeded or not.
2151 (We will signal an error, below, if it failed.) */
2152 #ifdef HAVE_WORKING_VFORK
2153 /* Restore the parent's signal handlers. */
2154 sigaction (SIGINT
, &sigint_action
, 0);
2155 sigaction (SIGQUIT
, &sigquit_action
, 0);
2157 sigaction (SIGHUP
, &sighup_action
, 0);
2159 #endif /* HAVE_WORKING_VFORK */
2160 /* Stop blocking signals in the parent. */
2161 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2163 /* Now generate the error if vfork failed. */
2165 report_file_error ("Doing vfork", Qnil
);
2169 create_pty (Lisp_Object process
)
2171 int inchannel
, outchannel
;
2173 /* Use volatile to protect variables from being clobbered by longjmp. */
2174 volatile int forkin
, forkout
;
2175 volatile int pty_flag
= 0;
2177 inchannel
= outchannel
= -1;
2180 if (!NILP (Vprocess_connection_type
))
2181 outchannel
= inchannel
= allocate_pty ();
2185 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2186 /* On most USG systems it does not work to open the pty's tty here,
2187 then close it and reopen it in the child. */
2189 /* Don't let this terminal become our controlling terminal
2190 (in case we don't have one). */
2191 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
2193 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
2196 report_file_error ("Opening pty", Qnil
);
2197 #if defined (DONT_REOPEN_PTY)
2198 /* In the case that vfork is defined as fork, the parent process
2199 (Emacs) may send some data before the child process completes
2200 tty options setup. So we setup tty before forking. */
2201 child_setup_tty (forkout
);
2202 #endif /* DONT_REOPEN_PTY */
2204 forkin
= forkout
= -1;
2205 #endif /* not USG, or USG_SUBTTY_WORKS */
2208 #endif /* HAVE_PTYS */
2211 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
2212 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
2215 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
2216 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
2220 /* Record this as an active process, with its channels.
2221 As a result, child_setup will close Emacs's side of the pipes. */
2222 chan_process
[inchannel
] = process
;
2223 XPROCESS (process
)->infd
= inchannel
;
2224 XPROCESS (process
)->outfd
= outchannel
;
2226 /* Previously we recorded the tty descriptor used in the subprocess.
2227 It was only used for getting the foreground tty process, so now
2228 we just reopen the device (see emacs_get_tty_pgrp) as this is
2229 more portable (see USG_SUBTTY_WORKS above). */
2231 XPROCESS (process
)->pty_flag
= pty_flag
;
2232 XPROCESS (process
)->status
= Qrun
;
2233 setup_process_coding_systems (process
);
2235 FD_SET (inchannel
, &input_wait_mask
);
2236 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2237 if (inchannel
> max_process_desc
)
2238 max_process_desc
= inchannel
;
2240 XPROCESS (process
)->pid
= -2;
2243 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2246 XPROCESS (process
)->tty_name
= Qnil
;
2250 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2251 The address family of sa is not included in the result. */
2254 conv_sockaddr_to_lisp (struct sockaddr
*sa
, int len
)
2256 Lisp_Object address
;
2259 register struct Lisp_Vector
*p
;
2261 /* Workaround for a bug in getsockname on BSD: Names bound to
2262 sockets in the UNIX domain are inaccessible; getsockname returns
2263 a zero length name. */
2264 if (len
< offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
))
2265 return empty_unibyte_string
;
2267 switch (sa
->sa_family
)
2271 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2272 len
= sizeof (sin
->sin_addr
) + 1;
2273 address
= Fmake_vector (make_number (len
), Qnil
);
2274 p
= XVECTOR (address
);
2275 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2276 cp
= (unsigned char *) &sin
->sin_addr
;
2282 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2283 uint16_t *ip6
= (uint16_t *) &sin6
->sin6_addr
;
2284 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2285 address
= Fmake_vector (make_number (len
), Qnil
);
2286 p
= XVECTOR (address
);
2287 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2288 for (i
= 0; i
< len
; i
++)
2289 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2293 #ifdef HAVE_LOCAL_SOCKETS
2296 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2297 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2298 if (sockun
->sun_path
[i
] == 0)
2300 return make_unibyte_string (sockun
->sun_path
, i
);
2304 len
-= offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
);
2305 address
= Fcons (make_number (sa
->sa_family
),
2306 Fmake_vector (make_number (len
), Qnil
));
2307 p
= XVECTOR (XCDR (address
));
2308 cp
= (unsigned char *) &sa
->sa_family
+ sizeof (sa
->sa_family
);
2314 p
->contents
[i
++] = make_number (*cp
++);
2320 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2323 get_lisp_to_sockaddr_size (Lisp_Object address
, int *familyp
)
2325 register struct Lisp_Vector
*p
;
2327 if (VECTORP (address
))
2329 p
= XVECTOR (address
);
2333 return sizeof (struct sockaddr_in
);
2336 else if (p
->size
== 9)
2338 *familyp
= AF_INET6
;
2339 return sizeof (struct sockaddr_in6
);
2343 #ifdef HAVE_LOCAL_SOCKETS
2344 else if (STRINGP (address
))
2346 *familyp
= AF_LOCAL
;
2347 return sizeof (struct sockaddr_un
);
2350 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2352 struct sockaddr
*sa
;
2353 *familyp
= XINT (XCAR (address
));
2354 p
= XVECTOR (XCDR (address
));
2355 return p
->size
+ sizeof (sa
->sa_family
);
2360 /* Convert an address object (vector or string) to an internal sockaddr.
2362 The address format has been basically validated by
2363 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2364 it could have come from user data. So if FAMILY is not valid,
2365 we return after zeroing *SA. */
2368 conv_lisp_to_sockaddr (int family
, Lisp_Object address
, struct sockaddr
*sa
, int len
)
2370 register struct Lisp_Vector
*p
;
2371 register unsigned char *cp
= NULL
;
2374 memset (sa
, 0, len
);
2376 if (VECTORP (address
))
2378 p
= XVECTOR (address
);
2379 if (family
== AF_INET
)
2381 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2382 len
= sizeof (sin
->sin_addr
) + 1;
2383 i
= XINT (p
->contents
[--len
]);
2384 sin
->sin_port
= htons (i
);
2385 cp
= (unsigned char *)&sin
->sin_addr
;
2386 sa
->sa_family
= family
;
2389 else if (family
== AF_INET6
)
2391 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2392 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2393 len
= sizeof (sin6
->sin6_addr
) + 1;
2394 i
= XINT (p
->contents
[--len
]);
2395 sin6
->sin6_port
= htons (i
);
2396 for (i
= 0; i
< len
; i
++)
2397 if (INTEGERP (p
->contents
[i
]))
2399 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2402 sa
->sa_family
= family
;
2409 else if (STRINGP (address
))
2411 #ifdef HAVE_LOCAL_SOCKETS
2412 if (family
== AF_LOCAL
)
2414 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2415 cp
= SDATA (address
);
2416 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2417 sockun
->sun_path
[i
] = *cp
++;
2418 sa
->sa_family
= family
;
2425 p
= XVECTOR (XCDR (address
));
2426 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2429 for (i
= 0; i
< len
; i
++)
2430 if (INTEGERP (p
->contents
[i
]))
2431 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2434 #ifdef DATAGRAM_SOCKETS
2435 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2437 doc
: /* Get the current datagram address associated with PROCESS. */)
2438 (Lisp_Object process
)
2442 CHECK_PROCESS (process
);
2444 if (!DATAGRAM_CONN_P (process
))
2447 channel
= XPROCESS (process
)->infd
;
2448 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2449 datagram_address
[channel
].len
);
2452 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2454 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2455 Returns nil upon error setting address, ADDRESS otherwise. */)
2456 (Lisp_Object process
, Lisp_Object address
)
2461 CHECK_PROCESS (process
);
2463 if (!DATAGRAM_CONN_P (process
))
2466 channel
= XPROCESS (process
)->infd
;
2468 len
= get_lisp_to_sockaddr_size (address
, &family
);
2469 if (datagram_address
[channel
].len
!= len
)
2471 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2477 static const struct socket_options
{
2478 /* The name of this option. Should be lowercase version of option
2479 name without SO_ prefix. */
2481 /* Option level SOL_... */
2483 /* Option number SO_... */
2485 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2486 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2487 } socket_options
[] =
2489 #ifdef SO_BINDTODEVICE
2490 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2493 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2496 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2499 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2502 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2505 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2508 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2511 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2513 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2516 /* Set option OPT to value VAL on socket S.
2518 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2519 Signals an error if setting a known option fails.
2523 set_socket_option (int s
, Lisp_Object opt
, Lisp_Object val
)
2526 const struct socket_options
*sopt
;
2531 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2532 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2533 if (strcmp (name
, sopt
->name
) == 0)
2536 switch (sopt
->opttype
)
2541 optval
= NILP (val
) ? 0 : 1;
2542 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2543 &optval
, sizeof (optval
));
2551 optval
= XINT (val
);
2553 error ("Bad option value for %s", name
);
2554 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2555 &optval
, sizeof (optval
));
2559 #ifdef SO_BINDTODEVICE
2562 char devname
[IFNAMSIZ
+1];
2564 /* This is broken, at least in the Linux 2.4 kernel.
2565 To unbind, the arg must be a zero integer, not the empty string.
2566 This should work on all systems. KFS. 2003-09-23. */
2567 memset (devname
, 0, sizeof devname
);
2570 char *arg
= (char *) SDATA (val
);
2571 int len
= min (strlen (arg
), IFNAMSIZ
);
2572 memcpy (devname
, arg
, len
);
2574 else if (!NILP (val
))
2575 error ("Bad option value for %s", name
);
2576 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2585 struct linger linger
;
2588 linger
.l_linger
= 0;
2590 linger
.l_linger
= XINT (val
);
2592 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2593 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2594 &linger
, sizeof (linger
));
2604 report_file_error ("Cannot set network option",
2605 Fcons (opt
, Fcons (val
, Qnil
)));
2606 return (1 << sopt
->optbit
);
2610 DEFUN ("set-network-process-option",
2611 Fset_network_process_option
, Sset_network_process_option
,
2613 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2614 See `make-network-process' for a list of options and values.
2615 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2616 OPTION is not a supported option, return nil instead; otherwise return t. */)
2617 (Lisp_Object process
, Lisp_Object option
, Lisp_Object value
, Lisp_Object no_error
)
2620 struct Lisp_Process
*p
;
2622 CHECK_PROCESS (process
);
2623 p
= XPROCESS (process
);
2624 if (!NETCONN1_P (p
))
2625 error ("Process is not a network process");
2629 error ("Process is not running");
2631 if (set_socket_option (s
, option
, value
))
2633 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2637 if (NILP (no_error
))
2638 error ("Unknown or unsupported option");
2644 DEFUN ("serial-process-configure",
2645 Fserial_process_configure
,
2646 Sserial_process_configure
,
2648 doc
: /* Configure speed, bytesize, etc. of a serial process.
2650 Arguments are specified as keyword/argument pairs. Attributes that
2651 are not given are re-initialized from the process's current
2652 configuration (available via the function `process-contact') or set to
2653 reasonable default values. The following arguments are defined:
2659 -- Any of these arguments can be given to identify the process that is
2660 to be configured. If none of these arguments is given, the current
2661 buffer's process is used.
2663 :speed SPEED -- SPEED is the speed of the serial port in bits per
2664 second, also called baud rate. Any value can be given for SPEED, but
2665 most serial ports work only at a few defined values between 1200 and
2666 115200, with 9600 being the most common value. If SPEED is nil, the
2667 serial port is not configured any further, i.e., all other arguments
2668 are ignored. This may be useful for special serial ports such as
2669 Bluetooth-to-serial converters which can only be configured through AT
2670 commands. A value of nil for SPEED can be used only when passed
2671 through `make-serial-process' or `serial-term'.
2673 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2674 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2676 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2677 `odd' (use odd parity), or the symbol `even' (use even parity). If
2678 PARITY is not given, no parity is used.
2680 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2681 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2682 is not given or nil, 1 stopbit is used.
2684 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2685 flowcontrol to be used, which is either nil (don't use flowcontrol),
2686 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2687 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2688 flowcontrol is used.
2690 `serial-process-configure' is called by `make-serial-process' for the
2691 initial configuration of the serial port.
2695 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2697 \(serial-process-configure
2698 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2700 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2702 usage: (serial-process-configure &rest ARGS) */)
2703 (int nargs
, Lisp_Object
*args
)
2705 struct Lisp_Process
*p
;
2706 Lisp_Object contact
= Qnil
;
2707 Lisp_Object proc
= Qnil
;
2708 struct gcpro gcpro1
;
2710 contact
= Flist (nargs
, args
);
2713 proc
= Fplist_get (contact
, QCprocess
);
2715 proc
= Fplist_get (contact
, QCname
);
2717 proc
= Fplist_get (contact
, QCbuffer
);
2719 proc
= Fplist_get (contact
, QCport
);
2720 proc
= get_process (proc
);
2721 p
= XPROCESS (proc
);
2722 if (!EQ (p
->type
, Qserial
))
2723 error ("Not a serial process");
2725 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2731 serial_configure (p
, contact
);
2737 /* Used by make-serial-process to recover from errors. */
2738 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2740 if (!PROCESSP (proc
))
2742 remove_process (proc
);
2746 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2748 doc
: /* Create and return a serial port process.
2750 In Emacs, serial port connections are represented by process objects,
2751 so input and output work as for subprocesses, and `delete-process'
2752 closes a serial port connection. However, a serial process has no
2753 process id, it cannot be signaled, and the status codes are different
2754 from normal processes.
2756 `make-serial-process' creates a process and a buffer, on which you
2757 probably want to use `process-send-string'. Try \\[serial-term] for
2758 an interactive terminal. See below for examples.
2760 Arguments are specified as keyword/argument pairs. The following
2761 arguments are defined:
2763 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2764 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2765 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2766 the backslashes in strings).
2768 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2769 which is called by `make-serial-process'.
2771 :name NAME -- NAME is the name of the process. If NAME is not given,
2772 the value of PORT is used.
2774 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2775 with the process. Process output goes at the end of that buffer,
2776 unless you specify an output stream or filter function to handle the
2777 output. If BUFFER is not given, the value of NAME is used.
2779 :coding CODING -- If CODING is a symbol, it specifies the coding
2780 system used for both reading and writing for this process. If CODING
2781 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2782 ENCODING is used for writing.
2784 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2785 the process is running. If BOOL is not given, query before exiting.
2787 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2788 In the stopped state, a serial process does not accept incoming data,
2789 but you can send outgoing data. The stopped state is cleared by
2790 `continue-process' and set by `stop-process'.
2792 :filter FILTER -- Install FILTER as the process filter.
2794 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2796 :plist PLIST -- Install PLIST as the initial plist of the process.
2803 -- These arguments are handled by `serial-process-configure', which is
2804 called by `make-serial-process'.
2806 The original argument list, possibly modified by later configuration,
2807 is available via the function `process-contact'.
2811 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2813 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2815 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2817 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2819 usage: (make-serial-process &rest ARGS) */)
2820 (int nargs
, Lisp_Object
*args
)
2823 Lisp_Object proc
, contact
, port
;
2824 struct Lisp_Process
*p
;
2825 struct gcpro gcpro1
;
2826 Lisp_Object name
, buffer
;
2827 Lisp_Object tem
, val
;
2828 int specpdl_count
= -1;
2833 contact
= Flist (nargs
, args
);
2836 port
= Fplist_get (contact
, QCport
);
2838 error ("No port specified");
2839 CHECK_STRING (port
);
2841 if (NILP (Fplist_member (contact
, QCspeed
)))
2842 error (":speed not specified");
2843 if (!NILP (Fplist_get (contact
, QCspeed
)))
2844 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2846 name
= Fplist_get (contact
, QCname
);
2849 CHECK_STRING (name
);
2850 proc
= make_process (name
);
2851 specpdl_count
= SPECPDL_INDEX ();
2852 record_unwind_protect (make_serial_process_unwind
, proc
);
2853 p
= XPROCESS (proc
);
2855 fd
= serial_open ((char*) SDATA (port
));
2858 if (fd
> max_process_desc
)
2859 max_process_desc
= fd
;
2860 chan_process
[fd
] = proc
;
2862 buffer
= Fplist_get (contact
, QCbuffer
);
2865 buffer
= Fget_buffer_create (buffer
);
2868 p
->childp
= contact
;
2869 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2871 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2872 p
->filter
= Fplist_get (contact
, QCfilter
);
2874 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2875 p
->kill_without_query
= 1;
2876 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2880 if (!EQ (p
->command
, Qt
))
2882 FD_SET (fd
, &input_wait_mask
);
2883 FD_SET (fd
, &non_keyboard_wait_mask
);
2886 if (BUFFERP (buffer
))
2888 set_marker_both (p
->mark
, buffer
,
2889 BUF_ZV (XBUFFER (buffer
)),
2890 BUF_ZV_BYTE (XBUFFER (buffer
)));
2893 tem
= Fplist_member (contact
, QCcoding
);
2894 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2900 val
= XCAR (XCDR (tem
));
2904 else if (!NILP (Vcoding_system_for_read
))
2905 val
= Vcoding_system_for_read
;
2906 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2907 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2909 p
->decode_coding_system
= val
;
2914 val
= XCAR (XCDR (tem
));
2918 else if (!NILP (Vcoding_system_for_write
))
2919 val
= Vcoding_system_for_write
;
2920 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2921 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2923 p
->encode_coding_system
= val
;
2925 setup_process_coding_systems (proc
);
2926 p
->decoding_buf
= make_uninit_string (0);
2927 p
->decoding_carryover
= 0;
2928 p
->encoding_buf
= make_uninit_string (0);
2929 p
->inherit_coding_system_flag
2930 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2932 Fserial_process_configure (nargs
, args
);
2934 specpdl_ptr
= specpdl
+ specpdl_count
;
2940 /* Create a network stream/datagram client/server process. Treated
2941 exactly like a normal process when reading and writing. Primary
2942 differences are in status display and process deletion. A network
2943 connection has no PID; you cannot signal it. All you can do is
2944 stop/continue it and deactivate/close it via delete-process */
2946 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2948 doc
: /* Create and return a network server or client process.
2950 In Emacs, network connections are represented by process objects, so
2951 input and output work as for subprocesses and `delete-process' closes
2952 a network connection. However, a network process has no process id,
2953 it cannot be signaled, and the status codes are different from normal
2956 Arguments are specified as keyword/argument pairs. The following
2957 arguments are defined:
2959 :name NAME -- NAME is name for process. It is modified if necessary
2962 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2963 with the process. Process output goes at end of that buffer, unless
2964 you specify an output stream or filter function to handle the output.
2965 BUFFER may be also nil, meaning that this process is not associated
2968 :host HOST -- HOST is name of the host to connect to, or its IP
2969 address. The symbol `local' specifies the local host. If specified
2970 for a server process, it must be a valid name or address for the local
2971 host, and only clients connecting to that address will be accepted.
2973 :service SERVICE -- SERVICE is name of the service desired, or an
2974 integer specifying a port number to connect to. If SERVICE is t,
2975 a random port number is selected for the server. (If Emacs was
2976 compiled with getaddrinfo, a port number can also be specified as a
2977 string, e.g. "80", as well as an integer. This is not portable.)
2979 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2980 stream type connection, `datagram' creates a datagram type connection,
2981 `seqpacket' creates a reliable datagram connection.
2983 :family FAMILY -- FAMILY is the address (and protocol) family for the
2984 service specified by HOST and SERVICE. The default (nil) is to use
2985 whatever address family (IPv4 or IPv6) that is defined for the host
2986 and port number specified by HOST and SERVICE. Other address families
2988 local -- for a local (i.e. UNIX) address specified by SERVICE.
2989 ipv4 -- use IPv4 address family only.
2990 ipv6 -- use IPv6 address family only.
2992 :local ADDRESS -- ADDRESS is the local address used for the connection.
2993 This parameter is ignored when opening a client process. When specified
2994 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2996 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2997 connection. This parameter is ignored when opening a stream server
2998 process. For a datagram server process, it specifies the initial
2999 setting of the remote datagram address. When specified for a client
3000 process, the FAMILY, HOST, and SERVICE args are ignored.
3002 The format of ADDRESS depends on the address family:
3003 - An IPv4 address is represented as an vector of integers [A B C D P]
3004 corresponding to numeric IP address A.B.C.D and port number P.
3005 - A local address is represented as a string with the address in the
3006 local address space.
3007 - An "unsupported family" address is represented by a cons (F . AV)
3008 where F is the family number and AV is a vector containing the socket
3009 address data with one element per address data byte. Do not rely on
3010 this format in portable code, as it may depend on implementation
3011 defined constants, data sizes, and data structure alignment.
3013 :coding CODING -- If CODING is a symbol, it specifies the coding
3014 system used for both reading and writing for this process. If CODING
3015 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3016 ENCODING is used for writing.
3018 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3019 return without waiting for the connection to complete; instead, the
3020 sentinel function will be called with second arg matching "open" (if
3021 successful) or "failed" when the connect completes. Default is to use
3022 a blocking connect (i.e. wait) for stream type connections.
3024 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3025 running when Emacs is exited.
3027 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3028 In the stopped state, a server process does not accept new
3029 connections, and a client process does not handle incoming traffic.
3030 The stopped state is cleared by `continue-process' and set by
3033 :filter FILTER -- Install FILTER as the process filter.
3035 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3036 process filter are multibyte, otherwise they are unibyte.
3037 If this keyword is not specified, the strings are multibyte if
3038 `default-enable-multibyte-characters' is non-nil.
3040 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3042 :log LOG -- Install LOG as the server process log function. This
3043 function is called when the server accepts a network connection from a
3044 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3045 is the server process, CLIENT is the new process for the connection,
3046 and MESSAGE is a string.
3048 :plist PLIST -- Install PLIST as the new process' initial plist.
3050 :server QLEN -- if QLEN is non-nil, create a server process for the
3051 specified FAMILY, SERVICE, and connection type (stream or datagram).
3052 If QLEN is an integer, it is used as the max. length of the server's
3053 pending connection queue (also known as the backlog); the default
3054 queue length is 5. Default is to create a client process.
3056 The following network options can be specified for this connection:
3058 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3059 :dontroute BOOL -- Only send to directly connected hosts.
3060 :keepalive BOOL -- Send keep-alive messages on network stream.
3061 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3062 :oobinline BOOL -- Place out-of-band data in receive data stream.
3063 :priority INT -- Set protocol defined priority for sent packets.
3064 :reuseaddr BOOL -- Allow reusing a recently used local address
3065 (this is allowed by default for a server process).
3066 :bindtodevice NAME -- bind to interface NAME. Using this may require
3067 special privileges on some systems.
3069 Consult the relevant system programmer's manual pages for more
3070 information on using these options.
3073 A server process will listen for and accept connections from clients.
3074 When a client connection is accepted, a new network process is created
3075 for the connection with the following parameters:
3077 - The client's process name is constructed by concatenating the server
3078 process' NAME and a client identification string.
3079 - If the FILTER argument is non-nil, the client process will not get a
3080 separate process buffer; otherwise, the client's process buffer is a newly
3081 created buffer named after the server process' BUFFER name or process
3082 NAME concatenated with the client identification string.
3083 - The connection type and the process filter and sentinel parameters are
3084 inherited from the server process' TYPE, FILTER and SENTINEL.
3085 - The client process' contact info is set according to the client's
3086 addressing information (typically an IP address and a port number).
3087 - The client process' plist is initialized from the server's plist.
3089 Notice that the FILTER and SENTINEL args are never used directly by
3090 the server process. Also, the BUFFER argument is not used directly by
3091 the server process, but via the optional :log function, accepted (and
3092 failed) connections may be logged in the server process' buffer.
3094 The original argument list, modified with the actual connection
3095 information, is available via the `process-contact' function.
3097 usage: (make-network-process &rest ARGS) */)
3098 (int nargs
, Lisp_Object
*args
)
3101 Lisp_Object contact
;
3102 struct Lisp_Process
*p
;
3103 #ifdef HAVE_GETADDRINFO
3104 struct addrinfo ai
, *res
, *lres
;
3105 struct addrinfo hints
;
3106 const char *portstring
;
3108 #else /* HAVE_GETADDRINFO */
3109 struct _emacs_addrinfo
3115 struct sockaddr
*ai_addr
;
3116 struct _emacs_addrinfo
*ai_next
;
3118 #endif /* HAVE_GETADDRINFO */
3119 struct sockaddr_in address_in
;
3120 #ifdef HAVE_LOCAL_SOCKETS
3121 struct sockaddr_un address_un
;
3126 int s
= -1, outch
, inch
;
3127 struct gcpro gcpro1
;
3128 int count
= SPECPDL_INDEX ();
3130 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3132 Lisp_Object name
, buffer
, host
, service
, address
;
3133 Lisp_Object filter
, sentinel
;
3134 int is_non_blocking_client
= 0;
3135 int is_server
= 0, backlog
= 5;
3142 /* Save arguments for process-contact and clone-process. */
3143 contact
= Flist (nargs
, args
);
3147 /* Ensure socket support is loaded if available. */
3148 init_winsock (TRUE
);
3151 /* :type TYPE (nil: stream, datagram */
3152 tem
= Fplist_get (contact
, QCtype
);
3154 socktype
= SOCK_STREAM
;
3155 #ifdef DATAGRAM_SOCKETS
3156 else if (EQ (tem
, Qdatagram
))
3157 socktype
= SOCK_DGRAM
;
3159 #ifdef HAVE_SEQPACKET
3160 else if (EQ (tem
, Qseqpacket
))
3161 socktype
= SOCK_SEQPACKET
;
3164 error ("Unsupported connection type");
3167 tem
= Fplist_get (contact
, QCserver
);
3170 /* Don't support network sockets when non-blocking mode is
3171 not available, since a blocked Emacs is not useful. */
3172 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3173 error ("Network servers not supported");
3177 backlog
= XINT (tem
);
3181 /* Make QCaddress an alias for :local (server) or :remote (client). */
3182 QCaddress
= is_server
? QClocal
: QCremote
;
3185 if (!is_server
&& socktype
!= SOCK_DGRAM
3186 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3188 #ifndef NON_BLOCKING_CONNECT
3189 error ("Non-blocking connect not supported");
3191 is_non_blocking_client
= 1;
3195 name
= Fplist_get (contact
, QCname
);
3196 buffer
= Fplist_get (contact
, QCbuffer
);
3197 filter
= Fplist_get (contact
, QCfilter
);
3198 sentinel
= Fplist_get (contact
, QCsentinel
);
3200 CHECK_STRING (name
);
3202 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3203 ai
.ai_socktype
= socktype
;
3208 /* :local ADDRESS or :remote ADDRESS */
3209 address
= Fplist_get (contact
, QCaddress
);
3210 if (!NILP (address
))
3212 host
= service
= Qnil
;
3214 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3215 error ("Malformed :address");
3216 ai
.ai_family
= family
;
3217 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3218 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3222 /* :family FAMILY -- nil (for Inet), local, or integer. */
3223 tem
= Fplist_get (contact
, QCfamily
);
3226 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3232 #ifdef HAVE_LOCAL_SOCKETS
3233 else if (EQ (tem
, Qlocal
))
3237 else if (EQ (tem
, Qipv6
))
3240 else if (EQ (tem
, Qipv4
))
3242 else if (INTEGERP (tem
))
3243 family
= XINT (tem
);
3245 error ("Unknown address family");
3247 ai
.ai_family
= family
;
3249 /* :service SERVICE -- string, integer (port number), or t (random port). */
3250 service
= Fplist_get (contact
, QCservice
);
3252 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3253 host
= Fplist_get (contact
, QChost
);
3256 if (EQ (host
, Qlocal
))
3257 /* Depending on setup, "localhost" may map to different IPv4 and/or
3258 IPv6 addresses, so it's better to be explicit. (Bug#6781) */
3259 host
= build_string ("127.0.0.1");
3260 CHECK_STRING (host
);
3263 #ifdef HAVE_LOCAL_SOCKETS
3264 if (family
== AF_LOCAL
)
3268 message (":family local ignores the :host \"%s\" property",
3270 contact
= Fplist_put (contact
, QChost
, Qnil
);
3273 CHECK_STRING (service
);
3274 memset (&address_un
, 0, sizeof address_un
);
3275 address_un
.sun_family
= AF_LOCAL
;
3276 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3277 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3278 ai
.ai_addrlen
= sizeof address_un
;
3283 /* Slow down polling to every ten seconds.
3284 Some kernels have a bug which causes retrying connect to fail
3285 after a connect. Polling can interfere with gethostbyname too. */
3286 #ifdef POLL_FOR_INPUT
3287 if (socktype
!= SOCK_DGRAM
)
3289 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3290 bind_polling_period (10);
3294 #ifdef HAVE_GETADDRINFO
3295 /* If we have a host, use getaddrinfo to resolve both host and service.
3296 Otherwise, use getservbyname to lookup the service. */
3300 /* SERVICE can either be a string or int.
3301 Convert to a C string for later use by getaddrinfo. */
3302 if (EQ (service
, Qt
))
3304 else if (INTEGERP (service
))
3306 sprintf (portbuf
, "%ld", (long) XINT (service
));
3307 portstring
= portbuf
;
3311 CHECK_STRING (service
);
3312 portstring
= SDATA (service
);
3317 memset (&hints
, 0, sizeof (hints
));
3319 hints
.ai_family
= family
;
3320 hints
.ai_socktype
= socktype
;
3321 hints
.ai_protocol
= 0;
3323 #ifdef HAVE_RES_INIT
3327 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3329 #ifdef HAVE_GAI_STRERROR
3330 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror (ret
));
3332 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3338 #endif /* HAVE_GETADDRINFO */
3340 /* We end up here if getaddrinfo is not defined, or in case no hostname
3341 has been specified (e.g. for a local server process). */
3343 if (EQ (service
, Qt
))
3345 else if (INTEGERP (service
))
3346 port
= htons ((unsigned short) XINT (service
));
3349 struct servent
*svc_info
;
3350 CHECK_STRING (service
);
3351 svc_info
= getservbyname (SDATA (service
),
3352 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3354 error ("Unknown service: %s", SDATA (service
));
3355 port
= svc_info
->s_port
;
3358 memset (&address_in
, 0, sizeof address_in
);
3359 address_in
.sin_family
= family
;
3360 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3361 address_in
.sin_port
= port
;
3363 #ifndef HAVE_GETADDRINFO
3366 struct hostent
*host_info_ptr
;
3368 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3369 as it may `hang' Emacs for a very long time. */
3373 #ifdef HAVE_RES_INIT
3377 host_info_ptr
= gethostbyname (SDATA (host
));
3382 memcpy (&address_in
.sin_addr
, host_info_ptr
->h_addr
,
3383 host_info_ptr
->h_length
);
3384 family
= host_info_ptr
->h_addrtype
;
3385 address_in
.sin_family
= family
;
3388 /* Attempt to interpret host as numeric inet address */
3390 unsigned long numeric_addr
;
3391 numeric_addr
= inet_addr ((char *) SDATA (host
));
3392 if (numeric_addr
== -1)
3393 error ("Unknown host \"%s\"", SDATA (host
));
3395 memcpy (&address_in
.sin_addr
, &numeric_addr
,
3396 sizeof (address_in
.sin_addr
));
3400 #endif /* not HAVE_GETADDRINFO */
3402 ai
.ai_family
= family
;
3403 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3404 ai
.ai_addrlen
= sizeof address_in
;
3408 /* Do this in case we never enter the for-loop below. */
3409 count1
= SPECPDL_INDEX ();
3412 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3418 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3425 #ifdef DATAGRAM_SOCKETS
3426 if (!is_server
&& socktype
== SOCK_DGRAM
)
3428 #endif /* DATAGRAM_SOCKETS */
3430 #ifdef NON_BLOCKING_CONNECT
3431 if (is_non_blocking_client
)
3434 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3436 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3448 /* Make us close S if quit. */
3449 record_unwind_protect (close_file_unwind
, make_number (s
));
3451 /* Parse network options in the arg list.
3452 We simply ignore anything which isn't a known option (including other keywords).
3453 An error is signaled if setting a known option fails. */
3454 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3455 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3459 /* Configure as a server socket. */
3461 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3462 explicit :reuseaddr key to override this. */
3463 #ifdef HAVE_LOCAL_SOCKETS
3464 if (family
!= AF_LOCAL
)
3466 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3469 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3470 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3473 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3474 report_file_error ("Cannot bind server socket", Qnil
);
3476 #ifdef HAVE_GETSOCKNAME
3477 if (EQ (service
, Qt
))
3479 struct sockaddr_in sa1
;
3480 int len1
= sizeof (sa1
);
3481 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3483 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3484 service
= make_number (ntohs (sa1
.sin_port
));
3485 contact
= Fplist_put (contact
, QCservice
, service
);
3490 if (socktype
!= SOCK_DGRAM
&& listen (s
, backlog
))
3491 report_file_error ("Cannot listen on server socket", Qnil
);
3499 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3502 if (ret
== 0 || xerrno
== EISCONN
)
3504 /* The unwind-protect will be discarded afterwards.
3505 Likewise for immediate_quit. */
3509 #ifdef NON_BLOCKING_CONNECT
3511 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3515 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3522 if (xerrno
== EINTR
)
3524 /* Unlike most other syscalls connect() cannot be called
3525 again. (That would return EALREADY.) The proper way to
3526 wait for completion is select(). */
3533 sc
= select (s
+ 1, (SELECT_TYPE
*)0, &fdset
, (SELECT_TYPE
*)0,
3540 report_file_error ("select failed", Qnil
);
3544 len
= sizeof xerrno
;
3545 eassert (FD_ISSET (s
, &fdset
));
3546 if (getsockopt (s
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &len
) == -1)
3547 report_file_error ("getsockopt failed", Qnil
);
3549 errno
= xerrno
, report_file_error ("error during connect", Qnil
);
3553 #endif /* !WINDOWSNT */
3557 /* Discard the unwind protect closing S. */
3558 specpdl_ptr
= specpdl
+ count1
;
3563 if (xerrno
== EINTR
)
3570 #ifdef DATAGRAM_SOCKETS
3571 if (socktype
== SOCK_DGRAM
)
3573 if (datagram_address
[s
].sa
)
3575 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3576 datagram_address
[s
].len
= lres
->ai_addrlen
;
3580 memset (datagram_address
[s
].sa
, 0, lres
->ai_addrlen
);
3581 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3584 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3585 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3586 conv_lisp_to_sockaddr (rfamily
, remote
,
3587 datagram_address
[s
].sa
, rlen
);
3591 memcpy (datagram_address
[s
].sa
, lres
->ai_addr
, lres
->ai_addrlen
);
3594 contact
= Fplist_put (contact
, QCaddress
,
3595 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3596 #ifdef HAVE_GETSOCKNAME
3599 struct sockaddr_in sa1
;
3600 int len1
= sizeof (sa1
);
3601 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3602 contact
= Fplist_put (contact
, QClocal
,
3603 conv_sockaddr_to_lisp ((struct sockaddr
*)&sa1
, len1
));
3610 #ifdef HAVE_GETADDRINFO
3619 /* Discard the unwind protect for closing S, if any. */
3620 specpdl_ptr
= specpdl
+ count1
;
3622 /* Unwind bind_polling_period and request_sigio. */
3623 unbind_to (count
, Qnil
);
3627 /* If non-blocking got this far - and failed - assume non-blocking is
3628 not supported after all. This is probably a wrong assumption, but
3629 the normal blocking calls to open-network-stream handles this error
3631 if (is_non_blocking_client
)
3636 report_file_error ("make server process failed", contact
);
3638 report_file_error ("make client process failed", contact
);
3645 buffer
= Fget_buffer_create (buffer
);
3646 proc
= make_process (name
);
3648 chan_process
[inch
] = proc
;
3651 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3654 fcntl (inch
, F_SETFL
, O_NDELAY
);
3658 p
= XPROCESS (proc
);
3660 p
->childp
= contact
;
3661 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3665 p
->sentinel
= sentinel
;
3667 p
->log
= Fplist_get (contact
, QClog
);
3668 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3669 p
->kill_without_query
= 1;
3670 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3675 if (is_server
&& socktype
!= SOCK_DGRAM
)
3676 p
->status
= Qlisten
;
3678 /* Make the process marker point into the process buffer (if any). */
3679 if (BUFFERP (buffer
))
3680 set_marker_both (p
->mark
, buffer
,
3681 BUF_ZV (XBUFFER (buffer
)),
3682 BUF_ZV_BYTE (XBUFFER (buffer
)));
3684 #ifdef NON_BLOCKING_CONNECT
3685 if (is_non_blocking_client
)
3687 /* We may get here if connect did succeed immediately. However,
3688 in that case, we still need to signal this like a non-blocking
3690 p
->status
= Qconnect
;
3691 if (!FD_ISSET (inch
, &connect_wait_mask
))
3693 FD_SET (inch
, &connect_wait_mask
);
3694 FD_SET (inch
, &write_mask
);
3695 num_pending_connects
++;
3700 /* A server may have a client filter setting of Qt, but it must
3701 still listen for incoming connects unless it is stopped. */
3702 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3703 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3705 FD_SET (inch
, &input_wait_mask
);
3706 FD_SET (inch
, &non_keyboard_wait_mask
);
3709 if (inch
> max_process_desc
)
3710 max_process_desc
= inch
;
3712 tem
= Fplist_member (contact
, QCcoding
);
3713 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3714 tem
= Qnil
; /* No error message (too late!). */
3717 /* Setup coding systems for communicating with the network stream. */
3718 struct gcpro gcpro1
;
3719 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3720 Lisp_Object coding_systems
= Qt
;
3721 Lisp_Object args
[5], val
;
3725 val
= XCAR (XCDR (tem
));
3729 else if (!NILP (Vcoding_system_for_read
))
3730 val
= Vcoding_system_for_read
;
3731 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3732 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3733 /* We dare not decode end-of-line format by setting VAL to
3734 Qraw_text, because the existing Emacs Lisp libraries
3735 assume that they receive bare code including a sequene of
3740 if (NILP (host
) || NILP (service
))
3741 coding_systems
= Qnil
;
3744 args
[0] = Qopen_network_stream
, args
[1] = name
,
3745 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3747 coding_systems
= Ffind_operation_coding_system (5, args
);
3750 if (CONSP (coding_systems
))
3751 val
= XCAR (coding_systems
);
3752 else if (CONSP (Vdefault_process_coding_system
))
3753 val
= XCAR (Vdefault_process_coding_system
);
3757 p
->decode_coding_system
= val
;
3761 val
= XCAR (XCDR (tem
));
3765 else if (!NILP (Vcoding_system_for_write
))
3766 val
= Vcoding_system_for_write
;
3767 else if (NILP (current_buffer
->enable_multibyte_characters
))
3771 if (EQ (coding_systems
, Qt
))
3773 if (NILP (host
) || NILP (service
))
3774 coding_systems
= Qnil
;
3777 args
[0] = Qopen_network_stream
, args
[1] = name
,
3778 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3780 coding_systems
= Ffind_operation_coding_system (5, args
);
3784 if (CONSP (coding_systems
))
3785 val
= XCDR (coding_systems
);
3786 else if (CONSP (Vdefault_process_coding_system
))
3787 val
= XCDR (Vdefault_process_coding_system
);
3791 p
->encode_coding_system
= val
;
3793 setup_process_coding_systems (proc
);
3795 p
->decoding_buf
= make_uninit_string (0);
3796 p
->decoding_carryover
= 0;
3797 p
->encoding_buf
= make_uninit_string (0);
3799 p
->inherit_coding_system_flag
3800 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3807 #if defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3810 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3811 doc
: /* Return an alist of all network interfaces and their network address.
3812 Each element is a cons, the car of which is a string containing the
3813 interface name, and the cdr is the network address in internal
3814 format; see the description of ADDRESS in `make-network-process'. */)
3817 struct ifconf ifconf
;
3818 struct ifreq
*ifreqs
= NULL
;
3823 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3829 buf_size
= ifaces
* sizeof (ifreqs
[0]);
3830 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3837 ifconf
.ifc_len
= buf_size
;
3838 ifconf
.ifc_req
= ifreqs
;
3839 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3845 if (ifconf
.ifc_len
== buf_size
)
3849 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3852 while (--ifaces
>= 0)
3854 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3855 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3856 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3858 memcpy (namebuf
, ifq
->ifr_name
, sizeof (ifq
->ifr_name
));
3859 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3860 res
= Fcons (Fcons (build_string (namebuf
),
3861 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3862 sizeof (struct sockaddr
))),
3868 #endif /* SIOCGIFCONF */
3870 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3874 const char *flag_sym
;
3877 static const struct ifflag_def ifflag_table
[] = {
3881 #ifdef IFF_BROADCAST
3882 { IFF_BROADCAST
, "broadcast" },
3885 { IFF_DEBUG
, "debug" },
3888 { IFF_LOOPBACK
, "loopback" },
3890 #ifdef IFF_POINTOPOINT
3891 { IFF_POINTOPOINT
, "pointopoint" },
3894 { IFF_RUNNING
, "running" },
3897 { IFF_NOARP
, "noarp" },
3900 { IFF_PROMISC
, "promisc" },
3902 #ifdef IFF_NOTRAILERS
3903 { IFF_NOTRAILERS
, "notrailers" },
3906 { IFF_ALLMULTI
, "allmulti" },
3909 { IFF_MASTER
, "master" },
3912 { IFF_SLAVE
, "slave" },
3914 #ifdef IFF_MULTICAST
3915 { IFF_MULTICAST
, "multicast" },
3918 { IFF_PORTSEL
, "portsel" },
3920 #ifdef IFF_AUTOMEDIA
3921 { IFF_AUTOMEDIA
, "automedia" },
3924 { IFF_DYNAMIC
, "dynamic" },
3927 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3930 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3933 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3936 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3939 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3944 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3945 doc
: /* Return information about network interface named IFNAME.
3946 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3947 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3948 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3949 FLAGS is the current flags of the interface. */)
3950 (Lisp_Object ifname
)
3953 Lisp_Object res
= Qnil
;
3958 CHECK_STRING (ifname
);
3960 memset (rq
.ifr_name
, 0, sizeof rq
.ifr_name
);
3961 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3963 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3968 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3969 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3971 int flags
= rq
.ifr_flags
;
3972 const struct ifflag_def
*fp
;
3976 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3978 if (flags
& fp
->flag_bit
)
3980 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3981 flags
-= fp
->flag_bit
;
3984 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3986 if (flags
& (1 << fnum
))
3988 elt
= Fcons (make_number (fnum
), elt
);
3993 res
= Fcons (elt
, res
);
3996 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3997 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3999 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
4000 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
4004 for (n
= 0; n
< 6; n
++)
4005 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
4006 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
4009 res
= Fcons (elt
, res
);
4012 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4013 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4016 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4017 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4019 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4023 res
= Fcons (elt
, res
);
4026 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4027 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4030 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4033 res
= Fcons (elt
, res
);
4036 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4037 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4040 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4043 res
= Fcons (elt
, res
);
4047 return any
? res
: Qnil
;
4050 #endif /* defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H) */
4052 /* Turn off input and output for process PROC. */
4055 deactivate_process (Lisp_Object proc
)
4057 register int inchannel
, outchannel
;
4058 register struct Lisp_Process
*p
= XPROCESS (proc
);
4060 inchannel
= p
->infd
;
4061 outchannel
= p
->outfd
;
4063 #ifdef ADAPTIVE_READ_BUFFERING
4064 if (p
->read_output_delay
> 0)
4066 if (--process_output_delay_count
< 0)
4067 process_output_delay_count
= 0;
4068 p
->read_output_delay
= 0;
4069 p
->read_output_skip
= 0;
4075 /* Beware SIGCHLD hereabouts. */
4076 flush_pending_output (inchannel
);
4077 emacs_close (inchannel
);
4078 if (outchannel
>= 0 && outchannel
!= inchannel
)
4079 emacs_close (outchannel
);
4083 #ifdef DATAGRAM_SOCKETS
4084 if (DATAGRAM_CHAN_P (inchannel
))
4086 xfree (datagram_address
[inchannel
].sa
);
4087 datagram_address
[inchannel
].sa
= 0;
4088 datagram_address
[inchannel
].len
= 0;
4091 chan_process
[inchannel
] = Qnil
;
4092 FD_CLR (inchannel
, &input_wait_mask
);
4093 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4094 #ifdef NON_BLOCKING_CONNECT
4095 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4097 FD_CLR (inchannel
, &connect_wait_mask
);
4098 FD_CLR (inchannel
, &write_mask
);
4099 if (--num_pending_connects
< 0)
4103 if (inchannel
== max_process_desc
)
4106 /* We just closed the highest-numbered process input descriptor,
4107 so recompute the highest-numbered one now. */
4108 max_process_desc
= 0;
4109 for (i
= 0; i
< MAXDESC
; i
++)
4110 if (!NILP (chan_process
[i
]))
4111 max_process_desc
= i
;
4117 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4119 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4120 It is read into the process' buffers or given to their filter functions.
4121 Non-nil arg PROCESS means do not return until some output has been received
4124 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4125 and milliseconds to wait; return after that much time whether or not
4126 there is any subprocess output. If SECONDS is a floating point number,
4127 it specifies a fractional number of seconds to wait.
4128 The MILLISEC argument is obsolete and should be avoided.
4130 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4131 from PROCESS, suspending reading output from other processes.
4132 If JUST-THIS-ONE is an integer, don't run any timers either.
4133 Return non-nil if we received any output before the timeout expired. */)
4134 (register Lisp_Object process
, Lisp_Object seconds
, Lisp_Object millisec
, Lisp_Object just_this_one
)
4136 int secs
, usecs
= 0;
4138 if (! NILP (process
))
4139 CHECK_PROCESS (process
);
4141 just_this_one
= Qnil
;
4143 if (!NILP (millisec
))
4144 { /* Obsolete calling convention using integers rather than floats. */
4145 CHECK_NUMBER (millisec
);
4147 seconds
= make_float (XINT (millisec
) / 1000.0);
4150 CHECK_NUMBER (seconds
);
4151 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4155 if (!NILP (seconds
))
4157 if (INTEGERP (seconds
))
4158 secs
= XINT (seconds
);
4159 else if (FLOATP (seconds
))
4161 double timeout
= XFLOAT_DATA (seconds
);
4162 secs
= (int) timeout
;
4163 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4166 wrong_type_argument (Qnumberp
, seconds
);
4168 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4169 secs
= -1, usecs
= 0;
4172 secs
= NILP (process
) ? -1 : 0;
4175 (wait_reading_process_output (secs
, usecs
, 0, 0,
4177 !NILP (process
) ? XPROCESS (process
) : NULL
,
4178 NILP (just_this_one
) ? 0 :
4179 !INTEGERP (just_this_one
) ? 1 : -1)
4183 /* Accept a connection for server process SERVER on CHANNEL. */
4185 static int connect_counter
= 0;
4188 server_accept_connection (Lisp_Object server
, int channel
)
4190 Lisp_Object proc
, caller
, name
, buffer
;
4191 Lisp_Object contact
, host
, service
;
4192 struct Lisp_Process
*ps
= XPROCESS (server
);
4193 struct Lisp_Process
*p
;
4197 struct sockaddr_in in
;
4199 struct sockaddr_in6 in6
;
4201 #ifdef HAVE_LOCAL_SOCKETS
4202 struct sockaddr_un un
;
4205 int len
= sizeof saddr
;
4207 s
= accept (channel
, &saddr
.sa
, &len
);
4216 if (code
== EWOULDBLOCK
)
4220 if (!NILP (ps
->log
))
4221 call3 (ps
->log
, server
, Qnil
,
4222 concat3 (build_string ("accept failed with code"),
4223 Fnumber_to_string (make_number (code
)),
4224 build_string ("\n")));
4230 /* Setup a new process to handle the connection. */
4232 /* Generate a unique identification of the caller, and build contact
4233 information for this process. */
4236 switch (saddr
.sa
.sa_family
)
4240 Lisp_Object args
[5];
4241 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4242 args
[0] = build_string ("%d.%d.%d.%d");
4243 args
[1] = make_number (*ip
++);
4244 args
[2] = make_number (*ip
++);
4245 args
[3] = make_number (*ip
++);
4246 args
[4] = make_number (*ip
++);
4247 host
= Fformat (5, args
);
4248 service
= make_number (ntohs (saddr
.in
.sin_port
));
4250 args
[0] = build_string (" <%s:%d>");
4253 caller
= Fformat (3, args
);
4260 Lisp_Object args
[9];
4261 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4263 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4264 for (i
= 0; i
< 8; i
++)
4265 args
[i
+1] = make_number (ntohs (ip6
[i
]));
4266 host
= Fformat (9, args
);
4267 service
= make_number (ntohs (saddr
.in
.sin_port
));
4269 args
[0] = build_string (" <[%s]:%d>");
4272 caller
= Fformat (3, args
);
4277 #ifdef HAVE_LOCAL_SOCKETS
4281 caller
= Fnumber_to_string (make_number (connect_counter
));
4282 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4286 /* Create a new buffer name for this process if it doesn't have a
4287 filter. The new buffer name is based on the buffer name or
4288 process name of the server process concatenated with the caller
4291 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4295 buffer
= ps
->buffer
;
4297 buffer
= Fbuffer_name (buffer
);
4302 buffer
= concat2 (buffer
, caller
);
4303 buffer
= Fget_buffer_create (buffer
);
4307 /* Generate a unique name for the new server process. Combine the
4308 server process name with the caller identification. */
4310 name
= concat2 (ps
->name
, caller
);
4311 proc
= make_process (name
);
4313 chan_process
[s
] = proc
;
4316 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4319 fcntl (s
, F_SETFL
, O_NDELAY
);
4323 p
= XPROCESS (proc
);
4325 /* Build new contact information for this setup. */
4326 contact
= Fcopy_sequence (ps
->childp
);
4327 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4328 contact
= Fplist_put (contact
, QChost
, host
);
4329 if (!NILP (service
))
4330 contact
= Fplist_put (contact
, QCservice
, service
);
4331 contact
= Fplist_put (contact
, QCremote
,
4332 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4333 #ifdef HAVE_GETSOCKNAME
4335 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4336 contact
= Fplist_put (contact
, QClocal
,
4337 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4340 p
->childp
= contact
;
4341 p
->plist
= Fcopy_sequence (ps
->plist
);
4345 p
->sentinel
= ps
->sentinel
;
4346 p
->filter
= ps
->filter
;
4353 /* Client processes for accepted connections are not stopped initially. */
4354 if (!EQ (p
->filter
, Qt
))
4356 FD_SET (s
, &input_wait_mask
);
4357 FD_SET (s
, &non_keyboard_wait_mask
);
4360 if (s
> max_process_desc
)
4361 max_process_desc
= s
;
4363 /* Setup coding system for new process based on server process.
4364 This seems to be the proper thing to do, as the coding system
4365 of the new process should reflect the settings at the time the
4366 server socket was opened; not the current settings. */
4368 p
->decode_coding_system
= ps
->decode_coding_system
;
4369 p
->encode_coding_system
= ps
->encode_coding_system
;
4370 setup_process_coding_systems (proc
);
4372 p
->decoding_buf
= make_uninit_string (0);
4373 p
->decoding_carryover
= 0;
4374 p
->encoding_buf
= make_uninit_string (0);
4376 p
->inherit_coding_system_flag
4377 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4379 if (!NILP (ps
->log
))
4380 call3 (ps
->log
, server
, proc
,
4381 concat3 (build_string ("accept from "),
4382 (STRINGP (host
) ? host
: build_string ("-")),
4383 build_string ("\n")));
4385 if (!NILP (p
->sentinel
))
4386 exec_sentinel (proc
,
4387 concat3 (build_string ("open from "),
4388 (STRINGP (host
) ? host
: build_string ("-")),
4389 build_string ("\n")));
4392 /* This variable is different from waiting_for_input in keyboard.c.
4393 It is used to communicate to a lisp process-filter/sentinel (via the
4394 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4395 for user-input when that process-filter was called.
4396 waiting_for_input cannot be used as that is by definition 0 when
4397 lisp code is being evalled.
4398 This is also used in record_asynch_buffer_change.
4399 For that purpose, this must be 0
4400 when not inside wait_reading_process_output. */
4401 static int waiting_for_user_input_p
;
4404 wait_reading_process_output_unwind (Lisp_Object data
)
4406 waiting_for_user_input_p
= XINT (data
);
4410 /* This is here so breakpoints can be put on it. */
4412 wait_reading_process_output_1 (void)
4416 /* Use a wrapper around select to work around a bug in gdb 5.3.
4417 Normally, the wrapper is optimized away by inlining.
4419 If emacs is stopped inside select, the gdb backtrace doesn't
4420 show the function which called select, so it is practically
4421 impossible to step through wait_reading_process_output. */
4425 select_wrapper (int n
, fd_set
*rfd
, fd_set
*wfd
, fd_set
*xfd
, struct timeval
*tmo
)
4427 return select (n
, rfd
, wfd
, xfd
, tmo
);
4429 #define select select_wrapper
4432 /* Read and dispose of subprocess output while waiting for timeout to
4433 elapse and/or keyboard input to be available.
4436 timeout in seconds, or
4437 zero for no limit, or
4438 -1 means gobble data immediately available but don't wait for any.
4441 an additional duration to wait, measured in microseconds.
4442 If this is nonzero and time_limit is 0, then the timeout
4443 consists of MICROSECS only.
4445 READ_KBD is a lisp value:
4446 0 to ignore keyboard input, or
4447 1 to return when input is available, or
4448 -1 meaning caller will actually read the input, so don't throw to
4449 the quit handler, or
4451 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4452 output that arrives.
4454 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4455 (and gobble terminal input into the buffer if any arrives).
4457 If WAIT_PROC is specified, wait until something arrives from that
4458 process. The return value is true if we read some input from
4461 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4462 (suspending output from other processes). A negative value
4463 means don't run any timers either.
4465 If WAIT_PROC is specified, then the function returns true if we
4466 received input from that process before the timeout elapsed.
4467 Otherwise, return true if we received input from any process. */
4470 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
4472 Lisp_Object wait_for_cell
,
4473 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
4475 register int channel
, nfds
;
4476 SELECT_TYPE Available
;
4477 SELECT_TYPE Writeok
;
4479 int check_delay
, no_avail
;
4482 EMACS_TIME timeout
, end_time
;
4483 int wait_channel
= -1;
4484 int got_some_input
= 0;
4485 int count
= SPECPDL_INDEX ();
4487 FD_ZERO (&Available
);
4490 if (time_limit
== 0 && microsecs
== 0 && wait_proc
&& !NILP (Vinhibit_quit
)
4491 && !(CONSP (wait_proc
->status
) && EQ (XCAR (wait_proc
->status
), Qexit
)))
4492 message ("Blocking call to accept-process-output with quit inhibited!!");
4494 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4495 if (wait_proc
!= NULL
)
4496 wait_channel
= wait_proc
->infd
;
4498 record_unwind_protect (wait_reading_process_output_unwind
,
4499 make_number (waiting_for_user_input_p
));
4500 waiting_for_user_input_p
= read_kbd
;
4502 /* Since we may need to wait several times,
4503 compute the absolute time to return at. */
4504 if (time_limit
|| microsecs
)
4506 EMACS_GET_TIME (end_time
);
4507 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4508 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4513 int timeout_reduced_for_timers
= 0;
4515 /* If calling from keyboard input, do not quit
4516 since we want to return C-g as an input character.
4517 Otherwise, do pending quit if requested. */
4522 process_pending_signals ();
4525 /* Exit now if the cell we're waiting for became non-nil. */
4526 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4529 /* Compute time from now till when time limit is up */
4530 /* Exit if already run out */
4531 if (time_limit
== -1)
4533 /* -1 specified for timeout means
4534 gobble output available now
4535 but don't wait at all. */
4537 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4539 else if (time_limit
|| microsecs
)
4541 EMACS_GET_TIME (timeout
);
4542 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4543 if (EMACS_TIME_NEG_P (timeout
))
4548 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4551 /* Normally we run timers here.
4552 But not if wait_for_cell; in those cases,
4553 the wait is supposed to be short,
4554 and those callers cannot handle running arbitrary Lisp code here. */
4555 if (NILP (wait_for_cell
)
4556 && just_wait_proc
>= 0)
4558 EMACS_TIME timer_delay
;
4562 int old_timers_run
= timers_run
;
4563 struct buffer
*old_buffer
= current_buffer
;
4564 Lisp_Object old_window
= selected_window
;
4566 timer_delay
= timer_check (1);
4568 /* If a timer has run, this might have changed buffers
4569 an alike. Make read_key_sequence aware of that. */
4570 if (timers_run
!= old_timers_run
4571 && (old_buffer
!= current_buffer
4572 || !EQ (old_window
, selected_window
))
4573 && waiting_for_user_input_p
== -1)
4574 record_asynch_buffer_change ();
4576 if (timers_run
!= old_timers_run
&& do_display
)
4577 /* We must retry, since a timer may have requeued itself
4578 and that could alter the time_delay. */
4579 redisplay_preserve_echo_area (9);
4583 while (!detect_input_pending ());
4585 /* If there is unread keyboard input, also return. */
4587 && requeued_events_pending_p ())
4590 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4592 EMACS_TIME difference
;
4593 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4594 if (EMACS_TIME_NEG_P (difference
))
4596 timeout
= timer_delay
;
4597 timeout_reduced_for_timers
= 1;
4600 /* If time_limit is -1, we are not going to wait at all. */
4601 else if (time_limit
!= -1)
4603 /* This is so a breakpoint can be put here. */
4604 wait_reading_process_output_1 ();
4608 /* Cause C-g and alarm signals to take immediate action,
4609 and cause input available signals to zero out timeout.
4611 It is important that we do this before checking for process
4612 activity. If we get a SIGCHLD after the explicit checks for
4613 process activity, timeout is the only way we will know. */
4615 set_waiting_for_input (&timeout
);
4617 /* If status of something has changed, and no input is
4618 available, notify the user of the change right away. After
4619 this explicit check, we'll let the SIGCHLD handler zap
4620 timeout to get our attention. */
4621 if (update_tick
!= process_tick
)
4626 if (kbd_on_hold_p ())
4629 Atemp
= input_wait_mask
;
4632 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4633 if ((select (max (max_process_desc
, max_input_desc
) + 1,
4635 #ifdef NON_BLOCKING_CONNECT
4636 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4640 (SELECT_TYPE
*)0, &timeout
)
4643 /* It's okay for us to do this and then continue with
4644 the loop, since timeout has already been zeroed out. */
4645 clear_waiting_for_input ();
4646 status_notify (NULL
);
4647 if (do_display
) redisplay_preserve_echo_area (13);
4651 /* Don't wait for output from a non-running process. Just
4652 read whatever data has already been received. */
4653 if (wait_proc
&& wait_proc
->raw_status_new
)
4654 update_status (wait_proc
);
4656 && ! EQ (wait_proc
->status
, Qrun
)
4657 && ! EQ (wait_proc
->status
, Qconnect
))
4659 int nread
, total_nread
= 0;
4661 clear_waiting_for_input ();
4662 XSETPROCESS (proc
, wait_proc
);
4664 /* Read data from the process, until we exhaust it. */
4665 while (wait_proc
->infd
>= 0)
4667 nread
= read_process_output (proc
, wait_proc
->infd
);
4674 total_nread
+= nread
;
4678 else if (nread
== -1 && EIO
== errno
)
4682 else if (nread
== -1 && EAGAIN
== errno
)
4686 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4690 if (total_nread
> 0 && do_display
)
4691 redisplay_preserve_echo_area (10);
4696 /* Wait till there is something to do */
4698 if (wait_proc
&& just_wait_proc
)
4700 if (wait_proc
->infd
< 0) /* Terminated */
4702 FD_SET (wait_proc
->infd
, &Available
);
4706 else if (!NILP (wait_for_cell
))
4708 Available
= non_process_wait_mask
;
4715 Available
= non_keyboard_wait_mask
;
4717 Available
= input_wait_mask
;
4718 Writeok
= write_mask
;
4719 #ifdef SELECT_CANT_DO_WRITE_MASK
4724 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4727 /* If frame size has changed or the window is newly mapped,
4728 redisplay now, before we start to wait. There is a race
4729 condition here; if a SIGIO arrives between now and the select
4730 and indicates that a frame is trashed, the select may block
4731 displaying a trashed screen. */
4732 if (frame_garbaged
&& do_display
)
4734 clear_waiting_for_input ();
4735 redisplay_preserve_echo_area (11);
4737 set_waiting_for_input (&timeout
);
4741 if (read_kbd
&& detect_input_pending ())
4749 #ifdef ADAPTIVE_READ_BUFFERING
4750 /* Set the timeout for adaptive read buffering if any
4751 process has non-zero read_output_skip and non-zero
4752 read_output_delay, and we are not reading output for a
4753 specific wait_channel. It is not executed if
4754 Vprocess_adaptive_read_buffering is nil. */
4755 if (process_output_skip
&& check_delay
> 0)
4757 int usecs
= EMACS_USECS (timeout
);
4758 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4759 usecs
= READ_OUTPUT_DELAY_MAX
;
4760 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4762 proc
= chan_process
[channel
];
4765 /* Find minimum non-zero read_output_delay among the
4766 processes with non-zero read_output_skip. */
4767 if (XPROCESS (proc
)->read_output_delay
> 0)
4770 if (!XPROCESS (proc
)->read_output_skip
)
4772 FD_CLR (channel
, &Available
);
4773 XPROCESS (proc
)->read_output_skip
= 0;
4774 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4775 usecs
= XPROCESS (proc
)->read_output_delay
;
4778 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4779 process_output_skip
= 0;
4782 #if defined (USE_GTK) || defined (HAVE_GCONF)
4784 #elif defined (HAVE_NS)
4789 (max (max_process_desc
, max_input_desc
) + 1,
4791 (check_write
? &Writeok
: (SELECT_TYPE
*)0),
4792 (SELECT_TYPE
*)0, &timeout
);
4797 /* Make C-g and alarm signals set flags again */
4798 clear_waiting_for_input ();
4800 /* If we woke up due to SIGWINCH, actually change size now. */
4801 do_pending_window_change (0);
4803 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4804 /* We wanted the full specified time, so return now. */
4808 if (xerrno
== EINTR
)
4810 else if (xerrno
== EBADF
)
4813 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4814 the child's closure of the pts gives the parent a SIGHUP, and
4815 the ptc file descriptor is automatically closed,
4816 yielding EBADF here or at select() call above.
4817 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4818 in m/ibmrt-aix.h), and here we just ignore the select error.
4819 Cleanup occurs c/o status_notify after SIGCLD. */
4820 no_avail
= 1; /* Cannot depend on values returned */
4826 error ("select error: %s", emacs_strerror (xerrno
));
4831 FD_ZERO (&Available
);
4835 #if 0 /* When polling is used, interrupt_input is 0,
4836 so get_input_pending should read the input.
4837 So this should not be needed. */
4838 /* If we are using polling for input,
4839 and we see input available, make it get read now.
4840 Otherwise it might not actually get read for a second.
4841 And on hpux, since we turn off polling in wait_reading_process_output,
4842 it might never get read at all if we don't spend much time
4843 outside of wait_reading_process_output. */
4844 if (read_kbd
&& interrupt_input
4845 && keyboard_bit_set (&Available
)
4846 && input_polling_used ())
4847 kill (getpid (), SIGALRM
);
4850 /* Check for keyboard input */
4851 /* If there is any, return immediately
4852 to give it higher priority than subprocesses */
4856 int old_timers_run
= timers_run
;
4857 struct buffer
*old_buffer
= current_buffer
;
4858 Lisp_Object old_window
= selected_window
;
4861 if (detect_input_pending_run_timers (do_display
))
4863 swallow_events (do_display
);
4864 if (detect_input_pending_run_timers (do_display
))
4868 /* If a timer has run, this might have changed buffers
4869 an alike. Make read_key_sequence aware of that. */
4870 if (timers_run
!= old_timers_run
4871 && waiting_for_user_input_p
== -1
4872 && (old_buffer
!= current_buffer
4873 || !EQ (old_window
, selected_window
)))
4874 record_asynch_buffer_change ();
4880 /* If there is unread keyboard input, also return. */
4882 && requeued_events_pending_p ())
4885 /* If we are not checking for keyboard input now,
4886 do process events (but don't run any timers).
4887 This is so that X events will be processed.
4888 Otherwise they may have to wait until polling takes place.
4889 That would causes delays in pasting selections, for example.
4891 (We used to do this only if wait_for_cell.) */
4892 if (read_kbd
== 0 && detect_input_pending ())
4894 swallow_events (do_display
);
4895 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4896 if (detect_input_pending ())
4901 /* Exit now if the cell we're waiting for became non-nil. */
4902 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4906 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4907 go read it. This can happen with X on BSD after logging out.
4908 In that case, there really is no input and no SIGIO,
4909 but select says there is input. */
4911 if (read_kbd
&& interrupt_input
4912 && keyboard_bit_set (&Available
) && ! noninteractive
)
4913 kill (getpid (), SIGIO
);
4917 got_some_input
|= nfds
> 0;
4919 /* If checking input just got us a size-change event from X,
4920 obey it now if we should. */
4921 if (read_kbd
|| ! NILP (wait_for_cell
))
4922 do_pending_window_change (0);
4924 /* Check for data from a process. */
4925 if (no_avail
|| nfds
== 0)
4928 for (channel
= 0; channel
<= max_input_desc
; ++channel
)
4930 struct fd_callback_data
*d
= &fd_callback_info
[channel
];
4931 if (FD_ISSET (channel
, &Available
)
4933 && (d
->condition
& FOR_READ
) != 0)
4934 d
->func (channel
, d
->data
, 1);
4935 if (FD_ISSET (channel
, &write_mask
)
4937 && (d
->condition
& FOR_WRITE
) != 0)
4938 d
->func (channel
, d
->data
, 0);
4941 /* Really FIRST_PROC_DESC should be 0 on Unix,
4942 but this is safer in the short run. */
4943 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4945 if (FD_ISSET (channel
, &Available
)
4946 && FD_ISSET (channel
, &non_keyboard_wait_mask
)
4947 && !FD_ISSET (channel
, &non_process_wait_mask
))
4951 /* If waiting for this channel, arrange to return as
4952 soon as no more input to be processed. No more
4954 if (wait_channel
== channel
)
4960 proc
= chan_process
[channel
];
4964 /* If this is a server stream socket, accept connection. */
4965 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4967 server_accept_connection (proc
, channel
);
4971 /* Read data from the process, starting with our
4972 buffered-ahead character if we have one. */
4974 nread
= read_process_output (proc
, channel
);
4977 /* Since read_process_output can run a filter,
4978 which can call accept-process-output,
4979 don't try to read from any other processes
4980 before doing the select again. */
4981 FD_ZERO (&Available
);
4984 redisplay_preserve_echo_area (12);
4987 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4990 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4991 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4993 else if (nread
== -1 && errno
== EAGAIN
)
4997 else if (nread
== -1 && errno
== EAGAIN
)
4999 /* Note that we cannot distinguish between no input
5000 available now and a closed pipe.
5001 With luck, a closed pipe will be accompanied by
5002 subprocess termination and SIGCHLD. */
5003 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5005 #endif /* O_NDELAY */
5006 #endif /* O_NONBLOCK */
5008 /* On some OSs with ptys, when the process on one end of
5009 a pty exits, the other end gets an error reading with
5010 errno = EIO instead of getting an EOF (0 bytes read).
5011 Therefore, if we get an error reading and errno =
5012 EIO, just continue, because the child process has
5013 exited and should clean itself up soon (e.g. when we
5016 However, it has been known to happen that the SIGCHLD
5017 got lost. So raise the signal again just in case.
5019 else if (nread
== -1 && errno
== EIO
)
5021 /* Clear the descriptor now, so we only raise the
5022 signal once. Don't do this if `process' is only
5024 if (XPROCESS (proc
)->pid
!= -2)
5026 FD_CLR (channel
, &input_wait_mask
);
5027 FD_CLR (channel
, &non_keyboard_wait_mask
);
5029 kill (getpid (), SIGCHLD
);
5032 #endif /* HAVE_PTYS */
5033 /* If we can detect process termination, don't consider the process
5034 gone just because its pipe is closed. */
5036 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5041 /* Preserve status of processes already terminated. */
5042 XPROCESS (proc
)->tick
= ++process_tick
;
5043 deactivate_process (proc
);
5044 if (XPROCESS (proc
)->raw_status_new
)
5045 update_status (XPROCESS (proc
));
5046 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5047 XPROCESS (proc
)->status
5048 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5051 #ifdef NON_BLOCKING_CONNECT
5052 if (FD_ISSET (channel
, &Writeok
)
5053 && FD_ISSET (channel
, &connect_wait_mask
))
5055 struct Lisp_Process
*p
;
5057 FD_CLR (channel
, &connect_wait_mask
);
5058 FD_CLR (channel
, &write_mask
);
5059 if (--num_pending_connects
< 0)
5062 proc
= chan_process
[channel
];
5066 p
= XPROCESS (proc
);
5069 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5070 So only use it on systems where it is known to work. */
5072 int xlen
= sizeof (xerrno
);
5073 if (getsockopt (channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5078 struct sockaddr pname
;
5079 int pnamelen
= sizeof (pname
);
5081 /* If connection failed, getpeername will fail. */
5083 if (getpeername (channel
, &pname
, &pnamelen
) < 0)
5085 /* Obtain connect failure code through error slippage. */
5088 if (errno
== ENOTCONN
&& read (channel
, &dummy
, 1) < 0)
5095 p
->tick
= ++process_tick
;
5096 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5097 deactivate_process (proc
);
5102 /* Execute the sentinel here. If we had relied on
5103 status_notify to do it later, it will read input
5104 from the process before calling the sentinel. */
5105 exec_sentinel (proc
, build_string ("open\n"));
5106 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5108 FD_SET (p
->infd
, &input_wait_mask
);
5109 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5113 #endif /* NON_BLOCKING_CONNECT */
5114 } /* end for each file descriptor */
5115 } /* end while exit conditions not met */
5117 unbind_to (count
, Qnil
);
5119 /* If calling from keyboard input, do not quit
5120 since we want to return C-g as an input character.
5121 Otherwise, do pending quit if requested. */
5124 /* Prevent input_pending from remaining set if we quit. */
5125 clear_input_pending ();
5129 return got_some_input
;
5132 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5135 read_process_output_call (Lisp_Object fun_and_args
)
5137 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5141 read_process_output_error_handler (Lisp_Object error
)
5143 cmd_error_internal (error
, "error in process filter: ");
5145 update_echo_area ();
5146 Fsleep_for (make_number (2), Qnil
);
5150 /* Read pending output from the process channel,
5151 starting with our buffered-ahead character if we have one.
5152 Yield number of decoded characters read.
5154 This function reads at most 4096 characters.
5155 If you want to read all available subprocess output,
5156 you must call it repeatedly until it returns zero.
5158 The characters read are decoded according to PROC's coding-system
5162 read_process_output (Lisp_Object proc
, register int channel
)
5164 register int nbytes
;
5166 register Lisp_Object outstream
;
5167 register struct Lisp_Process
*p
= XPROCESS (proc
);
5168 register EMACS_INT opoint
;
5169 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5170 int carryover
= p
->decoding_carryover
;
5172 int count
= SPECPDL_INDEX ();
5173 Lisp_Object odeactivate
;
5175 chars
= (char *) alloca (carryover
+ readmax
);
5177 /* See the comment above. */
5178 memcpy (chars
, SDATA (p
->decoding_buf
), carryover
);
5180 #ifdef DATAGRAM_SOCKETS
5181 /* We have a working select, so proc_buffered_char is always -1. */
5182 if (DATAGRAM_CHAN_P (channel
))
5184 int len
= datagram_address
[channel
].len
;
5185 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5186 0, datagram_address
[channel
].sa
, &len
);
5190 if (proc_buffered_char
[channel
] < 0)
5193 if (XPROCESS (proc
)->gnutls_p
)
5194 nbytes
= emacs_gnutls_read (channel
, XPROCESS (proc
),
5195 chars
+ carryover
, readmax
);
5198 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5199 #ifdef ADAPTIVE_READ_BUFFERING
5200 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5202 int delay
= p
->read_output_delay
;
5205 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5208 process_output_delay_count
++;
5209 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5212 else if (delay
> 0 && (nbytes
== readmax
))
5214 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5216 process_output_delay_count
--;
5218 p
->read_output_delay
= delay
;
5221 p
->read_output_skip
= 1;
5222 process_output_skip
= 1;
5229 chars
[carryover
] = proc_buffered_char
[channel
];
5230 proc_buffered_char
[channel
] = -1;
5232 if (XPROCESS (proc
)->gnutls_p
)
5233 nbytes
= emacs_gnutls_read (channel
, XPROCESS (proc
),
5234 chars
+ carryover
+ 1, readmax
- 1);
5237 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5241 nbytes
= nbytes
+ 1;
5244 p
->decoding_carryover
= 0;
5246 /* At this point, NBYTES holds number of bytes just received
5247 (including the one in proc_buffered_char[channel]). */
5250 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5252 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5255 /* Now set NBYTES how many bytes we must decode. */
5256 nbytes
+= carryover
;
5258 odeactivate
= Vdeactivate_mark
;
5259 /* There's no good reason to let process filters change the current
5260 buffer, and many callers of accept-process-output, sit-for, and
5261 friends don't expect current-buffer to be changed from under them. */
5262 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
5264 /* Read and dispose of the process output. */
5265 outstream
= p
->filter
;
5266 if (!NILP (outstream
))
5268 Lisp_Object obuffer
, okeymap
;
5270 int outer_running_asynch_code
= running_asynch_code
;
5271 int waiting
= waiting_for_user_input_p
;
5273 /* No need to gcpro these, because all we do with them later
5274 is test them for EQness, and none of them should be a string. */
5275 XSETBUFFER (obuffer
, current_buffer
);
5276 okeymap
= current_buffer
->keymap
;
5278 /* We inhibit quit here instead of just catching it so that
5279 hitting ^G when a filter happens to be running won't screw
5281 specbind (Qinhibit_quit
, Qt
);
5282 specbind (Qlast_nonmenu_event
, Qt
);
5284 /* In case we get recursively called,
5285 and we already saved the match data nonrecursively,
5286 save the same match data in safely recursive fashion. */
5287 if (outer_running_asynch_code
)
5290 /* Don't clobber the CURRENT match data, either! */
5291 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5292 restore_search_regs ();
5293 record_unwind_save_match_data ();
5294 Fset_match_data (tem
, Qt
);
5297 /* For speed, if a search happens within this code,
5298 save the match data in a special nonrecursive fashion. */
5299 running_asynch_code
= 1;
5301 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5302 text
= coding
->dst_object
;
5303 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5304 /* A new coding system might be found. */
5305 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5307 p
->decode_coding_system
= Vlast_coding_system_used
;
5309 /* Don't call setup_coding_system for
5310 proc_decode_coding_system[channel] here. It is done in
5311 detect_coding called via decode_coding above. */
5313 /* If a coding system for encoding is not yet decided, we set
5314 it as the same as coding-system for decoding.
5316 But, before doing that we must check if
5317 proc_encode_coding_system[p->outfd] surely points to a
5318 valid memory because p->outfd will be changed once EOF is
5319 sent to the process. */
5320 if (NILP (p
->encode_coding_system
)
5321 && proc_encode_coding_system
[p
->outfd
])
5323 p
->encode_coding_system
5324 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5325 setup_coding_system (p
->encode_coding_system
,
5326 proc_encode_coding_system
[p
->outfd
]);
5330 if (coding
->carryover_bytes
> 0)
5332 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5333 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5334 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5335 coding
->carryover_bytes
);
5336 p
->decoding_carryover
= coding
->carryover_bytes
;
5338 if (SBYTES (text
) > 0)
5339 internal_condition_case_1 (read_process_output_call
,
5341 Fcons (proc
, Fcons (text
, Qnil
))),
5342 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5343 read_process_output_error_handler
);
5345 /* If we saved the match data nonrecursively, restore it now. */
5346 restore_search_regs ();
5347 running_asynch_code
= outer_running_asynch_code
;
5349 /* Restore waiting_for_user_input_p as it was
5350 when we were called, in case the filter clobbered it. */
5351 waiting_for_user_input_p
= waiting
;
5353 #if 0 /* Call record_asynch_buffer_change unconditionally,
5354 because we might have changed minor modes or other things
5355 that affect key bindings. */
5356 if (! EQ (Fcurrent_buffer (), obuffer
)
5357 || ! EQ (current_buffer
->keymap
, okeymap
))
5359 /* But do it only if the caller is actually going to read events.
5360 Otherwise there's no need to make him wake up, and it could
5361 cause trouble (for example it would make sit_for return). */
5362 if (waiting_for_user_input_p
== -1)
5363 record_asynch_buffer_change ();
5366 /* If no filter, write into buffer if it isn't dead. */
5367 else if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5369 Lisp_Object old_read_only
;
5370 EMACS_INT old_begv
, old_zv
;
5371 EMACS_INT old_begv_byte
, old_zv_byte
;
5372 EMACS_INT before
, before_byte
;
5373 EMACS_INT opoint_byte
;
5377 Fset_buffer (p
->buffer
);
5379 opoint_byte
= PT_BYTE
;
5380 old_read_only
= current_buffer
->read_only
;
5383 old_begv_byte
= BEGV_BYTE
;
5384 old_zv_byte
= ZV_BYTE
;
5386 current_buffer
->read_only
= Qnil
;
5388 /* Insert new output into buffer
5389 at the current end-of-output marker,
5390 thus preserving logical ordering of input and output. */
5391 if (XMARKER (p
->mark
)->buffer
)
5392 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5393 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5396 SET_PT_BOTH (ZV
, ZV_BYTE
);
5398 before_byte
= PT_BYTE
;
5400 /* If the output marker is outside of the visible region, save
5401 the restriction and widen. */
5402 if (! (BEGV
<= PT
&& PT
<= ZV
))
5405 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5406 text
= coding
->dst_object
;
5407 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5408 /* A new coding system might be found. See the comment in the
5409 similar code in the previous `if' block. */
5410 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5412 p
->decode_coding_system
= Vlast_coding_system_used
;
5413 if (NILP (p
->encode_coding_system
)
5414 && proc_encode_coding_system
[p
->outfd
])
5416 p
->encode_coding_system
5417 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5418 setup_coding_system (p
->encode_coding_system
,
5419 proc_encode_coding_system
[p
->outfd
]);
5422 if (coding
->carryover_bytes
> 0)
5424 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5425 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5426 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5427 coding
->carryover_bytes
);
5428 p
->decoding_carryover
= coding
->carryover_bytes
;
5430 /* Adjust the multibyteness of TEXT to that of the buffer. */
5431 if (NILP (current_buffer
->enable_multibyte_characters
)
5432 != ! STRING_MULTIBYTE (text
))
5433 text
= (STRING_MULTIBYTE (text
)
5434 ? Fstring_as_unibyte (text
)
5435 : Fstring_to_multibyte (text
));
5436 /* Insert before markers in case we are inserting where
5437 the buffer's mark is, and the user's next command is Meta-y. */
5438 insert_from_string_before_markers (text
, 0, 0,
5439 SCHARS (text
), SBYTES (text
), 0);
5441 /* Make sure the process marker's position is valid when the
5442 process buffer is changed in the signal_after_change above.
5443 W3 is known to do that. */
5444 if (BUFFERP (p
->buffer
)
5445 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5446 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5448 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5450 update_mode_lines
++;
5452 /* Make sure opoint and the old restrictions
5453 float ahead of any new text just as point would. */
5454 if (opoint
>= before
)
5456 opoint
+= PT
- before
;
5457 opoint_byte
+= PT_BYTE
- before_byte
;
5459 if (old_begv
> before
)
5461 old_begv
+= PT
- before
;
5462 old_begv_byte
+= PT_BYTE
- before_byte
;
5464 if (old_zv
>= before
)
5466 old_zv
+= PT
- before
;
5467 old_zv_byte
+= PT_BYTE
- before_byte
;
5470 /* If the restriction isn't what it should be, set it. */
5471 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5472 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5475 current_buffer
->read_only
= old_read_only
;
5476 SET_PT_BOTH (opoint
, opoint_byte
);
5478 /* Handling the process output should not deactivate the mark. */
5479 Vdeactivate_mark
= odeactivate
;
5481 unbind_to (count
, Qnil
);
5485 /* Sending data to subprocess */
5487 jmp_buf send_process_frame
;
5488 Lisp_Object process_sent_to
;
5491 send_process_trap (int ignore
)
5493 SIGNAL_THREAD_CHECK (SIGPIPE
);
5494 sigunblock (sigmask (SIGPIPE
));
5495 longjmp (send_process_frame
, 1);
5498 /* Send some data to process PROC.
5499 BUF is the beginning of the data; LEN is the number of characters.
5500 OBJECT is the Lisp object that the data comes from. If OBJECT is
5501 nil or t, it means that the data comes from C string.
5503 If OBJECT is not nil, the data is encoded by PROC's coding-system
5504 for encoding before it is sent.
5506 This function can evaluate Lisp code and can garbage collect. */
5509 send_process (volatile Lisp_Object proc
, const unsigned char *volatile buf
,
5510 volatile EMACS_INT len
, volatile Lisp_Object object
)
5512 /* Use volatile to protect variables from being clobbered by longjmp. */
5513 struct Lisp_Process
*p
= XPROCESS (proc
);
5515 struct coding_system
*coding
;
5516 struct gcpro gcpro1
;
5517 SIGTYPE (*volatile old_sigpipe
) (int);
5521 if (p
->raw_status_new
)
5523 if (! EQ (p
->status
, Qrun
))
5524 error ("Process %s not running", SDATA (p
->name
));
5526 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5528 coding
= proc_encode_coding_system
[p
->outfd
];
5529 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5531 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5532 || (BUFFERP (object
)
5533 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5536 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5537 /* The coding system for encoding was changed to raw-text
5538 because we sent a unibyte text previously. Now we are
5539 sending a multibyte text, thus we must encode it by the
5540 original coding system specified for the current process. */
5541 setup_coding_system (p
->encode_coding_system
, coding
);
5542 coding
->src_multibyte
= 1;
5546 /* For sending a unibyte text, character code conversion should
5547 not take place but EOL conversion should. So, setup raw-text
5548 or one of the subsidiary if we have not yet done it. */
5549 if (CODING_REQUIRE_ENCODING (coding
))
5551 if (CODING_REQUIRE_FLUSHING (coding
))
5553 /* But, before changing the coding, we must flush out data. */
5554 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5555 send_process (proc
, "", 0, Qt
);
5556 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5558 setup_coding_system (raw_text_coding_system
5559 (Vlast_coding_system_used
),
5561 coding
->src_multibyte
= 0;
5564 coding
->dst_multibyte
= 0;
5566 if (CODING_REQUIRE_ENCODING (coding
))
5568 coding
->dst_object
= Qt
;
5569 if (BUFFERP (object
))
5571 EMACS_INT from_byte
, from
, to
;
5572 EMACS_INT save_pt
, save_pt_byte
;
5573 struct buffer
*cur
= current_buffer
;
5575 set_buffer_internal (XBUFFER (object
));
5576 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5578 from_byte
= PTR_BYTE_POS (buf
);
5579 from
= BYTE_TO_CHAR (from_byte
);
5580 to
= BYTE_TO_CHAR (from_byte
+ len
);
5581 TEMP_SET_PT_BOTH (from
, from_byte
);
5582 encode_coding_object (coding
, object
, from
, from_byte
,
5583 to
, from_byte
+ len
, Qt
);
5584 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5585 set_buffer_internal (cur
);
5587 else if (STRINGP (object
))
5589 encode_coding_object (coding
, object
, 0, 0, SCHARS (object
),
5590 SBYTES (object
), Qt
);
5594 coding
->dst_object
= make_unibyte_string (buf
, len
);
5595 coding
->produced
= len
;
5598 len
= coding
->produced
;
5599 object
= coding
->dst_object
;
5600 buf
= SDATA (object
);
5603 if (pty_max_bytes
== 0)
5605 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5606 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5607 if (pty_max_bytes
< 0)
5608 pty_max_bytes
= 250;
5610 pty_max_bytes
= 250;
5612 /* Deduct one, to leave space for the eof. */
5616 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5617 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5618 when returning with longjmp despite being declared volatile. */
5619 if (!setjmp (send_process_frame
))
5621 process_sent_to
= proc
;
5624 EMACS_INT
this = len
;
5626 /* Send this batch, using one or more write calls. */
5629 int outfd
= p
->outfd
;
5630 old_sigpipe
= (SIGTYPE (*) (int)) signal (SIGPIPE
, send_process_trap
);
5631 #ifdef DATAGRAM_SOCKETS
5632 if (DATAGRAM_CHAN_P (outfd
))
5634 rv
= sendto (outfd
, (char *) buf
, this,
5635 0, datagram_address
[outfd
].sa
,
5636 datagram_address
[outfd
].len
);
5637 if (rv
< 0 && errno
== EMSGSIZE
)
5639 signal (SIGPIPE
, old_sigpipe
);
5640 report_file_error ("sending datagram",
5641 Fcons (proc
, Qnil
));
5648 if (XPROCESS (proc
)->gnutls_p
)
5649 rv
= emacs_gnutls_write (outfd
,
5651 (char *) buf
, this);
5654 rv
= emacs_write (outfd
, (char *) buf
, this);
5655 #ifdef ADAPTIVE_READ_BUFFERING
5656 if (p
->read_output_delay
> 0
5657 && p
->adaptive_read_buffering
== 1)
5659 p
->read_output_delay
= 0;
5660 process_output_delay_count
--;
5661 p
->read_output_skip
= 0;
5665 signal (SIGPIPE
, old_sigpipe
);
5671 || errno
== EWOULDBLOCK
5677 /* Buffer is full. Wait, accepting input;
5678 that may allow the program
5679 to finish doing output and read more. */
5683 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5684 /* A gross hack to work around a bug in FreeBSD.
5685 In the following sequence, read(2) returns
5689 write(2) 954 bytes, get EAGAIN
5690 read(2) 1024 bytes in process_read_output
5691 read(2) 11 bytes in process_read_output
5693 That is, read(2) returns more bytes than have
5694 ever been written successfully. The 1033 bytes
5695 read are the 1022 bytes written successfully
5696 after processing (for example with CRs added if
5697 the terminal is set up that way which it is
5698 here). The same bytes will be seen again in a
5699 later read(2), without the CRs. */
5701 if (errno
== EAGAIN
)
5704 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5706 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5708 /* Running filters might relocate buffers or strings.
5709 Arrange to relocate BUF. */
5710 if (BUFFERP (object
))
5711 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5712 else if (STRINGP (object
))
5713 offset
= buf
- SDATA (object
);
5715 #ifdef EMACS_HAS_USECS
5716 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5718 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5721 if (BUFFERP (object
))
5722 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5723 else if (STRINGP (object
))
5724 buf
= offset
+ SDATA (object
);
5729 /* This is a real error. */
5730 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5740 signal (SIGPIPE
, old_sigpipe
);
5741 proc
= process_sent_to
;
5742 p
= XPROCESS (proc
);
5743 p
->raw_status_new
= 0;
5744 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5745 p
->tick
= ++process_tick
;
5746 deactivate_process (proc
);
5747 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5753 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5755 doc
: /* Send current contents of region as input to PROCESS.
5756 PROCESS may be a process, a buffer, the name of a process or buffer, or
5757 nil, indicating the current buffer's process.
5758 Called from program, takes three arguments, PROCESS, START and END.
5759 If the region is more than 500 characters long,
5760 it is sent in several bunches. This may happen even for shorter regions.
5761 Output from processes can arrive in between bunches. */)
5762 (Lisp_Object process
, Lisp_Object start
, Lisp_Object end
)
5765 EMACS_INT start1
, end1
;
5767 proc
= get_process (process
);
5768 validate_region (&start
, &end
);
5770 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5771 move_gap (XINT (start
));
5773 start1
= CHAR_TO_BYTE (XINT (start
));
5774 end1
= CHAR_TO_BYTE (XINT (end
));
5775 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5776 Fcurrent_buffer ());
5781 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5783 doc
: /* Send PROCESS the contents of STRING as input.
5784 PROCESS may be a process, a buffer, the name of a process or buffer, or
5785 nil, indicating the current buffer's process.
5786 If STRING is more than 500 characters long,
5787 it is sent in several bunches. This may happen even for shorter strings.
5788 Output from processes can arrive in between bunches. */)
5789 (Lisp_Object process
, Lisp_Object string
)
5792 CHECK_STRING (string
);
5793 proc
= get_process (process
);
5794 send_process (proc
, SDATA (string
),
5795 SBYTES (string
), string
);
5799 /* Return the foreground process group for the tty/pty that
5800 the process P uses. */
5802 emacs_get_tty_pgrp (struct Lisp_Process
*p
)
5807 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5810 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5811 master side. Try the slave side. */
5812 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
5816 ioctl (fd
, TIOCGPGRP
, &gid
);
5820 #endif /* defined (TIOCGPGRP ) */
5825 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5826 Sprocess_running_child_p
, 0, 1, 0,
5827 doc
: /* Return t if PROCESS has given the terminal to a child.
5828 If the operating system does not make it possible to find out,
5829 return t unconditionally. */)
5830 (Lisp_Object process
)
5832 /* Initialize in case ioctl doesn't exist or gives an error,
5833 in a way that will cause returning t. */
5836 struct Lisp_Process
*p
;
5838 proc
= get_process (process
);
5839 p
= XPROCESS (proc
);
5841 if (!EQ (p
->type
, Qreal
))
5842 error ("Process %s is not a subprocess",
5845 error ("Process %s is not active",
5848 gid
= emacs_get_tty_pgrp (p
);
5855 /* send a signal number SIGNO to PROCESS.
5856 If CURRENT_GROUP is t, that means send to the process group
5857 that currently owns the terminal being used to communicate with PROCESS.
5858 This is used for various commands in shell mode.
5859 If CURRENT_GROUP is lambda, that means send to the process group
5860 that currently owns the terminal, but only if it is NOT the shell itself.
5862 If NOMSG is zero, insert signal-announcements into process's buffers
5865 If we can, we try to signal PROCESS by sending control characters
5866 down the pty. This allows us to signal inferiors who have changed
5867 their uid, for which killpg would return an EPERM error. */
5870 process_send_signal (Lisp_Object process
, int signo
, Lisp_Object current_group
,
5874 register struct Lisp_Process
*p
;
5878 proc
= get_process (process
);
5879 p
= XPROCESS (proc
);
5881 if (!EQ (p
->type
, Qreal
))
5882 error ("Process %s is not a subprocess",
5885 error ("Process %s is not active",
5889 current_group
= Qnil
;
5891 /* If we are using pgrps, get a pgrp number and make it negative. */
5892 if (NILP (current_group
))
5893 /* Send the signal to the shell's process group. */
5897 #ifdef SIGNALS_VIA_CHARACTERS
5898 /* If possible, send signals to the entire pgrp
5899 by sending an input character to it. */
5902 cc_t
*sig_char
= NULL
;
5904 tcgetattr (p
->infd
, &t
);
5909 sig_char
= &t
.c_cc
[VINTR
];
5913 sig_char
= &t
.c_cc
[VQUIT
];
5917 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5918 sig_char
= &t
.c_cc
[VSWTCH
];
5920 sig_char
= &t
.c_cc
[VSUSP
];
5925 if (sig_char
&& *sig_char
!= CDISABLE
)
5927 send_process (proc
, sig_char
, 1, Qnil
);
5930 /* If we can't send the signal with a character,
5931 fall through and send it another way. */
5933 /* The code above may fall through if it can't
5934 handle the signal. */
5935 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5938 /* Get the current pgrp using the tty itself, if we have that.
5939 Otherwise, use the pty to get the pgrp.
5940 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5941 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5942 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5943 His patch indicates that if TIOCGPGRP returns an error, then
5944 we should just assume that p->pid is also the process group id. */
5946 gid
= emacs_get_tty_pgrp (p
);
5949 /* If we can't get the information, assume
5950 the shell owns the tty. */
5953 /* It is not clear whether anything really can set GID to -1.
5954 Perhaps on some system one of those ioctls can or could do so.
5955 Or perhaps this is vestigial. */
5958 #else /* ! defined (TIOCGPGRP ) */
5959 /* Can't select pgrps on this system, so we know that
5960 the child itself heads the pgrp. */
5962 #endif /* ! defined (TIOCGPGRP ) */
5964 /* If current_group is lambda, and the shell owns the terminal,
5965 don't send any signal. */
5966 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5974 p
->raw_status_new
= 0;
5976 p
->tick
= ++process_tick
;
5979 status_notify (NULL
);
5980 redisplay_preserve_echo_area (13);
5983 #endif /* ! defined (SIGCONT) */
5987 flush_pending_output (p
->infd
);
5991 /* If we don't have process groups, send the signal to the immediate
5992 subprocess. That isn't really right, but it's better than any
5993 obvious alternative. */
5996 kill (p
->pid
, signo
);
6000 /* gid may be a pid, or minus a pgrp's number */
6002 if (!NILP (current_group
))
6004 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6005 EMACS_KILLPG (gid
, signo
);
6012 #else /* ! defined (TIOCSIGSEND) */
6013 EMACS_KILLPG (gid
, signo
);
6014 #endif /* ! defined (TIOCSIGSEND) */
6017 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6018 doc
: /* Interrupt process PROCESS.
6019 PROCESS may be a process, a buffer, or the name of a process or buffer.
6020 No arg or nil means current buffer's process.
6021 Second arg CURRENT-GROUP non-nil means send signal to
6022 the current process-group of the process's controlling terminal
6023 rather than to the process's own process group.
6024 If the process is a shell, this means interrupt current subjob
6025 rather than the shell.
6027 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6028 don't send the signal. */)
6029 (Lisp_Object process
, Lisp_Object current_group
)
6031 process_send_signal (process
, SIGINT
, current_group
, 0);
6035 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6036 doc
: /* Kill process PROCESS. May be process or name of one.
6037 See function `interrupt-process' for more details on usage. */)
6038 (Lisp_Object process
, Lisp_Object current_group
)
6040 process_send_signal (process
, SIGKILL
, current_group
, 0);
6044 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6045 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6046 See function `interrupt-process' for more details on usage. */)
6047 (Lisp_Object process
, Lisp_Object current_group
)
6049 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6053 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6054 doc
: /* Stop process PROCESS. May be process or name of one.
6055 See function `interrupt-process' for more details on usage.
6056 If PROCESS is a network or serial process, inhibit handling of incoming
6058 (Lisp_Object process
, Lisp_Object current_group
)
6060 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6062 struct Lisp_Process
*p
;
6064 p
= XPROCESS (process
);
6065 if (NILP (p
->command
)
6068 FD_CLR (p
->infd
, &input_wait_mask
);
6069 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6075 error ("No SIGTSTP support");
6077 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6082 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6083 doc
: /* Continue process PROCESS. May be process or name of one.
6084 See function `interrupt-process' for more details on usage.
6085 If PROCESS is a network or serial process, resume handling of incoming
6087 (Lisp_Object process
, Lisp_Object current_group
)
6089 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6091 struct Lisp_Process
*p
;
6093 p
= XPROCESS (process
);
6094 if (EQ (p
->command
, Qt
)
6096 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6098 FD_SET (p
->infd
, &input_wait_mask
);
6099 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6101 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6102 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6103 #else /* not WINDOWSNT */
6104 tcflush (p
->infd
, TCIFLUSH
);
6105 #endif /* not WINDOWSNT */
6111 process_send_signal (process
, SIGCONT
, current_group
, 0);
6113 error ("No SIGCONT support");
6118 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6119 2, 2, "sProcess (name or number): \nnSignal code: ",
6120 doc
: /* Send PROCESS the signal with code SIGCODE.
6121 PROCESS may also be a number specifying the process id of the
6122 process to signal; in this case, the process need not be a child of
6124 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6125 (Lisp_Object process
, Lisp_Object sigcode
)
6129 if (INTEGERP (process
))
6131 pid
= XINT (process
);
6135 if (FLOATP (process
))
6137 pid
= (pid_t
) XFLOAT_DATA (process
);
6141 if (STRINGP (process
))
6144 if (tem
= Fget_process (process
), NILP (tem
))
6146 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6153 process
= get_process (process
);
6158 CHECK_PROCESS (process
);
6159 pid
= XPROCESS (process
)->pid
;
6161 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6165 #define parse_signal(NAME, VALUE) \
6166 else if (!xstrcasecmp (name, NAME)) \
6167 XSETINT (sigcode, VALUE)
6169 if (INTEGERP (sigcode
))
6173 unsigned char *name
;
6175 CHECK_SYMBOL (sigcode
);
6176 name
= SDATA (SYMBOL_NAME (sigcode
));
6178 if (!strncmp (name
, "SIG", 3) || !strncmp (name
, "sig", 3))
6184 parse_signal ("usr1", SIGUSR1
);
6187 parse_signal ("usr2", SIGUSR2
);
6190 parse_signal ("term", SIGTERM
);
6193 parse_signal ("hup", SIGHUP
);
6196 parse_signal ("int", SIGINT
);
6199 parse_signal ("quit", SIGQUIT
);
6202 parse_signal ("ill", SIGILL
);
6205 parse_signal ("abrt", SIGABRT
);
6208 parse_signal ("emt", SIGEMT
);
6211 parse_signal ("kill", SIGKILL
);
6214 parse_signal ("fpe", SIGFPE
);
6217 parse_signal ("bus", SIGBUS
);
6220 parse_signal ("segv", SIGSEGV
);
6223 parse_signal ("sys", SIGSYS
);
6226 parse_signal ("pipe", SIGPIPE
);
6229 parse_signal ("alrm", SIGALRM
);
6232 parse_signal ("urg", SIGURG
);
6235 parse_signal ("stop", SIGSTOP
);
6238 parse_signal ("tstp", SIGTSTP
);
6241 parse_signal ("cont", SIGCONT
);
6244 parse_signal ("chld", SIGCHLD
);
6247 parse_signal ("ttin", SIGTTIN
);
6250 parse_signal ("ttou", SIGTTOU
);
6253 parse_signal ("io", SIGIO
);
6256 parse_signal ("xcpu", SIGXCPU
);
6259 parse_signal ("xfsz", SIGXFSZ
);
6262 parse_signal ("vtalrm", SIGVTALRM
);
6265 parse_signal ("prof", SIGPROF
);
6268 parse_signal ("winch", SIGWINCH
);
6271 parse_signal ("info", SIGINFO
);
6274 error ("Undefined signal name %s", name
);
6279 return make_number (kill (pid
, XINT (sigcode
)));
6282 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6283 doc
: /* Make PROCESS see end-of-file in its input.
6284 EOF comes after any text already sent to it.
6285 PROCESS may be a process, a buffer, the name of a process or buffer, or
6286 nil, indicating the current buffer's process.
6287 If PROCESS is a network connection, or is a process communicating
6288 through a pipe (as opposed to a pty), then you cannot send any more
6289 text to PROCESS after you call this function.
6290 If PROCESS is a serial process, wait until all output written to the
6291 process has been transmitted to the serial port. */)
6292 (Lisp_Object process
)
6295 struct coding_system
*coding
;
6297 if (DATAGRAM_CONN_P (process
))
6300 proc
= get_process (process
);
6301 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6303 /* Make sure the process is really alive. */
6304 if (XPROCESS (proc
)->raw_status_new
)
6305 update_status (XPROCESS (proc
));
6306 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6307 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6309 if (CODING_REQUIRE_FLUSHING (coding
))
6311 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6312 send_process (proc
, "", 0, Qnil
);
6315 if (XPROCESS (proc
)->pty_flag
)
6316 send_process (proc
, "\004", 1, Qnil
);
6317 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6320 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6321 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6322 #endif /* not WINDOWSNT */
6323 /* Do nothing on Windows because writes are blocking. */
6327 int old_outfd
, new_outfd
;
6329 #ifdef HAVE_SHUTDOWN
6330 /* If this is a network connection, or socketpair is used
6331 for communication with the subprocess, call shutdown to cause EOF.
6332 (In some old system, shutdown to socketpair doesn't work.
6333 Then we just can't win.) */
6334 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6335 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6336 shutdown (XPROCESS (proc
)->outfd
, 1);
6337 /* In case of socketpair, outfd == infd, so don't close it. */
6338 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6339 emacs_close (XPROCESS (proc
)->outfd
);
6340 #else /* not HAVE_SHUTDOWN */
6341 emacs_close (XPROCESS (proc
)->outfd
);
6342 #endif /* not HAVE_SHUTDOWN */
6343 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6346 old_outfd
= XPROCESS (proc
)->outfd
;
6348 if (!proc_encode_coding_system
[new_outfd
])
6349 proc_encode_coding_system
[new_outfd
]
6350 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6351 memcpy (proc_encode_coding_system
[new_outfd
],
6352 proc_encode_coding_system
[old_outfd
],
6353 sizeof (struct coding_system
));
6354 memset (proc_encode_coding_system
[old_outfd
], 0,
6355 sizeof (struct coding_system
));
6357 XPROCESS (proc
)->outfd
= new_outfd
;
6362 /* On receipt of a signal that a child status has changed, loop asking
6363 about children with changed statuses until the system says there
6366 All we do is change the status; we do not run sentinels or print
6367 notifications. That is saved for the next time keyboard input is
6368 done, in order to avoid timing errors.
6370 ** WARNING: this can be called during garbage collection.
6371 Therefore, it must not be fooled by the presence of mark bits in
6374 ** USG WARNING: Although it is not obvious from the documentation
6375 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6376 signal() before executing at least one wait(), otherwise the
6377 handler will be called again, resulting in an infinite loop. The
6378 relevant portion of the documentation reads "SIGCLD signals will be
6379 queued and the signal-catching function will be continually
6380 reentered until the queue is empty". Invoking signal() causes the
6381 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6384 ** Malloc WARNING: This should never call malloc either directly or
6385 indirectly; if it does, that is a bug */
6389 sigchld_handler (int signo
)
6391 int old_errno
= errno
;
6393 struct Lisp_Process
*p
;
6395 SIGNAL_THREAD_CHECK (signo
);
6406 #endif /* no WUNTRACED */
6407 /* Keep trying to get a status until we get a definitive result. */
6411 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6413 while (pid
< 0 && errno
== EINTR
);
6417 /* PID == 0 means no processes found, PID == -1 means a real
6418 failure. We have done all our job, so return. */
6425 #endif /* no WNOHANG */
6427 /* Find the process that signaled us, and record its status. */
6429 /* The process can have been deleted by Fdelete_process. */
6430 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6432 Lisp_Object xpid
= XCAR (tail
);
6433 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6434 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6436 XSETCAR (tail
, Qnil
);
6437 goto sigchld_end_of_loop
;
6441 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6443 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6445 proc
= XCDR (XCAR (tail
));
6446 p
= XPROCESS (proc
);
6447 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6452 /* Look for an asynchronous process whose pid hasn't been filled
6455 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6457 proc
= XCDR (XCAR (tail
));
6458 p
= XPROCESS (proc
);
6464 /* Change the status of the process that was found. */
6467 int clear_desc_flag
= 0;
6469 p
->tick
= ++process_tick
;
6471 p
->raw_status_new
= 1;
6473 /* If process has terminated, stop waiting for its output. */
6474 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6476 clear_desc_flag
= 1;
6478 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6479 if (clear_desc_flag
)
6481 FD_CLR (p
->infd
, &input_wait_mask
);
6482 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6485 /* Tell wait_reading_process_output that it needs to wake up and
6487 if (input_available_clear_time
)
6488 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6491 /* There was no asynchronous process found for that pid: we have
6492 a synchronous process. */
6495 synch_process_alive
= 0;
6497 /* Report the status of the synchronous process. */
6499 synch_process_retcode
= WRETCODE (w
);
6500 else if (WIFSIGNALED (w
))
6501 synch_process_termsig
= WTERMSIG (w
);
6503 /* Tell wait_reading_process_output that it needs to wake up and
6505 if (input_available_clear_time
)
6506 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6509 sigchld_end_of_loop
:
6512 /* On some systems, we must return right away.
6513 If any more processes want to signal us, we will
6515 Otherwise (on systems that have WNOHANG), loop around
6516 to use up all the processes that have something to tell us. */
6517 #if (defined WINDOWSNT \
6518 || (defined USG && !defined GNU_LINUX \
6519 && !(defined HPUX && defined WNOHANG)))
6522 #endif /* USG, but not HPUX with WNOHANG */
6525 #endif /* SIGCHLD */
6529 exec_sentinel_unwind (Lisp_Object data
)
6531 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6536 exec_sentinel_error_handler (Lisp_Object error
)
6538 cmd_error_internal (error
, "error in process sentinel: ");
6540 update_echo_area ();
6541 Fsleep_for (make_number (2), Qnil
);
6546 exec_sentinel (Lisp_Object proc
, Lisp_Object reason
)
6548 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6549 register struct Lisp_Process
*p
= XPROCESS (proc
);
6550 int count
= SPECPDL_INDEX ();
6551 int outer_running_asynch_code
= running_asynch_code
;
6552 int waiting
= waiting_for_user_input_p
;
6554 if (inhibit_sentinels
)
6557 /* No need to gcpro these, because all we do with them later
6558 is test them for EQness, and none of them should be a string. */
6559 odeactivate
= Vdeactivate_mark
;
6560 XSETBUFFER (obuffer
, current_buffer
);
6561 okeymap
= current_buffer
->keymap
;
6563 /* There's no good reason to let sentinels change the current
6564 buffer, and many callers of accept-process-output, sit-for, and
6565 friends don't expect current-buffer to be changed from under them. */
6566 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
6568 sentinel
= p
->sentinel
;
6569 if (NILP (sentinel
))
6572 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6573 assure that it gets restored no matter how the sentinel exits. */
6575 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6576 /* Inhibit quit so that random quits don't screw up a running filter. */
6577 specbind (Qinhibit_quit
, Qt
);
6578 specbind (Qlast_nonmenu_event
, Qt
); /* Why? --Stef */
6580 /* In case we get recursively called,
6581 and we already saved the match data nonrecursively,
6582 save the same match data in safely recursive fashion. */
6583 if (outer_running_asynch_code
)
6586 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6587 restore_search_regs ();
6588 record_unwind_save_match_data ();
6589 Fset_match_data (tem
, Qt
);
6592 /* For speed, if a search happens within this code,
6593 save the match data in a special nonrecursive fashion. */
6594 running_asynch_code
= 1;
6596 internal_condition_case_1 (read_process_output_call
,
6598 Fcons (proc
, Fcons (reason
, Qnil
))),
6599 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6600 exec_sentinel_error_handler
);
6602 /* If we saved the match data nonrecursively, restore it now. */
6603 restore_search_regs ();
6604 running_asynch_code
= outer_running_asynch_code
;
6606 Vdeactivate_mark
= odeactivate
;
6608 /* Restore waiting_for_user_input_p as it was
6609 when we were called, in case the filter clobbered it. */
6610 waiting_for_user_input_p
= waiting
;
6613 if (! EQ (Fcurrent_buffer (), obuffer
)
6614 || ! EQ (current_buffer
->keymap
, okeymap
))
6616 /* But do it only if the caller is actually going to read events.
6617 Otherwise there's no need to make him wake up, and it could
6618 cause trouble (for example it would make sit_for return). */
6619 if (waiting_for_user_input_p
== -1)
6620 record_asynch_buffer_change ();
6622 unbind_to (count
, Qnil
);
6625 /* Report all recent events of a change in process status
6626 (either run the sentinel or output a message).
6627 This is usually done while Emacs is waiting for keyboard input
6628 but can be done at other times. */
6631 status_notify (struct Lisp_Process
*deleting_process
)
6633 register Lisp_Object proc
, buffer
;
6634 Lisp_Object tail
, msg
;
6635 struct gcpro gcpro1
, gcpro2
;
6639 /* We need to gcpro tail; if read_process_output calls a filter
6640 which deletes a process and removes the cons to which tail points
6641 from Vprocess_alist, and then causes a GC, tail is an unprotected
6645 /* Set this now, so that if new processes are created by sentinels
6646 that we run, we get called again to handle their status changes. */
6647 update_tick
= process_tick
;
6649 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6652 register struct Lisp_Process
*p
;
6654 proc
= Fcdr (XCAR (tail
));
6655 p
= XPROCESS (proc
);
6657 if (p
->tick
!= p
->update_tick
)
6659 p
->update_tick
= p
->tick
;
6661 /* If process is still active, read any output that remains. */
6662 while (! EQ (p
->filter
, Qt
)
6663 && ! EQ (p
->status
, Qconnect
)
6664 && ! EQ (p
->status
, Qlisten
)
6665 /* Network or serial process not stopped: */
6666 && ! EQ (p
->command
, Qt
)
6668 && p
!= deleting_process
6669 && read_process_output (proc
, p
->infd
) > 0);
6673 /* Get the text to use for the message. */
6674 if (p
->raw_status_new
)
6676 msg
= status_message (p
);
6678 /* If process is terminated, deactivate it or delete it. */
6680 if (CONSP (p
->status
))
6681 symbol
= XCAR (p
->status
);
6683 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6684 || EQ (symbol
, Qclosed
))
6686 if (delete_exited_processes
)
6687 remove_process (proc
);
6689 deactivate_process (proc
);
6692 /* The actions above may have further incremented p->tick.
6693 So set p->update_tick again
6694 so that an error in the sentinel will not cause
6695 this code to be run again. */
6696 p
->update_tick
= p
->tick
;
6697 /* Now output the message suitably. */
6698 if (!NILP (p
->sentinel
))
6699 exec_sentinel (proc
, msg
);
6700 /* Don't bother with a message in the buffer
6701 when a process becomes runnable. */
6702 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6705 struct buffer
*old
= current_buffer
;
6706 EMACS_INT opoint
, opoint_byte
;
6707 EMACS_INT before
, before_byte
;
6709 /* Avoid error if buffer is deleted
6710 (probably that's why the process is dead, too) */
6711 if (NILP (XBUFFER (buffer
)->name
))
6713 Fset_buffer (buffer
);
6716 opoint_byte
= PT_BYTE
;
6717 /* Insert new output into buffer
6718 at the current end-of-output marker,
6719 thus preserving logical ordering of input and output. */
6720 if (XMARKER (p
->mark
)->buffer
)
6721 Fgoto_char (p
->mark
);
6723 SET_PT_BOTH (ZV
, ZV_BYTE
);
6726 before_byte
= PT_BYTE
;
6728 tem
= current_buffer
->read_only
;
6729 current_buffer
->read_only
= Qnil
;
6730 insert_string ("\nProcess ");
6731 Finsert (1, &p
->name
);
6732 insert_string (" ");
6734 current_buffer
->read_only
= tem
;
6735 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6737 if (opoint
>= before
)
6738 SET_PT_BOTH (opoint
+ (PT
- before
),
6739 opoint_byte
+ (PT_BYTE
- before_byte
));
6741 SET_PT_BOTH (opoint
, opoint_byte
);
6743 set_buffer_internal (old
);
6748 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6753 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6754 Sset_process_coding_system
, 1, 3, 0,
6755 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6756 DECODING will be used to decode subprocess output and ENCODING to
6757 encode subprocess input. */)
6758 (register Lisp_Object process
, Lisp_Object decoding
, Lisp_Object encoding
)
6760 register struct Lisp_Process
*p
;
6762 CHECK_PROCESS (process
);
6763 p
= XPROCESS (process
);
6765 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6767 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6768 Fcheck_coding_system (decoding
);
6769 Fcheck_coding_system (encoding
);
6770 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6771 p
->decode_coding_system
= decoding
;
6772 p
->encode_coding_system
= encoding
;
6773 setup_process_coding_systems (process
);
6778 DEFUN ("process-coding-system",
6779 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6780 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6781 (register Lisp_Object process
)
6783 CHECK_PROCESS (process
);
6784 return Fcons (XPROCESS (process
)->decode_coding_system
,
6785 XPROCESS (process
)->encode_coding_system
);
6788 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6789 Sset_process_filter_multibyte
, 2, 2, 0,
6790 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6791 If FLAG is non-nil, the filter is given multibyte strings.
6792 If FLAG is nil, the filter is given unibyte strings. In this case,
6793 all character code conversion except for end-of-line conversion is
6795 (Lisp_Object process
, Lisp_Object flag
)
6797 register struct Lisp_Process
*p
;
6799 CHECK_PROCESS (process
);
6800 p
= XPROCESS (process
);
6802 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
6803 setup_process_coding_systems (process
);
6808 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6809 Sprocess_filter_multibyte_p
, 1, 1, 0,
6810 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6811 (Lisp_Object process
)
6813 register struct Lisp_Process
*p
;
6814 struct coding_system
*coding
;
6816 CHECK_PROCESS (process
);
6817 p
= XPROCESS (process
);
6818 coding
= proc_decode_coding_system
[p
->infd
];
6819 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6826 add_gpm_wait_descriptor (int desc
)
6828 add_keyboard_wait_descriptor (desc
);
6832 delete_gpm_wait_descriptor (int desc
)
6834 delete_keyboard_wait_descriptor (desc
);
6837 /* Return nonzero if *MASK has a bit set
6838 that corresponds to one of the keyboard input descriptors. */
6841 keyboard_bit_set (fd_set
*mask
)
6845 for (fd
= 0; fd
<= max_input_desc
; fd
++)
6846 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6847 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6853 #else /* not subprocesses */
6855 /* Defined on msdos.c. */
6856 extern int sys_select (int, SELECT_TYPE
*, SELECT_TYPE
*, SELECT_TYPE
*,
6859 /* Implementation of wait_reading_process_output, assuming that there
6860 are no subprocesses. Used only by the MS-DOS build.
6862 Wait for timeout to elapse and/or keyboard input to be available.
6865 timeout in seconds, or
6866 zero for no limit, or
6867 -1 means gobble data immediately available but don't wait for any.
6869 read_kbd is a Lisp_Object:
6870 0 to ignore keyboard input, or
6871 1 to return when input is available, or
6872 -1 means caller will actually read the input, so don't throw to
6875 see full version for other parameters. We know that wait_proc will
6876 always be NULL, since `subprocesses' isn't defined.
6878 do_display != 0 means redisplay should be done to show subprocess
6879 output that arrives.
6881 Return true if we received input from any process. */
6884 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
6886 Lisp_Object wait_for_cell
,
6887 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
6890 EMACS_TIME end_time
, timeout
;
6891 SELECT_TYPE waitchannels
;
6894 /* What does time_limit really mean? */
6895 if (time_limit
|| microsecs
)
6897 EMACS_GET_TIME (end_time
);
6898 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
6899 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
6902 /* Turn off periodic alarms (in case they are in use)
6903 and then turn off any other atimers,
6904 because the select emulator uses alarms. */
6906 turn_on_atimers (0);
6910 int timeout_reduced_for_timers
= 0;
6912 /* If calling from keyboard input, do not quit
6913 since we want to return C-g as an input character.
6914 Otherwise, do pending quit if requested. */
6918 /* Exit now if the cell we're waiting for became non-nil. */
6919 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6922 /* Compute time from now till when time limit is up */
6923 /* Exit if already run out */
6924 if (time_limit
== -1)
6926 /* -1 specified for timeout means
6927 gobble output available now
6928 but don't wait at all. */
6930 EMACS_SET_SECS_USECS (timeout
, 0, 0);
6932 else if (time_limit
|| microsecs
)
6934 EMACS_GET_TIME (timeout
);
6935 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
6936 if (EMACS_TIME_NEG_P (timeout
))
6941 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
6944 /* If our caller will not immediately handle keyboard events,
6945 run timer events directly.
6946 (Callers that will immediately read keyboard events
6947 call timer_delay on their own.) */
6948 if (NILP (wait_for_cell
))
6950 EMACS_TIME timer_delay
;
6954 int old_timers_run
= timers_run
;
6955 timer_delay
= timer_check (1);
6956 if (timers_run
!= old_timers_run
&& do_display
)
6957 /* We must retry, since a timer may have requeued itself
6958 and that could alter the time delay. */
6959 redisplay_preserve_echo_area (14);
6963 while (!detect_input_pending ());
6965 /* If there is unread keyboard input, also return. */
6967 && requeued_events_pending_p ())
6970 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
6972 EMACS_TIME difference
;
6973 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
6974 if (EMACS_TIME_NEG_P (difference
))
6976 timeout
= timer_delay
;
6977 timeout_reduced_for_timers
= 1;
6982 /* Cause C-g and alarm signals to take immediate action,
6983 and cause input available signals to zero out timeout. */
6985 set_waiting_for_input (&timeout
);
6987 /* Wait till there is something to do. */
6989 if (! read_kbd
&& NILP (wait_for_cell
))
6990 FD_ZERO (&waitchannels
);
6992 FD_SET (0, &waitchannels
);
6994 /* If a frame has been newly mapped and needs updating,
6995 reprocess its display stuff. */
6996 if (frame_garbaged
&& do_display
)
6998 clear_waiting_for_input ();
6999 redisplay_preserve_echo_area (15);
7001 set_waiting_for_input (&timeout
);
7004 if (read_kbd
&& detect_input_pending ())
7007 FD_ZERO (&waitchannels
);
7010 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7015 /* Make C-g and alarm signals set flags again */
7016 clear_waiting_for_input ();
7018 /* If we woke up due to SIGWINCH, actually change size now. */
7019 do_pending_window_change (0);
7021 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7022 /* We waited the full specified time, so return now. */
7027 /* If the system call was interrupted, then go around the
7029 if (xerrno
== EINTR
)
7030 FD_ZERO (&waitchannels
);
7032 error ("select error: %s", emacs_strerror (xerrno
));
7035 /* Check for keyboard input */
7038 && detect_input_pending_run_timers (do_display
))
7040 swallow_events (do_display
);
7041 if (detect_input_pending_run_timers (do_display
))
7045 /* If there is unread keyboard input, also return. */
7047 && requeued_events_pending_p ())
7050 /* If wait_for_cell. check for keyboard input
7051 but don't run any timers.
7052 ??? (It seems wrong to me to check for keyboard
7053 input at all when wait_for_cell, but the code
7054 has been this way since July 1994.
7055 Try changing this after version 19.31.) */
7056 if (! NILP (wait_for_cell
)
7057 && detect_input_pending ())
7059 swallow_events (do_display
);
7060 if (detect_input_pending ())
7064 /* Exit now if the cell we're waiting for became non-nil. */
7065 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7074 #endif /* not subprocesses */
7076 /* The following functions are needed even if async subprocesses are
7077 not supported. Some of them are no-op stubs in that case. */
7079 /* Add DESC to the set of keyboard input descriptors. */
7082 add_keyboard_wait_descriptor (int desc
)
7084 #ifdef subprocesses /* actually means "not MSDOS" */
7085 FD_SET (desc
, &input_wait_mask
);
7086 FD_SET (desc
, &non_process_wait_mask
);
7087 if (desc
> max_input_desc
)
7088 max_input_desc
= desc
;
7092 /* From now on, do not expect DESC to give keyboard input. */
7095 delete_keyboard_wait_descriptor (int desc
)
7099 int lim
= max_input_desc
;
7101 FD_CLR (desc
, &input_wait_mask
);
7102 FD_CLR (desc
, &non_process_wait_mask
);
7104 if (desc
== max_input_desc
)
7105 for (fd
= 0; fd
< lim
; fd
++)
7106 if (FD_ISSET (fd
, &input_wait_mask
) || FD_ISSET (fd
, &write_mask
))
7107 max_input_desc
= fd
;
7111 /* Setup coding systems of PROCESS. */
7114 setup_process_coding_systems (Lisp_Object process
)
7117 struct Lisp_Process
*p
= XPROCESS (process
);
7119 int outch
= p
->outfd
;
7120 Lisp_Object coding_system
;
7122 if (inch
< 0 || outch
< 0)
7125 if (!proc_decode_coding_system
[inch
])
7126 proc_decode_coding_system
[inch
]
7127 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7128 coding_system
= p
->decode_coding_system
;
7129 if (! NILP (p
->filter
))
7131 else if (BUFFERP (p
->buffer
))
7133 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
7134 coding_system
= raw_text_coding_system (coding_system
);
7136 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
7138 if (!proc_encode_coding_system
[outch
])
7139 proc_encode_coding_system
[outch
]
7140 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7141 setup_coding_system (p
->encode_coding_system
,
7142 proc_encode_coding_system
[outch
]);
7146 /* Close all descriptors currently in use for communication
7147 with subprocess. This is used in a newly-forked subprocess
7148 to get rid of irrelevant descriptors. */
7151 close_process_descs (void)
7155 for (i
= 0; i
< MAXDESC
; i
++)
7157 Lisp_Object process
;
7158 process
= chan_process
[i
];
7159 if (!NILP (process
))
7161 int in
= XPROCESS (process
)->infd
;
7162 int out
= XPROCESS (process
)->outfd
;
7165 if (out
>= 0 && in
!= out
)
7172 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7173 doc
: /* Return the (or a) process associated with BUFFER.
7174 BUFFER may be a buffer or the name of one. */)
7175 (register Lisp_Object buffer
)
7178 register Lisp_Object buf
, tail
, proc
;
7180 if (NILP (buffer
)) return Qnil
;
7181 buf
= Fget_buffer (buffer
);
7182 if (NILP (buf
)) return Qnil
;
7184 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7186 proc
= Fcdr (XCAR (tail
));
7187 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
7190 #endif /* subprocesses */
7194 DEFUN ("process-inherit-coding-system-flag",
7195 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7197 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
7198 If this flag is t, `buffer-file-coding-system' of the buffer
7199 associated with PROCESS will inherit the coding system used to decode
7200 the process output. */)
7201 (register Lisp_Object process
)
7204 CHECK_PROCESS (process
);
7205 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
7207 /* Ignore the argument and return the value of
7208 inherit-process-coding-system. */
7209 return inherit_process_coding_system
? Qt
: Qnil
;
7213 /* Kill all processes associated with `buffer'.
7214 If `buffer' is nil, kill all processes */
7217 kill_buffer_processes (Lisp_Object buffer
)
7220 Lisp_Object tail
, proc
;
7222 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7224 proc
= XCDR (XCAR (tail
));
7226 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
7228 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
7229 Fdelete_process (proc
);
7230 else if (XPROCESS (proc
)->infd
>= 0)
7231 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
7234 #else /* subprocesses */
7235 /* Since we have no subprocesses, this does nothing. */
7236 #endif /* subprocesses */
7239 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
7241 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
7242 This is intended for use by asynchronous process output filters and sentinels. */)
7246 return (waiting_for_user_input_p
? Qt
: Qnil
);
7252 /* Stop reading input from keyboard sources. */
7255 hold_keyboard_input (void)
7260 /* Resume reading input from keyboard sources. */
7263 unhold_keyboard_input (void)
7268 /* Return non-zero if keyboard input is on hold, zero otherwise. */
7271 kbd_on_hold_p (void)
7273 return kbd_is_on_hold
;
7277 /* Enumeration of and access to system processes a-la ps(1). */
7279 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7281 doc
: /* Return a list of numerical process IDs of all running processes.
7282 If this functionality is unsupported, return nil.
7284 See `process-attributes' for getting attributes of a process given its ID. */)
7287 return list_system_processes ();
7290 DEFUN ("process-attributes", Fprocess_attributes
,
7291 Sprocess_attributes
, 1, 1, 0,
7292 doc
: /* Return attributes of the process given by its PID, a number.
7294 Value is an alist where each element is a cons cell of the form
7298 If this functionality is unsupported, the value is nil.
7300 See `list-system-processes' for getting a list of all process IDs.
7302 The KEYs of the attributes that this function may return are listed
7303 below, together with the type of the associated VALUE (in parentheses).
7304 Not all platforms support all of these attributes; unsupported
7305 attributes will not appear in the returned alist.
7306 Unless explicitly indicated otherwise, numbers can have either
7307 integer or floating point values.
7309 euid -- Effective user User ID of the process (number)
7310 user -- User name corresponding to euid (string)
7311 egid -- Effective user Group ID of the process (number)
7312 group -- Group name corresponding to egid (string)
7313 comm -- Command name (executable name only) (string)
7314 state -- Process state code, such as "S", "R", or "T" (string)
7315 ppid -- Parent process ID (number)
7316 pgrp -- Process group ID (number)
7317 sess -- Session ID, i.e. process ID of session leader (number)
7318 ttname -- Controlling tty name (string)
7319 tpgid -- ID of foreground process group on the process's tty (number)
7320 minflt -- number of minor page faults (number)
7321 majflt -- number of major page faults (number)
7322 cminflt -- cumulative number of minor page faults (number)
7323 cmajflt -- cumulative number of major page faults (number)
7324 utime -- user time used by the process, in the (HIGH LOW USEC) format
7325 stime -- system time used by the process, in the (HIGH LOW USEC) format
7326 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7327 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7328 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7329 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7330 pri -- priority of the process (number)
7331 nice -- nice value of the process (number)
7332 thcount -- process thread count (number)
7333 start -- time the process started, in the (HIGH LOW USEC) format
7334 vsize -- virtual memory size of the process in KB's (number)
7335 rss -- resident set size of the process in KB's (number)
7336 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7337 pcpu -- percents of CPU time used by the process (floating-point number)
7338 pmem -- percents of total physical memory used by process's resident set
7339 (floating-point number)
7340 args -- command line which invoked the process (string). */)
7343 return system_process_attributes (pid
);
7353 inhibit_sentinels
= 0;
7357 if (! noninteractive
|| initialized
)
7359 signal (SIGCHLD
, sigchld_handler
);
7362 FD_ZERO (&input_wait_mask
);
7363 FD_ZERO (&non_keyboard_wait_mask
);
7364 FD_ZERO (&non_process_wait_mask
);
7365 FD_ZERO (&write_mask
);
7366 max_process_desc
= 0;
7367 memset (fd_callback_info
, 0, sizeof (fd_callback_info
));
7369 #ifdef NON_BLOCKING_CONNECT
7370 FD_ZERO (&connect_wait_mask
);
7371 num_pending_connects
= 0;
7374 #ifdef ADAPTIVE_READ_BUFFERING
7375 process_output_delay_count
= 0;
7376 process_output_skip
= 0;
7379 /* Don't do this, it caused infinite select loops. The display
7380 method should call add_keyboard_wait_descriptor on stdin if it
7383 FD_SET (0, &input_wait_mask
);
7386 Vprocess_alist
= Qnil
;
7388 deleted_pid_list
= Qnil
;
7390 for (i
= 0; i
< MAXDESC
; i
++)
7392 chan_process
[i
] = Qnil
;
7393 proc_buffered_char
[i
] = -1;
7395 memset (proc_decode_coding_system
, 0, sizeof proc_decode_coding_system
);
7396 memset (proc_encode_coding_system
, 0, sizeof proc_encode_coding_system
);
7397 #ifdef DATAGRAM_SOCKETS
7398 memset (datagram_address
, 0, sizeof datagram_address
);
7402 Lisp_Object subfeatures
= Qnil
;
7403 const struct socket_options
*sopt
;
7405 #define ADD_SUBFEATURE(key, val) \
7406 subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)
7408 #ifdef NON_BLOCKING_CONNECT
7409 ADD_SUBFEATURE (QCnowait
, Qt
);
7411 #ifdef DATAGRAM_SOCKETS
7412 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7414 #ifdef HAVE_SEQPACKET
7415 ADD_SUBFEATURE (QCtype
, Qseqpacket
);
7417 #ifdef HAVE_LOCAL_SOCKETS
7418 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7420 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7422 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7424 #ifdef HAVE_GETSOCKNAME
7425 ADD_SUBFEATURE (QCservice
, Qt
);
7427 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7428 ADD_SUBFEATURE (QCserver
, Qt
);
7431 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7432 subfeatures
= pure_cons (intern_c_string (sopt
->name
), subfeatures
);
7434 Fprovide (intern_c_string ("make-network-process"), subfeatures
);
7437 #if defined (DARWIN_OS)
7438 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7439 processes. As such, we only change the default value. */
7442 const char *release
= get_operating_system_release ();
7443 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7444 && release
[1] == '.')) {
7445 Vprocess_connection_type
= Qnil
;
7449 #endif /* subprocesses */
7454 syms_of_process (void)
7458 Qprocessp
= intern_c_string ("processp");
7459 staticpro (&Qprocessp
);
7460 Qrun
= intern_c_string ("run");
7462 Qstop
= intern_c_string ("stop");
7464 Qsignal
= intern_c_string ("signal");
7465 staticpro (&Qsignal
);
7467 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7470 Qexit = intern_c_string ("exit");
7471 staticpro (&Qexit); */
7473 Qopen
= intern_c_string ("open");
7475 Qclosed
= intern_c_string ("closed");
7476 staticpro (&Qclosed
);
7477 Qconnect
= intern_c_string ("connect");
7478 staticpro (&Qconnect
);
7479 Qfailed
= intern_c_string ("failed");
7480 staticpro (&Qfailed
);
7481 Qlisten
= intern_c_string ("listen");
7482 staticpro (&Qlisten
);
7483 Qlocal
= intern_c_string ("local");
7484 staticpro (&Qlocal
);
7485 Qipv4
= intern_c_string ("ipv4");
7488 Qipv6
= intern_c_string ("ipv6");
7491 Qdatagram
= intern_c_string ("datagram");
7492 staticpro (&Qdatagram
);
7493 Qseqpacket
= intern_c_string ("seqpacket");
7494 staticpro (&Qseqpacket
);
7496 QCport
= intern_c_string (":port");
7497 staticpro (&QCport
);
7498 QCspeed
= intern_c_string (":speed");
7499 staticpro (&QCspeed
);
7500 QCprocess
= intern_c_string (":process");
7501 staticpro (&QCprocess
);
7503 QCbytesize
= intern_c_string (":bytesize");
7504 staticpro (&QCbytesize
);
7505 QCstopbits
= intern_c_string (":stopbits");
7506 staticpro (&QCstopbits
);
7507 QCparity
= intern_c_string (":parity");
7508 staticpro (&QCparity
);
7509 Qodd
= intern_c_string ("odd");
7511 Qeven
= intern_c_string ("even");
7513 QCflowcontrol
= intern_c_string (":flowcontrol");
7514 staticpro (&QCflowcontrol
);
7515 Qhw
= intern_c_string ("hw");
7517 Qsw
= intern_c_string ("sw");
7519 QCsummary
= intern_c_string (":summary");
7520 staticpro (&QCsummary
);
7522 Qreal
= intern_c_string ("real");
7524 Qnetwork
= intern_c_string ("network");
7525 staticpro (&Qnetwork
);
7526 Qserial
= intern_c_string ("serial");
7527 staticpro (&Qserial
);
7528 QCbuffer
= intern_c_string (":buffer");
7529 staticpro (&QCbuffer
);
7530 QChost
= intern_c_string (":host");
7531 staticpro (&QChost
);
7532 QCservice
= intern_c_string (":service");
7533 staticpro (&QCservice
);
7534 QClocal
= intern_c_string (":local");
7535 staticpro (&QClocal
);
7536 QCremote
= intern_c_string (":remote");
7537 staticpro (&QCremote
);
7538 QCcoding
= intern_c_string (":coding");
7539 staticpro (&QCcoding
);
7540 QCserver
= intern_c_string (":server");
7541 staticpro (&QCserver
);
7542 QCnowait
= intern_c_string (":nowait");
7543 staticpro (&QCnowait
);
7544 QCsentinel
= intern_c_string (":sentinel");
7545 staticpro (&QCsentinel
);
7546 QClog
= intern_c_string (":log");
7548 QCnoquery
= intern_c_string (":noquery");
7549 staticpro (&QCnoquery
);
7550 QCstop
= intern_c_string (":stop");
7551 staticpro (&QCstop
);
7552 QCoptions
= intern_c_string (":options");
7553 staticpro (&QCoptions
);
7554 QCplist
= intern_c_string (":plist");
7555 staticpro (&QCplist
);
7557 Qlast_nonmenu_event
= intern_c_string ("last-nonmenu-event");
7558 staticpro (&Qlast_nonmenu_event
);
7560 staticpro (&Vprocess_alist
);
7562 staticpro (&deleted_pid_list
);
7565 #endif /* subprocesses */
7567 QCname
= intern_c_string (":name");
7568 staticpro (&QCname
);
7569 QCtype
= intern_c_string (":type");
7570 staticpro (&QCtype
);
7572 Qeuid
= intern_c_string ("euid");
7574 Qegid
= intern_c_string ("egid");
7576 Quser
= intern_c_string ("user");
7578 Qgroup
= intern_c_string ("group");
7579 staticpro (&Qgroup
);
7580 Qcomm
= intern_c_string ("comm");
7582 Qstate
= intern_c_string ("state");
7583 staticpro (&Qstate
);
7584 Qppid
= intern_c_string ("ppid");
7586 Qpgrp
= intern_c_string ("pgrp");
7588 Qsess
= intern_c_string ("sess");
7590 Qttname
= intern_c_string ("ttname");
7591 staticpro (&Qttname
);
7592 Qtpgid
= intern_c_string ("tpgid");
7593 staticpro (&Qtpgid
);
7594 Qminflt
= intern_c_string ("minflt");
7595 staticpro (&Qminflt
);
7596 Qmajflt
= intern_c_string ("majflt");
7597 staticpro (&Qmajflt
);
7598 Qcminflt
= intern_c_string ("cminflt");
7599 staticpro (&Qcminflt
);
7600 Qcmajflt
= intern_c_string ("cmajflt");
7601 staticpro (&Qcmajflt
);
7602 Qutime
= intern_c_string ("utime");
7603 staticpro (&Qutime
);
7604 Qstime
= intern_c_string ("stime");
7605 staticpro (&Qstime
);
7606 Qtime
= intern_c_string ("time");
7608 Qcutime
= intern_c_string ("cutime");
7609 staticpro (&Qcutime
);
7610 Qcstime
= intern_c_string ("cstime");
7611 staticpro (&Qcstime
);
7612 Qctime
= intern_c_string ("ctime");
7613 staticpro (&Qctime
);
7614 Qpri
= intern_c_string ("pri");
7616 Qnice
= intern_c_string ("nice");
7618 Qthcount
= intern_c_string ("thcount");
7619 staticpro (&Qthcount
);
7620 Qstart
= intern_c_string ("start");
7621 staticpro (&Qstart
);
7622 Qvsize
= intern_c_string ("vsize");
7623 staticpro (&Qvsize
);
7624 Qrss
= intern_c_string ("rss");
7626 Qetime
= intern_c_string ("etime");
7627 staticpro (&Qetime
);
7628 Qpcpu
= intern_c_string ("pcpu");
7630 Qpmem
= intern_c_string ("pmem");
7632 Qargs
= intern_c_string ("args");
7635 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7636 doc
: /* *Non-nil means delete processes immediately when they exit.
7637 A value of nil means don't delete them until `list-processes' is run. */);
7639 delete_exited_processes
= 1;
7642 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7643 doc
: /* Control type of device used to communicate with subprocesses.
7644 Values are nil to use a pipe, or t or `pty' to use a pty.
7645 The value has no effect if the system has no ptys or if all ptys are busy:
7646 then a pipe is used in any case.
7647 The value takes effect when `start-process' is called. */);
7648 Vprocess_connection_type
= Qt
;
7650 #ifdef ADAPTIVE_READ_BUFFERING
7651 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7652 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7653 On some systems, when Emacs reads the output from a subprocess, the output data
7654 is read in very small blocks, potentially resulting in very poor performance.
7655 This behavior can be remedied to some extent by setting this variable to a
7656 non-nil value, as it will automatically delay reading from such processes, to
7657 allow them to produce more output before Emacs tries to read it.
7658 If the value is t, the delay is reset after each write to the process; any other
7659 non-nil value means that the delay is not reset on write.
7660 The variable takes effect when `start-process' is called. */);
7661 Vprocess_adaptive_read_buffering
= Qt
;
7664 defsubr (&Sprocessp
);
7665 defsubr (&Sget_process
);
7666 defsubr (&Sdelete_process
);
7667 defsubr (&Sprocess_status
);
7668 defsubr (&Sprocess_exit_status
);
7669 defsubr (&Sprocess_id
);
7670 defsubr (&Sprocess_name
);
7671 defsubr (&Sprocess_tty_name
);
7672 defsubr (&Sprocess_command
);
7673 defsubr (&Sset_process_buffer
);
7674 defsubr (&Sprocess_buffer
);
7675 defsubr (&Sprocess_mark
);
7676 defsubr (&Sset_process_filter
);
7677 defsubr (&Sprocess_filter
);
7678 defsubr (&Sset_process_sentinel
);
7679 defsubr (&Sprocess_sentinel
);
7680 defsubr (&Sset_process_window_size
);
7681 defsubr (&Sset_process_inherit_coding_system_flag
);
7682 defsubr (&Sset_process_query_on_exit_flag
);
7683 defsubr (&Sprocess_query_on_exit_flag
);
7684 defsubr (&Sprocess_contact
);
7685 defsubr (&Sprocess_plist
);
7686 defsubr (&Sset_process_plist
);
7687 defsubr (&Slist_processes
);
7688 defsubr (&Sprocess_list
);
7689 defsubr (&Sstart_process
);
7690 defsubr (&Sserial_process_configure
);
7691 defsubr (&Smake_serial_process
);
7692 defsubr (&Sset_network_process_option
);
7693 defsubr (&Smake_network_process
);
7694 defsubr (&Sformat_network_address
);
7695 #if defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7697 defsubr (&Snetwork_interface_list
);
7699 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7700 defsubr (&Snetwork_interface_info
);
7702 #endif /* defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H) */
7703 #ifdef DATAGRAM_SOCKETS
7704 defsubr (&Sprocess_datagram_address
);
7705 defsubr (&Sset_process_datagram_address
);
7707 defsubr (&Saccept_process_output
);
7708 defsubr (&Sprocess_send_region
);
7709 defsubr (&Sprocess_send_string
);
7710 defsubr (&Sinterrupt_process
);
7711 defsubr (&Skill_process
);
7712 defsubr (&Squit_process
);
7713 defsubr (&Sstop_process
);
7714 defsubr (&Scontinue_process
);
7715 defsubr (&Sprocess_running_child_p
);
7716 defsubr (&Sprocess_send_eof
);
7717 defsubr (&Ssignal_process
);
7718 defsubr (&Swaiting_for_user_input_p
);
7719 defsubr (&Sprocess_type
);
7720 defsubr (&Sset_process_coding_system
);
7721 defsubr (&Sprocess_coding_system
);
7722 defsubr (&Sset_process_filter_multibyte
);
7723 defsubr (&Sprocess_filter_multibyte_p
);
7725 #endif /* subprocesses */
7727 defsubr (&Sget_buffer_process
);
7728 defsubr (&Sprocess_inherit_coding_system_flag
);
7729 defsubr (&Slist_system_processes
);
7730 defsubr (&Sprocess_attributes
);
7733 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7734 (do not change this comment) */