1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h> /* some typedefs are used in sys/file.h */
31 #ifdef HAVE_INTTYPES_H
40 /* Only MS-DOS does not define `subprocesses'. */
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 /* Are local (unix) sockets supported? */
49 #if defined (HAVE_SYS_UN_H)
50 #if !defined (AF_LOCAL) && defined (AF_UNIX)
51 #define AF_LOCAL AF_UNIX
54 #define HAVE_LOCAL_SOCKETS
59 #if defined(HAVE_SYS_IOCTL_H)
60 #include <sys/ioctl.h>
61 #if defined(HAVE_NET_IF_H)
63 #endif /* HAVE_NET_IF_H */
64 #endif /* HAVE_SYS_IOCTL_H */
71 #include <netinet/in.h>
72 #include <arpa/nameser.h>
84 #endif /* subprocesses */
92 #include "character.h"
96 #include "termhooks.h"
100 #include "blockinput.h"
101 #include "dispextern.h"
102 #include "composite.h"
104 #include "sysselect.h"
105 #include "syssignal.h"
111 #if defined (USE_GTK) || defined (HAVE_GCONF)
112 #include "xgselect.h"
113 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
118 extern int timers_run
;
120 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
121 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
122 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
123 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
124 Lisp_Object QCname
, QCtype
;
126 /* Non-zero if keyboard input is on hold, zero otherwise. */
128 static int kbd_is_on_hold
;
130 /* Nonzero means delete a process right away if it exits. */
131 static int delete_exited_processes
;
133 /* Nonzero means don't run process sentinels. This is used
135 int inhibit_sentinels
;
139 Lisp_Object Qprocessp
;
140 Lisp_Object Qrun
, Qstop
, Qsignal
;
141 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
142 Lisp_Object Qlocal
, Qipv4
, Qdatagram
, Qseqpacket
;
143 Lisp_Object Qreal
, Qnetwork
, Qserial
;
147 Lisp_Object QCport
, QCspeed
, QCprocess
;
148 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
149 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
150 Lisp_Object QCbuffer
, QChost
, QCservice
;
151 Lisp_Object QClocal
, QCremote
, QCcoding
;
152 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
153 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
154 Lisp_Object Qlast_nonmenu_event
;
155 /* QCfamily is declared and initialized in xfaces.c,
156 QCfilter in keyboard.c. */
157 extern Lisp_Object QCfamily
, QCfilter
;
159 /* Qexit is declared and initialized in eval.c. */
161 /* QCfamily is defined in xfaces.c. */
162 extern Lisp_Object QCfamily
;
163 /* QCfilter is defined in keyboard.c. */
164 extern Lisp_Object QCfilter
;
166 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
167 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
168 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
169 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
171 /* Define first descriptor number available for subprocesses. */
172 #define FIRST_PROC_DESC 3
174 extern const char *get_operating_system_release (void);
176 /* From sysdep.c or w32.c */
177 extern int serial_open (char *port
);
178 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
184 /* t means use pty, nil means use a pipe,
185 maybe other values to come. */
186 static Lisp_Object Vprocess_connection_type
;
188 /* These next two vars are non-static since sysdep.c uses them in the
189 emulation of `select'. */
190 /* Number of events of change of status of a process. */
192 /* Number of events for which the user or sentinel has been notified. */
195 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
197 /* Only W32 has this, it really means that select can't take write mask. */
198 #ifdef BROKEN_NON_BLOCKING_CONNECT
199 #undef NON_BLOCKING_CONNECT
200 #define SELECT_CANT_DO_WRITE_MASK
202 #ifndef NON_BLOCKING_CONNECT
204 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
205 #if defined (O_NONBLOCK) || defined (O_NDELAY)
206 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
207 #define NON_BLOCKING_CONNECT
208 #endif /* EWOULDBLOCK || EINPROGRESS */
209 #endif /* O_NONBLOCK || O_NDELAY */
210 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
211 #endif /* HAVE_SELECT */
212 #endif /* NON_BLOCKING_CONNECT */
213 #endif /* BROKEN_NON_BLOCKING_CONNECT */
215 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
216 this system. We need to read full packets, so we need a
217 "non-destructive" select. So we require either native select,
218 or emulation of select using FIONREAD. */
220 #ifdef BROKEN_DATAGRAM_SOCKETS
221 #undef DATAGRAM_SOCKETS
223 #ifndef DATAGRAM_SOCKETS
224 #if defined (HAVE_SELECT) || defined (FIONREAD)
225 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
226 #define DATAGRAM_SOCKETS
227 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
228 #endif /* HAVE_SELECT || FIONREAD */
229 #endif /* DATAGRAM_SOCKETS */
230 #endif /* BROKEN_DATAGRAM_SOCKETS */
232 #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
233 # define HAVE_SEQPACKET
236 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
237 #ifdef EMACS_HAS_USECS
238 #define ADAPTIVE_READ_BUFFERING
242 #ifdef ADAPTIVE_READ_BUFFERING
243 #define READ_OUTPUT_DELAY_INCREMENT 10000
244 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
245 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
247 /* Number of processes which have a non-zero read_output_delay,
248 and therefore might be delayed for adaptive read buffering. */
250 static int process_output_delay_count
;
252 /* Non-zero if any process has non-nil read_output_skip. */
254 static int process_output_skip
;
256 /* Non-nil means to delay reading process output to improve buffering.
257 A value of t means that delay is reset after each send, any other
258 non-nil value does not reset the delay. A value of nil disables
259 adaptive read buffering completely. */
260 static Lisp_Object Vprocess_adaptive_read_buffering
;
262 #define process_output_delay_count 0
265 static int keyboard_bit_set (SELECT_TYPE
*);
266 static void deactivate_process (Lisp_Object
);
267 static void status_notify (struct Lisp_Process
*);
268 static int read_process_output (Lisp_Object
, int);
269 static void create_pty (Lisp_Object
);
271 /* If we support a window system, turn on the code to poll periodically
272 to detect C-g. It isn't actually used when doing interrupt input. */
273 #ifdef HAVE_WINDOW_SYSTEM
274 #define POLL_FOR_INPUT
277 static Lisp_Object
get_process (register Lisp_Object name
);
278 static void exec_sentinel (Lisp_Object proc
, Lisp_Object reason
);
280 /* Mask of bits indicating the descriptors that we wait for input on. */
282 static SELECT_TYPE input_wait_mask
;
284 /* Mask that excludes keyboard input descriptor(s). */
286 static SELECT_TYPE non_keyboard_wait_mask
;
288 /* Mask that excludes process input descriptor(s). */
290 static SELECT_TYPE non_process_wait_mask
;
292 /* Mask for selecting for write. */
294 static SELECT_TYPE write_mask
;
296 #ifdef NON_BLOCKING_CONNECT
297 /* Mask of bits indicating the descriptors that we wait for connect to
298 complete on. Once they complete, they are removed from this mask
299 and added to the input_wait_mask and non_keyboard_wait_mask. */
301 static SELECT_TYPE connect_wait_mask
;
303 /* Number of bits set in connect_wait_mask. */
304 static int num_pending_connects
;
306 #define IF_NON_BLOCKING_CONNECT(s) s
307 #else /* NON_BLOCKING_CONNECT */
308 #define IF_NON_BLOCKING_CONNECT(s)
309 #endif /* NON_BLOCKING_CONNECT */
311 /* The largest descriptor currently in use for a process object. */
312 static int max_process_desc
;
314 /* The largest descriptor currently in use for input. */
315 static int max_input_desc
;
317 /* Indexed by descriptor, gives the process (if any) for that descriptor */
318 Lisp_Object chan_process
[MAXDESC
];
320 /* Alist of elements (NAME . PROCESS) */
321 Lisp_Object Vprocess_alist
;
323 /* Buffered-ahead input char from process, indexed by channel.
324 -1 means empty (no char is buffered).
325 Used on sys V where the only way to tell if there is any
326 output from the process is to read at least one char.
327 Always -1 on systems that support FIONREAD. */
329 /* Don't make static; need to access externally. */
330 int proc_buffered_char
[MAXDESC
];
332 /* Table of `struct coding-system' for each process. */
333 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
334 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
336 #ifdef DATAGRAM_SOCKETS
337 /* Table of `partner address' for datagram sockets. */
338 struct sockaddr_and_len
{
341 } datagram_address
[MAXDESC
];
342 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
343 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
345 #define DATAGRAM_CHAN_P(chan) (0)
346 #define DATAGRAM_CONN_P(proc) (0)
349 /* Maximum number of bytes to send to a pty without an eof. */
350 static int pty_max_bytes
;
354 struct fd_callback_data
360 int condition
; /* mask of the defines above. */
361 } fd_callback_info
[MAXDESC
];
364 /* Add a file descriptor FD to be monitored for when read is possible.
365 When read is possible, call FUNC with argument DATA. */
368 add_read_fd (int fd
, fd_callback func
, void *data
)
370 xassert (fd
< MAXDESC
);
371 add_keyboard_wait_descriptor (fd
);
373 fd_callback_info
[fd
].func
= func
;
374 fd_callback_info
[fd
].data
= data
;
375 fd_callback_info
[fd
].condition
|= FOR_READ
;
378 /* Stop monitoring file descriptor FD for when read is possible. */
381 delete_read_fd (int fd
)
383 xassert (fd
< MAXDESC
);
384 delete_keyboard_wait_descriptor (fd
);
386 fd_callback_info
[fd
].condition
&= ~FOR_READ
;
387 if (fd_callback_info
[fd
].condition
== 0)
389 fd_callback_info
[fd
].func
= 0;
390 fd_callback_info
[fd
].data
= 0;
394 /* Add a file descriptor FD to be monitored for when write is possible.
395 When write is possible, call FUNC with argument DATA. */
398 add_write_fd (int fd
, fd_callback func
, void *data
)
400 xassert (fd
< MAXDESC
);
401 FD_SET (fd
, &write_mask
);
402 if (fd
> max_input_desc
)
405 fd_callback_info
[fd
].func
= func
;
406 fd_callback_info
[fd
].data
= data
;
407 fd_callback_info
[fd
].condition
|= FOR_WRITE
;
410 /* Stop monitoring file descriptor FD for when write is possible. */
413 delete_write_fd (int fd
)
415 int lim
= max_input_desc
;
417 xassert (fd
< MAXDESC
);
418 FD_CLR (fd
, &write_mask
);
419 fd_callback_info
[fd
].condition
&= ~FOR_WRITE
;
420 if (fd_callback_info
[fd
].condition
== 0)
422 fd_callback_info
[fd
].func
= 0;
423 fd_callback_info
[fd
].data
= 0;
425 if (fd
== max_input_desc
)
426 for (fd
= lim
; fd
>= 0; fd
--)
427 if (FD_ISSET (fd
, &input_wait_mask
) || FD_ISSET (fd
, &write_mask
))
437 /* Compute the Lisp form of the process status, p->status, from
438 the numeric status that was returned by `wait'. */
440 static Lisp_Object
status_convert (int);
443 update_status (struct Lisp_Process
*p
)
445 eassert (p
->raw_status_new
);
446 p
->status
= status_convert (p
->raw_status
);
447 p
->raw_status_new
= 0;
450 /* Convert a process status word in Unix format to
451 the list that we use internally. */
454 status_convert (int w
)
457 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
458 else if (WIFEXITED (w
))
459 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
460 WCOREDUMP (w
) ? Qt
: Qnil
));
461 else if (WIFSIGNALED (w
))
462 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
463 WCOREDUMP (w
) ? Qt
: Qnil
));
468 /* Given a status-list, extract the three pieces of information
469 and store them individually through the three pointers. */
472 decode_status (Lisp_Object l
, Lisp_Object
*symbol
, int *code
, int *coredump
)
486 *code
= XFASTINT (XCAR (tem
));
488 *coredump
= !NILP (tem
);
492 /* Return a string describing a process status list. */
495 status_message (struct Lisp_Process
*p
)
497 Lisp_Object status
= p
->status
;
500 Lisp_Object string
, string2
;
502 decode_status (status
, &symbol
, &code
, &coredump
);
504 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
507 synchronize_system_messages_locale ();
508 signame
= strsignal (code
);
510 string
= build_string ("unknown");
515 string
= make_unibyte_string (signame
, strlen (signame
));
516 if (! NILP (Vlocale_coding_system
))
517 string
= (code_convert_string_norecord
518 (string
, Vlocale_coding_system
, 0));
519 c1
= STRING_CHAR ((char *) SDATA (string
));
522 Faset (string
, make_number (0), make_number (c2
));
524 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
525 return concat2 (string
, string2
);
527 else if (EQ (symbol
, Qexit
))
530 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
532 return build_string ("finished\n");
533 string
= Fnumber_to_string (make_number (code
));
534 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
535 return concat3 (build_string ("exited abnormally with code "),
538 else if (EQ (symbol
, Qfailed
))
540 string
= Fnumber_to_string (make_number (code
));
541 string2
= build_string ("\n");
542 return concat3 (build_string ("failed with code "),
546 return Fcopy_sequence (Fsymbol_name (symbol
));
551 /* The file name of the pty opened by allocate_pty. */
552 static char pty_name
[24];
554 /* Open an available pty, returning a file descriptor.
555 Return -1 on failure.
556 The file name of the terminal corresponding to the pty
557 is left in the variable pty_name. */
568 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
569 for (i
= 0; i
< 16; i
++)
572 struct stat stb
; /* Used in some PTY_OPEN. */
573 #ifdef PTY_NAME_SPRINTF
576 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
577 #endif /* no PTY_NAME_SPRINTF */
581 #else /* no PTY_OPEN */
583 { /* Some systems name their pseudoterminals so that there are gaps in
584 the usual sequence - for example, on HP9000/S700 systems, there
585 are no pseudoterminals with names ending in 'f'. So we wait for
586 three failures in a row before deciding that we've reached the
588 int failed_count
= 0;
590 if (stat (pty_name
, &stb
) < 0)
593 if (failed_count
>= 3)
600 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
602 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
605 #endif /* no PTY_OPEN */
609 /* check to make certain that both sides are available
610 this avoids a nasty yet stupid bug in rlogins */
611 #ifdef PTY_TTY_NAME_SPRINTF
614 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
615 #endif /* no PTY_TTY_NAME_SPRINTF */
616 if (access (pty_name
, 6) != 0)
631 #endif /* HAVE_PTYS */
634 make_process (Lisp_Object name
)
636 register Lisp_Object val
, tem
, name1
;
637 register struct Lisp_Process
*p
;
641 p
= allocate_process ();
649 p
->raw_status_new
= 0;
651 p
->mark
= Fmake_marker ();
652 p
->kill_without_query
= 0;
654 #ifdef ADAPTIVE_READ_BUFFERING
655 p
->adaptive_read_buffering
= 0;
656 p
->read_output_delay
= 0;
657 p
->read_output_skip
= 0;
661 p
->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
662 p
->gnutls_log_level
= 0;
666 /* If name is already in use, modify it until it is unused. */
671 tem
= Fget_process (name1
);
672 if (NILP (tem
)) break;
673 sprintf (suffix
, "<%d>", i
);
674 name1
= concat2 (name
, build_string (suffix
));
678 XSETPROCESS (val
, p
);
679 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
684 remove_process (register Lisp_Object proc
)
686 register Lisp_Object pair
;
688 pair
= Frassq (proc
, Vprocess_alist
);
689 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
691 deactivate_process (proc
);
695 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
696 doc
: /* Return t if OBJECT is a process. */)
699 return PROCESSP (object
) ? Qt
: Qnil
;
702 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
703 doc
: /* Return the process named NAME, or nil if there is none. */)
704 (register Lisp_Object name
)
709 return Fcdr (Fassoc (name
, Vprocess_alist
));
712 /* This is how commands for the user decode process arguments. It
713 accepts a process, a process name, a buffer, a buffer name, or nil.
714 Buffers denote the first process in the buffer, and nil denotes the
718 get_process (register Lisp_Object name
)
720 register Lisp_Object proc
, obj
;
723 obj
= Fget_process (name
);
725 obj
= Fget_buffer (name
);
727 error ("Process %s does not exist", SDATA (name
));
729 else if (NILP (name
))
730 obj
= Fcurrent_buffer ();
734 /* Now obj should be either a buffer object or a process object.
738 proc
= Fget_buffer_process (obj
);
740 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
752 /* Fdelete_process promises to immediately forget about the process, but in
753 reality, Emacs needs to remember those processes until they have been
754 treated by sigchld_handler; otherwise this handler would consider the
755 process as being synchronous and say that the synchronous process is
757 static Lisp_Object deleted_pid_list
;
760 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
761 doc
: /* Delete PROCESS: kill it and forget about it immediately.
762 PROCESS may be a process, a buffer, the name of a process or buffer, or
763 nil, indicating the current buffer's process. */)
764 (register Lisp_Object process
)
766 register struct Lisp_Process
*p
;
768 process
= get_process (process
);
769 p
= XPROCESS (process
);
771 p
->raw_status_new
= 0;
772 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
774 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
775 p
->tick
= ++process_tick
;
777 redisplay_preserve_echo_area (13);
779 else if (p
->infd
>= 0)
783 /* Assignment to EMACS_INT stops GCC whining about limited range
785 EMACS_INT pid
= p
->pid
;
787 /* No problem storing the pid here, as it is still in Vprocess_alist. */
788 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
789 /* GC treated elements set to nil. */
790 Fdelq (Qnil
, deleted_pid_list
));
791 /* If the process has already signaled, remove it from the list. */
792 if (p
->raw_status_new
)
795 if (CONSP (p
->status
))
796 symbol
= XCAR (p
->status
);
797 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
799 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
803 Fkill_process (process
, Qnil
);
804 /* Do this now, since remove_process will make sigchld_handler do nothing. */
806 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
807 p
->tick
= ++process_tick
;
809 redisplay_preserve_echo_area (13);
812 remove_process (process
);
816 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
817 doc
: /* Return the status of PROCESS.
818 The returned value is one of the following symbols:
819 run -- for a process that is running.
820 stop -- for a process stopped but continuable.
821 exit -- for a process that has exited.
822 signal -- for a process that has got a fatal signal.
823 open -- for a network stream connection that is open.
824 listen -- for a network stream server that is listening.
825 closed -- for a network stream connection that is closed.
826 connect -- when waiting for a non-blocking connection to complete.
827 failed -- when a non-blocking connection has failed.
828 nil -- if arg is a process name and no such process exists.
829 PROCESS may be a process, a buffer, the name of a process, or
830 nil, indicating the current buffer's process. */)
831 (register Lisp_Object process
)
833 register struct Lisp_Process
*p
;
834 register Lisp_Object status
;
836 if (STRINGP (process
))
837 process
= Fget_process (process
);
839 process
= get_process (process
);
844 p
= XPROCESS (process
);
845 if (p
->raw_status_new
)
849 status
= XCAR (status
);
850 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
852 if (EQ (status
, Qexit
))
854 else if (EQ (p
->command
, Qt
))
856 else if (EQ (status
, Qrun
))
862 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
864 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
865 If PROCESS has not yet exited or died, return 0. */)
866 (register Lisp_Object process
)
868 CHECK_PROCESS (process
);
869 if (XPROCESS (process
)->raw_status_new
)
870 update_status (XPROCESS (process
));
871 if (CONSP (XPROCESS (process
)->status
))
872 return XCAR (XCDR (XPROCESS (process
)->status
));
873 return make_number (0);
876 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
877 doc
: /* Return the process id of PROCESS.
878 This is the pid of the external process which PROCESS uses or talks to.
879 For a network connection, this value is nil. */)
880 (register Lisp_Object process
)
882 /* Assignment to EMACS_INT stops GCC whining about limited range of
886 CHECK_PROCESS (process
);
887 pid
= XPROCESS (process
)->pid
;
888 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
891 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
892 doc
: /* Return the name of PROCESS, as a string.
893 This is the name of the program invoked in PROCESS,
894 possibly modified to make it unique among process names. */)
895 (register Lisp_Object process
)
897 CHECK_PROCESS (process
);
898 return XPROCESS (process
)->name
;
901 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
902 doc
: /* Return the command that was executed to start PROCESS.
903 This is a list of strings, the first string being the program executed
904 and the rest of the strings being the arguments given to it.
905 For a network or serial process, this is nil (process is running) or t
906 \(process is stopped). */)
907 (register Lisp_Object process
)
909 CHECK_PROCESS (process
);
910 return XPROCESS (process
)->command
;
913 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
914 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
915 This is the terminal that the process itself reads and writes on,
916 not the name of the pty that Emacs uses to talk with that terminal. */)
917 (register Lisp_Object process
)
919 CHECK_PROCESS (process
);
920 return XPROCESS (process
)->tty_name
;
923 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
925 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
926 (register Lisp_Object process
, Lisp_Object buffer
)
928 struct Lisp_Process
*p
;
930 CHECK_PROCESS (process
);
932 CHECK_BUFFER (buffer
);
933 p
= XPROCESS (process
);
935 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
936 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
937 setup_process_coding_systems (process
);
941 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
943 doc
: /* Return the buffer PROCESS is associated with.
944 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
945 (register Lisp_Object process
)
947 CHECK_PROCESS (process
);
948 return XPROCESS (process
)->buffer
;
951 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
953 doc
: /* Return the marker for the end of the last output from PROCESS. */)
954 (register Lisp_Object process
)
956 CHECK_PROCESS (process
);
957 return XPROCESS (process
)->mark
;
960 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
962 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
963 A value of t means stop accepting output from the process.
965 When a process has a filter, its buffer is not used for output.
966 Instead, each time it does output, the entire string of output is
967 passed to the filter.
969 The filter gets two arguments: the process and the string of output.
970 The string argument is normally a multibyte string, except:
971 - if the process' input coding system is no-conversion or raw-text,
972 it is a unibyte string (the non-converted input), or else
973 - if `default-enable-multibyte-characters' is nil, it is a unibyte
974 string (the result of converting the decoded input multibyte
975 string to unibyte with `string-make-unibyte'). */)
976 (register Lisp_Object process
, Lisp_Object filter
)
978 struct Lisp_Process
*p
;
980 CHECK_PROCESS (process
);
981 p
= XPROCESS (process
);
983 /* Don't signal an error if the process' input file descriptor
984 is closed. This could make debugging Lisp more difficult,
985 for example when doing something like
987 (setq process (start-process ...))
989 (set-process-filter process ...) */
993 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
995 FD_CLR (p
->infd
, &input_wait_mask
);
996 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
998 else if (EQ (p
->filter
, Qt
)
999 /* Network or serial process not stopped: */
1000 && !EQ (p
->command
, Qt
))
1002 FD_SET (p
->infd
, &input_wait_mask
);
1003 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1008 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1009 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1010 setup_process_coding_systems (process
);
1014 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1016 doc
: /* Returns the filter function of PROCESS; nil if none.
1017 See `set-process-filter' for more info on filter functions. */)
1018 (register Lisp_Object process
)
1020 CHECK_PROCESS (process
);
1021 return XPROCESS (process
)->filter
;
1024 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1026 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1027 The sentinel is called as a function when the process changes state.
1028 It gets two arguments: the process, and a string describing the change. */)
1029 (register Lisp_Object process
, Lisp_Object sentinel
)
1031 struct Lisp_Process
*p
;
1033 CHECK_PROCESS (process
);
1034 p
= XPROCESS (process
);
1036 p
->sentinel
= sentinel
;
1037 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1038 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1042 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1044 doc
: /* Return the sentinel of PROCESS; nil if none.
1045 See `set-process-sentinel' for more info on sentinels. */)
1046 (register Lisp_Object process
)
1048 CHECK_PROCESS (process
);
1049 return XPROCESS (process
)->sentinel
;
1052 DEFUN ("set-process-window-size", Fset_process_window_size
,
1053 Sset_process_window_size
, 3, 3, 0,
1054 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1055 (register Lisp_Object process
, Lisp_Object height
, Lisp_Object width
)
1057 CHECK_PROCESS (process
);
1058 CHECK_NATNUM (height
);
1059 CHECK_NATNUM (width
);
1061 if (XPROCESS (process
)->infd
< 0
1062 || set_window_size (XPROCESS (process
)->infd
,
1063 XINT (height
), XINT (width
)) <= 0)
1069 DEFUN ("set-process-inherit-coding-system-flag",
1070 Fset_process_inherit_coding_system_flag
,
1071 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1072 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1073 If the second argument FLAG is non-nil, then the variable
1074 `buffer-file-coding-system' of the buffer associated with PROCESS
1075 will be bound to the value of the coding system used to decode
1078 This is useful when the coding system specified for the process buffer
1079 leaves either the character code conversion or the end-of-line conversion
1080 unspecified, or if the coding system used to decode the process output
1081 is more appropriate for saving the process buffer.
1083 Binding the variable `inherit-process-coding-system' to non-nil before
1084 starting the process is an alternative way of setting the inherit flag
1085 for the process which will run. */)
1086 (register Lisp_Object process
, Lisp_Object flag
)
1088 CHECK_PROCESS (process
);
1089 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1093 DEFUN ("set-process-query-on-exit-flag",
1094 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1096 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1097 If the second argument FLAG is non-nil, Emacs will query the user before
1098 exiting or killing a buffer if PROCESS is running. */)
1099 (register Lisp_Object process
, Lisp_Object flag
)
1101 CHECK_PROCESS (process
);
1102 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1106 DEFUN ("process-query-on-exit-flag",
1107 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1109 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1110 (register Lisp_Object process
)
1112 CHECK_PROCESS (process
);
1113 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1116 #ifdef DATAGRAM_SOCKETS
1117 Lisp_Object
Fprocess_datagram_address (Lisp_Object process
);
1120 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1122 doc
: /* Return the contact info of PROCESS; t for a real child.
1123 For a network or serial connection, the value depends on the optional
1124 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1125 SERVICE) for a network connection or (PORT SPEED) for a serial
1126 connection. If KEY is t, the complete contact information for the
1127 connection is returned, else the specific value for the keyword KEY is
1128 returned. See `make-network-process' or `make-serial-process' for a
1129 list of keywords. */)
1130 (register Lisp_Object process
, Lisp_Object key
)
1132 Lisp_Object contact
;
1134 CHECK_PROCESS (process
);
1135 contact
= XPROCESS (process
)->childp
;
1137 #ifdef DATAGRAM_SOCKETS
1138 if (DATAGRAM_CONN_P (process
)
1139 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1140 contact
= Fplist_put (contact
, QCremote
,
1141 Fprocess_datagram_address (process
));
1144 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1146 if (NILP (key
) && NETCONN_P (process
))
1147 return Fcons (Fplist_get (contact
, QChost
),
1148 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1149 if (NILP (key
) && SERIALCONN_P (process
))
1150 return Fcons (Fplist_get (contact
, QCport
),
1151 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1152 return Fplist_get (contact
, key
);
1155 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1157 doc
: /* Return the plist of PROCESS. */)
1158 (register Lisp_Object process
)
1160 CHECK_PROCESS (process
);
1161 return XPROCESS (process
)->plist
;
1164 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1166 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1167 (register Lisp_Object process
, Lisp_Object plist
)
1169 CHECK_PROCESS (process
);
1172 XPROCESS (process
)->plist
= plist
;
1176 #if 0 /* Turned off because we don't currently record this info
1177 in the process. Perhaps add it. */
1178 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1179 doc
: /* Return the connection type of PROCESS.
1180 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1181 a socket connection. */)
1182 (Lisp_Object process
)
1184 return XPROCESS (process
)->type
;
1188 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1189 doc
: /* Return the connection type of PROCESS.
1190 The value is either the symbol `real', `network', or `serial'.
1191 PROCESS may be a process, a buffer, the name of a process or buffer, or
1192 nil, indicating the current buffer's process. */)
1193 (Lisp_Object process
)
1196 proc
= get_process (process
);
1197 return XPROCESS (proc
)->type
;
1200 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1202 doc
: /* Convert network ADDRESS from internal format to a string.
1203 A 4 or 5 element vector represents an IPv4 address (with port number).
1204 An 8 or 9 element vector represents an IPv6 address (with port number).
1205 If optional second argument OMIT-PORT is non-nil, don't include a port
1206 number in the string, even when present in ADDRESS.
1207 Returns nil if format of ADDRESS is invalid. */)
1208 (Lisp_Object address
, Lisp_Object omit_port
)
1213 if (STRINGP (address
)) /* AF_LOCAL */
1216 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1218 register struct Lisp_Vector
*p
= XVECTOR (address
);
1219 Lisp_Object args
[10];
1222 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1224 args
[0] = build_string ("%d.%d.%d.%d");
1227 else if (p
->size
== 5)
1229 args
[0] = build_string ("%d.%d.%d.%d:%d");
1232 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1234 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1237 else if (p
->size
== 9)
1239 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1245 for (i
= 0; i
< nargs
; i
++)
1247 EMACS_INT element
= XINT (p
->contents
[i
]);
1249 if (element
< 0 || element
> 65535)
1252 if (nargs
<= 5 /* IPv4 */
1253 && i
< 4 /* host, not port */
1257 args
[i
+1] = p
->contents
[i
];
1260 return Fformat (nargs
+1, args
);
1263 if (CONSP (address
))
1265 Lisp_Object args
[2];
1266 args
[0] = build_string ("<Family %d>");
1267 args
[1] = Fcar (address
);
1268 return Fformat (2, args
);
1275 list_processes_1 (Lisp_Object query_only
)
1277 register Lisp_Object tail
, tem
;
1278 Lisp_Object proc
, minspace
, tem1
;
1279 register struct Lisp_Process
*p
;
1281 int w_proc
, w_buffer
, w_tty
;
1283 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1285 w_proc
= 4; /* Proc */
1286 w_buffer
= 6; /* Buffer */
1287 w_tty
= 0; /* Omit if no ttys */
1289 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1293 proc
= Fcdr (XCAR (tail
));
1294 p
= XPROCESS (proc
);
1297 if (!NILP (query_only
) && p
->kill_without_query
)
1299 if (STRINGP (p
->name
)
1300 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1302 if (!NILP (p
->buffer
))
1304 if (NILP (XBUFFER (p
->buffer
)->name
))
1307 w_buffer
= 8; /* (Killed) */
1309 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1312 if (STRINGP (p
->tty_name
)
1313 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1317 XSETFASTINT (i_status
, w_proc
+ 1);
1318 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1321 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1322 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1327 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1330 XSETFASTINT (minspace
, 1);
1332 set_buffer_internal (XBUFFER (Vstandard_output
));
1333 current_buffer
->undo_list
= Qt
;
1335 current_buffer
->truncate_lines
= Qt
;
1337 write_string ("Proc", -1);
1338 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1339 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1342 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1344 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1345 write_string ("\n", -1);
1347 write_string ("----", -1);
1348 Findent_to (i_status
, minspace
); write_string ("------", -1);
1349 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1352 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1354 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1355 write_string ("\n", -1);
1357 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1361 proc
= Fcdr (XCAR (tail
));
1362 p
= XPROCESS (proc
);
1365 if (!NILP (query_only
) && p
->kill_without_query
)
1368 Finsert (1, &p
->name
);
1369 Findent_to (i_status
, minspace
);
1371 if (p
->raw_status_new
)
1374 if (CONSP (p
->status
))
1375 symbol
= XCAR (p
->status
);
1377 if (EQ (symbol
, Qsignal
))
1380 tem
= Fcar (Fcdr (p
->status
));
1381 Fprinc (symbol
, Qnil
);
1383 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1385 if (EQ (symbol
, Qexit
))
1386 write_string ("closed", -1);
1387 else if (EQ (p
->command
, Qt
))
1388 write_string ("stopped", -1);
1389 else if (EQ (symbol
, Qrun
))
1390 write_string ("open", -1);
1392 Fprinc (symbol
, Qnil
);
1394 else if (SERIALCONN1_P (p
))
1396 write_string ("running", -1);
1399 Fprinc (symbol
, Qnil
);
1401 if (EQ (symbol
, Qexit
))
1404 tem
= Fcar (Fcdr (p
->status
));
1407 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1408 write_string (tembuf
, -1);
1412 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1415 Findent_to (i_buffer
, minspace
);
1416 if (NILP (p
->buffer
))
1417 insert_string ("(none)");
1418 else if (NILP (XBUFFER (p
->buffer
)->name
))
1419 insert_string ("(Killed)");
1421 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1425 Findent_to (i_tty
, minspace
);
1426 if (STRINGP (p
->tty_name
))
1427 Finsert (1, &p
->tty_name
);
1430 Findent_to (i_command
, minspace
);
1432 if (EQ (p
->status
, Qlisten
))
1434 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1435 if (INTEGERP (port
))
1436 port
= Fnumber_to_string (port
);
1438 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1439 sprintf (tembuf
, "(network %s server on %s)\n",
1440 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1441 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1442 insert_string (tembuf
);
1444 else if (NETCONN1_P (p
))
1446 /* For a local socket, there is no host name,
1447 so display service instead. */
1448 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1449 if (!STRINGP (host
))
1451 host
= Fplist_get (p
->childp
, QCservice
);
1452 if (INTEGERP (host
))
1453 host
= Fnumber_to_string (host
);
1456 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1457 sprintf (tembuf
, "(network %s connection to %s)\n",
1458 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1459 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1460 insert_string (tembuf
);
1462 else if (SERIALCONN1_P (p
))
1464 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1465 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1466 insert_string ("(serial port ");
1468 insert_string (SDATA (port
));
1470 insert_string ("?");
1471 if (INTEGERP (speed
))
1473 sprintf (tembuf
, " at %ld b/s", (long) XINT (speed
));
1474 insert_string (tembuf
);
1476 insert_string (")\n");
1490 insert_string (" ");
1492 insert_string ("\n");
1497 status_notify (NULL
);
1498 redisplay_preserve_echo_area (13);
1503 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1504 doc
: /* Display a list of all processes.
1505 If optional argument QUERY-ONLY is non-nil, only processes with
1506 the query-on-exit flag set will be listed.
1507 Any process listed as exited or signaled is actually eliminated
1508 after the listing is made. */)
1509 (Lisp_Object query_only
)
1511 internal_with_output_to_temp_buffer ("*Process List*",
1512 list_processes_1
, query_only
);
1516 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1517 doc
: /* Return a list of all processes. */)
1520 return Fmapcar (Qcdr
, Vprocess_alist
);
1523 /* Starting asynchronous inferior processes. */
1525 static Lisp_Object
start_process_unwind (Lisp_Object proc
);
1527 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1528 doc
: /* Start a program in a subprocess. Return the process object for it.
1529 NAME is name for process. It is modified if necessary to make it unique.
1530 BUFFER is the buffer (or buffer name) to associate with the process.
1532 Process output (both standard output and standard error streams) goes
1533 at end of BUFFER, unless you specify an output stream or filter
1534 function to handle the output. BUFFER may also be nil, meaning that
1535 this process is not associated with any buffer.
1537 PROGRAM is the program file name. It is searched for in PATH. If
1538 nil, just associate a pty with the buffer. Remaining arguments are
1539 strings to give program as arguments.
1541 If you want to separate standard output from standard error, invoke
1542 the command through a shell and redirect one of them using the shell
1545 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1546 (int nargs
, register Lisp_Object
*args
)
1548 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1549 register unsigned char **new_argv
;
1551 int count
= SPECPDL_INDEX ();
1555 buffer
= Fget_buffer_create (buffer
);
1557 /* Make sure that the child will be able to chdir to the current
1558 buffer's current directory, or its unhandled equivalent. We
1559 can't just have the child check for an error when it does the
1560 chdir, since it's in a vfork.
1562 We have to GCPRO around this because Fexpand_file_name and
1563 Funhandled_file_name_directory might call a file name handling
1564 function. The argument list is protected by the caller, so all
1565 we really have to worry about is buffer. */
1567 struct gcpro gcpro1
, gcpro2
;
1569 current_dir
= current_buffer
->directory
;
1571 GCPRO2 (buffer
, current_dir
);
1573 current_dir
= Funhandled_file_name_directory (current_dir
);
1574 if (NILP (current_dir
))
1575 /* If the file name handler says that current_dir is unreachable, use
1576 a sensible default. */
1577 current_dir
= build_string ("~/");
1578 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1579 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1580 report_file_error ("Setting current directory",
1581 Fcons (current_buffer
->directory
, Qnil
));
1587 CHECK_STRING (name
);
1591 if (!NILP (program
))
1592 CHECK_STRING (program
);
1594 proc
= make_process (name
);
1595 /* If an error occurs and we can't start the process, we want to
1596 remove it from the process list. This means that each error
1597 check in create_process doesn't need to call remove_process
1598 itself; it's all taken care of here. */
1599 record_unwind_protect (start_process_unwind
, proc
);
1601 XPROCESS (proc
)->childp
= Qt
;
1602 XPROCESS (proc
)->plist
= Qnil
;
1603 XPROCESS (proc
)->type
= Qreal
;
1604 XPROCESS (proc
)->buffer
= buffer
;
1605 XPROCESS (proc
)->sentinel
= Qnil
;
1606 XPROCESS (proc
)->filter
= Qnil
;
1607 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1610 /* AKA GNUTLS_INITSTAGE(proc). */
1611 XPROCESS (proc
)->gnutls_initstage
= GNUTLS_STAGE_EMPTY
;
1612 XPROCESS (proc
)->gnutls_cred_type
= Qnil
;
1615 #ifdef ADAPTIVE_READ_BUFFERING
1616 XPROCESS (proc
)->adaptive_read_buffering
1617 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1618 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1621 /* Make the process marker point into the process buffer (if any). */
1622 if (BUFFERP (buffer
))
1623 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1624 BUF_ZV (XBUFFER (buffer
)),
1625 BUF_ZV_BYTE (XBUFFER (buffer
)));
1628 /* Decide coding systems for communicating with the process. Here
1629 we don't setup the structure coding_system nor pay attention to
1630 unibyte mode. They are done in create_process. */
1632 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1633 Lisp_Object coding_systems
= Qt
;
1634 Lisp_Object val
, *args2
;
1635 struct gcpro gcpro1
, gcpro2
;
1637 val
= Vcoding_system_for_read
;
1640 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1641 args2
[0] = Qstart_process
;
1642 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1643 GCPRO2 (proc
, current_dir
);
1644 if (!NILP (program
))
1645 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1647 if (CONSP (coding_systems
))
1648 val
= XCAR (coding_systems
);
1649 else if (CONSP (Vdefault_process_coding_system
))
1650 val
= XCAR (Vdefault_process_coding_system
);
1652 XPROCESS (proc
)->decode_coding_system
= val
;
1654 val
= Vcoding_system_for_write
;
1657 if (EQ (coding_systems
, Qt
))
1659 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1660 args2
[0] = Qstart_process
;
1661 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1662 GCPRO2 (proc
, current_dir
);
1663 if (!NILP (program
))
1664 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1667 if (CONSP (coding_systems
))
1668 val
= XCDR (coding_systems
);
1669 else if (CONSP (Vdefault_process_coding_system
))
1670 val
= XCDR (Vdefault_process_coding_system
);
1672 XPROCESS (proc
)->encode_coding_system
= val
;
1673 /* Note: At this momemnt, the above coding system may leave
1674 text-conversion or eol-conversion unspecified. They will be
1675 decided after we read output from the process and decode it by
1676 some coding system, or just before we actually send a text to
1681 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1682 XPROCESS (proc
)->decoding_carryover
= 0;
1683 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1685 XPROCESS (proc
)->inherit_coding_system_flag
1686 = !(NILP (buffer
) || !inherit_process_coding_system
);
1688 if (!NILP (program
))
1690 /* If program file name is not absolute, search our path for it.
1691 Put the name we will really use in TEM. */
1692 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1693 && !(SCHARS (program
) > 1
1694 && IS_DEVICE_SEP (SREF (program
, 1))))
1696 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1699 GCPRO4 (name
, program
, buffer
, current_dir
);
1700 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1703 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1704 tem
= Fexpand_file_name (tem
, Qnil
);
1708 if (!NILP (Ffile_directory_p (program
)))
1709 error ("Specified program for new process is a directory");
1713 /* If program file name starts with /: for quoting a magic name,
1715 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1716 && SREF (tem
, 1) == ':')
1717 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1720 Lisp_Object arg_encoding
= Qnil
;
1721 struct gcpro gcpro1
;
1724 /* Encode the file name and put it in NEW_ARGV.
1725 That's where the child will use it to execute the program. */
1726 tem
= Fcons (ENCODE_FILE (tem
), Qnil
);
1728 /* Here we encode arguments by the coding system used for sending
1729 data to the process. We don't support using different coding
1730 systems for encoding arguments and for encoding data sent to the
1733 for (i
= 3; i
< nargs
; i
++)
1735 tem
= Fcons (args
[i
], tem
);
1736 CHECK_STRING (XCAR (tem
));
1737 if (STRING_MULTIBYTE (XCAR (tem
)))
1739 if (NILP (arg_encoding
))
1740 arg_encoding
= (complement_process_encoding_system
1741 (XPROCESS (proc
)->encode_coding_system
));
1743 code_convert_string_norecord
1744 (XCAR (tem
), arg_encoding
, 1));
1751 /* Now that everything is encoded we can collect the strings into
1753 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1754 new_argv
[nargs
- 2] = 0;
1756 for (i
= nargs
- 3; i
>= 0; i
--)
1758 new_argv
[i
] = SDATA (XCAR (tem
));
1762 create_process (proc
, (char **) new_argv
, current_dir
);
1767 return unbind_to (count
, proc
);
1770 /* This function is the unwind_protect form for Fstart_process. If
1771 PROC doesn't have its pid set, then we know someone has signaled
1772 an error and the process wasn't started successfully, so we should
1773 remove it from the process list. */
1775 start_process_unwind (Lisp_Object proc
)
1777 if (!PROCESSP (proc
))
1780 /* Was PROC started successfully? */
1781 if (XPROCESS (proc
)->pid
== -1)
1782 remove_process (proc
);
1788 create_process_1 (struct atimer
*timer
)
1790 /* Nothing to do. */
1795 create_process (Lisp_Object process
, char **new_argv
, Lisp_Object current_dir
)
1797 int inchannel
, outchannel
;
1800 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1801 int wait_child_setup
[2];
1805 struct sigaction sigint_action
;
1806 struct sigaction sigquit_action
;
1808 struct sigaction sighup_action
;
1810 /* Use volatile to protect variables from being clobbered by longjmp. */
1811 volatile int forkin
, forkout
;
1812 volatile int pty_flag
= 0;
1814 extern char **environ
;
1817 inchannel
= outchannel
= -1;
1820 if (!NILP (Vprocess_connection_type
))
1821 outchannel
= inchannel
= allocate_pty ();
1825 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1826 /* On most USG systems it does not work to open the pty's tty here,
1827 then close it and reopen it in the child. */
1829 /* Don't let this terminal become our controlling terminal
1830 (in case we don't have one). */
1831 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1833 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1836 report_file_error ("Opening pty", Qnil
);
1838 forkin
= forkout
= -1;
1839 #endif /* not USG, or USG_SUBTTY_WORKS */
1843 #endif /* HAVE_PTYS */
1848 report_file_error ("Creating pipe", Qnil
);
1854 emacs_close (inchannel
);
1855 emacs_close (forkout
);
1856 report_file_error ("Creating pipe", Qnil
);
1862 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1866 tem
= pipe (wait_child_setup
);
1868 report_file_error ("Creating pipe", Qnil
);
1869 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1871 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1874 emacs_close (wait_child_setup
[0]);
1875 emacs_close (wait_child_setup
[1]);
1876 report_file_error ("Setting file descriptor flags", Qnil
);
1882 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1883 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1886 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1887 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1891 /* Record this as an active process, with its channels.
1892 As a result, child_setup will close Emacs's side of the pipes. */
1893 chan_process
[inchannel
] = process
;
1894 XPROCESS (process
)->infd
= inchannel
;
1895 XPROCESS (process
)->outfd
= outchannel
;
1897 /* Previously we recorded the tty descriptor used in the subprocess.
1898 It was only used for getting the foreground tty process, so now
1899 we just reopen the device (see emacs_get_tty_pgrp) as this is
1900 more portable (see USG_SUBTTY_WORKS above). */
1902 XPROCESS (process
)->pty_flag
= pty_flag
;
1903 XPROCESS (process
)->status
= Qrun
;
1904 setup_process_coding_systems (process
);
1906 /* Delay interrupts until we have a chance to store
1907 the new fork's pid in its process structure */
1908 sigemptyset (&blocked
);
1910 sigaddset (&blocked
, SIGCHLD
);
1912 #ifdef HAVE_WORKING_VFORK
1913 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1914 this sets the parent's signal handlers as well as the child's.
1915 So delay all interrupts whose handlers the child might munge,
1916 and record the current handlers so they can be restored later. */
1917 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1918 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1920 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1922 #endif /* HAVE_WORKING_VFORK */
1923 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1925 FD_SET (inchannel
, &input_wait_mask
);
1926 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1927 if (inchannel
> max_process_desc
)
1928 max_process_desc
= inchannel
;
1930 /* Until we store the proper pid, enable sigchld_handler
1931 to recognize an unknown pid as standing for this process.
1932 It is very important not to let this `marker' value stay
1933 in the table after this function has returned; if it does
1934 it might cause call-process to hang and subsequent asynchronous
1935 processes to get their return values scrambled. */
1936 XPROCESS (process
)->pid
= -1;
1941 /* child_setup must clobber environ on systems with true vfork.
1942 Protect it from permanent change. */
1943 char **save_environ
= environ
;
1945 current_dir
= ENCODE_FILE (current_dir
);
1950 #endif /* not WINDOWSNT */
1952 int xforkin
= forkin
;
1953 int xforkout
= forkout
;
1955 #if 0 /* This was probably a mistake--it duplicates code later on,
1956 but fails to handle all the cases. */
1957 /* Make sure SIGCHLD is not blocked in the child. */
1958 sigsetmask (SIGEMPTYMASK
);
1961 /* Make the pty be the controlling terminal of the process. */
1963 /* First, disconnect its current controlling terminal. */
1965 /* We tried doing setsid only if pty_flag, but it caused
1966 process_set_signal to fail on SGI when using a pipe. */
1968 /* Make the pty's terminal the controlling terminal. */
1969 if (pty_flag
&& xforkin
>= 0)
1972 /* We ignore the return value
1973 because faith@cs.unc.edu says that is necessary on Linux. */
1974 ioctl (xforkin
, TIOCSCTTY
, 0);
1977 #else /* not HAVE_SETSID */
1979 /* It's very important to call setpgrp here and no time
1980 afterwards. Otherwise, we lose our controlling tty which
1981 is set when we open the pty. */
1984 #endif /* not HAVE_SETSID */
1985 #if defined (LDISC1)
1986 if (pty_flag
&& xforkin
>= 0)
1989 tcgetattr (xforkin
, &t
);
1991 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1992 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1995 #if defined (NTTYDISC) && defined (TIOCSETD)
1996 if (pty_flag
&& xforkin
>= 0)
1998 /* Use new line discipline. */
1999 int ldisc
= NTTYDISC
;
2000 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2005 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2006 can do TIOCSPGRP only to the process's controlling tty. */
2009 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2010 I can't test it since I don't have 4.3. */
2011 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2014 ioctl (j
, TIOCNOTTY
, 0);
2018 /* In order to get a controlling terminal on some versions
2019 of BSD, it is necessary to put the process in pgrp 0
2020 before it opens the terminal. */
2028 #endif /* TIOCNOTTY */
2030 #if !defined (DONT_REOPEN_PTY)
2031 /*** There is a suggestion that this ought to be a
2032 conditional on TIOCSPGRP,
2033 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2034 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2035 that system does seem to need this code, even though
2036 both HAVE_SETSID and TIOCSCTTY are defined. */
2037 /* Now close the pty (if we had it open) and reopen it.
2038 This makes the pty the controlling terminal of the subprocess. */
2042 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2045 emacs_close (xforkin
);
2046 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2050 emacs_write (1, "Couldn't open the pty terminal ", 31);
2051 emacs_write (1, pty_name
, strlen (pty_name
));
2052 emacs_write (1, "\n", 1);
2057 #endif /* not DONT_REOPEN_PTY */
2059 #ifdef SETUP_SLAVE_PTY
2064 #endif /* SETUP_SLAVE_PTY */
2066 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2067 Now reenable it in the child, so it will die when we want it to. */
2069 signal (SIGHUP
, SIG_DFL
);
2071 #endif /* HAVE_PTYS */
2073 signal (SIGINT
, SIG_DFL
);
2074 signal (SIGQUIT
, SIG_DFL
);
2076 /* Stop blocking signals in the child. */
2077 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2080 child_setup_tty (xforkout
);
2082 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2083 new_argv
, 1, current_dir
);
2084 #else /* not WINDOWSNT */
2086 emacs_close (wait_child_setup
[0]);
2088 child_setup (xforkin
, xforkout
, xforkout
,
2089 new_argv
, 1, current_dir
);
2090 #endif /* not WINDOWSNT */
2092 environ
= save_environ
;
2097 /* This runs in the Emacs process. */
2101 emacs_close (forkin
);
2102 if (forkin
!= forkout
&& forkout
>= 0)
2103 emacs_close (forkout
);
2107 /* vfork succeeded. */
2108 XPROCESS (process
)->pid
= pid
;
2111 register_child (pid
, inchannel
);
2112 #endif /* WINDOWSNT */
2114 /* If the subfork execv fails, and it exits,
2115 this close hangs. I don't know why.
2116 So have an interrupt jar it loose. */
2118 struct atimer
*timer
;
2122 EMACS_SET_SECS_USECS (offset
, 1, 0);
2123 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2126 emacs_close (forkin
);
2128 cancel_atimer (timer
);
2132 if (forkin
!= forkout
&& forkout
>= 0)
2133 emacs_close (forkout
);
2137 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2140 XPROCESS (process
)->tty_name
= Qnil
;
2142 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2143 /* Wait for child_setup to complete in case that vfork is
2144 actually defined as fork. The descriptor wait_child_setup[1]
2145 of a pipe is closed at the child side either by close-on-exec
2146 on successful execvp or the _exit call in child_setup. */
2150 emacs_close (wait_child_setup
[1]);
2151 emacs_read (wait_child_setup
[0], &dummy
, 1);
2152 emacs_close (wait_child_setup
[0]);
2157 /* Restore the signal state whether vfork succeeded or not.
2158 (We will signal an error, below, if it failed.) */
2159 #ifdef HAVE_WORKING_VFORK
2160 /* Restore the parent's signal handlers. */
2161 sigaction (SIGINT
, &sigint_action
, 0);
2162 sigaction (SIGQUIT
, &sigquit_action
, 0);
2164 sigaction (SIGHUP
, &sighup_action
, 0);
2166 #endif /* HAVE_WORKING_VFORK */
2167 /* Stop blocking signals in the parent. */
2168 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2170 /* Now generate the error if vfork failed. */
2172 report_file_error ("Doing vfork", Qnil
);
2176 create_pty (Lisp_Object process
)
2178 int inchannel
, outchannel
;
2180 /* Use volatile to protect variables from being clobbered by longjmp. */
2181 volatile int forkin
, forkout
;
2182 volatile int pty_flag
= 0;
2184 inchannel
= outchannel
= -1;
2187 if (!NILP (Vprocess_connection_type
))
2188 outchannel
= inchannel
= allocate_pty ();
2192 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2193 /* On most USG systems it does not work to open the pty's tty here,
2194 then close it and reopen it in the child. */
2196 /* Don't let this terminal become our controlling terminal
2197 (in case we don't have one). */
2198 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
2200 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
2203 report_file_error ("Opening pty", Qnil
);
2204 #if defined (DONT_REOPEN_PTY)
2205 /* In the case that vfork is defined as fork, the parent process
2206 (Emacs) may send some data before the child process completes
2207 tty options setup. So we setup tty before forking. */
2208 child_setup_tty (forkout
);
2209 #endif /* DONT_REOPEN_PTY */
2211 forkin
= forkout
= -1;
2212 #endif /* not USG, or USG_SUBTTY_WORKS */
2215 #endif /* HAVE_PTYS */
2218 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
2219 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
2222 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
2223 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
2227 /* Record this as an active process, with its channels.
2228 As a result, child_setup will close Emacs's side of the pipes. */
2229 chan_process
[inchannel
] = process
;
2230 XPROCESS (process
)->infd
= inchannel
;
2231 XPROCESS (process
)->outfd
= outchannel
;
2233 /* Previously we recorded the tty descriptor used in the subprocess.
2234 It was only used for getting the foreground tty process, so now
2235 we just reopen the device (see emacs_get_tty_pgrp) as this is
2236 more portable (see USG_SUBTTY_WORKS above). */
2238 XPROCESS (process
)->pty_flag
= pty_flag
;
2239 XPROCESS (process
)->status
= Qrun
;
2240 setup_process_coding_systems (process
);
2242 FD_SET (inchannel
, &input_wait_mask
);
2243 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2244 if (inchannel
> max_process_desc
)
2245 max_process_desc
= inchannel
;
2247 XPROCESS (process
)->pid
= -2;
2250 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2253 XPROCESS (process
)->tty_name
= Qnil
;
2257 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2258 The address family of sa is not included in the result. */
2261 conv_sockaddr_to_lisp (struct sockaddr
*sa
, int len
)
2263 Lisp_Object address
;
2266 register struct Lisp_Vector
*p
;
2268 /* Workaround for a bug in getsockname on BSD: Names bound to
2269 sockets in the UNIX domain are inaccessible; getsockname returns
2270 a zero length name. */
2271 if (len
< offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
))
2272 return empty_unibyte_string
;
2274 switch (sa
->sa_family
)
2278 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2279 len
= sizeof (sin
->sin_addr
) + 1;
2280 address
= Fmake_vector (make_number (len
), Qnil
);
2281 p
= XVECTOR (address
);
2282 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2283 cp
= (unsigned char *) &sin
->sin_addr
;
2289 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2290 uint16_t *ip6
= (uint16_t *) &sin6
->sin6_addr
;
2291 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2292 address
= Fmake_vector (make_number (len
), Qnil
);
2293 p
= XVECTOR (address
);
2294 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2295 for (i
= 0; i
< len
; i
++)
2296 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2300 #ifdef HAVE_LOCAL_SOCKETS
2303 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2304 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2305 if (sockun
->sun_path
[i
] == 0)
2307 return make_unibyte_string (sockun
->sun_path
, i
);
2311 len
-= offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
);
2312 address
= Fcons (make_number (sa
->sa_family
),
2313 Fmake_vector (make_number (len
), Qnil
));
2314 p
= XVECTOR (XCDR (address
));
2315 cp
= (unsigned char *) &sa
->sa_family
+ sizeof (sa
->sa_family
);
2321 p
->contents
[i
++] = make_number (*cp
++);
2327 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2330 get_lisp_to_sockaddr_size (Lisp_Object address
, int *familyp
)
2332 register struct Lisp_Vector
*p
;
2334 if (VECTORP (address
))
2336 p
= XVECTOR (address
);
2340 return sizeof (struct sockaddr_in
);
2343 else if (p
->size
== 9)
2345 *familyp
= AF_INET6
;
2346 return sizeof (struct sockaddr_in6
);
2350 #ifdef HAVE_LOCAL_SOCKETS
2351 else if (STRINGP (address
))
2353 *familyp
= AF_LOCAL
;
2354 return sizeof (struct sockaddr_un
);
2357 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2359 struct sockaddr
*sa
;
2360 *familyp
= XINT (XCAR (address
));
2361 p
= XVECTOR (XCDR (address
));
2362 return p
->size
+ sizeof (sa
->sa_family
);
2367 /* Convert an address object (vector or string) to an internal sockaddr.
2369 The address format has been basically validated by
2370 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2371 it could have come from user data. So if FAMILY is not valid,
2372 we return after zeroing *SA. */
2375 conv_lisp_to_sockaddr (int family
, Lisp_Object address
, struct sockaddr
*sa
, int len
)
2377 register struct Lisp_Vector
*p
;
2378 register unsigned char *cp
= NULL
;
2381 memset (sa
, 0, len
);
2383 if (VECTORP (address
))
2385 p
= XVECTOR (address
);
2386 if (family
== AF_INET
)
2388 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2389 len
= sizeof (sin
->sin_addr
) + 1;
2390 i
= XINT (p
->contents
[--len
]);
2391 sin
->sin_port
= htons (i
);
2392 cp
= (unsigned char *)&sin
->sin_addr
;
2393 sa
->sa_family
= family
;
2396 else if (family
== AF_INET6
)
2398 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2399 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2400 len
= sizeof (sin6
->sin6_addr
) + 1;
2401 i
= XINT (p
->contents
[--len
]);
2402 sin6
->sin6_port
= htons (i
);
2403 for (i
= 0; i
< len
; i
++)
2404 if (INTEGERP (p
->contents
[i
]))
2406 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2409 sa
->sa_family
= family
;
2416 else if (STRINGP (address
))
2418 #ifdef HAVE_LOCAL_SOCKETS
2419 if (family
== AF_LOCAL
)
2421 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2422 cp
= SDATA (address
);
2423 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2424 sockun
->sun_path
[i
] = *cp
++;
2425 sa
->sa_family
= family
;
2432 p
= XVECTOR (XCDR (address
));
2433 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2436 for (i
= 0; i
< len
; i
++)
2437 if (INTEGERP (p
->contents
[i
]))
2438 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2441 #ifdef DATAGRAM_SOCKETS
2442 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2444 doc
: /* Get the current datagram address associated with PROCESS. */)
2445 (Lisp_Object process
)
2449 CHECK_PROCESS (process
);
2451 if (!DATAGRAM_CONN_P (process
))
2454 channel
= XPROCESS (process
)->infd
;
2455 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2456 datagram_address
[channel
].len
);
2459 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2461 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2462 Returns nil upon error setting address, ADDRESS otherwise. */)
2463 (Lisp_Object process
, Lisp_Object address
)
2468 CHECK_PROCESS (process
);
2470 if (!DATAGRAM_CONN_P (process
))
2473 channel
= XPROCESS (process
)->infd
;
2475 len
= get_lisp_to_sockaddr_size (address
, &family
);
2476 if (datagram_address
[channel
].len
!= len
)
2478 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2484 static const struct socket_options
{
2485 /* The name of this option. Should be lowercase version of option
2486 name without SO_ prefix. */
2488 /* Option level SOL_... */
2490 /* Option number SO_... */
2492 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2493 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2494 } socket_options
[] =
2496 #ifdef SO_BINDTODEVICE
2497 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2500 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2503 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2506 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2509 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2512 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2515 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2518 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2520 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2523 /* Set option OPT to value VAL on socket S.
2525 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2526 Signals an error if setting a known option fails.
2530 set_socket_option (int s
, Lisp_Object opt
, Lisp_Object val
)
2533 const struct socket_options
*sopt
;
2538 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2539 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2540 if (strcmp (name
, sopt
->name
) == 0)
2543 switch (sopt
->opttype
)
2548 optval
= NILP (val
) ? 0 : 1;
2549 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2550 &optval
, sizeof (optval
));
2558 optval
= XINT (val
);
2560 error ("Bad option value for %s", name
);
2561 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2562 &optval
, sizeof (optval
));
2566 #ifdef SO_BINDTODEVICE
2569 char devname
[IFNAMSIZ
+1];
2571 /* This is broken, at least in the Linux 2.4 kernel.
2572 To unbind, the arg must be a zero integer, not the empty string.
2573 This should work on all systems. KFS. 2003-09-23. */
2574 memset (devname
, 0, sizeof devname
);
2577 char *arg
= (char *) SDATA (val
);
2578 int len
= min (strlen (arg
), IFNAMSIZ
);
2579 memcpy (devname
, arg
, len
);
2581 else if (!NILP (val
))
2582 error ("Bad option value for %s", name
);
2583 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2592 struct linger linger
;
2595 linger
.l_linger
= 0;
2597 linger
.l_linger
= XINT (val
);
2599 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2600 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2601 &linger
, sizeof (linger
));
2611 report_file_error ("Cannot set network option",
2612 Fcons (opt
, Fcons (val
, Qnil
)));
2613 return (1 << sopt
->optbit
);
2617 DEFUN ("set-network-process-option",
2618 Fset_network_process_option
, Sset_network_process_option
,
2620 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2621 See `make-network-process' for a list of options and values.
2622 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2623 OPTION is not a supported option, return nil instead; otherwise return t. */)
2624 (Lisp_Object process
, Lisp_Object option
, Lisp_Object value
, Lisp_Object no_error
)
2627 struct Lisp_Process
*p
;
2629 CHECK_PROCESS (process
);
2630 p
= XPROCESS (process
);
2631 if (!NETCONN1_P (p
))
2632 error ("Process is not a network process");
2636 error ("Process is not running");
2638 if (set_socket_option (s
, option
, value
))
2640 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2644 if (NILP (no_error
))
2645 error ("Unknown or unsupported option");
2651 DEFUN ("serial-process-configure",
2652 Fserial_process_configure
,
2653 Sserial_process_configure
,
2655 doc
: /* Configure speed, bytesize, etc. of a serial process.
2657 Arguments are specified as keyword/argument pairs. Attributes that
2658 are not given are re-initialized from the process's current
2659 configuration (available via the function `process-contact') or set to
2660 reasonable default values. The following arguments are defined:
2666 -- Any of these arguments can be given to identify the process that is
2667 to be configured. If none of these arguments is given, the current
2668 buffer's process is used.
2670 :speed SPEED -- SPEED is the speed of the serial port in bits per
2671 second, also called baud rate. Any value can be given for SPEED, but
2672 most serial ports work only at a few defined values between 1200 and
2673 115200, with 9600 being the most common value. If SPEED is nil, the
2674 serial port is not configured any further, i.e., all other arguments
2675 are ignored. This may be useful for special serial ports such as
2676 Bluetooth-to-serial converters which can only be configured through AT
2677 commands. A value of nil for SPEED can be used only when passed
2678 through `make-serial-process' or `serial-term'.
2680 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2681 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2683 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2684 `odd' (use odd parity), or the symbol `even' (use even parity). If
2685 PARITY is not given, no parity is used.
2687 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2688 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2689 is not given or nil, 1 stopbit is used.
2691 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2692 flowcontrol to be used, which is either nil (don't use flowcontrol),
2693 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2694 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2695 flowcontrol is used.
2697 `serial-process-configure' is called by `make-serial-process' for the
2698 initial configuration of the serial port.
2702 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2704 \(serial-process-configure
2705 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2707 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2709 usage: (serial-process-configure &rest ARGS) */)
2710 (int nargs
, Lisp_Object
*args
)
2712 struct Lisp_Process
*p
;
2713 Lisp_Object contact
= Qnil
;
2714 Lisp_Object proc
= Qnil
;
2715 struct gcpro gcpro1
;
2717 contact
= Flist (nargs
, args
);
2720 proc
= Fplist_get (contact
, QCprocess
);
2722 proc
= Fplist_get (contact
, QCname
);
2724 proc
= Fplist_get (contact
, QCbuffer
);
2726 proc
= Fplist_get (contact
, QCport
);
2727 proc
= get_process (proc
);
2728 p
= XPROCESS (proc
);
2729 if (!EQ (p
->type
, Qserial
))
2730 error ("Not a serial process");
2732 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2738 serial_configure (p
, contact
);
2744 /* Used by make-serial-process to recover from errors. */
2745 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2747 if (!PROCESSP (proc
))
2749 remove_process (proc
);
2753 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2755 doc
: /* Create and return a serial port process.
2757 In Emacs, serial port connections are represented by process objects,
2758 so input and output work as for subprocesses, and `delete-process'
2759 closes a serial port connection. However, a serial process has no
2760 process id, it cannot be signaled, and the status codes are different
2761 from normal processes.
2763 `make-serial-process' creates a process and a buffer, on which you
2764 probably want to use `process-send-string'. Try \\[serial-term] for
2765 an interactive terminal. See below for examples.
2767 Arguments are specified as keyword/argument pairs. The following
2768 arguments are defined:
2770 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2771 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2772 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2773 the backslashes in strings).
2775 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2776 which is called by `make-serial-process'.
2778 :name NAME -- NAME is the name of the process. If NAME is not given,
2779 the value of PORT is used.
2781 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2782 with the process. Process output goes at the end of that buffer,
2783 unless you specify an output stream or filter function to handle the
2784 output. If BUFFER is not given, the value of NAME is used.
2786 :coding CODING -- If CODING is a symbol, it specifies the coding
2787 system used for both reading and writing for this process. If CODING
2788 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2789 ENCODING is used for writing.
2791 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2792 the process is running. If BOOL is not given, query before exiting.
2794 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2795 In the stopped state, a serial process does not accept incoming data,
2796 but you can send outgoing data. The stopped state is cleared by
2797 `continue-process' and set by `stop-process'.
2799 :filter FILTER -- Install FILTER as the process filter.
2801 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2803 :plist PLIST -- Install PLIST as the initial plist of the process.
2810 -- These arguments are handled by `serial-process-configure', which is
2811 called by `make-serial-process'.
2813 The original argument list, possibly modified by later configuration,
2814 is available via the function `process-contact'.
2818 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2820 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2822 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2824 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2826 usage: (make-serial-process &rest ARGS) */)
2827 (int nargs
, Lisp_Object
*args
)
2830 Lisp_Object proc
, contact
, port
;
2831 struct Lisp_Process
*p
;
2832 struct gcpro gcpro1
;
2833 Lisp_Object name
, buffer
;
2834 Lisp_Object tem
, val
;
2835 int specpdl_count
= -1;
2840 contact
= Flist (nargs
, args
);
2843 port
= Fplist_get (contact
, QCport
);
2845 error ("No port specified");
2846 CHECK_STRING (port
);
2848 if (NILP (Fplist_member (contact
, QCspeed
)))
2849 error (":speed not specified");
2850 if (!NILP (Fplist_get (contact
, QCspeed
)))
2851 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2853 name
= Fplist_get (contact
, QCname
);
2856 CHECK_STRING (name
);
2857 proc
= make_process (name
);
2858 specpdl_count
= SPECPDL_INDEX ();
2859 record_unwind_protect (make_serial_process_unwind
, proc
);
2860 p
= XPROCESS (proc
);
2862 fd
= serial_open ((char*) SDATA (port
));
2865 if (fd
> max_process_desc
)
2866 max_process_desc
= fd
;
2867 chan_process
[fd
] = proc
;
2869 buffer
= Fplist_get (contact
, QCbuffer
);
2872 buffer
= Fget_buffer_create (buffer
);
2875 p
->childp
= contact
;
2876 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2878 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2879 p
->filter
= Fplist_get (contact
, QCfilter
);
2881 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2882 p
->kill_without_query
= 1;
2883 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2887 if (!EQ (p
->command
, Qt
))
2889 FD_SET (fd
, &input_wait_mask
);
2890 FD_SET (fd
, &non_keyboard_wait_mask
);
2893 if (BUFFERP (buffer
))
2895 set_marker_both (p
->mark
, buffer
,
2896 BUF_ZV (XBUFFER (buffer
)),
2897 BUF_ZV_BYTE (XBUFFER (buffer
)));
2900 tem
= Fplist_member (contact
, QCcoding
);
2901 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2907 val
= XCAR (XCDR (tem
));
2911 else if (!NILP (Vcoding_system_for_read
))
2912 val
= Vcoding_system_for_read
;
2913 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2914 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2916 p
->decode_coding_system
= val
;
2921 val
= XCAR (XCDR (tem
));
2925 else if (!NILP (Vcoding_system_for_write
))
2926 val
= Vcoding_system_for_write
;
2927 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2928 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2930 p
->encode_coding_system
= val
;
2932 setup_process_coding_systems (proc
);
2933 p
->decoding_buf
= make_uninit_string (0);
2934 p
->decoding_carryover
= 0;
2935 p
->encoding_buf
= make_uninit_string (0);
2936 p
->inherit_coding_system_flag
2937 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2939 Fserial_process_configure (nargs
, args
);
2941 specpdl_ptr
= specpdl
+ specpdl_count
;
2947 /* Create a network stream/datagram client/server process. Treated
2948 exactly like a normal process when reading and writing. Primary
2949 differences are in status display and process deletion. A network
2950 connection has no PID; you cannot signal it. All you can do is
2951 stop/continue it and deactivate/close it via delete-process */
2953 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2955 doc
: /* Create and return a network server or client process.
2957 In Emacs, network connections are represented by process objects, so
2958 input and output work as for subprocesses and `delete-process' closes
2959 a network connection. However, a network process has no process id,
2960 it cannot be signaled, and the status codes are different from normal
2963 Arguments are specified as keyword/argument pairs. The following
2964 arguments are defined:
2966 :name NAME -- NAME is name for process. It is modified if necessary
2969 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2970 with the process. Process output goes at end of that buffer, unless
2971 you specify an output stream or filter function to handle the output.
2972 BUFFER may be also nil, meaning that this process is not associated
2975 :host HOST -- HOST is name of the host to connect to, or its IP
2976 address. The symbol `local' specifies the local host. If specified
2977 for a server process, it must be a valid name or address for the local
2978 host, and only clients connecting to that address will be accepted.
2980 :service SERVICE -- SERVICE is name of the service desired, or an
2981 integer specifying a port number to connect to. If SERVICE is t,
2982 a random port number is selected for the server. (If Emacs was
2983 compiled with getaddrinfo, a port number can also be specified as a
2984 string, e.g. "80", as well as an integer. This is not portable.)
2986 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2987 stream type connection, `datagram' creates a datagram type connection,
2988 `seqpacket' creates a reliable datagram connection.
2990 :family FAMILY -- FAMILY is the address (and protocol) family for the
2991 service specified by HOST and SERVICE. The default (nil) is to use
2992 whatever address family (IPv4 or IPv6) that is defined for the host
2993 and port number specified by HOST and SERVICE. Other address families
2995 local -- for a local (i.e. UNIX) address specified by SERVICE.
2996 ipv4 -- use IPv4 address family only.
2997 ipv6 -- use IPv6 address family only.
2999 :local ADDRESS -- ADDRESS is the local address used for the connection.
3000 This parameter is ignored when opening a client process. When specified
3001 for a server process, the FAMILY, HOST and SERVICE args are ignored.
3003 :remote ADDRESS -- ADDRESS is the remote partner's address for the
3004 connection. This parameter is ignored when opening a stream server
3005 process. For a datagram server process, it specifies the initial
3006 setting of the remote datagram address. When specified for a client
3007 process, the FAMILY, HOST, and SERVICE args are ignored.
3009 The format of ADDRESS depends on the address family:
3010 - An IPv4 address is represented as an vector of integers [A B C D P]
3011 corresponding to numeric IP address A.B.C.D and port number P.
3012 - A local address is represented as a string with the address in the
3013 local address space.
3014 - An "unsupported family" address is represented by a cons (F . AV)
3015 where F is the family number and AV is a vector containing the socket
3016 address data with one element per address data byte. Do not rely on
3017 this format in portable code, as it may depend on implementation
3018 defined constants, data sizes, and data structure alignment.
3020 :coding CODING -- If CODING is a symbol, it specifies the coding
3021 system used for both reading and writing for this process. If CODING
3022 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3023 ENCODING is used for writing.
3025 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3026 return without waiting for the connection to complete; instead, the
3027 sentinel function will be called with second arg matching "open" (if
3028 successful) or "failed" when the connect completes. Default is to use
3029 a blocking connect (i.e. wait) for stream type connections.
3031 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3032 running when Emacs is exited.
3034 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3035 In the stopped state, a server process does not accept new
3036 connections, and a client process does not handle incoming traffic.
3037 The stopped state is cleared by `continue-process' and set by
3040 :filter FILTER -- Install FILTER as the process filter.
3042 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3043 process filter are multibyte, otherwise they are unibyte.
3044 If this keyword is not specified, the strings are multibyte if
3045 `default-enable-multibyte-characters' is non-nil.
3047 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3049 :log LOG -- Install LOG as the server process log function. This
3050 function is called when the server accepts a network connection from a
3051 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3052 is the server process, CLIENT is the new process for the connection,
3053 and MESSAGE is a string.
3055 :plist PLIST -- Install PLIST as the new process' initial plist.
3057 :server QLEN -- if QLEN is non-nil, create a server process for the
3058 specified FAMILY, SERVICE, and connection type (stream or datagram).
3059 If QLEN is an integer, it is used as the max. length of the server's
3060 pending connection queue (also known as the backlog); the default
3061 queue length is 5. Default is to create a client process.
3063 The following network options can be specified for this connection:
3065 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3066 :dontroute BOOL -- Only send to directly connected hosts.
3067 :keepalive BOOL -- Send keep-alive messages on network stream.
3068 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3069 :oobinline BOOL -- Place out-of-band data in receive data stream.
3070 :priority INT -- Set protocol defined priority for sent packets.
3071 :reuseaddr BOOL -- Allow reusing a recently used local address
3072 (this is allowed by default for a server process).
3073 :bindtodevice NAME -- bind to interface NAME. Using this may require
3074 special privileges on some systems.
3076 Consult the relevant system programmer's manual pages for more
3077 information on using these options.
3080 A server process will listen for and accept connections from clients.
3081 When a client connection is accepted, a new network process is created
3082 for the connection with the following parameters:
3084 - The client's process name is constructed by concatenating the server
3085 process' NAME and a client identification string.
3086 - If the FILTER argument is non-nil, the client process will not get a
3087 separate process buffer; otherwise, the client's process buffer is a newly
3088 created buffer named after the server process' BUFFER name or process
3089 NAME concatenated with the client identification string.
3090 - The connection type and the process filter and sentinel parameters are
3091 inherited from the server process' TYPE, FILTER and SENTINEL.
3092 - The client process' contact info is set according to the client's
3093 addressing information (typically an IP address and a port number).
3094 - The client process' plist is initialized from the server's plist.
3096 Notice that the FILTER and SENTINEL args are never used directly by
3097 the server process. Also, the BUFFER argument is not used directly by
3098 the server process, but via the optional :log function, accepted (and
3099 failed) connections may be logged in the server process' buffer.
3101 The original argument list, modified with the actual connection
3102 information, is available via the `process-contact' function.
3104 usage: (make-network-process &rest ARGS) */)
3105 (int nargs
, Lisp_Object
*args
)
3108 Lisp_Object contact
;
3109 struct Lisp_Process
*p
;
3110 #ifdef HAVE_GETADDRINFO
3111 struct addrinfo ai
, *res
, *lres
;
3112 struct addrinfo hints
;
3113 const char *portstring
;
3115 #else /* HAVE_GETADDRINFO */
3116 struct _emacs_addrinfo
3122 struct sockaddr
*ai_addr
;
3123 struct _emacs_addrinfo
*ai_next
;
3125 #endif /* HAVE_GETADDRINFO */
3126 struct sockaddr_in address_in
;
3127 #ifdef HAVE_LOCAL_SOCKETS
3128 struct sockaddr_un address_un
;
3133 int s
= -1, outch
, inch
;
3134 struct gcpro gcpro1
;
3135 int count
= SPECPDL_INDEX ();
3137 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3139 Lisp_Object name
, buffer
, host
, service
, address
;
3140 Lisp_Object filter
, sentinel
;
3141 int is_non_blocking_client
= 0;
3142 int is_server
= 0, backlog
= 5;
3149 /* Save arguments for process-contact and clone-process. */
3150 contact
= Flist (nargs
, args
);
3154 /* Ensure socket support is loaded if available. */
3155 init_winsock (TRUE
);
3158 /* :type TYPE (nil: stream, datagram */
3159 tem
= Fplist_get (contact
, QCtype
);
3161 socktype
= SOCK_STREAM
;
3162 #ifdef DATAGRAM_SOCKETS
3163 else if (EQ (tem
, Qdatagram
))
3164 socktype
= SOCK_DGRAM
;
3166 #ifdef HAVE_SEQPACKET
3167 else if (EQ (tem
, Qseqpacket
))
3168 socktype
= SOCK_SEQPACKET
;
3171 error ("Unsupported connection type");
3174 tem
= Fplist_get (contact
, QCserver
);
3177 /* Don't support network sockets when non-blocking mode is
3178 not available, since a blocked Emacs is not useful. */
3179 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3180 error ("Network servers not supported");
3184 backlog
= XINT (tem
);
3188 /* Make QCaddress an alias for :local (server) or :remote (client). */
3189 QCaddress
= is_server
? QClocal
: QCremote
;
3192 if (!is_server
&& socktype
!= SOCK_DGRAM
3193 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3195 #ifndef NON_BLOCKING_CONNECT
3196 error ("Non-blocking connect not supported");
3198 is_non_blocking_client
= 1;
3202 name
= Fplist_get (contact
, QCname
);
3203 buffer
= Fplist_get (contact
, QCbuffer
);
3204 filter
= Fplist_get (contact
, QCfilter
);
3205 sentinel
= Fplist_get (contact
, QCsentinel
);
3207 CHECK_STRING (name
);
3209 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3210 ai
.ai_socktype
= socktype
;
3215 /* :local ADDRESS or :remote ADDRESS */
3216 address
= Fplist_get (contact
, QCaddress
);
3217 if (!NILP (address
))
3219 host
= service
= Qnil
;
3221 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3222 error ("Malformed :address");
3223 ai
.ai_family
= family
;
3224 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3225 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3229 /* :family FAMILY -- nil (for Inet), local, or integer. */
3230 tem
= Fplist_get (contact
, QCfamily
);
3233 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3239 #ifdef HAVE_LOCAL_SOCKETS
3240 else if (EQ (tem
, Qlocal
))
3244 else if (EQ (tem
, Qipv6
))
3247 else if (EQ (tem
, Qipv4
))
3249 else if (INTEGERP (tem
))
3250 family
= XINT (tem
);
3252 error ("Unknown address family");
3254 ai
.ai_family
= family
;
3256 /* :service SERVICE -- string, integer (port number), or t (random port). */
3257 service
= Fplist_get (contact
, QCservice
);
3259 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3260 host
= Fplist_get (contact
, QChost
);
3263 if (EQ (host
, Qlocal
))
3264 /* Depending on setup, "localhost" may map to different IPv4 and/or
3265 IPv6 addresses, so it's better to be explicit. (Bug#6781) */
3266 host
= build_string ("127.0.0.1");
3267 CHECK_STRING (host
);
3270 #ifdef HAVE_LOCAL_SOCKETS
3271 if (family
== AF_LOCAL
)
3275 message (":family local ignores the :host \"%s\" property",
3277 contact
= Fplist_put (contact
, QChost
, Qnil
);
3280 CHECK_STRING (service
);
3281 memset (&address_un
, 0, sizeof address_un
);
3282 address_un
.sun_family
= AF_LOCAL
;
3283 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3284 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3285 ai
.ai_addrlen
= sizeof address_un
;
3290 /* Slow down polling to every ten seconds.
3291 Some kernels have a bug which causes retrying connect to fail
3292 after a connect. Polling can interfere with gethostbyname too. */
3293 #ifdef POLL_FOR_INPUT
3294 if (socktype
!= SOCK_DGRAM
)
3296 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3297 bind_polling_period (10);
3301 #ifdef HAVE_GETADDRINFO
3302 /* If we have a host, use getaddrinfo to resolve both host and service.
3303 Otherwise, use getservbyname to lookup the service. */
3307 /* SERVICE can either be a string or int.
3308 Convert to a C string for later use by getaddrinfo. */
3309 if (EQ (service
, Qt
))
3311 else if (INTEGERP (service
))
3313 sprintf (portbuf
, "%ld", (long) XINT (service
));
3314 portstring
= portbuf
;
3318 CHECK_STRING (service
);
3319 portstring
= SDATA (service
);
3324 memset (&hints
, 0, sizeof (hints
));
3326 hints
.ai_family
= family
;
3327 hints
.ai_socktype
= socktype
;
3328 hints
.ai_protocol
= 0;
3330 #ifdef HAVE_RES_INIT
3334 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3336 #ifdef HAVE_GAI_STRERROR
3337 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror (ret
));
3339 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3345 #endif /* HAVE_GETADDRINFO */
3347 /* We end up here if getaddrinfo is not defined, or in case no hostname
3348 has been specified (e.g. for a local server process). */
3350 if (EQ (service
, Qt
))
3352 else if (INTEGERP (service
))
3353 port
= htons ((unsigned short) XINT (service
));
3356 struct servent
*svc_info
;
3357 CHECK_STRING (service
);
3358 svc_info
= getservbyname (SDATA (service
),
3359 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3361 error ("Unknown service: %s", SDATA (service
));
3362 port
= svc_info
->s_port
;
3365 memset (&address_in
, 0, sizeof address_in
);
3366 address_in
.sin_family
= family
;
3367 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3368 address_in
.sin_port
= port
;
3370 #ifndef HAVE_GETADDRINFO
3373 struct hostent
*host_info_ptr
;
3375 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3376 as it may `hang' Emacs for a very long time. */
3380 #ifdef HAVE_RES_INIT
3384 host_info_ptr
= gethostbyname (SDATA (host
));
3389 memcpy (&address_in
.sin_addr
, host_info_ptr
->h_addr
,
3390 host_info_ptr
->h_length
);
3391 family
= host_info_ptr
->h_addrtype
;
3392 address_in
.sin_family
= family
;
3395 /* Attempt to interpret host as numeric inet address */
3397 unsigned long numeric_addr
;
3398 numeric_addr
= inet_addr ((char *) SDATA (host
));
3399 if (numeric_addr
== -1)
3400 error ("Unknown host \"%s\"", SDATA (host
));
3402 memcpy (&address_in
.sin_addr
, &numeric_addr
,
3403 sizeof (address_in
.sin_addr
));
3407 #endif /* not HAVE_GETADDRINFO */
3409 ai
.ai_family
= family
;
3410 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3411 ai
.ai_addrlen
= sizeof address_in
;
3415 /* Do this in case we never enter the for-loop below. */
3416 count1
= SPECPDL_INDEX ();
3419 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3425 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3432 #ifdef DATAGRAM_SOCKETS
3433 if (!is_server
&& socktype
== SOCK_DGRAM
)
3435 #endif /* DATAGRAM_SOCKETS */
3437 #ifdef NON_BLOCKING_CONNECT
3438 if (is_non_blocking_client
)
3441 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3443 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3455 /* Make us close S if quit. */
3456 record_unwind_protect (close_file_unwind
, make_number (s
));
3458 /* Parse network options in the arg list.
3459 We simply ignore anything which isn't a known option (including other keywords).
3460 An error is signaled if setting a known option fails. */
3461 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3462 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3466 /* Configure as a server socket. */
3468 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3469 explicit :reuseaddr key to override this. */
3470 #ifdef HAVE_LOCAL_SOCKETS
3471 if (family
!= AF_LOCAL
)
3473 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3476 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3477 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3480 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3481 report_file_error ("Cannot bind server socket", Qnil
);
3483 #ifdef HAVE_GETSOCKNAME
3484 if (EQ (service
, Qt
))
3486 struct sockaddr_in sa1
;
3487 int len1
= sizeof (sa1
);
3488 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3490 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3491 service
= make_number (ntohs (sa1
.sin_port
));
3492 contact
= Fplist_put (contact
, QCservice
, service
);
3497 if (socktype
!= SOCK_DGRAM
&& listen (s
, backlog
))
3498 report_file_error ("Cannot listen on server socket", Qnil
);
3506 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3509 if (ret
== 0 || xerrno
== EISCONN
)
3511 /* The unwind-protect will be discarded afterwards.
3512 Likewise for immediate_quit. */
3516 #ifdef NON_BLOCKING_CONNECT
3518 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3522 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3529 if (xerrno
== EINTR
)
3531 /* Unlike most other syscalls connect() cannot be called
3532 again. (That would return EALREADY.) The proper way to
3533 wait for completion is select(). */
3540 sc
= select (s
+ 1, (SELECT_TYPE
*)0, &fdset
, (SELECT_TYPE
*)0,
3547 report_file_error ("select failed", Qnil
);
3551 len
= sizeof xerrno
;
3552 eassert (FD_ISSET (s
, &fdset
));
3553 if (getsockopt (s
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &len
) == -1)
3554 report_file_error ("getsockopt failed", Qnil
);
3556 errno
= xerrno
, report_file_error ("error during connect", Qnil
);
3560 #endif /* !WINDOWSNT */
3564 /* Discard the unwind protect closing S. */
3565 specpdl_ptr
= specpdl
+ count1
;
3570 if (xerrno
== EINTR
)
3577 #ifdef DATAGRAM_SOCKETS
3578 if (socktype
== SOCK_DGRAM
)
3580 if (datagram_address
[s
].sa
)
3582 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3583 datagram_address
[s
].len
= lres
->ai_addrlen
;
3587 memset (datagram_address
[s
].sa
, 0, lres
->ai_addrlen
);
3588 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3591 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3592 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3593 conv_lisp_to_sockaddr (rfamily
, remote
,
3594 datagram_address
[s
].sa
, rlen
);
3598 memcpy (datagram_address
[s
].sa
, lres
->ai_addr
, lres
->ai_addrlen
);
3601 contact
= Fplist_put (contact
, QCaddress
,
3602 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3603 #ifdef HAVE_GETSOCKNAME
3606 struct sockaddr_in sa1
;
3607 int len1
= sizeof (sa1
);
3608 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3609 contact
= Fplist_put (contact
, QClocal
,
3610 conv_sockaddr_to_lisp ((struct sockaddr
*)&sa1
, len1
));
3617 #ifdef HAVE_GETADDRINFO
3626 /* Discard the unwind protect for closing S, if any. */
3627 specpdl_ptr
= specpdl
+ count1
;
3629 /* Unwind bind_polling_period and request_sigio. */
3630 unbind_to (count
, Qnil
);
3634 /* If non-blocking got this far - and failed - assume non-blocking is
3635 not supported after all. This is probably a wrong assumption, but
3636 the normal blocking calls to open-network-stream handles this error
3638 if (is_non_blocking_client
)
3643 report_file_error ("make server process failed", contact
);
3645 report_file_error ("make client process failed", contact
);
3652 buffer
= Fget_buffer_create (buffer
);
3653 proc
= make_process (name
);
3655 chan_process
[inch
] = proc
;
3658 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3661 fcntl (inch
, F_SETFL
, O_NDELAY
);
3665 p
= XPROCESS (proc
);
3667 p
->childp
= contact
;
3668 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3672 p
->sentinel
= sentinel
;
3674 p
->log
= Fplist_get (contact
, QClog
);
3675 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3676 p
->kill_without_query
= 1;
3677 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3682 if (is_server
&& socktype
!= SOCK_DGRAM
)
3683 p
->status
= Qlisten
;
3685 /* Make the process marker point into the process buffer (if any). */
3686 if (BUFFERP (buffer
))
3687 set_marker_both (p
->mark
, buffer
,
3688 BUF_ZV (XBUFFER (buffer
)),
3689 BUF_ZV_BYTE (XBUFFER (buffer
)));
3691 #ifdef NON_BLOCKING_CONNECT
3692 if (is_non_blocking_client
)
3694 /* We may get here if connect did succeed immediately. However,
3695 in that case, we still need to signal this like a non-blocking
3697 p
->status
= Qconnect
;
3698 if (!FD_ISSET (inch
, &connect_wait_mask
))
3700 FD_SET (inch
, &connect_wait_mask
);
3701 FD_SET (inch
, &write_mask
);
3702 num_pending_connects
++;
3707 /* A server may have a client filter setting of Qt, but it must
3708 still listen for incoming connects unless it is stopped. */
3709 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3710 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3712 FD_SET (inch
, &input_wait_mask
);
3713 FD_SET (inch
, &non_keyboard_wait_mask
);
3716 if (inch
> max_process_desc
)
3717 max_process_desc
= inch
;
3719 tem
= Fplist_member (contact
, QCcoding
);
3720 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3721 tem
= Qnil
; /* No error message (too late!). */
3724 /* Setup coding systems for communicating with the network stream. */
3725 struct gcpro gcpro1
;
3726 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3727 Lisp_Object coding_systems
= Qt
;
3728 Lisp_Object args
[5], val
;
3732 val
= XCAR (XCDR (tem
));
3736 else if (!NILP (Vcoding_system_for_read
))
3737 val
= Vcoding_system_for_read
;
3738 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3739 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3740 /* We dare not decode end-of-line format by setting VAL to
3741 Qraw_text, because the existing Emacs Lisp libraries
3742 assume that they receive bare code including a sequene of
3747 if (NILP (host
) || NILP (service
))
3748 coding_systems
= Qnil
;
3751 args
[0] = Qopen_network_stream
, args
[1] = name
,
3752 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3754 coding_systems
= Ffind_operation_coding_system (5, args
);
3757 if (CONSP (coding_systems
))
3758 val
= XCAR (coding_systems
);
3759 else if (CONSP (Vdefault_process_coding_system
))
3760 val
= XCAR (Vdefault_process_coding_system
);
3764 p
->decode_coding_system
= val
;
3768 val
= XCAR (XCDR (tem
));
3772 else if (!NILP (Vcoding_system_for_write
))
3773 val
= Vcoding_system_for_write
;
3774 else if (NILP (current_buffer
->enable_multibyte_characters
))
3778 if (EQ (coding_systems
, Qt
))
3780 if (NILP (host
) || NILP (service
))
3781 coding_systems
= Qnil
;
3784 args
[0] = Qopen_network_stream
, args
[1] = name
,
3785 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3787 coding_systems
= Ffind_operation_coding_system (5, args
);
3791 if (CONSP (coding_systems
))
3792 val
= XCDR (coding_systems
);
3793 else if (CONSP (Vdefault_process_coding_system
))
3794 val
= XCDR (Vdefault_process_coding_system
);
3798 p
->encode_coding_system
= val
;
3800 setup_process_coding_systems (proc
);
3802 p
->decoding_buf
= make_uninit_string (0);
3803 p
->decoding_carryover
= 0;
3804 p
->encoding_buf
= make_uninit_string (0);
3806 p
->inherit_coding_system_flag
3807 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3814 #if defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3817 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3818 doc
: /* Return an alist of all network interfaces and their network address.
3819 Each element is a cons, the car of which is a string containing the
3820 interface name, and the cdr is the network address in internal
3821 format; see the description of ADDRESS in `make-network-process'. */)
3824 struct ifconf ifconf
;
3825 struct ifreq
*ifreqs
= NULL
;
3830 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3836 buf_size
= ifaces
* sizeof (ifreqs
[0]);
3837 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3844 ifconf
.ifc_len
= buf_size
;
3845 ifconf
.ifc_req
= ifreqs
;
3846 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3852 if (ifconf
.ifc_len
== buf_size
)
3856 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3859 while (--ifaces
>= 0)
3861 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3862 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3863 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3865 memcpy (namebuf
, ifq
->ifr_name
, sizeof (ifq
->ifr_name
));
3866 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3867 res
= Fcons (Fcons (build_string (namebuf
),
3868 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3869 sizeof (struct sockaddr
))),
3875 #endif /* SIOCGIFCONF */
3877 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3881 const char *flag_sym
;
3884 static const struct ifflag_def ifflag_table
[] = {
3888 #ifdef IFF_BROADCAST
3889 { IFF_BROADCAST
, "broadcast" },
3892 { IFF_DEBUG
, "debug" },
3895 { IFF_LOOPBACK
, "loopback" },
3897 #ifdef IFF_POINTOPOINT
3898 { IFF_POINTOPOINT
, "pointopoint" },
3901 { IFF_RUNNING
, "running" },
3904 { IFF_NOARP
, "noarp" },
3907 { IFF_PROMISC
, "promisc" },
3909 #ifdef IFF_NOTRAILERS
3910 { IFF_NOTRAILERS
, "notrailers" },
3913 { IFF_ALLMULTI
, "allmulti" },
3916 { IFF_MASTER
, "master" },
3919 { IFF_SLAVE
, "slave" },
3921 #ifdef IFF_MULTICAST
3922 { IFF_MULTICAST
, "multicast" },
3925 { IFF_PORTSEL
, "portsel" },
3927 #ifdef IFF_AUTOMEDIA
3928 { IFF_AUTOMEDIA
, "automedia" },
3931 { IFF_DYNAMIC
, "dynamic" },
3934 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3937 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3940 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3943 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3946 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3951 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3952 doc
: /* Return information about network interface named IFNAME.
3953 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3954 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3955 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3956 FLAGS is the current flags of the interface. */)
3957 (Lisp_Object ifname
)
3960 Lisp_Object res
= Qnil
;
3965 CHECK_STRING (ifname
);
3967 memset (rq
.ifr_name
, 0, sizeof rq
.ifr_name
);
3968 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3970 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3975 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3976 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3978 int flags
= rq
.ifr_flags
;
3979 const struct ifflag_def
*fp
;
3983 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3985 if (flags
& fp
->flag_bit
)
3987 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3988 flags
-= fp
->flag_bit
;
3991 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3993 if (flags
& (1 << fnum
))
3995 elt
= Fcons (make_number (fnum
), elt
);
4000 res
= Fcons (elt
, res
);
4003 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
4004 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
4006 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
4007 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
4011 for (n
= 0; n
< 6; n
++)
4012 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
4013 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
4016 res
= Fcons (elt
, res
);
4019 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4020 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4023 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4024 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4026 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4030 res
= Fcons (elt
, res
);
4033 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4034 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4037 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4040 res
= Fcons (elt
, res
);
4043 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4044 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4047 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4050 res
= Fcons (elt
, res
);
4054 return any
? res
: Qnil
;
4057 #endif /* defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H) */
4059 /* Turn off input and output for process PROC. */
4062 deactivate_process (Lisp_Object proc
)
4064 register int inchannel
, outchannel
;
4065 register struct Lisp_Process
*p
= XPROCESS (proc
);
4067 inchannel
= p
->infd
;
4068 outchannel
= p
->outfd
;
4070 #ifdef ADAPTIVE_READ_BUFFERING
4071 if (p
->read_output_delay
> 0)
4073 if (--process_output_delay_count
< 0)
4074 process_output_delay_count
= 0;
4075 p
->read_output_delay
= 0;
4076 p
->read_output_skip
= 0;
4082 /* Beware SIGCHLD hereabouts. */
4083 flush_pending_output (inchannel
);
4084 emacs_close (inchannel
);
4085 if (outchannel
>= 0 && outchannel
!= inchannel
)
4086 emacs_close (outchannel
);
4090 #ifdef DATAGRAM_SOCKETS
4091 if (DATAGRAM_CHAN_P (inchannel
))
4093 xfree (datagram_address
[inchannel
].sa
);
4094 datagram_address
[inchannel
].sa
= 0;
4095 datagram_address
[inchannel
].len
= 0;
4098 chan_process
[inchannel
] = Qnil
;
4099 FD_CLR (inchannel
, &input_wait_mask
);
4100 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4101 #ifdef NON_BLOCKING_CONNECT
4102 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4104 FD_CLR (inchannel
, &connect_wait_mask
);
4105 FD_CLR (inchannel
, &write_mask
);
4106 if (--num_pending_connects
< 0)
4110 if (inchannel
== max_process_desc
)
4113 /* We just closed the highest-numbered process input descriptor,
4114 so recompute the highest-numbered one now. */
4115 max_process_desc
= 0;
4116 for (i
= 0; i
< MAXDESC
; i
++)
4117 if (!NILP (chan_process
[i
]))
4118 max_process_desc
= i
;
4124 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4126 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4127 It is read into the process' buffers or given to their filter functions.
4128 Non-nil arg PROCESS means do not return until some output has been received
4131 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4132 and milliseconds to wait; return after that much time whether or not
4133 there is any subprocess output. If SECONDS is a floating point number,
4134 it specifies a fractional number of seconds to wait.
4135 The MILLISEC argument is obsolete and should be avoided.
4137 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4138 from PROCESS, suspending reading output from other processes.
4139 If JUST-THIS-ONE is an integer, don't run any timers either.
4140 Return non-nil if we received any output before the timeout expired. */)
4141 (register Lisp_Object process
, Lisp_Object seconds
, Lisp_Object millisec
, Lisp_Object just_this_one
)
4143 int secs
, usecs
= 0;
4145 if (! NILP (process
))
4146 CHECK_PROCESS (process
);
4148 just_this_one
= Qnil
;
4150 if (!NILP (millisec
))
4151 { /* Obsolete calling convention using integers rather than floats. */
4152 CHECK_NUMBER (millisec
);
4154 seconds
= make_float (XINT (millisec
) / 1000.0);
4157 CHECK_NUMBER (seconds
);
4158 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4162 if (!NILP (seconds
))
4164 if (INTEGERP (seconds
))
4165 secs
= XINT (seconds
);
4166 else if (FLOATP (seconds
))
4168 double timeout
= XFLOAT_DATA (seconds
);
4169 secs
= (int) timeout
;
4170 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4173 wrong_type_argument (Qnumberp
, seconds
);
4175 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4176 secs
= -1, usecs
= 0;
4179 secs
= NILP (process
) ? -1 : 0;
4182 (wait_reading_process_output (secs
, usecs
, 0, 0,
4184 !NILP (process
) ? XPROCESS (process
) : NULL
,
4185 NILP (just_this_one
) ? 0 :
4186 !INTEGERP (just_this_one
) ? 1 : -1)
4190 /* Accept a connection for server process SERVER on CHANNEL. */
4192 static int connect_counter
= 0;
4195 server_accept_connection (Lisp_Object server
, int channel
)
4197 Lisp_Object proc
, caller
, name
, buffer
;
4198 Lisp_Object contact
, host
, service
;
4199 struct Lisp_Process
*ps
= XPROCESS (server
);
4200 struct Lisp_Process
*p
;
4204 struct sockaddr_in in
;
4206 struct sockaddr_in6 in6
;
4208 #ifdef HAVE_LOCAL_SOCKETS
4209 struct sockaddr_un un
;
4212 int len
= sizeof saddr
;
4214 s
= accept (channel
, &saddr
.sa
, &len
);
4223 if (code
== EWOULDBLOCK
)
4227 if (!NILP (ps
->log
))
4228 call3 (ps
->log
, server
, Qnil
,
4229 concat3 (build_string ("accept failed with code"),
4230 Fnumber_to_string (make_number (code
)),
4231 build_string ("\n")));
4237 /* Setup a new process to handle the connection. */
4239 /* Generate a unique identification of the caller, and build contact
4240 information for this process. */
4243 switch (saddr
.sa
.sa_family
)
4247 Lisp_Object args
[5];
4248 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4249 args
[0] = build_string ("%d.%d.%d.%d");
4250 args
[1] = make_number (*ip
++);
4251 args
[2] = make_number (*ip
++);
4252 args
[3] = make_number (*ip
++);
4253 args
[4] = make_number (*ip
++);
4254 host
= Fformat (5, args
);
4255 service
= make_number (ntohs (saddr
.in
.sin_port
));
4257 args
[0] = build_string (" <%s:%d>");
4260 caller
= Fformat (3, args
);
4267 Lisp_Object args
[9];
4268 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4270 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4271 for (i
= 0; i
< 8; i
++)
4272 args
[i
+1] = make_number (ntohs (ip6
[i
]));
4273 host
= Fformat (9, args
);
4274 service
= make_number (ntohs (saddr
.in
.sin_port
));
4276 args
[0] = build_string (" <[%s]:%d>");
4279 caller
= Fformat (3, args
);
4284 #ifdef HAVE_LOCAL_SOCKETS
4288 caller
= Fnumber_to_string (make_number (connect_counter
));
4289 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4293 /* Create a new buffer name for this process if it doesn't have a
4294 filter. The new buffer name is based on the buffer name or
4295 process name of the server process concatenated with the caller
4298 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4302 buffer
= ps
->buffer
;
4304 buffer
= Fbuffer_name (buffer
);
4309 buffer
= concat2 (buffer
, caller
);
4310 buffer
= Fget_buffer_create (buffer
);
4314 /* Generate a unique name for the new server process. Combine the
4315 server process name with the caller identification. */
4317 name
= concat2 (ps
->name
, caller
);
4318 proc
= make_process (name
);
4320 chan_process
[s
] = proc
;
4323 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4326 fcntl (s
, F_SETFL
, O_NDELAY
);
4330 p
= XPROCESS (proc
);
4332 /* Build new contact information for this setup. */
4333 contact
= Fcopy_sequence (ps
->childp
);
4334 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4335 contact
= Fplist_put (contact
, QChost
, host
);
4336 if (!NILP (service
))
4337 contact
= Fplist_put (contact
, QCservice
, service
);
4338 contact
= Fplist_put (contact
, QCremote
,
4339 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4340 #ifdef HAVE_GETSOCKNAME
4342 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4343 contact
= Fplist_put (contact
, QClocal
,
4344 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4347 p
->childp
= contact
;
4348 p
->plist
= Fcopy_sequence (ps
->plist
);
4352 p
->sentinel
= ps
->sentinel
;
4353 p
->filter
= ps
->filter
;
4360 /* Client processes for accepted connections are not stopped initially. */
4361 if (!EQ (p
->filter
, Qt
))
4363 FD_SET (s
, &input_wait_mask
);
4364 FD_SET (s
, &non_keyboard_wait_mask
);
4367 if (s
> max_process_desc
)
4368 max_process_desc
= s
;
4370 /* Setup coding system for new process based on server process.
4371 This seems to be the proper thing to do, as the coding system
4372 of the new process should reflect the settings at the time the
4373 server socket was opened; not the current settings. */
4375 p
->decode_coding_system
= ps
->decode_coding_system
;
4376 p
->encode_coding_system
= ps
->encode_coding_system
;
4377 setup_process_coding_systems (proc
);
4379 p
->decoding_buf
= make_uninit_string (0);
4380 p
->decoding_carryover
= 0;
4381 p
->encoding_buf
= make_uninit_string (0);
4383 p
->inherit_coding_system_flag
4384 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4386 if (!NILP (ps
->log
))
4387 call3 (ps
->log
, server
, proc
,
4388 concat3 (build_string ("accept from "),
4389 (STRINGP (host
) ? host
: build_string ("-")),
4390 build_string ("\n")));
4392 if (!NILP (p
->sentinel
))
4393 exec_sentinel (proc
,
4394 concat3 (build_string ("open from "),
4395 (STRINGP (host
) ? host
: build_string ("-")),
4396 build_string ("\n")));
4399 /* This variable is different from waiting_for_input in keyboard.c.
4400 It is used to communicate to a lisp process-filter/sentinel (via the
4401 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4402 for user-input when that process-filter was called.
4403 waiting_for_input cannot be used as that is by definition 0 when
4404 lisp code is being evalled.
4405 This is also used in record_asynch_buffer_change.
4406 For that purpose, this must be 0
4407 when not inside wait_reading_process_output. */
4408 static int waiting_for_user_input_p
;
4411 wait_reading_process_output_unwind (Lisp_Object data
)
4413 waiting_for_user_input_p
= XINT (data
);
4417 /* This is here so breakpoints can be put on it. */
4419 wait_reading_process_output_1 (void)
4423 /* Use a wrapper around select to work around a bug in gdb 5.3.
4424 Normally, the wrapper is optimized away by inlining.
4426 If emacs is stopped inside select, the gdb backtrace doesn't
4427 show the function which called select, so it is practically
4428 impossible to step through wait_reading_process_output. */
4432 select_wrapper (int n
, fd_set
*rfd
, fd_set
*wfd
, fd_set
*xfd
, struct timeval
*tmo
)
4434 return select (n
, rfd
, wfd
, xfd
, tmo
);
4436 #define select select_wrapper
4439 /* Read and dispose of subprocess output while waiting for timeout to
4440 elapse and/or keyboard input to be available.
4443 timeout in seconds, or
4444 zero for no limit, or
4445 -1 means gobble data immediately available but don't wait for any.
4448 an additional duration to wait, measured in microseconds.
4449 If this is nonzero and time_limit is 0, then the timeout
4450 consists of MICROSECS only.
4452 READ_KBD is a lisp value:
4453 0 to ignore keyboard input, or
4454 1 to return when input is available, or
4455 -1 meaning caller will actually read the input, so don't throw to
4456 the quit handler, or
4458 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4459 output that arrives.
4461 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4462 (and gobble terminal input into the buffer if any arrives).
4464 If WAIT_PROC is specified, wait until something arrives from that
4465 process. The return value is true if we read some input from
4468 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4469 (suspending output from other processes). A negative value
4470 means don't run any timers either.
4472 If WAIT_PROC is specified, then the function returns true if we
4473 received input from that process before the timeout elapsed.
4474 Otherwise, return true if we received input from any process. */
4477 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
4479 Lisp_Object wait_for_cell
,
4480 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
4482 register int channel
, nfds
;
4483 SELECT_TYPE Available
;
4484 SELECT_TYPE Writeok
;
4486 int check_delay
, no_avail
;
4489 EMACS_TIME timeout
, end_time
;
4490 int wait_channel
= -1;
4491 int got_some_input
= 0;
4492 int count
= SPECPDL_INDEX ();
4494 FD_ZERO (&Available
);
4497 if (time_limit
== 0 && microsecs
== 0 && wait_proc
&& !NILP (Vinhibit_quit
)
4498 && !(CONSP (wait_proc
->status
) && EQ (XCAR (wait_proc
->status
), Qexit
)))
4499 message ("Blocking call to accept-process-output with quit inhibited!!");
4501 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4502 if (wait_proc
!= NULL
)
4503 wait_channel
= wait_proc
->infd
;
4505 record_unwind_protect (wait_reading_process_output_unwind
,
4506 make_number (waiting_for_user_input_p
));
4507 waiting_for_user_input_p
= read_kbd
;
4509 /* Since we may need to wait several times,
4510 compute the absolute time to return at. */
4511 if (time_limit
|| microsecs
)
4513 EMACS_GET_TIME (end_time
);
4514 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4515 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4520 int timeout_reduced_for_timers
= 0;
4522 /* If calling from keyboard input, do not quit
4523 since we want to return C-g as an input character.
4524 Otherwise, do pending quit if requested. */
4529 process_pending_signals ();
4532 /* Exit now if the cell we're waiting for became non-nil. */
4533 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4536 /* Compute time from now till when time limit is up */
4537 /* Exit if already run out */
4538 if (time_limit
== -1)
4540 /* -1 specified for timeout means
4541 gobble output available now
4542 but don't wait at all. */
4544 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4546 else if (time_limit
|| microsecs
)
4548 EMACS_GET_TIME (timeout
);
4549 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4550 if (EMACS_TIME_NEG_P (timeout
))
4555 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4558 /* Normally we run timers here.
4559 But not if wait_for_cell; in those cases,
4560 the wait is supposed to be short,
4561 and those callers cannot handle running arbitrary Lisp code here. */
4562 if (NILP (wait_for_cell
)
4563 && just_wait_proc
>= 0)
4565 EMACS_TIME timer_delay
;
4569 int old_timers_run
= timers_run
;
4570 struct buffer
*old_buffer
= current_buffer
;
4571 Lisp_Object old_window
= selected_window
;
4573 timer_delay
= timer_check (1);
4575 /* If a timer has run, this might have changed buffers
4576 an alike. Make read_key_sequence aware of that. */
4577 if (timers_run
!= old_timers_run
4578 && (old_buffer
!= current_buffer
4579 || !EQ (old_window
, selected_window
))
4580 && waiting_for_user_input_p
== -1)
4581 record_asynch_buffer_change ();
4583 if (timers_run
!= old_timers_run
&& do_display
)
4584 /* We must retry, since a timer may have requeued itself
4585 and that could alter the time_delay. */
4586 redisplay_preserve_echo_area (9);
4590 while (!detect_input_pending ());
4592 /* If there is unread keyboard input, also return. */
4594 && requeued_events_pending_p ())
4597 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4599 EMACS_TIME difference
;
4600 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4601 if (EMACS_TIME_NEG_P (difference
))
4603 timeout
= timer_delay
;
4604 timeout_reduced_for_timers
= 1;
4607 /* If time_limit is -1, we are not going to wait at all. */
4608 else if (time_limit
!= -1)
4610 /* This is so a breakpoint can be put here. */
4611 wait_reading_process_output_1 ();
4615 /* Cause C-g and alarm signals to take immediate action,
4616 and cause input available signals to zero out timeout.
4618 It is important that we do this before checking for process
4619 activity. If we get a SIGCHLD after the explicit checks for
4620 process activity, timeout is the only way we will know. */
4622 set_waiting_for_input (&timeout
);
4624 /* If status of something has changed, and no input is
4625 available, notify the user of the change right away. After
4626 this explicit check, we'll let the SIGCHLD handler zap
4627 timeout to get our attention. */
4628 if (update_tick
!= process_tick
)
4633 if (kbd_on_hold_p ())
4636 Atemp
= input_wait_mask
;
4639 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4640 if ((select (max (max_process_desc
, max_input_desc
) + 1,
4642 #ifdef NON_BLOCKING_CONNECT
4643 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4647 (SELECT_TYPE
*)0, &timeout
)
4650 /* It's okay for us to do this and then continue with
4651 the loop, since timeout has already been zeroed out. */
4652 clear_waiting_for_input ();
4653 status_notify (NULL
);
4654 if (do_display
) redisplay_preserve_echo_area (13);
4658 /* Don't wait for output from a non-running process. Just
4659 read whatever data has already been received. */
4660 if (wait_proc
&& wait_proc
->raw_status_new
)
4661 update_status (wait_proc
);
4663 && ! EQ (wait_proc
->status
, Qrun
)
4664 && ! EQ (wait_proc
->status
, Qconnect
))
4666 int nread
, total_nread
= 0;
4668 clear_waiting_for_input ();
4669 XSETPROCESS (proc
, wait_proc
);
4671 /* Read data from the process, until we exhaust it. */
4672 while (wait_proc
->infd
>= 0)
4674 nread
= read_process_output (proc
, wait_proc
->infd
);
4681 total_nread
+= nread
;
4685 else if (nread
== -1 && EIO
== errno
)
4689 else if (nread
== -1 && EAGAIN
== errno
)
4693 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4697 if (total_nread
> 0 && do_display
)
4698 redisplay_preserve_echo_area (10);
4703 /* Wait till there is something to do */
4705 if (wait_proc
&& just_wait_proc
)
4707 if (wait_proc
->infd
< 0) /* Terminated */
4709 FD_SET (wait_proc
->infd
, &Available
);
4713 else if (!NILP (wait_for_cell
))
4715 Available
= non_process_wait_mask
;
4722 Available
= non_keyboard_wait_mask
;
4724 Available
= input_wait_mask
;
4725 Writeok
= write_mask
;
4726 #ifdef SELECT_CANT_DO_WRITE_MASK
4731 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4734 /* If frame size has changed or the window is newly mapped,
4735 redisplay now, before we start to wait. There is a race
4736 condition here; if a SIGIO arrives between now and the select
4737 and indicates that a frame is trashed, the select may block
4738 displaying a trashed screen. */
4739 if (frame_garbaged
&& do_display
)
4741 clear_waiting_for_input ();
4742 redisplay_preserve_echo_area (11);
4744 set_waiting_for_input (&timeout
);
4748 if (read_kbd
&& detect_input_pending ())
4756 #ifdef ADAPTIVE_READ_BUFFERING
4757 /* Set the timeout for adaptive read buffering if any
4758 process has non-zero read_output_skip and non-zero
4759 read_output_delay, and we are not reading output for a
4760 specific wait_channel. It is not executed if
4761 Vprocess_adaptive_read_buffering is nil. */
4762 if (process_output_skip
&& check_delay
> 0)
4764 int usecs
= EMACS_USECS (timeout
);
4765 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4766 usecs
= READ_OUTPUT_DELAY_MAX
;
4767 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4769 proc
= chan_process
[channel
];
4772 /* Find minimum non-zero read_output_delay among the
4773 processes with non-zero read_output_skip. */
4774 if (XPROCESS (proc
)->read_output_delay
> 0)
4777 if (!XPROCESS (proc
)->read_output_skip
)
4779 FD_CLR (channel
, &Available
);
4780 XPROCESS (proc
)->read_output_skip
= 0;
4781 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4782 usecs
= XPROCESS (proc
)->read_output_delay
;
4785 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4786 process_output_skip
= 0;
4789 #if defined (USE_GTK) || defined (HAVE_GCONF)
4791 #elif defined (HAVE_NS)
4796 (max (max_process_desc
, max_input_desc
) + 1,
4798 (check_write
? &Writeok
: (SELECT_TYPE
*)0),
4799 (SELECT_TYPE
*)0, &timeout
);
4804 /* Make C-g and alarm signals set flags again */
4805 clear_waiting_for_input ();
4807 /* If we woke up due to SIGWINCH, actually change size now. */
4808 do_pending_window_change (0);
4810 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4811 /* We wanted the full specified time, so return now. */
4815 if (xerrno
== EINTR
)
4817 else if (xerrno
== EBADF
)
4820 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4821 the child's closure of the pts gives the parent a SIGHUP, and
4822 the ptc file descriptor is automatically closed,
4823 yielding EBADF here or at select() call above.
4824 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4825 in m/ibmrt-aix.h), and here we just ignore the select error.
4826 Cleanup occurs c/o status_notify after SIGCLD. */
4827 no_avail
= 1; /* Cannot depend on values returned */
4833 error ("select error: %s", emacs_strerror (xerrno
));
4838 FD_ZERO (&Available
);
4842 #if 0 /* When polling is used, interrupt_input is 0,
4843 so get_input_pending should read the input.
4844 So this should not be needed. */
4845 /* If we are using polling for input,
4846 and we see input available, make it get read now.
4847 Otherwise it might not actually get read for a second.
4848 And on hpux, since we turn off polling in wait_reading_process_output,
4849 it might never get read at all if we don't spend much time
4850 outside of wait_reading_process_output. */
4851 if (read_kbd
&& interrupt_input
4852 && keyboard_bit_set (&Available
)
4853 && input_polling_used ())
4854 kill (getpid (), SIGALRM
);
4857 /* Check for keyboard input */
4858 /* If there is any, return immediately
4859 to give it higher priority than subprocesses */
4863 int old_timers_run
= timers_run
;
4864 struct buffer
*old_buffer
= current_buffer
;
4865 Lisp_Object old_window
= selected_window
;
4868 if (detect_input_pending_run_timers (do_display
))
4870 swallow_events (do_display
);
4871 if (detect_input_pending_run_timers (do_display
))
4875 /* If a timer has run, this might have changed buffers
4876 an alike. Make read_key_sequence aware of that. */
4877 if (timers_run
!= old_timers_run
4878 && waiting_for_user_input_p
== -1
4879 && (old_buffer
!= current_buffer
4880 || !EQ (old_window
, selected_window
)))
4881 record_asynch_buffer_change ();
4887 /* If there is unread keyboard input, also return. */
4889 && requeued_events_pending_p ())
4892 /* If we are not checking for keyboard input now,
4893 do process events (but don't run any timers).
4894 This is so that X events will be processed.
4895 Otherwise they may have to wait until polling takes place.
4896 That would causes delays in pasting selections, for example.
4898 (We used to do this only if wait_for_cell.) */
4899 if (read_kbd
== 0 && detect_input_pending ())
4901 swallow_events (do_display
);
4902 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4903 if (detect_input_pending ())
4908 /* Exit now if the cell we're waiting for became non-nil. */
4909 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4913 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4914 go read it. This can happen with X on BSD after logging out.
4915 In that case, there really is no input and no SIGIO,
4916 but select says there is input. */
4918 if (read_kbd
&& interrupt_input
4919 && keyboard_bit_set (&Available
) && ! noninteractive
)
4920 kill (getpid (), SIGIO
);
4924 got_some_input
|= nfds
> 0;
4926 /* If checking input just got us a size-change event from X,
4927 obey it now if we should. */
4928 if (read_kbd
|| ! NILP (wait_for_cell
))
4929 do_pending_window_change (0);
4931 /* Check for data from a process. */
4932 if (no_avail
|| nfds
== 0)
4935 for (channel
= 0; channel
<= max_input_desc
; ++channel
)
4937 struct fd_callback_data
*d
= &fd_callback_info
[channel
];
4938 if (FD_ISSET (channel
, &Available
)
4940 && (d
->condition
& FOR_READ
) != 0)
4941 d
->func (channel
, d
->data
, 1);
4942 if (FD_ISSET (channel
, &write_mask
)
4944 && (d
->condition
& FOR_WRITE
) != 0)
4945 d
->func (channel
, d
->data
, 0);
4948 /* Really FIRST_PROC_DESC should be 0 on Unix,
4949 but this is safer in the short run. */
4950 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4952 if (FD_ISSET (channel
, &Available
)
4953 && FD_ISSET (channel
, &non_keyboard_wait_mask
)
4954 && !FD_ISSET (channel
, &non_process_wait_mask
))
4958 /* If waiting for this channel, arrange to return as
4959 soon as no more input to be processed. No more
4961 if (wait_channel
== channel
)
4967 proc
= chan_process
[channel
];
4971 /* If this is a server stream socket, accept connection. */
4972 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4974 server_accept_connection (proc
, channel
);
4978 /* Read data from the process, starting with our
4979 buffered-ahead character if we have one. */
4981 nread
= read_process_output (proc
, channel
);
4984 /* Since read_process_output can run a filter,
4985 which can call accept-process-output,
4986 don't try to read from any other processes
4987 before doing the select again. */
4988 FD_ZERO (&Available
);
4991 redisplay_preserve_echo_area (12);
4994 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4997 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4998 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
5000 else if (nread
== -1 && errno
== EAGAIN
)
5004 else if (nread
== -1 && errno
== EAGAIN
)
5006 /* Note that we cannot distinguish between no input
5007 available now and a closed pipe.
5008 With luck, a closed pipe will be accompanied by
5009 subprocess termination and SIGCHLD. */
5010 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5012 #endif /* O_NDELAY */
5013 #endif /* O_NONBLOCK */
5015 /* On some OSs with ptys, when the process on one end of
5016 a pty exits, the other end gets an error reading with
5017 errno = EIO instead of getting an EOF (0 bytes read).
5018 Therefore, if we get an error reading and errno =
5019 EIO, just continue, because the child process has
5020 exited and should clean itself up soon (e.g. when we
5023 However, it has been known to happen that the SIGCHLD
5024 got lost. So raise the signal again just in case.
5026 else if (nread
== -1 && errno
== EIO
)
5028 /* Clear the descriptor now, so we only raise the
5029 signal once. Don't do this if `process' is only
5031 if (XPROCESS (proc
)->pid
!= -2)
5033 FD_CLR (channel
, &input_wait_mask
);
5034 FD_CLR (channel
, &non_keyboard_wait_mask
);
5036 kill (getpid (), SIGCHLD
);
5039 #endif /* HAVE_PTYS */
5040 /* If we can detect process termination, don't consider the process
5041 gone just because its pipe is closed. */
5043 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5048 /* Preserve status of processes already terminated. */
5049 XPROCESS (proc
)->tick
= ++process_tick
;
5050 deactivate_process (proc
);
5051 if (XPROCESS (proc
)->raw_status_new
)
5052 update_status (XPROCESS (proc
));
5053 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5054 XPROCESS (proc
)->status
5055 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5058 #ifdef NON_BLOCKING_CONNECT
5059 if (FD_ISSET (channel
, &Writeok
)
5060 && FD_ISSET (channel
, &connect_wait_mask
))
5062 struct Lisp_Process
*p
;
5064 FD_CLR (channel
, &connect_wait_mask
);
5065 FD_CLR (channel
, &write_mask
);
5066 if (--num_pending_connects
< 0)
5069 proc
= chan_process
[channel
];
5073 p
= XPROCESS (proc
);
5076 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5077 So only use it on systems where it is known to work. */
5079 int xlen
= sizeof (xerrno
);
5080 if (getsockopt (channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5085 struct sockaddr pname
;
5086 int pnamelen
= sizeof (pname
);
5088 /* If connection failed, getpeername will fail. */
5090 if (getpeername (channel
, &pname
, &pnamelen
) < 0)
5092 /* Obtain connect failure code through error slippage. */
5095 if (errno
== ENOTCONN
&& read (channel
, &dummy
, 1) < 0)
5102 p
->tick
= ++process_tick
;
5103 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5104 deactivate_process (proc
);
5109 /* Execute the sentinel here. If we had relied on
5110 status_notify to do it later, it will read input
5111 from the process before calling the sentinel. */
5112 exec_sentinel (proc
, build_string ("open\n"));
5113 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5115 FD_SET (p
->infd
, &input_wait_mask
);
5116 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5120 #endif /* NON_BLOCKING_CONNECT */
5121 } /* end for each file descriptor */
5122 } /* end while exit conditions not met */
5124 unbind_to (count
, Qnil
);
5126 /* If calling from keyboard input, do not quit
5127 since we want to return C-g as an input character.
5128 Otherwise, do pending quit if requested. */
5131 /* Prevent input_pending from remaining set if we quit. */
5132 clear_input_pending ();
5136 return got_some_input
;
5139 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5142 read_process_output_call (Lisp_Object fun_and_args
)
5144 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5148 read_process_output_error_handler (Lisp_Object error
)
5150 cmd_error_internal (error
, "error in process filter: ");
5152 update_echo_area ();
5153 Fsleep_for (make_number (2), Qnil
);
5157 /* Read pending output from the process channel,
5158 starting with our buffered-ahead character if we have one.
5159 Yield number of decoded characters read.
5161 This function reads at most 4096 characters.
5162 If you want to read all available subprocess output,
5163 you must call it repeatedly until it returns zero.
5165 The characters read are decoded according to PROC's coding-system
5169 read_process_output (Lisp_Object proc
, register int channel
)
5171 register int nbytes
;
5173 register Lisp_Object outstream
;
5174 register struct Lisp_Process
*p
= XPROCESS (proc
);
5175 register EMACS_INT opoint
;
5176 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5177 int carryover
= p
->decoding_carryover
;
5179 int count
= SPECPDL_INDEX ();
5180 Lisp_Object odeactivate
;
5182 chars
= (char *) alloca (carryover
+ readmax
);
5184 /* See the comment above. */
5185 memcpy (chars
, SDATA (p
->decoding_buf
), carryover
);
5187 #ifdef DATAGRAM_SOCKETS
5188 /* We have a working select, so proc_buffered_char is always -1. */
5189 if (DATAGRAM_CHAN_P (channel
))
5191 int len
= datagram_address
[channel
].len
;
5192 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5193 0, datagram_address
[channel
].sa
, &len
);
5197 if (proc_buffered_char
[channel
] < 0)
5200 if (XPROCESS (proc
)->gnutls_p
)
5201 nbytes
= emacs_gnutls_read (channel
, XPROCESS (proc
),
5202 chars
+ carryover
, readmax
);
5205 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5206 #ifdef ADAPTIVE_READ_BUFFERING
5207 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5209 int delay
= p
->read_output_delay
;
5212 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5215 process_output_delay_count
++;
5216 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5219 else if (delay
> 0 && (nbytes
== readmax
))
5221 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5223 process_output_delay_count
--;
5225 p
->read_output_delay
= delay
;
5228 p
->read_output_skip
= 1;
5229 process_output_skip
= 1;
5236 chars
[carryover
] = proc_buffered_char
[channel
];
5237 proc_buffered_char
[channel
] = -1;
5239 if (XPROCESS (proc
)->gnutls_p
)
5240 nbytes
= emacs_gnutls_read (channel
, XPROCESS (proc
),
5241 chars
+ carryover
+ 1, readmax
- 1);
5244 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5248 nbytes
= nbytes
+ 1;
5251 p
->decoding_carryover
= 0;
5253 /* At this point, NBYTES holds number of bytes just received
5254 (including the one in proc_buffered_char[channel]). */
5257 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5259 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5262 /* Now set NBYTES how many bytes we must decode. */
5263 nbytes
+= carryover
;
5265 odeactivate
= Vdeactivate_mark
;
5266 /* There's no good reason to let process filters change the current
5267 buffer, and many callers of accept-process-output, sit-for, and
5268 friends don't expect current-buffer to be changed from under them. */
5269 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
5271 /* Read and dispose of the process output. */
5272 outstream
= p
->filter
;
5273 if (!NILP (outstream
))
5275 Lisp_Object obuffer
, okeymap
;
5277 int outer_running_asynch_code
= running_asynch_code
;
5278 int waiting
= waiting_for_user_input_p
;
5280 /* No need to gcpro these, because all we do with them later
5281 is test them for EQness, and none of them should be a string. */
5282 XSETBUFFER (obuffer
, current_buffer
);
5283 okeymap
= current_buffer
->keymap
;
5285 /* We inhibit quit here instead of just catching it so that
5286 hitting ^G when a filter happens to be running won't screw
5288 specbind (Qinhibit_quit
, Qt
);
5289 specbind (Qlast_nonmenu_event
, Qt
);
5291 /* In case we get recursively called,
5292 and we already saved the match data nonrecursively,
5293 save the same match data in safely recursive fashion. */
5294 if (outer_running_asynch_code
)
5297 /* Don't clobber the CURRENT match data, either! */
5298 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5299 restore_search_regs ();
5300 record_unwind_save_match_data ();
5301 Fset_match_data (tem
, Qt
);
5304 /* For speed, if a search happens within this code,
5305 save the match data in a special nonrecursive fashion. */
5306 running_asynch_code
= 1;
5308 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5309 text
= coding
->dst_object
;
5310 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5311 /* A new coding system might be found. */
5312 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5314 p
->decode_coding_system
= Vlast_coding_system_used
;
5316 /* Don't call setup_coding_system for
5317 proc_decode_coding_system[channel] here. It is done in
5318 detect_coding called via decode_coding above. */
5320 /* If a coding system for encoding is not yet decided, we set
5321 it as the same as coding-system for decoding.
5323 But, before doing that we must check if
5324 proc_encode_coding_system[p->outfd] surely points to a
5325 valid memory because p->outfd will be changed once EOF is
5326 sent to the process. */
5327 if (NILP (p
->encode_coding_system
)
5328 && proc_encode_coding_system
[p
->outfd
])
5330 p
->encode_coding_system
5331 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5332 setup_coding_system (p
->encode_coding_system
,
5333 proc_encode_coding_system
[p
->outfd
]);
5337 if (coding
->carryover_bytes
> 0)
5339 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5340 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5341 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5342 coding
->carryover_bytes
);
5343 p
->decoding_carryover
= coding
->carryover_bytes
;
5345 if (SBYTES (text
) > 0)
5346 internal_condition_case_1 (read_process_output_call
,
5348 Fcons (proc
, Fcons (text
, Qnil
))),
5349 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5350 read_process_output_error_handler
);
5352 /* If we saved the match data nonrecursively, restore it now. */
5353 restore_search_regs ();
5354 running_asynch_code
= outer_running_asynch_code
;
5356 /* Restore waiting_for_user_input_p as it was
5357 when we were called, in case the filter clobbered it. */
5358 waiting_for_user_input_p
= waiting
;
5360 #if 0 /* Call record_asynch_buffer_change unconditionally,
5361 because we might have changed minor modes or other things
5362 that affect key bindings. */
5363 if (! EQ (Fcurrent_buffer (), obuffer
)
5364 || ! EQ (current_buffer
->keymap
, okeymap
))
5366 /* But do it only if the caller is actually going to read events.
5367 Otherwise there's no need to make him wake up, and it could
5368 cause trouble (for example it would make sit_for return). */
5369 if (waiting_for_user_input_p
== -1)
5370 record_asynch_buffer_change ();
5373 /* If no filter, write into buffer if it isn't dead. */
5374 else if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5376 Lisp_Object old_read_only
;
5377 EMACS_INT old_begv
, old_zv
;
5378 EMACS_INT old_begv_byte
, old_zv_byte
;
5379 EMACS_INT before
, before_byte
;
5380 EMACS_INT opoint_byte
;
5384 Fset_buffer (p
->buffer
);
5386 opoint_byte
= PT_BYTE
;
5387 old_read_only
= current_buffer
->read_only
;
5390 old_begv_byte
= BEGV_BYTE
;
5391 old_zv_byte
= ZV_BYTE
;
5393 current_buffer
->read_only
= Qnil
;
5395 /* Insert new output into buffer
5396 at the current end-of-output marker,
5397 thus preserving logical ordering of input and output. */
5398 if (XMARKER (p
->mark
)->buffer
)
5399 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5400 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5403 SET_PT_BOTH (ZV
, ZV_BYTE
);
5405 before_byte
= PT_BYTE
;
5407 /* If the output marker is outside of the visible region, save
5408 the restriction and widen. */
5409 if (! (BEGV
<= PT
&& PT
<= ZV
))
5412 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5413 text
= coding
->dst_object
;
5414 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5415 /* A new coding system might be found. See the comment in the
5416 similar code in the previous `if' block. */
5417 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5419 p
->decode_coding_system
= Vlast_coding_system_used
;
5420 if (NILP (p
->encode_coding_system
)
5421 && proc_encode_coding_system
[p
->outfd
])
5423 p
->encode_coding_system
5424 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5425 setup_coding_system (p
->encode_coding_system
,
5426 proc_encode_coding_system
[p
->outfd
]);
5429 if (coding
->carryover_bytes
> 0)
5431 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5432 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5433 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5434 coding
->carryover_bytes
);
5435 p
->decoding_carryover
= coding
->carryover_bytes
;
5437 /* Adjust the multibyteness of TEXT to that of the buffer. */
5438 if (NILP (current_buffer
->enable_multibyte_characters
)
5439 != ! STRING_MULTIBYTE (text
))
5440 text
= (STRING_MULTIBYTE (text
)
5441 ? Fstring_as_unibyte (text
)
5442 : Fstring_to_multibyte (text
));
5443 /* Insert before markers in case we are inserting where
5444 the buffer's mark is, and the user's next command is Meta-y. */
5445 insert_from_string_before_markers (text
, 0, 0,
5446 SCHARS (text
), SBYTES (text
), 0);
5448 /* Make sure the process marker's position is valid when the
5449 process buffer is changed in the signal_after_change above.
5450 W3 is known to do that. */
5451 if (BUFFERP (p
->buffer
)
5452 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5453 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5455 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5457 update_mode_lines
++;
5459 /* Make sure opoint and the old restrictions
5460 float ahead of any new text just as point would. */
5461 if (opoint
>= before
)
5463 opoint
+= PT
- before
;
5464 opoint_byte
+= PT_BYTE
- before_byte
;
5466 if (old_begv
> before
)
5468 old_begv
+= PT
- before
;
5469 old_begv_byte
+= PT_BYTE
- before_byte
;
5471 if (old_zv
>= before
)
5473 old_zv
+= PT
- before
;
5474 old_zv_byte
+= PT_BYTE
- before_byte
;
5477 /* If the restriction isn't what it should be, set it. */
5478 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5479 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5482 current_buffer
->read_only
= old_read_only
;
5483 SET_PT_BOTH (opoint
, opoint_byte
);
5485 /* Handling the process output should not deactivate the mark. */
5486 Vdeactivate_mark
= odeactivate
;
5488 unbind_to (count
, Qnil
);
5492 /* Sending data to subprocess */
5494 jmp_buf send_process_frame
;
5495 Lisp_Object process_sent_to
;
5498 send_process_trap (int ignore
)
5500 SIGNAL_THREAD_CHECK (SIGPIPE
);
5501 sigunblock (sigmask (SIGPIPE
));
5502 longjmp (send_process_frame
, 1);
5505 /* Send some data to process PROC.
5506 BUF is the beginning of the data; LEN is the number of characters.
5507 OBJECT is the Lisp object that the data comes from. If OBJECT is
5508 nil or t, it means that the data comes from C string.
5510 If OBJECT is not nil, the data is encoded by PROC's coding-system
5511 for encoding before it is sent.
5513 This function can evaluate Lisp code and can garbage collect. */
5516 send_process (volatile Lisp_Object proc
, const unsigned char *volatile buf
,
5517 volatile EMACS_INT len
, volatile Lisp_Object object
)
5519 /* Use volatile to protect variables from being clobbered by longjmp. */
5520 struct Lisp_Process
*p
= XPROCESS (proc
);
5522 struct coding_system
*coding
;
5523 struct gcpro gcpro1
;
5524 SIGTYPE (*volatile old_sigpipe
) (int);
5528 if (p
->raw_status_new
)
5530 if (! EQ (p
->status
, Qrun
))
5531 error ("Process %s not running", SDATA (p
->name
));
5533 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5535 coding
= proc_encode_coding_system
[p
->outfd
];
5536 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5538 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5539 || (BUFFERP (object
)
5540 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5543 p
->encode_coding_system
5544 = complement_process_encoding_system (p
->encode_coding_system
);
5545 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5547 /* The coding system for encoding was changed to raw-text
5548 because we sent a unibyte text previously. Now we are
5549 sending a multibyte text, thus we must encode it by the
5550 original coding system specified for the current process.
5552 Another reason we comming here is that the coding system
5553 was just complemented and new one was returned by
5554 complement_process_encoding_system. */
5555 setup_coding_system (p
->encode_coding_system
, coding
);
5556 Vlast_coding_system_used
= p
->encode_coding_system
;
5558 coding
->src_multibyte
= 1;
5562 /* For sending a unibyte text, character code conversion should
5563 not take place but EOL conversion should. So, setup raw-text
5564 or one of the subsidiary if we have not yet done it. */
5565 if (CODING_REQUIRE_ENCODING (coding
))
5567 if (CODING_REQUIRE_FLUSHING (coding
))
5569 /* But, before changing the coding, we must flush out data. */
5570 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5571 send_process (proc
, "", 0, Qt
);
5572 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5574 setup_coding_system (raw_text_coding_system
5575 (Vlast_coding_system_used
),
5577 coding
->src_multibyte
= 0;
5580 coding
->dst_multibyte
= 0;
5582 if (CODING_REQUIRE_ENCODING (coding
))
5584 coding
->dst_object
= Qt
;
5585 if (BUFFERP (object
))
5587 EMACS_INT from_byte
, from
, to
;
5588 EMACS_INT save_pt
, save_pt_byte
;
5589 struct buffer
*cur
= current_buffer
;
5591 set_buffer_internal (XBUFFER (object
));
5592 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5594 from_byte
= PTR_BYTE_POS (buf
);
5595 from
= BYTE_TO_CHAR (from_byte
);
5596 to
= BYTE_TO_CHAR (from_byte
+ len
);
5597 TEMP_SET_PT_BOTH (from
, from_byte
);
5598 encode_coding_object (coding
, object
, from
, from_byte
,
5599 to
, from_byte
+ len
, Qt
);
5600 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5601 set_buffer_internal (cur
);
5603 else if (STRINGP (object
))
5605 encode_coding_object (coding
, object
, 0, 0, SCHARS (object
),
5606 SBYTES (object
), Qt
);
5610 coding
->dst_object
= make_unibyte_string (buf
, len
);
5611 coding
->produced
= len
;
5614 len
= coding
->produced
;
5615 object
= coding
->dst_object
;
5616 buf
= SDATA (object
);
5619 if (pty_max_bytes
== 0)
5621 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5622 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5623 if (pty_max_bytes
< 0)
5624 pty_max_bytes
= 250;
5626 pty_max_bytes
= 250;
5628 /* Deduct one, to leave space for the eof. */
5632 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5633 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5634 when returning with longjmp despite being declared volatile. */
5635 if (!setjmp (send_process_frame
))
5637 process_sent_to
= proc
;
5640 EMACS_INT
this = len
;
5642 /* Send this batch, using one or more write calls. */
5645 int outfd
= p
->outfd
;
5646 old_sigpipe
= (SIGTYPE (*) (int)) signal (SIGPIPE
, send_process_trap
);
5647 #ifdef DATAGRAM_SOCKETS
5648 if (DATAGRAM_CHAN_P (outfd
))
5650 rv
= sendto (outfd
, (char *) buf
, this,
5651 0, datagram_address
[outfd
].sa
,
5652 datagram_address
[outfd
].len
);
5653 if (rv
< 0 && errno
== EMSGSIZE
)
5655 signal (SIGPIPE
, old_sigpipe
);
5656 report_file_error ("sending datagram",
5657 Fcons (proc
, Qnil
));
5664 if (XPROCESS (proc
)->gnutls_p
)
5665 rv
= emacs_gnutls_write (outfd
,
5667 (char *) buf
, this);
5670 rv
= emacs_write (outfd
, (char *) buf
, this);
5671 #ifdef ADAPTIVE_READ_BUFFERING
5672 if (p
->read_output_delay
> 0
5673 && p
->adaptive_read_buffering
== 1)
5675 p
->read_output_delay
= 0;
5676 process_output_delay_count
--;
5677 p
->read_output_skip
= 0;
5681 signal (SIGPIPE
, old_sigpipe
);
5687 || errno
== EWOULDBLOCK
5693 /* Buffer is full. Wait, accepting input;
5694 that may allow the program
5695 to finish doing output and read more. */
5699 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5700 /* A gross hack to work around a bug in FreeBSD.
5701 In the following sequence, read(2) returns
5705 write(2) 954 bytes, get EAGAIN
5706 read(2) 1024 bytes in process_read_output
5707 read(2) 11 bytes in process_read_output
5709 That is, read(2) returns more bytes than have
5710 ever been written successfully. The 1033 bytes
5711 read are the 1022 bytes written successfully
5712 after processing (for example with CRs added if
5713 the terminal is set up that way which it is
5714 here). The same bytes will be seen again in a
5715 later read(2), without the CRs. */
5717 if (errno
== EAGAIN
)
5720 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5722 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5724 /* Running filters might relocate buffers or strings.
5725 Arrange to relocate BUF. */
5726 if (BUFFERP (object
))
5727 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5728 else if (STRINGP (object
))
5729 offset
= buf
- SDATA (object
);
5731 #ifdef EMACS_HAS_USECS
5732 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5734 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5737 if (BUFFERP (object
))
5738 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5739 else if (STRINGP (object
))
5740 buf
= offset
+ SDATA (object
);
5745 /* This is a real error. */
5746 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5756 signal (SIGPIPE
, old_sigpipe
);
5757 proc
= process_sent_to
;
5758 p
= XPROCESS (proc
);
5759 p
->raw_status_new
= 0;
5760 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5761 p
->tick
= ++process_tick
;
5762 deactivate_process (proc
);
5763 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5769 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5771 doc
: /* Send current contents of region as input to PROCESS.
5772 PROCESS may be a process, a buffer, the name of a process or buffer, or
5773 nil, indicating the current buffer's process.
5774 Called from program, takes three arguments, PROCESS, START and END.
5775 If the region is more than 500 characters long,
5776 it is sent in several bunches. This may happen even for shorter regions.
5777 Output from processes can arrive in between bunches. */)
5778 (Lisp_Object process
, Lisp_Object start
, Lisp_Object end
)
5781 EMACS_INT start1
, end1
;
5783 proc
= get_process (process
);
5784 validate_region (&start
, &end
);
5786 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5787 move_gap (XINT (start
));
5789 start1
= CHAR_TO_BYTE (XINT (start
));
5790 end1
= CHAR_TO_BYTE (XINT (end
));
5791 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5792 Fcurrent_buffer ());
5797 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5799 doc
: /* Send PROCESS the contents of STRING as input.
5800 PROCESS may be a process, a buffer, the name of a process or buffer, or
5801 nil, indicating the current buffer's process.
5802 If STRING is more than 500 characters long,
5803 it is sent in several bunches. This may happen even for shorter strings.
5804 Output from processes can arrive in between bunches. */)
5805 (Lisp_Object process
, Lisp_Object string
)
5808 CHECK_STRING (string
);
5809 proc
= get_process (process
);
5810 send_process (proc
, SDATA (string
),
5811 SBYTES (string
), string
);
5815 /* Return the foreground process group for the tty/pty that
5816 the process P uses. */
5818 emacs_get_tty_pgrp (struct Lisp_Process
*p
)
5823 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5826 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5827 master side. Try the slave side. */
5828 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
5832 ioctl (fd
, TIOCGPGRP
, &gid
);
5836 #endif /* defined (TIOCGPGRP ) */
5841 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5842 Sprocess_running_child_p
, 0, 1, 0,
5843 doc
: /* Return t if PROCESS has given the terminal to a child.
5844 If the operating system does not make it possible to find out,
5845 return t unconditionally. */)
5846 (Lisp_Object process
)
5848 /* Initialize in case ioctl doesn't exist or gives an error,
5849 in a way that will cause returning t. */
5852 struct Lisp_Process
*p
;
5854 proc
= get_process (process
);
5855 p
= XPROCESS (proc
);
5857 if (!EQ (p
->type
, Qreal
))
5858 error ("Process %s is not a subprocess",
5861 error ("Process %s is not active",
5864 gid
= emacs_get_tty_pgrp (p
);
5871 /* send a signal number SIGNO to PROCESS.
5872 If CURRENT_GROUP is t, that means send to the process group
5873 that currently owns the terminal being used to communicate with PROCESS.
5874 This is used for various commands in shell mode.
5875 If CURRENT_GROUP is lambda, that means send to the process group
5876 that currently owns the terminal, but only if it is NOT the shell itself.
5878 If NOMSG is zero, insert signal-announcements into process's buffers
5881 If we can, we try to signal PROCESS by sending control characters
5882 down the pty. This allows us to signal inferiors who have changed
5883 their uid, for which killpg would return an EPERM error. */
5886 process_send_signal (Lisp_Object process
, int signo
, Lisp_Object current_group
,
5890 register struct Lisp_Process
*p
;
5894 proc
= get_process (process
);
5895 p
= XPROCESS (proc
);
5897 if (!EQ (p
->type
, Qreal
))
5898 error ("Process %s is not a subprocess",
5901 error ("Process %s is not active",
5905 current_group
= Qnil
;
5907 /* If we are using pgrps, get a pgrp number and make it negative. */
5908 if (NILP (current_group
))
5909 /* Send the signal to the shell's process group. */
5913 #ifdef SIGNALS_VIA_CHARACTERS
5914 /* If possible, send signals to the entire pgrp
5915 by sending an input character to it. */
5918 cc_t
*sig_char
= NULL
;
5920 tcgetattr (p
->infd
, &t
);
5925 sig_char
= &t
.c_cc
[VINTR
];
5929 sig_char
= &t
.c_cc
[VQUIT
];
5933 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5934 sig_char
= &t
.c_cc
[VSWTCH
];
5936 sig_char
= &t
.c_cc
[VSUSP
];
5941 if (sig_char
&& *sig_char
!= CDISABLE
)
5943 send_process (proc
, sig_char
, 1, Qnil
);
5946 /* If we can't send the signal with a character,
5947 fall through and send it another way. */
5949 /* The code above may fall through if it can't
5950 handle the signal. */
5951 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5954 /* Get the current pgrp using the tty itself, if we have that.
5955 Otherwise, use the pty to get the pgrp.
5956 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5957 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5958 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5959 His patch indicates that if TIOCGPGRP returns an error, then
5960 we should just assume that p->pid is also the process group id. */
5962 gid
= emacs_get_tty_pgrp (p
);
5965 /* If we can't get the information, assume
5966 the shell owns the tty. */
5969 /* It is not clear whether anything really can set GID to -1.
5970 Perhaps on some system one of those ioctls can or could do so.
5971 Or perhaps this is vestigial. */
5974 #else /* ! defined (TIOCGPGRP ) */
5975 /* Can't select pgrps on this system, so we know that
5976 the child itself heads the pgrp. */
5978 #endif /* ! defined (TIOCGPGRP ) */
5980 /* If current_group is lambda, and the shell owns the terminal,
5981 don't send any signal. */
5982 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5990 p
->raw_status_new
= 0;
5992 p
->tick
= ++process_tick
;
5995 status_notify (NULL
);
5996 redisplay_preserve_echo_area (13);
5999 #endif /* ! defined (SIGCONT) */
6003 flush_pending_output (p
->infd
);
6007 /* If we don't have process groups, send the signal to the immediate
6008 subprocess. That isn't really right, but it's better than any
6009 obvious alternative. */
6012 kill (p
->pid
, signo
);
6016 /* gid may be a pid, or minus a pgrp's number */
6018 if (!NILP (current_group
))
6020 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6021 EMACS_KILLPG (gid
, signo
);
6028 #else /* ! defined (TIOCSIGSEND) */
6029 EMACS_KILLPG (gid
, signo
);
6030 #endif /* ! defined (TIOCSIGSEND) */
6033 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6034 doc
: /* Interrupt process PROCESS.
6035 PROCESS may be a process, a buffer, or the name of a process or buffer.
6036 No arg or nil means current buffer's process.
6037 Second arg CURRENT-GROUP non-nil means send signal to
6038 the current process-group of the process's controlling terminal
6039 rather than to the process's own process group.
6040 If the process is a shell, this means interrupt current subjob
6041 rather than the shell.
6043 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6044 don't send the signal. */)
6045 (Lisp_Object process
, Lisp_Object current_group
)
6047 process_send_signal (process
, SIGINT
, current_group
, 0);
6051 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6052 doc
: /* Kill process PROCESS. May be process or name of one.
6053 See function `interrupt-process' for more details on usage. */)
6054 (Lisp_Object process
, Lisp_Object current_group
)
6056 process_send_signal (process
, SIGKILL
, current_group
, 0);
6060 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6061 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6062 See function `interrupt-process' for more details on usage. */)
6063 (Lisp_Object process
, Lisp_Object current_group
)
6065 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6069 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6070 doc
: /* Stop process PROCESS. May be process or name of one.
6071 See function `interrupt-process' for more details on usage.
6072 If PROCESS is a network or serial process, inhibit handling of incoming
6074 (Lisp_Object process
, Lisp_Object current_group
)
6076 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6078 struct Lisp_Process
*p
;
6080 p
= XPROCESS (process
);
6081 if (NILP (p
->command
)
6084 FD_CLR (p
->infd
, &input_wait_mask
);
6085 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6091 error ("No SIGTSTP support");
6093 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6098 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6099 doc
: /* Continue process PROCESS. May be process or name of one.
6100 See function `interrupt-process' for more details on usage.
6101 If PROCESS is a network or serial process, resume handling of incoming
6103 (Lisp_Object process
, Lisp_Object current_group
)
6105 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6107 struct Lisp_Process
*p
;
6109 p
= XPROCESS (process
);
6110 if (EQ (p
->command
, Qt
)
6112 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6114 FD_SET (p
->infd
, &input_wait_mask
);
6115 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6117 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6118 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6119 #else /* not WINDOWSNT */
6120 tcflush (p
->infd
, TCIFLUSH
);
6121 #endif /* not WINDOWSNT */
6127 process_send_signal (process
, SIGCONT
, current_group
, 0);
6129 error ("No SIGCONT support");
6134 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6135 2, 2, "sProcess (name or number): \nnSignal code: ",
6136 doc
: /* Send PROCESS the signal with code SIGCODE.
6137 PROCESS may also be a number specifying the process id of the
6138 process to signal; in this case, the process need not be a child of
6140 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6141 (Lisp_Object process
, Lisp_Object sigcode
)
6145 if (INTEGERP (process
))
6147 pid
= XINT (process
);
6151 if (FLOATP (process
))
6153 pid
= (pid_t
) XFLOAT_DATA (process
);
6157 if (STRINGP (process
))
6160 if (tem
= Fget_process (process
), NILP (tem
))
6162 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6169 process
= get_process (process
);
6174 CHECK_PROCESS (process
);
6175 pid
= XPROCESS (process
)->pid
;
6177 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6181 #define parse_signal(NAME, VALUE) \
6182 else if (!xstrcasecmp (name, NAME)) \
6183 XSETINT (sigcode, VALUE)
6185 if (INTEGERP (sigcode
))
6189 unsigned char *name
;
6191 CHECK_SYMBOL (sigcode
);
6192 name
= SDATA (SYMBOL_NAME (sigcode
));
6194 if (!strncmp (name
, "SIG", 3) || !strncmp (name
, "sig", 3))
6200 parse_signal ("usr1", SIGUSR1
);
6203 parse_signal ("usr2", SIGUSR2
);
6206 parse_signal ("term", SIGTERM
);
6209 parse_signal ("hup", SIGHUP
);
6212 parse_signal ("int", SIGINT
);
6215 parse_signal ("quit", SIGQUIT
);
6218 parse_signal ("ill", SIGILL
);
6221 parse_signal ("abrt", SIGABRT
);
6224 parse_signal ("emt", SIGEMT
);
6227 parse_signal ("kill", SIGKILL
);
6230 parse_signal ("fpe", SIGFPE
);
6233 parse_signal ("bus", SIGBUS
);
6236 parse_signal ("segv", SIGSEGV
);
6239 parse_signal ("sys", SIGSYS
);
6242 parse_signal ("pipe", SIGPIPE
);
6245 parse_signal ("alrm", SIGALRM
);
6248 parse_signal ("urg", SIGURG
);
6251 parse_signal ("stop", SIGSTOP
);
6254 parse_signal ("tstp", SIGTSTP
);
6257 parse_signal ("cont", SIGCONT
);
6260 parse_signal ("chld", SIGCHLD
);
6263 parse_signal ("ttin", SIGTTIN
);
6266 parse_signal ("ttou", SIGTTOU
);
6269 parse_signal ("io", SIGIO
);
6272 parse_signal ("xcpu", SIGXCPU
);
6275 parse_signal ("xfsz", SIGXFSZ
);
6278 parse_signal ("vtalrm", SIGVTALRM
);
6281 parse_signal ("prof", SIGPROF
);
6284 parse_signal ("winch", SIGWINCH
);
6287 parse_signal ("info", SIGINFO
);
6290 error ("Undefined signal name %s", name
);
6295 return make_number (kill (pid
, XINT (sigcode
)));
6298 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6299 doc
: /* Make PROCESS see end-of-file in its input.
6300 EOF comes after any text already sent to it.
6301 PROCESS may be a process, a buffer, the name of a process or buffer, or
6302 nil, indicating the current buffer's process.
6303 If PROCESS is a network connection, or is a process communicating
6304 through a pipe (as opposed to a pty), then you cannot send any more
6305 text to PROCESS after you call this function.
6306 If PROCESS is a serial process, wait until all output written to the
6307 process has been transmitted to the serial port. */)
6308 (Lisp_Object process
)
6311 struct coding_system
*coding
;
6313 if (DATAGRAM_CONN_P (process
))
6316 proc
= get_process (process
);
6317 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6319 /* Make sure the process is really alive. */
6320 if (XPROCESS (proc
)->raw_status_new
)
6321 update_status (XPROCESS (proc
));
6322 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6323 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6325 if (CODING_REQUIRE_FLUSHING (coding
))
6327 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6328 send_process (proc
, "", 0, Qnil
);
6331 if (XPROCESS (proc
)->pty_flag
)
6332 send_process (proc
, "\004", 1, Qnil
);
6333 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6336 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6337 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6338 #endif /* not WINDOWSNT */
6339 /* Do nothing on Windows because writes are blocking. */
6343 int old_outfd
, new_outfd
;
6345 #ifdef HAVE_SHUTDOWN
6346 /* If this is a network connection, or socketpair is used
6347 for communication with the subprocess, call shutdown to cause EOF.
6348 (In some old system, shutdown to socketpair doesn't work.
6349 Then we just can't win.) */
6350 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6351 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6352 shutdown (XPROCESS (proc
)->outfd
, 1);
6353 /* In case of socketpair, outfd == infd, so don't close it. */
6354 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6355 emacs_close (XPROCESS (proc
)->outfd
);
6356 #else /* not HAVE_SHUTDOWN */
6357 emacs_close (XPROCESS (proc
)->outfd
);
6358 #endif /* not HAVE_SHUTDOWN */
6359 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6362 old_outfd
= XPROCESS (proc
)->outfd
;
6364 if (!proc_encode_coding_system
[new_outfd
])
6365 proc_encode_coding_system
[new_outfd
]
6366 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6367 memcpy (proc_encode_coding_system
[new_outfd
],
6368 proc_encode_coding_system
[old_outfd
],
6369 sizeof (struct coding_system
));
6370 memset (proc_encode_coding_system
[old_outfd
], 0,
6371 sizeof (struct coding_system
));
6373 XPROCESS (proc
)->outfd
= new_outfd
;
6378 /* On receipt of a signal that a child status has changed, loop asking
6379 about children with changed statuses until the system says there
6382 All we do is change the status; we do not run sentinels or print
6383 notifications. That is saved for the next time keyboard input is
6384 done, in order to avoid timing errors.
6386 ** WARNING: this can be called during garbage collection.
6387 Therefore, it must not be fooled by the presence of mark bits in
6390 ** USG WARNING: Although it is not obvious from the documentation
6391 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6392 signal() before executing at least one wait(), otherwise the
6393 handler will be called again, resulting in an infinite loop. The
6394 relevant portion of the documentation reads "SIGCLD signals will be
6395 queued and the signal-catching function will be continually
6396 reentered until the queue is empty". Invoking signal() causes the
6397 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6400 ** Malloc WARNING: This should never call malloc either directly or
6401 indirectly; if it does, that is a bug */
6405 sigchld_handler (int signo
)
6407 int old_errno
= errno
;
6409 struct Lisp_Process
*p
;
6411 SIGNAL_THREAD_CHECK (signo
);
6422 #endif /* no WUNTRACED */
6423 /* Keep trying to get a status until we get a definitive result. */
6427 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6429 while (pid
< 0 && errno
== EINTR
);
6433 /* PID == 0 means no processes found, PID == -1 means a real
6434 failure. We have done all our job, so return. */
6441 #endif /* no WNOHANG */
6443 /* Find the process that signaled us, and record its status. */
6445 /* The process can have been deleted by Fdelete_process. */
6446 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6448 Lisp_Object xpid
= XCAR (tail
);
6449 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6450 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6452 XSETCAR (tail
, Qnil
);
6453 goto sigchld_end_of_loop
;
6457 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6459 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6461 proc
= XCDR (XCAR (tail
));
6462 p
= XPROCESS (proc
);
6463 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6468 /* Look for an asynchronous process whose pid hasn't been filled
6471 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6473 proc
= XCDR (XCAR (tail
));
6474 p
= XPROCESS (proc
);
6480 /* Change the status of the process that was found. */
6483 int clear_desc_flag
= 0;
6485 p
->tick
= ++process_tick
;
6487 p
->raw_status_new
= 1;
6489 /* If process has terminated, stop waiting for its output. */
6490 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6492 clear_desc_flag
= 1;
6494 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6495 if (clear_desc_flag
)
6497 FD_CLR (p
->infd
, &input_wait_mask
);
6498 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6501 /* Tell wait_reading_process_output that it needs to wake up and
6503 if (input_available_clear_time
)
6504 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6507 /* There was no asynchronous process found for that pid: we have
6508 a synchronous process. */
6511 synch_process_alive
= 0;
6513 /* Report the status of the synchronous process. */
6515 synch_process_retcode
= WRETCODE (w
);
6516 else if (WIFSIGNALED (w
))
6517 synch_process_termsig
= WTERMSIG (w
);
6519 /* Tell wait_reading_process_output that it needs to wake up and
6521 if (input_available_clear_time
)
6522 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6525 sigchld_end_of_loop
:
6528 /* On some systems, we must return right away.
6529 If any more processes want to signal us, we will
6531 Otherwise (on systems that have WNOHANG), loop around
6532 to use up all the processes that have something to tell us. */
6533 #if (defined WINDOWSNT \
6534 || (defined USG && !defined GNU_LINUX \
6535 && !(defined HPUX && defined WNOHANG)))
6538 #endif /* USG, but not HPUX with WNOHANG */
6541 #endif /* SIGCHLD */
6545 exec_sentinel_unwind (Lisp_Object data
)
6547 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6552 exec_sentinel_error_handler (Lisp_Object error
)
6554 cmd_error_internal (error
, "error in process sentinel: ");
6556 update_echo_area ();
6557 Fsleep_for (make_number (2), Qnil
);
6562 exec_sentinel (Lisp_Object proc
, Lisp_Object reason
)
6564 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6565 register struct Lisp_Process
*p
= XPROCESS (proc
);
6566 int count
= SPECPDL_INDEX ();
6567 int outer_running_asynch_code
= running_asynch_code
;
6568 int waiting
= waiting_for_user_input_p
;
6570 if (inhibit_sentinels
)
6573 /* No need to gcpro these, because all we do with them later
6574 is test them for EQness, and none of them should be a string. */
6575 odeactivate
= Vdeactivate_mark
;
6576 XSETBUFFER (obuffer
, current_buffer
);
6577 okeymap
= current_buffer
->keymap
;
6579 /* There's no good reason to let sentinels change the current
6580 buffer, and many callers of accept-process-output, sit-for, and
6581 friends don't expect current-buffer to be changed from under them. */
6582 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
6584 sentinel
= p
->sentinel
;
6585 if (NILP (sentinel
))
6588 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6589 assure that it gets restored no matter how the sentinel exits. */
6591 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6592 /* Inhibit quit so that random quits don't screw up a running filter. */
6593 specbind (Qinhibit_quit
, Qt
);
6594 specbind (Qlast_nonmenu_event
, Qt
); /* Why? --Stef */
6596 /* In case we get recursively called,
6597 and we already saved the match data nonrecursively,
6598 save the same match data in safely recursive fashion. */
6599 if (outer_running_asynch_code
)
6602 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6603 restore_search_regs ();
6604 record_unwind_save_match_data ();
6605 Fset_match_data (tem
, Qt
);
6608 /* For speed, if a search happens within this code,
6609 save the match data in a special nonrecursive fashion. */
6610 running_asynch_code
= 1;
6612 internal_condition_case_1 (read_process_output_call
,
6614 Fcons (proc
, Fcons (reason
, Qnil
))),
6615 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6616 exec_sentinel_error_handler
);
6618 /* If we saved the match data nonrecursively, restore it now. */
6619 restore_search_regs ();
6620 running_asynch_code
= outer_running_asynch_code
;
6622 Vdeactivate_mark
= odeactivate
;
6624 /* Restore waiting_for_user_input_p as it was
6625 when we were called, in case the filter clobbered it. */
6626 waiting_for_user_input_p
= waiting
;
6629 if (! EQ (Fcurrent_buffer (), obuffer
)
6630 || ! EQ (current_buffer
->keymap
, okeymap
))
6632 /* But do it only if the caller is actually going to read events.
6633 Otherwise there's no need to make him wake up, and it could
6634 cause trouble (for example it would make sit_for return). */
6635 if (waiting_for_user_input_p
== -1)
6636 record_asynch_buffer_change ();
6638 unbind_to (count
, Qnil
);
6641 /* Report all recent events of a change in process status
6642 (either run the sentinel or output a message).
6643 This is usually done while Emacs is waiting for keyboard input
6644 but can be done at other times. */
6647 status_notify (struct Lisp_Process
*deleting_process
)
6649 register Lisp_Object proc
, buffer
;
6650 Lisp_Object tail
, msg
;
6651 struct gcpro gcpro1
, gcpro2
;
6655 /* We need to gcpro tail; if read_process_output calls a filter
6656 which deletes a process and removes the cons to which tail points
6657 from Vprocess_alist, and then causes a GC, tail is an unprotected
6661 /* Set this now, so that if new processes are created by sentinels
6662 that we run, we get called again to handle their status changes. */
6663 update_tick
= process_tick
;
6665 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6668 register struct Lisp_Process
*p
;
6670 proc
= Fcdr (XCAR (tail
));
6671 p
= XPROCESS (proc
);
6673 if (p
->tick
!= p
->update_tick
)
6675 p
->update_tick
= p
->tick
;
6677 /* If process is still active, read any output that remains. */
6678 while (! EQ (p
->filter
, Qt
)
6679 && ! EQ (p
->status
, Qconnect
)
6680 && ! EQ (p
->status
, Qlisten
)
6681 /* Network or serial process not stopped: */
6682 && ! EQ (p
->command
, Qt
)
6684 && p
!= deleting_process
6685 && read_process_output (proc
, p
->infd
) > 0);
6689 /* Get the text to use for the message. */
6690 if (p
->raw_status_new
)
6692 msg
= status_message (p
);
6694 /* If process is terminated, deactivate it or delete it. */
6696 if (CONSP (p
->status
))
6697 symbol
= XCAR (p
->status
);
6699 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6700 || EQ (symbol
, Qclosed
))
6702 if (delete_exited_processes
)
6703 remove_process (proc
);
6705 deactivate_process (proc
);
6708 /* The actions above may have further incremented p->tick.
6709 So set p->update_tick again
6710 so that an error in the sentinel will not cause
6711 this code to be run again. */
6712 p
->update_tick
= p
->tick
;
6713 /* Now output the message suitably. */
6714 if (!NILP (p
->sentinel
))
6715 exec_sentinel (proc
, msg
);
6716 /* Don't bother with a message in the buffer
6717 when a process becomes runnable. */
6718 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6721 struct buffer
*old
= current_buffer
;
6722 EMACS_INT opoint
, opoint_byte
;
6723 EMACS_INT before
, before_byte
;
6725 /* Avoid error if buffer is deleted
6726 (probably that's why the process is dead, too) */
6727 if (NILP (XBUFFER (buffer
)->name
))
6729 Fset_buffer (buffer
);
6732 opoint_byte
= PT_BYTE
;
6733 /* Insert new output into buffer
6734 at the current end-of-output marker,
6735 thus preserving logical ordering of input and output. */
6736 if (XMARKER (p
->mark
)->buffer
)
6737 Fgoto_char (p
->mark
);
6739 SET_PT_BOTH (ZV
, ZV_BYTE
);
6742 before_byte
= PT_BYTE
;
6744 tem
= current_buffer
->read_only
;
6745 current_buffer
->read_only
= Qnil
;
6746 insert_string ("\nProcess ");
6747 Finsert (1, &p
->name
);
6748 insert_string (" ");
6750 current_buffer
->read_only
= tem
;
6751 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6753 if (opoint
>= before
)
6754 SET_PT_BOTH (opoint
+ (PT
- before
),
6755 opoint_byte
+ (PT_BYTE
- before_byte
));
6757 SET_PT_BOTH (opoint
, opoint_byte
);
6759 set_buffer_internal (old
);
6764 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6769 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6770 Sset_process_coding_system
, 1, 3, 0,
6771 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6772 DECODING will be used to decode subprocess output and ENCODING to
6773 encode subprocess input. */)
6774 (register Lisp_Object process
, Lisp_Object decoding
, Lisp_Object encoding
)
6776 register struct Lisp_Process
*p
;
6778 CHECK_PROCESS (process
);
6779 p
= XPROCESS (process
);
6781 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6783 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6784 Fcheck_coding_system (decoding
);
6785 Fcheck_coding_system (encoding
);
6786 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6787 p
->decode_coding_system
= decoding
;
6788 p
->encode_coding_system
= encoding
;
6789 setup_process_coding_systems (process
);
6794 DEFUN ("process-coding-system",
6795 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6796 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6797 (register Lisp_Object process
)
6799 CHECK_PROCESS (process
);
6800 return Fcons (XPROCESS (process
)->decode_coding_system
,
6801 XPROCESS (process
)->encode_coding_system
);
6804 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6805 Sset_process_filter_multibyte
, 2, 2, 0,
6806 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6807 If FLAG is non-nil, the filter is given multibyte strings.
6808 If FLAG is nil, the filter is given unibyte strings. In this case,
6809 all character code conversion except for end-of-line conversion is
6811 (Lisp_Object process
, Lisp_Object flag
)
6813 register struct Lisp_Process
*p
;
6815 CHECK_PROCESS (process
);
6816 p
= XPROCESS (process
);
6818 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
6819 setup_process_coding_systems (process
);
6824 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6825 Sprocess_filter_multibyte_p
, 1, 1, 0,
6826 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6827 (Lisp_Object process
)
6829 register struct Lisp_Process
*p
;
6830 struct coding_system
*coding
;
6832 CHECK_PROCESS (process
);
6833 p
= XPROCESS (process
);
6834 coding
= proc_decode_coding_system
[p
->infd
];
6835 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6842 add_gpm_wait_descriptor (int desc
)
6844 add_keyboard_wait_descriptor (desc
);
6848 delete_gpm_wait_descriptor (int desc
)
6850 delete_keyboard_wait_descriptor (desc
);
6853 /* Return nonzero if *MASK has a bit set
6854 that corresponds to one of the keyboard input descriptors. */
6857 keyboard_bit_set (fd_set
*mask
)
6861 for (fd
= 0; fd
<= max_input_desc
; fd
++)
6862 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6863 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6869 #else /* not subprocesses */
6871 /* Defined on msdos.c. */
6872 extern int sys_select (int, SELECT_TYPE
*, SELECT_TYPE
*, SELECT_TYPE
*,
6875 /* Implementation of wait_reading_process_output, assuming that there
6876 are no subprocesses. Used only by the MS-DOS build.
6878 Wait for timeout to elapse and/or keyboard input to be available.
6881 timeout in seconds, or
6882 zero for no limit, or
6883 -1 means gobble data immediately available but don't wait for any.
6885 read_kbd is a Lisp_Object:
6886 0 to ignore keyboard input, or
6887 1 to return when input is available, or
6888 -1 means caller will actually read the input, so don't throw to
6891 see full version for other parameters. We know that wait_proc will
6892 always be NULL, since `subprocesses' isn't defined.
6894 do_display != 0 means redisplay should be done to show subprocess
6895 output that arrives.
6897 Return true if we received input from any process. */
6900 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
6902 Lisp_Object wait_for_cell
,
6903 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
6906 EMACS_TIME end_time
, timeout
;
6907 SELECT_TYPE waitchannels
;
6910 /* What does time_limit really mean? */
6911 if (time_limit
|| microsecs
)
6913 EMACS_GET_TIME (end_time
);
6914 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
6915 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
6918 /* Turn off periodic alarms (in case they are in use)
6919 and then turn off any other atimers,
6920 because the select emulator uses alarms. */
6922 turn_on_atimers (0);
6926 int timeout_reduced_for_timers
= 0;
6928 /* If calling from keyboard input, do not quit
6929 since we want to return C-g as an input character.
6930 Otherwise, do pending quit if requested. */
6934 /* Exit now if the cell we're waiting for became non-nil. */
6935 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6938 /* Compute time from now till when time limit is up */
6939 /* Exit if already run out */
6940 if (time_limit
== -1)
6942 /* -1 specified for timeout means
6943 gobble output available now
6944 but don't wait at all. */
6946 EMACS_SET_SECS_USECS (timeout
, 0, 0);
6948 else if (time_limit
|| microsecs
)
6950 EMACS_GET_TIME (timeout
);
6951 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
6952 if (EMACS_TIME_NEG_P (timeout
))
6957 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
6960 /* If our caller will not immediately handle keyboard events,
6961 run timer events directly.
6962 (Callers that will immediately read keyboard events
6963 call timer_delay on their own.) */
6964 if (NILP (wait_for_cell
))
6966 EMACS_TIME timer_delay
;
6970 int old_timers_run
= timers_run
;
6971 timer_delay
= timer_check (1);
6972 if (timers_run
!= old_timers_run
&& do_display
)
6973 /* We must retry, since a timer may have requeued itself
6974 and that could alter the time delay. */
6975 redisplay_preserve_echo_area (14);
6979 while (!detect_input_pending ());
6981 /* If there is unread keyboard input, also return. */
6983 && requeued_events_pending_p ())
6986 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
6988 EMACS_TIME difference
;
6989 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
6990 if (EMACS_TIME_NEG_P (difference
))
6992 timeout
= timer_delay
;
6993 timeout_reduced_for_timers
= 1;
6998 /* Cause C-g and alarm signals to take immediate action,
6999 and cause input available signals to zero out timeout. */
7001 set_waiting_for_input (&timeout
);
7003 /* Wait till there is something to do. */
7005 if (! read_kbd
&& NILP (wait_for_cell
))
7006 FD_ZERO (&waitchannels
);
7008 FD_SET (0, &waitchannels
);
7010 /* If a frame has been newly mapped and needs updating,
7011 reprocess its display stuff. */
7012 if (frame_garbaged
&& do_display
)
7014 clear_waiting_for_input ();
7015 redisplay_preserve_echo_area (15);
7017 set_waiting_for_input (&timeout
);
7020 if (read_kbd
&& detect_input_pending ())
7023 FD_ZERO (&waitchannels
);
7026 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7031 /* Make C-g and alarm signals set flags again */
7032 clear_waiting_for_input ();
7034 /* If we woke up due to SIGWINCH, actually change size now. */
7035 do_pending_window_change (0);
7037 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7038 /* We waited the full specified time, so return now. */
7043 /* If the system call was interrupted, then go around the
7045 if (xerrno
== EINTR
)
7046 FD_ZERO (&waitchannels
);
7048 error ("select error: %s", emacs_strerror (xerrno
));
7051 /* Check for keyboard input */
7054 && detect_input_pending_run_timers (do_display
))
7056 swallow_events (do_display
);
7057 if (detect_input_pending_run_timers (do_display
))
7061 /* If there is unread keyboard input, also return. */
7063 && requeued_events_pending_p ())
7066 /* If wait_for_cell. check for keyboard input
7067 but don't run any timers.
7068 ??? (It seems wrong to me to check for keyboard
7069 input at all when wait_for_cell, but the code
7070 has been this way since July 1994.
7071 Try changing this after version 19.31.) */
7072 if (! NILP (wait_for_cell
)
7073 && detect_input_pending ())
7075 swallow_events (do_display
);
7076 if (detect_input_pending ())
7080 /* Exit now if the cell we're waiting for became non-nil. */
7081 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7090 #endif /* not subprocesses */
7092 /* The following functions are needed even if async subprocesses are
7093 not supported. Some of them are no-op stubs in that case. */
7095 /* Add DESC to the set of keyboard input descriptors. */
7098 add_keyboard_wait_descriptor (int desc
)
7100 #ifdef subprocesses /* actually means "not MSDOS" */
7101 FD_SET (desc
, &input_wait_mask
);
7102 FD_SET (desc
, &non_process_wait_mask
);
7103 if (desc
> max_input_desc
)
7104 max_input_desc
= desc
;
7108 /* From now on, do not expect DESC to give keyboard input. */
7111 delete_keyboard_wait_descriptor (int desc
)
7115 int lim
= max_input_desc
;
7117 FD_CLR (desc
, &input_wait_mask
);
7118 FD_CLR (desc
, &non_process_wait_mask
);
7120 if (desc
== max_input_desc
)
7121 for (fd
= 0; fd
< lim
; fd
++)
7122 if (FD_ISSET (fd
, &input_wait_mask
) || FD_ISSET (fd
, &write_mask
))
7123 max_input_desc
= fd
;
7127 /* Setup coding systems of PROCESS. */
7130 setup_process_coding_systems (Lisp_Object process
)
7133 struct Lisp_Process
*p
= XPROCESS (process
);
7135 int outch
= p
->outfd
;
7136 Lisp_Object coding_system
;
7138 if (inch
< 0 || outch
< 0)
7141 if (!proc_decode_coding_system
[inch
])
7142 proc_decode_coding_system
[inch
]
7143 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7144 coding_system
= p
->decode_coding_system
;
7145 if (! NILP (p
->filter
))
7147 else if (BUFFERP (p
->buffer
))
7149 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
7150 coding_system
= raw_text_coding_system (coding_system
);
7152 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
7154 if (!proc_encode_coding_system
[outch
])
7155 proc_encode_coding_system
[outch
]
7156 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7157 setup_coding_system (p
->encode_coding_system
,
7158 proc_encode_coding_system
[outch
]);
7162 /* Close all descriptors currently in use for communication
7163 with subprocess. This is used in a newly-forked subprocess
7164 to get rid of irrelevant descriptors. */
7167 close_process_descs (void)
7171 for (i
= 0; i
< MAXDESC
; i
++)
7173 Lisp_Object process
;
7174 process
= chan_process
[i
];
7175 if (!NILP (process
))
7177 int in
= XPROCESS (process
)->infd
;
7178 int out
= XPROCESS (process
)->outfd
;
7181 if (out
>= 0 && in
!= out
)
7188 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7189 doc
: /* Return the (or a) process associated with BUFFER.
7190 BUFFER may be a buffer or the name of one. */)
7191 (register Lisp_Object buffer
)
7194 register Lisp_Object buf
, tail
, proc
;
7196 if (NILP (buffer
)) return Qnil
;
7197 buf
= Fget_buffer (buffer
);
7198 if (NILP (buf
)) return Qnil
;
7200 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7202 proc
= Fcdr (XCAR (tail
));
7203 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
7206 #endif /* subprocesses */
7210 DEFUN ("process-inherit-coding-system-flag",
7211 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7213 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
7214 If this flag is t, `buffer-file-coding-system' of the buffer
7215 associated with PROCESS will inherit the coding system used to decode
7216 the process output. */)
7217 (register Lisp_Object process
)
7220 CHECK_PROCESS (process
);
7221 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
7223 /* Ignore the argument and return the value of
7224 inherit-process-coding-system. */
7225 return inherit_process_coding_system
? Qt
: Qnil
;
7229 /* Kill all processes associated with `buffer'.
7230 If `buffer' is nil, kill all processes */
7233 kill_buffer_processes (Lisp_Object buffer
)
7236 Lisp_Object tail
, proc
;
7238 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7240 proc
= XCDR (XCAR (tail
));
7242 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
7244 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
7245 Fdelete_process (proc
);
7246 else if (XPROCESS (proc
)->infd
>= 0)
7247 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
7250 #else /* subprocesses */
7251 /* Since we have no subprocesses, this does nothing. */
7252 #endif /* subprocesses */
7255 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
7257 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
7258 This is intended for use by asynchronous process output filters and sentinels. */)
7262 return (waiting_for_user_input_p
? Qt
: Qnil
);
7268 /* Stop reading input from keyboard sources. */
7271 hold_keyboard_input (void)
7276 /* Resume reading input from keyboard sources. */
7279 unhold_keyboard_input (void)
7284 /* Return non-zero if keyboard input is on hold, zero otherwise. */
7287 kbd_on_hold_p (void)
7289 return kbd_is_on_hold
;
7293 /* Enumeration of and access to system processes a-la ps(1). */
7295 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7297 doc
: /* Return a list of numerical process IDs of all running processes.
7298 If this functionality is unsupported, return nil.
7300 See `process-attributes' for getting attributes of a process given its ID. */)
7303 return list_system_processes ();
7306 DEFUN ("process-attributes", Fprocess_attributes
,
7307 Sprocess_attributes
, 1, 1, 0,
7308 doc
: /* Return attributes of the process given by its PID, a number.
7310 Value is an alist where each element is a cons cell of the form
7314 If this functionality is unsupported, the value is nil.
7316 See `list-system-processes' for getting a list of all process IDs.
7318 The KEYs of the attributes that this function may return are listed
7319 below, together with the type of the associated VALUE (in parentheses).
7320 Not all platforms support all of these attributes; unsupported
7321 attributes will not appear in the returned alist.
7322 Unless explicitly indicated otherwise, numbers can have either
7323 integer or floating point values.
7325 euid -- Effective user User ID of the process (number)
7326 user -- User name corresponding to euid (string)
7327 egid -- Effective user Group ID of the process (number)
7328 group -- Group name corresponding to egid (string)
7329 comm -- Command name (executable name only) (string)
7330 state -- Process state code, such as "S", "R", or "T" (string)
7331 ppid -- Parent process ID (number)
7332 pgrp -- Process group ID (number)
7333 sess -- Session ID, i.e. process ID of session leader (number)
7334 ttname -- Controlling tty name (string)
7335 tpgid -- ID of foreground process group on the process's tty (number)
7336 minflt -- number of minor page faults (number)
7337 majflt -- number of major page faults (number)
7338 cminflt -- cumulative number of minor page faults (number)
7339 cmajflt -- cumulative number of major page faults (number)
7340 utime -- user time used by the process, in the (HIGH LOW USEC) format
7341 stime -- system time used by the process, in the (HIGH LOW USEC) format
7342 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7343 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7344 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7345 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7346 pri -- priority of the process (number)
7347 nice -- nice value of the process (number)
7348 thcount -- process thread count (number)
7349 start -- time the process started, in the (HIGH LOW USEC) format
7350 vsize -- virtual memory size of the process in KB's (number)
7351 rss -- resident set size of the process in KB's (number)
7352 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7353 pcpu -- percents of CPU time used by the process (floating-point number)
7354 pmem -- percents of total physical memory used by process's resident set
7355 (floating-point number)
7356 args -- command line which invoked the process (string). */)
7359 return system_process_attributes (pid
);
7369 inhibit_sentinels
= 0;
7373 if (! noninteractive
|| initialized
)
7375 signal (SIGCHLD
, sigchld_handler
);
7378 FD_ZERO (&input_wait_mask
);
7379 FD_ZERO (&non_keyboard_wait_mask
);
7380 FD_ZERO (&non_process_wait_mask
);
7381 FD_ZERO (&write_mask
);
7382 max_process_desc
= 0;
7383 memset (fd_callback_info
, 0, sizeof (fd_callback_info
));
7385 #ifdef NON_BLOCKING_CONNECT
7386 FD_ZERO (&connect_wait_mask
);
7387 num_pending_connects
= 0;
7390 #ifdef ADAPTIVE_READ_BUFFERING
7391 process_output_delay_count
= 0;
7392 process_output_skip
= 0;
7395 /* Don't do this, it caused infinite select loops. The display
7396 method should call add_keyboard_wait_descriptor on stdin if it
7399 FD_SET (0, &input_wait_mask
);
7402 Vprocess_alist
= Qnil
;
7404 deleted_pid_list
= Qnil
;
7406 for (i
= 0; i
< MAXDESC
; i
++)
7408 chan_process
[i
] = Qnil
;
7409 proc_buffered_char
[i
] = -1;
7411 memset (proc_decode_coding_system
, 0, sizeof proc_decode_coding_system
);
7412 memset (proc_encode_coding_system
, 0, sizeof proc_encode_coding_system
);
7413 #ifdef DATAGRAM_SOCKETS
7414 memset (datagram_address
, 0, sizeof datagram_address
);
7418 Lisp_Object subfeatures
= Qnil
;
7419 const struct socket_options
*sopt
;
7421 #define ADD_SUBFEATURE(key, val) \
7422 subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)
7424 #ifdef NON_BLOCKING_CONNECT
7425 ADD_SUBFEATURE (QCnowait
, Qt
);
7427 #ifdef DATAGRAM_SOCKETS
7428 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7430 #ifdef HAVE_SEQPACKET
7431 ADD_SUBFEATURE (QCtype
, Qseqpacket
);
7433 #ifdef HAVE_LOCAL_SOCKETS
7434 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7436 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7438 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7440 #ifdef HAVE_GETSOCKNAME
7441 ADD_SUBFEATURE (QCservice
, Qt
);
7443 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7444 ADD_SUBFEATURE (QCserver
, Qt
);
7447 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7448 subfeatures
= pure_cons (intern_c_string (sopt
->name
), subfeatures
);
7450 Fprovide (intern_c_string ("make-network-process"), subfeatures
);
7453 #if defined (DARWIN_OS)
7454 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7455 processes. As such, we only change the default value. */
7458 const char *release
= get_operating_system_release ();
7459 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7460 && release
[1] == '.')) {
7461 Vprocess_connection_type
= Qnil
;
7465 #endif /* subprocesses */
7470 syms_of_process (void)
7474 Qprocessp
= intern_c_string ("processp");
7475 staticpro (&Qprocessp
);
7476 Qrun
= intern_c_string ("run");
7478 Qstop
= intern_c_string ("stop");
7480 Qsignal
= intern_c_string ("signal");
7481 staticpro (&Qsignal
);
7483 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7486 Qexit = intern_c_string ("exit");
7487 staticpro (&Qexit); */
7489 Qopen
= intern_c_string ("open");
7491 Qclosed
= intern_c_string ("closed");
7492 staticpro (&Qclosed
);
7493 Qconnect
= intern_c_string ("connect");
7494 staticpro (&Qconnect
);
7495 Qfailed
= intern_c_string ("failed");
7496 staticpro (&Qfailed
);
7497 Qlisten
= intern_c_string ("listen");
7498 staticpro (&Qlisten
);
7499 Qlocal
= intern_c_string ("local");
7500 staticpro (&Qlocal
);
7501 Qipv4
= intern_c_string ("ipv4");
7504 Qipv6
= intern_c_string ("ipv6");
7507 Qdatagram
= intern_c_string ("datagram");
7508 staticpro (&Qdatagram
);
7509 Qseqpacket
= intern_c_string ("seqpacket");
7510 staticpro (&Qseqpacket
);
7512 QCport
= intern_c_string (":port");
7513 staticpro (&QCport
);
7514 QCspeed
= intern_c_string (":speed");
7515 staticpro (&QCspeed
);
7516 QCprocess
= intern_c_string (":process");
7517 staticpro (&QCprocess
);
7519 QCbytesize
= intern_c_string (":bytesize");
7520 staticpro (&QCbytesize
);
7521 QCstopbits
= intern_c_string (":stopbits");
7522 staticpro (&QCstopbits
);
7523 QCparity
= intern_c_string (":parity");
7524 staticpro (&QCparity
);
7525 Qodd
= intern_c_string ("odd");
7527 Qeven
= intern_c_string ("even");
7529 QCflowcontrol
= intern_c_string (":flowcontrol");
7530 staticpro (&QCflowcontrol
);
7531 Qhw
= intern_c_string ("hw");
7533 Qsw
= intern_c_string ("sw");
7535 QCsummary
= intern_c_string (":summary");
7536 staticpro (&QCsummary
);
7538 Qreal
= intern_c_string ("real");
7540 Qnetwork
= intern_c_string ("network");
7541 staticpro (&Qnetwork
);
7542 Qserial
= intern_c_string ("serial");
7543 staticpro (&Qserial
);
7544 QCbuffer
= intern_c_string (":buffer");
7545 staticpro (&QCbuffer
);
7546 QChost
= intern_c_string (":host");
7547 staticpro (&QChost
);
7548 QCservice
= intern_c_string (":service");
7549 staticpro (&QCservice
);
7550 QClocal
= intern_c_string (":local");
7551 staticpro (&QClocal
);
7552 QCremote
= intern_c_string (":remote");
7553 staticpro (&QCremote
);
7554 QCcoding
= intern_c_string (":coding");
7555 staticpro (&QCcoding
);
7556 QCserver
= intern_c_string (":server");
7557 staticpro (&QCserver
);
7558 QCnowait
= intern_c_string (":nowait");
7559 staticpro (&QCnowait
);
7560 QCsentinel
= intern_c_string (":sentinel");
7561 staticpro (&QCsentinel
);
7562 QClog
= intern_c_string (":log");
7564 QCnoquery
= intern_c_string (":noquery");
7565 staticpro (&QCnoquery
);
7566 QCstop
= intern_c_string (":stop");
7567 staticpro (&QCstop
);
7568 QCoptions
= intern_c_string (":options");
7569 staticpro (&QCoptions
);
7570 QCplist
= intern_c_string (":plist");
7571 staticpro (&QCplist
);
7573 Qlast_nonmenu_event
= intern_c_string ("last-nonmenu-event");
7574 staticpro (&Qlast_nonmenu_event
);
7576 staticpro (&Vprocess_alist
);
7578 staticpro (&deleted_pid_list
);
7581 #endif /* subprocesses */
7583 QCname
= intern_c_string (":name");
7584 staticpro (&QCname
);
7585 QCtype
= intern_c_string (":type");
7586 staticpro (&QCtype
);
7588 Qeuid
= intern_c_string ("euid");
7590 Qegid
= intern_c_string ("egid");
7592 Quser
= intern_c_string ("user");
7594 Qgroup
= intern_c_string ("group");
7595 staticpro (&Qgroup
);
7596 Qcomm
= intern_c_string ("comm");
7598 Qstate
= intern_c_string ("state");
7599 staticpro (&Qstate
);
7600 Qppid
= intern_c_string ("ppid");
7602 Qpgrp
= intern_c_string ("pgrp");
7604 Qsess
= intern_c_string ("sess");
7606 Qttname
= intern_c_string ("ttname");
7607 staticpro (&Qttname
);
7608 Qtpgid
= intern_c_string ("tpgid");
7609 staticpro (&Qtpgid
);
7610 Qminflt
= intern_c_string ("minflt");
7611 staticpro (&Qminflt
);
7612 Qmajflt
= intern_c_string ("majflt");
7613 staticpro (&Qmajflt
);
7614 Qcminflt
= intern_c_string ("cminflt");
7615 staticpro (&Qcminflt
);
7616 Qcmajflt
= intern_c_string ("cmajflt");
7617 staticpro (&Qcmajflt
);
7618 Qutime
= intern_c_string ("utime");
7619 staticpro (&Qutime
);
7620 Qstime
= intern_c_string ("stime");
7621 staticpro (&Qstime
);
7622 Qtime
= intern_c_string ("time");
7624 Qcutime
= intern_c_string ("cutime");
7625 staticpro (&Qcutime
);
7626 Qcstime
= intern_c_string ("cstime");
7627 staticpro (&Qcstime
);
7628 Qctime
= intern_c_string ("ctime");
7629 staticpro (&Qctime
);
7630 Qpri
= intern_c_string ("pri");
7632 Qnice
= intern_c_string ("nice");
7634 Qthcount
= intern_c_string ("thcount");
7635 staticpro (&Qthcount
);
7636 Qstart
= intern_c_string ("start");
7637 staticpro (&Qstart
);
7638 Qvsize
= intern_c_string ("vsize");
7639 staticpro (&Qvsize
);
7640 Qrss
= intern_c_string ("rss");
7642 Qetime
= intern_c_string ("etime");
7643 staticpro (&Qetime
);
7644 Qpcpu
= intern_c_string ("pcpu");
7646 Qpmem
= intern_c_string ("pmem");
7648 Qargs
= intern_c_string ("args");
7651 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7652 doc
: /* *Non-nil means delete processes immediately when they exit.
7653 A value of nil means don't delete them until `list-processes' is run. */);
7655 delete_exited_processes
= 1;
7658 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7659 doc
: /* Control type of device used to communicate with subprocesses.
7660 Values are nil to use a pipe, or t or `pty' to use a pty.
7661 The value has no effect if the system has no ptys or if all ptys are busy:
7662 then a pipe is used in any case.
7663 The value takes effect when `start-process' is called. */);
7664 Vprocess_connection_type
= Qt
;
7666 #ifdef ADAPTIVE_READ_BUFFERING
7667 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7668 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7669 On some systems, when Emacs reads the output from a subprocess, the output data
7670 is read in very small blocks, potentially resulting in very poor performance.
7671 This behavior can be remedied to some extent by setting this variable to a
7672 non-nil value, as it will automatically delay reading from such processes, to
7673 allow them to produce more output before Emacs tries to read it.
7674 If the value is t, the delay is reset after each write to the process; any other
7675 non-nil value means that the delay is not reset on write.
7676 The variable takes effect when `start-process' is called. */);
7677 Vprocess_adaptive_read_buffering
= Qt
;
7680 defsubr (&Sprocessp
);
7681 defsubr (&Sget_process
);
7682 defsubr (&Sdelete_process
);
7683 defsubr (&Sprocess_status
);
7684 defsubr (&Sprocess_exit_status
);
7685 defsubr (&Sprocess_id
);
7686 defsubr (&Sprocess_name
);
7687 defsubr (&Sprocess_tty_name
);
7688 defsubr (&Sprocess_command
);
7689 defsubr (&Sset_process_buffer
);
7690 defsubr (&Sprocess_buffer
);
7691 defsubr (&Sprocess_mark
);
7692 defsubr (&Sset_process_filter
);
7693 defsubr (&Sprocess_filter
);
7694 defsubr (&Sset_process_sentinel
);
7695 defsubr (&Sprocess_sentinel
);
7696 defsubr (&Sset_process_window_size
);
7697 defsubr (&Sset_process_inherit_coding_system_flag
);
7698 defsubr (&Sset_process_query_on_exit_flag
);
7699 defsubr (&Sprocess_query_on_exit_flag
);
7700 defsubr (&Sprocess_contact
);
7701 defsubr (&Sprocess_plist
);
7702 defsubr (&Sset_process_plist
);
7703 defsubr (&Slist_processes
);
7704 defsubr (&Sprocess_list
);
7705 defsubr (&Sstart_process
);
7706 defsubr (&Sserial_process_configure
);
7707 defsubr (&Smake_serial_process
);
7708 defsubr (&Sset_network_process_option
);
7709 defsubr (&Smake_network_process
);
7710 defsubr (&Sformat_network_address
);
7711 #if defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7713 defsubr (&Snetwork_interface_list
);
7715 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7716 defsubr (&Snetwork_interface_info
);
7718 #endif /* defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H) */
7719 #ifdef DATAGRAM_SOCKETS
7720 defsubr (&Sprocess_datagram_address
);
7721 defsubr (&Sset_process_datagram_address
);
7723 defsubr (&Saccept_process_output
);
7724 defsubr (&Sprocess_send_region
);
7725 defsubr (&Sprocess_send_string
);
7726 defsubr (&Sinterrupt_process
);
7727 defsubr (&Skill_process
);
7728 defsubr (&Squit_process
);
7729 defsubr (&Sstop_process
);
7730 defsubr (&Scontinue_process
);
7731 defsubr (&Sprocess_running_child_p
);
7732 defsubr (&Sprocess_send_eof
);
7733 defsubr (&Ssignal_process
);
7734 defsubr (&Swaiting_for_user_input_p
);
7735 defsubr (&Sprocess_type
);
7736 defsubr (&Sset_process_coding_system
);
7737 defsubr (&Sprocess_coding_system
);
7738 defsubr (&Sset_process_filter_multibyte
);
7739 defsubr (&Sprocess_filter_multibyte_p
);
7741 #endif /* subprocesses */
7743 defsubr (&Sget_buffer_process
);
7744 defsubr (&Sprocess_inherit_coding_system_flag
);
7745 defsubr (&Slist_system_processes
);
7746 defsubr (&Sprocess_attributes
);
7749 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7750 (do not change this comment) */