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