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 const char *script_env_get(struct grub2_script *script, const char *name)
25 struct env_entry *entry;
27 list_for_each_entry(&script->environment, entry, list)
28 if (!strcmp(entry->name, name))
34 void script_env_set(struct grub2_script *script,
35 const char *name, const char *value)
37 struct env_entry *e, *entry = NULL;
39 list_for_each_entry(&script->environment, e, list) {
40 if (!strcmp(e->name, name)) {
47 entry = talloc(script, struct env_entry);
49 list_add(&script->environment, &entry->list);
55 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
59 val = script_env_get(script, word->name);
63 word->text = talloc_strdup(script, val);
68 static bool is_delim(char c)
70 return c == ' ' || c == '\t';
73 /* For non-double-quoted variable expansions, we may need to split the
74 * variable's value into multiple argv items.
76 * This function sets the word->text to the initial set of non-delimiter chars
77 * in the expanded var value. We then skip any delimiter chars, and (if
78 * required), create the new argv item with the remaining text, and
79 * add it to the argv list, after top_word.
81 static void process_split(struct grub2_script *script,
82 struct grub2_word *top_word,
83 struct grub2_word *word)
85 int i, len, delim_start = -1, delim_end = -1;
86 struct grub2_word *new_word;
89 len = strlen(word->text);
91 /* Scan our string for the start of a delim (delim_start), and the
92 * start of any new text (delim_end). */
93 for (i = 0; i < len; i++) {
94 if (is_delim(word->text[i])) {
95 if (delim_start == -1)
97 } else if (delim_start != -1) {
103 /* No delim? nothing to do. The process_expansions loop will
104 * append this word's text to the top word, if necessary
106 if (delim_start == -1)
109 /* Set this word's text value to the text before the delimiter.
110 * this will get appended to the top word
112 word->text[delim_start] = '\0';
114 /* No trailing text? If there are no following word tokens, we're done.
115 * Otherwise, we need to start a new argv item with those tokens */
116 if (delim_end == -1) {
121 remainder = word->text + delim_end;
124 new_word = talloc(script, struct grub2_word);
125 new_word->type = GRUB2_WORD_TEXT;
126 /* if there's no trailing text, we know we don't need to re-split */
127 new_word->split = delim_end != -1;
128 new_word->next = word->next;
129 new_word->last = NULL;
130 new_word->text = talloc_strdup(new_word, remainder);
132 /* stitch it into the argv list before this word */
133 list_insert_after(&top_word->argv_list,
134 &new_word->argv_list);
136 /* terminate this word */
140 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
143 * Once that's done, we may (if split == true) have to split the word to create
146 static void process_expansions(struct grub2_script *script,
147 struct grub2_argv *argv)
149 struct grub2_word *top_word, *word;
154 list_for_each_entry(&argv->words, top_word, argv_list) {
157 /* expand vars and squash the list of words into the head
158 * of the argv word list */
159 for (word = top_word; word; word = word->next) {
161 /* if it's a variable, perform the substitution */
162 if (word->type == GRUB2_WORD_VAR) {
163 expand_var(script, word);
164 word->type = GRUB2_WORD_TEXT;
167 /* split; this will potentially insert argv
168 * entries after top_word. */
170 process_split(script, top_word, word);
172 /* accumulate word text into the top word, so
173 * we end up with a shallow tree of argv data */
174 /* todo: don't do this in process_split */
175 if (word != top_word) {
176 top_word->text = talloc_asprintf_append(
183 top_word->next = NULL;
186 /* convert the list to an argv array, to pass to the function */
187 argv->argv = talloc_array(script, char *, argv->argc);
190 list_for_each_entry(&argv->words, word, argv_list)
191 argv->argv[i++] = word->text;
194 static int statements_execute(struct grub2_script *script,
195 struct grub2_statements *stmts)
197 struct grub2_statement *stmt;
200 list_for_each_entry(&stmts->list, stmt, list) {
202 rc = stmt->exec(script, stmt);
207 int statement_simple_execute(struct grub2_script *script,
208 struct grub2_statement *statement)
210 struct grub2_statement_simple *st = to_stmt_simple(statement);
211 struct grub2_command *cmd;
217 process_expansions(script, st->argv);
222 cmd = script_lookup_command(script, st->argv->argv[0]);
224 fprintf(stderr, "undefined command '%s'\n", st->argv->argv[0]);
228 rc = cmd->exec(script, st->argv->argc, st->argv->argv);
233 int statement_if_execute(struct grub2_script *script,
234 struct grub2_statement *statement)
236 struct grub2_statement_if *st = to_stmt_if(statement);
237 struct grub2_statements *case_stmts;
240 rc = st->condition->exec(script, st->condition);
243 case_stmts = st->true_case;
245 case_stmts = st->false_case;
248 statements_execute(script, case_stmts);
255 int statement_menuentry_execute(struct grub2_script *script,
256 struct grub2_statement *statement)
258 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
259 struct discover_boot_option *opt;
261 process_expansions(script, st->argv);
263 opt = discover_boot_option_create(script->ctx, script->ctx->device);
264 if (st->argv->argc > 0) {
265 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
267 opt->option->name = talloc_strdup(opt, "(unknown)");
272 statements_execute(script, st->statements);
274 discover_context_add_boot_option(script->ctx, opt);
280 static void init_env(struct grub2_script *script)
282 struct env_entry *env;
284 list_init(&script->environment);
286 env = talloc(script, struct env_entry);
287 env->name = talloc_strdup(env, "prefix");
288 env->value = talloc_strdup(env, "/");
290 list_add(&script->environment, &env->list);
293 struct grub2_command *script_lookup_command(struct grub2_script *script,
296 struct grub2_command *command;
298 list_for_each_entry(&script->commands, command, list) {
299 if (!strcmp(command->name, name))
306 void script_register_command(struct grub2_script *script,
307 struct grub2_command *command)
309 list_add(&script->commands, &command->list);
313 void script_execute(struct grub2_script *script)
315 statements_execute(script, script->statements);
318 struct grub2_script *create_script(struct grub2_parser *parser,
319 struct discover_context *ctx)
321 struct grub2_script *script;
323 script = talloc(parser, struct grub2_script);
329 list_init(&script->commands);
330 register_builtins(script);