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)
18 struct list_item list;
21 static const char *env_lookup(struct grub2_script *script,
22 const char *name, int name_len)
24 struct env_entry *entry;
27 str = talloc_strndup(script, name, name_len);
28 printf("%s: %s\n", __func__, str);
30 list_for_each_entry(&script->environment, entry, list)
31 if (!strncmp(entry->name, name, name_len))
37 static bool expand_word(struct grub2_script *script, struct grub2_word *word)
39 const char *val, *src;
46 n = regexec(&script->var_re, src, 1, &match, 0);
47 printf("%s %s: %d\n", __func__, word->text, n);
51 val = env_lookup(script, src + match.rm_so,
52 match.rm_eo - match.rm_so);
56 dest = talloc_strndup(script, src, match.rm_so);
57 dest = talloc_asprintf_append(dest, "%s%s", val, src + match.rm_eo);
63 /* iterate through the words in an argv, looking for expansions. If a
64 * word is marked with expand == true, then we process any variable
67 * Once that's done, we may (if split == true) have to split the word to create
70 static void process_expansions(struct grub2_script *script,
71 struct grub2_argv *argv)
73 struct grub2_word *word;
75 list_for_each_entry(&argv->words, word, argv_list) {
79 expand_word(script, word);
83 int statements_execute(struct grub2_script *script,
84 struct grub2_statements *stmts)
86 struct grub2_statement *stmt;
89 list_for_each_entry(&stmts->list, stmt, list) {
91 rc = stmt->exec(script, stmt);
92 printf("%s(%p)\n", __func__, stmt);
97 int statement_simple_execute(struct grub2_script *script,
98 struct grub2_statement *statement)
100 struct grub2_statement_simple *st = to_stmt_simple(statement);
105 process_expansions(script, st->argv);
110 int statement_if_execute(struct grub2_script *script,
111 struct grub2_statement *statement)
113 struct grub2_statement_if *st = to_stmt_if(statement);
114 struct grub2_statements *case_stmts;
117 rc = st->condition->exec(script, st->condition);
120 case_stmts = st->true_case;
122 case_stmts = st->false_case;
125 statements_execute(script, case_stmts);
133 void script_execute(struct grub2_script *script)
135 statements_execute(script, script->statements);
138 static int script_destroy(void *p)
140 struct grub2_script *script = p;
141 regfree(&script->var_re);
145 struct grub2_script *create_script(void *ctx)
147 struct grub2_script *script;
150 script = talloc(ctx, struct grub2_script);
152 rc = regcomp(&script->var_re,
153 "\\$\\{?([[:alpha:]][_[:alnum:]]*|[0-9]|[\\?@\\*#])\\}?",
157 regerror(rc, &script->var_re, err, sizeof(err));
158 fprintf(stderr, "RE error %d: %s\n", rc, err);
163 talloc_set_destructor(script, script_destroy);
165 list_init(&script->environment);