Function parameters support to GRUB script.
* script/yylex.l (VARIABLE): Regular expression update. * script/function.c (grub_script_function_call): Moved ... * script/execute.c (grub_script_function_call): ... to here. (grub_script_execute_arglist_to_argv): Removed. (grub_script_arglist_to_argv): New function. * script/argv.c: New file. (grub_script_argv_free): New function. (grub_script_argv_next): Likewise. (grub_script_argv_append): Likewise. (grub_script_argv_split_append): Likewise. * include/grub/script_sh.h (grub_script_argv): New struct. (grub_script_argv_free): New function. (grub_script_argv_next): Likewise. (grub_script_argv_append): Likewise. (grub_script_argv_split_append): Likewise. * conf/common.rmk (normal.mod): New source script/argv.c. * tests/grub_script_echo1.in: More tests. * tests/grub_script_vars1.in: Likewise. * tests/grub_script_functions.in: New test case. * conf/tests.rmk: Rules for new testcase.
This commit is contained in:
commit
f12c842082
11 changed files with 584 additions and 186 deletions
27
ChangeLog
27
ChangeLog
|
@ -1,3 +1,30 @@
|
|||
2010-08-12 BVK Chaitanya <bvk.groups@gmail.com>
|
||||
|
||||
Function parameters support to GRUB script.
|
||||
|
||||
* script/yylex.l (VARIABLE): Regular expression update.
|
||||
* script/function.c (grub_script_function_call): Moved ...
|
||||
* script/execute.c (grub_script_function_call): ... to here.
|
||||
(grub_script_execute_arglist_to_argv): Removed.
|
||||
(grub_script_arglist_to_argv): New function.
|
||||
* script/argv.c: New file.
|
||||
(grub_script_argv_free): New function.
|
||||
(grub_script_argv_next): Likewise.
|
||||
(grub_script_argv_append): Likewise.
|
||||
(grub_script_argv_split_append): Likewise.
|
||||
* include/grub/script_sh.h (grub_script_argv): New struct.
|
||||
(grub_script_argv_free): New function.
|
||||
(grub_script_argv_next): Likewise.
|
||||
(grub_script_argv_append): Likewise.
|
||||
(grub_script_argv_split_append): Likewise.
|
||||
|
||||
* conf/common.rmk (normal.mod): New source script/argv.c.
|
||||
|
||||
* tests/grub_script_echo1.in: More tests.
|
||||
* tests/grub_script_vars1.in: Likewise.
|
||||
* tests/grub_script_functions.in: New test case.
|
||||
* conf/tests.rmk: Rules for new testcase.
|
||||
|
||||
2010-08-12 BVK Chaitanya <bvk.groups@gmail.com>
|
||||
|
||||
Remove grub_script_cmdblock struct.
|
||||
|
|
|
@ -636,7 +636,7 @@ normal_mod_SOURCES = normal/main.c normal/cmdline.c normal/dyncmd.c \
|
|||
normal/color.c normal/completion.c normal/datetime.c normal/menu.c \
|
||||
normal/menu_entry.c normal/menu_text.c normal/charset.c \
|
||||
normal/misc.c normal/crypto.c normal/term.c normal/context.c \
|
||||
script/main.c script/script.c script/execute.c unidata.c \
|
||||
script/main.c script/script.c script/execute.c script/argv.c unidata.c \
|
||||
script/function.c script/lexer.c grub_script.tab.c grub_script.yy.c
|
||||
normal_mod_CFLAGS = $(COMMON_CFLAGS) $(POSIX_CFLAGS) -Wno-error
|
||||
normal_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
|
||||
{
|
||||
unsigned argc;
|
||||
char **args;
|
||||
};
|
||||
|
||||
/* A complete argument. It consists of a list of one or more `struct
|
||||
grub_script_arg's. */
|
||||
struct grub_script_arglist
|
||||
|
@ -215,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);
|
||||
|
||||
|
@ -333,7 +345,8 @@ grub_script_function_t grub_script_function_create (struct grub_script_arg *func
|
|||
struct grub_script *cmd);
|
||||
void grub_script_function_remove (const char *name);
|
||||
grub_script_function_t grub_script_function_find (char *functionname);
|
||||
int grub_script_function_call (grub_script_function_t func,
|
||||
|
||||
grub_err_t grub_script_function_call (grub_script_function_t func,
|
||||
int argc, char **args);
|
||||
|
||||
char **
|
||||
|
|
133
script/argv.c
Normal file
133
script/argv.c
Normal file
|
@ -0,0 +1,133 @@
|
|||
/* 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>
|
||||
|
||||
/* Return nearest power of two that is >= v. */
|
||||
static unsigned
|
||||
round_up_exp (unsigned v)
|
||||
{
|
||||
v--;
|
||||
v |= v >> 1;
|
||||
v |= v >> 2;
|
||||
v |= v >> 4;
|
||||
v |= v >> 8;
|
||||
v |= v >> 16;
|
||||
|
||||
if (sizeof (v) > 4)
|
||||
v |= v >> 32;
|
||||
|
||||
v++;
|
||||
v += (v == 0);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
void
|
||||
grub_script_argv_free (struct grub_script_argv *argv)
|
||||
{
|
||||
unsigned 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->args && argv->args[argv->argc - 1] == 0)
|
||||
return 0;
|
||||
|
||||
p = grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *)));
|
||||
if (! p)
|
||||
return 1;
|
||||
|
||||
argv->argc++;
|
||||
argv->args = p;
|
||||
|
||||
if (argv->argc == 1)
|
||||
argv->args[0] = 0;
|
||||
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, round_up_exp ((a + b + 1) * sizeof (char)));
|
||||
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;
|
||||
}
|
395
script/execute.c
395
script/execute.c
|
@ -30,6 +30,199 @@
|
|||
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)
|
||||
{
|
||||
struct grub_script_argv result = { 0, 0 };
|
||||
|
||||
if (grub_script_argv_next (&result))
|
||||
goto fail;
|
||||
|
||||
if (! grub_env_special (name))
|
||||
{
|
||||
char *v = grub_env_get (name);
|
||||
if (v && v[0])
|
||||
{
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
{
|
||||
if (grub_script_argv_split_append (&result, v))
|
||||
goto fail;
|
||||
}
|
||||
else
|
||||
if (grub_script_argv_append (&result, v))
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
else if (! scope)
|
||||
{
|
||||
if (grub_script_argv_append (&result, 0))
|
||||
goto fail;
|
||||
}
|
||||
else if (grub_strcmp (name, "#") == 0)
|
||||
{
|
||||
char buffer[ERRNO_DIGITS_MAX + 1];
|
||||
grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
|
||||
if (grub_script_argv_append (&result, buffer))
|
||||
goto fail;
|
||||
}
|
||||
else if (grub_strcmp (name, "*") == 0)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < scope->argv.argc; i++)
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
{
|
||||
if (i != 0 && grub_script_argv_next (&result))
|
||||
goto fail;
|
||||
|
||||
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
|
||||
goto fail;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i != 0 && grub_script_argv_append (&result, " "))
|
||||
goto fail;
|
||||
|
||||
if (grub_script_argv_append (&result, scope->argv.args[i]))
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
else if (grub_strcmp (name, "@") == 0)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < scope->argv.argc; i++)
|
||||
{
|
||||
if (i != 0 && grub_script_argv_next (&result))
|
||||
goto fail;
|
||||
|
||||
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
||||
{
|
||||
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
|
||||
goto fail;
|
||||
}
|
||||
else
|
||||
if (grub_script_argv_append (&result, scope->argv.args[i]))
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
if (grub_script_argv_split_append (&result,
|
||||
scope->argv.args[num - 1]))
|
||||
goto fail;
|
||||
}
|
||||
else
|
||||
if (grub_script_argv_append (&result, scope->argv.args[num - 1]))
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
return result.args;
|
||||
|
||||
fail:
|
||||
|
||||
grub_script_argv_free (&result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
char **values = 0;
|
||||
struct grub_script_arg *arg = 0;
|
||||
struct grub_script_argv result = { 0, 0 };
|
||||
|
||||
for (; arglist && arglist->arg; arglist = arglist->next)
|
||||
{
|
||||
if (grub_script_argv_next (&result))
|
||||
goto fail;
|
||||
|
||||
arg = arglist->arg;
|
||||
while (arg)
|
||||
{
|
||||
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 && grub_script_argv_next (&result))
|
||||
goto fail;
|
||||
|
||||
if (grub_script_argv_append (&result, values[i]))
|
||||
goto fail;
|
||||
}
|
||||
grub_free (values);
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_TEXT:
|
||||
if (grub_strlen (arg->str) &&
|
||||
grub_script_argv_append (&result, arg->str))
|
||||
goto fail;
|
||||
break;
|
||||
|
||||
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
|
||||
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
|
||||
if (grub_script_argv_append (&result, arg->str))
|
||||
goto fail;
|
||||
break;
|
||||
}
|
||||
arg = arg->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (! result.args[result.argc - 1])
|
||||
result.argc--;
|
||||
|
||||
*argv = result;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
|
||||
grub_script_argv_free (&result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_script_execute_cmd (struct grub_script_cmd *cmd)
|
||||
{
|
||||
|
@ -46,149 +239,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 +264,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 || ! args[0])
|
||||
if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
|
||||
return grub_errno;
|
||||
|
||||
cmdname = args[0];
|
||||
cmdname = argv.args[0];
|
||||
grubcmd = grub_command_find (cmdname);
|
||||
if (! grubcmd)
|
||||
{
|
||||
|
@ -232,13 +297,14 @@ 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_argv_free (&argv);
|
||||
grub_print_error ();
|
||||
|
||||
return 0;
|
||||
|
@ -247,14 +313,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;
|
||||
|
@ -307,25 +371,22 @@ grub_script_execute_cmdif (struct grub_script_cmd *cmd)
|
|||
grub_err_t
|
||||
grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
|
||||
{
|
||||
int i;
|
||||
int result;
|
||||
char **args;
|
||||
int argcount;
|
||||
unsigned i;
|
||||
grub_err_t result;
|
||||
struct grub_script_argv argv = { 0, 0 };
|
||||
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 +415,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;
|
||||
}
|
||||
|
|
|
@ -103,12 +103,3 @@ grub_script_function_find (char *functionname)
|
|||
|
||||
return func;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -119,7 +119,8 @@ DIGITS [[:digit:]]+
|
|||
NAME [[:alpha:]_][[:alnum:]_]*
|
||||
|
||||
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
|
||||
|
Loading…
Reference in a new issue