5 #include <talloc/talloc.h>
9 #define to_stmt_simple(stmt) \
10 container_of(stmt, struct grub2_statement_simple, st)
11 #define to_stmt_if(stmt) \
12 container_of(stmt, struct grub2_statement_if, st)
17 struct list_item list;
20 static const char *env_lookup(struct grub2_script *script,
23 struct env_entry *entry;
25 list_for_each_entry(&script->environment, entry, list)
26 if (!strcmp(entry->name, name))
32 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
36 val = env_lookup(script, word->var.name);
40 word->type = GRUB2_WORD_TEXT;
41 word->text = talloc_strdup(script, val);
46 /* iterate through the words in an argv, looking for GRUB2_WORD_VAR expansions.
48 * Once that's done, we may (if split == true) have to split the word to create
51 static void process_expansions(struct grub2_script *script,
52 struct grub2_argv *argv)
54 struct grub2_word *top_word, *word;
56 list_for_each_entry(&argv->words, top_word, argv_list) {
57 /* expand vars and squash the list of words into the top struct.
60 for (word = top_word; word; word = word->next) {
61 if (word->type == GRUB2_WORD_VAR)
62 expand_var(script, word);
67 top_word->text = talloc_asprintf_append(top_word->text,
70 top_word->next = NULL;
74 int statements_execute(struct grub2_script *script,
75 struct grub2_statements *stmts)
77 struct grub2_statement *stmt;
80 list_for_each_entry(&stmts->list, stmt, list) {
82 rc = stmt->exec(script, stmt);
87 int statement_simple_execute(struct grub2_script *script,
88 struct grub2_statement *statement)
90 struct grub2_statement_simple *st = to_stmt_simple(statement);
95 process_expansions(script, st->argv);
100 int statement_if_execute(struct grub2_script *script,
101 struct grub2_statement *statement)
103 struct grub2_statement_if *st = to_stmt_if(statement);
104 struct grub2_statements *case_stmts;
107 rc = st->condition->exec(script, st->condition);
110 case_stmts = st->true_case;
112 case_stmts = st->false_case;
115 statements_execute(script, case_stmts);
122 static void init_env(struct grub2_script *script)
124 struct env_entry *env;
126 list_init(&script->environment);
128 env = talloc(script, struct env_entry);
129 env->name = talloc_strdup(env, "prefix");
130 env->value = talloc_strdup(env, "/");
132 list_add(&script->environment, &env->list);
136 void script_execute(struct grub2_script *script)
138 statements_execute(script, script->statements);
141 struct grub2_script *create_script(void *ctx)
143 struct grub2_script *script;
145 script = talloc(ctx, struct grub2_script);