Merge from emacs--devo--0
[bpt/emacs.git] / src / callproc.c
index 8e7ff24..5bd4cee 100644 (file)
@@ -6,7 +6,7 @@ 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 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -119,7 +119,7 @@ Lisp_Object Vprocess_environment;
 Lisp_Object Qbuffer_file_type;
 #endif /* DOS_NT */
 
-/* True iff we are about to fork off a synchronous process or if we
+/* True if we are about to fork off a synchronous process or if we
    are waiting for it.  */
 int synch_process_alive;
 
@@ -1245,6 +1245,8 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
 {
   char **env;
   char *pwd_var;
+  char *term_var;
+  char *display_var;
 #ifdef WINDOWSNT
   int cpid;
   HANDLE handles[3];
@@ -1325,9 +1327,12 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
     register char **new_env;
     char **p, **q;
     register int new_length;
-    Lisp_Object local = get_frame_param (XFRAME (Fframe_with_environment (selected_frame)),
-                                         Qenvironment);
+    Lisp_Object local = selected_frame; /* get_frame_param (XFRAME (Fframe_with_environment (selected_frame)), */
+/*                                          Qenvironment); */
 
+    Lisp_Object term;
+    Lisp_Object display;
+    
     new_length = 0;
 
     for (tem = Vprocess_environment;
@@ -1335,10 +1340,21 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
          tem = XCDR (tem))
       new_length++;
 
+#if 0    
     for (tem = local;
         CONSP (tem) && STRINGP (XCAR (tem));
         tem = XCDR (tem))
       new_length++;
+#endif
+
+    /* Add TERM and DISPLAY from the frame local values. */
+    term = get_frame_param (XFRAME (local), Qterm_environment_variable);
+    if (! NILP (term))
+      new_length++;
+
+    display = get_frame_param (XFRAME (local), Qdisplay_environment_variable);
+    if (! NILP (display))
+      new_length++;
 
     /* new_length + 2 to include PWD and terminating 0.  */
     env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
@@ -1348,18 +1364,43 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
     if (egetenv ("PWD"))
       *new_env++ = pwd_var;
  
+    if (! NILP (term))
+      {
+       int vlen = strlen ("TERM=") + strlen (SDATA (term)) + 1;
+       char *vdata = (char *) alloca (vlen);
+       strcpy (vdata, "TERM=");
+       strcat (vdata, SDATA (term));
+       new_env = add_env (env, new_env, vdata);
+      }
+
+    if (! NILP (display))
+      {
+       int vlen = strlen ("DISPLAY=") + strlen (SDATA (display)) + 1;
+       char *vdata = (char *) alloca (vlen);
+       strcpy (vdata, "DISPLAY=");
+       strcat (vdata, SDATA (display));
+       new_env = add_env (env, new_env, vdata);
+      }
+
     /* Overrides.  */
     for (tem = Vprocess_environment;
         CONSP (tem) && STRINGP (XCAR (tem));
         tem = XCDR (tem))
-      new_env = add_env (env, new_env, SDATA (XCAR (tem)));
+      {
+       if ((strcmp (SDATA (XCAR (tem)), "TERM") != 0)
+           && (strcmp (SDATA (XCAR (tem)), "DISPLAY") != 0))
+         new_env = add_env (env, new_env, SDATA (XCAR (tem)));
+      }
 
+  
+#if 0    
     /* Local part of environment.  */
     for (tem = local;
          CONSP (tem) && STRINGP (XCAR (tem));
          tem = XCDR (tem))
       new_env = add_env (env, new_env, SDATA (XCAR (tem)));
-
+#endif
+    
     *new_env = 0;
 
     /* Remove variable names without values.  */
@@ -1373,6 +1414,8 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
           p++;
       }
   }
+
+  
 #ifdef WINDOWSNT
   prepare_standard_handles (in, out, err, handles);
   set_process_dir (SDATA (current_dir));
@@ -1494,6 +1537,9 @@ getenv_internal (var, varlen, value, valuelen, frame)
      Lisp_Object frame;
 {
   Lisp_Object scan;
+  Lisp_Object term;
+  Lisp_Object display;
+  
 
   if (NILP (frame))
     {
@@ -1528,6 +1574,56 @@ getenv_internal (var, varlen, value, valuelen, frame)
       frame = selected_frame;
     }
 
+  /* For TERM and DISPLAY first try to get the values from the frame. */
+  term = get_frame_param (XFRAME (frame), Qterm_environment_variable);
+  if (strcmp (var, "TERM") == 0)
+    if (! NILP (term))
+      {
+         *value    = (char *) SDATA (term);
+         *valuelen = SBYTES (term);
+         return 1;
+      }
+  display = get_frame_param (XFRAME (frame), Qdisplay_environment_variable);
+  if (strcmp (var, "DISPLAY") == 0)
+    if (! NILP (display))
+      {
+         *value    = (char *) SDATA (display);
+         *valuelen = SBYTES (display);
+         return 1;
+      }
+
+  {
+    /* Try to find VAR in Vprocess_environment.  */
+    for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
+      {
+       Lisp_Object entry = XCAR (scan);
+       if (STRINGP (entry)
+           && SBYTES (entry) >= varlen
+#ifdef WINDOWSNT
+           /* NT environment variables are case insensitive.  */
+           && ! strnicmp (SDATA (entry), var, varlen)
+#else  /* not WINDOWSNT */
+           && ! bcmp (SDATA (entry), var, varlen)
+#endif /* not WINDOWSNT */
+           )
+         {
+           if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=')
+             {
+               *value = (char *) SDATA (entry) + (varlen + 1);
+               *valuelen = SBYTES (entry) - (varlen + 1);
+               return 1;
+             }
+           else if (SBYTES (entry) == varlen)
+             {
+               /* Lone variable names in Vprocess_environment mean that
+                  variable should be removed from the environment. */
+               return 0;
+             }
+         }
+      }
+  }
+
+#if 0
   /* Find the environment in which to search the variable. */
   CHECK_FRAME (frame);
   frame = Fframe_with_environment (frame);
@@ -1555,7 +1651,7 @@ getenv_internal (var, varlen, value, valuelen, frame)
          return 1;
        }
     }
-
+#endif
   return 0;
 }
 
@@ -1737,14 +1833,15 @@ void
 set_initial_environment ()
 {
   register char **envp;
-  Lisp_Object env = Qnil;
+  Lisp_Object env = Vprocess_environment;
 #ifndef CANNOT_DUMP
   if (initialized)
 #endif
     {
       for (envp = environ; *envp; envp++)
-        env = Fcons (build_string (*envp), env);
-      store_frame_param (SELECTED_FRAME(), Qenvironment, env);
+       Vprocess_environment = Fcons (build_string (*envp),
+                                     Vprocess_environment);
+      store_frame_param (SELECTED_FRAME(), Qenvironment, Vprocess_environment);
     }
 }
 
@@ -1781,12 +1878,12 @@ These are files intended for Emacs to use while it runs.  */);
 
   DEFVAR_LISP ("doc-directory", &Vdoc_directory,
               doc: /* Directory containing the DOC file that comes with GNU Emacs.
-This is usually the same as data-directory.  */);
+This is usually the same as `data-directory'.  */);
 
   DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
               doc: /* For internal use by the build procedure only.
 This is the name of the directory in which the build procedure installed
-Emacs's info files; the default value for Info-default-directory-list
+Emacs's info files; the default value for `Info-default-directory-list'
 includes this.  */);
   Vconfigure_info_directory = build_string (PATH_INFO);