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)
13 #define to_stmt_menuentry(stmt) \
14 container_of(stmt, struct grub2_statement_menuentry, st)
19 struct list_item list;
22 static const char *env_lookup(struct grub2_script *script,
25 struct env_entry *entry;
27 list_for_each_entry(&script->environment, entry, list)
28 if (!strcmp(entry->name, name))
34 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
38 val = env_lookup(script, word->var.name);
42 word->type = GRUB2_WORD_TEXT;
43 word->text = talloc_strdup(script, val);
48 /* iterate through the words in an argv, looking for GRUB2_WORD_VAR expansions.
50 * Once that's done, we may (if split == true) have to split the word to create
53 static void process_expansions(struct grub2_script *script,
54 struct grub2_argv *argv)
56 struct grub2_word *top_word, *word;
58 list_for_each_entry(&argv->words, top_word, argv_list) {
59 /* expand vars and squash the list of words into the top struct.
62 for (word = top_word; word; word = word->next) {
63 if (word->type == GRUB2_WORD_VAR)
64 expand_var(script, word);
69 top_word->text = talloc_asprintf_append(top_word->text,
72 top_word->next = NULL;
76 int statements_execute(struct grub2_script *script,
77 struct grub2_statements *stmts)
79 struct grub2_statement *stmt;
82 list_for_each_entry(&stmts->list, stmt, list) {
84 rc = stmt->exec(script, stmt);
89 int statement_simple_execute(struct grub2_script *script,
90 struct grub2_statement *statement)
92 struct grub2_statement_simple *st = to_stmt_simple(statement);
97 process_expansions(script, st->argv);
102 int statement_if_execute(struct grub2_script *script,
103 struct grub2_statement *statement)
105 struct grub2_statement_if *st = to_stmt_if(statement);
106 struct grub2_statements *case_stmts;
109 rc = st->condition->exec(script, st->condition);
112 case_stmts = st->true_case;
114 case_stmts = st->false_case;
117 statements_execute(script, case_stmts);
124 int statement_menuentry_execute(struct grub2_script *script,
125 struct grub2_statement *statement)
127 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
129 process_expansions(script, st->argv);
130 statements_execute(script, st->statements);
135 static void init_env(struct grub2_script *script)
137 struct env_entry *env;
139 list_init(&script->environment);
141 env = talloc(script, struct env_entry);
142 env->name = talloc_strdup(env, "prefix");
143 env->value = talloc_strdup(env, "/");
145 list_add(&script->environment, &env->list);
149 void script_execute(struct grub2_script *script)
151 statements_execute(script, script->statements);
154 struct grub2_script *create_script(void *ctx)
156 struct grub2_script *script;
158 script = talloc(ctx, struct grub2_script);