| 1 |
#!/usr/bin/perl -T |
| 2 |
|
| 3 |
# The content of the following variable is configuration data: |
| 4 |
@DiscoverMacro::datafile_path = ('program','.'); |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
discovermacro.pl - Discover which LaTeX package contains a missing macro |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
B<discovermacro.pl> \I<themissingmacro> |
| 13 |
|
| 14 |
B<discovermacro.pl> I<mydocument.log> |
| 15 |
|
| 16 |
B<discovermacro.pl> |
| 17 |
|
| 18 |
=head1 DESCRIPTION |
| 19 |
|
| 20 |
The ucs package uses many macros from many packages which have to be |
| 21 |
included with B<\usepackage> before the inclusion of F<ucs.sty>. |
| 22 |
|
| 23 |
The missing macro can be given directly as parameter. Note that the |
| 24 |
backslash must be included, so with most shells you either need to |
| 25 |
quote it or write a double backslash (e. g. \\cyrc or '\cyrc'). |
| 26 |
|
| 27 |
An alternative way is to give a LaTeX log file as parameter in which |
| 28 |
case the missing macros are parsed from it. |
| 29 |
|
| 30 |
If no argument is given, the newest LaTeX log file in the current |
| 31 |
directory is used as default. |
| 32 |
|
| 33 |
=head1 ALTERNATIVES |
| 34 |
|
| 35 |
B<discovermacro.pl> uses the data file F<ltxmacrs.txt> which is human |
| 36 |
readable, thus can be used directly. |
| 37 |
|
| 38 |
=cut |
| 39 |
|
| 40 |
use strict; |
| 41 |
|
| 42 |
package DiscoverMacro::H; |
| 43 |
|
| 44 |
sub cleanline($) { |
| 45 |
my $line = shift; |
| 46 |
chomp $line; |
| 47 |
$line =~ s/^\s*//; |
| 48 |
$line =~ s/^\#.*//; |
| 49 |
return $line; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
sub readline2($) { |
| 54 |
my $file = shift; |
| 55 |
my $line = <$file>; |
| 56 |
while ($line =~ /\\\n?$/s) { |
| 57 |
$line =~ s/\\\n?$//; |
| 58 |
my $next = <$file>; |
| 59 |
$next =~ s/^\s*//; |
| 60 |
$line .= $next; |
| 61 |
} |
| 62 |
return $line; |
| 63 |
} |
| 64 |
|
| 65 |
sub escape($$) { |
| 66 |
my ($format,$text) = @_; |
| 67 |
if ($format eq 'text/plain') { |
| 68 |
$text =~ s/\[\[\[.*?\]\]\]//g; |
| 69 |
} elsif ($format eq 'text/html') { |
| 70 |
$text =~ s{[<>&]|(\n[ \t]*)}{ my $x=$&; |
| 71 |
if ($x =~ /^\n/) { |
| 72 |
$x=~s/\t/ /g; |
| 73 |
$x=~s/ / /g; |
| 74 |
"<br>$x"; |
| 75 |
} else { |
| 76 |
sprintf "&#%d;", ord $& }}egs; |
| 77 |
$text =~ s{\[\[\[.+?\]\]\]}{ |
| 78 |
my ($t) = ($& =~ /\[\[\[(.+?)\]\]\]/); |
| 79 |
my @t = split / /,$t; |
| 80 |
my $r = ''; |
| 81 |
if ($t[0] eq 'NAME') { |
| 82 |
$r = "<a name=\"$t[1]\">"; |
| 83 |
} elsif ($t[0] eq 'HREF') { |
| 84 |
$r = "<a href=\"$t[1]\">"; |
| 85 |
} elsif ($t[0] eq '/NAME') { |
| 86 |
$r = "</a>"; |
| 87 |
} elsif ($t[0] eq '/HREF') { |
| 88 |
$r = "</a>"; |
| 89 |
} else { |
| 90 |
warn "escape($format,[[[$t[0]...]]])"; |
| 91 |
} |
| 92 |
}ge; |
| 93 |
return $text; |
| 94 |
} elsif ($format eq 'identifier') { |
| 95 |
$text =~ s/[^a-z0-9]/ |
| 96 |
sprintf "_%02X", ord $&/ge; |
| 97 |
} else { |
| 98 |
warn "Unknown format $format"; |
| 99 |
return escape('text/plain',$text); |
| 100 |
} |
| 101 |
return $text; |
| 102 |
} |
| 103 |
|
| 104 |
sub header($$) { |
| 105 |
my ($format,$header) = @_; |
| 106 |
$header = escape($format,$header); |
| 107 |
if ($format eq 'text/plain') { |
| 108 |
my $len = length $header; |
| 109 |
return "\n$header\n".('=' x $len)."\n"; |
| 110 |
} elsif ($format eq 'text/html') { |
| 111 |
return "<p><table border=1>\n<tr><th align=left colspan=2>$header</th></tr>\n"; |
| 112 |
} else { |
| 113 |
warn "Unknown format $format"; |
| 114 |
return header('text/plain',$header); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
sub footer($) { |
| 119 |
my ($format) = @_; |
| 120 |
if ($format eq 'text/plain') { |
| 121 |
return "\n"; |
| 122 |
} elsif ($format eq 'text/html') { |
| 123 |
return "</table></p>\n\n"; |
| 124 |
} else { |
| 125 |
warn "Unknown format $format"; |
| 126 |
return footer('text/plain'); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
sub line($$) { |
| 131 |
my ($format,$line) = @_; |
| 132 |
$line = escape $format, $line; |
| 133 |
if ($format eq 'text/plain') { |
| 134 |
return "$line\n"; |
| 135 |
} elsif ($format eq 'text/html') { |
| 136 |
return "<tr><td colspan=2>$line</td></tr>\n"; |
| 137 |
} else { |
| 138 |
warn "Unknown format $format"; |
| 139 |
return line($format,$line); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
sub twocol($$$) { |
| 145 |
my ($format,$col1,$col2) = @_; |
| 146 |
$col1 = escape $format, $col1; |
| 147 |
$col2 = escape $format, $col2; |
| 148 |
if ($format eq 'text/plain') { |
| 149 |
return "$col1\t$col2\n"; |
| 150 |
} elsif ($format eq 'text/html') { |
| 151 |
return "<tr><td>$col1 </td><td>$col2</td></tr>\n"; |
| 152 |
} else { |
| 153 |
warn "Unknown format $format"; |
| 154 |
return twocol('text/plain',$col1,$col2); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
package DiscoverMacro::Feature; |
| 160 |
|
| 161 |
use vars qw/$obj_count/; |
| 162 |
$obj_count = 0; |
| 163 |
|
| 164 |
sub new($$$) { |
| 165 |
my ($proto,$db,$name) = @_; |
| 166 |
$obj_count++; |
| 167 |
my $class = ref($proto) || $proto; |
| 168 |
my $self = {}; |
| 169 |
bless $self, $class; |
| 170 |
$self->{name} = $name; |
| 171 |
$self->{db} = $db; |
| 172 |
$self->loadfeature(); |
| 173 |
return $self; |
| 174 |
} |
| 175 |
|
| 176 |
sub DESTROY { |
| 177 |
$obj_count--; |
| 178 |
} |
| 179 |
|
| 180 |
sub getfontenc($) { |
| 181 |
my $self = shift; |
| 182 |
return $self->{fontencoding}; |
| 183 |
} |
| 184 |
|
| 185 |
sub getlatexcmd($) { |
| 186 |
$_[0]->{latexcmd}; |
| 187 |
} |
| 188 |
|
| 189 |
sub getneeds($) { |
| 190 |
my $self = shift; |
| 191 |
return $self->{needs}; |
| 192 |
} |
| 193 |
|
| 194 |
sub getpackages($) { |
| 195 |
my $self = shift; |
| 196 |
return $self->{packages}; |
| 197 |
} |
| 198 |
|
| 199 |
sub getpackages_ascode($) { |
| 200 |
my $self = shift; |
| 201 |
return join '', map { |
| 202 |
my $p = $_; |
| 203 |
$p = "{$_}" unless $p =~ /\}$/; |
| 204 |
"\\usepackage$p\n"; |
| 205 |
} @{$self->{packages}}; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
sub loadfeature($) { |
| 210 |
my $self = shift; |
| 211 |
my $pos = $self->{db}->getfeaturepos($self->{name}); |
| 212 |
unless (defined $pos) { |
| 213 |
$self->{undefined} = 1; |
| 214 |
return; |
| 215 |
} |
| 216 |
my $file = $self->{db}->getfileat($pos); |
| 217 |
my $line; |
| 218 |
my $lastfile = undef; |
| 219 |
while (defined ($line = DiscoverMacro::H::readline2($file))) { |
| 220 |
my $tlastfile; |
| 221 |
$line = DiscoverMacro::H::cleanline $line; |
| 222 |
next if $line eq ''; |
| 223 |
my @line = split ' ',$line; |
| 224 |
if ($line[0] eq 'FONTENCODING') { |
| 225 |
warn "Two fontencodings in feature $self->{name}" |
| 226 |
if defined $self->{fontencoding}; |
| 227 |
$self->{fontencoding} = $line[1]; |
| 228 |
} elsif ($line[0] eq 'CTAN') { |
| 229 |
if (!defined $lastfile) { |
| 230 |
die "CTAN not preceded by FILE in feature $self->{name}"; } |
| 231 |
my $ctan = $self->{ctan}; |
| 232 |
warn "Two CTAN locations defined for file $lastfile ". |
| 233 |
"in feature $self->{name}" if defined $$ctan{$lastfile}; |
| 234 |
$$ctan{$lastfile} = $line[1]; |
| 235 |
$self->{ctan} = $ctan; |
| 236 |
} elsif ($line[0] eq 'LATEXCMD') { |
| 237 |
warn "Two LATEXCMDs in feature $self->{name}" |
| 238 |
if defined $self->{latexcmd}; |
| 239 |
$self->{latexcmd} = $line[1]; |
| 240 |
} elsif ($line[0] eq 'NEEDS') { |
| 241 |
warn "Two NEEDS lines in feature $self->{name}" |
| 242 |
if defined $self->{needs}; |
| 243 |
$self->{needs} = join ' ',@line[1..$#line]; |
| 244 |
} elsif ($line[0] eq 'END') { |
| 245 |
warn "FEATURE $self->{name} ended by END $line[1]" |
| 246 |
if $line[1] ne 'FEATURE'; |
| 247 |
last; |
| 248 |
} elsif ($line[0] eq 'COMMENT') { |
| 249 |
my $comment = readblock($file,'COMMENT',1); |
| 250 |
if (defined $self->{comment}) { |
| 251 |
warn "Two COMMENT sections in feature $self->{name}.". |
| 252 |
"Concatenating"; |
| 253 |
$comment = "$self->{comment}$comment"; } |
| 254 |
$self->{comment} = $comment; |
| 255 |
} elsif ($line[0] eq 'INSTALL') { |
| 256 |
my $install = readblock($file,'INSTALL',1); |
| 257 |
if (defined $self->{install}) { |
| 258 |
warn "Two INSTALL sections in feature $self->{name}.". |
| 259 |
"Concatenating"; |
| 260 |
$install = "$self->{install}$install"; } |
| 261 |
$self->{install} = $install; |
| 262 |
} elsif ($line[0] eq 'FILE') { |
| 263 |
push @{$self->{files}}, $line[1]; |
| 264 |
$tlastfile = $line[1]; |
| 265 |
} elsif ($line[0] eq 'PACKAGE') { |
| 266 |
push @{$self->{packages}}, $line[1]; |
| 267 |
} else { |
| 268 |
warn "Unknown command in feature $self->{name}: $line[0]"; |
| 269 |
} |
| 270 |
$lastfile = $tlastfile; $tlastfile = undef; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
sub readblock($$$) { |
| 275 |
my ($file,$blocktype,$raw) = @_; |
| 276 |
my $line; |
| 277 |
my $str = ''; |
| 278 |
while (defined ($line = $raw?<$file>:DiscoverMacro::H::readline2($file))) { |
| 279 |
return $str if ($line =~ /^\s*END\s+\Q$blocktype\E\s*$/); |
| 280 |
$line = DiscoverMacro::H::cleanline $line unless $raw; |
| 281 |
#print "L:$line\n"; |
| 282 |
$str .= $line; |
| 283 |
} |
| 284 |
warn "EOF in $blocktype"; |
| 285 |
return $str; |
| 286 |
} |
| 287 |
|
| 288 |
sub isdefined($) { |
| 289 |
my $self = shift; |
| 290 |
return !$self->{undefined}; |
| 291 |
} |
| 292 |
|
| 293 |
sub as_text($$) { |
| 294 |
my ($self,$f) = @_; |
| 295 |
my $link = "[[[NAME ". |
| 296 |
DiscoverMacro::H::escape('identifier',$self->{name})."]]]"; |
| 297 |
my $text = DiscoverMacro::H::header |
| 298 |
($f,"${link}Feature: $self->{name}\[[[/NAME]]]"); |
| 299 |
if ($self->{undefined}) { |
| 300 |
$text .= DiscoverMacro::H::line($f,"No information available"); |
| 301 |
} |
| 302 |
if (defined $self->{fontencoding}) { |
| 303 |
$text .= DiscoverMacro::H::twocol($f,"Fontencoding:", |
| 304 |
$self->{fontencoding}); |
| 305 |
} |
| 306 |
for my $i (@{$self->{packages}}) { |
| 307 |
$text .= DiscoverMacro::H::twocol($f,"Package:",$i); |
| 308 |
} |
| 309 |
if (defined $self->{needs}) { |
| 310 |
$text .= DiscoverMacro::H::twocol($f,"Needs:",$self->{needs}); |
| 311 |
} |
| 312 |
for my $i (@{$self->{files}}) { |
| 313 |
my $ctan = ${$self->{ctan}}{$i}; |
| 314 |
if (defined $ctan) { |
| 315 |
$ctan = " (CTAN: [[[HREF http://www.ctan.org/tex-archive/". |
| 316 |
"$ctan]]]$ctan\[[[/HREF]]])"; |
| 317 |
} else { $ctan = ''; }; |
| 318 |
$text .= DiscoverMacro::H::twocol($f,"Needed file:",$i.$ctan); |
| 319 |
} |
| 320 |
if ($self->{comment}) { |
| 321 |
$text .= DiscoverMacro::H::twocol($f,"Comment:",$self->{comment}); |
| 322 |
} |
| 323 |
if ($self->{install}) { |
| 324 |
$text .= DiscoverMacro::H::twocol($f,"Installation:",$self->{install}); |
| 325 |
} |
| 326 |
$text .= DiscoverMacro::H::footer $f; |
| 327 |
return $text; |
| 328 |
} |
| 329 |
|
| 330 |
package DiscoverMacro::Macro; |
| 331 |
|
| 332 |
use Data::Dumper; |
| 333 |
|
| 334 |
use vars qw/$obj_count/; |
| 335 |
$obj_count = 0; |
| 336 |
|
| 337 |
sub new($$$) { |
| 338 |
my ($proto,$db,$macro) = @_; |
| 339 |
my $class = ref($proto) || $proto; |
| 340 |
my $self = {}; |
| 341 |
$obj_count++; |
| 342 |
bless $self, $class; |
| 343 |
$self->{macro} = $macro; |
| 344 |
$self->{db} = $db; |
| 345 |
$self->loadlist(); |
| 346 |
return $self; |
| 347 |
} |
| 348 |
|
| 349 |
sub DESTROY { |
| 350 |
$obj_count--; |
| 351 |
} |
| 352 |
|
| 353 |
sub isdefined($) { |
| 354 |
my $self = shift; |
| 355 |
return !$self->{undefined}; |
| 356 |
} |
| 357 |
|
| 358 |
sub getname($) { |
| 359 |
my $self = shift; |
| 360 |
return $self->{macro}; |
| 361 |
} |
| 362 |
|
| 363 |
sub getlist($) { |
| 364 |
my $self = shift; |
| 365 |
return $self->{list}; |
| 366 |
} |
| 367 |
|
| 368 |
sub loadlist($) { |
| 369 |
my $self = shift; |
| 370 |
my $macros = $self->{db}->{macros}; |
| 371 |
my $macro = $self->{macro}; |
| 372 |
my $list = $$macros{$macro}; |
| 373 |
unless (defined $list) { |
| 374 |
my $regex = $self->{db}->{regexmacros}; |
| 375 |
while (my ($re,$l) = each %$regex) { |
| 376 |
#print "RE: $re\n"; |
| 377 |
if ($macro =~ /$re/) { |
| 378 |
$self->{regex} = $re; |
| 379 |
$list = $l; last; } |
| 380 |
} |
| 381 |
} |
| 382 |
unless (defined $list) { |
| 383 |
$self->{undefined} = 1; |
| 384 |
return; |
| 385 |
} |
| 386 |
$self->{list} = $list; |
| 387 |
my %tlist; |
| 388 |
my @list = grep { my $bad = $_ eq 'OR' || $_ eq 'AND' || $tlist{$_}; |
| 389 |
$tlist{$_}=1; !$bad; } split ' ', $self->{list}; |
| 390 |
my %features = (); |
| 391 |
for my $i (@list) { |
| 392 |
$features{$i} = new DiscoverMacro::Feature($self->{db},$i) |
| 393 |
unless defined $features{$i}; |
| 394 |
} |
| 395 |
$self->{features} = \%features; |
| 396 |
} |
| 397 |
|
| 398 |
sub getfeature($) { |
| 399 |
my ($self,$name) = @_; |
| 400 |
return ${$self->{features}}{$name}; |
| 401 |
} |
| 402 |
|
| 403 |
sub as_text($$) { |
| 404 |
my ($self,$f) = @_; |
| 405 |
my $text = ''; |
| 406 |
my $link = "[[[NAME ". |
| 407 |
DiscoverMacro::H::escape('identifier',$self->{macro})."]]]"; |
| 408 |
$text .= DiscoverMacro::H::header |
| 409 |
($f,"${link}Macro: $self->{macro}\[[[/NAME]]]"); |
| 410 |
if ($self->{undefined}) { |
| 411 |
$text .= DiscoverMacro::H::line($f,"Macro is unknown."); |
| 412 |
} else { |
| 413 |
my $list = $self->{list}; |
| 414 |
my $features = $self->{features}; |
| 415 |
$list =~ s{\S+}{ |
| 416 |
my $w = $&; |
| 417 |
if ($$features{$w}) { |
| 418 |
$w = "[[[HREF #".DiscoverMacro::H::escape('identifier',$w). |
| 419 |
"]]]$w\[[[/HREF]]]"; |
| 420 |
} |
| 421 |
$w; |
| 422 |
}ge; |
| 423 |
$text .= DiscoverMacro::H::twocol($f,"Available with:",$list); |
| 424 |
} |
| 425 |
$text .= DiscoverMacro::H::footer($f); |
| 426 |
for my $feature (values %{$self->{features}}) { |
| 427 |
$text .= $feature->as_text($f); |
| 428 |
} |
| 429 |
return $text; |
| 430 |
} |
| 431 |
|
| 432 |
package DiscoverMacro; |
| 433 |
|
| 434 |
use IO::File; |
| 435 |
use Data::Dumper; |
| 436 |
|
| 437 |
use vars qw/$obj_count @datafile_path/; |
| 438 |
$obj_count = 0; |
| 439 |
|
| 440 |
sub new($) { |
| 441 |
my ($proto) = @_; |
| 442 |
my $class = ref($proto) || $proto; |
| 443 |
my $self = {}; |
| 444 |
$obj_count++; |
| 445 |
bless $self, $class; |
| 446 |
$self->{missingmacros} = {}; |
| 447 |
$self->opendata(); |
| 448 |
return $self; |
| 449 |
} |
| 450 |
|
| 451 |
sub DESTROY { |
| 452 |
$obj_count--; |
| 453 |
} |
| 454 |
|
| 455 |
sub skip_to_end($$$) { |
| 456 |
my ($file,$type,$lnr) = @_; |
| 457 |
my $line; |
| 458 |
while (defined ($line = DiscoverMacro::H::readline2($file))) { |
| 459 |
return $file->getpos |
| 460 |
if $line =~ /^\s*END\s*\Q$type\E\s*$/; |
| 461 |
} |
| 462 |
warn "Could not find end of $type-section started on line $lnr"; |
| 463 |
return undef; |
| 464 |
} |
| 465 |
|
| 466 |
sub lineinfo($) { |
| 467 |
my $lnr = shift; |
| 468 |
return "ltxmacrs.txt:$lnr: "; |
| 469 |
} |
| 470 |
|
| 471 |
sub opendata($) { |
| 472 |
my $self = shift; |
| 473 |
my $macros = {}; |
| 474 |
my $regexmacros = {}; |
| 475 |
my $features = {}; |
| 476 |
my $datafile = $self->{datafile}; |
| 477 |
unless (defined $datafile) { |
| 478 |
for my $dir (@datafile_path) { |
| 479 |
if ($dir eq 'program') { |
| 480 |
$datafile = $0; $datafile =~ s@[^/]*[email protected]@; |
| 481 |
} elsif (-d $dir) { |
| 482 |
$datafile = "$dir/ltxmacrs.txt"; |
| 483 |
} else { |
| 484 |
$datafile = "$dir"; |
| 485 |
} |
| 486 |
if (-e $datafile) { last; } |
| 487 |
else { $datafile = undef; } |
| 488 |
} |
| 489 |
die "Could not find data file ltxmacrs.txt in path ". |
| 490 |
join ', ', map { |
| 491 |
($_ eq 'program')?'script location':$_ } @datafile_path |
| 492 |
unless defined $datafile; |
| 493 |
$self->{datafile} = $datafile; |
| 494 |
} |
| 495 |
my $data = new IO::File($datafile,"r") or |
| 496 |
die "Could not open $datafile for reading"; |
| 497 |
my $line; |
| 498 |
while (defined ($line = DiscoverMacro::H::readline2($data))) { |
| 499 |
$line = DiscoverMacro::H::cleanline($line); |
| 500 |
next if $line eq ''; |
| 501 |
my (@line) = split ' ', $line; |
| 502 |
if ($line[0] eq 'MACRO') { |
| 503 |
# print Dumper(\@line); |
| 504 |
warn lineinfo($.)."Macro $line[1] given twice\n" |
| 505 |
if defined $$macros{$line[1]}; |
| 506 |
$$macros{$line[1]} = join ' ', @line[2..$#line]; |
| 507 |
} elsif ($line[0] eq 'REGEX') { |
| 508 |
warn lineinfo($.)."Regex macro $line[1] given twice\n" |
| 509 |
if defined $$regexmacros{$line[1]}; |
| 510 |
$$regexmacros{$line[1]} = join ' ', @line[2..$#line]; |
| 511 |
} elsif ($line[0] eq 'FEATURE') { |
| 512 |
$$features{$line[1]} = $data->getpos; |
| 513 |
skip_to_end($data,'FEATURE',$.); |
| 514 |
} else { |
| 515 |
warn lineinfo($.)."Unknown command '$line[0]'\n"; |
| 516 |
} |
| 517 |
} |
| 518 |
$self->{macros} = $macros; |
| 519 |
$self->{regexmacros} = $regexmacros; |
| 520 |
$self->{features} = $features; |
| 521 |
$self->{datafile} = $data; |
| 522 |
|
| 523 |
# print Dumper($macros); |
| 524 |
} |
| 525 |
|
| 526 |
sub getmacro($$) { |
| 527 |
my ($self,$macro) = @_; |
| 528 |
return new DiscoverMacro::Macro($self,$macro); |
| 529 |
} |
| 530 |
|
| 531 |
sub getfeaturepos($$) { |
| 532 |
my ($self,$feature) = @_; |
| 533 |
my $features = $self->{features}; |
| 534 |
return $$features{$feature}; |
| 535 |
} |
| 536 |
|
| 537 |
sub readlog($$;$) { |
| 538 |
my ($self,$filename,$file) = @_; |
| 539 |
my $found = 0; |
| 540 |
if (!defined $file) { |
| 541 |
$file = new IO::File($filename,"r") |
| 542 |
or die "Could not open log file '$filename' for reading: $!"; |
| 543 |
} |
| 544 |
my $line; |
| 545 |
while (defined ($line = <$file>)) { |
| 546 |
chomp $line; |
| 547 |
if ($line =~ /^! Undefined control sequence\.$/) { |
| 548 |
chomp ($line = <$file>); |
| 549 |
#print "LINE: '$line'\n"; |
| 550 |
my $dots = ''; |
| 551 |
my ($macro) = ($line =~ /(\\.)$/); |
| 552 |
($dots,$macro) = |
| 553 |
($line =~ /(...)(\\[a-zA-Z@]+) ?$/) unless defined $macro; |
| 554 |
($dots,$macro) = |
| 555 |
($line =~ /(...)([a-zA-Z@]+) ?$/) unless defined $macro; |
| 556 |
my $regex = 0; |
| 557 |
if ($dots eq '...') { |
| 558 |
$regex = 1; |
| 559 |
$macro =~ s/^\\//; |
| 560 |
$macro = "\\\\[a-zA-Z@]*\Q$macro"; |
| 561 |
} else { |
| 562 |
$macro = "\\$macro" unless $macro =~ /^\\/; |
| 563 |
} |
| 564 |
unless (defined $macro) { |
| 565 |
warn "$filename:$.: Could not identify undefined control in:\n". |
| 566 |
"\t$line\n"; |
| 567 |
next; }; |
| 568 |
#print "MACRO: '$macro'\n"; |
| 569 |
if ($regex) { |
| 570 |
$found = 0; |
| 571 |
for my $m (keys %{$self->{macros}}) { |
| 572 |
if ($m =~ /$macro/) { |
| 573 |
$self->addmacro($m); |
| 574 |
$found = 1; } |
| 575 |
} |
| 576 |
$self->addmacro("REGEX:$macro") unless $found; |
| 577 |
} else { |
| 578 |
$self->addmacro($macro); |
| 579 |
} |
| 580 |
$found = 1; |
| 581 |
} elsif ($line =~ /^! Package babel Error: You haven\'t defined the language (.+) yet.$/) { |
| 582 |
my $lang = $1; |
| 583 |
$self->addmacro("\\selectlanguage{$lang}"); |
| 584 |
$found = 1; |
| 585 |
} elsif ($line =~ /^! LaTeX Error: Environment .* undefined\.$/) { |
| 586 |
my ($env) = |
| 587 |
($line =~ /^! LaTeX Error: Environment (.*) undefined\.$/); |
| 588 |
unless (defined $env) { |
| 589 |
warn "$filename:$.: Could not identify environment in:\n". |
| 590 |
"\t$line\n" unless defined $env; |
| 591 |
next; }; |
| 592 |
$self->addmacro("\\begin{$env}"); |
| 593 |
$found = 1; |
| 594 |
} elsif ($line =~ /^! LaTeX Error: Encoding scheme \`.*\' unknown\.$/) { |
| 595 |
my ($fe) = |
| 596 |
($line =~ /^! LaTeX Error: Encoding scheme \`(.*)\' unknown\.$/); |
| 597 |
unless (defined $fe) { |
| 598 |
warn "$filename:$.: Could not identify fontencoding in:\n". |
| 599 |
"\t$line\n" unless defined $fe; |
| 600 |
next; }; |
| 601 |
$self->addmacro("\\fontencoding{$fe}"); |
| 602 |
$found = 1; |
| 603 |
} elsif ($line =~ /^! LaTeX Error: Command .* (not provided|unavailable)/) { |
| 604 |
my ($macro) = |
| 605 |
($line =~ /^! LaTeX Error: Command (.*) (not provided|unavailable)/); |
| 606 |
unless (defined $macro) { |
| 607 |
warn "$filename:$.: Could not identify macro in:\n". |
| 608 |
"\t$line\n" unless defined $macro; |
| 609 |
next; }; |
| 610 |
$macro = "\\$macro" unless $macro =~ /^\\/; |
| 611 |
$self->addmacro("$macro"); |
| 612 |
$found = 1; |
| 613 |
} elsif ($line =~ /^! Package ucs Error: Unknown .* tag '.*' \((.*)\)\.$/) { |
| 614 |
my ($macro) = |
| 615 |
($line =~ /^! Package ucs Error: Unknown .* tag '.*' \((.*)\)\.$/); |
| 616 |
unless (defined $macro) { |
| 617 |
warn "$filename:$.: Could not identify macro in:\n". |
| 618 |
"\t$line\n"; |
| 619 |
next; }; |
| 620 |
$macro = "\\$macro" unless $macro =~ /^\\/; |
| 621 |
$self->addmacro("$macro"); |
| 622 |
$found = 1; |
| 623 |
} elsif ($line =~ /^! Font .*=([^=]+) at .* not loadable: Metric \(TFM\)/) { |
| 624 |
my ($tfm) = ($line =~ /^! Font .*=([^=]+) at .* not loadable: Metric \(TFM\)/); |
| 625 |
unless (defined $tfm) { |
| 626 |
warn "$filename:$.: Could not identify TFM filename in:\n". |
| 627 |
"\t$line\n"; |
| 628 |
next; }; |
| 629 |
print "XXX: $tfm\n"; |
| 630 |
$self->addmacro("$tfm.tfm"); |
| 631 |
$found = 1; |
| 632 |
} |
| 633 |
} |
| 634 |
close $file; |
| 635 |
return $found; |
| 636 |
} |
| 637 |
|
| 638 |
sub addmacro($$%) { |
| 639 |
my ($self,$macro) = @_; |
| 640 |
my $macros = $self->{missingmacros}; |
| 641 |
return $$macros{$macro} if $$macros{$macro}; |
| 642 |
my $res = $self->getmacro($macro); |
| 643 |
$$macros{$macro} = $res if defined $res; |
| 644 |
return $res; |
| 645 |
} |
| 646 |
|
| 647 |
sub getfileat($$) { |
| 648 |
my ($self,$pos) = @_; |
| 649 |
my $file = $self->{datafile}; |
| 650 |
$file->setpos($pos) or |
| 651 |
die "Could not seek in datafile to pos $pos"; |
| 652 |
return $file; |
| 653 |
} |
| 654 |
|
| 655 |
sub getmissingmacros($) { |
| 656 |
my $self = shift; |
| 657 |
return $self->{missingmacros}; |
| 658 |
} |
| 659 |
|
| 660 |
sub as_text($$) { |
| 661 |
my ($self,$f) = @_; |
| 662 |
my $macros = $self->{missingmacros}; |
| 663 |
my $str = ''; |
| 664 |
for my $m (keys %$macros) { |
| 665 |
$str .= $$macros{$m}->as_text($f); |
| 666 |
} |
| 667 |
return $str; |
| 668 |
} |
| 669 |
|
| 670 |
sub close($) { |
| 671 |
my $self = shift; |
| 672 |
$self->{macros} = undef; |
| 673 |
$self->{features} = undef; |
| 674 |
$self->{missingmacros} = undef; |
| 675 |
close $self->{datafile}; |
| 676 |
} |
| 677 |
|
| 678 |
sub getfeature($$) { |
| 679 |
my ($self,$feature) = @_; |
| 680 |
return new DiscoverMacro::Feature($self,$feature); |
| 681 |
} |
| 682 |
|
| 683 |
sub scanformacro($%) { |
| 684 |
my ($self,%args) = @_; |
| 685 |
my $file = $args{file}; |
| 686 |
my $fh = new IO::File($file,"r") or |
| 687 |
die "Could not open $file for reading: $!"; |
| 688 |
my $line; |
| 689 |
# {\newenvironment}{\renewenvironment}{\newif} |
| 690 |
my $newcommand = '\\\\newcommand|\\\\renewcommand|\\\\providecommand|'. |
| 691 |
'\\\\DeclareRobustCommand|\\\\def'; |
| 692 |
my $newenvironment = '\\\\newenvironment|\\\\renewenvironment'; |
| 693 |
my @macros; |
| 694 |
while (defined ($line = <$fh>)) { |
| 695 |
chomp $line; |
| 696 |
print "L: $line\n"; |
| 697 |
$line =~ s{($newcommand) \s* \{? (\\[a-zA-Z@]+) \}?}{ |
| 698 |
print "MACRO: $2\n"; |
| 699 |
my ($env) = ($2 =~ /^\\end(.*)$/); |
| 700 |
if (defined $env) { |
| 701 |
print "ENV: $env\n"; } |
| 702 |
}gxeo; |
| 703 |
$line =~ s{($newenvironment) \s* \{ ([^\}]+) \} }{ |
| 704 |
print "ENV: $2\n"; |
| 705 |
}gxeo; |
| 706 |
} |
| 707 |
print Dumper [\@macros]; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
sub main(@) { |
| 712 |
if (@_) { |
| 713 |
for my $file (@_) { |
| 714 |
my $obj = new DiscoverMacro; |
| 715 |
if (-e $file) { |
| 716 |
$obj->readlog($file); |
| 717 |
} else { |
| 718 |
$obj->addmacro($file); |
| 719 |
} |
| 720 |
print $obj->as_text('text/plain'); |
| 721 |
}; |
| 722 |
} else { |
| 723 |
my ($file) = |
| 724 |
sort { my @a = stat $a; my @b = stat $b; $b[9] <=> $a[9] } |
| 725 |
grep { $_ ne 'missfont.log' } (<*.log>); |
| 726 |
die "No logfile found" if (!defined $file); |
| 727 |
print "Using logfile $file.\n"; |
| 728 |
&main($file); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
sub test() { |
| 733 |
my $obj = new DiscoverMacro; |
| 734 |
$obj->scanformacro(file => 'test.tex'); |
| 735 |
exit; |
| 736 |
} |
| 737 |
|
| 738 |
#test; |
| 739 |
|
| 740 |
caller || &main(@ARGV); |
| 741 |
|
| 742 |
1; |