2009-05-02 Bean <bean123ch@gmail.com>

* conf/common.rmk (grub_script.tab.c): Change normal/parser.y to
	script/sh/parser.y.
	(pkglib_MODULES): Add normal.mod and sh.mod.
	(normal_SOURCES): New variable.
	(normal_mod_CFLAGS): Likewise.
	(normal_mod_LDFLAGS): Likewise.
	(sh_mod_SOURCES): Likewise.
	(sh_mod_CFLAGS): Likewise.
	(sh_mod_LDFLAGS): Likewise.

	* conf/i386-pc.rmk (normal/lexer.c_DEPENDENCIES): Changed to
	script/sh/lexer.c_DEPENDENCIES.
	(kernel_img_SOURCES): Remove kern/rescue.c, and kern/reader.c,
	kern/rescue_reader.c and kern/rescue_parser.c.
	(kernel_img_HEADERS): Remove rescue.h, add reader.h.
	(grub_emu_SOURCES): Change source files.
	(pkglib_MODULES): Remove normal.mod.
	(normal_SOURCES): Removed.
	(normal_mod_CFLAGS): Likewise.
	(normal_mod_LDFLAGS): Likewise.
	* conf/i386-coreboot.rmk: Likewise.
	* conf/i386-efi.rmk: Likewise.
	* conf/i386-ieee1276.rmk: Likewise.
	* conf/powerpc-ieee1275.rmk: Likewise.
	* conf/sparc64-ieee1275.rmk: Likewise.
	* conf/x86_64-efi.rmk: Likewise.

	* include/grub/command.h (grub_command_execute): New inline function.

	* include/grub/menu.h (grub_menu_entry): Removed commands field.

	* include/grub/normal.h: Remove <grub/setjmp.h>.
	(grub_fs_module_list): Moved to normal/autofs.c.
	(grub_exit_env): Removed.
	(grub_command_execute): Likewise.
	(grub_normal_menu_addentry): Renamed to grub_menu_addentry, removed
	parameter script.
	(read_command_list): New function declaration.
	(read_fs_list): Likewise.

	* include/parser.h: Include <grub/reader.h>.
	(grub_parser_split_cmdline): Change type of getline parameter.
	(grub_parser): New structure.
	(grub_parser_class): New variable.
	(grub_parser_execute): New function declaration.
	(grub_register_rescue_parser): Likewise.
	(grub_parser_register): New inline function.
	(grub_parser_unregister): Likewise.
	(grub_parser_get_current): Likewise.
	(grub_parser_set_current): Likewise.

	* include/grub/reader.h: New file.
	* kern/reader.c: Likewise.
	* kern/rescue_parser.c: Likewise.
	* kern/rescue_reader.c: Likewise.
	* normal/autofs.c: Likewise.
	* normal/dyncmd.c: Likewise.

	* include/grub/rescue.h: Removed.
	* normal/command.h: Likewise.

	* include/grub/script.h: Moved to ...
	* include/grub/script_sh.h: ... Moved here.
	* normal/execute.c: Moved to ...
	* script/sh/execute.c: ... Moved here.
	* normal/function.c: Moved to ...
	* script/sh/function.c: ... Moved here.
	* normal/lexer.c: Moved to ...
	* script/sh/lexer.c: ... Moved here.
	* normal/parser.y: Moved to ...
	* script/sh/parser.y: ... Moved here.
	* normal/script.c: Moved to ...
	* script/sh/script.c: ... Moved here.

	* normal/main.c: Remove <grub/rescue.h> and <grub/script.h>, include
	<grub/reader.h>.
	(grub_exit_env): Removed.
	(fs_module_list): Moved to normal/autofs.c.
	(grub_file_getline): Don't handle comment here.
	(free_menu): Skip removed field entry->commands.
	(grub_normal_menu_addentry): Removed as grub_menu_entry, removed
	script parameter.
	(read_config_file): Removed nested parameter, change getline function.
	(grub_enter_normal_mode): Removed.
	(grub_dyncmd_dispatcher): Moved to normal/dyncmd.c.
	(read_command_list): Likewise.
	(autoload_fs_module): Moved to normal/autofs.c.
	(read_fs_list): Likewise.
	(reader_nested): New variable.
	(grub_normal_execute): Run parser.sh to switch to sh parser.
	(grub_cmd_rescue): Removed.
	(cmd_normal): Removed.
	(grub_cmd_normal): Unregister itself at the beginning. Don't register
	rescue command.
	(grub_cmdline_run): New function.
	(grub_normal_reader_init): Likewise.
	(grub_normal_read_line): Likewise.
	(grub_env_write_pager): Likewise.
	(cmdline): New variable.
	(grub_normal_reader): Likewise.
	(GRUB_MOD_INIT): Register normal reader and set as current, register
	pager hook, register normal command with grub_register_command_prio,
	so that it won't show up in command.lst.
	(GRUB_MOD_FINI): Unregister normal reader, unhook pager, clear
	grub_fs_autoload_hook.

	* normal/menu.c: Remove <grub/script.h>, add <grub/command.h>.
	(grub_menu_execute_entry): Replace grub_script_execute with
	grub_parser_execute, change parameter to grub_command_execute.

	* normal/menu_text.c: Remove <grub/script.h>.

	* normal/menu_entry.c: Remove <grub/script.h>, add <grub/command.h>
	and <grub/parser.h>.
	(run): Change editor_getline to use new parser interface. Change
	parameter to grub_command_execute.

	* kern/main.c: Remove <grub/rescue.h>, include <grub/command.h>,
	<grub/reader.h> and <grub/parser.h>.
	(grub_load_normal_mode): Execute normal command.
	(grub_main): Call grub_register_core_commands,
	grub_register_rescue_parser and grub_register_rescue_reader, use
	grub_reader_loop to enter input loop.

	* kern/parser.c (grub_parser_spli_cmdline): Change type of getline
	parameter.
	(grub_parser_class): New variable.
	(grub_parser_execute): New function.

	* loader/i386/multiboot.c: Remove <grub/rescue.h>.
	* loader/multiboot2.c: Likewise.
	* loader/sparc64/ieee1275/linux.c: Likewise.

	* util/grub-emu.c (read_command_list): New dummy function.
This commit is contained in:
bean 2009-05-02 19:49:34 +00:00
parent 18db813d65
commit d558e6b5ac
39 changed files with 1132 additions and 877 deletions

View file

@ -20,20 +20,15 @@
#include <grub/kernel.h>
#include <grub/normal.h>
#include <grub/dl.h>
#include <grub/rescue.h>
#include <grub/misc.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/term.h>
#include <grub/env.h>
#include <grub/parser.h>
#include <grub/script.h>
#include <grub/reader.h>
#include <grub/menu_viewer.h>
grub_jmp_buf grub_exit_env;
static grub_fs_module_list_t fs_module_list = 0;
#define GRUB_DEFAULT_HISTORY_SIZE 50
/* Read a line from the file FILE. */
@ -43,7 +38,6 @@ grub_file_getline (grub_file_t file)
char c;
int pos = 0;
int literal = 0;
int comment = 0;
char *cmdline;
int max_len = 64;
@ -84,16 +78,9 @@ grub_file_getline (grub_file_t file)
if (c == '\\')
literal = 1;
if (comment)
if (pos == 0)
{
if (c == '\n')
comment = 0;
}
else if (pos == 0)
{
if (c == '#')
comment = 1;
else if (! grub_isspace (c))
if (! grub_isspace (c))
cmdline[pos++] = c;
}
else
@ -138,7 +125,6 @@ free_menu (grub_menu_t menu)
{
grub_menu_entry_t next_entry = entry->next;
grub_script_free (entry->commands);
grub_free ((void *) entry->title);
grub_free ((void *) entry->sourcecode);
entry = next_entry;
@ -164,8 +150,8 @@ free_menu_entry_classes (struct grub_menu_entry_class *head)
}
grub_err_t
grub_normal_menu_addentry (int argc, const char **args,
struct grub_script *script, const char *sourcecode)
grub_menu_addentry (int argc, const char **args,
const char *sourcecode)
{
const char *menutitle = 0;
const char *menusourcecode;
@ -237,7 +223,7 @@ grub_normal_menu_addentry (int argc, const char **args,
/* Handle invalid argument. */
failed = 1;
grub_error (GRUB_ERR_MENU,
"invalid argument for menuentry: %s", args[i]);
"invalid argument for menuentry: %s", args[i]);
break;
}
}
@ -251,7 +237,7 @@ grub_normal_menu_addentry (int argc, const char **args,
{
failed = 1;
grub_error (GRUB_ERR_MENU,
"too many titles for menuentry: %s", args[i]);
"too many titles for menuentry: %s", args[i]);
break;
}
}
@ -287,7 +273,6 @@ grub_normal_menu_addentry (int argc, const char **args,
return grub_errno;
}
(*last)->commands = script;
(*last)->title = menutitle;
(*last)->classes = classes_head;
(*last)->next = 0;
@ -299,20 +284,56 @@ grub_normal_menu_addentry (int argc, const char **args,
}
static grub_menu_t
read_config_file (const char *config, int nested)
read_config_file (const char *config)
{
grub_file_t file;
auto grub_err_t getline (char **line);
int currline = 0;
int errors = 0;
grub_parser_t old_parser = 0;
grub_err_t getline (char **line)
auto grub_err_t getline (char **line, int cont);
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
{
currline++;
while (1)
{
char *buf;
*line = grub_file_getline (file);
if (! *line)
return grub_errno;
*line = buf = grub_file_getline (file);
if (! buf)
return grub_errno;
if (buf[0] == '#')
{
if (buf[1] == '!')
{
grub_parser_t parser;
grub_named_list_t list;
buf += 2;
while (grub_isspace (*buf))
buf++;
if (! old_parser)
old_parser = grub_parser_get_current ();
list = GRUB_AS_NAMED_LIST (grub_parser_class.handler_list);
parser = grub_named_list_find (list, buf);
if (parser)
grub_parser_set_current (parser);
else
{
char cmd_name[8 + grub_strlen (buf)];
/* Perhaps it's not loaded yet, try the autoload
command. */
grub_strcpy (cmd_name, "parser.");
grub_strcat (cmd_name, buf);
grub_command_execute (cmd_name, 0, 0);
}
}
grub_free (*line);
}
else
break;
}
return GRUB_ERR_NONE;
}
@ -320,14 +341,15 @@ read_config_file (const char *config, int nested)
grub_menu_t newmenu;
newmenu = grub_env_get_data_slot ("menu");
if (nested || ! newmenu)
if (! newmenu)
{
newmenu = grub_malloc (sizeof (*newmenu));
if (! newmenu)
return 0;
newmenu->size = 0;
newmenu->entry_list = 0;
grub_env_set_data_slot ("menu", newmenu);
}
/* Try to open the config file. */
@ -335,58 +357,15 @@ read_config_file (const char *config, int nested)
if (! file)
return 0;
grub_env_set_data_slot ("menu", newmenu);
while (1)
{
struct grub_script *parsed_script;
int startline;
char *cmdline;
cmdline = grub_file_getline (file);
if (!cmdline)
break;
startline = ++currline;
/* Execute the script, line for line. */
parsed_script = grub_script_parse (cmdline, getline);
grub_free (cmdline);
if (! parsed_script)
{
grub_printf ("(line %d-%d)\n", startline, currline);
errors++;
continue;
}
/* Execute the command(s). */
grub_script_execute (parsed_script);
/* Ignore errors. */
grub_errno = GRUB_ERR_NONE;
/* The parsed script was executed, throw it away. */
grub_script_free (parsed_script);
}
grub_reader_loop (getline);
grub_file_close (file);
if (errors > 0)
grub_wait_after_message ();
if (old_parser)
grub_parser_set_current (old_parser);
return newmenu;
}
/* This starts the normal mode. */
void
grub_enter_normal_mode (const char *config)
{
if (grub_setjmp (grub_exit_env) == 0)
grub_normal_execute (config, 0, 0);
}
/* Initialize the screen. */
void
grub_normal_init_page (void)
@ -409,237 +388,7 @@ grub_normal_init_page (void)
#undef TITLE
}
static grub_err_t
grub_dyncmd_dispatcher (struct grub_command *cmd,
int argc, char **args)
{
char *modname = cmd->data;
grub_dl_t mod;
grub_err_t ret;
mod = grub_dl_load (modname);
if (mod)
{
char *name;
grub_free (modname);
grub_dl_ref (mod);
name = (char *) cmd->name;
grub_unregister_command (cmd);
cmd = grub_command_find (name);
if (cmd)
ret = (cmd->func) (cmd, argc, args);
else
ret = grub_errno;
grub_free (name);
}
else
ret = grub_errno;
return ret;
}
/* Read the file command.lst for auto-loading. */
static void
read_command_list (void)
{
const char *prefix;
static int first_time = 1;
/* Make sure that this function does not get executed twice. */
if (! first_time)
return;
first_time = 0;
prefix = grub_env_get ("prefix");
if (prefix)
{
char *filename;
filename = grub_malloc (grub_strlen (prefix) + sizeof ("/command.lst"));
if (filename)
{
grub_file_t file;
grub_sprintf (filename, "%s/command.lst", prefix);
file = grub_file_open (filename);
if (file)
{
char *buf = 0;
for (;; grub_free(buf))
{
char *p, *name, *modname;
grub_command_t cmd;
int prio = 0;
buf = grub_file_getline (file);
if (! buf)
break;
name = buf;
if (*name == '*')
{
name++;
prio++;
}
if (! grub_isgraph (name[0]))
continue;
p = grub_strchr (name, ':');
if (! p)
continue;
*p = '\0';
while (*++p == ' ')
;
if (! grub_isgraph (*p))
continue;
if (grub_dl_get (p))
continue;
name = grub_strdup (name);
if (! name)
continue;
modname = grub_strdup (p);
if (! modname)
{
grub_free (name);
continue;
}
cmd = grub_register_command_prio (name,
grub_dyncmd_dispatcher,
0, "not loaded", prio);
if (! cmd)
{
grub_free (name);
grub_free (modname);
continue;
}
cmd->flags |= GRUB_COMMAND_FLAG_DYNCMD;
cmd->data = modname;
/* Update the active flag. */
grub_command_find (name);
}
grub_file_close (file);
}
grub_free (filename);
}
}
/* Ignore errors. */
grub_errno = GRUB_ERR_NONE;
}
/* The auto-loading hook for filesystems. */
static int
autoload_fs_module (void)
{
grub_fs_module_list_t p;
while ((p = fs_module_list) != 0)
{
if (! grub_dl_get (p->name) && grub_dl_load (p->name))
return 1;
fs_module_list = p->next;
grub_free (p->name);
grub_free (p);
}
return 0;
}
/* Read the file fs.lst for auto-loading. */
static void
read_fs_list (void)
{
const char *prefix;
static int first_time = 1;
/* Make sure that this function does not get executed twice. */
if (! first_time)
return;
first_time = 0;
prefix = grub_env_get ("prefix");
if (prefix)
{
char *filename;
filename = grub_malloc (grub_strlen (prefix) + sizeof ("/fs.lst"));
if (filename)
{
grub_file_t file;
grub_sprintf (filename, "%s/fs.lst", prefix);
file = grub_file_open (filename);
if (file)
{
while (1)
{
char *buf;
char *p;
char *q;
grub_fs_module_list_t fs_mod;
buf = grub_file_getline (file);
if (! buf)
break;
p = buf;
q = buf + grub_strlen (buf) - 1;
/* Ignore space. */
while (grub_isspace (*p))
p++;
while (p < q && grub_isspace (*q))
*q-- = '\0';
/* If the line is empty, skip it. */
if (p >= q)
continue;
fs_mod = grub_malloc (sizeof (*fs_mod));
if (! fs_mod)
continue;
fs_mod->name = grub_strdup (p);
if (! fs_mod->name)
{
grub_free (fs_mod);
continue;
}
fs_mod->next = fs_module_list;
fs_module_list = fs_mod;
}
grub_file_close (file);
}
grub_free (filename);
}
}
/* Ignore errors. */
grub_errno = GRUB_ERR_NONE;
/* Set the hook. */
grub_fs_autoload_hook = autoload_fs_module;
}
static int reader_nested;
/* Read the config file COFIG, and execute the menu interface or
the command-line interface if BATCH is false. */
@ -651,10 +400,13 @@ grub_normal_execute (const char *config, int nested, int batch)
read_command_list ();
read_fs_list ();
read_handler_list ();
grub_command_execute ("parser.sh", 0, 0);
reader_nested = nested;
if (config)
{
menu = read_config_file (config, nested);
menu = read_config_file (config);
/* Ignore any error. */
grub_errno = GRUB_ERR_NONE;
@ -663,39 +415,27 @@ grub_normal_execute (const char *config, int nested, int batch)
if (! batch)
{
if (menu && menu->size)
{
grub_menu_viewer_show_menu (menu, nested);
if (nested)
free_menu (menu);
}
else
grub_cmdline_run (nested);
{
grub_menu_viewer_show_menu (menu, nested);
if (nested)
free_menu (menu);
}
}
}
static grub_err_t
grub_cmd_rescue (struct grub_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
/* This starts the normal mode. */
void
grub_enter_normal_mode (const char *config)
{
grub_longjmp (grub_exit_env, 0);
/* Never reach here. */
return 0;
grub_normal_execute (config, 0, 0);
}
static grub_command_t cmd_normal;
/* Enter normal mode from rescue mode. */
static grub_err_t
grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
grub_cmd_normal (struct grub_command *cmd,
int argc, char *argv[])
{
grub_command_t cmd_rescue;
grub_unregister_command (cmd_normal);
cmd_rescue = grub_register_command ("rescue", grub_cmd_rescue,
0, "enter rescue mode");
grub_unregister_command (cmd);
if (argc == 0)
{
@ -722,12 +462,77 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
grub_enter_normal_mode (argv[0]);
quit:
grub_unregister_command (cmd_rescue);
cmd_normal = grub_register_command_prio ("normal", grub_cmd_normal,
0, "enter normal mode", 0);
return 0;
}
void
grub_cmdline_run (int nested)
{
grub_reader_t reader = grub_reader_get_current ();
reader_nested = nested;
if (reader->init)
reader->init ();
grub_reader_loop (0);
}
static grub_err_t
grub_normal_reader_init (void)
{
grub_normal_init_page ();
grub_setcursor (1);
grub_printf ("\
[ Minimal BASH-like line editing is supported. For the first word, TAB\n\
lists possible command completions. Anywhere else TAB lists possible\n\
device/file completions.%s ]\n\n",
reader_nested ? " ESC at any time exits." : "");
return 0;
}
static char cmdline[GRUB_MAX_CMDLINE];
static grub_err_t
grub_normal_read_line (char **line, int cont)
{
grub_parser_t parser = grub_parser_get_current ();
char prompt[8 + grub_strlen (parser->name)];
grub_sprintf (prompt, "%s:%s> ", parser->name, (cont) ? "" : "grub");
while (1)
{
cmdline[0] = 0;
if (grub_cmdline_get (prompt, cmdline, sizeof (cmdline), 0, 1))
break;
if ((reader_nested) || (cont))
{
*line = 0;
return grub_errno;
}
}
*line = grub_strdup (cmdline);
return 0;
}
static struct grub_reader grub_normal_reader =
{
.name = "normal",
.init = grub_normal_reader_init,
.read_line = grub_normal_read_line
};
static char *
grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
const char *val)
{
grub_set_more ((*val == '1'));
return grub_strdup (val);
}
GRUB_MOD_INIT(normal)
{
/* Normal mode shouldn't be unloaded. */
@ -738,9 +543,13 @@ GRUB_MOD_INIT(normal)
grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
grub_reader_register ("normal", &grub_normal_reader);
grub_reader_set_current (&grub_normal_reader);
grub_register_variable_hook ("pager", 0, grub_env_write_pager);
/* Register a command "normal" for the rescue mode. */
cmd_normal = grub_register_command ("normal", grub_cmd_normal,
0, "enter normal mode");
grub_register_command_prio ("normal", grub_cmd_normal,
0, "Enter normal mode", 0);
/* Reload terminal colors when these variables are written to. */
grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
@ -754,6 +563,8 @@ GRUB_MOD_INIT(normal)
GRUB_MOD_FINI(normal)
{
grub_set_history (0);
grub_unregister_command (cmd_normal);
grub_reader_unregister (&grub_normal_reader);
grub_register_variable_hook ("pager", 0, 0);
grub_fs_autoload_hook = 0;
free_handler_list ();
}