1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000,01,02,03,04
3 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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"
92 extern noshare
char **environ
;
95 extern char **environ
;
100 #if !defined (USG) || defined (BSD_PGRPS)
102 #define setpgrp setpgid
106 Lisp_Object Vexec_path
, Vexec_directory
, Vexec_suffixes
;
107 Lisp_Object Vdata_directory
, Vdoc_directory
;
108 Lisp_Object Vconfigure_info_directory
, Vshared_game_score_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 /* Nonzero => this is the signal number that terminated the subprocess. */
127 int synch_process_termsig
;
129 /* If synch_process_death is zero,
130 this is exit code of synchronous subprocess. */
131 int synch_process_retcode
;
133 extern Lisp_Object Vdoc_file_name
;
135 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
137 /* Clean up when exiting Fcall_process.
138 On MSDOS, delete the temporary file on any kind of termination.
139 On Unix, kill the process and any children on termination by signal. */
141 /* Nonzero if this is termination due to exit. */
142 static int call_process_exited
;
144 #ifndef VMS /* VMS version is in vmsproc.c. */
147 call_process_kill (fdpid
)
150 emacs_close (XFASTINT (Fcar (fdpid
)));
151 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
152 synch_process_alive
= 0;
157 call_process_cleanup (fdpid
)
160 #if defined (MSDOS) || defined (MAC_OS8)
161 /* for MSDOS fdpid is really (fd . tempfile) */
162 register Lisp_Object file
;
164 emacs_close (XFASTINT (Fcar (fdpid
)));
165 if (strcmp (SDATA (file
), NULL_DEVICE
) != 0)
166 unlink (SDATA (file
));
167 #else /* not MSDOS and not MAC_OS8 */
168 register int pid
= XFASTINT (Fcdr (fdpid
));
170 if (call_process_exited
)
172 emacs_close (XFASTINT (Fcar (fdpid
)));
176 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
178 int count
= SPECPDL_INDEX ();
179 record_unwind_protect (call_process_kill
, fdpid
);
180 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
183 wait_for_termination (pid
);
185 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
186 message1 ("Waiting for process to die...done");
188 synch_process_alive
= 0;
189 emacs_close (XFASTINT (Fcar (fdpid
)));
190 #endif /* not MSDOS */
194 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
195 doc
: /* Call PROGRAM synchronously in separate process.
196 The remaining arguments are optional.
197 The program's input comes from file INFILE (nil means `/dev/null').
198 Insert output in BUFFER before point; t means current buffer;
199 nil for BUFFER means discard it; 0 means discard and don't wait.
200 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
201 REAL-BUFFER says what to do with standard output, as above,
202 while STDERR-FILE says what to do with standard error in the child.
203 STDERR-FILE may be nil (discard standard error output),
204 t (mix it with ordinary output), or a file name string.
206 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
207 Remaining arguments are strings passed as command arguments to PROGRAM.
209 If BUFFER is 0, `call-process' returns immediately with value nil.
210 Otherwise it waits for PROGRAM to terminate
211 and returns a numeric exit status or a signal description string.
212 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
214 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
217 register Lisp_Object
*args
;
219 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
225 int bufsize
= sizeof buf
;
226 int count
= SPECPDL_INDEX ();
228 register const unsigned char **new_argv
229 = (const unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
230 struct buffer
*old
= current_buffer
;
231 /* File to use for stderr in the child.
232 t means use same as standard output. */
233 Lisp_Object error_file
;
234 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
235 char *outf
, *tempfile
;
245 struct coding_system process_coding
; /* coding-system of process output */
246 struct coding_system argument_coding
; /* coding-system of arguments */
247 /* Set to the return value of Ffind_operation_coding_system. */
248 Lisp_Object coding_systems
;
250 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
253 CHECK_STRING (args
[0]);
258 /* Without asynchronous processes we cannot have BUFFER == 0. */
260 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
261 error ("Operating system cannot handle asynchronous subprocesses");
262 #endif /* subprocesses */
264 /* Decide the coding-system for giving arguments. */
266 Lisp_Object val
, *args2
;
269 /* If arguments are supplied, we may have to encode them. */
274 for (i
= 4; i
< nargs
; i
++)
275 CHECK_STRING (args
[i
]);
277 for (i
= 4; i
< nargs
; i
++)
278 if (STRING_MULTIBYTE (args
[i
]))
281 if (!NILP (Vcoding_system_for_write
))
282 val
= Vcoding_system_for_write
;
283 else if (! must_encode
)
287 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
288 args2
[0] = Qcall_process
;
289 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
290 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
291 if (CONSP (coding_systems
))
292 val
= XCDR (coding_systems
);
293 else if (CONSP (Vdefault_process_coding_system
))
294 val
= XCDR (Vdefault_process_coding_system
);
298 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
302 if (nargs
>= 2 && ! NILP (args
[1]))
304 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
305 CHECK_STRING (infile
);
308 infile
= build_string (NULL_DEVICE
);
314 /* If BUFFER is a list, its meaning is
315 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
318 if (CONSP (XCDR (buffer
)))
320 Lisp_Object stderr_file
;
321 stderr_file
= XCAR (XCDR (buffer
));
323 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
324 error_file
= stderr_file
;
326 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
329 buffer
= XCAR (buffer
);
332 if (!(EQ (buffer
, Qnil
)
334 || INTEGERP (buffer
)))
336 Lisp_Object spec_buffer
;
337 spec_buffer
= buffer
;
338 buffer
= Fget_buffer_create (buffer
);
339 /* Mention the buffer name for a better error message. */
341 CHECK_BUFFER (spec_buffer
);
342 CHECK_BUFFER (buffer
);
348 /* Make sure that the child will be able to chdir to the current
349 buffer's current directory, or its unhandled equivalent. We
350 can't just have the child check for an error when it does the
351 chdir, since it's in a vfork.
353 We have to GCPRO around this because Fexpand_file_name,
354 Funhandled_file_name_directory, and Ffile_accessible_directory_p
355 might call a file name handling function. The argument list is
356 protected by the caller, so all we really have to worry about is
359 struct gcpro gcpro1
, gcpro2
, gcpro3
;
361 current_dir
= current_buffer
->directory
;
363 GCPRO3 (infile
, buffer
, current_dir
);
366 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
368 if (NILP (Ffile_accessible_directory_p (current_dir
)))
369 report_file_error ("Setting current directory",
370 Fcons (current_buffer
->directory
, Qnil
));
375 display
= nargs
>= 4 ? args
[3] : Qnil
;
377 filefd
= emacs_open (SDATA (infile
), O_RDONLY
, 0);
380 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
382 /* Search for program; barf if not found. */
386 GCPRO1 (current_dir
);
387 openp (Vexec_path
, args
[0], Vexec_suffixes
, &path
, make_number (X_OK
));
392 emacs_close (filefd
);
393 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
396 /* If program file name starts with /: for quoting a magic name,
398 if (SBYTES (path
) > 2 && SREF (path
, 0) == '/'
399 && SREF (path
, 1) == ':')
400 path
= Fsubstring (path
, make_number (2), Qnil
);
402 new_argv
[0] = SDATA (path
);
406 struct gcpro gcpro1
, gcpro2
, gcpro3
;
408 GCPRO3 (infile
, buffer
, current_dir
);
409 argument_coding
.dst_multibyte
= 0;
410 for (i
= 4; i
< nargs
; i
++)
412 argument_coding
.src_multibyte
= STRING_MULTIBYTE (args
[i
]);
413 if (CODING_REQUIRE_ENCODING (&argument_coding
))
414 /* We must encode this argument. */
415 args
[i
] = encode_coding_string (&argument_coding
, args
[i
], 1);
416 new_argv
[i
- 3] = SDATA (args
[i
]);
419 new_argv
[nargs
- 3] = 0;
424 #ifdef MSDOS /* MW, July 1993 */
425 if ((outf
= egetenv ("TMPDIR")))
426 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
429 tempfile
= alloca (20);
432 dostounix_filename (tempfile
);
433 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
434 strcat (tempfile
, "/");
435 strcat (tempfile
, "detmp.XXX");
438 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
441 emacs_close (filefd
);
442 report_file_error ("Opening process output file",
443 Fcons (build_string (tempfile
), Qnil
));
450 /* Since we don't have pipes on the Mac, create a temporary file to
451 hold the output of the subprocess. */
452 tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
453 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
454 SBYTES (Vtemp_file_name_pattern
) + 1);
458 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
462 report_file_error ("Opening process output file",
463 Fcons (build_string (tempfile
), Qnil
));
469 if (INTEGERP (buffer
))
470 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
478 emacs_close (filefd
);
479 report_file_error ("Creating process pipe", Qnil
);
484 /* Replaced by close_process_descs */
485 set_exclusive_use (fd
[0]);
490 /* child_setup must clobber environ in systems with true vfork.
491 Protect it from permanent change. */
492 register char **save_environ
= environ
;
493 register int fd1
= fd
[1];
496 #if 0 /* Some systems don't have sigblock. */
497 mask
= sigblock (sigmask (SIGCHLD
));
500 /* Record that we're about to create a synchronous process. */
501 synch_process_alive
= 1;
503 /* These vars record information from process termination.
504 Clear them now before process can possibly terminate,
505 to avoid timing error if process terminates soon. */
506 synch_process_death
= 0;
507 synch_process_retcode
= 0;
508 synch_process_termsig
= 0;
510 if (NILP (error_file
))
511 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
512 else if (STRINGP (error_file
))
515 fd_error
= emacs_open (SDATA (error_file
),
516 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
518 #else /* not DOS_NT */
519 fd_error
= creat (SDATA (error_file
), 0666);
520 #endif /* not DOS_NT */
525 emacs_close (filefd
);
533 report_file_error ("Cannot redirect stderr",
534 Fcons ((NILP (error_file
)
535 ? build_string (NULL_DEVICE
) : error_file
),
539 current_dir
= ENCODE_FILE (current_dir
);
543 /* Call run_mac_command in sysdep.c here directly instead of doing
544 a child_setup as for MSDOS and other platforms. Note that this
545 code does not handle passing the environment to the synchronous
547 char *infn
, *outfn
, *errfn
, *currdn
;
549 /* close these files so subprocess can write to them */
551 if (fd_error
!= outfilefd
)
553 fd1
= -1; /* No harm in closing that one! */
555 infn
= SDATA (infile
);
557 if (NILP (error_file
))
559 else if (EQ (Qt
, error_file
))
562 errfn
= SDATA (error_file
);
563 currdn
= SDATA (current_dir
);
564 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
566 /* Record that the synchronous process exited and note its
567 termination status. */
568 synch_process_alive
= 0;
569 synch_process_retcode
= pid
;
570 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
572 synchronize_system_messages_locale ();
573 synch_process_death
= strerror (errno
);
576 /* Since CRLF is converted to LF within `decode_coding', we can
577 always open a file with binary mode. */
578 fd
[0] = open (tempfile
, O_BINARY
);
583 report_file_error ("Cannot re-open temporary file", Qnil
);
586 #else /* not MAC_OS8 */
587 #ifdef MSDOS /* MW, July 1993 */
588 /* Note that on MSDOS `child_setup' actually returns the child process
589 exit status, not its PID, so we assign it to `synch_process_retcode'
591 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
594 /* Record that the synchronous process exited and note its
595 termination status. */
596 synch_process_alive
= 0;
597 synch_process_retcode
= pid
;
598 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
600 synchronize_system_messages_locale ();
601 synch_process_death
= strerror (errno
);
604 emacs_close (outfilefd
);
605 if (fd_error
!= outfilefd
)
606 emacs_close (fd_error
);
607 fd1
= -1; /* No harm in closing that one! */
608 /* Since CRLF is converted to LF within `decode_coding', we can
609 always open a file with binary mode. */
610 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
614 emacs_close (filefd
);
615 report_file_error ("Cannot re-open temporary file", Qnil
);
617 #else /* not MSDOS */
619 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
621 #else /* not WINDOWSNT */
631 #if defined (USG) && !defined (BSD_PGRPS)
636 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
639 #endif /* not WINDOWSNT */
641 /* The MSDOS case did this already. */
643 emacs_close (fd_error
);
644 #endif /* not MSDOS */
645 #endif /* not MAC_OS8 */
647 environ
= save_environ
;
649 /* Close most of our fd's, but not fd[0]
650 since we will use that to read input from. */
651 emacs_close (filefd
);
652 if (fd1
>= 0 && fd1
!= fd_error
)
660 report_file_error ("Doing vfork", Qnil
);
663 if (INTEGERP (buffer
))
668 /* If Emacs has been built with asynchronous subprocess support,
669 we don't need to do this, I think because it will then have
670 the facilities for handling SIGCHLD. */
671 wait_without_blocking ();
672 #endif /* subprocesses */
676 /* Enable sending signal if user quits below. */
677 call_process_exited
= 0;
679 #if defined(MSDOS) || defined(MAC_OS8)
680 /* MSDOS needs different cleanup information. */
681 record_unwind_protect (call_process_cleanup
,
682 Fcons (make_number (fd
[0]), build_string (tempfile
)));
684 record_unwind_protect (call_process_cleanup
,
685 Fcons (make_number (fd
[0]), make_number (pid
)));
686 #endif /* not MSDOS and not MAC_OS8 */
689 if (BUFFERP (buffer
))
690 Fset_buffer (buffer
);
694 /* If BUFFER is nil, we must read process output once and then
695 discard it, so setup coding system but with nil. */
696 setup_coding_system (Qnil
, &process_coding
);
700 Lisp_Object val
, *args2
;
703 if (!NILP (Vcoding_system_for_read
))
704 val
= Vcoding_system_for_read
;
707 if (EQ (coding_systems
, Qt
))
711 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
712 args2
[0] = Qcall_process
;
713 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
715 = Ffind_operation_coding_system (nargs
+ 1, args2
);
717 if (CONSP (coding_systems
))
718 val
= XCAR (coding_systems
);
719 else if (CONSP (Vdefault_process_coding_system
))
720 val
= XCAR (Vdefault_process_coding_system
);
724 Fcheck_coding_system (val
);
725 /* In unibyte mode, character code conversion should not take
726 place but EOL conversion should. So, setup raw-text or one
727 of the subsidiary according to the information just setup. */
728 if (NILP (current_buffer
->enable_multibyte_characters
)
730 val
= raw_text_coding_system (val
);
731 setup_coding_system (val
, &process_coding
);
742 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
743 struct coding_system saved_coding
;
745 saved_coding
= process_coding
;
748 /* Repeatedly read until we've filled as much as possible
749 of the buffer size we have. But don't read
750 less than 1024--save that for the next bufferful. */
752 while (nread
< bufsize
- 1024)
754 int this_read
= emacs_read (fd
[0], bufptr
+ nread
,
762 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
767 total_read
+= this_read
;
769 if (display_on_the_fly
)
773 /* Now NREAD is the total amount of data in the buffer. */
778 if (NILP (current_buffer
->enable_multibyte_characters
)
779 && ! CODING_MAY_REQUIRE_DECODING (&process_coding
))
780 insert_1_both (bufptr
, nread
, nread
, 0, 1, 0);
782 { /* We have to decode the input. */
785 XSETBUFFER (buf
, current_buffer
);
786 decode_coding_c_string (&process_coding
, bufptr
, nread
,
788 if (display_on_the_fly
789 && CODING_REQUIRE_DETECTION (&saved_coding
)
790 && ! CODING_REQUIRE_DETECTION (&process_coding
))
792 /* We have detected some coding system. But,
793 there's a possibility that the detection was
794 done by insufficient data. So, we give up
795 displaying on the fly. */
796 if (process_coding
.produced
> 0)
797 del_range_2 (process_coding
.dst_pos
,
798 process_coding
.dst_pos_byte
,
799 process_coding
.dst_pos
800 + process_coding
.produced_char
,
801 process_coding
.dst_pos_byte
802 + process_coding
.produced
, 0);
803 display_on_the_fly
= 0;
804 process_coding
= saved_coding
;
809 TEMP_SET_PT_BOTH (PT
+ process_coding
.produced_char
,
810 PT_BYTE
+ process_coding
.produced
);
811 carryover
= process_coding
.carryover_bytes
;
813 /* As CARRYOVER should not be that large, we had
814 better avoid overhead of bcopy. */
815 BCOPY_SHORT (process_coding
.carryover
, bufptr
,
816 process_coding
.carryover_bytes
);
820 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
823 /* Make the buffer bigger as we continue to read more data,
825 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
830 tempptr
= (char *) alloca (bufsize
);
831 bcopy (bufptr
, tempptr
, bufsize
/ 2);
835 if (!NILP (display
) && INTERACTIVE
)
838 prepare_menu_bars ();
840 redisplay_preserve_echo_area (1);
847 Vlast_coding_system_used
= CODING_ID_NAME (process_coding
.id
);
848 /* If the caller required, let the buffer inherit the
849 coding-system used to decode the process output. */
850 if (inherit_process_coding_system
)
851 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
852 make_number (total_read
));
855 /* Wait for it to terminate, unless it already has. */
856 wait_for_termination (pid
);
860 set_buffer_internal (old
);
862 /* Don't kill any children that the subprocess may have left behind
864 call_process_exited
= 1;
866 unbind_to (count
, Qnil
);
868 if (synch_process_termsig
)
872 synchronize_system_messages_locale ();
873 signame
= strsignal (synch_process_termsig
);
878 synch_process_death
= signame
;
881 if (synch_process_death
)
882 return code_convert_string_norecord (build_string (synch_process_death
),
883 Vlocale_coding_system
, 0);
884 return make_number (synch_process_retcode
);
889 delete_temp_file (name
)
892 /* Use Fdelete_file (indirectly) because that runs a file name handler.
893 We did that when writing the file, so we should do so when deleting. */
894 internal_delete_file (name
);
898 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
900 doc
: /* Send text from START to END to a synchronous process running PROGRAM.
901 The remaining arguments are optional.
902 Delete the text if fourth arg DELETE is non-nil.
904 Insert output in BUFFER before point; t means current buffer;
905 nil for BUFFER means discard it; 0 means discard and don't wait.
906 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
907 REAL-BUFFER says what to do with standard output, as above,
908 while STDERR-FILE says what to do with standard error in the child.
909 STDERR-FILE may be nil (discard standard error output),
910 t (mix it with ordinary output), or a file name string.
912 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
913 Remaining args are passed to PROGRAM at startup as command args.
915 If BUFFER is 0, `call-process-region' returns immediately with value nil.
916 Otherwise it waits for PROGRAM to terminate
917 and returns a numeric exit status or a signal description string.
918 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
920 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
923 register Lisp_Object
*args
;
926 Lisp_Object filename_string
;
927 register Lisp_Object start
, end
;
928 int count
= SPECPDL_INDEX ();
929 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
930 Lisp_Object coding_systems
;
931 Lisp_Object val
, *args2
;
937 if ((outf
= egetenv ("TMPDIR"))
938 || (outf
= egetenv ("TMP"))
939 || (outf
= egetenv ("TEMP")))
940 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
943 tempfile
= alloca (20);
946 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
947 strcat (tempfile
, "/");
948 if ('/' == DIRECTORY_SEP
)
949 dostounix_filename (tempfile
);
951 unixtodos_filename (tempfile
);
953 strcat (tempfile
, "emXXXXXX");
955 strcat (tempfile
, "detmp.XXX");
957 #else /* not DOS_NT */
958 char *tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
959 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
960 SBYTES (Vtemp_file_name_pattern
) + 1);
961 #endif /* not DOS_NT */
967 int fd
= mkstemp (tempfile
);
969 report_file_error ("Failed to open temporary file",
970 Fcons (Vtemp_file_name_pattern
, Qnil
));
978 filename_string
= build_string (tempfile
);
979 GCPRO1 (filename_string
);
982 /* Decide coding-system of the contents of the temporary file. */
983 if (!NILP (Vcoding_system_for_write
))
984 val
= Vcoding_system_for_write
;
985 else if (NILP (current_buffer
->enable_multibyte_characters
))
989 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
990 args2
[0] = Qcall_process_region
;
991 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
992 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
993 if (CONSP (coding_systems
))
994 val
= XCDR (coding_systems
);
995 else if (CONSP (Vdefault_process_coding_system
))
996 val
= XCDR (Vdefault_process_coding_system
);
1002 int count1
= SPECPDL_INDEX ();
1004 specbind (intern ("coding-system-for-write"), val
);
1005 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1007 unbind_to (count1
, Qnil
);
1010 /* Note that Fcall_process takes care of binding
1011 coding-system-for-read. */
1013 record_unwind_protect (delete_temp_file
, filename_string
);
1015 if (nargs
> 3 && !NILP (args
[3]))
1016 Fdelete_region (start
, end
);
1028 args
[1] = filename_string
;
1030 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1033 #ifndef VMS /* VMS version is in vmsproc.c. */
1035 static int relocate_fd ();
1037 /* This is the last thing run in a newly forked inferior
1038 either synchronous or asynchronous.
1039 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1040 Initialize inferior's priority, pgrp, connected dir and environment.
1041 then exec another program based on new_argv.
1043 This function may change environ for the superior process.
1044 Therefore, the superior process must save and restore the value
1045 of environ around the vfork and the call to this function.
1047 SET_PGRP is nonzero if we should put the subprocess into a separate
1050 CURRENT_DIR is an elisp string giving the path of the current
1051 directory the subprocess should have. Since we can't really signal
1052 a decent error from within the child, this should be verified as an
1053 executable directory by the parent. */
1056 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1058 register char **new_argv
;
1060 Lisp_Object current_dir
;
1067 #endif /* WINDOWSNT */
1069 int pid
= getpid ();
1071 #ifdef SET_EMACS_PRIORITY
1073 extern EMACS_INT emacs_priority
;
1075 if (emacs_priority
< 0)
1076 nice (- emacs_priority
);
1081 /* Close Emacs's descriptors that this process should not have. */
1082 close_process_descs ();
1084 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1085 we will lose if we call close_load_descs here. */
1087 close_load_descs ();
1090 /* Note that use of alloca is always safe here. It's obvious for systems
1091 that do not have true vfork or that have true (stack) alloca.
1092 If using vfork and C_ALLOCA it is safe because that changes
1093 the superior's static variables as if the superior had done alloca
1094 and will be cleaned up in the usual way. */
1096 register char *temp
;
1099 i
= SBYTES (current_dir
);
1101 /* MSDOS must have all environment variables malloc'ed, because
1102 low-level libc functions that launch subsidiary processes rely
1104 pwd_var
= (char *) xmalloc (i
+ 6);
1106 pwd_var
= (char *) alloca (i
+ 6);
1109 bcopy ("PWD=", pwd_var
, 4);
1110 bcopy (SDATA (current_dir
), temp
, i
);
1111 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1115 /* We can't signal an Elisp error here; we're in a vfork. Since
1116 the callers check the current directory before forking, this
1117 should only return an error if the directory's permissions
1118 are changed between the check and this chdir, but we should
1120 if (chdir (temp
) < 0)
1125 /* Get past the drive letter, so that d:/ is left alone. */
1126 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1133 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1134 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1138 /* Set `env' to a vector of the strings in Vprocess_environment. */
1140 register Lisp_Object tem
;
1141 register char **new_env
;
1142 register int new_length
;
1145 for (tem
= Vprocess_environment
;
1146 CONSP (tem
) && STRINGP (XCAR (tem
));
1150 /* new_length + 2 to include PWD and terminating 0. */
1151 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1153 /* If we have a PWD envvar, pass one down,
1154 but with corrected value. */
1156 *new_env
++ = pwd_var
;
1158 /* Copy the Vprocess_environment strings into new_env. */
1159 for (tem
= Vprocess_environment
;
1160 CONSP (tem
) && STRINGP (XCAR (tem
));
1164 char *string
= (char *) SDATA (XCAR (tem
));
1165 /* See if this string duplicates any string already in the env.
1166 If so, don't put it in.
1167 When an env var has multiple definitions,
1168 we keep the definition that comes first in process-environment. */
1169 for (; ep
!= new_env
; ep
++)
1171 char *p
= *ep
, *q
= string
;
1175 /* The string is malformed; might as well drop it. */
1184 *new_env
++ = string
;
1190 prepare_standard_handles (in
, out
, err
, handles
);
1191 set_process_dir (SDATA (current_dir
));
1192 #else /* not WINDOWSNT */
1193 /* Make sure that in, out, and err are not actually already in
1194 descriptors zero, one, or two; this could happen if Emacs is
1195 started with its standard in, out, or error closed, as might
1198 int oin
= in
, oout
= out
;
1200 /* We have to avoid relocating the same descriptor twice! */
1202 in
= relocate_fd (in
, 3);
1207 out
= relocate_fd (out
, 3);
1211 else if (err
== oout
)
1214 err
= relocate_fd (err
, 3);
1228 #endif /* not MSDOS */
1229 #endif /* not WINDOWSNT */
1231 #if defined(USG) && !defined(BSD_PGRPS)
1232 #ifndef SETPGRP_RELEASES_CTTY
1233 setpgrp (); /* No arguments but equivalent in this case */
1238 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1239 EMACS_SET_TTY_PGRP (0, &pid
);
1242 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1245 /* An error occurred while trying to run the subprocess. */
1246 report_file_error ("Spawning child process", Qnil
);
1248 #else /* not MSDOS */
1250 /* Spawn the child. (See ntproc.c:Spawnve). */
1251 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1252 reset_standard_handles (in
, out
, err
, handles
);
1254 /* An error occurred while trying to spawn the process. */
1255 report_file_error ("Spawning child process", Qnil
);
1257 #else /* not WINDOWSNT */
1258 /* execvp does not accept an environment arg so the only way
1259 to pass this environment is to set environ. Our caller
1260 is responsible for restoring the ambient value of environ. */
1262 execvp (new_argv
[0], new_argv
);
1264 emacs_write (1, "Can't exec program: ", 20);
1265 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1266 emacs_write (1, "\n", 1);
1268 #endif /* not WINDOWSNT */
1269 #endif /* not MSDOS */
1272 /* Move the file descriptor FD so that its number is not less than MINFD.
1273 If the file descriptor is moved at all, the original is freed. */
1275 relocate_fd (fd
, minfd
)
1285 char *message1
= "Error while setting up child: ";
1286 char *errmessage
= strerror (errno
);
1287 char *message2
= "\n";
1288 emacs_write (2, message1
, strlen (message1
));
1289 emacs_write (2, errmessage
, strlen (errmessage
));
1290 emacs_write (2, message2
, strlen (message2
));
1293 /* Note that we hold the original FD open while we recurse,
1294 to guarantee we'll get a new FD if we need it. */
1295 new = relocate_fd (new, minfd
);
1302 getenv_internal (var
, varlen
, value
, valuelen
)
1310 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1314 entry
= XCAR (scan
);
1316 && SBYTES (entry
) > varlen
1317 && SREF (entry
, varlen
) == '='
1319 /* NT environment variables are case insensitive. */
1320 && ! strnicmp (SDATA (entry
), var
, varlen
)
1321 #else /* not WINDOWSNT */
1322 && ! bcmp (SDATA (entry
), var
, varlen
)
1323 #endif /* not WINDOWSNT */
1326 *value
= (char *) SDATA (entry
) + (varlen
+ 1);
1327 *valuelen
= SBYTES (entry
) - (varlen
+ 1);
1335 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1336 doc
: /* Return the value of environment variable VAR, as a string.
1337 VAR should be a string. Value is nil if VAR is undefined in the environment.
1338 This function consults the variable ``process-environment'' for its value. */)
1346 if (getenv_internal (SDATA (var
), SBYTES (var
),
1348 return make_string (value
, valuelen
);
1353 /* A version of getenv that consults process_environment, easily
1362 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1368 #endif /* not VMS */
1370 /* This is run before init_cmdargs. */
1375 char *data_dir
= egetenv ("EMACSDATA");
1376 char *doc_dir
= egetenv ("EMACSDOC");
1379 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1382 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1385 /* Check the EMACSPATH environment variable, defaulting to the
1386 PATH_EXEC path from epaths.h. */
1387 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1388 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1389 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1392 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1397 char *data_dir
= egetenv ("EMACSDATA");
1400 Lisp_Object tempdir
;
1402 if (!NILP (Vinstallation_directory
))
1404 /* Add to the path the lib-src subdir of the installation dir. */
1406 tem
= Fexpand_file_name (build_string ("lib-src"),
1407 Vinstallation_directory
);
1409 /* MSDOS uses wrapped binaries, so don't do this. */
1410 if (NILP (Fmember (tem
, Vexec_path
)))
1412 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1413 Vexec_path
= Fcons (tem
, Vexec_path
);
1414 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1417 Vexec_directory
= Ffile_name_as_directory (tem
);
1418 #endif /* not DOS_NT */
1420 /* Maybe use ../etc as well as ../lib-src. */
1423 tem
= Fexpand_file_name (build_string ("etc"),
1424 Vinstallation_directory
);
1425 Vdoc_directory
= Ffile_name_as_directory (tem
);
1429 /* Look for the files that should be in etc. We don't use
1430 Vinstallation_directory, because these files are never installed
1431 near the executable, and they are never in the build
1432 directory when that's different from the source directory.
1434 Instead, if these files are not in the nominal place, we try the
1435 source directory. */
1438 Lisp_Object tem
, tem1
, srcdir
;
1440 srcdir
= Fexpand_file_name (build_string ("../src/"),
1441 build_string (PATH_DUMPLOADSEARCH
));
1442 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1443 tem1
= Ffile_exists_p (tem
);
1444 if (!NILP (Fequal (srcdir
, Vinvocation_directory
)) || NILP (tem1
))
1447 newdir
= Fexpand_file_name (build_string ("../etc/"),
1448 build_string (PATH_DUMPLOADSEARCH
));
1449 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1450 tem1
= Ffile_exists_p (tem
);
1452 Vdata_directory
= newdir
;
1460 tempdir
= Fdirectory_file_name (Vexec_directory
);
1461 if (access (SDATA (tempdir
), 0) < 0)
1462 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1466 tempdir
= Fdirectory_file_name (Vdata_directory
);
1467 if (access (SDATA (tempdir
), 0) < 0)
1468 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1472 Vshell_file_name
= build_string ("*dcl*");
1474 sh
= (char *) getenv ("SHELL");
1475 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1479 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1481 if (getenv ("TMPDIR"))
1483 char *dir
= getenv ("TMPDIR");
1484 Vtemp_file_name_pattern
1485 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1486 build_string (dir
));
1489 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1493 Vshared_game_score_directory
= Qnil
;
1495 Vshared_game_score_directory
= build_string (PATH_GAME
);
1496 if (NILP (Ffile_directory_p (Vshared_game_score_directory
)))
1497 Vshared_game_score_directory
= Qnil
;
1502 set_process_environment ()
1504 register char **envp
;
1506 Vprocess_environment
= Qnil
;
1510 for (envp
= environ
; *envp
; envp
++)
1511 Vprocess_environment
= Fcons (build_string (*envp
),
1512 Vprocess_environment
);
1519 Qbuffer_file_type
= intern ("buffer-file-type");
1520 staticpro (&Qbuffer_file_type
);
1523 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1524 doc
: /* *File name to load inferior shells from.
1525 Initialized from the SHELL environment variable. */);
1527 DEFVAR_LISP ("exec-path", &Vexec_path
,
1528 doc
: /* *List of directories to search programs to run in subprocesses.
1529 Each element is a string (directory name) or nil (try default directory). */);
1531 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes
,
1532 doc
: /* *List of suffixes to try to find executable file names.
1533 Each element is a string. */);
1534 Vexec_suffixes
= Qnil
;
1536 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1537 doc
: /* Directory for executables for Emacs to invoke.
1538 More generally, this includes any architecture-dependent files
1539 that are built and installed from the Emacs distribution. */);
1541 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1542 doc
: /* Directory of machine-independent files that come with GNU Emacs.
1543 These are files intended for Emacs to use while it runs. */);
1545 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1546 doc
: /* Directory containing the DOC file that comes with GNU Emacs.
1547 This is usually the same as data-directory. */);
1549 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1550 doc
: /* For internal use by the build procedure only.
1551 This is the name of the directory in which the build procedure installed
1552 Emacs's info files; the default value for Info-default-directory-list
1554 Vconfigure_info_directory
= build_string (PATH_INFO
);
1556 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory
,
1557 doc
: /* Directory of score files for games which come with GNU Emacs.
1558 If this variable is nil, then Emacs is unable to use a shared directory. */);
1560 Vshared_game_score_directory
= Qnil
;
1562 Vshared_game_score_directory
= build_string (PATH_GAME
);
1565 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1566 doc
: /* Pattern for making names for temporary files.
1567 This is used by `call-process-region'. */);
1568 /* This variable is initialized in init_callproc. */
1570 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1571 doc
: /* List of environment variables for subprocesses to inherit.
1572 Each element should be a string of the form ENVVARNAME=VALUE.
1573 If multiple entries define the same variable, the first one always
1575 The environment which Emacs inherits is placed in this variable
1577 Non-ASCII characters are encoded according to the initial value of
1578 `locale-coding-system', i.e. the elements must normally be decoded for use.
1579 See `setenv' and `getenv'. */);
1582 defsubr (&Scall_process
);
1583 defsubr (&Sgetenv_internal
);
1585 defsubr (&Scall_process_region
);
1588 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1589 (do not change this comment) */