6 #include <types/types.h>
7 #include <talloc/talloc.h>
11 #define to_stmt_simple(stmt) \
12 container_of(stmt, struct grub2_statement_simple, st)
13 #define to_stmt_block(stmt) \
14 container_of(stmt, struct grub2_statement_block, st)
15 #define to_stmt_if(stmt) \
16 container_of(stmt, struct grub2_statement_if, st)
17 #define to_stmt_menuentry(stmt) \
18 container_of(stmt, struct grub2_statement_menuentry, st)
19 #define to_stmt_function(stmt) \
20 container_of(stmt, struct grub2_statement_function, st)
21 #define to_stmt_conditional(stmt) \
22 container_of(stmt, struct grub2_statement_conditional, st)
27 struct list_item list;
30 struct grub2_symtab_entry {
34 struct list_item list;
37 static struct grub2_symtab_entry *script_lookup_function(
38 struct grub2_script *script, const char *name)
40 struct grub2_symtab_entry *entry;
42 list_for_each_entry(&script->symtab, entry, list) {
43 if (!strcmp(entry->name, name))
50 const char *script_env_get(struct grub2_script *script, const char *name)
52 struct env_entry *entry;
54 list_for_each_entry(&script->environment, entry, list)
55 if (!strcmp(entry->name, name))
61 void script_env_set(struct grub2_script *script,
62 const char *name, const char *value)
64 struct env_entry *e, *entry = NULL;
66 list_for_each_entry(&script->environment, e, list) {
67 if (!strcmp(e->name, name)) {
74 entry = talloc(script, struct env_entry);
76 list_add(&script->environment, &entry->list);
82 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
86 val = script_env_get(script, word->name);
90 word->text = talloc_strdup(script, val);
95 static bool is_delim(char c)
97 return c == ' ' || c == '\t';
100 static bool option_is_default(struct grub2_script *script,
101 struct discover_boot_option *opt)
103 unsigned int default_idx;
107 var = script_env_get(script, "default");
111 default_idx = strtoul(var, &end, 10);
112 if (end != var && *end == '\0')
113 return default_idx == script->n_options;
115 return !strcmp(opt->option->name, var);
118 /* For non-double-quoted variable expansions, we may need to split the
119 * variable's value into multiple argv items.
121 * This function sets the word->text to the initial set of non-delimiter chars
122 * in the expanded var value. We then skip any delimiter chars, and (if
123 * required), create the new argv item with the remaining text, and
124 * add it to the argv list, after top_word.
126 static void process_split(struct grub2_script *script,
127 struct grub2_word *top_word,
128 struct grub2_word *word)
130 int i, len, delim_start = -1, delim_end = -1;
131 struct grub2_word *new_word;
134 len = strlen(word->text);
136 /* Scan our string for the start of a delim (delim_start), and the
137 * start of any new text (delim_end). */
138 for (i = 0; i < len; i++) {
139 if (is_delim(word->text[i])) {
140 if (delim_start == -1)
142 } else if (delim_start != -1) {
148 /* No delim? nothing to do. The process_expansions loop will
149 * append this word's text to the top word, if necessary
151 if (delim_start == -1)
154 /* Set this word's text value to the text before the delimiter.
155 * this will get appended to the top word
157 word->text[delim_start] = '\0';
159 /* No trailing text? If there are no following word tokens, we're done.
160 * Otherwise, we need to start a new argv item with those tokens */
161 if (delim_end == -1) {
166 remainder = word->text + delim_end;
169 new_word = talloc(script, struct grub2_word);
170 new_word->type = GRUB2_WORD_TEXT;
171 /* if there's no trailing text, we know we don't need to re-split */
172 new_word->split = delim_end != -1;
173 new_word->next = word->next;
174 new_word->last = NULL;
175 new_word->text = talloc_strdup(new_word, remainder);
177 /* stitch it into the argv list before this word */
178 list_insert_after(&top_word->argv_list,
179 &new_word->argv_list);
181 /* terminate this word */
185 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
188 * Once that's done, we may (if split == true) have to split the word to create
191 static void process_expansions(struct grub2_script *script,
192 struct grub2_argv *argv)
194 struct grub2_word *top_word, *word;
199 list_for_each_entry(&argv->words, top_word, argv_list) {
202 /* expand vars and squash the list of words into the head
203 * of the argv word list */
204 for (word = top_word; word; word = word->next) {
206 /* if it's a variable, perform the substitution */
207 if (word->type == GRUB2_WORD_VAR) {
208 expand_var(script, word);
209 word->type = GRUB2_WORD_TEXT;
212 /* split; this will potentially insert argv
213 * entries after top_word. */
215 process_split(script, top_word, word);
217 /* accumulate word text into the top word, so
218 * we end up with a shallow tree of argv data */
219 /* todo: don't do this in process_split */
220 if (word != top_word) {
221 top_word->text = talloc_asprintf_append(
228 top_word->next = NULL;
231 /* convert the list to an argv array, to pass to the function */
232 argv->argv = talloc_array(script, char *, argv->argc);
235 list_for_each_entry(&argv->words, word, argv_list)
236 argv->argv[i++] = word->text;
239 static int statements_execute(struct grub2_script *script,
240 struct grub2_statements *stmts)
242 struct grub2_statement *stmt;
245 list_for_each_entry(&stmts->list, stmt, list) {
247 rc = stmt->exec(script, stmt);
252 int statement_simple_execute(struct grub2_script *script,
253 struct grub2_statement *statement)
255 struct grub2_statement_simple *st = to_stmt_simple(statement);
256 struct grub2_symtab_entry *entry;
263 process_expansions(script, st->argv);
268 /* is this a var=value assignment? */
269 pos = strchr(st->argv->argv[0], '=');
272 name = st->argv->argv[0];
273 name = talloc_strndup(st, name, pos - name);
275 script_env_set(script, name, value);
279 entry = script_lookup_function(script, st->argv->argv[0]);
281 fprintf(stderr, "undefined function '%s'\n", st->argv->argv[0]);
285 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
290 int statement_block_execute(struct grub2_script *script,
291 struct grub2_statement *statement)
293 struct grub2_statement_block *st = to_stmt_block(statement);
294 return statements_execute(script, st->statements);
297 /* returns 0 if the statement was executed, 1 otherwise */
298 static int statement_conditional_execute(struct grub2_script *script,
299 struct grub2_statement *statement, bool *executed)
301 struct grub2_statement_conditional *st = to_stmt_conditional(statement);
304 rc = st->condition->exec(script, st->condition);
307 rc = statements_execute(script, st->statements);
312 int statement_if_execute(struct grub2_script *script,
313 struct grub2_statement *statement)
315 struct grub2_statement_if *st = to_stmt_if(statement);
316 struct grub2_statement *conditional;
320 list_for_each_entry(&st->conditionals->list, conditional, list) {
321 rc = statement_conditional_execute(script,
322 conditional, &executed);
327 if (!executed && st->else_case)
328 rc = statements_execute(script, st->else_case);
333 int statement_menuentry_execute(struct grub2_script *script,
334 struct grub2_statement *statement)
336 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
337 struct discover_boot_option *opt;
339 process_expansions(script, st->argv);
341 opt = discover_boot_option_create(script->ctx, script->ctx->device);
342 if (st->argv->argc > 0) {
343 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
345 opt->option->name = talloc_strdup(opt, "(unknown)");
347 opt->option->id = talloc_asprintf(opt->option, "%s#%s",
348 script->ctx->device->device->id,
353 statements_execute(script, st->statements);
355 opt->option->is_default = option_is_default(script, opt);
357 discover_context_add_boot_option(script->ctx, opt);
364 static int function_invoke(struct grub2_script *script,
365 void *data, int argc, char **argv)
367 struct grub2_statement_function *fn = data;
371 /* set positional parameters */
372 for (i = 0; i < argc; i++) {
373 name = talloc_asprintf(script, "$%d", i);
374 script_env_set(script, name, argv[i]);
377 return statements_execute(script, fn->body);
380 int statement_function_execute(struct grub2_script *script,
381 struct grub2_statement *statement)
383 struct grub2_statement_function *st = to_stmt_function(statement);
386 if (st->name->type == GRUB2_WORD_VAR)
387 expand_var(script, st->name);
389 name = st->name->text;
390 script_register_function(script, name, function_invoke, st);
395 static void init_env(struct grub2_script *script)
397 struct env_entry *env;
399 list_init(&script->environment);
401 env = talloc(script, struct env_entry);
402 env->name = talloc_strdup(env, "prefix");
403 env->value = talloc_strdup(env, "/");
405 list_add(&script->environment, &env->list);
408 void script_register_function(struct grub2_script *script,
409 const char *name, grub2_function fn,
412 struct grub2_symtab_entry *entry;
414 entry = talloc(script, struct grub2_symtab_entry);
418 list_add(&script->symtab, &entry->list);
422 void script_execute(struct grub2_script *script)
424 statements_execute(script, script->statements);
427 struct grub2_script *create_script(struct grub2_parser *parser,
428 struct discover_context *ctx)
430 struct grub2_script *script;
432 script = talloc_zero(parser, struct grub2_script);
437 list_init(&script->symtab);
438 register_builtins(script);