5 #include <types/types.h>
6 #include <talloc/talloc.h>
10 #define to_stmt_simple(stmt) \
11 container_of(stmt, struct grub2_statement_simple, st)
12 #define to_stmt_if(stmt) \
13 container_of(stmt, struct grub2_statement_if, st)
14 #define to_stmt_menuentry(stmt) \
15 container_of(stmt, struct grub2_statement_menuentry, st)
16 #define to_stmt_function(stmt) \
17 container_of(stmt, struct grub2_statement_function, st)
18 #define to_stmt_conditional(stmt) \
19 container_of(stmt, struct grub2_statement_conditional, st)
24 struct list_item list;
27 struct grub2_symtab_entry {
31 struct list_item list;
34 static struct grub2_symtab_entry *script_lookup_function(
35 struct grub2_script *script, const char *name)
37 struct grub2_symtab_entry *entry;
39 list_for_each_entry(&script->symtab, entry, list) {
40 if (!strcmp(entry->name, name))
47 const char *script_env_get(struct grub2_script *script, const char *name)
49 struct env_entry *entry;
51 list_for_each_entry(&script->environment, entry, list)
52 if (!strcmp(entry->name, name))
58 void script_env_set(struct grub2_script *script,
59 const char *name, const char *value)
61 struct env_entry *e, *entry = NULL;
63 list_for_each_entry(&script->environment, e, list) {
64 if (!strcmp(e->name, name)) {
71 entry = talloc(script, struct env_entry);
73 list_add(&script->environment, &entry->list);
79 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
83 val = script_env_get(script, word->name);
87 word->text = talloc_strdup(script, val);
92 static bool is_delim(char c)
94 return c == ' ' || c == '\t';
97 /* For non-double-quoted variable expansions, we may need to split the
98 * variable's value into multiple argv items.
100 * This function sets the word->text to the initial set of non-delimiter chars
101 * in the expanded var value. We then skip any delimiter chars, and (if
102 * required), create the new argv item with the remaining text, and
103 * add it to the argv list, after top_word.
105 static void process_split(struct grub2_script *script,
106 struct grub2_word *top_word,
107 struct grub2_word *word)
109 int i, len, delim_start = -1, delim_end = -1;
110 struct grub2_word *new_word;
113 len = strlen(word->text);
115 /* Scan our string for the start of a delim (delim_start), and the
116 * start of any new text (delim_end). */
117 for (i = 0; i < len; i++) {
118 if (is_delim(word->text[i])) {
119 if (delim_start == -1)
121 } else if (delim_start != -1) {
127 /* No delim? nothing to do. The process_expansions loop will
128 * append this word's text to the top word, if necessary
130 if (delim_start == -1)
133 /* Set this word's text value to the text before the delimiter.
134 * this will get appended to the top word
136 word->text[delim_start] = '\0';
138 /* No trailing text? If there are no following word tokens, we're done.
139 * Otherwise, we need to start a new argv item with those tokens */
140 if (delim_end == -1) {
145 remainder = word->text + delim_end;
148 new_word = talloc(script, struct grub2_word);
149 new_word->type = GRUB2_WORD_TEXT;
150 /* if there's no trailing text, we know we don't need to re-split */
151 new_word->split = delim_end != -1;
152 new_word->next = word->next;
153 new_word->last = NULL;
154 new_word->text = talloc_strdup(new_word, remainder);
156 /* stitch it into the argv list before this word */
157 list_insert_after(&top_word->argv_list,
158 &new_word->argv_list);
160 /* terminate this word */
164 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
167 * Once that's done, we may (if split == true) have to split the word to create
170 static void process_expansions(struct grub2_script *script,
171 struct grub2_argv *argv)
173 struct grub2_word *top_word, *word;
178 list_for_each_entry(&argv->words, top_word, argv_list) {
181 /* expand vars and squash the list of words into the head
182 * of the argv word list */
183 for (word = top_word; word; word = word->next) {
185 /* if it's a variable, perform the substitution */
186 if (word->type == GRUB2_WORD_VAR) {
187 expand_var(script, word);
188 word->type = GRUB2_WORD_TEXT;
191 /* split; this will potentially insert argv
192 * entries after top_word. */
194 process_split(script, top_word, word);
196 /* accumulate word text into the top word, so
197 * we end up with a shallow tree of argv data */
198 /* todo: don't do this in process_split */
199 if (word != top_word) {
200 top_word->text = talloc_asprintf_append(
207 top_word->next = NULL;
210 /* convert the list to an argv array, to pass to the function */
211 argv->argv = talloc_array(script, char *, argv->argc);
214 list_for_each_entry(&argv->words, word, argv_list)
215 argv->argv[i++] = word->text;
218 static int statements_execute(struct grub2_script *script,
219 struct grub2_statements *stmts)
221 struct grub2_statement *stmt;
224 list_for_each_entry(&stmts->list, stmt, list) {
226 rc = stmt->exec(script, stmt);
231 int statement_simple_execute(struct grub2_script *script,
232 struct grub2_statement *statement)
234 struct grub2_statement_simple *st = to_stmt_simple(statement);
235 struct grub2_symtab_entry *entry;
241 process_expansions(script, st->argv);
246 entry = script_lookup_function(script, st->argv->argv[0]);
248 fprintf(stderr, "undefined function '%s'\n", st->argv->argv[0]);
252 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
257 /* returns 0 if the statement was executed, 1 otherwise */
258 static int statement_conditional_execute(struct grub2_script *script,
259 struct grub2_statement *statement, bool *executed)
261 struct grub2_statement_conditional *st = to_stmt_conditional(statement);
264 rc = st->condition->exec(script, st->condition);
267 rc = statements_execute(script, st->statements);
272 int statement_if_execute(struct grub2_script *script,
273 struct grub2_statement *statement)
275 struct grub2_statement_if *st = to_stmt_if(statement);
276 struct grub2_statement *conditional;
280 list_for_each_entry(&st->conditionals->list, conditional, list) {
281 rc = statement_conditional_execute(script,
282 conditional, &executed);
287 if (!executed && st->else_case)
288 rc = statements_execute(script, st->else_case);
293 int statement_menuentry_execute(struct grub2_script *script,
294 struct grub2_statement *statement)
296 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
297 struct discover_boot_option *opt;
299 process_expansions(script, st->argv);
301 opt = discover_boot_option_create(script->ctx, script->ctx->device);
302 if (st->argv->argc > 0) {
303 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
305 opt->option->name = talloc_strdup(opt, "(unknown)");
310 statements_execute(script, st->statements);
312 discover_context_add_boot_option(script->ctx, opt);
318 static int function_invoke(struct grub2_script *script,
319 void *data, int argc, char **argv)
321 struct grub2_statement_function *fn = data;
325 /* set positional parameters */
326 for (i = 0; i < argc; i++) {
327 name = talloc_asprintf(script, "$%d", i);
328 script_env_set(script, name, argv[i]);
331 return statements_execute(script, fn->body);
334 int statement_function_execute(struct grub2_script *script,
335 struct grub2_statement *statement)
337 struct grub2_statement_function *st = to_stmt_function(statement);
340 if (st->name->type == GRUB2_WORD_VAR)
341 expand_var(script, st->name);
343 name = st->name->text;
344 script_register_function(script, name, function_invoke, st);
349 static void init_env(struct grub2_script *script)
351 struct env_entry *env;
353 list_init(&script->environment);
355 env = talloc(script, struct env_entry);
356 env->name = talloc_strdup(env, "prefix");
357 env->value = talloc_strdup(env, "/");
359 list_add(&script->environment, &env->list);
362 void script_register_function(struct grub2_script *script,
363 const char *name, grub2_function fn,
366 struct grub2_symtab_entry *entry;
368 entry = talloc(script, struct grub2_symtab_entry);
372 list_add(&script->symtab, &entry->list);
376 void script_execute(struct grub2_script *script)
378 statements_execute(script, script->statements);
381 struct grub2_script *create_script(struct grub2_parser *parser,
382 struct discover_context *ctx)
384 struct grub2_script *script;
386 script = talloc(parser, struct grub2_script);
392 list_init(&script->symtab);
393 register_builtins(script);