]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
its: Improve error handling.
authorBruno Haible <bruno@clisp.org>
Sun, 13 Oct 2024 01:00:35 +0000 (03:00 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 13 Oct 2024 01:01:35 +0000 (03:01 +0200)
* 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.

gettext-tools/src/its.c

index edff70d60128969becb2cce66bd9b03c4f7cadae..7b676542d119c50a4c3390a803a0cb5482b5677f 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#include <libxml/xmlerror.h>
 #include <libxml/tree.h>
 #include <libxml/parser.h>
 #include <libxml/xmlwriter.h>
 
 /* =================== 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