Commit | Line | Data |
---|---|---|
80856e74 | 1 | /* Synchronous subprocess invocation for GNU Emacs. |
f8c25f1b | 2 | Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc. |
80856e74 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
826c56ac | 8 | the Free Software Foundation; either version 2, or (at your option) |
80856e74 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
80856e74 JB |
20 | |
21 | ||
22 | #include <signal.h> | |
e576cab4 | 23 | #include <errno.h> |
80856e74 | 24 | |
18160b98 | 25 | #include <config.h> |
565620a5 | 26 | #include <stdio.h> |
80856e74 | 27 | |
426b37ae | 28 | extern int errno; |
826c56ac | 29 | extern char *strerror (); |
426b37ae | 30 | |
80856e74 JB |
31 | /* Define SIGCHLD as an alias for SIGCLD. */ |
32 | ||
33 | #if !defined (SIGCHLD) && defined (SIGCLD) | |
34 | #define SIGCHLD SIGCLD | |
35 | #endif /* SIGCLD */ | |
36 | ||
37 | #include <sys/types.h> | |
88a64fef | 38 | |
80856e74 JB |
39 | #include <sys/file.h> |
40 | #ifdef USG5 | |
472e83fe | 41 | #define INCLUDED_FCNTL |
80856e74 JB |
42 | #include <fcntl.h> |
43 | #endif | |
44 | ||
bad95d8f RS |
45 | #ifdef WINDOWSNT |
46 | #define NOMINMAX | |
47 | #include <windows.h> | |
48 | #include <stdlib.h> /* for proper declaration of environ */ | |
49 | #include <fcntl.h> | |
489f9371 | 50 | #include "w32.h" |
bad95d8f RS |
51 | #define _P_NOWAIT 1 /* from process.h */ |
52 | #endif | |
53 | ||
7e6c2178 | 54 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
00353d4f | 55 | #include "msdos.h" |
472e83fe | 56 | #define INCLUDED_FCNTL |
7e6c2178 RS |
57 | #include <fcntl.h> |
58 | #include <sys/stat.h> | |
59 | #include <sys/param.h> | |
60 | #include <errno.h> | |
61 | #endif /* MSDOS */ | |
62 | ||
80856e74 JB |
63 | #ifndef O_RDONLY |
64 | #define O_RDONLY 0 | |
65 | #endif | |
66 | ||
67 | #ifndef O_WRONLY | |
68 | #define O_WRONLY 1 | |
69 | #endif | |
70 | ||
71 | #include "lisp.h" | |
72 | #include "commands.h" | |
73 | #include "buffer.h" | |
32d08644 KH |
74 | #include "charset.h" |
75 | #include "coding.h" | |
2a6b3537 | 76 | #include <paths.h> |
80856e74 | 77 | #include "process.h" |
d177f194 | 78 | #include "syssignal.h" |
a129418f | 79 | #include "systty.h" |
80856e74 JB |
80 | |
81 | #ifdef VMS | |
82 | extern noshare char **environ; | |
83 | #else | |
84 | extern char **environ; | |
85 | #endif | |
86 | ||
87 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
88 | ||
bad95d8f | 89 | #ifdef DOS_NT |
093650fe RS |
90 | /* When we are starting external processes we need to know whether they |
91 | take binary input (no conversion) or text input (\n is converted to | |
92 | \r\n). Similar for output: if newlines are written as \r\n then it's | |
93 | text process output, otherwise it's binary. */ | |
94 | Lisp_Object Vbinary_process_input; | |
95 | Lisp_Object Vbinary_process_output; | |
bad95d8f | 96 | #endif /* DOS_NT */ |
7e6c2178 | 97 | |
35a2f4b8 | 98 | Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory; |
ed61592a | 99 | Lisp_Object Vconfigure_info_directory; |
8abd035b | 100 | Lisp_Object Vtemp_file_name_pattern; |
80856e74 JB |
101 | |
102 | Lisp_Object Vshell_file_name; | |
103 | ||
80856e74 | 104 | Lisp_Object Vprocess_environment; |
80856e74 | 105 | |
bad95d8f | 106 | #ifdef DOS_NT |
093650fe | 107 | Lisp_Object Qbuffer_file_type; |
bad95d8f | 108 | #endif /* DOS_NT */ |
093650fe | 109 | |
80856e74 JB |
110 | /* True iff we are about to fork off a synchronous process or if we |
111 | are waiting for it. */ | |
112 | int synch_process_alive; | |
113 | ||
114 | /* Nonzero => this is a string explaining death of synchronous subprocess. */ | |
115 | char *synch_process_death; | |
116 | ||
117 | /* If synch_process_death is zero, | |
118 | this is exit code of synchronous subprocess. */ | |
119 | int synch_process_retcode; | |
8de15d69 RS |
120 | |
121 | extern Lisp_Object Vdoc_file_name; | |
89e1ec1d | 122 | |
8d024345 | 123 | extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system; |
80856e74 | 124 | \f |
37d54121 RS |
125 | /* Clean up when exiting Fcall_process. |
126 | On MSDOS, delete the temporary file on any kind of termination. | |
127 | On Unix, kill the process and any children on termination by signal. */ | |
128 | ||
129 | /* Nonzero if this is termination due to exit. */ | |
130 | static int call_process_exited; | |
131 | ||
80856e74 JB |
132 | #ifndef VMS /* VMS version is in vmsproc.c. */ |
133 | ||
d177f194 JB |
134 | static Lisp_Object |
135 | call_process_kill (fdpid) | |
136 | Lisp_Object fdpid; | |
137 | { | |
138 | close (XFASTINT (Fcar (fdpid))); | |
139 | EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL); | |
140 | synch_process_alive = 0; | |
141 | return Qnil; | |
142 | } | |
143 | ||
80856e74 JB |
144 | Lisp_Object |
145 | call_process_cleanup (fdpid) | |
146 | Lisp_Object fdpid; | |
147 | { | |
7e6c2178 RS |
148 | #ifdef MSDOS |
149 | /* for MSDOS fdpid is really (fd . tempfile) */ | |
c1350752 KH |
150 | register Lisp_Object file; |
151 | file = Fcdr (fdpid); | |
7e6c2178 RS |
152 | close (XFASTINT (Fcar (fdpid))); |
153 | if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0) | |
154 | unlink (XSTRING (file)->data); | |
155 | #else /* not MSDOS */ | |
d177f194 JB |
156 | register int pid = XFASTINT (Fcdr (fdpid)); |
157 | ||
6b6e798b | 158 | |
37d54121 | 159 | if (call_process_exited) |
6b6e798b RS |
160 | { |
161 | close (XFASTINT (Fcar (fdpid))); | |
162 | return Qnil; | |
163 | } | |
37d54121 | 164 | |
d177f194 JB |
165 | if (EMACS_KILLPG (pid, SIGINT) == 0) |
166 | { | |
167 | int count = specpdl_ptr - specpdl; | |
168 | record_unwind_protect (call_process_kill, fdpid); | |
169 | message1 ("Waiting for process to die...(type C-g again to kill it instantly)"); | |
170 | immediate_quit = 1; | |
171 | QUIT; | |
172 | wait_for_termination (pid); | |
173 | immediate_quit = 0; | |
174 | specpdl_ptr = specpdl + count; /* Discard the unwind protect. */ | |
175 | message1 ("Waiting for process to die...done"); | |
176 | } | |
80856e74 | 177 | synch_process_alive = 0; |
d177f194 | 178 | close (XFASTINT (Fcar (fdpid))); |
7e6c2178 | 179 | #endif /* not MSDOS */ |
80856e74 JB |
180 | return Qnil; |
181 | } | |
182 | ||
183 | DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0, | |
184 | "Call PROGRAM synchronously in separate process.\n\ | |
185 | The program's input comes from file INFILE (nil means `/dev/null').\n\ | |
186 | Insert output in BUFFER before point; t means current buffer;\n\ | |
187 | nil for BUFFER means discard it; 0 means discard and don't wait.\n\ | |
39eaa782 RS |
188 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\ |
189 | REAL-BUFFER says what to do with standard output, as above,\n\ | |
190 | while STDERR-FILE says what to do with standard error in the child.\n\ | |
191 | STDERR-FILE may be nil (discard standard error output),\n\ | |
192 | t (mix it with ordinary output), or a file name string.\n\ | |
193 | \n\ | |
80856e74 JB |
194 | Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\ |
195 | Remaining arguments are strings passed as command arguments to PROGRAM.\n\ | |
39eaa782 RS |
196 | \n\ |
197 | If BUFFER is 0, `call-process' returns immediately with value nil.\n\ | |
198 | Otherwise it waits for PROGRAM to terminate\n\ | |
e576cab4 | 199 | and returns a numeric exit status or a signal description string.\n\ |
d177f194 | 200 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.") |
80856e74 JB |
201 | (nargs, args) |
202 | int nargs; | |
203 | register Lisp_Object *args; | |
204 | { | |
58616e67 | 205 | Lisp_Object infile, buffer, current_dir, display, path; |
80856e74 JB |
206 | int fd[2]; |
207 | int filefd; | |
208 | register int pid; | |
6e3bfbb2 RS |
209 | char buf[16384]; |
210 | char *bufptr = buf; | |
211 | int bufsize = 16384; | |
80856e74 | 212 | int count = specpdl_ptr - specpdl; |
2d607244 | 213 | |
80856e74 JB |
214 | register unsigned char **new_argv |
215 | = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); | |
216 | struct buffer *old = current_buffer; | |
39eaa782 RS |
217 | /* File to use for stderr in the child. |
218 | t means use same as standard output. */ | |
219 | Lisp_Object error_file; | |
7e6c2178 RS |
220 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
221 | char *outf, *tempfile; | |
222 | int outfilefd; | |
223 | #endif | |
80856e74 JB |
224 | #if 0 |
225 | int mask; | |
226 | #endif | |
32d08644 KH |
227 | struct coding_system process_coding; /* coding-system of process output */ |
228 | struct coding_system argument_coding; /* coding-system of arguments */ | |
229 | ||
80856e74 JB |
230 | CHECK_STRING (args[0], 0); |
231 | ||
39eaa782 RS |
232 | error_file = Qt; |
233 | ||
7e6c2178 RS |
234 | #ifndef subprocesses |
235 | /* Without asynchronous processes we cannot have BUFFER == 0. */ | |
d50d3dc8 | 236 | if (nargs >= 3 && INTEGERP (args[2])) |
7e6c2178 RS |
237 | error ("Operating system cannot handle asynchronous subprocesses"); |
238 | #endif /* subprocesses */ | |
239 | ||
32d08644 KH |
240 | /* Decide the coding-system for giving arguments and reading process |
241 | output. */ | |
242 | { | |
243 | Lisp_Object val, *args2; | |
08ee4e87 | 244 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
32d08644 KH |
245 | Lisp_Object coding_systems = Qt; |
246 | int i; | |
247 | ||
248 | /* If arguments are supplied, we may have to encode them. */ | |
249 | if (nargs >= 5) | |
250 | { | |
30d57b8e RS |
251 | int must_encode = 0; |
252 | ||
a2286b5c | 253 | for (i = 4; i < nargs; i++) |
30d57b8e RS |
254 | if (STRING_MULTIBYTE (args[i])) |
255 | must_encode = 1; | |
256 | ||
beacaab3 KH |
257 | if (!NILP (Vcoding_system_for_write)) |
258 | val = Vcoding_system_for_write; | |
30d57b8e | 259 | else if (! must_encode) |
beacaab3 KH |
260 | val = Qnil; |
261 | else | |
32d08644 KH |
262 | { |
263 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); | |
264 | args2[0] = Qcall_process; | |
265 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
08ee4e87 | 266 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); |
776b95cb KH |
267 | if (CONSP (coding_systems)) |
268 | val = XCONS (coding_systems)->cdr; | |
269 | else if (CONSP (Vdefault_process_coding_system)) | |
270 | val = XCONS (Vdefault_process_coding_system)->cdr; | |
beacaab3 KH |
271 | else |
272 | val = Qnil; | |
32d08644 KH |
273 | } |
274 | setup_coding_system (Fcheck_coding_system (val), &argument_coding); | |
275 | } | |
276 | ||
277 | /* If BUFFER is nil, we must read process output once and then | |
278 | discard it, so setup coding system but with nil. If BUFFER is | |
279 | an integer, we can discard it without reading. */ | |
280 | if (nargs < 3 || NILP (args[2])) | |
281 | setup_coding_system (Qnil, &process_coding); | |
282 | else if (!INTEGERP (args[2])) | |
283 | { | |
beacaab3 KH |
284 | val = Qnil; |
285 | if (!NILP (Vcoding_system_for_read)) | |
286 | val = Vcoding_system_for_read; | |
287 | else if (NILP (current_buffer->enable_multibyte_characters)) | |
321fecde | 288 | val = Qraw_text; |
beacaab3 | 289 | else |
32d08644 KH |
290 | { |
291 | if (!EQ (coding_systems, Qt)) | |
292 | { | |
293 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); | |
294 | args2[0] = Qcall_process; | |
295 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
08ee4e87 KH |
296 | coding_systems |
297 | = Ffind_operation_coding_system (nargs + 1, args2); | |
32d08644 | 298 | } |
776b95cb KH |
299 | if (CONSP (coding_systems)) |
300 | val = XCONS (coding_systems)->car; | |
301 | else if (CONSP (Vdefault_process_coding_system)) | |
302 | val = XCONS (Vdefault_process_coding_system)->car; | |
beacaab3 KH |
303 | else |
304 | val = Qnil; | |
32d08644 KH |
305 | } |
306 | setup_coding_system (Fcheck_coding_system (val), &process_coding); | |
c49f3d5a | 307 | #ifdef MSDOS |
91489411 RS |
308 | /* On MSDOS, if the user did not ask for binary, treat it as |
309 | "text" which means doing CRLF conversion. Otherwise, leave | |
310 | the EOLs alone. | |
311 | ||
312 | Note that ``binary'' here only means whether EOLs should or | |
313 | should not be converted, since that's what Vbinary_process_XXXput | |
314 | meant in the days before the coding systems were introduced. | |
315 | ||
316 | For other conversions, the caller should set coding-system | |
317 | variables explicitly, or rely on auto-detection. */ | |
318 | ||
c49f3d5a RS |
319 | /* FIXME: this probably should be moved into the guts of |
320 | `Ffind_operation_coding_system' for the case of `call-process'. */ | |
321 | if (NILP (Vbinary_process_output)) | |
322 | { | |
323 | process_coding.eol_type = CODING_EOL_CRLF; | |
324 | if (process_coding.type == coding_type_no_conversion) | |
325 | /* FIXME: should we set type to undecided? */ | |
326 | process_coding.type = coding_type_emacs_mule; | |
327 | } | |
91489411 RS |
328 | else |
329 | process_coding.eol_type = CODING_EOL_LF; | |
c49f3d5a | 330 | #endif |
32d08644 KH |
331 | } |
332 | } | |
333 | ||
e576cab4 JB |
334 | if (nargs >= 2 && ! NILP (args[1])) |
335 | { | |
336 | infile = Fexpand_file_name (args[1], current_buffer->directory); | |
337 | CHECK_STRING (infile, 1); | |
338 | } | |
80856e74 | 339 | else |
5437e9f9 | 340 | infile = build_string (NULL_DEVICE); |
80856e74 | 341 | |
e576cab4 JB |
342 | if (nargs >= 3) |
343 | { | |
39eaa782 RS |
344 | buffer = args[2]; |
345 | ||
346 | /* If BUFFER is a list, its meaning is | |
347 | (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */ | |
348 | if (CONSP (buffer)) | |
349 | { | |
350 | if (CONSP (XCONS (buffer)->cdr)) | |
45be8a1e | 351 | { |
a9d4f28a | 352 | Lisp_Object stderr_file; |
45be8a1e RS |
353 | stderr_file = XCONS (XCONS (buffer)->cdr)->car; |
354 | ||
355 | if (NILP (stderr_file) || EQ (Qt, stderr_file)) | |
356 | error_file = stderr_file; | |
357 | else | |
358 | error_file = Fexpand_file_name (stderr_file, Qnil); | |
359 | } | |
360 | ||
39eaa782 RS |
361 | buffer = XCONS (buffer)->car; |
362 | } | |
044512ed | 363 | |
39eaa782 RS |
364 | if (!(EQ (buffer, Qnil) |
365 | || EQ (buffer, Qt) | |
366 | || XFASTINT (buffer) == 0)) | |
e576cab4 | 367 | { |
39eaa782 RS |
368 | Lisp_Object spec_buffer; |
369 | spec_buffer = buffer; | |
50fe359b | 370 | buffer = Fget_buffer_create (buffer); |
39eaa782 RS |
371 | /* Mention the buffer name for a better error message. */ |
372 | if (NILP (buffer)) | |
373 | CHECK_BUFFER (spec_buffer, 2); | |
e576cab4 JB |
374 | CHECK_BUFFER (buffer, 2); |
375 | } | |
376 | } | |
377 | else | |
378 | buffer = Qnil; | |
80856e74 | 379 | |
58616e67 JB |
380 | /* Make sure that the child will be able to chdir to the current |
381 | buffer's current directory, or its unhandled equivalent. We | |
382 | can't just have the child check for an error when it does the | |
383 | chdir, since it's in a vfork. | |
384 | ||
385 | We have to GCPRO around this because Fexpand_file_name, | |
386 | Funhandled_file_name_directory, and Ffile_accessible_directory_p | |
387 | might call a file name handling function. The argument list is | |
388 | protected by the caller, so all we really have to worry about is | |
389 | buffer. */ | |
390 | { | |
391 | struct gcpro gcpro1, gcpro2, gcpro3; | |
392 | ||
393 | current_dir = current_buffer->directory; | |
394 | ||
395 | GCPRO3 (infile, buffer, current_dir); | |
396 | ||
c52b0b34 KH |
397 | current_dir |
398 | = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir), | |
399 | Qnil); | |
58616e67 JB |
400 | if (NILP (Ffile_accessible_directory_p (current_dir))) |
401 | report_file_error ("Setting current directory", | |
402 | Fcons (current_buffer->directory, Qnil)); | |
403 | ||
404 | UNGCPRO; | |
405 | } | |
406 | ||
e576cab4 | 407 | display = nargs >= 4 ? args[3] : Qnil; |
80856e74 | 408 | |
e576cab4 | 409 | filefd = open (XSTRING (infile)->data, O_RDONLY, 0); |
80856e74 JB |
410 | if (filefd < 0) |
411 | { | |
e576cab4 | 412 | report_file_error ("Opening process input file", Fcons (infile, Qnil)); |
80856e74 JB |
413 | } |
414 | /* Search for program; barf if not found. */ | |
c52b0b34 KH |
415 | { |
416 | struct gcpro gcpro1; | |
417 | ||
418 | GCPRO1 (current_dir); | |
419 | openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1); | |
420 | UNGCPRO; | |
421 | } | |
012c6fcb | 422 | if (NILP (path)) |
80856e74 JB |
423 | { |
424 | close (filefd); | |
425 | report_file_error ("Searching for program", Fcons (args[0], Qnil)); | |
426 | } | |
427 | new_argv[0] = XSTRING (path)->data; | |
c364e618 KH |
428 | if (nargs > 4) |
429 | { | |
430 | register int i; | |
431 | ||
432 | for (i = 4; i < nargs; i++) CHECK_STRING (args[i], i); | |
433 | ||
434 | if (! CODING_REQUIRE_ENCODING (&argument_coding)) | |
435 | { | |
436 | for (i = 4; i < nargs; i++) | |
437 | new_argv[i - 3] = XSTRING (args[i])->data; | |
438 | } | |
439 | else | |
440 | { | |
441 | /* We must encode the arguments. */ | |
442 | struct gcpro gcpro1, gcpro2, gcpro3; | |
443 | ||
444 | GCPRO3 (infile, buffer, current_dir); | |
445 | for (i = 4; i < nargs; i++) | |
446 | { | |
447 | int size = encoding_buffer_size (&argument_coding, | |
30d57b8e | 448 | XSTRING (args[i])->size_byte); |
c364e618 | 449 | unsigned char *dummy1 = (unsigned char *) alloca (size); |
321fecde | 450 | int dummy; |
c364e618 KH |
451 | |
452 | /* The Irix 4.0 compiler barfs if we eliminate dummy. */ | |
453 | new_argv[i - 3] = dummy1; | |
321fecde KH |
454 | encode_coding (&argument_coding, |
455 | XSTRING (args[i])->data, | |
456 | new_argv[i - 3], | |
457 | XSTRING (args[i])->size_byte, | |
458 | size); | |
459 | new_argv[i - 3][argument_coding.produced] = 0; | |
c364e618 KH |
460 | } |
461 | UNGCPRO; | |
462 | } | |
db54baaa | 463 | new_argv[nargs - 3] = 0; |
c364e618 | 464 | } |
db54baaa KH |
465 | else |
466 | new_argv[1] = 0; | |
80856e74 | 467 | |
7e6c2178 | 468 | #ifdef MSDOS /* MW, July 1993 */ |
7e6c2178 RS |
469 | if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP"))) |
470 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); | |
471 | else | |
472 | { | |
473 | tempfile = alloca (20); | |
474 | *tempfile = '\0'; | |
475 | } | |
476 | dostounix_filename (tempfile); | |
477 | if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/') | |
478 | strcat (tempfile, "/"); | |
479 | strcat (tempfile, "detmp.XXX"); | |
480 | mktemp (tempfile); | |
481 | ||
482 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); | |
483 | if (outfilefd < 0) | |
484 | { | |
485 | close (filefd); | |
6f89d28a MB |
486 | report_file_error ("Opening process output file", |
487 | Fcons (build_string (tempfile), Qnil)); | |
7e6c2178 | 488 | } |
6f89d28a | 489 | fd[0] = filefd; |
2610078a | 490 | fd[1] = outfilefd; |
6f89d28a | 491 | #endif /* MSDOS */ |
7e6c2178 | 492 | |
d50d3dc8 | 493 | if (INTEGERP (buffer)) |
5437e9f9 | 494 | fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1; |
80856e74 JB |
495 | else |
496 | { | |
7e6c2178 | 497 | #ifndef MSDOS |
80856e74 | 498 | pipe (fd); |
7e6c2178 | 499 | #endif |
80856e74 JB |
500 | #if 0 |
501 | /* Replaced by close_process_descs */ | |
502 | set_exclusive_use (fd[0]); | |
503 | #endif | |
504 | } | |
505 | ||
506 | { | |
507 | /* child_setup must clobber environ in systems with true vfork. | |
508 | Protect it from permanent change. */ | |
509 | register char **save_environ = environ; | |
510 | register int fd1 = fd[1]; | |
39eaa782 | 511 | int fd_error = fd1; |
80856e74 JB |
512 | |
513 | #if 0 /* Some systems don't have sigblock. */ | |
e065a56e | 514 | mask = sigblock (sigmask (SIGCHLD)); |
80856e74 JB |
515 | #endif |
516 | ||
517 | /* Record that we're about to create a synchronous process. */ | |
518 | synch_process_alive = 1; | |
519 | ||
5c03767e RS |
520 | /* These vars record information from process termination. |
521 | Clear them now before process can possibly terminate, | |
522 | to avoid timing error if process terminates soon. */ | |
523 | synch_process_death = 0; | |
524 | synch_process_retcode = 0; | |
525 | ||
39eaa782 RS |
526 | if (NILP (error_file)) |
527 | fd_error = open (NULL_DEVICE, O_WRONLY); | |
528 | else if (STRINGP (error_file)) | |
529 | { | |
530 | #ifdef DOS_NT | |
531 | fd_error = open (XSTRING (error_file)->data, | |
532 | O_WRONLY | O_TRUNC | O_CREAT | O_TEXT, | |
533 | S_IREAD | S_IWRITE); | |
534 | #else /* not DOS_NT */ | |
535 | fd_error = creat (XSTRING (error_file)->data, 0666); | |
536 | #endif /* not DOS_NT */ | |
537 | } | |
538 | ||
539 | if (fd_error < 0) | |
540 | { | |
541 | close (filefd); | |
6f89d28a MB |
542 | if (fd[0] != filefd) |
543 | close (fd[0]); | |
39eaa782 RS |
544 | if (fd1 >= 0) |
545 | close (fd1); | |
6f89d28a MB |
546 | #ifdef MSDOS |
547 | unlink (tempfile); | |
548 | #endif | |
549 | report_file_error ("Cannot redirect stderr", | |
550 | Fcons ((NILP (error_file) | |
551 | ? build_string (NULL_DEVICE) : error_file), | |
552 | Qnil)); | |
39eaa782 | 553 | } |
89e1ec1d | 554 | |
8d024345 | 555 | current_dir = ENCODE_FILE (current_dir); |
89e1ec1d | 556 | |
2610078a KH |
557 | #ifdef MSDOS /* MW, July 1993 */ |
558 | /* ??? Someone who knows MSDOG needs to check whether this properly | |
559 | closes all descriptors that it opens. | |
560 | ||
561 | Note that run_msdos_command() actually returns the child process | |
562 | exit status, not its PID, so we assign it to `synch_process_retcode' | |
563 | below. */ | |
564 | pid = run_msdos_command (new_argv, current_dir, | |
565 | filefd, outfilefd, fd_error); | |
39eaa782 | 566 | |
2610078a KH |
567 | /* Record that the synchronous process exited and note its |
568 | termination status. */ | |
569 | synch_process_alive = 0; | |
570 | synch_process_retcode = pid; | |
571 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ | |
759ae811 | 572 | synch_process_death = strerror (errno); |
2610078a KH |
573 | |
574 | close (outfilefd); | |
575 | if (fd_error != outfilefd) | |
576 | close (fd_error); | |
577 | fd1 = -1; /* No harm in closing that one! */ | |
32d08644 KH |
578 | /* Since CRLF is converted to LF within `decode_coding', we can |
579 | always open a file with binary mode. */ | |
580 | fd[0] = open (tempfile, O_BINARY); | |
2610078a KH |
581 | if (fd[0] < 0) |
582 | { | |
583 | unlink (tempfile); | |
584 | close (filefd); | |
585 | report_file_error ("Cannot re-open temporary file", Qnil); | |
586 | } | |
587 | #else /* not MSDOS */ | |
bad95d8f | 588 | #ifdef WINDOWSNT |
2d607244 RS |
589 | pid = child_setup (filefd, fd1, fd_error, (char **) new_argv, |
590 | 0, current_dir); | |
bad95d8f | 591 | #else /* not WINDOWSNT */ |
80856e74 JB |
592 | pid = vfork (); |
593 | ||
594 | if (pid == 0) | |
595 | { | |
596 | if (fd[0] >= 0) | |
597 | close (fd[0]); | |
1e7963c7 RS |
598 | #ifdef HAVE_SETSID |
599 | setsid (); | |
600 | #endif | |
601 | #if defined (USG) && !defined (BSD_PGRPS) | |
80856e74 JB |
602 | setpgrp (); |
603 | #else | |
604 | setpgrp (pid, pid); | |
605 | #endif /* USG */ | |
2d607244 RS |
606 | child_setup (filefd, fd1, fd_error, (char **) new_argv, |
607 | 0, current_dir); | |
80856e74 | 608 | } |
bad95d8f | 609 | #endif /* not WINDOWSNT */ |
cd5f8f60 RS |
610 | |
611 | /* The MSDOS case did this already. */ | |
612 | if (fd_error >= 0) | |
613 | close (fd_error); | |
2610078a | 614 | #endif /* not MSDOS */ |
80856e74 | 615 | |
80856e74 JB |
616 | environ = save_environ; |
617 | ||
6b6e798b RS |
618 | /* Close most of our fd's, but not fd[0] |
619 | since we will use that to read input from. */ | |
80856e74 | 620 | close (filefd); |
799abb26 | 621 | if (fd1 >= 0 && fd1 != fd_error) |
7e6c2178 | 622 | close (fd1); |
80856e74 JB |
623 | } |
624 | ||
625 | if (pid < 0) | |
626 | { | |
6b6e798b RS |
627 | if (fd[0] >= 0) |
628 | close (fd[0]); | |
80856e74 JB |
629 | report_file_error ("Doing vfork", Qnil); |
630 | } | |
631 | ||
d50d3dc8 | 632 | if (INTEGERP (buffer)) |
80856e74 | 633 | { |
6b6e798b RS |
634 | if (fd[0] >= 0) |
635 | close (fd[0]); | |
80856e74 | 636 | #ifndef subprocesses |
e576cab4 JB |
637 | /* If Emacs has been built with asynchronous subprocess support, |
638 | we don't need to do this, I think because it will then have | |
639 | the facilities for handling SIGCHLD. */ | |
80856e74 JB |
640 | wait_without_blocking (); |
641 | #endif /* subprocesses */ | |
80856e74 JB |
642 | return Qnil; |
643 | } | |
644 | ||
6b6e798b | 645 | /* Enable sending signal if user quits below. */ |
37d54121 RS |
646 | call_process_exited = 0; |
647 | ||
7e6c2178 RS |
648 | #ifdef MSDOS |
649 | /* MSDOS needs different cleanup information. */ | |
650 | record_unwind_protect (call_process_cleanup, | |
651 | Fcons (make_number (fd[0]), build_string (tempfile))); | |
652 | #else | |
80856e74 JB |
653 | record_unwind_protect (call_process_cleanup, |
654 | Fcons (make_number (fd[0]), make_number (pid))); | |
7e6c2178 | 655 | #endif /* not MSDOS */ |
80856e74 JB |
656 | |
657 | ||
d50d3dc8 | 658 | if (BUFFERP (buffer)) |
80856e74 JB |
659 | Fset_buffer (buffer); |
660 | ||
661 | immediate_quit = 1; | |
662 | QUIT; | |
663 | ||
664 | { | |
665 | register int nread; | |
0ad477db | 666 | int first = 1; |
6e3bfbb2 | 667 | int total_read = 0; |
321fecde | 668 | int carryover = 0; |
80856e74 | 669 | |
60558b19 | 670 | while (1) |
80856e74 | 671 | { |
60558b19 RS |
672 | /* Repeatedly read until we've filled as much as possible |
673 | of the buffer size we have. But don't read | |
8e6208c5 | 674 | less than 1024--save that for the next bufferful. */ |
60558b19 | 675 | |
321fecde | 676 | nread = carryover; |
60558b19 | 677 | while (nread < bufsize - 1024) |
00fb3e95 | 678 | { |
321fecde | 679 | int this_read = read (fd[0], bufptr + nread, bufsize - nread); |
60558b19 RS |
680 | |
681 | if (this_read < 0) | |
682 | goto give_up; | |
683 | ||
684 | if (this_read == 0) | |
685 | goto give_up_1; | |
686 | ||
687 | nread += this_read; | |
00fb3e95 | 688 | } |
60558b19 RS |
689 | |
690 | give_up_1: | |
691 | ||
692 | /* Now NREAD is the total amount of data in the buffer. */ | |
321fecde | 693 | if (nread == carryover) |
32d08644 KH |
694 | /* Here, just tell decode_coding that we are processing the |
695 | last block. We break the loop after decoding. */ | |
321fecde | 696 | process_coding.mode |= CODING_MODE_LAST_BLOCK; |
60558b19 | 697 | |
80856e74 | 698 | immediate_quit = 0; |
321fecde | 699 | total_read += nread - carryover; |
6e3bfbb2 | 700 | |
012c6fcb | 701 | if (!NILP (buffer)) |
32d08644 KH |
702 | { |
703 | if (process_coding.type == coding_type_no_conversion) | |
704 | insert (bufptr, nread); | |
705 | else | |
706 | { /* We have to decode the input. */ | |
321fecde | 707 | int size = decoding_buffer_size (&process_coding, nread); |
32d08644 | 708 | char *decoding_buf = get_conversion_buffer (size); |
32d08644 | 709 | |
321fecde KH |
710 | decode_coding (&process_coding, bufptr, decoding_buf, |
711 | nread, size); | |
712 | if (process_coding.produced > 0) | |
713 | insert (decoding_buf, process_coding.produced); | |
11218c68 | 714 | carryover = nread - process_coding.consumed; |
321fecde KH |
715 | if (carryover > 0) |
716 | { | |
717 | /* As CARRYOVER should not be that large, we had | |
718 | better avoid overhead of bcopy. */ | |
719 | char *p = bufptr + process_coding.consumed; | |
720 | char *pend = p + carryover; | |
721 | char *dst = bufptr; | |
722 | ||
723 | while (p < pend) *dst++ = *p++; | |
724 | } | |
32d08644 KH |
725 | } |
726 | } | |
321fecde KH |
727 | if (process_coding.mode & CODING_MODE_LAST_BLOCK) |
728 | { | |
729 | if (carryover > 0) | |
730 | insert (bufptr, carryover); | |
731 | break; | |
732 | } | |
6e3bfbb2 RS |
733 | |
734 | /* Make the buffer bigger as we continue to read more data, | |
735 | but not past 64k. */ | |
736 | if (bufsize < 64 * 1024 && total_read > 32 * bufsize) | |
737 | { | |
738 | bufsize *= 2; | |
739 | bufptr = (char *) alloca (bufsize); | |
740 | } | |
741 | ||
012c6fcb | 742 | if (!NILP (display) && INTERACTIVE) |
0ad477db RS |
743 | { |
744 | if (first) | |
745 | prepare_menu_bars (); | |
746 | first = 0; | |
747 | redisplay_preserve_echo_area (); | |
748 | } | |
80856e74 JB |
749 | immediate_quit = 1; |
750 | QUIT; | |
751 | } | |
60558b19 | 752 | give_up: ; |
80856e74 JB |
753 | } |
754 | ||
bbd29cfe KH |
755 | Vlast_coding_system_used = process_coding.symbol; |
756 | ||
80856e74 JB |
757 | /* Wait for it to terminate, unless it already has. */ |
758 | wait_for_termination (pid); | |
759 | ||
760 | immediate_quit = 0; | |
761 | ||
762 | set_buffer_internal (old); | |
763 | ||
37d54121 RS |
764 | /* Don't kill any children that the subprocess may have left behind |
765 | when exiting. */ | |
766 | call_process_exited = 1; | |
767 | ||
80856e74 JB |
768 | unbind_to (count, Qnil); |
769 | ||
80856e74 JB |
770 | if (synch_process_death) |
771 | return build_string (synch_process_death); | |
772 | return make_number (synch_process_retcode); | |
773 | } | |
774 | #endif | |
775 | \f | |
9fefd2ba | 776 | static Lisp_Object |
80856e74 JB |
777 | delete_temp_file (name) |
778 | Lisp_Object name; | |
779 | { | |
2e3dc201 | 780 | /* Use Fdelete_file (indirectly) because that runs a file name handler. |
59750d69 | 781 | We did that when writing the file, so we should do so when deleting. */ |
2e3dc201 | 782 | internal_delete_file (name); |
80856e74 JB |
783 | } |
784 | ||
785 | DEFUN ("call-process-region", Fcall_process_region, Scall_process_region, | |
786 | 3, MANY, 0, | |
787 | "Send text from START to END to a synchronous process running PROGRAM.\n\ | |
788 | Delete the text if fourth arg DELETE is non-nil.\n\ | |
39eaa782 | 789 | \n\ |
80856e74 JB |
790 | Insert output in BUFFER before point; t means current buffer;\n\ |
791 | nil for BUFFER means discard it; 0 means discard and don't wait.\n\ | |
39eaa782 RS |
792 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\ |
793 | REAL-BUFFER says what to do with standard output, as above,\n\ | |
794 | while STDERR-FILE says what to do with standard error in the child.\n\ | |
795 | STDERR-FILE may be nil (discard standard error output),\n\ | |
796 | t (mix it with ordinary output), or a file name string.\n\ | |
797 | \n\ | |
80856e74 JB |
798 | Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\ |
799 | Remaining args are passed to PROGRAM at startup as command args.\n\ | |
39eaa782 RS |
800 | \n\ |
801 | If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\ | |
802 | Otherwise it waits for PROGRAM to terminate\n\ | |
e576cab4 | 803 | and returns a numeric exit status or a signal description string.\n\ |
d177f194 | 804 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.") |
80856e74 JB |
805 | (nargs, args) |
806 | int nargs; | |
807 | register Lisp_Object *args; | |
808 | { | |
39323a7e KH |
809 | struct gcpro gcpro1; |
810 | Lisp_Object filename_string; | |
811 | register Lisp_Object start, end; | |
d3e81d0a | 812 | int count = specpdl_ptr - specpdl; |
08ee4e87 | 813 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
32d08644 KH |
814 | Lisp_Object coding_systems = Qt; |
815 | Lisp_Object val, *args2; | |
816 | int i; | |
bad95d8f | 817 | #ifdef DOS_NT |
7e6c2178 | 818 | char *tempfile; |
7e6c2178 RS |
819 | char *outf = '\0'; |
820 | ||
821 | if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP"))) | |
822 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); | |
823 | else | |
824 | { | |
825 | tempfile = alloca (20); | |
826 | *tempfile = '\0'; | |
827 | } | |
0774fcf8 | 828 | if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1])) |
7e6c2178 | 829 | strcat (tempfile, "/"); |
5711b547 RS |
830 | if ('/' == DIRECTORY_SEP) |
831 | dostounix_filename (tempfile); | |
832 | else | |
833 | unixtodos_filename (tempfile); | |
0774fcf8 RS |
834 | #ifdef WINDOWSNT |
835 | strcat (tempfile, "emXXXXXX"); | |
836 | #else | |
7e6c2178 | 837 | strcat (tempfile, "detmp.XXX"); |
0774fcf8 | 838 | #endif |
bad95d8f | 839 | #else /* not DOS_NT */ |
30d57b8e | 840 | char *tempfile = (char *) alloca (XSTRING (Vtemp_file_name_pattern)->size_byte + 1); |
8abd035b | 841 | bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile, |
30d57b8e | 842 | XSTRING (Vtemp_file_name_pattern)->size_byte + 1); |
bad95d8f | 843 | #endif /* not DOS_NT */ |
7e6c2178 | 844 | |
80856e74 JB |
845 | mktemp (tempfile); |
846 | ||
847 | filename_string = build_string (tempfile); | |
39323a7e | 848 | GCPRO1 (filename_string); |
80856e74 JB |
849 | start = args[0]; |
850 | end = args[1]; | |
32d08644 | 851 | /* Decide coding-system of the contents of the temporary file. */ |
bad95d8f | 852 | #ifdef DOS_NT |
91489411 RS |
853 | /* This is to cause find-buffer-file-type-coding-system (see |
854 | dos-w32.el) to choose correct EOL translation for write-region. */ | |
093650fe | 855 | specbind (Qbuffer_file_type, Vbinary_process_input); |
91489411 RS |
856 | #endif |
857 | if (!NILP (Vcoding_system_for_write)) | |
858 | val = Vcoding_system_for_write; | |
859 | else if (NILP (current_buffer->enable_multibyte_characters)) | |
32d08644 KH |
860 | val = Qnil; |
861 | else | |
beacaab3 | 862 | { |
91489411 RS |
863 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); |
864 | args2[0] = Qcall_process_region; | |
865 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
866 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); | |
867 | if (CONSP (coding_systems)) | |
868 | val = XCONS (coding_systems)->cdr; | |
869 | else if (CONSP (Vdefault_process_coding_system)) | |
870 | val = XCONS (Vdefault_process_coding_system)->cdr; | |
beacaab3 | 871 | else |
91489411 | 872 | val = Qnil; |
beacaab3 | 873 | } |
32d08644 KH |
874 | |
875 | #ifdef DOS_NT | |
91489411 RS |
876 | /* binary-process-input tells whether the buffer needs to be |
877 | written with EOL conversions, but it doesn't say anything | |
878 | about the rest of text encoding. It takes effect whenever | |
879 | the coding system doesn't otherwise specify what to do for | |
880 | eol conversion. */ | |
881 | if (NILP (val)) | |
beacaab3 | 882 | { |
91489411 RS |
883 | if (! NILP (Vbinary_process_input)) |
884 | val = intern ("undecided-unix"); | |
beacaab3 | 885 | else |
91489411 | 886 | val = intern ("undecided-dos"); |
beacaab3 | 887 | } |
0f8b8c50 | 888 | else if (SYMBOLP (val) && NILP (Vcoding_system_for_write)) |
91489411 RS |
889 | { |
890 | Lisp_Object eolval; | |
891 | eolval = Fget (val, Qeol_type); | |
892 | if (VECTORP (eolval) && XVECTOR (eolval)->size > 1) | |
893 | /* Use element 1 (CRLF conversion) for "text", | |
894 | and element 0 (LF conversion) for "binary". */ | |
895 | val = XVECTOR (eolval)->contents[NILP (Vbinary_process_input)]; | |
896 | } | |
897 | #endif | |
898 | ||
899 | specbind (intern ("coding-system-for-write"), val); | |
7d88962c | 900 | Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil); |
91489411 RS |
901 | |
902 | /* Note that Fcall_process takes care of binding | |
903 | coding-system-for-read. */ | |
093650fe | 904 | |
80856e74 JB |
905 | record_unwind_protect (delete_temp_file, filename_string); |
906 | ||
012c6fcb | 907 | if (!NILP (args[3])) |
80856e74 JB |
908 | Fdelete_region (start, end); |
909 | ||
910 | args[3] = filename_string; | |
80856e74 | 911 | |
39323a7e | 912 | RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs - 2, args + 2))); |
80856e74 JB |
913 | } |
914 | \f | |
915 | #ifndef VMS /* VMS version is in vmsproc.c. */ | |
916 | ||
917 | /* This is the last thing run in a newly forked inferior | |
918 | either synchronous or asynchronous. | |
919 | Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2. | |
920 | Initialize inferior's priority, pgrp, connected dir and environment. | |
921 | then exec another program based on new_argv. | |
922 | ||
923 | This function may change environ for the superior process. | |
924 | Therefore, the superior process must save and restore the value | |
925 | of environ around the vfork and the call to this function. | |
926 | ||
927 | ENV is the environment for the subprocess. | |
928 | ||
929 | SET_PGRP is nonzero if we should put the subprocess into a separate | |
e576cab4 JB |
930 | process group. |
931 | ||
932 | CURRENT_DIR is an elisp string giving the path of the current | |
933 | directory the subprocess should have. Since we can't really signal | |
934 | a decent error from within the child, this should be verified as an | |
935 | executable directory by the parent. */ | |
80856e74 | 936 | |
e576cab4 | 937 | child_setup (in, out, err, new_argv, set_pgrp, current_dir) |
80856e74 JB |
938 | int in, out, err; |
939 | register char **new_argv; | |
80856e74 | 940 | int set_pgrp; |
e576cab4 | 941 | Lisp_Object current_dir; |
80856e74 | 942 | { |
7e6c2178 RS |
943 | #ifdef MSDOS |
944 | /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system | |
945 | instead. */ | |
946 | #else /* not MSDOS */ | |
e576cab4 | 947 | char **env; |
7fcf7f05 | 948 | char *pwd_var; |
bad95d8f RS |
949 | #ifdef WINDOWSNT |
950 | int cpid; | |
4252a4bd | 951 | HANDLE handles[3]; |
bad95d8f | 952 | #endif /* WINDOWSNT */ |
e576cab4 | 953 | |
33abe2d9 | 954 | int pid = getpid (); |
80856e74 | 955 | |
68d10241 | 956 | #ifdef SET_EMACS_PRIORITY |
4f0b9d49 JB |
957 | { |
958 | extern int emacs_priority; | |
959 | ||
68d10241 RS |
960 | if (emacs_priority < 0) |
961 | nice (- emacs_priority); | |
4f0b9d49 | 962 | } |
5b633aeb | 963 | #endif |
80856e74 JB |
964 | |
965 | #ifdef subprocesses | |
966 | /* Close Emacs's descriptors that this process should not have. */ | |
967 | close_process_descs (); | |
968 | #endif | |
4458cebe | 969 | close_load_descs (); |
80856e74 JB |
970 | |
971 | /* Note that use of alloca is always safe here. It's obvious for systems | |
972 | that do not have true vfork or that have true (stack) alloca. | |
973 | If using vfork and C_ALLOCA it is safe because that changes | |
974 | the superior's static variables as if the superior had done alloca | |
975 | and will be cleaned up in the usual way. */ | |
e576cab4 | 976 | { |
7fcf7f05 | 977 | register char *temp; |
e576cab4 | 978 | register int i; |
77d78be1 | 979 | |
30d57b8e | 980 | i = XSTRING (current_dir)->size_byte; |
7fcf7f05 RS |
981 | pwd_var = (char *) alloca (i + 6); |
982 | temp = pwd_var + 4; | |
983 | bcopy ("PWD=", pwd_var, 4); | |
e576cab4 | 984 | bcopy (XSTRING (current_dir)->data, temp, i); |
bad95d8f | 985 | if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP; |
e576cab4 JB |
986 | temp[i] = 0; |
987 | ||
b4c7684c | 988 | #ifndef WINDOWSNT |
e576cab4 JB |
989 | /* We can't signal an Elisp error here; we're in a vfork. Since |
990 | the callers check the current directory before forking, this | |
991 | should only return an error if the directory's permissions | |
992 | are changed between the check and this chdir, but we should | |
993 | at least check. */ | |
994 | if (chdir (temp) < 0) | |
20b25e46 | 995 | _exit (errno); |
b4c7684c | 996 | #endif |
7fcf7f05 RS |
997 | |
998 | /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */ | |
bad95d8f | 999 | while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1])) |
7fcf7f05 | 1000 | temp[--i] = 0; |
e576cab4 | 1001 | } |
80856e74 | 1002 | |
80856e74 JB |
1003 | /* Set `env' to a vector of the strings in Vprocess_environment. */ |
1004 | { | |
1005 | register Lisp_Object tem; | |
1006 | register char **new_env; | |
1007 | register int new_length; | |
1008 | ||
1009 | new_length = 0; | |
1010 | for (tem = Vprocess_environment; | |
d50d3dc8 | 1011 | CONSP (tem) && STRINGP (XCONS (tem)->car); |
80856e74 JB |
1012 | tem = XCONS (tem)->cdr) |
1013 | new_length++; | |
1014 | ||
7fcf7f05 RS |
1015 | /* new_length + 2 to include PWD and terminating 0. */ |
1016 | env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *)); | |
1017 | ||
1018 | /* If we have a PWD envvar, pass one down, | |
1019 | but with corrected value. */ | |
1020 | if (getenv ("PWD")) | |
1021 | *new_env++ = pwd_var; | |
80856e74 | 1022 | |
cd9565ba | 1023 | /* Copy the Vprocess_environment strings into new_env. */ |
80856e74 | 1024 | for (tem = Vprocess_environment; |
d50d3dc8 | 1025 | CONSP (tem) && STRINGP (XCONS (tem)->car); |
80856e74 | 1026 | tem = XCONS (tem)->cdr) |
cd9565ba RS |
1027 | { |
1028 | char **ep = env; | |
1029 | char *string = (char *) XSTRING (XCONS (tem)->car)->data; | |
1030 | /* See if this string duplicates any string already in the env. | |
1031 | If so, don't put it in. | |
1032 | When an env var has multiple definitions, | |
1033 | we keep the definition that comes first in process-environment. */ | |
1034 | for (; ep != new_env; ep++) | |
1035 | { | |
1036 | char *p = *ep, *q = string; | |
1037 | while (1) | |
1038 | { | |
1039 | if (*q == 0) | |
1040 | /* The string is malformed; might as well drop it. */ | |
1041 | goto duplicate; | |
1042 | if (*q != *p) | |
1043 | break; | |
1044 | if (*q == '=') | |
1045 | goto duplicate; | |
1046 | p++, q++; | |
1047 | } | |
1048 | } | |
1049 | *new_env++ = string; | |
1050 | duplicate: ; | |
1051 | } | |
80856e74 JB |
1052 | *new_env = 0; |
1053 | } | |
bad95d8f RS |
1054 | #ifdef WINDOWSNT |
1055 | prepare_standard_handles (in, out, err, handles); | |
b4c7684c | 1056 | set_process_dir (XSTRING (current_dir)->data); |
bad95d8f | 1057 | #else /* not WINDOWSNT */ |
426b37ae JB |
1058 | /* Make sure that in, out, and err are not actually already in |
1059 | descriptors zero, one, or two; this could happen if Emacs is | |
7e6c2178 | 1060 | started with its standard in, out, or error closed, as might |
426b37ae | 1061 | happen under X. */ |
f29f9e4a RS |
1062 | { |
1063 | int oin = in, oout = out; | |
1064 | ||
1065 | /* We have to avoid relocating the same descriptor twice! */ | |
1066 | ||
1067 | in = relocate_fd (in, 3); | |
1068 | ||
1069 | if (out == oin) | |
1070 | out = in; | |
1071 | else | |
3e9367e7 | 1072 | out = relocate_fd (out, 3); |
f29f9e4a RS |
1073 | |
1074 | if (err == oin) | |
1075 | err = in; | |
1076 | else if (err == oout) | |
1077 | err = out; | |
1078 | else | |
3e9367e7 | 1079 | err = relocate_fd (err, 3); |
f29f9e4a | 1080 | } |
426b37ae | 1081 | |
80856e74 JB |
1082 | close (0); |
1083 | close (1); | |
1084 | close (2); | |
1085 | ||
1086 | dup2 (in, 0); | |
1087 | dup2 (out, 1); | |
1088 | dup2 (err, 2); | |
1089 | close (in); | |
1090 | close (out); | |
1091 | close (err); | |
bad95d8f | 1092 | #endif /* not WINDOWSNT */ |
80856e74 | 1093 | |
6b2cd868 | 1094 | #if defined(USG) && !defined(BSD_PGRPS) |
fdba8590 | 1095 | #ifndef SETPGRP_RELEASES_CTTY |
e576cab4 | 1096 | setpgrp (); /* No arguments but equivalent in this case */ |
fdba8590 | 1097 | #endif |
e576cab4 JB |
1098 | #else |
1099 | setpgrp (pid, pid); | |
1100 | #endif /* USG */ | |
a129418f RS |
1101 | /* setpgrp_of_tty is incorrect here; it uses input_fd. */ |
1102 | EMACS_SET_TTY_PGRP (0, &pid); | |
80856e74 JB |
1103 | |
1104 | #ifdef vipc | |
1105 | something missing here; | |
1106 | #endif /* vipc */ | |
1107 | ||
bad95d8f RS |
1108 | #ifdef WINDOWSNT |
1109 | /* Spawn the child. (See ntproc.c:Spawnve). */ | |
1110 | cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env); | |
db77d785 | 1111 | reset_standard_handles (in, out, err, handles); |
ff27bfbe KH |
1112 | if (cpid == -1) |
1113 | /* An error occurred while trying to spawn the process. */ | |
1114 | report_file_error ("Spawning child process", Qnil); | |
bad95d8f RS |
1115 | return cpid; |
1116 | #else /* not WINDOWSNT */ | |
80856e74 JB |
1117 | /* execvp does not accept an environment arg so the only way |
1118 | to pass this environment is to set environ. Our caller | |
1119 | is responsible for restoring the ambient value of environ. */ | |
1120 | environ = env; | |
1121 | execvp (new_argv[0], new_argv); | |
1122 | ||
f040c0ba | 1123 | write (1, "Can't exec program: ", 20); |
80856e74 | 1124 | write (1, new_argv[0], strlen (new_argv[0])); |
d20b8af6 | 1125 | write (1, "\n", 1); |
80856e74 | 1126 | _exit (1); |
bad95d8f | 1127 | #endif /* not WINDOWSNT */ |
7e6c2178 | 1128 | #endif /* not MSDOS */ |
80856e74 JB |
1129 | } |
1130 | ||
426b37ae JB |
1131 | /* Move the file descriptor FD so that its number is not less than MIN. |
1132 | If the file descriptor is moved at all, the original is freed. */ | |
1133 | int | |
1134 | relocate_fd (fd, min) | |
1135 | int fd, min; | |
1136 | { | |
1137 | if (fd >= min) | |
1138 | return fd; | |
1139 | else | |
1140 | { | |
1141 | int new = dup (fd); | |
1142 | if (new == -1) | |
1143 | { | |
20c018a0 | 1144 | char *message1 = "Error while setting up child: "; |
826c56ac | 1145 | char *errmessage = strerror (errno); |
20c018a0 JB |
1146 | char *message2 = "\n"; |
1147 | write (2, message1, strlen (message1)); | |
826c56ac | 1148 | write (2, errmessage, strlen (errmessage)); |
20c018a0 | 1149 | write (2, message2, strlen (message2)); |
426b37ae JB |
1150 | _exit (1); |
1151 | } | |
1152 | /* Note that we hold the original FD open while we recurse, | |
1153 | to guarantee we'll get a new FD if we need it. */ | |
1154 | new = relocate_fd (new, min); | |
1155 | close (fd); | |
1156 | return new; | |
1157 | } | |
1158 | } | |
1159 | ||
012c6fcb JA |
1160 | static int |
1161 | getenv_internal (var, varlen, value, valuelen) | |
1162 | char *var; | |
1163 | int varlen; | |
1164 | char **value; | |
1165 | int *valuelen; | |
1166 | { | |
1167 | Lisp_Object scan; | |
1168 | ||
1169 | for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr) | |
1170 | { | |
c1350752 KH |
1171 | Lisp_Object entry; |
1172 | ||
1173 | entry = XCONS (scan)->car; | |
d50d3dc8 | 1174 | if (STRINGP (entry) |
30d57b8e | 1175 | && XSTRING (entry)->size_byte > varlen |
012c6fcb | 1176 | && XSTRING (entry)->data[varlen] == '=' |
bad95d8f RS |
1177 | #ifdef WINDOWSNT |
1178 | /* NT environment variables are case insensitive. */ | |
a9971c6d | 1179 | && ! strnicmp (XSTRING (entry)->data, var, varlen) |
bad95d8f | 1180 | #else /* not WINDOWSNT */ |
a9971c6d | 1181 | && ! bcmp (XSTRING (entry)->data, var, varlen) |
bad95d8f | 1182 | #endif /* not WINDOWSNT */ |
a9971c6d | 1183 | ) |
012c6fcb JA |
1184 | { |
1185 | *value = (char *) XSTRING (entry)->data + (varlen + 1); | |
30d57b8e | 1186 | *valuelen = XSTRING (entry)->size_byte - (varlen + 1); |
012c6fcb JA |
1187 | return 1; |
1188 | } | |
1189 | } | |
1190 | ||
1191 | return 0; | |
1192 | } | |
1193 | ||
0ad477db | 1194 | DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0, |
012c6fcb JA |
1195 | "Return the value of environment variable VAR, as a string.\n\ |
1196 | VAR should be a string. Value is nil if VAR is undefined in the environment.\n\ | |
1197 | This function consults the variable ``process-environment'' for its value.") | |
1198 | (var) | |
1199 | Lisp_Object var; | |
1200 | { | |
1201 | char *value; | |
1202 | int valuelen; | |
1203 | ||
1204 | CHECK_STRING (var, 0); | |
30d57b8e | 1205 | if (getenv_internal (XSTRING (var)->data, XSTRING (var)->size_byte, |
012c6fcb JA |
1206 | &value, &valuelen)) |
1207 | return make_string (value, valuelen); | |
1208 | else | |
1209 | return Qnil; | |
1210 | } | |
1211 | ||
1212 | /* A version of getenv that consults process_environment, easily | |
e576cab4 | 1213 | callable from C. */ |
012c6fcb JA |
1214 | char * |
1215 | egetenv (var) | |
e576cab4 | 1216 | char *var; |
012c6fcb JA |
1217 | { |
1218 | char *value; | |
1219 | int valuelen; | |
1220 | ||
1221 | if (getenv_internal (var, strlen (var), &value, &valuelen)) | |
1222 | return value; | |
1223 | else | |
1224 | return 0; | |
1225 | } | |
1226 | ||
80856e74 JB |
1227 | #endif /* not VMS */ |
1228 | \f | |
8de15d69 | 1229 | /* This is run before init_cmdargs. */ |
7e6c2178 | 1230 | |
8de15d69 RS |
1231 | init_callproc_1 () |
1232 | { | |
1233 | char *data_dir = egetenv ("EMACSDATA"); | |
35a2f4b8 KH |
1234 | char *doc_dir = egetenv ("EMACSDOC"); |
1235 | ||
8de15d69 | 1236 | Vdata_directory |
7e6c2178 | 1237 | = Ffile_name_as_directory (build_string (data_dir ? data_dir |
8de15d69 | 1238 | : PATH_DATA)); |
35a2f4b8 KH |
1239 | Vdoc_directory |
1240 | = Ffile_name_as_directory (build_string (doc_dir ? doc_dir | |
1241 | : PATH_DOC)); | |
9453ea7b | 1242 | |
e576cab4 JB |
1243 | /* Check the EMACSPATH environment variable, defaulting to the |
1244 | PATH_EXEC path from paths.h. */ | |
1245 | Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC); | |
80856e74 JB |
1246 | Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path)); |
1247 | Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path); | |
8de15d69 RS |
1248 | } |
1249 | ||
e17f7533 | 1250 | /* This is run after init_cmdargs, when Vinstallation_directory is valid. */ |
8de15d69 RS |
1251 | |
1252 | init_callproc () | |
1253 | { | |
1254 | char *data_dir = egetenv ("EMACSDATA"); | |
1255 | ||
1256 | register char * sh; | |
1257 | Lisp_Object tempdir; | |
1258 | ||
05630743 | 1259 | if (initialized && !NILP (Vinstallation_directory)) |
8de15d69 | 1260 | { |
05630743 RS |
1261 | /* Add to the path the lib-src subdir of the installation dir. */ |
1262 | Lisp_Object tem; | |
1263 | tem = Fexpand_file_name (build_string ("lib-src"), | |
1264 | Vinstallation_directory); | |
1265 | if (NILP (Fmember (tem, Vexec_path))) | |
8de15d69 | 1266 | { |
bad95d8f | 1267 | #ifndef DOS_NT |
1a6640ec | 1268 | /* MSDOS uses wrapped binaries, so don't do this. */ |
8de15d69 RS |
1269 | Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil)); |
1270 | Vexec_directory = Ffile_name_as_directory (tem); | |
bad95d8f | 1271 | #endif /* not DOS_NT */ |
e17f7533 | 1272 | } |
8de15d69 | 1273 | |
e17f7533 RS |
1274 | /* Maybe use ../etc as well as ../lib-src. */ |
1275 | if (data_dir == 0) | |
1276 | { | |
1277 | tem = Fexpand_file_name (build_string ("etc"), | |
1278 | Vinstallation_directory); | |
1279 | Vdoc_directory = Ffile_name_as_directory (tem); | |
8de15d69 RS |
1280 | } |
1281 | } | |
7e933683 RS |
1282 | |
1283 | /* Look for the files that should be in etc. We don't use | |
1284 | Vinstallation_directory, because these files are never installed | |
e17f7533 | 1285 | near the executable, and they are never in the build |
7e933683 RS |
1286 | directory when that's different from the source directory. |
1287 | ||
1288 | Instead, if these files are not in the nominal place, we try the | |
1289 | source directory. */ | |
1290 | if (data_dir == 0) | |
1291 | { | |
1292 | Lisp_Object tem, tem1, newdir; | |
1293 | ||
1294 | tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory); | |
1295 | tem1 = Ffile_exists_p (tem); | |
1296 | if (NILP (tem1)) | |
1297 | { | |
1298 | newdir = Fexpand_file_name (build_string ("../etc/"), | |
1299 | build_string (PATH_DUMPLOADSEARCH)); | |
1300 | tem = Fexpand_file_name (build_string ("GNU"), newdir); | |
1301 | tem1 = Ffile_exists_p (tem); | |
1302 | if (!NILP (tem1)) | |
1303 | Vdata_directory = newdir; | |
1304 | } | |
1305 | } | |
80856e74 | 1306 | |
d883eb62 RS |
1307 | #ifndef CANNOT_DUMP |
1308 | if (initialized) | |
1309 | #endif | |
1310 | { | |
1311 | tempdir = Fdirectory_file_name (Vexec_directory); | |
1312 | if (access (XSTRING (tempdir)->data, 0) < 0) | |
1313 | dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n", | |
1314 | Vexec_directory); | |
1315 | } | |
80856e74 | 1316 | |
e576cab4 JB |
1317 | tempdir = Fdirectory_file_name (Vdata_directory); |
1318 | if (access (XSTRING (tempdir)->data, 0) < 0) | |
76d5c6cf RS |
1319 | dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n", |
1320 | Vdata_directory); | |
e576cab4 | 1321 | |
80856e74 JB |
1322 | #ifdef VMS |
1323 | Vshell_file_name = build_string ("*dcl*"); | |
1324 | #else | |
e576cab4 | 1325 | sh = (char *) getenv ("SHELL"); |
80856e74 JB |
1326 | Vshell_file_name = build_string (sh ? sh : "/bin/sh"); |
1327 | #endif | |
8abd035b RS |
1328 | |
1329 | #ifdef VMS | |
1330 | Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX."); | |
1331 | #else | |
1332 | if (getenv ("TMPDIR")) | |
1333 | { | |
1334 | char *dir = getenv ("TMPDIR"); | |
1335 | Vtemp_file_name_pattern | |
1336 | = Fexpand_file_name (build_string ("emacsXXXXXX"), | |
1337 | build_string (dir)); | |
1338 | } | |
1339 | else | |
1340 | Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX"); | |
1341 | #endif | |
9fefd2ba JB |
1342 | } |
1343 | ||
1344 | set_process_environment () | |
1345 | { | |
1346 | register char **envp; | |
80856e74 | 1347 | |
80856e74 JB |
1348 | Vprocess_environment = Qnil; |
1349 | #ifndef CANNOT_DUMP | |
1350 | if (initialized) | |
1351 | #endif | |
1352 | for (envp = environ; *envp; envp++) | |
1353 | Vprocess_environment = Fcons (build_string (*envp), | |
1354 | Vprocess_environment); | |
80856e74 JB |
1355 | } |
1356 | ||
1357 | syms_of_callproc () | |
1358 | { | |
bad95d8f | 1359 | #ifdef DOS_NT |
093650fe RS |
1360 | Qbuffer_file_type = intern ("buffer-file-type"); |
1361 | staticpro (&Qbuffer_file_type); | |
1362 | ||
1363 | DEFVAR_LISP ("binary-process-input", &Vbinary_process_input, | |
1364 | "*If non-nil then new subprocesses are assumed to take binary input."); | |
1365 | Vbinary_process_input = Qnil; | |
1366 | ||
1367 | DEFVAR_LISP ("binary-process-output", &Vbinary_process_output, | |
7e6c2178 | 1368 | "*If non-nil then new subprocesses are assumed to produce binary output."); |
093650fe | 1369 | Vbinary_process_output = Qnil; |
bad95d8f | 1370 | #endif /* DOS_NT */ |
7e6c2178 | 1371 | |
80856e74 JB |
1372 | DEFVAR_LISP ("shell-file-name", &Vshell_file_name, |
1373 | "*File name to load inferior shells from.\n\ | |
1374 | Initialized from the SHELL environment variable."); | |
1375 | ||
1376 | DEFVAR_LISP ("exec-path", &Vexec_path, | |
1377 | "*List of directories to search programs to run in subprocesses.\n\ | |
1378 | Each element is a string (directory name) or nil (try default directory)."); | |
1379 | ||
1380 | DEFVAR_LISP ("exec-directory", &Vexec_directory, | |
e576cab4 JB |
1381 | "Directory of architecture-dependent files that come with GNU Emacs,\n\ |
1382 | especially executable programs intended for Emacs to invoke."); | |
1383 | ||
1384 | DEFVAR_LISP ("data-directory", &Vdata_directory, | |
1385 | "Directory of architecture-independent files that come with GNU Emacs,\n\ | |
1386 | intended for Emacs to use."); | |
80856e74 | 1387 | |
35a2f4b8 KH |
1388 | DEFVAR_LISP ("doc-directory", &Vdoc_directory, |
1389 | "Directory containing the DOC file that comes with GNU Emacs.\n\ | |
1390 | This is usually the same as data-directory."); | |
1391 | ||
ed61592a JB |
1392 | DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory, |
1393 | "For internal use by the build procedure only.\n\ | |
1394 | This is the name of the directory in which the build procedure installed\n\ | |
1395 | Emacs's info files; the default value for Info-default-directory-list\n\ | |
1396 | includes this."); | |
1397 | Vconfigure_info_directory = build_string (PATH_INFO); | |
1398 | ||
8abd035b RS |
1399 | DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern, |
1400 | "Pattern for making names for temporary files.\n\ | |
1401 | This is used by `call-process-region'."); | |
0537ec48 | 1402 | /* This variable is initialized in init_callproc. */ |
8abd035b | 1403 | |
80856e74 | 1404 | DEFVAR_LISP ("process-environment", &Vprocess_environment, |
e576cab4 JB |
1405 | "List of environment variables for subprocesses to inherit.\n\ |
1406 | Each element should be a string of the form ENVVARNAME=VALUE.\n\ | |
1407 | The environment which Emacs inherits is placed in this variable\n\ | |
1408 | when Emacs starts."); | |
80856e74 JB |
1409 | |
1410 | #ifndef VMS | |
1411 | defsubr (&Scall_process); | |
012c6fcb | 1412 | defsubr (&Sgetenv); |
986ffb24 | 1413 | #endif |
e576cab4 | 1414 | defsubr (&Scall_process_region); |
80856e74 | 1415 | } |