parser.y 10.5 KB
Newer Older
1 2 3
/* parser.y - The scripting parser.  */
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2005,2006,2007,2008,2009,2010  Free Software Foundation, Inc.
5
 *
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 19 20
 */

%{
21
#include <grub/script_sh.h>
22
#include <grub/mm.h>
BVK Chaitanya's avatar
BVK Chaitanya committed
23
#include <grub/misc.h>
24
#include <grub/i18n.h>
25

26 27
#define YYFREE          grub_free
#define YYMALLOC        grub_malloc
28
#define YYLTYPE_IS_TRIVIAL      0
29
#define YYENABLE_NLS    0
30

BVK Chaitanya's avatar
BVK Chaitanya committed
31
#include "grub_script.tab.h"
32

33 34
#pragma GCC diagnostic ignored "-Wmissing-declarations"

35 36 37 38 39 40 41
%}

%union {
  struct grub_script_cmd *cmd;
  struct grub_script_arglist *arglist;
  struct grub_script_arg *arg;
  char *string;
42 43 44
  struct {
    unsigned offset;
    struct grub_script_mem *memory;
45
    struct grub_script *scripts;
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
%token GRUB_PARSER_TOKEN_BAD
%token GRUB_PARSER_TOKEN_EOF 0 "end-of-input"

%token GRUB_PARSER_TOKEN_NEWLINE "\n"
%token GRUB_PARSER_TOKEN_AND     "&&"
%token GRUB_PARSER_TOKEN_OR      "||"
%token GRUB_PARSER_TOKEN_SEMI2   ";;"
%token GRUB_PARSER_TOKEN_PIPE    "|"
%token GRUB_PARSER_TOKEN_AMP     "&"
%token GRUB_PARSER_TOKEN_SEMI    ";"
%token GRUB_PARSER_TOKEN_LBR     "{"
%token GRUB_PARSER_TOKEN_RBR     "}"
%token GRUB_PARSER_TOKEN_NOT     "!"
%token GRUB_PARSER_TOKEN_LSQBR2  "["
%token GRUB_PARSER_TOKEN_RSQBR2  "]"
%token GRUB_PARSER_TOKEN_LT      "<"
%token GRUB_PARSER_TOKEN_GT      ">"

%token <arg> GRUB_PARSER_TOKEN_CASE      "case"
%token <arg> GRUB_PARSER_TOKEN_DO        "do"
%token <arg> GRUB_PARSER_TOKEN_DONE      "done"
%token <arg> GRUB_PARSER_TOKEN_ELIF      "elif"
%token <arg> GRUB_PARSER_TOKEN_ELSE      "else"
%token <arg> GRUB_PARSER_TOKEN_ESAC      "esac"
%token <arg> GRUB_PARSER_TOKEN_FI        "fi"
%token <arg> GRUB_PARSER_TOKEN_FOR       "for"
%token <arg> GRUB_PARSER_TOKEN_IF        "if"
%token <arg> GRUB_PARSER_TOKEN_IN        "in"
%token <arg> GRUB_PARSER_TOKEN_SELECT    "select"
%token <arg> GRUB_PARSER_TOKEN_THEN      "then"
%token <arg> GRUB_PARSER_TOKEN_UNTIL     "until"
%token <arg> GRUB_PARSER_TOKEN_WHILE     "while"
%token <arg> GRUB_PARSER_TOKEN_FUNCTION  "function"
%token <arg> GRUB_PARSER_TOKEN_NAME      "name"
%token <arg> GRUB_PARSER_TOKEN_WORD      "word"

85
%type <arg> block block0
86
%type <arglist> word argument arguments0 arguments1
BVK Chaitanya's avatar
BVK Chaitanya committed
87

88
%type <cmd> script_init script
BVK Chaitanya's avatar
BVK Chaitanya committed
89
%type <cmd> grubcmd ifclause ifcmd forcmd whilecmd untilcmd
90
%type <cmd> command commands1 statement
91

92
%pure-parser
93
%lex-param   { struct grub_parser_param *state };
94 95
%parse-param { struct grub_parser_param *state };

96 97
%start script_init

98 99
%%
/* It should be possible to do this in a clean way...  */
100
script_init: { state->err = 0; } script { state->parsed = $2; state->err = 0; }
101 102
;

103 104 105 106
script: newlines0
        {
          $$ = 0;
        }
107
      | script statement delimiter newlines0
108
        {
109
          $$ = grub_script_append_cmd (state, $1, $2);
110 111 112 113
        }
      | error
        {
          $$ = 0;
114
          yyerror (state, N_("Incorrect command"));
115 116
          yyerrok;
        }
117 118
;

119 120
newlines0: /* Empty */ | newlines1 ;
newlines1: newlines0 "\n" ;
121

122 123 124 125 126 127
delimiter: ";"
         | "\n"
;
delimiters0: /* Empty */ | delimiters1 ;
delimiters1: delimiter
          | delimiters1 "\n"
128 129
;

130 131
word: GRUB_PARSER_TOKEN_NAME { $$ = grub_script_add_arglist (state, 0, $1); }
    | GRUB_PARSER_TOKEN_WORD { $$ = grub_script_add_arglist (state, 0, $1); }
132 133
;

134 135
statement: command   { $$ = $1; }
         | function  { $$ = 0;  }
136
;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

argument : "case"      { $$ = grub_script_add_arglist (state, 0, $1); }
         | "do"        { $$ = grub_script_add_arglist (state, 0, $1); }
         | "done"      { $$ = grub_script_add_arglist (state, 0, $1); }
         | "elif"      { $$ = grub_script_add_arglist (state, 0, $1); }
         | "else"      { $$ = grub_script_add_arglist (state, 0, $1); }
         | "esac"      { $$ = grub_script_add_arglist (state, 0, $1); }
         | "fi"        { $$ = grub_script_add_arglist (state, 0, $1); }
         | "for"       { $$ = grub_script_add_arglist (state, 0, $1); }
         | "if"        { $$ = grub_script_add_arglist (state, 0, $1); }
         | "in"        { $$ = grub_script_add_arglist (state, 0, $1); }
         | "select"    { $$ = grub_script_add_arglist (state, 0, $1); }
         | "then"      { $$ = grub_script_add_arglist (state, 0, $1); }
         | "until"     { $$ = grub_script_add_arglist (state, 0, $1); }
         | "while"     { $$ = grub_script_add_arglist (state, 0, $1); }
         | "function"  { $$ = grub_script_add_arglist (state, 0, $1); }
         | word { $$ = $1; }
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
/*
  Block parameter is passed to commands in two forms: as unparsed
  string and as pre-parsed grub_script object.  Passing as grub_script
  object makes memory management difficult, because:

  (1) Command may want to keep a reference to grub_script objects for
      later use, so script framework may not free the grub_script
      object after command completes.

  (2) Command may get called multiple times with same grub_script
      object under loops, so we should not let command implementation
      to free the grub_script object.

  To solve above problems, we rely on reference counting for
  grub_script objects.  Commands that want to keep the grub_script
  object must take a reference to it.

  Other complexity comes with arbitrary nesting of grub_script
  objects: a grub_script object may have commands with several block
  parameters, and each block parameter may further contain multiple
  block parameters nested.  We use temporary variable, state->scripts
  to collect nested child scripts (that are linked by siblings and
  children members), and will build grub_scripts tree from bottom.
 */
180
block: "{"
BVK Chaitanya's avatar
BVK Chaitanya committed
181 182 183
       {
         grub_script_lexer_ref (state->lexerstate);
         $<offset>$ = grub_script_lexer_record_start (state);
184
	 $<memory>$ = grub_script_mem_record (state);
185 186 187 188

	 /* save currently known scripts.  */
	 $<scripts>$ = state->scripts;
	 state->scripts = 0;
BVK Chaitanya's avatar
BVK Chaitanya committed
189 190 191 192
       }
       commands1 delimiters0 "}"
       {
         char *p;
193
	 struct grub_script_mem *memory;
194
	 struct grub_script *s = $<scripts>2;
BVK Chaitanya's avatar
BVK Chaitanya committed
195

196 197
	 memory = grub_script_mem_record_stop (state, $<memory>2);
         if ((p = grub_script_lexer_record_stop (state, $<offset>2)))
BVK Chaitanya's avatar
BVK Chaitanya committed
198 199
	   *grub_strrchr (p, '}') = '\0';

200 201
	 $$ = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p);
	 if (! $$ || ! ($$->script = grub_script_create ($3, memory)))
202
	   grub_script_mem_free (memory);
BVK Chaitanya's avatar
BVK Chaitanya committed
203

204 205 206 207 208 209 210
	 else {
	   /* attach nested scripts to $$->script as children */
	   $$->script->children = state->scripts;

	   /* restore old scripts; append $$->script to siblings. */
	   state->scripts = $<scripts>2 ?: $$->script;
	   if (s) {
211 212 213
	     while (s->next_siblings)
	       s = s->next_siblings;
	     s->next_siblings = $$->script;
214 215 216
	   }
	 }

217
         grub_script_lexer_deref (state->lexerstate);
BVK Chaitanya's avatar
BVK Chaitanya committed
218 219
       }
;
220 221 222
block0: /* Empty */ { $$ = 0; }
      | block { $$ = $1; }
;
BVK Chaitanya's avatar
BVK Chaitanya committed
223

224 225 226 227 228 229 230 231 232 233 234 235 236
arguments0: /* Empty */ { $$ = 0; }
          | arguments1  { $$ = $1; }
;
arguments1: argument arguments0
            {
	      if ($1 && $2)
		{
		  $1->next = $2;
		  $1->argcount += $2->argcount;
		  $2->argcount = 0;
		}
	      $$ = $1;
            }
237 238
;

239
grubcmd: word arguments0 block0
240
         {
241 242 243 244 245 246 247 248 249
	   struct grub_script_arglist *x = $2;

	   if ($3)
	     x = grub_script_add_arglist (state, $2, $3);

           if ($1 && x) {
             $1->next = x;
             $1->argcount += x->argcount;
             x->argcount = 0;
250 251 252
           }
           $$ = grub_script_create_cmdline (state, $1);
         }
253 254
;

255
/* A single command.  */
256 257 258 259 260
command: grubcmd  { $$ = $1; }
       | ifcmd    { $$ = $1; }
       | forcmd   { $$ = $1; }
       | whilecmd { $$ = $1; }
       | untilcmd { $$ = $1; }
261 262
;

263 264 265
/* A list of commands. */
commands1: newlines0 command
           {
266
             $$ = grub_script_append_cmd (state, 0, $2);
267 268 269
           }
         | commands1 delimiters1 command
           {
270
	     $$ = grub_script_append_cmd (state, $1, $3);
271
           }
272 273
;

274
function: "function" "name"
275 276 277
          {
            grub_script_lexer_ref (state->lexerstate);
            state->func_mem = grub_script_mem_record (state);
278 279 280

	    $<scripts>$ = state->scripts;
	    state->scripts = 0;
281 282 283 284 285 286 287
          }
          delimiters0 "{" commands1 delimiters1 "}"
          {
            struct grub_script *script;
            state->func_mem = grub_script_mem_record_stop (state,
                                                           state->func_mem);
            script = grub_script_create ($6, state->func_mem);
288 289
            if (! script)
	      grub_script_mem_free (state->func_mem);
290 291
	    else {
	      script->children = state->scripts;
292
	      grub_script_function_create ($2, script);
293
	    }
294

295
	    state->scripts = $<scripts>3;
296 297
            grub_script_lexer_deref (state->lexerstate);
          }
298 299
;

300 301 302 303 304 305 306 307 308
ifcmd: "if"
	{
	  grub_script_lexer_ref (state->lexerstate);
	}
	ifclause "fi"
	{
	  $$ = $3;
	  grub_script_lexer_deref (state->lexerstate);
	}
309
;
310 311 312 313 314 315 316 317 318 319 320 321
ifclause: commands1 delimiters1 "then" commands1 delimiters1
	  {
	    $$ = grub_script_create_cmdif (state, $1, $4, 0);
	  }
	| commands1 delimiters1 "then" commands1 delimiters1 "else" commands1 delimiters1
	  {
	    $$ = grub_script_create_cmdif (state, $1, $4, $7);
	  }
	| commands1 delimiters1 "then" commands1 delimiters1 "elif" ifclause
	  {
	    $$ = grub_script_create_cmdif (state, $1, $4, $7);
	  }
322
;
323 324 325 326 327 328 329 330 331 332 333

forcmd: "for" "name"
        {
	  grub_script_lexer_ref (state->lexerstate);
        }
        "in" arguments0 delimiters1 "do" commands1 delimiters1 "done"
	{
	  $$ = grub_script_create_cmdfor (state, $2, $5, $8);
	  grub_script_lexer_deref (state->lexerstate);
	}
;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

whilecmd: "while"
          {
	    grub_script_lexer_ref (state->lexerstate);
          }
          commands1 delimiters1 "do" commands1 delimiters1 "done"
	  {
	    $$ = grub_script_create_cmdwhile (state, $3, $6, 0);
	    grub_script_lexer_deref (state->lexerstate);
	  }
;

untilcmd: "until"
          {
	    grub_script_lexer_ref (state->lexerstate);
          }
          commands1 delimiters1 "do" commands1 delimiters1 "done"
	  {
	    $$ = grub_script_create_cmdwhile (state, $3, $6, 1);
	    grub_script_lexer_deref (state->lexerstate);
	  }
;