#define _GNU_SOURCE
#include <config.h>
#include <signal.h>
#ifdef subprocesses
#include <stdio.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined(WINDOWSNT) || defined(UNIX98_PTYS)
#include <stdlib.h>
#include <fcntl.h>
#endif
#ifdef HAVE_SOCKETS
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef NEED_NET_ERRNO_H
#include <net/errno.h>
#endif
#endif
#ifdef TERM
#include <client.h>
#endif
#ifdef HAVE_BROKEN_INET_ADDR
#define IN_ADDR struct in_addr
#define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
#else
#define IN_ADDR unsigned long
#define NUMERIC_ADDR_ERROR (numeric_addr == -1)
#endif
#if defined(BSD_SYSTEM) || defined(STRIDE)
#include <sys/ioctl.h>
#if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
#include <fcntl.h>
#endif
#endif
#ifdef BROKEN_O_NONBLOCK
#undef O_NONBLOCK
#endif
#ifdef NEED_BSDTTY
#include <bsdtty.h>
#endif
#ifdef IRIS
#include <sys/sysmacros.h>
#endif
#include "systime.h"
#include "systty.h"
#include "lisp.h"
#include "window.h"
#include "buffer.h"
#include "charset.h"
#include "coding.h"
#include "process.h"
#include "termhooks.h"
#include "termopts.h"
#include "commands.h"
#include "keyboard.h"
#include "frame.h"
#include "blockinput.h"
#include "dispextern.h"
#include "composite.h"
#include "atimer.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
Lisp_Object Qprocessp;
Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
Lisp_Object Qlast_nonmenu_event;
#ifdef HAVE_SOCKETS
#define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
#else
#define NETCONN_P(p) 0
#endif
#ifdef VMS
#define FIRST_PROC_DESC 1
#else
#define FIRST_PROC_DESC 3
#endif
#if !defined (SIGCHLD) && defined (SIGCLD)
#define SIGCHLD SIGCLD
#endif
#include "syssignal.h"
#include "syswait.h"
extern void set_waiting_for_input P_ ((EMACS_TIME *));
#ifndef USE_CRT_DLL
extern int errno;
#endif
#ifdef VMS
extern char *sys_errlist[];
#endif
#ifndef HAVE_H_ERRNO
extern int h_errno;
#endif
static Lisp_Object Vprocess_connection_type;
#ifdef SKTPAIR
#ifndef HAVE_SOCKETS
#include <sys/socket.h>
#endif
#endif
int process_tick;
int update_tick;
#include "sysselect.h"
extern int keyboard_bit_set P_ ((SELECT_TYPE *));
#ifdef HAVE_WINDOW_SYSTEM
#define POLL_FOR_INPUT
#endif
static SELECT_TYPE input_wait_mask;
static SELECT_TYPE non_keyboard_wait_mask;
static SELECT_TYPE non_process_wait_mask;
static int max_process_desc;
static int max_keyboard_desc;
static int delete_exited_processes;
Lisp_Object chan_process[MAXDESC];
Lisp_Object Vprocess_alist;
int proc_buffered_char[MAXDESC];
static struct coding_system *proc_decode_coding_system[MAXDESC];
static struct coding_system *proc_encode_coding_system[MAXDESC];
static Lisp_Object get_process ();
extern EMACS_TIME timer_check ();
extern int timers_run;
static int pty_max_bytes;
extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
#ifdef HAVE_PTYS
static char pty_name[24];
#endif
Lisp_Object status_convert ();
void
update_status (p)
struct Lisp_Process *p;
{
union { int i; WAITTYPE wt; } u;
u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
p->status = status_convert (u.wt);
p->raw_status_low = Qnil;
p->raw_status_high = Qnil;
}
Lisp_Object
status_convert (w)
WAITTYPE w;
{
if (WIFSTOPPED (w))
return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
else if (WIFEXITED (w))
return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
WCOREDUMP (w) ? Qt : Qnil));
else if (WIFSIGNALED (w))
return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
WCOREDUMP (w) ? Qt : Qnil));
else
return Qrun;
}
void
decode_status (l, symbol, code, coredump)
Lisp_Object l;
Lisp_Object *symbol;
int *code;
int *coredump;
{
Lisp_Object tem;
if (SYMBOLP (l))
{
*symbol = l;
*code = 0;
*coredump = 0;
}
else
{
*symbol = XCAR (l);
tem = XCDR (l);
*code = XFASTINT (XCAR (tem));
tem = XCDR (tem);
*coredump = !NILP (tem);
}
}
Lisp_Object
status_message (status)
Lisp_Object status;
{
Lisp_Object symbol;
int code, coredump;
Lisp_Object string, string2;
decode_status (status, &symbol, &code, &coredump);
if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
{
char *signame;
synchronize_system_messages_locale ();
signame = strsignal (code);
if (signame == 0)
signame = "unknown";
string = build_string (signame);
string2 = build_string (coredump ? " (core dumped)\n" : "\n");
XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
return concat2 (string, string2);
}
else if (EQ (symbol, Qexit))
{
if (code == 0)
return build_string ("finished\n");
string = Fnumber_to_string (make_number (code));
string2 = build_string (coredump ? " (core dumped)\n" : "\n");
return concat2 (build_string ("exited abnormally with code "),
concat2 (string, string2));
}
else
return Fcopy_sequence (Fsymbol_name (symbol));
}
#ifdef HAVE_PTYS
int
allocate_pty ()
{
struct stat stb;
register int c, i;
int fd;
int failed_count = 0;
#ifdef PTY_ITERATION
PTY_ITERATION
#else
for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
for (i = 0; i < 16; i++)
#endif
{
#ifdef PTY_NAME_SPRINTF
PTY_NAME_SPRINTF
#else
sprintf (pty_name, "/dev/pty%c%x", c, i);
#endif
#ifdef PTY_OPEN
PTY_OPEN;
#else
#ifdef IRIS
*ptyv = emacs_open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
if (fd < 0)
return -1;
if (fstat (fd, &stb) < 0)
return -1;
#else
if (stat (pty_name, &stb) < 0)
{
failed_count++;
if (failed_count >= 3)
return -1;
}
else
failed_count = 0;
#ifdef O_NONBLOCK
fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
#else
fd = emacs_open (pty_name, O_RDWR | O_NDELAY, 0);
#endif
#endif
#endif
if (fd >= 0)
{
#ifdef PTY_TTY_NAME_SPRINTF
PTY_TTY_NAME_SPRINTF
#else
sprintf (pty_name, "/dev/tty%c%x", c, i);
#endif
#ifndef UNIPLUS
if (access (pty_name, 6) != 0)
{
emacs_close (fd);
#if !defined(IRIS) && !defined(__sgi)
continue;
#else
return -1;
#endif
}
#endif
setup_pty (fd);
return fd;
}
}
return -1;
}
#endif
Lisp_Object
make_process (name)
Lisp_Object name;
{
register Lisp_Object val, tem, name1;
register struct Lisp_Process *p;
char suffix[10];
register int i;
p = allocate_process ();
XSETINT (p->infd, -1);
XSETINT (p->outfd, -1);
XSETFASTINT (p->pid, 0);
XSETFASTINT (p->tick, 0);
XSETFASTINT (p->update_tick, 0);
p->raw_status_low = Qnil;
p->raw_status_high = Qnil;
p->status = Qrun;
p->mark = Fmake_marker ();
name1 = name;
for (i = 1; ; i++)
{
tem = Fget_process (name1);
if (NILP (tem)) break;
sprintf (suffix, "<%d>", i);
name1 = concat2 (name, build_string (suffix));
}
name = name1;
p->name = name;
XSETPROCESS (val, p);
Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
return val;
}
void
remove_process (proc)
register Lisp_Object proc;
{
register Lisp_Object pair;
pair = Frassq (proc, Vprocess_alist);
Vprocess_alist = Fdelq (pair, Vprocess_alist);
deactivate_process (proc);
}
DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
"Return t if OBJECT is a process.")
(object)
Lisp_Object object;
{
return PROCESSP (object) ? Qt : Qnil;
}
DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
"Return the process named NAME, or nil if there is none.")
(name)
register Lisp_Object name;
{
if (PROCESSP (name))
return name;
CHECK_STRING (name, 0);
return Fcdr (Fassoc (name, Vprocess_alist));
}
DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
"Return the (or a) process associated with BUFFER.\n\
BUFFER may be a buffer or the name of one.")
(buffer)
register Lisp_Object buffer;
{
register Lisp_Object buf, tail, proc;
if (NILP (buffer)) return Qnil;
buf = Fget_buffer (buffer);
if (NILP (buf)) return Qnil;
for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
{
proc = Fcdr (Fcar (tail));
if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
return proc;
}
return Qnil;
}
static Lisp_Object
get_process (name)
register Lisp_Object name;
{
register Lisp_Object proc, obj;
if (STRINGP (name))
{
obj = Fget_process (name);
if (NILP (obj))
obj = Fget_buffer (name);
if (NILP (obj))
error ("Process %s does not exist", XSTRING (name)->data);
}
else if (NILP (name))
obj = Fcurrent_buffer ();
else
obj = name;
if (BUFFERP (obj))
{
proc = Fget_buffer_process (obj);
if (NILP (proc))
error ("Buffer %s has no process", XSTRING (XBUFFER (obj)->name)->data);
}
else
{
CHECK_PROCESS (obj, 0);
proc = obj;
}
return proc;
}
DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
"Delete PROCESS: kill it and forget about it immediately.\n\
PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
nil, indicating the current buffer's process.")
(process)
register Lisp_Object process;
{
process = get_process (process);
XPROCESS (process)->raw_status_low = Qnil;
XPROCESS (process)->raw_status_high = Qnil;
if (NETCONN_P (process))
{
XPROCESS (process)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
XSETINT (XPROCESS (process)->tick, ++process_tick);
}
else if (XINT (XPROCESS (process)->infd) >= 0)
{
Fkill_process (process, Qnil);
XPROCESS (process)->status
= Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
XSETINT (XPROCESS (process)->tick, ++process_tick);
status_notify ();
}
remove_process (process);
return Qnil;
}
DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
"Return the status of PROCESS.\n\
The returned value is one of the following symbols:\n\
run -- for a process that is running.\n\
stop -- for a process stopped but continuable.\n\
exit -- for a process that has exited.\n\
signal -- for a process that has got a fatal signal.\n\
open -- for a network stream connection that is open.\n\
closed -- for a network stream connection that is closed.\n\
nil -- if arg is a process name and no such process exists.\n\
PROCESS may be a process, a buffer, the name of a process, or\n\
nil, indicating the current buffer's process.")
(process)
register Lisp_Object process;
{
register struct Lisp_Process *p;
register Lisp_Object status;
if (STRINGP (process))
process = Fget_process (process);
else
process = get_process (process);
if (NILP (process))
return process;
p = XPROCESS (process);
if (!NILP (p->raw_status_low))
update_status (p);
status = p->status;
if (CONSP (status))
status = XCAR (status);
if (NETCONN_P (process))
{
if (EQ (status, Qrun))
status = Qopen;
else if (EQ (status, Qexit))
status = Qclosed;
}
return status;
}
DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
1, 1, 0,
"Return the exit status of PROCESS or the signal number that killed it.\n\
If PROCESS has not yet exited or died, return 0.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
if (!NILP (XPROCESS (process)->raw_status_low))
update_status (XPROCESS (process));
if (CONSP (XPROCESS (process)->status))
return XCAR (XCDR (XPROCESS (process)->status));
return make_number (0);
}
DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
"Return the process id of PROCESS.\n\
This is the pid of the Unix process which PROCESS uses or talks to.\n\
For a network connection, this value is nil.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->pid;
}
DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
"Return the name of PROCESS, as a string.\n\
This is the name of the program invoked in PROCESS,\n\
possibly modified to make it unique among process names.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->name;
}
DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
"Return the command that was executed to start PROCESS.\n\
This is a list of strings, the first string being the program executed\n\
and the rest of the strings being the arguments given to it.\n\
For a non-child channel, this is nil.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->command;
}
DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
"Return the name of the terminal PROCESS uses, or nil if none.\n\
This is the terminal that the process itself reads and writes on,\n\
not the name of the pty that Emacs uses to talk with that terminal.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->tty_name;
}
DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
2, 2, 0,
"Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
(process, buffer)
register Lisp_Object process, buffer;
{
CHECK_PROCESS (process, 0);
if (!NILP (buffer))
CHECK_BUFFER (buffer, 1);
XPROCESS (process)->buffer = buffer;
return buffer;
}
DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
1, 1, 0,
"Return the buffer PROCESS is associated with.\n\
Output from PROCESS is inserted in this buffer unless PROCESS has a filter.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->buffer;
}
DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
1, 1, 0,
"Return the marker for the end of the last output from PROCESS.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->mark;
}
DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
2, 2, 0,
"Give PROCESS the filter function FILTER; nil means no filter.\n\
t means stop accepting output from the process.\n\
When a process has a filter, each time it does output\n\
the entire string of output is passed to the filter.\n\
The filter gets two arguments: the process and the string of output.\n\
If the process has a filter, its buffer is not used for output.")
(process, filter)
register Lisp_Object process, filter;
{
struct Lisp_Process *p;
CHECK_PROCESS (process, 0);
p = XPROCESS (process);
if (XINT (p->infd) >= 0)
{
if (EQ (filter, Qt))
{
FD_CLR (XINT (p->infd), &input_wait_mask);
FD_CLR (XINT (p->infd), &non_keyboard_wait_mask);
}
else if (EQ (XPROCESS (process)->filter, Qt))
{
FD_SET (XINT (p->infd), &input_wait_mask);
FD_SET (XINT (p->infd), &non_keyboard_wait_mask);
}
}
p->filter = filter;
return filter;
}
DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
1, 1, 0,
"Returns the filter function of PROCESS; nil if none.\n\
See `set-process-filter' for more info on filter functions.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->filter;
}
DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
2, 2, 0,
"Give PROCESS the sentinel SENTINEL; nil for none.\n\
The sentinel is called as a function when the process changes state.\n\
It gets two arguments: the process, and a string describing the change.")
(process, sentinel)
register Lisp_Object process, sentinel;
{
CHECK_PROCESS (process, 0);
XPROCESS (process)->sentinel = sentinel;
return sentinel;
}
DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
1, 1, 0,
"Return the sentinel of PROCESS; nil if none.\n\
See `set-process-sentinel' for more info on sentinels.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->sentinel;
}
DEFUN ("set-process-window-size", Fset_process_window_size,
Sset_process_window_size, 3, 3, 0,
"Tell PROCESS that it has logical window size HEIGHT and WIDTH.")
(process, height, width)
register Lisp_Object process, height, width;
{
CHECK_PROCESS (process, 0);
CHECK_NATNUM (height, 0);
CHECK_NATNUM (width, 0);
if (XINT (XPROCESS (process)->infd) < 0
|| set_window_size (XINT (XPROCESS (process)->infd),
XINT (height), XINT (width)) <= 0)
return Qnil;
else
return Qt;
}
DEFUN ("set-process-inherit-coding-system-flag",
Fset_process_inherit_coding_system_flag,
Sset_process_inherit_coding_system_flag, 2, 2, 0,
"Determine whether buffer of PROCESS will inherit coding-system.\n\
If the second argument FLAG is non-nil, then the variable\n\
`buffer-file-coding-system' of the buffer associated with PROCESS\n\
will be bound to the value of the coding system used to decode\n\
the process output.\n\
\n\
This is useful when the coding system specified for the process buffer\n\
leaves either the character code conversion or the end-of-line conversion\n\
unspecified, or if the coding system used to decode the process output\n\
is more appropriate for saving the process buffer.\n\
\n\
Binding the variable `inherit-process-coding-system' to non-nil before\n\
starting the process is an alternative way of setting the inherit flag\n\
for the process which will run.")
(process, flag)
register Lisp_Object process, flag;
{
CHECK_PROCESS (process, 0);
XPROCESS (process)->inherit_coding_system_flag = flag;
return flag;
}
DEFUN ("process-inherit-coding-system-flag",
Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
1, 1, 0,
"Return the value of inherit-coding-system flag for PROCESS.\n\
If this flag is t, `buffer-file-coding-system' of the buffer\n\
associated with PROCESS will inherit the coding system used to decode\n\
the process output.")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->inherit_coding_system_flag;
}
DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
Sprocess_kill_without_query, 1, 2, 0,
"Say no query needed if PROCESS is running when Emacs is exited.\n\
Optional second argument if non-nil says to require a query.\n\
Value is t if a query was formerly required.")
(process, value)
register Lisp_Object process, value;
{
Lisp_Object tem;
CHECK_PROCESS (process, 0);
tem = XPROCESS (process)->kill_without_query;
XPROCESS (process)->kill_without_query = Fnull (value);
return Fnull (tem);
}
DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
1, 1, 0,
"Return the contact info of PROCESS; t for a real child.\n\
For a net connection, the value is a cons cell of the form (HOST SERVICE).")
(process)
register Lisp_Object process;
{
CHECK_PROCESS (process, 0);
return XPROCESS (process)->childp;
}
#if 0
DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0,
"Return the connection type of PROCESS.\n\
The value is nil for a pipe, t or `pty' for a pty, or `stream' for\n\
a socket connection.")
(process)
Lisp_Object process;
{
return XPROCESS (process)->type;
}
#endif
Lisp_Object
list_processes_1 ()
{
register Lisp_Object tail, tem;
Lisp_Object proc, minspace, tem1;
register struct Lisp_Process *p;
char tembuf[80];
XSETFASTINT (minspace, 1);
set_buffer_internal (XBUFFER (Vstandard_output));
Fbuffer_disable_undo (Vstandard_output);
current_buffer->truncate_lines = Qt;
write_string ("\
Proc Status Buffer Tty Command\n\
---- ------ ------ --- -------\n", -1);
for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
{
Lisp_Object symbol;
proc = Fcdr (Fcar (tail));
p = XPROCESS (proc);
if (NILP (p->childp))
continue;
Finsert (1, &p->name);
Findent_to (make_number (13), minspace);
if (!NILP (p->raw_status_low))
update_status (p);
symbol = p->status;
if (CONSP (p->status))
symbol = XCAR (p->status);
if (EQ (symbol, Qsignal))
{
Lisp_Object tem;
tem = Fcar (Fcdr (p->status));
#ifdef VMS
if (XINT (tem) < NSIG)
write_string (sys_errlist [XINT (tem)], -1);
else
#endif
Fprinc (symbol, Qnil);
}
else if (NETCONN_P (proc))
{
if (EQ (symbol, Qrun))
write_string ("open", -1);
else if (EQ (symbol, Qexit))
write_string ("closed", -1);
else
Fprinc (symbol, Qnil);
}
else
Fprinc (symbol, Qnil);
if (EQ (symbol, Qexit))
{
Lisp_Object tem;
tem = Fcar (Fcdr (p->status));
if (XFASTINT (tem))
{
sprintf (tembuf, " %d", (int) XFASTINT (tem));
write_string (tembuf, -1);
}
}
if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
remove_process (proc);
Findent_to (make_number (22), minspace);
if (NILP (p->buffer))
insert_string ("(none)");
else if (NILP (XBUFFER (p->buffer)->name))
insert_string ("(Killed)");
else
Finsert (1, &XBUFFER (p->buffer)->name);
Findent_to (make_number (37), minspace);
if (STRINGP (p->tty_name))
Finsert (1, &p->tty_name);
else
insert_string ("(none)");
Findent_to (make_number (49), minspace);
if (NETCONN_P (proc))
{
sprintf (tembuf, "(network stream connection to %s)\n",
XSTRING (XCAR (p->childp))->data);
insert_string (tembuf);
}
else
{
tem = p->command;
while (1)
{
tem1 = Fcar (tem);
Finsert (1, &tem1);
tem = Fcdr (tem);
if (NILP (tem))
break;
insert_string (" ");
}
insert_string ("\n");
}
}
return Qnil;
}
DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
"Display a list of all processes.\n\
Any process listed as exited or signaled is actually eliminated\n\
after the listing is made.")
()
{
internal_with_output_to_temp_buffer ("*Process List*",
list_processes_1, Qnil);
return Qnil;
}
DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
"Return a list of all processes.")
()
{
return Fmapcar (Qcdr, Vprocess_alist);
}
static Lisp_Object start_process_unwind ();
DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
"Start a program in a subprocess. Return the process object for it.\n\
NAME is name for process. It is modified if necessary to make it unique.\n\
BUFFER is the buffer or (buffer-name) to associate with the process.\n\
Process output goes at end of that buffer, unless you specify\n\
an output stream or filter function to handle the output.\n\
BUFFER may be also nil, meaning that this process is not associated\n\
with any buffer.\n\
Third arg is program file name. It is searched for in PATH.\n\
Remaining arguments are strings to give program as arguments.")
(nargs, args)
int nargs;
register Lisp_Object *args;
{
Lisp_Object buffer, name, program, proc, current_dir, tem;
#ifdef VMS
register unsigned char *new_argv;
int len;
#else
register unsigned char **new_argv;
#endif
register int i;
int count = specpdl_ptr - specpdl;
buffer = args[1];
if (!NILP (buffer))
buffer = Fget_buffer_create (buffer);
{
struct gcpro gcpro1, gcpro2;
current_dir = current_buffer->directory;
GCPRO2 (buffer, current_dir);
current_dir
= expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
Qnil);
if (NILP (Ffile_accessible_directory_p (current_dir)))
report_file_error ("Setting current directory",
Fcons (current_buffer->directory, Qnil));
UNGCPRO;
}
name = args[0];
CHECK_STRING (name, 0);
program = args[2];
CHECK_STRING (program, 2);
proc = make_process (name);
record_unwind_protect (start_process_unwind, proc);
XPROCESS (proc)->childp = Qt;
XPROCESS (proc)->command_channel_p = Qnil;
XPROCESS (proc)->buffer = buffer;
XPROCESS (proc)->sentinel = Qnil;
XPROCESS (proc)->filter = Qnil;
XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
if (!NILP (buffer))
set_marker_both (XPROCESS (proc)->mark, buffer,
BUF_ZV (XBUFFER (buffer)),
BUF_ZV_BYTE (XBUFFER (buffer)));
{
Lisp_Object coding_systems = Qt;
Lisp_Object val, *args2;
struct gcpro gcpro1, gcpro2;
val = Vcoding_system_for_read;
if (NILP (val))
{
args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
args2[0] = Qstart_process;
for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
GCPRO2 (proc, current_dir);
coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
UNGCPRO;
if (CONSP (coding_systems))
val = XCAR (coding_systems);
else if (CONSP (Vdefault_process_coding_system))
val = XCAR (Vdefault_process_coding_system);
}
XPROCESS (proc)->decode_coding_system = val;
val = Vcoding_system_for_write;
if (NILP (val))
{
if (EQ (coding_systems, Qt))
{
args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof args2);
args2[0] = Qstart_process;
for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
GCPRO2 (proc, current_dir);
coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
UNGCPRO;
}
if (CONSP (coding_systems))
val = XCDR (coding_systems);
else if (CONSP (Vdefault_process_coding_system))
val = XCDR (Vdefault_process_coding_system);
}
XPROCESS (proc)->encode_coding_system = val;
}
#ifdef VMS
len = STRING_BYTES (XSTRING (program)) + 2;
for (i = 3; i < nargs; i++)
{
tem = args[i];
CHECK_STRING (tem, i);
len += STRING_BYTES (XSTRING (tem)) + 1;
}
new_argv = (unsigned char *) alloca (len);
strcpy (new_argv, XSTRING (program)->data);
for (i = 3; i < nargs; i++)
{
tem = args[i];
CHECK_STRING (tem, i);
strcat (new_argv, " ");
strcat (new_argv, XSTRING (tem)->data);
}
#else
new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
if (!IS_DIRECTORY_SEP (XSTRING (program)->data[0])
&& !(XSTRING (program)->size > 1
&& IS_DEVICE_SEP (XSTRING (program)->data[1])))
{
struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
tem = Qnil;
GCPRO4 (name, program, buffer, current_dir);
openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1);
UNGCPRO;
if (NILP (tem))
report_file_error ("Searching for program", Fcons (program, Qnil));
tem = Fexpand_file_name (tem, Qnil);
tem = ENCODE_FILE (tem);
new_argv[0] = XSTRING (tem)->data;
}
else
{
if (!NILP (Ffile_directory_p (program)))
error ("Specified program for new process is a directory");
tem = ENCODE_FILE (program);
new_argv[0] = XSTRING (tem)->data;
}
for (i = 3; i < nargs; i++)
{
tem = args[i];
CHECK_STRING (tem, i);
if (STRING_MULTIBYTE (tem))
tem = (code_convert_string_norecord
(tem, XPROCESS (proc)->encode_coding_system, 1));
new_argv[i - 2] = XSTRING (tem)->data;
}
new_argv[i - 2] = 0;
#endif
XPROCESS (proc)->decoding_buf = make_uninit_string (0);
XPROCESS (proc)->decoding_carryover = make_number (0);
XPROCESS (proc)->encoding_buf = make_uninit_string (0);
XPROCESS (proc)->encoding_carryover = make_number (0);
XPROCESS (proc)->inherit_coding_system_flag
= (NILP (buffer) || !inherit_process_coding_system
? Qnil : Qt);
create_process (proc, (char **) new_argv, current_dir);
return unbind_to (count, proc);
}
static Lisp_Object
start_process_unwind (proc)
Lisp_Object proc;
{
if (!PROCESSP (proc))
abort ();
if (XINT (XPROCESS (proc)->pid) <= 0)
remove_process (proc);
return Qnil;
}
void
create_process_1 (timer)
struct atimer *timer;
{
}
#if 0
#ifdef USG
#ifdef SIGCHLD
int sigchld_deferred;
SIGTYPE
create_process_sigchld ()
{
signal (SIGCHLD, create_process_sigchld);
sigchld_deferred = 1;
}
#endif
#endif
#endif
#ifndef VMS
void
create_process (process, new_argv, current_dir)
Lisp_Object process;
char **new_argv;
Lisp_Object current_dir;
{
int pid, inchannel, outchannel;
int sv[2];
#ifdef POSIX_SIGNALS
sigset_t procmask;
sigset_t blocked;
struct sigaction sigint_action;
struct sigaction sigquit_action;
#ifdef AIX
struct sigaction sighup_action;
#endif
#else
#if 0
#ifdef SIGCHLD
SIGTYPE (*sigchld)();
#endif
#endif
#endif
volatile int forkin, forkout;
volatile int pty_flag = 0;
#ifndef USE_CRT_DLL
extern char **environ;
#endif
Lisp_Object buffer = XPROCESS (process)->buffer;
inchannel = outchannel = -1;
#ifdef HAVE_PTYS
if (!NILP (Vprocess_connection_type))
outchannel = inchannel = allocate_pty ();
if (inchannel >= 0)
{
#ifndef USG
#ifdef O_NOCTTY
forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
#else
forkout = forkin = emacs_open (pty_name, O_RDWR, 0);
#endif
if (forkin < 0)
report_file_error ("Opening pty", Qnil);
#else
forkin = forkout = -1;
#endif
pty_flag = 1;
}
else
#endif
#ifdef SKTPAIR
{
if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
report_file_error ("Opening socketpair", Qnil);
outchannel = inchannel = sv[0];
forkout = forkin = sv[1];
}
#else
{
int tem;
tem = pipe (sv);
if (tem < 0)
report_file_error ("Creating pipe", Qnil);
inchannel = sv[0];
forkout = sv[1];
tem = pipe (sv);
if (tem < 0)
{
emacs_close (inchannel);
emacs_close (forkout);
report_file_error ("Creating pipe", Qnil);
}
outchannel = sv[1];
forkin = sv[0];
}
#endif
#if 0
set_exclusive_use (inchannel);
set_exclusive_use (outchannel);
#endif
#if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
{
int one = 1;
ioctl (inchannel, FIONBIO, &one);
}
#endif
#ifdef O_NONBLOCK
fcntl (inchannel, F_SETFL, O_NONBLOCK);
fcntl (outchannel, F_SETFL, O_NONBLOCK);
#else
#ifdef O_NDELAY
fcntl (inchannel, F_SETFL, O_NDELAY);
fcntl (outchannel, F_SETFL, O_NDELAY);
#endif
#endif
chan_process[inchannel] = process;
XSETINT (XPROCESS (process)->infd, inchannel);
XSETINT (XPROCESS (process)->outfd, outchannel);
if (forkin < 0)
XPROCESS (process)->subtty = Qnil;
else
XSETFASTINT (XPROCESS (process)->subtty, forkin);
XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
XPROCESS (process)->status = Qrun;
if (!proc_decode_coding_system[inchannel])
proc_decode_coding_system[inchannel]
= (struct coding_system *) xmalloc (sizeof (struct coding_system));
setup_coding_system (XPROCESS (process)->decode_coding_system,
proc_decode_coding_system[inchannel]);
if (!proc_encode_coding_system[outchannel])
proc_encode_coding_system[outchannel]
= (struct coding_system *) xmalloc (sizeof (struct coding_system));
setup_coding_system (XPROCESS (process)->encode_coding_system,
proc_encode_coding_system[outchannel]);
#ifdef POSIX_SIGNALS
sigemptyset (&blocked);
#ifdef SIGCHLD
sigaddset (&blocked, SIGCHLD);
#endif
#ifdef HAVE_VFORK
sigaddset (&blocked, SIGINT ); sigaction (SIGINT , 0, &sigint_action );
sigaddset (&blocked, SIGQUIT); sigaction (SIGQUIT, 0, &sigquit_action);
#ifdef AIX
sigaddset (&blocked, SIGHUP ); sigaction (SIGHUP , 0, &sighup_action );
#endif
#endif
sigprocmask (SIG_BLOCK, &blocked, &procmask);
#else
#ifdef SIGCHLD
#ifdef BSD4_1
sighold (SIGCHLD);
#else
#if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
sigsetmask (sigmask (SIGCHLD));
#else
#if 0
sigchld_deferred = 0;
sigchld = signal (SIGCHLD, create_process_sigchld);
#endif
#endif
#endif
#endif
#endif
FD_SET (inchannel, &input_wait_mask);
FD_SET (inchannel, &non_keyboard_wait_mask);
if (inchannel > max_process_desc)
max_process_desc = inchannel;
XSETINT (XPROCESS (process)->pid, -1);
BLOCK_INPUT;
{
char **save_environ = environ;
current_dir = ENCODE_FILE (current_dir);
#ifndef WINDOWSNT
#ifndef PUMA_VFORK_ISSUES_CLEARED_UP
pid = fork ();
#else
pid = vfork ();
#endif
if (pid == 0)
#endif
{
int xforkin = forkin;
int xforkout = forkout;
#if 0
sigsetmask (SIGEMPTYMASK);
#endif
#ifdef HAVE_PTYS
#ifdef HAVE_SETSID
setsid ();
if (pty_flag)
{
#ifdef TIOCSCTTY
ioctl (xforkin, TIOCSCTTY, 0);
#endif
}
#else
#ifdef USG
setpgrp ();
#endif
#endif
#if defined (HAVE_TERMIOS) && defined (LDISC1)
if (pty_flag && xforkin >= 0)
{
struct termios t;
tcgetattr (xforkin, &t);
t.c_lflag = LDISC1;
if (tcsetattr (xforkin, TCSANOW, &t) < 0)
emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
}
#else
#if defined (NTTYDISC) && defined (TIOCSETD)
if (pty_flag && xforkin >= 0)
{
int ldisc = NTTYDISC;
ioctl (xforkin, TIOCSETD, &ldisc);
}
#endif
#endif
#ifdef TIOCNOTTY
if (pty_flag)
{
int j = emacs_open ("/dev/tty", O_RDWR, 0);
ioctl (j, TIOCNOTTY, 0);
emacs_close (j);
#ifndef USG
#ifdef HAVE_SETPGID
setpgid (0, 0);
#else
setpgrp (0, 0);
#endif
#endif
}
#endif
#if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
if (pty_flag)
{
#ifdef SET_CHILD_PTY_PGRP
int pgrp = getpid ();
#endif
if (xforkin >= 0)
emacs_close (xforkin);
xforkout = xforkin = emacs_open (pty_name, O_RDWR, 0);
if (xforkin < 0)
{
emacs_write (1, "Couldn't open the pty terminal ", 31);
emacs_write (1, pty_name, strlen (pty_name));
emacs_write (1, "\n", 1);
_exit (1);
}
#ifdef SET_CHILD_PTY_PGRP
ioctl (xforkin, TIOCSPGRP, &pgrp);
ioctl (xforkout, TIOCSPGRP, &pgrp);
#endif
}
#endif
#ifdef SETUP_SLAVE_PTY
if (pty_flag)
{
SETUP_SLAVE_PTY;
}
#endif
#ifdef AIX
if (pty_flag)
signal (SIGHUP, SIG_DFL);
#endif
#endif
signal (SIGINT, SIG_DFL);
signal (SIGQUIT, SIG_DFL);
#ifdef POSIX_SIGNALS
sigprocmask (SIG_SETMASK, &procmask, 0);
#else
#ifdef SIGCHLD
#ifdef BSD4_1
sigrelse (SIGCHLD);
#else
#if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
sigsetmask (SIGEMPTYMASK);
#else
#if 0
signal (SIGCHLD, sigchld);
#endif
#endif
#endif
#endif
#endif
if (pty_flag)
child_setup_tty (xforkout);
#ifdef WINDOWSNT
pid = child_setup (xforkin, xforkout, xforkout,
new_argv, 1, current_dir);
#else
child_setup (xforkin, xforkout, xforkout,
new_argv, 1, current_dir);
#endif
}
environ = save_environ;
}
UNBLOCK_INPUT;
if (pid < 0)
{
if (forkin >= 0)
emacs_close (forkin);
if (forkin != forkout && forkout >= 0)
emacs_close (forkout);
}
else
{
XSETFASTINT (XPROCESS (process)->pid, pid);
#ifdef WINDOWSNT
register_child (pid, inchannel);
#endif
{
struct atimer *timer;
EMACS_TIME offset;
stop_polling ();
EMACS_SET_SECS_USECS (offset, 1, 0);
timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
XPROCESS (process)->subtty = Qnil;
if (forkin >= 0)
emacs_close (forkin);
cancel_atimer (timer);
start_polling ();
}
if (forkin != forkout && forkout >= 0)
emacs_close (forkout);
#ifdef HAVE_PTYS
if (pty_flag)
XPROCESS (process)->tty_name = build_string (pty_name);
else
#endif
XPROCESS (process)->tty_name = Qnil;
}
#ifdef POSIX_SIGNALS
#ifdef HAVE_VFORK
sigaction (SIGINT, &sigint_action, 0);
sigaction (SIGQUIT, &sigquit_action, 0);
#ifdef AIX
sigaction (SIGHUP, &sighup_action, 0);
#endif
#endif
sigprocmask (SIG_SETMASK, &procmask, 0);
#else
#ifdef SIGCHLD
#ifdef BSD4_1
sigrelse (SIGCHLD);
#else
#if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
sigsetmask (SIGEMPTYMASK);
#else
#if 0
signal (SIGCHLD, sigchld);
if (sigchld_deferred)
kill (getpid (), SIGCHLD);
#endif
#endif
#endif
#endif
#endif
if (pid < 0)
report_file_error ("Doing vfork", Qnil);
}
#endif
#ifdef HAVE_SOCKETS
DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream,
4, 4, 0,
"Open a TCP connection for a service to a host.\n\
Returns a subprocess-object to represent the connection.\n\
Input and output work as for subprocesses; `delete-process' closes it.\n\
Args are NAME BUFFER HOST SERVICE.\n\
NAME is name for process. It is modified if necessary to make it unique.\n\
BUFFER is the buffer (or buffer-name) to associate with the process.\n\
Process output goes at end of that buffer, unless you specify\n\
an output stream or filter function to handle the output.\n\
BUFFER may be also nil, meaning that this process is not associated\n\
with any buffer\n\
Third arg is name of the host to connect to, or its IP address.\n\
Fourth arg SERVICE is name of the service desired, or an integer\n\
specifying a port number to connect to.")
(name, buffer, host, service)
Lisp_Object name, buffer, host, service;
{
Lisp_Object proc;
#ifdef HAVE_GETADDRINFO
struct addrinfo hints, *res, *lres;
int ret = 0;
int xerrno = 0;
char *portstring, portbuf[128];
#else
struct sockaddr_in address;
struct servent *svc_info;
struct hostent *host_info_ptr, host_info;
char *(addr_list[2]);
IN_ADDR numeric_addr;
int port;
#endif
int s = -1, outch, inch;
struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
int retry = 0;
int count = specpdl_ptr - specpdl;
int count1;
#ifdef WINDOWSNT
init_winsock (TRUE);
#endif
GCPRO4 (name, buffer, host, service);
CHECK_STRING (name, 0);
CHECK_STRING (host, 0);
#ifdef HAVE_GETADDRINFO
if (INTEGERP (service))
{
sprintf (portbuf, "%ld", (long) XINT (service));
portstring = portbuf;
}
else
{
CHECK_STRING (service, 0);
portstring = XSTRING (service)->data;
}
#else
if (INTEGERP (service))
port = htons ((unsigned short) XINT (service));
else
{
CHECK_STRING (service, 0);
svc_info = getservbyname (XSTRING (service)->data, "tcp");
if (svc_info == 0)
error ("Unknown service \"%s\"", XSTRING (service)->data);
port = svc_info->s_port;
}
#endif
#ifdef POLL_FOR_INPUT
record_unwind_protect (unwind_stop_other_atimers, Qnil);
bind_polling_period (10);
#endif
#ifndef TERM
#ifdef HAVE_GETADDRINFO
immediate_quit = 1;
QUIT;
memset (&hints, 0, sizeof (hints));
hints.ai_flags = 0;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
ret = getaddrinfo (XSTRING (host)->data, portstring, &hints, &res);
if (ret)
#ifdef HAVE_GAI_STRERROR
error ("%s/%s %s", XSTRING (host)->data, portstring, gai_strerror(ret));
#else
error ("%s/%s getaddrinfo error %d", XSTRING (host)->data, portstring,
ret);
#endif
immediate_quit = 0;
count1 = specpdl_ptr - specpdl;
s = -1;
for (lres = res; lres; lres = lres->ai_next)
{
s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol);
if (s < 0)
{
xerrno = errno;
continue;
}
if (interrupt_input)
unrequest_sigio ();
count1 = specpdl_ptr - specpdl;
record_unwind_protect (close_file_unwind, make_number (s));
loop:
immediate_quit = 1;
QUIT;
turn_on_atimers (0);
ret = connect (s, lres->ai_addr, lres->ai_addrlen);
xerrno = errno;
turn_on_atimers (1);
if (ret == 0 || xerrno == EISCONN)
break;
immediate_quit = 0;
if (xerrno == EINTR)
goto loop;
if (xerrno == EADDRINUSE && retry < 20)
{
Fsleep_for (make_number (1), Qnil);
retry++;
goto loop;
}
specpdl_ptr = specpdl + count1;
count1 = specpdl_ptr - specpdl;
emacs_close (s);
s = -1;
}
freeaddrinfo (res);
if (s < 0)
{
if (interrupt_input)
request_sigio ();
errno = xerrno;
report_file_error ("connection failed",
Fcons (host, Fcons (name, Qnil)));
}
#else
while (1)
{
#if 0
#ifdef TRY_AGAIN
h_errno = 0;
#endif
#endif
immediate_quit = 1;
QUIT;
host_info_ptr = gethostbyname (XSTRING (host)->data);
immediate_quit = 0;
#if 0
#ifdef TRY_AGAIN
if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN))
#endif
#endif
break;
Fsleep_for (make_number (1), Qnil);
}
if (host_info_ptr == 0)
{
numeric_addr = inet_addr ((char *) XSTRING (host)->data);
if (NUMERIC_ADDR_ERROR)
error ("Unknown host \"%s\"", XSTRING (host)->data);
host_info_ptr = &host_info;
host_info.h_name = 0;
host_info.h_aliases = 0;
host_info.h_addrtype = AF_INET;
#ifdef h_addr
host_info.h_addr_list = &(addr_list[0]);
#endif
host_info.h_addr = (char*)(&numeric_addr);
addr_list[1] = 0;
host_info.h_length = sizeof (numeric_addr);
}
bzero (&address, sizeof address);
bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr,
host_info_ptr->h_length);
address.sin_family = host_info_ptr->h_addrtype;
address.sin_port = port;
s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
if (s < 0)
report_file_error ("error creating socket", Fcons (name, Qnil));
count1 = specpdl_ptr - specpdl;
record_unwind_protect (close_file_unwind, make_number (s));
if (interrupt_input)
unrequest_sigio ();
loop:
immediate_quit = 1;
QUIT;
if (connect (s, (struct sockaddr *) &address, sizeof address) == -1
&& errno != EISCONN)
{
int xerrno = errno;
immediate_quit = 0;
if (errno == EINTR)
goto loop;
if (errno == EADDRINUSE && retry < 20)
{
Fsleep_for (make_number (1), Qnil);
retry++;
goto loop;
}
specpdl_ptr = specpdl + count1;
emacs_close (s);
if (interrupt_input)
request_sigio ();
errno = xerrno;
report_file_error ("connection failed",
Fcons (host, Fcons (name, Qnil)));
}
#endif
immediate_quit = 0;
specpdl_ptr = specpdl + count1;
#ifdef POLL_FOR_INPUT
unbind_to (count, Qnil);
#endif
if (interrupt_input)
request_sigio ();
#else
s = connect_server (0);
if (s < 0)
report_file_error ("error creating socket", Fcons (name, Qnil));
send_command (s, C_PORT, 0, "%s:%d", XSTRING (host)->data, ntohs (port));
send_command (s, C_DUMB, 1, 0);
#endif
inch = s;
outch = s;
if (!NILP (buffer))
buffer = Fget_buffer_create (buffer);
proc = make_process (name);
chan_process[inch] = proc;
#ifdef O_NONBLOCK
fcntl (inch, F_SETFL, O_NONBLOCK);
#else
#ifdef O_NDELAY
fcntl (inch, F_SETFL, O_NDELAY);
#endif
#endif
XPROCESS (proc)->childp = Fcons (host, Fcons (service, Qnil));
XPROCESS (proc)->command_channel_p = Qnil;
XPROCESS (proc)->buffer = buffer;
XPROCESS (proc)->sentinel = Qnil;
XPROCESS (proc)->filter = Qnil;
XPROCESS (proc)->command = Qnil;
XPROCESS (proc)->pid = Qnil;
XSETINT (XPROCESS (proc)->infd, inch);
XSETINT (XPROCESS (proc)->outfd, outch);
XPROCESS (proc)->status = Qrun;
FD_SET (inch, &input_wait_mask);
FD_SET (inch, &non_keyboard_wait_mask);
if (inch > max_process_desc)
max_process_desc = inch;
{
struct gcpro gcpro1;
Lisp_Object coding_systems = Qt;
Lisp_Object args[5], val;
if (!NILP (Vcoding_system_for_read))
val = Vcoding_system_for_read;
else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
|| (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
val = Qnil;
else
{
args[0] = Qopen_network_stream, args[1] = name,
args[2] = buffer, args[3] = host, args[4] = service;
GCPRO1 (proc);
coding_systems = Ffind_operation_coding_system (5, args);
UNGCPRO;
if (CONSP (coding_systems))
val = XCAR (coding_systems);
else if (CONSP (Vdefault_process_coding_system))
val = XCAR (Vdefault_process_coding_system);
else
val = Qnil;
}
XPROCESS (proc)->decode_coding_system = val;
if (!NILP (Vcoding_system_for_write))
val = Vcoding_system_for_write;
else if (NILP (current_buffer->enable_multibyte_characters))
val = Qnil;
else
{
if (EQ (coding_systems, Qt))
{
args[0] = Qopen_network_stream, args[1] = name,
args[2] = buffer, args[3] = host, args[4] = service;
GCPRO1 (proc);
coding_systems = Ffind_operation_coding_system (5, args);
UNGCPRO;
}
if (CONSP (coding_systems))
val = XCDR (coding_systems);
else if (CONSP (Vdefault_process_coding_system))
val = XCDR (Vdefault_process_coding_system);
else
val = Qnil;
}
XPROCESS (proc)->encode_coding_system = val;
}
if (!proc_decode_coding_system[inch])
proc_decode_coding_system[inch]
= (struct coding_system *) xmalloc (sizeof (struct coding_system));
setup_coding_system (XPROCESS (proc)->decode_coding_system,
proc_decode_coding_system[inch]);
if (!proc_encode_coding_system[outch])
proc_encode_coding_system[outch]
= (struct coding_system *) xmalloc (sizeof (struct coding_system));
setup_coding_system (XPROCESS (proc)->encode_coding_system,
proc_encode_coding_system[outch]);
XPROCESS (proc)->decoding_buf = make_uninit_string (0);
XPROCESS (proc)->decoding_carryover = make_number (0);
XPROCESS (proc)->encoding_buf = make_uninit_string (0);
XPROCESS (proc)->encoding_carryover = make_number (0);
XPROCESS (proc)->inherit_coding_system_flag
= (NILP (buffer) || !inherit_process_coding_system
? Qnil : Qt);
UNGCPRO;
return proc;
}
#endif
void
deactivate_process (proc)
Lisp_Object proc;
{
register int inchannel, outchannel;
register struct Lisp_Process *p = XPROCESS (proc);
inchannel = XINT (p->infd);
outchannel = XINT (p->outfd);
if (inchannel >= 0)
{
flush_pending_output (inchannel);
#ifdef VMS
{
VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
sys$dassgn (outchannel);
vs = get_vms_process_pointer (p->pid);
if (vs)
give_back_vms_process_stuff (vs);
}
#else
emacs_close (inchannel);
if (outchannel >= 0 && outchannel != inchannel)
emacs_close (outchannel);
#endif
XSETINT (p->infd, -1);
XSETINT (p->outfd, -1);
chan_process[inchannel] = Qnil;
FD_CLR (inchannel, &input_wait_mask);
FD_CLR (inchannel, &non_keyboard_wait_mask);
if (inchannel == max_process_desc)
{
int i;
max_process_desc = 0;
for (i = 0; i < MAXDESC; i++)
if (!NILP (chan_process[i]))
max_process_desc = i;
}
}
}
void
close_process_descs ()
{
#ifndef WINDOWSNT
int i;
for (i = 0; i < MAXDESC; i++)
{
Lisp_Object process;
process = chan_process[i];
if (!NILP (process))
{
int in = XINT (XPROCESS (process)->infd);
int out = XINT (XPROCESS (process)->outfd);
if (in >= 0)
emacs_close (in);
if (out >= 0 && in != out)
emacs_close (out);
}
}
#endif
}
DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
0, 3, 0,
"Allow any pending output from subprocesses to be read by Emacs.\n\
It is read into the process' buffers or given to their filter functions.\n\
Non-nil arg PROCESS means do not return until some output has been received\n\
from PROCESS.\n\
Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
seconds and microseconds to wait; return after that much time whether\n\
or not there is input.\n\
Return non-nil iff we received any output before the timeout expired.")
(process, timeout, timeout_msecs)
register Lisp_Object process, timeout, timeout_msecs;
{
int seconds;
int useconds;
if (! NILP (process))
CHECK_PROCESS (process, 0);
if (! NILP (timeout_msecs))
{
CHECK_NUMBER (timeout_msecs, 2);
useconds = XINT (timeout_msecs);
if (!INTEGERP (timeout))
XSETINT (timeout, 0);
{
int carry = useconds / 1000000;
XSETINT (timeout, XINT (timeout) + carry);
useconds -= carry * 1000000;
if (useconds < 0)
{
XSETINT (timeout, XINT (timeout) - 1);
useconds += 1000000;
}
}
}
else
useconds = 0;
if (! NILP (timeout))
{
CHECK_NUMBER (timeout, 1);
seconds = XINT (timeout);
if (seconds < 0 || (seconds == 0 && useconds == 0))
seconds = -1;
}
else
{
if (NILP (process))
seconds = -1;
else
seconds = 0;
}
if (NILP (process))
XSETFASTINT (process, 0);
return
(wait_reading_process_input (seconds, useconds, process, 0)
? Qt : Qnil);
}
static int waiting_for_user_input_p;
static void
wait_reading_process_input_1 ()
{
}
int
wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
int time_limit, microsecs;
Lisp_Object read_kbd;
int do_display;
{
register int channel, nfds;
static SELECT_TYPE Available;
int xerrno;
Lisp_Object proc;
EMACS_TIME timeout, end_time;
SELECT_TYPE Atemp;
int wait_channel = -1;
struct Lisp_Process *wait_proc = 0;
int got_some_input = 0;
Lisp_Object *wait_for_cell = 0;
FD_ZERO (&Available);
if (PROCESSP (read_kbd))
{
wait_proc = XPROCESS (read_kbd);
wait_channel = XINT (wait_proc->infd);
XSETFASTINT (read_kbd, 0);
}
if (CONSP (read_kbd))
{
wait_for_cell = &XCAR (read_kbd);
XSETFASTINT (read_kbd, 0);
}
waiting_for_user_input_p = XINT (read_kbd);
if (time_limit || microsecs)
{
EMACS_GET_TIME (end_time);
EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
EMACS_ADD_TIME (end_time, end_time, timeout);
}
#ifdef hpux
turn_on_atimers (0);
#endif
while (1)
{
int timeout_reduced_for_timers = 0;
if (XINT (read_kbd) >= 0)
QUIT;
if (wait_for_cell && ! NILP (*wait_for_cell))
break;
if (time_limit == -1)
{
EMACS_SET_SECS_USECS (timeout, 0, 0);
}
else if (time_limit || microsecs)
{
EMACS_GET_TIME (timeout);
EMACS_SUB_TIME (timeout, end_time, timeout);
if (EMACS_TIME_NEG_P (timeout))
break;
}
else
{
EMACS_SET_SECS_USECS (timeout, 100000, 0);
}
if (! wait_for_cell)
{
EMACS_TIME timer_delay;
do
{
int old_timers_run = timers_run;
struct buffer *old_buffer = current_buffer;
timer_delay = timer_check (1);
if (timers_run != old_timers_run
&& old_buffer != current_buffer
&& waiting_for_user_input_p == -1)
record_asynch_buffer_change ();
if (timers_run != old_timers_run && do_display)
redisplay_preserve_echo_area (9);
else
break;
}
while (!detect_input_pending ());
if (XINT (read_kbd) != 0
&& requeued_events_pending_p ())
break;
if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
{
EMACS_TIME difference;
EMACS_SUB_TIME (difference, timer_delay, timeout);
if (EMACS_TIME_NEG_P (difference))
{
timeout = timer_delay;
timeout_reduced_for_timers = 1;
}
}
else if (time_limit != -1)
{
wait_reading_process_input_1 ();
}
}
if (XINT (read_kbd) < 0)
set_waiting_for_input (&timeout);
if (update_tick != process_tick && do_display)
{
Atemp = input_wait_mask;
EMACS_SET_SECS_USECS (timeout, 0, 0);
if ((select (max (max_process_desc, max_keyboard_desc) + 1,
&Atemp, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
&timeout)
<= 0))
{
clear_waiting_for_input ();
status_notify ();
}
}
if (wait_proc != 0 && !NILP (wait_proc->raw_status_low))
update_status (wait_proc);
if (wait_proc != 0
&& ! EQ (wait_proc->status, Qrun))
{
int nread, total_nread = 0;
clear_waiting_for_input ();
XSETPROCESS (proc, wait_proc);
while (XINT (wait_proc->infd) >= 0)
{
nread = read_process_output (proc, XINT (wait_proc->infd));
if (nread == 0)
break;
if (0 < nread)
total_nread += nread;
#ifdef EIO
else if (nread == -1 && EIO == errno)
break;
#endif
#ifdef EAGAIN
else if (nread == -1 && EAGAIN == errno)
break;
#endif
#ifdef EWOULDBLOCK
else if (nread == -1 && EWOULDBLOCK == errno)
break;
#endif
}
if (total_nread > 0 && do_display)
redisplay_preserve_echo_area (10);
break;
}
if (wait_for_cell)
Available = non_process_wait_mask;
else if (! XINT (read_kbd))
Available = non_keyboard_wait_mask;
else
Available = input_wait_mask;
if (frame_garbaged && do_display)
{
clear_waiting_for_input ();
redisplay_preserve_echo_area (11);
if (XINT (read_kbd) < 0)
set_waiting_for_input (&timeout);
}
if (XINT (read_kbd) && detect_input_pending ())
{
nfds = 0;
FD_ZERO (&Available);
}
else
nfds = select (max (max_process_desc, max_keyboard_desc) + 1,
&Available, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
&timeout);
xerrno = errno;
clear_waiting_for_input ();
do_pending_window_change (0);
if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
break;
if (nfds < 0)
{
if (xerrno == EINTR)
FD_ZERO (&Available);
#ifdef ultrix
else if (xerrno == ENOMEM)
FD_ZERO (&Available);
#endif
#ifdef ALLIANT
else if (xerrno == EFAULT)
FD_ZERO (&Available);
#endif
else if (xerrno == EBADF)
{
#ifdef AIX
FD_ZERO (&Available);
#else
abort ();
#endif
}
else
error ("select error: %s", emacs_strerror (xerrno));
}
#if defined(sun) && !defined(USG5_4)
else if (nfds > 0 && keyboard_bit_set (&Available)
&& interrupt_input)
kill (getpid (), SIGIO);
#endif
#if 0
if (XINT (read_kbd) && interrupt_input
&& keyboard_bit_set (&Available)
&& input_polling_used ())
kill (getpid (), SIGALRM);
#endif
if (XINT (read_kbd) != 0)
{
int old_timers_run = timers_run;
struct buffer *old_buffer = current_buffer;
int leave = 0;
if (detect_input_pending_run_timers (do_display))
{
swallow_events (do_display);
if (detect_input_pending_run_timers (do_display))
leave = 1;
}
if (timers_run != old_timers_run
&& waiting_for_user_input_p == -1
&& old_buffer != current_buffer)
record_asynch_buffer_change ();
if (leave)
break;
}
if (XINT (read_kbd) != 0
&& requeued_events_pending_p ())
break;
if (XINT (read_kbd) == 0 && detect_input_pending ())
{
swallow_events (do_display);
#if 0
if (detect_input_pending ())
break;
#endif
}
if (wait_for_cell && ! NILP (*wait_for_cell))
break;
#ifdef SIGIO
if (XINT (read_kbd) && interrupt_input
&& keyboard_bit_set (&Available))
kill (getpid (), SIGIO);
#endif
if (! wait_proc)
got_some_input |= nfds > 0;
if (XINT (read_kbd) || wait_for_cell)
do_pending_window_change (0);
for (channel = 0; channel <= max_process_desc; channel++)
{
if (FD_ISSET (channel, &Available)
&& FD_ISSET (channel, &non_keyboard_wait_mask))
{
int nread;
if (wait_channel == channel)
{
wait_channel = -1;
time_limit = -1;
got_some_input = 1;
}
proc = chan_process[channel];
if (NILP (proc))
continue;
nread = read_process_output (proc, channel);
if (nread > 0)
{
FD_ZERO (&Available);
if (do_display)
redisplay_preserve_echo_area (12);
}
#ifdef EWOULDBLOCK
else if (nread == -1 && errno == EWOULDBLOCK)
;
#endif
#ifdef O_NONBLOCK
else if (nread == -1 && errno == EAGAIN)
;
#else
#ifdef O_NDELAY
else if (nread == -1 && errno == EAGAIN)
;
else if (nread == 0 && !NETCONN_P (proc))
;
#endif
#endif
#ifdef HAVE_PTYS
else if (nread == -1 && errno == EIO)
kill (getpid (), SIGCHLD);
#endif
#ifdef SIGCHLD
else if (nread == 0 && !NETCONN_P (proc))
;
#endif
else
{
XSETINT (XPROCESS (proc)->tick, ++process_tick);
deactivate_process (proc);
if (!NILP (XPROCESS (proc)->raw_status_low))
update_status (XPROCESS (proc));
if (EQ (XPROCESS (proc)->status, Qrun))
XPROCESS (proc)->status
= Fcons (Qexit, Fcons (make_number (256), Qnil));
}
}
}
}
waiting_for_user_input_p = 0;
if (XINT (read_kbd) >= 0)
{
clear_input_pending ();
QUIT;
}
#ifdef hpux
start_polling ();
#endif
return got_some_input;
}
static Lisp_Object
read_process_output_call (fun_and_args)
Lisp_Object fun_and_args;
{
return apply1 (XCAR (fun_and_args), XCDR (fun_and_args));
}
static Lisp_Object
read_process_output_error_handler (error)
Lisp_Object error;
{
cmd_error_internal (error, "error in process filter: ");
Vinhibit_quit = Qt;
update_echo_area ();
Fsleep_for (make_number (2), Qnil);
return Qt;
}
int
read_process_output (proc, channel)
Lisp_Object proc;
register int channel;
{
register int nchars, nbytes;
char *chars;
register Lisp_Object outstream;
register struct buffer *old = current_buffer;
register struct Lisp_Process *p = XPROCESS (proc);
register int opoint;
struct coding_system *coding = proc_decode_coding_system[channel];
int carryover = XINT (p->decoding_carryover);
#ifdef VMS
VMS_PROC_STUFF *vs, *get_vms_process_pointer();
vs = get_vms_process_pointer (p->pid);
if (vs)
{
if (!vs->iosb[0])
return (0);
if (!(vs->iosb[0] & 1))
return -1;
}
else
error ("Could not get VMS process pointer");
chars = vs->inputBuffer;
nbytes = clean_vms_buffer (chars, vs->iosb[1]);
if (nbytes <= 0)
{
start_vms_process_read (vs);
return 1;
}
if (carryover > 0)
{
chars = (char *) alloca (nbytes + carryover);
bcopy (XSTRING (p->decoding_buf)->data, buf, carryover);
bcopy (vs->inputBuffer, chars + carryover, nbytes);
}
#else
chars = (char *) alloca (carryover + 1024);
if (carryover)
bcopy (XSTRING (p->decoding_buf)->data, chars, carryover);
if (proc_buffered_char[channel] < 0)
nbytes = emacs_read (channel, chars + carryover, 1024 - carryover);
else
{
chars[carryover] = proc_buffered_char[channel];
proc_buffered_char[channel] = -1;
nbytes = emacs_read (channel, chars + carryover + 1, 1023 - carryover);
if (nbytes < 0)
nbytes = 1;
else
nbytes = nbytes + 1;
}
#endif
XSETINT (p->decoding_carryover, 0);
if (nbytes <= 0)
{
if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK)
return nbytes;
coding->mode |= CODING_MODE_LAST_BLOCK;
}
nbytes += carryover;
outstream = p->filter;
if (!NILP (outstream))
{
int count = specpdl_ptr - specpdl;
Lisp_Object odeactivate;
Lisp_Object obuffer, okeymap;
Lisp_Object text;
int outer_running_asynch_code = running_asynch_code;
int waiting = waiting_for_user_input_p;
odeactivate = Vdeactivate_mark;
XSETBUFFER (obuffer, current_buffer);
okeymap = current_buffer->keymap;
specbind (Qinhibit_quit, Qt);
specbind (Qlast_nonmenu_event, Qt);
if (outer_running_asynch_code)
{
Lisp_Object tem;
tem = Fmatch_data (Qnil, Qnil);
restore_match_data ();
record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
Fset_match_data (tem);
}
running_asynch_code = 1;
text = decode_coding_string (make_unibyte_string (chars, nbytes),
coding, 0);
if (NILP (buffer_defaults.enable_multibyte_characters))
text = string_make_unibyte (text);
Vlast_coding_system_used = coding->symbol;
if (!EQ (p->decode_coding_system, coding->symbol))
{
p->decode_coding_system = coding->symbol;
if (NILP (p->encode_coding_system)
&& proc_encode_coding_system[XINT (p->outfd)])
{
p->encode_coding_system = coding->symbol;
setup_coding_system (coding->symbol,
proc_encode_coding_system[XINT (p->outfd)]);
}
}
carryover = nbytes - coding->consumed;
bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
carryover);
XSETINT (p->decoding_carryover, carryover);
nbytes = STRING_BYTES (XSTRING (text));
nchars = XSTRING (text)->size;
if (nbytes > 0)
internal_condition_case_1 (read_process_output_call,
Fcons (outstream,
Fcons (proc, Fcons (text, Qnil))),
!NILP (Vdebug_on_error) ? Qnil : Qerror,
read_process_output_error_handler);
restore_match_data ();
running_asynch_code = outer_running_asynch_code;
Vdeactivate_mark = odeactivate;
waiting_for_user_input_p = waiting;
#if 0
if (! EQ (Fcurrent_buffer (), obuffer)
|| ! EQ (current_buffer->keymap, okeymap))
#endif
if (waiting_for_user_input_p == -1)
record_asynch_buffer_change ();
#ifdef VMS
start_vms_process_read (vs);
#endif
unbind_to (count, Qnil);
return nchars;
}
if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
{
Lisp_Object old_read_only;
int old_begv, old_zv;
int old_begv_byte, old_zv_byte;
Lisp_Object odeactivate;
int before, before_byte;
int opoint_byte;
Lisp_Object text;
struct buffer *b;
odeactivate = Vdeactivate_mark;
Fset_buffer (p->buffer);
opoint = PT;
opoint_byte = PT_BYTE;
old_read_only = current_buffer->read_only;
old_begv = BEGV;
old_zv = ZV;
old_begv_byte = BEGV_BYTE;
old_zv_byte = ZV_BYTE;
current_buffer->read_only = Qnil;
if (XMARKER (p->mark)->buffer)
SET_PT_BOTH (clip_to_bounds (BEGV, marker_position (p->mark), ZV),
clip_to_bounds (BEGV_BYTE, marker_byte_position (p->mark),
ZV_BYTE));
else
SET_PT_BOTH (ZV, ZV_BYTE);
before = PT;
before_byte = PT_BYTE;
if (! (BEGV <= PT && PT <= ZV))
Fwiden ();
text = decode_coding_string (make_unibyte_string (chars, nbytes),
coding, 0);
Vlast_coding_system_used = coding->symbol;
if (!EQ (p->decode_coding_system, coding->symbol))
{
p->decode_coding_system = coding->symbol;
if (NILP (p->encode_coding_system)
&& proc_encode_coding_system[XINT (p->outfd)])
{
p->encode_coding_system = coding->symbol;
setup_coding_system (coding->symbol,
proc_encode_coding_system[XINT (p->outfd)]);
}
}
carryover = nbytes - coding->consumed;
bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
carryover);
XSETINT (p->decoding_carryover, carryover);
if (NILP (current_buffer->enable_multibyte_characters)
!= ! STRING_MULTIBYTE (text))
text = (STRING_MULTIBYTE (text)
? Fstring_as_unibyte (text)
: Fstring_as_multibyte (text));
nbytes = STRING_BYTES (XSTRING (text));
nchars = XSTRING (text)->size;
insert_from_string_before_markers (text, 0, 0, nchars, nbytes, 0);
if (BUFFERP (p->buffer)
&& (b = XBUFFER (p->buffer), b != current_buffer))
set_marker_both (p->mark, p->buffer, BUF_PT (b), BUF_PT_BYTE (b));
else
set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
update_mode_lines++;
if (opoint >= before)
{
opoint += PT - before;
opoint_byte += PT_BYTE - before_byte;
}
if (old_begv > before)
{
old_begv += PT - before;
old_begv_byte += PT_BYTE - before_byte;
}
if (old_zv >= before)
{
old_zv += PT - before;
old_zv_byte += PT_BYTE - before_byte;
}
if (old_begv != BEGV || old_zv != ZV)
Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
Vdeactivate_mark = odeactivate;
current_buffer->read_only = old_read_only;
SET_PT_BOTH (opoint, opoint_byte);
set_buffer_internal (old);
}
#ifdef VMS
start_vms_process_read (vs);
#endif
return nbytes;
}
DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
0, 0, 0,
"Returns non-nil if emacs is waiting for input from the user.\n\
This is intended for use by asynchronous process output filters and sentinels.")
()
{
return (waiting_for_user_input_p ? Qt : Qnil);
}
jmp_buf send_process_frame;
Lisp_Object process_sent_to;
SIGTYPE
send_process_trap ()
{
#ifdef BSD4_1
sigrelse (SIGPIPE);
sigrelse (SIGALRM);
#endif
longjmp (send_process_frame, 1);
}
void
send_process (proc, buf, len, object)
volatile Lisp_Object proc;
unsigned char *volatile buf;
volatile int len;
volatile Lisp_Object object;
{
int rv;
struct coding_system *coding;
struct gcpro gcpro1;
GCPRO1 (object);
#ifdef VMS
struct Lisp_Process *p = XPROCESS (proc);
VMS_PROC_STUFF *vs, *get_vms_process_pointer();
#endif
if (! NILP (XPROCESS (proc)->raw_status_low))
update_status (XPROCESS (proc));
if (! EQ (XPROCESS (proc)->status, Qrun))
error ("Process %s not running",
XSTRING (XPROCESS (proc)->name)->data);
if (XINT (XPROCESS (proc)->outfd) < 0)
error ("Output file descriptor of %s is closed",
XSTRING (XPROCESS (proc)->name)->data);
coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
Vlast_coding_system_used = coding->symbol;
if ((STRINGP (object) && STRING_MULTIBYTE (object))
|| (BUFFERP (object)
&& !NILP (XBUFFER (object)->enable_multibyte_characters))
|| EQ (object, Qt))
{
if (!EQ (coding->symbol, XPROCESS (proc)->encode_coding_system))
setup_coding_system (XPROCESS (proc)->encode_coding_system,
coding);
coding->src_multibyte = 1;
}
else
{
if (coding->type != coding_type_raw_text)
{
if (CODING_REQUIRE_FLUSHING (coding))
{
coding->mode |= CODING_MODE_LAST_BLOCK;
send_process (proc, "", 0, Qt);
}
coding->src_multibyte = 0;
setup_raw_text_coding_system (coding);
}
}
coding->dst_multibyte = 0;
if (CODING_REQUIRE_ENCODING (coding))
{
int require = encoding_buffer_size (coding, len);
int from_byte = -1, from = -1, to = -1;
unsigned char *temp_buf = NULL;
if (BUFFERP (object))
{
from_byte = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
from = buf_bytepos_to_charpos (XBUFFER (object), from_byte);
to = buf_bytepos_to_charpos (XBUFFER (object), from_byte + len);
}
else if (STRINGP (object))
{
from_byte = buf - XSTRING (object)->data;
from = string_byte_to_char (object, from_byte);
to = string_byte_to_char (object, from_byte + len);
}
if (coding->composing != COMPOSITION_DISABLED)
{
if (from_byte >= 0)
coding_save_composition (coding, from, to, object);
else
coding->composing = COMPOSITION_DISABLED;
}
if (STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf)) < require)
XPROCESS (proc)->encoding_buf = make_uninit_string (require);
if (from_byte >= 0)
buf = (BUFFERP (object)
? BUF_BYTE_ADDRESS (XBUFFER (object), from_byte)
: XSTRING (object)->data + from_byte);
object = XPROCESS (proc)->encoding_buf;
encode_coding (coding, (char *) buf, XSTRING (object)->data,
len, STRING_BYTES (XSTRING (object)));
len = coding->produced;
buf = XSTRING (object)->data;
if (temp_buf)
xfree (temp_buf);
}
#ifdef VMS
vs = get_vms_process_pointer (p->pid);
if (vs == 0)
error ("Could not find this process: %x", p->pid);
else if (write_to_vms_process (vs, buf, len))
;
#else
if (pty_max_bytes == 0)
{
#if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
pty_max_bytes = fpathconf (XFASTINT (XPROCESS (proc)->outfd),
_PC_MAX_CANON);
if (pty_max_bytes < 0)
pty_max_bytes = 250;
#else
pty_max_bytes = 250;
#endif
pty_max_bytes--;
}
if (!setjmp (send_process_frame))
{
process_sent_to = proc;
while (len > 0)
{
int this = len;
SIGTYPE (*old_sigpipe)();
if (!NILP (XPROCESS (proc)->pty_flag))
{
int linepos = 0;
unsigned char *ptr = (unsigned char *) buf;
unsigned char *end = (unsigned char *) buf + len;
while (ptr != end && linepos < pty_max_bytes)
{
if (*ptr == '\n')
linepos = 0;
else
linepos++;
ptr++;
}
this = ptr - buf;
}
while (this > 0)
{
old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
rv = emacs_write (XINT (XPROCESS (proc)->outfd),
(char *) buf, this);
signal (SIGPIPE, old_sigpipe);
if (rv < 0)
{
if (0
#ifdef EWOULDBLOCK
|| errno == EWOULDBLOCK
#endif
#ifdef EAGAIN
|| errno == EAGAIN
#endif
)
{
Lisp_Object zero;
int offset = 0;
#ifdef BROKEN_PTY_READ_AFTER_EAGAIN
if (errno == EAGAIN)
{
int flags = FWRITE;
ioctl (XINT (XPROCESS (proc)->outfd), TIOCFLUSH,
&flags);
}
#endif
if (BUFFERP (object))
offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
else if (STRINGP (object))
offset = buf - XSTRING (object)->data;
XSETFASTINT (zero, 0);
#ifdef EMACS_HAS_USECS
wait_reading_process_input (0, 20000, zero, 0);
#else
wait_reading_process_input (1, 0, zero, 0);
#endif
if (BUFFERP (object))
buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
else if (STRINGP (object))
buf = offset + XSTRING (object)->data;
rv = 0;
}
else
report_file_error ("writing to process", Fcons (proc, Qnil));
}
buf += rv;
len -= rv;
this -= rv;
}
if (len > 0)
Fprocess_send_eof (proc);
}
}
#endif
else
{
#ifndef VMS
proc = process_sent_to;
#endif
XPROCESS (proc)->raw_status_low = Qnil;
XPROCESS (proc)->raw_status_high = Qnil;
XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
XSETINT (XPROCESS (proc)->tick, ++process_tick);
deactivate_process (proc);
#ifdef VMS
error ("Error writing to process %s; closed it",
XSTRING (XPROCESS (proc)->name)->data);
#else
error ("SIGPIPE raised on process %s; closed it",
XSTRING (XPROCESS (proc)->name)->data);
#endif
}
UNGCPRO;
}
DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
3, 3, 0,
"Send current contents of region as input to PROCESS.\n\
PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
nil, indicating the current buffer's process.\n\
Called from program, takes three arguments, PROCESS, START and END.\n\
If the region is more than 500 characters long,\n\
it is sent in several bunches. This may happen even for shorter regions.\n\
Output from processes can arrive in between bunches.")
(process, start, end)
Lisp_Object process, start, end;
{
Lisp_Object proc;
int start1, end1;
proc = get_process (process);
validate_region (&start, &end);
if (XINT (start) < GPT && XINT (end) > GPT)
move_gap (XINT (start));
start1 = CHAR_TO_BYTE (XINT (start));
end1 = CHAR_TO_BYTE (XINT (end));
send_process (proc, BYTE_POS_ADDR (start1), end1 - start1,
Fcurrent_buffer ());
return Qnil;
}
DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
2, 2, 0,
"Send PROCESS the contents of STRING as input.\n\
PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
nil, indicating the current buffer's process.\n\
If STRING is more than 500 characters long,\n\
it is sent in several bunches. This may happen even for shorter strings.\n\
Output from processes can arrive in between bunches.")
(process, string)
Lisp_Object process, string;
{
Lisp_Object proc;
CHECK_STRING (string, 1);
proc = get_process (process);
send_process (proc, XSTRING (string)->data,
STRING_BYTES (XSTRING (string)), string);
return Qnil;
}
DEFUN ("process-running-child-p", Fprocess_running_child_p,
Sprocess_running_child_p, 0, 1, 0,
"Return t if PROCESS has given the terminal to a child.\n\
If the operating system does not make it possible to find out,\n\
return t unconditionally.")
(process)
Lisp_Object process;
{
int gid = 0;
Lisp_Object proc;
struct Lisp_Process *p;
proc = get_process (process);
p = XPROCESS (proc);
if (!EQ (p->childp, Qt))
error ("Process %s is not a subprocess",
XSTRING (p->name)->data);
if (XINT (p->infd) < 0)
error ("Process %s is not active",
XSTRING (p->name)->data);
#ifdef TIOCGPGRP
if (!NILP (p->subtty))
ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
else
ioctl (XINT (p->infd), TIOCGPGRP, &gid);
#endif
if (gid == XFASTINT (p->pid))
return Qnil;
return Qt;
}
static void
process_send_signal (process, signo, current_group, nomsg)
Lisp_Object process;
int signo;
Lisp_Object current_group;
int nomsg;
{
Lisp_Object proc;
register struct Lisp_Process *p;
int gid;
int no_pgrp = 0;
proc = get_process (process);
p = XPROCESS (proc);
if (!EQ (p->childp, Qt))
error ("Process %s is not a subprocess",
XSTRING (p->name)->data);
if (XINT (p->infd) < 0)
error ("Process %s is not active",
XSTRING (p->name)->data);
if (NILP (p->pty_flag))
current_group = Qnil;
if (!NILP (current_group))
{
#ifdef SIGNALS_VIA_CHARACTERS
#ifdef HAVE_TERMIOS
struct termios t;
switch (signo)
{
case SIGINT:
tcgetattr (XINT (p->infd), &t);
send_process (proc, &t.c_cc[VINTR], 1, Qnil);
return;
case SIGQUIT:
tcgetattr (XINT (p->infd), &t);
send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
return;
case SIGTSTP:
tcgetattr (XINT (p->infd), &t);
#if defined (VSWTCH) && !defined (PREFER_VSUSP)
send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
#else
send_process (proc, &t.c_cc[VSUSP], 1, Qnil);
#endif
return;
}
#else
#if defined (TIOCGLTC) && defined (TIOCGETC)
struct tchars c;
struct ltchars lc;
switch (signo)
{
case SIGINT:
ioctl (XINT (p->infd), TIOCGETC, &c);
send_process (proc, &c.t_intrc, 1, Qnil);
return;
case SIGQUIT:
ioctl (XINT (p->infd), TIOCGETC, &c);
send_process (proc, &c.t_quitc, 1, Qnil);
return;
#ifdef SIGTSTP
case SIGTSTP:
ioctl (XINT (p->infd), TIOCGLTC, &lc);
send_process (proc, &lc.t_suspc, 1, Qnil);
return;
#endif
}
#else
#ifdef TCGETA
struct termio t;
switch (signo)
{
case SIGINT:
ioctl (XINT (p->infd), TCGETA, &t);
send_process (proc, &t.c_cc[VINTR], 1, Qnil);
return;
case SIGQUIT:
ioctl (XINT (p->infd), TCGETA, &t);
send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
return;
#ifdef SIGTSTP
case SIGTSTP:
ioctl (XINT (p->infd), TCGETA, &t);
send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
return;
#endif
}
#else
Your configuration files are messed up.
#endif
#endif
#endif
#endif
#ifdef TIOCGPGRP
{
int err;
if (!NILP (p->subtty))
err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
else
err = ioctl (XINT (p->infd), TIOCGPGRP, &gid);
#ifdef pfa
if (err == -1)
gid = - XFASTINT (p->pid);
#endif
}
if (gid == -1)
no_pgrp = 1;
else
gid = - gid;
#else
gid = - XFASTINT (p->pid);
#endif
if (EQ (current_group, Qlambda) && gid == - XFASTINT (p->pid))
return;
}
else
gid = - XFASTINT (p->pid);
switch (signo)
{
#ifdef SIGCONT
case SIGCONT:
p->raw_status_low = Qnil;
p->raw_status_high = Qnil;
p->status = Qrun;
XSETINT (p->tick, ++process_tick);
if (!nomsg)
status_notify ();
break;
#endif
case SIGINT:
#ifdef VMS
send_process (proc, "\003", 1, Qnil);
goto whoosh;
#endif
case SIGQUIT:
#ifdef VMS
send_process (proc, "\031", 1, Qnil);
goto whoosh;
#endif
case SIGKILL:
#ifdef VMS
sys$forcex (&(XFASTINT (p->pid)), 0, 1);
whoosh:
#endif
flush_pending_output (XINT (p->infd));
break;
}
if (no_pgrp)
{
kill (XFASTINT (p->pid), signo);
return;
}
#ifdef TIOCSIGSEND
if (!NILP (current_group))
ioctl (XINT (p->infd), TIOCSIGSEND, signo);
else
{
gid = - XFASTINT (p->pid);
kill (gid, signo);
}
#else
EMACS_KILLPG (-gid, signo);
#endif
}
DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
"Interrupt process PROCESS.\n\
PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
nil or no arg means current buffer's process.\n\
Second arg CURRENT-GROUP non-nil means send signal to\n\
the current process-group of the process's controlling terminal\n\
rather than to the process's own process group.\n\
If the process is a shell, this means interrupt current subjob\n\
rather than the shell.\n\
\n\
If CURRENT-GROUP is `lambda', and if the shell owns the terminal,\n\
don't send the signal.")
(process, current_group)
Lisp_Object process, current_group;
{
process_send_signal (process, SIGINT, current_group, 0);
return process;
}
DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
"Kill process PROCESS. May be process or name of one.\n\
See function `interrupt-process' for more details on usage.")
(process, current_group)
Lisp_Object process, current_group;
{
process_send_signal (process, SIGKILL, current_group, 0);
return process;
}
DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
"Send QUIT signal to process PROCESS. May be process or name of one.\n\
See function `interrupt-process' for more details on usage.")
(process, current_group)
Lisp_Object process, current_group;
{
process_send_signal (process, SIGQUIT, current_group, 0);
return process;
}
DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
"Stop process PROCESS. May be process or name of one.\n\
See function `interrupt-process' for more details on usage.")
(process, current_group)
Lisp_Object process, current_group;
{
#ifndef SIGTSTP
error ("no SIGTSTP support");
#else
process_send_signal (process, SIGTSTP, current_group, 0);
#endif
return process;
}
DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
"Continue process PROCESS. May be process or name of one.\n\
See function `interrupt-process' for more details on usage.")
(process, current_group)
Lisp_Object process, current_group;
{
#ifdef SIGCONT
process_send_signal (process, SIGCONT, current_group, 0);
#else
error ("no SIGCONT support");
#endif
return process;
}
DEFUN ("signal-process", Fsignal_process, Ssignal_process,
2, 2, "nProcess number: \nnSignal code: ",
"Send the process with process id PID the signal with code SIGCODE.\n\
PID must be an integer. The process need not be a child of this Emacs.\n\
SIGCODE may be an integer, or a symbol whose name is a signal name.")
(pid, sigcode)
Lisp_Object pid, sigcode;
{
CHECK_NUMBER (pid, 0);
#define handle_signal(NAME, VALUE) \
else if (!strcmp (name, NAME)) \
XSETINT (sigcode, VALUE)
if (INTEGERP (sigcode))
;
else
{
unsigned char *name;
CHECK_SYMBOL (sigcode, 1);
name = XSYMBOL (sigcode)->name->data;
if (0)
;
#ifdef SIGHUP
handle_signal ("SIGHUP", SIGHUP);
#endif
#ifdef SIGINT
handle_signal ("SIGINT", SIGINT);
#endif
#ifdef SIGQUIT
handle_signal ("SIGQUIT", SIGQUIT);
#endif
#ifdef SIGILL
handle_signal ("SIGILL", SIGILL);
#endif
#ifdef SIGABRT
handle_signal ("SIGABRT", SIGABRT);
#endif
#ifdef SIGEMT
handle_signal ("SIGEMT", SIGEMT);
#endif
#ifdef SIGKILL
handle_signal ("SIGKILL", SIGKILL);
#endif
#ifdef SIGFPE
handle_signal ("SIGFPE", SIGFPE);
#endif
#ifdef SIGBUS
handle_signal ("SIGBUS", SIGBUS);
#endif
#ifdef SIGSEGV
handle_signal ("SIGSEGV", SIGSEGV);
#endif
#ifdef SIGSYS
handle_signal ("SIGSYS", SIGSYS);
#endif
#ifdef SIGPIPE
handle_signal ("SIGPIPE", SIGPIPE);
#endif
#ifdef SIGALRM
handle_signal ("SIGALRM", SIGALRM);
#endif
#ifdef SIGTERM
handle_signal ("SIGTERM", SIGTERM);
#endif
#ifdef SIGURG
handle_signal ("SIGURG", SIGURG);
#endif
#ifdef SIGSTOP
handle_signal ("SIGSTOP", SIGSTOP);
#endif
#ifdef SIGTSTP
handle_signal ("SIGTSTP", SIGTSTP);
#endif
#ifdef SIGCONT
handle_signal ("SIGCONT", SIGCONT);
#endif
#ifdef SIGCHLD
handle_signal ("SIGCHLD", SIGCHLD);
#endif
#ifdef SIGTTIN
handle_signal ("SIGTTIN", SIGTTIN);
#endif
#ifdef SIGTTOU
handle_signal ("SIGTTOU", SIGTTOU);
#endif
#ifdef SIGIO
handle_signal ("SIGIO", SIGIO);
#endif
#ifdef SIGXCPU
handle_signal ("SIGXCPU", SIGXCPU);
#endif
#ifdef SIGXFSZ
handle_signal ("SIGXFSZ", SIGXFSZ);
#endif
#ifdef SIGVTALRM
handle_signal ("SIGVTALRM", SIGVTALRM);
#endif
#ifdef SIGPROF
handle_signal ("SIGPROF", SIGPROF);
#endif
#ifdef SIGWINCH
handle_signal ("SIGWINCH", SIGWINCH);
#endif
#ifdef SIGINFO
handle_signal ("SIGINFO", SIGINFO);
#endif
#ifdef SIGUSR1
handle_signal ("SIGUSR1", SIGUSR1);
#endif
#ifdef SIGUSR2
handle_signal ("SIGUSR2", SIGUSR2);
#endif
else
error ("Undefined signal name %s", name);
}
#undef handle_signal
return make_number (kill (XINT (pid), XINT (sigcode)));
}
DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
"Make PROCESS see end-of-file in its input.\n\
EOF comes after any text already sent to it.\n\
PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
nil, indicating the current buffer's process.\n\
If PROCESS is a network connection, or is a process communicating\n\
through a pipe (as opposed to a pty), then you cannot send any more\n\
text to PROCESS after you call this function.")
(process)
Lisp_Object process;
{
Lisp_Object proc;
struct coding_system *coding;
proc = get_process (process);
coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
if (! NILP (XPROCESS (proc)->raw_status_low))
update_status (XPROCESS (proc));
if (! EQ (XPROCESS (proc)->status, Qrun))
error ("Process %s not running", XSTRING (XPROCESS (proc)->name)->data);
if (CODING_REQUIRE_FLUSHING (coding))
{
coding->mode |= CODING_MODE_LAST_BLOCK;
send_process (proc, "", 0, Qnil);
}
#ifdef VMS
send_process (proc, "\032", 1, Qnil);
#else
if (!NILP (XPROCESS (proc)->pty_flag))
send_process (proc, "\004", 1, Qnil);
else
{
int old_outfd, new_outfd;
#ifdef HAVE_SHUTDOWN
if (NILP (XPROCESS (proc)->pid)
|| XINT (XPROCESS (proc)->outfd) == XINT (XPROCESS (proc)->infd))
shutdown (XINT (XPROCESS (proc)->outfd), 1);
if (XINT (XPROCESS (proc)->outfd) != XINT (XPROCESS (proc)->infd))
emacs_close (XINT (XPROCESS (proc)->outfd));
#else
emacs_close (XINT (XPROCESS (proc)->outfd));
#endif
new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
old_outfd = XINT (XPROCESS (proc)->outfd);
if (!proc_encode_coding_system[new_outfd])
proc_encode_coding_system[new_outfd]
= (struct coding_system *) xmalloc (sizeof (struct coding_system));
bcopy (proc_encode_coding_system[old_outfd],
proc_encode_coding_system[new_outfd],
sizeof (struct coding_system));
bzero (proc_encode_coding_system[old_outfd],
sizeof (struct coding_system));
XSETINT (XPROCESS (proc)->outfd, new_outfd);
}
#endif
return process;
}
void
kill_buffer_processes (buffer)
Lisp_Object buffer;
{
Lisp_Object tail, proc;
for (tail = Vprocess_alist; GC_CONSP (tail); tail = XCDR (tail))
{
proc = XCDR (XCAR (tail));
if (GC_PROCESSP (proc)
&& (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
{
if (NETCONN_P (proc))
Fdelete_process (proc);
else if (XINT (XPROCESS (proc)->infd) >= 0)
process_send_signal (proc, SIGHUP, Qnil, 1);
}
}
}
SIGTYPE
sigchld_handler (signo)
int signo;
{
int old_errno = errno;
Lisp_Object proc;
register struct Lisp_Process *p;
extern EMACS_TIME *input_available_clear_time;
#ifdef BSD4_1
extern int sigheld;
sigheld |= sigbit (SIGCHLD);
#endif
while (1)
{
register int pid;
WAITTYPE w;
Lisp_Object tail;
#ifdef WNOHANG
#ifndef WUNTRACED
#define WUNTRACED 0
#endif
do
{
errno = 0;
pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
}
while (pid < 0 && errno == EINTR);
if (pid <= 0)
{
#if defined (USG) && !defined (POSIX_SIGNALS)
signal (signo, sigchld_handler);
#endif
#ifdef BSD4_1
sigheld &= ~sigbit (SIGCHLD);
sigrelse (SIGCHLD);
#endif
errno = old_errno;
return;
}
#else
pid = wait (&w);
#endif
p = 0;
for (tail = Vprocess_alist; GC_CONSP (tail); tail = XCDR (tail))
{
proc = XCDR (XCAR (tail));
p = XPROCESS (proc);
if (GC_EQ (p->childp, Qt) && XINT (p->pid) == pid)
break;
p = 0;
}
if (p == 0)
for (tail = Vprocess_alist; GC_CONSP (tail); tail = XCDR (tail))
{
proc = XCDR (XCAR (tail));
p = XPROCESS (proc);
if (GC_INTEGERP (p->pid) && XINT (p->pid) == -1)
break;
p = 0;
}
if (p != 0)
{
union { int i; WAITTYPE wt; } u;
int clear_desc_flag = 0;
XSETINT (p->tick, ++process_tick);
u.wt = w;
XSETINT (p->raw_status_low, u.i & 0xffff);
XSETINT (p->raw_status_high, u.i >> 16);
if ((WIFSIGNALED (w) || WIFEXITED (w))
&& XINT (p->infd) >= 0)
clear_desc_flag = 1;
if (clear_desc_flag)
{
FD_CLR (XINT (p->infd), &input_wait_mask);
FD_CLR (XINT (p->infd), &non_keyboard_wait_mask);
}
if (input_available_clear_time)
EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
}
else
{
synch_process_alive = 0;
if (WIFEXITED (w))
synch_process_retcode = WRETCODE (w);
else if (WIFSIGNALED (w))
{
int code = WTERMSIG (w);
char *signame;
synchronize_system_messages_locale ();
signame = strsignal (code);
if (signame == 0)
signame = "unknown";
synch_process_death = signame;
}
if (input_available_clear_time)
EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
}
#if (defined WINDOWSNT \
|| (defined USG && !defined LINUX \
&& !(defined HPUX && defined WNOHANG)))
#if defined (USG) && ! defined (POSIX_SIGNALS)
signal (signo, sigchld_handler);
#endif
errno = old_errno;
return;
#endif
}
}
static Lisp_Object
exec_sentinel_unwind (data)
Lisp_Object data;
{
XPROCESS (XCAR (data))->sentinel = XCDR (data);
return Qnil;
}
static Lisp_Object
exec_sentinel_error_handler (error)
Lisp_Object error;
{
cmd_error_internal (error, "error in process sentinel: ");
Vinhibit_quit = Qt;
update_echo_area ();
Fsleep_for (make_number (2), Qnil);
return Qt;
}
static void
exec_sentinel (proc, reason)
Lisp_Object proc, reason;
{
Lisp_Object sentinel, obuffer, odeactivate, okeymap;
register struct Lisp_Process *p = XPROCESS (proc);
int count = specpdl_ptr - specpdl;
int outer_running_asynch_code = running_asynch_code;
int waiting = waiting_for_user_input_p;
odeactivate = Vdeactivate_mark;
XSETBUFFER (obuffer, current_buffer);
okeymap = current_buffer->keymap;
sentinel = p->sentinel;
if (NILP (sentinel))
return;
p->sentinel = Qnil;
record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
specbind (Qinhibit_quit, Qt);
specbind (Qlast_nonmenu_event, Qt);
if (outer_running_asynch_code)
{
Lisp_Object tem;
tem = Fmatch_data (Qnil, Qnil);
restore_match_data ();
record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
Fset_match_data (tem);
}
running_asynch_code = 1;
internal_condition_case_1 (read_process_output_call,
Fcons (sentinel,
Fcons (proc, Fcons (reason, Qnil))),
!NILP (Vdebug_on_error) ? Qnil : Qerror,
exec_sentinel_error_handler);
restore_match_data ();
running_asynch_code = outer_running_asynch_code;
Vdeactivate_mark = odeactivate;
waiting_for_user_input_p = waiting;
#if 0
if (! EQ (Fcurrent_buffer (), obuffer)
|| ! EQ (current_buffer->keymap, okeymap))
#endif
if (waiting_for_user_input_p == -1)
record_asynch_buffer_change ();
unbind_to (count, Qnil);
}
void
status_notify ()
{
register Lisp_Object proc, buffer;
Lisp_Object tail, msg;
struct gcpro gcpro1, gcpro2;
tail = Qnil;
msg = Qnil;
GCPRO2 (tail, msg);
update_tick = process_tick;
for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
{
Lisp_Object symbol;
register struct Lisp_Process *p;
proc = Fcdr (Fcar (tail));
p = XPROCESS (proc);
if (XINT (p->tick) != XINT (p->update_tick))
{
XSETINT (p->update_tick, XINT (p->tick));
while (! EQ (p->filter, Qt)
&& XINT (p->infd) >= 0
&& read_process_output (proc, XINT (p->infd)) > 0);
buffer = p->buffer;
if (!NILP (p->raw_status_low))
update_status (p);
msg = status_message (p->status);
symbol = p->status;
if (CONSP (p->status))
symbol = XCAR (p->status);
if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
|| EQ (symbol, Qclosed))
{
if (delete_exited_processes)
remove_process (proc);
else
deactivate_process (proc);
}
XSETINT (p->update_tick, XINT (p->tick));
if (!NILP (p->sentinel))
exec_sentinel (proc, msg);
else if (!EQ (symbol, Qrun) && !NILP (buffer))
{
Lisp_Object ro, tem;
struct buffer *old = current_buffer;
int opoint, opoint_byte;
int before, before_byte;
ro = XBUFFER (buffer)->read_only;
if (NILP (XBUFFER (buffer)->name))
continue;
Fset_buffer (buffer);
opoint = PT;
opoint_byte = PT_BYTE;
if (XMARKER (p->mark)->buffer)
Fgoto_char (p->mark);
else
SET_PT_BOTH (ZV, ZV_BYTE);
before = PT;
before_byte = PT_BYTE;
tem = current_buffer->read_only;
current_buffer->read_only = Qnil;
insert_string ("\nProcess ");
Finsert (1, &p->name);
insert_string (" ");
Finsert (1, &msg);
current_buffer->read_only = tem;
set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
if (opoint >= before)
SET_PT_BOTH (opoint + (PT - before),
opoint_byte + (PT_BYTE - before_byte));
else
SET_PT_BOTH (opoint, opoint_byte);
set_buffer_internal (old);
}
}
}
update_mode_lines++;
redisplay_preserve_echo_area (13);
UNGCPRO;
}
DEFUN ("set-process-coding-system", Fset_process_coding_system,
Sset_process_coding_system, 1, 3, 0,
"Set coding systems of PROCESS to DECODING and ENCODING.\n\
DECODING will be used to decode subprocess output and ENCODING to\n\
encode subprocess input.")
(proc, decoding, encoding)
register Lisp_Object proc, decoding, encoding;
{
register struct Lisp_Process *p;
CHECK_PROCESS (proc, 0);
p = XPROCESS (proc);
if (XINT (p->infd) < 0)
error ("Input file descriptor of %s closed", XSTRING (p->name)->data);
if (XINT (p->outfd) < 0)
error ("Output file descriptor of %s closed", XSTRING (p->name)->data);
p->decode_coding_system = Fcheck_coding_system (decoding);
p->encode_coding_system = Fcheck_coding_system (encoding);
setup_coding_system (decoding,
proc_decode_coding_system[XINT (p->infd)]);
setup_coding_system (encoding,
proc_encode_coding_system[XINT (p->outfd)]);
return Qnil;
}
DEFUN ("process-coding-system",
Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
"Return a cons of coding systems for decoding and encoding of PROCESS.")
(proc)
register Lisp_Object proc;
{
CHECK_PROCESS (proc, 0);
return Fcons (XPROCESS (proc)->decode_coding_system,
XPROCESS (proc)->encode_coding_system);
}
static int add_keyboard_wait_descriptor_called_flag;
void
add_keyboard_wait_descriptor (desc)
int desc;
{
if (! add_keyboard_wait_descriptor_called_flag)
FD_CLR (0, &input_wait_mask);
add_keyboard_wait_descriptor_called_flag = 1;
FD_SET (desc, &input_wait_mask);
FD_SET (desc, &non_process_wait_mask);
if (desc > max_keyboard_desc)
max_keyboard_desc = desc;
}
void
delete_keyboard_wait_descriptor (desc)
int desc;
{
int fd;
int lim = max_keyboard_desc;
FD_CLR (desc, &input_wait_mask);
FD_CLR (desc, &non_process_wait_mask);
if (desc == max_keyboard_desc)
for (fd = 0; fd < lim; fd++)
if (FD_ISSET (fd, &input_wait_mask)
&& !FD_ISSET (fd, &non_keyboard_wait_mask))
max_keyboard_desc = fd;
}
int
keyboard_bit_set (mask)
SELECT_TYPE *mask;
{
int fd;
for (fd = 0; fd <= max_keyboard_desc; fd++)
if (FD_ISSET (fd, mask) && FD_ISSET (fd, &input_wait_mask)
&& !FD_ISSET (fd, &non_keyboard_wait_mask))
return 1;
return 0;
}
void
init_emacs_process ()
{
register int i;
#ifdef SIGCHLD
#ifndef CANNOT_DUMP
if (! noninteractive || initialized)
#endif
signal (SIGCHLD, sigchld_handler);
#endif
FD_ZERO (&input_wait_mask);
FD_ZERO (&non_keyboard_wait_mask);
FD_ZERO (&non_process_wait_mask);
max_process_desc = 0;
FD_SET (0, &input_wait_mask);
Vprocess_alist = Qnil;
for (i = 0; i < MAXDESC; i++)
{
chan_process[i] = Qnil;
proc_buffered_char[i] = -1;
}
bzero (proc_decode_coding_system, sizeof proc_decode_coding_system);
bzero (proc_encode_coding_system, sizeof proc_encode_coding_system);
}
void
syms_of_process ()
{
Qprocessp = intern ("processp");
staticpro (&Qprocessp);
Qrun = intern ("run");
staticpro (&Qrun);
Qstop = intern ("stop");
staticpro (&Qstop);
Qsignal = intern ("signal");
staticpro (&Qsignal);
Qopen = intern ("open");
staticpro (&Qopen);
Qclosed = intern ("closed");
staticpro (&Qclosed);
Qlast_nonmenu_event = intern ("last-nonmenu-event");
staticpro (&Qlast_nonmenu_event);
staticpro (&Vprocess_alist);
DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
"*Non-nil means delete processes immediately when they exit.\n\
nil means don't delete them until `list-processes' is run.");
delete_exited_processes = 1;
DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
"Control type of device used to communicate with subprocesses.\n\
Values are nil to use a pipe, or t or `pty' to use a pty.\n\
The value has no effect if the system has no ptys or if all ptys are busy:\n\
then a pipe is used in any case.\n\
The value takes effect when `start-process' is called.");
Vprocess_connection_type = Qt;
defsubr (&Sprocessp);
defsubr (&Sget_process);
defsubr (&Sget_buffer_process);
defsubr (&Sdelete_process);
defsubr (&Sprocess_status);
defsubr (&Sprocess_exit_status);
defsubr (&Sprocess_id);
defsubr (&Sprocess_name);
defsubr (&Sprocess_tty_name);
defsubr (&Sprocess_command);
defsubr (&Sset_process_buffer);
defsubr (&Sprocess_buffer);
defsubr (&Sprocess_mark);
defsubr (&Sset_process_filter);
defsubr (&Sprocess_filter);
defsubr (&Sset_process_sentinel);
defsubr (&Sprocess_sentinel);
defsubr (&Sset_process_window_size);
defsubr (&Sset_process_inherit_coding_system_flag);
defsubr (&Sprocess_inherit_coding_system_flag);
defsubr (&Sprocess_kill_without_query);
defsubr (&Sprocess_contact);
defsubr (&Slist_processes);
defsubr (&Sprocess_list);
defsubr (&Sstart_process);
#ifdef HAVE_SOCKETS
defsubr (&Sopen_network_stream);
#endif
defsubr (&Saccept_process_output);
defsubr (&Sprocess_send_region);
defsubr (&Sprocess_send_string);
defsubr (&Sinterrupt_process);
defsubr (&Skill_process);
defsubr (&Squit_process);
defsubr (&Sstop_process);
defsubr (&Scontinue_process);
defsubr (&Sprocess_running_child_p);
defsubr (&Sprocess_send_eof);
defsubr (&Ssignal_process);
defsubr (&Swaiting_for_user_input_p);
defsubr (&Sset_process_coding_system);
defsubr (&Sprocess_coding_system);
}
#else
#include <sys/types.h>
#include <errno.h>
#include "lisp.h"
#include "systime.h"
#include "charset.h"
#include "coding.h"
#include "termopts.h"
#include "sysselect.h"
extern int frame_garbaged;
extern EMACS_TIME timer_check ();
extern int timers_run;
int
wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
int time_limit, microsecs;
Lisp_Object read_kbd;
int do_display;
{
register int nfds;
EMACS_TIME end_time, timeout;
SELECT_TYPE waitchannels;
int xerrno;
Lisp_Object *wait_for_cell = 0;
if (CONSP (read_kbd))
{
wait_for_cell = &XCAR (read_kbd);
XSETFASTINT (read_kbd, 0);
}
if (time_limit || microsecs)
{
EMACS_GET_TIME (end_time);
EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
EMACS_ADD_TIME (end_time, end_time, timeout);
}
turn_on_atimers (0);
while (1)
{
int timeout_reduced_for_timers = 0;
if (XINT (read_kbd) >= 0)
QUIT;
if (wait_for_cell && ! NILP (*wait_for_cell))
break;
if (time_limit == -1)
{
EMACS_SET_SECS_USECS (timeout, 0, 0);
}
else if (time_limit || microsecs)
{
EMACS_GET_TIME (timeout);
EMACS_SUB_TIME (timeout, end_time, timeout);
if (EMACS_TIME_NEG_P (timeout))
break;
}
else
{
EMACS_SET_SECS_USECS (timeout, 100000, 0);
}
if (! wait_for_cell)
{
EMACS_TIME timer_delay;
do
{
int old_timers_run = timers_run;
timer_delay = timer_check (1);
if (timers_run != old_timers_run && do_display)
redisplay_preserve_echo_area (14);
else
break;
}
while (!detect_input_pending ());
if (XINT (read_kbd) != 0
&& requeued_events_pending_p ())
break;
if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
{
EMACS_TIME difference;
EMACS_SUB_TIME (difference, timer_delay, timeout);
if (EMACS_TIME_NEG_P (difference))
{
timeout = timer_delay;
timeout_reduced_for_timers = 1;
}
}
}
if (XINT (read_kbd) < 0)
set_waiting_for_input (&timeout);
if (! XINT (read_kbd) && wait_for_cell == 0)
FD_ZERO (&waitchannels);
else
FD_SET (0, &waitchannels);
if (frame_garbaged && do_display)
{
clear_waiting_for_input ();
redisplay_preserve_echo_area (15);
if (XINT (read_kbd) < 0)
set_waiting_for_input (&timeout);
}
if (XINT (read_kbd) && detect_input_pending ())
{
nfds = 0;
FD_ZERO (&waitchannels);
}
else
nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
&timeout);
xerrno = errno;
clear_waiting_for_input ();
do_pending_window_change (0);
if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
break;
if (nfds == -1)
{
if (xerrno == EINTR)
FD_ZERO (&waitchannels);
else
error ("select error: %s", emacs_strerror (xerrno));
}
#ifdef sun
else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
kill (getpid (), SIGIO);
#endif
#ifdef SIGIO
if (XINT (read_kbd) && interrupt_input && (waitchannels & 1))
kill (getpid (), SIGIO);
#endif
if ((XINT (read_kbd) != 0)
&& detect_input_pending_run_timers (do_display))
{
swallow_events (do_display);
if (detect_input_pending_run_timers (do_display))
break;
}
if (XINT (read_kbd) != 0
&& requeued_events_pending_p ())
break;
if (wait_for_cell
&& detect_input_pending ())
{
swallow_events (do_display);
if (detect_input_pending ())
break;
}
if (wait_for_cell && ! NILP (*wait_for_cell))
break;
}
start_polling ();
return 0;
}
DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
0)
(name)
register Lisp_Object name;
{
return Qnil;
}
DEFUN ("process-inherit-coding-system-flag",
Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
1, 1, 0,
0)
(process)
register Lisp_Object process;
{
return inherit_process_coding_system ? Qt : Qnil;
}
void
kill_buffer_processes (buffer)
Lisp_Object buffer;
{
}
void
init_emacs_process ()
{
}
void
syms_of_process ()
{
defsubr (&Sget_buffer_process);
defsubr (&Sprocess_inherit_coding_system_flag);
}
#endif