From: Bruno Haible Date: Sun, 13 Oct 2024 01:00:35 +0000 (+0200) Subject: its: Improve error handling. X-Git-Tag: v0.23~68 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c2e6408656ee2ae9a681bd24aae1e0f719ee4109;p=thirdparty%2Fgettext.git its: Improve error handling. * gettext-tools/src/its.c: Include libxml/xmlerror.h. (structured_error): New function. (its_rule_list_add_from_file, its_rule_list_add_from_string, its_rule_list_extract, its_merge_context_alloc): Exit upon fatal error. Set the structured_error for the rest of the function. (its_merge_context_merge, its_merge_context_write): Set the structured_error. --- diff --git a/gettext-tools/src/its.c b/gettext-tools/src/its.c index edff70d60..7b676542d 100644 --- a/gettext-tools/src/its.c +++ b/gettext-tools/src/its.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -72,6 +73,15 @@ /* =================== Common API for xgettext and msgfmt =================== */ +/* ----------------------------- Error handling ----------------------------- */ + +static void +structured_error (void *data, xmlError *err) +{ + error (0, err->level == XML_ERR_FATAL ? EXIT_FAILURE : 0, + _("%s error: %s"), "libxml2", err->message); +} + /* --------------------------------- Values --------------------------------- */ struct its_value_ty @@ -1542,12 +1552,17 @@ its_rule_list_add_from_file (struct its_rule_list_ty *rules, if (doc == NULL) { const xmlError *err = xmlGetLastError (); - error (0, 0, _("cannot read %s: %s"), filename, err->message); + error (0, err->level == XML_ERR_FATAL ? EXIT_FAILURE : 0, + _("cannot read %s: %s"), filename, err->message); return false; } + xmlSetStructuredErrorFunc (NULL, structured_error); + result = its_rule_list_add_from_doc (rules, doc); xmlFreeDoc (doc); + + xmlSetStructuredErrorFunc (NULL, NULL); return result; } @@ -1568,12 +1583,17 @@ its_rule_list_add_from_string (struct its_rule_list_ty *rules, if (doc == NULL) { const xmlError *err = xmlGetLastError (); - error (0, 0, _("cannot read %s: %s"), "(internal)", err->message); + error (0, err->level == XML_ERR_FATAL ? EXIT_FAILURE : 0, + _("cannot read %s: %s"), "(internal)", err->message); return false; } + xmlSetStructuredErrorFunc (NULL, structured_error); + result = its_rule_list_add_from_doc (rules, doc); xmlFreeDoc (doc); + + xmlSetStructuredErrorFunc (NULL, NULL); return result; } @@ -1976,10 +1996,13 @@ its_rule_list_extract (its_rule_list_ty *rules, if (doc == NULL) { const xmlError *err = xmlGetLastError (); - error (0, 0, _("cannot read %s: %s"), logical_filename, err->message); + error (0, err->level == XML_ERR_FATAL ? EXIT_FAILURE : 0, + _("cannot read %s: %s"), logical_filename, err->message); return; } + xmlSetStructuredErrorFunc (NULL, structured_error); + its_rule_list_apply (rules, doc); memset (&nodes, 0, sizeof (struct its_node_list_ty)); @@ -1995,6 +2018,8 @@ its_rule_list_extract (its_rule_list_ty *rules, free (nodes.items); xmlFreeDoc (doc); + + xmlSetStructuredErrorFunc (NULL, NULL); } @@ -2269,11 +2294,15 @@ its_merge_context_merge (its_merge_context_ty *context, { size_t i; + xmlSetStructuredErrorFunc (NULL, structured_error); + for (i = 0; i < context->nodes.nitems; i++) its_merge_context_merge_node (context, context->nodes.items[i], language, mlp, replace_text); + + xmlSetStructuredErrorFunc (NULL, NULL); } struct its_merge_context_ty * @@ -2291,10 +2320,13 @@ its_merge_context_alloc (its_rule_list_ty *rules, if (doc == NULL) { const xmlError *err = xmlGetLastError (); - error (0, 0, _("cannot read %s: %s"), filename, err->message); + error (0, err->level == XML_ERR_FATAL ? EXIT_FAILURE : 0, + _("cannot read %s: %s"), filename, err->message); return NULL; } + xmlSetStructuredErrorFunc (NULL, structured_error); + its_rule_list_apply (rules, doc); result = XMALLOC (struct its_merge_context_ty); @@ -2307,6 +2339,7 @@ its_merge_context_alloc (its_rule_list_ty *rules, &result->nodes, xmlDocGetRootElement (result->doc)); + xmlSetStructuredErrorFunc (NULL, NULL); return result; } @@ -2314,7 +2347,11 @@ void its_merge_context_write (struct its_merge_context_ty *context, FILE *fp) { + xmlSetStructuredErrorFunc (NULL, structured_error); + xmlDocFormatDump (fp, context->doc, 1); + + xmlSetStructuredErrorFunc (NULL, NULL); } void