1 /* Asynchronous subprocess control for GNU Emacs.
3 Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2011
4 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
38 /* Only MS-DOS does not define `subprocesses'. */
41 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 /* Are local (unix) sockets supported? */
47 #if defined (HAVE_SYS_UN_H)
48 #if !defined (AF_LOCAL) && defined (AF_UNIX)
49 #define AF_LOCAL AF_UNIX
52 #define HAVE_LOCAL_SOCKETS
57 #include <sys/ioctl.h>
58 #if defined(HAVE_NET_IF_H)
60 #endif /* HAVE_NET_IF_H */
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
80 #endif /* subprocesses */
88 #include "character.h"
92 #include "termhooks.h"
96 #include "blockinput.h"
97 #include "dispextern.h"
98 #include "composite.h"
100 #include "sysselect.h"
101 #include "syssignal.h"
107 #if defined (USE_GTK) || defined (HAVE_GCONF)
108 #include "xgselect.h"
109 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
114 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
115 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
116 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
117 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
118 Lisp_Object QCname
, QCtype
;
120 /* Non-zero if keyboard input is on hold, zero otherwise. */
122 static int kbd_is_on_hold
;
124 /* Nonzero means don't run process sentinels. This is used
126 int inhibit_sentinels
;
130 Lisp_Object Qprocessp
;
131 Lisp_Object Qrun
, Qstop
, Qsignal
;
132 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
133 Lisp_Object Qlocal
, Qipv4
, Qdatagram
, Qseqpacket
;
134 Lisp_Object Qreal
, Qnetwork
, Qserial
;
138 Lisp_Object QCport
, QCspeed
, QCprocess
;
139 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
140 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
141 Lisp_Object QCbuffer
, QChost
, QCservice
;
142 Lisp_Object QClocal
, QCremote
, QCcoding
;
143 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
144 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
145 Lisp_Object Qlast_nonmenu_event
;
146 /* QCfamily is declared and initialized in xfaces.c,
147 QCfilter in keyboard.c. */
148 extern Lisp_Object QCfamily
, QCfilter
;
150 /* Qexit is declared and initialized in eval.c. */
152 /* QCfamily is defined in xfaces.c. */
153 extern Lisp_Object QCfamily
;
154 /* QCfilter is defined in keyboard.c. */
155 extern Lisp_Object QCfilter
;
157 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
158 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
159 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
160 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
162 /* Define first descriptor number available for subprocesses. */
163 #define FIRST_PROC_DESC 3
169 /* These next two vars are non-static since sysdep.c uses them in the
170 emulation of `select'. */
171 /* Number of events of change of status of a process. */
173 /* Number of events for which the user or sentinel has been notified. */
176 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
178 /* Only W32 has this, it really means that select can't take write mask. */
179 #ifdef BROKEN_NON_BLOCKING_CONNECT
180 #undef NON_BLOCKING_CONNECT
181 #define SELECT_CANT_DO_WRITE_MASK
183 #ifndef NON_BLOCKING_CONNECT
185 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
186 #if defined (O_NONBLOCK) || defined (O_NDELAY)
187 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
188 #define NON_BLOCKING_CONNECT
189 #endif /* EWOULDBLOCK || EINPROGRESS */
190 #endif /* O_NONBLOCK || O_NDELAY */
191 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
192 #endif /* HAVE_SELECT */
193 #endif /* NON_BLOCKING_CONNECT */
194 #endif /* BROKEN_NON_BLOCKING_CONNECT */
196 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
197 this system. We need to read full packets, so we need a
198 "non-destructive" select. So we require either native select,
199 or emulation of select using FIONREAD. */
201 #ifdef BROKEN_DATAGRAM_SOCKETS
202 #undef DATAGRAM_SOCKETS
204 #ifndef DATAGRAM_SOCKETS
205 #if defined (HAVE_SELECT) || defined (FIONREAD)
206 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
207 #define DATAGRAM_SOCKETS
208 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
209 #endif /* HAVE_SELECT || FIONREAD */
210 #endif /* DATAGRAM_SOCKETS */
211 #endif /* BROKEN_DATAGRAM_SOCKETS */
213 #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
214 # define HAVE_SEQPACKET
217 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
218 #ifdef EMACS_HAS_USECS
219 #define ADAPTIVE_READ_BUFFERING
223 #ifdef ADAPTIVE_READ_BUFFERING
224 #define READ_OUTPUT_DELAY_INCREMENT 10000
225 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
226 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
228 /* Number of processes which have a non-zero read_output_delay,
229 and therefore might be delayed for adaptive read buffering. */
231 static int process_output_delay_count
;
233 /* Non-zero if any process has non-nil read_output_skip. */
235 static int process_output_skip
;
238 #define process_output_delay_count 0
241 static int keyboard_bit_set (SELECT_TYPE
*);
242 static void deactivate_process (Lisp_Object
);
243 static void status_notify (struct Lisp_Process
*);
244 static int read_process_output (Lisp_Object
, int);
245 static void create_pty (Lisp_Object
);
247 /* If we support a window system, turn on the code to poll periodically
248 to detect C-g. It isn't actually used when doing interrupt input. */
249 #ifdef HAVE_WINDOW_SYSTEM
250 #define POLL_FOR_INPUT
253 static Lisp_Object
get_process (register Lisp_Object name
);
254 static void exec_sentinel (Lisp_Object proc
, Lisp_Object reason
);
256 /* Mask of bits indicating the descriptors that we wait for input on. */
258 static SELECT_TYPE input_wait_mask
;
260 /* Mask that excludes keyboard input descriptor(s). */
262 static SELECT_TYPE non_keyboard_wait_mask
;
264 /* Mask that excludes process input descriptor(s). */
266 static SELECT_TYPE non_process_wait_mask
;
268 /* Mask for selecting for write. */
270 static SELECT_TYPE write_mask
;
272 #ifdef NON_BLOCKING_CONNECT
273 /* Mask of bits indicating the descriptors that we wait for connect to
274 complete on. Once they complete, they are removed from this mask
275 and added to the input_wait_mask and non_keyboard_wait_mask. */
277 static SELECT_TYPE connect_wait_mask
;
279 /* Number of bits set in connect_wait_mask. */
280 static int num_pending_connects
;
282 #define IF_NON_BLOCKING_CONNECT(s) s
283 #else /* NON_BLOCKING_CONNECT */
284 #define IF_NON_BLOCKING_CONNECT(s)
285 #endif /* NON_BLOCKING_CONNECT */
287 /* The largest descriptor currently in use for a process object. */
288 static int max_process_desc
;
290 /* The largest descriptor currently in use for input. */
291 static int max_input_desc
;
293 /* Indexed by descriptor, gives the process (if any) for that descriptor */
294 Lisp_Object chan_process
[MAXDESC
];
296 /* Alist of elements (NAME . PROCESS) */
297 Lisp_Object Vprocess_alist
;
299 /* Buffered-ahead input char from process, indexed by channel.
300 -1 means empty (no char is buffered).
301 Used on sys V where the only way to tell if there is any
302 output from the process is to read at least one char.
303 Always -1 on systems that support FIONREAD. */
305 /* Don't make static; need to access externally. */
306 int proc_buffered_char
[MAXDESC
];
308 /* Table of `struct coding-system' for each process. */
309 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
310 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
312 #ifdef DATAGRAM_SOCKETS
313 /* Table of `partner address' for datagram sockets. */
314 struct sockaddr_and_len
{
317 } datagram_address
[MAXDESC
];
318 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
319 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
321 #define DATAGRAM_CHAN_P(chan) (0)
322 #define DATAGRAM_CONN_P(proc) (0)
325 /* Maximum number of bytes to send to a pty without an eof. */
326 static int pty_max_bytes
;
330 struct fd_callback_data
336 int condition
; /* mask of the defines above. */
337 } fd_callback_info
[MAXDESC
];
340 /* Add a file descriptor FD to be monitored for when read is possible.
341 When read is possible, call FUNC with argument DATA. */
344 add_read_fd (int fd
, fd_callback func
, void *data
)
346 xassert (fd
< MAXDESC
);
347 add_keyboard_wait_descriptor (fd
);
349 fd_callback_info
[fd
].func
= func
;
350 fd_callback_info
[fd
].data
= data
;
351 fd_callback_info
[fd
].condition
|= FOR_READ
;
354 /* Stop monitoring file descriptor FD for when read is possible. */
357 delete_read_fd (int fd
)
359 xassert (fd
< MAXDESC
);
360 delete_keyboard_wait_descriptor (fd
);
362 fd_callback_info
[fd
].condition
&= ~FOR_READ
;
363 if (fd_callback_info
[fd
].condition
== 0)
365 fd_callback_info
[fd
].func
= 0;
366 fd_callback_info
[fd
].data
= 0;
370 /* Add a file descriptor FD to be monitored for when write is possible.
371 When write is possible, call FUNC with argument DATA. */
374 add_write_fd (int fd
, fd_callback func
, void *data
)
376 xassert (fd
< MAXDESC
);
377 FD_SET (fd
, &write_mask
);
378 if (fd
> max_input_desc
)
381 fd_callback_info
[fd
].func
= func
;
382 fd_callback_info
[fd
].data
= data
;
383 fd_callback_info
[fd
].condition
|= FOR_WRITE
;
386 /* Stop monitoring file descriptor FD for when write is possible. */
389 delete_write_fd (int fd
)
391 int lim
= max_input_desc
;
393 xassert (fd
< MAXDESC
);
394 FD_CLR (fd
, &write_mask
);
395 fd_callback_info
[fd
].condition
&= ~FOR_WRITE
;
396 if (fd_callback_info
[fd
].condition
== 0)
398 fd_callback_info
[fd
].func
= 0;
399 fd_callback_info
[fd
].data
= 0;
401 if (fd
== max_input_desc
)
402 for (fd
= lim
; fd
>= 0; fd
--)
403 if (FD_ISSET (fd
, &input_wait_mask
) || FD_ISSET (fd
, &write_mask
))
413 /* Compute the Lisp form of the process status, p->status, from
414 the numeric status that was returned by `wait'. */
416 static Lisp_Object
status_convert (int);
419 update_status (struct Lisp_Process
*p
)
421 eassert (p
->raw_status_new
);
422 p
->status
= status_convert (p
->raw_status
);
423 p
->raw_status_new
= 0;
426 /* Convert a process status word in Unix format to
427 the list that we use internally. */
430 status_convert (int w
)
433 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
434 else if (WIFEXITED (w
))
435 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
436 WCOREDUMP (w
) ? Qt
: Qnil
));
437 else if (WIFSIGNALED (w
))
438 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
439 WCOREDUMP (w
) ? Qt
: Qnil
));
444 /* Given a status-list, extract the three pieces of information
445 and store them individually through the three pointers. */
448 decode_status (Lisp_Object l
, Lisp_Object
*symbol
, int *code
, int *coredump
)
462 *code
= XFASTINT (XCAR (tem
));
464 *coredump
= !NILP (tem
);
468 /* Return a string describing a process status list. */
471 status_message (struct Lisp_Process
*p
)
473 Lisp_Object status
= p
->status
;
476 Lisp_Object string
, string2
;
478 decode_status (status
, &symbol
, &code
, &coredump
);
480 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
483 synchronize_system_messages_locale ();
484 signame
= strsignal (code
);
486 string
= build_string ("unknown");
491 string
= make_unibyte_string (signame
, strlen (signame
));
492 if (! NILP (Vlocale_coding_system
))
493 string
= (code_convert_string_norecord
494 (string
, Vlocale_coding_system
, 0));
495 c1
= STRING_CHAR (SDATA (string
));
498 Faset (string
, make_number (0), make_number (c2
));
500 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
501 return concat2 (string
, string2
);
503 else if (EQ (symbol
, Qexit
))
506 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
508 return build_string ("finished\n");
509 string
= Fnumber_to_string (make_number (code
));
510 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
511 return concat3 (build_string ("exited abnormally with code "),
514 else if (EQ (symbol
, Qfailed
))
516 string
= Fnumber_to_string (make_number (code
));
517 string2
= build_string ("\n");
518 return concat3 (build_string ("failed with code "),
522 return Fcopy_sequence (Fsymbol_name (symbol
));
527 /* The file name of the pty opened by allocate_pty. */
528 static char pty_name
[24];
530 /* Open an available pty, returning a file descriptor.
531 Return -1 on failure.
532 The file name of the terminal corresponding to the pty
533 is left in the variable pty_name. */
544 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
545 for (i
= 0; i
< 16; i
++)
548 #ifdef PTY_NAME_SPRINTF
551 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
552 #endif /* no PTY_NAME_SPRINTF */
556 #else /* no PTY_OPEN */
558 { /* Some systems name their pseudoterminals so that there are gaps in
559 the usual sequence - for example, on HP9000/S700 systems, there
560 are no pseudoterminals with names ending in 'f'. So we wait for
561 three failures in a row before deciding that we've reached the
563 int failed_count
= 0;
566 if (stat (pty_name
, &stb
) < 0)
569 if (failed_count
>= 3)
576 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
578 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
581 #endif /* no PTY_OPEN */
585 /* check to make certain that both sides are available
586 this avoids a nasty yet stupid bug in rlogins */
587 #ifdef PTY_TTY_NAME_SPRINTF
590 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
591 #endif /* no PTY_TTY_NAME_SPRINTF */
592 if (access (pty_name
, 6) != 0)
607 #endif /* HAVE_PTYS */
610 make_process (Lisp_Object name
)
612 register Lisp_Object val
, tem
, name1
;
613 register struct Lisp_Process
*p
;
617 p
= allocate_process ();
625 p
->raw_status_new
= 0;
627 p
->mark
= Fmake_marker ();
628 p
->kill_without_query
= 0;
630 #ifdef ADAPTIVE_READ_BUFFERING
631 p
->adaptive_read_buffering
= 0;
632 p
->read_output_delay
= 0;
633 p
->read_output_skip
= 0;
637 p
->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
638 p
->gnutls_log_level
= 0;
642 /* If name is already in use, modify it until it is unused. */
647 tem
= Fget_process (name1
);
648 if (NILP (tem
)) break;
649 sprintf (suffix
, "<%d>", i
);
650 name1
= concat2 (name
, build_string (suffix
));
654 XSETPROCESS (val
, p
);
655 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
660 remove_process (register Lisp_Object proc
)
662 register Lisp_Object pair
;
664 pair
= Frassq (proc
, Vprocess_alist
);
665 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
667 deactivate_process (proc
);
671 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
672 doc
: /* Return t if OBJECT is a process. */)
675 return PROCESSP (object
) ? Qt
: Qnil
;
678 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
679 doc
: /* Return the process named NAME, or nil if there is none. */)
680 (register Lisp_Object name
)
685 return Fcdr (Fassoc (name
, Vprocess_alist
));
688 /* This is how commands for the user decode process arguments. It
689 accepts a process, a process name, a buffer, a buffer name, or nil.
690 Buffers denote the first process in the buffer, and nil denotes the
694 get_process (register Lisp_Object name
)
696 register Lisp_Object proc
, obj
;
699 obj
= Fget_process (name
);
701 obj
= Fget_buffer (name
);
703 error ("Process %s does not exist", SDATA (name
));
705 else if (NILP (name
))
706 obj
= Fcurrent_buffer ();
710 /* Now obj should be either a buffer object or a process object.
714 proc
= Fget_buffer_process (obj
);
716 error ("Buffer %s has no process", SDATA (BVAR (XBUFFER (obj
), name
)));
728 /* Fdelete_process promises to immediately forget about the process, but in
729 reality, Emacs needs to remember those processes until they have been
730 treated by sigchld_handler; otherwise this handler would consider the
731 process as being synchronous and say that the synchronous process is
733 static Lisp_Object deleted_pid_list
;
736 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
737 doc
: /* Delete PROCESS: kill it and forget about it immediately.
738 PROCESS may be a process, a buffer, the name of a process or buffer, or
739 nil, indicating the current buffer's process. */)
740 (register Lisp_Object process
)
742 register struct Lisp_Process
*p
;
744 process
= get_process (process
);
745 p
= XPROCESS (process
);
747 p
->raw_status_new
= 0;
748 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
750 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
751 p
->tick
= ++process_tick
;
753 redisplay_preserve_echo_area (13);
755 else if (p
->infd
>= 0)
759 /* Assignment to EMACS_INT stops GCC whining about limited range
761 EMACS_INT pid
= p
->pid
;
763 /* No problem storing the pid here, as it is still in Vprocess_alist. */
764 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
765 /* GC treated elements set to nil. */
766 Fdelq (Qnil
, deleted_pid_list
));
767 /* If the process has already signaled, remove it from the list. */
768 if (p
->raw_status_new
)
771 if (CONSP (p
->status
))
772 symbol
= XCAR (p
->status
);
773 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
775 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
779 Fkill_process (process
, Qnil
);
780 /* Do this now, since remove_process will make sigchld_handler do nothing. */
782 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
783 p
->tick
= ++process_tick
;
785 redisplay_preserve_echo_area (13);
788 remove_process (process
);
792 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
793 doc
: /* Return the status of PROCESS.
794 The returned value is one of the following symbols:
795 run -- for a process that is running.
796 stop -- for a process stopped but continuable.
797 exit -- for a process that has exited.
798 signal -- for a process that has got a fatal signal.
799 open -- for a network stream connection that is open.
800 listen -- for a network stream server that is listening.
801 closed -- for a network stream connection that is closed.
802 connect -- when waiting for a non-blocking connection to complete.
803 failed -- when a non-blocking connection has failed.
804 nil -- if arg is a process name and no such process exists.
805 PROCESS may be a process, a buffer, the name of a process, or
806 nil, indicating the current buffer's process. */)
807 (register Lisp_Object process
)
809 register struct Lisp_Process
*p
;
810 register Lisp_Object status
;
812 if (STRINGP (process
))
813 process
= Fget_process (process
);
815 process
= get_process (process
);
820 p
= XPROCESS (process
);
821 if (p
->raw_status_new
)
825 status
= XCAR (status
);
826 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
828 if (EQ (status
, Qexit
))
830 else if (EQ (p
->command
, Qt
))
832 else if (EQ (status
, Qrun
))
838 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
840 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
841 If PROCESS has not yet exited or died, return 0. */)
842 (register Lisp_Object process
)
844 CHECK_PROCESS (process
);
845 if (XPROCESS (process
)->raw_status_new
)
846 update_status (XPROCESS (process
));
847 if (CONSP (XPROCESS (process
)->status
))
848 return XCAR (XCDR (XPROCESS (process
)->status
));
849 return make_number (0);
852 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
853 doc
: /* Return the process id of PROCESS.
854 This is the pid of the external process which PROCESS uses or talks to.
855 For a network connection, this value is nil. */)
856 (register Lisp_Object process
)
858 /* Assignment to EMACS_INT stops GCC whining about limited range of
862 CHECK_PROCESS (process
);
863 pid
= XPROCESS (process
)->pid
;
864 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
867 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
868 doc
: /* Return the name of PROCESS, as a string.
869 This is the name of the program invoked in PROCESS,
870 possibly modified to make it unique among process names. */)
871 (register Lisp_Object process
)
873 CHECK_PROCESS (process
);
874 return XPROCESS (process
)->name
;
877 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
878 doc
: /* Return the command that was executed to start PROCESS.
879 This is a list of strings, the first string being the program executed
880 and the rest of the strings being the arguments given to it.
881 For a network or serial process, this is nil (process is running) or t
882 \(process is stopped). */)
883 (register Lisp_Object process
)
885 CHECK_PROCESS (process
);
886 return XPROCESS (process
)->command
;
889 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
890 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
891 This is the terminal that the process itself reads and writes on,
892 not the name of the pty that Emacs uses to talk with that terminal. */)
893 (register Lisp_Object process
)
895 CHECK_PROCESS (process
);
896 return XPROCESS (process
)->tty_name
;
899 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
901 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
902 (register Lisp_Object process
, Lisp_Object buffer
)
904 struct Lisp_Process
*p
;
906 CHECK_PROCESS (process
);
908 CHECK_BUFFER (buffer
);
909 p
= XPROCESS (process
);
911 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
912 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
913 setup_process_coding_systems (process
);
917 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
919 doc
: /* Return the buffer PROCESS is associated with.
920 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
921 (register Lisp_Object process
)
923 CHECK_PROCESS (process
);
924 return XPROCESS (process
)->buffer
;
927 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
929 doc
: /* Return the marker for the end of the last output from PROCESS. */)
930 (register Lisp_Object process
)
932 CHECK_PROCESS (process
);
933 return XPROCESS (process
)->mark
;
936 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
938 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
939 A value of t means stop accepting output from the process.
941 When a process has a filter, its buffer is not used for output.
942 Instead, each time it does output, the entire string of output is
943 passed to the filter.
945 The filter gets two arguments: the process and the string of output.
946 The string argument is normally a multibyte string, except:
947 - if the process' input coding system is no-conversion or raw-text,
948 it is a unibyte string (the non-converted input), or else
949 - if `default-enable-multibyte-characters' is nil, it is a unibyte
950 string (the result of converting the decoded input multibyte
951 string to unibyte with `string-make-unibyte'). */)
952 (register Lisp_Object process
, Lisp_Object filter
)
954 struct Lisp_Process
*p
;
956 CHECK_PROCESS (process
);
957 p
= XPROCESS (process
);
959 /* Don't signal an error if the process' input file descriptor
960 is closed. This could make debugging Lisp more difficult,
961 for example when doing something like
963 (setq process (start-process ...))
965 (set-process-filter process ...) */
969 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
971 FD_CLR (p
->infd
, &input_wait_mask
);
972 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
974 else if (EQ (p
->filter
, Qt
)
975 /* Network or serial process not stopped: */
976 && !EQ (p
->command
, Qt
))
978 FD_SET (p
->infd
, &input_wait_mask
);
979 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
984 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
985 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
986 setup_process_coding_systems (process
);
990 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
992 doc
: /* Returns the filter function of PROCESS; nil if none.
993 See `set-process-filter' for more info on filter functions. */)
994 (register Lisp_Object process
)
996 CHECK_PROCESS (process
);
997 return XPROCESS (process
)->filter
;
1000 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1002 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1003 The sentinel is called as a function when the process changes state.
1004 It gets two arguments: the process, and a string describing the change. */)
1005 (register Lisp_Object process
, Lisp_Object sentinel
)
1007 struct Lisp_Process
*p
;
1009 CHECK_PROCESS (process
);
1010 p
= XPROCESS (process
);
1012 p
->sentinel
= sentinel
;
1013 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1014 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1018 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1020 doc
: /* Return the sentinel of PROCESS; nil if none.
1021 See `set-process-sentinel' for more info on sentinels. */)
1022 (register Lisp_Object process
)
1024 CHECK_PROCESS (process
);
1025 return XPROCESS (process
)->sentinel
;
1028 DEFUN ("set-process-window-size", Fset_process_window_size
,
1029 Sset_process_window_size
, 3, 3, 0,
1030 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1031 (register Lisp_Object process
, Lisp_Object height
, Lisp_Object width
)
1033 CHECK_PROCESS (process
);
1034 CHECK_NATNUM (height
);
1035 CHECK_NATNUM (width
);
1037 if (XPROCESS (process
)->infd
< 0
1038 || set_window_size (XPROCESS (process
)->infd
,
1039 XINT (height
), XINT (width
)) <= 0)
1045 DEFUN ("set-process-inherit-coding-system-flag",
1046 Fset_process_inherit_coding_system_flag
,
1047 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1048 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1049 If the second argument FLAG is non-nil, then the variable
1050 `buffer-file-coding-system' of the buffer associated with PROCESS
1051 will be bound to the value of the coding system used to decode
1054 This is useful when the coding system specified for the process buffer
1055 leaves either the character code conversion or the end-of-line conversion
1056 unspecified, or if the coding system used to decode the process output
1057 is more appropriate for saving the process buffer.
1059 Binding the variable `inherit-process-coding-system' to non-nil before
1060 starting the process is an alternative way of setting the inherit flag
1061 for the process which will run. */)
1062 (register Lisp_Object process
, Lisp_Object flag
)
1064 CHECK_PROCESS (process
);
1065 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1069 DEFUN ("set-process-query-on-exit-flag",
1070 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1072 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1073 If the second argument FLAG is non-nil, Emacs will query the user before
1074 exiting or killing a buffer if PROCESS is running. */)
1075 (register Lisp_Object process
, Lisp_Object flag
)
1077 CHECK_PROCESS (process
);
1078 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1082 DEFUN ("process-query-on-exit-flag",
1083 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1085 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1086 (register Lisp_Object process
)
1088 CHECK_PROCESS (process
);
1089 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1092 #ifdef DATAGRAM_SOCKETS
1093 Lisp_Object
Fprocess_datagram_address (Lisp_Object process
);
1096 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1098 doc
: /* Return the contact info of PROCESS; t for a real child.
1099 For a network or serial connection, the value depends on the optional
1100 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1101 SERVICE) for a network connection or (PORT SPEED) for a serial
1102 connection. If KEY is t, the complete contact information for the
1103 connection is returned, else the specific value for the keyword KEY is
1104 returned. See `make-network-process' or `make-serial-process' for a
1105 list of keywords. */)
1106 (register Lisp_Object process
, Lisp_Object key
)
1108 Lisp_Object contact
;
1110 CHECK_PROCESS (process
);
1111 contact
= XPROCESS (process
)->childp
;
1113 #ifdef DATAGRAM_SOCKETS
1114 if (DATAGRAM_CONN_P (process
)
1115 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1116 contact
= Fplist_put (contact
, QCremote
,
1117 Fprocess_datagram_address (process
));
1120 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1122 if (NILP (key
) && NETCONN_P (process
))
1123 return Fcons (Fplist_get (contact
, QChost
),
1124 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1125 if (NILP (key
) && SERIALCONN_P (process
))
1126 return Fcons (Fplist_get (contact
, QCport
),
1127 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1128 return Fplist_get (contact
, key
);
1131 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1133 doc
: /* Return the plist of PROCESS. */)
1134 (register Lisp_Object process
)
1136 CHECK_PROCESS (process
);
1137 return XPROCESS (process
)->plist
;
1140 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1142 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1143 (register Lisp_Object process
, Lisp_Object plist
)
1145 CHECK_PROCESS (process
);
1148 XPROCESS (process
)->plist
= plist
;
1152 #if 0 /* Turned off because we don't currently record this info
1153 in the process. Perhaps add it. */
1154 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1155 doc
: /* Return the connection type of PROCESS.
1156 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1157 a socket connection. */)
1158 (Lisp_Object process
)
1160 return XPROCESS (process
)->type
;
1164 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1165 doc
: /* Return the connection type of PROCESS.
1166 The value is either the symbol `real', `network', or `serial'.
1167 PROCESS may be a process, a buffer, the name of a process or buffer, or
1168 nil, indicating the current buffer's process. */)
1169 (Lisp_Object process
)
1172 proc
= get_process (process
);
1173 return XPROCESS (proc
)->type
;
1176 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1178 doc
: /* Convert network ADDRESS from internal format to a string.
1179 A 4 or 5 element vector represents an IPv4 address (with port number).
1180 An 8 or 9 element vector represents an IPv6 address (with port number).
1181 If optional second argument OMIT-PORT is non-nil, don't include a port
1182 number in the string, even when present in ADDRESS.
1183 Returns nil if format of ADDRESS is invalid. */)
1184 (Lisp_Object address
, Lisp_Object omit_port
)
1189 if (STRINGP (address
)) /* AF_LOCAL */
1192 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1194 register struct Lisp_Vector
*p
= XVECTOR (address
);
1195 Lisp_Object args
[10];
1198 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1200 args
[0] = build_string ("%d.%d.%d.%d");
1203 else if (p
->size
== 5)
1205 args
[0] = build_string ("%d.%d.%d.%d:%d");
1208 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1210 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1213 else if (p
->size
== 9)
1215 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1221 for (i
= 0; i
< nargs
; i
++)
1223 EMACS_INT element
= XINT (p
->contents
[i
]);
1225 if (element
< 0 || element
> 65535)
1228 if (nargs
<= 5 /* IPv4 */
1229 && i
< 4 /* host, not port */
1233 args
[i
+1] = p
->contents
[i
];
1236 return Fformat (nargs
+1, args
);
1239 if (CONSP (address
))
1241 Lisp_Object args
[2];
1242 args
[0] = build_string ("<Family %d>");
1243 args
[1] = Fcar (address
);
1244 return Fformat (2, args
);
1251 list_processes_1 (Lisp_Object query_only
)
1253 register Lisp_Object tail
, tem
;
1254 Lisp_Object proc
, minspace
, tem1
;
1255 register struct Lisp_Process
*p
;
1257 int w_proc
, w_buffer
, w_tty
;
1259 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1261 w_proc
= 4; /* Proc */
1262 w_buffer
= 6; /* Buffer */
1263 w_tty
= 0; /* Omit if no ttys */
1265 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1269 proc
= Fcdr (XCAR (tail
));
1270 p
= XPROCESS (proc
);
1273 if (!NILP (query_only
) && p
->kill_without_query
)
1275 if (STRINGP (p
->name
)
1276 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1278 if (!NILP (p
->buffer
))
1280 if (NILP (BVAR (XBUFFER (p
->buffer
), name
)))
1283 w_buffer
= 8; /* (Killed) */
1285 else if ((i
= SCHARS (BVAR (XBUFFER (p
->buffer
), name
)), (i
> w_buffer
)))
1288 if (STRINGP (p
->tty_name
)
1289 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1293 XSETFASTINT (i_status
, w_proc
+ 1);
1294 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1297 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1298 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1303 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1306 XSETFASTINT (minspace
, 1);
1308 set_buffer_internal (XBUFFER (Vstandard_output
));
1309 BVAR (current_buffer
, undo_list
) = Qt
;
1311 BVAR (current_buffer
, truncate_lines
) = Qt
;
1313 write_string ("Proc", -1);
1314 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1315 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1318 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1320 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1321 write_string ("\n", -1);
1323 write_string ("----", -1);
1324 Findent_to (i_status
, minspace
); write_string ("------", -1);
1325 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1328 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1330 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1331 write_string ("\n", -1);
1333 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1337 proc
= Fcdr (XCAR (tail
));
1338 p
= XPROCESS (proc
);
1341 if (!NILP (query_only
) && p
->kill_without_query
)
1344 Finsert (1, &p
->name
);
1345 Findent_to (i_status
, minspace
);
1347 if (p
->raw_status_new
)
1350 if (CONSP (p
->status
))
1351 symbol
= XCAR (p
->status
);
1353 if (EQ (symbol
, Qsignal
))
1356 tem
= Fcar (Fcdr (p
->status
));
1357 Fprinc (symbol
, Qnil
);
1359 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1361 if (EQ (symbol
, Qexit
))
1362 write_string ("closed", -1);
1363 else if (EQ (p
->command
, Qt
))
1364 write_string ("stopped", -1);
1365 else if (EQ (symbol
, Qrun
))
1366 write_string ("open", -1);
1368 Fprinc (symbol
, Qnil
);
1370 else if (SERIALCONN1_P (p
))
1372 write_string ("running", -1);
1375 Fprinc (symbol
, Qnil
);
1377 if (EQ (symbol
, Qexit
))
1380 tem
= Fcar (Fcdr (p
->status
));
1383 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1384 write_string (tembuf
, -1);
1388 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1391 Findent_to (i_buffer
, minspace
);
1392 if (NILP (p
->buffer
))
1393 insert_string ("(none)");
1394 else if (NILP (BVAR (XBUFFER (p
->buffer
), name
)))
1395 insert_string ("(Killed)");
1397 Finsert (1, &BVAR (XBUFFER (p
->buffer
), name
));
1401 Findent_to (i_tty
, minspace
);
1402 if (STRINGP (p
->tty_name
))
1403 Finsert (1, &p
->tty_name
);
1406 Findent_to (i_command
, minspace
);
1408 if (EQ (p
->status
, Qlisten
))
1410 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1411 if (INTEGERP (port
))
1412 port
= Fnumber_to_string (port
);
1414 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1415 sprintf (tembuf
, "(network %s server on %s)\n",
1416 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1417 (STRINGP (port
) ? SSDATA (port
) : "?"));
1418 insert_string (tembuf
);
1420 else if (NETCONN1_P (p
))
1422 /* For a local socket, there is no host name,
1423 so display service instead. */
1424 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1425 if (!STRINGP (host
))
1427 host
= Fplist_get (p
->childp
, QCservice
);
1428 if (INTEGERP (host
))
1429 host
= Fnumber_to_string (host
);
1432 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1433 sprintf (tembuf
, "(network %s connection to %s)\n",
1434 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1435 (STRINGP (host
) ? SSDATA (host
) : "?"));
1436 insert_string (tembuf
);
1438 else if (SERIALCONN1_P (p
))
1440 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1441 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1442 insert_string ("(serial port ");
1444 insert_string (SSDATA (port
));
1446 insert_string ("?");
1447 if (INTEGERP (speed
))
1449 sprintf (tembuf
, " at %ld b/s", (long) XINT (speed
));
1450 insert_string (tembuf
);
1452 insert_string (")\n");
1466 insert_string (" ");
1468 insert_string ("\n");
1473 status_notify (NULL
);
1474 redisplay_preserve_echo_area (13);
1479 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1480 doc
: /* Display a list of all processes.
1481 If optional argument QUERY-ONLY is non-nil, only processes with
1482 the query-on-exit flag set will be listed.
1483 Any process listed as exited or signaled is actually eliminated
1484 after the listing is made. */)
1485 (Lisp_Object query_only
)
1487 internal_with_output_to_temp_buffer ("*Process List*",
1488 list_processes_1
, query_only
);
1492 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1493 doc
: /* Return a list of all processes. */)
1496 return Fmapcar (Qcdr
, Vprocess_alist
);
1499 /* Starting asynchronous inferior processes. */
1501 static Lisp_Object
start_process_unwind (Lisp_Object proc
);
1503 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1504 doc
: /* Start a program in a subprocess. Return the process object for it.
1505 NAME is name for process. It is modified if necessary to make it unique.
1506 BUFFER is the buffer (or buffer name) to associate with the process.
1508 Process output (both standard output and standard error streams) goes
1509 at end of BUFFER, unless you specify an output stream or filter
1510 function to handle the output. BUFFER may also be nil, meaning that
1511 this process is not associated with any buffer.
1513 PROGRAM is the program file name. It is searched for in PATH. If
1514 nil, just associate a pty with the buffer. Remaining arguments are
1515 strings to give program as arguments.
1517 If you want to separate standard output from standard error, invoke
1518 the command through a shell and redirect one of them using the shell
1521 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1522 (int nargs
, register Lisp_Object
*args
)
1524 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1525 register unsigned char **new_argv
;
1527 int count
= SPECPDL_INDEX ();
1531 buffer
= Fget_buffer_create (buffer
);
1533 /* Make sure that the child will be able to chdir to the current
1534 buffer's current directory, or its unhandled equivalent. We
1535 can't just have the child check for an error when it does the
1536 chdir, since it's in a vfork.
1538 We have to GCPRO around this because Fexpand_file_name and
1539 Funhandled_file_name_directory might call a file name handling
1540 function. The argument list is protected by the caller, so all
1541 we really have to worry about is buffer. */
1543 struct gcpro gcpro1
, gcpro2
;
1545 current_dir
= BVAR (current_buffer
, directory
);
1547 GCPRO2 (buffer
, current_dir
);
1549 current_dir
= Funhandled_file_name_directory (current_dir
);
1550 if (NILP (current_dir
))
1551 /* If the file name handler says that current_dir is unreachable, use
1552 a sensible default. */
1553 current_dir
= build_string ("~/");
1554 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1555 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1556 report_file_error ("Setting current directory",
1557 Fcons (BVAR (current_buffer
, directory
), Qnil
));
1563 CHECK_STRING (name
);
1567 if (!NILP (program
))
1568 CHECK_STRING (program
);
1570 proc
= make_process (name
);
1571 /* If an error occurs and we can't start the process, we want to
1572 remove it from the process list. This means that each error
1573 check in create_process doesn't need to call remove_process
1574 itself; it's all taken care of here. */
1575 record_unwind_protect (start_process_unwind
, proc
);
1577 XPROCESS (proc
)->childp
= Qt
;
1578 XPROCESS (proc
)->plist
= Qnil
;
1579 XPROCESS (proc
)->type
= Qreal
;
1580 XPROCESS (proc
)->buffer
= buffer
;
1581 XPROCESS (proc
)->sentinel
= Qnil
;
1582 XPROCESS (proc
)->filter
= Qnil
;
1583 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1586 /* AKA GNUTLS_INITSTAGE(proc). */
1587 XPROCESS (proc
)->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
1588 XPROCESS (proc
)->gnutls_cred_type
= Qnil
;
1591 #ifdef ADAPTIVE_READ_BUFFERING
1592 XPROCESS (proc
)->adaptive_read_buffering
1593 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1594 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1597 /* Make the process marker point into the process buffer (if any). */
1598 if (BUFFERP (buffer
))
1599 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1600 BUF_ZV (XBUFFER (buffer
)),
1601 BUF_ZV_BYTE (XBUFFER (buffer
)));
1604 /* Decide coding systems for communicating with the process. Here
1605 we don't setup the structure coding_system nor pay attention to
1606 unibyte mode. They are done in create_process. */
1608 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1609 Lisp_Object coding_systems
= Qt
;
1610 Lisp_Object val
, *args2
;
1611 struct gcpro gcpro1
, gcpro2
;
1613 val
= Vcoding_system_for_read
;
1616 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1617 args2
[0] = Qstart_process
;
1618 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1619 GCPRO2 (proc
, current_dir
);
1620 if (!NILP (program
))
1621 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1623 if (CONSP (coding_systems
))
1624 val
= XCAR (coding_systems
);
1625 else if (CONSP (Vdefault_process_coding_system
))
1626 val
= XCAR (Vdefault_process_coding_system
);
1628 XPROCESS (proc
)->decode_coding_system
= val
;
1630 val
= Vcoding_system_for_write
;
1633 if (EQ (coding_systems
, Qt
))
1635 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1636 args2
[0] = Qstart_process
;
1637 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1638 GCPRO2 (proc
, current_dir
);
1639 if (!NILP (program
))
1640 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1643 if (CONSP (coding_systems
))
1644 val
= XCDR (coding_systems
);
1645 else if (CONSP (Vdefault_process_coding_system
))
1646 val
= XCDR (Vdefault_process_coding_system
);
1648 XPROCESS (proc
)->encode_coding_system
= val
;
1649 /* Note: At this momemnt, the above coding system may leave
1650 text-conversion or eol-conversion unspecified. They will be
1651 decided after we read output from the process and decode it by
1652 some coding system, or just before we actually send a text to
1657 XPROCESS (proc
)->decoding_buf
= empty_unibyte_string
;
1658 XPROCESS (proc
)->decoding_carryover
= 0;
1659 XPROCESS (proc
)->encoding_buf
= empty_unibyte_string
;
1661 XPROCESS (proc
)->inherit_coding_system_flag
1662 = !(NILP (buffer
) || !inherit_process_coding_system
);
1664 if (!NILP (program
))
1666 /* If program file name is not absolute, search our path for it.
1667 Put the name we will really use in TEM. */
1668 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1669 && !(SCHARS (program
) > 1
1670 && IS_DEVICE_SEP (SREF (program
, 1))))
1672 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1675 GCPRO4 (name
, program
, buffer
, current_dir
);
1676 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1679 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1680 tem
= Fexpand_file_name (tem
, Qnil
);
1684 if (!NILP (Ffile_directory_p (program
)))
1685 error ("Specified program for new process is a directory");
1689 /* If program file name starts with /: for quoting a magic name,
1691 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1692 && SREF (tem
, 1) == ':')
1693 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1696 Lisp_Object arg_encoding
= Qnil
;
1697 struct gcpro gcpro1
;
1700 /* Encode the file name and put it in NEW_ARGV.
1701 That's where the child will use it to execute the program. */
1702 tem
= Fcons (ENCODE_FILE (tem
), Qnil
);
1704 /* Here we encode arguments by the coding system used for sending
1705 data to the process. We don't support using different coding
1706 systems for encoding arguments and for encoding data sent to the
1709 for (i
= 3; i
< nargs
; i
++)
1711 tem
= Fcons (args
[i
], tem
);
1712 CHECK_STRING (XCAR (tem
));
1713 if (STRING_MULTIBYTE (XCAR (tem
)))
1715 if (NILP (arg_encoding
))
1716 arg_encoding
= (complement_process_encoding_system
1717 (XPROCESS (proc
)->encode_coding_system
));
1719 code_convert_string_norecord
1720 (XCAR (tem
), arg_encoding
, 1));
1727 /* Now that everything is encoded we can collect the strings into
1729 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1730 new_argv
[nargs
- 2] = 0;
1732 for (i
= nargs
- 3; i
>= 0; i
--)
1734 new_argv
[i
] = SDATA (XCAR (tem
));
1738 create_process (proc
, (char **) new_argv
, current_dir
);
1743 return unbind_to (count
, proc
);
1746 /* This function is the unwind_protect form for Fstart_process. If
1747 PROC doesn't have its pid set, then we know someone has signaled
1748 an error and the process wasn't started successfully, so we should
1749 remove it from the process list. */
1751 start_process_unwind (Lisp_Object proc
)
1753 if (!PROCESSP (proc
))
1756 /* Was PROC started successfully? */
1757 if (XPROCESS (proc
)->pid
== -1)
1758 remove_process (proc
);
1764 create_process_1 (struct atimer
*timer
)
1766 /* Nothing to do. */
1771 create_process (Lisp_Object process
, char **new_argv
, Lisp_Object current_dir
)
1773 int inchannel
, outchannel
;
1776 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1777 int wait_child_setup
[2];
1781 struct sigaction sigint_action
;
1782 struct sigaction sigquit_action
;
1783 struct sigaction sigpipe_action
;
1785 struct sigaction sighup_action
;
1787 /* Use volatile to protect variables from being clobbered by longjmp. */
1788 volatile int forkin
, forkout
;
1789 volatile int pty_flag
= 0;
1791 extern char **environ
;
1794 inchannel
= outchannel
= -1;
1797 if (!NILP (Vprocess_connection_type
))
1798 outchannel
= inchannel
= allocate_pty ();
1802 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1803 /* On most USG systems it does not work to open the pty's tty here,
1804 then close it and reopen it in the child. */
1806 /* Don't let this terminal become our controlling terminal
1807 (in case we don't have one). */
1808 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1810 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1813 report_file_error ("Opening pty", Qnil
);
1815 forkin
= forkout
= -1;
1816 #endif /* not USG, or USG_SUBTTY_WORKS */
1820 #endif /* HAVE_PTYS */
1825 report_file_error ("Creating pipe", Qnil
);
1831 emacs_close (inchannel
);
1832 emacs_close (forkout
);
1833 report_file_error ("Creating pipe", Qnil
);
1839 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1843 tem
= pipe (wait_child_setup
);
1845 report_file_error ("Creating pipe", Qnil
);
1846 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1848 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1851 emacs_close (wait_child_setup
[0]);
1852 emacs_close (wait_child_setup
[1]);
1853 report_file_error ("Setting file descriptor flags", Qnil
);
1859 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1860 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1863 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1864 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1868 /* Record this as an active process, with its channels.
1869 As a result, child_setup will close Emacs's side of the pipes. */
1870 chan_process
[inchannel
] = process
;
1871 XPROCESS (process
)->infd
= inchannel
;
1872 XPROCESS (process
)->outfd
= outchannel
;
1874 /* Previously we recorded the tty descriptor used in the subprocess.
1875 It was only used for getting the foreground tty process, so now
1876 we just reopen the device (see emacs_get_tty_pgrp) as this is
1877 more portable (see USG_SUBTTY_WORKS above). */
1879 XPROCESS (process
)->pty_flag
= pty_flag
;
1880 XPROCESS (process
)->status
= Qrun
;
1881 setup_process_coding_systems (process
);
1883 /* Delay interrupts until we have a chance to store
1884 the new fork's pid in its process structure */
1885 sigemptyset (&blocked
);
1887 sigaddset (&blocked
, SIGCHLD
);
1889 #ifdef HAVE_WORKING_VFORK
1890 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1891 this sets the parent's signal handlers as well as the child's.
1892 So delay all interrupts whose handlers the child might munge,
1893 and record the current handlers so they can be restored later. */
1894 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1895 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1896 sigaddset (&blocked
, SIGPIPE
); sigaction (SIGPIPE
, 0, &sigpipe_action
);
1898 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1900 #endif /* HAVE_WORKING_VFORK */
1901 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1903 FD_SET (inchannel
, &input_wait_mask
);
1904 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1905 if (inchannel
> max_process_desc
)
1906 max_process_desc
= inchannel
;
1908 /* Until we store the proper pid, enable sigchld_handler
1909 to recognize an unknown pid as standing for this process.
1910 It is very important not to let this `marker' value stay
1911 in the table after this function has returned; if it does
1912 it might cause call-process to hang and subsequent asynchronous
1913 processes to get their return values scrambled. */
1914 XPROCESS (process
)->pid
= -1;
1919 /* child_setup must clobber environ on systems with true vfork.
1920 Protect it from permanent change. */
1921 char **save_environ
= environ
;
1923 current_dir
= ENCODE_FILE (current_dir
);
1928 #endif /* not WINDOWSNT */
1930 int xforkin
= forkin
;
1931 int xforkout
= forkout
;
1933 #if 0 /* This was probably a mistake--it duplicates code later on,
1934 but fails to handle all the cases. */
1935 /* Make sure SIGCHLD is not blocked in the child. */
1936 sigsetmask (SIGEMPTYMASK
);
1939 /* Make the pty be the controlling terminal of the process. */
1941 /* First, disconnect its current controlling terminal. */
1943 /* We tried doing setsid only if pty_flag, but it caused
1944 process_set_signal to fail on SGI when using a pipe. */
1946 /* Make the pty's terminal the controlling terminal. */
1947 if (pty_flag
&& xforkin
>= 0)
1950 /* We ignore the return value
1951 because faith@cs.unc.edu says that is necessary on Linux. */
1952 ioctl (xforkin
, TIOCSCTTY
, 0);
1955 #else /* not HAVE_SETSID */
1957 /* It's very important to call setpgrp here and no time
1958 afterwards. Otherwise, we lose our controlling tty which
1959 is set when we open the pty. */
1962 #endif /* not HAVE_SETSID */
1963 #if defined (LDISC1)
1964 if (pty_flag
&& xforkin
>= 0)
1967 tcgetattr (xforkin
, &t
);
1969 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1970 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1973 #if defined (NTTYDISC) && defined (TIOCSETD)
1974 if (pty_flag
&& xforkin
>= 0)
1976 /* Use new line discipline. */
1977 int ldisc
= NTTYDISC
;
1978 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1983 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1984 can do TIOCSPGRP only to the process's controlling tty. */
1987 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1988 I can't test it since I don't have 4.3. */
1989 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
1992 ioctl (j
, TIOCNOTTY
, 0);
1996 /* In order to get a controlling terminal on some versions
1997 of BSD, it is necessary to put the process in pgrp 0
1998 before it opens the terminal. */
2006 #endif /* TIOCNOTTY */
2008 #if !defined (DONT_REOPEN_PTY)
2009 /*** There is a suggestion that this ought to be a
2010 conditional on TIOCSPGRP,
2011 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2012 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2013 that system does seem to need this code, even though
2014 both HAVE_SETSID and TIOCSCTTY are defined. */
2015 /* Now close the pty (if we had it open) and reopen it.
2016 This makes the pty the controlling terminal of the subprocess. */
2020 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2023 emacs_close (xforkin
);
2024 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2028 emacs_write (1, "Couldn't open the pty terminal ", 31);
2029 emacs_write (1, pty_name
, strlen (pty_name
));
2030 emacs_write (1, "\n", 1);
2035 #endif /* not DONT_REOPEN_PTY */
2037 #ifdef SETUP_SLAVE_PTY
2042 #endif /* SETUP_SLAVE_PTY */
2044 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2045 Now reenable it in the child, so it will die when we want it to. */
2047 signal (SIGHUP
, SIG_DFL
);
2049 #endif /* HAVE_PTYS */
2051 signal (SIGINT
, SIG_DFL
);
2052 signal (SIGQUIT
, SIG_DFL
);
2053 /* GConf causes us to ignore SIGPIPE, make sure it is restored
2055 signal (SIGPIPE
, SIG_DFL
);
2057 /* Stop blocking signals in the child. */
2058 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2061 child_setup_tty (xforkout
);
2063 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2064 new_argv
, 1, current_dir
);
2065 #else /* not WINDOWSNT */
2067 emacs_close (wait_child_setup
[0]);
2069 child_setup (xforkin
, xforkout
, xforkout
,
2070 new_argv
, 1, current_dir
);
2071 #endif /* not WINDOWSNT */
2073 environ
= save_environ
;
2078 /* This runs in the Emacs process. */
2082 emacs_close (forkin
);
2083 if (forkin
!= forkout
&& forkout
>= 0)
2084 emacs_close (forkout
);
2088 /* vfork succeeded. */
2089 XPROCESS (process
)->pid
= pid
;
2092 register_child (pid
, inchannel
);
2093 #endif /* WINDOWSNT */
2095 /* If the subfork execv fails, and it exits,
2096 this close hangs. I don't know why.
2097 So have an interrupt jar it loose. */
2099 struct atimer
*timer
;
2103 EMACS_SET_SECS_USECS (offset
, 1, 0);
2104 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2107 emacs_close (forkin
);
2109 cancel_atimer (timer
);
2113 if (forkin
!= forkout
&& forkout
>= 0)
2114 emacs_close (forkout
);
2118 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2121 XPROCESS (process
)->tty_name
= Qnil
;
2123 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2124 /* Wait for child_setup to complete in case that vfork is
2125 actually defined as fork. The descriptor wait_child_setup[1]
2126 of a pipe is closed at the child side either by close-on-exec
2127 on successful execvp or the _exit call in child_setup. */
2131 emacs_close (wait_child_setup
[1]);
2132 emacs_read (wait_child_setup
[0], &dummy
, 1);
2133 emacs_close (wait_child_setup
[0]);
2138 /* Restore the signal state whether vfork succeeded or not.
2139 (We will signal an error, below, if it failed.) */
2140 #ifdef HAVE_WORKING_VFORK
2141 /* Restore the parent's signal handlers. */
2142 sigaction (SIGINT
, &sigint_action
, 0);
2143 sigaction (SIGQUIT
, &sigquit_action
, 0);
2144 sigaction (SIGPIPE
, &sigpipe_action
, 0);
2146 sigaction (SIGHUP
, &sighup_action
, 0);
2148 #endif /* HAVE_WORKING_VFORK */
2149 /* Stop blocking signals in the parent. */
2150 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2152 /* Now generate the error if vfork failed. */
2154 report_file_error ("Doing vfork", Qnil
);
2158 create_pty (Lisp_Object process
)
2160 int inchannel
, outchannel
;
2162 /* Use volatile to protect variables from being clobbered by longjmp. */
2163 volatile int forkin
, forkout
;
2164 volatile int pty_flag
= 0;
2166 inchannel
= outchannel
= -1;
2169 if (!NILP (Vprocess_connection_type
))
2170 outchannel
= inchannel
= allocate_pty ();
2174 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2175 /* On most USG systems it does not work to open the pty's tty here,
2176 then close it and reopen it in the child. */
2178 /* Don't let this terminal become our controlling terminal
2179 (in case we don't have one). */
2180 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
2182 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
2185 report_file_error ("Opening pty", Qnil
);
2186 #if defined (DONT_REOPEN_PTY)
2187 /* In the case that vfork is defined as fork, the parent process
2188 (Emacs) may send some data before the child process completes
2189 tty options setup. So we setup tty before forking. */
2190 child_setup_tty (forkout
);
2191 #endif /* DONT_REOPEN_PTY */
2193 forkin
= forkout
= -1;
2194 #endif /* not USG, or USG_SUBTTY_WORKS */
2197 #endif /* HAVE_PTYS */
2200 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
2201 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
2204 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
2205 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
2209 /* Record this as an active process, with its channels.
2210 As a result, child_setup will close Emacs's side of the pipes. */
2211 chan_process
[inchannel
] = process
;
2212 XPROCESS (process
)->infd
= inchannel
;
2213 XPROCESS (process
)->outfd
= outchannel
;
2215 /* Previously we recorded the tty descriptor used in the subprocess.
2216 It was only used for getting the foreground tty process, so now
2217 we just reopen the device (see emacs_get_tty_pgrp) as this is
2218 more portable (see USG_SUBTTY_WORKS above). */
2220 XPROCESS (process
)->pty_flag
= pty_flag
;
2221 XPROCESS (process
)->status
= Qrun
;
2222 setup_process_coding_systems (process
);
2224 FD_SET (inchannel
, &input_wait_mask
);
2225 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2226 if (inchannel
> max_process_desc
)
2227 max_process_desc
= inchannel
;
2229 XPROCESS (process
)->pid
= -2;
2232 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2235 XPROCESS (process
)->tty_name
= Qnil
;
2239 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2240 The address family of sa is not included in the result. */
2243 conv_sockaddr_to_lisp (struct sockaddr
*sa
, int len
)
2245 Lisp_Object address
;
2248 register struct Lisp_Vector
*p
;
2250 /* Workaround for a bug in getsockname on BSD: Names bound to
2251 sockets in the UNIX domain are inaccessible; getsockname returns
2252 a zero length name. */
2253 if (len
< offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
))
2254 return empty_unibyte_string
;
2256 switch (sa
->sa_family
)
2260 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2261 len
= sizeof (sin
->sin_addr
) + 1;
2262 address
= Fmake_vector (make_number (len
), Qnil
);
2263 p
= XVECTOR (address
);
2264 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2265 cp
= (unsigned char *) &sin
->sin_addr
;
2271 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2272 uint16_t *ip6
= (uint16_t *) &sin6
->sin6_addr
;
2273 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2274 address
= Fmake_vector (make_number (len
), Qnil
);
2275 p
= XVECTOR (address
);
2276 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2277 for (i
= 0; i
< len
; i
++)
2278 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2282 #ifdef HAVE_LOCAL_SOCKETS
2285 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2286 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2287 if (sockun
->sun_path
[i
] == 0)
2289 return make_unibyte_string (sockun
->sun_path
, i
);
2293 len
-= offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
);
2294 address
= Fcons (make_number (sa
->sa_family
),
2295 Fmake_vector (make_number (len
), Qnil
));
2296 p
= XVECTOR (XCDR (address
));
2297 cp
= (unsigned char *) &sa
->sa_family
+ sizeof (sa
->sa_family
);
2303 p
->contents
[i
++] = make_number (*cp
++);
2309 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2312 get_lisp_to_sockaddr_size (Lisp_Object address
, int *familyp
)
2314 register struct Lisp_Vector
*p
;
2316 if (VECTORP (address
))
2318 p
= XVECTOR (address
);
2322 return sizeof (struct sockaddr_in
);
2325 else if (p
->size
== 9)
2327 *familyp
= AF_INET6
;
2328 return sizeof (struct sockaddr_in6
);
2332 #ifdef HAVE_LOCAL_SOCKETS
2333 else if (STRINGP (address
))
2335 *familyp
= AF_LOCAL
;
2336 return sizeof (struct sockaddr_un
);
2339 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2341 struct sockaddr
*sa
;
2342 *familyp
= XINT (XCAR (address
));
2343 p
= XVECTOR (XCDR (address
));
2344 return p
->size
+ sizeof (sa
->sa_family
);
2349 /* Convert an address object (vector or string) to an internal sockaddr.
2351 The address format has been basically validated by
2352 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2353 it could have come from user data. So if FAMILY is not valid,
2354 we return after zeroing *SA. */
2357 conv_lisp_to_sockaddr (int family
, Lisp_Object address
, struct sockaddr
*sa
, int len
)
2359 register struct Lisp_Vector
*p
;
2360 register unsigned char *cp
= NULL
;
2363 memset (sa
, 0, len
);
2365 if (VECTORP (address
))
2367 p
= XVECTOR (address
);
2368 if (family
== AF_INET
)
2370 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2371 len
= sizeof (sin
->sin_addr
) + 1;
2372 i
= XINT (p
->contents
[--len
]);
2373 sin
->sin_port
= htons (i
);
2374 cp
= (unsigned char *)&sin
->sin_addr
;
2375 sa
->sa_family
= family
;
2378 else if (family
== AF_INET6
)
2380 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2381 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2382 len
= sizeof (sin6
->sin6_addr
) + 1;
2383 i
= XINT (p
->contents
[--len
]);
2384 sin6
->sin6_port
= htons (i
);
2385 for (i
= 0; i
< len
; i
++)
2386 if (INTEGERP (p
->contents
[i
]))
2388 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2391 sa
->sa_family
= family
;
2398 else if (STRINGP (address
))
2400 #ifdef HAVE_LOCAL_SOCKETS
2401 if (family
== AF_LOCAL
)
2403 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2404 cp
= SDATA (address
);
2405 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2406 sockun
->sun_path
[i
] = *cp
++;
2407 sa
->sa_family
= family
;
2414 p
= XVECTOR (XCDR (address
));
2415 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2418 for (i
= 0; i
< len
; i
++)
2419 if (INTEGERP (p
->contents
[i
]))
2420 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2423 #ifdef DATAGRAM_SOCKETS
2424 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2426 doc
: /* Get the current datagram address associated with PROCESS. */)
2427 (Lisp_Object process
)
2431 CHECK_PROCESS (process
);
2433 if (!DATAGRAM_CONN_P (process
))
2436 channel
= XPROCESS (process
)->infd
;
2437 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2438 datagram_address
[channel
].len
);
2441 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2443 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2444 Returns nil upon error setting address, ADDRESS otherwise. */)
2445 (Lisp_Object process
, Lisp_Object address
)
2450 CHECK_PROCESS (process
);
2452 if (!DATAGRAM_CONN_P (process
))
2455 channel
= XPROCESS (process
)->infd
;
2457 len
= get_lisp_to_sockaddr_size (address
, &family
);
2458 if (datagram_address
[channel
].len
!= len
)
2460 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2466 static const struct socket_options
{
2467 /* The name of this option. Should be lowercase version of option
2468 name without SO_ prefix. */
2470 /* Option level SOL_... */
2472 /* Option number SO_... */
2474 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2475 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2476 } socket_options
[] =
2478 #ifdef SO_BINDTODEVICE
2479 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2482 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2485 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2488 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2491 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2494 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2497 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2500 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2502 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2505 /* Set option OPT to value VAL on socket S.
2507 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2508 Signals an error if setting a known option fails.
2512 set_socket_option (int s
, Lisp_Object opt
, Lisp_Object val
)
2515 const struct socket_options
*sopt
;
2520 name
= SSDATA (SYMBOL_NAME (opt
));
2521 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2522 if (strcmp (name
, sopt
->name
) == 0)
2525 switch (sopt
->opttype
)
2530 optval
= NILP (val
) ? 0 : 1;
2531 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2532 &optval
, sizeof (optval
));
2540 optval
= XINT (val
);
2542 error ("Bad option value for %s", name
);
2543 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2544 &optval
, sizeof (optval
));
2548 #ifdef SO_BINDTODEVICE
2551 char devname
[IFNAMSIZ
+1];
2553 /* This is broken, at least in the Linux 2.4 kernel.
2554 To unbind, the arg must be a zero integer, not the empty string.
2555 This should work on all systems. KFS. 2003-09-23. */
2556 memset (devname
, 0, sizeof devname
);
2559 char *arg
= SSDATA (val
);
2560 int len
= min (strlen (arg
), IFNAMSIZ
);
2561 memcpy (devname
, arg
, len
);
2563 else if (!NILP (val
))
2564 error ("Bad option value for %s", name
);
2565 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2574 struct linger linger
;
2577 linger
.l_linger
= 0;
2579 linger
.l_linger
= XINT (val
);
2581 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2582 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2583 &linger
, sizeof (linger
));
2593 report_file_error ("Cannot set network option",
2594 Fcons (opt
, Fcons (val
, Qnil
)));
2595 return (1 << sopt
->optbit
);
2599 DEFUN ("set-network-process-option",
2600 Fset_network_process_option
, Sset_network_process_option
,
2602 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2603 See `make-network-process' for a list of options and values.
2604 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2605 OPTION is not a supported option, return nil instead; otherwise return t. */)
2606 (Lisp_Object process
, Lisp_Object option
, Lisp_Object value
, Lisp_Object no_error
)
2609 struct Lisp_Process
*p
;
2611 CHECK_PROCESS (process
);
2612 p
= XPROCESS (process
);
2613 if (!NETCONN1_P (p
))
2614 error ("Process is not a network process");
2618 error ("Process is not running");
2620 if (set_socket_option (s
, option
, value
))
2622 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2626 if (NILP (no_error
))
2627 error ("Unknown or unsupported option");
2633 DEFUN ("serial-process-configure",
2634 Fserial_process_configure
,
2635 Sserial_process_configure
,
2637 doc
: /* Configure speed, bytesize, etc. of a serial process.
2639 Arguments are specified as keyword/argument pairs. Attributes that
2640 are not given are re-initialized from the process's current
2641 configuration (available via the function `process-contact') or set to
2642 reasonable default values. The following arguments are defined:
2648 -- Any of these arguments can be given to identify the process that is
2649 to be configured. If none of these arguments is given, the current
2650 buffer's process is used.
2652 :speed SPEED -- SPEED is the speed of the serial port in bits per
2653 second, also called baud rate. Any value can be given for SPEED, but
2654 most serial ports work only at a few defined values between 1200 and
2655 115200, with 9600 being the most common value. If SPEED is nil, the
2656 serial port is not configured any further, i.e., all other arguments
2657 are ignored. This may be useful for special serial ports such as
2658 Bluetooth-to-serial converters which can only be configured through AT
2659 commands. A value of nil for SPEED can be used only when passed
2660 through `make-serial-process' or `serial-term'.
2662 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2663 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2665 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2666 `odd' (use odd parity), or the symbol `even' (use even parity). If
2667 PARITY is not given, no parity is used.
2669 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2670 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2671 is not given or nil, 1 stopbit is used.
2673 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2674 flowcontrol to be used, which is either nil (don't use flowcontrol),
2675 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2676 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2677 flowcontrol is used.
2679 `serial-process-configure' is called by `make-serial-process' for the
2680 initial configuration of the serial port.
2684 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2686 \(serial-process-configure
2687 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2689 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2691 usage: (serial-process-configure &rest ARGS) */)
2692 (int nargs
, Lisp_Object
*args
)
2694 struct Lisp_Process
*p
;
2695 Lisp_Object contact
= Qnil
;
2696 Lisp_Object proc
= Qnil
;
2697 struct gcpro gcpro1
;
2699 contact
= Flist (nargs
, args
);
2702 proc
= Fplist_get (contact
, QCprocess
);
2704 proc
= Fplist_get (contact
, QCname
);
2706 proc
= Fplist_get (contact
, QCbuffer
);
2708 proc
= Fplist_get (contact
, QCport
);
2709 proc
= get_process (proc
);
2710 p
= XPROCESS (proc
);
2711 if (!EQ (p
->type
, Qserial
))
2712 error ("Not a serial process");
2714 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2720 serial_configure (p
, contact
);
2726 /* Used by make-serial-process to recover from errors. */
2728 make_serial_process_unwind (Lisp_Object proc
)
2730 if (!PROCESSP (proc
))
2732 remove_process (proc
);
2736 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2738 doc
: /* Create and return a serial port process.
2740 In Emacs, serial port connections are represented by process objects,
2741 so input and output work as for subprocesses, and `delete-process'
2742 closes a serial port connection. However, a serial process has no
2743 process id, it cannot be signaled, and the status codes are different
2744 from normal processes.
2746 `make-serial-process' creates a process and a buffer, on which you
2747 probably want to use `process-send-string'. Try \\[serial-term] for
2748 an interactive terminal. See below for examples.
2750 Arguments are specified as keyword/argument pairs. The following
2751 arguments are defined:
2753 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2754 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2755 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2756 the backslashes in strings).
2758 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2759 which is called by `make-serial-process'.
2761 :name NAME -- NAME is the name of the process. If NAME is not given,
2762 the value of PORT is used.
2764 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2765 with the process. Process output goes at the end of that buffer,
2766 unless you specify an output stream or filter function to handle the
2767 output. If BUFFER is not given, the value of NAME is used.
2769 :coding CODING -- If CODING is a symbol, it specifies the coding
2770 system used for both reading and writing for this process. If CODING
2771 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2772 ENCODING is used for writing.
2774 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2775 the process is running. If BOOL is not given, query before exiting.
2777 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2778 In the stopped state, a serial process does not accept incoming data,
2779 but you can send outgoing data. The stopped state is cleared by
2780 `continue-process' and set by `stop-process'.
2782 :filter FILTER -- Install FILTER as the process filter.
2784 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2786 :plist PLIST -- Install PLIST as the initial plist of the process.
2793 -- These arguments are handled by `serial-process-configure', which is
2794 called by `make-serial-process'.
2796 The original argument list, possibly modified by later configuration,
2797 is available via the function `process-contact'.
2801 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2803 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2805 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2807 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2809 usage: (make-serial-process &rest ARGS) */)
2810 (int nargs
, Lisp_Object
*args
)
2813 Lisp_Object proc
, contact
, port
;
2814 struct Lisp_Process
*p
;
2815 struct gcpro gcpro1
;
2816 Lisp_Object name
, buffer
;
2817 Lisp_Object tem
, val
;
2818 int specpdl_count
= -1;
2823 contact
= Flist (nargs
, args
);
2826 port
= Fplist_get (contact
, QCport
);
2828 error ("No port specified");
2829 CHECK_STRING (port
);
2831 if (NILP (Fplist_member (contact
, QCspeed
)))
2832 error (":speed not specified");
2833 if (!NILP (Fplist_get (contact
, QCspeed
)))
2834 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2836 name
= Fplist_get (contact
, QCname
);
2839 CHECK_STRING (name
);
2840 proc
= make_process (name
);
2841 specpdl_count
= SPECPDL_INDEX ();
2842 record_unwind_protect (make_serial_process_unwind
, proc
);
2843 p
= XPROCESS (proc
);
2845 fd
= serial_open (SSDATA (port
));
2848 if (fd
> max_process_desc
)
2849 max_process_desc
= fd
;
2850 chan_process
[fd
] = proc
;
2852 buffer
= Fplist_get (contact
, QCbuffer
);
2855 buffer
= Fget_buffer_create (buffer
);
2858 p
->childp
= contact
;
2859 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2861 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2862 p
->filter
= Fplist_get (contact
, QCfilter
);
2864 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2865 p
->kill_without_query
= 1;
2866 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2870 if (!EQ (p
->command
, Qt
))
2872 FD_SET (fd
, &input_wait_mask
);
2873 FD_SET (fd
, &non_keyboard_wait_mask
);
2876 if (BUFFERP (buffer
))
2878 set_marker_both (p
->mark
, buffer
,
2879 BUF_ZV (XBUFFER (buffer
)),
2880 BUF_ZV_BYTE (XBUFFER (buffer
)));
2883 tem
= Fplist_member (contact
, QCcoding
);
2884 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2890 val
= XCAR (XCDR (tem
));
2894 else if (!NILP (Vcoding_system_for_read
))
2895 val
= Vcoding_system_for_read
;
2896 else if ((!NILP (buffer
) && NILP (BVAR (XBUFFER (buffer
), enable_multibyte_characters
)))
2897 || (NILP (buffer
) && NILP (BVAR (&buffer_defaults
, enable_multibyte_characters
))))
2899 p
->decode_coding_system
= val
;
2904 val
= XCAR (XCDR (tem
));
2908 else if (!NILP (Vcoding_system_for_write
))
2909 val
= Vcoding_system_for_write
;
2910 else if ((!NILP (buffer
) && NILP (BVAR (XBUFFER (buffer
), enable_multibyte_characters
)))
2911 || (NILP (buffer
) && NILP (BVAR (&buffer_defaults
, enable_multibyte_characters
))))
2913 p
->encode_coding_system
= val
;
2915 setup_process_coding_systems (proc
);
2916 p
->decoding_buf
= empty_unibyte_string
;
2917 p
->decoding_carryover
= 0;
2918 p
->encoding_buf
= empty_unibyte_string
;
2919 p
->inherit_coding_system_flag
2920 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2922 Fserial_process_configure (nargs
, args
);
2924 specpdl_ptr
= specpdl
+ specpdl_count
;
2930 /* Create a network stream/datagram client/server process. Treated
2931 exactly like a normal process when reading and writing. Primary
2932 differences are in status display and process deletion. A network
2933 connection has no PID; you cannot signal it. All you can do is
2934 stop/continue it and deactivate/close it via delete-process */
2936 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2938 doc
: /* Create and return a network server or client process.
2940 In Emacs, network connections are represented by process objects, so
2941 input and output work as for subprocesses and `delete-process' closes
2942 a network connection. However, a network process has no process id,
2943 it cannot be signaled, and the status codes are different from normal
2946 Arguments are specified as keyword/argument pairs. The following
2947 arguments are defined:
2949 :name NAME -- NAME is name for process. It is modified if necessary
2952 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2953 with the process. Process output goes at end of that buffer, unless
2954 you specify an output stream or filter function to handle the output.
2955 BUFFER may be also nil, meaning that this process is not associated
2958 :host HOST -- HOST is name of the host to connect to, or its IP
2959 address. The symbol `local' specifies the local host. If specified
2960 for a server process, it must be a valid name or address for the local
2961 host, and only clients connecting to that address will be accepted.
2963 :service SERVICE -- SERVICE is name of the service desired, or an
2964 integer specifying a port number to connect to. If SERVICE is t,
2965 a random port number is selected for the server. (If Emacs was
2966 compiled with getaddrinfo, a port number can also be specified as a
2967 string, e.g. "80", as well as an integer. This is not portable.)
2969 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2970 stream type connection, `datagram' creates a datagram type connection,
2971 `seqpacket' creates a reliable datagram connection.
2973 :family FAMILY -- FAMILY is the address (and protocol) family for the
2974 service specified by HOST and SERVICE. The default (nil) is to use
2975 whatever address family (IPv4 or IPv6) that is defined for the host
2976 and port number specified by HOST and SERVICE. Other address families
2978 local -- for a local (i.e. UNIX) address specified by SERVICE.
2979 ipv4 -- use IPv4 address family only.
2980 ipv6 -- use IPv6 address family only.
2982 :local ADDRESS -- ADDRESS is the local address used for the connection.
2983 This parameter is ignored when opening a client process. When specified
2984 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2986 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2987 connection. This parameter is ignored when opening a stream server
2988 process. For a datagram server process, it specifies the initial
2989 setting of the remote datagram address. When specified for a client
2990 process, the FAMILY, HOST, and SERVICE args are ignored.
2992 The format of ADDRESS depends on the address family:
2993 - An IPv4 address is represented as an vector of integers [A B C D P]
2994 corresponding to numeric IP address A.B.C.D and port number P.
2995 - A local address is represented as a string with the address in the
2996 local address space.
2997 - An "unsupported family" address is represented by a cons (F . AV)
2998 where F is the family number and AV is a vector containing the socket
2999 address data with one element per address data byte. Do not rely on
3000 this format in portable code, as it may depend on implementation
3001 defined constants, data sizes, and data structure alignment.
3003 :coding CODING -- If CODING is a symbol, it specifies the coding
3004 system used for both reading and writing for this process. If CODING
3005 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3006 ENCODING is used for writing.
3008 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3009 return without waiting for the connection to complete; instead, the
3010 sentinel function will be called with second arg matching "open" (if
3011 successful) or "failed" when the connect completes. Default is to use
3012 a blocking connect (i.e. wait) for stream type connections.
3014 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3015 running when Emacs is exited.
3017 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3018 In the stopped state, a server process does not accept new
3019 connections, and a client process does not handle incoming traffic.
3020 The stopped state is cleared by `continue-process' and set by
3023 :filter FILTER -- Install FILTER as the process filter.
3025 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3026 process filter are multibyte, otherwise they are unibyte.
3027 If this keyword is not specified, the strings are multibyte if
3028 `default-enable-multibyte-characters' is non-nil.
3030 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3032 :log LOG -- Install LOG as the server process log function. This
3033 function is called when the server accepts a network connection from a
3034 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3035 is the server process, CLIENT is the new process for the connection,
3036 and MESSAGE is a string.
3038 :plist PLIST -- Install PLIST as the new process' initial plist.
3040 :server QLEN -- if QLEN is non-nil, create a server process for the
3041 specified FAMILY, SERVICE, and connection type (stream or datagram).
3042 If QLEN is an integer, it is used as the max. length of the server's
3043 pending connection queue (also known as the backlog); the default
3044 queue length is 5. Default is to create a client process.
3046 The following network options can be specified for this connection:
3048 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3049 :dontroute BOOL -- Only send to directly connected hosts.
3050 :keepalive BOOL -- Send keep-alive messages on network stream.
3051 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3052 :oobinline BOOL -- Place out-of-band data in receive data stream.
3053 :priority INT -- Set protocol defined priority for sent packets.
3054 :reuseaddr BOOL -- Allow reusing a recently used local address
3055 (this is allowed by default for a server process).
3056 :bindtodevice NAME -- bind to interface NAME. Using this may require
3057 special privileges on some systems.
3059 Consult the relevant system programmer's manual pages for more
3060 information on using these options.
3063 A server process will listen for and accept connections from clients.
3064 When a client connection is accepted, a new network process is created
3065 for the connection with the following parameters:
3067 - The client's process name is constructed by concatenating the server
3068 process' NAME and a client identification string.
3069 - If the FILTER argument is non-nil, the client process will not get a
3070 separate process buffer; otherwise, the client's process buffer is a newly
3071 created buffer named after the server process' BUFFER name or process
3072 NAME concatenated with the client identification string.
3073 - The connection type and the process filter and sentinel parameters are
3074 inherited from the server process' TYPE, FILTER and SENTINEL.
3075 - The client process' contact info is set according to the client's
3076 addressing information (typically an IP address and a port number).
3077 - The client process' plist is initialized from the server's plist.
3079 Notice that the FILTER and SENTINEL args are never used directly by
3080 the server process. Also, the BUFFER argument is not used directly by
3081 the server process, but via the optional :log function, accepted (and
3082 failed) connections may be logged in the server process' buffer.
3084 The original argument list, modified with the actual connection
3085 information, is available via the `process-contact' function.
3087 usage: (make-network-process &rest ARGS) */)
3088 (int nargs
, Lisp_Object
*args
)
3091 Lisp_Object contact
;
3092 struct Lisp_Process
*p
;
3093 #ifdef HAVE_GETADDRINFO
3094 struct addrinfo ai
, *res
, *lres
;
3095 struct addrinfo hints
;
3096 const char *portstring
;
3098 #else /* HAVE_GETADDRINFO */
3099 struct _emacs_addrinfo
3105 struct sockaddr
*ai_addr
;
3106 struct _emacs_addrinfo
*ai_next
;
3108 #endif /* HAVE_GETADDRINFO */
3109 struct sockaddr_in address_in
;
3110 #ifdef HAVE_LOCAL_SOCKETS
3111 struct sockaddr_un address_un
;
3116 int s
= -1, outch
, inch
;
3117 struct gcpro gcpro1
;
3118 int count
= SPECPDL_INDEX ();
3120 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3122 Lisp_Object name
, buffer
, host
, service
, address
;
3123 Lisp_Object filter
, sentinel
;
3124 int is_non_blocking_client
= 0;
3125 int is_server
= 0, backlog
= 5;
3132 /* Save arguments for process-contact and clone-process. */
3133 contact
= Flist (nargs
, args
);
3137 /* Ensure socket support is loaded if available. */
3138 init_winsock (TRUE
);
3141 /* :type TYPE (nil: stream, datagram */
3142 tem
= Fplist_get (contact
, QCtype
);
3144 socktype
= SOCK_STREAM
;
3145 #ifdef DATAGRAM_SOCKETS
3146 else if (EQ (tem
, Qdatagram
))
3147 socktype
= SOCK_DGRAM
;
3149 #ifdef HAVE_SEQPACKET
3150 else if (EQ (tem
, Qseqpacket
))
3151 socktype
= SOCK_SEQPACKET
;
3154 error ("Unsupported connection type");
3157 tem
= Fplist_get (contact
, QCserver
);
3160 /* Don't support network sockets when non-blocking mode is
3161 not available, since a blocked Emacs is not useful. */
3162 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3163 error ("Network servers not supported");
3167 backlog
= XINT (tem
);
3171 /* Make QCaddress an alias for :local (server) or :remote (client). */
3172 QCaddress
= is_server
? QClocal
: QCremote
;
3175 if (!is_server
&& socktype
!= SOCK_DGRAM
3176 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3178 #ifndef NON_BLOCKING_CONNECT
3179 error ("Non-blocking connect not supported");
3181 is_non_blocking_client
= 1;
3185 name
= Fplist_get (contact
, QCname
);
3186 buffer
= Fplist_get (contact
, QCbuffer
);
3187 filter
= Fplist_get (contact
, QCfilter
);
3188 sentinel
= Fplist_get (contact
, QCsentinel
);
3190 CHECK_STRING (name
);
3192 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3193 ai
.ai_socktype
= socktype
;
3198 /* :local ADDRESS or :remote ADDRESS */
3199 address
= Fplist_get (contact
, QCaddress
);
3200 if (!NILP (address
))
3202 host
= service
= Qnil
;
3204 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3205 error ("Malformed :address");
3206 ai
.ai_family
= family
;
3207 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3208 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3212 /* :family FAMILY -- nil (for Inet), local, or integer. */
3213 tem
= Fplist_get (contact
, QCfamily
);
3216 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3222 #ifdef HAVE_LOCAL_SOCKETS
3223 else if (EQ (tem
, Qlocal
))
3227 else if (EQ (tem
, Qipv6
))
3230 else if (EQ (tem
, Qipv4
))
3232 else if (INTEGERP (tem
))
3233 family
= XINT (tem
);
3235 error ("Unknown address family");
3237 ai
.ai_family
= family
;
3239 /* :service SERVICE -- string, integer (port number), or t (random port). */
3240 service
= Fplist_get (contact
, QCservice
);
3242 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3243 host
= Fplist_get (contact
, QChost
);
3246 if (EQ (host
, Qlocal
))
3247 /* Depending on setup, "localhost" may map to different IPv4 and/or
3248 IPv6 addresses, so it's better to be explicit. (Bug#6781) */
3249 host
= build_string ("127.0.0.1");
3250 CHECK_STRING (host
);
3253 #ifdef HAVE_LOCAL_SOCKETS
3254 if (family
== AF_LOCAL
)
3258 message (":family local ignores the :host \"%s\" property",
3260 contact
= Fplist_put (contact
, QChost
, Qnil
);
3263 CHECK_STRING (service
);
3264 memset (&address_un
, 0, sizeof address_un
);
3265 address_un
.sun_family
= AF_LOCAL
;
3266 strncpy (address_un
.sun_path
, SSDATA (service
), sizeof address_un
.sun_path
);
3267 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3268 ai
.ai_addrlen
= sizeof address_un
;
3273 /* Slow down polling to every ten seconds.
3274 Some kernels have a bug which causes retrying connect to fail
3275 after a connect. Polling can interfere with gethostbyname too. */
3276 #ifdef POLL_FOR_INPUT
3277 if (socktype
!= SOCK_DGRAM
)
3279 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3280 bind_polling_period (10);
3284 #ifdef HAVE_GETADDRINFO
3285 /* If we have a host, use getaddrinfo to resolve both host and service.
3286 Otherwise, use getservbyname to lookup the service. */
3290 /* SERVICE can either be a string or int.
3291 Convert to a C string for later use by getaddrinfo. */
3292 if (EQ (service
, Qt
))
3294 else if (INTEGERP (service
))
3296 sprintf (portbuf
, "%ld", (long) XINT (service
));
3297 portstring
= portbuf
;
3301 CHECK_STRING (service
);
3302 portstring
= SSDATA (service
);
3307 memset (&hints
, 0, sizeof (hints
));
3309 hints
.ai_family
= family
;
3310 hints
.ai_socktype
= socktype
;
3311 hints
.ai_protocol
= 0;
3313 #ifdef HAVE_RES_INIT
3317 ret
= getaddrinfo (SSDATA (host
), portstring
, &hints
, &res
);
3319 #ifdef HAVE_GAI_STRERROR
3320 error ("%s/%s %s", SSDATA (host
), portstring
, gai_strerror (ret
));
3322 error ("%s/%s getaddrinfo error %d", SSDATA (host
), portstring
, ret
);
3328 #endif /* HAVE_GETADDRINFO */
3330 /* We end up here if getaddrinfo is not defined, or in case no hostname
3331 has been specified (e.g. for a local server process). */
3333 if (EQ (service
, Qt
))
3335 else if (INTEGERP (service
))
3336 port
= htons ((unsigned short) XINT (service
));
3339 struct servent
*svc_info
;
3340 CHECK_STRING (service
);
3341 svc_info
= getservbyname (SSDATA (service
),
3342 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3344 error ("Unknown service: %s", SDATA (service
));
3345 port
= svc_info
->s_port
;
3348 memset (&address_in
, 0, sizeof address_in
);
3349 address_in
.sin_family
= family
;
3350 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3351 address_in
.sin_port
= port
;
3353 #ifndef HAVE_GETADDRINFO
3356 struct hostent
*host_info_ptr
;
3358 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3359 as it may `hang' Emacs for a very long time. */
3363 #ifdef HAVE_RES_INIT
3367 host_info_ptr
= gethostbyname (SDATA (host
));
3372 memcpy (&address_in
.sin_addr
, host_info_ptr
->h_addr
,
3373 host_info_ptr
->h_length
);
3374 family
= host_info_ptr
->h_addrtype
;
3375 address_in
.sin_family
= family
;
3378 /* Attempt to interpret host as numeric inet address */
3380 unsigned long numeric_addr
;
3381 numeric_addr
= inet_addr (SSDATA (host
));
3382 if (numeric_addr
== -1)
3383 error ("Unknown host \"%s\"", SDATA (host
));
3385 memcpy (&address_in
.sin_addr
, &numeric_addr
,
3386 sizeof (address_in
.sin_addr
));
3390 #endif /* not HAVE_GETADDRINFO */
3392 ai
.ai_family
= family
;
3393 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3394 ai
.ai_addrlen
= sizeof address_in
;
3398 /* Do this in case we never enter the for-loop below. */
3399 count1
= SPECPDL_INDEX ();
3402 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3408 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3415 #ifdef DATAGRAM_SOCKETS
3416 if (!is_server
&& socktype
== SOCK_DGRAM
)
3418 #endif /* DATAGRAM_SOCKETS */
3420 #ifdef NON_BLOCKING_CONNECT
3421 if (is_non_blocking_client
)
3424 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3426 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3438 /* Make us close S if quit. */
3439 record_unwind_protect (close_file_unwind
, make_number (s
));
3441 /* Parse network options in the arg list.
3442 We simply ignore anything which isn't a known option (including other keywords).
3443 An error is signaled if setting a known option fails. */
3444 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3445 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3449 /* Configure as a server socket. */
3451 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3452 explicit :reuseaddr key to override this. */
3453 #ifdef HAVE_LOCAL_SOCKETS
3454 if (family
!= AF_LOCAL
)
3456 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3459 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3460 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3463 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3464 report_file_error ("Cannot bind server socket", Qnil
);
3466 #ifdef HAVE_GETSOCKNAME
3467 if (EQ (service
, Qt
))
3469 struct sockaddr_in sa1
;
3470 socklen_t len1
= sizeof (sa1
);
3471 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3473 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3474 service
= make_number (ntohs (sa1
.sin_port
));
3475 contact
= Fplist_put (contact
, QCservice
, service
);
3480 if (socktype
!= SOCK_DGRAM
&& listen (s
, backlog
))
3481 report_file_error ("Cannot listen on server socket", Qnil
);
3489 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3492 if (ret
== 0 || xerrno
== EISCONN
)
3494 /* The unwind-protect will be discarded afterwards.
3495 Likewise for immediate_quit. */
3499 #ifdef NON_BLOCKING_CONNECT
3501 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3505 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3512 if (xerrno
== EINTR
)
3514 /* Unlike most other syscalls connect() cannot be called
3515 again. (That would return EALREADY.) The proper way to
3516 wait for completion is select(). */
3524 sc
= select (s
+ 1, (SELECT_TYPE
*)0, &fdset
, (SELECT_TYPE
*)0,
3531 report_file_error ("select failed", Qnil
);
3535 len
= sizeof xerrno
;
3536 eassert (FD_ISSET (s
, &fdset
));
3537 if (getsockopt (s
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &len
) == -1)
3538 report_file_error ("getsockopt failed", Qnil
);
3540 errno
= xerrno
, report_file_error ("error during connect", Qnil
);
3544 #endif /* !WINDOWSNT */
3548 /* Discard the unwind protect closing S. */
3549 specpdl_ptr
= specpdl
+ count1
;
3554 if (xerrno
== EINTR
)
3561 #ifdef DATAGRAM_SOCKETS
3562 if (socktype
== SOCK_DGRAM
)
3564 if (datagram_address
[s
].sa
)
3566 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3567 datagram_address
[s
].len
= lres
->ai_addrlen
;
3571 memset (datagram_address
[s
].sa
, 0, lres
->ai_addrlen
);
3572 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3575 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3576 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3577 conv_lisp_to_sockaddr (rfamily
, remote
,
3578 datagram_address
[s
].sa
, rlen
);
3582 memcpy (datagram_address
[s
].sa
, lres
->ai_addr
, lres
->ai_addrlen
);
3585 contact
= Fplist_put (contact
, QCaddress
,
3586 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3587 #ifdef HAVE_GETSOCKNAME
3590 struct sockaddr_in sa1
;
3591 socklen_t len1
= sizeof (sa1
);
3592 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3593 contact
= Fplist_put (contact
, QClocal
,
3594 conv_sockaddr_to_lisp ((struct sockaddr
*)&sa1
, len1
));
3601 #ifdef HAVE_GETADDRINFO
3610 /* Discard the unwind protect for closing S, if any. */
3611 specpdl_ptr
= specpdl
+ count1
;
3613 /* Unwind bind_polling_period and request_sigio. */
3614 unbind_to (count
, Qnil
);
3618 /* If non-blocking got this far - and failed - assume non-blocking is
3619 not supported after all. This is probably a wrong assumption, but
3620 the normal blocking calls to open-network-stream handles this error
3622 if (is_non_blocking_client
)
3627 report_file_error ("make server process failed", contact
);
3629 report_file_error ("make client process failed", contact
);
3636 buffer
= Fget_buffer_create (buffer
);
3637 proc
= make_process (name
);
3639 chan_process
[inch
] = proc
;
3642 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3645 fcntl (inch
, F_SETFL
, O_NDELAY
);
3649 p
= XPROCESS (proc
);
3651 p
->childp
= contact
;
3652 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3656 p
->sentinel
= sentinel
;
3658 p
->log
= Fplist_get (contact
, QClog
);
3659 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3660 p
->kill_without_query
= 1;
3661 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3666 if (is_server
&& socktype
!= SOCK_DGRAM
)
3667 p
->status
= Qlisten
;
3669 /* Make the process marker point into the process buffer (if any). */
3670 if (BUFFERP (buffer
))
3671 set_marker_both (p
->mark
, buffer
,
3672 BUF_ZV (XBUFFER (buffer
)),
3673 BUF_ZV_BYTE (XBUFFER (buffer
)));
3675 #ifdef NON_BLOCKING_CONNECT
3676 if (is_non_blocking_client
)
3678 /* We may get here if connect did succeed immediately. However,
3679 in that case, we still need to signal this like a non-blocking
3681 p
->status
= Qconnect
;
3682 if (!FD_ISSET (inch
, &connect_wait_mask
))
3684 FD_SET (inch
, &connect_wait_mask
);
3685 FD_SET (inch
, &write_mask
);
3686 num_pending_connects
++;
3691 /* A server may have a client filter setting of Qt, but it must
3692 still listen for incoming connects unless it is stopped. */
3693 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3694 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3696 FD_SET (inch
, &input_wait_mask
);
3697 FD_SET (inch
, &non_keyboard_wait_mask
);
3700 if (inch
> max_process_desc
)
3701 max_process_desc
= inch
;
3703 tem
= Fplist_member (contact
, QCcoding
);
3704 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3705 tem
= Qnil
; /* No error message (too late!). */
3708 /* Setup coding systems for communicating with the network stream. */
3709 struct gcpro gcpro1
;
3710 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3711 Lisp_Object coding_systems
= Qt
;
3712 Lisp_Object args
[5], val
;
3716 val
= XCAR (XCDR (tem
));
3720 else if (!NILP (Vcoding_system_for_read
))
3721 val
= Vcoding_system_for_read
;
3722 else if ((!NILP (buffer
) && NILP (BVAR (XBUFFER (buffer
), enable_multibyte_characters
)))
3723 || (NILP (buffer
) && NILP (BVAR (&buffer_defaults
, enable_multibyte_characters
))))
3724 /* We dare not decode end-of-line format by setting VAL to
3725 Qraw_text, because the existing Emacs Lisp libraries
3726 assume that they receive bare code including a sequene of
3731 if (NILP (host
) || NILP (service
))
3732 coding_systems
= Qnil
;
3735 args
[0] = Qopen_network_stream
, args
[1] = name
,
3736 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3738 coding_systems
= Ffind_operation_coding_system (5, args
);
3741 if (CONSP (coding_systems
))
3742 val
= XCAR (coding_systems
);
3743 else if (CONSP (Vdefault_process_coding_system
))
3744 val
= XCAR (Vdefault_process_coding_system
);
3748 p
->decode_coding_system
= val
;
3752 val
= XCAR (XCDR (tem
));
3756 else if (!NILP (Vcoding_system_for_write
))
3757 val
= Vcoding_system_for_write
;
3758 else if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
3762 if (EQ (coding_systems
, Qt
))
3764 if (NILP (host
) || NILP (service
))
3765 coding_systems
= Qnil
;
3768 args
[0] = Qopen_network_stream
, args
[1] = name
,
3769 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3771 coding_systems
= Ffind_operation_coding_system (5, args
);
3775 if (CONSP (coding_systems
))
3776 val
= XCDR (coding_systems
);
3777 else if (CONSP (Vdefault_process_coding_system
))
3778 val
= XCDR (Vdefault_process_coding_system
);
3782 p
->encode_coding_system
= val
;
3784 setup_process_coding_systems (proc
);
3786 p
->decoding_buf
= empty_unibyte_string
;
3787 p
->decoding_carryover
= 0;
3788 p
->encoding_buf
= empty_unibyte_string
;
3790 p
->inherit_coding_system_flag
3791 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3798 #if defined(HAVE_NET_IF_H)
3801 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3802 doc
: /* Return an alist of all network interfaces and their network address.
3803 Each element is a cons, the car of which is a string containing the
3804 interface name, and the cdr is the network address in internal
3805 format; see the description of ADDRESS in `make-network-process'. */)
3808 struct ifconf ifconf
;
3809 struct ifreq
*ifreqs
= NULL
;
3814 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3820 buf_size
= ifaces
* sizeof (ifreqs
[0]);
3821 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3828 ifconf
.ifc_len
= buf_size
;
3829 ifconf
.ifc_req
= ifreqs
;
3830 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3836 if (ifconf
.ifc_len
== buf_size
)
3840 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3843 while (--ifaces
>= 0)
3845 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3846 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3847 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3849 memcpy (namebuf
, ifq
->ifr_name
, sizeof (ifq
->ifr_name
));
3850 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3851 res
= Fcons (Fcons (build_string (namebuf
),
3852 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3853 sizeof (struct sockaddr
))),
3859 #endif /* SIOCGIFCONF */
3861 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3865 const char *flag_sym
;
3868 static const struct ifflag_def ifflag_table
[] = {
3872 #ifdef IFF_BROADCAST
3873 { IFF_BROADCAST
, "broadcast" },
3876 { IFF_DEBUG
, "debug" },
3879 { IFF_LOOPBACK
, "loopback" },
3881 #ifdef IFF_POINTOPOINT
3882 { IFF_POINTOPOINT
, "pointopoint" },
3885 { IFF_RUNNING
, "running" },
3888 { IFF_NOARP
, "noarp" },
3891 { IFF_PROMISC
, "promisc" },
3893 #ifdef IFF_NOTRAILERS
3894 { IFF_NOTRAILERS
, "notrailers" },
3897 { IFF_ALLMULTI
, "allmulti" },
3900 { IFF_MASTER
, "master" },
3903 { IFF_SLAVE
, "slave" },
3905 #ifdef IFF_MULTICAST
3906 { IFF_MULTICAST
, "multicast" },
3909 { IFF_PORTSEL
, "portsel" },
3911 #ifdef IFF_AUTOMEDIA
3912 { IFF_AUTOMEDIA
, "automedia" },
3915 { IFF_DYNAMIC
, "dynamic" },
3918 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3921 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3924 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3927 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3930 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3935 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3936 doc
: /* Return information about network interface named IFNAME.
3937 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3938 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3939 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3940 FLAGS is the current flags of the interface. */)
3941 (Lisp_Object ifname
)
3944 Lisp_Object res
= Qnil
;
3949 CHECK_STRING (ifname
);
3951 memset (rq
.ifr_name
, 0, sizeof rq
.ifr_name
);
3952 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3954 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3959 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3960 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3962 int flags
= rq
.ifr_flags
;
3963 const struct ifflag_def
*fp
;
3967 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3969 if (flags
& fp
->flag_bit
)
3971 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3972 flags
-= fp
->flag_bit
;
3975 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3977 if (flags
& (1 << fnum
))
3979 elt
= Fcons (make_number (fnum
), elt
);
3984 res
= Fcons (elt
, res
);
3987 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3988 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3990 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3991 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3995 for (n
= 0; n
< 6; n
++)
3996 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3997 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
4000 res
= Fcons (elt
, res
);
4003 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4004 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4007 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4008 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4010 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4014 res
= Fcons (elt
, res
);
4017 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4018 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4021 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4024 res
= Fcons (elt
, res
);
4027 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4028 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4031 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4034 res
= Fcons (elt
, res
);
4038 return any
? res
: Qnil
;
4041 #endif /* defined(HAVE_NET_IF_H) */
4043 /* Turn off input and output for process PROC. */
4046 deactivate_process (Lisp_Object proc
)
4048 register int inchannel
, outchannel
;
4049 register struct Lisp_Process
*p
= XPROCESS (proc
);
4051 inchannel
= p
->infd
;
4052 outchannel
= p
->outfd
;
4054 #ifdef ADAPTIVE_READ_BUFFERING
4055 if (p
->read_output_delay
> 0)
4057 if (--process_output_delay_count
< 0)
4058 process_output_delay_count
= 0;
4059 p
->read_output_delay
= 0;
4060 p
->read_output_skip
= 0;
4066 /* Beware SIGCHLD hereabouts. */
4067 flush_pending_output (inchannel
);
4068 emacs_close (inchannel
);
4069 if (outchannel
>= 0 && outchannel
!= inchannel
)
4070 emacs_close (outchannel
);
4074 #ifdef DATAGRAM_SOCKETS
4075 if (DATAGRAM_CHAN_P (inchannel
))
4077 xfree (datagram_address
[inchannel
].sa
);
4078 datagram_address
[inchannel
].sa
= 0;
4079 datagram_address
[inchannel
].len
= 0;
4082 chan_process
[inchannel
] = Qnil
;
4083 FD_CLR (inchannel
, &input_wait_mask
);
4084 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4085 #ifdef NON_BLOCKING_CONNECT
4086 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4088 FD_CLR (inchannel
, &connect_wait_mask
);
4089 FD_CLR (inchannel
, &write_mask
);
4090 if (--num_pending_connects
< 0)
4094 if (inchannel
== max_process_desc
)
4097 /* We just closed the highest-numbered process input descriptor,
4098 so recompute the highest-numbered one now. */
4099 max_process_desc
= 0;
4100 for (i
= 0; i
< MAXDESC
; i
++)
4101 if (!NILP (chan_process
[i
]))
4102 max_process_desc
= i
;
4108 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4110 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4111 It is read into the process' buffers or given to their filter functions.
4112 Non-nil arg PROCESS means do not return until some output has been received
4115 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4116 and milliseconds to wait; return after that much time whether or not
4117 there is any subprocess output. If SECONDS is a floating point number,
4118 it specifies a fractional number of seconds to wait.
4119 The MILLISEC argument is obsolete and should be avoided.
4121 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4122 from PROCESS, suspending reading output from other processes.
4123 If JUST-THIS-ONE is an integer, don't run any timers either.
4124 Return non-nil if we received any output before the timeout expired. */)
4125 (register Lisp_Object process
, Lisp_Object seconds
, Lisp_Object millisec
, Lisp_Object just_this_one
)
4127 int secs
, usecs
= 0;
4129 if (! NILP (process
))
4130 CHECK_PROCESS (process
);
4132 just_this_one
= Qnil
;
4134 if (!NILP (millisec
))
4135 { /* Obsolete calling convention using integers rather than floats. */
4136 CHECK_NUMBER (millisec
);
4138 seconds
= make_float (XINT (millisec
) / 1000.0);
4141 CHECK_NUMBER (seconds
);
4142 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4146 if (!NILP (seconds
))
4148 if (INTEGERP (seconds
))
4149 secs
= XINT (seconds
);
4150 else if (FLOATP (seconds
))
4152 double timeout
= XFLOAT_DATA (seconds
);
4153 secs
= (int) timeout
;
4154 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4157 wrong_type_argument (Qnumberp
, seconds
);
4159 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4160 secs
= -1, usecs
= 0;
4163 secs
= NILP (process
) ? -1 : 0;
4166 (wait_reading_process_output (secs
, usecs
, 0, 0,
4168 !NILP (process
) ? XPROCESS (process
) : NULL
,
4169 NILP (just_this_one
) ? 0 :
4170 !INTEGERP (just_this_one
) ? 1 : -1)
4174 /* Accept a connection for server process SERVER on CHANNEL. */
4176 static int connect_counter
= 0;
4179 server_accept_connection (Lisp_Object server
, int channel
)
4181 Lisp_Object proc
, caller
, name
, buffer
;
4182 Lisp_Object contact
, host
, service
;
4183 struct Lisp_Process
*ps
= XPROCESS (server
);
4184 struct Lisp_Process
*p
;
4188 struct sockaddr_in in
;
4190 struct sockaddr_in6 in6
;
4192 #ifdef HAVE_LOCAL_SOCKETS
4193 struct sockaddr_un un
;
4196 socklen_t len
= sizeof saddr
;
4198 s
= accept (channel
, &saddr
.sa
, &len
);
4207 if (code
== EWOULDBLOCK
)
4211 if (!NILP (ps
->log
))
4212 call3 (ps
->log
, server
, Qnil
,
4213 concat3 (build_string ("accept failed with code"),
4214 Fnumber_to_string (make_number (code
)),
4215 build_string ("\n")));
4221 /* Setup a new process to handle the connection. */
4223 /* Generate a unique identification of the caller, and build contact
4224 information for this process. */
4227 switch (saddr
.sa
.sa_family
)
4231 Lisp_Object args
[5];
4232 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4233 args
[0] = build_string ("%d.%d.%d.%d");
4234 args
[1] = make_number (*ip
++);
4235 args
[2] = make_number (*ip
++);
4236 args
[3] = make_number (*ip
++);
4237 args
[4] = make_number (*ip
++);
4238 host
= Fformat (5, args
);
4239 service
= make_number (ntohs (saddr
.in
.sin_port
));
4241 args
[0] = build_string (" <%s:%d>");
4244 caller
= Fformat (3, args
);
4251 Lisp_Object args
[9];
4252 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4254 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4255 for (i
= 0; i
< 8; i
++)
4256 args
[i
+1] = make_number (ntohs (ip6
[i
]));
4257 host
= Fformat (9, args
);
4258 service
= make_number (ntohs (saddr
.in
.sin_port
));
4260 args
[0] = build_string (" <[%s]:%d>");
4263 caller
= Fformat (3, args
);
4268 #ifdef HAVE_LOCAL_SOCKETS
4272 caller
= Fnumber_to_string (make_number (connect_counter
));
4273 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4277 /* Create a new buffer name for this process if it doesn't have a
4278 filter. The new buffer name is based on the buffer name or
4279 process name of the server process concatenated with the caller
4282 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4286 buffer
= ps
->buffer
;
4288 buffer
= Fbuffer_name (buffer
);
4293 buffer
= concat2 (buffer
, caller
);
4294 buffer
= Fget_buffer_create (buffer
);
4298 /* Generate a unique name for the new server process. Combine the
4299 server process name with the caller identification. */
4301 name
= concat2 (ps
->name
, caller
);
4302 proc
= make_process (name
);
4304 chan_process
[s
] = proc
;
4307 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4310 fcntl (s
, F_SETFL
, O_NDELAY
);
4314 p
= XPROCESS (proc
);
4316 /* Build new contact information for this setup. */
4317 contact
= Fcopy_sequence (ps
->childp
);
4318 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4319 contact
= Fplist_put (contact
, QChost
, host
);
4320 if (!NILP (service
))
4321 contact
= Fplist_put (contact
, QCservice
, service
);
4322 contact
= Fplist_put (contact
, QCremote
,
4323 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4324 #ifdef HAVE_GETSOCKNAME
4326 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4327 contact
= Fplist_put (contact
, QClocal
,
4328 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4331 p
->childp
= contact
;
4332 p
->plist
= Fcopy_sequence (ps
->plist
);
4336 p
->sentinel
= ps
->sentinel
;
4337 p
->filter
= ps
->filter
;
4344 /* Client processes for accepted connections are not stopped initially. */
4345 if (!EQ (p
->filter
, Qt
))
4347 FD_SET (s
, &input_wait_mask
);
4348 FD_SET (s
, &non_keyboard_wait_mask
);
4351 if (s
> max_process_desc
)
4352 max_process_desc
= s
;
4354 /* Setup coding system for new process based on server process.
4355 This seems to be the proper thing to do, as the coding system
4356 of the new process should reflect the settings at the time the
4357 server socket was opened; not the current settings. */
4359 p
->decode_coding_system
= ps
->decode_coding_system
;
4360 p
->encode_coding_system
= ps
->encode_coding_system
;
4361 setup_process_coding_systems (proc
);
4363 p
->decoding_buf
= empty_unibyte_string
;
4364 p
->decoding_carryover
= 0;
4365 p
->encoding_buf
= empty_unibyte_string
;
4367 p
->inherit_coding_system_flag
4368 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4370 if (!NILP (ps
->log
))
4371 call3 (ps
->log
, server
, proc
,
4372 concat3 (build_string ("accept from "),
4373 (STRINGP (host
) ? host
: build_string ("-")),
4374 build_string ("\n")));
4376 if (!NILP (p
->sentinel
))
4377 exec_sentinel (proc
,
4378 concat3 (build_string ("open from "),
4379 (STRINGP (host
) ? host
: build_string ("-")),
4380 build_string ("\n")));
4383 /* This variable is different from waiting_for_input in keyboard.c.
4384 It is used to communicate to a lisp process-filter/sentinel (via the
4385 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4386 for user-input when that process-filter was called.
4387 waiting_for_input cannot be used as that is by definition 0 when
4388 lisp code is being evalled.
4389 This is also used in record_asynch_buffer_change.
4390 For that purpose, this must be 0
4391 when not inside wait_reading_process_output. */
4392 static int waiting_for_user_input_p
;
4395 wait_reading_process_output_unwind (Lisp_Object data
)
4397 waiting_for_user_input_p
= XINT (data
);
4401 /* This is here so breakpoints can be put on it. */
4403 wait_reading_process_output_1 (void)
4407 /* Use a wrapper around select to work around a bug in gdb 5.3.
4408 Normally, the wrapper is optimized away by inlining.
4410 If emacs is stopped inside select, the gdb backtrace doesn't
4411 show the function which called select, so it is practically
4412 impossible to step through wait_reading_process_output. */
4416 select_wrapper (int n
, fd_set
*rfd
, fd_set
*wfd
, fd_set
*xfd
, struct timeval
*tmo
)
4418 return select (n
, rfd
, wfd
, xfd
, tmo
);
4420 #define select select_wrapper
4423 /* Read and dispose of subprocess output while waiting for timeout to
4424 elapse and/or keyboard input to be available.
4427 timeout in seconds, or
4428 zero for no limit, or
4429 -1 means gobble data immediately available but don't wait for any.
4432 an additional duration to wait, measured in microseconds.
4433 If this is nonzero and time_limit is 0, then the timeout
4434 consists of MICROSECS only.
4436 READ_KBD is a lisp value:
4437 0 to ignore keyboard input, or
4438 1 to return when input is available, or
4439 -1 meaning caller will actually read the input, so don't throw to
4440 the quit handler, or
4442 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4443 output that arrives.
4445 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4446 (and gobble terminal input into the buffer if any arrives).
4448 If WAIT_PROC is specified, wait until something arrives from that
4449 process. The return value is true if we read some input from
4452 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4453 (suspending output from other processes). A negative value
4454 means don't run any timers either.
4456 If WAIT_PROC is specified, then the function returns true if we
4457 received input from that process before the timeout elapsed.
4458 Otherwise, return true if we received input from any process. */
4461 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
4463 Lisp_Object wait_for_cell
,
4464 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
4466 register int channel
, nfds
;
4467 SELECT_TYPE Available
;
4468 SELECT_TYPE Writeok
;
4470 int check_delay
, no_avail
;
4473 EMACS_TIME timeout
, end_time
;
4474 int wait_channel
= -1;
4475 int got_some_input
= 0;
4476 int count
= SPECPDL_INDEX ();
4478 FD_ZERO (&Available
);
4481 if (time_limit
== 0 && microsecs
== 0 && wait_proc
&& !NILP (Vinhibit_quit
)
4482 && !(CONSP (wait_proc
->status
) && EQ (XCAR (wait_proc
->status
), Qexit
)))
4483 message ("Blocking call to accept-process-output with quit inhibited!!");
4485 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4486 if (wait_proc
!= NULL
)
4487 wait_channel
= wait_proc
->infd
;
4489 record_unwind_protect (wait_reading_process_output_unwind
,
4490 make_number (waiting_for_user_input_p
));
4491 waiting_for_user_input_p
= read_kbd
;
4493 /* Since we may need to wait several times,
4494 compute the absolute time to return at. */
4495 if (time_limit
|| microsecs
)
4497 EMACS_GET_TIME (end_time
);
4498 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4499 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4504 int timeout_reduced_for_timers
= 0;
4506 /* If calling from keyboard input, do not quit
4507 since we want to return C-g as an input character.
4508 Otherwise, do pending quit if requested. */
4513 process_pending_signals ();
4516 /* Exit now if the cell we're waiting for became non-nil. */
4517 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4520 /* Compute time from now till when time limit is up */
4521 /* Exit if already run out */
4522 if (time_limit
== -1)
4524 /* -1 specified for timeout means
4525 gobble output available now
4526 but don't wait at all. */
4528 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4530 else if (time_limit
|| microsecs
)
4532 EMACS_GET_TIME (timeout
);
4533 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4534 if (EMACS_TIME_NEG_P (timeout
))
4539 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4542 /* Normally we run timers here.
4543 But not if wait_for_cell; in those cases,
4544 the wait is supposed to be short,
4545 and those callers cannot handle running arbitrary Lisp code here. */
4546 if (NILP (wait_for_cell
)
4547 && just_wait_proc
>= 0)
4549 EMACS_TIME timer_delay
;
4553 int old_timers_run
= timers_run
;
4554 struct buffer
*old_buffer
= current_buffer
;
4555 Lisp_Object old_window
= selected_window
;
4557 timer_delay
= timer_check (1);
4559 /* If a timer has run, this might have changed buffers
4560 an alike. Make read_key_sequence aware of that. */
4561 if (timers_run
!= old_timers_run
4562 && (old_buffer
!= current_buffer
4563 || !EQ (old_window
, selected_window
))
4564 && waiting_for_user_input_p
== -1)
4565 record_asynch_buffer_change ();
4567 if (timers_run
!= old_timers_run
&& do_display
)
4568 /* We must retry, since a timer may have requeued itself
4569 and that could alter the time_delay. */
4570 redisplay_preserve_echo_area (9);
4574 while (!detect_input_pending ());
4576 /* If there is unread keyboard input, also return. */
4578 && requeued_events_pending_p ())
4581 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4583 EMACS_TIME difference
;
4584 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4585 if (EMACS_TIME_NEG_P (difference
))
4587 timeout
= timer_delay
;
4588 timeout_reduced_for_timers
= 1;
4591 /* If time_limit is -1, we are not going to wait at all. */
4592 else if (time_limit
!= -1)
4594 /* This is so a breakpoint can be put here. */
4595 wait_reading_process_output_1 ();
4599 /* Cause C-g and alarm signals to take immediate action,
4600 and cause input available signals to zero out timeout.
4602 It is important that we do this before checking for process
4603 activity. If we get a SIGCHLD after the explicit checks for
4604 process activity, timeout is the only way we will know. */
4606 set_waiting_for_input (&timeout
);
4608 /* If status of something has changed, and no input is
4609 available, notify the user of the change right away. After
4610 this explicit check, we'll let the SIGCHLD handler zap
4611 timeout to get our attention. */
4612 if (update_tick
!= process_tick
)
4617 if (kbd_on_hold_p ())
4620 Atemp
= input_wait_mask
;
4623 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4624 if ((select (max (max_process_desc
, max_input_desc
) + 1,
4626 #ifdef NON_BLOCKING_CONNECT
4627 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4631 (SELECT_TYPE
*)0, &timeout
)
4634 /* It's okay for us to do this and then continue with
4635 the loop, since timeout has already been zeroed out. */
4636 clear_waiting_for_input ();
4637 status_notify (NULL
);
4638 if (do_display
) redisplay_preserve_echo_area (13);
4642 /* Don't wait for output from a non-running process. Just
4643 read whatever data has already been received. */
4644 if (wait_proc
&& wait_proc
->raw_status_new
)
4645 update_status (wait_proc
);
4647 && ! EQ (wait_proc
->status
, Qrun
)
4648 && ! EQ (wait_proc
->status
, Qconnect
))
4650 int nread
, total_nread
= 0;
4652 clear_waiting_for_input ();
4653 XSETPROCESS (proc
, wait_proc
);
4655 /* Read data from the process, until we exhaust it. */
4656 while (wait_proc
->infd
>= 0)
4658 nread
= read_process_output (proc
, wait_proc
->infd
);
4665 total_nread
+= nread
;
4669 else if (nread
== -1 && EIO
== errno
)
4673 else if (nread
== -1 && EAGAIN
== errno
)
4677 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4681 if (total_nread
> 0 && do_display
)
4682 redisplay_preserve_echo_area (10);
4687 /* Wait till there is something to do */
4689 if (wait_proc
&& just_wait_proc
)
4691 if (wait_proc
->infd
< 0) /* Terminated */
4693 FD_SET (wait_proc
->infd
, &Available
);
4697 else if (!NILP (wait_for_cell
))
4699 Available
= non_process_wait_mask
;
4706 Available
= non_keyboard_wait_mask
;
4708 Available
= input_wait_mask
;
4709 Writeok
= write_mask
;
4710 #ifdef SELECT_CANT_DO_WRITE_MASK
4715 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4718 /* If frame size has changed or the window is newly mapped,
4719 redisplay now, before we start to wait. There is a race
4720 condition here; if a SIGIO arrives between now and the select
4721 and indicates that a frame is trashed, the select may block
4722 displaying a trashed screen. */
4723 if (frame_garbaged
&& do_display
)
4725 clear_waiting_for_input ();
4726 redisplay_preserve_echo_area (11);
4728 set_waiting_for_input (&timeout
);
4732 if (read_kbd
&& detect_input_pending ())
4740 #ifdef ADAPTIVE_READ_BUFFERING
4741 /* Set the timeout for adaptive read buffering if any
4742 process has non-zero read_output_skip and non-zero
4743 read_output_delay, and we are not reading output for a
4744 specific wait_channel. It is not executed if
4745 Vprocess_adaptive_read_buffering is nil. */
4746 if (process_output_skip
&& check_delay
> 0)
4748 int usecs
= EMACS_USECS (timeout
);
4749 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4750 usecs
= READ_OUTPUT_DELAY_MAX
;
4751 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4753 proc
= chan_process
[channel
];
4756 /* Find minimum non-zero read_output_delay among the
4757 processes with non-zero read_output_skip. */
4758 if (XPROCESS (proc
)->read_output_delay
> 0)
4761 if (!XPROCESS (proc
)->read_output_skip
)
4763 FD_CLR (channel
, &Available
);
4764 XPROCESS (proc
)->read_output_skip
= 0;
4765 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4766 usecs
= XPROCESS (proc
)->read_output_delay
;
4769 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4770 process_output_skip
= 0;
4773 #if defined (USE_GTK) || defined (HAVE_GCONF)
4775 #elif defined (HAVE_NS)
4780 (max (max_process_desc
, max_input_desc
) + 1,
4782 (check_write
? &Writeok
: (SELECT_TYPE
*)0),
4783 (SELECT_TYPE
*)0, &timeout
);
4788 /* Make C-g and alarm signals set flags again */
4789 clear_waiting_for_input ();
4791 /* If we woke up due to SIGWINCH, actually change size now. */
4792 do_pending_window_change (0);
4794 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4795 /* We wanted the full specified time, so return now. */
4799 if (xerrno
== EINTR
)
4801 else if (xerrno
== EBADF
)
4804 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4805 the child's closure of the pts gives the parent a SIGHUP, and
4806 the ptc file descriptor is automatically closed,
4807 yielding EBADF here or at select() call above.
4808 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4809 in m/ibmrt-aix.h), and here we just ignore the select error.
4810 Cleanup occurs c/o status_notify after SIGCLD. */
4811 no_avail
= 1; /* Cannot depend on values returned */
4817 error ("select error: %s", emacs_strerror (xerrno
));
4822 FD_ZERO (&Available
);
4826 #if 0 /* When polling is used, interrupt_input is 0,
4827 so get_input_pending should read the input.
4828 So this should not be needed. */
4829 /* If we are using polling for input,
4830 and we see input available, make it get read now.
4831 Otherwise it might not actually get read for a second.
4832 And on hpux, since we turn off polling in wait_reading_process_output,
4833 it might never get read at all if we don't spend much time
4834 outside of wait_reading_process_output. */
4835 if (read_kbd
&& interrupt_input
4836 && keyboard_bit_set (&Available
)
4837 && input_polling_used ())
4838 kill (getpid (), SIGALRM
);
4841 /* Check for keyboard input */
4842 /* If there is any, return immediately
4843 to give it higher priority than subprocesses */
4847 int old_timers_run
= timers_run
;
4848 struct buffer
*old_buffer
= current_buffer
;
4849 Lisp_Object old_window
= selected_window
;
4852 if (detect_input_pending_run_timers (do_display
))
4854 swallow_events (do_display
);
4855 if (detect_input_pending_run_timers (do_display
))
4859 /* If a timer has run, this might have changed buffers
4860 an alike. Make read_key_sequence aware of that. */
4861 if (timers_run
!= old_timers_run
4862 && waiting_for_user_input_p
== -1
4863 && (old_buffer
!= current_buffer
4864 || !EQ (old_window
, selected_window
)))
4865 record_asynch_buffer_change ();
4871 /* If there is unread keyboard input, also return. */
4873 && requeued_events_pending_p ())
4876 /* If we are not checking for keyboard input now,
4877 do process events (but don't run any timers).
4878 This is so that X events will be processed.
4879 Otherwise they may have to wait until polling takes place.
4880 That would causes delays in pasting selections, for example.
4882 (We used to do this only if wait_for_cell.) */
4883 if (read_kbd
== 0 && detect_input_pending ())
4885 swallow_events (do_display
);
4886 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4887 if (detect_input_pending ())
4892 /* Exit now if the cell we're waiting for became non-nil. */
4893 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4897 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4898 go read it. This can happen with X on BSD after logging out.
4899 In that case, there really is no input and no SIGIO,
4900 but select says there is input. */
4902 if (read_kbd
&& interrupt_input
4903 && keyboard_bit_set (&Available
) && ! noninteractive
)
4904 kill (getpid (), SIGIO
);
4908 got_some_input
|= nfds
> 0;
4910 /* If checking input just got us a size-change event from X,
4911 obey it now if we should. */
4912 if (read_kbd
|| ! NILP (wait_for_cell
))
4913 do_pending_window_change (0);
4915 /* Check for data from a process. */
4916 if (no_avail
|| nfds
== 0)
4919 for (channel
= 0; channel
<= max_input_desc
; ++channel
)
4921 struct fd_callback_data
*d
= &fd_callback_info
[channel
];
4922 if (FD_ISSET (channel
, &Available
)
4924 && (d
->condition
& FOR_READ
) != 0)
4925 d
->func (channel
, d
->data
, 1);
4926 if (FD_ISSET (channel
, &write_mask
)
4928 && (d
->condition
& FOR_WRITE
) != 0)
4929 d
->func (channel
, d
->data
, 0);
4932 /* Really FIRST_PROC_DESC should be 0 on Unix,
4933 but this is safer in the short run. */
4934 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4936 if (FD_ISSET (channel
, &Available
)
4937 && FD_ISSET (channel
, &non_keyboard_wait_mask
)
4938 && !FD_ISSET (channel
, &non_process_wait_mask
))
4942 /* If waiting for this channel, arrange to return as
4943 soon as no more input to be processed. No more
4945 if (wait_channel
== channel
)
4951 proc
= chan_process
[channel
];
4955 /* If this is a server stream socket, accept connection. */
4956 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4958 server_accept_connection (proc
, channel
);
4962 /* Read data from the process, starting with our
4963 buffered-ahead character if we have one. */
4965 nread
= read_process_output (proc
, channel
);
4968 /* Since read_process_output can run a filter,
4969 which can call accept-process-output,
4970 don't try to read from any other processes
4971 before doing the select again. */
4972 FD_ZERO (&Available
);
4975 redisplay_preserve_echo_area (12);
4978 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4981 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4982 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4984 else if (nread
== -1 && errno
== EAGAIN
)
4988 else if (nread
== -1 && errno
== EAGAIN
)
4990 /* Note that we cannot distinguish between no input
4991 available now and a closed pipe.
4992 With luck, a closed pipe will be accompanied by
4993 subprocess termination and SIGCHLD. */
4994 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
4996 #endif /* O_NDELAY */
4997 #endif /* O_NONBLOCK */
4999 /* On some OSs with ptys, when the process on one end of
5000 a pty exits, the other end gets an error reading with
5001 errno = EIO instead of getting an EOF (0 bytes read).
5002 Therefore, if we get an error reading and errno =
5003 EIO, just continue, because the child process has
5004 exited and should clean itself up soon (e.g. when we
5007 However, it has been known to happen that the SIGCHLD
5008 got lost. So raise the signal again just in case.
5010 else if (nread
== -1 && errno
== EIO
)
5012 /* Clear the descriptor now, so we only raise the
5013 signal once. Don't do this if `process' is only
5015 if (XPROCESS (proc
)->pid
!= -2)
5017 FD_CLR (channel
, &input_wait_mask
);
5018 FD_CLR (channel
, &non_keyboard_wait_mask
);
5020 kill (getpid (), SIGCHLD
);
5023 #endif /* HAVE_PTYS */
5024 /* If we can detect process termination, don't consider the process
5025 gone just because its pipe is closed. */
5027 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5032 /* Preserve status of processes already terminated. */
5033 XPROCESS (proc
)->tick
= ++process_tick
;
5034 deactivate_process (proc
);
5035 if (XPROCESS (proc
)->raw_status_new
)
5036 update_status (XPROCESS (proc
));
5037 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5038 XPROCESS (proc
)->status
5039 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5042 #ifdef NON_BLOCKING_CONNECT
5043 if (FD_ISSET (channel
, &Writeok
)
5044 && FD_ISSET (channel
, &connect_wait_mask
))
5046 struct Lisp_Process
*p
;
5048 FD_CLR (channel
, &connect_wait_mask
);
5049 FD_CLR (channel
, &write_mask
);
5050 if (--num_pending_connects
< 0)
5053 proc
= chan_process
[channel
];
5057 p
= XPROCESS (proc
);
5060 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5061 So only use it on systems where it is known to work. */
5063 socklen_t xlen
= sizeof (xerrno
);
5064 if (getsockopt (channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5069 struct sockaddr pname
;
5070 int pnamelen
= sizeof (pname
);
5072 /* If connection failed, getpeername will fail. */
5074 if (getpeername (channel
, &pname
, &pnamelen
) < 0)
5076 /* Obtain connect failure code through error slippage. */
5079 if (errno
== ENOTCONN
&& read (channel
, &dummy
, 1) < 0)
5086 p
->tick
= ++process_tick
;
5087 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5088 deactivate_process (proc
);
5093 /* Execute the sentinel here. If we had relied on
5094 status_notify to do it later, it will read input
5095 from the process before calling the sentinel. */
5096 exec_sentinel (proc
, build_string ("open\n"));
5097 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5099 FD_SET (p
->infd
, &input_wait_mask
);
5100 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5104 #endif /* NON_BLOCKING_CONNECT */
5105 } /* end for each file descriptor */
5106 } /* end while exit conditions not met */
5108 unbind_to (count
, Qnil
);
5110 /* If calling from keyboard input, do not quit
5111 since we want to return C-g as an input character.
5112 Otherwise, do pending quit if requested. */
5115 /* Prevent input_pending from remaining set if we quit. */
5116 clear_input_pending ();
5120 return got_some_input
;
5123 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5126 read_process_output_call (Lisp_Object fun_and_args
)
5128 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5132 read_process_output_error_handler (Lisp_Object error
)
5134 cmd_error_internal (error
, "error in process filter: ");
5136 update_echo_area ();
5137 Fsleep_for (make_number (2), Qnil
);
5141 /* Read pending output from the process channel,
5142 starting with our buffered-ahead character if we have one.
5143 Yield number of decoded characters read.
5145 This function reads at most 4096 characters.
5146 If you want to read all available subprocess output,
5147 you must call it repeatedly until it returns zero.
5149 The characters read are decoded according to PROC's coding-system
5153 read_process_output (Lisp_Object proc
, register int channel
)
5155 register int nbytes
;
5157 register Lisp_Object outstream
;
5158 register struct Lisp_Process
*p
= XPROCESS (proc
);
5159 register EMACS_INT opoint
;
5160 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5161 int carryover
= p
->decoding_carryover
;
5163 int count
= SPECPDL_INDEX ();
5164 Lisp_Object odeactivate
;
5166 chars
= (char *) alloca (carryover
+ readmax
);
5168 /* See the comment above. */
5169 memcpy (chars
, SDATA (p
->decoding_buf
), carryover
);
5171 #ifdef DATAGRAM_SOCKETS
5172 /* We have a working select, so proc_buffered_char is always -1. */
5173 if (DATAGRAM_CHAN_P (channel
))
5175 socklen_t len
= datagram_address
[channel
].len
;
5176 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5177 0, datagram_address
[channel
].sa
, &len
);
5181 if (proc_buffered_char
[channel
] < 0)
5184 if (XPROCESS (proc
)->gnutls_p
)
5185 nbytes
= emacs_gnutls_read (channel
, XPROCESS (proc
),
5186 chars
+ carryover
, readmax
);
5189 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5190 #ifdef ADAPTIVE_READ_BUFFERING
5191 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5193 int delay
= p
->read_output_delay
;
5196 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5199 process_output_delay_count
++;
5200 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5203 else if (delay
> 0 && (nbytes
== readmax
))
5205 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5207 process_output_delay_count
--;
5209 p
->read_output_delay
= delay
;
5212 p
->read_output_skip
= 1;
5213 process_output_skip
= 1;
5220 chars
[carryover
] = proc_buffered_char
[channel
];
5221 proc_buffered_char
[channel
] = -1;
5223 if (XPROCESS (proc
)->gnutls_p
)
5224 nbytes
= emacs_gnutls_read (channel
, XPROCESS (proc
),
5225 chars
+ carryover
+ 1, readmax
- 1);
5228 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5232 nbytes
= nbytes
+ 1;
5235 p
->decoding_carryover
= 0;
5237 /* At this point, NBYTES holds number of bytes just received
5238 (including the one in proc_buffered_char[channel]). */
5241 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5243 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5246 /* Now set NBYTES how many bytes we must decode. */
5247 nbytes
+= carryover
;
5249 odeactivate
= Vdeactivate_mark
;
5250 /* There's no good reason to let process filters change the current
5251 buffer, and many callers of accept-process-output, sit-for, and
5252 friends don't expect current-buffer to be changed from under them. */
5253 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
5255 /* Read and dispose of the process output. */
5256 outstream
= p
->filter
;
5257 if (!NILP (outstream
))
5259 Lisp_Object obuffer
, okeymap
;
5261 int outer_running_asynch_code
= running_asynch_code
;
5262 int waiting
= waiting_for_user_input_p
;
5264 /* No need to gcpro these, because all we do with them later
5265 is test them for EQness, and none of them should be a string. */
5266 XSETBUFFER (obuffer
, current_buffer
);
5267 okeymap
= BVAR (current_buffer
, keymap
);
5269 /* We inhibit quit here instead of just catching it so that
5270 hitting ^G when a filter happens to be running won't screw
5272 specbind (Qinhibit_quit
, Qt
);
5273 specbind (Qlast_nonmenu_event
, Qt
);
5275 /* In case we get recursively called,
5276 and we already saved the match data nonrecursively,
5277 save the same match data in safely recursive fashion. */
5278 if (outer_running_asynch_code
)
5281 /* Don't clobber the CURRENT match data, either! */
5282 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5283 restore_search_regs ();
5284 record_unwind_save_match_data ();
5285 Fset_match_data (tem
, Qt
);
5288 /* For speed, if a search happens within this code,
5289 save the match data in a special nonrecursive fashion. */
5290 running_asynch_code
= 1;
5292 decode_coding_c_string (coding
, (unsigned char *) chars
, nbytes
, Qt
);
5293 text
= coding
->dst_object
;
5294 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5295 /* A new coding system might be found. */
5296 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5298 p
->decode_coding_system
= Vlast_coding_system_used
;
5300 /* Don't call setup_coding_system for
5301 proc_decode_coding_system[channel] here. It is done in
5302 detect_coding called via decode_coding above. */
5304 /* If a coding system for encoding is not yet decided, we set
5305 it as the same as coding-system for decoding.
5307 But, before doing that we must check if
5308 proc_encode_coding_system[p->outfd] surely points to a
5309 valid memory because p->outfd will be changed once EOF is
5310 sent to the process. */
5311 if (NILP (p
->encode_coding_system
)
5312 && proc_encode_coding_system
[p
->outfd
])
5314 p
->encode_coding_system
5315 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5316 setup_coding_system (p
->encode_coding_system
,
5317 proc_encode_coding_system
[p
->outfd
]);
5321 if (coding
->carryover_bytes
> 0)
5323 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5324 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5325 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5326 coding
->carryover_bytes
);
5327 p
->decoding_carryover
= coding
->carryover_bytes
;
5329 if (SBYTES (text
) > 0)
5330 internal_condition_case_1 (read_process_output_call
,
5332 Fcons (proc
, Fcons (text
, Qnil
))),
5333 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5334 read_process_output_error_handler
);
5336 /* If we saved the match data nonrecursively, restore it now. */
5337 restore_search_regs ();
5338 running_asynch_code
= outer_running_asynch_code
;
5340 /* Restore waiting_for_user_input_p as it was
5341 when we were called, in case the filter clobbered it. */
5342 waiting_for_user_input_p
= waiting
;
5344 #if 0 /* Call record_asynch_buffer_change unconditionally,
5345 because we might have changed minor modes or other things
5346 that affect key bindings. */
5347 if (! EQ (Fcurrent_buffer (), obuffer
)
5348 || ! EQ (current_buffer
->keymap
, okeymap
))
5350 /* But do it only if the caller is actually going to read events.
5351 Otherwise there's no need to make him wake up, and it could
5352 cause trouble (for example it would make sit_for return). */
5353 if (waiting_for_user_input_p
== -1)
5354 record_asynch_buffer_change ();
5357 /* If no filter, write into buffer if it isn't dead. */
5358 else if (!NILP (p
->buffer
) && !NILP (BVAR (XBUFFER (p
->buffer
), name
)))
5360 Lisp_Object old_read_only
;
5361 EMACS_INT old_begv
, old_zv
;
5362 EMACS_INT old_begv_byte
, old_zv_byte
;
5363 EMACS_INT before
, before_byte
;
5364 EMACS_INT opoint_byte
;
5368 Fset_buffer (p
->buffer
);
5370 opoint_byte
= PT_BYTE
;
5371 old_read_only
= BVAR (current_buffer
, read_only
);
5374 old_begv_byte
= BEGV_BYTE
;
5375 old_zv_byte
= ZV_BYTE
;
5377 BVAR (current_buffer
, read_only
) = Qnil
;
5379 /* Insert new output into buffer
5380 at the current end-of-output marker,
5381 thus preserving logical ordering of input and output. */
5382 if (XMARKER (p
->mark
)->buffer
)
5383 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5384 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5387 SET_PT_BOTH (ZV
, ZV_BYTE
);
5389 before_byte
= PT_BYTE
;
5391 /* If the output marker is outside of the visible region, save
5392 the restriction and widen. */
5393 if (! (BEGV
<= PT
&& PT
<= ZV
))
5396 decode_coding_c_string (coding
, (unsigned char *) chars
, nbytes
, Qt
);
5397 text
= coding
->dst_object
;
5398 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5399 /* A new coding system might be found. See the comment in the
5400 similar code in the previous `if' block. */
5401 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5403 p
->decode_coding_system
= Vlast_coding_system_used
;
5404 if (NILP (p
->encode_coding_system
)
5405 && proc_encode_coding_system
[p
->outfd
])
5407 p
->encode_coding_system
5408 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5409 setup_coding_system (p
->encode_coding_system
,
5410 proc_encode_coding_system
[p
->outfd
]);
5413 if (coding
->carryover_bytes
> 0)
5415 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5416 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5417 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5418 coding
->carryover_bytes
);
5419 p
->decoding_carryover
= coding
->carryover_bytes
;
5421 /* Adjust the multibyteness of TEXT to that of the buffer. */
5422 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
5423 != ! STRING_MULTIBYTE (text
))
5424 text
= (STRING_MULTIBYTE (text
)
5425 ? Fstring_as_unibyte (text
)
5426 : Fstring_to_multibyte (text
));
5427 /* Insert before markers in case we are inserting where
5428 the buffer's mark is, and the user's next command is Meta-y. */
5429 insert_from_string_before_markers (text
, 0, 0,
5430 SCHARS (text
), SBYTES (text
), 0);
5432 /* Make sure the process marker's position is valid when the
5433 process buffer is changed in the signal_after_change above.
5434 W3 is known to do that. */
5435 if (BUFFERP (p
->buffer
)
5436 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5437 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5439 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5441 update_mode_lines
++;
5443 /* Make sure opoint and the old restrictions
5444 float ahead of any new text just as point would. */
5445 if (opoint
>= before
)
5447 opoint
+= PT
- before
;
5448 opoint_byte
+= PT_BYTE
- before_byte
;
5450 if (old_begv
> before
)
5452 old_begv
+= PT
- before
;
5453 old_begv_byte
+= PT_BYTE
- before_byte
;
5455 if (old_zv
>= before
)
5457 old_zv
+= PT
- before
;
5458 old_zv_byte
+= PT_BYTE
- before_byte
;
5461 /* If the restriction isn't what it should be, set it. */
5462 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5463 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5466 BVAR (current_buffer
, read_only
) = old_read_only
;
5467 SET_PT_BOTH (opoint
, opoint_byte
);
5469 /* Handling the process output should not deactivate the mark. */
5470 Vdeactivate_mark
= odeactivate
;
5472 unbind_to (count
, Qnil
);
5476 /* Sending data to subprocess */
5478 jmp_buf send_process_frame
;
5479 Lisp_Object process_sent_to
;
5482 send_process_trap (int ignore
)
5484 SIGNAL_THREAD_CHECK (SIGPIPE
);
5485 sigunblock (sigmask (SIGPIPE
));
5486 longjmp (send_process_frame
, 1);
5489 /* Send some data to process PROC.
5490 BUF is the beginning of the data; LEN is the number of characters.
5491 OBJECT is the Lisp object that the data comes from. If OBJECT is
5492 nil or t, it means that the data comes from C string.
5494 If OBJECT is not nil, the data is encoded by PROC's coding-system
5495 for encoding before it is sent.
5497 This function can evaluate Lisp code and can garbage collect. */
5500 send_process (volatile Lisp_Object proc
, const char *volatile buf
,
5501 volatile EMACS_INT len
, volatile Lisp_Object object
)
5503 /* Use volatile to protect variables from being clobbered by longjmp. */
5504 struct Lisp_Process
*p
= XPROCESS (proc
);
5506 struct coding_system
*coding
;
5507 struct gcpro gcpro1
;
5508 SIGTYPE (*volatile old_sigpipe
) (int);
5512 if (p
->raw_status_new
)
5514 if (! EQ (p
->status
, Qrun
))
5515 error ("Process %s not running", SDATA (p
->name
));
5517 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5519 coding
= proc_encode_coding_system
[p
->outfd
];
5520 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5522 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5523 || (BUFFERP (object
)
5524 && !NILP (BVAR (XBUFFER (object
), enable_multibyte_characters
)))
5527 p
->encode_coding_system
5528 = complement_process_encoding_system (p
->encode_coding_system
);
5529 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5531 /* The coding system for encoding was changed to raw-text
5532 because we sent a unibyte text previously. Now we are
5533 sending a multibyte text, thus we must encode it by the
5534 original coding system specified for the current process.
5536 Another reason we comming here is that the coding system
5537 was just complemented and new one was returned by
5538 complement_process_encoding_system. */
5539 setup_coding_system (p
->encode_coding_system
, coding
);
5540 Vlast_coding_system_used
= p
->encode_coding_system
;
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 ((unsigned char *) 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
= SSDATA (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
, 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
,
5654 rv
= emacs_write (outfd
, 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
),
5712 (unsigned char *) buf
);
5713 else if (STRINGP (object
))
5714 offset
= buf
- SSDATA (object
);
5716 #ifdef EMACS_HAS_USECS
5717 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5719 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5722 if (BUFFERP (object
))
5723 buf
= (char *) BUF_BYTE_ADDRESS (XBUFFER (object
),
5725 else if (STRINGP (object
))
5726 buf
= offset
+ SSDATA (object
);
5731 /* This is a real error. */
5732 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5742 signal (SIGPIPE
, old_sigpipe
);
5743 proc
= process_sent_to
;
5744 p
= XPROCESS (proc
);
5745 p
->raw_status_new
= 0;
5746 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5747 p
->tick
= ++process_tick
;
5748 deactivate_process (proc
);
5749 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5755 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5757 doc
: /* Send current contents of region as input to PROCESS.
5758 PROCESS may be a process, a buffer, the name of a process or buffer, or
5759 nil, indicating the current buffer's process.
5760 Called from program, takes three arguments, PROCESS, START and END.
5761 If the region is more than 500 characters long,
5762 it is sent in several bunches. This may happen even for shorter regions.
5763 Output from processes can arrive in between bunches. */)
5764 (Lisp_Object process
, Lisp_Object start
, Lisp_Object end
)
5767 EMACS_INT start1
, end1
;
5769 proc
= get_process (process
);
5770 validate_region (&start
, &end
);
5772 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5773 move_gap (XINT (start
));
5775 start1
= CHAR_TO_BYTE (XINT (start
));
5776 end1
= CHAR_TO_BYTE (XINT (end
));
5777 send_process (proc
, (char *) BYTE_POS_ADDR (start1
), end1
- start1
,
5778 Fcurrent_buffer ());
5783 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5785 doc
: /* Send PROCESS the contents of STRING as input.
5786 PROCESS may be a process, a buffer, the name of a process or buffer, or
5787 nil, indicating the current buffer's process.
5788 If STRING is more than 500 characters long,
5789 it is sent in several bunches. This may happen even for shorter strings.
5790 Output from processes can arrive in between bunches. */)
5791 (Lisp_Object process
, Lisp_Object string
)
5794 CHECK_STRING (string
);
5795 proc
= get_process (process
);
5796 send_process (proc
, SSDATA (string
),
5797 SBYTES (string
), string
);
5801 /* Return the foreground process group for the tty/pty that
5802 the process P uses. */
5804 emacs_get_tty_pgrp (struct Lisp_Process
*p
)
5809 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5812 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5813 master side. Try the slave side. */
5814 fd
= emacs_open (SSDATA (p
->tty_name
), O_RDONLY
, 0);
5818 ioctl (fd
, TIOCGPGRP
, &gid
);
5822 #endif /* defined (TIOCGPGRP ) */
5827 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5828 Sprocess_running_child_p
, 0, 1, 0,
5829 doc
: /* Return t if PROCESS has given the terminal to a child.
5830 If the operating system does not make it possible to find out,
5831 return t unconditionally. */)
5832 (Lisp_Object process
)
5834 /* Initialize in case ioctl doesn't exist or gives an error,
5835 in a way that will cause returning t. */
5838 struct Lisp_Process
*p
;
5840 proc
= get_process (process
);
5841 p
= XPROCESS (proc
);
5843 if (!EQ (p
->type
, Qreal
))
5844 error ("Process %s is not a subprocess",
5847 error ("Process %s is not active",
5850 gid
= emacs_get_tty_pgrp (p
);
5857 /* send a signal number SIGNO to PROCESS.
5858 If CURRENT_GROUP is t, that means send to the process group
5859 that currently owns the terminal being used to communicate with PROCESS.
5860 This is used for various commands in shell mode.
5861 If CURRENT_GROUP is lambda, that means send to the process group
5862 that currently owns the terminal, but only if it is NOT the shell itself.
5864 If NOMSG is zero, insert signal-announcements into process's buffers
5867 If we can, we try to signal PROCESS by sending control characters
5868 down the pty. This allows us to signal inferiors who have changed
5869 their uid, for which killpg would return an EPERM error. */
5872 process_send_signal (Lisp_Object process
, int signo
, Lisp_Object current_group
,
5876 register struct Lisp_Process
*p
;
5880 proc
= get_process (process
);
5881 p
= XPROCESS (proc
);
5883 if (!EQ (p
->type
, Qreal
))
5884 error ("Process %s is not a subprocess",
5887 error ("Process %s is not active",
5891 current_group
= Qnil
;
5893 /* If we are using pgrps, get a pgrp number and make it negative. */
5894 if (NILP (current_group
))
5895 /* Send the signal to the shell's process group. */
5899 #ifdef SIGNALS_VIA_CHARACTERS
5900 /* If possible, send signals to the entire pgrp
5901 by sending an input character to it. */
5904 cc_t
*sig_char
= NULL
;
5906 tcgetattr (p
->infd
, &t
);
5911 sig_char
= &t
.c_cc
[VINTR
];
5915 sig_char
= &t
.c_cc
[VQUIT
];
5919 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5920 sig_char
= &t
.c_cc
[VSWTCH
];
5922 sig_char
= &t
.c_cc
[VSUSP
];
5927 if (sig_char
&& *sig_char
!= CDISABLE
)
5929 send_process (proc
, sig_char
, 1, Qnil
);
5932 /* If we can't send the signal with a character,
5933 fall through and send it another way. */
5935 /* The code above may fall through if it can't
5936 handle the signal. */
5937 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5940 /* Get the current pgrp using the tty itself, if we have that.
5941 Otherwise, use the pty to get the pgrp.
5942 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5943 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5944 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5945 His patch indicates that if TIOCGPGRP returns an error, then
5946 we should just assume that p->pid is also the process group id. */
5948 gid
= emacs_get_tty_pgrp (p
);
5951 /* If we can't get the information, assume
5952 the shell owns the tty. */
5955 /* It is not clear whether anything really can set GID to -1.
5956 Perhaps on some system one of those ioctls can or could do so.
5957 Or perhaps this is vestigial. */
5960 #else /* ! defined (TIOCGPGRP ) */
5961 /* Can't select pgrps on this system, so we know that
5962 the child itself heads the pgrp. */
5964 #endif /* ! defined (TIOCGPGRP ) */
5966 /* If current_group is lambda, and the shell owns the terminal,
5967 don't send any signal. */
5968 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5976 p
->raw_status_new
= 0;
5978 p
->tick
= ++process_tick
;
5981 status_notify (NULL
);
5982 redisplay_preserve_echo_area (13);
5985 #endif /* ! defined (SIGCONT) */
5989 flush_pending_output (p
->infd
);
5993 /* If we don't have process groups, send the signal to the immediate
5994 subprocess. That isn't really right, but it's better than any
5995 obvious alternative. */
5998 kill (p
->pid
, signo
);
6002 /* gid may be a pid, or minus a pgrp's number */
6004 if (!NILP (current_group
))
6006 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6007 EMACS_KILLPG (gid
, signo
);
6014 #else /* ! defined (TIOCSIGSEND) */
6015 EMACS_KILLPG (gid
, signo
);
6016 #endif /* ! defined (TIOCSIGSEND) */
6019 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6020 doc
: /* Interrupt process PROCESS.
6021 PROCESS may be a process, a buffer, or the name of a process or buffer.
6022 No arg or nil means current buffer's process.
6023 Second arg CURRENT-GROUP non-nil means send signal to
6024 the current process-group of the process's controlling terminal
6025 rather than to the process's own process group.
6026 If the process is a shell, this means interrupt current subjob
6027 rather than the shell.
6029 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6030 don't send the signal. */)
6031 (Lisp_Object process
, Lisp_Object current_group
)
6033 process_send_signal (process
, SIGINT
, current_group
, 0);
6037 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6038 doc
: /* Kill process PROCESS. May be process or name of one.
6039 See function `interrupt-process' for more details on usage. */)
6040 (Lisp_Object process
, Lisp_Object current_group
)
6042 process_send_signal (process
, SIGKILL
, current_group
, 0);
6046 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6047 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6048 See function `interrupt-process' for more details on usage. */)
6049 (Lisp_Object process
, Lisp_Object current_group
)
6051 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6055 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6056 doc
: /* Stop process PROCESS. May be process or name of one.
6057 See function `interrupt-process' for more details on usage.
6058 If PROCESS is a network or serial process, inhibit handling of incoming
6060 (Lisp_Object process
, Lisp_Object current_group
)
6062 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6064 struct Lisp_Process
*p
;
6066 p
= XPROCESS (process
);
6067 if (NILP (p
->command
)
6070 FD_CLR (p
->infd
, &input_wait_mask
);
6071 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6077 error ("No SIGTSTP support");
6079 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6084 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6085 doc
: /* Continue process PROCESS. May be process or name of one.
6086 See function `interrupt-process' for more details on usage.
6087 If PROCESS is a network or serial process, resume handling of incoming
6089 (Lisp_Object process
, Lisp_Object current_group
)
6091 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6093 struct Lisp_Process
*p
;
6095 p
= XPROCESS (process
);
6096 if (EQ (p
->command
, Qt
)
6098 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6100 FD_SET (p
->infd
, &input_wait_mask
);
6101 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6103 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6104 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6105 #else /* not WINDOWSNT */
6106 tcflush (p
->infd
, TCIFLUSH
);
6107 #endif /* not WINDOWSNT */
6113 process_send_signal (process
, SIGCONT
, current_group
, 0);
6115 error ("No SIGCONT support");
6120 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6121 2, 2, "sProcess (name or number): \nnSignal code: ",
6122 doc
: /* Send PROCESS the signal with code SIGCODE.
6123 PROCESS may also be a number specifying the process id of the
6124 process to signal; in this case, the process need not be a child of
6126 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6127 (Lisp_Object process
, Lisp_Object sigcode
)
6131 if (INTEGERP (process
))
6133 pid
= XINT (process
);
6137 if (FLOATP (process
))
6139 pid
= (pid_t
) XFLOAT_DATA (process
);
6143 if (STRINGP (process
))
6146 if (tem
= Fget_process (process
), NILP (tem
))
6148 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6155 process
= get_process (process
);
6160 CHECK_PROCESS (process
);
6161 pid
= XPROCESS (process
)->pid
;
6163 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6167 #define parse_signal(NAME, VALUE) \
6168 else if (!xstrcasecmp (name, NAME)) \
6169 XSETINT (sigcode, VALUE)
6171 if (INTEGERP (sigcode
))
6177 CHECK_SYMBOL (sigcode
);
6178 name
= SSDATA (SYMBOL_NAME (sigcode
));
6180 if (!strncmp (name
, "SIG", 3) || !strncmp (name
, "sig", 3))
6186 parse_signal ("usr1", SIGUSR1
);
6189 parse_signal ("usr2", SIGUSR2
);
6192 parse_signal ("term", SIGTERM
);
6195 parse_signal ("hup", SIGHUP
);
6198 parse_signal ("int", SIGINT
);
6201 parse_signal ("quit", SIGQUIT
);
6204 parse_signal ("ill", SIGILL
);
6207 parse_signal ("abrt", SIGABRT
);
6210 parse_signal ("emt", SIGEMT
);
6213 parse_signal ("kill", SIGKILL
);
6216 parse_signal ("fpe", SIGFPE
);
6219 parse_signal ("bus", SIGBUS
);
6222 parse_signal ("segv", SIGSEGV
);
6225 parse_signal ("sys", SIGSYS
);
6228 parse_signal ("pipe", SIGPIPE
);
6231 parse_signal ("alrm", SIGALRM
);
6234 parse_signal ("urg", SIGURG
);
6237 parse_signal ("stop", SIGSTOP
);
6240 parse_signal ("tstp", SIGTSTP
);
6243 parse_signal ("cont", SIGCONT
);
6246 parse_signal ("chld", SIGCHLD
);
6249 parse_signal ("ttin", SIGTTIN
);
6252 parse_signal ("ttou", SIGTTOU
);
6255 parse_signal ("io", SIGIO
);
6258 parse_signal ("xcpu", SIGXCPU
);
6261 parse_signal ("xfsz", SIGXFSZ
);
6264 parse_signal ("vtalrm", SIGVTALRM
);
6267 parse_signal ("prof", SIGPROF
);
6270 parse_signal ("winch", SIGWINCH
);
6273 parse_signal ("info", SIGINFO
);
6276 error ("Undefined signal name %s", name
);
6281 return make_number (kill (pid
, XINT (sigcode
)));
6284 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6285 doc
: /* Make PROCESS see end-of-file in its input.
6286 EOF comes after any text already sent to it.
6287 PROCESS may be a process, a buffer, the name of a process or buffer, or
6288 nil, indicating the current buffer's process.
6289 If PROCESS is a network connection, or is a process communicating
6290 through a pipe (as opposed to a pty), then you cannot send any more
6291 text to PROCESS after you call this function.
6292 If PROCESS is a serial process, wait until all output written to the
6293 process has been transmitted to the serial port. */)
6294 (Lisp_Object process
)
6297 struct coding_system
*coding
;
6299 if (DATAGRAM_CONN_P (process
))
6302 proc
= get_process (process
);
6303 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6305 /* Make sure the process is really alive. */
6306 if (XPROCESS (proc
)->raw_status_new
)
6307 update_status (XPROCESS (proc
));
6308 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6309 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6311 if (CODING_REQUIRE_FLUSHING (coding
))
6313 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6314 send_process (proc
, "", 0, Qnil
);
6317 if (XPROCESS (proc
)->pty_flag
)
6318 send_process (proc
, "\004", 1, Qnil
);
6319 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6322 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6323 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6324 #endif /* not WINDOWSNT */
6325 /* Do nothing on Windows because writes are blocking. */
6329 int old_outfd
, new_outfd
;
6331 #ifdef HAVE_SHUTDOWN
6332 /* If this is a network connection, or socketpair is used
6333 for communication with the subprocess, call shutdown to cause EOF.
6334 (In some old system, shutdown to socketpair doesn't work.
6335 Then we just can't win.) */
6336 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6337 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6338 shutdown (XPROCESS (proc
)->outfd
, 1);
6339 /* In case of socketpair, outfd == infd, so don't close it. */
6340 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6341 emacs_close (XPROCESS (proc
)->outfd
);
6342 #else /* not HAVE_SHUTDOWN */
6343 emacs_close (XPROCESS (proc
)->outfd
);
6344 #endif /* not HAVE_SHUTDOWN */
6345 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6348 old_outfd
= XPROCESS (proc
)->outfd
;
6350 if (!proc_encode_coding_system
[new_outfd
])
6351 proc_encode_coding_system
[new_outfd
]
6352 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6353 memcpy (proc_encode_coding_system
[new_outfd
],
6354 proc_encode_coding_system
[old_outfd
],
6355 sizeof (struct coding_system
));
6356 memset (proc_encode_coding_system
[old_outfd
], 0,
6357 sizeof (struct coding_system
));
6359 XPROCESS (proc
)->outfd
= new_outfd
;
6364 /* On receipt of a signal that a child status has changed, loop asking
6365 about children with changed statuses until the system says there
6368 All we do is change the status; we do not run sentinels or print
6369 notifications. That is saved for the next time keyboard input is
6370 done, in order to avoid timing errors.
6372 ** WARNING: this can be called during garbage collection.
6373 Therefore, it must not be fooled by the presence of mark bits in
6376 ** USG WARNING: Although it is not obvious from the documentation
6377 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6378 signal() before executing at least one wait(), otherwise the
6379 handler will be called again, resulting in an infinite loop. The
6380 relevant portion of the documentation reads "SIGCLD signals will be
6381 queued and the signal-catching function will be continually
6382 reentered until the queue is empty". Invoking signal() causes the
6383 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6386 ** Malloc WARNING: This should never call malloc either directly or
6387 indirectly; if it does, that is a bug */
6391 sigchld_handler (int signo
)
6393 int old_errno
= errno
;
6395 struct Lisp_Process
*p
;
6397 SIGNAL_THREAD_CHECK (signo
);
6408 #endif /* no WUNTRACED */
6409 /* Keep trying to get a status until we get a definitive result. */
6413 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6415 while (pid
< 0 && errno
== EINTR
);
6419 /* PID == 0 means no processes found, PID == -1 means a real
6420 failure. We have done all our job, so return. */
6427 #endif /* no WNOHANG */
6429 /* Find the process that signaled us, and record its status. */
6431 /* The process can have been deleted by Fdelete_process. */
6432 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6434 Lisp_Object xpid
= XCAR (tail
);
6435 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6436 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6438 XSETCAR (tail
, Qnil
);
6439 goto sigchld_end_of_loop
;
6443 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6445 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6447 proc
= XCDR (XCAR (tail
));
6448 p
= XPROCESS (proc
);
6449 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6454 /* Look for an asynchronous process whose pid hasn't been filled
6457 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6459 proc
= XCDR (XCAR (tail
));
6460 p
= XPROCESS (proc
);
6466 /* Change the status of the process that was found. */
6469 int clear_desc_flag
= 0;
6471 p
->tick
= ++process_tick
;
6473 p
->raw_status_new
= 1;
6475 /* If process has terminated, stop waiting for its output. */
6476 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6478 clear_desc_flag
= 1;
6480 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6481 if (clear_desc_flag
)
6483 FD_CLR (p
->infd
, &input_wait_mask
);
6484 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6487 /* Tell wait_reading_process_output that it needs to wake up and
6489 if (input_available_clear_time
)
6490 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6493 /* There was no asynchronous process found for that pid: we have
6494 a synchronous process. */
6497 synch_process_alive
= 0;
6499 /* Report the status of the synchronous process. */
6501 synch_process_retcode
= WRETCODE (w
);
6502 else if (WIFSIGNALED (w
))
6503 synch_process_termsig
= WTERMSIG (w
);
6505 /* Tell wait_reading_process_output that it needs to wake up and
6507 if (input_available_clear_time
)
6508 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6511 sigchld_end_of_loop
:
6514 /* On some systems, we must return right away.
6515 If any more processes want to signal us, we will
6517 Otherwise (on systems that have WNOHANG), loop around
6518 to use up all the processes that have something to tell us. */
6519 #if (defined WINDOWSNT \
6520 || (defined USG && !defined GNU_LINUX \
6521 && !(defined HPUX && defined WNOHANG)))
6524 #endif /* USG, but not HPUX with WNOHANG */
6527 #endif /* SIGCHLD */
6531 exec_sentinel_unwind (Lisp_Object data
)
6533 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6538 exec_sentinel_error_handler (Lisp_Object error
)
6540 cmd_error_internal (error
, "error in process sentinel: ");
6542 update_echo_area ();
6543 Fsleep_for (make_number (2), Qnil
);
6548 exec_sentinel (Lisp_Object proc
, Lisp_Object reason
)
6550 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6551 register struct Lisp_Process
*p
= XPROCESS (proc
);
6552 int count
= SPECPDL_INDEX ();
6553 int outer_running_asynch_code
= running_asynch_code
;
6554 int waiting
= waiting_for_user_input_p
;
6556 if (inhibit_sentinels
)
6559 /* No need to gcpro these, because all we do with them later
6560 is test them for EQness, and none of them should be a string. */
6561 odeactivate
= Vdeactivate_mark
;
6562 XSETBUFFER (obuffer
, current_buffer
);
6563 okeymap
= BVAR (current_buffer
, keymap
);
6565 /* There's no good reason to let sentinels change the current
6566 buffer, and many callers of accept-process-output, sit-for, and
6567 friends don't expect current-buffer to be changed from under them. */
6568 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
6570 sentinel
= p
->sentinel
;
6571 if (NILP (sentinel
))
6574 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6575 assure that it gets restored no matter how the sentinel exits. */
6577 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6578 /* Inhibit quit so that random quits don't screw up a running filter. */
6579 specbind (Qinhibit_quit
, Qt
);
6580 specbind (Qlast_nonmenu_event
, Qt
); /* Why? --Stef */
6582 /* In case we get recursively called,
6583 and we already saved the match data nonrecursively,
6584 save the same match data in safely recursive fashion. */
6585 if (outer_running_asynch_code
)
6588 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6589 restore_search_regs ();
6590 record_unwind_save_match_data ();
6591 Fset_match_data (tem
, Qt
);
6594 /* For speed, if a search happens within this code,
6595 save the match data in a special nonrecursive fashion. */
6596 running_asynch_code
= 1;
6598 internal_condition_case_1 (read_process_output_call
,
6600 Fcons (proc
, Fcons (reason
, Qnil
))),
6601 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6602 exec_sentinel_error_handler
);
6604 /* If we saved the match data nonrecursively, restore it now. */
6605 restore_search_regs ();
6606 running_asynch_code
= outer_running_asynch_code
;
6608 Vdeactivate_mark
= odeactivate
;
6610 /* Restore waiting_for_user_input_p as it was
6611 when we were called, in case the filter clobbered it. */
6612 waiting_for_user_input_p
= waiting
;
6615 if (! EQ (Fcurrent_buffer (), obuffer
)
6616 || ! EQ (current_buffer
->keymap
, okeymap
))
6618 /* But do it only if the caller is actually going to read events.
6619 Otherwise there's no need to make him wake up, and it could
6620 cause trouble (for example it would make sit_for return). */
6621 if (waiting_for_user_input_p
== -1)
6622 record_asynch_buffer_change ();
6624 unbind_to (count
, Qnil
);
6627 /* Report all recent events of a change in process status
6628 (either run the sentinel or output a message).
6629 This is usually done while Emacs is waiting for keyboard input
6630 but can be done at other times. */
6633 status_notify (struct Lisp_Process
*deleting_process
)
6635 register Lisp_Object proc
, buffer
;
6636 Lisp_Object tail
, msg
;
6637 struct gcpro gcpro1
, gcpro2
;
6641 /* We need to gcpro tail; if read_process_output calls a filter
6642 which deletes a process and removes the cons to which tail points
6643 from Vprocess_alist, and then causes a GC, tail is an unprotected
6647 /* Set this now, so that if new processes are created by sentinels
6648 that we run, we get called again to handle their status changes. */
6649 update_tick
= process_tick
;
6651 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6654 register struct Lisp_Process
*p
;
6656 proc
= Fcdr (XCAR (tail
));
6657 p
= XPROCESS (proc
);
6659 if (p
->tick
!= p
->update_tick
)
6661 p
->update_tick
= p
->tick
;
6663 /* If process is still active, read any output that remains. */
6664 while (! EQ (p
->filter
, Qt
)
6665 && ! EQ (p
->status
, Qconnect
)
6666 && ! EQ (p
->status
, Qlisten
)
6667 /* Network or serial process not stopped: */
6668 && ! EQ (p
->command
, Qt
)
6670 && p
!= deleting_process
6671 && read_process_output (proc
, p
->infd
) > 0);
6675 /* Get the text to use for the message. */
6676 if (p
->raw_status_new
)
6678 msg
= status_message (p
);
6680 /* If process is terminated, deactivate it or delete it. */
6682 if (CONSP (p
->status
))
6683 symbol
= XCAR (p
->status
);
6685 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6686 || EQ (symbol
, Qclosed
))
6688 if (delete_exited_processes
)
6689 remove_process (proc
);
6691 deactivate_process (proc
);
6694 /* The actions above may have further incremented p->tick.
6695 So set p->update_tick again
6696 so that an error in the sentinel will not cause
6697 this code to be run again. */
6698 p
->update_tick
= p
->tick
;
6699 /* Now output the message suitably. */
6700 if (!NILP (p
->sentinel
))
6701 exec_sentinel (proc
, msg
);
6702 /* Don't bother with a message in the buffer
6703 when a process becomes runnable. */
6704 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6707 struct buffer
*old
= current_buffer
;
6708 EMACS_INT opoint
, opoint_byte
;
6709 EMACS_INT before
, before_byte
;
6711 /* Avoid error if buffer is deleted
6712 (probably that's why the process is dead, too) */
6713 if (NILP (BVAR (XBUFFER (buffer
), name
)))
6715 Fset_buffer (buffer
);
6718 opoint_byte
= PT_BYTE
;
6719 /* Insert new output into buffer
6720 at the current end-of-output marker,
6721 thus preserving logical ordering of input and output. */
6722 if (XMARKER (p
->mark
)->buffer
)
6723 Fgoto_char (p
->mark
);
6725 SET_PT_BOTH (ZV
, ZV_BYTE
);
6728 before_byte
= PT_BYTE
;
6730 tem
= BVAR (current_buffer
, read_only
);
6731 BVAR (current_buffer
, read_only
) = Qnil
;
6732 insert_string ("\nProcess ");
6733 Finsert (1, &p
->name
);
6734 insert_string (" ");
6736 BVAR (current_buffer
, read_only
) = tem
;
6737 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6739 if (opoint
>= before
)
6740 SET_PT_BOTH (opoint
+ (PT
- before
),
6741 opoint_byte
+ (PT_BYTE
- before_byte
));
6743 SET_PT_BOTH (opoint
, opoint_byte
);
6745 set_buffer_internal (old
);
6750 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6755 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6756 Sset_process_coding_system
, 1, 3, 0,
6757 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6758 DECODING will be used to decode subprocess output and ENCODING to
6759 encode subprocess input. */)
6760 (register Lisp_Object process
, Lisp_Object decoding
, Lisp_Object encoding
)
6762 register struct Lisp_Process
*p
;
6764 CHECK_PROCESS (process
);
6765 p
= XPROCESS (process
);
6767 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6769 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6770 Fcheck_coding_system (decoding
);
6771 Fcheck_coding_system (encoding
);
6772 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6773 p
->decode_coding_system
= decoding
;
6774 p
->encode_coding_system
= encoding
;
6775 setup_process_coding_systems (process
);
6780 DEFUN ("process-coding-system",
6781 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6782 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6783 (register Lisp_Object process
)
6785 CHECK_PROCESS (process
);
6786 return Fcons (XPROCESS (process
)->decode_coding_system
,
6787 XPROCESS (process
)->encode_coding_system
);
6790 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6791 Sset_process_filter_multibyte
, 2, 2, 0,
6792 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6793 If FLAG is non-nil, the filter is given multibyte strings.
6794 If FLAG is nil, the filter is given unibyte strings. In this case,
6795 all character code conversion except for end-of-line conversion is
6797 (Lisp_Object process
, Lisp_Object flag
)
6799 register struct Lisp_Process
*p
;
6801 CHECK_PROCESS (process
);
6802 p
= XPROCESS (process
);
6804 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
6805 setup_process_coding_systems (process
);
6810 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6811 Sprocess_filter_multibyte_p
, 1, 1, 0,
6812 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6813 (Lisp_Object process
)
6815 register struct Lisp_Process
*p
;
6816 struct coding_system
*coding
;
6818 CHECK_PROCESS (process
);
6819 p
= XPROCESS (process
);
6820 coding
= proc_decode_coding_system
[p
->infd
];
6821 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6828 add_gpm_wait_descriptor (int desc
)
6830 add_keyboard_wait_descriptor (desc
);
6834 delete_gpm_wait_descriptor (int desc
)
6836 delete_keyboard_wait_descriptor (desc
);
6839 /* Return nonzero if *MASK has a bit set
6840 that corresponds to one of the keyboard input descriptors. */
6843 keyboard_bit_set (fd_set
*mask
)
6847 for (fd
= 0; fd
<= max_input_desc
; fd
++)
6848 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6849 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6855 #else /* not subprocesses */
6857 /* Defined on msdos.c. */
6858 extern int sys_select (int, SELECT_TYPE
*, SELECT_TYPE
*, SELECT_TYPE
*,
6861 /* Implementation of wait_reading_process_output, assuming that there
6862 are no subprocesses. Used only by the MS-DOS build.
6864 Wait for timeout to elapse and/or keyboard input to be available.
6867 timeout in seconds, or
6868 zero for no limit, or
6869 -1 means gobble data immediately available but don't wait for any.
6871 read_kbd is a Lisp_Object:
6872 0 to ignore keyboard input, or
6873 1 to return when input is available, or
6874 -1 means caller will actually read the input, so don't throw to
6877 see full version for other parameters. We know that wait_proc will
6878 always be NULL, since `subprocesses' isn't defined.
6880 do_display != 0 means redisplay should be done to show subprocess
6881 output that arrives.
6883 Return true if we received input from any process. */
6886 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
6888 Lisp_Object wait_for_cell
,
6889 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
6892 EMACS_TIME end_time
, timeout
;
6893 SELECT_TYPE waitchannels
;
6896 /* What does time_limit really mean? */
6897 if (time_limit
|| microsecs
)
6899 EMACS_GET_TIME (end_time
);
6900 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
6901 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
6904 /* Turn off periodic alarms (in case they are in use)
6905 and then turn off any other atimers,
6906 because the select emulator uses alarms. */
6908 turn_on_atimers (0);
6912 int timeout_reduced_for_timers
= 0;
6914 /* If calling from keyboard input, do not quit
6915 since we want to return C-g as an input character.
6916 Otherwise, do pending quit if requested. */
6920 /* Exit now if the cell we're waiting for became non-nil. */
6921 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6924 /* Compute time from now till when time limit is up */
6925 /* Exit if already run out */
6926 if (time_limit
== -1)
6928 /* -1 specified for timeout means
6929 gobble output available now
6930 but don't wait at all. */
6932 EMACS_SET_SECS_USECS (timeout
, 0, 0);
6934 else if (time_limit
|| microsecs
)
6936 EMACS_GET_TIME (timeout
);
6937 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
6938 if (EMACS_TIME_NEG_P (timeout
))
6943 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
6946 /* If our caller will not immediately handle keyboard events,
6947 run timer events directly.
6948 (Callers that will immediately read keyboard events
6949 call timer_delay on their own.) */
6950 if (NILP (wait_for_cell
))
6952 EMACS_TIME timer_delay
;
6956 int old_timers_run
= timers_run
;
6957 timer_delay
= timer_check (1);
6958 if (timers_run
!= old_timers_run
&& do_display
)
6959 /* We must retry, since a timer may have requeued itself
6960 and that could alter the time delay. */
6961 redisplay_preserve_echo_area (14);
6965 while (!detect_input_pending ());
6967 /* If there is unread keyboard input, also return. */
6969 && requeued_events_pending_p ())
6972 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
6974 EMACS_TIME difference
;
6975 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
6976 if (EMACS_TIME_NEG_P (difference
))
6978 timeout
= timer_delay
;
6979 timeout_reduced_for_timers
= 1;
6984 /* Cause C-g and alarm signals to take immediate action,
6985 and cause input available signals to zero out timeout. */
6987 set_waiting_for_input (&timeout
);
6989 /* Wait till there is something to do. */
6991 if (! read_kbd
&& NILP (wait_for_cell
))
6992 FD_ZERO (&waitchannels
);
6994 FD_SET (0, &waitchannels
);
6996 /* If a frame has been newly mapped and needs updating,
6997 reprocess its display stuff. */
6998 if (frame_garbaged
&& do_display
)
7000 clear_waiting_for_input ();
7001 redisplay_preserve_echo_area (15);
7003 set_waiting_for_input (&timeout
);
7006 if (read_kbd
&& detect_input_pending ())
7009 FD_ZERO (&waitchannels
);
7012 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7017 /* Make C-g and alarm signals set flags again */
7018 clear_waiting_for_input ();
7020 /* If we woke up due to SIGWINCH, actually change size now. */
7021 do_pending_window_change (0);
7023 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7024 /* We waited the full specified time, so return now. */
7029 /* If the system call was interrupted, then go around the
7031 if (xerrno
== EINTR
)
7032 FD_ZERO (&waitchannels
);
7034 error ("select error: %s", emacs_strerror (xerrno
));
7037 /* Check for keyboard input */
7040 && detect_input_pending_run_timers (do_display
))
7042 swallow_events (do_display
);
7043 if (detect_input_pending_run_timers (do_display
))
7047 /* If there is unread keyboard input, also return. */
7049 && requeued_events_pending_p ())
7052 /* If wait_for_cell. check for keyboard input
7053 but don't run any timers.
7054 ??? (It seems wrong to me to check for keyboard
7055 input at all when wait_for_cell, but the code
7056 has been this way since July 1994.
7057 Try changing this after version 19.31.) */
7058 if (! NILP (wait_for_cell
)
7059 && detect_input_pending ())
7061 swallow_events (do_display
);
7062 if (detect_input_pending ())
7066 /* Exit now if the cell we're waiting for became non-nil. */
7067 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7076 #endif /* not subprocesses */
7078 /* The following functions are needed even if async subprocesses are
7079 not supported. Some of them are no-op stubs in that case. */
7081 /* Add DESC to the set of keyboard input descriptors. */
7084 add_keyboard_wait_descriptor (int desc
)
7086 #ifdef subprocesses /* actually means "not MSDOS" */
7087 FD_SET (desc
, &input_wait_mask
);
7088 FD_SET (desc
, &non_process_wait_mask
);
7089 if (desc
> max_input_desc
)
7090 max_input_desc
= desc
;
7094 /* From now on, do not expect DESC to give keyboard input. */
7097 delete_keyboard_wait_descriptor (int desc
)
7101 int lim
= max_input_desc
;
7103 FD_CLR (desc
, &input_wait_mask
);
7104 FD_CLR (desc
, &non_process_wait_mask
);
7106 if (desc
== max_input_desc
)
7107 for (fd
= 0; fd
< lim
; fd
++)
7108 if (FD_ISSET (fd
, &input_wait_mask
) || FD_ISSET (fd
, &write_mask
))
7109 max_input_desc
= fd
;
7113 /* Setup coding systems of PROCESS. */
7116 setup_process_coding_systems (Lisp_Object process
)
7119 struct Lisp_Process
*p
= XPROCESS (process
);
7121 int outch
= p
->outfd
;
7122 Lisp_Object coding_system
;
7124 if (inch
< 0 || outch
< 0)
7127 if (!proc_decode_coding_system
[inch
])
7128 proc_decode_coding_system
[inch
]
7129 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7130 coding_system
= p
->decode_coding_system
;
7131 if (! NILP (p
->filter
))
7133 else if (BUFFERP (p
->buffer
))
7135 if (NILP (BVAR (XBUFFER (p
->buffer
), enable_multibyte_characters
)))
7136 coding_system
= raw_text_coding_system (coding_system
);
7138 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
7140 if (!proc_encode_coding_system
[outch
])
7141 proc_encode_coding_system
[outch
]
7142 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7143 setup_coding_system (p
->encode_coding_system
,
7144 proc_encode_coding_system
[outch
]);
7148 /* Close all descriptors currently in use for communication
7149 with subprocess. This is used in a newly-forked subprocess
7150 to get rid of irrelevant descriptors. */
7153 close_process_descs (void)
7157 for (i
= 0; i
< MAXDESC
; i
++)
7159 Lisp_Object process
;
7160 process
= chan_process
[i
];
7161 if (!NILP (process
))
7163 int in
= XPROCESS (process
)->infd
;
7164 int out
= XPROCESS (process
)->outfd
;
7167 if (out
>= 0 && in
!= out
)
7174 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7175 doc
: /* Return the (or a) process associated with BUFFER.
7176 BUFFER may be a buffer or the name of one. */)
7177 (register Lisp_Object buffer
)
7180 register Lisp_Object buf
, tail
, proc
;
7182 if (NILP (buffer
)) return Qnil
;
7183 buf
= Fget_buffer (buffer
);
7184 if (NILP (buf
)) return Qnil
;
7186 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7188 proc
= Fcdr (XCAR (tail
));
7189 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
7192 #endif /* subprocesses */
7196 DEFUN ("process-inherit-coding-system-flag",
7197 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7199 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
7200 If this flag is t, `buffer-file-coding-system' of the buffer
7201 associated with PROCESS will inherit the coding system used to decode
7202 the process output. */)
7203 (register Lisp_Object process
)
7206 CHECK_PROCESS (process
);
7207 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
7209 /* Ignore the argument and return the value of
7210 inherit-process-coding-system. */
7211 return inherit_process_coding_system
? Qt
: Qnil
;
7215 /* Kill all processes associated with `buffer'.
7216 If `buffer' is nil, kill all processes */
7219 kill_buffer_processes (Lisp_Object buffer
)
7222 Lisp_Object tail
, proc
;
7224 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7226 proc
= XCDR (XCAR (tail
));
7228 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
7230 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
7231 Fdelete_process (proc
);
7232 else if (XPROCESS (proc
)->infd
>= 0)
7233 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
7236 #else /* subprocesses */
7237 /* Since we have no subprocesses, this does nothing. */
7238 #endif /* subprocesses */
7241 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
7243 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
7244 This is intended for use by asynchronous process output filters and sentinels. */)
7248 return (waiting_for_user_input_p
? Qt
: Qnil
);
7254 /* Stop reading input from keyboard sources. */
7257 hold_keyboard_input (void)
7262 /* Resume reading input from keyboard sources. */
7265 unhold_keyboard_input (void)
7270 /* Return non-zero if keyboard input is on hold, zero otherwise. */
7273 kbd_on_hold_p (void)
7275 return kbd_is_on_hold
;
7279 /* Enumeration of and access to system processes a-la ps(1). */
7281 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7283 doc
: /* Return a list of numerical process IDs of all running processes.
7284 If this functionality is unsupported, return nil.
7286 See `process-attributes' for getting attributes of a process given its ID. */)
7289 return list_system_processes ();
7292 DEFUN ("process-attributes", Fprocess_attributes
,
7293 Sprocess_attributes
, 1, 1, 0,
7294 doc
: /* Return attributes of the process given by its PID, a number.
7296 Value is an alist where each element is a cons cell of the form
7300 If this functionality is unsupported, the value is nil.
7302 See `list-system-processes' for getting a list of all process IDs.
7304 The KEYs of the attributes that this function may return are listed
7305 below, together with the type of the associated VALUE (in parentheses).
7306 Not all platforms support all of these attributes; unsupported
7307 attributes will not appear in the returned alist.
7308 Unless explicitly indicated otherwise, numbers can have either
7309 integer or floating point values.
7311 euid -- Effective user User ID of the process (number)
7312 user -- User name corresponding to euid (string)
7313 egid -- Effective user Group ID of the process (number)
7314 group -- Group name corresponding to egid (string)
7315 comm -- Command name (executable name only) (string)
7316 state -- Process state code, such as "S", "R", or "T" (string)
7317 ppid -- Parent process ID (number)
7318 pgrp -- Process group ID (number)
7319 sess -- Session ID, i.e. process ID of session leader (number)
7320 ttname -- Controlling tty name (string)
7321 tpgid -- ID of foreground process group on the process's tty (number)
7322 minflt -- number of minor page faults (number)
7323 majflt -- number of major page faults (number)
7324 cminflt -- cumulative number of minor page faults (number)
7325 cmajflt -- cumulative number of major page faults (number)
7326 utime -- user time used by the process, in the (HIGH LOW USEC) format
7327 stime -- system time used by the process, in the (HIGH LOW USEC) format
7328 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7329 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7330 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7331 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7332 pri -- priority of the process (number)
7333 nice -- nice value of the process (number)
7334 thcount -- process thread count (number)
7335 start -- time the process started, in the (HIGH LOW USEC) format
7336 vsize -- virtual memory size of the process in KB's (number)
7337 rss -- resident set size of the process in KB's (number)
7338 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7339 pcpu -- percents of CPU time used by the process (floating-point number)
7340 pmem -- percents of total physical memory used by process's resident set
7341 (floating-point number)
7342 args -- command line which invoked the process (string). */)
7345 return system_process_attributes (pid
);
7355 inhibit_sentinels
= 0;
7359 if (! noninteractive
|| initialized
)
7361 signal (SIGCHLD
, sigchld_handler
);
7364 FD_ZERO (&input_wait_mask
);
7365 FD_ZERO (&non_keyboard_wait_mask
);
7366 FD_ZERO (&non_process_wait_mask
);
7367 FD_ZERO (&write_mask
);
7368 max_process_desc
= 0;
7369 memset (fd_callback_info
, 0, sizeof (fd_callback_info
));
7371 #ifdef NON_BLOCKING_CONNECT
7372 FD_ZERO (&connect_wait_mask
);
7373 num_pending_connects
= 0;
7376 #ifdef ADAPTIVE_READ_BUFFERING
7377 process_output_delay_count
= 0;
7378 process_output_skip
= 0;
7381 /* Don't do this, it caused infinite select loops. The display
7382 method should call add_keyboard_wait_descriptor on stdin if it
7385 FD_SET (0, &input_wait_mask
);
7388 Vprocess_alist
= Qnil
;
7390 deleted_pid_list
= Qnil
;
7392 for (i
= 0; i
< MAXDESC
; i
++)
7394 chan_process
[i
] = Qnil
;
7395 proc_buffered_char
[i
] = -1;
7397 memset (proc_decode_coding_system
, 0, sizeof proc_decode_coding_system
);
7398 memset (proc_encode_coding_system
, 0, sizeof proc_encode_coding_system
);
7399 #ifdef DATAGRAM_SOCKETS
7400 memset (datagram_address
, 0, sizeof datagram_address
);
7404 Lisp_Object subfeatures
= Qnil
;
7405 const struct socket_options
*sopt
;
7407 #define ADD_SUBFEATURE(key, val) \
7408 subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)
7410 #ifdef NON_BLOCKING_CONNECT
7411 ADD_SUBFEATURE (QCnowait
, Qt
);
7413 #ifdef DATAGRAM_SOCKETS
7414 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7416 #ifdef HAVE_SEQPACKET
7417 ADD_SUBFEATURE (QCtype
, Qseqpacket
);
7419 #ifdef HAVE_LOCAL_SOCKETS
7420 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7422 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7424 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7426 #ifdef HAVE_GETSOCKNAME
7427 ADD_SUBFEATURE (QCservice
, Qt
);
7429 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7430 ADD_SUBFEATURE (QCserver
, Qt
);
7433 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7434 subfeatures
= pure_cons (intern_c_string (sopt
->name
), subfeatures
);
7436 Fprovide (intern_c_string ("make-network-process"), subfeatures
);
7439 #if defined (DARWIN_OS)
7440 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7441 processes. As such, we only change the default value. */
7444 const char *release
= get_operating_system_release ();
7445 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7446 && release
[1] == '.')) {
7447 Vprocess_connection_type
= Qnil
;
7451 #endif /* subprocesses */
7456 syms_of_process (void)
7460 Qprocessp
= intern_c_string ("processp");
7461 staticpro (&Qprocessp
);
7462 Qrun
= intern_c_string ("run");
7464 Qstop
= intern_c_string ("stop");
7466 Qsignal
= intern_c_string ("signal");
7467 staticpro (&Qsignal
);
7469 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7472 Qexit = intern_c_string ("exit");
7473 staticpro (&Qexit); */
7475 Qopen
= intern_c_string ("open");
7477 Qclosed
= intern_c_string ("closed");
7478 staticpro (&Qclosed
);
7479 Qconnect
= intern_c_string ("connect");
7480 staticpro (&Qconnect
);
7481 Qfailed
= intern_c_string ("failed");
7482 staticpro (&Qfailed
);
7483 Qlisten
= intern_c_string ("listen");
7484 staticpro (&Qlisten
);
7485 Qlocal
= intern_c_string ("local");
7486 staticpro (&Qlocal
);
7487 Qipv4
= intern_c_string ("ipv4");
7490 Qipv6
= intern_c_string ("ipv6");
7493 Qdatagram
= intern_c_string ("datagram");
7494 staticpro (&Qdatagram
);
7495 Qseqpacket
= intern_c_string ("seqpacket");
7496 staticpro (&Qseqpacket
);
7498 QCport
= intern_c_string (":port");
7499 staticpro (&QCport
);
7500 QCspeed
= intern_c_string (":speed");
7501 staticpro (&QCspeed
);
7502 QCprocess
= intern_c_string (":process");
7503 staticpro (&QCprocess
);
7505 QCbytesize
= intern_c_string (":bytesize");
7506 staticpro (&QCbytesize
);
7507 QCstopbits
= intern_c_string (":stopbits");
7508 staticpro (&QCstopbits
);
7509 QCparity
= intern_c_string (":parity");
7510 staticpro (&QCparity
);
7511 Qodd
= intern_c_string ("odd");
7513 Qeven
= intern_c_string ("even");
7515 QCflowcontrol
= intern_c_string (":flowcontrol");
7516 staticpro (&QCflowcontrol
);
7517 Qhw
= intern_c_string ("hw");
7519 Qsw
= intern_c_string ("sw");
7521 QCsummary
= intern_c_string (":summary");
7522 staticpro (&QCsummary
);
7524 Qreal
= intern_c_string ("real");
7526 Qnetwork
= intern_c_string ("network");
7527 staticpro (&Qnetwork
);
7528 Qserial
= intern_c_string ("serial");
7529 staticpro (&Qserial
);
7530 QCbuffer
= intern_c_string (":buffer");
7531 staticpro (&QCbuffer
);
7532 QChost
= intern_c_string (":host");
7533 staticpro (&QChost
);
7534 QCservice
= intern_c_string (":service");
7535 staticpro (&QCservice
);
7536 QClocal
= intern_c_string (":local");
7537 staticpro (&QClocal
);
7538 QCremote
= intern_c_string (":remote");
7539 staticpro (&QCremote
);
7540 QCcoding
= intern_c_string (":coding");
7541 staticpro (&QCcoding
);
7542 QCserver
= intern_c_string (":server");
7543 staticpro (&QCserver
);
7544 QCnowait
= intern_c_string (":nowait");
7545 staticpro (&QCnowait
);
7546 QCsentinel
= intern_c_string (":sentinel");
7547 staticpro (&QCsentinel
);
7548 QClog
= intern_c_string (":log");
7550 QCnoquery
= intern_c_string (":noquery");
7551 staticpro (&QCnoquery
);
7552 QCstop
= intern_c_string (":stop");
7553 staticpro (&QCstop
);
7554 QCoptions
= intern_c_string (":options");
7555 staticpro (&QCoptions
);
7556 QCplist
= intern_c_string (":plist");
7557 staticpro (&QCplist
);
7559 Qlast_nonmenu_event
= intern_c_string ("last-nonmenu-event");
7560 staticpro (&Qlast_nonmenu_event
);
7562 staticpro (&Vprocess_alist
);
7564 staticpro (&deleted_pid_list
);
7567 #endif /* subprocesses */
7569 QCname
= intern_c_string (":name");
7570 staticpro (&QCname
);
7571 QCtype
= intern_c_string (":type");
7572 staticpro (&QCtype
);
7574 Qeuid
= intern_c_string ("euid");
7576 Qegid
= intern_c_string ("egid");
7578 Quser
= intern_c_string ("user");
7580 Qgroup
= intern_c_string ("group");
7581 staticpro (&Qgroup
);
7582 Qcomm
= intern_c_string ("comm");
7584 Qstate
= intern_c_string ("state");
7585 staticpro (&Qstate
);
7586 Qppid
= intern_c_string ("ppid");
7588 Qpgrp
= intern_c_string ("pgrp");
7590 Qsess
= intern_c_string ("sess");
7592 Qttname
= intern_c_string ("ttname");
7593 staticpro (&Qttname
);
7594 Qtpgid
= intern_c_string ("tpgid");
7595 staticpro (&Qtpgid
);
7596 Qminflt
= intern_c_string ("minflt");
7597 staticpro (&Qminflt
);
7598 Qmajflt
= intern_c_string ("majflt");
7599 staticpro (&Qmajflt
);
7600 Qcminflt
= intern_c_string ("cminflt");
7601 staticpro (&Qcminflt
);
7602 Qcmajflt
= intern_c_string ("cmajflt");
7603 staticpro (&Qcmajflt
);
7604 Qutime
= intern_c_string ("utime");
7605 staticpro (&Qutime
);
7606 Qstime
= intern_c_string ("stime");
7607 staticpro (&Qstime
);
7608 Qtime
= intern_c_string ("time");
7610 Qcutime
= intern_c_string ("cutime");
7611 staticpro (&Qcutime
);
7612 Qcstime
= intern_c_string ("cstime");
7613 staticpro (&Qcstime
);
7614 Qctime
= intern_c_string ("ctime");
7615 staticpro (&Qctime
);
7616 Qpri
= intern_c_string ("pri");
7618 Qnice
= intern_c_string ("nice");
7620 Qthcount
= intern_c_string ("thcount");
7621 staticpro (&Qthcount
);
7622 Qstart
= intern_c_string ("start");
7623 staticpro (&Qstart
);
7624 Qvsize
= intern_c_string ("vsize");
7625 staticpro (&Qvsize
);
7626 Qrss
= intern_c_string ("rss");
7628 Qetime
= intern_c_string ("etime");
7629 staticpro (&Qetime
);
7630 Qpcpu
= intern_c_string ("pcpu");
7632 Qpmem
= intern_c_string ("pmem");
7634 Qargs
= intern_c_string ("args");
7637 DEFVAR_BOOL ("delete-exited-processes", delete_exited_processes
,
7638 doc
: /* *Non-nil means delete processes immediately when they exit.
7639 A value of nil means don't delete them until `list-processes' is run. */);
7641 delete_exited_processes
= 1;
7644 DEFVAR_LISP ("process-connection-type", Vprocess_connection_type
,
7645 doc
: /* Control type of device used to communicate with subprocesses.
7646 Values are nil to use a pipe, or t or `pty' to use a pty.
7647 The value has no effect if the system has no ptys or if all ptys are busy:
7648 then a pipe is used in any case.
7649 The value takes effect when `start-process' is called. */);
7650 Vprocess_connection_type
= Qt
;
7652 #ifdef ADAPTIVE_READ_BUFFERING
7653 DEFVAR_LISP ("process-adaptive-read-buffering", Vprocess_adaptive_read_buffering
,
7654 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7655 On some systems, when Emacs reads the output from a subprocess, the output data
7656 is read in very small blocks, potentially resulting in very poor performance.
7657 This behavior can be remedied to some extent by setting this variable to a
7658 non-nil value, as it will automatically delay reading from such processes, to
7659 allow them to produce more output before Emacs tries to read it.
7660 If the value is t, the delay is reset after each write to the process; any other
7661 non-nil value means that the delay is not reset on write.
7662 The variable takes effect when `start-process' is called. */);
7663 Vprocess_adaptive_read_buffering
= Qt
;
7666 defsubr (&Sprocessp
);
7667 defsubr (&Sget_process
);
7668 defsubr (&Sdelete_process
);
7669 defsubr (&Sprocess_status
);
7670 defsubr (&Sprocess_exit_status
);
7671 defsubr (&Sprocess_id
);
7672 defsubr (&Sprocess_name
);
7673 defsubr (&Sprocess_tty_name
);
7674 defsubr (&Sprocess_command
);
7675 defsubr (&Sset_process_buffer
);
7676 defsubr (&Sprocess_buffer
);
7677 defsubr (&Sprocess_mark
);
7678 defsubr (&Sset_process_filter
);
7679 defsubr (&Sprocess_filter
);
7680 defsubr (&Sset_process_sentinel
);
7681 defsubr (&Sprocess_sentinel
);
7682 defsubr (&Sset_process_window_size
);
7683 defsubr (&Sset_process_inherit_coding_system_flag
);
7684 defsubr (&Sset_process_query_on_exit_flag
);
7685 defsubr (&Sprocess_query_on_exit_flag
);
7686 defsubr (&Sprocess_contact
);
7687 defsubr (&Sprocess_plist
);
7688 defsubr (&Sset_process_plist
);
7689 defsubr (&Slist_processes
);
7690 defsubr (&Sprocess_list
);
7691 defsubr (&Sstart_process
);
7692 defsubr (&Sserial_process_configure
);
7693 defsubr (&Smake_serial_process
);
7694 defsubr (&Sset_network_process_option
);
7695 defsubr (&Smake_network_process
);
7696 defsubr (&Sformat_network_address
);
7697 #if defined(HAVE_NET_IF_H)
7699 defsubr (&Snetwork_interface_list
);
7701 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7702 defsubr (&Snetwork_interface_info
);
7704 #endif /* defined(HAVE_NET_IF_H) */
7705 #ifdef DATAGRAM_SOCKETS
7706 defsubr (&Sprocess_datagram_address
);
7707 defsubr (&Sset_process_datagram_address
);
7709 defsubr (&Saccept_process_output
);
7710 defsubr (&Sprocess_send_region
);
7711 defsubr (&Sprocess_send_string
);
7712 defsubr (&Sinterrupt_process
);
7713 defsubr (&Skill_process
);
7714 defsubr (&Squit_process
);
7715 defsubr (&Sstop_process
);
7716 defsubr (&Scontinue_process
);
7717 defsubr (&Sprocess_running_child_p
);
7718 defsubr (&Sprocess_send_eof
);
7719 defsubr (&Ssignal_process
);
7720 defsubr (&Swaiting_for_user_input_p
);
7721 defsubr (&Sprocess_type
);
7722 defsubr (&Sset_process_coding_system
);
7723 defsubr (&Sprocess_coding_system
);
7724 defsubr (&Sset_process_filter_multibyte
);
7725 defsubr (&Sprocess_filter_multibyte_p
);
7727 #endif /* subprocesses */
7729 defsubr (&Sget_buffer_process
);
7730 defsubr (&Sprocess_inherit_coding_system_flag
);
7731 defsubr (&Slist_system_processes
);
7732 defsubr (&Sprocess_attributes
);