00000062
  N   ˆl–ßi~”ji?u ¾ ƒq—n JbÑ¿�þD1…fßp`f7Ùn7�üÿ¿„?Äa–ßi~”Ji?u8 ¸Û—Ò˜´oÂ *¡    #!/usr/bin/env perl -w

#########################################################################
# Written and (C) by JÃ©rÃ´me Lelong <jerome.lelong@gmail.com>            #
#                                                                       #
# This program is free software; you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation; either version 3 of the License, or     #
# (at your option) any later version.                                   #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>. #
#########################################################################

use strict;
use warnings;
use File::Basename;
use Cwd;
use Getopt::Long;
# recognised math environments
my @have_star_modes = qw( equation eqnarray align multline table gather);
my $have_star_mode = join('|', @have_star_modes);
$have_star_mode = "($have_star_mode)";


my $TEX = 'pdflatex';
my $TEXOPTIONS = ' -interaction nonstopmode ';
my $USERTEXOPTIONS = '';
my $RUNQUIET = 0;
my $RUNDEBUG = 0;
my $PARSEONLY = 0;

# Create a hash with three keys "str", "line", "file" and returns a
# reference to it
#
# Input: 3 args
#   a string
#   a line number
#   a filename
sub new_ref {
    my ($str, $line, $file) = @_;
    my $ref = {};
    $ref->{str}  = $str;
    $ref->{line} = $line;
    $ref->{file} = $file;
    return $ref;
}

# Create a hash with six keys "str", "begin", "end", "star",
# "label_line" and "label" and returns a reference to it.
#
# Input : 7 args
#    a string (the env name)
#    line of beginning
#    line of end
#    has the env a star?
#    line on which the label appears
#    value of the label
#    file name
sub new_math_env {
    my ($str, $begin, $end, $star, $label_line, $label, $file) = @_;
    my $this = {};
    $this->{str}        = $str;
    $this->{begin}      = $begin;
    $this->{end}        = $end;
    $this->{star}       = $star;
    $this->{label_line} = $label_line;
    $this->{label}      = $label;
    $this->{file}       = $file;
    return $this;
}


# Parse .chk file and looks for the environments defined by
# @have_star_modes. The first and last line of the environment are stored
# along with the name of the environment. One last variable is
# used to remember if the star version was used.
#
# Input:
#   a chk file content
#   entries
#   refs
#   labels
# the last 3 args are passed as references and are modified
sub chk_parse {
    my ($txt, $entries, $refs, $labels, $citations, $bibcites) = @_;
    my ($str, $begin, $end, $star, $labelled_env, $label, $file, $entry, $l);

    while (defined($l = shift(@$txt))) {
        if ($l =~ m/^label (.+) line (\d+) file (.+)\n$/o) {
            push(@$labels, new_ref($1, $2, $3));
        }
        elsif ($l =~ m/^ref (.+) line (\d+) file (.+)$/o) {
            push(@$refs, new_ref($1, $2, $3));
        }
        elsif ($l =~ m/^citation (.+) line (\d+) file (.+)$/o) {
            foreach my $c (split(/\s*,\s*/, $1)) {
                ${$citations}{$c} = 1;
            }
        }
        elsif ($l =~ m/^bibcite (.+) line (\d+) file (.+)$/o) {
            push(@$bibcites, $1);
        }
        elsif ($l =~ m/^begin\{$have_star_mode(\**)\} line (\d+) file (.+)$/o) {
            $str          = $1;
            $star         = 1;
            $star         = 0 unless ($2);
            $begin        = $3;
            $file         = $4;
            $label    