[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / glafp-utils / scripts / ltx.prl
1 $Pgm = $0; $Pgm =~ s/.*\/([^\/]+)$/\1/;
2 #
3 # set up signal handler
4 sub quit_upon_signal { &rm_temp_files_and_exit(); }
5 $SIG{'INT'}  = 'quit_upon_signal';
6 $SIG{'QUIT'} = 'quit_upon_signal';
7 #
8 $Verbose = 0;
9 if ($ARGV[0] eq '-v') {
10     $Verbose = 1;
11     shift(@ARGV);
12 }
13 #
14 die "$Pgm: must have exactly one argument\n" if $#ARGV != 0;
15 # figure out input file and its filename root
16 if (-f $ARGV[0]) {
17     $TeX_input = $ARGV[0];
18     if ($TeX_input =~ /(.+)\.[^\.\/\n]+$/) {
19         $TeX_root  = $1;
20     } else {
21         $TeX_root  = $TeX_input;
22     }
23 } elsif (-f $ARGV[0].'.tex') {
24     $TeX_input = $ARGV[0].'.tex';
25     $TeX_root  = $ARGV[0];
26 } else {
27     die "$Pgm: input file $ARGV[0] doesn't exist\n";
28 }
29
30 sub rm_temp_files {
31     system("rm -f $(TMPDIR)/ltx-*.$$");
32 }
33 sub rm_temp_files_and_exit {
34     system("rm -f $(TMPDIR)/ltx-*.$$");
35     exit(1);
36 }
37 $SIG{'INT'}  = 'rm_temp_files_and_exit';
38 $SIG{'QUIT'} = 'rm_temp_files_and_exit';
39
40 sub die_gracefully {
41     local($msg) = @_;
42     
43     print STDERR $msg;
44     &rm_temp_files_and_exit();
45 }
46
47 # must read through root file to see if a \bibliography
48 # is there...
49 $Bibliography_requested = 0;
50 open(TEXIF, "<$TeX_input") 
51     || &die_gracefully("$Pgm: Can't read $TeX_input\n");
52 while (<TEXIF>) {
53     $Bibliography_requested = 1 if /^\\bibliography/;
54 }
55 close(TEXIF);
56 &die_gracefully("$Pgm: reading $TeX_input had errors\n") if $? >> 8;
57
58 # run latex first time (?)
59 &run_latex(); # sets $Says_labels_changed
60 $Times_run = 1;
61
62 while (&something_more_needed()) {
63
64     print STDERR "labels_changed=$Says_label_changed;bibtex_needed=$BibTeX_run_needed;makeindex_needed=$MakeIndex_run_needed\n" if $Verbose;
65
66     if ($BibTeX_run_needed) {
67         &run_bibtex();
68     }
69     if ($MakeIndex_run_needed) {
70         unlink "$TeX_root.ind";
71         (system("makeindex $TeX_root.idx") >> 8) 
72             && &die_gracefully("$Pgm: makeindex $TeX_root.idx had errors\n");
73     }
74
75     # save (copy) .aux file as .aux-prev file for future ref
76     # ditto for .idx file
77     unlink "$TeX_root.aux-prev";
78     (system("cp $TeX_root.aux $TeX_root.aux-prev") >> 8)
79         && &die_gracefully("$Pgm: cp $TeX_root.aux $TeX_root.aux-prev failed\n");
80     if (-f "$TeX_root.idx") {
81         unlink "$TeX_root.idx-prev";
82         (system("cp $TeX_root.idx $TeX_root.idx-prev") >> 8)
83             && &die_gracefully("$Pgm: cp $TeX_root.idx $TeX_root.idx-prev failed\n");
84     }
85
86     # run latex again
87     &run_latex(); # sets $Says_labels_changed
88     $Times_run++;
89
90     if ($Times_run >= 4) {
91         print STDERR "*** I don't run LaTeX more than four times;\n";
92         print STDERR "*** Something is probably wrong...\n";
93         &rm_temp_files_and_exit();
94     }
95 }
96 &rm_temp_files();
97 exit(0);
98
99 sub run_latex {
100     $Says_labels_changed = 0;
101     $Multiply_defined_labels = 0;
102
103     select(STDERR); $| = 1; select(STDOUT); # no buffering on STDERR
104     print STDERR "$Pgm: *** running LaTeX...\n" if $Verbose;
105     unlink "$TeX_root.dvi";
106
107     open(LTXPIPE, "latex $TeX_input 2>&1 |") 
108         || &die_gracefully("$Pgm: Can't run latex pipe\n");
109     while (<LTXPIPE>) {
110         $Multiply_defined_labels = 1 if /^LaTeX Warning: Label .* multiply defined/;
111         $Says_labels_changed = 1 if /^LaTeX Warning: Label\(s\) may have changed/
112                                         && ! $Multiply_defined_labels;
113         print STDERR $_;
114     }
115     close(LTXPIPE);
116     &die_gracefully("$Pgm: LaTeX run had errors\n") if $? >> 8;
117
118     # sort .idx file, because this helps makeindex
119     # (can you say `bug'?)
120     if (-f "$TeX_root.idx") {
121         print STDERR "$Pgm: *** sorting $TeX_root.idx...\n" if $Verbose;
122         (system("sort $TeX_root.idx -o $TeX_root.idx") >> 8)
123         && &die_gracefully("$Pgm: sorting $TeX_root.idx failed\n");
124     }
125
126 }
127
128 sub run_bibtex { # ugly because bibtex doesn't return a correct error status
129     local($bibtex_had_errors) = 0;
130
131     print STDERR "$Pgm: *** running BibTeX...\n" if $Verbose;
132     unlink "$TeX_root.bbl";
133
134     $| = 1; # no buffering
135     open(BIBTXPIPE, "bibtex $TeX_root 2>&1 |") 
136         || &die_gracefully("$Pgm: Can't run bibtex pipe\n");
137     while (<BIBTXPIPE>) {
138         $bibtex_had_errors = 1 if /^\(There.*error message(s)?\)$/;
139         print STDERR $_;
140     }
141     close(BIBTXPIPE);
142     &die_gracefully("$Pgm: BibTeX run had errors\n")
143         if $? >> 8 || $bibtex_had_errors;
144 }
145
146 sub something_more_needed {
147     # returns 1 or 0 if we need to run LaTeX
148     # possibly preceded by bibtex and/or makeindex run
149
150     # $Says_labels_changed was set by previous &run_latex...
151     $BibTeX_run_needed    = 0;
152     $MakeIndex_run_needed = 0;
153
154     if ( ! -f ($TeX_root . '.aux-prev')) { # this was the first run
155
156         print STDERR "$Pgm: *** 'twas first run of LaTeX on $TeX_input\n" if $Verbose;
157
158         # we need makeindex to run if a non-zero-sized .idx file exists
159         #
160         $MakeIndex_run_needed = 1
161             if -f "$TeX_root.idx" && -s "$TeX_root.idx";
162
163         # we need bibtex to run if there are \citations in the .aux file
164         #
165         &slurp_aux_file('aux');
166         $BibTeX_run_needed = 1
167             if $Bibliography_requested &&
168               -f "$(TMPDIR)/ltx-aux-cite.$$" &&
169               -s "$(TMPDIR)/ltx-aux-cite.$$";
170
171
172     } else { # ltx had been run before (.aux-prev/.idx-prev files exist)
173
174         # slurp both .aux and .aux-prev files
175         &slurp_aux_file('aux');
176         &slurp_aux_file('aux-prev');
177
178         local($tmp_pre) = "$(TMPDIR)/ltx";
179
180         if ((-s "$tmp_pre-.aux-cite.$$") # there are still \cite's in there
181          && (system("cmp -s $tmp_pre-.aux-cite.$$ $tmp_pre-.aux-prev-cite.$$") >> 8)) {
182             $BibTeX_run_needed = 1 if $Bibliography_requested;
183             if ($Verbose) {
184                 system("$(CONTEXT_DIFF) $tmp_pre-.aux-prev-cite.$$ $tmp_pre-.aux-cite.$$");
185             }
186         }
187
188         if (-f "$TeX_root.idx") {
189             $MakeIndex_run_needed =
190                 (system("cmp -s $TeX_root.idx $TeX_root.idx-prev") >> 8) ? 1 : 0;
191             if ($MakeIndex_run_needed && $Verbose) {
192                 system("$(CONTEXT_DIFF) $TeX_root.idx-prev $TeX_root.idx");
193             }
194         }
195     }
196
197     $Says_labels_changed || $BibTeX_run_needed || $MakeIndex_run_needed;
198 }
199
200 sub slurp_aux_file {
201     local($ext) = @_;
202
203     # copy all citations from slurpfile into $(TMPDIR)/ltx-$ext-cite.$$
204
205     open(SLURPF,"< $TeX_root.$ext")
206         || &die_gracefully("$Pgm: Can't open $TeX_root.$ext for reading\n");
207     open(CITEF,"> $(TMPDIR)/ltx-$ext-cite.$$")
208         || &die_gracefully("$Pgm: Can't open $(TMPDIR)/ltx-$ext-cite.$$ for writing\n");
209
210     while (<SLURPF>) {
211         print CITEF $_  if /\\citation/;
212     }
213     close(CITEF);
214     close(SLURPF);
215 }