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