pulled-in func-params branch
This commit is contained in:
commit
c77d412448
13 changed files with 562 additions and 236 deletions
|
@ -660,7 +660,7 @@ normal_mod_CFLAGS = $(COMMON_CFLAGS)
|
|||
normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
||||
# For sh.mod.
|
||||
sh_mod_SOURCES = script/main.c script/script.c script/execute.c \
|
||||
sh_mod_SOURCES = script/main.c script/script.c script/argv.c script/execute.c \
|
||||
script/function.c script/lexer.c grub_script.tab.c grub_script.yy.c
|
||||
sh_mod_CFLAGS = $(COMMON_CFLAGS) $(POSIX_CFLAGS) -Wno-error
|
||||
sh_mod_LDFLAGS = $(COMMON_LDFLAGS)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -63,6 +63,13 @@ struct grub_script_arg
|
|||
struct grub_script_arg *next;
|
||||
};
|
||||
|
||||
/* An argument vector. */
|
||||
struct grub_script_argv
|
||||
{
|
||||
int argc;
|
||||
char **args;
|
||||
};
|
||||
|
||||
/* A complete argument. It consists of a list of one or more `struct
|
||||
grub_script_arg's. */
|
||||
struct grub_script_arglist
|
||||
|
@ -82,15 +89,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
|
||||
{
|
||||
|
@ -224,6 +222,11 @@ struct grub_parser_param
|
|||
struct grub_lexer_param *lexerstate;
|
||||
};
|
||||
|
||||
void grub_script_argv_free (struct grub_script_argv *argv);
|
||||
int grub_script_argv_next (struct grub_script_argv *argv);
|
||||
int grub_script_argv_append (struct grub_script_argv *argv, const char *s);
|
||||
int grub_script_argv_split_append (struct grub_script_argv *argv, char *s);
|
||||
|
||||
struct grub_script_arglist *
|
||||
grub_script_create_arglist (struct grub_parser_param *state);
|
||||
|
||||
|
@ -234,8 +237,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 +263,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 +302,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 +341,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);
|
||||
|
|
128
script/argv.c
Normal file
128
script/argv.c
Normal file
|
@ -0,0 +1,128 @@
|
|||
/* argv.c - methods for constructing argument vector */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <grub/mm.h>
|
||||
#include <grub/script_sh.h>
|
||||
|
||||
#define ARG_ALLOCATION_UNIT (32 * sizeof (char))
|
||||
#define ARGV_ALLOCATION_UNIT (8 * sizeof (void*))
|
||||
|
||||
void
|
||||
grub_script_argv_free (struct grub_script_argv *argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (argv->args)
|
||||
{
|
||||
for (i = 0; i < argv->argc; i++)
|
||||
grub_free (argv->args[i]);
|
||||
|
||||
grub_free (argv->args);
|
||||
}
|
||||
|
||||
argv->argc = 0;
|
||||
argv->args = 0;
|
||||
}
|
||||
|
||||
/* Prepare for next argc. */
|
||||
int
|
||||
grub_script_argv_next (struct grub_script_argv *argv)
|
||||
{
|
||||
char **p = argv->args;
|
||||
|
||||
if (argv->argc == 0)
|
||||
{
|
||||
p = grub_malloc (ALIGN_UP (2 * sizeof (char *), ARG_ALLOCATION_UNIT));
|
||||
if (! p)
|
||||
return 1;
|
||||
|
||||
argv->argc = 1;
|
||||
argv->args = p;
|
||||
argv->args[0] = 0;
|
||||
argv->args[1] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (! argv->args[argv->argc - 1])
|
||||
return 0;
|
||||
|
||||
p = grub_realloc (p, ALIGN_UP ((argv->argc + 1) * sizeof (char *),
|
||||
ARG_ALLOCATION_UNIT));
|
||||
if (! p)
|
||||
return 1;
|
||||
|
||||
argv->argc++;
|
||||
argv->args = p;
|
||||
argv->args[argv->argc] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Append `s' to the last argument. */
|
||||
int
|
||||
grub_script_argv_append (struct grub_script_argv *argv, const char *s)
|
||||
{
|
||||
int a, b;
|
||||
char *p = argv->args[argv->argc - 1];
|
||||
|
||||
if (! s)
|
||||
return 0;
|
||||
|
||||
a = p ? grub_strlen (p) : 0;
|
||||
b = grub_strlen (s);
|
||||
|
||||
p = grub_realloc (p, ALIGN_UP ((a + b + 1) * sizeof (char),
|
||||
ARG_ALLOCATION_UNIT));
|
||||
if (! p)
|
||||
return 1;
|
||||
|
||||
grub_strcpy (p + a, s);
|
||||
argv->args[argv->argc - 1] = p;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Split `s' and append words as multiple arguments. */
|
||||
int
|
||||
grub_script_argv_split_append (struct grub_script_argv *argv, char *s)
|
||||
{
|
||||
char ch;
|
||||
char *p;
|
||||
int errors = 0;
|
||||
|
||||
if (! s)
|
||||
return 0;
|
||||
|
||||
while (! errors && *s)
|
||||
{
|
||||
p = s;
|
||||
while (*s && ! grub_isspace (*s))
|
||||
s++;
|
||||
|
||||
ch = *s;
|
||||
*s = '\0';
|
||||
errors += grub_script_argv_append (argv, p);
|
||||
*s = ch;
|
||||
|
||||
while (*s && grub_isspace (*s))
|
||||
s++;
|
||||
|
||||
if (*s)
|
||||
errors += grub_script_argv_next (argv);
|
||||
}
|
||||
return errors;
|
||||
}
|
379
script/execute.c
379
script/execute.c
|
@ -30,6 +30,182 @@
|
|||
is sizeof (int) * 3, and one extra for a possible -ve sign. */
|
||||
#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
|
||||
|
||||
/* Scope for grub script functions. */
|
||||
struct grub_script_scope
|
||||
{
|
||||
struct grub_script_argv argv;
|
||||
};
|
||||
static struct grub_script_scope *scope = 0;
|
||||
|
||||
static int
|
||||
grub_env_special (const char *name)
|
||||
{
|
||||
if (grub_isdigit (name[0]) ||
|
||||
grub_strcmp (name, "#") == 0 ||
|
||||
grub_strcmp (name, "*") == 0 ||
|
||||
grub_strcmp (name, "@") == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char **
|
||||
grub_script_env_get (const char *name, grub_script_arg_type_t type)
|
||||
{
|
||||
int errors = 0;
|
||||
struct grub_script_argv result = { 0, 0 };
|
||||
|
||||
errors += grub_script_argv_next (&result);
|
||||
if (! grub_env_special (name))
|
||||
{
|
||||
char *v = grub_env_get (name);
|
||||
if (v && v[0])
|
||||
{
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
errors += grub_script_argv_split_append (&result, v);
|
||||
else
|
||||
errors += grub_script_argv_append (&result, v);
|
||||
}
|
||||
}
|
||||
else if (! scope)
|
||||
errors += grub_script_argv_append (&result, 0);
|
||||
|
||||
else if (grub_strcmp (name, "#") == 0)
|
||||
{
|
||||
char buffer[ERRNO_DIGITS_MAX + 1];
|
||||
grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
|
||||
errors += grub_script_argv_append (&result, buffer);
|
||||
}
|
||||
else if (grub_strcmp (name, "*") == 0)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; ! errors && i < scope->argv.argc; i++)
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
{
|
||||
if (i != 0)
|
||||
errors += grub_script_argv_next (&result);
|
||||
errors += grub_script_argv_split_append (&result,
|
||||
scope->argv.args[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i != 0)
|
||||
errors += grub_script_argv_append (&result, " ");
|
||||
errors += grub_script_argv_append (&result,
|
||||
scope->argv.args[i]);
|
||||
}
|
||||
}
|
||||
else if (grub_strcmp (name, "@") == 0)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; ! errors && i < scope->argv.argc; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
errors += grub_script_argv_next (&result);
|
||||
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
errors += grub_script_argv_split_append (&result,
|
||||
scope->argv.args[i]);
|
||||
else
|
||||
errors += grub_script_argv_append (&result,
|
||||
scope->argv.args[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned long num = grub_strtoul (name, 0, 10);
|
||||
if (num == 0)
|
||||
; /* XXX no file name, for now. */
|
||||
|
||||
else if (num <= scope->argv.argc)
|
||||
{
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
errors += grub_script_argv_split_append (&result,
|
||||
scope->argv.args[num - 1]);
|
||||
else
|
||||
errors += grub_script_argv_append (&result,
|
||||
scope->argv.args[num - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (errors)
|
||||
{
|
||||
grub_script_argv_free (&result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result.args;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_script_env_set (const char *name, const char *val)
|
||||
{
|
||||
if (grub_env_special (name))
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad variable name");
|
||||
|
||||
return grub_env_set (name, val);
|
||||
}
|
||||
|
||||
/* Expand arguments in ARGLIST into multiple arguments. */
|
||||
static int
|
||||
grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
|
||||
struct grub_script_argv *argv)
|
||||
{
|
||||
int i;
|
||||
int error = 0;
|
||||
char **values = 0;
|
||||
struct grub_script_arg *arg = 0;
|
||||
struct grub_script_argv result = { 0, 0 };
|
||||
|
||||
for (; error == 0 && arglist && arglist->arg; arglist = arglist->next)
|
||||
{
|
||||
error += grub_script_argv_next (&result);
|
||||
|
||||
arg = arglist->arg;
|
||||
while (arg)
|
||||
{
|
||||
if (error)
|
||||
break;
|
||||
|
||||
switch (arg->type)
|
||||
{
|
||||
case GRUB_SCRIPT_ARG_TYPE_VAR:
|
||||
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
|
||||
values = grub_script_env_get (arg->str, arg->type);
|
||||
for (i = 0; values && values[i]; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
error += grub_script_argv_next (&result);
|
||||
error += grub_script_argv_append (&result, values[i]);
|
||||
}
|
||||
grub_free (values);
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_TEXT:
|
||||
if (grub_strlen (arg->str))
|
||||
error += grub_script_argv_append (&result, arg->str);
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
|
||||
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
|
||||
error += grub_script_argv_append (&result, arg->str);
|
||||
break;
|
||||
}
|
||||
arg = arg->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (error)
|
||||
return 1;
|
||||
|
||||
if (! result.args[result.argc - 1])
|
||||
result.argc--;
|
||||
|
||||
*argv = result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_script_execute_cmd (struct grub_script_cmd *cmd)
|
||||
{
|
||||
|
@ -46,149 +222,24 @@ grub_script_execute_cmd (struct grub_script_cmd *cmd)
|
|||
return ret;
|
||||
}
|
||||
|
||||
#define ARG_ALLOCATION_UNIT (32 * sizeof (char))
|
||||
#define ARGV_ALLOCATION_UNIT (8 * sizeof (void*))
|
||||
|
||||
/* Expand arguments in ARGLIST into multiple arguments. */
|
||||
char **
|
||||
grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count)
|
||||
/* Execute a function call. */
|
||||
grub_err_t
|
||||
grub_script_function_call (grub_script_function_t func, int argc, char **args)
|
||||
{
|
||||
int i;
|
||||
int oom;
|
||||
int argc;
|
||||
int empty;
|
||||
char *ptr;
|
||||
char **argv;
|
||||
char *value;
|
||||
struct grub_script_arg *arg;
|
||||
grub_err_t ret = 0;
|
||||
struct grub_script_scope *old_scope;
|
||||
struct grub_script_scope new_scope;
|
||||
|
||||
auto void push (char *str);
|
||||
void push (char *str)
|
||||
{
|
||||
char **p;
|
||||
new_scope.argv.argc = argc;
|
||||
new_scope.argv.args = args;
|
||||
|
||||
if (oom)
|
||||
return;
|
||||
old_scope = scope;
|
||||
scope = &new_scope;
|
||||
|
||||
p = grub_realloc (argv, ALIGN_UP (sizeof(char*) * (argc + 1), ARGV_ALLOCATION_UNIT));
|
||||
if (!p)
|
||||
oom = 1;
|
||||
else
|
||||
{
|
||||
p[argc++] = str;
|
||||
argv = p;
|
||||
}
|
||||
}
|
||||
ret = grub_script_execute (func->func);
|
||||
|
||||
auto char* append (const char *str, grub_size_t nchar);
|
||||
char* append (const char *str, grub_size_t nchar)
|
||||
{
|
||||
int len;
|
||||
int old;
|
||||
char *p;
|
||||
|
||||
if (oom || !str)
|
||||
return 0;
|
||||
|
||||
len = nchar ?: grub_strlen (str);
|
||||
old = argv[argc - 1] ? grub_strlen (argv[argc - 1]) : 0;
|
||||
p = grub_realloc (argv[argc - 1], ALIGN_UP(old + len + 1, ARG_ALLOCATION_UNIT));
|
||||
|
||||
if (p)
|
||||
{
|
||||
grub_strncpy (p + old, str, len);
|
||||
p[old + len] = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
oom = 1;
|
||||
grub_free (argv[argc - 1]);
|
||||
}
|
||||
argv[argc - 1] = p;
|
||||
return argv[argc - 1];
|
||||
}
|
||||
|
||||
/* Move *STR to the begining of next word, but return current word. */
|
||||
auto char* move_to_next (char **str);
|
||||
char* move_to_next (char **str)
|
||||
{
|
||||
char *end;
|
||||
char *start;
|
||||
|
||||
if (oom || !str || !*str)
|
||||
return 0;
|
||||
|
||||
start = *str;
|
||||
while (*start && grub_isspace (*start)) start++;
|
||||
if (*start == '\0')
|
||||
return 0;
|
||||
|
||||
end = start + 1;
|
||||
while (*end && !grub_isspace (*end)) end++;
|
||||
|
||||
*str = end;
|
||||
return start;
|
||||
}
|
||||
|
||||
oom = 0;
|
||||
argv = 0;
|
||||
argc = 0;
|
||||
push (0);
|
||||
for (; arglist; arglist = arglist->next)
|
||||
{
|
||||
empty = 1;
|
||||
arg = arglist->arg;
|
||||
while (arg)
|
||||
{
|
||||
switch (arg->type)
|
||||
{
|
||||
case GRUB_SCRIPT_ARG_TYPE_VAR:
|
||||
value = grub_env_get (arg->str);
|
||||
while (value && *value && (ptr = move_to_next(&value)))
|
||||
{
|
||||
empty = 0;
|
||||
append (ptr, value - ptr);
|
||||
if (*value) push(0);
|
||||
}
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_TEXT:
|
||||
if (grub_strlen (arg->str) > 0)
|
||||
{
|
||||
empty = 0;
|
||||
append (arg->str, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
|
||||
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
|
||||
empty = 0;
|
||||
append (arg->str, 0);
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
|
||||
empty = 0;
|
||||
append (grub_env_get (arg->str), 0);
|
||||
break;
|
||||
}
|
||||
arg = arg->next;
|
||||
}
|
||||
if (!empty)
|
||||
push (0);
|
||||
}
|
||||
|
||||
if (oom)
|
||||
{
|
||||
for (i = 0; i < argc; i++)
|
||||
grub_free (argv[i]);
|
||||
grub_free (argv);
|
||||
argv = 0;
|
||||
}
|
||||
|
||||
if (argv)
|
||||
*count = argc - 1;
|
||||
|
||||
return argv;
|
||||
scope = old_scope;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Execute a single command line. */
|
||||
|
@ -196,21 +247,18 @@ grub_err_t
|
|||
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
|
||||
{
|
||||
struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
|
||||
char **args = 0;
|
||||
int i = 0;
|
||||
grub_command_t grubcmd;
|
||||
grub_err_t ret = 0;
|
||||
int argcount = 0;
|
||||
grub_script_function_t func = 0;
|
||||
char errnobuf[18];
|
||||
char *cmdname;
|
||||
struct grub_script_argv argv = { 0, 0 };
|
||||
|
||||
/* Lookup the command. */
|
||||
args = grub_script_execute_arglist_to_argv (cmdline->arglist, &argcount);
|
||||
if (!args)
|
||||
if (grub_script_arglist_to_argv (cmdline->arglist, &argv))
|
||||
return grub_errno;
|
||||
|
||||
cmdname = args[0];
|
||||
cmdname = argv.args[0];
|
||||
grubcmd = grub_command_find (cmdname);
|
||||
if (! grubcmd)
|
||||
{
|
||||
|
@ -232,7 +280,7 @@ 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);
|
||||
|
||||
|
@ -247,14 +295,12 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
|
|||
|
||||
/* Execute the GRUB command or function. */
|
||||
if (grubcmd)
|
||||
ret = (grubcmd->func) (grubcmd, argcount - 1, args + 1);
|
||||
ret = (grubcmd->func) (grubcmd, argv.argc - 1, argv.args + 1);
|
||||
else
|
||||
ret = grub_script_function_call (func, argcount - 1, args + 1);
|
||||
ret = grub_script_function_call (func, argv.argc - 1, argv.args + 1);
|
||||
|
||||
/* Free arguments. */
|
||||
for (i = 0; i < argcount; i++)
|
||||
grub_free (args[i]);
|
||||
grub_free (args);
|
||||
grub_script_argv_free (&argv);
|
||||
|
||||
if (grub_errno == GRUB_ERR_TEST_FAILURE)
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
|
@ -269,13 +315,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;
|
||||
|
@ -309,23 +355,20 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
|
|||
{
|
||||
int i;
|
||||
int result;
|
||||
char **args;
|
||||
int argcount;
|
||||
struct grub_script_argv argv;
|
||||
struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
|
||||
|
||||
args = grub_script_execute_arglist_to_argv (cmdfor->words, &argcount);
|
||||
if (!args)
|
||||
if (grub_script_arglist_to_argv (cmdfor->words, &argv))
|
||||
return grub_errno;
|
||||
|
||||
result = 0;
|
||||
for (i = 0; i < argcount; i++)
|
||||
for (i = 0; i < argv.argc; i++)
|
||||
{
|
||||
grub_env_set (cmdfor->name->str, args[i]);
|
||||
grub_script_env_set (cmdfor->name->str, argv.args[i]);
|
||||
result = grub_script_execute_cmd (cmdfor->list);
|
||||
grub_free (args[i]);
|
||||
}
|
||||
|
||||
grub_free (args);
|
||||
grub_script_argv_free (&argv);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -354,26 +397,20 @@ grub_err_t
|
|||
grub_script_execute_menuentry (struct grub_script_cmd *cmd)
|
||||
{
|
||||
struct grub_script_cmd_menuentry *cmd_menuentry;
|
||||
char **args = 0;
|
||||
int argcount = 0;
|
||||
int i = 0;
|
||||
struct grub_script_argv argv = {0, 0};
|
||||
|
||||
cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;
|
||||
|
||||
if (cmd_menuentry->arglist)
|
||||
{
|
||||
args = grub_script_execute_arglist_to_argv (cmd_menuentry->arglist, &argcount);
|
||||
if (!args)
|
||||
if (grub_script_arglist_to_argv (cmd_menuentry->arglist, &argv))
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
grub_normal_add_menu_entry (argcount, (const char **) args,
|
||||
grub_normal_add_menu_entry (argv.argc, (const char **) argv.args,
|
||||
cmd_menuentry->sourcecode);
|
||||
|
||||
/* Free arguments. */
|
||||
for (i = 0; i < argcount; i++)
|
||||
grub_free (args[i]);
|
||||
grub_free (args);
|
||||
grub_script_argv_free (&argv);
|
||||
|
||||
return grub_errno;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -16,6 +16,33 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# simple arguments
|
||||
echo one two three
|
||||
echo "one two three"
|
||||
echo 'one two three'
|
||||
|
||||
# empty arguments
|
||||
echo a "" b
|
||||
echo a '' b
|
||||
|
||||
echo a $foo b
|
||||
echo a ${foo} b
|
||||
|
||||
echo a "$foo" b
|
||||
echo a "${foo}" b
|
||||
|
||||
# multi-part arguments
|
||||
echo one"two"three
|
||||
echo one${two}three
|
||||
echo one"two"$three
|
||||
|
||||
echo one'two'three
|
||||
echo one${two}three
|
||||
echo one'two'$three
|
||||
|
||||
echo one'two'three"four"five${six}seven$eight
|
||||
|
||||
|
||||
foo=bar
|
||||
echo $foo ${foo}
|
||||
echo "$foo" "${foo}"
|
||||
|
|
147
tests/grub_script_functions.in
Normal file
147
tests/grub_script_functions.in
Normal file
|
@ -0,0 +1,147 @@
|
|||
#! @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
|
||||
fcount a
|
||||
fcount a b
|
||||
|
||||
echo parameter count, with nesting
|
||||
function ffcount {
|
||||
echo ffcount "$#"
|
||||
fcount
|
||||
fcount a
|
||||
fcount a b
|
||||
}
|
||||
|
||||
ffcount
|
||||
ffcount 1
|
||||
ffcount 1 2
|
||||
|
||||
echo parameters
|
||||
function fparam {
|
||||
echo fparam 1 $1
|
||||
echo fparam 2 $2
|
||||
echo fparam 3 $3
|
||||
}
|
||||
|
||||
fparam
|
||||
fparam a
|
||||
fparam a b
|
||||
|
||||
echo parameters, with nesting
|
||||
function ffparam {
|
||||
echo ffparam 1 $1
|
||||
echo ffparam 2 $2
|
||||
echo ffparam 3 $3
|
||||
fparam
|
||||
fparam a
|
||||
fparam a b
|
||||
}
|
||||
|
||||
ffparam
|
||||
ffparam 1
|
||||
ffparam 1 2
|
||||
|
||||
echo parameter expansion with specials
|
||||
function fstar {
|
||||
for f in $*
|
||||
do
|
||||
echo fstar $f
|
||||
done
|
||||
|
||||
for f in aaa$*bbb
|
||||
do
|
||||
echo fstar $f
|
||||
done
|
||||
}
|
||||
|
||||
fstar
|
||||
fstar a
|
||||
fstar a "1 2"
|
||||
fstar a "1 2" b
|
||||
|
||||
function fdqstar {
|
||||
for f in "$*"
|
||||
do
|
||||
echo fdqstar $f
|
||||
done
|
||||
|
||||
for f in aaa"$*"bbb
|
||||
do
|
||||
echo fdqstar $f
|
||||
done
|
||||
|
||||
for f in "aaa$*bbb"
|
||||
do
|
||||
echo fdqstar $f
|
||||
done
|
||||
}
|
||||
|
||||
fdqstar
|
||||
fdqstar a
|
||||
fdqstar a "1 2"
|
||||
fdqstar a "1 2" b
|
||||
|
||||
function fat {
|
||||
for f in $@
|
||||
do
|
||||
echo fat $f
|
||||
done
|
||||
|
||||
for f in aaa$@bbb
|
||||
do
|
||||
echo fat $f
|
||||
done
|
||||
}
|
||||
|
||||
fat
|
||||
fat a
|
||||
fat a "1 2"
|
||||
fat a "1 2" b
|
||||
fat a "1 2" b "c d"
|
||||
fat a "1 2" b "c d" e
|
||||
|
||||
function fdqat {
|
||||
for f in "$@"
|
||||
do
|
||||
echo fdqat $f
|
||||
done
|
||||
|
||||
for f in aaa"$@"bbb
|
||||
do
|
||||
echo fdqat $f
|
||||
done
|
||||
|
||||
for f in "aaa$@bbb"
|
||||
do
|
||||
echo fdqat $f
|
||||
done
|
||||
}
|
||||
|
||||
# fdqat # this case needs special handling, lets ignore till we really need it.
|
||||
fdqat a
|
||||
fdqat a "1 2"
|
||||
fdqat a "1 2" b
|
||||
fdqat a "1 2" b "c d"
|
||||
fdqat a "1 2" b "c d" e
|
||||
|
|
@ -28,7 +28,7 @@ foo=foo
|
|||
echo "" $foo
|
||||
|
||||
echo $bar $foo
|
||||
|
||||
|
||||
bar=""
|
||||
echo $bar $foo
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue