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 filename_string
= build_string (tempfile
);
973 GCPRO1 (filename_string
);
976 /* Decide coding-system of the contents of the temporary file. */
977 if (!NILP (Vcoding_system_for_write
))
978 val
= Vcoding_system_for_write
;
979 else if (NILP (current_buffer
->enable_multibyte_characters
))
983 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
984 args2
[0] = Qcall_process_region
;
985 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
986 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
987 if (CONSP (coding_systems
))
988 val
= XCDR (coding_systems
);
989 else if (CONSP (Vdefault_process_coding_system
))
990 val
= XCDR (Vdefault_process_coding_system
);
996 int count1
= specpdl_ptr
- specpdl
;
998 specbind (intern ("coding-system-for-write"), val
);
999 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1001 unbind_to (count1
, Qnil
);
1004 /* Note that Fcall_process takes care of binding
1005 coding-system-for-read. */
1007 record_unwind_protect (delete_temp_file
, filename_string
);
1009 if (nargs
> 3 && !NILP (args
[3]))
1010 Fdelete_region (start
, end
);
1022 args
[1] = filename_string
;
1024 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1027 #ifndef VMS /* VMS version is in vmsproc.c. */
1029 static int relocate_fd ();
1031 /* This is the last thing run in a newly forked inferior
1032 either synchronous or asynchronous.
1033 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1034 Initialize inferior's priority, pgrp, connected dir and environment.
1035 then exec another program based on new_argv.
1037 This function may change environ for the superior process.
1038 Therefore, the superior process must save and restore the value
1039 of environ around the vfork and the call to this function.
1041 SET_PGRP is nonzero if we should put the subprocess into a separate
1044 CURRENT_DIR is an elisp string giving the path of the current
1045 directory the subprocess should have. Since we can't really signal
1046 a decent error from within the child, this should be verified as an
1047 executable directory by the parent. */
1050 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1052 register char **new_argv
;
1054 Lisp_Object current_dir
;
1061 #endif /* WINDOWSNT */
1063 int pid
= getpid ();
1065 #ifdef SET_EMACS_PRIORITY
1067 extern int emacs_priority
;
1069 if (emacs_priority
< 0)
1070 nice (- emacs_priority
);
1075 /* Close Emacs's descriptors that this process should not have. */
1076 close_process_descs ();
1078 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1079 we will lose if we call close_load_descs here. */
1081 close_load_descs ();
1084 /* Note that use of alloca is always safe here. It's obvious for systems
1085 that do not have true vfork or that have true (stack) alloca.
1086 If using vfork and C_ALLOCA it is safe because that changes
1087 the superior's static variables as if the superior had done alloca
1088 and will be cleaned up in the usual way. */
1090 register char *temp
;
1093 i
= STRING_BYTES (XSTRING (current_dir
));
1095 /* MSDOS must have all environment variables malloc'ed, because
1096 low-level libc functions that launch subsidiary processes rely
1098 pwd_var
= (char *) xmalloc (i
+ 6);
1100 pwd_var
= (char *) alloca (i
+ 6);
1103 bcopy ("PWD=", pwd_var
, 4);
1104 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
1105 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1109 /* We can't signal an Elisp error here; we're in a vfork. Since
1110 the callers check the current directory before forking, this
1111 should only return an error if the directory's permissions
1112 are changed between the check and this chdir, but we should
1114 if (chdir (temp
) < 0)
1119 /* Get past the drive letter, so that d:/ is left alone. */
1120 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1127 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1128 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1132 /* Set `env' to a vector of the strings in Vprocess_environment. */
1134 register Lisp_Object tem
;
1135 register char **new_env
;
1136 register int new_length
;
1139 for (tem
= Vprocess_environment
;
1140 CONSP (tem
) && STRINGP (XCAR (tem
));
1144 /* new_length + 2 to include PWD and terminating 0. */
1145 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1147 /* If we have a PWD envvar, pass one down,
1148 but with corrected value. */
1150 *new_env
++ = pwd_var
;
1152 /* Copy the Vprocess_environment strings into new_env. */
1153 for (tem
= Vprocess_environment
;
1154 CONSP (tem
) && STRINGP (XCAR (tem
));
1158 char *string
= (char *) XSTRING (XCAR (tem
))->data
;
1159 /* See if this string duplicates any string already in the env.
1160 If so, don't put it in.
1161 When an env var has multiple definitions,
1162 we keep the definition that comes first in process-environment. */
1163 for (; ep
!= new_env
; ep
++)
1165 char *p
= *ep
, *q
= string
;
1169 /* The string is malformed; might as well drop it. */
1178 *new_env
++ = string
;
1184 prepare_standard_handles (in
, out
, err
, handles
);
1185 set_process_dir (XSTRING (current_dir
)->data
);
1186 #else /* not WINDOWSNT */
1187 /* Make sure that in, out, and err are not actually already in
1188 descriptors zero, one, or two; this could happen if Emacs is
1189 started with its standard in, out, or error closed, as might
1192 int oin
= in
, oout
= out
;
1194 /* We have to avoid relocating the same descriptor twice! */
1196 in
= relocate_fd (in
, 3);
1201 out
= relocate_fd (out
, 3);
1205 else if (err
== oout
)
1208 err
= relocate_fd (err
, 3);
1222 #endif /* not MSDOS */
1223 #endif /* not WINDOWSNT */
1225 #if defined(USG) && !defined(BSD_PGRPS)
1226 #ifndef SETPGRP_RELEASES_CTTY
1227 setpgrp (); /* No arguments but equivalent in this case */
1232 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1233 EMACS_SET_TTY_PGRP (0, &pid
);
1236 something missing here
;
1240 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1243 /* An error occurred while trying to run the subprocess. */
1244 report_file_error ("Spawning child process", Qnil
);
1246 #else /* not MSDOS */
1248 /* Spawn the child. (See ntproc.c:Spawnve). */
1249 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1250 reset_standard_handles (in
, out
, err
, handles
);
1252 /* An error occurred while trying to spawn the process. */
1253 report_file_error ("Spawning child process", Qnil
);
1255 #else /* not WINDOWSNT */
1256 /* execvp does not accept an environment arg so the only way
1257 to pass this environment is to set environ. Our caller
1258 is responsible for restoring the ambient value of environ. */
1260 execvp (new_argv
[0], new_argv
);
1262 emacs_write (1, "Can't exec program: ", 20);
1263 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1264 emacs_write (1, "\n", 1);
1266 #endif /* not WINDOWSNT */
1267 #endif /* not MSDOS */
1270 /* Move the file descriptor FD so that its number is not less than MINFD.
1271 If the file descriptor is moved at all, the original is freed. */
1273 relocate_fd (fd
, minfd
)
1283 char *message1
= "Error while setting up child: ";
1284 char *errmessage
= strerror (errno
);
1285 char *message2
= "\n";
1286 emacs_write (2, message1
, strlen (message1
));
1287 emacs_write (2, errmessage
, strlen (errmessage
));
1288 emacs_write (2, message2
, strlen (message2
));
1291 /* Note that we hold the original FD open while we recurse,
1292 to guarantee we'll get a new FD if we need it. */
1293 new = relocate_fd (new, minfd
);
1300 getenv_internal (var
, varlen
, value
, valuelen
)
1308 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1312 entry
= XCAR (scan
);
1314 && STRING_BYTES (XSTRING (entry
)) > varlen
1315 && XSTRING (entry
)->data
[varlen
] == '='
1317 /* NT environment variables are case insensitive. */
1318 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1319 #else /* not WINDOWSNT */
1320 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1321 #endif /* not WINDOWSNT */
1324 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1325 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1333 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1334 "Return the value of environment variable VAR, as a string.\n\
1335 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1336 This function consults the variable ``process-environment'' for its value.")
1343 CHECK_STRING (var
, 0);
1344 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1346 return make_string (value
, valuelen
);
1351 /* A version of getenv that consults process_environment, easily
1360 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1366 #endif /* not VMS */
1368 /* This is run before init_cmdargs. */
1373 char *data_dir
= egetenv ("EMACSDATA");
1374 char *doc_dir
= egetenv ("EMACSDOC");
1377 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1380 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1383 /* Check the EMACSPATH environment variable, defaulting to the
1384 PATH_EXEC path from epaths.h. */
1385 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1386 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1387 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1390 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1395 char *data_dir
= egetenv ("EMACSDATA");
1398 Lisp_Object tempdir
;
1400 if (!NILP (Vinstallation_directory
))
1402 /* Add to the path the lib-src subdir of the installation dir. */
1404 tem
= Fexpand_file_name (build_string ("lib-src"),
1405 Vinstallation_directory
);
1407 /* MSDOS uses wrapped binaries, so don't do this. */
1408 if (NILP (Fmember (tem
, Vexec_path
)))
1409 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1411 Vexec_directory
= Ffile_name_as_directory (tem
);
1412 #endif /* not DOS_NT */
1414 /* Maybe use ../etc as well as ../lib-src. */
1417 tem
= Fexpand_file_name (build_string ("etc"),
1418 Vinstallation_directory
);
1419 Vdoc_directory
= Ffile_name_as_directory (tem
);
1423 /* Look for the files that should be in etc. We don't use
1424 Vinstallation_directory, because these files are never installed
1425 near the executable, and they are never in the build
1426 directory when that's different from the source directory.
1428 Instead, if these files are not in the nominal place, we try the
1429 source directory. */
1432 Lisp_Object tem
, tem1
, newdir
;
1434 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1435 tem1
= Ffile_exists_p (tem
);
1438 newdir
= Fexpand_file_name (build_string ("../etc/"),
1439 build_string (PATH_DUMPLOADSEARCH
));
1440 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1441 tem1
= Ffile_exists_p (tem
);
1443 Vdata_directory
= newdir
;
1451 tempdir
= Fdirectory_file_name (Vexec_directory
);
1452 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1453 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1457 tempdir
= Fdirectory_file_name (Vdata_directory
);
1458 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1459 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1463 Vshell_file_name
= build_string ("*dcl*");
1465 sh
= (char *) getenv ("SHELL");
1466 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1470 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1472 if (getenv ("TMPDIR"))
1474 char *dir
= getenv ("TMPDIR");
1475 Vtemp_file_name_pattern
1476 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1477 build_string (dir
));
1480 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1485 set_process_environment ()
1487 register char **envp
;
1489 Vprocess_environment
= Qnil
;
1493 for (envp
= environ
; *envp
; envp
++)
1494 Vprocess_environment
= Fcons (build_string (*envp
),
1495 Vprocess_environment
);
1502 Qbuffer_file_type
= intern ("buffer-file-type");
1503 staticpro (&Qbuffer_file_type
);
1506 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1507 "*File name to load inferior shells from.\n\
1508 Initialized from the SHELL environment variable.");
1510 DEFVAR_LISP ("exec-path", &Vexec_path
,
1511 "*List of directories to search programs to run in subprocesses.\n\
1512 Each element is a string (directory name) or nil (try default directory).");
1514 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1515 "Directory for executables for Emacs to invoke.\n\
1516 More generally, this includes any architecture-dependent files\n\
1517 that are built and installed from the Emacs distribution.");
1519 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1520 "Directory of machine-independent files that come with GNU Emacs.\n\
1521 These are files intended for Emacs to use while it runs.");
1523 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1524 "Directory containing the DOC file that comes with GNU Emacs.\n\
1525 This is usually the same as data-directory.");
1527 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1528 "For internal use by the build procedure only.\n\
1529 This is the name of the directory in which the build procedure installed\n\
1530 Emacs's info files; the default value for Info-default-directory-list\n\
1532 Vconfigure_info_directory
= build_string (PATH_INFO
);
1534 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1535 "Pattern for making names for temporary files.\n\
1536 This is used by `call-process-region'.");
1537 /* This variable is initialized in init_callproc. */
1539 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1540 "List of environment variables for subprocesses to inherit.\n\
1541 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1542 The environment which Emacs inherits is placed in this variable\n\
1543 when Emacs starts.");
1546 defsubr (&Scall_process
);
1547 defsubr (&Sgetenv_internal
);
1549 defsubr (&Scall_process_region
);