]> code.delx.au - gnu-emacs/blob - src/callproc.c
(comint-preinput-scroll-to-bottom): If SCROLL is `this',
[gnu-emacs] / src / callproc.c
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993 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 #include <errno.h>
23
24 #include <config.h>
25
26 extern int errno;
27 #ifndef VMS
28 extern char *sys_errlist[];
29 #endif
30
31 /* Define SIGCHLD as an alias for SIGCLD. */
32
33 #if !defined (SIGCHLD) && defined (SIGCLD)
34 #define SIGCHLD SIGCLD
35 #endif /* SIGCLD */
36
37 #include <sys/types.h>
38
39 #include <sys/file.h>
40 #ifdef USG5
41 #include <fcntl.h>
42 #endif
43
44 #ifndef O_RDONLY
45 #define O_RDONLY 0
46 #endif
47
48 #ifndef O_WRONLY
49 #define O_WRONLY 1
50 #endif
51
52 #include "lisp.h"
53 #include "commands.h"
54 #include "buffer.h"
55 #include <paths.h>
56 #include "process.h"
57 #include "syssignal.h"
58
59 #ifdef VMS
60 extern noshare char **environ;
61 #else
62 extern char **environ;
63 #endif
64
65 #define max(a, b) ((a) > (b) ? (a) : (b))
66
67 Lisp_Object Vexec_path, Vexec_directory, Vdata_directory;
68 Lisp_Object Vconfigure_info_directory;
69
70 Lisp_Object Vshell_file_name;
71
72 Lisp_Object Vprocess_environment;
73
74 /* True iff we are about to fork off a synchronous process or if we
75 are waiting for it. */
76 int synch_process_alive;
77
78 /* Nonzero => this is a string explaining death of synchronous subprocess. */
79 char *synch_process_death;
80
81 /* If synch_process_death is zero,
82 this is exit code of synchronous subprocess. */
83 int synch_process_retcode;
84 \f
85 #ifndef VMS /* VMS version is in vmsproc.c. */
86
87 static Lisp_Object
88 call_process_kill (fdpid)
89 Lisp_Object fdpid;
90 {
91 close (XFASTINT (Fcar (fdpid)));
92 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
93 synch_process_alive = 0;
94 return Qnil;
95 }
96
97 Lisp_Object
98 call_process_cleanup (fdpid)
99 Lisp_Object fdpid;
100 {
101 register int pid = XFASTINT (Fcdr (fdpid));
102
103 if (EMACS_KILLPG (pid, SIGINT) == 0)
104 {
105 int count = specpdl_ptr - specpdl;
106 record_unwind_protect (call_process_kill, fdpid);
107 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
108 immediate_quit = 1;
109 QUIT;
110 wait_for_termination (pid);
111 immediate_quit = 0;
112 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
113 message1 ("Waiting for process to die...done");
114 }
115 synch_process_alive = 0;
116 close (XFASTINT (Fcar (fdpid)));
117 return Qnil;
118 }
119
120 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
121 "Call PROGRAM synchronously in separate process.\n\
122 The program's input comes from file INFILE (nil means `/dev/null').\n\
123 Insert output in BUFFER before point; t means current buffer;\n\
124 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
125 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
126 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
127 If BUFFER is 0, returns immediately with value nil.\n\
128 Otherwise waits for PROGRAM to terminate\n\
129 and returns a numeric exit status or a signal description string.\n\
130 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
131 (nargs, args)
132 int nargs;
133 register Lisp_Object *args;
134 {
135 Lisp_Object infile, buffer, current_dir, display, path;
136 int fd[2];
137 int filefd;
138 register int pid;
139 char buf[1024];
140 int count = specpdl_ptr - specpdl;
141 register unsigned char **new_argv
142 = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
143 struct buffer *old = current_buffer;
144 #if 0
145 int mask;
146 #endif
147 CHECK_STRING (args[0], 0);
148
149 if (nargs >= 2 && ! NILP (args[1]))
150 {
151 infile = Fexpand_file_name (args[1], current_buffer->directory);
152 CHECK_STRING (infile, 1);
153 }
154 else
155 infile = build_string (NULL_DEVICE);
156
157 if (nargs >= 3)
158 {
159 register Lisp_Object tem;
160
161 buffer = tem = args[2];
162 if (!(EQ (tem, Qnil)
163 || EQ (tem, Qt)
164 || XFASTINT (tem) == 0))
165 {
166 buffer = Fget_buffer (tem);
167 CHECK_BUFFER (buffer, 2);
168 }
169 }
170 else
171 buffer = Qnil;
172
173 /* Make sure that the child will be able to chdir to the current
174 buffer's current directory, or its unhandled equivalent. We
175 can't just have the child check for an error when it does the
176 chdir, since it's in a vfork.
177
178 We have to GCPRO around this because Fexpand_file_name,
179 Funhandled_file_name_directory, and Ffile_accessible_directory_p
180 might call a file name handling function. The argument list is
181 protected by the caller, so all we really have to worry about is
182 buffer. */
183 {
184 struct gcpro gcpro1, gcpro2, gcpro3;
185
186 current_dir = current_buffer->directory;
187
188 GCPRO3 (infile, buffer, current_dir);
189
190 current_dir =
191 expand_and_dir_to_file
192 (Funhandled_file_name_directory (current_dir), Qnil);
193 if (NILP (Ffile_accessible_directory_p (current_dir)))
194 report_file_error ("Setting current directory",
195 Fcons (current_buffer->directory, Qnil));
196
197 UNGCPRO;
198 }
199
200 display = nargs >= 4 ? args[3] : Qnil;
201
202 {
203 register int i;
204 for (i = 4; i < nargs; i++)
205 {
206 CHECK_STRING (args[i], i);
207 new_argv[i - 3] = XSTRING (args[i])->data;
208 }
209 /* Program name is first command arg */
210 new_argv[0] = XSTRING (args[0])->data;
211 new_argv[i - 3] = 0;
212 }
213
214 filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
215 if (filefd < 0)
216 {
217 report_file_error ("Opening process input file", Fcons (infile, Qnil));
218 }
219 /* Search for program; barf if not found. */
220 openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
221 if (NILP (path))
222 {
223 close (filefd);
224 report_file_error ("Searching for program", Fcons (args[0], Qnil));
225 }
226 new_argv[0] = XSTRING (path)->data;
227
228 if (XTYPE (buffer) == Lisp_Int)
229 fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
230 else
231 {
232 pipe (fd);
233 #if 0
234 /* Replaced by close_process_descs */
235 set_exclusive_use (fd[0]);
236 #endif
237 }
238
239 {
240 /* child_setup must clobber environ in systems with true vfork.
241 Protect it from permanent change. */
242 register char **save_environ = environ;
243 register int fd1 = fd[1];
244
245 #if 0 /* Some systems don't have sigblock. */
246 mask = sigblock (sigmask (SIGCHLD));
247 #endif
248
249 /* Record that we're about to create a synchronous process. */
250 synch_process_alive = 1;
251
252 /* These vars record information from process termination.
253 Clear them now before process can possibly terminate,
254 to avoid timing error if process terminates soon. */
255 synch_process_death = 0;
256 synch_process_retcode = 0;
257
258 pid = vfork ();
259
260 if (pid == 0)
261 {
262 if (fd[0] >= 0)
263 close (fd[0]);
264 #ifdef USG
265 setpgrp ();
266 #else
267 setpgrp (pid, pid);
268 #endif /* USG */
269 child_setup (filefd, fd1, fd1, new_argv, 0, current_dir);
270 }
271
272 #if 0
273 /* Tell SIGCHLD handler to look for this pid. */
274 synch_process_pid = pid;
275 /* Now let SIGCHLD come through. */
276 sigsetmask (mask);
277 #endif
278
279 environ = save_environ;
280
281 close (filefd);
282 close (fd1);
283 }
284
285 if (pid < 0)
286 {
287 close (fd[0]);
288 report_file_error ("Doing vfork", Qnil);
289 }
290
291 if (XTYPE (buffer) == Lisp_Int)
292 {
293 #ifndef subprocesses
294 /* If Emacs has been built with asynchronous subprocess support,
295 we don't need to do this, I think because it will then have
296 the facilities for handling SIGCHLD. */
297 wait_without_blocking ();
298 #endif /* subprocesses */
299 return Qnil;
300 }
301
302 record_unwind_protect (call_process_cleanup,
303 Fcons (make_number (fd[0]), make_number (pid)));
304
305
306 if (XTYPE (buffer) == Lisp_Buffer)
307 Fset_buffer (buffer);
308
309 immediate_quit = 1;
310 QUIT;
311
312 {
313 register int nread;
314
315 while ((nread = read (fd[0], buf, sizeof buf)) > 0)
316 {
317 immediate_quit = 0;
318 if (!NILP (buffer))
319 insert (buf, nread);
320 if (!NILP (display) && INTERACTIVE)
321 redisplay_preserve_echo_area ();
322 immediate_quit = 1;
323 QUIT;
324 }
325 }
326
327 /* Wait for it to terminate, unless it already has. */
328 wait_for_termination (pid);
329
330 immediate_quit = 0;
331
332 set_buffer_internal (old);
333
334 unbind_to (count, Qnil);
335
336 if (synch_process_death)
337 return build_string (synch_process_death);
338 return make_number (synch_process_retcode);
339 }
340 #endif
341 \f
342 static Lisp_Object
343 delete_temp_file (name)
344 Lisp_Object name;
345 {
346 unlink (XSTRING (name)->data);
347 }
348
349 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
350 3, MANY, 0,
351 "Send text from START to END to a synchronous process running PROGRAM.\n\
352 Delete the text if fourth arg DELETE is non-nil.\n\
353 Insert output in BUFFER before point; t means current buffer;\n\
354 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
355 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
356 Remaining args are passed to PROGRAM at startup as command args.\n\
357 If BUFFER is nil, returns immediately with value nil.\n\
358 Otherwise waits for PROGRAM to terminate\n\
359 and returns a numeric exit status or a signal description string.\n\
360 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
361 (nargs, args)
362 int nargs;
363 register Lisp_Object *args;
364 {
365 register Lisp_Object filename_string, start, end;
366 char tempfile[20];
367 int count = specpdl_ptr - specpdl;
368
369 #ifdef VMS
370 strcpy (tempfile, "tmp:emacsXXXXXX.");
371 #else
372 strcpy (tempfile, "/tmp/emacsXXXXXX");
373 #endif
374 mktemp (tempfile);
375
376 filename_string = build_string (tempfile);
377 start = args[0];
378 end = args[1];
379 Fwrite_region (start, end, filename_string, Qnil, Qlambda);
380 record_unwind_protect (delete_temp_file, filename_string);
381
382 if (!NILP (args[3]))
383 Fdelete_region (start, end);
384
385 args[3] = filename_string;
386
387 return unbind_to (count, Fcall_process (nargs - 2, args + 2));
388 }
389 \f
390 #ifndef VMS /* VMS version is in vmsproc.c. */
391
392 /* This is the last thing run in a newly forked inferior
393 either synchronous or asynchronous.
394 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
395 Initialize inferior's priority, pgrp, connected dir and environment.
396 then exec another program based on new_argv.
397
398 This function may change environ for the superior process.
399 Therefore, the superior process must save and restore the value
400 of environ around the vfork and the call to this function.
401
402 ENV is the environment for the subprocess.
403
404 SET_PGRP is nonzero if we should put the subprocess into a separate
405 process group.
406
407 CURRENT_DIR is an elisp string giving the path of the current
408 directory the subprocess should have. Since we can't really signal
409 a decent error from within the child, this should be verified as an
410 executable directory by the parent. */
411
412 child_setup (in, out, err, new_argv, set_pgrp, current_dir)
413 int in, out, err;
414 register char **new_argv;
415 int set_pgrp;
416 Lisp_Object current_dir;
417 {
418 char **env;
419
420 register int pid = getpid();
421
422 {
423 extern int emacs_priority;
424
425 nice (- emacs_priority);
426 }
427
428 #ifdef subprocesses
429 /* Close Emacs's descriptors that this process should not have. */
430 close_process_descs ();
431 #endif
432
433 /* Note that use of alloca is always safe here. It's obvious for systems
434 that do not have true vfork or that have true (stack) alloca.
435 If using vfork and C_ALLOCA it is safe because that changes
436 the superior's static variables as if the superior had done alloca
437 and will be cleaned up in the usual way. */
438 {
439 register unsigned char *temp;
440 register int i;
441
442 i = XSTRING (current_dir)->size;
443 temp = (unsigned char *) alloca (i + 2);
444 bcopy (XSTRING (current_dir)->data, temp, i);
445 if (temp[i - 1] != '/') temp[i++] = '/';
446 temp[i] = 0;
447
448 /* We can't signal an Elisp error here; we're in a vfork. Since
449 the callers check the current directory before forking, this
450 should only return an error if the directory's permissions
451 are changed between the check and this chdir, but we should
452 at least check. */
453 if (chdir (temp) < 0)
454 exit (errno);
455 }
456
457 /* Set `env' to a vector of the strings in Vprocess_environment. */
458 {
459 register Lisp_Object tem;
460 register char **new_env;
461 register int new_length;
462
463 new_length = 0;
464 for (tem = Vprocess_environment;
465 (XTYPE (tem) == Lisp_Cons
466 && XTYPE (XCONS (tem)->car) == Lisp_String);
467 tem = XCONS (tem)->cdr)
468 new_length++;
469
470 /* new_length + 1 to include terminating 0. */
471 env = new_env = (char **) alloca ((new_length + 1) * sizeof (char *));
472
473 /* Copy the Vprocess_environment strings into new_env. */
474 for (tem = Vprocess_environment;
475 (XTYPE (tem) == Lisp_Cons
476 && XTYPE (XCONS (tem)->car) == Lisp_String);
477 tem = XCONS (tem)->cdr)
478 {
479 char **ep = env;
480 char *string = (char *) XSTRING (XCONS (tem)->car)->data;
481 /* See if this string duplicates any string already in the env.
482 If so, don't put it in.
483 When an env var has multiple definitions,
484 we keep the definition that comes first in process-environment. */
485 for (; ep != new_env; ep++)
486 {
487 char *p = *ep, *q = string;
488 while (1)
489 {
490 if (*q == 0)
491 /* The string is malformed; might as well drop it. */
492 goto duplicate;
493 if (*q != *p)
494 break;
495 if (*q == '=')
496 goto duplicate;
497 p++, q++;
498 }
499 }
500 *new_env++ = string;
501 duplicate: ;
502 }
503 *new_env = 0;
504 }
505
506 /* Make sure that in, out, and err are not actually already in
507 descriptors zero, one, or two; this could happen if Emacs is
508 started with its standard in, our, or error closed, as might
509 happen under X. */
510 in = relocate_fd (in, 3);
511 out = relocate_fd (out, 3);
512 err = relocate_fd (err, 3);
513
514 close (0);
515 close (1);
516 close (2);
517
518 dup2 (in, 0);
519 dup2 (out, 1);
520 dup2 (err, 2);
521 close (in);
522 close (out);
523 close (err);
524
525 #if !defined (IRIX)
526 #if defined (USG)
527 setpgrp (); /* No arguments but equivalent in this case */
528 #else
529 setpgrp (pid, pid);
530 #endif /* USG */
531 #endif /* IRIX */
532 setpgrp_of_tty (pid);
533
534 #ifdef vipc
535 something missing here;
536 #endif /* vipc */
537
538 /* execvp does not accept an environment arg so the only way
539 to pass this environment is to set environ. Our caller
540 is responsible for restoring the ambient value of environ. */
541 environ = env;
542 execvp (new_argv[0], new_argv);
543
544 write (1, "Couldn't exec the program ", 26);
545 write (1, new_argv[0], strlen (new_argv[0]));
546 _exit (1);
547 }
548
549 /* Move the file descriptor FD so that its number is not less than MIN.
550 If the file descriptor is moved at all, the original is freed. */
551 int
552 relocate_fd (fd, min)
553 int fd, min;
554 {
555 if (fd >= min)
556 return fd;
557 else
558 {
559 int new = dup (fd);
560 if (new == -1)
561 {
562 char *message1 = "Error while setting up child: ";
563 char *message2 = "\n";
564 write (2, message1, strlen (message1));
565 write (2, sys_errlist[errno], strlen (sys_errlist[errno]));
566 write (2, message2, strlen (message2));
567 _exit (1);
568 }
569 /* Note that we hold the original FD open while we recurse,
570 to guarantee we'll get a new FD if we need it. */
571 new = relocate_fd (new, min);
572 close (fd);
573 return new;
574 }
575 }
576
577 static int
578 getenv_internal (var, varlen, value, valuelen)
579 char *var;
580 int varlen;
581 char **value;
582 int *valuelen;
583 {
584 Lisp_Object scan;
585
586 for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
587 {
588 Lisp_Object entry = XCONS (scan)->car;
589
590 if (XTYPE (entry) == Lisp_String
591 && XSTRING (entry)->size > varlen
592 && XSTRING (entry)->data[varlen] == '='
593 && ! bcmp (XSTRING (entry)->data, var, varlen))
594 {
595 *value = (char *) XSTRING (entry)->data + (varlen + 1);
596 *valuelen = XSTRING (entry)->size - (varlen + 1);
597 return 1;
598 }
599 }
600
601 return 0;
602 }
603
604 DEFUN ("getenv", Fgetenv, Sgetenv, 1, 2, 0,
605 "Return the value of environment variable VAR, as a string.\n\
606 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
607 This function consults the variable ``process-environment'' for its value.")
608 (var)
609 Lisp_Object var;
610 {
611 char *value;
612 int valuelen;
613
614 CHECK_STRING (var, 0);
615 if (getenv_internal (XSTRING (var)->data, XSTRING (var)->size,
616 &value, &valuelen))
617 return make_string (value, valuelen);
618 else
619 return Qnil;
620 }
621
622 /* A version of getenv that consults process_environment, easily
623 callable from C. */
624 char *
625 egetenv (var)
626 char *var;
627 {
628 char *value;
629 int valuelen;
630
631 if (getenv_internal (var, strlen (var), &value, &valuelen))
632 return value;
633 else
634 return 0;
635 }
636
637 #endif /* not VMS */
638 \f
639 init_callproc ()
640 {
641 register char * sh;
642 Lisp_Object tempdir;
643
644 {
645 char *data_dir = egetenv ("EMACSDATA");
646
647 Vdata_directory =
648 Ffile_name_as_directory
649 (build_string (data_dir ? data_dir : PATH_DATA));
650 }
651
652 /* Check the EMACSPATH environment variable, defaulting to the
653 PATH_EXEC path from paths.h. */
654 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
655 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
656 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
657
658 tempdir = Fdirectory_file_name (Vexec_directory);
659 if (access (XSTRING (tempdir)->data, 0) < 0)
660 {
661 printf ("Warning: arch-dependent data dir (%s) does not exist.\n",
662 XSTRING (Vexec_directory)->data);
663 sleep (2);
664 }
665
666 tempdir = Fdirectory_file_name (Vdata_directory);
667 if (access (XSTRING (tempdir)->data, 0) < 0)
668 {
669 printf ("Warning: arch-independent data dir (%s) does not exist.\n",
670 XSTRING (Vdata_directory)->data);
671 sleep (2);
672 }
673
674 #ifdef VMS
675 Vshell_file_name = build_string ("*dcl*");
676 #else
677 sh = (char *) getenv ("SHELL");
678 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
679 #endif
680 }
681
682 set_process_environment ()
683 {
684 register char **envp;
685
686 Vprocess_environment = Qnil;
687 #ifndef CANNOT_DUMP
688 if (initialized)
689 #endif
690 for (envp = environ; *envp; envp++)
691 Vprocess_environment = Fcons (build_string (*envp),
692 Vprocess_environment);
693 }
694
695 syms_of_callproc ()
696 {
697 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
698 "*File name to load inferior shells from.\n\
699 Initialized from the SHELL environment variable.");
700
701 DEFVAR_LISP ("exec-path", &Vexec_path,
702 "*List of directories to search programs to run in subprocesses.\n\
703 Each element is a string (directory name) or nil (try default directory).");
704
705 DEFVAR_LISP ("exec-directory", &Vexec_directory,
706 "Directory of architecture-dependent files that come with GNU Emacs,\n\
707 especially executable programs intended for Emacs to invoke.");
708
709 DEFVAR_LISP ("data-directory", &Vdata_directory,
710 "Directory of architecture-independent files that come with GNU Emacs,\n\
711 intended for Emacs to use.");
712
713 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
714 "For internal use by the build procedure only.\n\
715 This is the name of the directory in which the build procedure installed\n\
716 Emacs's info files; the default value for Info-default-directory-list\n\
717 includes this.");
718 Vconfigure_info_directory = build_string (PATH_INFO);
719
720 DEFVAR_LISP ("process-environment", &Vprocess_environment,
721 "List of environment variables for subprocesses to inherit.\n\
722 Each element should be a string of the form ENVVARNAME=VALUE.\n\
723 The environment which Emacs inherits is placed in this variable\n\
724 when Emacs starts.");
725
726 #ifndef VMS
727 defsubr (&Scall_process);
728 defsubr (&Sgetenv);
729 #endif
730 defsubr (&Scall_process_region);
731 }