bug-texinfo
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Request for feedback on texi2any customization file


From: Gavin Smith
Subject: Re: Request for feedback on texi2any customization file
Date: Sat, 2 Sep 2023 09:03:10 +0100

On Sat, Sep 02, 2023 at 12:22:52AM +0100, Gavin Smith wrote:
> Here's the change I'm looking at currently:

The change with futher work is below.

It may be difficult to get the encoding status of the text right:

$ cat warn2.init 
warn "Datei konnte nicht geöffnet werden";

$ ../tp/texi2any.pl --init-file warn2.init test.texi 
texi2any: warning: Datei konnte nicht geöffnet werden at ./warn2.init line 1.

The following is ok, though, with the utf8 pragma to mark text as encoded:

$ cat warn3.init 
use utf8;
warn "Datei konnte nicht geöffnet werden";
$ ../tp/texi2any.pl --init-file warn3.init test.texi 
texi2any: warning: Datei konnte nicht geöffnet werden at ./warn3.init line 2.

As the existing comment already implies, it's not clear whether Perl warning
messages come pre-encoded or not.  I haven't been able to get Perl to output
an inbuilt warning with a non-ASCII character yet; that would be one thing
to check.

We could not bother capturing warnings and just let them output by default
but the benefit of capturing them is that we can prefix them with the
program name "texi2any", as well as possibly terminating if we want to
treat warnings as fatal.

I still need to investigate any differences between "do" and "require" in
where the files are looked for.


diff --git a/tp/Texinfo/Config.pm b/tp/Texinfo/Config.pm
index 47082541fa..5995845a3e 100644
--- a/tp/Texinfo/Config.pm
+++ b/tp/Texinfo/Config.pm
@@ -121,23 +121,60 @@ sub _GNUT_document_warn($) {
                    "%s: warning: %s"), $real_command_name, $text)."\n"));
 }
 
+sub _GNUT_document_fatal($) {
+  my $text = shift;
+  chomp ($text);
+  die(_GNUT_encode_message(
+        sprintf(__p("program name: error_message",
+                   "%s: %s"), $real_command_name, $text)."\n"));
+}
+
 # used to register messages by the user with texinfo_register_init_loading_*
 my @init_file_loading_messages;
+
+# used in GNUT_load_init_file as list of warnings
+my @module_warnings;
+
 # called from texi2any.pl main program and t/test_utils.pl.
 # eval $FILE in the Texinfo::Config namespace. $FILE should be a binary string.
 sub GNUT_load_init_file($) {
   my $file = shift;
   push @init_file_loading_messages, [];
-  eval { require($file) ;};
-  my $e = $@;
-  if ($e ne '') {
-    # $e may not be correctly encoded, but it is not clear what is to
-    # be expected.  In the eval documentation there is only information
-    # on the string eval case, not the block eval used here, and the
-    # information is not particularly clear.
-    _GNUT_document_warn(sprintf(__("error loading %s: %s"),
-                _GNUT_decode_input($file), $e));
+
+  @module_warnings = ();
+  sub _save_warning {
+    my $warning = shift;
+    push @module_warnings, $warning;
+
   }
+
+  local $SIG{__WARN__} = \&_save_warning;
+  my $result = do($file);
+  my $message = $@;
+  my $read_error = $!;
+  local $SIG{__WARN__} = undef;
+
+  if (!defined($result)) {
+    if (defined($message)) {
+      _GNUT_document_fatal(sprintf
+                 (__("error parsing %s: %s"),
+                  _GNUT_decode_input($file), $message));
+    } elsif (defined($read_error)) {
+      _GNUT_document_fatal(sprintf
+                 (__("error reading %s: %s"),
+                  _GNUT_decode_input($file), $read_error));
+    }
+  }
+
+  # Print any saved warnings, if any.  We don't need to print the name
+  # of the input file as the warnings already include it.
+  for my $warning (@module_warnings) {
+    _GNUT_document_warn($warning);
+  }
+
+  # Note: $message, $read_error and @module_warnings may not be correctly
+  # encoded if they are set as already-encoded strings.
+
   my $file_loading_messages = pop @init_file_loading_messages;
   my $error_nr = 0;
   for my $error (@{$file_loading_messages}) {





reply via email to

[Prev in Thread] Current Thread [Next in Thread]