a5bd9f6
From 4c12073435b0bc85e373bd74bb8091ccc538df6b Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Thu, 28 Feb 2013 09:36:55 +0100
a5bd9f6
Subject: [PATCH 163/364] 	* util/grub-fstest.c: Remove nested functions.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog          |   4 ++
a5bd9f6
 util/grub-fstest.c | 166 ++++++++++++++++++++++++++++-------------------------
a5bd9f6
 2 files changed, 93 insertions(+), 77 deletions(-)
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index bb02830..06123b6 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,3 +1,7 @@
a5bd9f6
+2013-02-28  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
+	* util/grub-fstest.c: Remove nested functions.
a5bd9f6
+
a5bd9f6
 2013-02-27  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
 	* grub-core/loader/machoXX.c: Remove nested functions.
a5bd9f6
diff --git a/util/grub-fstest.c b/util/grub-fstest.c
a5bd9f6
index a546b75..253dee8 100644
a5bd9f6
--- a/util/grub-fstest.c
a5bd9f6
+++ b/util/grub-fstest.c
a5bd9f6
@@ -79,7 +79,7 @@ static grub_disk_addr_t skip, leng;
a5bd9f6
 static int uncompress = 0;
a5bd9f6
 
a5bd9f6
 static void
a5bd9f6
-read_file (char *pathname, int (*hook) (grub_off_t ofs, char *buf, int len))
a5bd9f6
+read_file (char *pathname, int (*hook) (grub_off_t ofs, char *buf, int len, void *hook_arg), void *hook_arg)
a5bd9f6
 {
a5bd9f6
   static char buf[BUF_SIZE];
a5bd9f6
   grub_file_t file;
a5bd9f6
@@ -108,7 +108,7 @@ read_file (char *pathname, int (*hook) (grub_off_t ofs, char *buf, int len))
a5bd9f6
             grub_util_error (_("disk read fails at offset %lld, length %d"),
a5bd9f6
                              skip, len);
a5bd9f6
 
a5bd9f6
-          if (hook (skip, buf, len))
a5bd9f6
+          if (hook (skip, buf, len, hook_arg))
a5bd9f6
             break;
a5bd9f6
 
a5bd9f6
           skip += len;
a5bd9f6
@@ -158,7 +158,7 @@ read_file (char *pathname, int (*hook) (grub_off_t ofs, char *buf, int len))
a5bd9f6
 	    break;
a5bd9f6
 	  }
a5bd9f6
 
a5bd9f6
-	if ((sz == 0) || (hook (ofs, buf, sz)))
a5bd9f6
+	if ((sz == 0) || (hook (ofs, buf, sz, hook_arg)))
a5bd9f6
 	  break;
a5bd9f6
 
a5bd9f6
 	ofs += sz;
a5bd9f6
@@ -169,87 +169,99 @@ read_file (char *pathname, int (*hook) (grub_off_t ofs, char *buf, int len))
a5bd9f6
   grub_file_close (file);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
-static void
a5bd9f6
-cmd_cp (char *src, char *dest)
a5bd9f6
+struct cp_hook_ctx
a5bd9f6
 {
a5bd9f6
   FILE *ff;
a5bd9f6
+  const char *dest;
a5bd9f6
+};
a5bd9f6
 
a5bd9f6
-  auto int cp_hook (grub_off_t ofs, char *buf, int len);
a5bd9f6
-  int cp_hook (grub_off_t ofs, char *buf, int len)
a5bd9f6
-  {
a5bd9f6
-    (void) ofs;
a5bd9f6
+static int
a5bd9f6
+cp_hook (grub_off_t ofs, char *buf, int len, void *_ctx)
a5bd9f6
+{
a5bd9f6
+  struct cp_hook_ctx *ctx = _ctx;
a5bd9f6
+  (void) ofs;
a5bd9f6
 
a5bd9f6
-    if ((int) fwrite (buf, 1, len, ff) != len)
a5bd9f6
-      {
a5bd9f6
-	grub_util_error (_("cannot write to `%s': %s"),
a5bd9f6
-			 dest, strerror (errno));
a5bd9f6
-	return 1;
a5bd9f6
-      }
a5bd9f6
+  if ((int) fwrite (buf, 1, len, ctx->ff) != len)
a5bd9f6
+    {
a5bd9f6
+      grub_util_error (_("cannot write to `%s': %s"),
a5bd9f6
+		       ctx->dest, strerror (errno));
a5bd9f6
+      return 1;
a5bd9f6
+    }
a5bd9f6
 
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
+  return 0;
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
-  ff = fopen (dest, "wb");
a5bd9f6
-  if (ff == NULL)
a5bd9f6
+static void
a5bd9f6
+cmd_cp (char *src, const char *dest)
a5bd9f6
+{
a5bd9f6
+  struct cp_hook_ctx ctx = 
a5bd9f6
+    {
a5bd9f6
+      .dest = dest
a5bd9f6
+    };
a5bd9f6
+
a5bd9f6
+  ctx.ff = fopen (dest, "wb");
a5bd9f6
+  if (ctx.ff == NULL)
a5bd9f6
     {
a5bd9f6
       grub_util_error (_("cannot open OS file `%s': %s"), dest,
a5bd9f6
 		       strerror (errno));
a5bd9f6
       return;
a5bd9f6
     }
a5bd9f6
-  read_file (src, cp_hook);
a5bd9f6
-  fclose (ff);
a5bd9f6
+  read_file (src, cp_hook, &ctx;;
a5bd9f6
+  fclose (ctx.ff);
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
+static int
a5bd9f6
+cat_hook (grub_off_t ofs, char *buf, int len, void *_arg __attribute__ ((unused)))
a5bd9f6
+{
a5bd9f6
+  (void) ofs;
a5bd9f6
+
a5bd9f6
+  if ((int) fwrite (buf, 1, len, stdout) != len)
a5bd9f6
+    {
a5bd9f6
+      grub_util_error (_("cannot write to the stdout: %s"),
a5bd9f6
+		       strerror (errno));
a5bd9f6
+      return 1;
a5bd9f6
+    }
a5bd9f6
+
a5bd9f6
+  return 0;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 static void
a5bd9f6
 cmd_cat (char *src)
a5bd9f6
 {
a5bd9f6
-  auto int cat_hook (grub_off_t ofs, char *buf, int len);
a5bd9f6
-  int cat_hook (grub_off_t ofs, char *buf, int len)
a5bd9f6
-  {
a5bd9f6
-    (void) ofs;
a5bd9f6
+  read_file (src, cat_hook, 0);
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
-    if ((int) fwrite (buf, 1, len, stdout) != len)
a5bd9f6
-      {
a5bd9f6
-	grub_util_error (_("cannot write to the stdout: %s"),
a5bd9f6
-			 strerror (errno));
a5bd9f6
-	return 1;
a5bd9f6
-      }
a5bd9f6
+static int
a5bd9f6
+cmp_hook (grub_off_t ofs, char *buf, int len, void *ff_in)
a5bd9f6
+{
a5bd9f6
+  FILE *ff = ff_in;
a5bd9f6
+  static char buf_1[BUF_SIZE];
a5bd9f6
+  if ((int) fread (buf_1, 1, len, ff) != len)
a5bd9f6
+    {
a5bd9f6
+      grub_util_error (_("read error at offset %llu: %s"), ofs,
a5bd9f6
+		       grub_errmsg);
a5bd9f6
+      return 1;
a5bd9f6
+    }
a5bd9f6
 
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
+  if (grub_memcmp (buf, buf_1, len) != 0)
a5bd9f6
+    {
a5bd9f6
+      int i;
a5bd9f6
 
a5bd9f6
-  read_file (src, cat_hook);
a5bd9f6
+      for (i = 0; i < len; i++, ofs++)
a5bd9f6
+	if (buf_1[i] != buf[i])
a5bd9f6
+	  {
a5bd9f6
+	    grub_util_error (_("compare fail at offset %llu"), ofs);
a5bd9f6
+	    return 1;
a5bd9f6
+	  }
a5bd9f6
+    }
a5bd9f6
+  return 0;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
+
a5bd9f6
 static void
a5bd9f6
 cmd_cmp (char *src, char *dest)
a5bd9f6
 {
a5bd9f6
   FILE *ff;
a5bd9f6
-  static char buf_1[BUF_SIZE];
a5bd9f6
-
a5bd9f6
-  auto int cmp_hook (grub_off_t ofs, char *buf, int len);
a5bd9f6
-  int cmp_hook (grub_off_t ofs, char *buf, int len)
a5bd9f6
-  {
a5bd9f6
-    if ((int) fread (buf_1, 1, len, ff) != len)
a5bd9f6
-      {
a5bd9f6
-	grub_util_error (_("read error at offset %llu: %s"), ofs,
a5bd9f6
-			 grub_errmsg);
a5bd9f6
-	return 1;
a5bd9f6
-      }
a5bd9f6
-
a5bd9f6
-    if (grub_memcmp (buf, buf_1, len))
a5bd9f6
-      {
a5bd9f6
-	int i;
a5bd9f6
-
a5bd9f6
-	for (i = 0; i < len; i++, ofs++)
a5bd9f6
-	  if (buf_1[i] != buf[i])
a5bd9f6
-	    {
a5bd9f6
-	      grub_util_error (_("compare fail at offset %llu"), ofs);
a5bd9f6
-	      return 1;
a5bd9f6
-	    }
a5bd9f6
-      }
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
 
a5bd9f6
   struct stat st;
a5bd9f6
   if (stat (dest, &st) == -1)
a5bd9f6
@@ -306,7 +318,7 @@ cmd_cmp (char *src, char *dest)
a5bd9f6
     grub_util_error (_("cannot seek `%s': %s"), dest,
a5bd9f6
 		     strerror (errno));
a5bd9f6
 
a5bd9f6
-  read_file (src, cmp_hook);
a5bd9f6
+  read_file (src, cmp_hook, ff);
a5bd9f6
 
a5bd9f6
   {
a5bd9f6
     grub_uint64_t pre;
a5bd9f6
@@ -318,17 +330,26 @@ cmd_cmp (char *src, char *dest)
a5bd9f6
   fclose (ff);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
+static int
a5bd9f6
+hex_hook (grub_off_t ofs, char *buf, int len, void *arg __attribute__ ((unused)))
a5bd9f6
+{
a5bd9f6
+  hexdump (ofs, buf, len);
a5bd9f6
+  return 0;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
 static void
a5bd9f6
 cmd_hex (char *pathname)
a5bd9f6
 {
a5bd9f6
-  auto int hex_hook (grub_off_t ofs, char *buf, int len);
a5bd9f6
-  int hex_hook (grub_off_t ofs, char *buf, int len)
a5bd9f6
-  {
a5bd9f6
-    hexdump (ofs, buf, len);
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
+  read_file (pathname, hex_hook, 0);
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
-  read_file (pathname, hex_hook);
a5bd9f6
+static int
a5bd9f6
+crc_hook (grub_off_t ofs, char *buf, int len, void *crc_ctx)
a5bd9f6
+{
a5bd9f6
+  (void) ofs;
a5bd9f6
+  
a5bd9f6
+  GRUB_MD_CRC32->write(crc_ctx, buf, len);
a5bd9f6
+  return 0;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 static void
a5bd9f6
@@ -337,16 +358,7 @@ cmd_crc (char *pathname)
a5bd9f6
   grub_uint8_t crc32_context[GRUB_MD_CRC32->contextsize];
a5bd9f6
   GRUB_MD_CRC32->init(crc32_context);
a5bd9f6
 
a5bd9f6
-  auto int crc_hook (grub_off_t ofs, char *buf, int len);
a5bd9f6
-  int crc_hook (grub_off_t ofs, char *buf, int len)
a5bd9f6
-  {
a5bd9f6
-    (void) ofs;
a5bd9f6
-
a5bd9f6
-    GRUB_MD_CRC32->write(crc32_context, buf, len);
a5bd9f6
-    return 0;
a5bd9f6
-  }
a5bd9f6
-
a5bd9f6
-  read_file (pathname, crc_hook);
a5bd9f6
+  read_file (pathname, crc_hook, crc32_context);
a5bd9f6
   GRUB_MD_CRC32->final(crc32_context);
a5bd9f6
   printf ("%08x\n",
a5bd9f6
 	  grub_be_to_cpu32 (grub_get_unaligned32 (GRUB_MD_CRC32->read (crc32_context))));
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6