]> git.ozlabs.org Git - petitboot/blobdiff - discover/grub2/script.c
discover/grub2: Remove debug printfs
[petitboot] / discover / grub2 / script.c
index 067b0c9440857f48b0ce4c0ad84c3e3e559484ac..3fee4705622524f34d0139f1861e2cc07b789932 100644 (file)
@@ -7,6 +7,11 @@
 
 #include "grub2.h"
 
+#define to_stmt_simple(stmt) \
+       container_of(stmt, struct grub2_statement_simple, st)
+#define to_stmt_if(stmt) \
+       container_of(stmt, struct grub2_statement_if, st)
+
 struct env_entry {
        const char              *name;
        const char              *value;
@@ -17,13 +22,10 @@ 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);
 
        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;
@@ -34,17 +36,21 @@ 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;
 
        n = regexec(&script->var_re, src, 1, &match, 0);
-       if (n == 0)
+       if (n != 0)
                return false;
 
-       val = env_lookup(script, src + match.rm_so,
-                                match.rm_eo - match.rm_so);
-       if (val)
+       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 = "";
 
        dest = talloc_strndup(script, src, match.rm_so);
@@ -74,6 +80,73 @@ static void process_expansions(struct grub2_script *script,
        }
 }
 
+int statements_execute(struct grub2_script *script,
+               struct grub2_statements *stmts)
+{
+       struct grub2_statement *stmt;
+       int rc = 0;
+
+       list_for_each_entry(&stmts->list, stmt, list) {
+               if (stmt->exec)
+                       rc = stmt->exec(script, stmt);
+       }
+       return rc;
+}
+
+int statement_simple_execute(struct grub2_script *script,
+               struct grub2_statement *statement)
+{
+       struct grub2_statement_simple *st = to_stmt_simple(statement);
+
+       if (!st->argv)
+               return 0;
+
+       process_expansions(script, st->argv);
+
+       return 0;
+}
+
+int statement_if_execute(struct grub2_script *script,
+               struct grub2_statement *statement)
+{
+       struct grub2_statement_if *st = to_stmt_if(statement);
+       struct grub2_statements *case_stmts;
+       int rc;
+
+       rc = st->condition->exec(script, st->condition);
+
+       if (rc == 0)
+               case_stmts = st->true_case;
+       else
+               case_stmts = st->false_case;
+
+       if (case_stmts)
+               statements_execute(script, case_stmts);
+       else
+               rc = 0;
+
+       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)
+{
+       statements_execute(script, script->statements);
+}
+
 static int script_destroy(void *p)
 {
        struct grub2_script *script = p;
@@ -101,7 +174,7 @@ struct grub2_script *create_script(void *ctx)
        }
        talloc_set_destructor(script, script_destroy);
 
-       list_init(&script->environment);
+       init_env(script);
 
        return script;
 }