summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/chklref/chklref.pl
blob: 6e8ba38e27f3a8cdfffa65c29f0837a08f7dfd7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/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        = "";
            $labelled_env = 0;
            while (defined($l = shift(@$txt))) {
                if ($l =~ m/^label (.+) line (\d+) file (.+)$/o) {
                    push(@$labels, new_ref($1, $2, $3));
                    $label       = $1;
                    $labelled_env = $2;
                }
                elsif ($l =~ m/^ref (.+) line (\d+) file (.+)$/o) {
                    push(@$refs, new_ref($1, $2, $3));
                }
                elsif ($l =~ m/^end\{$str\*{$star}\} line (\d+) file (.+)$/) {
                    $end = $1;
                    $entry =  new_math_env($str, $begin, $end, $star, $labelled_env,
                        $label, $file);
                    push(@$entries, $entry);
                    last;
                }
            }
        }
    }
    return;
}

# Find environments with both labels and stars
# Input : list of entries
sub star_label {
    my ($entries) = @_;
    print "************************************\n";
    print "** Labels in starred environments **\n";
    print "************************************\n";
    foreach my $e (@$entries) {
        printf("-- %-20s line %4d: remove label %s \n",
            $e->{file}, $e->{label_line}, $e->{label})
        if (($e->{star} == 1) && ($e->{label_line} > 0));

        printf("-- %-20s line %4d: consider using a STAR environment\n",
            $e->{file}, $e->{begin})
        if (($e->{star} == 0) && ($e->{label_line} == 0));
    }
    print "\n";
    return;
}

# Find labels without any corrsponding references.
# references must be sorted
sub unused_label {
    my ($labels, $refs) = @_;
    my $found;

    print "*******************\n";
    print "** Unused Labels **\n";
    print "*******************\n";
    foreach my $l (@$labels) {
        $found = 0;
        foreach my $r (@$refs) {
            if ($r->{str} eq $l->{str}) {
                $found = 1;
                last;
            }
            elsif ($r->{str} ge $l->{str}) {
                last;
            }
        }
        printf("-- %-20s line %4d: remove label %s\n",
            $l->{file}, $l->{line}, $l->{str})
        if (!$found);
    }
    print "\n";
    return;
}

# Find bibitems without any corresponding \cite
sub unused_cites {
    my ($bibcites, $citations) = @_;
    print "**********************************\n";
    print "** Uncited Bibliography entries **\n";
    print "**********************************\n";
    foreach my $c (@$bibcites) {
        print("remove bibitem : $c\n") unless exists ${$citations}{$c};
    }
    return;
}

# Find and remove duplicates in an
# array of { str, line } entries.
# Note that the array must be sorted according to str
# Input : ref or label list
# Return the corresponding list
sub rm_duplicate {
    my ($array) = @_;
    my $prev = '___________not_a_true_label______';
    my @uniq_array =
    grep { $_->{str} ne $prev && (($prev) = $_->{str}) } @$array;
    return \@uniq_array;
}

# Display all math envs with their characteristics
# Input args
#  line of beginning
#  line of end
#  starred or not
#  label
sub disp_msg {
    my ($entries) = @_;
    my @entries = @$entries;

    foreach my $e (@entries) {
        print("env $e->{str} \n");
        print("\tbeginning : $e->{begin}\n\tend : $e->{end}\n");
        print("\tstar environment\n") if ($e->{star} == 1);
        print("\tlabelled environement\n") if ($e->{label} > 0);
        print("\n");
    }
    return;
}

sub parse {
    my ($chkfile) = @_;
    my @entries = ();
    my @refs    = ();
    my @labels  = ();
    my %citations = ();
    my @bibcites  = ();
    my $FIC;

    # Read the whole file to an array because label and end commands
    # generally need to be swapped.
    open($FIC, '<', $chkfile) or die("open: $!");
    my @txt = <$FIC>;
    close($FIC);

    chk_parse(\@txt, \@entries, \@refs, \@labels, \%citations, \@bibcites);

    @labels = sort { $a->{line} cmp $b->{line} } @labels;
    @refs   = sort { $a->{str} cmp $b->{str} } @refs;
    my $uniq_refs = rm_duplicate(\@refs);
    star_label(\@entries);
    #disp_msg( \@entries );
    unused_label(\@labels, $uniq_refs);
    unused_cites(\@bibcites, \%citations);
}

sub usage {
    print << 'EOT';
Usage: chklref [options] texfile

Check unused labels and bibitems in a LaTeX file

Options:
  --tex <compiler>, -t     Specify the TeX compiler to be used. Default is `pdflatex`.
  --tex-options            List of options to pass to the TeX compiler. It should be a quoted
                           string of white space delimited options. Note that we always add
                           `-interaction nonstopmode` on top of these options.
  --debug, -d              Run in debug mode. Do not clean the generated `.chk` file.
  --quiet, -q              Run in quiet mode. Do not print the output of the TeX compiler.
  --parse-only             Do not run the LaTeX compiler but use the already existing `.chk` file.
                           When this option is passed, the following other options are meaningless:
                           `--tex`, `--tex-options`, `--quiet`, `--debug`.
  --version, -v            Print the version of this script.
  --help,h                 Print this help.


Report bugs to https://github.com/jlelong/chklref. `chklref` is known not to work with `cleveref`. If you would like to contribute to `chklref`, feel free to open a PR on https://github.com/jlelong/chklref.
EOT
    exit(0)
}

sub version {
    print << 'EOT';
chklref 3.1.2

Copyright 2005-2019 Jerome Lelong <jerome.lelong@gmail.com>.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under GPLv3.

Written by Jerome Lelong <jerome.lelong@gmail.com>
EOT
    exit(0)
}

sub texcompile {
    my ($texfile, $basetexfile) = @_;
    print "Running $TEX $TEXOPTIONS $USERTEXOPTIONS \"$texfile\" to collect labels references and environment declarations\n\n";
    my $texoutput = `$TEX $TEXOPTIONS $USERTEXOPTIONS "$texfile" 2>&1`;
    print($texoutput) unless $RUNQUIET;
    unlink($basetexfile . '.chk');
    $texoutput = `$TEX $TEXOPTIONS $USERTEXOPTIONS --jobname "$basetexfile" '\\RequirePackage{chklref}\\input' "{$texfile}"  2>&1`;
    print($texoutput) unless $RUNQUIET;
}

Getopt::Long::Configure ("bundling");
GetOptions (
   'version|v!' => \&version,
   'quiet|q!' => \$RUNQUIET,
   'debug|d!' => \$RUNDEBUG,
   'tex|t=s' => \$TEX,
   'texoptions=s' => \$USERTEXOPTIONS,
   'parse-only' => \$PARSEONLY,
   'help|h' => \&usage
) || usage();

if (@ARGV != 1) {
    usage();
    exit 0;
}

my $texfile = $ARGV[0];
my $cwd = getcwd();
my $dirtexfile = dirname($texfile);
my $basetexfile = basename($texfile, '.tex');
my $chkfile = $basetexfile . '.chk';
chdir($dirtexfile);
if ($PARSEONLY) {
    parse($chkfile);
}
else {
    texcompile($texfile, $basetexfile);
    parse($chkfile);
    if (! $RUNDEBUG) {
        unlink($chkfile);
    }
}