emulator_options: introduced recursive variable substitution
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 17 Jun 2015 05:49:20 +0000 (14:49 +0900)
committerminkee Lee <minkee.lee@samsung.com>
Wed, 17 Jun 2015 06:24:15 +0000 (15:24 +0900)
Users can write variable position-independently.

Change-Id: I515843030f3d87bfea14a420133ffb1cdc4a1bac
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
tizen/src/emulator_options.c

index 8e8c9d7cf95e895d481b88ddb9bb80af579ef08f..e2eff580e9df4c5702a184c8e6cbacf512c5dd4c 100644 (file)
@@ -133,7 +133,7 @@ static void reset_default_opts(void)
     }
 }
 
-static char *substitute_variables(char *src)
+static char *substitute_variables(char *src, bool recursive)
 {
     int i = 0;
     int start_index = -1;
@@ -164,12 +164,16 @@ static char *substitute_variables(char *src)
             // search stored variables
             value = get_variable(name);
 
-            // if there is no name in stored variables,
-            // try to search environment variables
             if(!value) {
+                // if there is no name in stored variables,
+                // try to search environment variables
                 value = getenv(name);
             }
 
+            if (recursive) {
+                value = substitute_variables(value, true);
+            }
+
             if(!value) {
                 fprintf(stderr, "[%s] is not set."
                         " Please input value using commandline argument"
@@ -276,9 +280,9 @@ bool load_conf(const char * const conf)
                 {
                     gchar **splitted = g_strsplit(token, "=", 2);
                     if (splitted[0] && splitted[1]) {
-                        char *value = substitute_variables(splitted[1]);
-                        set_variable(g_strdup(splitted[0]), value,
-                                false);
+                        // FIXME: we override previous value if already exist.
+                        // We should warn to users.
+                        set_variable(splitted[0], splitted[1], true);
                     }
                     g_strfreev(splitted);
 
@@ -293,6 +297,12 @@ bool load_conf(const char * const conf)
         }
     }
 
+    // assemble variables
+    struct variable *var = NULL;
+    QTAILQ_FOREACH(var, &variables, entry) {
+        set_variable(var->name, substitute_variables(var->value, true), true);
+    }
+
     fclose(file);
     return true;
 }
@@ -335,7 +345,7 @@ static bool assemble_args(int *argc, char **argv,
         }
 
         // substitute variables
-        argv[(*argc)++] = substitute_variables(str);
+        argv[(*argc)++] = substitute_variables(str, false);
     }
 
     return true;