]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
discover/grub2: Add initial command execution
[petitboot] / discover / grub2 / script.c
1
2 #include <sys/types.h>
3 #include <string.h>
4
5 #include <talloc/talloc.h>
6
7 #include "grub2.h"
8
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)
15
16 struct env_entry {
17         const char              *name;
18         const char              *value;
19         struct list_item        list;
20 };
21
22 const char *script_env_get(struct grub2_script *script, const char *name)
23 {
24         struct env_entry *entry;
25
26         list_for_each_entry(&script->environment, entry, list)
27                 if (!strcmp(entry->name, name))
28                         return entry->value;
29
30         return NULL;
31 }
32
33 void script_env_set(struct grub2_script *script,
34                 const char *name, const char *value)
35 {
36         struct env_entry *e, *entry = NULL;
37
38         list_for_each_entry(&script->environment, e, list) {
39                 if (!strcmp(e->name, name)) {
40                         entry = e;
41                         break;
42                 }
43         }
44
45         if (!entry) {
46                 entry = talloc(script, struct env_entry);
47                 entry->name = name;
48                 list_add(&script->environment, &entry->list);
49         }
50
51         entry->value = value;
52 }
53
54 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
55 {
56         const char *val;
57
58         val = script_env_get(script, word->var.name);
59         if (!val)
60                 val = "";
61
62         word->type = GRUB2_WORD_TEXT;
63         word->text = talloc_strdup(script, val);
64
65         return true;
66 }
67
68 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
69  * expansions.
70  *
71  * Once that's done, we may (if split == true) have to split the word to create
72  * new argv items
73  */
74 static void process_expansions(struct grub2_script *script,
75                 struct grub2_argv *argv)
76 {
77         struct grub2_word *top_word, *word;
78         int i;
79
80         argv->argc = 0;
81
82         list_for_each_entry(&argv->words, top_word, argv_list) {
83                 argv->argc++;
84
85                 /* expand vars and squash the list of words into the top struct.
86                  * todo: splitting
87                  */
88                 for (word = top_word; word; word = word->next) {
89                         if (word->type == GRUB2_WORD_VAR)
90                                 expand_var(script, word);
91
92                         if (word == top_word)
93                                 continue;
94
95                         top_word->text = talloc_asprintf_append(top_word->text,
96                                         "%s", word->text);
97                 }
98                 top_word->next = NULL;
99         }
100
101         argv->argv = talloc_array(script, char *, argv->argc);
102         i = 0;
103
104         list_for_each_entry(&argv->words, word, argv_list)
105                 argv->argv[i++] = word->text;
106 }
107
108 int statements_execute(struct grub2_script *script,
109                 struct grub2_statements *stmts)
110 {
111         struct grub2_statement *stmt;
112         int rc = 0;
113
114         list_for_each_entry(&stmts->list, stmt, list) {
115                 if (stmt->exec)
116                         rc = stmt->exec(script, stmt);
117         }
118         return rc;
119 }
120
121 int statement_simple_execute(struct grub2_script *script,
122                 struct grub2_statement *statement)
123 {
124         struct grub2_statement_simple *st = to_stmt_simple(statement);
125         struct grub2_command *cmd;
126         int rc;
127
128         if (!st->argv)
129                 return 0;
130
131         process_expansions(script, st->argv);
132
133         if (!st->argv->argc)
134                 return 0;
135
136         cmd = script_lookup_command(script, st->argv->argv[0]);
137         if (!cmd) {
138                 fprintf(stderr, "undefined command '%s'\n", st->argv->argv[0]);
139                 return 0;
140         }
141
142         rc = cmd->exec(script, st->argv->argc, st->argv->argv);
143
144         return rc;
145 }
146
147 int statement_if_execute(struct grub2_script *script,
148                 struct grub2_statement *statement)
149 {
150         struct grub2_statement_if *st = to_stmt_if(statement);
151         struct grub2_statements *case_stmts;
152         int rc;
153
154         rc = st->condition->exec(script, st->condition);
155
156         if (rc == 0)
157                 case_stmts = st->true_case;
158         else
159                 case_stmts = st->false_case;
160
161         if (case_stmts)
162                 statements_execute(script, case_stmts);
163         else
164                 rc = 0;
165
166         return rc;
167 }
168
169 int statement_menuentry_execute(struct grub2_script *script,
170                 struct grub2_statement *statement)
171 {
172         struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
173
174         process_expansions(script, st->argv);
175         statements_execute(script, st->statements);
176
177         return 0;
178 }
179
180 static void init_env(struct grub2_script *script)
181 {
182         struct env_entry *env;
183
184         list_init(&script->environment);
185
186         env = talloc(script, struct env_entry);
187         env->name = talloc_strdup(env, "prefix");
188         env->value = talloc_strdup(env, "/");
189
190         list_add(&script->environment, &env->list);
191 }
192
193 struct grub2_command *script_lookup_command(struct grub2_script *script,
194                 const char *name)
195 {
196         struct grub2_command *command;
197
198         list_for_each_entry(&script->commands, command, list) {
199                 if (!strcmp(command->name, name))
200                         return command;
201         }
202
203         return NULL;
204 }
205
206 void script_register_command(struct grub2_script *script,
207                 struct grub2_command *command)
208 {
209         list_add(&script->commands, &command->list);
210 }
211
212
213 void script_execute(struct grub2_script *script)
214 {
215         statements_execute(script, script->statements);
216 }
217
218 struct grub2_script *create_script(void *ctx)
219 {
220         struct grub2_script *script;
221
222         script = talloc(ctx, struct grub2_script);
223
224         init_env(script);
225         list_init(&script->commands);
226         register_builtins(script);
227
228         return script;
229 }
230