nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0108-Remove-nested-functions-from-script-reading-and-pars.patch

f96e0b
From 6b2f4673544608621b9c527983037f446caa201d Mon Sep 17 00:00:00 2001
f96e0b
From: Colin Watson <cjwatson@ubuntu.com>
f96e0b
Date: Tue, 15 Jan 2013 12:03:25 +0000
f96e0b
Subject: [PATCH 108/482] Remove nested functions from script reading and
f96e0b
 parsing.
f96e0b
f96e0b
* grub-core/kern/parser.c (grub_parser_split_cmdline): Add
f96e0b
getline_data argument, passed to getline.
f96e0b
* grub-core/kern/rescue_parser.c (grub_rescue_parse_line): Add
f96e0b
getline_data argument, passed to grub_parser_split_cmdline.
f96e0b
* grub-core/script/lexer.c (grub_script_lexer_yywrap): Pass
f96e0b
lexerstate->getline_data to lexerstate->getline.
f96e0b
(grub_script_lexer_init): Add getline_data argument, saved in
f96e0b
lexerstate->getline_data.
f96e0b
* grub-core/script/main.c (grub_normal_parse_line): Add getline_data
f96e0b
argument, passed to grub_script_parse.
f96e0b
* grub-core/script/script.c (grub_script_parse): Add getline_data
f96e0b
argument, passed to grub_script_lexer_init.
f96e0b
* include/grub/parser.h (grub_parser_split_cmdline): Update
f96e0b
prototype.  Update all callers to pass appropriate getline data.
f96e0b
(struct grub_parser.parse_line): Likewise.
f96e0b
(grub_rescue_parse_line): Likewise.
f96e0b
* include/grub/reader.h (grub_reader_getline_t): Add void *
f96e0b
argument.
f96e0b
* include/grub/script_sh.h (struct grub_lexer_param): Add
f96e0b
getline_data member.
f96e0b
(grub_script_parse): Update prototype.  Update all callers to pass
f96e0b
appropriate getline data.
f96e0b
(grub_script_lexer_init): Likewise.
f96e0b
(grub_normal_parse_line): Likewise.
f96e0b
f96e0b
* grub-core/commands/legacycfg.c (legacy_file_getline): Add unused
f96e0b
data argument.
f96e0b
* grub-core/kern/parser.c (grub_parser_execute: getline): Make
f96e0b
static instead of nested.  Rename to ...
f96e0b
(grub_parser_execute_getline): ... this.
f96e0b
* grub-core/kern/rescue_reader.c (grub_rescue_read_line): Add unused
f96e0b
data argument.
f96e0b
* grub-core/normal/main.c (read_config_file: getline): Make static
f96e0b
instead of nested.  Rename to ...
f96e0b
(read_config_file_getline): ... this.
f96e0b
(grub_normal_read_line): Add unused data argument.
f96e0b
* grub-core/script/execute.c (grub_script_execute_sourcecode:
f96e0b
getline): Make static instead of nested.  Rename to ...
f96e0b
(grub_script_execute_sourcecode_getline): ... this.
f96e0b
* util/grub-script-check.c (main: get_config_line): Make static
f96e0b
instead of nested.
f96e0b
---
f96e0b
 ChangeLog                      |  46 ++++++++++++++++
f96e0b
 grub-core/commands/legacycfg.c |   7 +--
f96e0b
 grub-core/kern/parser.c        |  54 ++++++++++---------
f96e0b
 grub-core/kern/rescue_parser.c |   6 ++-
f96e0b
 grub-core/kern/rescue_reader.c |   7 +--
f96e0b
 grub-core/normal/completion.c  |   2 +-
f96e0b
 grub-core/normal/main.c        |  53 +++++++++---------
f96e0b
 grub-core/script/execute.c     |  51 ++++++++++--------
f96e0b
 grub-core/script/lexer.c       |   9 ++--
f96e0b
 grub-core/script/main.c        |   5 +-
f96e0b
 grub-core/script/script.c      |   6 ++-
f96e0b
 include/grub/parser.h          |   7 ++-
f96e0b
 include/grub/reader.h          |   2 +-
f96e0b
 include/grub/script_sh.h       |  12 +++--
f96e0b
 util/grub-script-check.c       | 119 ++++++++++++++++++++++-------------------
f96e0b
 15 files changed, 238 insertions(+), 148 deletions(-)
f96e0b
f96e0b
diff --git a/ChangeLog b/ChangeLog
f96e0b
index d07f235..d02749b 100644
f96e0b
--- a/ChangeLog
f96e0b
+++ b/ChangeLog
f96e0b
@@ -1,5 +1,51 @@
f96e0b
 2013-01-15  Colin Watson  <cjwatson@ubuntu.com>
f96e0b
 
f96e0b
+	Remove nested functions from script reading and parsing.
f96e0b
+
f96e0b
+	* grub-core/kern/parser.c (grub_parser_split_cmdline): Add
f96e0b
+	getline_data argument, passed to getline.
f96e0b
+	* grub-core/kern/rescue_parser.c (grub_rescue_parse_line): Add
f96e0b
+	getline_data argument, passed to grub_parser_split_cmdline.
f96e0b
+	* grub-core/script/lexer.c (grub_script_lexer_yywrap): Pass
f96e0b
+	lexerstate->getline_data to lexerstate->getline.
f96e0b
+	(grub_script_lexer_init): Add getline_data argument, saved in
f96e0b
+	lexerstate->getline_data.
f96e0b
+	* grub-core/script/main.c (grub_normal_parse_line): Add getline_data
f96e0b
+	argument, passed to grub_script_parse.
f96e0b
+	* grub-core/script/script.c (grub_script_parse): Add getline_data
f96e0b
+	argument, passed to grub_script_lexer_init.
f96e0b
+	* include/grub/parser.h (grub_parser_split_cmdline): Update
f96e0b
+	prototype.  Update all callers to pass appropriate getline data.
f96e0b
+	(struct grub_parser.parse_line): Likewise.
f96e0b
+	(grub_rescue_parse_line): Likewise.
f96e0b
+	* include/grub/reader.h (grub_reader_getline_t): Add void *
f96e0b
+	argument.
f96e0b
+	* include/grub/script_sh.h (struct grub_lexer_param): Add
f96e0b
+	getline_data member.
f96e0b
+	(grub_script_parse): Update prototype.  Update all callers to pass
f96e0b
+	appropriate getline data.
f96e0b
+	(grub_script_lexer_init): Likewise.
f96e0b
+	(grub_normal_parse_line): Likewise.
f96e0b
+
f96e0b
+	* grub-core/commands/legacycfg.c (legacy_file_getline): Add unused
f96e0b
+	data argument.
f96e0b
+	* grub-core/kern/parser.c (grub_parser_execute: getline): Make
f96e0b
+	static instead of nested.  Rename to ...
f96e0b
+	(grub_parser_execute_getline): ... this.
f96e0b
+	* grub-core/kern/rescue_reader.c (grub_rescue_read_line): Add unused
f96e0b
+	data argument.
f96e0b
+	* grub-core/normal/main.c (read_config_file: getline): Make static
f96e0b
+	instead of nested.  Rename to ...
f96e0b
+	(read_config_file_getline): ... this.
f96e0b
+	(grub_normal_read_line): Add unused data argument.
f96e0b
+	* grub-core/script/execute.c (grub_script_execute_sourcecode:
f96e0b
+	getline): Make static instead of nested.  Rename to ...
f96e0b
+	(grub_script_execute_sourcecode_getline): ... this.
f96e0b
+	* util/grub-script-check.c (main: get_config_line): Make static
f96e0b
+	instead of nested.
f96e0b
+
f96e0b
+2013-01-15  Colin Watson  <cjwatson@ubuntu.com>
f96e0b
+
f96e0b
 	Remove nested functions from memory map iterators.
f96e0b
 
f96e0b
 	* grub-core/efiemu/mm.c (grub_efiemu_mmap_iterate): Add hook_data
f96e0b
diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c
f96e0b
index e34eed4..a3ad5c6 100644
f96e0b
--- a/grub-core/commands/legacycfg.c
f96e0b
+++ b/grub-core/commands/legacycfg.c
f96e0b
@@ -37,7 +37,8 @@ GRUB_MOD_LICENSE ("GPLv3+");
f96e0b
 
f96e0b
 /* Helper for legacy_file.  */
f96e0b
 static grub_err_t
f96e0b
-legacy_file_getline (char **line, int cont __attribute__ ((unused)))
f96e0b
+legacy_file_getline (char **line, int cont __attribute__ ((unused)),
f96e0b
+		     void *data __attribute__ ((unused)))
f96e0b
 {
f96e0b
   *line = 0;
f96e0b
   return GRUB_ERR_NONE;
f96e0b
@@ -134,7 +135,7 @@ legacy_file (const char *filename)
f96e0b
 
f96e0b
       if (parsed && !entryname)
f96e0b
 	{
f96e0b
-	  grub_normal_parse_line (parsed, legacy_file_getline);
f96e0b
+	  grub_normal_parse_line (parsed, legacy_file_getline, NULL);
f96e0b
 	  grub_print_error ();
f96e0b
 	  grub_free (parsed);
f96e0b
 	  parsed = NULL;
f96e0b
@@ -180,7 +181,7 @@ legacy_file (const char *filename)
f96e0b
       grub_free (args);
f96e0b
     }
f96e0b
 
f96e0b
-  grub_normal_parse_line (suffix, legacy_file_getline);
f96e0b
+  grub_normal_parse_line (suffix, legacy_file_getline, NULL);
f96e0b
   grub_print_error ();
f96e0b
   grub_free (suffix);
f96e0b
   grub_free (entrysrc);
f96e0b
diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
f96e0b
index d1be53e..b261fa0 100644
f96e0b
--- a/grub-core/kern/parser.c
f96e0b
+++ b/grub-core/kern/parser.c
f96e0b
@@ -107,7 +107,8 @@ check_varstate (grub_parser_state_t s)
f96e0b
 }
f96e0b
 
f96e0b
 grub_err_t
f96e0b
-grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
f96e0b
+grub_parser_split_cmdline (const char *cmdline,
f96e0b
+			   grub_reader_getline_t getline, void *getline_data,
f96e0b
 			   int *argc, char ***argv)
f96e0b
 {
f96e0b
   grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
f96e0b
@@ -149,7 +150,7 @@ grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
f96e0b
       if (!rd || !*rd)
f96e0b
 	{
f96e0b
 	  if (getline)
f96e0b
-	    getline (&rd, 1);
f96e0b
+	    getline (&rd, 1, getline_data);
f96e0b
 	  else
f96e0b
 	    break;
f96e0b
 	}
f96e0b
@@ -232,36 +233,39 @@ grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
f96e0b
   return 0;
f96e0b
 }
f96e0b
 
f96e0b
+/* Helper for grub_parser_execute.  */
f96e0b
+static grub_err_t
f96e0b
+grub_parser_execute_getline (char **line, int cont __attribute__ ((unused)),
f96e0b
+			     void *data)
f96e0b
+{
f96e0b
+  char **source = data;
f96e0b
+  char *p;
f96e0b
+
f96e0b
+  if (!*source)
f96e0b
+    {
f96e0b
+      *line = 0;
f96e0b
+      return 0;
f96e0b
+    }
f96e0b
+
f96e0b
+  p = grub_strchr (*source, '\n');
f96e0b
+
f96e0b
+  if (p)
f96e0b
+    *line = grub_strndup (*source, p - *source);
f96e0b
+  else
f96e0b
+    *line = grub_strdup (*source);
f96e0b
+  *source = p ? p + 1 : 0;
f96e0b
+  return 0;
f96e0b
+}
f96e0b
+
f96e0b
 grub_err_t
f96e0b
 grub_parser_execute (char *source)
f96e0b
 {
f96e0b
-  auto grub_err_t getline (char **line, int cont);
f96e0b
-  grub_err_t getline (char **line, int cont __attribute__ ((unused)))
f96e0b
-  {
f96e0b
-    char *p;
f96e0b
-
f96e0b
-    if (!source)
f96e0b
-      {
f96e0b
-	*line = 0;
f96e0b
-	return 0;
f96e0b
-      }
f96e0b
-
f96e0b
-    p = grub_strchr (source, '\n');
f96e0b
-
f96e0b
-    if (p)
f96e0b
-      *line = grub_strndup (source, p - source);
f96e0b
-    else
f96e0b
-      *line = grub_strdup (source);
f96e0b
-    source = p ? p + 1 : 0;
f96e0b
-    return 0;
f96e0b
-  }
f96e0b
-
f96e0b
   while (source)
f96e0b
     {
f96e0b
       char *line;
f96e0b
 
f96e0b
-      getline (&line, 0);
f96e0b
-      grub_rescue_parse_line (line, getline);
f96e0b
+      grub_parser_execute_getline (&line, 0, &source);
f96e0b
+      grub_rescue_parse_line (line, grub_parser_execute_getline, &source);
f96e0b
       grub_free (line);
f96e0b
     }
f96e0b
 
f96e0b
diff --git a/grub-core/kern/rescue_parser.c b/grub-core/kern/rescue_parser.c
f96e0b
index 656342d..ab3d041 100644
f96e0b
--- a/grub-core/kern/rescue_parser.c
f96e0b
+++ b/grub-core/kern/rescue_parser.c
f96e0b
@@ -26,14 +26,16 @@
f96e0b
 #include <grub/i18n.h>
f96e0b
 
f96e0b
 grub_err_t
f96e0b
-grub_rescue_parse_line (char *line, grub_reader_getline_t getline)
f96e0b
+grub_rescue_parse_line (char *line,
f96e0b
+			grub_reader_getline_t getline, void *getline_data)
f96e0b
 {
f96e0b
   char *name;
f96e0b
   int n;
f96e0b
   grub_command_t cmd;
f96e0b
   char **args;
f96e0b
 
f96e0b
-  if (grub_parser_split_cmdline (line, getline, &n, &args) || n < 0)
f96e0b
+  if (grub_parser_split_cmdline (line, getline, getline_data, &n, &args)
f96e0b
+      || n < 0)
f96e0b
     return grub_errno;
f96e0b
 
f96e0b
   if (n == 0)
f96e0b
diff --git a/grub-core/kern/rescue_reader.c b/grub-core/kern/rescue_reader.c
f96e0b
index 4587b94..dcd7d44 100644
f96e0b
--- a/grub-core/kern/rescue_reader.c
f96e0b
+++ b/grub-core/kern/rescue_reader.c
f96e0b
@@ -30,7 +30,8 @@ static char linebuf[GRUB_RESCUE_BUF_SIZE];
f96e0b
 
f96e0b
 /* Prompt to input a command and read the line.  */
f96e0b
 static grub_err_t
f96e0b
-grub_rescue_read_line (char **line, int cont)
f96e0b
+grub_rescue_read_line (char **line, int cont,
f96e0b
+		       void *data __attribute__ ((unused)))
f96e0b
 {
f96e0b
   int c;
f96e0b
   int pos = 0;
f96e0b
@@ -87,11 +88,11 @@ grub_rescue_run (void)
f96e0b
       grub_print_error ();
f96e0b
       grub_errno = GRUB_ERR_NONE;
f96e0b
 
f96e0b
-      grub_rescue_read_line (&line, 0);
f96e0b
+      grub_rescue_read_line (&line, 0, NULL);
f96e0b
       if (! line || line[0] == '\0')
f96e0b
 	continue;
f96e0b
 
f96e0b
-      grub_rescue_parse_line (line, grub_rescue_read_line);
f96e0b
+      grub_rescue_parse_line (line, grub_rescue_read_line, NULL);
f96e0b
       grub_free (line);
f96e0b
     }
f96e0b
 }
f96e0b
diff --git a/grub-core/normal/completion.c b/grub-core/normal/completion.c
f96e0b
index cae78f1..805f002 100644
f96e0b
--- a/grub-core/normal/completion.c
f96e0b
+++ b/grub-core/normal/completion.c
f96e0b
@@ -418,7 +418,7 @@ grub_normal_do_completion (char *buf, int *restore,
f96e0b
 
f96e0b
   *restore = 1;
f96e0b
 
f96e0b
-  if (grub_parser_split_cmdline (buf, 0, &argc, &argv))
f96e0b
+  if (grub_parser_split_cmdline (buf, 0, 0, &argc, &argv))
f96e0b
     return 0;
f96e0b
 
f96e0b
   if (argc == 0)
f96e0b
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
f96e0b
index 13473ec..07f337d 100644
f96e0b
--- a/grub-core/normal/main.c
f96e0b
+++ b/grub-core/normal/main.c
f96e0b
@@ -134,33 +134,37 @@ grub_normal_free_menu (grub_menu_t menu)
f96e0b
   grub_env_unset_menu ();
f96e0b
 }
f96e0b
 
f96e0b
-static grub_menu_t
f96e0b
-read_config_file (const char *config)
f96e0b
+/* Helper for read_config_file.  */
f96e0b
+static grub_err_t
f96e0b
+read_config_file_getline (char **line, int cont __attribute__ ((unused)),
f96e0b
+			  void *data)
f96e0b
 {
f96e0b
-  grub_file_t file;
f96e0b
-  const char *old_file, *old_dir;
f96e0b
-  char *config_dir, *ptr = 0;
f96e0b
+  grub_file_t file = data;
f96e0b
 
f96e0b
-  auto grub_err_t getline (char **line, int cont);
f96e0b
-  grub_err_t getline (char **line, int cont __attribute__ ((unused)))
f96e0b
+  while (1)
f96e0b
     {
f96e0b
-      while (1)
f96e0b
-	{
f96e0b
-	  char *buf;
f96e0b
+      char *buf;
f96e0b
 
f96e0b
-	  *line = buf = grub_file_getline (file);
f96e0b
-	  if (! buf)
f96e0b
-	    return grub_errno;
f96e0b
+      *line = buf = grub_file_getline (file);
f96e0b
+      if (! buf)
f96e0b
+	return grub_errno;
f96e0b
 
f96e0b
-	  if (buf[0] == '#')
f96e0b
-	    grub_free (*line);
f96e0b
-	  else
f96e0b
-	    break;
f96e0b
-	}
f96e0b
-
f96e0b
-      return GRUB_ERR_NONE;
f96e0b
+      if (buf[0] == '#')
f96e0b
+	grub_free (*line);
f96e0b
+      else
f96e0b
+	break;
f96e0b
     }
f96e0b
 
f96e0b
+  return GRUB_ERR_NONE;
f96e0b
+}
f96e0b
+
f96e0b
+static grub_menu_t
f96e0b
+read_config_file (const char *config)
f96e0b
+{
f96e0b
+  grub_file_t file;
f96e0b
+  const char *old_file, *old_dir;
f96e0b
+  char *config_dir, *ptr = 0;
f96e0b
+
f96e0b
   grub_menu_t newmenu;
f96e0b
 
f96e0b
   newmenu = grub_env_get_menu ();
f96e0b
@@ -199,10 +203,10 @@ read_config_file (const char *config)
f96e0b
       grub_print_error ();
f96e0b
       grub_errno = GRUB_ERR_NONE;
f96e0b
 
f96e0b
-      if ((getline (&line, 0)) || (! line))
f96e0b
+      if ((read_config_file_getline (&line, 0, file)) || (! line))
f96e0b
 	break;
f96e0b
 
f96e0b
-      grub_normal_parse_line (line, getline);
f96e0b
+      grub_normal_parse_line (line, read_config_file_getline, file);
f96e0b
       grub_free (line);
f96e0b
     }
f96e0b
 
f96e0b
@@ -427,7 +431,8 @@ grub_normal_read_line_real (char **line, int cont, int nested)
f96e0b
 }
f96e0b
 
f96e0b
 static grub_err_t
f96e0b
-grub_normal_read_line (char **line, int cont)
f96e0b
+grub_normal_read_line (char **line, int cont,
f96e0b
+		       void *data __attribute__ ((unused)))
f96e0b
 {
f96e0b
   return grub_normal_read_line_real (line, cont, 0);
f96e0b
 }
f96e0b
@@ -463,7 +468,7 @@ grub_cmdline_run (int nested)
f96e0b
       if (! line)
f96e0b
 	break;
f96e0b
 
f96e0b
-      grub_normal_parse_line (line, grub_normal_read_line);
f96e0b
+      grub_normal_parse_line (line, grub_normal_read_line, NULL);
f96e0b
       grub_free (line);
f96e0b
     }
f96e0b
 }
f96e0b
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
f96e0b
index b5e6eb0..6619c2e 100644
f96e0b
--- a/grub-core/script/execute.c
f96e0b
+++ b/grub-core/script/execute.c
f96e0b
@@ -783,6 +783,31 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
f96e0b
   return ret;
f96e0b
 }
f96e0b
 
f96e0b
+/* Helper for grub_script_execute_sourcecode.  */
f96e0b
+static grub_err_t
f96e0b
+grub_script_execute_sourcecode_getline (char **line,
f96e0b
+					int cont __attribute__ ((unused)),
f96e0b
+					void *data)
f96e0b
+{
f96e0b
+  const char **source = data;
f96e0b
+  const char *p;
f96e0b
+
f96e0b
+  if (! *source)
f96e0b
+    {
f96e0b
+      *line = 0;
f96e0b
+      return 0;
f96e0b
+    }
f96e0b
+
f96e0b
+  p = grub_strchr (*source, '\n');
f96e0b
+
f96e0b
+  if (p)
f96e0b
+    *line = grub_strndup (*source, p - *source);
f96e0b
+  else
f96e0b
+    *line = grub_strdup (*source);
f96e0b
+  *source = p ? p + 1 : 0;
f96e0b
+  return 0;
f96e0b
+}
f96e0b
+
f96e0b
 /* Execute a source script.  */
f96e0b
 grub_err_t
f96e0b
 grub_script_execute_sourcecode (const char *source, int argc, char **args)
f96e0b
@@ -792,27 +817,6 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
f96e0b
   struct grub_script_scope new_scope;
f96e0b
   struct grub_script_scope *old_scope;
f96e0b
 
f96e0b
-  auto grub_err_t getline (char **line, int cont);
f96e0b
-  grub_err_t getline (char **line, int cont __attribute__ ((unused)))
f96e0b
-  {
f96e0b
-    const char *p;
f96e0b
-
f96e0b
-    if (! source)
f96e0b
-      {
f96e0b
-	*line = 0;
f96e0b
-	return 0;
f96e0b
-      }
f96e0b
-
f96e0b
-    p = grub_strchr (source, '\n');
f96e0b
-
f96e0b
-    if (p)
f96e0b
-      *line = grub_strndup (source, p - source);
f96e0b
-    else
f96e0b
-      *line = grub_strdup (source);
f96e0b
-    source = p ? p + 1 : 0;
f96e0b
-    return 0;
f96e0b
-  }
f96e0b
-
f96e0b
   new_scope.argv.argc = argc;
f96e0b
   new_scope.argv.args = args;
f96e0b
   new_scope.flags = 0;
f96e0b
@@ -824,8 +828,9 @@ grub_script_execute_sourcecode (const char *source, int argc, char **args)
f96e0b
     {
f96e0b
       char *line;
f96e0b
 
f96e0b
-      getline (&line, 0);
f96e0b
-      parsed_script = grub_script_parse (line, getline);
f96e0b
+      grub_script_execute_sourcecode_getline (&line, 0, &source);
f96e0b
+      parsed_script = grub_script_parse
f96e0b
+	(line, grub_script_execute_sourcecode_getline, &source);
f96e0b
       if (! parsed_script)
f96e0b
 	{
f96e0b
 	  ret = grub_errno;
f96e0b
diff --git a/grub-core/script/lexer.c b/grub-core/script/lexer.c
f96e0b
index 396d512..cb9d6b0 100644
f96e0b
--- a/grub-core/script/lexer.c
f96e0b
+++ b/grub-core/script/lexer.c
f96e0b
@@ -147,7 +147,7 @@ grub_script_lexer_yywrap (struct grub_parser_param *parserstate,
f96e0b
 
f96e0b
   line = 0;
f96e0b
   if (! input)
f96e0b
-    lexerstate->getline (&line, 1);
f96e0b
+    lexerstate->getline (&line, 1, lexerstate->getline_data);
f96e0b
   else
f96e0b
     line = grub_strdup (input);
f96e0b
 
f96e0b
@@ -216,7 +216,7 @@ grub_script_lexer_yywrap (struct grub_parser_param *parserstate,
f96e0b
 
f96e0b
 struct grub_lexer_param *
f96e0b
 grub_script_lexer_init (struct grub_parser_param *parser, char *script,
f96e0b
-			grub_reader_getline_t arg_getline)
f96e0b
+			grub_reader_getline_t getline, void *getline_data)
f96e0b
 {
f96e0b
   struct grub_lexer_param *lexerstate;
f96e0b
 
f96e0b
@@ -232,7 +232,10 @@ grub_script_lexer_init (struct grub_parser_param *parser, char *script,
f96e0b
       return 0;
f96e0b
     }
f96e0b
 
f96e0b
-  lexerstate->getline = arg_getline;	/* rest are all zeros already */
f96e0b
+  lexerstate->getline = getline;
f96e0b
+  lexerstate->getline_data = getline_data;
f96e0b
+  /* The other elements of lexerstate are all zeros already.  */
f96e0b
+
f96e0b
   if (yylex_init (&lexerstate->yyscanner))
f96e0b
     {
f96e0b
       grub_free (lexerstate->text);
f96e0b
diff --git a/grub-core/script/main.c b/grub-core/script/main.c
f96e0b
index aa24d16..854a25a 100644
f96e0b
--- a/grub-core/script/main.c
f96e0b
+++ b/grub-core/script/main.c
f96e0b
@@ -22,12 +22,13 @@
f96e0b
 #include <grub/script_sh.h>
f96e0b
 
f96e0b
 grub_err_t
f96e0b
-grub_normal_parse_line (char *line, grub_reader_getline_t getline)
f96e0b
+grub_normal_parse_line (char *line,
f96e0b
+			grub_reader_getline_t getline, void *getline_data)
f96e0b
 {
f96e0b
   struct grub_script *parsed_script;
f96e0b
 
f96e0b
   /* Parse the script.  */
f96e0b
-  parsed_script = grub_script_parse (line, getline);
f96e0b
+  parsed_script = grub_script_parse (line, getline, getline_data);
f96e0b
 
f96e0b
   if (parsed_script)
f96e0b
     {
f96e0b
diff --git a/grub-core/script/script.c b/grub-core/script/script.c
f96e0b
index ad30183..ec4d433 100644
f96e0b
--- a/grub-core/script/script.c
f96e0b
+++ b/grub-core/script/script.c
f96e0b
@@ -340,7 +340,8 @@ grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem)
f96e0b
 /* Parse the script passed in SCRIPT and return the parsed
f96e0b
    datastructure that is ready to be interpreted.  */
f96e0b
 struct grub_script *
f96e0b
-grub_script_parse (char *script, grub_reader_getline_t getline)
f96e0b
+grub_script_parse (char *script,
f96e0b
+		   grub_reader_getline_t getline, void *getline_data)
f96e0b
 {
f96e0b
   struct grub_script *parsed;
f96e0b
   struct grub_script_mem *membackup;
f96e0b
@@ -359,7 +360,8 @@ grub_script_parse (char *script, grub_reader_getline_t getline)
f96e0b
     }
f96e0b
 
f96e0b
   /* Initialize the lexer.  */
f96e0b
-  lexstate = grub_script_lexer_init (parsestate, script, getline);
f96e0b
+  lexstate = grub_script_lexer_init (parsestate, script,
f96e0b
+				     getline, getline_data);
f96e0b
   if (!lexstate)
f96e0b
     {
f96e0b
       grub_free (parsed);
f96e0b
diff --git a/include/grub/parser.h b/include/grub/parser.h
f96e0b
index de4da05..bf9c7c6 100644
f96e0b
--- a/include/grub/parser.h
f96e0b
+++ b/include/grub/parser.h
f96e0b
@@ -63,6 +63,7 @@ EXPORT_FUNC (grub_parser_cmdline_state) (grub_parser_state_t state,
f96e0b
 grub_err_t
f96e0b
 EXPORT_FUNC (grub_parser_split_cmdline) (const char *cmdline,
f96e0b
 					 grub_reader_getline_t getline,
f96e0b
+					 void *getline_data,
f96e0b
 					 int *argc, char ***argv);
f96e0b
 
f96e0b
 struct grub_parser
f96e0b
@@ -79,13 +80,15 @@ struct grub_parser
f96e0b
   /* Clean up the parser.  */
f96e0b
   grub_err_t (*fini) (void);
f96e0b
 
f96e0b
-  grub_err_t (*parse_line) (char *line, grub_reader_getline_t getline);
f96e0b
+  grub_err_t (*parse_line) (char *line,
f96e0b
+			    grub_reader_getline_t getline, void *getline_data);
f96e0b
 };
f96e0b
 typedef struct grub_parser *grub_parser_t;
f96e0b
 
f96e0b
 grub_err_t grub_parser_execute (char *source);
f96e0b
 
f96e0b
 grub_err_t
f96e0b
-grub_rescue_parse_line (char *line, grub_reader_getline_t getline);
f96e0b
+grub_rescue_parse_line (char *line,
f96e0b
+			grub_reader_getline_t getline, void *getline_data);
f96e0b
 
f96e0b
 #endif /* ! GRUB_PARSER_HEADER */
f96e0b
diff --git a/include/grub/reader.h b/include/grub/reader.h
f96e0b
index cd92df8..fd86b20 100644
f96e0b
--- a/include/grub/reader.h
f96e0b
+++ b/include/grub/reader.h
f96e0b
@@ -22,7 +22,7 @@
f96e0b
 
f96e0b
 #include <grub/err.h>
f96e0b
 
f96e0b
-typedef grub_err_t (*grub_reader_getline_t) (char **, int);
f96e0b
+typedef grub_err_t (*grub_reader_getline_t) (char **, int, void *);
f96e0b
 
f96e0b
 void grub_rescue_run (void) __attribute__ ((noreturn));
f96e0b
 
f96e0b
diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h
f96e0b
index d99cbf7..78602e4 100644
f96e0b
--- a/include/grub/script_sh.h
f96e0b
+++ b/include/grub/script_sh.h
f96e0b
@@ -161,6 +161,9 @@ struct grub_lexer_param
f96e0b
      expected, but not available.  */
f96e0b
   grub_reader_getline_t getline;
f96e0b
 
f96e0b
+  /* Caller-supplied data passed to `getline'.  */
f96e0b
+  void *getline_data;
f96e0b
+
f96e0b
   /* A reference counter.  If this is >0 it means that the parser
f96e0b
      expects more tokens and `getline' should be called to fetch more.
f96e0b
      Otherwise the lexer can stop processing if the current buffer is
f96e0b
@@ -287,14 +290,16 @@ grub_script_arg_add (struct grub_parser_param *state,
f96e0b
 		     grub_script_arg_type_t type, char *str);
f96e0b
 
f96e0b
 struct grub_script *grub_script_parse (char *script,
f96e0b
-				       grub_reader_getline_t getline);
f96e0b
+				       grub_reader_getline_t getline,
f96e0b
+				       void *getline_data);
f96e0b
 void grub_script_free (struct grub_script *script);
f96e0b
 struct grub_script *grub_script_create (struct grub_script_cmd *cmd,
f96e0b
 					struct grub_script_mem *mem);
f96e0b
 
f96e0b
 struct grub_lexer_param *grub_script_lexer_init (struct grub_parser_param *parser,
f96e0b
 						 char *script,
f96e0b
-						 grub_reader_getline_t getline);
f96e0b
+						 grub_reader_getline_t getline,
f96e0b
+						 void *getline_data);
f96e0b
 void grub_script_lexer_fini (struct grub_lexer_param *);
f96e0b
 void grub_script_lexer_ref (struct grub_lexer_param *);
f96e0b
 void grub_script_lexer_deref (struct grub_lexer_param *);
f96e0b
@@ -380,7 +385,8 @@ char **
f96e0b
 grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count);
f96e0b
 
f96e0b
 grub_err_t
f96e0b
-grub_normal_parse_line (char *line, grub_reader_getline_t getline);
f96e0b
+grub_normal_parse_line (char *line,
f96e0b
+			grub_reader_getline_t getline, void *getline_data);
f96e0b
 
f96e0b
 static inline struct grub_script *
f96e0b
 grub_script_ref (struct grub_script *script)
f96e0b
diff --git a/util/grub-script-check.c b/util/grub-script-check.c
f96e0b
index 73d51f0..203a3ff 100644
f96e0b
--- a/util/grub-script-check.c
f96e0b
+++ b/util/grub-script-check.c
f96e0b
@@ -85,81 +85,92 @@ static struct argp argp = {
f96e0b
   NULL, NULL, NULL
f96e0b
 };
f96e0b
 
f96e0b
+/* Context for main.  */
f96e0b
+struct main_ctx
f96e0b
+{
f96e0b
+  int lineno;
f96e0b
+  FILE *file;
f96e0b
+  struct arguments arguments;
f96e0b
+};
f96e0b
+
f96e0b
+/* Helper for main.  */
f96e0b
+static grub_err_t
f96e0b
+get_config_line (char **line, int cont __attribute__ ((unused)), void *data)
f96e0b
+{
f96e0b
+  struct main_ctx *ctx = data;
f96e0b
+  int i;
f96e0b
+  char *cmdline = 0;
f96e0b
+  size_t len = 0;
f96e0b
+  ssize_t curread;
f96e0b
+
f96e0b
+  curread = getline (&cmdline, &len, (ctx->file ?: stdin));
f96e0b
+  if (curread == -1)
f96e0b
+    {
f96e0b
+      *line = 0;
f96e0b
+      grub_errno = GRUB_ERR_READ_ERROR;
f96e0b
+
f96e0b
+      if (cmdline)
f96e0b
+	free (cmdline);
f96e0b
+      return grub_errno;
f96e0b
+    }
f96e0b
+
f96e0b
+  if (ctx->arguments.verbose)
f96e0b
+    grub_printf ("%s", cmdline);
f96e0b
+
f96e0b
+  for (i = 0; cmdline[i] != '\0'; i++)
f96e0b
+    {
f96e0b
+      /* Replace tabs and carriage returns with spaces.  */
f96e0b
+      if (cmdline[i] == '\t' || cmdline[i] == '\r')
f96e0b
+	cmdline[i] = ' ';
f96e0b
+
f96e0b
+      /* Replace '\n' with '\0'.  */
f96e0b
+      if (cmdline[i] == '\n')
f96e0b
+	cmdline[i] = '\0';
f96e0b
+    }
f96e0b
+
f96e0b
+  ctx->lineno++;
f96e0b
+  *line = grub_strdup (cmdline);
f96e0b
+
f96e0b
+  free (cmdline);
f96e0b
+  return 0;
f96e0b
+}
f96e0b
+
f96e0b
 int
f96e0b
 main (int argc, char *argv[])
f96e0b
 {
f96e0b
+  struct main_ctx ctx = {
f96e0b
+    .lineno = 0,
f96e0b
+    .file = 0
f96e0b
+  };
f96e0b
   char *input;
f96e0b
-  int lineno = 0;
f96e0b
-  FILE *file = 0;
f96e0b
-  struct arguments arguments;
f96e0b
   int found_input = 0;
f96e0b
   struct grub_script *script = NULL;
f96e0b
 
f96e0b
-  auto grub_err_t get_config_line (char **line, int cont);
f96e0b
-  grub_err_t get_config_line (char **line, int cont __attribute__ ((unused)))
f96e0b
-  {
f96e0b
-    int i;
f96e0b
-    char *cmdline = 0;
f96e0b
-    size_t len = 0;
f96e0b
-    ssize_t curread;
f96e0b
-
f96e0b
-    curread = getline(&cmdline, &len, (file ?: stdin));
f96e0b
-    if (curread == -1)
f96e0b
-      {
f96e0b
-	*line = 0;
f96e0b
-	grub_errno = GRUB_ERR_READ_ERROR;
f96e0b
-
f96e0b
-	if (cmdline)
f96e0b
-	  free (cmdline);
f96e0b
-	return grub_errno;
f96e0b
-      }
f96e0b
-
f96e0b
-    if (arguments.verbose)
f96e0b
-      grub_printf("%s", cmdline);
f96e0b
-
f96e0b
-    for (i = 0; cmdline[i] != '\0'; i++)
f96e0b
-      {
f96e0b
-	/* Replace tabs and carriage returns with spaces.  */
f96e0b
-	if (cmdline[i] == '\t' || cmdline[i] == '\r')
f96e0b
-	  cmdline[i] = ' ';
f96e0b
-
f96e0b
-	/* Replace '\n' with '\0'.  */
f96e0b
-	if (cmdline[i] == '\n')
f96e0b
-	  cmdline[i] = '\0';
f96e0b
-      }
f96e0b
-
f96e0b
-    lineno++;
f96e0b
-    *line = grub_strdup (cmdline);
f96e0b
-
f96e0b
-    free (cmdline);
f96e0b
-    return 0;
f96e0b
-  }
f96e0b
-
f96e0b
   set_program_name (argv[0]);
f96e0b
   grub_util_init_nls ();
f96e0b
 
f96e0b
-  memset (&arguments, 0, sizeof (struct arguments));
f96e0b
+  memset (&ctx.arguments, 0, sizeof (struct arguments));
f96e0b
 
f96e0b
   /* Check for options.  */
f96e0b
-  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
f96e0b
+  if (argp_parse (&argp, argc, argv, 0, 0, &ctx.arguments) != 0)
f96e0b
     {
f96e0b
       fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
f96e0b
       exit(1);
f96e0b
     }
f96e0b
 
f96e0b
   /* Obtain ARGUMENT.  */
f96e0b
-  if (!arguments.filename)
f96e0b
+  if (!ctx.arguments.filename)
f96e0b
     {
f96e0b
-      file = 0; /* read from stdin */
f96e0b
+      ctx.file = 0; /* read from stdin */
f96e0b
     }
f96e0b
   else
f96e0b
     {
f96e0b
-      file = fopen (arguments.filename, "r");
f96e0b
-      if (! file)
f96e0b
+      ctx.file = fopen (ctx.arguments.filename, "r");
f96e0b
+      if (! ctx.file)
f96e0b
 	{
f96e0b
           char *program = xstrdup(program_name);
f96e0b
 	  fprintf (stderr, "%s: %s: %s\n", program_name, 
f96e0b
-		   arguments.filename, strerror(errno));
f96e0b
+		   ctx.arguments.filename, strerror (errno));
f96e0b
           argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
f96e0b
           free(program);
f96e0b
           exit(1);
f96e0b
@@ -169,12 +180,12 @@ main (int argc, char *argv[])
f96e0b
   do
f96e0b
     {
f96e0b
       input = 0;
f96e0b
-      get_config_line(&input, 0);
f96e0b
+      get_config_line (&input, 0, &ctx;;
f96e0b
       if (! input) 
f96e0b
 	break;
f96e0b
       found_input = 1;
f96e0b
 
f96e0b
-      script = grub_script_parse (input, get_config_line);
f96e0b
+      script = grub_script_parse (input, get_config_line, &ctx;;
f96e0b
       if (script)
f96e0b
 	{
f96e0b
 	  grub_script_execute (script);
f96e0b
@@ -184,11 +195,11 @@ main (int argc, char *argv[])
f96e0b
       grub_free (input);
f96e0b
     } while (script != 0);
f96e0b
 
f96e0b
-  if (file) fclose (file);
f96e0b
+  if (ctx.file) fclose (ctx.file);
f96e0b
 
f96e0b
   if (found_input && script == 0)
f96e0b
     {
f96e0b
-      fprintf (stderr, _("Syntax error at line %u\n"), lineno);
f96e0b
+      fprintf (stderr, _("Syntax error at line %u\n"), ctx.lineno);
f96e0b
       return 1;
f96e0b
     }
f96e0b
 
f96e0b
-- 
f96e0b
1.8.2.1
f96e0b