Commit | Line | Data |
---|---|---|
80856e74 | 1 | /* Synchronous subprocess invocation for GNU Emacs. |
fe12847a | 2 | Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001, |
0b5538bd | 3 | 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
80856e74 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
826c56ac | 9 | the Free Software Foundation; either version 2, or (at your option) |
80856e74 JB |
10 | any later version. |
11 | ||
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
4fc5845f LK |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
80856e74 JB |
21 | |
22 | ||
68c45bf0 | 23 | #include <config.h> |
80856e74 | 24 | #include <signal.h> |
e576cab4 | 25 | #include <errno.h> |
565620a5 | 26 | #include <stdio.h> |
80856e74 | 27 | |
03695ace | 28 | #ifndef USE_CRT_DLL |
426b37ae | 29 | extern int errno; |
03695ace | 30 | #endif |
426b37ae | 31 | |
80856e74 JB |
32 | /* Define SIGCHLD as an alias for SIGCLD. */ |
33 | ||
34 | #if !defined (SIGCHLD) && defined (SIGCLD) | |
35 | #define SIGCHLD SIGCLD | |
36 | #endif /* SIGCLD */ | |
37 | ||
38 | #include <sys/types.h> | |
88a64fef | 39 | |
3cbd6585 GM |
40 | #ifdef HAVE_UNISTD_H |
41 | #include <unistd.h> | |
42 | #endif | |
43 | ||
80856e74 | 44 | #include <sys/file.h> |
776a24a1 | 45 | #ifdef HAVE_FCNTL_H |
472e83fe | 46 | #define INCLUDED_FCNTL |
80856e74 JB |
47 | #include <fcntl.h> |
48 | #endif | |
49 | ||
bad95d8f RS |
50 | #ifdef WINDOWSNT |
51 | #define NOMINMAX | |
52 | #include <windows.h> | |
53 | #include <stdlib.h> /* for proper declaration of environ */ | |
54 | #include <fcntl.h> | |
489f9371 | 55 | #include "w32.h" |
bad95d8f RS |
56 | #define _P_NOWAIT 1 /* from process.h */ |
57 | #endif | |
58 | ||
7e6c2178 | 59 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
472e83fe | 60 | #define INCLUDED_FCNTL |
7e6c2178 RS |
61 | #include <fcntl.h> |
62 | #include <sys/stat.h> | |
63 | #include <sys/param.h> | |
64 | #include <errno.h> | |
65 | #endif /* MSDOS */ | |
66 | ||
80856e74 JB |
67 | #ifndef O_RDONLY |
68 | #define O_RDONLY 0 | |
69 | #endif | |
70 | ||
71 | #ifndef O_WRONLY | |
72 | #define O_WRONLY 1 | |
73 | #endif | |
74 | ||
75 | #include "lisp.h" | |
76 | #include "commands.h" | |
77 | #include "buffer.h" | |
32d08644 | 78 | #include "charset.h" |
edf496dd | 79 | #include "ccl.h" |
32d08644 | 80 | #include "coding.h" |
f0b950cf | 81 | #include "composite.h" |
57bda87a | 82 | #include <epaths.h> |
80856e74 | 83 | #include "process.h" |
d177f194 | 84 | #include "syssignal.h" |
a129418f | 85 | #include "systty.h" |
aba637ec | 86 | #include "blockinput.h" |
f105f403 KL |
87 | #include "frame.h" |
88 | #include "termhooks.h" | |
80856e74 | 89 | |
5f027cea EZ |
90 | #ifdef MSDOS |
91 | #include "msdos.h" | |
92 | #endif | |
93 | ||
80856e74 JB |
94 | #ifdef VMS |
95 | extern noshare char **environ; | |
96 | #else | |
03695ace | 97 | #ifndef USE_CRT_DLL |
80856e74 JB |
98 | extern char **environ; |
99 | #endif | |
03695ace | 100 | #endif |
80856e74 | 101 | |
f95c3f91 | 102 | #ifdef HAVE_SETPGID |
2b7e8799 | 103 | #if !defined (USG) || defined (BSD_PGRPS) |
320695d8 | 104 | #undef setpgrp |
f95c3f91 GM |
105 | #define setpgrp setpgid |
106 | #endif | |
2b7e8799 | 107 | #endif |
f95c3f91 | 108 | |
b81a1b72 SM |
109 | Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes; |
110 | Lisp_Object Vdata_directory, Vdoc_directory; | |
1e7ce61b | 111 | Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory; |
8abd035b | 112 | Lisp_Object Vtemp_file_name_pattern; |
80856e74 JB |
113 | |
114 | Lisp_Object Vshell_file_name; | |
115 | ||
5990851d | 116 | Lisp_Object Vglobal_environment; |
80856e74 | 117 | Lisp_Object Vprocess_environment; |
80856e74 | 118 | |
bad95d8f | 119 | #ifdef DOS_NT |
093650fe | 120 | Lisp_Object Qbuffer_file_type; |
bad95d8f | 121 | #endif /* DOS_NT */ |
093650fe | 122 | |
80856e74 JB |
123 | /* True iff we are about to fork off a synchronous process or if we |
124 | are waiting for it. */ | |
125 | int synch_process_alive; | |
126 | ||
127 | /* Nonzero => this is a string explaining death of synchronous subprocess. */ | |
128 | char *synch_process_death; | |
129 | ||
ca498128 JD |
130 | /* Nonzero => this is the signal number that terminated the subprocess. */ |
131 | int synch_process_termsig; | |
132 | ||
80856e74 JB |
133 | /* If synch_process_death is zero, |
134 | this is exit code of synchronous subprocess. */ | |
135 | int synch_process_retcode; | |
f105f403 KL |
136 | |
137 | /* List of environment variables to look up in emacsclient. */ | |
138 | Lisp_Object Vlocal_environment_variables; | |
139 | ||
80856e74 | 140 | \f |
37d54121 RS |
141 | /* Clean up when exiting Fcall_process. |
142 | On MSDOS, delete the temporary file on any kind of termination. | |
143 | On Unix, kill the process and any children on termination by signal. */ | |
144 | ||
145 | /* Nonzero if this is termination due to exit. */ | |
146 | static int call_process_exited; | |
147 | ||
80856e74 JB |
148 | #ifndef VMS /* VMS version is in vmsproc.c. */ |
149 | ||
d177f194 JB |
150 | static Lisp_Object |
151 | call_process_kill (fdpid) | |
152 | Lisp_Object fdpid; | |
153 | { | |
68c45bf0 | 154 | emacs_close (XFASTINT (Fcar (fdpid))); |
d177f194 JB |
155 | EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL); |
156 | synch_process_alive = 0; | |
157 | return Qnil; | |
158 | } | |
159 | ||
80856e74 JB |
160 | Lisp_Object |
161 | call_process_cleanup (fdpid) | |
162 | Lisp_Object fdpid; | |
163 | { | |
e0f712ba | 164 | #if defined (MSDOS) || defined (MAC_OS8) |
7e6c2178 | 165 | /* for MSDOS fdpid is really (fd . tempfile) */ |
c1350752 KH |
166 | register Lisp_Object file; |
167 | file = Fcdr (fdpid); | |
68c45bf0 | 168 | emacs_close (XFASTINT (Fcar (fdpid))); |
d5db4077 KR |
169 | if (strcmp (SDATA (file), NULL_DEVICE) != 0) |
170 | unlink (SDATA (file)); | |
e0f712ba | 171 | #else /* not MSDOS and not MAC_OS8 */ |
d177f194 JB |
172 | register int pid = XFASTINT (Fcdr (fdpid)); |
173 | ||
37d54121 | 174 | if (call_process_exited) |
6b6e798b | 175 | { |
68c45bf0 | 176 | emacs_close (XFASTINT (Fcar (fdpid))); |
6b6e798b RS |
177 | return Qnil; |
178 | } | |
37d54121 | 179 | |
d177f194 JB |
180 | if (EMACS_KILLPG (pid, SIGINT) == 0) |
181 | { | |
aed13378 | 182 | int count = SPECPDL_INDEX (); |
d177f194 JB |
183 | record_unwind_protect (call_process_kill, fdpid); |
184 | message1 ("Waiting for process to die...(type C-g again to kill it instantly)"); | |
185 | immediate_quit = 1; | |
186 | QUIT; | |
187 | wait_for_termination (pid); | |
188 | immediate_quit = 0; | |
189 | specpdl_ptr = specpdl + count; /* Discard the unwind protect. */ | |
190 | message1 ("Waiting for process to die...done"); | |
191 | } | |
80856e74 | 192 | synch_process_alive = 0; |
68c45bf0 | 193 | emacs_close (XFASTINT (Fcar (fdpid))); |
7e6c2178 | 194 | #endif /* not MSDOS */ |
80856e74 JB |
195 | return Qnil; |
196 | } | |
197 | ||
198 | DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0, | |
fdb82f93 PJ |
199 | doc: /* Call PROGRAM synchronously in separate process. |
200 | The remaining arguments are optional. | |
201 | The program's input comes from file INFILE (nil means `/dev/null'). | |
202 | Insert output in BUFFER before point; t means current buffer; | |
203 | nil for BUFFER means discard it; 0 means discard and don't wait. | |
204 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
205 | REAL-BUFFER says what to do with standard output, as above, | |
206 | while STDERR-FILE says what to do with standard error in the child. | |
207 | STDERR-FILE may be nil (discard standard error output), | |
208 | t (mix it with ordinary output), or a file name string. | |
209 | ||
210 | Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted. | |
211 | Remaining arguments are strings passed as command arguments to PROGRAM. | |
212 | ||
213 | If BUFFER is 0, `call-process' returns immediately with value nil. | |
214 | Otherwise it waits for PROGRAM to terminate | |
215 | and returns a numeric exit status or a signal description string. | |
d98b59b5 MB |
216 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again. |
217 | ||
218 | usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */) | |
fdb82f93 | 219 | (nargs, args) |
80856e74 JB |
220 | int nargs; |
221 | register Lisp_Object *args; | |
222 | { | |
0aa2630f KS |
223 | Lisp_Object infile, buffer, current_dir, path; |
224 | int display_p; | |
80856e74 JB |
225 | int fd[2]; |
226 | int filefd; | |
227 | register int pid; | |
4da256b1 KS |
228 | #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024) |
229 | #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN) | |
230 | char buf[CALLPROC_BUFFER_SIZE_MAX]; | |
231 | int bufsize = CALLPROC_BUFFER_SIZE_MIN; | |
aed13378 | 232 | int count = SPECPDL_INDEX (); |
2d607244 | 233 | |
4d3b07fd KR |
234 | register const unsigned char **new_argv |
235 | = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); | |
80856e74 | 236 | struct buffer *old = current_buffer; |
39eaa782 RS |
237 | /* File to use for stderr in the child. |
238 | t means use same as standard output. */ | |
239 | Lisp_Object error_file; | |
7e6c2178 RS |
240 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
241 | char *outf, *tempfile; | |
242 | int outfilefd; | |
243 | #endif | |
e0f712ba | 244 | #ifdef MAC_OS8 |
052062e0 RS |
245 | char *tempfile; |
246 | int outfilefd; | |
247 | #endif | |
80856e74 JB |
248 | #if 0 |
249 | int mask; | |
250 | #endif | |
32d08644 KH |
251 | struct coding_system process_coding; /* coding-system of process output */ |
252 | struct coding_system argument_coding; /* coding-system of arguments */ | |
09494912 RS |
253 | /* Set to the return value of Ffind_operation_coding_system. */ |
254 | Lisp_Object coding_systems; | |
255 | ||
256 | /* Qt denotes that Ffind_operation_coding_system is not yet called. */ | |
257 | coding_systems = Qt; | |
32d08644 | 258 | |
b7826503 | 259 | CHECK_STRING (args[0]); |
80856e74 | 260 | |
39eaa782 RS |
261 | error_file = Qt; |
262 | ||
7e6c2178 RS |
263 | #ifndef subprocesses |
264 | /* Without asynchronous processes we cannot have BUFFER == 0. */ | |
177c0ea7 | 265 | if (nargs >= 3 |
09ffb8b5 | 266 | && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2]))) |
7e6c2178 RS |
267 | error ("Operating system cannot handle asynchronous subprocesses"); |
268 | #endif /* subprocesses */ | |
269 | ||
09494912 | 270 | /* Decide the coding-system for giving arguments. */ |
32d08644 KH |
271 | { |
272 | Lisp_Object val, *args2; | |
32d08644 KH |
273 | int i; |
274 | ||
275 | /* If arguments are supplied, we may have to encode them. */ | |
276 | if (nargs >= 5) | |
277 | { | |
30d57b8e RS |
278 | int must_encode = 0; |
279 | ||
e7c1c20e | 280 | for (i = 4; i < nargs; i++) |
b7826503 | 281 | CHECK_STRING (args[i]); |
e7c1c20e | 282 | |
a2286b5c | 283 | for (i = 4; i < nargs; i++) |
30d57b8e RS |
284 | if (STRING_MULTIBYTE (args[i])) |
285 | must_encode = 1; | |
286 | ||
beacaab3 KH |
287 | if (!NILP (Vcoding_system_for_write)) |
288 | val = Vcoding_system_for_write; | |
30d57b8e | 289 | else if (! must_encode) |
beacaab3 KH |
290 | val = Qnil; |
291 | else | |
32d08644 KH |
292 | { |
293 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); | |
294 | args2[0] = Qcall_process; | |
295 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
08ee4e87 | 296 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); |
776b95cb | 297 | if (CONSP (coding_systems)) |
70949dac | 298 | val = XCDR (coding_systems); |
776b95cb | 299 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 300 | val = XCDR (Vdefault_process_coding_system); |
beacaab3 KH |
301 | else |
302 | val = Qnil; | |
32d08644 KH |
303 | } |
304 | setup_coding_system (Fcheck_coding_system (val), &argument_coding); | |
305 | } | |
32d08644 KH |
306 | } |
307 | ||
e576cab4 JB |
308 | if (nargs >= 2 && ! NILP (args[1])) |
309 | { | |
310 | infile = Fexpand_file_name (args[1], current_buffer->directory); | |
b7826503 | 311 | CHECK_STRING (infile); |
e576cab4 | 312 | } |
80856e74 | 313 | else |
5437e9f9 | 314 | infile = build_string (NULL_DEVICE); |
80856e74 | 315 | |
e576cab4 JB |
316 | if (nargs >= 3) |
317 | { | |
39eaa782 RS |
318 | buffer = args[2]; |
319 | ||
320 | /* If BUFFER is a list, its meaning is | |
321 | (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */ | |
322 | if (CONSP (buffer)) | |
323 | { | |
70949dac | 324 | if (CONSP (XCDR (buffer))) |
45be8a1e | 325 | { |
a9d4f28a | 326 | Lisp_Object stderr_file; |
70949dac | 327 | stderr_file = XCAR (XCDR (buffer)); |
45be8a1e RS |
328 | |
329 | if (NILP (stderr_file) || EQ (Qt, stderr_file)) | |
330 | error_file = stderr_file; | |
331 | else | |
332 | error_file = Fexpand_file_name (stderr_file, Qnil); | |
333 | } | |
334 | ||
70949dac | 335 | buffer = XCAR (buffer); |
39eaa782 | 336 | } |
044512ed | 337 | |
39eaa782 RS |
338 | if (!(EQ (buffer, Qnil) |
339 | || EQ (buffer, Qt) | |
3ffde7d6 | 340 | || INTEGERP (buffer))) |
e576cab4 | 341 | { |
39eaa782 RS |
342 | Lisp_Object spec_buffer; |
343 | spec_buffer = buffer; | |
50fe359b | 344 | buffer = Fget_buffer_create (buffer); |
39eaa782 RS |
345 | /* Mention the buffer name for a better error message. */ |
346 | if (NILP (buffer)) | |
b7826503 PJ |
347 | CHECK_BUFFER (spec_buffer); |
348 | CHECK_BUFFER (buffer); | |
e576cab4 JB |
349 | } |
350 | } | |
177c0ea7 | 351 | else |
e576cab4 | 352 | buffer = Qnil; |
80856e74 | 353 | |
58616e67 JB |
354 | /* Make sure that the child will be able to chdir to the current |
355 | buffer's current directory, or its unhandled equivalent. We | |
356 | can't just have the child check for an error when it does the | |
357 | chdir, since it's in a vfork. | |
358 | ||
359 | We have to GCPRO around this because Fexpand_file_name, | |
360 | Funhandled_file_name_directory, and Ffile_accessible_directory_p | |
361 | might call a file name handling function. The argument list is | |
362 | protected by the caller, so all we really have to worry about is | |
363 | buffer. */ | |
364 | { | |
365 | struct gcpro gcpro1, gcpro2, gcpro3; | |
366 | ||
367 | current_dir = current_buffer->directory; | |
368 | ||
369 | GCPRO3 (infile, buffer, current_dir); | |
370 | ||
c52b0b34 KH |
371 | current_dir |
372 | = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir), | |
373 | Qnil); | |
58616e67 JB |
374 | if (NILP (Ffile_accessible_directory_p (current_dir))) |
375 | report_file_error ("Setting current directory", | |
376 | Fcons (current_buffer->directory, Qnil)); | |
377 | ||
378 | UNGCPRO; | |
379 | } | |
380 | ||
0aa2630f | 381 | display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]); |
80856e74 | 382 | |
d5db4077 | 383 | filefd = emacs_open (SDATA (infile), O_RDONLY, 0); |
80856e74 JB |
384 | if (filefd < 0) |
385 | { | |
e576cab4 | 386 | report_file_error ("Opening process input file", Fcons (infile, Qnil)); |
80856e74 JB |
387 | } |
388 | /* Search for program; barf if not found. */ | |
c52b0b34 KH |
389 | { |
390 | struct gcpro gcpro1; | |
391 | ||
392 | GCPRO1 (current_dir); | |
5c150961 | 393 | openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK)); |
c52b0b34 KH |
394 | UNGCPRO; |
395 | } | |
012c6fcb | 396 | if (NILP (path)) |
80856e74 | 397 | { |
68c45bf0 | 398 | emacs_close (filefd); |
80856e74 JB |
399 | report_file_error ("Searching for program", Fcons (args[0], Qnil)); |
400 | } | |
8ee8f447 RS |
401 | |
402 | /* If program file name starts with /: for quoting a magic name, | |
403 | discard that. */ | |
404 | if (SBYTES (path) > 2 && SREF (path, 0) == '/' | |
405 | && SREF (path, 1) == ':') | |
406 | path = Fsubstring (path, make_number (2), Qnil); | |
407 | ||
d5db4077 | 408 | new_argv[0] = SDATA (path); |
c364e618 KH |
409 | if (nargs > 4) |
410 | { | |
411 | register int i; | |
c5bfa12b | 412 | struct gcpro gcpro1, gcpro2, gcpro3; |
c364e618 | 413 | |
c5bfa12b KH |
414 | GCPRO3 (infile, buffer, current_dir); |
415 | argument_coding.dst_multibyte = 0; | |
416 | for (i = 4; i < nargs; i++) | |
c364e618 | 417 | { |
c5bfa12b KH |
418 | argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]); |
419 | if (CODING_REQUIRE_ENCODING (&argument_coding)) | |
c364e618 | 420 | { |
c5bfa12b KH |
421 | /* We must encode this argument. */ |
422 | args[i] = encode_coding_string (args[i], &argument_coding, 1); | |
423 | if (argument_coding.type == coding_type_ccl) | |
424 | setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil); | |
c364e618 | 425 | } |
d5db4077 | 426 | new_argv[i - 3] = SDATA (args[i]); |
c364e618 | 427 | } |
c5bfa12b | 428 | UNGCPRO; |
db54baaa | 429 | new_argv[nargs - 3] = 0; |
c364e618 | 430 | } |
db54baaa KH |
431 | else |
432 | new_argv[1] = 0; | |
80856e74 | 433 | |
7e6c2178 | 434 | #ifdef MSDOS /* MW, July 1993 */ |
8a52365c | 435 | if ((outf = egetenv ("TMPDIR"))) |
7e6c2178 RS |
436 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); |
437 | else | |
438 | { | |
439 | tempfile = alloca (20); | |
440 | *tempfile = '\0'; | |
441 | } | |
442 | dostounix_filename (tempfile); | |
177c0ea7 | 443 | if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/') |
7e6c2178 RS |
444 | strcat (tempfile, "/"); |
445 | strcat (tempfile, "detmp.XXX"); | |
446 | mktemp (tempfile); | |
447 | ||
448 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); | |
449 | if (outfilefd < 0) | |
450 | { | |
68c45bf0 | 451 | emacs_close (filefd); |
6f89d28a MB |
452 | report_file_error ("Opening process output file", |
453 | Fcons (build_string (tempfile), Qnil)); | |
7e6c2178 | 454 | } |
6f89d28a | 455 | fd[0] = filefd; |
2610078a | 456 | fd[1] = outfilefd; |
6f89d28a | 457 | #endif /* MSDOS */ |
7e6c2178 | 458 | |
e0f712ba | 459 | #ifdef MAC_OS8 |
052062e0 RS |
460 | /* Since we don't have pipes on the Mac, create a temporary file to |
461 | hold the output of the subprocess. */ | |
d5db4077 KR |
462 | tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1); |
463 | bcopy (SDATA (Vtemp_file_name_pattern), tempfile, | |
464 | SBYTES (Vtemp_file_name_pattern) + 1); | |
052062e0 RS |
465 | |
466 | mktemp (tempfile); | |
467 | ||
468 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); | |
469 | if (outfilefd < 0) | |
470 | { | |
471 | close (filefd); | |
472 | report_file_error ("Opening process output file", | |
473 | Fcons (build_string (tempfile), Qnil)); | |
474 | } | |
475 | fd[0] = filefd; | |
476 | fd[1] = outfilefd; | |
e0f712ba | 477 | #endif /* MAC_OS8 */ |
052062e0 | 478 | |
d50d3dc8 | 479 | if (INTEGERP (buffer)) |
68c45bf0 | 480 | fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1; |
80856e74 JB |
481 | else |
482 | { | |
7e6c2178 | 483 | #ifndef MSDOS |
e0f712ba | 484 | #ifndef MAC_OS8 |
db92b288 GM |
485 | errno = 0; |
486 | if (pipe (fd) == -1) | |
487 | { | |
488 | emacs_close (filefd); | |
489 | report_file_error ("Creating process pipe", Qnil); | |
490 | } | |
7e6c2178 | 491 | #endif |
052062e0 | 492 | #endif |
80856e74 JB |
493 | #if 0 |
494 | /* Replaced by close_process_descs */ | |
495 | set_exclusive_use (fd[0]); | |
496 | #endif | |
497 | } | |
498 | ||
499 | { | |
500 | /* child_setup must clobber environ in systems with true vfork. | |
501 | Protect it from permanent change. */ | |
502 | register char **save_environ = environ; | |
503 | register int fd1 = fd[1]; | |
39eaa782 | 504 | int fd_error = fd1; |
80856e74 JB |
505 | |
506 | #if 0 /* Some systems don't have sigblock. */ | |
e065a56e | 507 | mask = sigblock (sigmask (SIGCHLD)); |
80856e74 JB |
508 | #endif |
509 | ||
510 | /* Record that we're about to create a synchronous process. */ | |
511 | synch_process_alive = 1; | |
512 | ||
5c03767e RS |
513 | /* These vars record information from process termination. |
514 | Clear them now before process can possibly terminate, | |
515 | to avoid timing error if process terminates soon. */ | |
516 | synch_process_death = 0; | |
517 | synch_process_retcode = 0; | |
ca498128 | 518 | synch_process_termsig = 0; |
5c03767e | 519 | |
39eaa782 | 520 | if (NILP (error_file)) |
68c45bf0 | 521 | fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0); |
39eaa782 RS |
522 | else if (STRINGP (error_file)) |
523 | { | |
524 | #ifdef DOS_NT | |
d5db4077 | 525 | fd_error = emacs_open (SDATA (error_file), |
68c45bf0 PE |
526 | O_WRONLY | O_TRUNC | O_CREAT | O_TEXT, |
527 | S_IREAD | S_IWRITE); | |
39eaa782 | 528 | #else /* not DOS_NT */ |
d5db4077 | 529 | fd_error = creat (SDATA (error_file), 0666); |
39eaa782 RS |
530 | #endif /* not DOS_NT */ |
531 | } | |
532 | ||
533 | if (fd_error < 0) | |
534 | { | |
68c45bf0 | 535 | emacs_close (filefd); |
6f89d28a | 536 | if (fd[0] != filefd) |
68c45bf0 | 537 | emacs_close (fd[0]); |
39eaa782 | 538 | if (fd1 >= 0) |
68c45bf0 | 539 | emacs_close (fd1); |
6f89d28a MB |
540 | #ifdef MSDOS |
541 | unlink (tempfile); | |
542 | #endif | |
543 | report_file_error ("Cannot redirect stderr", | |
544 | Fcons ((NILP (error_file) | |
545 | ? build_string (NULL_DEVICE) : error_file), | |
546 | Qnil)); | |
39eaa782 | 547 | } |
89e1ec1d | 548 | |
8d024345 | 549 | current_dir = ENCODE_FILE (current_dir); |
89e1ec1d | 550 | |
e0f712ba | 551 | #ifdef MAC_OS8 |
052062e0 RS |
552 | { |
553 | /* Call run_mac_command in sysdep.c here directly instead of doing | |
554 | a child_setup as for MSDOS and other platforms. Note that this | |
555 | code does not handle passing the environment to the synchronous | |
556 | Mac subprocess. */ | |
557 | char *infn, *outfn, *errfn, *currdn; | |
177c0ea7 | 558 | |
052062e0 RS |
559 | /* close these files so subprocess can write to them */ |
560 | close (outfilefd); | |
561 | if (fd_error != outfilefd) | |
562 | close (fd_error); | |
563 | fd1 = -1; /* No harm in closing that one! */ | |
564 | ||
d5db4077 | 565 | infn = SDATA (infile); |
052062e0 RS |
566 | outfn = tempfile; |
567 | if (NILP (error_file)) | |
568 | errfn = NULL_DEVICE; | |
569 | else if (EQ (Qt, error_file)) | |
570 | errfn = outfn; | |
571 | else | |
d5db4077 KR |
572 | errfn = SDATA (error_file); |
573 | currdn = SDATA (current_dir); | |
052062e0 RS |
574 | pid = run_mac_command (new_argv, currdn, infn, outfn, errfn); |
575 | ||
576 | /* Record that the synchronous process exited and note its | |
577 | termination status. */ | |
578 | synch_process_alive = 0; | |
579 | synch_process_retcode = pid; | |
580 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ | |
68c45bf0 | 581 | { |
ca9c0567 | 582 | synchronize_system_messages_locale (); |
68c45bf0 PE |
583 | synch_process_death = strerror (errno); |
584 | } | |
052062e0 RS |
585 | |
586 | /* Since CRLF is converted to LF within `decode_coding', we can | |
587 | always open a file with binary mode. */ | |
588 | fd[0] = open (tempfile, O_BINARY); | |
589 | if (fd[0] < 0) | |
590 | { | |
591 | unlink (tempfile); | |
592 | close (filefd); | |
593 | report_file_error ("Cannot re-open temporary file", Qnil); | |
594 | } | |
595 | } | |
e0f712ba | 596 | #else /* not MAC_OS8 */ |
2610078a | 597 | #ifdef MSDOS /* MW, July 1993 */ |
c17c4250 | 598 | /* Note that on MSDOS `child_setup' actually returns the child process |
2610078a KH |
599 | exit status, not its PID, so we assign it to `synch_process_retcode' |
600 | below. */ | |
c17c4250 EZ |
601 | pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv, |
602 | 0, current_dir); | |
39eaa782 | 603 | |
2610078a KH |
604 | /* Record that the synchronous process exited and note its |
605 | termination status. */ | |
606 | synch_process_alive = 0; | |
607 | synch_process_retcode = pid; | |
608 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ | |
68c45bf0 | 609 | { |
ca9c0567 | 610 | synchronize_system_messages_locale (); |
68c45bf0 PE |
611 | synch_process_death = strerror (errno); |
612 | } | |
2610078a | 613 | |
68c45bf0 | 614 | emacs_close (outfilefd); |
2610078a | 615 | if (fd_error != outfilefd) |
68c45bf0 | 616 | emacs_close (fd_error); |
2610078a | 617 | fd1 = -1; /* No harm in closing that one! */ |
32d08644 KH |
618 | /* Since CRLF is converted to LF within `decode_coding', we can |
619 | always open a file with binary mode. */ | |
68c45bf0 | 620 | fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0); |
2610078a KH |
621 | if (fd[0] < 0) |
622 | { | |
623 | unlink (tempfile); | |
68c45bf0 | 624 | emacs_close (filefd); |
2610078a KH |
625 | report_file_error ("Cannot re-open temporary file", Qnil); |
626 | } | |
627 | #else /* not MSDOS */ | |
bad95d8f | 628 | #ifdef WINDOWSNT |
2d607244 RS |
629 | pid = child_setup (filefd, fd1, fd_error, (char **) new_argv, |
630 | 0, current_dir); | |
bad95d8f | 631 | #else /* not WINDOWSNT */ |
aba637ec KS |
632 | BLOCK_INPUT; |
633 | ||
80856e74 JB |
634 | pid = vfork (); |
635 | ||
636 | if (pid == 0) | |
637 | { | |
638 | if (fd[0] >= 0) | |
68c45bf0 | 639 | emacs_close (fd[0]); |
1e7963c7 RS |
640 | #ifdef HAVE_SETSID |
641 | setsid (); | |
642 | #endif | |
643 | #if defined (USG) && !defined (BSD_PGRPS) | |
80856e74 JB |
644 | setpgrp (); |
645 | #else | |
646 | setpgrp (pid, pid); | |
647 | #endif /* USG */ | |
2d607244 RS |
648 | child_setup (filefd, fd1, fd_error, (char **) new_argv, |
649 | 0, current_dir); | |
80856e74 | 650 | } |
aba637ec KS |
651 | |
652 | UNBLOCK_INPUT; | |
bad95d8f | 653 | #endif /* not WINDOWSNT */ |
cd5f8f60 RS |
654 | |
655 | /* The MSDOS case did this already. */ | |
656 | if (fd_error >= 0) | |
68c45bf0 | 657 | emacs_close (fd_error); |
2610078a | 658 | #endif /* not MSDOS */ |
e0f712ba | 659 | #endif /* not MAC_OS8 */ |
80856e74 | 660 | |
80856e74 JB |
661 | environ = save_environ; |
662 | ||
6b6e798b RS |
663 | /* Close most of our fd's, but not fd[0] |
664 | since we will use that to read input from. */ | |
68c45bf0 | 665 | emacs_close (filefd); |
799abb26 | 666 | if (fd1 >= 0 && fd1 != fd_error) |
68c45bf0 | 667 | emacs_close (fd1); |
80856e74 JB |
668 | } |
669 | ||
670 | if (pid < 0) | |
671 | { | |
6b6e798b | 672 | if (fd[0] >= 0) |
68c45bf0 | 673 | emacs_close (fd[0]); |
80856e74 JB |
674 | report_file_error ("Doing vfork", Qnil); |
675 | } | |
676 | ||
d50d3dc8 | 677 | if (INTEGERP (buffer)) |
80856e74 | 678 | { |
6b6e798b | 679 | if (fd[0] >= 0) |
68c45bf0 | 680 | emacs_close (fd[0]); |
80856e74 | 681 | #ifndef subprocesses |
e576cab4 JB |
682 | /* If Emacs has been built with asynchronous subprocess support, |
683 | we don't need to do this, I think because it will then have | |
684 | the facilities for handling SIGCHLD. */ | |
80856e74 JB |
685 | wait_without_blocking (); |
686 | #endif /* subprocesses */ | |
80856e74 JB |
687 | return Qnil; |
688 | } | |
689 | ||
6b6e798b | 690 | /* Enable sending signal if user quits below. */ |
37d54121 RS |
691 | call_process_exited = 0; |
692 | ||
e0f712ba | 693 | #if defined(MSDOS) || defined(MAC_OS8) |
7e6c2178 RS |
694 | /* MSDOS needs different cleanup information. */ |
695 | record_unwind_protect (call_process_cleanup, | |
696 | Fcons (make_number (fd[0]), build_string (tempfile))); | |
697 | #else | |
80856e74 JB |
698 | record_unwind_protect (call_process_cleanup, |
699 | Fcons (make_number (fd[0]), make_number (pid))); | |
e0f712ba | 700 | #endif /* not MSDOS and not MAC_OS8 */ |
80856e74 JB |
701 | |
702 | ||
d50d3dc8 | 703 | if (BUFFERP (buffer)) |
80856e74 JB |
704 | Fset_buffer (buffer); |
705 | ||
09494912 RS |
706 | if (NILP (buffer)) |
707 | { | |
708 | /* If BUFFER is nil, we must read process output once and then | |
709 | discard it, so setup coding system but with nil. */ | |
710 | setup_coding_system (Qnil, &process_coding); | |
711 | } | |
712 | else | |
713 | { | |
714 | Lisp_Object val, *args2; | |
715 | ||
716 | val = Qnil; | |
717 | if (!NILP (Vcoding_system_for_read)) | |
718 | val = Vcoding_system_for_read; | |
719 | else | |
720 | { | |
721 | if (EQ (coding_systems, Qt)) | |
722 | { | |
723 | int i; | |
724 | ||
725 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); | |
726 | args2[0] = Qcall_process; | |
727 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
728 | coding_systems | |
729 | = Ffind_operation_coding_system (nargs + 1, args2); | |
730 | } | |
731 | if (CONSP (coding_systems)) | |
70949dac | 732 | val = XCAR (coding_systems); |
09494912 | 733 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 734 | val = XCAR (Vdefault_process_coding_system); |
09494912 RS |
735 | else |
736 | val = Qnil; | |
737 | } | |
738 | setup_coding_system (Fcheck_coding_system (val), &process_coding); | |
739 | /* In unibyte mode, character code conversion should not take | |
740 | place but EOL conversion should. So, setup raw-text or one | |
741 | of the subsidiary according to the information just setup. */ | |
742 | if (NILP (current_buffer->enable_multibyte_characters) | |
743 | && !NILP (val)) | |
744 | setup_raw_text_coding_system (&process_coding); | |
745 | } | |
c5bfa12b KH |
746 | process_coding.src_multibyte = 0; |
747 | process_coding.dst_multibyte | |
748 | = (BUFFERP (buffer) | |
749 | ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters) | |
750 | : ! NILP (current_buffer->enable_multibyte_characters)); | |
09494912 | 751 | |
80856e74 JB |
752 | immediate_quit = 1; |
753 | QUIT; | |
754 | ||
755 | { | |
756 | register int nread; | |
0ad477db | 757 | int first = 1; |
6e3bfbb2 | 758 | int total_read = 0; |
321fecde | 759 | int carryover = 0; |
0aa2630f | 760 | int display_on_the_fly = display_p; |
05b44e90 | 761 | struct coding_system saved_coding; |
f778b157 KH |
762 | int pt_orig = PT, pt_byte_orig = PT_BYTE; |
763 | int inserted; | |
05b44e90 KH |
764 | |
765 | saved_coding = process_coding; | |
f0b950cf KH |
766 | if (process_coding.composing != COMPOSITION_DISABLED) |
767 | coding_allocate_composition_data (&process_coding, PT); | |
60558b19 | 768 | while (1) |
80856e74 | 769 | { |
60558b19 RS |
770 | /* Repeatedly read until we've filled as much as possible |
771 | of the buffer size we have. But don't read | |
8e6208c5 | 772 | less than 1024--save that for the next bufferful. */ |
321fecde | 773 | nread = carryover; |
60558b19 | 774 | while (nread < bufsize - 1024) |
00fb3e95 | 775 | { |
4da256b1 | 776 | int this_read = emacs_read (fd[0], buf + nread, |
68c45bf0 | 777 | bufsize - nread); |
60558b19 RS |
778 | |
779 | if (this_read < 0) | |
780 | goto give_up; | |
781 | ||
782 | if (this_read == 0) | |
7a7ab107 KH |
783 | { |
784 | process_coding.mode |= CODING_MODE_LAST_BLOCK; | |
785 | break; | |
786 | } | |
60558b19 RS |
787 | |
788 | nread += this_read; | |
7a7ab107 | 789 | total_read += this_read; |
60558b19 | 790 | |
7a7ab107 KH |
791 | if (display_on_the_fly) |
792 | break; | |
793 | } | |
60558b19 RS |
794 | |
795 | /* Now NREAD is the total amount of data in the buffer. */ | |
80856e74 | 796 | immediate_quit = 0; |
177c0ea7 | 797 | |
012c6fcb | 798 | if (!NILP (buffer)) |
32d08644 | 799 | { |
c5bfa12b | 800 | if (! CODING_MAY_REQUIRE_DECODING (&process_coding)) |
4da256b1 | 801 | insert_1_both (buf, nread, nread, 0, 1, 0); |
32d08644 KH |
802 | else |
803 | { /* We have to decode the input. */ | |
f0b950cf KH |
804 | int size; |
805 | char *decoding_buf; | |
32d08644 | 806 | |
f0b950cf KH |
807 | repeat_decoding: |
808 | size = decoding_buffer_size (&process_coding, nread); | |
809 | decoding_buf = (char *) xmalloc (size); | |
177c0ea7 | 810 | |
b252f801 KH |
811 | /* We can't use the macro CODING_REQUIRE_DETECTION |
812 | because it always returns nonzero if the coding | |
813 | system requires EOL detection. Here, we have to | |
814 | check only whether or not the coding system | |
815 | requires text-encoding detection. */ | |
816 | if (process_coding.type == coding_type_undecided) | |
950a45d0 | 817 | { |
4da256b1 | 818 | detect_coding (&process_coding, buf, nread); |
950a45d0 | 819 | if (process_coding.composing != COMPOSITION_DISABLED) |
07c381ad KH |
820 | /* We have not yet allocated the composition |
821 | data because the coding type was undecided. */ | |
950a45d0 KH |
822 | coding_allocate_composition_data (&process_coding, PT); |
823 | } | |
2d892150 KH |
824 | if (process_coding.cmp_data) |
825 | process_coding.cmp_data->char_offset = PT; | |
177c0ea7 | 826 | |
4da256b1 | 827 | decode_coding (&process_coding, buf, decoding_buf, |
321fecde | 828 | nread, size); |
177c0ea7 | 829 | |
7a7ab107 KH |
830 | if (display_on_the_fly |
831 | && saved_coding.type == coding_type_undecided | |
832 | && process_coding.type != coding_type_undecided) | |
833 | { | |
834 | /* We have detected some coding system. But, | |
835 | there's a possibility that the detection was | |
0aa2630f KS |
836 | done by insufficient data. So, we try the code |
837 | detection again with more data. */ | |
a871dd58 | 838 | xfree (decoding_buf); |
7a7ab107 KH |
839 | display_on_the_fly = 0; |
840 | process_coding = saved_coding; | |
841 | carryover = nread; | |
0aa2630f KS |
842 | /* This is to make the above condition always |
843 | fails in the future. */ | |
844 | saved_coding.type = coding_type_no_conversion; | |
7a7ab107 KH |
845 | continue; |
846 | } | |
177c0ea7 | 847 | |
321fecde | 848 | if (process_coding.produced > 0) |
2d892150 KH |
849 | insert_1_both (decoding_buf, process_coding.produced_char, |
850 | process_coding.produced, 0, 1, 0); | |
a871dd58 | 851 | xfree (decoding_buf); |
7bdba03c GM |
852 | |
853 | if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL) | |
854 | { | |
855 | Lisp_Object eol_type, coding; | |
856 | ||
857 | if (process_coding.eol_type == CODING_EOL_CR) | |
858 | { | |
859 | /* CRs have been replaced with LFs. Undo | |
860 | that in the text inserted above. */ | |
861 | unsigned char *p; | |
177c0ea7 | 862 | |
7bdba03c | 863 | move_gap_both (PT, PT_BYTE); |
177c0ea7 | 864 | |
7bdba03c GM |
865 | p = BYTE_POS_ADDR (pt_byte_orig); |
866 | for (; p < GPT_ADDR; ++p) | |
867 | if (*p == '\n') | |
868 | *p = '\r'; | |
869 | } | |
870 | else if (process_coding.eol_type == CODING_EOL_CRLF) | |
871 | { | |
872 | /* CR LFs have been replaced with LFs. Undo | |
873 | that by inserting CRs in front of LFs in | |
874 | the text inserted above. */ | |
875 | EMACS_INT bytepos, old_pt, old_pt_byte, nCR; | |
876 | ||
877 | old_pt = PT; | |
878 | old_pt_byte = PT_BYTE; | |
879 | nCR = 0; | |
177c0ea7 | 880 | |
7bdba03c GM |
881 | for (bytepos = PT_BYTE - 1; |
882 | bytepos >= pt_byte_orig; | |
883 | --bytepos) | |
884 | if (FETCH_BYTE (bytepos) == '\n') | |
885 | { | |
886 | EMACS_INT charpos = BYTE_TO_CHAR (bytepos); | |
887 | TEMP_SET_PT_BOTH (charpos, bytepos); | |
888 | insert_1_both ("\r", 1, 1, 0, 1, 0); | |
889 | ++nCR; | |
890 | } | |
891 | ||
892 | TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR); | |
893 | } | |
894 | ||
895 | /* Set the coding system symbol to that for | |
896 | Unix-like EOL. */ | |
897 | eol_type = Fget (saved_coding.symbol, Qeol_type); | |
898 | if (VECTORP (eol_type) | |
899 | && ASIZE (eol_type) == 3 | |
900 | && SYMBOLP (AREF (eol_type, CODING_EOL_LF))) | |
901 | coding = AREF (eol_type, CODING_EOL_LF); | |
902 | else | |
903 | coding = saved_coding.symbol; | |
177c0ea7 | 904 | |
7bdba03c GM |
905 | process_coding.symbol = coding; |
906 | process_coding.eol_type = CODING_EOL_LF; | |
907 | process_coding.mode | |
908 | &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL; | |
909 | } | |
177c0ea7 | 910 | |
f0b950cf KH |
911 | nread -= process_coding.consumed; |
912 | carryover = nread; | |
321fecde | 913 | if (carryover > 0) |
c5bfa12b KH |
914 | /* As CARRYOVER should not be that large, we had |
915 | better avoid overhead of bcopy. */ | |
4da256b1 | 916 | BCOPY_SHORT (buf + process_coding.consumed, buf, |
c5bfa12b | 917 | carryover); |
f0b950cf KH |
918 | if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP) |
919 | { | |
920 | /* The decoding ended because of insufficient data | |
921 | area to record information about composition. | |
922 | We must try decoding with additional data area | |
2d892150 | 923 | before reading more output for the process. */ |
f0b950cf KH |
924 | coding_allocate_composition_data (&process_coding, PT); |
925 | goto repeat_decoding; | |
926 | } | |
32d08644 KH |
927 | } |
928 | } | |
c5bfa12b | 929 | |
321fecde | 930 | if (process_coding.mode & CODING_MODE_LAST_BLOCK) |
c5bfa12b | 931 | break; |
6e3bfbb2 | 932 | |
4da256b1 | 933 | #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX) |
6e3bfbb2 | 934 | /* Make the buffer bigger as we continue to read more data, |
4da256b1 KS |
935 | but not past CALLPROC_BUFFER_SIZE_MAX. */ |
936 | if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize) | |
937 | if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX) | |
938 | bufsize = CALLPROC_BUFFER_SIZE_MAX; | |
939 | #endif | |
6e3bfbb2 | 940 | |
0aa2630f | 941 | if (display_p) |
0ad477db RS |
942 | { |
943 | if (first) | |
944 | prepare_menu_bars (); | |
945 | first = 0; | |
3007ebfb | 946 | redisplay_preserve_echo_area (1); |
0aa2630f KS |
947 | /* This variable might have been set to 0 for code |
948 | detection. In that case, we set it back to 1 because | |
949 | we should have already detected a coding system. */ | |
950 | display_on_the_fly = 1; | |
0ad477db | 951 | } |
80856e74 JB |
952 | immediate_quit = 1; |
953 | QUIT; | |
954 | } | |
60558b19 | 955 | give_up: ; |
80856e74 | 956 | |
2d892150 KH |
957 | if (!NILP (buffer) |
958 | && process_coding.cmp_data) | |
959 | { | |
960 | coding_restore_composition (&process_coding, Fcurrent_buffer ()); | |
961 | coding_free_composition_data (&process_coding); | |
962 | } | |
f0b950cf | 963 | |
838c9726 | 964 | { |
aed13378 | 965 | int post_read_count = SPECPDL_INDEX (); |
838c9726 KH |
966 | |
967 | record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
968 | inserted = PT - pt_orig; | |
969 | TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig); | |
970 | if (SYMBOLP (process_coding.post_read_conversion) | |
971 | && !NILP (Ffboundp (process_coding.post_read_conversion))) | |
972 | call1 (process_coding.post_read_conversion, make_number (inserted)); | |
f778b157 | 973 | |
838c9726 | 974 | Vlast_coding_system_used = process_coding.symbol; |
bbd29cfe | 975 | |
838c9726 KH |
976 | /* If the caller required, let the buffer inherit the |
977 | coding-system used to decode the process output. */ | |
978 | if (inherit_process_coding_system) | |
979 | call1 (intern ("after-insert-file-set-buffer-file-coding-system"), | |
980 | make_number (total_read)); | |
981 | ||
982 | unbind_to (post_read_count, Qnil); | |
983 | } | |
3b440bb5 EZ |
984 | } |
985 | ||
80856e74 JB |
986 | /* Wait for it to terminate, unless it already has. */ |
987 | wait_for_termination (pid); | |
988 | ||
989 | immediate_quit = 0; | |
990 | ||
991 | set_buffer_internal (old); | |
992 | ||
37d54121 RS |
993 | /* Don't kill any children that the subprocess may have left behind |
994 | when exiting. */ | |
995 | call_process_exited = 1; | |
996 | ||
80856e74 JB |
997 | unbind_to (count, Qnil); |
998 | ||
ca498128 JD |
999 | if (synch_process_termsig) |
1000 | { | |
1001 | char *signame; | |
1002 | ||
1003 | synchronize_system_messages_locale (); | |
1004 | signame = strsignal (synch_process_termsig); | |
1005 | ||
1006 | if (signame == 0) | |
1007 | signame = "unknown"; | |
1008 | ||
1009 | synch_process_death = signame; | |
1010 | } | |
1011 | ||
80856e74 | 1012 | if (synch_process_death) |
68c45bf0 PE |
1013 | return code_convert_string_norecord (build_string (synch_process_death), |
1014 | Vlocale_coding_system, 0); | |
80856e74 JB |
1015 | return make_number (synch_process_retcode); |
1016 | } | |
1017 | #endif | |
1018 | \f | |
9fefd2ba | 1019 | static Lisp_Object |
80856e74 JB |
1020 | delete_temp_file (name) |
1021 | Lisp_Object name; | |
1022 | { | |
2e3dc201 | 1023 | /* Use Fdelete_file (indirectly) because that runs a file name handler. |
59750d69 | 1024 | We did that when writing the file, so we should do so when deleting. */ |
2e3dc201 | 1025 | internal_delete_file (name); |
320695d8 | 1026 | return Qnil; |
80856e74 JB |
1027 | } |
1028 | ||
1029 | DEFUN ("call-process-region", Fcall_process_region, Scall_process_region, | |
fdb82f93 PJ |
1030 | 3, MANY, 0, |
1031 | doc: /* Send text from START to END to a synchronous process running PROGRAM. | |
1032 | The remaining arguments are optional. | |
1033 | Delete the text if fourth arg DELETE is non-nil. | |
1034 | ||
1035 | Insert output in BUFFER before point; t means current buffer; | |
1036 | nil for BUFFER means discard it; 0 means discard and don't wait. | |
1037 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
1038 | REAL-BUFFER says what to do with standard output, as above, | |
1039 | while STDERR-FILE says what to do with standard error in the child. | |
1040 | STDERR-FILE may be nil (discard standard error output), | |
1041 | t (mix it with ordinary output), or a file name string. | |
1042 | ||
1043 | Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted. | |
1044 | Remaining args are passed to PROGRAM at startup as command args. | |
1045 | ||
ba9a5174 | 1046 | If BUFFER is 0, `call-process-region' returns immediately with value nil. |
fdb82f93 PJ |
1047 | Otherwise it waits for PROGRAM to terminate |
1048 | and returns a numeric exit status or a signal description string. | |
d98b59b5 MB |
1049 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again. |
1050 | ||
1051 | usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */) | |
fdb82f93 | 1052 | (nargs, args) |
80856e74 JB |
1053 | int nargs; |
1054 | register Lisp_Object *args; | |
1055 | { | |
39323a7e KH |
1056 | struct gcpro gcpro1; |
1057 | Lisp_Object filename_string; | |
1058 | register Lisp_Object start, end; | |
aed13378 | 1059 | int count = SPECPDL_INDEX (); |
08ee4e87 | 1060 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
09494912 | 1061 | Lisp_Object coding_systems; |
32d08644 KH |
1062 | Lisp_Object val, *args2; |
1063 | int i; | |
bad95d8f | 1064 | #ifdef DOS_NT |
7e6c2178 | 1065 | char *tempfile; |
7e6c2178 RS |
1066 | char *outf = '\0'; |
1067 | ||
8a52365c EZ |
1068 | if ((outf = egetenv ("TMPDIR")) |
1069 | || (outf = egetenv ("TMP")) | |
1070 | || (outf = egetenv ("TEMP"))) | |
7e6c2178 RS |
1071 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); |
1072 | else | |
1073 | { | |
1074 | tempfile = alloca (20); | |
1075 | *tempfile = '\0'; | |
1076 | } | |
0774fcf8 | 1077 | if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1])) |
7e6c2178 | 1078 | strcat (tempfile, "/"); |
5711b547 RS |
1079 | if ('/' == DIRECTORY_SEP) |
1080 | dostounix_filename (tempfile); | |
1081 | else | |
1082 | unixtodos_filename (tempfile); | |
0774fcf8 RS |
1083 | #ifdef WINDOWSNT |
1084 | strcat (tempfile, "emXXXXXX"); | |
1085 | #else | |
7e6c2178 | 1086 | strcat (tempfile, "detmp.XXX"); |
0774fcf8 | 1087 | #endif |
bad95d8f | 1088 | #else /* not DOS_NT */ |
d5db4077 KR |
1089 | char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1); |
1090 | bcopy (SDATA (Vtemp_file_name_pattern), tempfile, | |
1091 | SBYTES (Vtemp_file_name_pattern) + 1); | |
bad95d8f | 1092 | #endif /* not DOS_NT */ |
7e6c2178 | 1093 | |
09494912 RS |
1094 | coding_systems = Qt; |
1095 | ||
1ddc85a4 DL |
1096 | #ifdef HAVE_MKSTEMP |
1097 | { | |
1098 | int fd = mkstemp (tempfile); | |
1099 | if (fd == -1) | |
1100 | report_file_error ("Failed to open temporary file", | |
1101 | Fcons (Vtemp_file_name_pattern, Qnil)); | |
1102 | else | |
1103 | close (fd); | |
1104 | } | |
1105 | #else | |
80856e74 | 1106 | mktemp (tempfile); |
1ddc85a4 | 1107 | #endif |
80856e74 JB |
1108 | |
1109 | filename_string = build_string (tempfile); | |
39323a7e | 1110 | GCPRO1 (filename_string); |
80856e74 JB |
1111 | start = args[0]; |
1112 | end = args[1]; | |
32d08644 | 1113 | /* Decide coding-system of the contents of the temporary file. */ |
91489411 RS |
1114 | if (!NILP (Vcoding_system_for_write)) |
1115 | val = Vcoding_system_for_write; | |
1116 | else if (NILP (current_buffer->enable_multibyte_characters)) | |
32d08644 KH |
1117 | val = Qnil; |
1118 | else | |
beacaab3 | 1119 | { |
91489411 RS |
1120 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); |
1121 | args2[0] = Qcall_process_region; | |
1122 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; | |
1123 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); | |
1124 | if (CONSP (coding_systems)) | |
70949dac | 1125 | val = XCDR (coding_systems); |
91489411 | 1126 | else if (CONSP (Vdefault_process_coding_system)) |
70949dac | 1127 | val = XCDR (Vdefault_process_coding_system); |
beacaab3 | 1128 | else |
91489411 | 1129 | val = Qnil; |
beacaab3 | 1130 | } |
32d08644 | 1131 | |
168afdaa | 1132 | { |
aed13378 | 1133 | int count1 = SPECPDL_INDEX (); |
168afdaa RS |
1134 | |
1135 | specbind (intern ("coding-system-for-write"), val); | |
1136 | Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil); | |
1137 | ||
1138 | unbind_to (count1, Qnil); | |
1139 | } | |
91489411 | 1140 | |
177c0ea7 | 1141 | /* Note that Fcall_process takes care of binding |
91489411 | 1142 | coding-system-for-read. */ |
093650fe | 1143 | |
80856e74 JB |
1144 | record_unwind_protect (delete_temp_file, filename_string); |
1145 | ||
edf496dd | 1146 | if (nargs > 3 && !NILP (args[3])) |
80856e74 JB |
1147 | Fdelete_region (start, end); |
1148 | ||
edf496dd KH |
1149 | if (nargs > 3) |
1150 | { | |
1151 | args += 2; | |
1152 | nargs -= 2; | |
1153 | } | |
1154 | else | |
1155 | { | |
1156 | args[0] = args[2]; | |
1157 | nargs = 2; | |
1158 | } | |
1159 | args[1] = filename_string; | |
80856e74 | 1160 | |
edf496dd | 1161 | RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args))); |
80856e74 JB |
1162 | } |
1163 | \f | |
1164 | #ifndef VMS /* VMS version is in vmsproc.c. */ | |
1165 | ||
dfcf069d AS |
1166 | static int relocate_fd (); |
1167 | ||
5990851d KL |
1168 | static char ** |
1169 | add_env (char **env, char **new_env, char *string) | |
1170 | { | |
1171 | char **ep; | |
1172 | int ok = 1; | |
1173 | if (string == NULL) | |
1174 | return new_env; | |
1175 | ||
1176 | /* See if this string duplicates any string already in the env. | |
1177 | If so, don't put it in. | |
1178 | When an env var has multiple definitions, | |
1179 | we keep the definition that comes first in process-environment. */ | |
1180 | for (ep = env; ok && ep != new_env; ep++) | |
1181 | { | |
1182 | char *p = *ep, *q = string; | |
1183 | while (ok) | |
1184 | { | |
1185 | if (*q != *p) | |
1186 | break; | |
1187 | if (*q == 0) | |
1188 | /* The string is a lone variable name; keep it for now, we | |
1189 | will remove it later. It is a placeholder for a | |
1190 | variable that is not to be included in the environment. */ | |
1191 | break; | |
1192 | if (*q == '=') | |
1193 | ok = 0; | |
1194 | p++, q++; | |
1195 | } | |
1196 | } | |
1197 | if (ok) | |
1198 | *new_env++ = string; | |
1199 | return new_env; | |
1200 | } | |
1201 | ||
80856e74 JB |
1202 | /* This is the last thing run in a newly forked inferior |
1203 | either synchronous or asynchronous. | |
1204 | Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2. | |
1205 | Initialize inferior's priority, pgrp, connected dir and environment. | |
1206 | then exec another program based on new_argv. | |
1207 | ||
1208 | This function may change environ for the superior process. | |
1209 | Therefore, the superior process must save and restore the value | |
1210 | of environ around the vfork and the call to this function. | |
1211 | ||
80856e74 | 1212 | SET_PGRP is nonzero if we should put the subprocess into a separate |
177c0ea7 | 1213 | process group. |
e576cab4 JB |
1214 | |
1215 | CURRENT_DIR is an elisp string giving the path of the current | |
1216 | directory the subprocess should have. Since we can't really signal | |
1217 | a decent error from within the child, this should be verified as an | |
1218 | executable directory by the parent. */ | |
80856e74 | 1219 | |
dfcf069d | 1220 | int |
e576cab4 | 1221 | child_setup (in, out, err, new_argv, set_pgrp, current_dir) |
80856e74 JB |
1222 | int in, out, err; |
1223 | register char **new_argv; | |
80856e74 | 1224 | int set_pgrp; |
e576cab4 | 1225 | Lisp_Object current_dir; |
80856e74 | 1226 | { |
e576cab4 | 1227 | char **env; |
7fcf7f05 | 1228 | char *pwd_var; |
bad95d8f RS |
1229 | #ifdef WINDOWSNT |
1230 | int cpid; | |
4252a4bd | 1231 | HANDLE handles[3]; |
bad95d8f | 1232 | #endif /* WINDOWSNT */ |
e576cab4 | 1233 | |
33abe2d9 | 1234 | int pid = getpid (); |
80856e74 | 1235 | |
68d10241 | 1236 | #ifdef SET_EMACS_PRIORITY |
4f0b9d49 | 1237 | { |
31ade731 | 1238 | extern EMACS_INT emacs_priority; |
4f0b9d49 | 1239 | |
68d10241 RS |
1240 | if (emacs_priority < 0) |
1241 | nice (- emacs_priority); | |
4f0b9d49 | 1242 | } |
5b633aeb | 1243 | #endif |
80856e74 JB |
1244 | |
1245 | #ifdef subprocesses | |
1246 | /* Close Emacs's descriptors that this process should not have. */ | |
1247 | close_process_descs (); | |
1248 | #endif | |
c17c4250 EZ |
1249 | /* DOS_NT isn't in a vfork, so if we are in the middle of load-file, |
1250 | we will lose if we call close_load_descs here. */ | |
1251 | #ifndef DOS_NT | |
4458cebe | 1252 | close_load_descs (); |
c17c4250 | 1253 | #endif |
80856e74 JB |
1254 | |
1255 | /* Note that use of alloca is always safe here. It's obvious for systems | |
1256 | that do not have true vfork or that have true (stack) alloca. | |
1257 | If using vfork and C_ALLOCA it is safe because that changes | |
1258 | the superior's static variables as if the superior had done alloca | |
1259 | and will be cleaned up in the usual way. */ | |
e576cab4 | 1260 | { |
7fcf7f05 | 1261 | register char *temp; |
e576cab4 | 1262 | register int i; |
77d78be1 | 1263 | |
d5db4077 | 1264 | i = SBYTES (current_dir); |
16425c4a EZ |
1265 | #ifdef MSDOS |
1266 | /* MSDOS must have all environment variables malloc'ed, because | |
1267 | low-level libc functions that launch subsidiary processes rely | |
1268 | on that. */ | |
1269 | pwd_var = (char *) xmalloc (i + 6); | |
1270 | #else | |
7fcf7f05 | 1271 | pwd_var = (char *) alloca (i + 6); |
16425c4a | 1272 | #endif |
7fcf7f05 RS |
1273 | temp = pwd_var + 4; |
1274 | bcopy ("PWD=", pwd_var, 4); | |
d5db4077 | 1275 | bcopy (SDATA (current_dir), temp, i); |
bad95d8f | 1276 | if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP; |
e576cab4 JB |
1277 | temp[i] = 0; |
1278 | ||
c17c4250 | 1279 | #ifndef DOS_NT |
e576cab4 JB |
1280 | /* We can't signal an Elisp error here; we're in a vfork. Since |
1281 | the callers check the current directory before forking, this | |
1282 | should only return an error if the directory's permissions | |
1283 | are changed between the check and this chdir, but we should | |
1284 | at least check. */ | |
1285 | if (chdir (temp) < 0) | |
20b25e46 | 1286 | _exit (errno); |
b4c7684c | 1287 | #endif |
7fcf7f05 | 1288 | |
c17c4250 EZ |
1289 | #ifdef DOS_NT |
1290 | /* Get past the drive letter, so that d:/ is left alone. */ | |
1291 | if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2])) | |
1292 | { | |
1293 | temp += 2; | |
1294 | i -= 2; | |
1295 | } | |
1296 | #endif | |
1297 | ||
7fcf7f05 | 1298 | /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */ |
bad95d8f | 1299 | while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1])) |
7fcf7f05 | 1300 | temp[--i] = 0; |
e576cab4 | 1301 | } |
80856e74 | 1302 | |
5990851d | 1303 | /* Set `env' to a vector of the strings in the environment. */ |
80856e74 JB |
1304 | { |
1305 | register Lisp_Object tem; | |
1306 | register char **new_env; | |
5990851d | 1307 | char **p, **q; |
80856e74 | 1308 | register int new_length; |
5990851d | 1309 | Lisp_Object environment = Vglobal_environment; |
f105f403 | 1310 | Lisp_Object local; |
80856e74 JB |
1311 | |
1312 | new_length = 0; | |
f105f403 | 1313 | |
5990851d KL |
1314 | for (tem = Vprocess_environment; |
1315 | CONSP (tem) && STRINGP (XCAR (tem)); | |
1316 | tem = XCDR (tem)) | |
1317 | new_length++; | |
1318 | ||
f105f403 KL |
1319 | if (!NILP (Vlocal_environment_variables)) |
1320 | { | |
da8e8fc1 KL |
1321 | local = get_frame_param (XFRAME (Fframe_with_environment (selected_frame)), |
1322 | Qenvironment); | |
f105f403 KL |
1323 | if (EQ (Vlocal_environment_variables, Qt) |
1324 | && !NILP (local)) | |
1325 | environment = local; | |
1326 | else if (CONSP (local)) | |
1327 | { | |
1328 | new_length += Fsafe_length (Vlocal_environment_variables); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | for (tem = environment; | |
70949dac KR |
1333 | CONSP (tem) && STRINGP (XCAR (tem)); |
1334 | tem = XCDR (tem)) | |
80856e74 JB |
1335 | new_length++; |
1336 | ||
7fcf7f05 RS |
1337 | /* new_length + 2 to include PWD and terminating 0. */ |
1338 | env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *)); | |
1339 | ||
1340 | /* If we have a PWD envvar, pass one down, | |
1341 | but with corrected value. */ | |
1342 | if (getenv ("PWD")) | |
1343 | *new_env++ = pwd_var; | |
5990851d KL |
1344 | |
1345 | /* Overrides. */ | |
1346 | for (tem = Vprocess_environment; | |
1347 | CONSP (tem) && STRINGP (XCAR (tem)); | |
1348 | tem = XCDR (tem)) | |
1349 | new_env = add_env (env, new_env, SDATA (XCAR (tem))); | |
80856e74 | 1350 | |
5990851d | 1351 | /* Local part of environment, if Vlocal_environment_variables is a list. */ |
f105f403 KL |
1352 | for (tem = Vlocal_environment_variables; |
1353 | CONSP (tem) && STRINGP (XCAR (tem)); | |
1354 | tem = XCDR (tem)) | |
5990851d | 1355 | new_env = add_env (env, new_env, egetenv (SDATA (XCAR (tem)))); |
f105f403 | 1356 | |
5990851d | 1357 | /* The rest of the environment (either Vglobal_environment or the |
da8e8fc1 | 1358 | 'environment frame parameter). */ |
f105f403 | 1359 | for (tem = environment; |
70949dac KR |
1360 | CONSP (tem) && STRINGP (XCAR (tem)); |
1361 | tem = XCDR (tem)) | |
5990851d KL |
1362 | new_env = add_env (env, new_env, SDATA (XCAR (tem))); |
1363 | ||
1364 | *new_env = 0; | |
1365 | ||
1366 | /* Remove variable names without values. */ | |
1367 | p = q = env; | |
1368 | while (*p != 0) | |
cd9565ba | 1369 | { |
5990851d KL |
1370 | while (*q != 0 && strchr (*q, '=') == NULL) |
1371 | *q++; | |
1372 | *p = *q++; | |
1373 | if (*p != 0) | |
1374 | p++; | |
cd9565ba | 1375 | } |
80856e74 | 1376 | } |
bad95d8f RS |
1377 | #ifdef WINDOWSNT |
1378 | prepare_standard_handles (in, out, err, handles); | |
d5db4077 | 1379 | set_process_dir (SDATA (current_dir)); |
bad95d8f | 1380 | #else /* not WINDOWSNT */ |
426b37ae JB |
1381 | /* Make sure that in, out, and err are not actually already in |
1382 | descriptors zero, one, or two; this could happen if Emacs is | |
7e6c2178 | 1383 | started with its standard in, out, or error closed, as might |
426b37ae | 1384 | happen under X. */ |
f29f9e4a RS |
1385 | { |
1386 | int oin = in, oout = out; | |
1387 | ||
1388 | /* We have to avoid relocating the same descriptor twice! */ | |
1389 | ||
1390 | in = relocate_fd (in, 3); | |
1391 | ||
1392 | if (out == oin) | |
1393 | out = in; | |
1394 | else | |
3e9367e7 | 1395 | out = relocate_fd (out, 3); |
f29f9e4a RS |
1396 | |
1397 | if (err == oin) | |
1398 | err = in; | |
1399 | else if (err == oout) | |
1400 | err = out; | |
1401 | else | |
3e9367e7 | 1402 | err = relocate_fd (err, 3); |
f29f9e4a | 1403 | } |
426b37ae | 1404 | |
c17c4250 | 1405 | #ifndef MSDOS |
68c45bf0 PE |
1406 | emacs_close (0); |
1407 | emacs_close (1); | |
1408 | emacs_close (2); | |
80856e74 JB |
1409 | |
1410 | dup2 (in, 0); | |
1411 | dup2 (out, 1); | |
1412 | dup2 (err, 2); | |
68c45bf0 PE |
1413 | emacs_close (in); |
1414 | emacs_close (out); | |
1415 | emacs_close (err); | |
c17c4250 | 1416 | #endif /* not MSDOS */ |
bad95d8f | 1417 | #endif /* not WINDOWSNT */ |
80856e74 | 1418 | |
6b2cd868 | 1419 | #if defined(USG) && !defined(BSD_PGRPS) |
fdba8590 | 1420 | #ifndef SETPGRP_RELEASES_CTTY |
e576cab4 | 1421 | setpgrp (); /* No arguments but equivalent in this case */ |
fdba8590 | 1422 | #endif |
e576cab4 JB |
1423 | #else |
1424 | setpgrp (pid, pid); | |
1425 | #endif /* USG */ | |
a129418f RS |
1426 | /* setpgrp_of_tty is incorrect here; it uses input_fd. */ |
1427 | EMACS_SET_TTY_PGRP (0, &pid); | |
80856e74 | 1428 | |
c17c4250 EZ |
1429 | #ifdef MSDOS |
1430 | pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env); | |
a3f0666f | 1431 | xfree (pwd_var); |
c17c4250 EZ |
1432 | if (pid == -1) |
1433 | /* An error occurred while trying to run the subprocess. */ | |
1434 | report_file_error ("Spawning child process", Qnil); | |
1435 | return pid; | |
1436 | #else /* not MSDOS */ | |
bad95d8f RS |
1437 | #ifdef WINDOWSNT |
1438 | /* Spawn the child. (See ntproc.c:Spawnve). */ | |
1439 | cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env); | |
db77d785 | 1440 | reset_standard_handles (in, out, err, handles); |
ff27bfbe KH |
1441 | if (cpid == -1) |
1442 | /* An error occurred while trying to spawn the process. */ | |
1443 | report_file_error ("Spawning child process", Qnil); | |
bad95d8f RS |
1444 | return cpid; |
1445 | #else /* not WINDOWSNT */ | |
80856e74 JB |
1446 | /* execvp does not accept an environment arg so the only way |
1447 | to pass this environment is to set environ. Our caller | |
1448 | is responsible for restoring the ambient value of environ. */ | |
1449 | environ = env; | |
1450 | execvp (new_argv[0], new_argv); | |
1451 | ||
68c45bf0 PE |
1452 | emacs_write (1, "Can't exec program: ", 20); |
1453 | emacs_write (1, new_argv[0], strlen (new_argv[0])); | |
1454 | emacs_write (1, "\n", 1); | |
80856e74 | 1455 | _exit (1); |
bad95d8f | 1456 | #endif /* not WINDOWSNT */ |
7e6c2178 | 1457 | #endif /* not MSDOS */ |
80856e74 JB |
1458 | } |
1459 | ||
a3833dfe | 1460 | /* Move the file descriptor FD so that its number is not less than MINFD. |
426b37ae | 1461 | If the file descriptor is moved at all, the original is freed. */ |
dfcf069d | 1462 | static int |
a3833dfe KH |
1463 | relocate_fd (fd, minfd) |
1464 | int fd, minfd; | |
426b37ae | 1465 | { |
a3833dfe | 1466 | if (fd >= minfd) |
426b37ae JB |
1467 | return fd; |
1468 | else | |
1469 | { | |
1470 | int new = dup (fd); | |
1471 | if (new == -1) | |
1472 | { | |
20c018a0 | 1473 | char *message1 = "Error while setting up child: "; |
826c56ac | 1474 | char *errmessage = strerror (errno); |
20c018a0 | 1475 | char *message2 = "\n"; |
68c45bf0 PE |
1476 | emacs_write (2, message1, strlen (message1)); |
1477 | emacs_write (2, errmessage, strlen (errmessage)); | |
1478 | emacs_write (2, message2, strlen (message2)); | |
426b37ae JB |
1479 | _exit (1); |
1480 | } | |
1481 | /* Note that we hold the original FD open while we recurse, | |
1482 | to guarantee we'll get a new FD if we need it. */ | |
a3833dfe | 1483 | new = relocate_fd (new, minfd); |
68c45bf0 | 1484 | emacs_close (fd); |
426b37ae JB |
1485 | return new; |
1486 | } | |
1487 | } | |
1488 | ||
012c6fcb | 1489 | static int |
da8e8fc1 | 1490 | getenv_internal (var, varlen, value, valuelen, frame) |
012c6fcb JA |
1491 | char *var; |
1492 | int varlen; | |
1493 | char **value; | |
1494 | int *valuelen; | |
da8e8fc1 | 1495 | Lisp_Object frame; |
012c6fcb JA |
1496 | { |
1497 | Lisp_Object scan; | |
5990851d KL |
1498 | Lisp_Object environment = Vglobal_environment; |
1499 | ||
1500 | /* Try to find VAR in Vprocess_environment first. */ | |
1501 | for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan)) | |
1502 | { | |
1503 | Lisp_Object entry = XCAR (scan); | |
1504 | if (STRINGP (entry) | |
1505 | && SBYTES (entry) >= varlen | |
1506 | #ifdef WINDOWSNT | |
1507 | /* NT environment variables are case insensitive. */ | |
1508 | && ! strnicmp (SDATA (entry), var, varlen) | |
1509 | #else /* not WINDOWSNT */ | |
1510 | && ! bcmp (SDATA (entry), var, varlen) | |
1511 | #endif /* not WINDOWSNT */ | |
1512 | ) | |
1513 | { | |
1514 | if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=') | |
1515 | { | |
1516 | *value = (char *) SDATA (entry) + (varlen + 1); | |
1517 | *valuelen = SBYTES (entry) - (varlen + 1); | |
1518 | return 1; | |
1519 | } | |
1520 | else if (SBYTES (entry) == varlen) | |
1521 | { | |
1522 | /* Lone variable names in Vprocess_environment mean that | |
1523 | variable should be removed from the environment. */ | |
1524 | return 0; | |
1525 | } | |
1526 | } | |
1527 | } | |
f105f403 KL |
1528 | |
1529 | /* Find the environment in which to search the variable. */ | |
da8e8fc1 | 1530 | if (!NILP (frame)) |
f105f403 | 1531 | { |
da8e8fc1 KL |
1532 | CHECK_FRAME (frame); |
1533 | frame = Fframe_with_environment (frame); | |
1534 | Lisp_Object local = get_frame_param (XFRAME (frame), Qenvironment); | |
5990851d | 1535 | /* Use Vglobal_environment if there is no local environment. */ |
f105f403 KL |
1536 | if (!NILP (local)) |
1537 | environment = local; | |
1538 | } | |
1539 | else if (!NILP (Vlocal_environment_variables)) | |
1540 | { | |
da8e8fc1 KL |
1541 | Lisp_Object local = get_frame_param (XFRAME (Fframe_with_environment (selected_frame)), |
1542 | Qenvironment); | |
f105f403 KL |
1543 | if (EQ (Vlocal_environment_variables, Qt) |
1544 | && !NILP (local)) | |
1545 | environment = local; | |
1546 | else if (CONSP (local)) | |
1547 | { | |
1548 | for (scan = Vlocal_environment_variables; CONSP (scan); scan = XCDR (scan)) | |
1549 | { | |
1550 | Lisp_Object entry = XCAR (scan); | |
1551 | if (STRINGP (entry) | |
1552 | && SBYTES (entry) == varlen | |
1553 | #ifdef WINDOWSNT | |
1554 | /* NT environment variables are case insensitive. */ | |
1555 | && ! strnicmp (SDATA (entry), var, varlen) | |
1556 | #else /* not WINDOWSNT */ | |
1557 | && ! bcmp (SDATA (entry), var, varlen) | |
1558 | #endif /* not WINDOWSNT */ | |
1559 | ) | |
1560 | { | |
1561 | environment = local; | |
1562 | break; | |
1563 | } | |
1564 | } | |
1565 | } | |
1566 | } | |
012c6fcb | 1567 | |
f105f403 | 1568 | for (scan = environment; CONSP (scan); scan = XCDR (scan)) |
012c6fcb | 1569 | { |
c1350752 KH |
1570 | Lisp_Object entry; |
1571 | ||
70949dac | 1572 | entry = XCAR (scan); |
d50d3dc8 | 1573 | if (STRINGP (entry) |
f105f403 KL |
1574 | && SBYTES (entry) > varlen |
1575 | && SREF (entry, varlen) == '=' | |
bad95d8f | 1576 | #ifdef WINDOWSNT |
f105f403 KL |
1577 | /* NT environment variables are case insensitive. */ |
1578 | && ! strnicmp (SDATA (entry), var, varlen) | |
bad95d8f | 1579 | #else /* not WINDOWSNT */ |
f105f403 | 1580 | && ! bcmp (SDATA (entry), var, varlen) |
bad95d8f | 1581 | #endif /* not WINDOWSNT */ |
f105f403 | 1582 | ) |
012c6fcb | 1583 | { |
d5db4077 KR |
1584 | *value = (char *) SDATA (entry) + (varlen + 1); |
1585 | *valuelen = SBYTES (entry) - (varlen + 1); | |
012c6fcb JA |
1586 | return 1; |
1587 | } | |
1588 | } | |
1589 | ||
1590 | return 0; | |
1591 | } | |
1592 | ||
f105f403 | 1593 | DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0, |
5990851d KL |
1594 | doc: /* Get the value of environment variable VARIABLE. |
1595 | VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
1596 | the environment. Otherwise, value is a string. | |
f105f403 | 1597 | |
da8e8fc1 KL |
1598 | If optional parameter FRAME is non-nil, then it should be a frame. If |
1599 | that frame has its own set of environment variables, this function | |
1600 | will look up VARIABLE in there. | |
5990851d KL |
1601 | |
1602 | Otherwise, this function searches `process-environment' for VARIABLE. | |
17ccbd91 KL |
1603 | If it is not found there, then it continues the search in either |
1604 | `global-environment' or the environment list of the selected frame, | |
1605 | depending on the value of `local-environment-variables'. */) | |
da8e8fc1 KL |
1606 | (variable, frame) |
1607 | Lisp_Object variable, frame; | |
012c6fcb JA |
1608 | { |
1609 | char *value; | |
1610 | int valuelen; | |
1611 | ||
5990851d KL |
1612 | CHECK_STRING (variable); |
1613 | if (getenv_internal (SDATA (variable), SBYTES (variable), | |
da8e8fc1 | 1614 | &value, &valuelen, frame)) |
012c6fcb JA |
1615 | return make_string (value, valuelen); |
1616 | else | |
1617 | return Qnil; | |
1618 | } | |
1619 | ||
5990851d KL |
1620 | /* A version of getenv that consults the Lisp environment lists, |
1621 | easily callable from C. */ | |
012c6fcb JA |
1622 | char * |
1623 | egetenv (var) | |
e576cab4 | 1624 | char *var; |
012c6fcb JA |
1625 | { |
1626 | char *value; | |
1627 | int valuelen; | |
1628 | ||
f105f403 | 1629 | if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil)) |
012c6fcb JA |
1630 | return value; |
1631 | else | |
1632 | return 0; | |
1633 | } | |
1634 | ||
80856e74 JB |
1635 | #endif /* not VMS */ |
1636 | \f | |
8de15d69 | 1637 | /* This is run before init_cmdargs. */ |
177c0ea7 | 1638 | |
dfcf069d | 1639 | void |
8de15d69 RS |
1640 | init_callproc_1 () |
1641 | { | |
1642 | char *data_dir = egetenv ("EMACSDATA"); | |
35a2f4b8 KH |
1643 | char *doc_dir = egetenv ("EMACSDOC"); |
1644 | ||
8de15d69 | 1645 | Vdata_directory |
177c0ea7 | 1646 | = Ffile_name_as_directory (build_string (data_dir ? data_dir |
8de15d69 | 1647 | : PATH_DATA)); |
35a2f4b8 KH |
1648 | Vdoc_directory |
1649 | = Ffile_name_as_directory (build_string (doc_dir ? doc_dir | |
1650 | : PATH_DOC)); | |
9453ea7b | 1651 | |
e576cab4 | 1652 | /* Check the EMACSPATH environment variable, defaulting to the |
57bda87a | 1653 | PATH_EXEC path from epaths.h. */ |
e576cab4 | 1654 | Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC); |
80856e74 JB |
1655 | Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path)); |
1656 | Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path); | |
8de15d69 RS |
1657 | } |
1658 | ||
e17f7533 | 1659 | /* This is run after init_cmdargs, when Vinstallation_directory is valid. */ |
8de15d69 | 1660 | |
dfcf069d | 1661 | void |
8de15d69 RS |
1662 | init_callproc () |
1663 | { | |
1664 | char *data_dir = egetenv ("EMACSDATA"); | |
177c0ea7 | 1665 | |
8de15d69 RS |
1666 | register char * sh; |
1667 | Lisp_Object tempdir; | |
1668 | ||
9cc4fad5 | 1669 | if (!NILP (Vinstallation_directory)) |
8de15d69 | 1670 | { |
05630743 RS |
1671 | /* Add to the path the lib-src subdir of the installation dir. */ |
1672 | Lisp_Object tem; | |
1673 | tem = Fexpand_file_name (build_string ("lib-src"), | |
1674 | Vinstallation_directory); | |
bad95d8f | 1675 | #ifndef DOS_NT |
1a6640ec | 1676 | /* MSDOS uses wrapped binaries, so don't do this. */ |
0fa248bc | 1677 | if (NILP (Fmember (tem, Vexec_path))) |
70ec1377 RS |
1678 | { |
1679 | Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC); | |
1680 | Vexec_path = Fcons (tem, Vexec_path); | |
1681 | Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path); | |
1682 | } | |
177c0ea7 | 1683 | |
0fa248bc | 1684 | Vexec_directory = Ffile_name_as_directory (tem); |
bad95d8f | 1685 | #endif /* not DOS_NT */ |
8de15d69 | 1686 | |
e17f7533 RS |
1687 | /* Maybe use ../etc as well as ../lib-src. */ |
1688 | if (data_dir == 0) | |
1689 | { | |
1690 | tem = Fexpand_file_name (build_string ("etc"), | |
1691 | Vinstallation_directory); | |
1692 | Vdoc_directory = Ffile_name_as_directory (tem); | |
8de15d69 RS |
1693 | } |
1694 | } | |
7e933683 RS |
1695 | |
1696 | /* Look for the files that should be in etc. We don't use | |
1697 | Vinstallation_directory, because these files are never installed | |
e17f7533 | 1698 | near the executable, and they are never in the build |
7e933683 RS |
1699 | directory when that's different from the source directory. |
1700 | ||
1701 | Instead, if these files are not in the nominal place, we try the | |
1702 | source directory. */ | |
1703 | if (data_dir == 0) | |
1704 | { | |
70ec1377 | 1705 | Lisp_Object tem, tem1, srcdir; |
7e933683 | 1706 | |
70ec1377 RS |
1707 | srcdir = Fexpand_file_name (build_string ("../src/"), |
1708 | build_string (PATH_DUMPLOADSEARCH)); | |
7e933683 RS |
1709 | tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory); |
1710 | tem1 = Ffile_exists_p (tem); | |
70ec1377 | 1711 | if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1)) |
7e933683 | 1712 | { |
70ec1377 | 1713 | Lisp_Object newdir; |
7e933683 RS |
1714 | newdir = Fexpand_file_name (build_string ("../etc/"), |
1715 | build_string (PATH_DUMPLOADSEARCH)); | |
1716 | tem = Fexpand_file_name (build_string ("GNU"), newdir); | |
1717 | tem1 = Ffile_exists_p (tem); | |
1718 | if (!NILP (tem1)) | |
1719 | Vdata_directory = newdir; | |
1720 | } | |
1721 | } | |
80856e74 | 1722 | |
d883eb62 RS |
1723 | #ifndef CANNOT_DUMP |
1724 | if (initialized) | |
1725 | #endif | |
1726 | { | |
1727 | tempdir = Fdirectory_file_name (Vexec_directory); | |
d5db4077 | 1728 | if (access (SDATA (tempdir), 0) < 0) |
d883eb62 RS |
1729 | dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n", |
1730 | Vexec_directory); | |
1731 | } | |
80856e74 | 1732 | |
e576cab4 | 1733 | tempdir = Fdirectory_file_name (Vdata_directory); |
d5db4077 | 1734 | if (access (SDATA (tempdir), 0) < 0) |
76d5c6cf RS |
1735 | dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n", |
1736 | Vdata_directory); | |
e576cab4 | 1737 | |
80856e74 JB |
1738 | #ifdef VMS |
1739 | Vshell_file_name = build_string ("*dcl*"); | |
1740 | #else | |
e576cab4 | 1741 | sh = (char *) getenv ("SHELL"); |
80856e74 JB |
1742 | Vshell_file_name = build_string (sh ? sh : "/bin/sh"); |
1743 | #endif | |
8abd035b RS |
1744 | |
1745 | #ifdef VMS | |
1746 | Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX."); | |
1747 | #else | |
1748 | if (getenv ("TMPDIR")) | |
1749 | { | |
1750 | char *dir = getenv ("TMPDIR"); | |
1751 | Vtemp_file_name_pattern | |
1752 | = Fexpand_file_name (build_string ("emacsXXXXXX"), | |
1753 | build_string (dir)); | |
1754 | } | |
1755 | else | |
1756 | Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX"); | |
1757 | #endif | |
63789758 | 1758 | |
40b49d4b JB |
1759 | #ifdef DOS_NT |
1760 | Vshared_game_score_directory = Qnil; | |
1761 | #else | |
63789758 RS |
1762 | Vshared_game_score_directory = build_string (PATH_GAME); |
1763 | if (NILP (Ffile_directory_p (Vshared_game_score_directory))) | |
1764 | Vshared_game_score_directory = Qnil; | |
40b49d4b | 1765 | #endif |
9fefd2ba JB |
1766 | } |
1767 | ||
dfcf069d | 1768 | void |
5990851d | 1769 | set_global_environment () |
9fefd2ba JB |
1770 | { |
1771 | register char **envp; | |
80856e74 | 1772 | |
5990851d | 1773 | Vglobal_environment = Qnil; |
80856e74 JB |
1774 | #ifndef CANNOT_DUMP |
1775 | if (initialized) | |
1776 | #endif | |
1777 | for (envp = environ; *envp; envp++) | |
5990851d KL |
1778 | Vglobal_environment = Fcons (build_string (*envp), |
1779 | Vglobal_environment); | |
80856e74 JB |
1780 | } |
1781 | ||
dfcf069d | 1782 | void |
80856e74 JB |
1783 | syms_of_callproc () |
1784 | { | |
bad95d8f | 1785 | #ifdef DOS_NT |
093650fe RS |
1786 | Qbuffer_file_type = intern ("buffer-file-type"); |
1787 | staticpro (&Qbuffer_file_type); | |
bad95d8f | 1788 | #endif /* DOS_NT */ |
7e6c2178 | 1789 | |
80856e74 | 1790 | DEFVAR_LISP ("shell-file-name", &Vshell_file_name, |
fdb82f93 PJ |
1791 | doc: /* *File name to load inferior shells from. |
1792 | Initialized from the SHELL environment variable. */); | |
80856e74 JB |
1793 | |
1794 | DEFVAR_LISP ("exec-path", &Vexec_path, | |
fdb82f93 PJ |
1795 | doc: /* *List of directories to search programs to run in subprocesses. |
1796 | Each element is a string (directory name) or nil (try default directory). */); | |
80856e74 | 1797 | |
b81a1b72 | 1798 | DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes, |
fdb82f93 PJ |
1799 | doc: /* *List of suffixes to try to find executable file names. |
1800 | Each element is a string. */); | |
33d5af99 | 1801 | Vexec_suffixes = Qnil; |
b81a1b72 | 1802 | |
80856e74 | 1803 | DEFVAR_LISP ("exec-directory", &Vexec_directory, |
fdb82f93 PJ |
1804 | doc: /* Directory for executables for Emacs to invoke. |
1805 | More generally, this includes any architecture-dependent files | |
1806 | that are built and installed from the Emacs distribution. */); | |
e576cab4 JB |
1807 | |
1808 | DEFVAR_LISP ("data-directory", &Vdata_directory, | |
fdb82f93 PJ |
1809 | doc: /* Directory of machine-independent files that come with GNU Emacs. |
1810 | These are files intended for Emacs to use while it runs. */); | |
80856e74 | 1811 | |
35a2f4b8 | 1812 | DEFVAR_LISP ("doc-directory", &Vdoc_directory, |
fdb82f93 PJ |
1813 | doc: /* Directory containing the DOC file that comes with GNU Emacs. |
1814 | This is usually the same as data-directory. */); | |
35a2f4b8 | 1815 | |
ed61592a | 1816 | DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory, |
fdb82f93 PJ |
1817 | doc: /* For internal use by the build procedure only. |
1818 | This is the name of the directory in which the build procedure installed | |
1819 | Emacs's info files; the default value for Info-default-directory-list | |
1820 | includes this. */); | |
ed61592a JB |
1821 | Vconfigure_info_directory = build_string (PATH_INFO); |
1822 | ||
1e7ce61b | 1823 | DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory, |
b065672a CW |
1824 | doc: /* Directory of score files for games which come with GNU Emacs. |
1825 | If this variable is nil, then Emacs is unable to use a shared directory. */); | |
40b49d4b JB |
1826 | #ifdef DOS_NT |
1827 | Vshared_game_score_directory = Qnil; | |
1828 | #else | |
63789758 | 1829 | Vshared_game_score_directory = build_string (PATH_GAME); |
40b49d4b | 1830 | #endif |
b065672a | 1831 | |
8abd035b | 1832 | DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern, |
fdb82f93 PJ |
1833 | doc: /* Pattern for making names for temporary files. |
1834 | This is used by `call-process-region'. */); | |
0537ec48 | 1835 | /* This variable is initialized in init_callproc. */ |
8abd035b | 1836 | |
5990851d KL |
1837 | DEFVAR_LISP ("global-environment", &Vglobal_environment, |
1838 | doc: /* Global list of environment variables for subprocesses to inherit. | |
fdb82f93 | 1839 | Each element should be a string of the form ENVVARNAME=VALUE. |
5990851d KL |
1840 | |
1841 | The environment which Emacs inherits is placed in this variable when | |
1842 | Emacs starts. | |
1843 | ||
da8e8fc1 KL |
1844 | Some frames may have their own local list of environment variables in |
1845 | their 'environment parameter, which may override this global list; see | |
17ccbd91 KL |
1846 | `local-environment-variables' and `frame-with-environment'. See |
1847 | `process-environment' for a way to modify an environment variable on | |
1848 | all frames. | |
5990851d | 1849 | |
fdb82f93 PJ |
1850 | If multiple entries define the same variable, the first one always |
1851 | takes precedence. | |
5990851d | 1852 | |
776a24a1 DL |
1853 | Non-ASCII characters are encoded according to the initial value of |
1854 | `locale-coding-system', i.e. the elements must normally be decoded for use. | |
5990851d KL |
1855 | See `setenv' and `getenv'. */); |
1856 | ||
1857 | DEFVAR_LISP ("process-environment", &Vprocess_environment, | |
1858 | doc: /* List of overridden environment variables for subprocesses to inherit. | |
1859 | Each element should be a string of the form ENVVARNAME=VALUE. | |
1860 | ||
1861 | Entries in this list take precedence to those in `global-environment' | |
17ccbd91 KL |
1862 | or the frame-local environments. (See `local-environment-variables' |
1863 | and `frame-with-environment'.) Therefore, let-binding | |
1864 | `process-environment' is an easy way to temporarily change the value | |
1865 | of an environment variable, irrespective of where it comes from. To | |
1866 | use `process-environment' to remove an environment variable, include | |
1867 | only its name in the list, without "=VALUE". | |
5990851d KL |
1868 | |
1869 | This variable is set to nil when Emacs starts. | |
1870 | ||
1871 | If multiple entries define the same variable, the first one always | |
1872 | takes precedence. | |
1873 | ||
1874 | Non-ASCII characters are encoded according to the initial value of | |
1875 | `locale-coding-system', i.e. the elements must normally be decoded for | |
1876 | use. | |
1877 | ||
776a24a1 | 1878 | See `setenv' and `getenv'. */); |
86f5ca04 | 1879 | Vprocess_environment = Qnil; |
80856e74 JB |
1880 | |
1881 | #ifndef VMS | |
1882 | defsubr (&Scall_process); | |
83fa009c | 1883 | defsubr (&Sgetenv_internal); |
986ffb24 | 1884 | #endif |
e576cab4 | 1885 | defsubr (&Scall_process_region); |
f105f403 KL |
1886 | |
1887 | DEFVAR_LISP ("local-environment-variables", &Vlocal_environment_variables, | |
da8e8fc1 | 1888 | doc: /* Enable or disable frame-local environment variables. |
f105f403 | 1889 | If set to t, `getenv', `setenv' and subprocess creation functions use |
da8e8fc1 KL |
1890 | the local environment of the selected frame, ignoring |
1891 | `global-environment'. | |
f105f403 | 1892 | |
5990851d | 1893 | If set to nil, Emacs uses `global-environment' and ignores the |
da8e8fc1 | 1894 | frame-local environment. |
f105f403 | 1895 | |
5990851d | 1896 | Otherwise, `local-environment-variables' should be a list of variable |
da8e8fc1 | 1897 | names (represented by Lisp strings) to look up in the frame's |
17ccbd91 KL |
1898 | environment. The rest will come from `global-environment'. |
1899 | ||
1900 | The frame-local environment is stored in the 'environment frame | |
1901 | parameter. See `frame-with-environment'. */); | |
f105f403 | 1902 | Vlocal_environment_variables = Qnil; |
80856e74 | 1903 | } |
ab5796a9 MB |
1904 | |
1905 | /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95 | |
1906 | (do not change this comment) */ |