]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
discover/grub2: Initial environment handling
[petitboot] / discover / grub2 / script.c
1
2 #include <sys/types.h>
3 #include <regex.h>
4 #include <string.h>
5
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
15 struct env_entry {
16         const char              *name;
17         const char              *value;
18         struct list_item        list;
19 };
20
21 static const char *env_lookup(struct grub2_script *script,
22                 const char *name, int name_len)
23 {
24         struct env_entry *entry;
25
26         printf("%s: %.*s\n", __func__, name_len, name);
27
28         list_for_each_entry(&script->environment, entry, list)
29                 if (!strncmp(entry->name, name, name_len)
30                                 && entry->name[name_len] == '\0')
31                         return entry->value;
32
33         return NULL;
34 }
35
36 static bool expand_word(struct grub2_script *script, struct grub2_word *word)
37 {
38         const char *val, *src;
39         char *dest = NULL;
40         regmatch_t match;
41         int n, i;
42
43         src = word->text;
44
45         n = regexec(&script->var_re, src, 1, &match, 0);
46         printf("%s %s: %d\n", __func__, word->text, n);
47         if (n != 0)
48                 return false;
49
50         i = 0;
51         if (src[match.rm_so + 1] == '{')
52                 i++;
53
54         val = env_lookup(script, src + match.rm_so + 1 + i,
55                                  match.rm_eo - match.rm_so - 1 - (i * 2));
56         if (!val)
57                 val = "";
58
59         printf("repl: %s\n", val);
60
61         dest = talloc_strndup(script, src, match.rm_so);
62         dest = talloc_asprintf_append(dest, "%s%s", val, src + match.rm_eo);
63
64         word->text = dest;
65         return true;
66 }
67
68 /* iterate through the words in an argv, looking for expansions. If a
69  * word is marked with expand == true, then we process any variable
70  * substitutions.
71  *
72  * Once that's done, we may (if split == true) have to split the word to create
73  * new argv items
74  */
75 static void process_expansions(struct grub2_script *script,
76                 struct grub2_argv *argv)
77 {
78         struct grub2_word *word;
79
80         list_for_each_entry(&argv->words, word, argv_list) {
81                 if (!word->expand)
82                         continue;
83
84                 expand_word(script, word);
85         }
86 }
87
88 int statements_execute(struct grub2_script *script,
89                 struct grub2_statements *stmts)
90 {
91         struct grub2_statement *stmt;
92         int rc = 0;
93
94         list_for_each_entry(&stmts->list, stmt, list) {
95                 if (stmt->exec)
96                         rc = stmt->exec(script, stmt);
97         }
98         return rc;
99 }
100
101 int statement_simple_execute(struct grub2_script *script,
102                 struct grub2_statement *statement)
103 {
104         struct grub2_statement_simple *st = to_stmt_simple(statement);
105
106         if (!st->argv)
107                 return 0;
108
109         process_expansions(script, st->argv);
110
111         return 0;
112 }
113
114 int statement_if_execute(struct grub2_script *script,
115                 struct grub2_statement *statement)
116 {
117         struct grub2_statement_if *st = to_stmt_if(statement);
118         struct grub2_statements *case_stmts;
119         int rc;
120
121         rc = st->condition->exec(script, st->condition);
122
123         if (rc == 0)
124                 case_stmts = st->true_case;
125         else
126                 case_stmts = st->false_case;
127
128         if (case_stmts)
129                 statements_execute(script, case_stmts);
130         else
131                 rc = 0;
132
133         return rc;
134 }
135
136 static void init_env(struct grub2_script *script)
137 {
138         struct env_entry *env;
139
140         list_init(&script->environment);
141
142         env = talloc(script, struct env_entry);
143         env->name = talloc_strdup(env, "prefix");
144         env->value = talloc_strdup(env, "/");
145
146         list_add(&script->environment, &env->list);
147 }
148
149
150 void script_execute(struct grub2_script *script)
151 {
152         statements_execute(script, script->statements);
153 }
154
155 static int script_destroy(void *p)
156 {
157         struct grub2_script *script = p;
158         regfree(&script->var_re);
159         return 0;
160 }
161
162 struct grub2_script *create_script(void *ctx)
163 {
164         struct grub2_script *script;
165         int rc;
166
167         script = talloc(ctx, struct grub2_script);
168
169         rc = regcomp(&script->var_re,
170                 "\\$\\{?([[:alpha:]][_[:alnum:]]*|[0-9]|[\\?@\\*#])\\}?",
171                         REG_EXTENDED);
172         if (rc) {
173                 char err[200];
174                 regerror(rc, &script->var_re, err, sizeof(err));
175                 fprintf(stderr, "RE error %d: %s\n", rc, err);
176                 talloc_free(script);
177                 return NULL;
178
179         }
180         talloc_set_destructor(script, script_destroy);
181
182         init_env(script);
183
184         return script;
185 }
186