Fix typo in previous change.
[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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <signal.h>
e576cab4 22#include <errno.h>
80856e74 23
18160b98 24#include <config.h>
565620a5 25#include <stdio.h>
80856e74 26
426b37ae 27extern int errno;
826c56ac 28extern char *strerror ();
426b37ae 29
80856e74
JB
30/* Define SIGCHLD as an alias for SIGCLD. */
31
32#if !defined (SIGCHLD) && defined (SIGCLD)
33#define SIGCHLD SIGCLD
34#endif /* SIGCLD */
35
36#include <sys/types.h>
88a64fef 37
80856e74
JB
38#include <sys/file.h>
39#ifdef USG5
472e83fe 40#define INCLUDED_FCNTL
80856e74
JB
41#include <fcntl.h>
42#endif
43
bad95d8f
RS
44#ifdef WINDOWSNT
45#define NOMINMAX
46#include <windows.h>
47#include <stdlib.h> /* for proper declaration of environ */
48#include <fcntl.h>
49#include "nt.h"
50#define _P_NOWAIT 1 /* from process.h */
51#endif
52
7e6c2178 53#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
00353d4f 54#include "msdos.h"
472e83fe 55#define INCLUDED_FCNTL
7e6c2178
RS
56#include <fcntl.h>
57#include <sys/stat.h>
58#include <sys/param.h>
59#include <errno.h>
60#endif /* MSDOS */
61
80856e74
JB
62#ifndef O_RDONLY
63#define O_RDONLY 0
64#endif
65
66#ifndef O_WRONLY
67#define O_WRONLY 1
68#endif
69
70#include "lisp.h"
71#include "commands.h"
72#include "buffer.h"
2a6b3537 73#include <paths.h>
80856e74 74#include "process.h"
d177f194 75#include "syssignal.h"
a129418f 76#include "systty.h"
80856e74
JB
77
78#ifdef VMS
79extern noshare char **environ;
80#else
81extern char **environ;
82#endif
83
84#define max(a, b) ((a) > (b) ? (a) : (b))
85
bad95d8f 86#ifdef DOS_NT
093650fe
RS
87/* When we are starting external processes we need to know whether they
88 take binary input (no conversion) or text input (\n is converted to
89 \r\n). Similar for output: if newlines are written as \r\n then it's
90 text process output, otherwise it's binary. */
91Lisp_Object Vbinary_process_input;
92Lisp_Object Vbinary_process_output;
bad95d8f 93#endif /* DOS_NT */
7e6c2178 94
35a2f4b8 95Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
ed61592a 96Lisp_Object Vconfigure_info_directory;
80856e74
JB
97
98Lisp_Object Vshell_file_name;
99
80856e74 100Lisp_Object Vprocess_environment;
80856e74 101
bad95d8f 102#ifdef DOS_NT
093650fe 103Lisp_Object Qbuffer_file_type;
bad95d8f 104#endif /* DOS_NT */
093650fe 105
80856e74
JB
106/* True iff we are about to fork off a synchronous process or if we
107 are waiting for it. */
108int synch_process_alive;
109
110/* Nonzero => this is a string explaining death of synchronous subprocess. */
111char *synch_process_death;
112
113/* If synch_process_death is zero,
114 this is exit code of synchronous subprocess. */
115int synch_process_retcode;
8de15d69
RS
116
117extern Lisp_Object Vdoc_file_name;
80856e74 118\f
37d54121
RS
119/* Clean up when exiting Fcall_process.
120 On MSDOS, delete the temporary file on any kind of termination.
121 On Unix, kill the process and any children on termination by signal. */
122
123/* Nonzero if this is termination due to exit. */
124static int call_process_exited;
125
80856e74
JB
126#ifndef VMS /* VMS version is in vmsproc.c. */
127
d177f194
JB
128static Lisp_Object
129call_process_kill (fdpid)
130 Lisp_Object fdpid;
131{
132 close (XFASTINT (Fcar (fdpid)));
133 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
134 synch_process_alive = 0;
135 return Qnil;
136}
137
80856e74
JB
138Lisp_Object
139call_process_cleanup (fdpid)
140 Lisp_Object fdpid;
141{
7e6c2178
RS
142#ifdef MSDOS
143 /* for MSDOS fdpid is really (fd . tempfile) */
c1350752
KH
144 register Lisp_Object file;
145 file = Fcdr (fdpid);
7e6c2178
RS
146 close (XFASTINT (Fcar (fdpid)));
147 if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0)
148 unlink (XSTRING (file)->data);
149#else /* not MSDOS */
d177f194
JB
150 register int pid = XFASTINT (Fcdr (fdpid));
151
6b6e798b 152
37d54121 153 if (call_process_exited)
6b6e798b
RS
154 {
155 close (XFASTINT (Fcar (fdpid)));
156 return Qnil;
157 }
37d54121 158
d177f194
JB
159 if (EMACS_KILLPG (pid, SIGINT) == 0)
160 {
161 int count = specpdl_ptr - specpdl;
162 record_unwind_protect (call_process_kill, fdpid);
163 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
164 immediate_quit = 1;
165 QUIT;
166 wait_for_termination (pid);
167 immediate_quit = 0;
168 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
169 message1 ("Waiting for process to die...done");
170 }
80856e74 171 synch_process_alive = 0;
d177f194 172 close (XFASTINT (Fcar (fdpid)));
7e6c2178 173#endif /* not MSDOS */
80856e74
JB
174 return Qnil;
175}
176
177DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
178 "Call PROGRAM synchronously in separate process.\n\
179The program's input comes from file INFILE (nil means `/dev/null').\n\
180Insert output in BUFFER before point; t means current buffer;\n\
181 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
39eaa782
RS
182BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
183REAL-BUFFER says what to do with standard output, as above,\n\
184while STDERR-FILE says what to do with standard error in the child.\n\
185STDERR-FILE may be nil (discard standard error output),\n\
186t (mix it with ordinary output), or a file name string.\n\
187\n\
80856e74
JB
188Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
189Remaining arguments are strings passed as command arguments to PROGRAM.\n\
39eaa782
RS
190\n\
191If BUFFER is 0, `call-process' returns immediately with value nil.\n\
192Otherwise it waits for PROGRAM to terminate\n\
e576cab4 193and returns a numeric exit status or a signal description string.\n\
d177f194 194If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
80856e74
JB
195 (nargs, args)
196 int nargs;
197 register Lisp_Object *args;
198{
58616e67 199 Lisp_Object infile, buffer, current_dir, display, path;
80856e74
JB
200 int fd[2];
201 int filefd;
202 register int pid;
203 char buf[1024];
204 int count = specpdl_ptr - specpdl;
205 register unsigned char **new_argv
206 = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
207 struct buffer *old = current_buffer;
39eaa782
RS
208 /* File to use for stderr in the child.
209 t means use same as standard output. */
210 Lisp_Object error_file;
7e6c2178
RS
211#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
212 char *outf, *tempfile;
213 int outfilefd;
214#endif
80856e74
JB
215#if 0
216 int mask;
217#endif
80856e74
JB
218 CHECK_STRING (args[0], 0);
219
39eaa782
RS
220 error_file = Qt;
221
7e6c2178
RS
222#ifndef subprocesses
223 /* Without asynchronous processes we cannot have BUFFER == 0. */
d50d3dc8 224 if (nargs >= 3 && INTEGERP (args[2]))
7e6c2178
RS
225 error ("Operating system cannot handle asynchronous subprocesses");
226#endif /* subprocesses */
227
e576cab4
JB
228 if (nargs >= 2 && ! NILP (args[1]))
229 {
230 infile = Fexpand_file_name (args[1], current_buffer->directory);
231 CHECK_STRING (infile, 1);
232 }
80856e74 233 else
5437e9f9 234 infile = build_string (NULL_DEVICE);
80856e74 235
e576cab4
JB
236 if (nargs >= 3)
237 {
39eaa782
RS
238 buffer = args[2];
239
240 /* If BUFFER is a list, its meaning is
241 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
242 if (CONSP (buffer))
243 {
244 if (CONSP (XCONS (buffer)->cdr))
245 error_file = XCONS (XCONS (buffer)->cdr)->car;
246 buffer = XCONS (buffer)->car;
247 }
044512ed 248
39eaa782
RS
249 if (!(EQ (buffer, Qnil)
250 || EQ (buffer, Qt)
251 || XFASTINT (buffer) == 0))
e576cab4 252 {
39eaa782
RS
253 Lisp_Object spec_buffer;
254 spec_buffer = buffer;
255 buffer = Fget_buffer (buffer);
256 /* Mention the buffer name for a better error message. */
257 if (NILP (buffer))
258 CHECK_BUFFER (spec_buffer, 2);
e576cab4
JB
259 CHECK_BUFFER (buffer, 2);
260 }
261 }
262 else
263 buffer = Qnil;
80856e74 264
58616e67
JB
265 /* Make sure that the child will be able to chdir to the current
266 buffer's current directory, or its unhandled equivalent. We
267 can't just have the child check for an error when it does the
268 chdir, since it's in a vfork.
269
270 We have to GCPRO around this because Fexpand_file_name,
271 Funhandled_file_name_directory, and Ffile_accessible_directory_p
272 might call a file name handling function. The argument list is
273 protected by the caller, so all we really have to worry about is
274 buffer. */
275 {
276 struct gcpro gcpro1, gcpro2, gcpro3;
277
278 current_dir = current_buffer->directory;
279
280 GCPRO3 (infile, buffer, current_dir);
281
c52b0b34
KH
282 current_dir
283 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
284 Qnil);
58616e67
JB
285 if (NILP (Ffile_accessible_directory_p (current_dir)))
286 report_file_error ("Setting current directory",
287 Fcons (current_buffer->directory, Qnil));
288
289 UNGCPRO;
290 }
291
e576cab4 292 display = nargs >= 4 ? args[3] : Qnil;
80856e74 293
e576cab4 294 filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
80856e74
JB
295 if (filefd < 0)
296 {
e576cab4 297 report_file_error ("Opening process input file", Fcons (infile, Qnil));
80856e74
JB
298 }
299 /* Search for program; barf if not found. */
c52b0b34
KH
300 {
301 struct gcpro gcpro1;
302
303 GCPRO1 (current_dir);
304 openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
305 UNGCPRO;
306 }
012c6fcb 307 if (NILP (path))
80856e74
JB
308 {
309 close (filefd);
310 report_file_error ("Searching for program", Fcons (args[0], Qnil));
311 }
312 new_argv[0] = XSTRING (path)->data;
c52b0b34
KH
313 {
314 register int i;
315 for (i = 4; i < nargs; i++)
316 {
317 CHECK_STRING (args[i], i);
318 new_argv[i - 3] = XSTRING (args[i])->data;
319 }
320 new_argv[i - 3] = 0;
321 }
80856e74 322
7e6c2178
RS
323#ifdef MSDOS /* MW, July 1993 */
324 /* These vars record information from process termination.
325 Clear them now before process can possibly terminate,
326 to avoid timing error if process terminates soon. */
327 synch_process_death = 0;
328 synch_process_retcode = 0;
329
330 if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
331 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
332 else
333 {
334 tempfile = alloca (20);
335 *tempfile = '\0';
336 }
337 dostounix_filename (tempfile);
338 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
339 strcat (tempfile, "/");
340 strcat (tempfile, "detmp.XXX");
341 mktemp (tempfile);
342
343 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
344 if (outfilefd < 0)
345 {
346 close (filefd);
347 report_file_error ("Opening process output file", Fcons (tempfile, Qnil));
348 }
349#endif
350
d50d3dc8 351 if (INTEGERP (buffer))
5437e9f9 352 fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
80856e74
JB
353 else
354 {
7e6c2178 355#ifndef MSDOS
bad95d8f
RS
356#ifdef WINDOWSNT
357 pipe_with_inherited_out (fd);
358#else /* not WINDOWSNT */
80856e74 359 pipe (fd);
bad95d8f 360#endif /* not WINDOWSNT */
7e6c2178 361#endif
80856e74
JB
362#if 0
363 /* Replaced by close_process_descs */
364 set_exclusive_use (fd[0]);
365#endif
366 }
367
368 {
369 /* child_setup must clobber environ in systems with true vfork.
370 Protect it from permanent change. */
371 register char **save_environ = environ;
372 register int fd1 = fd[1];
39eaa782 373 int fd_error = fd1;
80856e74
JB
374
375#if 0 /* Some systems don't have sigblock. */
e065a56e 376 mask = sigblock (sigmask (SIGCHLD));
80856e74
JB
377#endif
378
379 /* Record that we're about to create a synchronous process. */
380 synch_process_alive = 1;
381
5c03767e
RS
382 /* These vars record information from process termination.
383 Clear them now before process can possibly terminate,
384 to avoid timing error if process terminates soon. */
385 synch_process_death = 0;
386 synch_process_retcode = 0;
387
7e6c2178 388#ifdef MSDOS /* MW, July 1993 */
6b6e798b
RS
389 /* ??? Someone who knows MSDOG needs to check whether this properly
390 closes all descriptors that it opens. */
7e6c2178
RS
391 pid = run_msdos_command (new_argv, current_dir, filefd, outfilefd);
392 close (outfilefd);
393 fd1 = -1; /* No harm in closing that one! */
093650fe 394 fd[0] = open (tempfile, NILP (Vbinary_process_output) ? O_TEXT : O_BINARY);
7e6c2178
RS
395 if (fd[0] < 0)
396 {
397 unlink (tempfile);
6b6e798b 398 close (filefd);
7e6c2178
RS
399 report_file_error ("Cannot re-open temporary file", Qnil);
400 }
401#else /* not MSDOS */
39eaa782
RS
402
403 if (NILP (error_file))
404 fd_error = open (NULL_DEVICE, O_WRONLY);
405 else if (STRINGP (error_file))
406 {
407#ifdef DOS_NT
408 fd_error = open (XSTRING (error_file)->data,
409 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
410 S_IREAD | S_IWRITE);
411#else /* not DOS_NT */
412 fd_error = creat (XSTRING (error_file)->data, 0666);
413#endif /* not DOS_NT */
414 }
415
416 if (fd_error < 0)
417 {
418 close (filefd);
419 close (fd[0]);
420 if (fd1 >= 0)
421 close (fd1);
422 report_file_error ("Cannot open", error_file);
423 }
424
bad95d8f 425#ifdef WINDOWSNT
39eaa782 426 pid = child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
bad95d8f 427#else /* not WINDOWSNT */
80856e74
JB
428 pid = vfork ();
429
430 if (pid == 0)
431 {
432 if (fd[0] >= 0)
433 close (fd[0]);
5a570e37 434#ifdef USG
80856e74
JB
435 setpgrp ();
436#else
437 setpgrp (pid, pid);
438#endif /* USG */
39eaa782 439 child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
80856e74 440 }
7e6c2178 441#endif /* not MSDOS */
bad95d8f 442#endif /* not WINDOWSNT */
80856e74 443
80856e74
JB
444 environ = save_environ;
445
6b6e798b
RS
446 /* Close most of our fd's, but not fd[0]
447 since we will use that to read input from. */
80856e74 448 close (filefd);
7e6c2178
RS
449 if (fd1 >= 0)
450 close (fd1);
80856e74
JB
451 }
452
453 if (pid < 0)
454 {
6b6e798b
RS
455 if (fd[0] >= 0)
456 close (fd[0]);
80856e74
JB
457 report_file_error ("Doing vfork", Qnil);
458 }
459
d50d3dc8 460 if (INTEGERP (buffer))
80856e74 461 {
6b6e798b
RS
462 if (fd[0] >= 0)
463 close (fd[0]);
80856e74 464#ifndef subprocesses
e576cab4
JB
465 /* If Emacs has been built with asynchronous subprocess support,
466 we don't need to do this, I think because it will then have
467 the facilities for handling SIGCHLD. */
80856e74
JB
468 wait_without_blocking ();
469#endif /* subprocesses */
80856e74
JB
470 return Qnil;
471 }
472
6b6e798b 473 /* Enable sending signal if user quits below. */
37d54121
RS
474 call_process_exited = 0;
475
7e6c2178
RS
476#ifdef MSDOS
477 /* MSDOS needs different cleanup information. */
478 record_unwind_protect (call_process_cleanup,
479 Fcons (make_number (fd[0]), build_string (tempfile)));
480#else
80856e74
JB
481 record_unwind_protect (call_process_cleanup,
482 Fcons (make_number (fd[0]), make_number (pid)));
7e6c2178 483#endif /* not MSDOS */
80856e74
JB
484
485
d50d3dc8 486 if (BUFFERP (buffer))
80856e74
JB
487 Fset_buffer (buffer);
488
489 immediate_quit = 1;
490 QUIT;
491
492 {
493 register int nread;
0ad477db 494 int first = 1;
80856e74 495
00fb3e95 496 while ((nread = read (fd[0], buf, sizeof buf)) != 0)
80856e74 497 {
00fb3e95
RS
498 if (nread < 0)
499 {
500#if defined (__osf__) && defined (__alpha)
501 continue; /* Work around bug in DEC OSF/1 V3.0. */
502#else
503 break;
504#endif
505 }
80856e74 506 immediate_quit = 0;
012c6fcb 507 if (!NILP (buffer))
80856e74 508 insert (buf, nread);
012c6fcb 509 if (!NILP (display) && INTERACTIVE)
0ad477db
RS
510 {
511 if (first)
512 prepare_menu_bars ();
513 first = 0;
514 redisplay_preserve_echo_area ();
515 }
80856e74
JB
516 immediate_quit = 1;
517 QUIT;
518 }
519 }
520
521 /* Wait for it to terminate, unless it already has. */
522 wait_for_termination (pid);
523
524 immediate_quit = 0;
525
526 set_buffer_internal (old);
527
37d54121
RS
528 /* Don't kill any children that the subprocess may have left behind
529 when exiting. */
530 call_process_exited = 1;
531
80856e74
JB
532 unbind_to (count, Qnil);
533
80856e74
JB
534 if (synch_process_death)
535 return build_string (synch_process_death);
536 return make_number (synch_process_retcode);
537}
538#endif
539\f
9fefd2ba 540static Lisp_Object
80856e74
JB
541delete_temp_file (name)
542 Lisp_Object name;
543{
2e3dc201 544 /* Use Fdelete_file (indirectly) because that runs a file name handler.
59750d69 545 We did that when writing the file, so we should do so when deleting. */
2e3dc201 546 internal_delete_file (name);
80856e74
JB
547}
548
549DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
550 3, MANY, 0,
551 "Send text from START to END to a synchronous process running PROGRAM.\n\
552Delete the text if fourth arg DELETE is non-nil.\n\
39eaa782 553\n\
80856e74
JB
554Insert output in BUFFER before point; t means current buffer;\n\
555 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
39eaa782
RS
556BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
557REAL-BUFFER says what to do with standard output, as above,\n\
558while STDERR-FILE says what to do with standard error in the child.\n\
559STDERR-FILE may be nil (discard standard error output),\n\
560t (mix it with ordinary output), or a file name string.\n\
561\n\
80856e74
JB
562Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
563Remaining args are passed to PROGRAM at startup as command args.\n\
39eaa782
RS
564\n\
565If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
566Otherwise it waits for PROGRAM to terminate\n\
e576cab4 567and returns a numeric exit status or a signal description string.\n\
d177f194 568If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
80856e74
JB
569 (nargs, args)
570 int nargs;
571 register Lisp_Object *args;
572{
39323a7e
KH
573 struct gcpro gcpro1;
574 Lisp_Object filename_string;
575 register Lisp_Object start, end;
bad95d8f 576#ifdef DOS_NT
7e6c2178
RS
577 char *tempfile;
578#else
80856e74 579 char tempfile[20];
7e6c2178 580#endif
80856e74 581 int count = specpdl_ptr - specpdl;
bad95d8f 582#ifdef DOS_NT
7e6c2178
RS
583 char *outf = '\0';
584
585 if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
586 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
587 else
588 {
589 tempfile = alloca (20);
590 *tempfile = '\0';
591 }
592 dostounix_filename (tempfile);
593 if (tempfile[strlen (tempfile) - 1] != '/')
594 strcat (tempfile, "/");
595 strcat (tempfile, "detmp.XXX");
bad95d8f 596#else /* not DOS_NT */
80856e74
JB
597
598#ifdef VMS
599 strcpy (tempfile, "tmp:emacsXXXXXX.");
600#else
601 strcpy (tempfile, "/tmp/emacsXXXXXX");
602#endif
bad95d8f 603#endif /* not DOS_NT */
7e6c2178 604
80856e74
JB
605 mktemp (tempfile);
606
607 filename_string = build_string (tempfile);
39323a7e 608 GCPRO1 (filename_string);
80856e74
JB
609 start = args[0];
610 end = args[1];
bad95d8f 611#ifdef DOS_NT
093650fe
RS
612 specbind (Qbuffer_file_type, Vbinary_process_input);
613 Fwrite_region (start, end, filename_string, Qnil, Qlambda);
614 unbind_to (count, Qnil);
bad95d8f 615#else /* not DOS_NT */
80856e74 616 Fwrite_region (start, end, filename_string, Qnil, Qlambda);
bad95d8f 617#endif /* not DOS_NT */
093650fe 618
80856e74
JB
619 record_unwind_protect (delete_temp_file, filename_string);
620
012c6fcb 621 if (!NILP (args[3]))
80856e74
JB
622 Fdelete_region (start, end);
623
624 args[3] = filename_string;
80856e74 625
39323a7e 626 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs - 2, args + 2)));
80856e74
JB
627}
628\f
629#ifndef VMS /* VMS version is in vmsproc.c. */
630
631/* This is the last thing run in a newly forked inferior
632 either synchronous or asynchronous.
633 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
634 Initialize inferior's priority, pgrp, connected dir and environment.
635 then exec another program based on new_argv.
636
637 This function may change environ for the superior process.
638 Therefore, the superior process must save and restore the value
639 of environ around the vfork and the call to this function.
640
641 ENV is the environment for the subprocess.
642
643 SET_PGRP is nonzero if we should put the subprocess into a separate
e576cab4
JB
644 process group.
645
646 CURRENT_DIR is an elisp string giving the path of the current
647 directory the subprocess should have. Since we can't really signal
648 a decent error from within the child, this should be verified as an
649 executable directory by the parent. */
80856e74 650
e576cab4 651child_setup (in, out, err, new_argv, set_pgrp, current_dir)
80856e74
JB
652 int in, out, err;
653 register char **new_argv;
80856e74 654 int set_pgrp;
e576cab4 655 Lisp_Object current_dir;
80856e74 656{
7e6c2178
RS
657#ifdef MSDOS
658 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
659 instead. */
660#else /* not MSDOS */
e576cab4 661 char **env;
7fcf7f05 662 char *pwd_var;
bad95d8f
RS
663#ifdef WINDOWSNT
664 int cpid;
665 HANDLE handles[4];
666#endif /* WINDOWSNT */
e576cab4 667
33abe2d9 668 int pid = getpid ();
80856e74 669
68d10241 670#ifdef SET_EMACS_PRIORITY
4f0b9d49
JB
671 {
672 extern int emacs_priority;
673
68d10241
RS
674 if (emacs_priority < 0)
675 nice (- emacs_priority);
4f0b9d49 676 }
5b633aeb 677#endif
80856e74
JB
678
679#ifdef subprocesses
680 /* Close Emacs's descriptors that this process should not have. */
681 close_process_descs ();
682#endif
4458cebe 683 close_load_descs ();
80856e74
JB
684
685 /* Note that use of alloca is always safe here. It's obvious for systems
686 that do not have true vfork or that have true (stack) alloca.
687 If using vfork and C_ALLOCA it is safe because that changes
688 the superior's static variables as if the superior had done alloca
689 and will be cleaned up in the usual way. */
e576cab4 690 {
7fcf7f05 691 register char *temp;
e576cab4 692 register int i;
77d78be1 693
e576cab4 694 i = XSTRING (current_dir)->size;
7fcf7f05
RS
695 pwd_var = (char *) alloca (i + 6);
696 temp = pwd_var + 4;
697 bcopy ("PWD=", pwd_var, 4);
e576cab4 698 bcopy (XSTRING (current_dir)->data, temp, i);
bad95d8f 699 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
e576cab4
JB
700 temp[i] = 0;
701
702 /* We can't signal an Elisp error here; we're in a vfork. Since
703 the callers check the current directory before forking, this
704 should only return an error if the directory's permissions
705 are changed between the check and this chdir, but we should
706 at least check. */
707 if (chdir (temp) < 0)
20b25e46 708 _exit (errno);
7fcf7f05
RS
709
710 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
bad95d8f 711 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
7fcf7f05 712 temp[--i] = 0;
e576cab4 713 }
80856e74 714
80856e74
JB
715 /* Set `env' to a vector of the strings in Vprocess_environment. */
716 {
717 register Lisp_Object tem;
718 register char **new_env;
719 register int new_length;
720
721 new_length = 0;
722 for (tem = Vprocess_environment;
d50d3dc8 723 CONSP (tem) && STRINGP (XCONS (tem)->car);
80856e74
JB
724 tem = XCONS (tem)->cdr)
725 new_length++;
726
7fcf7f05
RS
727 /* new_length + 2 to include PWD and terminating 0. */
728 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
729
730 /* If we have a PWD envvar, pass one down,
731 but with corrected value. */
732 if (getenv ("PWD"))
733 *new_env++ = pwd_var;
80856e74 734
cd9565ba 735 /* Copy the Vprocess_environment strings into new_env. */
80856e74 736 for (tem = Vprocess_environment;
d50d3dc8 737 CONSP (tem) && STRINGP (XCONS (tem)->car);
80856e74 738 tem = XCONS (tem)->cdr)
cd9565ba
RS
739 {
740 char **ep = env;
741 char *string = (char *) XSTRING (XCONS (tem)->car)->data;
742 /* See if this string duplicates any string already in the env.
743 If so, don't put it in.
744 When an env var has multiple definitions,
745 we keep the definition that comes first in process-environment. */
746 for (; ep != new_env; ep++)
747 {
748 char *p = *ep, *q = string;
749 while (1)
750 {
751 if (*q == 0)
752 /* The string is malformed; might as well drop it. */
753 goto duplicate;
754 if (*q != *p)
755 break;
756 if (*q == '=')
757 goto duplicate;
758 p++, q++;
759 }
760 }
761 *new_env++ = string;
762 duplicate: ;
763 }
80856e74
JB
764 *new_env = 0;
765 }
bad95d8f
RS
766#ifdef WINDOWSNT
767 prepare_standard_handles (in, out, err, handles);
768#else /* not WINDOWSNT */
426b37ae
JB
769 /* Make sure that in, out, and err are not actually already in
770 descriptors zero, one, or two; this could happen if Emacs is
7e6c2178 771 started with its standard in, out, or error closed, as might
426b37ae
JB
772 happen under X. */
773 in = relocate_fd (in, 3);
3e9367e7
KH
774 if (out == err)
775 err = out = relocate_fd (out, 3);
776 else
777 {
778 out = relocate_fd (out, 3);
779 err = relocate_fd (err, 3);
780 }
426b37ae 781
80856e74
JB
782 close (0);
783 close (1);
784 close (2);
785
786 dup2 (in, 0);
787 dup2 (out, 1);
788 dup2 (err, 2);
789 close (in);
790 close (out);
791 close (err);
bad95d8f 792#endif /* not WINDOWSNT */
80856e74 793
fdba8590
RS
794#ifdef USG
795#ifndef SETPGRP_RELEASES_CTTY
e576cab4 796 setpgrp (); /* No arguments but equivalent in this case */
fdba8590 797#endif
e576cab4
JB
798#else
799 setpgrp (pid, pid);
800#endif /* USG */
a129418f
RS
801 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
802 EMACS_SET_TTY_PGRP (0, &pid);
80856e74
JB
803
804#ifdef vipc
805 something missing here;
806#endif /* vipc */
807
bad95d8f
RS
808#ifdef WINDOWSNT
809 /* Spawn the child. (See ntproc.c:Spawnve). */
810 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
ff27bfbe
KH
811 if (cpid == -1)
812 /* An error occurred while trying to spawn the process. */
813 report_file_error ("Spawning child process", Qnil);
bad95d8f
RS
814 reset_standard_handles (in, out, err, handles);
815 return cpid;
816#else /* not WINDOWSNT */
80856e74
JB
817 /* execvp does not accept an environment arg so the only way
818 to pass this environment is to set environ. Our caller
819 is responsible for restoring the ambient value of environ. */
820 environ = env;
821 execvp (new_argv[0], new_argv);
822
823 write (1, "Couldn't exec the program ", 26);
824 write (1, new_argv[0], strlen (new_argv[0]));
825 _exit (1);
bad95d8f 826#endif /* not WINDOWSNT */
7e6c2178 827#endif /* not MSDOS */
80856e74
JB
828}
829
426b37ae
JB
830/* Move the file descriptor FD so that its number is not less than MIN.
831 If the file descriptor is moved at all, the original is freed. */
832int
833relocate_fd (fd, min)
834 int fd, min;
835{
836 if (fd >= min)
837 return fd;
838 else
839 {
840 int new = dup (fd);
841 if (new == -1)
842 {
20c018a0 843 char *message1 = "Error while setting up child: ";
826c56ac 844 char *errmessage = strerror (errno);
20c018a0
JB
845 char *message2 = "\n";
846 write (2, message1, strlen (message1));
826c56ac 847 write (2, errmessage, strlen (errmessage));
20c018a0 848 write (2, message2, strlen (message2));
426b37ae
JB
849 _exit (1);
850 }
851 /* Note that we hold the original FD open while we recurse,
852 to guarantee we'll get a new FD if we need it. */
853 new = relocate_fd (new, min);
854 close (fd);
855 return new;
856 }
857}
858
012c6fcb
JA
859static int
860getenv_internal (var, varlen, value, valuelen)
861 char *var;
862 int varlen;
863 char **value;
864 int *valuelen;
865{
866 Lisp_Object scan;
867
868 for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
869 {
c1350752
KH
870 Lisp_Object entry;
871
872 entry = XCONS (scan)->car;
d50d3dc8 873 if (STRINGP (entry)
012c6fcb
JA
874 && XSTRING (entry)->size > varlen
875 && XSTRING (entry)->data[varlen] == '='
bad95d8f
RS
876#ifdef WINDOWSNT
877 /* NT environment variables are case insensitive. */
878 && ! strnicmp (XSTRING (entry)->data, var, varlen))
879#else /* not WINDOWSNT */
012c6fcb 880 && ! bcmp (XSTRING (entry)->data, var, varlen))
bad95d8f 881#endif /* not WINDOWSNT */
012c6fcb
JA
882 {
883 *value = (char *) XSTRING (entry)->data + (varlen + 1);
884 *valuelen = XSTRING (entry)->size - (varlen + 1);
885 return 1;
886 }
887 }
888
889 return 0;
890}
891
0ad477db 892DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
012c6fcb
JA
893 "Return the value of environment variable VAR, as a string.\n\
894VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
895This function consults the variable ``process-environment'' for its value.")
896 (var)
897 Lisp_Object var;
898{
899 char *value;
900 int valuelen;
901
902 CHECK_STRING (var, 0);
903 if (getenv_internal (XSTRING (var)->data, XSTRING (var)->size,
904 &value, &valuelen))
905 return make_string (value, valuelen);
906 else
907 return Qnil;
908}
909
910/* A version of getenv that consults process_environment, easily
e576cab4 911 callable from C. */
012c6fcb
JA
912char *
913egetenv (var)
e576cab4 914 char *var;
012c6fcb
JA
915{
916 char *value;
917 int valuelen;
918
919 if (getenv_internal (var, strlen (var), &value, &valuelen))
920 return value;
921 else
922 return 0;
923}
924
80856e74
JB
925#endif /* not VMS */
926\f
8de15d69 927/* This is run before init_cmdargs. */
7e6c2178 928
8de15d69
RS
929init_callproc_1 ()
930{
931 char *data_dir = egetenv ("EMACSDATA");
35a2f4b8
KH
932 char *doc_dir = egetenv ("EMACSDOC");
933
8de15d69 934 Vdata_directory
7e6c2178 935 = Ffile_name_as_directory (build_string (data_dir ? data_dir
8de15d69 936 : PATH_DATA));
35a2f4b8
KH
937 Vdoc_directory
938 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
939 : PATH_DOC));
9453ea7b 940
e576cab4
JB
941 /* Check the EMACSPATH environment variable, defaulting to the
942 PATH_EXEC path from paths.h. */
943 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
80856e74
JB
944 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
945 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
8de15d69
RS
946}
947
948/* This is run after init_cmdargs, so that Vinvocation_directory is valid. */
949
950init_callproc ()
951{
952 char *data_dir = egetenv ("EMACSDATA");
953
954 register char * sh;
955 Lisp_Object tempdir;
956
05630743 957 if (initialized && !NILP (Vinstallation_directory))
8de15d69 958 {
05630743
RS
959 /* Add to the path the lib-src subdir of the installation dir. */
960 Lisp_Object tem;
961 tem = Fexpand_file_name (build_string ("lib-src"),
962 Vinstallation_directory);
963 if (NILP (Fmember (tem, Vexec_path)))
8de15d69 964 {
bad95d8f 965#ifndef DOS_NT
1a6640ec 966 /* MSDOS uses wrapped binaries, so don't do this. */
8de15d69
RS
967 Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
968 Vexec_directory = Ffile_name_as_directory (tem);
bad95d8f 969#endif /* not DOS_NT */
8de15d69
RS
970
971 /* If we use ../lib-src, maybe use ../etc as well.
972 Do so if ../etc exists and has our DOC-... file in it. */
973 if (data_dir == 0)
974 {
05630743
RS
975 tem = Fexpand_file_name (build_string ("etc"),
976 Vinstallation_directory);
7e933683 977 Vdoc_directory = Ffile_name_as_directory (tem);
8de15d69
RS
978 }
979 }
980 }
7e933683
RS
981
982 /* Look for the files that should be in etc. We don't use
983 Vinstallation_directory, because these files are never installed
984 in /bin near the executable, and they are never in the build
985 directory when that's different from the source directory.
986
987 Instead, if these files are not in the nominal place, we try the
988 source directory. */
989 if (data_dir == 0)
990 {
991 Lisp_Object tem, tem1, newdir;
992
993 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
994 tem1 = Ffile_exists_p (tem);
995 if (NILP (tem1))
996 {
997 newdir = Fexpand_file_name (build_string ("../etc/"),
998 build_string (PATH_DUMPLOADSEARCH));
999 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1000 tem1 = Ffile_exists_p (tem);
1001 if (!NILP (tem1))
1002 Vdata_directory = newdir;
1003 }
1004 }
80856e74 1005
e576cab4
JB
1006 tempdir = Fdirectory_file_name (Vexec_directory);
1007 if (access (XSTRING (tempdir)->data, 0) < 0)
80856e74 1008 {
0af6a831
RS
1009 fprintf (stderr,
1010 "Warning: arch-dependent data dir (%s) does not exist.\n",
1011 XSTRING (Vexec_directory)->data);
80856e74
JB
1012 sleep (2);
1013 }
1014
e576cab4
JB
1015 tempdir = Fdirectory_file_name (Vdata_directory);
1016 if (access (XSTRING (tempdir)->data, 0) < 0)
1017 {
0af6a831
RS
1018 fprintf (stderr,
1019 "Warning: arch-independent data dir (%s) does not exist.\n",
1020 XSTRING (Vdata_directory)->data);
e576cab4
JB
1021 sleep (2);
1022 }
1023
80856e74
JB
1024#ifdef VMS
1025 Vshell_file_name = build_string ("*dcl*");
1026#else
e576cab4 1027 sh = (char *) getenv ("SHELL");
80856e74
JB
1028 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1029#endif
9fefd2ba
JB
1030}
1031
1032set_process_environment ()
1033{
1034 register char **envp;
80856e74 1035
80856e74
JB
1036 Vprocess_environment = Qnil;
1037#ifndef CANNOT_DUMP
1038 if (initialized)
1039#endif
1040 for (envp = environ; *envp; envp++)
1041 Vprocess_environment = Fcons (build_string (*envp),
1042 Vprocess_environment);
80856e74
JB
1043}
1044
1045syms_of_callproc ()
1046{
bad95d8f 1047#ifdef DOS_NT
093650fe
RS
1048 Qbuffer_file_type = intern ("buffer-file-type");
1049 staticpro (&Qbuffer_file_type);
1050
1051 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input,
1052 "*If non-nil then new subprocesses are assumed to take binary input.");
1053 Vbinary_process_input = Qnil;
1054
1055 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output,
7e6c2178 1056 "*If non-nil then new subprocesses are assumed to produce binary output.");
093650fe 1057 Vbinary_process_output = Qnil;
bad95d8f 1058#endif /* DOS_NT */
7e6c2178 1059
80856e74
JB
1060 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1061 "*File name to load inferior shells from.\n\
1062Initialized from the SHELL environment variable.");
1063
1064 DEFVAR_LISP ("exec-path", &Vexec_path,
1065 "*List of directories to search programs to run in subprocesses.\n\
1066Each element is a string (directory name) or nil (try default directory).");
1067
1068 DEFVAR_LISP ("exec-directory", &Vexec_directory,
e576cab4
JB
1069 "Directory of architecture-dependent files that come with GNU Emacs,\n\
1070especially executable programs intended for Emacs to invoke.");
1071
1072 DEFVAR_LISP ("data-directory", &Vdata_directory,
1073 "Directory of architecture-independent files that come with GNU Emacs,\n\
1074intended for Emacs to use.");
80856e74 1075
35a2f4b8
KH
1076 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1077 "Directory containing the DOC file that comes with GNU Emacs.\n\
1078This is usually the same as data-directory.");
1079
ed61592a
JB
1080 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1081 "For internal use by the build procedure only.\n\
1082This is the name of the directory in which the build procedure installed\n\
1083Emacs's info files; the default value for Info-default-directory-list\n\
1084includes this.");
1085 Vconfigure_info_directory = build_string (PATH_INFO);
1086
80856e74 1087 DEFVAR_LISP ("process-environment", &Vprocess_environment,
e576cab4
JB
1088 "List of environment variables for subprocesses to inherit.\n\
1089Each element should be a string of the form ENVVARNAME=VALUE.\n\
1090The environment which Emacs inherits is placed in this variable\n\
1091when Emacs starts.");
80856e74
JB
1092
1093#ifndef VMS
1094 defsubr (&Scall_process);
012c6fcb 1095 defsubr (&Sgetenv);
986ffb24 1096#endif
e576cab4 1097 defsubr (&Scall_process_region);
80856e74 1098}