1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
32 /* Define SIGCHLD as an alias for SIGCLD. */
34 #if !defined (SIGCHLD) && defined (SIGCLD)
35 #define SIGCHLD SIGCLD
38 #include <sys/types.h>
46 #define INCLUDED_FCNTL
53 #include <stdlib.h> /* for proper declaration of environ */
56 #define _P_NOWAIT 1 /* from process.h */
59 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
60 #define INCLUDED_FCNTL
63 #include <sys/param.h>
78 #include "character.h"
81 #include "composite.h"
84 #include "syssignal.h"
86 #include "blockinput.h"
93 extern noshare
char **environ
;
96 extern char **environ
;
101 #if !defined (USG) || defined (BSD_PGRPS)
103 #define setpgrp setpgid
107 Lisp_Object Vexec_path
, Vexec_directory
, Vexec_suffixes
;
108 Lisp_Object Vdata_directory
, Vdoc_directory
;
109 Lisp_Object Vconfigure_info_directory
, Vshared_game_score_directory
;
110 Lisp_Object Vtemp_file_name_pattern
;
112 Lisp_Object Vshell_file_name
;
114 Lisp_Object Vprocess_environment
;
117 Lisp_Object Qbuffer_file_type
;
120 /* True iff we are about to fork off a synchronous process or if we
121 are waiting for it. */
122 int synch_process_alive
;
124 /* Nonzero => this is a string explaining death of synchronous subprocess. */
125 char *synch_process_death
;
127 /* Nonzero => this is the signal number that terminated the subprocess. */
128 int synch_process_termsig
;
130 /* If synch_process_death is zero,
131 this is exit code of synchronous subprocess. */
132 int synch_process_retcode
;
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 (MAC_OS8)
158 /* for MSDOS fdpid is really (fd . tempfile) */
159 register Lisp_Object file
;
161 emacs_close (XFASTINT (Fcar (fdpid
)));
162 if (strcmp (SDATA (file
), NULL_DEVICE
) != 0)
163 unlink (SDATA (file
));
164 #else /* not MSDOS and not MAC_OS8 */
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_INDEX ();
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 doc
: /* Call PROGRAM synchronously in separate process.
193 The remaining arguments are optional.
194 The program's input comes from file INFILE (nil means `/dev/null').
195 Insert output in BUFFER before point; t means current buffer;
196 nil for BUFFER means discard it; 0 means discard and don't wait.
197 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
198 REAL-BUFFER says what to do with standard output, as above,
199 while STDERR-FILE says what to do with standard error in the child.
200 STDERR-FILE may be nil (discard standard error output),
201 t (mix it with ordinary output), or a file name string.
203 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
204 Remaining arguments are strings passed as command arguments to PROGRAM.
206 If executable PROGRAM can't be found as an executable, `call-process'
207 signals a Lisp error. `call-process' reports errors in execution of
208 the program only through its return and output.
210 If BUFFER is 0, `call-process' returns immediately with value nil.
211 Otherwise it waits for PROGRAM to terminate
212 and returns a numeric exit status or a signal description string.
213 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
215 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
218 register Lisp_Object
*args
;
220 Lisp_Object infile
, buffer
, current_dir
, path
;
225 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
226 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
227 char buf
[CALLPROC_BUFFER_SIZE_MAX
];
228 int bufsize
= CALLPROC_BUFFER_SIZE_MIN
;
229 int count
= SPECPDL_INDEX ();
231 register const unsigned char **new_argv
232 = (const unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
233 struct buffer
*old
= current_buffer
;
234 /* File to use for stderr in the child.
235 t means use same as standard output. */
236 Lisp_Object error_file
;
237 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
238 char *outf
, *tempfile
;
248 struct coding_system process_coding
; /* coding-system of process output */
249 struct coding_system argument_coding
; /* coding-system of arguments */
250 /* Set to the return value of Ffind_operation_coding_system. */
251 Lisp_Object coding_systems
;
253 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
256 CHECK_STRING (args
[0]);
261 /* Without asynchronous processes we cannot have BUFFER == 0. */
263 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
264 error ("Operating system cannot handle asynchronous subprocesses");
265 #endif /* subprocesses */
267 /* Decide the coding-system for giving arguments. */
269 Lisp_Object val
, *args2
;
272 /* If arguments are supplied, we may have to encode them. */
276 Lisp_Object coding_attrs
;
278 for (i
= 4; i
< nargs
; i
++)
279 CHECK_STRING (args
[i
]);
281 for (i
= 4; i
< nargs
; i
++)
282 if (STRING_MULTIBYTE (args
[i
]))
285 if (!NILP (Vcoding_system_for_write
))
286 val
= Vcoding_system_for_write
;
287 else if (! must_encode
)
291 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
292 args2
[0] = Qcall_process
;
293 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
294 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
295 if (CONSP (coding_systems
))
296 val
= XCDR (coding_systems
);
297 else if (CONSP (Vdefault_process_coding_system
))
298 val
= XCDR (Vdefault_process_coding_system
);
302 val
= coding_inherit_eol_type (val
, Qnil
);
303 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
304 coding_attrs
= CODING_ID_ATTRS (argument_coding
.id
);
305 if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs
)))
307 /* We should not use an ASCII incompatible coding system. */
308 val
= raw_text_coding_system (val
);
309 setup_coding_system (val
, &argument_coding
);
314 if (nargs
>= 2 && ! NILP (args
[1]))
316 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
317 CHECK_STRING (infile
);
320 infile
= build_string (NULL_DEVICE
);
326 /* If BUFFER is a list, its meaning is
327 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
330 if (CONSP (XCDR (buffer
)))
332 Lisp_Object stderr_file
;
333 stderr_file
= XCAR (XCDR (buffer
));
335 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
336 error_file
= stderr_file
;
338 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
341 buffer
= XCAR (buffer
);
344 if (!(EQ (buffer
, Qnil
)
346 || INTEGERP (buffer
)))
348 Lisp_Object spec_buffer
;
349 spec_buffer
= buffer
;
350 buffer
= Fget_buffer_create (buffer
);
351 /* Mention the buffer name for a better error message. */
353 CHECK_BUFFER (spec_buffer
);
354 CHECK_BUFFER (buffer
);
360 /* Make sure that the child will be able to chdir to the current
361 buffer's current directory, or its unhandled equivalent. We
362 can't just have the child check for an error when it does the
363 chdir, since it's in a vfork.
365 We have to GCPRO around this because Fexpand_file_name,
366 Funhandled_file_name_directory, and Ffile_accessible_directory_p
367 might call a file name handling function. The argument list is
368 protected by the caller, so all we really have to worry about is
371 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
373 current_dir
= current_buffer
->directory
;
375 GCPRO4 (infile
, buffer
, current_dir
, error_file
);
378 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
380 if (NILP (Ffile_accessible_directory_p (current_dir
)))
381 report_file_error ("Setting current directory",
382 Fcons (current_buffer
->directory
, Qnil
));
384 if (STRING_MULTIBYTE (infile
))
385 infile
= ENCODE_FILE (infile
);
386 if (STRING_MULTIBYTE (current_dir
))
387 current_dir
= ENCODE_FILE (current_dir
);
388 if (STRINGP (error_file
) && STRING_MULTIBYTE (error_file
))
389 error_file
= ENCODE_FILE (error_file
);
393 display_p
= INTERACTIVE
&& nargs
>= 4 && !NILP (args
[3]);
395 filefd
= emacs_open (SDATA (infile
), O_RDONLY
, 0);
398 infile
= DECODE_FILE (infile
);
399 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
401 /* Search for program; barf if not found. */
405 GCPRO1 (current_dir
);
406 openp (Vexec_path
, args
[0], Vexec_suffixes
, &path
, make_number (X_OK
));
411 emacs_close (filefd
);
412 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
415 /* If program file name starts with /: for quoting a magic name,
417 if (SBYTES (path
) > 2 && SREF (path
, 0) == '/'
418 && SREF (path
, 1) == ':')
419 path
= Fsubstring (path
, make_number (2), Qnil
);
421 new_argv
[0] = SDATA (path
);
425 struct gcpro gcpro1
, gcpro2
, gcpro3
;
427 GCPRO3 (infile
, buffer
, current_dir
);
428 argument_coding
.dst_multibyte
= 0;
429 for (i
= 4; i
< nargs
; i
++)
431 argument_coding
.src_multibyte
= STRING_MULTIBYTE (args
[i
]);
432 if (CODING_REQUIRE_ENCODING (&argument_coding
))
433 /* We must encode this argument. */
434 args
[i
] = encode_coding_string (&argument_coding
, args
[i
], 1);
435 new_argv
[i
- 3] = SDATA (args
[i
]);
438 new_argv
[nargs
- 3] = 0;
443 #ifdef MSDOS /* MW, July 1993 */
444 if ((outf
= egetenv ("TMPDIR")))
445 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
448 tempfile
= alloca (20);
451 dostounix_filename (tempfile
);
452 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
453 strcat (tempfile
, "/");
454 strcat (tempfile
, "detmp.XXX");
457 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
460 emacs_close (filefd
);
461 report_file_error ("Opening process output file",
462 Fcons (build_string (tempfile
), Qnil
));
469 /* Since we don't have pipes on the Mac, create a temporary file to
470 hold the output of the subprocess. */
471 tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
472 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
473 SBYTES (Vtemp_file_name_pattern
) + 1);
477 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
481 report_file_error ("Opening process output file",
482 Fcons (build_string (tempfile
), Qnil
));
488 if (INTEGERP (buffer
))
489 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
497 emacs_close (filefd
);
498 report_file_error ("Creating process pipe", Qnil
);
503 /* Replaced by close_process_descs */
504 set_exclusive_use (fd
[0]);
509 /* child_setup must clobber environ in systems with true vfork.
510 Protect it from permanent change. */
511 register char **save_environ
= environ
;
512 register int fd1
= fd
[1];
515 #if 0 /* Some systems don't have sigblock. */
516 mask
= sigblock (sigmask (SIGCHLD
));
519 /* Record that we're about to create a synchronous process. */
520 synch_process_alive
= 1;
522 /* These vars record information from process termination.
523 Clear them now before process can possibly terminate,
524 to avoid timing error if process terminates soon. */
525 synch_process_death
= 0;
526 synch_process_retcode
= 0;
527 synch_process_termsig
= 0;
529 if (NILP (error_file
))
530 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
531 else if (STRINGP (error_file
))
534 fd_error
= emacs_open (SDATA (error_file
),
535 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
537 #else /* not DOS_NT */
538 fd_error
= creat (SDATA (error_file
), 0666);
539 #endif /* not DOS_NT */
544 emacs_close (filefd
);
552 if (NILP (error_file
))
553 error_file
= build_string (NULL_DEVICE
);
554 else if (STRINGP (error_file
))
555 error_file
= DECODE_FILE (error_file
);
556 report_file_error ("Cannot redirect stderr", Fcons (error_file
, Qnil
));
561 /* Call run_mac_command in sysdep.c here directly instead of doing
562 a child_setup as for MSDOS and other platforms. Note that this
563 code does not handle passing the environment to the synchronous
565 char *infn
, *outfn
, *errfn
, *currdn
;
567 /* close these files so subprocess can write to them */
569 if (fd_error
!= outfilefd
)
571 fd1
= -1; /* No harm in closing that one! */
573 infn
= SDATA (infile
);
575 if (NILP (error_file
))
577 else if (EQ (Qt
, error_file
))
580 errfn
= SDATA (error_file
);
581 currdn
= SDATA (current_dir
);
582 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
584 /* Record that the synchronous process exited and note its
585 termination status. */
586 synch_process_alive
= 0;
587 synch_process_retcode
= pid
;
588 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
590 synchronize_system_messages_locale ();
591 synch_process_death
= strerror (errno
);
594 /* Since CRLF is converted to LF within `decode_coding', we can
595 always open a file with binary mode. */
596 fd
[0] = open (tempfile
, O_BINARY
);
601 report_file_error ("Cannot re-open temporary file", Qnil
);
604 #else /* not MAC_OS8 */
605 #ifdef MSDOS /* MW, July 1993 */
606 /* Note that on MSDOS `child_setup' actually returns the child process
607 exit status, not its PID, so we assign it to `synch_process_retcode'
609 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
612 /* Record that the synchronous process exited and note its
613 termination status. */
614 synch_process_alive
= 0;
615 synch_process_retcode
= pid
;
616 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
618 synchronize_system_messages_locale ();
619 synch_process_death
= strerror (errno
);
622 emacs_close (outfilefd
);
623 if (fd_error
!= outfilefd
)
624 emacs_close (fd_error
);
625 fd1
= -1; /* No harm in closing that one! */
626 /* Since CRLF is converted to LF within `decode_coding', we can
627 always open a file with binary mode. */
628 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
632 emacs_close (filefd
);
633 report_file_error ("Cannot re-open temporary file", Qnil
);
635 #else /* not MSDOS */
637 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
639 #else /* not WINDOWSNT */
651 #if defined (USG) && !defined (BSD_PGRPS)
656 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
661 #endif /* not WINDOWSNT */
663 /* The MSDOS case did this already. */
665 emacs_close (fd_error
);
666 #endif /* not MSDOS */
667 #endif /* not MAC_OS8 */
669 environ
= save_environ
;
671 /* Close most of our fd's, but not fd[0]
672 since we will use that to read input from. */
673 emacs_close (filefd
);
674 if (fd1
>= 0 && fd1
!= fd_error
)
682 report_file_error ("Doing vfork", Qnil
);
685 if (INTEGERP (buffer
))
690 /* If Emacs has been built with asynchronous subprocess support,
691 we don't need to do this, I think because it will then have
692 the facilities for handling SIGCHLD. */
693 wait_without_blocking ();
694 #endif /* subprocesses */
698 /* Enable sending signal if user quits below. */
699 call_process_exited
= 0;
701 #if defined(MSDOS) || defined(MAC_OS8)
702 /* MSDOS needs different cleanup information. */
703 record_unwind_protect (call_process_cleanup
,
704 Fcons (make_number (fd
[0]), build_string (tempfile
)));
706 record_unwind_protect (call_process_cleanup
,
707 Fcons (make_number (fd
[0]), make_number (pid
)));
708 #endif /* not MSDOS and not MAC_OS8 */
711 if (BUFFERP (buffer
))
712 Fset_buffer (buffer
);
716 /* If BUFFER is nil, we must read process output once and then
717 discard it, so setup coding system but with nil. */
718 setup_coding_system (Qnil
, &process_coding
);
722 Lisp_Object val
, *args2
;
725 if (!NILP (Vcoding_system_for_read
))
726 val
= Vcoding_system_for_read
;
729 if (EQ (coding_systems
, Qt
))
733 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
734 args2
[0] = Qcall_process
;
735 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
737 = Ffind_operation_coding_system (nargs
+ 1, args2
);
739 if (CONSP (coding_systems
))
740 val
= XCAR (coding_systems
);
741 else if (CONSP (Vdefault_process_coding_system
))
742 val
= XCAR (Vdefault_process_coding_system
);
746 Fcheck_coding_system (val
);
747 /* In unibyte mode, character code conversion should not take
748 place but EOL conversion should. So, setup raw-text or one
749 of the subsidiary according to the information just setup. */
750 if (NILP (current_buffer
->enable_multibyte_characters
)
752 val
= raw_text_coding_system (val
);
753 setup_coding_system (val
, &process_coding
);
764 int display_on_the_fly
= display_p
;
765 struct coding_system saved_coding
;
767 saved_coding
= process_coding
;
770 /* Repeatedly read until we've filled as much as possible
771 of the buffer size we have. But don't read
772 less than 1024--save that for the next bufferful. */
774 while (nread
< bufsize
- 1024)
776 int this_read
= emacs_read (fd
[0], buf
+ nread
,
784 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
789 total_read
+= this_read
;
791 if (display_on_the_fly
)
795 /* Now NREAD is the total amount of data in the buffer. */
800 if (NILP (current_buffer
->enable_multibyte_characters
)
801 && ! CODING_MAY_REQUIRE_DECODING (&process_coding
))
802 insert_1_both (buf
, nread
, nread
, 0, 1, 0);
804 { /* We have to decode the input. */
807 XSETBUFFER (curbuf
, current_buffer
);
808 decode_coding_c_string (&process_coding
, buf
, nread
,
810 if (display_on_the_fly
811 && CODING_REQUIRE_DETECTION (&saved_coding
)
812 && ! CODING_REQUIRE_DETECTION (&process_coding
))
814 /* We have detected some coding system. But,
815 there's a possibility that the detection was
816 done by insufficient data. So, we give up
817 displaying on the fly. */
818 if (process_coding
.produced
> 0)
819 del_range_2 (process_coding
.dst_pos
,
820 process_coding
.dst_pos_byte
,
821 process_coding
.dst_pos
822 + process_coding
.produced_char
,
823 process_coding
.dst_pos_byte
824 + process_coding
.produced
, 0);
825 display_on_the_fly
= 0;
826 process_coding
= saved_coding
;
828 /* This is to make the above condition always
829 fails in the future. */
830 saved_coding
.common_flags
831 &= ~CODING_REQUIRE_DETECTION_MASK
;
835 TEMP_SET_PT_BOTH (PT
+ process_coding
.produced_char
,
836 PT_BYTE
+ process_coding
.produced
);
837 carryover
= process_coding
.carryover_bytes
;
839 /* As CARRYOVER should not be that large, we had
840 better avoid overhead of bcopy. */
841 BCOPY_SHORT (process_coding
.carryover
, buf
,
842 process_coding
.carryover_bytes
);
846 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
849 #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
850 /* Make the buffer bigger as we continue to read more data,
851 but not past CALLPROC_BUFFER_SIZE_MAX. */
852 if (bufsize
< CALLPROC_BUFFER_SIZE_MAX
&& total_read
> 32 * bufsize
)
853 if ((bufsize
*= 2) > CALLPROC_BUFFER_SIZE_MAX
)
854 bufsize
= CALLPROC_BUFFER_SIZE_MAX
;
860 prepare_menu_bars ();
862 redisplay_preserve_echo_area (1);
863 /* This variable might have been set to 0 for code
864 detection. In that case, we set it back to 1 because
865 we should have already detected a coding system. */
866 display_on_the_fly
= 1;
873 Vlast_coding_system_used
= CODING_ID_NAME (process_coding
.id
);
874 /* If the caller required, let the buffer inherit the
875 coding-system used to decode the process output. */
876 if (inherit_process_coding_system
)
877 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
878 make_number (total_read
));
881 /* Wait for it to terminate, unless it already has. */
882 wait_for_termination (pid
);
886 set_buffer_internal (old
);
888 /* Don't kill any children that the subprocess may have left behind
890 call_process_exited
= 1;
892 unbind_to (count
, Qnil
);
894 if (synch_process_termsig
)
898 synchronize_system_messages_locale ();
899 signame
= strsignal (synch_process_termsig
);
904 synch_process_death
= signame
;
907 if (synch_process_death
)
908 return code_convert_string_norecord (build_string (synch_process_death
),
909 Vlocale_coding_system
, 0);
910 return make_number (synch_process_retcode
);
915 delete_temp_file (name
)
918 /* Suppress jka-compr handling, etc. */
919 int count
= SPECPDL_INDEX ();
920 specbind (intern ("file-name-handler-alist"), Qnil
);
921 internal_delete_file (name
);
922 unbind_to (count
, Qnil
);
926 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
928 doc
: /* Send text from START to END to a synchronous process running PROGRAM.
929 The remaining arguments are optional.
930 Delete the text if fourth arg DELETE is non-nil.
932 Insert output in BUFFER before point; t means current buffer;
933 nil for BUFFER means discard it; 0 means discard and don't wait.
934 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
935 REAL-BUFFER says what to do with standard output, as above,
936 while STDERR-FILE says what to do with standard error in the child.
937 STDERR-FILE may be nil (discard standard error output),
938 t (mix it with ordinary output), or a file name string.
940 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
941 Remaining args are passed to PROGRAM at startup as command args.
943 If BUFFER is 0, `call-process-region' returns immediately with value nil.
944 Otherwise it waits for PROGRAM to terminate
945 and returns a numeric exit status or a signal description string.
946 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
948 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
951 register Lisp_Object
*args
;
954 Lisp_Object filename_string
;
955 register Lisp_Object start
, end
;
956 int count
= SPECPDL_INDEX ();
957 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
958 Lisp_Object coding_systems
;
959 Lisp_Object val
, *args2
;
965 if ((outf
= egetenv ("TMPDIR"))
966 || (outf
= egetenv ("TMP"))
967 || (outf
= egetenv ("TEMP")))
968 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
971 tempfile
= alloca (20);
974 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
975 strcat (tempfile
, "/");
976 if ('/' == DIRECTORY_SEP
)
977 dostounix_filename (tempfile
);
979 unixtodos_filename (tempfile
);
981 strcat (tempfile
, "emXXXXXX");
983 strcat (tempfile
, "detmp.XXX");
985 #else /* not DOS_NT */
986 char *tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
987 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
988 SBYTES (Vtemp_file_name_pattern
) + 1);
989 #endif /* not DOS_NT */
998 fd
= mkstemp (tempfile
);
1001 report_file_error ("Failed to open temporary file",
1002 Fcons (Vtemp_file_name_pattern
, Qnil
));
1010 filename_string
= build_string (tempfile
);
1011 GCPRO1 (filename_string
);
1014 /* Decide coding-system of the contents of the temporary file. */
1015 if (!NILP (Vcoding_system_for_write
))
1016 val
= Vcoding_system_for_write
;
1017 else if (NILP (current_buffer
->enable_multibyte_characters
))
1021 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1022 args2
[0] = Qcall_process_region
;
1023 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1024 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1025 if (CONSP (coding_systems
))
1026 val
= XCDR (coding_systems
);
1027 else if (CONSP (Vdefault_process_coding_system
))
1028 val
= XCDR (Vdefault_process_coding_system
);
1034 int count1
= SPECPDL_INDEX ();
1036 specbind (intern ("coding-system-for-write"), val
);
1037 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
1038 happen to get a ".Z" suffix. */
1039 specbind (intern ("file-name-handler-alist"), Qnil
);
1040 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1042 unbind_to (count1
, Qnil
);
1045 /* Note that Fcall_process takes care of binding
1046 coding-system-for-read. */
1048 record_unwind_protect (delete_temp_file
, filename_string
);
1050 if (nargs
> 3 && !NILP (args
[3]))
1051 Fdelete_region (start
, end
);
1063 args
[1] = filename_string
;
1065 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1068 #ifndef VMS /* VMS version is in vmsproc.c. */
1070 static int relocate_fd ();
1072 /* This is the last thing run in a newly forked inferior
1073 either synchronous or asynchronous.
1074 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1075 Initialize inferior's priority, pgrp, connected dir and environment.
1076 then exec another program based on new_argv.
1078 This function may change environ for the superior process.
1079 Therefore, the superior process must save and restore the value
1080 of environ around the vfork and the call to this function.
1082 SET_PGRP is nonzero if we should put the subprocess into a separate
1085 CURRENT_DIR is an elisp string giving the path of the current
1086 directory the subprocess should have. Since we can't really signal
1087 a decent error from within the child, this should be verified as an
1088 executable directory by the parent. */
1091 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1093 register char **new_argv
;
1095 Lisp_Object current_dir
;
1102 #endif /* WINDOWSNT */
1104 int pid
= getpid ();
1106 #ifdef SET_EMACS_PRIORITY
1108 extern EMACS_INT emacs_priority
;
1110 if (emacs_priority
< 0)
1111 nice (- emacs_priority
);
1116 /* Close Emacs's descriptors that this process should not have. */
1117 close_process_descs ();
1119 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1120 we will lose if we call close_load_descs here. */
1122 close_load_descs ();
1125 /* Note that use of alloca is always safe here. It's obvious for systems
1126 that do not have true vfork or that have true (stack) alloca.
1127 If using vfork and C_ALLOCA it is safe because that changes
1128 the superior's static variables as if the superior had done alloca
1129 and will be cleaned up in the usual way. */
1131 register char *temp
;
1134 i
= SBYTES (current_dir
);
1136 /* MSDOS must have all environment variables malloc'ed, because
1137 low-level libc functions that launch subsidiary processes rely
1139 pwd_var
= (char *) xmalloc (i
+ 6);
1141 pwd_var
= (char *) alloca (i
+ 6);
1144 bcopy ("PWD=", pwd_var
, 4);
1145 bcopy (SDATA (current_dir
), temp
, i
);
1146 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1150 /* We can't signal an Elisp error here; we're in a vfork. Since
1151 the callers check the current directory before forking, this
1152 should only return an error if the directory's permissions
1153 are changed between the check and this chdir, but we should
1155 if (chdir (temp
) < 0)
1160 /* Get past the drive letter, so that d:/ is left alone. */
1161 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1168 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1169 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1173 /* Set `env' to a vector of the strings in Vprocess_environment. */
1175 register Lisp_Object tem
;
1176 register char **new_env
;
1177 register int new_length
;
1180 for (tem
= Vprocess_environment
;
1181 CONSP (tem
) && STRINGP (XCAR (tem
));
1185 /* new_length + 2 to include PWD and terminating 0. */
1186 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1188 /* If we have a PWD envvar, pass one down,
1189 but with corrected value. */
1191 *new_env
++ = pwd_var
;
1193 /* Copy the Vprocess_environment strings into new_env. */
1194 for (tem
= Vprocess_environment
;
1195 CONSP (tem
) && STRINGP (XCAR (tem
));
1199 char *string
= (char *) SDATA (XCAR (tem
));
1200 /* See if this string duplicates any string already in the env.
1201 If so, don't put it in.
1202 When an env var has multiple definitions,
1203 we keep the definition that comes first in process-environment. */
1204 for (; ep
!= new_env
; ep
++)
1206 char *p
= *ep
, *q
= string
;
1210 /* The string is malformed; might as well drop it. */
1219 *new_env
++ = string
;
1225 prepare_standard_handles (in
, out
, err
, handles
);
1226 set_process_dir (SDATA (current_dir
));
1227 #else /* not WINDOWSNT */
1228 /* Make sure that in, out, and err are not actually already in
1229 descriptors zero, one, or two; this could happen if Emacs is
1230 started with its standard in, out, or error closed, as might
1233 int oin
= in
, oout
= out
;
1235 /* We have to avoid relocating the same descriptor twice! */
1237 in
= relocate_fd (in
, 3);
1242 out
= relocate_fd (out
, 3);
1246 else if (err
== oout
)
1249 err
= relocate_fd (err
, 3);
1263 #endif /* not MSDOS */
1264 #endif /* not WINDOWSNT */
1266 #if defined(USG) && !defined(BSD_PGRPS)
1267 #ifndef SETPGRP_RELEASES_CTTY
1268 setpgrp (); /* No arguments but equivalent in this case */
1273 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1274 EMACS_SET_TTY_PGRP (0, &pid
);
1277 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1280 /* An error occurred while trying to run the subprocess. */
1281 report_file_error ("Spawning child process", Qnil
);
1283 #else /* not MSDOS */
1285 /* Spawn the child. (See ntproc.c:Spawnve). */
1286 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1287 reset_standard_handles (in
, out
, err
, handles
);
1289 /* An error occurred while trying to spawn the process. */
1290 report_file_error ("Spawning child process", Qnil
);
1292 #else /* not WINDOWSNT */
1293 /* execvp does not accept an environment arg so the only way
1294 to pass this environment is to set environ. Our caller
1295 is responsible for restoring the ambient value of environ. */
1297 execvp (new_argv
[0], new_argv
);
1299 emacs_write (1, "Can't exec program: ", 20);
1300 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1301 emacs_write (1, "\n", 1);
1303 #endif /* not WINDOWSNT */
1304 #endif /* not MSDOS */
1307 /* Move the file descriptor FD so that its number is not less than MINFD.
1308 If the file descriptor is moved at all, the original is freed. */
1310 relocate_fd (fd
, minfd
)
1320 char *message1
= "Error while setting up child: ";
1321 char *errmessage
= strerror (errno
);
1322 char *message2
= "\n";
1323 emacs_write (2, message1
, strlen (message1
));
1324 emacs_write (2, errmessage
, strlen (errmessage
));
1325 emacs_write (2, message2
, strlen (message2
));
1328 /* Note that we hold the original FD open while we recurse,
1329 to guarantee we'll get a new FD if we need it. */
1330 new = relocate_fd (new, minfd
);
1337 getenv_internal (var
, varlen
, value
, valuelen
)
1345 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1349 entry
= XCAR (scan
);
1351 && SBYTES (entry
) > varlen
1352 && SREF (entry
, varlen
) == '='
1354 /* NT environment variables are case insensitive. */
1355 && ! strnicmp (SDATA (entry
), var
, varlen
)
1356 #else /* not WINDOWSNT */
1357 && ! bcmp (SDATA (entry
), var
, varlen
)
1358 #endif /* not WINDOWSNT */
1361 *value
= (char *) SDATA (entry
) + (varlen
+ 1);
1362 *valuelen
= SBYTES (entry
) - (varlen
+ 1);
1370 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1371 doc
: /* Return the value of environment variable VAR, as a string.
1372 VAR should be a string. Value is nil if VAR is undefined in the environment.
1373 This function consults the variable `process-environment' for its value. */)
1381 if (getenv_internal (SDATA (var
), SBYTES (var
),
1383 return make_string (value
, valuelen
);
1388 /* A version of getenv that consults process_environment, easily
1397 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1403 #endif /* not VMS */
1405 /* This is run before init_cmdargs. */
1410 char *data_dir
= egetenv ("EMACSDATA");
1411 char *doc_dir
= egetenv ("EMACSDOC");
1414 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1417 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1420 /* Check the EMACSPATH environment variable, defaulting to the
1421 PATH_EXEC path from epaths.h. */
1422 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1423 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1424 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1427 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1432 char *data_dir
= egetenv ("EMACSDATA");
1435 Lisp_Object tempdir
;
1437 if (!NILP (Vinstallation_directory
))
1439 /* Add to the path the lib-src subdir of the installation dir. */
1441 tem
= Fexpand_file_name (build_string ("lib-src"),
1442 Vinstallation_directory
);
1444 /* MSDOS uses wrapped binaries, so don't do this. */
1445 if (NILP (Fmember (tem
, Vexec_path
)))
1447 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1448 Vexec_path
= Fcons (tem
, Vexec_path
);
1449 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1452 Vexec_directory
= Ffile_name_as_directory (tem
);
1453 #endif /* not DOS_NT */
1455 /* Maybe use ../etc as well as ../lib-src. */
1458 tem
= Fexpand_file_name (build_string ("etc"),
1459 Vinstallation_directory
);
1460 Vdoc_directory
= Ffile_name_as_directory (tem
);
1464 /* Look for the files that should be in etc. We don't use
1465 Vinstallation_directory, because these files are never installed
1466 near the executable, and they are never in the build
1467 directory when that's different from the source directory.
1469 Instead, if these files are not in the nominal place, we try the
1470 source directory. */
1473 Lisp_Object tem
, tem1
, srcdir
;
1475 srcdir
= Fexpand_file_name (build_string ("../src/"),
1476 build_string (PATH_DUMPLOADSEARCH
));
1477 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1478 tem1
= Ffile_exists_p (tem
);
1479 if (!NILP (Fequal (srcdir
, Vinvocation_directory
)) || NILP (tem1
))
1482 newdir
= Fexpand_file_name (build_string ("../etc/"),
1483 build_string (PATH_DUMPLOADSEARCH
));
1484 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1485 tem1
= Ffile_exists_p (tem
);
1487 Vdata_directory
= newdir
;
1495 tempdir
= Fdirectory_file_name (Vexec_directory
);
1496 if (access (SDATA (tempdir
), 0) < 0)
1497 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1501 tempdir
= Fdirectory_file_name (Vdata_directory
);
1502 if (access (SDATA (tempdir
), 0) < 0)
1503 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1507 Vshell_file_name
= build_string ("*dcl*");
1509 sh
= (char *) getenv ("SHELL");
1510 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1514 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1516 if (getenv ("TMPDIR"))
1518 char *dir
= getenv ("TMPDIR");
1519 Vtemp_file_name_pattern
1520 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1521 build_string (dir
));
1524 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1528 Vshared_game_score_directory
= Qnil
;
1530 Vshared_game_score_directory
= build_string (PATH_GAME
);
1531 if (NILP (Ffile_directory_p (Vshared_game_score_directory
)))
1532 Vshared_game_score_directory
= Qnil
;
1537 set_process_environment ()
1539 register char **envp
;
1541 Vprocess_environment
= Qnil
;
1545 for (envp
= environ
; *envp
; envp
++)
1546 Vprocess_environment
= Fcons (build_string (*envp
),
1547 Vprocess_environment
);
1554 Qbuffer_file_type
= intern ("buffer-file-type");
1555 staticpro (&Qbuffer_file_type
);
1558 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1559 doc
: /* *File name to load inferior shells from.
1560 Initialized from the SHELL environment variable, or to a system-dependent
1561 default if SHELL is not set. */);
1563 DEFVAR_LISP ("exec-path", &Vexec_path
,
1564 doc
: /* *List of directories to search programs to run in subprocesses.
1565 Each element is a string (directory name) or nil (try default directory). */);
1567 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes
,
1568 doc
: /* *List of suffixes to try to find executable file names.
1569 Each element is a string. */);
1570 Vexec_suffixes
= Qnil
;
1572 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1573 doc
: /* Directory for executables for Emacs to invoke.
1574 More generally, this includes any architecture-dependent files
1575 that are built and installed from the Emacs distribution. */);
1577 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1578 doc
: /* Directory of machine-independent files that come with GNU Emacs.
1579 These are files intended for Emacs to use while it runs. */);
1581 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1582 doc
: /* Directory containing the DOC file that comes with GNU Emacs.
1583 This is usually the same as `data-directory'. */);
1585 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1586 doc
: /* For internal use by the build procedure only.
1587 This is the name of the directory in which the build procedure installed
1588 Emacs's info files; the default value for `Info-default-directory-list'
1590 Vconfigure_info_directory
= build_string (PATH_INFO
);
1592 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory
,
1593 doc
: /* Directory of score files for games which come with GNU Emacs.
1594 If this variable is nil, then Emacs is unable to use a shared directory. */);
1596 Vshared_game_score_directory
= Qnil
;
1598 Vshared_game_score_directory
= build_string (PATH_GAME
);
1601 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1602 doc
: /* Pattern for making names for temporary files.
1603 This is used by `call-process-region'. */);
1604 /* This variable is initialized in init_callproc. */
1606 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1607 doc
: /* List of environment variables for subprocesses to inherit.
1608 Each element should be a string of the form ENVVARNAME=VALUE.
1609 If multiple entries define the same variable, the first one always
1611 The environment which Emacs inherits is placed in this variable
1613 Non-ASCII characters are encoded according to the initial value of
1614 `locale-coding-system', i.e. the elements must normally be decoded for use.
1615 See `setenv' and `getenv'. */);
1618 defsubr (&Scall_process
);
1619 defsubr (&Sgetenv_internal
);
1621 defsubr (&Scall_process_region
);
1624 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1625 (do not change this comment) */