1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99,2000 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. */
31 /* Define SIGCHLD as an alias for SIGCLD. */
33 #if !defined (SIGCHLD) && defined (SIGCLD)
34 #define SIGCHLD SIGCLD
37 #include <sys/types.h>
45 #define INCLUDED_FCNTL
52 #include <stdlib.h> /* for proper declaration of environ */
55 #define _P_NOWAIT 1 /* from process.h */
58 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
59 #define INCLUDED_FCNTL
62 #include <sys/param.h>
80 #include "composite.h"
83 #include "syssignal.h"
91 extern noshare
char **environ
;
94 extern char **environ
;
99 #if !defined (USG) || defined (BSD_PGRPS)
101 #define setpgrp setpgid
105 #define max(a, b) ((a) > (b) ? (a) : (b))
107 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
108 Lisp_Object Vconfigure_info_directory
;
109 Lisp_Object Vtemp_file_name_pattern
;
111 Lisp_Object Vshell_file_name
;
113 Lisp_Object Vprocess_environment
;
116 Lisp_Object Qbuffer_file_type
;
119 /* True iff we are about to fork off a synchronous process or if we
120 are waiting for it. */
121 int synch_process_alive
;
123 /* Nonzero => this is a string explaining death of synchronous subprocess. */
124 char *synch_process_death
;
126 /* If synch_process_death is zero,
127 this is exit code of synchronous subprocess. */
128 int synch_process_retcode
;
130 extern Lisp_Object Vdoc_file_name
;
132 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
134 /* Clean up when exiting Fcall_process.
135 On MSDOS, delete the temporary file on any kind of termination.
136 On Unix, kill the process and any children on termination by signal. */
138 /* Nonzero if this is termination due to exit. */
139 static int call_process_exited
;
141 #ifndef VMS /* VMS version is in vmsproc.c. */
144 call_process_kill (fdpid
)
147 emacs_close (XFASTINT (Fcar (fdpid
)));
148 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
149 synch_process_alive
= 0;
154 call_process_cleanup (fdpid
)
157 #if defined (MSDOS) || defined (macintosh)
158 /* for MSDOS fdpid is really (fd . tempfile) */
159 register Lisp_Object file
;
161 emacs_close (XFASTINT (Fcar (fdpid
)));
162 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
163 unlink (XSTRING (file
)->data
);
164 #else /* not MSDOS and not macintosh */
165 register int pid
= XFASTINT (Fcdr (fdpid
));
167 if (call_process_exited
)
169 emacs_close (XFASTINT (Fcar (fdpid
)));
173 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
175 int count
= specpdl_ptr
- specpdl
;
176 record_unwind_protect (call_process_kill
, fdpid
);
177 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
180 wait_for_termination (pid
);
182 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
183 message1 ("Waiting for process to die...done");
185 synch_process_alive
= 0;
186 emacs_close (XFASTINT (Fcar (fdpid
)));
187 #endif /* not MSDOS */
191 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
192 "Call PROGRAM synchronously in separate process.\n\
193 The remaining arguments are optional.\n\
194 The program's input comes from file INFILE (nil means `/dev/null').\n\
195 Insert output in BUFFER before point; t means current buffer;\n\
196 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
197 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
198 REAL-BUFFER says what to do with standard output, as above,\n\
199 while STDERR-FILE says what to do with standard error in the child.\n\
200 STDERR-FILE may be nil (discard standard error output),\n\
201 t (mix it with ordinary output), or a file name string.\n\
203 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
204 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
206 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
207 Otherwise it waits for PROGRAM to terminate\n\
208 and returns a numeric exit status or a signal description string.\n\
209 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
212 register Lisp_Object
*args
;
214 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
221 int count
= specpdl_ptr
- specpdl
;
223 register unsigned char **new_argv
224 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
225 struct buffer
*old
= current_buffer
;
226 /* File to use for stderr in the child.
227 t means use same as standard output. */
228 Lisp_Object error_file
;
229 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
230 char *outf
, *tempfile
;
240 struct coding_system process_coding
; /* coding-system of process output */
241 struct coding_system argument_coding
; /* coding-system of arguments */
242 /* Set to the return value of Ffind_operation_coding_system. */
243 Lisp_Object coding_systems
;
245 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
248 CHECK_STRING (args
[0], 0);
253 /* Without asynchronous processes we cannot have BUFFER == 0. */
255 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
256 error ("Operating system cannot handle asynchronous subprocesses");
257 #endif /* subprocesses */
259 /* Decide the coding-system for giving arguments. */
261 Lisp_Object val
, *args2
;
264 /* If arguments are supplied, we may have to encode them. */
269 for (i
= 4; i
< nargs
; i
++)
270 CHECK_STRING (args
[i
], i
);
272 for (i
= 4; i
< nargs
; i
++)
273 if (STRING_MULTIBYTE (args
[i
]))
276 if (!NILP (Vcoding_system_for_write
))
277 val
= Vcoding_system_for_write
;
278 else if (! must_encode
)
282 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
283 args2
[0] = Qcall_process
;
284 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
285 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
286 if (CONSP (coding_systems
))
287 val
= XCDR (coding_systems
);
288 else if (CONSP (Vdefault_process_coding_system
))
289 val
= XCDR (Vdefault_process_coding_system
);
293 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
297 if (nargs
>= 2 && ! NILP (args
[1]))
299 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
300 CHECK_STRING (infile
, 1);
303 infile
= build_string (NULL_DEVICE
);
309 /* If BUFFER is a list, its meaning is
310 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
313 if (CONSP (XCDR (buffer
)))
315 Lisp_Object stderr_file
;
316 stderr_file
= XCAR (XCDR (buffer
));
318 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
319 error_file
= stderr_file
;
321 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
324 buffer
= XCAR (buffer
);
327 if (!(EQ (buffer
, Qnil
)
329 || INTEGERP (buffer
)))
331 Lisp_Object spec_buffer
;
332 spec_buffer
= buffer
;
333 buffer
= Fget_buffer_create (buffer
);
334 /* Mention the buffer name for a better error message. */
336 CHECK_BUFFER (spec_buffer
, 2);
337 CHECK_BUFFER (buffer
, 2);
343 /* Make sure that the child will be able to chdir to the current
344 buffer's current directory, or its unhandled equivalent. We
345 can't just have the child check for an error when it does the
346 chdir, since it's in a vfork.
348 We have to GCPRO around this because Fexpand_file_name,
349 Funhandled_file_name_directory, and Ffile_accessible_directory_p
350 might call a file name handling function. The argument list is
351 protected by the caller, so all we really have to worry about is
354 struct gcpro gcpro1
, gcpro2
, gcpro3
;
356 current_dir
= current_buffer
->directory
;
358 GCPRO3 (infile
, buffer
, current_dir
);
361 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
363 if (NILP (Ffile_accessible_directory_p (current_dir
)))
364 report_file_error ("Setting current directory",
365 Fcons (current_buffer
->directory
, Qnil
));
370 display
= nargs
>= 4 ? args
[3] : Qnil
;
372 filefd
= emacs_open (XSTRING (infile
)->data
, O_RDONLY
, 0);
375 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
377 /* Search for program; barf if not found. */
381 GCPRO1 (current_dir
);
382 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
387 emacs_close (filefd
);
388 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
390 new_argv
[0] = XSTRING (path
)->data
;
394 struct gcpro gcpro1
, gcpro2
, gcpro3
;
396 GCPRO3 (infile
, buffer
, current_dir
);
397 argument_coding
.dst_multibyte
= 0;
398 for (i
= 4; i
< nargs
; i
++)
400 argument_coding
.src_multibyte
= STRING_MULTIBYTE (args
[i
]);
401 if (CODING_REQUIRE_ENCODING (&argument_coding
))
403 /* We must encode this argument. */
404 args
[i
] = encode_coding_string (args
[i
], &argument_coding
, 1);
405 if (argument_coding
.type
== coding_type_ccl
)
406 setup_ccl_program (&(argument_coding
.spec
.ccl
.encoder
), Qnil
);
408 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
411 new_argv
[nargs
- 3] = 0;
416 #ifdef MSDOS /* MW, July 1993 */
417 if ((outf
= egetenv ("TMPDIR")))
418 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
421 tempfile
= alloca (20);
424 dostounix_filename (tempfile
);
425 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
426 strcat (tempfile
, "/");
427 strcat (tempfile
, "detmp.XXX");
430 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
433 emacs_close (filefd
);
434 report_file_error ("Opening process output file",
435 Fcons (build_string (tempfile
), Qnil
));
442 /* Since we don't have pipes on the Mac, create a temporary file to
443 hold the output of the subprocess. */
444 tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
445 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
446 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
450 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
454 report_file_error ("Opening process output file",
455 Fcons (build_string (tempfile
), Qnil
));
459 #endif /* macintosh */
461 if (INTEGERP (buffer
))
462 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
471 /* Replaced by close_process_descs */
472 set_exclusive_use (fd
[0]);
477 /* child_setup must clobber environ in systems with true vfork.
478 Protect it from permanent change. */
479 register char **save_environ
= environ
;
480 register int fd1
= fd
[1];
483 #if 0 /* Some systems don't have sigblock. */
484 mask
= sigblock (sigmask (SIGCHLD
));
487 /* Record that we're about to create a synchronous process. */
488 synch_process_alive
= 1;
490 /* These vars record information from process termination.
491 Clear them now before process can possibly terminate,
492 to avoid timing error if process terminates soon. */
493 synch_process_death
= 0;
494 synch_process_retcode
= 0;
496 if (NILP (error_file
))
497 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
498 else if (STRINGP (error_file
))
501 fd_error
= emacs_open (XSTRING (error_file
)->data
,
502 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
504 #else /* not DOS_NT */
505 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
506 #endif /* not DOS_NT */
511 emacs_close (filefd
);
519 report_file_error ("Cannot redirect stderr",
520 Fcons ((NILP (error_file
)
521 ? build_string (NULL_DEVICE
) : error_file
),
525 current_dir
= ENCODE_FILE (current_dir
);
529 /* Call run_mac_command in sysdep.c here directly instead of doing
530 a child_setup as for MSDOS and other platforms. Note that this
531 code does not handle passing the environment to the synchronous
533 char *infn
, *outfn
, *errfn
, *currdn
;
535 /* close these files so subprocess can write to them */
537 if (fd_error
!= outfilefd
)
539 fd1
= -1; /* No harm in closing that one! */
541 infn
= XSTRING (infile
)->data
;
543 if (NILP (error_file
))
545 else if (EQ (Qt
, error_file
))
548 errfn
= XSTRING (error_file
)->data
;
549 currdn
= XSTRING (current_dir
)->data
;
550 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
552 /* Record that the synchronous process exited and note its
553 termination status. */
554 synch_process_alive
= 0;
555 synch_process_retcode
= pid
;
556 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
558 synchronize_system_messages_locale ();
559 synch_process_death
= strerror (errno
);
562 /* Since CRLF is converted to LF within `decode_coding', we can
563 always open a file with binary mode. */
564 fd
[0] = open (tempfile
, O_BINARY
);
569 report_file_error ("Cannot re-open temporary file", Qnil
);
572 #else /* not macintosh */
573 #ifdef MSDOS /* MW, July 1993 */
574 /* Note that on MSDOS `child_setup' actually returns the child process
575 exit status, not its PID, so we assign it to `synch_process_retcode'
577 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
580 /* Record that the synchronous process exited and note its
581 termination status. */
582 synch_process_alive
= 0;
583 synch_process_retcode
= pid
;
584 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
586 synchronize_system_messages_locale ();
587 synch_process_death
= strerror (errno
);
590 emacs_close (outfilefd
);
591 if (fd_error
!= outfilefd
)
592 emacs_close (fd_error
);
593 fd1
= -1; /* No harm in closing that one! */
594 /* Since CRLF is converted to LF within `decode_coding', we can
595 always open a file with binary mode. */
596 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
600 emacs_close (filefd
);
601 report_file_error ("Cannot re-open temporary file", Qnil
);
603 #else /* not MSDOS */
605 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
607 #else /* not WINDOWSNT */
617 #if defined (USG) && !defined (BSD_PGRPS)
622 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
625 #endif /* not WINDOWSNT */
627 /* The MSDOS case did this already. */
629 emacs_close (fd_error
);
630 #endif /* not MSDOS */
631 #endif /* not macintosh */
633 environ
= save_environ
;
635 /* Close most of our fd's, but not fd[0]
636 since we will use that to read input from. */
637 emacs_close (filefd
);
638 if (fd1
>= 0 && fd1
!= fd_error
)
646 report_file_error ("Doing vfork", Qnil
);
649 if (INTEGERP (buffer
))
654 /* If Emacs has been built with asynchronous subprocess support,
655 we don't need to do this, I think because it will then have
656 the facilities for handling SIGCHLD. */
657 wait_without_blocking ();
658 #endif /* subprocesses */
662 /* Enable sending signal if user quits below. */
663 call_process_exited
= 0;
665 #if defined(MSDOS) || defined(macintosh)
666 /* MSDOS needs different cleanup information. */
667 record_unwind_protect (call_process_cleanup
,
668 Fcons (make_number (fd
[0]), build_string (tempfile
)));
670 record_unwind_protect (call_process_cleanup
,
671 Fcons (make_number (fd
[0]), make_number (pid
)));
672 #endif /* not MSDOS and not macintosh */
675 if (BUFFERP (buffer
))
676 Fset_buffer (buffer
);
680 /* If BUFFER is nil, we must read process output once and then
681 discard it, so setup coding system but with nil. */
682 setup_coding_system (Qnil
, &process_coding
);
686 Lisp_Object val
, *args2
;
689 if (!NILP (Vcoding_system_for_read
))
690 val
= Vcoding_system_for_read
;
693 if (EQ (coding_systems
, Qt
))
697 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
698 args2
[0] = Qcall_process
;
699 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
701 = Ffind_operation_coding_system (nargs
+ 1, args2
);
703 if (CONSP (coding_systems
))
704 val
= XCAR (coding_systems
);
705 else if (CONSP (Vdefault_process_coding_system
))
706 val
= XCAR (Vdefault_process_coding_system
);
710 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
711 /* In unibyte mode, character code conversion should not take
712 place but EOL conversion should. So, setup raw-text or one
713 of the subsidiary according to the information just setup. */
714 if (NILP (current_buffer
->enable_multibyte_characters
)
716 setup_raw_text_coding_system (&process_coding
);
718 process_coding
.src_multibyte
= 0;
719 process_coding
.dst_multibyte
721 ? ! NILP (XBUFFER (buffer
)->enable_multibyte_characters
)
722 : ! NILP (current_buffer
->enable_multibyte_characters
));
732 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
733 struct coding_system saved_coding
;
734 int pt_orig
= PT
, pt_byte_orig
= PT_BYTE
;
737 saved_coding
= process_coding
;
738 if (process_coding
.composing
!= COMPOSITION_DISABLED
)
739 coding_allocate_composition_data (&process_coding
, PT
);
742 /* Repeatedly read until we've filled as much as possible
743 of the buffer size we have. But don't read
744 less than 1024--save that for the next bufferful. */
746 while (nread
< bufsize
- 1024)
748 int this_read
= emacs_read (fd
[0], bufptr
+ nread
,
756 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
761 total_read
+= this_read
;
763 if (display_on_the_fly
)
767 /* Now NREAD is the total amount of data in the buffer. */
772 if (! CODING_MAY_REQUIRE_DECODING (&process_coding
))
773 insert_1_both (bufptr
, nread
, nread
, 0, 1, 0);
775 { /* We have to decode the input. */
780 size
= decoding_buffer_size (&process_coding
, nread
);
781 decoding_buf
= (char *) xmalloc (size
);
782 if (process_coding
.cmp_data
)
783 process_coding
.cmp_data
->char_offset
= PT
;
784 decode_coding (&process_coding
, bufptr
, decoding_buf
,
786 if (display_on_the_fly
787 && saved_coding
.type
== coding_type_undecided
788 && process_coding
.type
!= coding_type_undecided
)
790 /* We have detected some coding system. But,
791 there's a possibility that the detection was
792 done by insufficient data. So, we give up
793 displaying on the fly. */
794 xfree (decoding_buf
);
795 display_on_the_fly
= 0;
796 process_coding
= saved_coding
;
800 if (process_coding
.produced
> 0)
801 insert_1_both (decoding_buf
, process_coding
.produced_char
,
802 process_coding
.produced
, 0, 1, 0);
803 xfree (decoding_buf
);
804 nread
-= process_coding
.consumed
;
807 /* As CARRYOVER should not be that large, we had
808 better avoid overhead of bcopy. */
809 BCOPY_SHORT (bufptr
+ process_coding
.consumed
, bufptr
,
811 if (process_coding
.result
== CODING_FINISH_INSUFFICIENT_CMP
)
813 /* The decoding ended because of insufficient data
814 area to record information about composition.
815 We must try decoding with additional data area
816 before reading more output for the process. */
817 coding_allocate_composition_data (&process_coding
, PT
);
818 goto repeat_decoding
;
823 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
826 /* Make the buffer bigger as we continue to read more data,
828 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
831 bufptr
= (char *) alloca (bufsize
);
834 if (!NILP (display
) && INTERACTIVE
)
837 prepare_menu_bars ();
839 redisplay_preserve_echo_area ();
847 && process_coding
.cmp_data
)
849 coding_restore_composition (&process_coding
, Fcurrent_buffer ());
850 coding_free_composition_data (&process_coding
);
854 int post_read_count
= specpdl_ptr
- specpdl
;
856 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
857 inserted
= PT
- pt_orig
;
858 TEMP_SET_PT_BOTH (pt_orig
, pt_byte_orig
);
859 if (SYMBOLP (process_coding
.post_read_conversion
)
860 && !NILP (Ffboundp (process_coding
.post_read_conversion
)))
861 call1 (process_coding
.post_read_conversion
, make_number (inserted
));
863 Vlast_coding_system_used
= process_coding
.symbol
;
865 /* If the caller required, let the buffer inherit the
866 coding-system used to decode the process output. */
867 if (inherit_process_coding_system
)
868 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
869 make_number (total_read
));
871 unbind_to (post_read_count
, Qnil
);
875 /* Wait for it to terminate, unless it already has. */
876 wait_for_termination (pid
);
880 set_buffer_internal (old
);
882 /* Don't kill any children that the subprocess may have left behind
884 call_process_exited
= 1;
886 unbind_to (count
, Qnil
);
888 if (synch_process_death
)
889 return code_convert_string_norecord (build_string (synch_process_death
),
890 Vlocale_coding_system
, 0);
891 return make_number (synch_process_retcode
);
896 delete_temp_file (name
)
899 /* Use Fdelete_file (indirectly) because that runs a file name handler.
900 We did that when writing the file, so we should do so when deleting. */
901 internal_delete_file (name
);
905 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
907 "Send text from START to END to a synchronous process running PROGRAM.\n\
908 The remaining arguments are optional.\n\
909 Delete the text if fourth arg DELETE is non-nil.\n\
911 Insert output in BUFFER before point; t means current buffer;\n\
912 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
913 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
914 REAL-BUFFER says what to do with standard output, as above,\n\
915 while STDERR-FILE says what to do with standard error in the child.\n\
916 STDERR-FILE may be nil (discard standard error output),\n\
917 t (mix it with ordinary output), or a file name string.\n\
919 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
920 Remaining args are passed to PROGRAM at startup as command args.\n\
922 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
923 Otherwise it waits for PROGRAM to terminate\n\
924 and returns a numeric exit status or a signal description string.\n\
925 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
928 register Lisp_Object
*args
;
931 Lisp_Object filename_string
;
932 register Lisp_Object start
, end
;
933 int count
= specpdl_ptr
- specpdl
;
934 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
935 Lisp_Object coding_systems
;
936 Lisp_Object val
, *args2
;
942 if ((outf
= egetenv ("TMPDIR"))
943 || (outf
= egetenv ("TMP"))
944 || (outf
= egetenv ("TEMP")))
945 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
948 tempfile
= alloca (20);
951 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
952 strcat (tempfile
, "/");
953 if ('/' == DIRECTORY_SEP
)
954 dostounix_filename (tempfile
);
956 unixtodos_filename (tempfile
);
958 strcat (tempfile
, "emXXXXXX");
960 strcat (tempfile
, "detmp.XXX");
962 #else /* not DOS_NT */
963 char *tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
964 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
965 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
966 #endif /* not DOS_NT */
972 int fd
= mkstemp (tempfile
);
974 report_file_error ("Failed to open temporary file",
975 Fcons (Vtemp_file_name_pattern
, Qnil
));
983 filename_string
= build_string (tempfile
);
984 GCPRO1 (filename_string
);
987 /* Decide coding-system of the contents of the temporary file. */
988 if (!NILP (Vcoding_system_for_write
))
989 val
= Vcoding_system_for_write
;
990 else if (NILP (current_buffer
->enable_multibyte_characters
))
994 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
995 args2
[0] = Qcall_process_region
;
996 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
997 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
998 if (CONSP (coding_systems
))
999 val
= XCDR (coding_systems
);
1000 else if (CONSP (Vdefault_process_coding_system
))
1001 val
= XCDR (Vdefault_process_coding_system
);
1007 int count1
= specpdl_ptr
- specpdl
;
1009 specbind (intern ("coding-system-for-write"), val
);
1010 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1012 unbind_to (count1
, Qnil
);
1015 /* Note that Fcall_process takes care of binding
1016 coding-system-for-read. */
1018 record_unwind_protect (delete_temp_file
, filename_string
);
1020 if (nargs
> 3 && !NILP (args
[3]))
1021 Fdelete_region (start
, end
);
1033 args
[1] = filename_string
;
1035 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1038 #ifndef VMS /* VMS version is in vmsproc.c. */
1040 static int relocate_fd ();
1042 /* This is the last thing run in a newly forked inferior
1043 either synchronous or asynchronous.
1044 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1045 Initialize inferior's priority, pgrp, connected dir and environment.
1046 then exec another program based on new_argv.
1048 This function may change environ for the superior process.
1049 Therefore, the superior process must save and restore the value
1050 of environ around the vfork and the call to this function.
1052 SET_PGRP is nonzero if we should put the subprocess into a separate
1055 CURRENT_DIR is an elisp string giving the path of the current
1056 directory the subprocess should have. Since we can't really signal
1057 a decent error from within the child, this should be verified as an
1058 executable directory by the parent. */
1061 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1063 register char **new_argv
;
1065 Lisp_Object current_dir
;
1072 #endif /* WINDOWSNT */
1074 int pid
= getpid ();
1076 #ifdef SET_EMACS_PRIORITY
1078 extern int emacs_priority
;
1080 if (emacs_priority
< 0)
1081 nice (- emacs_priority
);
1086 /* Close Emacs's descriptors that this process should not have. */
1087 close_process_descs ();
1089 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1090 we will lose if we call close_load_descs here. */
1092 close_load_descs ();
1095 /* Note that use of alloca is always safe here. It's obvious for systems
1096 that do not have true vfork or that have true (stack) alloca.
1097 If using vfork and C_ALLOCA it is safe because that changes
1098 the superior's static variables as if the superior had done alloca
1099 and will be cleaned up in the usual way. */
1101 register char *temp
;
1104 i
= STRING_BYTES (XSTRING (current_dir
));
1106 /* MSDOS must have all environment variables malloc'ed, because
1107 low-level libc functions that launch subsidiary processes rely
1109 pwd_var
= (char *) xmalloc (i
+ 6);
1111 pwd_var
= (char *) alloca (i
+ 6);
1114 bcopy ("PWD=", pwd_var
, 4);
1115 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
1116 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1120 /* We can't signal an Elisp error here; we're in a vfork. Since
1121 the callers check the current directory before forking, this
1122 should only return an error if the directory's permissions
1123 are changed between the check and this chdir, but we should
1125 if (chdir (temp
) < 0)
1130 /* Get past the drive letter, so that d:/ is left alone. */
1131 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1138 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1139 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1143 /* Set `env' to a vector of the strings in Vprocess_environment. */
1145 register Lisp_Object tem
;
1146 register char **new_env
;
1147 register int new_length
;
1150 for (tem
= Vprocess_environment
;
1151 CONSP (tem
) && STRINGP (XCAR (tem
));
1155 /* new_length + 2 to include PWD and terminating 0. */
1156 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1158 /* If we have a PWD envvar, pass one down,
1159 but with corrected value. */
1161 *new_env
++ = pwd_var
;
1163 /* Copy the Vprocess_environment strings into new_env. */
1164 for (tem
= Vprocess_environment
;
1165 CONSP (tem
) && STRINGP (XCAR (tem
));
1169 char *string
= (char *) XSTRING (XCAR (tem
))->data
;
1170 /* See if this string duplicates any string already in the env.
1171 If so, don't put it in.
1172 When an env var has multiple definitions,
1173 we keep the definition that comes first in process-environment. */
1174 for (; ep
!= new_env
; ep
++)
1176 char *p
= *ep
, *q
= string
;
1180 /* The string is malformed; might as well drop it. */
1189 *new_env
++ = string
;
1195 prepare_standard_handles (in
, out
, err
, handles
);
1196 set_process_dir (XSTRING (current_dir
)->data
);
1197 #else /* not WINDOWSNT */
1198 /* Make sure that in, out, and err are not actually already in
1199 descriptors zero, one, or two; this could happen if Emacs is
1200 started with its standard in, out, or error closed, as might
1203 int oin
= in
, oout
= out
;
1205 /* We have to avoid relocating the same descriptor twice! */
1207 in
= relocate_fd (in
, 3);
1212 out
= relocate_fd (out
, 3);
1216 else if (err
== oout
)
1219 err
= relocate_fd (err
, 3);
1233 #endif /* not MSDOS */
1234 #endif /* not WINDOWSNT */
1236 #if defined(USG) && !defined(BSD_PGRPS)
1237 #ifndef SETPGRP_RELEASES_CTTY
1238 setpgrp (); /* No arguments but equivalent in this case */
1243 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1244 EMACS_SET_TTY_PGRP (0, &pid
);
1247 something missing here
;
1251 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1254 /* An error occurred while trying to run the subprocess. */
1255 report_file_error ("Spawning child process", Qnil
);
1257 #else /* not MSDOS */
1259 /* Spawn the child. (See ntproc.c:Spawnve). */
1260 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1261 reset_standard_handles (in
, out
, err
, handles
);
1263 /* An error occurred while trying to spawn the process. */
1264 report_file_error ("Spawning child process", Qnil
);
1266 #else /* not WINDOWSNT */
1267 /* execvp does not accept an environment arg so the only way
1268 to pass this environment is to set environ. Our caller
1269 is responsible for restoring the ambient value of environ. */
1271 execvp (new_argv
[0], new_argv
);
1273 emacs_write (1, "Can't exec program: ", 20);
1274 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1275 emacs_write (1, "\n", 1);
1277 #endif /* not WINDOWSNT */
1278 #endif /* not MSDOS */
1281 /* Move the file descriptor FD so that its number is not less than MINFD.
1282 If the file descriptor is moved at all, the original is freed. */
1284 relocate_fd (fd
, minfd
)
1294 char *message1
= "Error while setting up child: ";
1295 char *errmessage
= strerror (errno
);
1296 char *message2
= "\n";
1297 emacs_write (2, message1
, strlen (message1
));
1298 emacs_write (2, errmessage
, strlen (errmessage
));
1299 emacs_write (2, message2
, strlen (message2
));
1302 /* Note that we hold the original FD open while we recurse,
1303 to guarantee we'll get a new FD if we need it. */
1304 new = relocate_fd (new, minfd
);
1311 getenv_internal (var
, varlen
, value
, valuelen
)
1319 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1323 entry
= XCAR (scan
);
1325 && STRING_BYTES (XSTRING (entry
)) > varlen
1326 && XSTRING (entry
)->data
[varlen
] == '='
1328 /* NT environment variables are case insensitive. */
1329 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1330 #else /* not WINDOWSNT */
1331 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1332 #endif /* not WINDOWSNT */
1335 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1336 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1344 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1345 "Return the value of environment variable VAR, as a string.\n\
1346 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1347 This function consults the variable ``process-environment'' for its value.")
1354 CHECK_STRING (var
, 0);
1355 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1357 return make_string (value
, valuelen
);
1362 /* A version of getenv that consults process_environment, easily
1371 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1377 #endif /* not VMS */
1379 /* This is run before init_cmdargs. */
1384 char *data_dir
= egetenv ("EMACSDATA");
1385 char *doc_dir
= egetenv ("EMACSDOC");
1388 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1391 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1394 /* Check the EMACSPATH environment variable, defaulting to the
1395 PATH_EXEC path from epaths.h. */
1396 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1397 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1398 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1401 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1406 char *data_dir
= egetenv ("EMACSDATA");
1409 Lisp_Object tempdir
;
1411 if (!NILP (Vinstallation_directory
))
1413 /* Add to the path the lib-src subdir of the installation dir. */
1415 tem
= Fexpand_file_name (build_string ("lib-src"),
1416 Vinstallation_directory
);
1418 /* MSDOS uses wrapped binaries, so don't do this. */
1419 if (NILP (Fmember (tem
, Vexec_path
)))
1420 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1422 Vexec_directory
= Ffile_name_as_directory (tem
);
1423 #endif /* not DOS_NT */
1425 /* Maybe use ../etc as well as ../lib-src. */
1428 tem
= Fexpand_file_name (build_string ("etc"),
1429 Vinstallation_directory
);
1430 Vdoc_directory
= Ffile_name_as_directory (tem
);
1434 /* Look for the files that should be in etc. We don't use
1435 Vinstallation_directory, because these files are never installed
1436 near the executable, and they are never in the build
1437 directory when that's different from the source directory.
1439 Instead, if these files are not in the nominal place, we try the
1440 source directory. */
1443 Lisp_Object tem
, tem1
, newdir
;
1445 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1446 tem1
= Ffile_exists_p (tem
);
1449 newdir
= Fexpand_file_name (build_string ("../etc/"),
1450 build_string (PATH_DUMPLOADSEARCH
));
1451 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1452 tem1
= Ffile_exists_p (tem
);
1454 Vdata_directory
= newdir
;
1462 tempdir
= Fdirectory_file_name (Vexec_directory
);
1463 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1464 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1468 tempdir
= Fdirectory_file_name (Vdata_directory
);
1469 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1470 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1474 Vshell_file_name
= build_string ("*dcl*");
1476 sh
= (char *) getenv ("SHELL");
1477 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1481 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1483 if (getenv ("TMPDIR"))
1485 char *dir
= getenv ("TMPDIR");
1486 Vtemp_file_name_pattern
1487 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1488 build_string (dir
));
1491 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1496 set_process_environment ()
1498 register char **envp
;
1500 Vprocess_environment
= Qnil
;
1504 for (envp
= environ
; *envp
; envp
++)
1505 Vprocess_environment
= Fcons (build_string (*envp
),
1506 Vprocess_environment
);
1513 Qbuffer_file_type
= intern ("buffer-file-type");
1514 staticpro (&Qbuffer_file_type
);
1517 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1518 "*File name to load inferior shells from.\n\
1519 Initialized from the SHELL environment variable.");
1521 DEFVAR_LISP ("exec-path", &Vexec_path
,
1522 "*List of directories to search programs to run in subprocesses.\n\
1523 Each element is a string (directory name) or nil (try default directory).");
1525 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1526 "Directory for executables for Emacs to invoke.\n\
1527 More generally, this includes any architecture-dependent files\n\
1528 that are built and installed from the Emacs distribution.");
1530 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1531 "Directory of machine-independent files that come with GNU Emacs.\n\
1532 These are files intended for Emacs to use while it runs.");
1534 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1535 "Directory containing the DOC file that comes with GNU Emacs.\n\
1536 This is usually the same as data-directory.");
1538 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1539 "For internal use by the build procedure only.\n\
1540 This is the name of the directory in which the build procedure installed\n\
1541 Emacs's info files; the default value for Info-default-directory-list\n\
1543 Vconfigure_info_directory
= build_string (PATH_INFO
);
1545 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1546 "Pattern for making names for temporary files.\n\
1547 This is used by `call-process-region'.");
1548 /* This variable is initialized in init_callproc. */
1550 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1551 "List of environment variables for subprocesses to inherit.\n\
1552 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1553 The environment which Emacs inherits is placed in this variable\n\
1554 when Emacs starts.");
1557 defsubr (&Scall_process
);
1558 defsubr (&Sgetenv_internal
);
1560 defsubr (&Scall_process_region
);