summaryrefslogtreecommitdiffstats
path: root/discover/grub2/parser.y
blob: 859eba30479b93eeac8fa4a8c00d21d7916931d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

%pure-parser
%lex-param { yyscan_t scanner }
%parse-param { struct grub2_parser *parser }
%error-verbose

%{
#include <talloc/talloc.h>

#include "grub2.h"
#include "parser.h"
#include "lexer.h"

#define YYLEX_PARAM parser->scanner

static void yyerror(struct grub2_parser *, char const *s);
%}

%union {
	struct grub2_word	*word;
	struct grub2_argv	*argv;
	struct grub2_statement	*statement;
	struct grub2_statements	*statements;
}

/* reserved words */
%token	TOKEN_LDSQBRACKET	"[["
%token	TOKEN_RDSQBRACKET	"]]"
%token	TOKEN_CASE		"case"
%token	TOKEN_DO		"do"
%token	TOKEN_DONE		"done"
%token	TOKEN_ELIF		"elif"
%token	TOKEN_ELSE		"else"
%token	TOKEN_ESAC		"esac"
%token	TOKEN_FI		"fi"
%token	TOKEN_FOR		"for"
%token	TOKEN_FUNCTION		"function"
%token	TOKEN_IF		"if"
%token	TOKEN_IN		"in"
%token	TOKEN_MENUENTRY		"menuentry"
%token	TOKEN_SELECT		"select"
%token	TOKEN_SUBMENU		"submenu"
%token	TOKEN_THEN		"then"
%token	TOKEN_TIME		"time"
%token	TOKEN_UTIL		"until"
%token	TOKEN_WHILE		"while"

%type <statement>	statement
%type <statements>	statements
%type <argv>		words
%type <word>		word

/* syntax */
%token	TOKEN_EOL
%token	TOKEN_DELIM
%token	<word> TOKEN_WORD

%start	script
%debug

%%

script: statements {
		parser->script->statements = $1;
	}

statements: statement {
		$$ = create_statements(parser);
		statement_append($$, $1);
	}
	| statements statement {
		statement_append($1, $2);
		$$ = $1;
	}

statement: TOKEN_EOL {
		$$ = NULL;
	}
	| words TOKEN_EOL {
		   $$ = create_statement_simple(parser, $1);
	}
	| '{' statements '}' {
		$$ = create_statement_block(parser, $2);
	}
	| "if" TOKEN_DELIM statement
		"then" TOKEN_EOL
		statements
		"fi" TOKEN_EOL {
		$$ = create_statement_if(parser, $3, $6, NULL);
	}
	| "if" TOKEN_DELIM statement
		"then" TOKEN_EOL
		statements
		"else" TOKEN_EOL
		statements
		"fi" TOKEN_EOL {
		$$ = create_statement_if(parser, $3, $6, $9);
	}
	| "function" TOKEN_DELIM word TOKEN_DELIM '{' statements '}' {
		$$ = create_statement_function(parser, $3, $6);
	}
	| "menuentry" TOKEN_DELIM words TOKEN_DELIM
		'{' statements '}'
		TOKEN_EOL {
		$$ = create_statement_menuentry(parser, $3, $6);
	}
	| "submenu" TOKEN_DELIM words TOKEN_DELIM
		'{' statements '}'
		TOKEN_EOL {
		/* we just flatten everything */
		$$ = create_statement_block(parser, $6);
	}

words:	word {
		$$ = create_argv(parser);
		argv_append($$, $1);
	}
	| words TOKEN_DELIM word {
		argv_append($1, $3);
		$$ = $1;
	}

word:	TOKEN_WORD
	| word TOKEN_WORD {
		word_append($1, $2);
		$$ = $1;
	}

%%
void yyerror(struct grub2_parser *parser, char const *s)
{
	fprintf(stderr, "%d: error: %s '%s'\n",
			yyget_lineno(parser->scanner),
			s, yyget_text(parser->scanner));
}

struct grub2_statements *create_statements(struct grub2_parser *parser)
{
	struct grub2_statements *stmts = talloc(parser,
			struct grub2_statements);
	list_init(&stmts->list);
	return stmts;
}

struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
		struct grub2_argv *argv)
{
	struct grub2_statement_simple *stmt =
		talloc(parser, struct grub2_statement_simple);
	stmt->st.type = STMT_TYPE_SIMPLE;
	stmt->st.exec = statement_simple_execute;
	stmt->argv = argv;
	return &stmt->st;
}

struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
		struct grub2_argv *argv, struct grub2_statements *stmts)
{
	struct grub2_statement_menuentry *stmt =
		talloc(parser, struct grub2_statement_menuentry);
	stmt->st.type = STMT_TYPE_MENUENTRY;
	stmt->st.exec = statement_menuentry_execute;
	stmt->argv = argv;
	stmt->statements = stmts;
	return &stmt->st;
}

struct grub2_statement *create_statement_if(struct grub2_parser *parser,
		struct grub2_statement *condition,
		struct grub2_statements *true_case,
		struct grub2_statements *false_case)
{
	struct grub2_statement_if *stmt =
		talloc(parser, struct grub2_statement_if);
	stmt->st.type = STMT_TYPE_IF;
	stmt->st.exec = statement_if_execute;
	stmt->condition = condition;
	stmt->true_case = true_case;
	stmt->false_case = false_case;
	return &stmt->st;
}

struct grub2_statement *create_statement_block(struct grub2_parser *parser,
		struct grub2_statements *stmts)
{
	struct grub2_statement_block *stmt =
		talloc(parser, struct grub2_statement_block);
	stmt->st.type = STMT_TYPE_BLOCK;
	stmt->st.exec = NULL;
	stmt->statements = stmts;
	return &stmt->st;
}

struct grub2_statement *create_statement_function(struct grub2_parser *parser,
		struct grub2_word *name, struct grub2_statements *body)
{
	struct grub2_statement_function *stmt =
		talloc(parser, struct grub2_statement_function);
	stmt->st.exec = statement_function_execute;
	stmt->name = name;
	stmt->body = body;
	return &stmt->st;
}

void statement_append(struct grub2_statements *stmts,
		struct grub2_statement *stmt)
{
	if (!stmt)
		return;
	list_add_tail(&stmts->list, &stmt->list);
}

struct grub2_word *create_word_text(struct grub2_parser *parser,
		const char *text)
{
	struct grub2_word *word = talloc(parser, struct grub2_word);
	word->type = GRUB2_WORD_TEXT;
	word->split = false;
	word->text = talloc_strdup(word, text);
	word->next = NULL;
	word->last = word;
	return word;
}

struct grub2_word *create_word_var(struct grub2_parser *parser,
		const char *name, bool split)
{
	struct grub2_word *word = talloc(parser, struct grub2_word);
	word->type = GRUB2_WORD_VAR;
	word->name = talloc_strdup(word, name);
	word->split = split;
	word->next = NULL;
	word->last = word;
	return word;
}

struct grub2_argv *create_argv(struct grub2_parser *parser)
{
	struct grub2_argv *argv = talloc(parser, struct grub2_argv);
	list_init(&argv->words);
	return argv;
}

void argv_append(struct grub2_argv *argv, struct grub2_word *word)
{
	list_add_tail(&argv->words, &word->argv_list);
}

void word_append(struct grub2_word *w1, struct grub2_word *w2)
{
	w1->last->next = w2;
	w1->last = w2;
}

struct grub2_parser *grub2_parser_create(struct discover_context *ctx)
{
	struct grub2_parser *parser;

	parser = talloc(ctx, struct grub2_parser);
	yylex_init_extra(parser, &parser->scanner);
	parser->script = create_script(parser, ctx);

	return parser;
}

void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len)
{
	YY_BUFFER_STATE bufstate;
	int rc;

	bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);

	rc = yyparse(parser);

	yy_delete_buffer(bufstate, parser->scanner);

	if (!rc)
		script_execute(parser->script);
}

OpenPOWER on IntegriCloud