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