1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 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 1, 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. */
26 /* Define SIGCHLD as an alias for SIGCLD. */
28 #if !defined (SIGCHLD) && defined (SIGCLD)
29 #define SIGCHLD SIGCLD
32 #include <sys/types.h>
33 #define PRIO_PROCESS 0
52 #include "syssignal.h"
55 extern noshare
char **environ
;
57 extern char **environ
;
60 #define max(a, b) ((a) > (b) ? (a) : (b))
62 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
;
64 Lisp_Object Vshell_file_name
;
66 Lisp_Object Vprocess_environment
;
68 /* True iff we are about to fork off a synchronous process or if we
69 are waiting for it. */
70 int synch_process_alive
;
72 /* Nonzero => this is a string explaining death of synchronous subprocess. */
73 char *synch_process_death
;
75 /* If synch_process_death is zero,
76 this is exit code of synchronous subprocess. */
77 int synch_process_retcode
;
79 #ifndef VMS /* VMS version is in vmsproc.c. */
82 call_process_kill (fdpid
)
85 close (XFASTINT (Fcar (fdpid
)));
86 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
87 synch_process_alive
= 0;
92 call_process_cleanup (fdpid
)
95 register int pid
= XFASTINT (Fcdr (fdpid
));
97 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
99 int count
= specpdl_ptr
- specpdl
;
100 record_unwind_protect (call_process_kill
, fdpid
);
101 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
104 wait_for_termination (pid
);
106 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
107 message1 ("Waiting for process to die...done");
109 synch_process_alive
= 0;
110 close (XFASTINT (Fcar (fdpid
)));
114 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
115 "Call PROGRAM synchronously in separate process.\n\
116 The program's input comes from file INFILE (nil means `/dev/null').\n\
117 Insert output in BUFFER before point; t means current buffer;\n\
118 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
119 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
120 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
121 If BUFFER is 0, returns immediately with value nil.\n\
122 Otherwise waits for PROGRAM to terminate\n\
123 and returns a numeric exit status or a signal description string.\n\
124 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
127 register Lisp_Object
*args
;
129 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
134 int count
= specpdl_ptr
- specpdl
;
135 register unsigned char **new_argv
136 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
137 struct buffer
*old
= current_buffer
;
141 CHECK_STRING (args
[0], 0);
143 if (nargs
>= 2 && ! NILP (args
[1]))
145 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
146 CHECK_STRING (infile
, 1);
150 infile
= build_string ("NLA0:");
152 infile
= build_string ("/dev/null");
157 register Lisp_Object tem
;
159 buffer
= tem
= args
[2];
162 || XFASTINT (tem
) == 0))
164 buffer
= Fget_buffer (tem
);
165 CHECK_BUFFER (buffer
, 2);
171 /* Make sure that the child will be able to chdir to the current
172 buffer's current directory, or its unhandled equivalent. We
173 can't just have the child check for an error when it does the
174 chdir, since it's in a vfork.
176 We have to GCPRO around this because Fexpand_file_name,
177 Funhandled_file_name_directory, and Ffile_accessible_directory_p
178 might call a file name handling function. The argument list is
179 protected by the caller, so all we really have to worry about is
182 struct gcpro gcpro1
, gcpro2
, gcpro3
;
184 current_dir
= current_buffer
->directory
;
186 GCPRO3 (infile
, buffer
, current_dir
);
189 expand_and_dir_to_file
190 (Funhandled_file_name_directory (current_dir
), Qnil
);
191 if (NILP (Ffile_accessible_directory_p (current_dir
)))
192 report_file_error ("Setting current directory",
193 Fcons (current_buffer
->directory
, Qnil
));
198 display
= nargs
>= 4 ? args
[3] : Qnil
;
202 for (i
= 4; i
< nargs
; i
++)
204 CHECK_STRING (args
[i
], i
);
205 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
207 /* Program name is first command arg */
208 new_argv
[0] = XSTRING (args
[0])->data
;
212 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
215 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
217 /* Search for program; barf if not found. */
218 openp (Vexec_path
, args
[0], "", &path
, 1);
222 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
224 new_argv
[0] = XSTRING (path
)->data
;
226 if (XTYPE (buffer
) == Lisp_Int
)
227 fd
[1] = open ("/dev/null", O_WRONLY
), fd
[0] = -1;
232 /* Replaced by close_process_descs */
233 set_exclusive_use (fd
[0]);
238 /* child_setup must clobber environ in systems with true vfork.
239 Protect it from permanent change. */
240 register char **save_environ
= environ
;
241 register int fd1
= fd
[1];
243 #if 0 /* Some systems don't have sigblock. */
244 mask
= sigblock (sigmask (SIGCHLD
));
247 /* Record that we're about to create a synchronous process. */
248 synch_process_alive
= 1;
261 child_setup (filefd
, fd1
, fd1
, new_argv
, 0, current_dir
);
265 /* Tell SIGCHLD handler to look for this pid. */
266 synch_process_pid
= pid
;
267 /* Now let SIGCHLD come through. */
271 environ
= save_environ
;
280 report_file_error ("Doing vfork", Qnil
);
283 if (XTYPE (buffer
) == Lisp_Int
)
286 /* If Emacs has been built with asynchronous subprocess support,
287 we don't need to do this, I think because it will then have
288 the facilities for handling SIGCHLD. */
289 wait_without_blocking ();
290 #endif /* subprocesses */
294 synch_process_death
= 0;
295 synch_process_retcode
= 0;
297 record_unwind_protect (call_process_cleanup
,
298 Fcons (make_number (fd
[0]), make_number (pid
)));
301 if (XTYPE (buffer
) == Lisp_Buffer
)
302 Fset_buffer (buffer
);
310 while ((nread
= read (fd
[0], buf
, sizeof buf
)) > 0)
315 if (!NILP (display
) && INTERACTIVE
)
316 redisplay_preserve_echo_area ();
322 /* Wait for it to terminate, unless it already has. */
323 wait_for_termination (pid
);
327 set_buffer_internal (old
);
329 unbind_to (count
, Qnil
);
331 if (synch_process_death
)
332 return build_string (synch_process_death
);
333 return make_number (synch_process_retcode
);
338 delete_temp_file (name
)
341 unlink (XSTRING (name
)->data
);
344 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
346 "Send text from START to END to a synchronous process running PROGRAM.\n\
347 Delete the text if fourth arg DELETE is non-nil.\n\
348 Insert output in BUFFER before point; t means current buffer;\n\
349 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
350 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
351 Remaining args are passed to PROGRAM at startup as command args.\n\
352 If BUFFER is nil, returns immediately with value nil.\n\
353 Otherwise waits for PROGRAM to terminate\n\
354 and returns a numeric exit status or a signal description string.\n\
355 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
358 register Lisp_Object
*args
;
360 register Lisp_Object filename_string
, start
, end
;
362 int count
= specpdl_ptr
- specpdl
;
365 strcpy (tempfile
, "tmp:emacsXXXXXX.");
367 strcpy (tempfile
, "/tmp/emacsXXXXXX");
371 filename_string
= build_string (tempfile
);
374 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
);
375 record_unwind_protect (delete_temp_file
, filename_string
);
378 Fdelete_region (start
, end
);
380 args
[3] = filename_string
;
382 return unbind_to (count
, Fcall_process (nargs
- 2, args
+ 2));
385 #ifndef VMS /* VMS version is in vmsproc.c. */
387 /* This is the last thing run in a newly forked inferior
388 either synchronous or asynchronous.
389 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
390 Initialize inferior's priority, pgrp, connected dir and environment.
391 then exec another program based on new_argv.
393 This function may change environ for the superior process.
394 Therefore, the superior process must save and restore the value
395 of environ around the vfork and the call to this function.
397 ENV is the environment for the subprocess.
399 SET_PGRP is nonzero if we should put the subprocess into a separate
402 CURRENT_DIR is an elisp string giving the path of the current
403 directory the subprocess should have. Since we can't really signal
404 a decent error from within the child, this should be verified as an
405 executable directory by the parent. */
407 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
409 register char **new_argv
;
411 Lisp_Object current_dir
;
415 register int pid
= getpid();
418 extern int emacs_priority
;
420 nice (- emacs_priority
);
424 /* Close Emacs's descriptors that this process should not have. */
425 close_process_descs ();
428 /* Note that use of alloca is always safe here. It's obvious for systems
429 that do not have true vfork or that have true (stack) alloca.
430 If using vfork and C_ALLOCA it is safe because that changes
431 the superior's static variables as if the superior had done alloca
432 and will be cleaned up in the usual way. */
434 register unsigned char *temp
;
437 i
= XSTRING (current_dir
)->size
;
438 temp
= (unsigned char *) alloca (i
+ 2);
439 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
440 if (temp
[i
- 1] != '/') temp
[i
++] = '/';
443 /* We can't signal an Elisp error here; we're in a vfork. Since
444 the callers check the current directory before forking, this
445 should only return an error if the directory's permissions
446 are changed between the check and this chdir, but we should
448 if (chdir (temp
) < 0)
452 /* Set `env' to a vector of the strings in Vprocess_environment. */
454 register Lisp_Object tem
;
455 register char **new_env
;
456 register int new_length
;
459 for (tem
= Vprocess_environment
;
460 (XTYPE (tem
) == Lisp_Cons
461 && XTYPE (XCONS (tem
)->car
) == Lisp_String
);
462 tem
= XCONS (tem
)->cdr
)
465 /* new_length + 1 to include terminating 0 */
466 env
= new_env
= (char **) alloca ((new_length
+ 1) * sizeof (char *));
468 /* Copy the Vprocess_alist strings into new_env. */
469 for (tem
= Vprocess_environment
;
470 (XTYPE (tem
) == Lisp_Cons
471 && XTYPE (XCONS (tem
)->car
) == Lisp_String
);
472 tem
= XCONS (tem
)->cdr
)
473 *new_env
++ = (char *) XSTRING (XCONS (tem
)->car
)->data
;
489 setpgrp (); /* No arguments but equivalent in this case */
493 setpgrp_of_tty (pid
);
496 something missing here
;
499 /* execvp does not accept an environment arg so the only way
500 to pass this environment is to set environ. Our caller
501 is responsible for restoring the ambient value of environ. */
503 execvp (new_argv
[0], new_argv
);
505 write (1, "Couldn't exec the program ", 26);
506 write (1, new_argv
[0], strlen (new_argv
[0]));
511 getenv_internal (var
, varlen
, value
, valuelen
)
519 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
521 Lisp_Object entry
= XCONS (scan
)->car
;
523 if (XTYPE (entry
) == Lisp_String
524 && XSTRING (entry
)->size
> varlen
525 && XSTRING (entry
)->data
[varlen
] == '='
526 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
))
528 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
529 *valuelen
= XSTRING (entry
)->size
- (varlen
+ 1);
537 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 2, 0,
538 "Return the value of environment variable VAR, as a string.\n\
539 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
540 This function consults the variable ``process-environment'' for its value.")
547 CHECK_STRING (var
, 0);
548 if (getenv_internal (XSTRING (var
)->data
, XSTRING (var
)->size
,
550 return make_string (value
, valuelen
);
555 /* A version of getenv that consults process_environment, easily
564 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
578 char *data_dir
= egetenv ("EMACSDATA");
581 Ffile_name_as_directory
582 (build_string (data_dir
? data_dir
: PATH_DATA
));
585 /* Check the EMACSPATH environment variable, defaulting to the
586 PATH_EXEC path from paths.h. */
587 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
588 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
589 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
591 tempdir
= Fdirectory_file_name (Vexec_directory
);
592 if (access (XSTRING (tempdir
)->data
, 0) < 0)
594 printf ("Warning: arch-dependent data dir (%s) does not exist.\n",
595 XSTRING (Vexec_directory
)->data
);
599 tempdir
= Fdirectory_file_name (Vdata_directory
);
600 if (access (XSTRING (tempdir
)->data
, 0) < 0)
602 printf ("Warning: arch-independent data dir (%s) does not exist.\n",
603 XSTRING (Vdata_directory
)->data
);
608 Vshell_file_name
= build_string ("*dcl*");
610 sh
= (char *) getenv ("SHELL");
611 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
615 set_process_environment ()
617 register char **envp
;
619 Vprocess_environment
= Qnil
;
623 for (envp
= environ
; *envp
; envp
++)
624 Vprocess_environment
= Fcons (build_string (*envp
),
625 Vprocess_environment
);
630 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
631 "*File name to load inferior shells from.\n\
632 Initialized from the SHELL environment variable.");
634 DEFVAR_LISP ("exec-path", &Vexec_path
,
635 "*List of directories to search programs to run in subprocesses.\n\
636 Each element is a string (directory name) or nil (try default directory).");
638 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
639 "Directory of architecture-dependent files that come with GNU Emacs,\n\
640 especially executable programs intended for Emacs to invoke.");
642 DEFVAR_LISP ("data-directory", &Vdata_directory
,
643 "Directory of architecture-independent files that come with GNU Emacs,\n\
644 intended for Emacs to use.");
646 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
647 "List of environment variables for subprocesses to inherit.\n\
648 Each element should be a string of the form ENVVARNAME=VALUE.\n\
649 The environment which Emacs inherits is placed in this variable\n\
650 when Emacs starts.");
653 defsubr (&Scall_process
);
656 defsubr (&Scall_process_region
);