1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
29 extern char *strerror ();
31 /* Define SIGCHLD as an alias for SIGCLD. */
33 #if !defined (SIGCHLD) && defined (SIGCLD)
34 #define SIGCHLD SIGCLD
37 #include <sys/types.h>
41 #define INCLUDED_FCNTL
48 #include <stdlib.h> /* for proper declaration of environ */
51 #define _P_NOWAIT 1 /* from process.h */
54 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
56 #define INCLUDED_FCNTL
59 #include <sys/param.h>
78 #include "syssignal.h"
82 extern noshare
char **environ
;
84 extern char **environ
;
87 #define max(a, b) ((a) > (b) ? (a) : (b))
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
;
98 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
99 Lisp_Object Vconfigure_info_directory
;
100 Lisp_Object Vtemp_file_name_pattern
;
102 Lisp_Object Vshell_file_name
;
104 Lisp_Object Vprocess_environment
;
107 Lisp_Object Qbuffer_file_type
;
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
;
114 /* Nonzero => this is a string explaining death of synchronous subprocess. */
115 char *synch_process_death
;
117 /* If synch_process_death is zero,
118 this is exit code of synchronous subprocess. */
119 int synch_process_retcode
;
121 extern Lisp_Object Vdoc_file_name
;
123 extern Lisp_Object Vfile_name_coding_system
;
125 /* Clean up when exiting Fcall_process.
126 On MSDOS, delete the temporary file on any kind of termination.
127 On Unix, kill the process and any children on termination by signal. */
129 /* Nonzero if this is termination due to exit. */
130 static int call_process_exited
;
132 #ifndef VMS /* VMS version is in vmsproc.c. */
135 call_process_kill (fdpid
)
138 close (XFASTINT (Fcar (fdpid
)));
139 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
140 synch_process_alive
= 0;
145 call_process_cleanup (fdpid
)
149 /* for MSDOS fdpid is really (fd . tempfile) */
150 register Lisp_Object file
;
152 close (XFASTINT (Fcar (fdpid
)));
153 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
154 unlink (XSTRING (file
)->data
);
155 #else /* not MSDOS */
156 register int pid
= XFASTINT (Fcdr (fdpid
));
159 if (call_process_exited
)
161 close (XFASTINT (Fcar (fdpid
)));
165 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
167 int count
= specpdl_ptr
- specpdl
;
168 record_unwind_protect (call_process_kill
, fdpid
);
169 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
172 wait_for_termination (pid
);
174 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
175 message1 ("Waiting for process to die...done");
177 synch_process_alive
= 0;
178 close (XFASTINT (Fcar (fdpid
)));
179 #endif /* not MSDOS */
183 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
184 "Call PROGRAM synchronously in separate process.\n\
185 The program's input comes from file INFILE (nil means `/dev/null').\n\
186 Insert output in BUFFER before point; t means current buffer;\n\
187 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
188 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
189 REAL-BUFFER says what to do with standard output, as above,\n\
190 while STDERR-FILE says what to do with standard error in the child.\n\
191 STDERR-FILE may be nil (discard standard error output),\n\
192 t (mix it with ordinary output), or a file name string.\n\
194 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
195 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
197 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
198 Otherwise it waits for PROGRAM to terminate\n\
199 and returns a numeric exit status or a signal description string.\n\
200 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
203 register Lisp_Object
*args
;
205 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
212 int count
= specpdl_ptr
- specpdl
;
214 register unsigned char **new_argv
215 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
216 struct buffer
*old
= current_buffer
;
217 /* File to use for stderr in the child.
218 t means use same as standard output. */
219 Lisp_Object error_file
;
220 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
221 char *outf
, *tempfile
;
227 struct coding_system process_coding
; /* coding-system of process output */
228 struct coding_system argument_coding
; /* coding-system of arguments */
230 CHECK_STRING (args
[0], 0);
235 /* Without asynchronous processes we cannot have BUFFER == 0. */
236 if (nargs
>= 3 && INTEGERP (args
[2]))
237 error ("Operating system cannot handle asynchronous subprocesses");
238 #endif /* subprocesses */
240 /* Decide the coding-system for giving arguments and reading process
243 Lisp_Object val
, *args2
;
244 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
245 Lisp_Object coding_systems
= Qt
;
248 /* If arguments are supplied, we may have to encode them. */
253 for (i
= 4; i
< nargs
; i
++)
254 if (STRING_MULTIBYTE (args
[i
]))
257 if (!NILP (Vcoding_system_for_write
))
258 val
= Vcoding_system_for_write
;
259 else if (! must_encode
)
263 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
264 args2
[0] = Qcall_process
;
265 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
266 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
267 if (CONSP (coding_systems
))
268 val
= XCONS (coding_systems
)->cdr
;
269 else if (CONSP (Vdefault_process_coding_system
))
270 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
274 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
277 /* If BUFFER is nil, we must read process output once and then
278 discard it, so setup coding system but with nil. If BUFFER is
279 an integer, we can discard it without reading. */
280 if (nargs
< 3 || NILP (args
[2]))
281 setup_coding_system (Qnil
, &process_coding
);
282 else if (!INTEGERP (args
[2]))
285 if (!NILP (Vcoding_system_for_read
))
286 val
= Vcoding_system_for_read
;
287 else if (NILP (current_buffer
->enable_multibyte_characters
))
291 if (!EQ (coding_systems
, Qt
))
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
];
297 = Ffind_operation_coding_system (nargs
+ 1, args2
);
299 if (CONSP (coding_systems
))
300 val
= XCONS (coding_systems
)->car
;
301 else if (CONSP (Vdefault_process_coding_system
))
302 val
= XCONS (Vdefault_process_coding_system
)->car
;
306 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
308 /* On MSDOS, if the user did not ask for binary, treat it as
309 "text" which means doing CRLF conversion. Otherwise, leave
312 Note that ``binary'' here only means whether EOLs should or
313 should not be converted, since that's what Vbinary_process_XXXput
314 meant in the days before the coding systems were introduced.
316 For other conversions, the caller should set coding-system
317 variables explicitly, or rely on auto-detection. */
319 /* FIXME: this probably should be moved into the guts of
320 `Ffind_operation_coding_system' for the case of `call-process'. */
321 if (NILP (Vbinary_process_output
))
323 process_coding
.eol_type
= CODING_EOL_CRLF
;
324 if (process_coding
.type
== coding_type_no_conversion
)
325 /* FIXME: should we set type to undecided? */
326 process_coding
.type
= coding_type_emacs_mule
;
329 process_coding
.eol_type
= CODING_EOL_LF
;
334 if (nargs
>= 2 && ! NILP (args
[1]))
336 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
337 CHECK_STRING (infile
, 1);
340 infile
= build_string (NULL_DEVICE
);
346 /* If BUFFER is a list, its meaning is
347 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
350 if (CONSP (XCONS (buffer
)->cdr
))
352 Lisp_Object stderr_file
;
353 stderr_file
= XCONS (XCONS (buffer
)->cdr
)->car
;
355 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
356 error_file
= stderr_file
;
358 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
361 buffer
= XCONS (buffer
)->car
;
364 if (!(EQ (buffer
, Qnil
)
366 || XFASTINT (buffer
) == 0))
368 Lisp_Object spec_buffer
;
369 spec_buffer
= buffer
;
370 buffer
= Fget_buffer_create (buffer
);
371 /* Mention the buffer name for a better error message. */
373 CHECK_BUFFER (spec_buffer
, 2);
374 CHECK_BUFFER (buffer
, 2);
380 /* Make sure that the child will be able to chdir to the current
381 buffer's current directory, or its unhandled equivalent. We
382 can't just have the child check for an error when it does the
383 chdir, since it's in a vfork.
385 We have to GCPRO around this because Fexpand_file_name,
386 Funhandled_file_name_directory, and Ffile_accessible_directory_p
387 might call a file name handling function. The argument list is
388 protected by the caller, so all we really have to worry about is
391 struct gcpro gcpro1
, gcpro2
, gcpro3
;
393 current_dir
= current_buffer
->directory
;
395 GCPRO3 (infile
, buffer
, current_dir
);
398 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
400 if (NILP (Ffile_accessible_directory_p (current_dir
)))
401 report_file_error ("Setting current directory",
402 Fcons (current_buffer
->directory
, Qnil
));
407 display
= nargs
>= 4 ? args
[3] : Qnil
;
409 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
412 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
414 /* Search for program; barf if not found. */
418 GCPRO1 (current_dir
);
419 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
425 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
427 new_argv
[0] = XSTRING (path
)->data
;
432 for (i
= 4; i
< nargs
; i
++) CHECK_STRING (args
[i
], i
);
434 if (! CODING_REQUIRE_ENCODING (&argument_coding
))
436 for (i
= 4; i
< nargs
; i
++)
437 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
441 /* We must encode the arguments. */
442 struct gcpro gcpro1
, gcpro2
, gcpro3
;
444 GCPRO3 (infile
, buffer
, current_dir
);
445 for (i
= 4; i
< nargs
; i
++)
447 int size
= encoding_buffer_size (&argument_coding
,
448 XSTRING (args
[i
])->size_byte
);
449 unsigned char *dummy1
= (unsigned char *) alloca (size
);
452 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
453 new_argv
[i
- 3] = dummy1
;
454 encode_coding (&argument_coding
,
455 XSTRING (args
[i
])->data
,
457 XSTRING (args
[i
])->size_byte
,
459 new_argv
[i
- 3][argument_coding
.produced
] = 0;
463 new_argv
[nargs
- 3] = 0;
468 #ifdef MSDOS /* MW, July 1993 */
469 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
470 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
473 tempfile
= alloca (20);
476 dostounix_filename (tempfile
);
477 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
478 strcat (tempfile
, "/");
479 strcat (tempfile
, "detmp.XXX");
482 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
486 report_file_error ("Opening process output file",
487 Fcons (build_string (tempfile
), Qnil
));
493 if (INTEGERP (buffer
))
494 fd
[1] = open (NULL_DEVICE
, O_WRONLY
), fd
[0] = -1;
501 /* Replaced by close_process_descs */
502 set_exclusive_use (fd
[0]);
507 /* child_setup must clobber environ in systems with true vfork.
508 Protect it from permanent change. */
509 register char **save_environ
= environ
;
510 register int fd1
= fd
[1];
513 #if 0 /* Some systems don't have sigblock. */
514 mask
= sigblock (sigmask (SIGCHLD
));
517 /* Record that we're about to create a synchronous process. */
518 synch_process_alive
= 1;
520 /* These vars record information from process termination.
521 Clear them now before process can possibly terminate,
522 to avoid timing error if process terminates soon. */
523 synch_process_death
= 0;
524 synch_process_retcode
= 0;
526 if (NILP (error_file
))
527 fd_error
= open (NULL_DEVICE
, O_WRONLY
);
528 else if (STRINGP (error_file
))
531 fd_error
= open (XSTRING (error_file
)->data
,
532 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
534 #else /* not DOS_NT */
535 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
536 #endif /* not DOS_NT */
549 report_file_error ("Cannot redirect stderr",
550 Fcons ((NILP (error_file
)
551 ? build_string (NULL_DEVICE
) : error_file
),
556 = Fencode_coding_string (current_dir
, Vfile_name_coding_system
, Qt
);
558 #ifdef MSDOS /* MW, July 1993 */
559 /* ??? Someone who knows MSDOG needs to check whether this properly
560 closes all descriptors that it opens.
562 Note that run_msdos_command() actually returns the child process
563 exit status, not its PID, so we assign it to `synch_process_retcode'
565 pid
= run_msdos_command (new_argv
, current_dir
,
566 filefd
, outfilefd
, fd_error
);
568 /* Record that the synchronous process exited and note its
569 termination status. */
570 synch_process_alive
= 0;
571 synch_process_retcode
= pid
;
572 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
573 synch_process_death
= strerror (errno
);
576 if (fd_error
!= outfilefd
)
578 fd1
= -1; /* No harm in closing that one! */
579 /* Since CRLF is converted to LF within `decode_coding', we can
580 always open a file with binary mode. */
581 fd
[0] = open (tempfile
, O_BINARY
);
586 report_file_error ("Cannot re-open temporary file", Qnil
);
588 #else /* not MSDOS */
590 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
592 #else /* not WINDOWSNT */
602 #if defined (USG) && !defined (BSD_PGRPS)
607 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
610 #endif /* not WINDOWSNT */
612 /* The MSDOS case did this already. */
615 #endif /* not MSDOS */
617 environ
= save_environ
;
619 /* Close most of our fd's, but not fd[0]
620 since we will use that to read input from. */
622 if (fd1
>= 0 && fd1
!= fd_error
)
630 report_file_error ("Doing vfork", Qnil
);
633 if (INTEGERP (buffer
))
638 /* If Emacs has been built with asynchronous subprocess support,
639 we don't need to do this, I think because it will then have
640 the facilities for handling SIGCHLD. */
641 wait_without_blocking ();
642 #endif /* subprocesses */
646 /* Enable sending signal if user quits below. */
647 call_process_exited
= 0;
650 /* MSDOS needs different cleanup information. */
651 record_unwind_protect (call_process_cleanup
,
652 Fcons (make_number (fd
[0]), build_string (tempfile
)));
654 record_unwind_protect (call_process_cleanup
,
655 Fcons (make_number (fd
[0]), make_number (pid
)));
656 #endif /* not MSDOS */
659 if (BUFFERP (buffer
))
660 Fset_buffer (buffer
);
673 /* Repeatedly read until we've filled as much as possible
674 of the buffer size we have. But don't read
675 less than 1024--save that for the next bufferful. */
678 while (nread
< bufsize
- 1024)
680 int this_read
= read (fd
[0], bufptr
+ nread
, bufsize
- nread
);
693 /* Now NREAD is the total amount of data in the buffer. */
694 if (nread
== carryover
)
695 /* Here, just tell decode_coding that we are processing the
696 last block. We break the loop after decoding. */
697 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
700 total_read
+= nread
- carryover
;
704 if (process_coding
.type
== coding_type_no_conversion
)
705 insert (bufptr
, nread
);
707 { /* We have to decode the input. */
708 int size
= decoding_buffer_size (&process_coding
, nread
);
709 char *decoding_buf
= get_conversion_buffer (size
);
711 decode_coding (&process_coding
, bufptr
, decoding_buf
,
713 if (process_coding
.produced
> 0)
714 insert (decoding_buf
, process_coding
.produced
);
715 carryover
= process_coding
.produced
- process_coding
.consumed
;
718 /* As CARRYOVER should not be that large, we had
719 better avoid overhead of bcopy. */
720 char *p
= bufptr
+ process_coding
.consumed
;
721 char *pend
= p
+ carryover
;
724 while (p
< pend
) *dst
++ = *p
++;
728 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
731 insert (bufptr
, carryover
);
735 /* Make the buffer bigger as we continue to read more data,
737 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
740 bufptr
= (char *) alloca (bufsize
);
743 if (!NILP (display
) && INTERACTIVE
)
746 prepare_menu_bars ();
748 redisplay_preserve_echo_area ();
756 Vlast_coding_system_used
= process_coding
.symbol
;
758 /* Wait for it to terminate, unless it already has. */
759 wait_for_termination (pid
);
763 set_buffer_internal (old
);
765 /* Don't kill any children that the subprocess may have left behind
767 call_process_exited
= 1;
769 unbind_to (count
, Qnil
);
771 if (synch_process_death
)
772 return build_string (synch_process_death
);
773 return make_number (synch_process_retcode
);
778 delete_temp_file (name
)
781 /* Use Fdelete_file (indirectly) because that runs a file name handler.
782 We did that when writing the file, so we should do so when deleting. */
783 internal_delete_file (name
);
786 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
788 "Send text from START to END to a synchronous process running PROGRAM.\n\
789 Delete the text if fourth arg DELETE is non-nil.\n\
791 Insert output in BUFFER before point; t means current buffer;\n\
792 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
793 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
794 REAL-BUFFER says what to do with standard output, as above,\n\
795 while STDERR-FILE says what to do with standard error in the child.\n\
796 STDERR-FILE may be nil (discard standard error output),\n\
797 t (mix it with ordinary output), or a file name string.\n\
799 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
800 Remaining args are passed to PROGRAM at startup as command args.\n\
802 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
803 Otherwise it waits for PROGRAM to terminate\n\
804 and returns a numeric exit status or a signal description string.\n\
805 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
808 register Lisp_Object
*args
;
811 Lisp_Object filename_string
;
812 register Lisp_Object start
, end
;
813 int count
= specpdl_ptr
- specpdl
;
814 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
815 Lisp_Object coding_systems
= Qt
;
816 Lisp_Object val
, *args2
;
822 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
823 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
826 tempfile
= alloca (20);
829 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
830 strcat (tempfile
, "/");
831 if ('/' == DIRECTORY_SEP
)
832 dostounix_filename (tempfile
);
834 unixtodos_filename (tempfile
);
836 strcat (tempfile
, "emXXXXXX");
838 strcat (tempfile
, "detmp.XXX");
840 #else /* not DOS_NT */
841 char *tempfile
= (char *) alloca (XSTRING (Vtemp_file_name_pattern
)->size_byte
+ 1);
842 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
843 XSTRING (Vtemp_file_name_pattern
)->size_byte
+ 1);
844 #endif /* not DOS_NT */
848 filename_string
= build_string (tempfile
);
849 GCPRO1 (filename_string
);
852 /* Decide coding-system of the contents of the temporary file. */
854 /* This is to cause find-buffer-file-type-coding-system (see
855 dos-w32.el) to choose correct EOL translation for write-region. */
856 specbind (Qbuffer_file_type
, Vbinary_process_input
);
858 if (!NILP (Vcoding_system_for_write
))
859 val
= Vcoding_system_for_write
;
860 else if (NILP (current_buffer
->enable_multibyte_characters
))
864 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
865 args2
[0] = Qcall_process_region
;
866 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
867 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
868 if (CONSP (coding_systems
))
869 val
= XCONS (coding_systems
)->cdr
;
870 else if (CONSP (Vdefault_process_coding_system
))
871 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
877 /* binary-process-input tells whether the buffer needs to be
878 written with EOL conversions, but it doesn't say anything
879 about the rest of text encoding. It takes effect whenever
880 the coding system doesn't otherwise specify what to do for
884 if (! NILP (Vbinary_process_input
))
885 val
= intern ("undecided-unix");
887 val
= intern ("undecided-dos");
889 else if (SYMBOLP (val
) && NILP (Vcoding_system_for_write
))
892 eolval
= Fget (val
, Qeol_type
);
893 if (VECTORP (eolval
) && XVECTOR (eolval
)->size
> 1)
894 /* Use element 1 (CRLF conversion) for "text",
895 and element 0 (LF conversion) for "binary". */
896 val
= XVECTOR (eolval
)->contents
[NILP (Vbinary_process_input
)];
900 specbind (intern ("coding-system-for-write"), val
);
901 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
);
903 /* Note that Fcall_process takes care of binding
904 coding-system-for-read. */
906 record_unwind_protect (delete_temp_file
, filename_string
);
909 Fdelete_region (start
, end
);
911 args
[3] = filename_string
;
913 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
- 2, args
+ 2)));
916 #ifndef VMS /* VMS version is in vmsproc.c. */
918 /* This is the last thing run in a newly forked inferior
919 either synchronous or asynchronous.
920 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
921 Initialize inferior's priority, pgrp, connected dir and environment.
922 then exec another program based on new_argv.
924 This function may change environ for the superior process.
925 Therefore, the superior process must save and restore the value
926 of environ around the vfork and the call to this function.
928 ENV is the environment for the subprocess.
930 SET_PGRP is nonzero if we should put the subprocess into a separate
933 CURRENT_DIR is an elisp string giving the path of the current
934 directory the subprocess should have. Since we can't really signal
935 a decent error from within the child, this should be verified as an
936 executable directory by the parent. */
938 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
940 register char **new_argv
;
942 Lisp_Object current_dir
;
945 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
947 #else /* not MSDOS */
953 #endif /* WINDOWSNT */
957 #ifdef SET_EMACS_PRIORITY
959 extern int emacs_priority
;
961 if (emacs_priority
< 0)
962 nice (- emacs_priority
);
967 /* Close Emacs's descriptors that this process should not have. */
968 close_process_descs ();
972 /* Note that use of alloca is always safe here. It's obvious for systems
973 that do not have true vfork or that have true (stack) alloca.
974 If using vfork and C_ALLOCA it is safe because that changes
975 the superior's static variables as if the superior had done alloca
976 and will be cleaned up in the usual way. */
981 i
= XSTRING (current_dir
)->size_byte
;
982 pwd_var
= (char *) alloca (i
+ 6);
984 bcopy ("PWD=", pwd_var
, 4);
985 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
986 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
990 /* We can't signal an Elisp error here; we're in a vfork. Since
991 the callers check the current directory before forking, this
992 should only return an error if the directory's permissions
993 are changed between the check and this chdir, but we should
995 if (chdir (temp
) < 0)
999 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1000 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1004 /* Set `env' to a vector of the strings in Vprocess_environment. */
1006 register Lisp_Object tem
;
1007 register char **new_env
;
1008 register int new_length
;
1011 for (tem
= Vprocess_environment
;
1012 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
1013 tem
= XCONS (tem
)->cdr
)
1016 /* new_length + 2 to include PWD and terminating 0. */
1017 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1019 /* If we have a PWD envvar, pass one down,
1020 but with corrected value. */
1022 *new_env
++ = pwd_var
;
1024 /* Copy the Vprocess_environment strings into new_env. */
1025 for (tem
= Vprocess_environment
;
1026 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
1027 tem
= XCONS (tem
)->cdr
)
1030 char *string
= (char *) XSTRING (XCONS (tem
)->car
)->data
;
1031 /* See if this string duplicates any string already in the env.
1032 If so, don't put it in.
1033 When an env var has multiple definitions,
1034 we keep the definition that comes first in process-environment. */
1035 for (; ep
!= new_env
; ep
++)
1037 char *p
= *ep
, *q
= string
;
1041 /* The string is malformed; might as well drop it. */
1050 *new_env
++ = string
;
1056 prepare_standard_handles (in
, out
, err
, handles
);
1057 set_process_dir (XSTRING (current_dir
)->data
);
1058 #else /* not WINDOWSNT */
1059 /* Make sure that in, out, and err are not actually already in
1060 descriptors zero, one, or two; this could happen if Emacs is
1061 started with its standard in, out, or error closed, as might
1064 int oin
= in
, oout
= out
;
1066 /* We have to avoid relocating the same descriptor twice! */
1068 in
= relocate_fd (in
, 3);
1073 out
= relocate_fd (out
, 3);
1077 else if (err
== oout
)
1080 err
= relocate_fd (err
, 3);
1093 #endif /* not WINDOWSNT */
1095 #if defined(USG) && !defined(BSD_PGRPS)
1096 #ifndef SETPGRP_RELEASES_CTTY
1097 setpgrp (); /* No arguments but equivalent in this case */
1102 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1103 EMACS_SET_TTY_PGRP (0, &pid
);
1106 something missing here
;
1110 /* Spawn the child. (See ntproc.c:Spawnve). */
1111 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1112 reset_standard_handles (in
, out
, err
, handles
);
1114 /* An error occurred while trying to spawn the process. */
1115 report_file_error ("Spawning child process", Qnil
);
1117 #else /* not WINDOWSNT */
1118 /* execvp does not accept an environment arg so the only way
1119 to pass this environment is to set environ. Our caller
1120 is responsible for restoring the ambient value of environ. */
1122 execvp (new_argv
[0], new_argv
);
1124 write (1, "Can't exec program: ", 20);
1125 write (1, new_argv
[0], strlen (new_argv
[0]));
1128 #endif /* not WINDOWSNT */
1129 #endif /* not MSDOS */
1132 /* Move the file descriptor FD so that its number is not less than MIN.
1133 If the file descriptor is moved at all, the original is freed. */
1135 relocate_fd (fd
, min
)
1145 char *message1
= "Error while setting up child: ";
1146 char *errmessage
= strerror (errno
);
1147 char *message2
= "\n";
1148 write (2, message1
, strlen (message1
));
1149 write (2, errmessage
, strlen (errmessage
));
1150 write (2, message2
, strlen (message2
));
1153 /* Note that we hold the original FD open while we recurse,
1154 to guarantee we'll get a new FD if we need it. */
1155 new = relocate_fd (new, min
);
1162 getenv_internal (var
, varlen
, value
, valuelen
)
1170 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
1174 entry
= XCONS (scan
)->car
;
1176 && XSTRING (entry
)->size_byte
> varlen
1177 && XSTRING (entry
)->data
[varlen
] == '='
1179 /* NT environment variables are case insensitive. */
1180 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1181 #else /* not WINDOWSNT */
1182 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1183 #endif /* not WINDOWSNT */
1186 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1187 *valuelen
= XSTRING (entry
)->size_byte
- (varlen
+ 1);
1195 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
1196 "Return the value of environment variable VAR, as a string.\n\
1197 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1198 This function consults the variable ``process-environment'' for its value.")
1205 CHECK_STRING (var
, 0);
1206 if (getenv_internal (XSTRING (var
)->data
, XSTRING (var
)->size_byte
,
1208 return make_string (value
, valuelen
);
1213 /* A version of getenv that consults process_environment, easily
1222 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1228 #endif /* not VMS */
1230 /* This is run before init_cmdargs. */
1234 char *data_dir
= egetenv ("EMACSDATA");
1235 char *doc_dir
= egetenv ("EMACSDOC");
1238 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1241 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1244 /* Check the EMACSPATH environment variable, defaulting to the
1245 PATH_EXEC path from paths.h. */
1246 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1247 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1248 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1251 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1255 char *data_dir
= egetenv ("EMACSDATA");
1258 Lisp_Object tempdir
;
1260 if (initialized
&& !NILP (Vinstallation_directory
))
1262 /* Add to the path the lib-src subdir of the installation dir. */
1264 tem
= Fexpand_file_name (build_string ("lib-src"),
1265 Vinstallation_directory
);
1266 if (NILP (Fmember (tem
, Vexec_path
)))
1269 /* MSDOS uses wrapped binaries, so don't do this. */
1270 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1271 Vexec_directory
= Ffile_name_as_directory (tem
);
1272 #endif /* not DOS_NT */
1275 /* Maybe use ../etc as well as ../lib-src. */
1278 tem
= Fexpand_file_name (build_string ("etc"),
1279 Vinstallation_directory
);
1280 Vdoc_directory
= Ffile_name_as_directory (tem
);
1284 /* Look for the files that should be in etc. We don't use
1285 Vinstallation_directory, because these files are never installed
1286 near the executable, and they are never in the build
1287 directory when that's different from the source directory.
1289 Instead, if these files are not in the nominal place, we try the
1290 source directory. */
1293 Lisp_Object tem
, tem1
, newdir
;
1295 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1296 tem1
= Ffile_exists_p (tem
);
1299 newdir
= Fexpand_file_name (build_string ("../etc/"),
1300 build_string (PATH_DUMPLOADSEARCH
));
1301 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1302 tem1
= Ffile_exists_p (tem
);
1304 Vdata_directory
= newdir
;
1312 tempdir
= Fdirectory_file_name (Vexec_directory
);
1313 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1314 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1318 tempdir
= Fdirectory_file_name (Vdata_directory
);
1319 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1320 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1324 Vshell_file_name
= build_string ("*dcl*");
1326 sh
= (char *) getenv ("SHELL");
1327 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1331 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1333 if (getenv ("TMPDIR"))
1335 char *dir
= getenv ("TMPDIR");
1336 Vtemp_file_name_pattern
1337 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1338 build_string (dir
));
1341 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1345 set_process_environment ()
1347 register char **envp
;
1349 Vprocess_environment
= Qnil
;
1353 for (envp
= environ
; *envp
; envp
++)
1354 Vprocess_environment
= Fcons (build_string (*envp
),
1355 Vprocess_environment
);
1361 Qbuffer_file_type
= intern ("buffer-file-type");
1362 staticpro (&Qbuffer_file_type
);
1364 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input
,
1365 "*If non-nil then new subprocesses are assumed to take binary input.");
1366 Vbinary_process_input
= Qnil
;
1368 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output
,
1369 "*If non-nil then new subprocesses are assumed to produce binary output.");
1370 Vbinary_process_output
= Qnil
;
1373 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1374 "*File name to load inferior shells from.\n\
1375 Initialized from the SHELL environment variable.");
1377 DEFVAR_LISP ("exec-path", &Vexec_path
,
1378 "*List of directories to search programs to run in subprocesses.\n\
1379 Each element is a string (directory name) or nil (try default directory).");
1381 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1382 "Directory of architecture-dependent files that come with GNU Emacs,\n\
1383 especially executable programs intended for Emacs to invoke.");
1385 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1386 "Directory of architecture-independent files that come with GNU Emacs,\n\
1387 intended for Emacs to use.");
1389 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1390 "Directory containing the DOC file that comes with GNU Emacs.\n\
1391 This is usually the same as data-directory.");
1393 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1394 "For internal use by the build procedure only.\n\
1395 This is the name of the directory in which the build procedure installed\n\
1396 Emacs's info files; the default value for Info-default-directory-list\n\
1398 Vconfigure_info_directory
= build_string (PATH_INFO
);
1400 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1401 "Pattern for making names for temporary files.\n\
1402 This is used by `call-process-region'.");
1403 /* This variable is initialized in init_callproc. */
1405 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1406 "List of environment variables for subprocesses to inherit.\n\
1407 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1408 The environment which Emacs inherits is placed in this variable\n\
1409 when Emacs starts.");
1412 defsubr (&Scall_process
);
1415 defsubr (&Scall_process_region
);