]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
8b008c4b91e919172858ec9864b3b6f23dff763c
[petitboot] / discover / grub2 / script.c
1
2 #include <sys/types.h>
3 #include <string.h>
4
5 #include <types/types.h>
6 #include <talloc/talloc.h>
7
8 #include "grub2.h"
9
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
17 struct env_entry {
18         const char              *name;
19         const char              *value;
20         struct list_item        list;
21 };
22
23 const char *script_env_get(struct grub2_script *script, const char *name)
24 {
25         struct env_entry *entry;
26
27         list_for_each_entry(&script->environment, entry, list)
28                 if (!strcmp(entry->name, name))
29                         return entry->value;
30
31         return NULL;
32 }
33
34 void script_env_set(struct grub2_script *script,
35                 const char *name, const char *value)
36 {
37         struct env_entry *e, *entry = NULL;
38
39         list_for_each_entry(&script->environment, e, list) {
40                 if (!strcmp(e->name, name)) {
41                         entry = e;
42                         break;
43                 }
44         }
45
46         if (!entry) {
47                 entry = talloc(script, struct env_entry);
48                 entry->name = name;
49                 list_add(&script->environment, &entry->list);
50         }
51
52         entry->value = value;
53 }
54
55 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
56 {
57         const char *val;
58
59         val = script_env_get(script, word->name);
60         if (!val)
61                 val = "";
62
63         word->text = talloc_strdup(script, val);
64
65         return true;
66 }
67
68 static bool is_delim(char c)
69 {
70         return c == ' ' || c == '\t';
71 }
72
73 /* For non-double-quoted variable expansions, we may need to split the
74  * variable's value into multiple argv items.
75  *
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.
80  */
81 static void process_split(struct grub2_script *script,
82                 struct grub2_word *top_word,
83                 struct grub2_word *word)
84 {
85         int i, len, delim_start = -1, delim_end = -1;
86         struct grub2_word *new_word;
87         char *remainder;
88
89         len = strlen(word->text);
90
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)
96                                 delim_start = i;
97                 } else if (delim_start != -1) {
98                         delim_end = i;
99                         break;
100                 }
101         }
102
103         /* No delim? nothing to do. The process_expansions loop will
104          * append this word's text to the top word, if necessary
105          */
106         if (delim_start == -1)
107                 return;
108
109         /* Set this word's text value to the text before the delimiter.
110          * this will get appended to the top word
111          */
112         word->text[delim_start] = '\0';
113
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) {
117                 if (!word->next)
118                         return;
119                 remainder = "";
120         } else {
121                 remainder = word->text + delim_end;
122         }
123
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);
131
132         /* stitch it into the argv list before this word */
133         list_insert_after(&top_word->argv_list,
134                            &new_word->argv_list);
135
136         /* terminate this word */
137         word->next = NULL;
138 }
139
140 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
141  * expansions.
142  *
143  * Once that's done, we may (if split == true) have to split the word to create
144  * new argv items
145  */
146 static void process_expansions(struct grub2_script *script,
147                 struct grub2_argv *argv)
148 {
149         struct grub2_word *top_word, *word;
150         int i;
151
152         argv->argc = 0;
153
154         list_for_each_entry(&argv->words, top_word, argv_list) {
155                 argv->argc++;
156
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) {
160
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;
165                         }
166
167                         /* split; this will potentially insert argv
168                          * entries after top_word. */
169                         if (word->split)
170                                 process_split(script, top_word, word);
171
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(
177                                                         top_word->text,
178                                                         "%s", word->text);
179                         }
180
181
182                 }
183                 top_word->next = NULL;
184         }
185
186         /* convert the list to an argv array, to pass to the function */
187         argv->argv = talloc_array(script, char *, argv->argc);
188         i = 0;
189
190         list_for_each_entry(&argv->words, word, argv_list)
191                 argv->argv[i++] = word->text;
192 }
193
194 static int statements_execute(struct grub2_script *script,
195                 struct grub2_statements *stmts)
196 {
197         struct grub2_statement *stmt;
198         int rc = 0;
199
200         list_for_each_entry(&stmts->list, stmt, list) {
201                 if (stmt->exec)
202                         rc = stmt->exec(script, stmt);
203         }
204         return rc;
205 }
206
207 int statement_simple_execute(struct grub2_script *script,
208                 struct grub2_statement *statement)
209 {
210         struct grub2_statement_simple *st = to_stmt_simple(statement);
211         struct grub2_command *cmd;
212         int rc;
213
214         if (!st->argv)
215                 return 0;
216
217         process_expansions(script, st->argv);
218
219         if (!st->argv->argc)
220                 return 0;
221
222         cmd = script_lookup_command(script, st->argv->argv[0]);
223         if (!cmd) {
224                 fprintf(stderr, "undefined command '%s'\n", st->argv->argv[0]);
225                 return 0;
226         }
227
228         rc = cmd->exec(script, st->argv->argc, st->argv->argv);
229
230         return rc;
231 }
232
233 int statement_if_execute(struct grub2_script *script,
234                 struct grub2_statement *statement)
235 {
236         struct grub2_statement_if *st = to_stmt_if(statement);
237         struct grub2_statements *case_stmts;
238         int rc;
239
240         rc = st->condition->exec(script, st->condition);
241
242         if (rc == 0)
243                 case_stmts = st->true_case;
244         else
245                 case_stmts = st->false_case;
246
247         if (case_stmts)
248                 statements_execute(script, case_stmts);
249         else
250                 rc = 0;
251
252         return rc;
253 }
254
255 int statement_menuentry_execute(struct grub2_script *script,
256                 struct grub2_statement *statement)
257 {
258         struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
259         struct discover_boot_option *opt;
260
261         process_expansions(script, st->argv);
262
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]);
266         } else {
267                 opt->option->name = talloc_strdup(opt, "(unknown)");
268         }
269
270         script->opt = opt;
271
272         statements_execute(script, st->statements);
273
274         discover_context_add_boot_option(script->ctx, opt);
275         script->opt = NULL;
276
277         return 0;
278 }
279
280 static void init_env(struct grub2_script *script)
281 {
282         struct env_entry *env;
283
284         list_init(&script->environment);
285
286         env = talloc(script, struct env_entry);
287         env->name = talloc_strdup(env, "prefix");
288         env->value = talloc_strdup(env, "/");
289
290         list_add(&script->environment, &env->list);
291 }
292
293 struct grub2_command *script_lookup_command(struct grub2_script *script,
294                 const char *name)
295 {
296         struct grub2_command *command;
297
298         list_for_each_entry(&script->commands, command, list) {
299                 if (!strcmp(command->name, name))
300                         return command;
301         }
302
303         return NULL;
304 }
305
306 void script_register_command(struct grub2_script *script,
307                 struct grub2_command *command)
308 {
309         list_add(&script->commands, &command->list);
310 }
311
312
313 void script_execute(struct grub2_script *script)
314 {
315         statements_execute(script, script->statements);
316 }
317
318 struct grub2_script *create_script(struct grub2_parser *parser,
319                 struct discover_context *ctx)
320 {
321         struct grub2_script *script;
322
323         script = talloc(parser, struct grub2_script);
324
325         init_env(script);
326         script->ctx = ctx;
327         script->opt = NULL;
328
329         list_init(&script->commands);
330         register_builtins(script);
331
332         return script;
333 }
334