[2337] 2009-06-17 Vladimir Serbinenko <[email protected]>

Vladimir Serbinenko <[email protected]> Wed, 17 Jun 2009 13:47:38 +0000
Newsgroups gmane.comp.boot-loaders.grub.cvs
Message-ID <[email protected]>
Revision: 2337
          http://svn.sv.gnu.org/viewvc/?view=rev&root=grub&revision=2337
Author:   phcoder
Date:     2009-06-17 13:47:37 +0000 (Wed, 17 Jun 2009)
Log Message:
-----------
2009-06-17  Vladimir Serbinenko  <[email protected]>

	Fix newline handling

	* include/grub/script_sh.h (grub_lexer_param): new field was_newline
	* script/sh/lexer.c (grub_script_lexer_init): initilaise was_newline
	(grub_script_yylex): don't segfault on unterminated script
	newline terminates command and variable

Modified Paths:
--------------
    trunk/grub2/ChangeLog
    trunk/grub2/include/grub/script_sh.h
    trunk/grub2/script/sh/lexer.c

Modified: trunk/grub2/ChangeLog
===================================================================
--- trunk/grub2/ChangeLog	2009-06-17 13:15:33 UTC (rev 2336)
+++ trunk/grub2/ChangeLog	2009-06-17 13:47:37 UTC (rev 2337)
@@ -1,5 +1,14 @@
 2009-06-17  Vladimir Serbinenko  <[email protected]>
 
+	Fix newline handling
+
+	* include/grub/script_sh.h (grub_lexer_param): new field was_newline
+	* script/sh/lexer.c (grub_script_lexer_init): initilaise was_newline
+	(grub_script_yylex): don't segfault on unterminated script
+	newline terminates command and variable
+
+2009-06-17  Vladimir Serbinenko  <[email protected]>
+
 	avoid double grub_adjust_range call. Bug reported by David Simner
 
 	* kern/disk.c (grub_disk_write): change to raw disk access before

Modified: trunk/grub2/include/grub/script_sh.h
===================================================================
--- trunk/grub2/include/grub/script_sh.h	2009-06-17 13:15:33 UTC (rev 2336)
+++ trunk/grub2/include/grub/script_sh.h	2009-06-17 13:47:37 UTC (rev 2337)
@@ -159,6 +159,9 @@
 
   /* The token that is already parsed but not yet returned. */
   int tokenonhold;
+
+  /* Was the last token a newline? */
+  int was_newline;
 };
 
 /* State of the parser as passes to the parser.  */

Modified: trunk/grub2/script/sh/lexer.c
===================================================================
--- trunk/grub2/script/sh/lexer.c	2009-06-17 13:15:33 UTC (rev 2336)
+++ trunk/grub2/script/sh/lexer.c	2009-06-17 13:47:37 UTC (rev 2337)
@@ -64,6 +64,7 @@
   param->recordpos = 0;
   param->recordlen = 0;
   param->tokenonhold = 0;
+  param->was_newline = 0;
 
   return param;
 }
@@ -158,8 +159,7 @@
 
   for (;! state->done; firstrun = 0)
     {
-
-      if (! *state->script)
+      if (! state->script || ! *state->script)
 	{
 	  /* Check if more tokens are requested by the parser.  */
 	  if (((state->refs && ! parsestate->err)
@@ -169,8 +169,17 @@
 	      && state->getline)
 	    {
 	      int doexit = 0;
-	      while (!state->script || ! *state->script)
+	      if (state->state != GRUB_PARSER_STATE_ESC
+		  && state->state != GRUB_PARSER_STATE_QUOTE
+		  && state->state != GRUB_PARSER_STATE_DQUOTE
+		  && ! state->was_newline)
 		{
+		  state->was_newline = 1;
+		  state->tokenonhold = '\n';
+		  break;
+		}
+	      while (! state->script || ! *state->script)
+		{
 		  grub_free (state->newscript);
 		  state->newscript = 0;
 		  state->getline (&state->newscript, 1);
@@ -185,13 +194,10 @@
 		break;
 	      grub_dprintf ("scripting", "token=`\\n'\n");
 	      recordchar (state, '\n');
-	      if (state->state != GRUB_PARSER_STATE_ESC
-		  && state->state != GRUB_PARSER_STATE_DQUOTE
-		  && state->state != GRUB_PARSER_STATE_QUOTE)
-		{
-		  state->tokenonhold = '\n';
-		  break;
-		}
+	      if (state->state == GRUB_PARSER_STATE_VARNAME)
+		state->state = GRUB_PARSER_STATE_TEXT;
+	      if (state->state == GRUB_PARSER_STATE_QVARNAME)
+		state->state = GRUB_PARSER_STATE_DQUOTE;
 	      if (state->state == GRUB_PARSER_STATE_DQUOTE
 		  || state->state == GRUB_PARSER_STATE_QUOTE)
 		yylval->arg = grub_script_arg_add (parsestate, yylval->arg,
@@ -208,6 +214,7 @@
 	      break;
 	    }
 	}
+      state->was_newline = 0;
 
       newstate = grub_parser_cmdline_state (state->state, *state->script, &use);