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)
20 struct list_item list;
23 struct grub2_symtab_entry {
27 struct list_item list;
30 static struct grub2_symtab_entry *script_lookup_function(
31 struct grub2_script *script, const char *name)
33 struct grub2_symtab_entry *entry;
35 list_for_each_entry(&script->symtab, entry, list) {
36 if (!strcmp(entry->name, name))
43 const char *script_env_get(struct grub2_script *script, const char *name)
45 struct env_entry *entry;
47 list_for_each_entry(&script->environment, entry, list)
48 if (!strcmp(entry->name, name))
54 void script_env_set(struct grub2_script *script,
55 const char *name, const char *value)
57 struct env_entry *e, *entry = NULL;
59 list_for_each_entry(&script->environment, e, list) {
60 if (!strcmp(e->name, name)) {
67 entry = talloc(script, struct env_entry);
69 list_add(&script->environment, &entry->list);
75 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
79 val = script_env_get(script, word->name);
83 word->text = talloc_strdup(script, val);
88 static bool is_delim(char c)
90 return c == ' ' || c == '\t';
93 /* For non-double-quoted variable expansions, we may need to split the
94 * variable's value into multiple argv items.
96 * This function sets the word->text to the initial set of non-delimiter chars
97 * in the expanded var value. We then skip any delimiter chars, and (if
98 * required), create the new argv item with the remaining text, and
99 * add it to the argv list, after top_word.
101 static void process_split(struct grub2_script *script,
102 struct grub2_word *top_word,
103 struct grub2_word *word)
105 int i, len, delim_start = -1, delim_end = -1;
106 struct grub2_word *new_word;
109 len = strlen(word->text);
111 /* Scan our string for the start of a delim (delim_start), and the
112 * start of any new text (delim_end). */
113 for (i = 0; i < len; i++) {
114 if (is_delim(word->text[i])) {
115 if (delim_start == -1)
117 } else if (delim_start != -1) {
123 /* No delim? nothing to do. The process_expansions loop will
124 * append this word's text to the top word, if necessary
126 if (delim_start == -1)
129 /* Set this word's text value to the text before the delimiter.
130 * this will get appended to the top word
132 word->text[delim_start] = '\0';
134 /* No trailing text? If there are no following word tokens, we're done.
135 * Otherwise, we need to start a new argv item with those tokens */
136 if (delim_end == -1) {
141 remainder = word->text + delim_end;
144 new_word = talloc(script, struct grub2_word);
145 new_word->type = GRUB2_WORD_TEXT;
146 /* if there's no trailing text, we know we don't need to re-split */
147 new_word->split = delim_end != -1;
148 new_word->next = word->next;
149 new_word->last = NULL;
150 new_word->text = talloc_strdup(new_word, remainder);
152 /* stitch it into the argv list before this word */
153 list_insert_after(&top_word->argv_list,
154 &new_word->argv_list);
156 /* terminate this word */
160 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
163 * Once that's done, we may (if split == true) have to split the word to create
166 static void process_expansions(struct grub2_script *script,
167 struct grub2_argv *argv)
169 struct grub2_word *top_word, *word;
174 list_for_each_entry(&argv->words, top_word, argv_list) {
177 /* expand vars and squash the list of words into the head
178 * of the argv word list */
179 for (word = top_word; word; word = word->next) {
181 /* if it's a variable, perform the substitution */
182 if (word->type == GRUB2_WORD_VAR) {
183 expand_var(script, word);
184 word->type = GRUB2_WORD_TEXT;
187 /* split; this will potentially insert argv
188 * entries after top_word. */
190 process_split(script, top_word, word);
192 /* accumulate word text into the top word, so
193 * we end up with a shallow tree of argv data */
194 /* todo: don't do this in process_split */
195 if (word != top_word) {
196 top_word->text = talloc_asprintf_append(
203 top_word->next = NULL;
206 /* convert the list to an argv array, to pass to the function */
207 argv->argv = talloc_array(script, char *, argv->argc);
210 list_for_each_entry(&argv->words, word, argv_list)
211 argv->argv[i++] = word->text;
214 static int statements_execute(struct grub2_script *script,
215 struct grub2_statements *stmts)
217 struct grub2_statement *stmt;
220 list_for_each_entry(&stmts->list, stmt, list) {
222 rc = stmt->exec(script, stmt);
227 int statement_simple_execute(struct grub2_script *script,
228 struct grub2_statement *statement)
230 struct grub2_statement_simple *st = to_stmt_simple(statement);
231 struct grub2_symtab_entry *entry;
237 process_expansions(script, st->argv);
242 entry = script_lookup_function(script, st->argv->argv[0]);
244 fprintf(stderr, "undefined function '%s'\n", st->argv->argv[0]);
248 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
253 int statement_if_execute(struct grub2_script *script,
254 struct grub2_statement *statement)
256 struct grub2_statement_if *st = to_stmt_if(statement);
257 struct grub2_statements *case_stmts;
260 rc = st->condition->exec(script, st->condition);
263 case_stmts = st->true_case;
265 case_stmts = st->false_case;
268 statements_execute(script, case_stmts);
275 int statement_menuentry_execute(struct grub2_script *script,
276 struct grub2_statement *statement)
278 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
279 struct discover_boot_option *opt;
281 process_expansions(script, st->argv);
283 opt = discover_boot_option_create(script->ctx, script->ctx->device);
284 if (st->argv->argc > 0) {
285 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
287 opt->option->name = talloc_strdup(opt, "(unknown)");
292 statements_execute(script, st->statements);
294 discover_context_add_boot_option(script->ctx, opt);
300 static void init_env(struct grub2_script *script)
302 struct env_entry *env;
304 list_init(&script->environment);
306 env = talloc(script, struct env_entry);
307 env->name = talloc_strdup(env, "prefix");
308 env->value = talloc_strdup(env, "/");
310 list_add(&script->environment, &env->list);
313 void script_register_function(struct grub2_script *script,
314 const char *name, grub2_function fn,
317 struct grub2_symtab_entry *entry;
319 entry = talloc(script, struct grub2_symtab_entry);
323 list_add(&script->symtab, &entry->list);
327 void script_execute(struct grub2_script *script)
329 statements_execute(script, script->statements);
332 struct grub2_script *create_script(struct grub2_parser *parser,
333 struct discover_context *ctx)
335 struct grub2_script *script;
337 script = talloc(parser, struct grub2_script);
343 list_init(&script->symtab);
344 register_builtins(script);