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