2005-11-06 22:19:59 +00:00
|
|
|
/* execute.c -- Execute a GRUB script. */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-22 13:37:27 +00:00
|
|
|
* Copyright (C) 2005,2007,2008,2009,2010 Free Software Foundation, Inc.
|
2005-11-06 22:19:59 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2005-11-06 22:19:59 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2005-11-06 22:19:59 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2005-11-06 22:19:59 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2005-11-06 22:19:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/env.h>
|
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.
2009-05-02 19:49:34 +00:00
|
|
|
#include <grub/script_sh.h>
|
|
|
|
#include <grub/command.h>
|
|
|
|
#include <grub/menu.h>
|
2009-03-21 Bean <bean123ch@gmail.com>
* commands/blocklist.c: Add include file <grub/command.h>, remove
<grub/normal.h> and <grub/arg.h>.
(grub_cmd_blocklist): Use the new command interface.
(GRUB_MOD_INIT): Likewise.
(GRUB_MOD_FINI): Likewise.
* commands/boot.c: Likewise.
* commands/cat.c: Likewise.
* commands/cmp.c: Likewise.
* commands/configfile.c: Likewise.
* commands/crc.c: Likewise.
* commands/echo.c: Likewise.
* commands/halt.c: Likewise.
* commands/handler.c: Likewise.
* commands/hdparm.c: Likewise.
* commands/help.c: Likewise.
* commands/hexdump.c: Likewise.
* commands/loadenv.c: Likewise.
* commands/ls.c: Likewise.
* commands/lsmmap.c: Likewise.
* commands/lspci.c: Likewise.
* commands/loadenv.c: Likewise.
* commands/read.c: Likewise.
* commands/reboot.c: Likewise.
* commands/search.c: Likewise.
* commands/sleep.c: Likewise.
* commands/test.c: Likewise.
* commands/usbtest.c: Likewise.
* commands/videotest.c: Likewise.
* commands/i386/cpuid.c: Likewise.
* commands/i386/pc/halt.c: Likewise.
* commands/i386/pc/play.c: Likewise.
* commands/i386/pc/pxecmd.c: Likewise.
* commands/i386/pc/vbeinfo.c: Likewise.
* commands/i386/pc/vbetest.c: Likewise.
* commands/ieee1275/suspend.c: Likewise.
* disk/loopback.c: Likewise.
* font/font_cmd.c: Likewise.
* hello/hello.c: Likewise.
* loader/efi/appleloader.c: Likewise.
* loader/efi/chainloader.c: Likewise.
* loader/i386/bsd.c: Likewise.
* loader/i386/efi/linux.c: Likewise.
* loader/i386/ieee1275/linux.c: Likewise.
* loader/i386/linux.c: Likewise.
* loader/i386/pc/chainloader.c: Likewise.
* loader/i386/pc/linux.c: Likewise.
* loader/powerpc/ieee1275/linux.c: Likewise.
* loader/multiboot_loader.c: Likewise.
* term/gfxterm.c: Likewise.
* term/i386/pc/serial.c: Likewise.
* term/terminfo.c: Likewise.
* term/i386/pc/vesafb.c: Removed <grub/arg.h>.
* term/i386/pc/vga.c: Likewise.
* video/readers/jpeg.c: Likewise.
* video/readers/png.c: Likewise.
* video/readers/tga.c: Likewise.
* util/grub-fstest (cmd_loopback): Removed.
(cmd_blocklist): Likewise.
(cmd_ls): Likewise.
(grub_register_command): Likewise.
(grub_unregister_command): Likewise.
(execute_command): Use grub_command_find to locate command and execute
it.
* include/grub/efi/chainloader.h: Removed.
* loader/efi/chainloader_normal.c: Likewise.
* loader/i386/bsd_normal.c: Likewise.
* loader/i386/pc/chainloader_normal.c: Likewise.
* loader/i386/pc/multiboot_normal.c: Likewise.
* loader/linux_normal.c: Likewise.
* loader/multiboot_loader_normal.c: Likewise.
* loader/powerpc/ieee1275/linux_normal.c: Likewise.
* gencmdlist.sh: Scan new registration command grub_register_extcmd
and grub_register_command_p1.
* conf/common.rmk (grub_fstest_SOURCES): Add kern/list.c,
kern/command.c, lib/arg.c and commands/extcmd.c.
(pkglib_MODULES): Remove boot.mod, and minicmd.mod and extcmd.mod.
(minicmd_mod_SOURCES): New variable.
(minicmd_mod_CFLAGS): Likewise.
(minicmd_mod_LDFLAGS): Likewise.
(extcmd_mod_SOURCES): Likewise.
(extcmd_mod_CFLAGS): Likewise.
(extcmd_mod_LDFLAGS): Likewise.
(boot_mod_SOURCES): Removed.
(boot_mod_CFLAGS): Likewise.
(boot_mod_LDFLAGS): Likewise.
* conf/i386-pc.rmk (kernel_img_SOURCES): Add kern/command.c and
kern/corecmd.c.
(kernel_img_HEADERS): Add command.h.
(grub_emu_SOURCES): Remove commands/boot.c and normal/arg.c, add
commands/minicmd.c, kern/command.c, kern/corecmd.c, commands/extcmd.c
and lib/arg.c.
(pkglib_MODULES): Change _linux.mod, _chain.mod, _bsd.mod and
_multiboot.mod as linux.mod, chain.mod, bsd.mod and multiboot.mod,
remove the corresponding normal mode command.
(normal_mod_SOURCES): Remove normal/arg.c.
* conf/i386-coreboot.rmk: Likewise.
* conf/i386-efi.rmk: Likewise.
* conf/i386-ieee1275.rmk: Likewise.
* conf/powerpc-ieee1275.rmk: Likewise.
* conf/x86_64-efi.rmk: Likewise.
* include/grub/arg.h: Move from here ...
* include/grub/lib/arg.h: ... to here.
* normal/arg.c: Move from here ...
* lib/arg.c: ... to here.
* commands/extcmd.c: New file.
* commands/minicmd.c: Likewise.
* include/grub/command.h: Likewise.
* include/grub/extcmd.h: Likewise.
* kern/command.c: Likewise.
* kern/corecmd.c: Likewise.
* kern/list.c (grub_list_iterate): Return int instead of void.
(grub_list_insert): New function.
(grub_prio_list_insert): Likewise.
* kern/rescue.c (grub_rescue_command): Removed.
(grub_rescue_command_list): Likewise.
(grub_rescue_register_command): Likewise.
(grub_rescue_unregister_command): Likewise.
(grub_rescue_cmd_boot): Move to minicmd.c
(grub_rescue_cmd_help): Likewise.
(grub_rescue_cmd_info): Likewise.
(grub_rescue_cmd_boot): Likewise.
(grub_rescue_cmd_testload): Likewise.
(grub_rescue_cmd_dump): Likewise.
(grub_rescue_cmd_rmmod): Likewise.
(grub_rescue_cmd_lsmod): Likewise.
(grub_rescue_cmd_exit): Likewise.
(grub_rescue_print_devices): Moved to corecmd.c.
(grub_rescue_print_files): Likewise.
(grub_rescue_cmd_ls): Likewise.
(grub_rescue_cmd_insmod): Likewise.
(grub_rescue_cmd_set): Likewise.
(grub_rescue_cmd_unset): Likewise.
(attemp_normal_mode): Use grub_command_find to get normal module.
(grub_enter_rescue_mode): Use grub_register_core_commands to register
commands, remove grub_rescue_regiter_command calls.
* normal/command.c (grub_regiser_command): Removed.
(grub_unregister_command): Likewise.
(grub_command_find): Likewise.
(grub_iterate_commands): Likewise.
(rescue_command): Likewise.
(export_command): Moved to corecmd.c.
(set_command): Removed.
(unset_command): Likewise.
(insmod_command): Likewise.
(rmmod_command): Likewise.
(lsmod_command): Likewise.
(grub_command_init): Likewise.
* normal/completion.c (iterate_command): Use cmd->prio to check for
active command.
(complete_arguments): Use grub_extcmd_t structure to find options.
(grub_normal_do_completion): Change function grub_iterate_commands to
grub_command_iterate.
* normal/execute.c (grub_script_execute_cmd): No need to parse
argument here.
* normal/main.c (grub_dyncmd_dispatcher): New function.
(read_command_list): Register unload commands as dyncmd.
(grub_cmd_normal): Use new command interface, register rescue,
unregister normal at entry, register normal, unregister rescue at exit.
* include/grub/list.h (grub_list_test_t): New type.
(grub_list_iterate): Return int instead of void.
(grub_list_insert): New function.
(GRUB_AS_NAMED_LIST_P): New macro.
(GRUB_AS_PRIO_LIST): Likewise.
(GRUB_AS_PRIO_LIST_P): Likewise.
(GRUB_PRIO_LIST_PRIO_MASK): New constant.
(GRUB_PRIO_LIST_FLAG_ACTIVE): Likewise.
(grub_prio_list): New structure.
(grub_prio_list_insert): New function.
(grub_prio_list_remove): New inline function.
* include/grub/normal.h: Remove <grub/arg.h>, add <grub/command.h>.
(GRUB_COMMAND_FLAG_CMDLINE): Moved to command.h.
(GRUB_COMMAND_FLAG_MENU): Likewise.
(GRUB_COMMAND_FLAG_BOTH): Likewise.
(GRUB_COMMAND_FLAG_TITLE): Likewise.
(GRUB_COMMAND_FLAG_NO_ECHO): Likewise.
(GRUB_COMMAND_FLAG_NO_ARG_PARSE): Removed.
(GRUB_COMMAND_FLAG_NOT_LOADED): Likewise.
(grub_command): Likewise.
(grub_register_command): Likewise.
(grub_command_find): Likewise.
(grub_iterate_commands): Likewise.
(grub_command_init): Likewise.
(grub_arg_parse): Likewise.
(grub_arg_show_help): Likewise.
* include/grub/rescue.h (grub_rescue_register_command): Removed.
(grub_rescue_unregister_command): Likewise.
* include/grub/i386/bsd.h: Remove grub_rescue_cmd_freebsd,
grub_rescue_cmd_openbsd, grub_rescue_cmd_netbsd,
grub_rescue_cmd_freebsd_loadenv and grub_rescue_cmd_freebsd_module.
* include/grub/i386/efi/loader.h: Remove grub_rescue_cmd_linux and
grub_rescue_cmd_initrd.
* include/grub/i386/loader.h: Likewise.
* include/grub/x86_64/loader.h: Likewise.
* include/grub/i386/pc/chainloader.h: Remove grub_chainloader_cmd.
2009-03-21 08:39:59 +00:00
|
|
|
#include <grub/lib/arg.h>
|
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.
2009-05-02 19:49:34 +00:00
|
|
|
#include <grub/normal.h>
|
2010-06-10 06:42:03 +00:00
|
|
|
#include <grub/extcmd.h>
|
2012-02-12 14:25:25 +00:00
|
|
|
#include <grub/i18n.h>
|
2015-08-10 22:27:12 +00:00
|
|
|
#include <grub/tpm.h>
|
2018-11-29 19:28:08 +00:00
|
|
|
#include <grub/verify.h>
|
2005-11-06 22:19:59 +00:00
|
|
|
|
2010-01-26 07:02:24 +00:00
|
|
|
/* Max digits for a char is 3 (0xFF is 255), similarly for an int it
|
|
|
|
is sizeof (int) * 3, and one extra for a possible -ve sign. */
|
|
|
|
#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
|
|
|
|
|
2010-05-05 10:49:31 +00:00
|
|
|
static unsigned long is_continue;
|
2010-05-05 10:04:26 +00:00
|
|
|
static unsigned long active_loops;
|
|
|
|
static unsigned long active_breaks;
|
2010-08-25 14:05:52 +00:00
|
|
|
static unsigned long function_return;
|
2010-05-12 12:29:09 +00:00
|
|
|
|
2010-08-15 06:32:33 +00:00
|
|
|
#define GRUB_SCRIPT_SCOPE_MALLOCED 1
|
|
|
|
#define GRUB_SCRIPT_SCOPE_ARGS_MALLOCED 2
|
2010-05-12 12:29:09 +00:00
|
|
|
|
2010-05-11 05:22:10 +00:00
|
|
|
/* Scope for grub script functions. */
|
|
|
|
struct grub_script_scope
|
|
|
|
{
|
2010-08-15 06:32:33 +00:00
|
|
|
unsigned flags;
|
|
|
|
unsigned shifts;
|
2010-05-12 04:49:12 +00:00
|
|
|
struct grub_script_argv argv;
|
2010-05-11 05:22:10 +00:00
|
|
|
};
|
2010-05-05 09:17:50 +00:00
|
|
|
static struct grub_script_scope *scope = 0;
|
|
|
|
|
2010-07-29 18:02:56 +00:00
|
|
|
/* Wildcard translator for GRUB script. */
|
2010-09-03 15:05:23 +00:00
|
|
|
struct grub_script_wildcard_translator *grub_wildcard_translator;
|
2010-07-29 18:02:56 +00:00
|
|
|
|
2012-06-08 20:54:21 +00:00
|
|
|
static char*
|
|
|
|
wildcard_escape (const char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
char ch;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
len = grub_strlen (s);
|
|
|
|
p = grub_malloc (len * 2 + 1);
|
|
|
|
if (! p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while ((ch = *s++))
|
|
|
|
{
|
2012-06-19 12:13:19 +00:00
|
|
|
if (ch == '*' || ch == '\\' || ch == '?')
|
2012-06-08 20:54:21 +00:00
|
|
|
p[i++] = '\\';
|
|
|
|
p[i++] = ch;
|
|
|
|
}
|
|
|
|
p[i] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
wildcard_unescape (const char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
char ch;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
len = grub_strlen (s);
|
|
|
|
p = grub_malloc (len + 1);
|
|
|
|
if (! p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while ((ch = *s++))
|
|
|
|
{
|
|
|
|
if (ch == '\\')
|
|
|
|
p[i++] = *s++;
|
|
|
|
else
|
|
|
|
p[i++] = ch;
|
|
|
|
}
|
|
|
|
p[i] = '\0';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-08-15 06:32:33 +00:00
|
|
|
static void
|
|
|
|
replace_scope (struct grub_script_scope *new_scope)
|
|
|
|
{
|
|
|
|
if (scope)
|
|
|
|
{
|
|
|
|
scope->argv.argc += scope->shifts;
|
|
|
|
scope->argv.args -= scope->shifts;
|
|
|
|
|
|
|
|
if (scope->flags & GRUB_SCRIPT_SCOPE_ARGS_MALLOCED)
|
|
|
|
grub_script_argv_free (&scope->argv);
|
|
|
|
|
|
|
|
if (scope->flags & GRUB_SCRIPT_SCOPE_MALLOCED)
|
|
|
|
grub_free (scope);
|
|
|
|
}
|
|
|
|
scope = new_scope;
|
|
|
|
}
|
|
|
|
|
2010-05-07 04:38:09 +00:00
|
|
|
grub_err_t
|
2010-05-05 10:49:31 +00:00
|
|
|
grub_script_break (grub_command_t cmd, int argc, char *argv[])
|
2010-05-05 10:04:26 +00:00
|
|
|
{
|
|
|
|
char *p = 0;
|
|
|
|
unsigned long count;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
count = 1;
|
2012-02-12 20:33:48 +00:00
|
|
|
else if (argc > 1)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = grub_strtoul (argv[0], &p, 10);
|
|
|
|
if (grub_errno)
|
|
|
|
return grub_errno;
|
|
|
|
if (*p != '\0')
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unrecognized number"));
|
|
|
|
if (count == 0)
|
2012-03-10 12:19:46 +00:00
|
|
|
/* TRANSLATORS: 0 is a quantifier. "break" (similar to bash)
|
|
|
|
can be used e.g. to break 3 loops at once.
|
|
|
|
But asking it to break 0 loops makes no sense. */
|
2012-02-12 20:33:48 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't break 0 loops"));
|
|
|
|
}
|
2010-05-05 10:04:26 +00:00
|
|
|
|
2010-05-05 10:49:31 +00:00
|
|
|
is_continue = grub_strcmp (cmd->name, "break") ? 1 : 0;
|
2012-01-14 14:44:34 +00:00
|
|
|
active_breaks = count;
|
|
|
|
if (active_breaks > active_loops)
|
|
|
|
active_breaks = active_loops;
|
2010-05-05 10:04:26 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-06-13 04:18:47 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_script_shift (grub_command_t cmd __attribute__((unused)),
|
|
|
|
int argc, char *argv[])
|
2010-05-07 04:38:09 +00:00
|
|
|
{
|
|
|
|
char *p = 0;
|
|
|
|
unsigned long n = 0;
|
|
|
|
|
|
|
|
if (! scope)
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
n = 1;
|
2010-05-12 12:05:07 +00:00
|
|
|
|
2010-05-07 04:38:09 +00:00
|
|
|
else if (argc > 1)
|
|
|
|
return GRUB_ERR_BAD_ARGUMENT;
|
2010-05-12 12:05:07 +00:00
|
|
|
|
2010-05-07 04:38:09 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
n = grub_strtoul (argv[0], &p, 10);
|
2010-05-12 12:13:49 +00:00
|
|
|
if (*p != '\0')
|
2010-05-07 04:38:09 +00:00
|
|
|
return GRUB_ERR_BAD_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2010-05-12 12:13:49 +00:00
|
|
|
if (n > scope->argv.argc)
|
|
|
|
return GRUB_ERR_BAD_ARGUMENT;
|
|
|
|
|
2010-08-15 06:32:33 +00:00
|
|
|
scope->shifts += n;
|
2010-05-12 12:05:07 +00:00
|
|
|
scope->argv.argc -= n;
|
|
|
|
scope->argv.args += n;
|
2010-05-07 04:38:09 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-08-15 06:32:33 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_script_setparams (grub_command_t cmd __attribute__((unused)),
|
|
|
|
int argc, char **args)
|
|
|
|
{
|
|
|
|
struct grub_script_scope *new_scope;
|
2010-09-04 05:26:23 +00:00
|
|
|
struct grub_script_argv argv = { 0, 0, 0 };
|
2010-08-15 06:32:33 +00:00
|
|
|
|
|
|
|
if (! scope)
|
|
|
|
return GRUB_ERR_INVALID_COMMAND;
|
|
|
|
|
|
|
|
new_scope = grub_malloc (sizeof (*new_scope));
|
|
|
|
if (! new_scope)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
if (grub_script_argv_make (&argv, argc, args))
|
|
|
|
{
|
|
|
|
grub_free (new_scope);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_scope->shifts = 0;
|
|
|
|
new_scope->argv = argv;
|
|
|
|
new_scope->flags = GRUB_SCRIPT_SCOPE_MALLOCED |
|
|
|
|
GRUB_SCRIPT_SCOPE_ARGS_MALLOCED;
|
|
|
|
|
|
|
|
replace_scope (new_scope);
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-08-25 14:05:52 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_script_return (grub_command_t cmd __attribute__((unused)),
|
|
|
|
int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
unsigned long n;
|
|
|
|
|
|
|
|
if (! scope || argc > 1)
|
2012-02-12 14:25:25 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
2012-03-11 22:36:35 +00:00
|
|
|
/* TRANSLATORS: It's about not being
|
|
|
|
inside a function. "return" can be used only
|
|
|
|
in a function and this error occurs if it's used
|
|
|
|
anywhere else. */
|
2012-03-04 11:14:33 +00:00
|
|
|
N_("not in function body"));
|
2010-08-25 14:05:52 +00:00
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
{
|
2012-02-04 11:21:21 +00:00
|
|
|
const char *t;
|
2010-08-25 14:05:52 +00:00
|
|
|
function_return = 1;
|
2012-02-04 10:52:10 +00:00
|
|
|
t = grub_env_get ("?");
|
|
|
|
if (!t)
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
return grub_strtoul (t, NULL, 10);
|
2010-08-25 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n = grub_strtoul (argv[0], &p, 10);
|
2012-02-12 20:33:48 +00:00
|
|
|
if (grub_errno)
|
|
|
|
return grub_errno;
|
2010-08-25 14:05:52 +00:00
|
|
|
if (*p != '\0')
|
2012-02-12 20:33:48 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
N_("unrecognized number"));
|
2010-08-25 14:05:52 +00:00
|
|
|
|
|
|
|
function_return = 1;
|
2012-02-12 20:33:48 +00:00
|
|
|
return n ? grub_error (n, N_("false")) : GRUB_ERR_NONE;
|
2010-08-25 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
static int
|
|
|
|
grub_env_special (const char *name)
|
2010-05-05 09:17:50 +00:00
|
|
|
{
|
2010-05-12 04:49:12 +00:00
|
|
|
if (grub_isdigit (name[0]) ||
|
|
|
|
grub_strcmp (name, "#") == 0 ||
|
|
|
|
grub_strcmp (name, "*") == 0 ||
|
|
|
|
grub_strcmp (name, "@") == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-05 09:17:50 +00:00
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
static char **
|
|
|
|
grub_script_env_get (const char *name, grub_script_arg_type_t type)
|
|
|
|
{
|
2010-08-15 06:32:33 +00:00
|
|
|
unsigned i;
|
2010-06-10 06:42:03 +00:00
|
|
|
struct grub_script_argv result = { 0, 0, 0 };
|
2010-05-05 09:17:50 +00:00
|
|
|
|
2010-05-21 10:13:24 +00:00
|
|
|
if (grub_script_argv_next (&result))
|
|
|
|
goto fail;
|
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
if (! grub_env_special (name))
|
2010-05-05 09:17:50 +00:00
|
|
|
{
|
2011-11-11 19:34:37 +00:00
|
|
|
const char *v = grub_env_get (name);
|
2010-05-12 04:49:12 +00:00
|
|
|
if (v && v[0])
|
2010-05-05 09:17:50 +00:00
|
|
|
{
|
2010-05-12 04:49:12 +00:00
|
|
|
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
2010-05-21 10:13:24 +00:00
|
|
|
{
|
|
|
|
if (grub_script_argv_split_append (&result, v))
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-05-12 04:49:12 +00:00
|
|
|
else
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_append (&result, v, grub_strlen (v)))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-05-05 09:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 04:49:12 +00:00
|
|
|
else if (! scope)
|
2010-05-21 10:13:24 +00:00
|
|
|
{
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_append (&result, 0, 0))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2010-05-05 09:17:50 +00:00
|
|
|
else if (grub_strcmp (name, "#") == 0)
|
|
|
|
{
|
2010-05-12 04:49:12 +00:00
|
|
|
char buffer[ERRNO_DIGITS_MAX + 1];
|
|
|
|
grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_append (&result, buffer, grub_strlen (buffer)))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
|
|
|
else if (grub_strcmp (name, "*") == 0)
|
|
|
|
{
|
2010-05-21 10:13:24 +00:00
|
|
|
for (i = 0; i < scope->argv.argc; i++)
|
2010-05-12 04:49:12 +00:00
|
|
|
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
|
|
|
{
|
2010-05-21 10:13:24 +00:00
|
|
|
if (i != 0 && grub_script_argv_next (&result))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
|
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-11 19:34:37 +00:00
|
|
|
if (i != 0 && grub_script_argv_append (&result, " ", 1))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
|
|
|
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_append (&result, scope->argv.args[i],
|
|
|
|
grub_strlen (scope->argv.args[i])))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (grub_strcmp (name, "@") == 0)
|
|
|
|
{
|
2010-05-21 10:13:24 +00:00
|
|
|
for (i = 0; i < scope->argv.argc; i++)
|
2010-05-12 04:49:12 +00:00
|
|
|
{
|
2010-05-21 10:13:24 +00:00
|
|
|
if (i != 0 && grub_script_argv_next (&result))
|
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
|
|
|
|
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
2010-05-21 10:13:24 +00:00
|
|
|
{
|
|
|
|
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-05-12 04:49:12 +00:00
|
|
|
else
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_append (&result, scope->argv.args[i],
|
|
|
|
grub_strlen (scope->argv.args[i])))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
2010-05-05 09:17:50 +00:00
|
|
|
}
|
|
|
|
else
|
2010-05-12 04:49:12 +00:00
|
|
|
{
|
|
|
|
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)
|
2010-05-21 10:13:24 +00:00
|
|
|
{
|
|
|
|
if (grub_script_argv_split_append (&result,
|
|
|
|
scope->argv.args[num - 1]))
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-05-12 04:49:12 +00:00
|
|
|
else
|
2011-11-11 19:34:37 +00:00
|
|
|
if (grub_script_argv_append (&result, scope->argv.args[num - 1],
|
|
|
|
grub_strlen (scope->argv.args[num - 1])
|
|
|
|
))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 05:15:22 +00:00
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
return result.args;
|
2010-05-21 10:13:24 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
grub_script_argv_free (&result);
|
|
|
|
return 0;
|
2010-05-05 09:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_script_env_set (const char *name, const char *val)
|
|
|
|
{
|
2010-05-12 04:49:12 +00:00
|
|
|
if (grub_env_special (name))
|
2012-02-12 14:25:25 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
N_("invalid variable name `%s'"), name);
|
2010-05-05 09:17:50 +00:00
|
|
|
|
|
|
|
return grub_env_set (name, val);
|
|
|
|
}
|
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
struct gettext_context
|
|
|
|
{
|
|
|
|
char **allowed_strings;
|
|
|
|
grub_size_t nallowed_strings;
|
|
|
|
grub_size_t additional_len;
|
|
|
|
};
|
|
|
|
|
2012-03-11 13:46:48 +00:00
|
|
|
static int
|
|
|
|
parse_string (const char *str,
|
2013-03-02 11:17:52 +00:00
|
|
|
int (*hook) (const char *var, grub_size_t varlen,
|
|
|
|
char **ptr, struct gettext_context *ctx),
|
|
|
|
struct gettext_context *ctx,
|
|
|
|
char *put)
|
2012-03-11 13:46:48 +00:00
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
int escaped = 0;
|
|
|
|
const char *optr;
|
|
|
|
|
|
|
|
for (ptr = str; ptr && *ptr; )
|
|
|
|
switch (*ptr)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
escaped = !escaped;
|
|
|
|
if (!escaped && put)
|
2013-03-02 11:17:52 +00:00
|
|
|
*(put++) = '\\';
|
2012-03-11 13:46:48 +00:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
case '$':
|
|
|
|
if (escaped)
|
|
|
|
{
|
|
|
|
escaped = 0;
|
|
|
|
if (put)
|
2013-03-02 11:17:52 +00:00
|
|
|
*(put++) = *ptr;
|
2012-03-11 13:46:48 +00:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
switch (*ptr)
|
|
|
|
{
|
|
|
|
case '{':
|
|
|
|
{
|
|
|
|
optr = ptr + 1;
|
|
|
|
ptr = grub_strchr (optr, '}');
|
|
|
|
if (!ptr)
|
|
|
|
break;
|
2013-03-02 11:17:52 +00:00
|
|
|
if (hook (optr, ptr - optr, &put, ctx))
|
2012-03-11 13:46:48 +00:00
|
|
|
return 1;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '0' ... '9':
|
|
|
|
optr = ptr;
|
|
|
|
while (*ptr >= '0' && *ptr <= '9')
|
|
|
|
ptr++;
|
2013-03-02 11:17:52 +00:00
|
|
|
if (hook (optr, ptr - optr, &put, ctx))
|
2012-03-11 13:46:48 +00:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case 'a' ... 'z':
|
|
|
|
case 'A' ... 'Z':
|
|
|
|
case '_':
|
|
|
|
optr = ptr;
|
|
|
|
while ((*ptr >= '0' && *ptr <= '9')
|
|
|
|
|| (*ptr >= 'a' && *ptr <= 'z')
|
|
|
|
|| (*ptr >= 'A' && *ptr <= 'Z')
|
|
|
|
|| *ptr == '_')
|
|
|
|
ptr++;
|
2013-03-02 11:17:52 +00:00
|
|
|
if (hook (optr, ptr - optr, &put, ctx))
|
2012-03-11 13:46:48 +00:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
case '#':
|
2013-03-02 11:17:52 +00:00
|
|
|
if (hook (ptr, 1, &put, ctx))
|
2012-03-11 13:46:48 +00:00
|
|
|
return 1;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (put)
|
2013-03-02 11:17:52 +00:00
|
|
|
*(put++) = '$';
|
2012-03-11 13:46:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (escaped && put)
|
2013-03-02 11:17:52 +00:00
|
|
|
*(put++) = '\\';
|
2012-03-11 13:46:48 +00:00
|
|
|
escaped = 0;
|
|
|
|
if (put)
|
2013-03-02 11:17:52 +00:00
|
|
|
*(put++) = *ptr;
|
2012-03-11 13:46:48 +00:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-02 11:17:52 +00:00
|
|
|
if (put)
|
|
|
|
*(put++) = 0;
|
2012-03-11 13:46:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-03-02 11:17:52 +00:00
|
|
|
gettext_putvar (const char *str, grub_size_t len,
|
|
|
|
char **ptr, struct gettext_context *ctx)
|
2012-03-11 13:46:48 +00:00
|
|
|
{
|
2013-03-02 11:17:52 +00:00
|
|
|
const char *var;
|
|
|
|
grub_size_t i;
|
2012-03-11 13:46:48 +00:00
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
for (i = 0; i < ctx->nallowed_strings; i++)
|
|
|
|
if (grub_strncmp (ctx->allowed_strings[i], str, len) == 0
|
|
|
|
&& ctx->allowed_strings[i][len] == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == ctx->nallowed_strings)
|
2012-03-11 13:46:48 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
/* Enough for any number. */
|
|
|
|
if (len == 1 && str[0] == '#')
|
|
|
|
{
|
|
|
|
grub_snprintf (*ptr, 30, "%u", scope->argv.argc);
|
|
|
|
*ptr += grub_strlen (*ptr);
|
2012-03-11 13:46:48 +00:00
|
|
|
return 0;
|
2013-03-02 11:17:52 +00:00
|
|
|
}
|
|
|
|
var = grub_env_get (ctx->allowed_strings[i]);
|
|
|
|
if (var)
|
|
|
|
*ptr = grub_stpcpy (*ptr, var);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-11 13:46:48 +00:00
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
static int
|
|
|
|
gettext_save_allow (const char *str, grub_size_t len,
|
|
|
|
char **ptr __attribute__ ((unused)),
|
|
|
|
struct gettext_context *ctx)
|
|
|
|
{
|
|
|
|
ctx->allowed_strings[ctx->nallowed_strings++] = grub_strndup (str, len);
|
|
|
|
if (!ctx->allowed_strings[ctx->nallowed_strings - 1])
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-11 13:46:48 +00:00
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
static int
|
|
|
|
gettext_getlen (const char *str, grub_size_t len,
|
|
|
|
char **ptr __attribute__ ((unused)),
|
|
|
|
struct gettext_context *ctx)
|
|
|
|
{
|
|
|
|
const char *var;
|
|
|
|
grub_size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->nallowed_strings; i++)
|
|
|
|
if (grub_strncmp (ctx->allowed_strings[i], str, len) == 0
|
|
|
|
&& ctx->allowed_strings[i][len] == 0)
|
|
|
|
break;
|
|
|
|
if (i == ctx->nallowed_strings)
|
|
|
|
return 0;
|
2012-03-11 13:46:48 +00:00
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
/* Enough for any number. */
|
|
|
|
if (len == 1 && str[0] == '#')
|
|
|
|
{
|
|
|
|
ctx->additional_len += 30;
|
2012-03-11 13:46:48 +00:00
|
|
|
return 0;
|
2013-03-02 11:17:52 +00:00
|
|
|
}
|
|
|
|
var = grub_env_get (ctx->allowed_strings[i]);
|
|
|
|
if (var)
|
|
|
|
ctx->additional_len += grub_strlen (var);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-11 13:46:48 +00:00
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
static int
|
|
|
|
gettext_append (struct grub_script_argv *result, const char *orig_str)
|
|
|
|
{
|
|
|
|
const char *template;
|
|
|
|
char *res = 0;
|
|
|
|
struct gettext_context ctx = {
|
|
|
|
.allowed_strings = 0,
|
|
|
|
.nallowed_strings = 0,
|
|
|
|
.additional_len = 1
|
|
|
|
};
|
|
|
|
int rval = 1;
|
|
|
|
const char *iptr;
|
2012-03-11 13:46:48 +00:00
|
|
|
|
|
|
|
grub_size_t dollar_cnt = 0;
|
|
|
|
|
|
|
|
for (iptr = orig_str; *iptr; iptr++)
|
|
|
|
if (*iptr == '$')
|
|
|
|
dollar_cnt++;
|
2013-03-02 11:17:52 +00:00
|
|
|
ctx.allowed_strings = grub_malloc (sizeof (ctx.allowed_strings[0]) * dollar_cnt);
|
2012-03-11 13:46:48 +00:00
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
if (parse_string (orig_str, gettext_save_allow, &ctx, 0))
|
2012-03-11 13:46:48 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
template = _(orig_str);
|
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
if (parse_string (template, gettext_getlen, &ctx, 0))
|
2012-03-11 13:46:48 +00:00
|
|
|
goto fail;
|
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
res = grub_malloc (grub_strlen (template) + ctx.additional_len);
|
2012-03-11 13:46:48 +00:00
|
|
|
if (!res)
|
|
|
|
goto fail;
|
|
|
|
|
2013-03-02 11:17:52 +00:00
|
|
|
if (parse_string (template, gettext_putvar, &ctx, res))
|
2012-03-11 13:46:48 +00:00
|
|
|
goto fail;
|
|
|
|
|
2012-06-08 20:54:21 +00:00
|
|
|
char *escaped = 0;
|
|
|
|
escaped = wildcard_escape (res);
|
|
|
|
if (grub_script_argv_append (result, escaped, grub_strlen (escaped)))
|
2012-03-11 13:46:48 +00:00
|
|
|
{
|
|
|
|
grub_free (escaped);
|
|
|
|
goto fail;
|
2012-06-08 20:54:21 +00:00
|
|
|
}
|
|
|
|
grub_free (escaped);
|
2012-03-11 13:46:48 +00:00
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
fail:
|
|
|
|
grub_free (res);
|
|
|
|
{
|
|
|
|
grub_size_t i;
|
2013-03-02 11:17:52 +00:00
|
|
|
for (i = 0; i < ctx.nallowed_strings; i++)
|
|
|
|
grub_free (ctx.allowed_strings[i]);
|
2012-03-11 13:46:48 +00:00
|
|
|
}
|
2013-03-02 11:17:52 +00:00
|
|
|
grub_free (ctx.allowed_strings);
|
2012-03-11 13:46:48 +00:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2013-03-03 14:26:29 +00:00
|
|
|
static int
|
|
|
|
append (struct grub_script_argv *result,
|
|
|
|
const char *s, int escape_type)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
char *p = 0;
|
|
|
|
|
|
|
|
if (escape_type == 0)
|
|
|
|
return grub_script_argv_append (result, s, grub_strlen (s));
|
|
|
|
|
|
|
|
if (escape_type > 0)
|
|
|
|
p = wildcard_escape (s);
|
|
|
|
else if (escape_type < 0)
|
|
|
|
p = wildcard_unescape (s);
|
|
|
|
|
|
|
|
if (! p)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = grub_script_argv_append (result, p, grub_strlen (p));
|
|
|
|
grub_free (p);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-07-29 17:22:09 +00:00
|
|
|
/* Convert arguments in ARGLIST into ARGV form. */
|
2010-05-12 04:49:12 +00:00
|
|
|
static int
|
|
|
|
grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
|
|
|
|
struct grub_script_argv *argv)
|
2010-01-22 13:37:27 +00:00
|
|
|
{
|
|
|
|
int i;
|
2010-05-12 04:49:12 +00:00
|
|
|
char **values = 0;
|
|
|
|
struct grub_script_arg *arg = 0;
|
2010-06-10 06:42:03 +00:00
|
|
|
struct grub_script_argv result = { 0, 0, 0 };
|
2010-05-12 04:49:12 +00:00
|
|
|
|
2010-05-21 10:13:24 +00:00
|
|
|
for (; arglist && arglist->arg; arglist = arglist->next)
|
2005-11-06 22:19:59 +00:00
|
|
|
{
|
2010-05-21 10:13:24 +00:00
|
|
|
if (grub_script_argv_next (&result))
|
|
|
|
goto fail;
|
2010-05-12 04:49:12 +00:00
|
|
|
|
2010-01-22 13:37:27 +00:00
|
|
|
arg = arglist->arg;
|
|
|
|
while (arg)
|
2005-11-06 22:19:59 +00:00
|
|
|
{
|
2010-01-22 13:37:27 +00:00
|
|
|
switch (arg->type)
|
|
|
|
{
|
|
|
|
case GRUB_SCRIPT_ARG_TYPE_VAR:
|
2010-05-12 04:49:12 +00:00
|
|
|
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
|
2015-01-28 17:35:28 +00:00
|
|
|
{
|
|
|
|
int need_cleanup = 0;
|
2010-05-18 15:33:35 +00:00
|
|
|
|
2015-01-28 17:35:28 +00:00
|
|
|
values = grub_script_env_get (arg->str, arg->type);
|
|
|
|
for (i = 0; values && values[i]; i++)
|
|
|
|
{
|
|
|
|
if (!need_cleanup)
|
|
|
|
{
|
|
|
|
if (i != 0 && grub_script_argv_next (&result))
|
|
|
|
{
|
|
|
|
need_cleanup = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg->type == GRUB_SCRIPT_ARG_TYPE_VAR)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char ch;
|
|
|
|
char *p;
|
|
|
|
char *op;
|
|
|
|
const char *s = values[i];
|
|
|
|
|
|
|
|
len = grub_strlen (values[i]);
|
|
|
|
/* \? -> \\\? */
|
|
|
|
/* \* -> \\\* */
|
|
|
|
/* \ -> \\ */
|
|
|
|
p = grub_malloc (len * 2 + 1);
|
|
|
|
if (! p)
|
|
|
|
{
|
|
|
|
need_cleanup = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
op = p;
|
|
|
|
while ((ch = *s++))
|
|
|
|
{
|
|
|
|
if (ch == '\\')
|
|
|
|
{
|
|
|
|
*op++ = '\\';
|
|
|
|
if (*s == '?' || *s == '*')
|
|
|
|
*op++ = '\\';
|
|
|
|
}
|
|
|
|
*op++ = ch;
|
|
|
|
}
|
|
|
|
*op = '\0';
|
|
|
|
|
2016-01-12 19:50:30 +00:00
|
|
|
need_cleanup = grub_script_argv_append (&result, p, op - p);
|
|
|
|
grub_free (p);
|
|
|
|
/* Fall through to cleanup */
|
2015-01-28 17:35:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-12 19:50:30 +00:00
|
|
|
need_cleanup = append (&result, values[i], 1);
|
|
|
|
/* Fall through to cleanup */
|
2015-01-28 17:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
grub_free (values[i]);
|
|
|
|
}
|
|
|
|
grub_free (values);
|
|
|
|
|
|
|
|
if (need_cleanup)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2010-01-22 13:37:27 +00:00
|
|
|
|
2010-04-30 06:39:31 +00:00
|
|
|
case GRUB_SCRIPT_ARG_TYPE_BLOCK:
|
2012-06-21 20:02:09 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
if (grub_script_argv_append (&result, "{", 1))
|
|
|
|
goto fail;
|
|
|
|
p = wildcard_escape (arg->str);
|
|
|
|
if (!p)
|
|
|
|
goto fail;
|
|
|
|
if (grub_script_argv_append (&result, p,
|
|
|
|
grub_strlen (p)))
|
|
|
|
{
|
|
|
|
grub_free (p);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
grub_free (p);
|
|
|
|
if (grub_script_argv_append (&result, "}", 1))
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-08-09 16:12:24 +00:00
|
|
|
result.script = arg->script;
|
2010-06-10 06:42:03 +00:00
|
|
|
break;
|
|
|
|
|
2010-01-22 13:37:27 +00:00
|
|
|
case GRUB_SCRIPT_ARG_TYPE_TEXT:
|
2012-02-26 18:02:46 +00:00
|
|
|
if (arg->str[0] &&
|
2011-11-11 19:34:37 +00:00
|
|
|
grub_script_argv_append (&result, arg->str,
|
|
|
|
grub_strlen (arg->str)))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-01-22 13:37:27 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-26 18:02:46 +00:00
|
|
|
case GRUB_SCRIPT_ARG_TYPE_GETTEXT:
|
|
|
|
{
|
2012-03-11 13:46:48 +00:00
|
|
|
if (gettext_append (&result, arg->str))
|
2012-02-26 18:02:46 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-01-22 13:37:27 +00:00
|
|
|
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
|
|
|
|
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
|
2013-03-03 14:26:29 +00:00
|
|
|
if (append (&result, arg->str, 1))
|
2010-05-21 10:13:24 +00:00
|
|
|
goto fail;
|
2010-01-22 13:37:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
arg = arg->next;
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
if (! result.args[result.argc - 1])
|
|
|
|
result.argc--;
|
2005-11-06 22:19:59 +00:00
|
|
|
|
2010-07-29 17:22:09 +00:00
|
|
|
/* Perform wildcard expansion. */
|
|
|
|
|
2012-06-08 20:54:21 +00:00
|
|
|
int j;
|
|
|
|
int failed = 0;
|
|
|
|
struct grub_script_argv unexpanded = result;
|
|
|
|
|
|
|
|
result.argc = 0;
|
|
|
|
result.args = 0;
|
|
|
|
for (i = 0; unexpanded.args[i]; i++)
|
2010-07-29 17:22:09 +00:00
|
|
|
{
|
|
|
|
char **expansions = 0;
|
2012-06-08 20:54:21 +00:00
|
|
|
if (grub_wildcard_translator
|
|
|
|
&& grub_wildcard_translator->expand (unexpanded.args[i],
|
|
|
|
&expansions))
|
2010-07-29 17:22:09 +00:00
|
|
|
{
|
2012-06-08 20:54:21 +00:00
|
|
|
grub_script_argv_free (&unexpanded);
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-07-29 17:22:09 +00:00
|
|
|
|
2012-06-08 20:54:21 +00:00
|
|
|
if (! expansions)
|
|
|
|
{
|
|
|
|
grub_script_argv_next (&result);
|
2013-03-03 14:26:29 +00:00
|
|
|
append (&result, unexpanded.args[i], -1);
|
2012-06-08 20:54:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (j = 0; expansions[j]; j++)
|
2010-07-29 17:22:09 +00:00
|
|
|
{
|
2012-06-08 20:54:21 +00:00
|
|
|
failed = (failed || grub_script_argv_next (&result) ||
|
2013-03-03 14:26:29 +00:00
|
|
|
append (&result, expansions[j], 0));
|
2012-06-08 20:54:21 +00:00
|
|
|
grub_free (expansions[j]);
|
2010-07-29 17:22:09 +00:00
|
|
|
}
|
2012-06-08 20:54:21 +00:00
|
|
|
grub_free (expansions);
|
|
|
|
|
|
|
|
if (failed)
|
2010-07-29 17:22:09 +00:00
|
|
|
{
|
2012-06-08 20:54:21 +00:00
|
|
|
grub_script_argv_free (&unexpanded);
|
|
|
|
goto fail;
|
2010-07-29 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 20:54:21 +00:00
|
|
|
grub_script_argv_free (&unexpanded);
|
2010-05-18 15:33:35 +00:00
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
*argv = result;
|
|
|
|
return 0;
|
2010-05-21 10:13:24 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
grub_script_argv_free (&result);
|
|
|
|
return 1;
|
2010-05-12 04:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_script_execute_cmd (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char errnobuf[ERRNO_DIGITS_MAX + 1];
|
|
|
|
|
|
|
|
if (cmd == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = cmd->exec (cmd);
|
2010-01-23 03:42:46 +00:00
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
|
|
|
|
grub_env_set ("?", errnobuf);
|
|
|
|
return ret;
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 09:17:50 +00:00
|
|
|
/* Execute a function call. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_function_call (grub_script_function_t func, int argc, char **args)
|
|
|
|
{
|
|
|
|
grub_err_t ret = 0;
|
2010-05-05 10:04:26 +00:00
|
|
|
unsigned long loops = active_loops;
|
2010-05-11 05:22:10 +00:00
|
|
|
struct grub_script_scope *old_scope;
|
2010-05-05 09:17:50 +00:00
|
|
|
struct grub_script_scope new_scope;
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
active_loops = 0;
|
2010-08-15 06:32:33 +00:00
|
|
|
new_scope.flags = 0;
|
|
|
|
new_scope.shifts = 0;
|
2010-05-12 04:49:12 +00:00
|
|
|
new_scope.argv.argc = argc;
|
|
|
|
new_scope.argv.args = args;
|
2010-05-11 05:22:10 +00:00
|
|
|
|
|
|
|
old_scope = scope;
|
|
|
|
scope = &new_scope;
|
2010-05-05 09:17:50 +00:00
|
|
|
|
|
|
|
ret = grub_script_execute (func->func);
|
|
|
|
|
2010-08-25 14:05:52 +00:00
|
|
|
function_return = 0;
|
2010-05-05 10:04:26 +00:00
|
|
|
active_loops = loops;
|
2010-08-15 06:32:33 +00:00
|
|
|
replace_scope (old_scope); /* free any scopes by setparams */
|
2010-05-05 09:17:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-15 12:03:25 +00:00
|
|
|
/* Helper for grub_script_execute_sourcecode. */
|
|
|
|
static grub_err_t
|
|
|
|
grub_script_execute_sourcecode_getline (char **line,
|
|
|
|
int cont __attribute__ ((unused)),
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
const char **source = data;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (! *source)
|
|
|
|
{
|
|
|
|
*line = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = grub_strchr (*source, '\n');
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
*line = grub_strndup (*source, p - *source);
|
|
|
|
else
|
|
|
|
*line = grub_strdup (*source);
|
|
|
|
*source = p ? p + 1 : 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-22 15:45:14 +00:00
|
|
|
/* Execute a source script. */
|
|
|
|
grub_err_t
|
2013-06-07 16:36:42 +00:00
|
|
|
grub_script_execute_sourcecode (const char *source)
|
2010-07-22 15:45:14 +00:00
|
|
|
{
|
|
|
|
grub_err_t ret = 0;
|
|
|
|
struct grub_script *parsed_script;
|
|
|
|
|
|
|
|
while (source)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
2013-01-15 12:03:25 +00:00
|
|
|
grub_script_execute_sourcecode_getline (&line, 0, &source);
|
|
|
|
parsed_script = grub_script_parse
|
|
|
|
(line, grub_script_execute_sourcecode_getline, &source);
|
2010-07-22 15:45:14 +00:00
|
|
|
if (! parsed_script)
|
|
|
|
{
|
|
|
|
ret = grub_errno;
|
2013-06-07 16:36:42 +00:00
|
|
|
grub_free (line);
|
2010-07-22 15:45:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = grub_script_execute (parsed_script);
|
2013-06-07 16:36:42 +00:00
|
|
|
grub_script_free (parsed_script);
|
2010-07-22 15:45:14 +00:00
|
|
|
grub_free (line);
|
|
|
|
}
|
|
|
|
|
2013-06-07 16:36:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute a source script in new scope. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_new_scope (const char *source, int argc, char **args)
|
|
|
|
{
|
|
|
|
grub_err_t ret = 0;
|
|
|
|
struct grub_script_scope new_scope;
|
|
|
|
struct grub_script_scope *old_scope;
|
|
|
|
|
|
|
|
new_scope.argv.argc = argc;
|
|
|
|
new_scope.argv.args = args;
|
|
|
|
new_scope.flags = 0;
|
|
|
|
|
|
|
|
old_scope = scope;
|
|
|
|
scope = &new_scope;
|
|
|
|
|
|
|
|
ret = grub_script_execute_sourcecode (source);
|
|
|
|
|
2010-05-11 05:22:10 +00:00
|
|
|
scope = old_scope;
|
2010-05-05 09:17:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* Execute a single command line. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
|
|
|
|
grub_command_t grubcmd;
|
|
|
|
grub_err_t ret = 0;
|
|
|
|
grub_script_function_t func = 0;
|
2009-12-29 09:04:06 +00:00
|
|
|
char errnobuf[18];
|
2015-08-10 22:27:12 +00:00
|
|
|
char *cmdname, *cmdstring;
|
|
|
|
int argc, offset = 0, cmdlen = 0;
|
|
|
|
unsigned int i;
|
2010-09-05 09:27:28 +00:00
|
|
|
char **args;
|
|
|
|
int invert;
|
2010-06-10 06:42:03 +00:00
|
|
|
struct grub_script_argv argv = { 0, 0, 0 };
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Lookup the command. */
|
2010-08-10 04:22:16 +00:00
|
|
|
if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
|
2010-01-22 13:37:27 +00:00
|
|
|
return grub_errno;
|
|
|
|
|
2018-11-29 19:28:08 +00:00
|
|
|
for (i = 0; i < argv.argc; i++)
|
|
|
|
{
|
|
|
|
cmdlen += grub_strlen (argv.args[i]) + 1;
|
|
|
|
}
|
2015-08-10 22:27:12 +00:00
|
|
|
|
|
|
|
cmdstring = grub_malloc (cmdlen);
|
|
|
|
if (!cmdstring)
|
2018-11-29 19:28:08 +00:00
|
|
|
{
|
|
|
|
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
|
|
|
|
N_("cannot allocate command buffer"));
|
|
|
|
}
|
2015-08-10 22:27:12 +00:00
|
|
|
|
2018-11-29 19:28:08 +00:00
|
|
|
for (i = 0; i < argv.argc; i++)
|
|
|
|
{
|
|
|
|
offset += grub_snprintf (cmdstring + offset, cmdlen - offset, "%s ",
|
|
|
|
argv.args[i]);
|
|
|
|
}
|
|
|
|
cmdstring[cmdlen - 1] = '\0';
|
|
|
|
grub_verify_string (cmdstring, GRUB_VERIFY_COMMAND);
|
|
|
|
grub_free (cmdstring);
|
2010-09-05 09:27:28 +00:00
|
|
|
invert = 0;
|
|
|
|
argc = argv.argc - 1;
|
|
|
|
args = argv.args + 1;
|
2010-05-12 04:49:12 +00:00
|
|
|
cmdname = argv.args[0];
|
2010-09-05 09:27:28 +00:00
|
|
|
if (grub_strcmp (cmdname, "!") == 0)
|
|
|
|
{
|
|
|
|
if (argv.argc < 2 || ! argv.args[1])
|
|
|
|
{
|
|
|
|
grub_script_argv_free (&argv);
|
2012-02-12 14:25:25 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT,
|
|
|
|
N_("no command is specified"));
|
2010-09-05 09:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
invert = 1;
|
|
|
|
argc = argv.argc - 2;
|
|
|
|
args = argv.args + 2;
|
|
|
|
cmdname = argv.args[1];
|
|
|
|
}
|
2009-06-04 16:18:35 +00:00
|
|
|
grubcmd = grub_command_find (cmdname);
|
2005-11-06 22:19:59 +00:00
|
|
|
if (! grubcmd)
|
|
|
|
{
|
2008-01-15 15:32:17 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* It's not a GRUB command, try all functions. */
|
2009-06-04 16:18:35 +00:00
|
|
|
func = grub_script_function_find (cmdname);
|
2005-11-06 22:19:59 +00:00
|
|
|
if (! func)
|
2006-04-30 20:29:51 +00:00
|
|
|
{
|
|
|
|
/* As a last resort, try if it is an assignment. */
|
2009-06-04 16:18:35 +00:00
|
|
|
char *assign = grub_strdup (cmdname);
|
2006-04-30 20:29:51 +00:00
|
|
|
char *eq = grub_strchr (assign, '=');
|
|
|
|
|
|
|
|
if (eq)
|
|
|
|
{
|
2009-12-13 12:35:20 +00:00
|
|
|
/* This was set because the command was not found. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
2006-04-30 20:29:51 +00:00
|
|
|
/* Create two strings and set the variable. */
|
|
|
|
*eq = '\0';
|
|
|
|
eq++;
|
2010-05-05 09:17:50 +00:00
|
|
|
grub_script_env_set (assign, eq);
|
2006-04-30 20:29:51 +00:00
|
|
|
}
|
|
|
|
grub_free (assign);
|
2009-09-08 19:10:34 +00:00
|
|
|
|
2009-12-29 09:04:06 +00:00
|
|
|
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
|
2010-05-05 09:17:50 +00:00
|
|
|
grub_script_env_set ("?", errnobuf);
|
2009-09-08 19:10:34 +00:00
|
|
|
|
2010-05-18 15:33:35 +00:00
|
|
|
grub_script_argv_free (&argv);
|
2010-02-14 13:46:19 +00:00
|
|
|
grub_print_error ();
|
|
|
|
|
2006-04-30 20:29:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute the GRUB command or function. */
|
|
|
|
if (grubcmd)
|
2010-06-10 06:42:03 +00:00
|
|
|
{
|
2010-09-20 21:01:34 +00:00
|
|
|
if (grub_extractor_level && !(grubcmd->flags
|
|
|
|
& GRUB_COMMAND_FLAG_EXTRACTOR))
|
|
|
|
ret = grub_error (GRUB_ERR_EXTRACTOR,
|
|
|
|
"%s isn't allowed to execute in an extractor",
|
2010-09-16 21:48:32 +00:00
|
|
|
cmdname);
|
|
|
|
else if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) &&
|
|
|
|
(grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD))
|
2010-09-05 09:27:28 +00:00
|
|
|
ret = grub_extcmd_dispatcher (grubcmd, argc, args, argv.script);
|
2010-06-10 06:42:03 +00:00
|
|
|
else
|
2010-09-05 09:27:28 +00:00
|
|
|
ret = (grubcmd->func) (grubcmd, argc, args);
|
2010-06-10 06:42:03 +00:00
|
|
|
}
|
2005-11-06 22:19:59 +00:00
|
|
|
else
|
2010-09-05 09:27:28 +00:00
|
|
|
ret = grub_script_function_call (func, argc, args);
|
|
|
|
|
|
|
|
if (invert)
|
2010-09-07 16:16:04 +00:00
|
|
|
{
|
|
|
|
if (ret == GRUB_ERR_TEST_FAILURE)
|
|
|
|
grub_errno = ret = GRUB_ERR_NONE;
|
|
|
|
else if (ret == GRUB_ERR_NONE)
|
2012-02-12 14:25:25 +00:00
|
|
|
ret = grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
|
2010-09-07 16:16:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
grub_print_error ();
|
2010-09-08 03:51:02 +00:00
|
|
|
ret = GRUB_ERR_NONE;
|
2010-09-07 16:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Free arguments. */
|
2010-05-12 04:49:12 +00:00
|
|
|
grub_script_argv_free (&argv);
|
2005-11-06 22:19:59 +00:00
|
|
|
|
2010-02-10 17:09:44 +00:00
|
|
|
if (grub_errno == GRUB_ERR_TEST_FAILURE)
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
grub_print_error ();
|
|
|
|
|
2009-12-29 09:04:06 +00:00
|
|
|
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
|
2005-11-06 22:19:59 +00:00
|
|
|
grub_env_set ("?", errnobuf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-31 09:15:43 +00:00
|
|
|
/* Execute a block of one or more commands. */
|
2005-11-06 22:19:59 +00:00
|
|
|
grub_err_t
|
2010-05-05 08:35:06 +00:00
|
|
|
grub_script_execute_cmdlist (struct grub_script_cmd *list)
|
2005-11-06 22:19:59 +00:00
|
|
|
{
|
2010-01-26 07:02:24 +00:00
|
|
|
int ret = 0;
|
2010-05-05 08:35:06 +00:00
|
|
|
struct grub_script_cmd *cmd;
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Loop over every command and execute it. */
|
2010-08-25 14:05:52 +00:00
|
|
|
for (cmd = list->next; cmd; cmd = cmd->next)
|
|
|
|
{
|
|
|
|
if (active_breaks)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ret = grub_script_execute_cmd (cmd);
|
|
|
|
|
|
|
|
if (function_return)
|
|
|
|
break;
|
|
|
|
}
|
2005-11-06 22:19:59 +00:00
|
|
|
|
2010-01-26 07:02:24 +00:00
|
|
|
return ret;
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute an if statement. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
|
|
|
|
{
|
2010-08-25 14:05:52 +00:00
|
|
|
int ret;
|
2011-11-11 19:34:37 +00:00
|
|
|
const char *result;
|
2010-08-25 14:05:52 +00:00
|
|
|
struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
|
2005-11-06 22:19:59 +00:00
|
|
|
|
|
|
|
/* Check if the commands results in a true or a false. The value is
|
2006-05-28 21:58:35 +00:00
|
|
|
read from the env variable `?'. */
|
2010-08-25 14:05:52 +00:00
|
|
|
ret = grub_script_execute_cmd (cmdif->exec_to_evaluate);
|
|
|
|
if (function_return)
|
|
|
|
return ret;
|
2005-11-06 22:19:59 +00:00
|
|
|
|
2010-08-25 14:05:52 +00:00
|
|
|
result = grub_env_get ("?");
|
2009-05-03 06:50:20 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* Execute the `if' or the `else' part depending on the value of
|
2006-05-28 21:58:35 +00:00
|
|
|
`?'. */
|
|
|
|
if (result && ! grub_strcmp (result, "0"))
|
|
|
|
return grub_script_execute_cmd (cmdif->exec_on_true);
|
2005-11-06 22:19:59 +00:00
|
|
|
else
|
2006-05-28 21:58:35 +00:00
|
|
|
return grub_script_execute_cmd (cmdif->exec_on_false);
|
2005-11-06 22:19:59 +00:00
|
|
|
}
|
|
|
|
|
2010-01-23 05:33:41 +00:00
|
|
|
/* Execute a for statement. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
|
|
|
|
{
|
2010-05-19 04:55:41 +00:00
|
|
|
unsigned i;
|
|
|
|
grub_err_t result;
|
2010-06-10 06:42:03 +00:00
|
|
|
struct grub_script_argv argv = { 0, 0, 0 };
|
2010-01-23 05:33:41 +00:00
|
|
|
struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
|
|
|
|
|
2010-05-12 04:49:12 +00:00
|
|
|
if (grub_script_arglist_to_argv (cmdfor->words, &argv))
|
2010-01-23 05:33:41 +00:00
|
|
|
return grub_errno;
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
active_loops++;
|
2010-01-23 05:33:41 +00:00
|
|
|
result = 0;
|
2010-05-12 04:49:12 +00:00
|
|
|
for (i = 0; i < argv.argc; i++)
|
2010-01-23 05:33:41 +00:00
|
|
|
{
|
2010-05-05 10:49:31 +00:00
|
|
|
if (is_continue && active_breaks == 1)
|
|
|
|
active_breaks = 0;
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
if (! active_breaks)
|
|
|
|
{
|
2010-05-12 12:29:09 +00:00
|
|
|
grub_script_env_set (cmdfor->name->str, argv.args[i]);
|
2010-05-05 10:04:26 +00:00
|
|
|
result = grub_script_execute_cmd (cmdfor->list);
|
2010-08-25 14:05:52 +00:00
|
|
|
if (function_return)
|
|
|
|
break;
|
2010-05-05 10:04:26 +00:00
|
|
|
}
|
2010-01-23 05:33:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
if (active_breaks)
|
|
|
|
active_breaks--;
|
|
|
|
|
|
|
|
active_loops--;
|
2010-05-12 04:49:12 +00:00
|
|
|
grub_script_argv_free (&argv);
|
2010-01-23 05:33:41 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-01-23 05:49:26 +00:00
|
|
|
/* Execute a "while" or "until" command. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute_cmdwhile (struct grub_script_cmd *cmd)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct grub_script_cmdwhile *cmdwhile = (struct grub_script_cmdwhile *) cmd;
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
active_loops++;
|
2010-01-23 05:49:26 +00:00
|
|
|
do {
|
2010-08-25 14:05:52 +00:00
|
|
|
result = grub_script_execute_cmd (cmdwhile->cond);
|
|
|
|
if (function_return)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (cmdwhile->until ? !result : result)
|
2010-01-23 05:49:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
result = grub_script_execute_cmd (cmdwhile->list);
|
2010-08-25 14:05:52 +00:00
|
|
|
if (function_return)
|
|
|
|
break;
|
2010-05-05 10:04:26 +00:00
|
|
|
|
2010-05-05 10:49:31 +00:00
|
|
|
if (active_breaks == 1 && is_continue)
|
|
|
|
active_breaks = 0;
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
if (active_breaks)
|
2010-05-05 10:49:31 +00:00
|
|
|
break;
|
2010-05-05 10:04:26 +00:00
|
|
|
|
2010-01-23 05:49:26 +00:00
|
|
|
} while (1); /* XXX Put a check for ^C here */
|
|
|
|
|
2010-05-05 10:49:31 +00:00
|
|
|
if (active_breaks)
|
|
|
|
active_breaks--;
|
|
|
|
|
2010-05-05 10:04:26 +00:00
|
|
|
active_loops--;
|
2010-01-23 05:49:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-11-06 22:19:59 +00:00
|
|
|
/* Execute any GRUB pre-parsed command or script. */
|
|
|
|
grub_err_t
|
|
|
|
grub_script_execute (struct grub_script *script)
|
|
|
|
{
|
|
|
|
if (script == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return grub_script_execute_cmd (script->cmd);
|
|
|
|
}
|