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