1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h> /* some typedefs are used in sys/file.h */
31 #ifdef HAVE_INTTYPES_H
43 /* Only MS-DOS does not define `subprocesses'. */
46 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 /* Are local (unix) sockets supported? */
52 #if defined (HAVE_SYS_UN_H)
53 #if !defined (AF_LOCAL) && defined (AF_UNIX)
54 #define AF_LOCAL AF_UNIX
57 #define HAVE_LOCAL_SOCKETS
62 #if defined(HAVE_SYS_IOCTL_H)
63 #include <sys/ioctl.h>
64 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
66 #endif /* HAVE_PTYS and no O_NDELAY */
67 #if defined(HAVE_NET_IF_H)
69 #endif /* HAVE_NET_IF_H */
70 #endif /* HAVE_SYS_IOCTL_H */
81 #include <netinet/in.h>
82 #include <arpa/nameser.h>
90 #endif /* subprocesses */
98 #include "character.h"
102 #include "termhooks.h"
103 #include "termopts.h"
104 #include "commands.h"
105 #include "keyboard.h"
106 #include "blockinput.h"
107 #include "dispextern.h"
108 #include "composite.h"
110 #include "sysselect.h"
111 #include "syssignal.h"
114 #if defined (USE_GTK) || defined (HAVE_GCONF)
115 #include "xgselect.h"
116 #endif /* defined (USE_GTK) || defined (HAVE_GCONF) */
120 extern int timers_run
;
122 Lisp_Object Qeuid
, Qegid
, Qcomm
, Qstate
, Qppid
, Qpgrp
, Qsess
, Qttname
, Qtpgid
;
123 Lisp_Object Qminflt
, Qmajflt
, Qcminflt
, Qcmajflt
, Qutime
, Qstime
, Qcstime
;
124 Lisp_Object Qcutime
, Qpri
, Qnice
, Qthcount
, Qstart
, Qvsize
, Qrss
, Qargs
;
125 Lisp_Object Quser
, Qgroup
, Qetime
, Qpcpu
, Qpmem
, Qtime
, Qctime
;
126 Lisp_Object QCname
, QCtype
;
128 /* Non-zero if keyboard input is on hold, zero otherwise. */
130 static int kbd_is_on_hold
;
132 /* Nonzero means delete a process right away if it exits. */
133 static int delete_exited_processes
;
135 /* Nonzero means don't run process sentinels. This is used
137 int inhibit_sentinels
;
141 Lisp_Object Qprocessp
;
142 Lisp_Object Qrun
, Qstop
, Qsignal
;
143 Lisp_Object Qopen
, Qclosed
, Qconnect
, Qfailed
, Qlisten
;
144 Lisp_Object Qlocal
, Qipv4
, Qdatagram
, Qseqpacket
;
145 Lisp_Object Qreal
, Qnetwork
, Qserial
;
149 Lisp_Object QCport
, QCspeed
, QCprocess
;
150 Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
151 Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
152 Lisp_Object QCbuffer
, QChost
, QCservice
;
153 Lisp_Object QClocal
, QCremote
, QCcoding
;
154 Lisp_Object QCserver
, QCnowait
, QCnoquery
, QCstop
;
155 Lisp_Object QCsentinel
, QClog
, QCoptions
, QCplist
;
156 Lisp_Object Qlast_nonmenu_event
;
157 /* QCfamily is declared and initialized in xfaces.c,
158 QCfilter in keyboard.c. */
159 extern Lisp_Object QCfamily
, QCfilter
;
161 /* Qexit is declared and initialized in eval.c. */
163 /* QCfamily is defined in xfaces.c. */
164 extern Lisp_Object QCfamily
;
165 /* QCfilter is defined in keyboard.c. */
166 extern Lisp_Object QCfilter
;
168 #define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
169 #define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
170 #define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
171 #define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
173 /* Define first descriptor number available for subprocesses. */
174 #define FIRST_PROC_DESC 3
176 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
179 #if !defined (SIGCHLD) && defined (SIGCLD)
180 #define SIGCHLD SIGCLD
183 extern char *get_operating_system_release (void);
185 /* Serial processes require termios or Windows. */
186 #if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
191 /* From sysdep.c or w32.c */
192 extern int serial_open (char *port
);
193 extern void serial_configure (struct Lisp_Process
*p
, Lisp_Object contact
);
200 /* t means use pty, nil means use a pipe,
201 maybe other values to come. */
202 static Lisp_Object Vprocess_connection_type
;
204 /* These next two vars are non-static since sysdep.c uses them in the
205 emulation of `select'. */
206 /* Number of events of change of status of a process. */
208 /* Number of events for which the user or sentinel has been notified. */
211 /* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
213 #ifdef BROKEN_NON_BLOCKING_CONNECT
214 #undef NON_BLOCKING_CONNECT
216 #ifndef NON_BLOCKING_CONNECT
218 #if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
219 #if defined (O_NONBLOCK) || defined (O_NDELAY)
220 #if defined (EWOULDBLOCK) || defined (EINPROGRESS)
221 #define NON_BLOCKING_CONNECT
222 #endif /* EWOULDBLOCK || EINPROGRESS */
223 #endif /* O_NONBLOCK || O_NDELAY */
224 #endif /* HAVE_GETPEERNAME || GNU_LINUX */
225 #endif /* HAVE_SELECT */
226 #endif /* NON_BLOCKING_CONNECT */
227 #endif /* BROKEN_NON_BLOCKING_CONNECT */
229 /* Define DATAGRAM_SOCKETS if datagrams can be used safely on
230 this system. We need to read full packets, so we need a
231 "non-destructive" select. So we require either native select,
232 or emulation of select using FIONREAD. */
234 #ifdef BROKEN_DATAGRAM_SOCKETS
235 #undef DATAGRAM_SOCKETS
237 #ifndef DATAGRAM_SOCKETS
238 #if defined (HAVE_SELECT) || defined (FIONREAD)
239 #if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
240 #define DATAGRAM_SOCKETS
241 #endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
242 #endif /* HAVE_SELECT || FIONREAD */
243 #endif /* DATAGRAM_SOCKETS */
244 #endif /* BROKEN_DATAGRAM_SOCKETS */
246 #if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
247 # define HAVE_SEQPACKET
250 #if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
251 #ifdef EMACS_HAS_USECS
252 #define ADAPTIVE_READ_BUFFERING
256 #ifdef ADAPTIVE_READ_BUFFERING
257 #define READ_OUTPUT_DELAY_INCREMENT 10000
258 #define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
259 #define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
261 /* Number of processes which have a non-zero read_output_delay,
262 and therefore might be delayed for adaptive read buffering. */
264 static int process_output_delay_count
;
266 /* Non-zero if any process has non-nil read_output_skip. */
268 static int process_output_skip
;
270 /* Non-nil means to delay reading process output to improve buffering.
271 A value of t means that delay is reset after each send, any other
272 non-nil value does not reset the delay. A value of nil disables
273 adaptive read buffering completely. */
274 static Lisp_Object Vprocess_adaptive_read_buffering
;
276 #define process_output_delay_count 0
279 static int keyboard_bit_set (SELECT_TYPE
*);
280 static void deactivate_process (Lisp_Object
);
281 static void status_notify (struct Lisp_Process
*);
282 static int read_process_output (Lisp_Object
, int);
283 static void create_pty (Lisp_Object
);
285 /* If we support a window system, turn on the code to poll periodically
286 to detect C-g. It isn't actually used when doing interrupt input. */
287 #ifdef HAVE_WINDOW_SYSTEM
288 #define POLL_FOR_INPUT
291 static Lisp_Object
get_process (register Lisp_Object name
);
292 static void exec_sentinel (Lisp_Object proc
, Lisp_Object reason
);
294 /* Mask of bits indicating the descriptors that we wait for input on. */
296 static SELECT_TYPE input_wait_mask
;
298 /* Mask that excludes keyboard input descriptor(s). */
300 static SELECT_TYPE non_keyboard_wait_mask
;
302 /* Mask that excludes process input descriptor(s). */
304 static SELECT_TYPE non_process_wait_mask
;
306 /* Mask for the gpm mouse input descriptor. */
308 static SELECT_TYPE gpm_wait_mask
;
310 #ifdef NON_BLOCKING_CONNECT
311 /* Mask of bits indicating the descriptors that we wait for connect to
312 complete on. Once they complete, they are removed from this mask
313 and added to the input_wait_mask and non_keyboard_wait_mask. */
315 static SELECT_TYPE connect_wait_mask
;
317 /* Number of bits set in connect_wait_mask. */
318 static int num_pending_connects
;
320 #define IF_NON_BLOCKING_CONNECT(s) s
321 #else /* NON_BLOCKING_CONNECT */
322 #define IF_NON_BLOCKING_CONNECT(s)
323 #endif /* NON_BLOCKING_CONNECT */
325 /* The largest descriptor currently in use for a process object. */
326 static int max_process_desc
;
328 /* The largest descriptor currently in use for keyboard input. */
329 static int max_keyboard_desc
;
331 /* The largest descriptor currently in use for gpm mouse input. */
332 static int max_gpm_desc
;
334 /* Indexed by descriptor, gives the process (if any) for that descriptor */
335 Lisp_Object chan_process
[MAXDESC
];
337 /* Alist of elements (NAME . PROCESS) */
338 Lisp_Object Vprocess_alist
;
340 /* Buffered-ahead input char from process, indexed by channel.
341 -1 means empty (no char is buffered).
342 Used on sys V where the only way to tell if there is any
343 output from the process is to read at least one char.
344 Always -1 on systems that support FIONREAD. */
346 /* Don't make static; need to access externally. */
347 int proc_buffered_char
[MAXDESC
];
349 /* Table of `struct coding-system' for each process. */
350 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
351 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
353 #ifdef DATAGRAM_SOCKETS
354 /* Table of `partner address' for datagram sockets. */
355 struct sockaddr_and_len
{
358 } datagram_address
[MAXDESC
];
359 #define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
360 #define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
362 #define DATAGRAM_CHAN_P(chan) (0)
363 #define DATAGRAM_CONN_P(proc) (0)
366 /* Maximum number of bytes to send to a pty without an eof. */
367 static int pty_max_bytes
;
373 /* The file name of the pty opened by allocate_pty. */
375 static char pty_name
[24];
378 /* Compute the Lisp form of the process status, p->status, from
379 the numeric status that was returned by `wait'. */
381 static Lisp_Object
status_convert (int);
384 update_status (struct Lisp_Process
*p
)
386 eassert (p
->raw_status_new
);
387 p
->status
= status_convert (p
->raw_status
);
388 p
->raw_status_new
= 0;
391 /* Convert a process status word in Unix format to
392 the list that we use internally. */
395 status_convert (int w
)
398 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
399 else if (WIFEXITED (w
))
400 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
401 WCOREDUMP (w
) ? Qt
: Qnil
));
402 else if (WIFSIGNALED (w
))
403 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
404 WCOREDUMP (w
) ? Qt
: Qnil
));
409 /* Given a status-list, extract the three pieces of information
410 and store them individually through the three pointers. */
413 decode_status (Lisp_Object l
, Lisp_Object
*symbol
, int *code
, int *coredump
)
427 *code
= XFASTINT (XCAR (tem
));
429 *coredump
= !NILP (tem
);
433 /* Return a string describing a process status list. */
436 status_message (struct Lisp_Process
*p
)
438 Lisp_Object status
= p
->status
;
441 Lisp_Object string
, string2
;
443 decode_status (status
, &symbol
, &code
, &coredump
);
445 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
448 synchronize_system_messages_locale ();
449 signame
= strsignal (code
);
451 string
= build_string ("unknown");
456 string
= make_unibyte_string (signame
, strlen (signame
));
457 if (! NILP (Vlocale_coding_system
))
458 string
= (code_convert_string_norecord
459 (string
, Vlocale_coding_system
, 0));
460 c1
= STRING_CHAR ((char *) SDATA (string
));
463 Faset (string
, make_number (0), make_number (c2
));
465 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
466 return concat2 (string
, string2
);
468 else if (EQ (symbol
, Qexit
))
471 return build_string (code
== 0 ? "deleted\n" : "connection broken by remote peer\n");
473 return build_string ("finished\n");
474 string
= Fnumber_to_string (make_number (code
));
475 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
476 return concat3 (build_string ("exited abnormally with code "),
479 else if (EQ (symbol
, Qfailed
))
481 string
= Fnumber_to_string (make_number (code
));
482 string2
= build_string ("\n");
483 return concat3 (build_string ("failed with code "),
487 return Fcopy_sequence (Fsymbol_name (symbol
));
492 /* Open an available pty, returning a file descriptor.
493 Return -1 on failure.
494 The file name of the terminal corresponding to the pty
495 is left in the variable pty_name. */
506 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
507 for (i
= 0; i
< 16; i
++)
510 struct stat stb
; /* Used in some PTY_OPEN. */
511 #ifdef PTY_NAME_SPRINTF
514 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
515 #endif /* no PTY_NAME_SPRINTF */
519 #else /* no PTY_OPEN */
521 { /* Some systems name their pseudoterminals so that there are gaps in
522 the usual sequence - for example, on HP9000/S700 systems, there
523 are no pseudoterminals with names ending in 'f'. So we wait for
524 three failures in a row before deciding that we've reached the
526 int failed_count
= 0;
528 if (stat (pty_name
, &stb
) < 0)
531 if (failed_count
>= 3)
538 fd
= emacs_open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
540 fd
= emacs_open (pty_name
, O_RDWR
| O_NDELAY
, 0);
543 #endif /* no PTY_OPEN */
547 /* check to make certain that both sides are available
548 this avoids a nasty yet stupid bug in rlogins */
549 #ifdef PTY_TTY_NAME_SPRINTF
552 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
553 #endif /* no PTY_TTY_NAME_SPRINTF */
554 if (access (pty_name
, 6) != 0)
569 #endif /* HAVE_PTYS */
572 make_process (Lisp_Object name
)
574 register Lisp_Object val
, tem
, name1
;
575 register struct Lisp_Process
*p
;
579 p
= allocate_process ();
587 p
->raw_status_new
= 0;
589 p
->mark
= Fmake_marker ();
590 p
->kill_without_query
= 0;
592 #ifdef ADAPTIVE_READ_BUFFERING
593 p
->adaptive_read_buffering
= 0;
594 p
->read_output_delay
= 0;
595 p
->read_output_skip
= 0;
598 /* If name is already in use, modify it until it is unused. */
603 tem
= Fget_process (name1
);
604 if (NILP (tem
)) break;
605 sprintf (suffix
, "<%d>", i
);
606 name1
= concat2 (name
, build_string (suffix
));
610 XSETPROCESS (val
, p
);
611 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
616 remove_process (register Lisp_Object proc
)
618 register Lisp_Object pair
;
620 pair
= Frassq (proc
, Vprocess_alist
);
621 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
623 deactivate_process (proc
);
627 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
628 doc
: /* Return t if OBJECT is a process. */)
631 return PROCESSP (object
) ? Qt
: Qnil
;
634 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
635 doc
: /* Return the process named NAME, or nil if there is none. */)
636 (register Lisp_Object name
)
641 return Fcdr (Fassoc (name
, Vprocess_alist
));
644 /* This is how commands for the user decode process arguments. It
645 accepts a process, a process name, a buffer, a buffer name, or nil.
646 Buffers denote the first process in the buffer, and nil denotes the
650 get_process (register Lisp_Object name
)
652 register Lisp_Object proc
, obj
;
655 obj
= Fget_process (name
);
657 obj
= Fget_buffer (name
);
659 error ("Process %s does not exist", SDATA (name
));
661 else if (NILP (name
))
662 obj
= Fcurrent_buffer ();
666 /* Now obj should be either a buffer object or a process object.
670 proc
= Fget_buffer_process (obj
);
672 error ("Buffer %s has no process", SDATA (XBUFFER (obj
)->name
));
684 /* Fdelete_process promises to immediately forget about the process, but in
685 reality, Emacs needs to remember those processes until they have been
686 treated by sigchld_handler; otherwise this handler would consider the
687 process as being synchronous and say that the synchronous process is
689 static Lisp_Object deleted_pid_list
;
692 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
693 doc
: /* Delete PROCESS: kill it and forget about it immediately.
694 PROCESS may be a process, a buffer, the name of a process or buffer, or
695 nil, indicating the current buffer's process. */)
696 (register Lisp_Object process
)
698 register struct Lisp_Process
*p
;
700 process
= get_process (process
);
701 p
= XPROCESS (process
);
703 p
->raw_status_new
= 0;
704 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
706 p
->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
707 p
->tick
= ++process_tick
;
709 redisplay_preserve_echo_area (13);
711 else if (p
->infd
>= 0)
715 /* Assignment to EMACS_INT stops GCC whining about limited range
717 EMACS_INT pid
= p
->pid
;
719 /* No problem storing the pid here, as it is still in Vprocess_alist. */
720 deleted_pid_list
= Fcons (make_fixnum_or_float (pid
),
721 /* GC treated elements set to nil. */
722 Fdelq (Qnil
, deleted_pid_list
));
723 /* If the process has already signaled, remove it from the list. */
724 if (p
->raw_status_new
)
727 if (CONSP (p
->status
))
728 symbol
= XCAR (p
->status
);
729 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
731 = Fdelete (make_fixnum_or_float (pid
), deleted_pid_list
);
735 Fkill_process (process
, Qnil
);
736 /* Do this now, since remove_process will make sigchld_handler do nothing. */
738 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
739 p
->tick
= ++process_tick
;
741 redisplay_preserve_echo_area (13);
744 remove_process (process
);
748 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
749 doc
: /* Return the status of PROCESS.
750 The returned value is one of the following symbols:
751 run -- for a process that is running.
752 stop -- for a process stopped but continuable.
753 exit -- for a process that has exited.
754 signal -- for a process that has got a fatal signal.
755 open -- for a network stream connection that is open.
756 listen -- for a network stream server that is listening.
757 closed -- for a network stream connection that is closed.
758 connect -- when waiting for a non-blocking connection to complete.
759 failed -- when a non-blocking connection has failed.
760 nil -- if arg is a process name and no such process exists.
761 PROCESS may be a process, a buffer, the name of a process, or
762 nil, indicating the current buffer's process. */)
763 (register Lisp_Object process
)
765 register struct Lisp_Process
*p
;
766 register Lisp_Object status
;
768 if (STRINGP (process
))
769 process
= Fget_process (process
);
771 process
= get_process (process
);
776 p
= XPROCESS (process
);
777 if (p
->raw_status_new
)
781 status
= XCAR (status
);
782 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
784 if (EQ (status
, Qexit
))
786 else if (EQ (p
->command
, Qt
))
788 else if (EQ (status
, Qrun
))
794 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
796 doc
: /* Return the exit status of PROCESS or the signal number that killed it.
797 If PROCESS has not yet exited or died, return 0. */)
798 (register Lisp_Object process
)
800 CHECK_PROCESS (process
);
801 if (XPROCESS (process
)->raw_status_new
)
802 update_status (XPROCESS (process
));
803 if (CONSP (XPROCESS (process
)->status
))
804 return XCAR (XCDR (XPROCESS (process
)->status
));
805 return make_number (0);
808 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
809 doc
: /* Return the process id of PROCESS.
810 This is the pid of the external process which PROCESS uses or talks to.
811 For a network connection, this value is nil. */)
812 (register Lisp_Object process
)
814 /* Assignment to EMACS_INT stops GCC whining about limited range of
818 CHECK_PROCESS (process
);
819 pid
= XPROCESS (process
)->pid
;
820 return (pid
? make_fixnum_or_float (pid
) : Qnil
);
823 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
824 doc
: /* Return the name of PROCESS, as a string.
825 This is the name of the program invoked in PROCESS,
826 possibly modified to make it unique among process names. */)
827 (register Lisp_Object process
)
829 CHECK_PROCESS (process
);
830 return XPROCESS (process
)->name
;
833 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
834 doc
: /* Return the command that was executed to start PROCESS.
835 This is a list of strings, the first string being the program executed
836 and the rest of the strings being the arguments given to it.
837 For a network or serial process, this is nil (process is running) or t
838 \(process is stopped). */)
839 (register Lisp_Object process
)
841 CHECK_PROCESS (process
);
842 return XPROCESS (process
)->command
;
845 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
846 doc
: /* Return the name of the terminal PROCESS uses, or nil if none.
847 This is the terminal that the process itself reads and writes on,
848 not the name of the pty that Emacs uses to talk with that terminal. */)
849 (register Lisp_Object process
)
851 CHECK_PROCESS (process
);
852 return XPROCESS (process
)->tty_name
;
855 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
857 doc
: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
858 (register Lisp_Object process
, Lisp_Object buffer
)
860 struct Lisp_Process
*p
;
862 CHECK_PROCESS (process
);
864 CHECK_BUFFER (buffer
);
865 p
= XPROCESS (process
);
867 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
868 p
->childp
= Fplist_put (p
->childp
, QCbuffer
, buffer
);
869 setup_process_coding_systems (process
);
873 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
875 doc
: /* Return the buffer PROCESS is associated with.
876 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
877 (register Lisp_Object process
)
879 CHECK_PROCESS (process
);
880 return XPROCESS (process
)->buffer
;
883 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
885 doc
: /* Return the marker for the end of the last output from PROCESS. */)
886 (register Lisp_Object process
)
888 CHECK_PROCESS (process
);
889 return XPROCESS (process
)->mark
;
892 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
894 doc
: /* Give PROCESS the filter function FILTER; nil means no filter.
895 A value of t means stop accepting output from the process.
897 When a process has a filter, its buffer is not used for output.
898 Instead, each time it does output, the entire string of output is
899 passed to the filter.
901 The filter gets two arguments: the process and the string of output.
902 The string argument is normally a multibyte string, except:
903 - if the process' input coding system is no-conversion or raw-text,
904 it is a unibyte string (the non-converted input), or else
905 - if `default-enable-multibyte-characters' is nil, it is a unibyte
906 string (the result of converting the decoded input multibyte
907 string to unibyte with `string-make-unibyte'). */)
908 (register Lisp_Object process
, Lisp_Object filter
)
910 struct Lisp_Process
*p
;
912 CHECK_PROCESS (process
);
913 p
= XPROCESS (process
);
915 /* Don't signal an error if the process' input file descriptor
916 is closed. This could make debugging Lisp more difficult,
917 for example when doing something like
919 (setq process (start-process ...))
921 (set-process-filter process ...) */
925 if (EQ (filter
, Qt
) && !EQ (p
->status
, Qlisten
))
927 FD_CLR (p
->infd
, &input_wait_mask
);
928 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
930 else if (EQ (p
->filter
, Qt
)
931 /* Network or serial process not stopped: */
932 && !EQ (p
->command
, Qt
))
934 FD_SET (p
->infd
, &input_wait_mask
);
935 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
940 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
941 p
->childp
= Fplist_put (p
->childp
, QCfilter
, filter
);
942 setup_process_coding_systems (process
);
946 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
948 doc
: /* Returns the filter function of PROCESS; nil if none.
949 See `set-process-filter' for more info on filter functions. */)
950 (register Lisp_Object process
)
952 CHECK_PROCESS (process
);
953 return XPROCESS (process
)->filter
;
956 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
958 doc
: /* Give PROCESS the sentinel SENTINEL; nil for none.
959 The sentinel is called as a function when the process changes state.
960 It gets two arguments: the process, and a string describing the change. */)
961 (register Lisp_Object process
, Lisp_Object sentinel
)
963 struct Lisp_Process
*p
;
965 CHECK_PROCESS (process
);
966 p
= XPROCESS (process
);
968 p
->sentinel
= sentinel
;
969 if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
970 p
->childp
= Fplist_put (p
->childp
, QCsentinel
, sentinel
);
974 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
976 doc
: /* Return the sentinel of PROCESS; nil if none.
977 See `set-process-sentinel' for more info on sentinels. */)
978 (register Lisp_Object process
)
980 CHECK_PROCESS (process
);
981 return XPROCESS (process
)->sentinel
;
984 DEFUN ("set-process-window-size", Fset_process_window_size
,
985 Sset_process_window_size
, 3, 3, 0,
986 doc
: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
987 (register Lisp_Object process
, Lisp_Object height
, Lisp_Object width
)
989 CHECK_PROCESS (process
);
990 CHECK_NATNUM (height
);
991 CHECK_NATNUM (width
);
993 if (XPROCESS (process
)->infd
< 0
994 || set_window_size (XPROCESS (process
)->infd
,
995 XINT (height
), XINT (width
)) <= 0)
1001 DEFUN ("set-process-inherit-coding-system-flag",
1002 Fset_process_inherit_coding_system_flag
,
1003 Sset_process_inherit_coding_system_flag
, 2, 2, 0,
1004 doc
: /* Determine whether buffer of PROCESS will inherit coding-system.
1005 If the second argument FLAG is non-nil, then the variable
1006 `buffer-file-coding-system' of the buffer associated with PROCESS
1007 will be bound to the value of the coding system used to decode
1010 This is useful when the coding system specified for the process buffer
1011 leaves either the character code conversion or the end-of-line conversion
1012 unspecified, or if the coding system used to decode the process output
1013 is more appropriate for saving the process buffer.
1015 Binding the variable `inherit-process-coding-system' to non-nil before
1016 starting the process is an alternative way of setting the inherit flag
1017 for the process which will run. */)
1018 (register Lisp_Object process
, Lisp_Object flag
)
1020 CHECK_PROCESS (process
);
1021 XPROCESS (process
)->inherit_coding_system_flag
= !NILP (flag
);
1025 DEFUN ("set-process-query-on-exit-flag",
1026 Fset_process_query_on_exit_flag
, Sset_process_query_on_exit_flag
,
1028 doc
: /* Specify if query is needed for PROCESS when Emacs is exited.
1029 If the second argument FLAG is non-nil, Emacs will query the user before
1030 exiting or killing a buffer if PROCESS is running. */)
1031 (register Lisp_Object process
, Lisp_Object flag
)
1033 CHECK_PROCESS (process
);
1034 XPROCESS (process
)->kill_without_query
= NILP (flag
);
1038 DEFUN ("process-query-on-exit-flag",
1039 Fprocess_query_on_exit_flag
, Sprocess_query_on_exit_flag
,
1041 doc
: /* Return the current value of query-on-exit flag for PROCESS. */)
1042 (register Lisp_Object process
)
1044 CHECK_PROCESS (process
);
1045 return (XPROCESS (process
)->kill_without_query
? Qnil
: Qt
);
1048 #ifdef DATAGRAM_SOCKETS
1049 Lisp_Object
Fprocess_datagram_address (Lisp_Object process
);
1052 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
1054 doc
: /* Return the contact info of PROCESS; t for a real child.
1055 For a network or serial connection, the value depends on the optional
1056 KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1057 SERVICE) for a network connection or (PORT SPEED) for a serial
1058 connection. If KEY is t, the complete contact information for the
1059 connection is returned, else the specific value for the keyword KEY is
1060 returned. See `make-network-process' or `make-serial-process' for a
1061 list of keywords. */)
1062 (register Lisp_Object process
, Lisp_Object key
)
1064 Lisp_Object contact
;
1066 CHECK_PROCESS (process
);
1067 contact
= XPROCESS (process
)->childp
;
1069 #ifdef DATAGRAM_SOCKETS
1070 if (DATAGRAM_CONN_P (process
)
1071 && (EQ (key
, Qt
) || EQ (key
, QCremote
)))
1072 contact
= Fplist_put (contact
, QCremote
,
1073 Fprocess_datagram_address (process
));
1076 if ((!NETCONN_P (process
) && !SERIALCONN_P (process
)) || EQ (key
, Qt
))
1078 if (NILP (key
) && NETCONN_P (process
))
1079 return Fcons (Fplist_get (contact
, QChost
),
1080 Fcons (Fplist_get (contact
, QCservice
), Qnil
));
1081 if (NILP (key
) && SERIALCONN_P (process
))
1082 return Fcons (Fplist_get (contact
, QCport
),
1083 Fcons (Fplist_get (contact
, QCspeed
), Qnil
));
1084 return Fplist_get (contact
, key
);
1087 DEFUN ("process-plist", Fprocess_plist
, Sprocess_plist
,
1089 doc
: /* Return the plist of PROCESS. */)
1090 (register Lisp_Object process
)
1092 CHECK_PROCESS (process
);
1093 return XPROCESS (process
)->plist
;
1096 DEFUN ("set-process-plist", Fset_process_plist
, Sset_process_plist
,
1098 doc
: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
1099 (register Lisp_Object process
, Lisp_Object plist
)
1101 CHECK_PROCESS (process
);
1104 XPROCESS (process
)->plist
= plist
;
1108 #if 0 /* Turned off because we don't currently record this info
1109 in the process. Perhaps add it. */
1110 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
1111 doc
: /* Return the connection type of PROCESS.
1112 The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1113 a socket connection. */)
1114 (Lisp_Object process
)
1116 return XPROCESS (process
)->type
;
1120 DEFUN ("process-type", Fprocess_type
, Sprocess_type
, 1, 1, 0,
1121 doc
: /* Return the connection type of PROCESS.
1122 The value is either the symbol `real', `network', or `serial'.
1123 PROCESS may be a process, a buffer, the name of a process or buffer, or
1124 nil, indicating the current buffer's process. */)
1125 (Lisp_Object process
)
1128 proc
= get_process (process
);
1129 return XPROCESS (proc
)->type
;
1132 DEFUN ("format-network-address", Fformat_network_address
, Sformat_network_address
,
1134 doc
: /* Convert network ADDRESS from internal format to a string.
1135 A 4 or 5 element vector represents an IPv4 address (with port number).
1136 An 8 or 9 element vector represents an IPv6 address (with port number).
1137 If optional second argument OMIT-PORT is non-nil, don't include a port
1138 number in the string, even when present in ADDRESS.
1139 Returns nil if format of ADDRESS is invalid. */)
1140 (Lisp_Object address
, Lisp_Object omit_port
)
1145 if (STRINGP (address
)) /* AF_LOCAL */
1148 if (VECTORP (address
)) /* AF_INET or AF_INET6 */
1150 register struct Lisp_Vector
*p
= XVECTOR (address
);
1151 Lisp_Object args
[10];
1154 if (p
->size
== 4 || (p
->size
== 5 && !NILP (omit_port
)))
1156 args
[0] = build_string ("%d.%d.%d.%d");
1159 else if (p
->size
== 5)
1161 args
[0] = build_string ("%d.%d.%d.%d:%d");
1164 else if (p
->size
== 8 || (p
->size
== 9 && !NILP (omit_port
)))
1166 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1169 else if (p
->size
== 9)
1171 args
[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1177 for (i
= 0; i
< nargs
; i
++)
1179 EMACS_INT element
= XINT (p
->contents
[i
]);
1181 if (element
< 0 || element
> 65535)
1184 if (nargs
<= 5 /* IPv4 */
1185 && i
< 4 /* host, not port */
1189 args
[i
+1] = p
->contents
[i
];
1192 return Fformat (nargs
+1, args
);
1195 if (CONSP (address
))
1197 Lisp_Object args
[2];
1198 args
[0] = build_string ("<Family %d>");
1199 args
[1] = Fcar (address
);
1200 return Fformat (2, args
);
1207 list_processes_1 (Lisp_Object query_only
)
1209 register Lisp_Object tail
, tem
;
1210 Lisp_Object proc
, minspace
, tem1
;
1211 register struct Lisp_Process
*p
;
1213 int w_proc
, w_buffer
, w_tty
;
1215 Lisp_Object i_status
, i_buffer
, i_tty
, i_command
;
1217 w_proc
= 4; /* Proc */
1218 w_buffer
= 6; /* Buffer */
1219 w_tty
= 0; /* Omit if no ttys */
1221 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1225 proc
= Fcdr (XCAR (tail
));
1226 p
= XPROCESS (proc
);
1229 if (!NILP (query_only
) && p
->kill_without_query
)
1231 if (STRINGP (p
->name
)
1232 && ( i
= SCHARS (p
->name
), (i
> w_proc
)))
1234 if (!NILP (p
->buffer
))
1236 if (NILP (XBUFFER (p
->buffer
)->name
))
1239 w_buffer
= 8; /* (Killed) */
1241 else if ((i
= SCHARS (XBUFFER (p
->buffer
)->name
), (i
> w_buffer
)))
1244 if (STRINGP (p
->tty_name
)
1245 && (i
= SCHARS (p
->tty_name
), (i
> w_tty
)))
1249 XSETFASTINT (i_status
, w_proc
+ 1);
1250 XSETFASTINT (i_buffer
, XFASTINT (i_status
) + 9);
1253 XSETFASTINT (i_tty
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1254 XSETFASTINT (i_command
, XFASTINT (i_tty
) + w_tty
+ 1);
1259 XSETFASTINT (i_command
, XFASTINT (i_buffer
) + w_buffer
+ 1);
1262 XSETFASTINT (minspace
, 1);
1264 set_buffer_internal (XBUFFER (Vstandard_output
));
1265 current_buffer
->undo_list
= Qt
;
1267 current_buffer
->truncate_lines
= Qt
;
1269 write_string ("Proc", -1);
1270 Findent_to (i_status
, minspace
); write_string ("Status", -1);
1271 Findent_to (i_buffer
, minspace
); write_string ("Buffer", -1);
1274 Findent_to (i_tty
, minspace
); write_string ("Tty", -1);
1276 Findent_to (i_command
, minspace
); write_string ("Command", -1);
1277 write_string ("\n", -1);
1279 write_string ("----", -1);
1280 Findent_to (i_status
, minspace
); write_string ("------", -1);
1281 Findent_to (i_buffer
, minspace
); write_string ("------", -1);
1284 Findent_to (i_tty
, minspace
); write_string ("---", -1);
1286 Findent_to (i_command
, minspace
); write_string ("-------", -1);
1287 write_string ("\n", -1);
1289 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
1293 proc
= Fcdr (XCAR (tail
));
1294 p
= XPROCESS (proc
);
1297 if (!NILP (query_only
) && p
->kill_without_query
)
1300 Finsert (1, &p
->name
);
1301 Findent_to (i_status
, minspace
);
1303 if (p
->raw_status_new
)
1306 if (CONSP (p
->status
))
1307 symbol
= XCAR (p
->status
);
1309 if (EQ (symbol
, Qsignal
))
1312 tem
= Fcar (Fcdr (p
->status
));
1313 Fprinc (symbol
, Qnil
);
1315 else if (NETCONN1_P (p
) || SERIALCONN1_P (p
))
1317 if (EQ (symbol
, Qexit
))
1318 write_string ("closed", -1);
1319 else if (EQ (p
->command
, Qt
))
1320 write_string ("stopped", -1);
1321 else if (EQ (symbol
, Qrun
))
1322 write_string ("open", -1);
1324 Fprinc (symbol
, Qnil
);
1326 else if (SERIALCONN1_P (p
))
1328 write_string ("running", -1);
1331 Fprinc (symbol
, Qnil
);
1333 if (EQ (symbol
, Qexit
))
1336 tem
= Fcar (Fcdr (p
->status
));
1339 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
1340 write_string (tembuf
, -1);
1344 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
) || EQ (symbol
, Qclosed
))
1347 Findent_to (i_buffer
, minspace
);
1348 if (NILP (p
->buffer
))
1349 insert_string ("(none)");
1350 else if (NILP (XBUFFER (p
->buffer
)->name
))
1351 insert_string ("(Killed)");
1353 Finsert (1, &XBUFFER (p
->buffer
)->name
);
1357 Findent_to (i_tty
, minspace
);
1358 if (STRINGP (p
->tty_name
))
1359 Finsert (1, &p
->tty_name
);
1362 Findent_to (i_command
, minspace
);
1364 if (EQ (p
->status
, Qlisten
))
1366 Lisp_Object port
= Fplist_get (p
->childp
, QCservice
);
1367 if (INTEGERP (port
))
1368 port
= Fnumber_to_string (port
);
1370 port
= Fformat_network_address (Fplist_get (p
->childp
, QClocal
), Qnil
);
1371 sprintf (tembuf
, "(network %s server on %s)\n",
1372 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1373 (STRINGP (port
) ? (char *)SDATA (port
) : "?"));
1374 insert_string (tembuf
);
1376 else if (NETCONN1_P (p
))
1378 /* For a local socket, there is no host name,
1379 so display service instead. */
1380 Lisp_Object host
= Fplist_get (p
->childp
, QChost
);
1381 if (!STRINGP (host
))
1383 host
= Fplist_get (p
->childp
, QCservice
);
1384 if (INTEGERP (host
))
1385 host
= Fnumber_to_string (host
);
1388 host
= Fformat_network_address (Fplist_get (p
->childp
, QCremote
), Qnil
);
1389 sprintf (tembuf
, "(network %s connection to %s)\n",
1390 (DATAGRAM_CHAN_P (p
->infd
) ? "datagram" : "stream"),
1391 (STRINGP (host
) ? (char *)SDATA (host
) : "?"));
1392 insert_string (tembuf
);
1394 else if (SERIALCONN1_P (p
))
1396 Lisp_Object port
= Fplist_get (p
->childp
, QCport
);
1397 Lisp_Object speed
= Fplist_get (p
->childp
, QCspeed
);
1398 insert_string ("(serial port ");
1400 insert_string (SDATA (port
));
1402 insert_string ("?");
1403 if (INTEGERP (speed
))
1405 sprintf (tembuf
, " at %ld b/s", (long) XINT (speed
));
1406 insert_string (tembuf
);
1408 insert_string (")\n");
1422 insert_string (" ");
1424 insert_string ("\n");
1429 status_notify (NULL
);
1430 redisplay_preserve_echo_area (13);
1435 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 1, "P",
1436 doc
: /* Display a list of all processes.
1437 If optional argument QUERY-ONLY is non-nil, only processes with
1438 the query-on-exit flag set will be listed.
1439 Any process listed as exited or signaled is actually eliminated
1440 after the listing is made. */)
1441 (Lisp_Object query_only
)
1443 internal_with_output_to_temp_buffer ("*Process List*",
1444 list_processes_1
, query_only
);
1448 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1449 doc
: /* Return a list of all processes. */)
1452 return Fmapcar (Qcdr
, Vprocess_alist
);
1455 /* Starting asynchronous inferior processes. */
1457 static Lisp_Object
start_process_unwind (Lisp_Object proc
);
1459 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1460 doc
: /* Start a program in a subprocess. Return the process object for it.
1461 NAME is name for process. It is modified if necessary to make it unique.
1462 BUFFER is the buffer (or buffer name) to associate with the process.
1464 Process output (both standard output and standard error streams) goes
1465 at end of BUFFER, unless you specify an output stream or filter
1466 function to handle the output. BUFFER may also be nil, meaning that
1467 this process is not associated with any buffer.
1469 PROGRAM is the program file name. It is searched for in PATH. If
1470 nil, just associate a pty with the buffer. Remaining arguments are
1471 strings to give program as arguments.
1473 If you want to separate standard output from standard error, invoke
1474 the command through a shell and redirect one of them using the shell
1477 usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1478 (int nargs
, register Lisp_Object
*args
)
1480 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1481 register unsigned char **new_argv
;
1483 int count
= SPECPDL_INDEX ();
1487 buffer
= Fget_buffer_create (buffer
);
1489 /* Make sure that the child will be able to chdir to the current
1490 buffer's current directory, or its unhandled equivalent. We
1491 can't just have the child check for an error when it does the
1492 chdir, since it's in a vfork.
1494 We have to GCPRO around this because Fexpand_file_name and
1495 Funhandled_file_name_directory might call a file name handling
1496 function. The argument list is protected by the caller, so all
1497 we really have to worry about is buffer. */
1499 struct gcpro gcpro1
, gcpro2
;
1501 current_dir
= current_buffer
->directory
;
1503 GCPRO2 (buffer
, current_dir
);
1505 current_dir
= Funhandled_file_name_directory (current_dir
);
1506 if (NILP (current_dir
))
1507 /* If the file name handler says that current_dir is unreachable, use
1508 a sensible default. */
1509 current_dir
= build_string ("~/");
1510 current_dir
= expand_and_dir_to_file (current_dir
, Qnil
);
1511 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1512 report_file_error ("Setting current directory",
1513 Fcons (current_buffer
->directory
, Qnil
));
1519 CHECK_STRING (name
);
1523 if (!NILP (program
))
1524 CHECK_STRING (program
);
1526 proc
= make_process (name
);
1527 /* If an error occurs and we can't start the process, we want to
1528 remove it from the process list. This means that each error
1529 check in create_process doesn't need to call remove_process
1530 itself; it's all taken care of here. */
1531 record_unwind_protect (start_process_unwind
, proc
);
1533 XPROCESS (proc
)->childp
= Qt
;
1534 XPROCESS (proc
)->plist
= Qnil
;
1535 XPROCESS (proc
)->type
= Qreal
;
1536 XPROCESS (proc
)->buffer
= buffer
;
1537 XPROCESS (proc
)->sentinel
= Qnil
;
1538 XPROCESS (proc
)->filter
= Qnil
;
1539 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1541 #ifdef ADAPTIVE_READ_BUFFERING
1542 XPROCESS (proc
)->adaptive_read_buffering
1543 = (NILP (Vprocess_adaptive_read_buffering
) ? 0
1544 : EQ (Vprocess_adaptive_read_buffering
, Qt
) ? 1 : 2);
1547 /* Make the process marker point into the process buffer (if any). */
1548 if (BUFFERP (buffer
))
1549 set_marker_both (XPROCESS (proc
)->mark
, buffer
,
1550 BUF_ZV (XBUFFER (buffer
)),
1551 BUF_ZV_BYTE (XBUFFER (buffer
)));
1554 /* Decide coding systems for communicating with the process. Here
1555 we don't setup the structure coding_system nor pay attention to
1556 unibyte mode. They are done in create_process. */
1558 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1559 Lisp_Object coding_systems
= Qt
;
1560 Lisp_Object val
, *args2
;
1561 struct gcpro gcpro1
, gcpro2
;
1563 val
= Vcoding_system_for_read
;
1566 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1567 args2
[0] = Qstart_process
;
1568 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1569 GCPRO2 (proc
, current_dir
);
1570 if (!NILP (program
))
1571 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1573 if (CONSP (coding_systems
))
1574 val
= XCAR (coding_systems
);
1575 else if (CONSP (Vdefault_process_coding_system
))
1576 val
= XCAR (Vdefault_process_coding_system
);
1578 XPROCESS (proc
)->decode_coding_system
= val
;
1580 val
= Vcoding_system_for_write
;
1583 if (EQ (coding_systems
, Qt
))
1585 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1586 args2
[0] = Qstart_process
;
1587 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1588 GCPRO2 (proc
, current_dir
);
1589 if (!NILP (program
))
1590 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1593 if (CONSP (coding_systems
))
1594 val
= XCDR (coding_systems
);
1595 else if (CONSP (Vdefault_process_coding_system
))
1596 val
= XCDR (Vdefault_process_coding_system
);
1598 XPROCESS (proc
)->encode_coding_system
= val
;
1602 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1603 XPROCESS (proc
)->decoding_carryover
= 0;
1604 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1606 XPROCESS (proc
)->inherit_coding_system_flag
1607 = !(NILP (buffer
) || !inherit_process_coding_system
);
1609 if (!NILP (program
))
1611 /* If program file name is not absolute, search our path for it.
1612 Put the name we will really use in TEM. */
1613 if (!IS_DIRECTORY_SEP (SREF (program
, 0))
1614 && !(SCHARS (program
) > 1
1615 && IS_DEVICE_SEP (SREF (program
, 1))))
1617 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1620 GCPRO4 (name
, program
, buffer
, current_dir
);
1621 openp (Vexec_path
, program
, Vexec_suffixes
, &tem
, make_number (X_OK
));
1624 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1625 tem
= Fexpand_file_name (tem
, Qnil
);
1629 if (!NILP (Ffile_directory_p (program
)))
1630 error ("Specified program for new process is a directory");
1634 /* If program file name starts with /: for quoting a magic name,
1636 if (SBYTES (tem
) > 2 && SREF (tem
, 0) == '/'
1637 && SREF (tem
, 1) == ':')
1638 tem
= Fsubstring (tem
, make_number (2), Qnil
);
1641 struct gcpro gcpro1
;
1644 /* Encode the file name and put it in NEW_ARGV.
1645 That's where the child will use it to execute the program. */
1646 tem
= Fcons (ENCODE_FILE (tem
), Qnil
);
1648 /* Here we encode arguments by the coding system used for sending
1649 data to the process. We don't support using different coding
1650 systems for encoding arguments and for encoding data sent to the
1653 for (i
= 3; i
< nargs
; i
++)
1655 tem
= Fcons (args
[i
], tem
);
1656 CHECK_STRING (XCAR (tem
));
1657 if (STRING_MULTIBYTE (XCAR (tem
)))
1659 code_convert_string_norecord
1660 (XCAR (tem
), XPROCESS (proc
)->encode_coding_system
, 1));
1666 /* Now that everything is encoded we can collect the strings into
1668 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1669 new_argv
[nargs
- 2] = 0;
1671 for (i
= nargs
- 3; i
>= 0; i
--)
1673 new_argv
[i
] = SDATA (XCAR (tem
));
1677 create_process (proc
, (char **) new_argv
, current_dir
);
1682 return unbind_to (count
, proc
);
1685 /* This function is the unwind_protect form for Fstart_process. If
1686 PROC doesn't have its pid set, then we know someone has signaled
1687 an error and the process wasn't started successfully, so we should
1688 remove it from the process list. */
1690 start_process_unwind (Lisp_Object proc
)
1692 if (!PROCESSP (proc
))
1695 /* Was PROC started successfully? */
1696 if (XPROCESS (proc
)->pid
== -1)
1697 remove_process (proc
);
1703 create_process_1 (struct atimer
*timer
)
1705 /* Nothing to do. */
1710 create_process (Lisp_Object process
, char **new_argv
, Lisp_Object current_dir
)
1712 int inchannel
, outchannel
;
1715 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1716 int wait_child_setup
[2];
1720 struct sigaction sigint_action
;
1721 struct sigaction sigquit_action
;
1723 struct sigaction sighup_action
;
1725 /* Use volatile to protect variables from being clobbered by longjmp. */
1726 volatile int forkin
, forkout
;
1727 volatile int pty_flag
= 0;
1729 extern char **environ
;
1732 inchannel
= outchannel
= -1;
1735 if (!NILP (Vprocess_connection_type
))
1736 outchannel
= inchannel
= allocate_pty ();
1740 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1741 /* On most USG systems it does not work to open the pty's tty here,
1742 then close it and reopen it in the child. */
1744 /* Don't let this terminal become our controlling terminal
1745 (in case we don't have one). */
1746 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1748 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
1751 report_file_error ("Opening pty", Qnil
);
1753 forkin
= forkout
= -1;
1754 #endif /* not USG, or USG_SUBTTY_WORKS */
1758 #endif /* HAVE_PTYS */
1763 report_file_error ("Creating pipe", Qnil
);
1769 emacs_close (inchannel
);
1770 emacs_close (forkout
);
1771 report_file_error ("Creating pipe", Qnil
);
1777 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1781 tem
= pipe (wait_child_setup
);
1783 report_file_error ("Creating pipe", Qnil
);
1784 tem
= fcntl (wait_child_setup
[1], F_GETFD
, 0);
1786 tem
= fcntl (wait_child_setup
[1], F_SETFD
, tem
| FD_CLOEXEC
);
1789 emacs_close (wait_child_setup
[0]);
1790 emacs_close (wait_child_setup
[1]);
1791 report_file_error ("Setting file descriptor flags", Qnil
);
1797 /* Replaced by close_process_descs */
1798 set_exclusive_use (inchannel
);
1799 set_exclusive_use (outchannel
);
1803 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1804 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1807 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1808 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1812 /* Record this as an active process, with its channels.
1813 As a result, child_setup will close Emacs's side of the pipes. */
1814 chan_process
[inchannel
] = process
;
1815 XPROCESS (process
)->infd
= inchannel
;
1816 XPROCESS (process
)->outfd
= outchannel
;
1818 /* Previously we recorded the tty descriptor used in the subprocess.
1819 It was only used for getting the foreground tty process, so now
1820 we just reopen the device (see emacs_get_tty_pgrp) as this is
1821 more portable (see USG_SUBTTY_WORKS above). */
1823 XPROCESS (process
)->pty_flag
= pty_flag
;
1824 XPROCESS (process
)->status
= Qrun
;
1825 setup_process_coding_systems (process
);
1827 /* Delay interrupts until we have a chance to store
1828 the new fork's pid in its process structure */
1829 sigemptyset (&blocked
);
1831 sigaddset (&blocked
, SIGCHLD
);
1833 #ifdef HAVE_WORKING_VFORK
1834 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1835 this sets the parent's signal handlers as well as the child's.
1836 So delay all interrupts whose handlers the child might munge,
1837 and record the current handlers so they can be restored later. */
1838 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1839 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1841 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1843 #endif /* HAVE_WORKING_VFORK */
1844 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1846 FD_SET (inchannel
, &input_wait_mask
);
1847 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1848 if (inchannel
> max_process_desc
)
1849 max_process_desc
= inchannel
;
1851 /* Until we store the proper pid, enable sigchld_handler
1852 to recognize an unknown pid as standing for this process.
1853 It is very important not to let this `marker' value stay
1854 in the table after this function has returned; if it does
1855 it might cause call-process to hang and subsequent asynchronous
1856 processes to get their return values scrambled. */
1857 XPROCESS (process
)->pid
= -1;
1862 /* child_setup must clobber environ on systems with true vfork.
1863 Protect it from permanent change. */
1864 char **save_environ
= environ
;
1866 current_dir
= ENCODE_FILE (current_dir
);
1871 #endif /* not WINDOWSNT */
1873 int xforkin
= forkin
;
1874 int xforkout
= forkout
;
1876 #if 0 /* This was probably a mistake--it duplicates code later on,
1877 but fails to handle all the cases. */
1878 /* Make sure SIGCHLD is not blocked in the child. */
1879 sigsetmask (SIGEMPTYMASK
);
1882 /* Make the pty be the controlling terminal of the process. */
1884 /* First, disconnect its current controlling terminal. */
1886 /* We tried doing setsid only if pty_flag, but it caused
1887 process_set_signal to fail on SGI when using a pipe. */
1889 /* Make the pty's terminal the controlling terminal. */
1890 if (pty_flag
&& xforkin
>= 0)
1893 /* We ignore the return value
1894 because faith@cs.unc.edu says that is necessary on Linux. */
1895 ioctl (xforkin
, TIOCSCTTY
, 0);
1898 #else /* not HAVE_SETSID */
1900 /* It's very important to call setpgrp here and no time
1901 afterwards. Otherwise, we lose our controlling tty which
1902 is set when we open the pty. */
1905 #endif /* not HAVE_SETSID */
1906 #if defined (HAVE_TERMIOS) && defined (LDISC1)
1907 if (pty_flag
&& xforkin
>= 0)
1910 tcgetattr (xforkin
, &t
);
1912 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1913 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1916 #if defined (NTTYDISC) && defined (TIOCSETD)
1917 if (pty_flag
&& xforkin
>= 0)
1919 /* Use new line discipline. */
1920 int ldisc
= NTTYDISC
;
1921 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1926 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1927 can do TIOCSPGRP only to the process's controlling tty. */
1930 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1931 I can't test it since I don't have 4.3. */
1932 int j
= emacs_open ("/dev/tty", O_RDWR
, 0);
1935 ioctl (j
, TIOCNOTTY
, 0);
1939 /* In order to get a controlling terminal on some versions
1940 of BSD, it is necessary to put the process in pgrp 0
1941 before it opens the terminal. */
1949 #endif /* TIOCNOTTY */
1951 #if !defined (DONT_REOPEN_PTY)
1952 /*** There is a suggestion that this ought to be a
1953 conditional on TIOCSPGRP,
1954 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
1955 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1956 that system does seem to need this code, even though
1957 both HAVE_SETSID and TIOCSCTTY are defined. */
1958 /* Now close the pty (if we had it open) and reopen it.
1959 This makes the pty the controlling terminal of the subprocess. */
1963 /* I wonder if emacs_close (emacs_open (pty_name, ...))
1966 emacs_close (xforkin
);
1967 xforkout
= xforkin
= emacs_open (pty_name
, O_RDWR
, 0);
1971 emacs_write (1, "Couldn't open the pty terminal ", 31);
1972 emacs_write (1, pty_name
, strlen (pty_name
));
1973 emacs_write (1, "\n", 1);
1978 #endif /* not DONT_REOPEN_PTY */
1980 #ifdef SETUP_SLAVE_PTY
1985 #endif /* SETUP_SLAVE_PTY */
1987 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1988 Now reenable it in the child, so it will die when we want it to. */
1990 signal (SIGHUP
, SIG_DFL
);
1992 #endif /* HAVE_PTYS */
1994 signal (SIGINT
, SIG_DFL
);
1995 signal (SIGQUIT
, SIG_DFL
);
1997 /* Stop blocking signals in the child. */
1998 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2001 child_setup_tty (xforkout
);
2003 pid
= child_setup (xforkin
, xforkout
, xforkout
,
2004 new_argv
, 1, current_dir
);
2005 #else /* not WINDOWSNT */
2007 emacs_close (wait_child_setup
[0]);
2009 child_setup (xforkin
, xforkout
, xforkout
,
2010 new_argv
, 1, current_dir
);
2011 #endif /* not WINDOWSNT */
2013 environ
= save_environ
;
2018 /* This runs in the Emacs process. */
2022 emacs_close (forkin
);
2023 if (forkin
!= forkout
&& forkout
>= 0)
2024 emacs_close (forkout
);
2028 /* vfork succeeded. */
2029 XPROCESS (process
)->pid
= pid
;
2032 register_child (pid
, inchannel
);
2033 #endif /* WINDOWSNT */
2035 /* If the subfork execv fails, and it exits,
2036 this close hangs. I don't know why.
2037 So have an interrupt jar it loose. */
2039 struct atimer
*timer
;
2043 EMACS_SET_SECS_USECS (offset
, 1, 0);
2044 timer
= start_atimer (ATIMER_RELATIVE
, offset
, create_process_1
, 0);
2047 emacs_close (forkin
);
2049 cancel_atimer (timer
);
2053 if (forkin
!= forkout
&& forkout
>= 0)
2054 emacs_close (forkout
);
2058 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2061 XPROCESS (process
)->tty_name
= Qnil
;
2063 #if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2064 /* Wait for child_setup to complete in case that vfork is
2065 actually defined as fork. The descriptor wait_child_setup[1]
2066 of a pipe is closed at the child side either by close-on-exec
2067 on successful execvp or the _exit call in child_setup. */
2071 emacs_close (wait_child_setup
[1]);
2072 emacs_read (wait_child_setup
[0], &dummy
, 1);
2073 emacs_close (wait_child_setup
[0]);
2078 /* Restore the signal state whether vfork succeeded or not.
2079 (We will signal an error, below, if it failed.) */
2080 #ifdef HAVE_WORKING_VFORK
2081 /* Restore the parent's signal handlers. */
2082 sigaction (SIGINT
, &sigint_action
, 0);
2083 sigaction (SIGQUIT
, &sigquit_action
, 0);
2085 sigaction (SIGHUP
, &sighup_action
, 0);
2087 #endif /* HAVE_WORKING_VFORK */
2088 /* Stop blocking signals in the parent. */
2089 sigprocmask (SIG_SETMASK
, &procmask
, 0);
2091 /* Now generate the error if vfork failed. */
2093 report_file_error ("Doing vfork", Qnil
);
2097 create_pty (Lisp_Object process
)
2099 int inchannel
, outchannel
;
2101 /* Use volatile to protect variables from being clobbered by longjmp. */
2102 volatile int forkin
, forkout
;
2103 volatile int pty_flag
= 0;
2105 inchannel
= outchannel
= -1;
2108 if (!NILP (Vprocess_connection_type
))
2109 outchannel
= inchannel
= allocate_pty ();
2113 #if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2114 /* On most USG systems it does not work to open the pty's tty here,
2115 then close it and reopen it in the child. */
2117 /* Don't let this terminal become our controlling terminal
2118 (in case we don't have one). */
2119 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
2121 forkout
= forkin
= emacs_open (pty_name
, O_RDWR
, 0);
2124 report_file_error ("Opening pty", Qnil
);
2125 #if defined (DONT_REOPEN_PTY)
2126 /* In the case that vfork is defined as fork, the parent process
2127 (Emacs) may send some data before the child process completes
2128 tty options setup. So we setup tty before forking. */
2129 child_setup_tty (forkout
);
2130 #endif /* DONT_REOPEN_PTY */
2132 forkin
= forkout
= -1;
2133 #endif /* not USG, or USG_SUBTTY_WORKS */
2136 #endif /* HAVE_PTYS */
2139 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
2140 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
2143 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
2144 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
2148 /* Record this as an active process, with its channels.
2149 As a result, child_setup will close Emacs's side of the pipes. */
2150 chan_process
[inchannel
] = process
;
2151 XPROCESS (process
)->infd
= inchannel
;
2152 XPROCESS (process
)->outfd
= outchannel
;
2154 /* Previously we recorded the tty descriptor used in the subprocess.
2155 It was only used for getting the foreground tty process, so now
2156 we just reopen the device (see emacs_get_tty_pgrp) as this is
2157 more portable (see USG_SUBTTY_WORKS above). */
2159 XPROCESS (process
)->pty_flag
= pty_flag
;
2160 XPROCESS (process
)->status
= Qrun
;
2161 setup_process_coding_systems (process
);
2163 FD_SET (inchannel
, &input_wait_mask
);
2164 FD_SET (inchannel
, &non_keyboard_wait_mask
);
2165 if (inchannel
> max_process_desc
)
2166 max_process_desc
= inchannel
;
2168 XPROCESS (process
)->pid
= -2;
2171 XPROCESS (process
)->tty_name
= build_string (pty_name
);
2174 XPROCESS (process
)->tty_name
= Qnil
;
2178 /* Convert an internal struct sockaddr to a lisp object (vector or string).
2179 The address family of sa is not included in the result. */
2182 conv_sockaddr_to_lisp (struct sockaddr
*sa
, int len
)
2184 Lisp_Object address
;
2187 register struct Lisp_Vector
*p
;
2189 /* Workaround for a bug in getsockname on BSD: Names bound to
2190 sockets in the UNIX domain are inaccessible; getsockname returns
2191 a zero length name. */
2192 if (len
< offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
))
2193 return empty_unibyte_string
;
2195 switch (sa
->sa_family
)
2199 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2200 len
= sizeof (sin
->sin_addr
) + 1;
2201 address
= Fmake_vector (make_number (len
), Qnil
);
2202 p
= XVECTOR (address
);
2203 p
->contents
[--len
] = make_number (ntohs (sin
->sin_port
));
2204 cp
= (unsigned char *) &sin
->sin_addr
;
2210 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2211 uint16_t *ip6
= (uint16_t *) &sin6
->sin6_addr
;
2212 len
= sizeof (sin6
->sin6_addr
)/2 + 1;
2213 address
= Fmake_vector (make_number (len
), Qnil
);
2214 p
= XVECTOR (address
);
2215 p
->contents
[--len
] = make_number (ntohs (sin6
->sin6_port
));
2216 for (i
= 0; i
< len
; i
++)
2217 p
->contents
[i
] = make_number (ntohs (ip6
[i
]));
2221 #ifdef HAVE_LOCAL_SOCKETS
2224 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2225 for (i
= 0; i
< sizeof (sockun
->sun_path
); i
++)
2226 if (sockun
->sun_path
[i
] == 0)
2228 return make_unibyte_string (sockun
->sun_path
, i
);
2232 len
-= offsetof (struct sockaddr
, sa_family
) + sizeof (sa
->sa_family
);
2233 address
= Fcons (make_number (sa
->sa_family
),
2234 Fmake_vector (make_number (len
), Qnil
));
2235 p
= XVECTOR (XCDR (address
));
2236 cp
= (unsigned char *) &sa
->sa_family
+ sizeof (sa
->sa_family
);
2242 p
->contents
[i
++] = make_number (*cp
++);
2248 /* Get family and required size for sockaddr structure to hold ADDRESS. */
2251 get_lisp_to_sockaddr_size (Lisp_Object address
, int *familyp
)
2253 register struct Lisp_Vector
*p
;
2255 if (VECTORP (address
))
2257 p
= XVECTOR (address
);
2261 return sizeof (struct sockaddr_in
);
2264 else if (p
->size
== 9)
2266 *familyp
= AF_INET6
;
2267 return sizeof (struct sockaddr_in6
);
2271 #ifdef HAVE_LOCAL_SOCKETS
2272 else if (STRINGP (address
))
2274 *familyp
= AF_LOCAL
;
2275 return sizeof (struct sockaddr_un
);
2278 else if (CONSP (address
) && INTEGERP (XCAR (address
)) && VECTORP (XCDR (address
)))
2280 struct sockaddr
*sa
;
2281 *familyp
= XINT (XCAR (address
));
2282 p
= XVECTOR (XCDR (address
));
2283 return p
->size
+ sizeof (sa
->sa_family
);
2288 /* Convert an address object (vector or string) to an internal sockaddr.
2290 The address format has been basically validated by
2291 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2292 it could have come from user data. So if FAMILY is not valid,
2293 we return after zeroing *SA. */
2296 conv_lisp_to_sockaddr (int family
, Lisp_Object address
, struct sockaddr
*sa
, int len
)
2298 register struct Lisp_Vector
*p
;
2299 register unsigned char *cp
= NULL
;
2302 memset (sa
, 0, len
);
2304 if (VECTORP (address
))
2306 p
= XVECTOR (address
);
2307 if (family
== AF_INET
)
2309 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
2310 len
= sizeof (sin
->sin_addr
) + 1;
2311 i
= XINT (p
->contents
[--len
]);
2312 sin
->sin_port
= htons (i
);
2313 cp
= (unsigned char *)&sin
->sin_addr
;
2314 sa
->sa_family
= family
;
2317 else if (family
== AF_INET6
)
2319 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) sa
;
2320 uint16_t *ip6
= (uint16_t *)&sin6
->sin6_addr
;
2321 len
= sizeof (sin6
->sin6_addr
) + 1;
2322 i
= XINT (p
->contents
[--len
]);
2323 sin6
->sin6_port
= htons (i
);
2324 for (i
= 0; i
< len
; i
++)
2325 if (INTEGERP (p
->contents
[i
]))
2327 int j
= XFASTINT (p
->contents
[i
]) & 0xffff;
2330 sa
->sa_family
= family
;
2337 else if (STRINGP (address
))
2339 #ifdef HAVE_LOCAL_SOCKETS
2340 if (family
== AF_LOCAL
)
2342 struct sockaddr_un
*sockun
= (struct sockaddr_un
*) sa
;
2343 cp
= SDATA (address
);
2344 for (i
= 0; i
< sizeof (sockun
->sun_path
) && *cp
; i
++)
2345 sockun
->sun_path
[i
] = *cp
++;
2346 sa
->sa_family
= family
;
2353 p
= XVECTOR (XCDR (address
));
2354 cp
= (unsigned char *)sa
+ sizeof (sa
->sa_family
);
2357 for (i
= 0; i
< len
; i
++)
2358 if (INTEGERP (p
->contents
[i
]))
2359 *cp
++ = XFASTINT (p
->contents
[i
]) & 0xff;
2362 #ifdef DATAGRAM_SOCKETS
2363 DEFUN ("process-datagram-address", Fprocess_datagram_address
, Sprocess_datagram_address
,
2365 doc
: /* Get the current datagram address associated with PROCESS. */)
2366 (Lisp_Object process
)
2370 CHECK_PROCESS (process
);
2372 if (!DATAGRAM_CONN_P (process
))
2375 channel
= XPROCESS (process
)->infd
;
2376 return conv_sockaddr_to_lisp (datagram_address
[channel
].sa
,
2377 datagram_address
[channel
].len
);
2380 DEFUN ("set-process-datagram-address", Fset_process_datagram_address
, Sset_process_datagram_address
,
2382 doc
: /* Set the datagram address for PROCESS to ADDRESS.
2383 Returns nil upon error setting address, ADDRESS otherwise. */)
2384 (Lisp_Object process
, Lisp_Object address
)
2389 CHECK_PROCESS (process
);
2391 if (!DATAGRAM_CONN_P (process
))
2394 channel
= XPROCESS (process
)->infd
;
2396 len
= get_lisp_to_sockaddr_size (address
, &family
);
2397 if (datagram_address
[channel
].len
!= len
)
2399 conv_lisp_to_sockaddr (family
, address
, datagram_address
[channel
].sa
, len
);
2405 static const struct socket_options
{
2406 /* The name of this option. Should be lowercase version of option
2407 name without SO_ prefix. */
2409 /* Option level SOL_... */
2411 /* Option number SO_... */
2413 enum { SOPT_UNKNOWN
, SOPT_BOOL
, SOPT_INT
, SOPT_IFNAME
, SOPT_LINGER
} opttype
;
2414 enum { OPIX_NONE
=0, OPIX_MISC
=1, OPIX_REUSEADDR
=2 } optbit
;
2415 } socket_options
[] =
2417 #ifdef SO_BINDTODEVICE
2418 { ":bindtodevice", SOL_SOCKET
, SO_BINDTODEVICE
, SOPT_IFNAME
, OPIX_MISC
},
2421 { ":broadcast", SOL_SOCKET
, SO_BROADCAST
, SOPT_BOOL
, OPIX_MISC
},
2424 { ":dontroute", SOL_SOCKET
, SO_DONTROUTE
, SOPT_BOOL
, OPIX_MISC
},
2427 { ":keepalive", SOL_SOCKET
, SO_KEEPALIVE
, SOPT_BOOL
, OPIX_MISC
},
2430 { ":linger", SOL_SOCKET
, SO_LINGER
, SOPT_LINGER
, OPIX_MISC
},
2433 { ":oobinline", SOL_SOCKET
, SO_OOBINLINE
, SOPT_BOOL
, OPIX_MISC
},
2436 { ":priority", SOL_SOCKET
, SO_PRIORITY
, SOPT_INT
, OPIX_MISC
},
2439 { ":reuseaddr", SOL_SOCKET
, SO_REUSEADDR
, SOPT_BOOL
, OPIX_REUSEADDR
},
2441 { 0, 0, 0, SOPT_UNKNOWN
, OPIX_NONE
}
2444 /* Set option OPT to value VAL on socket S.
2446 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2447 Signals an error if setting a known option fails.
2451 set_socket_option (int s
, Lisp_Object opt
, Lisp_Object val
)
2454 const struct socket_options
*sopt
;
2459 name
= (char *) SDATA (SYMBOL_NAME (opt
));
2460 for (sopt
= socket_options
; sopt
->name
; sopt
++)
2461 if (strcmp (name
, sopt
->name
) == 0)
2464 switch (sopt
->opttype
)
2469 optval
= NILP (val
) ? 0 : 1;
2470 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2471 &optval
, sizeof (optval
));
2479 optval
= XINT (val
);
2481 error ("Bad option value for %s", name
);
2482 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2483 &optval
, sizeof (optval
));
2487 #ifdef SO_BINDTODEVICE
2490 char devname
[IFNAMSIZ
+1];
2492 /* This is broken, at least in the Linux 2.4 kernel.
2493 To unbind, the arg must be a zero integer, not the empty string.
2494 This should work on all systems. KFS. 2003-09-23. */
2495 memset (devname
, 0, sizeof devname
);
2498 char *arg
= (char *) SDATA (val
);
2499 int len
= min (strlen (arg
), IFNAMSIZ
);
2500 memcpy (devname
, arg
, len
);
2502 else if (!NILP (val
))
2503 error ("Bad option value for %s", name
);
2504 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2513 struct linger linger
;
2516 linger
.l_linger
= 0;
2518 linger
.l_linger
= XINT (val
);
2520 linger
.l_onoff
= NILP (val
) ? 0 : 1;
2521 ret
= setsockopt (s
, sopt
->optlevel
, sopt
->optnum
,
2522 &linger
, sizeof (linger
));
2532 report_file_error ("Cannot set network option",
2533 Fcons (opt
, Fcons (val
, Qnil
)));
2534 return (1 << sopt
->optbit
);
2538 DEFUN ("set-network-process-option",
2539 Fset_network_process_option
, Sset_network_process_option
,
2541 doc
: /* For network process PROCESS set option OPTION to value VALUE.
2542 See `make-network-process' for a list of options and values.
2543 If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2544 OPTION is not a supported option, return nil instead; otherwise return t. */)
2545 (Lisp_Object process
, Lisp_Object option
, Lisp_Object value
, Lisp_Object no_error
)
2548 struct Lisp_Process
*p
;
2550 CHECK_PROCESS (process
);
2551 p
= XPROCESS (process
);
2552 if (!NETCONN1_P (p
))
2553 error ("Process is not a network process");
2557 error ("Process is not running");
2559 if (set_socket_option (s
, option
, value
))
2561 p
->childp
= Fplist_put (p
->childp
, option
, value
);
2565 if (NILP (no_error
))
2566 error ("Unknown or unsupported option");
2573 DEFUN ("serial-process-configure",
2574 Fserial_process_configure
,
2575 Sserial_process_configure
,
2577 doc
: /* Configure speed, bytesize, etc. of a serial process.
2579 Arguments are specified as keyword/argument pairs. Attributes that
2580 are not given are re-initialized from the process's current
2581 configuration (available via the function `process-contact') or set to
2582 reasonable default values. The following arguments are defined:
2588 -- Any of these arguments can be given to identify the process that is
2589 to be configured. If none of these arguments is given, the current
2590 buffer's process is used.
2592 :speed SPEED -- SPEED is the speed of the serial port in bits per
2593 second, also called baud rate. Any value can be given for SPEED, but
2594 most serial ports work only at a few defined values between 1200 and
2595 115200, with 9600 being the most common value. If SPEED is nil, the
2596 serial port is not configured any further, i.e., all other arguments
2597 are ignored. This may be useful for special serial ports such as
2598 Bluetooth-to-serial converters which can only be configured through AT
2599 commands. A value of nil for SPEED can be used only when passed
2600 through `make-serial-process' or `serial-term'.
2602 :bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2603 can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2605 :parity PARITY -- PARITY can be nil (don't use parity), the symbol
2606 `odd' (use odd parity), or the symbol `even' (use even parity). If
2607 PARITY is not given, no parity is used.
2609 :stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2610 terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2611 is not given or nil, 1 stopbit is used.
2613 :flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2614 flowcontrol to be used, which is either nil (don't use flowcontrol),
2615 the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2616 \(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2617 flowcontrol is used.
2619 `serial-process-configure' is called by `make-serial-process' for the
2620 initial configuration of the serial port.
2624 \(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2626 \(serial-process-configure
2627 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2629 \(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2631 usage: (serial-process-configure &rest ARGS) */)
2632 (int nargs
, Lisp_Object
*args
)
2634 struct Lisp_Process
*p
;
2635 Lisp_Object contact
= Qnil
;
2636 Lisp_Object proc
= Qnil
;
2637 struct gcpro gcpro1
;
2639 contact
= Flist (nargs
, args
);
2642 proc
= Fplist_get (contact
, QCprocess
);
2644 proc
= Fplist_get (contact
, QCname
);
2646 proc
= Fplist_get (contact
, QCbuffer
);
2648 proc
= Fplist_get (contact
, QCport
);
2649 proc
= get_process (proc
);
2650 p
= XPROCESS (proc
);
2651 if (!EQ (p
->type
, Qserial
))
2652 error ("Not a serial process");
2654 if (NILP (Fplist_get (p
->childp
, QCspeed
)))
2660 serial_configure (p
, contact
);
2666 /* Used by make-serial-process to recover from errors. */
2667 Lisp_Object
make_serial_process_unwind (Lisp_Object proc
)
2669 if (!PROCESSP (proc
))
2671 remove_process (proc
);
2675 DEFUN ("make-serial-process", Fmake_serial_process
, Smake_serial_process
,
2677 doc
: /* Create and return a serial port process.
2679 In Emacs, serial port connections are represented by process objects,
2680 so input and output work as for subprocesses, and `delete-process'
2681 closes a serial port connection. However, a serial process has no
2682 process id, it cannot be signaled, and the status codes are different
2683 from normal processes.
2685 `make-serial-process' creates a process and a buffer, on which you
2686 probably want to use `process-send-string'. Try \\[serial-term] for
2687 an interactive terminal. See below for examples.
2689 Arguments are specified as keyword/argument pairs. The following
2690 arguments are defined:
2692 :port PORT -- (mandatory) PORT is the path or name of the serial port.
2693 For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2694 could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2695 the backslashes in strings).
2697 :speed SPEED -- (mandatory) is handled by `serial-process-configure',
2698 which is called by `make-serial-process'.
2700 :name NAME -- NAME is the name of the process. If NAME is not given,
2701 the value of PORT is used.
2703 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2704 with the process. Process output goes at the end of that buffer,
2705 unless you specify an output stream or filter function to handle the
2706 output. If BUFFER is not given, the value of NAME is used.
2708 :coding CODING -- If CODING is a symbol, it specifies the coding
2709 system used for both reading and writing for this process. If CODING
2710 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2711 ENCODING is used for writing.
2713 :noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2714 the process is running. If BOOL is not given, query before exiting.
2716 :stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2717 In the stopped state, a serial process does not accept incoming data,
2718 but you can send outgoing data. The stopped state is cleared by
2719 `continue-process' and set by `stop-process'.
2721 :filter FILTER -- Install FILTER as the process filter.
2723 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2725 :plist PLIST -- Install PLIST as the initial plist of the process.
2732 -- These arguments are handled by `serial-process-configure', which is
2733 called by `make-serial-process'.
2735 The original argument list, possibly modified by later configuration,
2736 is available via the function `process-contact'.
2740 \(make-serial-process :port "/dev/ttyS0" :speed 9600)
2742 \(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2744 \(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2746 \(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2748 usage: (make-serial-process &rest ARGS) */)
2749 (int nargs
, Lisp_Object
*args
)
2752 Lisp_Object proc
, contact
, port
;
2753 struct Lisp_Process
*p
;
2754 struct gcpro gcpro1
;
2755 Lisp_Object name
, buffer
;
2756 Lisp_Object tem
, val
;
2757 int specpdl_count
= -1;
2762 contact
= Flist (nargs
, args
);
2765 port
= Fplist_get (contact
, QCport
);
2767 error ("No port specified");
2768 CHECK_STRING (port
);
2770 if (NILP (Fplist_member (contact
, QCspeed
)))
2771 error (":speed not specified");
2772 if (!NILP (Fplist_get (contact
, QCspeed
)))
2773 CHECK_NUMBER (Fplist_get (contact
, QCspeed
));
2775 name
= Fplist_get (contact
, QCname
);
2778 CHECK_STRING (name
);
2779 proc
= make_process (name
);
2780 specpdl_count
= SPECPDL_INDEX ();
2781 record_unwind_protect (make_serial_process_unwind
, proc
);
2782 p
= XPROCESS (proc
);
2784 fd
= serial_open ((char*) SDATA (port
));
2787 if (fd
> max_process_desc
)
2788 max_process_desc
= fd
;
2789 chan_process
[fd
] = proc
;
2791 buffer
= Fplist_get (contact
, QCbuffer
);
2794 buffer
= Fget_buffer_create (buffer
);
2797 p
->childp
= contact
;
2798 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
2800 p
->sentinel
= Fplist_get (contact
, QCsentinel
);
2801 p
->filter
= Fplist_get (contact
, QCfilter
);
2803 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
2804 p
->kill_without_query
= 1;
2805 if (tem
= Fplist_get (contact
, QCstop
), !NILP (tem
))
2809 if (!EQ (p
->command
, Qt
))
2811 FD_SET (fd
, &input_wait_mask
);
2812 FD_SET (fd
, &non_keyboard_wait_mask
);
2815 if (BUFFERP (buffer
))
2817 set_marker_both (p
->mark
, buffer
,
2818 BUF_ZV (XBUFFER (buffer
)),
2819 BUF_ZV_BYTE (XBUFFER (buffer
)));
2822 tem
= Fplist_member (contact
, QCcoding
);
2823 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
2829 val
= XCAR (XCDR (tem
));
2833 else if (!NILP (Vcoding_system_for_read
))
2834 val
= Vcoding_system_for_read
;
2835 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2836 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2838 p
->decode_coding_system
= val
;
2843 val
= XCAR (XCDR (tem
));
2847 else if (!NILP (Vcoding_system_for_write
))
2848 val
= Vcoding_system_for_write
;
2849 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
2850 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
2852 p
->encode_coding_system
= val
;
2854 setup_process_coding_systems (proc
);
2855 p
->decoding_buf
= make_uninit_string (0);
2856 p
->decoding_carryover
= 0;
2857 p
->encoding_buf
= make_uninit_string (0);
2858 p
->inherit_coding_system_flag
2859 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
2861 Fserial_process_configure (nargs
, args
);
2863 specpdl_ptr
= specpdl
+ specpdl_count
;
2868 #endif /* HAVE_SERIAL */
2870 /* Create a network stream/datagram client/server process. Treated
2871 exactly like a normal process when reading and writing. Primary
2872 differences are in status display and process deletion. A network
2873 connection has no PID; you cannot signal it. All you can do is
2874 stop/continue it and deactivate/close it via delete-process */
2876 DEFUN ("make-network-process", Fmake_network_process
, Smake_network_process
,
2878 doc
: /* Create and return a network server or client process.
2880 In Emacs, network connections are represented by process objects, so
2881 input and output work as for subprocesses and `delete-process' closes
2882 a network connection. However, a network process has no process id,
2883 it cannot be signaled, and the status codes are different from normal
2886 Arguments are specified as keyword/argument pairs. The following
2887 arguments are defined:
2889 :name NAME -- NAME is name for process. It is modified if necessary
2892 :buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2893 with the process. Process output goes at end of that buffer, unless
2894 you specify an output stream or filter function to handle the output.
2895 BUFFER may be also nil, meaning that this process is not associated
2898 :host HOST -- HOST is name of the host to connect to, or its IP
2899 address. The symbol `local' specifies the local host. If specified
2900 for a server process, it must be a valid name or address for the local
2901 host, and only clients connecting to that address will be accepted.
2903 :service SERVICE -- SERVICE is name of the service desired, or an
2904 integer specifying a port number to connect to. If SERVICE is t,
2905 a random port number is selected for the server. (If Emacs was
2906 compiled with getaddrinfo, a port number can also be specified as a
2907 string, e.g. "80", as well as an integer. This is not portable.)
2909 :type TYPE -- TYPE is the type of connection. The default (nil) is a
2910 stream type connection, `datagram' creates a datagram type connection,
2911 `seqpacket' creates a reliable datagram connection.
2913 :family FAMILY -- FAMILY is the address (and protocol) family for the
2914 service specified by HOST and SERVICE. The default (nil) is to use
2915 whatever address family (IPv4 or IPv6) that is defined for the host
2916 and port number specified by HOST and SERVICE. Other address families
2918 local -- for a local (i.e. UNIX) address specified by SERVICE.
2919 ipv4 -- use IPv4 address family only.
2920 ipv6 -- use IPv6 address family only.
2922 :local ADDRESS -- ADDRESS is the local address used for the connection.
2923 This parameter is ignored when opening a client process. When specified
2924 for a server process, the FAMILY, HOST and SERVICE args are ignored.
2926 :remote ADDRESS -- ADDRESS is the remote partner's address for the
2927 connection. This parameter is ignored when opening a stream server
2928 process. For a datagram server process, it specifies the initial
2929 setting of the remote datagram address. When specified for a client
2930 process, the FAMILY, HOST, and SERVICE args are ignored.
2932 The format of ADDRESS depends on the address family:
2933 - An IPv4 address is represented as an vector of integers [A B C D P]
2934 corresponding to numeric IP address A.B.C.D and port number P.
2935 - A local address is represented as a string with the address in the
2936 local address space.
2937 - An "unsupported family" address is represented by a cons (F . AV)
2938 where F is the family number and AV is a vector containing the socket
2939 address data with one element per address data byte. Do not rely on
2940 this format in portable code, as it may depend on implementation
2941 defined constants, data sizes, and data structure alignment.
2943 :coding CODING -- If CODING is a symbol, it specifies the coding
2944 system used for both reading and writing for this process. If CODING
2945 is a cons (DECODING . ENCODING), DECODING is used for reading, and
2946 ENCODING is used for writing.
2948 :nowait BOOL -- If BOOL is non-nil for a stream type client process,
2949 return without waiting for the connection to complete; instead, the
2950 sentinel function will be called with second arg matching "open" (if
2951 successful) or "failed" when the connect completes. Default is to use
2952 a blocking connect (i.e. wait) for stream type connections.
2954 :noquery BOOL -- Query the user unless BOOL is non-nil, and process is
2955 running when Emacs is exited.
2957 :stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
2958 In the stopped state, a server process does not accept new
2959 connections, and a client process does not handle incoming traffic.
2960 The stopped state is cleared by `continue-process' and set by
2963 :filter FILTER -- Install FILTER as the process filter.
2965 :filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
2966 process filter are multibyte, otherwise they are unibyte.
2967 If this keyword is not specified, the strings are multibyte if
2968 `default-enable-multibyte-characters' is non-nil.
2970 :sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2972 :log LOG -- Install LOG as the server process log function. This
2973 function is called when the server accepts a network connection from a
2974 client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
2975 is the server process, CLIENT is the new process for the connection,
2976 and MESSAGE is a string.
2978 :plist PLIST -- Install PLIST as the new process' initial plist.
2980 :server QLEN -- if QLEN is non-nil, create a server process for the
2981 specified FAMILY, SERVICE, and connection type (stream or datagram).
2982 If QLEN is an integer, it is used as the max. length of the server's
2983 pending connection queue (also known as the backlog); the default
2984 queue length is 5. Default is to create a client process.
2986 The following network options can be specified for this connection:
2988 :broadcast BOOL -- Allow send and receive of datagram broadcasts.
2989 :dontroute BOOL -- Only send to directly connected hosts.
2990 :keepalive BOOL -- Send keep-alive messages on network stream.
2991 :linger BOOL or TIMEOUT -- Send queued messages before closing.
2992 :oobinline BOOL -- Place out-of-band data in receive data stream.
2993 :priority INT -- Set protocol defined priority for sent packets.
2994 :reuseaddr BOOL -- Allow reusing a recently used local address
2995 (this is allowed by default for a server process).
2996 :bindtodevice NAME -- bind to interface NAME. Using this may require
2997 special privileges on some systems.
2999 Consult the relevant system programmer's manual pages for more
3000 information on using these options.
3003 A server process will listen for and accept connections from clients.
3004 When a client connection is accepted, a new network process is created
3005 for the connection with the following parameters:
3007 - The client's process name is constructed by concatenating the server
3008 process' NAME and a client identification string.
3009 - If the FILTER argument is non-nil, the client process will not get a
3010 separate process buffer; otherwise, the client's process buffer is a newly
3011 created buffer named after the server process' BUFFER name or process
3012 NAME concatenated with the client identification string.
3013 - The connection type and the process filter and sentinel parameters are
3014 inherited from the server process' TYPE, FILTER and SENTINEL.
3015 - The client process' contact info is set according to the client's
3016 addressing information (typically an IP address and a port number).
3017 - The client process' plist is initialized from the server's plist.
3019 Notice that the FILTER and SENTINEL args are never used directly by
3020 the server process. Also, the BUFFER argument is not used directly by
3021 the server process, but via the optional :log function, accepted (and
3022 failed) connections may be logged in the server process' buffer.
3024 The original argument list, modified with the actual connection
3025 information, is available via the `process-contact' function.
3027 usage: (make-network-process &rest ARGS) */)
3028 (int nargs
, Lisp_Object
*args
)
3031 Lisp_Object contact
;
3032 struct Lisp_Process
*p
;
3033 #ifdef HAVE_GETADDRINFO
3034 struct addrinfo ai
, *res
, *lres
;
3035 struct addrinfo hints
;
3036 char *portstring
, portbuf
[128];
3037 #else /* HAVE_GETADDRINFO */
3038 struct _emacs_addrinfo
3044 struct sockaddr
*ai_addr
;
3045 struct _emacs_addrinfo
*ai_next
;
3047 #endif /* HAVE_GETADDRINFO */
3048 struct sockaddr_in address_in
;
3049 #ifdef HAVE_LOCAL_SOCKETS
3050 struct sockaddr_un address_un
;
3055 int s
= -1, outch
, inch
;
3056 struct gcpro gcpro1
;
3057 int count
= SPECPDL_INDEX ();
3059 Lisp_Object QCaddress
; /* one of QClocal or QCremote */
3061 Lisp_Object name
, buffer
, host
, service
, address
;
3062 Lisp_Object filter
, sentinel
;
3063 int is_non_blocking_client
= 0;
3064 int is_server
= 0, backlog
= 5;
3071 /* Save arguments for process-contact and clone-process. */
3072 contact
= Flist (nargs
, args
);
3076 /* Ensure socket support is loaded if available. */
3077 init_winsock (TRUE
);
3080 /* :type TYPE (nil: stream, datagram */
3081 tem
= Fplist_get (contact
, QCtype
);
3083 socktype
= SOCK_STREAM
;
3084 #ifdef DATAGRAM_SOCKETS
3085 else if (EQ (tem
, Qdatagram
))
3086 socktype
= SOCK_DGRAM
;
3088 #ifdef HAVE_SEQPACKET
3089 else if (EQ (tem
, Qseqpacket
))
3090 socktype
= SOCK_SEQPACKET
;
3093 error ("Unsupported connection type");
3096 tem
= Fplist_get (contact
, QCserver
);
3099 /* Don't support network sockets when non-blocking mode is
3100 not available, since a blocked Emacs is not useful. */
3101 #if !defined(O_NONBLOCK) && !defined(O_NDELAY)
3102 error ("Network servers not supported");
3106 backlog
= XINT (tem
);
3110 /* Make QCaddress an alias for :local (server) or :remote (client). */
3111 QCaddress
= is_server
? QClocal
: QCremote
;
3114 if (!is_server
&& socktype
!= SOCK_DGRAM
3115 && (tem
= Fplist_get (contact
, QCnowait
), !NILP (tem
)))
3117 #ifndef NON_BLOCKING_CONNECT
3118 error ("Non-blocking connect not supported");
3120 is_non_blocking_client
= 1;
3124 name
= Fplist_get (contact
, QCname
);
3125 buffer
= Fplist_get (contact
, QCbuffer
);
3126 filter
= Fplist_get (contact
, QCfilter
);
3127 sentinel
= Fplist_get (contact
, QCsentinel
);
3129 CHECK_STRING (name
);
3131 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3132 ai
.ai_socktype
= socktype
;
3137 /* :local ADDRESS or :remote ADDRESS */
3138 address
= Fplist_get (contact
, QCaddress
);
3139 if (!NILP (address
))
3141 host
= service
= Qnil
;
3143 if (!(ai
.ai_addrlen
= get_lisp_to_sockaddr_size (address
, &family
)))
3144 error ("Malformed :address");
3145 ai
.ai_family
= family
;
3146 ai
.ai_addr
= alloca (ai
.ai_addrlen
);
3147 conv_lisp_to_sockaddr (family
, address
, ai
.ai_addr
, ai
.ai_addrlen
);
3151 /* :family FAMILY -- nil (for Inet), local, or integer. */
3152 tem
= Fplist_get (contact
, QCfamily
);
3155 #if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
3161 #ifdef HAVE_LOCAL_SOCKETS
3162 else if (EQ (tem
, Qlocal
))
3166 else if (EQ (tem
, Qipv6
))
3169 else if (EQ (tem
, Qipv4
))
3171 else if (INTEGERP (tem
))
3172 family
= XINT (tem
);
3174 error ("Unknown address family");
3176 ai
.ai_family
= family
;
3178 /* :service SERVICE -- string, integer (port number), or t (random port). */
3179 service
= Fplist_get (contact
, QCservice
);
3181 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3182 host
= Fplist_get (contact
, QChost
);
3185 if (EQ (host
, Qlocal
))
3186 host
= build_string ("localhost");
3187 CHECK_STRING (host
);
3190 #ifdef HAVE_LOCAL_SOCKETS
3191 if (family
== AF_LOCAL
)
3195 message (":family local ignores the :host \"%s\" property",
3197 contact
= Fplist_put (contact
, QChost
, Qnil
);
3200 CHECK_STRING (service
);
3201 memset (&address_un
, 0, sizeof address_un
);
3202 address_un
.sun_family
= AF_LOCAL
;
3203 strncpy (address_un
.sun_path
, SDATA (service
), sizeof address_un
.sun_path
);
3204 ai
.ai_addr
= (struct sockaddr
*) &address_un
;
3205 ai
.ai_addrlen
= sizeof address_un
;
3210 /* Slow down polling to every ten seconds.
3211 Some kernels have a bug which causes retrying connect to fail
3212 after a connect. Polling can interfere with gethostbyname too. */
3213 #ifdef POLL_FOR_INPUT
3214 if (socktype
!= SOCK_DGRAM
)
3216 record_unwind_protect (unwind_stop_other_atimers
, Qnil
);
3217 bind_polling_period (10);
3221 #ifdef HAVE_GETADDRINFO
3222 /* If we have a host, use getaddrinfo to resolve both host and service.
3223 Otherwise, use getservbyname to lookup the service. */
3227 /* SERVICE can either be a string or int.
3228 Convert to a C string for later use by getaddrinfo. */
3229 if (EQ (service
, Qt
))
3231 else if (INTEGERP (service
))
3233 sprintf (portbuf
, "%ld", (long) XINT (service
));
3234 portstring
= portbuf
;
3238 CHECK_STRING (service
);
3239 portstring
= SDATA (service
);
3244 memset (&hints
, 0, sizeof (hints
));
3246 hints
.ai_family
= family
;
3247 hints
.ai_socktype
= socktype
;
3248 hints
.ai_protocol
= 0;
3250 #ifdef HAVE_RES_INIT
3254 ret
= getaddrinfo (SDATA (host
), portstring
, &hints
, &res
);
3256 #ifdef HAVE_GAI_STRERROR
3257 error ("%s/%s %s", SDATA (host
), portstring
, gai_strerror (ret
));
3259 error ("%s/%s getaddrinfo error %d", SDATA (host
), portstring
, ret
);
3265 #endif /* HAVE_GETADDRINFO */
3267 /* We end up here if getaddrinfo is not defined, or in case no hostname
3268 has been specified (e.g. for a local server process). */
3270 if (EQ (service
, Qt
))
3272 else if (INTEGERP (service
))
3273 port
= htons ((unsigned short) XINT (service
));
3276 struct servent
*svc_info
;
3277 CHECK_STRING (service
);
3278 svc_info
= getservbyname (SDATA (service
),
3279 (socktype
== SOCK_DGRAM
? "udp" : "tcp"));
3281 error ("Unknown service: %s", SDATA (service
));
3282 port
= svc_info
->s_port
;
3285 memset (&address_in
, 0, sizeof address_in
);
3286 address_in
.sin_family
= family
;
3287 address_in
.sin_addr
.s_addr
= INADDR_ANY
;
3288 address_in
.sin_port
= port
;
3290 #ifndef HAVE_GETADDRINFO
3293 struct hostent
*host_info_ptr
;
3295 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
3296 as it may `hang' Emacs for a very long time. */
3300 #ifdef HAVE_RES_INIT
3304 host_info_ptr
= gethostbyname (SDATA (host
));
3309 memcpy (&address_in
.sin_addr
, host_info_ptr
->h_addr
,
3310 host_info_ptr
->h_length
);
3311 family
= host_info_ptr
->h_addrtype
;
3312 address_in
.sin_family
= family
;
3315 /* Attempt to interpret host as numeric inet address */
3317 unsigned long numeric_addr
;
3318 numeric_addr
= inet_addr ((char *) SDATA (host
));
3319 if (numeric_addr
== -1)
3320 error ("Unknown host \"%s\"", SDATA (host
));
3322 memcpy (&address_in
.sin_addr
, &numeric_addr
,
3323 sizeof (address_in
.sin_addr
));
3327 #endif /* not HAVE_GETADDRINFO */
3329 ai
.ai_family
= family
;
3330 ai
.ai_addr
= (struct sockaddr
*) &address_in
;
3331 ai
.ai_addrlen
= sizeof address_in
;
3335 /* Do this in case we never enter the for-loop below. */
3336 count1
= SPECPDL_INDEX ();
3339 for (lres
= res
; lres
; lres
= lres
->ai_next
)
3345 s
= socket (lres
->ai_family
, lres
->ai_socktype
, lres
->ai_protocol
);
3352 #ifdef DATAGRAM_SOCKETS
3353 if (!is_server
&& socktype
== SOCK_DGRAM
)
3355 #endif /* DATAGRAM_SOCKETS */
3357 #ifdef NON_BLOCKING_CONNECT
3358 if (is_non_blocking_client
)
3361 ret
= fcntl (s
, F_SETFL
, O_NONBLOCK
);
3363 ret
= fcntl (s
, F_SETFL
, O_NDELAY
);
3375 /* Make us close S if quit. */
3376 record_unwind_protect (close_file_unwind
, make_number (s
));
3378 /* Parse network options in the arg list.
3379 We simply ignore anything which isn't a known option (including other keywords).
3380 An error is signaled if setting a known option fails. */
3381 for (optn
= optbits
= 0; optn
< nargs
-1; optn
+= 2)
3382 optbits
|= set_socket_option (s
, args
[optn
], args
[optn
+1]);
3386 /* Configure as a server socket. */
3388 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3389 explicit :reuseaddr key to override this. */
3390 #ifdef HAVE_LOCAL_SOCKETS
3391 if (family
!= AF_LOCAL
)
3393 if (!(optbits
& (1 << OPIX_REUSEADDR
)))
3396 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof optval
))
3397 report_file_error ("Cannot set reuse option on server socket", Qnil
);
3400 if (bind (s
, lres
->ai_addr
, lres
->ai_addrlen
))
3401 report_file_error ("Cannot bind server socket", Qnil
);
3403 #ifdef HAVE_GETSOCKNAME
3404 if (EQ (service
, Qt
))
3406 struct sockaddr_in sa1
;
3407 int len1
= sizeof (sa1
);
3408 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3410 ((struct sockaddr_in
*)(lres
->ai_addr
))->sin_port
= sa1
.sin_port
;
3411 service
= make_number (ntohs (sa1
.sin_port
));
3412 contact
= Fplist_put (contact
, QCservice
, service
);
3417 if (socktype
!= SOCK_DGRAM
&& listen (s
, backlog
))
3418 report_file_error ("Cannot listen on server socket", Qnil
);
3426 ret
= connect (s
, lres
->ai_addr
, lres
->ai_addrlen
);
3429 if (ret
== 0 || xerrno
== EISCONN
)
3431 /* The unwind-protect will be discarded afterwards.
3432 Likewise for immediate_quit. */
3436 #ifdef NON_BLOCKING_CONNECT
3438 if (is_non_blocking_client
&& xerrno
== EINPROGRESS
)
3442 if (is_non_blocking_client
&& xerrno
== EWOULDBLOCK
)
3449 if (xerrno
== EINTR
)
3451 /* Unlike most other syscalls connect() cannot be called
3452 again. (That would return EALREADY.) The proper way to
3453 wait for completion is select(). */
3460 sc
= select (s
+ 1, (SELECT_TYPE
*)0, &fdset
, (SELECT_TYPE
*)0,
3467 report_file_error ("select failed", Qnil
);
3471 len
= sizeof xerrno
;
3472 eassert (FD_ISSET (s
, &fdset
));
3473 if (getsockopt (s
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &len
) == -1)
3474 report_file_error ("getsockopt failed", Qnil
);
3476 errno
= xerrno
, report_file_error ("error during connect", Qnil
);
3480 #endif /* !WINDOWSNT */
3484 /* Discard the unwind protect closing S. */
3485 specpdl_ptr
= specpdl
+ count1
;
3490 if (xerrno
== EINTR
)
3497 #ifdef DATAGRAM_SOCKETS
3498 if (socktype
== SOCK_DGRAM
)
3500 if (datagram_address
[s
].sa
)
3502 datagram_address
[s
].sa
= (struct sockaddr
*) xmalloc (lres
->ai_addrlen
);
3503 datagram_address
[s
].len
= lres
->ai_addrlen
;
3507 memset (datagram_address
[s
].sa
, 0, lres
->ai_addrlen
);
3508 if (remote
= Fplist_get (contact
, QCremote
), !NILP (remote
))
3511 rlen
= get_lisp_to_sockaddr_size (remote
, &rfamily
);
3512 if (rfamily
== lres
->ai_family
&& rlen
== lres
->ai_addrlen
)
3513 conv_lisp_to_sockaddr (rfamily
, remote
,
3514 datagram_address
[s
].sa
, rlen
);
3518 memcpy (datagram_address
[s
].sa
, lres
->ai_addr
, lres
->ai_addrlen
);
3521 contact
= Fplist_put (contact
, QCaddress
,
3522 conv_sockaddr_to_lisp (lres
->ai_addr
, lres
->ai_addrlen
));
3523 #ifdef HAVE_GETSOCKNAME
3526 struct sockaddr_in sa1
;
3527 int len1
= sizeof (sa1
);
3528 if (getsockname (s
, (struct sockaddr
*)&sa1
, &len1
) == 0)
3529 contact
= Fplist_put (contact
, QClocal
,
3530 conv_sockaddr_to_lisp ((struct sockaddr
*)&sa1
, len1
));
3537 #ifdef HAVE_GETADDRINFO
3546 /* Discard the unwind protect for closing S, if any. */
3547 specpdl_ptr
= specpdl
+ count1
;
3549 /* Unwind bind_polling_period and request_sigio. */
3550 unbind_to (count
, Qnil
);
3554 /* If non-blocking got this far - and failed - assume non-blocking is
3555 not supported after all. This is probably a wrong assumption, but
3556 the normal blocking calls to open-network-stream handles this error
3558 if (is_non_blocking_client
)
3563 report_file_error ("make server process failed", contact
);
3565 report_file_error ("make client process failed", contact
);
3572 buffer
= Fget_buffer_create (buffer
);
3573 proc
= make_process (name
);
3575 chan_process
[inch
] = proc
;
3578 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
3581 fcntl (inch
, F_SETFL
, O_NDELAY
);
3585 p
= XPROCESS (proc
);
3587 p
->childp
= contact
;
3588 p
->plist
= Fcopy_sequence (Fplist_get (contact
, QCplist
));
3592 p
->sentinel
= sentinel
;
3594 p
->log
= Fplist_get (contact
, QClog
);
3595 if (tem
= Fplist_get (contact
, QCnoquery
), !NILP (tem
))
3596 p
->kill_without_query
= 1;
3597 if ((tem
= Fplist_get (contact
, QCstop
), !NILP (tem
)))
3602 if (is_server
&& socktype
!= SOCK_DGRAM
)
3603 p
->status
= Qlisten
;
3605 /* Make the process marker point into the process buffer (if any). */
3606 if (BUFFERP (buffer
))
3607 set_marker_both (p
->mark
, buffer
,
3608 BUF_ZV (XBUFFER (buffer
)),
3609 BUF_ZV_BYTE (XBUFFER (buffer
)));
3611 #ifdef NON_BLOCKING_CONNECT
3612 if (is_non_blocking_client
)
3614 /* We may get here if connect did succeed immediately. However,
3615 in that case, we still need to signal this like a non-blocking
3617 p
->status
= Qconnect
;
3618 if (!FD_ISSET (inch
, &connect_wait_mask
))
3620 FD_SET (inch
, &connect_wait_mask
);
3621 num_pending_connects
++;
3626 /* A server may have a client filter setting of Qt, but it must
3627 still listen for incoming connects unless it is stopped. */
3628 if ((!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
3629 || (EQ (p
->status
, Qlisten
) && NILP (p
->command
)))
3631 FD_SET (inch
, &input_wait_mask
);
3632 FD_SET (inch
, &non_keyboard_wait_mask
);
3635 if (inch
> max_process_desc
)
3636 max_process_desc
= inch
;
3638 tem
= Fplist_member (contact
, QCcoding
);
3639 if (!NILP (tem
) && (!CONSP (tem
) || !CONSP (XCDR (tem
))))
3640 tem
= Qnil
; /* No error message (too late!). */
3643 /* Setup coding systems for communicating with the network stream. */
3644 struct gcpro gcpro1
;
3645 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3646 Lisp_Object coding_systems
= Qt
;
3647 Lisp_Object args
[5], val
;
3651 val
= XCAR (XCDR (tem
));
3655 else if (!NILP (Vcoding_system_for_read
))
3656 val
= Vcoding_system_for_read
;
3657 else if ((!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
))
3658 || (NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
)))
3659 /* We dare not decode end-of-line format by setting VAL to
3660 Qraw_text, because the existing Emacs Lisp libraries
3661 assume that they receive bare code including a sequene of
3666 if (NILP (host
) || NILP (service
))
3667 coding_systems
= Qnil
;
3670 args
[0] = Qopen_network_stream
, args
[1] = name
,
3671 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3673 coding_systems
= Ffind_operation_coding_system (5, args
);
3676 if (CONSP (coding_systems
))
3677 val
= XCAR (coding_systems
);
3678 else if (CONSP (Vdefault_process_coding_system
))
3679 val
= XCAR (Vdefault_process_coding_system
);
3683 p
->decode_coding_system
= val
;
3687 val
= XCAR (XCDR (tem
));
3691 else if (!NILP (Vcoding_system_for_write
))
3692 val
= Vcoding_system_for_write
;
3693 else if (NILP (current_buffer
->enable_multibyte_characters
))
3697 if (EQ (coding_systems
, Qt
))
3699 if (NILP (host
) || NILP (service
))
3700 coding_systems
= Qnil
;
3703 args
[0] = Qopen_network_stream
, args
[1] = name
,
3704 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
3706 coding_systems
= Ffind_operation_coding_system (5, args
);
3710 if (CONSP (coding_systems
))
3711 val
= XCDR (coding_systems
);
3712 else if (CONSP (Vdefault_process_coding_system
))
3713 val
= XCDR (Vdefault_process_coding_system
);
3717 p
->encode_coding_system
= val
;
3719 setup_process_coding_systems (proc
);
3721 p
->decoding_buf
= make_uninit_string (0);
3722 p
->decoding_carryover
= 0;
3723 p
->encoding_buf
= make_uninit_string (0);
3725 p
->inherit_coding_system_flag
3726 = !(!NILP (tem
) || NILP (buffer
) || !inherit_process_coding_system
);
3733 #if defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3736 DEFUN ("network-interface-list", Fnetwork_interface_list
, Snetwork_interface_list
, 0, 0, 0,
3737 doc
: /* Return an alist of all network interfaces and their network address.
3738 Each element is a cons, the car of which is a string containing the
3739 interface name, and the cdr is the network address in internal
3740 format; see the description of ADDRESS in `make-network-process'. */)
3743 struct ifconf ifconf
;
3744 struct ifreq
*ifreqs
= NULL
;
3749 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3755 buf_size
= ifaces
* sizeof (ifreqs
[0]);
3756 ifreqs
= (struct ifreq
*)xrealloc(ifreqs
, buf_size
);
3763 ifconf
.ifc_len
= buf_size
;
3764 ifconf
.ifc_req
= ifreqs
;
3765 if (ioctl (s
, SIOCGIFCONF
, &ifconf
))
3771 if (ifconf
.ifc_len
== buf_size
)
3775 ifaces
= ifconf
.ifc_len
/ sizeof (ifreqs
[0]);
3778 while (--ifaces
>= 0)
3780 struct ifreq
*ifq
= &ifreqs
[ifaces
];
3781 char namebuf
[sizeof (ifq
->ifr_name
) + 1];
3782 if (ifq
->ifr_addr
.sa_family
!= AF_INET
)
3784 memcpy (namebuf
, ifq
->ifr_name
, sizeof (ifq
->ifr_name
));
3785 namebuf
[sizeof (ifq
->ifr_name
)] = 0;
3786 res
= Fcons (Fcons (build_string (namebuf
),
3787 conv_sockaddr_to_lisp (&ifq
->ifr_addr
,
3788 sizeof (struct sockaddr
))),
3794 #endif /* SIOCGIFCONF */
3796 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3800 const char *flag_sym
;
3803 static const struct ifflag_def ifflag_table
[] = {
3807 #ifdef IFF_BROADCAST
3808 { IFF_BROADCAST
, "broadcast" },
3811 { IFF_DEBUG
, "debug" },
3814 { IFF_LOOPBACK
, "loopback" },
3816 #ifdef IFF_POINTOPOINT
3817 { IFF_POINTOPOINT
, "pointopoint" },
3820 { IFF_RUNNING
, "running" },
3823 { IFF_NOARP
, "noarp" },
3826 { IFF_PROMISC
, "promisc" },
3828 #ifdef IFF_NOTRAILERS
3829 { IFF_NOTRAILERS
, "notrailers" },
3832 { IFF_ALLMULTI
, "allmulti" },
3835 { IFF_MASTER
, "master" },
3838 { IFF_SLAVE
, "slave" },
3840 #ifdef IFF_MULTICAST
3841 { IFF_MULTICAST
, "multicast" },
3844 { IFF_PORTSEL
, "portsel" },
3846 #ifdef IFF_AUTOMEDIA
3847 { IFF_AUTOMEDIA
, "automedia" },
3850 { IFF_DYNAMIC
, "dynamic" },
3853 { IFF_OACTIVE
, "oactive" }, /* OpenBSD: transmission in progress */
3856 { IFF_SIMPLEX
, "simplex" }, /* OpenBSD: can't hear own transmissions */
3859 { IFF_LINK0
, "link0" }, /* OpenBSD: per link layer defined bit */
3862 { IFF_LINK1
, "link1" }, /* OpenBSD: per link layer defined bit */
3865 { IFF_LINK2
, "link2" }, /* OpenBSD: per link layer defined bit */
3870 DEFUN ("network-interface-info", Fnetwork_interface_info
, Snetwork_interface_info
, 1, 1, 0,
3871 doc
: /* Return information about network interface named IFNAME.
3872 The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3873 where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3874 NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3875 FLAGS is the current flags of the interface. */)
3876 (Lisp_Object ifname
)
3879 Lisp_Object res
= Qnil
;
3884 CHECK_STRING (ifname
);
3886 memset (rq
.ifr_name
, 0, sizeof rq
.ifr_name
);
3887 strncpy (rq
.ifr_name
, SDATA (ifname
), sizeof (rq
.ifr_name
));
3889 s
= socket (AF_INET
, SOCK_STREAM
, 0);
3894 #if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3895 if (ioctl (s
, SIOCGIFFLAGS
, &rq
) == 0)
3897 int flags
= rq
.ifr_flags
;
3898 const struct ifflag_def
*fp
;
3902 for (fp
= ifflag_table
; flags
!= 0 && fp
->flag_sym
; fp
++)
3904 if (flags
& fp
->flag_bit
)
3906 elt
= Fcons (intern (fp
->flag_sym
), elt
);
3907 flags
-= fp
->flag_bit
;
3910 for (fnum
= 0; flags
&& fnum
< 32; fnum
++)
3912 if (flags
& (1 << fnum
))
3914 elt
= Fcons (make_number (fnum
), elt
);
3919 res
= Fcons (elt
, res
);
3922 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3923 if (ioctl (s
, SIOCGIFHWADDR
, &rq
) == 0)
3925 Lisp_Object hwaddr
= Fmake_vector (make_number (6), Qnil
);
3926 register struct Lisp_Vector
*p
= XVECTOR (hwaddr
);
3930 for (n
= 0; n
< 6; n
++)
3931 p
->contents
[n
] = make_number (((unsigned char *)&rq
.ifr_hwaddr
.sa_data
[0])[n
]);
3932 elt
= Fcons (make_number (rq
.ifr_hwaddr
.sa_family
), hwaddr
);
3935 res
= Fcons (elt
, res
);
3938 #if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
3939 if (ioctl (s
, SIOCGIFNETMASK
, &rq
) == 0)
3942 #ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
3943 elt
= conv_sockaddr_to_lisp (&rq
.ifr_netmask
, sizeof (rq
.ifr_netmask
));
3945 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3949 res
= Fcons (elt
, res
);
3952 #if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
3953 if (ioctl (s
, SIOCGIFBRDADDR
, &rq
) == 0)
3956 elt
= conv_sockaddr_to_lisp (&rq
.ifr_broadaddr
, sizeof (rq
.ifr_broadaddr
));
3959 res
= Fcons (elt
, res
);
3962 #if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
3963 if (ioctl (s
, SIOCGIFADDR
, &rq
) == 0)
3966 elt
= conv_sockaddr_to_lisp (&rq
.ifr_addr
, sizeof (rq
.ifr_addr
));
3969 res
= Fcons (elt
, res
);
3973 return any
? res
: Qnil
;
3976 #endif /* defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H) */
3978 /* Turn off input and output for process PROC. */
3981 deactivate_process (Lisp_Object proc
)
3983 register int inchannel
, outchannel
;
3984 register struct Lisp_Process
*p
= XPROCESS (proc
);
3986 inchannel
= p
->infd
;
3987 outchannel
= p
->outfd
;
3989 #ifdef ADAPTIVE_READ_BUFFERING
3990 if (p
->read_output_delay
> 0)
3992 if (--process_output_delay_count
< 0)
3993 process_output_delay_count
= 0;
3994 p
->read_output_delay
= 0;
3995 p
->read_output_skip
= 0;
4001 /* Beware SIGCHLD hereabouts. */
4002 flush_pending_output (inchannel
);
4003 emacs_close (inchannel
);
4004 if (outchannel
>= 0 && outchannel
!= inchannel
)
4005 emacs_close (outchannel
);
4009 #ifdef DATAGRAM_SOCKETS
4010 if (DATAGRAM_CHAN_P (inchannel
))
4012 xfree (datagram_address
[inchannel
].sa
);
4013 datagram_address
[inchannel
].sa
= 0;
4014 datagram_address
[inchannel
].len
= 0;
4017 chan_process
[inchannel
] = Qnil
;
4018 FD_CLR (inchannel
, &input_wait_mask
);
4019 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
4020 #ifdef NON_BLOCKING_CONNECT
4021 if (FD_ISSET (inchannel
, &connect_wait_mask
))
4023 FD_CLR (inchannel
, &connect_wait_mask
);
4024 if (--num_pending_connects
< 0)
4028 if (inchannel
== max_process_desc
)
4031 /* We just closed the highest-numbered process input descriptor,
4032 so recompute the highest-numbered one now. */
4033 max_process_desc
= 0;
4034 for (i
= 0; i
< MAXDESC
; i
++)
4035 if (!NILP (chan_process
[i
]))
4036 max_process_desc
= i
;
4042 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
4044 doc
: /* Allow any pending output from subprocesses to be read by Emacs.
4045 It is read into the process' buffers or given to their filter functions.
4046 Non-nil arg PROCESS means do not return until some output has been received
4049 Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4050 and milliseconds to wait; return after that much time whether or not
4051 there is any subprocess output. If SECONDS is a floating point number,
4052 it specifies a fractional number of seconds to wait.
4053 The MILLISEC argument is obsolete and should be avoided.
4055 If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4056 from PROCESS, suspending reading output from other processes.
4057 If JUST-THIS-ONE is an integer, don't run any timers either.
4058 Return non-nil if we received any output before the timeout expired. */)
4059 (register Lisp_Object process
, Lisp_Object seconds
, Lisp_Object millisec
, Lisp_Object just_this_one
)
4061 int secs
, usecs
= 0;
4063 if (! NILP (process
))
4064 CHECK_PROCESS (process
);
4066 just_this_one
= Qnil
;
4068 if (!NILP (millisec
))
4069 { /* Obsolete calling convention using integers rather than floats. */
4070 CHECK_NUMBER (millisec
);
4072 seconds
= make_float (XINT (millisec
) / 1000.0);
4075 CHECK_NUMBER (seconds
);
4076 seconds
= make_float (XINT (millisec
) / 1000.0 + XINT (seconds
));
4080 if (!NILP (seconds
))
4082 if (INTEGERP (seconds
))
4083 secs
= XINT (seconds
);
4084 else if (FLOATP (seconds
))
4086 double timeout
= XFLOAT_DATA (seconds
);
4087 secs
= (int) timeout
;
4088 usecs
= (int) ((timeout
- (double) secs
) * 1000000);
4091 wrong_type_argument (Qnumberp
, seconds
);
4093 if (secs
< 0 || (secs
== 0 && usecs
== 0))
4094 secs
= -1, usecs
= 0;
4097 secs
= NILP (process
) ? -1 : 0;
4100 (wait_reading_process_output (secs
, usecs
, 0, 0,
4102 !NILP (process
) ? XPROCESS (process
) : NULL
,
4103 NILP (just_this_one
) ? 0 :
4104 !INTEGERP (just_this_one
) ? 1 : -1)
4108 /* Accept a connection for server process SERVER on CHANNEL. */
4110 static int connect_counter
= 0;
4113 server_accept_connection (Lisp_Object server
, int channel
)
4115 Lisp_Object proc
, caller
, name
, buffer
;
4116 Lisp_Object contact
, host
, service
;
4117 struct Lisp_Process
*ps
= XPROCESS (server
);
4118 struct Lisp_Process
*p
;
4122 struct sockaddr_in in
;
4124 struct sockaddr_in6 in6
;
4126 #ifdef HAVE_LOCAL_SOCKETS
4127 struct sockaddr_un un
;
4130 int len
= sizeof saddr
;
4132 s
= accept (channel
, &saddr
.sa
, &len
);
4141 if (code
== EWOULDBLOCK
)
4145 if (!NILP (ps
->log
))
4146 call3 (ps
->log
, server
, Qnil
,
4147 concat3 (build_string ("accept failed with code"),
4148 Fnumber_to_string (make_number (code
)),
4149 build_string ("\n")));
4155 /* Setup a new process to handle the connection. */
4157 /* Generate a unique identification of the caller, and build contact
4158 information for this process. */
4161 switch (saddr
.sa
.sa_family
)
4165 Lisp_Object args
[5];
4166 unsigned char *ip
= (unsigned char *)&saddr
.in
.sin_addr
.s_addr
;
4167 args
[0] = build_string ("%d.%d.%d.%d");
4168 args
[1] = make_number (*ip
++);
4169 args
[2] = make_number (*ip
++);
4170 args
[3] = make_number (*ip
++);
4171 args
[4] = make_number (*ip
++);
4172 host
= Fformat (5, args
);
4173 service
= make_number (ntohs (saddr
.in
.sin_port
));
4175 args
[0] = build_string (" <%s:%d>");
4178 caller
= Fformat (3, args
);
4185 Lisp_Object args
[9];
4186 uint16_t *ip6
= (uint16_t *)&saddr
.in6
.sin6_addr
;
4188 args
[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4189 for (i
= 0; i
< 8; i
++)
4190 args
[i
+1] = make_number (ntohs (ip6
[i
]));
4191 host
= Fformat (9, args
);
4192 service
= make_number (ntohs (saddr
.in
.sin_port
));
4194 args
[0] = build_string (" <[%s]:%d>");
4197 caller
= Fformat (3, args
);
4202 #ifdef HAVE_LOCAL_SOCKETS
4206 caller
= Fnumber_to_string (make_number (connect_counter
));
4207 caller
= concat3 (build_string (" <"), caller
, build_string (">"));
4211 /* Create a new buffer name for this process if it doesn't have a
4212 filter. The new buffer name is based on the buffer name or
4213 process name of the server process concatenated with the caller
4216 if (!NILP (ps
->filter
) && !EQ (ps
->filter
, Qt
))
4220 buffer
= ps
->buffer
;
4222 buffer
= Fbuffer_name (buffer
);
4227 buffer
= concat2 (buffer
, caller
);
4228 buffer
= Fget_buffer_create (buffer
);
4232 /* Generate a unique name for the new server process. Combine the
4233 server process name with the caller identification. */
4235 name
= concat2 (ps
->name
, caller
);
4236 proc
= make_process (name
);
4238 chan_process
[s
] = proc
;
4241 fcntl (s
, F_SETFL
, O_NONBLOCK
);
4244 fcntl (s
, F_SETFL
, O_NDELAY
);
4248 p
= XPROCESS (proc
);
4250 /* Build new contact information for this setup. */
4251 contact
= Fcopy_sequence (ps
->childp
);
4252 contact
= Fplist_put (contact
, QCserver
, Qnil
);
4253 contact
= Fplist_put (contact
, QChost
, host
);
4254 if (!NILP (service
))
4255 contact
= Fplist_put (contact
, QCservice
, service
);
4256 contact
= Fplist_put (contact
, QCremote
,
4257 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4258 #ifdef HAVE_GETSOCKNAME
4260 if (getsockname (s
, &saddr
.sa
, &len
) == 0)
4261 contact
= Fplist_put (contact
, QClocal
,
4262 conv_sockaddr_to_lisp (&saddr
.sa
, len
));
4265 p
->childp
= contact
;
4266 p
->plist
= Fcopy_sequence (ps
->plist
);
4270 p
->sentinel
= ps
->sentinel
;
4271 p
->filter
= ps
->filter
;
4278 /* Client processes for accepted connections are not stopped initially. */
4279 if (!EQ (p
->filter
, Qt
))
4281 FD_SET (s
, &input_wait_mask
);
4282 FD_SET (s
, &non_keyboard_wait_mask
);
4285 if (s
> max_process_desc
)
4286 max_process_desc
= s
;
4288 /* Setup coding system for new process based on server process.
4289 This seems to be the proper thing to do, as the coding system
4290 of the new process should reflect the settings at the time the
4291 server socket was opened; not the current settings. */
4293 p
->decode_coding_system
= ps
->decode_coding_system
;
4294 p
->encode_coding_system
= ps
->encode_coding_system
;
4295 setup_process_coding_systems (proc
);
4297 p
->decoding_buf
= make_uninit_string (0);
4298 p
->decoding_carryover
= 0;
4299 p
->encoding_buf
= make_uninit_string (0);
4301 p
->inherit_coding_system_flag
4302 = (NILP (buffer
) ? 0 : ps
->inherit_coding_system_flag
);
4304 if (!NILP (ps
->log
))
4305 call3 (ps
->log
, server
, proc
,
4306 concat3 (build_string ("accept from "),
4307 (STRINGP (host
) ? host
: build_string ("-")),
4308 build_string ("\n")));
4310 if (!NILP (p
->sentinel
))
4311 exec_sentinel (proc
,
4312 concat3 (build_string ("open from "),
4313 (STRINGP (host
) ? host
: build_string ("-")),
4314 build_string ("\n")));
4317 /* This variable is different from waiting_for_input in keyboard.c.
4318 It is used to communicate to a lisp process-filter/sentinel (via the
4319 function Fwaiting_for_user_input_p below) whether Emacs was waiting
4320 for user-input when that process-filter was called.
4321 waiting_for_input cannot be used as that is by definition 0 when
4322 lisp code is being evalled.
4323 This is also used in record_asynch_buffer_change.
4324 For that purpose, this must be 0
4325 when not inside wait_reading_process_output. */
4326 static int waiting_for_user_input_p
;
4329 wait_reading_process_output_unwind (Lisp_Object data
)
4331 waiting_for_user_input_p
= XINT (data
);
4335 /* This is here so breakpoints can be put on it. */
4337 wait_reading_process_output_1 (void)
4341 /* Use a wrapper around select to work around a bug in gdb 5.3.
4342 Normally, the wrapper is optimized away by inlining.
4344 If emacs is stopped inside select, the gdb backtrace doesn't
4345 show the function which called select, so it is practically
4346 impossible to step through wait_reading_process_output. */
4350 select_wrapper (int n
, fd_set
*rfd
, fd_set
*wfd
, fd_set
*xfd
, struct timeval
*tmo
)
4352 return select (n
, rfd
, wfd
, xfd
, tmo
);
4354 #define select select_wrapper
4357 /* Read and dispose of subprocess output while waiting for timeout to
4358 elapse and/or keyboard input to be available.
4361 timeout in seconds, or
4362 zero for no limit, or
4363 -1 means gobble data immediately available but don't wait for any.
4366 an additional duration to wait, measured in microseconds.
4367 If this is nonzero and time_limit is 0, then the timeout
4368 consists of MICROSECS only.
4370 READ_KBD is a lisp value:
4371 0 to ignore keyboard input, or
4372 1 to return when input is available, or
4373 -1 meaning caller will actually read the input, so don't throw to
4374 the quit handler, or
4376 DO_DISPLAY != 0 means redisplay should be done to show subprocess
4377 output that arrives.
4379 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4380 (and gobble terminal input into the buffer if any arrives).
4382 If WAIT_PROC is specified, wait until something arrives from that
4383 process. The return value is true if we read some input from
4386 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4387 (suspending output from other processes). A negative value
4388 means don't run any timers either.
4390 If WAIT_PROC is specified, then the function returns true if we
4391 received input from that process before the timeout elapsed.
4392 Otherwise, return true if we received input from any process. */
4395 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
4397 Lisp_Object wait_for_cell
,
4398 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
4400 register int channel
, nfds
;
4401 SELECT_TYPE Available
;
4402 #ifdef NON_BLOCKING_CONNECT
4403 SELECT_TYPE Connecting
;
4406 int check_delay
, no_avail
;
4409 EMACS_TIME timeout
, end_time
;
4410 int wait_channel
= -1;
4411 int got_some_input
= 0;
4412 int count
= SPECPDL_INDEX ();
4414 FD_ZERO (&Available
);
4415 #ifdef NON_BLOCKING_CONNECT
4416 FD_ZERO (&Connecting
);
4419 if (time_limit
== 0 && wait_proc
&& !NILP (Vinhibit_quit
)
4420 && !(CONSP (wait_proc
->status
) && EQ (XCAR (wait_proc
->status
), Qexit
)))
4421 message ("Blocking call to accept-process-output with quit inhibited!!");
4423 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4424 if (wait_proc
!= NULL
)
4425 wait_channel
= wait_proc
->infd
;
4427 record_unwind_protect (wait_reading_process_output_unwind
,
4428 make_number (waiting_for_user_input_p
));
4429 waiting_for_user_input_p
= read_kbd
;
4431 /* Since we may need to wait several times,
4432 compute the absolute time to return at. */
4433 if (time_limit
|| microsecs
)
4435 EMACS_GET_TIME (end_time
);
4436 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4437 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4442 int timeout_reduced_for_timers
= 0;
4444 /* If calling from keyboard input, do not quit
4445 since we want to return C-g as an input character.
4446 Otherwise, do pending quit if requested. */
4451 process_pending_signals ();
4454 /* Exit now if the cell we're waiting for became non-nil. */
4455 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4458 /* Compute time from now till when time limit is up */
4459 /* Exit if already run out */
4460 if (time_limit
== -1)
4462 /* -1 specified for timeout means
4463 gobble output available now
4464 but don't wait at all. */
4466 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4468 else if (time_limit
|| microsecs
)
4470 EMACS_GET_TIME (timeout
);
4471 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4472 if (EMACS_TIME_NEG_P (timeout
))
4477 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4480 /* Normally we run timers here.
4481 But not if wait_for_cell; in those cases,
4482 the wait is supposed to be short,
4483 and those callers cannot handle running arbitrary Lisp code here. */
4484 if (NILP (wait_for_cell
)
4485 && just_wait_proc
>= 0)
4487 EMACS_TIME timer_delay
;
4491 int old_timers_run
= timers_run
;
4492 struct buffer
*old_buffer
= current_buffer
;
4493 Lisp_Object old_window
= selected_window
;
4495 timer_delay
= timer_check (1);
4497 /* If a timer has run, this might have changed buffers
4498 an alike. Make read_key_sequence aware of that. */
4499 if (timers_run
!= old_timers_run
4500 && (old_buffer
!= current_buffer
4501 || !EQ (old_window
, selected_window
))
4502 && waiting_for_user_input_p
== -1)
4503 record_asynch_buffer_change ();
4505 if (timers_run
!= old_timers_run
&& do_display
)
4506 /* We must retry, since a timer may have requeued itself
4507 and that could alter the time_delay. */
4508 redisplay_preserve_echo_area (9);
4512 while (!detect_input_pending ());
4514 /* If there is unread keyboard input, also return. */
4516 && requeued_events_pending_p ())
4519 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4521 EMACS_TIME difference
;
4522 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4523 if (EMACS_TIME_NEG_P (difference
))
4525 timeout
= timer_delay
;
4526 timeout_reduced_for_timers
= 1;
4529 /* If time_limit is -1, we are not going to wait at all. */
4530 else if (time_limit
!= -1)
4532 /* This is so a breakpoint can be put here. */
4533 wait_reading_process_output_1 ();
4537 /* Cause C-g and alarm signals to take immediate action,
4538 and cause input available signals to zero out timeout.
4540 It is important that we do this before checking for process
4541 activity. If we get a SIGCHLD after the explicit checks for
4542 process activity, timeout is the only way we will know. */
4544 set_waiting_for_input (&timeout
);
4546 /* If status of something has changed, and no input is
4547 available, notify the user of the change right away. After
4548 this explicit check, we'll let the SIGCHLD handler zap
4549 timeout to get our attention. */
4550 if (update_tick
!= process_tick
)
4553 #ifdef NON_BLOCKING_CONNECT
4557 if (kbd_on_hold_p ())
4560 Atemp
= input_wait_mask
;
4561 IF_NON_BLOCKING_CONNECT (Ctemp
= connect_wait_mask
);
4563 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4564 if ((select (max (max (max_process_desc
, max_keyboard_desc
),
4567 #ifdef NON_BLOCKING_CONNECT
4568 (num_pending_connects
> 0 ? &Ctemp
: (SELECT_TYPE
*)0),
4572 (SELECT_TYPE
*)0, &timeout
)
4575 /* It's okay for us to do this and then continue with
4576 the loop, since timeout has already been zeroed out. */
4577 clear_waiting_for_input ();
4578 status_notify (NULL
);
4579 if (do_display
) redisplay_preserve_echo_area (13);
4583 /* Don't wait for output from a non-running process. Just
4584 read whatever data has already been received. */
4585 if (wait_proc
&& wait_proc
->raw_status_new
)
4586 update_status (wait_proc
);
4588 && ! EQ (wait_proc
->status
, Qrun
)
4589 && ! EQ (wait_proc
->status
, Qconnect
))
4591 int nread
, total_nread
= 0;
4593 clear_waiting_for_input ();
4594 XSETPROCESS (proc
, wait_proc
);
4596 /* Read data from the process, until we exhaust it. */
4597 while (wait_proc
->infd
>= 0)
4599 nread
= read_process_output (proc
, wait_proc
->infd
);
4606 total_nread
+= nread
;
4610 else if (nread
== -1 && EIO
== errno
)
4614 else if (nread
== -1 && EAGAIN
== errno
)
4618 else if (nread
== -1 && EWOULDBLOCK
== errno
)
4622 if (total_nread
> 0 && do_display
)
4623 redisplay_preserve_echo_area (10);
4628 /* Wait till there is something to do */
4630 if (wait_proc
&& just_wait_proc
)
4632 if (wait_proc
->infd
< 0) /* Terminated */
4634 FD_SET (wait_proc
->infd
, &Available
);
4636 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4638 else if (!NILP (wait_for_cell
))
4640 Available
= non_process_wait_mask
;
4642 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4647 Available
= non_keyboard_wait_mask
;
4649 Available
= input_wait_mask
;
4650 IF_NON_BLOCKING_CONNECT (check_connect
= (num_pending_connects
> 0));
4651 check_delay
= wait_channel
>= 0 ? 0 : process_output_delay_count
;
4654 /* If frame size has changed or the window is newly mapped,
4655 redisplay now, before we start to wait. There is a race
4656 condition here; if a SIGIO arrives between now and the select
4657 and indicates that a frame is trashed, the select may block
4658 displaying a trashed screen. */
4659 if (frame_garbaged
&& do_display
)
4661 clear_waiting_for_input ();
4662 redisplay_preserve_echo_area (11);
4664 set_waiting_for_input (&timeout
);
4668 if (read_kbd
&& detect_input_pending ())
4675 #ifdef NON_BLOCKING_CONNECT
4677 Connecting
= connect_wait_mask
;
4680 #ifdef ADAPTIVE_READ_BUFFERING
4681 /* Set the timeout for adaptive read buffering if any
4682 process has non-zero read_output_skip and non-zero
4683 read_output_delay, and we are not reading output for a
4684 specific wait_channel. It is not executed if
4685 Vprocess_adaptive_read_buffering is nil. */
4686 if (process_output_skip
&& check_delay
> 0)
4688 int usecs
= EMACS_USECS (timeout
);
4689 if (EMACS_SECS (timeout
) > 0 || usecs
> READ_OUTPUT_DELAY_MAX
)
4690 usecs
= READ_OUTPUT_DELAY_MAX
;
4691 for (channel
= 0; check_delay
> 0 && channel
<= max_process_desc
; channel
++)
4693 proc
= chan_process
[channel
];
4696 /* Find minimum non-zero read_output_delay among the
4697 processes with non-zero read_output_skip. */
4698 if (XPROCESS (proc
)->read_output_delay
> 0)
4701 if (!XPROCESS (proc
)->read_output_skip
)
4703 FD_CLR (channel
, &Available
);
4704 XPROCESS (proc
)->read_output_skip
= 0;
4705 if (XPROCESS (proc
)->read_output_delay
< usecs
)
4706 usecs
= XPROCESS (proc
)->read_output_delay
;
4709 EMACS_SET_SECS_USECS (timeout
, 0, usecs
);
4710 process_output_skip
= 0;
4713 #if defined (USE_GTK) || defined (HAVE_GCONF)
4715 #elif defined (HAVE_NS)
4720 (max (max (max_process_desc
, max_keyboard_desc
),
4723 #ifdef NON_BLOCKING_CONNECT
4724 (check_connect
? &Connecting
: (SELECT_TYPE
*)0),
4728 (SELECT_TYPE
*)0, &timeout
);
4733 /* Make C-g and alarm signals set flags again */
4734 clear_waiting_for_input ();
4736 /* If we woke up due to SIGWINCH, actually change size now. */
4737 do_pending_window_change (0);
4739 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4740 /* We wanted the full specified time, so return now. */
4744 if (xerrno
== EINTR
)
4746 else if (xerrno
== EBADF
)
4749 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4750 the child's closure of the pts gives the parent a SIGHUP, and
4751 the ptc file descriptor is automatically closed,
4752 yielding EBADF here or at select() call above.
4753 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
4754 in m/ibmrt-aix.h), and here we just ignore the select error.
4755 Cleanup occurs c/o status_notify after SIGCLD. */
4756 no_avail
= 1; /* Cannot depend on values returned */
4762 error ("select error: %s", emacs_strerror (xerrno
));
4767 FD_ZERO (&Available
);
4768 IF_NON_BLOCKING_CONNECT (check_connect
= 0);
4771 #if 0 /* When polling is used, interrupt_input is 0,
4772 so get_input_pending should read the input.
4773 So this should not be needed. */
4774 /* If we are using polling for input,
4775 and we see input available, make it get read now.
4776 Otherwise it might not actually get read for a second.
4777 And on hpux, since we turn off polling in wait_reading_process_output,
4778 it might never get read at all if we don't spend much time
4779 outside of wait_reading_process_output. */
4780 if (read_kbd
&& interrupt_input
4781 && keyboard_bit_set (&Available
)
4782 && input_polling_used ())
4783 kill (getpid (), SIGALRM
);
4786 /* Check for keyboard input */
4787 /* If there is any, return immediately
4788 to give it higher priority than subprocesses */
4792 int old_timers_run
= timers_run
;
4793 struct buffer
*old_buffer
= current_buffer
;
4794 Lisp_Object old_window
= selected_window
;
4797 if (detect_input_pending_run_timers (do_display
))
4799 swallow_events (do_display
);
4800 if (detect_input_pending_run_timers (do_display
))
4804 /* If a timer has run, this might have changed buffers
4805 an alike. Make read_key_sequence aware of that. */
4806 if (timers_run
!= old_timers_run
4807 && waiting_for_user_input_p
== -1
4808 && (old_buffer
!= current_buffer
4809 || !EQ (old_window
, selected_window
)))
4810 record_asynch_buffer_change ();
4816 /* If there is unread keyboard input, also return. */
4818 && requeued_events_pending_p ())
4821 /* If we are not checking for keyboard input now,
4822 do process events (but don't run any timers).
4823 This is so that X events will be processed.
4824 Otherwise they may have to wait until polling takes place.
4825 That would causes delays in pasting selections, for example.
4827 (We used to do this only if wait_for_cell.) */
4828 if (read_kbd
== 0 && detect_input_pending ())
4830 swallow_events (do_display
);
4831 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
4832 if (detect_input_pending ())
4837 /* Exit now if the cell we're waiting for became non-nil. */
4838 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
4842 /* If we think we have keyboard input waiting, but didn't get SIGIO,
4843 go read it. This can happen with X on BSD after logging out.
4844 In that case, there really is no input and no SIGIO,
4845 but select says there is input. */
4847 if (read_kbd
&& interrupt_input
4848 && keyboard_bit_set (&Available
) && ! noninteractive
)
4849 kill (getpid (), SIGIO
);
4853 got_some_input
|= nfds
> 0;
4855 /* If checking input just got us a size-change event from X,
4856 obey it now if we should. */
4857 if (read_kbd
|| ! NILP (wait_for_cell
))
4858 do_pending_window_change (0);
4860 /* Check for data from a process. */
4861 if (no_avail
|| nfds
== 0)
4864 /* Really FIRST_PROC_DESC should be 0 on Unix,
4865 but this is safer in the short run. */
4866 for (channel
= 0; channel
<= max_process_desc
; channel
++)
4868 if (FD_ISSET (channel
, &Available
)
4869 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
4873 /* If waiting for this channel, arrange to return as
4874 soon as no more input to be processed. No more
4876 if (wait_channel
== channel
)
4882 proc
= chan_process
[channel
];
4886 /* If this is a server stream socket, accept connection. */
4887 if (EQ (XPROCESS (proc
)->status
, Qlisten
))
4889 server_accept_connection (proc
, channel
);
4893 /* Read data from the process, starting with our
4894 buffered-ahead character if we have one. */
4896 nread
= read_process_output (proc
, channel
);
4899 /* Since read_process_output can run a filter,
4900 which can call accept-process-output,
4901 don't try to read from any other processes
4902 before doing the select again. */
4903 FD_ZERO (&Available
);
4906 redisplay_preserve_echo_area (12);
4909 else if (nread
== -1 && errno
== EWOULDBLOCK
)
4912 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
4913 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
4915 else if (nread
== -1 && errno
== EAGAIN
)
4919 else if (nread
== -1 && errno
== EAGAIN
)
4921 /* Note that we cannot distinguish between no input
4922 available now and a closed pipe.
4923 With luck, a closed pipe will be accompanied by
4924 subprocess termination and SIGCHLD. */
4925 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
4927 #endif /* O_NDELAY */
4928 #endif /* O_NONBLOCK */
4930 /* On some OSs with ptys, when the process on one end of
4931 a pty exits, the other end gets an error reading with
4932 errno = EIO instead of getting an EOF (0 bytes read).
4933 Therefore, if we get an error reading and errno =
4934 EIO, just continue, because the child process has
4935 exited and should clean itself up soon (e.g. when we
4938 However, it has been known to happen that the SIGCHLD
4939 got lost. So raise the signal again just in case.
4941 else if (nread
== -1 && errno
== EIO
)
4943 /* Clear the descriptor now, so we only raise the
4944 signal once. Don't do this if `process' is only
4946 if (XPROCESS (proc
)->pid
!= -2)
4948 FD_CLR (channel
, &input_wait_mask
);
4949 FD_CLR (channel
, &non_keyboard_wait_mask
);
4951 kill (getpid (), SIGCHLD
);
4954 #endif /* HAVE_PTYS */
4955 /* If we can detect process termination, don't consider the process
4956 gone just because its pipe is closed. */
4958 else if (nread
== 0 && !NETCONN_P (proc
) && !SERIALCONN_P (proc
))
4963 /* Preserve status of processes already terminated. */
4964 XPROCESS (proc
)->tick
= ++process_tick
;
4965 deactivate_process (proc
);
4966 if (XPROCESS (proc
)->raw_status_new
)
4967 update_status (XPROCESS (proc
));
4968 if (EQ (XPROCESS (proc
)->status
, Qrun
))
4969 XPROCESS (proc
)->status
4970 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
4973 #ifdef NON_BLOCKING_CONNECT
4974 if (check_connect
&& FD_ISSET (channel
, &Connecting
)
4975 && FD_ISSET (channel
, &connect_wait_mask
))
4977 struct Lisp_Process
*p
;
4979 FD_CLR (channel
, &connect_wait_mask
);
4980 if (--num_pending_connects
< 0)
4983 proc
= chan_process
[channel
];
4987 p
= XPROCESS (proc
);
4990 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
4991 So only use it on systems where it is known to work. */
4993 int xlen
= sizeof (xerrno
);
4994 if (getsockopt (channel
, SOL_SOCKET
, SO_ERROR
, &xerrno
, &xlen
))
4999 struct sockaddr pname
;
5000 int pnamelen
= sizeof (pname
);
5002 /* If connection failed, getpeername will fail. */
5004 if (getpeername (channel
, &pname
, &pnamelen
) < 0)
5006 /* Obtain connect failure code through error slippage. */
5009 if (errno
== ENOTCONN
&& read (channel
, &dummy
, 1) < 0)
5016 p
->tick
= ++process_tick
;
5017 p
->status
= Fcons (Qfailed
, Fcons (make_number (xerrno
), Qnil
));
5018 deactivate_process (proc
);
5023 /* Execute the sentinel here. If we had relied on
5024 status_notify to do it later, it will read input
5025 from the process before calling the sentinel. */
5026 exec_sentinel (proc
, build_string ("open\n"));
5027 if (!EQ (p
->filter
, Qt
) && !EQ (p
->command
, Qt
))
5029 FD_SET (p
->infd
, &input_wait_mask
);
5030 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
5034 #endif /* NON_BLOCKING_CONNECT */
5035 } /* end for each file descriptor */
5036 } /* end while exit conditions not met */
5038 unbind_to (count
, Qnil
);
5040 /* If calling from keyboard input, do not quit
5041 since we want to return C-g as an input character.
5042 Otherwise, do pending quit if requested. */
5045 /* Prevent input_pending from remaining set if we quit. */
5046 clear_input_pending ();
5050 return got_some_input
;
5053 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5056 read_process_output_call (Lisp_Object fun_and_args
)
5058 return apply1 (XCAR (fun_and_args
), XCDR (fun_and_args
));
5062 read_process_output_error_handler (Lisp_Object error
)
5064 cmd_error_internal (error
, "error in process filter: ");
5066 update_echo_area ();
5067 Fsleep_for (make_number (2), Qnil
);
5071 /* Read pending output from the process channel,
5072 starting with our buffered-ahead character if we have one.
5073 Yield number of decoded characters read.
5075 This function reads at most 4096 characters.
5076 If you want to read all available subprocess output,
5077 you must call it repeatedly until it returns zero.
5079 The characters read are decoded according to PROC's coding-system
5083 read_process_output (Lisp_Object proc
, register int channel
)
5085 register int nbytes
;
5087 register Lisp_Object outstream
;
5088 register struct Lisp_Process
*p
= XPROCESS (proc
);
5089 register int opoint
;
5090 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
5091 int carryover
= p
->decoding_carryover
;
5093 int count
= SPECPDL_INDEX ();
5094 Lisp_Object odeactivate
;
5096 chars
= (char *) alloca (carryover
+ readmax
);
5098 /* See the comment above. */
5099 memcpy (chars
, SDATA (p
->decoding_buf
), carryover
);
5101 #ifdef DATAGRAM_SOCKETS
5102 /* We have a working select, so proc_buffered_char is always -1. */
5103 if (DATAGRAM_CHAN_P (channel
))
5105 int len
= datagram_address
[channel
].len
;
5106 nbytes
= recvfrom (channel
, chars
+ carryover
, readmax
,
5107 0, datagram_address
[channel
].sa
, &len
);
5111 if (proc_buffered_char
[channel
] < 0)
5113 nbytes
= emacs_read (channel
, chars
+ carryover
, readmax
);
5114 #ifdef ADAPTIVE_READ_BUFFERING
5115 if (nbytes
> 0 && p
->adaptive_read_buffering
)
5117 int delay
= p
->read_output_delay
;
5120 if (delay
< READ_OUTPUT_DELAY_MAX_MAX
)
5123 process_output_delay_count
++;
5124 delay
+= READ_OUTPUT_DELAY_INCREMENT
* 2;
5127 else if (delay
> 0 && (nbytes
== readmax
))
5129 delay
-= READ_OUTPUT_DELAY_INCREMENT
;
5131 process_output_delay_count
--;
5133 p
->read_output_delay
= delay
;
5136 p
->read_output_skip
= 1;
5137 process_output_skip
= 1;
5144 chars
[carryover
] = proc_buffered_char
[channel
];
5145 proc_buffered_char
[channel
] = -1;
5146 nbytes
= emacs_read (channel
, chars
+ carryover
+ 1, readmax
- 1);
5150 nbytes
= nbytes
+ 1;
5153 p
->decoding_carryover
= 0;
5155 /* At this point, NBYTES holds number of bytes just received
5156 (including the one in proc_buffered_char[channel]). */
5159 if (nbytes
< 0 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5161 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5164 /* Now set NBYTES how many bytes we must decode. */
5165 nbytes
+= carryover
;
5167 odeactivate
= Vdeactivate_mark
;
5168 /* There's no good reason to let process filters change the current
5169 buffer, and many callers of accept-process-output, sit-for, and
5170 friends don't expect current-buffer to be changed from under them. */
5171 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
5173 /* Read and dispose of the process output. */
5174 outstream
= p
->filter
;
5175 if (!NILP (outstream
))
5177 Lisp_Object obuffer
, okeymap
;
5179 int outer_running_asynch_code
= running_asynch_code
;
5180 int waiting
= waiting_for_user_input_p
;
5182 /* No need to gcpro these, because all we do with them later
5183 is test them for EQness, and none of them should be a string. */
5184 XSETBUFFER (obuffer
, current_buffer
);
5185 okeymap
= current_buffer
->keymap
;
5187 /* We inhibit quit here instead of just catching it so that
5188 hitting ^G when a filter happens to be running won't screw
5190 specbind (Qinhibit_quit
, Qt
);
5191 specbind (Qlast_nonmenu_event
, Qt
);
5193 /* In case we get recursively called,
5194 and we already saved the match data nonrecursively,
5195 save the same match data in safely recursive fashion. */
5196 if (outer_running_asynch_code
)
5199 /* Don't clobber the CURRENT match data, either! */
5200 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
5201 restore_search_regs ();
5202 record_unwind_save_match_data ();
5203 Fset_match_data (tem
, Qt
);
5206 /* For speed, if a search happens within this code,
5207 save the match data in a special nonrecursive fashion. */
5208 running_asynch_code
= 1;
5210 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5211 text
= coding
->dst_object
;
5212 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5213 /* A new coding system might be found. */
5214 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5216 p
->decode_coding_system
= Vlast_coding_system_used
;
5218 /* Don't call setup_coding_system for
5219 proc_decode_coding_system[channel] here. It is done in
5220 detect_coding called via decode_coding above. */
5222 /* If a coding system for encoding is not yet decided, we set
5223 it as the same as coding-system for decoding.
5225 But, before doing that we must check if
5226 proc_encode_coding_system[p->outfd] surely points to a
5227 valid memory because p->outfd will be changed once EOF is
5228 sent to the process. */
5229 if (NILP (p
->encode_coding_system
)
5230 && proc_encode_coding_system
[p
->outfd
])
5232 p
->encode_coding_system
5233 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5234 setup_coding_system (p
->encode_coding_system
,
5235 proc_encode_coding_system
[p
->outfd
]);
5239 if (coding
->carryover_bytes
> 0)
5241 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5242 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5243 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5244 coding
->carryover_bytes
);
5245 p
->decoding_carryover
= coding
->carryover_bytes
;
5247 if (SBYTES (text
) > 0)
5248 internal_condition_case_1 (read_process_output_call
,
5250 Fcons (proc
, Fcons (text
, Qnil
))),
5251 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
5252 read_process_output_error_handler
);
5254 /* If we saved the match data nonrecursively, restore it now. */
5255 restore_search_regs ();
5256 running_asynch_code
= outer_running_asynch_code
;
5258 /* Restore waiting_for_user_input_p as it was
5259 when we were called, in case the filter clobbered it. */
5260 waiting_for_user_input_p
= waiting
;
5262 #if 0 /* Call record_asynch_buffer_change unconditionally,
5263 because we might have changed minor modes or other things
5264 that affect key bindings. */
5265 if (! EQ (Fcurrent_buffer (), obuffer
)
5266 || ! EQ (current_buffer
->keymap
, okeymap
))
5268 /* But do it only if the caller is actually going to read events.
5269 Otherwise there's no need to make him wake up, and it could
5270 cause trouble (for example it would make sit_for return). */
5271 if (waiting_for_user_input_p
== -1)
5272 record_asynch_buffer_change ();
5275 /* If no filter, write into buffer if it isn't dead. */
5276 else if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
5278 Lisp_Object old_read_only
;
5279 int old_begv
, old_zv
;
5280 int old_begv_byte
, old_zv_byte
;
5281 int before
, before_byte
;
5286 Fset_buffer (p
->buffer
);
5288 opoint_byte
= PT_BYTE
;
5289 old_read_only
= current_buffer
->read_only
;
5292 old_begv_byte
= BEGV_BYTE
;
5293 old_zv_byte
= ZV_BYTE
;
5295 current_buffer
->read_only
= Qnil
;
5297 /* Insert new output into buffer
5298 at the current end-of-output marker,
5299 thus preserving logical ordering of input and output. */
5300 if (XMARKER (p
->mark
)->buffer
)
5301 SET_PT_BOTH (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
),
5302 clip_to_bounds (BEGV_BYTE
, marker_byte_position (p
->mark
),
5305 SET_PT_BOTH (ZV
, ZV_BYTE
);
5307 before_byte
= PT_BYTE
;
5309 /* If the output marker is outside of the visible region, save
5310 the restriction and widen. */
5311 if (! (BEGV
<= PT
&& PT
<= ZV
))
5314 decode_coding_c_string (coding
, chars
, nbytes
, Qt
);
5315 text
= coding
->dst_object
;
5316 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5317 /* A new coding system might be found. See the comment in the
5318 similar code in the previous `if' block. */
5319 if (!EQ (p
->decode_coding_system
, Vlast_coding_system_used
))
5321 p
->decode_coding_system
= Vlast_coding_system_used
;
5322 if (NILP (p
->encode_coding_system
)
5323 && proc_encode_coding_system
[p
->outfd
])
5325 p
->encode_coding_system
5326 = coding_inherit_eol_type (Vlast_coding_system_used
, Qnil
);
5327 setup_coding_system (p
->encode_coding_system
,
5328 proc_encode_coding_system
[p
->outfd
]);
5331 if (coding
->carryover_bytes
> 0)
5333 if (SCHARS (p
->decoding_buf
) < coding
->carryover_bytes
)
5334 p
->decoding_buf
= make_uninit_string (coding
->carryover_bytes
);
5335 memcpy (SDATA (p
->decoding_buf
), coding
->carryover
,
5336 coding
->carryover_bytes
);
5337 p
->decoding_carryover
= coding
->carryover_bytes
;
5339 /* Adjust the multibyteness of TEXT to that of the buffer. */
5340 if (NILP (current_buffer
->enable_multibyte_characters
)
5341 != ! STRING_MULTIBYTE (text
))
5342 text
= (STRING_MULTIBYTE (text
)
5343 ? Fstring_as_unibyte (text
)
5344 : Fstring_to_multibyte (text
));
5345 /* Insert before markers in case we are inserting where
5346 the buffer's mark is, and the user's next command is Meta-y. */
5347 insert_from_string_before_markers (text
, 0, 0,
5348 SCHARS (text
), SBYTES (text
), 0);
5350 /* Make sure the process marker's position is valid when the
5351 process buffer is changed in the signal_after_change above.
5352 W3 is known to do that. */
5353 if (BUFFERP (p
->buffer
)
5354 && (b
= XBUFFER (p
->buffer
), b
!= current_buffer
))
5355 set_marker_both (p
->mark
, p
->buffer
, BUF_PT (b
), BUF_PT_BYTE (b
));
5357 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
5359 update_mode_lines
++;
5361 /* Make sure opoint and the old restrictions
5362 float ahead of any new text just as point would. */
5363 if (opoint
>= before
)
5365 opoint
+= PT
- before
;
5366 opoint_byte
+= PT_BYTE
- before_byte
;
5368 if (old_begv
> before
)
5370 old_begv
+= PT
- before
;
5371 old_begv_byte
+= PT_BYTE
- before_byte
;
5373 if (old_zv
>= before
)
5375 old_zv
+= PT
- before
;
5376 old_zv_byte
+= PT_BYTE
- before_byte
;
5379 /* If the restriction isn't what it should be, set it. */
5380 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
5381 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
5384 current_buffer
->read_only
= old_read_only
;
5385 SET_PT_BOTH (opoint
, opoint_byte
);
5387 /* Handling the process output should not deactivate the mark. */
5388 Vdeactivate_mark
= odeactivate
;
5390 unbind_to (count
, Qnil
);
5394 /* Sending data to subprocess */
5396 jmp_buf send_process_frame
;
5397 Lisp_Object process_sent_to
;
5400 send_process_trap (int ignore
)
5402 SIGNAL_THREAD_CHECK (SIGPIPE
);
5403 sigunblock (sigmask (SIGPIPE
));
5404 longjmp (send_process_frame
, 1);
5407 /* Send some data to process PROC.
5408 BUF is the beginning of the data; LEN is the number of characters.
5409 OBJECT is the Lisp object that the data comes from. If OBJECT is
5410 nil or t, it means that the data comes from C string.
5412 If OBJECT is not nil, the data is encoded by PROC's coding-system
5413 for encoding before it is sent.
5415 This function can evaluate Lisp code and can garbage collect. */
5418 send_process (volatile Lisp_Object proc
, unsigned char *volatile buf
,
5419 volatile int len
, volatile Lisp_Object object
)
5421 /* Use volatile to protect variables from being clobbered by longjmp. */
5422 struct Lisp_Process
*p
= XPROCESS (proc
);
5424 struct coding_system
*coding
;
5425 struct gcpro gcpro1
;
5426 SIGTYPE (*volatile old_sigpipe
) (int);
5430 if (p
->raw_status_new
)
5432 if (! EQ (p
->status
, Qrun
))
5433 error ("Process %s not running", SDATA (p
->name
));
5435 error ("Output file descriptor of %s is closed", SDATA (p
->name
));
5437 coding
= proc_encode_coding_system
[p
->outfd
];
5438 Vlast_coding_system_used
= CODING_ID_NAME (coding
->id
);
5440 if ((STRINGP (object
) && STRING_MULTIBYTE (object
))
5441 || (BUFFERP (object
)
5442 && !NILP (XBUFFER (object
)->enable_multibyte_characters
))
5445 if (!EQ (Vlast_coding_system_used
, p
->encode_coding_system
))
5446 /* The coding system for encoding was changed to raw-text
5447 because we sent a unibyte text previously. Now we are
5448 sending a multibyte text, thus we must encode it by the
5449 original coding system specified for the current process. */
5450 setup_coding_system (p
->encode_coding_system
, coding
);
5451 coding
->src_multibyte
= 1;
5455 /* For sending a unibyte text, character code conversion should
5456 not take place but EOL conversion should. So, setup raw-text
5457 or one of the subsidiary if we have not yet done it. */
5458 if (CODING_REQUIRE_ENCODING (coding
))
5460 if (CODING_REQUIRE_FLUSHING (coding
))
5462 /* But, before changing the coding, we must flush out data. */
5463 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5464 send_process (proc
, "", 0, Qt
);
5465 coding
->mode
&= CODING_MODE_LAST_BLOCK
;
5467 setup_coding_system (raw_text_coding_system
5468 (Vlast_coding_system_used
),
5470 coding
->src_multibyte
= 0;
5473 coding
->dst_multibyte
= 0;
5475 if (CODING_REQUIRE_ENCODING (coding
))
5477 coding
->dst_object
= Qt
;
5478 if (BUFFERP (object
))
5480 int from_byte
, from
, to
;
5481 int save_pt
, save_pt_byte
;
5482 struct buffer
*cur
= current_buffer
;
5484 set_buffer_internal (XBUFFER (object
));
5485 save_pt
= PT
, save_pt_byte
= PT_BYTE
;
5487 from_byte
= PTR_BYTE_POS (buf
);
5488 from
= BYTE_TO_CHAR (from_byte
);
5489 to
= BYTE_TO_CHAR (from_byte
+ len
);
5490 TEMP_SET_PT_BOTH (from
, from_byte
);
5491 encode_coding_object (coding
, object
, from
, from_byte
,
5492 to
, from_byte
+ len
, Qt
);
5493 TEMP_SET_PT_BOTH (save_pt
, save_pt_byte
);
5494 set_buffer_internal (cur
);
5496 else if (STRINGP (object
))
5498 encode_coding_object (coding
, object
, 0, 0, SCHARS (object
),
5499 SBYTES (object
), Qt
);
5503 coding
->dst_object
= make_unibyte_string (buf
, len
);
5504 coding
->produced
= len
;
5507 len
= coding
->produced
;
5508 object
= coding
->dst_object
;
5509 buf
= SDATA (object
);
5512 if (pty_max_bytes
== 0)
5514 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
5515 pty_max_bytes
= fpathconf (p
->outfd
, _PC_MAX_CANON
);
5516 if (pty_max_bytes
< 0)
5517 pty_max_bytes
= 250;
5519 pty_max_bytes
= 250;
5521 /* Deduct one, to leave space for the eof. */
5525 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5526 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5527 when returning with longjmp despite being declared volatile. */
5528 if (!setjmp (send_process_frame
))
5530 process_sent_to
= proc
;
5535 /* Send this batch, using one or more write calls. */
5538 int outfd
= p
->outfd
;
5539 old_sigpipe
= (SIGTYPE (*) (int)) signal (SIGPIPE
, send_process_trap
);
5540 #ifdef DATAGRAM_SOCKETS
5541 if (DATAGRAM_CHAN_P (outfd
))
5543 rv
= sendto (outfd
, (char *) buf
, this,
5544 0, datagram_address
[outfd
].sa
,
5545 datagram_address
[outfd
].len
);
5546 if (rv
< 0 && errno
== EMSGSIZE
)
5548 signal (SIGPIPE
, old_sigpipe
);
5549 report_file_error ("sending datagram",
5550 Fcons (proc
, Qnil
));
5556 rv
= emacs_write (outfd
, (char *) buf
, this);
5557 #ifdef ADAPTIVE_READ_BUFFERING
5558 if (p
->read_output_delay
> 0
5559 && p
->adaptive_read_buffering
== 1)
5561 p
->read_output_delay
= 0;
5562 process_output_delay_count
--;
5563 p
->read_output_skip
= 0;
5567 signal (SIGPIPE
, old_sigpipe
);
5573 || errno
== EWOULDBLOCK
5579 /* Buffer is full. Wait, accepting input;
5580 that may allow the program
5581 to finish doing output and read more. */
5585 #ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5586 /* A gross hack to work around a bug in FreeBSD.
5587 In the following sequence, read(2) returns
5591 write(2) 954 bytes, get EAGAIN
5592 read(2) 1024 bytes in process_read_output
5593 read(2) 11 bytes in process_read_output
5595 That is, read(2) returns more bytes than have
5596 ever been written successfully. The 1033 bytes
5597 read are the 1022 bytes written successfully
5598 after processing (for example with CRs added if
5599 the terminal is set up that way which it is
5600 here). The same bytes will be seen again in a
5601 later read(2), without the CRs. */
5603 if (errno
== EAGAIN
)
5606 ioctl (p
->outfd
, TIOCFLUSH
, &flags
);
5608 #endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5610 /* Running filters might relocate buffers or strings.
5611 Arrange to relocate BUF. */
5612 if (BUFFERP (object
))
5613 offset
= BUF_PTR_BYTE_POS (XBUFFER (object
), buf
);
5614 else if (STRINGP (object
))
5615 offset
= buf
- SDATA (object
);
5617 #ifdef EMACS_HAS_USECS
5618 wait_reading_process_output (0, 20000, 0, 0, Qnil
, NULL
, 0);
5620 wait_reading_process_output (1, 0, 0, 0, Qnil
, NULL
, 0);
5623 if (BUFFERP (object
))
5624 buf
= BUF_BYTE_ADDRESS (XBUFFER (object
), offset
);
5625 else if (STRINGP (object
))
5626 buf
= offset
+ SDATA (object
);
5631 /* This is a real error. */
5632 report_file_error ("writing to process", Fcons (proc
, Qnil
));
5642 signal (SIGPIPE
, old_sigpipe
);
5643 proc
= process_sent_to
;
5644 p
= XPROCESS (proc
);
5645 p
->raw_status_new
= 0;
5646 p
->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
5647 p
->tick
= ++process_tick
;
5648 deactivate_process (proc
);
5649 error ("SIGPIPE raised on process %s; closed it", SDATA (p
->name
));
5655 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
5657 doc
: /* Send current contents of region as input to PROCESS.
5658 PROCESS may be a process, a buffer, the name of a process or buffer, or
5659 nil, indicating the current buffer's process.
5660 Called from program, takes three arguments, PROCESS, START and END.
5661 If the region is more than 500 characters long,
5662 it is sent in several bunches. This may happen even for shorter regions.
5663 Output from processes can arrive in between bunches. */)
5664 (Lisp_Object process
, Lisp_Object start
, Lisp_Object end
)
5669 proc
= get_process (process
);
5670 validate_region (&start
, &end
);
5672 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
5673 move_gap (XINT (start
));
5675 start1
= CHAR_TO_BYTE (XINT (start
));
5676 end1
= CHAR_TO_BYTE (XINT (end
));
5677 send_process (proc
, BYTE_POS_ADDR (start1
), end1
- start1
,
5678 Fcurrent_buffer ());
5683 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
5685 doc
: /* Send PROCESS the contents of STRING as input.
5686 PROCESS may be a process, a buffer, the name of a process or buffer, or
5687 nil, indicating the current buffer's process.
5688 If STRING is more than 500 characters long,
5689 it is sent in several bunches. This may happen even for shorter strings.
5690 Output from processes can arrive in between bunches. */)
5691 (Lisp_Object process
, Lisp_Object string
)
5694 CHECK_STRING (string
);
5695 proc
= get_process (process
);
5696 send_process (proc
, SDATA (string
),
5697 SBYTES (string
), string
);
5701 /* Return the foreground process group for the tty/pty that
5702 the process P uses. */
5704 emacs_get_tty_pgrp (struct Lisp_Process
*p
)
5709 if (ioctl (p
->infd
, TIOCGPGRP
, &gid
) == -1 && ! NILP (p
->tty_name
))
5712 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5713 master side. Try the slave side. */
5714 fd
= emacs_open (SDATA (p
->tty_name
), O_RDONLY
, 0);
5718 ioctl (fd
, TIOCGPGRP
, &gid
);
5722 #endif /* defined (TIOCGPGRP ) */
5727 DEFUN ("process-running-child-p", Fprocess_running_child_p
,
5728 Sprocess_running_child_p
, 0, 1, 0,
5729 doc
: /* Return t if PROCESS has given the terminal to a child.
5730 If the operating system does not make it possible to find out,
5731 return t unconditionally. */)
5732 (Lisp_Object process
)
5734 /* Initialize in case ioctl doesn't exist or gives an error,
5735 in a way that will cause returning t. */
5738 struct Lisp_Process
*p
;
5740 proc
= get_process (process
);
5741 p
= XPROCESS (proc
);
5743 if (!EQ (p
->type
, Qreal
))
5744 error ("Process %s is not a subprocess",
5747 error ("Process %s is not active",
5750 gid
= emacs_get_tty_pgrp (p
);
5757 /* send a signal number SIGNO to PROCESS.
5758 If CURRENT_GROUP is t, that means send to the process group
5759 that currently owns the terminal being used to communicate with PROCESS.
5760 This is used for various commands in shell mode.
5761 If CURRENT_GROUP is lambda, that means send to the process group
5762 that currently owns the terminal, but only if it is NOT the shell itself.
5764 If NOMSG is zero, insert signal-announcements into process's buffers
5767 If we can, we try to signal PROCESS by sending control characters
5768 down the pty. This allows us to signal inferiors who have changed
5769 their uid, for which killpg would return an EPERM error. */
5772 process_send_signal (Lisp_Object process
, int signo
, Lisp_Object current_group
,
5776 register struct Lisp_Process
*p
;
5780 proc
= get_process (process
);
5781 p
= XPROCESS (proc
);
5783 if (!EQ (p
->type
, Qreal
))
5784 error ("Process %s is not a subprocess",
5787 error ("Process %s is not active",
5791 current_group
= Qnil
;
5793 /* If we are using pgrps, get a pgrp number and make it negative. */
5794 if (NILP (current_group
))
5795 /* Send the signal to the shell's process group. */
5799 #ifdef SIGNALS_VIA_CHARACTERS
5800 /* If possible, send signals to the entire pgrp
5801 by sending an input character to it. */
5803 /* TERMIOS is the latest and bestest, and seems most likely to
5804 work. If the system has it, use it. */
5807 cc_t
*sig_char
= NULL
;
5809 tcgetattr (p
->infd
, &t
);
5814 sig_char
= &t
.c_cc
[VINTR
];
5818 sig_char
= &t
.c_cc
[VQUIT
];
5822 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
5823 sig_char
= &t
.c_cc
[VSWTCH
];
5825 sig_char
= &t
.c_cc
[VSUSP
];
5830 if (sig_char
&& *sig_char
!= CDISABLE
)
5832 send_process (proc
, sig_char
, 1, Qnil
);
5835 /* If we can't send the signal with a character,
5836 fall through and send it another way. */
5837 #else /* ! HAVE_TERMIOS */
5839 /* On Berkeley descendants, the following IOCTL's retrieve the
5840 current control characters. */
5841 #if defined (TIOCGLTC) && defined (TIOCGETC)
5849 ioctl (p
->infd
, TIOCGETC
, &c
);
5850 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
5853 ioctl (p
->infd
, TIOCGETC
, &c
);
5854 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
5858 ioctl (p
->infd
, TIOCGLTC
, &lc
);
5859 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
5861 #endif /* ! defined (SIGTSTP) */
5864 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5866 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
5873 ioctl (p
->infd
, TCGETA
, &t
);
5874 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
5877 ioctl (p
->infd
, TCGETA
, &t
);
5878 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
5882 ioctl (p
->infd
, TCGETA
, &t
);
5883 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
5885 #endif /* ! defined (SIGTSTP) */
5887 #else /* ! defined (TCGETA) */
5888 Your configuration files are messed up
.
5889 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
5890 you'd better be using one of the alternatives above! */
5891 #endif /* ! defined (TCGETA) */
5892 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
5893 /* In this case, the code above should alway return. */
5895 #endif /* ! defined HAVE_TERMIOS */
5897 /* The code above may fall through if it can't
5898 handle the signal. */
5899 #endif /* defined (SIGNALS_VIA_CHARACTERS) */
5902 /* Get the current pgrp using the tty itself, if we have that.
5903 Otherwise, use the pty to get the pgrp.
5904 On pfa systems, saka@pfu.fujitsu.co.JP writes:
5905 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
5906 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
5907 His patch indicates that if TIOCGPGRP returns an error, then
5908 we should just assume that p->pid is also the process group id. */
5910 gid
= emacs_get_tty_pgrp (p
);
5913 /* If we can't get the information, assume
5914 the shell owns the tty. */
5917 /* It is not clear whether anything really can set GID to -1.
5918 Perhaps on some system one of those ioctls can or could do so.
5919 Or perhaps this is vestigial. */
5922 #else /* ! defined (TIOCGPGRP ) */
5923 /* Can't select pgrps on this system, so we know that
5924 the child itself heads the pgrp. */
5926 #endif /* ! defined (TIOCGPGRP ) */
5928 /* If current_group is lambda, and the shell owns the terminal,
5929 don't send any signal. */
5930 if (EQ (current_group
, Qlambda
) && gid
== p
->pid
)
5938 p
->raw_status_new
= 0;
5940 p
->tick
= ++process_tick
;
5943 status_notify (NULL
);
5944 redisplay_preserve_echo_area (13);
5947 #endif /* ! defined (SIGCONT) */
5951 flush_pending_output (p
->infd
);
5955 /* If we don't have process groups, send the signal to the immediate
5956 subprocess. That isn't really right, but it's better than any
5957 obvious alternative. */
5960 kill (p
->pid
, signo
);
5964 /* gid may be a pid, or minus a pgrp's number */
5966 if (!NILP (current_group
))
5968 if (ioctl (p
->infd
, TIOCSIGSEND
, signo
) == -1)
5969 EMACS_KILLPG (gid
, signo
);
5976 #else /* ! defined (TIOCSIGSEND) */
5977 EMACS_KILLPG (gid
, signo
);
5978 #endif /* ! defined (TIOCSIGSEND) */
5981 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
5982 doc
: /* Interrupt process PROCESS.
5983 PROCESS may be a process, a buffer, or the name of a process or buffer.
5984 No arg or nil means current buffer's process.
5985 Second arg CURRENT-GROUP non-nil means send signal to
5986 the current process-group of the process's controlling terminal
5987 rather than to the process's own process group.
5988 If the process is a shell, this means interrupt current subjob
5989 rather than the shell.
5991 If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
5992 don't send the signal. */)
5993 (Lisp_Object process
, Lisp_Object current_group
)
5995 process_send_signal (process
, SIGINT
, current_group
, 0);
5999 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
6000 doc
: /* Kill process PROCESS. May be process or name of one.
6001 See function `interrupt-process' for more details on usage. */)
6002 (Lisp_Object process
, Lisp_Object current_group
)
6004 process_send_signal (process
, SIGKILL
, current_group
, 0);
6008 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
6009 doc
: /* Send QUIT signal to process PROCESS. May be process or name of one.
6010 See function `interrupt-process' for more details on usage. */)
6011 (Lisp_Object process
, Lisp_Object current_group
)
6013 process_send_signal (process
, SIGQUIT
, current_group
, 0);
6017 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
6018 doc
: /* Stop process PROCESS. May be process or name of one.
6019 See function `interrupt-process' for more details on usage.
6020 If PROCESS is a network or serial process, inhibit handling of incoming
6022 (Lisp_Object process
, Lisp_Object current_group
)
6024 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6026 struct Lisp_Process
*p
;
6028 p
= XPROCESS (process
);
6029 if (NILP (p
->command
)
6032 FD_CLR (p
->infd
, &input_wait_mask
);
6033 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6039 error ("No SIGTSTP support");
6041 process_send_signal (process
, SIGTSTP
, current_group
, 0);
6046 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
6047 doc
: /* Continue process PROCESS. May be process or name of one.
6048 See function `interrupt-process' for more details on usage.
6049 If PROCESS is a network or serial process, resume handling of incoming
6051 (Lisp_Object process
, Lisp_Object current_group
)
6053 if (PROCESSP (process
) && (NETCONN_P (process
) || SERIALCONN_P (process
)))
6055 struct Lisp_Process
*p
;
6057 p
= XPROCESS (process
);
6058 if (EQ (p
->command
, Qt
)
6060 && (!EQ (p
->filter
, Qt
) || EQ (p
->status
, Qlisten
)))
6062 FD_SET (p
->infd
, &input_wait_mask
);
6063 FD_SET (p
->infd
, &non_keyboard_wait_mask
);
6065 if (fd_info
[ p
->infd
].flags
& FILE_SERIAL
)
6066 PurgeComm (fd_info
[ p
->infd
].hnd
, PURGE_RXABORT
| PURGE_RXCLEAR
);
6069 tcflush (p
->infd
, TCIFLUSH
);
6076 process_send_signal (process
, SIGCONT
, current_group
, 0);
6078 error ("No SIGCONT support");
6083 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
6084 2, 2, "sProcess (name or number): \nnSignal code: ",
6085 doc
: /* Send PROCESS the signal with code SIGCODE.
6086 PROCESS may also be a number specifying the process id of the
6087 process to signal; in this case, the process need not be a child of
6089 SIGCODE may be an integer, or a symbol whose name is a signal name. */)
6090 (Lisp_Object process
, Lisp_Object sigcode
)
6094 if (INTEGERP (process
))
6096 pid
= XINT (process
);
6100 if (FLOATP (process
))
6102 pid
= (pid_t
) XFLOAT_DATA (process
);
6106 if (STRINGP (process
))
6109 if (tem
= Fget_process (process
), NILP (tem
))
6111 pid
= XINT (Fstring_to_number (process
, make_number (10)));
6118 process
= get_process (process
);
6123 CHECK_PROCESS (process
);
6124 pid
= XPROCESS (process
)->pid
;
6126 error ("Cannot signal process %s", SDATA (XPROCESS (process
)->name
));
6130 #define parse_signal(NAME, VALUE) \
6131 else if (!xstrcasecmp (name, NAME)) \
6132 XSETINT (sigcode, VALUE)
6134 if (INTEGERP (sigcode
))
6138 unsigned char *name
;
6140 CHECK_SYMBOL (sigcode
);
6141 name
= SDATA (SYMBOL_NAME (sigcode
));
6143 if (!strncmp (name
, "SIG", 3) || !strncmp (name
, "sig", 3))
6149 parse_signal ("usr1", SIGUSR1
);
6152 parse_signal ("usr2", SIGUSR2
);
6155 parse_signal ("term", SIGTERM
);
6158 parse_signal ("hup", SIGHUP
);
6161 parse_signal ("int", SIGINT
);
6164 parse_signal ("quit", SIGQUIT
);
6167 parse_signal ("ill", SIGILL
);
6170 parse_signal ("abrt", SIGABRT
);
6173 parse_signal ("emt", SIGEMT
);
6176 parse_signal ("kill", SIGKILL
);
6179 parse_signal ("fpe", SIGFPE
);
6182 parse_signal ("bus", SIGBUS
);
6185 parse_signal ("segv", SIGSEGV
);
6188 parse_signal ("sys", SIGSYS
);
6191 parse_signal ("pipe", SIGPIPE
);
6194 parse_signal ("alrm", SIGALRM
);
6197 parse_signal ("urg", SIGURG
);
6200 parse_signal ("stop", SIGSTOP
);
6203 parse_signal ("tstp", SIGTSTP
);
6206 parse_signal ("cont", SIGCONT
);
6209 parse_signal ("chld", SIGCHLD
);
6212 parse_signal ("ttin", SIGTTIN
);
6215 parse_signal ("ttou", SIGTTOU
);
6218 parse_signal ("io", SIGIO
);
6221 parse_signal ("xcpu", SIGXCPU
);
6224 parse_signal ("xfsz", SIGXFSZ
);
6227 parse_signal ("vtalrm", SIGVTALRM
);
6230 parse_signal ("prof", SIGPROF
);
6233 parse_signal ("winch", SIGWINCH
);
6236 parse_signal ("info", SIGINFO
);
6239 error ("Undefined signal name %s", name
);
6244 return make_number (kill (pid
, XINT (sigcode
)));
6247 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
6248 doc
: /* Make PROCESS see end-of-file in its input.
6249 EOF comes after any text already sent to it.
6250 PROCESS may be a process, a buffer, the name of a process or buffer, or
6251 nil, indicating the current buffer's process.
6252 If PROCESS is a network connection, or is a process communicating
6253 through a pipe (as opposed to a pty), then you cannot send any more
6254 text to PROCESS after you call this function.
6255 If PROCESS is a serial process, wait until all output written to the
6256 process has been transmitted to the serial port. */)
6257 (Lisp_Object process
)
6260 struct coding_system
*coding
;
6262 if (DATAGRAM_CONN_P (process
))
6265 proc
= get_process (process
);
6266 coding
= proc_encode_coding_system
[XPROCESS (proc
)->outfd
];
6268 /* Make sure the process is really alive. */
6269 if (XPROCESS (proc
)->raw_status_new
)
6270 update_status (XPROCESS (proc
));
6271 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
6272 error ("Process %s not running", SDATA (XPROCESS (proc
)->name
));
6274 if (CODING_REQUIRE_FLUSHING (coding
))
6276 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6277 send_process (proc
, "", 0, Qnil
);
6280 if (XPROCESS (proc
)->pty_flag
)
6281 send_process (proc
, "\004", 1, Qnil
);
6282 else if (EQ (XPROCESS (proc
)->type
, Qserial
))
6285 if (tcdrain (XPROCESS (proc
)->outfd
) != 0)
6286 error ("tcdrain() failed: %s", emacs_strerror (errno
));
6288 /* Do nothing on Windows because writes are blocking. */
6292 int old_outfd
, new_outfd
;
6294 #ifdef HAVE_SHUTDOWN
6295 /* If this is a network connection, or socketpair is used
6296 for communication with the subprocess, call shutdown to cause EOF.
6297 (In some old system, shutdown to socketpair doesn't work.
6298 Then we just can't win.) */
6299 if (EQ (XPROCESS (proc
)->type
, Qnetwork
)
6300 || XPROCESS (proc
)->outfd
== XPROCESS (proc
)->infd
)
6301 shutdown (XPROCESS (proc
)->outfd
, 1);
6302 /* In case of socketpair, outfd == infd, so don't close it. */
6303 if (XPROCESS (proc
)->outfd
!= XPROCESS (proc
)->infd
)
6304 emacs_close (XPROCESS (proc
)->outfd
);
6305 #else /* not HAVE_SHUTDOWN */
6306 emacs_close (XPROCESS (proc
)->outfd
);
6307 #endif /* not HAVE_SHUTDOWN */
6308 new_outfd
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
6311 old_outfd
= XPROCESS (proc
)->outfd
;
6313 if (!proc_encode_coding_system
[new_outfd
])
6314 proc_encode_coding_system
[new_outfd
]
6315 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
6316 memcpy (proc_encode_coding_system
[new_outfd
],
6317 proc_encode_coding_system
[old_outfd
],
6318 sizeof (struct coding_system
));
6319 memset (proc_encode_coding_system
[old_outfd
], 0,
6320 sizeof (struct coding_system
));
6322 XPROCESS (proc
)->outfd
= new_outfd
;
6327 /* On receipt of a signal that a child status has changed, loop asking
6328 about children with changed statuses until the system says there
6331 All we do is change the status; we do not run sentinels or print
6332 notifications. That is saved for the next time keyboard input is
6333 done, in order to avoid timing errors.
6335 ** WARNING: this can be called during garbage collection.
6336 Therefore, it must not be fooled by the presence of mark bits in
6339 ** USG WARNING: Although it is not obvious from the documentation
6340 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6341 signal() before executing at least one wait(), otherwise the
6342 handler will be called again, resulting in an infinite loop. The
6343 relevant portion of the documentation reads "SIGCLD signals will be
6344 queued and the signal-catching function will be continually
6345 reentered until the queue is empty". Invoking signal() causes the
6346 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6349 ** Malloc WARNING: This should never call malloc either directly or
6350 indirectly; if it does, that is a bug */
6354 sigchld_handler (int signo
)
6356 int old_errno
= errno
;
6358 struct Lisp_Process
*p
;
6360 SIGNAL_THREAD_CHECK (signo
);
6371 #endif /* no WUNTRACED */
6372 /* Keep trying to get a status until we get a definitive result. */
6376 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
6378 while (pid
< 0 && errno
== EINTR
);
6382 /* PID == 0 means no processes found, PID == -1 means a real
6383 failure. We have done all our job, so return. */
6390 #endif /* no WNOHANG */
6392 /* Find the process that signaled us, and record its status. */
6394 /* The process can have been deleted by Fdelete_process. */
6395 for (tail
= deleted_pid_list
; CONSP (tail
); tail
= XCDR (tail
))
6397 Lisp_Object xpid
= XCAR (tail
);
6398 if ((INTEGERP (xpid
) && pid
== (pid_t
) XINT (xpid
))
6399 || (FLOATP (xpid
) && pid
== (pid_t
) XFLOAT_DATA (xpid
)))
6401 XSETCAR (tail
, Qnil
);
6402 goto sigchld_end_of_loop
;
6406 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
6408 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6410 proc
= XCDR (XCAR (tail
));
6411 p
= XPROCESS (proc
);
6412 if (EQ (p
->type
, Qreal
) && p
->pid
== pid
)
6417 /* Look for an asynchronous process whose pid hasn't been filled
6420 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6422 proc
= XCDR (XCAR (tail
));
6423 p
= XPROCESS (proc
);
6429 /* Change the status of the process that was found. */
6432 int clear_desc_flag
= 0;
6434 p
->tick
= ++process_tick
;
6436 p
->raw_status_new
= 1;
6438 /* If process has terminated, stop waiting for its output. */
6439 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
6441 clear_desc_flag
= 1;
6443 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6444 if (clear_desc_flag
)
6446 FD_CLR (p
->infd
, &input_wait_mask
);
6447 FD_CLR (p
->infd
, &non_keyboard_wait_mask
);
6450 /* Tell wait_reading_process_output that it needs to wake up and
6452 if (input_available_clear_time
)
6453 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6456 /* There was no asynchronous process found for that pid: we have
6457 a synchronous process. */
6460 synch_process_alive
= 0;
6462 /* Report the status of the synchronous process. */
6464 synch_process_retcode
= WRETCODE (w
);
6465 else if (WIFSIGNALED (w
))
6466 synch_process_termsig
= WTERMSIG (w
);
6468 /* Tell wait_reading_process_output that it needs to wake up and
6470 if (input_available_clear_time
)
6471 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
6474 sigchld_end_of_loop
:
6477 /* On some systems, we must return right away.
6478 If any more processes want to signal us, we will
6480 Otherwise (on systems that have WNOHANG), loop around
6481 to use up all the processes that have something to tell us. */
6482 #if (defined WINDOWSNT \
6483 || (defined USG && !defined GNU_LINUX \
6484 && !(defined HPUX && defined WNOHANG)))
6487 #endif /* USG, but not HPUX with WNOHANG */
6490 #endif /* SIGCHLD */
6494 exec_sentinel_unwind (Lisp_Object data
)
6496 XPROCESS (XCAR (data
))->sentinel
= XCDR (data
);
6501 exec_sentinel_error_handler (Lisp_Object error
)
6503 cmd_error_internal (error
, "error in process sentinel: ");
6505 update_echo_area ();
6506 Fsleep_for (make_number (2), Qnil
);
6511 exec_sentinel (Lisp_Object proc
, Lisp_Object reason
)
6513 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
6514 register struct Lisp_Process
*p
= XPROCESS (proc
);
6515 int count
= SPECPDL_INDEX ();
6516 int outer_running_asynch_code
= running_asynch_code
;
6517 int waiting
= waiting_for_user_input_p
;
6519 if (inhibit_sentinels
)
6522 /* No need to gcpro these, because all we do with them later
6523 is test them for EQness, and none of them should be a string. */
6524 odeactivate
= Vdeactivate_mark
;
6525 XSETBUFFER (obuffer
, current_buffer
);
6526 okeymap
= current_buffer
->keymap
;
6528 /* There's no good reason to let sentinels change the current
6529 buffer, and many callers of accept-process-output, sit-for, and
6530 friends don't expect current-buffer to be changed from under them. */
6531 record_unwind_protect (set_buffer_if_live
, Fcurrent_buffer ());
6533 sentinel
= p
->sentinel
;
6534 if (NILP (sentinel
))
6537 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6538 assure that it gets restored no matter how the sentinel exits. */
6540 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
6541 /* Inhibit quit so that random quits don't screw up a running filter. */
6542 specbind (Qinhibit_quit
, Qt
);
6543 specbind (Qlast_nonmenu_event
, Qt
); /* Why? --Stef */
6545 /* In case we get recursively called,
6546 and we already saved the match data nonrecursively,
6547 save the same match data in safely recursive fashion. */
6548 if (outer_running_asynch_code
)
6551 tem
= Fmatch_data (Qnil
, Qnil
, Qnil
);
6552 restore_search_regs ();
6553 record_unwind_save_match_data ();
6554 Fset_match_data (tem
, Qt
);
6557 /* For speed, if a search happens within this code,
6558 save the match data in a special nonrecursive fashion. */
6559 running_asynch_code
= 1;
6561 internal_condition_case_1 (read_process_output_call
,
6563 Fcons (proc
, Fcons (reason
, Qnil
))),
6564 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
6565 exec_sentinel_error_handler
);
6567 /* If we saved the match data nonrecursively, restore it now. */
6568 restore_search_regs ();
6569 running_asynch_code
= outer_running_asynch_code
;
6571 Vdeactivate_mark
= odeactivate
;
6573 /* Restore waiting_for_user_input_p as it was
6574 when we were called, in case the filter clobbered it. */
6575 waiting_for_user_input_p
= waiting
;
6578 if (! EQ (Fcurrent_buffer (), obuffer
)
6579 || ! EQ (current_buffer
->keymap
, okeymap
))
6581 /* But do it only if the caller is actually going to read events.
6582 Otherwise there's no need to make him wake up, and it could
6583 cause trouble (for example it would make sit_for return). */
6584 if (waiting_for_user_input_p
== -1)
6585 record_asynch_buffer_change ();
6587 unbind_to (count
, Qnil
);
6590 /* Report all recent events of a change in process status
6591 (either run the sentinel or output a message).
6592 This is usually done while Emacs is waiting for keyboard input
6593 but can be done at other times. */
6596 status_notify (struct Lisp_Process
*deleting_process
)
6598 register Lisp_Object proc
, buffer
;
6599 Lisp_Object tail
, msg
;
6600 struct gcpro gcpro1
, gcpro2
;
6604 /* We need to gcpro tail; if read_process_output calls a filter
6605 which deletes a process and removes the cons to which tail points
6606 from Vprocess_alist, and then causes a GC, tail is an unprotected
6610 /* Set this now, so that if new processes are created by sentinels
6611 that we run, we get called again to handle their status changes. */
6612 update_tick
= process_tick
;
6614 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
6617 register struct Lisp_Process
*p
;
6619 proc
= Fcdr (XCAR (tail
));
6620 p
= XPROCESS (proc
);
6622 if (p
->tick
!= p
->update_tick
)
6624 p
->update_tick
= p
->tick
;
6626 /* If process is still active, read any output that remains. */
6627 while (! EQ (p
->filter
, Qt
)
6628 && ! EQ (p
->status
, Qconnect
)
6629 && ! EQ (p
->status
, Qlisten
)
6630 /* Network or serial process not stopped: */
6631 && ! EQ (p
->command
, Qt
)
6633 && p
!= deleting_process
6634 && read_process_output (proc
, p
->infd
) > 0);
6638 /* Get the text to use for the message. */
6639 if (p
->raw_status_new
)
6641 msg
= status_message (p
);
6643 /* If process is terminated, deactivate it or delete it. */
6645 if (CONSP (p
->status
))
6646 symbol
= XCAR (p
->status
);
6648 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
6649 || EQ (symbol
, Qclosed
))
6651 if (delete_exited_processes
)
6652 remove_process (proc
);
6654 deactivate_process (proc
);
6657 /* The actions above may have further incremented p->tick.
6658 So set p->update_tick again
6659 so that an error in the sentinel will not cause
6660 this code to be run again. */
6661 p
->update_tick
= p
->tick
;
6662 /* Now output the message suitably. */
6663 if (!NILP (p
->sentinel
))
6664 exec_sentinel (proc
, msg
);
6665 /* Don't bother with a message in the buffer
6666 when a process becomes runnable. */
6667 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
6670 struct buffer
*old
= current_buffer
;
6671 int opoint
, opoint_byte
;
6672 int before
, before_byte
;
6674 /* Avoid error if buffer is deleted
6675 (probably that's why the process is dead, too) */
6676 if (NILP (XBUFFER (buffer
)->name
))
6678 Fset_buffer (buffer
);
6681 opoint_byte
= PT_BYTE
;
6682 /* Insert new output into buffer
6683 at the current end-of-output marker,
6684 thus preserving logical ordering of input and output. */
6685 if (XMARKER (p
->mark
)->buffer
)
6686 Fgoto_char (p
->mark
);
6688 SET_PT_BOTH (ZV
, ZV_BYTE
);
6691 before_byte
= PT_BYTE
;
6693 tem
= current_buffer
->read_only
;
6694 current_buffer
->read_only
= Qnil
;
6695 insert_string ("\nProcess ");
6696 Finsert (1, &p
->name
);
6697 insert_string (" ");
6699 current_buffer
->read_only
= tem
;
6700 set_marker_both (p
->mark
, p
->buffer
, PT
, PT_BYTE
);
6702 if (opoint
>= before
)
6703 SET_PT_BOTH (opoint
+ (PT
- before
),
6704 opoint_byte
+ (PT_BYTE
- before_byte
));
6706 SET_PT_BOTH (opoint
, opoint_byte
);
6708 set_buffer_internal (old
);
6713 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
6718 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
6719 Sset_process_coding_system
, 1, 3, 0,
6720 doc
: /* Set coding systems of PROCESS to DECODING and ENCODING.
6721 DECODING will be used to decode subprocess output and ENCODING to
6722 encode subprocess input. */)
6723 (register Lisp_Object process
, Lisp_Object decoding
, Lisp_Object encoding
)
6725 register struct Lisp_Process
*p
;
6727 CHECK_PROCESS (process
);
6728 p
= XPROCESS (process
);
6730 error ("Input file descriptor of %s closed", SDATA (p
->name
));
6732 error ("Output file descriptor of %s closed", SDATA (p
->name
));
6733 Fcheck_coding_system (decoding
);
6734 Fcheck_coding_system (encoding
);
6735 encoding
= coding_inherit_eol_type (encoding
, Qnil
);
6736 p
->decode_coding_system
= decoding
;
6737 p
->encode_coding_system
= encoding
;
6738 setup_process_coding_systems (process
);
6743 DEFUN ("process-coding-system",
6744 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
6745 doc
: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
6746 (register Lisp_Object process
)
6748 CHECK_PROCESS (process
);
6749 return Fcons (XPROCESS (process
)->decode_coding_system
,
6750 XPROCESS (process
)->encode_coding_system
);
6753 DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte
,
6754 Sset_process_filter_multibyte
, 2, 2, 0,
6755 doc
: /* Set multibyteness of the strings given to PROCESS's filter.
6756 If FLAG is non-nil, the filter is given multibyte strings.
6757 If FLAG is nil, the filter is given unibyte strings. In this case,
6758 all character code conversion except for end-of-line conversion is
6760 (Lisp_Object process
, Lisp_Object flag
)
6762 register struct Lisp_Process
*p
;
6764 CHECK_PROCESS (process
);
6765 p
= XPROCESS (process
);
6767 p
->decode_coding_system
= raw_text_coding_system (p
->decode_coding_system
);
6768 setup_process_coding_systems (process
);
6773 DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p
,
6774 Sprocess_filter_multibyte_p
, 1, 1, 0,
6775 doc
: /* Return t if a multibyte string is given to PROCESS's filter.*/)
6776 (Lisp_Object process
)
6778 register struct Lisp_Process
*p
;
6779 struct coding_system
*coding
;
6781 CHECK_PROCESS (process
);
6782 p
= XPROCESS (process
);
6783 coding
= proc_decode_coding_system
[p
->infd
];
6784 return (CODING_FOR_UNIBYTE (coding
) ? Qnil
: Qt
);
6790 static int add_gpm_wait_descriptor_called_flag
;
6793 add_gpm_wait_descriptor (int desc
)
6795 if (! add_gpm_wait_descriptor_called_flag
)
6796 FD_CLR (0, &input_wait_mask
);
6797 add_gpm_wait_descriptor_called_flag
= 1;
6798 FD_SET (desc
, &input_wait_mask
);
6799 FD_SET (desc
, &gpm_wait_mask
);
6800 if (desc
> max_gpm_desc
)
6801 max_gpm_desc
= desc
;
6805 delete_gpm_wait_descriptor (int desc
)
6808 int lim
= max_gpm_desc
;
6810 FD_CLR (desc
, &input_wait_mask
);
6811 FD_CLR (desc
, &non_process_wait_mask
);
6813 if (desc
== max_gpm_desc
)
6814 for (fd
= 0; fd
< lim
; fd
++)
6815 if (FD_ISSET (fd
, &input_wait_mask
)
6816 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
6817 && !FD_ISSET (fd
, &non_process_wait_mask
))
6821 /* Return nonzero if *MASK has a bit set
6822 that corresponds to one of the keyboard input descriptors. */
6825 keyboard_bit_set (fd_set
*mask
)
6829 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
6830 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
6831 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
6837 #else /* not subprocesses */
6839 /* Defined on msdos.c. */
6840 extern int sys_select (int, SELECT_TYPE
*, SELECT_TYPE
*, SELECT_TYPE
*,
6843 /* Implementation of wait_reading_process_output, assuming that there
6844 are no subprocesses. Used only by the MS-DOS build.
6846 Wait for timeout to elapse and/or keyboard input to be available.
6849 timeout in seconds, or
6850 zero for no limit, or
6851 -1 means gobble data immediately available but don't wait for any.
6853 read_kbd is a Lisp_Object:
6854 0 to ignore keyboard input, or
6855 1 to return when input is available, or
6856 -1 means caller will actually read the input, so don't throw to
6859 see full version for other parameters. We know that wait_proc will
6860 always be NULL, since `subprocesses' isn't defined.
6862 do_display != 0 means redisplay should be done to show subprocess
6863 output that arrives.
6865 Return true if we received input from any process. */
6868 wait_reading_process_output (int time_limit
, int microsecs
, int read_kbd
,
6870 Lisp_Object wait_for_cell
,
6871 struct Lisp_Process
*wait_proc
, int just_wait_proc
)
6874 EMACS_TIME end_time
, timeout
;
6875 SELECT_TYPE waitchannels
;
6878 /* What does time_limit really mean? */
6879 if (time_limit
|| microsecs
)
6881 EMACS_GET_TIME (end_time
);
6882 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
6883 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
6886 /* Turn off periodic alarms (in case they are in use)
6887 and then turn off any other atimers,
6888 because the select emulator uses alarms. */
6890 turn_on_atimers (0);
6894 int timeout_reduced_for_timers
= 0;
6896 /* If calling from keyboard input, do not quit
6897 since we want to return C-g as an input character.
6898 Otherwise, do pending quit if requested. */
6902 /* Exit now if the cell we're waiting for became non-nil. */
6903 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
6906 /* Compute time from now till when time limit is up */
6907 /* Exit if already run out */
6908 if (time_limit
== -1)
6910 /* -1 specified for timeout means
6911 gobble output available now
6912 but don't wait at all. */
6914 EMACS_SET_SECS_USECS (timeout
, 0, 0);
6916 else if (time_limit
|| microsecs
)
6918 EMACS_GET_TIME (timeout
);
6919 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
6920 if (EMACS_TIME_NEG_P (timeout
))
6925 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
6928 /* If our caller will not immediately handle keyboard events,
6929 run timer events directly.
6930 (Callers that will immediately read keyboard events
6931 call timer_delay on their own.) */
6932 if (NILP (wait_for_cell
))
6934 EMACS_TIME timer_delay
;
6938 int old_timers_run
= timers_run
;
6939 timer_delay
= timer_check (1);
6940 if (timers_run
!= old_timers_run
&& do_display
)
6941 /* We must retry, since a timer may have requeued itself
6942 and that could alter the time delay. */
6943 redisplay_preserve_echo_area (14);
6947 while (!detect_input_pending ());
6949 /* If there is unread keyboard input, also return. */
6951 && requeued_events_pending_p ())
6954 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
6956 EMACS_TIME difference
;
6957 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
6958 if (EMACS_TIME_NEG_P (difference
))
6960 timeout
= timer_delay
;
6961 timeout_reduced_for_timers
= 1;
6966 /* Cause C-g and alarm signals to take immediate action,
6967 and cause input available signals to zero out timeout. */
6969 set_waiting_for_input (&timeout
);
6971 /* Wait till there is something to do. */
6973 if (! read_kbd
&& NILP (wait_for_cell
))
6974 FD_ZERO (&waitchannels
);
6976 FD_SET (0, &waitchannels
);
6978 /* If a frame has been newly mapped and needs updating,
6979 reprocess its display stuff. */
6980 if (frame_garbaged
&& do_display
)
6982 clear_waiting_for_input ();
6983 redisplay_preserve_echo_area (15);
6985 set_waiting_for_input (&timeout
);
6988 if (read_kbd
&& detect_input_pending ())
6991 FD_ZERO (&waitchannels
);
6994 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
6999 /* Make C-g and alarm signals set flags again */
7000 clear_waiting_for_input ();
7002 /* If we woke up due to SIGWINCH, actually change size now. */
7003 do_pending_window_change (0);
7005 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
7006 /* We waited the full specified time, so return now. */
7011 /* If the system call was interrupted, then go around the
7013 if (xerrno
== EINTR
)
7014 FD_ZERO (&waitchannels
);
7016 error ("select error: %s", emacs_strerror (xerrno
));
7019 /* Check for keyboard input */
7022 && detect_input_pending_run_timers (do_display
))
7024 swallow_events (do_display
);
7025 if (detect_input_pending_run_timers (do_display
))
7029 /* If there is unread keyboard input, also return. */
7031 && requeued_events_pending_p ())
7034 /* If wait_for_cell. check for keyboard input
7035 but don't run any timers.
7036 ??? (It seems wrong to me to check for keyboard
7037 input at all when wait_for_cell, but the code
7038 has been this way since July 1994.
7039 Try changing this after version 19.31.) */
7040 if (! NILP (wait_for_cell
)
7041 && detect_input_pending ())
7043 swallow_events (do_display
);
7044 if (detect_input_pending ())
7048 /* Exit now if the cell we're waiting for became non-nil. */
7049 if (! NILP (wait_for_cell
) && ! NILP (XCAR (wait_for_cell
)))
7058 #endif /* not subprocesses */
7060 /* The following functions are needed even if async subprocesses are
7061 not supported. Some of them are no-op stubs in that case. */
7063 /* Add DESC to the set of keyboard input descriptors. */
7066 add_keyboard_wait_descriptor (int desc
)
7069 FD_SET (desc
, &input_wait_mask
);
7070 FD_SET (desc
, &non_process_wait_mask
);
7071 if (desc
> max_keyboard_desc
)
7072 max_keyboard_desc
= desc
;
7076 /* From now on, do not expect DESC to give keyboard input. */
7079 delete_keyboard_wait_descriptor (int desc
)
7083 int lim
= max_keyboard_desc
;
7085 FD_CLR (desc
, &input_wait_mask
);
7086 FD_CLR (desc
, &non_process_wait_mask
);
7088 if (desc
== max_keyboard_desc
)
7089 for (fd
= 0; fd
< lim
; fd
++)
7090 if (FD_ISSET (fd
, &input_wait_mask
)
7091 && !FD_ISSET (fd
, &non_keyboard_wait_mask
)
7092 && !FD_ISSET (fd
, &gpm_wait_mask
))
7093 max_keyboard_desc
= fd
;
7094 #endif /* subprocesses */
7097 /* Setup coding systems of PROCESS. */
7100 setup_process_coding_systems (Lisp_Object process
)
7103 struct Lisp_Process
*p
= XPROCESS (process
);
7105 int outch
= p
->outfd
;
7106 Lisp_Object coding_system
;
7108 if (inch
< 0 || outch
< 0)
7111 if (!proc_decode_coding_system
[inch
])
7112 proc_decode_coding_system
[inch
]
7113 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7114 coding_system
= p
->decode_coding_system
;
7115 if (! NILP (p
->filter
))
7117 else if (BUFFERP (p
->buffer
))
7119 if (NILP (XBUFFER (p
->buffer
)->enable_multibyte_characters
))
7120 coding_system
= raw_text_coding_system (coding_system
);
7122 setup_coding_system (coding_system
, proc_decode_coding_system
[inch
]);
7124 if (!proc_encode_coding_system
[outch
])
7125 proc_encode_coding_system
[outch
]
7126 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
7127 setup_coding_system (p
->encode_coding_system
,
7128 proc_encode_coding_system
[outch
]);
7132 /* Close all descriptors currently in use for communication
7133 with subprocess. This is used in a newly-forked subprocess
7134 to get rid of irrelevant descriptors. */
7137 close_process_descs (void)
7141 for (i
= 0; i
< MAXDESC
; i
++)
7143 Lisp_Object process
;
7144 process
= chan_process
[i
];
7145 if (!NILP (process
))
7147 int in
= XPROCESS (process
)->infd
;
7148 int out
= XPROCESS (process
)->outfd
;
7151 if (out
>= 0 && in
!= out
)
7158 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
7159 doc
: /* Return the (or a) process associated with BUFFER.
7160 BUFFER may be a buffer or the name of one. */)
7161 (register Lisp_Object buffer
)
7164 register Lisp_Object buf
, tail
, proc
;
7166 if (NILP (buffer
)) return Qnil
;
7167 buf
= Fget_buffer (buffer
);
7168 if (NILP (buf
)) return Qnil
;
7170 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7172 proc
= Fcdr (XCAR (tail
));
7173 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
7176 #endif /* subprocesses */
7180 DEFUN ("process-inherit-coding-system-flag",
7181 Fprocess_inherit_coding_system_flag
, Sprocess_inherit_coding_system_flag
,
7183 doc
: /* Return the value of inherit-coding-system flag for PROCESS.
7184 If this flag is t, `buffer-file-coding-system' of the buffer
7185 associated with PROCESS will inherit the coding system used to decode
7186 the process output. */)
7187 (register Lisp_Object process
)
7190 CHECK_PROCESS (process
);
7191 return XPROCESS (process
)->inherit_coding_system_flag
? Qt
: Qnil
;
7193 /* Ignore the argument and return the value of
7194 inherit-process-coding-system. */
7195 return inherit_process_coding_system
? Qt
: Qnil
;
7199 /* Kill all processes associated with `buffer'.
7200 If `buffer' is nil, kill all processes */
7203 kill_buffer_processes (Lisp_Object buffer
)
7206 Lisp_Object tail
, proc
;
7208 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCDR (tail
))
7210 proc
= XCDR (XCAR (tail
));
7212 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
7214 if (NETCONN_P (proc
) || SERIALCONN_P (proc
))
7215 Fdelete_process (proc
);
7216 else if (XPROCESS (proc
)->infd
>= 0)
7217 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
7220 #else /* subprocesses */
7221 /* Since we have no subprocesses, this does nothing. */
7222 #endif /* subprocesses */
7225 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
7227 doc
: /* Returns non-nil if Emacs is waiting for input from the user.
7228 This is intended for use by asynchronous process output filters and sentinels. */)
7232 return (waiting_for_user_input_p
? Qt
: Qnil
);
7238 /* Stop reading input from keyboard sources. */
7241 hold_keyboard_input (void)
7246 /* Resume reading input from keyboard sources. */
7249 unhold_keyboard_input (void)
7254 /* Return non-zero if keyboard input is on hold, zero otherwise. */
7257 kbd_on_hold_p (void)
7259 return kbd_is_on_hold
;
7263 /* Enumeration of and access to system processes a-la ps(1). */
7265 DEFUN ("list-system-processes", Flist_system_processes
, Slist_system_processes
,
7267 doc
: /* Return a list of numerical process IDs of all running processes.
7268 If this functionality is unsupported, return nil.
7270 See `process-attributes' for getting attributes of a process given its ID. */)
7273 return list_system_processes ();
7276 DEFUN ("process-attributes", Fprocess_attributes
,
7277 Sprocess_attributes
, 1, 1, 0,
7278 doc
: /* Return attributes of the process given by its PID, a number.
7280 Value is an alist where each element is a cons cell of the form
7284 If this functionality is unsupported, the value is nil.
7286 See `list-system-processes' for getting a list of all process IDs.
7288 The KEYs of the attributes that this function may return are listed
7289 below, together with the type of the associated VALUE (in parentheses).
7290 Not all platforms support all of these attributes; unsupported
7291 attributes will not appear in the returned alist.
7292 Unless explicitly indicated otherwise, numbers can have either
7293 integer or floating point values.
7295 euid -- Effective user User ID of the process (number)
7296 user -- User name corresponding to euid (string)
7297 egid -- Effective user Group ID of the process (number)
7298 group -- Group name corresponding to egid (string)
7299 comm -- Command name (executable name only) (string)
7300 state -- Process state code, such as "S", "R", or "T" (string)
7301 ppid -- Parent process ID (number)
7302 pgrp -- Process group ID (number)
7303 sess -- Session ID, i.e. process ID of session leader (number)
7304 ttname -- Controlling tty name (string)
7305 tpgid -- ID of foreground process group on the process's tty (number)
7306 minflt -- number of minor page faults (number)
7307 majflt -- number of major page faults (number)
7308 cminflt -- cumulative number of minor page faults (number)
7309 cmajflt -- cumulative number of major page faults (number)
7310 utime -- user time used by the process, in the (HIGH LOW USEC) format
7311 stime -- system time used by the process, in the (HIGH LOW USEC) format
7312 time -- sum of utime and stime, in the (HIGH LOW USEC) format
7313 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7314 cstime -- system time used by the process and its children, (HIGH LOW USEC)
7315 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
7316 pri -- priority of the process (number)
7317 nice -- nice value of the process (number)
7318 thcount -- process thread count (number)
7319 start -- time the process started, in the (HIGH LOW USEC) format
7320 vsize -- virtual memory size of the process in KB's (number)
7321 rss -- resident set size of the process in KB's (number)
7322 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7323 pcpu -- percents of CPU time used by the process (floating-point number)
7324 pmem -- percents of total physical memory used by process's resident set
7325 (floating-point number)
7326 args -- command line which invoked the process (string). */)
7329 return system_process_attributes (pid
);
7339 inhibit_sentinels
= 0;
7343 if (! noninteractive
|| initialized
)
7345 signal (SIGCHLD
, sigchld_handler
);
7348 FD_ZERO (&input_wait_mask
);
7349 FD_ZERO (&non_keyboard_wait_mask
);
7350 FD_ZERO (&non_process_wait_mask
);
7351 max_process_desc
= 0;
7353 #ifdef NON_BLOCKING_CONNECT
7354 FD_ZERO (&connect_wait_mask
);
7355 num_pending_connects
= 0;
7358 #ifdef ADAPTIVE_READ_BUFFERING
7359 process_output_delay_count
= 0;
7360 process_output_skip
= 0;
7363 /* Don't do this, it caused infinite select loops. The display
7364 method should call add_keyboard_wait_descriptor on stdin if it
7367 FD_SET (0, &input_wait_mask
);
7370 Vprocess_alist
= Qnil
;
7372 deleted_pid_list
= Qnil
;
7374 for (i
= 0; i
< MAXDESC
; i
++)
7376 chan_process
[i
] = Qnil
;
7377 proc_buffered_char
[i
] = -1;
7379 memset (proc_decode_coding_system
, 0, sizeof proc_decode_coding_system
);
7380 memset (proc_encode_coding_system
, 0, sizeof proc_encode_coding_system
);
7381 #ifdef DATAGRAM_SOCKETS
7382 memset (datagram_address
, 0, sizeof datagram_address
);
7386 Lisp_Object subfeatures
= Qnil
;
7387 const struct socket_options
*sopt
;
7389 #define ADD_SUBFEATURE(key, val) \
7390 subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)
7392 #ifdef NON_BLOCKING_CONNECT
7393 ADD_SUBFEATURE (QCnowait
, Qt
);
7395 #ifdef DATAGRAM_SOCKETS
7396 ADD_SUBFEATURE (QCtype
, Qdatagram
);
7398 #ifdef HAVE_SEQPACKET
7399 ADD_SUBFEATURE (QCtype
, Qseqpacket
);
7401 #ifdef HAVE_LOCAL_SOCKETS
7402 ADD_SUBFEATURE (QCfamily
, Qlocal
);
7404 ADD_SUBFEATURE (QCfamily
, Qipv4
);
7406 ADD_SUBFEATURE (QCfamily
, Qipv6
);
7408 #ifdef HAVE_GETSOCKNAME
7409 ADD_SUBFEATURE (QCservice
, Qt
);
7411 #if defined(O_NONBLOCK) || defined(O_NDELAY)
7412 ADD_SUBFEATURE (QCserver
, Qt
);
7415 for (sopt
= socket_options
; sopt
->name
; sopt
++)
7416 subfeatures
= pure_cons (intern_c_string (sopt
->name
), subfeatures
);
7418 Fprovide (intern_c_string ("make-network-process"), subfeatures
);
7421 #if defined (DARWIN_OS)
7422 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
7423 processes. As such, we only change the default value. */
7426 char *release
= get_operating_system_release ();
7427 if (!release
|| !release
[0] || (release
[0] < MIN_PTY_KERNEL_VERSION
7428 && release
[1] == '.')) {
7429 Vprocess_connection_type
= Qnil
;
7433 #endif /* subprocesses */
7438 syms_of_process (void)
7442 Qprocessp
= intern_c_string ("processp");
7443 staticpro (&Qprocessp
);
7444 Qrun
= intern_c_string ("run");
7446 Qstop
= intern_c_string ("stop");
7448 Qsignal
= intern_c_string ("signal");
7449 staticpro (&Qsignal
);
7451 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7454 Qexit = intern_c_string ("exit");
7455 staticpro (&Qexit); */
7457 Qopen
= intern_c_string ("open");
7459 Qclosed
= intern_c_string ("closed");
7460 staticpro (&Qclosed
);
7461 Qconnect
= intern_c_string ("connect");
7462 staticpro (&Qconnect
);
7463 Qfailed
= intern_c_string ("failed");
7464 staticpro (&Qfailed
);
7465 Qlisten
= intern_c_string ("listen");
7466 staticpro (&Qlisten
);
7467 Qlocal
= intern_c_string ("local");
7468 staticpro (&Qlocal
);
7469 Qipv4
= intern_c_string ("ipv4");
7472 Qipv6
= intern_c_string ("ipv6");
7475 Qdatagram
= intern_c_string ("datagram");
7476 staticpro (&Qdatagram
);
7477 Qseqpacket
= intern_c_string ("seqpacket");
7478 staticpro (&Qseqpacket
);
7480 QCport
= intern_c_string (":port");
7481 staticpro (&QCport
);
7482 QCspeed
= intern_c_string (":speed");
7483 staticpro (&QCspeed
);
7484 QCprocess
= intern_c_string (":process");
7485 staticpro (&QCprocess
);
7487 QCbytesize
= intern_c_string (":bytesize");
7488 staticpro (&QCbytesize
);
7489 QCstopbits
= intern_c_string (":stopbits");
7490 staticpro (&QCstopbits
);
7491 QCparity
= intern_c_string (":parity");
7492 staticpro (&QCparity
);
7493 Qodd
= intern_c_string ("odd");
7495 Qeven
= intern_c_string ("even");
7497 QCflowcontrol
= intern_c_string (":flowcontrol");
7498 staticpro (&QCflowcontrol
);
7499 Qhw
= intern_c_string ("hw");
7501 Qsw
= intern_c_string ("sw");
7503 QCsummary
= intern_c_string (":summary");
7504 staticpro (&QCsummary
);
7506 Qreal
= intern_c_string ("real");
7508 Qnetwork
= intern_c_string ("network");
7509 staticpro (&Qnetwork
);
7510 Qserial
= intern_c_string ("serial");
7511 staticpro (&Qserial
);
7512 QCbuffer
= intern_c_string (":buffer");
7513 staticpro (&QCbuffer
);
7514 QChost
= intern_c_string (":host");
7515 staticpro (&QChost
);
7516 QCservice
= intern_c_string (":service");
7517 staticpro (&QCservice
);
7518 QClocal
= intern_c_string (":local");
7519 staticpro (&QClocal
);
7520 QCremote
= intern_c_string (":remote");
7521 staticpro (&QCremote
);
7522 QCcoding
= intern_c_string (":coding");
7523 staticpro (&QCcoding
);
7524 QCserver
= intern_c_string (":server");
7525 staticpro (&QCserver
);
7526 QCnowait
= intern_c_string (":nowait");
7527 staticpro (&QCnowait
);
7528 QCsentinel
= intern_c_string (":sentinel");
7529 staticpro (&QCsentinel
);
7530 QClog
= intern_c_string (":log");
7532 QCnoquery
= intern_c_string (":noquery");
7533 staticpro (&QCnoquery
);
7534 QCstop
= intern_c_string (":stop");
7535 staticpro (&QCstop
);
7536 QCoptions
= intern_c_string (":options");
7537 staticpro (&QCoptions
);
7538 QCplist
= intern_c_string (":plist");
7539 staticpro (&QCplist
);
7541 Qlast_nonmenu_event
= intern_c_string ("last-nonmenu-event");
7542 staticpro (&Qlast_nonmenu_event
);
7544 staticpro (&Vprocess_alist
);
7546 staticpro (&deleted_pid_list
);
7549 #endif /* subprocesses */
7551 QCname
= intern_c_string (":name");
7552 staticpro (&QCname
);
7553 QCtype
= intern_c_string (":type");
7554 staticpro (&QCtype
);
7556 Qeuid
= intern_c_string ("euid");
7558 Qegid
= intern_c_string ("egid");
7560 Quser
= intern_c_string ("user");
7562 Qgroup
= intern_c_string ("group");
7563 staticpro (&Qgroup
);
7564 Qcomm
= intern_c_string ("comm");
7566 Qstate
= intern_c_string ("state");
7567 staticpro (&Qstate
);
7568 Qppid
= intern_c_string ("ppid");
7570 Qpgrp
= intern_c_string ("pgrp");
7572 Qsess
= intern_c_string ("sess");
7574 Qttname
= intern_c_string ("ttname");
7575 staticpro (&Qttname
);
7576 Qtpgid
= intern_c_string ("tpgid");
7577 staticpro (&Qtpgid
);
7578 Qminflt
= intern_c_string ("minflt");
7579 staticpro (&Qminflt
);
7580 Qmajflt
= intern_c_string ("majflt");
7581 staticpro (&Qmajflt
);
7582 Qcminflt
= intern_c_string ("cminflt");
7583 staticpro (&Qcminflt
);
7584 Qcmajflt
= intern_c_string ("cmajflt");
7585 staticpro (&Qcmajflt
);
7586 Qutime
= intern_c_string ("utime");
7587 staticpro (&Qutime
);
7588 Qstime
= intern_c_string ("stime");
7589 staticpro (&Qstime
);
7590 Qtime
= intern_c_string ("time");
7592 Qcutime
= intern_c_string ("cutime");
7593 staticpro (&Qcutime
);
7594 Qcstime
= intern_c_string ("cstime");
7595 staticpro (&Qcstime
);
7596 Qctime
= intern_c_string ("ctime");
7597 staticpro (&Qctime
);
7598 Qpri
= intern_c_string ("pri");
7600 Qnice
= intern_c_string ("nice");
7602 Qthcount
= intern_c_string ("thcount");
7603 staticpro (&Qthcount
);
7604 Qstart
= intern_c_string ("start");
7605 staticpro (&Qstart
);
7606 Qvsize
= intern_c_string ("vsize");
7607 staticpro (&Qvsize
);
7608 Qrss
= intern_c_string ("rss");
7610 Qetime
= intern_c_string ("etime");
7611 staticpro (&Qetime
);
7612 Qpcpu
= intern_c_string ("pcpu");
7614 Qpmem
= intern_c_string ("pmem");
7616 Qargs
= intern_c_string ("args");
7619 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
7620 doc
: /* *Non-nil means delete processes immediately when they exit.
7621 A value of nil means don't delete them until `list-processes' is run. */);
7623 delete_exited_processes
= 1;
7626 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
7627 doc
: /* Control type of device used to communicate with subprocesses.
7628 Values are nil to use a pipe, or t or `pty' to use a pty.
7629 The value has no effect if the system has no ptys or if all ptys are busy:
7630 then a pipe is used in any case.
7631 The value takes effect when `start-process' is called. */);
7632 Vprocess_connection_type
= Qt
;
7634 #ifdef ADAPTIVE_READ_BUFFERING
7635 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering
,
7636 doc
: /* If non-nil, improve receive buffering by delaying after short reads.
7637 On some systems, when Emacs reads the output from a subprocess, the output data
7638 is read in very small blocks, potentially resulting in very poor performance.
7639 This behavior can be remedied to some extent by setting this variable to a
7640 non-nil value, as it will automatically delay reading from such processes, to
7641 allow them to produce more output before Emacs tries to read it.
7642 If the value is t, the delay is reset after each write to the process; any other
7643 non-nil value means that the delay is not reset on write.
7644 The variable takes effect when `start-process' is called. */);
7645 Vprocess_adaptive_read_buffering
= Qt
;
7648 defsubr (&Sprocessp
);
7649 defsubr (&Sget_process
);
7650 defsubr (&Sdelete_process
);
7651 defsubr (&Sprocess_status
);
7652 defsubr (&Sprocess_exit_status
);
7653 defsubr (&Sprocess_id
);
7654 defsubr (&Sprocess_name
);
7655 defsubr (&Sprocess_tty_name
);
7656 defsubr (&Sprocess_command
);
7657 defsubr (&Sset_process_buffer
);
7658 defsubr (&Sprocess_buffer
);
7659 defsubr (&Sprocess_mark
);
7660 defsubr (&Sset_process_filter
);
7661 defsubr (&Sprocess_filter
);
7662 defsubr (&Sset_process_sentinel
);
7663 defsubr (&Sprocess_sentinel
);
7664 defsubr (&Sset_process_window_size
);
7665 defsubr (&Sset_process_inherit_coding_system_flag
);
7666 defsubr (&Sset_process_query_on_exit_flag
);
7667 defsubr (&Sprocess_query_on_exit_flag
);
7668 defsubr (&Sprocess_contact
);
7669 defsubr (&Sprocess_plist
);
7670 defsubr (&Sset_process_plist
);
7671 defsubr (&Slist_processes
);
7672 defsubr (&Sprocess_list
);
7673 defsubr (&Sstart_process
);
7675 defsubr (&Sserial_process_configure
);
7676 defsubr (&Smake_serial_process
);
7677 #endif /* HAVE_SERIAL */
7678 defsubr (&Sset_network_process_option
);
7679 defsubr (&Smake_network_process
);
7680 defsubr (&Sformat_network_address
);
7681 #if defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7683 defsubr (&Snetwork_interface_list
);
7685 #if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7686 defsubr (&Snetwork_interface_info
);
7688 #endif /* defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H) */
7689 #ifdef DATAGRAM_SOCKETS
7690 defsubr (&Sprocess_datagram_address
);
7691 defsubr (&Sset_process_datagram_address
);
7693 defsubr (&Saccept_process_output
);
7694 defsubr (&Sprocess_send_region
);
7695 defsubr (&Sprocess_send_string
);
7696 defsubr (&Sinterrupt_process
);
7697 defsubr (&Skill_process
);
7698 defsubr (&Squit_process
);
7699 defsubr (&Sstop_process
);
7700 defsubr (&Scontinue_process
);
7701 defsubr (&Sprocess_running_child_p
);
7702 defsubr (&Sprocess_send_eof
);
7703 defsubr (&Ssignal_process
);
7704 defsubr (&Swaiting_for_user_input_p
);
7705 defsubr (&Sprocess_type
);
7706 defsubr (&Sset_process_coding_system
);
7707 defsubr (&Sprocess_coding_system
);
7708 defsubr (&Sset_process_filter_multibyte
);
7709 defsubr (&Sprocess_filter_multibyte_p
);
7711 #endif /* subprocesses */
7713 defsubr (&Sget_buffer_process
);
7714 defsubr (&Sprocess_inherit_coding_system_flag
);
7715 defsubr (&Slist_system_processes
);
7716 defsubr (&Sprocess_attributes
);
7719 /* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7720 (do not change this comment) */