]> code.delx.au - gnu-emacs/blob - src/callproc.c
(SET_RAW_SYNTAX_ENTRY, SYNTAX_ENTRY): Adjusted for the change of
[gnu-emacs] / src / callproc.c
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <signal.h>
23 #include <errno.h>
24
25 #include <config.h>
26 #include <stdio.h>
27
28 extern int errno;
29 extern char *strerror ();
30
31 /* Define SIGCHLD as an alias for SIGCLD. */
32
33 #if !defined (SIGCHLD) && defined (SIGCLD)
34 #define SIGCHLD SIGCLD
35 #endif /* SIGCLD */
36
37 #include <sys/types.h>
38
39 #include <sys/file.h>
40 #ifdef USG5
41 #define INCLUDED_FCNTL
42 #include <fcntl.h>
43 #endif
44
45 #ifdef WINDOWSNT
46 #define NOMINMAX
47 #include <windows.h>
48 #include <stdlib.h> /* for proper declaration of environ */
49 #include <fcntl.h>
50 #include "w32.h"
51 #define _P_NOWAIT 1 /* from process.h */
52 #endif
53
54 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
55 #include "msdos.h"
56 #define INCLUDED_FCNTL
57 #include <fcntl.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <errno.h>
61 #endif /* MSDOS */
62
63 #ifndef O_RDONLY
64 #define O_RDONLY 0
65 #endif
66
67 #ifndef O_WRONLY
68 #define O_WRONLY 1
69 #endif
70
71 #include "lisp.h"
72 #include "commands.h"
73 #include "buffer.h"
74 #include "charset.h"
75 #include "coding.h"
76 #include <paths.h>
77 #include "process.h"
78 #include "syssignal.h"
79 #include "systty.h"
80
81 #ifdef VMS
82 extern noshare char **environ;
83 #else
84 extern char **environ;
85 #endif
86
87 #define max(a, b) ((a) > (b) ? (a) : (b))
88
89 #ifdef DOS_NT
90 /* When we are starting external processes we need to know whether they
91 take binary input (no conversion) or text input (\n is converted to
92 \r\n). Similar for output: if newlines are written as \r\n then it's
93 text process output, otherwise it's binary. */
94 Lisp_Object Vbinary_process_input;
95 Lisp_Object Vbinary_process_output;
96 #endif /* DOS_NT */
97
98 Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
99 Lisp_Object Vconfigure_info_directory;
100 Lisp_Object Vtemp_file_name_pattern;
101
102 Lisp_Object Vshell_file_name;
103
104 Lisp_Object Vprocess_environment;
105
106 #ifdef DOS_NT
107 Lisp_Object Qbuffer_file_type;
108 #endif /* DOS_NT */
109
110 /* True iff we are about to fork off a synchronous process or if we
111 are waiting for it. */
112 int synch_process_alive;
113
114 /* Nonzero => this is a string explaining death of synchronous subprocess. */
115 char *synch_process_death;
116
117 /* If synch_process_death is zero,
118 this is exit code of synchronous subprocess. */
119 int synch_process_retcode;
120
121 extern Lisp_Object Vdoc_file_name;
122 \f
123 /* Clean up when exiting Fcall_process.
124 On MSDOS, delete the temporary file on any kind of termination.
125 On Unix, kill the process and any children on termination by signal. */
126
127 /* Nonzero if this is termination due to exit. */
128 static int call_process_exited;
129
130 #ifndef VMS /* VMS version is in vmsproc.c. */
131
132 static Lisp_Object
133 call_process_kill (fdpid)
134 Lisp_Object fdpid;
135 {
136 close (XFASTINT (Fcar (fdpid)));
137 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
138 synch_process_alive = 0;
139 return Qnil;
140 }
141
142 Lisp_Object
143 call_process_cleanup (fdpid)
144 Lisp_Object fdpid;
145 {
146 #ifdef MSDOS
147 /* for MSDOS fdpid is really (fd . tempfile) */
148 register Lisp_Object file;
149 file = Fcdr (fdpid);
150 close (XFASTINT (Fcar (fdpid)));
151 if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0)
152 unlink (XSTRING (file)->data);
153 #else /* not MSDOS */
154 register int pid = XFASTINT (Fcdr (fdpid));
155
156
157 if (call_process_exited)
158 {
159 close (XFASTINT (Fcar (fdpid)));
160 return Qnil;
161 }
162
163 if (EMACS_KILLPG (pid, SIGINT) == 0)
164 {
165 int count = specpdl_ptr - specpdl;
166 record_unwind_protect (call_process_kill, fdpid);
167 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
168 immediate_quit = 1;
169 QUIT;
170 wait_for_termination (pid);
171 immediate_quit = 0;
172 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
173 message1 ("Waiting for process to die...done");
174 }
175 synch_process_alive = 0;
176 close (XFASTINT (Fcar (fdpid)));
177 #endif /* not MSDOS */
178 return Qnil;
179 }
180
181 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
182 "Call PROGRAM synchronously in separate process.\n\
183 The program's input comes from file INFILE (nil means `/dev/null').\n\
184 Insert output in BUFFER before point; t means current buffer;\n\
185 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
186 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
187 REAL-BUFFER says what to do with standard output, as above,\n\
188 while STDERR-FILE says what to do with standard error in the child.\n\
189 STDERR-FILE may be nil (discard standard error output),\n\
190 t (mix it with ordinary output), or a file name string.\n\
191 \n\
192 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
193 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
194 \n\
195 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
196 Otherwise it waits for PROGRAM to terminate\n\
197 and returns a numeric exit status or a signal description string.\n\
198 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
199 (nargs, args)
200 int nargs;
201 register Lisp_Object *args;
202 {
203 Lisp_Object infile, buffer, current_dir, display, path;
204 int fd[2];
205 int filefd;
206 register int pid;
207 char buf[16384];
208 char *bufptr = buf;
209 int bufsize = 16384;
210 int count = specpdl_ptr - specpdl;
211 register unsigned char **new_argv
212 = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
213 struct buffer *old = current_buffer;
214 /* File to use for stderr in the child.
215 t means use same as standard output. */
216 Lisp_Object error_file;
217 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
218 char *outf, *tempfile;
219 int outfilefd;
220 #endif
221 #if 0
222 int mask;
223 #endif
224 struct coding_system process_coding; /* coding-system of process output */
225 struct coding_system argument_coding; /* coding-system of arguments */
226
227 CHECK_STRING (args[0], 0);
228
229 error_file = Qt;
230
231 #ifndef subprocesses
232 /* Without asynchronous processes we cannot have BUFFER == 0. */
233 if (nargs >= 3 && INTEGERP (args[2]))
234 error ("Operating system cannot handle asynchronous subprocesses");
235 #endif /* subprocesses */
236
237 /* Decide the coding-system for giving arguments and reading process
238 output. */
239 {
240 Lisp_Object val, *args2;
241 /* Qt denotes that we have not yet called Ffind_coding_system. */
242 Lisp_Object coding_systems = Qt;
243 int i;
244
245 /* If arguments are supplied, we may have to encode them. */
246 if (nargs >= 5)
247 {
248 if (NILP (val = Vcoding_system_for_write))
249 {
250 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
251 args2[0] = Qcall_process;
252 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
253 coding_systems = Ffind_coding_system (nargs + 1, args2);
254 val = CONSP (coding_systems) ? XCONS (coding_systems)->cdr : Qnil;
255 }
256 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
257 }
258
259 /* If BUFFER is nil, we must read process output once and then
260 discard it, so setup coding system but with nil. If BUFFER is
261 an integer, we can discard it without reading. */
262 if (nargs < 3 || NILP (args[2]))
263 setup_coding_system (Qnil, &process_coding);
264 else if (!INTEGERP (args[2]))
265 {
266 if (NILP (val = Vcoding_system_for_read))
267 {
268 if (!EQ (coding_systems, Qt))
269 {
270 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
271 args2[0] = Qcall_process;
272 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
273 coding_systems = Ffind_coding_system (nargs + 1, args2);
274 }
275 val = CONSP (coding_systems) ? XCONS (coding_systems)->car : Qnil;
276 }
277 setup_coding_system (Fcheck_coding_system (val), &process_coding);
278 }
279 }
280
281 if (nargs >= 2 && ! NILP (args[1]))
282 {
283 infile = Fexpand_file_name (args[1], current_buffer->directory);
284 CHECK_STRING (infile, 1);
285 }
286 else
287 infile = build_string (NULL_DEVICE);
288
289 if (nargs >= 3)
290 {
291 buffer = args[2];
292
293 /* If BUFFER is a list, its meaning is
294 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
295 if (CONSP (buffer))
296 {
297 if (CONSP (XCONS (buffer)->cdr))
298 {
299 Lisp_Object stderr_file;
300 stderr_file = XCONS (XCONS (buffer)->cdr)->car;
301
302 if (NILP (stderr_file) || EQ (Qt, stderr_file))
303 error_file = stderr_file;
304 else
305 error_file = Fexpand_file_name (stderr_file, Qnil);
306 }
307
308 buffer = XCONS (buffer)->car;
309 }
310
311 if (!(EQ (buffer, Qnil)
312 || EQ (buffer, Qt)
313 || XFASTINT (buffer) == 0))
314 {
315 Lisp_Object spec_buffer;
316 spec_buffer = buffer;
317 buffer = Fget_buffer (buffer);
318 /* Mention the buffer name for a better error message. */
319 if (NILP (buffer))
320 CHECK_BUFFER (spec_buffer, 2);
321 CHECK_BUFFER (buffer, 2);
322 }
323 }
324 else
325 buffer = Qnil;
326
327 /* Make sure that the child will be able to chdir to the current
328 buffer's current directory, or its unhandled equivalent. We
329 can't just have the child check for an error when it does the
330 chdir, since it's in a vfork.
331
332 We have to GCPRO around this because Fexpand_file_name,
333 Funhandled_file_name_directory, and Ffile_accessible_directory_p
334 might call a file name handling function. The argument list is
335 protected by the caller, so all we really have to worry about is
336 buffer. */
337 {
338 struct gcpro gcpro1, gcpro2, gcpro3;
339
340 current_dir = current_buffer->directory;
341
342 GCPRO3 (infile, buffer, current_dir);
343
344 current_dir
345 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
346 Qnil);
347 if (NILP (Ffile_accessible_directory_p (current_dir)))
348 report_file_error ("Setting current directory",
349 Fcons (current_buffer->directory, Qnil));
350
351 UNGCPRO;
352 }
353
354 display = nargs >= 4 ? args[3] : Qnil;
355
356 filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
357 if (filefd < 0)
358 {
359 report_file_error ("Opening process input file", Fcons (infile, Qnil));
360 }
361 /* Search for program; barf if not found. */
362 {
363 struct gcpro gcpro1;
364
365 GCPRO1 (current_dir);
366 openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
367 UNGCPRO;
368 }
369 if (NILP (path))
370 {
371 close (filefd);
372 report_file_error ("Searching for program", Fcons (args[0], Qnil));
373 }
374 new_argv[0] = XSTRING (path)->data;
375 {
376 register int i;
377 for (i = 4; i < nargs; i++)
378 {
379 CHECK_STRING (args[i], i);
380 if (argument_coding.type == coding_type_no_conversion)
381 new_argv[i - 3] = XSTRING (args[i])->data;
382 else
383 {
384 /* We must encode the arguments. */
385 int size = encoding_buffer_size (&argument_coding,
386 XSTRING (args[i])->size);
387 int produced, dummy;
388
389 new_argv[i - 3] = (unsigned char *) alloca (size);
390 produced = encode_coding (&argument_coding,
391 XSTRING (args[i])->data, new_argv[i - 3],
392 XSTRING (args[i])->size, size, &dummy);
393 new_argv[i - 3][produced] = 0;
394 }
395 }
396 new_argv[i - 3] = 0;
397 }
398
399 #ifdef MSDOS /* MW, July 1993 */
400 if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
401 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
402 else
403 {
404 tempfile = alloca (20);
405 *tempfile = '\0';
406 }
407 dostounix_filename (tempfile);
408 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
409 strcat (tempfile, "/");
410 strcat (tempfile, "detmp.XXX");
411 mktemp (tempfile);
412
413 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
414 if (outfilefd < 0)
415 {
416 close (filefd);
417 report_file_error ("Opening process output file",
418 Fcons (build_string (tempfile), Qnil));
419 }
420 fd[0] = filefd;
421 fd[1] = outfilefd;
422 #endif /* MSDOS */
423
424 if (INTEGERP (buffer))
425 fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
426 else
427 {
428 #ifndef MSDOS
429 pipe (fd);
430 #endif
431 #if 0
432 /* Replaced by close_process_descs */
433 set_exclusive_use (fd[0]);
434 #endif
435 }
436
437 {
438 /* child_setup must clobber environ in systems with true vfork.
439 Protect it from permanent change. */
440 register char **save_environ = environ;
441 register int fd1 = fd[1];
442 int fd_error = fd1;
443
444 #if 0 /* Some systems don't have sigblock. */
445 mask = sigblock (sigmask (SIGCHLD));
446 #endif
447
448 /* Record that we're about to create a synchronous process. */
449 synch_process_alive = 1;
450
451 /* These vars record information from process termination.
452 Clear them now before process can possibly terminate,
453 to avoid timing error if process terminates soon. */
454 synch_process_death = 0;
455 synch_process_retcode = 0;
456
457 if (NILP (error_file))
458 fd_error = open (NULL_DEVICE, O_WRONLY);
459 else if (STRINGP (error_file))
460 {
461 #ifdef DOS_NT
462 fd_error = open (XSTRING (error_file)->data,
463 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
464 S_IREAD | S_IWRITE);
465 #else /* not DOS_NT */
466 fd_error = creat (XSTRING (error_file)->data, 0666);
467 #endif /* not DOS_NT */
468 }
469
470 if (fd_error < 0)
471 {
472 close (filefd);
473 if (fd[0] != filefd)
474 close (fd[0]);
475 if (fd1 >= 0)
476 close (fd1);
477 #ifdef MSDOS
478 unlink (tempfile);
479 #endif
480 report_file_error ("Cannot redirect stderr",
481 Fcons ((NILP (error_file)
482 ? build_string (NULL_DEVICE) : error_file),
483 Qnil));
484 }
485 #ifdef MSDOS /* MW, July 1993 */
486 /* ??? Someone who knows MSDOG needs to check whether this properly
487 closes all descriptors that it opens.
488
489 Note that run_msdos_command() actually returns the child process
490 exit status, not its PID, so we assign it to `synch_process_retcode'
491 below. */
492 pid = run_msdos_command (new_argv, current_dir,
493 filefd, outfilefd, fd_error);
494
495 /* Record that the synchronous process exited and note its
496 termination status. */
497 synch_process_alive = 0;
498 synch_process_retcode = pid;
499 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
500 synch_process_death = strerror(errno);
501
502 close (outfilefd);
503 if (fd_error != outfilefd)
504 close (fd_error);
505 fd1 = -1; /* No harm in closing that one! */
506 /* Since CRLF is converted to LF within `decode_coding', we can
507 always open a file with binary mode. */
508 fd[0] = open (tempfile, O_BINARY);
509 if (fd[0] < 0)
510 {
511 unlink (tempfile);
512 close (filefd);
513 report_file_error ("Cannot re-open temporary file", Qnil);
514 }
515 #else /* not MSDOS */
516 #ifdef WINDOWSNT
517 pid = child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
518 #else /* not WINDOWSNT */
519 pid = vfork ();
520
521 if (pid == 0)
522 {
523 if (fd[0] >= 0)
524 close (fd[0]);
525 #if defined(USG) && !defined(BSD_PGRPS)
526 setpgrp ();
527 #else
528 setpgrp (pid, pid);
529 #endif /* USG */
530 child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
531 }
532 #endif /* not WINDOWSNT */
533
534 /* The MSDOS case did this already. */
535 if (fd_error >= 0)
536 close (fd_error);
537 #endif /* not MSDOS */
538
539 environ = save_environ;
540
541 /* Close most of our fd's, but not fd[0]
542 since we will use that to read input from. */
543 close (filefd);
544 if (fd1 >= 0 && fd1 != fd_error)
545 close (fd1);
546 }
547
548 if (pid < 0)
549 {
550 if (fd[0] >= 0)
551 close (fd[0]);
552 report_file_error ("Doing vfork", Qnil);
553 }
554
555 if (INTEGERP (buffer))
556 {
557 if (fd[0] >= 0)
558 close (fd[0]);
559 #ifndef subprocesses
560 /* If Emacs has been built with asynchronous subprocess support,
561 we don't need to do this, I think because it will then have
562 the facilities for handling SIGCHLD. */
563 wait_without_blocking ();
564 #endif /* subprocesses */
565 return Qnil;
566 }
567
568 /* Enable sending signal if user quits below. */
569 call_process_exited = 0;
570
571 #ifdef MSDOS
572 /* MSDOS needs different cleanup information. */
573 record_unwind_protect (call_process_cleanup,
574 Fcons (make_number (fd[0]), build_string (tempfile)));
575 #else
576 record_unwind_protect (call_process_cleanup,
577 Fcons (make_number (fd[0]), make_number (pid)));
578 #endif /* not MSDOS */
579
580
581 if (BUFFERP (buffer))
582 Fset_buffer (buffer);
583
584 immediate_quit = 1;
585 QUIT;
586
587 {
588 register int nread;
589 int first = 1;
590 int total_read = 0;
591
592 while (1)
593 {
594 /* Repeatedly read until we've filled as much as possible
595 of the buffer size we have. But don't read
596 less than 1024--save that for the next bufferful. */
597
598 nread = process_coding.carryover_size; /* This value is initially 0. */
599 while (nread < bufsize - 1024)
600 {
601 int this_read
602 = read (fd[0], bufptr + nread, bufsize - nread);
603
604 if (this_read < 0)
605 goto give_up;
606
607 if (this_read == 0)
608 goto give_up_1;
609
610 nread += this_read;
611 }
612
613 give_up_1:
614
615 /* Now NREAD is the total amount of data in the buffer. */
616 if (nread == 0)
617 /* Here, just tell decode_coding that we are processing the
618 last block. We break the loop after decoding. */
619 process_coding.last_block = 1;
620
621 immediate_quit = 0;
622 total_read += nread;
623
624 if (!NILP (buffer))
625 {
626 if (process_coding.type == coding_type_no_conversion)
627 insert (bufptr, nread);
628 else
629 { /* We have to decode the input. */
630 int size = decoding_buffer_size (&process_coding, bufsize);
631 char *decoding_buf = get_conversion_buffer (size);
632 int dummy;
633
634 nread = decode_coding (&process_coding, bufptr, decoding_buf,
635 nread, size, &dummy);
636 if (nread > 0)
637 insert (decoding_buf, nread);
638 }
639 }
640
641 if (process_coding.last_block)
642 break;
643
644 /* Make the buffer bigger as we continue to read more data,
645 but not past 64k. */
646 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
647 {
648 bufsize *= 2;
649 bufptr = (char *) alloca (bufsize);
650 }
651
652 if (!NILP (buffer) && process_coding.carryover_size > 0)
653 /* We have carryover in the last decoding. It should be
654 processed again after reading more data. */
655 bcopy (process_coding.carryover, bufptr,
656 process_coding.carryover_size);
657
658 if (!NILP (display) && INTERACTIVE)
659 {
660 if (first)
661 prepare_menu_bars ();
662 first = 0;
663 redisplay_preserve_echo_area ();
664 }
665 immediate_quit = 1;
666 QUIT;
667 }
668 give_up: ;
669 }
670
671 /* Wait for it to terminate, unless it already has. */
672 wait_for_termination (pid);
673
674 immediate_quit = 0;
675
676 set_buffer_internal (old);
677
678 /* Don't kill any children that the subprocess may have left behind
679 when exiting. */
680 call_process_exited = 1;
681
682 unbind_to (count, Qnil);
683
684 if (synch_process_death)
685 return build_string (synch_process_death);
686 return make_number (synch_process_retcode);
687 }
688 #endif
689 \f
690 static Lisp_Object
691 delete_temp_file (name)
692 Lisp_Object name;
693 {
694 /* Use Fdelete_file (indirectly) because that runs a file name handler.
695 We did that when writing the file, so we should do so when deleting. */
696 internal_delete_file (name);
697 }
698
699 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
700 3, MANY, 0,
701 "Send text from START to END to a synchronous process running PROGRAM.\n\
702 Delete the text if fourth arg DELETE is non-nil.\n\
703 \n\
704 Insert output in BUFFER before point; t means current buffer;\n\
705 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
706 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
707 REAL-BUFFER says what to do with standard output, as above,\n\
708 while STDERR-FILE says what to do with standard error in the child.\n\
709 STDERR-FILE may be nil (discard standard error output),\n\
710 t (mix it with ordinary output), or a file name string.\n\
711 \n\
712 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
713 Remaining args are passed to PROGRAM at startup as command args.\n\
714 \n\
715 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
716 Otherwise it waits for PROGRAM to terminate\n\
717 and returns a numeric exit status or a signal description string.\n\
718 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
719 (nargs, args)
720 int nargs;
721 register Lisp_Object *args;
722 {
723 struct gcpro gcpro1;
724 Lisp_Object filename_string;
725 register Lisp_Object start, end;
726 int count = specpdl_ptr - specpdl;
727 /* Qt denotes that we have not yet called Ffind_coding_system. */
728 Lisp_Object coding_systems = Qt;
729 Lisp_Object val, *args2;
730 int i;
731 #ifdef DOS_NT
732 char *tempfile;
733 char *outf = '\0';
734
735 if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
736 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
737 else
738 {
739 tempfile = alloca (20);
740 *tempfile = '\0';
741 }
742 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
743 strcat (tempfile, "/");
744 if ('/' == DIRECTORY_SEP)
745 dostounix_filename (tempfile);
746 else
747 unixtodos_filename (tempfile);
748 #ifdef WINDOWSNT
749 strcat (tempfile, "emXXXXXX");
750 #else
751 strcat (tempfile, "detmp.XXX");
752 #endif
753 #else /* not DOS_NT */
754 char *tempfile = (char *) alloca (XSTRING (Vtemp_file_name_pattern)->size + 1);
755 bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile,
756 XSTRING (Vtemp_file_name_pattern)->size + 1);
757 #endif /* not DOS_NT */
758
759 mktemp (tempfile);
760
761 filename_string = build_string (tempfile);
762 GCPRO1 (filename_string);
763 start = args[0];
764 end = args[1];
765 /* Decide coding-system of the contents of the temporary file. */
766 #ifdef DOS_NT
767 specbind (Qbuffer_file_type, Vbinary_process_input);
768 if (NILP (Vbinary_process_input))
769 val = Qnil;
770 else
771 #endif
772 if (NILP (val = Vcoding_system_for_write))
773 {
774 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
775 args2[0] = Qcall_process_region;
776 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
777 coding_systems = Ffind_coding_system (nargs + 1, args2);
778 val = CONSP (coding_systems) ? XCONS (coding_systems)->cdr : Qnil;
779 }
780 specbind (intern ("coding-system-for-write"), val);
781 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil);
782
783 #ifdef DOS_NT
784 if (NILP (Vbinary_process_input))
785 val = Qnil;
786 else
787 #endif
788 if (NILP (val = Vcoding_system_for_read))
789 {
790 if (EQ (coding_systems, Qt))
791 {
792 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
793 args2[0] = Qcall_process_region;
794 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
795 coding_systems = Ffind_coding_system (nargs + 1, args2);
796 }
797 val = CONSP (coding_systems) ? XCONS (coding_systems)->car : Qnil;
798 }
799 specbind (intern ("coding-system-for-read"), val);
800
801 record_unwind_protect (delete_temp_file, filename_string);
802
803 if (!NILP (args[3]))
804 Fdelete_region (start, end);
805
806 args[3] = filename_string;
807
808 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs - 2, args + 2)));
809 }
810 \f
811 #ifndef VMS /* VMS version is in vmsproc.c. */
812
813 /* This is the last thing run in a newly forked inferior
814 either synchronous or asynchronous.
815 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
816 Initialize inferior's priority, pgrp, connected dir and environment.
817 then exec another program based on new_argv.
818
819 This function may change environ for the superior process.
820 Therefore, the superior process must save and restore the value
821 of environ around the vfork and the call to this function.
822
823 ENV is the environment for the subprocess.
824
825 SET_PGRP is nonzero if we should put the subprocess into a separate
826 process group.
827
828 CURRENT_DIR is an elisp string giving the path of the current
829 directory the subprocess should have. Since we can't really signal
830 a decent error from within the child, this should be verified as an
831 executable directory by the parent. */
832
833 child_setup (in, out, err, new_argv, set_pgrp, current_dir)
834 int in, out, err;
835 register char **new_argv;
836 int set_pgrp;
837 Lisp_Object current_dir;
838 {
839 #ifdef MSDOS
840 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
841 instead. */
842 #else /* not MSDOS */
843 char **env;
844 char *pwd_var;
845 #ifdef WINDOWSNT
846 int cpid;
847 HANDLE handles[3];
848 #endif /* WINDOWSNT */
849
850 int pid = getpid ();
851
852 #ifdef SET_EMACS_PRIORITY
853 {
854 extern int emacs_priority;
855
856 if (emacs_priority < 0)
857 nice (- emacs_priority);
858 }
859 #endif
860
861 #ifdef subprocesses
862 /* Close Emacs's descriptors that this process should not have. */
863 close_process_descs ();
864 #endif
865 close_load_descs ();
866
867 /* Note that use of alloca is always safe here. It's obvious for systems
868 that do not have true vfork or that have true (stack) alloca.
869 If using vfork and C_ALLOCA it is safe because that changes
870 the superior's static variables as if the superior had done alloca
871 and will be cleaned up in the usual way. */
872 {
873 register char *temp;
874 register int i;
875
876 i = XSTRING (current_dir)->size;
877 pwd_var = (char *) alloca (i + 6);
878 temp = pwd_var + 4;
879 bcopy ("PWD=", pwd_var, 4);
880 bcopy (XSTRING (current_dir)->data, temp, i);
881 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
882 temp[i] = 0;
883
884 /* We can't signal an Elisp error here; we're in a vfork. Since
885 the callers check the current directory before forking, this
886 should only return an error if the directory's permissions
887 are changed between the check and this chdir, but we should
888 at least check. */
889 if (chdir (temp) < 0)
890 _exit (errno);
891
892 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
893 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
894 temp[--i] = 0;
895 }
896
897 /* Set `env' to a vector of the strings in Vprocess_environment. */
898 {
899 register Lisp_Object tem;
900 register char **new_env;
901 register int new_length;
902
903 new_length = 0;
904 for (tem = Vprocess_environment;
905 CONSP (tem) && STRINGP (XCONS (tem)->car);
906 tem = XCONS (tem)->cdr)
907 new_length++;
908
909 /* new_length + 2 to include PWD and terminating 0. */
910 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
911
912 /* If we have a PWD envvar, pass one down,
913 but with corrected value. */
914 if (getenv ("PWD"))
915 *new_env++ = pwd_var;
916
917 /* Copy the Vprocess_environment strings into new_env. */
918 for (tem = Vprocess_environment;
919 CONSP (tem) && STRINGP (XCONS (tem)->car);
920 tem = XCONS (tem)->cdr)
921 {
922 char **ep = env;
923 char *string = (char *) XSTRING (XCONS (tem)->car)->data;
924 /* See if this string duplicates any string already in the env.
925 If so, don't put it in.
926 When an env var has multiple definitions,
927 we keep the definition that comes first in process-environment. */
928 for (; ep != new_env; ep++)
929 {
930 char *p = *ep, *q = string;
931 while (1)
932 {
933 if (*q == 0)
934 /* The string is malformed; might as well drop it. */
935 goto duplicate;
936 if (*q != *p)
937 break;
938 if (*q == '=')
939 goto duplicate;
940 p++, q++;
941 }
942 }
943 *new_env++ = string;
944 duplicate: ;
945 }
946 *new_env = 0;
947 }
948 #ifdef WINDOWSNT
949 prepare_standard_handles (in, out, err, handles);
950 #else /* not WINDOWSNT */
951 /* Make sure that in, out, and err are not actually already in
952 descriptors zero, one, or two; this could happen if Emacs is
953 started with its standard in, out, or error closed, as might
954 happen under X. */
955 {
956 int oin = in, oout = out;
957
958 /* We have to avoid relocating the same descriptor twice! */
959
960 in = relocate_fd (in, 3);
961
962 if (out == oin)
963 out = in;
964 else
965 out = relocate_fd (out, 3);
966
967 if (err == oin)
968 err = in;
969 else if (err == oout)
970 err = out;
971 else
972 err = relocate_fd (err, 3);
973 }
974
975 close (0);
976 close (1);
977 close (2);
978
979 dup2 (in, 0);
980 dup2 (out, 1);
981 dup2 (err, 2);
982 close (in);
983 close (out);
984 close (err);
985 #endif /* not WINDOWSNT */
986
987 #if defined(USG) && !defined(BSD_PGRPS)
988 #ifndef SETPGRP_RELEASES_CTTY
989 setpgrp (); /* No arguments but equivalent in this case */
990 #endif
991 #else
992 setpgrp (pid, pid);
993 #endif /* USG */
994 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
995 EMACS_SET_TTY_PGRP (0, &pid);
996
997 #ifdef vipc
998 something missing here;
999 #endif /* vipc */
1000
1001 #ifdef WINDOWSNT
1002 /* Spawn the child. (See ntproc.c:Spawnve). */
1003 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1004 if (cpid == -1)
1005 /* An error occurred while trying to spawn the process. */
1006 report_file_error ("Spawning child process", Qnil);
1007 reset_standard_handles (in, out, err, handles);
1008 return cpid;
1009 #else /* not WINDOWSNT */
1010 /* execvp does not accept an environment arg so the only way
1011 to pass this environment is to set environ. Our caller
1012 is responsible for restoring the ambient value of environ. */
1013 environ = env;
1014 execvp (new_argv[0], new_argv);
1015
1016 write (1, "Can't exec program: ", 20);
1017 write (1, new_argv[0], strlen (new_argv[0]));
1018 write (1, "\n", 1);
1019 _exit (1);
1020 #endif /* not WINDOWSNT */
1021 #endif /* not MSDOS */
1022 }
1023
1024 /* Move the file descriptor FD so that its number is not less than MIN.
1025 If the file descriptor is moved at all, the original is freed. */
1026 int
1027 relocate_fd (fd, min)
1028 int fd, min;
1029 {
1030 if (fd >= min)
1031 return fd;
1032 else
1033 {
1034 int new = dup (fd);
1035 if (new == -1)
1036 {
1037 char *message1 = "Error while setting up child: ";
1038 char *errmessage = strerror (errno);
1039 char *message2 = "\n";
1040 write (2, message1, strlen (message1));
1041 write (2, errmessage, strlen (errmessage));
1042 write (2, message2, strlen (message2));
1043 _exit (1);
1044 }
1045 /* Note that we hold the original FD open while we recurse,
1046 to guarantee we'll get a new FD if we need it. */
1047 new = relocate_fd (new, min);
1048 close (fd);
1049 return new;
1050 }
1051 }
1052
1053 static int
1054 getenv_internal (var, varlen, value, valuelen)
1055 char *var;
1056 int varlen;
1057 char **value;
1058 int *valuelen;
1059 {
1060 Lisp_Object scan;
1061
1062 for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
1063 {
1064 Lisp_Object entry;
1065
1066 entry = XCONS (scan)->car;
1067 if (STRINGP (entry)
1068 && XSTRING (entry)->size > varlen
1069 && XSTRING (entry)->data[varlen] == '='
1070 #ifdef WINDOWSNT
1071 /* NT environment variables are case insensitive. */
1072 && ! strnicmp (XSTRING (entry)->data, var, varlen)
1073 #else /* not WINDOWSNT */
1074 && ! bcmp (XSTRING (entry)->data, var, varlen)
1075 #endif /* not WINDOWSNT */
1076 )
1077 {
1078 *value = (char *) XSTRING (entry)->data + (varlen + 1);
1079 *valuelen = XSTRING (entry)->size - (varlen + 1);
1080 return 1;
1081 }
1082 }
1083
1084 return 0;
1085 }
1086
1087 DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
1088 "Return the value of environment variable VAR, as a string.\n\
1089 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1090 This function consults the variable ``process-environment'' for its value.")
1091 (var)
1092 Lisp_Object var;
1093 {
1094 char *value;
1095 int valuelen;
1096
1097 CHECK_STRING (var, 0);
1098 if (getenv_internal (XSTRING (var)->data, XSTRING (var)->size,
1099 &value, &valuelen))
1100 return make_string (value, valuelen);
1101 else
1102 return Qnil;
1103 }
1104
1105 /* A version of getenv that consults process_environment, easily
1106 callable from C. */
1107 char *
1108 egetenv (var)
1109 char *var;
1110 {
1111 char *value;
1112 int valuelen;
1113
1114 if (getenv_internal (var, strlen (var), &value, &valuelen))
1115 return value;
1116 else
1117 return 0;
1118 }
1119
1120 #endif /* not VMS */
1121 \f
1122 /* This is run before init_cmdargs. */
1123
1124 init_callproc_1 ()
1125 {
1126 char *data_dir = egetenv ("EMACSDATA");
1127 char *doc_dir = egetenv ("EMACSDOC");
1128
1129 Vdata_directory
1130 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1131 : PATH_DATA));
1132 Vdoc_directory
1133 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1134 : PATH_DOC));
1135
1136 /* Check the EMACSPATH environment variable, defaulting to the
1137 PATH_EXEC path from paths.h. */
1138 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1139 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1140 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1141 }
1142
1143 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1144
1145 init_callproc ()
1146 {
1147 char *data_dir = egetenv ("EMACSDATA");
1148
1149 register char * sh;
1150 Lisp_Object tempdir;
1151
1152 if (initialized && !NILP (Vinstallation_directory))
1153 {
1154 /* Add to the path the lib-src subdir of the installation dir. */
1155 Lisp_Object tem;
1156 tem = Fexpand_file_name (build_string ("lib-src"),
1157 Vinstallation_directory);
1158 if (NILP (Fmember (tem, Vexec_path)))
1159 {
1160 #ifndef DOS_NT
1161 /* MSDOS uses wrapped binaries, so don't do this. */
1162 Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
1163 Vexec_directory = Ffile_name_as_directory (tem);
1164 #endif /* not DOS_NT */
1165 }
1166
1167 /* Maybe use ../etc as well as ../lib-src. */
1168 if (data_dir == 0)
1169 {
1170 tem = Fexpand_file_name (build_string ("etc"),
1171 Vinstallation_directory);
1172 Vdoc_directory = Ffile_name_as_directory (tem);
1173 }
1174 }
1175
1176 /* Look for the files that should be in etc. We don't use
1177 Vinstallation_directory, because these files are never installed
1178 near the executable, and they are never in the build
1179 directory when that's different from the source directory.
1180
1181 Instead, if these files are not in the nominal place, we try the
1182 source directory. */
1183 if (data_dir == 0)
1184 {
1185 Lisp_Object tem, tem1, newdir;
1186
1187 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1188 tem1 = Ffile_exists_p (tem);
1189 if (NILP (tem1))
1190 {
1191 newdir = Fexpand_file_name (build_string ("../etc/"),
1192 build_string (PATH_DUMPLOADSEARCH));
1193 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1194 tem1 = Ffile_exists_p (tem);
1195 if (!NILP (tem1))
1196 Vdata_directory = newdir;
1197 }
1198 }
1199
1200 tempdir = Fdirectory_file_name (Vexec_directory);
1201 if (access (XSTRING (tempdir)->data, 0) < 0)
1202 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1203 Vexec_directory);
1204
1205 tempdir = Fdirectory_file_name (Vdata_directory);
1206 if (access (XSTRING (tempdir)->data, 0) < 0)
1207 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1208 Vdata_directory);
1209
1210 #ifdef VMS
1211 Vshell_file_name = build_string ("*dcl*");
1212 #else
1213 sh = (char *) getenv ("SHELL");
1214 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1215 #endif
1216
1217 #ifdef VMS
1218 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1219 #else
1220 if (getenv ("TMPDIR"))
1221 {
1222 char *dir = getenv ("TMPDIR");
1223 Vtemp_file_name_pattern
1224 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1225 build_string (dir));
1226 }
1227 else
1228 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1229 #endif
1230 }
1231
1232 set_process_environment ()
1233 {
1234 register char **envp;
1235
1236 Vprocess_environment = Qnil;
1237 #ifndef CANNOT_DUMP
1238 if (initialized)
1239 #endif
1240 for (envp = environ; *envp; envp++)
1241 Vprocess_environment = Fcons (build_string (*envp),
1242 Vprocess_environment);
1243 }
1244
1245 syms_of_callproc ()
1246 {
1247 #ifdef DOS_NT
1248 Qbuffer_file_type = intern ("buffer-file-type");
1249 staticpro (&Qbuffer_file_type);
1250
1251 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input,
1252 "*If non-nil then new subprocesses are assumed to take binary input.");
1253 Vbinary_process_input = Qnil;
1254
1255 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output,
1256 "*If non-nil then new subprocesses are assumed to produce binary output.");
1257 Vbinary_process_output = Qnil;
1258 #endif /* DOS_NT */
1259
1260 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1261 "*File name to load inferior shells from.\n\
1262 Initialized from the SHELL environment variable.");
1263
1264 DEFVAR_LISP ("exec-path", &Vexec_path,
1265 "*List of directories to search programs to run in subprocesses.\n\
1266 Each element is a string (directory name) or nil (try default directory).");
1267
1268 DEFVAR_LISP ("exec-directory", &Vexec_directory,
1269 "Directory of architecture-dependent files that come with GNU Emacs,\n\
1270 especially executable programs intended for Emacs to invoke.");
1271
1272 DEFVAR_LISP ("data-directory", &Vdata_directory,
1273 "Directory of architecture-independent files that come with GNU Emacs,\n\
1274 intended for Emacs to use.");
1275
1276 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1277 "Directory containing the DOC file that comes with GNU Emacs.\n\
1278 This is usually the same as data-directory.");
1279
1280 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1281 "For internal use by the build procedure only.\n\
1282 This is the name of the directory in which the build procedure installed\n\
1283 Emacs's info files; the default value for Info-default-directory-list\n\
1284 includes this.");
1285 Vconfigure_info_directory = build_string (PATH_INFO);
1286
1287 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
1288 "Pattern for making names for temporary files.\n\
1289 This is used by `call-process-region'.");
1290 /* The real initialization is when we start again. */
1291 Vtemp_file_name_pattern = Qnil;
1292
1293 DEFVAR_LISP ("process-environment", &Vprocess_environment,
1294 "List of environment variables for subprocesses to inherit.\n\
1295 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1296 The environment which Emacs inherits is placed in this variable\n\
1297 when Emacs starts.");
1298
1299 #ifndef VMS
1300 defsubr (&Scall_process);
1301 defsubr (&Sgetenv);
1302 #endif
1303 defsubr (&Scall_process_region);
1304 }