1
/* parser.y - The scripting parser. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21
#include <grub/script_sh.h>
23
#include <grub/misc.h>
25
#define YYFREE grub_free
26
#define YYMALLOC grub_malloc
27
#define YYLTYPE_IS_TRIVIAL 0
28
#define YYENABLE_NLS 0
30
#include "grub_script.tab.h"
34
struct grub_script_cmd *cmd;
35
struct grub_script_arglist *arglist;
36
struct grub_script_arg *arg;
40
struct grub_script_mem *memory;
41
struct grub_script *scripts;
45
%token GRUB_PARSER_TOKEN_BAD
46
%token GRUB_PARSER_TOKEN_EOF 0 "end-of-input"
48
%token GRUB_PARSER_TOKEN_NEWLINE "\n"
49
%token GRUB_PARSER_TOKEN_AND "&&"
50
%token GRUB_PARSER_TOKEN_OR "||"
51
%token GRUB_PARSER_TOKEN_SEMI2 ";;"
52
%token GRUB_PARSER_TOKEN_PIPE "|"
53
%token GRUB_PARSER_TOKEN_AMP "&"
54
%token GRUB_PARSER_TOKEN_SEMI ";"
55
%token GRUB_PARSER_TOKEN_LBR "{"
56
%token GRUB_PARSER_TOKEN_RBR "}"
57
%token GRUB_PARSER_TOKEN_NOT "!"
58
%token GRUB_PARSER_TOKEN_LSQBR2 "["
59
%token GRUB_PARSER_TOKEN_RSQBR2 "]"
60
%token GRUB_PARSER_TOKEN_LT "<"
61
%token GRUB_PARSER_TOKEN_GT ">"
63
%token <arg> GRUB_PARSER_TOKEN_CASE "case"
64
%token <arg> GRUB_PARSER_TOKEN_DO "do"
65
%token <arg> GRUB_PARSER_TOKEN_DONE "done"
66
%token <arg> GRUB_PARSER_TOKEN_ELIF "elif"
67
%token <arg> GRUB_PARSER_TOKEN_ELSE "else"
68
%token <arg> GRUB_PARSER_TOKEN_ESAC "esac"
69
%token <arg> GRUB_PARSER_TOKEN_FI "fi"
70
%token <arg> GRUB_PARSER_TOKEN_FOR "for"
71
%token <arg> GRUB_PARSER_TOKEN_IF "if"
72
%token <arg> GRUB_PARSER_TOKEN_IN "in"
73
%token <arg> GRUB_PARSER_TOKEN_SELECT "select"
74
%token <arg> GRUB_PARSER_TOKEN_THEN "then"
75
%token <arg> GRUB_PARSER_TOKEN_UNTIL "until"
76
%token <arg> GRUB_PARSER_TOKEN_WHILE "while"
77
%token <arg> GRUB_PARSER_TOKEN_TIME "time"
78
%token <arg> GRUB_PARSER_TOKEN_FUNCTION "function"
79
%token <arg> GRUB_PARSER_TOKEN_NAME "name"
80
%token <arg> GRUB_PARSER_TOKEN_WORD "word"
82
%type <arg> block block0
83
%type <arglist> word argument arguments0 arguments1
85
%type <cmd> script_init script
86
%type <cmd> grubcmd ifclause ifcmd forcmd whilecmd untilcmd
87
%type <cmd> command commands1 statement
90
%lex-param { struct grub_parser_param *state };
91
%parse-param { struct grub_parser_param *state };
96
/* It should be possible to do this in a clean way... */
97
script_init: { state->err = 0; } script { state->parsed = $2; state->err = 0; }
104
| script statement delimiter newlines0
106
$$ = grub_script_append_cmd (state, $1, $2);
111
yyerror (state, "Incorrect command");
116
newlines0: /* Empty */ | newlines1 ;
117
newlines1: newlines0 "\n" ;
122
delimiters0: /* Empty */ | delimiters1 ;
123
delimiters1: delimiter
127
word: GRUB_PARSER_TOKEN_NAME { $$ = grub_script_add_arglist (state, 0, $1); }
128
| GRUB_PARSER_TOKEN_WORD { $$ = grub_script_add_arglist (state, 0, $1); }
131
statement: command { $$ = $1; }
132
| function { $$ = 0; }
135
argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); }
136
| "do" { $$ = grub_script_add_arglist (state, 0, $1); }
137
| "done" { $$ = grub_script_add_arglist (state, 0, $1); }
138
| "elif" { $$ = grub_script_add_arglist (state, 0, $1); }
139
| "else" { $$ = grub_script_add_arglist (state, 0, $1); }
140
| "esac" { $$ = grub_script_add_arglist (state, 0, $1); }
141
| "fi" { $$ = grub_script_add_arglist (state, 0, $1); }
142
| "for" { $$ = grub_script_add_arglist (state, 0, $1); }
143
| "if" { $$ = grub_script_add_arglist (state, 0, $1); }
144
| "in" { $$ = grub_script_add_arglist (state, 0, $1); }
145
| "select" { $$ = grub_script_add_arglist (state, 0, $1); }
146
| "then" { $$ = grub_script_add_arglist (state, 0, $1); }
147
| "until" { $$ = grub_script_add_arglist (state, 0, $1); }
148
| "while" { $$ = grub_script_add_arglist (state, 0, $1); }
149
| "function" { $$ = grub_script_add_arglist (state, 0, $1); }
154
Block parameter is passed to commands in two forms: as unparsed
155
string and as pre-parsed grub_script object. Passing as grub_script
156
object makes memory management difficult, because:
158
(1) Command may want to keep a reference to grub_script objects for
159
later use, so script framework may not free the grub_script
160
object after command completes.
162
(2) Command may get called multiple times with same grub_script
163
object under loops, so we should not let command implementation
164
to free the grub_script object.
166
To solve above problems, we rely on reference counting for
167
grub_script objects. Commands that want to keep the grub_script
168
object must take a reference to it.
170
Other complexity comes with arbitrary nesting of grub_script
171
objects: a grub_script object may have commands with several block
172
parameters, and each block parameter may further contain multiple
173
block parameters nested. We use temporary variable, state->scripts
174
to collect nested child scripts (that are linked by siblings and
175
children members), and will build grub_scripts tree from bottom.
179
grub_script_lexer_ref (state->lexerstate);
180
$<offset>$ = grub_script_lexer_record_start (state);
181
$<memory>$ = grub_script_mem_record (state);
183
/* save currently known scripts. */
184
$<scripts>$ = state->scripts;
187
commands1 delimiters0 "}"
190
struct grub_script_mem *memory;
191
struct grub_script *s = $<scripts>2;
193
memory = grub_script_mem_record_stop (state, $<memory>2);
194
if ((p = grub_script_lexer_record_stop (state, $<offset>2)))
195
*grub_strrchr (p, '}') = '\0';
197
$$ = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p);
198
if (! $$ || ! ($$->script = grub_script_create ($3, memory)))
199
grub_script_mem_free (memory);
202
/* attach nested scripts to $$->script as children */
203
$$->script->children = state->scripts;
205
/* restore old scripts; append $$->script to siblings. */
206
state->scripts = $<scripts>2 ?: $$->script;
208
while (s->next_siblings)
209
s = s->next_siblings;
210
s->next_siblings = $$->script;
214
grub_script_lexer_deref (state->lexerstate);
217
block0: /* Empty */ { $$ = 0; }
221
arguments0: /* Empty */ { $$ = 0; }
222
| arguments1 { $$ = $1; }
224
arguments1: argument arguments0
229
$1->argcount += $2->argcount;
236
grubcmd: word arguments0 block0
238
struct grub_script_arglist *x = $2;
241
x = grub_script_add_arglist (state, $2, $3);
245
$1->argcount += x->argcount;
248
$$ = grub_script_create_cmdline (state, $1);
252
/* A single command. */
253
command: grubcmd { $$ = $1; }
255
| forcmd { $$ = $1; }
256
| whilecmd { $$ = $1; }
257
| untilcmd { $$ = $1; }
260
/* A list of commands. */
261
commands1: newlines0 command
263
$$ = grub_script_append_cmd (state, 0, $2);
265
| commands1 delimiters1 command
267
$$ = grub_script_append_cmd (state, $1, $3);
271
function: "function" "name"
273
grub_script_lexer_ref (state->lexerstate);
274
state->func_mem = grub_script_mem_record (state);
276
$<scripts>$ = state->scripts;
279
delimiters0 "{" commands1 delimiters1 "}"
281
struct grub_script *script;
282
state->func_mem = grub_script_mem_record_stop (state,
284
script = grub_script_create ($6, state->func_mem);
286
grub_script_mem_free (state->func_mem);
288
script->children = state->scripts;
289
grub_script_function_create ($2, script);
292
state->scripts = $<scripts>3;
293
grub_script_lexer_deref (state->lexerstate);
299
grub_script_lexer_ref (state->lexerstate);
304
grub_script_lexer_deref (state->lexerstate);
307
ifclause: commands1 delimiters1 "then" commands1 delimiters1
309
$$ = grub_script_create_cmdif (state, $1, $4, 0);
311
| commands1 delimiters1 "then" commands1 delimiters1 "else" commands1 delimiters1
313
$$ = grub_script_create_cmdif (state, $1, $4, $7);
315
| commands1 delimiters1 "then" commands1 delimiters1 "elif" ifclause
317
$$ = grub_script_create_cmdif (state, $1, $4, $7);
323
grub_script_lexer_ref (state->lexerstate);
325
"in" arguments0 delimiters1 "do" commands1 delimiters1 "done"
327
$$ = grub_script_create_cmdfor (state, $2, $5, $8);
328
grub_script_lexer_deref (state->lexerstate);
334
grub_script_lexer_ref (state->lexerstate);
336
commands1 delimiters1 "do" commands1 delimiters1 "done"
338
$$ = grub_script_create_cmdwhile (state, $3, $6, 0);
339
grub_script_lexer_deref (state->lexerstate);
345
grub_script_lexer_ref (state->lexerstate);
347
commands1 delimiters1 "do" commands1 delimiters1 "done"
349
$$ = grub_script_create_cmdwhile (state, $3, $6, 1);
350
grub_script_lexer_deref (state->lexerstate);