From: Jeremy Kerr Date: Fri, 13 Sep 2013 05:40:40 +0000 (+0800) Subject: discover/grub2: Initial environment handling X-Git-Tag: v1.0.0~468 X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=561bedfedc9565764f71ac95957accabfdc677ab discover/grub2: Initial environment handling A simple linked-list implementation of string pairs. Signed-off-by: Jeremy Kerr --- diff --git a/discover/grub2/script.c b/discover/grub2/script.c index b6d3221..65cab15 100644 --- a/discover/grub2/script.c +++ b/discover/grub2/script.c @@ -22,13 +22,12 @@ static const char *env_lookup(struct grub2_script *script, const char *name, int name_len) { struct env_entry *entry; - const char *str; - str = talloc_strndup(script, name, name_len); - printf("%s: %s\n", __func__, str); + printf("%s: %.*s\n", __func__, name_len, name); list_for_each_entry(&script->environment, entry, list) - if (!strncmp(entry->name, name, name_len)) + if (!strncmp(entry->name, name, name_len) + && entry->name[name_len] == '\0') return entry->value; return NULL; @@ -39,7 +38,7 @@ static bool expand_word(struct grub2_script *script, struct grub2_word *word) const char *val, *src; char *dest = NULL; regmatch_t match; - int n; + int n, i; src = word->text; @@ -48,11 +47,17 @@ static bool expand_word(struct grub2_script *script, struct grub2_word *word) if (n != 0) return false; - val = env_lookup(script, src + match.rm_so, - match.rm_eo - match.rm_so); + i = 0; + if (src[match.rm_so + 1] == '{') + i++; + + val = env_lookup(script, src + match.rm_so + 1 + i, + match.rm_eo - match.rm_so - 1 - (i * 2)); if (!val) val = ""; + printf("repl: %s\n", val); + dest = talloc_strndup(script, src, match.rm_so); dest = talloc_asprintf_append(dest, "%s%s", val, src + match.rm_eo); @@ -89,7 +94,6 @@ int statements_execute(struct grub2_script *script, list_for_each_entry(&stmts->list, stmt, list) { if (stmt->exec) rc = stmt->exec(script, stmt); - printf("%s(%p)\n", __func__, stmt); } return rc; } @@ -129,6 +133,19 @@ int statement_if_execute(struct grub2_script *script, return rc; } +static void init_env(struct grub2_script *script) +{ + struct env_entry *env; + + list_init(&script->environment); + + env = talloc(script, struct env_entry); + env->name = talloc_strdup(env, "prefix"); + env->value = talloc_strdup(env, "/"); + + list_add(&script->environment, &env->list); +} + void script_execute(struct grub2_script *script) { @@ -162,7 +179,7 @@ struct grub2_script *create_script(void *ctx) } talloc_set_destructor(script, script_destroy); - list_init(&script->environment); + init_env(script); return script; }