root/trunk/src/callproc.c

Revision 4220, 50.4 kB (checked in by miyoshi, 5 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to native
Line 
1 /* Synchronous subprocess invocation for GNU Emacs.
2    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3                  2002, 2003, 2004, 2005, 2006, 2007, 2008
4                  Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.  */
22
23
24 #include <config.h>
25 #include <signal.h>
26 #include <errno.h>
27 #include <stdio.h>
28
29 #ifndef USE_CRT_DLL
30 extern int errno;
31 #endif
32
33 /* Define SIGCHLD as an alias for SIGCLD.  */
34
35 #if !defined (SIGCHLD) && defined (SIGCLD)
36 #define SIGCHLD SIGCLD
37 #endif /* SIGCLD */
38
39 #include <sys/types.h>
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include <sys/file.h>
46 #ifdef HAVE_FCNTL_H
47 #define INCLUDED_FCNTL
48 #include <fcntl.h>
49 #endif
50
51 #ifdef WINDOWSNT
52 #define NOMINMAX
53 #include <windows.h>
54 #include <stdlib.h>     /* for proper declaration of environ */
55 #include <fcntl.h>
56 #include "w32.h"
57 #define _P_NOWAIT 1     /* from process.h */
58 #endif
59
60 #ifdef MSDOS    /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
61 #define INCLUDED_FCNTL
62 #include <fcntl.h>
63 #include <sys/stat.h>
64 #include <sys/param.h>
65 #include <errno.h>
66 #endif /* MSDOS */
67
68 #ifndef O_RDONLY
69 #define O_RDONLY 0
70 #endif
71
72 #ifndef O_WRONLY
73 #define O_WRONLY 1
74 #endif
75
76 #include "lisp.h"
77 #include "commands.h"
78 #include "buffer.h"
79 #include "charset.h"
80 #include "ccl.h"
81 #include "coding.h"
82 #include "composite.h"
83 #include <epaths.h>
84 #include "process.h"
85 #include "syssignal.h"
86 #include "systty.h"
87 #include "blockinput.h"
88
89 #ifdef MSDOS
90 #include "msdos.h"
91 #endif
92
93 #ifdef VMS
94 extern noshare char **environ;
95 #else
96 #ifndef USE_CRT_DLL
97 extern char **environ;
98 #endif
99 #endif
100
101 #ifdef HAVE_SETPGID
102 #if !defined (USG) || defined (BSD_PGRPS)
103 #undef setpgrp
104 #define setpgrp setpgid
105 #endif
106 #endif
107
108 Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes;
109 Lisp_Object Vdata_directory, Vdoc_directory;
110 Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory;
111 Lisp_Object Vtemp_file_name_pattern;
112
113 Lisp_Object Vshell_file_name;
114
115 Lisp_Object Vprocess_environment;
116
117 #ifdef MEADOW
118 int mw32_process_expects_pty;
119 Lisp_Object Vmw32_process_under_setup;
120 #endif
121
122 #ifdef DOS_NT
123 Lisp_Object Qbuffer_file_type;
124 #endif /* DOS_NT */
125
126 /* True if we are about to fork off a synchronous process or if we
127    are waiting for it.  */
128 int synch_process_alive;
129
130 /* Nonzero => this is a string explaining death of synchronous subprocess.  */
131 char *synch_process_death;
132
133 /* Nonzero => this is the signal number that terminated the subprocess.  */
134 int synch_process_termsig;
135
136 /* If synch_process_death is zero,
137    this is exit code of synchronous subprocess.  */
138 int synch_process_retcode;
139
140 /* Clean up when exiting Fcall_process.
141    On MSDOS, delete the temporary file on any kind of termination.
142    On Unix, kill the process and any children on termination by signal.  */
143
144 /* Nonzero if this is termination due to exit.  */
145 static int call_process_exited;
146
147 #ifndef VMS  /* VMS version is in vmsproc.c.  */
148
149 static Lisp_Object
150 call_process_kill (fdpid)
151      Lisp_Object fdpid;
152 {
153   emacs_close (XFASTINT (Fcar (fdpid)));
154   EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
155   synch_process_alive = 0;
156   return Qnil;
157 }
158
159 Lisp_Object
160 call_process_cleanup (fdpid)
161      Lisp_Object fdpid;
162 {
163 #if defined (MSDOS) || defined (MAC_OS8)
164   /* for MSDOS fdpid is really (fd . tempfile)  */
165   register Lisp_Object file;
166   file = Fcdr (fdpid);
167   emacs_close (XFASTINT (Fcar (fdpid)));
168   if (strcmp (SDATA (file), NULL_DEVICE) != 0)
169     unlink (SDATA (file));
170 #else /* not MSDOS and not MAC_OS8 */
171   register int pid = XFASTINT (Fcdr (fdpid));
172
173   if (call_process_exited)
174     {
175       emacs_close (XFASTINT (Fcar (fdpid)));
176       return Qnil;
177     }
178
179   if (EMACS_KILLPG (pid, SIGINT) == 0)
180     {
181       int count = SPECPDL_INDEX ();
182       record_unwind_protect (call_process_kill, fdpid);
183       message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
184       immediate_quit = 1;
185       QUIT;
186       wait_for_termination (pid);
187       immediate_quit = 0;
188       specpdl_ptr = specpdl + count; /* Discard the unwind protect.  */
189       message1 ("Waiting for process to die...done");
190     }
191   synch_process_alive = 0;
192   emacs_close (XFASTINT (Fcar (fdpid)));
193 #endif /* not MSDOS */
194   return Qnil;
195 }
196
197 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
198        doc: /* Call PROGRAM synchronously in separate process.
199 The remaining arguments are optional.
200 The program's input comes from file INFILE (nil means `/dev/null').
201 Insert output in BUFFER before point; t means current buffer;
202  nil for BUFFER means discard it; 0 means discard and don't wait.
203 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
204 REAL-BUFFER says what to do with standard output, as above,
205 while STDERR-FILE says what to do with standard error in the child.
206 STDERR-FILE may be nil (discard standard error output),
207 t (mix it with ordinary output), or a file name string.
208
209 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
210 Remaining arguments are strings passed as command arguments to PROGRAM.
211
212 If executable PROGRAM can't be found as an executable, `call-process'
213 signals a Lisp error.  `call-process' reports errors in execution of
214 the program only through its return and output.
215
216 If BUFFER is 0, `call-process' returns immediately with value nil.
217 Otherwise it waits for PROGRAM to terminate
218 and returns a numeric exit status or a signal description string.
219 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
220
221 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
222      (nargs, args)
223      int nargs;
224      register Lisp_Object *args;
225 {
226   Lisp_Object infile, buffer, current_dir, path;
227   int display_p;
228   int fd[2];
229   int filefd;
230   register int pid;
231 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
232 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
233   char buf[CALLPROC_BUFFER_SIZE_MAX];
234   int bufsize = CALLPROC_BUFFER_SIZE_MIN;
235   int count = SPECPDL_INDEX ();
236
237   register const unsigned char **new_argv
238     = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
239   struct buffer *old = current_buffer;
240   /* File to use for stderr in the child.
241      t means use same as standard output.  */
242   Lisp_Object error_file;
243 #ifdef MSDOS    /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
244   char *outf, *tempfile;
245   int outfilefd;
246 #endif
247 #ifdef MAC_OS8
248   char *tempfile;
249   int outfilefd;
250 #endif
251 #if 0
252   int mask;
253 #endif
254   struct coding_system process_coding; /* coding-system of process output */
255   struct coding_system argument_coding; /* coding-system of arguments */
256   /* Set to the return value of Ffind_operation_coding_system.  */
257   Lisp_Object coding_systems;
258
259   /* Qt denotes that Ffind_operation_coding_system is not yet called.  */
260   coding_systems = Qt;
261
262   CHECK_STRING (args[0]);
263
264   error_file = Qt;
265
266 #ifndef subprocesses
267   /* Without asynchronous processes we cannot have BUFFER == 0.  */
268   if (nargs >= 3
269       && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
270     error ("Operating system cannot handle asynchronous subprocesses");
271 #endif /* subprocesses */
272
273   /* Decide the coding-system for giving arguments.  */
274   {
275     Lisp_Object val, *args2;
276     int i;
277
278     /* If arguments are supplied, we may have to encode them.  */
279     if (nargs >= 5)
280       {
281         int must_encode = 0;
282
283         for (i = 4; i < nargs; i++)
284           CHECK_STRING (args[i]);
285
286         for (i = 4; i < nargs; i++)
287           if (STRING_MULTIBYTE (args[i]))
288             must_encode = 1;
289
290         if (!NILP (Vcoding_system_for_write))
291           val = Vcoding_system_for_write;
292         else if (! must_encode)
293           val = Qnil;
294         else
295           {
296             args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
297             args2[0] = Qcall_process;
298             for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
299             coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
300             if (CONSP (coding_systems))
301               val = XCDR (coding_systems);
302             else if (CONSP (Vdefault_process_coding_system))
303               val = XCDR (Vdefault_process_coding_system);
304             else
305               val = Qnil;
306           }
307         setup_coding_system (Fcheck_coding_system (val), &argument_coding);
308         if (argument_coding.common_flags & CODING_ASCII_INCOMPATIBLE_MASK)
309           setup_coding_system (Qraw_text, &argument_coding);
310         if (argument_coding.eol_type == CODING_EOL_UNDECIDED)
311           argument_coding.eol_type = system_eol_type;
312       }
313   }
314
315   if (nargs >= 2 && ! NILP (args[1]))
316     {
317       infile = Fexpand_file_name (args[1], current_buffer->directory);
318       CHECK_STRING (infile);
319     }
320   else
321     infile = build_string (NULL_DEVICE);
322
323   if (nargs >= 3)
324     {
325       buffer = args[2];
326
327       /* If BUFFER is a list, its meaning is
328          (BUFFER-FOR-STDOUT FILE-FOR-STDERR).  */
329       if (CONSP (buffer))
330         {
331           if (CONSP (XCDR (buffer)))
332             {
333               Lisp_Object stderr_file;
334               stderr_file = XCAR (XCDR (buffer));
335
336               if (NILP (stderr_file) || EQ (Qt, stderr_file))
337                 error_file = stderr_file;
338               else
339                 error_file = Fexpand_file_name (stderr_file, Qnil);
340             }
341
342           buffer = XCAR (buffer);
343         }
344
345       if (!(EQ (buffer, Qnil)
346             || EQ (buffer, Qt)
347             || INTEGERP (buffer)))
348         {
349           Lisp_Object spec_buffer;
350           spec_buffer = buffer;
351           buffer = Fget_buffer_create (buffer);
352           /* Mention the buffer name for a better error message.  */
353           if (NILP (buffer))
354             CHECK_BUFFER (spec_buffer);
355           CHECK_BUFFER (buffer);
356         }
357     }
358   else
359     buffer = Qnil;
360
361   /* Make sure that the child will be able to chdir to the current
362      buffer's current directory, or its unhandled equivalent.  We
363      can't just have the child check for an error when it does the
364      chdir, since it's in a vfork.
365
366      We have to GCPRO around this because Fexpand_file_name,
367      Funhandled_file_name_directory, and Ffile_accessible_directory_p
368      might call a file name handling function.  The argument list is
369      protected by the caller, so all we really have to worry about is
370      buffer.  */
371   {
372     struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
373
374     current_dir = current_buffer->directory;
375
376     GCPRO4 (infile, buffer, current_dir, error_file);
377
378     current_dir
379       = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
380                                 Qnil);
381     if (NILP (Ffile_accessible_directory_p (current_dir)))
382       report_file_error ("Setting current directory",
383                          Fcons (current_buffer->directory, Qnil));
384
385     if (STRING_MULTIBYTE (infile))
386       infile = ENCODE_FILE (infile);
387     if (STRING_MULTIBYTE (current_dir))
388       current_dir = ENCODE_FILE (current_dir);
389     if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
390       error_file = ENCODE_FILE (error_file);
391     UNGCPRO;
392   }
393
394   display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
395
396 #ifdef MEADOW
397   infile = ENCODE_FILE (infile);
398 #endif
399   filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
400   if (filefd < 0)
401     {
402       infile = DECODE_FILE (infile);
403       report_file_error ("Opening process input file", Fcons (infile, Qnil));
404     }
405   /* Search for program; barf if not found.  */
406   {
407     struct gcpro gcpro1;
408
409     GCPRO1 (current_dir);
410 #ifndef MEADOW
411     openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
412 #else
413     /* for script execution on MSWindows, not apply exec_only for openp */
414     /* openp without exec_only returns fd not used, free it immediately */
415     {
416       int fd = openp (Vexec_path, args[0], Vexec_suffixes, &path, Qnil);
417       if (fd > 0) close(fd);
418     }
419 #endif
420     UNGCPRO;
421   }
422   if (NILP (path))
423     {
424       emacs_close (filefd);
425       report_file_error ("Searching for program", Fcons (args[0], Qnil));
426     }
427
428   /* If program file name starts with /: for quoting a magic name,
429      discard that.  */
430   if (SBYTES (path) > 2 && SREF (path, 0) == '/'
431       && SREF (path, 1) == ':')
432     path = Fsubstring (path, make_number (2), Qnil);
433
434 #ifdef MEADOW
435   path = ENCODE_FILE (path);
436 #endif
437   new_argv[0] = SDATA (path);
438   if (nargs > 4)
439     {
440       register int i;
441       struct gcpro gcpro1, gcpro2, gcpro3;
442
443       GCPRO3 (infile, buffer, current_dir);
444       argument_coding.dst_multibyte = 0;
445       for (i = 4; i < nargs; i++)
446         {
447           argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
448           if (CODING_REQUIRE_ENCODING (&argument_coding))
449             {
450               /* We must encode this argument.  */
451               args[i] = encode_coding_string (args[i], &argument_coding, 1);
452               if (argument_coding.type == coding_type_ccl)
453                 setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
454             }
455           new_argv[i - 3] = SDATA (args[i]);
456         }
457       UNGCPRO;
458       new_argv[nargs - 3] = 0;
459     }
460   else
461     new_argv[1] = 0;
462
463 #ifdef MSDOS /* MW, July 1993 */
464   if ((outf = egetenv ("TMPDIR")))
465     strcpy (tempfile = alloca (strlen (outf) + 20), outf);
466   else
467     {
468       tempfile = alloca (20);
469       *tempfile = '\0';
470     }
471   dostounix_filename (tempfile);
472   if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
473     strcat (tempfile, "/");
474   strcat (tempfile, "detmp.XXX");
475   mktemp (tempfile);
476
477   outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
478   if (outfilefd < 0)
479     {
480       emacs_close (filefd);
481       report_file_error ("Opening process output file",
482                          Fcons (build_string (tempfile), Qnil));
483     }
484   fd[0] = filefd;
485   fd[1] = outfilefd;
486 #endif /* MSDOS */
487
488 #ifdef MAC_OS8
489   /* Since we don't have pipes on the Mac, create a temporary file to
490      hold the output of the subprocess.  */
491   tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
492   bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
493          SBYTES (Vtemp_file_name_pattern) + 1);
494
495   mktemp (tempfile);
496
497   outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
498   if (outfilefd < 0)
499     {
500       close (filefd);
501       report_file_error ("Opening process output file",
502                          Fcons (build_string (tempfile), Qnil));
503     }
504   fd[0] = filefd;
505   fd[1] = outfilefd;
506 #endif /* MAC_OS8 */
507
508   if (INTEGERP (buffer))
509     fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
510   else
511     {
512 #ifndef MSDOS
513 #ifndef MAC_OS8
514       errno = 0;
515       if (pipe (fd) == -1)
516         {
517           emacs_close (filefd);
518           report_file_error ("Creating process pipe", Qnil);
519         }
520 #endif
521 #endif
522 #if 0
523       /* Replaced by close_process_descs */
524       set_exclusive_use (fd[0]);
525 #endif
526     }
527
528   {
529     /* child_setup must clobber environ in systems with true vfork.
530        Protect it from permanent change.  */
531     register char **save_environ = environ;
532     register int fd1 = fd[1];
533     int fd_error = fd1;
534
535 #if 0  /* Some systems don't have sigblock.  */
536     mask = sigblock (sigmask (SIGCHLD));
537 #endif
538
539     /* Record that we're about to create a synchronous process.  */
540     synch_process_alive = 1;
541
542     /* These vars record information from process termination.
543        Clear them now before process can possibly terminate,
544        to avoid timing error if process terminates soon.  */
545     synch_process_death = 0;
546     synch_process_retcode = 0;
547     synch_process_termsig = 0;
548
549     if (NILP (error_file))
550       fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
551     else if (STRINGP (error_file))
552       {
553 #ifdef DOS_NT
554 #ifdef MEADOW
555         error_file = ENCODE_FILE (error_file);
556 #endif
557         fd_error = emacs_open (SDATA (error_file),
558                                O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
559                                S_IREAD | S_IWRITE);
560 #else  /* not DOS_NT */
561         fd_error = creat (SDATA (error_file), 0666);
562 #endif /* not DOS_NT */
563       }
564
565     if (fd_error < 0)
566       {
567         emacs_close (filefd);
568         if (fd[0] != filefd)
569           emacs_close (fd[0]);
570         if (fd1 >= 0)
571           emacs_close (fd1);
572 #ifdef MSDOS
573         unlink (tempfile);
574 #endif
575         if (NILP (error_file))
576           error_file = build_string (NULL_DEVICE);
577         else if (STRINGP (error_file))
578           error_file = DECODE_FILE (error_file);
579         report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
580       }
581
582 #ifdef MAC_OS8
583     {
584       /* Call run_mac_command in sysdep.c here directly instead of doing
585          a child_setup as for MSDOS and other platforms.  Note that this
586          code does not handle passing the environment to the synchronous
587          Mac subprocess.  */
588       char *infn, *outfn, *errfn, *currdn;
589
590       /* close these files so subprocess can write to them */
591       close (outfilefd);
592       if (fd_error != outfilefd)
593         close (fd_error);
594       fd1 = -1; /* No harm in closing that one! */
595
596       infn = SDATA (infile);
597       outfn = tempfile;
598       if (NILP (error_file))
599         errfn = NULL_DEVICE;
600       else if (EQ (Qt, error_file))
601         errfn = outfn;
602       else
603         errfn =