1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988 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. */
25 /* Define SIGCHLD as an alias for SIGCLD. */
27 #if !defined (SIGCHLD) && defined (SIGCLD)
28 #define SIGCHLD SIGCLD
31 #include <sys/types.h>
32 #define PRIO_PROCESS 0
53 extern noshare
char **environ
;
55 extern char **environ
;
58 #define max(a, b) ((a) > (b) ? (a) : (b))
60 Lisp_Object Vexec_path
, Vexec_directory
;
62 Lisp_Object Vshell_file_name
;
64 #ifndef MAINTAIN_ENVIRONMENT
65 /* List of strings to append to front of environment of
66 all subprocesses when they are started. */
68 Lisp_Object Vprocess_environment
;
71 /* True iff we are about to fork off a synchronous process or if we
72 are waiting for it. */
73 int synch_process_alive
;
75 /* Nonzero => this is a string explaining death of synchronous subprocess. */
76 char *synch_process_death
;
78 /* If synch_process_death is zero,
79 this is exit code of synchronous subprocess. */
80 int synch_process_retcode
;
82 #ifndef VMS /* VMS version is in vmsproc.c. */
85 call_process_cleanup (fdpid
)
88 register Lisp_Object fd
, pid
;
91 close (XFASTINT (fd
));
92 kill (XFASTINT (pid
), SIGKILL
);
93 synch_process_alive
= 0;
97 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
98 "Call PROGRAM synchronously in separate process.\n\
99 The program's input comes from file INFILE (nil means `/dev/null').\n\
100 Insert output in BUFFER before point; t means current buffer;\n\
101 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
102 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
103 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
104 If BUFFER is nil or 0, returns immediately with value nil.\n\
105 Otherwise waits for PROGRAM to terminate\n\
106 and returns a numeric exit status or a signal name as a string.\n\
107 If you quit, the process is killed with SIGKILL.")
110 register Lisp_Object
*args
;
112 Lisp_Object display
, buffer
, path
;
117 int count
= specpdl_ptr
- specpdl
;
118 register unsigned char **new_argv
119 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
120 struct buffer
*old
= current_buffer
;
127 gcpro1
.nvars
= nargs
;
129 CHECK_STRING (args
[0], 0);
131 if (nargs
<= 1 || NULL (args
[1]))
132 args
[1] = build_string ("/dev/null");
134 args
[1] = Fexpand_file_name (args
[1], current_buffer
->directory
);
136 CHECK_STRING (args
[1], 1);
139 register Lisp_Object tem
;
140 buffer
= tem
= args
[2];
143 else if (!(EQ (tem
, Qnil
) || EQ (tem
, Qt
)
144 || XFASTINT (tem
) == 0))
146 buffer
= Fget_buffer (tem
);
147 CHECK_BUFFER (buffer
, 2);
151 display
= nargs
>= 3 ? args
[3] : Qnil
;
155 for (i
= 4; i
< nargs
; i
++)
157 CHECK_STRING (args
[i
], i
);
158 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
160 /* Program name is first command arg */
161 new_argv
[0] = XSTRING (args
[0])->data
;
165 filefd
= open (XSTRING (args
[1])->data
, O_RDONLY
, 0);
168 report_file_error ("Opening process input file", Fcons (args
[1], Qnil
));
170 /* Search for program; barf if not found. */
171 openp (Vexec_path
, args
[0], "", &path
, 1);
175 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
177 new_argv
[0] = XSTRING (path
)->data
;
179 if (XTYPE (buffer
) == Lisp_Int
)
180 fd
[1] = open ("/dev/null", O_WRONLY
), fd
[0] = -1;
185 /* Replaced by close_process_descs */
186 set_exclusive_use (fd
[0]);
191 /* child_setup must clobber environ in systems with true vfork.
192 Protect it from permanent change. */
193 register char **save_environ
= environ
;
194 register int fd1
= fd
[1];
197 #ifdef MAINTAIN_ENVIRONMENT
198 env
= (char **) alloca (size_of_current_environ ());
199 get_current_environ (env
);
202 #endif /* MAINTAIN_ENVIRONMENT */
204 #if 0 /* Some systems don't have sigblock. */
205 mask
= sigblock (sigmask (SIGCHLD
));
208 /* Record that we're about to create a synchronous process. */
209 synch_process_alive
= 1;
222 child_setup (filefd
, fd1
, fd1
, new_argv
, env
, 0);
226 /* Tell SIGCHLD handler to look for this pid. */
227 synch_process_pid
= pid
;
228 /* Now let SIGCHLD come through. */
232 environ
= save_environ
;
241 report_file_error ("Doing vfork", Qnil
);
244 if (XTYPE (buffer
) == Lisp_Int
)
247 wait_without_blocking ();
248 #endif /* subprocesses */
254 record_unwind_protect (call_process_cleanup
,
255 Fcons (make_number (fd
[0]), make_number (pid
)));
258 if (XTYPE (buffer
) == Lisp_Buffer
)
259 Fset_buffer (buffer
);
267 while ((nread
= read (fd
[0], buf
, sizeof buf
)) > 0)
272 if (!NULL (display
) && INTERACTIVE
)
273 redisplay_preserve_echo_area ();
279 /* Wait for it to terminate, unless it already has. */
280 wait_for_termination (pid
);
284 set_buffer_internal (old
);
286 unbind_to (count
, Qnil
);
290 if (synch_process_death
)
291 return build_string (synch_process_death
);
292 return make_number (synch_process_retcode
);
297 delete_temp_file (name
)
300 unlink (XSTRING (name
)->data
);
303 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
305 "Send text from START to END to a synchronous process running PROGRAM.\n\
306 Delete the text if fourth arg DELETE is non-nil.\n\
307 Insert output in BUFFER before point; t means current buffer;\n\
308 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
309 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
310 Remaining args are passed to PROGRAM at startup as command args.\n\
311 If BUFFER is nil, returns immediately with value nil.\n\
312 Otherwise waits for PROGRAM to terminate\n\
313 and returns a numeric exit status or a signal name as a string.\n\
314 If you quit, the process is killed with SIGKILL.")
317 register Lisp_Object
*args
;
319 register Lisp_Object filename_string
, start
, end
;
321 int count
= specpdl_ptr
- specpdl
;
328 strcpy (tempfile
, "tmp:emacsXXXXXX.");
330 strcpy (tempfile
, "/tmp/emacsXXXXXX");
334 filename_string
= build_string (tempfile
);
337 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
);
338 record_unwind_protect (delete_temp_file
, filename_string
);
341 Fdelete_region (start
, end
);
343 args
[3] = filename_string
;
344 Fcall_process (nargs
- 2, args
+ 2);
347 return unbind_to (count
, Qnil
);
350 #ifndef VMS /* VMS version is in vmsproc.c. */
352 /* This is the last thing run in a newly forked inferior
353 either synchronous or asynchronous.
354 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
355 Initialize inferior's priority, pgrp, connected dir and environment.
356 then exec another program based on new_argv.
358 This function may change environ for the superior process.
359 Therefore, the superior process must save and restore the value
360 of environ around the vfork and the call to this function.
362 ENV is the environment for the subprocess.
364 SET_PGRP is nonzero if we should put the subprocess into a separate
367 child_setup (in
, out
, err
, new_argv
, env
, set_pgrp
)
369 register char **new_argv
;
373 register int pid
= getpid();
375 setpriority (PRIO_PROCESS
, pid
, 0);
378 /* Close Emacs's descriptors that this process should not have. */
379 close_process_descs ();
382 /* Note that use of alloca is always safe here. It's obvious for systems
383 that do not have true vfork or that have true (stack) alloca.
384 If using vfork and C_ALLOCA it is safe because that changes
385 the superior's static variables as if the superior had done alloca
386 and will be cleaned up in the usual way. */
388 if (XTYPE (current_buffer
->directory
) == Lisp_String
)
390 register unsigned char *temp
;
393 i
= XSTRING (current_buffer
->directory
)->size
;
394 temp
= (unsigned char *) alloca (i
+ 2);
395 bcopy (XSTRING (current_buffer
->directory
)->data
, temp
, i
);
396 if (temp
[i
- 1] != '/') temp
[i
++] = '/';
398 /* Switch to that directory, and report any error. */
399 if (chdir (temp
) < 0)
400 report_file_error ("In chdir",
401 Fcons (current_buffer
->directory
, Qnil
));
404 #ifndef MAINTAIN_ENVIRONMENT
405 /* Set `env' to a vector of the strings in Vprocess_environment. */
407 register Lisp_Object tem
;
408 register char **new_env
;
409 register int new_length
;
412 for (tem
= Vprocess_environment
;
413 (XTYPE (tem
) == Lisp_Cons
414 && XTYPE (XCONS (tem
)->car
) == Lisp_String
);
415 tem
= XCONS (tem
)->cdr
)
418 /* new_length + 1 to include terminating 0 */
419 env
= new_env
= (char **) alloca ((new_length
+ 1) * sizeof (char *));
421 /* Copy the env strings into new_env. */
422 for (tem
= Vprocess_environment
;
423 (XTYPE (tem
) == Lisp_Cons
424 && XTYPE (XCONS (tem
)->car
) == Lisp_String
);
425 tem
= XCONS (tem
)->cdr
)
426 *new_env
++ = (char *) XSTRING (XCONS (tem
)->car
)->data
;
429 #endif /* Not MAINTAIN_ENVIRONMENT */
442 setpgrp_of_tty (pid
);
445 something missing here
;
448 /* execvp does not accept an environment arg so the only way
449 to pass this environment is to set environ. Our caller
450 is responsible for restoring the ambient value of environ. */
452 execvp (new_argv
[0], new_argv
);
454 write (1, "Couldn't exec the program ", 26);
455 write (1, new_argv
[0], strlen (new_argv
[0]));
464 register char **envp
;
467 /* Turn PATH_EXEC into a path. `==' is just a string which we know
468 will not be the name of an environment variable. */
469 Vexec_path
= decode_env_path ("==", PATH_EXEC
);
470 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
471 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
473 execdir
= Fdirectory_file_name (Vexec_directory
);
474 if (access (XSTRING (execdir
)->data
, 0) < 0)
476 printf ("Warning: executable/documentation dir (%s) does not exist.\n",
477 XSTRING (Vexec_directory
)->data
);
482 Vshell_file_name
= build_string ("*dcl*");
484 sh
= (char *) egetenv ("SHELL");
485 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
488 #ifndef MAINTAIN_ENVIRONMENT
489 /* The equivalent of this operation was done
490 in init_environ in environ.c if MAINTAIN_ENVIRONMENT */
491 Vprocess_environment
= Qnil
;
495 for (envp
= environ
; *envp
; envp
++)
496 Vprocess_environment
= Fcons (build_string (*envp
),
497 Vprocess_environment
);
498 #endif /* MAINTAIN_ENVIRONMENT */
503 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
504 "*File name to load inferior shells from.\n\
505 Initialized from the SHELL environment variable.");
507 DEFVAR_LISP ("exec-path", &Vexec_path
,
508 "*List of directories to search programs to run in subprocesses.\n\
509 Each element is a string (directory name) or nil (try default directory).");
511 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
512 "Directory that holds programs that come with GNU Emacs,\n\
513 intended for Emacs to invoke.");
515 #ifndef MAINTAIN_ENVIRONMENT
516 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
517 "List of strings to append to environment of subprocesses that are started.\n\
518 Each string should have the format ENVVARNAME=VALUE.");
522 defsubr (&Scall_process
);
524 defsubr (&Scall_process_region
);