pulled-in func-params branch

This commit is contained in:
BVK Chaitanya 2010-05-07 08:38:08 +05:30
commit 7897070571
9 changed files with 182 additions and 72 deletions

View file

@ -71,6 +71,9 @@ grub_script_dollar_SOURCES = tests/grub_script_dollar.in
check_SCRIPTS += grub_script_comments
grub_script_comments_SOURCES = tests/grub_script_comments.in
check_SCRIPTS += grub_script_functions
grub_script_functions_SOURCES = tests/grub_script_functions.in
# List of tests to execute on "make check"
# SCRIPTED_TESTS = example_scripted_test
# SCRIPTED_TESTS += example_grub_script_test
@ -87,6 +90,7 @@ SCRIPTED_TESTS += grub_script_blanklines
SCRIPTED_TESTS += grub_script_final_semicolon
SCRIPTED_TESTS += grub_script_dollar
SCRIPTED_TESTS += grub_script_comments
SCRIPTED_TESTS += grub_script_functions
# dependencies between tests and testing-tools
$(SCRIPTED_TESTS): grub-shell grub-shell-tester

View file

@ -73,6 +73,15 @@ struct grub_script_arglist
int argcount;
};
/* Scope for grub script constructs. */
struct grub_script_scope
{
struct grub_script_scope *next;
char **args;
unsigned int argc;
};
/* A single command line. */
struct grub_script_cmdline
{
@ -82,15 +91,6 @@ struct grub_script_cmdline
struct grub_script_arglist *arglist;
};
/* A block of commands, this can be used to group commands. */
struct grub_script_cmdblock
{
struct grub_script_cmd cmd;
/* A chain of commands. */
struct grub_script_cmd *cmdlist;
};
/* An if statement. */
struct grub_script_cmdif
{
@ -234,8 +234,6 @@ grub_script_add_arglist (struct grub_parser_param *state,
struct grub_script_cmd *
grub_script_create_cmdline (struct grub_parser_param *state,
struct grub_script_arglist *arglist);
struct grub_script_cmd *
grub_script_create_cmdblock (struct grub_parser_param *state);
struct grub_script_cmd *
grub_script_create_cmdif (struct grub_parser_param *state,
@ -262,9 +260,9 @@ grub_script_create_cmdmenu (struct grub_parser_param *state,
int options);
struct grub_script_cmd *
grub_script_add_cmd (struct grub_parser_param *state,
struct grub_script_cmdblock *cmdblock,
struct grub_script_cmd *cmd);
grub_script_append_cmd (struct grub_parser_param *state,
struct grub_script_cmd *list,
struct grub_script_cmd *last);
struct grub_script_arg *
grub_script_arg_add (struct grub_parser_param *state,
struct grub_script_arg *arg,
@ -301,7 +299,7 @@ void grub_script_yyerror (struct grub_parser_param *, char const *);
/* Commands to execute, don't use these directly. */
grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdblock (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdlist (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdfor (struct grub_script_cmd *cmd);
grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
@ -340,8 +338,8 @@ grub_script_function_t grub_script_function_create (struct grub_script_arg *func
void grub_script_function_remove (const char *name);
grub_script_function_t grub_script_function_find (char *functionname);
int grub_script_function_iterate (int (*iterate) (grub_script_function_t));
int grub_script_function_call (grub_script_function_t func,
int argc, char **args);
grub_err_t grub_script_function_call (grub_script_function_t func,
int argc, char **args);
char **
grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count);

View file

@ -30,6 +30,52 @@
is sizeof (int) * 3, and one extra for a possible -ve sign. */
#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
static struct grub_script_scope *scope = 0;
static char *
grub_script_env_get (const char *name)
{
char *p = 0;
unsigned long num = 0;
if (! scope)
return grub_env_get (name);
if (grub_isdigit (name[0]))
{
num = grub_strtoul (name, &p, 10);
if (p && *p == '\0')
{
if (num == 0)
return 0; /* XXX no file name, for now. */
return (num > scope->argc ? 0 : scope->args[num - 1]);
}
else
{
grub_error (GRUB_ERR_BAD_ARGUMENT, "bad variabe name substitution");
return 0;
}
}
else if (grub_strcmp (name, "#") == 0)
{
static char buf[32]; /* Rewritten everytime. */
grub_snprintf (buf, sizeof (buf), "%u", scope->argc);
return buf;
}
else
return grub_env_get (name);
}
static grub_err_t
grub_script_env_set (const char *name, const char *val)
{
if (grub_isdigit (name[0]) || grub_strcmp (name, "#") == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad variable name");
return grub_env_set (name, val);
}
static grub_err_t
grub_script_execute_cmd (struct grub_script_cmd *cmd)
{
@ -143,7 +189,7 @@ grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *c
switch (arg->type)
{
case GRUB_SCRIPT_ARG_TYPE_VAR:
value = grub_env_get (arg->str);
value = grub_script_env_get (arg->str);
while (value && *value && (ptr = move_to_next(&value)))
{
empty = 0;
@ -168,7 +214,7 @@ grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *c
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
empty = 0;
append (grub_env_get (arg->str), 0);
append (grub_script_env_get (arg->str), 0);
break;
}
arg = arg->next;
@ -191,6 +237,23 @@ grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *c
return argv;
}
/* Execute a function call. */
grub_err_t
grub_script_function_call (grub_script_function_t func, int argc, char **args)
{
grub_err_t ret = 0;
struct grub_script_scope new_scope;
new_scope.argc = argc;
new_scope.args = args;
grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope));
ret = grub_script_execute (func->func);
grub_list_pop (GRUB_AS_LIST_P (&scope));
return ret;
}
/* Execute a single command line. */
grub_err_t
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
@ -232,12 +295,12 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
/* Create two strings and set the variable. */
*eq = '\0';
eq++;
grub_env_set (assign, eq);
grub_script_env_set (assign, eq);
}
grub_free (assign);
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
grub_env_set ("?", errnobuf);
grub_script_env_set ("?", errnobuf);
grub_print_error ();
@ -269,13 +332,13 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
/* Execute a block of one or more commands. */
grub_err_t
grub_script_execute_cmdblock (struct grub_script_cmd *cmd)
grub_script_execute_cmdlist (struct grub_script_cmd *list)
{
int ret = 0;
struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd;
struct grub_script_cmd *cmd;
/* Loop over every command and execute it. */
for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next)
for (cmd = list->next; cmd; cmd = cmd->next)
ret = grub_script_execute_cmd (cmd);
return ret;
@ -291,7 +354,7 @@ grub_script_execute_cmdif (struct grub_script_cmd *cmd)
/* Check if the commands results in a true or a false. The value is
read from the env variable `?'. */
grub_script_execute_cmd (cmdif->exec_to_evaluate);
result = grub_env_get ("?");
result = grub_script_env_get ("?");
grub_errno = GRUB_ERR_NONE;
@ -320,7 +383,7 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
result = 0;
for (i = 0; i < argcount; i++)
{
grub_env_set (cmdfor->name->str, args[i]);
grub_script_env_set (cmdfor->name->str, args[i]);
result = grub_script_execute_cmd (cmdfor->list);
grub_free (args[i]);
}

View file

@ -115,12 +115,3 @@ grub_script_function_iterate (int (*iterate) (grub_script_function_t))
return 0;
}
int
grub_script_function_call (grub_script_function_t func,
int argc __attribute__((unused)),
char **args __attribute__((unused)))
{
/* XXX: Arguments are not supported yet. */
return grub_script_execute (func->func);
}

View file

@ -96,9 +96,7 @@ script: newlines0
}
| script statement delimiter newlines0
{
struct grub_script_cmdblock *cmdblock;
cmdblock = (struct grub_script_cmdblock *) $1;
$$ = grub_script_add_cmd (state, cmdblock, $2);
$$ = grub_script_append_cmd (state, $1, $2);
}
| error
{
@ -183,13 +181,11 @@ command: grubcmd { $$ = $1; }
/* A list of commands. */
commands1: newlines0 command
{
$$ = grub_script_add_cmd (state, 0, $2);
$$ = grub_script_append_cmd (state, 0, $2);
}
| commands1 delimiters1 command
{
struct grub_script_cmdblock *cmdblock;
cmdblock = (struct grub_script_cmdblock *) $1;
$$ = grub_script_add_cmd (state, cmdblock, $3);
$$ = grub_script_append_cmd (state, $1, $3);
}
;

View file

@ -291,46 +291,40 @@ grub_script_create_cmdmenu (struct grub_parser_param *state,
return (struct grub_script_cmd *) cmd;
}
/* Create a block of commands. CMD contains the command that should
be added at the end of CMDBLOCK's list. If CMDBLOCK is zero, a new
cmdblock will be created. */
/* Create a chain of commands. LAST contains the command that should
be added at the end of LIST's list. If LIST is zero, a new list
will be created. */
struct grub_script_cmd *
grub_script_add_cmd (struct grub_parser_param *state,
struct grub_script_cmdblock *cmdblock,
struct grub_script_cmd *cmd)
grub_script_append_cmd (struct grub_parser_param *state,
struct grub_script_cmd *list,
struct grub_script_cmd *last)
{
struct grub_script_cmd *ptr;
grub_dprintf ("scripting", "cmdblock\n");
grub_dprintf ("scripting", "append command\n");
if (!cmd)
return (struct grub_script_cmd *) cmdblock;
if (! last)
return list;
if (!cmdblock)
if (! list)
{
cmdblock = grub_script_malloc (state, sizeof (*cmdblock));
if (!cmdblock)
list = grub_script_malloc (state, sizeof (*list));
if (! list)
return 0;
cmdblock->cmd.exec = grub_script_execute_cmdblock;
cmdblock->cmd.next = 0;
cmdblock->cmdlist = cmd;
cmd->next = 0;
list->exec = grub_script_execute_cmdlist;
list->next = last;
}
else
{
if (!cmdblock->cmdlist)
cmdblock->cmdlist = cmd;
else
{
ptr = cmdblock->cmdlist;
while (ptr->next)
ptr = ptr->next;
ptr->next = cmd;
}
ptr = list;
while (ptr->next)
ptr = ptr->next;
ptr->next = last;
}
return (struct grub_script_cmd *) cmdblock;
return list;
}

View file

@ -119,7 +119,8 @@ DIGITS [[:digit:]]+
NAME [[:alpha:]_][[:alnum:][:digit:]_]*
ESC \\.
VARIABLE ${NAME}|$\{{NAME}\}|${DIGITS}|$\{{DIGITS}\}|$\?|$\{\?\}
SPECIAL \?|\#
VARIABLE ${NAME}|$\{{NAME}\}|${DIGITS}|$\{{DIGITS}\}|${SPECIAL}|$\{{SPECIAL}\}
DQSTR \"([^\\\"]|{ESC})*\"
SQSTR \'[^\']*\'
WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
@ -221,7 +222,7 @@ WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
}
<VAR>{
\? |
{SPECIAL} |
{DIGITS} |
{NAME} {
COPY (yytext, yyleng);
@ -231,7 +232,7 @@ WORD ({CHAR}|{DQSTR}|{SQSTR}|{ESC}|{VARIABLE})+
else
ARG (GRUB_SCRIPT_ARG_TYPE_DQVAR);
}
\{\?\} |
\{{SPECIAL}\} |
\{{DIGITS}\} |
\{{NAME}\} {
yytext[yyleng - 1] = '\0';

View file

@ -0,0 +1,63 @@
#! @builddir@/grub-shell-tester
# Run GRUB script in a Qemu instance
# Copyright (C) 2010 Free Software Foundation, Inc.
#
# GRUB 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.
#
# GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
echo parameter count
function fcount {
echo "$#"
}
fcount
fcount a
fcount a b
echo parameter count, with nesting
function ffcount {
echo "$#"
fcount
fcount a
fcount a b
}
ffcount
ffcount 1
ffcount 1 2
echo parameters
function fparam {
echo 1 $1
echo 2 $2
echo 3 $3
}
fparam
fparam a
fparam a b
echo parameters, with nesting
function ffparam {
echo 1 $1
echo 2 $2
echo 3 $3
fparam
fparam a
fparam a b
}
ffparam
ffparam 1
ffparam 1 2

View file

@ -70,7 +70,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)
}
grub_err_t
grub_script_execute_cmdblock (struct grub_script_cmd *cmd __attribute__ ((unused)))
grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
{
return 0;
}