aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c261
1 files changed, 170 insertions, 91 deletions
diff --git a/src/process.c b/src/process.c
index 1d935ba8a3e..2800fa58340 100644
--- a/src/process.c
+++ b/src/process.c
@@ -195,24 +195,15 @@ static EMACS_INT process_tick;
195/* Number of events for which the user or sentinel has been notified. */ 195/* Number of events for which the user or sentinel has been notified. */
196static EMACS_INT update_tick; 196static EMACS_INT update_tick;
197 197
198/* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */ 198/* Define NON_BLOCKING_CONNECT if we can support non-blocking connects.
199 The code can be simplified by assuming NON_BLOCKING_CONNECT once
200 Emacs starts assuming POSIX 1003.1-2001 or later. */
199 201
200/* Only W32 has this, it really means that select can't take write mask. */ 202#if (defined HAVE_SELECT \
201#ifdef BROKEN_NON_BLOCKING_CONNECT 203 && (defined GNU_LINUX || defined HAVE_GETPEERNAME) \
202#undef NON_BLOCKING_CONNECT 204 && (defined EWOULDBLOCK || defined EINPROGRESS))
203enum { SELECT_CAN_DO_WRITE_MASK = false }; 205# define NON_BLOCKING_CONNECT
204#else 206#endif
205enum { SELECT_CAN_DO_WRITE_MASK = true };
206#ifndef NON_BLOCKING_CONNECT
207#ifdef HAVE_SELECT
208#if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
209#if defined (EWOULDBLOCK) || defined (EINPROGRESS)
210#define NON_BLOCKING_CONNECT
211#endif /* EWOULDBLOCK || EINPROGRESS */
212#endif /* HAVE_GETPEERNAME || GNU_LINUX */
213#endif /* HAVE_SELECT */
214#endif /* NON_BLOCKING_CONNECT */
215#endif /* BROKEN_NON_BLOCKING_CONNECT */
216 207
217/* Define DATAGRAM_SOCKETS if datagrams can be used safely on 208/* Define DATAGRAM_SOCKETS if datagrams can be used safely on
218 this system. We need to read full packets, so we need a 209 this system. We need to read full packets, so we need a
@@ -1355,34 +1346,62 @@ DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1355 1346
1356static void start_process_unwind (Lisp_Object proc); 1347static void start_process_unwind (Lisp_Object proc);
1357 1348
1358DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0, 1349DEFUN ("make-process", Fmake_process, Smake_process, 0, MANY, 0,
1359 doc: /* Start a program in a subprocess. Return the process object for it. 1350 doc: /* Start a program in a subprocess. Return the process object for it.
1360NAME is name for process. It is modified if necessary to make it unique.
1361BUFFER is the buffer (or buffer name) to associate with the process.
1362 1351
1363Process output (both standard output and standard error streams) goes 1352This is similar to `start-process', but arguments are specified as
1364at end of BUFFER, unless you specify an output stream or filter 1353keyword/argument pairs. The following arguments are defined:
1365function to handle the output. BUFFER may also be nil, meaning that 1354
1366this process is not associated with any buffer. 1355:name NAME -- NAME is name for process. It is modified if necessary
1356to make it unique.
1357
1358:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
1359with the process. Process output goes at end of that buffer, unless
1360you specify an output stream or filter function to handle the output.
1361BUFFER may be also nil, meaning that this process is not associated
1362with any buffer.
1363
1364:command COMMAND -- COMMAND is a list starting with the program file
1365name, followed by strings to give to the program as arguments.
1367 1366
1368PROGRAM is the program file name. It is searched for in `exec-path' 1367:coding CODING -- If CODING is a symbol, it specifies the coding
1369(which see). If nil, just associate a pty with the buffer. Remaining 1368system used for both reading and writing for this process. If CODING
1370arguments are strings to give program as arguments. 1369is a cons (DECODING . ENCODING), DECODING is used for reading, and
1370ENCODING is used for writing.
1371 1371
1372If you want to separate standard output from standard error, invoke 1372:noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
1373the command through a shell and redirect one of them using the shell 1373the process is running. If BOOL is not given, query before exiting.
1374syntax.
1375 1374
1376usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */) 1375:stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
1376In the stopped state, a process does not accept incoming data, but you
1377can send outgoing data. The stopped state is cleared by
1378`continue-process' and set by `stop-process'.
1379
1380:connection-type TYPE -- TYPE is control type of device used to
1381communicate with subprocesses. Values are `pipe' to use a pipe, `pty'
1382to use a pty, or nil to use the default specified through
1383`process-connection-type'.
1384
1385:filter FILTER -- Install FILTER as the process filter.
1386
1387:sentinel SENTINEL -- Install SENTINEL as the process sentinel.
1388
1389usage: (make-process &rest ARGS) */)
1377 (ptrdiff_t nargs, Lisp_Object *args) 1390 (ptrdiff_t nargs, Lisp_Object *args)
1378{ 1391{
1379 Lisp_Object buffer, name, program, proc, current_dir, tem; 1392 Lisp_Object buffer, name, command, program, proc, contact, current_dir, tem;
1380 unsigned char **new_argv;
1381 ptrdiff_t i;
1382 ptrdiff_t count = SPECPDL_INDEX (); 1393 ptrdiff_t count = SPECPDL_INDEX ();
1394 struct gcpro gcpro1;
1383 USE_SAFE_ALLOCA; 1395 USE_SAFE_ALLOCA;
1384 1396
1385 buffer = args[1]; 1397 if (nargs == 0)
1398 return Qnil;
1399
1400 /* Save arguments for process-contact and clone-process. */
1401 contact = Flist (nargs, args);
1402 GCPRO1 (contact);
1403
1404 buffer = Fplist_get (contact, QCbuffer);
1386 if (!NILP (buffer)) 1405 if (!NILP (buffer))
1387 buffer = Fget_buffer_create (buffer); 1406 buffer = Fget_buffer_create (buffer);
1388 1407
@@ -1402,10 +1421,14 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1402 UNGCPRO; 1421 UNGCPRO;
1403 } 1422 }
1404 1423
1405 name = args[0]; 1424 name = Fplist_get (contact, QCname);
1406 CHECK_STRING (name); 1425 CHECK_STRING (name);
1407 1426
1408 program = args[2]; 1427 command = Fplist_get (contact, QCcommand);
1428 if (CONSP (command))
1429 program = XCAR (command);
1430 else
1431 program = Qnil;
1409 1432
1410 if (!NILP (program)) 1433 if (!NILP (program))
1411 CHECK_STRING (program); 1434 CHECK_STRING (program);
@@ -1423,7 +1446,22 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1423 pset_buffer (XPROCESS (proc), buffer); 1446 pset_buffer (XPROCESS (proc), buffer);
1424 pset_sentinel (XPROCESS (proc), Qinternal_default_process_sentinel); 1447 pset_sentinel (XPROCESS (proc), Qinternal_default_process_sentinel);
1425 pset_filter (XPROCESS (proc), Qinternal_default_process_filter); 1448 pset_filter (XPROCESS (proc), Qinternal_default_process_filter);
1426 pset_command (XPROCESS (proc), Flist (nargs - 2, args + 2)); 1449 pset_command (XPROCESS (proc), Fcopy_sequence (command));
1450
1451 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
1452 XPROCESS (proc)->kill_without_query = 1;
1453 if (tem = Fplist_get (contact, QCstop), !NILP (tem))
1454 pset_command (XPROCESS (proc), Qt);
1455
1456 tem = Fplist_get (contact, QCconnection_type);
1457 if (EQ (tem, Qpty))
1458 XPROCESS (proc)->pty_flag = true;
1459 else if (EQ (tem, Qpipe))
1460 XPROCESS (proc)->pty_flag = false;
1461 else if (NILP (tem))
1462 XPROCESS (proc)->pty_flag = !NILP (Vprocess_connection_type);
1463 else
1464 report_file_error ("Unknown connection type", tem);
1427 1465
1428#ifdef HAVE_GNUTLS 1466#ifdef HAVE_GNUTLS
1429 /* AKA GNUTLS_INITSTAGE(proc). */ 1467 /* AKA GNUTLS_INITSTAGE(proc). */
@@ -1453,15 +1491,29 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1453 Lisp_Object val, *args2; 1491 Lisp_Object val, *args2;
1454 struct gcpro gcpro1, gcpro2; 1492 struct gcpro gcpro1, gcpro2;
1455 1493
1456 val = Vcoding_system_for_read; 1494 tem = Fplist_get (contact, QCcoding);
1495 if (!NILP (tem))
1496 {
1497 val = tem;
1498 if (CONSP (val))
1499 val = XCAR (val);
1500 }
1501 else
1502 val = Vcoding_system_for_read;
1457 if (NILP (val)) 1503 if (NILP (val))
1458 { 1504 {
1459 SAFE_ALLOCA_LISP (args2, nargs + 1); 1505 ptrdiff_t nargs2 = 3 + XINT (Flength (command));
1460 args2[0] = Qstart_process; 1506 Lisp_Object tem2;
1461 for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; 1507 SAFE_ALLOCA_LISP (args2, nargs2);
1508 ptrdiff_t i = 0;
1509 args2[i++] = Qstart_process;
1510 args2[i++] = name;
1511 args2[i++] = buffer;
1512 for (tem2 = command; CONSP (tem2); tem2 = XCDR (tem2))
1513 args2[i++] = XCAR (tem2);
1462 GCPRO2 (proc, current_dir); 1514 GCPRO2 (proc, current_dir);
1463 if (!NILP (program)) 1515 if (!NILP (program))
1464 coding_systems = Ffind_operation_coding_system (nargs + 1, args2); 1516 coding_systems = Ffind_operation_coding_system (nargs2, args2);
1465 UNGCPRO; 1517 UNGCPRO;
1466 if (CONSP (coding_systems)) 1518 if (CONSP (coding_systems))
1467 val = XCAR (coding_systems); 1519 val = XCAR (coding_systems);
@@ -1470,17 +1522,30 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1470 } 1522 }
1471 pset_decode_coding_system (XPROCESS (proc), val); 1523 pset_decode_coding_system (XPROCESS (proc), val);
1472 1524
1473 val = Vcoding_system_for_write; 1525 if (!NILP (tem))
1526 {
1527 val = tem;
1528 if (CONSP (val))
1529 val = XCDR (val);
1530 }
1531 else
1532 val = Vcoding_system_for_write;
1474 if (NILP (val)) 1533 if (NILP (val))
1475 { 1534 {
1476 if (EQ (coding_systems, Qt)) 1535 if (EQ (coding_systems, Qt))
1477 { 1536 {
1478 SAFE_ALLOCA_LISP (args2, nargs + 1); 1537 ptrdiff_t nargs2 = 3 + XINT (Flength (command));
1479 args2[0] = Qstart_process; 1538 Lisp_Object tem2;
1480 for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; 1539 SAFE_ALLOCA_LISP (args2, nargs2);
1540 ptrdiff_t i = 0;
1541 args2[i++] = Qstart_process;
1542 args2[i++] = name;
1543 args2[i++] = buffer;
1544 for (tem2 = command; CONSP (tem2); tem2 = XCDR (tem2))
1545 args2[i++] = XCAR (tem2);
1481 GCPRO2 (proc, current_dir); 1546 GCPRO2 (proc, current_dir);
1482 if (!NILP (program)) 1547 if (!NILP (program))
1483 coding_systems = Ffind_operation_coding_system (nargs + 1, args2); 1548 coding_systems = Ffind_operation_coding_system (nargs2, args2);
1484 UNGCPRO; 1549 UNGCPRO;
1485 } 1550 }
1486 if (CONSP (coding_systems)) 1551 if (CONSP (coding_systems))
@@ -1506,16 +1571,18 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1506 1571
1507 if (!NILP (program)) 1572 if (!NILP (program))
1508 { 1573 {
1574 Lisp_Object program_args = XCDR (command);
1575
1509 /* If program file name is not absolute, search our path for it. 1576 /* If program file name is not absolute, search our path for it.
1510 Put the name we will really use in TEM. */ 1577 Put the name we will really use in TEM. */
1511 if (!IS_DIRECTORY_SEP (SREF (program, 0)) 1578 if (!IS_DIRECTORY_SEP (SREF (program, 0))
1512 && !(SCHARS (program) > 1 1579 && !(SCHARS (program) > 1
1513 && IS_DEVICE_SEP (SREF (program, 1)))) 1580 && IS_DEVICE_SEP (SREF (program, 1))))
1514 { 1581 {
1515 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; 1582 struct gcpro gcpro1, gcpro2;
1516 1583
1517 tem = Qnil; 1584 tem = Qnil;
1518 GCPRO4 (name, program, buffer, current_dir); 1585 GCPRO2 (buffer, current_dir);
1519 openp (Vexec_path, program, Vexec_suffixes, &tem, 1586 openp (Vexec_path, program, Vexec_suffixes, &tem,
1520 make_number (X_OK), false); 1587 make_number (X_OK), false);
1521 UNGCPRO; 1588 UNGCPRO;
@@ -1533,54 +1600,55 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
1533 /* Remove "/:" from TEM. */ 1600 /* Remove "/:" from TEM. */
1534 tem = remove_slash_colon (tem); 1601 tem = remove_slash_colon (tem);
1535 1602
1536 { 1603 Lisp_Object arg_encoding = Qnil;
1537 Lisp_Object arg_encoding = Qnil; 1604 struct gcpro gcpro1;
1538 struct gcpro gcpro1; 1605 GCPRO1 (tem);
1539 GCPRO1 (tem);
1540 1606
1541 /* Encode the file name and put it in NEW_ARGV. 1607 /* Encode the file name and put it in NEW_ARGV.
1542 That's where the child will use it to execute the program. */ 1608 That's where the child will use it to execute the program. */
1543 tem = list1 (ENCODE_FILE (tem)); 1609 tem = list1 (ENCODE_FILE (tem));
1610 ptrdiff_t new_argc = 1;
1544 1611
1545 /* Here we encode arguments by the coding system used for sending 1612 /* Here we encode arguments by the coding system used for sending
1546 data to the process. We don't support using different coding 1613 data to the process. We don't support using different coding
1547 systems for encoding arguments and for encoding data sent to the 1614 systems for encoding arguments and for encoding data sent to the
1548 process. */ 1615 process. */
1549 1616
1550 for (i = 3; i < nargs; i++) 1617 for (Lisp_Object tem2 = program_args; CONSP (tem2); tem2 = XCDR (tem2))
1551 { 1618 {
1552 tem = Fcons (args[i], tem); 1619 Lisp_Object arg = XCAR (tem2);
1553 CHECK_STRING (XCAR (tem)); 1620 CHECK_STRING (arg);
1554 if (STRING_MULTIBYTE (XCAR (tem))) 1621 if (STRING_MULTIBYTE (arg))
1555 { 1622 {
1556 if (NILP (arg_encoding)) 1623 if (NILP (arg_encoding))
1557 arg_encoding = (complement_process_encoding_system 1624 arg_encoding = (complement_process_encoding_system
1558 (XPROCESS (proc)->encode_coding_system)); 1625 (XPROCESS (proc)->encode_coding_system));
1559 XSETCAR (tem, 1626 arg = code_convert_string_norecord (arg, arg_encoding, 1);
1560 code_convert_string_norecord 1627 }
1561 (XCAR (tem), arg_encoding, 1)); 1628 tem = Fcons (arg, tem);
1562 } 1629 new_argc++;
1563 } 1630 }
1564 1631
1565 UNGCPRO; 1632 UNGCPRO;
1566 }
1567 1633
1568 /* Now that everything is encoded we can collect the strings into 1634 /* Now that everything is encoded we can collect the strings into
1569 NEW_ARGV. */ 1635 NEW_ARGV. */
1570 SAFE_NALLOCA (new_argv, 1, nargs - 1); 1636 char **new_argv;
1571 new_argv[nargs - 2] = 0; 1637 SAFE_NALLOCA (new_argv, 1, new_argc + 1);
1638 new_argv[new_argc] = 0;
1572 1639
1573 for (i = nargs - 2; i-- != 0; ) 1640 for (ptrdiff_t i = new_argc - 1; i >= 0; i--)
1574 { 1641 {
1575 new_argv[i] = SDATA (XCAR (tem)); 1642 new_argv[i] = SSDATA (XCAR (tem));
1576 tem = XCDR (tem); 1643 tem = XCDR (tem);
1577 } 1644 }
1578 1645
1579 create_process (proc, (char **) new_argv, current_dir); 1646 create_process (proc, new_argv, current_dir);
1580 } 1647 }
1581 else 1648 else
1582 create_pty (proc); 1649 create_pty (proc);
1583 1650
1651 UNGCPRO;
1584 SAFE_FREE (); 1652 SAFE_FREE ();
1585 return unbind_to (count, proc); 1653 return unbind_to (count, proc);
1586} 1654}
@@ -1648,7 +1716,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1648 1716
1649 inchannel = outchannel = -1; 1717 inchannel = outchannel = -1;
1650 1718
1651 if (!NILP (Vprocess_connection_type)) 1719 if (p->pty_flag)
1652 outchannel = inchannel = allocate_pty (pty_name); 1720 outchannel = inchannel = allocate_pty (pty_name);
1653 1721
1654 if (inchannel >= 0) 1722 if (inchannel >= 0)
@@ -1701,8 +1769,12 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
1701 p->pty_flag = pty_flag; 1769 p->pty_flag = pty_flag;
1702 pset_status (p, Qrun); 1770 pset_status (p, Qrun);
1703 1771
1704 FD_SET (inchannel, &input_wait_mask); 1772 if (!EQ (p->command, Qt))
1705 FD_SET (inchannel, &non_keyboard_wait_mask); 1773 {
1774 FD_SET (inchannel, &input_wait_mask);
1775 FD_SET (inchannel, &non_keyboard_wait_mask);
1776 }
1777
1706 if (inchannel > max_process_desc) 1778 if (inchannel > max_process_desc)
1707 max_process_desc = inchannel; 1779 max_process_desc = inchannel;
1708 1780
@@ -1894,7 +1966,7 @@ create_pty (Lisp_Object process)
1894{ 1966{
1895 struct Lisp_Process *p = XPROCESS (process); 1967 struct Lisp_Process *p = XPROCESS (process);
1896 char pty_name[PTY_NAME_SIZE]; 1968 char pty_name[PTY_NAME_SIZE];
1897 int pty_fd = NILP (Vprocess_connection_type) ? -1 : allocate_pty (pty_name); 1969 int pty_fd = !p->pty_flag ? -1 : allocate_pty (pty_name);
1898 1970
1899 if (pty_fd >= 0) 1971 if (pty_fd >= 0)
1900 { 1972 {
@@ -4525,7 +4597,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
4525 Available = input_wait_mask; 4597 Available = input_wait_mask;
4526 Writeok = write_mask; 4598 Writeok = write_mask;
4527 check_delay = wait_proc ? 0 : process_output_delay_count; 4599 check_delay = wait_proc ? 0 : process_output_delay_count;
4528 check_write = SELECT_CAN_DO_WRITE_MASK; 4600 check_write = true;
4529 } 4601 }
4530 4602
4531 /* If frame size has changed or the window is newly mapped, 4603 /* If frame size has changed or the window is newly mapped,
@@ -5658,9 +5730,10 @@ emacs_get_tty_pgrp (struct Lisp_Process *p)
5658 5730
5659DEFUN ("process-running-child-p", Fprocess_running_child_p, 5731DEFUN ("process-running-child-p", Fprocess_running_child_p,
5660 Sprocess_running_child_p, 0, 1, 0, 5732 Sprocess_running_child_p, 0, 1, 0,
5661 doc: /* Return t if PROCESS has given the terminal to a child. 5733 doc: /* Return non-nil if PROCESS has given the terminal to a
5662If the operating system does not make it possible to find out, 5734child. If the operating system does not make it possible to find out,
5663return t unconditionally. */) 5735return t. If we can find out, return the numeric ID of the foreground
5736process group. */)
5664 (Lisp_Object process) 5737 (Lisp_Object process)
5665{ 5738{
5666 /* Initialize in case ioctl doesn't exist or gives an error, 5739 /* Initialize in case ioctl doesn't exist or gives an error,
@@ -5683,6 +5756,8 @@ return t unconditionally. */)
5683 5756
5684 if (gid == p->pid) 5757 if (gid == p->pid)
5685 return Qnil; 5758 return Qnil;
5759 if (gid != -1)
5760 return make_number (gid);
5686 return Qt; 5761 return Qt;
5687} 5762}
5688 5763
@@ -7269,6 +7344,10 @@ syms_of_process (void)
7269 DEFSYM (QCstop, ":stop"); 7344 DEFSYM (QCstop, ":stop");
7270 DEFSYM (QCoptions, ":options"); 7345 DEFSYM (QCoptions, ":options");
7271 DEFSYM (QCplist, ":plist"); 7346 DEFSYM (QCplist, ":plist");
7347 DEFSYM (QCcommand, ":command");
7348 DEFSYM (QCconnection_type, ":connection-type");
7349 DEFSYM (Qpty, "pty");
7350 DEFSYM (Qpipe, "pipe");
7272 7351
7273 DEFSYM (Qlast_nonmenu_event, "last-nonmenu-event"); 7352 DEFSYM (Qlast_nonmenu_event, "last-nonmenu-event");
7274 7353
@@ -7371,7 +7450,7 @@ The variable takes effect when `start-process' is called. */);
7371 defsubr (&Sprocess_plist); 7450 defsubr (&Sprocess_plist);
7372 defsubr (&Sset_process_plist); 7451 defsubr (&Sset_process_plist);
7373 defsubr (&Sprocess_list); 7452 defsubr (&Sprocess_list);
7374 defsubr (&Sstart_process); 7453 defsubr (&Smake_process);
7375 defsubr (&Sserial_process_configure); 7454 defsubr (&Sserial_process_configure);
7376 defsubr (&Smake_serial_process); 7455 defsubr (&Smake_serial_process);
7377 defsubr (&Sset_network_process_option); 7456 defsubr (&Sset_network_process_option);