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