1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 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/>. */
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
41 #ifdef HAVE_INTTYPES_H
48 #if defined(WINDOWSNT) || defined(UNIX98_PTYS)
51 #endif /* not WINDOWSNT */
53 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
54 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
59 /* Are local (unix) sockets supported? */
60 #if defined (HAVE_SYS_UN_H)
61 #if !defined (AF_LOCAL) && defined (AF_UNIX)
62 #define AF_LOCAL AF_UNIX
65 #define HAVE_LOCAL_SOCKETS
69 #endif /* HAVE_SOCKETS */
71 #if defined(BSD_SYSTEM)
72 #include <sys/ioctl.h>
73 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
75 #endif /* HAVE_PTYS and no O_NDELAY */
76 #endif /* BSD_SYSTEM */
82 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
84 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
85 /* sys/ioctl.h may have been included already */
87 #include <sys/ioctl.h>
98 #include <netinet/in.h>
99 #include <arpa/nameser.h>
109 #include "character.h"
113 #include "termhooks.h"
114 #include "termopts.h"
115 #include "commands.h"
116 #include "keyboard.h"
117 #include "blockinput.h"
118 #include "dispextern.h"
119 #include "composite.h"
122 Lisp_Object Qprocessp
;
123 Lisp_Object Qrun
, Qstop
, Qsignal
;
124 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
125 Lisp_Object Qlocal
, Qipv4
, Qdatagram
;
126 Lisp_Object Qreal
, Qnetwork
, Qserial
;
130 Lisp_Object QCport
, QCspeed
, QCprocess
;
131 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
132 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
133 Lisp_Object QCname
, QCbuffer
, QChost
, QCservice
, QCtype
;
134 Lisp_Object QClocal
, QCremote
, QCcoding
;
135 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
136 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
137 Lisp_Object Qlast_nonmenu_event
;
138 /* QCfamily is declared and initialized in xfaces.c,
139 QCfilter in keyboard.c. */
140 extern Lisp_Object QCfamily
, QCfilter
;
142 /* Qexit is declared and initialized in eval.c. */
144 /* QCfamily is defined in xfaces.c. */
145 extern Lisp_Object QCfamily
;
146 /* QCfilter is defined in keyboard.c. */
147 extern Lisp_Object QCfilter
;
150 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
151 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
152 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
153 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
155 #define NETCONN_P(p) 0
156 #define NETCONN1_P(p) 0
157 #define SERIALCONN_P(p) 0
158 #define SERIALCONN1_P(p) 0
159 #endif /* HAVE_SOCKETS */
161 /* Define first descriptor number available for subprocesses. */
163 #define FIRST_PROC_DESC 1
165 #define FIRST_PROC_DESC 3
168 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
171 #if !defined (SIGCHLD) && defined (SIGCLD)
172 #define SIGCHLD SIGCLD
175 #include "syssignal.h"
179 extern char *get_operating_system_release ();
181 /* Serial processes require termios or Windows. */
182 #if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
187 /* From sysdep.c or w32.c */
188 extern int serial_open (char *port
);
189 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
196 extern char *sys_errlist
[];
203 /* t means use pty, nil means use a pipe,
204 maybe other values to come. */
205 static Lisp_Object Vprocess_connection_type
;
207 /* These next two vars are non-static since sysdep.c uses them in the
208 emulation of `select'. */
209 /* Number of events of change of status of a process. */
211 /* Number of events for which the user or sentinel has been notified. */
214 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
216 #ifdef BROKEN_NON_BLOCKING_CONNECT
217 #undef NON_BLOCKING_CONNECT
219 #ifndef NON_BLOCKING_CONNECT
222 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
223 #if defined (O_NONBLOCK) || defined (O_NDELAY)
224 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
225 #define NON_BLOCKING_CONNECT
226 #endif /* EWOULDBLOCK || EINPROGRESS */
227 #endif /* O_NONBLOCK || O_NDELAY */
228 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
229 #endif /* HAVE_SELECT */
230 #endif /* HAVE_SOCKETS */
231 #endif /* NON_BLOCKING_CONNECT */
232 #endif /* BROKEN_NON_BLOCKING_CONNECT */
234 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
235 this system. We need to read full packets, so we need a
236 "non-destructive" select. So we require either native select,
237 or emulation of select using FIONREAD. */
239 #ifdef BROKEN_DATAGRAM_SOCKETS
240 #undef DATAGRAM_SOCKETS
242 #ifndef DATAGRAM_SOCKETS
244 #if defined (HAVE_SELECT) || defined (FIONREAD)
245 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
246 #define DATAGRAM_SOCKETS
247 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
248 #endif /* HAVE_SELECT || FIONREAD */
249 #endif /* HAVE_SOCKETS */
250 #endif /* DATAGRAM_SOCKETS */
251 #endif /* BROKEN_DATAGRAM_SOCKETS */
253 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
254 #ifdef EMACS_HAS_USECS
255 #define ADAPTIVE_READ_BUFFERING
259 #ifdef ADAPTIVE_READ_BUFFERING
260 #define READ_OUTPUT_DELAY_INCREMENT 10000
261 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
262 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
264 /* Number of processes which have a non-zero read_output_delay,
265 and therefore might be delayed for adaptive read buffering. */
267 static int process_output_delay_count
;
269 /* Non-zero if any process has non-nil read_output_skip. */
271 static int process_output_skip
;
273 /* Non-nil means to delay reading process output to improve buffering.
274 A value of t means that delay is reset after each send, any other
275 non-nil value does not reset the delay. A value of nil disables
276 adaptive read buffering completely. */
277 static Lisp_Object Vprocess_adaptive_read_buffering
;
279 #define process_output_delay_count 0
283 #include "sysselect.h"
285 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
286 static void deactivate_process
P_ ((Lisp_Object
));
287 static void status_notify
P_ ((struct Lisp_Process
*));
288 static int read_process_output
P_ ((Lisp_Object
, int));
290 /* If we support a window system, turn on the code to poll periodically
291 to detect C-g. It isn't actually used when doing interrupt input. */
292 #ifdef HAVE_WINDOW_SYSTEM
293 #define POLL_FOR_INPUT
296 static Lisp_Object
get_process ();
297 static void exec_sentinel ();
299 extern EMACS_TIME
timer_check ();
300 extern int timers_run
;
302 /* Mask of bits indicating the descriptors that we wait for input on. */
304 static SELECT_TYPE input_wait_mask
;
306 /* Mask that excludes keyboard input descriptor(s). */
308 static SELECT_TYPE non_keyboard_wait_mask
;
310 /* Mask that excludes process input descriptor(s). */
312 static SELECT_TYPE non_process_wait_mask
;
314 /* Mask for the gpm mouse input descriptor. */
316 static SELECT_TYPE gpm_wait_mask
;
318 #ifdef NON_BLOCKING_CONNECT
319 /* Mask of bits indicating the descriptors that we wait for connect to
320 complete on. Once they complete, they are removed from this mask
321 and added to the input_wait_mask and non_keyboard_wait_mask. */
323 static SELECT_TYPE connect_wait_mask
;
325 /* Number of bits set in connect_wait_mask. */
326 static int num_pending_connects
;
328 #define IF_NON_BLOCKING_CONNECT(s) s
330 #define IF_NON_BLOCKING_CONNECT(s)
333 /* The largest descriptor currently in use for a process object. */
334 static int max_process_desc
;
336 /* The largest descriptor currently in use for keyboard input. */
337 static int max_keyboard_desc
;
339 /* The largest descriptor currently in use for gpm mouse input. */
340 static int max_gpm_desc
;
342 /* Nonzero means delete a process right away if it exits. */
343 static int delete_exited_processes
;
345 /* Indexed by descriptor, gives the process (if any) for that descriptor */
346 Lisp_Object chan_process
[MAXDESC
];
348 /* Alist of elements (NAME . PROCESS) */
349 Lisp_Object Vprocess_alist
;
351 /* Buffered-ahead input char from process, indexed by channel.
352 -1 means empty (no char is buffered).
353 Used on sys V where the only way to tell if there is any
354 output from the process is to read at least one char.
355 Always -1 on systems that support FIONREAD. */
357 /* Don't make static; need to access externally. */
358 int proc_buffered_char
[MAXDESC
];
360 /* Table of `struct coding-system' for each process. */
361 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
362 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
364 #ifdef DATAGRAM_SOCKETS
365 /* Table of `partner address' for datagram sockets. */
366 struct sockaddr_and_len
{
369 } datagram_address
[MAXDESC
];
370 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
371 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
373 #define DATAGRAM_CHAN_P(chan) (0)
374 #define DATAGRAM_CONN_P(proc) (0)
377 /* Maximum number of bytes to send to a pty without an eof. */
378 static int pty_max_bytes
;
380 /* Nonzero means don't run process sentinels. This is used
382 int inhibit_sentinels
;
388 /* The file name of the pty opened by allocate_pty. */
390 static char pty_name
[24];
393 /* Compute the Lisp form of the process status, p->status, from
394 the numeric status that was returned by `wait'. */
396 static Lisp_Object
status_convert (int);
400 struct Lisp_Process
*p
;
402 eassert (p
->raw_status_new
);
403 p
->status
= status_convert (p
->raw_status
);
404 p
->raw_status_new
= 0;
407 /* Convert a process status word in Unix format to
408 the list that we use internally. */
411 status_convert (int w
)
414 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
415 else if (WIFEXITED (w
))
416 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
417 WCOREDUMP (w
) ? Qt
: Qnil
));
418 else if (WIFSIGNALED (w
))
419 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
420 WCOREDUMP (w
) ? Qt
: Qnil
));
425 /* Given a status-list, extract the three pieces of information
426 and store them individually through the three pointers. */
429 decode_status (l
, symbol
, code
, coredump
)
447 *code
= XFASTINT (XCAR (tem
));
449 *coredump
= !NILP (tem
);
453 /* Return a string describing a process status list. */
457 struct Lisp_Process
*p
;
459 Lisp_Object status
= p
->status
;
462 Lisp_Object string
, string2
;
464 decode_status (status
, &symbol
, &code
, &coredump
);
466 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
469 synchronize_system_messages_locale ();
470 signame
= strsignal (code
);
473 string
= build_string (signame
);
474 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
475 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
476 return concat2 (string
, string2
);
478 else if (EQ (symbol
, Qexit
))
481 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
483 return build_string ("finished\n");
484 string
= Fnumber_to_string (make_number (code
));
485 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
486 return concat3 (build_string ("exited abnormally with code "),
489 else if (EQ (symbol
, Qfailed
))
491 string
= Fnumber_to_string (make_number (code
));
492 string2
= build_string ("\n");
493 return concat3 (build_string ("failed with code "),
497 return Fcopy_sequence (Fsymbol_name (symbol
));
502 /* Open an available pty, returning a file descriptor.
503 Return -1 on failure.
504 The file name of the terminal corresponding to the pty
505 is left in the variable pty_name. */
516 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
517 for (i
= 0; i
< 16; i
++)
520 struct stat stb
; /* Used in some PTY_OPEN. */
521 #ifdef PTY_NAME_SPRINTF
524 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
525 #endif /* no PTY_NAME_SPRINTF */
529 #else /* no PTY_OPEN */
531 { /* Some systems name their pseudoterminals so that there are gaps in
532 the usual sequence - for example, on HP9000/S700 systems, there
533 are no pseudoterminals with names ending in 'f'. So we wait for
534 three failures in a row before deciding that we've reached the
536 int failed_count
= 0;
538 if (stat (pty_name
, &stb
) < 0)
541 if (failed_count
>= 3)
548 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
550 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
553 #endif /* no PTY_OPEN */
557 /* check to make certain that both sides are available
558 this avoids a nasty yet stupid bug in rlogins */
559 #ifdef PTY_TTY_NAME_SPRINTF
562 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
563 #endif /* no PTY_TTY_NAME_SPRINTF */
564 if (access (pty_name
, 6) != 0)
579 #endif /* HAVE_PTYS */
585 register Lisp_Object val
, tem
, name1
;
586 register struct Lisp_Process
*p
;
590 p
= allocate_process ();
598 p
->raw_status_new
= 0;
600 p
->mark
= Fmake_marker ();
602 #ifdef ADAPTIVE_READ_BUFFERING
603 p
->adaptive_read_buffering
= 0;
604 p
->read_output_delay
= 0;
605 p
->read_output_skip
= 0;
608 /* If name is already in use, modify it until it is unused. */
613 tem
= Fget_process (name1
);
614 if (NILP (tem
)) break;
615 sprintf (suffix
, "<%d>", i
);
616 name1
= concat2 (name
, build_string (suffix
));
620 XSETPROCESS (val
, p
);
621 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
626 remove_process (proc
)
627 register Lisp_Object proc
;
629 register Lisp_Object pair
;
631 pair
= Frassq (proc
, Vprocess_alist
);
632 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
634 deactivate_process (proc
);
637 /* Setup coding systems of PROCESS. */
640 setup_process_coding_systems (process
)
643 struct Lisp_Process
*p
= XPROCESS (process
);
645 int outch
= p
->outfd
;
646 Lisp_Object coding_system
;
648 if (inch
< 0 || outch
< 0)
651 if (!proc_decode_coding_system
[inch
])
652 proc_decode_coding_system
[inch
]
653 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
654 coding_system
= p
->decode_coding_system
;
655 if (! NILP (p
->filter
))
657 else if (BUFFERP (p
->buffer
))
659 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
660 coding_system
= raw_text_coding_system (coding_system
);
662 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
664 if (!proc_encode_coding_system
[outch
])
665 proc_encode_coding_system
[outch
]
666 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
667 setup_coding_system (p
->encode_coding_system
,
668 proc_encode_coding_system
[outch
]);
671 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
672 doc
: /* Return t if OBJECT is a process. */)
676 return PROCESSP (object
) ? Qt
: Qnil
;
679 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
680 doc
: /* Return the process named NAME, or nil if there is none. */)
682 register Lisp_Object name
;
687 return Fcdr (Fassoc (name
, Vprocess_alist
));
690 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
691 doc
: /* Return the (or a) process associated with BUFFER.
692 BUFFER may be a buffer or the name of one. */)
694 register Lisp_Object buffer
;
696 register Lisp_Object buf
, tail
, proc
;
698 if (NILP (buffer
)) return Qnil
;
699 buf
= Fget_buffer (buffer
);
700 if (NILP (buf
)) return Qnil
;
702 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
704 proc
= Fcdr (XCAR (tail
));
705 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
711 /* This is how commands for the user decode process arguments. It
712 accepts a process, a process name, a buffer, a buffer name, or nil.
713 Buffers denote the first process in the buffer, and nil denotes the
718 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. */)
765 register Lisp_Object process
;
767 register struct Lisp_Process
*p
;
769 process
= get_process (process
);
770 p
= XPROCESS (process
);
772 p
->raw_status_new
= 0;
773 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
775 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
776 p
->tick
= ++process_tick
;
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
;
811 remove_process (process
);
815 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
816 doc
: /* Return the status of PROCESS.
817 The returned value is one of the following symbols:
818 run -- for a process that is running.
819 stop -- for a process stopped but continuable.
820 exit -- for a process that has exited.
821 signal -- for a process that has got a fatal signal.
822 open -- for a network stream connection that is open.
823 listen -- for a network stream server that is listening.
824 closed -- for a network stream connection that is closed.
825 connect -- when waiting for a non-blocking connection to complete.
826 failed -- when a non-blocking connection has failed.
827 nil -- if arg is a process name and no such process exists.
828 PROCESS may be a process, a buffer, the name of a process, or
829 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. */)
867 register Lisp_Object process
;
869 CHECK_PROCESS (process
);
870 if (XPROCESS (process
)->raw_status_new
)
871 update_status (XPROCESS (process
));
872 if (CONSP (XPROCESS (process
)->status
))
873 return XCAR (XCDR (XPROCESS (process
)->status
));
874 return make_number (0);
877 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
878 doc
: /* Return the process id of PROCESS.
879 This is the pid of the external process which PROCESS uses or talks to.
880 For a network connection, this value is nil. */)
882 register Lisp_Object process
;
884 /* Assignment to EMACS_INT stops GCC whining about limited range of
888 CHECK_PROCESS (process
);
889 pid
= XPROCESS (process
)->pid
;
890 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
893 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
894 doc
: /* Return the name of PROCESS, as a string.
895 This is the name of the program invoked in PROCESS,
896 possibly modified to make it unique among process names. */)
898 register Lisp_Object process
;
900 CHECK_PROCESS (process
);
901 return XPROCESS (process
)->name
;
904 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
905 doc
: /* Return the command that was executed to start PROCESS.
906 This is a list of strings, the first string being the program executed
907 and the rest of the strings being the arguments given to it.
908 For a network or serial process, this is nil (process is running) or t
909 \(process is stopped). */)
911 register Lisp_Object process
;
913 CHECK_PROCESS (process
);
914 return XPROCESS (process
)->command
;
917 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
918 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
919 This is the terminal that the process itself reads and writes on,
920 not the name of the pty that Emacs uses to talk with that terminal. */)
922 register Lisp_Object process
;
924 CHECK_PROCESS (process
);
925 return XPROCESS (process
)->tty_name
;
928 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
930 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
932 register Lisp_Object process
, buffer
;
934 struct Lisp_Process
*p
;
936 CHECK_PROCESS (process
);
938 CHECK_BUFFER (buffer
);
939 p
= XPROCESS (process
);
941 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
942 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
943 setup_process_coding_systems (process
);
947 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
949 doc
: /* Return the buffer PROCESS is associated with.
950 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
952 register Lisp_Object process
;
954 CHECK_PROCESS (process
);
955 return XPROCESS (process
)->buffer
;
958 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
960 doc
: /* Return the marker for the end of the last output from PROCESS. */)
962 register Lisp_Object process
;
964 CHECK_PROCESS (process
);
965 return XPROCESS (process
)->mark
;
968 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
970 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
971 A value of t means stop accepting output from the process.
973 When a process has a filter, its buffer is not used for output.
974 Instead, each time it does output, the entire string of output is
975 passed to the filter.
977 The filter gets two arguments: the process and the string of output.
978 The string argument is normally a multibyte string, except:
979 - if the process' input coding system is no-conversion or raw-text,
980 it is a unibyte string (the non-converted input), or else
981 - if `default-enable-multibyte-characters' is nil, it is a unibyte
982 string (the result of converting the decoded input multibyte
983 string to unibyte with `string-make-unibyte'). */)
985 register Lisp_Object process
, filter
;
987 struct Lisp_Process
*p
;
989 CHECK_PROCESS (process
);
990 p
= XPROCESS (process
);
992 /* Don't signal an error if the process' input file descriptor
993 is closed. This could make debugging Lisp more difficult,
994 for example when doing something like
996 (setq process (start-process ...))
998 (set-process-filter process ...) */
1002 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1004 FD_CLR (p
->infd
, &input_wait_mask
);
1005 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1007 else if (EQ (p
->filter
, Qt
)
1008 /* Network or serial process not stopped: */
1009 && !EQ (p
->command
, Qt
))
1011 FD_SET (p
->infd
, &input_wait_mask
);
1012 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1017 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1018 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1019 setup_process_coding_systems (process
);
1023 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1025 doc
: /* Returns the filter function of PROCESS; nil if none.
1026 See `set-process-filter' for more info on filter functions. */)
1028 register Lisp_Object process
;
1030 CHECK_PROCESS (process
);
1031 return XPROCESS (process
)->filter
;
1034 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1036 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1037 The sentinel is called as a function when the process changes state.
1038 It gets two arguments: the process, and a string describing the change. */)
1040 register Lisp_Object process
, sentinel
;
1042 struct Lisp_Process
*p
;
1044 CHECK_PROCESS (process
);
1045 p
= XPROCESS (process
);
1047 p
->sentinel
= sentinel
;
1048 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1049 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1053 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1055 doc
: /* Return the sentinel of PROCESS; nil if none.
1056 See `set-process-sentinel' for more info on sentinels. */)
1058 register Lisp_Object process
;
1060 CHECK_PROCESS (process
);
1061 return XPROCESS (process
)->sentinel
;
1064 DEFUN ("set-process-window-size", Fset_process_window_size
,
1065 Sset_process_window_size
, 3, 3, 0,
1066 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1067 (process
, height
, width
)
1068 register Lisp_Object process
, height
, width
;
1070 CHECK_PROCESS (process
);
1071 CHECK_NATNUM (height
);
1072 CHECK_NATNUM (width
);
1074 if (XPROCESS (process
)->infd
< 0
1075 || set_window_size (XPROCESS (process
)->infd
,
1076 XINT (height
), XINT (width
)) <= 0)
1082 DEFUN ("set-process-inherit-coding-system-flag",
1083 Fset_process_inherit_coding_system_flag
,
1084 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1085 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1086 If the second argument FLAG is non-nil, then the variable
1087 `buffer-file-coding-system' of the buffer associated with PROCESS
1088 will be bound to the value of the coding system used to decode
1091 This is useful when the coding system specified for the process buffer
1092 leaves either the character code conversion or the end-of-line conversion
1093 unspecified, or if the coding system used to decode the process output
1094 is more appropriate for saving the process buffer.
1096 Binding the variable `inherit-process-coding-system' to non-nil before
1097 starting the process is an alternative way of setting the inherit flag
1098 for the process which will run. */)
1100 register Lisp_Object process
, flag
;
1102 CHECK_PROCESS (process
);
1103 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1107 DEFUN ("process-inherit-coding-system-flag",
1108 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1110 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1111 If this flag is t, `buffer-file-coding-system' of the buffer
1112 associated with PROCESS will inherit the coding system used to decode
1113 the process output. */)
1115 register Lisp_Object process
;
1117 CHECK_PROCESS (process
);
1118 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
1121 DEFUN ("set-process-query-on-exit-flag",
1122 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1124 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1125 If the second argument FLAG is non-nil, Emacs will query the user before
1126 exiting if PROCESS is running. */)
1128 register Lisp_Object process
, flag
;
1130 CHECK_PROCESS (process
);
1131 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1135 DEFUN ("process-query-on-exit-flag",
1136 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1138 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1140 register Lisp_Object process
;
1142 CHECK_PROCESS (process
);
1143 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1146 #ifdef DATAGRAM_SOCKETS
1147 Lisp_Object
Fprocess_datagram_address ();
1150 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1152 doc
: /* Return the contact info of PROCESS; t for a real child.
1153 For a network or serial connection, the value depends on the optional
1154 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1155 SERVICE) for a network connection or (PORT SPEED) for a serial
1156 connection. If KEY is t, the complete contact information for the
1157 connection is returned, else the specific value for the keyword KEY is
1158 returned. See `make-network-process' or `make-serial-process' for a
1159 list of keywords. */)
1161 register Lisp_Object process
, key
;
1163 Lisp_Object contact
;
1165 CHECK_PROCESS (process
);
1166 contact
= XPROCESS (process
)->childp
;
1168 #ifdef DATAGRAM_SOCKETS
1169 if (DATAGRAM_CONN_P (process
)
1170 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1171 contact
= Fplist_put (contact
, QCremote
,
1172 Fprocess_datagram_address (process
));
1175 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1177 if (NILP (key
) && NETCONN_P (process
))
1178 return Fcons (Fplist_get (contact
, QChost
),
1179 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1180 if (NILP (key
) && SERIALCONN_P (process
))
1181 return Fcons (Fplist_get (contact
, QCport
),
1182 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1183 return Fplist_get (contact
, key
);
1186 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1188 doc
: /* Return the plist of PROCESS. */)
1190 register Lisp_Object process
;
1192 CHECK_PROCESS (process
);
1193 return XPROCESS (process
)->plist
;
1196 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1198 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1200 register Lisp_Object process
, plist
;
1202 CHECK_PROCESS (process
);
1205 XPROCESS (process
)->plist
= plist
;
1209 #if 0 /* Turned off because we don't currently record this info
1210 in the process. Perhaps add it. */
1211 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1212 doc
: /* Return the connection type of PROCESS.
1213 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1214 a socket connection. */)
1216 Lisp_Object process
;
1218 return XPROCESS (process
)->type
;
1222 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1223 doc
: /* Return the connection type of PROCESS.
1224 The value is either the symbol `real', `network', or `serial'.
1225 PROCESS may be a process, a buffer, the name of a process or buffer, or
1226 nil, indicating the current buffer's process. */)
1228 Lisp_Object process
;
1231 proc
= get_process (process
);
1232 return XPROCESS (proc
)->type
;
1236 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1238 doc
: /* Convert network ADDRESS from internal format to a string.
1239 A 4 or 5 element vector represents an IPv4 address (with port number).
1240 An 8 or 9 element vector represents an IPv6 address (with port number).
1241 If optional second argument OMIT-PORT is non-nil, don't include a port
1242 number in the string, even when present in ADDRESS.
1243 Returns nil if format of ADDRESS is invalid. */)
1244 (address
, omit_port
)
1245 Lisp_Object address
, omit_port
;
1250 if (STRINGP (address
)) /* AF_LOCAL */
1253 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1255 register struct Lisp_Vector
*p
= XVECTOR (address
);
1256 Lisp_Object args
[10];
1259 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1261 args
[0] = build_string ("%d.%d.%d.%d");
1264 else if (p
->size
== 5)
1266 args
[0] = build_string ("%d.%d.%d.%d:%d");
1269 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1271 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1274 else if (p
->size
== 9)
1276 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1282 for (i
= 0; i
< nargs
; i
++)
1284 EMACS_INT element
= XINT (p
->contents
[i
]);
1286 if (element
< 0 || element
> 65535)
1289 if (nargs
<= 5 /* IPv4 */
1290 && i
< 4 /* host, not port */
1294 args
[i
+1] = p
->contents
[i
];
1297 return Fformat (nargs
+1, args
);
1300 if (CONSP (address
))
1302 Lisp_Object args
[2];
1303 args
[0] = build_string ("<Family %d>");
1304 args
[1] = Fcar (address
);
1305 return Fformat (2, args
);
1313 list_processes_1 (query_only
)
1314 Lisp_Object query_only
;
1316 register Lisp_Object tail
, tem
;
1317 Lisp_Object proc
, minspace
, tem1
;
1318 register struct Lisp_Process
*p
;
1320 int w_proc
, w_buffer
, w_tty
;
1322 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1324 w_proc
= 4; /* Proc */
1325 w_buffer
= 6; /* Buffer */
1326 w_tty
= 0; /* Omit if no ttys */
1328 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1332 proc
= Fcdr (XCAR (tail
));
1333 p
= XPROCESS (proc
);
1336 if (!NILP (query_only
) && p
->kill_without_query
)
1338 if (STRINGP (p
->name
)
1339 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1341 if (!NILP (p
->buffer
))
1343 if (NILP (XBUFFER (p
->buffer
)->name
))
1346 w_buffer
= 8; /* (Killed) */
1348 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1351 if (STRINGP (p
->tty_name
)
1352 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1356 XSETFASTINT (i_status
, w_proc
+ 1);
1357 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1360 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1361 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1366 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1369 XSETFASTINT (minspace
, 1);
1371 set_buffer_internal (XBUFFER (Vstandard_output
));
1372 current_buffer
->undo_list
= Qt
;
1374 current_buffer
->truncate_lines
= Qt
;
1376 write_string ("Proc", -1);
1377 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1378 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1381 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1383 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1384 write_string ("\n", -1);
1386 write_string ("----", -1);
1387 Findent_to (i_status
, minspace
); write_string ("------", -1);
1388 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1391 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1393 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1394 write_string ("\n", -1);
1396 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1400 proc
= Fcdr (XCAR (tail
));
1401 p
= XPROCESS (proc
);
1404 if (!NILP (query_only
) && p
->kill_without_query
)
1407 Finsert (1, &p
->name
);
1408 Findent_to (i_status
, minspace
);
1410 if (p
->raw_status_new
)
1413 if (CONSP (p
->status
))
1414 symbol
= XCAR (p
->status
);
1416 if (EQ (symbol
, Qsignal
))
1419 tem
= Fcar (Fcdr (p
->status
));
1421 if (XINT (tem
) < NSIG
)
1422 write_string (sys_errlist
[XINT (tem
)], -1);
1425 Fprinc (symbol
, Qnil
);
1427 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1429 if (EQ (symbol
, Qexit
))
1430 write_string ("closed", -1);
1431 else if (EQ (p
->command
, Qt
))
1432 write_string ("stopped", -1);
1433 else if (EQ (symbol
, Qrun
))
1434 write_string ("open", -1);
1436 Fprinc (symbol
, Qnil
);
1438 else if (SERIALCONN1_P (p
))
1440 write_string ("running", -1);
1443 Fprinc (symbol
, Qnil
);
1445 if (EQ (symbol
, Qexit
))
1448 tem
= Fcar (Fcdr (p
->status
));
1451 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1452 write_string (tembuf
, -1);
1456 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1459 Findent_to (i_buffer
, minspace
);
1460 if (NILP (p
->buffer
))
1461 insert_string ("(none)");
1462 else if (NILP (XBUFFER (p
->buffer
)->name
))
1463 insert_string ("(Killed)");
1465 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1469 Findent_to (i_tty
, minspace
);
1470 if (STRINGP (p
->tty_name
))
1471 Finsert (1, &p
->tty_name
);
1474 Findent_to (i_command
, minspace
);
1476 if (EQ (p
->status
, Qlisten
))
1478 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1479 if (INTEGERP (port
))
1480 port
= Fnumber_to_string (port
);
1482 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1483 sprintf (tembuf
, "(network %s server on %s)\n",
1484 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1485 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1486 insert_string (tembuf
);
1488 else if (NETCONN1_P (p
))
1490 /* For a local socket, there is no host name,
1491 so display service instead. */
1492 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1493 if (!STRINGP (host
))
1495 host
= Fplist_get (p
->childp
, QCservice
);
1496 if (INTEGERP (host
))
1497 host
= Fnumber_to_string (host
);
1500 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1501 sprintf (tembuf
, "(network %s connection to %s)\n",
1502 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1503 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1504 insert_string (tembuf
);
1506 else if (SERIALCONN1_P (p
))
1508 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1509 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1510 insert_string ("(serial port ");
1512 insert_string (SDATA (port
));
1514 insert_string ("?");
1515 if (INTEGERP (speed
))
1517 sprintf (tembuf
, " at %d b/s", XINT (speed
));
1518 insert_string (tembuf
);
1520 insert_string (")\n");
1532 insert_string (" ");
1534 insert_string ("\n");
1538 status_notify (NULL
);
1542 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1543 doc
: /* Display a list of all processes.
1544 If optional argument QUERY-ONLY is non-nil, only processes with
1545 the query-on-exit flag set will be listed.
1546 Any process listed as exited or signaled is actually eliminated
1547 after the listing is made. */)
1549 Lisp_Object query_only
;
1551 internal_with_output_to_temp_buffer ("*Process List*",
1552 list_processes_1
, query_only
);
1556 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1557 doc
: /* Return a list of all processes. */)
1560 return Fmapcar (Qcdr
, Vprocess_alist
);
1563 /* Starting asynchronous inferior processes. */
1565 static Lisp_Object
start_process_unwind ();
1567 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1568 doc
: /* Start a program in a subprocess. Return the process object for it.
1569 NAME is name for process. It is modified if necessary to make it unique.
1570 BUFFER is the buffer (or buffer name) to associate with the process.
1572 Process output (both standard output and standard error streams) goes
1573 at end of BUFFER, unless you specify an output stream or filter
1574 function to handle the output. BUFFER may also be nil, meaning that
1575 this process is not associated with any buffer.
1577 PROGRAM is the program file name. It is searched for in PATH.
1578 Remaining arguments are strings to give program as arguments.
1580 If you want to separate standard output from standard error, invoke
1581 the command through a shell and redirect one of them using the shell
1584 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1587 register Lisp_Object
*args
;
1589 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1591 register unsigned char *new_argv
;
1594 register unsigned char **new_argv
;
1597 int count
= SPECPDL_INDEX ();
1601 buffer
= Fget_buffer_create (buffer
);
1603 /* Make sure that the child will be able to chdir to the current
1604 buffer's current directory, or its unhandled equivalent. We
1605 can't just have the child check for an error when it does the
1606 chdir, since it's in a vfork.
1608 We have to GCPRO around this because Fexpand_file_name and
1609 Funhandled_file_name_directory might call a file name handling
1610 function. The argument list is protected by the caller, so all
1611 we really have to worry about is buffer. */
1613 struct gcpro gcpro1
, gcpro2
;
1615 current_dir
= current_buffer
->directory
;
1617 GCPRO2 (buffer
, current_dir
);
1619 current_dir
= Funhandled_file_name_directory (current_dir
);
1620 if (NILP (current_dir
))
1621 /* If the file name handler says that current_dir is unreachable, use
1622 a sensible default. */
1623 current_dir
= build_string ("~/");
1624 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1625 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1626 report_file_error ("Setting current directory",
1627 Fcons (current_buffer
->directory
, Qnil
));
1633 CHECK_STRING (name
);
1637 CHECK_STRING (program
);
1639 proc
= make_process (name
);
1640 /* If an error occurs and we can't start the process, we want to
1641 remove it from the process list. This means that each error
1642 check in create_process doesn't need to call remove_process
1643 itself; it's all taken care of here. */
1644 record_unwind_protect (start_process_unwind
, proc
);
1646 XPROCESS (proc
)->childp
= Qt
;
1647 XPROCESS (proc
)->plist
= Qnil
;
1648 XPROCESS (proc
)->type
= Qreal
;
1649 XPROCESS (proc
)->buffer
= buffer
;
1650 XPROCESS (proc
)->sentinel
= Qnil
;
1651 XPROCESS (proc
)->filter
= Qnil
;
1652 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1654 #ifdef ADAPTIVE_READ_BUFFERING
1655 XPROCESS (proc
)->adaptive_read_buffering
1656 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1657 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1660 /* Make the process marker point into the process buffer (if any). */
1661 if (BUFFERP (buffer
))
1662 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1663 BUF_ZV (XBUFFER (buffer
)),
1664 BUF_ZV_BYTE (XBUFFER (buffer
)));
1667 /* Decide coding systems for communicating with the process. Here
1668 we don't setup the structure coding_system nor pay attention to
1669 unibyte mode. They are done in create_process. */
1671 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1672 Lisp_Object coding_systems
= Qt
;
1673 Lisp_Object val
, *args2
;
1674 struct gcpro gcpro1
, gcpro2
;
1676 val
= Vcoding_system_for_read
;
1679 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1680 args2
[0] = Qstart_process
;
1681 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1682 GCPRO2 (proc
, current_dir
);
1683 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1685 if (CONSP (coding_systems
))
1686 val
= XCAR (coding_systems
);
1687 else if (CONSP (Vdefault_process_coding_system
))
1688 val
= XCAR (Vdefault_process_coding_system
);
1690 XPROCESS (proc
)->decode_coding_system
= val
;
1692 val
= Vcoding_system_for_write
;
1695 if (EQ (coding_systems
, Qt
))
1697 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1698 args2
[0] = Qstart_process
;
1699 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1700 GCPRO2 (proc
, current_dir
);
1701 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1704 if (CONSP (coding_systems
))
1705 val
= XCDR (coding_systems
);
1706 else if (CONSP (Vdefault_process_coding_system
))
1707 val
= XCDR (Vdefault_process_coding_system
);
1709 XPROCESS (proc
)->encode_coding_system
= val
;
1713 /* Make a one member argv with all args concatenated
1714 together separated by a blank. */
1715 len
= SBYTES (program
) + 2;
1716 for (i
= 3; i
< nargs
; i
++)
1720 len
+= SBYTES (tem
) + 1; /* count the blank */
1722 new_argv
= (unsigned char *) alloca (len
);
1723 strcpy (new_argv
, SDATA (program
));
1724 for (i
= 3; i
< nargs
; i
++)
1728 strcat (new_argv
, " ");
1729 strcat (new_argv
, SDATA (tem
));
1731 /* Need to add code here to check for program existence on VMS */
1734 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1736 /* If program file name is not absolute, search our path for it.
1737 Put the name we will really use in TEM. */
1738 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1739 && !(SCHARS (program
) > 1
1740 && IS_DEVICE_SEP (SREF (program
, 1))))
1742 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1745 GCPRO4 (name
, program
, buffer
, current_dir
);
1746 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1749 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1750 tem
= Fexpand_file_name (tem
, Qnil
);
1754 if (!NILP (Ffile_directory_p (program
)))
1755 error ("Specified program for new process is a directory");
1759 /* If program file name starts with /: for quoting a magic name,
1761 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1762 && SREF (tem
, 1) == ':')
1763 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1765 /* Encode the file name and put it in NEW_ARGV.
1766 That's where the child will use it to execute the program. */
1767 tem
= ENCODE_FILE (tem
);
1768 new_argv
[0] = SDATA (tem
);
1770 /* Here we encode arguments by the coding system used for sending
1771 data to the process. We don't support using different coding
1772 systems for encoding arguments and for encoding data sent to the
1775 for (i
= 3; i
< nargs
; i
++)
1779 if (STRING_MULTIBYTE (tem
))
1780 tem
= (code_convert_string_norecord
1781 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1782 new_argv
[i
- 2] = SDATA (tem
);
1784 new_argv
[i
- 2] = 0;
1785 #endif /* not VMS */
1787 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1788 XPROCESS (proc
)->decoding_carryover
= 0;
1789 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1791 XPROCESS (proc
)->inherit_coding_system_flag
1792 = !(NILP (buffer
) || !inherit_process_coding_system
);
1794 create_process (proc
, (char **) new_argv
, current_dir
);
1796 return unbind_to (count
, proc
);
1799 /* This function is the unwind_protect form for Fstart_process. If
1800 PROC doesn't have its pid set, then we know someone has signaled
1801 an error and the process wasn't started successfully, so we should
1802 remove it from the process list. */
1804 start_process_unwind (proc
)
1807 if (!PROCESSP (proc
))
1810 /* Was PROC started successfully? */
1811 if (XPROCESS (proc
)->pid
<= 0)
1812 remove_process (proc
);
1818 create_process_1 (timer
)
1819 struct atimer
*timer
;
1821 /* Nothing to do. */
1825 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1828 /* Mimic blocking of signals on system V, which doesn't really have it. */
1830 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1831 int sigchld_deferred
;
1834 create_process_sigchld ()
1836 signal (SIGCHLD
, create_process_sigchld
);
1838 sigchld_deferred
= 1;
1844 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1846 create_process (process
, new_argv
, current_dir
)
1847 Lisp_Object process
;
1849 Lisp_Object current_dir
;
1851 int inchannel
, outchannel
;
1854 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1855 int wait_child_setup
[2];
1857 #ifdef POSIX_SIGNALS
1860 struct sigaction sigint_action
;
1861 struct sigaction sigquit_action
;
1863 struct sigaction sighup_action
;
1865 #else /* !POSIX_SIGNALS */
1868 SIGTYPE (*sigchld
)();
1871 #endif /* !POSIX_SIGNALS */
1872 /* Use volatile to protect variables from being clobbered by longjmp. */
1873 volatile int forkin
, forkout
;
1874 volatile int pty_flag
= 0;
1876 extern char **environ
;
1879 inchannel
= outchannel
= -1;
1882 if (!NILP (Vprocess_connection_type
))
1883 outchannel
= inchannel
= allocate_pty ();
1887 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1888 /* On most USG systems it does not work to open the pty's tty here,
1889 then close it and reopen it in the child. */
1891 /* Don't let this terminal become our controlling terminal
1892 (in case we don't have one). */
1893 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1895 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1898 report_file_error ("Opening pty", Qnil
);
1899 #if defined (DONT_REOPEN_PTY)
1900 /* In the case that vfork is defined as fork, the parent process
1901 (Emacs) may send some data before the child process completes
1902 tty options setup. So we setup tty before forking. */
1903 child_setup_tty (forkout
);
1904 #endif /* DONT_REOPEN_PTY */
1906 forkin
= forkout
= -1;
1907 #endif /* not USG, or USG_SUBTTY_WORKS */
1911 #endif /* HAVE_PTYS */
1916 report_file_error ("Creating pipe", Qnil
);
1922 emacs_close (inchannel
);
1923 emacs_close (forkout
);
1924 report_file_error ("Creating pipe", Qnil
);
1930 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1934 tem
= pipe (wait_child_setup
);
1936 report_file_error ("Creating pipe", Qnil
);
1937 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1939 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1942 emacs_close (wait_child_setup
[0]);
1943 emacs_close (wait_child_setup
[1]);
1944 report_file_error ("Setting file descriptor flags", Qnil
);
1950 /* Replaced by close_process_descs */
1951 set_exclusive_use (inchannel
);
1952 set_exclusive_use (outchannel
);
1956 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1957 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1960 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1961 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1965 /* Record this as an active process, with its channels.
1966 As a result, child_setup will close Emacs's side of the pipes. */
1967 chan_process
[inchannel
] = process
;
1968 XPROCESS (process
)->infd
= inchannel
;
1969 XPROCESS (process
)->outfd
= outchannel
;
1971 /* Previously we recorded the tty descriptor used in the subprocess.
1972 It was only used for getting the foreground tty process, so now
1973 we just reopen the device (see emacs_get_tty_pgrp) as this is
1974 more portable (see USG_SUBTTY_WORKS above). */
1976 XPROCESS (process
)->pty_flag
= pty_flag
;
1977 XPROCESS (process
)->status
= Qrun
;
1978 setup_process_coding_systems (process
);
1980 /* Delay interrupts until we have a chance to store
1981 the new fork's pid in its process structure */
1982 #ifdef POSIX_SIGNALS
1983 sigemptyset (&blocked
);
1985 sigaddset (&blocked
, SIGCHLD
);
1987 #ifdef HAVE_WORKING_VFORK
1988 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1989 this sets the parent's signal handlers as well as the child's.
1990 So delay all interrupts whose handlers the child might munge,
1991 and record the current handlers so they can be restored later. */
1992 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1993 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1995 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1997 #endif /* HAVE_WORKING_VFORK */
1998 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1999 #else /* !POSIX_SIGNALS */
2001 #if defined (BSD_SYSTEM) || defined (HPUX)
2002 sigsetmask (sigmask (SIGCHLD
));
2003 #else /* ordinary USG */
2005 sigchld_deferred
= 0;
2006 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
2008 #endif /* ordinary USG */
2009 #endif /* SIGCHLD */
2010 #endif /* !POSIX_SIGNALS */
2012 FD_SET (inchannel
, &input_wait_mask
);
2013 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2014 if (inchannel
> max_process_desc
)
2015 max_process_desc
= inchannel
;
2017 /* Until we store the proper pid, enable sigchld_handler
2018 to recognize an unknown pid as standing for this process.
2019 It is very important not to let this `marker' value stay
2020 in the table after this function has returned; if it does
2021 it might cause call-process to hang and subsequent asynchronous
2022 processes to get their return values scrambled. */
2023 XPROCESS (process
)->pid
= -1;
2028 /* child_setup must clobber environ on systems with true vfork.
2029 Protect it from permanent change. */
2030 char **save_environ
= environ
;
2032 current_dir
= ENCODE_FILE (current_dir
);
2037 #endif /* not WINDOWSNT */
2039 int xforkin
= forkin
;
2040 int xforkout
= forkout
;
2042 #if 0 /* This was probably a mistake--it duplicates code later on,
2043 but fails to handle all the cases. */
2044 /* Make sure SIGCHLD is not blocked in the child. */
2045 sigsetmask (SIGEMPTYMASK
);
2048 /* Make the pty be the controlling terminal of the process. */
2050 /* First, disconnect its current controlling terminal. */
2052 /* We tried doing setsid only if pty_flag, but it caused
2053 process_set_signal to fail on SGI when using a pipe. */
2055 /* Make the pty's terminal the controlling terminal. */
2059 /* We ignore the return value
2060 because faith@cs.unc.edu says that is necessary on Linux. */
2061 ioctl (xforkin
, TIOCSCTTY
, 0);
2064 #else /* not HAVE_SETSID */
2066 /* It's very important to call setpgrp here and no time
2067 afterwards. Otherwise, we lose our controlling tty which
2068 is set when we open the pty. */
2071 #endif /* not HAVE_SETSID */
2072 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2073 if (pty_flag
&& xforkin
>= 0)
2076 tcgetattr (xforkin
, &t
);
2078 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2079 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2082 #if defined (NTTYDISC) && defined (TIOCSETD)
2083 if (pty_flag
&& xforkin
>= 0)
2085 /* Use new line discipline. */
2086 int ldisc
= NTTYDISC
;
2087 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2092 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2093 can do TIOCSPGRP only to the process's controlling tty. */
2096 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2097 I can't test it since I don't have 4.3. */
2098 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2099 ioctl (j
, TIOCNOTTY
, 0);
2102 /* In order to get a controlling terminal on some versions
2103 of BSD, it is necessary to put the process in pgrp 0
2104 before it opens the terminal. */
2112 #endif /* TIOCNOTTY */
2114 #if !defined (DONT_REOPEN_PTY)
2115 /*** There is a suggestion that this ought to be a
2116 conditional on TIOCSPGRP,
2117 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2118 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2119 that system does seem to need this code, even though
2120 both HAVE_SETSID and TIOCSCTTY are defined. */
2121 /* Now close the pty (if we had it open) and reopen it.
2122 This makes the pty the controlling terminal of the subprocess. */
2126 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2129 emacs_close (xforkin
);
2130 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2134 emacs_write (1, "Couldn't open the pty terminal ", 31);
2135 emacs_write (1, pty_name
, strlen (pty_name
));
2136 emacs_write (1, "\n", 1);
2141 #endif /* not DONT_REOPEN_PTY */
2143 #ifdef SETUP_SLAVE_PTY
2148 #endif /* SETUP_SLAVE_PTY */
2150 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2151 Now reenable it in the child, so it will die when we want it to. */
2153 signal (SIGHUP
, SIG_DFL
);
2155 #endif /* HAVE_PTYS */
2157 signal (SIGINT
, SIG_DFL
);
2158 signal (SIGQUIT
, SIG_DFL
);
2160 /* Stop blocking signals in the child. */
2161 #ifdef POSIX_SIGNALS
2162 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2163 #else /* !POSIX_SIGNALS */
2165 #if defined (BSD_SYSTEM) || defined (HPUX)
2166 sigsetmask (SIGEMPTYMASK
);
2167 #else /* ordinary USG */
2169 signal (SIGCHLD
, sigchld
);
2171 #endif /* ordinary USG */
2172 #endif /* SIGCHLD */
2173 #endif /* !POSIX_SIGNALS */
2175 #if !defined (DONT_REOPEN_PTY)
2177 child_setup_tty (xforkout
);
2178 #endif /* not DONT_REOPEN_PTY */
2180 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2181 new_argv
, 1, current_dir
);
2182 #else /* not WINDOWSNT */
2184 emacs_close (wait_child_setup
[0]);
2186 child_setup (xforkin
, xforkout
, xforkout
,
2187 new_argv
, 1, current_dir
);
2188 #endif /* not WINDOWSNT */
2190 environ
= save_environ
;
2195 /* This runs in the Emacs process. */
2199 emacs_close (forkin
);
2200 if (forkin
!= forkout
&& forkout
>= 0)
2201 emacs_close (forkout
);
2205 /* vfork succeeded. */
2206 XPROCESS (process
)->pid
= pid
;
2209 register_child (pid
, inchannel
);
2210 #endif /* WINDOWSNT */
2212 /* If the subfork execv fails, and it exits,
2213 this close hangs. I don't know why.
2214 So have an interrupt jar it loose. */
2216 struct atimer
*timer
;
2220 EMACS_SET_SECS_USECS (offset
, 1, 0);
2221 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2224 emacs_close (forkin
);
2226 cancel_atimer (timer
);
2230 if (forkin
!= forkout
&& forkout
>= 0)
2231 emacs_close (forkout
);
2235 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2238 XPROCESS (process
)->tty_name
= Qnil
;
2240 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2241 /* Wait for child_setup to complete in case that vfork is
2242 actually defined as fork. The descriptor wait_child_setup[1]
2243 of a pipe is closed at the child side either by close-on-exec
2244 on successful execvp or the _exit call in child_setup. */
2248 emacs_close (wait_child_setup
[1]);
2249 emacs_read (wait_child_setup
[0], &dummy
, 1);
2250 emacs_close (wait_child_setup
[0]);
2255 /* Restore the signal state whether vfork succeeded or not.
2256 (We will signal an error, below, if it failed.) */
2257 #ifdef POSIX_SIGNALS
2258 #ifdef HAVE_WORKING_VFORK
2259 /* Restore the parent's signal handlers. */
2260 sigaction (SIGINT
, &sigint_action
, 0);
2261 sigaction (SIGQUIT
, &sigquit_action
, 0);
2263 sigaction (SIGHUP
, &sighup_action
, 0);
2265 #endif /* HAVE_WORKING_VFORK */
2266 /* Stop blocking signals in the parent. */
2267 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2268 #else /* !POSIX_SIGNALS */
2270 #if defined (BSD_SYSTEM) || defined (HPUX)
2271 sigsetmask (SIGEMPTYMASK
);
2272 #else /* ordinary USG */
2274 signal (SIGCHLD
, sigchld
);
2275 /* Now really handle any of these signals
2276 that came in during this function. */
2277 if (sigchld_deferred
)
2278 kill (getpid (), SIGCHLD
);
2280 #endif /* ordinary USG */
2281 #endif /* SIGCHLD */
2282 #endif /* !POSIX_SIGNALS */
2284 /* Now generate the error if vfork failed. */
2286 report_file_error ("Doing vfork", Qnil
);
2288 #endif /* not VMS */
2293 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2294 The address family of sa is not included in the result. */
2297 conv_sockaddr_to_lisp (sa
, len
)
2298 struct sockaddr
*sa
;
2301 Lisp_Object address
;
2304 register struct Lisp_Vector
*p
;
2306 switch (sa
->sa_family
)
2310 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2311 len
= sizeof (sin
->sin_addr
) + 1;
2312 address
= Fmake_vector (make_number (len
), Qnil
);
2313 p
= XVECTOR (address
);
2314 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2315 cp
= (unsigned char *)&sin
->sin_addr
;
2321 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2322 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2323 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2324 address
= Fmake_vector (make_number (len
), Qnil
);
2325 p
= XVECTOR (address
);
2326 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2327 for (i
= 0; i
< len
; i
++)
2328 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2332 #ifdef HAVE_LOCAL_SOCKETS
2335 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2336 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2337 if (sockun
->sun_path
[i
] == 0)
2339 return make_unibyte_string (sockun
->sun_path
, i
);
2343 len
-= sizeof (sa
->sa_family
);
2344 address
= Fcons (make_number (sa
->sa_family
),
2345 Fmake_vector (make_number (len
), Qnil
));
2346 p
= XVECTOR (XCDR (address
));
2347 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2353 p
->contents
[i
++] = make_number (*cp
++);
2359 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2362 get_lisp_to_sockaddr_size (address
, familyp
)
2363 Lisp_Object address
;
2366 register struct Lisp_Vector
*p
;
2368 if (VECTORP (address
))
2370 p
= XVECTOR (address
);
2374 return sizeof (struct sockaddr_in
);
2377 else if (p
->size
== 9)
2379 *familyp
= AF_INET6
;
2380 return sizeof (struct sockaddr_in6
);
2384 #ifdef HAVE_LOCAL_SOCKETS
2385 else if (STRINGP (address
))
2387 *familyp
= AF_LOCAL
;
2388 return sizeof (struct sockaddr_un
);
2391 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2393 struct sockaddr
*sa
;
2394 *familyp
= XINT (XCAR (address
));
2395 p
= XVECTOR (XCDR (address
));
2396 return p
->size
+ sizeof (sa
->sa_family
);
2401 /* Convert an address object (vector or string) to an internal sockaddr.
2403 The address format has been basically validated by
2404 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2405 it could have come from user data. So if FAMILY is not valid,
2406 we return after zeroing *SA. */
2409 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2411 Lisp_Object address
;
2412 struct sockaddr
*sa
;
2415 register struct Lisp_Vector
*p
;
2416 register unsigned char *cp
= NULL
;
2421 if (VECTORP (address
))
2423 p
= XVECTOR (address
);
2424 if (family
== AF_INET
)
2426 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2427 len
= sizeof (sin
->sin_addr
) + 1;
2428 i
= XINT (p
->contents
[--len
]);
2429 sin
->sin_port
= htons (i
);
2430 cp
= (unsigned char *)&sin
->sin_addr
;
2431 sa
->sa_family
= family
;
2434 else if (family
== AF_INET6
)
2436 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2437 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2438 len
= sizeof (sin6
->sin6_addr
) + 1;
2439 i
= XINT (p
->contents
[--len
]);
2440 sin6
->sin6_port
= htons (i
);
2441 for (i
= 0; i
< len
; i
++)
2442 if (INTEGERP (p
->contents
[i
]))
2444 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2447 sa
->sa_family
= family
;
2452 else if (STRINGP (address
))
2454 #ifdef HAVE_LOCAL_SOCKETS
2455 if (family
== AF_LOCAL
)
2457 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2458 cp
= SDATA (address
);
2459 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2460 sockun
->sun_path
[i
] = *cp
++;
2461 sa
->sa_family
= family
;
2468 p
= XVECTOR (XCDR (address
));
2469 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2472 for (i
= 0; i
< len
; i
++)
2473 if (INTEGERP (p
->contents
[i
]))
2474 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2477 #ifdef DATAGRAM_SOCKETS
2478 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2480 doc
: /* Get the current datagram address associated with PROCESS. */)
2482 Lisp_Object process
;
2486 CHECK_PROCESS (process
);
2488 if (!DATAGRAM_CONN_P (process
))
2491 channel
= XPROCESS (process
)->infd
;
2492 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2493 datagram_address
[channel
].len
);
2496 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2498 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2499 Returns nil upon error setting address, ADDRESS otherwise. */)
2501 Lisp_Object process
, address
;
2506 CHECK_PROCESS (process
);
2508 if (!DATAGRAM_CONN_P (process
))
2511 channel
= XPROCESS (process
)->infd
;
2513 len
= get_lisp_to_sockaddr_size (address
, &family
);
2514 if (datagram_address
[channel
].len
!= len
)
2516 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2522 static struct socket_options
{
2523 /* The name of this option. Should be lowercase version of option
2524 name without SO_ prefix. */
2526 /* Option level SOL_... */
2528 /* Option number SO_... */
2530 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2531 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2532 } socket_options
[] =
2534 #ifdef SO_BINDTODEVICE
2535 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2538 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2541 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2544 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2547 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2550 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2553 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2556 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2558 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2561 /* Set option OPT to value VAL on socket S.
2563 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2564 Signals an error if setting a known option fails.
2568 set_socket_option (s
, opt
, val
)
2570 Lisp_Object opt
, val
;
2573 struct socket_options
*sopt
;
2578 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2579 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2580 if (strcmp (name
, sopt
->name
) == 0)
2583 switch (sopt
->opttype
)
2588 optval
= NILP (val
) ? 0 : 1;
2589 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2590 &optval
, sizeof (optval
));
2598 optval
= XINT (val
);
2600 error ("Bad option value for %s", name
);
2601 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2602 &optval
, sizeof (optval
));
2606 #ifdef SO_BINDTODEVICE
2609 char devname
[IFNAMSIZ
+1];
2611 /* This is broken, at least in the Linux 2.4 kernel.
2612 To unbind, the arg must be a zero integer, not the empty string.
2613 This should work on all systems. KFS. 2003-09-23. */
2614 bzero (devname
, sizeof devname
);
2617 char *arg
= (char *) SDATA (val
);
2618 int len
= min (strlen (arg
), IFNAMSIZ
);
2619 bcopy (arg
, devname
, len
);
2621 else if (!NILP (val
))
2622 error ("Bad option value for %s", name
);
2623 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2632 struct linger linger
;
2635 linger
.l_linger
= 0;
2637 linger
.l_linger
= XINT (val
);
2639 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2640 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2641 &linger
, sizeof (linger
));
2651 report_file_error ("Cannot set network option",
2652 Fcons (opt
, Fcons (val
, Qnil
)));
2653 return (1 << sopt
->optbit
);
2657 DEFUN ("set-network-process-option",
2658 Fset_network_process_option
, Sset_network_process_option
,
2660 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2661 See `make-network-process' for a list of options and values.
2662 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2663 OPTION is not a supported option, return nil instead; otherwise return t. */)
2664 (process
, option
, value
, no_error
)
2665 Lisp_Object process
, option
, value
;
2666 Lisp_Object no_error
;
2669 struct Lisp_Process
*p
;
2671 CHECK_PROCESS (process
);
2672 p
= XPROCESS (process
);
2673 if (!NETCONN1_P (p
))
2674 error ("Process is not a network process");
2678 error ("Process is not running");
2680 if (set_socket_option (s
, option
, value
))
2682 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2686 if (NILP (no_error
))
2687 error ("Unknown or unsupported option");
2694 DEFUN ("serial-process-configure",
2695 Fserial_process_configure
,
2696 Sserial_process_configure
,
2698 doc
: /* Configure speed, bytesize, etc. of a serial process.
2700 Arguments are specified as keyword/argument pairs. Attributes that
2701 are not given are re-initialized from the process's current
2702 configuration (available via the function `process-contact') or set to
2703 reasonable default values. The following arguments are defined:
2709 -- Any of these arguments can be given to identify the process that is
2710 to be configured. If none of these arguments is given, the current
2711 buffer's process is used.
2713 :speed SPEED -- SPEED is the speed of the serial port in bits per
2714 second, also called baud rate. Any value can be given for SPEED, but
2715 most serial ports work only at a few defined values between 1200 and
2716 115200, with 9600 being the most common value. If SPEED is nil, the
2717 serial port is not configured any further, i.e., all other arguments
2718 are ignored. This may be useful for special serial ports such as
2719 Bluetooth-to-serial converters which can only be configured through AT
2720 commands. A value of nil for SPEED can be used only when passed
2721 through `make-serial-process' or `serial-term'.
2723 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2724 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2726 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2727 `odd' (use odd parity), or the symbol `even' (use even parity). If
2728 PARITY is not given, no parity is used.
2730 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2731 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2732 is not given or nil, 1 stopbit is used.
2734 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2735 flowcontrol to be used, which is either nil (don't use flowcontrol),
2736 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2737 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2738 flowcontrol is used.
2740 `serial-process-configure' is called by `make-serial-process' for the
2741 initial configuration of the serial port.
2745 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2747 \(serial-process-configure
2748 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2750 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2752 usage: (serial-process-configure &rest ARGS) */)
2757 struct Lisp_Process
*p
;
2758 Lisp_Object contact
= Qnil
;
2759 Lisp_Object proc
= Qnil
;
2760 struct gcpro gcpro1
;
2762 contact
= Flist (nargs
, args
);
2765 proc
= Fplist_get (contact
, QCprocess
);
2767 proc
= Fplist_get (contact
, QCname
);
2769 proc
= Fplist_get (contact
, QCbuffer
);
2771 proc
= Fplist_get (contact
, QCport
);
2772 proc
= get_process (proc
);
2773 p
= XPROCESS (proc
);
2774 if (!EQ (p
->type
, Qserial
))
2775 error ("Not a serial process");
2777 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2783 serial_configure (p
, contact
);
2789 /* Used by make-serial-process to recover from errors. */
2790 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2792 if (!PROCESSP (proc
))
2794 remove_process (proc
);
2798 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2800 doc
: /* Create and return a serial port process.
2802 In Emacs, serial port connections are represented by process objects,
2803 so input and output work as for subprocesses, and `delete-process'
2804 closes a serial port connection. However, a serial process has no
2805 process id, it cannot be signaled, and the status codes are different
2806 from normal processes.
2808 `make-serial-process' creates a process and a buffer, on which you
2809 probably want to use `process-send-string'. Try \\[serial-term] for
2810 an interactive terminal. See below for examples.
2812 Arguments are specified as keyword/argument pairs. The following
2813 arguments are defined:
2815 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2816 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2817 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2818 the backslashes in strings).
2820 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2821 which is called by `make-serial-process'.
2823 :name NAME -- NAME is the name of the process. If NAME is not given,
2824 the value of PORT is used.
2826 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2827 with the process. Process output goes at the end of that buffer,
2828 unless you specify an output stream or filter function to handle the
2829 output. If BUFFER is not given, the value of NAME is used.
2831 :coding CODING -- If CODING is a symbol, it specifies the coding
2832 system used for both reading and writing for this process. If CODING
2833 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2834 ENCODING is used for writing.
2836 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2837 the process is running. If BOOL is not given, query before exiting.
2839 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2840 In the stopped state, a serial process does not accept incoming data,
2841 but you can send outgoing data. The stopped state is cleared by
2842 `continue-process' and set by `stop-process'.
2844 :filter FILTER -- Install FILTER as the process filter.
2846 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2848 :plist PLIST -- Install PLIST as the initial plist of the process.
2855 -- These arguments are handled by `serial-process-configure', which is
2856 called by `make-serial-process'.
2858 The original argument list, possibly modified by later configuration,
2859 is available via the function `process-contact'.
2863 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2865 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2867 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2869 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2871 usage: (make-serial-process &rest ARGS) */)
2877 Lisp_Object proc
, contact
, port
;
2878 struct Lisp_Process
*p
;
2879 struct gcpro gcpro1
;
2880 Lisp_Object name
, buffer
;
2881 Lisp_Object tem
, val
;
2882 int specpdl_count
= -1;
2887 contact
= Flist (nargs
, args
);
2890 port
= Fplist_get (contact
, QCport
);
2892 error ("No port specified");
2893 CHECK_STRING (port
);
2895 if (NILP (Fplist_member (contact
, QCspeed
)))
2896 error (":speed not specified");
2897 if (!NILP (Fplist_get (contact
, QCspeed
)))
2898 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2900 name
= Fplist_get (contact
, QCname
);
2903 CHECK_STRING (name
);
2904 proc
= make_process (name
);
2905 specpdl_count
= SPECPDL_INDEX ();
2906 record_unwind_protect (make_serial_process_unwind
, proc
);
2907 p
= XPROCESS (proc
);
2909 fd
= serial_open ((char*) SDATA (port
));
2912 if (fd
> max_process_desc
)
2913 max_process_desc
= fd
;
2914 chan_process
[fd
] = proc
;
2916 buffer
= Fplist_get (contact
, QCbuffer
);
2919 buffer
= Fget_buffer_create (buffer
);
2922 p
->childp
= contact
;
2923 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2925 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2926 p
->filter
= Fplist_get (contact
, QCfilter
);
2928 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2929 p
->kill_without_query
= 1;
2930 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2934 if (!EQ (p
->command
, Qt
))
2936 FD_SET (fd
, &input_wait_mask
);
2937 FD_SET (fd
, &non_keyboard_wait_mask
);
2940 if (BUFFERP (buffer
))
2942 set_marker_both (p
->mark
, buffer
,
2943 BUF_ZV (XBUFFER (buffer
)),
2944 BUF_ZV_BYTE (XBUFFER (buffer
)));
2947 tem
= Fplist_member (contact
, QCcoding
);
2948 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2954 val
= XCAR (XCDR (tem
));
2958 else if (!NILP (Vcoding_system_for_read
))
2959 val
= Vcoding_system_for_read
;
2960 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2961 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2963 p
->decode_coding_system
= val
;
2968 val
= XCAR (XCDR (tem
));
2972 else if (!NILP (Vcoding_system_for_write
))
2973 val
= Vcoding_system_for_write
;
2974 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2975 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2977 p
->encode_coding_system
= val
;
2979 setup_process_coding_systems (proc
);
2980 p
->decoding_buf
= make_uninit_string (0);
2981 p
->decoding_carryover
= 0;
2982 p
->encoding_buf
= make_uninit_string (0);
2983 p
->inherit_coding_system_flag
2984 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2986 Fserial_process_configure(nargs
, args
);
2988 specpdl_ptr
= specpdl
+ specpdl_count
;
2993 #endif /* HAVE_SERIAL */
2995 /* Create a network stream/datagram client/server process. Treated
2996 exactly like a normal process when reading and writing. Primary
2997 differences are in status display and process deletion. A network
2998 connection has no PID; you cannot signal it. All you can do is
2999 stop/continue it and deactivate/close it via delete-process */
3001 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
3003 doc
: /* Create and return a network server or client process.
3005 In Emacs, network connections are represented by process objects, so
3006 input and output work as for subprocesses and `delete-process' closes
3007 a network connection. However, a network process has no process id,
3008 it cannot be signaled, and the status codes are different from normal
3011 Arguments are specified as keyword/argument pairs. The following
3012 arguments are defined:
3014 :name NAME -- NAME is name for process. It is modified if necessary
3017 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
3018 with the process. Process output goes at end of that buffer, unless
3019 you specify an output stream or filter function to handle the output.
3020 BUFFER may be also nil, meaning that this process is not associated
3023 :host HOST -- HOST is name of the host to connect to, or its IP
3024 address. The symbol `local' specifies the local host. If specified
3025 for a server process, it must be a valid name or address for the local
3026 host, and only clients connecting to that address will be accepted.
3028 :service SERVICE -- SERVICE is name of the service desired, or an
3029 integer specifying a port number to connect to. If SERVICE is t,
3030 a random port number is selected for the server. (If Emacs was
3031 compiled with getaddrinfo, a port number can also be specified as a
3032 string, e.g. "80", as well as an integer. This is not portable.)
3034 :type TYPE -- TYPE is the type of connection. The default (nil) is a
3035 stream type connection, `datagram' creates a datagram type connection.
3037 :family FAMILY -- FAMILY is the address (and protocol) family for the
3038 service specified by HOST and SERVICE. The default (nil) is to use
3039 whatever address family (IPv4 or IPv6) that is defined for the host
3040 and port number specified by HOST and SERVICE. Other address families
3042 local -- for a local (i.e. UNIX) address specified by SERVICE.
3043 ipv4 -- use IPv4 address family only.
3044 ipv6 -- use IPv6 address family only.
3046 :local ADDRESS -- ADDRESS is the local address used for the connection.
3047 This parameter is ignored when opening a client process. When specified
3048 for a server process, the FAMILY, HOST and SERVICE args are ignored.
3050 :remote ADDRESS -- ADDRESS is the remote partner's address for the
3051 connection. This parameter is ignored when opening a stream server
3052 process. For a datagram server process, it specifies the initial
3053 setting of the remote datagram address. When specified for a client
3054 process, the FAMILY, HOST, and SERVICE args are ignored.
3056 The format of ADDRESS depends on the address family:
3057 - An IPv4 address is represented as an vector of integers [A B C D P]
3058 corresponding to numeric IP address A.B.C.D and port number P.
3059 - A local address is represented as a string with the address in the
3060 local address space.
3061 - An "unsupported family" address is represented by a cons (F . AV)
3062 where F is the family number and AV is a vector containing the socket
3063 address data with one element per address data byte. Do not rely on
3064 this format in portable code, as it may depend on implementation
3065 defined constants, data sizes, and data structure alignment.
3067 :coding CODING -- If CODING is a symbol, it specifies the coding
3068 system used for both reading and writing for this process. If CODING
3069 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3070 ENCODING is used for writing.
3072 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3073 return without waiting for the connection to complete; instead, the
3074 sentinel function will be called with second arg matching "open" (if
3075 successful) or "failed" when the connect completes. Default is to use
3076 a blocking connect (i.e. wait) for stream type connections.
3078 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3079 running when Emacs is exited.
3081 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3082 In the stopped state, a server process does not accept new
3083 connections, and a client process does not handle incoming traffic.
3084 The stopped state is cleared by `continue-process' and set by
3087 :filter FILTER -- Install FILTER as the process filter.
3089 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3090 process filter are multibyte, otherwise they are unibyte.
3091 If this keyword is not specified, the strings are multibyte if
3092 `default-enable-multibyte-characters' is non-nil.
3094 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3096 :log LOG -- Install LOG as the server process log function. This
3097 function is called when the server accepts a network connection from a
3098 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3099 is the server process, CLIENT is the new process for the connection,
3100 and MESSAGE is a string.
3102 :plist PLIST -- Install PLIST as the new process' initial plist.
3104 :server QLEN -- if QLEN is non-nil, create a server process for the
3105 specified FAMILY, SERVICE, and connection type (stream or datagram).
3106 If QLEN is an integer, it is used as the max. length of the server's
3107 pending connection queue (also known as the backlog); the default
3108 queue length is 5. Default is to create a client process.
3110 The following network options can be specified for this connection:
3112 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3113 :dontroute BOOL -- Only send to directly connected hosts.
3114 :keepalive BOOL -- Send keep-alive messages on network stream.
3115 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3116 :oobinline BOOL -- Place out-of-band data in receive data stream.
3117 :priority INT -- Set protocol defined priority for sent packets.
3118 :reuseaddr BOOL -- Allow reusing a recently used local address
3119 (this is allowed by default for a server process).
3120 :bindtodevice NAME -- bind to interface NAME. Using this may require
3121 special privileges on some systems.
3123 Consult the relevant system programmer's manual pages for more
3124 information on using these options.
3127 A server process will listen for and accept connections from clients.
3128 When a client connection is accepted, a new network process is created
3129 for the connection with the following parameters:
3131 - The client's process name is constructed by concatenating the server
3132 process' NAME and a client identification string.
3133 - If the FILTER argument is non-nil, the client process will not get a
3134 separate process buffer; otherwise, the client's process buffer is a newly
3135 created buffer named after the server process' BUFFER name or process
3136 NAME concatenated with the client identification string.
3137 - The connection type and the process filter and sentinel parameters are
3138 inherited from the server process' TYPE, FILTER and SENTINEL.
3139 - The client process' contact info is set according to the client's
3140 addressing information (typically an IP address and a port number).
3141 - The client process' plist is initialized from the server's plist.
3143 Notice that the FILTER and SENTINEL args are never used directly by
3144 the server process. Also, the BUFFER argument is not used directly by
3145 the server process, but via the optional :log function, accepted (and
3146 failed) connections may be logged in the server process' buffer.
3148 The original argument list, modified with the actual connection
3149 information, is available via the `process-contact' function.
3151 usage: (make-network-process &rest ARGS) */)
3157 Lisp_Object contact
;
3158 struct Lisp_Process
*p
;
3159 #ifdef HAVE_GETADDRINFO
3160 struct addrinfo ai
, *res
, *lres
;
3161 struct addrinfo hints
;
3162 char *portstring
, portbuf
[128];
3163 #else /* HAVE_GETADDRINFO */
3164 struct _emacs_addrinfo
3170 struct sockaddr
*ai_addr
;
3171 struct _emacs_addrinfo
*ai_next
;
3173 #endif /* HAVE_GETADDRINFO */
3174 struct sockaddr_in address_in
;
3175 #ifdef HAVE_LOCAL_SOCKETS
3176 struct sockaddr_un address_un
;
3181 int s
= -1, outch
, inch
;
3182 struct gcpro gcpro1
;
3183 int count
= SPECPDL_INDEX ();
3185 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3187 Lisp_Object name
, buffer
, host
, service
, address
;
3188 Lisp_Object filter
, sentinel
;
3189 int is_non_blocking_client
= 0;
3190 int is_server
= 0, backlog
= 5;
3197 /* Save arguments for process-contact and clone-process. */
3198 contact
= Flist (nargs
, args
);
3202 /* Ensure socket support is loaded if available. */
3203 init_winsock (TRUE
);
3206 /* :type TYPE (nil: stream, datagram */
3207 tem
= Fplist_get (contact
, QCtype
);
3209 socktype
= SOCK_STREAM
;
3210 #ifdef DATAGRAM_SOCKETS
3211 else if (EQ (tem
, Qdatagram
))
3212 socktype
= SOCK_DGRAM
;
3215 error ("Unsupported connection type");
3218 tem
= Fplist_get (contact
, QCserver
);
3221 /* Don't support network sockets when non-blocking mode is
3222 not available, since a blocked Emacs is not useful. */
3223 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3224 error ("Network servers not supported");
3228 backlog
= XINT (tem
);
3232 /* Make QCaddress an alias for :local (server) or :remote (client). */
3233 QCaddress
= is_server
? QClocal
: QCremote
;
3236 if (!is_server
&& socktype
== SOCK_STREAM
3237 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3239 #ifndef NON_BLOCKING_CONNECT
3240 error ("Non-blocking connect not supported");
3242 is_non_blocking_client
= 1;
3246 name
= Fplist_get (contact
, QCname
);
3247 buffer
= Fplist_get (contact
, QCbuffer
);
3248 filter
= Fplist_get (contact
, QCfilter
);
3249 sentinel
= Fplist_get (contact
, QCsentinel
);
3251 CHECK_STRING (name
);
3253 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3254 ai
.ai_socktype
= socktype
;
3259 /* :local ADDRESS or :remote ADDRESS */
3260 address
= Fplist_get (contact
, QCaddress
);
3261 if (!NILP (address
))
3263 host
= service
= Qnil
;
3265 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3266 error ("Malformed :address");
3267 ai
.ai_family
= family
;
3268 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3269 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3273 /* :family FAMILY -- nil (for Inet), local, or integer. */
3274 tem
= Fplist_get (contact
, QCfamily
);
3277 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3283 #ifdef HAVE_LOCAL_SOCKETS
3284 else if (EQ (tem
, Qlocal
))
3288 else if (EQ (tem
, Qipv6
))
3291 else if (EQ (tem
, Qipv4
))
3293 else if (INTEGERP (tem
))
3294 family
= XINT (tem
);
3296 error ("Unknown address family");
3298 ai
.ai_family
= family
;
3300 /* :service SERVICE -- string, integer (port number), or t (random port). */
3301 service
= Fplist_get (contact
, QCservice
);
3303 #ifdef HAVE_LOCAL_SOCKETS
3304 if (family
== AF_LOCAL
)
3306 /* Host is not used. */
3308 CHECK_STRING (service
);
3309 bzero (&address_un
, sizeof address_un
);
3310 address_un
.sun_family
= AF_LOCAL
;
3311 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3312 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3313 ai
.ai_addrlen
= sizeof address_un
;
3318 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3319 host
= Fplist_get (contact
, QChost
);
3322 if (EQ (host
, Qlocal
))
3323 host
= build_string ("localhost");
3324 CHECK_STRING (host
);
3327 /* Slow down polling to every ten seconds.
3328 Some kernels have a bug which causes retrying connect to fail
3329 after a connect. Polling can interfere with gethostbyname too. */
3330 #ifdef POLL_FOR_INPUT
3331 if (socktype
== SOCK_STREAM
)
3333 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3334 bind_polling_period (10);
3338 #ifdef HAVE_GETADDRINFO
3339 /* If we have a host, use getaddrinfo to resolve both host and service.
3340 Otherwise, use getservbyname to lookup the service. */
3344 /* SERVICE can either be a string or int.
3345 Convert to a C string for later use by getaddrinfo. */
3346 if (EQ (service
, Qt
))
3348 else if (INTEGERP (service
))
3350 sprintf (portbuf
, "%ld", (long) XINT (service
));
3351 portstring
= portbuf
;
3355 CHECK_STRING (service
);
3356 portstring
= SDATA (service
);
3361 memset (&hints
, 0, sizeof (hints
));
3363 hints
.ai_family
= family
;
3364 hints
.ai_socktype
= socktype
;
3365 hints
.ai_protocol
= 0;
3367 #ifdef HAVE_RES_INIT
3371 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3373 #ifdef HAVE_GAI_STRERROR
3374 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3376 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3382 #endif /* HAVE_GETADDRINFO */
3384 /* We end up here if getaddrinfo is not defined, or in case no hostname
3385 has been specified (e.g. for a local server process). */
3387 if (EQ (service
, Qt
))
3389 else if (INTEGERP (service
))
3390 port
= htons ((unsigned short) XINT (service
));
3393 struct servent
*svc_info
;
3394 CHECK_STRING (service
);
3395 svc_info
= getservbyname (SDATA (service
),
3396 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3398 error ("Unknown service: %s", SDATA (service
));
3399 port
= svc_info
->s_port
;
3402 bzero (&address_in
, sizeof address_in
);
3403 address_in
.sin_family
= family
;
3404 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3405 address_in
.sin_port
= port
;
3407 #ifndef HAVE_GETADDRINFO
3410 struct hostent
*host_info_ptr
;
3412 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3413 as it may `hang' Emacs for a very long time. */
3417 #ifdef HAVE_RES_INIT
3421 host_info_ptr
= gethostbyname (SDATA (host
));
3426 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3427 host_info_ptr
->h_length
);
3428 family
= host_info_ptr
->h_addrtype
;
3429 address_in
.sin_family
= family
;
3432 /* Attempt to interpret host as numeric inet address */
3434 unsigned long numeric_addr
;
3435 numeric_addr
= inet_addr ((char *) SDATA (host
));
3436 if (numeric_addr
== -1)
3437 error ("Unknown host \"%s\"", SDATA (host
));
3439 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3440 sizeof (address_in
.sin_addr
));
3444 #endif /* not HAVE_GETADDRINFO */
3446 ai
.ai_family
= family
;
3447 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3448 ai
.ai_addrlen
= sizeof address_in
;
3452 /* Do this in case we never enter the for-loop below. */
3453 count1
= SPECPDL_INDEX ();
3456 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3462 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3469 #ifdef DATAGRAM_SOCKETS
3470 if (!is_server
&& socktype
== SOCK_DGRAM
)
3472 #endif /* DATAGRAM_SOCKETS */
3474 #ifdef NON_BLOCKING_CONNECT
3475 if (is_non_blocking_client
)
3478 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3480 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3492 /* Make us close S if quit. */
3493 record_unwind_protect (close_file_unwind
, make_number (s
));
3495 /* Parse network options in the arg list.
3496 We simply ignore anything which isn't a known option (including other keywords).
3497 An error is signaled if setting a known option fails. */
3498 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3499 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3503 /* Configure as a server socket. */
3505 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3506 explicit :reuseaddr key to override this. */
3507 #ifdef HAVE_LOCAL_SOCKETS
3508 if (family
!= AF_LOCAL
)
3510 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3513 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3514 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3517 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3518 report_file_error ("Cannot bind server socket", Qnil
);
3520 #ifdef HAVE_GETSOCKNAME
3521 if (EQ (service
, Qt
))
3523 struct sockaddr_in sa1
;
3524 int len1
= sizeof (sa1
);
3525 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3527 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3528 service
= make_number (ntohs (sa1
.sin_port
));
3529 contact
= Fplist_put (contact
, QCservice
, service
);
3534 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3535 report_file_error ("Cannot listen on server socket", Qnil
);
3543 /* This turns off all alarm-based interrupts; the
3544 bind_polling_period call above doesn't always turn all the
3545 short-interval ones off, especially if interrupt_input is
3548 It'd be nice to be able to control the connect timeout
3549 though. Would non-blocking connect calls be portable?
3551 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3553 turn_on_atimers (0);
3555 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3558 turn_on_atimers (1);
3560 if (ret
== 0 || xerrno
== EISCONN
)
3562 /* The unwind-protect will be discarded afterwards.
3563 Likewise for immediate_quit. */
3567 #ifdef NON_BLOCKING_CONNECT
3569 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3573 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3581 /* Discard the unwind protect closing S. */
3582 specpdl_ptr
= specpdl
+ count1
;
3586 if (xerrno
== EINTR
)
3592 #ifdef DATAGRAM_SOCKETS
3593 if (socktype
== SOCK_DGRAM
)
3595 if (datagram_address
[s
].sa
)
3597 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3598 datagram_address
[s
].len
= lres
->ai_addrlen
;
3602 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3603 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3606 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3607 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3608 conv_lisp_to_sockaddr (rfamily
, remote
,
3609 datagram_address
[s
].sa
, rlen
);
3613 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3616 contact
= Fplist_put (contact
, QCaddress
,
3617 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3618 #ifdef HAVE_GETSOCKNAME
3621 struct sockaddr_in sa1
;
3622 int len1
= sizeof (sa1
);
3623 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3624 contact
= Fplist_put (contact
, QClocal
,
3625 conv_sockaddr_to_lisp (&sa1
, len1
));
3632 #ifdef HAVE_GETADDRINFO
3641 /* Discard the unwind protect for closing S, if any. */
3642 specpdl_ptr
= specpdl
+ count1
;
3644 /* Unwind bind_polling_period and request_sigio. */
3645 unbind_to (count
, Qnil
);
3649 /* If non-blocking got this far - and failed - assume non-blocking is
3650 not supported after all. This is probably a wrong assumption, but
3651 the normal blocking calls to open-network-stream handles this error
3653 if (is_non_blocking_client
)
3658 report_file_error ("make server process failed", contact
);
3660 report_file_error ("make client process failed", contact
);
3667 buffer
= Fget_buffer_create (buffer
);
3668 proc
= make_process (name
);
3670 chan_process
[inch
] = proc
;
3673 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3676 fcntl (inch
, F_SETFL
, O_NDELAY
);
3680 p
= XPROCESS (proc
);
3682 p
->childp
= contact
;
3683 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3687 p
->sentinel
= sentinel
;
3689 p
->log
= Fplist_get (contact
, QClog
);
3690 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3691 p
->kill_without_query
= 1;
3692 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3697 if (is_server
&& socktype
== SOCK_STREAM
)
3698 p
->status
= Qlisten
;
3700 /* Make the process marker point into the process buffer (if any). */
3701 if (BUFFERP (buffer
))
3702 set_marker_both (p
->mark
, buffer
,
3703 BUF_ZV (XBUFFER (buffer
)),
3704 BUF_ZV_BYTE (XBUFFER (buffer
)));
3706 #ifdef NON_BLOCKING_CONNECT
3707 if (is_non_blocking_client
)
3709 /* We may get here if connect did succeed immediately. However,
3710 in that case, we still need to signal this like a non-blocking
3712 p
->status
= Qconnect
;
3713 if (!FD_ISSET (inch
, &connect_wait_mask
))
3715 FD_SET (inch
, &connect_wait_mask
);
3716 num_pending_connects
++;
3721 /* A server may have a client filter setting of Qt, but it must
3722 still listen for incoming connects unless it is stopped. */
3723 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3724 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3726 FD_SET (inch
, &input_wait_mask
);
3727 FD_SET (inch
, &non_keyboard_wait_mask
);
3730 if (inch
> max_process_desc
)
3731 max_process_desc
= inch
;
3733 tem
= Fplist_member (contact
, QCcoding
);
3734 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3735 tem
= Qnil
; /* No error message (too late!). */
3738 /* Setup coding systems for communicating with the network stream. */
3739 struct gcpro gcpro1
;
3740 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3741 Lisp_Object coding_systems
= Qt
;
3742 Lisp_Object args
[5], val
;
3746 val
= XCAR (XCDR (tem
));
3750 else if (!NILP (Vcoding_system_for_read
))
3751 val
= Vcoding_system_for_read
;
3752 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3753 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3754 /* We dare not decode end-of-line format by setting VAL to
3755 Qraw_text, because the existing Emacs Lisp libraries
3756 assume that they receive bare code including a sequene of
3761 if (NILP (host
) || NILP (service
))
3762 coding_systems
= Qnil
;
3765 args
[0] = Qopen_network_stream
, args
[1] = name
,
3766 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3768 coding_systems
= Ffind_operation_coding_system (5, args
);
3771 if (CONSP (coding_systems
))
3772 val
= XCAR (coding_systems
);
3773 else if (CONSP (Vdefault_process_coding_system
))
3774 val
= XCAR (Vdefault_process_coding_system
);
3778 p
->decode_coding_system
= val
;
3782 val
= XCAR (XCDR (tem
));
3786 else if (!NILP (Vcoding_system_for_write
))
3787 val
= Vcoding_system_for_write
;
3788 else if (NILP (current_buffer
->enable_multibyte_characters
))
3792 if (EQ (coding_systems
, Qt
))
3794 if (NILP (host
) || NILP (service
))
3795 coding_systems
= Qnil
;
3798 args
[0] = Qopen_network_stream
, args
[1] = name
,
3799 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3801 coding_systems
= Ffind_operation_coding_system (5, args
);
3805 if (CONSP (coding_systems
))
3806 val
= XCDR (coding_systems
);
3807 else if (CONSP (Vdefault_process_coding_system
))
3808 val
= XCDR (Vdefault_process_coding_system
);
3812 p
->encode_coding_system
= val
;
3814 setup_process_coding_systems (proc
);
3816 p
->decoding_buf
= make_uninit_string (0);
3817 p
->decoding_carryover
= 0;
3818 p
->encoding_buf
= make_uninit_string (0);
3820 p
->inherit_coding_system_flag
3821 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3826 #endif /* HAVE_SOCKETS */
3829 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3832 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3833 doc
: /* Return an alist of all network interfaces and their network address.
3834 Each element is a cons, the car of which is a string containing the
3835 interface name, and the cdr is the network address in internal
3836 format; see the description of ADDRESS in `make-network-process'. */)
3839 struct ifconf ifconf
;
3840 struct ifreq
*ifreqs
= NULL
;
3845 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3851 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3852 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3859 ifconf
.ifc_len
= buf_size
;
3860 ifconf
.ifc_req
= ifreqs
;
3861 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3867 if (ifconf
.ifc_len
== buf_size
)
3871 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3874 while (--ifaces
>= 0)
3876 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3877 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3878 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3880 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3881 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3882 res
= Fcons (Fcons (build_string (namebuf
),
3883 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3884 sizeof (struct sockaddr
))),
3890 #endif /* SIOCGIFCONF */
3892 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3899 static struct ifflag_def ifflag_table
[] = {
3903 #ifdef IFF_BROADCAST
3904 { IFF_BROADCAST
, "broadcast" },
3907 { IFF_DEBUG
, "debug" },
3910 { IFF_LOOPBACK
, "loopback" },
3912 #ifdef IFF_POINTOPOINT
3913 { IFF_POINTOPOINT
, "pointopoint" },
3916 { IFF_RUNNING
, "running" },
3919 { IFF_NOARP
, "noarp" },
3922 { IFF_PROMISC
, "promisc" },
3924 #ifdef IFF_NOTRAILERS
3925 { IFF_NOTRAILERS
, "notrailers" },
3928 { IFF_ALLMULTI
, "allmulti" },
3931 { IFF_MASTER
, "master" },
3934 { IFF_SLAVE
, "slave" },
3936 #ifdef IFF_MULTICAST
3937 { IFF_MULTICAST
, "multicast" },
3940 { IFF_PORTSEL
, "portsel" },
3942 #ifdef IFF_AUTOMEDIA
3943 { IFF_AUTOMEDIA
, "automedia" },
3946 { IFF_DYNAMIC
, "dynamic" },
3949 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3952 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3955 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3958 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3961 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3966 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3967 doc
: /* Return information about network interface named IFNAME.
3968 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3969 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3970 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3971 FLAGS is the current flags of the interface. */)
3976 Lisp_Object res
= Qnil
;
3981 CHECK_STRING (ifname
);
3983 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
3984 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3986 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3991 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3992 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3994 int flags
= rq
.ifr_flags
;
3995 struct ifflag_def
*fp
;
3999 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
4001 if (flags
& fp
->flag_bit
)
4003 elt
= Fcons (intern (fp
->flag_sym
), elt
);
4004 flags
-= fp
->flag_bit
;
4007 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
4009 if (flags
& (1 << fnum
))
4011 elt
= Fcons (make_number (fnum
), elt
);
4016 res
= Fcons (elt
, res
);
4019 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
4020 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
4022 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
4023 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
4027 for (n
= 0; n
< 6; n
++)
4028 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
4029 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
4032 res
= Fcons (elt
, res
);
4035 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4036 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4039 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4040 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4042 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4046 res
= Fcons (elt
, res
);
4049 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4050 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4053 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4056 res
= Fcons (elt
, res
);
4059 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4060 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4063 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4066 res
= Fcons (elt
, res
);
4070 return any
? res
: Qnil
;
4073 #endif /* HAVE_SOCKETS */
4075 /* Turn off input and output for process PROC. */
4078 deactivate_process (proc
)
4081 register int inchannel
, outchannel
;
4082 register struct Lisp_Process
*p
= XPROCESS (proc
);
4084 inchannel
= p
->infd
;
4085 outchannel
= p
->outfd
;
4087 #ifdef ADAPTIVE_READ_BUFFERING
4088 if (p
->read_output_delay
> 0)
4090 if (--process_output_delay_count
< 0)
4091 process_output_delay_count
= 0;
4092 p
->read_output_delay
= 0;
4093 p
->read_output_skip
= 0;
4099 /* Beware SIGCHLD hereabouts. */
4100 flush_pending_output (inchannel
);
4103 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
4104 sys$
dassgn (outchannel
);
4105 vs
= get_vms_process_pointer (p
->pid
);
4107 give_back_vms_process_stuff (vs
);
4110 emacs_close (inchannel
);
4111 if (outchannel
>= 0 && outchannel
!= inchannel
)
4112 emacs_close (outchannel
);
4117 #ifdef DATAGRAM_SOCKETS
4118 if (DATAGRAM_CHAN_P (inchannel
))
4120 xfree (datagram_address
[inchannel
].sa
);
4121 datagram_address
[inchannel
].sa
= 0;
4122 datagram_address
[inchannel
].len
= 0;
4125 chan_process
[inchannel
] = Qnil
;
4126 FD_CLR (inchannel
, &input_wait_mask
);
4127 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4128 #ifdef NON_BLOCKING_CONNECT
4129 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4131 FD_CLR (inchannel
, &connect_wait_mask
);
4132 if (--num_pending_connects
< 0)
4136 if (inchannel
== max_process_desc
)
4139 /* We just closed the highest-numbered process input descriptor,
4140 so recompute the highest-numbered one now. */
4141 max_process_desc
= 0;
4142 for (i
= 0; i
< MAXDESC
; i
++)
4143 if (!NILP (chan_process
[i
]))
4144 max_process_desc
= i
;
4149 /* Close all descriptors currently in use for communication
4150 with subprocess. This is used in a newly-forked subprocess
4151 to get rid of irrelevant descriptors. */
4154 close_process_descs ()
4158 for (i
= 0; i
< MAXDESC
; i
++)
4160 Lisp_Object process
;
4161 process
= chan_process
[i
];
4162 if (!NILP (process
))
4164 int in
= XPROCESS (process
)->infd
;
4165 int out
= XPROCESS (process
)->outfd
;
4168 if (out
>= 0 && in
!= out
)
4175 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4177 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4178 It is read into the process' buffers or given to their filter functions.
4179 Non-nil arg PROCESS means do not return until some output has been received
4182 Non-nil second arg SECONDS and third arg MILLISEC are number of
4183 seconds and milliseconds to wait; return after that much time whether
4184 or not there is input. If SECONDS is a floating point number,
4185 it specifies a fractional number of seconds to wait.
4186 The MILLISEC argument is obsolete and should be avoided.
4188 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4189 from PROCESS, suspending reading output from other processes.
4190 If JUST-THIS-ONE is an integer, don't run any timers either.
4191 Return non-nil if we received any output before the timeout expired. */)
4192 (process
, seconds
, millisec
, just_this_one
)
4193 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
4195 int secs
, usecs
= 0;
4197 if (! NILP (process
))
4198 CHECK_PROCESS (process
);
4200 just_this_one
= Qnil
;
4202 if (!NILP (millisec
))
4203 { /* Obsolete calling convention using integers rather than floats. */
4204 CHECK_NUMBER (millisec
);
4206 seconds
= make_float (XINT (millisec
) / 1000.0);
4209 CHECK_NUMBER (seconds
);
4210 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4214 if (!NILP (seconds
))
4216 if (INTEGERP (seconds
))
4217 secs
= XINT (seconds
);
4218 else if (FLOATP (seconds
))
4220 double timeout
= XFLOAT_DATA (seconds
);
4221 secs
= (int) timeout
;
4222 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4225 wrong_type_argument (Qnumberp
, seconds
);
4227 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4228 secs
= -1, usecs
= 0;
4231 secs
= NILP (process
) ? -1 : 0;
4234 (wait_reading_process_output (secs
, usecs
, 0, 0,
4236 !NILP (process
) ? XPROCESS (process
) : NULL
,
4237 NILP (just_this_one
) ? 0 :
4238 !INTEGERP (just_this_one
) ? 1 : -1)
4242 /* Accept a connection for server process SERVER on CHANNEL. */
4244 static int connect_counter
= 0;
4247 server_accept_connection (server
, channel
)
4251 Lisp_Object proc
, caller
, name
, buffer
;
4252 Lisp_Object contact
, host
, service
;
4253 struct Lisp_Process
*ps
= XPROCESS (server
);
4254 struct Lisp_Process
*p
;
4258 struct sockaddr_in in
;
4260 struct sockaddr_in6 in6
;
4262 #ifdef HAVE_LOCAL_SOCKETS
4263 struct sockaddr_un un
;
4266 int len
= sizeof saddr
;
4268 s
= accept (channel
, &saddr
.sa
, &len
);
4277 if (code
== EWOULDBLOCK
)
4281 if (!NILP (ps
->log
))
4282 call3 (ps
->log
, server
, Qnil
,
4283 concat3 (build_string ("accept failed with code"),
4284 Fnumber_to_string (make_number (code
)),
4285 build_string ("\n")));
4291 /* Setup a new process to handle the connection. */
4293 /* Generate a unique identification of the caller, and build contact
4294 information for this process. */
4297 switch (saddr
.sa
.sa_family
)
4301 Lisp_Object args
[5];
4302 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4303 args
[0] = build_string ("%d.%d.%d.%d");
4304 args
[1] = make_number (*ip
++);
4305 args
[2] = make_number (*ip
++);
4306 args
[3] = make_number (*ip
++);
4307 args
[4] = make_number (*ip
++);
4308 host
= Fformat (5, args
);
4309 service
= make_number (ntohs (saddr
.in
.sin_port
));
4311 args
[0] = build_string (" <%s:%d>");
4314 caller
= Fformat (3, args
);
4321 Lisp_Object args
[9];
4322 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4324 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4325 for (i
= 0; i
< 8; i
++)
4326 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4327 host
= Fformat (9, args
);
4328 service
= make_number (ntohs (saddr
.in
.sin_port
));
4330 args
[0] = build_string (" <[%s]:%d>");
4333 caller
= Fformat (3, args
);
4338 #ifdef HAVE_LOCAL_SOCKETS
4342 caller
= Fnumber_to_string (make_number (connect_counter
));
4343 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4347 /* Create a new buffer name for this process if it doesn't have a
4348 filter. The new buffer name is based on the buffer name or
4349 process name of the server process concatenated with the caller
4352 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4356 buffer
= ps
->buffer
;
4358 buffer
= Fbuffer_name (buffer
);
4363 buffer
= concat2 (buffer
, caller
);
4364 buffer
= Fget_buffer_create (buffer
);
4368 /* Generate a unique name for the new server process. Combine the
4369 server process name with the caller identification. */
4371 name
= concat2 (ps
->name
, caller
);
4372 proc
= make_process (name
);
4374 chan_process
[s
] = proc
;
4377 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4380 fcntl (s
, F_SETFL
, O_NDELAY
);
4384 p
= XPROCESS (proc
);
4386 /* Build new contact information for this setup. */
4387 contact
= Fcopy_sequence (ps
->childp
);
4388 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4389 contact
= Fplist_put (contact
, QChost
, host
);
4390 if (!NILP (service
))
4391 contact
= Fplist_put (contact
, QCservice
, service
);
4392 contact
= Fplist_put (contact
, QCremote
,
4393 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4394 #ifdef HAVE_GETSOCKNAME
4396 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4397 contact
= Fplist_put (contact
, QClocal
,
4398 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4401 p
->childp
= contact
;
4402 p
->plist
= Fcopy_sequence (ps
->plist
);
4406 p
->sentinel
= ps
->sentinel
;
4407 p
->filter
= ps
->filter
;
4414 /* Client processes for accepted connections are not stopped initially. */
4415 if (!EQ (p
->filter
, Qt
))
4417 FD_SET (s
, &input_wait_mask
);
4418 FD_SET (s
, &non_keyboard_wait_mask
);
4421 if (s
> max_process_desc
)
4422 max_process_desc
= s
;
4424 /* Setup coding system for new process based on server process.
4425 This seems to be the proper thing to do, as the coding system
4426 of the new process should reflect the settings at the time the
4427 server socket was opened; not the current settings. */
4429 p
->decode_coding_system
= ps
->decode_coding_system
;
4430 p
->encode_coding_system
= ps
->encode_coding_system
;
4431 setup_process_coding_systems (proc
);
4433 p
->decoding_buf
= make_uninit_string (0);
4434 p
->decoding_carryover
= 0;
4435 p
->encoding_buf
= make_uninit_string (0);
4437 p
->inherit_coding_system_flag
4438 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4440 if (!NILP (ps
->log
))
4441 call3 (ps
->log
, server
, proc
,
4442 concat3 (build_string ("accept from "),
4443 (STRINGP (host
) ? host
: build_string ("-")),
4444 build_string ("\n")));
4446 if (!NILP (p
->sentinel
))
4447 exec_sentinel (proc
,
4448 concat3 (build_string ("open from "),
4449 (STRINGP (host
) ? host
: build_string ("-")),
4450 build_string ("\n")));
4453 /* This variable is different from waiting_for_input in keyboard.c.
4454 It is used to communicate to a lisp process-filter/sentinel (via the
4455 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4456 for user-input when that process-filter was called.
4457 waiting_for_input cannot be used as that is by definition 0 when
4458 lisp code is being evalled.
4459 This is also used in record_asynch_buffer_change.
4460 For that purpose, this must be 0
4461 when not inside wait_reading_process_output. */
4462 static int waiting_for_user_input_p
;
4465 wait_reading_process_output_unwind (data
)
4468 waiting_for_user_input_p
= XINT (data
);
4472 /* This is here so breakpoints can be put on it. */
4474 wait_reading_process_output_1 ()
4478 /* Use a wrapper around select to work around a bug in gdb 5.3.
4479 Normally, the wrapper is optimzed away by inlining.
4481 If emacs is stopped inside select, the gdb backtrace doesn't
4482 show the function which called select, so it is practically
4483 impossible to step through wait_reading_process_output. */
4487 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4489 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4492 return select (n
, rfd
, wfd
, xfd
, tmo
);
4494 #define select select_wrapper
4497 /* Read and dispose of subprocess output while waiting for timeout to
4498 elapse and/or keyboard input to be available.
4501 timeout in seconds, or
4502 zero for no limit, or
4503 -1 means gobble data immediately available but don't wait for any.
4506 an additional duration to wait, measured in microseconds.
4507 If this is nonzero and time_limit is 0, then the timeout
4508 consists of MICROSECS only.
4510 READ_KBD is a lisp value:
4511 0 to ignore keyboard input, or
4512 1 to return when input is available, or
4513 -1 meaning caller will actually read the input, so don't throw to
4514 the quit handler, or
4516 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4517 output that arrives.
4519 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4520 (and gobble terminal input into the buffer if any arrives).
4522 If WAIT_PROC is specified, wait until something arrives from that
4523 process. The return value is true if we read some input from
4526 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4527 (suspending output from other processes). A negative value
4528 means don't run any timers either.
4530 If WAIT_PROC is specified, then the function returns true if we
4531 received input from that process before the timeout elapsed.
4532 Otherwise, return true if we received input from any process. */
4535 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4536 wait_for_cell
, wait_proc
, just_wait_proc
)
4537 int time_limit
, microsecs
, read_kbd
, do_display
;
4538 Lisp_Object wait_for_cell
;
4539 struct Lisp_Process
*wait_proc
;
4542 register int channel
, nfds
;
4543 SELECT_TYPE Available
;
4544 #ifdef NON_BLOCKING_CONNECT
4545 SELECT_TYPE Connecting
;
4548 int check_delay
, no_avail
;
4551 EMACS_TIME timeout
, end_time
;
4552 int wait_channel
= -1;
4553 int got_some_input
= 0;
4554 int count
= SPECPDL_INDEX ();
4556 FD_ZERO (&Available
);
4557 #ifdef NON_BLOCKING_CONNECT
4558 FD_ZERO (&Connecting
);
4561 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4562 if (wait_proc
!= NULL
)
4563 wait_channel
= wait_proc
->infd
;
4565 record_unwind_protect (wait_reading_process_output_unwind
,
4566 make_number (waiting_for_user_input_p
));
4567 waiting_for_user_input_p
= read_kbd
;
4569 /* Since we may need to wait several times,
4570 compute the absolute time to return at. */
4571 if (time_limit
|| microsecs
)
4573 EMACS_GET_TIME (end_time
);
4574 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4575 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4580 int timeout_reduced_for_timers
= 0;
4582 /* If calling from keyboard input, do not quit
4583 since we want to return C-g as an input character.
4584 Otherwise, do pending quit if requested. */
4590 if (interrupt_input_pending
)
4591 handle_async_input ();
4592 if (pending_atimers
)
4593 do_pending_atimers ();
4597 /* Exit now if the cell we're waiting for became non-nil. */
4598 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4601 /* Compute time from now till when time limit is up */
4602 /* Exit if already run out */
4603 if (time_limit
== -1)
4605 /* -1 specified for timeout means
4606 gobble output available now
4607 but don't wait at all. */
4609 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4611 else if (time_limit
|| microsecs
)
4613 EMACS_GET_TIME (timeout
);
4614 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4615 if (EMACS_TIME_NEG_P (timeout
))
4620 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4623 /* Normally we run timers here.
4624 But not if wait_for_cell; in those cases,
4625 the wait is supposed to be short,
4626 and those callers cannot handle running arbitrary Lisp code here. */
4627 if (NILP (wait_for_cell
)
4628 && just_wait_proc
>= 0)
4630 EMACS_TIME timer_delay
;
4634 int old_timers_run
= timers_run
;
4635 struct buffer
*old_buffer
= current_buffer
;
4636 Lisp_Object old_window
= selected_window
;
4638 timer_delay
= timer_check (1);
4640 /* If a timer has run, this might have changed buffers
4641 an alike. Make read_key_sequence aware of that. */
4642 if (timers_run
!= old_timers_run
4643 && (old_buffer
!= current_buffer
4644 || !EQ (old_window
, selected_window
))
4645 && waiting_for_user_input_p
== -1)
4646 record_asynch_buffer_change ();
4648 if (timers_run
!= old_timers_run
&& do_display
)
4649 /* We must retry, since a timer may have requeued itself
4650 and that could alter the time_delay. */
4651 redisplay_preserve_echo_area (9);
4655 while (!detect_input_pending ());
4657 /* If there is unread keyboard input, also return. */
4659 && requeued_events_pending_p ())
4662 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4664 EMACS_TIME difference
;
4665 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4666 if (EMACS_TIME_NEG_P (difference
))
4668 timeout
= timer_delay
;
4669 timeout_reduced_for_timers
= 1;
4672 /* If time_limit is -1, we are not going to wait at all. */
4673 else if (time_limit
!= -1)
4675 /* This is so a breakpoint can be put here. */
4676 wait_reading_process_output_1 ();
4680 /* Cause C-g and alarm signals to take immediate action,
4681 and cause input available signals to zero out timeout.
4683 It is important that we do this before checking for process
4684 activity. If we get a SIGCHLD after the explicit checks for
4685 process activity, timeout is the only way we will know. */
4687 set_waiting_for_input (&timeout
);
4689 /* If status of something has changed, and no input is
4690 available, notify the user of the change right away. After
4691 this explicit check, we'll let the SIGCHLD handler zap
4692 timeout to get our attention. When Emacs is run
4693 interactively, only do this with a nonzero DO_DISPLAY
4694 argument, because status_notify triggers redisplay. */
4695 if (update_tick
!= process_tick
4696 && (do_display
|| noninteractive
))
4699 #ifdef NON_BLOCKING_CONNECT
4703 Atemp
= input_wait_mask
;
4704 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4706 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4707 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4710 #ifdef NON_BLOCKING_CONNECT
4711 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4715 (SELECT_TYPE
*)0, &timeout
)
4718 /* It's okay for us to do this and then continue with
4719 the loop, since timeout has already been zeroed out. */
4720 clear_waiting_for_input ();
4721 status_notify (NULL
);
4725 /* Don't wait for output from a non-running process. Just
4726 read whatever data has already been received. */
4727 if (wait_proc
&& wait_proc
->raw_status_new
)
4728 update_status (wait_proc
);
4730 && ! EQ (wait_proc
->status
, Qrun
)
4731 && ! EQ (wait_proc
->status
, Qconnect
))
4733 int nread
, total_nread
= 0;
4735 clear_waiting_for_input ();
4736 XSETPROCESS (proc
, wait_proc
);
4738 /* Read data from the process, until we exhaust it. */
4739 while (wait_proc
->infd
>= 0)
4741 nread
= read_process_output (proc
, wait_proc
->infd
);
4748 total_nread
+= nread
;
4752 else if (nread
== -1 && EIO
== errno
)
4756 else if (nread
== -1 && EAGAIN
== errno
)
4760 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4764 if (total_nread
> 0 && do_display
)
4765 redisplay_preserve_echo_area (10);
4770 /* Wait till there is something to do */
4772 if (wait_proc
&& just_wait_proc
)
4774 if (wait_proc
->infd
< 0) /* Terminated */
4776 FD_SET (wait_proc
->infd
, &Available
);
4778 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4780 else if (!NILP (wait_for_cell
))
4782 Available
= non_process_wait_mask
;
4784 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4789 Available
= non_keyboard_wait_mask
;
4791 Available
= input_wait_mask
;
4792 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4793 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4796 /* If frame size has changed or the window is newly mapped,
4797 redisplay now, before we start to wait. There is a race
4798 condition here; if a SIGIO arrives between now and the select
4799 and indicates that a frame is trashed, the select may block
4800 displaying a trashed screen. */
4801 if (frame_garbaged
&& do_display
)
4803 clear_waiting_for_input ();
4804 redisplay_preserve_echo_area (11);
4806 set_waiting_for_input (&timeout
);
4810 if (read_kbd
&& detect_input_pending ())
4817 #ifdef NON_BLOCKING_CONNECT
4819 Connecting
= connect_wait_mask
;
4822 #ifdef ADAPTIVE_READ_BUFFERING
4823 /* Set the timeout for adaptive read buffering if any
4824 process has non-zero read_output_skip and non-zero
4825 read_output_delay, and we are not reading output for a
4826 specific wait_channel. It is not executed if
4827 Vprocess_adaptive_read_buffering is nil. */
4828 if (process_output_skip
&& check_delay
> 0)
4830 int usecs
= EMACS_USECS (timeout
);
4831 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4832 usecs
= READ_OUTPUT_DELAY_MAX
;
4833 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4835 proc
= chan_process
[channel
];
4838 /* Find minimum non-zero read_output_delay among the
4839 processes with non-zero read_output_skip. */
4840 if (XPROCESS (proc
)->read_output_delay
> 0)
4843 if (!XPROCESS (proc
)->read_output_skip
)
4845 FD_CLR (channel
, &Available
);
4846 XPROCESS (proc
)->read_output_skip
= 0;
4847 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4848 usecs
= XPROCESS (proc
)->read_output_delay
;
4851 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4852 process_output_skip
= 0;
4860 (max (max (max_process_desc
, max_keyboard_desc
),
4863 #ifdef NON_BLOCKING_CONNECT
4864 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4868 (SELECT_TYPE
*)0, &timeout
);
4873 /* Make C-g and alarm signals set flags again */
4874 clear_waiting_for_input ();
4876 /* If we woke up due to SIGWINCH, actually change size now. */
4877 do_pending_window_change (0);
4879 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4880 /* We wanted the full specified time, so return now. */
4884 if (xerrno
== EINTR
)
4886 else if (xerrno
== EBADF
)
4889 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4890 the child's closure of the pts gives the parent a SIGHUP, and
4891 the ptc file descriptor is automatically closed,
4892 yielding EBADF here or at select() call above.
4893 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4894 in m/ibmrt-aix.h), and here we just ignore the select error.
4895 Cleanup occurs c/o status_notify after SIGCLD. */
4896 no_avail
= 1; /* Cannot depend on values returned */
4902 error ("select error: %s", emacs_strerror (xerrno
));
4907 FD_ZERO (&Available
);
4908 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4911 #if 0 /* When polling is used, interrupt_input is 0,
4912 so get_input_pending should read the input.
4913 So this should not be needed. */
4914 /* If we are using polling for input,
4915 and we see input available, make it get read now.
4916 Otherwise it might not actually get read for a second.
4917 And on hpux, since we turn off polling in wait_reading_process_output,
4918 it might never get read at all if we don't spend much time
4919 outside of wait_reading_process_output. */
4920 if (read_kbd
&& interrupt_input
4921 && keyboard_bit_set (&Available
)
4922 && input_polling_used ())
4923 kill (getpid (), SIGALRM
);
4926 /* Check for keyboard input */
4927 /* If there is any, return immediately
4928 to give it higher priority than subprocesses */
4932 int old_timers_run
= timers_run
;
4933 struct buffer
*old_buffer
= current_buffer
;
4934 Lisp_Object old_window
= selected_window
;
4937 if (detect_input_pending_run_timers (do_display
))
4939 swallow_events (do_display
);
4940 if (detect_input_pending_run_timers (do_display
))
4944 /* If a timer has run, this might have changed buffers
4945 an alike. Make read_key_sequence aware of that. */
4946 if (timers_run
!= old_timers_run
4947 && waiting_for_user_input_p
== -1
4948 && (old_buffer
!= current_buffer
4949 || !EQ (old_window
, selected_window
)))
4950 record_asynch_buffer_change ();
4956 /* If there is unread keyboard input, also return. */
4958 && requeued_events_pending_p ())
4961 /* If we are not checking for keyboard input now,
4962 do process events (but don't run any timers).
4963 This is so that X events will be processed.
4964 Otherwise they may have to wait until polling takes place.
4965 That would causes delays in pasting selections, for example.
4967 (We used to do this only if wait_for_cell.) */
4968 if (read_kbd
== 0 && detect_input_pending ())
4970 swallow_events (do_display
);
4971 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4972 if (detect_input_pending ())
4977 /* Exit now if the cell we're waiting for became non-nil. */
4978 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4982 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4983 go read it. This can happen with X on BSD after logging out.
4984 In that case, there really is no input and no SIGIO,
4985 but select says there is input. */
4987 if (read_kbd
&& interrupt_input
4988 && keyboard_bit_set (&Available
) && ! noninteractive
)
4989 kill (getpid (), SIGIO
);
4993 got_some_input
|= nfds
> 0;
4995 /* If checking input just got us a size-change event from X,
4996 obey it now if we should. */
4997 if (read_kbd
|| ! NILP (wait_for_cell
))
4998 do_pending_window_change (0);
5000 /* Check for data from a process. */
5001 if (no_avail
|| nfds
== 0)
5004 /* Really FIRST_PROC_DESC should be 0 on Unix,
5005 but this is safer in the short run. */
5006 for (channel
= 0; channel
<= max_process_desc
; channel
++)
5008 if (FD_ISSET (channel
, &Available
)
5009 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
5013 /* If waiting for this channel, arrange to return as
5014 soon as no more input to be processed. No more
5016 if (wait_channel
== channel
)
5022 proc
= chan_process
[channel
];
5026 /* If this is a server stream socket, accept connection. */
5027 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
5029 server_accept_connection (proc
, channel
);
5033 /* Read data from the process, starting with our
5034 buffered-ahead character if we have one. */
5036 nread
= read_process_output (proc
, channel
);
5039 /* Since read_process_output can run a filter,
5040 which can call accept-process-output,
5041 don't try to read from any other processes
5042 before doing the select again. */
5043 FD_ZERO (&Available
);
5046 redisplay_preserve_echo_area (12);
5049 else if (nread
== -1 && errno
== EWOULDBLOCK
)
5052 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
5053 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
5055 else if (nread
== -1 && errno
== EAGAIN
)
5059 else if (nread
== -1 && errno
== EAGAIN
)
5061 /* Note that we cannot distinguish between no input
5062 available now and a closed pipe.
5063 With luck, a closed pipe will be accompanied by
5064 subprocess termination and SIGCHLD. */
5065 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5067 #endif /* O_NDELAY */
5068 #endif /* O_NONBLOCK */
5070 /* On some OSs with ptys, when the process on one end of
5071 a pty exits, the other end gets an error reading with
5072 errno = EIO instead of getting an EOF (0 bytes read).
5073 Therefore, if we get an error reading and errno =
5074 EIO, just continue, because the child process has
5075 exited and should clean itself up soon (e.g. when we
5078 However, it has been known to happen that the SIGCHLD
5079 got lost. So raise the signal again just in case.
5081 else if (nread
== -1 && errno
== EIO
)
5083 /* Clear the descriptor now, so we only raise the signal once. */
5084 FD_CLR (channel
, &input_wait_mask
);
5085 FD_CLR (channel
, &non_keyboard_wait_mask
);
5087 kill (getpid (), SIGCHLD
);
5089 #endif /* HAVE_PTYS */
5090 /* If we can detect process termination, don't consider the process
5091 gone just because its pipe is closed. */
5093 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5098 /* Preserve status of processes already terminated. */
5099 XPROCESS (proc
)->tick
= ++process_tick
;
5100 deactivate_process (proc
);
5101 if (XPROCESS (proc
)->raw_status_new
)
5102 update_status (XPROCESS (proc
));
5103 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5104 XPROCESS (proc
)->status
5105 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5108 #ifdef NON_BLOCKING_CONNECT
5109 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
5110 && FD_ISSET (channel
, &connect_wait_mask
))
5112 struct Lisp_Process
*p
;
5114 FD_CLR (channel
, &connect_wait_mask
);
5115 if (--num_pending_connects
< 0)
5118 proc
= chan_process
[channel
];
5122 p
= XPROCESS (proc
);
5125 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5126 So only use it on systems where it is known to work. */
5128 int xlen
= sizeof(xerrno
);
5129 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5134 struct sockaddr pname
;
5135 int pnamelen
= sizeof(pname
);
5137 /* If connection failed, getpeername will fail. */
5139 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
5141 /* Obtain connect failure code through error slippage. */
5144 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
5151 p
->tick
= ++process_tick
;
5152 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5153 deactivate_process (proc
);
5158 /* Execute the sentinel here. If we had relied on
5159 status_notify to do it later, it will read input
5160 from the process before calling the sentinel. */
5161 exec_sentinel (proc
, build_string ("open\n"));
5162 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5164 FD_SET (p
->infd
, &input_wait_mask
);
5165 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5169 #endif /* NON_BLOCKING_CONNECT */
5170 } /* end for each file descriptor */
5171 } /* end while exit conditions not met */
5173 unbind_to (count
, Qnil
);
5175 /* If calling from keyboard input, do not quit
5176 since we want to return C-g as an input character.
5177 Otherwise, do pending quit if requested. */
5180 /* Prevent input_pending from remaining set if we quit. */
5181 clear_input_pending ();
5185 return got_some_input
;
5188 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5191 read_process_output_call (fun_and_args
)
5192 Lisp_Object fun_and_args
;
5194 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5198 read_process_output_error_handler (error
)
5201 cmd_error_internal (error
, "error in process filter: ");
5203 update_echo_area ();
5204 Fsleep_for (make_number (2), Qnil
);
5208 /* Read pending output from the process channel,
5209 starting with our buffered-ahead character if we have one.
5210 Yield number of decoded characters read.
5212 This function reads at most 4096 characters.
5213 If you want to read all available subprocess output,
5214 you must call it repeatedly until it returns zero.
5216 The characters read are decoded according to PROC's coding-system
5220 read_process_output (proc
, channel
)
5222 register int channel
;
5224 register int nbytes
;
5226 register Lisp_Object outstream
;
5227 register struct buffer
*old
= current_buffer
;
5228 register struct Lisp_Process
*p
= XPROCESS (proc
);
5229 register int opoint
;
5230 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5231 int carryover
= p
->decoding_carryover
;
5235 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5237 vs
= get_vms_process_pointer (p
->pid
);
5241 return (0); /* Really weird if it does this */
5242 if (!(vs
->iosb
[0] & 1))
5243 return -1; /* I/O error */
5246 error ("Could not get VMS process pointer");
5247 chars
= vs
->inputBuffer
;
5248 nbytes
= clean_vms_buffer (chars
, vs
->iosb
[1]);
5251 start_vms_process_read (vs
); /* Crank up the next read on the process */
5252 return 1; /* Nothing worth printing, say we got 1 */
5256 /* The data carried over in the previous decoding (which are at
5257 the tail of decoding buffer) should be prepended to the new
5258 data read to decode all together. */
5259 chars
= (char *) alloca (nbytes
+ carryover
);
5260 bcopy (SDATA (p
->decoding_buf
), buf
, carryover
);
5261 bcopy (vs
->inputBuffer
, chars
+ carryover
, nbytes
);
5265 chars
= (char *) alloca (carryover
+ readmax
);
5267 /* See the comment above. */
5268 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5270 #ifdef DATAGRAM_SOCKETS
5271 /* We have a working select, so proc_buffered_char is always -1. */
5272 if (DATAGRAM_CHAN_P (channel
))
5274 int len
= datagram_address
[channel
].len
;
5275 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5276 0, datagram_address
[channel
].sa
, &len
);
5280 if (proc_buffered_char
[channel
] < 0)
5282 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5283 #ifdef ADAPTIVE_READ_BUFFERING
5284 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5286 int delay
= p
->read_output_delay
;
5289 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5292 process_output_delay_count
++;
5293 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5296 else if (delay
> 0 && (nbytes
== readmax
))
5298 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5300 process_output_delay_count
--;
5302 p
->read_output_delay
= delay
;
5305 p
->read_output_skip
= 1;
5306 process_output_skip
= 1;
5313 chars
[carryover
] = proc_buffered_char
[channel
];
5314 proc_buffered_char
[channel
] = -1;
5315 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5319 nbytes
= nbytes
+ 1;
5321 #endif /* not VMS */
5323 p
->decoding_carryover
= 0;
5325 /* At this point, NBYTES holds number of bytes just received
5326 (including the one in proc_buffered_char[channel]). */
5329 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5331 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5334 /* Now set NBYTES how many bytes we must decode. */
5335 nbytes
+= carryover
;
5337 /* Read and dispose of the process output. */
5338 outstream
= p
->filter
;
5339 if (!NILP (outstream
))
5341 /* We inhibit quit here instead of just catching it so that
5342 hitting ^G when a filter happens to be running won't screw
5344 int count
= SPECPDL_INDEX ();
5345 Lisp_Object odeactivate
;
5346 Lisp_Object obuffer
, okeymap
;
5348 int outer_running_asynch_code
= running_asynch_code
;
5349 int waiting
= waiting_for_user_input_p
;
5351 /* No need to gcpro these, because all we do with them later
5352 is test them for EQness, and none of them should be a string. */
5353 odeactivate
= Vdeactivate_mark
;
5354 XSETBUFFER (obuffer
, current_buffer
);
5355 okeymap
= current_buffer
->keymap
;
5357 specbind (Qinhibit_quit
, Qt
);
5358 specbind (Qlast_nonmenu_event
, Qt
);
5360 /* In case we get recursively called,
5361 and we already saved the match data nonrecursively,
5362 save the same match data in safely recursive fashion. */
5363 if (outer_running_asynch_code
)
5366 /* Don't clobber the CURRENT match data, either! */
5367 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5368 restore_search_regs ();
5369 record_unwind_save_match_data ();
5370 Fset_match_data (tem
, Qt
);
5373 /* For speed, if a search happens within this code,
5374 save the match data in a special nonrecursive fashion. */
5375 running_asynch_code
= 1;
5377 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5378 text
= coding
->dst_object
;
5379 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5380 /* A new coding system might be found. */
5381 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5383 p
->decode_coding_system
= Vlast_coding_system_used
;
5385 /* Don't call setup_coding_system for
5386 proc_decode_coding_system[channel] here. It is done in
5387 detect_coding called via decode_coding above. */
5389 /* If a coding system for encoding is not yet decided, we set
5390 it as the same as coding-system for decoding.
5392 But, before doing that we must check if
5393 proc_encode_coding_system[p->outfd] surely points to a
5394 valid memory because p->outfd will be changed once EOF is
5395 sent to the process. */
5396 if (NILP (p
->encode_coding_system
)
5397 && proc_encode_coding_system
[p
->outfd
])
5399 p
->encode_coding_system
5400 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5401 setup_coding_system (p
->encode_coding_system
,
5402 proc_encode_coding_system
[p
->outfd
]);
5406 if (coding
->carryover_bytes
> 0)
5408 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5409 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5410 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5411 coding
->carryover_bytes
);
5412 p
->decoding_carryover
= coding
->carryover_bytes
;
5414 if (SBYTES (text
) > 0)
5415 internal_condition_case_1 (read_process_output_call
,
5417 Fcons (proc
, Fcons (text
, Qnil
))),
5418 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5419 read_process_output_error_handler
);
5421 /* If we saved the match data nonrecursively, restore it now. */
5422 restore_search_regs ();
5423 running_asynch_code
= outer_running_asynch_code
;
5425 /* Handling the process output should not deactivate the mark. */
5426 Vdeactivate_mark
= odeactivate
;
5428 /* Restore waiting_for_user_input_p as it was
5429 when we were called, in case the filter clobbered it. */
5430 waiting_for_user_input_p
= waiting
;
5432 #if 0 /* Call record_asynch_buffer_change unconditionally,
5433 because we might have changed minor modes or other things
5434 that affect key bindings. */
5435 if (! EQ (Fcurrent_buffer (), obuffer
)
5436 || ! EQ (current_buffer
->keymap
, okeymap
))
5438 /* But do it only if the caller is actually going to read events.
5439 Otherwise there's no need to make him wake up, and it could
5440 cause trouble (for example it would make sit_for return). */
5441 if (waiting_for_user_input_p
== -1)
5442 record_asynch_buffer_change ();
5445 start_vms_process_read (vs
);
5447 unbind_to (count
, Qnil
);
5451 /* If no filter, write into buffer if it isn't dead. */
5452 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5454 Lisp_Object old_read_only
;
5455 int old_begv
, old_zv
;
5456 int old_begv_byte
, old_zv_byte
;
5457 Lisp_Object odeactivate
;
5458 int before
, before_byte
;
5463 odeactivate
= Vdeactivate_mark
;
5465 Fset_buffer (p
->buffer
);
5467 opoint_byte
= PT_BYTE
;
5468 old_read_only
= current_buffer
->read_only
;
5471 old_begv_byte
= BEGV_BYTE
;
5472 old_zv_byte
= ZV_BYTE
;
5474 current_buffer
->read_only
= Qnil
;
5476 /* Insert new output into buffer
5477 at the current end-of-output marker,
5478 thus preserving logical ordering of input and output. */
5479 if (XMARKER (p
->mark
)->buffer
)
5480 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5481 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5484 SET_PT_BOTH (ZV
, ZV_BYTE
);
5486 before_byte
= PT_BYTE
;
5488 /* If the output marker is outside of the visible region, save
5489 the restriction and widen. */
5490 if (! (BEGV
<= PT
&& PT
<= ZV
))
5493 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5494 text
= coding
->dst_object
;
5495 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5496 /* A new coding system might be found. See the comment in the
5497 similar code in the previous `if' block. */
5498 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5500 p
->decode_coding_system
= Vlast_coding_system_used
;
5501 if (NILP (p
->encode_coding_system
)
5502 && proc_encode_coding_system
[p
->outfd
])
5504 p
->encode_coding_system
5505 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5506 setup_coding_system (p
->encode_coding_system
,
5507 proc_encode_coding_system
[p
->outfd
]);
5510 if (coding
->carryover_bytes
> 0)
5512 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5513 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5514 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5515 coding
->carryover_bytes
);
5516 p
->decoding_carryover
= coding
->carryover_bytes
;
5518 /* Adjust the multibyteness of TEXT to that of the buffer. */
5519 if (NILP (current_buffer
->enable_multibyte_characters
)
5520 != ! STRING_MULTIBYTE (text
))
5521 text
= (STRING_MULTIBYTE (text
)
5522 ? Fstring_as_unibyte (text
)
5523 : Fstring_to_multibyte (text
));
5524 /* Insert before markers in case we are inserting where
5525 the buffer's mark is, and the user's next command is Meta-y. */
5526 insert_from_string_before_markers (text
, 0, 0,
5527 SCHARS (text
), SBYTES (text
), 0);
5529 /* Make sure the process marker's position is valid when the
5530 process buffer is changed in the signal_after_change above.
5531 W3 is known to do that. */
5532 if (BUFFERP (p
->buffer
)
5533 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5534 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5536 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5538 update_mode_lines
++;
5540 /* Make sure opoint and the old restrictions
5541 float ahead of any new text just as point would. */
5542 if (opoint
>= before
)
5544 opoint
+= PT
- before
;
5545 opoint_byte
+= PT_BYTE
- before_byte
;
5547 if (old_begv
> before
)
5549 old_begv
+= PT
- before
;
5550 old_begv_byte
+= PT_BYTE
- before_byte
;
5552 if (old_zv
>= before
)
5554 old_zv
+= PT
- before
;
5555 old_zv_byte
+= PT_BYTE
- before_byte
;
5558 /* If the restriction isn't what it should be, set it. */
5559 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5560 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5562 /* Handling the process output should not deactivate the mark. */
5563 Vdeactivate_mark
= odeactivate
;
5565 current_buffer
->read_only
= old_read_only
;
5566 SET_PT_BOTH (opoint
, opoint_byte
);
5567 set_buffer_internal (old
);
5570 start_vms_process_read (vs
);
5575 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5577 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5578 This is intended for use by asynchronous process output filters and sentinels. */)
5581 return (waiting_for_user_input_p
? Qt
: Qnil
);
5584 /* Sending data to subprocess */
5586 jmp_buf send_process_frame
;
5587 Lisp_Object process_sent_to
;
5590 send_process_trap ()
5592 SIGNAL_THREAD_CHECK (SIGPIPE
);
5593 sigunblock (sigmask (SIGPIPE
));
5594 longjmp (send_process_frame
, 1);
5597 /* Send some data to process PROC.
5598 BUF is the beginning of the data; LEN is the number of characters.
5599 OBJECT is the Lisp object that the data comes from. If OBJECT is
5600 nil or t, it means that the data comes from C string.
5602 If OBJECT is not nil, the data is encoded by PROC's coding-system
5603 for encoding before it is sent.
5605 This function can evaluate Lisp code and can garbage collect. */
5608 send_process (proc
, buf
, len
, object
)
5609 volatile Lisp_Object proc
;
5610 unsigned char *volatile buf
;
5612 volatile Lisp_Object object
;
5614 /* Use volatile to protect variables from being clobbered by longjmp. */
5615 struct Lisp_Process
*p
= XPROCESS (proc
);
5617 struct coding_system
*coding
;
5618 struct gcpro gcpro1
;
5619 SIGTYPE (*volatile old_sigpipe
) ();
5624 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5627 if (p
->raw_status_new
)
5629 if (! EQ (p
->status
, Qrun
))
5630 error ("Process %s not running", SDATA (p
->name
));
5632 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5634 coding
= proc_encode_coding_system
[p
->outfd
];
5635 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5637 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5638 || (BUFFERP (object
)
5639 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5642 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5643 /* The coding system for encoding was changed to raw-text
5644 because we sent a unibyte text previously. Now we are
5645 sending a multibyte text, thus we must encode it by the
5646 original coding system specified for the current process. */
5647 setup_coding_system (p
->encode_coding_system
, coding
);
5648 coding
->src_multibyte
= 1;
5652 /* For sending a unibyte text, character code conversion should
5653 not take place but EOL conversion should. So, setup raw-text
5654 or one of the subsidiary if we have not yet done it. */
5655 if (CODING_REQUIRE_ENCODING (coding
))
5657 if (CODING_REQUIRE_FLUSHING (coding
))
5659 /* But, before changing the coding, we must flush out data. */
5660 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5661 send_process (proc
, "", 0, Qt
);
5662 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5664 setup_coding_system (raw_text_coding_system
5665 (Vlast_coding_system_used
),
5667 coding
->src_multibyte
= 0;
5670 coding
->dst_multibyte
= 0;
5672 if (CODING_REQUIRE_ENCODING (coding
))
5674 coding
->dst_object
= Qt
;
5675 if (BUFFERP (object
))
5677 int from_byte
, from
, to
;
5678 int save_pt
, save_pt_byte
;
5679 struct buffer
*cur
= current_buffer
;
5681 set_buffer_internal (XBUFFER (object
));
5682 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5684 from_byte
= PTR_BYTE_POS (buf
);
5685 from
= BYTE_TO_CHAR (from_byte
);
5686 to
= BYTE_TO_CHAR (from_byte
+ len
);
5687 TEMP_SET_PT_BOTH (from
, from_byte
);
5688 encode_coding_object (coding
, object
, from
, from_byte
,
5689 to
, from_byte
+ len
, Qt
);
5690 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5691 set_buffer_internal (cur
);
5693 else if (STRINGP (object
))
5695 encode_coding_string (coding
, object
, 1);
5699 coding
->dst_object
= make_unibyte_string (buf
, len
);
5700 coding
->produced
= len
;
5703 len
= coding
->produced
;
5704 buf
= SDATA (coding
->dst_object
);
5708 vs
= get_vms_process_pointer (p
->pid
);
5710 error ("Could not find this process: %x", p
->pid
);
5711 else if (write_to_vms_process (vs
, buf
, len
))
5715 if (pty_max_bytes
== 0)
5717 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5718 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5719 if (pty_max_bytes
< 0)
5720 pty_max_bytes
= 250;
5722 pty_max_bytes
= 250;
5724 /* Deduct one, to leave space for the eof. */
5728 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5729 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5730 when returning with longjmp despite being declared volatile. */
5731 if (!setjmp (send_process_frame
))
5733 process_sent_to
= proc
;
5738 /* Decide how much data we can send in one batch.
5739 Long lines need to be split into multiple batches. */
5742 /* Starting this at zero is always correct when not the first
5743 iteration because the previous iteration ended by sending C-d.
5744 It may not be correct for the first iteration
5745 if a partial line was sent in a separate send_process call.
5746 If that proves worth handling, we need to save linepos
5747 in the process object. */
5749 unsigned char *ptr
= (unsigned char *) buf
;
5750 unsigned char *end
= (unsigned char *) buf
+ len
;
5752 /* Scan through this text for a line that is too long. */
5753 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5761 /* If we found one, break the line there
5762 and put in a C-d to force the buffer through. */
5766 /* Send this batch, using one or more write calls. */
5769 int outfd
= p
->outfd
;
5770 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5771 #ifdef DATAGRAM_SOCKETS
5772 if (DATAGRAM_CHAN_P (outfd
))
5774 rv
= sendto (outfd
, (char *) buf
, this,
5775 0, datagram_address
[outfd
].sa
,
5776 datagram_address
[outfd
].len
);
5777 if (rv
< 0 && errno
== EMSGSIZE
)
5779 signal (SIGPIPE
, old_sigpipe
);
5780 report_file_error ("sending datagram",
5781 Fcons (proc
, Qnil
));
5787 rv
= emacs_write (outfd
, (char *) buf
, this);
5788 #ifdef ADAPTIVE_READ_BUFFERING
5789 if (p
->read_output_delay
> 0
5790 && p
->adaptive_read_buffering
== 1)
5792 p
->read_output_delay
= 0;
5793 process_output_delay_count
--;
5794 p
->read_output_skip
= 0;
5798 signal (SIGPIPE
, old_sigpipe
);
5804 || errno
== EWOULDBLOCK
5810 /* Buffer is full. Wait, accepting input;
5811 that may allow the program
5812 to finish doing output and read more. */
5816 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5817 /* A gross hack to work around a bug in FreeBSD.
5818 In the following sequence, read(2) returns
5822 write(2) 954 bytes, get EAGAIN
5823 read(2) 1024 bytes in process_read_output
5824 read(2) 11 bytes in process_read_output
5826 That is, read(2) returns more bytes than have
5827 ever been written successfully. The 1033 bytes
5828 read are the 1022 bytes written successfully
5829 after processing (for example with CRs added if
5830 the terminal is set up that way which it is
5831 here). The same bytes will be seen again in a
5832 later read(2), without the CRs. */
5834 if (errno
== EAGAIN
)
5837 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5839 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5841 /* Running filters might relocate buffers or strings.
5842 Arrange to relocate BUF. */
5843 if (BUFFERP (object
))
5844 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5845 else if (STRINGP (object
))
5846 offset
= buf
- SDATA (object
);
5848 #ifdef EMACS_HAS_USECS
5849 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5851 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5854 if (BUFFERP (object
))
5855 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5856 else if (STRINGP (object
))
5857 buf
= offset
+ SDATA (object
);
5862 /* This is a real error. */
5863 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5870 /* If we sent just part of the string, put in an EOF (C-d)
5871 to force it through, before we send the rest. */
5873 Fprocess_send_eof (proc
);
5876 #endif /* not VMS */
5879 signal (SIGPIPE
, old_sigpipe
);
5881 proc
= process_sent_to
;
5882 p
= XPROCESS (proc
);
5884 p
->raw_status_new
= 0;
5885 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5886 p
->tick
= ++process_tick
;
5887 deactivate_process (proc
);
5889 error ("Error writing to process %s; closed it", SDATA (p
->name
));
5891 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5898 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5900 doc
: /* Send current contents of region as input to PROCESS.
5901 PROCESS may be a process, a buffer, the name of a process or buffer, or
5902 nil, indicating the current buffer's process.
5903 Called from program, takes three arguments, PROCESS, START and END.
5904 If the region is more than 500 characters long,
5905 it is sent in several bunches. This may happen even for shorter regions.
5906 Output from processes can arrive in between bunches. */)
5907 (process
, start
, end
)
5908 Lisp_Object process
, start
, end
;
5913 proc
= get_process (process
);
5914 validate_region (&start
, &end
);
5916 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5917 move_gap (XINT (start
));
5919 start1
= CHAR_TO_BYTE (XINT (start
));
5920 end1
= CHAR_TO_BYTE (XINT (end
));
5921 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5922 Fcurrent_buffer ());
5927 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5929 doc
: /* Send PROCESS the contents of STRING as input.
5930 PROCESS may be a process, a buffer, the name of a process or buffer, or
5931 nil, indicating the current buffer's process.
5932 If STRING is more than 500 characters long,
5933 it is sent in several bunches. This may happen even for shorter strings.
5934 Output from processes can arrive in between bunches. */)
5936 Lisp_Object process
, string
;
5939 CHECK_STRING (string
);
5940 proc
= get_process (process
);
5941 send_process (proc
, SDATA (string
),
5942 SBYTES (string
), string
);
5946 /* Return the foreground process group for the tty/pty that
5947 the process P uses. */
5949 emacs_get_tty_pgrp (p
)
5950 struct Lisp_Process
*p
;
5955 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5958 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5959 master side. Try the slave side. */
5960 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
5964 ioctl (fd
, TIOCGPGRP
, &gid
);
5968 #endif /* defined (TIOCGPGRP ) */
5973 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5974 Sprocess_running_child_p
, 0, 1, 0,
5975 doc
: /* Return t if PROCESS has given the terminal to a child.
5976 If the operating system does not make it possible to find out,
5977 return t unconditionally. */)
5979 Lisp_Object process
;
5981 /* Initialize in case ioctl doesn't exist or gives an error,
5982 in a way that will cause returning t. */
5985 struct Lisp_Process
*p
;
5987 proc
= get_process (process
);
5988 p
= XPROCESS (proc
);
5990 if (!EQ (p
->type
, Qreal
))
5991 error ("Process %s is not a subprocess",
5994 error ("Process %s is not active",
5997 gid
= emacs_get_tty_pgrp (p
);
6004 /* send a signal number SIGNO to PROCESS.
6005 If CURRENT_GROUP is t, that means send to the process group
6006 that currently owns the terminal being used to communicate with PROCESS.
6007 This is used for various commands in shell mode.
6008 If CURRENT_GROUP is lambda, that means send to the process group
6009 that currently owns the terminal, but only if it is NOT the shell itself.
6011 If NOMSG is zero, insert signal-announcements into process's buffers
6014 If we can, we try to signal PROCESS by sending control characters
6015 down the pty. This allows us to signal inferiors who have changed
6016 their uid, for which killpg would return an EPERM error. */
6019 process_send_signal (process
, signo
, current_group
, nomsg
)
6020 Lisp_Object process
;
6022 Lisp_Object current_group
;
6026 register struct Lisp_Process
*p
;
6030 proc
= get_process (process
);
6031 p
= XPROCESS (proc
);
6033 if (!EQ (p
->type
, Qreal
))
6034 error ("Process %s is not a subprocess",
6037 error ("Process %s is not active",
6041 current_group
= Qnil
;
6043 /* If we are using pgrps, get a pgrp number and make it negative. */
6044 if (NILP (current_group
))
6045 /* Send the signal to the shell's process group. */
6049 #ifdef SIGNALS_VIA_CHARACTERS
6050 /* If possible, send signals to the entire pgrp
6051 by sending an input character to it. */
6053 /* TERMIOS is the latest and bestest, and seems most likely to
6054 work. If the system has it, use it. */
6057 cc_t
*sig_char
= NULL
;
6059 tcgetattr (p
->infd
, &t
);
6064 sig_char
= &t
.c_cc
[VINTR
];
6068 sig_char
= &t
.c_cc
[VQUIT
];
6072 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
6073 sig_char
= &t
.c_cc
[VSWTCH
];
6075 sig_char
= &t
.c_cc
[VSUSP
];
6080 if (sig_char
&& *sig_char
!= CDISABLE
)
6082 send_process (proc
, sig_char
, 1, Qnil
);
6085 /* If we can't send the signal with a character,
6086 fall through and send it another way. */
6087 #else /* ! HAVE_TERMIOS */
6089 /* On Berkeley descendants, the following IOCTL's retrieve the
6090 current control characters. */
6091 #if defined (TIOCGLTC) && defined (TIOCGETC)
6099 ioctl (p
->infd
, TIOCGETC
, &c
);
6100 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
6103 ioctl (p
->infd
, TIOCGETC
, &c
);
6104 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
6108 ioctl (p
->infd
, TIOCGLTC
, &lc
);
6109 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
6111 #endif /* ! defined (SIGTSTP) */
6114 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6116 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6123 ioctl (p
->infd
, TCGETA
, &t
);
6124 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
6127 ioctl (p
->infd
, TCGETA
, &t
);
6128 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
6132 ioctl (p
->infd
, TCGETA
, &t
);
6133 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
6135 #endif /* ! defined (SIGTSTP) */
6137 #else /* ! defined (TCGETA) */
6138 Your configuration files are messed up
.
6139 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6140 you'd better be using one of the alternatives above! */
6141 #endif /* ! defined (TCGETA) */
6142 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6143 /* In this case, the code above should alway return. */
6145 #endif /* ! defined HAVE_TERMIOS */
6147 /* The code above may fall through if it can't
6148 handle the signal. */
6149 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
6152 /* Get the current pgrp using the tty itself, if we have that.
6153 Otherwise, use the pty to get the pgrp.
6154 On pfa systems, saka@pfu.fujitsu.co.JP writes:
6155 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6156 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
6157 His patch indicates that if TIOCGPGRP returns an error, then
6158 we should just assume that p->pid is also the process group id. */
6160 gid
= emacs_get_tty_pgrp (p
);
6163 /* If we can't get the information, assume
6164 the shell owns the tty. */
6167 /* It is not clear whether anything really can set GID to -1.
6168 Perhaps on some system one of those ioctls can or could do so.
6169 Or perhaps this is vestigial. */
6172 #else /* ! defined (TIOCGPGRP ) */
6173 /* Can't select pgrps on this system, so we know that
6174 the child itself heads the pgrp. */
6176 #endif /* ! defined (TIOCGPGRP ) */
6178 /* If current_group is lambda, and the shell owns the terminal,
6179 don't send any signal. */
6180 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
6188 p
->raw_status_new
= 0;
6190 p
->tick
= ++process_tick
;
6192 status_notify (NULL
);
6194 #endif /* ! defined (SIGCONT) */
6197 send_process (proc
, "\003", 1, Qnil
); /* ^C */
6202 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
6207 sys$
forcex (&(p
->pid
), 0, 1);
6210 flush_pending_output (p
->infd
);
6214 /* If we don't have process groups, send the signal to the immediate
6215 subprocess. That isn't really right, but it's better than any
6216 obvious alternative. */
6219 kill (p
->pid
, signo
);
6223 /* gid may be a pid, or minus a pgrp's number */
6225 if (!NILP (current_group
))
6227 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6228 EMACS_KILLPG (gid
, signo
);
6235 #else /* ! defined (TIOCSIGSEND) */
6236 EMACS_KILLPG (gid
, signo
);
6237 #endif /* ! defined (TIOCSIGSEND) */
6240 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6241 doc
: /* Interrupt process PROCESS.
6242 PROCESS may be a process, a buffer, or the name of a process or buffer.
6243 No arg or nil means current buffer's process.
6244 Second arg CURRENT-GROUP non-nil means send signal to
6245 the current process-group of the process's controlling terminal
6246 rather than to the process's own process group.
6247 If the process is a shell, this means interrupt current subjob
6248 rather than the shell.
6250 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6251 don't send the signal. */)
6252 (process
, current_group
)
6253 Lisp_Object process
, current_group
;
6255 process_send_signal (process
, SIGINT
, current_group
, 0);
6259 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6260 doc
: /* Kill process PROCESS. May be process or name of one.
6261 See function `interrupt-process' for more details on usage. */)
6262 (process
, current_group
)
6263 Lisp_Object process
, current_group
;
6265 process_send_signal (process
, SIGKILL
, current_group
, 0);
6269 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6270 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6271 See function `interrupt-process' for more details on usage. */)
6272 (process
, current_group
)
6273 Lisp_Object process
, current_group
;
6275 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6279 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6280 doc
: /* Stop process PROCESS. May be process or name of one.
6281 See function `interrupt-process' for more details on usage.
6282 If PROCESS is a network or serial process, inhibit handling of incoming
6284 (process
, current_group
)
6285 Lisp_Object process
, current_group
;
6288 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6290 struct Lisp_Process
*p
;
6292 p
= XPROCESS (process
);
6293 if (NILP (p
->command
)
6296 FD_CLR (p
->infd
, &input_wait_mask
);
6297 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6304 error ("No SIGTSTP support");
6306 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6311 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6312 doc
: /* Continue process PROCESS. May be process or name of one.
6313 See function `interrupt-process' for more details on usage.
6314 If PROCESS is a network or serial process, resume handling of incoming
6316 (process
, current_group
)
6317 Lisp_Object process
, current_group
;
6320 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6322 struct Lisp_Process
*p
;
6324 p
= XPROCESS (process
);
6325 if (EQ (p
->command
, Qt
)
6327 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6329 FD_SET (p
->infd
, &input_wait_mask
);
6330 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6332 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6333 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6336 tcflush (p
->infd
, TCIFLUSH
);
6344 process_send_signal (process
, SIGCONT
, current_group
, 0);
6346 error ("No SIGCONT support");
6351 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6352 2, 2, "sProcess (name or number): \nnSignal code: ",
6353 doc
: /* Send PROCESS the signal with code SIGCODE.
6354 PROCESS may also be a number specifying the process id of the
6355 process to signal; in this case, the process need not be a child of
6357 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6359 Lisp_Object process
, sigcode
;
6363 if (INTEGERP (process
))
6365 pid
= XINT (process
);
6369 if (FLOATP (process
))
6371 pid
= (pid_t
) XFLOAT_DATA (process
);
6375 if (STRINGP (process
))
6378 if (tem
= Fget_process (process
), NILP (tem
))
6380 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6387 process
= get_process (process
);
6392 CHECK_PROCESS (process
);
6393 pid
= XPROCESS (process
)->pid
;
6395 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6399 #define parse_signal(NAME, VALUE) \
6400 else if (!xstrcasecmp (name, NAME)) \
6401 XSETINT (sigcode, VALUE)
6403 if (INTEGERP (sigcode
))
6407 unsigned char *name
;
6409 CHECK_SYMBOL (sigcode
);
6410 name
= SDATA (SYMBOL_NAME (sigcode
));
6412 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6418 parse_signal ("usr1", SIGUSR1
);
6421 parse_signal ("usr2", SIGUSR2
);
6424 parse_signal ("term", SIGTERM
);
6427 parse_signal ("hup", SIGHUP
);
6430 parse_signal ("int", SIGINT
);
6433 parse_signal ("quit", SIGQUIT
);
6436 parse_signal ("ill", SIGILL
);
6439 parse_signal ("abrt", SIGABRT
);
6442 parse_signal ("emt", SIGEMT
);
6445 parse_signal ("kill", SIGKILL
);
6448 parse_signal ("fpe", SIGFPE
);
6451 parse_signal ("bus", SIGBUS
);
6454 parse_signal ("segv", SIGSEGV
);
6457 parse_signal ("sys", SIGSYS
);
6460 parse_signal ("pipe", SIGPIPE
);
6463 parse_signal ("alrm", SIGALRM
);
6466 parse_signal ("urg", SIGURG
);
6469 parse_signal ("stop", SIGSTOP
);
6472 parse_signal ("tstp", SIGTSTP
);
6475 parse_signal ("cont", SIGCONT
);
6478 parse_signal ("chld", SIGCHLD
);
6481 parse_signal ("ttin", SIGTTIN
);
6484 parse_signal ("ttou", SIGTTOU
);
6487 parse_signal ("io", SIGIO
);
6490 parse_signal ("xcpu", SIGXCPU
);
6493 parse_signal ("xfsz", SIGXFSZ
);
6496 parse_signal ("vtalrm", SIGVTALRM
);
6499 parse_signal ("prof", SIGPROF
);
6502 parse_signal ("winch", SIGWINCH
);
6505 parse_signal ("info", SIGINFO
);
6508 error ("Undefined signal name %s", name
);
6513 return make_number (kill (pid
, XINT (sigcode
)));
6516 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6517 doc
: /* Make PROCESS see end-of-file in its input.
6518 EOF comes after any text already sent to it.
6519 PROCESS may be a process, a buffer, the name of a process or buffer, or
6520 nil, indicating the current buffer's process.
6521 If PROCESS is a network connection, or is a process communicating
6522 through a pipe (as opposed to a pty), then you cannot send any more
6523 text to PROCESS after you call this function.
6524 If PROCESS is a serial process, wait until all output written to the
6525 process has been transmitted to the serial port. */)
6527 Lisp_Object process
;
6530 struct coding_system
*coding
;
6532 if (DATAGRAM_CONN_P (process
))
6535 proc
= get_process (process
);
6536 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6538 /* Make sure the process is really alive. */
6539 if (XPROCESS (proc
)->raw_status_new
)
6540 update_status (XPROCESS (proc
));
6541 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6542 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6544 if (CODING_REQUIRE_FLUSHING (coding
))
6546 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6547 send_process (proc
, "", 0, Qnil
);
6551 send_process (proc
, "\032", 1, Qnil
); /* ^z */
6553 if (XPROCESS (proc
)->pty_flag
)
6554 send_process (proc
, "\004", 1, Qnil
);
6555 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6558 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6559 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6561 /* Do nothing on Windows because writes are blocking. */
6565 int old_outfd
, new_outfd
;
6567 #ifdef HAVE_SHUTDOWN
6568 /* If this is a network connection, or socketpair is used
6569 for communication with the subprocess, call shutdown to cause EOF.
6570 (In some old system, shutdown to socketpair doesn't work.
6571 Then we just can't win.) */
6572 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6573 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6574 shutdown (XPROCESS (proc
)->outfd
, 1);
6575 /* In case of socketpair, outfd == infd, so don't close it. */
6576 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6577 emacs_close (XPROCESS (proc
)->outfd
);
6578 #else /* not HAVE_SHUTDOWN */
6579 emacs_close (XPROCESS (proc
)->outfd
);
6580 #endif /* not HAVE_SHUTDOWN */
6581 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6584 old_outfd
= XPROCESS (proc
)->outfd
;
6586 if (!proc_encode_coding_system
[new_outfd
])
6587 proc_encode_coding_system
[new_outfd
]
6588 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6589 bcopy (proc_encode_coding_system
[old_outfd
],
6590 proc_encode_coding_system
[new_outfd
],
6591 sizeof (struct coding_system
));
6592 bzero (proc_encode_coding_system
[old_outfd
],
6593 sizeof (struct coding_system
));
6595 XPROCESS (proc
)->outfd
= new_outfd
;
6601 /* Kill all processes associated with `buffer'.
6602 If `buffer' is nil, kill all processes */
6605 kill_buffer_processes (buffer
)
6608 Lisp_Object tail
, proc
;
6610 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6612 proc
= XCDR (XCAR (tail
));
6614 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6616 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
6617 Fdelete_process (proc
);
6618 else if (XPROCESS (proc
)->infd
>= 0)
6619 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6624 /* On receipt of a signal that a child status has changed, loop asking
6625 about children with changed statuses until the system says there
6628 All we do is change the status; we do not run sentinels or print
6629 notifications. That is saved for the next time keyboard input is
6630 done, in order to avoid timing errors.
6632 ** WARNING: this can be called during garbage collection.
6633 Therefore, it must not be fooled by the presence of mark bits in
6636 ** USG WARNING: Although it is not obvious from the documentation
6637 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6638 signal() before executing at least one wait(), otherwise the
6639 handler will be called again, resulting in an infinite loop. The
6640 relevant portion of the documentation reads "SIGCLD signals will be
6641 queued and the signal-catching function will be continually
6642 reentered until the queue is empty". Invoking signal() causes the
6643 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6646 ** Malloc WARNING: This should never call malloc either directly or
6647 indirectly; if it does, that is a bug */
6651 sigchld_handler (signo
)
6654 int old_errno
= errno
;
6656 register struct Lisp_Process
*p
;
6657 extern EMACS_TIME
*input_available_clear_time
;
6659 SIGNAL_THREAD_CHECK (signo
);
6670 #endif /* no WUNTRACED */
6671 /* Keep trying to get a status until we get a definitive result. */
6675 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6677 while (pid
< 0 && errno
== EINTR
);
6681 /* PID == 0 means no processes found, PID == -1 means a real
6682 failure. We have done all our job, so return. */
6684 /* USG systems forget handlers when they are used;
6685 must reestablish each time */
6686 #if defined (USG) && !defined (POSIX_SIGNALS)
6687 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6694 #endif /* no WNOHANG */
6696 /* Find the process that signaled us, and record its status. */
6698 /* The process can have been deleted by Fdelete_process. */
6699 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6701 Lisp_Object xpid
= XCAR (tail
);
6702 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6703 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6705 XSETCAR (tail
, Qnil
);
6706 goto sigchld_end_of_loop
;
6710 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6712 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6714 proc
= XCDR (XCAR (tail
));
6715 p
= XPROCESS (proc
);
6716 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6721 /* Look for an asynchronous process whose pid hasn't been filled
6724 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6726 proc
= XCDR (XCAR (tail
));
6727 p
= XPROCESS (proc
);
6733 /* Change the status of the process that was found. */
6736 int clear_desc_flag
= 0;
6738 p
->tick
= ++process_tick
;
6740 p
->raw_status_new
= 1;
6742 /* If process has terminated, stop waiting for its output. */
6743 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6745 clear_desc_flag
= 1;
6747 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6748 if (clear_desc_flag
)
6750 FD_CLR (p
->infd
, &input_wait_mask
);
6751 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6754 /* Tell wait_reading_process_output that it needs to wake up and
6756 if (input_available_clear_time
)
6757 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6760 /* There was no asynchronous process found for that pid: we have
6761 a synchronous process. */
6764 synch_process_alive
= 0;
6766 /* Report the status of the synchronous process. */
6768 synch_process_retcode
= WRETCODE (w
);
6769 else if (WIFSIGNALED (w
))
6770 synch_process_termsig
= WTERMSIG (w
);
6772 /* Tell wait_reading_process_output that it needs to wake up and
6774 if (input_available_clear_time
)
6775 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6778 sigchld_end_of_loop
:
6781 /* On some systems, we must return right away.
6782 If any more processes want to signal us, we will
6784 Otherwise (on systems that have WNOHANG), loop around
6785 to use up all the processes that have something to tell us. */
6786 #if (defined WINDOWSNT \
6787 || (defined USG && !defined GNU_LINUX \
6788 && !(defined HPUX && defined WNOHANG)))
6789 #if defined (USG) && ! defined (POSIX_SIGNALS)
6790 signal (signo
, sigchld_handler
);
6794 #endif /* USG, but not HPUX with WNOHANG */
6797 #endif /* SIGCHLD */
6801 exec_sentinel_unwind (data
)
6804 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6809 exec_sentinel_error_handler (error
)
6812 cmd_error_internal (error
, "error in process sentinel: ");
6814 update_echo_area ();
6815 Fsleep_for (make_number (2), Qnil
);
6820 exec_sentinel (proc
, reason
)
6821 Lisp_Object proc
, reason
;
6823 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6824 register struct Lisp_Process
*p
= XPROCESS (proc
);
6825 int count
= SPECPDL_INDEX ();
6826 int outer_running_asynch_code
= running_asynch_code
;
6827 int waiting
= waiting_for_user_input_p
;
6829 if (inhibit_sentinels
)
6832 /* No need to gcpro these, because all we do with them later
6833 is test them for EQness, and none of them should be a string. */
6834 odeactivate
= Vdeactivate_mark
;
6835 XSETBUFFER (obuffer
, current_buffer
);
6836 okeymap
= current_buffer
->keymap
;
6838 sentinel
= p
->sentinel
;
6839 if (NILP (sentinel
))
6842 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6843 assure that it gets restored no matter how the sentinel exits. */
6845 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6846 /* Inhibit quit so that random quits don't screw up a running filter. */
6847 specbind (Qinhibit_quit
, Qt
);
6848 specbind (Qlast_nonmenu_event
, Qt
);
6850 /* In case we get recursively called,
6851 and we already saved the match data nonrecursively,
6852 save the same match data in safely recursive fashion. */
6853 if (outer_running_asynch_code
)
6856 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6857 restore_search_regs ();
6858 record_unwind_save_match_data ();
6859 Fset_match_data (tem
, Qt
);
6862 /* For speed, if a search happens within this code,
6863 save the match data in a special nonrecursive fashion. */
6864 running_asynch_code
= 1;
6866 internal_condition_case_1 (read_process_output_call
,
6868 Fcons (proc
, Fcons (reason
, Qnil
))),
6869 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6870 exec_sentinel_error_handler
);
6872 /* If we saved the match data nonrecursively, restore it now. */
6873 restore_search_regs ();
6874 running_asynch_code
= outer_running_asynch_code
;
6876 Vdeactivate_mark
= odeactivate
;
6878 /* Restore waiting_for_user_input_p as it was
6879 when we were called, in case the filter clobbered it. */
6880 waiting_for_user_input_p
= waiting
;
6883 if (! EQ (Fcurrent_buffer (), obuffer
)
6884 || ! EQ (current_buffer
->keymap
, okeymap
))
6886 /* But do it only if the caller is actually going to read events.
6887 Otherwise there's no need to make him wake up, and it could
6888 cause trouble (for example it would make sit_for return). */
6889 if (waiting_for_user_input_p
== -1)
6890 record_asynch_buffer_change ();
6892 unbind_to (count
, Qnil
);
6895 /* Report all recent events of a change in process status
6896 (either run the sentinel or output a message).
6897 This is usually done while Emacs is waiting for keyboard input
6898 but can be done at other times. */
6901 status_notify (deleting_process
)
6902 struct Lisp_Process
*deleting_process
;
6904 register Lisp_Object proc
, buffer
;
6905 Lisp_Object tail
, msg
;
6906 struct gcpro gcpro1
, gcpro2
;
6910 /* We need to gcpro tail; if read_process_output calls a filter
6911 which deletes a process and removes the cons to which tail points
6912 from Vprocess_alist, and then causes a GC, tail is an unprotected
6916 /* Set this now, so that if new processes are created by sentinels
6917 that we run, we get called again to handle their status changes. */
6918 update_tick
= process_tick
;
6920 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6923 register struct Lisp_Process
*p
;
6925 proc
= Fcdr (XCAR (tail
));
6926 p
= XPROCESS (proc
);
6928 if (p
->tick
!= p
->update_tick
)
6930 p
->update_tick
= p
->tick
;
6932 /* If process is still active, read any output that remains. */
6933 while (! EQ (p
->filter
, Qt
)
6934 && ! EQ (p
->status
, Qconnect
)
6935 && ! EQ (p
->status
, Qlisten
)
6936 /* Network or serial process not stopped: */
6937 && ! EQ (p
->command
, Qt
)
6939 && p
!= deleting_process
6940 && read_process_output (proc
, p
->infd
) > 0);
6944 /* Get the text to use for the message. */
6945 if (p
->raw_status_new
)
6947 msg
= status_message (p
);
6949 /* If process is terminated, deactivate it or delete it. */
6951 if (CONSP (p
->status
))
6952 symbol
= XCAR (p
->status
);
6954 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6955 || EQ (symbol
, Qclosed
))
6957 if (delete_exited_processes
)
6958 remove_process (proc
);
6960 deactivate_process (proc
);
6963 /* The actions above may have further incremented p->tick.
6964 So set p->update_tick again
6965 so that an error in the sentinel will not cause
6966 this code to be run again. */
6967 p
->update_tick
= p
->tick
;
6968 /* Now output the message suitably. */
6969 if (!NILP (p
->sentinel
))
6970 exec_sentinel (proc
, msg
);
6971 /* Don't bother with a message in the buffer
6972 when a process becomes runnable. */
6973 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6975 Lisp_Object ro
, tem
;
6976 struct buffer
*old
= current_buffer
;
6977 int opoint
, opoint_byte
;
6978 int before
, before_byte
;
6980 ro
= XBUFFER (buffer
)->read_only
;
6982 /* Avoid error if buffer is deleted
6983 (probably that's why the process is dead, too) */
6984 if (NILP (XBUFFER (buffer
)->name
))
6986 Fset_buffer (buffer
);
6989 opoint_byte
= PT_BYTE
;
6990 /* Insert new output into buffer
6991 at the current end-of-output marker,
6992 thus preserving logical ordering of input and output. */
6993 if (XMARKER (p
->mark
)->buffer
)
6994 Fgoto_char (p
->mark
);
6996 SET_PT_BOTH (ZV
, ZV_BYTE
);
6999 before_byte
= PT_BYTE
;
7001 tem
= current_buffer
->read_only
;
7002 current_buffer
->read_only
= Qnil
;
7003 insert_string ("\nProcess ");
7004 Finsert (1, &p
->name
);
7005 insert_string (" ");
7007 current_buffer
->read_only
= tem
;
7008 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
7010 if (opoint
>= before
)
7011 SET_PT_BOTH (opoint
+ (PT
- before
),
7012 opoint_byte
+ (PT_BYTE
- before_byte
));
7014 SET_PT_BOTH (opoint
, opoint_byte
);
7016 set_buffer_internal (old
);
7021 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
7022 redisplay_preserve_echo_area (13);
7028 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
7029 Sset_process_coding_system
, 1, 3, 0,
7030 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
7031 DECODING will be used to decode subprocess output and ENCODING to
7032 encode subprocess input. */)
7033 (process
, decoding
, encoding
)
7034 register Lisp_Object process
, decoding
, encoding
;
7036 register struct Lisp_Process
*p
;
7038 CHECK_PROCESS (process
);
7039 p
= XPROCESS (process
);
7041 error ("Input file descriptor of %s closed", SDATA (p
->name
));
7043 error ("Output file descriptor of %s closed", SDATA (p
->name
));
7044 Fcheck_coding_system (decoding
);
7045 Fcheck_coding_system (encoding
);
7046 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
7047 p
->decode_coding_system
= decoding
;
7048 p
->encode_coding_system
= encoding
;
7049 setup_process_coding_systems (process
);
7054 DEFUN ("process-coding-system",
7055 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
7056 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
7058 register Lisp_Object process
;
7060 CHECK_PROCESS (process
);
7061 return Fcons (XPROCESS (process
)->decode_coding_system
,
7062 XPROCESS (process
)->encode_coding_system
);
7065 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
7066 Sset_process_filter_multibyte
, 2, 2, 0,
7067 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
7068 If FLAG is non-nil, the filter is given multibyte strings.
7069 If FLAG is nil, the filter is given unibyte strings. In this case,
7070 all character code conversion except for end-of-line conversion is
7073 Lisp_Object process
, flag
;
7075 register struct Lisp_Process
*p
;
7077 CHECK_PROCESS (process
);
7078 p
= XPROCESS (process
);
7080 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
7081 setup_process_coding_systems (process
);
7086 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
7087 Sprocess_filter_multibyte_p
, 1, 1, 0,
7088 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
7090 Lisp_Object process
;
7092 register struct Lisp_Process
*p
;
7093 struct coding_system
*coding
;
7095 CHECK_PROCESS (process
);
7096 p
= XPROCESS (process
);
7097 coding
= proc_decode_coding_system
[p
->infd
];
7098 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
7103 /* Add DESC to the set of keyboard input descriptors. */
7106 add_keyboard_wait_descriptor (desc
)
7109 FD_SET (desc
, &input_wait_mask
);
7110 FD_SET (desc
, &non_process_wait_mask
);
7111 if (desc
> max_keyboard_desc
)
7112 max_keyboard_desc
= desc
;
7115 static int add_gpm_wait_descriptor_called_flag
;
7118 add_gpm_wait_descriptor (desc
)
7121 if (! add_gpm_wait_descriptor_called_flag
)
7122 FD_CLR (0, &input_wait_mask
);
7123 add_gpm_wait_descriptor_called_flag
= 1;
7124 FD_SET (desc
, &input_wait_mask
);
7125 FD_SET (desc
, &gpm_wait_mask
);
7126 if (desc
> max_gpm_desc
)
7127 max_gpm_desc
= desc
;
7130 /* From now on, do not expect DESC to give keyboard input. */
7133 delete_keyboard_wait_descriptor (desc
)
7137 int lim
= max_keyboard_desc
;
7139 FD_CLR (desc
, &input_wait_mask
);
7140 FD_CLR (desc
, &non_process_wait_mask
);
7142 if (desc
== max_keyboard_desc
)
7143 for (fd
= 0; fd
< lim
; fd
++)
7144 if (FD_ISSET (fd
, &input_wait_mask
)
7145 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7146 && !FD_ISSET (fd
, &gpm_wait_mask
))
7147 max_keyboard_desc
= fd
;
7151 delete_gpm_wait_descriptor (desc
)
7155 int lim
= max_gpm_desc
;
7157 FD_CLR (desc
, &input_wait_mask
);
7158 FD_CLR (desc
, &non_process_wait_mask
);
7160 if (desc
== max_gpm_desc
)
7161 for (fd
= 0; fd
< lim
; fd
++)
7162 if (FD_ISSET (fd
, &input_wait_mask
)
7163 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7164 && !FD_ISSET (fd
, &non_process_wait_mask
))
7168 /* Return nonzero if *MASK has a bit set
7169 that corresponds to one of the keyboard input descriptors. */
7172 keyboard_bit_set (mask
)
7177 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
7178 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
7179 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
7190 inhibit_sentinels
= 0;
7194 if (! noninteractive
|| initialized
)
7196 signal (SIGCHLD
, sigchld_handler
);
7199 FD_ZERO (&input_wait_mask
);
7200 FD_ZERO (&non_keyboard_wait_mask
);
7201 FD_ZERO (&non_process_wait_mask
);
7202 max_process_desc
= 0;
7204 #ifdef NON_BLOCKING_CONNECT
7205 FD_ZERO (&connect_wait_mask
);
7206 num_pending_connects
= 0;
7209 #ifdef ADAPTIVE_READ_BUFFERING
7210 process_output_delay_count
= 0;
7211 process_output_skip
= 0;
7214 /* Don't do this, it caused infinite select loops. The display
7215 method should call add_keyboard_wait_descriptor on stdin if it
7218 FD_SET (0, &input_wait_mask
);
7221 Vprocess_alist
= Qnil
;
7223 deleted_pid_list
= Qnil
;
7225 for (i
= 0; i
< MAXDESC
; i
++)
7227 chan_process
[i
] = Qnil
;
7228 proc_buffered_char
[i
] = -1;
7230 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
7231 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
7232 #ifdef DATAGRAM_SOCKETS
7233 bzero (datagram_address
, sizeof datagram_address
);
7238 Lisp_Object subfeatures
= Qnil
;
7239 struct socket_options
*sopt
;
7241 #define ADD_SUBFEATURE(key, val) \
7242 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7244 #ifdef NON_BLOCKING_CONNECT
7245 ADD_SUBFEATURE (QCnowait
, Qt
);
7247 #ifdef DATAGRAM_SOCKETS
7248 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7250 #ifdef HAVE_LOCAL_SOCKETS
7251 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7253 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7255 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7257 #ifdef HAVE_GETSOCKNAME
7258 ADD_SUBFEATURE (QCservice
, Qt
);
7260 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7261 ADD_SUBFEATURE (QCserver
, Qt
);
7264 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7265 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7267 Fprovide (intern ("make-network-process"), subfeatures
);
7269 #endif /* HAVE_SOCKETS */
7271 #if defined (DARWIN)
7272 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7273 processes. As such, we only change the default value. */
7276 char *release
= get_operating_system_release();
7277 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7278 && release
[1] == '.')) {
7279 Vprocess_connection_type
= Qnil
;
7288 Qprocessp
= intern ("processp");
7289 staticpro (&Qprocessp
);
7290 Qrun
= intern ("run");
7292 Qstop
= intern ("stop");
7294 Qsignal
= intern ("signal");
7295 staticpro (&Qsignal
);
7297 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7300 Qexit = intern ("exit");
7301 staticpro (&Qexit); */
7303 Qopen
= intern ("open");
7305 Qclosed
= intern ("closed");
7306 staticpro (&Qclosed
);
7307 Qconnect
= intern ("connect");
7308 staticpro (&Qconnect
);
7309 Qfailed
= intern ("failed");
7310 staticpro (&Qfailed
);
7311 Qlisten
= intern ("listen");
7312 staticpro (&Qlisten
);
7313 Qlocal
= intern ("local");
7314 staticpro (&Qlocal
);
7315 Qipv4
= intern ("ipv4");
7318 Qipv6
= intern ("ipv6");
7321 Qdatagram
= intern ("datagram");
7322 staticpro (&Qdatagram
);
7324 QCport
= intern (":port");
7325 staticpro (&QCport
);
7326 QCspeed
= intern (":speed");
7327 staticpro (&QCspeed
);
7328 QCprocess
= intern (":process");
7329 staticpro (&QCprocess
);
7331 QCbytesize
= intern (":bytesize");
7332 staticpro (&QCbytesize
);
7333 QCstopbits
= intern (":stopbits");
7334 staticpro (&QCstopbits
);
7335 QCparity
= intern (":parity");
7336 staticpro (&QCparity
);
7337 Qodd
= intern ("odd");
7339 Qeven
= intern ("even");
7341 QCflowcontrol
= intern (":flowcontrol");
7342 staticpro (&QCflowcontrol
);
7343 Qhw
= intern ("hw");
7345 Qsw
= intern ("sw");
7347 QCsummary
= intern (":summary");
7348 staticpro (&QCsummary
);
7350 Qreal
= intern ("real");
7352 Qnetwork
= intern ("network");
7353 staticpro (&Qnetwork
);
7354 Qserial
= intern ("serial");
7355 staticpro (&Qserial
);
7357 QCname
= intern (":name");
7358 staticpro (&QCname
);
7359 QCbuffer
= intern (":buffer");
7360 staticpro (&QCbuffer
);
7361 QChost
= intern (":host");
7362 staticpro (&QChost
);
7363 QCservice
= intern (":service");
7364 staticpro (&QCservice
);
7365 QCtype
= intern (":type");
7366 staticpro (&QCtype
);
7367 QClocal
= intern (":local");
7368 staticpro (&QClocal
);
7369 QCremote
= intern (":remote");
7370 staticpro (&QCremote
);
7371 QCcoding
= intern (":coding");
7372 staticpro (&QCcoding
);
7373 QCserver
= intern (":server");
7374 staticpro (&QCserver
);
7375 QCnowait
= intern (":nowait");
7376 staticpro (&QCnowait
);
7377 QCsentinel
= intern (":sentinel");
7378 staticpro (&QCsentinel
);
7379 QClog
= intern (":log");
7381 QCnoquery
= intern (":noquery");
7382 staticpro (&QCnoquery
);
7383 QCstop
= intern (":stop");
7384 staticpro (&QCstop
);
7385 QCoptions
= intern (":options");
7386 staticpro (&QCoptions
);
7387 QCplist
= intern (":plist");
7388 staticpro (&QCplist
);
7390 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7391 staticpro (&Qlast_nonmenu_event
);
7393 staticpro (&Vprocess_alist
);
7395 staticpro (&deleted_pid_list
);
7398 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7399 doc
: /* *Non-nil means delete processes immediately when they exit.
7400 A value of nil means don't delete them until `list-processes' is run. */);
7402 delete_exited_processes
= 1;
7404 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7405 doc
: /* Control type of device used to communicate with subprocesses.
7406 Values are nil to use a pipe, or t or `pty' to use a pty.
7407 The value has no effect if the system has no ptys or if all ptys are busy:
7408 then a pipe is used in any case.
7409 The value takes effect when `start-process' is called. */);
7410 Vprocess_connection_type
= Qt
;
7412 #ifdef ADAPTIVE_READ_BUFFERING
7413 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7414 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7415 On some systems, when Emacs reads the output from a subprocess, the output data
7416 is read in very small blocks, potentially resulting in very poor performance.
7417 This behavior can be remedied to some extent by setting this variable to a
7418 non-nil value, as it will automatically delay reading from such processes, to
7419 allow them to produce more output before Emacs tries to read it.
7420 If the value is t, the delay is reset after each write to the process; any other
7421 non-nil value means that the delay is not reset on write.
7422 The variable takes effect when `start-process' is called. */);
7423 Vprocess_adaptive_read_buffering
= Qt
;
7426 defsubr (&Sprocessp
);
7427 defsubr (&Sget_process
);
7428 defsubr (&Sget_buffer_process
);
7429 defsubr (&Sdelete_process
);
7430 defsubr (&Sprocess_status
);
7431 defsubr (&Sprocess_exit_status
);
7432 defsubr (&Sprocess_id
);
7433 defsubr (&Sprocess_name
);
7434 defsubr (&Sprocess_tty_name
);
7435 defsubr (&Sprocess_command
);
7436 defsubr (&Sset_process_buffer
);
7437 defsubr (&Sprocess_buffer
);
7438 defsubr (&Sprocess_mark
);
7439 defsubr (&Sset_process_filter
);
7440 defsubr (&Sprocess_filter
);
7441 defsubr (&Sset_process_sentinel
);
7442 defsubr (&Sprocess_sentinel
);
7443 defsubr (&Sset_process_window_size
);
7444 defsubr (&Sset_process_inherit_coding_system_flag
);
7445 defsubr (&Sprocess_inherit_coding_system_flag
);
7446 defsubr (&Sset_process_query_on_exit_flag
);
7447 defsubr (&Sprocess_query_on_exit_flag
);
7448 defsubr (&Sprocess_contact
);
7449 defsubr (&Sprocess_plist
);
7450 defsubr (&Sset_process_plist
);
7451 defsubr (&Slist_processes
);
7452 defsubr (&Sprocess_list
);
7453 defsubr (&Sstart_process
);
7455 defsubr (&Sserial_process_configure
);
7456 defsubr (&Smake_serial_process
);
7457 #endif /* HAVE_SERIAL */
7459 defsubr (&Sset_network_process_option
);
7460 defsubr (&Smake_network_process
);
7461 defsubr (&Sformat_network_address
);
7462 #endif /* HAVE_SOCKETS */
7463 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7465 defsubr (&Snetwork_interface_list
);
7467 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7468 defsubr (&Snetwork_interface_info
);
7470 #endif /* HAVE_SOCKETS ... */
7471 #ifdef DATAGRAM_SOCKETS
7472 defsubr (&Sprocess_datagram_address
);
7473 defsubr (&Sset_process_datagram_address
);
7475 defsubr (&Saccept_process_output
);
7476 defsubr (&Sprocess_send_region
);
7477 defsubr (&Sprocess_send_string
);
7478 defsubr (&Sinterrupt_process
);
7479 defsubr (&Skill_process
);
7480 defsubr (&Squit_process
);
7481 defsubr (&Sstop_process
);
7482 defsubr (&Scontinue_process
);
7483 defsubr (&Sprocess_running_child_p
);
7484 defsubr (&Sprocess_send_eof
);
7485 defsubr (&Ssignal_process
);
7486 defsubr (&Swaiting_for_user_input_p
);
7487 defsubr (&Sprocess_type
);
7488 defsubr (&Sset_process_coding_system
);
7489 defsubr (&Sprocess_coding_system
);
7490 defsubr (&Sset_process_filter_multibyte
);
7491 defsubr (&Sprocess_filter_multibyte_p
);
7495 #else /* not subprocesses */
7497 #include <sys/types.h>
7501 #include "systime.h"
7502 #include "character.h"
7504 #include "termopts.h"
7505 #include "sysselect.h"
7507 extern int frame_garbaged
;
7509 extern EMACS_TIME
timer_check ();
7510 extern int timers_run
;
7514 /* As described above, except assuming that there are no subprocesses:
7516 Wait for timeout to elapse and/or keyboard input to be available.
7519 timeout in seconds, or
7520 zero for no limit, or
7521 -1 means gobble data immediately available but don't wait for any.
7523 read_kbd is a Lisp_Object:
7524 0 to ignore keyboard input, or
7525 1 to return when input is available, or
7526 -1 means caller will actually read the input, so don't throw to
7529 see full version for other parameters. We know that wait_proc will
7530 always be NULL, since `subprocesses' isn't defined.
7532 do_display != 0 means redisplay should be done to show subprocess
7533 output that arrives.
7535 Return true if we received input from any process. */
7538 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7539 wait_for_cell
, wait_proc
, just_wait_proc
)
7540 int time_limit
, microsecs
, read_kbd
, do_display
;
7541 Lisp_Object wait_for_cell
;
7542 struct Lisp_Process
*wait_proc
;
7546 EMACS_TIME end_time
, timeout
;
7547 SELECT_TYPE waitchannels
;
7550 /* What does time_limit really mean? */
7551 if (time_limit
|| microsecs
)
7553 EMACS_GET_TIME (end_time
);
7554 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7555 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7558 /* Turn off periodic alarms (in case they are in use)
7559 and then turn off any other atimers,
7560 because the select emulator uses alarms. */
7562 turn_on_atimers (0);
7566 int timeout_reduced_for_timers
= 0;
7568 /* If calling from keyboard input, do not quit
7569 since we want to return C-g as an input character.
7570 Otherwise, do pending quit if requested. */
7574 /* Exit now if the cell we're waiting for became non-nil. */
7575 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7578 /* Compute time from now till when time limit is up */
7579 /* Exit if already run out */
7580 if (time_limit
== -1)
7582 /* -1 specified for timeout means
7583 gobble output available now
7584 but don't wait at all. */
7586 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7588 else if (time_limit
|| microsecs
)
7590 EMACS_GET_TIME (timeout
);
7591 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7592 if (EMACS_TIME_NEG_P (timeout
))
7597 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7600 /* If our caller will not immediately handle keyboard events,
7601 run timer events directly.
7602 (Callers that will immediately read keyboard events
7603 call timer_delay on their own.) */
7604 if (NILP (wait_for_cell
))
7606 EMACS_TIME timer_delay
;
7610 int old_timers_run
= timers_run
;
7611 timer_delay
= timer_check (1);
7612 if (timers_run
!= old_timers_run
&& do_display
)
7613 /* We must retry, since a timer may have requeued itself
7614 and that could alter the time delay. */
7615 redisplay_preserve_echo_area (14);
7619 while (!detect_input_pending ());
7621 /* If there is unread keyboard input, also return. */
7623 && requeued_events_pending_p ())
7626 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7628 EMACS_TIME difference
;
7629 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7630 if (EMACS_TIME_NEG_P (difference
))
7632 timeout
= timer_delay
;
7633 timeout_reduced_for_timers
= 1;
7638 /* Cause C-g and alarm signals to take immediate action,
7639 and cause input available signals to zero out timeout. */
7641 set_waiting_for_input (&timeout
);
7643 /* Wait till there is something to do. */
7645 if (! read_kbd
&& NILP (wait_for_cell
))
7646 FD_ZERO (&waitchannels
);
7648 FD_SET (0, &waitchannels
);
7650 /* If a frame has been newly mapped and needs updating,
7651 reprocess its display stuff. */
7652 if (frame_garbaged
&& do_display
)
7654 clear_waiting_for_input ();
7655 redisplay_preserve_echo_area (15);
7657 set_waiting_for_input (&timeout
);
7660 if (read_kbd
&& detect_input_pending ())
7663 FD_ZERO (&waitchannels
);
7666 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7671 /* Make C-g and alarm signals set flags again */
7672 clear_waiting_for_input ();
7674 /* If we woke up due to SIGWINCH, actually change size now. */
7675 do_pending_window_change (0);
7677 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7678 /* We waited the full specified time, so return now. */
7683 /* If the system call was interrupted, then go around the
7685 if (xerrno
== EINTR
)
7686 FD_ZERO (&waitchannels
);
7688 error ("select error: %s", emacs_strerror (xerrno
));
7691 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7692 /* System sometimes fails to deliver SIGIO. */
7693 kill (getpid (), SIGIO
);
7696 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7697 kill (getpid (), SIGIO
);
7700 /* Check for keyboard input */
7703 && detect_input_pending_run_timers (do_display
))
7705 swallow_events (do_display
);
7706 if (detect_input_pending_run_timers (do_display
))
7710 /* If there is unread keyboard input, also return. */
7712 && requeued_events_pending_p ())
7715 /* If wait_for_cell. check for keyboard input
7716 but don't run any timers.
7717 ??? (It seems wrong to me to check for keyboard
7718 input at all when wait_for_cell, but the code
7719 has been this way since July 1994.
7720 Try changing this after version 19.31.) */
7721 if (! NILP (wait_for_cell
)
7722 && detect_input_pending ())
7724 swallow_events (do_display
);
7725 if (detect_input_pending ())
7729 /* Exit now if the cell we're waiting for became non-nil. */
7730 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7740 /* Don't confuse make-docfile by having two doc strings for this function.
7741 make-docfile does not pay attention to #if, for good reason! */
7742 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7745 register Lisp_Object name
;
7750 /* Don't confuse make-docfile by having two doc strings for this function.
7751 make-docfile does not pay attention to #if, for good reason! */
7752 DEFUN ("process-inherit-coding-system-flag",
7753 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7757 register Lisp_Object process
;
7759 /* Ignore the argument and return the value of
7760 inherit-process-coding-system. */
7761 return inherit_process_coding_system
? Qt
: Qnil
;
7764 /* Kill all processes associated with `buffer'.
7765 If `buffer' is nil, kill all processes.
7766 Since we have no subprocesses, this does nothing. */
7769 kill_buffer_processes (buffer
)
7782 QCtype
= intern (":type");
7783 staticpro (&QCtype
);
7785 defsubr (&Sget_buffer_process
);
7786 defsubr (&Sprocess_inherit_coding_system_flag
);
7790 #endif /* not subprocesses */
7792 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7793 (do not change this comment) */