5 #include <talloc/talloc.h>
9 #define to_stmt_simple(stmt) \
10 container_of(stmt, struct grub2_statement_simple, st)
11 #define to_stmt_if(stmt) \
12 container_of(stmt, struct grub2_statement_if, st)
13 #define to_stmt_menuentry(stmt) \
14 container_of(stmt, struct grub2_statement_menuentry, st)
19 struct list_item list;
22 const char *script_env_get(struct grub2_script *script, const char *name)
24 struct env_entry *entry;
26 list_for_each_entry(&script->environment, entry, list)
27 if (!strcmp(entry->name, name))
33 void script_env_set(struct grub2_script *script,
34 const char *name, const char *value)
36 struct env_entry *e, *entry = NULL;
38 list_for_each_entry(&script->environment, e, list) {
39 if (!strcmp(e->name, name)) {
46 entry = talloc(script, struct env_entry);
48 list_add(&script->environment, &entry->list);
54 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
58 val = script_env_get(script, word->name);
62 word->text = talloc_strdup(script, val);
67 static bool is_delim(char c)
69 return c == ' ' || c == '\t';
72 /* For non-double-quoted variable expansions, we may need to split the
73 * variable's value into multiple argv items.
75 * This function sets the word->text to the initial set of non-delimiter chars
76 * in the expanded var value. We then skip any delimiter chars, and (if
77 * required), create the new argv item with the remaining text, and
78 * add it to the argv list, after top_word.
80 static void process_split(struct grub2_script *script,
81 struct grub2_word *top_word,
82 struct grub2_word *word)
84 int i, len, delim_start = -1, delim_end = -1;
85 struct grub2_word *new_word;
88 len = strlen(word->text);
90 /* Scan our string for the start of a delim (delim_start), and the
91 * start of any new text (delim_end). */
92 for (i = 0; i < len; i++) {
93 if (is_delim(word->text[i])) {
94 if (delim_start == -1)
96 } else if (delim_start != -1) {
102 /* No delim? nothing to do. The process_expansions loop will
103 * append this word's text to the top word, if necessary
105 if (delim_start == -1)
108 /* Set this word's text value to the text before the delimiter.
109 * this will get appended to the top word
111 word->text[delim_start] = '\0';
113 /* No trailing text? If there are no following word tokens, we're done.
114 * Otherwise, we need to start a new argv item with those tokens */
115 if (delim_end == -1) {
120 remainder = word->text + delim_end;
123 new_word = talloc(script, struct grub2_word);
124 new_word->type = GRUB2_WORD_TEXT;
125 /* if there's no trailing text, we know we don't need to re-split */
126 new_word->split = delim_end != -1;
127 new_word->next = word->next;
128 new_word->last = NULL;
129 new_word->text = talloc_strdup(new_word, remainder);
131 /* stitch it into the argv list before this word */
132 list_insert_after(&top_word->argv_list,
133 &new_word->argv_list);
135 /* terminate this word */
139 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
142 * Once that's done, we may (if split == true) have to split the word to create
145 static void process_expansions(struct grub2_script *script,
146 struct grub2_argv *argv)
148 struct grub2_word *top_word, *word;
153 list_for_each_entry(&argv->words, top_word, argv_list) {
156 /* expand vars and squash the list of words into the head
157 * of the argv word list */
158 for (word = top_word; word; word = word->next) {
160 /* if it's a variable, perform the substitution */
161 if (word->type == GRUB2_WORD_VAR) {
162 expand_var(script, word);
163 word->type = GRUB2_WORD_TEXT;
166 /* split; this will potentially insert argv
167 * entries after top_word. */
169 process_split(script, top_word, word);
171 /* accumulate word text into the top word, so
172 * we end up with a shallow tree of argv data */
173 /* todo: don't do this in process_split */
174 if (word != top_word) {
175 top_word->text = talloc_asprintf_append(
182 top_word->next = NULL;
185 /* convert the list to an argv array, to pass to the function */
186 argv->argv = talloc_array(script, char *, argv->argc);
189 list_for_each_entry(&argv->words, word, argv_list)
190 argv->argv[i++] = word->text;
193 static int statements_execute(struct grub2_script *script,
194 struct grub2_statements *stmts)
196 struct grub2_statement *stmt;
199 list_for_each_entry(&stmts->list, stmt, list) {
201 rc = stmt->exec(script, stmt);
206 int statement_simple_execute(struct grub2_script *script,
207 struct grub2_statement *statement)
209 struct grub2_statement_simple *st = to_stmt_simple(statement);
210 struct grub2_command *cmd;
216 process_expansions(script, st->argv);
221 cmd = script_lookup_command(script, st->argv->argv[0]);
223 fprintf(stderr, "undefined command '%s'\n", st->argv->argv[0]);
227 rc = cmd->exec(script, st->argv->argc, st->argv->argv);
232 int statement_if_execute(struct grub2_script *script,
233 struct grub2_statement *statement)
235 struct grub2_statement_if *st = to_stmt_if(statement);
236 struct grub2_statements *case_stmts;
239 rc = st->condition->exec(script, st->condition);
242 case_stmts = st->true_case;
244 case_stmts = st->false_case;
247 statements_execute(script, case_stmts);
254 int statement_menuentry_execute(struct grub2_script *script,
255 struct grub2_statement *statement)
257 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
259 process_expansions(script, st->argv);
260 statements_execute(script, st->statements);
265 static void init_env(struct grub2_script *script)
267 struct env_entry *env;
269 list_init(&script->environment);
271 env = talloc(script, struct env_entry);
272 env->name = talloc_strdup(env, "prefix");
273 env->value = talloc_strdup(env, "/");
275 list_add(&script->environment, &env->list);
278 struct grub2_command *script_lookup_command(struct grub2_script *script,
281 struct grub2_command *command;
283 list_for_each_entry(&script->commands, command, list) {
284 if (!strcmp(command->name, name))
291 void script_register_command(struct grub2_script *script,
292 struct grub2_command *command)
294 list_add(&script->commands, &command->list);
298 void script_execute(struct grub2_script *script)
300 statements_execute(script, script->statements);
303 struct grub2_script *create_script(struct grub2_parser *parser,
304 struct discover_context *ctx)
306 struct grub2_script *script;
308 script = talloc(parser, struct grub2_script);
313 list_init(&script->commands);
314 register_builtins(script);