2009-06-04 Vladimir Serbinenko <phcoder@gmail.com>
Script fixes * include/grub/script_sh.h (grub_script_cmdline): remove cmdline (grub_lexer_param): add tokenonhold (grub_script_create_cmdline): remove cmdline. All callers updated (grub_script_function_create): make functionname grub_script_arg. All callers updated (grub_script_execute_argument_to_string): new prototype * kern/parser.c (state_transitions): reorder (grub_parser_cmdline_state): fix a bug and make more compact * script/sh/execute.c (grub_script_execute_argument_to_string): make global (grub_script_execute_cmdline): use new format * script/sh/function.c (grub_script_function_create): make functionname grub_script_arg. All callers updated * script/sh/lexer.c (grub_script_lexer_init): initilaize tokenonhold (grub_script_yylex): remove (grub_script_yylex2): renamed to ... (grub_script_yylex): ...renamed parse the expressions like a${b}c * script/sh/parser.y (GRUB_PARSER_TOKEN_ARG): new typed terminal (GRUB_PARSER_TOKEN_VAR): remove (GRUB_PARSER_TOKEN_NAME): likewise ("if"): declare as typeless ("while"): likewise ("function"): likewise ("else"): likewise ("then"): likewise ("fi"): likewise (text): remove (argument): likewise (script): accept empty scripts and make exit on error (arguments): use GRUB_PARSER_TOKEN_ARG (function): likewise (command): move error handling to script (menuentry): move grub_script_lexer_ref before * script/sh/script.c (grub_script_create_cmdline): remove cmdline argument. All callers updated
This commit is contained in:
parent
f4448a0792
commit
fda6cb987f
8 changed files with 304 additions and 280 deletions
|
@ -62,6 +62,7 @@ grub_script_lexer_init (char *script, grub_reader_getline_t getline)
|
|||
param->recording = 0;
|
||||
param->recordpos = 0;
|
||||
param->recordlen = 0;
|
||||
param->tokenonhold = 0;
|
||||
|
||||
return param;
|
||||
}
|
||||
|
@ -135,225 +136,249 @@ nextchar (struct grub_lexer_param *state)
|
|||
state->script++;
|
||||
}
|
||||
|
||||
int
|
||||
grub_script_yylex2 (union YYSTYPE *yylval,
|
||||
struct grub_parser_param *parsestate);
|
||||
|
||||
int
|
||||
grub_script_yylex (union YYSTYPE *yylval, struct grub_parser_param *parsestate)
|
||||
{
|
||||
int r = -1;
|
||||
|
||||
while (r == -1)
|
||||
{
|
||||
r = grub_script_yylex2 (yylval, parsestate);
|
||||
if (r == ' ')
|
||||
r = -1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
grub_script_yylex2 (union YYSTYPE *yylval, struct grub_parser_param *parsestate)
|
||||
{
|
||||
grub_parser_state_t newstate;
|
||||
char use;
|
||||
char *buffer;
|
||||
char *bp;
|
||||
struct grub_lexer_param *state = parsestate->lexerstate;
|
||||
int firstrun = 1;
|
||||
|
||||
if (state->done)
|
||||
return 0;
|
||||
yylval->arg = 0;
|
||||
|
||||
if (! *state->script)
|
||||
if (state->tokenonhold)
|
||||
{
|
||||
/* Check if more tokens are requested by the parser. */
|
||||
if ((state->refs
|
||||
|| state->state == GRUB_PARSER_STATE_ESC)
|
||||
&& state->getline)
|
||||
int token = state->tokenonhold;
|
||||
state->tokenonhold = 0;
|
||||
return token;
|
||||
}
|
||||
|
||||
for (;! state->done && (*state->script || firstrun); firstrun = 0)
|
||||
{
|
||||
|
||||
if (! *state->script)
|
||||
{
|
||||
while (!state->script || ! grub_strlen (state->script))
|
||||
/* Check if more tokens are requested by the parser. */
|
||||
if (((state->refs && ! parsestate->err)
|
||||
|| state->state == GRUB_PARSER_STATE_ESC)
|
||||
&& state->getline)
|
||||
{
|
||||
int doexit = 0;
|
||||
while (!state->script || ! grub_strlen (state->script))
|
||||
{
|
||||
grub_free (state->newscript);
|
||||
state->newscript = 0;
|
||||
state->getline (&state->newscript, 1);
|
||||
state->script = state->newscript;
|
||||
if (! state->script)
|
||||
{
|
||||
doexit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (doexit)
|
||||
break;
|
||||
grub_dprintf ("scripting", "token=`\\n'\n");
|
||||
recordchar (state, '\n');
|
||||
if (state->state != GRUB_PARSER_STATE_ESC)
|
||||
{
|
||||
state->tokenonhold = '\n';
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
grub_free (state->newscript);
|
||||
state->newscript = 0;
|
||||
state->getline (&state->newscript, 1);
|
||||
state->script = state->newscript;
|
||||
if (! state->script)
|
||||
return 0;
|
||||
}
|
||||
grub_dprintf ("scripting", "token=`\\n'\n");
|
||||
recordchar (state, '\n');
|
||||
if (state->state != GRUB_PARSER_STATE_ESC)
|
||||
return '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
grub_free (state->newscript);
|
||||
state->newscript = 0;
|
||||
state->done = 1;
|
||||
grub_dprintf ("scripting", "token=`\\n'\n");
|
||||
return '\n';
|
||||
}
|
||||
}
|
||||
|
||||
newstate = grub_parser_cmdline_state (state->state, *state->script, &use);
|
||||
|
||||
/* Check if it is a text. */
|
||||
if (check_textstate (newstate))
|
||||
{
|
||||
/* In case the string is not quoted, this can be a one char
|
||||
length symbol. */
|
||||
if (newstate == GRUB_PARSER_STATE_TEXT)
|
||||
{
|
||||
switch (*state->script)
|
||||
{
|
||||
case ' ':
|
||||
while (*state->script)
|
||||
{
|
||||
newstate = grub_parser_cmdline_state (state->state,
|
||||
*state->script, &use);
|
||||
if (! (state->state == GRUB_PARSER_STATE_TEXT
|
||||
&& *state->script == ' '))
|
||||
{
|
||||
grub_dprintf ("scripting", "token=` '\n");
|
||||
return ' ';
|
||||
}
|
||||
state->state = newstate;
|
||||
nextchar (state);
|
||||
}
|
||||
grub_dprintf ("scripting", "token=` '\n");
|
||||
return ' ';
|
||||
case '{':
|
||||
case '}':
|
||||
case ';':
|
||||
case '\n':
|
||||
{
|
||||
char c;
|
||||
grub_dprintf ("scripting", "token=`%c'\n", *state->script);
|
||||
c = *state->script;;
|
||||
nextchar (state);
|
||||
return c;
|
||||
}
|
||||
state->done = 1;
|
||||
grub_dprintf ("scripting", "token=`\\n'\n");
|
||||
state->tokenonhold = '\n';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* XXX: Use a better size. */
|
||||
buffer = grub_script_malloc (parsestate, 2048);
|
||||
if (! buffer)
|
||||
return 0;
|
||||
|
||||
bp = buffer;
|
||||
|
||||
/* Read one token, possible quoted. */
|
||||
while (*state->script)
|
||||
newstate = grub_parser_cmdline_state (state->state, *state->script, &use);
|
||||
|
||||
/* Check if it is a text. */
|
||||
if (check_textstate (newstate))
|
||||
{
|
||||
newstate = grub_parser_cmdline_state (state->state,
|
||||
*state->script, &use);
|
||||
|
||||
/* Check if a variable name starts. */
|
||||
if (check_varstate (newstate))
|
||||
break;
|
||||
|
||||
/* If the string is not quoted or escaped, stop processing
|
||||
when a special token was found. It will be recognized
|
||||
next time when this function is called. */
|
||||
if (newstate == GRUB_PARSER_STATE_TEXT
|
||||
&& state->state != GRUB_PARSER_STATE_ESC)
|
||||
/* In case the string is not quoted, this can be a one char
|
||||
length symbol. */
|
||||
if (newstate == GRUB_PARSER_STATE_TEXT)
|
||||
{
|
||||
int breakout = 0;
|
||||
|
||||
switch (use)
|
||||
int doexit = 0;
|
||||
switch (*state->script)
|
||||
{
|
||||
case ' ':
|
||||
while (*state->script)
|
||||
{
|
||||
newstate = grub_parser_cmdline_state (state->state,
|
||||
*state->script, &use);
|
||||
if (! (state->state == GRUB_PARSER_STATE_TEXT
|
||||
&& *state->script == ' '))
|
||||
{
|
||||
grub_dprintf ("scripting", "token=` '\n");
|
||||
if (! firstrun)
|
||||
doexit = 1;
|
||||
break;
|
||||
}
|
||||
state->state = newstate;
|
||||
nextchar (state);
|
||||
}
|
||||
grub_dprintf ("scripting", "token=` '\n");
|
||||
if (! firstrun)
|
||||
doexit = 1;
|
||||
break;
|
||||
case '{':
|
||||
case '}':
|
||||
case ';':
|
||||
case '\n':
|
||||
breakout = 1;
|
||||
{
|
||||
char c;
|
||||
grub_dprintf ("scripting", "token=`%c'\n", *state->script);
|
||||
c = *state->script;;
|
||||
nextchar (state);
|
||||
state->tokenonhold = c;
|
||||
doexit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (breakout)
|
||||
if (doexit)
|
||||
break;
|
||||
*(bp++) = use;
|
||||
}
|
||||
else if (use)
|
||||
*(bp++) = use;
|
||||
|
||||
state->state = newstate;
|
||||
nextchar (state);
|
||||
}
|
||||
|
||||
/* A string of text was read in. */
|
||||
*bp = '\0';
|
||||
grub_dprintf ("scripting", "token=`%s'\n", buffer);
|
||||
yylval->string = buffer;
|
||||
|
||||
/* Detect some special tokens. */
|
||||
if (! grub_strcmp (buffer, "while"))
|
||||
return GRUB_PARSER_TOKEN_WHILE;
|
||||
else if (! grub_strcmp (buffer, "if"))
|
||||
return GRUB_PARSER_TOKEN_IF;
|
||||
else if (! grub_strcmp (buffer, "function"))
|
||||
return GRUB_PARSER_TOKEN_FUNCTION;
|
||||
else if (! grub_strcmp (buffer, "menuentry"))
|
||||
return GRUB_PARSER_TOKEN_MENUENTRY;
|
||||
else if (! grub_strcmp (buffer, "@"))
|
||||
return GRUB_PARSER_TOKEN_MENUENTRY;
|
||||
else if (! grub_strcmp (buffer, "else"))
|
||||
return GRUB_PARSER_TOKEN_ELSE;
|
||||
else if (! grub_strcmp (buffer, "then"))
|
||||
return GRUB_PARSER_TOKEN_THEN;
|
||||
else if (! grub_strcmp (buffer, "fi"))
|
||||
return GRUB_PARSER_TOKEN_FI;
|
||||
else
|
||||
return GRUB_PARSER_TOKEN_NAME;
|
||||
}
|
||||
else if (newstate == GRUB_PARSER_STATE_VAR
|
||||
|| newstate == GRUB_PARSER_STATE_QVAR)
|
||||
{
|
||||
/* XXX: Use a better size. */
|
||||
buffer = grub_script_malloc (parsestate, 2096);
|
||||
if (! buffer)
|
||||
return 0;
|
||||
|
||||
bp = buffer;
|
||||
|
||||
/* This is a variable, read the variable name. */
|
||||
while (*state->script)
|
||||
{
|
||||
newstate = grub_parser_cmdline_state (state->state,
|
||||
*state->script, &use);
|
||||
|
||||
/* Check if this character is not part of the variable name
|
||||
anymore. */
|
||||
if (! (check_varstate (newstate)))
|
||||
/* XXX: Use a better size. */
|
||||
buffer = grub_script_malloc (parsestate, 2048);
|
||||
if (! buffer)
|
||||
return 0;
|
||||
|
||||
bp = buffer;
|
||||
|
||||
/* Read one token, possible quoted. */
|
||||
while (*state->script)
|
||||
{
|
||||
if (state->state == GRUB_PARSER_STATE_VARNAME2
|
||||
|| state->state == GRUB_PARSER_STATE_QVARNAME2)
|
||||
nextchar (state);
|
||||
newstate = grub_parser_cmdline_state (state->state,
|
||||
*state->script, &use);
|
||||
|
||||
/* Check if a variable name starts. */
|
||||
if (check_varstate (newstate))
|
||||
break;
|
||||
|
||||
/* If the string is not quoted or escaped, stop processing
|
||||
when a special token was found. It will be recognized
|
||||
next time when this function is called. */
|
||||
if (newstate == GRUB_PARSER_STATE_TEXT
|
||||
&& state->state != GRUB_PARSER_STATE_ESC)
|
||||
{
|
||||
int breakout = 0;
|
||||
|
||||
switch (use)
|
||||
{
|
||||
case ' ':
|
||||
case '{':
|
||||
case '}':
|
||||
case ';':
|
||||
case '\n':
|
||||
breakout = 1;
|
||||
}
|
||||
if (breakout)
|
||||
break;
|
||||
if (use)
|
||||
*(bp++) = use;
|
||||
}
|
||||
else if (use)
|
||||
*(bp++) = use;
|
||||
|
||||
state->state = newstate;
|
||||
break;
|
||||
nextchar (state);
|
||||
}
|
||||
|
||||
if (use)
|
||||
*(bp++) = use;
|
||||
nextchar (state);
|
||||
state->state = newstate;
|
||||
/* A string of text was read in. */
|
||||
*bp = '\0';
|
||||
grub_dprintf ("scripting", "token=`%s'\n", buffer);
|
||||
yylval->arg = grub_script_arg_add (parsestate, yylval->arg,
|
||||
GRUB_SCRIPT_ARG_TYPE_STR, buffer);
|
||||
|
||||
}
|
||||
|
||||
*bp = '\0';
|
||||
state->state = newstate;
|
||||
yylval->string = buffer;
|
||||
grub_dprintf ("scripting", "vartoken=`%s'\n", buffer);
|
||||
|
||||
return GRUB_PARSER_TOKEN_VAR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* There is either text or a variable name. In the case you
|
||||
else if (newstate == GRUB_PARSER_STATE_VAR
|
||||
|| newstate == GRUB_PARSER_STATE_QVAR)
|
||||
{
|
||||
/* XXX: Use a better size. */
|
||||
buffer = grub_script_malloc (parsestate, 2096);
|
||||
if (! buffer)
|
||||
return 0;
|
||||
|
||||
bp = buffer;
|
||||
|
||||
/* This is a variable, read the variable name. */
|
||||
while (*state->script)
|
||||
{
|
||||
newstate = grub_parser_cmdline_state (state->state,
|
||||
*state->script, &use);
|
||||
|
||||
/* Check if this character is not part of the variable name
|
||||
anymore. */
|
||||
if (! (check_varstate (newstate)))
|
||||
{
|
||||
if (state->state == GRUB_PARSER_STATE_VARNAME2
|
||||
|| state->state == GRUB_PARSER_STATE_QVARNAME2)
|
||||
nextchar (state);
|
||||
state->state = newstate;
|
||||
break;
|
||||
}
|
||||
|
||||
if (use)
|
||||
*(bp++) = use;
|
||||
nextchar (state);
|
||||
state->state = newstate;
|
||||
}
|
||||
|
||||
*bp = '\0';
|
||||
state->state = newstate;
|
||||
yylval->arg = grub_script_arg_add (parsestate, yylval->arg,
|
||||
GRUB_SCRIPT_ARG_TYPE_VAR, buffer);
|
||||
grub_dprintf ("scripting", "vartoken=`%s'\n", buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* There is either text or a variable name. In the case you
|
||||
arrive here there is a serious problem with the lexer. */
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "Internal error\n");
|
||||
return 0;
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "Internal error\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (yylval->arg == 0)
|
||||
{
|
||||
int token = state->tokenonhold;
|
||||
state->tokenonhold = 0;
|
||||
return token;
|
||||
}
|
||||
|
||||
if (yylval->arg->next == 0 && yylval->arg->type == GRUB_SCRIPT_ARG_TYPE_STR)
|
||||
{
|
||||
/* Detect some special tokens. */
|
||||
if (! grub_strcmp (yylval->arg->str, "while"))
|
||||
return GRUB_PARSER_TOKEN_WHILE;
|
||||
else if (! grub_strcmp (yylval->arg->str, "if"))
|
||||
return GRUB_PARSER_TOKEN_IF;
|
||||
else if (! grub_strcmp (yylval->arg->str, "function"))
|
||||
return GRUB_PARSER_TOKEN_FUNCTION;
|
||||
else if (! grub_strcmp (yylval->arg->str, "menuentry"))
|
||||
return GRUB_PARSER_TOKEN_MENUENTRY;
|
||||
else if (! grub_strcmp (yylval->arg->str, "@"))
|
||||
return GRUB_PARSER_TOKEN_MENUENTRY;
|
||||
else if (! grub_strcmp (yylval->arg->str, "else"))
|
||||
return GRUB_PARSER_TOKEN_ELSE;
|
||||
else if (! grub_strcmp (yylval->arg->str, "then"))
|
||||
return GRUB_PARSER_TOKEN_THEN;
|
||||
else if (! grub_strcmp (yylval->arg->str, "fi"))
|
||||
return GRUB_PARSER_TOKEN_FI;
|
||||
}
|
||||
|
||||
return GRUB_PARSER_TOKEN_ARG;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue