Parent Directory
|
Revision Log
adapted pdf_latex to work with pdflatex. call it pip_pdflatex. it doesn't exactly work yet. it might not be worth it. -sam
1 #!/usr/bin/perl -w 2 # 3 # pip_latex 4 # 5 # Wrapper for latex to use it as a filter. I would use the ordinary 6 # pip, but tex has its own pecularities which need their own wrapper. 7 # 8 # Usage: 9 # 10 # % pip_latex [-v [0|1|2]] [-L] 11 # to read LaTeX source from stdin, write DVI to stdout 12 # 13 # Alternatively, invoke as 'pip_tex' to use as a wrapper for tex. 14 # 15 # This program makes some effort to weed out actual error messages 16 # from tex's output, but they're not very parseable so it will 17 # probably miss lots. There are three ways to deal with errors: 18 # 19 # -v 0 default: only error lines (if any) printed to stderr 20 # -v 1 if any errors, all of tex's output printed 21 # -v 2 get the full TeX verbosity every time 22 # 23 # Finding what exactly is an error (rather than just chatter) seems 24 # very difficult. So the 'detection' catches only serious errors 25 # marked with '!', not overfull hboxes and the like. 26 # 27 # The flag -l means that the TeX logfile, rather than its ordinary 28 # output, will be printed to stdout. So for example -l -v1 will 29 # examine tex's ordinary output for 'errors', and if any are found, 30 # print the logfile to stderr. (-v0 -l makes no sense.) 31 # 32 # Version 0.1, for pip distribution 0.2.2 33 # 34 # This program is in the public domain. Use at your own risk. 35 # <http://www.doc.ic.ac.uk/~epa98/work/apps/pip/>. 36 # 37 # -- Ed Avis, epa98@doc.ic.ac.uk, 2001-01-22 38 # 39 40 use strict; 41 42 #my $TEX = ($0 =~ /pip_tex$/i) ? 'tex' : 'latex'; 43 my $TEX = ($0 =~ /pip_(.*)$/i) && $1; 44 my $TEXPUT='texput'; 45 my $AUX = '.aux'; 46 #my $DVI = '.dvi'; 47 my $DVI = '.pdf'; 48 my $LOG = '.log'; 49 my $DEV_NULL = '/dev/null'; 50 my $BUFSIZE = 100000; 51 52 # Prototypes 53 sub tmpdir(); 54 sub print_fh($$); 55 sub is_tex_error($); 56 57 use Getopt::Std; 58 use vars qw[$opt_v $opt_l]; getopts('v:l'); 59 $opt_v = 0 if not defined $opt_v; 60 if (@ARGV or (defined $opt_v and $opt_v !~ /^[012]$/)) { 61 die "usage: $0 [-v [0|1|2]] [-l]"; 62 } 63 64 my $dir = tmpdir(); 65 chdir $dir or die "cannot chdir to $dir: $!"; 66 67 # FIXME: should look at exit status from $TEX. Could use my_system() 68 # from the unarc distribution? 69 # 70 open (TEXOUT, "$TEX 2>&1 |") 71 or die "cannot run $TEX: $!"; 72 73 if ($opt_v == 0 and not $opt_l) { 74 # Look for errors and print them 75 while (<TEXOUT>) { 76 print STDERR if is_tex_error($_); 77 } 78 } 79 elsif ($opt_v == 0 and $opt_l) { 80 die '-l makes no sense without -v1 or -v2'; 81 } 82 elsif ($opt_v == 1 and not $opt_l) { 83 # Look for errors, if any then print the entire output 84 my $backlog = ''; 85 while (<TEXOUT>) { 86 if (is_tex_error($_)) { 87 # Found an error - print the entire output 88 print STDERR $backlog; 89 print STDERR; 90 print_fh(\*TEXOUT, \*STDERR); 91 last; 92 } 93 else { 94 $backlog .= $_; 95 } 96 } 97 } 98 elsif ($opt_v == 1 and $opt_l) { 99 # Look for errors, if any then print the logfile 100 while (<TEXOUT>) { 101 if (is_tex_error($_)) { 102 open (LOG, "$TEXPUT$LOG") 103 or die "cannot open $TEXPUT$LOG in $dir: $!"; 104 print_fh(\*LOG, \*STDERR); 105 last; 106 } 107 } 108 } 109 elsif ($opt_v == 2 and not $opt_l) { 110 # Show all of tex's stdout 111 print_fh(\*TEXOUT, \*STDERR); 112 } 113 elsif ($opt_v == 2 and $opt_l) { 114 # Read all of tex's output so it finishes 115 read(TEXOUT, $_, $BUFSIZE) 116 until eof TEXOUT; 117 118 # Show the logfile 119 open (LOG, "$TEXPUT$LOG") 120 or die "cannot open $TEXPUT$LOG in $dir: $!"; 121 print_fh(\*LOG, \*STDERR); 122 } 123 else { die } 124 125 # Print DVI to stdout, if there is one 126 if (open (DVI, "$TEXPUT$DVI")) { 127 print_fh(\*DVI, \*STDOUT); 128 } 129 else { 130 warn "cannot open $TEXPUT$DVI in $dir: $!, producing no output"; 131 } 132 133 # Clean up. For some reason tex sometimes creates files named '.dvi' 134 # etc. 135 # 136 foreach ($LOG, $AUX, $DVI) { 137 (not -f) or unlink or die "cannot unlink $_ from $dir: $!"; 138 $_ = "$TEXPUT$_"; 139 (not -f) or unlink or die "cannot unlink $_ from $dir: $!"; 140 } 141 142 foreach (<*>, <.*>) { 143 next if $_ eq '.' or $_ eq '..'; 144 warn "found unexpected output file $_ in $dir"; 145 unlink or die "cannot unlink $_ from $dir: $!"; 146 } 147 148 chdir '..' or die "cannot chdir to .. from $dir: $!"; 149 rmdir $dir or die "cannot rmdir $dir: $!"; 150 151 152 # tmpdir() 153 # 154 # Create a temporary directory and return its name. 155 # 156 sub tmpdir() { 157 die 'usage: tmpdir()' if @_; 158 159 my $tmp; 160 foreach (@ENV{qw[TMP TMPDIR TEMP]}, 161 '/tmp', '/usr/tmp', '/var/tmp', '/temp' ) 162 { 163 if (defined and -d and -r and -w and -x) { 164 $tmp = $_; 165 last; 166 } 167 } 168 169 my $r = "$tmp/$$"; 170 171 # FIXME: This may be a security hole? 172 warn "stale $r exists, trying to reuse" if -e $r; 173 174 mkdir $r, 0700 or die "cannot mkdir $r: $!"; 175 return $r; 176 } 177 178 179 # print_fh() 180 # 181 # Read all the characters left in a filehandle and print them to 182 # another filehandle. 183 # 184 # Parameters: 185 # filehandle to read from (ref to filehandle glob) 186 # filehandle to write to 187 # 188 # For example, 189 # print_fh(\*IN, \*OUT) 190 # 191 # This is equivalent to while (<IN>) { print OUT }, but faster. 192 # 193 sub print_fh($$) { 194 die 'usage: print_fh(input fh, output fh)' if @_ != 2; 195 my ($in, $out) = @_; 196 local $_; 197 until (eof $in) { 198 read($in, $_, $BUFSIZE) 199 or die 'error reading from filehandle, or undetected eof'; 200 print $out $_; 201 } 202 } 203 204 205 # is_tex_error() 206 # 207 # An attempt to guess whether a line of TeX output or logfile is an 208 # error message. But this is almost impossible, so we catch only 209 # serious errors, those beginning with '!', '*!', '**!', etc. 210 # 211 sub is_tex_error($) { 212 die 'usage: is_tex_error(line of output from tex)' if @_ != 1; 213 local $_ = shift; 214 /^\**!/; 215 } 216
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |