From: Bruno Haible Date: Mon, 7 May 2001 13:20:28 +0000 (+0000) Subject: Simplify error message and multiline warning code. X-Git-Tag: v0.11~736 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3e821497f71f2d0e3134e8c75d414e379043837b;p=thirdparty%2Fgettext.git Simplify error message and multiline warning code. --- diff --git a/lib/ChangeLog b/lib/ChangeLog index 2a1736afe..3c966c738 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,10 @@ +2001-05-02 Bruno Haible + + * 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 Prefix most error messages with the program name, except those diff --git a/lib/Makefile.am b/lib/Makefile.am index 3dee1f848..4946be981 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 index 000000000..5b035cd96 --- /dev/null +++ b/lib/xerror.c @@ -0,0 +1,141 @@ +/* Multiline error-reporting functions. + Copyright (C) 2001 Free Software Foundation, Inc. + Written by Bruno Haible , 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 +#include +#include + +#include "xerror.h" +#include "error.h" +#include "progname.h" +#include "mbswidth.h" +#include "libgettext.h" + +#define _(str) gettext (str) + +#if __STDC__ +# include +# define VA_START(args, lastarg) va_start(args, lastarg) +#else +# include +# 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 index 000000000..788cbb2b0 --- /dev/null +++ b/lib/xerror.h @@ -0,0 +1,45 @@ +/* Multiline error-reporting functions. + Copyright (C) 2001 Free Software Foundation, Inc. + Written by Bruno Haible , 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 */ diff --git a/po/ChangeLog b/po/ChangeLog index 446cd2982..7567230b7 100644 --- a/po/ChangeLog +++ b/po/ChangeLog @@ -1,3 +1,7 @@ +2001-05-02 Bruno Haible + + * POTFILES.in: Add lib/xerror.c. + 2001-04-30 Bruno Haible * et.po: New file, from Ivar Smolin . diff --git a/po/POTFILES.in b/po/POTFILES.in index 235a9e7c4..cbb18c6e3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -8,6 +8,7 @@ lib/error.c lib/getopt.c lib/obstack.c +lib/xerror.c lib/xmalloc.c # Package source files diff --git a/src/ChangeLog b/src/ChangeLog index d84d63d09..b0d75212e 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2001-05-02 Bruno Haible + + * 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 Prefix most error messages with the program name, except those diff --git a/src/msgfmt.c b/src/msgfmt.c index b75f5d282..7edd74c4a 100644 --- a/src/msgfmt.c +++ b/src/msgfmt.c @@ -33,6 +33,7 @@ #include "dir-list.h" #include "error.h" #include "progname.h" +#include "xerror.h" #include "getline.h" #include "printf.h" #include @@ -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; diff --git a/src/po-charset.c b/src/po-charset.c index 7f2263a8e..13cb87a14 100644 --- a/src/po-charset.c +++ b/src/po-charset.c @@ -21,13 +21,10 @@ # include "config.h" #endif -#include -#include - #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"))); } }