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;
472 /* Replaced by close_process_descs */
473 set_exclusive_use (fd
[0]);
478 /* child_setup must clobber environ in systems with true vfork.
479 Protect it from permanent change. */
480 register char **save_environ
= environ
;
481 register int fd1
= fd
[1];
484 #if 0 /* Some systems don't have sigblock. */
485 mask
= sigblock (sigmask (SIGCHLD
));
488 /* Record that we're about to create a synchronous process. */
489 synch_process_alive
= 1;
491 /* These vars record information from process termination.
492 Clear them now before process can possibly terminate,
493 to avoid timing error if process terminates soon. */
494 synch_process_death
= 0;
495 synch_process_retcode
= 0;
497 if (NILP (error_file
))
498 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
499 else if (STRINGP (error_file
))
502 fd_error
= emacs_open (XSTRING (error_file
)->data
,
503 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
505 #else /* not DOS_NT */
506 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
507 #endif /* not DOS_NT */
512 emacs_close (filefd
);
520 report_file_error ("Cannot redirect stderr",
521 Fcons ((NILP (error_file
)
522 ? build_string (NULL_DEVICE
) : error_file
),
526 current_dir
= ENCODE_FILE (current_dir
);
530 /* Call run_mac_command in sysdep.c here directly instead of doing
531 a child_setup as for MSDOS and other platforms. Note that this
532 code does not handle passing the environment to the synchronous
534 char *infn
, *outfn
, *errfn
, *currdn
;
536 /* close these files so subprocess can write to them */
538 if (fd_error
!= outfilefd
)
540 fd1
= -1; /* No harm in closing that one! */
542 infn
= XSTRING (infile
)->data
;
544 if (NILP (error_file
))
546 else if (EQ (Qt
, error_file
))
549 errfn
= XSTRING (error_file
)->data
;
550 currdn
= XSTRING (current_dir
)->data
;
551 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
553 /* Record that the synchronous process exited and note its
554 termination status. */
555 synch_process_alive
= 0;
556 synch_process_retcode
= pid
;
557 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
559 synchronize_system_messages_locale ();
560 synch_process_death
= strerror (errno
);
563 /* Since CRLF is converted to LF within `decode_coding', we can
564 always open a file with binary mode. */
565 fd
[0] = open (tempfile
, O_BINARY
);
570 report_file_error ("Cannot re-open temporary file", Qnil
);
573 #else /* not macintosh */
574 #ifdef MSDOS /* MW, July 1993 */
575 /* Note that on MSDOS `child_setup' actually returns the child process
576 exit status, not its PID, so we assign it to `synch_process_retcode'
578 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
581 /* Record that the synchronous process exited and note its
582 termination status. */
583 synch_process_alive
= 0;
584 synch_process_retcode
= pid
;
585 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
587 synchronize_system_messages_locale ();
588 synch_process_death
= strerror (errno
);
591 emacs_close (outfilefd
);
592 if (fd_error
!= outfilefd
)
593 emacs_close (fd_error
);
594 fd1
= -1; /* No harm in closing that one! */
595 /* Since CRLF is converted to LF within `decode_coding', we can
596 always open a file with binary mode. */
597 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
601 emacs_close (filefd
);
602 report_file_error ("Cannot re-open temporary file", Qnil
);
604 #else /* not MSDOS */
606 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
608 #else /* not WINDOWSNT */
618 #if defined (USG) && !defined (BSD_PGRPS)
623 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
626 #endif /* not WINDOWSNT */
628 /* The MSDOS case did this already. */
630 emacs_close (fd_error
);
631 #endif /* not MSDOS */
632 #endif /* not macintosh */
634 environ
= save_environ
;
636 /* Close most of our fd's, but not fd[0]
637 since we will use that to read input from. */
638 emacs_close (filefd
);
639 if (fd1
>= 0 && fd1
!= fd_error
)
647 report_file_error ("Doing vfork", Qnil
);
650 if (INTEGERP (buffer
))
655 /* If Emacs has been built with asynchronous subprocess support,
656 we don't need to do this, I think because it will then have
657 the facilities for handling SIGCHLD. */
658 wait_without_blocking ();
659 #endif /* subprocesses */
663 /* Enable sending signal if user quits below. */
664 call_process_exited
= 0;
666 #if defined(MSDOS) || defined(macintosh)
667 /* MSDOS needs different cleanup information. */
668 record_unwind_protect (call_process_cleanup
,
669 Fcons (make_number (fd
[0]), build_string (tempfile
)));
671 record_unwind_protect (call_process_cleanup
,
672 Fcons (make_number (fd
[0]), make_number (pid
)));
673 #endif /* not MSDOS and not macintosh */
676 if (BUFFERP (buffer
))
677 Fset_buffer (buffer
);
681 /* If BUFFER is nil, we must read process output once and then
682 discard it, so setup coding system but with nil. */
683 setup_coding_system (Qnil
, &process_coding
);
687 Lisp_Object val
, *args2
;
690 if (!NILP (Vcoding_system_for_read
))
691 val
= Vcoding_system_for_read
;
694 if (EQ (coding_systems
, Qt
))
698 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
699 args2
[0] = Qcall_process
;
700 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
702 = Ffind_operation_coding_system (nargs
+ 1, args2
);
704 if (CONSP (coding_systems
))
705 val
= XCAR (coding_systems
);
706 else if (CONSP (Vdefault_process_coding_system
))
707 val
= XCAR (Vdefault_process_coding_system
);
711 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
712 /* In unibyte mode, character code conversion should not take
713 place but EOL conversion should. So, setup raw-text or one
714 of the subsidiary according to the information just setup. */
715 if (NILP (current_buffer
->enable_multibyte_characters
)
717 setup_raw_text_coding_system (&process_coding
);
719 process_coding
.src_multibyte
= 0;
720 process_coding
.dst_multibyte
722 ? ! NILP (XBUFFER (buffer
)->enable_multibyte_characters
)
723 : ! NILP (current_buffer
->enable_multibyte_characters
));
733 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
734 struct coding_system saved_coding
;
735 int pt_orig
= PT
, pt_byte_orig
= PT_BYTE
;
738 saved_coding
= process_coding
;
739 if (process_coding
.composing
!= COMPOSITION_DISABLED
)
740 coding_allocate_composition_data (&process_coding
, PT
);
743 /* Repeatedly read until we've filled as much as possible
744 of the buffer size we have. But don't read
745 less than 1024--save that for the next bufferful. */
747 while (nread
< bufsize
- 1024)
749 int this_read
= emacs_read (fd
[0], bufptr
+ nread
,
757 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
762 total_read
+= this_read
;
764 if (display_on_the_fly
)
768 /* Now NREAD is the total amount of data in the buffer. */
773 if (! CODING_MAY_REQUIRE_DECODING (&process_coding
))
774 insert_1_both (bufptr
, nread
, nread
, 0, 1, 0);
776 { /* We have to decode the input. */
781 size
= decoding_buffer_size (&process_coding
, nread
);
782 decoding_buf
= (char *) xmalloc (size
);
784 if (process_coding
.cmp_data
)
785 process_coding
.cmp_data
->char_offset
= PT
;
787 decode_coding (&process_coding
, bufptr
, decoding_buf
,
790 if (display_on_the_fly
791 && saved_coding
.type
== coding_type_undecided
792 && process_coding
.type
!= coding_type_undecided
)
794 /* We have detected some coding system. But,
795 there's a possibility that the detection was
796 done by insufficient data. So, we give up
797 displaying on the fly. */
798 xfree (decoding_buf
);
799 display_on_the_fly
= 0;
800 process_coding
= saved_coding
;
805 if (process_coding
.produced
> 0)
806 insert_1_both (decoding_buf
, process_coding
.produced_char
,
807 process_coding
.produced
, 0, 1, 0);
808 xfree (decoding_buf
);
810 if (process_coding
.result
== CODING_FINISH_INCONSISTENT_EOL
)
812 Lisp_Object eol_type
, coding
;
814 if (process_coding
.eol_type
== CODING_EOL_CR
)
816 /* CRs have been replaced with LFs. Undo
817 that in the text inserted above. */
820 move_gap_both (PT
, PT_BYTE
);
822 p
= BYTE_POS_ADDR (pt_byte_orig
);
823 for (; p
< GPT_ADDR
; ++p
)
827 else if (process_coding
.eol_type
== CODING_EOL_CRLF
)
829 /* CR LFs have been replaced with LFs. Undo
830 that by inserting CRs in front of LFs in
831 the text inserted above. */
832 EMACS_INT bytepos
, old_pt
, old_pt_byte
, nCR
;
835 old_pt_byte
= PT_BYTE
;
838 for (bytepos
= PT_BYTE
- 1;
839 bytepos
>= pt_byte_orig
;
841 if (FETCH_BYTE (bytepos
) == '\n')
843 EMACS_INT charpos
= BYTE_TO_CHAR (bytepos
);
844 TEMP_SET_PT_BOTH (charpos
, bytepos
);
845 insert_1_both ("\r", 1, 1, 0, 1, 0);
849 TEMP_SET_PT_BOTH (old_pt
+ nCR
, old_pt_byte
+ nCR
);
852 /* Set the coding system symbol to that for
854 eol_type
= Fget (saved_coding
.symbol
, Qeol_type
);
855 if (VECTORP (eol_type
)
856 && ASIZE (eol_type
) == 3
857 && SYMBOLP (AREF (eol_type
, CODING_EOL_LF
)))
858 coding
= AREF (eol_type
, CODING_EOL_LF
);
860 coding
= saved_coding
.symbol
;
862 process_coding
.symbol
= coding
;
863 process_coding
.eol_type
= CODING_EOL_LF
;
865 &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
868 nread
-= process_coding
.consumed
;
871 /* As CARRYOVER should not be that large, we had
872 better avoid overhead of bcopy. */
873 BCOPY_SHORT (bufptr
+ process_coding
.consumed
, bufptr
,
875 if (process_coding
.result
== CODING_FINISH_INSUFFICIENT_CMP
)
877 /* The decoding ended because of insufficient data
878 area to record information about composition.
879 We must try decoding with additional data area
880 before reading more output for the process. */
881 coding_allocate_composition_data (&process_coding
, PT
);
882 goto repeat_decoding
;
887 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
890 /* Make the buffer bigger as we continue to read more data,
892 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
895 bufptr
= (char *) alloca (bufsize
);
898 if (!NILP (display
) && INTERACTIVE
)
901 prepare_menu_bars ();
903 redisplay_preserve_echo_area (1);
911 && process_coding
.cmp_data
)
913 coding_restore_composition (&process_coding
, Fcurrent_buffer ());
914 coding_free_composition_data (&process_coding
);
918 int post_read_count
= specpdl_ptr
- specpdl
;
920 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
921 inserted
= PT
- pt_orig
;
922 TEMP_SET_PT_BOTH (pt_orig
, pt_byte_orig
);
923 if (SYMBOLP (process_coding
.post_read_conversion
)
924 && !NILP (Ffboundp (process_coding
.post_read_conversion
)))
925 call1 (process_coding
.post_read_conversion
, make_number (inserted
));
927 Vlast_coding_system_used
= process_coding
.symbol
;
929 /* If the caller required, let the buffer inherit the
930 coding-system used to decode the process output. */
931 if (inherit_process_coding_system
)
932 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
933 make_number (total_read
));
935 unbind_to (post_read_count
, Qnil
);
939 /* Wait for it to terminate, unless it already has. */
940 wait_for_termination (pid
);
944 set_buffer_internal (old
);
946 /* Don't kill any children that the subprocess may have left behind
948 call_process_exited
= 1;
950 unbind_to (count
, Qnil
);
952 if (synch_process_death
)
953 return code_convert_string_norecord (build_string (synch_process_death
),
954 Vlocale_coding_system
, 0);
955 return make_number (synch_process_retcode
);
960 delete_temp_file (name
)
963 /* Use Fdelete_file (indirectly) because that runs a file name handler.
964 We did that when writing the file, so we should do so when deleting. */
965 internal_delete_file (name
);
969 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
971 "Send text from START to END to a synchronous process running PROGRAM.\n\
972 The remaining arguments are optional.\n\
973 Delete the text if fourth arg DELETE is non-nil.\n\
975 Insert output in BUFFER before point; t means current buffer;\n\
976 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
977 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
978 REAL-BUFFER says what to do with standard output, as above,\n\
979 while STDERR-FILE says what to do with standard error in the child.\n\
980 STDERR-FILE may be nil (discard standard error output),\n\
981 t (mix it with ordinary output), or a file name string.\n\
983 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
984 Remaining args are passed to PROGRAM at startup as command args.\n\
986 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
987 Otherwise it waits for PROGRAM to terminate\n\
988 and returns a numeric exit status or a signal description string.\n\
989 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
992 register Lisp_Object
*args
;
995 Lisp_Object filename_string
;
996 register Lisp_Object start
, end
;
997 int count
= specpdl_ptr
- specpdl
;
998 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
999 Lisp_Object coding_systems
;
1000 Lisp_Object val
, *args2
;
1006 if ((outf
= egetenv ("TMPDIR"))
1007 || (outf
= egetenv ("TMP"))
1008 || (outf
= egetenv ("TEMP")))
1009 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
1012 tempfile
= alloca (20);
1015 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
1016 strcat (tempfile
, "/");
1017 if ('/' == DIRECTORY_SEP
)
1018 dostounix_filename (tempfile
);
1020 unixtodos_filename (tempfile
);
1022 strcat (tempfile
, "emXXXXXX");
1024 strcat (tempfile
, "detmp.XXX");
1026 #else /* not DOS_NT */
1027 char *tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
1028 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
1029 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
1030 #endif /* not DOS_NT */
1032 coding_systems
= Qt
;
1036 int fd
= mkstemp (tempfile
);
1038 report_file_error ("Failed to open temporary file",
1039 Fcons (Vtemp_file_name_pattern
, Qnil
));
1047 filename_string
= build_string (tempfile
);
1048 GCPRO1 (filename_string
);
1051 /* Decide coding-system of the contents of the temporary file. */
1052 if (!NILP (Vcoding_system_for_write
))
1053 val
= Vcoding_system_for_write
;
1054 else if (NILP (current_buffer
->enable_multibyte_characters
))
1058 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1059 args2
[0] = Qcall_process_region
;
1060 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1061 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1062 if (CONSP (coding_systems
))
1063 val
= XCDR (coding_systems
);
1064 else if (CONSP (Vdefault_process_coding_system
))
1065 val
= XCDR (Vdefault_process_coding_system
);
1071 int count1
= specpdl_ptr
- specpdl
;
1073 specbind (intern ("coding-system-for-write"), val
);
1074 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1076 unbind_to (count1
, Qnil
);
1079 /* Note that Fcall_process takes care of binding
1080 coding-system-for-read. */
1082 record_unwind_protect (delete_temp_file
, filename_string
);
1084 if (nargs
> 3 && !NILP (args
[3]))
1085 Fdelete_region (start
, end
);
1097 args
[1] = filename_string
;
1099 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1102 #ifndef VMS /* VMS version is in vmsproc.c. */
1104 static int relocate_fd ();
1106 /* This is the last thing run in a newly forked inferior
1107 either synchronous or asynchronous.
1108 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1109 Initialize inferior's priority, pgrp, connected dir and environment.
1110 then exec another program based on new_argv.
1112 This function may change environ for the superior process.
1113 Therefore, the superior process must save and restore the value
1114 of environ around the vfork and the call to this function.
1116 SET_PGRP is nonzero if we should put the subprocess into a separate
1119 CURRENT_DIR is an elisp string giving the path of the current
1120 directory the subprocess should have. Since we can't really signal
1121 a decent error from within the child, this should be verified as an
1122 executable directory by the parent. */
1125 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1127 register char **new_argv
;
1129 Lisp_Object current_dir
;
1136 #endif /* WINDOWSNT */
1138 int pid
= getpid ();
1140 #ifdef SET_EMACS_PRIORITY
1142 extern int emacs_priority
;
1144 if (emacs_priority
< 0)
1145 nice (- emacs_priority
);
1150 /* Close Emacs's descriptors that this process should not have. */
1151 close_process_descs ();
1153 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1154 we will lose if we call close_load_descs here. */
1156 close_load_descs ();
1159 /* Note that use of alloca is always safe here. It's obvious for systems
1160 that do not have true vfork or that have true (stack) alloca.
1161 If using vfork and C_ALLOCA it is safe because that changes
1162 the superior's static variables as if the superior had done alloca
1163 and will be cleaned up in the usual way. */
1165 register char *temp
;
1168 i
= STRING_BYTES (XSTRING (current_dir
));
1170 /* MSDOS must have all environment variables malloc'ed, because
1171 low-level libc functions that launch subsidiary processes rely
1173 pwd_var
= (char *) xmalloc (i
+ 6);
1175 pwd_var
= (char *) alloca (i
+ 6);
1178 bcopy ("PWD=", pwd_var
, 4);
1179 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
1180 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1184 /* We can't signal an Elisp error here; we're in a vfork. Since
1185 the callers check the current directory before forking, this
1186 should only return an error if the directory's permissions
1187 are changed between the check and this chdir, but we should
1189 if (chdir (temp
) < 0)
1194 /* Get past the drive letter, so that d:/ is left alone. */
1195 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1202 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1203 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1207 /* Set `env' to a vector of the strings in Vprocess_environment. */
1209 register Lisp_Object tem
;
1210 register char **new_env
;
1211 register int new_length
;
1214 for (tem
= Vprocess_environment
;
1215 CONSP (tem
) && STRINGP (XCAR (tem
));
1219 /* new_length + 2 to include PWD and terminating 0. */
1220 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1222 /* If we have a PWD envvar, pass one down,
1223 but with corrected value. */
1225 *new_env
++ = pwd_var
;
1227 /* Copy the Vprocess_environment strings into new_env. */
1228 for (tem
= Vprocess_environment
;
1229 CONSP (tem
) && STRINGP (XCAR (tem
));
1233 char *string
= (char *) XSTRING (XCAR (tem
))->data
;
1234 /* See if this string duplicates any string already in the env.
1235 If so, don't put it in.
1236 When an env var has multiple definitions,
1237 we keep the definition that comes first in process-environment. */
1238 for (; ep
!= new_env
; ep
++)
1240 char *p
= *ep
, *q
= string
;
1244 /* The string is malformed; might as well drop it. */
1253 *new_env
++ = string
;
1259 prepare_standard_handles (in
, out
, err
, handles
);
1260 set_process_dir (XSTRING (current_dir
)->data
);
1261 #else /* not WINDOWSNT */
1262 /* Make sure that in, out, and err are not actually already in
1263 descriptors zero, one, or two; this could happen if Emacs is
1264 started with its standard in, out, or error closed, as might
1267 int oin
= in
, oout
= out
;
1269 /* We have to avoid relocating the same descriptor twice! */
1271 in
= relocate_fd (in
, 3);
1276 out
= relocate_fd (out
, 3);
1280 else if (err
== oout
)
1283 err
= relocate_fd (err
, 3);
1297 #endif /* not MSDOS */
1298 #endif /* not WINDOWSNT */
1300 #if defined(USG) && !defined(BSD_PGRPS)
1301 #ifndef SETPGRP_RELEASES_CTTY
1302 setpgrp (); /* No arguments but equivalent in this case */
1307 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1308 EMACS_SET_TTY_PGRP (0, &pid
);
1311 something missing here
;
1315 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1318 /* An error occurred while trying to run the subprocess. */
1319 report_file_error ("Spawning child process", Qnil
);
1321 #else /* not MSDOS */
1323 /* Spawn the child. (See ntproc.c:Spawnve). */
1324 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1325 reset_standard_handles (in
, out
, err
, handles
);
1327 /* An error occurred while trying to spawn the process. */
1328 report_file_error ("Spawning child process", Qnil
);
1330 #else /* not WINDOWSNT */
1331 /* execvp does not accept an environment arg so the only way
1332 to pass this environment is to set environ. Our caller
1333 is responsible for restoring the ambient value of environ. */
1335 execvp (new_argv
[0], new_argv
);
1337 emacs_write (1, "Can't exec program: ", 20);
1338 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1339 emacs_write (1, "\n", 1);
1341 #endif /* not WINDOWSNT */
1342 #endif /* not MSDOS */
1345 /* Move the file descriptor FD so that its number is not less than MINFD.
1346 If the file descriptor is moved at all, the original is freed. */
1348 relocate_fd (fd
, minfd
)
1358 char *message1
= "Error while setting up child: ";
1359 char *errmessage
= strerror (errno
);
1360 char *message2
= "\n";
1361 emacs_write (2, message1
, strlen (message1
));
1362 emacs_write (2, errmessage
, strlen (errmessage
));
1363 emacs_write (2, message2
, strlen (message2
));
1366 /* Note that we hold the original FD open while we recurse,
1367 to guarantee we'll get a new FD if we need it. */
1368 new = relocate_fd (new, minfd
);
1375 getenv_internal (var
, varlen
, value
, valuelen
)
1383 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1387 entry
= XCAR (scan
);
1389 && STRING_BYTES (XSTRING (entry
)) > varlen
1390 && XSTRING (entry
)->data
[varlen
] == '='
1392 /* NT environment variables are case insensitive. */
1393 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1394 #else /* not WINDOWSNT */
1395 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1396 #endif /* not WINDOWSNT */
1399 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1400 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1408 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1409 "Return the value of environment variable VAR, as a string.\n\
1410 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1411 This function consults the variable ``process-environment'' for its value.")
1418 CHECK_STRING (var
, 0);
1419 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1421 return make_string (value
, valuelen
);
1426 /* A version of getenv that consults process_environment, easily
1435 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1441 #endif /* not VMS */
1443 /* This is run before init_cmdargs. */
1448 char *data_dir
= egetenv ("EMACSDATA");
1449 char *doc_dir
= egetenv ("EMACSDOC");
1452 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1455 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1458 /* Check the EMACSPATH environment variable, defaulting to the
1459 PATH_EXEC path from epaths.h. */
1460 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1461 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1462 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1465 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1470 char *data_dir
= egetenv ("EMACSDATA");
1473 Lisp_Object tempdir
;
1475 if (!NILP (Vinstallation_directory
))
1477 /* Add to the path the lib-src subdir of the installation dir. */
1479 tem
= Fexpand_file_name (build_string ("lib-src"),
1480 Vinstallation_directory
);
1482 /* MSDOS uses wrapped binaries, so don't do this. */
1483 if (NILP (Fmember (tem
, Vexec_path
)))
1484 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1486 Vexec_directory
= Ffile_name_as_directory (tem
);
1487 #endif /* not DOS_NT */
1489 /* Maybe use ../etc as well as ../lib-src. */
1492 tem
= Fexpand_file_name (build_string ("etc"),
1493 Vinstallation_directory
);
1494 Vdoc_directory
= Ffile_name_as_directory (tem
);
1498 /* Look for the files that should be in etc. We don't use
1499 Vinstallation_directory, because these files are never installed
1500 near the executable, and they are never in the build
1501 directory when that's different from the source directory.
1503 Instead, if these files are not in the nominal place, we try the
1504 source directory. */
1507 Lisp_Object tem
, tem1
, newdir
;
1509 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1510 tem1
= Ffile_exists_p (tem
);
1513 newdir
= Fexpand_file_name (build_string ("../etc/"),
1514 build_string (PATH_DUMPLOADSEARCH
));
1515 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1516 tem1
= Ffile_exists_p (tem
);
1518 Vdata_directory
= newdir
;
1526 tempdir
= Fdirectory_file_name (Vexec_directory
);
1527 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1528 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1532 tempdir
= Fdirectory_file_name (Vdata_directory
);
1533 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1534 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1538 Vshell_file_name
= build_string ("*dcl*");
1540 sh
= (char *) getenv ("SHELL");
1541 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1545 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1547 if (getenv ("TMPDIR"))
1549 char *dir
= getenv ("TMPDIR");
1550 Vtemp_file_name_pattern
1551 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1552 build_string (dir
));
1555 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1560 set_process_environment ()
1562 register char **envp
;
1564 Vprocess_environment
= Qnil
;
1568 for (envp
= environ
; *envp
; envp
++)
1569 Vprocess_environment
= Fcons (build_string (*envp
),
1570 Vprocess_environment
);
1577 Qbuffer_file_type
= intern ("buffer-file-type");
1578 staticpro (&Qbuffer_file_type
);
1581 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1582 "*File name to load inferior shells from.\n\
1583 Initialized from the SHELL environment variable.");
1585 DEFVAR_LISP ("exec-path", &Vexec_path
,
1586 "*List of directories to search programs to run in subprocesses.\n\
1587 Each element is a string (directory name) or nil (try default directory).");
1589 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1590 "Directory for executables for Emacs to invoke.\n\
1591 More generally, this includes any architecture-dependent files\n\
1592 that are built and installed from the Emacs distribution.");
1594 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1595 "Directory of machine-independent files that come with GNU Emacs.\n\
1596 These are files intended for Emacs to use while it runs.");
1598 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1599 "Directory containing the DOC file that comes with GNU Emacs.\n\
1600 This is usually the same as data-directory.");
1602 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1603 "For internal use by the build procedure only.\n\
1604 This is the name of the directory in which the build procedure installed\n\
1605 Emacs's info files; the default value for Info-default-directory-list\n\
1607 Vconfigure_info_directory
= build_string (PATH_INFO
);
1609 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1610 "Pattern for making names for temporary files.\n\
1611 This is used by `call-process-region'.");
1612 /* This variable is initialized in init_callproc. */
1614 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1615 "List of environment variables for subprocesses to inherit.\n\
1616 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1617 The environment which Emacs inherits is placed in this variable\n\
1618 when Emacs starts.");
1621 defsubr (&Scall_process
);
1622 defsubr (&Sgetenv_internal
);
1624 defsubr (&Scall_process_region
);