comparison src/process.c @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents 56c54cf7c5b6
children 6a378aca36af
comparison
equal deleted inserted replaced
69:804d1389bcd6 70:131b0175ea99
42 #include "lstream.h" 42 #include "lstream.h"
43 #include "opaque.h" 43 #include "opaque.h"
44 #include "process.h" 44 #include "process.h"
45 #include "sysdep.h" 45 #include "sysdep.h"
46 #include "window.h" 46 #include "window.h"
47 #ifdef MULE
48 #include "mule-coding.h"
49 #endif
47 50
48 #include <setjmp.h> 51 #include <setjmp.h>
49 #include "sysfile.h" 52 #include "sysfile.h"
50 #include "sysproc.h" 53 #include "sysproc.h"
51 #include "systime.h" 54 #include "systime.h"
783 Bufbyte eof_char = get_eof_char (p); 786 Bufbyte eof_char = get_eof_char (p);
784 int pty_max_bytes = get_pty_max_bytes (p); 787 int pty_max_bytes = get_pty_max_bytes (p);
785 filedesc_stream_set_pty_flushing (XLSTREAM (p->outstream), 788 filedesc_stream_set_pty_flushing (XLSTREAM (p->outstream),
786 pty_max_bytes, eof_char); 789 pty_max_bytes, eof_char);
787 } 790 }
791 #ifdef MULE
792 p->instream = make_decoding_input_stream
793 (XLSTREAM (p->instream),
794 Fget_coding_system (Vprocess_input_coding_system));
795 Lstream_set_character_mode (XLSTREAM (p->instream));
796 p->outstream = make_encoding_output_stream
797 (XLSTREAM (p->outstream),
798 Fget_coding_system (Vprocess_output_coding_system));
799 /* CODE_CNTL (&out_state[outchannel]) |= CC_END; !!####
800 What's going on here? */
801 #endif /* MULE */
788 } 802 }
789 803
790 static void 804 static void
791 create_process (Lisp_Object process, 805 create_process (Lisp_Object process,
792 char **new_argv, CONST char *current_dir) 806 char **new_argv, CONST char *current_dir)
1021 close_descriptor_pair (forkin, forkout); 1035 close_descriptor_pair (forkin, forkout);
1022 report_file_error ("Doing vfork", Qnil); 1036 report_file_error ("Doing vfork", Qnil);
1023 } 1037 }
1024 1038
1025 p->pid = make_int (pid); 1039 p->pid = make_int (pid);
1026 /* #### dmoore - why is this commented out, otherwise we leave
1027 subtty = forkin, but then we close forkin just below. */
1028 /* p->subtty = -1; */ 1040 /* p->subtty = -1; */
1029 1041
1030 #ifdef WINDOWSNT 1042 #ifdef WINDOWSNT
1031 register_child (pid, inchannel); 1043 register_child (pid, inchannel);
1032 #endif /* WINDOWSNT */ 1044 #endif /* WINDOWSNT */
1094 INCODE and OUTCODE specify the coding-system objects used in input/output 1106 INCODE and OUTCODE specify the coding-system objects used in input/output
1095 from/to the process. 1107 from/to the process.
1096 */ 1108 */
1097 (int nargs, Lisp_Object *args)) 1109 (int nargs, Lisp_Object *args))
1098 { 1110 {
1099 /* This function can call lisp */
1100 /* !!#### This function has not been Mule-ized */ 1111 /* !!#### This function has not been Mule-ized */
1112 /* This function can GC */
1101 Lisp_Object buffer, name, program, proc, current_dir; 1113 Lisp_Object buffer, name, program, proc, current_dir;
1102 Lisp_Object tem; 1114 Lisp_Object tem;
1103 int speccount = specpdl_depth (); 1115 int speccount = specpdl_depth ();
1104 struct gcpro gcpro1, gcpro2, gcpro3;
1105 #ifdef VMS 1116 #ifdef VMS
1106 char *new_argv; 1117 char *new_argv;
1107 int len; 1118 int len;
1108 #else 1119 #else
1109 char **new_argv; 1120 char **new_argv;
1110 #endif 1121 #endif
1111 int i; 1122 int i;
1112 1123
1113 name = args[0];
1114 buffer = args[1]; 1124 buffer = args[1];
1115 program = args[2];
1116 current_dir = Qnil;
1117
1118 /* Protect against various file handlers doing GCs below. */
1119 GCPRO3 (buffer, program, current_dir);
1120
1121 if (!NILP (buffer)) 1125 if (!NILP (buffer))
1122 buffer = Fget_buffer_create (buffer); 1126 buffer = Fget_buffer_create (buffer);
1123 1127
1124 CHECK_STRING (name); 1128 CHECK_STRING (args[0]); /* name */
1125 CHECK_STRING (program); 1129 CHECK_STRING (args[2]); /* program */
1126 1130
1127 /* Make sure that the child will be able to chdir to the current 1131 /* Make sure that the child will be able to chdir to the current
1128 buffer's current directory, or its unhandled equivalent. We 1132 buffer's current directory, or its unhandled equivalent. We
1129 can't just have the child check for an error when it does the 1133 can't just have the child check for an error when it does the
1130 chdir, since it's in a vfork. 1134 chdir, since it's in a vfork.
1131 1135
1132 Note: these assignments and calls are like this in order to insure 1136 We have to GCPRO around this because Fexpand_file_name and
1133 "caller protects args" GC semantics. */ 1137 Funhandled_file_name_directory might call a file name handling
1134 current_dir = current_buffer->directory; 1138 function. The argument list is protected by the caller, so all
1135 current_dir = Funhandled_file_name_directory (current_dir); 1139 we really have to worry about is buffer. */
1136 current_dir = expand_and_dir_to_file (current_dir, Qnil); 1140 {
1137 1141 struct gcpro gcpro1, gcpro2; /* Caller gc-protects args[] */
1142
1143 current_dir = current_buffer->directory;
1144
1145 GCPRO2 (buffer, current_dir);
1146
1147 current_dir =
1148 expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
1149 Qnil);
1138 #if 0 /* This loser breaks ange-ftp */ 1150 #if 0 /* This loser breaks ange-ftp */
1139 /* dmoore - if you re-enable this code, you have to gcprotect 1151 if (NILP (Ffile_accessible_directory_p (current_dir)))
1140 current_buffer through the above calls. */ 1152 report_file_error ("Setting current directory",
1141 if (NILP (Ffile_accessible_directory_p (current_dir))) 1153 list1 (current_buffer->directory));
1142 report_file_error ("Setting current directory",
1143 list1 (current_buffer->directory));
1144 #endif /* 0 */ 1154 #endif /* 0 */
1155
1156 UNGCPRO;
1157 }
1158
1159 name = args[0];
1160 program = args[2];
1145 1161
1146 #ifdef VMS 1162 #ifdef VMS
1147 /* Make a one member argv with all args concatenated 1163 /* Make a one member argv with all args concatenated
1148 together separated by a blank. */ 1164 together separated by a blank. */
1149 len = XSTRING_LENGTH (program) + 2; 1165 len = XSTRING_LENGTH (program) + 2;
1163 strcat (new_argv, XSTRING_DATA (tem)); 1179 strcat (new_argv, XSTRING_DATA (tem));
1164 } 1180 }
1165 /* Need to add code here to check for program existence on VMS */ 1181 /* Need to add code here to check for program existence on VMS */
1166 1182
1167 #else /* not VMS */ 1183 #else /* not VMS */
1184 new_argv = (char **)
1185 alloca ((nargs - 1) * sizeof (char *));
1186
1187 new_argv[0] = (char *) XSTRING_DATA (program);
1188
1168 /* If program file name is not absolute, search our path for it */ 1189 /* If program file name is not absolute, search our path for it */
1169 if (!IS_DIRECTORY_SEP (XSTRING_BYTE (program, 0)) 1190 if (!IS_DIRECTORY_SEP (string_byte (XSTRING (program), 0))
1170 && !(XSTRING_LENGTH (program) > 1 1191 && !(XSTRING_LENGTH (program) > 1
1171 && IS_DEVICE_SEP (XSTRING_BYTE (program, 1)))) 1192 && IS_DEVICE_SEP (string_byte (XSTRING (program), 1))))
1172 { 1193 {
1173 struct gcpro ngcpro1; 1194 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; /* Caller protects args[] */
1174 1195 GCPRO4 (buffer, current_dir, name, program);
1196
1175 tem = Qnil; 1197 tem = Qnil;
1176 NGCPRO1 (tem);
1177 locate_file (Vexec_path, program, EXEC_SUFFIXES, &tem, 1198 locate_file (Vexec_path, program, EXEC_SUFFIXES, &tem,
1178 X_OK); 1199 X_OK);
1200 UNGCPRO;
1179 if (NILP (tem)) 1201 if (NILP (tem))
1180 report_file_error ("Searching for program", list1 (program)); 1202 report_file_error ("Searching for program", list1 (program));
1181 program = Fexpand_file_name (tem, Qnil); 1203 tem = Fexpand_file_name (tem, Qnil);
1182 NUNGCPRO; 1204 new_argv[0] = (char *) XSTRING_DATA (tem);
1183 } 1205 }
1184 else 1206 else
1185 { 1207 {
1186 if (!NILP (Ffile_directory_p (program))) 1208 if (!NILP (Ffile_directory_p (program)))
1187 error ("Specified program for new process is a directory"); 1209 error ("Specified program for new process is a directory");
1188 } 1210 }
1189 1211
1190 /* Nothing below here GCs so our string pointers shouldn't move. */
1191 new_argv = (char **) alloca ((nargs - 1) * sizeof (char *));
1192 new_argv[0] = (char *) XSTRING_DATA (program);
1193 for (i = 3; i < nargs; i++) 1212 for (i = 3; i < nargs; i++)
1194 { 1213 {
1195 tem = args[i]; 1214 tem = args[i];
1196 CHECK_STRING (tem); 1215 CHECK_STRING (tem);
1197 new_argv[i - 2] = (char *) XSTRING_DATA (tem); 1216 new_argv[i - 2] = (char *) XSTRING_DATA (tem);
1217 itself; it's all taken care of here. */ 1236 itself; it's all taken care of here. */
1218 record_unwind_protect (start_process_unwind, proc); 1237 record_unwind_protect (start_process_unwind, proc);
1219 1238
1220 create_process (proc, new_argv, (char *) XSTRING_DATA (current_dir)); 1239 create_process (proc, new_argv, (char *) XSTRING_DATA (current_dir));
1221 1240
1222 UNGCPRO;
1223 return unbind_to (speccount, proc); 1241 return unbind_to (speccount, proc);
1224 } 1242 }
1225 1243
1226 1244
1227 /* connect to an existing file descriptor. This is very similar to 1245 /* connect to an existing file descriptor. This is very similar to
1283 static int 1301 static int
1284 get_internet_address (Lisp_Object host, struct sockaddr_in *address, 1302 get_internet_address (Lisp_Object host, struct sockaddr_in *address,
1285 Error_behavior errb) 1303 Error_behavior errb)
1286 { 1304 {
1287 struct hostent *host_info_ptr; 1305 struct hostent *host_info_ptr;
1288 #ifdef TRY_AGAIN
1289 int count = 0;
1290 #endif
1291 1306
1292 #ifndef HAVE_TERM 1307 #ifndef HAVE_TERM
1293 memset (address, 0, sizeof (*address)); 1308 memset (address, 0, sizeof (*address));
1294 1309
1295 while (1) 1310 while (1)
1296 { 1311 {
1297 #ifdef TRY_AGAIN 1312 #ifdef TRY_AGAIN
1298 if (count++ > 10) break;
1299 h_errno = 0; 1313 h_errno = 0;
1300 #endif 1314 #endif
1301 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */ 1315 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */
1302 slow_down_interrupts (); 1316 slow_down_interrupts ();
1303 host_info_ptr = gethostbyname ((char *) XSTRING_DATA (host)); 1317 host_info_ptr = gethostbyname ((char *) XSTRING_DATA (host));
1343 1357
1344 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 4, 0, /* 1358 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 4, 0, /*
1345 Open a TCP connection for a service to a host. 1359 Open a TCP connection for a service to a host.
1346 Returns a subprocess-object to represent the connection. 1360 Returns a subprocess-object to represent the connection.
1347 Input and output work as for subprocesses; `delete-process' closes it. 1361 Input and output work as for subprocesses; `delete-process' closes it.
1348 Args are NAME BUFFER HOST SERVICE. 1362
1349 NAME is name for process. It is modified if necessary to make it unique. 1363 NAME is name for process. It is modified if necessary to make it unique.
1350 BUFFER is the buffer (or buffer-name) to associate with the process. 1364 BUFFER is the buffer (or buffer-name) to associate with the process.
1351 Process output goes at end of that buffer, unless you specify 1365 Process output goes at end of that buffer, unless you specify
1352 an output stream or filter function to handle the output. 1366 an output stream or filter function to handle the output.
1353 BUFFER may be also nil, meaning that this process is not associated 1367 BUFFER may also be nil, meaning that this process is not associated
1354 with any buffer 1368 with any buffer.
1355 Third arg is name of the host to connect to, or its IP address. 1369 Third arg is name of the host to connect to, or its IP address.
1356 Fourth arg SERVICE is name of the service desired, or an integer 1370 Fourth arg SERVICE is name of the service desired, or an integer
1357 specifying a port number to connect to. 1371 specifying a port number to connect to.
1358 */ 1372 */
1359 (name, buffer, host, service)) 1373 (name, buffer, host, service))
1440 goto loop; 1454 goto loop;
1441 if (errno == EADDRINUSE && retry < 20) 1455 if (errno == EADDRINUSE && retry < 20)
1442 { 1456 {
1443 /* A delay here is needed on some FreeBSD systems, 1457 /* A delay here is needed on some FreeBSD systems,
1444 and it is harmless, since this retrying takes time anyway 1458 and it is harmless, since this retrying takes time anyway
1445 and should be infrequent. 1459 and should be infrequent. */
1446 `sleep-for' allowed for quitting this loop with interrupts 1460 Fsleep_for (make_int (1));
1447 slowed down so it can't be used here. Async timers should
1448 already be disabled at this point so we can use `sleep'. */
1449 sleep (1);
1450 retry++; 1461 retry++;
1451 goto loop; 1462 goto loop;
1452 } 1463 }
1453 1464
1454 close (s); 1465 close (s);
1720 1731
1721 MARK_MODELINE_CHANGED; 1732 MARK_MODELINE_CHANGED;
1722 1733
1723 /* If the restriction isn't what it should be, set it. */ 1734 /* If the restriction isn't what it should be, set it. */
1724 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf)) 1735 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
1725 { 1736 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
1726 Fwiden(p->buffer); 1737 p->buffer);
1727 old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
1728 old_begv,
1729 BUF_Z (buf));
1730 old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
1731 old_zv,
1732 BUF_Z (buf));
1733 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
1734 p->buffer);
1735 }
1736 1738
1737 /* Handling the process output should not deactivate the mark. */ 1739 /* Handling the process output should not deactivate the mark. */
1738 zmacs_region_stays = old_zmacs_region_stays; 1740 zmacs_region_stays = old_zmacs_region_stays;
1739 buf->read_only = old_read_only; 1741 buf->read_only = old_read_only;
1740 old_point = bufpos_clip_to_bounds (BUF_BEGV (buf),
1741 old_point,
1742 BUF_ZV (buf));
1743 BUF_SET_PT (buf, old_point); 1742 BUF_SET_PT (buf, old_point);
1744 1743
1745 UNGCPRO; 1744 UNGCPRO;
1746 } 1745 }
1747 #ifdef VMS 1746 #ifdef VMS
1867 p->tick++; 1866 p->tick++;
1868 process_tick++; 1867 process_tick++;
1869 deactivate_process (proc); 1868 deactivate_process (proc);
1870 #ifdef VMS 1869 #ifdef VMS
1871 error ("Error writing to process %s; closed it", 1870 error ("Error writing to process %s; closed it",
1872 XSTRING_DATA (p->name)); 1871 XSTRING_DATA (p->name));
1873 #else 1872 #else
1874 error ("SIGPIPE raised on process %s; closed it", 1873 error ("SIGPIPE raised on process %s; closed it",
1875 XSTRING_DATA (p->name)); 1874 XSTRING_DATA (p->name));
1876 #endif 1875 #endif
1877 } 1876 }
1878 Lstream_flush (XLSTREAM (p->outstream)); 1877 Lstream_flush (XLSTREAM (p->outstream));
1879 UNGCPRO; 1878 UNGCPRO;
1880 } 1879 }
2007 len = bto - bfr; 2006 len = bto - bfr;
2008 2007
2009 send_process (proc, string, 0, bfr, len); 2008 send_process (proc, string, 0, bfr, len);
2010 return (Qnil); 2009 return (Qnil);
2011 } 2010 }
2011
2012 #ifdef MULE
2013
2014 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
2015 Return PROCESS's input coding system.
2016 */
2017 (process))
2018 {
2019 process = get_process (process);
2020 return decoding_stream_coding_system (XLSTREAM ( XPROCESS (process)->instream) );
2021 }
2022
2023 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
2024 Return PROCESS's output coding system.
2025 */
2026 (process))
2027 {
2028 process = get_process (process);
2029 return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->outstream));
2030 }
2031
2032 DEFUN ("set-process-input-coding-system",
2033 Fset_process_input_coding_system, 2, 2, 0, /*
2034 Set PROCESS's input coding system to CODESYS.
2035 */
2036 (process, codesys))
2037 {
2038 codesys = Fget_coding_system (codesys);
2039 process = get_process (process);
2040 set_decoding_stream_coding_system ( XLSTREAM ( XPROCESS (process)->instream ), codesys);
2041 return Qnil;
2042 }
2043
2044 DEFUN ("set-process-output-coding-system",
2045 Fset_process_output_coding_system, 2, 2, 0, /*
2046 Set PROCESS's output coding system to CODESYS.
2047 */
2048 (process, codesys))
2049 {
2050 codesys = Fget_coding_system (codesys);
2051 process = get_process (process);
2052 set_encoding_stream_coding_system
2053 ( XLSTREAM ( XPROCESS (process)->outstream), codesys);
2054 return Qnil;
2055 }
2056
2057 #endif /* MULE */
2012 2058
2013 2059
2014 /************************************************************************/ 2060 /************************************************************************/
2015 /* process status */ 2061 /* process status */
2016 /************************************************************************/ 2062 /************************************************************************/
2158 reap_exited_processes (void) 2204 reap_exited_processes (void)
2159 { 2205 {
2160 int i; 2206 int i;
2161 struct Lisp_Process *p; 2207 struct Lisp_Process *p;
2162 2208
2163 if (exited_processes_index <= 0)
2164 return;
2165 EMACS_BLOCK_SIGNAL (SIGCHLD); 2209 EMACS_BLOCK_SIGNAL (SIGCHLD);
2166 for (i = 0; i < exited_processes_index; i++) 2210 for (i = 0; i < exited_processes_index; i++)
2167 { 2211 {
2168 int pid = exited_processes[i]; 2212 int pid = exited_processes[i];
2169 WAITTYPE w = exited_processes_status[i]; 2213 WAITTYPE w = exited_processes_status[i];
2240 race conditions with the SIGCHLD_HAPPENED flag). */ 2284 race conditions with the SIGCHLD_HAPPENED flag). */
2241 2285
2242 static void 2286 static void
2243 record_exited_processes (int block_sigchld) 2287 record_exited_processes (int block_sigchld)
2244 { 2288 {
2245 if (!sigchld_happened)
2246 return;
2247 if (block_sigchld) 2289 if (block_sigchld)
2248 EMACS_BLOCK_SIGNAL (SIGCHLD); 2290 EMACS_BLOCK_SIGNAL (SIGCHLD);
2249 2291
2250 while (sigchld_happened) 2292 while (sigchld_happened)
2251 { 2293 {
2512 Finsert (1, &msg); 2554 Finsert (1, &msg);
2513 current_buffer->read_only = old_read_only; 2555 current_buffer->read_only = old_read_only;
2514 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)), 2556 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
2515 p->buffer); 2557 p->buffer);
2516 2558
2517 opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
2518 opoint,
2519 BUF_ZV (XBUFFER (p->buffer)));
2520 BUF_SET_PT (current_buffer, opoint); 2559 BUF_SET_PT (current_buffer, opoint);
2521 Fset_buffer (old); 2560 Fset_buffer (old);
2522 NUNGCPRO; 2561 NUNGCPRO;
2523 } 2562 }
2524 } 2563 }
2679 int gid; 2718 int gid;
2680 int no_pgrp = 0; 2719 int no_pgrp = 0;
2681 2720
2682 if (network_connection_p (proc)) 2721 if (network_connection_p (proc))
2683 error ("Network connection %s is not a subprocess", 2722 error ("Network connection %s is not a subprocess",
2684 XSTRING_DATA (p->name)); 2723 XSTRING_DATA (p->name));
2685 if (p->infd < 0) 2724 if (p->infd < 0)
2686 error ("Process %s is not active", 2725 error ("Process %s is not active",
2687 XSTRING_DATA (p->name)); 2726 XSTRING_DATA (p->name));
2688 2727
2689 if (!p->pty_flag) 2728 if (!p->pty_flag)
2690 current_group = 0; 2729 current_group = 0;
2691 2730
2692 /* If we are using pgrps, get a pgrp number and make it negative. */ 2731 /* If we are using pgrps, get a pgrp number and make it negative. */
3282 DEFSUBR (Fstop_process); 3321 DEFSUBR (Fstop_process);
3283 DEFSUBR (Fcontinue_process); 3322 DEFSUBR (Fcontinue_process);
3284 DEFSUBR (Fprocess_send_eof); 3323 DEFSUBR (Fprocess_send_eof);
3285 DEFSUBR (Fsignal_process); 3324 DEFSUBR (Fsignal_process);
3286 /* DEFSUBR (Fprocess_connection); */ 3325 /* DEFSUBR (Fprocess_connection); */
3326 #ifdef MULE
3327 DEFSUBR (Fprocess_input_coding_system);
3328 DEFSUBR (Fprocess_output_coding_system);
3329 DEFSUBR (Fset_process_input_coding_system);
3330 DEFSUBR (Fset_process_output_coding_system);
3331 #endif /* MULE */
3287 } 3332 }
3288 3333
3289 void 3334 void
3290 vars_of_process (void) 3335 vars_of_process (void)
3291 { 3336 {