discover/grub2: Handle var tokens in lexer
[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
14 struct env_entry {
15         const char              *name;
16         const char              *value;
17         struct list_item        list;
18 };
19
20 static const char *env_lookup(struct grub2_script *script,
21                 const char *name)
22 {
23         struct env_entry *entry;
24
25         list_for_each_entry(&script->environment, entry, list)
26                 if (!strcmp(entry->name, name))
27                         return entry->value;
28
29         return NULL;
30 }
31
32 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
33 {
34         const char *val;
35
36         val = env_lookup(script, word->var.name);
37         if (!val)
38                 val = "";
39
40         word->type = GRUB2_WORD_TEXT;
41         word->text = talloc_strdup(script, val);
42
43         return true;
44 }
45
46 /* iterate through the words in an argv, looking for GRUB2_WORD_VAR expansions.
47  *
48  * Once that's done, we may (if split == true) have to split the word to create
49  * new argv items
50  */
51 static void process_expansions(struct grub2_script *script,
52                 struct grub2_argv *argv)
53 {
54         struct grub2_word *top_word, *word;
55
56         list_for_each_entry(&argv->words, top_word, argv_list) {
57                 /* expand vars and squash the list of words into the top struct.
58                  * todo: splitting
59                  */
60                 for (word = top_word; word; word = word->next) {
61                         if (word->type == GRUB2_WORD_VAR)
62                                 expand_var(script, word);
63
64                         if (word == top_word)
65                                 continue;
66
67                         top_word->text = talloc_asprintf_append(top_word->text,
68                                         "%s", word->text);
69                 }
70                 top_word->next = NULL;
71         }
72 }
73
74 int statements_execute(struct grub2_script *script,
75                 struct grub2_statements *stmts)
76 {
77         struct grub2_statement *stmt;
78         int rc = 0;
79
80         list_for_each_entry(&stmts->list, stmt, list) {
81                 if (stmt->exec)
82                         rc = stmt->exec(script, stmt);
83         }
84         return rc;
85 }
86
87 int statement_simple_execute(struct grub2_script *script,
88                 struct grub2_statement *statement)
89 {
90         struct grub2_statement_simple *st = to_stmt_simple(statement);
91
92         if (!st->argv)
93                 return 0;
94
95         process_expansions(script, st->argv);
96
97         return 0;
98 }
99
100 int statement_if_execute(struct grub2_script *script,
101                 struct grub2_statement *statement)
102 {
103         struct grub2_statement_if *st = to_stmt_if(statement);
104         struct grub2_statements *case_stmts;
105         int rc;
106
107         rc = st->condition->exec(script, st->condition);
108
109         if (rc == 0)
110                 case_stmts = st->true_case;
111         else
112                 case_stmts = st->false_case;
113
114         if (case_stmts)
115                 statements_execute(script, case_stmts);
116         else
117                 rc = 0;
118
119         return rc;
120 }
121
122 static void init_env(struct grub2_script *script)
123 {
124         struct env_entry *env;
125
126         list_init(&script->environment);
127
128         env = talloc(script, struct env_entry);
129         env->name = talloc_strdup(env, "prefix");
130         env->value = talloc_strdup(env, "/");
131
132         list_add(&script->environment, &env->list);
133 }
134
135
136 void script_execute(struct grub2_script *script)
137 {
138         statements_execute(script, script->statements);
139 }
140
141 struct grub2_script *create_script(void *ctx)
142 {
143         struct grub2_script *script;
144
145         script = talloc(ctx, struct grub2_script);
146
147         init_env(script);
148
149         return script;
150 }
151