2005-11-06 Marco Gerards <mgerards@xs4all.nl>

Add initial scripting support.

	* commands/test.c: New file.
	* include/grub/script.h: Likewise.
	* normal/execute.c: Likewise.
	* normal/function.c: Likewise.
	* normal/lexer.c: Likewise.
	* normal/parser.y: Likewise.
	* normal/script.c: Likewise.

	* configure.ac: Add `AC_PROG_YACC' test.

	* conf/i386-pc.rmk (grub_emu_SOURCES): Add `commands/test.c',
	`normal/execute.c', `normal/lexer.c', `grub_script.tab.c',
	`normal/function.c' and `normal/script.c'.
	(normal_mod_SOURCES): `normal/execute.c', `normal/lexer.c',
	`grub_script.tab.c', `normal/function.c' and `normal/script.c'.
	(test_mod_SOURCES, test_mod_CFLAGS, test_mod_LDFLAGS): New variables.
	(pkgdata_MODULES): Add `test.mod'.
	(grub_script.tab.c): New rule.
	(grub_script.tab.h): Likewise.

	* include/grub/err.h (grub_err_t): Add `GRUB_ERR_TEST_FAILURE'.

	* include/grub/normal.h (grub_test_init): New prototype.
	(grub_test_fini): Likewise.

	* normal/command.c: Include <grub/script.h>.
	(grub_command_execute): Rewritten.

	* util/grub-emu.c (main): Call `grub_test_init' and
	`grub_test_fini'.
This commit is contained in:
marco_g 2005-11-06 22:19:59 +00:00
parent 77500b2bf0
commit daac212ae3
16 changed files with 1702 additions and 76 deletions

View file

@ -25,6 +25,7 @@
#include <grub/env.h>
#include <grub/dl.h>
#include <grub/parser.h>
#include <grub/script.h>
static grub_command_t grub_command_list;
@ -193,42 +194,9 @@ grub_command_execute (char *cmdline, int interactive)
return grub_cmdline_get (">", *s, GRUB_MAX_CMDLINE, 0, 1);
}
grub_command_t cmd;
grub_err_t ret = 0;
char *pager;
int num;
char **args;
struct grub_arg_list *state;
struct grub_arg_option *parser;
int maxargs = 0;
char **arglist;
int numargs;
if (grub_parser_split_cmdline (cmdline, cmdline_get, &num, &args))
return 0;
/* In case of an assignment set the environment accordingly instead
of calling a function. */
if (num == 0 && grub_strchr (args[0], '='))
{
char *val;
if (! interactive)
grub_printf ("%s\n", cmdline);
val = grub_strchr (args[0], '=');
val[0] = 0;
grub_env_set (args[0], val + 1);
val[0] = '=';
return 0;
}
cmd = grub_command_find (args[0]);
if (! cmd)
return -1;
if (! (cmd->flags & GRUB_COMMAND_FLAG_NO_ECHO) && ! interactive)
grub_printf ("%s\n", cmdline);
struct grub_script *parsed_script;
/* Enable the pager if the environment pager is set to 1. */
if (interactive)
@ -237,27 +205,22 @@ grub_command_execute (char *cmdline, int interactive)
pager = 0;
if (pager && (! grub_strcmp (pager, "1")))
grub_set_more (1);
parser = (struct grub_arg_option *) cmd->options;
while (parser && (parser++)->doc)
maxargs++;
state = grub_malloc (sizeof (struct grub_arg_list) * maxargs);
grub_memset (state, 0, sizeof (struct grub_arg_list) * maxargs);
if (! (cmd->flags & GRUB_COMMAND_FLAG_NO_ARG_PARSE))
/* Parse the script. */
parsed_script = grub_script_parse (cmdline, cmdline_get);
if (parsed_script)
{
if (grub_arg_parse (cmd, num, &args[1], state, &arglist, &numargs))
ret = (cmd->func) (state, numargs, arglist);
/* Execute the command(s). */
grub_script_execute (parsed_script);
/* The parsed script was executed, throw it away. */
grub_script_free (parsed_script);
}
else
ret = (cmd->func) (state, num, &args[1]);
grub_free (state);
if (pager && (! grub_strcmp (pager, "1")))
grub_set_more (0);
grub_free (args);
return ret;
}