aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
authorKenichi Handa2012-07-17 07:25:00 +0900
committerKenichi Handa2012-07-17 07:25:00 +0900
commit69c41c4070c86baac11a627e9c3d366420aeb7cc (patch)
treee07fda92570b5e4f264c9a7869b57960940008f0 /src/process.c
parent8c536f15bf95916d56bb50495d22b7da7e09fff9 (diff)
parent758e556a7ab8f61c007e34310ba399a9aaf15362 (diff)
downloademacs-69c41c4070c86baac11a627e9c3d366420aeb7cc.tar.gz
emacs-69c41c4070c86baac11a627e9c3d366420aeb7cc.zip
merge trunk
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c505
1 files changed, 284 insertions, 221 deletions
diff --git a/src/process.c b/src/process.c
index 4d59ff0d452..ceb5c81d32b 100644
--- a/src/process.c
+++ b/src/process.c
@@ -93,8 +93,8 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
93#include "systty.h" 93#include "systty.h"
94 94
95#include "window.h" 95#include "window.h"
96#include "buffer.h"
97#include "character.h" 96#include "character.h"
97#include "buffer.h"
98#include "coding.h" 98#include "coding.h"
99#include "process.h" 99#include "process.h"
100#include "frame.h" 100#include "frame.h"
@@ -228,13 +228,11 @@ static EMACS_INT update_tick;
228#endif 228#endif
229 229
230#if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING) 230#if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
231#ifdef EMACS_HAS_USECS
232#define ADAPTIVE_READ_BUFFERING 231#define ADAPTIVE_READ_BUFFERING
233#endif 232#endif
234#endif
235 233
236#ifdef ADAPTIVE_READ_BUFFERING 234#ifdef ADAPTIVE_READ_BUFFERING
237#define READ_OUTPUT_DELAY_INCREMENT 10000 235#define READ_OUTPUT_DELAY_INCREMENT (EMACS_TIME_RESOLUTION / 100)
238#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5) 236#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
239#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7) 237#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
240 238
@@ -251,7 +249,6 @@ static int process_output_skip;
251#define process_output_delay_count 0 249#define process_output_delay_count 0
252#endif 250#endif
253 251
254static Lisp_Object Fget_process (Lisp_Object);
255static void create_process (Lisp_Object, char **, Lisp_Object); 252static void create_process (Lisp_Object, char **, Lisp_Object);
256#ifdef SIGIO 253#ifdef SIGIO
257static int keyboard_bit_set (SELECT_TYPE *); 254static int keyboard_bit_set (SELECT_TYPE *);
@@ -355,7 +352,7 @@ static struct fd_callback_data
355void 352void
356add_read_fd (int fd, fd_callback func, void *data) 353add_read_fd (int fd, fd_callback func, void *data)
357{ 354{
358 xassert (fd < MAXDESC); 355 eassert (fd < MAXDESC);
359 add_keyboard_wait_descriptor (fd); 356 add_keyboard_wait_descriptor (fd);
360 357
361 fd_callback_info[fd].func = func; 358 fd_callback_info[fd].func = func;
@@ -368,7 +365,7 @@ add_read_fd (int fd, fd_callback func, void *data)
368void 365void
369delete_read_fd (int fd) 366delete_read_fd (int fd)
370{ 367{
371 xassert (fd < MAXDESC); 368 eassert (fd < MAXDESC);
372 delete_keyboard_wait_descriptor (fd); 369 delete_keyboard_wait_descriptor (fd);
373 370
374 fd_callback_info[fd].condition &= ~FOR_READ; 371 fd_callback_info[fd].condition &= ~FOR_READ;
@@ -385,7 +382,7 @@ delete_read_fd (int fd)
385void 382void
386add_write_fd (int fd, fd_callback func, void *data) 383add_write_fd (int fd, fd_callback func, void *data)
387{ 384{
388 xassert (fd < MAXDESC); 385 eassert (fd < MAXDESC);
389 FD_SET (fd, &write_mask); 386 FD_SET (fd, &write_mask);
390 if (fd > max_input_desc) 387 if (fd > max_input_desc)
391 max_input_desc = fd; 388 max_input_desc = fd;
@@ -402,7 +399,7 @@ delete_write_fd (int fd)
402{ 399{
403 int lim = max_input_desc; 400 int lim = max_input_desc;
404 401
405 xassert (fd < MAXDESC); 402 eassert (fd < MAXDESC);
406 FD_CLR (fd, &write_mask); 403 FD_CLR (fd, &write_mask);
407 fd_callback_info[fd].condition &= ~FOR_WRITE; 404 fd_callback_info[fd].condition &= ~FOR_WRITE;
408 if (fd_callback_info[fd].condition == 0) 405 if (fd_callback_info[fd].condition == 0)
@@ -627,34 +624,18 @@ make_process (Lisp_Object name)
627 printmax_t i; 624 printmax_t i;
628 625
629 p = allocate_process (); 626 p = allocate_process ();
630 627 /* Initialize Lisp data. Note that allocate_process initializes all
631 p->infd = -1; 628 Lisp data to nil, so do it only for slots which should not be nil. */
632 p->outfd = -1;
633 p->tick = 0;
634 p->update_tick = 0;
635 p->pid = 0;
636 p->pty_flag = 0;
637 p->raw_status_new = 0;
638 p->status = Qrun; 629 p->status = Qrun;
639 p->mark = Fmake_marker (); 630 p->mark = Fmake_marker ();
640 p->kill_without_query = 0;
641 631
642#ifdef ADAPTIVE_READ_BUFFERING 632 /* Initialize non-Lisp data. Note that allocate_process zeroes out all
643 p->adaptive_read_buffering = 0; 633 non-Lisp data, so do it only for slots which should not be zero. */
644 p->read_output_delay = 0; 634 p->infd = -1;
645 p->read_output_skip = 0; 635 p->outfd = -1;
646#endif
647 636
648#ifdef HAVE_GNUTLS 637#ifdef HAVE_GNUTLS
649 p->gnutls_initstage = GNUTLS_STAGE_EMPTY; 638 p->gnutls_initstage = GNUTLS_STAGE_EMPTY;
650 /* Default log level. */
651 p->gnutls_log_level = 0;
652 /* GnuTLS handshakes attempted for this connection. */
653 p->gnutls_handshakes_tried = 0;
654 p->gnutls_p = 0;
655 p->gnutls_state = NULL;
656 p->gnutls_x509_cred = NULL;
657 p->gnutls_anon_cred = NULL;
658#endif 639#endif
659 640
660 /* If name is already in use, modify it until it is unused. */ 641 /* If name is already in use, modify it until it is unused. */
@@ -664,8 +645,7 @@ make_process (Lisp_Object name)
664 { 645 {
665 tem = Fget_process (name1); 646 tem = Fget_process (name1);
666 if (NILP (tem)) break; 647 if (NILP (tem)) break;
667 sprintf (suffix, "<%"pMd">", i); 648 name1 = concat2 (name, make_formatted_string (suffix, "<%"pMd">", i));
668 name1 = concat2 (name, build_string (suffix));
669 } 649 }
670 name = name1; 650 name = name1;
671 p->name = name; 651 p->name = name;
@@ -1046,8 +1026,8 @@ DEFUN ("set-process-window-size", Fset_process_window_size,
1046 (register Lisp_Object process, Lisp_Object height, Lisp_Object width) 1026 (register Lisp_Object process, Lisp_Object height, Lisp_Object width)
1047{ 1027{
1048 CHECK_PROCESS (process); 1028 CHECK_PROCESS (process);
1049 CHECK_RANGED_INTEGER (0, height, INT_MAX); 1029 CHECK_RANGED_INTEGER (height, 0, INT_MAX);
1050 CHECK_RANGED_INTEGER (0, width, INT_MAX); 1030 CHECK_RANGED_INTEGER (width, 0, INT_MAX);
1051 1031
1052 if (XPROCESS (process)->infd < 0 1032 if (XPROCESS (process)->infd < 0
1053 || set_window_size (XPROCESS (process)->infd, 1033 || set_window_size (XPROCESS (process)->infd,
@@ -1107,10 +1087,6 @@ DEFUN ("process-query-on-exit-flag",
1107 return (XPROCESS (process)->kill_without_query ? Qnil : Qt); 1087 return (XPROCESS (process)->kill_without_query ? Qnil : Qt);
1108} 1088}
1109 1089
1110#ifdef DATAGRAM_SOCKETS
1111static Lisp_Object Fprocess_datagram_address (Lisp_Object);
1112#endif
1113
1114DEFUN ("process-contact", Fprocess_contact, Sprocess_contact, 1090DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
1115 1, 2, 0, 1091 1, 2, 0,
1116 doc: /* Return the contact info of PROCESS; t for a real child. 1092 doc: /* Return the contact info of PROCESS; t for a real child.
@@ -1388,7 +1364,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1388 val = Vcoding_system_for_read; 1364 val = Vcoding_system_for_read;
1389 if (NILP (val)) 1365 if (NILP (val))
1390 { 1366 {
1391 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); 1367 args2 = alloca ((nargs + 1) * sizeof *args2);
1392 args2[0] = Qstart_process; 1368 args2[0] = Qstart_process;
1393 for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; 1369 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1394 GCPRO2 (proc, current_dir); 1370 GCPRO2 (proc, current_dir);
@@ -1407,7 +1383,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1407 { 1383 {
1408 if (EQ (coding_systems, Qt)) 1384 if (EQ (coding_systems, Qt))
1409 { 1385 {
1410 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); 1386 args2 = alloca ((nargs + 1) * sizeof *args2);
1411 args2[0] = Qstart_process; 1387 args2[0] = Qstart_process;
1412 for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; 1388 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1413 GCPRO2 (proc, current_dir); 1389 GCPRO2 (proc, current_dir);
@@ -1501,7 +1477,7 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1501 1477
1502 /* Now that everything is encoded we can collect the strings into 1478 /* Now that everything is encoded we can collect the strings into
1503 NEW_ARGV. */ 1479 NEW_ARGV. */
1504 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *)); 1480 new_argv = alloca ((nargs - 1) * sizeof *new_argv);
1505 new_argv[nargs - 2] = 0; 1481 new_argv[nargs - 2] = 0;
1506 1482
1507 for (i = nargs - 2; i-- != 0; ) 1483 for (i = nargs - 2; i-- != 0; )
@@ -1875,10 +1851,9 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1875 So have an interrupt jar it loose. */ 1851 So have an interrupt jar it loose. */
1876 { 1852 {
1877 struct atimer *timer; 1853 struct atimer *timer;
1878 EMACS_TIME offset; 1854 EMACS_TIME offset = make_emacs_time (1, 0);
1879 1855
1880 stop_polling (); 1856 stop_polling ();
1881 EMACS_SET_SECS_USECS (offset, 1, 0);
1882 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0); 1857 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
1883 1858
1884 if (forkin >= 0) 1859 if (forkin >= 0)
@@ -3037,7 +3012,9 @@ usage: (make-network-process &rest ARGS) */)
3037 CHECK_STRING (service); 3012 CHECK_STRING (service);
3038 memset (&address_un, 0, sizeof address_un); 3013 memset (&address_un, 0, sizeof address_un);
3039 address_un.sun_family = AF_LOCAL; 3014 address_un.sun_family = AF_LOCAL;
3040 strncpy (address_un.sun_path, SSDATA (service), sizeof address_un.sun_path); 3015 if (sizeof address_un.sun_path <= SBYTES (service))
3016 error ("Service name too long");
3017 strcpy (address_un.sun_path, SSDATA (service));
3041 ai.ai_addr = (struct sockaddr *) &address_un; 3018 ai.ai_addr = (struct sockaddr *) &address_un;
3042 ai.ai_addrlen = sizeof address_un; 3019 ai.ai_addrlen = sizeof address_un;
3043 goto open_socket; 3020 goto open_socket;
@@ -3290,7 +3267,7 @@ usage: (make-network-process &rest ARGS) */)
3290 { 3267 {
3291 /* Unlike most other syscalls connect() cannot be called 3268 /* Unlike most other syscalls connect() cannot be called
3292 again. (That would return EALREADY.) The proper way to 3269 again. (That would return EALREADY.) The proper way to
3293 wait for completion is select(). */ 3270 wait for completion is pselect(). */
3294 int sc; 3271 int sc;
3295 socklen_t len; 3272 socklen_t len;
3296 SELECT_TYPE fdset; 3273 SELECT_TYPE fdset;
@@ -3298,8 +3275,7 @@ usage: (make-network-process &rest ARGS) */)
3298 FD_ZERO (&fdset); 3275 FD_ZERO (&fdset);
3299 FD_SET (s, &fdset); 3276 FD_SET (s, &fdset);
3300 QUIT; 3277 QUIT;
3301 sc = select (s + 1, (SELECT_TYPE *)0, &fdset, (SELECT_TYPE *)0, 3278 sc = pselect (s + 1, NULL, &fdset, NULL, NULL, NULL);
3302 (EMACS_TIME *)0);
3303 if (sc == -1) 3279 if (sc == -1)
3304 { 3280 {
3305 if (errno == EINTR) 3281 if (errno == EINTR)
@@ -3340,7 +3316,7 @@ usage: (make-network-process &rest ARGS) */)
3340 { 3316 {
3341 if (datagram_address[s].sa) 3317 if (datagram_address[s].sa)
3342 abort (); 3318 abort ();
3343 datagram_address[s].sa = (struct sockaddr *) xmalloc (lres->ai_addrlen); 3319 datagram_address[s].sa = xmalloc (lres->ai_addrlen);
3344 datagram_address[s].len = lres->ai_addrlen; 3320 datagram_address[s].len = lres->ai_addrlen;
3345 if (is_server) 3321 if (is_server)
3346 { 3322 {
@@ -3742,8 +3718,9 @@ FLAGS is the current flags of the interface. */)
3742 3718
3743 CHECK_STRING (ifname); 3719 CHECK_STRING (ifname);
3744 3720
3745 memset (rq.ifr_name, 0, sizeof rq.ifr_name); 3721 if (sizeof rq.ifr_name <= SBYTES (ifname))
3746 strncpy (rq.ifr_name, SSDATA (ifname), sizeof (rq.ifr_name)); 3722 error ("interface name too long");
3723 strcpy (rq.ifr_name, SSDATA (ifname));
3747 3724
3748 s = socket (AF_INET, SOCK_STREAM, 0); 3725 s = socket (AF_INET, SOCK_STREAM, 0);
3749 if (s < 0) 3726 if (s < 0)
@@ -3960,7 +3937,8 @@ If JUST-THIS-ONE is an integer, don't run any timers either.
3960Return non-nil if we received any output before the timeout expired. */) 3937Return non-nil if we received any output before the timeout expired. */)
3961 (register Lisp_Object process, Lisp_Object seconds, Lisp_Object millisec, Lisp_Object just_this_one) 3938 (register Lisp_Object process, Lisp_Object seconds, Lisp_Object millisec, Lisp_Object just_this_one)
3962{ 3939{
3963 int secs = -1, usecs = 0; 3940 intmax_t secs;
3941 int nsecs;
3964 3942
3965 if (! NILP (process)) 3943 if (! NILP (process))
3966 CHECK_PROCESS (process); 3944 CHECK_PROCESS (process);
@@ -3979,17 +3957,36 @@ Return non-nil if we received any output before the timeout expired. */)
3979 } 3957 }
3980 } 3958 }
3981 3959
3960 secs = 0;
3961 nsecs = -1;
3962
3982 if (!NILP (seconds)) 3963 if (!NILP (seconds))
3983 { 3964 {
3984 double duration = extract_float (seconds); 3965 if (INTEGERP (seconds))
3985 if (0 < duration) 3966 {
3986 duration_to_sec_usec (duration, &secs, &usecs); 3967 if (0 < XINT (seconds))
3968 {
3969 secs = XINT (seconds);
3970 nsecs = 0;
3971 }
3972 }
3973 else if (FLOATP (seconds))
3974 {
3975 if (0 < XFLOAT_DATA (seconds))
3976 {
3977 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (seconds));
3978 secs = min (EMACS_SECS (t), WAIT_READING_MAX);
3979 nsecs = EMACS_NSECS (t);
3980 }
3981 }
3982 else
3983 wrong_type_argument (Qnumberp, seconds);
3987 } 3984 }
3988 else if (!NILP (process)) 3985 else if (! NILP (process))
3989 secs = 0; 3986 nsecs = 0;
3990 3987
3991 return 3988 return
3992 (wait_reading_process_output (secs, usecs, 0, 0, 3989 (wait_reading_process_output (secs, nsecs, 0, 0,
3993 Qnil, 3990 Qnil,
3994 !NILP (process) ? XPROCESS (process) : NULL, 3991 !NILP (process) ? XPROCESS (process) : NULL,
3995 NILP (just_this_one) ? 0 : 3992 NILP (just_this_one) ? 0 :
@@ -4230,36 +4227,21 @@ wait_reading_process_output_1 (void)
4230{ 4227{
4231} 4228}
4232 4229
4233/* Use a wrapper around select to work around a bug in gdb 5.3.
4234 Normally, the wrapper is optimized away by inlining.
4235
4236 If emacs is stopped inside select, the gdb backtrace doesn't
4237 show the function which called select, so it is practically
4238 impossible to step through wait_reading_process_output. */
4239
4240#ifndef select
4241static inline int
4242select_wrapper (int n, fd_set *rfd, fd_set *wfd, fd_set *xfd, struct timeval *tmo)
4243{
4244 return select (n, rfd, wfd, xfd, tmo);
4245}
4246#define select select_wrapper
4247#endif
4248
4249/* Read and dispose of subprocess output while waiting for timeout to 4230/* Read and dispose of subprocess output while waiting for timeout to
4250 elapse and/or keyboard input to be available. 4231 elapse and/or keyboard input to be available.
4251 4232
4252 TIME_LIMIT is: 4233 TIME_LIMIT is:
4253 timeout in seconds, or 4234 timeout in seconds
4254 zero for no limit, or 4235 If negative, gobble data immediately available but don't wait for any.
4255 -1 means gobble data immediately available but don't wait for any.
4256 4236
4257 MICROSECS is: 4237 NSECS is:
4258 an additional duration to wait, measured in microseconds. 4238 an additional duration to wait, measured in nanoseconds
4259 If this is nonzero and time_limit is 0, then the timeout 4239 If TIME_LIMIT is zero, then:
4260 consists of MICROSECS only. 4240 If NSECS == 0, there is no limit.
4241 If NSECS > 0, the timeout consists of NSECS only.
4242 If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
4261 4243
4262 READ_KBD is a lisp value: 4244 READ_KBD is:
4263 0 to ignore keyboard input, or 4245 0 to ignore keyboard input, or
4264 1 to return when input is available, or 4246 1 to return when input is available, or
4265 -1 meaning caller will actually read the input, so don't throw to 4247 -1 meaning caller will actually read the input, so don't throw to
@@ -4284,7 +4266,7 @@ select_wrapper (int n, fd_set *rfd, fd_set *wfd, fd_set *xfd, struct timeval *tm
4284 Otherwise, return true if we received input from any process. */ 4266 Otherwise, return true if we received input from any process. */
4285 4267
4286int 4268int
4287wait_reading_process_output (int time_limit, int microsecs, int read_kbd, 4269wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4288 int do_display, 4270 int do_display,
4289 Lisp_Object wait_for_cell, 4271 Lisp_Object wait_for_cell,
4290 struct Lisp_Process *wait_proc, int just_wait_proc) 4272 struct Lisp_Process *wait_proc, int just_wait_proc)
@@ -4304,7 +4286,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4304 FD_ZERO (&Available); 4286 FD_ZERO (&Available);
4305 FD_ZERO (&Writeok); 4287 FD_ZERO (&Writeok);
4306 4288
4307 if (time_limit == 0 && microsecs == 0 && wait_proc && !NILP (Vinhibit_quit) 4289 if (time_limit == 0 && nsecs == 0 && wait_proc && !NILP (Vinhibit_quit)
4308 && !(CONSP (wait_proc->status) && EQ (XCAR (wait_proc->status), Qexit))) 4290 && !(CONSP (wait_proc->status) && EQ (XCAR (wait_proc->status), Qexit)))
4309 message ("Blocking call to accept-process-output with quit inhibited!!"); 4291 message ("Blocking call to accept-process-output with quit inhibited!!");
4310 4292
@@ -4316,13 +4298,20 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4316 make_number (waiting_for_user_input_p)); 4298 make_number (waiting_for_user_input_p));
4317 waiting_for_user_input_p = read_kbd; 4299 waiting_for_user_input_p = read_kbd;
4318 4300
4301 if (time_limit < 0)
4302 {
4303 time_limit = 0;
4304 nsecs = -1;
4305 }
4306 else if (TYPE_MAXIMUM (time_t) < time_limit)
4307 time_limit = TYPE_MAXIMUM (time_t);
4308
4319 /* Since we may need to wait several times, 4309 /* Since we may need to wait several times,
4320 compute the absolute time to return at. */ 4310 compute the absolute time to return at. */
4321 if (time_limit || microsecs) 4311 if (time_limit || 0 < nsecs)
4322 { 4312 {
4323 EMACS_GET_TIME (end_time); 4313 timeout = make_emacs_time (time_limit, nsecs);
4324 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); 4314 end_time = add_emacs_time (current_emacs_time (), timeout);
4325 EMACS_ADD_TIME (end_time, end_time, timeout);
4326 } 4315 }
4327 4316
4328 while (1) 4317 while (1)
@@ -4345,24 +4334,24 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4345 4334
4346 /* Compute time from now till when time limit is up */ 4335 /* Compute time from now till when time limit is up */
4347 /* Exit if already run out */ 4336 /* Exit if already run out */
4348 if (time_limit == -1) 4337 if (nsecs < 0)
4349 { 4338 {
4350 /* -1 specified for timeout means 4339 /* A negative timeout means
4351 gobble output available now 4340 gobble output available now
4352 but don't wait at all. */ 4341 but don't wait at all. */
4353 4342
4354 EMACS_SET_SECS_USECS (timeout, 0, 0); 4343 timeout = make_emacs_time (0, 0);
4355 } 4344 }
4356 else if (time_limit || microsecs) 4345 else if (time_limit || 0 < nsecs)
4357 { 4346 {
4358 EMACS_GET_TIME (timeout); 4347 EMACS_TIME now = current_emacs_time ();
4359 EMACS_SUB_TIME (timeout, end_time, timeout); 4348 if (EMACS_TIME_LE (end_time, now))
4360 if (EMACS_TIME_NEG_P (timeout))
4361 break; 4349 break;
4350 timeout = sub_emacs_time (end_time, now);
4362 } 4351 }
4363 else 4352 else
4364 { 4353 {
4365 EMACS_SET_SECS_USECS (timeout, 100000, 0); 4354 timeout = make_emacs_time (100000, 0);
4366 } 4355 }
4367 4356
4368 /* Normally we run timers here. 4357 /* Normally we run timers here.
@@ -4404,21 +4393,22 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4404 && requeued_events_pending_p ()) 4393 && requeued_events_pending_p ())
4405 break; 4394 break;
4406 4395
4407 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1) 4396 /* A negative timeout means do not wait at all. */
4397 if (0 <= nsecs)
4408 { 4398 {
4409 EMACS_TIME difference; 4399 if (EMACS_TIME_VALID_P (timer_delay))
4410 EMACS_SUB_TIME (difference, timer_delay, timeout);
4411 if (EMACS_TIME_NEG_P (difference))
4412 { 4400 {
4413 timeout = timer_delay; 4401 if (EMACS_TIME_LT (timer_delay, timeout))
4414 timeout_reduced_for_timers = 1; 4402 {
4403 timeout = timer_delay;
4404 timeout_reduced_for_timers = 1;
4405 }
4406 }
4407 else
4408 {
4409 /* This is so a breakpoint can be put here. */
4410 wait_reading_process_output_1 ();
4415 } 4411 }
4416 }
4417 /* If time_limit is -1, we are not going to wait at all. */
4418 else if (time_limit != -1)
4419 {
4420 /* This is so a breakpoint can be put here. */
4421 wait_reading_process_output_1 ();
4422 } 4412 }
4423 } 4413 }
4424 4414
@@ -4446,15 +4436,15 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4446 Atemp = input_wait_mask; 4436 Atemp = input_wait_mask;
4447 Ctemp = write_mask; 4437 Ctemp = write_mask;
4448 4438
4449 EMACS_SET_SECS_USECS (timeout, 0, 0); 4439 timeout = make_emacs_time (0, 0);
4450 if ((select (max (max_process_desc, max_input_desc) + 1, 4440 if ((pselect (max (max_process_desc, max_input_desc) + 1,
4451 &Atemp, 4441 &Atemp,
4452#ifdef NON_BLOCKING_CONNECT 4442#ifdef NON_BLOCKING_CONNECT
4453 (num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0), 4443 (num_pending_connects > 0 ? &Ctemp : NULL),
4454#else 4444#else
4455 (SELECT_TYPE *)0, 4445 NULL,
4456#endif 4446#endif
4457 (SELECT_TYPE *)0, &timeout) 4447 NULL, &timeout, NULL)
4458 <= 0)) 4448 <= 0))
4459 { 4449 {
4460 /* It's okay for us to do this and then continue with 4450 /* It's okay for us to do this and then continue with
@@ -4577,9 +4567,9 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4577 Vprocess_adaptive_read_buffering is nil. */ 4567 Vprocess_adaptive_read_buffering is nil. */
4578 if (process_output_skip && check_delay > 0) 4568 if (process_output_skip && check_delay > 0)
4579 { 4569 {
4580 int usecs = EMACS_USECS (timeout); 4570 int nsecs = EMACS_NSECS (timeout);
4581 if (EMACS_SECS (timeout) > 0 || usecs > READ_OUTPUT_DELAY_MAX) 4571 if (EMACS_SECS (timeout) > 0 || nsecs > READ_OUTPUT_DELAY_MAX)
4582 usecs = READ_OUTPUT_DELAY_MAX; 4572 nsecs = READ_OUTPUT_DELAY_MAX;
4583 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++) 4573 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4584 { 4574 {
4585 proc = chan_process[channel]; 4575 proc = chan_process[channel];
@@ -4594,11 +4584,11 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4594 continue; 4584 continue;
4595 FD_CLR (channel, &Available); 4585 FD_CLR (channel, &Available);
4596 XPROCESS (proc)->read_output_skip = 0; 4586 XPROCESS (proc)->read_output_skip = 0;
4597 if (XPROCESS (proc)->read_output_delay < usecs) 4587 if (XPROCESS (proc)->read_output_delay < nsecs)
4598 usecs = XPROCESS (proc)->read_output_delay; 4588 nsecs = XPROCESS (proc)->read_output_delay;
4599 } 4589 }
4600 } 4590 }
4601 EMACS_SET_SECS_USECS (timeout, 0, usecs); 4591 timeout = make_emacs_time (0, nsecs);
4602 process_output_skip = 0; 4592 process_output_skip = 0;
4603 } 4593 }
4604#endif 4594#endif
@@ -4607,12 +4597,12 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4607#elif defined (HAVE_NS) 4597#elif defined (HAVE_NS)
4608 nfds = ns_select 4598 nfds = ns_select
4609#else 4599#else
4610 nfds = select 4600 nfds = pselect
4611#endif 4601#endif
4612 (max (max_process_desc, max_input_desc) + 1, 4602 (max (max_process_desc, max_input_desc) + 1,
4613 &Available, 4603 &Available,
4614 (check_write ? &Writeok : (SELECT_TYPE *)0), 4604 (check_write ? &Writeok : (SELECT_TYPE *)0),
4615 (SELECT_TYPE *)0, &timeout); 4605 NULL, &timeout, NULL);
4616 4606
4617#ifdef HAVE_GNUTLS 4607#ifdef HAVE_GNUTLS
4618 /* GnuTLS buffers data internally. In lowat mode it leaves 4608 /* GnuTLS buffers data internally. In lowat mode it leaves
@@ -4670,8 +4660,8 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4670 /* If we woke up due to SIGWINCH, actually change size now. */ 4660 /* If we woke up due to SIGWINCH, actually change size now. */
4671 do_pending_window_change (0); 4661 do_pending_window_change (0);
4672 4662
4673 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers) 4663 if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers)
4674 /* We wanted the full specified time, so return now. */ 4664 /* We waited the full specified time, so return now. */
4675 break; 4665 break;
4676 if (nfds < 0) 4666 if (nfds < 0)
4677 { 4667 {
@@ -4822,7 +4812,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
4822 if (wait_channel == channel) 4812 if (wait_channel == channel)
4823 { 4813 {
4824 wait_channel = -1; 4814 wait_channel = -1;
4825 time_limit = -1; 4815 nsecs = -1;
4826 got_some_input = 1; 4816 got_some_input = 1;
4827 } 4817 }
4828 proc = chan_process[channel]; 4818 proc = chan_process[channel];
@@ -5047,7 +5037,7 @@ read_process_output (Lisp_Object proc, register int channel)
5047 ptrdiff_t count = SPECPDL_INDEX (); 5037 ptrdiff_t count = SPECPDL_INDEX ();
5048 Lisp_Object odeactivate; 5038 Lisp_Object odeactivate;
5049 5039
5050 chars = (char *) alloca (carryover + readmax); 5040 chars = alloca (carryover + readmax);
5051 if (carryover) 5041 if (carryover)
5052 /* See the comment above. */ 5042 /* See the comment above. */
5053 memcpy (chars, SDATA (p->decoding_buf), carryover); 5043 memcpy (chars, SDATA (p->decoding_buf), carryover);
@@ -5360,7 +5350,7 @@ static jmp_buf send_process_frame;
5360static Lisp_Object process_sent_to; 5350static Lisp_Object process_sent_to;
5361 5351
5362#ifndef FORWARD_SIGNAL_TO_MAIN_THREAD 5352#ifndef FORWARD_SIGNAL_TO_MAIN_THREAD
5363static void send_process_trap (int) NO_RETURN; 5353static _Noreturn void send_process_trap (int);
5364#endif 5354#endif
5365 5355
5366static void 5356static void
@@ -5371,6 +5361,78 @@ send_process_trap (int ignore)
5371 longjmp (send_process_frame, 1); 5361 longjmp (send_process_frame, 1);
5372} 5362}
5373 5363
5364/* In send_process, when a write fails temporarily,
5365 wait_reading_process_output is called. It may execute user code,
5366 e.g. timers, that attempts to write new data to the same process.
5367 We must ensure that data is sent in the right order, and not
5368 interspersed half-completed with other writes (Bug#10815). This is
5369 handled by the write_queue element of struct process. It is a list
5370 with each entry having the form
5371
5372 (string . (offset . length))
5373
5374 where STRING is a lisp string, OFFSET is the offset into the
5375 string's byte sequence from which we should begin to send, and
5376 LENGTH is the number of bytes left to send. */
5377
5378/* Create a new entry in write_queue.
5379 INPUT_OBJ should be a buffer, string Qt, or Qnil.
5380 BUF is a pointer to the string sequence of the input_obj or a C
5381 string in case of Qt or Qnil. */
5382
5383static void
5384write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj,
5385 const char *buf, ptrdiff_t len, int front)
5386{
5387 ptrdiff_t offset;
5388 Lisp_Object entry, obj;
5389
5390 if (STRINGP (input_obj))
5391 {
5392 offset = buf - SSDATA (input_obj);
5393 obj = input_obj;
5394 }
5395 else
5396 {
5397 offset = 0;
5398 obj = make_unibyte_string (buf, len);
5399 }
5400
5401 entry = Fcons (obj, Fcons (make_number (offset), make_number (len)));
5402
5403 if (front)
5404 p->write_queue = Fcons (entry, p->write_queue);
5405 else
5406 p->write_queue = nconc2 (p->write_queue, Fcons (entry, Qnil));
5407}
5408
5409/* Remove the first element in the write_queue of process P, put its
5410 contents in OBJ, BUF and LEN, and return non-zero. If the
5411 write_queue is empty, return zero. */
5412
5413static int
5414write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj,
5415 const char **buf, ptrdiff_t *len)
5416{
5417 Lisp_Object entry, offset_length;
5418 ptrdiff_t offset;
5419
5420 if (NILP (p->write_queue))
5421 return 0;
5422
5423 entry = XCAR (p->write_queue);
5424 p->write_queue = XCDR (p->write_queue);
5425
5426 *obj = XCAR (entry);
5427 offset_length = XCDR (entry);
5428
5429 *len = XINT (XCDR (offset_length));
5430 offset = XINT (XCAR (offset_length));
5431 *buf = SSDATA (*obj) + offset;
5432
5433 return 1;
5434}
5435
5374/* Send some data to process PROC. 5436/* Send some data to process PROC.
5375 BUF is the beginning of the data; LEN is the number of characters. 5437 BUF is the beginning of the data; LEN is the number of characters.
5376 OBJECT is the Lisp object that the data comes from. If OBJECT is 5438 OBJECT is the Lisp object that the data comes from. If OBJECT is
@@ -5389,11 +5451,8 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5389 struct Lisp_Process *p = XPROCESS (proc); 5451 struct Lisp_Process *p = XPROCESS (proc);
5390 ssize_t rv; 5452 ssize_t rv;
5391 struct coding_system *coding; 5453 struct coding_system *coding;
5392 struct gcpro gcpro1;
5393 void (*volatile old_sigpipe) (int); 5454 void (*volatile old_sigpipe) (int);
5394 5455
5395 GCPRO1 (object);
5396
5397 if (p->raw_status_new) 5456 if (p->raw_status_new)
5398 update_status (p); 5457 update_status (p);
5399 if (! EQ (p->status, Qrun)) 5458 if (! EQ (p->status, Qrun))
@@ -5505,22 +5564,37 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5505 if (!setjmp (send_process_frame)) 5564 if (!setjmp (send_process_frame))
5506 { 5565 {
5507 p = XPROCESS (proc); /* Repair any setjmp clobbering. */ 5566 p = XPROCESS (proc); /* Repair any setjmp clobbering. */
5508
5509 process_sent_to = proc; 5567 process_sent_to = proc;
5510 while (len > 0) 5568
5569 /* If there is already data in the write_queue, put the new data
5570 in the back of queue. Otherwise, ignore it. */
5571 if (!NILP (p->write_queue))
5572 write_queue_push (p, object, buf, len, 0);
5573
5574 do /* while !NILP (p->write_queue) */
5511 { 5575 {
5512 ptrdiff_t this = len; 5576 ptrdiff_t cur_len = -1;
5577 const char *cur_buf;
5578 Lisp_Object cur_object;
5579
5580 /* If write_queue is empty, ignore it. */
5581 if (!write_queue_pop (p, &cur_object, &cur_buf, &cur_len))
5582 {
5583 cur_len = len;
5584 cur_buf = buf;
5585 cur_object = object;
5586 }
5513 5587
5514 /* Send this batch, using one or more write calls. */ 5588 while (cur_len > 0)
5515 while (this > 0)
5516 { 5589 {
5590 /* Send this batch, using one or more write calls. */
5517 ptrdiff_t written = 0; 5591 ptrdiff_t written = 0;
5518 int outfd = p->outfd; 5592 int outfd = p->outfd;
5519 old_sigpipe = (void (*) (int)) signal (SIGPIPE, send_process_trap); 5593 old_sigpipe = (void (*) (int)) signal (SIGPIPE, send_process_trap);
5520#ifdef DATAGRAM_SOCKETS 5594#ifdef DATAGRAM_SOCKETS
5521 if (DATAGRAM_CHAN_P (outfd)) 5595 if (DATAGRAM_CHAN_P (outfd))
5522 { 5596 {
5523 rv = sendto (outfd, buf, this, 5597 rv = sendto (outfd, cur_buf, cur_len,
5524 0, datagram_address[outfd].sa, 5598 0, datagram_address[outfd].sa,
5525 datagram_address[outfd].len); 5599 datagram_address[outfd].len);
5526 if (0 <= rv) 5600 if (0 <= rv)
@@ -5537,10 +5611,10 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5537 { 5611 {
5538#ifdef HAVE_GNUTLS 5612#ifdef HAVE_GNUTLS
5539 if (p->gnutls_p) 5613 if (p->gnutls_p)
5540 written = emacs_gnutls_write (p, buf, this); 5614 written = emacs_gnutls_write (p, cur_buf, cur_len);
5541 else 5615 else
5542#endif 5616#endif
5543 written = emacs_write (outfd, buf, this); 5617 written = emacs_write (outfd, cur_buf, cur_len);
5544 rv = (written ? 0 : -1); 5618 rv = (written ? 0 : -1);
5545#ifdef ADAPTIVE_READ_BUFFERING 5619#ifdef ADAPTIVE_READ_BUFFERING
5546 if (p->read_output_delay > 0 5620 if (p->read_output_delay > 0
@@ -5568,8 +5642,6 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5568 that may allow the program 5642 that may allow the program
5569 to finish doing output and read more. */ 5643 to finish doing output and read more. */
5570 { 5644 {
5571 ptrdiff_t offset = 0;
5572
5573#ifdef BROKEN_PTY_READ_AFTER_EAGAIN 5645#ifdef BROKEN_PTY_READ_AFTER_EAGAIN
5574 /* A gross hack to work around a bug in FreeBSD. 5646 /* A gross hack to work around a bug in FreeBSD.
5575 In the following sequence, read(2) returns 5647 In the following sequence, read(2) returns
@@ -5595,35 +5667,22 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5595 } 5667 }
5596#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */ 5668#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
5597 5669
5598 /* Running filters might relocate buffers or strings. 5670 /* Put what we should have written in wait_queue. */
5599 Arrange to relocate BUF. */ 5671 write_queue_push (p, cur_object, cur_buf, cur_len, 1);
5600 if (BUFFERP (object)) 5672 wait_reading_process_output (0, 20 * 1000 * 1000,
5601 offset = BUF_PTR_BYTE_POS (XBUFFER (object), 5673 0, 0, Qnil, NULL, 0);
5602 (unsigned char *) buf); 5674 /* Reread queue, to see what is left. */
5603 else if (STRINGP (object)) 5675 break;
5604 offset = buf - SSDATA (object);
5605
5606#ifdef EMACS_HAS_USECS
5607 wait_reading_process_output (0, 20000, 0, 0, Qnil, NULL, 0);
5608#else
5609 wait_reading_process_output (1, 0, 0, 0, Qnil, NULL, 0);
5610#endif
5611
5612 if (BUFFERP (object))
5613 buf = (char *) BUF_BYTE_ADDRESS (XBUFFER (object),
5614 offset);
5615 else if (STRINGP (object))
5616 buf = offset + SSDATA (object);
5617 } 5676 }
5618 else 5677 else
5619 /* This is a real error. */ 5678 /* This is a real error. */
5620 report_file_error ("writing to process", Fcons (proc, Qnil)); 5679 report_file_error ("writing to process", Fcons (proc, Qnil));
5621 } 5680 }
5622 buf += written; 5681 cur_buf += written;
5623 len -= written; 5682 cur_len -= written;
5624 this -= written;
5625 } 5683 }
5626 } 5684 }
5685 while (!NILP (p->write_queue));
5627 } 5686 }
5628 else 5687 else
5629 { 5688 {
@@ -5636,8 +5695,6 @@ send_process (volatile Lisp_Object proc, const char *volatile buf,
5636 deactivate_process (proc); 5695 deactivate_process (proc);
5637 error ("SIGPIPE raised on process %s; closed it", SDATA (p->name)); 5696 error ("SIGPIPE raised on process %s; closed it", SDATA (p->name));
5638 } 5697 }
5639
5640 UNGCPRO;
5641} 5698}
5642 5699
5643DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region, 5700DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
@@ -6229,7 +6286,7 @@ process has been transmitted to the serial port. */)
6229 6286
6230 if (!proc_encode_coding_system[new_outfd]) 6287 if (!proc_encode_coding_system[new_outfd])
6231 proc_encode_coding_system[new_outfd] 6288 proc_encode_coding_system[new_outfd]
6232 = (struct coding_system *) xmalloc (sizeof (struct coding_system)); 6289 = xmalloc (sizeof (struct coding_system));
6233 memcpy (proc_encode_coding_system[new_outfd], 6290 memcpy (proc_encode_coding_system[new_outfd],
6234 proc_encode_coding_system[old_outfd], 6291 proc_encode_coding_system[old_outfd],
6235 sizeof (struct coding_system)); 6292 sizeof (struct coding_system));
@@ -6367,7 +6424,7 @@ sigchld_handler (int signo)
6367 /* Tell wait_reading_process_output that it needs to wake up and 6424 /* Tell wait_reading_process_output that it needs to wake up and
6368 look around. */ 6425 look around. */
6369 if (input_available_clear_time) 6426 if (input_available_clear_time)
6370 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); 6427 *input_available_clear_time = make_emacs_time (0, 0);
6371 } 6428 }
6372 6429
6373 /* There was no asynchronous process found for that pid: we have 6430 /* There was no asynchronous process found for that pid: we have
@@ -6385,7 +6442,7 @@ sigchld_handler (int signo)
6385 /* Tell wait_reading_process_output that it needs to wake up and 6442 /* Tell wait_reading_process_output that it needs to wake up and
6386 look around. */ 6443 look around. */
6387 if (input_available_clear_time) 6444 if (input_available_clear_time)
6388 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0); 6445 *input_available_clear_time = make_emacs_time (0, 0);
6389 } 6446 }
6390 6447
6391 sigchld_end_of_loop: 6448 sigchld_end_of_loop:
@@ -6746,19 +6803,25 @@ keyboard_bit_set (fd_set *mask)
6746 6803
6747/* Defined on msdos.c. */ 6804/* Defined on msdos.c. */
6748extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *, 6805extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
6749 EMACS_TIME *); 6806 EMACS_TIME *, void *);
6750 6807
6751/* Implementation of wait_reading_process_output, assuming that there 6808/* Implementation of wait_reading_process_output, assuming that there
6752 are no subprocesses. Used only by the MS-DOS build. 6809 are no subprocesses. Used only by the MS-DOS build.
6753 6810
6754 Wait for timeout to elapse and/or keyboard input to be available. 6811 Wait for timeout to elapse and/or keyboard input to be available.
6755 6812
6756 time_limit is: 6813 TIME_LIMIT is:
6757 timeout in seconds, or 6814 timeout in seconds
6758 zero for no limit, or 6815 If negative, gobble data immediately available but don't wait for any.
6759 -1 means gobble data immediately available but don't wait for any. 6816
6817 NSECS is:
6818 an additional duration to wait, measured in nanoseconds
6819 If TIME_LIMIT is zero, then:
6820 If NSECS == 0, there is no limit.
6821 If NSECS > 0, the timeout consists of NSECS only.
6822 If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.
6760 6823
6761 read_kbd is a Lisp_Object: 6824 READ_KBD is:
6762 0 to ignore keyboard input, or 6825 0 to ignore keyboard input, or
6763 1 to return when input is available, or 6826 1 to return when input is available, or
6764 -1 means caller will actually read the input, so don't throw to 6827 -1 means caller will actually read the input, so don't throw to
@@ -6767,28 +6830,33 @@ extern int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
6767 see full version for other parameters. We know that wait_proc will 6830 see full version for other parameters. We know that wait_proc will
6768 always be NULL, since `subprocesses' isn't defined. 6831 always be NULL, since `subprocesses' isn't defined.
6769 6832
6770 do_display != 0 means redisplay should be done to show subprocess 6833 DO_DISPLAY != 0 means redisplay should be done to show subprocess
6771 output that arrives. 6834 output that arrives.
6772 6835
6773 Return true if we received input from any process. */ 6836 Return true if we received input from any process. */
6774 6837
6775int 6838int
6776wait_reading_process_output (int time_limit, int microsecs, int read_kbd, 6839wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
6777 int do_display, 6840 int do_display,
6778 Lisp_Object wait_for_cell, 6841 Lisp_Object wait_for_cell,
6779 struct Lisp_Process *wait_proc, int just_wait_proc) 6842 struct Lisp_Process *wait_proc, int just_wait_proc)
6780{ 6843{
6781 register int nfds; 6844 register int nfds;
6782 EMACS_TIME end_time, timeout; 6845 EMACS_TIME end_time, timeout;
6783 SELECT_TYPE waitchannels; 6846
6784 int xerrno; 6847 if (time_limit < 0)
6848 {
6849 time_limit = 0;
6850 nsecs = -1;
6851 }
6852 else if (TYPE_MAXIMUM (time_t) < time_limit)
6853 time_limit = TYPE_MAXIMUM (time_t);
6785 6854
6786 /* What does time_limit really mean? */ 6855 /* What does time_limit really mean? */
6787 if (time_limit || microsecs) 6856 if (time_limit || 0 < nsecs)
6788 { 6857 {
6789 EMACS_GET_TIME (end_time); 6858 timeout = make_emacs_time (time_limit, nsecs);
6790 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs); 6859 end_time = add_emacs_time (current_emacs_time (), timeout);
6791 EMACS_ADD_TIME (end_time, end_time, timeout);
6792 } 6860 }
6793 6861
6794 /* Turn off periodic alarms (in case they are in use) 6862 /* Turn off periodic alarms (in case they are in use)
@@ -6800,6 +6868,8 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6800 while (1) 6868 while (1)
6801 { 6869 {
6802 int timeout_reduced_for_timers = 0; 6870 int timeout_reduced_for_timers = 0;
6871 SELECT_TYPE waitchannels;
6872 int xerrno;
6803 6873
6804 /* If calling from keyboard input, do not quit 6874 /* If calling from keyboard input, do not quit
6805 since we want to return C-g as an input character. 6875 since we want to return C-g as an input character.
@@ -6813,24 +6883,24 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6813 6883
6814 /* Compute time from now till when time limit is up */ 6884 /* Compute time from now till when time limit is up */
6815 /* Exit if already run out */ 6885 /* Exit if already run out */
6816 if (time_limit == -1) 6886 if (nsecs < 0)
6817 { 6887 {
6818 /* -1 specified for timeout means 6888 /* A negative timeout means
6819 gobble output available now 6889 gobble output available now
6820 but don't wait at all. */ 6890 but don't wait at all. */
6821 6891
6822 EMACS_SET_SECS_USECS (timeout, 0, 0); 6892 timeout = make_emacs_time (0, 0);
6823 } 6893 }
6824 else if (time_limit || microsecs) 6894 else if (time_limit || 0 < nsecs)
6825 { 6895 {
6826 EMACS_GET_TIME (timeout); 6896 EMACS_TIME now = current_emacs_time ();
6827 EMACS_SUB_TIME (timeout, end_time, timeout); 6897 if (EMACS_TIME_LE (end_time, now))
6828 if (EMACS_TIME_NEG_P (timeout))
6829 break; 6898 break;
6899 timeout = sub_emacs_time (end_time, now);
6830 } 6900 }
6831 else 6901 else
6832 { 6902 {
6833 EMACS_SET_SECS_USECS (timeout, 100000, 0); 6903 timeout = make_emacs_time (100000, 0);
6834 } 6904 }
6835 6905
6836 /* If our caller will not immediately handle keyboard events, 6906 /* If our caller will not immediately handle keyboard events,
@@ -6859,11 +6929,9 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6859 && requeued_events_pending_p ()) 6929 && requeued_events_pending_p ())
6860 break; 6930 break;
6861 6931
6862 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1) 6932 if (EMACS_TIME_VALID_P (timer_delay) && 0 <= nsecs)
6863 { 6933 {
6864 EMACS_TIME difference; 6934 if (EMACS_TIME_LT (timer_delay, timeout))
6865 EMACS_SUB_TIME (difference, timer_delay, timeout);
6866 if (EMACS_TIME_NEG_P (difference))
6867 { 6935 {
6868 timeout = timer_delay; 6936 timeout = timer_delay;
6869 timeout_reduced_for_timers = 1; 6937 timeout_reduced_for_timers = 1;
@@ -6876,13 +6944,6 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6876 if (read_kbd < 0) 6944 if (read_kbd < 0)
6877 set_waiting_for_input (&timeout); 6945 set_waiting_for_input (&timeout);
6878 6946
6879 /* Wait till there is something to do. */
6880
6881 if (! read_kbd && NILP (wait_for_cell))
6882 FD_ZERO (&waitchannels);
6883 else
6884 FD_SET (0, &waitchannels);
6885
6886 /* If a frame has been newly mapped and needs updating, 6947 /* If a frame has been newly mapped and needs updating,
6887 reprocess its display stuff. */ 6948 reprocess its display stuff. */
6888 if (frame_garbaged && do_display) 6949 if (frame_garbaged && do_display)
@@ -6893,14 +6954,16 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6893 set_waiting_for_input (&timeout); 6954 set_waiting_for_input (&timeout);
6894 } 6955 }
6895 6956
6957 /* Wait till there is something to do. */
6958 FD_ZERO (&waitchannels);
6896 if (read_kbd && detect_input_pending ()) 6959 if (read_kbd && detect_input_pending ())
6960 nfds = 0;
6961 else
6897 { 6962 {
6898 nfds = 0; 6963 if (read_kbd || !NILP (wait_for_cell))
6899 FD_ZERO (&waitchannels); 6964 FD_SET (0, &waitchannels);
6965 nfds = pselect (1, &waitchannels, NULL, NULL, &timeout, NULL);
6900 } 6966 }
6901 else
6902 nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
6903 &timeout);
6904 6967
6905 xerrno = errno; 6968 xerrno = errno;
6906 6969
@@ -6910,7 +6973,7 @@ wait_reading_process_output (int time_limit, int microsecs, int read_kbd,
6910 /* If we woke up due to SIGWINCH, actually change size now. */ 6973 /* If we woke up due to SIGWINCH, actually change size now. */
6911 do_pending_window_change (0); 6974 do_pending_window_change (0);
6912 6975
6913 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers) 6976 if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers)
6914 /* We waited the full specified time, so return now. */ 6977 /* We waited the full specified time, so return now. */
6915 break; 6978 break;
6916 6979
@@ -7015,8 +7078,7 @@ setup_process_coding_systems (Lisp_Object process)
7015 return; 7078 return;
7016 7079
7017 if (!proc_decode_coding_system[inch]) 7080 if (!proc_decode_coding_system[inch])
7018 proc_decode_coding_system[inch] 7081 proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system));
7019 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
7020 coding_system = p->decode_coding_system; 7082 coding_system = p->decode_coding_system;
7021 if (! NILP (p->filter)) 7083 if (! NILP (p->filter))
7022 ; 7084 ;
@@ -7028,8 +7090,7 @@ setup_process_coding_systems (Lisp_Object process)
7028 setup_coding_system (coding_system, proc_decode_coding_system[inch]); 7090 setup_coding_system (coding_system, proc_decode_coding_system[inch]);
7029 7091
7030 if (!proc_encode_coding_system[outch]) 7092 if (!proc_encode_coding_system[outch])
7031 proc_encode_coding_system[outch] 7093 proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system));
7032 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
7033 setup_coding_system (p->encode_coding_system, 7094 setup_coding_system (p->encode_coding_system,
7034 proc_encode_coding_system[outch]); 7095 proc_encode_coding_system[outch]);
7035#endif 7096#endif
@@ -7213,19 +7274,20 @@ integer or floating point values.
7213 majflt -- number of major page faults (number) 7274 majflt -- number of major page faults (number)
7214 cminflt -- cumulative number of minor page faults (number) 7275 cminflt -- cumulative number of minor page faults (number)
7215 cmajflt -- cumulative number of major page faults (number) 7276 cmajflt -- cumulative number of major page faults (number)
7216 utime -- user time used by the process, in the (HIGH LOW USEC) format 7277 utime -- user time used by the process, in (current-time) format,
7217 stime -- system time used by the process, in the (HIGH LOW USEC) format 7278 which is a list of integers (HIGH LOW USEC PSEC)
7218 time -- sum of utime and stime, in the (HIGH LOW USEC) format 7279 stime -- system time used by the process (current-time)
7219 cutime -- user time used by the process and its children, (HIGH LOW USEC) 7280 time -- sum of utime and stime (current-time)
7220 cstime -- system time used by the process and its children, (HIGH LOW USEC) 7281 cutime -- user time used by the process and its children (current-time)
7221 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format 7282 cstime -- system time used by the process and its children (current-time)
7283 ctime -- sum of cutime and cstime (current-time)
7222 pri -- priority of the process (number) 7284 pri -- priority of the process (number)
7223 nice -- nice value of the process (number) 7285 nice -- nice value of the process (number)
7224 thcount -- process thread count (number) 7286 thcount -- process thread count (number)
7225 start -- time the process started, in the (HIGH LOW USEC) format 7287 start -- time the process started (current-time)
7226 vsize -- virtual memory size of the process in KB's (number) 7288 vsize -- virtual memory size of the process in KB's (number)
7227 rss -- resident set size of the process in KB's (number) 7289 rss -- resident set size of the process in KB's (number)
7228 etime -- elapsed time the process is running, in (HIGH LOW USEC) format 7290 etime -- elapsed time the process is running, in (HIGH LOW USEC PSEC) format
7229 pcpu -- percents of CPU time used by the process (floating-point number) 7291 pcpu -- percents of CPU time used by the process (floating-point number)
7230 pmem -- percents of total physical memory used by process's resident set 7292 pmem -- percents of total physical memory used by process's resident set
7231 (floating-point number) 7293 (floating-point number)
@@ -7236,8 +7298,10 @@ integer or floating point values.
7236} 7298}
7237 7299
7238 7300
7301/* This is not called "init_process" because that is the name of a
7302 Mach system call, so it would cause problems on Darwin systems. */
7239void 7303void
7240init_process (void) 7304init_process_emacs (void)
7241{ 7305{
7242#ifdef subprocesses 7306#ifdef subprocesses
7243 register int i; 7307 register int i;
@@ -7334,8 +7398,7 @@ init_process (void)
7334 char const *release = (STRINGP (Voperating_system_release) 7398 char const *release = (STRINGP (Voperating_system_release)
7335 ? SSDATA (Voperating_system_release) 7399 ? SSDATA (Voperating_system_release)
7336 : 0); 7400 : 0);
7337 if (!release || !release[0] || (release[0] < MIN_PTY_KERNEL_VERSION 7401 if (!release || !release[0] || (release[0] < '7' && release[1] == '.')) {
7338 && release[1] == '.')) {
7339 Vprocess_connection_type = Qnil; 7402 Vprocess_connection_type = Qnil;
7340 } 7403 }
7341 } 7404 }