home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / callproc.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  43KB  |  1,488 lines

  1. /* Synchronous subprocess invocation for GNU Emacs.
  2.    Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21. /* Modified for emx by Jeremy Bowen, May 1999 based on patches
  22.    to v19.33 by Eberhard Mattes */
  23.  
  24. #include <signal.h>
  25. #include <errno.h>
  26.  
  27. #include <config.h>
  28. #include <stdio.h>
  29.  
  30. extern int errno;
  31. extern char *strerror ();
  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. #include <sys/file.h>
  42. #ifdef USG5
  43. #define INCLUDED_FCNTL
  44. #include <fcntl.h>
  45. #endif
  46.  
  47. #ifdef WINDOWSNT
  48. #define NOMINMAX
  49. #include <windows.h>
  50. #include <stdlib.h>    /* for proper declaration of environ */
  51. #include <fcntl.h>
  52. #include "w32.h"
  53. #define _P_NOWAIT 1    /* from process.h */
  54. #endif
  55.  
  56. #ifdef MSDOS    /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
  57. #define INCLUDED_FCNTL
  58. #include <fcntl.h>
  59. #include <sys/stat.h>
  60. #include <sys/param.h>
  61. #include <errno.h>
  62. #endif /* MSDOS */
  63.  
  64. #ifndef O_RDONLY
  65. #define O_RDONLY 0
  66. #endif
  67.  
  68. #ifndef O_WRONLY
  69. #define O_WRONLY 1
  70. #endif
  71.  
  72. #include "lisp.h"
  73. #include "commands.h"
  74. #include "buffer.h"
  75. #include "charset.h"
  76. #include "coding.h"
  77. #include <paths.h>
  78. #include "process.h"
  79. #include "syssignal.h"
  80. #include "systty.h"
  81.  
  82. #ifdef MSDOS
  83. #include "msdos.h"
  84. #endif
  85.  
  86. #ifdef VMS
  87. extern noshare char **environ;
  88. #else
  89. extern char **environ;
  90. #endif
  91.  
  92. #define max(a, b) ((a) > (b) ? (a) : (b))
  93.  
  94. #ifdef EMX
  95. extern Lisp_Object find_program_name_handler ();
  96. extern Lisp_Object Qprogram_name_handler_alist;
  97. extern Lisp_Object Vprogram_name_handler_alist;
  98. static Lisp_Object Qcall_process;
  99. #endif /* EMX */
  100.  
  101. #ifdef DOS_NT
  102. /* When we are starting external processes we need to know whether they
  103.    take binary input (no conversion) or text input (\n is converted to
  104.    \r\n).  Similar for output: if newlines are written as \r\n then it's
  105.    text process output, otherwise it's binary.  */
  106. Lisp_Object Vbinary_process_input;
  107. Lisp_Object Vbinary_process_output;
  108. #endif /* DOS_NT */
  109.  
  110. Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
  111. Lisp_Object Vconfigure_info_directory;
  112. Lisp_Object Vtemp_file_name_pattern;
  113.  
  114. Lisp_Object Vshell_file_name;
  115.  
  116. Lisp_Object Vprocess_environment;
  117.  
  118. #ifdef DOS_NT
  119. Lisp_Object Qbuffer_file_type;
  120. #endif /* DOS_NT */
  121.  
  122. /* True iff we are about to fork off a synchronous process or if we
  123.    are waiting for it.  */
  124. int synch_process_alive;
  125.  
  126. /* Nonzero => this is a string explaining death of synchronous subprocess.  */
  127. char *synch_process_death;
  128.  
  129. /* If synch_process_death is zero,
  130.    this is exit code of synchronous subprocess.  */
  131. int synch_process_retcode;
  132.  
  133. extern Lisp_Object Vdoc_file_name;
  134.  
  135. extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
  136.  
  137. /* Clean up when exiting Fcall_process.
  138.    On MSDOS, delete the temporary file on any kind of termination.
  139.    On Unix, kill the process and any children on termination by signal.  */
  140.  
  141. /* Nonzero if this is termination due to exit.  */
  142. static int call_process_exited;
  143.  
  144. #ifndef VMS  /* VMS version is in vmsproc.c.  */
  145.  
  146. static Lisp_Object
  147. call_process_kill (fdpid)
  148.      Lisp_Object fdpid;
  149. {
  150.   close (XFASTINT (Fcar (fdpid)));
  151.   EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
  152.   synch_process_alive = 0;
  153.   return Qnil;
  154. }
  155.  
  156. Lisp_Object
  157. call_process_cleanup (fdpid)
  158.      Lisp_Object fdpid;
  159. {
  160. #ifdef MSDOS
  161.   /* for MSDOS fdpid is really (fd . tempfile)  */
  162.   register Lisp_Object file;
  163.   file = Fcdr (fdpid);
  164.   close (XFASTINT (Fcar (fdpid)));
  165.   if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0)
  166.     unlink (XSTRING (file)->data);
  167. #else /* not MSDOS */
  168.   register int pid = XFASTINT (Fcdr (fdpid));
  169.  
  170.  
  171.   if (call_process_exited)
  172.     {
  173.       close (XFASTINT (Fcar (fdpid)));
  174.       return Qnil;
  175.     }
  176.  
  177.   if (EMACS_KILLPG (pid, SIGINT) == 0)
  178.     {
  179.       int count = specpdl_ptr - specpdl;
  180.       record_unwind_protect (call_process_kill, fdpid);
  181.       message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
  182.       immediate_quit = 1;
  183.       QUIT;
  184.       wait_for_termination (pid);
  185.       immediate_quit = 0;
  186.       specpdl_ptr = specpdl + count; /* Discard the unwind protect.  */
  187.       message1 ("Waiting for process to die...done");
  188.     }
  189.   synch_process_alive = 0;
  190.   close (XFASTINT (Fcar (fdpid)));
  191. #endif /* not MSDOS */
  192.   return Qnil;
  193. }
  194.  
  195. DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
  196.   "Call PROGRAM synchronously in separate process.\n\
  197. The program's input comes from file INFILE (nil means `/dev/null').\n\
  198. Insert output in BUFFER before point; t means current buffer;\n\
  199.  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
  200. BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
  201. REAL-BUFFER says what to do with standard output, as above,\n\
  202. while STDERR-FILE says what to do with standard error in the child.\n\
  203. STDERR-FILE may be nil (discard standard error output),\n\
  204. t (mix it with ordinary output), or a file name string.\n\
  205. \n\
  206. Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  207. Remaining arguments are strings passed as command arguments to PROGRAM.\n\
  208. \n\
  209. If BUFFER is 0, `call-process' returns immediately with value nil.\n\
  210. Otherwise it waits for PROGRAM to terminate\n\
  211. and returns a numeric exit status or a signal description string.\n\
  212. If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
  213.   (nargs, args)
  214.      int nargs;
  215.      register Lisp_Object *args;
  216. {
  217.   Lisp_Object infile, buffer, current_dir, display, path;
  218.   int fd[2];
  219.   int filefd;
  220.   register int pid;
  221.   char buf[16384];
  222.   char *bufptr = buf;
  223.   int bufsize = 16384;
  224.   int count = specpdl_ptr - specpdl;
  225.  
  226.   register unsigned char **new_argv
  227.     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
  228.   struct buffer *old = current_buffer;
  229.   /* File to use for stderr in the child.
  230.      t means use same as standard output.  */
  231.   Lisp_Object error_file;
  232. #ifdef MSDOS    /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
  233.   char *outf, *tempfile;
  234.   int outfilefd;
  235. #endif
  236. #if 0
  237.   int mask;
  238. #endif
  239.   struct coding_system process_coding; /* coding-system of process output */
  240.   struct coding_system argument_coding;    /* coding-system of arguments */
  241.  
  242.   CHECK_STRING (args[0], 0);
  243.  
  244. #ifdef EMX
  245.   {
  246.     Lisp_Object handler;
  247.  
  248.     handler = find_program_name_handler (args[0]);
  249.     if (!NILP (handler))
  250.       return call3 (handler,
  251.                     Fcons (Qcall_process, Flist (3, args + 1)),
  252.                     args[0],
  253.                     nargs >= 5 ? Flist (nargs - 4, args + 4) : Qnil);
  254.   }
  255.   check_process_priority ();
  256. #endif /* EMX */
  257.  
  258.   error_file = Qt;
  259.  
  260. #ifndef subprocesses
  261.   /* Without asynchronous processes we cannot have BUFFER == 0.  */
  262.   if (nargs >= 3 
  263.       && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
  264.     error ("Operating system cannot handle asynchronous subprocesses");
  265. #endif /* subprocesses */
  266.  
  267.   /* Decide the coding-system for giving arguments and reading process
  268.      output.  */
  269.   {
  270.     Lisp_Object val, *args2;
  271.     /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
  272.     Lisp_Object coding_systems = Qt;
  273.     int i;
  274.  
  275.     /* If arguments are supplied, we may have to encode them.  */
  276.     if (nargs >= 5)
  277.       {
  278.     int must_encode = 0;
  279.  
  280.     for (i = 4; i < nargs; i++)
  281.       CHECK_STRING (args[i], i);
  282.  
  283.     for (i = 4; i < nargs; i++)
  284.       if (STRING_MULTIBYTE (args[i]))
  285.         must_encode = 1;
  286.  
  287.     if (!NILP (Vcoding_system_for_write))
  288.       val = Vcoding_system_for_write;
  289.     else if (! must_encode)
  290.       val = Qnil;
  291.     else
  292.       {
  293.         args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
  294.         args2[0] = Qcall_process;
  295.         for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
  296.         coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
  297.         if (CONSP (coding_systems))
  298.           val = XCONS (coding_systems)->cdr;
  299.         else if (CONSP (Vdefault_process_coding_system))
  300.           val = XCONS (Vdefault_process_coding_system)->cdr;
  301.         else
  302.           val = Qnil;
  303.       }
  304.     setup_coding_system (Fcheck_coding_system (val), &argument_coding);
  305.       }
  306.  
  307.     /* If BUFFER is nil, we must read process output once and then
  308.        discard it, so setup coding system but with nil.  If BUFFER is
  309.        an integer, we can discard it without reading.  */
  310.     if (nargs < 3 || NILP (args[2])
  311.     || (CONSP (args[2]) && NILP (XCAR (args[2]))))
  312.       setup_coding_system (Qnil, &process_coding);
  313.     else if (!INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2]))
  314.       {
  315.     val = Qnil;
  316.     if (!NILP (Vcoding_system_for_read))
  317.       val = Vcoding_system_for_read;
  318.     else if (NILP (current_buffer->enable_multibyte_characters))
  319.       val = Qraw_text;
  320.     else
  321.       {
  322.         if (EQ (coding_systems, Qt))
  323.           {
  324.         args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
  325.         args2[0] = Qcall_process;
  326.         for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
  327.         coding_systems
  328.           = Ffind_operation_coding_system (nargs + 1, args2);
  329.           }
  330.         if (CONSP (coding_systems))
  331.           val = XCONS (coding_systems)->car;
  332.         else if (CONSP (Vdefault_process_coding_system))
  333.           val = XCONS (Vdefault_process_coding_system)->car;
  334.         else
  335.           val = Qnil;
  336.       }
  337.     setup_coding_system (Fcheck_coding_system (val), &process_coding);
  338.       }
  339.   }
  340.  
  341.   if (nargs >= 2 && ! NILP (args[1]))
  342.     {
  343.       infile = Fexpand_file_name (args[1], current_buffer->directory);
  344.       CHECK_STRING (infile, 1);
  345.     }
  346.   else
  347.     infile = build_string (NULL_DEVICE);
  348.  
  349.   if (nargs >= 3)
  350.     {
  351.       buffer = args[2];
  352.  
  353.       /* If BUFFER is a list, its meaning is
  354.      (BUFFER-FOR-STDOUT FILE-FOR-STDERR).  */
  355.       if (CONSP (buffer))
  356.     {
  357.       if (CONSP (XCONS (buffer)->cdr))
  358.         {
  359.           Lisp_Object stderr_file;
  360.           stderr_file = XCONS (XCONS (buffer)->cdr)->car;
  361.  
  362.           if (NILP (stderr_file) || EQ (Qt, stderr_file))
  363.         error_file = stderr_file;
  364.           else
  365.         error_file = Fexpand_file_name (stderr_file, Qnil);
  366.         }
  367.  
  368.       buffer = XCONS (buffer)->car;
  369.     }
  370.  
  371.       if (!(EQ (buffer, Qnil)
  372.         || EQ (buffer, Qt)
  373.         || INTEGERP (buffer)))
  374.     {
  375.       Lisp_Object spec_buffer;
  376.       spec_buffer = buffer;
  377.       buffer = Fget_buffer_create (buffer);
  378.       /* Mention the buffer name for a better error message.  */
  379.       if (NILP (buffer))
  380.         CHECK_BUFFER (spec_buffer, 2);
  381.       CHECK_BUFFER (buffer, 2);
  382.     }
  383.     }
  384.   else 
  385.     buffer = Qnil;
  386.  
  387.   /* Make sure that the child will be able to chdir to the current
  388.      buffer's current directory, or its unhandled equivalent.  We
  389.      can't just have the child check for an error when it does the
  390.      chdir, since it's in a vfork.
  391.  
  392.      We have to GCPRO around this because Fexpand_file_name,
  393.      Funhandled_file_name_directory, and Ffile_accessible_directory_p
  394.      might call a file name handling function.  The argument list is
  395.      protected by the caller, so all we really have to worry about is
  396.      buffer.  */
  397.   {
  398.     struct gcpro gcpro1, gcpro2, gcpro3;
  399.  
  400.     current_dir = current_buffer->directory;
  401.  
  402.     GCPRO3 (infile, buffer, current_dir);
  403.  
  404.     current_dir
  405.       = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
  406.                 Qnil);
  407.     if (NILP (Ffile_accessible_directory_p (current_dir)))
  408.       report_file_error ("Setting current directory",
  409.              Fcons (current_buffer->directory, Qnil));
  410.  
  411.     UNGCPRO;
  412.   }
  413.  
  414.   display = nargs >= 4 ? args[3] : Qnil;
  415.  
  416.   filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
  417.   if (filefd < 0)
  418.     {
  419.       report_file_error ("Opening process input file", Fcons (infile, Qnil));
  420.     }
  421.   /* Search for program; barf if not found.  */
  422.   {
  423.     struct gcpro gcpro1;
  424.  
  425.     GCPRO1 (current_dir);
  426.     openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
  427.     UNGCPRO;
  428.   }
  429.   if (NILP (path))
  430.     {
  431.       close (filefd);
  432.       report_file_error ("Searching for program", Fcons (args[0], Qnil));
  433.     }
  434.   new_argv[0] = XSTRING (path)->data;
  435.   if (nargs > 4)
  436.     {
  437.       register int i;
  438.  
  439.       if (! CODING_REQUIRE_ENCODING (&argument_coding))
  440.     {
  441.       for (i = 4; i < nargs; i++)
  442.         new_argv[i - 3] = XSTRING (args[i])->data;
  443.     }
  444.       else
  445.     {
  446.       /* We must encode the arguments.  */
  447.       struct gcpro gcpro1, gcpro2, gcpro3;
  448.  
  449.       GCPRO3 (infile, buffer, current_dir);
  450.       for (i = 4; i < nargs; i++)
  451.         {
  452.           int size = encoding_buffer_size (&argument_coding,
  453.                            STRING_BYTES (XSTRING (args[i])));
  454.           unsigned char *dummy1 = (unsigned char *) alloca (size);
  455.           int dummy;
  456.  
  457.           /* The Irix 4.0 compiler barfs if we eliminate dummy.  */
  458.           new_argv[i - 3] = dummy1;
  459.           encode_coding (&argument_coding,
  460.                  XSTRING (args[i])->data,
  461.                  new_argv[i - 3],
  462.                  STRING_BYTES (XSTRING (args[i])),
  463.                  size);
  464.           new_argv[i - 3][argument_coding.produced] = 0;
  465.         }
  466.       UNGCPRO;
  467.     }
  468.       new_argv[nargs - 3] = 0;
  469.     }
  470.   else
  471.     new_argv[1] = 0;
  472.  
  473. #ifdef MSDOS /* MW, July 1993 */
  474.   if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
  475.     strcpy (tempfile = alloca (strlen (outf) + 20), outf);
  476.   else
  477.     {
  478.       tempfile = alloca (20);
  479.       *tempfile = '\0';
  480.     }
  481.   dostounix_filename (tempfile);
  482.   if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/') 
  483.     strcat (tempfile, "/");
  484.   strcat (tempfile, "detmp.XXX");
  485.   mktemp (tempfile);
  486.  
  487.   outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
  488.   if (outfilefd < 0)
  489.     {
  490.       close (filefd);
  491.       report_file_error ("Opening process output file",
  492.              Fcons (build_string (tempfile), Qnil));
  493.     }
  494.   fd[0] = filefd;
  495.   fd[1] = outfilefd;
  496. #endif /* MSDOS */
  497.  
  498.   if (INTEGERP (buffer))
  499.     fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
  500.   else
  501.     {
  502. #ifndef MSDOS
  503.       pipe (fd);
  504. #ifdef EMX
  505.       emx_proc_output_pipe (fd[0]);
  506. #endif /* EMX */
  507. #endif
  508. #if 0
  509.       /* Replaced by close_process_descs */
  510.       set_exclusive_use (fd[0]);
  511. #endif
  512.     }
  513.  
  514.   {
  515.     /* child_setup must clobber environ in systems with true vfork.
  516.        Protect it from permanent change.  */
  517.     register char **save_environ = environ;
  518.     register int fd1 = fd[1];
  519.     int fd_error = fd1;
  520.  
  521. #if 0  /* Some systems don't have sigblock.  */
  522.     mask = sigblock (sigmask (SIGCHLD));
  523. #endif
  524.  
  525.     /* Record that we're about to create a synchronous process.  */
  526.     synch_process_alive = 1;
  527.  
  528.     /* These vars record information from process termination.
  529.        Clear them now before process can possibly terminate,
  530.        to avoid timing error if process terminates soon.  */
  531.     synch_process_death = 0;
  532.     synch_process_retcode = 0;
  533.  
  534.     if (NILP (error_file))
  535.       fd_error = open (NULL_DEVICE, O_WRONLY);
  536.     else if (STRINGP (error_file))
  537.       {
  538. #ifdef DOS_NT
  539.     fd_error = open (XSTRING (error_file)->data,
  540.              O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
  541.              S_IREAD | S_IWRITE);
  542. #else  /* not DOS_NT */
  543.     fd_error = creat (XSTRING (error_file)->data, 0666);
  544. #endif /* not DOS_NT */
  545.       }
  546.  
  547.     if (fd_error < 0)
  548.       {
  549.     close (filefd);
  550.     if (fd[0] != filefd)
  551.       close (fd[0]);
  552.     if (fd1 >= 0)
  553.       close (fd1);
  554. #ifdef MSDOS
  555.     unlink (tempfile);
  556. #endif
  557.     report_file_error ("Cannot redirect stderr",
  558.                Fcons ((NILP (error_file)
  559.                    ? build_string (NULL_DEVICE) : error_file),
  560.                   Qnil));
  561.       }
  562.  
  563.     current_dir = ENCODE_FILE (current_dir);
  564.  
  565. #ifdef MSDOS /* MW, July 1993 */
  566.     /* Note that on MSDOS `child_setup' actually returns the child process
  567.        exit status, not its PID, so we assign it to `synch_process_retcode'
  568.        below.  */
  569.     pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
  570.                0, current_dir);
  571.  
  572.     /* Record that the synchronous process exited and note its
  573.        termination status.  */
  574.     synch_process_alive = 0;
  575.     synch_process_retcode = pid;
  576.     if (synch_process_retcode < 0)  /* means it couldn't be exec'ed */
  577.       synch_process_death = strerror (errno);
  578.  
  579.     close (outfilefd);
  580.     if (fd_error != outfilefd)
  581.       close (fd_error);
  582.     fd1 = -1; /* No harm in closing that one!  */
  583.     /* Since CRLF is converted to LF within `decode_coding', we can
  584.        always open a file with binary mode.  */
  585.     fd[0] = open (tempfile, O_BINARY);
  586.     if (fd[0] < 0)
  587.       {
  588.     unlink (tempfile);
  589.     close (filefd);
  590.     report_file_error ("Cannot re-open temporary file", Qnil);
  591.       }
  592. #else /* not MSDOS */
  593. #ifdef WINDOWSNT
  594.     pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
  595.                0, current_dir);
  596. #else  /* not WINDOWSNT */
  597. #ifdef EMX
  598.     if (fd[0] >= 0)
  599.       fcntl (fd[0], F_SETFD, 1);
  600.     pid = emx_child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
  601. #else /* not EMX */
  602.     pid = vfork ();
  603.  
  604.     if (pid == 0)
  605.       {
  606.     if (fd[0] >= 0)
  607.       close (fd[0]);
  608. #ifdef HAVE_SETSID
  609.         setsid ();
  610. #endif
  611. #if defined (USG) && !defined (BSD_PGRPS)
  612.         setpgrp ();
  613. #else
  614.         setpgrp (pid, pid);
  615. #endif /* USG */
  616.     child_setup (filefd, fd1, fd_error, (char **) new_argv,
  617.              0, current_dir);
  618.       }
  619. #endif /* not EMX */
  620. #endif /* not WINDOWSNT */
  621.  
  622.     /* The MSDOS case did this already.  */
  623.     if (fd_error >= 0)
  624.       close (fd_error);
  625. #endif /* not MSDOS */
  626.  
  627.     environ = save_environ;
  628.  
  629.     /* Close most of our fd's, but not fd[0]
  630.        since we will use that to read input from.  */
  631.     close (filefd);
  632.     if (fd1 >= 0 && fd1 != fd_error)
  633.       close (fd1);
  634.   }
  635.  
  636.   if (pid < 0)
  637.     {
  638.       if (fd[0] >= 0)
  639.     close (fd[0]);
  640. #ifndef EMX
  641.       report_file_error ("Doing vfork", Qnil);
  642. #else /* EMX */
  643.       report_file_error ("Running process", Qnil);
  644. #endif
  645.     }
  646.  
  647.   if (INTEGERP (buffer))
  648.     {
  649.       if (fd[0] >= 0)
  650.     close (fd[0]);
  651. #ifndef subprocesses
  652.       /* If Emacs has been built with asynchronous subprocess support,
  653.      we don't need to do this, I think because it will then have
  654.      the facilities for handling SIGCHLD.  */
  655.       wait_without_blocking ();
  656. #endif /* subprocesses */
  657.       return Qnil;
  658.     }
  659.  
  660.   /* Enable sending signal if user quits below.  */
  661.   call_process_exited = 0;
  662.  
  663. #ifdef MSDOS
  664.   /* MSDOS needs different cleanup information.  */
  665.   record_unwind_protect (call_process_cleanup,
  666.              Fcons (make_number (fd[0]), build_string (tempfile)));
  667. #else
  668.   record_unwind_protect (call_process_cleanup,
  669.              Fcons (make_number (fd[0]), make_number (pid)));
  670. #endif /* not MSDOS */
  671.  
  672.  
  673.   if (BUFFERP (buffer))
  674.     Fset_buffer (buffer);
  675.  
  676.   immediate_quit = 1;
  677.   QUIT;
  678.  
  679.   {
  680.     register int nread;
  681.     int first = 1;
  682.     int total_read = 0;
  683.     int carryover = 0;
  684.     int display_on_the_fly = !NILP (display) && INTERACTIVE;
  685.     struct coding_system saved_coding;
  686.  
  687.     saved_coding = process_coding;
  688.  
  689.     while (1)
  690.       {
  691.     /* Repeatedly read until we've filled as much as possible
  692.        of the buffer size we have.  But don't read
  693.        less than 1024--save that for the next bufferful.  */
  694.     nread = carryover;
  695.     while (nread < bufsize - 1024)
  696.       {
  697.         int this_read = read (fd[0], bufptr + nread, bufsize - nread);
  698.  
  699.         if (this_read < 0)
  700.           goto give_up;
  701.  
  702.         if (this_read == 0)
  703.           {
  704.         process_coding.mode |= CODING_MODE_LAST_BLOCK;
  705.         break;
  706.           }
  707.  
  708.         nread += this_read;
  709.         total_read += this_read;
  710.  
  711.         if (display_on_the_fly)
  712.           break;
  713.       }
  714.  
  715.     /* Now NREAD is the total amount of data in the buffer.  */
  716.     immediate_quit = 0;
  717.     
  718.     if (!NILP (buffer))
  719.       {
  720.         if (process_coding.type == coding_type_no_conversion)
  721.           insert (bufptr, nread);
  722.         else
  723.           {            /* We have to decode the input.  */
  724.         int size = decoding_buffer_size (&process_coding, nread);
  725.         char *decoding_buf = (char *) malloc (size);
  726.  
  727.         decode_coding (&process_coding, bufptr, decoding_buf,
  728.                    nread, size);
  729.         if (display_on_the_fly
  730.             && saved_coding.type == coding_type_undecided
  731.             && process_coding.type != coding_type_undecided)
  732.           {
  733.             /* We have detected some coding system.  But,
  734.                there's a possibility that the detection was
  735.                done by insufficient data.  So, we give up
  736.                displaying on the fly.  */
  737.             free (decoding_buf);
  738.             display_on_the_fly = 0;
  739.             process_coding = saved_coding;
  740.             carryover = nread;
  741.             continue;
  742.           }
  743.         if (process_coding.produced > 0)
  744.           insert (decoding_buf, process_coding.produced);
  745.         free (decoding_buf);
  746.         carryover = nread - process_coding.consumed;
  747.         if (carryover > 0)
  748.           {
  749.             /* As CARRYOVER should not be that large, we had
  750.                better avoid overhead of bcopy.  */
  751.             char *p = bufptr + process_coding.consumed;
  752.             char *pend = p + carryover;
  753.             char *dst = bufptr;
  754.  
  755.             while (p < pend) *dst++ = *p++;
  756.           }
  757.           }
  758.       }
  759.     if (process_coding.mode & CODING_MODE_LAST_BLOCK)
  760.       {
  761.         if (carryover > 0)
  762.           insert (bufptr, carryover);
  763.         break;
  764.       }
  765.  
  766.     /* Make the buffer bigger as we continue to read more data,
  767.        but not past 64k.  */
  768.     if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
  769.       {
  770.         bufsize *= 2;
  771.         bufptr = (char *) alloca (bufsize);
  772.       }
  773.  
  774.     if (!NILP (display) && INTERACTIVE)
  775.       {
  776.         if (first)
  777.           prepare_menu_bars ();
  778.         first = 0;
  779.         redisplay_preserve_echo_area ();
  780.       }
  781.     immediate_quit = 1;
  782.     QUIT;
  783.       }
  784.   give_up: ;
  785.  
  786.   Vlast_coding_system_used = process_coding.symbol;
  787.  
  788.   /* If the caller required, let the buffer inherit the
  789.      coding-system used to decode the process output.  */
  790.   if (inherit_process_coding_system)
  791.     call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
  792.        make_number (total_read));
  793.   }
  794.  
  795.   /* Wait for it to terminate, unless it already has.  */
  796.   wait_for_termination (pid);
  797.  
  798.   immediate_quit = 0;
  799.  
  800.   set_buffer_internal (old);
  801.  
  802.   /* Don't kill any children that the subprocess may have left behind
  803.      when exiting.  */
  804.   call_process_exited = 1;
  805.  
  806.   unbind_to (count, Qnil);
  807.  
  808.   if (synch_process_death)
  809.     return build_string (synch_process_death);
  810.   return make_number (synch_process_retcode);
  811. }
  812. #endif
  813.  
  814. static Lisp_Object
  815. delete_temp_file (name)
  816.      Lisp_Object name;
  817. {
  818. #ifndef EMX
  819.   /* Use Fdelete_file (indirectly) because that runs a file name handler.
  820.      We did that when writing the file, so we should do so when deleting.  */
  821.   internal_delete_file (name);
  822. #else /* EMX */
  823.   /* TODO: call file name handler, see above. */
  824.   if (unlink (XSTRING (name)->data) != 0 && errno == EACCES)
  825.     emx_async_delete_add (XSTRING (name)->data);
  826. #endif /* EMX */
  827. }
  828.  
  829. DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
  830.   3, MANY, 0,
  831.   "Send text from START to END to a synchronous process running PROGRAM.\n\
  832. Delete the text if fourth arg DELETE is non-nil.\n\
  833. \n\
  834. Insert output in BUFFER before point; t means current buffer;\n\
  835.  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
  836. BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
  837. REAL-BUFFER says what to do with standard output, as above,\n\
  838. while STDERR-FILE says what to do with standard error in the child.\n\
  839. STDERR-FILE may be nil (discard standard error output),\n\
  840. t (mix it with ordinary output), or a file name string.\n\
  841. \n\
  842. Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  843. Remaining args are passed to PROGRAM at startup as command args.\n\
  844. \n\
  845. If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
  846. Otherwise it waits for PROGRAM to terminate\n\
  847. and returns a numeric exit status or a signal description string.\n\
  848. If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
  849.   (nargs, args)
  850.      int nargs;
  851.      register Lisp_Object *args;
  852. {
  853.   struct gcpro gcpro1;
  854.   Lisp_Object filename_string;
  855.   register Lisp_Object start, end;
  856. #ifdef EMX
  857.   char tempfile[260];
  858. #endif /* EMX */
  859.   int count = specpdl_ptr - specpdl;
  860.   /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
  861.   Lisp_Object coding_systems = Qt;
  862.   Lisp_Object val, *args2;
  863.   int i;
  864. #ifdef DOS_NT
  865.   char *tempfile;
  866.   char *outf = '\0';
  867.  
  868.   if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
  869.     strcpy (tempfile = alloca (strlen (outf) + 20), outf);
  870.   else
  871.     {
  872.       tempfile = alloca (20);
  873.       *tempfile = '\0';
  874.     }
  875.   if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
  876.     strcat (tempfile, "/");
  877.   if ('/' == DIRECTORY_SEP)
  878.     dostounix_filename (tempfile);
  879.   else
  880.     unixtodos_filename (tempfile);
  881. #ifdef WINDOWSNT
  882.   strcat (tempfile, "emXXXXXX");
  883. #else
  884.   strcat (tempfile, "detmp.XXX");
  885. #endif
  886. #else /* not DOS_NT */
  887. #ifdef EMX
  888.   extern char *tempnam ();
  889.   char *t = tempnam ("/tmp", "em");
  890.   if (t)
  891.     {
  892.       strcpy (tempfile, t);
  893.       free (t);
  894.     }
  895.   else
  896.     strcpy (tempfile, "/tmp/emXXXXXX");
  897. #else /* not EMX */
  898.   char *tempfile = (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
  899.   bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile,
  900.      STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
  901. #endif /* not EMX */
  902. #endif /* not DOS_NT */
  903.  
  904.   mktemp (tempfile);
  905.  
  906.   filename_string = build_string (tempfile);
  907.   GCPRO1 (filename_string);
  908.   start = args[0];
  909.   end = args[1];
  910.   /* Decide coding-system of the contents of the temporary file.  */
  911.   if (!NILP (Vcoding_system_for_write))
  912.     val = Vcoding_system_for_write;
  913.   else if (NILP (current_buffer->enable_multibyte_characters))
  914.     val = Qnil;
  915.   else
  916.     {
  917.       args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
  918.       args2[0] = Qcall_process_region;
  919.       for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
  920.       coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
  921.       if (CONSP (coding_systems))
  922.     val = XCONS (coding_systems)->cdr;
  923.       else if (CONSP (Vdefault_process_coding_system))
  924.     val = XCONS (Vdefault_process_coding_system)->cdr;
  925.       else
  926.     val = Qnil;
  927.     }
  928.  
  929.   {
  930.     int count1 = specpdl_ptr - specpdl;
  931.  
  932.     specbind (intern ("coding-system-for-write"), val);
  933.     Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
  934.  
  935.     unbind_to (count1, Qnil);
  936.   }
  937.  
  938.   /* Note that Fcall_process takes care of binding 
  939.      coding-system-for-read.  */
  940.  
  941.   record_unwind_protect (delete_temp_file, filename_string);
  942.  
  943.   if (!NILP (args[3]))
  944.     Fdelete_region (start, end);
  945.  
  946.   args[3] = filename_string;
  947.  
  948.   RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs - 2, args + 2)));
  949. }
  950.  
  951. #ifndef VMS /* VMS version is in vmsproc.c.  */
  952. #ifndef EMX /* emx version is in emxdep.c */
  953.  
  954. static int relocate_fd ();
  955.  
  956. /* This is the last thing run in a newly forked inferior
  957.    either synchronous or asynchronous.
  958.    Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
  959.    Initialize inferior's priority, pgrp, connected dir and environment.
  960.    then exec another program based on new_argv.
  961.  
  962.    This function may change environ for the superior process.
  963.    Therefore, the superior process must save and restore the value
  964.    of environ around the vfork and the call to this function.
  965.  
  966.    SET_PGRP is nonzero if we should put the subprocess into a separate
  967.    process group.  
  968.  
  969.    CURRENT_DIR is an elisp string giving the path of the current
  970.    directory the subprocess should have.  Since we can't really signal
  971.    a decent error from within the child, this should be verified as an
  972.    executable directory by the parent.  */
  973.  
  974. int
  975. child_setup (in, out, err, new_argv, set_pgrp, current_dir)
  976.      int in, out, err;
  977.      register char **new_argv;
  978.      int set_pgrp;
  979.      Lisp_Object current_dir;
  980. {
  981.   char **env;
  982.   char *pwd_var;
  983. #ifdef WINDOWSNT
  984.   int cpid;
  985.   HANDLE handles[3];
  986. #endif /* WINDOWSNT */
  987.  
  988.   int pid = getpid ();
  989.  
  990. #ifdef SET_EMACS_PRIORITY
  991.   {
  992.     extern int emacs_priority;
  993.  
  994.     if (emacs_priority < 0)
  995.       nice (- emacs_priority);
  996.   }
  997. #endif
  998.  
  999. #ifdef subprocesses
  1000.   /* Close Emacs's descriptors that this process should not have.  */
  1001.   close_process_descs ();
  1002. #endif
  1003.   /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
  1004.      we will lose if we call close_load_descs here.  */
  1005. #ifndef DOS_NT
  1006.   close_load_descs ();
  1007. #endif
  1008.  
  1009.   /* Note that use of alloca is always safe here.  It's obvious for systems
  1010.      that do not have true vfork or that have true (stack) alloca.
  1011.      If using vfork and C_ALLOCA it is safe because that changes
  1012.      the superior's static variables as if the superior had done alloca
  1013.      and will be cleaned up in the usual way.  */
  1014.   {
  1015.     register char *temp;
  1016.     register int i;
  1017.  
  1018.     i = STRING_BYTES (XSTRING (current_dir));
  1019.     pwd_var = (char *) alloca (i + 6);
  1020.     temp = pwd_var + 4;
  1021.     bcopy ("PWD=", pwd_var, 4);
  1022.     bcopy (XSTRING (current_dir)->data, temp, i);
  1023.     if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
  1024.     temp[i] = 0;
  1025.  
  1026. #ifndef DOS_NT
  1027.     /* We can't signal an Elisp error here; we're in a vfork.  Since
  1028.        the callers check the current directory before forking, this
  1029.        should only return an error if the directory's permissions
  1030.        are changed between the check and this chdir, but we should
  1031.        at least check.  */
  1032.     if (chdir (temp) < 0)
  1033.       _exit (errno);
  1034. #endif
  1035.  
  1036. #ifdef DOS_NT
  1037.     /* Get past the drive letter, so that d:/ is left alone.  */
  1038.     if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
  1039.       {
  1040.     temp += 2;
  1041.     i -= 2;
  1042.       }
  1043. #endif
  1044.  
  1045.     /* Strip trailing slashes for PWD, but leave "/" and "//" alone.  */
  1046.     while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
  1047.       temp[--i] = 0;
  1048.   }
  1049.  
  1050.   /* Set `env' to a vector of the strings in Vprocess_environment.  */
  1051.   {
  1052.     register Lisp_Object tem;
  1053.     register char **new_env;
  1054.     register int new_length;
  1055.  
  1056.     new_length = 0;
  1057.     for (tem = Vprocess_environment;
  1058.      CONSP (tem) && STRINGP (XCONS (tem)->car);
  1059.      tem = XCONS (tem)->cdr)
  1060.       new_length++;
  1061.  
  1062.     /* new_length + 2 to include PWD and terminating 0.  */
  1063.     env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
  1064.  
  1065.     /* If we have a PWD envvar, pass one down,
  1066.        but with corrected value.  */
  1067.     if (getenv ("PWD"))
  1068.       *new_env++ = pwd_var;
  1069.  
  1070.     /* Copy the Vprocess_environment strings into new_env.  */
  1071.     for (tem = Vprocess_environment;
  1072.      CONSP (tem) && STRINGP (XCONS (tem)->car);
  1073.      tem = XCONS (tem)->cdr)
  1074.       {
  1075.     char **ep = env;
  1076.     char *string = (char *) XSTRING (XCONS (tem)->car)->data;
  1077.     /* See if this string duplicates any string already in the env.
  1078.        If so, don't put it in.
  1079.        When an env var has multiple definitions,
  1080.        we keep the definition that comes first in process-environment.  */
  1081.     for (; ep != new_env; ep++)
  1082.       {
  1083.         char *p = *ep, *q = string;
  1084.         while (1)
  1085.           {
  1086.         if (*q == 0)
  1087.           /* The string is malformed; might as well drop it.  */
  1088.           goto duplicate;
  1089.         if (*q != *p)
  1090.           break;
  1091.         if (*q == '=')
  1092.           goto duplicate;
  1093.         p++, q++;
  1094.           }
  1095.       }
  1096.     *new_env++ = string;
  1097.       duplicate: ;
  1098.       }
  1099.     *new_env = 0;
  1100.   }
  1101. #ifdef WINDOWSNT
  1102.   prepare_standard_handles (in, out, err, handles);
  1103.   set_process_dir (XSTRING (current_dir)->data);
  1104. #else  /* not WINDOWSNT */
  1105.   /* Make sure that in, out, and err are not actually already in
  1106.      descriptors zero, one, or two; this could happen if Emacs is
  1107.      started with its standard in, out, or error closed, as might
  1108.      happen under X.  */
  1109.   {
  1110.     int oin = in, oout = out;
  1111.  
  1112.     /* We have to avoid relocating the same descriptor twice!  */
  1113.  
  1114.     in = relocate_fd (in, 3);
  1115.  
  1116.     if (out == oin)
  1117.       out = in;
  1118.     else
  1119.       out = relocate_fd (out, 3);
  1120.  
  1121.     if (err == oin)
  1122.       err = in;
  1123.     else if (err == oout)
  1124.       err = out;
  1125.     else
  1126.       err = relocate_fd (err, 3);
  1127.   }
  1128.  
  1129. #ifndef MSDOS
  1130.   close (0);
  1131.   close (1);
  1132.   close (2);
  1133.  
  1134.   dup2 (in, 0);
  1135.   dup2 (out, 1);
  1136.   dup2 (err, 2);
  1137.   close (in);
  1138.   close (out);
  1139.   close (err);
  1140. #endif /* not MSDOS */
  1141. #endif /* not WINDOWSNT */
  1142.  
  1143. #if defined(USG) && !defined(BSD_PGRPS)
  1144. #ifndef SETPGRP_RELEASES_CTTY
  1145.   setpgrp ();            /* No arguments but equivalent in this case */
  1146. #endif
  1147. #else
  1148.   setpgrp (pid, pid);
  1149. #endif /* USG */
  1150.   /* setpgrp_of_tty is incorrect here; it uses input_fd.  */
  1151.   EMACS_SET_TTY_PGRP (0, &pid);
  1152.  
  1153. #ifdef vipc
  1154.   something missing here;
  1155. #endif /* vipc */
  1156.  
  1157. #ifdef MSDOS
  1158.   pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
  1159.   if (pid == -1)
  1160.     /* An error occurred while trying to run the subprocess.  */
  1161.     report_file_error ("Spawning child process", Qnil);
  1162.   return pid;
  1163. #else  /* not MSDOS */
  1164. #ifdef WINDOWSNT
  1165.   /* Spawn the child.  (See ntproc.c:Spawnve).  */
  1166.   cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
  1167.   reset_standard_handles (in, out, err, handles);
  1168.   if (cpid == -1)
  1169.     /* An error occurred while trying to spawn the process.  */
  1170.     report_file_error ("Spawning child process", Qnil);
  1171.   return cpid;
  1172. #else /* not WINDOWSNT */
  1173.   /* execvp does not accept an environment arg so the only way
  1174.      to pass this environment is to set environ.  Our caller
  1175.      is responsible for restoring the ambient value of environ.  */
  1176.   environ = env;
  1177.   execvp (new_argv[0], new_argv);
  1178.  
  1179.   write (1, "Can't exec program: ", 20);
  1180.   write (1, new_argv[0], strlen (new_argv[0]));
  1181.   write (1, "\n", 1);
  1182.   _exit (1);
  1183. #endif /* not WINDOWSNT */
  1184. #endif /* not MSDOS */
  1185. }
  1186.  
  1187. /* Move the file descriptor FD so that its number is not less than MINFD.
  1188.    If the file descriptor is moved at all, the original is freed.  */
  1189. static int
  1190. relocate_fd (fd, minfd)
  1191.      int fd, minfd;
  1192. {
  1193.   if (fd >= minfd)
  1194.     return fd;
  1195.   else
  1196.     {
  1197.       int new = dup (fd);
  1198.       if (new == -1)
  1199.     {
  1200.       char *message1 = "Error while setting up child: ";
  1201.       char *errmessage = strerror (errno);
  1202.       char *message2 = "\n";
  1203.       write (2, message1, strlen (message1));
  1204.       write (2, errmessage, strlen (errmessage));
  1205.       write (2, message2, strlen (message2));
  1206.       _exit (1);
  1207.     }
  1208.       /* Note that we hold the original FD open while we recurse,
  1209.      to guarantee we'll get a new FD if we need it.  */
  1210.       new = relocate_fd (new, minfd);
  1211.       close (fd);
  1212.       return new;
  1213.     }
  1214. }
  1215.  
  1216. #endif /* not EMX */
  1217.  
  1218. static int
  1219. getenv_internal (var, varlen, value, valuelen)
  1220.      char *var;
  1221.      int varlen;
  1222.      char **value;
  1223.      int *valuelen;
  1224. {
  1225.   Lisp_Object scan;
  1226.  
  1227.   for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
  1228.     {
  1229.       Lisp_Object entry;
  1230.  
  1231.       entry = XCONS (scan)->car;
  1232.       if (STRINGP (entry)
  1233.       && STRING_BYTES (XSTRING (entry)) > varlen
  1234.       && XSTRING (entry)->data[varlen] == '='
  1235. #ifdef WINDOWSNT
  1236.       /* NT environment variables are case insensitive.  */
  1237.       && ! strnicmp (XSTRING (entry)->data, var, varlen)
  1238. #else  /* not WINDOWSNT */
  1239.       && ! bcmp (XSTRING (entry)->data, var, varlen)
  1240. #endif /* not WINDOWSNT */
  1241.       )
  1242.     {
  1243.       *value    = (char *) XSTRING (entry)->data + (varlen + 1);
  1244.       *valuelen = STRING_BYTES (XSTRING (entry)) - (varlen + 1);
  1245.       return 1;
  1246.     }
  1247.     }
  1248.  
  1249.   return 0;
  1250. }
  1251.  
  1252. DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
  1253.   "Return the value of environment variable VAR, as a string.\n\
  1254. VAR should be a string.  Value is nil if VAR is undefined in the environment.\n\
  1255. This function consults the variable ``process-environment'' for its value.")
  1256.   (var)
  1257.      Lisp_Object var;
  1258. {
  1259.   char *value;
  1260.   int valuelen;
  1261.  
  1262.   CHECK_STRING (var, 0);
  1263.   if (getenv_internal (XSTRING (var)->data, STRING_BYTES (XSTRING (var)),
  1264.                &value, &valuelen))
  1265.     return make_string (value, valuelen);
  1266.   else
  1267.     return Qnil;
  1268. }
  1269.  
  1270. /* A version of getenv that consults process_environment, easily
  1271.    callable from C.  */
  1272. char *
  1273. egetenv (var)
  1274.      char *var;
  1275. {
  1276.   char *value;
  1277.   int valuelen;
  1278.  
  1279.   if (getenv_internal (var, strlen (var), &value, &valuelen))
  1280.     return value;
  1281.   else
  1282.     return 0;
  1283. }
  1284.  
  1285. #endif /* not VMS */
  1286.  
  1287. /* This is run before init_cmdargs.  */
  1288.   
  1289. void
  1290. init_callproc_1 ()
  1291. {
  1292.   char *data_dir = egetenv ("EMACSDATA");
  1293.   char *doc_dir = egetenv ("EMACSDOC");
  1294.  
  1295.   Vdata_directory
  1296.     = Ffile_name_as_directory (build_string (data_dir ? data_dir 
  1297.                          : PATH_DATA));
  1298.   Vdoc_directory
  1299.     = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
  1300.                          : PATH_DOC));
  1301.  
  1302.   /* Check the EMACSPATH environment variable, defaulting to the
  1303.      PATH_EXEC path from paths.h.  */
  1304.   Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
  1305.   Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
  1306.   Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
  1307. }
  1308.  
  1309. /* This is run after init_cmdargs, when Vinstallation_directory is valid.  */
  1310.  
  1311. void
  1312. init_callproc ()
  1313. {
  1314.   char *data_dir = egetenv ("EMACSDATA");
  1315.     
  1316.   register char * sh;
  1317.   Lisp_Object tempdir;
  1318.  
  1319.   if (!NILP (Vinstallation_directory))
  1320.     {
  1321.       /* Add to the path the lib-src subdir of the installation dir.  */
  1322.       Lisp_Object tem;
  1323. #ifdef EMX
  1324.       tem = Fexpand_file_name (build_string ("bin"),
  1325.                    Vinstallation_directory);
  1326. #else /* not EMX */
  1327.       tem = Fexpand_file_name (build_string ("lib-src"),
  1328.                    Vinstallation_directory);
  1329. #endif /* not EMX */
  1330.       if (NILP (Fmember (tem, Vexec_path)))
  1331.     {
  1332. #ifndef DOS_NT
  1333.       /* MSDOS uses wrapped binaries, so don't do this.  */
  1334.       Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
  1335.       Vexec_directory = Ffile_name_as_directory (tem);
  1336. #endif /* not DOS_NT */
  1337.     }
  1338.  
  1339.       /* Maybe use ../etc as well as ../lib-src.  */
  1340.       if (data_dir == 0)
  1341.     {
  1342. #ifdef EMX
  1343.       tem = Fexpand_file_name (build_string ("data"),
  1344.                    Vinstallation_directory);
  1345. #else /* not EMX */
  1346.       tem = Fexpand_file_name (build_string ("etc"),
  1347.                    Vinstallation_directory);
  1348. #endif /* not EMX */
  1349.       Vdoc_directory = Ffile_name_as_directory (tem);
  1350. #ifdef EMX
  1351.       Vdata_directory = Vdoc_directory;
  1352. #endif /* EMX */
  1353.     }
  1354.     }
  1355.  
  1356.   /* Look for the files that should be in etc.  We don't use
  1357.      Vinstallation_directory, because these files are never installed
  1358.      near the executable, and they are never in the build
  1359.      directory when that's different from the source directory.
  1360.  
  1361.      Instead, if these files are not in the nominal place, we try the
  1362.      source directory.  */
  1363.   if (data_dir == 0)
  1364.     {
  1365.       Lisp_Object tem, tem1, newdir;
  1366.  
  1367.       tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
  1368.       tem1 = Ffile_exists_p (tem);
  1369.       if (NILP (tem1))
  1370.     {
  1371.       newdir = Fexpand_file_name (build_string ("../etc/"),
  1372.                       build_string (PATH_DUMPLOADSEARCH));
  1373.       tem = Fexpand_file_name (build_string ("GNU"), newdir);
  1374.       tem1 = Ffile_exists_p (tem);
  1375.       if (!NILP (tem1))
  1376.         Vdata_directory = newdir;
  1377.     }
  1378.     }
  1379.  
  1380. #ifndef CANNOT_DUMP
  1381.   if (initialized)
  1382. #endif
  1383.     {
  1384.       tempdir = Fdirectory_file_name (Vexec_directory);
  1385.       if (access (XSTRING (tempdir)->data, 0) < 0)
  1386.     dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
  1387.              Vexec_directory);
  1388.     }
  1389.  
  1390.   tempdir = Fdirectory_file_name (Vdata_directory);
  1391.   if (access (XSTRING (tempdir)->data, 0) < 0)
  1392.     dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
  1393.          Vdata_directory);
  1394.  
  1395. #ifdef VMS
  1396.   Vshell_file_name = build_string ("*dcl*");
  1397. #else
  1398.   sh = (char *) getenv ("SHELL");
  1399.   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
  1400. #endif
  1401.  
  1402. #ifdef VMS
  1403.   Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
  1404. #else
  1405.   if (getenv ("TMPDIR"))
  1406.     {
  1407.       char *dir = getenv ("TMPDIR");
  1408.       Vtemp_file_name_pattern
  1409.     = Fexpand_file_name (build_string ("emacsXXXXXX"),
  1410.                  build_string (dir));
  1411.     }
  1412.   else
  1413.     Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
  1414. #endif
  1415. }
  1416.  
  1417. void
  1418. set_process_environment ()
  1419. {
  1420.   register char **envp;
  1421.  
  1422.   Vprocess_environment = Qnil;
  1423. #ifndef CANNOT_DUMP
  1424.   if (initialized)
  1425. #endif
  1426.     for (envp = environ; *envp; envp++)
  1427.       Vprocess_environment = Fcons (build_string (*envp),
  1428.                     Vprocess_environment);
  1429. }
  1430.  
  1431. void
  1432. syms_of_callproc ()
  1433. {
  1434. #ifdef EMX
  1435.   Qcall_process = intern ("call-process");
  1436.   staticpro (&Qcall_process);
  1437. #endif /* EMX */
  1438. #ifdef DOS_NT
  1439.   Qbuffer_file_type = intern ("buffer-file-type");
  1440.   staticpro (&Qbuffer_file_type);
  1441. #endif /* DOS_NT */
  1442.  
  1443.   DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
  1444.     "*File name to load inferior shells from.\n\
  1445. Initialized from the SHELL environment variable.");
  1446.  
  1447.   DEFVAR_LISP ("exec-path", &Vexec_path,
  1448.     "*List of directories to search programs to run in subprocesses.\n\
  1449. Each element is a string (directory name) or nil (try default directory).");
  1450.  
  1451.   DEFVAR_LISP ("exec-directory", &Vexec_directory,
  1452.     "Directory for executables for Emacs to invoke.\n\
  1453. More generally, this includes any architecture-dependent files\n\
  1454. that are built and installed from the Emacs distribution.");
  1455.  
  1456.   DEFVAR_LISP ("data-directory", &Vdata_directory,
  1457.     "Directory of machine-independent files that come with GNU Emacs.\n\
  1458. These are files intended for Emacs to use while it runs.");
  1459.  
  1460.   DEFVAR_LISP ("doc-directory", &Vdoc_directory,
  1461.     "Directory containing the DOC file that comes with GNU Emacs.\n\
  1462. This is usually the same as data-directory.");
  1463.  
  1464.   DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
  1465.     "For internal use by the build procedure only.\n\
  1466. This is the name of the directory in which the build procedure installed\n\
  1467. Emacs's info files; the default value for Info-default-directory-list\n\
  1468. includes this.");
  1469.   Vconfigure_info_directory = build_string (PATH_INFO);
  1470.  
  1471.   DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
  1472.     "Pattern for making names for temporary files.\n\
  1473. This is used by `call-process-region'.");
  1474.   /* This variable is initialized in init_callproc.  */
  1475.  
  1476.   DEFVAR_LISP ("process-environment", &Vprocess_environment,
  1477.     "List of environment variables for subprocesses to inherit.\n\
  1478. Each element should be a string of the form ENVVARNAME=VALUE.\n\
  1479. The environment which Emacs inherits is placed in this variable\n\
  1480. when Emacs starts.");
  1481.  
  1482. #ifndef VMS
  1483.   defsubr (&Scall_process);
  1484.   defsubr (&Sgetenv);
  1485. #endif
  1486.   defsubr (&Scall_process_region);
  1487. }
  1488.