Skip to contentSkip to conversation

grub-install: locale depends on nls

PATCH Superseded Olaf Hering 2 participants 4 messages Mar 6, 2018 – Mar 8, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 grub-install: locale depends on nls

With --disable-nls no locales exist.

Avoid runtime error by moving code that copies locales into its own function. Return early in case nls was disabled. That way the compiler will throw away unreachable code, no need to put preprocessor conditionals everywhere to avoid warnings about unused code.

Fix memleak by freeing dstf.
Convert tabs to spaces in moved code.

---
 util/grub-install-common.c | 107 ++++++++++++++++++++++++---------------------
 1 file changed, 58 insertions(+), 49 deletions(-)
util/grub-install-common.c +58 -49
index 9e3e358c9..56197fa1d 100644
@@ -725,13 +725,68 @@ grub_install_get_platform_platform (enum grub_install_plat platid)
   return platforms[platid].platform;
 }
 
+static void
+grub_install_copy_nls(const char *src, const char *dst)
+{
+  char *dst_locale;
+
+#if !(defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
+  /* No locales exist without NLS. */
+  return;
+#endif
+
+  dst_locale = grub_util_path_concat (2, dst, "locale");
+  grub_install_mkdir_p (dst_locale);
+  clean_grub_dir (dst_locale);
+
+  if (install_locales.is_default)
+    {
+      char *srcd = grub_util_path_concat (2, src, "po");
+      copy_by_ext (srcd, dst_locale, ".mo", 0);
+      copy_locales (dst_locale);
+      free (srcd);
+    }
+  else
+    {
+      size_t i;
+      const char *locale_dir = get_localedir ();
+
+      for (i = 0; i < install_locales.n_entries; i++)
+      {
+        char *srcf = grub_util_path_concat_ext (3, src, "po",
+                                                install_locales.entries[i],
+                                                ".mo");
+        char *dstf = grub_util_path_concat_ext (2, dst_locale,
+                                                install_locales.entries[i],
+                                                ".mo");
+        if (grub_install_compress_file (srcf, dstf, 0))
+          {
+            free (srcf);
+            free (dstf);
+            continue;
+          }
+        free (srcf);
+        srcf = grub_util_path_concat_ext (4, locale_dir,
+                                          install_locales.entries[i],
+                                          "LC_MESSAGES", PACKAGE, ".mo");
+        if (grub_install_compress_file (srcf, dstf, 0) == 0)
+          {
+            grub_util_error (_("cannot find locale `%s'"),
+                             install_locales.entries[i]);
+          }
+        free (srcf);
+        free (dstf);
+      }
+    }
+  free (dst_locale);
+}
 
 void
 grub_install_copy_files (const char *src,
 			 const char *dst,
 			 enum grub_install_plat platid)
 {
-  char *dst_platform, *dst_locale, *dst_fonts;
+  char *dst_platform, *dst_fonts;
   const char *pkgdatadir = grub_util_get_pkgdatadir ();
   char *themes_dir;
 
@@ -742,13 +797,12 @@ grub_install_copy_files (const char *src,
     dst_platform = grub_util_path_concat (2, dst, platform);
     free (platform);
   }
-  dst_locale = grub_util_path_concat (2, dst, "locale");
   dst_fonts = grub_util_path_concat (2, dst, "fonts");
   grub_install_mkdir_p (dst_platform);
-  grub_install_mkdir_p (dst_locale);
   clean_grub_dir (dst);
   clean_grub_dir (dst_platform);
-  clean_grub_dir (dst_locale);
+
+  grub_install_copy_nls(src, dst);
 
   if (install_modules.is_default)
     copy_by_ext (src, dst_platform, ".mod", 1);
@@ -797,50 +851,6 @@ grub_install_copy_files (const char *src,
       free (dstf);
     }
 
-  if (install_locales.is_default)
-    {
-      char *srcd = grub_util_path_concat (2, src, "po");
-      copy_by_ext (srcd, dst_locale, ".mo", 0);
-      copy_locales (dst_locale);
-      free (srcd);
-    }
-  else
-    {
-      const char *locale_dir = get_localedir ();
-
-      for (i = 0; i < install_locales.n_entries; i++)
-	{
-	  char *srcf = grub_util_path_concat_ext (3, src,
-						"po",
-						install_locales.entries[i],
-						".mo");
-	  char *dstf = grub_util_path_concat_ext (2, dst_locale,
-						install_locales.entries[i],
-						".mo");
-	  if (grub_install_compress_file (srcf, dstf, 0))
-	    {
-	      free (srcf);
-	      free (dstf);
-	      continue;
-	    }
-	  free (srcf);
-	  srcf = grub_util_path_concat_ext (4,
-						 locale_dir,
-						 install_locales.entries[i],
-						 "LC_MESSAGES",
-						 PACKAGE,
-						 ".mo");
-	  if (grub_install_compress_file (srcf, dstf, 0))
-	    {
-	      free (srcf);
-	      free (dstf);
-	      continue;
-	    }
-	  grub_util_error (_("cannot find locale `%s'"),
-			   install_locales.entries[i]);
-	}
-    }
-
   if (install_themes.is_default)
     {
       install_themes.is_default = 0;
@@ -903,7 +913,6 @@ grub_install_copy_files (const char *src,
     }
 
   free (dst_platform);
-  free (dst_locale);
   free (dst_fonts);
 }
 
#1 lore raw
Quoted 22 lines · Olaf Hering

With --disable-nls no locales exist.

Avoid runtime error by moving code that copies locales into its own function. Return early in case nls was disabled. That way the compiler will throw away unreachable code, no need to put preprocessor conditionals everywhere to avoid warnings about unused code.

Fix memleak by freeing dstf.
Convert tabs to spaces in moved code.

---
 util/grub-install-common.c | 107 ++++++++++++++++++++++++---------------------
 1 file changed, 58 insertions(+), 49 deletions(-)
util/grub-install-common.c +0 -0
index 9e3e358c9..56197fa1d 100644
@@ -725,13 +725,68 @@ grub_install_get_platform_platform (enum grub_install_plat platid)
   return platforms[platid].platform;
 }

util/grub-install-common.c grub_install_get_platform_platform (enum grub_install_plat platid) · line 736

Quoted 10 lines
util/grub-install-common.c +9 -0
 
+static void
+grub_install_copy_nls(const char *src, const char *dst)
+{
+  char *dst_locale;
+
+#if !(defined (GRUB_UTIL) && defined(ENABLE_NLS) && ENABLE_NLS)
+  /* No locales exist without NLS. */
+  return;
+#endif

Sorry, I do not like it. Please use "__attribute__ ((unused))" for the function arguments and put #if/#endif around its whole body.

Daniel

#2 lore raw

Am Wed, 7 Mar 2018 22:30:53 +0100
schrieb Daniel Kiper <[email protected]>:

Quoted 2 lines

Sorry, I do not like it. Please use "__attribute__ ((unused))" for the function arguments and put #if/#endif around its whole body.

It is not just this function body, many more parts will be unneeded. Really polluted the entire file with #if and __attributes__?

Olaf

#3 lore raw
Quoted 7 lines · Olaf Hering

Am Wed, 7 Mar 2018 22:30:53 +0100
schrieb Daniel Kiper <[email protected]>:

Quoted 2 lines

Sorry, I do not like it. Please use "__attribute__ ((unused))" for the function arguments and put #if/#endif around its whole body.

It is not just this function body, many more parts will be unneeded.

I am not sure what are you talking about.

Quoted 1 line

Really polluted the entire file with #if and __attributes__?

I think that you have two choices here. You can add __attribute__ to grub_install_copy_nls() or #if#endif whole function and its call. Both are not perfect. However, if you would ask me I probably would choose the latter.

Daniel