From 7795affeb2df547abe201200b33c80678ef5282c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 3 Aug 2024 13:10:46 +0200 Subject: [PATCH] Refactor: Remove code duplication. * gettext-tools/src/po-xerror.h: Include xerror-handler.h. Remove all declarations. (PO_SEVERITY_*): Define as aliases of CAT_SEVERITY_*. (po_xerror, po_xerror2): New macros. * gettext-tools/src/po-xerror.c: Remove file. * gettext-tools/src/Makefile.am (COMMON_SOURCE): Remove po-xerror.c. * gettext-tools/src/msgmerge.c (silent_xerror2): New function. (match_domain): Pass a stack-allocated xerror_handler to check_plural_eval. * gettext-tools/woe32dll/gettextsrc-exports.c (po_xerror, po_xerror2): Remove exports. * gettext-tools/po/POTFILES.in: Add src/xerror-handler.c. Remove src/po-xerror.c. --- gettext-tools/po/POTFILES.in | 2 +- gettext-tools/src/Makefile.am | 5 +- gettext-tools/src/msgmerge.c | 26 ++- gettext-tools/src/po-xerror.c | 201 -------------------- gettext-tools/src/po-xerror.h | 67 +------ gettext-tools/woe32dll/gettextsrc-exports.c | 2 - 6 files changed, 29 insertions(+), 274 deletions(-) delete mode 100644 gettext-tools/src/po-xerror.c diff --git a/gettext-tools/po/POTFILES.in b/gettext-tools/po/POTFILES.in index 92a037c52..8897b8fda 100644 --- a/gettext-tools/po/POTFILES.in +++ b/gettext-tools/po/POTFILES.in @@ -90,7 +90,6 @@ src/msgunfmt.c src/msguniq.c src/open-catalog.c src/po-charset.c -src/po-xerror.c src/read-catalog.c src/read-csharp.c src/read-desktop.c @@ -119,6 +118,7 @@ src/x-c.c src/x-csharp.c src/x-desktop.c src/x-elisp.c +src/xerror-handler.c src/xg-arglist-parser.c src/xg-check.c src/xg-encoding.c diff --git a/gettext-tools/src/Makefile.am b/gettext-tools/src/Makefile.am index c13e412cd..6d1a1c26a 100644 --- a/gettext-tools/src/Makefile.am +++ b/gettext-tools/src/Makefile.am @@ -42,7 +42,7 @@ noinst_LTLIBRARIES = libgettextsrc.la endif noinst_HEADERS = \ - pos.h message.h po-error.h po-xerror.h xerror-handler.h \ + pos.h message.h po-error.h xerror-handler.h po-xerror.h \ read-po-internal.h po-charset.h read-po-lex.h \ open-catalog.h read-catalog-special.h \ read-catalog-abstract.h read-catalog.h read-catalog-file.h \ @@ -146,8 +146,7 @@ CSHARPCOMPFLAGS = @CSHARPCOMPFLAGS@ COMMON_SOURCE = \ message.c \ pos.c \ - po-error.c \ - po-xerror.c + po-error.c if !WOE32DLL COMMON_SOURCE += xerror-handler.c else diff --git a/gettext-tools/src/msgmerge.c b/gettext-tools/src/msgmerge.c index fd7d8b4c5..8cc278bec 100644 --- a/gettext-tools/src/msgmerge.c +++ b/gettext-tools/src/msgmerge.c @@ -966,7 +966,16 @@ silent_xerror (int severity, int multiline_p, const char *message_text) { } - +static void +silent_xerror2 (int severity, + const struct message_ty *message1, + const char *filename1, size_t lineno1, size_t column1, + int multiline_p1, const char *message_text1, + const struct message_ty *message2, + const char *filename2, size_t lineno2, size_t column2, + int multiline_p2, const char *message_text2) +{ +} static message_ty * message_merge (message_ty *def, message_ty *ref, bool force_fuzzy, @@ -1481,21 +1490,22 @@ match_domain (const char *fn1, const char *fn2, /* Determine the plural distribution of the plural_expr formula. */ { /* Disable error output temporarily. */ - void (*old_po_xerror) (int, const struct message_ty *, const char *, size_t, - size_t, int, const char *) - = po_xerror; - po_xerror = silent_xerror; + unsigned int error_count = 0; + struct xerror_handler local_xerror_handler = + { + silent_xerror, + silent_xerror2, + &error_count + }; if (check_plural_eval (plural_expr, nplurals, header_entry, - &distribution, textmode_xerror_handler) > 0) + &distribution, &local_xerror_handler) > 0) { distribution.expr = NULL; distribution.often = NULL; distribution.often_length = 0; distribution.histogram = NULL; } - - po_xerror = old_po_xerror; } } diff --git a/gettext-tools/src/po-xerror.c b/gettext-tools/src/po-xerror.c deleted file mode 100644 index daa7740c3..000000000 --- a/gettext-tools/src/po-xerror.c +++ /dev/null @@ -1,201 +0,0 @@ -/* Error handling during reading and writing of PO files. - Copyright (C) 2005-2024 Free Software Foundation, Inc. - Written by Bruno Haible , 2005. - - 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 3 of the License, 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, see . */ - - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -/* Specification. */ -#include "po-xerror.h" - -#include -#include - -#include -#include "message.h" -#include "error-progname.h" -#include "xalloc.h" -#include "xerror.h" -#include "xvasprintf.h" -#include "po-error.h" -#if IN_LIBGETTEXTPO -# define program_name getprogname () -#else -# include "progname.h" -#endif -#include "gettext.h" - -#define _(str) gettext (str) - - -static void -xerror (int severity, const char *prefix_tail, - const char *filename, size_t lineno, size_t column, - int multiline_p, const char *message_text) -{ - if (multiline_p) - { - bool old_error_with_progname = error_with_progname; - char *prefix; - - if (filename != NULL) - { - if (lineno != (size_t)(-1)) - { - if (column != (size_t)(-1)) - prefix = - xasprintf ("%s:%ld:%ld: %s", filename, - (long) lineno, (long) column, prefix_tail); - else - prefix = - xasprintf ("%s:%ld: %s", filename, - (long) lineno, prefix_tail); - } - else - prefix = xasprintf ("%s: %s", filename, prefix_tail); - error_with_progname = false; - } - else - prefix = xasprintf ("%s: %s", program_name, prefix_tail); - - if (severity >= PO_SEVERITY_ERROR) - po_multiline_error (prefix, xstrdup (message_text)); - else - po_multiline_warning (prefix, xstrdup (message_text)); - error_with_progname = old_error_with_progname; - - if (severity == PO_SEVERITY_FATAL_ERROR) - exit (EXIT_FAILURE); - } - else - { - int exit_status = - (severity == PO_SEVERITY_FATAL_ERROR ? EXIT_FAILURE : 0); - - if (filename != NULL) - { - error_with_progname = false; - if (lineno != (size_t)(-1)) - { - if (column != (size_t)(-1)) - po_error (exit_status, 0, "%s:%ld:%ld: %s%s", - filename, (long) lineno, (long) column, - prefix_tail, message_text); - else - po_error_at_line (exit_status, 0, filename, lineno, "%s%s", - prefix_tail, message_text); - } - else - po_error (exit_status, 0, "%s: %s%s", - filename, prefix_tail, message_text); - error_with_progname = true; - } - else - po_error (exit_status, 0, "%s%s", prefix_tail, message_text); - if (severity < PO_SEVERITY_ERROR) - --error_message_count; - } -} - -/* The default error handler is based on the lower-level error handler - in po-error.h. */ -void -textmode_xerror (int severity, - const struct message_ty *message, - const char *filename, size_t lineno, size_t column, - int multiline_p, const char *message_text) -{ - const char *prefix_tail = - (severity == PO_SEVERITY_WARNING ? _("warning: ") : ""); - - if (message != NULL && (filename == NULL || lineno == (size_t)(-1))) - { - filename = message->pos.file_name; - lineno = message->pos.line_number; - column = (size_t)(-1); - } - - xerror (severity, prefix_tail, filename, lineno, column, - multiline_p, message_text); -} - -void -textmode_xerror2 (int severity, - const struct message_ty *message1, - const char *filename1, size_t lineno1, size_t column1, - int multiline_p1, const char *message_text1, - const struct message_ty *message2, - const char *filename2, size_t lineno2, size_t column2, - int multiline_p2, const char *message_text2) -{ - int severity1 = /* Don't exit before both texts have been output. */ - (severity == PO_SEVERITY_FATAL_ERROR ? PO_SEVERITY_ERROR : severity); - const char *prefix_tail = - (severity == PO_SEVERITY_WARNING ? _("warning: ") : ""); - - if (message1 != NULL && (filename1 == NULL || lineno1 == (size_t)(-1))) - { - filename1 = message1->pos.file_name; - lineno1 = message1->pos.line_number; - column1 = (size_t)(-1); - } - - if (message2 != NULL && (filename2 == NULL || lineno2 == (size_t)(-1))) - { - filename2 = message2->pos.file_name; - lineno2 = message2->pos.line_number; - column2 = (size_t)(-1); - } - - if (multiline_p1) - xerror (severity1, prefix_tail, filename1, lineno1, column1, multiline_p1, - message_text1); - else - { - char *message_text1_extended = xasprintf ("%s...", message_text1); - xerror (severity1, prefix_tail, filename1, lineno1, column1, - multiline_p1, message_text1_extended); - free (message_text1_extended); - } - - { - char *message_text2_extended = xasprintf ("...%s", message_text2); - xerror (severity, prefix_tail, filename2, lineno2, column2, - multiline_p2, message_text2_extended); - free (message_text2_extended); - } - - if (severity >= PO_SEVERITY_ERROR) - /* error_message_count needs to be incremented only by 1, not by 2. */ - --error_message_count; -} - -void (*po_xerror) (int severity, - const struct message_ty *message, - const char *filename, size_t lineno, size_t column, - int multiline_p, const char *message_text) - = textmode_xerror; - -void (*po_xerror2) (int severity, - const struct message_ty *message1, - const char *filename1, size_t lineno1, size_t column1, - int multiline_p1, const char *message_text1, - const struct message_ty *message2, - const char *filename2, size_t lineno2, size_t column2, - int multiline_p2, const char *message_text2) - = textmode_xerror2; diff --git a/gettext-tools/src/po-xerror.h b/gettext-tools/src/po-xerror.h index 726af227a..834ede533 100644 --- a/gettext-tools/src/po-xerror.h +++ b/gettext-tools/src/po-xerror.h @@ -1,5 +1,5 @@ /* Error handling during reading and writing of PO files. - Copyright (C) 2005-2023 Free Software Foundation, Inc. + Copyright (C) 2005-2024 Free Software Foundation, Inc. Written by Bruno Haible , 2005. This program is free software: you can redistribute it and/or modify @@ -18,65 +18,14 @@ #ifndef _PO_XERROR_H #define _PO_XERROR_H -#include +/* A thin wrapper around xerror-handler.h. */ +#include "xerror-handler.h" -#ifdef __cplusplus -extern "C" { -#endif +#define PO_SEVERITY_WARNING CAT_SEVERITY_WARNING /* just a warning, tell the user */ +#define PO_SEVERITY_ERROR CAT_SEVERITY_ERROR /* an error, the operation cannot complete */ +#define PO_SEVERITY_FATAL_ERROR CAT_SEVERITY_FATAL_ERROR /* an error, the operation must be aborted */ -struct message_ty; - - -/* A higher-level error handler than the one in po-error.h. */ - -/* These values must be the same as those in gettext-po.h. */ -#define PO_SEVERITY_WARNING 0 /* just a warning, tell the user */ -#define PO_SEVERITY_ERROR 1 /* an error, the operation cannot complete */ -#define PO_SEVERITY_FATAL_ERROR 2 /* an error, the operation must be aborted */ - -/* Signal a problem of the given severity. - MESSAGE and/or FILENAME + LINENO indicate where the problem occurred. - If FILENAME is NULL, FILENAME and LINENO and COLUMN should be ignored. - If LINENO is (size_t)(-1), LINENO and COLUMN should be ignored. - If COLUMN is (size_t)(-1), it should be ignored. - MESSAGE_TEXT is the problem description (if MULTILINE_P is true, - multiple lines of text, each terminated with a newline, otherwise - usually a single line). - Must not return if SEVERITY is PO_SEVERITY_FATAL_ERROR. */ -extern DLL_VARIABLE - void (*po_xerror) (int severity, - const struct message_ty *message, - const char *filename, size_t lineno, size_t column, - int multiline_p, const char *message_text); - -/* Signal a problem that refers to two messages. - Similar to two calls to po_xerror. - If possible, a "..." can be appended to MESSAGE_TEXT1 and prepended to - MESSAGE_TEXT2. */ -extern DLL_VARIABLE - void (*po_xerror2) (int severity, - const struct message_ty *message1, - const char *filename1, size_t lineno1, size_t column1, - int multiline_p1, const char *message_text1, - const struct message_ty *message2, - const char *filename2, size_t lineno2, size_t column2, - int multiline_p2, const char *message_text2); - -/* The default error handler. */ -extern void textmode_xerror (int severity, - const struct message_ty *message, - const char *filename, size_t lineno, size_t column, - int multiline_p, const char *message_text); -extern void textmode_xerror2 (int severity, - const struct message_ty *message1, - const char *filename1, size_t lineno1, size_t column1, - int multiline_p1, const char *message_text1, - const struct message_ty *message2, - const char *filename2, size_t lineno2, size_t column2, - int multiline_p2, const char *message_text2); - -#ifdef __cplusplus -} -#endif +#define po_xerror (textmode_xerror_handler->xerror) +#define po_xerror2 (textmode_xerror_handler->xerror2) #endif /* _PO_XERROR_H */ diff --git a/gettext-tools/woe32dll/gettextsrc-exports.c b/gettext-tools/woe32dll/gettextsrc-exports.c index 79cbac622..8d487a648 100644 --- a/gettext-tools/woe32dll/gettextsrc-exports.c +++ b/gettext-tools/woe32dll/gettextsrc-exports.c @@ -75,8 +75,6 @@ VARIABLE(po_error) VARIABLE(po_error_at_line) VARIABLE(po_multiline_error) VARIABLE(po_multiline_warning) -VARIABLE(po_xerror) -VARIABLE(po_xerror2) VARIABLE(syntax_check_name) VARIABLE(textmode_xerror_handler_struct) VARIABLE(use_first) -- 2.47.3