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)
22 struct list_item list;
25 struct grub2_symtab_entry {
29 struct list_item list;
32 static struct grub2_symtab_entry *script_lookup_function(
33 struct grub2_script *script, const char *name)
35 struct grub2_symtab_entry *entry;
37 list_for_each_entry(&script->symtab, entry, list) {
38 if (!strcmp(entry->name, name))
45 const char *script_env_get(struct grub2_script *script, const char *name)
47 struct env_entry *entry;
49 list_for_each_entry(&script->environment, entry, list)
50 if (!strcmp(entry->name, name))
56 void script_env_set(struct grub2_script *script,
57 const char *name, const char *value)
59 struct env_entry *e, *entry = NULL;
61 list_for_each_entry(&script->environment, e, list) {
62 if (!strcmp(e->name, name)) {
69 entry = talloc(script, struct env_entry);
71 list_add(&script->environment, &entry->list);
77 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
81 val = script_env_get(script, word->name);
85 word->text = talloc_strdup(script, val);
90 static bool is_delim(char c)
92 return c == ' ' || c == '\t';
95 /* For non-double-quoted variable expansions, we may need to split the
96 * variable's value into multiple argv items.
98 * This function sets the word->text to the initial set of non-delimiter chars
99 * in the expanded var value. We then skip any delimiter chars, and (if
100 * required), create the new argv item with the remaining text, and
101 * add it to the argv list, after top_word.
103 static void process_split(struct grub2_script *script,
104 struct grub2_word *top_word,
105 struct grub2_word *word)
107 int i, len, delim_start = -1, delim_end = -1;
108 struct grub2_word *new_word;
111 len = strlen(word->text);
113 /* Scan our string for the start of a delim (delim_start), and the
114 * start of any new text (delim_end). */
115 for (i = 0; i < len; i++) {
116 if (is_delim(word->text[i])) {
117 if (delim_start == -1)
119 } else if (delim_start != -1) {
125 /* No delim? nothing to do. The process_expansions loop will
126 * append this word's text to the top word, if necessary
128 if (delim_start == -1)
131 /* Set this word's text value to the text before the delimiter.
132 * this will get appended to the top word
134 word->text[delim_start] = '\0';
136 /* No trailing text? If there are no following word tokens, we're done.
137 * Otherwise, we need to start a new argv item with those tokens */
138 if (delim_end == -1) {
143 remainder = word->text + delim_end;
146 new_word = talloc(script, struct grub2_word);
147 new_word->type = GRUB2_WORD_TEXT;
148 /* if there's no trailing text, we know we don't need to re-split */
149 new_word->split = delim_end != -1;
150 new_word->next = word->next;
151 new_word->last = NULL;
152 new_word->text = talloc_strdup(new_word, remainder);
154 /* stitch it into the argv list before this word */
155 list_insert_after(&top_word->argv_list,
156 &new_word->argv_list);
158 /* terminate this word */
162 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
165 * Once that's done, we may (if split == true) have to split the word to create
168 static void process_expansions(struct grub2_script *script,
169 struct grub2_argv *argv)
171 struct grub2_word *top_word, *word;
176 list_for_each_entry(&argv->words, top_word, argv_list) {
179 /* expand vars and squash the list of words into the head
180 * of the argv word list */
181 for (word = top_word; word; word = word->next) {
183 /* if it's a variable, perform the substitution */
184 if (word->type == GRUB2_WORD_VAR) {
185 expand_var(script, word);
186 word->type = GRUB2_WORD_TEXT;
189 /* split; this will potentially insert argv
190 * entries after top_word. */
192 process_split(script, top_word, word);
194 /* accumulate word text into the top word, so
195 * we end up with a shallow tree of argv data */
196 /* todo: don't do this in process_split */
197 if (word != top_word) {
198 top_word->text = talloc_asprintf_append(
205 top_word->next = NULL;
208 /* convert the list to an argv array, to pass to the function */
209 argv->argv = talloc_array(script, char *, argv->argc);
212 list_for_each_entry(&argv->words, word, argv_list)
213 argv->argv[i++] = word->text;
216 static int statements_execute(struct grub2_script *script,
217 struct grub2_statements *stmts)
219 struct grub2_statement *stmt;
222 list_for_each_entry(&stmts->list, stmt, list) {
224 rc = stmt->exec(script, stmt);
229 int statement_simple_execute(struct grub2_script *script,
230 struct grub2_statement *statement)
232 struct grub2_statement_simple *st = to_stmt_simple(statement);
233 struct grub2_symtab_entry *entry;
239 process_expansions(script, st->argv);
244 entry = script_lookup_function(script, st->argv->argv[0]);
246 fprintf(stderr, "undefined function '%s'\n", st->argv->argv[0]);
250 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
255 int statement_if_execute(struct grub2_script *script,
256 struct grub2_statement *statement)
258 struct grub2_statement_if *st = to_stmt_if(statement);
259 struct grub2_statements *case_stmts;
262 rc = st->condition->exec(script, st->condition);
265 case_stmts = st->true_case;
267 case_stmts = st->false_case;
270 statements_execute(script, case_stmts);
277 int statement_menuentry_execute(struct grub2_script *script,
278 struct grub2_statement *statement)
280 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
281 struct discover_boot_option *opt;
283 process_expansions(script, st->argv);
285 opt = discover_boot_option_create(script->ctx, script->ctx->device);
286 if (st->argv->argc > 0) {
287 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
289 opt->option->name = talloc_strdup(opt, "(unknown)");
294 statements_execute(script, st->statements);
296 discover_context_add_boot_option(script->ctx, opt);
302 static int function_invoke(struct grub2_script *script,
303 void *data, int argc, char **argv)
305 struct grub2_statement_function *fn = data;
309 /* set positional parameters */
310 for (i = 0; i < argc; i++) {
311 name = talloc_asprintf(script, "$%d", i);
312 script_env_set(script, name, argv[i]);
315 return statements_execute(script, fn->body);
318 int statement_function_execute(struct grub2_script *script,
319 struct grub2_statement *statement)
321 struct grub2_statement_function *st = to_stmt_function(statement);
324 if (st->name->type == GRUB2_WORD_VAR)
325 expand_var(script, st->name);
327 name = st->name->text;
328 script_register_function(script, name, function_invoke, st);
333 static void init_env(struct grub2_script *script)
335 struct env_entry *env;
337 list_init(&script->environment);
339 env = talloc(script, struct env_entry);
340 env->name = talloc_strdup(env, "prefix");
341 env->value = talloc_strdup(env, "/");
343 list_add(&script->environment, &env->list);
346 void script_register_function(struct grub2_script *script,
347 const char *name, grub2_function fn,
350 struct grub2_symtab_entry *entry;
352 entry = talloc(script, struct grub2_symtab_entry);
356 list_add(&script->symtab, &entry->list);
360 void script_execute(struct grub2_script *script)
362 statements_execute(script, script->statements);
365 struct grub2_script *create_script(struct grub2_parser *parser,
366 struct discover_context *ctx)
368 struct grub2_script *script;
370 script = talloc(parser, struct grub2_script);
376 list_init(&script->symtab);
377 register_builtins(script);