]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Simplify error message and multiline warning code.
authorBruno Haible <bruno@clisp.org>
Mon, 7 May 2001 13:20:28 +0000 (13:20 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 7 May 2001 13:20:28 +0000 (13:20 +0000)
lib/ChangeLog
lib/Makefile.am
lib/xerror.c [new file with mode: 0644]
lib/xerror.h [new file with mode: 0644]
po/ChangeLog
po/POTFILES.in
src/ChangeLog
src/msgfmt.c
src/po-charset.c

index 2a1736afe15ee4a4f9f6d025f857786320fea540..3c966c7386333a36575dad7581b7718b964b2446 100644 (file)
@@ -1,3 +1,10 @@
+2001-05-02  Bruno Haible  <haible@clisp.cons.org>
+
+       * xerror.h: New file.
+       * xerror.c: New file.
+       * Makefile.am (libnlsut_a_SOURCES): Add xerror.c.
+       (noinst_HEADERS): Add xerror.h.
+
 2001-05-01  Bruno Haible  <haible@clisp.cons.org>
 
        Prefix most error messages with the program name, except those
index 3dee1f848b417279d5b281f92dd0008ffb1d7989..4946be981c1ae48b771e1054266000c2e141218d 100644 (file)
@@ -28,13 +28,13 @@ gen-lbrkprop.c 3level.h
 
 libnlsut_a_SOURCES = basename.c c-ctype.c concatpath.c fstrcmp.c \
 getopt.c getopt1.c hash.c linebreak.c localcharset.c mbswidth.c obstack.c \
-xgetcwd.c xmalloc.c xstrdup.c
+xerror.c xgetcwd.c xmalloc.c xstrdup.c
 
 libnlsut_a_LIBADD = @ALLOCA@ @LIBOBJS@
 
 noinst_HEADERS = c-ctype.h error.h fstrcmp.h getline.h getopt.h hash.h \
 lbrkprop.h linebreak.h mbswidth.h obstack.h printf-parse.h printf.h system.h \
-pathmax.h
+pathmax.h xerror.h
 
 DEFS = -DLIBDIR=\"$(libdir)\" @DEFS@
 INCLUDES = -I. -I$(srcdir) -I.. -I../intl
diff --git a/lib/xerror.c b/lib/xerror.c
new file mode 100644 (file)
index 0000000..5b035cd
--- /dev/null
@@ -0,0 +1,141 @@
+/* Multiline error-reporting functions.
+   Copyright (C) 2001 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
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "xerror.h"
+#include "error.h"
+#include "progname.h"
+#include "mbswidth.h"
+#include "libgettext.h"
+
+#define _(str) gettext (str)
+
+#if __STDC__
+# include <stdarg.h>
+# define VA_START(args, lastarg) va_start(args, lastarg)
+#else
+# include <varargs.h>
+# define VA_START(args, lastarg) va_start(args)
+# define NEW_VARARGS 1 /* or 0 if it doesn't work */
+#endif
+
+/* Format a message and return the freshly allocated resulting string.  */
+char *
+#if __STDC__
+xasprintf (const char *format, ...)
+#elif NEW_VARARGS
+xasprintf (format, va_alist)
+     const char *format;
+     va_dcl
+#else
+xasprintf (va_alist)
+     va_dcl
+#endif
+{
+#if !__STDC__ && !NEW_VARARGS
+  const char *format;
+#endif
+  va_list args;
+  char *result;
+
+  va_start (args, format);
+#if !__STDC__ && !NEW_VARARGS
+  format = va_arg (args, const char *);
+#endif
+  if (vasprintf (&result, format, args) < 0)
+    error (EXIT_FAILURE, 0, _("memory exhausted"));
+  va_end (args);
+  return result;
+}
+
+/* Emit a multiline warning to stderr, consisting of MESSAGE, with the
+   first line prefixed with PREFIX and the remaining lines prefixed with
+   the same amount of spaces.  Reuse the spaces of the previous call if
+   PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
+void
+multiline_warning (prefix, message)
+     char *prefix;
+     char *message;
+{
+  static int width;
+  const char *cp;
+  int i;
+
+  fflush (stdout);
+
+  cp = message;
+
+  if (prefix != NULL)
+    {
+      width = 0;
+      if (error_with_progname)
+       {
+         fprintf (stderr, "%s: ", program_name);
+         width += mbswidth (program_name, 0) + 2;
+       }
+      fputs (prefix, stderr);
+      width += mbswidth (prefix, 0);
+      free (prefix);
+      goto after_indent;
+    }
+
+  while (1)
+    {
+      const char *np;
+
+      for (i = width; i > 0; i--)
+       putc (' ', stderr);
+
+    after_indent:
+      np = strchr (cp, '\n');
+
+      if (np == NULL || np[1] == '\0')
+       {
+         fputs (cp, stderr);
+         break;
+       }
+
+      np++;
+      fwrite (cp, 1, np - cp, stderr);
+      cp = np;
+    }
+
+  free (message);
+}
+
+/* Emit a multiline error to stderr, consisting of MESSAGE, with the
+   first line prefixed with PREFIX and the remaining lines prefixed with
+   the same amount of spaces.  Reuse the spaces of the previous call if
+   PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
+void
+multiline_error (prefix, message)
+     char *prefix;
+     char *message;
+{
+  if (prefix != NULL)
+    ++error_message_count;
+  multiline_warning (prefix, message);
+}
diff --git a/lib/xerror.h b/lib/xerror.h
new file mode 100644 (file)
index 0000000..788cbb2
--- /dev/null
@@ -0,0 +1,45 @@
+/* Multiline error-reporting functions.
+   Copyright (C) 2001 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
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#ifndef _XERROR_H
+#define _XERROR_H
+
+/* Get fallback definition of __attribute__.  */
+#include "error.h"
+
+/* Format a message and return the freshly allocated resulting string.  */
+#if defined (__STDC__) && __STDC__
+extern char *xasprintf (const char *format, ...)
+     __attribute__ ((__format__ (__printf__, 1, 2)));
+#else
+extern char *xasprintf ();
+#endif
+
+/* Emit a multiline warning to stderr, consisting of MESSAGE, with the
+   first line prefixed with PREFIX and the remaining lines prefixed with
+   the same amount of spaces.  Reuse the spaces of the previous call if
+   PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
+extern void multiline_warning PARAMS ((char *prefix, char *message));
+
+/* Emit a multiline error to stderr, consisting of MESSAGE, with the
+   first line prefixed with PREFIX and the remaining lines prefixed with
+   the same amount of spaces.  Reuse the spaces of the previous call if
+   PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
+extern void multiline_error PARAMS ((char *prefix, char *message));
+
+#endif /* _XERROR_H */
index 446cd2982145170441ebef76183f4a22a0f7c34e..7567230b75e427a419bacb2d2c70189a60678e48 100644 (file)
@@ -1,3 +1,7 @@
+2001-05-02  Bruno Haible  <haible@clisp.cons.org>
+
+       * POTFILES.in: Add lib/xerror.c.
+
 2001-04-30  Bruno Haible  <haible@clisp.cons.org>
 
        * et.po: New file, from Ivar Smolin <okul@trenet.ee>.
index 235a9e7c47866cad44d8c2d56db6284d344fd94b..cbb18c6e343a34b4309f17840276276b4a124716 100644 (file)
@@ -8,6 +8,7 @@
 lib/error.c
 lib/getopt.c
 lib/obstack.c
+lib/xerror.c
 lib/xmalloc.c
 
 # Package source files
index d84d63d09bd4ea54836e31c2a2bd4ef5e613c864..b0d75212e5ee4b8793c109d4e9932a19d4824387 100644 (file)
@@ -1,3 +1,12 @@
+2001-05-02  Bruno Haible  <haible@clisp.cons.org>
+
+       * po-charset.c: Don't include stdio.h, stdlib.h, mbswidth.h. Include
+       xerror.h instead.
+       (multiline_warning): Move to xerror.c.
+       (po_lex_charset_set): Use xasprintf instead of asprintf.
+       * msgfmt.c: Include xerror.h.
+       (format_debrief): Use multiline_error for the error message.
+
 2001-05-01  Bruno Haible  <haible@clisp.cons.org>
 
        Prefix most error messages with the program name, except those
index b75f5d282dac86e04bf5e4127dc621d11572c95a..7edd74c4a2ae998086d42c0d091f5909f113db85 100644 (file)
@@ -33,6 +33,7 @@
 #include "dir-list.h"
 #include "error.h"
 #include "progname.h"
+#include "xerror.h"
 #include "getline.h"
 #include "printf.h"
 #include <system.h>
@@ -487,9 +488,14 @@ format_debrief (that)
      FIXME: Should do this even if not in verbose mode, because the
      consequences are not harmless.  But it breaks the test suite.  */
   if (verbose_level > 0 && this->has_header_entry == 0)
-    error (0, 0, _("%s: warning: PO file header missing, fuzzy, or invalid\n\
-%*s  warning: charset conversion will not work"),
-          gram_pos.file_name, (int) strlen (gram_pos.file_name), "");
+    {
+      multiline_error (xasprintf ("%s: ", gram_pos.file_name),
+                      xasprintf (_("\
+warning: PO file header missing, fuzzy, or invalid\n")));
+      multiline_error (NULL,
+                      xasprintf (_("\
+warning: charset conversion will not work\n")));
+    }
 }
 
 
@@ -726,7 +732,7 @@ format_comment_special (that, s)
          warned = 1;
          error (0, 0, _("\
 %s: warning: source file contains fuzzy translation"),
-                 gram_pos.file_name);
+                gram_pos.file_name);
        }
 
       this->is_fuzzy = 1;
index 7f2263a8eded4f7bfbe2864215aa854c025075e1..13cb87a14fd7c0c157250aae7b0908fcbd398015 100644 (file)
 # include "config.h"
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
-
 #include "po-charset.h"
 #include "error.h"
+#include "xerror.h"
 #include "system.h"
-#include "mbswidth.h"
 #include "libgettext.h"
 
 extern const char *program_name;
@@ -111,55 +108,6 @@ po_lex_charset_init ()
 #endif
 }
 
-/* Emit a multiline warning to stderr, consisting of MESSAGE, with the
-   first line prefixed with PREFIX and the remaining lines prefixed with
-   the same amount of spaces.  Reuse the spaces of the previous call if
-   PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
-static void
-multiline_warning (prefix, message)
-     char *prefix;
-     char *message;
-{
-  static int width;
-  const char *cp;
-  int i;
-
-  fflush (stdout);
-
-  cp = message;
-
-  if (prefix != NULL)
-    {
-      fputs (prefix, stderr);
-      width = mbswidth (prefix, 0);
-      free (prefix);
-      goto after_indent;
-    }
-
-  while (1)
-    {
-      const char *np;
-
-      for (i = width; i > 0; i--)
-       putc (' ', stderr);
-
-    after_indent:
-      np = strchr (cp, '\n');
-
-      if (np == NULL || np[1] == '\0')
-       {
-         fputs (cp, stderr);
-         break;
-       }
-
-      np++;
-      fwrite (cp, 1, np - cp, stderr);
-      cp = np;
-    }
-
-  free (message);
-}
-
 void
 po_lex_charset_set (header_entry, filename)
      const char *header_entry;
@@ -194,19 +142,11 @@ po_lex_charset_set (header_entry, filename)
          if (!(filenamelen >= 4
                && memcmp (filename + filenamelen - 4, ".pot", 4) == 0
                && strcmp (charset, "CHARSET") == 0))
-           {
-             char *prefix;
-             char *msg;
-
-             asprintf (&prefix, _("%s: warning: "), filename);
-             asprintf (&msg, _("\
+           multiline_warning (xasprintf (_("%s: warning: "), filename),
+                              xasprintf (_("\
 Charset \"%s\" is not a portable encoding name.\n\
 Message conversion to user's charset might not work.\n"),
-                       charset);
-             if (prefix == NULL || msg == NULL)
-               error (EXIT_FAILURE, 0, _("memory exhausted"));
-             multiline_warning (prefix, msg);
-           }
+                                         charset));
        }
       else
        {
@@ -261,8 +201,6 @@ Message conversion to user's charset might not work.\n"),
                {
                  size_t i;
                  const char *note;
-                 char *prefix;
-                 char *msg;
 
                  for (i = 0; i < SIZEOF (weird_charsets); i++)
                    if (strcmp (po_lex_charset, weird_charsets[i]) == 0)
@@ -272,29 +210,22 @@ Message conversion to user's charset might not work.\n"),
                  else
                    note = _("Continuing anyway.");
 
-                 asprintf (&prefix, _("%s: warning: "), filename);
-                 asprintf (&msg, _("\
+                 multiline_warning (xasprintf (_("%s: warning: "), filename),
+                                    xasprintf (_("\
 Charset \"%s\" is not supported. %s relies on iconv(),\n\
 and iconv() does not support \"%s\".\n"),
-                           po_lex_charset, basename (program_name),
-                           po_lex_charset);
-                 if (prefix == NULL || msg == NULL)
-                   error (EXIT_FAILURE, 0, _("memory exhausted"));
-                 multiline_warning (prefix, msg);
+                                               po_lex_charset,
+                                               basename (program_name),
+                                               po_lex_charset));
 
 # if !defined _LIBICONV_VERSION
-                 asprintf (&msg, _("\
+                 multiline_warning (NULL,
+                                    xasprintf (_("\
 Installing GNU libiconv and then reinstalling GNU gettext\n\
-would fix this problem.\n"));
-                 if (msg == NULL)
-                   error (EXIT_FAILURE, 0, _("memory exhausted"));
-                 multiline_warning (NULL, msg);
+would fix this problem.\n")));
 # endif
 
-                 asprintf (&msg, _("%s\n"), note);
-                 if (msg == NULL)
-                   error (EXIT_FAILURE, 0, _("memory exhausted"));
-                 multiline_warning (NULL, msg);
+                 multiline_warning (NULL, xasprintf (_("%s\n"), note));
                }
 #else
              for (i = 0; i < SIZEOF (weird_charsets); i++)
@@ -304,29 +235,20 @@ would fix this problem.\n"));
                {
                  const char *note =
                    _("Continuing anyway, expect parse errors.");
-                 char *prefix;
-                 char *msg;
 
-                 asprintf (&prefix, _("%s: warning: "), filename);
-                 asprintf (&msg, _("\
+                 multiline_warning (xasprintf (_("%s: warning: "), filename),
+                                    xasprintf (_("\
 Charset \"%s\" is not supported. %s relies on iconv().\n\
 This version was built without iconv().\n"),
-                           po_lex_charset, basename (program_name));
-                 if (prefix == NULL || msg == NULL)
-                   error (EXIT_FAILURE, 0, _("memory exhausted"));
-                 multiline_warning (prefix, msg);
+                                               po_lex_charset,
+                                               basename (program_name)));
 
-                 asprintf (&msg, _("\
+                 multiline_warning (NULL,
+                                    xasprintf (_("\
 Installing GNU libiconv and then reinstalling GNU gettext\n\
-would fix this problem.\n"));
-                 if (msg == NULL)
-                   error (EXIT_FAILURE, 0, _("memory exhausted"));
-                 multiline_warning (NULL, msg);
+would fix this problem.\n")));
 
-                 asprintf (&msg, _("%s\n"), note);
-                 if (msg == NULL)
-                   error (EXIT_FAILURE, 0, _("memory exhausted"));
-                 multiline_warning (NULL, msg);
+                 multiline_warning (NULL, xasprintf (_("%s\n"), note));
                }
 #endif
            }
@@ -340,18 +262,10 @@ would fix this problem.\n"));
 
       if (!(filenamelen >= 4
            && memcmp (filename + filenamelen - 4, ".pot", 4) == 0))
-       {
-         char *prefix;
-         char *msg;
-
-         asprintf (&prefix, _("%s: warning: "), filename);
-         asprintf (&msg, _("\
+       multiline_warning (xasprintf (_("%s: warning: "), filename),
+                          xasprintf (_("\
 Charset missing in header.\n\
-Message conversion to user's charset will not work.\n"));
-         if (prefix == NULL || msg == NULL)
-           error (EXIT_FAILURE, 0, _("memory exhausted"));
-         multiline_warning (prefix, msg);
-       }
+Message conversion to user's charset will not work.\n")));
     }
 }