/* parser.y - The scripting parser. */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * 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 * along with VAS_EBOOT. If not, see . */ %{ #include #include #include #include #define YYFREE VasEBoot_free #define YYMALLOC VasEBoot_malloc #define YYLTYPE_IS_TRIVIAL 0 #define YYENABLE_NLS 0 #include "VasEBoot_script.tab.h" #pragma GCC diagnostic ignored "-Wmissing-declarations" %} %union { struct VasEBoot_script_cmd *cmd; struct VasEBoot_script_arglist *arglist; struct VasEBoot_script_arg *arg; char *string; struct { unsigned offset; struct VasEBoot_script_mem *memory; struct VasEBoot_script *scripts; }; } %token VAS_EBOOT_PARSER_TOKEN_BAD %token VAS_EBOOT_PARSER_TOKEN_EOF 0 "end-of-input" %token VAS_EBOOT_PARSER_TOKEN_NEWLINE "\n" %token VAS_EBOOT_PARSER_TOKEN_AND "&&" %token VAS_EBOOT_PARSER_TOKEN_OR "||" %token VAS_EBOOT_PARSER_TOKEN_SEMI2 ";;" %token VAS_EBOOT_PARSER_TOKEN_PIPE "|" %token VAS_EBOOT_PARSER_TOKEN_AMP "&" %token VAS_EBOOT_PARSER_TOKEN_SEMI ";" %token VAS_EBOOT_PARSER_TOKEN_LBR "{" %token VAS_EBOOT_PARSER_TOKEN_RBR "}" %token VAS_EBOOT_PARSER_TOKEN_NOT "!" %token VAS_EBOOT_PARSER_TOKEN_LSQBR2 "[" %token VAS_EBOOT_PARSER_TOKEN_RSQBR2 "]" %token VAS_EBOOT_PARSER_TOKEN_LT "<" %token VAS_EBOOT_PARSER_TOKEN_GT ">" %token VAS_EBOOT_PARSER_TOKEN_CASE "case" %token VAS_EBOOT_PARSER_TOKEN_DO "do" %token VAS_EBOOT_PARSER_TOKEN_DONE "done" %token VAS_EBOOT_PARSER_TOKEN_ELIF "elif" %token VAS_EBOOT_PARSER_TOKEN_ELSE "else" %token VAS_EBOOT_PARSER_TOKEN_ESAC "esac" %token VAS_EBOOT_PARSER_TOKEN_FI "fi" %token VAS_EBOOT_PARSER_TOKEN_FOR "for" %token VAS_EBOOT_PARSER_TOKEN_IF "if" %token VAS_EBOOT_PARSER_TOKEN_IN "in" %token VAS_EBOOT_PARSER_TOKEN_SELECT "select" %token VAS_EBOOT_PARSER_TOKEN_THEN "then" %token VAS_EBOOT_PARSER_TOKEN_UNTIL "until" %token VAS_EBOOT_PARSER_TOKEN_WHILE "while" %token VAS_EBOOT_PARSER_TOKEN_FUNCTION "function" %token VAS_EBOOT_PARSER_TOKEN_NAME "name" %token VAS_EBOOT_PARSER_TOKEN_WORD "word" %type block block0 %type word argument arguments0 arguments1 %type script_init script %type VasEBootcmd ifclause ifcmd forcmd whilecmd untilcmd %type command commands1 statement %pure-parser %lex-param { struct VasEBoot_parser_param *state }; %parse-param { struct VasEBoot_parser_param *state }; %start script_init %% /* It should be possible to do this in a clean way... */ script_init: { state->err = 0; } script { state->parsed = $2; state->err = 0; } ; script: newlines0 { $$ = 0; } | script statement delimiter newlines0 { $$ = VasEBoot_script_append_cmd (state, $1, $2); } | error { $$ = 0; yyerror (state, N_("Incorrect command")); yyerrok; } ; newlines0: /* Empty */ | newlines1 ; newlines1: newlines0 "\n" ; delimiter: ";" | "\n" ; delimiters0: /* Empty */ | delimiters1 ; delimiters1: delimiter | delimiters1 "\n" ; word: VAS_EBOOT_PARSER_TOKEN_NAME { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | VAS_EBOOT_PARSER_TOKEN_WORD { $$ = VasEBoot_script_add_arglist (state, 0, $1); } ; statement: command { $$ = $1; } | function { $$ = 0; } ; argument : "case" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "do" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "done" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "elif" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "else" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "esac" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "fi" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "for" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "if" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "in" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "select" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "then" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "until" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "while" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | "function" { $$ = VasEBoot_script_add_arglist (state, 0, $1); } | word { $$ = $1; } ; /* Block parameter is passed to commands in two forms: as unparsed string and as pre-parsed VasEBoot_script object. Passing as VasEBoot_script object makes memory management difficult, because: (1) Command may want to keep a reference to VasEBoot_script objects for later use, so script framework may not free the VasEBoot_script object after command completes. (2) Command may get called multiple times with same VasEBoot_script object under loops, so we should not let command implementation to free the VasEBoot_script object. To solve above problems, we rely on reference counting for VasEBoot_script objects. Commands that want to keep the VasEBoot_script object must take a reference to it. Other complexity comes with arbitrary nesting of VasEBoot_script objects: a VasEBoot_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 VasEBoot_scripts tree from bottom. */ block: "{" { VasEBoot_script_lexer_ref (state->lexerstate); $$ = VasEBoot_script_lexer_record_start (state); $$ = VasEBoot_script_mem_record (state); /* save currently known scripts. */ $$ = state->scripts; state->scripts = 0; } commands1 delimiters0 "}" { char *p; struct VasEBoot_script_mem *memory; struct VasEBoot_script *s = $2; memory = VasEBoot_script_mem_record_stop (state, $2); if ((p = VasEBoot_script_lexer_record_stop (state, $2))) *VasEBoot_strrchr (p, '}') = '\0'; $$ = VasEBoot_script_arg_add (state, 0, VAS_EBOOT_SCRIPT_ARG_TYPE_BLOCK, p); if (! $$ || ! ($$->script = VasEBoot_script_create ($3, memory))) VasEBoot_script_mem_free (memory); else { /* attach nested scripts to $$->script as children */ $$->script->children = state->scripts; /* restore old scripts; append $$->script to siblings. */ state->scripts = $2 ?: $$->script; if (s) { while (s->next_siblings) s = s->next_siblings; s->next_siblings = $$->script; } } VasEBoot_script_lexer_deref (state->lexerstate); } ; block0: /* Empty */ { $$ = 0; } | block { $$ = $1; } ; arguments0: /* Empty */ { $$ = 0; } | arguments1 { $$ = $1; } ; arguments1: argument arguments0 { if ($1 && $2) { $1->next = $2; $1->argcount += $2->argcount; $2->argcount = 0; } $$ = $1; } ; VasEBootcmd: word arguments0 block0 { struct VasEBoot_script_arglist *x = $2; if ($3) x = VasEBoot_script_add_arglist (state, $2, $3); if ($1 && x) { $1->next = x; $1->argcount += x->argcount; x->argcount = 0; } $$ = VasEBoot_script_create_cmdline (state, $1); } ; /* A single command. */ command: VasEBootcmd { $$ = $1; } | ifcmd { $$ = $1; } | forcmd { $$ = $1; } | whilecmd { $$ = $1; } | untilcmd { $$ = $1; } ; /* A list of commands. */ commands1: newlines0 command { $$ = VasEBoot_script_append_cmd (state, 0, $2); } | commands1 delimiters1 command { $$ = VasEBoot_script_append_cmd (state, $1, $3); } ; function: "function" "name" { VasEBoot_script_lexer_ref (state->lexerstate); state->func_mem = VasEBoot_script_mem_record (state); $$ = state->scripts; state->scripts = 0; } newlines0 "{" commands1 delimiters1 "}" { struct VasEBoot_script *script; state->func_mem = VasEBoot_script_mem_record_stop (state, state->func_mem); script = VasEBoot_script_create ($6, state->func_mem); if (! script) VasEBoot_script_mem_free (state->func_mem); else { script->children = state->scripts; if (!VasEBoot_script_function_create ($2, script)) VasEBoot_script_free (script); } state->scripts = $3; VasEBoot_script_lexer_deref (state->lexerstate); } ; ifcmd: "if" { VasEBoot_script_lexer_ref (state->lexerstate); } ifclause "fi" { $$ = $3; VasEBoot_script_lexer_deref (state->lexerstate); } ; ifclause: commands1 delimiters1 "then" commands1 delimiters1 { $$ = VasEBoot_script_create_cmdif (state, $1, $4, 0); } | commands1 delimiters1 "then" commands1 delimiters1 "else" commands1 delimiters1 { $$ = VasEBoot_script_create_cmdif (state, $1, $4, $7); } | commands1 delimiters1 "then" commands1 delimiters1 "elif" ifclause { $$ = VasEBoot_script_create_cmdif (state, $1, $4, $7); } ; forcmd: "for" "name" { VasEBoot_script_lexer_ref (state->lexerstate); } "in" arguments0 delimiters1 "do" commands1 delimiters1 "done" { $$ = VasEBoot_script_create_cmdfor (state, $2, $5, $8); VasEBoot_script_lexer_deref (state->lexerstate); } ; whilecmd: "while" { VasEBoot_script_lexer_ref (state->lexerstate); } commands1 delimiters1 "do" commands1 delimiters1 "done" { $$ = VasEBoot_script_create_cmdwhile (state, $3, $6, 0); VasEBoot_script_lexer_deref (state->lexerstate); } ; untilcmd: "until" { VasEBoot_script_lexer_ref (state->lexerstate); } commands1 delimiters1 "do" commands1 delimiters1 "done" { $$ = VasEBoot_script_create_cmdwhile (state, $3, $6, 1); VasEBoot_script_lexer_deref (state->lexerstate); } ;