]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Improved error message when a PO file is not valid in the specified encoding.
authorBruno Haible <bruno@clisp.org>
Tue, 3 May 2005 10:41:18 +0000 (10:41 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:12:33 +0000 (12:12 +0200)
gettext-tools/src/ChangeLog
gettext-tools/src/msgl-iconv.c
gettext-tools/src/msgl-iconv.h
gettext-tools/src/xgettext.c

index 9a75d92dac7f19c3f45f9ae81f1b5e37598e36fb..0036cab678f50beca541de6b11d8cc513df7f18b 100644 (file)
@@ -1,3 +1,16 @@
+2005-05-01  Bruno Haible  <bruno@clisp.org>
+
+       Improved error message.
+       * msgl-iconv.h (struct conversion_context): New type.
+       (convert_string): Add context argument.
+       * msgl-iconv.c (conversion_error): New function.
+       (convert_string, convert_string_list, convert_msgid, convert_msgstr):
+       Add context argument.
+       (iconv_message_list): Construct context for them.
+       * xgettext.c (convert_string): Add context argument.
+       (from_current_source_encoding): Construct context for convert_string.
+       Reported by Hans Ulrich Niedermann <debian@n-dimensional.de>.
+
 2005-04-18  Bruno Haible  <bruno@clisp.org>
 
        * po-lex.h (po_gram_error, po_gram_error_at_line): Test for
index d5f93b7efb7c87bc23c7f1e53ef80290be9a8d19..f1c549ec0d4fe79413516f12861ca6061b0ef51c 100644 (file)
@@ -1,5 +1,5 @@
 /* Message list charset and locale charset handling.
-   Copyright (C) 2001-2003 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2005 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -171,8 +171,29 @@ iconv_string (iconv_t cd, const char *start, const char *end,
 #undef tmpbufsize
 }
 
+static void conversion_error (const struct conversion_context* context)
+#if defined __GNUC__ && ((__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || __GNUC__ > 2)
+     __attribute__ ((noreturn))
+#endif
+;
+static void
+conversion_error (const struct conversion_context* context)
+{
+  if (context->to_code == po_charset_utf8)
+    /* If a conversion to UTF-8 fails, the problem lies in the input.  */
+    error (EXIT_FAILURE, 0, _("%s: input is not valid in \"%s\" encoding"),
+          context->from_filename, context->from_code);
+  else
+    error (EXIT_FAILURE, 0,
+          _("%s: error while converting from \"%s\" encoding to \"%s\" encoding"),
+          context->from_filename, context->from_code, context->to_code);
+  /* NOTREACHED */
+  abort ();
+}
+
 char *
-convert_string (iconv_t cd, const char *string)
+convert_string (iconv_t cd, const char *string,
+               const struct conversion_context* context)
 {
   size_t len = strlen (string) + 1;
   char *result = NULL;
@@ -184,31 +205,34 @@ convert_string (iconv_t cd, const char *string)
        && strlen (result) == resultlen - 1)
       return result;
 
-  error (EXIT_FAILURE, 0, _("conversion failure"));
+  conversion_error (context);
   /* NOTREACHED */
   return NULL;
 }
 
 static void
-convert_string_list (iconv_t cd, string_list_ty *slp)
+convert_string_list (iconv_t cd, string_list_ty *slp,
+                    const struct conversion_context* context)
 {
   size_t i;
 
   if (slp != NULL)
     for (i = 0; i < slp->nitems; i++)
-      slp->item[i] = convert_string (cd, slp->item[i]);
+      slp->item[i] = convert_string (cd, slp->item[i], context);
 }
 
 static void
-convert_msgid (iconv_t cd, message_ty *mp)
+convert_msgid (iconv_t cd, message_ty *mp,
+              const struct conversion_context* context)
 {
-  mp->msgid = convert_string (cd, mp->msgid);
+  mp->msgid = convert_string (cd, mp->msgid, context);
   if (mp->msgid_plural != NULL)
-    mp->msgid_plural = convert_string (cd, mp->msgid_plural);
+    mp->msgid_plural = convert_string (cd, mp->msgid_plural, context);
 }
 
 static void
-convert_msgstr (iconv_t cd, message_ty *mp)
+convert_msgstr (iconv_t cd, message_ty *mp,
+               const struct conversion_context* context)
 {
   char *result = NULL;
   size_t resultlen;
@@ -242,7 +266,7 @@ convert_msgstr (iconv_t cd, message_ty *mp)
          }
       }
 
-  error (EXIT_FAILURE, 0, _("conversion failure"));
+  conversion_error (context);
 }
 
 #endif
@@ -345,6 +369,7 @@ input file doesn't contain a header entry with a charset specification"));
     {
 #if HAVE_ICONV
       iconv_t cd;
+      struct conversion_context context;
       bool msgids_changed;
 
       /* Avoid glibc-2.1 bug with EUC-KR.  */
@@ -360,6 +385,10 @@ Cannot convert from \"%s\" to \"%s\". %s relies on iconv(), \
 and iconv() does not support this conversion."),
               canon_from_code, canon_to_code, basename (program_name));
 
+      context.from_code = canon_from_code;
+      context.to_code = canon_to_code;
+      context.from_filename = from_filename;
+
       msgids_changed = false;
       for (j = 0; j < mlp->nitems; j++)
        {
@@ -367,10 +396,10 @@ and iconv() does not support this conversion."),
 
          if (!is_ascii_string (mp->msgid))
            msgids_changed = true;
-         convert_string_list (cd, mp->comment);
-         convert_string_list (cd, mp->comment_dot);
-         convert_msgid (cd, mp);
-         convert_msgstr (cd, mp);
+         convert_string_list (cd, mp->comment, &context);
+         convert_string_list (cd, mp->comment_dot, &context);
+         convert_msgid (cd, mp, &context);
+         convert_msgstr (cd, mp, &context);
        }
 
       iconv_close (cd);
index 44acb71bdc404749403f63edb2676b098bc2aa02..2696dd1b7c21a367bc4d08d3ecfecf2c263930ef 100644 (file)
@@ -1,5 +1,5 @@
 /* Message list character set conversion.
-   Copyright (C) 2001-2003 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2005 Free Software Foundation, Inc.
    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
    This program is free software; you can redistribute it and/or modify
@@ -32,8 +32,19 @@ extern "C" {
 
 
 #if HAVE_ICONV
+
+/* A context, used for accurate error messages.  */
+struct conversion_context
+{
+  const char *from_code;     /* canonicalized encoding name for input */
+  const char *to_code;       /* canonicalized encoding name for output */
+  const char *from_filename; /* file name where the input comes from */
+};
+
 /* Converts the STRING through the conversion descriptor CD.  */
-extern char *convert_string (iconv_t cd, const char *string);
+extern char *convert_string (iconv_t cd, const char *string,
+                            const struct conversion_context* context);
+
 #endif
 
 /* Converts the message list MLP to the (already canonicalized) encoding
index eb035f91b5efd7b3971c70555ca8162bef5637c1..30b6608e77897ce448d725082606fda2445aac33 100644 (file)
@@ -1647,7 +1647,7 @@ extract_from_file (const char *file_name, extractor_ty extractor,
    xgettext_global_source_encoding and thus also for
    xgettext_current_source_encoding are ASCII and UTF-8.
    convert_string() should not be called in this case.  */
-#define convert_string(cd,string) (abort (), (string))
+#define convert_string(cd,string,context) (abort (), (string))
 #endif
 
 /* Convert the given string from xgettext_current_source_encoding to
@@ -1677,7 +1677,15 @@ Please specify the source encoding through --from-code.\n"),
        }
     }
   else if (xgettext_current_source_encoding != po_charset_utf8)
-    string = convert_string (xgettext_current_source_iconv, string);
+    {
+      struct conversion_context context;
+
+      context.from_code = xgettext_current_source_encoding;
+      context.to_code = po_charset_utf8;
+      context.from_filename = file_name;
+
+      string = convert_string (xgettext_current_source_iconv, string, &context);
+    }
 
   return (char *) string;
 }