grub2/grub2-parser: accept no whitespace in grub menuentry
[petitboot] / discover / grub2 / grub2-parser.y
1
2 %pure-parser
3 %lex-param { yyscan_t scanner }
4 %parse-param { struct grub2_parser *parser }
5 %parse-param { void *scanner }
6 %error-verbose
7
8 %{
9 #include <talloc/talloc.h>
10 #include <log/log.h>
11
12 #include "grub2.h"
13
14 void yyerror(struct grub2_parser *parser, void *scanner, const char *fmt, ...);
15 %}
16
17 %union {
18         struct grub2_word       *word;
19         struct grub2_argv       *argv;
20         struct grub2_statement  *statement;
21         struct grub2_statements *statements;
22 }
23
24 %printer { fprintf(yyoutput, "%s%s:'%s'",
25                 $$->type == GRUB2_WORD_VAR ? "var" : "text",
26                 $$->type == GRUB2_WORD_VAR && !$$->split ? "[nosplit]" : "",
27                 $$->name); } <word>
28
29 /* reserved words */
30 %token  TOKEN_LDSQBRACKET       "[["
31 %token  TOKEN_RDSQBRACKET       "]]"
32 %token  TOKEN_CASE              "case"
33 %token  TOKEN_DO                "do"
34 %token  TOKEN_DONE              "done"
35 %token  TOKEN_ELIF              "elif"
36 %token  TOKEN_ELSE              "else"
37 %token  TOKEN_ESAC              "esac"
38 %token  TOKEN_FI                "fi"
39 %token  TOKEN_FOR               "for"
40 %token  TOKEN_FUNCTION          "function"
41 %token  TOKEN_IF                "if"
42 %token  TOKEN_IN                "in"
43 %token  TOKEN_MENUENTRY         "menuentry"
44 %token  TOKEN_SELECT            "select"
45 %token  TOKEN_SUBMENU           "submenu"
46 %token  TOKEN_THEN              "then"
47 %token  TOKEN_TIME              "time"
48 %token  TOKEN_UTIL              "until"
49 %token  TOKEN_WHILE             "while"
50
51 %type <statement>       statement
52 %type <statements>      statements
53 %type <statement>       conditional
54 %type <statement>       elif
55 %type <statements>      elifs
56 %type <argv>            words
57 %type <word>            word
58
59 /* syntax */
60 %token  TOKEN_EOL
61 %token  TOKEN_DELIM
62 %token  <word> TOKEN_WORD
63 %token  TOKEN_EOF 0
64
65 %start  script
66 %debug
67
68 %{
69 #include "grub2-lexer.h"
70 %}
71
72 %%
73
74 script: statements {
75                 parser->script->statements = $1;
76         }
77
78 statements: statement {
79                 $$ = create_statements(parser);
80                 statement_append($$, $1);
81         }
82         | statements eol statement {
83                 statement_append($1, $3);
84                 $$ = $1;
85         }
86
87 conditional: statement eol "then" statements {
88                 $$ = create_statement_conditional(parser, $1, $4);
89         }
90
91 elif: "elif" conditional {
92                 $$ = $2;
93       }
94
95 elifs: /* empty */ {
96                 $$ = create_statements(parser);
97         }
98         | elifs elif {
99                 statement_append($1, $2);
100                 $$ = $1;
101         }
102
103 statement: {
104                    $$ = NULL;
105         }
106         | words delim0 {
107                    $$ = create_statement_simple(parser, $1);
108         }
109         | '{' statements '}' {
110                 $$ = create_statement_block(parser, $2);
111         }
112         | "if" conditional elifs "fi" {
113                 $$ = create_statement_if(parser, $2, $3, NULL);
114         }
115         | "if" conditional
116                 elifs
117                 "else"
118                 statements
119                 "fi" {
120                 $$ = create_statement_if(parser, $2, $3, $5);
121         }
122         | "function" word delim '{' statements '}' {
123                 $$ = create_statement_function(parser, $2, $5);
124         }
125         | "menuentry" words delim0
126                 '{' statements '}' {
127                 $$ = create_statement_menuentry(parser, $2, $5);
128         }
129         | "submenu" words delim
130                 '{' statements '}' {
131                 /* we just flatten everything */
132                 $$ = create_statement_block(parser, $5);
133         }
134         | "for" word delim "in" delim words eol
135                 "do"
136                 statements
137                 "done" {
138                 $$ = create_statement_for(parser, $2, $6, $9);
139         }
140
141 words:  word {
142                 $$ = create_argv(parser);
143                 argv_append($$, $1);
144         }
145         | words delim word {
146                 argv_append($1, $3);
147                 $$ = $1;
148         }
149
150 word:   TOKEN_WORD
151         | word TOKEN_WORD {
152                 word_append($1, $2);
153                 $$ = $1;
154         }
155
156 delim0: /* empty */ |
157         delim
158
159 delim:  TOKEN_DELIM |
160         delim TOKEN_DELIM
161
162 eol:    TOKEN_EOL;
163 %%
164 void yyerror(struct grub2_parser *parser, void *scanner, const char *fmt, ...)
165 {
166         const char *str;
167         va_list ap;
168
169         va_start(ap, fmt);
170         str = talloc_vasprintf(parser, fmt, ap);
171         va_end(ap);
172
173         pb_log("parse error: %d('%s'): %s\n", yyget_lineno(scanner),
174                                         yyget_text(scanner), str);
175 }
176
177 struct grub2_statements *create_statements(struct grub2_parser *parser)
178 {
179         struct grub2_statements *stmts = talloc(parser,
180                         struct grub2_statements);
181         list_init(&stmts->list);
182         return stmts;
183 }
184
185 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
186                 struct grub2_argv *argv)
187 {
188         struct grub2_statement_simple *stmt =
189                 talloc(parser, struct grub2_statement_simple);
190         stmt->st.type = STMT_TYPE_SIMPLE;
191         stmt->st.exec = statement_simple_execute;
192         stmt->argv = argv;
193         return &stmt->st;
194 }
195
196 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
197                 struct grub2_argv *argv, struct grub2_statements *stmts)
198 {
199         struct grub2_statement_menuentry *stmt =
200                 talloc(parser, struct grub2_statement_menuentry);
201         stmt->st.type = STMT_TYPE_MENUENTRY;
202         stmt->st.exec = statement_menuentry_execute;
203         stmt->argv = argv;
204         stmt->statements = stmts;
205         return &stmt->st;
206 }
207
208 struct grub2_statement *create_statement_conditional(
209                 struct grub2_parser *parser,
210                 struct grub2_statement *condition,
211                 struct grub2_statements *statements)
212 {
213         struct grub2_statement_conditional *stmt =
214                 talloc(parser, struct grub2_statement_conditional);
215         stmt->st.type = STMT_TYPE_CONDITIONAL;
216         stmt->condition = condition;
217         stmt->statements = statements;
218         return &stmt->st;
219 }
220
221 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
222                 struct grub2_statement *conditional,
223                 struct grub2_statements *elifs,
224                 struct grub2_statements *else_case)
225 {
226         struct grub2_statement_if *stmt =
227                 talloc(parser, struct grub2_statement_if);
228
229         list_add(&elifs->list, &conditional->list);
230
231         stmt->st.type = STMT_TYPE_IF;
232         stmt->st.exec = statement_if_execute;
233         stmt->conditionals = elifs;
234         stmt->else_case = else_case;
235         return &stmt->st;
236 }
237
238 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
239                 struct grub2_statements *stmts)
240 {
241         struct grub2_statement_block *stmt =
242                 talloc(parser, struct grub2_statement_block);
243         stmt->st.type = STMT_TYPE_BLOCK;
244         stmt->st.exec = statement_block_execute;
245         stmt->statements = stmts;
246         return &stmt->st;
247 }
248
249 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
250                 struct grub2_word *name, struct grub2_statements *body)
251 {
252         struct grub2_statement_function *stmt =
253                 talloc(parser, struct grub2_statement_function);
254         stmt->st.exec = statement_function_execute;
255         stmt->name = name;
256         stmt->body = body;
257         return &stmt->st;
258 }
259
260 struct grub2_statement *create_statement_for(struct grub2_parser *parser,
261                 struct grub2_word *var, struct grub2_argv *list,
262                 struct grub2_statements *body)
263 {
264         struct grub2_statement_for *stmt =
265                 talloc(parser, struct grub2_statement_for);
266         stmt->st.exec = statement_for_execute;
267         stmt->var = var;
268         stmt->list = list;
269         stmt->body = body;
270         return &stmt->st;
271 }
272
273 void statement_append(struct grub2_statements *stmts,
274                 struct grub2_statement *stmt)
275 {
276         if (stmt)
277                 list_add_tail(&stmts->list, &stmt->list);
278 }
279
280 struct grub2_word *create_word_text(struct grub2_parser *parser,
281                 const char *text)
282 {
283         struct grub2_word *word = talloc(parser, struct grub2_word);
284         word->type = GRUB2_WORD_TEXT;
285         word->split = false;
286         word->text = talloc_strdup(word, text);
287         word->next = NULL;
288         word->last = word;
289         return word;
290 }
291
292 struct grub2_word *create_word_var(struct grub2_parser *parser,
293                 const char *name, bool split)
294 {
295         struct grub2_word *word = talloc(parser, struct grub2_word);
296         word->type = GRUB2_WORD_VAR;
297         word->name = talloc_strdup(word, name);
298         word->split = split;
299         word->next = NULL;
300         word->last = word;
301         return word;
302 }
303
304 struct grub2_argv *create_argv(struct grub2_parser *parser)
305 {
306         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
307         list_init(&argv->words);
308         return argv;
309 }
310
311 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
312 {
313         list_add_tail(&argv->words, &word->argv_list);
314 }
315
316 void word_append(struct grub2_word *w1, struct grub2_word *w2)
317 {
318         w1->last->next = w2;
319         w1->last = w2;
320 }
321
322 struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
323 {
324         struct grub2_parser *parser;
325
326         parser = talloc(ctx, struct grub2_parser);
327         yylex_init_extra(parser, &parser->scanner);
328         parser->script = create_script(parser, ctx);
329         parser->inter_word = false;
330
331         return parser;
332 }
333
334 void grub2_parser_parse(struct grub2_parser *parser, const char *filename,
335                 char *buf, int len)
336 {
337         YY_BUFFER_STATE bufstate;
338         int rc;
339
340         if (!len)
341                 return;
342
343         parser->script->filename = filename;
344
345         bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
346         yyset_lineno(1, parser->scanner);
347
348         rc = yyparse(parser, parser->scanner);
349
350         yy_delete_buffer(bufstate, parser->scanner);
351
352         if (!rc)
353                 script_execute(parser->script);
354 }
355