From 0993355a68b5e954c26184a4fb19310c15eef701 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 29 Apr 2010 18:10:22 +0530 Subject: [PATCH 01/18] nested recording support --- include/grub/script_sh.h | 4 +-- script/lexer.c | 68 +++++++++++++++++----------------------- script/parser.y | 11 ++++--- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index b55b6a806..17b1c5a5a 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -282,8 +282,8 @@ struct grub_lexer_param *grub_script_lexer_init (struct grub_parser_param *parse void grub_script_lexer_fini (struct grub_lexer_param *); void grub_script_lexer_ref (struct grub_lexer_param *); void grub_script_lexer_deref (struct grub_lexer_param *); -void grub_script_lexer_record_start (struct grub_parser_param *); -char *grub_script_lexer_record_stop (struct grub_parser_param *); +unsigned grub_script_lexer_record_start (struct grub_parser_param *); +char *grub_script_lexer_record_stop (struct grub_parser_param *, unsigned); int grub_script_lexer_yywrap (struct grub_parser_param *); void grub_script_lexer_record (struct grub_parser_param *, char *); diff --git a/script/lexer.c b/script/lexer.c index 42a570348..73adf627f 100644 --- a/script/lexer.c +++ b/script/lexer.c @@ -38,68 +38,57 @@ grub_script_lexer_deref (struct grub_lexer_param *state) } /* Start recording all characters passing through the lexer. */ -void +unsigned grub_script_lexer_record_start (struct grub_parser_param *parser) { struct grub_lexer_param *lexer = parser->lexerstate; - lexer->record = 1; - lexer->recordpos = 0; - if (lexer->recording) /* reuse last record */ - return; + lexer->record++; + if (lexer->recording) + return lexer->recordpos; + lexer->recordpos = 0; lexer->recordlen = GRUB_LEXER_INITIAL_RECORD_SIZE; lexer->recording = grub_malloc (lexer->recordlen); if (!lexer->recording) { grub_script_yyerror (parser, 0); - lexer->record = 0; lexer->recordlen = 0; } + return lexer->recordpos; } char * -grub_script_lexer_record_stop (struct grub_parser_param *parser) +grub_script_lexer_record_stop (struct grub_parser_param *parser, unsigned offset) { - char *ptr; + int count; char *result; struct grub_lexer_param *lexer = parser->lexerstate; - auto char *compact (char *start, char *end); - char *compact (char *start, char *end) - { - /* Delete '{' and '}' characters and whitespaces. */ - while (*start && grub_isspace (*start)) start++; - if (*start == '{') start++; - while (*start && grub_isspace (*start)) start++; - - while (*end && grub_isspace (*end)) end--; - if (*end == '}') end--; - while (*end && grub_isspace (*end)) end--; - end[1] = '\0'; - - return start; - } - - if (!lexer->record || !lexer->recording) + if (!lexer->record) return 0; - /* XXX This is not necessary in BASH. */ + lexer->record--; + if (!lexer->recording) + return 0; - ptr = compact (lexer->recording, lexer->recording + lexer->recordpos - 1); - lexer->record = 0; - lexer->recordpos = 0; - - /* This memory would be freed by, grub_script_free. */ - result = grub_script_malloc (parser, grub_strlen (ptr) + 1); - if (result) - grub_strcpy (result, ptr); + count = lexer->recordpos - offset; + result = grub_script_malloc (parser, count + 1); + if (result) { + grub_strncpy (result, lexer->recording + offset, count); + result[count] = '\0'; + } + if (lexer->record == 0) + { + grub_free (lexer->recording); + lexer->recording = 0; + lexer->recordlen = 0; + lexer->recordpos = 0; + } return result; } -#define MAX(a,b) ((a) < (b) ? (b) : (a)) - /* Record STR if input recording is enabled. */ void grub_script_lexer_record (struct grub_parser_param *parser, char *str) @@ -108,21 +97,20 @@ grub_script_lexer_record (struct grub_parser_param *parser, char *str) char *old; struct grub_lexer_param *lexer = parser->lexerstate; - if (!lexer->record) + if (!lexer->record || !lexer->recording) return; len = grub_strlen (str); if (lexer->recordpos + len + 1 > lexer->recordlen) { old = lexer->recording; - lexer->recordlen = MAX (len, lexer->recordlen) * 2; + lexer->recordlen = grub_max (len, lexer->recordlen) * 2; lexer->recording = grub_realloc (lexer->recording, lexer->recordlen); if (!lexer->recording) { grub_free (old); - lexer->record = 0; lexer->recordpos = 0; - lexer->recordlen /= 2; + lexer->recordlen = 0; grub_script_yyerror (parser, 0); return; } diff --git a/script/parser.y b/script/parser.y index b5815ea8d..e5de35cf4 100644 --- a/script/parser.y +++ b/script/parser.y @@ -33,6 +33,7 @@ struct grub_script_arglist *arglist; struct grub_script_arg *arg; char *string; + unsigned offset; } %token GRUB_PARSER_TOKEN_BAD @@ -217,14 +218,16 @@ menuentry: "menuentry" } arguments1 { - grub_script_lexer_record_start (state); + $$ = grub_script_lexer_record_start (state); } delimiters0 "{" commands1 delimiters1 "}" { - char *menu_entry; - menu_entry = grub_script_lexer_record_stop (state); + char *def; + def = grub_script_lexer_record_stop (state, $4); + *grub_strrchr(def, '}') = '\0'; + grub_script_lexer_deref (state->lexerstate); - $$ = grub_script_create_cmdmenu (state, $3, menu_entry, 0); + $$ = grub_script_create_cmdmenu (state, $3, def, 0); } ; From 19dd394f56733b6beecf560b82984c293522455b Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Fri, 30 Apr 2010 12:09:31 +0530 Subject: [PATCH 02/18] block argument support --- include/grub/script_sh.h | 6 ++++- script/execute.c | 1 + script/parser.y | 50 ++++++++++++++++++++++++++++++++++++++-- script/yylex.l | 2 +- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index 17b1c5a5a..e1bf6f22e 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -49,7 +49,8 @@ typedef enum GRUB_SCRIPT_ARG_TYPE_TEXT, GRUB_SCRIPT_ARG_TYPE_DQVAR, GRUB_SCRIPT_ARG_TYPE_DQSTR, - GRUB_SCRIPT_ARG_TYPE_SQSTR + GRUB_SCRIPT_ARG_TYPE_SQSTR, + GRUB_SCRIPT_ARG_TYPE_BLOCK } grub_script_arg_type_t; /* A part of an argument. */ @@ -59,6 +60,9 @@ struct grub_script_arg char *str; + /* Parsed block argument. */ + struct grub_script_cmd *block; + /* Next argument part. */ struct grub_script_arg *next; }; diff --git a/script/execute.c b/script/execute.c index 40f161267..31fce554c 100644 --- a/script/execute.c +++ b/script/execute.c @@ -152,6 +152,7 @@ grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *c } break; + case GRUB_SCRIPT_ARG_TYPE_BLOCK: case GRUB_SCRIPT_ARG_TYPE_TEXT: if (grub_strlen (arg->str) > 0) { diff --git a/script/parser.y b/script/parser.y index e5de35cf4..9d256a153 100644 --- a/script/parser.y +++ b/script/parser.y @@ -74,7 +74,7 @@ %token GRUB_PARSER_TOKEN_NAME "name" %token GRUB_PARSER_TOKEN_WORD "word" -%type word argument arguments0 arguments1 +%type word argument block parameters0 parameters1 arguments0 arguments1 %type script_init script %type grubcmd ifclause ifcmd forcmd whilecmd untilcmd @@ -147,6 +147,27 @@ argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); } | word { $$ = $1; } ; +block: "{" + { + grub_script_lexer_ref (state->lexerstate); + $$ = grub_script_lexer_record_start (state); + } + commands1 delimiters0 "}" + { + char *p; + struct grub_script_arg *arg; + + grub_script_lexer_deref (state->lexerstate); + if (p = grub_script_lexer_record_stop (state, $2)) + *grub_strrchr (p, '}') = '\0'; + + if (arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p)) + arg->block = $3; + + $$ = grub_script_add_arglist (state, 0, arg); + } +; + arguments0: /* Empty */ { $$ = 0; } | arguments1 { $$ = $1; } ; @@ -162,7 +183,32 @@ arguments1: argument arguments0 } ; -grubcmd: word arguments0 +parameters1: argument parameters0 + { + if ($1 && $2) + { + $1->next = $2; + $1->argcount += $2->argcount; + $2->argcount = 0; + } + $$ = $1; + } + | block parameters0 + { + if ($1 && $2) + { + $1->next = $2; + $1->argcount += $2->argcount; + $2->argcount = 0; + } + $$ = $1; + } +; +parameters0: /* Empty */ { $$ = 0; } + | parameters1 { $$ = $1; } +; + +grubcmd: word parameters0 { if ($1 && $2) { $1->next = $2; diff --git a/script/yylex.l b/script/yylex.l index 29aa5c2e3..585f818cb 100644 --- a/script/yylex.l +++ b/script/yylex.l @@ -114,7 +114,7 @@ typedef size_t yy_size_t; BLANK [ \t] COMMENT ^[ \t]*#.*$ -CHAR [^|&$;<> \t\n\'\"\\] +CHAR [^{}|&$;<> \t\n\'\"\\] DIGITS [[:digit:]]+ NAME [[:alpha:]_][[:alnum:][:digit:]_]* From 6c9aa3df16e5add2b88b96556b5e0d0ba8ce9475 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Wed, 9 Jun 2010 11:29:11 +0530 Subject: [PATCH 03/18] block params are grub_script with independent memory --- include/grub/script_sh.h | 2 +- script/parser.y | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index e1bf6f22e..9eccb9028 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -61,7 +61,7 @@ struct grub_script_arg char *str; /* Parsed block argument. */ - struct grub_script_cmd *block; + struct grub_script block; /* Next argument part. */ struct grub_script_arg *next; diff --git a/script/parser.y b/script/parser.y index 9d256a153..6669b783c 100644 --- a/script/parser.y +++ b/script/parser.y @@ -33,7 +33,10 @@ struct grub_script_arglist *arglist; struct grub_script_arg *arg; char *string; - unsigned offset; + struct { + unsigned offset; + struct grub_script_mem *memory; + }; } %token GRUB_PARSER_TOKEN_BAD @@ -147,24 +150,30 @@ argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); } | word { $$ = $1; } ; -block: "{" +block: "{" { grub_script_lexer_ref (state->lexerstate); $$ = grub_script_lexer_record_start (state); + $$ = grub_script_mem_record (state); } commands1 delimiters0 "}" { char *p; struct grub_script_arg *arg; + struct grub_script_mem *memory; - grub_script_lexer_deref (state->lexerstate); - if (p = grub_script_lexer_record_stop (state, $2)) + memory = grub_script_mem_record_stop (state, $2); + if ((p = grub_script_lexer_record_stop (state, $2))) *grub_strrchr (p, '}') = '\0'; if (arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p)) - arg->block = $3; + { + arg->block.cmd = $3; + arg->block.mem = memory; + } $$ = grub_script_add_arglist (state, 0, arg); + grub_script_lexer_deref (state->lexerstate); } ; From 28be0e94db5c92202a94fe204beb5d0ea0936771 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 10 Jun 2010 12:12:03 +0530 Subject: [PATCH 04/18] add example usage to hello command --- commands/acpi.c | 5 ++--- commands/echo.c | 4 ++-- commands/extcmd.c | 23 ++++++++++++++----- commands/hashsum.c | 6 ++--- commands/hdparm.c | 4 ++-- commands/help.c | 2 +- commands/hexdump.c | 4 ++-- commands/i386/cpuid.c | 2 +- commands/i386/pc/drivemap.c | 12 +++++----- commands/i386/pc/halt.c | 4 ++-- commands/iorw.c | 8 +++---- commands/keystatus.c | 4 ++-- commands/loadenv.c | 12 +++++----- commands/ls.c | 4 ++-- commands/lspci.c | 4 ++-- commands/memrw.c | 8 +++---- commands/probe.c | 4 ++-- commands/search_wrap.c | 4 ++-- commands/setpci.c | 16 ++++++------- commands/sleep.c | 4 ++-- disk/loopback.c | 4 ++-- hello/hello.c | 38 ++++++++++++++++++++++++++----- include/grub/command.h | 2 ++ include/grub/extcmd.h | 22 +++++++++++++++--- include/grub/script_sh.h | 3 +++ loader/i386/bsd.c | 20 ++++++++--------- loader/xnu.c | 6 ++--- script/argv.c | 45 +++++++++++++++++++++++++++++++++++-- script/execute.c | 25 ++++++++++++++++----- script/parser.y | 2 +- script/script.c | 8 ++++++- term/gfxterm.c | 7 +++--- term/serial.c | 4 ++-- tests/lib/functional_test.c | 2 +- 34 files changed, 220 insertions(+), 102 deletions(-) diff --git a/commands/acpi.c b/commands/acpi.c index 5bbfd008b..bdfdea073 100644 --- a/commands/acpi.c +++ b/commands/acpi.c @@ -456,10 +456,9 @@ free_tables (void) } static grub_err_t -grub_cmd_acpi (struct grub_extcmd *cmd, - int argc, char **args) +grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; struct grub_acpi_rsdp_v10 *rsdp; struct efiemu_acpi_table *cur, *t; grub_err_t err; diff --git a/commands/echo.c b/commands/echo.c index 4fea091cb..6bc00eae8 100644 --- a/commands/echo.c +++ b/commands/echo.c @@ -30,9 +30,9 @@ static const struct grub_arg_option options[] = }; static grub_err_t -grub_cmd_echo (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_echo (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; int newline = 1; int i; diff --git a/commands/extcmd.c b/commands/extcmd.c index 16796febf..76cbe4e26 100644 --- a/commands/extcmd.c +++ b/commands/extcmd.c @@ -21,15 +21,17 @@ #include #include #include +#include -static grub_err_t -grub_extcmd_dispatcher (struct grub_command *cmd, - int argc, char **args) +grub_err_t +grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, + struct grub_script **scripts) { int new_argc; char **new_args; struct grub_arg_option *parser; struct grub_arg_list *state; + struct grub_extcmd_context context; int maxargs = 0; grub_err_t ret; grub_extcmd_t ext; @@ -44,8 +46,11 @@ grub_extcmd_dispatcher (struct grub_command *cmd, if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc)) { - ext->state = state; - ret = (ext->func) (ext, new_argc, new_args); + context.extcmd = ext; + context.state = state; + context.script_params = scripts; + + ret = (ext->func) (&context, new_argc, new_args); grub_free (new_args); } else @@ -56,6 +61,12 @@ grub_extcmd_dispatcher (struct grub_command *cmd, return ret; } +static grub_err_t +grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args) +{ + return grub_extcmd_dispatcher (cmd, argc, args, 0); +} + grub_extcmd_t grub_register_extcmd (const char *name, grub_extcmd_func_t func, unsigned flags, const char *summary, @@ -69,7 +80,7 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func, if (! ext) return 0; - cmd = grub_register_command_prio (name, grub_extcmd_dispatcher, + cmd = grub_register_command_prio (name, grub_extcmd_dispatch, summary, description, 1); if (! cmd) { diff --git a/commands/hashsum.c b/commands/hashsum.c index d5f551dbb..df85015a6 100644 --- a/commands/hashsum.c +++ b/commands/hashsum.c @@ -165,10 +165,10 @@ check_list (const gcry_md_spec_t *hash, const char *hashfilename, } static grub_err_t -grub_cmd_hashsum (struct grub_extcmd *cmd, +grub_cmd_hashsum (struct grub_extcmd_context *ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; const char *hashname = NULL; const char *prefix = NULL; const gcry_md_spec_t *hash; @@ -177,7 +177,7 @@ grub_cmd_hashsum (struct grub_extcmd *cmd, unsigned unread = 0; for (i = 0; i < ARRAY_SIZE (aliases); i++) - if (grub_strcmp (cmd->cmd->name, aliases[i].name) == 0) + if (grub_strcmp (ctxt->extcmd->cmd->name, aliases[i].name) == 0) hashname = aliases[i].hashname; if (state[0].set) hashname = state[0].arg; diff --git a/commands/hdparm.c b/commands/hdparm.c index a3f8bbff0..54724077d 100644 --- a/commands/hdparm.c +++ b/commands/hdparm.c @@ -270,9 +270,9 @@ static int get_int_arg (const struct grub_arg_list *state) } static grub_err_t -grub_cmd_hdparm (grub_extcmd_t cmd, int argc, char **args) // state???? +grub_cmd_hdparm (grub_extcmd_context_t ctxt, int argc, char **args) // state???? { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; /* Check command line. */ if (argc != 1) diff --git a/commands/help.c b/commands/help.c index c2aad03b2..2b2eeaa84 100644 --- a/commands/help.c +++ b/commands/help.c @@ -27,7 +27,7 @@ #include static grub_err_t -grub_cmd_help (grub_extcmd_t ext __attribute__ ((unused)), int argc, +grub_cmd_help (grub_extcmd_context_t ctxt __attribute__ ((unused)), int argc, char **args) { int cnt = 0; diff --git a/commands/hexdump.c b/commands/hexdump.c index c1d4ecba9..6c518f649 100644 --- a/commands/hexdump.c +++ b/commands/hexdump.c @@ -34,9 +34,9 @@ static const struct grub_arg_option options[] = { }; static grub_err_t -grub_cmd_hexdump (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; char buf[GRUB_DISK_SECTOR_SIZE * 4]; grub_ssize_t size, length; grub_disk_addr_t skip; diff --git a/commands/i386/cpuid.c b/commands/i386/cpuid.c index 6eebf91a1..412b284d0 100644 --- a/commands/i386/cpuid.c +++ b/commands/i386/cpuid.c @@ -43,7 +43,7 @@ static const struct grub_arg_option options[] = unsigned char grub_cpuid_has_longmode = 0; static grub_err_t -grub_cmd_cpuid (grub_extcmd_t cmd __attribute__ ((unused)), +grub_cmd_cpuid (grub_extcmd_context_t ctxt __attribute__ ((unused)), int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { diff --git a/commands/i386/pc/drivemap.c b/commands/i386/pc/drivemap.c index 4afc43358..7ecfe7454 100644 --- a/commands/i386/pc/drivemap.c +++ b/commands/i386/pc/drivemap.c @@ -196,13 +196,13 @@ list_mappings (void) } static grub_err_t -grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args) +grub_cmd_drivemap (struct grub_extcmd_context *ctxt, int argc, char **args) { - if (cmd->state[OPTIDX_LIST].set) + if (ctxt->state[OPTIDX_LIST].set) { return list_mappings (); } - else if (cmd->state[OPTIDX_RESET].set) + else if (ctxt->state[OPTIDX_RESET].set) { /* Reset: just delete all mappings, freeing their memory. */ drivemap_node_t *curnode = map_head; @@ -216,7 +216,7 @@ grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args) map_head = 0; return GRUB_ERR_NONE; } - else if (!cmd->state[OPTIDX_SWAP].set && argc == 0) + else if (!ctxt->state[OPTIDX_SWAP].set && argc == 0) { /* No arguments */ return list_mappings (); @@ -248,11 +248,11 @@ grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args) } /* Set the mapping for the disk (overwrites any existing mapping). */ grub_dprintf ("drivemap", "%s %s (%02x) = %s (%02x)\n", - cmd->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping", + ctxt->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping", args[1], mapto, args[0], mapfrom); err = drivemap_set (mapto, mapfrom); /* If -s, perform the reverse mapping too (only if the first was OK). */ - if (cmd->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE) + if (ctxt->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE) err = drivemap_set (mapfrom, mapto); return err; } diff --git a/commands/i386/pc/halt.c b/commands/i386/pc/halt.c index 4c39612ae..10b298644 100644 --- a/commands/i386/pc/halt.c +++ b/commands/i386/pc/halt.c @@ -29,12 +29,12 @@ static const struct grub_arg_option options[] = }; static grub_err_t -grub_cmd_halt (grub_extcmd_t cmd, +grub_cmd_halt (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; int no_apm = 0; if (state[0].set) no_apm = 1; diff --git a/commands/iorw.c b/commands/iorw.c index 474c8712e..bd0183794 100644 --- a/commands/iorw.c +++ b/commands/iorw.c @@ -36,7 +36,7 @@ static const struct grub_arg_option options[] = static grub_err_t -grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv) +grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv) { grub_target_addr_t addr; grub_uint32_t value = 0; @@ -46,7 +46,7 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv) return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid number of arguments"); addr = grub_strtoul (argv[0], 0, 0); - switch (cmd->cmd->name[sizeof ("in") - 1]) + switch (ctxt->extcmd->cmd->name[sizeof ("in") - 1]) { case 'l': value = grub_inl (addr); @@ -61,10 +61,10 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv) break; } - if (cmd->state[0].set) + if (ctxt->state[0].set) { grub_snprintf (buf, sizeof (buf), "%x", value); - grub_env_set (cmd->state[0].arg, buf); + grub_env_set (ctxt->state[0].arg, buf); } else grub_printf ("0x%x\n", value); diff --git a/commands/keystatus.c b/commands/keystatus.c index 838792889..c83c00560 100644 --- a/commands/keystatus.c +++ b/commands/keystatus.c @@ -34,11 +34,11 @@ static const struct grub_arg_option options[] = #define grub_cur_term_input grub_term_get_current_input () static grub_err_t -grub_cmd_keystatus (grub_extcmd_t cmd, +grub_cmd_keystatus (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; int expect_mods = 0; int mods; diff --git a/commands/loadenv.c b/commands/loadenv.c index d763b2d5e..381810a92 100644 --- a/commands/loadenv.c +++ b/commands/loadenv.c @@ -111,11 +111,11 @@ read_envblk_file (grub_file_t file) } static grub_err_t -grub_cmd_load_env (grub_extcmd_t cmd, +grub_cmd_load_env (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; grub_file_t file; grub_envblk_t envblk; @@ -143,11 +143,11 @@ grub_cmd_load_env (grub_extcmd_t cmd, } static grub_err_t -grub_cmd_list_env (grub_extcmd_t cmd, +grub_cmd_list_env (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; grub_file_t file; grub_envblk_t envblk; @@ -280,9 +280,9 @@ write_blocklists (grub_envblk_t envblk, struct blocklist *blocklists, } static grub_err_t -grub_cmd_save_env (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_save_env (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; grub_file_t file; grub_envblk_t envblk; struct blocklist *head = 0; diff --git a/commands/ls.c b/commands/ls.c index eb1049617..6b2f63c19 100644 --- a/commands/ls.c +++ b/commands/ls.c @@ -248,9 +248,9 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human) } static grub_err_t -grub_cmd_ls (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; if (argc == 0) grub_ls_list_devices (state[0].set); diff --git a/commands/lspci.c b/commands/lspci.c index a69bb35ad..bc52e060a 100644 --- a/commands/lspci.c +++ b/commands/lspci.c @@ -211,11 +211,11 @@ grub_lspci_iter (grub_pci_device_t dev, grub_pci_id_t pciid) } static grub_err_t -grub_cmd_lspci (grub_extcmd_t cmd, +grub_cmd_lspci (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { - iospace = cmd->state[0].set; + iospace = ctxt->state[0].set; grub_pci_iterate (grub_lspci_iter); return GRUB_ERR_NONE; } diff --git a/commands/memrw.c b/commands/memrw.c index 6a4d43be2..d28f45ec1 100644 --- a/commands/memrw.c +++ b/commands/memrw.c @@ -35,7 +35,7 @@ static const struct grub_arg_option options[] = static grub_err_t -grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv) +grub_cmd_read (grub_extcmd_context_t ctxt, int argc, char **argv) { grub_target_addr_t addr; grub_uint32_t value = 0; @@ -45,7 +45,7 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv) return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid number of arguments"); addr = grub_strtoul (argv[0], 0, 0); - switch (cmd->cmd->name[sizeof ("read_") - 1]) + switch (ctxt->extcmd->cmd->name[sizeof ("read_") - 1]) { case 'd': value = *((volatile grub_uint32_t *) addr); @@ -60,10 +60,10 @@ grub_cmd_read (grub_extcmd_t cmd, int argc, char **argv) break; } - if (cmd->state[0].set) + if (ctxt->state[0].set) { grub_snprintf (buf, sizeof (buf), "%x", value); - grub_env_set (cmd->state[0].arg, buf); + grub_env_set (ctxt->state[0].arg, buf); } else grub_printf ("0x%x\n", value); diff --git a/commands/probe.c b/commands/probe.c index c2cc599e9..f3941e029 100644 --- a/commands/probe.c +++ b/commands/probe.c @@ -45,9 +45,9 @@ static const struct grub_arg_option options[] = }; static grub_err_t -grub_cmd_probe (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; grub_device_t dev; grub_fs_t fs; char *ptr; diff --git a/commands/search_wrap.c b/commands/search_wrap.c index 2891d85d7..fff3fb47a 100644 --- a/commands/search_wrap.c +++ b/commands/search_wrap.c @@ -50,9 +50,9 @@ enum options }; static grub_err_t -grub_cmd_search (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; const char *var = 0; if (argc == 0) diff --git a/commands/setpci.c b/commands/setpci.c index fbc7c214e..89a0085d8 100644 --- a/commands/setpci.c +++ b/commands/setpci.c @@ -155,7 +155,7 @@ grub_setpci_iter (grub_pci_device_t dev, grub_pci_id_t pciid) } static grub_err_t -grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv) +grub_cmd_setpci (grub_extcmd_context_t ctxt, int argc, char **argv) { const char *ptr; unsigned i; @@ -163,14 +163,14 @@ grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv) pciid_check_value = 0; pciid_check_mask = 0; - if (cmd->state[0].set) + if (ctxt->state[0].set) { - ptr = cmd->state[0].arg; + ptr = ctxt->state[0].arg; pciid_check_value |= (grub_strtoul (ptr, (char **) &ptr, 16) & 0xffff); if (grub_errno == GRUB_ERR_BAD_NUMBER) { grub_errno = GRUB_ERR_NONE; - ptr = cmd->state[0].arg; + ptr = ctxt->state[0].arg; } else pciid_check_mask |= 0xffff; @@ -191,11 +191,11 @@ grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv) check_bus = check_device = check_function = 0; - if (cmd->state[1].set) + if (ctxt->state[1].set) { const char *optr; - ptr = cmd->state[1].arg; + ptr = ctxt->state[1].arg; optr = ptr; bus = grub_strtoul (ptr, (char **) &ptr, 16); if (grub_errno == GRUB_ERR_BAD_NUMBER) @@ -229,8 +229,8 @@ grub_cmd_setpci (grub_extcmd_t cmd, int argc, char **argv) } } - if (cmd->state[2].set) - varname = cmd->state[2].arg; + if (ctxt->state[2].set) + varname = ctxt->state[2].arg; else varname = NULL; diff --git a/commands/sleep.c b/commands/sleep.c index ead279506..ee0875cf7 100644 --- a/commands/sleep.c +++ b/commands/sleep.c @@ -60,9 +60,9 @@ grub_interruptible_millisleep (grub_uint32_t ms) } static grub_err_t -grub_cmd_sleep (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_sleep (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; int n; if (argc != 1) diff --git a/disk/loopback.c b/disk/loopback.c index 1b525e05f..52929e765 100644 --- a/disk/loopback.c +++ b/disk/loopback.c @@ -71,9 +71,9 @@ delete_loopback (const char *name) /* The command to add and remove loopback devices. */ static grub_err_t -grub_cmd_loopback (grub_extcmd_t cmd, int argc, char **args) +grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args) { - struct grub_arg_list *state = state = cmd->state; + struct grub_arg_list *state = ctxt->state; grub_file_t file; struct grub_loopback *newdev; diff --git a/hello/hello.c b/hello/hello.c index eff07d941..16c3bb5ce 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -26,12 +26,36 @@ #include #include +static struct grub_script *script; + static grub_err_t -grub_cmd_hello (struct grub_extcmd *cmd __attribute__ ((unused)), - int argc __attribute__ ((unused)), - char **args __attribute__ ((unused))) +grub_cmd_hello (grub_extcmd_context_t ctxt, + int argc, char **args __attribute__ ((unused))) { - grub_printf ("Hello World\n"); + if (argc == 0 && script == 0) + grub_printf ("Hello World\n"); + + else if (argc == 0) + grub_script_execute (script); + + else + { + if (! ctxt->script_params || ! ctxt->script_params[0]) + return 1; + + if (script) + grub_script_free (script); + + script = grub_malloc (sizeof (*script)); + if (! script) + return 1; + + script->cmd = ctxt->script_params[0]->cmd; + script->mem = ctxt->script_params[0]->mem; + ctxt->script_params[0]->cmd = 0; + ctxt->script_params[0]->mem = 0; + } + return 0; } @@ -39,11 +63,15 @@ static grub_extcmd_t cmd; GRUB_MOD_INIT(hello) { - cmd = grub_register_extcmd ("hello", grub_cmd_hello, GRUB_COMMAND_FLAG_BOTH, + cmd = grub_register_extcmd ("hello", grub_cmd_hello, + GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS, 0, N_("Say \"Hello World\"."), 0); } GRUB_MOD_FINI(hello) { + if (script) + grub_script_free (script); + grub_unregister_extcmd (cmd); } diff --git a/include/grub/command.h b/include/grub/command.h index 6e9942b60..c2523f288 100644 --- a/include/grub/command.h +++ b/include/grub/command.h @@ -37,6 +37,8 @@ #define GRUB_COMMAND_FLAG_EXTCMD 0x10 /* This is an dynamic command. */ #define GRUB_COMMAND_FLAG_DYNCMD 0x20 +/* This command accepts block arguments. */ +#define GRUB_COMMAND_FLAG_BLOCKS 0x40 struct grub_command; diff --git a/include/grub/extcmd.h b/include/grub/extcmd.h index 03eaba8f8..54f0958fe 100644 --- a/include/grub/extcmd.h +++ b/include/grub/extcmd.h @@ -21,10 +21,12 @@ #include #include +#include struct grub_extcmd; +struct grub_extcmd_context; -typedef grub_err_t (*grub_extcmd_func_t) (struct grub_extcmd *cmd, +typedef grub_err_t (*grub_extcmd_func_t) (struct grub_extcmd_context *ctxt, int argc, char **args); /* The argcmd description. */ @@ -38,11 +40,21 @@ struct grub_extcmd const struct grub_arg_option *options; void *data; - - struct grub_arg_list *state; }; typedef struct grub_extcmd *grub_extcmd_t; +/* Command context for each instance of execution. */ +struct grub_extcmd_context +{ + struct grub_extcmd *extcmd; + + struct grub_arg_list *state; + + /* Script parameters, if any. */ + struct grub_script **script_params; +}; +typedef struct grub_extcmd_context *grub_extcmd_context_t; + grub_extcmd_t grub_register_extcmd (const char *name, grub_extcmd_func_t func, unsigned flags, @@ -52,4 +64,8 @@ grub_extcmd_t grub_register_extcmd (const char *name, void grub_unregister_extcmd (grub_extcmd_t cmd); +grub_err_t +grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, + struct grub_script **scripts); + #endif /* ! GRUB_EXTCMD_HEADER */ diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index 87fb26f93..a17dcca70 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -72,6 +72,7 @@ struct grub_script_argv { unsigned argc; char **args; + struct grub_script **scripts; }; /* A complete argument. It consists of a list of one or more `struct @@ -230,6 +231,8 @@ void grub_script_argv_free (struct grub_script_argv *argv); int grub_script_argv_next (struct grub_script_argv *argv); int grub_script_argv_append (struct grub_script_argv *argv, const char *s); int grub_script_argv_split_append (struct grub_script_argv *argv, char *s); +int grub_script_argv_script_append (struct grub_script_argv *argv, + struct grub_script *s); struct grub_script_arglist * grub_script_create_arglist (struct grub_parser_param *state); diff --git a/loader/i386/bsd.c b/loader/i386/bsd.c index 3c7fe2fee..c92712562 100644 --- a/loader/i386/bsd.c +++ b/loader/i386/bsd.c @@ -946,10 +946,10 @@ grub_bsd_parse_flags (const struct grub_arg_list *state, } static grub_err_t -grub_cmd_freebsd (grub_extcmd_t cmd, int argc, char *argv[]) +grub_cmd_freebsd (grub_extcmd_context_t ctxt, int argc, char *argv[]) { kernel_type = KERNEL_TYPE_FREEBSD; - bootflags = grub_bsd_parse_flags (cmd->state, freebsd_flags); + bootflags = grub_bsd_parse_flags (ctxt->state, freebsd_flags); if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { @@ -1009,16 +1009,16 @@ grub_cmd_freebsd (grub_extcmd_t cmd, int argc, char *argv[]) } static grub_err_t -grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[]) +grub_cmd_openbsd (grub_extcmd_context_t ctxt, int argc, char *argv[]) { grub_uint32_t bootdev; kernel_type = KERNEL_TYPE_OPENBSD; - bootflags = grub_bsd_parse_flags (cmd->state, openbsd_flags); + bootflags = grub_bsd_parse_flags (ctxt->state, openbsd_flags); - if (cmd->state[OPENBSD_ROOT_ARG].set) + if (ctxt->state[OPENBSD_ROOT_ARG].set) { - const char *arg = cmd->state[OPENBSD_ROOT_ARG].arg; + const char *arg = ctxt->state[OPENBSD_ROOT_ARG].arg; int unit, part; if (*(arg++) != 'w' || *(arg++) != 'd') return grub_error (GRUB_ERR_BAD_ARGUMENT, @@ -1049,16 +1049,16 @@ grub_cmd_openbsd (grub_extcmd_t cmd, int argc, char *argv[]) } static grub_err_t -grub_cmd_netbsd (grub_extcmd_t cmd, int argc, char *argv[]) +grub_cmd_netbsd (grub_extcmd_context_t ctxt, int argc, char *argv[]) { kernel_type = KERNEL_TYPE_NETBSD; - bootflags = grub_bsd_parse_flags (cmd->state, netbsd_flags); + bootflags = grub_bsd_parse_flags (ctxt->state, netbsd_flags); if (grub_bsd_load (argc, argv) == GRUB_ERR_NONE) { grub_loader_set (grub_netbsd_boot, grub_bsd_unload, 1); - if (cmd->state[NETBSD_ROOT_ARG].set) - netbsd_root = grub_strdup (cmd->state[NETBSD_ROOT_ARG].arg); + if (ctxt->state[NETBSD_ROOT_ARG].set) + netbsd_root = grub_strdup (ctxt->state[NETBSD_ROOT_ARG].arg); } return grub_errno; diff --git a/loader/xnu.c b/loader/xnu.c index 8f1d0c641..5c7bd50be 100644 --- a/loader/xnu.c +++ b/loader/xnu.c @@ -1368,15 +1368,15 @@ static const struct grub_arg_option xnu_splash_cmd_options[] = }; static grub_err_t -grub_cmd_xnu_splash (grub_extcmd_t cmd, +grub_cmd_xnu_splash (grub_extcmd_context_t ctxt, int argc, char *args[]) { grub_err_t err; if (argc != 1) return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); - if (cmd->state[XNU_SPLASH_CMD_ARGINDEX_MODE].set && - grub_strcmp (cmd->state[XNU_SPLASH_CMD_ARGINDEX_MODE].arg, + if (ctxt->state[XNU_SPLASH_CMD_ARGINDEX_MODE].set && + grub_strcmp (ctxt->state[XNU_SPLASH_CMD_ARGINDEX_MODE].arg, "stretch") == 0) grub_xnu_bitmap_mode = GRUB_XNU_BITMAP_STRETCH; else diff --git a/script/argv.c b/script/argv.c index 69322779d..f962556d8 100644 --- a/script/argv.c +++ b/script/argv.c @@ -52,8 +52,17 @@ grub_script_argv_free (struct grub_script_argv *argv) grub_free (argv->args); } + if (argv->scripts) + { + for (i = 0; i < argv->argc; i++) + grub_script_free (argv->scripts[i]); + + grub_free (argv->scripts); + } + argv->argc = 0; argv->args = 0; + argv->scripts = 0; } /* Prepare for next argc. */ @@ -61,20 +70,33 @@ int grub_script_argv_next (struct grub_script_argv *argv) { char **p = argv->args; + struct grub_script **q = argv->scripts; - if (argv->args && argv->args[argv->argc - 1] == 0) + if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0) return 0; p = grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *))); if (! p) return 1; + q = grub_realloc (q, round_up_exp ((argv->argc + 2) * sizeof (struct grub_script *))); + if (! q) + { + grub_free (p); + return 1; + } + argv->argc++; argv->args = p; + argv->scripts = q; if (argv->argc == 1) - argv->args[0] = 0; + { + argv->args[0] = 0; + argv->scripts[0] = 0; + } argv->args[argv->argc] = 0; + argv->scripts[argv->argc] = 0; return 0; } @@ -100,6 +122,25 @@ grub_script_argv_append (struct grub_script_argv *argv, const char *s) return 0; } +/* Append grub_script `s' as the last argument. */ +int +grub_script_argv_script_append (struct grub_script_argv *argv, + struct grub_script *script) +{ + struct grub_script *s; + + s = grub_malloc (sizeof (*s)); + if (! s) + return 1; + + if (argv->scripts[argv->argc - 1]) + grub_script_free (argv->scripts[argv->argc - 1]); + + *s = *script; + argv->scripts[argv->argc - 1] = s; + return 0; +} + /* Split `s' and append words as multiple arguments. */ int grub_script_argv_split_append (struct grub_script_argv *argv, char *s) diff --git a/script/execute.c b/script/execute.c index 691b3c893..0ac42cf4a 100644 --- a/script/execute.c +++ b/script/execute.c @@ -25,6 +25,7 @@ #include #include #include +#include /* 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. */ @@ -51,7 +52,7 @@ grub_env_special (const char *name) static char ** grub_script_env_get (const char *name, grub_script_arg_type_t type) { - struct grub_script_argv result = { 0, 0 }; + struct grub_script_argv result = { 0, 0, 0 }; if (grub_script_argv_next (&result)) goto fail; @@ -169,7 +170,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, int i; char **values = 0; struct grub_script_arg *arg = 0; - struct grub_script_argv result = { 0, 0 }; + struct grub_script_argv result = { 0, 0, 0 }; for (; arglist && arglist->arg; arglist = arglist->next) { @@ -196,6 +197,11 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, break; case GRUB_SCRIPT_ARG_TYPE_BLOCK: + if (grub_script_argv_append (&result, arg->str) || + grub_script_argv_script_append (&result, &arg->block)) + goto fail; + break; + case GRUB_SCRIPT_ARG_TYPE_TEXT: if (grub_strlen (arg->str) && grub_script_argv_append (&result, arg->str)) @@ -270,7 +276,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) grub_script_function_t func = 0; char errnobuf[18]; char *cmdname; - struct grub_script_argv argv = { 0, 0 }; + struct grub_script_argv argv = { 0, 0, 0 }; /* Lookup the command. */ if (grub_script_arglist_to_argv (cmdline->arglist, &argv)) @@ -314,7 +320,14 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) /* Execute the GRUB command or function. */ if (grubcmd) - ret = (grubcmd->func) (grubcmd, argv.argc - 1, argv.args + 1); + { + if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) && + (grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD)) + ret = grub_extcmd_dispatcher (grubcmd, argv.argc - 1, argv.args + 1, + argv.scripts + 1); + else + ret = (grubcmd->func) (grubcmd, argv.argc - 1, argv.args + 1); + } else ret = grub_script_function_call (func, argv.argc - 1, argv.args + 1); @@ -374,7 +387,7 @@ grub_script_execute_cmdfor (struct grub_script_cmd *cmd) { unsigned i; grub_err_t result; - struct grub_script_argv argv = { 0, 0 }; + struct grub_script_argv argv = { 0, 0, 0 }; struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd; if (grub_script_arglist_to_argv (cmdfor->words, &argv)) @@ -416,7 +429,7 @@ grub_err_t grub_script_execute_menuentry (struct grub_script_cmd *cmd) { struct grub_script_cmd_menuentry *cmd_menuentry; - struct grub_script_argv argv = { 0, 0 }; + struct grub_script_argv argv = { 0, 0, 0 }; cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd; diff --git a/script/parser.y b/script/parser.y index 653e2bc84..d774ca918 100644 --- a/script/parser.y +++ b/script/parser.y @@ -164,7 +164,7 @@ block: "{" if ((p = grub_script_lexer_record_stop (state, $2))) *grub_strrchr (p, '}') = '\0'; - if (arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p)) + if ((arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p))) { arg->block.cmd = $3; arg->block.mem = memory; diff --git a/script/script.c b/script/script.c index 9cee40dcb..9017a08e8 100644 --- a/script/script.c +++ b/script/script.c @@ -96,7 +96,10 @@ grub_script_free (struct grub_script *script) { if (!script) return; - grub_script_mem_free (script->mem); + + if (script->mem) + grub_script_mem_free (script->mem); + grub_free (script); } @@ -119,6 +122,9 @@ grub_script_arg_add (struct grub_parser_param *state, return arg; argpart->type = type; + argpart->block.mem = 0; + argpart->block.cmd = 0; + len = grub_strlen (str) + 1; argpart->str = grub_script_malloc (state, len); if (!argpart->str) diff --git a/term/gfxterm.c b/term/gfxterm.c index ecfe4ff3b..ffded5e6b 100644 --- a/term/gfxterm.c +++ b/term/gfxterm.c @@ -1091,11 +1091,10 @@ static const struct grub_arg_option background_image_cmd_options[] = }; static grub_err_t -grub_gfxterm_background_image_cmd (grub_extcmd_t cmd __attribute__ ((unused)), - int argc, - char **args) +grub_gfxterm_background_image_cmd (grub_extcmd_context_t ctxt, + int argc, char **args) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; /* Check that we have video adapter active. */ if (grub_video_get_info(NULL) != GRUB_ERR_NONE) diff --git a/term/serial.c b/term/serial.c index 2347bb3ee..168e40bad 100644 --- a/term/serial.c +++ b/term/serial.c @@ -497,11 +497,11 @@ static struct grub_term_output grub_serial_term_output = static grub_err_t -grub_cmd_serial (grub_extcmd_t cmd, +grub_cmd_serial (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { - struct grub_arg_list *state = cmd->state; + struct grub_arg_list *state = ctxt->state; struct serial_port backup_settings = serial_settings; grub_err_t hwiniterr; diff --git a/tests/lib/functional_test.c b/tests/lib/functional_test.c index 8ff08cf8a..8be6c8e89 100644 --- a/tests/lib/functional_test.c +++ b/tests/lib/functional_test.c @@ -22,7 +22,7 @@ #include static grub_err_t -grub_functional_test (struct grub_extcmd *cmd __attribute__ ((unused)), +grub_functional_test (grub_extcmd_context_t ctxt __attribute__ ((unused)), int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { From ead4142900803233bc8cc34eac9fe98ab30833ea Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 10 Jun 2010 12:26:07 +0530 Subject: [PATCH 05/18] fix hello help msg --- hello/hello.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hello/hello.c b/hello/hello.c index 16c3bb5ce..992c99f71 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -65,7 +65,7 @@ GRUB_MOD_INIT(hello) { cmd = grub_register_extcmd ("hello", grub_cmd_hello, GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS, - 0, N_("Say \"Hello World\"."), 0); + N_("[BLOCK]"), N_("Say \"Hello World\"."), 0); } GRUB_MOD_FINI(hello) From a992a71ed8df5188e9cdb3d2f5f502c2cc2e6417 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 10 Jun 2010 20:49:57 +0530 Subject: [PATCH 06/18] fix memory issues when used inside loops --- hello/hello.c | 14 ++++---------- include/grub/script_sh.h | 21 ++++++++++++++++++++- script/argv.c | 14 ++++---------- script/execute.c | 2 +- script/parser.y | 14 +++++++------- script/script.c | 17 ++++++----------- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/hello/hello.c b/hello/hello.c index 992c99f71..118966291 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -44,16 +44,9 @@ grub_cmd_hello (grub_extcmd_context_t ctxt, return 1; if (script) - grub_script_free (script); + grub_script_put (script); - script = grub_malloc (sizeof (*script)); - if (! script) - return 1; - - script->cmd = ctxt->script_params[0]->cmd; - script->mem = ctxt->script_params[0]->mem; - ctxt->script_params[0]->cmd = 0; - ctxt->script_params[0]->mem = 0; + script = grub_script_get (ctxt->script_params[0]); } return 0; @@ -71,7 +64,8 @@ GRUB_MOD_INIT(hello) GRUB_MOD_FINI(hello) { if (script) - grub_script_free (script); + grub_script_put (script); + script = 0; grub_unregister_extcmd (cmd); } diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index a17dcca70..2ada5d474 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -39,6 +39,7 @@ struct grub_script_cmd struct grub_script { + unsigned refcnt; struct grub_script_mem *mem; struct grub_script_cmd *cmd; }; @@ -61,7 +62,7 @@ struct grub_script_arg char *str; /* Parsed block argument. */ - struct grub_script block; + struct grub_script *block; /* Next argument part. */ struct grub_script_arg *next; @@ -227,6 +228,8 @@ struct grub_parser_param struct grub_lexer_param *lexerstate; }; +void grub_script_mem_free (struct grub_script_mem *mem); + void grub_script_argv_free (struct grub_script_argv *argv); int grub_script_argv_next (struct grub_script_argv *argv); int grub_script_argv_append (struct grub_script_argv *argv, const char *s); @@ -354,4 +357,20 @@ grub_err_t grub_script_function_call (grub_script_function_t func, char ** grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count); +static inline struct grub_script * +grub_script_get (struct grub_script *script) +{ + script->refcnt++; + return script; +} + +static inline void +grub_script_put (struct grub_script *script) +{ + if (script->refcnt == 0) + grub_script_free (script); + else + script->refcnt--; +} + #endif /* ! GRUB_NORMAL_PARSER_HEADER */ diff --git a/script/argv.c b/script/argv.c index f962556d8..8dd563345 100644 --- a/script/argv.c +++ b/script/argv.c @@ -55,7 +55,8 @@ grub_script_argv_free (struct grub_script_argv *argv) if (argv->scripts) { for (i = 0; i < argv->argc; i++) - grub_script_free (argv->scripts[i]); + if (argv->scripts[i]) + grub_script_put (argv->scripts[i]); grub_free (argv->scripts); } @@ -127,17 +128,10 @@ int grub_script_argv_script_append (struct grub_script_argv *argv, struct grub_script *script) { - struct grub_script *s; - - s = grub_malloc (sizeof (*s)); - if (! s) - return 1; - if (argv->scripts[argv->argc - 1]) - grub_script_free (argv->scripts[argv->argc - 1]); + grub_script_put (argv->scripts[argv->argc - 1]); - *s = *script; - argv->scripts[argv->argc - 1] = s; + argv->scripts[argv->argc - 1] = grub_script_get (script); return 0; } diff --git a/script/execute.c b/script/execute.c index 0ac42cf4a..c6a6c2484 100644 --- a/script/execute.c +++ b/script/execute.c @@ -198,7 +198,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, case GRUB_SCRIPT_ARG_TYPE_BLOCK: if (grub_script_argv_append (&result, arg->str) || - grub_script_argv_script_append (&result, &arg->block)) + grub_script_argv_script_append (&result, arg->block)) goto fail; break; diff --git a/script/parser.y b/script/parser.y index d774ca918..1202fef29 100644 --- a/script/parser.y +++ b/script/parser.y @@ -164,11 +164,9 @@ block: "{" if ((p = grub_script_lexer_record_stop (state, $2))) *grub_strrchr (p, '}') = '\0'; - if ((arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p))) - { - arg->block.cmd = $3; - arg->block.mem = memory; - } + arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p); + if (! arg || ! (arg->block = grub_script_create ($3, memory))) + grub_script_mem_free (memory); $$ = grub_script_add_arglist (state, 0, arg); grub_script_lexer_deref (state->lexerstate); @@ -256,8 +254,10 @@ function: "function" "name" state->func_mem = grub_script_mem_record_stop (state, state->func_mem); script = grub_script_create ($6, state->func_mem); - if (script) - grub_script_function_create ($2, script); + if (! script) + grub_script_mem_free (state->func_mem); + else + grub_script_function_create ($2, script); grub_script_lexer_deref (state->lexerstate); } diff --git a/script/script.c b/script/script.c index 9017a08e8..b7cbdff1d 100644 --- a/script/script.c +++ b/script/script.c @@ -54,7 +54,7 @@ grub_script_malloc (struct grub_parser_param *state, grub_size_t size) } /* Free all memory described by MEM. */ -static void +void grub_script_mem_free (struct grub_script_mem *mem) { struct grub_script_mem *memfree; @@ -122,8 +122,7 @@ grub_script_arg_add (struct grub_parser_param *state, return arg; argpart->type = type; - argpart->block.mem = 0; - argpart->block.cmd = 0; + argpart->block = 0; len = grub_strlen (str) + 1; argpart->str = grub_script_malloc (state, len); @@ -341,16 +340,12 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem) struct grub_script *parsed; parsed = grub_malloc (sizeof (*parsed)); - if (!parsed) - { - grub_script_mem_free (mem); - grub_free (cmd); - - return 0; - } + if (! parsed) + return 0; parsed->mem = mem; parsed->cmd = cmd; + parsed->refcnt = 0; return parsed; } @@ -365,7 +360,7 @@ grub_script_parse (char *script, grub_reader_getline_t getline) struct grub_lexer_param *lexstate; struct grub_parser_param *parsestate; - parsed = grub_malloc (sizeof (*parsed)); + parsed = grub_script_create (0, 0); if (!parsed) return 0; From f6e59c46e1a32e38e0dc73e417db3cafab54668a Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Sun, 18 Jul 2010 20:46:21 +0530 Subject: [PATCH 07/18] update dynamic cmd dispatch with scripts support --- commands/extcmd.c | 21 ++++++++++++++++----- include/grub/extcmd.h | 8 ++++++++ normal/dyncmd.c | 36 ++++++++++++++++++++++++++---------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/commands/extcmd.c b/commands/extcmd.c index 76cbe4e26..173dea193 100644 --- a/commands/extcmd.c +++ b/commands/extcmd.c @@ -68,10 +68,11 @@ grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args) } grub_extcmd_t -grub_register_extcmd (const char *name, grub_extcmd_func_t func, - unsigned flags, const char *summary, - const char *description, - const struct grub_arg_option *parser) +grub_register_extcmd_prio (const char *name, grub_extcmd_func_t func, + unsigned flags, const char *summary, + const char *description, + const struct grub_arg_option *parser, + int prio) { grub_extcmd_t ext; grub_command_t cmd; @@ -81,7 +82,7 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func, return 0; cmd = grub_register_command_prio (name, grub_extcmd_dispatch, - summary, description, 1); + summary, description, prio); if (! cmd) { grub_free (ext); @@ -99,6 +100,16 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func, return ext; } +grub_extcmd_t +grub_register_extcmd (const char *name, grub_extcmd_func_t func, + unsigned flags, const char *summary, + const char *description, + const struct grub_arg_option *parser) +{ + return grub_register_extcmd_prio (name, func, flags, + summary, description, parser, 1); +} + void grub_unregister_extcmd (grub_extcmd_t ext) { diff --git a/include/grub/extcmd.h b/include/grub/extcmd.h index 54f0958fe..a5e4d18fe 100644 --- a/include/grub/extcmd.h +++ b/include/grub/extcmd.h @@ -62,6 +62,14 @@ grub_extcmd_t grub_register_extcmd (const char *name, const char *description, const struct grub_arg_option *parser); +grub_extcmd_t grub_register_extcmd_prio (const char *name, + grub_extcmd_func_t func, + unsigned flags, + const char *summary, + const char *description, + const struct grub_arg_option *parser, + int prio); + void grub_unregister_extcmd (grub_extcmd_t cmd); grub_err_t diff --git a/normal/dyncmd.c b/normal/dyncmd.c index a3cafa514..172baa44e 100644 --- a/normal/dyncmd.c +++ b/normal/dyncmd.c @@ -23,16 +23,21 @@ #include #include #include +#include +#include #include static grub_err_t -grub_dyncmd_dispatcher (struct grub_command *cmd, +grub_dyncmd_dispatcher (struct grub_extcmd_context *ctxt, int argc, char **args) { - char *modname = cmd->data; + char *modname; grub_dl_t mod; grub_err_t ret; + grub_extcmd_t extcmd = ctxt->extcmd; + grub_command_t cmd = extcmd->cmd; + modname = extcmd->data; mod = grub_dl_load (modname); if (mod) { @@ -42,11 +47,18 @@ grub_dyncmd_dispatcher (struct grub_command *cmd, grub_dl_ref (mod); name = (char *) cmd->name; - grub_unregister_command (cmd); + grub_unregister_extcmd (extcmd); cmd = grub_command_find (name); if (cmd) - ret = (cmd->func) (cmd, argc, args); + { + if (cmd->flags & GRUB_COMMAND_FLAG_BLOCKS && + cmd->flags & GRUB_COMMAND_FLAG_EXTCMD) + ret = grub_extcmd_dispatcher (cmd, argc, args, + ctxt->script_params); + else + ret = (cmd->func) (cmd, argc, args); + } else ret = grub_errno; @@ -81,13 +93,14 @@ read_command_list (const char *prefix) for (ptr = grub_command_list; ptr; ptr = next) { next = ptr->next; - if (ptr->func == grub_dyncmd_dispatcher) + if (ptr->flags & GRUB_COMMAND_FLAG_DYNCMD) { if (last) last->next = ptr->next; else grub_command_list = ptr->next; grub_free (ptr); + grub_free (ptr->data); /* extcmd struct */ } else last = ptr; @@ -96,7 +109,7 @@ read_command_list (const char *prefix) for (;; grub_free (buf)) { char *p, *name, *modname; - grub_command_t cmd; + grub_extcmd_t cmd; int prio = 0; buf = grub_file_getline (file); @@ -139,16 +152,19 @@ read_command_list (const char *prefix) continue; } - cmd = grub_register_command_prio (name, - grub_dyncmd_dispatcher, - 0, N_("not loaded"), prio); + cmd = grub_register_extcmd_prio (name, + grub_dyncmd_dispatcher, + GRUB_COMMAND_FLAG_BLOCKS + | GRUB_COMMAND_FLAG_EXTCMD + | GRUB_COMMAND_FLAG_DYNCMD, + 0, N_("not loaded"), 0, + prio); if (! cmd) { grub_free (name); grub_free (modname); continue; } - cmd->flags |= GRUB_COMMAND_FLAG_DYNCMD; cmd->data = modname; /* Update the active flag. */ From 93889c47179b98729f7f501901d4e670ab7d5d2a Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Sun, 18 Jul 2010 21:00:58 +0530 Subject: [PATCH 08/18] undo changes to hello command --- hello/hello.c | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/hello/hello.c b/hello/hello.c index 118966291..183ee7798 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -26,29 +26,12 @@ #include #include -static struct grub_script *script; - static grub_err_t -grub_cmd_hello (grub_extcmd_context_t ctxt, - int argc, char **args __attribute__ ((unused))) +grub_cmd_hello (grub_extcmd_context_t ctxt __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) { - if (argc == 0 && script == 0) - grub_printf ("Hello World\n"); - - else if (argc == 0) - grub_script_execute (script); - - else - { - if (! ctxt->script_params || ! ctxt->script_params[0]) - return 1; - - if (script) - grub_script_put (script); - - script = grub_script_get (ctxt->script_params[0]); - } - + grub_printf ("Hello World\n"); return 0; } @@ -56,16 +39,11 @@ static grub_extcmd_t cmd; GRUB_MOD_INIT(hello) { - cmd = grub_register_extcmd ("hello", grub_cmd_hello, - GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS, - N_("[BLOCK]"), N_("Say \"Hello World\"."), 0); + cmd = grub_register_extcmd ("hello", grub_cmd_hello, GRUB_COMMAND_FLAG_BOTH, + 0, N_("Say \"Hello World\"."), 0); } GRUB_MOD_FINI(hello) { - if (script) - grub_script_put (script); - - script = 0; grub_unregister_extcmd (cmd); } From 9b65d8c4f50e6c9a244895837a2843f2e926af6b Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Mon, 19 Jul 2010 00:44:21 +0530 Subject: [PATCH 09/18] fix automatic option parsing for dynamic commands --- commands/extcmd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/commands/extcmd.c b/commands/extcmd.c index 173dea193..349e9bfc7 100644 --- a/commands/extcmd.c +++ b/commands/extcmd.c @@ -34,9 +34,20 @@ grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, struct grub_extcmd_context context; int maxargs = 0; grub_err_t ret; - grub_extcmd_t ext; + grub_extcmd_t ext = cmd->data; + + context.state = 0; + context.extcmd = ext; + context.script_params = scripts; + + /* Dynamic commands should not perform option parsing before + corresponding module gets loaded. */ + if (cmd->flags & GRUB_COMMAND_FLAG_DYNCMD) + { + ret = (ext->func) (&context, argc, args); + return ret; + } - ext = cmd->data; parser = (struct grub_arg_option *) ext->options; while (parser && (parser++)->doc) maxargs++; @@ -46,10 +57,7 @@ grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc)) { - context.extcmd = ext; context.state = state; - context.script_params = scripts; - ret = (ext->func) (&context, new_argc, new_args); grub_free (new_args); } From 15ee6f9dc7744859d494ca69a6c9738ed7721138 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Tue, 20 Jul 2010 00:10:44 +0530 Subject: [PATCH 10/18] add {} around block-arg that is passed through argv --- script/execute.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/script/execute.c b/script/execute.c index c6a6c2484..1e9cd00b7 100644 --- a/script/execute.c +++ b/script/execute.c @@ -197,7 +197,9 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, break; case GRUB_SCRIPT_ARG_TYPE_BLOCK: - if (grub_script_argv_append (&result, arg->str) || + if (grub_script_argv_append (&result, "{") || + grub_script_argv_append (&result, arg->str) || + grub_script_argv_append (&result, "}") || grub_script_argv_script_append (&result, arg->block)) goto fail; break; From 9ebedc24f2e7f3e5d63b18e9db226bdc15445ec5 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Wed, 21 Jul 2010 03:47:30 +0530 Subject: [PATCH 11/18] restrict to only one block-arg (as last) param per command --- commands/extcmd.c | 4 ++-- include/grub/extcmd.h | 6 +++--- include/grub/script_sh.h | 6 ++---- normal/dyncmd.c | 3 +-- script/argv.c | 38 ++------------------------------------ script/execute.c | 6 +++--- script/parser.y | 37 +++++++++++++++++-------------------- script/script.c | 2 +- 8 files changed, 31 insertions(+), 71 deletions(-) diff --git a/commands/extcmd.c b/commands/extcmd.c index 349e9bfc7..cc0362e14 100644 --- a/commands/extcmd.c +++ b/commands/extcmd.c @@ -25,7 +25,7 @@ grub_err_t grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, - struct grub_script **scripts) + struct grub_script *script) { int new_argc; char **new_args; @@ -38,7 +38,7 @@ grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, context.state = 0; context.extcmd = ext; - context.script_params = scripts; + context.script = script; /* Dynamic commands should not perform option parsing before corresponding module gets loaded. */ diff --git a/include/grub/extcmd.h b/include/grub/extcmd.h index a5e4d18fe..773e47854 100644 --- a/include/grub/extcmd.h +++ b/include/grub/extcmd.h @@ -50,8 +50,8 @@ struct grub_extcmd_context struct grub_arg_list *state; - /* Script parameters, if any. */ - struct grub_script **script_params; + /* Script parameter, if any. */ + struct grub_script *script; }; typedef struct grub_extcmd_context *grub_extcmd_context_t; @@ -74,6 +74,6 @@ void grub_unregister_extcmd (grub_extcmd_t cmd); grub_err_t grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, - struct grub_script **scripts); + struct grub_script *script); #endif /* ! GRUB_EXTCMD_HEADER */ diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index 6471c7de3..43791d040 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -62,7 +62,7 @@ struct grub_script_arg char *str; /* Parsed block argument. */ - struct grub_script *block; + struct grub_script *script; /* Next argument part. */ struct grub_script_arg *next; @@ -73,7 +73,7 @@ struct grub_script_argv { unsigned argc; char **args; - struct grub_script **scripts; + struct grub_script *script; }; /* A complete argument. It consists of a list of one or more `struct @@ -234,8 +234,6 @@ void grub_script_argv_free (struct grub_script_argv *argv); int grub_script_argv_next (struct grub_script_argv *argv); int grub_script_argv_append (struct grub_script_argv *argv, const char *s); int grub_script_argv_split_append (struct grub_script_argv *argv, char *s); -int grub_script_argv_script_append (struct grub_script_argv *argv, - struct grub_script *s); struct grub_script_arglist * grub_script_create_arglist (struct grub_parser_param *state); diff --git a/normal/dyncmd.c b/normal/dyncmd.c index 172baa44e..ed98855eb 100644 --- a/normal/dyncmd.c +++ b/normal/dyncmd.c @@ -54,8 +54,7 @@ grub_dyncmd_dispatcher (struct grub_extcmd_context *ctxt, { if (cmd->flags & GRUB_COMMAND_FLAG_BLOCKS && cmd->flags & GRUB_COMMAND_FLAG_EXTCMD) - ret = grub_extcmd_dispatcher (cmd, argc, args, - ctxt->script_params); + ret = grub_extcmd_dispatcher (cmd, argc, args, ctxt->script); else ret = (cmd->func) (cmd, argc, args); } diff --git a/script/argv.c b/script/argv.c index 8dd563345..42d573a0a 100644 --- a/script/argv.c +++ b/script/argv.c @@ -18,6 +18,7 @@ */ #include +#include #include static unsigned @@ -52,18 +53,8 @@ grub_script_argv_free (struct grub_script_argv *argv) grub_free (argv->args); } - if (argv->scripts) - { - for (i = 0; i < argv->argc; i++) - if (argv->scripts[i]) - grub_script_put (argv->scripts[i]); - - grub_free (argv->scripts); - } - argv->argc = 0; argv->args = 0; - argv->scripts = 0; } /* Prepare for next argc. */ @@ -71,7 +62,6 @@ int grub_script_argv_next (struct grub_script_argv *argv) { char **p = argv->args; - struct grub_script **q = argv->scripts; if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0) return 0; @@ -80,24 +70,12 @@ grub_script_argv_next (struct grub_script_argv *argv) if (! p) return 1; - q = grub_realloc (q, round_up_exp ((argv->argc + 2) * sizeof (struct grub_script *))); - if (! q) - { - grub_free (p); - return 1; - } - argv->argc++; argv->args = p; - argv->scripts = q; if (argv->argc == 1) - { - argv->args[0] = 0; - argv->scripts[0] = 0; - } + argv->args[0] = 0; argv->args[argv->argc] = 0; - argv->scripts[argv->argc] = 0; return 0; } @@ -123,18 +101,6 @@ grub_script_argv_append (struct grub_script_argv *argv, const char *s) return 0; } -/* Append grub_script `s' as the last argument. */ -int -grub_script_argv_script_append (struct grub_script_argv *argv, - struct grub_script *script) -{ - if (argv->scripts[argv->argc - 1]) - grub_script_put (argv->scripts[argv->argc - 1]); - - argv->scripts[argv->argc - 1] = grub_script_get (script); - return 0; -} - /* Split `s' and append words as multiple arguments. */ int grub_script_argv_split_append (struct grub_script_argv *argv, char *s) diff --git a/script/execute.c b/script/execute.c index 1e9cd00b7..932be6635 100644 --- a/script/execute.c +++ b/script/execute.c @@ -199,9 +199,9 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, case GRUB_SCRIPT_ARG_TYPE_BLOCK: if (grub_script_argv_append (&result, "{") || grub_script_argv_append (&result, arg->str) || - grub_script_argv_append (&result, "}") || - grub_script_argv_script_append (&result, arg->block)) + grub_script_argv_append (&result, "}")) goto fail; + result.script = arg->script; break; case GRUB_SCRIPT_ARG_TYPE_TEXT: @@ -326,7 +326,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) && (grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD)) ret = grub_extcmd_dispatcher (grubcmd, argv.argc - 1, argv.args + 1, - argv.scripts + 1); + argv.script); else ret = (grubcmd->func) (grubcmd, argv.argc - 1, argv.args + 1); } diff --git a/script/parser.y b/script/parser.y index eea7ba111..76dc3fbca 100644 --- a/script/parser.y +++ b/script/parser.y @@ -79,7 +79,8 @@ %token GRUB_PARSER_TOKEN_NAME "name" %token GRUB_PARSER_TOKEN_WORD "word" -%type word argument block parameters0 parameters1 arguments0 arguments1 +%type block block0 +%type word argument parameters0 parameters1 arguments0 arguments1 %type script_init script %type grubcmd ifclause ifcmd forcmd whilecmd untilcmd @@ -160,21 +161,22 @@ block: "{" commands1 delimiters0 "}" { char *p; - struct grub_script_arg *arg; struct grub_script_mem *memory; memory = grub_script_mem_record_stop (state, $2); if ((p = grub_script_lexer_record_stop (state, $2))) *grub_strrchr (p, '}') = '\0'; - arg = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p); - if (! arg || ! (arg->block = grub_script_create ($3, memory))) + $$ = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p); + if (! $$ || ! ($$->script = grub_script_create ($3, memory))) grub_script_mem_free (memory); - $$ = grub_script_add_arglist (state, 0, arg); grub_script_lexer_deref (state->lexerstate); } ; +block0: /* Empty */ { $$ = 0; } + | block { $$ = $1; } +; arguments0: /* Empty */ { $$ = 0; } | arguments1 { $$ = $1; } @@ -201,27 +203,22 @@ parameters1: argument parameters0 } $$ = $1; } - | block parameters0 - { - if ($1 && $2) - { - $1->next = $2; - $1->argcount += $2->argcount; - $2->argcount = 0; - } - $$ = $1; - } ; parameters0: /* Empty */ { $$ = 0; } | parameters1 { $$ = $1; } ; -grubcmd: word parameters0 +grubcmd: word parameters0 block0 { - if ($1 && $2) { - $1->next = $2; - $1->argcount += $2->argcount; - $2->argcount = 0; + struct grub_script_arglist *x = $2; + + if ($3) + x = grub_script_add_arglist (state, $2, $3); + + if ($1 && x) { + $1->next = x; + $1->argcount += x->argcount; + x->argcount = 0; } $$ = grub_script_create_cmdline (state, $1); } diff --git a/script/script.c b/script/script.c index b7cbdff1d..7cf2f6bae 100644 --- a/script/script.c +++ b/script/script.c @@ -122,7 +122,7 @@ grub_script_arg_add (struct grub_parser_param *state, return arg; argpart->type = type; - argpart->block = 0; + argpart->script = 0; len = grub_strlen (str) + 1; argpart->str = grub_script_malloc (state, len); From 3a082b7f2a92bad9630fd931fff58e33057e537c Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 22 Jul 2010 04:49:05 +0530 Subject: [PATCH 12/18] memory management for block parameters --- include/grub/script_sh.h | 13 +++++++++- script/argv.c | 3 +++ script/execute.c | 2 +- script/main.c | 2 +- script/parser.y | 53 ++++++++++++++++++++++++++++++++++++++-- script/script.c | 10 ++++++++ 6 files changed, 78 insertions(+), 5 deletions(-) diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index 43791d040..7618633ad 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -42,6 +42,10 @@ struct grub_script unsigned refcnt; struct grub_script_mem *mem; struct grub_script_cmd *cmd; + + /* Other grub_script's from block arguments. */ + struct grub_script *siblings; + struct grub_script *children; }; typedef enum @@ -222,6 +226,9 @@ struct grub_parser_param /* The memory that was used while parsing and scanning. */ struct grub_script_mem *memused; + /* The block argument scripts. */ + struct grub_script *scripts; + /* The result of the parser. */ struct grub_script_cmd *parsed; @@ -365,13 +372,17 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline); static inline struct grub_script * grub_script_get (struct grub_script *script) { - script->refcnt++; + if (script) + script->refcnt++; return script; } static inline void grub_script_put (struct grub_script *script) { + if (! script) + return; + if (script->refcnt == 0) grub_script_free (script); else diff --git a/script/argv.c b/script/argv.c index 42d573a0a..a7acbc23e 100644 --- a/script/argv.c +++ b/script/argv.c @@ -52,9 +52,12 @@ grub_script_argv_free (struct grub_script_argv *argv) grub_free (argv->args); } + if (argv->script) + grub_script_put (argv->script); argv->argc = 0; argv->args = 0; + argv->script = 0; } /* Prepare for next argc. */ diff --git a/script/execute.c b/script/execute.c index 932be6635..b9538c29b 100644 --- a/script/execute.c +++ b/script/execute.c @@ -201,7 +201,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, grub_script_argv_append (&result, arg->str) || grub_script_argv_append (&result, "}")) goto fail; - result.script = arg->script; + result.script = grub_script_get (arg->script); break; case GRUB_SCRIPT_ARG_TYPE_TEXT: diff --git a/script/main.c b/script/main.c index 752a8cd8a..19ce88c43 100644 --- a/script/main.c +++ b/script/main.c @@ -34,7 +34,7 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline) grub_script_execute (parsed_script); /* The parsed script was executed, throw it away. */ - grub_script_free (parsed_script); + grub_script_put (parsed_script); } return grub_errno; diff --git a/script/parser.y b/script/parser.y index 76dc3fbca..4f57ea4ff 100644 --- a/script/parser.y +++ b/script/parser.y @@ -38,6 +38,7 @@ struct { unsigned offset; struct grub_script_mem *memory; + struct grub_script *scripts; }; } @@ -152,16 +153,45 @@ argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); } | word { $$ = $1; } ; +/* + Block parameter is passed to commands in two forms: as unparsed + string and as pre-parsed grub_script object. Passing as grub_script + object makes memory management difficult, because: + + (1) Command may want to keep a reference to grub_script objects for + later use, so script framework may not free the grub_script + object after command completes. + + (2) Command may get called multiple times with same grub_script + object under loops, so we should not let command implementation + to free the grub_script object. + + To solve above problems, we rely on reference counting for + grub_script objects. Commands that want to keep the grub_script + object must take a reference to it. + + Other complexity comes with arbitrary nesting of grub_script + objects: a grub_script object may have commands with several block + parameters, and each block parameter may further contain multiple + block parameters nested. We use temporary variable, state->scripts + to collect nested child scripts (that are linked by siblings and + children members), and will build grub_scripts tree from bottom. + */ block: "{" { grub_script_lexer_ref (state->lexerstate); $$ = grub_script_lexer_record_start (state); $$ = grub_script_mem_record (state); + + /* save currently known scripts. */ + $$ = state->scripts; + state->scripts = 0; } commands1 delimiters0 "}" { char *p; struct grub_script_mem *memory; + struct grub_script *s = $2; memory = grub_script_mem_record_stop (state, $2); if ((p = grub_script_lexer_record_stop (state, $2))) @@ -171,6 +201,19 @@ block: "{" if (! $$ || ! ($$->script = grub_script_create ($3, memory))) grub_script_mem_free (memory); + else { + /* attach nested scripts to $$->script as children */ + $$->script->children = state->scripts; + + /* restore old scripts; append $$->script to siblings. */ + state->scripts = $2 ?: $$->script; + if (s) { + while (s->siblings) + s = s->siblings; + s->siblings = $$->script; + } + } + grub_script_lexer_deref (state->lexerstate); } ; @@ -243,10 +286,13 @@ commands1: newlines0 command } ; -function: "function" "name" +function: "function" "name" { grub_script_lexer_ref (state->lexerstate); state->func_mem = grub_script_mem_record (state); + + $$ = state->scripts; + state->scripts = 0; } delimiters0 "{" commands1 delimiters1 "}" { @@ -256,9 +302,12 @@ function: "function" "name" script = grub_script_create ($6, state->func_mem); if (! script) grub_script_mem_free (state->func_mem); - else + else { + script->children = state->scripts; grub_script_function_create ($2, script); + } + state->scripts = $3; grub_script_lexer_deref (state->lexerstate); } ; diff --git a/script/script.c b/script/script.c index 7cf2f6bae..6509b5f5d 100644 --- a/script/script.c +++ b/script/script.c @@ -94,12 +94,19 @@ grub_script_mem_record_stop (struct grub_parser_param *state, void grub_script_free (struct grub_script *script) { + struct grub_script *s; + if (!script) return; if (script->mem) grub_script_mem_free (script->mem); + s = script->children; + while (s) { + grub_script_put (s); + s = s->siblings; + } grub_free (script); } @@ -346,6 +353,8 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem) parsed->mem = mem; parsed->cmd = cmd; parsed->refcnt = 0; + parsed->siblings = 0; + parsed->children = 0; return parsed; } @@ -394,6 +403,7 @@ grub_script_parse (char *script, grub_reader_getline_t getline) parsed->mem = grub_script_mem_record_stop (parsestate, membackup); parsed->cmd = parsestate->parsed; + parsed->children = parsestate->scripts; grub_script_lexer_fini (lexstate); grub_free (parsestate); From 1d0546767810ccce6c2090b675f6b5006f2b307a Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 22 Jul 2010 05:05:49 +0530 Subject: [PATCH 13/18] simplify --- commands/extcmd.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commands/extcmd.c b/commands/extcmd.c index cc0362e14..ecf2d4133 100644 --- a/commands/extcmd.c +++ b/commands/extcmd.c @@ -40,9 +40,7 @@ grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, context.extcmd = ext; context.script = script; - /* Dynamic commands should not perform option parsing before - corresponding module gets loaded. */ - if (cmd->flags & GRUB_COMMAND_FLAG_DYNCMD) + if (! ext->options) { ret = (ext->func) (&context, argc, args); return ret; From 70abc7023b42b2e19cc41760df09cf9a82bded6e Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 22 Jul 2010 21:13:45 +0530 Subject: [PATCH 14/18] fix a memory issue --- script/script.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/script/script.c b/script/script.c index 6509b5f5d..8d856c493 100644 --- a/script/script.c +++ b/script/script.c @@ -95,8 +95,9 @@ void grub_script_free (struct grub_script *script) { struct grub_script *s; + struct grub_script *t; - if (!script) + if (! script) return; if (script->mem) @@ -104,8 +105,9 @@ grub_script_free (struct grub_script *script) s = script->children; while (s) { + t = s->siblings; grub_script_put (s); - s = s->siblings; + s = t; } grub_free (script); } From 88513a523636adfd76db27cdab93ad183fd1d2d2 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Thu, 22 Jul 2010 22:50:04 +0530 Subject: [PATCH 15/18] removed unnecessary grammar rules --- script/parser.y | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/script/parser.y b/script/parser.y index 4f57ea4ff..ce97ab174 100644 --- a/script/parser.y +++ b/script/parser.y @@ -81,7 +81,7 @@ %token GRUB_PARSER_TOKEN_WORD "word" %type block block0 -%type word argument parameters0 parameters1 arguments0 arguments1 +%type word argument arguments0 arguments1 %type script_init script %type grubcmd ifclause ifcmd forcmd whilecmd untilcmd @@ -236,22 +236,7 @@ arguments1: argument arguments0 } ; -parameters1: argument parameters0 - { - if ($1 && $2) - { - $1->next = $2; - $1->argcount += $2->argcount; - $2->argcount = 0; - } - $$ = $1; - } -; -parameters0: /* Empty */ { $$ = 0; } - | parameters1 { $$ = $1; } -; - -grubcmd: word parameters0 block0 +grubcmd: word arguments0 block0 { struct grub_script_arglist *x = $2; From 1c34fc8fec49958b71e37dbc0f75536b845f0c28 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Fri, 30 Jul 2010 02:31:14 +0530 Subject: [PATCH 16/18] fix help message logic --- commands/help.c | 3 ++- normal/dyncmd.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/commands/help.c b/commands/help.c index fecc6f665..1ca46aa7e 100644 --- a/commands/help.c +++ b/commands/help.c @@ -112,7 +112,8 @@ grub_cmd_help (grub_extcmd_context_t ctxt __attribute__ ((unused)), int argc, if (cnt++ > 0) grub_printf ("\n\n"); - if (cmd->flags & GRUB_COMMAND_FLAG_EXTCMD) + if ((cmd->flags & GRUB_COMMAND_FLAG_EXTCMD) && + ! (cmd->flags & GRUB_COMMAND_FLAG_DYNCMD)) grub_arg_show_help ((grub_extcmd_t) cmd->data); else grub_printf ("%s %s %s\n%s\n", _("Usage:"), cmd->name, _(cmd->summary), diff --git a/normal/dyncmd.c b/normal/dyncmd.c index ed98855eb..3519253f6 100644 --- a/normal/dyncmd.c +++ b/normal/dyncmd.c @@ -155,6 +155,7 @@ read_command_list (const char *prefix) grub_dyncmd_dispatcher, GRUB_COMMAND_FLAG_BLOCKS | GRUB_COMMAND_FLAG_EXTCMD + | GRUB_COMMAND_FLAG_CMDLINE | GRUB_COMMAND_FLAG_DYNCMD, 0, N_("not loaded"), 0, prio); From b0ecfcd3603bf2dbca3960676ee196cf4fc75367 Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Wed, 4 Aug 2010 11:21:08 +0530 Subject: [PATCH 17/18] fixed reference counting bug --- script/argv.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/script/argv.c b/script/argv.c index a7acbc23e..63b44e322 100644 --- a/script/argv.c +++ b/script/argv.c @@ -52,9 +52,6 @@ grub_script_argv_free (struct grub_script_argv *argv) grub_free (argv->args); } - if (argv->script) - grub_script_put (argv->script); - argv->argc = 0; argv->args = 0; argv->script = 0; From 67a9e4d95d551594830b3635ec2cdf7acf0035cd Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Mon, 9 Aug 2010 21:42:24 +0530 Subject: [PATCH 18/18] review fixes and a testcase --- conf/tests.rmk | 9 +++++++ include/grub/script_sh.h | 8 +++--- script/execute.c | 2 +- script/main.c | 2 +- script/parser.y | 17 ++++++------ script/script.c | 6 ++--- tests/grub_script_blockarg.in | 41 ++++++++++++++++++++++++++++ tests/test_blockarg.c | 51 +++++++++++++++++++++++++++++++++++ 8 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 tests/grub_script_blockarg.in create mode 100644 tests/test_blockarg.c diff --git a/conf/tests.rmk b/conf/tests.rmk index 9144e5528..a7fdf4033 100644 --- a/conf/tests.rmk +++ b/conf/tests.rmk @@ -19,6 +19,11 @@ functional_test_mod_SOURCES = tests/lib/functional_test.c tests/lib/test.c functional_test_mod_CFLAGS = $(COMMON_CFLAGS) functional_test_mod_LDFLAGS = $(COMMON_LDFLAGS) +pkglib_MODULES += test_blockarg.mod +test_blockarg_mod_SOURCES = tests/test_blockarg.c +test_blockarg_mod_CFLAGS = $(COMMON_CFLAGS) +test_blockarg_mod_LDFLAGS = $(COMMON_LDFLAGS) + # Rules for unit tests check_UTILITIES += example_unit_test example_unit_test_SOURCES = tests/example_unit_test.c kern/list.c kern/misc.c tests/lib/test.c tests/lib/unit_test.c @@ -74,6 +79,9 @@ grub_script_comments_SOURCES = tests/grub_script_comments.in check_SCRIPTS += grub_script_functions grub_script_functions_SOURCES = tests/grub_script_functions.in +check_SCRIPTS += grub_script_blockarg +grub_script_blockarg_SOURCES = tests/grub_script_blockarg.in + # List of tests to execute on "make check" # SCRIPTED_TESTS = example_scripted_test # SCRIPTED_TESTS += example_grub_script_test @@ -91,6 +99,7 @@ SCRIPTED_TESTS += grub_script_final_semicolon SCRIPTED_TESTS += grub_script_dollar SCRIPTED_TESTS += grub_script_comments SCRIPTED_TESTS += grub_script_functions +SCRIPTED_TESTS += grub_script_blockarg # dependencies between tests and testing-tools $(SCRIPTED_TESTS): grub-shell grub-shell-tester diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index ffedf5c75..6a959e16f 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -43,8 +43,8 @@ struct grub_script struct grub_script_mem *mem; struct grub_script_cmd *cmd; - /* Other grub_script's from block arguments. */ - struct grub_script *siblings; + /* grub_scripts from block arguments. */ + struct grub_script *next_siblings; struct grub_script *children; }; @@ -371,7 +371,7 @@ grub_err_t grub_normal_parse_line (char *line, grub_reader_getline_t getline); static inline struct grub_script * -grub_script_get (struct grub_script *script) +grub_script_ref (struct grub_script *script) { if (script) script->refcnt++; @@ -379,7 +379,7 @@ grub_script_get (struct grub_script *script) } static inline void -grub_script_put (struct grub_script *script) +grub_script_unref (struct grub_script *script) { if (! script) return; diff --git a/script/execute.c b/script/execute.c index b9538c29b..932be6635 100644 --- a/script/execute.c +++ b/script/execute.c @@ -201,7 +201,7 @@ grub_script_arglist_to_argv (struct grub_script_arglist *arglist, grub_script_argv_append (&result, arg->str) || grub_script_argv_append (&result, "}")) goto fail; - result.script = grub_script_get (arg->script); + result.script = arg->script; break; case GRUB_SCRIPT_ARG_TYPE_TEXT: diff --git a/script/main.c b/script/main.c index 19ce88c43..620d9deac 100644 --- a/script/main.c +++ b/script/main.c @@ -34,7 +34,7 @@ grub_normal_parse_line (char *line, grub_reader_getline_t getline) grub_script_execute (parsed_script); /* The parsed script was executed, throw it away. */ - grub_script_put (parsed_script); + grub_script_unref (parsed_script); } return grub_errno; diff --git a/script/parser.y b/script/parser.y index ce97ab174..3faaf4736 100644 --- a/script/parser.y +++ b/script/parser.y @@ -208,9 +208,9 @@ block: "{" /* restore old scripts; append $$->script to siblings. */ state->scripts = $2 ?: $$->script; if (s) { - while (s->siblings) - s = s->siblings; - s->siblings = $$->script; + while (s->next_siblings) + s = s->next_siblings; + s->next_siblings = $$->script; } } @@ -243,11 +243,12 @@ grubcmd: word arguments0 block0 if ($3) x = grub_script_add_arglist (state, $2, $3); - if ($1 && x) { - $1->next = x; - $1->argcount += x->argcount; - x->argcount = 0; - } + if ($1 && x) + { + $1->next = x; + $1->argcount += x->argcount; + x->argcount = 0; + } $$ = grub_script_create_cmdline (state, $1); } ; diff --git a/script/script.c b/script/script.c index 8d856c493..25a34be0d 100644 --- a/script/script.c +++ b/script/script.c @@ -105,8 +105,8 @@ grub_script_free (struct grub_script *script) s = script->children; while (s) { - t = s->siblings; - grub_script_put (s); + t = s->next_siblings; + grub_script_unref (s); s = t; } grub_free (script); @@ -355,8 +355,8 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem) parsed->mem = mem; parsed->cmd = cmd; parsed->refcnt = 0; - parsed->siblings = 0; parsed->children = 0; + parsed->next_siblings = 0; return parsed; } diff --git a/tests/grub_script_blockarg.in b/tests/grub_script_blockarg.in new file mode 100644 index 000000000..783cee8e0 --- /dev/null +++ b/tests/grub_script_blockarg.in @@ -0,0 +1,41 @@ +#! /bin/bash + +# Run GRUB script in a Qemu instance +# Copyright (C) 2010 Free Software Foundation, Inc. +# +# GRUB is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# GRUB is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GRUB. If not, see . + +error_if_not () { + if test "$1" != "$2"; then + echo "[$1]" != "[$2]" + exit 1 + fi +} + +cmd='test_blockarg { true }' +v=`echo "$cmd" | @builddir@/grub-shell` +error_if_not "$v" '{ true }' + +tmp=`mktemp` +cmd='test_blockarg { test_blockarg { true } }' +echo "$cmd" | @builddir@/grub-shell >$tmp +error_if_not "`head -n1 $tmp|tail -n1`" '{ test_blockarg { true } }' +error_if_not "`head -n2 $tmp|tail -n1`" '{ true }' + +cmd='test_blockarg { test_blockarg { test_blockarg { true } }; test_blockarg { true } }' +echo "$cmd" | @builddir@/grub-shell >$tmp +error_if_not "`head -n1 $tmp|tail -n1`" '{ test_blockarg { test_blockarg { true } }; test_blockarg { true } }' +error_if_not "`head -n2 $tmp|tail -n1`" '{ test_blockarg { true } }' +error_if_not "`head -n3 $tmp|tail -n1`" '{ true }' +error_if_not "`head -n4 $tmp|tail -n1`" '{ true }' diff --git a/tests/test_blockarg.c b/tests/test_blockarg.c new file mode 100644 index 000000000..bb6f3c3f0 --- /dev/null +++ b/tests/test_blockarg.c @@ -0,0 +1,51 @@ +/* test_blockarg.c - print and execute block argument */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +static grub_err_t +test_blockarg (grub_extcmd_context_t ctxt, int argc, char **args) +{ + if (! ctxt->script) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "no block parameter"); + + grub_printf ("%s\n", args[argc - 1]); + grub_script_execute (ctxt->script); + return GRUB_ERR_NONE; +} + +static grub_extcmd_t cmd; + +GRUB_MOD_INIT(test_blockarg) +{ + cmd = grub_register_extcmd ("test_blockarg", test_blockarg, + GRUB_COMMAND_FLAG_BOTH | GRUB_COMMAND_FLAG_BLOCKS, + N_("BLOCK"), + N_("Print and execute block argument."), 0); +} + +GRUB_MOD_FINI(test_blockarg) +{ + grub_unregister_extcmd (cmd); +}