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