[Initng-svn] r2996 - in initng: plugins/envparser src

svn at initng.thinktux.net svn at initng.thinktux.net
Thu Feb 16 00:56:02 CET 2006


Author: makomk
Date: Thu Feb 16 00:56:02 2006
New Revision: 2996

Modified:
   initng/plugins/envparser/initng_envparser.c
   initng/src/initng_env_variable.c
   initng/src/initng_env_variable.h
Log:
Possible fix for bug #451 (not properly tested yet, since InitNG is slightly broken 
anyway...)


Modified: initng/plugins/envparser/initng_envparser.c
==============================================================================
--- initng/plugins/envparser/initng_envparser.c	(original)
+++ initng/plugins/envparser/initng_envparser.c	Thu Feb 16 00:56:02 2006
@@ -33,6 +33,7 @@
 #include "../../src/initng_open_read_close.h"
 #include "../../src/initng_string_tools.h"
 #include "../../src/initng_active_db.h"
+#include "../../src/initng_env_variable.h"
 
 #include "initng_envparser.h"
 s_entry ENV_FILE = { "env_file", STRINGS, NULL,
@@ -181,7 +182,17 @@
         point += i;
 
         /* add to service cache */
-        initng_service_cache_set_string_var(&ENV, vn, s, vv);
+	if(initng_service_cache_is_var(&ENV, vn, s)) 
+	{
+	    const char* oldval = initng_service_cache_get_string_var(&ENV, vn, s);
+	    char* fixed = fix_redefined_variable(vn, oldval, vv);
+	    free(vv);
+	    initng_service_cache_set_string_var(&ENV, vn, s, fixed);
+	} 
+	else
+	{
+	    initng_service_cache_set_string_var(&ENV, vn, s, vv);
+	}
 
         /* go to next row and parse */
         JUMP_TO_NEXT_ROW(point);

Modified: initng/src/initng_env_variable.c
==============================================================================
--- initng/src/initng_env_variable.c	(original)
+++ initng/src/initng_env_variable.c	Thu Feb 16 00:56:02 2006
@@ -209,16 +209,15 @@
                 {
                     if (is_same_env_var(env[i], var))
                     {
-                        free(var);
-                        var = NULL;
+		        /* we may want to override environemntal variables
+			   set above, particularly PATH and HOME */
+                        free(env[i]);
+                        env[i] = var;
                         break;
                     }
                 }
 
                 if (i == nr)
-                    /* insert an i_strdup below if you want to be able to free
-                     * the output of new_environ() safely
-                     */
                     env[nr++] = var;
             }
         }
@@ -269,6 +268,98 @@
     return var1[i] == var2[i];
 }
 
+/* Handles redefinitions like "env FOO = $FOO bar" */
+char *fix_redefined_variable(const char* name, const char* oldval, 
+			     const char* newdef) {
+    char *to;                   /* this is the pointer we are going to return */
+    char *set;                  /* pointer to step */
+
+    assert(name);
+    assert(oldval);
+    assert(newdef);
+
+    /* make sure we got any data */
+    if (!newdef)
+        return (NULL);
+
+    /* allocate that much memory that we think we need */
+    /* TODO, add checks that this value is never overridden */
+    to = (char *) i_calloc((strlen(newdef) + 150), sizeof(char));
+    if (!to)
+        return (NULL);
+
+    set = to;
+    /* while there is data to parse */
+    while (newdef[0])
+    {
+        int len = 0;
+
+        /*
+         * if its not containing the magical char loop, just copy the
+         * char and continue the main loop.
+         */
+        if (newdef[0] != '$')
+        {
+            set[0] = newdef[0];
+            newdef++;
+            set++;
+            set[0] = '\0';
+            continue;
+        }
+
+
+        /* jump the '$' char */
+        newdef++;
+        if (!newdef[0])
+            continue;
+
+        /*
+         * Calculate length of variable ${VARIABLE}.
+         */
+        if (newdef[0] == '{')                 /* if this is a ${VARIABLE} */
+        {
+            newdef++;
+            for (; newdef[len] && newdef[len] != '}'; len++) ;
+        }
+        else                                /* else a $VARIABLE */
+        {
+            for (; isgraph(newdef[len]); len++) ;
+        }
+
+        if (strncasecmp(newdef, name, len) == 0 && (int)strlen(name) == len)
+        {
+            strcpy(set, oldval);
+	    /* go forward */
+	    newdef += len;
+	    if (newdef[0] == '}')
+		newdef++;
+	    while (set[0])
+		set++;
+        }
+	else
+	{
+	    if(newdef[-1] == '{') {
+		strncpy(set, newdef-2, len+2);
+		newdef += len; set += len+2;
+		set[0] = '\0';
+	    }
+            else
+	    {
+		strncpy(set, newdef-1, len+1);
+		newdef += len; set += len+1;
+		set[0] = '\0';
+	    }
+	}
+    }
+
+    /* make sure end is terminated */
+    set[0] = '\0';
+
+    /* return the string we now fixed */
+    return (to);
+
+}
+
 /* This function will change variables in from, like $NAME, $SERVICE, $CLASS
  * into its right content, and return a new allocated string with the result.
  */

Modified: initng/src/initng_env_variable.h
==============================================================================
--- initng/src/initng_env_variable.h	(original)
+++ initng/src/initng_env_variable.h	Thu Feb 16 00:56:02 2006
@@ -24,6 +24,8 @@
 #include "initng.h"
 #include "initng_active_db.h"
 
+char *fix_redefined_variable(const char* name, const char* oldval, 
+			     const char* newdef);
 char *fix_variables(const char *from, active_db_h * s);
 char **new_environ(active_db_h * s);
 void free_environ(char **tf);


More information about the Initng-svn mailing list