1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 extern char *strerror ();
30 /* Define SIGCHLD as an alias for SIGCLD. */
32 #if !defined (SIGCHLD) && defined (SIGCLD)
33 #define SIGCHLD SIGCLD
36 #include <sys/types.h>
40 #define INCLUDED_FCNTL
44 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
45 #define INCLUDED_FCNTL
48 #include <sys/param.h>
65 #include "syssignal.h"
69 extern noshare
char **environ
;
71 extern char **environ
;
74 #define max(a, b) ((a) > (b) ? (a) : (b))
77 Lisp_Object Vbinary_process
;
80 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
81 Lisp_Object Vconfigure_info_directory
;
83 Lisp_Object Vshell_file_name
;
85 Lisp_Object Vprocess_environment
;
87 /* True iff we are about to fork off a synchronous process or if we
88 are waiting for it. */
89 int synch_process_alive
;
91 /* Nonzero => this is a string explaining death of synchronous subprocess. */
92 char *synch_process_death
;
94 /* If synch_process_death is zero,
95 this is exit code of synchronous subprocess. */
96 int synch_process_retcode
;
98 extern Lisp_Object Vdoc_file_name
;
100 /* Clean up when exiting Fcall_process.
101 On MSDOS, delete the temporary file on any kind of termination.
102 On Unix, kill the process and any children on termination by signal. */
104 /* Nonzero if this is termination due to exit. */
105 static int call_process_exited
;
107 #ifndef VMS /* VMS version is in vmsproc.c. */
110 call_process_kill (fdpid
)
113 close (XFASTINT (Fcar (fdpid
)));
114 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
115 synch_process_alive
= 0;
120 call_process_cleanup (fdpid
)
124 /* for MSDOS fdpid is really (fd . tempfile) */
125 register Lisp_Object file
;
127 close (XFASTINT (Fcar (fdpid
)));
128 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
129 unlink (XSTRING (file
)->data
);
130 #else /* not MSDOS */
131 register int pid
= XFASTINT (Fcdr (fdpid
));
134 if (call_process_exited
)
136 close (XFASTINT (Fcar (fdpid
)));
140 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
142 int count
= specpdl_ptr
- specpdl
;
143 record_unwind_protect (call_process_kill
, fdpid
);
144 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
147 wait_for_termination (pid
);
149 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
150 message1 ("Waiting for process to die...done");
152 synch_process_alive
= 0;
153 close (XFASTINT (Fcar (fdpid
)));
154 #endif /* not MSDOS */
158 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
159 "Call PROGRAM synchronously in separate process.\n\
160 The program's input comes from file INFILE (nil means `/dev/null').\n\
161 Insert output in BUFFER before point; t means current buffer;\n\
162 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
163 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
164 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
165 If BUFFER is 0, returns immediately with value nil.\n\
166 Otherwise waits for PROGRAM to terminate\n\
167 and returns a numeric exit status or a signal description string.\n\
168 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
171 register Lisp_Object
*args
;
173 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
178 int count
= specpdl_ptr
- specpdl
;
179 register unsigned char **new_argv
180 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
181 struct buffer
*old
= current_buffer
;
182 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
183 char *outf
, *tempfile
;
189 CHECK_STRING (args
[0], 0);
192 /* Without asynchronous processes we cannot have BUFFER == 0. */
193 if (nargs
>= 3 && XTYPE (args
[2]) == Lisp_Int
)
194 error ("Operating system cannot handle asynchronous subprocesses");
195 #endif /* subprocesses */
197 if (nargs
>= 2 && ! NILP (args
[1]))
199 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
200 CHECK_STRING (infile
, 1);
203 infile
= build_string (NULL_DEVICE
);
207 register Lisp_Object tem
;
209 buffer
= tem
= args
[2];
212 || XFASTINT (tem
) == 0))
214 buffer
= Fget_buffer (tem
);
215 CHECK_BUFFER (buffer
, 2);
221 /* Make sure that the child will be able to chdir to the current
222 buffer's current directory, or its unhandled equivalent. We
223 can't just have the child check for an error when it does the
224 chdir, since it's in a vfork.
226 We have to GCPRO around this because Fexpand_file_name,
227 Funhandled_file_name_directory, and Ffile_accessible_directory_p
228 might call a file name handling function. The argument list is
229 protected by the caller, so all we really have to worry about is
232 struct gcpro gcpro1
, gcpro2
, gcpro3
;
234 current_dir
= current_buffer
->directory
;
236 GCPRO3 (infile
, buffer
, current_dir
);
239 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
241 if (NILP (Ffile_accessible_directory_p (current_dir
)))
242 report_file_error ("Setting current directory",
243 Fcons (current_buffer
->directory
, Qnil
));
248 display
= nargs
>= 4 ? args
[3] : Qnil
;
250 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
253 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
255 /* Search for program; barf if not found. */
259 GCPRO1 (current_dir
);
260 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
266 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
268 new_argv
[0] = XSTRING (path
)->data
;
271 for (i
= 4; i
< nargs
; i
++)
273 CHECK_STRING (args
[i
], i
);
274 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
279 #ifdef MSDOS /* MW, July 1993 */
280 /* These vars record information from process termination.
281 Clear them now before process can possibly terminate,
282 to avoid timing error if process terminates soon. */
283 synch_process_death
= 0;
284 synch_process_retcode
= 0;
286 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
287 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
290 tempfile
= alloca (20);
293 dostounix_filename (tempfile
);
294 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
295 strcat (tempfile
, "/");
296 strcat (tempfile
, "detmp.XXX");
299 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
303 report_file_error ("Opening process output file", Fcons (tempfile
, Qnil
));
307 if (XTYPE (buffer
) == Lisp_Int
)
308 fd
[1] = open (NULL_DEVICE
, O_WRONLY
), fd
[0] = -1;
315 /* Replaced by close_process_descs */
316 set_exclusive_use (fd
[0]);
321 /* child_setup must clobber environ in systems with true vfork.
322 Protect it from permanent change. */
323 register char **save_environ
= environ
;
324 register int fd1
= fd
[1];
326 #if 0 /* Some systems don't have sigblock. */
327 mask
= sigblock (sigmask (SIGCHLD
));
330 /* Record that we're about to create a synchronous process. */
331 synch_process_alive
= 1;
333 /* These vars record information from process termination.
334 Clear them now before process can possibly terminate,
335 to avoid timing error if process terminates soon. */
336 synch_process_death
= 0;
337 synch_process_retcode
= 0;
339 #ifdef MSDOS /* MW, July 1993 */
340 /* ??? Someone who knows MSDOG needs to check whether this properly
341 closes all descriptors that it opens. */
342 pid
= run_msdos_command (new_argv
, current_dir
, filefd
, outfilefd
);
344 fd1
= -1; /* No harm in closing that one! */
345 fd
[0] = open (tempfile
, NILP (Vbinary_process
) ? O_TEXT
: O_BINARY
);
350 report_file_error ("Cannot re-open temporary file", Qnil
);
352 #else /* not MSDOS */
364 child_setup (filefd
, fd1
, fd1
, new_argv
, 0, current_dir
);
366 #endif /* not MSDOS */
368 environ
= save_environ
;
370 /* Close most of our fd's, but not fd[0]
371 since we will use that to read input from. */
381 report_file_error ("Doing vfork", Qnil
);
384 if (XTYPE (buffer
) == Lisp_Int
)
389 /* If Emacs has been built with asynchronous subprocess support,
390 we don't need to do this, I think because it will then have
391 the facilities for handling SIGCHLD. */
392 wait_without_blocking ();
393 #endif /* subprocesses */
397 /* Enable sending signal if user quits below. */
398 call_process_exited
= 0;
401 /* MSDOS needs different cleanup information. */
402 record_unwind_protect (call_process_cleanup
,
403 Fcons (make_number (fd
[0]), build_string (tempfile
)));
405 record_unwind_protect (call_process_cleanup
,
406 Fcons (make_number (fd
[0]), make_number (pid
)));
407 #endif /* not MSDOS */
410 if (XTYPE (buffer
) == Lisp_Buffer
)
411 Fset_buffer (buffer
);
420 while ((nread
= read (fd
[0], buf
, sizeof buf
)) > 0)
425 if (!NILP (display
) && INTERACTIVE
)
428 prepare_menu_bars ();
430 redisplay_preserve_echo_area ();
437 /* Wait for it to terminate, unless it already has. */
438 wait_for_termination (pid
);
442 set_buffer_internal (old
);
444 /* Don't kill any children that the subprocess may have left behind
446 call_process_exited
= 1;
448 unbind_to (count
, Qnil
);
450 if (synch_process_death
)
451 return build_string (synch_process_death
);
452 return make_number (synch_process_retcode
);
457 delete_temp_file (name
)
460 unlink (XSTRING (name
)->data
);
463 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
465 "Send text from START to END to a synchronous process running PROGRAM.\n\
466 Delete the text if fourth arg DELETE is non-nil.\n\
467 Insert output in BUFFER before point; t means current buffer;\n\
468 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
469 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
470 Remaining args are passed to PROGRAM at startup as command args.\n\
471 If BUFFER is nil, returns immediately with value nil.\n\
472 Otherwise waits for PROGRAM to terminate\n\
473 and returns a numeric exit status or a signal description string.\n\
474 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
477 register Lisp_Object
*args
;
479 register Lisp_Object filename_string
, start
, end
;
485 int count
= specpdl_ptr
- specpdl
;
489 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
490 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
493 tempfile
= alloca (20);
496 dostounix_filename (tempfile
);
497 if (tempfile
[strlen (tempfile
) - 1] != '/')
498 strcat (tempfile
, "/");
499 strcat (tempfile
, "detmp.XXX");
500 #else /* not MSDOS */
503 strcpy (tempfile
, "tmp:emacsXXXXXX.");
505 strcpy (tempfile
, "/tmp/emacsXXXXXX");
507 #endif /* not MSDOS */
511 filename_string
= build_string (tempfile
);
514 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
);
515 record_unwind_protect (delete_temp_file
, filename_string
);
518 Fdelete_region (start
, end
);
520 args
[3] = filename_string
;
522 return unbind_to (count
, Fcall_process (nargs
- 2, args
+ 2));
525 #ifndef VMS /* VMS version is in vmsproc.c. */
527 /* This is the last thing run in a newly forked inferior
528 either synchronous or asynchronous.
529 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
530 Initialize inferior's priority, pgrp, connected dir and environment.
531 then exec another program based on new_argv.
533 This function may change environ for the superior process.
534 Therefore, the superior process must save and restore the value
535 of environ around the vfork and the call to this function.
537 ENV is the environment for the subprocess.
539 SET_PGRP is nonzero if we should put the subprocess into a separate
542 CURRENT_DIR is an elisp string giving the path of the current
543 directory the subprocess should have. Since we can't really signal
544 a decent error from within the child, this should be verified as an
545 executable directory by the parent. */
547 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
549 register char **new_argv
;
551 Lisp_Object current_dir
;
554 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
556 #else /* not MSDOS */
562 extern int emacs_priority
;
564 nice (- emacs_priority
);
568 /* Close Emacs's descriptors that this process should not have. */
569 close_process_descs ();
573 /* Note that use of alloca is always safe here. It's obvious for systems
574 that do not have true vfork or that have true (stack) alloca.
575 If using vfork and C_ALLOCA it is safe because that changes
576 the superior's static variables as if the superior had done alloca
577 and will be cleaned up in the usual way. */
579 register unsigned char *temp
;
582 i
= XSTRING (current_dir
)->size
;
583 temp
= (unsigned char *) alloca (i
+ 2);
584 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
585 if (temp
[i
- 1] != '/') temp
[i
++] = '/';
588 /* We can't signal an Elisp error here; we're in a vfork. Since
589 the callers check the current directory before forking, this
590 should only return an error if the directory's permissions
591 are changed between the check and this chdir, but we should
593 if (chdir (temp
) < 0)
597 /* Set `env' to a vector of the strings in Vprocess_environment. */
599 register Lisp_Object tem
;
600 register char **new_env
;
601 register int new_length
;
604 for (tem
= Vprocess_environment
;
605 (XTYPE (tem
) == Lisp_Cons
606 && XTYPE (XCONS (tem
)->car
) == Lisp_String
);
607 tem
= XCONS (tem
)->cdr
)
610 /* new_length + 1 to include terminating 0. */
611 env
= new_env
= (char **) alloca ((new_length
+ 1) * sizeof (char *));
613 /* Copy the Vprocess_environment strings into new_env. */
614 for (tem
= Vprocess_environment
;
615 (XTYPE (tem
) == Lisp_Cons
616 && XTYPE (XCONS (tem
)->car
) == Lisp_String
);
617 tem
= XCONS (tem
)->cdr
)
620 char *string
= (char *) XSTRING (XCONS (tem
)->car
)->data
;
621 /* See if this string duplicates any string already in the env.
622 If so, don't put it in.
623 When an env var has multiple definitions,
624 we keep the definition that comes first in process-environment. */
625 for (; ep
!= new_env
; ep
++)
627 char *p
= *ep
, *q
= string
;
631 /* The string is malformed; might as well drop it. */
646 /* Make sure that in, out, and err are not actually already in
647 descriptors zero, one, or two; this could happen if Emacs is
648 started with its standard in, out, or error closed, as might
650 in
= relocate_fd (in
, 3);
652 err
= out
= relocate_fd (out
, 3);
655 out
= relocate_fd (out
, 3);
656 err
= relocate_fd (err
, 3);
671 #ifndef SETPGRP_RELEASES_CTTY
672 setpgrp (); /* No arguments but equivalent in this case */
677 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
678 EMACS_SET_TTY_PGRP (0, &pid
);
681 something missing here
;
684 /* execvp does not accept an environment arg so the only way
685 to pass this environment is to set environ. Our caller
686 is responsible for restoring the ambient value of environ. */
688 execvp (new_argv
[0], new_argv
);
690 write (1, "Couldn't exec the program ", 26);
691 write (1, new_argv
[0], strlen (new_argv
[0]));
693 #endif /* not MSDOS */
696 /* Move the file descriptor FD so that its number is not less than MIN.
697 If the file descriptor is moved at all, the original is freed. */
699 relocate_fd (fd
, min
)
709 char *message1
= "Error while setting up child: ";
710 char *errmessage
= strerror (errno
);
711 char *message2
= "\n";
712 write (2, message1
, strlen (message1
));
713 write (2, errmessage
, strlen (errmessage
));
714 write (2, message2
, strlen (message2
));
717 /* Note that we hold the original FD open while we recurse,
718 to guarantee we'll get a new FD if we need it. */
719 new = relocate_fd (new, min
);
726 getenv_internal (var
, varlen
, value
, valuelen
)
734 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
738 entry
= XCONS (scan
)->car
;
739 if (XTYPE (entry
) == Lisp_String
740 && XSTRING (entry
)->size
> varlen
741 && XSTRING (entry
)->data
[varlen
] == '='
742 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
))
744 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
745 *valuelen
= XSTRING (entry
)->size
- (varlen
+ 1);
753 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
754 "Return the value of environment variable VAR, as a string.\n\
755 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
756 This function consults the variable ``process-environment'' for its value.")
763 CHECK_STRING (var
, 0);
764 if (getenv_internal (XSTRING (var
)->data
, XSTRING (var
)->size
,
766 return make_string (value
, valuelen
);
771 /* A version of getenv that consults process_environment, easily
780 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
788 /* This is run before init_cmdargs. */
792 char *data_dir
= egetenv ("EMACSDATA");
793 char *doc_dir
= egetenv ("EMACSDOC");
796 = Ffile_name_as_directory (build_string (data_dir
? data_dir
799 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
802 /* Check the EMACSPATH environment variable, defaulting to the
803 PATH_EXEC path from paths.h. */
804 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
805 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
806 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
809 /* This is run after init_cmdargs, so that Vinvocation_directory is valid. */
813 char *data_dir
= egetenv ("EMACSDATA");
818 if (initialized
&& !NILP (Vinstallation_directory
))
820 /* Add to the path the lib-src subdir of the installation dir. */
822 tem
= Fexpand_file_name (build_string ("lib-src"),
823 Vinstallation_directory
);
824 if (NILP (Fmember (tem
, Vexec_path
)))
826 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
827 Vexec_directory
= Ffile_name_as_directory (tem
);
829 /* If we use ../lib-src, maybe use ../etc as well.
830 Do so if ../etc exists and has our DOC-... file in it. */
833 tem
= Fexpand_file_name (build_string ("etc"),
834 Vinstallation_directory
);
835 Vdata_directory
= Ffile_name_as_directory (tem
);
840 tempdir
= Fdirectory_file_name (Vexec_directory
);
841 if (access (XSTRING (tempdir
)->data
, 0) < 0)
844 "Warning: arch-dependent data dir (%s) does not exist.\n",
845 XSTRING (Vexec_directory
)->data
);
849 tempdir
= Fdirectory_file_name (Vdata_directory
);
850 if (access (XSTRING (tempdir
)->data
, 0) < 0)
853 "Warning: arch-independent data dir (%s) does not exist.\n",
854 XSTRING (Vdata_directory
)->data
);
859 Vshell_file_name
= build_string ("*dcl*");
861 sh
= (char *) getenv ("SHELL");
862 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
866 set_process_environment ()
868 register char **envp
;
870 Vprocess_environment
= Qnil
;
874 for (envp
= environ
; *envp
; envp
++)
875 Vprocess_environment
= Fcons (build_string (*envp
),
876 Vprocess_environment
);
882 DEFVAR_LISP ("binary-process", &Vbinary_process
,
883 "*If non-nil then new subprocesses are assumed to produce binary output.");
884 Vbinary_process
= Qnil
;
887 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
888 "*File name to load inferior shells from.\n\
889 Initialized from the SHELL environment variable.");
891 DEFVAR_LISP ("exec-path", &Vexec_path
,
892 "*List of directories to search programs to run in subprocesses.\n\
893 Each element is a string (directory name) or nil (try default directory).");
895 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
896 "Directory of architecture-dependent files that come with GNU Emacs,\n\
897 especially executable programs intended for Emacs to invoke.");
899 DEFVAR_LISP ("data-directory", &Vdata_directory
,
900 "Directory of architecture-independent files that come with GNU Emacs,\n\
901 intended for Emacs to use.");
903 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
904 "Directory containing the DOC file that comes with GNU Emacs.\n\
905 This is usually the same as data-directory.");
907 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
908 "For internal use by the build procedure only.\n\
909 This is the name of the directory in which the build procedure installed\n\
910 Emacs's info files; the default value for Info-default-directory-list\n\
912 Vconfigure_info_directory
= build_string (PATH_INFO
);
914 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
915 "List of environment variables for subprocesses to inherit.\n\
916 Each element should be a string of the form ENVVARNAME=VALUE.\n\
917 The environment which Emacs inherits is placed in this variable\n\
918 when Emacs starts.");
921 defsubr (&Scall_process
);
924 defsubr (&Scall_process_region
);