a5bd9f6
From 5a04b68d6ab44ac2da3dee3d0d36747156c21238 Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Sat, 2 Mar 2013 12:17:52 +0100
a5bd9f6
Subject: [PATCH 182/364] 	* grub-core/script/execute.c (gettext_append):
a5bd9f6
 Remove nested functions.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                  |   9 +++
a5bd9f6
 grub-core/script/execute.c | 180 ++++++++++++++++++++++++---------------------
a5bd9f6
 2 files changed, 107 insertions(+), 82 deletions(-)
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index ddd903a..07318d8 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,5 +1,14 @@
a5bd9f6
 2013-03-02  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
+	* grub-core/script/execute.c (gettext_append): Remove nested functions.
a5bd9f6
+
a5bd9f6
+2013-03-02  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
+	* grub-core/normal/charset.c (grub_bidi_logical_to_visual): Add
a5bd9f6
+	hook pass-through parameter. All users updated and unnested.
a5bd9f6
+
a5bd9f6
+2013-03-02  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
 	* grub-core/commands/loadenv.c (grub_cmd_list_env): Move print_var
a5bd9f6
 	out of its parent.
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
a5bd9f6
index 6619c2e..d6a2c78 100644
a5bd9f6
--- a/grub-core/script/execute.c
a5bd9f6
+++ b/grub-core/script/execute.c
a5bd9f6
@@ -372,10 +372,19 @@ grub_script_env_set (const char *name, const char *val)
a5bd9f6
   return grub_env_set (name, val);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
+struct gettext_context
a5bd9f6
+{
a5bd9f6
+  char **allowed_strings;
a5bd9f6
+  grub_size_t nallowed_strings;
a5bd9f6
+  grub_size_t additional_len;
a5bd9f6
+};
a5bd9f6
+
a5bd9f6
 static int
a5bd9f6
 parse_string (const char *str,
a5bd9f6
-	      int (*hook) (const char *var, grub_size_t varlen),
a5bd9f6
-	      char **put)
a5bd9f6
+	      int (*hook) (const char *var, grub_size_t varlen,
a5bd9f6
+			   char **ptr, struct gettext_context *ctx),
a5bd9f6
+	      struct gettext_context *ctx,
a5bd9f6
+	      char *put)
a5bd9f6
 {
a5bd9f6
   const char *ptr;
a5bd9f6
   int escaped = 0;
a5bd9f6
@@ -387,7 +396,7 @@ parse_string (const char *str,
a5bd9f6
       case '\\':
a5bd9f6
 	escaped = !escaped;
a5bd9f6
 	if (!escaped && put)
a5bd9f6
-	  *((*put)++) = '\\';
a5bd9f6
+	  *(put++) = '\\';
a5bd9f6
 	ptr++;
a5bd9f6
 	break;
a5bd9f6
       case '$':
a5bd9f6
@@ -395,7 +404,7 @@ parse_string (const char *str,
a5bd9f6
 	  {
a5bd9f6
 	    escaped = 0;
a5bd9f6
 	    if (put)
a5bd9f6
-	      *((*put)++) = *ptr;
a5bd9f6
+	      *(put++) = *ptr;
a5bd9f6
 	    ptr++;
a5bd9f6
 	    break;
a5bd9f6
 	  }
a5bd9f6
@@ -409,7 +418,7 @@ parse_string (const char *str,
a5bd9f6
 	      ptr = grub_strchr (optr, '}');
a5bd9f6
 	      if (!ptr)
a5bd9f6
 		break;
a5bd9f6
-	      if (hook (optr, ptr - optr))
a5bd9f6
+	      if (hook (optr, ptr - optr, &put, ctx))
a5bd9f6
 		return 1;
a5bd9f6
 	      ptr++;
a5bd9f6
 	      break;
a5bd9f6
@@ -418,7 +427,7 @@ parse_string (const char *str,
a5bd9f6
 	    optr = ptr;
a5bd9f6
 	    while (*ptr >= '0' && *ptr <= '9')
a5bd9f6
 	      ptr++;
a5bd9f6
-	    if (hook (optr, ptr - optr))
a5bd9f6
+	    if (hook (optr, ptr - optr, &put, ctx))
a5bd9f6
 	      return 1;
a5bd9f6
 	    break;
a5bd9f6
 	  case 'a' ... 'z':
a5bd9f6
@@ -430,129 +439,136 @@ parse_string (const char *str,
a5bd9f6
 		   || (*ptr >= 'A' && *ptr <= 'Z')
a5bd9f6
 		   || *ptr == '_')
a5bd9f6
 	      ptr++;
a5bd9f6
-	    if (hook (optr, ptr - optr))
a5bd9f6
+	    if (hook (optr, ptr - optr, &put, ctx))
a5bd9f6
 	      return 1;
a5bd9f6
 	    break;
a5bd9f6
 	  case '?':
a5bd9f6
 	  case '#':
a5bd9f6
-	    if (hook (ptr, 1))
a5bd9f6
+	    if (hook (ptr, 1, &put, ctx))
a5bd9f6
 	      return 1;
a5bd9f6
 	    ptr++;
a5bd9f6
 	    break;
a5bd9f6
 	  default:
a5bd9f6
 	    if (put)
a5bd9f6
-	      *((*put)++) = '$';
a5bd9f6
+	      *(put++) = '$';
a5bd9f6
 	  }
a5bd9f6
 	break;
a5bd9f6
       default:
a5bd9f6
 	if (escaped && put)
a5bd9f6
-	  *((*put)++) = '\\';
a5bd9f6
+	  *(put++) = '\\';
a5bd9f6
 	escaped = 0;
a5bd9f6
 	if (put)
a5bd9f6
-	  *((*put)++) = *ptr;
a5bd9f6
+	  *(put++) = *ptr;
a5bd9f6
 	ptr++;
a5bd9f6
 	break;
a5bd9f6
       }
a5bd9f6
+  if (put)
a5bd9f6
+    *(put++) = 0;
a5bd9f6
   return 0;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 static int
a5bd9f6
-gettext_append (struct grub_script_argv *result, const char *orig_str)
a5bd9f6
+gettext_putvar (const char *str, grub_size_t len,
a5bd9f6
+		char **ptr, struct gettext_context *ctx)
a5bd9f6
 {
a5bd9f6
-  const char *template;
a5bd9f6
-  char *res = 0, *ptr;
a5bd9f6
-  char **allowed_strings;
a5bd9f6
-  grub_size_t nallowed_strings = 0;
a5bd9f6
-  grub_size_t additional_len = 1;
a5bd9f6
-  int rval = 1;
a5bd9f6
-  const char *iptr;
a5bd9f6
+  const char *var;
a5bd9f6
+  grub_size_t i;
a5bd9f6
 
a5bd9f6
-  auto int save_allow (const char *str, grub_size_t len);
a5bd9f6
-  int save_allow (const char *str, grub_size_t len)
a5bd9f6
-  {
a5bd9f6
-    allowed_strings[nallowed_strings++] = grub_strndup (str, len);
a5bd9f6
-    if (!allowed_strings[nallowed_strings - 1])
a5bd9f6
-      return 1;
a5bd9f6
+  for (i = 0; i < ctx->nallowed_strings; i++)
a5bd9f6
+    if (grub_strncmp (ctx->allowed_strings[i], str, len) == 0
a5bd9f6
+	&& ctx->allowed_strings[i][len] == 0)
a5bd9f6
+      {
a5bd9f6
+	break;
a5bd9f6
+      }
a5bd9f6
+  if (i == ctx->nallowed_strings)
a5bd9f6
     return 0;
a5bd9f6
-  }
a5bd9f6
-
a5bd9f6
-  auto int getlen (const char *str, grub_size_t len);
a5bd9f6
-  int getlen (const char *str, grub_size_t len)
a5bd9f6
-  {
a5bd9f6
-    const char *var;
a5bd9f6
-    grub_size_t i;
a5bd9f6
 
a5bd9f6
-    for (i = 0; i < nallowed_strings; i++)
a5bd9f6
-      if (grub_strncmp (allowed_strings[i], str, len) == 0
a5bd9f6
-	  && allowed_strings[i][len] == 0)
a5bd9f6
-	break;
a5bd9f6
-    if (i == nallowed_strings)
a5bd9f6
+  /* Enough for any number.  */
a5bd9f6
+  if (len == 1 && str[0] == '#')
a5bd9f6
+    {
a5bd9f6
+      grub_snprintf (*ptr, 30, "%u", scope->argv.argc);
a5bd9f6
+      *ptr += grub_strlen (*ptr);
a5bd9f6
       return 0;
a5bd9f6
+    }
a5bd9f6
+  var = grub_env_get (ctx->allowed_strings[i]);
a5bd9f6
+  if (var)
a5bd9f6
+    *ptr = grub_stpcpy (*ptr, var);
a5bd9f6
+  return 0;
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
-    /* Enough for any number.  */
a5bd9f6
-    if (len == 1 && str[0] == '#')
a5bd9f6
-      {
a5bd9f6
-	additional_len += 30;
a5bd9f6
-	return 0;
a5bd9f6
-      }
a5bd9f6
-    var = grub_env_get (allowed_strings[i]);
a5bd9f6
-    if (var)
a5bd9f6
-      additional_len += grub_strlen (var);
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
+static int
a5bd9f6
+gettext_save_allow (const char *str, grub_size_t len,
a5bd9f6
+		    char **ptr __attribute__ ((unused)),
a5bd9f6
+		    struct gettext_context *ctx)
a5bd9f6
+{
a5bd9f6
+  ctx->allowed_strings[ctx->nallowed_strings++] = grub_strndup (str, len);
a5bd9f6
+  if (!ctx->allowed_strings[ctx->nallowed_strings - 1])
a5bd9f6
+    return 1;
a5bd9f6
+  return 0;
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
-  auto int putvar (const char *str, grub_size_t len);
a5bd9f6
-  int putvar (const char *str, grub_size_t len)
a5bd9f6
-  {
a5bd9f6
-    const char *var;
a5bd9f6
-    grub_size_t i;
a5bd9f6
+static int
a5bd9f6
+gettext_getlen (const char *str, grub_size_t len,
a5bd9f6
+		char **ptr __attribute__ ((unused)),
a5bd9f6
+		struct gettext_context *ctx)
a5bd9f6
+{
a5bd9f6
+  const char *var;
a5bd9f6
+  grub_size_t i;
a5bd9f6
 
a5bd9f6
-    for (i = 0; i < nallowed_strings; i++)
a5bd9f6
-      if (grub_strncmp (allowed_strings[i], str, len) == 0
a5bd9f6
-	  && allowed_strings[i][len] == 0)
a5bd9f6
-       	{
a5bd9f6
-	  break;
a5bd9f6
-	}
a5bd9f6
-    if (i == nallowed_strings)
a5bd9f6
+  for (i = 0; i < ctx->nallowed_strings; i++)
a5bd9f6
+    if (grub_strncmp (ctx->allowed_strings[i], str, len) == 0
a5bd9f6
+	&& ctx->allowed_strings[i][len] == 0)
a5bd9f6
+      break;
a5bd9f6
+  if (i == ctx->nallowed_strings)
a5bd9f6
+    return 0;
a5bd9f6
+
a5bd9f6
+  /* Enough for any number.  */
a5bd9f6
+  if (len == 1 && str[0] == '#')
a5bd9f6
+    {
a5bd9f6
+      ctx->additional_len += 30;
a5bd9f6
       return 0;
a5bd9f6
+    }
a5bd9f6
+  var = grub_env_get (ctx->allowed_strings[i]);
a5bd9f6
+  if (var)
a5bd9f6
+    ctx->additional_len += grub_strlen (var);
a5bd9f6
+  return 0;
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
-    /* Enough for any number.  */
a5bd9f6
-    if (len == 1 && str[0] == '#')
a5bd9f6
-      {
a5bd9f6
-	grub_snprintf (ptr, 30, "%u", scope->argv.argc);
a5bd9f6
-	ptr += grub_strlen (ptr);
a5bd9f6
-	return 0;
a5bd9f6
-      }
a5bd9f6
-    var = grub_env_get (allowed_strings[i]);
a5bd9f6
-    if (var)
a5bd9f6
-      ptr = grub_stpcpy (ptr, var);
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
+static int
a5bd9f6
+gettext_append (struct grub_script_argv *result, const char *orig_str)
a5bd9f6
+{
a5bd9f6
+  const char *template;
a5bd9f6
+  char *res = 0;
a5bd9f6
+  struct gettext_context ctx = {
a5bd9f6
+    .allowed_strings = 0,
a5bd9f6
+    .nallowed_strings = 0,
a5bd9f6
+    .additional_len = 1
a5bd9f6
+  };
a5bd9f6
+  int rval = 1;
a5bd9f6
+  const char *iptr;
a5bd9f6
 
a5bd9f6
   grub_size_t dollar_cnt = 0;
a5bd9f6
 
a5bd9f6
   for (iptr = orig_str; *iptr; iptr++)
a5bd9f6
     if (*iptr == '$')
a5bd9f6
       dollar_cnt++;
a5bd9f6
-  allowed_strings = grub_malloc (sizeof (allowed_strings[0]) * dollar_cnt);
a5bd9f6
+  ctx.allowed_strings = grub_malloc (sizeof (ctx.allowed_strings[0]) * dollar_cnt);
a5bd9f6
 
a5bd9f6
-  if (parse_string (orig_str, save_allow, 0))
a5bd9f6
+  if (parse_string (orig_str, gettext_save_allow, &ctx, 0))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
   template = _(orig_str);
a5bd9f6
 
a5bd9f6
-  if (parse_string (template, getlen, 0))
a5bd9f6
+  if (parse_string (template, gettext_getlen, &ctx, 0))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  res = grub_malloc (grub_strlen (template) + additional_len);
a5bd9f6
+  res = grub_malloc (grub_strlen (template) + ctx.additional_len);
a5bd9f6
   if (!res)
a5bd9f6
     goto fail;
a5bd9f6
-  ptr = res;
a5bd9f6
 
a5bd9f6
-  if (parse_string (template, putvar, &ptr))
a5bd9f6
+  if (parse_string (template, gettext_putvar, &ctx, res))
a5bd9f6
     goto fail;
a5bd9f6
 
a5bd9f6
-  *ptr = 0;
a5bd9f6
   char *escaped = 0;
a5bd9f6
   escaped = wildcard_escape (res);
a5bd9f6
   if (grub_script_argv_append (result, escaped, grub_strlen (escaped)))
a5bd9f6
@@ -567,10 +583,10 @@ gettext_append (struct grub_script_argv *result, const char *orig_str)
a5bd9f6
   grub_free (res);
a5bd9f6
   {
a5bd9f6
     grub_size_t i;
a5bd9f6
-    for (i = 0; i < nallowed_strings; i++)
a5bd9f6
-      grub_free (allowed_strings[i]);
a5bd9f6
+    for (i = 0; i < ctx.nallowed_strings; i++)
a5bd9f6
+      grub_free (ctx.allowed_strings[i]);
a5bd9f6
   }
a5bd9f6
-  grub_free (allowed_strings);
a5bd9f6
+  grub_free (ctx.allowed_strings);
a5bd9f6
   return rval;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6