Merge from emacs--devo--0
[emacs.git] / src / callproc.c
bloba3b7b7afa4a9db2c0f8257a51564f9c8097a1a1e
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include <signal.h>
26 #include <errno.h>
27 #include <stdio.h>
29 #ifndef USE_CRT_DLL
30 extern int errno;
31 #endif
33 /* Define SIGCHLD as an alias for SIGCLD. */
35 #if !defined (SIGCHLD) && defined (SIGCLD)
36 #define SIGCHLD SIGCLD
37 #endif /* SIGCLD */
39 #include <sys/types.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
45 #include <sys/file.h>
46 #ifdef HAVE_FCNTL_H
47 #define INCLUDED_FCNTL
48 #include <fcntl.h>
49 #endif
51 #ifdef WINDOWSNT
52 #define NOMINMAX
53 #include <windows.h>
54 #include <stdlib.h> /* for proper declaration of environ */
55 #include <fcntl.h>
56 #include "w32.h"
57 #define _P_NOWAIT 1 /* from process.h */
58 #endif
60 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
61 #define INCLUDED_FCNTL
62 #include <fcntl.h>
63 #include <sys/stat.h>
64 #include <sys/param.h>
65 #include <errno.h>
66 #endif /* MSDOS */
68 #ifndef O_RDONLY
69 #define O_RDONLY 0
70 #endif
72 #ifndef O_WRONLY
73 #define O_WRONLY 1
74 #endif
76 #include "lisp.h"
77 #include "commands.h"
78 #include "buffer.h"
79 #include "character.h"
80 #include "ccl.h"
81 #include "coding.h"
82 #include "composite.h"
83 #include <epaths.h>
84 #include "process.h"
85 #include "syssignal.h"
86 #include "systty.h"
87 #include "blockinput.h"
88 #include "frame.h"
89 #include "termhooks.h"
91 #ifdef MSDOS
92 #include "msdos.h"
93 #endif
95 #ifdef VMS
96 extern noshare char **environ;
97 #else
98 #ifndef USE_CRT_DLL
99 extern char **environ;
100 #endif
101 #endif
103 #ifdef HAVE_SETPGID
104 #if !defined (USG) || defined (BSD_PGRPS)
105 #undef setpgrp
106 #define setpgrp setpgid
107 #endif
108 #endif
110 Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes;
111 Lisp_Object Vdata_directory, Vdoc_directory;
112 Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory;
113 Lisp_Object Vtemp_file_name_pattern;
115 Lisp_Object Vshell_file_name;
117 Lisp_Object Vprocess_environment, Vinitial_environment;
119 #ifdef DOS_NT
120 Lisp_Object Qbuffer_file_type;
121 #endif /* DOS_NT */
123 /* True if we are about to fork off a synchronous process or if we
124 are waiting for it. */
125 int synch_process_alive;
127 /* Nonzero => this is a string explaining death of synchronous subprocess. */
128 char *synch_process_death;
130 /* Nonzero => this is the signal number that terminated the subprocess. */
131 int synch_process_termsig;
133 /* If synch_process_death is zero,
134 this is exit code of synchronous subprocess. */
135 int synch_process_retcode;
138 /* Clean up when exiting Fcall_process.
139 On MSDOS, delete the temporary file on any kind of termination.
140 On Unix, kill the process and any children on termination by signal. */
142 /* Nonzero if this is termination due to exit. */
143 static int call_process_exited;
145 EXFUN (Fgetenv_internal, 2);
147 #ifndef VMS /* VMS version is in vmsproc.c. */
149 static Lisp_Object
150 call_process_kill (fdpid)
151 Lisp_Object fdpid;
153 emacs_close (XFASTINT (Fcar (fdpid)));
154 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
155 synch_process_alive = 0;
156 return Qnil;
159 Lisp_Object
160 call_process_cleanup (fdpid)
161 Lisp_Object fdpid;
163 #if defined (MSDOS)
164 /* for MSDOS fdpid is really (fd . tempfile) */
165 register Lisp_Object file;
166 file = Fcdr (fdpid);
167 emacs_close (XFASTINT (Fcar (fdpid)));
168 if (strcmp (SDATA (file), NULL_DEVICE) != 0)
169 unlink (SDATA (file));
170 #else /* not MSDOS */
171 register int pid = XFASTINT (Fcdr (fdpid));
173 if (call_process_exited)
175 emacs_close (XFASTINT (Fcar (fdpid)));
176 return Qnil;
179 if (EMACS_KILLPG (pid, SIGINT) == 0)
181 int count = SPECPDL_INDEX ();
182 record_unwind_protect (call_process_kill, fdpid);
183 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
184 immediate_quit = 1;
185 QUIT;
186 wait_for_termination (pid);
187 immediate_quit = 0;
188 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
189 message1 ("Waiting for process to die...done");
191 synch_process_alive = 0;
192 emacs_close (XFASTINT (Fcar (fdpid)));
193 #endif /* not MSDOS */
194 return Qnil;
197 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
198 doc: /* Call PROGRAM synchronously in separate process.
199 The remaining arguments are optional.
200 The program's input comes from file INFILE (nil means `/dev/null').
201 Insert output in BUFFER before point; t means current buffer;
202 nil for BUFFER means discard it; 0 means discard and don't wait.
203 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
204 REAL-BUFFER says what to do with standard output, as above,
205 while STDERR-FILE says what to do with standard error in the child.
206 STDERR-FILE may be nil (discard standard error output),
207 t (mix it with ordinary output), or a file name string.
209 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
210 Remaining arguments are strings passed as command arguments to PROGRAM.
212 If executable PROGRAM can't be found as an executable, `call-process'
213 signals a Lisp error. `call-process' reports errors in execution of
214 the program only through its return and output.
216 If BUFFER is 0, `call-process' returns immediately with value nil.
217 Otherwise it waits for PROGRAM to terminate
218 and returns a numeric exit status or a signal description string.
219 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
221 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
222 (nargs, args)
223 int nargs;
224 register Lisp_Object *args;
226 Lisp_Object infile, buffer, current_dir, path;
227 int display_p;
228 int fd[2];
229 int filefd;
230 register int pid;
231 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
232 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
233 char buf[CALLPROC_BUFFER_SIZE_MAX];
234 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
235 int count = SPECPDL_INDEX ();
237 register const unsigned char **new_argv
238 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
239 struct buffer *old = current_buffer;
240 /* File to use for stderr in the child.
241 t means use same as standard output. */
242 Lisp_Object error_file;
243 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
244 char *outf, *tempfile;
245 int outfilefd;
246 #endif
247 #if 0
248 int mask;
249 #endif
250 struct coding_system process_coding; /* coding-system of process output */
251 struct coding_system argument_coding; /* coding-system of arguments */
252 /* Set to the return value of Ffind_operation_coding_system. */
253 Lisp_Object coding_systems;
255 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
256 coding_systems = Qt;
258 CHECK_STRING (args[0]);
260 error_file = Qt;
262 #ifndef subprocesses
263 /* Without asynchronous processes we cannot have BUFFER == 0. */
264 if (nargs >= 3
265 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
266 error ("Operating system cannot handle asynchronous subprocesses");
267 #endif /* subprocesses */
269 /* Decide the coding-system for giving arguments. */
271 Lisp_Object val, *args2;
272 int i;
274 /* If arguments are supplied, we may have to encode them. */
275 if (nargs >= 5)
277 int must_encode = 0;
278 Lisp_Object coding_attrs;
280 for (i = 4; i < nargs; i++)
281 CHECK_STRING (args[i]);
283 for (i = 4; i < nargs; i++)
284 if (STRING_MULTIBYTE (args[i]))
285 must_encode = 1;
287 if (!NILP (Vcoding_system_for_write))
288 val = Vcoding_system_for_write;
289 else if (! must_encode)
290 val = Qnil;
291 else
293 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
294 args2[0] = Qcall_process;
295 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
296 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
297 if (CONSP (coding_systems))
298 val = XCDR (coding_systems);
299 else if (CONSP (Vdefault_process_coding_system))
300 val = XCDR (Vdefault_process_coding_system);
301 else
302 val = Qnil;
304 val = coding_inherit_eol_type (val, Qnil);
305 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
306 coding_attrs = CODING_ID_ATTRS (argument_coding.id);
307 if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
309 /* We should not use an ASCII incompatible coding system. */
310 val = raw_text_coding_system (val);
311 setup_coding_system (val, &argument_coding);
316 if (nargs >= 2 && ! NILP (args[1]))
318 infile = Fexpand_file_name (args[1], current_buffer->directory);
319 CHECK_STRING (infile);
321 else
322 infile = build_string (NULL_DEVICE);
324 if (nargs >= 3)
326 buffer = args[2];
328 /* If BUFFER is a list, its meaning is
329 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
330 if (CONSP (buffer))
332 if (CONSP (XCDR (buffer)))
334 Lisp_Object stderr_file;
335 stderr_file = XCAR (XCDR (buffer));
337 if (NILP (stderr_file) || EQ (Qt, stderr_file))
338 error_file = stderr_file;
339 else
340 error_file = Fexpand_file_name (stderr_file, Qnil);
343 buffer = XCAR (buffer);
346 if (!(EQ (buffer, Qnil)
347 || EQ (buffer, Qt)
348 || INTEGERP (buffer)))
350 Lisp_Object spec_buffer;
351 spec_buffer = buffer;
352 buffer = Fget_buffer_create (buffer);
353 /* Mention the buffer name for a better error message. */
354 if (NILP (buffer))
355 CHECK_BUFFER (spec_buffer);
356 CHECK_BUFFER (buffer);
359 else
360 buffer = Qnil;
362 /* Make sure that the child will be able to chdir to the current
363 buffer's current directory, or its unhandled equivalent. We
364 can't just have the child check for an error when it does the
365 chdir, since it's in a vfork.
367 We have to GCPRO around this because Fexpand_file_name,
368 Funhandled_file_name_directory, and Ffile_accessible_directory_p
369 might call a file name handling function. The argument list is
370 protected by the caller, so all we really have to worry about is
371 buffer. */
373 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
375 current_dir = current_buffer->directory;
377 GCPRO4 (infile, buffer, current_dir, error_file);
379 current_dir
380 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
381 Qnil);
382 if (NILP (Ffile_accessible_directory_p (current_dir)))
383 report_file_error ("Setting current directory",
384 Fcons (current_buffer->directory, Qnil));
386 if (STRING_MULTIBYTE (infile))
387 infile = ENCODE_FILE (infile);
388 if (STRING_MULTIBYTE (current_dir))
389 current_dir = ENCODE_FILE (current_dir);
390 if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
391 error_file = ENCODE_FILE (error_file);
392 UNGCPRO;
395 display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
397 filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
398 if (filefd < 0)
400 infile = DECODE_FILE (infile);
401 report_file_error ("Opening process input file", Fcons (infile, Qnil));
403 /* Search for program; barf if not found. */
405 struct gcpro gcpro1;
407 GCPRO1 (current_dir);
408 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
409 UNGCPRO;
411 if (NILP (path))
413 emacs_close (filefd);
414 report_file_error ("Searching for program", Fcons (args[0], Qnil));
417 /* If program file name starts with /: for quoting a magic name,
418 discard that. */
419 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
420 && SREF (path, 1) == ':')
421 path = Fsubstring (path, make_number (2), Qnil);
423 new_argv[0] = SDATA (path);
424 if (nargs > 4)
426 register int i;
427 struct gcpro gcpro1, gcpro2, gcpro3;
429 GCPRO3 (infile, buffer, current_dir);
430 argument_coding.dst_multibyte = 0;
431 for (i = 4; i < nargs; i++)
433 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
434 if (CODING_REQUIRE_ENCODING (&argument_coding))
435 /* We must encode this argument. */
436 args[i] = encode_coding_string (&argument_coding, args[i], 1);
437 new_argv[i - 3] = SDATA (args[i]);
439 UNGCPRO;
440 new_argv[nargs - 3] = 0;
442 else
443 new_argv[1] = 0;
445 #ifdef MSDOS /* MW, July 1993 */
446 if ((outf = egetenv ("TMPDIR")))
447 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
448 else
450 tempfile = alloca (20);
451 *tempfile = '\0';
453 dostounix_filename (tempfile);
454 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
455 strcat (tempfile, "/");
456 strcat (tempfile, "detmp.XXX");
457 mktemp (tempfile);
459 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
460 if (outfilefd < 0)
462 emacs_close (filefd);
463 report_file_error ("Opening process output file",
464 Fcons (build_string (tempfile), Qnil));
466 fd[0] = filefd;
467 fd[1] = outfilefd;
468 #endif /* MSDOS */
470 if (INTEGERP (buffer))
471 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
472 else
474 #ifndef MSDOS
475 errno = 0;
476 if (pipe (fd) == -1)
478 emacs_close (filefd);
479 report_file_error ("Creating process pipe", Qnil);
481 #endif
482 #if 0
483 /* Replaced by close_process_descs */
484 set_exclusive_use (fd[0]);
485 #endif
489 /* child_setup must clobber environ in systems with true vfork.
490 Protect it from permanent change. */
491 register char **save_environ = environ;
492 register int fd1 = fd[1];
493 int fd_error = fd1;
495 #if 0 /* Some systems don't have sigblock. */
496 mask = sigblock (sigmask (SIGCHLD));
497 #endif
499 /* Record that we're about to create a synchronous process. */
500 synch_process_alive = 1;
502 /* These vars record information from process termination.
503 Clear them now before process can possibly terminate,
504 to avoid timing error if process terminates soon. */
505 synch_process_death = 0;
506 synch_process_retcode = 0;
507 synch_process_termsig = 0;
509 if (NILP (error_file))
510 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
511 else if (STRINGP (error_file))
513 #ifdef DOS_NT
514 fd_error = emacs_open (SDATA (error_file),
515 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
516 S_IREAD | S_IWRITE);
517 #else /* not DOS_NT */
518 fd_error = creat (SDATA (error_file), 0666);
519 #endif /* not DOS_NT */
522 if (fd_error < 0)
524 emacs_close (filefd);
525 if (fd[0] != filefd)
526 emacs_close (fd[0]);
527 if (fd1 >= 0)
528 emacs_close (fd1);
529 #ifdef MSDOS
530 unlink (tempfile);
531 #endif
532 if (NILP (error_file))
533 error_file = build_string (NULL_DEVICE);
534 else if (STRINGP (error_file))
535 error_file = DECODE_FILE (error_file);
536 report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
539 #ifdef MSDOS /* MW, July 1993 */
540 /* Note that on MSDOS `child_setup' actually returns the child process
541 exit status, not its PID, so we assign it to `synch_process_retcode'
542 below. */
543 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
544 0, current_dir);
546 /* Record that the synchronous process exited and note its
547 termination status. */
548 synch_process_alive = 0;
549 synch_process_retcode = pid;
550 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
552 synchronize_system_messages_locale ();
553 synch_process_death = strerror (errno);
556 emacs_close (outfilefd);
557 if (fd_error != outfilefd)
558 emacs_close (fd_error);
559 fd1 = -1; /* No harm in closing that one! */
560 /* Since CRLF is converted to LF within `decode_coding', we can
561 always open a file with binary mode. */
562 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
563 if (fd[0] < 0)
565 unlink (tempfile);
566 emacs_close (filefd);
567 report_file_error ("Cannot re-open temporary file", Qnil);
569 #else /* not MSDOS */
570 #ifdef WINDOWSNT
571 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
572 0, current_dir);
573 #else /* not WINDOWSNT */
574 BLOCK_INPUT;
576 pid = vfork ();
578 if (pid == 0)
580 if (fd[0] >= 0)
581 emacs_close (fd[0]);
582 #ifdef HAVE_SETSID
583 setsid ();
584 #endif
585 #if defined (USG) && !defined (BSD_PGRPS)
586 setpgrp ();
587 #else
588 setpgrp (pid, pid);
589 #endif /* USG */
590 child_setup (filefd, fd1, fd_error, (char **) new_argv,
591 0, current_dir);
594 UNBLOCK_INPUT;
595 #endif /* not WINDOWSNT */
597 /* The MSDOS case did this already. */
598 if (fd_error >= 0)
599 emacs_close (fd_error);
600 #endif /* not MSDOS */
602 environ = save_environ;
604 /* Close most of our fd's, but not fd[0]
605 since we will use that to read input from. */
606 emacs_close (filefd);
607 if (fd1 >= 0 && fd1 != fd_error)
608 emacs_close (fd1);
611 if (pid < 0)
613 if (fd[0] >= 0)
614 emacs_close (fd[0]);
615 report_file_error ("Doing vfork", Qnil);
618 if (INTEGERP (buffer))
620 if (fd[0] >= 0)
621 emacs_close (fd[0]);
622 #ifndef subprocesses
623 /* If Emacs has been built with asynchronous subprocess support,
624 we don't need to do this, I think because it will then have
625 the facilities for handling SIGCHLD. */
626 wait_without_blocking ();
627 #endif /* subprocesses */
628 return Qnil;
631 /* Enable sending signal if user quits below. */
632 call_process_exited = 0;
634 #if defined(MSDOS)
635 /* MSDOS needs different cleanup information. */
636 record_unwind_protect (call_process_cleanup,
637 Fcons (make_number (fd[0]), build_string (tempfile)));
638 #else
639 record_unwind_protect (call_process_cleanup,
640 Fcons (make_number (fd[0]), make_number (pid)));
641 #endif /* not MSDOS */
644 if (BUFFERP (buffer))
645 Fset_buffer (buffer);
647 if (NILP (buffer))
649 /* If BUFFER is nil, we must read process output once and then
650 discard it, so setup coding system but with nil. */
651 setup_coding_system (Qnil, &process_coding);
653 else
655 Lisp_Object val, *args2;
657 val = Qnil;
658 if (!NILP (Vcoding_system_for_read))
659 val = Vcoding_system_for_read;
660 else
662 if (EQ (coding_systems, Qt))
664 int i;
666 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
667 args2[0] = Qcall_process;
668 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
669 coding_systems
670 = Ffind_operation_coding_system (nargs + 1, args2);
672 if (CONSP (coding_systems))
673 val = XCAR (coding_systems);
674 else if (CONSP (Vdefault_process_coding_system))
675 val = XCAR (Vdefault_process_coding_system);
676 else
677 val = Qnil;
679 Fcheck_coding_system (val);
680 /* In unibyte mode, character code conversion should not take
681 place but EOL conversion should. So, setup raw-text or one
682 of the subsidiary according to the information just setup. */
683 if (NILP (current_buffer->enable_multibyte_characters)
684 && !NILP (val))
685 val = raw_text_coding_system (val);
686 setup_coding_system (val, &process_coding);
689 immediate_quit = 1;
690 QUIT;
693 register int nread;
694 int first = 1;
695 int total_read = 0;
696 int carryover = 0;
697 int display_on_the_fly = display_p;
698 struct coding_system saved_coding;
700 saved_coding = process_coding;
701 while (1)
703 /* Repeatedly read until we've filled as much as possible
704 of the buffer size we have. But don't read
705 less than 1024--save that for the next bufferful. */
706 nread = carryover;
707 while (nread < bufsize - 1024)
709 int this_read = emacs_read (fd[0], buf + nread,
710 bufsize - nread);
712 if (this_read < 0)
713 goto give_up;
715 if (this_read == 0)
717 process_coding.mode |= CODING_MODE_LAST_BLOCK;
718 break;
721 nread += this_read;
722 total_read += this_read;
724 if (display_on_the_fly)
725 break;
728 /* Now NREAD is the total amount of data in the buffer. */
729 immediate_quit = 0;
731 if (!NILP (buffer))
733 if (NILP (current_buffer->enable_multibyte_characters)
734 && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
735 insert_1_both (buf, nread, nread, 0, 1, 0);
736 else
737 { /* We have to decode the input. */
738 Lisp_Object curbuf;
740 XSETBUFFER (curbuf, current_buffer);
741 decode_coding_c_string (&process_coding, buf, nread,
742 curbuf);
743 if (display_on_the_fly
744 && CODING_REQUIRE_DETECTION (&saved_coding)
745 && ! CODING_REQUIRE_DETECTION (&process_coding))
747 /* We have detected some coding system. But,
748 there's a possibility that the detection was
749 done by insufficient data. So, we give up
750 displaying on the fly. */
751 if (process_coding.produced > 0)
752 del_range_2 (process_coding.dst_pos,
753 process_coding.dst_pos_byte,
754 process_coding.dst_pos
755 + process_coding.produced_char,
756 process_coding.dst_pos_byte
757 + process_coding.produced, 0);
758 display_on_the_fly = 0;
759 process_coding = saved_coding;
760 carryover = nread;
761 /* This is to make the above condition always
762 fails in the future. */
763 saved_coding.common_flags
764 &= ~CODING_REQUIRE_DETECTION_MASK;
765 continue;
768 TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
769 PT_BYTE + process_coding.produced);
770 carryover = process_coding.carryover_bytes;
771 if (carryover > 0)
772 /* As CARRYOVER should not be that large, we had
773 better avoid overhead of bcopy. */
774 BCOPY_SHORT (process_coding.carryover, buf,
775 process_coding.carryover_bytes);
779 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
780 break;
782 #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
783 /* Make the buffer bigger as we continue to read more data,
784 but not past CALLPROC_BUFFER_SIZE_MAX. */
785 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
786 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
787 bufsize = CALLPROC_BUFFER_SIZE_MAX;
788 #endif
790 if (display_p)
792 if (first)
793 prepare_menu_bars ();
794 first = 0;
795 redisplay_preserve_echo_area (1);
796 /* This variable might have been set to 0 for code
797 detection. In that case, we set it back to 1 because
798 we should have already detected a coding system. */
799 display_on_the_fly = 1;
801 immediate_quit = 1;
802 QUIT;
804 give_up: ;
806 Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
807 /* If the caller required, let the buffer inherit the
808 coding-system used to decode the process output. */
809 if (inherit_process_coding_system)
810 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
811 make_number (total_read));
814 /* Wait for it to terminate, unless it already has. */
815 wait_for_termination (pid);
817 immediate_quit = 0;
819 set_buffer_internal (old);
821 /* Don't kill any children that the subprocess may have left behind
822 when exiting. */
823 call_process_exited = 1;
825 unbind_to (count, Qnil);
827 if (synch_process_termsig)
829 char *signame;
831 synchronize_system_messages_locale ();
832 signame = strsignal (synch_process_termsig);
834 if (signame == 0)
835 signame = "unknown";
837 synch_process_death = signame;
840 if (synch_process_death)
841 return code_convert_string_norecord (build_string (synch_process_death),
842 Vlocale_coding_system, 0);
843 return make_number (synch_process_retcode);
845 #endif
847 static Lisp_Object
848 delete_temp_file (name)
849 Lisp_Object name;
851 /* Suppress jka-compr handling, etc. */
852 int count = SPECPDL_INDEX ();
853 specbind (intern ("file-name-handler-alist"), Qnil);
854 internal_delete_file (name);
855 unbind_to (count, Qnil);
856 return Qnil;
859 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
860 3, MANY, 0,
861 doc: /* Send text from START to END to a synchronous process running PROGRAM.
862 The remaining arguments are optional.
863 Delete the text if fourth arg DELETE is non-nil.
865 Insert output in BUFFER before point; t means current buffer;
866 nil for BUFFER means discard it; 0 means discard and don't wait.
867 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
868 REAL-BUFFER says what to do with standard output, as above,
869 while STDERR-FILE says what to do with standard error in the child.
870 STDERR-FILE may be nil (discard standard error output),
871 t (mix it with ordinary output), or a file name string.
873 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
874 Remaining args are passed to PROGRAM at startup as command args.
876 If BUFFER is 0, `call-process-region' returns immediately with value nil.
877 Otherwise it waits for PROGRAM to terminate
878 and returns a numeric exit status or a signal description string.
879 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
881 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
882 (nargs, args)
883 int nargs;
884 register Lisp_Object *args;
886 struct gcpro gcpro1;
887 Lisp_Object filename_string;
888 register Lisp_Object start, end;
889 int count = SPECPDL_INDEX ();
890 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
891 Lisp_Object coding_systems;
892 Lisp_Object val, *args2;
893 int i;
894 #ifdef DOS_NT
895 char *tempfile;
896 char *outf = '\0';
898 if ((outf = egetenv ("TMPDIR"))
899 || (outf = egetenv ("TMP"))
900 || (outf = egetenv ("TEMP")))
901 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
902 else
904 tempfile = alloca (20);
905 *tempfile = '\0';
907 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
908 strcat (tempfile, "/");
909 if ('/' == DIRECTORY_SEP)
910 dostounix_filename (tempfile);
911 else
912 unixtodos_filename (tempfile);
913 #ifdef WINDOWSNT
914 strcat (tempfile, "emXXXXXX");
915 #else
916 strcat (tempfile, "detmp.XXX");
917 #endif
918 #else /* not DOS_NT */
919 char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
920 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
921 SBYTES (Vtemp_file_name_pattern) + 1);
922 #endif /* not DOS_NT */
924 coding_systems = Qt;
926 #ifdef HAVE_MKSTEMP
928 int fd;
930 BLOCK_INPUT;
931 fd = mkstemp (tempfile);
932 UNBLOCK_INPUT;
933 if (fd == -1)
934 report_file_error ("Failed to open temporary file",
935 Fcons (Vtemp_file_name_pattern, Qnil));
936 else
937 close (fd);
939 #else
940 mktemp (tempfile);
941 #endif
943 filename_string = build_string (tempfile);
944 GCPRO1 (filename_string);
945 start = args[0];
946 end = args[1];
947 /* Decide coding-system of the contents of the temporary file. */
948 if (!NILP (Vcoding_system_for_write))
949 val = Vcoding_system_for_write;
950 else if (NILP (current_buffer->enable_multibyte_characters))
951 val = Qnil;
952 else
954 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
955 args2[0] = Qcall_process_region;
956 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
957 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
958 if (CONSP (coding_systems))
959 val = XCDR (coding_systems);
960 else if (CONSP (Vdefault_process_coding_system))
961 val = XCDR (Vdefault_process_coding_system);
962 else
963 val = Qnil;
967 int count1 = SPECPDL_INDEX ();
969 specbind (intern ("coding-system-for-write"), val);
970 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
971 happen to get a ".Z" suffix. */
972 specbind (intern ("file-name-handler-alist"), Qnil);
973 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
975 unbind_to (count1, Qnil);
978 /* Note that Fcall_process takes care of binding
979 coding-system-for-read. */
981 record_unwind_protect (delete_temp_file, filename_string);
983 if (nargs > 3 && !NILP (args[3]))
984 Fdelete_region (start, end);
986 if (nargs > 3)
988 args += 2;
989 nargs -= 2;
991 else
993 args[0] = args[2];
994 nargs = 2;
996 args[1] = filename_string;
998 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
1001 #ifndef VMS /* VMS version is in vmsproc.c. */
1003 static int relocate_fd ();
1005 static char **
1006 add_env (char **env, char **new_env, char *string)
1008 char **ep;
1009 int ok = 1;
1010 if (string == NULL)
1011 return new_env;
1013 /* See if this string duplicates any string already in the env.
1014 If so, don't put it in.
1015 When an env var has multiple definitions,
1016 we keep the definition that comes first in process-environment. */
1017 for (ep = env; ok && ep != new_env; ep++)
1019 char *p = *ep, *q = string;
1020 while (ok)
1022 if (*q != *p)
1023 break;
1024 if (*q == 0)
1025 /* The string is a lone variable name; keep it for now, we
1026 will remove it later. It is a placeholder for a
1027 variable that is not to be included in the environment. */
1028 break;
1029 if (*q == '=')
1030 ok = 0;
1031 p++, q++;
1034 if (ok)
1035 *new_env++ = string;
1036 return new_env;
1039 /* This is the last thing run in a newly forked inferior
1040 either synchronous or asynchronous.
1041 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1042 Initialize inferior's priority, pgrp, connected dir and environment.
1043 then exec another program based on new_argv.
1045 This function may change environ for the superior process.
1046 Therefore, the superior process must save and restore the value
1047 of environ around the vfork and the call to this function.
1049 SET_PGRP is nonzero if we should put the subprocess into a separate
1050 process group.
1052 CURRENT_DIR is an elisp string giving the path of the current
1053 directory the subprocess should have. Since we can't really signal
1054 a decent error from within the child, this should be verified as an
1055 executable directory by the parent. */
1058 child_setup (in, out, err, new_argv, set_pgrp, current_dir)
1059 int in, out, err;
1060 register char **new_argv;
1061 int set_pgrp;
1062 Lisp_Object current_dir;
1064 char **env;
1065 char *pwd_var;
1066 #ifdef WINDOWSNT
1067 int cpid;
1068 HANDLE handles[3];
1069 #endif /* WINDOWSNT */
1071 int pid = getpid ();
1073 #ifdef SET_EMACS_PRIORITY
1075 extern EMACS_INT emacs_priority;
1077 if (emacs_priority < 0)
1078 nice (- emacs_priority);
1080 #endif
1082 #ifdef subprocesses
1083 /* Close Emacs's descriptors that this process should not have. */
1084 close_process_descs ();
1085 #endif
1086 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1087 we will lose if we call close_load_descs here. */
1088 #ifndef DOS_NT
1089 close_load_descs ();
1090 #endif
1092 /* Note that use of alloca is always safe here. It's obvious for systems
1093 that do not have true vfork or that have true (stack) alloca.
1094 If using vfork and C_ALLOCA (when Emacs used to include
1095 src/alloca.c) it is safe because that changes the superior's
1096 static variables as if the superior had done alloca and will be
1097 cleaned up in the usual way. */
1099 register char *temp;
1100 register int i;
1102 i = SBYTES (current_dir);
1103 #ifdef MSDOS
1104 /* MSDOS must have all environment variables malloc'ed, because
1105 low-level libc functions that launch subsidiary processes rely
1106 on that. */
1107 pwd_var = (char *) xmalloc (i + 6);
1108 #else
1109 pwd_var = (char *) alloca (i + 6);
1110 #endif
1111 temp = pwd_var + 4;
1112 bcopy ("PWD=", pwd_var, 4);
1113 bcopy (SDATA (current_dir), temp, i);
1114 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
1115 temp[i] = 0;
1117 #ifndef DOS_NT
1118 /* We can't signal an Elisp error here; we're in a vfork. Since
1119 the callers check the current directory before forking, this
1120 should only return an error if the directory's permissions
1121 are changed between the check and this chdir, but we should
1122 at least check. */
1123 if (chdir (temp) < 0)
1124 _exit (errno);
1125 #endif
1127 #ifdef DOS_NT
1128 /* Get past the drive letter, so that d:/ is left alone. */
1129 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1131 temp += 2;
1132 i -= 2;
1134 #endif
1136 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1137 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
1138 temp[--i] = 0;
1141 /* Set `env' to a vector of the strings in the environment. */
1143 register Lisp_Object tem;
1144 register char **new_env;
1145 char **p, **q;
1146 register int new_length;
1147 Lisp_Object display = Qnil;
1149 new_length = 0;
1151 for (tem = Vprocess_environment;
1152 CONSP (tem) && STRINGP (XCAR (tem));
1153 tem = XCDR (tem))
1155 if (strncmp (SDATA (XCAR (tem)), "DISPLAY", 7) == 0
1156 && (SDATA (XCAR (tem)) [7] == '\0'
1157 || SDATA (XCAR (tem)) [7] == '='))
1158 /* DISPLAY is specified in process-environment. */
1159 display = Qt;
1160 new_length++;
1163 /* If not provided yet, use the frame's DISPLAY. */
1164 if (NILP (display))
1166 Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
1167 if (!STRINGP (tmp) && CONSP (Vinitial_environment))
1168 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1169 tmp = Fgetenv_internal (build_string ("DISPLAY"),
1170 Vinitial_environment);
1171 if (STRINGP (tmp))
1173 display = tmp;
1174 new_length++;
1178 /* new_length + 2 to include PWD and terminating 0. */
1179 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1180 /* If we have a PWD envvar, pass one down,
1181 but with corrected value. */
1182 if (egetenv ("PWD"))
1183 *new_env++ = pwd_var;
1185 if (STRINGP (display))
1187 int vlen = strlen ("DISPLAY=") + strlen (SDATA (display)) + 1;
1188 char *vdata = (char *) alloca (vlen);
1189 strcpy (vdata, "DISPLAY=");
1190 strcat (vdata, SDATA (display));
1191 new_env = add_env (env, new_env, vdata);
1194 /* Overrides. */
1195 for (tem = Vprocess_environment;
1196 CONSP (tem) && STRINGP (XCAR (tem));
1197 tem = XCDR (tem))
1198 new_env = add_env (env, new_env, SDATA (XCAR (tem)));
1200 *new_env = 0;
1202 /* Remove variable names without values. */
1203 p = q = env;
1204 while (*p != 0)
1206 while (*q != 0 && strchr (*q, '=') == NULL)
1207 q++;
1208 *p = *q++;
1209 if (*p != 0)
1210 p++;
1215 #ifdef WINDOWSNT
1216 prepare_standard_handles (in, out, err, handles);
1217 set_process_dir (SDATA (current_dir));
1218 #else /* not WINDOWSNT */
1219 /* Make sure that in, out, and err are not actually already in
1220 descriptors zero, one, or two; this could happen if Emacs is
1221 started with its standard in, out, or error closed, as might
1222 happen under X. */
1224 int oin = in, oout = out;
1226 /* We have to avoid relocating the same descriptor twice! */
1228 in = relocate_fd (in, 3);
1230 if (out == oin)
1231 out = in;
1232 else
1233 out = relocate_fd (out, 3);
1235 if (err == oin)
1236 err = in;
1237 else if (err == oout)
1238 err = out;
1239 else
1240 err = relocate_fd (err, 3);
1243 #ifndef MSDOS
1244 emacs_close (0);
1245 emacs_close (1);
1246 emacs_close (2);
1248 dup2 (in, 0);
1249 dup2 (out, 1);
1250 dup2 (err, 2);
1251 emacs_close (in);
1252 emacs_close (out);
1253 emacs_close (err);
1254 #endif /* not MSDOS */
1255 #endif /* not WINDOWSNT */
1257 #if defined(USG) && !defined(BSD_PGRPS)
1258 #ifndef SETPGRP_RELEASES_CTTY
1259 setpgrp (); /* No arguments but equivalent in this case */
1260 #endif
1261 #else
1262 setpgrp (pid, pid);
1263 #endif /* USG */
1264 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1265 EMACS_SET_TTY_PGRP (0, &pid);
1267 #ifdef MSDOS
1268 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1269 xfree (pwd_var);
1270 if (pid == -1)
1271 /* An error occurred while trying to run the subprocess. */
1272 report_file_error ("Spawning child process", Qnil);
1273 return pid;
1274 #else /* not MSDOS */
1275 #ifdef WINDOWSNT
1276 /* Spawn the child. (See ntproc.c:Spawnve). */
1277 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1278 reset_standard_handles (in, out, err, handles);
1279 if (cpid == -1)
1280 /* An error occurred while trying to spawn the process. */
1281 report_file_error ("Spawning child process", Qnil);
1282 return cpid;
1283 #else /* not WINDOWSNT */
1284 /* execvp does not accept an environment arg so the only way
1285 to pass this environment is to set environ. Our caller
1286 is responsible for restoring the ambient value of environ. */
1287 environ = env;
1288 execvp (new_argv[0], new_argv);
1290 emacs_write (1, "Can't exec program: ", 20);
1291 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1292 emacs_write (1, "\n", 1);
1293 _exit (1);
1294 #endif /* not WINDOWSNT */
1295 #endif /* not MSDOS */
1298 /* Move the file descriptor FD so that its number is not less than MINFD.
1299 If the file descriptor is moved at all, the original is freed. */
1300 static int
1301 relocate_fd (fd, minfd)
1302 int fd, minfd;
1304 if (fd >= minfd)
1305 return fd;
1306 else
1308 int new = dup (fd);
1309 if (new == -1)
1311 char *message1 = "Error while setting up child: ";
1312 char *errmessage = strerror (errno);
1313 char *message2 = "\n";
1314 emacs_write (2, message1, strlen (message1));
1315 emacs_write (2, errmessage, strlen (errmessage));
1316 emacs_write (2, message2, strlen (message2));
1317 _exit (1);
1319 /* Note that we hold the original FD open while we recurse,
1320 to guarantee we'll get a new FD if we need it. */
1321 new = relocate_fd (new, minfd);
1322 emacs_close (fd);
1323 return new;
1327 static int
1328 getenv_internal_1 (var, varlen, value, valuelen, env)
1329 char *var;
1330 int varlen;
1331 char **value;
1332 int *valuelen;
1333 Lisp_Object env;
1335 for (; CONSP (env); env = XCDR (env))
1337 Lisp_Object entry = XCAR (env);
1338 if (STRINGP (entry)
1339 && SBYTES (entry) >= varlen
1340 #ifdef WINDOWSNT
1341 /* NT environment variables are case insensitive. */
1342 && ! strnicmp (SDATA (entry), var, varlen)
1343 #else /* not WINDOWSNT */
1344 && ! bcmp (SDATA (entry), var, varlen)
1345 #endif /* not WINDOWSNT */
1348 if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=')
1350 *value = (char *) SDATA (entry) + (varlen + 1);
1351 *valuelen = SBYTES (entry) - (varlen + 1);
1352 return 1;
1354 else if (SBYTES (entry) == varlen)
1356 /* Lone variable names in Vprocess_environment mean that
1357 variable should be removed from the environment. */
1358 *value = NULL;
1359 return 1;
1363 return 0;
1366 static int
1367 getenv_internal (var, varlen, value, valuelen, frame)
1368 char *var;
1369 int varlen;
1370 char **value;
1371 int *valuelen;
1372 Lisp_Object frame;
1374 /* Try to find VAR in Vprocess_environment first. */
1375 if (getenv_internal_1 (var, varlen, value, valuelen,
1376 Vprocess_environment))
1377 return *value ? 1 : 0;
1379 /* For DISPLAY try to get the values from the frame or the initial env. */
1380 if (strcmp (var, "DISPLAY") == 0)
1382 Lisp_Object display
1383 = Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay);
1384 if (STRINGP (display))
1386 *value = (char *) SDATA (display);
1387 *valuelen = SBYTES (display);
1388 return 1;
1390 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1391 if (getenv_internal_1 (var, varlen, value, valuelen,
1392 Vinitial_environment))
1393 return *value ? 1 : 0;
1396 return 0;
1399 DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
1400 doc: /* Get the value of environment variable VARIABLE.
1401 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
1402 the environment. Otherwise, value is a string.
1404 This function searches `process-environment' for VARIABLE. If it is
1405 not found there, then it continues the search in the environment list
1406 of the selected frame.
1408 If optional parameter ENV is a list, then search this list instead of
1409 `process-environment', and return t when encountering a negative entry.
1411 If it is a frame, then this function will ignore `process-environment' and
1412 will simply look up the variable in that frame's environment. */)
1413 (variable, env)
1414 Lisp_Object variable, env;
1416 char *value;
1417 int valuelen;
1419 CHECK_STRING (variable);
1420 if (CONSP (env))
1422 if (getenv_internal_1 (SDATA (variable), SBYTES (variable),
1423 &value, &valuelen, env))
1424 return value ? make_string (value, valuelen) : Qt;
1425 else
1426 return Qnil;
1428 else if (getenv_internal (SDATA (variable), SBYTES (variable),
1429 &value, &valuelen, env))
1430 return make_string (value, valuelen);
1431 else
1432 return Qnil;
1435 /* A version of getenv that consults the Lisp environment lists,
1436 easily callable from C. */
1437 char *
1438 egetenv (var)
1439 char *var;
1441 char *value;
1442 int valuelen;
1444 if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil))
1445 return value;
1446 else
1447 return 0;
1450 #endif /* not VMS */
1452 /* This is run before init_cmdargs. */
1454 void
1455 init_callproc_1 ()
1457 char *data_dir = egetenv ("EMACSDATA");
1458 char *doc_dir = egetenv ("EMACSDOC");
1460 Vdata_directory
1461 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1462 : PATH_DATA));
1463 Vdoc_directory
1464 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1465 : PATH_DOC));
1467 /* Check the EMACSPATH environment variable, defaulting to the
1468 PATH_EXEC path from epaths.h. */
1469 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1470 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1471 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1474 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1476 void
1477 init_callproc ()
1479 char *data_dir = egetenv ("EMACSDATA");
1481 register char * sh;
1482 Lisp_Object tempdir;
1484 if (!NILP (Vinstallation_directory))
1486 /* Add to the path the lib-src subdir of the installation dir. */
1487 Lisp_Object tem;
1488 tem = Fexpand_file_name (build_string ("lib-src"),
1489 Vinstallation_directory);
1490 #ifndef DOS_NT
1491 /* MSDOS uses wrapped binaries, so don't do this. */
1492 if (NILP (Fmember (tem, Vexec_path)))
1494 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1495 Vexec_path = Fcons (tem, Vexec_path);
1496 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1499 Vexec_directory = Ffile_name_as_directory (tem);
1500 #endif /* not DOS_NT */
1502 /* Maybe use ../etc as well as ../lib-src. */
1503 if (data_dir == 0)
1505 tem = Fexpand_file_name (build_string ("etc"),
1506 Vinstallation_directory);
1507 Vdoc_directory = Ffile_name_as_directory (tem);
1511 /* Look for the files that should be in etc. We don't use
1512 Vinstallation_directory, because these files are never installed
1513 near the executable, and they are never in the build
1514 directory when that's different from the source directory.
1516 Instead, if these files are not in the nominal place, we try the
1517 source directory. */
1518 if (data_dir == 0)
1520 Lisp_Object tem, tem1, srcdir;
1522 srcdir = Fexpand_file_name (build_string ("../src/"),
1523 build_string (PATH_DUMPLOADSEARCH));
1524 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1525 tem1 = Ffile_exists_p (tem);
1526 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
1528 Lisp_Object newdir;
1529 newdir = Fexpand_file_name (build_string ("../etc/"),
1530 build_string (PATH_DUMPLOADSEARCH));
1531 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1532 tem1 = Ffile_exists_p (tem);
1533 if (!NILP (tem1))
1534 Vdata_directory = newdir;
1538 #ifndef CANNOT_DUMP
1539 if (initialized)
1540 #endif
1542 tempdir = Fdirectory_file_name (Vexec_directory);
1543 if (access (SDATA (tempdir), 0) < 0)
1544 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1545 Vexec_directory);
1548 tempdir = Fdirectory_file_name (Vdata_directory);
1549 if (access (SDATA (tempdir), 0) < 0)
1550 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1551 Vdata_directory);
1553 #ifdef VMS
1554 Vshell_file_name = build_string ("*dcl*");
1555 #else
1556 sh = (char *) getenv ("SHELL");
1557 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1558 #endif
1560 #ifdef VMS
1561 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1562 #else
1563 if (getenv ("TMPDIR"))
1565 char *dir = getenv ("TMPDIR");
1566 Vtemp_file_name_pattern
1567 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1568 build_string (dir));
1570 else
1571 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1572 #endif
1574 #ifdef DOS_NT
1575 Vshared_game_score_directory = Qnil;
1576 #else
1577 Vshared_game_score_directory = build_string (PATH_GAME);
1578 if (NILP (Ffile_directory_p (Vshared_game_score_directory)))
1579 Vshared_game_score_directory = Qnil;
1580 #endif
1583 void
1584 set_initial_environment ()
1586 register char **envp;
1587 #ifndef CANNOT_DUMP
1588 if (initialized)
1589 #endif
1591 for (envp = environ; *envp; envp++)
1592 Vprocess_environment = Fcons (build_string (*envp),
1593 Vprocess_environment);
1594 /* Ideally, the `copy' shouldn't be necessary, but it seems it's frequent
1595 to use `delete' and friends on process-environment. */
1596 Vinitial_environment = Fcopy_sequence (Vprocess_environment);
1600 void
1601 syms_of_callproc ()
1603 #ifdef DOS_NT
1604 Qbuffer_file_type = intern ("buffer-file-type");
1605 staticpro (&Qbuffer_file_type);
1606 #endif /* DOS_NT */
1608 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1609 doc: /* *File name to load inferior shells from.
1610 Initialized from the SHELL environment variable, or to a system-dependent
1611 default if SHELL is not set. */);
1613 DEFVAR_LISP ("exec-path", &Vexec_path,
1614 doc: /* *List of directories to search programs to run in subprocesses.
1615 Each element is a string (directory name) or nil (try default directory). */);
1617 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes,
1618 doc: /* *List of suffixes to try to find executable file names.
1619 Each element is a string. */);
1620 Vexec_suffixes = Qnil;
1622 DEFVAR_LISP ("exec-directory", &Vexec_directory,
1623 doc: /* Directory for executables for Emacs to invoke.
1624 More generally, this includes any architecture-dependent files
1625 that are built and installed from the Emacs distribution. */);
1627 DEFVAR_LISP ("data-directory", &Vdata_directory,
1628 doc: /* Directory of machine-independent files that come with GNU Emacs.
1629 These are files intended for Emacs to use while it runs. */);
1631 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1632 doc: /* Directory containing the DOC file that comes with GNU Emacs.
1633 This is usually the same as `data-directory'. */);
1635 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1636 doc: /* For internal use by the build procedure only.
1637 This is the name of the directory in which the build procedure installed
1638 Emacs's info files; the default value for `Info-default-directory-list'
1639 includes this. */);
1640 Vconfigure_info_directory = build_string (PATH_INFO);
1642 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory,
1643 doc: /* Directory of score files for games which come with GNU Emacs.
1644 If this variable is nil, then Emacs is unable to use a shared directory. */);
1645 #ifdef DOS_NT
1646 Vshared_game_score_directory = Qnil;
1647 #else
1648 Vshared_game_score_directory = build_string (PATH_GAME);
1649 #endif
1651 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
1652 doc: /* Pattern for making names for temporary files.
1653 This is used by `call-process-region'. */);
1654 /* This variable is initialized in init_callproc. */
1656 DEFVAR_LISP ("initial-environment", &Vinitial_environment,
1657 doc: /* List of environment variables inherited from the parent process.
1658 Each element should be a string of the form ENVVARNAME=VALUE.
1659 The elements must normally be decoded (using `locale-coding-system') for use. */);
1660 Vinitial_environment = Qnil;
1662 DEFVAR_LISP ("process-environment", &Vprocess_environment,
1663 doc: /* List of overridden environment variables for subprocesses to inherit.
1664 Each element should be a string of the form ENVVARNAME=VALUE.
1666 Entries in this list take precedence to those in the frame-local
1667 environments. Therefore, let-binding `process-environment' is an easy
1668 way to temporarily change the value of an environment variable,
1669 irrespective of where it comes from. To use `process-environment' to
1670 remove an environment variable, include only its name in the list,
1671 without "=VALUE".
1673 This variable is set to nil when Emacs starts.
1675 If multiple entries define the same variable, the first one always
1676 takes precedence.
1678 Non-ASCII characters are encoded according to the initial value of
1679 `locale-coding-system', i.e. the elements must normally be decoded for
1680 use.
1682 See `setenv' and `getenv'. */);
1683 Vprocess_environment = Qnil;
1685 #ifndef VMS
1686 defsubr (&Scall_process);
1687 defsubr (&Sgetenv_internal);
1688 #endif
1689 defsubr (&Scall_process_region);
1692 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1693 (do not change this comment) */