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