Skip to content

Commit

Permalink
Provide better diagnostic information and exit statuses in texml wrap…
Browse files Browse the repository at this point in the history
…per. Added termination_status and termination_message attributes to TeX::Interpreter and reworked raw die statements into fatal_error's.
  • Loading branch information
davidmjones committed Dec 4, 2024
1 parent e3c0811 commit 91d6bdc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 64 deletions.
74 changes: 34 additions & 40 deletions bin/texml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use strict;
use warnings;

use version; our $VERSION = qv '1.3.5';
use version; our $VERSION = qv '1.4.0';

binmode STDERR, ":utf8";
binmode STDOUT, ":utf8";
Expand Down Expand Up @@ -112,7 +112,7 @@ our %OPT = (debug => 0,
## ##
######################################################################

sub usage() {
sub usage {
my $usage = << "EOF";
Usage: $PROGRAM_NAME [options] filename
Expand All @@ -128,15 +128,15 @@ EOF
exit 0;
}

sub init_config() {
sub init_config {
return if defined $CFG;

$CFG = TeXML::CFG->get_cfg( { cfg_file => $OPT{cfg_file} } );

return;
}

sub list_cfg() {
sub list_cfg {
if (! defined $CFG) {
print "No config file loaded\n";

Expand All @@ -152,7 +152,7 @@ sub list_cfg() {
exit 0;
}

sub use_xetex( $ ) {
sub use_xetex {
my $tex_file = shift;

open(my $fh, "<", $tex_file) or do {
Expand Down Expand Up @@ -221,7 +221,7 @@ sub xml_pp {
return;
}

sub process_file( $ ) {
sub process_file {
my $tex_file = shift;

my $use_xetex = $OPT{use_xetex} || use_xetex($tex_file);
Expand All @@ -246,42 +246,38 @@ sub process_file( $ ) {
# $ENV{TEXINPUTS} = ".:$dirname//:";
# }

my $convert = TeX::Interpreter::LaTeX->new({ fmt => 'latex',
unicode_input => $OPT{utf8},
do_svg => $OPT{do_svg},
use_xetex => $use_xetex,
debug => $OPT{debug},
profiling => $OPT{profiling},
job_name => $OPT{job_name},
});
my $TeX = TeX::Interpreter::LaTeX->new({ fmt => 'latex',
unicode_input => $OPT{utf8},
do_svg => $OPT{do_svg},
use_xetex => $use_xetex,
debug => $OPT{debug},
profiling => $OPT{profiling},
job_name => $OPT{job_name},
});

$convert->set_use_mathjax(1);
$TeX->set_use_mathjax(1);

eval { $convert->TeX($tex_file) };
my $status = eval { $TeX->TeX($tex_file) };

if ($@) {
print STDERR "$@\n";
if ($@ || $status) {
my $msg = $@ || $TeX->get_termination_message();

print STDERR "No XML was generated: $msg\n";

## This is insurance in case something kills us without
## setting a termination status.

$status ||= 1;

exit $status;
}

if ($OPT{pretty_print}) {
my $xml_file = $convert->get_output_file_name();

# Preserve spaces inside the name/suffix element so
# <suffix><x>, </x>Jr.</suffix> doesn't get turned into
#
# <suffix>
# <x>, </x>Jr.</suffix>
#
# Surprisingly, this is the only place this problem has popped
# up so far.
my $xml_file = $TeX->get_output_file_name();

print "\nPretty-printing $xml_file\n";

eval { xml_pp($xml_file) };

if ($@) {
print "Could not pretty-print file: $@\n";
}
xml_pp($xml_file);
}

return;
Expand Down Expand Up @@ -313,7 +309,7 @@ if ($OPT{list_cfg}) {
list_cfg();
}

usage() unless @ARGV;
usage() unless @ARGV == 1;

my $extra_lib = $CFG->val($PROGRAM_NAME, 'extra_lib');

Expand All @@ -331,14 +327,12 @@ if ($OPT{debug}) {
$ENV{TMPDIR} = getcwd();
}

while (my $tex_file = shift @ARGV) {
do { warn "Can't find $tex_file\n"; next; } unless -e $tex_file;
my $tex_file = shift @ARGV;

do { warn "Can't read $tex_file\n"; next; } unless -r $tex_file;
die "Can't find $tex_file\n" unless -e $tex_file;

process_file($tex_file);
die "Can't read $tex_file\n" unless -r $tex_file;

print "\n" if @ARGV;
}
process_file($tex_file);

__END__
63 changes: 39 additions & 24 deletions lib/perl/TeX/Interpreter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ my %fmt_file_of :ATTR(:name<fmt_file>);

my %interaction_mode_of :ATTR(:name<interaction_mode> :default<error_stop_mode>);

my %termination_status_of :COUNTER(:name<termination_status> :default<0>);
my %termination_message_of :ATTR(:name<termination_message>);

my %cur_font_of :ATTR(:name<cur_font> :default(-1));
my %cur_lang_of :ATTR(:name<cur_lang> :set<*custom*> :default(0)); #*
my %cur_enc_of :ATTR;
Expand All @@ -214,8 +217,6 @@ my %debug_of :BOOLEAN(:name<debug> :default<0>);

my %nofiles_of :BOOLEAN(:name<nofiles> :get<nofiles> :default<false>);

my %newlines_per_par_of :COUNTER(:name<newlines_per_par> :default<2>);

my %unicode_input_of :BOOLEAN(:name<unicode_input> :default<false>);
my %unicode_output_of :BOOLEAN(:name<unicode_output> :default<true>);

Expand Down Expand Up @@ -840,9 +841,7 @@ sub initialize_print_selector {
sub jump_out {
my $tex = shift;

$tex->end_of_TEX();

return;
return $tex->end_of_TEX();
}

sub error_message {
Expand Down Expand Up @@ -978,15 +977,19 @@ sub succumb {
sub fatal_error {
my $tex = shift;

my $s = shift;
my $message = shift;
my $status = @_ ? shift : 1;

$tex->set_termination_status($status);
$tex->set_termination_message($message);

$tex->normalize_selector();

$tex->print_err("Emergency stop");

$tex->print_err($s);
$tex->print_err($message);

$tex->set_help($s);
$tex->set_help($message);

$tex->succumb();

Expand Down Expand Up @@ -2452,7 +2455,9 @@ sub load_primitive( $;$ ) {
}
}

die "Could not load primitive '$class_name'\n" unless defined $primitive;
if (! defined $primitive) {
$tex->fatal_error("Could not load primitive '$class_name'");
}
}

return $primitive;
Expand Down Expand Up @@ -4024,7 +4029,7 @@ sub finish_line {
} elsif ($state == skip_blanks) {
# no-op
} else {
die "Invalid lexer state: $state";
$tex->fatal_error("Invalid lexer state: $state");
}

return $cur_tok;
Expand All @@ -4046,7 +4051,7 @@ sub process_space {

return SPACE_TOKEN;
} else {
die "Invalid lexer state '$state'";
$tex->fatal_error("Invalid lexer state '$state'");
}
}

Expand Down Expand Up @@ -6389,7 +6394,7 @@ sub load_output_module {
my $class = shift;

if (! eval "require $class") {
die "Could not load output class '$class': $@\n";
$tex->fatal_error("Could not load output class '$class': $@");
}

return;
Expand Down Expand Up @@ -6446,7 +6451,9 @@ sub ensure_output_open {
## Might want to do something more elaborate here. Otherwise,
## delete the following

if ($@) { die $@ };
if ($@) {
$tex->fatal_error($@);
};

$tex->set_output_handle($handle);

Expand Down Expand Up @@ -6483,7 +6490,7 @@ sub open_log_file {
## NEEDS IMPROVEMENT

my $fh = $tex->a_open_out($log_name) or do {
die "Can't open log file $log_name: $!\n";
$tex->fatal_error("Can't open log file $log_name: $!");

# @<Try to get a different log file name@>;
};
Expand Down Expand Up @@ -6712,7 +6719,9 @@ sub push_output {
## Might want to do something more elaborate here. Otherwise,
## delete the following

if ($@) { die $@ };
if ($@) {
$tex->fatal_error("Unable to initialize output: $@");
};

$tex->set_output_handle($handle);
$tex->set_output_module($output_module);
Expand Down Expand Up @@ -6854,10 +6863,20 @@ sub finish_output_file {

my $dom = $fh->close_document();

if (nonempty(my $output_file_name = $tex->get_output_file_name())) {
if ($output_file_name ne DEV_NULL) {
my $state = $dom->toFile($output_file_name, 1) if defined $dom;
if (defined $dom) {
if (nonempty(my $output_file_name = $tex->get_output_file_name())) {
if ($output_file_name ne DEV_NULL) {
eval { $dom->toFile($output_file_name, 1) };

if ($@) {
$tex->set_termination_message("Could not create $output_file_name: $@");
$tex->set_termination_status(1);
}
}
}
} else {
$tex->set_termination_message("No DOM was created!");
$tex->set_termination_status(1);
}

$tex->delete_output_handle();
Expand Down Expand Up @@ -10898,19 +10917,15 @@ sub TeX {

$tex->final_cleanup(); # { prepare for death }

$tex->end_of_TEX();

return;
return $tex->end_of_TEX();
}

sub end_of_TEX {
my $tex = shift;

$tex->close_files_and_terminate();

# exit;

return;
return $tex->termination_status();
}

sub close_files_and_terminate {
Expand Down

0 comments on commit 91d6bdc

Please sign in to comment.