Commit | Line | Data |
---|---|---|
80856e74 | 1 | /* Synchronous subprocess invocation for GNU Emacs. |
fe12847a | 2 | Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001, |
8cabe764 GM |
3 | 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
4 | Free Software Foundation, Inc. | |
80856e74 JB |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
8 | GNU Emacs is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
684d6f5b | 10 | the Free Software Foundation; either version 3, or (at your option) |
80856e74 JB |
11 | any later version. |
12 | ||
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU Emacs; see the file COPYING. If not, write to | |
4fc5845f LK |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
80856e74 JB |
22 | |
23 | ||
68c45bf0 | 24 | #include <config.h> |
80856e74 | 25 | #include <signal.h> |
e576cab4 | 26 | #include <errno.h> |
565620a5 | 27 | #include <stdio.h> |
80856e74 | 28 | |
03695ace | 29 | #ifndef USE_CRT_DLL |
426b37ae | 30 | extern int errno; |
03695ace | 31 | #endif |
426b37ae | 32 | |
80856e74 JB |
33 | /* Define SIGCHLD as an alias for SIGCLD. */ |
34 | ||
35 | #if !defined (SIGCHLD) && defined (SIGCLD) | |
36 | #define SIGCHLD SIGCLD | |
37 | #endif /* SIGCLD */ | |
38 | ||
39 | #include <sys/types.h> | |
88a64fef | 40 | |
3cbd6585 GM |
41 | #ifdef HAVE_UNISTD_H |
42 | #include <unistd.h> | |
43 | #endif | |
44 | ||
80856e74 | 45 | #include <sys/file.h> |
776a24a1 | 46 | #ifdef HAVE_FCNTL_H |
472e83fe | 47 | #define INCLUDED_FCNTL |
80856e74 JB |
48 | #include <fcntl.h> |
49 | #endif | |
50 | ||
bad95d8f RS |
51 | #ifdef WINDOWSNT |
52 | #define NOMINMAX | |
53 | #include <windows.h> | |
54 | #include <stdlib.h> /* for proper declaration of environ */ | |
55 | #include <fcntl.h> | |
489f9371 | 56 | #include "w32.h" |
bad95d8f RS |
57 | #define _P_NOWAIT 1 /* from process.h */ |
58 | #endif | |
59 | ||
7e6c2178 | 60 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
472e83fe | 61 | #define INCLUDED_FCNTL |
7e6c2178 RS |
62 | #include <fcntl.h> |
63 | #include <sys/stat.h> | |
64 | #include <sys/param.h> | |
65 | #include <errno.h> | |
66 | #endif /* MSDOS */ | |
67 | ||
80856e74 JB |
68 | #ifndef O_RDONLY |
69 | #define O_RDONLY 0 | |
70 | #endif | |
71 | ||
72 | #ifndef O_WRONLY | |
73 | #define O_WRONLY 1 | |
74 | #endif | |
75 | ||
76 | #include "lisp.h" | |
77 | #include "commands.h" | |
78 | #include "buffer.h" | |
32d08644 | 79 | #include "charset.h" |
edf496dd | 80 | #include "ccl.h" |
32d08644 | 81 | #include "coding.h" |
f0b950cf | 82 | #include "composite.h" |
57bda87a | 83 | #include <epaths.h> |
80856e74 | 84 | #include "process.h" |
d177f194 | 85 | #include "syssignal.h" |
a129418f | 86 | #include "systty.h" |
aba637ec | 87 | #include "blockinput.h" |
f105f403 KL |
88 | #include "frame.h" |
89 | #include "termhooks.h" | |
80856e74 | 90 | |
5f027cea EZ |
91 | #ifdef MSDOS |
92 | #include "msdos.h" | |
93 | #endif | |
94 | ||
80856e74 JB |
95 | #ifdef VMS |
96 | extern noshare char **environ; | |
97 | #else | |
03695ace | 98 | #ifndef USE_CRT_DLL |
80856e74 JB |
99 | extern char **environ; |
100 | #endif | |
03695ace | 101 | #endif |
80856e74 | 102 | |
f95c3f91 | 103 | #ifdef HAVE_SETPGID |
2b7e8799 | 104 | #if !defined (USG) || defined (BSD_PGRPS) |
320695d8 | 105 | #undef setpgrp |
f95c3f91 GM |
106 | #define setpgrp setpgid |
107 | #endif | |
2b7e8799 | 108 | #endif |
f95c3f91 | 109 | |
b81a1b72 SM |
110 | Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes; |
111 | Lisp_Object Vdata_directory, Vdoc_directory; | |
1e7ce61b | 112 | Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory; |
8abd035b | 113 | Lisp_Object Vtemp_file_name_pattern; |
80856e74 JB |
114 | |
115 | Lisp_Object Vshell_file_name; | |
116 | ||
6b8e474c | 117 | Lisp_Object Vprocess_environment, Vinitial_environment; |
80856e74 | 118 | |
bad95d8f | 119 | #ifdef DOS_NT |
093650fe | 120 | Lisp_Object Qbuffer_file_type; |
bad95d8f | 121 | #endif /* DOS_NT */ |
093650fe | 122 | |
e0f24100 | 123 | /* True if we are about to fork off a synchronous process or if we |
80856e74 JB |
124 | are waiting for it. */ |
125 | int synch_process_alive; | |
126 | ||
127 | /* Nonzero => this is a string explaining death of synchronous subprocess. */ | |
128 | char *synch_process_death; | |
129 | ||
ca498128 JD |
130 | /* Nonzero => this is the signal number that terminated the subprocess. */ |
131 | int synch_process_termsig; | |
132 | ||
80856e74 JB |
133 | /* If synch_process_death is zero, |
134 | this is exit code of synchronous subprocess. */ | |
135 | int synch_process_retcode; | |
f105f403 | 136 | |
80856e74 | 137 | \f |
37d54121 RS |
138 | /* Clean up when exiting Fcall_process. |
139 | On MSDOS, delete the temporary file on any kind of termination. | |
140 | On Unix, kill the process and any children on termination by signal. */ | |
141 | ||
142 | /* Nonzero if this is termination due to exit. */ | |
143 | static int call_process_exited; | |
144 | ||
d2bb6598 SM |
145 | EXFUN (Fgetenv_internal, 2); |
146 | ||
80856e74 JB |
147 | #ifndef VMS /* VMS version is in vmsproc.c. */ |
148 | ||
d177f194 JB |
149 | static Lisp_Object |
150 | call_process_kill (fdpid) | |
151 | Lisp_Object fdpid; | |
152 | { | |
68c45bf0 | 153 | emacs_close (XFASTINT (Fcar (fdpid))); |
d177f194 JB |
154 | EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL); |
155 | synch_process_alive = 0; | |
156 | return Qnil; | |
157 | } | |
158 | ||
80856e74 JB |
159 | Lisp_Object |
160 | call_process_cleanup (fdpid) | |
161 | Lisp_Object fdpid; | |
162 | { | |
e0f712ba | 163 | #if defined (MSDOS) || defined (MAC_OS8) |
7e6c2178 | 164 | /* for MSDOS fdpid is really (fd . tempfile) */ |
c1350752 KH |
165 | register Lisp_Object file; |
166 | file = Fcdr (fdpid); | |
68c45bf0 | 167 | emacs_close (XFASTINT (Fcar (fdpid))); |
d5db4077 KR |
168 | if (strcmp (SDATA (file), NULL_DEVICE) != 0) |
169 | unlink (SDATA (file)); | |
e0f712ba | 170 | #else /* not MSDOS and not MAC_OS8 */ |
d177f194 JB |
171 | register int pid = XFASTINT (Fcdr (fdpid)); |
172 | ||
37d54121 | 173 | if (call_process_exited) |
6b6e798b | 174 | { |
68c45bf0 | 175 | emacs_close (XFASTINT (Fcar (fdpid))); |
6b6e798b RS |
176 | return Qnil; |
177 | } | |
37d54121 | 178 | |
d177f194 JB |
179 | if (EMACS_KILLPG (pid, SIGINT) == 0) |
180 | { | |
aed13378 | 181 | int count = SPECPDL_INDEX (); |
d177f194 JB |
182 | record_unwind_protect (call_process_kill, fdpid); |
183 | message1 ("Waiting for process to die...(type C-g again to kill it instantly)"); | |
184 | immediate_quit = 1; | |
185 | QUIT; | |
186 | wait_for_termination (pid); | |
187 | immediate_quit = 0; | |
188 | specpdl_ptr = specpdl + count; /* Discard the unwind protect. */ | |
189 | message1 ("Waiting for process to die...done"); | |
190 | } | |
80856e74 | 191 | synch_process_alive = 0; |
68c45bf0 | 192 | emacs_close (XFASTINT (Fcar (fdpid))); |
7e6c2178 | 193 | #endif /* not MSDOS */ |
80856e74 JB |
194 | return Qnil; |
195 | } | |
196 | ||
197 | DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0, | |
fdb82f93 PJ |
198 | doc: /* Call PROGRAM synchronously in separate process. |
199 | The remaining arguments are optional. | |
200 | The program's input comes from file INFILE (nil means `/dev/null'). | |
201 | Insert output in BUFFER before point; t means current buffer; | |
202 | nil for BUFFER means discard it; 0 means discard and don't wait. | |
203 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
204 | REAL-BUFFER says what to do with standard output, as above, | |
205 | while STDERR-FILE says what to do with standard error in the child. | |
206 | STDERR-FILE may be nil (discard standard error output), | |
207 | t (mix it with ordinary output), or a file name string. | |
208 | ||
209 | Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted. | |
210 | Remaining arguments are strings passed as command arguments to PROGRAM. | |
211 | ||
a4feb144 RS |
212 | If executable PROGRAM can't be found as an executable, `call-process' |
213 | signals a Lisp error. `call-process' reports errors in execution of | |
214 | the program only through its return and output. | |
215 | ||
fdb82f93 PJ |
216 | If BUFFER is 0, `call-process' returns immediately with value nil. |
217 | Otherwise it waits for PROGRAM to terminate | |
218 | and returns a numeric exit status or a signal description string. | |
d98b59b5 MB |
219 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again. |
220 | ||
221 | usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */) | |
fdb82f93 | 222 | (nargs, args) |
80856e74 JB |
223 | int nargs; |
224 | register Lisp_Object *args; | |
225 | { | |
0aa2630f KS |
226 | Lisp_Object infile, buffer, current_dir, path; |
227 | int display_p; | |
80856e74 JB |
228 | int fd[2]; |
229 | int filefd; | |
230 | register int pid; | |
4da256b1 KS |
231 | #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024) |
232 | #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN) | |
233 | char buf[CALLPROC_BUFFER_SIZE_MAX]; | |
234 | int bufsize = CALLPROC_BUFFER_SIZE_MIN; | |
aed13378 | 235 | int count = SPECPDL_INDEX (); |
2d607244 | 236 | |
4d3b07fd KR |
237 | register const unsigned char **new_argv |
238 | = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); | |
80856e74 | 239 | struct buffer *old = current_buffer; |
39eaa782 RS |
240 | /* File to use for stderr in the child. |
241 | t means use same as standard output. */ | |
242 | Lisp_Object error_file; | |
7e6c2178 RS |
243 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
244 | char *outf, *tempfile; | |
245 | int outfilefd; | |
246 | #endif | |
e0f712ba | 247 | #ifdef MAC_OS8 |
052062e0 RS |
248 | char *tempfile; |
249 | int outfilefd; | |
250 | #endif | |
80856e74 JB |
251 | #if 0 |
252 | int mask; | |
253 | #endif | |
32d08644 KH |
254 | struct coding_system process_coding; /* coding-system of process output */ |
255 | struct coding_system argument_coding; /* coding-system of arguments */ | |
09494912 RS |
256 | /* Set to the return value of Ffind_operation_coding_system. */ |
257 | Lisp_Object coding_systems; | |
258 | ||
259 | /* Qt denotes that Ffind_operation_coding_system is not yet called. */ | |
260 | coding_systems = Qt; | |
32d08644 | 261 | |
b7826503 | 262 | CHECK_STRING (args[0]); |
80856e74 | 263 | |
39eaa782 RS |
264 | error_file = Qt; |
265 | ||
7e6c2178 RS |
266 | #ifndef subprocesses |
267 | /* Without asynchronous processes we cannot have BUFFER == 0. */ | |
177c0ea7 | 268 | if (nargs >= 3 |
09ffb8b5 | 269 | && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2]))) |
7e6c2178 RS |
270 | error ("Operating system cannot handle asynchronous subprocesses"); |
271 | #endif /* subprocesses */ | |
272 | ||
09494912 | 273 | /* Decide the coding-system for giving arguments. */ |
32d08644 KH |
274 | { |
275 | Lisp_Object val, *args2; | |
32d08644 KH |
276 | int i; |
277 | ||
278 | /* If arguments are supplied, we may have to encode them. */ | |
279 | if (nargs >= 5) | |
280 | { | |
30d57b8e RS |
281 | int must_encode = 0; |
282 | ||
e7c1c20e | 283 | for (i = 4; i < nargs; i++) |
b7826503 | 284 | CHECK_STRING (args[i]); |
e7c1c20e | 285 | |
a2286b5c | 286 | for (i = 4; i < nargs; i++) |
30d57b8e RS |
287 | if (STRING_MULTIBYTE (args[i])) |
288 | must_encode = 1; | |
289 | ||
beacaab3 KH |
290 | if (!NILP (Vcoding_system_for_write)) |
291 | val = Vcoding_system_for_write; | |
30d57b8e | 292 | else if (! must_encode) |
beacaab3 KH |
293 | val = Qnil; |
294 | else | |
32d08644 KH |
295 | { |
296 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); | |
297 | args2[0] = Qcall_process; | |
298 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
08ee4e87 | 299 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); |
776b95cb | 300 | if (CONSP (coding_systems)) |
70949dac | 301 | val = XCDR (coding_systems); |
776b95cb | 302 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 303 | val = XCDR (Vdefault_process_coding_system); |
beacaab3 KH |
304 | else |
305 | val = Qnil; | |
32d08644 KH |
306 | } |
307 | setup_coding_system (Fcheck_coding_system (val), &argument_coding); | |
8acb7dad KH |
308 | if (argument_coding.common_flags & CODING_ASCII_INCOMPATIBLE_MASK) |
309 | setup_coding_system (Qraw_text, &argument_coding); | |
76a25e79 KH |
310 | if (argument_coding.eol_type == CODING_EOL_UNDECIDED) |
311 | argument_coding.eol_type = system_eol_type; | |
32d08644 | 312 | } |
32d08644 KH |
313 | } |
314 | ||
e576cab4 JB |
315 | if (nargs >= 2 && ! NILP (args[1])) |
316 | { | |
317 | infile = Fexpand_file_name (args[1], current_buffer->directory); | |
b7826503 | 318 | CHECK_STRING (infile); |
e576cab4 | 319 | } |
80856e74 | 320 | else |
5437e9f9 | 321 | infile = build_string (NULL_DEVICE); |
80856e74 | 322 | |
e576cab4 JB |
323 | if (nargs >= 3) |
324 | { | |
39eaa782 RS |
325 | buffer = args[2]; |
326 | ||
327 | /* If BUFFER is a list, its meaning is | |
328 | (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */ | |
329 | if (CONSP (buffer)) | |
330 | { | |
70949dac | 331 | if (CONSP (XCDR (buffer))) |
45be8a1e | 332 | { |
a9d4f28a | 333 | Lisp_Object stderr_file; |
70949dac | 334 | stderr_file = XCAR (XCDR (buffer)); |
45be8a1e RS |
335 | |
336 | if (NILP (stderr_file) || EQ (Qt, stderr_file)) | |
337 | error_file = stderr_file; | |
338 | else | |
339 | error_file = Fexpand_file_name (stderr_file, Qnil); | |
340 | } | |
341 | ||
70949dac | 342 | buffer = XCAR (buffer); |
39eaa782 | 343 | } |
044512ed | 344 | |
39eaa782 RS |
345 | if (!(EQ (buffer, Qnil) |
346 | || EQ (buffer, Qt) | |
3ffde7d6 | 347 | || INTEGERP (buffer))) |
e576cab4 | 348 | { |
39eaa782 RS |
349 | Lisp_Object spec_buffer; |
350 | spec_buffer = buffer; | |
50fe359b | 351 | buffer = Fget_buffer_create (buffer); |
39eaa782 RS |
352 | /* Mention the buffer name for a better error message. */ |
353 | if (NILP (buffer)) | |
b7826503 PJ |
354 | CHECK_BUFFER (spec_buffer); |
355 | CHECK_BUFFER (buffer); | |
e576cab4 JB |
356 | } |
357 | } | |
177c0ea7 | 358 | else |
e576cab4 | 359 | buffer = Qnil; |
80856e74 | 360 | |
58616e67 JB |
361 | /* Make sure that the child will be able to chdir to the current |
362 | buffer's current directory, or its unhandled equivalent. We | |
363 | can't just have the child check for an error when it does the | |
364 | chdir, since it's in a vfork. | |
365 | ||
366 | We have to GCPRO around this because Fexpand_file_name, | |
367 | Funhandled_file_name_directory, and Ffile_accessible_directory_p | |
368 | might call a file name handling function. The argument list is | |
369 | protected by the caller, so all we really have to worry about is | |
370 | buffer. */ | |
371 | { | |
34b87689 | 372 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; |
58616e67 JB |
373 | |
374 | current_dir = current_buffer->directory; | |
375 | ||
34b87689 | 376 | GCPRO4 (infile, buffer, current_dir, error_file); |
58616e67 | 377 | |
c52b0b34 KH |
378 | current_dir |
379 | = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir), | |
380 | Qnil); | |
58616e67 JB |
381 | if (NILP (Ffile_accessible_directory_p (current_dir))) |
382 | report_file_error ("Setting current directory", | |
383 | Fcons (current_buffer->directory, Qnil)); | |
384 | ||
34b87689 KH |
385 | if (STRING_MULTIBYTE (infile)) |
386 | infile = ENCODE_FILE (infile); | |
387 | if (STRING_MULTIBYTE (current_dir)) | |
388 | current_dir = ENCODE_FILE (current_dir); | |
389 | if (STRINGP (error_file) && STRING_MULTIBYTE (error_file)) | |
390 | error_file = ENCODE_FILE (error_file); | |
58616e67 JB |
391 | UNGCPRO; |
392 | } | |
393 | ||
0aa2630f | 394 | display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]); |
80856e74 | 395 | |
d5db4077 | 396 | filefd = emacs_open (SDATA (infile), O_RDONLY, 0); |
80856e74 JB |
397 | if (filefd < 0) |
398 | { | |
34b87689 | 399 | infile = DECODE_FILE (infile); |
e576cab4 | 400 | report_file_error ("Opening process input file", Fcons (infile, Qnil)); |
80856e74 JB |
401 | } |
402 | /* Search for program; barf if not found. */ | |
c52b0b34 KH |
403 | { |
404 | struct gcpro gcpro1; | |
405 | ||
406 | GCPRO1 (current_dir); | |
5c150961 | 407 | openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK)); |
c52b0b34 KH |
408 | UNGCPRO; |
409 | } | |
012c6fcb | 410 | if (NILP (path)) |
80856e74 | 411 | { |
68c45bf0 | 412 | emacs_close (filefd); |
80856e74 JB |
413 | report_file_error ("Searching for program", Fcons (args[0], Qnil)); |
414 | } | |
8ee8f447 RS |
415 | |
416 | /* If program file name starts with /: for quoting a magic name, | |
417 | discard that. */ | |
418 | if (SBYTES (path) > 2 && SREF (path, 0) == '/' | |
419 | && SREF (path, 1) == ':') | |
420 | path = Fsubstring (path, make_number (2), Qnil); | |
421 | ||
d5db4077 | 422 | new_argv[0] = SDATA (path); |
c364e618 KH |
423 | if (nargs > 4) |
424 | { | |
425 | register int i; | |
c5bfa12b | 426 | struct gcpro gcpro1, gcpro2, gcpro3; |
c364e618 | 427 | |
c5bfa12b KH |
428 | GCPRO3 (infile, buffer, current_dir); |
429 | argument_coding.dst_multibyte = 0; | |
430 | for (i = 4; i < nargs; i++) | |
c364e618 | 431 | { |
c5bfa12b KH |
432 | argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]); |
433 | if (CODING_REQUIRE_ENCODING (&argument_coding)) | |
c364e618 | 434 | { |
c5bfa12b KH |
435 | /* We must encode this argument. */ |
436 | args[i] = encode_coding_string (args[i], &argument_coding, 1); | |
437 | if (argument_coding.type == coding_type_ccl) | |
438 | setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil); | |
c364e618 | 439 | } |
d5db4077 | 440 | new_argv[i - 3] = SDATA (args[i]); |
c364e618 | 441 | } |
c5bfa12b | 442 | UNGCPRO; |
db54baaa | 443 | new_argv[nargs - 3] = 0; |
c364e618 | 444 | } |
db54baaa KH |
445 | else |
446 | new_argv[1] = 0; | |
80856e74 | 447 | |
7e6c2178 | 448 | #ifdef MSDOS /* MW, July 1993 */ |
8a52365c | 449 | if ((outf = egetenv ("TMPDIR"))) |
7e6c2178 RS |
450 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); |
451 | else | |
452 | { | |
453 | tempfile = alloca (20); | |
454 | *tempfile = '\0'; | |
455 | } | |
456 | dostounix_filename (tempfile); | |
177c0ea7 | 457 | if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/') |
7e6c2178 RS |
458 | strcat (tempfile, "/"); |
459 | strcat (tempfile, "detmp.XXX"); | |
460 | mktemp (tempfile); | |
461 | ||
462 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); | |
463 | if (outfilefd < 0) | |
464 | { | |
68c45bf0 | 465 | emacs_close (filefd); |
6f89d28a MB |
466 | report_file_error ("Opening process output file", |
467 | Fcons (build_string (tempfile), Qnil)); | |
7e6c2178 | 468 | } |
6f89d28a | 469 | fd[0] = filefd; |
2610078a | 470 | fd[1] = outfilefd; |
6f89d28a | 471 | #endif /* MSDOS */ |
7e6c2178 | 472 | |
e0f712ba | 473 | #ifdef MAC_OS8 |
052062e0 RS |
474 | /* Since we don't have pipes on the Mac, create a temporary file to |
475 | hold the output of the subprocess. */ | |
d5db4077 KR |
476 | tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1); |
477 | bcopy (SDATA (Vtemp_file_name_pattern), tempfile, | |
478 | SBYTES (Vtemp_file_name_pattern) + 1); | |
052062e0 RS |
479 | |
480 | mktemp (tempfile); | |
481 | ||
482 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); | |
483 | if (outfilefd < 0) | |
484 | { | |
485 | close (filefd); | |
486 | report_file_error ("Opening process output file", | |
487 | Fcons (build_string (tempfile), Qnil)); | |
488 | } | |
489 | fd[0] = filefd; | |
490 | fd[1] = outfilefd; | |
e0f712ba | 491 | #endif /* MAC_OS8 */ |
052062e0 | 492 | |
d50d3dc8 | 493 | if (INTEGERP (buffer)) |
68c45bf0 | 494 | fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1; |
80856e74 JB |
495 | else |
496 | { | |
7e6c2178 | 497 | #ifndef MSDOS |
e0f712ba | 498 | #ifndef MAC_OS8 |
db92b288 GM |
499 | errno = 0; |
500 | if (pipe (fd) == -1) | |
501 | { | |
502 | emacs_close (filefd); | |
503 | report_file_error ("Creating process pipe", Qnil); | |
504 | } | |
7e6c2178 | 505 | #endif |
052062e0 | 506 | #endif |
80856e74 JB |
507 | #if 0 |
508 | /* Replaced by close_process_descs */ | |
509 | set_exclusive_use (fd[0]); | |
510 | #endif | |
511 | } | |
512 | ||
513 | { | |
514 | /* child_setup must clobber environ in systems with true vfork. | |
515 | Protect it from permanent change. */ | |
516 | register char **save_environ = environ; | |
517 | register int fd1 = fd[1]; | |
39eaa782 | 518 | int fd_error = fd1; |
80856e74 JB |
519 | |
520 | #if 0 /* Some systems don't have sigblock. */ | |
e065a56e | 521 | mask = sigblock (sigmask (SIGCHLD)); |
80856e74 JB |
522 | #endif |
523 | ||
524 | /* Record that we're about to create a synchronous process. */ | |
525 | synch_process_alive = 1; | |
526 | ||
5c03767e RS |
527 | /* These vars record information from process termination. |
528 | Clear them now before process can possibly terminate, | |
529 | to avoid timing error if process terminates soon. */ | |
530 | synch_process_death = 0; | |
531 | synch_process_retcode = 0; | |
ca498128 | 532 | synch_process_termsig = 0; |
5c03767e | 533 | |
39eaa782 | 534 | if (NILP (error_file)) |
68c45bf0 | 535 | fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0); |
39eaa782 RS |
536 | else if (STRINGP (error_file)) |
537 | { | |
538 | #ifdef DOS_NT | |
d5db4077 | 539 | fd_error = emacs_open (SDATA (error_file), |
68c45bf0 PE |
540 | O_WRONLY | O_TRUNC | O_CREAT | O_TEXT, |
541 | S_IREAD | S_IWRITE); | |
39eaa782 | 542 | #else /* not DOS_NT */ |
d5db4077 | 543 | fd_error = creat (SDATA (error_file), 0666); |
39eaa782 RS |
544 | #endif /* not DOS_NT */ |
545 | } | |
546 | ||
547 | if (fd_error < 0) | |
548 | { | |
68c45bf0 | 549 | emacs_close (filefd); |
6f89d28a | 550 | if (fd[0] != filefd) |
68c45bf0 | 551 | emacs_close (fd[0]); |
39eaa782 | 552 | if (fd1 >= 0) |
68c45bf0 | 553 | emacs_close (fd1); |
6f89d28a MB |
554 | #ifdef MSDOS |
555 | unlink (tempfile); | |
556 | #endif | |
34b87689 KH |
557 | if (NILP (error_file)) |
558 | error_file = build_string (NULL_DEVICE); | |
559 | else if (STRINGP (error_file)) | |
560 | error_file = DECODE_FILE (error_file); | |
561 | report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil)); | |
39eaa782 | 562 | } |
89e1ec1d | 563 | |
e0f712ba | 564 | #ifdef MAC_OS8 |
052062e0 RS |
565 | { |
566 | /* Call run_mac_command in sysdep.c here directly instead of doing | |
567 | a child_setup as for MSDOS and other platforms. Note that this | |
568 | code does not handle passing the environment to the synchronous | |
569 | Mac subprocess. */ | |
570 | char *infn, *outfn, *errfn, *currdn; | |
177c0ea7 | 571 | |
052062e0 RS |
572 | /* close these files so subprocess can write to them */ |
573 | close (outfilefd); | |
574 | if (fd_error != outfilefd) | |
575 | close (fd_error); | |
576 | fd1 = -1; /* No harm in closing that one! */ | |
577 | ||
d5db4077 | 578 | infn = SDATA (infile); |
052062e0 RS |
579 | outfn = tempfile; |
580 | if (NILP (error_file)) | |
581 | errfn = NULL_DEVICE; | |
582 | else if (EQ (Qt, error_file)) | |
583 | errfn = outfn; | |
584 | else | |
d5db4077 KR |
585 | errfn = SDATA (error_file); |
586 | currdn = SDATA (current_dir); | |
052062e0 RS |
587 | pid = run_mac_command (new_argv, currdn, infn, outfn, errfn); |
588 | ||
589 | /* Record that the synchronous process exited and note its | |
590 | termination status. */ | |
591 | synch_process_alive = 0; | |
592 | synch_process_retcode = pid; | |
593 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ | |
68c45bf0 | 594 | { |
ca9c0567 | 595 | synchronize_system_messages_locale (); |
68c45bf0 PE |
596 | synch_process_death = strerror (errno); |
597 | } | |
052062e0 RS |
598 | |
599 | /* Since CRLF is converted to LF within `decode_coding', we can | |
600 | always open a file with binary mode. */ | |
601 | fd[0] = open (tempfile, O_BINARY); | |
602 | if (fd[0] < 0) | |
603 | { | |
604 | unlink (tempfile); | |
605 | close (filefd); | |
606 | report_file_error ("Cannot re-open temporary file", Qnil); | |
607 | } | |
608 | } | |
e0f712ba | 609 | #else /* not MAC_OS8 */ |
2610078a | 610 | #ifdef MSDOS /* MW, July 1993 */ |
c17c4250 | 611 | /* Note that on MSDOS `child_setup' actually returns the child process |
2610078a KH |
612 | exit status, not its PID, so we assign it to `synch_process_retcode' |
613 | below. */ | |
c17c4250 EZ |
614 | pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv, |
615 | 0, current_dir); | |
39eaa782 | 616 | |
2610078a KH |
617 | /* Record that the synchronous process exited and note its |
618 | termination status. */ | |
619 | synch_process_alive = 0; | |
620 | synch_process_retcode = pid; | |
621 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ | |
68c45bf0 | 622 | { |
ca9c0567 | 623 | synchronize_system_messages_locale (); |
68c45bf0 PE |
624 | synch_process_death = strerror (errno); |
625 | } | |
2610078a | 626 | |
68c45bf0 | 627 | emacs_close (outfilefd); |
2610078a | 628 | if (fd_error != outfilefd) |
68c45bf0 | 629 | emacs_close (fd_error); |
2610078a | 630 | fd1 = -1; /* No harm in closing that one! */ |
32d08644 KH |
631 | /* Since CRLF is converted to LF within `decode_coding', we can |
632 | always open a file with binary mode. */ | |
68c45bf0 | 633 | fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0); |
2610078a KH |
634 | if (fd[0] < 0) |
635 | { | |
636 | unlink (tempfile); | |
68c45bf0 | 637 | emacs_close (filefd); |
2610078a KH |
638 | report_file_error ("Cannot re-open temporary file", Qnil); |
639 | } | |
640 | #else /* not MSDOS */ | |
bad95d8f | 641 | #ifdef WINDOWSNT |
2d607244 RS |
642 | pid = child_setup (filefd, fd1, fd_error, (char **) new_argv, |
643 | 0, current_dir); | |
bad95d8f | 644 | #else /* not WINDOWSNT */ |
aba637ec KS |
645 | BLOCK_INPUT; |
646 | ||
80856e74 JB |
647 | pid = vfork (); |
648 | ||
649 | if (pid == 0) | |
650 | { | |
651 | if (fd[0] >= 0) | |
68c45bf0 | 652 | emacs_close (fd[0]); |
1e7963c7 RS |
653 | #ifdef HAVE_SETSID |
654 | setsid (); | |
655 | #endif | |
656 | #if defined (USG) && !defined (BSD_PGRPS) | |
80856e74 JB |
657 | setpgrp (); |
658 | #else | |
659 | setpgrp (pid, pid); | |
660 | #endif /* USG */ | |
2d607244 RS |
661 | child_setup (filefd, fd1, fd_error, (char **) new_argv, |
662 | 0, current_dir); | |
80856e74 | 663 | } |
aba637ec KS |
664 | |
665 | UNBLOCK_INPUT; | |
bad95d8f | 666 | #endif /* not WINDOWSNT */ |
cd5f8f60 RS |
667 | |
668 | /* The MSDOS case did this already. */ | |
669 | if (fd_error >= 0) | |
68c45bf0 | 670 | emacs_close (fd_error); |
2610078a | 671 | #endif /* not MSDOS */ |
e0f712ba | 672 | #endif /* not MAC_OS8 */ |
80856e74 | 673 | |
80856e74 JB |
674 | environ = save_environ; |
675 | ||
6b6e798b RS |
676 | /* Close most of our fd's, but not fd[0] |
677 | since we will use that to read input from. */ | |
68c45bf0 | 678 | emacs_close (filefd); |
799abb26 | 679 | if (fd1 >= 0 && fd1 != fd_error) |
68c45bf0 | 680 | emacs_close (fd1); |
80856e74 JB |
681 | } |
682 | ||
683 | if (pid < 0) | |
684 | { | |
6b6e798b | 685 | if (fd[0] >= 0) |
68c45bf0 | 686 | emacs_close (fd[0]); |
80856e74 JB |
687 | report_file_error ("Doing vfork", Qnil); |
688 | } | |
689 | ||
d50d3dc8 | 690 | if (INTEGERP (buffer)) |
80856e74 | 691 | { |
6b6e798b | 692 | if (fd[0] >= 0) |
68c45bf0 | 693 | emacs_close (fd[0]); |
80856e74 | 694 | #ifndef subprocesses |
e576cab4 JB |
695 | /* If Emacs has been built with asynchronous subprocess support, |
696 | we don't need to do this, I think because it will then have | |
697 | the facilities for handling SIGCHLD. */ | |
80856e74 JB |
698 | wait_without_blocking (); |
699 | #endif /* subprocesses */ | |
80856e74 JB |
700 | return Qnil; |
701 | } | |
702 | ||
6b6e798b | 703 | /* Enable sending signal if user quits below. */ |
37d54121 RS |
704 | call_process_exited = 0; |
705 | ||
e0f712ba | 706 | #if defined(MSDOS) || defined(MAC_OS8) |
7e6c2178 RS |
707 | /* MSDOS needs different cleanup information. */ |
708 | record_unwind_protect (call_process_cleanup, | |
709 | Fcons (make_number (fd[0]), build_string (tempfile))); | |
710 | #else | |
80856e74 JB |
711 | record_unwind_protect (call_process_cleanup, |
712 | Fcons (make_number (fd[0]), make_number (pid))); | |
e0f712ba | 713 | #endif /* not MSDOS and not MAC_OS8 */ |
80856e74 JB |
714 | |
715 | ||
d50d3dc8 | 716 | if (BUFFERP (buffer)) |
80856e74 JB |
717 | Fset_buffer (buffer); |
718 | ||
09494912 RS |
719 | if (NILP (buffer)) |
720 | { | |
721 | /* If BUFFER is nil, we must read process output once and then | |
722 | discard it, so setup coding system but with nil. */ | |
723 | setup_coding_system (Qnil, &process_coding); | |
724 | } | |
725 | else | |
726 | { | |
727 | Lisp_Object val, *args2; | |
728 | ||
729 | val = Qnil; | |
730 | if (!NILP (Vcoding_system_for_read)) | |
731 | val = Vcoding_system_for_read; | |
732 | else | |
733 | { | |
734 | if (EQ (coding_systems, Qt)) | |
735 | { | |
736 | int i; | |
737 | ||
738 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); | |
739 | args2[0] = Qcall_process; | |
740 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
741 | coding_systems | |
742 | = Ffind_operation_coding_system (nargs + 1, args2); | |
743 | } | |
744 | if (CONSP (coding_systems)) | |
70949dac | 745 | val = XCAR (coding_systems); |
09494912 | 746 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 747 | val = XCAR (Vdefault_process_coding_system); |
09494912 RS |
748 | else |
749 | val = Qnil; | |
750 | } | |
751 | setup_coding_system (Fcheck_coding_system (val), &process_coding); | |
752 | /* In unibyte mode, character code conversion should not take | |
753 | place but EOL conversion should. So, setup raw-text or one | |
754 | of the subsidiary according to the information just setup. */ | |
755 | if (NILP (current_buffer->enable_multibyte_characters) | |
756 | && !NILP (val)) | |
757 | setup_raw_text_coding_system (&process_coding); | |
758 | } | |
c5bfa12b KH |
759 | process_coding.src_multibyte = 0; |
760 | process_coding.dst_multibyte | |
761 | = (BUFFERP (buffer) | |
762 | ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters) | |
763 | : ! NILP (current_buffer->enable_multibyte_characters)); | |
09494912 | 764 | |
80856e74 JB |
765 | immediate_quit = 1; |
766 | QUIT; | |
767 | ||
768 | { | |
769 | register int nread; | |
0ad477db | 770 | int first = 1; |
6e3bfbb2 | 771 | int total_read = 0; |
321fecde | 772 | int carryover = 0; |
0aa2630f | 773 | int display_on_the_fly = display_p; |
05b44e90 | 774 | struct coding_system saved_coding; |
f778b157 KH |
775 | int pt_orig = PT, pt_byte_orig = PT_BYTE; |
776 | int inserted; | |
05b44e90 KH |
777 | |
778 | saved_coding = process_coding; | |
f0b950cf KH |
779 | if (process_coding.composing != COMPOSITION_DISABLED) |
780 | coding_allocate_composition_data (&process_coding, PT); | |
60558b19 | 781 | while (1) |
80856e74 | 782 | { |
60558b19 RS |
783 | /* Repeatedly read until we've filled as much as possible |
784 | of the buffer size we have. But don't read | |
8e6208c5 | 785 | less than 1024--save that for the next bufferful. */ |
321fecde | 786 | nread = carryover; |
60558b19 | 787 | while (nread < bufsize - 1024) |
00fb3e95 | 788 | { |
4da256b1 | 789 | int this_read = emacs_read (fd[0], buf + nread, |
68c45bf0 | 790 | bufsize - nread); |
60558b19 RS |
791 | |
792 | if (this_read < 0) | |
793 | goto give_up; | |
794 | ||
795 | if (this_read == 0) | |
7a7ab107 KH |
796 | { |
797 | process_coding.mode |= CODING_MODE_LAST_BLOCK; | |
798 | break; | |
799 | } | |
60558b19 RS |
800 | |
801 | nread += this_read; | |
7a7ab107 | 802 | total_read += this_read; |
60558b19 | 803 | |
7a7ab107 KH |
804 | if (display_on_the_fly) |
805 | break; | |
806 | } | |
60558b19 RS |
807 | |
808 | /* Now NREAD is the total amount of data in the buffer. */ | |
80856e74 | 809 | immediate_quit = 0; |
177c0ea7 | 810 | |
012c6fcb | 811 | if (!NILP (buffer)) |
32d08644 | 812 | { |
c5bfa12b | 813 | if (! CODING_MAY_REQUIRE_DECODING (&process_coding)) |
4da256b1 | 814 | insert_1_both (buf, nread, nread, 0, 1, 0); |
32d08644 KH |
815 | else |
816 | { /* We have to decode the input. */ | |
f0b950cf KH |
817 | int size; |
818 | char *decoding_buf; | |
32d08644 | 819 | |
f0b950cf KH |
820 | repeat_decoding: |
821 | size = decoding_buffer_size (&process_coding, nread); | |
822 | decoding_buf = (char *) xmalloc (size); | |
177c0ea7 | 823 | |
b252f801 KH |
824 | /* We can't use the macro CODING_REQUIRE_DETECTION |
825 | because it always returns nonzero if the coding | |
826 | system requires EOL detection. Here, we have to | |
827 | check only whether or not the coding system | |
828 | requires text-encoding detection. */ | |
829 | if (process_coding.type == coding_type_undecided) | |
950a45d0 | 830 | { |
4da256b1 | 831 | detect_coding (&process_coding, buf, nread); |
950a45d0 | 832 | if (process_coding.composing != COMPOSITION_DISABLED) |
07c381ad KH |
833 | /* We have not yet allocated the composition |
834 | data because the coding type was undecided. */ | |
950a45d0 KH |
835 | coding_allocate_composition_data (&process_coding, PT); |
836 | } | |
2d892150 KH |
837 | if (process_coding.cmp_data) |
838 | process_coding.cmp_data->char_offset = PT; | |
177c0ea7 | 839 | |
4da256b1 | 840 | decode_coding (&process_coding, buf, decoding_buf, |
321fecde | 841 | nread, size); |
177c0ea7 | 842 | |
7a7ab107 KH |
843 | if (display_on_the_fly |
844 | && saved_coding.type == coding_type_undecided | |
845 | && process_coding.type != coding_type_undecided) | |
846 | { | |
847 | /* We have detected some coding system. But, | |
848 | there's a possibility that the detection was | |
0aa2630f KS |
849 | done by insufficient data. So, we try the code |
850 | detection again with more data. */ | |
a871dd58 | 851 | xfree (decoding_buf); |
7a7ab107 KH |
852 | display_on_the_fly = 0; |
853 | process_coding = saved_coding; | |
854 | carryover = nread; | |
0aa2630f KS |
855 | /* This is to make the above condition always |
856 | fails in the future. */ | |
857 | saved_coding.type = coding_type_no_conversion; | |
7a7ab107 KH |
858 | continue; |
859 | } | |
177c0ea7 | 860 | |
321fecde | 861 | if (process_coding.produced > 0) |
2d892150 KH |
862 | insert_1_both (decoding_buf, process_coding.produced_char, |
863 | process_coding.produced, 0, 1, 0); | |
a871dd58 | 864 | xfree (decoding_buf); |
7bdba03c GM |
865 | |
866 | if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL) | |
867 | { | |
868 | Lisp_Object eol_type, coding; | |
869 | ||
870 | if (process_coding.eol_type == CODING_EOL_CR) | |
871 | { | |
872 | /* CRs have been replaced with LFs. Undo | |
873 | that in the text inserted above. */ | |
874 | unsigned char *p; | |
177c0ea7 | 875 | |
7bdba03c | 876 | move_gap_both (PT, PT_BYTE); |
177c0ea7 | 877 | |
7bdba03c GM |
878 | p = BYTE_POS_ADDR (pt_byte_orig); |
879 | for (; p < GPT_ADDR; ++p) | |
880 | if (*p == '\n') | |
881 | *p = '\r'; | |
882 | } | |
883 | else if (process_coding.eol_type == CODING_EOL_CRLF) | |
884 | { | |
885 | /* CR LFs have been replaced with LFs. Undo | |
886 | that by inserting CRs in front of LFs in | |
887 | the text inserted above. */ | |
888 | EMACS_INT bytepos, old_pt, old_pt_byte, nCR; | |
889 | ||
890 | old_pt = PT; | |
891 | old_pt_byte = PT_BYTE; | |
892 | nCR = 0; | |
177c0ea7 | 893 | |
7bdba03c GM |
894 | for (bytepos = PT_BYTE - 1; |
895 | bytepos >= pt_byte_orig; | |
896 | --bytepos) | |
897 | if (FETCH_BYTE (bytepos) == '\n') | |
898 | { | |
899 | EMACS_INT charpos = BYTE_TO_CHAR (bytepos); | |
900 | TEMP_SET_PT_BOTH (charpos, bytepos); | |
901 | insert_1_both ("\r", 1, 1, 0, 1, 0); | |
902 | ++nCR; | |
903 | } | |
904 | ||
905 | TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR); | |
906 | } | |
907 | ||
908 | /* Set the coding system symbol to that for | |
909 | Unix-like EOL. */ | |
910 | eol_type = Fget (saved_coding.symbol, Qeol_type); | |
911 | if (VECTORP (eol_type) | |
912 | && ASIZE (eol_type) == 3 | |
913 | && SYMBOLP (AREF (eol_type, CODING_EOL_LF))) | |
914 | coding = AREF (eol_type, CODING_EOL_LF); | |
915 | else | |
916 | coding = saved_coding.symbol; | |
177c0ea7 | 917 | |
7bdba03c GM |
918 | process_coding.symbol = coding; |
919 | process_coding.eol_type = CODING_EOL_LF; | |
920 | process_coding.mode | |
921 | &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL; | |
922 | } | |
177c0ea7 | 923 | |
f0b950cf KH |
924 | nread -= process_coding.consumed; |
925 | carryover = nread; | |
321fecde | 926 | if (carryover > 0) |
c5bfa12b KH |
927 | /* As CARRYOVER should not be that large, we had |
928 | better avoid overhead of bcopy. */ | |
4da256b1 | 929 | BCOPY_SHORT (buf + process_coding.consumed, buf, |
c5bfa12b | 930 | carryover); |
f0b950cf KH |
931 | if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP) |
932 | { | |
933 | /* The decoding ended because of insufficient data | |
934 | area to record information about composition. | |
935 | We must try decoding with additional data area | |
2d892150 | 936 | before reading more output for the process. */ |
f0b950cf KH |
937 | coding_allocate_composition_data (&process_coding, PT); |
938 | goto repeat_decoding; | |
939 | } | |
32d08644 KH |
940 | } |
941 | } | |
c5bfa12b | 942 | |
321fecde | 943 | if (process_coding.mode & CODING_MODE_LAST_BLOCK) |
c5bfa12b | 944 | break; |
6e3bfbb2 | 945 | |
4da256b1 | 946 | #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX) |
6e3bfbb2 | 947 | /* Make the buffer bigger as we continue to read more data, |
4da256b1 KS |
948 | but not past CALLPROC_BUFFER_SIZE_MAX. */ |
949 | if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize) | |
950 | if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX) | |
951 | bufsize = CALLPROC_BUFFER_SIZE_MAX; | |
952 | #endif | |
6e3bfbb2 | 953 | |
0aa2630f | 954 | if (display_p) |
0ad477db RS |
955 | { |
956 | if (first) | |
957 | prepare_menu_bars (); | |
958 | first = 0; | |
3007ebfb | 959 | redisplay_preserve_echo_area (1); |
0aa2630f KS |
960 | /* This variable might have been set to 0 for code |
961 | detection. In that case, we set it back to 1 because | |
962 | we should have already detected a coding system. */ | |
963 | display_on_the_fly = 1; | |
0ad477db | 964 | } |
80856e74 JB |
965 | immediate_quit = 1; |
966 | QUIT; | |
967 | } | |
60558b19 | 968 | give_up: ; |
80856e74 | 969 | |
2d892150 KH |
970 | if (!NILP (buffer) |
971 | && process_coding.cmp_data) | |
972 | { | |
973 | coding_restore_composition (&process_coding, Fcurrent_buffer ()); | |
974 | coding_free_composition_data (&process_coding); | |
975 | } | |
f0b950cf | 976 | |
838c9726 | 977 | { |
aed13378 | 978 | int post_read_count = SPECPDL_INDEX (); |
838c9726 KH |
979 | |
980 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
981 | inserted = PT - pt_orig; | |
982 | TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig); | |
983 | if (SYMBOLP (process_coding.post_read_conversion) | |
984 | && !NILP (Ffboundp (process_coding.post_read_conversion))) | |
985 | call1 (process_coding.post_read_conversion, make_number (inserted)); | |
f778b157 | 986 | |
838c9726 | 987 | Vlast_coding_system_used = process_coding.symbol; |
bbd29cfe | 988 | |
838c9726 KH |
989 | /* If the caller required, let the buffer inherit the |
990 | coding-system used to decode the process output. */ | |
991 | if (inherit_process_coding_system) | |
992 | call1 (intern ("after-insert-file-set-buffer-file-coding-system"), | |
993 | make_number (total_read)); | |
994 | ||
995 | unbind_to (post_read_count, Qnil); | |
996 | } | |
3b440bb5 EZ |
997 | } |
998 | ||
80856e74 JB |
999 | /* Wait for it to terminate, unless it already has. */ |
1000 | wait_for_termination (pid); | |
1001 | ||
1002 | immediate_quit = 0; | |
1003 | ||
1004 | set_buffer_internal (old); | |
1005 | ||
37d54121 RS |
1006 | /* Don't kill any children that the subprocess may have left behind |
1007 | when exiting. */ | |
1008 | call_process_exited = 1; | |
1009 | ||
80856e74 JB |
1010 | unbind_to (count, Qnil); |
1011 | ||
ca498128 JD |
1012 | if (synch_process_termsig) |
1013 | { | |
1014 | char *signame; | |
1015 | ||
1016 | synchronize_system_messages_locale (); | |
1017 | signame = strsignal (synch_process_termsig); | |
1018 | ||
1019 | if (signame == 0) | |
1020 | signame = "unknown"; | |
1021 | ||
1022 | synch_process_death = signame; | |
1023 | } | |
1024 | ||
80856e74 | 1025 | if (synch_process_death) |
68c45bf0 PE |
1026 | return code_convert_string_norecord (build_string (synch_process_death), |
1027 | Vlocale_coding_system, 0); | |
80856e74 JB |
1028 | return make_number (synch_process_retcode); |
1029 | } | |
1030 | #endif | |
1031 | \f | |
9fefd2ba | 1032 | static Lisp_Object |
80856e74 JB |
1033 | delete_temp_file (name) |
1034 | Lisp_Object name; | |
1035 | { | |
1a271e14 KR |
1036 | /* Suppress jka-compr handling, etc. */ |
1037 | int count = SPECPDL_INDEX (); | |
1038 | specbind (intern ("file-name-handler-alist"), Qnil); | |
2e3dc201 | 1039 | internal_delete_file (name); |
1a271e14 | 1040 | unbind_to (count, Qnil); |
320695d8 | 1041 | return Qnil; |
80856e74 JB |
1042 | } |
1043 | ||
1044 | DEFUN ("call-process-region", Fcall_process_region, Scall_process_region, | |
fdb82f93 PJ |
1045 | 3, MANY, 0, |
1046 | doc: /* Send text from START to END to a synchronous process running PROGRAM. | |
1047 | The remaining arguments are optional. | |
1048 | Delete the text if fourth arg DELETE is non-nil. | |
1049 | ||
1050 | Insert output in BUFFER before point; t means current buffer; | |
1051 | nil for BUFFER means discard it; 0 means discard and don't wait. | |
1052 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
1053 | REAL-BUFFER says what to do with standard output, as above, | |
1054 | while STDERR-FILE says what to do with standard error in the child. | |
1055 | STDERR-FILE may be nil (discard standard error output), | |
1056 | t (mix it with ordinary output), or a file name string. | |
1057 | ||
1058 | Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted. | |
1059 | Remaining args are passed to PROGRAM at startup as command args. | |
1060 | ||
ba9a5174 | 1061 | If BUFFER is 0, `call-process-region' returns immediately with value nil. |
fdb82f93 PJ |
1062 | Otherwise it waits for PROGRAM to terminate |
1063 | and returns a numeric exit status or a signal description string. | |
d98b59b5 MB |
1064 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again. |
1065 | ||
1066 | usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */) | |
fdb82f93 | 1067 | (nargs, args) |
80856e74 JB |
1068 | int nargs; |
1069 | register Lisp_Object *args; | |
1070 | { | |
39323a7e KH |
1071 | struct gcpro gcpro1; |
1072 | Lisp_Object filename_string; | |
1073 | register Lisp_Object start, end; | |
aed13378 | 1074 | int count = SPECPDL_INDEX (); |
08ee4e87 | 1075 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
09494912 | 1076 | Lisp_Object coding_systems; |
32d08644 KH |
1077 | Lisp_Object val, *args2; |
1078 | int i; | |
bad95d8f | 1079 | #ifdef DOS_NT |
7e6c2178 | 1080 | char *tempfile; |
7e6c2178 RS |
1081 | char *outf = '\0'; |
1082 | ||
8a52365c EZ |
1083 | if ((outf = egetenv ("TMPDIR")) |
1084 | || (outf = egetenv ("TMP")) | |
1085 | || (outf = egetenv ("TEMP"))) | |
7e6c2178 RS |
1086 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); |
1087 | else | |
1088 | { | |
1089 | tempfile = alloca (20); | |
1090 | *tempfile = '\0'; | |
1091 | } | |
0774fcf8 | 1092 | if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1])) |
7e6c2178 | 1093 | strcat (tempfile, "/"); |
5711b547 RS |
1094 | if ('/' == DIRECTORY_SEP) |
1095 | dostounix_filename (tempfile); | |
1096 | else | |
1097 | unixtodos_filename (tempfile); | |
0774fcf8 RS |
1098 | #ifdef WINDOWSNT |
1099 | strcat (tempfile, "emXXXXXX"); | |
1100 | #else | |
7e6c2178 | 1101 | strcat (tempfile, "detmp.XXX"); |
0774fcf8 | 1102 | #endif |
bad95d8f | 1103 | #else /* not DOS_NT */ |
d5db4077 KR |
1104 | char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1); |
1105 | bcopy (SDATA (Vtemp_file_name_pattern), tempfile, | |
1106 | SBYTES (Vtemp_file_name_pattern) + 1); | |
bad95d8f | 1107 | #endif /* not DOS_NT */ |
7e6c2178 | 1108 | |
09494912 RS |
1109 | coding_systems = Qt; |
1110 | ||
1ddc85a4 DL |
1111 | #ifdef HAVE_MKSTEMP |
1112 | { | |
d277f1f7 YM |
1113 | int fd; |
1114 | ||
1115 | BLOCK_INPUT; | |
1116 | fd = mkstemp (tempfile); | |
1117 | UNBLOCK_INPUT; | |
1ddc85a4 DL |
1118 | if (fd == -1) |
1119 | report_file_error ("Failed to open temporary file", | |
1120 | Fcons (Vtemp_file_name_pattern, Qnil)); | |
1121 | else | |
1122 | close (fd); | |
1123 | } | |
1124 | #else | |
80856e74 | 1125 | mktemp (tempfile); |
1ddc85a4 | 1126 | #endif |
80856e74 JB |
1127 | |
1128 | filename_string = build_string (tempfile); | |
39323a7e | 1129 | GCPRO1 (filename_string); |
80856e74 JB |
1130 | start = args[0]; |
1131 | end = args[1]; | |
32d08644 | 1132 | /* Decide coding-system of the contents of the temporary file. */ |
91489411 RS |
1133 | if (!NILP (Vcoding_system_for_write)) |
1134 | val = Vcoding_system_for_write; | |
1135 | else if (NILP (current_buffer->enable_multibyte_characters)) | |
32d08644 KH |
1136 | val = Qnil; |
1137 | else | |
beacaab3 | 1138 | { |
91489411 RS |
1139 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); |
1140 | args2[0] = Qcall_process_region; | |
1141 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
1142 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); | |
1143 | if (CONSP (coding_systems)) | |
70949dac | 1144 | val = XCDR (coding_systems); |
91489411 | 1145 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 1146 | val = XCDR (Vdefault_process_coding_system); |
beacaab3 | 1147 | else |
91489411 | 1148 | val = Qnil; |
beacaab3 | 1149 | } |
32d08644 | 1150 | |
168afdaa | 1151 | { |
aed13378 | 1152 | int count1 = SPECPDL_INDEX (); |
168afdaa RS |
1153 | |
1154 | specbind (intern ("coding-system-for-write"), val); | |
bb951f0e KR |
1155 | /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we |
1156 | happen to get a ".Z" suffix. */ | |
1157 | specbind (intern ("file-name-handler-alist"), Qnil); | |
168afdaa RS |
1158 | Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil); |
1159 | ||
1160 | unbind_to (count1, Qnil); | |
1161 | } | |
91489411 | 1162 | |
177c0ea7 | 1163 | /* Note that Fcall_process takes care of binding |
91489411 | 1164 | coding-system-for-read. */ |
093650fe | 1165 | |
80856e74 JB |
1166 | record_unwind_protect (delete_temp_file, filename_string); |
1167 | ||
edf496dd | 1168 | if (nargs > 3 && !NILP (args[3])) |
80856e74 JB |
1169 | Fdelete_region (start, end); |
1170 | ||
edf496dd KH |
1171 | if (nargs > 3) |
1172 | { | |
1173 | args += 2; | |
1174 | nargs -= 2; | |
1175 | } | |
1176 | else | |
1177 | { | |
1178 | args[0] = args[2]; | |
1179 | nargs = 2; | |
1180 | } | |
1181 | args[1] = filename_string; | |
80856e74 | 1182 | |
edf496dd | 1183 | RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args))); |
80856e74 JB |
1184 | } |
1185 | \f | |
1186 | #ifndef VMS /* VMS version is in vmsproc.c. */ | |
1187 | ||
dfcf069d AS |
1188 | static int relocate_fd (); |
1189 | ||
5990851d KL |
1190 | static char ** |
1191 | add_env (char **env, char **new_env, char *string) | |
1192 | { | |
1193 | char **ep; | |
1194 | int ok = 1; | |
1195 | if (string == NULL) | |
1196 | return new_env; | |
1197 | ||
1198 | /* See if this string duplicates any string already in the env. | |
1199 | If so, don't put it in. | |
1200 | When an env var has multiple definitions, | |
1201 | we keep the definition that comes first in process-environment. */ | |
1202 | for (ep = env; ok && ep != new_env; ep++) | |
1203 | { | |
1204 | char *p = *ep, *q = string; | |
1205 | while (ok) | |
1206 | { | |
1207 | if (*q != *p) | |
1208 | break; | |
1209 | if (*q == 0) | |
1210 | /* The string is a lone variable name; keep it for now, we | |
1211 | will remove it later. It is a placeholder for a | |
1212 | variable that is not to be included in the environment. */ | |
1213 | break; | |
1214 | if (*q == '=') | |
1215 | ok = 0; | |
1216 | p++, q++; | |
1217 | } | |
1218 | } | |
1219 | if (ok) | |
1220 | *new_env++ = string; | |
1221 | return new_env; | |
1222 | } | |
1223 | ||
80856e74 JB |
1224 | /* This is the last thing run in a newly forked inferior |
1225 | either synchronous or asynchronous. | |
1226 | Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2. | |
1227 | Initialize inferior's priority, pgrp, connected dir and environment. | |
1228 | then exec another program based on new_argv. | |
1229 | ||
1230 | This function may change environ for the superior process. | |
1231 | Therefore, the superior process must save and restore the value | |
1232 | of environ around the vfork and the call to this function. | |
1233 | ||
80856e74 | 1234 | SET_PGRP is nonzero if we should put the subprocess into a separate |
177c0ea7 | 1235 | process group. |
e576cab4 JB |
1236 | |
1237 | CURRENT_DIR is an elisp string giving the path of the current | |
1238 | directory the subprocess should have. Since we can't really signal | |
1239 | a decent error from within the child, this should be verified as an | |
1240 | executable directory by the parent. */ | |
80856e74 | 1241 | |
dfcf069d | 1242 | int |
e576cab4 | 1243 | child_setup (in, out, err, new_argv, set_pgrp, current_dir) |
80856e74 JB |
1244 | int in, out, err; |
1245 | register char **new_argv; | |
80856e74 | 1246 | int set_pgrp; |
e576cab4 | 1247 | Lisp_Object current_dir; |
80856e74 | 1248 | { |
e576cab4 | 1249 | char **env; |
7fcf7f05 | 1250 | char *pwd_var; |
bad95d8f RS |
1251 | #ifdef WINDOWSNT |
1252 | int cpid; | |
4252a4bd | 1253 | HANDLE handles[3]; |
bad95d8f | 1254 | #endif /* WINDOWSNT */ |
e576cab4 | 1255 | |
33abe2d9 | 1256 | int pid = getpid (); |
80856e74 | 1257 | |
68d10241 | 1258 | #ifdef SET_EMACS_PRIORITY |
4f0b9d49 | 1259 | { |
31ade731 | 1260 | extern EMACS_INT emacs_priority; |
4f0b9d49 | 1261 | |
68d10241 RS |
1262 | if (emacs_priority < 0) |
1263 | nice (- emacs_priority); | |
4f0b9d49 | 1264 | } |
5b633aeb | 1265 | #endif |
80856e74 JB |
1266 | |
1267 | #ifdef subprocesses | |
1268 | /* Close Emacs's descriptors that this process should not have. */ | |
1269 | close_process_descs (); | |
1270 | #endif | |
c17c4250 EZ |
1271 | /* DOS_NT isn't in a vfork, so if we are in the middle of load-file, |
1272 | we will lose if we call close_load_descs here. */ | |
1273 | #ifndef DOS_NT | |
4458cebe | 1274 | close_load_descs (); |
c17c4250 | 1275 | #endif |
80856e74 JB |
1276 | |
1277 | /* Note that use of alloca is always safe here. It's obvious for systems | |
1278 | that do not have true vfork or that have true (stack) alloca. | |
caa01fe0 GM |
1279 | If using vfork and C_ALLOCA (when Emacs used to include |
1280 | src/alloca.c) it is safe because that changes the superior's | |
1281 | static variables as if the superior had done alloca and will be | |
1282 | cleaned up in the usual way. */ | |
e576cab4 | 1283 | { |
7fcf7f05 | 1284 | register char *temp; |
e576cab4 | 1285 | register int i; |
77d78be1 | 1286 | |
d5db4077 | 1287 | i = SBYTES (current_dir); |
16425c4a EZ |
1288 | #ifdef MSDOS |
1289 | /* MSDOS must have all environment variables malloc'ed, because | |
1290 | low-level libc functions that launch subsidiary processes rely | |
1291 | on that. */ | |
1292 | pwd_var = (char *) xmalloc (i + 6); | |
1293 | #else | |
7fcf7f05 | 1294 | pwd_var = (char *) alloca (i + 6); |
16425c4a | 1295 | #endif |
7fcf7f05 RS |
1296 | temp = pwd_var + 4; |
1297 | bcopy ("PWD=", pwd_var, 4); | |
d5db4077 | 1298 | bcopy (SDATA (current_dir), temp, i); |
bad95d8f | 1299 | if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP; |
e576cab4 JB |
1300 | temp[i] = 0; |
1301 | ||
c17c4250 | 1302 | #ifndef DOS_NT |
e576cab4 JB |
1303 | /* We can't signal an Elisp error here; we're in a vfork. Since |
1304 | the callers check the current directory before forking, this | |
1305 | should only return an error if the directory's permissions | |
1306 | are changed between the check and this chdir, but we should | |
1307 | at least check. */ | |
1308 | if (chdir (temp) < 0) | |
20b25e46 | 1309 | _exit (errno); |
b4c7684c | 1310 | #endif |
7fcf7f05 | 1311 | |
c17c4250 EZ |
1312 | #ifdef DOS_NT |
1313 | /* Get past the drive letter, so that d:/ is left alone. */ | |
1314 | if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2])) | |
1315 | { | |
1316 | temp += 2; | |
1317 | i -= 2; | |
1318 | } | |
1319 | #endif | |
1320 | ||
7fcf7f05 | 1321 | /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */ |
bad95d8f | 1322 | while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1])) |
7fcf7f05 | 1323 | temp[--i] = 0; |
e576cab4 | 1324 | } |
80856e74 | 1325 | |
5990851d | 1326 | /* Set `env' to a vector of the strings in the environment. */ |
80856e74 JB |
1327 | { |
1328 | register Lisp_Object tem; | |
1329 | register char **new_env; | |
5990851d | 1330 | char **p, **q; |
80856e74 | 1331 | register int new_length; |
d2bb6598 | 1332 | Lisp_Object display = Qnil; |
de87fb59 | 1333 | |
80856e74 | 1334 | new_length = 0; |
f105f403 | 1335 | |
5990851d KL |
1336 | for (tem = Vprocess_environment; |
1337 | CONSP (tem) && STRINGP (XCAR (tem)); | |
1338 | tem = XCDR (tem)) | |
d2bb6598 SM |
1339 | { |
1340 | if (strncmp (SDATA (XCAR (tem)), "DISPLAY", 7) == 0 | |
1341 | && (SDATA (XCAR (tem)) [7] == '\0' | |
1342 | || SDATA (XCAR (tem)) [7] == '=')) | |
1343 | /* DISPLAY is specified in process-environment. */ | |
1344 | display = Qt; | |
1345 | new_length++; | |
1346 | } | |
de87fb59 | 1347 | |
d2bb6598 SM |
1348 | /* If not provided yet, use the frame's DISPLAY. */ |
1349 | if (NILP (display)) | |
1350 | { | |
1351 | Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay); | |
1352 | if (!STRINGP (tmp) && CONSP (Vinitial_environment)) | |
1353 | /* If still not found, Look for DISPLAY in Vinitial_environment. */ | |
1354 | tmp = Fgetenv_internal (build_string ("DISPLAY"), | |
1355 | Vinitial_environment); | |
1356 | if (STRINGP (tmp)) | |
1357 | { | |
1358 | display = tmp; | |
1359 | new_length++; | |
1360 | } | |
1361 | } | |
80856e74 | 1362 | |
7fcf7f05 RS |
1363 | /* new_length + 2 to include PWD and terminating 0. */ |
1364 | env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *)); | |
7fcf7f05 RS |
1365 | /* If we have a PWD envvar, pass one down, |
1366 | but with corrected value. */ | |
a13f8f50 | 1367 | if (egetenv ("PWD")) |
7fcf7f05 | 1368 | *new_env++ = pwd_var; |
5990851d | 1369 | |
6b8e474c | 1370 | if (STRINGP (display)) |
de87fb59 DN |
1371 | { |
1372 | int vlen = strlen ("DISPLAY=") + strlen (SDATA (display)) + 1; | |
1373 | char *vdata = (char *) alloca (vlen); | |
1374 | strcpy (vdata, "DISPLAY="); | |
1375 | strcat (vdata, SDATA (display)); | |
1376 | new_env = add_env (env, new_env, vdata); | |
1377 | } | |
1378 | ||
5990851d KL |
1379 | /* Overrides. */ |
1380 | for (tem = Vprocess_environment; | |
1381 | CONSP (tem) && STRINGP (XCAR (tem)); | |
1382 | tem = XCDR (tem)) | |
5990851d | 1383 | new_env = add_env (env, new_env, SDATA (XCAR (tem))); |
d2bb6598 | 1384 | |
5990851d KL |
1385 | *new_env = 0; |
1386 | ||
1387 | /* Remove variable names without values. */ | |
1388 | p = q = env; | |
1389 | while (*p != 0) | |
cd9565ba | 1390 | { |
5990851d | 1391 | while (*q != 0 && strchr (*q, '=') == NULL) |
1baf6db9 | 1392 | q++; |
5990851d KL |
1393 | *p = *q++; |
1394 | if (*p != 0) | |
1395 | p++; | |
cd9565ba | 1396 | } |
80856e74 | 1397 | } |
de87fb59 DN |
1398 | |
1399 | ||
bad95d8f RS |
1400 | #ifdef WINDOWSNT |
1401 | prepare_standard_handles (in, out, err, handles); | |
d5db4077 | 1402 | set_process_dir (SDATA (current_dir)); |
bad95d8f | 1403 | #else /* not WINDOWSNT */ |
426b37ae JB |
1404 | /* Make sure that in, out, and err are not actually already in |
1405 | descriptors zero, one, or two; this could happen if Emacs is | |
7e6c2178 | 1406 | started with its standard in, out, or error closed, as might |
426b37ae | 1407 | happen under X. */ |
f29f9e4a RS |
1408 | { |
1409 | int oin = in, oout = out; | |
1410 | ||
1411 | /* We have to avoid relocating the same descriptor twice! */ | |
1412 | ||
1413 | in = relocate_fd (in, 3); | |
1414 | ||
1415 | if (out == oin) | |
1416 | out = in; | |
1417 | else | |
3e9367e7 | 1418 | out = relocate_fd (out, 3); |
f29f9e4a RS |
1419 | |
1420 | if (err == oin) | |
1421 | err = in; | |
1422 | else if (err == oout) | |
1423 | err = out; | |
1424 | else | |
3e9367e7 | 1425 | err = relocate_fd (err, 3); |
f29f9e4a | 1426 | } |
426b37ae | 1427 | |
c17c4250 | 1428 | #ifndef MSDOS |
68c45bf0 PE |
1429 | emacs_close (0); |
1430 | emacs_close (1); | |
1431 | emacs_close (2); | |
80856e74 JB |
1432 | |
1433 | dup2 (in, 0); | |
1434 | dup2 (out, 1); | |
1435 | dup2 (err, 2); | |
68c45bf0 PE |
1436 | emacs_close (in); |
1437 | emacs_close (out); | |
1438 | emacs_close (err); | |
c17c4250 | 1439 | #endif /* not MSDOS */ |
bad95d8f | 1440 | #endif /* not WINDOWSNT */ |
80856e74 | 1441 | |
6b2cd868 | 1442 | #if defined(USG) && !defined(BSD_PGRPS) |
fdba8590 | 1443 | #ifndef SETPGRP_RELEASES_CTTY |
e576cab4 | 1444 | setpgrp (); /* No arguments but equivalent in this case */ |
fdba8590 | 1445 | #endif |
e576cab4 JB |
1446 | #else |
1447 | setpgrp (pid, pid); | |
1448 | #endif /* USG */ | |
a129418f RS |
1449 | /* setpgrp_of_tty is incorrect here; it uses input_fd. */ |
1450 | EMACS_SET_TTY_PGRP (0, &pid); | |
80856e74 | 1451 | |
c17c4250 EZ |
1452 | #ifdef MSDOS |
1453 | pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env); | |
a3f0666f | 1454 | xfree (pwd_var); |
c17c4250 EZ |
1455 | if (pid == -1) |
1456 | /* An error occurred while trying to run the subprocess. */ | |
1457 | report_file_error ("Spawning child process", Qnil); | |
1458 | return pid; | |
1459 | #else /* not MSDOS */ | |
bad95d8f RS |
1460 | #ifdef WINDOWSNT |
1461 | /* Spawn the child. (See ntproc.c:Spawnve). */ | |
1462 | cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env); | |
db77d785 | 1463 | reset_standard_handles (in, out, err, handles); |
ff27bfbe KH |
1464 | if (cpid == -1) |
1465 | /* An error occurred while trying to spawn the process. */ | |
1466 | report_file_error ("Spawning child process", Qnil); | |
bad95d8f RS |
1467 | return cpid; |
1468 | #else /* not WINDOWSNT */ | |
80856e74 JB |
1469 | /* execvp does not accept an environment arg so the only way |
1470 | to pass this environment is to set environ. Our caller | |
1471 | is responsible for restoring the ambient value of environ. */ | |
1472 | environ = env; | |
1473 | execvp (new_argv[0], new_argv); | |
1474 | ||
68c45bf0 PE |
1475 | emacs_write (1, "Can't exec program: ", 20); |
1476 | emacs_write (1, new_argv[0], strlen (new_argv[0])); | |
1477 | emacs_write (1, "\n", 1); | |
80856e74 | 1478 | _exit (1); |
bad95d8f | 1479 | #endif /* not WINDOWSNT */ |
7e6c2178 | 1480 | #endif /* not MSDOS */ |
80856e74 JB |
1481 | } |
1482 | ||
a3833dfe | 1483 | /* Move the file descriptor FD so that its number is not less than MINFD. |
426b37ae | 1484 | If the file descriptor is moved at all, the original is freed. */ |
dfcf069d | 1485 | static int |
a3833dfe KH |
1486 | relocate_fd (fd, minfd) |
1487 | int fd, minfd; | |
426b37ae | 1488 | { |
a3833dfe | 1489 | if (fd >= minfd) |
426b37ae JB |
1490 | return fd; |
1491 | else | |
1492 | { | |
1493 | int new = dup (fd); | |
1494 | if (new == -1) | |
1495 | { | |
20c018a0 | 1496 | char *message1 = "Error while setting up child: "; |
826c56ac | 1497 | char *errmessage = strerror (errno); |
20c018a0 | 1498 | char *message2 = "\n"; |
68c45bf0 PE |
1499 | emacs_write (2, message1, strlen (message1)); |
1500 | emacs_write (2, errmessage, strlen (errmessage)); | |
1501 | emacs_write (2, message2, strlen (message2)); | |
426b37ae JB |
1502 | _exit (1); |
1503 | } | |
1504 | /* Note that we hold the original FD open while we recurse, | |
1505 | to guarantee we'll get a new FD if we need it. */ | |
a3833dfe | 1506 | new = relocate_fd (new, minfd); |
68c45bf0 | 1507 | emacs_close (fd); |
426b37ae JB |
1508 | return new; |
1509 | } | |
1510 | } | |
1511 | ||
db699fc6 SM |
1512 | static int |
1513 | getenv_internal_1 (var, varlen, value, valuelen, env) | |
1514 | char *var; | |
1515 | int varlen; | |
1516 | char **value; | |
1517 | int *valuelen; | |
1518 | Lisp_Object env; | |
1519 | { | |
1520 | for (; CONSP (env); env = XCDR (env)) | |
1521 | { | |
1522 | Lisp_Object entry = XCAR (env); | |
1523 | if (STRINGP (entry) | |
1524 | && SBYTES (entry) >= varlen | |
1525 | #ifdef WINDOWSNT | |
1526 | /* NT environment variables are case insensitive. */ | |
1527 | && ! strnicmp (SDATA (entry), var, varlen) | |
1528 | #else /* not WINDOWSNT */ | |
1529 | && ! bcmp (SDATA (entry), var, varlen) | |
1530 | #endif /* not WINDOWSNT */ | |
1531 | ) | |
1532 | { | |
1533 | if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=') | |
1534 | { | |
1535 | *value = (char *) SDATA (entry) + (varlen + 1); | |
1536 | *valuelen = SBYTES (entry) - (varlen + 1); | |
1537 | return 1; | |
1538 | } | |
1539 | else if (SBYTES (entry) == varlen) | |
1540 | { | |
1541 | /* Lone variable names in Vprocess_environment mean that | |
1542 | variable should be removed from the environment. */ | |
1543 | *value = NULL; | |
1544 | return 1; | |
1545 | } | |
1546 | } | |
1547 | } | |
1548 | return 0; | |
1549 | } | |
1550 | ||
012c6fcb | 1551 | static int |
da8e8fc1 | 1552 | getenv_internal (var, varlen, value, valuelen, frame) |
012c6fcb JA |
1553 | char *var; |
1554 | int varlen; | |
1555 | char **value; | |
1556 | int *valuelen; | |
da8e8fc1 | 1557 | Lisp_Object frame; |
012c6fcb | 1558 | { |
d2bb6598 SM |
1559 | /* Try to find VAR in Vprocess_environment first. */ |
1560 | if (getenv_internal_1 (var, varlen, value, valuelen, | |
1561 | Vprocess_environment)) | |
1562 | return *value ? 1 : 0; | |
f105f403 | 1563 | |
d2bb6598 | 1564 | /* For DISPLAY try to get the values from the frame or the initial env. */ |
de87fb59 | 1565 | if (strcmp (var, "DISPLAY") == 0) |
d2bb6598 SM |
1566 | { |
1567 | Lisp_Object display | |
1568 | = Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay); | |
1569 | if (STRINGP (display)) | |
1570 | { | |
de87fb59 DN |
1571 | *value = (char *) SDATA (display); |
1572 | *valuelen = SBYTES (display); | |
1573 | return 1; | |
d2bb6598 SM |
1574 | } |
1575 | /* If still not found, Look for DISPLAY in Vinitial_environment. */ | |
1576 | if (getenv_internal_1 (var, varlen, value, valuelen, | |
1577 | Vinitial_environment)) | |
1578 | return *value ? 1 : 0; | |
1579 | } | |
012c6fcb | 1580 | |
012c6fcb JA |
1581 | return 0; |
1582 | } | |
1583 | ||
f105f403 | 1584 | DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0, |
5990851d KL |
1585 | doc: /* Get the value of environment variable VARIABLE. |
1586 | VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
1587 | the environment. Otherwise, value is a string. | |
f105f403 | 1588 | |
a13f8f50 KL |
1589 | This function searches `process-environment' for VARIABLE. If it is |
1590 | not found there, then it continues the search in the environment list | |
1591 | of the selected frame. | |
5990851d | 1592 | |
db699fc6 SM |
1593 | If optional parameter ENV is a list, then search this list instead of |
1594 | `process-environment', and return t when encountering a negative entry. | |
1595 | ||
1596 | If it is a frame, then this function will ignore `process-environment' and | |
1597 | will simply look up the variable in that frame's environment. */) | |
1598 | (variable, env) | |
1599 | Lisp_Object variable, env; | |
012c6fcb JA |
1600 | { |
1601 | char *value; | |
1602 | int valuelen; | |
1603 | ||
5990851d | 1604 | CHECK_STRING (variable); |
db699fc6 SM |
1605 | if (CONSP (env)) |
1606 | { | |
1607 | if (getenv_internal_1 (SDATA (variable), SBYTES (variable), | |
1608 | &value, &valuelen, env)) | |
1609 | return value ? make_string (value, valuelen) : Qt; | |
1610 | else | |
1611 | return Qnil; | |
1612 | } | |
1613 | else if (getenv_internal (SDATA (variable), SBYTES (variable), | |
d2bb6598 | 1614 | &value, &valuelen, env)) |
012c6fcb JA |
1615 | return make_string (value, valuelen); |
1616 | else | |
1617 | return Qnil; | |
1618 | } | |
1619 | ||
5990851d KL |
1620 | /* A version of getenv that consults the Lisp environment lists, |
1621 | easily callable from C. */ | |
012c6fcb JA |
1622 | char * |
1623 | egetenv (var) | |
e576cab4 | 1624 | char *var; |
012c6fcb JA |
1625 | { |
1626 | char *value; | |
1627 | int valuelen; | |
1628 | ||
f105f403 | 1629 | if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil)) |
012c6fcb JA |
1630 | return value; |
1631 | else | |
1632 | return 0; | |
1633 | } | |
1634 | ||
80856e74 JB |
1635 | #endif /* not VMS */ |
1636 | \f | |
8de15d69 | 1637 | /* This is run before init_cmdargs. */ |
177c0ea7 | 1638 | |
dfcf069d | 1639 | void |
8de15d69 RS |
1640 | init_callproc_1 () |
1641 | { | |
1642 | char *data_dir = egetenv ("EMACSDATA"); | |
35a2f4b8 KH |
1643 | char *doc_dir = egetenv ("EMACSDOC"); |
1644 | ||
8de15d69 | 1645 | Vdata_directory |
177c0ea7 | 1646 | = Ffile_name_as_directory (build_string (data_dir ? data_dir |
8de15d69 | 1647 | : PATH_DATA)); |
35a2f4b8 KH |
1648 | Vdoc_directory |
1649 | = Ffile_name_as_directory (build_string (doc_dir ? doc_dir | |
1650 | : PATH_DOC)); | |
9453ea7b | 1651 | |
e576cab4 | 1652 | /* Check the EMACSPATH environment variable, defaulting to the |
57bda87a | 1653 | PATH_EXEC path from epaths.h. */ |
e576cab4 | 1654 | Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC); |
80856e74 JB |
1655 | Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path)); |
1656 | Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path); | |
8de15d69 RS |
1657 | } |
1658 | ||
e17f7533 | 1659 | /* This is run after init_cmdargs, when Vinstallation_directory is valid. */ |
8de15d69 | 1660 | |
dfcf069d | 1661 | void |
8de15d69 RS |
1662 | init_callproc () |
1663 | { | |
1664 | char *data_dir = egetenv ("EMACSDATA"); | |
177c0ea7 | 1665 | |
8de15d69 RS |
1666 | register char * sh; |
1667 | Lisp_Object tempdir; | |
1668 | ||
9cc4fad5 | 1669 | if (!NILP (Vinstallation_directory)) |
8de15d69 | 1670 | { |
05630743 RS |
1671 | /* Add to the path the lib-src subdir of the installation dir. */ |
1672 | Lisp_Object tem; | |
1673 | tem = Fexpand_file_name (build_string ("lib-src"), | |
1674 | Vinstallation_directory); | |
bad95d8f | 1675 | #ifndef DOS_NT |
1a6640ec | 1676 | /* MSDOS uses wrapped binaries, so don't do this. */ |
0fa248bc | 1677 | if (NILP (Fmember (tem, Vexec_path))) |
70ec1377 RS |
1678 | { |
1679 | Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC); | |
1680 | Vexec_path = Fcons (tem, Vexec_path); | |
1681 | Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path); | |
1682 | } | |
177c0ea7 | 1683 | |
0fa248bc | 1684 | Vexec_directory = Ffile_name_as_directory (tem); |
bad95d8f | 1685 | #endif /* not DOS_NT */ |
8de15d69 | 1686 | |
e17f7533 RS |
1687 | /* Maybe use ../etc as well as ../lib-src. */ |
1688 | if (data_dir == 0) | |
1689 | { | |
1690 | tem = Fexpand_file_name (build_string ("etc"), | |
1691 | Vinstallation_directory); | |
1692 | Vdoc_directory = Ffile_name_as_directory (tem); | |
8de15d69 RS |
1693 | } |
1694 | } | |
7e933683 RS |
1695 | |
1696 | /* Look for the files that should be in etc. We don't use | |
1697 | Vinstallation_directory, because these files are never installed | |
e17f7533 | 1698 | near the executable, and they are never in the build |
7e933683 RS |
1699 | directory when that's different from the source directory. |
1700 | ||
1701 | Instead, if these files are not in the nominal place, we try the | |
1702 | source directory. */ | |
1703 | if (data_dir == 0) | |
1704 | { | |
70ec1377 | 1705 | Lisp_Object tem, tem1, srcdir; |
7e933683 | 1706 | |
70ec1377 RS |
1707 | srcdir = Fexpand_file_name (build_string ("../src/"), |
1708 | build_string (PATH_DUMPLOADSEARCH)); | |
7e933683 RS |
1709 | tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory); |
1710 | tem1 = Ffile_exists_p (tem); | |
70ec1377 | 1711 | if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1)) |
7e933683 | 1712 | { |
70ec1377 | 1713 | Lisp_Object newdir; |
7e933683 RS |
1714 | newdir = Fexpand_file_name (build_string ("../etc/"), |
1715 | build_string (PATH_DUMPLOADSEARCH)); | |
1716 | tem = Fexpand_file_name (build_string ("GNU"), newdir); | |
1717 | tem1 = Ffile_exists_p (tem); | |
1718 | if (!NILP (tem1)) | |
1719 | Vdata_directory = newdir; | |
1720 | } | |
1721 | } | |
80856e74 | 1722 | |
d883eb62 RS |
1723 | #ifndef CANNOT_DUMP |
1724 | if (initialized) | |
1725 | #endif | |
1726 | { | |
1727 | tempdir = Fdirectory_file_name (Vexec_directory); | |
d5db4077 | 1728 | if (access (SDATA (tempdir), 0) < 0) |
d883eb62 RS |
1729 | dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n", |
1730 | Vexec_directory); | |
1731 | } | |
80856e74 | 1732 | |
e576cab4 | 1733 | tempdir = Fdirectory_file_name (Vdata_directory); |
d5db4077 | 1734 | if (access (SDATA (tempdir), 0) < 0) |
76d5c6cf RS |
1735 | dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n", |
1736 | Vdata_directory); | |
e576cab4 | 1737 | |
80856e74 JB |
1738 | #ifdef VMS |
1739 | Vshell_file_name = build_string ("*dcl*"); | |
1740 | #else | |
e576cab4 | 1741 | sh = (char *) getenv ("SHELL"); |
80856e74 JB |
1742 | Vshell_file_name = build_string (sh ? sh : "/bin/sh"); |
1743 | #endif | |
8abd035b RS |
1744 | |
1745 | #ifdef VMS | |
1746 | Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX."); | |
1747 | #else | |
1748 | if (getenv ("TMPDIR")) | |
1749 | { | |
1750 | char *dir = getenv ("TMPDIR"); | |
1751 | Vtemp_file_name_pattern | |
a13f8f50 KL |
1752 | = Fexpand_file_name (build_string ("emacsXXXXXX"), |
1753 | build_string (dir)); | |
8abd035b RS |
1754 | } |
1755 | else | |
1756 | Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX"); | |
1757 | #endif | |
63789758 | 1758 | |
40b49d4b JB |
1759 | #ifdef DOS_NT |
1760 | Vshared_game_score_directory = Qnil; | |
1761 | #else | |
63789758 RS |
1762 | Vshared_game_score_directory = build_string (PATH_GAME); |
1763 | if (NILP (Ffile_directory_p (Vshared_game_score_directory))) | |
1764 | Vshared_game_score_directory = Qnil; | |
40b49d4b | 1765 | #endif |
9fefd2ba JB |
1766 | } |
1767 | ||
dfcf069d | 1768 | void |
a13f8f50 | 1769 | set_initial_environment () |
9fefd2ba JB |
1770 | { |
1771 | register char **envp; | |
80856e74 JB |
1772 | #ifndef CANNOT_DUMP |
1773 | if (initialized) | |
1774 | #endif | |
a13f8f50 KL |
1775 | { |
1776 | for (envp = environ; *envp; envp++) | |
de87fb59 DN |
1777 | Vprocess_environment = Fcons (build_string (*envp), |
1778 | Vprocess_environment); | |
3c33c79a SM |
1779 | /* Ideally, the `copy' shouldn't be necessary, but it seems it's frequent |
1780 | to use `delete' and friends on process-environment. */ | |
1781 | Vinitial_environment = Fcopy_sequence (Vprocess_environment); | |
a13f8f50 | 1782 | } |
80856e74 JB |
1783 | } |
1784 | ||
dfcf069d | 1785 | void |
80856e74 JB |
1786 | syms_of_callproc () |
1787 | { | |
bad95d8f | 1788 | #ifdef DOS_NT |
093650fe RS |
1789 | Qbuffer_file_type = intern ("buffer-file-type"); |
1790 | staticpro (&Qbuffer_file_type); | |
bad95d8f | 1791 | #endif /* DOS_NT */ |
7e6c2178 | 1792 | |
80856e74 | 1793 | DEFVAR_LISP ("shell-file-name", &Vshell_file_name, |
fdb82f93 | 1794 | doc: /* *File name to load inferior shells from. |
b20b29be EZ |
1795 | Initialized from the SHELL environment variable, or to a system-dependent |
1796 | default if SHELL is not set. */); | |
80856e74 JB |
1797 | |
1798 | DEFVAR_LISP ("exec-path", &Vexec_path, | |
fdb82f93 PJ |
1799 | doc: /* *List of directories to search programs to run in subprocesses. |
1800 | Each element is a string (directory name) or nil (try default directory). */); | |
80856e74 | 1801 | |
b81a1b72 | 1802 | DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes, |
fdb82f93 PJ |
1803 | doc: /* *List of suffixes to try to find executable file names. |
1804 | Each element is a string. */); | |
33d5af99 | 1805 | Vexec_suffixes = Qnil; |
b81a1b72 | 1806 | |
80856e74 | 1807 | DEFVAR_LISP ("exec-directory", &Vexec_directory, |
fdb82f93 PJ |
1808 | doc: /* Directory for executables for Emacs to invoke. |
1809 | More generally, this includes any architecture-dependent files | |
1810 | that are built and installed from the Emacs distribution. */); | |
e576cab4 JB |
1811 | |
1812 | DEFVAR_LISP ("data-directory", &Vdata_directory, | |
fdb82f93 PJ |
1813 | doc: /* Directory of machine-independent files that come with GNU Emacs. |
1814 | These are files intended for Emacs to use while it runs. */); | |
80856e74 | 1815 | |
35a2f4b8 | 1816 | DEFVAR_LISP ("doc-directory", &Vdoc_directory, |
fdb82f93 | 1817 | doc: /* Directory containing the DOC file that comes with GNU Emacs. |
ebf24b59 | 1818 | This is usually the same as `data-directory'. */); |
35a2f4b8 | 1819 | |
ed61592a | 1820 | DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory, |
fdb82f93 PJ |
1821 | doc: /* For internal use by the build procedure only. |
1822 | This is the name of the directory in which the build procedure installed | |
ebf24b59 | 1823 | Emacs's info files; the default value for `Info-default-directory-list' |
fdb82f93 | 1824 | includes this. */); |
ed61592a JB |
1825 | Vconfigure_info_directory = build_string (PATH_INFO); |
1826 | ||
1e7ce61b | 1827 | DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory, |
b065672a CW |
1828 | doc: /* Directory of score files for games which come with GNU Emacs. |
1829 | If this variable is nil, then Emacs is unable to use a shared directory. */); | |
40b49d4b JB |
1830 | #ifdef DOS_NT |
1831 | Vshared_game_score_directory = Qnil; | |
1832 | #else | |
63789758 | 1833 | Vshared_game_score_directory = build_string (PATH_GAME); |
40b49d4b | 1834 | #endif |
b065672a | 1835 | |
8abd035b | 1836 | DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern, |
fdb82f93 PJ |
1837 | doc: /* Pattern for making names for temporary files. |
1838 | This is used by `call-process-region'. */); | |
0537ec48 | 1839 | /* This variable is initialized in init_callproc. */ |
8abd035b | 1840 | |
6b8e474c SM |
1841 | DEFVAR_LISP ("initial-environment", &Vinitial_environment, |
1842 | doc: /* List of environment variables inherited from the parent process. | |
1843 | Each element should be a string of the form ENVVARNAME=VALUE. | |
1844 | The elements must normally be decoded (using `locale-coding-system') for use. */); | |
1845 | Vinitial_environment = Qnil; | |
1846 | ||
5990851d KL |
1847 | DEFVAR_LISP ("process-environment", &Vprocess_environment, |
1848 | doc: /* List of overridden environment variables for subprocesses to inherit. | |
1849 | Each element should be a string of the form ENVVARNAME=VALUE. | |
1850 | ||
a13f8f50 KL |
1851 | Entries in this list take precedence to those in the frame-local |
1852 | environments. Therefore, let-binding `process-environment' is an easy | |
1853 | way to temporarily change the value of an environment variable, | |
1854 | irrespective of where it comes from. To use `process-environment' to | |
1855 | remove an environment variable, include only its name in the list, | |
1856 | without "=VALUE". | |
5990851d KL |
1857 | |
1858 | This variable is set to nil when Emacs starts. | |
1859 | ||
1860 | If multiple entries define the same variable, the first one always | |
1861 | takes precedence. | |
1862 | ||
1863 | Non-ASCII characters are encoded according to the initial value of | |
1864 | `locale-coding-system', i.e. the elements must normally be decoded for | |
1865 | use. | |
1866 | ||
776a24a1 | 1867 | See `setenv' and `getenv'. */); |
86f5ca04 | 1868 | Vprocess_environment = Qnil; |
80856e74 JB |
1869 | |
1870 | #ifndef VMS | |
1871 | defsubr (&Scall_process); | |
83fa009c | 1872 | defsubr (&Sgetenv_internal); |
986ffb24 | 1873 | #endif |
e576cab4 | 1874 | defsubr (&Scall_process_region); |
80856e74 | 1875 | } |
ab5796a9 MB |
1876 | |
1877 | /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95 | |
1878 | (do not change this comment) */ |