From 0b0787bfcde6d3b3e6b3f9282b084e4054cd9290 Mon Sep 17 00:00:00 2001 From: SeokYeon Hwang Date: Mon, 16 Jun 2014 17:17:27 +0900 Subject: [PATCH] emulator: fix bug when multiple variables are in a line Change-Id: I5535bebcdd656bdd9700b82bf21f3dedf7ff615c Signed-off-by: SeokYeon Hwang --- tizen/src/emulator_options.c | 74 +++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/tizen/src/emulator_options.c b/tizen/src/emulator_options.c index 271bd42..ed63170 100644 --- a/tizen/src/emulator_options.c +++ b/tizen/src/emulator_options.c @@ -133,58 +133,68 @@ static void reset_default_opts(void) } } -static char *substitute_variables(char *str) +static char *substitute_variables(char *src) { int i = 0; int start_index = -1; int end_index = -1; + char *str = g_strdup(src); for (i = 0; str[i]; ++i) { if(str[i] == '$' && str[i + 1] && str[i + 1] == '{') { start_index = i++; + + continue; } else if(str[i] == '}') { end_index = i; - } - } - if (start_index != -1 && end_index != -1) { - char name[TOKEN_LIMIT]; - char *value = NULL; - char *arg = NULL; - int length; + if (start_index == -1 || end_index == -1) { + // must not enter here + continue; + } - g_strlcpy(name, str + start_index + 2, end_index - start_index - 1); + char name[TOKEN_LIMIT]; + char *value = NULL; + char *buf = NULL; + int length; - // search stored variables - value = get_variable(name); + g_strlcpy(name, str + start_index + 2, end_index - start_index - 1); - // if there is no name in stored variables, - // try to search environment variables - if(!value) { - value = getenv(name); - } + // search stored variables + value = get_variable(name); - if(!value) { - fprintf(stderr, "[%s] is not set." - " Please input value using commandline argument" - " \"--%s\" or profile default file or envirionment" - " variable.\n", name, name); - value = (char *)""; - } + // if there is no name in stored variables, + // try to search environment variables + if(!value) { + value = getenv(name); + } - length = start_index + strlen(value) + (strlen(str) - end_index); - arg = g_malloc(length); + if(!value) { + fprintf(stderr, "[%s] is not set." + " Please input value using commandline argument" + " \"--%s\" or profile default file or envirionment" + " variable.\n", name, name); + value = (char *)""; + } - g_strlcpy(arg, str, start_index + 1); - g_strlcat(arg, value, length); - g_strlcat(arg, str + end_index + 1, length); + length = start_index + strlen(value) + (strlen(str) - end_index); + buf = g_malloc(length); - return arg; - } - else { - return g_strdup(str); + g_strlcpy(buf, str, start_index + 1); + g_strlcat(buf, value, length); + g_strlcat(buf, str + end_index + 1, length); + + g_free(str); + str = buf; + + i = start_index + strlen(value); + + start_index = end_index = -1; + } } + + return str; } bool load_profile_default(const char * const profile) -- 2.7.4