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