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 /* TERM is a poor-man's SLIP, used on GNU/Linux. */
76 #if defined(BSD_SYSTEM)
77 #include <sys/ioctl.h>
78 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
80 #endif /* HAVE_PTYS and no O_NDELAY */
81 #endif /* BSD_SYSTEM */
83 #ifdef BROKEN_O_NONBLOCK
85 #endif /* BROKEN_O_NONBLOCK */
91 /* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
93 #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
94 /* sys/ioctl.h may have been included already */
96 #include <sys/ioctl.h>
103 #include <sys/sysmacros.h> /* for "minor" */
104 #endif /* not IRIS */
107 #include <sys/wait.h>
111 #include <netinet/in.h>
112 #include <arpa/nameser.h>
122 #include "character.h"
126 #include "termhooks.h"
127 #include "termopts.h"
128 #include "commands.h"
129 #include "keyboard.h"
130 #include "blockinput.h"
131 #include "dispextern.h"
132 #include "composite.h"
135 Lisp_Object Qprocessp
;
136 Lisp_Object Qrun
, Qstop
, Qsignal
;
137 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
138 Lisp_Object Qlocal
, Qipv4
, Qdatagram
;
139 Lisp_Object Qreal
, Qnetwork
, Qserial
;
143 Lisp_Object QCport
, QCspeed
, QCprocess
;
144 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
145 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
146 Lisp_Object QCname
, QCbuffer
, QChost
, QCservice
, QCtype
;
147 Lisp_Object QClocal
, QCremote
, QCcoding
;
148 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
149 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
150 Lisp_Object Qlast_nonmenu_event
;
151 /* QCfamily is declared and initialized in xfaces.c,
152 QCfilter in keyboard.c. */
153 extern Lisp_Object QCfamily
, QCfilter
;
155 /* Qexit is declared and initialized in eval.c. */
157 /* QCfamily is defined in xfaces.c. */
158 extern Lisp_Object QCfamily
;
159 /* QCfilter is defined in keyboard.c. */
160 extern Lisp_Object QCfilter
;
163 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
164 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
165 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
166 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
168 #define NETCONN_P(p) 0
169 #define NETCONN1_P(p) 0
170 #define SERIALCONN_P(p) 0
171 #define SERIALCONN1_P(p) 0
172 #endif /* HAVE_SOCKETS */
174 /* Define first descriptor number available for subprocesses. */
176 #define FIRST_PROC_DESC 1
178 #define FIRST_PROC_DESC 3
181 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
184 #if !defined (SIGCHLD) && defined (SIGCLD)
185 #define SIGCHLD SIGCLD
188 #include "syssignal.h"
192 extern char *get_operating_system_release ();
194 /* Serial processes require termios or Windows. */
195 #if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
200 /* From sysdep.c or w32.c */
201 extern int serial_open (char *port
);
202 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
209 extern char *sys_errlist
[];
216 /* t means use pty, nil means use a pipe,
217 maybe other values to come. */
218 static Lisp_Object Vprocess_connection_type
;
220 /* These next two vars are non-static since sysdep.c uses them in the
221 emulation of `select'. */
222 /* Number of events of change of status of a process. */
224 /* Number of events for which the user or sentinel has been notified. */
227 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
229 #ifdef BROKEN_NON_BLOCKING_CONNECT
230 #undef NON_BLOCKING_CONNECT
232 #ifndef NON_BLOCKING_CONNECT
235 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
236 #if defined (O_NONBLOCK) || defined (O_NDELAY)
237 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
238 #define NON_BLOCKING_CONNECT
239 #endif /* EWOULDBLOCK || EINPROGRESS */
240 #endif /* O_NONBLOCK || O_NDELAY */
241 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
242 #endif /* HAVE_SELECT */
243 #endif /* HAVE_SOCKETS */
244 #endif /* NON_BLOCKING_CONNECT */
245 #endif /* BROKEN_NON_BLOCKING_CONNECT */
247 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
248 this system. We need to read full packets, so we need a
249 "non-destructive" select. So we require either native select,
250 or emulation of select using FIONREAD. */
252 #ifdef BROKEN_DATAGRAM_SOCKETS
253 #undef DATAGRAM_SOCKETS
255 #ifndef DATAGRAM_SOCKETS
257 #if defined (HAVE_SELECT) || defined (FIONREAD)
258 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
259 #define DATAGRAM_SOCKETS
260 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
261 #endif /* HAVE_SELECT || FIONREAD */
262 #endif /* HAVE_SOCKETS */
263 #endif /* DATAGRAM_SOCKETS */
264 #endif /* BROKEN_DATAGRAM_SOCKETS */
267 #undef NON_BLOCKING_CONNECT
268 #undef DATAGRAM_SOCKETS
271 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
272 #ifdef EMACS_HAS_USECS
273 #define ADAPTIVE_READ_BUFFERING
277 #ifdef ADAPTIVE_READ_BUFFERING
278 #define READ_OUTPUT_DELAY_INCREMENT 10000
279 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
280 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
282 /* Number of processes which have a non-zero read_output_delay,
283 and therefore might be delayed for adaptive read buffering. */
285 static int process_output_delay_count
;
287 /* Non-zero if any process has non-nil read_output_skip. */
289 static int process_output_skip
;
291 /* Non-nil means to delay reading process output to improve buffering.
292 A value of t means that delay is reset after each send, any other
293 non-nil value does not reset the delay. A value of nil disables
294 adaptive read buffering completely. */
295 static Lisp_Object Vprocess_adaptive_read_buffering
;
297 #define process_output_delay_count 0
301 #include "sysselect.h"
303 static int keyboard_bit_set
P_ ((SELECT_TYPE
*));
304 static void deactivate_process
P_ ((Lisp_Object
));
305 static void status_notify
P_ ((struct Lisp_Process
*));
306 static int read_process_output
P_ ((Lisp_Object
, int));
308 /* If we support a window system, turn on the code to poll periodically
309 to detect C-g. It isn't actually used when doing interrupt input. */
310 #ifdef HAVE_WINDOW_SYSTEM
311 #define POLL_FOR_INPUT
314 static Lisp_Object
get_process ();
315 static void exec_sentinel ();
317 extern EMACS_TIME
timer_check ();
318 extern int timers_run
;
320 /* Mask of bits indicating the descriptors that we wait for input on. */
322 static SELECT_TYPE input_wait_mask
;
324 /* Mask that excludes keyboard input descriptor(s). */
326 static SELECT_TYPE non_keyboard_wait_mask
;
328 /* Mask that excludes process input descriptor(s). */
330 static SELECT_TYPE non_process_wait_mask
;
332 /* Mask for the gpm mouse input descriptor. */
334 static SELECT_TYPE gpm_wait_mask
;
336 #ifdef NON_BLOCKING_CONNECT
337 /* Mask of bits indicating the descriptors that we wait for connect to
338 complete on. Once they complete, they are removed from this mask
339 and added to the input_wait_mask and non_keyboard_wait_mask. */
341 static SELECT_TYPE connect_wait_mask
;
343 /* Number of bits set in connect_wait_mask. */
344 static int num_pending_connects
;
346 #define IF_NON_BLOCKING_CONNECT(s) s
348 #define IF_NON_BLOCKING_CONNECT(s)
351 /* The largest descriptor currently in use for a process object. */
352 static int max_process_desc
;
354 /* The largest descriptor currently in use for keyboard input. */
355 static int max_keyboard_desc
;
357 /* The largest descriptor currently in use for gpm mouse input. */
358 static int max_gpm_desc
;
360 /* Nonzero means delete a process right away if it exits. */
361 static int delete_exited_processes
;
363 /* Indexed by descriptor, gives the process (if any) for that descriptor */
364 Lisp_Object chan_process
[MAXDESC
];
366 /* Alist of elements (NAME . PROCESS) */
367 Lisp_Object Vprocess_alist
;
369 /* Buffered-ahead input char from process, indexed by channel.
370 -1 means empty (no char is buffered).
371 Used on sys V where the only way to tell if there is any
372 output from the process is to read at least one char.
373 Always -1 on systems that support FIONREAD. */
375 /* Don't make static; need to access externally. */
376 int proc_buffered_char
[MAXDESC
];
378 /* Table of `struct coding-system' for each process. */
379 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
380 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
382 #ifdef DATAGRAM_SOCKETS
383 /* Table of `partner address' for datagram sockets. */
384 struct sockaddr_and_len
{
387 } datagram_address
[MAXDESC
];
388 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
389 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
391 #define DATAGRAM_CHAN_P(chan) (0)
392 #define DATAGRAM_CONN_P(proc) (0)
395 /* Maximum number of bytes to send to a pty without an eof. */
396 static int pty_max_bytes
;
398 /* Nonzero means don't run process sentinels. This is used
400 int inhibit_sentinels
;
406 /* The file name of the pty opened by allocate_pty. */
408 static char pty_name
[24];
411 /* Compute the Lisp form of the process status, p->status, from
412 the numeric status that was returned by `wait'. */
414 static Lisp_Object
status_convert ();
418 struct Lisp_Process
*p
;
420 union { int i
; WAITTYPE wt
; } u
;
421 eassert (p
->raw_status_new
);
423 p
->status
= status_convert (u
.wt
);
424 p
->raw_status_new
= 0;
427 /* Convert a process status word in Unix format to
428 the list that we use internally. */
435 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
436 else if (WIFEXITED (w
))
437 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
438 WCOREDUMP (w
) ? Qt
: Qnil
));
439 else if (WIFSIGNALED (w
))
440 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
441 WCOREDUMP (w
) ? Qt
: Qnil
));
446 /* Given a status-list, extract the three pieces of information
447 and store them individually through the three pointers. */
450 decode_status (l
, symbol
, code
, coredump
)
468 *code
= XFASTINT (XCAR (tem
));
470 *coredump
= !NILP (tem
);
474 /* Return a string describing a process status list. */
478 struct Lisp_Process
*p
;
480 Lisp_Object status
= p
->status
;
483 Lisp_Object string
, string2
;
485 decode_status (status
, &symbol
, &code
, &coredump
);
487 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
490 synchronize_system_messages_locale ();
491 signame
= strsignal (code
);
494 string
= build_string (signame
);
495 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
496 SSET (string
, 0, DOWNCASE (SREF (string
, 0)));
497 return concat2 (string
, string2
);
499 else if (EQ (symbol
, Qexit
))
502 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
504 return build_string ("finished\n");
505 string
= Fnumber_to_string (make_number (code
));
506 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
507 return concat3 (build_string ("exited abnormally with code "),
510 else if (EQ (symbol
, Qfailed
))
512 string
= Fnumber_to_string (make_number (code
));
513 string2
= build_string ("\n");
514 return concat3 (build_string ("failed with code "),
518 return Fcopy_sequence (Fsymbol_name (symbol
));
523 /* Open an available pty, returning a file descriptor.
524 Return -1 on failure.
525 The file name of the terminal corresponding to the pty
526 is left in the variable pty_name. */
537 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
538 for (i
= 0; i
< 16; i
++)
541 struct stat stb
; /* Used in some PTY_OPEN. */
542 #ifdef PTY_NAME_SPRINTF
545 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
546 #endif /* no PTY_NAME_SPRINTF */
550 #else /* no PTY_OPEN */
553 /* Unusual IRIS code */
554 *ptyv
= emacs_open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
557 if (fstat (fd
, &stb
) < 0)
559 # else /* not IRIS */
560 { /* Some systems name their pseudoterminals so that there are gaps in
561 the usual sequence - for example, on HP9000/S700 systems, there
562 are no pseudoterminals with names ending in 'f'. So we wait for
563 three failures in a row before deciding that we've reached the
565 int failed_count
= 0;
567 if (stat (pty_name
, &stb
) < 0)
570 if (failed_count
>= 3)
577 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
579 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
581 # endif /* not IRIS */
583 #endif /* no PTY_OPEN */
587 /* check to make certain that both sides are available
588 this avoids a nasty yet stupid bug in rlogins */
589 #ifdef PTY_TTY_NAME_SPRINTF
592 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
593 #endif /* no PTY_TTY_NAME_SPRINTF */
594 if (access (pty_name
, 6) != 0)
597 # if !defined(IRIS) && !defined(__sgi)
609 #endif /* HAVE_PTYS */
615 register Lisp_Object val
, tem
, name1
;
616 register struct Lisp_Process
*p
;
620 p
= allocate_process ();
628 p
->raw_status_new
= 0;
630 p
->mark
= Fmake_marker ();
632 #ifdef ADAPTIVE_READ_BUFFERING
633 p
->adaptive_read_buffering
= 0;
634 p
->read_output_delay
= 0;
635 p
->read_output_skip
= 0;
638 /* If name is already in use, modify it until it is unused. */
643 tem
= Fget_process (name1
);
644 if (NILP (tem
)) break;
645 sprintf (suffix
, "<%d>", i
);
646 name1
= concat2 (name
, build_string (suffix
));
650 XSETPROCESS (val
, p
);
651 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
656 remove_process (proc
)
657 register Lisp_Object proc
;
659 register Lisp_Object pair
;
661 pair
= Frassq (proc
, Vprocess_alist
);
662 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
664 deactivate_process (proc
);
667 /* Setup coding systems of PROCESS. */
670 setup_process_coding_systems (process
)
673 struct Lisp_Process
*p
= XPROCESS (process
);
675 int outch
= p
->outfd
;
676 Lisp_Object coding_system
;
678 if (inch
< 0 || outch
< 0)
681 if (!proc_decode_coding_system
[inch
])
682 proc_decode_coding_system
[inch
]
683 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
684 coding_system
= p
->decode_coding_system
;
685 if (! NILP (p
->filter
))
687 else if (BUFFERP (p
->buffer
))
689 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
690 coding_system
= raw_text_coding_system (coding_system
);
692 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
694 if (!proc_encode_coding_system
[outch
])
695 proc_encode_coding_system
[outch
]
696 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
697 setup_coding_system (p
->encode_coding_system
,
698 proc_encode_coding_system
[outch
]);
701 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
702 doc
: /* Return t if OBJECT is a process. */)
706 return PROCESSP (object
) ? Qt
: Qnil
;
709 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
710 doc
: /* Return the process named NAME, or nil if there is none. */)
712 register Lisp_Object name
;
717 return Fcdr (Fassoc (name
, Vprocess_alist
));
720 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
721 doc
: /* Return the (or a) process associated with BUFFER.
722 BUFFER may be a buffer or the name of one. */)
724 register Lisp_Object buffer
;
726 register Lisp_Object buf
, tail
, proc
;
728 if (NILP (buffer
)) return Qnil
;
729 buf
= Fget_buffer (buffer
);
730 if (NILP (buf
)) return Qnil
;
732 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
734 proc
= Fcdr (XCAR (tail
));
735 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
741 /* This is how commands for the user decode process arguments. It
742 accepts a process, a process name, a buffer, a buffer name, or nil.
743 Buffers denote the first process in the buffer, and nil denotes the
748 register Lisp_Object name
;
750 register Lisp_Object proc
, obj
;
753 obj
= Fget_process (name
);
755 obj
= Fget_buffer (name
);
757 error ("Process %s does not exist", SDATA (name
));
759 else if (NILP (name
))
760 obj
= Fcurrent_buffer ();
764 /* Now obj should be either a buffer object or a process object.
768 proc
= Fget_buffer_process (obj
);
770 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
782 /* Fdelete_process promises to immediately forget about the process, but in
783 reality, Emacs needs to remember those processes until they have been
784 treated by sigchld_handler; otherwise this handler would consider the
785 process as being synchronous and say that the synchronous process is
787 static Lisp_Object deleted_pid_list
;
790 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
791 doc
: /* Delete PROCESS: kill it and forget about it immediately.
792 PROCESS may be a process, a buffer, the name of a process or buffer, or
793 nil, indicating the current buffer's process. */)
795 register Lisp_Object process
;
797 register struct Lisp_Process
*p
;
799 process
= get_process (process
);
800 p
= XPROCESS (process
);
802 p
->raw_status_new
= 0;
803 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
805 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
806 p
->tick
= ++process_tick
;
809 else if (p
->infd
>= 0)
813 /* Assignment to EMACS_INT stops GCC whining about limited range
815 EMACS_INT pid
= p
->pid
;
817 /* No problem storing the pid here, as it is still in Vprocess_alist. */
818 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
819 /* GC treated elements set to nil. */
820 Fdelq (Qnil
, deleted_pid_list
));
821 /* If the process has already signaled, remove it from the list. */
822 if (p
->raw_status_new
)
825 if (CONSP (p
->status
))
826 symbol
= XCAR (p
->status
);
827 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
829 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
833 Fkill_process (process
, Qnil
);
834 /* Do this now, since remove_process will make sigchld_handler do nothing. */
836 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
837 p
->tick
= ++process_tick
;
841 remove_process (process
);
845 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
846 doc
: /* Return the status of PROCESS.
847 The returned value is one of the following symbols:
848 run -- for a process that is running.
849 stop -- for a process stopped but continuable.
850 exit -- for a process that has exited.
851 signal -- for a process that has got a fatal signal.
852 open -- for a network stream connection that is open.
853 listen -- for a network stream server that is listening.
854 closed -- for a network stream connection that is closed.
855 connect -- when waiting for a non-blocking connection to complete.
856 failed -- when a non-blocking connection has failed.
857 nil -- if arg is a process name and no such process exists.
858 PROCESS may be a process, a buffer, the name of a process, or
859 nil, indicating the current buffer's process. */)
861 register Lisp_Object process
;
863 register struct Lisp_Process
*p
;
864 register Lisp_Object status
;
866 if (STRINGP (process
))
867 process
= Fget_process (process
);
869 process
= get_process (process
);
874 p
= XPROCESS (process
);
875 if (p
->raw_status_new
)
879 status
= XCAR (status
);
880 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
882 if (EQ (status
, Qexit
))
884 else if (EQ (p
->command
, Qt
))
886 else if (EQ (status
, Qrun
))
892 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
894 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
895 If PROCESS has not yet exited or died, return 0. */)
897 register Lisp_Object process
;
899 CHECK_PROCESS (process
);
900 if (XPROCESS (process
)->raw_status_new
)
901 update_status (XPROCESS (process
));
902 if (CONSP (XPROCESS (process
)->status
))
903 return XCAR (XCDR (XPROCESS (process
)->status
));
904 return make_number (0);
907 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
908 doc
: /* Return the process id of PROCESS.
909 This is the pid of the external process which PROCESS uses or talks to.
910 For a network connection, this value is nil. */)
912 register Lisp_Object process
;
914 /* Assignment to EMACS_INT stops GCC whining about limited range of
918 CHECK_PROCESS (process
);
919 pid
= XPROCESS (process
)->pid
;
920 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
923 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
924 doc
: /* Return the name of PROCESS, as a string.
925 This is the name of the program invoked in PROCESS,
926 possibly modified to make it unique among process names. */)
928 register Lisp_Object process
;
930 CHECK_PROCESS (process
);
931 return XPROCESS (process
)->name
;
934 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
935 doc
: /* Return the command that was executed to start PROCESS.
936 This is a list of strings, the first string being the program executed
937 and the rest of the strings being the arguments given to it.
938 For a network or serial process, this is nil (process is running) or t
939 \(process is stopped). */)
941 register Lisp_Object process
;
943 CHECK_PROCESS (process
);
944 return XPROCESS (process
)->command
;
947 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
948 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
949 This is the terminal that the process itself reads and writes on,
950 not the name of the pty that Emacs uses to talk with that terminal. */)
952 register Lisp_Object process
;
954 CHECK_PROCESS (process
);
955 return XPROCESS (process
)->tty_name
;
958 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
960 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
962 register Lisp_Object process
, buffer
;
964 struct Lisp_Process
*p
;
966 CHECK_PROCESS (process
);
968 CHECK_BUFFER (buffer
);
969 p
= XPROCESS (process
);
971 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
972 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
973 setup_process_coding_systems (process
);
977 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
979 doc
: /* Return the buffer PROCESS is associated with.
980 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
982 register Lisp_Object process
;
984 CHECK_PROCESS (process
);
985 return XPROCESS (process
)->buffer
;
988 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
990 doc
: /* Return the marker for the end of the last output from PROCESS. */)
992 register Lisp_Object process
;
994 CHECK_PROCESS (process
);
995 return XPROCESS (process
)->mark
;
998 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
1000 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
1001 A value of t means stop accepting output from the process.
1003 When a process has a filter, its buffer is not used for output.
1004 Instead, each time it does output, the entire string of output is
1005 passed to the filter.
1007 The filter gets two arguments: the process and the string of output.
1008 The string argument is normally a multibyte string, except:
1009 - if the process' input coding system is no-conversion or raw-text,
1010 it is a unibyte string (the non-converted input), or else
1011 - if `default-enable-multibyte-characters' is nil, it is a unibyte
1012 string (the result of converting the decoded input multibyte
1013 string to unibyte with `string-make-unibyte'). */)
1015 register Lisp_Object process
, filter
;
1017 struct Lisp_Process
*p
;
1019 CHECK_PROCESS (process
);
1020 p
= XPROCESS (process
);
1022 /* Don't signal an error if the process' input file descriptor
1023 is closed. This could make debugging Lisp more difficult,
1024 for example when doing something like
1026 (setq process (start-process ...))
1028 (set-process-filter process ...) */
1032 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
1034 FD_CLR (p
->infd
, &input_wait_mask
);
1035 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
1037 else if (EQ (p
->filter
, Qt
)
1038 /* Network or serial process not stopped: */
1039 && !EQ (p
->command
, Qt
))
1041 FD_SET (p
->infd
, &input_wait_mask
);
1042 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
1047 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1048 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
1049 setup_process_coding_systems (process
);
1053 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
1055 doc
: /* Returns the filter function of PROCESS; nil if none.
1056 See `set-process-filter' for more info on filter functions. */)
1058 register Lisp_Object process
;
1060 CHECK_PROCESS (process
);
1061 return XPROCESS (process
)->filter
;
1064 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
1066 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
1067 The sentinel is called as a function when the process changes state.
1068 It gets two arguments: the process, and a string describing the change. */)
1070 register Lisp_Object process
, sentinel
;
1072 struct Lisp_Process
*p
;
1074 CHECK_PROCESS (process
);
1075 p
= XPROCESS (process
);
1077 p
->sentinel
= sentinel
;
1078 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1079 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
1083 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
1085 doc
: /* Return the sentinel of PROCESS; nil if none.
1086 See `set-process-sentinel' for more info on sentinels. */)
1088 register Lisp_Object process
;
1090 CHECK_PROCESS (process
);
1091 return XPROCESS (process
)->sentinel
;
1094 DEFUN ("set-process-window-size", Fset_process_window_size
,
1095 Sset_process_window_size
, 3, 3, 0,
1096 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1097 (process
, height
, width
)
1098 register Lisp_Object process
, height
, width
;
1100 CHECK_PROCESS (process
);
1101 CHECK_NATNUM (height
);
1102 CHECK_NATNUM (width
);
1104 if (XPROCESS (process
)->infd
< 0
1105 || set_window_size (XPROCESS (process
)->infd
,
1106 XINT (height
), XINT (width
)) <= 0)
1112 DEFUN ("set-process-inherit-coding-system-flag",
1113 Fset_process_inherit_coding_system_flag
,
1114 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1115 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1116 If the second argument FLAG is non-nil, then the variable
1117 `buffer-file-coding-system' of the buffer associated with PROCESS
1118 will be bound to the value of the coding system used to decode
1121 This is useful when the coding system specified for the process buffer
1122 leaves either the character code conversion or the end-of-line conversion
1123 unspecified, or if the coding system used to decode the process output
1124 is more appropriate for saving the process buffer.
1126 Binding the variable `inherit-process-coding-system' to non-nil before
1127 starting the process is an alternative way of setting the inherit flag
1128 for the process which will run. */)
1130 register Lisp_Object process
, flag
;
1132 CHECK_PROCESS (process
);
1133 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1137 DEFUN ("process-inherit-coding-system-flag",
1138 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
1140 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
1141 If this flag is t, `buffer-file-coding-system' of the buffer
1142 associated with PROCESS will inherit the coding system used to decode
1143 the process output. */)
1145 register Lisp_Object process
;
1147 CHECK_PROCESS (process
);
1148 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
1151 DEFUN ("set-process-query-on-exit-flag",
1152 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1154 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1155 If the second argument FLAG is non-nil, Emacs will query the user before
1156 exiting if PROCESS is running. */)
1158 register Lisp_Object process
, flag
;
1160 CHECK_PROCESS (process
);
1161 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1165 DEFUN ("process-query-on-exit-flag",
1166 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1168 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1170 register Lisp_Object process
;
1172 CHECK_PROCESS (process
);
1173 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1176 #ifdef DATAGRAM_SOCKETS
1177 Lisp_Object
Fprocess_datagram_address ();
1180 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1182 doc
: /* Return the contact info of PROCESS; t for a real child.
1183 For a network or serial connection, the value depends on the optional
1184 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1185 SERVICE) for a network connection or (PORT SPEED) for a serial
1186 connection. If KEY is t, the complete contact information for the
1187 connection is returned, else the specific value for the keyword KEY is
1188 returned. See `make-network-process' or `make-serial-process' for a
1189 list of keywords. */)
1191 register Lisp_Object process
, key
;
1193 Lisp_Object contact
;
1195 CHECK_PROCESS (process
);
1196 contact
= XPROCESS (process
)->childp
;
1198 #ifdef DATAGRAM_SOCKETS
1199 if (DATAGRAM_CONN_P (process
)
1200 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1201 contact
= Fplist_put (contact
, QCremote
,
1202 Fprocess_datagram_address (process
));
1205 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1207 if (NILP (key
) && NETCONN_P (process
))
1208 return Fcons (Fplist_get (contact
, QChost
),
1209 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1210 if (NILP (key
) && SERIALCONN_P (process
))
1211 return Fcons (Fplist_get (contact
, QCport
),
1212 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1213 return Fplist_get (contact
, key
);
1216 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1218 doc
: /* Return the plist of PROCESS. */)
1220 register Lisp_Object process
;
1222 CHECK_PROCESS (process
);
1223 return XPROCESS (process
)->plist
;
1226 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1228 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1230 register Lisp_Object process
, plist
;
1232 CHECK_PROCESS (process
);
1235 XPROCESS (process
)->plist
= plist
;
1239 #if 0 /* Turned off because we don't currently record this info
1240 in the process. Perhaps add it. */
1241 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1242 doc
: /* Return the connection type of PROCESS.
1243 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1244 a socket connection. */)
1246 Lisp_Object process
;
1248 return XPROCESS (process
)->type
;
1252 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1253 doc
: /* Return the connection type of PROCESS.
1254 The value is either the symbol `real', `network', or `serial'.
1255 PROCESS may be a process, a buffer, the name of a process or buffer, or
1256 nil, indicating the current buffer's process. */)
1258 Lisp_Object process
;
1261 proc
= get_process (process
);
1262 return XPROCESS (proc
)->type
;
1266 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1268 doc
: /* Convert network ADDRESS from internal format to a string.
1269 A 4 or 5 element vector represents an IPv4 address (with port number).
1270 An 8 or 9 element vector represents an IPv6 address (with port number).
1271 If optional second argument OMIT-PORT is non-nil, don't include a port
1272 number in the string, even when present in ADDRESS.
1273 Returns nil if format of ADDRESS is invalid. */)
1274 (address
, omit_port
)
1275 Lisp_Object address
, omit_port
;
1280 if (STRINGP (address
)) /* AF_LOCAL */
1283 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1285 register struct Lisp_Vector
*p
= XVECTOR (address
);
1286 Lisp_Object args
[10];
1289 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1291 args
[0] = build_string ("%d.%d.%d.%d");
1294 else if (p
->size
== 5)
1296 args
[0] = build_string ("%d.%d.%d.%d:%d");
1299 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1301 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1304 else if (p
->size
== 9)
1306 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1312 for (i
= 0; i
< nargs
; i
++)
1314 EMACS_INT element
= XINT (p
->contents
[i
]);
1316 if (element
< 0 || element
> 65535)
1319 if (nargs
<= 5 /* IPv4 */
1320 && i
< 4 /* host, not port */
1324 args
[i
+1] = p
->contents
[i
];
1327 return Fformat (nargs
+1, args
);
1330 if (CONSP (address
))
1332 Lisp_Object args
[2];
1333 args
[0] = build_string ("<Family %d>");
1334 args
[1] = Fcar (address
);
1335 return Fformat (2, args
);
1343 list_processes_1 (query_only
)
1344 Lisp_Object query_only
;
1346 register Lisp_Object tail
, tem
;
1347 Lisp_Object proc
, minspace
, tem1
;
1348 register struct Lisp_Process
*p
;
1350 int w_proc
, w_buffer
, w_tty
;
1352 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1354 w_proc
= 4; /* Proc */
1355 w_buffer
= 6; /* Buffer */
1356 w_tty
= 0; /* Omit if no ttys */
1358 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1362 proc
= Fcdr (XCAR (tail
));
1363 p
= XPROCESS (proc
);
1366 if (!NILP (query_only
) && p
->kill_without_query
)
1368 if (STRINGP (p
->name
)
1369 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1371 if (!NILP (p
->buffer
))
1373 if (NILP (XBUFFER (p
->buffer
)->name
))
1376 w_buffer
= 8; /* (Killed) */
1378 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1381 if (STRINGP (p
->tty_name
)
1382 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1386 XSETFASTINT (i_status
, w_proc
+ 1);
1387 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1390 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1391 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1396 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1399 XSETFASTINT (minspace
, 1);
1401 set_buffer_internal (XBUFFER (Vstandard_output
));
1402 current_buffer
->undo_list
= Qt
;
1404 current_buffer
->truncate_lines
= Qt
;
1406 write_string ("Proc", -1);
1407 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1408 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1411 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1413 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1414 write_string ("\n", -1);
1416 write_string ("----", -1);
1417 Findent_to (i_status
, minspace
); write_string ("------", -1);
1418 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1421 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1423 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1424 write_string ("\n", -1);
1426 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1430 proc
= Fcdr (XCAR (tail
));
1431 p
= XPROCESS (proc
);
1434 if (!NILP (query_only
) && p
->kill_without_query
)
1437 Finsert (1, &p
->name
);
1438 Findent_to (i_status
, minspace
);
1440 if (p
->raw_status_new
)
1443 if (CONSP (p
->status
))
1444 symbol
= XCAR (p
->status
);
1446 if (EQ (symbol
, Qsignal
))
1449 tem
= Fcar (Fcdr (p
->status
));
1451 if (XINT (tem
) < NSIG
)
1452 write_string (sys_errlist
[XINT (tem
)], -1);
1455 Fprinc (symbol
, Qnil
);
1457 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1459 if (EQ (symbol
, Qexit
))
1460 write_string ("closed", -1);
1461 else if (EQ (p
->command
, Qt
))
1462 write_string ("stopped", -1);
1463 else if (EQ (symbol
, Qrun
))
1464 write_string ("open", -1);
1466 Fprinc (symbol
, Qnil
);
1468 else if (SERIALCONN1_P (p
))
1470 write_string ("running", -1);
1473 Fprinc (symbol
, Qnil
);
1475 if (EQ (symbol
, Qexit
))
1478 tem
= Fcar (Fcdr (p
->status
));
1481 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1482 write_string (tembuf
, -1);
1486 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1489 Findent_to (i_buffer
, minspace
);
1490 if (NILP (p
->buffer
))
1491 insert_string ("(none)");
1492 else if (NILP (XBUFFER (p
->buffer
)->name
))
1493 insert_string ("(Killed)");
1495 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1499 Findent_to (i_tty
, minspace
);
1500 if (STRINGP (p
->tty_name
))
1501 Finsert (1, &p
->tty_name
);
1504 Findent_to (i_command
, minspace
);
1506 if (EQ (p
->status
, Qlisten
))
1508 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1509 if (INTEGERP (port
))
1510 port
= Fnumber_to_string (port
);
1512 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1513 sprintf (tembuf
, "(network %s server on %s)\n",
1514 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1515 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1516 insert_string (tembuf
);
1518 else if (NETCONN1_P (p
))
1520 /* For a local socket, there is no host name,
1521 so display service instead. */
1522 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1523 if (!STRINGP (host
))
1525 host
= Fplist_get (p
->childp
, QCservice
);
1526 if (INTEGERP (host
))
1527 host
= Fnumber_to_string (host
);
1530 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1531 sprintf (tembuf
, "(network %s connection to %s)\n",
1532 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1533 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1534 insert_string (tembuf
);
1536 else if (SERIALCONN1_P (p
))
1538 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1539 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1540 insert_string ("(serial port ");
1542 insert_string (SDATA (port
));
1544 insert_string ("?");
1545 if (INTEGERP (speed
))
1547 sprintf (tembuf
, " at %d b/s", XINT (speed
));
1548 insert_string (tembuf
);
1550 insert_string (")\n");
1562 insert_string (" ");
1564 insert_string ("\n");
1568 status_notify (NULL
);
1572 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1573 doc
: /* Display a list of all processes.
1574 If optional argument QUERY-ONLY is non-nil, only processes with
1575 the query-on-exit flag set will be listed.
1576 Any process listed as exited or signaled is actually eliminated
1577 after the listing is made. */)
1579 Lisp_Object query_only
;
1581 internal_with_output_to_temp_buffer ("*Process List*",
1582 list_processes_1
, query_only
);
1586 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1587 doc
: /* Return a list of all processes. */)
1590 return Fmapcar (Qcdr
, Vprocess_alist
);
1593 /* Starting asynchronous inferior processes. */
1595 static Lisp_Object
start_process_unwind ();
1597 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1598 doc
: /* Start a program in a subprocess. Return the process object for it.
1599 NAME is name for process. It is modified if necessary to make it unique.
1600 BUFFER is the buffer (or buffer name) to associate with the process.
1602 Process output (both standard output and standard error streams) goes
1603 at end of BUFFER, unless you specify an output stream or filter
1604 function to handle the output. BUFFER may also be nil, meaning that
1605 this process is not associated with any buffer.
1607 PROGRAM is the program file name. It is searched for in PATH.
1608 Remaining arguments are strings to give program as arguments.
1610 If you want to separate standard output from standard error, invoke
1611 the command through a shell and redirect one of them using the shell
1614 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1617 register Lisp_Object
*args
;
1619 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1621 register unsigned char *new_argv
;
1624 register unsigned char **new_argv
;
1627 int count
= SPECPDL_INDEX ();
1631 buffer
= Fget_buffer_create (buffer
);
1633 /* Make sure that the child will be able to chdir to the current
1634 buffer's current directory, or its unhandled equivalent. We
1635 can't just have the child check for an error when it does the
1636 chdir, since it's in a vfork.
1638 We have to GCPRO around this because Fexpand_file_name and
1639 Funhandled_file_name_directory might call a file name handling
1640 function. The argument list is protected by the caller, so all
1641 we really have to worry about is buffer. */
1643 struct gcpro gcpro1
, gcpro2
;
1645 current_dir
= current_buffer
->directory
;
1647 GCPRO2 (buffer
, current_dir
);
1649 current_dir
= Funhandled_file_name_directory (current_dir
);
1650 if (NILP (current_dir
))
1651 /* If the file name handler says that current_dir is unreachable, use
1652 a sensible default. */
1653 current_dir
= build_string ("~/");
1654 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1655 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1656 report_file_error ("Setting current directory",
1657 Fcons (current_buffer
->directory
, Qnil
));
1663 CHECK_STRING (name
);
1667 CHECK_STRING (program
);
1669 proc
= make_process (name
);
1670 /* If an error occurs and we can't start the process, we want to
1671 remove it from the process list. This means that each error
1672 check in create_process doesn't need to call remove_process
1673 itself; it's all taken care of here. */
1674 record_unwind_protect (start_process_unwind
, proc
);
1676 XPROCESS (proc
)->childp
= Qt
;
1677 XPROCESS (proc
)->plist
= Qnil
;
1678 XPROCESS (proc
)->type
= Qreal
;
1679 XPROCESS (proc
)->buffer
= buffer
;
1680 XPROCESS (proc
)->sentinel
= Qnil
;
1681 XPROCESS (proc
)->filter
= Qnil
;
1682 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1684 #ifdef ADAPTIVE_READ_BUFFERING
1685 XPROCESS (proc
)->adaptive_read_buffering
1686 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1687 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1690 /* Make the process marker point into the process buffer (if any). */
1691 if (BUFFERP (buffer
))
1692 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1693 BUF_ZV (XBUFFER (buffer
)),
1694 BUF_ZV_BYTE (XBUFFER (buffer
)));
1697 /* Decide coding systems for communicating with the process. Here
1698 we don't setup the structure coding_system nor pay attention to
1699 unibyte mode. They are done in create_process. */
1701 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1702 Lisp_Object coding_systems
= Qt
;
1703 Lisp_Object val
, *args2
;
1704 struct gcpro gcpro1
, gcpro2
;
1706 val
= Vcoding_system_for_read
;
1709 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1710 args2
[0] = Qstart_process
;
1711 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1712 GCPRO2 (proc
, current_dir
);
1713 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1715 if (CONSP (coding_systems
))
1716 val
= XCAR (coding_systems
);
1717 else if (CONSP (Vdefault_process_coding_system
))
1718 val
= XCAR (Vdefault_process_coding_system
);
1720 XPROCESS (proc
)->decode_coding_system
= val
;
1722 val
= Vcoding_system_for_write
;
1725 if (EQ (coding_systems
, Qt
))
1727 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1728 args2
[0] = Qstart_process
;
1729 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1730 GCPRO2 (proc
, current_dir
);
1731 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1734 if (CONSP (coding_systems
))
1735 val
= XCDR (coding_systems
);
1736 else if (CONSP (Vdefault_process_coding_system
))
1737 val
= XCDR (Vdefault_process_coding_system
);
1739 XPROCESS (proc
)->encode_coding_system
= val
;
1743 /* Make a one member argv with all args concatenated
1744 together separated by a blank. */
1745 len
= SBYTES (program
) + 2;
1746 for (i
= 3; i
< nargs
; i
++)
1750 len
+= SBYTES (tem
) + 1; /* count the blank */
1752 new_argv
= (unsigned char *) alloca (len
);
1753 strcpy (new_argv
, SDATA (program
));
1754 for (i
= 3; i
< nargs
; i
++)
1758 strcat (new_argv
, " ");
1759 strcat (new_argv
, SDATA (tem
));
1761 /* Need to add code here to check for program existence on VMS */
1764 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1766 /* If program file name is not absolute, search our path for it.
1767 Put the name we will really use in TEM. */
1768 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1769 && !(SCHARS (program
) > 1
1770 && IS_DEVICE_SEP (SREF (program
, 1))))
1772 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1775 GCPRO4 (name
, program
, buffer
, current_dir
);
1776 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1779 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1780 tem
= Fexpand_file_name (tem
, Qnil
);
1784 if (!NILP (Ffile_directory_p (program
)))
1785 error ("Specified program for new process is a directory");
1789 /* If program file name starts with /: for quoting a magic name,
1791 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1792 && SREF (tem
, 1) == ':')
1793 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1795 /* Encode the file name and put it in NEW_ARGV.
1796 That's where the child will use it to execute the program. */
1797 tem
= ENCODE_FILE (tem
);
1798 new_argv
[0] = SDATA (tem
);
1800 /* Here we encode arguments by the coding system used for sending
1801 data to the process. We don't support using different coding
1802 systems for encoding arguments and for encoding data sent to the
1805 for (i
= 3; i
< nargs
; i
++)
1809 if (STRING_MULTIBYTE (tem
))
1810 tem
= (code_convert_string_norecord
1811 (tem
, XPROCESS (proc
)->encode_coding_system
, 1));
1812 new_argv
[i
- 2] = SDATA (tem
);
1814 new_argv
[i
- 2] = 0;
1815 #endif /* not VMS */
1817 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1818 XPROCESS (proc
)->decoding_carryover
= 0;
1819 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1821 XPROCESS (proc
)->inherit_coding_system_flag
1822 = !(NILP (buffer
) || !inherit_process_coding_system
);
1824 create_process (proc
, (char **) new_argv
, current_dir
);
1826 return unbind_to (count
, proc
);
1829 /* This function is the unwind_protect form for Fstart_process. If
1830 PROC doesn't have its pid set, then we know someone has signaled
1831 an error and the process wasn't started successfully, so we should
1832 remove it from the process list. */
1834 start_process_unwind (proc
)
1837 if (!PROCESSP (proc
))
1840 /* Was PROC started successfully? */
1841 if (XPROCESS (proc
)->pid
<= 0)
1842 remove_process (proc
);
1848 create_process_1 (timer
)
1849 struct atimer
*timer
;
1851 /* Nothing to do. */
1855 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1858 /* Mimic blocking of signals on system V, which doesn't really have it. */
1860 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1861 int sigchld_deferred
;
1864 create_process_sigchld ()
1866 signal (SIGCHLD
, create_process_sigchld
);
1868 sigchld_deferred
= 1;
1874 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1876 create_process (process
, new_argv
, current_dir
)
1877 Lisp_Object process
;
1879 Lisp_Object current_dir
;
1881 int inchannel
, outchannel
;
1884 #ifdef POSIX_SIGNALS
1887 struct sigaction sigint_action
;
1888 struct sigaction sigquit_action
;
1890 struct sigaction sighup_action
;
1892 #else /* !POSIX_SIGNALS */
1895 SIGTYPE (*sigchld
)();
1898 #endif /* !POSIX_SIGNALS */
1899 /* Use volatile to protect variables from being clobbered by longjmp. */
1900 volatile int forkin
, forkout
;
1901 volatile int pty_flag
= 0;
1903 extern char **environ
;
1906 inchannel
= outchannel
= -1;
1909 if (!NILP (Vprocess_connection_type
))
1910 outchannel
= inchannel
= allocate_pty ();
1914 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1915 /* On most USG systems it does not work to open the pty's tty here,
1916 then close it and reopen it in the child. */
1918 /* Don't let this terminal become our controlling terminal
1919 (in case we don't have one). */
1920 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1922 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1925 report_file_error ("Opening pty", Qnil
);
1926 #if defined (DONT_REOPEN_PTY)
1927 /* In the case that vfork is defined as fork, the parent process
1928 (Emacs) may send some data before the child process completes
1929 tty options setup. So we setup tty before forking. */
1930 child_setup_tty (forkout
);
1931 #endif /* DONT_REOPEN_PTY */
1933 forkin
= forkout
= -1;
1934 #endif /* not USG, or USG_SUBTTY_WORKS */
1938 #endif /* HAVE_PTYS */
1943 report_file_error ("Creating pipe", Qnil
);
1949 emacs_close (inchannel
);
1950 emacs_close (forkout
);
1951 report_file_error ("Creating pipe", Qnil
);
1958 /* Replaced by close_process_descs */
1959 set_exclusive_use (inchannel
);
1960 set_exclusive_use (outchannel
);
1964 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1965 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1968 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1969 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1973 /* Record this as an active process, with its channels.
1974 As a result, child_setup will close Emacs's side of the pipes. */
1975 chan_process
[inchannel
] = process
;
1976 XPROCESS (process
)->infd
= inchannel
;
1977 XPROCESS (process
)->outfd
= outchannel
;
1979 /* Previously we recorded the tty descriptor used in the subprocess.
1980 It was only used for getting the foreground tty process, so now
1981 we just reopen the device (see emacs_get_tty_pgrp) as this is
1982 more portable (see USG_SUBTTY_WORKS above). */
1984 XPROCESS (process
)->pty_flag
= pty_flag
;
1985 XPROCESS (process
)->status
= Qrun
;
1986 setup_process_coding_systems (process
);
1988 /* Delay interrupts until we have a chance to store
1989 the new fork's pid in its process structure */
1990 #ifdef POSIX_SIGNALS
1991 sigemptyset (&blocked
);
1993 sigaddset (&blocked
, SIGCHLD
);
1995 #ifdef HAVE_WORKING_VFORK
1996 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1997 this sets the parent's signal handlers as well as the child's.
1998 So delay all interrupts whose handlers the child might munge,
1999 and record the current handlers so they can be restored later. */
2000 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
2001 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
2003 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
2005 #endif /* HAVE_WORKING_VFORK */
2006 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
2007 #else /* !POSIX_SIGNALS */
2011 #else /* not BSD4_1 */
2012 #if defined (BSD_SYSTEM) || defined (HPUX)
2013 sigsetmask (sigmask (SIGCHLD
));
2014 #else /* ordinary USG */
2016 sigchld_deferred
= 0;
2017 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
2019 #endif /* ordinary USG */
2020 #endif /* not BSD4_1 */
2021 #endif /* SIGCHLD */
2022 #endif /* !POSIX_SIGNALS */
2024 FD_SET (inchannel
, &input_wait_mask
);
2025 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2026 if (inchannel
> max_process_desc
)
2027 max_process_desc
= inchannel
;
2029 /* Until we store the proper pid, enable sigchld_handler
2030 to recognize an unknown pid as standing for this process.
2031 It is very important not to let this `marker' value stay
2032 in the table after this function has returned; if it does
2033 it might cause call-process to hang and subsequent asynchronous
2034 processes to get their return values scrambled. */
2035 XPROCESS (process
)->pid
= -1;
2040 /* child_setup must clobber environ on systems with true vfork.
2041 Protect it from permanent change. */
2042 char **save_environ
= environ
;
2044 current_dir
= ENCODE_FILE (current_dir
);
2049 #endif /* not WINDOWSNT */
2051 int xforkin
= forkin
;
2052 int xforkout
= forkout
;
2054 #if 0 /* This was probably a mistake--it duplicates code later on,
2055 but fails to handle all the cases. */
2056 /* Make sure SIGCHLD is not blocked in the child. */
2057 sigsetmask (SIGEMPTYMASK
);
2060 /* Make the pty be the controlling terminal of the process. */
2062 /* First, disconnect its current controlling terminal. */
2064 /* We tried doing setsid only if pty_flag, but it caused
2065 process_set_signal to fail on SGI when using a pipe. */
2067 /* Make the pty's terminal the controlling terminal. */
2071 /* We ignore the return value
2072 because faith@cs.unc.edu says that is necessary on Linux. */
2073 ioctl (xforkin
, TIOCSCTTY
, 0);
2076 #else /* not HAVE_SETSID */
2078 /* It's very important to call setpgrp here and no time
2079 afterwards. Otherwise, we lose our controlling tty which
2080 is set when we open the pty. */
2083 #endif /* not HAVE_SETSID */
2084 #if defined (HAVE_TERMIOS) && defined (LDISC1)
2085 if (pty_flag
&& xforkin
>= 0)
2088 tcgetattr (xforkin
, &t
);
2090 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
2091 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
2094 #if defined (NTTYDISC) && defined (TIOCSETD)
2095 if (pty_flag
&& xforkin
>= 0)
2097 /* Use new line discipline. */
2098 int ldisc
= NTTYDISC
;
2099 ioctl (xforkin
, TIOCSETD
, &ldisc
);
2104 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2105 can do TIOCSPGRP only to the process's controlling tty. */
2108 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
2109 I can't test it since I don't have 4.3. */
2110 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
2111 ioctl (j
, TIOCNOTTY
, 0);
2114 /* In order to get a controlling terminal on some versions
2115 of BSD, it is necessary to put the process in pgrp 0
2116 before it opens the terminal. */
2124 #endif /* TIOCNOTTY */
2126 #if !defined (DONT_REOPEN_PTY)
2127 /*** There is a suggestion that this ought to be a
2128 conditional on TIOCSPGRP,
2129 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2130 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2131 that system does seem to need this code, even though
2132 both HAVE_SETSID and TIOCSCTTY are defined. */
2133 /* Now close the pty (if we had it open) and reopen it.
2134 This makes the pty the controlling terminal of the subprocess. */
2137 #ifdef SET_CHILD_PTY_PGRP
2138 int pgrp
= getpid ();
2141 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2144 emacs_close (xforkin
);
2145 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
2149 emacs_write (1, "Couldn't open the pty terminal ", 31);
2150 emacs_write (1, pty_name
, strlen (pty_name
));
2151 emacs_write (1, "\n", 1);
2155 #ifdef SET_CHILD_PTY_PGRP
2156 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
2157 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
2160 #endif /* not DONT_REOPEN_PTY */
2162 #ifdef SETUP_SLAVE_PTY
2167 #endif /* SETUP_SLAVE_PTY */
2169 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2170 Now reenable it in the child, so it will die when we want it to. */
2172 signal (SIGHUP
, SIG_DFL
);
2174 #endif /* HAVE_PTYS */
2176 signal (SIGINT
, SIG_DFL
);
2177 signal (SIGQUIT
, SIG_DFL
);
2179 /* Stop blocking signals in the child. */
2180 #ifdef POSIX_SIGNALS
2181 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2182 #else /* !POSIX_SIGNALS */
2186 #else /* not BSD4_1 */
2187 #if defined (BSD_SYSTEM) || defined (HPUX)
2188 sigsetmask (SIGEMPTYMASK
);
2189 #else /* ordinary USG */
2191 signal (SIGCHLD
, sigchld
);
2193 #endif /* ordinary USG */
2194 #endif /* not BSD4_1 */
2195 #endif /* SIGCHLD */
2196 #endif /* !POSIX_SIGNALS */
2198 #if !defined (DONT_REOPEN_PTY)
2200 child_setup_tty (xforkout
);
2201 #endif /* not DONT_REOPEN_PTY */
2203 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2204 new_argv
, 1, current_dir
);
2205 #else /* not WINDOWSNT */
2206 child_setup (xforkin
, xforkout
, xforkout
,
2207 new_argv
, 1, current_dir
);
2208 #endif /* not WINDOWSNT */
2210 environ
= save_environ
;
2215 /* This runs in the Emacs process. */
2219 emacs_close (forkin
);
2220 if (forkin
!= forkout
&& forkout
>= 0)
2221 emacs_close (forkout
);
2225 /* vfork succeeded. */
2226 XPROCESS (process
)->pid
= pid
;
2229 register_child (pid
, inchannel
);
2230 #endif /* WINDOWSNT */
2232 /* If the subfork execv fails, and it exits,
2233 this close hangs. I don't know why.
2234 So have an interrupt jar it loose. */
2236 struct atimer
*timer
;
2240 EMACS_SET_SECS_USECS (offset
, 1, 0);
2241 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2244 emacs_close (forkin
);
2246 cancel_atimer (timer
);
2250 if (forkin
!= forkout
&& forkout
>= 0)
2251 emacs_close (forkout
);
2255 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2258 XPROCESS (process
)->tty_name
= Qnil
;
2261 /* Restore the signal state whether vfork succeeded or not.
2262 (We will signal an error, below, if it failed.) */
2263 #ifdef POSIX_SIGNALS
2264 #ifdef HAVE_WORKING_VFORK
2265 /* Restore the parent's signal handlers. */
2266 sigaction (SIGINT
, &sigint_action
, 0);
2267 sigaction (SIGQUIT
, &sigquit_action
, 0);
2269 sigaction (SIGHUP
, &sighup_action
, 0);
2271 #endif /* HAVE_WORKING_VFORK */
2272 /* Stop blocking signals in the parent. */
2273 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2274 #else /* !POSIX_SIGNALS */
2278 #else /* not BSD4_1 */
2279 #if defined (BSD_SYSTEM) || defined (HPUX)
2280 sigsetmask (SIGEMPTYMASK
);
2281 #else /* ordinary USG */
2283 signal (SIGCHLD
, sigchld
);
2284 /* Now really handle any of these signals
2285 that came in during this function. */
2286 if (sigchld_deferred
)
2287 kill (getpid (), SIGCHLD
);
2289 #endif /* ordinary USG */
2290 #endif /* not BSD4_1 */
2291 #endif /* SIGCHLD */
2292 #endif /* !POSIX_SIGNALS */
2294 /* Now generate the error if vfork failed. */
2296 report_file_error ("Doing vfork", Qnil
);
2298 #endif /* not VMS */
2303 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2304 The address family of sa is not included in the result. */
2307 conv_sockaddr_to_lisp (sa
, len
)
2308 struct sockaddr
*sa
;
2311 Lisp_Object address
;
2314 register struct Lisp_Vector
*p
;
2316 switch (sa
->sa_family
)
2320 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2321 len
= sizeof (sin
->sin_addr
) + 1;
2322 address
= Fmake_vector (make_number (len
), Qnil
);
2323 p
= XVECTOR (address
);
2324 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2325 cp
= (unsigned char *)&sin
->sin_addr
;
2331 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2332 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2333 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2334 address
= Fmake_vector (make_number (len
), Qnil
);
2335 p
= XVECTOR (address
);
2336 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2337 for (i
= 0; i
< len
; i
++)
2338 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2342 #ifdef HAVE_LOCAL_SOCKETS
2345 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2346 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2347 if (sockun
->sun_path
[i
] == 0)
2349 return make_unibyte_string (sockun
->sun_path
, i
);
2353 len
-= sizeof (sa
->sa_family
);
2354 address
= Fcons (make_number (sa
->sa_family
),
2355 Fmake_vector (make_number (len
), Qnil
));
2356 p
= XVECTOR (XCDR (address
));
2357 cp
= (unsigned char *) sa
+ sizeof (sa
->sa_family
);
2363 p
->contents
[i
++] = make_number (*cp
++);
2369 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2372 get_lisp_to_sockaddr_size (address
, familyp
)
2373 Lisp_Object address
;
2376 register struct Lisp_Vector
*p
;
2378 if (VECTORP (address
))
2380 p
= XVECTOR (address
);
2384 return sizeof (struct sockaddr_in
);
2387 else if (p
->size
== 9)
2389 *familyp
= AF_INET6
;
2390 return sizeof (struct sockaddr_in6
);
2394 #ifdef HAVE_LOCAL_SOCKETS
2395 else if (STRINGP (address
))
2397 *familyp
= AF_LOCAL
;
2398 return sizeof (struct sockaddr_un
);
2401 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2403 struct sockaddr
*sa
;
2404 *familyp
= XINT (XCAR (address
));
2405 p
= XVECTOR (XCDR (address
));
2406 return p
->size
+ sizeof (sa
->sa_family
);
2411 /* Convert an address object (vector or string) to an internal sockaddr.
2413 The address format has been basically validated by
2414 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2415 it could have come from user data. So if FAMILY is not valid,
2416 we return after zeroing *SA. */
2419 conv_lisp_to_sockaddr (family
, address
, sa
, len
)
2421 Lisp_Object address
;
2422 struct sockaddr
*sa
;
2425 register struct Lisp_Vector
*p
;
2426 register unsigned char *cp
= NULL
;
2431 if (VECTORP (address
))
2433 p
= XVECTOR (address
);
2434 if (family
== AF_INET
)
2436 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2437 len
= sizeof (sin
->sin_addr
) + 1;
2438 i
= XINT (p
->contents
[--len
]);
2439 sin
->sin_port
= htons (i
);
2440 cp
= (unsigned char *)&sin
->sin_addr
;
2441 sa
->sa_family
= family
;
2444 else if (family
== AF_INET6
)
2446 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2447 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2448 len
= sizeof (sin6
->sin6_addr
) + 1;
2449 i
= XINT (p
->contents
[--len
]);
2450 sin6
->sin6_port
= htons (i
);
2451 for (i
= 0; i
< len
; i
++)
2452 if (INTEGERP (p
->contents
[i
]))
2454 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2457 sa
->sa_family
= family
;
2462 else if (STRINGP (address
))
2464 #ifdef HAVE_LOCAL_SOCKETS
2465 if (family
== AF_LOCAL
)
2467 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2468 cp
= SDATA (address
);
2469 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2470 sockun
->sun_path
[i
] = *cp
++;
2471 sa
->sa_family
= family
;
2478 p
= XVECTOR (XCDR (address
));
2479 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2482 for (i
= 0; i
< len
; i
++)
2483 if (INTEGERP (p
->contents
[i
]))
2484 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2487 #ifdef DATAGRAM_SOCKETS
2488 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2490 doc
: /* Get the current datagram address associated with PROCESS. */)
2492 Lisp_Object process
;
2496 CHECK_PROCESS (process
);
2498 if (!DATAGRAM_CONN_P (process
))
2501 channel
= XPROCESS (process
)->infd
;
2502 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2503 datagram_address
[channel
].len
);
2506 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2508 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2509 Returns nil upon error setting address, ADDRESS otherwise. */)
2511 Lisp_Object process
, address
;
2516 CHECK_PROCESS (process
);
2518 if (!DATAGRAM_CONN_P (process
))
2521 channel
= XPROCESS (process
)->infd
;
2523 len
= get_lisp_to_sockaddr_size (address
, &family
);
2524 if (datagram_address
[channel
].len
!= len
)
2526 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2532 static struct socket_options
{
2533 /* The name of this option. Should be lowercase version of option
2534 name without SO_ prefix. */
2536 /* Option level SOL_... */
2538 /* Option number SO_... */
2540 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2541 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2542 } socket_options
[] =
2544 #ifdef SO_BINDTODEVICE
2545 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2548 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2551 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2554 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2557 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2560 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2563 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2566 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2568 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2571 /* Set option OPT to value VAL on socket S.
2573 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2574 Signals an error if setting a known option fails.
2578 set_socket_option (s
, opt
, val
)
2580 Lisp_Object opt
, val
;
2583 struct socket_options
*sopt
;
2588 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2589 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2590 if (strcmp (name
, sopt
->name
) == 0)
2593 switch (sopt
->opttype
)
2598 optval
= NILP (val
) ? 0 : 1;
2599 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2600 &optval
, sizeof (optval
));
2608 optval
= XINT (val
);
2610 error ("Bad option value for %s", name
);
2611 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2612 &optval
, sizeof (optval
));
2616 #ifdef SO_BINDTODEVICE
2619 char devname
[IFNAMSIZ
+1];
2621 /* This is broken, at least in the Linux 2.4 kernel.
2622 To unbind, the arg must be a zero integer, not the empty string.
2623 This should work on all systems. KFS. 2003-09-23. */
2624 bzero (devname
, sizeof devname
);
2627 char *arg
= (char *) SDATA (val
);
2628 int len
= min (strlen (arg
), IFNAMSIZ
);
2629 bcopy (arg
, devname
, len
);
2631 else if (!NILP (val
))
2632 error ("Bad option value for %s", name
);
2633 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2642 struct linger linger
;
2645 linger
.l_linger
= 0;
2647 linger
.l_linger
= XINT (val
);
2649 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2650 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2651 &linger
, sizeof (linger
));
2661 report_file_error ("Cannot set network option",
2662 Fcons (opt
, Fcons (val
, Qnil
)));
2663 return (1 << sopt
->optbit
);
2667 DEFUN ("set-network-process-option",
2668 Fset_network_process_option
, Sset_network_process_option
,
2670 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2671 See `make-network-process' for a list of options and values.
2672 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2673 OPTION is not a supported option, return nil instead; otherwise return t. */)
2674 (process
, option
, value
, no_error
)
2675 Lisp_Object process
, option
, value
;
2676 Lisp_Object no_error
;
2679 struct Lisp_Process
*p
;
2681 CHECK_PROCESS (process
);
2682 p
= XPROCESS (process
);
2683 if (!NETCONN1_P (p
))
2684 error ("Process is not a network process");
2688 error ("Process is not running");
2690 if (set_socket_option (s
, option
, value
))
2692 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2696 if (NILP (no_error
))
2697 error ("Unknown or unsupported option");
2703 /* A version of request_sigio suitable for a record_unwind_protect. */
2707 unwind_request_sigio (dummy
)
2710 if (interrupt_input
)
2717 DEFUN ("serial-process-configure",
2718 Fserial_process_configure
,
2719 Sserial_process_configure
,
2721 doc
: /* Configure speed, bytesize, etc. of a serial process.
2723 Arguments are specified as keyword/argument pairs. Attributes that
2724 are not given are re-initialized from the process's current
2725 configuration (available via the function `process-contact') or set to
2726 reasonable default values. The following arguments are defined:
2732 -- Any of these arguments can be given to identify the process that is
2733 to be configured. If none of these arguments is given, the current
2734 buffer's process is used.
2736 :speed SPEED -- SPEED is the speed of the serial port in bits per
2737 second, also called baud rate. Any value can be given for SPEED, but
2738 most serial ports work only at a few defined values between 1200 and
2739 115200, with 9600 being the most common value. If SPEED is nil, the
2740 serial port is not configured any further, i.e., all other arguments
2741 are ignored. This may be useful for special serial ports such as
2742 Bluetooth-to-serial converters which can only be configured through AT
2743 commands. A value of nil for SPEED can be used only when passed
2744 through `make-serial-process' or `serial-term'.
2746 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2747 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2749 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2750 `odd' (use odd parity), or the symbol `even' (use even parity). If
2751 PARITY is not given, no parity is used.
2753 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2754 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2755 is not given or nil, 1 stopbit is used.
2757 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2758 flowcontrol to be used, which is either nil (don't use flowcontrol),
2759 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2760 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2761 flowcontrol is used.
2763 `serial-process-configure' is called by `make-serial-process' for the
2764 initial configuration of the serial port.
2768 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2770 \(serial-process-configure
2771 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2773 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2775 usage: (serial-process-configure &rest ARGS) */)
2780 struct Lisp_Process
*p
;
2781 Lisp_Object contact
= Qnil
;
2782 Lisp_Object proc
= Qnil
;
2783 struct gcpro gcpro1
;
2785 contact
= Flist (nargs
, args
);
2788 proc
= Fplist_get (contact
, QCprocess
);
2790 proc
= Fplist_get (contact
, QCname
);
2792 proc
= Fplist_get (contact
, QCbuffer
);
2794 proc
= Fplist_get (contact
, QCport
);
2795 proc
= get_process (proc
);
2796 p
= XPROCESS (proc
);
2797 if (!EQ (p
->type
, Qserial
))
2798 error ("Not a serial process");
2800 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2806 serial_configure (p
, contact
);
2811 #endif /* HAVE_SERIAL */
2814 /* Used by make-serial-process to recover from errors. */
2815 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2817 if (!PROCESSP (proc
))
2819 remove_process (proc
);
2822 #endif /* HAVE_SERIAL */
2825 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2827 doc
: /* Create and return a serial port process.
2829 In Emacs, serial port connections are represented by process objects,
2830 so input and output work as for subprocesses, and `delete-process'
2831 closes a serial port connection. However, a serial process has no
2832 process id, it cannot be signaled, and the status codes are different
2833 from normal processes.
2835 `make-serial-process' creates a process and a buffer, on which you
2836 probably want to use `process-send-string'. Try \\[serial-term] for
2837 an interactive terminal. See below for examples.
2839 Arguments are specified as keyword/argument pairs. The following
2840 arguments are defined:
2842 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2843 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2844 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2845 the backslashes in strings).
2847 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2848 which is called by `make-serial-process'.
2850 :name NAME -- NAME is the name of the process. If NAME is not given,
2851 the value of PORT is used.
2853 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2854 with the process. Process output goes at the end of that buffer,
2855 unless you specify an output stream or filter function to handle the
2856 output. If BUFFER is not given, the value of NAME is used.
2858 :coding CODING -- If CODING is a symbol, it specifies the coding
2859 system used for both reading and writing for this process. If CODING
2860 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2861 ENCODING is used for writing.
2863 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2864 the process is running. If BOOL is not given, query before exiting.
2866 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2867 In the stopped state, a serial process does not accept incoming data,
2868 but you can send outgoing data. The stopped state is cleared by
2869 `continue-process' and set by `stop-process'.
2871 :filter FILTER -- Install FILTER as the process filter.
2873 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2875 :plist PLIST -- Install PLIST as the initial plist of the process.
2882 -- These arguments are handled by `serial-process-configure', which is
2883 called by `make-serial-process'.
2885 The original argument list, possibly modified by later configuration,
2886 is available via the function `process-contact'.
2890 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2892 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2894 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2896 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2898 usage: (make-serial-process &rest ARGS) */)
2904 Lisp_Object proc
, contact
, port
;
2905 struct Lisp_Process
*p
;
2906 struct gcpro gcpro1
;
2907 Lisp_Object name
, buffer
;
2908 Lisp_Object tem
, val
;
2909 int specpdl_count
= -1;
2914 contact
= Flist (nargs
, args
);
2917 port
= Fplist_get (contact
, QCport
);
2919 error ("No port specified");
2920 CHECK_STRING (port
);
2922 if (NILP (Fplist_member (contact
, QCspeed
)))
2923 error (":speed not specified");
2924 if (!NILP (Fplist_get (contact
, QCspeed
)))
2925 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2927 name
= Fplist_get (contact
, QCname
);
2930 CHECK_STRING (name
);
2931 proc
= make_process (name
);
2932 specpdl_count
= SPECPDL_INDEX ();
2933 record_unwind_protect (make_serial_process_unwind
, proc
);
2934 p
= XPROCESS (proc
);
2936 fd
= serial_open ((char*) SDATA (port
));
2939 if (fd
> max_process_desc
)
2940 max_process_desc
= fd
;
2941 chan_process
[fd
] = proc
;
2943 buffer
= Fplist_get (contact
, QCbuffer
);
2946 buffer
= Fget_buffer_create (buffer
);
2949 p
->childp
= contact
;
2950 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2952 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2953 p
->filter
= Fplist_get (contact
, QCfilter
);
2955 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2956 p
->kill_without_query
= 1;
2957 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2961 if (!EQ (p
->command
, Qt
))
2963 FD_SET (fd
, &input_wait_mask
);
2964 FD_SET (fd
, &non_keyboard_wait_mask
);
2967 if (BUFFERP (buffer
))
2969 set_marker_both (p
->mark
, buffer
,
2970 BUF_ZV (XBUFFER (buffer
)),
2971 BUF_ZV_BYTE (XBUFFER (buffer
)));
2974 tem
= Fplist_member (contact
, QCcoding
);
2975 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2981 val
= XCAR (XCDR (tem
));
2985 else if (!NILP (Vcoding_system_for_read
))
2986 val
= Vcoding_system_for_read
;
2987 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2988 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2990 p
->decode_coding_system
= val
;
2995 val
= XCAR (XCDR (tem
));
2999 else if (!NILP (Vcoding_system_for_write
))
3000 val
= Vcoding_system_for_write
;
3001 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3002 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3004 p
->encode_coding_system
= val
;
3006 setup_process_coding_systems (proc
);
3007 p
->decoding_buf
= make_uninit_string (0);
3008 p
->decoding_carryover
= 0;
3009 p
->encoding_buf
= make_uninit_string (0);
3010 p
->inherit_coding_system_flag
3011 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3013 Fserial_process_configure(nargs
, args
);
3015 specpdl_ptr
= specpdl
+ specpdl_count
;
3020 #endif /* HAVE_SERIAL */
3022 /* Create a network stream/datagram client/server process. Treated
3023 exactly like a normal process when reading and writing. Primary
3024 differences are in status display and process deletion. A network
3025 connection has no PID; you cannot signal it. All you can do is
3026 stop/continue it and deactivate/close it via delete-process */
3028 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
3030 doc
: /* Create and return a network server or client process.
3032 In Emacs, network connections are represented by process objects, so
3033 input and output work as for subprocesses and `delete-process' closes
3034 a network connection. However, a network process has no process id,
3035 it cannot be signaled, and the status codes are different from normal
3038 Arguments are specified as keyword/argument pairs. The following
3039 arguments are defined:
3041 :name NAME -- NAME is name for process. It is modified if necessary
3044 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
3045 with the process. Process output goes at end of that buffer, unless
3046 you specify an output stream or filter function to handle the output.
3047 BUFFER may be also nil, meaning that this process is not associated
3050 :host HOST -- HOST is name of the host to connect to, or its IP
3051 address. The symbol `local' specifies the local host. If specified
3052 for a server process, it must be a valid name or address for the local
3053 host, and only clients connecting to that address will be accepted.
3055 :service SERVICE -- SERVICE is name of the service desired, or an
3056 integer specifying a port number to connect to. If SERVICE is t,
3057 a random port number is selected for the server. (If Emacs was
3058 compiled with getaddrinfo, a port number can also be specified as a
3059 string, e.g. "80", as well as an integer. This is not portable.)
3061 :type TYPE -- TYPE is the type of connection. The default (nil) is a
3062 stream type connection, `datagram' creates a datagram type connection.
3064 :family FAMILY -- FAMILY is the address (and protocol) family for the
3065 service specified by HOST and SERVICE. The default (nil) is to use
3066 whatever address family (IPv4 or IPv6) that is defined for the host
3067 and port number specified by HOST and SERVICE. Other address families
3069 local -- for a local (i.e. UNIX) address specified by SERVICE.
3070 ipv4 -- use IPv4 address family only.
3071 ipv6 -- use IPv6 address family only.
3073 :local ADDRESS -- ADDRESS is the local address used for the connection.
3074 This parameter is ignored when opening a client process. When specified
3075 for a server process, the FAMILY, HOST and SERVICE args are ignored.
3077 :remote ADDRESS -- ADDRESS is the remote partner's address for the
3078 connection. This parameter is ignored when opening a stream server
3079 process. For a datagram server process, it specifies the initial
3080 setting of the remote datagram address. When specified for a client
3081 process, the FAMILY, HOST, and SERVICE args are ignored.
3083 The format of ADDRESS depends on the address family:
3084 - An IPv4 address is represented as an vector of integers [A B C D P]
3085 corresponding to numeric IP address A.B.C.D and port number P.
3086 - A local address is represented as a string with the address in the
3087 local address space.
3088 - An "unsupported family" address is represented by a cons (F . AV)
3089 where F is the family number and AV is a vector containing the socket
3090 address data with one element per address data byte. Do not rely on
3091 this format in portable code, as it may depend on implementation
3092 defined constants, data sizes, and data structure alignment.
3094 :coding CODING -- If CODING is a symbol, it specifies the coding
3095 system used for both reading and writing for this process. If CODING
3096 is a cons (DECODING . ENCODING), DECODING is used for reading, and
3097 ENCODING is used for writing.
3099 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
3100 return without waiting for the connection to complete; instead, the
3101 sentinel function will be called with second arg matching "open" (if
3102 successful) or "failed" when the connect completes. Default is to use
3103 a blocking connect (i.e. wait) for stream type connections.
3105 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
3106 running when Emacs is exited.
3108 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3109 In the stopped state, a server process does not accept new
3110 connections, and a client process does not handle incoming traffic.
3111 The stopped state is cleared by `continue-process' and set by
3114 :filter FILTER -- Install FILTER as the process filter.
3116 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3117 process filter are multibyte, otherwise they are unibyte.
3118 If this keyword is not specified, the strings are multibyte if
3119 `default-enable-multibyte-characters' is non-nil.
3121 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3123 :log LOG -- Install LOG as the server process log function. This
3124 function is called when the server accepts a network connection from a
3125 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3126 is the server process, CLIENT is the new process for the connection,
3127 and MESSAGE is a string.
3129 :plist PLIST -- Install PLIST as the new process' initial plist.
3131 :server QLEN -- if QLEN is non-nil, create a server process for the
3132 specified FAMILY, SERVICE, and connection type (stream or datagram).
3133 If QLEN is an integer, it is used as the max. length of the server's
3134 pending connection queue (also known as the backlog); the default
3135 queue length is 5. Default is to create a client process.
3137 The following network options can be specified for this connection:
3139 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
3140 :dontroute BOOL -- Only send to directly connected hosts.
3141 :keepalive BOOL -- Send keep-alive messages on network stream.
3142 :linger BOOL or TIMEOUT -- Send queued messages before closing.
3143 :oobinline BOOL -- Place out-of-band data in receive data stream.
3144 :priority INT -- Set protocol defined priority for sent packets.
3145 :reuseaddr BOOL -- Allow reusing a recently used local address
3146 (this is allowed by default for a server process).
3147 :bindtodevice NAME -- bind to interface NAME. Using this may require
3148 special privileges on some systems.
3150 Consult the relevant system programmer's manual pages for more
3151 information on using these options.
3154 A server process will listen for and accept connections from clients.
3155 When a client connection is accepted, a new network process is created
3156 for the connection with the following parameters:
3158 - The client's process name is constructed by concatenating the server
3159 process' NAME and a client identification string.
3160 - If the FILTER argument is non-nil, the client process will not get a
3161 separate process buffer; otherwise, the client's process buffer is a newly
3162 created buffer named after the server process' BUFFER name or process
3163 NAME concatenated with the client identification string.
3164 - The connection type and the process filter and sentinel parameters are
3165 inherited from the server process' TYPE, FILTER and SENTINEL.
3166 - The client process' contact info is set according to the client's
3167 addressing information (typically an IP address and a port number).
3168 - The client process' plist is initialized from the server's plist.
3170 Notice that the FILTER and SENTINEL args are never used directly by
3171 the server process. Also, the BUFFER argument is not used directly by
3172 the server process, but via the optional :log function, accepted (and
3173 failed) connections may be logged in the server process' buffer.
3175 The original argument list, modified with the actual connection
3176 information, is available via the `process-contact' function.
3178 usage: (make-network-process &rest ARGS) */)
3184 Lisp_Object contact
;
3185 struct Lisp_Process
*p
;
3186 #ifdef HAVE_GETADDRINFO
3187 struct addrinfo ai
, *res
, *lres
;
3188 struct addrinfo hints
;
3189 char *portstring
, portbuf
[128];
3190 #else /* HAVE_GETADDRINFO */
3191 struct _emacs_addrinfo
3197 struct sockaddr
*ai_addr
;
3198 struct _emacs_addrinfo
*ai_next
;
3200 #endif /* HAVE_GETADDRINFO */
3201 struct sockaddr_in address_in
;
3202 #ifdef HAVE_LOCAL_SOCKETS
3203 struct sockaddr_un address_un
;
3208 int s
= -1, outch
, inch
;
3209 struct gcpro gcpro1
;
3210 int count
= SPECPDL_INDEX ();
3212 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3214 Lisp_Object name
, buffer
, host
, service
, address
;
3215 Lisp_Object filter
, sentinel
;
3216 int is_non_blocking_client
= 0;
3217 int is_server
= 0, backlog
= 5;
3224 /* Save arguments for process-contact and clone-process. */
3225 contact
= Flist (nargs
, args
);
3229 /* Ensure socket support is loaded if available. */
3230 init_winsock (TRUE
);
3233 /* :type TYPE (nil: stream, datagram */
3234 tem
= Fplist_get (contact
, QCtype
);
3236 socktype
= SOCK_STREAM
;
3237 #ifdef DATAGRAM_SOCKETS
3238 else if (EQ (tem
, Qdatagram
))
3239 socktype
= SOCK_DGRAM
;
3242 error ("Unsupported connection type");
3245 tem
= Fplist_get (contact
, QCserver
);
3248 /* Don't support network sockets when non-blocking mode is
3249 not available, since a blocked Emacs is not useful. */
3250 #if defined(TERM) || (!defined(O_NONBLOCK) && !defined(O_NDELAY))
3251 error ("Network servers not supported");
3255 backlog
= XINT (tem
);
3259 /* Make QCaddress an alias for :local (server) or :remote (client). */
3260 QCaddress
= is_server
? QClocal
: QCremote
;
3263 if (!is_server
&& socktype
== SOCK_STREAM
3264 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3266 #ifndef NON_BLOCKING_CONNECT
3267 error ("Non-blocking connect not supported");
3269 is_non_blocking_client
= 1;
3273 name
= Fplist_get (contact
, QCname
);
3274 buffer
= Fplist_get (contact
, QCbuffer
);
3275 filter
= Fplist_get (contact
, QCfilter
);
3276 sentinel
= Fplist_get (contact
, QCsentinel
);
3278 CHECK_STRING (name
);
3281 /* Let's handle TERM before things get complicated ... */
3282 host
= Fplist_get (contact
, QChost
);
3283 CHECK_STRING (host
);
3285 service
= Fplist_get (contact
, QCservice
);
3286 if (INTEGERP (service
))
3287 port
= htons ((unsigned short) XINT (service
));
3290 struct servent
*svc_info
;
3291 CHECK_STRING (service
);
3292 svc_info
= getservbyname (SDATA (service
), "tcp");
3294 error ("Unknown service: %s", SDATA (service
));
3295 port
= svc_info
->s_port
;
3298 s
= connect_server (0);
3300 report_file_error ("error creating socket", Fcons (name
, Qnil
));
3301 send_command (s
, C_PORT
, 0, "%s:%d", SDATA (host
), ntohs (port
));
3302 send_command (s
, C_DUMB
, 1, 0);
3304 #else /* not TERM */
3306 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3307 ai
.ai_socktype
= socktype
;
3312 /* :local ADDRESS or :remote ADDRESS */
3313 address
= Fplist_get (contact
, QCaddress
);
3314 if (!NILP (address
))
3316 host
= service
= Qnil
;
3318 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3319 error ("Malformed :address");
3320 ai
.ai_family
= family
;
3321 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3322 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3326 /* :family FAMILY -- nil (for Inet), local, or integer. */
3327 tem
= Fplist_get (contact
, QCfamily
);
3330 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3336 #ifdef HAVE_LOCAL_SOCKETS
3337 else if (EQ (tem
, Qlocal
))
3341 else if (EQ (tem
, Qipv6
))
3344 else if (EQ (tem
, Qipv4
))
3346 else if (INTEGERP (tem
))
3347 family
= XINT (tem
);
3349 error ("Unknown address family");
3351 ai
.ai_family
= family
;
3353 /* :service SERVICE -- string, integer (port number), or t (random port). */
3354 service
= Fplist_get (contact
, QCservice
);
3356 #ifdef HAVE_LOCAL_SOCKETS
3357 if (family
== AF_LOCAL
)
3359 /* Host is not used. */
3361 CHECK_STRING (service
);
3362 bzero (&address_un
, sizeof address_un
);
3363 address_un
.sun_family
= AF_LOCAL
;
3364 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3365 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3366 ai
.ai_addrlen
= sizeof address_un
;
3371 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3372 host
= Fplist_get (contact
, QChost
);
3375 if (EQ (host
, Qlocal
))
3376 host
= build_string ("localhost");
3377 CHECK_STRING (host
);
3380 /* Slow down polling to every ten seconds.
3381 Some kernels have a bug which causes retrying connect to fail
3382 after a connect. Polling can interfere with gethostbyname too. */
3383 #ifdef POLL_FOR_INPUT
3384 if (socktype
== SOCK_STREAM
)
3386 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3387 bind_polling_period (10);
3391 #ifdef HAVE_GETADDRINFO
3392 /* If we have a host, use getaddrinfo to resolve both host and service.
3393 Otherwise, use getservbyname to lookup the service. */
3397 /* SERVICE can either be a string or int.
3398 Convert to a C string for later use by getaddrinfo. */
3399 if (EQ (service
, Qt
))
3401 else if (INTEGERP (service
))
3403 sprintf (portbuf
, "%ld", (long) XINT (service
));
3404 portstring
= portbuf
;
3408 CHECK_STRING (service
);
3409 portstring
= SDATA (service
);
3414 memset (&hints
, 0, sizeof (hints
));
3416 hints
.ai_family
= family
;
3417 hints
.ai_socktype
= socktype
;
3418 hints
.ai_protocol
= 0;
3420 #ifdef HAVE_RES_INIT
3424 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3426 #ifdef HAVE_GAI_STRERROR
3427 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror(ret
));
3429 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3435 #endif /* HAVE_GETADDRINFO */
3437 /* We end up here if getaddrinfo is not defined, or in case no hostname
3438 has been specified (e.g. for a local server process). */
3440 if (EQ (service
, Qt
))
3442 else if (INTEGERP (service
))
3443 port
= htons ((unsigned short) XINT (service
));
3446 struct servent
*svc_info
;
3447 CHECK_STRING (service
);
3448 svc_info
= getservbyname (SDATA (service
),
3449 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3451 error ("Unknown service: %s", SDATA (service
));
3452 port
= svc_info
->s_port
;
3455 bzero (&address_in
, sizeof address_in
);
3456 address_in
.sin_family
= family
;
3457 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3458 address_in
.sin_port
= port
;
3460 #ifndef HAVE_GETADDRINFO
3463 struct hostent
*host_info_ptr
;
3465 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3466 as it may `hang' Emacs for a very long time. */
3470 #ifdef HAVE_RES_INIT
3474 host_info_ptr
= gethostbyname (SDATA (host
));
3479 bcopy (host_info_ptr
->h_addr
, (char *) &address_in
.sin_addr
,
3480 host_info_ptr
->h_length
);
3481 family
= host_info_ptr
->h_addrtype
;
3482 address_in
.sin_family
= family
;
3485 /* Attempt to interpret host as numeric inet address */
3487 unsigned long numeric_addr
;
3488 numeric_addr
= inet_addr ((char *) SDATA (host
));
3489 if (numeric_addr
== -1)
3490 error ("Unknown host \"%s\"", SDATA (host
));
3492 bcopy ((char *)&numeric_addr
, (char *) &address_in
.sin_addr
,
3493 sizeof (address_in
.sin_addr
));
3497 #endif /* not HAVE_GETADDRINFO */
3499 ai
.ai_family
= family
;
3500 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3501 ai
.ai_addrlen
= sizeof address_in
;
3506 /* Previously this was compiled unconditionally, but that seems
3507 unnecessary on modern systems, and `unrequest_sigio' was a noop
3508 under X anyway. --lorentey */
3509 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
3510 when connect is interrupted. So let's not let it get interrupted.
3511 Note we do not turn off polling, because polling is only used
3512 when not interrupt_input, and thus not normally used on the systems
3513 which have this bug. On systems which use polling, there's no way
3514 to quit if polling is turned off. */
3516 && !is_server
&& socktype
== SOCK_STREAM
)
3518 /* Comment from KFS: The original open-network-stream code
3519 didn't unwind protect this, but it seems like the proper
3520 thing to do. In any case, I don't see how it could harm to
3521 do this -- and it makes cleanup (using unbind_to) easier. */
3522 record_unwind_protect (unwind_request_sigio
, Qnil
);
3527 /* Do this in case we never enter the for-loop below. */
3528 count1
= SPECPDL_INDEX ();
3531 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3537 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3544 #ifdef DATAGRAM_SOCKETS
3545 if (!is_server
&& socktype
== SOCK_DGRAM
)
3547 #endif /* DATAGRAM_SOCKETS */
3549 #ifdef NON_BLOCKING_CONNECT
3550 if (is_non_blocking_client
)
3553 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3555 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3567 /* Make us close S if quit. */
3568 record_unwind_protect (close_file_unwind
, make_number (s
));
3570 /* Parse network options in the arg list.
3571 We simply ignore anything which isn't a known option (including other keywords).
3572 An error is signalled if setting a known option fails. */
3573 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3574 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3578 /* Configure as a server socket. */
3580 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3581 explicit :reuseaddr key to override this. */
3582 #ifdef HAVE_LOCAL_SOCKETS
3583 if (family
!= AF_LOCAL
)
3585 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3588 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3589 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3592 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3593 report_file_error ("Cannot bind server socket", Qnil
);
3595 #ifdef HAVE_GETSOCKNAME
3596 if (EQ (service
, Qt
))
3598 struct sockaddr_in sa1
;
3599 int len1
= sizeof (sa1
);
3600 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3602 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3603 service
= make_number (ntohs (sa1
.sin_port
));
3604 contact
= Fplist_put (contact
, QCservice
, service
);
3609 if (socktype
== SOCK_STREAM
&& listen (s
, backlog
))
3610 report_file_error ("Cannot listen on server socket", Qnil
);
3618 /* This turns off all alarm-based interrupts; the
3619 bind_polling_period call above doesn't always turn all the
3620 short-interval ones off, especially if interrupt_input is
3623 It'd be nice to be able to control the connect timeout
3624 though. Would non-blocking connect calls be portable?
3626 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3628 turn_on_atimers (0);
3630 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3633 turn_on_atimers (1);
3635 if (ret
== 0 || xerrno
== EISCONN
)
3637 /* The unwind-protect will be discarded afterwards.
3638 Likewise for immediate_quit. */
3642 #ifdef NON_BLOCKING_CONNECT
3644 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3648 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3656 /* Discard the unwind protect closing S. */
3657 specpdl_ptr
= specpdl
+ count1
;
3661 if (xerrno
== EINTR
)
3667 #ifdef DATAGRAM_SOCKETS
3668 if (socktype
== SOCK_DGRAM
)
3670 if (datagram_address
[s
].sa
)
3672 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3673 datagram_address
[s
].len
= lres
->ai_addrlen
;
3677 bzero (datagram_address
[s
].sa
, lres
->ai_addrlen
);
3678 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3681 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3682 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3683 conv_lisp_to_sockaddr (rfamily
, remote
,
3684 datagram_address
[s
].sa
, rlen
);
3688 bcopy (lres
->ai_addr
, datagram_address
[s
].sa
, lres
->ai_addrlen
);
3691 contact
= Fplist_put (contact
, QCaddress
,
3692 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3693 #ifdef HAVE_GETSOCKNAME
3696 struct sockaddr_in sa1
;
3697 int len1
= sizeof (sa1
);
3698 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3699 contact
= Fplist_put (contact
, QClocal
,
3700 conv_sockaddr_to_lisp (&sa1
, len1
));
3707 #ifdef HAVE_GETADDRINFO
3716 /* Discard the unwind protect for closing S, if any. */
3717 specpdl_ptr
= specpdl
+ count1
;
3719 /* Unwind bind_polling_period and request_sigio. */
3720 unbind_to (count
, Qnil
);
3724 /* If non-blocking got this far - and failed - assume non-blocking is
3725 not supported after all. This is probably a wrong assumption, but
3726 the normal blocking calls to open-network-stream handles this error
3728 if (is_non_blocking_client
)
3733 report_file_error ("make server process failed", contact
);
3735 report_file_error ("make client process failed", contact
);
3738 #endif /* not TERM */
3744 buffer
= Fget_buffer_create (buffer
);
3745 proc
= make_process (name
);
3747 chan_process
[inch
] = proc
;
3750 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3753 fcntl (inch
, F_SETFL
, O_NDELAY
);
3757 p
= XPROCESS (proc
);
3759 p
->childp
= contact
;
3760 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3764 p
->sentinel
= sentinel
;
3766 p
->log
= Fplist_get (contact
, QClog
);
3767 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3768 p
->kill_without_query
= 1;
3769 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3774 if (is_server
&& socktype
== SOCK_STREAM
)
3775 p
->status
= Qlisten
;
3777 /* Make the process marker point into the process buffer (if any). */
3778 if (BUFFERP (buffer
))
3779 set_marker_both (p
->mark
, buffer
,
3780 BUF_ZV (XBUFFER (buffer
)),
3781 BUF_ZV_BYTE (XBUFFER (buffer
)));
3783 #ifdef NON_BLOCKING_CONNECT
3784 if (is_non_blocking_client
)
3786 /* We may get here if connect did succeed immediately. However,
3787 in that case, we still need to signal this like a non-blocking
3789 p
->status
= Qconnect
;
3790 if (!FD_ISSET (inch
, &connect_wait_mask
))
3792 FD_SET (inch
, &connect_wait_mask
);
3793 num_pending_connects
++;
3798 /* A server may have a client filter setting of Qt, but it must
3799 still listen for incoming connects unless it is stopped. */
3800 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3801 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3803 FD_SET (inch
, &input_wait_mask
);
3804 FD_SET (inch
, &non_keyboard_wait_mask
);
3807 if (inch
> max_process_desc
)
3808 max_process_desc
= inch
;
3810 tem
= Fplist_member (contact
, QCcoding
);
3811 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3812 tem
= Qnil
; /* No error message (too late!). */
3815 /* Setup coding systems for communicating with the network stream. */
3816 struct gcpro gcpro1
;
3817 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3818 Lisp_Object coding_systems
= Qt
;
3819 Lisp_Object args
[5], val
;
3823 val
= XCAR (XCDR (tem
));
3827 else if (!NILP (Vcoding_system_for_read
))
3828 val
= Vcoding_system_for_read
;
3829 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3830 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3831 /* We dare not decode end-of-line format by setting VAL to
3832 Qraw_text, because the existing Emacs Lisp libraries
3833 assume that they receive bare code including a sequene of
3838 if (NILP (host
) || NILP (service
))
3839 coding_systems
= Qnil
;
3842 args
[0] = Qopen_network_stream
, args
[1] = name
,
3843 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3845 coding_systems
= Ffind_operation_coding_system (5, args
);
3848 if (CONSP (coding_systems
))
3849 val
= XCAR (coding_systems
);
3850 else if (CONSP (Vdefault_process_coding_system
))
3851 val
= XCAR (Vdefault_process_coding_system
);
3855 p
->decode_coding_system
= val
;
3859 val
= XCAR (XCDR (tem
));
3863 else if (!NILP (Vcoding_system_for_write
))
3864 val
= Vcoding_system_for_write
;
3865 else if (NILP (current_buffer
->enable_multibyte_characters
))
3869 if (EQ (coding_systems
, Qt
))
3871 if (NILP (host
) || NILP (service
))
3872 coding_systems
= Qnil
;
3875 args
[0] = Qopen_network_stream
, args
[1] = name
,
3876 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3878 coding_systems
= Ffind_operation_coding_system (5, args
);
3882 if (CONSP (coding_systems
))
3883 val
= XCDR (coding_systems
);
3884 else if (CONSP (Vdefault_process_coding_system
))
3885 val
= XCDR (Vdefault_process_coding_system
);
3889 p
->encode_coding_system
= val
;
3891 setup_process_coding_systems (proc
);
3893 p
->decoding_buf
= make_uninit_string (0);
3894 p
->decoding_carryover
= 0;
3895 p
->encoding_buf
= make_uninit_string (0);
3897 p
->inherit_coding_system_flag
3898 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3903 #endif /* HAVE_SOCKETS */
3906 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3909 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3910 doc
: /* Return an alist of all network interfaces and their network address.
3911 Each element is a cons, the car of which is a string containing the
3912 interface name, and the cdr is the network address in internal
3913 format; see the description of ADDRESS in `make-network-process'. */)
3916 struct ifconf ifconf
;
3917 struct ifreq
*ifreqs
= NULL
;
3922 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3928 buf_size
= ifaces
* sizeof(ifreqs
[0]);
3929 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3936 ifconf
.ifc_len
= buf_size
;
3937 ifconf
.ifc_req
= ifreqs
;
3938 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3944 if (ifconf
.ifc_len
== buf_size
)
3948 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3951 while (--ifaces
>= 0)
3953 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3954 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3955 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3957 bcopy (ifq
->ifr_name
, namebuf
, sizeof (ifq
->ifr_name
));
3958 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3959 res
= Fcons (Fcons (build_string (namebuf
),
3960 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3961 sizeof (struct sockaddr
))),
3967 #endif /* SIOCGIFCONF */
3969 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3976 static struct ifflag_def ifflag_table
[] = {
3980 #ifdef IFF_BROADCAST
3981 { IFF_BROADCAST
, "broadcast" },
3984 { IFF_DEBUG
, "debug" },
3987 { IFF_LOOPBACK
, "loopback" },
3989 #ifdef IFF_POINTOPOINT
3990 { IFF_POINTOPOINT
, "pointopoint" },
3993 { IFF_RUNNING
, "running" },
3996 { IFF_NOARP
, "noarp" },
3999 { IFF_PROMISC
, "promisc" },
4001 #ifdef IFF_NOTRAILERS
4002 { IFF_NOTRAILERS
, "notrailers" },
4005 { IFF_ALLMULTI
, "allmulti" },
4008 { IFF_MASTER
, "master" },
4011 { IFF_SLAVE
, "slave" },
4013 #ifdef IFF_MULTICAST
4014 { IFF_MULTICAST
, "multicast" },
4017 { IFF_PORTSEL
, "portsel" },
4019 #ifdef IFF_AUTOMEDIA
4020 { IFF_AUTOMEDIA
, "automedia" },
4023 { IFF_DYNAMIC
, "dynamic" },
4026 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
4029 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
4032 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
4035 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
4038 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
4043 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
4044 doc
: /* Return information about network interface named IFNAME.
4045 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
4046 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
4047 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
4048 FLAGS is the current flags of the interface. */)
4053 Lisp_Object res
= Qnil
;
4058 CHECK_STRING (ifname
);
4060 bzero (rq
.ifr_name
, sizeof rq
.ifr_name
);
4061 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
4063 s
= socket (AF_INET
, SOCK_STREAM
, 0);
4068 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
4069 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
4071 int flags
= rq
.ifr_flags
;
4072 struct ifflag_def
*fp
;
4076 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
4078 if (flags
& fp
->flag_bit
)
4080 elt
= Fcons (intern (fp
->flag_sym
), elt
);
4081 flags
-= fp
->flag_bit
;
4084 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
4086 if (flags
& (1 << fnum
))
4088 elt
= Fcons (make_number (fnum
), elt
);
4093 res
= Fcons (elt
, res
);
4096 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
4097 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
4099 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
4100 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
4104 for (n
= 0; n
< 6; n
++)
4105 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
4106 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
4109 res
= Fcons (elt
, res
);
4112 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
4113 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
4116 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
4117 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
4119 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4123 res
= Fcons (elt
, res
);
4126 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4127 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
4130 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
4133 res
= Fcons (elt
, res
);
4136 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4137 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
4140 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
4143 res
= Fcons (elt
, res
);
4147 return any
? res
: Qnil
;
4150 #endif /* HAVE_SOCKETS */
4152 /* Turn off input and output for process PROC. */
4155 deactivate_process (proc
)
4158 register int inchannel
, outchannel
;
4159 register struct Lisp_Process
*p
= XPROCESS (proc
);
4161 inchannel
= p
->infd
;
4162 outchannel
= p
->outfd
;
4164 #ifdef ADAPTIVE_READ_BUFFERING
4165 if (p
->read_output_delay
> 0)
4167 if (--process_output_delay_count
< 0)
4168 process_output_delay_count
= 0;
4169 p
->read_output_delay
= 0;
4170 p
->read_output_skip
= 0;
4176 /* Beware SIGCHLD hereabouts. */
4177 flush_pending_output (inchannel
);
4180 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
4181 sys$
dassgn (outchannel
);
4182 vs
= get_vms_process_pointer (p
->pid
);
4184 give_back_vms_process_stuff (vs
);
4187 emacs_close (inchannel
);
4188 if (outchannel
>= 0 && outchannel
!= inchannel
)
4189 emacs_close (outchannel
);
4194 #ifdef DATAGRAM_SOCKETS
4195 if (DATAGRAM_CHAN_P (inchannel
))
4197 xfree (datagram_address
[inchannel
].sa
);
4198 datagram_address
[inchannel
].sa
= 0;
4199 datagram_address
[inchannel
].len
= 0;
4202 chan_process
[inchannel
] = Qnil
;
4203 FD_CLR (inchannel
, &input_wait_mask
);
4204 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4205 #ifdef NON_BLOCKING_CONNECT
4206 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4208 FD_CLR (inchannel
, &connect_wait_mask
);
4209 if (--num_pending_connects
< 0)
4213 if (inchannel
== max_process_desc
)
4216 /* We just closed the highest-numbered process input descriptor,
4217 so recompute the highest-numbered one now. */
4218 max_process_desc
= 0;
4219 for (i
= 0; i
< MAXDESC
; i
++)
4220 if (!NILP (chan_process
[i
]))
4221 max_process_desc
= i
;
4226 /* Close all descriptors currently in use for communication
4227 with subprocess. This is used in a newly-forked subprocess
4228 to get rid of irrelevant descriptors. */
4231 close_process_descs ()
4235 for (i
= 0; i
< MAXDESC
; i
++)
4237 Lisp_Object process
;
4238 process
= chan_process
[i
];
4239 if (!NILP (process
))
4241 int in
= XPROCESS (process
)->infd
;
4242 int out
= XPROCESS (process
)->outfd
;
4245 if (out
>= 0 && in
!= out
)
4252 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4254 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4255 It is read into the process' buffers or given to their filter functions.
4256 Non-nil arg PROCESS means do not return until some output has been received
4259 Non-nil second arg SECONDS and third arg MILLISEC are number of
4260 seconds and milliseconds to wait; return after that much time whether
4261 or not there is input. If SECONDS is a floating point number,
4262 it specifies a fractional number of seconds to wait.
4263 The MILLISEC argument is obsolete and should be avoided.
4265 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4266 from PROCESS, suspending reading output from other processes.
4267 If JUST-THIS-ONE is an integer, don't run any timers either.
4268 Return non-nil if we received any output before the timeout expired. */)
4269 (process
, seconds
, millisec
, just_this_one
)
4270 register Lisp_Object process
, seconds
, millisec
, just_this_one
;
4272 int secs
, usecs
= 0;
4274 if (! NILP (process
))
4275 CHECK_PROCESS (process
);
4277 just_this_one
= Qnil
;
4279 if (!NILP (millisec
))
4280 { /* Obsolete calling convention using integers rather than floats. */
4281 CHECK_NUMBER (millisec
);
4283 seconds
= make_float (XINT (millisec
) / 1000.0);
4286 CHECK_NUMBER (seconds
);
4287 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4291 if (!NILP (seconds
))
4293 if (INTEGERP (seconds
))
4294 secs
= XINT (seconds
);
4295 else if (FLOATP (seconds
))
4297 double timeout
= XFLOAT_DATA (seconds
);
4298 secs
= (int) timeout
;
4299 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4302 wrong_type_argument (Qnumberp
, seconds
);
4304 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4305 secs
= -1, usecs
= 0;
4308 secs
= NILP (process
) ? -1 : 0;
4311 (wait_reading_process_output (secs
, usecs
, 0, 0,
4313 !NILP (process
) ? XPROCESS (process
) : NULL
,
4314 NILP (just_this_one
) ? 0 :
4315 !INTEGERP (just_this_one
) ? 1 : -1)
4319 /* Accept a connection for server process SERVER on CHANNEL. */
4321 static int connect_counter
= 0;
4324 server_accept_connection (server
, channel
)
4328 Lisp_Object proc
, caller
, name
, buffer
;
4329 Lisp_Object contact
, host
, service
;
4330 struct Lisp_Process
*ps
= XPROCESS (server
);
4331 struct Lisp_Process
*p
;
4335 struct sockaddr_in in
;
4337 struct sockaddr_in6 in6
;
4339 #ifdef HAVE_LOCAL_SOCKETS
4340 struct sockaddr_un un
;
4343 int len
= sizeof saddr
;
4345 s
= accept (channel
, &saddr
.sa
, &len
);
4354 if (code
== EWOULDBLOCK
)
4358 if (!NILP (ps
->log
))
4359 call3 (ps
->log
, server
, Qnil
,
4360 concat3 (build_string ("accept failed with code"),
4361 Fnumber_to_string (make_number (code
)),
4362 build_string ("\n")));
4368 /* Setup a new process to handle the connection. */
4370 /* Generate a unique identification of the caller, and build contact
4371 information for this process. */
4374 switch (saddr
.sa
.sa_family
)
4378 Lisp_Object args
[5];
4379 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4380 args
[0] = build_string ("%d.%d.%d.%d");
4381 args
[1] = make_number (*ip
++);
4382 args
[2] = make_number (*ip
++);
4383 args
[3] = make_number (*ip
++);
4384 args
[4] = make_number (*ip
++);
4385 host
= Fformat (5, args
);
4386 service
= make_number (ntohs (saddr
.in
.sin_port
));
4388 args
[0] = build_string (" <%s:%d>");
4391 caller
= Fformat (3, args
);
4398 Lisp_Object args
[9];
4399 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4401 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4402 for (i
= 0; i
< 8; i
++)
4403 args
[i
+1] = make_number (ntohs(ip6
[i
]));
4404 host
= Fformat (9, args
);
4405 service
= make_number (ntohs (saddr
.in
.sin_port
));
4407 args
[0] = build_string (" <[%s]:%d>");
4410 caller
= Fformat (3, args
);
4415 #ifdef HAVE_LOCAL_SOCKETS
4419 caller
= Fnumber_to_string (make_number (connect_counter
));
4420 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4424 /* Create a new buffer name for this process if it doesn't have a
4425 filter. The new buffer name is based on the buffer name or
4426 process name of the server process concatenated with the caller
4429 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4433 buffer
= ps
->buffer
;
4435 buffer
= Fbuffer_name (buffer
);
4440 buffer
= concat2 (buffer
, caller
);
4441 buffer
= Fget_buffer_create (buffer
);
4445 /* Generate a unique name for the new server process. Combine the
4446 server process name with the caller identification. */
4448 name
= concat2 (ps
->name
, caller
);
4449 proc
= make_process (name
);
4451 chan_process
[s
] = proc
;
4454 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4457 fcntl (s
, F_SETFL
, O_NDELAY
);
4461 p
= XPROCESS (proc
);
4463 /* Build new contact information for this setup. */
4464 contact
= Fcopy_sequence (ps
->childp
);
4465 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4466 contact
= Fplist_put (contact
, QChost
, host
);
4467 if (!NILP (service
))
4468 contact
= Fplist_put (contact
, QCservice
, service
);
4469 contact
= Fplist_put (contact
, QCremote
,
4470 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4471 #ifdef HAVE_GETSOCKNAME
4473 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4474 contact
= Fplist_put (contact
, QClocal
,
4475 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4478 p
->childp
= contact
;
4479 p
->plist
= Fcopy_sequence (ps
->plist
);
4483 p
->sentinel
= ps
->sentinel
;
4484 p
->filter
= ps
->filter
;
4491 /* Client processes for accepted connections are not stopped initially. */
4492 if (!EQ (p
->filter
, Qt
))
4494 FD_SET (s
, &input_wait_mask
);
4495 FD_SET (s
, &non_keyboard_wait_mask
);
4498 if (s
> max_process_desc
)
4499 max_process_desc
= s
;
4501 /* Setup coding system for new process based on server process.
4502 This seems to be the proper thing to do, as the coding system
4503 of the new process should reflect the settings at the time the
4504 server socket was opened; not the current settings. */
4506 p
->decode_coding_system
= ps
->decode_coding_system
;
4507 p
->encode_coding_system
= ps
->encode_coding_system
;
4508 setup_process_coding_systems (proc
);
4510 p
->decoding_buf
= make_uninit_string (0);
4511 p
->decoding_carryover
= 0;
4512 p
->encoding_buf
= make_uninit_string (0);
4514 p
->inherit_coding_system_flag
4515 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4517 if (!NILP (ps
->log
))
4518 call3 (ps
->log
, server
, proc
,
4519 concat3 (build_string ("accept from "),
4520 (STRINGP (host
) ? host
: build_string ("-")),
4521 build_string ("\n")));
4523 if (!NILP (p
->sentinel
))
4524 exec_sentinel (proc
,
4525 concat3 (build_string ("open from "),
4526 (STRINGP (host
) ? host
: build_string ("-")),
4527 build_string ("\n")));
4530 /* This variable is different from waiting_for_input in keyboard.c.
4531 It is used to communicate to a lisp process-filter/sentinel (via the
4532 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4533 for user-input when that process-filter was called.
4534 waiting_for_input cannot be used as that is by definition 0 when
4535 lisp code is being evalled.
4536 This is also used in record_asynch_buffer_change.
4537 For that purpose, this must be 0
4538 when not inside wait_reading_process_output. */
4539 static int waiting_for_user_input_p
;
4542 wait_reading_process_output_unwind (data
)
4545 waiting_for_user_input_p
= XINT (data
);
4549 /* This is here so breakpoints can be put on it. */
4551 wait_reading_process_output_1 ()
4555 /* Use a wrapper around select to work around a bug in gdb 5.3.
4556 Normally, the wrapper is optimzed away by inlining.
4558 If emacs is stopped inside select, the gdb backtrace doesn't
4559 show the function which called select, so it is practically
4560 impossible to step through wait_reading_process_output. */
4564 select_wrapper (n
, rfd
, wfd
, xfd
, tmo
)
4566 SELECT_TYPE
*rfd
, *wfd
, *xfd
;
4569 return select (n
, rfd
, wfd
, xfd
, tmo
);
4571 #define select select_wrapper
4574 /* Read and dispose of subprocess output while waiting for timeout to
4575 elapse and/or keyboard input to be available.
4578 timeout in seconds, or
4579 zero for no limit, or
4580 -1 means gobble data immediately available but don't wait for any.
4583 an additional duration to wait, measured in microseconds.
4584 If this is nonzero and time_limit is 0, then the timeout
4585 consists of MICROSECS only.
4587 READ_KBD is a lisp value:
4588 0 to ignore keyboard input, or
4589 1 to return when input is available, or
4590 -1 meaning caller will actually read the input, so don't throw to
4591 the quit handler, or
4593 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4594 output that arrives.
4596 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4597 (and gobble terminal input into the buffer if any arrives).
4599 If WAIT_PROC is specified, wait until something arrives from that
4600 process. The return value is true if we read some input from
4603 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4604 (suspending output from other processes). A negative value
4605 means don't run any timers either.
4607 If WAIT_PROC is specified, then the function returns true if we
4608 received input from that process before the timeout elapsed.
4609 Otherwise, return true if we received input from any process. */
4612 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
4613 wait_for_cell
, wait_proc
, just_wait_proc
)
4614 int time_limit
, microsecs
, read_kbd
, do_display
;
4615 Lisp_Object wait_for_cell
;
4616 struct Lisp_Process
*wait_proc
;
4619 register int channel
, nfds
;
4620 SELECT_TYPE Available
;
4621 #ifdef NON_BLOCKING_CONNECT
4622 SELECT_TYPE Connecting
;
4625 int check_delay
, no_avail
;
4628 EMACS_TIME timeout
, end_time
;
4629 int wait_channel
= -1;
4630 int got_some_input
= 0;
4631 int count
= SPECPDL_INDEX ();
4633 FD_ZERO (&Available
);
4634 #ifdef NON_BLOCKING_CONNECT
4635 FD_ZERO (&Connecting
);
4638 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4639 if (wait_proc
!= NULL
)
4640 wait_channel
= wait_proc
->infd
;
4642 record_unwind_protect (wait_reading_process_output_unwind
,
4643 make_number (waiting_for_user_input_p
));
4644 waiting_for_user_input_p
= read_kbd
;
4646 /* Since we may need to wait several times,
4647 compute the absolute time to return at. */
4648 if (time_limit
|| microsecs
)
4650 EMACS_GET_TIME (end_time
);
4651 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4652 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4654 #ifdef POLL_INTERRUPTED_SYS_CALL
4655 /* AlainF 5-Jul-1996
4656 HP-UX 10.10 seem to have problems with signals coming in
4657 Causes "poll: interrupted system call" messages when Emacs is run
4659 Turn off periodic alarms (in case they are in use),
4660 and then turn off any other atimers. */
4662 turn_on_atimers (0);
4663 #endif /* POLL_INTERRUPTED_SYS_CALL */
4667 int timeout_reduced_for_timers
= 0;
4669 /* If calling from keyboard input, do not quit
4670 since we want to return C-g as an input character.
4671 Otherwise, do pending quit if requested. */
4677 if (interrupt_input_pending
)
4678 handle_async_input ();
4679 if (pending_atimers
)
4680 do_pending_atimers ();
4684 /* Exit now if the cell we're waiting for became non-nil. */
4685 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4688 /* Compute time from now till when time limit is up */
4689 /* Exit if already run out */
4690 if (time_limit
== -1)
4692 /* -1 specified for timeout means
4693 gobble output available now
4694 but don't wait at all. */
4696 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4698 else if (time_limit
|| microsecs
)
4700 EMACS_GET_TIME (timeout
);
4701 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4702 if (EMACS_TIME_NEG_P (timeout
))
4707 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4710 /* Normally we run timers here.
4711 But not if wait_for_cell; in those cases,
4712 the wait is supposed to be short,
4713 and those callers cannot handle running arbitrary Lisp code here. */
4714 if (NILP (wait_for_cell
)
4715 && just_wait_proc
>= 0)
4717 EMACS_TIME timer_delay
;
4721 int old_timers_run
= timers_run
;
4722 struct buffer
*old_buffer
= current_buffer
;
4723 Lisp_Object old_window
= selected_window
;
4725 timer_delay
= timer_check (1);
4727 /* If a timer has run, this might have changed buffers
4728 an alike. Make read_key_sequence aware of that. */
4729 if (timers_run
!= old_timers_run
4730 && (old_buffer
!= current_buffer
4731 || !EQ (old_window
, selected_window
))
4732 && waiting_for_user_input_p
== -1)
4733 record_asynch_buffer_change ();
4735 if (timers_run
!= old_timers_run
&& do_display
)
4736 /* We must retry, since a timer may have requeued itself
4737 and that could alter the time_delay. */
4738 redisplay_preserve_echo_area (9);
4742 while (!detect_input_pending ());
4744 /* If there is unread keyboard input, also return. */
4746 && requeued_events_pending_p ())
4749 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4751 EMACS_TIME difference
;
4752 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4753 if (EMACS_TIME_NEG_P (difference
))
4755 timeout
= timer_delay
;
4756 timeout_reduced_for_timers
= 1;
4759 /* If time_limit is -1, we are not going to wait at all. */
4760 else if (time_limit
!= -1)
4762 /* This is so a breakpoint can be put here. */
4763 wait_reading_process_output_1 ();
4767 /* Cause C-g and alarm signals to take immediate action,
4768 and cause input available signals to zero out timeout.
4770 It is important that we do this before checking for process
4771 activity. If we get a SIGCHLD after the explicit checks for
4772 process activity, timeout is the only way we will know. */
4774 set_waiting_for_input (&timeout
);
4776 /* If status of something has changed, and no input is
4777 available, notify the user of the change right away. After
4778 this explicit check, we'll let the SIGCHLD handler zap
4779 timeout to get our attention. When Emacs is run
4780 interactively, only do this with a nonzero DO_DISPLAY
4781 argument, because status_notify triggers redisplay. */
4782 if (update_tick
!= process_tick
4783 && (do_display
|| noninteractive
))
4786 #ifdef NON_BLOCKING_CONNECT
4790 Atemp
= input_wait_mask
;
4792 /* On Mac OS X 10.0, the SELECT system call always says input is
4793 present (for reading) at stdin, even when none is. This
4794 causes the call to SELECT below to return 1 and
4795 status_notify not to be called. As a result output of
4796 subprocesses are incorrectly discarded.
4800 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4802 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4803 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4806 #ifdef NON_BLOCKING_CONNECT
4807 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4811 (SELECT_TYPE
*)0, &timeout
)
4814 /* It's okay for us to do this and then continue with
4815 the loop, since timeout has already been zeroed out. */
4816 clear_waiting_for_input ();
4817 status_notify (NULL
);
4821 /* Don't wait for output from a non-running process. Just
4822 read whatever data has already been received. */
4823 if (wait_proc
&& wait_proc
->raw_status_new
)
4824 update_status (wait_proc
);
4826 && ! EQ (wait_proc
->status
, Qrun
)
4827 && ! EQ (wait_proc
->status
, Qconnect
))
4829 int nread
, total_nread
= 0;
4831 clear_waiting_for_input ();
4832 XSETPROCESS (proc
, wait_proc
);
4834 /* Read data from the process, until we exhaust it. */
4835 while (wait_proc
->infd
>= 0)
4837 nread
= read_process_output (proc
, wait_proc
->infd
);
4844 total_nread
+= nread
;
4848 else if (nread
== -1 && EIO
== errno
)
4852 else if (nread
== -1 && EAGAIN
== errno
)
4856 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4860 if (total_nread
> 0 && do_display
)
4861 redisplay_preserve_echo_area (10);
4866 /* Wait till there is something to do */
4868 if (wait_proc
&& just_wait_proc
)
4870 if (wait_proc
->infd
< 0) /* Terminated */
4872 FD_SET (wait_proc
->infd
, &Available
);
4874 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4876 else if (!NILP (wait_for_cell
))
4878 Available
= non_process_wait_mask
;
4880 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4885 Available
= non_keyboard_wait_mask
;
4887 Available
= input_wait_mask
;
4888 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4889 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4892 /* If frame size has changed or the window is newly mapped,
4893 redisplay now, before we start to wait. There is a race
4894 condition here; if a SIGIO arrives between now and the select
4895 and indicates that a frame is trashed, the select may block
4896 displaying a trashed screen. */
4897 if (frame_garbaged
&& do_display
)
4899 clear_waiting_for_input ();
4900 redisplay_preserve_echo_area (11);
4902 set_waiting_for_input (&timeout
);
4906 if (read_kbd
&& detect_input_pending ())
4913 #ifdef NON_BLOCKING_CONNECT
4915 Connecting
= connect_wait_mask
;
4918 #ifdef ADAPTIVE_READ_BUFFERING
4919 /* Set the timeout for adaptive read buffering if any
4920 process has non-zero read_output_skip and non-zero
4921 read_output_delay, and we are not reading output for a
4922 specific wait_channel. It is not executed if
4923 Vprocess_adaptive_read_buffering is nil. */
4924 if (process_output_skip
&& check_delay
> 0)
4926 int usecs
= EMACS_USECS (timeout
);
4927 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4928 usecs
= READ_OUTPUT_DELAY_MAX
;
4929 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4931 proc
= chan_process
[channel
];
4934 /* Find minimum non-zero read_output_delay among the
4935 processes with non-zero read_output_skip. */
4936 if (XPROCESS (proc
)->read_output_delay
> 0)
4939 if (!XPROCESS (proc
)->read_output_skip
)
4941 FD_CLR (channel
, &Available
);
4942 XPROCESS (proc
)->read_output_skip
= 0;
4943 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4944 usecs
= XPROCESS (proc
)->read_output_delay
;
4947 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4948 process_output_skip
= 0;
4952 nfds
= select (max (max (max_process_desc
, max_keyboard_desc
),
4955 #ifdef NON_BLOCKING_CONNECT
4956 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4960 (SELECT_TYPE
*)0, &timeout
);
4965 /* Make C-g and alarm signals set flags again */
4966 clear_waiting_for_input ();
4968 /* If we woke up due to SIGWINCH, actually change size now. */
4969 do_pending_window_change (0);
4971 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4972 /* We wanted the full specified time, so return now. */
4976 if (xerrno
== EINTR
)
4979 /* Ultrix select seems to return ENOMEM when it is
4980 interrupted. Treat it just like EINTR. Bleah. Note
4981 that we want to test for the "ultrix" CPP symbol, not
4982 "__ultrix__"; the latter is only defined under GCC, but
4983 not by DEC's bundled CC. -JimB */
4984 else if (xerrno
== ENOMEM
)
4987 else if (xerrno
== EBADF
)
4990 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4991 the child's closure of the pts gives the parent a SIGHUP, and
4992 the ptc file descriptor is automatically closed,
4993 yielding EBADF here or at select() call above.
4994 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4995 in m/ibmrt-aix.h), and here we just ignore the select error.
4996 Cleanup occurs c/o status_notify after SIGCLD. */
4997 no_avail
= 1; /* Cannot depend on values returned */
5003 error ("select error: %s", emacs_strerror (xerrno
));
5008 FD_ZERO (&Available
);
5009 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
5012 #if defined(sun) && !defined(USG5_4)
5013 if (nfds
> 0 && keyboard_bit_set (&Available
)
5015 /* System sometimes fails to deliver SIGIO.
5017 David J. Mackenzie says that Emacs doesn't compile under
5018 Solaris if this code is enabled, thus the USG5_4 in the CPP
5019 conditional. "I haven't noticed any ill effects so far.
5020 If you find a Solaris expert somewhere, they might know
5022 kill (getpid (), SIGIO
);
5025 #if 0 /* When polling is used, interrupt_input is 0,
5026 so get_input_pending should read the input.
5027 So this should not be needed. */
5028 /* If we are using polling for input,
5029 and we see input available, make it get read now.
5030 Otherwise it might not actually get read for a second.
5031 And on hpux, since we turn off polling in wait_reading_process_output,
5032 it might never get read at all if we don't spend much time
5033 outside of wait_reading_process_output. */
5034 if (read_kbd
&& interrupt_input
5035 && keyboard_bit_set (&Available
)
5036 && input_polling_used ())
5037 kill (getpid (), SIGALRM
);
5040 /* Check for keyboard input */
5041 /* If there is any, return immediately
5042 to give it higher priority than subprocesses */
5046 int old_timers_run
= timers_run
;
5047 struct buffer
*old_buffer
= current_buffer
;
5048 Lisp_Object old_window
= selected_window
;
5051 if (detect_input_pending_run_timers (do_display
))
5053 swallow_events (do_display
);
5054 if (detect_input_pending_run_timers (do_display
))
5058 /* If a timer has run, this might have changed buffers
5059 an alike. Make read_key_sequence aware of that. */
5060 if (timers_run
!= old_timers_run
5061 && waiting_for_user_input_p
== -1
5062 && (old_buffer
!= current_buffer
5063 || !EQ (old_window
, selected_window
)))
5064 record_asynch_buffer_change ();
5070 /* If there is unread keyboard input, also return. */
5072 && requeued_events_pending_p ())
5075 /* If we are not checking for keyboard input now,
5076 do process events (but don't run any timers).
5077 This is so that X events will be processed.
5078 Otherwise they may have to wait until polling takes place.
5079 That would causes delays in pasting selections, for example.
5081 (We used to do this only if wait_for_cell.) */
5082 if (read_kbd
== 0 && detect_input_pending ())
5084 swallow_events (do_display
);
5085 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
5086 if (detect_input_pending ())
5091 /* Exit now if the cell we're waiting for became non-nil. */
5092 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
5096 /* If we think we have keyboard input waiting, but didn't get SIGIO,
5097 go read it. This can happen with X on BSD after logging out.
5098 In that case, there really is no input and no SIGIO,
5099 but select says there is input. */
5101 if (read_kbd
&& interrupt_input
5102 && keyboard_bit_set (&Available
) && ! noninteractive
)
5103 kill (getpid (), SIGIO
);
5107 got_some_input
|= nfds
> 0;
5109 /* If checking input just got us a size-change event from X,
5110 obey it now if we should. */
5111 if (read_kbd
|| ! NILP (wait_for_cell
))
5112 do_pending_window_change (0);
5114 /* Check for data from a process. */
5115 if (no_avail
|| nfds
== 0)
5118 /* Really FIRST_PROC_DESC should be 0 on Unix,
5119 but this is safer in the short run. */
5120 for (channel
= 0; channel
<= max_process_desc
; channel
++)
5122 if (FD_ISSET (channel
, &Available
)
5123 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
5127 /* If waiting for this channel, arrange to return as
5128 soon as no more input to be processed. No more
5130 if (wait_channel
== channel
)
5136 proc
= chan_process
[channel
];
5140 /* If this is a server stream socket, accept connection. */
5141 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
5143 server_accept_connection (proc
, channel
);
5147 /* Read data from the process, starting with our
5148 buffered-ahead character if we have one. */
5150 nread
= read_process_output (proc
, channel
);
5153 /* Since read_process_output can run a filter,
5154 which can call accept-process-output,
5155 don't try to read from any other processes
5156 before doing the select again. */
5157 FD_ZERO (&Available
);
5160 redisplay_preserve_echo_area (12);
5163 else if (nread
== -1 && errno
== EWOULDBLOCK
)
5166 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
5167 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
5169 else if (nread
== -1 && errno
== EAGAIN
)
5173 else if (nread
== -1 && errno
== EAGAIN
)
5175 /* Note that we cannot distinguish between no input
5176 available now and a closed pipe.
5177 With luck, a closed pipe will be accompanied by
5178 subprocess termination and SIGCHLD. */
5179 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5181 #endif /* O_NDELAY */
5182 #endif /* O_NONBLOCK */
5184 /* On some OSs with ptys, when the process on one end of
5185 a pty exits, the other end gets an error reading with
5186 errno = EIO instead of getting an EOF (0 bytes read).
5187 Therefore, if we get an error reading and errno =
5188 EIO, just continue, because the child process has
5189 exited and should clean itself up soon (e.g. when we
5192 However, it has been known to happen that the SIGCHLD
5193 got lost. So raise the signal again just in case.
5195 else if (nread
== -1 && errno
== EIO
)
5197 /* Clear the descriptor now, so we only raise the signal once. */
5198 FD_CLR (channel
, &input_wait_mask
);
5199 FD_CLR (channel
, &non_keyboard_wait_mask
);
5201 kill (getpid (), SIGCHLD
);
5203 #endif /* HAVE_PTYS */
5204 /* If we can detect process termination, don't consider the process
5205 gone just because its pipe is closed. */
5207 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
5212 /* Preserve status of processes already terminated. */
5213 XPROCESS (proc
)->tick
= ++process_tick
;
5214 deactivate_process (proc
);
5215 if (XPROCESS (proc
)->raw_status_new
)
5216 update_status (XPROCESS (proc
));
5217 if (EQ (XPROCESS (proc
)->status
, Qrun
))
5218 XPROCESS (proc
)->status
5219 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5222 #ifdef NON_BLOCKING_CONNECT
5223 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
5224 && FD_ISSET (channel
, &connect_wait_mask
))
5226 struct Lisp_Process
*p
;
5228 FD_CLR (channel
, &connect_wait_mask
);
5229 if (--num_pending_connects
< 0)
5232 proc
= chan_process
[channel
];
5236 p
= XPROCESS (proc
);
5239 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
5240 So only use it on systems where it is known to work. */
5242 int xlen
= sizeof(xerrno
);
5243 if (getsockopt(channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
5248 struct sockaddr pname
;
5249 int pnamelen
= sizeof(pname
);
5251 /* If connection failed, getpeername will fail. */
5253 if (getpeername(channel
, &pname
, &pnamelen
) < 0)
5255 /* Obtain connect failure code through error slippage. */
5258 if (errno
== ENOTCONN
&& read(channel
, &dummy
, 1) < 0)
5265 p
->tick
= ++process_tick
;
5266 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5267 deactivate_process (proc
);
5272 /* Execute the sentinel here. If we had relied on
5273 status_notify to do it later, it will read input
5274 from the process before calling the sentinel. */
5275 exec_sentinel (proc
, build_string ("open\n"));
5276 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5278 FD_SET (p
->infd
, &input_wait_mask
);
5279 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5283 #endif /* NON_BLOCKING_CONNECT */
5284 } /* end for each file descriptor */
5285 } /* end while exit conditions not met */
5287 unbind_to (count
, Qnil
);
5289 /* If calling from keyboard input, do not quit
5290 since we want to return C-g as an input character.
5291 Otherwise, do pending quit if requested. */
5294 /* Prevent input_pending from remaining set if we quit. */
5295 clear_input_pending ();
5298 #ifdef POLL_INTERRUPTED_SYS_CALL
5299 /* AlainF 5-Jul-1996
5300 HP-UX 10.10 seems to have problems with signals coming in
5301 Causes "poll: interrupted system call" messages when Emacs is run
5303 Turn periodic alarms back on */
5305 #endif /* POLL_INTERRUPTED_SYS_CALL */
5307 return got_some_input
;
5310 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5313 read_process_output_call (fun_and_args
)
5314 Lisp_Object fun_and_args
;
5316 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5320 read_process_output_error_handler (error
)
5323 cmd_error_internal (error
, "error in process filter: ");
5325 update_echo_area ();
5326 Fsleep_for (make_number (2), Qnil
);
5330 /* Read pending output from the process channel,
5331 starting with our buffered-ahead character if we have one.
5332 Yield number of decoded characters read.
5334 This function reads at most 4096 characters.
5335 If you want to read all available subprocess output,
5336 you must call it repeatedly until it returns zero.
5338 The characters read are decoded according to PROC's coding-system
5342 read_process_output (proc
, channel
)
5344 register int channel
;
5346 register int nbytes
;
5348 register Lisp_Object outstream
;
5349 register struct buffer
*old
= current_buffer
;
5350 register struct Lisp_Process
*p
= XPROCESS (proc
);
5351 register int opoint
;
5352 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5353 int carryover
= p
->decoding_carryover
;
5357 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5359 vs
= get_vms_process_pointer (p
->pid
);
5363 return (0); /* Really weird if it does this */
5364 if (!(vs
->iosb
[0] & 1))
5365 return -1; /* I/O error */
5368 error ("Could not get VMS process pointer");
5369 chars
= vs
->inputBuffer
;
5370 nbytes
= clean_vms_buffer (chars
, vs
->iosb
[1]);
5373 start_vms_process_read (vs
); /* Crank up the next read on the process */
5374 return 1; /* Nothing worth printing, say we got 1 */
5378 /* The data carried over in the previous decoding (which are at
5379 the tail of decoding buffer) should be prepended to the new
5380 data read to decode all together. */
5381 chars
= (char *) alloca (nbytes
+ carryover
);
5382 bcopy (SDATA (p
->decoding_buf
), buf
, carryover
);
5383 bcopy (vs
->inputBuffer
, chars
+ carryover
, nbytes
);
5387 chars
= (char *) alloca (carryover
+ readmax
);
5389 /* See the comment above. */
5390 bcopy (SDATA (p
->decoding_buf
), chars
, carryover
);
5392 #ifdef DATAGRAM_SOCKETS
5393 /* We have a working select, so proc_buffered_char is always -1. */
5394 if (DATAGRAM_CHAN_P (channel
))
5396 int len
= datagram_address
[channel
].len
;
5397 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5398 0, datagram_address
[channel
].sa
, &len
);
5402 if (proc_buffered_char
[channel
] < 0)
5404 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5405 #ifdef ADAPTIVE_READ_BUFFERING
5406 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5408 int delay
= p
->read_output_delay
;
5411 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5414 process_output_delay_count
++;
5415 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5418 else if (delay
> 0 && (nbytes
== readmax
))
5420 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5422 process_output_delay_count
--;
5424 p
->read_output_delay
= delay
;
5427 p
->read_output_skip
= 1;
5428 process_output_skip
= 1;
5435 chars
[carryover
] = proc_buffered_char
[channel
];
5436 proc_buffered_char
[channel
] = -1;
5437 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5441 nbytes
= nbytes
+ 1;
5443 #endif /* not VMS */
5445 p
->decoding_carryover
= 0;
5447 /* At this point, NBYTES holds number of bytes just received
5448 (including the one in proc_buffered_char[channel]). */
5451 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5453 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5456 /* Now set NBYTES how many bytes we must decode. */
5457 nbytes
+= carryover
;
5459 /* Read and dispose of the process output. */
5460 outstream
= p
->filter
;
5461 if (!NILP (outstream
))
5463 /* We inhibit quit here instead of just catching it so that
5464 hitting ^G when a filter happens to be running won't screw
5466 int count
= SPECPDL_INDEX ();
5467 Lisp_Object odeactivate
;
5468 Lisp_Object obuffer
, okeymap
;
5470 int outer_running_asynch_code
= running_asynch_code
;
5471 int waiting
= waiting_for_user_input_p
;
5473 /* No need to gcpro these, because all we do with them later
5474 is test them for EQness, and none of them should be a string. */
5475 odeactivate
= Vdeactivate_mark
;
5476 XSETBUFFER (obuffer
, current_buffer
);
5477 okeymap
= current_buffer
->keymap
;
5479 specbind (Qinhibit_quit
, Qt
);
5480 specbind (Qlast_nonmenu_event
, Qt
);
5482 /* In case we get recursively called,
5483 and we already saved the match data nonrecursively,
5484 save the same match data in safely recursive fashion. */
5485 if (outer_running_asynch_code
)
5488 /* Don't clobber the CURRENT match data, either! */
5489 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5490 restore_search_regs ();
5491 record_unwind_save_match_data ();
5492 Fset_match_data (tem
, Qt
);
5495 /* For speed, if a search happens within this code,
5496 save the match data in a special nonrecursive fashion. */
5497 running_asynch_code
= 1;
5499 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5500 text
= coding
->dst_object
;
5501 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5502 /* A new coding system might be found. */
5503 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5505 p
->decode_coding_system
= Vlast_coding_system_used
;
5507 /* Don't call setup_coding_system for
5508 proc_decode_coding_system[channel] here. It is done in
5509 detect_coding called via decode_coding above. */
5511 /* If a coding system for encoding is not yet decided, we set
5512 it as the same as coding-system for decoding.
5514 But, before doing that we must check if
5515 proc_encode_coding_system[p->outfd] surely points to a
5516 valid memory because p->outfd will be changed once EOF is
5517 sent to the process. */
5518 if (NILP (p
->encode_coding_system
)
5519 && proc_encode_coding_system
[p
->outfd
])
5521 p
->encode_coding_system
5522 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5523 setup_coding_system (p
->encode_coding_system
,
5524 proc_encode_coding_system
[p
->outfd
]);
5528 if (coding
->carryover_bytes
> 0)
5530 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5531 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5532 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5533 coding
->carryover_bytes
);
5534 p
->decoding_carryover
= coding
->carryover_bytes
;
5536 if (SBYTES (text
) > 0)
5537 internal_condition_case_1 (read_process_output_call
,
5539 Fcons (proc
, Fcons (text
, Qnil
))),
5540 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5541 read_process_output_error_handler
);
5543 /* If we saved the match data nonrecursively, restore it now. */
5544 restore_search_regs ();
5545 running_asynch_code
= outer_running_asynch_code
;
5547 /* Handling the process output should not deactivate the mark. */
5548 Vdeactivate_mark
= odeactivate
;
5550 /* Restore waiting_for_user_input_p as it was
5551 when we were called, in case the filter clobbered it. */
5552 waiting_for_user_input_p
= waiting
;
5554 #if 0 /* Call record_asynch_buffer_change unconditionally,
5555 because we might have changed minor modes or other things
5556 that affect key bindings. */
5557 if (! EQ (Fcurrent_buffer (), obuffer
)
5558 || ! EQ (current_buffer
->keymap
, okeymap
))
5560 /* But do it only if the caller is actually going to read events.
5561 Otherwise there's no need to make him wake up, and it could
5562 cause trouble (for example it would make sit_for return). */
5563 if (waiting_for_user_input_p
== -1)
5564 record_asynch_buffer_change ();
5567 start_vms_process_read (vs
);
5569 unbind_to (count
, Qnil
);
5573 /* If no filter, write into buffer if it isn't dead. */
5574 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5576 Lisp_Object old_read_only
;
5577 int old_begv
, old_zv
;
5578 int old_begv_byte
, old_zv_byte
;
5579 Lisp_Object odeactivate
;
5580 int before
, before_byte
;
5585 odeactivate
= Vdeactivate_mark
;
5587 Fset_buffer (p
->buffer
);
5589 opoint_byte
= PT_BYTE
;
5590 old_read_only
= current_buffer
->read_only
;
5593 old_begv_byte
= BEGV_BYTE
;
5594 old_zv_byte
= ZV_BYTE
;
5596 current_buffer
->read_only
= Qnil
;
5598 /* Insert new output into buffer
5599 at the current end-of-output marker,
5600 thus preserving logical ordering of input and output. */
5601 if (XMARKER (p
->mark
)->buffer
)
5602 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5603 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5606 SET_PT_BOTH (ZV
, ZV_BYTE
);
5608 before_byte
= PT_BYTE
;
5610 /* If the output marker is outside of the visible region, save
5611 the restriction and widen. */
5612 if (! (BEGV
<= PT
&& PT
<= ZV
))
5615 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5616 text
= coding
->dst_object
;
5617 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5618 /* A new coding system might be found. See the comment in the
5619 similar code in the previous `if' block. */
5620 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5622 p
->decode_coding_system
= Vlast_coding_system_used
;
5623 if (NILP (p
->encode_coding_system
)
5624 && proc_encode_coding_system
[p
->outfd
])
5626 p
->encode_coding_system
5627 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5628 setup_coding_system (p
->encode_coding_system
,
5629 proc_encode_coding_system
[p
->outfd
]);
5632 if (coding
->carryover_bytes
> 0)
5634 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5635 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5636 bcopy (coding
->carryover
, SDATA (p
->decoding_buf
),
5637 coding
->carryover_bytes
);
5638 p
->decoding_carryover
= coding
->carryover_bytes
;
5640 /* Adjust the multibyteness of TEXT to that of the buffer. */
5641 if (NILP (current_buffer
->enable_multibyte_characters
)
5642 != ! STRING_MULTIBYTE (text
))
5643 text
= (STRING_MULTIBYTE (text
)
5644 ? Fstring_as_unibyte (text
)
5645 : Fstring_to_multibyte (text
));
5646 /* Insert before markers in case we are inserting where
5647 the buffer's mark is, and the user's next command is Meta-y. */
5648 insert_from_string_before_markers (text
, 0, 0,
5649 SCHARS (text
), SBYTES (text
), 0);
5651 /* Make sure the process marker's position is valid when the
5652 process buffer is changed in the signal_after_change above.
5653 W3 is known to do that. */
5654 if (BUFFERP (p
->buffer
)
5655 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5656 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5658 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5660 update_mode_lines
++;
5662 /* Make sure opoint and the old restrictions
5663 float ahead of any new text just as point would. */
5664 if (opoint
>= before
)
5666 opoint
+= PT
- before
;
5667 opoint_byte
+= PT_BYTE
- before_byte
;
5669 if (old_begv
> before
)
5671 old_begv
+= PT
- before
;
5672 old_begv_byte
+= PT_BYTE
- before_byte
;
5674 if (old_zv
>= before
)
5676 old_zv
+= PT
- before
;
5677 old_zv_byte
+= PT_BYTE
- before_byte
;
5680 /* If the restriction isn't what it should be, set it. */
5681 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5682 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5684 /* Handling the process output should not deactivate the mark. */
5685 Vdeactivate_mark
= odeactivate
;
5687 current_buffer
->read_only
= old_read_only
;
5688 SET_PT_BOTH (opoint
, opoint_byte
);
5689 set_buffer_internal (old
);
5692 start_vms_process_read (vs
);
5697 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
5699 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
5700 This is intended for use by asynchronous process output filters and sentinels. */)
5703 return (waiting_for_user_input_p
? Qt
: Qnil
);
5706 /* Sending data to subprocess */
5708 jmp_buf send_process_frame
;
5709 Lisp_Object process_sent_to
;
5712 send_process_trap ()
5714 SIGNAL_THREAD_CHECK (SIGPIPE
);
5719 sigunblock (sigmask (SIGPIPE
));
5720 longjmp (send_process_frame
, 1);
5723 /* Send some data to process PROC.
5724 BUF is the beginning of the data; LEN is the number of characters.
5725 OBJECT is the Lisp object that the data comes from. If OBJECT is
5726 nil or t, it means that the data comes from C string.
5728 If OBJECT is not nil, the data is encoded by PROC's coding-system
5729 for encoding before it is sent.
5731 This function can evaluate Lisp code and can garbage collect. */
5734 send_process (proc
, buf
, len
, object
)
5735 volatile Lisp_Object proc
;
5736 unsigned char *volatile buf
;
5738 volatile Lisp_Object object
;
5740 /* Use volatile to protect variables from being clobbered by longjmp. */
5741 struct Lisp_Process
*p
= XPROCESS (proc
);
5743 struct coding_system
*coding
;
5744 struct gcpro gcpro1
;
5745 SIGTYPE (*volatile old_sigpipe
) ();
5750 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
5753 if (p
->raw_status_new
)
5755 if (! EQ (p
->status
, Qrun
))
5756 error ("Process %s not running", SDATA (p
->name
));
5758 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5760 coding
= proc_encode_coding_system
[p
->outfd
];
5761 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5763 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5764 || (BUFFERP (object
)
5765 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5768 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5769 /* The coding system for encoding was changed to raw-text
5770 because we sent a unibyte text previously. Now we are
5771 sending a multibyte text, thus we must encode it by the
5772 original coding system specified for the current process. */
5773 setup_coding_system (p
->encode_coding_system
, coding
);
5774 coding
->src_multibyte
= 1;
5778 /* For sending a unibyte text, character code conversion should
5779 not take place but EOL conversion should. So, setup raw-text
5780 or one of the subsidiary if we have not yet done it. */
5781 if (CODING_REQUIRE_ENCODING (coding
))
5783 if (CODING_REQUIRE_FLUSHING (coding
))
5785 /* But, before changing the coding, we must flush out data. */
5786 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5787 send_process (proc
, "", 0, Qt
);
5788 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5790 setup_coding_system (raw_text_coding_system
5791 (Vlast_coding_system_used
),
5793 coding
->src_multibyte
= 0;
5796 coding
->dst_multibyte
= 0;
5798 if (CODING_REQUIRE_ENCODING (coding
))
5800 coding
->dst_object
= Qt
;
5801 if (BUFFERP (object
))
5803 int from_byte
, from
, to
;
5804 int save_pt
, save_pt_byte
;
5805 struct buffer
*cur
= current_buffer
;
5807 set_buffer_internal (XBUFFER (object
));
5808 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5810 from_byte
= PTR_BYTE_POS (buf
);
5811 from
= BYTE_TO_CHAR (from_byte
);
5812 to
= BYTE_TO_CHAR (from_byte
+ len
);
5813 TEMP_SET_PT_BOTH (from
, from_byte
);
5814 encode_coding_object (coding
, object
, from
, from_byte
,
5815 to
, from_byte
+ len
, Qt
);
5816 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5817 set_buffer_internal (cur
);
5819 else if (STRINGP (object
))
5821 encode_coding_string (coding
, object
, 1);
5825 coding
->dst_object
= make_unibyte_string (buf
, len
);
5826 coding
->produced
= len
;
5829 len
= coding
->produced
;
5830 buf
= SDATA (coding
->dst_object
);
5834 vs
= get_vms_process_pointer (p
->pid
);
5836 error ("Could not find this process: %x", p
->pid
);
5837 else if (write_to_vms_process (vs
, buf
, len
))
5841 if (pty_max_bytes
== 0)
5843 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5844 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5845 if (pty_max_bytes
< 0)
5846 pty_max_bytes
= 250;
5848 pty_max_bytes
= 250;
5850 /* Deduct one, to leave space for the eof. */
5854 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5855 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5856 when returning with longjmp despite being declared volatile. */
5857 if (!setjmp (send_process_frame
))
5859 process_sent_to
= proc
;
5864 /* Decide how much data we can send in one batch.
5865 Long lines need to be split into multiple batches. */
5868 /* Starting this at zero is always correct when not the first
5869 iteration because the previous iteration ended by sending C-d.
5870 It may not be correct for the first iteration
5871 if a partial line was sent in a separate send_process call.
5872 If that proves worth handling, we need to save linepos
5873 in the process object. */
5875 unsigned char *ptr
= (unsigned char *) buf
;
5876 unsigned char *end
= (unsigned char *) buf
+ len
;
5878 /* Scan through this text for a line that is too long. */
5879 while (ptr
!= end
&& linepos
< pty_max_bytes
)
5887 /* If we found one, break the line there
5888 and put in a C-d to force the buffer through. */
5892 /* Send this batch, using one or more write calls. */
5895 int outfd
= p
->outfd
;
5896 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
5897 #ifdef DATAGRAM_SOCKETS
5898 if (DATAGRAM_CHAN_P (outfd
))
5900 rv
= sendto (outfd
, (char *) buf
, this,
5901 0, datagram_address
[outfd
].sa
,
5902 datagram_address
[outfd
].len
);
5903 if (rv
< 0 && errno
== EMSGSIZE
)
5905 signal (SIGPIPE
, old_sigpipe
);
5906 report_file_error ("sending datagram",
5907 Fcons (proc
, Qnil
));
5913 rv
= emacs_write (outfd
, (char *) buf
, this);
5914 #ifdef ADAPTIVE_READ_BUFFERING
5915 if (p
->read_output_delay
> 0
5916 && p
->adaptive_read_buffering
== 1)
5918 p
->read_output_delay
= 0;
5919 process_output_delay_count
--;
5920 p
->read_output_skip
= 0;
5924 signal (SIGPIPE
, old_sigpipe
);
5930 || errno
== EWOULDBLOCK
5936 /* Buffer is full. Wait, accepting input;
5937 that may allow the program
5938 to finish doing output and read more. */
5942 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5943 /* A gross hack to work around a bug in FreeBSD.
5944 In the following sequence, read(2) returns
5948 write(2) 954 bytes, get EAGAIN
5949 read(2) 1024 bytes in process_read_output
5950 read(2) 11 bytes in process_read_output
5952 That is, read(2) returns more bytes than have
5953 ever been written successfully. The 1033 bytes
5954 read are the 1022 bytes written successfully
5955 after processing (for example with CRs added if
5956 the terminal is set up that way which it is
5957 here). The same bytes will be seen again in a
5958 later read(2), without the CRs. */
5960 if (errno
== EAGAIN
)
5963 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5965 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5967 /* Running filters might relocate buffers or strings.
5968 Arrange to relocate BUF. */
5969 if (BUFFERP (object
))
5970 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5971 else if (STRINGP (object
))
5972 offset
= buf
- SDATA (object
);
5974 #ifdef EMACS_HAS_USECS
5975 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5977 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5980 if (BUFFERP (object
))
5981 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5982 else if (STRINGP (object
))
5983 buf
= offset
+ SDATA (object
);
5988 /* This is a real error. */
5989 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5996 /* If we sent just part of the string, put in an EOF (C-d)
5997 to force it through, before we send the rest. */
5999 Fprocess_send_eof (proc
);
6002 #endif /* not VMS */
6005 signal (SIGPIPE
, old_sigpipe
);
6007 proc
= process_sent_to
;
6008 p
= XPROCESS (proc
);
6010 p
->raw_status_new
= 0;
6011 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
6012 p
->tick
= ++process_tick
;
6013 deactivate_process (proc
);
6015 error ("Error writing to process %s; closed it", SDATA (p
->name
));
6017 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
6024 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
6026 doc
: /* Send current contents of region as input to PROCESS.
6027 PROCESS may be a process, a buffer, the name of a process or buffer, or
6028 nil, indicating the current buffer's process.
6029 Called from program, takes three arguments, PROCESS, START and END.
6030 If the region is more than 500 characters long,
6031 it is sent in several bunches. This may happen even for shorter regions.
6032 Output from processes can arrive in between bunches. */)
6033 (process
, start
, end
)
6034 Lisp_Object process
, start
, end
;
6039 proc
= get_process (process
);
6040 validate_region (&start
, &end
);
6042 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
6043 move_gap (XINT (start
));
6045 start1
= CHAR_TO_BYTE (XINT (start
));
6046 end1
= CHAR_TO_BYTE (XINT (end
));
6047 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
6048 Fcurrent_buffer ());
6053 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
6055 doc
: /* Send PROCESS the contents of STRING as input.
6056 PROCESS may be a process, a buffer, the name of a process or buffer, or
6057 nil, indicating the current buffer's process.
6058 If STRING is more than 500 characters long,
6059 it is sent in several bunches. This may happen even for shorter strings.
6060 Output from processes can arrive in between bunches. */)
6062 Lisp_Object process
, string
;
6065 CHECK_STRING (string
);
6066 proc
= get_process (process
);
6067 send_process (proc
, SDATA (string
),
6068 SBYTES (string
), string
);
6072 /* Return the foreground process group for the tty/pty that
6073 the process P uses. */
6075 emacs_get_tty_pgrp (p
)
6076 struct Lisp_Process
*p
;
6081 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
6084 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
6085 master side. Try the slave side. */
6086 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
6090 ioctl (fd
, TIOCGPGRP
, &gid
);
6094 #endif /* defined (TIOCGPGRP ) */
6099 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
6100 Sprocess_running_child_p
, 0, 1, 0,
6101 doc
: /* Return t if PROCESS has given the terminal to a child.
6102 If the operating system does not make it possible to find out,
6103 return t unconditionally. */)
6105 Lisp_Object process
;
6107 /* Initialize in case ioctl doesn't exist or gives an error,
6108 in a way that will cause returning t. */
6111 struct Lisp_Process
*p
;
6113 proc
= get_process (process
);
6114 p
= XPROCESS (proc
);
6116 if (!EQ (p
->type
, Qreal
))
6117 error ("Process %s is not a subprocess",
6120 error ("Process %s is not active",
6123 gid
= emacs_get_tty_pgrp (p
);
6130 /* send a signal number SIGNO to PROCESS.
6131 If CURRENT_GROUP is t, that means send to the process group
6132 that currently owns the terminal being used to communicate with PROCESS.
6133 This is used for various commands in shell mode.
6134 If CURRENT_GROUP is lambda, that means send to the process group
6135 that currently owns the terminal, but only if it is NOT the shell itself.
6137 If NOMSG is zero, insert signal-announcements into process's buffers
6140 If we can, we try to signal PROCESS by sending control characters
6141 down the pty. This allows us to signal inferiors who have changed
6142 their uid, for which killpg would return an EPERM error. */
6145 process_send_signal (process
, signo
, current_group
, nomsg
)
6146 Lisp_Object process
;
6148 Lisp_Object current_group
;
6152 register struct Lisp_Process
*p
;
6156 proc
= get_process (process
);
6157 p
= XPROCESS (proc
);
6159 if (!EQ (p
->type
, Qreal
))
6160 error ("Process %s is not a subprocess",
6163 error ("Process %s is not active",
6167 current_group
= Qnil
;
6169 /* If we are using pgrps, get a pgrp number and make it negative. */
6170 if (NILP (current_group
))
6171 /* Send the signal to the shell's process group. */
6175 #ifdef SIGNALS_VIA_CHARACTERS
6176 /* If possible, send signals to the entire pgrp
6177 by sending an input character to it. */
6179 /* TERMIOS is the latest and bestest, and seems most likely to
6180 work. If the system has it, use it. */
6183 cc_t
*sig_char
= NULL
;
6185 tcgetattr (p
->infd
, &t
);
6190 sig_char
= &t
.c_cc
[VINTR
];
6194 sig_char
= &t
.c_cc
[VQUIT
];
6198 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
6199 sig_char
= &t
.c_cc
[VSWTCH
];
6201 sig_char
= &t
.c_cc
[VSUSP
];
6206 if (sig_char
&& *sig_char
!= CDISABLE
)
6208 send_process (proc
, sig_char
, 1, Qnil
);
6211 /* If we can't send the signal with a character,
6212 fall through and send it another way. */
6213 #else /* ! HAVE_TERMIOS */
6215 /* On Berkeley descendants, the following IOCTL's retrieve the
6216 current control characters. */
6217 #if defined (TIOCGLTC) && defined (TIOCGETC)
6225 ioctl (p
->infd
, TIOCGETC
, &c
);
6226 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
6229 ioctl (p
->infd
, TIOCGETC
, &c
);
6230 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
6234 ioctl (p
->infd
, TIOCGLTC
, &lc
);
6235 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
6237 #endif /* ! defined (SIGTSTP) */
6240 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6242 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6249 ioctl (p
->infd
, TCGETA
, &t
);
6250 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
6253 ioctl (p
->infd
, TCGETA
, &t
);
6254 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
6258 ioctl (p
->infd
, TCGETA
, &t
);
6259 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
6261 #endif /* ! defined (SIGTSTP) */
6263 #else /* ! defined (TCGETA) */
6264 Your configuration files are messed up
.
6265 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6266 you'd better be using one of the alternatives above! */
6267 #endif /* ! defined (TCGETA) */
6268 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6269 /* In this case, the code above should alway return. */
6271 #endif /* ! defined HAVE_TERMIOS */
6273 /* The code above may fall through if it can't
6274 handle the signal. */
6275 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
6278 /* Get the current pgrp using the tty itself, if we have that.
6279 Otherwise, use the pty to get the pgrp.
6280 On pfa systems, saka@pfu.fujitsu.co.JP writes:
6281 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6282 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
6283 His patch indicates that if TIOCGPGRP returns an error, then
6284 we should just assume that p->pid is also the process group id. */
6286 gid
= emacs_get_tty_pgrp (p
);
6289 /* If we can't get the information, assume
6290 the shell owns the tty. */
6293 /* It is not clear whether anything really can set GID to -1.
6294 Perhaps on some system one of those ioctls can or could do so.
6295 Or perhaps this is vestigial. */
6298 #else /* ! defined (TIOCGPGRP ) */
6299 /* Can't select pgrps on this system, so we know that
6300 the child itself heads the pgrp. */
6302 #endif /* ! defined (TIOCGPGRP ) */
6304 /* If current_group is lambda, and the shell owns the terminal,
6305 don't send any signal. */
6306 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
6314 p
->raw_status_new
= 0;
6316 p
->tick
= ++process_tick
;
6318 status_notify (NULL
);
6320 #endif /* ! defined (SIGCONT) */
6323 send_process (proc
, "\003", 1, Qnil
); /* ^C */
6328 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
6333 sys$
forcex (&(p
->pid
), 0, 1);
6336 flush_pending_output (p
->infd
);
6340 /* If we don't have process groups, send the signal to the immediate
6341 subprocess. That isn't really right, but it's better than any
6342 obvious alternative. */
6345 kill (p
->pid
, signo
);
6349 /* gid may be a pid, or minus a pgrp's number */
6351 if (!NILP (current_group
))
6353 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
6354 EMACS_KILLPG (gid
, signo
);
6361 #else /* ! defined (TIOCSIGSEND) */
6362 EMACS_KILLPG (gid
, signo
);
6363 #endif /* ! defined (TIOCSIGSEND) */
6366 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
6367 doc
: /* Interrupt process PROCESS.
6368 PROCESS may be a process, a buffer, or the name of a process or buffer.
6369 No arg or nil means current buffer's process.
6370 Second arg CURRENT-GROUP non-nil means send signal to
6371 the current process-group of the process's controlling terminal
6372 rather than to the process's own process group.
6373 If the process is a shell, this means interrupt current subjob
6374 rather than the shell.
6376 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6377 don't send the signal. */)
6378 (process
, current_group
)
6379 Lisp_Object process
, current_group
;
6381 process_send_signal (process
, SIGINT
, current_group
, 0);
6385 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6386 doc
: /* Kill process PROCESS. May be process or name of one.
6387 See function `interrupt-process' for more details on usage. */)
6388 (process
, current_group
)
6389 Lisp_Object process
, current_group
;
6391 process_send_signal (process
, SIGKILL
, current_group
, 0);
6395 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6396 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6397 See function `interrupt-process' for more details on usage. */)
6398 (process
, current_group
)
6399 Lisp_Object process
, current_group
;
6401 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6405 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6406 doc
: /* Stop process PROCESS. May be process or name of one.
6407 See function `interrupt-process' for more details on usage.
6408 If PROCESS is a network or serial process, inhibit handling of incoming
6410 (process
, current_group
)
6411 Lisp_Object process
, current_group
;
6414 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6416 struct Lisp_Process
*p
;
6418 p
= XPROCESS (process
);
6419 if (NILP (p
->command
)
6422 FD_CLR (p
->infd
, &input_wait_mask
);
6423 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6430 error ("No SIGTSTP support");
6432 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6437 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6438 doc
: /* Continue process PROCESS. May be process or name of one.
6439 See function `interrupt-process' for more details on usage.
6440 If PROCESS is a network or serial process, resume handling of incoming
6442 (process
, current_group
)
6443 Lisp_Object process
, current_group
;
6446 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6448 struct Lisp_Process
*p
;
6450 p
= XPROCESS (process
);
6451 if (EQ (p
->command
, Qt
)
6453 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6455 FD_SET (p
->infd
, &input_wait_mask
);
6456 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6458 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6459 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6462 tcflush (p
->infd
, TCIFLUSH
);
6470 process_send_signal (process
, SIGCONT
, current_group
, 0);
6472 error ("No SIGCONT support");
6477 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6478 2, 2, "sProcess (name or number): \nnSignal code: ",
6479 doc
: /* Send PROCESS the signal with code SIGCODE.
6480 PROCESS may also be a number specifying the process id of the
6481 process to signal; in this case, the process need not be a child of
6483 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6485 Lisp_Object process
, sigcode
;
6489 if (INTEGERP (process
))
6491 pid
= XINT (process
);
6495 if (FLOATP (process
))
6497 pid
= (pid_t
) XFLOAT_DATA (process
);
6501 if (STRINGP (process
))
6504 if (tem
= Fget_process (process
), NILP (tem
))
6506 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6513 process
= get_process (process
);
6518 CHECK_PROCESS (process
);
6519 pid
= XPROCESS (process
)->pid
;
6521 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6525 #define parse_signal(NAME, VALUE) \
6526 else if (!xstrcasecmp (name, NAME)) \
6527 XSETINT (sigcode, VALUE)
6529 if (INTEGERP (sigcode
))
6533 unsigned char *name
;
6535 CHECK_SYMBOL (sigcode
);
6536 name
= SDATA (SYMBOL_NAME (sigcode
));
6538 if (!strncmp(name
, "SIG", 3) || !strncmp(name
, "sig", 3))
6544 parse_signal ("usr1", SIGUSR1
);
6547 parse_signal ("usr2", SIGUSR2
);
6550 parse_signal ("term", SIGTERM
);
6553 parse_signal ("hup", SIGHUP
);
6556 parse_signal ("int", SIGINT
);
6559 parse_signal ("quit", SIGQUIT
);
6562 parse_signal ("ill", SIGILL
);
6565 parse_signal ("abrt", SIGABRT
);
6568 parse_signal ("emt", SIGEMT
);
6571 parse_signal ("kill", SIGKILL
);
6574 parse_signal ("fpe", SIGFPE
);
6577 parse_signal ("bus", SIGBUS
);
6580 parse_signal ("segv", SIGSEGV
);
6583 parse_signal ("sys", SIGSYS
);
6586 parse_signal ("pipe", SIGPIPE
);
6589 parse_signal ("alrm", SIGALRM
);
6592 parse_signal ("urg", SIGURG
);
6595 parse_signal ("stop", SIGSTOP
);
6598 parse_signal ("tstp", SIGTSTP
);
6601 parse_signal ("cont", SIGCONT
);
6604 parse_signal ("chld", SIGCHLD
);
6607 parse_signal ("ttin", SIGTTIN
);
6610 parse_signal ("ttou", SIGTTOU
);
6613 parse_signal ("io", SIGIO
);
6616 parse_signal ("xcpu", SIGXCPU
);
6619 parse_signal ("xfsz", SIGXFSZ
);
6622 parse_signal ("vtalrm", SIGVTALRM
);
6625 parse_signal ("prof", SIGPROF
);
6628 parse_signal ("winch", SIGWINCH
);
6631 parse_signal ("info", SIGINFO
);
6634 error ("Undefined signal name %s", name
);
6639 return make_number (kill (pid
, XINT (sigcode
)));
6642 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6643 doc
: /* Make PROCESS see end-of-file in its input.
6644 EOF comes after any text already sent to it.
6645 PROCESS may be a process, a buffer, the name of a process or buffer, or
6646 nil, indicating the current buffer's process.
6647 If PROCESS is a network connection, or is a process communicating
6648 through a pipe (as opposed to a pty), then you cannot send any more
6649 text to PROCESS after you call this function.
6650 If PROCESS is a serial process, wait until all output written to the
6651 process has been transmitted to the serial port. */)
6653 Lisp_Object process
;
6656 struct coding_system
*coding
;
6658 if (DATAGRAM_CONN_P (process
))
6661 proc
= get_process (process
);
6662 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6664 /* Make sure the process is really alive. */
6665 if (XPROCESS (proc
)->raw_status_new
)
6666 update_status (XPROCESS (proc
));
6667 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6668 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6670 if (CODING_REQUIRE_FLUSHING (coding
))
6672 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6673 send_process (proc
, "", 0, Qnil
);
6677 send_process (proc
, "\032", 1, Qnil
); /* ^z */
6679 if (XPROCESS (proc
)->pty_flag
)
6680 send_process (proc
, "\004", 1, Qnil
);
6681 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6684 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6685 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6687 /* Do nothing on Windows because writes are blocking. */
6691 int old_outfd
, new_outfd
;
6693 #ifdef HAVE_SHUTDOWN
6694 /* If this is a network connection, or socketpair is used
6695 for communication with the subprocess, call shutdown to cause EOF.
6696 (In some old system, shutdown to socketpair doesn't work.
6697 Then we just can't win.) */
6698 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6699 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6700 shutdown (XPROCESS (proc
)->outfd
, 1);
6701 /* In case of socketpair, outfd == infd, so don't close it. */
6702 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6703 emacs_close (XPROCESS (proc
)->outfd
);
6704 #else /* not HAVE_SHUTDOWN */
6705 emacs_close (XPROCESS (proc
)->outfd
);
6706 #endif /* not HAVE_SHUTDOWN */
6707 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6710 old_outfd
= XPROCESS (proc
)->outfd
;
6712 if (!proc_encode_coding_system
[new_outfd
])
6713 proc_encode_coding_system
[new_outfd
]
6714 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6715 bcopy (proc_encode_coding_system
[old_outfd
],
6716 proc_encode_coding_system
[new_outfd
],
6717 sizeof (struct coding_system
));
6718 bzero (proc_encode_coding_system
[old_outfd
],
6719 sizeof (struct coding_system
));
6721 XPROCESS (proc
)->outfd
= new_outfd
;
6727 /* Kill all processes associated with `buffer'.
6728 If `buffer' is nil, kill all processes */
6731 kill_buffer_processes (buffer
)
6734 Lisp_Object tail
, proc
;
6736 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6738 proc
= XCDR (XCAR (tail
));
6740 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
6742 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
6743 Fdelete_process (proc
);
6744 else if (XPROCESS (proc
)->infd
>= 0)
6745 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
6750 /* On receipt of a signal that a child status has changed, loop asking
6751 about children with changed statuses until the system says there
6754 All we do is change the status; we do not run sentinels or print
6755 notifications. That is saved for the next time keyboard input is
6756 done, in order to avoid timing errors.
6758 ** WARNING: this can be called during garbage collection.
6759 Therefore, it must not be fooled by the presence of mark bits in
6762 ** USG WARNING: Although it is not obvious from the documentation
6763 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6764 signal() before executing at least one wait(), otherwise the
6765 handler will be called again, resulting in an infinite loop. The
6766 relevant portion of the documentation reads "SIGCLD signals will be
6767 queued and the signal-catching function will be continually
6768 reentered until the queue is empty". Invoking signal() causes the
6769 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6772 ** Malloc WARNING: This should never call malloc either directly or
6773 indirectly; if it does, that is a bug */
6777 sigchld_handler (signo
)
6780 int old_errno
= errno
;
6782 register struct Lisp_Process
*p
;
6783 extern EMACS_TIME
*input_available_clear_time
;
6785 SIGNAL_THREAD_CHECK (signo
);
6789 sigheld
|= sigbit (SIGCHLD
);
6801 #endif /* no WUNTRACED */
6802 /* Keep trying to get a status until we get a definitive result. */
6806 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6808 while (pid
< 0 && errno
== EINTR
);
6812 /* PID == 0 means no processes found, PID == -1 means a real
6813 failure. We have done all our job, so return. */
6815 /* USG systems forget handlers when they are used;
6816 must reestablish each time */
6817 #if defined (USG) && !defined (POSIX_SIGNALS)
6818 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
6821 sigheld
&= ~sigbit (SIGCHLD
);
6829 #endif /* no WNOHANG */
6831 /* Find the process that signaled us, and record its status. */
6833 /* The process can have been deleted by Fdelete_process. */
6834 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6836 Lisp_Object xpid
= XCAR (tail
);
6837 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6838 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6840 XSETCAR (tail
, Qnil
);
6841 goto sigchld_end_of_loop
;
6845 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6847 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6849 proc
= XCDR (XCAR (tail
));
6850 p
= XPROCESS (proc
);
6851 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6856 /* Look for an asynchronous process whose pid hasn't been filled
6859 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6861 proc
= XCDR (XCAR (tail
));
6862 p
= XPROCESS (proc
);
6868 /* Change the status of the process that was found. */
6871 union { int i
; WAITTYPE wt
; } u
;
6872 int clear_desc_flag
= 0;
6874 p
->tick
= ++process_tick
;
6876 p
->raw_status
= u
.i
;
6877 p
->raw_status_new
= 1;
6879 /* If process has terminated, stop waiting for its output. */
6880 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6882 clear_desc_flag
= 1;
6884 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6885 if (clear_desc_flag
)
6887 FD_CLR (p
->infd
, &input_wait_mask
);
6888 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6891 /* Tell wait_reading_process_output that it needs to wake up and
6893 if (input_available_clear_time
)
6894 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6897 /* There was no asynchronous process found for that pid: we have
6898 a synchronous process. */
6901 synch_process_alive
= 0;
6903 /* Report the status of the synchronous process. */
6905 synch_process_retcode
= WRETCODE (w
);
6906 else if (WIFSIGNALED (w
))
6907 synch_process_termsig
= WTERMSIG (w
);
6909 /* Tell wait_reading_process_output that it needs to wake up and
6911 if (input_available_clear_time
)
6912 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6915 sigchld_end_of_loop
:
6918 /* On some systems, we must return right away.
6919 If any more processes want to signal us, we will
6921 Otherwise (on systems that have WNOHANG), loop around
6922 to use up all the processes that have something to tell us. */
6923 #if (defined WINDOWSNT \
6924 || (defined USG && !defined GNU_LINUX \
6925 && !(defined HPUX && defined WNOHANG)))
6926 #if defined (USG) && ! defined (POSIX_SIGNALS)
6927 signal (signo
, sigchld_handler
);
6931 #endif /* USG, but not HPUX with WNOHANG */
6934 #endif /* SIGCHLD */
6938 exec_sentinel_unwind (data
)
6941 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6946 exec_sentinel_error_handler (error
)
6949 cmd_error_internal (error
, "error in process sentinel: ");
6951 update_echo_area ();
6952 Fsleep_for (make_number (2), Qnil
);
6957 exec_sentinel (proc
, reason
)
6958 Lisp_Object proc
, reason
;
6960 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6961 register struct Lisp_Process
*p
= XPROCESS (proc
);
6962 int count
= SPECPDL_INDEX ();
6963 int outer_running_asynch_code
= running_asynch_code
;
6964 int waiting
= waiting_for_user_input_p
;
6966 if (inhibit_sentinels
)
6969 /* No need to gcpro these, because all we do with them later
6970 is test them for EQness, and none of them should be a string. */
6971 odeactivate
= Vdeactivate_mark
;
6972 XSETBUFFER (obuffer
, current_buffer
);
6973 okeymap
= current_buffer
->keymap
;
6975 sentinel
= p
->sentinel
;
6976 if (NILP (sentinel
))
6979 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6980 assure that it gets restored no matter how the sentinel exits. */
6982 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6983 /* Inhibit quit so that random quits don't screw up a running filter. */
6984 specbind (Qinhibit_quit
, Qt
);
6985 specbind (Qlast_nonmenu_event
, Qt
);
6987 /* In case we get recursively called,
6988 and we already saved the match data nonrecursively,
6989 save the same match data in safely recursive fashion. */
6990 if (outer_running_asynch_code
)
6993 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6994 restore_search_regs ();
6995 record_unwind_save_match_data ();
6996 Fset_match_data (tem
, Qt
);
6999 /* For speed, if a search happens within this code,
7000 save the match data in a special nonrecursive fashion. */
7001 running_asynch_code
= 1;
7003 internal_condition_case_1 (read_process_output_call
,
7005 Fcons (proc
, Fcons (reason
, Qnil
))),
7006 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
7007 exec_sentinel_error_handler
);
7009 /* If we saved the match data nonrecursively, restore it now. */
7010 restore_search_regs ();
7011 running_asynch_code
= outer_running_asynch_code
;
7013 Vdeactivate_mark
= odeactivate
;
7015 /* Restore waiting_for_user_input_p as it was
7016 when we were called, in case the filter clobbered it. */
7017 waiting_for_user_input_p
= waiting
;
7020 if (! EQ (Fcurrent_buffer (), obuffer
)
7021 || ! EQ (current_buffer
->keymap
, okeymap
))
7023 /* But do it only if the caller is actually going to read events.
7024 Otherwise there's no need to make him wake up, and it could
7025 cause trouble (for example it would make sit_for return). */
7026 if (waiting_for_user_input_p
== -1)
7027 record_asynch_buffer_change ();
7029 unbind_to (count
, Qnil
);
7032 /* Report all recent events of a change in process status
7033 (either run the sentinel or output a message).
7034 This is usually done while Emacs is waiting for keyboard input
7035 but can be done at other times. */
7038 status_notify (deleting_process
)
7039 struct Lisp_Process
*deleting_process
;
7041 register Lisp_Object proc
, buffer
;
7042 Lisp_Object tail
, msg
;
7043 struct gcpro gcpro1
, gcpro2
;
7047 /* We need to gcpro tail; if read_process_output calls a filter
7048 which deletes a process and removes the cons to which tail points
7049 from Vprocess_alist, and then causes a GC, tail is an unprotected
7053 /* Set this now, so that if new processes are created by sentinels
7054 that we run, we get called again to handle their status changes. */
7055 update_tick
= process_tick
;
7057 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7060 register struct Lisp_Process
*p
;
7062 proc
= Fcdr (XCAR (tail
));
7063 p
= XPROCESS (proc
);
7065 if (p
->tick
!= p
->update_tick
)
7067 p
->update_tick
= p
->tick
;
7069 /* If process is still active, read any output that remains. */
7070 while (! EQ (p
->filter
, Qt
)
7071 && ! EQ (p
->status
, Qconnect
)
7072 && ! EQ (p
->status
, Qlisten
)
7073 /* Network or serial process not stopped: */
7074 && ! EQ (p
->command
, Qt
)
7076 && p
!= deleting_process
7077 && read_process_output (proc
, p
->infd
) > 0);
7081 /* Get the text to use for the message. */
7082 if (p
->raw_status_new
)
7084 msg
= status_message (p
);
7086 /* If process is terminated, deactivate it or delete it. */
7088 if (CONSP (p
->status
))
7089 symbol
= XCAR (p
->status
);
7091 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
7092 || EQ (symbol
, Qclosed
))
7094 if (delete_exited_processes
)
7095 remove_process (proc
);
7097 deactivate_process (proc
);
7100 /* The actions above may have further incremented p->tick.
7101 So set p->update_tick again
7102 so that an error in the sentinel will not cause
7103 this code to be run again. */
7104 p
->update_tick
= p
->tick
;
7105 /* Now output the message suitably. */
7106 if (!NILP (p
->sentinel
))
7107 exec_sentinel (proc
, msg
);
7108 /* Don't bother with a message in the buffer
7109 when a process becomes runnable. */
7110 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
7112 Lisp_Object ro
, tem
;
7113 struct buffer
*old
= current_buffer
;
7114 int opoint
, opoint_byte
;
7115 int before
, before_byte
;
7117 ro
= XBUFFER (buffer
)->read_only
;
7119 /* Avoid error if buffer is deleted
7120 (probably that's why the process is dead, too) */
7121 if (NILP (XBUFFER (buffer
)->name
))
7123 Fset_buffer (buffer
);
7126 opoint_byte
= PT_BYTE
;
7127 /* Insert new output into buffer
7128 at the current end-of-output marker,
7129 thus preserving logical ordering of input and output. */
7130 if (XMARKER (p
->mark
)->buffer
)
7131 Fgoto_char (p
->mark
);
7133 SET_PT_BOTH (ZV
, ZV_BYTE
);
7136 before_byte
= PT_BYTE
;
7138 tem
= current_buffer
->read_only
;
7139 current_buffer
->read_only
= Qnil
;
7140 insert_string ("\nProcess ");
7141 Finsert (1, &p
->name
);
7142 insert_string (" ");
7144 current_buffer
->read_only
= tem
;
7145 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
7147 if (opoint
>= before
)
7148 SET_PT_BOTH (opoint
+ (PT
- before
),
7149 opoint_byte
+ (PT_BYTE
- before_byte
));
7151 SET_PT_BOTH (opoint
, opoint_byte
);
7153 set_buffer_internal (old
);
7158 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
7159 redisplay_preserve_echo_area (13);
7165 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
7166 Sset_process_coding_system
, 1, 3, 0,
7167 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
7168 DECODING will be used to decode subprocess output and ENCODING to
7169 encode subprocess input. */)
7170 (process
, decoding
, encoding
)
7171 register Lisp_Object process
, decoding
, encoding
;
7173 register struct Lisp_Process
*p
;
7175 CHECK_PROCESS (process
);
7176 p
= XPROCESS (process
);
7178 error ("Input file descriptor of %s closed", SDATA (p
->name
));
7180 error ("Output file descriptor of %s closed", SDATA (p
->name
));
7181 Fcheck_coding_system (decoding
);
7182 Fcheck_coding_system (encoding
);
7183 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
7184 p
->decode_coding_system
= decoding
;
7185 p
->encode_coding_system
= encoding
;
7186 setup_process_coding_systems (process
);
7191 DEFUN ("process-coding-system",
7192 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
7193 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
7195 register Lisp_Object process
;
7197 CHECK_PROCESS (process
);
7198 return Fcons (XPROCESS (process
)->decode_coding_system
,
7199 XPROCESS (process
)->encode_coding_system
);
7202 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
7203 Sset_process_filter_multibyte
, 2, 2, 0,
7204 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
7205 If FLAG is non-nil, the filter is given multibyte strings.
7206 If FLAG is nil, the filter is given unibyte strings. In this case,
7207 all character code conversion except for end-of-line conversion is
7210 Lisp_Object process
, flag
;
7212 register struct Lisp_Process
*p
;
7214 CHECK_PROCESS (process
);
7215 p
= XPROCESS (process
);
7217 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
7218 setup_process_coding_systems (process
);
7223 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
7224 Sprocess_filter_multibyte_p
, 1, 1, 0,
7225 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
7227 Lisp_Object process
;
7229 register struct Lisp_Process
*p
;
7230 struct coding_system
*coding
;
7232 CHECK_PROCESS (process
);
7233 p
= XPROCESS (process
);
7234 coding
= proc_decode_coding_system
[p
->infd
];
7235 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
7240 /* Add DESC to the set of keyboard input descriptors. */
7243 add_keyboard_wait_descriptor (desc
)
7246 FD_SET (desc
, &input_wait_mask
);
7247 FD_SET (desc
, &non_process_wait_mask
);
7248 if (desc
> max_keyboard_desc
)
7249 max_keyboard_desc
= desc
;
7252 static int add_gpm_wait_descriptor_called_flag
;
7255 add_gpm_wait_descriptor (desc
)
7258 if (! add_gpm_wait_descriptor_called_flag
)
7259 FD_CLR (0, &input_wait_mask
);
7260 add_gpm_wait_descriptor_called_flag
= 1;
7261 FD_SET (desc
, &input_wait_mask
);
7262 FD_SET (desc
, &gpm_wait_mask
);
7263 if (desc
> max_gpm_desc
)
7264 max_gpm_desc
= desc
;
7267 /* From now on, do not expect DESC to give keyboard input. */
7270 delete_keyboard_wait_descriptor (desc
)
7274 int lim
= max_keyboard_desc
;
7276 FD_CLR (desc
, &input_wait_mask
);
7277 FD_CLR (desc
, &non_process_wait_mask
);
7279 if (desc
== max_keyboard_desc
)
7280 for (fd
= 0; fd
< lim
; fd
++)
7281 if (FD_ISSET (fd
, &input_wait_mask
)
7282 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7283 && !FD_ISSET (fd
, &gpm_wait_mask
))
7284 max_keyboard_desc
= fd
;
7288 delete_gpm_wait_descriptor (desc
)
7292 int lim
= max_gpm_desc
;
7294 FD_CLR (desc
, &input_wait_mask
);
7295 FD_CLR (desc
, &non_process_wait_mask
);
7297 if (desc
== max_gpm_desc
)
7298 for (fd
= 0; fd
< lim
; fd
++)
7299 if (FD_ISSET (fd
, &input_wait_mask
)
7300 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7301 && !FD_ISSET (fd
, &non_process_wait_mask
))
7305 /* Return nonzero if *MASK has a bit set
7306 that corresponds to one of the keyboard input descriptors. */
7309 keyboard_bit_set (mask
)
7314 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
7315 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
7316 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
7327 inhibit_sentinels
= 0;
7331 if (! noninteractive
|| initialized
)
7333 signal (SIGCHLD
, sigchld_handler
);
7336 FD_ZERO (&input_wait_mask
);
7337 FD_ZERO (&non_keyboard_wait_mask
);
7338 FD_ZERO (&non_process_wait_mask
);
7339 max_process_desc
= 0;
7341 #ifdef NON_BLOCKING_CONNECT
7342 FD_ZERO (&connect_wait_mask
);
7343 num_pending_connects
= 0;
7346 #ifdef ADAPTIVE_READ_BUFFERING
7347 process_output_delay_count
= 0;
7348 process_output_skip
= 0;
7351 /* Don't do this, it caused infinite select loops. The display
7352 method should call add_keyboard_wait_descriptor on stdin if it
7355 FD_SET (0, &input_wait_mask
);
7358 Vprocess_alist
= Qnil
;
7360 deleted_pid_list
= Qnil
;
7362 for (i
= 0; i
< MAXDESC
; i
++)
7364 chan_process
[i
] = Qnil
;
7365 proc_buffered_char
[i
] = -1;
7367 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
7368 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
7369 #ifdef DATAGRAM_SOCKETS
7370 bzero (datagram_address
, sizeof datagram_address
);
7375 Lisp_Object subfeatures
= Qnil
;
7376 struct socket_options
*sopt
;
7378 #define ADD_SUBFEATURE(key, val) \
7379 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7381 #ifdef NON_BLOCKING_CONNECT
7382 ADD_SUBFEATURE (QCnowait
, Qt
);
7384 #ifdef DATAGRAM_SOCKETS
7385 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7387 #ifdef HAVE_LOCAL_SOCKETS
7388 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7390 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7392 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7394 #ifdef HAVE_GETSOCKNAME
7395 ADD_SUBFEATURE (QCservice
, Qt
);
7397 #if !defined(TERM) && (defined(O_NONBLOCK) || defined(O_NDELAY))
7398 ADD_SUBFEATURE (QCserver
, Qt
);
7401 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7402 subfeatures
= Fcons (intern (sopt
->name
), subfeatures
);
7404 Fprovide (intern ("make-network-process"), subfeatures
);
7406 #endif /* HAVE_SOCKETS */
7408 #if defined (DARWIN) || defined (MAC_OSX)
7409 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7410 processes. As such, we only change the default value. */
7413 char *release
= get_operating_system_release();
7414 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7415 && release
[1] == '.')) {
7416 Vprocess_connection_type
= Qnil
;
7425 Qprocessp
= intern ("processp");
7426 staticpro (&Qprocessp
);
7427 Qrun
= intern ("run");
7429 Qstop
= intern ("stop");
7431 Qsignal
= intern ("signal");
7432 staticpro (&Qsignal
);
7434 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7437 Qexit = intern ("exit");
7438 staticpro (&Qexit); */
7440 Qopen
= intern ("open");
7442 Qclosed
= intern ("closed");
7443 staticpro (&Qclosed
);
7444 Qconnect
= intern ("connect");
7445 staticpro (&Qconnect
);
7446 Qfailed
= intern ("failed");
7447 staticpro (&Qfailed
);
7448 Qlisten
= intern ("listen");
7449 staticpro (&Qlisten
);
7450 Qlocal
= intern ("local");
7451 staticpro (&Qlocal
);
7452 Qipv4
= intern ("ipv4");
7455 Qipv6
= intern ("ipv6");
7458 Qdatagram
= intern ("datagram");
7459 staticpro (&Qdatagram
);
7461 QCport
= intern (":port");
7462 staticpro (&QCport
);
7463 QCspeed
= intern (":speed");
7464 staticpro (&QCspeed
);
7465 QCprocess
= intern (":process");
7466 staticpro (&QCprocess
);
7468 QCbytesize
= intern (":bytesize");
7469 staticpro (&QCbytesize
);
7470 QCstopbits
= intern (":stopbits");
7471 staticpro (&QCstopbits
);
7472 QCparity
= intern (":parity");
7473 staticpro (&QCparity
);
7474 Qodd
= intern ("odd");
7476 Qeven
= intern ("even");
7478 QCflowcontrol
= intern (":flowcontrol");
7479 staticpro (&QCflowcontrol
);
7480 Qhw
= intern ("hw");
7482 Qsw
= intern ("sw");
7484 QCsummary
= intern (":summary");
7485 staticpro (&QCsummary
);
7487 Qreal
= intern ("real");
7489 Qnetwork
= intern ("network");
7490 staticpro (&Qnetwork
);
7491 Qserial
= intern ("serial");
7492 staticpro (&Qserial
);
7494 QCname
= intern (":name");
7495 staticpro (&QCname
);
7496 QCbuffer
= intern (":buffer");
7497 staticpro (&QCbuffer
);
7498 QChost
= intern (":host");
7499 staticpro (&QChost
);
7500 QCservice
= intern (":service");
7501 staticpro (&QCservice
);
7502 QCtype
= intern (":type");
7503 staticpro (&QCtype
);
7504 QClocal
= intern (":local");
7505 staticpro (&QClocal
);
7506 QCremote
= intern (":remote");
7507 staticpro (&QCremote
);
7508 QCcoding
= intern (":coding");
7509 staticpro (&QCcoding
);
7510 QCserver
= intern (":server");
7511 staticpro (&QCserver
);
7512 QCnowait
= intern (":nowait");
7513 staticpro (&QCnowait
);
7514 QCsentinel
= intern (":sentinel");
7515 staticpro (&QCsentinel
);
7516 QClog
= intern (":log");
7518 QCnoquery
= intern (":noquery");
7519 staticpro (&QCnoquery
);
7520 QCstop
= intern (":stop");
7521 staticpro (&QCstop
);
7522 QCoptions
= intern (":options");
7523 staticpro (&QCoptions
);
7524 QCplist
= intern (":plist");
7525 staticpro (&QCplist
);
7527 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
7528 staticpro (&Qlast_nonmenu_event
);
7530 staticpro (&Vprocess_alist
);
7532 staticpro (&deleted_pid_list
);
7535 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7536 doc
: /* *Non-nil means delete processes immediately when they exit.
7537 A value of nil means don't delete them until `list-processes' is run. */);
7539 delete_exited_processes
= 1;
7541 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7542 doc
: /* Control type of device used to communicate with subprocesses.
7543 Values are nil to use a pipe, or t or `pty' to use a pty.
7544 The value has no effect if the system has no ptys or if all ptys are busy:
7545 then a pipe is used in any case.
7546 The value takes effect when `start-process' is called. */);
7547 Vprocess_connection_type
= Qt
;
7549 #ifdef ADAPTIVE_READ_BUFFERING
7550 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7551 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7552 On some systems, when Emacs reads the output from a subprocess, the output data
7553 is read in very small blocks, potentially resulting in very poor performance.
7554 This behavior can be remedied to some extent by setting this variable to a
7555 non-nil value, as it will automatically delay reading from such processes, to
7556 allow them to produce more output before Emacs tries to read it.
7557 If the value is t, the delay is reset after each write to the process; any other
7558 non-nil value means that the delay is not reset on write.
7559 The variable takes effect when `start-process' is called. */);
7560 Vprocess_adaptive_read_buffering
= Qt
;
7563 defsubr (&Sprocessp
);
7564 defsubr (&Sget_process
);
7565 defsubr (&Sget_buffer_process
);
7566 defsubr (&Sdelete_process
);
7567 defsubr (&Sprocess_status
);
7568 defsubr (&Sprocess_exit_status
);
7569 defsubr (&Sprocess_id
);
7570 defsubr (&Sprocess_name
);
7571 defsubr (&Sprocess_tty_name
);
7572 defsubr (&Sprocess_command
);
7573 defsubr (&Sset_process_buffer
);
7574 defsubr (&Sprocess_buffer
);
7575 defsubr (&Sprocess_mark
);
7576 defsubr (&Sset_process_filter
);
7577 defsubr (&Sprocess_filter
);
7578 defsubr (&Sset_process_sentinel
);
7579 defsubr (&Sprocess_sentinel
);
7580 defsubr (&Sset_process_window_size
);
7581 defsubr (&Sset_process_inherit_coding_system_flag
);
7582 defsubr (&Sprocess_inherit_coding_system_flag
);
7583 defsubr (&Sset_process_query_on_exit_flag
);
7584 defsubr (&Sprocess_query_on_exit_flag
);
7585 defsubr (&Sprocess_contact
);
7586 defsubr (&Sprocess_plist
);
7587 defsubr (&Sset_process_plist
);
7588 defsubr (&Slist_processes
);
7589 defsubr (&Sprocess_list
);
7590 defsubr (&Sstart_process
);
7592 defsubr (&Sserial_process_configure
);
7593 defsubr (&Smake_serial_process
);
7594 #endif /* HAVE_SERIAL */
7596 defsubr (&Sset_network_process_option
);
7597 defsubr (&Smake_network_process
);
7598 defsubr (&Sformat_network_address
);
7599 #endif /* HAVE_SOCKETS */
7600 #if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7602 defsubr (&Snetwork_interface_list
);
7604 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7605 defsubr (&Snetwork_interface_info
);
7607 #endif /* HAVE_SOCKETS ... */
7608 #ifdef DATAGRAM_SOCKETS
7609 defsubr (&Sprocess_datagram_address
);
7610 defsubr (&Sset_process_datagram_address
);
7612 defsubr (&Saccept_process_output
);
7613 defsubr (&Sprocess_send_region
);
7614 defsubr (&Sprocess_send_string
);
7615 defsubr (&Sinterrupt_process
);
7616 defsubr (&Skill_process
);
7617 defsubr (&Squit_process
);
7618 defsubr (&Sstop_process
);
7619 defsubr (&Scontinue_process
);
7620 defsubr (&Sprocess_running_child_p
);
7621 defsubr (&Sprocess_send_eof
);
7622 defsubr (&Ssignal_process
);
7623 defsubr (&Swaiting_for_user_input_p
);
7624 defsubr (&Sprocess_type
);
7625 defsubr (&Sset_process_coding_system
);
7626 defsubr (&Sprocess_coding_system
);
7627 defsubr (&Sset_process_filter_multibyte
);
7628 defsubr (&Sprocess_filter_multibyte_p
);
7632 #else /* not subprocesses */
7634 #include <sys/types.h>
7638 #include "systime.h"
7639 #include "character.h"
7641 #include "termopts.h"
7642 #include "sysselect.h"
7644 extern int frame_garbaged
;
7646 extern EMACS_TIME
timer_check ();
7647 extern int timers_run
;
7651 /* As described above, except assuming that there are no subprocesses:
7653 Wait for timeout to elapse and/or keyboard input to be available.
7656 timeout in seconds, or
7657 zero for no limit, or
7658 -1 means gobble data immediately available but don't wait for any.
7660 read_kbd is a Lisp_Object:
7661 0 to ignore keyboard input, or
7662 1 to return when input is available, or
7663 -1 means caller will actually read the input, so don't throw to
7666 see full version for other parameters. We know that wait_proc will
7667 always be NULL, since `subprocesses' isn't defined.
7669 do_display != 0 means redisplay should be done to show subprocess
7670 output that arrives.
7672 Return true if we received input from any process. */
7675 wait_reading_process_output (time_limit
, microsecs
, read_kbd
, do_display
,
7676 wait_for_cell
, wait_proc
, just_wait_proc
)
7677 int time_limit
, microsecs
, read_kbd
, do_display
;
7678 Lisp_Object wait_for_cell
;
7679 struct Lisp_Process
*wait_proc
;
7683 EMACS_TIME end_time
, timeout
;
7684 SELECT_TYPE waitchannels
;
7687 /* What does time_limit really mean? */
7688 if (time_limit
|| microsecs
)
7690 EMACS_GET_TIME (end_time
);
7691 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
7692 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
7695 /* Turn off periodic alarms (in case they are in use)
7696 and then turn off any other atimers,
7697 because the select emulator uses alarms. */
7699 turn_on_atimers (0);
7703 int timeout_reduced_for_timers
= 0;
7705 /* If calling from keyboard input, do not quit
7706 since we want to return C-g as an input character.
7707 Otherwise, do pending quit if requested. */
7711 /* Exit now if the cell we're waiting for became non-nil. */
7712 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7715 /* Compute time from now till when time limit is up */
7716 /* Exit if already run out */
7717 if (time_limit
== -1)
7719 /* -1 specified for timeout means
7720 gobble output available now
7721 but don't wait at all. */
7723 EMACS_SET_SECS_USECS (timeout
, 0, 0);
7725 else if (time_limit
|| microsecs
)
7727 EMACS_GET_TIME (timeout
);
7728 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
7729 if (EMACS_TIME_NEG_P (timeout
))
7734 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
7737 /* If our caller will not immediately handle keyboard events,
7738 run timer events directly.
7739 (Callers that will immediately read keyboard events
7740 call timer_delay on their own.) */
7741 if (NILP (wait_for_cell
))
7743 EMACS_TIME timer_delay
;
7747 int old_timers_run
= timers_run
;
7748 timer_delay
= timer_check (1);
7749 if (timers_run
!= old_timers_run
&& do_display
)
7750 /* We must retry, since a timer may have requeued itself
7751 and that could alter the time delay. */
7752 redisplay_preserve_echo_area (14);
7756 while (!detect_input_pending ());
7758 /* If there is unread keyboard input, also return. */
7760 && requeued_events_pending_p ())
7763 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
7765 EMACS_TIME difference
;
7766 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
7767 if (EMACS_TIME_NEG_P (difference
))
7769 timeout
= timer_delay
;
7770 timeout_reduced_for_timers
= 1;
7775 /* Cause C-g and alarm signals to take immediate action,
7776 and cause input available signals to zero out timeout. */
7778 set_waiting_for_input (&timeout
);
7780 /* Wait till there is something to do. */
7782 if (! read_kbd
&& NILP (wait_for_cell
))
7783 FD_ZERO (&waitchannels
);
7785 FD_SET (0, &waitchannels
);
7787 /* If a frame has been newly mapped and needs updating,
7788 reprocess its display stuff. */
7789 if (frame_garbaged
&& do_display
)
7791 clear_waiting_for_input ();
7792 redisplay_preserve_echo_area (15);
7794 set_waiting_for_input (&timeout
);
7797 if (read_kbd
&& detect_input_pending ())
7800 FD_ZERO (&waitchannels
);
7803 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
7808 /* Make C-g and alarm signals set flags again */
7809 clear_waiting_for_input ();
7811 /* If we woke up due to SIGWINCH, actually change size now. */
7812 do_pending_window_change (0);
7814 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7815 /* We waited the full specified time, so return now. */
7820 /* If the system call was interrupted, then go around the
7822 if (xerrno
== EINTR
)
7823 FD_ZERO (&waitchannels
);
7825 error ("select error: %s", emacs_strerror (xerrno
));
7828 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
7829 /* System sometimes fails to deliver SIGIO. */
7830 kill (getpid (), SIGIO
);
7833 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
7834 kill (getpid (), SIGIO
);
7837 /* Check for keyboard input */
7840 && detect_input_pending_run_timers (do_display
))
7842 swallow_events (do_display
);
7843 if (detect_input_pending_run_timers (do_display
))
7847 /* If there is unread keyboard input, also return. */
7849 && requeued_events_pending_p ())
7852 /* If wait_for_cell. check for keyboard input
7853 but don't run any timers.
7854 ??? (It seems wrong to me to check for keyboard
7855 input at all when wait_for_cell, but the code
7856 has been this way since July 1994.
7857 Try changing this after version 19.31.) */
7858 if (! NILP (wait_for_cell
)
7859 && detect_input_pending ())
7861 swallow_events (do_display
);
7862 if (detect_input_pending ())
7866 /* Exit now if the cell we're waiting for became non-nil. */
7867 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7877 /* Don't confuse make-docfile by having two doc strings for this function.
7878 make-docfile does not pay attention to #if, for good reason! */
7879 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7882 register Lisp_Object name
;
7887 /* Don't confuse make-docfile by having two doc strings for this function.
7888 make-docfile does not pay attention to #if, for good reason! */
7889 DEFUN ("process-inherit-coding-system-flag",
7890 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7894 register Lisp_Object process
;
7896 /* Ignore the argument and return the value of
7897 inherit-process-coding-system. */
7898 return inherit_process_coding_system
? Qt
: Qnil
;
7901 /* Kill all processes associated with `buffer'.
7902 If `buffer' is nil, kill all processes.
7903 Since we have no subprocesses, this does nothing. */
7906 kill_buffer_processes (buffer
)
7919 QCtype
= intern (":type");
7920 staticpro (&QCtype
);
7922 defsubr (&Sget_buffer_process
);
7923 defsubr (&Sprocess_inherit_coding_system_flag
);
7927 #endif /* not subprocesses */
7929 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7930 (do not change this comment) */