d558e6b5ac
* 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.
271 lines
6.7 KiB
C
271 lines
6.7 KiB
C
/* parser.c - the part of the parser that can return partial tokens */
|
|
/*
|
|
* GRUB -- GRand Unified Bootloader
|
|
* Copyright (C) 2005,2007,2009 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/parser.h>
|
|
#include <grub/env.h>
|
|
#include <grub/misc.h>
|
|
#include <grub/mm.h>
|
|
|
|
|
|
/* All the possible state transitions on the command line. If a
|
|
transition can not be found, it is assumed that there is no
|
|
transition and keep_value is assumed to be 1. */
|
|
static struct grub_parser_state_transition state_transitions[] =
|
|
{
|
|
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
|
|
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
|
|
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
|
|
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
|
|
|
|
{ GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
|
|
|
|
{ GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
|
|
|
|
{ GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
|
|
{ GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
|
|
|
|
{ GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
|
|
{ GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
|
|
{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
|
|
{ GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
|
|
|
|
{ GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
|
|
{ GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
|
|
{ GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
|
|
{ GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
|
|
{ GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
|
|
|
|
{ 0, 0, 0, 0}
|
|
};
|
|
|
|
|
|
/* Determines the state following STATE, determined by C. */
|
|
grub_parser_state_t
|
|
grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
|
|
{
|
|
struct grub_parser_state_transition *transition;
|
|
struct grub_parser_state_transition *next_match = 0;
|
|
struct grub_parser_state_transition default_transition;
|
|
int found = 0;
|
|
|
|
default_transition.to_state = state;
|
|
default_transition.keep_value = 1;
|
|
|
|
/* Look for a good translation. */
|
|
for (transition = state_transitions; transition->from_state; transition++)
|
|
{
|
|
/* An exact match was found, use it. */
|
|
if (transition->from_state == state && transition->input == c)
|
|
{
|
|
found = 1;
|
|
break;
|
|
}
|
|
|
|
/* A less perfect match was found, use this one if no exact
|
|
match can be found. */
|
|
if (transition->from_state == state && transition->input == 0)
|
|
next_match = transition;
|
|
}
|
|
|
|
if (! found)
|
|
{
|
|
if (next_match)
|
|
transition = next_match;
|
|
else
|
|
transition = &default_transition;
|
|
}
|
|
|
|
if (transition->keep_value)
|
|
*result = c;
|
|
else
|
|
*result = 0;
|
|
return transition->to_state;
|
|
}
|
|
|
|
|
|
grub_err_t
|
|
grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
|
|
int *argc, char ***argv)
|
|
{
|
|
grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
|
|
/* XXX: Fixed size buffer, perhaps this buffer should be dynamically
|
|
allocated. */
|
|
char buffer[1024];
|
|
char *bp = buffer;
|
|
char *rd = (char *) cmdline;
|
|
char varname[200];
|
|
char *vp = varname;
|
|
char *args;
|
|
int i;
|
|
|
|
auto int check_varstate (grub_parser_state_t s);
|
|
|
|
int check_varstate (grub_parser_state_t s)
|
|
{
|
|
return (s == GRUB_PARSER_STATE_VARNAME
|
|
|| s == GRUB_PARSER_STATE_VARNAME2
|
|
|| s == GRUB_PARSER_STATE_QVARNAME
|
|
|| s == GRUB_PARSER_STATE_QVARNAME2);
|
|
}
|
|
|
|
auto void add_var (grub_parser_state_t newstate);
|
|
|
|
void add_var (grub_parser_state_t newstate)
|
|
{
|
|
char *val;
|
|
|
|
/* Check if a variable was being read in and the end of the name
|
|
was reached. */
|
|
if (! (check_varstate (state) && !check_varstate (newstate)))
|
|
return;
|
|
|
|
*(vp++) = '\0';
|
|
val = grub_env_get (varname);
|
|
vp = varname;
|
|
if (! val)
|
|
return;
|
|
|
|
/* Insert the contents of the variable in the buffer. */
|
|
for (; *val; val++)
|
|
*(bp++) = *val;
|
|
}
|
|
|
|
*argc = 1;
|
|
do
|
|
{
|
|
if (! *rd)
|
|
{
|
|
if (getline)
|
|
getline (&rd, 1);
|
|
else break;
|
|
}
|
|
|
|
for (; *rd; rd++)
|
|
{
|
|
grub_parser_state_t newstate;
|
|
char use;
|
|
|
|
newstate = grub_parser_cmdline_state (state, *rd, &use);
|
|
|
|
/* If a variable was being processed and this character does
|
|
not describe the variable anymore, write the variable to
|
|
the buffer. */
|
|
add_var (newstate);
|
|
|
|
if (check_varstate (newstate))
|
|
{
|
|
if (use)
|
|
*(vp++) = use;
|
|
}
|
|
else
|
|
{
|
|
if (newstate == GRUB_PARSER_STATE_TEXT
|
|
&& state != GRUB_PARSER_STATE_ESC && use == ' ')
|
|
{
|
|
/* Don't add more than one argument if multiple
|
|
spaces are used. */
|
|
if (bp != buffer && *(bp - 1))
|
|
{
|
|
*(bp++) = '\0';
|
|
(*argc)++;
|
|
}
|
|
}
|
|
else if (use)
|
|
*(bp++) = use;
|
|
}
|
|
state = newstate;
|
|
}
|
|
} while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
|
|
*(bp++) = '\0';
|
|
|
|
/* A special case for when the last character was part of a
|
|
variable. */
|
|
add_var (GRUB_PARSER_STATE_TEXT);
|
|
|
|
|
|
/* Reserve memory for the return values. */
|
|
args = grub_malloc (bp - buffer);
|
|
if (! args)
|
|
return grub_errno;
|
|
grub_memcpy (args, buffer, bp - buffer);
|
|
|
|
*argv = grub_malloc (sizeof (char *) * (*argc + 1));
|
|
if (! *argv)
|
|
{
|
|
grub_free (args);
|
|
return grub_errno;
|
|
}
|
|
|
|
/* The arguments are separated with 0's, setup argv so it points to
|
|
the right values. */
|
|
bp = args;
|
|
for (i = 0; i < *argc; i++)
|
|
{
|
|
(*argv)[i] = bp;
|
|
while (*bp)
|
|
bp++;
|
|
bp++;
|
|
}
|
|
|
|
(*argc)--;
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct grub_handler_class grub_parser_class =
|
|
{
|
|
.name = "parser"
|
|
};
|
|
|
|
grub_err_t
|
|
grub_parser_execute (char *source)
|
|
{
|
|
auto grub_err_t getline (char **line, int cont);
|
|
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
|
|
{
|
|
char *p;
|
|
|
|
if (! source)
|
|
{
|
|
*line = 0;
|
|
return 0;
|
|
}
|
|
|
|
p = grub_strchr (source, '\n');
|
|
if (p)
|
|
*(p++) = 0;
|
|
|
|
*line = grub_strdup (source);
|
|
source = p;
|
|
return 0;
|
|
}
|
|
|
|
while (source)
|
|
{
|
|
char *line;
|
|
grub_parser_t parser;
|
|
|
|
getline (&line, 0);
|
|
parser = grub_parser_get_current ();
|
|
parser->parse_line (line, getline);
|
|
grub_free (line);
|
|
}
|
|
|
|
return grub_errno;
|
|
}
|