annotate src/callproc.c @ 405:698d5d6e8f8b

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