/* Nonzero if this is termination due to exit. */
static int call_process_exited;
+EXFUN (Fgetenv_internal, 2);
+
#ifndef VMS /* VMS version is in vmsproc.c. */
static Lisp_Object
/* 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. */
+ If using vfork and C_ALLOCA (when Emacs used to include
+ src/alloca.c) 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 char *temp;
register int i;
register char **new_env;
char **p, **q;
register int new_length;
- Lisp_Object local = selected_frame; /* get_frame_param (XFRAME (Fframe_with_environment (selected_frame)), */
-/* Qenvironment); */
-
- Lisp_Object display;
+ Lisp_Object display = Qnil;
new_length = 0;
for (tem = Vprocess_environment;
CONSP (tem) && STRINGP (XCAR (tem));
tem = XCDR (tem))
- new_length++;
-
-#if 0
- for (tem = local;
- CONSP (tem) && STRINGP (XCAR (tem));
- tem = XCDR (tem))
- new_length++;
-#endif
+ {
+ if (strncmp (SDATA (XCAR (tem)), "DISPLAY", 7) == 0
+ && (SDATA (XCAR (tem)) [7] == '\0'
+ || SDATA (XCAR (tem)) [7] == '='))
+ /* DISPLAY is specified in process-environment. */
+ display = Qt;
+ new_length++;
+ }
- /* Add DISPLAY from the frame local values. */
- display = get_frame_param (XFRAME (local), Qdisplay_environment_variable);
- if (! NILP (display))
- new_length++;
+ /* If not provided yet, use the frame's DISPLAY. */
+ if (NILP (display))
+ {
+ Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
+ if (!STRINGP (tmp) && CONSP (Vinitial_environment))
+ /* If still not found, Look for DISPLAY in Vinitial_environment. */
+ tmp = Fgetenv_internal (build_string ("DISPLAY"),
+ Vinitial_environment);
+ if (STRINGP (tmp))
+ {
+ display = tmp;
+ new_length++;
+ }
+ }
/* new_length + 2 to include PWD and terminating 0. */
env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
for (tem = Vprocess_environment;
CONSP (tem) && STRINGP (XCAR (tem));
tem = XCDR (tem))
- {
- if (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. */
}
static int
-getenv_internal (var, varlen, value, valuelen, frame)
+getenv_internal_1 (var, varlen, value, valuelen, env)
char *var;
int varlen;
char **value;
int *valuelen;
- Lisp_Object frame;
+ Lisp_Object env;
{
- Lisp_Object scan;
- Lisp_Object display;
-
- /* FIXME: Code duplication. */
-
- if (NILP (frame))
+ for (; CONSP (env); env = XCDR (env))
{
- /* Try to find VAR in Vprocess_environment first. */
- for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
- {
- Lisp_Object entry = XCAR (scan);
- if (STRINGP (entry)
- && SBYTES (entry) >= varlen
+ Lisp_Object entry = XCAR (env);
+ if (STRINGP (entry)
+ && SBYTES (entry) >= varlen
#ifdef WINDOWSNT
- /* NT environment variables are case insensitive. */
- && ! strnicmp (SDATA (entry), var, varlen)
+ /* NT environment variables are case insensitive. */
+ && ! strnicmp (SDATA (entry), var, varlen)
#else /* not WINDOWSNT */
- && ! bcmp (SDATA (entry), var, varlen)
+ && ! 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;
- }
- }
- }
- frame = selected_frame;
+ )
+ {
+ 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. */
+ *value = NULL;
+ return 1;
+ }
+ }
}
+ return 0;
+}
- /* For DISPLAY first try to get the values from the frame. */
- 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);
+static int
+getenv_internal (var, varlen, value, valuelen, frame)
+ char *var;
+ int varlen;
+ char **value;
+ int *valuelen;
+ Lisp_Object frame;
+{
+ /* Try to find VAR in Vprocess_environment first. */
+ if (getenv_internal_1 (var, varlen, value, valuelen,
+ Vprocess_environment))
+ return *value ? 1 : 0;
- for (scan = get_frame_param (XFRAME (frame), Qenvironment);
- CONSP (scan);
- scan = XCDR (scan))
+ /* For DISPLAY try to get the values from the frame or the initial env. */
+ if (strcmp (var, "DISPLAY") == 0)
{
- Lisp_Object entry;
-
- entry = XCAR (scan);
- if (STRINGP (entry)
- && SBYTES (entry) > varlen
- && SREF (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 */
- )
+ Lisp_Object display
+ = Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay);
+ if (STRINGP (display))
{
- *value = (char *) SDATA (entry) + (varlen + 1);
- *valuelen = SBYTES (entry) - (varlen + 1);
+ *value = (char *) SDATA (display);
+ *valuelen = SBYTES (display);
return 1;
}
+ /* If still not found, Look for DISPLAY in Vinitial_environment. */
+ if (getenv_internal_1 (var, varlen, value, valuelen,
+ Vinitial_environment))
+ return *value ? 1 : 0;
}
-#endif
+
return 0;
}
not found there, then it continues the search in the environment list
of the selected frame.
-If optional parameter FRAME is non-nil, then this function will ignore
-`process-environment' and will simply look up the variable in that
-frame's environment. */)
- (variable, frame)
- Lisp_Object variable, frame;
+If optional parameter ENV is a list, then search this list instead of
+`process-environment', and return t when encountering a negative entry.
+
+If it is a frame, then this function will ignore `process-environment' and
+will simply look up the variable in that frame's environment. */)
+ (variable, env)
+ Lisp_Object variable, env;
{
char *value;
int valuelen;
CHECK_STRING (variable);
- if (getenv_internal (SDATA (variable), SBYTES (variable),
- &value, &valuelen, frame))
+ if (CONSP (env))
+ {
+ if (getenv_internal_1 (SDATA (variable), SBYTES (variable),
+ &value, &valuelen, env))
+ return value ? make_string (value, valuelen) : Qt;
+ else
+ return Qnil;
+ }
+ else if (getenv_internal (SDATA (variable), SBYTES (variable),
+ &value, &valuelen, env))
return make_string (value, valuelen);
else
return Qnil;