1 /* Synchronous subprocess invocation for XEmacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Mule 2.0, FSF 19.30. */
22 /* Partly sync'ed with 19.36.4 */
35 #include "file-coding.h"
40 #include "sysfile.h" /* Always include after sysproc.h */
41 #include "syssignal.h" /* Always include before systty.h */
45 #define _P_NOWAIT 1 /* from process.h */
51 /* When we are starting external processes we need to know whether they
52 take binary input (no conversion) or text input (\n is converted to
53 \r\n). Similarly for output: if newlines are written as \r\n then it's
54 text process output, otherwise it's binary. */
55 Lisp_Object Vbinary_process_input;
56 Lisp_Object Vbinary_process_output;
59 Lisp_Object Vshell_file_name;
61 /* The environment to pass to all subprocesses when they are started.
62 This is in the semi-bogus format of ("VAR=VAL" "VAR2=VAL2" ... )
64 Lisp_Object Vprocess_environment;
66 /* True iff we are about to fork off a synchronous process or if we
67 are waiting for it. */
68 volatile int synch_process_alive;
70 /* Nonzero => this is a string explaining death of synchronous subprocess. */
71 CONST char *synch_process_death;
73 /* If synch_process_death is zero,
74 this is exit code of synchronous subprocess. */
75 int synch_process_retcode;
77 /* Clean up when exiting Fcall_process_internal.
78 On MSDOS, delete the temporary file on any kind of termination.
79 On Unix, kill the process and any children on termination by signal. */
81 /* Nonzero if this is termination due to exit. */
82 static int call_process_exited;
84 Lisp_Object Vlisp_EXEC_SUFFIXES;
87 call_process_kill (Lisp_Object fdpid)
89 Lisp_Object fd = Fcar (fdpid);
90 Lisp_Object pid = Fcdr (fdpid);
96 EMACS_KILLPG (XINT (pid), SIGKILL);
98 synch_process_alive = 0;
103 call_process_cleanup (Lisp_Object fdpid)
105 int fd = XINT (Fcar (fdpid));
106 int pid = XINT (Fcdr (fdpid));
108 if (!call_process_exited &&
109 EMACS_KILLPG (pid, SIGINT) == 0)
111 int speccount = specpdl_depth ();
113 record_unwind_protect (call_process_kill, fdpid);
114 /* #### "c-G" -- need non-consing Single-key-description */
115 message ("Waiting for process to die...(type C-g again to kill it instantly)");
117 wait_for_termination (pid);
119 /* "Discard" the unwind protect. */
122 unbind_to (speccount, Qnil);
124 message ("Waiting for process to die... done");
126 synch_process_alive = 0;
131 static Lisp_Object fork_error;
134 report_fork_error (char *string, Lisp_Object data)
136 Lisp_Object errstring = lisp_strerror (errno);
138 fork_error = Fcons (build_string (string), Fcons (errstring, data));
140 /* terminate this branch of the fork, without closing stdin/out/etc. */
145 DEFUN ("call-process-internal", Fcall_process_internal, 1, MANY, 0, /*
146 Call PROGRAM synchronously in separate process, with coding-system specified.
148 (PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS).
149 The program's input comes from file INFILE (nil means `/dev/null').
150 Insert output in BUFFER before point; t means current buffer;
151 nil for BUFFER means discard it; 0 means discard and don't wait.
152 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
153 REAL-BUFFER says what to do with standard output, as above,
154 while STDERR-FILE says what to do with standard error in the child.
155 STDERR-FILE may be nil (discard standard error output),
156 t (mix it with ordinary output), or a file name string.
158 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
159 Remaining arguments are strings passed as command arguments to PROGRAM.
161 If BUFFER is 0, `call-process' returns immediately with value nil.
162 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
163 or a signal description string.
164 If you quit, the process is killed with SIGINT, or SIGKILL if you
167 (int nargs, Lisp_Object *args))
169 /* This function can GC */
170 Lisp_Object infile, buffer, current_dir, display, path;
177 int speccount = specpdl_depth ();
178 struct gcpro gcpro1, gcpro2;
179 char **new_argv = alloca_array (char *, max (2, nargs - 2));
181 /* File to use for stderr in the child.
182 t means use same as standard output. */
183 Lisp_Object error_file;
185 CHECK_STRING (args[0]);
189 #if defined (NO_SUBPROCESSES)
190 /* Without asynchronous processes we cannot have BUFFER == 0. */
191 if (nargs >= 3 && !INTP (args[2]))
192 error ("Operating system cannot handle asynchronous subprocesses");
193 #endif /* NO_SUBPROCESSES */
195 /* Do this before building new_argv because GC in Lisp code
196 * called by various filename-hacking routines might relocate strings */
197 locate_file (Vexec_path, args[0], Vlisp_EXEC_SUFFIXES, &path, X_OK);
199 /* Make sure that the child will be able to chdir to the current
200 buffer's current directory, or its unhandled equivalent. We
201 can't just have the child check for an error when it does the
202 chdir, since it's in a vfork. */
204 struct gcpro ngcpro1, ngcpro2;
205 /* Do this test before building new_argv because GC in Lisp code
206 * called by various filename-hacking routines might relocate strings */
207 /* Make sure that the child will be able to chdir to the current
208 buffer's current directory. We can't just have the child check
209 for an error when it does the chdir, since it's in a vfork. */
211 NGCPRO2 (current_dir, path); /* Caller gcprotects args[] */
212 current_dir = current_buffer->directory;
213 current_dir = Funhandled_file_name_directory (current_dir);
214 current_dir = expand_and_dir_to_file (current_dir, Qnil);
216 /* This is in FSF, but it breaks everything in the presence of
217 ange-ftp-visited files, so away with it. */
218 if (NILP (Ffile_accessible_directory_p (current_dir)))
219 report_file_error ("Setting current directory",
220 Fcons (current_buffer->directory, Qnil));
225 GCPRO1 (current_dir);
227 if (nargs >= 2 && ! NILP (args[1]))
229 struct gcpro ngcpro1;
230 NGCPRO1 (current_buffer->directory);
231 infile = Fexpand_file_name (args[1], current_buffer->directory);
233 CHECK_STRING (infile);
236 infile = build_string (NULL_DEVICE);
240 GCPRO2 (infile, current_dir); /* Fexpand_file_name might trash it */
246 /* If BUFFER is a list, its meaning is
247 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
250 if (CONSP (XCDR (buffer)))
252 Lisp_Object file_for_stderr = XCAR (XCDR (buffer));
254 if (NILP (file_for_stderr) || EQ (Qt, file_for_stderr))
255 error_file = file_for_stderr;
257 error_file = Fexpand_file_name (file_for_stderr, Qnil);
260 buffer = XCAR (buffer);
263 if (!(EQ (buffer, Qnil)
267 Lisp_Object spec_buffer = buffer;
268 buffer = Fget_buffer (buffer);
269 /* Mention the buffer name for a better error message. */
271 CHECK_BUFFER (spec_buffer);
272 CHECK_BUFFER (buffer);
280 display = ((nargs >= 4) ? args[3] : Qnil);
282 /* From here we assume we won't GC (unless an error is signaled). */
285 for (i = 4; i < nargs; i++)
287 CHECK_STRING (args[i]);
288 new_argv[i - 3] = (char *) XSTRING_DATA (args[i]);
290 new_argv[nargs - 3] = 0;
294 report_file_error ("Searching for program", Fcons (args[0], Qnil));
295 new_argv[0] = (char *) XSTRING_DATA (path);
297 filefd = open ((char *) XSTRING_DATA (infile), O_RDONLY | OPEN_BINARY, 0);
299 report_file_error ("Opening process input file", Fcons (infile, Qnil));
303 fd[1] = open (NULL_DEVICE, O_WRONLY | OPEN_BINARY, 0);
310 /* Replaced by close_process_descs */
311 set_exclusive_use (fd[0]);
316 /* child_setup must clobber environ in systems with true vfork.
317 Protect it from permanent change. */
318 REGISTER char **save_environ = environ;
319 REGISTER int fd1 = fd[1];
325 /* Record that we're about to create a synchronous process. */
326 synch_process_alive = 1;
328 /* These vars record information from process termination.
329 Clear them now before process can possibly terminate,
330 to avoid timing error if process terminates soon. */
331 synch_process_death = 0;
332 synch_process_retcode = 0;
334 if (NILP (error_file))
335 fd_error = open (NULL_DEVICE, O_WRONLY | OPEN_BINARY);
336 else if (STRINGP (error_file))
338 fd_error = open ((CONST char *) XSTRING_DATA (error_file),
340 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
342 #else /* not DOS_NT */
343 O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
345 #endif /* not DOS_NT */
355 report_file_error ("Cannot open", Fcons(error_file, Qnil));
360 pid = child_setup (filefd, fd1, fd_error, new_argv,
361 (char *) XSTRING_DATA (current_dir));
362 #else /* not WINDOWSNT */
369 /* This is necessary because some shells may attempt to
370 access the current controlling terminal and will hang
371 if they are run in the background, as will be the case
372 when XEmacs is started in the background. Martin
373 Buchholz observed this problem running a subprocess
374 that used zsh to call gzip to uncompress an info
376 disconnect_controlling_terminal ();
377 child_setup (filefd, fd1, fd_error, new_argv,
378 (char *) XSTRING_DATA (current_dir));
383 #endif /* not WINDOWSNT */
385 environ = save_environ;
387 /* Close most of our fd's, but not fd[0]
388 since we will use that to read input from. */
394 if (!NILP (fork_error))
395 signal_error (Qfile_error, fork_error);
401 report_file_error ("Doing fork", Qnil);
408 #if defined (NO_SUBPROCESSES)
409 /* If Emacs has been built with asynchronous subprocess support,
410 we don't need to do this, I think because it will then have
411 the facilities for handling SIGCHLD. */
412 wait_without_blocking ();
413 #endif /* NO_SUBPROCESSES */
421 Lisp_Object instream;
422 struct gcpro ngcpro1;
424 /* Enable sending signal if user quits below. */
425 call_process_exited = 0;
427 record_unwind_protect (call_process_cleanup,
428 Fcons (make_int (fd[0]), make_int (pid)));
430 /* FSFmacs calls Fset_buffer() here. We don't have to because
431 we can insert into buffers other than the current one. */
433 XSETBUFFER (buffer, current_buffer);
434 instream = make_filedesc_input_stream (fd[0], 0, -1, LSTR_ALLOW_QUIT);
437 make_decoding_input_stream
438 (XLSTREAM (instream),
439 Fget_coding_system (Vcoding_system_for_read));
440 Lstream_set_character_mode (XLSTREAM (instream));
446 /* Repeatedly read until we've filled as much as possible
447 of the buffer size we have. But don't read
448 less than 1024--save that for the next bufferfull. */
451 while (nread < bufsize - 1024)
454 = Lstream_read (XLSTREAM (instream), bufptr + nread,
468 /* Now NREAD is the total amount of data in the buffer. */
473 /* Until we pull out of MULE things like
474 make_decoding_input_stream(), we do the following which is
475 less elegant. --marcpa */
478 if (NILP (Vbinary_process_output)) {
479 nread = crlf_to_lf(nread, bufptr, &lf_count);
487 buffer_insert_raw_string (XBUFFER (buffer), (Bufbyte *) bufptr,
490 /* Make the buffer bigger as we continue to read more data,
492 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
495 bufptr = (char *) alloca (bufsize);
498 if (!NILP (display) && INTERACTIVE)
505 Lstream_close (XLSTREAM (instream));
509 /* Wait for it to terminate, unless it already has. */
510 wait_for_termination (pid);
512 /* Don't kill any children that the subprocess may have left behind
514 call_process_exited = 1;
515 unbind_to (speccount, Qnil);
517 if (synch_process_death)
518 return build_string (synch_process_death);
519 return make_int (synch_process_retcode);
525 /* Move the file descriptor FD so that its number is not less than MIN. *
526 The original file descriptor remains open. */
528 relocate_fd (int fd, int min)
534 int newfd = dup (fd);
537 stderr_out ("Error while setting up child: %s\n",
541 return relocate_fd (newfd, min);
545 /* This is the last thing run in a newly forked inferior
546 either synchronous or asynchronous.
547 Copy descriptors IN, OUT and ERR
548 as descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.
549 Initialize inferior's priority, pgrp, connected dir and environment.
550 then exec another program based on new_argv.
552 This function may change environ for the superior process.
553 Therefore, the superior process must save and restore the value
554 of environ around the fork and the call to this function.
556 ENV is the environment for the subprocess.
558 XEmacs: We've removed the SET_PGRP argument because it's already
559 done by the callers of child_setup.
561 CURRENT_DIR is an elisp string giving the path of the current
562 directory the subprocess should have. Since we can't really signal
563 a decent error from within the child, this should be verified as an
564 executable directory by the parent. */
571 child_setup (int in, int out, int err, char **new_argv,
572 CONST char *current_dir)
579 #endif /* WINDOWSNT */
581 #ifdef SET_EMACS_PRIORITY
582 if (emacs_priority != 0)
583 nice (- emacs_priority);
586 #if !defined (NO_SUBPROCESSES) && !defined (WINDOWSNT)
587 /* Close Emacs's descriptors that this process should not have. */
588 close_process_descs ();
589 #endif /* not NO_SUBPROCESSES */
592 /* Note that use of alloca is always safe here. It's obvious for systems
593 that do not have true vfork or that have true (stack) alloca.
594 If using vfork and C_ALLOCA it is safe because that changes
595 the superior's static variables as if the superior had done alloca
596 and will be cleaned up in the usual way. */
600 i = strlen (current_dir);
601 pwd = alloca_array (char, i + 6);
602 memcpy (pwd, "PWD=", 4);
603 memcpy (pwd + 4, current_dir, i);
605 if (!IS_DIRECTORY_SEP (pwd[i - 1]))
606 pwd[i++] = DIRECTORY_SEP;
609 /* We can't signal an Elisp error here; we're in a vfork. Since
610 the callers check the current directory before forking, this
611 should only return an error if the directory's permissions
612 are changed between the check and this chdir, but we should
614 if (chdir (pwd + 4) < 0)
616 /* Don't report the chdir error, or ange-ftp.el doesn't work. */
617 /* (FSFmacs does _exit (errno) here.) */
622 /* Strip trailing "/". Cretinous *[]&@$#^%@#$% Un*x */
623 /* leave "//" (from FSF) */
624 while (i > 6 && IS_DIRECTORY_SEP (pwd[i - 1]))
629 /* Set `env' to a vector of the strings in Vprocess_environment. */
630 /* + 2 to include PWD and terminating 0. */
631 env = alloca_array (char *, XINT (Flength (Vprocess_environment)) + 2);
633 REGISTER Lisp_Object tail;
634 char **new_env = env;
636 /* If we have a PWD envvar and we know the real current directory,
637 pass one down, but with corrected value. */
638 if (pwd && getenv ("PWD"))
641 /* Copy the Vprocess_environment strings into new_env. */
642 for (tail = Vprocess_environment;
643 CONSP (tail) && STRINGP (XCAR (tail));
647 char *envvar_external;
648 Bufbyte *envvar_internal = XSTRING_DATA (XCAR (tail));
650 GET_C_CHARPTR_EXT_FILENAME_DATA_ALLOCA (envvar_internal, envvar_external);
652 /* See if envvar_external duplicates any string already in the env.
653 If so, don't put it in.
654 When an env var has multiple definitions,
655 we keep the definition that comes first in process-environment. */
656 for (; ep != new_env; ep++)
658 char *p = *ep, *q = envvar_external;
662 /* The string is malformed; might as well drop it. */
671 if (pwd && !strncmp ("PWD=", envvar_external, 4))
677 *new_env++ = envvar_external;
685 prepare_standard_handles (in, out, err, handles);
686 set_process_dir (current_dir);
687 #else /* not WINDOWSNT */
688 /* Make sure that in, out, and err are not actually already in
689 descriptors zero, one, or two; this could happen if Emacs is
690 started with its standard in, out, or error closed, as might
692 in = relocate_fd (in, 3);
693 out = relocate_fd (out, 3);
694 err = relocate_fd (err, 3);
696 /* Set the standard input/output channels of the new process. */
697 close (STDIN_FILENO);
698 close (STDOUT_FILENO);
699 close (STDERR_FILENO);
701 dup2 (in, STDIN_FILENO);
702 dup2 (out, STDOUT_FILENO);
703 dup2 (err, STDERR_FILENO);
709 /* I can't think of any reason why child processes need any more
710 than the standard 3 file descriptors. It would be cleaner to
711 close just the ones that need to be, but the following brute
712 force approach is certainly effective, and not too slow. */
715 for (fd=3; fd<=64; fd++)
718 #endif /* not WINDOWSNT */
721 something missing here;
725 /* Spawn the child. (See ntproc.c:Spawnve). */
726 cpid = spawnve (_P_NOWAIT, new_argv[0], (CONST char* CONST*)new_argv,
727 (CONST char* CONST*)env);
729 /* An error occurred while trying to spawn the process. */
730 report_file_error ("Spawning child process", Qnil);
731 reset_standard_handles (in, out, err, handles);
733 #else /* not WINDOWSNT */
734 /* execvp does not accept an environment arg so the only way
735 to pass this environment is to set environ. Our caller
736 is responsible for restoring the ambient value of environ. */
738 execvp (new_argv[0], new_argv);
740 stdout_out ("Can't exec program %s\n", new_argv[0]);
742 #endif /* not WINDOWSNT */
746 getenv_internal (CONST Bufbyte *var,
753 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
755 Lisp_Object entry = XCAR (scan);
758 && XSTRING_LENGTH (entry) > varlen
759 && XSTRING_BYTE (entry, varlen) == '='
761 /* NT environment variables are case insensitive. */
762 && ! memicmp (XSTRING_DATA (entry), var, varlen)
763 #else /* not WINDOWSNT */
764 && ! memcmp (XSTRING_DATA (entry), var, varlen)
765 #endif /* not WINDOWSNT */
768 *value = XSTRING_DATA (entry) + (varlen + 1);
769 *valuelen = XSTRING_LENGTH (entry) - (varlen + 1);
777 DEFUN ("getenv", Fgetenv, 1, 2, "sEnvironment variable: \np", /*
778 Return the value of environment variable VAR, as a string.
779 VAR is a string, the name of the variable.
780 When invoked interactively, prints the value in the echo area.
786 Lisp_Object v = Qnil;
791 if (getenv_internal (XSTRING_DATA (var), XSTRING_LENGTH (var),
793 v = make_string (value, valuelen);
794 if (!NILP (interactivep))
797 message ("%s not defined in environment", XSTRING_DATA (var));
799 /* #### Should use Fprin1_to_string or Fprin1 to handle string
800 containing quotes correctly. */
801 message ("\"%s\"", value);
806 /* A version of getenv that consults process_environment, easily
809 egetenv (CONST char *var)
814 if (getenv_internal ((CONST Bufbyte *) var, strlen (var), &value, &valuelen))
815 return (char *) value;
824 /* This function can GC */
827 /* jwz: always initialize Vprocess_environment, so that egetenv()
830 Vprocess_environment = Qnil;
831 for (envp = environ; envp && *envp; envp++)
833 Vprocess_environment = Fcons (build_ext_string (*envp, FORMAT_OS),
834 Vprocess_environment);
839 /* Initialize shell-file-name from environment variables or best guess. */
841 CONST char *shell = egetenv ("COMSPEC");
842 if (!shell) shell = "\\WINNT\\system32\\cmd.exe";
843 #else /* not WINDOWSNT */
844 CONST char *shell = egetenv ("SHELL");
845 if (!shell) shell = "/bin/sh";
848 Vshell_file_name = build_string (shell);
854 set_process_environment (void)
856 REGISTER char **envp;
858 Vprocess_environment = Qnil;
862 for (envp = environ; *envp; envp++)
863 Vprocess_environment = Fcons (build_string (*envp),
864 Vprocess_environment);
869 syms_of_callproc (void)
871 DEFSUBR (Fcall_process_internal);
876 vars_of_callproc (void)
878 /* This function can GC */
880 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input /*
881 *If non-nil then new subprocesses are assumed to take binary input.
883 Vbinary_process_input = Qnil;
885 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output /*
886 *If non-nil then new subprocesses are assumed to produce binary output.
888 Vbinary_process_output = Qnil;
891 DEFVAR_LISP ("shell-file-name", &Vshell_file_name /*
892 *File name to load inferior shells from.
893 Initialized from the SHELL environment variable.
896 DEFVAR_LISP ("process-environment", &Vprocess_environment /*
897 List of environment variables for subprocesses to inherit.
898 Each element should be a string of the form ENVVARNAME=VALUE.
899 The environment which Emacs inherits is placed in this variable
903 Vlisp_EXEC_SUFFIXES = build_string (EXEC_SUFFIXES);
904 staticpro (&Vlisp_EXEC_SUFFIXES);