1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000, 2001
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>
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 #define max(a, b) ((a) > (b) ? (a) : (b))
108 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
109 Lisp_Object Vconfigure_info_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 /* If synch_process_death is zero,
128 this is exit code of synchronous subprocess. */
129 int synch_process_retcode
;
131 extern Lisp_Object Vdoc_file_name
;
133 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
135 /* Clean up when exiting Fcall_process.
136 On MSDOS, delete the temporary file on any kind of termination.
137 On Unix, kill the process and any children on termination by signal. */
139 /* Nonzero if this is termination due to exit. */
140 static int call_process_exited
;
142 #ifndef VMS /* VMS version is in vmsproc.c. */
145 call_process_kill (fdpid
)
148 emacs_close (XFASTINT (Fcar (fdpid
)));
149 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
150 synch_process_alive
= 0;
155 call_process_cleanup (fdpid
)
158 #if defined (MSDOS) || defined (macintosh)
159 /* for MSDOS fdpid is really (fd . tempfile) */
160 register Lisp_Object file
;
162 emacs_close (XFASTINT (Fcar (fdpid
)));
163 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
164 unlink (XSTRING (file
)->data
);
165 #else /* not MSDOS and not macintosh */
166 register int pid
= XFASTINT (Fcdr (fdpid
));
168 if (call_process_exited
)
170 emacs_close (XFASTINT (Fcar (fdpid
)));
174 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
176 int count
= specpdl_ptr
- specpdl
;
177 record_unwind_protect (call_process_kill
, fdpid
);
178 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
181 wait_for_termination (pid
);
183 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
184 message1 ("Waiting for process to die...done");
186 synch_process_alive
= 0;
187 emacs_close (XFASTINT (Fcar (fdpid
)));
188 #endif /* not MSDOS */
192 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
193 "Call PROGRAM synchronously in separate process.\n\
194 The remaining arguments are optional.\n\
195 The program's input comes from file INFILE (nil means `/dev/null').\n\
196 Insert output in BUFFER before point; t means current buffer;\n\
197 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
198 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
199 REAL-BUFFER says what to do with standard output, as above,\n\
200 while STDERR-FILE says what to do with standard error in the child.\n\
201 STDERR-FILE may be nil (discard standard error output),\n\
202 t (mix it with ordinary output), or a file name string.\n\
204 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
205 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
207 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
208 Otherwise it waits for PROGRAM to terminate\n\
209 and returns a numeric exit status or a signal description string.\n\
210 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
213 register Lisp_Object
*args
;
215 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
222 int count
= specpdl_ptr
- specpdl
;
224 register unsigned char **new_argv
225 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
226 struct buffer
*old
= current_buffer
;
227 /* File to use for stderr in the child.
228 t means use same as standard output. */
229 Lisp_Object error_file
;
230 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
231 char *outf
, *tempfile
;
241 struct coding_system process_coding
; /* coding-system of process output */
242 struct coding_system argument_coding
; /* coding-system of arguments */
243 /* Set to the return value of Ffind_operation_coding_system. */
244 Lisp_Object coding_systems
;
246 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
249 CHECK_STRING (args
[0], 0);
254 /* Without asynchronous processes we cannot have BUFFER == 0. */
256 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
257 error ("Operating system cannot handle asynchronous subprocesses");
258 #endif /* subprocesses */
260 /* Decide the coding-system for giving arguments. */
262 Lisp_Object val
, *args2
;
265 /* If arguments are supplied, we may have to encode them. */
270 for (i
= 4; i
< nargs
; i
++)
271 CHECK_STRING (args
[i
], i
);
273 for (i
= 4; i
< nargs
; i
++)
274 if (STRING_MULTIBYTE (args
[i
]))
277 if (!NILP (Vcoding_system_for_write
))
278 val
= Vcoding_system_for_write
;
279 else if (! must_encode
)
283 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
284 args2
[0] = Qcall_process
;
285 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
286 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
287 if (CONSP (coding_systems
))
288 val
= XCDR (coding_systems
);
289 else if (CONSP (Vdefault_process_coding_system
))
290 val
= XCDR (Vdefault_process_coding_system
);
294 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
298 if (nargs
>= 2 && ! NILP (args
[1]))
300 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
301 CHECK_STRING (infile
, 1);
304 infile
= build_string (NULL_DEVICE
);
310 /* If BUFFER is a list, its meaning is
311 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
314 if (CONSP (XCDR (buffer
)))
316 Lisp_Object stderr_file
;
317 stderr_file
= XCAR (XCDR (buffer
));
319 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
320 error_file
= stderr_file
;
322 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
325 buffer
= XCAR (buffer
);
328 if (!(EQ (buffer
, Qnil
)
330 || INTEGERP (buffer
)))
332 Lisp_Object spec_buffer
;
333 spec_buffer
= buffer
;
334 buffer
= Fget_buffer_create (buffer
);
335 /* Mention the buffer name for a better error message. */
337 CHECK_BUFFER (spec_buffer
, 2);
338 CHECK_BUFFER (buffer
, 2);
344 /* Make sure that the child will be able to chdir to the current
345 buffer's current directory, or its unhandled equivalent. We
346 can't just have the child check for an error when it does the
347 chdir, since it's in a vfork.
349 We have to GCPRO around this because Fexpand_file_name,
350 Funhandled_file_name_directory, and Ffile_accessible_directory_p
351 might call a file name handling function. The argument list is
352 protected by the caller, so all we really have to worry about is
355 struct gcpro gcpro1
, gcpro2
, gcpro3
;
357 current_dir
= current_buffer
->directory
;
359 GCPRO3 (infile
, buffer
, current_dir
);
362 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
364 if (NILP (Ffile_accessible_directory_p (current_dir
)))
365 report_file_error ("Setting current directory",
366 Fcons (current_buffer
->directory
, Qnil
));
371 display
= nargs
>= 4 ? args
[3] : Qnil
;
373 filefd
= emacs_open (XSTRING (infile
)->data
, O_RDONLY
, 0);
376 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
378 /* Search for program; barf if not found. */
382 GCPRO1 (current_dir
);
383 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
388 emacs_close (filefd
);
389 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
391 new_argv
[0] = XSTRING (path
)->data
;
395 struct gcpro gcpro1
, gcpro2
, gcpro3
;
397 GCPRO3 (infile
, buffer
, current_dir
);
398 argument_coding
.dst_multibyte
= 0;
399 for (i
= 4; i
< nargs
; i
++)
401 argument_coding
.src_multibyte
= STRING_MULTIBYTE (args
[i
]);
402 if (CODING_REQUIRE_ENCODING (&argument_coding
))
404 /* We must encode this argument. */
405 args
[i
] = encode_coding_string (args
[i
], &argument_coding
, 1);
406 if (argument_coding
.type
== coding_type_ccl
)
407 setup_ccl_program (&(argument_coding
.spec
.ccl
.encoder
), Qnil
);
409 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
412 new_argv
[nargs
- 3] = 0;
417 #ifdef MSDOS /* MW, July 1993 */
418 if ((outf
= egetenv ("TMPDIR")))
419 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
422 tempfile
= alloca (20);
425 dostounix_filename (tempfile
);
426 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
427 strcat (tempfile
, "/");
428 strcat (tempfile
, "detmp.XXX");
431 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
434 emacs_close (filefd
);
435 report_file_error ("Opening process output file",
436 Fcons (build_string (tempfile
), Qnil
));
443 /* Since we don't have pipes on the Mac, create a temporary file to
444 hold the output of the subprocess. */
445 tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
446 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
447 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
451 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
455 report_file_error ("Opening process output file",
456 Fcons (build_string (tempfile
), Qnil
));
460 #endif /* macintosh */
462 if (INTEGERP (buffer
))
463 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
471 emacs_close (filefd
);
472 report_file_error ("Creating process pipe", Qnil
);
477 /* Replaced by close_process_descs */
478 set_exclusive_use (fd
[0]);
483 /* child_setup must clobber environ in systems with true vfork.
484 Protect it from permanent change. */
485 register char **save_environ
= environ
;
486 register int fd1
= fd
[1];
489 #if 0 /* Some systems don't have sigblock. */
490 mask
= sigblock (sigmask (SIGCHLD
));
493 /* Record that we're about to create a synchronous process. */
494 synch_process_alive
= 1;
496 /* These vars record information from process termination.
497 Clear them now before process can possibly terminate,
498 to avoid timing error if process terminates soon. */
499 synch_process_death
= 0;
500 synch_process_retcode
= 0;
502 if (NILP (error_file
))
503 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
504 else if (STRINGP (error_file
))
507 fd_error
= emacs_open (XSTRING (error_file
)->data
,
508 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
510 #else /* not DOS_NT */
511 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
512 #endif /* not DOS_NT */
517 emacs_close (filefd
);
525 report_file_error ("Cannot redirect stderr",
526 Fcons ((NILP (error_file
)
527 ? build_string (NULL_DEVICE
) : error_file
),
531 current_dir
= ENCODE_FILE (current_dir
);
535 /* Call run_mac_command in sysdep.c here directly instead of doing
536 a child_setup as for MSDOS and other platforms. Note that this
537 code does not handle passing the environment to the synchronous
539 char *infn
, *outfn
, *errfn
, *currdn
;
541 /* close these files so subprocess can write to them */
543 if (fd_error
!= outfilefd
)
545 fd1
= -1; /* No harm in closing that one! */
547 infn
= XSTRING (infile
)->data
;
549 if (NILP (error_file
))
551 else if (EQ (Qt
, error_file
))
554 errfn
= XSTRING (error_file
)->data
;
555 currdn
= XSTRING (current_dir
)->data
;
556 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
558 /* Record that the synchronous process exited and note its
559 termination status. */
560 synch_process_alive
= 0;
561 synch_process_retcode
= pid
;
562 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
564 synchronize_system_messages_locale ();
565 synch_process_death
= strerror (errno
);
568 /* Since CRLF is converted to LF within `decode_coding', we can
569 always open a file with binary mode. */
570 fd
[0] = open (tempfile
, O_BINARY
);
575 report_file_error ("Cannot re-open temporary file", Qnil
);
578 #else /* not macintosh */
579 #ifdef MSDOS /* MW, July 1993 */
580 /* Note that on MSDOS `child_setup' actually returns the child process
581 exit status, not its PID, so we assign it to `synch_process_retcode'
583 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
586 /* Record that the synchronous process exited and note its
587 termination status. */
588 synch_process_alive
= 0;
589 synch_process_retcode
= pid
;
590 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
592 synchronize_system_messages_locale ();
593 synch_process_death
= strerror (errno
);
596 emacs_close (outfilefd
);
597 if (fd_error
!= outfilefd
)
598 emacs_close (fd_error
);
599 fd1
= -1; /* No harm in closing that one! */
600 /* Since CRLF is converted to LF within `decode_coding', we can
601 always open a file with binary mode. */
602 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
606 emacs_close (filefd
);
607 report_file_error ("Cannot re-open temporary file", Qnil
);
609 #else /* not MSDOS */
611 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
613 #else /* not WINDOWSNT */
623 #if defined (USG) && !defined (BSD_PGRPS)
628 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
631 #endif /* not WINDOWSNT */
633 /* The MSDOS case did this already. */
635 emacs_close (fd_error
);
636 #endif /* not MSDOS */
637 #endif /* not macintosh */
639 environ
= save_environ
;
641 /* Close most of our fd's, but not fd[0]
642 since we will use that to read input from. */
643 emacs_close (filefd
);
644 if (fd1
>= 0 && fd1
!= fd_error
)
652 report_file_error ("Doing vfork", Qnil
);
655 if (INTEGERP (buffer
))
660 /* If Emacs has been built with asynchronous subprocess support,
661 we don't need to do this, I think because it will then have
662 the facilities for handling SIGCHLD. */
663 wait_without_blocking ();
664 #endif /* subprocesses */
668 /* Enable sending signal if user quits below. */
669 call_process_exited
= 0;
671 #if defined(MSDOS) || defined(macintosh)
672 /* MSDOS needs different cleanup information. */
673 record_unwind_protect (call_process_cleanup
,
674 Fcons (make_number (fd
[0]), build_string (tempfile
)));
676 record_unwind_protect (call_process_cleanup
,
677 Fcons (make_number (fd
[0]), make_number (pid
)));
678 #endif /* not MSDOS and not macintosh */
681 if (BUFFERP (buffer
))
682 Fset_buffer (buffer
);
686 /* If BUFFER is nil, we must read process output once and then
687 discard it, so setup coding system but with nil. */
688 setup_coding_system (Qnil
, &process_coding
);
692 Lisp_Object val
, *args2
;
695 if (!NILP (Vcoding_system_for_read
))
696 val
= Vcoding_system_for_read
;
699 if (EQ (coding_systems
, Qt
))
703 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
704 args2
[0] = Qcall_process
;
705 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
707 = Ffind_operation_coding_system (nargs
+ 1, args2
);
709 if (CONSP (coding_systems
))
710 val
= XCAR (coding_systems
);
711 else if (CONSP (Vdefault_process_coding_system
))
712 val
= XCAR (Vdefault_process_coding_system
);
716 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
717 /* In unibyte mode, character code conversion should not take
718 place but EOL conversion should. So, setup raw-text or one
719 of the subsidiary according to the information just setup. */
720 if (NILP (current_buffer
->enable_multibyte_characters
)
722 setup_raw_text_coding_system (&process_coding
);
724 process_coding
.src_multibyte
= 0;
725 process_coding
.dst_multibyte
727 ? ! NILP (XBUFFER (buffer
)->enable_multibyte_characters
)
728 : ! NILP (current_buffer
->enable_multibyte_characters
));
738 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
739 struct coding_system saved_coding
;
740 int pt_orig
= PT
, pt_byte_orig
= PT_BYTE
;
743 saved_coding
= process_coding
;
744 if (process_coding
.composing
!= COMPOSITION_DISABLED
)
745 coding_allocate_composition_data (&process_coding
, PT
);
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 (! CODING_MAY_REQUIRE_DECODING (&process_coding
))
779 insert_1_both (bufptr
, nread
, nread
, 0, 1, 0);
781 { /* We have to decode the input. */
786 size
= decoding_buffer_size (&process_coding
, nread
);
787 decoding_buf
= (char *) xmalloc (size
);
789 if (process_coding
.cmp_data
)
790 process_coding
.cmp_data
->char_offset
= PT
;
792 decode_coding (&process_coding
, bufptr
, decoding_buf
,
795 if (display_on_the_fly
796 && saved_coding
.type
== coding_type_undecided
797 && process_coding
.type
!= coding_type_undecided
)
799 /* We have detected some coding system. But,
800 there's a possibility that the detection was
801 done by insufficient data. So, we give up
802 displaying on the fly. */
803 xfree (decoding_buf
);
804 display_on_the_fly
= 0;
805 process_coding
= saved_coding
;
810 if (process_coding
.produced
> 0)
811 insert_1_both (decoding_buf
, process_coding
.produced_char
,
812 process_coding
.produced
, 0, 1, 0);
813 xfree (decoding_buf
);
815 if (process_coding
.result
== CODING_FINISH_INCONSISTENT_EOL
)
817 Lisp_Object eol_type
, coding
;
819 if (process_coding
.eol_type
== CODING_EOL_CR
)
821 /* CRs have been replaced with LFs. Undo
822 that in the text inserted above. */
825 move_gap_both (PT
, PT_BYTE
);
827 p
= BYTE_POS_ADDR (pt_byte_orig
);
828 for (; p
< GPT_ADDR
; ++p
)
832 else if (process_coding
.eol_type
== CODING_EOL_CRLF
)
834 /* CR LFs have been replaced with LFs. Undo
835 that by inserting CRs in front of LFs in
836 the text inserted above. */
837 EMACS_INT bytepos
, old_pt
, old_pt_byte
, nCR
;
840 old_pt_byte
= PT_BYTE
;
843 for (bytepos
= PT_BYTE
- 1;
844 bytepos
>= pt_byte_orig
;
846 if (FETCH_BYTE (bytepos
) == '\n')
848 EMACS_INT charpos
= BYTE_TO_CHAR (bytepos
);
849 TEMP_SET_PT_BOTH (charpos
, bytepos
);
850 insert_1_both ("\r", 1, 1, 0, 1, 0);
854 TEMP_SET_PT_BOTH (old_pt
+ nCR
, old_pt_byte
+ nCR
);
857 /* Set the coding system symbol to that for
859 eol_type
= Fget (saved_coding
.symbol
, Qeol_type
);
860 if (VECTORP (eol_type
)
861 && ASIZE (eol_type
) == 3
862 && SYMBOLP (AREF (eol_type
, CODING_EOL_LF
)))
863 coding
= AREF (eol_type
, CODING_EOL_LF
);
865 coding
= saved_coding
.symbol
;
867 process_coding
.symbol
= coding
;
868 process_coding
.eol_type
= CODING_EOL_LF
;
870 &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
873 nread
-= process_coding
.consumed
;
876 /* As CARRYOVER should not be that large, we had
877 better avoid overhead of bcopy. */
878 BCOPY_SHORT (bufptr
+ process_coding
.consumed
, bufptr
,
880 if (process_coding
.result
== CODING_FINISH_INSUFFICIENT_CMP
)
882 /* The decoding ended because of insufficient data
883 area to record information about composition.
884 We must try decoding with additional data area
885 before reading more output for the process. */
886 coding_allocate_composition_data (&process_coding
, PT
);
887 goto repeat_decoding
;
892 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
895 /* Make the buffer bigger as we continue to read more data,
897 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
900 bufptr
= (char *) alloca (bufsize
);
903 if (!NILP (display
) && INTERACTIVE
)
906 prepare_menu_bars ();
908 redisplay_preserve_echo_area (1);
916 && process_coding
.cmp_data
)
918 coding_restore_composition (&process_coding
, Fcurrent_buffer ());
919 coding_free_composition_data (&process_coding
);
923 int post_read_count
= specpdl_ptr
- specpdl
;
925 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
926 inserted
= PT
- pt_orig
;
927 TEMP_SET_PT_BOTH (pt_orig
, pt_byte_orig
);
928 if (SYMBOLP (process_coding
.post_read_conversion
)
929 && !NILP (Ffboundp (process_coding
.post_read_conversion
)))
930 call1 (process_coding
.post_read_conversion
, make_number (inserted
));
932 Vlast_coding_system_used
= process_coding
.symbol
;
934 /* If the caller required, let the buffer inherit the
935 coding-system used to decode the process output. */
936 if (inherit_process_coding_system
)
937 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
938 make_number (total_read
));
940 unbind_to (post_read_count
, Qnil
);
944 /* Wait for it to terminate, unless it already has. */
945 wait_for_termination (pid
);
949 set_buffer_internal (old
);
951 /* Don't kill any children that the subprocess may have left behind
953 call_process_exited
= 1;
955 unbind_to (count
, Qnil
);
957 if (synch_process_death
)
958 return code_convert_string_norecord (build_string (synch_process_death
),
959 Vlocale_coding_system
, 0);
960 return make_number (synch_process_retcode
);
965 delete_temp_file (name
)
968 /* Use Fdelete_file (indirectly) because that runs a file name handler.
969 We did that when writing the file, so we should do so when deleting. */
970 internal_delete_file (name
);
974 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
976 "Send text from START to END to a synchronous process running PROGRAM.\n\
977 The remaining arguments are optional.\n\
978 Delete the text if fourth arg DELETE is non-nil.\n\
980 Insert output in BUFFER before point; t means current buffer;\n\
981 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
982 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
983 REAL-BUFFER says what to do with standard output, as above,\n\
984 while STDERR-FILE says what to do with standard error in the child.\n\
985 STDERR-FILE may be nil (discard standard error output),\n\
986 t (mix it with ordinary output), or a file name string.\n\
988 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
989 Remaining args are passed to PROGRAM at startup as command args.\n\
991 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
992 Otherwise it waits for PROGRAM to terminate\n\
993 and returns a numeric exit status or a signal description string.\n\
994 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
997 register Lisp_Object
*args
;
1000 Lisp_Object filename_string
;
1001 register Lisp_Object start
, end
;
1002 int count
= specpdl_ptr
- specpdl
;
1003 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1004 Lisp_Object coding_systems
;
1005 Lisp_Object val
, *args2
;
1011 if ((outf
= egetenv ("TMPDIR"))
1012 || (outf
= egetenv ("TMP"))
1013 || (outf
= egetenv ("TEMP")))
1014 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
1017 tempfile
= alloca (20);
1020 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
1021 strcat (tempfile
, "/");
1022 if ('/' == DIRECTORY_SEP
)
1023 dostounix_filename (tempfile
);
1025 unixtodos_filename (tempfile
);
1027 strcat (tempfile
, "emXXXXXX");
1029 strcat (tempfile
, "detmp.XXX");
1031 #else /* not DOS_NT */
1032 char *tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
1033 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
1034 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
1035 #endif /* not DOS_NT */
1037 coding_systems
= Qt
;
1041 int fd
= mkstemp (tempfile
);
1043 report_file_error ("Failed to open temporary file",
1044 Fcons (Vtemp_file_name_pattern
, Qnil
));
1052 filename_string
= build_string (tempfile
);
1053 GCPRO1 (filename_string
);
1056 /* Decide coding-system of the contents of the temporary file. */
1057 if (!NILP (Vcoding_system_for_write
))
1058 val
= Vcoding_system_for_write
;
1059 else if (NILP (current_buffer
->enable_multibyte_characters
))
1063 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1064 args2
[0] = Qcall_process_region
;
1065 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1066 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1067 if (CONSP (coding_systems
))
1068 val
= XCDR (coding_systems
);
1069 else if (CONSP (Vdefault_process_coding_system
))
1070 val
= XCDR (Vdefault_process_coding_system
);
1076 int count1
= specpdl_ptr
- specpdl
;
1078 specbind (intern ("coding-system-for-write"), val
);
1079 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1081 unbind_to (count1
, Qnil
);
1084 /* Note that Fcall_process takes care of binding
1085 coding-system-for-read. */
1087 record_unwind_protect (delete_temp_file
, filename_string
);
1089 if (nargs
> 3 && !NILP (args
[3]))
1090 Fdelete_region (start
, end
);
1102 args
[1] = filename_string
;
1104 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1107 #ifndef VMS /* VMS version is in vmsproc.c. */
1109 static int relocate_fd ();
1111 /* This is the last thing run in a newly forked inferior
1112 either synchronous or asynchronous.
1113 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1114 Initialize inferior's priority, pgrp, connected dir and environment.
1115 then exec another program based on new_argv.
1117 This function may change environ for the superior process.
1118 Therefore, the superior process must save and restore the value
1119 of environ around the vfork and the call to this function.
1121 SET_PGRP is nonzero if we should put the subprocess into a separate
1124 CURRENT_DIR is an elisp string giving the path of the current
1125 directory the subprocess should have. Since we can't really signal
1126 a decent error from within the child, this should be verified as an
1127 executable directory by the parent. */
1130 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1132 register char **new_argv
;
1134 Lisp_Object current_dir
;
1141 #endif /* WINDOWSNT */
1143 int pid
= getpid ();
1145 #ifdef SET_EMACS_PRIORITY
1147 extern int emacs_priority
;
1149 if (emacs_priority
< 0)
1150 nice (- emacs_priority
);
1155 /* Close Emacs's descriptors that this process should not have. */
1156 close_process_descs ();
1158 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1159 we will lose if we call close_load_descs here. */
1161 close_load_descs ();
1164 /* Note that use of alloca is always safe here. It's obvious for systems
1165 that do not have true vfork or that have true (stack) alloca.
1166 If using vfork and C_ALLOCA it is safe because that changes
1167 the superior's static variables as if the superior had done alloca
1168 and will be cleaned up in the usual way. */
1170 register char *temp
;
1173 i
= STRING_BYTES (XSTRING (current_dir
));
1175 /* MSDOS must have all environment variables malloc'ed, because
1176 low-level libc functions that launch subsidiary processes rely
1178 pwd_var
= (char *) xmalloc (i
+ 6);
1180 pwd_var
= (char *) alloca (i
+ 6);
1183 bcopy ("PWD=", pwd_var
, 4);
1184 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
1185 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1189 /* We can't signal an Elisp error here; we're in a vfork. Since
1190 the callers check the current directory before forking, this
1191 should only return an error if the directory's permissions
1192 are changed between the check and this chdir, but we should
1194 if (chdir (temp
) < 0)
1199 /* Get past the drive letter, so that d:/ is left alone. */
1200 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1207 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1208 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1212 /* Set `env' to a vector of the strings in Vprocess_environment. */
1214 register Lisp_Object tem
;
1215 register char **new_env
;
1216 register int new_length
;
1219 for (tem
= Vprocess_environment
;
1220 CONSP (tem
) && STRINGP (XCAR (tem
));
1224 /* new_length + 2 to include PWD and terminating 0. */
1225 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1227 /* If we have a PWD envvar, pass one down,
1228 but with corrected value. */
1230 *new_env
++ = pwd_var
;
1232 /* Copy the Vprocess_environment strings into new_env. */
1233 for (tem
= Vprocess_environment
;
1234 CONSP (tem
) && STRINGP (XCAR (tem
));
1238 char *string
= (char *) XSTRING (XCAR (tem
))->data
;
1239 /* See if this string duplicates any string already in the env.
1240 If so, don't put it in.
1241 When an env var has multiple definitions,
1242 we keep the definition that comes first in process-environment. */
1243 for (; ep
!= new_env
; ep
++)
1245 char *p
= *ep
, *q
= string
;
1249 /* The string is malformed; might as well drop it. */
1258 *new_env
++ = string
;
1264 prepare_standard_handles (in
, out
, err
, handles
);
1265 set_process_dir (XSTRING (current_dir
)->data
);
1266 #else /* not WINDOWSNT */
1267 /* Make sure that in, out, and err are not actually already in
1268 descriptors zero, one, or two; this could happen if Emacs is
1269 started with its standard in, out, or error closed, as might
1272 int oin
= in
, oout
= out
;
1274 /* We have to avoid relocating the same descriptor twice! */
1276 in
= relocate_fd (in
, 3);
1281 out
= relocate_fd (out
, 3);
1285 else if (err
== oout
)
1288 err
= relocate_fd (err
, 3);
1302 #endif /* not MSDOS */
1303 #endif /* not WINDOWSNT */
1305 #if defined(USG) && !defined(BSD_PGRPS)
1306 #ifndef SETPGRP_RELEASES_CTTY
1307 setpgrp (); /* No arguments but equivalent in this case */
1312 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1313 EMACS_SET_TTY_PGRP (0, &pid
);
1316 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1319 /* An error occurred while trying to run the subprocess. */
1320 report_file_error ("Spawning child process", Qnil
);
1322 #else /* not MSDOS */
1324 /* Spawn the child. (See ntproc.c:Spawnve). */
1325 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1326 reset_standard_handles (in
, out
, err
, handles
);
1328 /* An error occurred while trying to spawn the process. */
1329 report_file_error ("Spawning child process", Qnil
);
1331 #else /* not WINDOWSNT */
1332 /* execvp does not accept an environment arg so the only way
1333 to pass this environment is to set environ. Our caller
1334 is responsible for restoring the ambient value of environ. */
1336 execvp (new_argv
[0], new_argv
);
1338 emacs_write (1, "Can't exec program: ", 20);
1339 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1340 emacs_write (1, "\n", 1);
1342 #endif /* not WINDOWSNT */
1343 #endif /* not MSDOS */
1346 /* Move the file descriptor FD so that its number is not less than MINFD.
1347 If the file descriptor is moved at all, the original is freed. */
1349 relocate_fd (fd
, minfd
)
1359 char *message1
= "Error while setting up child: ";
1360 char *errmessage
= strerror (errno
);
1361 char *message2
= "\n";
1362 emacs_write (2, message1
, strlen (message1
));
1363 emacs_write (2, errmessage
, strlen (errmessage
));
1364 emacs_write (2, message2
, strlen (message2
));
1367 /* Note that we hold the original FD open while we recurse,
1368 to guarantee we'll get a new FD if we need it. */
1369 new = relocate_fd (new, minfd
);
1376 getenv_internal (var
, varlen
, value
, valuelen
)
1384 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1388 entry
= XCAR (scan
);
1390 && STRING_BYTES (XSTRING (entry
)) > varlen
1391 && XSTRING (entry
)->data
[varlen
] == '='
1393 /* NT environment variables are case insensitive. */
1394 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1395 #else /* not WINDOWSNT */
1396 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1397 #endif /* not WINDOWSNT */
1400 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1401 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1409 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1410 "Return the value of environment variable VAR, as a string.\n\
1411 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1412 This function consults the variable ``process-environment'' for its value.")
1419 CHECK_STRING (var
, 0);
1420 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1422 return make_string (value
, valuelen
);
1427 /* A version of getenv that consults process_environment, easily
1436 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1442 #endif /* not VMS */
1444 /* This is run before init_cmdargs. */
1449 char *data_dir
= egetenv ("EMACSDATA");
1450 char *doc_dir
= egetenv ("EMACSDOC");
1453 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1456 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1459 /* Check the EMACSPATH environment variable, defaulting to the
1460 PATH_EXEC path from epaths.h. */
1461 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1462 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1463 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1466 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1471 char *data_dir
= egetenv ("EMACSDATA");
1474 Lisp_Object tempdir
;
1476 if (!NILP (Vinstallation_directory
))
1478 /* Add to the path the lib-src subdir of the installation dir. */
1480 tem
= Fexpand_file_name (build_string ("lib-src"),
1481 Vinstallation_directory
);
1483 /* MSDOS uses wrapped binaries, so don't do this. */
1484 if (NILP (Fmember (tem
, Vexec_path
)))
1485 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1487 Vexec_directory
= Ffile_name_as_directory (tem
);
1488 #endif /* not DOS_NT */
1490 /* Maybe use ../etc as well as ../lib-src. */
1493 tem
= Fexpand_file_name (build_string ("etc"),
1494 Vinstallation_directory
);
1495 Vdoc_directory
= Ffile_name_as_directory (tem
);
1499 /* Look for the files that should be in etc. We don't use
1500 Vinstallation_directory, because these files are never installed
1501 near the executable, and they are never in the build
1502 directory when that's different from the source directory.
1504 Instead, if these files are not in the nominal place, we try the
1505 source directory. */
1508 Lisp_Object tem
, tem1
, newdir
;
1510 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1511 tem1
= Ffile_exists_p (tem
);
1514 newdir
= Fexpand_file_name (build_string ("../etc/"),
1515 build_string (PATH_DUMPLOADSEARCH
));
1516 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1517 tem1
= Ffile_exists_p (tem
);
1519 Vdata_directory
= newdir
;
1527 tempdir
= Fdirectory_file_name (Vexec_directory
);
1528 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1529 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1533 tempdir
= Fdirectory_file_name (Vdata_directory
);
1534 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1535 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1539 Vshell_file_name
= build_string ("*dcl*");
1541 sh
= (char *) getenv ("SHELL");
1542 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1546 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1548 if (getenv ("TMPDIR"))
1550 char *dir
= getenv ("TMPDIR");
1551 Vtemp_file_name_pattern
1552 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1553 build_string (dir
));
1556 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1561 set_process_environment ()
1563 register char **envp
;
1565 Vprocess_environment
= Qnil
;
1569 for (envp
= environ
; *envp
; envp
++)
1570 Vprocess_environment
= Fcons (build_string (*envp
),
1571 Vprocess_environment
);
1578 Qbuffer_file_type
= intern ("buffer-file-type");
1579 staticpro (&Qbuffer_file_type
);
1582 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1583 "*File name to load inferior shells from.\n\
1584 Initialized from the SHELL environment variable.");
1586 DEFVAR_LISP ("exec-path", &Vexec_path
,
1587 "*List of directories to search programs to run in subprocesses.\n\
1588 Each element is a string (directory name) or nil (try default directory).");
1590 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1591 "Directory for executables for Emacs to invoke.\n\
1592 More generally, this includes any architecture-dependent files\n\
1593 that are built and installed from the Emacs distribution.");
1595 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1596 "Directory of machine-independent files that come with GNU Emacs.\n\
1597 These are files intended for Emacs to use while it runs.");
1599 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1600 "Directory containing the DOC file that comes with GNU Emacs.\n\
1601 This is usually the same as data-directory.");
1603 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1604 "For internal use by the build procedure only.\n\
1605 This is the name of the directory in which the build procedure installed\n\
1606 Emacs's info files; the default value for Info-default-directory-list\n\
1608 Vconfigure_info_directory
= build_string (PATH_INFO
);
1610 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1611 "Pattern for making names for temporary files.\n\
1612 This is used by `call-process-region'.");
1613 /* This variable is initialized in init_callproc. */
1615 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1616 "List of environment variables for subprocesses to inherit.\n\
1617 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1618 The environment which Emacs inherits is placed in this variable\n\
1619 when Emacs starts.");
1622 defsubr (&Scall_process
);
1623 defsubr (&Sgetenv_internal
);
1625 defsubr (&Scall_process_region
);