Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1I3l8Q-0005Pk-TD for mharc-grub-devel@gnu.org; Wed, 27 Jun 2007 23:57:34 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1I3l8O-0005PC-R6 for grub-devel@gnu.org; Wed, 27 Jun 2007 23:57:32 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1I3l8O-0005Om-0e for grub-devel@gnu.org; Wed, 27 Jun 2007 23:57:32 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1I3l8N-0005Oj-Q9 for grub-devel@gnu.org; Wed, 27 Jun 2007 23:57:31 -0400 Received: from m15-113.126.com ([220.181.15.113]) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1I3l8L-0007yE-NY for grub-devel@gnu.org; Wed, 27 Jun 2007 23:57:31 -0400 Received: from localhost (unknown [123.49.203.236]) by smtp5 (Coremail) with SMTP id wKjJDbAb9wKZMYNG1vrwBA==.7735S2; Thu, 28 Jun 2007 11:57:14 +0800 (CST) Date: Thu, 28 Jun 2007 11:56:58 +0800 From: Bean To: The development of GRUB 2 Message-ID: <20070628035658.GA12179@ws3.vdp.com> References: <20070625121637.GA19912@ws3.vdp.com> <20070627113523.GA16441@ws3.vdp.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="y0ulUmNC+osPPQO6" Content-Disposition: inline In-Reply-To: <20070627113523.GA16441@ws3.vdp.com> User-Agent: Mutt/1.5.13 (2006-08-11) X-Coremail-Antispam: 1U3Yxn0WfASr-VFAUDIcSsGvfJTg7C26xCjj4IEI4klw4CSww Aac4AC62xK8xCEY4vEwIxC4wC2zVAF1VAY17CE14v26r1j6r15Mx02cVAKzwASzI0EjI02 j7AqF2xKxwAv7VC0I7IYx2IY67AKxVWUJVWUGwCF72vE52k0Y41lYx0Ex4A2jsIE14v26r 1j6r4UM7C26IkvcIIF6IxKo4kEV4ylc2IjII80xcxEwVAKI48JM7AC8VAFwI0_Jr0_Gr1l b4IE77IF4wAFIxvE14AKwVWUJVWUGwAqx4xG64xvF2IEw4CE5I8CrVC2j2Wlb7Iv0xC_Cr UanT9S1TB71UUUUUUa7-sFnT9fnUUI43ZEXa7IUnDCztUUUUUFnT9fnUU== X-detected-kernel: Linux 2.6, seldom 2.4 (older, 4) Subject: [PATCH] bug fix for scripting (update 3) X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jun 2007 03:57:33 -0000 --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This update includes u2 and u2-1, and fix a small problem caused by u2-2. -- Bean --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="grub2-parser-u3.diff" 2007-06-28 Bean * kern/parser.c (state_transitions): Exit GRUB_PARSER_STATE_VARNAME state when "/" is encountered. * normal/execute.c (grub_script_argument_to_string): Check for undefined variable. * normal/execute.c (grub_script_execute_cmdline): Reset grub_errno. * normal/main.c (read_config_file): Reset grub_errno. * normal/lexer.c (grub_script_yylex2): Construct arg structure when mixed text is detected. * normal/lexer.c (check_textstate): Add GRUB_PARSER_STATE_ESC. * normal/parser.y : Change attribute of token GRUB_PARSER_TOKEN_VAR. Index: kern/parser.c =================================================================== RCS file: /sources/grub/grub2/kern/parser.c,v retrieving revision 1.2 diff -u -r1.2 parser.c --- kern/parser.c 23 Nov 2005 03:36:24 -0000 1.2 +++ kern/parser.c 28 Jun 2007 03:31:55 -0000 @@ -44,6 +44,7 @@ { GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0}, { GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1}, { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1}, + { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '/', 1}, { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0}, { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0}, Index: normal/execute.c =================================================================== RCS file: /sources/grub/grub2/normal/execute.c,v retrieving revision 1.4 diff -u -r1.4 execute.c --- normal/execute.c 28 May 2006 21:58:34 -0000 1.4 +++ normal/execute.c 28 Jun 2007 03:31:55 -0000 @@ -50,7 +50,8 @@ if (argi->type == 1) { val = grub_env_get (argi->str); - size += grub_strlen (val); + if (val) + size += grub_strlen (val); } else size += grub_strlen (argi->str); @@ -68,7 +69,8 @@ if (argi->type == 1) { val = grub_env_get (argi->str); - grub_strcat (chararg, val); + if (val) + grub_strcat (chararg, val); } else grub_strcat (chararg, argi->str); @@ -100,6 +102,9 @@ grubcmd = grub_command_find (cmdline->cmdname); if (! grubcmd) { + /* Reset grub_errno. */ + grub_errno = GRUB_ERR_NONE; + /* It's not a GRUB command, try all functions. */ func = grub_script_function_find (cmdline->cmdname); if (! func) @@ -119,7 +124,8 @@ grub_errno = GRUB_ERR_NONE; } grub_free (assign); - return 0; + ret = grub_errno; + goto quit; } } @@ -168,6 +174,7 @@ grub_free (args[i]); grub_free (args); +quit: grub_sprintf (errnobuf, "%d", ret); grub_env_set ("?", errnobuf); Index: normal/lexer.c =================================================================== RCS file: /sources/grub/grub2/normal/lexer.c,v retrieving revision 1.6 diff -u -r1.6 lexer.c --- normal/lexer.c 4 Jun 2006 15:56:55 -0000 1.6 +++ normal/lexer.c 28 Jun 2007 03:31:55 -0000 @@ -41,7 +41,8 @@ { return (state == GRUB_PARSER_STATE_TEXT || state == GRUB_PARSER_STATE_QUOTE - || state == GRUB_PARSER_STATE_DQUOTE); + || state == GRUB_PARSER_STATE_DQUOTE + || state == GRUB_PARSER_STATE_ESC); } struct grub_lexer_param * @@ -161,6 +162,8 @@ char *buffer; char *bp; struct grub_lexer_param *state = parsestate->lexerstate; + struct grub_script_arg *arg = 0; + int done = 0; if (state->done) return 0; @@ -196,13 +199,14 @@ } } +back: newstate = grub_parser_cmdline_state (state->state, *state->script, &use); /* Check if it is a text. */ if (check_textstate (newstate)) { /* In case the string is not quoted, this can be a one char - length symbol. */ + length symbol. */ if (newstate == GRUB_PARSER_STATE_TEXT) { switch (*state->script) @@ -213,7 +217,7 @@ newstate = grub_parser_cmdline_state (state->state, *state->script, &use); if (! (state->state == GRUB_PARSER_STATE_TEXT - && *state->script == ' ')) + && *state->script == ' ')) { grub_dprintf ("scripting", "token=` '\n"); return ' '; @@ -260,8 +264,6 @@ if (newstate == GRUB_PARSER_STATE_TEXT && state->state != GRUB_PARSER_STATE_ESC) { - int breakout = 0; - switch (use) { case ' ': @@ -269,13 +271,13 @@ case '}': case ';': case '\n': - breakout = 1; + done = 1; } - if (breakout) + if (done) break; - *(bp++) = use; } - else if (use) + + if (use) *(bp++) = use; state->state = newstate; @@ -285,27 +287,39 @@ /* A string of text was read in. */ *bp = '\0'; grub_dprintf ("scripting", "token=`%s'\n", buffer); - yylval->string = buffer; - /* Detect some special tokens. */ - if (! grub_strcmp (buffer, "while")) - return GRUB_PARSER_TOKEN_WHILE; - else if (! grub_strcmp (buffer, "if")) - return GRUB_PARSER_TOKEN_IF; - else if (! grub_strcmp (buffer, "function")) - return GRUB_PARSER_TOKEN_FUNCTION; - else if (! grub_strcmp (buffer, "menuentry")) - return GRUB_PARSER_TOKEN_MENUENTRY; - else if (! grub_strcmp (buffer, "@")) - return GRUB_PARSER_TOKEN_MENUENTRY; - else if (! grub_strcmp (buffer, "else")) - return GRUB_PARSER_TOKEN_ELSE; - else if (! grub_strcmp (buffer, "then")) - return GRUB_PARSER_TOKEN_THEN; - else if (! grub_strcmp (buffer, "fi")) - return GRUB_PARSER_TOKEN_FI; - else - return GRUB_PARSER_TOKEN_NAME; + if (!*state->script) + done = 1; + + if ((done) && (!arg)) + { + yylval->string = buffer; + + /* Detect some special tokens. */ + if (! grub_strcmp (buffer, "while")) + return GRUB_PARSER_TOKEN_WHILE; + else if (! grub_strcmp (buffer, "if")) + return GRUB_PARSER_TOKEN_IF; + else if (! grub_strcmp (buffer, "function")) + return GRUB_PARSER_TOKEN_FUNCTION; + else if (! grub_strcmp (buffer, "menuentry")) + return GRUB_PARSER_TOKEN_MENUENTRY; + else if (! grub_strcmp (buffer, "@")) + return GRUB_PARSER_TOKEN_MENUENTRY; + else if (! grub_strcmp (buffer, "else")) + return GRUB_PARSER_TOKEN_ELSE; + else if (! grub_strcmp (buffer, "then")) + return GRUB_PARSER_TOKEN_THEN; + else if (! grub_strcmp (buffer, "fi")) + return GRUB_PARSER_TOKEN_FI; + else + return GRUB_PARSER_TOKEN_NAME; + } + + if (bp != buffer) + arg = + grub_script_arg_add (parsestate, arg, GRUB_SCRIPT_ARG_TYPE_STR, + buffer); } else if (newstate == GRUB_PARSER_STATE_VAR || newstate == GRUB_PARSER_STATE_QVAR) @@ -341,19 +355,33 @@ } *bp = '\0'; - state->state = newstate; - yylval->string = buffer; - grub_dprintf ("scripting", "vartoken=`%s'\n", buffer); + if (newstate == GRUB_PARSER_STATE_VARNAME) + state->state = GRUB_PARSER_STATE_TEXT; - return GRUB_PARSER_TOKEN_VAR; + if (bp != buffer) + arg = grub_script_arg_add (parsestate, arg, GRUB_SCRIPT_ARG_TYPE_VAR, + buffer); } else { /* There is either text or a variable name. In the case you - arrive here there is a serious problem with the lexer. */ + arrive here there is a serious problem with the lexer. */ grub_error (GRUB_ERR_BAD_ARGUMENT, "Internal error\n"); return 0; } + + if ((done) || (! *state->script)) + { + if (arg) + { + yylval->arg = arg; + return GRUB_PARSER_TOKEN_VAR; + } + else + return ' '; + } + + goto back; } void Index: normal/main.c =================================================================== RCS file: /sources/grub/grub2/normal/main.c,v retrieving revision 1.20 diff -u -r1.20 main.c --- normal/main.c 5 Mar 2007 20:32:43 -0000 1.20 +++ normal/main.c 28 Jun 2007 03:31:55 -0000 @@ -262,6 +262,9 @@ /* Execute the command(s). */ grub_script_execute (parsed_script); + /* Reset grub_errno. */ + grub_errno = GRUB_ERR_NONE; + /* The parsed script was executed, throw it away. */ grub_script_free (parsed_script); } Index: normal/parser.y =================================================================== RCS file: /sources/grub/grub2/normal/parser.y,v retrieving revision 1.6 diff -u -r1.6 parser.y --- normal/parser.y 4 Jun 2006 15:56:55 -0000 1.6 +++ normal/parser.y 28 Jun 2007 03:31:55 -0000 @@ -46,9 +46,9 @@ %token GRUB_PARSER_TOKEN_VAR %type script grubcmd command commands commandblock menuentry if %type arguments; -%type argument; +%type argument GRUB_PARSER_TOKEN_VAR; %type "if" "while" "function" "else" "then" "fi" -%type text GRUB_PARSER_TOKEN_NAME GRUB_PARSER_TOKEN_VAR +%type text GRUB_PARSER_TOKEN_NAME %pure-parser %lex-param { struct grub_parser_param *state }; @@ -86,7 +86,7 @@ for example: `foo${bar}baz'. */ argument: GRUB_PARSER_TOKEN_VAR { - $$ = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_VAR, $1); + $$ = $1; } | text { --y0ulUmNC+osPPQO6--