d8307d
commit e485b2b6e006a7efa5d73e6be7e357a395c77fe3
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Tue Apr 23 18:16:26 2019 +0200
d8307d
d8307d
    locale: Add LOCPATH diagnostics to the locale program
d8307d
    
d8307d
    The implementation of quote_string is based on support_quote_blob.
d8307d
    
d8307d
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
d8307d
d8307d
diff --git a/locale/Makefile b/locale/Makefile
d8307d
index fd9972279ba7fe0b..42bb36c7d374eebe 100644
d8307d
--- a/locale/Makefile
d8307d
+++ b/locale/Makefile
d8307d
@@ -28,6 +28,7 @@ routines	= setlocale findlocale loadlocale loadarchive \
d8307d
 		  localeconv nl_langinfo nl_langinfo_l mb_cur_max \
d8307d
 		  newlocale duplocale freelocale uselocale
d8307d
 tests		= tst-C-locale tst-locname tst-duplocale
d8307d
+tests-special	= $(objpfx)tst-locale-locpath.out
d8307d
 categories	= ctype messages monetary numeric time paper name \
d8307d
 		  address telephone measurement identification collate
d8307d
 aux		= $(categories:%=lc-%) $(categories:%=C-%) SYS_libc C_name \
d8307d
@@ -104,3 +105,7 @@ cpp-srcs-left := $(localedef-modules) $(localedef-aux) $(locale-modules) \
d8307d
 		 $(lib-modules)
d8307d
 lib := locale-programs
d8307d
 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
d8307d
+
d8307d
+$(objpfx)tst-locale-locpath.out : tst-locale-locpath.sh $(objpfx)locale
d8307d
+	$(SHELL) $< '$(common-objpfx)' '$(test-wrapper)' '$(test-wrapper-env)' > $@; \
d8307d
+	$(evaluate-test)
d8307d
diff --git a/locale/programs/locale.c b/locale/programs/locale.c
d8307d
index 86941e4ef6e67d78..0e2e3e4e5788246f 100644
d8307d
--- a/locale/programs/locale.c
d8307d
+++ b/locale/programs/locale.c
d8307d
@@ -173,6 +173,9 @@ static int write_archive_locales (void **all_datap, char *linebuf);
d8307d
 static void write_charmaps (void);
d8307d
 static void show_locale_vars (void);
d8307d
 static void show_info (const char *name);
d8307d
+static void try_setlocale (int category, const char *category_name);
d8307d
+static char *quote_string (const char *input);
d8307d
+static void setlocale_diagnostics (void);
d8307d
 
d8307d
 
d8307d
 int
d8307d
@@ -186,10 +189,8 @@ main (int argc, char *argv[])
d8307d
 
d8307d
   /* Set locale.  Do not set LC_ALL because the other categories must
d8307d
      not be affected (according to POSIX.2).  */
d8307d
-  if (setlocale (LC_CTYPE, "") == NULL)
d8307d
-    error (0, errno, gettext ("Cannot set LC_CTYPE to default locale"));
d8307d
-  if (setlocale (LC_MESSAGES, "") == NULL)
d8307d
-    error (0, errno, gettext ("Cannot set LC_MESSAGES to default locale"));
d8307d
+  try_setlocale (LC_CTYPE, "LC_CTYPE");
d8307d
+  try_setlocale (LC_MESSAGES, "LC_MESSAGES");
d8307d
 
d8307d
   /* Initialize the message catalog.  */
d8307d
   textdomain (PACKAGE);
d8307d
@@ -200,9 +201,8 @@ main (int argc, char *argv[])
d8307d
   /* `-a' requests the names of all available locales.  */
d8307d
   if (do_all != 0)
d8307d
     {
d8307d
-      if (setlocale (LC_COLLATE, "") == NULL)
d8307d
-	error (0, errno,
d8307d
-	       gettext ("Cannot set LC_COLLATE to default locale"));
d8307d
+      setlocale_diagnostics ();
d8307d
+      try_setlocale (LC_COLLATE, "LC_COLLATE");
d8307d
       write_locales ();
d8307d
       exit (EXIT_SUCCESS);
d8307d
     }
d8307d
@@ -211,14 +211,15 @@ main (int argc, char *argv[])
d8307d
      used for the -f argument to localedef(1).  */
d8307d
   if (do_charmaps != 0)
d8307d
     {
d8307d
+      setlocale_diagnostics ();
d8307d
       write_charmaps ();
d8307d
       exit (EXIT_SUCCESS);
d8307d
     }
d8307d
 
d8307d
   /* Specific information about the current locale are requested.
d8307d
      Change to this locale now.  */
d8307d
-  if (setlocale (LC_ALL, "") == NULL)
d8307d
-    error (0, errno, gettext ("Cannot set LC_ALL to default locale"));
d8307d
+  try_setlocale (LC_ALL, "LC_ALL");
d8307d
+  setlocale_diagnostics ();
d8307d
 
d8307d
   /* If no real argument is given we have to print the contents of the
d8307d
      current locale definition variables.  These are LANG and the LC_*.  */
d8307d
@@ -983,3 +984,121 @@ show_info (const char *name)
d8307d
      For testing and perhaps advanced use allow some more symbols.  */
d8307d
   locale_special (name, show_category_name, show_keyword_name);
d8307d
 }
d8307d
+
d8307d
+/* Set to true by try_setlocale if setlocale fails.  Used by
d8307d
+   setlocale_diagnostics.  */
d8307d
+static bool setlocale_failed;
d8307d
+
d8307d
+/* Call setlocale, with non-fatal error reporting.  */
d8307d
+static void
d8307d
+try_setlocale (int category, const char *category_name)
d8307d
+{
d8307d
+  if (setlocale (category, "") == NULL)
d8307d
+    {
d8307d
+      error (0, errno, gettext ("Cannot set %s to default locale"),
d8307d
+	     category_name);
d8307d
+      setlocale_failed = true;
d8307d
+    }
d8307d
+}
d8307d
+
d8307d
+/* Return a quoted version of the passed string, or NULL on error.  */
d8307d
+static char *
d8307d
+quote_string (const char *input)
d8307d
+{
d8307d
+  char *buffer;
d8307d
+  size_t length;
d8307d
+  FILE *stream = open_memstream (&buffer, &length);
d8307d
+  if (stream == NULL)
d8307d
+    return NULL;
d8307d
+
d8307d
+  while (true)
d8307d
+    {
d8307d
+      unsigned char ch = *input++;
d8307d
+      if (ch == '\0')
d8307d
+	break;
d8307d
+
d8307d
+      /* Use C backslash escapes for those control characters for
d8307d
+         which they are defined.  */
d8307d
+      switch (ch)
d8307d
+        {
d8307d
+          case '\a':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('a', stream);
d8307d
+            break;
d8307d
+          case '\b':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('b', stream);
d8307d
+            break;
d8307d
+          case '\f':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('f', stream);
d8307d
+            break;
d8307d
+          case '\n':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('n', stream);
d8307d
+            break;
d8307d
+          case '\r':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('r', stream);
d8307d
+            break;
d8307d
+          case '\t':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('t', stream);
d8307d
+            break;
d8307d
+          case '\v':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked ('v', stream);
d8307d
+            break;
d8307d
+          case '\\':
d8307d
+          case '\'':
d8307d
+          case '\"':
d8307d
+            putc_unlocked ('\\', stream);
d8307d
+            putc_unlocked (ch, stream);
d8307d
+            break;
d8307d
+        default:
d8307d
+          if (ch < ' ' || ch > '~')
d8307d
+            /* Use octal sequences because they are fixed width,
d8307d
+               unlike hexadecimal sequences.  */
d8307d
+            fprintf (stream, "\\%03o", ch);
d8307d
+          else
d8307d
+            putc_unlocked (ch, stream);
d8307d
+        }
d8307d
+    }
d8307d
+
d8307d
+  if (ferror (stream))
d8307d
+    {
d8307d
+      fclose (stream);
d8307d
+      free (buffer);
d8307d
+      return NULL;
d8307d
+    }
d8307d
+  if (fclose (stream) != 0)
d8307d
+    {
d8307d
+      free (buffer);
d8307d
+      return NULL;
d8307d
+    }
d8307d
+
d8307d
+  return buffer;
d8307d
+}
d8307d
+
d8307d
+/* Print additional information if there was a setlocale error (during
d8307d
+   try_setlocale).  */
d8307d
+static void
d8307d
+setlocale_diagnostics (void)
d8307d
+{
d8307d
+  if (setlocale_failed)
d8307d
+    {
d8307d
+      const char *locpath = getenv ("LOCPATH");
d8307d
+      if (locpath != NULL)
d8307d
+	{
d8307d
+	  char *quoted = quote_string (locpath);
d8307d
+	  if (quoted != NULL)
d8307d
+	    fprintf (stderr,
d8307d
+		     gettext ("\
d8307d
+warning: The LOCPATH variable is set to \"%s\"\n"),
d8307d
+		     quoted);
d8307d
+	  else
d8307d
+	    fputs ("warning: The LOCPATH variable is set\n", stderr);
d8307d
+	  free (quoted);
d8307d
+	}
d8307d
+    }
d8307d
+}
d8307d
diff --git a/locale/tst-locale-locpath.sh b/locale/tst-locale-locpath.sh
d8307d
new file mode 100644
d8307d
index 0000000000000000..b83de90a39121af6
d8307d
--- /dev/null
d8307d
+++ b/locale/tst-locale-locpath.sh
d8307d
@@ -0,0 +1,83 @@
d8307d
+#!/bin/sh
d8307d
+# Test that locale prints LOCPATH on failure.
d8307d
+# Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+# This file is part of the GNU C Library.
d8307d
+
d8307d
+# The GNU C Library is free software; you can redistribute it and/or
d8307d
+# modify it under the terms of the GNU Lesser General Public
d8307d
+# License as published by the Free Software Foundation; either
d8307d
+# version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+# The GNU C Library is distributed in the hope that it will be useful,
d8307d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+# Lesser General Public License for more details.
d8307d
+
d8307d
+# You should have received a copy of the GNU Lesser General Public
d8307d
+# License along with the GNU C Library; if not, see
d8307d
+# <http://www.gnu.org/licenses/>.
d8307d
+
d8307d
+set -ex
d8307d
+
d8307d
+common_objpfx=$1
d8307d
+test_wrapper_env=$2
d8307d
+run_program_env=$3
d8307d
+
d8307d
+LIBPATH="$common_objpfx"
d8307d
+
d8307d
+testroot="${common_objpfx}locale/tst-locale-locpath-directory"
d8307d
+cleanup () {
d8307d
+    rm -rf "$testroot"
d8307d
+}
d8307d
+trap cleanup 0
d8307d
+
d8307d
+rm -rf "$testroot"
d8307d
+mkdir -p $testroot
d8307d
+
d8307d
+unset LANG
d8307d
+
d8307d
+${test_wrapper_env} \
d8307d
+${run_program_env} LC_ALL=invalid-locale LOCPATH=does-not-exist \
d8307d
+${common_objpfx}elf/ld.so --library-path "$LIBPATH" \
d8307d
+  "${common_objpfx}locale/locale" \
d8307d
+  > "$testroot/stdout" 2> "$testroot/stderr"
d8307d
+
d8307d
+echo "* standard error"
d8307d
+cat "$testroot/stderr"
d8307d
+echo "* standard output"
d8307d
+cat "$testroot/stdout"
d8307d
+
d8307d
+cat > "$testroot/stderr-expected" <
d8307d
+${common_objpfx}locale/locale: Cannot set LC_CTYPE to default locale: No such file or directory
d8307d
+${common_objpfx}locale/locale: Cannot set LC_MESSAGES to default locale: No such file or directory
d8307d
+${common_objpfx}locale/locale: Cannot set LC_ALL to default locale: No such file or directory
d8307d
+warning: The LOCPATH variable is set to "does-not-exist"
d8307d
+EOF
d8307d
+
d8307d
+cat > "$testroot/stdout-expected" <
d8307d
+LANG=
d8307d
+LC_CTYPE="invalid-locale"
d8307d
+LC_NUMERIC="invalid-locale"
d8307d
+LC_TIME="invalid-locale"
d8307d
+LC_COLLATE="invalid-locale"
d8307d
+LC_MONETARY="invalid-locale"
d8307d
+LC_MESSAGES="invalid-locale"
d8307d
+LC_PAPER="invalid-locale"
d8307d
+LC_NAME="invalid-locale"
d8307d
+LC_ADDRESS="invalid-locale"
d8307d
+LC_TELEPHONE="invalid-locale"
d8307d
+LC_MEASUREMENT="invalid-locale"
d8307d
+LC_IDENTIFICATION="invalid-locale"
d8307d
+LC_ALL=invalid-locale
d8307d
+EOF
d8307d
+
d8307d
+errors=0
d8307d
+if ! cmp -s "$testroot/stderr-expected" "$testroot/stderr" ; then
d8307d
+    echo "error: standard error not correct"
d8307d
+    errors=1
d8307d
+fi
d8307d
+if ! cmp -s "$testroot/stdout-expected" "$testroot/stdout" ; then
d8307d
+    echo "error: standard output not correct"
d8307d
+    errors=1
d8307d
+fi
d8307d
+exit $errors