Various small changes in addition to:
[emacs.git] / src / callproc.c
blob73d81d81b18853c59db59410db93266cfddfcb4b
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <stdio.h>
28 #ifndef USE_CRT_DLL
29 extern int errno;
30 #endif
32 /* Define SIGCHLD as an alias for SIGCLD. */
34 #if !defined (SIGCHLD) && defined (SIGCLD)
35 #define SIGCHLD SIGCLD
36 #endif /* SIGCLD */
38 #include <sys/types.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #include <sys/file.h>
45 #ifdef HAVE_FCNTL_H
46 #define INCLUDED_FCNTL
47 #include <fcntl.h>
48 #endif
50 #ifdef WINDOWSNT
51 #define NOMINMAX
52 #include <windows.h>
53 #include <stdlib.h> /* for proper declaration of environ */
54 #include <fcntl.h>
55 #include "w32.h"
56 #define _P_NOWAIT 1 /* from process.h */
57 #endif
59 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
60 #define INCLUDED_FCNTL
61 #include <fcntl.h>
62 #include <sys/stat.h>
63 #include <sys/param.h>
64 #include <errno.h>
65 #endif /* MSDOS */
67 #ifndef O_RDONLY
68 #define O_RDONLY 0
69 #endif
71 #ifndef O_WRONLY
72 #define O_WRONLY 1
73 #endif
75 #include "lisp.h"
76 #include "commands.h"
77 #include "buffer.h"
78 #include "charset.h"
79 #include "ccl.h"
80 #include "coding.h"
81 #include "composite.h"
82 #include <epaths.h>
83 #include "process.h"
84 #include "syssignal.h"
85 #include "systty.h"
87 #ifdef MSDOS
88 #include "msdos.h"
89 #endif
91 #ifdef VMS
92 extern noshare char **environ;
93 #else
94 #ifndef USE_CRT_DLL
95 extern char **environ;
96 #endif
97 #endif
99 #ifdef HAVE_SETPGID
100 #if !defined (USG) || defined (BSD_PGRPS)
101 #undef setpgrp
102 #define setpgrp setpgid
103 #endif
104 #endif
106 Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes;
107 Lisp_Object Vdata_directory, Vdoc_directory;
108 Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory;
109 Lisp_Object Vtemp_file_name_pattern;
111 Lisp_Object Vshell_file_name;
113 Lisp_Object Vprocess_environment;
115 #ifdef DOS_NT
116 Lisp_Object Qbuffer_file_type;
117 #endif /* DOS_NT */
119 /* True iff we are about to fork off a synchronous process or if we
120 are waiting for it. */
121 int synch_process_alive;
123 /* Nonzero => this is a string explaining death of synchronous subprocess. */
124 char *synch_process_death;
126 /* Nonzero => this is the signal number that terminated the subprocess. */
127 int synch_process_termsig;
129 /* If synch_process_death is zero,
130 this is exit code of synchronous subprocess. */
131 int synch_process_retcode;
133 extern Lisp_Object Vdoc_file_name;
135 extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
137 /* Clean up when exiting Fcall_process.
138 On MSDOS, delete the temporary file on any kind of termination.
139 On Unix, kill the process and any children on termination by signal. */
141 /* Nonzero if this is termination due to exit. */
142 static int call_process_exited;
144 #ifndef VMS /* VMS version is in vmsproc.c. */
146 static Lisp_Object
147 call_process_kill (fdpid)
148 Lisp_Object fdpid;
150 emacs_close (XFASTINT (Fcar (fdpid)));
151 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
152 synch_process_alive = 0;
153 return Qnil;
156 Lisp_Object
157 call_process_cleanup (fdpid)
158 Lisp_Object fdpid;
160 #if defined (MSDOS) || defined (MAC_OS8)
161 /* for MSDOS fdpid is really (fd . tempfile) */
162 register Lisp_Object file;
163 file = Fcdr (fdpid);
164 emacs_close (XFASTINT (Fcar (fdpid)));
165 if (strcmp (SDATA (file), NULL_DEVICE) != 0)
166 unlink (SDATA (file));
167 #else /* not MSDOS and not MAC_OS8 */
168 register int pid = XFASTINT (Fcdr (fdpid));
170 if (call_process_exited)
172 emacs_close (XFASTINT (Fcar (fdpid)));
173 return Qnil;
176 if (EMACS_KILLPG (pid, SIGINT) == 0)
178 int count = SPECPDL_INDEX ();
179 record_unwind_protect (call_process_kill, fdpid);
180 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
181 immediate_quit = 1;
182 QUIT;
183 wait_for_termination (pid);
184 immediate_quit = 0;
185 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
186 message1 ("Waiting for process to die...done");
188 synch_process_alive = 0;
189 emacs_close (XFASTINT (Fcar (fdpid)));
190 #endif /* not MSDOS */
191 return Qnil;
194 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
195 doc: /* Call PROGRAM synchronously in separate process.
196 The remaining arguments are optional.
197 The program's input comes from file INFILE (nil means `/dev/null').
198 Insert output in BUFFER before point; t means current buffer;
199 nil for BUFFER means discard it; 0 means discard and don't wait.
200 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
201 REAL-BUFFER says what to do with standard output, as above,
202 while STDERR-FILE says what to do with standard error in the child.
203 STDERR-FILE may be nil (discard standard error output),
204 t (mix it with ordinary output), or a file name string.
206 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
207 Remaining arguments are strings passed as command arguments to PROGRAM.
209 If BUFFER is 0, `call-process' returns immediately with value nil.
210 Otherwise it waits for PROGRAM to terminate
211 and returns a numeric exit status or a signal description string.
212 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
214 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
215 (nargs, args)
216 int nargs;
217 register Lisp_Object *args;
219 Lisp_Object infile, buffer, current_dir, display, path;
220 int fd[2];
221 int filefd;
222 register int pid;
223 char buf[16384];
224 char *bufptr = buf;
225 int bufsize = 16384;
226 int count = SPECPDL_INDEX ();
228 register const unsigned char **new_argv
229 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
230 struct buffer *old = current_buffer;
231 /* File to use for stderr in the child.
232 t means use same as standard output. */
233 Lisp_Object error_file;
234 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
235 char *outf, *tempfile;
236 int outfilefd;
237 #endif
238 #ifdef MAC_OS8
239 char *tempfile;
240 int outfilefd;
241 #endif
242 #if 0
243 int mask;
244 #endif
245 struct coding_system process_coding; /* coding-system of process output */
246 struct coding_system argument_coding; /* coding-system of arguments */
247 /* Set to the return value of Ffind_operation_coding_system. */
248 Lisp_Object coding_systems;
250 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
251 coding_systems = Qt;
253 CHECK_STRING (args[0]);
255 error_file = Qt;
257 #ifndef subprocesses
258 /* Without asynchronous processes we cannot have BUFFER == 0. */
259 if (nargs >= 3
260 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
261 error ("Operating system cannot handle asynchronous subprocesses");
262 #endif /* subprocesses */
264 /* Decide the coding-system for giving arguments. */
266 Lisp_Object val, *args2;
267 int i;
269 /* If arguments are supplied, we may have to encode them. */
270 if (nargs >= 5)
272 int must_encode = 0;
274 for (i = 4; i < nargs; i++)
275 CHECK_STRING (args[i]);
277 for (i = 4; i < nargs; i++)
278 if (STRING_MULTIBYTE (args[i]))
279 must_encode = 1;
281 if (!NILP (Vcoding_system_for_write))
282 val = Vcoding_system_for_write;
283 else if (! must_encode)
284 val = Qnil;
285 else
287 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
288 args2[0] = Qcall_process;
289 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
290 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
291 if (CONSP (coding_systems))
292 val = XCDR (coding_systems);
293 else if (CONSP (Vdefault_process_coding_system))
294 val = XCDR (Vdefault_process_coding_system);
295 else
296 val = Qnil;
298 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
302 if (nargs >= 2 && ! NILP (args[1]))
304 infile = Fexpand_file_name (args[1], current_buffer->directory);
305 CHECK_STRING (infile);
307 else
308 infile = build_string (NULL_DEVICE);
310 if (nargs >= 3)
312 buffer = args[2];
314 /* If BUFFER is a list, its meaning is
315 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
316 if (CONSP (buffer))
318 if (CONSP (XCDR (buffer)))
320 Lisp_Object stderr_file;
321 stderr_file = XCAR (XCDR (buffer));
323 if (NILP (stderr_file) || EQ (Qt, stderr_file))
324 error_file = stderr_file;
325 else
326 error_file = Fexpand_file_name (stderr_file, Qnil);
329 buffer = XCAR (buffer);
332 if (!(EQ (buffer, Qnil)
333 || EQ (buffer, Qt)
334 || INTEGERP (buffer)))
336 Lisp_Object spec_buffer;
337 spec_buffer = buffer;
338 buffer = Fget_buffer_create (buffer);
339 /* Mention the buffer name for a better error message. */
340 if (NILP (buffer))
341 CHECK_BUFFER (spec_buffer);
342 CHECK_BUFFER (buffer);
345 else
346 buffer = Qnil;
348 /* Make sure that the child will be able to chdir to the current
349 buffer's current directory, or its unhandled equivalent. We
350 can't just have the child check for an error when it does the
351 chdir, since it's in a vfork.
353 We have to GCPRO around this because Fexpand_file_name,
354 Funhandled_file_name_directory, and Ffile_accessible_directory_p
355 might call a file name handling function. The argument list is
356 protected by the caller, so all we really have to worry about is
357 buffer. */
359 struct gcpro gcpro1, gcpro2, gcpro3;
361 current_dir = current_buffer->directory;
363 GCPRO3 (infile, buffer, current_dir);
365 current_dir
366 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
367 Qnil);
368 if (NILP (Ffile_accessible_directory_p (current_dir)))
369 report_file_error ("Setting current directory",
370 Fcons (current_buffer->directory, Qnil));
372 UNGCPRO;
375 display = nargs >= 4 ? args[3] : Qnil;
377 filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
378 if (filefd < 0)
380 report_file_error ("Opening process input file", Fcons (infile, Qnil));
382 /* Search for program; barf if not found. */
384 struct gcpro gcpro1;
386 GCPRO1 (current_dir);
387 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
388 UNGCPRO;
390 if (NILP (path))
392 emacs_close (filefd);
393 report_file_error ("Searching for program", Fcons (args[0], Qnil));
396 /* If program file name starts with /: for quoting a magic name,
397 discard that. */
398 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
399 && SREF (path, 1) == ':')
400 path = Fsubstring (path, make_number (2), Qnil);
402 new_argv[0] = SDATA (path);
403 if (nargs > 4)
405 register int i;
406 struct gcpro gcpro1, gcpro2, gcpro3;
408 GCPRO3 (infile, buffer, current_dir);
409 argument_coding.dst_multibyte = 0;
410 for (i = 4; i < nargs; i++)
412 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
413 if (CODING_REQUIRE_ENCODING (&argument_coding))
415 /* We must encode this argument. */
416 args[i] = encode_coding_string (args[i], &argument_coding, 1);
417 if (argument_coding.type == coding_type_ccl)
418 setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
420 new_argv[i - 3] = SDATA (args[i]);
422 UNGCPRO;
423 new_argv[nargs - 3] = 0;
425 else
426 new_argv[1] = 0;
428 #ifdef MSDOS /* MW, July 1993 */
429 if ((outf = egetenv ("TMPDIR")))
430 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
431 else
433 tempfile = alloca (20);
434 *tempfile = '\0';
436 dostounix_filename (tempfile);
437 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
438 strcat (tempfile, "/");
439 strcat (tempfile, "detmp.XXX");
440 mktemp (tempfile);
442 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
443 if (outfilefd < 0)
445 emacs_close (filefd);
446 report_file_error ("Opening process output file",
447 Fcons (build_string (tempfile), Qnil));
449 fd[0] = filefd;
450 fd[1] = outfilefd;
451 #endif /* MSDOS */
453 #ifdef MAC_OS8
454 /* Since we don't have pipes on the Mac, create a temporary file to
455 hold the output of the subprocess. */
456 tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
457 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
458 SBYTES (Vtemp_file_name_pattern) + 1);
460 mktemp (tempfile);
462 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
463 if (outfilefd < 0)
465 close (filefd);
466 report_file_error ("Opening process output file",
467 Fcons (build_string (tempfile), Qnil));
469 fd[0] = filefd;
470 fd[1] = outfilefd;
471 #endif /* MAC_OS8 */
473 if (INTEGERP (buffer))
474 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
475 else
477 #ifndef MSDOS
478 #ifndef MAC_OS8
479 errno = 0;
480 if (pipe (fd) == -1)
482 emacs_close (filefd);
483 report_file_error ("Creating process pipe", Qnil);
485 #endif
486 #endif
487 #if 0
488 /* Replaced by close_process_descs */
489 set_exclusive_use (fd[0]);
490 #endif
494 /* child_setup must clobber environ in systems with true vfork.
495 Protect it from permanent change. */
496 register char **save_environ = environ;
497 register int fd1 = fd[1];
498 int fd_error = fd1;
500 #if 0 /* Some systems don't have sigblock. */
501 mask = sigblock (sigmask (SIGCHLD));
502 #endif
504 /* Record that we're about to create a synchronous process. */
505 synch_process_alive = 1;
507 /* These vars record information from process termination.
508 Clear them now before process can possibly terminate,
509 to avoid timing error if process terminates soon. */
510 synch_process_death = 0;
511 synch_process_retcode = 0;
512 synch_process_termsig = 0;
514 if (NILP (error_file))
515 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
516 else if (STRINGP (error_file))
518 #ifdef DOS_NT
519 fd_error = emacs_open (SDATA (error_file),
520 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
521 S_IREAD | S_IWRITE);
522 #else /* not DOS_NT */
523 fd_error = creat (SDATA (error_file), 0666);
524 #endif /* not DOS_NT */
527 if (fd_error < 0)
529 emacs_close (filefd);
530 if (fd[0] != filefd)
531 emacs_close (fd[0]);
532 if (fd1 >= 0)
533 emacs_close (fd1);
534 #ifdef MSDOS
535 unlink (tempfile);
536 #endif
537 report_file_error ("Cannot redirect stderr",
538 Fcons ((NILP (error_file)
539 ? build_string (NULL_DEVICE) : error_file),
540 Qnil));
543 current_dir = ENCODE_FILE (current_dir);
545 #ifdef MAC_OS8
547 /* Call run_mac_command in sysdep.c here directly instead of doing
548 a child_setup as for MSDOS and other platforms. Note that this
549 code does not handle passing the environment to the synchronous
550 Mac subprocess. */
551 char *infn, *outfn, *errfn, *currdn;
553 /* close these files so subprocess can write to them */
554 close (outfilefd);
555 if (fd_error != outfilefd)
556 close (fd_error);
557 fd1 = -1; /* No harm in closing that one! */
559 infn = SDATA (infile);
560 outfn = tempfile;
561 if (NILP (error_file))
562 errfn = NULL_DEVICE;
563 else if (EQ (Qt, error_file))
564 errfn = outfn;
565 else
566 errfn = SDATA (error_file);
567 currdn = SDATA (current_dir);
568 pid = run_mac_command (new_argv, currdn, infn, outfn, errfn);
570 /* Record that the synchronous process exited and note its
571 termination status. */
572 synch_process_alive = 0;
573 synch_process_retcode = pid;
574 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
576 synchronize_system_messages_locale ();
577 synch_process_death = strerror (errno);
580 /* Since CRLF is converted to LF within `decode_coding', we can
581 always open a file with binary mode. */
582 fd[0] = open (tempfile, O_BINARY);
583 if (fd[0] < 0)
585 unlink (tempfile);
586 close (filefd);
587 report_file_error ("Cannot re-open temporary file", Qnil);
590 #else /* not MAC_OS8 */
591 #ifdef MSDOS /* MW, July 1993 */
592 /* Note that on MSDOS `child_setup' actually returns the child process
593 exit status, not its PID, so we assign it to `synch_process_retcode'
594 below. */
595 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
596 0, current_dir);
598 /* Record that the synchronous process exited and note its
599 termination status. */
600 synch_process_alive = 0;
601 synch_process_retcode = pid;
602 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
604 synchronize_system_messages_locale ();
605 synch_process_death = strerror (errno);
608 emacs_close (outfilefd);
609 if (fd_error != outfilefd)
610 emacs_close (fd_error);
611 fd1 = -1; /* No harm in closing that one! */
612 /* Since CRLF is converted to LF within `decode_coding', we can
613 always open a file with binary mode. */
614 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
615 if (fd[0] < 0)
617 unlink (tempfile);
618 emacs_close (filefd);
619 report_file_error ("Cannot re-open temporary file", Qnil);
621 #else /* not MSDOS */
622 #ifdef WINDOWSNT
623 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
624 0, current_dir);
625 #else /* not WINDOWSNT */
626 pid = vfork ();
628 if (pid == 0)
630 if (fd[0] >= 0)
631 emacs_close (fd[0]);
632 #ifdef HAVE_SETSID
633 setsid ();
634 #endif
635 #if defined (USG) && !defined (BSD_PGRPS)
636 setpgrp ();
637 #else
638 setpgrp (pid, pid);
639 #endif /* USG */
640 child_setup (filefd, fd1, fd_error, (char **) new_argv,
641 0, current_dir);
643 #endif /* not WINDOWSNT */
645 /* The MSDOS case did this already. */
646 if (fd_error >= 0)
647 emacs_close (fd_error);
648 #endif /* not MSDOS */
649 #endif /* not MAC_OS8 */
651 environ = save_environ;
653 /* Close most of our fd's, but not fd[0]
654 since we will use that to read input from. */
655 emacs_close (filefd);
656 if (fd1 >= 0 && fd1 != fd_error)
657 emacs_close (fd1);
660 if (pid < 0)
662 if (fd[0] >= 0)
663 emacs_close (fd[0]);
664 report_file_error ("Doing vfork", Qnil);
667 if (INTEGERP (buffer))
669 if (fd[0] >= 0)
670 emacs_close (fd[0]);
671 #ifndef subprocesses
672 /* If Emacs has been built with asynchronous subprocess support,
673 we don't need to do this, I think because it will then have
674 the facilities for handling SIGCHLD. */
675 wait_without_blocking ();
676 #endif /* subprocesses */
677 return Qnil;
680 /* Enable sending signal if user quits below. */
681 call_process_exited = 0;
683 #if defined(MSDOS) || defined(MAC_OS8)
684 /* MSDOS needs different cleanup information. */
685 record_unwind_protect (call_process_cleanup,
686 Fcons (make_number (fd[0]), build_string (tempfile)));
687 #else
688 record_unwind_protect (call_process_cleanup,
689 Fcons (make_number (fd[0]), make_number (pid)));
690 #endif /* not MSDOS and not MAC_OS8 */
693 if (BUFFERP (buffer))
694 Fset_buffer (buffer);
696 if (NILP (buffer))
698 /* If BUFFER is nil, we must read process output once and then
699 discard it, so setup coding system but with nil. */
700 setup_coding_system (Qnil, &process_coding);
702 else
704 Lisp_Object val, *args2;
706 val = Qnil;
707 if (!NILP (Vcoding_system_for_read))
708 val = Vcoding_system_for_read;
709 else
711 if (EQ (coding_systems, Qt))
713 int i;
715 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
716 args2[0] = Qcall_process;
717 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
718 coding_systems
719 = Ffind_operation_coding_system (nargs + 1, args2);
721 if (CONSP (coding_systems))
722 val = XCAR (coding_systems);
723 else if (CONSP (Vdefault_process_coding_system))
724 val = XCAR (Vdefault_process_coding_system);
725 else
726 val = Qnil;
728 setup_coding_system (Fcheck_coding_system (val), &process_coding);
729 /* In unibyte mode, character code conversion should not take
730 place but EOL conversion should. So, setup raw-text or one
731 of the subsidiary according to the information just setup. */
732 if (NILP (current_buffer->enable_multibyte_characters)
733 && !NILP (val))
734 setup_raw_text_coding_system (&process_coding);
736 process_coding.src_multibyte = 0;
737 process_coding.dst_multibyte
738 = (BUFFERP (buffer)
739 ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters)
740 : ! NILP (current_buffer->enable_multibyte_characters));
742 immediate_quit = 1;
743 QUIT;
746 register int nread;
747 int first = 1;
748 int total_read = 0;
749 int carryover = 0;
750 int display_on_the_fly = !NILP (display) && INTERACTIVE;
751 struct coding_system saved_coding;
752 int pt_orig = PT, pt_byte_orig = PT_BYTE;
753 int inserted;
755 saved_coding = process_coding;
756 if (process_coding.composing != COMPOSITION_DISABLED)
757 coding_allocate_composition_data (&process_coding, PT);
758 while (1)
760 /* Repeatedly read until we've filled as much as possible
761 of the buffer size we have. But don't read
762 less than 1024--save that for the next bufferful. */
763 nread = carryover;
764 while (nread < bufsize - 1024)
766 int this_read = emacs_read (fd[0], bufptr + nread,
767 bufsize - nread);
769 if (this_read < 0)
770 goto give_up;
772 if (this_read == 0)
774 process_coding.mode |= CODING_MODE_LAST_BLOCK;
775 break;
778 nread += this_read;
779 total_read += this_read;
781 if (display_on_the_fly)
782 break;
785 /* Now NREAD is the total amount of data in the buffer. */
786 immediate_quit = 0;
788 if (!NILP (buffer))
790 if (! CODING_MAY_REQUIRE_DECODING (&process_coding))
791 insert_1_both (bufptr, nread, nread, 0, 1, 0);
792 else
793 { /* We have to decode the input. */
794 int size;
795 char *decoding_buf;
797 repeat_decoding:
798 size = decoding_buffer_size (&process_coding, nread);
799 decoding_buf = (char *) xmalloc (size);
801 /* We can't use the macro CODING_REQUIRE_DETECTION
802 because it always returns nonzero if the coding
803 system requires EOL detection. Here, we have to
804 check only whether or not the coding system
805 requires text-encoding detection. */
806 if (process_coding.type == coding_type_undecided)
808 detect_coding (&process_coding, bufptr, nread);
809 if (process_coding.composing != COMPOSITION_DISABLED)
810 coding_allocate_composition_data (&process_coding, PT);
812 if (process_coding.cmp_data)
813 process_coding.cmp_data->char_offset = PT;
815 decode_coding (&process_coding, bufptr, decoding_buf,
816 nread, size);
818 if (display_on_the_fly
819 && saved_coding.type == coding_type_undecided
820 && process_coding.type != coding_type_undecided)
822 /* We have detected some coding system. But,
823 there's a possibility that the detection was
824 done by insufficient data. So, we give up
825 displaying on the fly. */
826 xfree (decoding_buf);
827 display_on_the_fly = 0;
828 process_coding = saved_coding;
829 carryover = nread;
830 continue;
833 if (process_coding.produced > 0)
834 insert_1_both (decoding_buf, process_coding.produced_char,
835 process_coding.produced, 0, 1, 0);
836 xfree (decoding_buf);
838 if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL)
840 Lisp_Object eol_type, coding;
842 if (process_coding.eol_type == CODING_EOL_CR)
844 /* CRs have been replaced with LFs. Undo
845 that in the text inserted above. */
846 unsigned char *p;
848 move_gap_both (PT, PT_BYTE);
850 p = BYTE_POS_ADDR (pt_byte_orig);
851 for (; p < GPT_ADDR; ++p)
852 if (*p == '\n')
853 *p = '\r';
855 else if (process_coding.eol_type == CODING_EOL_CRLF)
857 /* CR LFs have been replaced with LFs. Undo
858 that by inserting CRs in front of LFs in
859 the text inserted above. */
860 EMACS_INT bytepos, old_pt, old_pt_byte, nCR;
862 old_pt = PT;
863 old_pt_byte = PT_BYTE;
864 nCR = 0;
866 for (bytepos = PT_BYTE - 1;
867 bytepos >= pt_byte_orig;
868 --bytepos)
869 if (FETCH_BYTE (bytepos) == '\n')
871 EMACS_INT charpos = BYTE_TO_CHAR (bytepos);
872 TEMP_SET_PT_BOTH (charpos, bytepos);
873 insert_1_both ("\r", 1, 1, 0, 1, 0);
874 ++nCR;
877 TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR);
880 /* Set the coding system symbol to that for
881 Unix-like EOL. */
882 eol_type = Fget (saved_coding.symbol, Qeol_type);
883 if (VECTORP (eol_type)
884 && ASIZE (eol_type) == 3
885 && SYMBOLP (AREF (eol_type, CODING_EOL_LF)))
886 coding = AREF (eol_type, CODING_EOL_LF);
887 else
888 coding = saved_coding.symbol;
890 process_coding.symbol = coding;
891 process_coding.eol_type = CODING_EOL_LF;
892 process_coding.mode
893 &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL;
896 nread -= process_coding.consumed;
897 carryover = nread;
898 if (carryover > 0)
899 /* As CARRYOVER should not be that large, we had
900 better avoid overhead of bcopy. */
901 BCOPY_SHORT (bufptr + process_coding.consumed, bufptr,
902 carryover);
903 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP)
905 /* The decoding ended because of insufficient data
906 area to record information about composition.
907 We must try decoding with additional data area
908 before reading more output for the process. */
909 coding_allocate_composition_data (&process_coding, PT);
910 goto repeat_decoding;
915 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
916 break;
918 /* Make the buffer bigger as we continue to read more data,
919 but not past 64k. */
920 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
922 char *tempptr;
923 bufsize *= 2;
925 tempptr = (char *) alloca (bufsize);
926 bcopy (bufptr, tempptr, bufsize / 2);
927 bufptr = tempptr;
930 if (!NILP (display) && INTERACTIVE)
932 if (first)
933 prepare_menu_bars ();
934 first = 0;
935 redisplay_preserve_echo_area (1);
937 immediate_quit = 1;
938 QUIT;
940 give_up: ;
942 if (!NILP (buffer)
943 && process_coding.cmp_data)
945 coding_restore_composition (&process_coding, Fcurrent_buffer ());
946 coding_free_composition_data (&process_coding);
950 int post_read_count = SPECPDL_INDEX ();
952 record_unwind_protect (save_excursion_restore, save_excursion_save ());
953 inserted = PT - pt_orig;
954 TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig);
955 if (SYMBOLP (process_coding.post_read_conversion)
956 && !NILP (Ffboundp (process_coding.post_read_conversion)))
957 call1 (process_coding.post_read_conversion, make_number (inserted));
959 Vlast_coding_system_used = process_coding.symbol;
961 /* If the caller required, let the buffer inherit the
962 coding-system used to decode the process output. */
963 if (inherit_process_coding_system)
964 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
965 make_number (total_read));
967 unbind_to (post_read_count, Qnil);
971 /* Wait for it to terminate, unless it already has. */
972 wait_for_termination (pid);
974 immediate_quit = 0;
976 set_buffer_internal (old);
978 /* Don't kill any children that the subprocess may have left behind
979 when exiting. */
980 call_process_exited = 1;
982 unbind_to (count, Qnil);
984 if (synch_process_termsig)
986 char *signame;
988 synchronize_system_messages_locale ();
989 signame = strsignal (synch_process_termsig);
991 if (signame == 0)
992 signame = "unknown";
994 synch_process_death = signame;
997 if (synch_process_death)
998 return code_convert_string_norecord (build_string (synch_process_death),
999 Vlocale_coding_system, 0);
1000 return make_number (synch_process_retcode);
1002 #endif
1004 static Lisp_Object
1005 delete_temp_file (name)
1006 Lisp_Object name;
1008 /* Use Fdelete_file (indirectly) because that runs a file name handler.
1009 We did that when writing the file, so we should do so when deleting. */
1010 internal_delete_file (name);
1011 return Qnil;
1014 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
1015 3, MANY, 0,
1016 doc: /* Send text from START to END to a synchronous process running PROGRAM.
1017 The remaining arguments are optional.
1018 Delete the text if fourth arg DELETE is non-nil.
1020 Insert output in BUFFER before point; t means current buffer;
1021 nil for BUFFER means discard it; 0 means discard and don't wait.
1022 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1023 REAL-BUFFER says what to do with standard output, as above,
1024 while STDERR-FILE says what to do with standard error in the child.
1025 STDERR-FILE may be nil (discard standard error output),
1026 t (mix it with ordinary output), or a file name string.
1028 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1029 Remaining args are passed to PROGRAM at startup as command args.
1031 If BUFFER is 0, `call-process-region' returns immediately with value nil.
1032 Otherwise it waits for PROGRAM to terminate
1033 and returns a numeric exit status or a signal description string.
1034 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
1036 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
1037 (nargs, args)
1038 int nargs;
1039 register Lisp_Object *args;
1041 struct gcpro gcpro1;
1042 Lisp_Object filename_string;
1043 register Lisp_Object start, end;
1044 int count = SPECPDL_INDEX ();
1045 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1046 Lisp_Object coding_systems;
1047 Lisp_Object val, *args2;
1048 int i;
1049 #ifdef DOS_NT
1050 char *tempfile;
1051 char *outf = '\0';
1053 if ((outf = egetenv ("TMPDIR"))
1054 || (outf = egetenv ("TMP"))
1055 || (outf = egetenv ("TEMP")))
1056 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
1057 else
1059 tempfile = alloca (20);
1060 *tempfile = '\0';
1062 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
1063 strcat (tempfile, "/");
1064 if ('/' == DIRECTORY_SEP)
1065 dostounix_filename (tempfile);
1066 else
1067 unixtodos_filename (tempfile);
1068 #ifdef WINDOWSNT
1069 strcat (tempfile, "emXXXXXX");
1070 #else
1071 strcat (tempfile, "detmp.XXX");
1072 #endif
1073 #else /* not DOS_NT */
1074 char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
1075 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
1076 SBYTES (Vtemp_file_name_pattern) + 1);
1077 #endif /* not DOS_NT */
1079 coding_systems = Qt;
1081 #ifdef HAVE_MKSTEMP
1083 int fd = mkstemp (tempfile);
1084 if (fd == -1)
1085 report_file_error ("Failed to open temporary file",
1086 Fcons (Vtemp_file_name_pattern, Qnil));
1087 else
1088 close (fd);
1090 #else
1091 mktemp (tempfile);
1092 #endif
1094 filename_string = build_string (tempfile);
1095 GCPRO1 (filename_string);
1096 start = args[0];
1097 end = args[1];
1098 /* Decide coding-system of the contents of the temporary file. */
1099 if (!NILP (Vcoding_system_for_write))
1100 val = Vcoding_system_for_write;
1101 else if (NILP (current_buffer->enable_multibyte_characters))
1102 val = Qnil;
1103 else
1105 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
1106 args2[0] = Qcall_process_region;
1107 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1108 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1109 if (CONSP (coding_systems))
1110 val = XCDR (coding_systems);
1111 else if (CONSP (Vdefault_process_coding_system))
1112 val = XCDR (Vdefault_process_coding_system);
1113 else
1114 val = Qnil;
1118 int count1 = SPECPDL_INDEX ();
1120 specbind (intern ("coding-system-for-write"), val);
1121 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1123 unbind_to (count1, Qnil);
1126 /* Note that Fcall_process takes care of binding
1127 coding-system-for-read. */
1129 record_unwind_protect (delete_temp_file, filename_string);
1131 if (nargs > 3 && !NILP (args[3]))
1132 Fdelete_region (start, end);
1134 if (nargs > 3)
1136 args += 2;
1137 nargs -= 2;
1139 else
1141 args[0] = args[2];
1142 nargs = 2;
1144 args[1] = filename_string;
1146 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
1149 #ifndef VMS /* VMS version is in vmsproc.c. */
1151 static int relocate_fd ();
1153 /* This is the last thing run in a newly forked inferior
1154 either synchronous or asynchronous.
1155 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1156 Initialize inferior's priority, pgrp, connected dir and environment.
1157 then exec another program based on new_argv.
1159 This function may change environ for the superior process.
1160 Therefore, the superior process must save and restore the value
1161 of environ around the vfork and the call to this function.
1163 SET_PGRP is nonzero if we should put the subprocess into a separate
1164 process group.
1166 CURRENT_DIR is an elisp string giving the path of the current
1167 directory the subprocess should have. Since we can't really signal
1168 a decent error from within the child, this should be verified as an
1169 executable directory by the parent. */
1172 child_setup (in, out, err, new_argv, set_pgrp, current_dir)
1173 int in, out, err;
1174 register char **new_argv;
1175 int set_pgrp;
1176 Lisp_Object current_dir;
1178 char **env;
1179 char *pwd_var;
1180 #ifdef WINDOWSNT
1181 int cpid;
1182 HANDLE handles[3];
1183 #endif /* WINDOWSNT */
1185 int pid = getpid ();
1187 #ifdef SET_EMACS_PRIORITY
1189 extern EMACS_INT emacs_priority;
1191 if (emacs_priority < 0)
1192 nice (- emacs_priority);
1194 #endif
1196 #ifdef subprocesses
1197 /* Close Emacs's descriptors that this process should not have. */
1198 close_process_descs ();
1199 #endif
1200 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1201 we will lose if we call close_load_descs here. */
1202 #ifndef DOS_NT
1203 close_load_descs ();
1204 #endif
1206 /* Note that use of alloca is always safe here. It's obvious for systems
1207 that do not have true vfork or that have true (stack) alloca.
1208 If using vfork and C_ALLOCA it is safe because that changes
1209 the superior's static variables as if the superior had done alloca
1210 and will be cleaned up in the usual way. */
1212 register char *temp;
1213 register int i;
1215 i = SBYTES (current_dir);
1216 #ifdef MSDOS
1217 /* MSDOS must have all environment variables malloc'ed, because
1218 low-level libc functions that launch subsidiary processes rely
1219 on that. */
1220 pwd_var = (char *) xmalloc (i + 6);
1221 #else
1222 pwd_var = (char *) alloca (i + 6);
1223 #endif
1224 temp = pwd_var + 4;
1225 bcopy ("PWD=", pwd_var, 4);
1226 bcopy (SDATA (current_dir), temp, i);
1227 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
1228 temp[i] = 0;
1230 #ifndef DOS_NT
1231 /* We can't signal an Elisp error here; we're in a vfork. Since
1232 the callers check the current directory before forking, this
1233 should only return an error if the directory's permissions
1234 are changed between the check and this chdir, but we should
1235 at least check. */
1236 if (chdir (temp) < 0)
1237 _exit (errno);
1238 #endif
1240 #ifdef DOS_NT
1241 /* Get past the drive letter, so that d:/ is left alone. */
1242 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1244 temp += 2;
1245 i -= 2;
1247 #endif
1249 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1250 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
1251 temp[--i] = 0;
1254 /* Set `env' to a vector of the strings in Vprocess_environment. */
1256 register Lisp_Object tem;
1257 register char **new_env;
1258 register int new_length;
1260 new_length = 0;
1261 for (tem = Vprocess_environment;
1262 CONSP (tem) && STRINGP (XCAR (tem));
1263 tem = XCDR (tem))
1264 new_length++;
1266 /* new_length + 2 to include PWD and terminating 0. */
1267 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1269 /* If we have a PWD envvar, pass one down,
1270 but with corrected value. */
1271 if (getenv ("PWD"))
1272 *new_env++ = pwd_var;
1274 /* Copy the Vprocess_environment strings into new_env. */
1275 for (tem = Vprocess_environment;
1276 CONSP (tem) && STRINGP (XCAR (tem));
1277 tem = XCDR (tem))
1279 char **ep = env;
1280 char *string = (char *) SDATA (XCAR (tem));
1281 /* See if this string duplicates any string already in the env.
1282 If so, don't put it in.
1283 When an env var has multiple definitions,
1284 we keep the definition that comes first in process-environment. */
1285 for (; ep != new_env; ep++)
1287 char *p = *ep, *q = string;
1288 while (1)
1290 if (*q == 0)
1291 /* The string is malformed; might as well drop it. */
1292 goto duplicate;
1293 if (*q != *p)
1294 break;
1295 if (*q == '=')
1296 goto duplicate;
1297 p++, q++;
1300 *new_env++ = string;
1301 duplicate: ;
1303 *new_env = 0;
1305 #ifdef WINDOWSNT
1306 prepare_standard_handles (in, out, err, handles);
1307 set_process_dir (SDATA (current_dir));
1308 #else /* not WINDOWSNT */
1309 /* Make sure that in, out, and err are not actually already in
1310 descriptors zero, one, or two; this could happen if Emacs is
1311 started with its standard in, out, or error closed, as might
1312 happen under X. */
1314 int oin = in, oout = out;
1316 /* We have to avoid relocating the same descriptor twice! */
1318 in = relocate_fd (in, 3);
1320 if (out == oin)
1321 out = in;
1322 else
1323 out = relocate_fd (out, 3);
1325 if (err == oin)
1326 err = in;
1327 else if (err == oout)
1328 err = out;
1329 else
1330 err = relocate_fd (err, 3);
1333 #ifndef MSDOS
1334 emacs_close (0);
1335 emacs_close (1);
1336 emacs_close (2);
1338 dup2 (in, 0);
1339 dup2 (out, 1);
1340 dup2 (err, 2);
1341 emacs_close (in);
1342 emacs_close (out);
1343 emacs_close (err);
1344 #endif /* not MSDOS */
1345 #endif /* not WINDOWSNT */
1347 #if defined(USG) && !defined(BSD_PGRPS)
1348 #ifndef SETPGRP_RELEASES_CTTY
1349 setpgrp (); /* No arguments but equivalent in this case */
1350 #endif
1351 #else
1352 setpgrp (pid, pid);
1353 #endif /* USG */
1354 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1355 EMACS_SET_TTY_PGRP (0, &pid);
1357 #ifdef MSDOS
1358 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1359 xfree (pwd_var);
1360 if (pid == -1)
1361 /* An error occurred while trying to run the subprocess. */
1362 report_file_error ("Spawning child process", Qnil);
1363 return pid;
1364 #else /* not MSDOS */
1365 #ifdef WINDOWSNT
1366 /* Spawn the child. (See ntproc.c:Spawnve). */
1367 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1368 reset_standard_handles (in, out, err, handles);
1369 if (cpid == -1)
1370 /* An error occurred while trying to spawn the process. */
1371 report_file_error ("Spawning child process", Qnil);
1372 return cpid;
1373 #else /* not WINDOWSNT */
1374 /* execvp does not accept an environment arg so the only way
1375 to pass this environment is to set environ. Our caller
1376 is responsible for restoring the ambient value of environ. */
1377 environ = env;
1378 execvp (new_argv[0], new_argv);
1380 emacs_write (1, "Can't exec program: ", 20);
1381 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1382 emacs_write (1, "\n", 1);
1383 _exit (1);
1384 #endif /* not WINDOWSNT */
1385 #endif /* not MSDOS */
1388 /* Move the file descriptor FD so that its number is not less than MINFD.
1389 If the file descriptor is moved at all, the original is freed. */
1390 static int
1391 relocate_fd (fd, minfd)
1392 int fd, minfd;
1394 if (fd >= minfd)
1395 return fd;
1396 else
1398 int new = dup (fd);
1399 if (new == -1)
1401 char *message1 = "Error while setting up child: ";
1402 char *errmessage = strerror (errno);
1403 char *message2 = "\n";
1404 emacs_write (2, message1, strlen (message1));
1405 emacs_write (2, errmessage, strlen (errmessage));
1406 emacs_write (2, message2, strlen (message2));
1407 _exit (1);
1409 /* Note that we hold the original FD open while we recurse,
1410 to guarantee we'll get a new FD if we need it. */
1411 new = relocate_fd (new, minfd);
1412 emacs_close (fd);
1413 return new;
1417 static int
1418 getenv_internal (var, varlen, value, valuelen)
1419 char *var;
1420 int varlen;
1421 char **value;
1422 int *valuelen;
1424 Lisp_Object scan;
1426 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
1428 Lisp_Object entry;
1430 entry = XCAR (scan);
1431 if (STRINGP (entry)
1432 && SBYTES (entry) > varlen
1433 && SREF (entry, varlen) == '='
1434 #ifdef WINDOWSNT
1435 /* NT environment variables are case insensitive. */
1436 && ! strnicmp (SDATA (entry), var, varlen)
1437 #else /* not WINDOWSNT */
1438 && ! bcmp (SDATA (entry), var, varlen)
1439 #endif /* not WINDOWSNT */
1442 *value = (char *) SDATA (entry) + (varlen + 1);
1443 *valuelen = SBYTES (entry) - (varlen + 1);
1444 return 1;
1448 return 0;
1451 DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 1, 0,
1452 doc: /* Return the value of environment variable VAR, as a string.
1453 VAR should be a string. Value is nil if VAR is undefined in the environment.
1454 This function consults the variable ``process-environment'' for its value. */)
1455 (var)
1456 Lisp_Object var;
1458 char *value;
1459 int valuelen;
1461 CHECK_STRING (var);
1462 if (getenv_internal (SDATA (var), SBYTES (var),
1463 &value, &valuelen))
1464 return make_string (value, valuelen);
1465 else
1466 return Qnil;
1469 /* A version of getenv that consults process_environment, easily
1470 callable from C. */
1471 char *
1472 egetenv (var)
1473 char *var;
1475 char *value;
1476 int valuelen;
1478 if (getenv_internal (var, strlen (var), &value, &valuelen))
1479 return value;
1480 else
1481 return 0;
1484 #endif /* not VMS */
1486 /* This is run before init_cmdargs. */
1488 void
1489 init_callproc_1 ()
1491 char *data_dir = egetenv ("EMACSDATA");
1492 char *doc_dir = egetenv ("EMACSDOC");
1494 Vdata_directory
1495 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1496 : PATH_DATA));
1497 Vdoc_directory
1498 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1499 : PATH_DOC));
1501 /* Check the EMACSPATH environment variable, defaulting to the
1502 PATH_EXEC path from epaths.h. */
1503 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1504 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1505 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1508 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1510 void
1511 init_callproc ()
1513 char *data_dir = egetenv ("EMACSDATA");
1515 register char * sh;
1516 Lisp_Object tempdir;
1518 if (!NILP (Vinstallation_directory))
1520 /* Add to the path the lib-src subdir of the installation dir. */
1521 Lisp_Object tem;
1522 tem = Fexpand_file_name (build_string ("lib-src"),
1523 Vinstallation_directory);
1524 #ifndef DOS_NT
1525 /* MSDOS uses wrapped binaries, so don't do this. */
1526 if (NILP (Fmember (tem, Vexec_path)))
1528 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1529 Vexec_path = Fcons (tem, Vexec_path);
1530 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1533 Vexec_directory = Ffile_name_as_directory (tem);
1534 #endif /* not DOS_NT */
1536 /* Maybe use ../etc as well as ../lib-src. */
1537 if (data_dir == 0)
1539 tem = Fexpand_file_name (build_string ("etc"),
1540 Vinstallation_directory);
1541 Vdoc_directory = Ffile_name_as_directory (tem);
1545 /* Look for the files that should be in etc. We don't use
1546 Vinstallation_directory, because these files are never installed
1547 near the executable, and they are never in the build
1548 directory when that's different from the source directory.
1550 Instead, if these files are not in the nominal place, we try the
1551 source directory. */
1552 if (data_dir == 0)
1554 Lisp_Object tem, tem1, srcdir;
1556 srcdir = Fexpand_file_name (build_string ("../src/"),
1557 build_string (PATH_DUMPLOADSEARCH));
1558 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1559 tem1 = Ffile_exists_p (tem);
1560 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
1562 Lisp_Object newdir;
1563 newdir = Fexpand_file_name (build_string ("../etc/"),
1564 build_string (PATH_DUMPLOADSEARCH));
1565 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1566 tem1 = Ffile_exists_p (tem);
1567 if (!NILP (tem1))
1568 Vdata_directory = newdir;
1572 #ifndef CANNOT_DUMP
1573 if (initialized)
1574 #endif
1576 tempdir = Fdirectory_file_name (Vexec_directory);
1577 if (access (SDATA (tempdir), 0) < 0)
1578 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1579 Vexec_directory);
1582 tempdir = Fdirectory_file_name (Vdata_directory);
1583 if (access (SDATA (tempdir), 0) < 0)
1584 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1585 Vdata_directory);
1587 #ifdef VMS
1588 Vshell_file_name = build_string ("*dcl*");
1589 #else
1590 sh = (char *) getenv ("SHELL");
1591 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1592 #endif
1594 #ifdef VMS
1595 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1596 #else
1597 if (getenv ("TMPDIR"))
1599 char *dir = getenv ("TMPDIR");
1600 Vtemp_file_name_pattern
1601 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1602 build_string (dir));
1604 else
1605 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1606 #endif
1608 #ifdef DOS_NT
1609 Vshared_game_score_directory = Qnil;
1610 #else
1611 Vshared_game_score_directory = build_string (PATH_GAME);
1612 if (NILP (Ffile_directory_p (Vshared_game_score_directory)))
1613 Vshared_game_score_directory = Qnil;
1614 #endif
1617 void
1618 set_process_environment ()
1620 register char **envp;
1622 Vprocess_environment = Qnil;
1623 #ifndef CANNOT_DUMP
1624 if (initialized)
1625 #endif
1626 for (envp = environ; *envp; envp++)
1627 Vprocess_environment = Fcons (build_string (*envp),
1628 Vprocess_environment);
1631 void
1632 syms_of_callproc ()
1634 #ifdef DOS_NT
1635 Qbuffer_file_type = intern ("buffer-file-type");
1636 staticpro (&Qbuffer_file_type);
1637 #endif /* DOS_NT */
1639 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1640 doc: /* *File name to load inferior shells from.
1641 Initialized from the SHELL environment variable. */);
1643 DEFVAR_LISP ("exec-path", &Vexec_path,
1644 doc: /* *List of directories to search programs to run in subprocesses.
1645 Each element is a string (directory name) or nil (try default directory). */);
1647 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes,
1648 doc: /* *List of suffixes to try to find executable file names.
1649 Each element is a string. */);
1650 Vexec_suffixes = Qnil;
1652 DEFVAR_LISP ("exec-directory", &Vexec_directory,
1653 doc: /* Directory for executables for Emacs to invoke.
1654 More generally, this includes any architecture-dependent files
1655 that are built and installed from the Emacs distribution. */);
1657 DEFVAR_LISP ("data-directory", &Vdata_directory,
1658 doc: /* Directory of machine-independent files that come with GNU Emacs.
1659 These are files intended for Emacs to use while it runs. */);
1661 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1662 doc: /* Directory containing the DOC file that comes with GNU Emacs.
1663 This is usually the same as data-directory. */);
1665 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1666 doc: /* For internal use by the build procedure only.
1667 This is the name of the directory in which the build procedure installed
1668 Emacs's info files; the default value for Info-default-directory-list
1669 includes this. */);
1670 Vconfigure_info_directory = build_string (PATH_INFO);
1672 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory,
1673 doc: /* Directory of score files for games which come with GNU Emacs.
1674 If this variable is nil, then Emacs is unable to use a shared directory. */);
1675 #ifdef DOS_NT
1676 Vshared_game_score_directory = Qnil;
1677 #else
1678 Vshared_game_score_directory = build_string (PATH_GAME);
1679 #endif
1681 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
1682 doc: /* Pattern for making names for temporary files.
1683 This is used by `call-process-region'. */);
1684 /* This variable is initialized in init_callproc. */
1686 DEFVAR_LISP ("process-environment", &Vprocess_environment,
1687 doc: /* List of environment variables for subprocesses to inherit.
1688 Each element should be a string of the form ENVVARNAME=VALUE.
1689 If multiple entries define the same variable, the first one always
1690 takes precedence.
1691 The environment which Emacs inherits is placed in this variable
1692 when Emacs starts.
1693 Non-ASCII characters are encoded according to the initial value of
1694 `locale-coding-system', i.e. the elements must normally be decoded for use.
1695 See `setenv' and `getenv'. */);
1697 #ifndef VMS
1698 defsubr (&Scall_process);
1699 defsubr (&Sgetenv_internal);
1700 #endif
1701 defsubr (&Scall_process_region);
1704 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1705 (do not change this comment) */