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