296
|
1 /* Synchronous subprocess invocation for GNU Emacs.
|
|
2 Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
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)
|
|
9 any later version.
|
|
10
|
|
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.
|
|
15
|
|
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. */
|
|
19
|
|
20
|
|
21 #include <signal.h>
|
|
22
|
|
23 #include "config.h"
|
|
24
|
|
25 /* Define SIGCHLD as an alias for SIGCLD. */
|
|
26
|
|
27 #if !defined (SIGCHLD) && defined (SIGCLD)
|
|
28 #define SIGCHLD SIGCLD
|
|
29 #endif /* SIGCLD */
|
|
30
|
|
31 #include <sys/types.h>
|
|
32 #define PRIO_PROCESS 0
|
|
33 #include <sys/file.h>
|
|
34 #ifdef USG5
|
|
35 #include <fcntl.h>
|
|
36 #endif
|
|
37
|
|
38 #ifndef O_RDONLY
|
|
39 #define O_RDONLY 0
|
|
40 #endif
|
|
41
|
|
42 #ifndef O_WRONLY
|
|
43 #define O_WRONLY 1
|
|
44 #endif
|
|
45
|
|
46 #include "lisp.h"
|
|
47 #include "commands.h"
|
|
48 #include "buffer.h"
|
|
49 #include "paths.h"
|
|
50 #include "process.h"
|
|
51
|
|
52 #ifdef VMS
|
|
53 extern noshare char **environ;
|
|
54 #else
|
|
55 extern char **environ;
|
|
56 #endif
|
|
57
|
|
58 #define max(a, b) ((a) > (b) ? (a) : (b))
|
|
59
|
439
|
60 Lisp_Object Vexec_path, Vexec_directory, Vdata_directory;
|
296
|
61
|
|
62 Lisp_Object Vshell_file_name;
|
|
63
|
|
64 #ifndef MAINTAIN_ENVIRONMENT
|
|
65 /* List of strings to append to front of environment of
|
|
66 all subprocesses when they are started. */
|
|
67
|
|
68 Lisp_Object Vprocess_environment;
|
|
69 #endif
|
|
70
|
|
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;
|
|
74
|
|
75 /* Nonzero => this is a string explaining death of synchronous subprocess. */
|
|
76 char *synch_process_death;
|
|
77
|
|
78 /* If synch_process_death is zero,
|
|
79 this is exit code of synchronous subprocess. */
|
|
80 int synch_process_retcode;
|
|
81
|
|
82 #ifndef VMS /* VMS version is in vmsproc.c. */
|
|
83
|
|
84 Lisp_Object
|
|
85 call_process_cleanup (fdpid)
|
|
86 Lisp_Object fdpid;
|
|
87 {
|
|
88 register Lisp_Object fd, pid;
|
|
89 fd = Fcar (fdpid);
|
|
90 pid = Fcdr (fdpid);
|
|
91 close (XFASTINT (fd));
|
|
92 kill (XFASTINT (pid), SIGKILL);
|
|
93 synch_process_alive = 0;
|
|
94 return Qnil;
|
|
95 }
|
|
96
|
|
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.")
|
|
108 (nargs, args)
|
|
109 int nargs;
|
|
110 register Lisp_Object *args;
|
|
111 {
|
|
112 Lisp_Object display, buffer, path;
|
|
113 int fd[2];
|
|
114 int filefd;
|
|
115 register int pid;
|
|
116 char buf[1024];
|
|
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;
|
|
121 #if 0
|
|
122 int mask;
|
|
123 #endif
|
|
124 CHECK_STRING (args[0], 0);
|
|
125
|
|
126 if (nargs <= 1 || NULL (args[1]))
|
|
127 args[1] = build_string ("/dev/null");
|
|
128 else
|
|
129 args[1] = Fexpand_file_name (args[1], current_buffer->directory);
|
|
130
|
|
131 CHECK_STRING (args[1], 1);
|
|
132
|
|
133 {
|
|
134 register Lisp_Object tem;
|
|
135 buffer = tem = args[2];
|
|
136 if (nargs <= 2)
|
|
137 buffer = Qnil;
|
|
138 else if (!(EQ (tem, Qnil) || EQ (tem, Qt)
|
|
139 || XFASTINT (tem) == 0))
|
|
140 {
|
|
141 buffer = Fget_buffer (tem);
|
|
142 CHECK_BUFFER (buffer, 2);
|
|
143 }
|
|
144 }
|
|
145
|
|
146 display = nargs >= 3 ? args[3] : Qnil;
|
|
147
|
|
148 {
|
|
149 register int i;
|
|
150 for (i = 4; i < nargs; i++)
|
|
151 {
|
|
152 CHECK_STRING (args[i], i);
|
|
153 new_argv[i - 3] = XSTRING (args[i])->data;
|
|
154 }
|
|
155 /* Program name is first command arg */
|
|
156 new_argv[0] = XSTRING (args[0])->data;
|
|
157 new_argv[i - 3] = 0;
|
|
158 }
|
|
159
|
|
160 filefd = open (XSTRING (args[1])->data, O_RDONLY, 0);
|
|
161 if (filefd < 0)
|
|
162 {
|
|
163 report_file_error ("Opening process input file", Fcons (args[1], Qnil));
|
|
164 }
|
|
165 /* Search for program; barf if not found. */
|
|
166 openp (Vexec_path, args[0], "", &path, 1);
|
|
167 if (NULL (path))
|
|
168 {
|
|
169 close (filefd);
|
|
170 report_file_error ("Searching for program", Fcons (args[0], Qnil));
|
|
171 }
|
|
172 new_argv[0] = XSTRING (path)->data;
|
|
173
|
|
174 if (XTYPE (buffer) == Lisp_Int)
|
|
175 fd[1] = open ("/dev/null", O_WRONLY), fd[0] = -1;
|
|
176 else
|
|
177 {
|
|
178 pipe (fd);
|
|
179 #if 0
|
|
180 /* Replaced by close_process_descs */
|
|
181 set_exclusive_use (fd[0]);
|
|
182 #endif
|
|
183 }
|
|
184
|
|
185 {
|
|
186 /* child_setup must clobber environ in systems with true vfork.
|
|
187 Protect it from permanent change. */
|
|
188 register char **save_environ = environ;
|
|
189 register int fd1 = fd[1];
|
|
190 char **env;
|
|
191
|
|
192 #ifdef MAINTAIN_ENVIRONMENT
|
|
193 env = (char **) alloca (size_of_current_environ ());
|
|
194 get_current_environ (env);
|
|
195 #else
|
|
196 env = environ;
|
|
197 #endif /* MAINTAIN_ENVIRONMENT */
|
|
198
|
|
199 #if 0 /* Some systems don't have sigblock. */
|
|
200 mask = sigblock (sigmask (SIGCHLD));
|
|
201 #endif
|
|
202
|
|
203 /* Record that we're about to create a synchronous process. */
|
|
204 synch_process_alive = 1;
|
|
205
|
|
206 pid = vfork ();
|
|
207
|
|
208 if (pid == 0)
|
|
209 {
|
|
210 if (fd[0] >= 0)
|
|
211 close (fd[0]);
|
|
212 #ifdef USG
|
|
213 setpgrp ();
|
|
214 #else
|
|
215 setpgrp (pid, pid);
|
|
216 #endif /* USG */
|
|
217 child_setup (filefd, fd1, fd1, new_argv, env, 0);
|
|
218 }
|
|
219
|
|
220 #if 0
|
|
221 /* Tell SIGCHLD handler to look for this pid. */
|
|
222 synch_process_pid = pid;
|
|
223 /* Now let SIGCHLD come through. */
|
|
224 sigsetmask (mask);
|
|
225 #endif
|
|
226
|
|
227 environ = save_environ;
|
|
228
|
|
229 close (filefd);
|
|
230 close (fd1);
|
|
231 }
|
|
232
|
|
233 if (pid < 0)
|
|
234 {
|
|
235 close (fd[0]);
|
|
236 report_file_error ("Doing vfork", Qnil);
|
|
237 }
|
|
238
|
|
239 if (XTYPE (buffer) == Lisp_Int)
|
|
240 {
|
|
241 #ifndef subprocesses
|
|
242 wait_without_blocking ();
|
|
243 #endif /* subprocesses */
|
|
244 return Qnil;
|
|
245 }
|
|
246
|
|
247 record_unwind_protect (call_process_cleanup,
|
|
248 Fcons (make_number (fd[0]), make_number (pid)));
|
|
249
|
|
250
|
|
251 if (XTYPE (buffer) == Lisp_Buffer)
|
|
252 Fset_buffer (buffer);
|
|
253
|
|
254 immediate_quit = 1;
|
|
255 QUIT;
|
|
256
|
|
257 {
|
|
258 register int nread;
|
|
259
|
|
260 while ((nread = read (fd[0], buf, sizeof buf)) > 0)
|
|
261 {
|
|
262 immediate_quit = 0;
|
|
263 if (!NULL (buffer))
|
|
264 insert (buf, nread);
|
|
265 if (!NULL (display) && INTERACTIVE)
|
|
266 redisplay_preserve_echo_area ();
|
|
267 immediate_quit = 1;
|
|
268 QUIT;
|
|
269 }
|
|
270 }
|
|
271
|
|
272 /* Wait for it to terminate, unless it already has. */
|
|
273 wait_for_termination (pid);
|
|
274
|
|
275 immediate_quit = 0;
|
|
276
|
|
277 set_buffer_internal (old);
|
|
278
|
|
279 unbind_to (count, Qnil);
|
|
280
|
|
281 if (synch_process_death)
|
|
282 return build_string (synch_process_death);
|
|
283 return make_number (synch_process_retcode);
|
|
284 }
|
|
285 #endif
|
|
286
|
|
287 static void
|
|
288 delete_temp_file (name)
|
|
289 Lisp_Object name;
|
|
290 {
|
|
291 unlink (XSTRING (name)->data);
|
|
292 }
|
|
293
|
|
294 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
|
|
295 3, MANY, 0,
|
|
296 "Send text from START to END to a synchronous process running PROGRAM.\n\
|
|
297 Delete the text if fourth arg DELETE is non-nil.\n\
|
|
298 Insert output in BUFFER before point; t means current buffer;\n\
|
|
299 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
|
|
300 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
|
|
301 Remaining args are passed to PROGRAM at startup as command args.\n\
|
|
302 If BUFFER is nil, returns immediately with value nil.\n\
|
|
303 Otherwise waits for PROGRAM to terminate\n\
|
|
304 and returns a numeric exit status or a signal name as a string.\n\
|
|
305 If you quit, the process is killed with SIGKILL.")
|
|
306 (nargs, args)
|
|
307 int nargs;
|
|
308 register Lisp_Object *args;
|
|
309 {
|
|
310 register Lisp_Object filename_string, start, end;
|
|
311 char tempfile[20];
|
|
312 int count = specpdl_ptr - specpdl;
|
|
313
|
|
314 #ifdef VMS
|
|
315 strcpy (tempfile, "tmp:emacsXXXXXX.");
|
|
316 #else
|
|
317 strcpy (tempfile, "/tmp/emacsXXXXXX");
|
|
318 #endif
|
|
319 mktemp (tempfile);
|
|
320
|
|
321 filename_string = build_string (tempfile);
|
|
322 start = args[0];
|
|
323 end = args[1];
|
|
324 Fwrite_region (start, end, filename_string, Qnil, Qlambda);
|
|
325 record_unwind_protect (delete_temp_file, filename_string);
|
|
326
|
|
327 if (!NULL (args[3]))
|
|
328 Fdelete_region (start, end);
|
|
329
|
|
330 args[3] = filename_string;
|
|
331 Fcall_process (nargs - 2, args + 2);
|
|
332
|
|
333 return unbind_to (count, Qnil);
|
|
334 }
|
|
335
|
|
336 #ifndef VMS /* VMS version is in vmsproc.c. */
|
|
337
|
|
338 /* This is the last thing run in a newly forked inferior
|
|
339 either synchronous or asynchronous.
|
|
340 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
|
|
341 Initialize inferior's priority, pgrp, connected dir and environment.
|
|
342 then exec another program based on new_argv.
|
|
343
|
|
344 This function may change environ for the superior process.
|
|
345 Therefore, the superior process must save and restore the value
|
|
346 of environ around the vfork and the call to this function.
|
|
347
|
|
348 ENV is the environment for the subprocess.
|
|
349
|
|
350 SET_PGRP is nonzero if we should put the subprocess into a separate
|
|
351 process group. */
|
|
352
|
|
353 child_setup (in, out, err, new_argv, env, set_pgrp)
|
|
354 int in, out, err;
|
|
355 register char **new_argv;
|
|
356 char **env;
|
|
357 int set_pgrp;
|
|
358 {
|
|
359 register int pid = getpid();
|
|
360
|
|
361 setpriority (PRIO_PROCESS, pid, 0);
|
|
362
|
|
363 #ifdef subprocesses
|
|
364 /* Close Emacs's descriptors that this process should not have. */
|
|
365 close_process_descs ();
|
|
366 #endif
|
|
367
|
|
368 /* Note that use of alloca is always safe here. It's obvious for systems
|
|
369 that do not have true vfork or that have true (stack) alloca.
|
|
370 If using vfork and C_ALLOCA it is safe because that changes
|
|
371 the superior's static variables as if the superior had done alloca
|
|
372 and will be cleaned up in the usual way. */
|
|
373
|
|
374 if (XTYPE (current_buffer->directory) == Lisp_String)
|
|
375 {
|
|
376 register unsigned char *temp;
|
|
377 register int i;
|
|
378
|
|
379 i = XSTRING (current_buffer->directory)->size;
|
|
380 temp = (unsigned char *) alloca (i + 2);
|
|
381 bcopy (XSTRING (current_buffer->directory)->data, temp, i);
|
|
382 if (temp[i - 1] != '/') temp[i++] = '/';
|
|
383 temp[i] = 0;
|
|
384 /* Switch to that directory, and report any error. */
|
|
385 if (chdir (temp) < 0)
|
|
386 report_file_error ("In chdir",
|
|
387 Fcons (current_buffer->directory, Qnil));
|
|
388 }
|
|
389
|
|
390 #ifndef MAINTAIN_ENVIRONMENT
|
|
391 /* Set `env' to a vector of the strings in Vprocess_environment. */
|
|
392 {
|
|
393 register Lisp_Object tem;
|
|
394 register char **new_env;
|
|
395 register int new_length;
|
|
396
|
|
397 new_length = 0;
|
|
398 for (tem = Vprocess_environment;
|
|
399 (XTYPE (tem) == Lisp_Cons
|
|
400 && XTYPE (XCONS (tem)->car) == Lisp_String);
|
|
401 tem = XCONS (tem)->cdr)
|
|
402 new_length++;
|
|
403
|
|
404 /* new_length + 1 to include terminating 0 */
|
|
405 env = new_env = (char **) alloca ((new_length + 1) * sizeof (char *));
|
|
406
|
|
407 /* Copy the env strings into new_env. */
|
|
408 for (tem = Vprocess_environment;
|
|
409 (XTYPE (tem) == Lisp_Cons
|
|
410 && XTYPE (XCONS (tem)->car) == Lisp_String);
|
|
411 tem = XCONS (tem)->cdr)
|
|
412 *new_env++ = (char *) XSTRING (XCONS (tem)->car)->data;
|
|
413 *new_env = 0;
|
|
414 }
|
|
415 #endif /* Not MAINTAIN_ENVIRONMENT */
|
|
416
|
|
417 close (0);
|
|
418 close (1);
|
|
419 close (2);
|
|
420
|
|
421 dup2 (in, 0);
|
|
422 dup2 (out, 1);
|
|
423 dup2 (err, 2);
|
|
424 close (in);
|
|
425 close (out);
|
|
426 close (err);
|
|
427
|
|
428 setpgrp_of_tty (pid);
|
|
429
|
|
430 #ifdef vipc
|
|
431 something missing here;
|
|
432 #endif /* vipc */
|
|
433
|
|
434 /* execvp does not accept an environment arg so the only way
|
|
435 to pass this environment is to set environ. Our caller
|
|
436 is responsible for restoring the ambient value of environ. */
|
|
437 environ = env;
|
|
438 execvp (new_argv[0], new_argv);
|
|
439
|
|
440 write (1, "Couldn't exec the program ", 26);
|
|
441 write (1, new_argv[0], strlen (new_argv[0]));
|
|
442 _exit (1);
|
|
443 }
|
|
444
|
|
445 #endif /* not VMS */
|
|
446
|
|
447 init_callproc ()
|
|
448 {
|
|
449 register char * sh;
|
|
450 register char **envp;
|
439
|
451 Lisp_Object tempdir;
|
|
452
|
|
453 Vdata_directory = Ffile_name_as_directory (build_string (PATH_DATA));
|
296
|
454
|
|
455 /* Turn PATH_EXEC into a path. `==' is just a string which we know
|
|
456 will not be the name of an environment variable. */
|
|
457 Vexec_path = decode_env_path ("==", PATH_EXEC);
|
|
458 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
|
|
459 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
|
|
460
|
439
|
461 tempdir = Fdirectory_file_name (Vexec_directory);
|
|
462 if (access (XSTRING (tempdir)->data, 0) < 0)
|
296
|
463 {
|
439
|
464 printf ("Warning: arch-dependent data dir (%s) does not exist.\n",
|
296
|
465 XSTRING (Vexec_directory)->data);
|
|
466 sleep (2);
|
|
467 }
|
|
468
|
439
|
469 tempdir = Fdirectory_file_name (Vdata_directory);
|
|
470 if (access (XSTRING (tempdir)->data, 0) < 0)
|
|
471 {
|
|
472 printf ("Warning: arch-independent data dir (%s) does not exist.\n",
|
|
473 XSTRING (Vdata_directory)->data);
|
|
474 sleep (2);
|
|
475 }
|
|
476
|
296
|
477 #ifdef VMS
|
|
478 Vshell_file_name = build_string ("*dcl*");
|
|
479 #else
|
|
480 sh = (char *) egetenv ("SHELL");
|
|
481 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
|
|
482 #endif
|
|
483
|
|
484 #ifndef MAINTAIN_ENVIRONMENT
|
|
485 /* The equivalent of this operation was done
|
|
486 in init_environ in environ.c if MAINTAIN_ENVIRONMENT */
|
|
487 Vprocess_environment = Qnil;
|
|
488 #ifndef CANNOT_DUMP
|
|
489 if (initialized)
|
|
490 #endif
|
|
491 for (envp = environ; *envp; envp++)
|
|
492 Vprocess_environment = Fcons (build_string (*envp),
|
|
493 Vprocess_environment);
|
|
494 #endif /* MAINTAIN_ENVIRONMENT */
|
|
495 }
|
|
496
|
|
497 syms_of_callproc ()
|
|
498 {
|
|
499 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
|
|
500 "*File name to load inferior shells from.\n\
|
|
501 Initialized from the SHELL environment variable.");
|
|
502
|
|
503 DEFVAR_LISP ("exec-path", &Vexec_path,
|
|
504 "*List of directories to search programs to run in subprocesses.\n\
|
|
505 Each element is a string (directory name) or nil (try default directory).");
|
|
506
|
|
507 DEFVAR_LISP ("exec-directory", &Vexec_directory,
|
439
|
508 "Directory of architecture-dependent files that come with GNU Emacs,\n\
|
|
509 especially executable programs intended for Emacs to invoke.");
|
|
510
|
|
511 DEFVAR_LISP ("data-directory", &Vdata_directory,
|
|
512 "Directory of architecture-independent files that come with GNU Emacs,\n\
|
|
513 intended for Emacs to use.");
|
296
|
514
|
|
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.");
|
|
519 #endif
|
|
520
|
|
521 #ifndef VMS
|
|
522 defsubr (&Scall_process);
|
|
523 #endif
|
|
524 defsubr (&Scall_process_region);
|
|
525 }
|