]> code.delx.au - gnu-emacs/blobdiff - src/callproc.c
(child_setup): Call close_load_descs.
[gnu-emacs] / src / callproc.c
index 85e86c50da94239f40a9590ffdf590d325659f0f..9b61002a530928e4443938a049a2c2f5fa7950d9 100644 (file)
@@ -1,11 +1,11 @@
 /* Synchronous subprocess invocation for GNU Emacs.
-   Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -19,8 +19,12 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 
 #include <signal.h>
+#include <errno.h>
 
-#include "config.h"
+#include <config.h>
+
+extern int errno;
+extern char *strerror ();
 
 /* Define SIGCHLD as an alias for SIGCLD.  */
 
@@ -29,12 +33,19 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #endif /* SIGCLD */
 
 #include <sys/types.h>
-#define PRIO_PROCESS 0
+
 #include <sys/file.h>
 #ifdef USG5
 #include <fcntl.h>
 #endif
 
+#ifdef MSDOS   /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <errno.h>
+#endif /* MSDOS */
+
 #ifndef O_RDONLY
 #define O_RDONLY 0
 #endif
@@ -46,8 +57,10 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "lisp.h"
 #include "commands.h"
 #include "buffer.h"
-#include "paths.h"
+#include <paths.h>
 #include "process.h"
+#include "syssignal.h"
+#include "systty.h"
 
 #ifdef VMS
 extern noshare char **environ;
@@ -57,16 +70,16 @@ extern char **environ;
 
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
-Lisp_Object Vexec_path, Vexec_directory;
+#ifdef MSDOS
+Lisp_Object Vbinary_process;
+#endif
 
-Lisp_Object Vshell_file_name;
+Lisp_Object Vexec_path, Vexec_directory, Vdata_directory;
+Lisp_Object Vconfigure_info_directory;
 
-#ifndef MAINTAIN_ENVIRONMENT
-/* List of strings to append to front of environment of
-   all subprocesses when they are started.  */
+Lisp_Object Vshell_file_name;
 
 Lisp_Object Vprocess_environment;
-#endif
 
 /* True iff we are about to fork off a synchronous process or if we
    are waiting for it.  */
@@ -78,19 +91,49 @@ char *synch_process_death;
 /* If synch_process_death is zero,
    this is exit code of synchronous subprocess.  */
 int synch_process_retcode;
+
+extern Lisp_Object Vdoc_file_name;
 \f
 #ifndef VMS  /* VMS version is in vmsproc.c.  */
 
+static Lisp_Object
+call_process_kill (fdpid)
+     Lisp_Object fdpid;
+{
+  close (XFASTINT (Fcar (fdpid)));
+  EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
+  synch_process_alive = 0;
+  return Qnil;
+}
+
 Lisp_Object
 call_process_cleanup (fdpid)
      Lisp_Object fdpid;
 {
-  register Lisp_Object fd, pid;
-  fd = Fcar (fdpid);
-  pid = Fcdr (fdpid);
-  close (XFASTINT (fd));
-  kill (XFASTINT (pid), SIGKILL);
+#ifdef MSDOS
+  /* for MSDOS fdpid is really (fd . tempfile)  */
+  register Lisp_Object file = Fcdr (fdpid);
+  close (XFASTINT (Fcar (fdpid)));
+  if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0)
+    unlink (XSTRING (file)->data);
+#else /* not MSDOS */
+  register int pid = XFASTINT (Fcdr (fdpid));
+
+  if (EMACS_KILLPG (pid, SIGINT) == 0)
+    {
+      int count = specpdl_ptr - specpdl;
+      record_unwind_protect (call_process_kill, fdpid);
+      message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
+      immediate_quit = 1;
+      QUIT;
+      wait_for_termination (pid);
+      immediate_quit = 0;
+      specpdl_ptr = specpdl + count; /* Discard the unwind protect.  */
+      message1 ("Waiting for process to die...done");
+    }
   synch_process_alive = 0;
+  close (XFASTINT (Fcar (fdpid)));
+#endif /* not MSDOS */
   return Qnil;
 }
 
@@ -101,15 +144,15 @@ Insert output in BUFFER before point; t means current buffer;\n\
  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
-If BUFFER is nil or 0, returns immediately with value nil.\n\
+If BUFFER is 0, returns immediately with value nil.\n\
 Otherwise waits for PROGRAM to terminate\n\
-and returns a numeric exit status or a signal name as a string.\n\
-If you quit, the process is killed with SIGKILL.")
+and returns a numeric exit status or a signal description string.\n\
+If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   (nargs, args)
      int nargs;
      register Lisp_Object *args;
 {
-  Lisp_Object display, buffer, path;
+  Lisp_Object infile, buffer, current_dir, display, path;
   int fd[2];
   int filefd;
   register int pid;
@@ -118,37 +161,73 @@ If you quit, the process is killed with SIGKILL.")
   register unsigned char **new_argv
     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
   struct buffer *old = current_buffer;
+#ifdef MSDOS   /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
+  char *outf, *tempfile;
+  int outfilefd;
+#endif
 #if 0
   int mask;
 #endif
-  struct gcpro gcpro1;
-
-  GCPRO1 (*args);
-  gcpro1.nvars = nargs;
-
   CHECK_STRING (args[0], 0);
 
-  if (nargs <= 1 || NILP (args[1]))
-    args[1] = build_string ("/dev/null");
+#ifndef subprocesses
+  /* Without asynchronous processes we cannot have BUFFER == 0.  */
+  if (nargs >= 3 && XTYPE (args[2]) == Lisp_Int)
+    error ("Operating system cannot handle asynchronous subprocesses");
+#endif /* subprocesses */
+
+  if (nargs >= 2 && ! NILP (args[1]))
+    {
+      infile = Fexpand_file_name (args[1], current_buffer->directory);
+      CHECK_STRING (infile, 1);
+    }
   else
-    args[1] = Fexpand_file_name (args[1], current_buffer->directory);
+    infile = build_string (NULL_DEVICE);
 
-  CHECK_STRING (args[1], 1);
+  if (nargs >= 3)
+    {
+      register Lisp_Object tem;
 
+      buffer = tem = args[2];
+      if (!(EQ (tem, Qnil)
+           || EQ (tem, Qt)
+           || XFASTINT (tem) == 0))
+       {
+         buffer = Fget_buffer (tem);
+         CHECK_BUFFER (buffer, 2);
+       }
+    }
+  else 
+    buffer = Qnil;
+
+  /* Make sure that the child will be able to chdir to the current
+     buffer's current directory, or its unhandled equivalent.  We
+     can't just have the child check for an error when it does the
+     chdir, since it's in a vfork.
+
+     We have to GCPRO around this because Fexpand_file_name,
+     Funhandled_file_name_directory, and Ffile_accessible_directory_p
+     might call a file name handling function.  The argument list is
+     protected by the caller, so all we really have to worry about is
+     buffer.  */
   {
-    register Lisp_Object tem;
-    buffer = tem = args[2];
-    if (nargs <= 2)
-      buffer = Qnil;
-    else if (!(EQ (tem, Qnil) || EQ (tem, Qt)
-              || XFASTINT (tem) == 0))
-      {
-       buffer = Fget_buffer (tem);
-       CHECK_BUFFER (buffer, 2);
-      }
+    struct gcpro gcpro1, gcpro2, gcpro3;
+
+    current_dir = current_buffer->directory;
+
+    GCPRO3 (infile, buffer, current_dir);
+
+    current_dir = 
+      expand_and_dir_to_file
+       (Funhandled_file_name_directory (current_dir), Qnil);
+    if (NILP (Ffile_accessible_directory_p (current_dir)))
+      report_file_error ("Setting current directory",
+                        Fcons (current_buffer->directory, Qnil));
+
+    UNGCPRO;
   }
 
-  display = nargs >= 3 ? args[3] : Qnil;
+  display = nargs >= 4 ? args[3] : Qnil;
 
   {
     register int i;
@@ -162,13 +241,13 @@ If you quit, the process is killed with SIGKILL.")
     new_argv[i - 3] = 0;
   }
 
-  filefd = open (XSTRING (args[1])->data, O_RDONLY, 0);
+  filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
   if (filefd < 0)
     {
-      report_file_error ("Opening process input file", Fcons (args[1], Qnil));
+      report_file_error ("Opening process input file", Fcons (infile, Qnil));
     }
   /* Search for program; barf if not found.  */
-  openp (Vexec_path, args[0], "", &path, 1);
+  openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
   if (NILP (path))
     {
       close (filefd);
@@ -176,11 +255,41 @@ If you quit, the process is killed with SIGKILL.")
     }
   new_argv[0] = XSTRING (path)->data;
 
+#ifdef MSDOS /* MW, July 1993 */
+  /* These vars record information from process termination.
+     Clear them now before process can possibly terminate,
+     to avoid timing error if process terminates soon.  */
+  synch_process_death = 0;
+  synch_process_retcode = 0;
+
+  if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
+    strcpy (tempfile = alloca (strlen (outf) + 20), outf);
+  else
+    {
+      tempfile = alloca (20);
+      *tempfile = '\0';
+    }
+  dostounix_filename (tempfile);
+  if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/') 
+    strcat (tempfile, "/");
+  strcat (tempfile, "detmp.XXX");
+  mktemp (tempfile);
+
+  outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
+  if (outfilefd < 0)
+    {
+      close (filefd);
+      report_file_error ("Opening process output file", Fcons (tempfile, Qnil));
+    }
+#endif
+
   if (XTYPE (buffer) == Lisp_Int)
-    fd[1] = open ("/dev/null", O_WRONLY), fd[0] = -1;
+    fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
   else
     {
+#ifndef MSDOS
       pipe (fd);
+#endif
 #if 0
       /* Replaced by close_process_descs */
       set_exclusive_use (fd[0]);
@@ -192,14 +301,6 @@ If you quit, the process is killed with SIGKILL.")
        Protect it from permanent change.  */
     register char **save_environ = environ;
     register int fd1 = fd[1];
-    char **env;
-
-#ifdef MAINTAIN_ENVIRONMENT
-    env = (char **) alloca (size_of_current_environ ());
-    get_current_environ (env);
-#else
-    env = environ;
-#endif /* MAINTAIN_ENVIRONMENT */
 
 #if 0  /* Some systems don't have sigblock.  */
     mask = sigblock (sigmask (SIGCHLD));
@@ -208,6 +309,23 @@ If you quit, the process is killed with SIGKILL.")
     /* Record that we're about to create a synchronous process.  */
     synch_process_alive = 1;
 
+    /* These vars record information from process termination.
+       Clear them now before process can possibly terminate,
+       to avoid timing error if process terminates soon.  */
+    synch_process_death = 0;
+    synch_process_retcode = 0;
+
+#ifdef MSDOS /* MW, July 1993 */
+    pid = run_msdos_command (new_argv, current_dir, filefd, outfilefd);
+    close (outfilefd);
+    fd1 = -1; /* No harm in closing that one!  */
+    fd[0] = open (tempfile, NILP (Vbinary_process) ? O_TEXT : O_BINARY);
+    if (fd[0] < 0)
+      {
+       unlink (tempfile);
+       report_file_error ("Cannot re-open temporary file", Qnil);
+      }
+#else /* not MSDOS */
     pid = vfork ();
 
     if (pid == 0)
@@ -219,8 +337,9 @@ If you quit, the process is killed with SIGKILL.")
 #else
         setpgrp (pid, pid);
 #endif /* USG */
-       child_setup (filefd, fd1, fd1, new_argv, env, 0);
+       child_setup (filefd, fd1, fd1, new_argv, 0, current_dir);
       }
+#endif /* not MSDOS */
 
 #if 0
     /* Tell SIGCHLD handler to look for this pid.  */
@@ -232,7 +351,8 @@ If you quit, the process is killed with SIGKILL.")
     environ = save_environ;
 
     close (filefd);
-    close (fd1);
+    if (fd1 >= 0)
+      close (fd1);
   }
 
   if (pid < 0)
@@ -244,15 +364,22 @@ If you quit, the process is killed with SIGKILL.")
   if (XTYPE (buffer) == Lisp_Int)
     {
 #ifndef subprocesses
+      /* If Emacs has been built with asynchronous subprocess support,
+        we don't need to do this, I think because it will then have
+        the facilities for handling SIGCHLD.  */
       wait_without_blocking ();
 #endif /* subprocesses */
-
-      UNGCPRO;
       return Qnil;
     }
 
+#ifdef MSDOS
+  /* MSDOS needs different cleanup information.  */
+  record_unwind_protect (call_process_cleanup,
+                        Fcons (make_number (fd[0]), build_string (tempfile)));
+#else
   record_unwind_protect (call_process_cleanup,
                         Fcons (make_number (fd[0]), make_number (pid)));
+#endif /* not MSDOS */
 
 
   if (XTYPE (buffer) == Lisp_Buffer)
@@ -263,6 +390,7 @@ If you quit, the process is killed with SIGKILL.")
 
   {
     register int nread;
+    int first = 1;
 
     while ((nread = read (fd[0], buf, sizeof buf)) > 0)
       {
@@ -270,7 +398,12 @@ If you quit, the process is killed with SIGKILL.")
        if (!NILP (buffer))
          insert (buf, nread);
        if (!NILP (display) && INTERACTIVE)
-         redisplay_preserve_echo_area ();
+         {
+           if (first)
+             prepare_menu_bars ();
+           first = 0;
+           redisplay_preserve_echo_area ();
+         }
        immediate_quit = 1;
        QUIT;
       }
@@ -285,15 +418,13 @@ If you quit, the process is killed with SIGKILL.")
 
   unbind_to (count, Qnil);
 
-  UNGCPRO;
-
   if (synch_process_death)
     return build_string (synch_process_death);
   return make_number (synch_process_retcode);
 }
 #endif
 \f
-static void
+static Lisp_Object
 delete_temp_file (name)
      Lisp_Object name;
 {
@@ -310,25 +441,42 @@ Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
 Remaining args are passed to PROGRAM at startup as command args.\n\
 If BUFFER is nil, returns immediately with value nil.\n\
 Otherwise waits for PROGRAM to terminate\n\
-and returns a numeric exit status or a signal name as a string.\n\
-If you quit, the process is killed with SIGKILL.")
+and returns a numeric exit status or a signal description string.\n\
+If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   (nargs, args)
      int nargs;
      register Lisp_Object *args;
 {
   register Lisp_Object filename_string, start, end;
+#ifdef MSDOS
+  char *tempfile;
+#else
   char tempfile[20];
+#endif
   int count = specpdl_ptr - specpdl;
-  struct gcpro gcpro1;
+#ifdef MSDOS
+  char *outf = '\0';
 
-  GCPRO1 (*args);
-  gcpro1.nvars = 2;
+  if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
+    strcpy (tempfile = alloca (strlen (outf) + 20), outf);
+  else
+    {
+      tempfile = alloca (20);
+      *tempfile = '\0';
+    }
+  dostounix_filename (tempfile);
+  if (tempfile[strlen (tempfile) - 1] != '/')
+    strcat (tempfile, "/");
+  strcat (tempfile, "detmp.XXX");
+#else /* not MSDOS */
 
 #ifdef VMS
   strcpy (tempfile, "tmp:emacsXXXXXX.");
 #else
   strcpy (tempfile, "/tmp/emacsXXXXXX");
 #endif
+#endif /* not MSDOS */
+
   mktemp (tempfile);
 
   filename_string = build_string (tempfile);
@@ -341,10 +489,8 @@ If you quit, the process is killed with SIGKILL.")
     Fdelete_region (start, end);
 
   args[3] = filename_string;
-  Fcall_process (nargs - 2, args + 2);
 
-  UNGCPRO;
-  return unbind_to (count, Qnil);
+  return unbind_to (count, Fcall_process (nargs - 2, args + 2));
 }
 \f
 #ifndef VMS /* VMS version is in vmsproc.c.  */
@@ -362,46 +508,63 @@ If you quit, the process is killed with SIGKILL.")
    ENV is the environment for the subprocess.
 
    SET_PGRP is nonzero if we should put the subprocess into a separate
-   process group.  */
+   process group.  
+
+   CURRENT_DIR is an elisp string giving the path of the current
+   directory the subprocess should have.  Since we can't really signal
+   a decent error from within the child, this should be verified as an
+   executable directory by the parent.  */
 
-child_setup (in, out, err, new_argv, env, set_pgrp)
+child_setup (in, out, err, new_argv, set_pgrp, current_dir)
      int in, out, err;
      register char **new_argv;
-     char **env;
      int set_pgrp;
+     Lisp_Object current_dir;
 {
-  register int pid = getpid();
+#ifdef MSDOS
+  /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
+     instead.  */
+#else /* not MSDOS */
+  char **env;
 
-  setpriority (PRIO_PROCESS, pid, 0);
+  int pid = getpid ();
+
+  {
+    extern int emacs_priority;
+
+    nice (- emacs_priority);
+  }
 
 #ifdef subprocesses
   /* Close Emacs's descriptors that this process should not have.  */
   close_process_descs ();
 #endif
+  close_load_descs ();
 
   /* Note that use of alloca is always safe here.  It's obvious for systems
      that do not have true vfork or that have true (stack) alloca.
      If using vfork and C_ALLOCA it is safe because that changes
      the superior's static variables as if the superior had done alloca
      and will be cleaned up in the usual way.  */
+  {
+    register unsigned char *temp;
+    register int i;
 
-  if (XTYPE (current_buffer->directory) == Lisp_String)
-    {
-      register unsigned char *temp;
-      register int i;
-
-      i = XSTRING (current_buffer->directory)->size;
-      temp = (unsigned char *) alloca (i + 2);
-      bcopy (XSTRING (current_buffer->directory)->data, temp, i);
-      if (temp[i - 1] != '/') temp[i++] = '/';
-      temp[i] = 0;
-      /* Switch to that directory, and report any error.  */
-      if (chdir (temp) < 0)
-       report_file_error ("In chdir",
-                          Fcons (current_buffer->directory, Qnil));
-    }
+    i = XSTRING (current_dir)->size;
+    temp = (unsigned char *) alloca (i + 2);
+    bcopy (XSTRING (current_dir)->data, temp, i);
+    if (temp[i - 1] != '/') temp[i++] = '/';
+    temp[i] = 0;
+
+    /* We can't signal an Elisp error here; we're in a vfork.  Since
+       the callers check the current directory before forking, this
+       should only return an error if the directory's permissions
+       are changed between the check and this chdir, but we should
+       at least check.  */
+    if (chdir (temp) < 0)
+      exit (errno);
+  }
 
-#ifndef MAINTAIN_ENVIRONMENT
   /* Set `env' to a vector of the strings in Vprocess_environment.  */
   {
     register Lisp_Object tem;
@@ -415,18 +578,49 @@ child_setup (in, out, err, new_argv, env, set_pgrp)
         tem = XCONS (tem)->cdr)
       new_length++;
 
-    /* new_length + 1 to include terminating 0 */
+    /* new_length + 1 to include terminating 0 */
     env = new_env = (char **) alloca ((new_length + 1) * sizeof (char *));
 
-    /* Copy the env strings into new_env.  */
+    /* Copy the Vprocess_environment strings into new_env.  */
     for (tem = Vprocess_environment;
         (XTYPE (tem) == Lisp_Cons
          && XTYPE (XCONS (tem)->car) == Lisp_String);
         tem = XCONS (tem)->cdr)
-      *new_env++ = (char *) XSTRING (XCONS (tem)->car)->data;
+      {
+       char **ep = env;
+       char *string = (char *) XSTRING (XCONS (tem)->car)->data;
+       /* See if this string duplicates any string already in the env.
+          If so, don't put it in.
+          When an env var has multiple definitions,
+          we keep the definition that comes first in process-environment.  */
+       for (; ep != new_env; ep++)
+         {
+           char *p = *ep, *q = string;
+           while (1)
+             {
+               if (*q == 0)
+                 /* The string is malformed; might as well drop it.  */
+                 goto duplicate;
+               if (*q != *p)
+                 break;
+               if (*q == '=')
+                 goto duplicate;
+               p++, q++;
+             }
+         }
+       *new_env++ = string;
+      duplicate: ;
+      }
     *new_env = 0;
   }
-#endif /* Not MAINTAIN_ENVIRONMENT */
+
+  /* Make sure that in, out, and err are not actually already in
+     descriptors zero, one, or two; this could happen if Emacs is
+     started with its standard in, out, or error closed, as might
+     happen under X.  */
+  in = relocate_fd (in, 3);
+  out = relocate_fd (out, 3);
+  err = relocate_fd (err, 3);
 
   close (0);
   close (1);
@@ -439,7 +633,15 @@ child_setup (in, out, err, new_argv, env, set_pgrp)
   close (out);
   close (err);
 
-  setpgrp_of_tty (pid);
+#ifdef USG
+#ifndef SETPGRP_RELEASES_CTTY
+  setpgrp ();                  /* No arguments but equivalent in this case */
+#endif
+#else
+  setpgrp (pid, pid);
+#endif /* USG */
+  /* setpgrp_of_tty is incorrect here; it uses input_fd.  */
+  EMACS_SET_TTY_PGRP (0, &pid);
 
 #ifdef vipc
   something missing here;
@@ -454,6 +656,36 @@ child_setup (in, out, err, new_argv, env, set_pgrp)
   write (1, "Couldn't exec the program ", 26);
   write (1, new_argv[0], strlen (new_argv[0]));
   _exit (1);
+#endif /* not MSDOS */
+}
+
+/* Move the file descriptor FD so that its number is not less than MIN.
+   If the file descriptor is moved at all, the original is freed.  */
+int
+relocate_fd (fd, min)
+     int fd, min;
+{
+  if (fd >= min)
+    return fd;
+  else
+    {
+      int new = dup (fd);
+      if (new == -1)
+       {
+         char *message1 = "Error while setting up child: ";
+         char *errmessage = strerror (errno);
+         char *message2 = "\n";
+         write (2, message1, strlen (message1));
+         write (2, errmessage, strlen (errmessage));
+         write (2, message2, strlen (message2));
+         _exit (1);
+       }
+      /* Note that we hold the original FD open while we recurse,
+        to guarantee we'll get a new FD if we need it.  */
+      new = relocate_fd (new, min);
+      close (fd);
+      return new;
+    }
 }
 
 static int
@@ -468,7 +700,7 @@ getenv_internal (var, varlen, value, valuelen)
   for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
     {
       Lisp_Object entry = XCONS (scan)->car;
-
+      
       if (XTYPE (entry) == Lisp_String
          && XSTRING (entry)->size > varlen
          && XSTRING (entry)->data[varlen] == '='
@@ -483,7 +715,7 @@ getenv_internal (var, varlen, value, valuelen)
   return 0;
 }
 
-DEFUN ("getenv", Fgetenv, Sgetenv, 1, 2, 0,
+DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
   "Return the value of environment variable VAR, as a string.\n\
 VAR should be a string.  Value is nil if VAR is undefined in the environment.\n\
 This function consults the variable ``process-environment'' for its value.")
@@ -502,10 +734,10 @@ This function consults the variable ``process-environment'' for its value.")
 }
 
 /* A version of getenv that consults process_environment, easily
-      callable from C.  */
+   callable from C.  */
 char *
 egetenv (var)
-          char *var;
+     char *var;
 {
   char *value;
   int valuelen;
@@ -518,36 +750,88 @@ egetenv (var)
 
 #endif /* not VMS */
 \f
+/* This is run before init_cmdargs.  */
+  
+init_callproc_1 ()
+{
+  char *data_dir = egetenv ("EMACSDATA");
+    
+  Vdata_directory
+    = Ffile_name_as_directory (build_string (data_dir ? data_dir 
+                                            : PATH_DATA));
+
+  /* Check the EMACSPATH environment variable, defaulting to the
+     PATH_EXEC path from paths.h.  */
+  Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
+  Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
+  Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
+}
+
+/* This is run after init_cmdargs, so that Vinvocation_directory is valid.  */
+
 init_callproc ()
 {
+  char *data_dir = egetenv ("EMACSDATA");
+    
   register char * sh;
-  register char **envp;
-  Lisp_Object execdir;
+  Lisp_Object tempdir;
 
-  /* Turn PATH_EXEC into a path.  `==' is just a string which we know
-     will not be the name of an environment variable.  */
-  Vexec_path = decode_env_path ("==", PATH_EXEC);
-  Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
-  Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
+  if (initialized && !NILP (Vinvocation_directory))
+    {
+      /* Add to the path the ../lib-src dir of the Emacs executable,
+        if that dir exists.  */
+      Lisp_Object tem, tem1;
+      tem = Fexpand_file_name (build_string ("../lib-src"),
+                              Vinvocation_directory);
+      tem1 = Ffile_exists_p (tem);
+      if (!NILP (tem1) && NILP (Fmember (tem, Vexec_path)))
+       {
+         Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
+         Vexec_directory = Ffile_name_as_directory (tem);
+
+         /* If we use ../lib-src, maybe use ../etc as well.
+            Do so if ../etc exists and has our DOC-... file in it.  */
+         if (data_dir == 0)
+           {
+             Lisp_Object tem, tem2, tem3;
+             tem = Fexpand_file_name (build_string ("../etc"),
+                                      Vinvocation_directory);
+             tem2 = Fexpand_file_name (Vdoc_file_name, tem);
+             tem3 = Ffile_exists_p (tem2);
+             if (!NILP (tem3))
+               Vdata_directory = Ffile_name_as_directory (tem);
+           }
+       }
+    }
 
-  execdir = Fdirectory_file_name (Vexec_directory);
-  if (access (XSTRING (execdir)->data, 0) < 0)
+  tempdir = Fdirectory_file_name (Vexec_directory);
+  if (access (XSTRING (tempdir)->data, 0) < 0)
     {
-      printf ("Warning: executable/documentation dir (%s) does not exist.\n",
+      printf ("Warning: arch-dependent data dir (%s) does not exist.\n",
              XSTRING (Vexec_directory)->data);
       sleep (2);
     }
 
+  tempdir = Fdirectory_file_name (Vdata_directory);
+  if (access (XSTRING (tempdir)->data, 0) < 0)
+    {
+      printf ("Warning: arch-independent data dir (%s) does not exist.\n",
+             XSTRING (Vdata_directory)->data);
+      sleep (2);
+    }
+
 #ifdef VMS
   Vshell_file_name = build_string ("*dcl*");
 #else
-  sh = (char *) egetenv ("SHELL");
+  sh = (char *) getenv ("SHELL");
   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
 #endif
+}
+
+set_process_environment ()
+{
+  register char **envp;
 
-#ifndef MAINTAIN_ENVIRONMENT
-  /* The equivalent of this operation was done
-     in init_environ in environ.c if MAINTAIN_ENVIRONMENT */
   Vprocess_environment = Qnil;
 #ifndef CANNOT_DUMP
   if (initialized)
@@ -555,11 +839,16 @@ init_callproc ()
     for (envp = environ; *envp; envp++)
       Vprocess_environment = Fcons (build_string (*envp),
                                    Vprocess_environment);
-#endif /* MAINTAIN_ENVIRONMENT */
 }
 
 syms_of_callproc ()
 {
+#ifdef MSDOS
+  DEFVAR_LISP ("binary-process", &Vbinary_process,
+    "*If non-nil then new subprocesses are assumed to produce binary output.");
+  Vbinary_process = Qnil;
+#endif
+
   DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
     "*File name to load inferior shells from.\n\
 Initialized from the SHELL environment variable.");
@@ -569,18 +858,29 @@ Initialized from the SHELL environment variable.");
 Each element is a string (directory name) or nil (try default directory).");
 
   DEFVAR_LISP ("exec-directory", &Vexec_directory,
-    "Directory that holds programs that come with GNU Emacs,\n\
-intended for Emacs to invoke.");
+    "Directory of architecture-dependent files that come with GNU Emacs,\n\
+especially executable programs intended for Emacs to invoke.");
+
+  DEFVAR_LISP ("data-directory", &Vdata_directory,
+    "Directory of architecture-independent files that come with GNU Emacs,\n\
+intended for Emacs to use.");
+
+  DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
+    "For internal use by the build procedure only.\n\
+This is the name of the directory in which the build procedure installed\n\
+Emacs's info files; the default value for Info-default-directory-list\n\
+includes this.");
+  Vconfigure_info_directory = build_string (PATH_INFO);
 
-#ifndef MAINTAIN_ENVIRONMENT
   DEFVAR_LISP ("process-environment", &Vprocess_environment,
-    "List of strings to append to environment of subprocesses that are started.\n\
-Each string should have the format ENVVARNAME=VALUE.");
-#endif
+    "List of environment variables for subprocesses to inherit.\n\
+Each element should be a string of the form ENVVARNAME=VALUE.\n\
+The environment which Emacs inherits is placed in this variable\n\
+when Emacs starts.");
 
 #ifndef VMS
   defsubr (&Scall_process);
+  defsubr (&Sgetenv);
 #endif
   defsubr (&Scall_process_region);
-  defsubr (&Sgetenv);
 }