Debian Codegraph @

texk/texlive/linked_scripts/texfot/texfot.pl

raw
1 #!/usr/bin/env perl
2 # $Id: texfot,v 1.54 2026/01/05 17:14:08 karl Exp $
3 # Invoke a TeX command, filtering all but interesting terminal output;
4 # do not look at the log or check any output files.
5 # Exit status is that of the subprogram.
6 # Tee the complete (unfiltered) standard output and standard error to
7 # (by default) /tmp/fot.$UID.
8 #
9 # Public domain. Originally written 2014 by Karl Berry.
10
11 my $ident = '$Id: texfot,v 1.54 2026/01/05 17:14:08 karl Exp $';
12 (my $prg = $0) =~ s,^.*/,,;
13 select STDERR; $| = 1; # no buffering
14 select STDOUT; $| = 1;
15
16 use IPC::Open3; # control what happens with stderr from the child.
17 use IO::File; # use new_tmpfile for that stderr.
18
19 # require_order because we don't want getopt to permute anything;
20 # arguments to the tex invocation must remain in order, not handled by us.
21 use Getopt::Long qw(:config require_order);
22 use Pod::Usage;
23
24 my @opt_accept = ();
25 my $opt_debug = 0;
26 my @opt_ignore = ();
27 my $opt_interactive = 0;
28 my $opt_quiet = 0;
29 my $opt_stderr = 1;
30 my $opt_tee = ($ENV{"TMPDIR"} || $ENV{"TMP"} || "/tmp") . "/fot.$>";
31 my $opt_version = 0;
32 my $opt_help = 0;
33
34 exit (&main ());
35
36
37 #
38 sub main {
39 my $ret = GetOptions (
40 "accept=s" => \@opt_accept,
41 "debug!" => \$opt_debug,
42 "ignore=s" => \@opt_ignore,
43 "interactive!" => \$opt_interactive,
44 "quiet!" => \$opt_quiet,
45 "stderr!" => \$opt_stderr,
46 "tee=s" => \$opt_tee,
47 "version" => \$opt_version,
48 "help|?" => \$opt_help) || pod2usage (2);
49
50 # --help, --version
51 pod2usage ("-exitstatus" => 0, "-verbose" => 2)
52 if $opt_help;
53 #
54 if ($opt_version) {
55 print "$ident\n";
56 return 0;
57 }
58
59 die "$prg: missing TeX command, i.e., a non-option argument\n"
60 . "Try --help if you need it."
61 if ! @ARGV;
62
63 # guess we're going to run something. typically no interaction.
64 close (STDIN) unless $opt_interactive;
65
66 local *FOTTMP;
67 $FOTTMP = ">$opt_tee";
68 open (FOTTMP) || die "$prg: aborting, open($FOTTMP) failed: $!";
69
70 # We need to separate stderr from stdout. Otherwise they are randomly
71 # merged, not always at line breaks, hence we can lose useful messages.
72 print "$0: invoking: @ARGV\n" unless $opt_quiet;
73
74 # In order to avoid deadlock when there is lots of stuff on stderr,
75 # we must write it to a temporary file
76 # http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command
77 local *TEXERR = IO::File->new_tmpfile
78 || die "IO::File->new_tmpfile failed: $!";
79
80 # But we can process stdout as it comes.
81 local *TEXOUT;
82 my $pid = open3 (undef, \*TEXOUT, ">&TEXERR", @ARGV)
83 || die "$prg: fork(TeX) failed: $! [cmd=@ARGV]\n";
84 &debug ("open3() returned pid $pid [cmd=@ARGV]");
85
86 # It's not ideal to read all of stdout and then all of stderr; it would
87 # be better to intermix them in the original order of child output.
88 # But this is simpler than other ways of avoiding possible deadlock (such
89 # as select, sysread, etc.).
90 &debug ("processing stdout from child");
91 &process_output (\*TEXOUT, "");
92
93 # Be sure everything is drained.
94 &debug ("starting waitpid() for $pid") ;
95 waitpid ($pid, 0) || die "$prg: waitpid($pid) failed: $!\n";
96 my $child_exit_status = $? >> 8;
97 &debug ("child exit status = $child_exit_status\n");
98
99 &debug ("processing stderr from child");
100 seek (TEXERR, 0, 0) || warn "seek(stderr) failed: $!";
101 &process_output (\*TEXERR, "[stderr] ");
102 close (TEXERR) || warn "close(stderr tmpfile) failed: $!";
103
104 return $child_exit_status;
105 }
106
107
108
109 # Read filehandle $FH, printing lines that we want to stdout,
110 # prefixed by $PREFIX.
111 # If $PREFIX is null (happens for processing stdout), omit lines by default;
112 # if $PREFIX is non-null (processing stderr), print lines by default.
113 #
114 sub process_output {
115 my ($fh,$prefix) = @_;
116
117 my $print_next = 0;
118 LINE: while (<$fh>) {
119 my $line = $_;
120 print FOTTMP $line; # tee everything
121
122 warn "\n" if $opt_debug; # get blank line without texfot: prefix
123 &debug ("looking at line: $_");
124
125 &debug ("checking if have print_next (= $print_next)\n");
126 if ($print_next) {
127 &debug (" printing next ($print_next)\n");
128 print $prefix;
129 print $line;
130 $print_next = 0;
131 next;
132 }
133
134 # don't anchor user accept patterns, leave it up to them.
135 for my $user_accept (@opt_accept) {
136 &debug ("checking user accept '$user_accept'\n");
137 if (/${user_accept}/) {
138 &debug (" found user accept ($user_accept)\n");
139 print $prefix;
140 print $line;
141 next;
142 }
143 }
144
145 &debug ("checking ignores\n");
146 next if /^(
147 LaTeX\ Warning:\ You\ have\ requested\ package
148 |LaTeX\ Font\ Warning:\ Some\ font\ shapes
149 |LaTeX\ Font\ Warning:\ Size\ substitutions
150 |Module\ luamplib\ Warning:\ This\ is\ MetaPost
151 |Package\ auxhook\ Warning:\ Cannot\ patch
152 |Package\ biditools\ Warning:\ Patching
153 |Package\ caption\ Warning:\ Un(supported|known)\ document\ class
154 |Package\ fixltx2e\ Warning:\ fixltx2e\ is\ not\ required
155 |Package\ frenchb?\.ldf\ Warning:\ (Figures|The\ definition)
156 |Package\ layouts\ Warning:\ Layout\ scale
157 |\*\*\*\ Reloading\ Xunicode\ for\ encoding # spurious ***
158 |This\ is\ `?(epsf\.tex|.*\.sty|TAP) # so what
159 |pdfTeX\ warning:.*inclusion:\ fou #nd PDF version ...
160 |pdfTeX\ warning:.*inclusion:\ mul #tiple pdfs with page group
161 |libpng\ warning:\ iCCP:\ Not\ recognizing
162 |!\ $
163 )/x;
164
165 # don't anchor user ignore patterns either.
166 for my $user_ignore (@opt_ignore) {
167 &debug ("checking user ignore '$user_ignore'\n");
168 next LINE if /${user_ignore}/;
169 }
170
171 &debug ("checking for print_next pattern\n");
172 if (/^(
173 .*?:[0-9]+: # usual file:lineno: form
174 |! # usual ! form
175 |>\ [^<] # from \show..., but not "> <img.whatever"
176 |.*pdfTeX\ warning # pdftex complaints often cross lines
177 |LaTeX\ Font\ Warning:\ Font\ shape
178 |Package\ hyperref\ Warning:\ Token\ not\ allowed
179 |removed\ on\ input\ line # hyperref
180 |Runaway\ argument
181 )/x) {
182 &debug (" found print_next ($1)\n");
183 print $prefix;
184 print $line;
185 $print_next = 1;
186 next;
187 }
188
189 &debug ("checking for showing\n");
190 if (/^(
191 This\ is
192 |Output\ written
193 |No\ pages\ of\ output
194 |\(.*end\ occurred
195 |(Und|Ov)erfull
196 |(LaTeX|Package|Class|Module).*(Error|Warning)
197 |.*Citation.*undefined
198 |.*\ Error # as in \Url Error ->...
199 |Missing\ character: # good to show (need \tracinglostchars=1)
200 |\\endL.*problem # XeTeX?
201 |\*\*\*\s # *** from some packages or subprograms
202 |l\.[0-9]+\ # line number marking
203 |all\ text\ was\ ignored\ after\ line
204 |.*Fatal\ error
205 |.*for\ symbol.*on\ input\ line
206 |\#\#
207 )/x) {
208 &debug (" matched for showing ($1)\n");
209 print $prefix;
210 print $line;
211 next;
212 }
213
214 &debug ("done with all checks\n");
215
216 if ($prefix && $opt_stderr) {
217 &debug ("prefix (stderr), showing line by default: $_");
218 print $prefix;
219 print $line;
220 } else {
221 &debug ("no prefix (stdout) or no stderr, ignoring line by default: $_");
222 }
223 }
224 }
225
226
227 sub debug { warn ("$prg: ", @_) if $opt_debug; }
228
229
230 __END__
231
232 =head1 NAME
233
234 texfot - run TeX, filtering online transcript for interesting messages
235
236 =head1 SYNOPSIS
237
238 texfot [I<option>]... I<texcmd> [I<texarg>...]
239
240 =head1 DESCRIPTION
241
242 C<texfot> invokes I<texcmd> with the given I<texarg> arguments,
243 filtering the online output for ``interesting'' messages. Its exit
244 value is that of I<texcmd>. Examples:
245
246 # Sample basic invocation:
247 texfot pdflatex file.tex
248
249 # Ordinarily the full output is copied to /tmp/fot.$UID before
250 # filtering, but that can be omitted, or the filename changed:
251 texfot --tee=/dev/null lualatex file.tex
252
253 # Example of more complex engine invocation:
254 texfot xelatex --recorder '\nonstopmode\input file'
255
256 Here is an example of what the output looks like (in its entirety) on an
257 error-free run:
258
259 /path/to/texfot: invoking: pdflatex hello.ltx
260 This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022) (preloaded format=pdflatex)
261 Output written on hello.pdf (1 page, 94415 bytes).
262
263 Aside from its own options, described below, C<texfot> just runs the
264 given command with the given arguments (same approach to command line
265 syntax as C<env>, C<nice>, C<timeout>, etc.). Thus, C<texfot> works with
266 any engine and any command line options.
267
268 C<texfot> does not look at the log file or any other possible output
269 file(s); it only looks at the standard output and standard error from
270 the command. stdout is processed first, then stderr. C<texfot> writes
271 all accepted lines to its stdout.
272
273 The messages shown are intended to be those which likely need action by
274 the author: error messages, overfull and underfull boxes, undefined
275 citations, missing characters from fonts, etc.
276
277 =head1 FLOW OF OPERATION
278
279 Here is the order in which lines of output are checked:
280
281 =over 4
282
283 =item 1.
284
285 If the ``next line'' needs to be printed (see below), print it.
286
287 =item 2.
288
289 Otherwise, if the line matches any user-supplied list of regexps to
290 accept (given with C<--accept>, see below), in that order, print it.
291
292 =item 3.
293
294 Otherwise, if the line matches the built-in list of regexps to ignore,
295 or any user-supplied list of regexps to ignore (given with C<--ignore>,
296 see below), in that order, ignore it.
297
298 =item 4.
299
300 Otherwise, if the line matches the list of regexps for which the next
301 line (two lines in all) should be shown, show this line and set the
302 ``next line'' flag for the next time around the loop. Examples are the
303 common C<!> and C<filename:lineno:> error messages, which are generally
304 followed by a line with specific detail about the error.
305
306 =item 5.
307
308 Otherwise, if the line matches the list of regexps to show, show it.
309
310 =item 6.
311
312 Otherwise, the default: if the line came from stdout, ignore it; if the
313 line came from stderr, print it (to stdout), with the prefix
314 S<C<[stderr] >>. This distinction is made because TeX engines write
315 relatively few messages to stderr, and it's likely that any such should
316 be considered.
317
318 =back
319
320 Once a particular check matches, the program moves on to process the
321 next line.
322
323 C<texfot> matches exclusively line-by-line; however, TeX itself folds
324 output lines, typically at column 79. This means matches might fail
325 because the text being matched was split over two lines. To work around
326 this, you can effectively turn off TeX's folding by setting the
327 C<max_print_line> parameter to a large number, either in the environment
328 or on the command:
329
330 # When errors are missed due to TeX's folding of lines:
331 texfot pdftex --cnf-line max_print_line=999 file.tex
332
333 # Equivalently:
334 env max_print_line=999 texfot pdftex file.tex
335
336 Don't hesitate to peruse the source to the script, which is essentially
337 a straightforward loop matching against the different lists as above.
338 You can see the exact regexps being matched in the different categories
339 in the source.
340
341 Incidentally, although nothing in this basic operation is specific to
342 TeX engines, all the regular expressions included in the program are
343 specific to TeX. So in practice the program isn't useful except with TeX
344 engines, although it would be easy enough to adapt it (if there was
345 anything else as verbose as TeX to make that useful).
346
347 =head1 OPTIONS
348
349 The following are the options to C<texfot> itself (not the TeX engine
350 being invoked; consult the engine documentation or C<--help> output for
351 that).
352
353 The first non-option terminates C<texfot>'s option parsing, and the
354 remainder of the command line is invoked as the TeX command, without
355 further parsing. For example, C<texfot --debug tex
356 --debug> will output debugging information from both C<texfot> and
357 C<tex>. TeX engines, unlike many standard programs, require that options
358 be specified before the input filename or text.
359
360 Options may start with either - or --, and may be unambiguously
361 abbreviated. It is best to use the full option name in scripts, though,
362 to avoid possible collisions with new options in the future.
363
364 =over 4
365
366 =item C<--accept> I<regexp>
367
368 Accept lines in the TeX output matching (Perl) I<regexp>. Can be
369 repeated. This list is checked first, so any and all matches will be
370 shown, regardless of other options. These regexps are not automatically
371 anchored (or otherwise altered), simply used as-is.
372
373 =item C<--debug>
374
375 =item C<--no-debug>
376
377 Output (or not) what the program is doing to standard error; off by default.
378
379 =item C<--ignore> I<regexp>
380
381 Ignore lines in the TeX output matching (Perl) I<regexp>. Can be
382 repeated. Adds to the default set of ignore regexps rather than
383 replacing. Like the acceptance regexps, these are not automatically
384 anchored (or otherwise altered).
385
386 =item C<--interactive>
387
388 =item C<--no-interactive>
389
390 By default, standard input to the TeX process is closed so that TeX's
391 interactive mode (waiting for input upon error, the C<*> prompt, etc.)
392 is never entered. Giving C<--interactive> allows interaction to happen.
393
394 =item C<--quiet>
395
396 =item C<--no-quiet>
397
398 By default, the TeX command being invoked is reported on standard
399 output; C<--quiet> omits that reporting. To get a completely silent run,
400 redirect standard output: S<C<texfot ... E<gt>/dev/null>>. (The only
401 messages to standard error should be errors from C<texfot> itself, so it
402 shouldn't be necessary to redirect that, but of course that could be
403 done as well.)
404
405 =item C<--stderr>
406
407 =item C<--no-stderr>
408
409 The default is for C<texfot> to report everything written to stderr by
410 the TeX command (on stdout). C<--no-stderr> omits that reporting. (Some
411 programs, C<dvisvgm> is one, can be rather verbose on stderr.)
412
413 =item C<--tee> I<file>
414
415 By default, the output being filtered is C<tee>-ed, before filtering, to
416 make it easy to check the full output in case of problems.
417
418 The default I<file> is C<$TMPDIR/fot.>I<uid>; if C<TMPDIR> is not set,
419 C<TMP> is used if set; if neither is set, the default directory is
420 C</tmp>. For example: C</tmp/fot.1001>. The I<uid> suffix is the
421 effective userid of the process, appended for basic avoidance of
422 collisions between different users on the same system.
423
424 This option allows specifying a different file. Use S<C<--tee
425 /dev/null>> to discard the original output.
426
427 =item C<--version>
428
429 Output version information and exit successfully.
430
431 =item C<--help>
432
433 Display this help and exit successfully.
434
435 =back
436
437 =head1 RATIONALE
438
439 I wrote this because, in my work as a TUGboat editor
440 (L<https://tug.org/TUGboat>, article submissions always welcome!), I run
441 and rerun many documents, many times each. It was easy to lose warnings
442 I needed to see in the mass of unvarying and uninteresting output from
443 TeX, such as style files being read and fonts being used. I wanted to
444 see all and only those messages which needed some action by me.
445
446 I found some other programs of a similar nature, the LaTeX package
447 C<silence>, and plenty of other (La)TeX wrappers, but it seemed none of
448 them did what I wanted. Either they read the log file (I wanted to look
449 at only the online output), or they output more, or less, than I wanted,
450 or they required invoking TeX differently (I wanted to keep my build
451 process exactly the same, most critically the TeX invocation, which can
452 get complicated). Hence I wrote this little script.
453
454 Here are some keywords if you want to explore other options:
455 texloganalyser, pydflatex, logfilter, latexmk, rubber, arara, and
456 searching for C<log> at L<https://ctan.org/search>.
457
458 C<texfot> is written in Perl, and runs on Unix. It may work on Windows
459 if Perl and other software is installed, but I don't use Windows and
460 don't support C<texfot> there.
461
462 The name comes from the C<trip.fot> and C<trap.fot> files that are part
463 of Knuth's trip and trap torture tests, which record the online output
464 from the programs. I am not sure what "fot" stands for in trip and trap,
465 but I can pretend that it stands for "filter online transcript" in the
466 present S<case :).>
467
468 =head1 AUTHORS AND COPYRIGHT
469
470 This script and its documentation were written by Karl Berry and both
471 are released to the public domain. Email C<[email protected]> with
472 bug reports. It has no home page beyond the package page on CTAN:
473 L<https://ctan.org/pkg/texfot>.
474
475 $Id: texfot,v 1.54 2026/01/05 17:14:08 karl Exp $
476
477 =cut