| 1 |
#! /usr/bin/env perl |
| 2 |
|
| 3 |
########################################################### |
| 4 |
# Prepare a LaTeX run for two-way communication with Perl # |
| 5 |
# By Scott Pakin <[email protected]> # |
| 6 |
########################################################### |
| 7 |
|
| 8 |
#------------------------------------------------------------------- |
| 9 |
# This is file `perltex.pl', |
| 10 |
# generated with the docstrip utility. |
| 11 |
# |
| 12 |
# The original source files were: |
| 13 |
# |
| 14 |
# perltex.dtx (with options: `perltex') |
| 15 |
# |
| 16 |
# This is a generated file. |
| 17 |
# |
| 18 |
# Copyright (C) 2003-2024 Scott Pakin <[email protected]> |
| 19 |
# |
| 20 |
# This file may be distributed and/or modified under the conditions |
| 21 |
# of the LaTeX Project Public License, either version 1.3c of this |
| 22 |
# license or (at your option) any later version. The latest |
| 23 |
# version of this license is in: |
| 24 |
# |
| 25 |
# http://www.latex-project.org/lppl.txt |
| 26 |
# |
| 27 |
# and version 1.3c or later is part of all distributions of LaTeX |
| 28 |
# version 2006/05/20 or later. |
| 29 |
#------------------------------------------------------------------- |
| 30 |
|
| 31 |
sub top_level_eval ($) |
| 32 |
{ |
| 33 |
return eval $_[0]; |
| 34 |
} |
| 35 |
use Safe; |
| 36 |
use Opcode; |
| 37 |
use Getopt::Long; |
| 38 |
use Pod::Usage; |
| 39 |
use File::Basename; |
| 40 |
use Fcntl; |
| 41 |
use POSIX; |
| 42 |
use File::Spec; |
| 43 |
use IO::Handle; |
| 44 |
use warnings; |
| 45 |
use strict; |
| 46 |
my $latexprog; |
| 47 |
my $runsafely = 1; |
| 48 |
my @permittedops; |
| 49 |
my $usepipe = 1; |
| 50 |
my $progname = basename $0; |
| 51 |
my $jobname = "texput"; |
| 52 |
my $toperl; |
| 53 |
my $fromperl; |
| 54 |
my $toflag; |
| 55 |
my $fromflag; |
| 56 |
my $doneflag; |
| 57 |
my $logfile; |
| 58 |
my $pipe; |
| 59 |
my @latexcmdline; |
| 60 |
my $styfile; |
| 61 |
my @macroexpansions; |
| 62 |
my $sandbox = new Safe; |
| 63 |
my $sandbox_eval; |
| 64 |
my $latexpid; |
| 65 |
my $pipestring = "\%\%\%\%\% Generated by $progname\n\\endinput\n"; |
| 66 |
$latexprog = $ENV{"PERLTEX"} || "latex"; |
| 67 |
Getopt::Long::Configure("require_order", "pass_through"); |
| 68 |
GetOptions("help" => sub {pod2usage(-verbose => 1)}, |
| 69 |
"latex=s" => \$latexprog, |
| 70 |
"safe!" => \$runsafely, |
| 71 |
"pipe!" => \$usepipe, |
| 72 |
"synctext=s" => \$pipestring, |
| 73 |
"makesty" => sub {$styfile = "noperltex.sty"}, |
| 74 |
"permit=s" => \@permittedops) || pod2usage(2); |
| 75 |
@latexcmdline = @ARGV; |
| 76 |
my $firstcmd = 0; |
| 77 |
for ($firstcmd=0; $firstcmd<=$#latexcmdline; $firstcmd++) { |
| 78 |
my $option = $latexcmdline[$firstcmd]; |
| 79 |
next if substr($option, 0, 1) eq "-"; |
| 80 |
if (substr ($option, 0, 1) ne "\\") { |
| 81 |
$jobname = basename $option, ".tex" ; |
| 82 |
$latexcmdline[$firstcmd] = "\\input $option"; |
| 83 |
} |
| 84 |
last; |
| 85 |
} |
| 86 |
push @latexcmdline, "" if $#latexcmdline==-1; |
| 87 |
my $separator = ""; |
| 88 |
foreach (1 .. 20) { |
| 89 |
$separator .= chr(ord("A") + rand(26)); |
| 90 |
} |
| 91 |
$toperl = $jobname . ".topl"; |
| 92 |
$fromperl = $jobname . ".frpl"; |
| 93 |
$toflag = $jobname . ".tfpl"; |
| 94 |
$fromflag = $jobname . ".ffpl"; |
| 95 |
$doneflag = $jobname . ".dfpl"; |
| 96 |
$logfile = $jobname . ".lgpl"; |
| 97 |
$pipe = $jobname . ".pipe"; |
| 98 |
$latexcmdline[$firstcmd] = |
| 99 |
sprintf '\makeatletter' . '\def%s{%s}' x 7 . '\makeatother%s', |
| 100 |
'\plmac@tag', $separator, |
| 101 |
'\plmac@tofile', $toperl, |
| 102 |
'\plmac@fromfile', $fromperl, |
| 103 |
'\plmac@toflag', $toflag, |
| 104 |
'\plmac@fromflag', $fromflag, |
| 105 |
'\plmac@doneflag', $doneflag, |
| 106 |
'\plmac@pipe', $pipe, |
| 107 |
$latexcmdline[$firstcmd]; |
| 108 |
$toperl = File::Spec->rel2abs($toperl); |
| 109 |
$fromperl = File::Spec->rel2abs($fromperl); |
| 110 |
$toflag = File::Spec->rel2abs($toflag); |
| 111 |
$fromflag = File::Spec->rel2abs($fromflag); |
| 112 |
$doneflag = File::Spec->rel2abs($doneflag); |
| 113 |
$logfile = File::Spec->rel2abs($logfile); |
| 114 |
$pipe = File::Spec->rel2abs($pipe); |
| 115 |
$SIG{"ALRM"} = sub { |
| 116 |
undef $latexpid; |
| 117 |
exit 0; |
| 118 |
}; |
| 119 |
$SIG{"PIPE"} = "IGNORE"; |
| 120 |
sub delete_files (@) |
| 121 |
{ |
| 122 |
foreach my $filename (@_) { |
| 123 |
unlink $filename; |
| 124 |
while (-e $filename) { |
| 125 |
unlink $filename; |
| 126 |
sleep 0; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
sub awaitexists ($) |
| 131 |
{ |
| 132 |
while (!-e $_[0]) { |
| 133 |
sleep 0; |
| 134 |
if (waitpid($latexpid, &WNOHANG)==-1) { |
| 135 |
delete_files($toperl, $fromperl, $toflag, |
| 136 |
$fromflag, $doneflag, $pipe); |
| 137 |
undef $latexpid; |
| 138 |
exit 0; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
delete_files($toperl, $fromperl, $toflag, $fromflag, $doneflag, $pipe); |
| 143 |
open (LOGFILE, ">$logfile") || die "open(\"$logfile\"): $!\n"; |
| 144 |
autoflush LOGFILE 1; |
| 145 |
if (defined $styfile) { |
| 146 |
open (STYFILE, ">$styfile") || die "open(\"$styfile\"): $!\n"; |
| 147 |
} |
| 148 |
if (!$usepipe || !eval {mkfifo($pipe, 0600)}) { |
| 149 |
sysopen PIPE, $pipe, O_WRONLY|O_CREAT, 0755; |
| 150 |
autoflush PIPE 1; |
| 151 |
print PIPE $pipestring; |
| 152 |
close PIPE; |
| 153 |
$usepipe = 0; |
| 154 |
} |
| 155 |
defined ($latexpid = fork) || die "fork: $!\n"; |
| 156 |
unshift @latexcmdline, $latexprog; |
| 157 |
if (!$latexpid) { |
| 158 |
exec {$latexcmdline[0]} @latexcmdline; |
| 159 |
die "exec('@latexcmdline'): $!\n"; |
| 160 |
} |
| 161 |
if ($runsafely) { |
| 162 |
@permittedops=(":browse") if $#permittedops==-1; |
| 163 |
$sandbox->permit_only (@permittedops); |
| 164 |
$sandbox_eval = sub {$sandbox->reval($_[0])}; |
| 165 |
} |
| 166 |
else { |
| 167 |
$sandbox_eval = \&top_level_eval; |
| 168 |
} |
| 169 |
while (1) { |
| 170 |
awaitexists($toflag); |
| 171 |
my $entirefile; |
| 172 |
{ |
| 173 |
local $/ = undef; |
| 174 |
open (TOPERL, "<$toperl") || die "open($toperl): $!\n"; |
| 175 |
$entirefile = <TOPERL>; |
| 176 |
close TOPERL; |
| 177 |
} |
| 178 |
$entirefile =~ s/\r//g; |
| 179 |
my ($optag, $macroname, @otherstuff) = |
| 180 |
map {chomp; $_} split "$separator\n", $entirefile; |
| 181 |
$macroname =~ s/^[^A-Za-z]+//; |
| 182 |
$macroname =~ s/\W/_/g; |
| 183 |
$macroname = "latex_" . $macroname; |
| 184 |
if ($optag eq "USE") { |
| 185 |
foreach (@otherstuff) { |
| 186 |
s/\\/\\\\/g; |
| 187 |
s/\'/\\\'/g; |
| 188 |
$_ = "'$_'"; |
| 189 |
} |
| 190 |
} |
| 191 |
my $perlcode; |
| 192 |
if ($optag eq "DEF") { |
| 193 |
$perlcode = |
| 194 |
sprintf "sub %s {%s}\n", |
| 195 |
$macroname, $otherstuff[0]; |
| 196 |
} |
| 197 |
elsif ($optag eq "USE") { |
| 198 |
$perlcode = sprintf "%s (%s);\n", $macroname, join(", ", @otherstuff); |
| 199 |
} |
| 200 |
elsif ($optag eq "RUN") { |
| 201 |
$perlcode = $otherstuff[0]; |
| 202 |
} |
| 203 |
else { |
| 204 |
die "${progname}: Internal error -- unexpected operation tag \"$optag\"\n"; |
| 205 |
} |
| 206 |
print LOGFILE "#" x 31, " PERL CODE ", "#" x 32, "\n"; |
| 207 |
print LOGFILE $perlcode, "\n"; |
| 208 |
undef $_; |
| 209 |
my $result; |
| 210 |
{ |
| 211 |
my $warningmsg; |
| 212 |
local $SIG{__WARN__} = |
| 213 |
sub {chomp ($warningmsg=$_[0]); return 0}; |
| 214 |
$result = $sandbox_eval->($perlcode); |
| 215 |
if (defined $warningmsg) { |
| 216 |
$warningmsg =~ s/at \(eval \d+\) line \d+\W+//; |
| 217 |
print LOGFILE "# ===> $warningmsg\n\n"; |
| 218 |
} |
| 219 |
} |
| 220 |
$result = "" if !$result || $optag eq "RUN"; |
| 221 |
if ($@) { |
| 222 |
my $msg = $@; |
| 223 |
$msg =~ s/at \(eval \d+\) line \d+\W+//; |
| 224 |
$msg =~ s/\n/\\MessageBreak\n/g; |
| 225 |
$msg =~ s/\s+/ /; |
| 226 |
$result = "\\PackageError{perltex}{$msg}"; |
| 227 |
my @helpstring; |
| 228 |
if ($msg =~ /\btrapped by\b/) { |
| 229 |
@helpstring = |
| 230 |
("The preceding error message comes from Perl. Apparently,", |
| 231 |
"the Perl code you tried to execute attempted to perform an", |
| 232 |
"`unsafe' operation. If you trust the Perl code (e.g., if", |
| 233 |
"you wrote it) then you can invoke perltex with the --nosafe", |
| 234 |
"option to allow arbitrary Perl code to execute.", |
| 235 |
"Alternatively, you can selectively enable Perl features", |
| 236 |
"using perltex's --permit option. Don't do this if you don't", |
| 237 |
"trust the Perl code, however; malicious Perl code can do a", |
| 238 |
"world of harm to your computer system."); |
| 239 |
} |
| 240 |
else { |
| 241 |
@helpstring = |
| 242 |
("The preceding error message comes from Perl. Apparently,", |
| 243 |
"there's a bug in your Perl code. You'll need to sort that", |
| 244 |
"out in your document and re-run perltex."); |
| 245 |
} |
| 246 |
my $helpstring = join ("\\MessageBreak\n", @helpstring); |
| 247 |
$helpstring =~ s/\. /.\\space\\space /g; |
| 248 |
$result .= "{$helpstring}"; |
| 249 |
} |
| 250 |
push @macroexpansions, $result if defined $styfile && $optag eq "USE"; |
| 251 |
print LOGFILE "%" x 30, " LATEX RESULT ", "%" x 30, "\n"; |
| 252 |
print LOGFILE $result, "\n\n"; |
| 253 |
$result .= '\endinput'; |
| 254 |
open (FROMPERL, ">$fromperl") || die "open($fromperl): $!\n"; |
| 255 |
syswrite FROMPERL, $result; |
| 256 |
close FROMPERL; |
| 257 |
delete_files($toflag, $toperl, $doneflag); |
| 258 |
open (FROMFLAG, ">$fromflag") || die "open($fromflag): $!\n"; |
| 259 |
close FROMFLAG; |
| 260 |
if (open (PIPE, ">$pipe")) { |
| 261 |
autoflush PIPE 1; |
| 262 |
print PIPE $pipestring; |
| 263 |
close PIPE; |
| 264 |
} |
| 265 |
awaitexists($toperl); |
| 266 |
delete_files($fromflag); |
| 267 |
open (DONEFLAG, ">$doneflag") || die "open($doneflag): $!\n"; |
| 268 |
close DONEFLAG; |
| 269 |
alarm 1; |
| 270 |
if (open (PIPE, ">$pipe")) { |
| 271 |
autoflush PIPE 1; |
| 272 |
print PIPE $pipestring; |
| 273 |
close PIPE; |
| 274 |
} |
| 275 |
alarm 0; |
| 276 |
} |
| 277 |
END { |
| 278 |
close LOGFILE; |
| 279 |
if (defined $latexpid) { |
| 280 |
kill (9, $latexpid); |
| 281 |
exit 1; |
| 282 |
} |
| 283 |
|
| 284 |
if (defined $styfile) { |
| 285 |
print STYFILE <<"STYFILEHEADER1"; |
| 286 |
\\NeedsTeXFormat{LaTeX2e}[1999/12/01] |
| 287 |
\\ProvidesPackage{noperltex} |
| 288 |
[2007/09/29 v1.4 Perl-free version of PerlTeX specific to $jobname.tex] |
| 289 |
STYFILEHEADER1 |
| 290 |
; |
| 291 |
print STYFILE <<'STYFILEHEADER2'; |
| 292 |
\RequirePackage{filecontents} |
| 293 |
|
| 294 |
\let\noperltex@PackageError=\PackageError |
| 295 |
\renewcommand{\PackageError}[3]{} |
| 296 |
\RequirePackage{perltex} |
| 297 |
\let\PackageError=\noperltex@PackageError |
| 298 |
|
| 299 |
\newcount\plmac@macro@invocation@num |
| 300 |
\gdef\plmac@show@placeholder#1#2\@empty{% |
| 301 |
\ifx#1U\relax |
| 302 |
\endgroup |
| 303 |
\advance\plmac@macro@invocation@num by 1\relax |
| 304 |
\global\plmac@macro@invocation@num=\plmac@macro@invocation@num |
| 305 |
\input{noperltex-\the\plmac@macro@[email protected]}% |
| 306 |
\else |
| 307 |
\endgroup |
| 308 |
\fi |
| 309 |
} |
| 310 |
STYFILEHEADER2 |
| 311 |
; |
| 312 |
foreach my $e (0 .. $#macroexpansions) { |
| 313 |
print STYFILE "\n"; |
| 314 |
printf STYFILE "%% Invocation #%d\n", 1+$e; |
| 315 |
printf STYFILE "\\begin{filecontents}{noperltex-%d.tex}\n", 1+$e; |
| 316 |
print STYFILE $macroexpansions[$e], "\\endinput\n"; |
| 317 |
print STYFILE "\\end{filecontents}\n"; |
| 318 |
} |
| 319 |
print STYFILE "\\endinput\n"; |
| 320 |
close STYFILE; |
| 321 |
} |
| 322 |
|
| 323 |
exit 0; |
| 324 |
} |
| 325 |
|
| 326 |
__END__ |
| 327 |
|
| 328 |
=head1 NAME |
| 329 |
|
| 330 |
perltex - enable LaTeX macros to be defined in terms of Perl code |
| 331 |
|
| 332 |
=head1 SYNOPSIS |
| 333 |
|
| 334 |
perltex |
| 335 |
[B<--help>] |
| 336 |
[B<--latex>=I<program>] |
| 337 |
[B<-->[B<no>]B<safe>] |
| 338 |
[B<--permit>=I<feature>] |
| 339 |
[B<--makesty>] |
| 340 |
[I<latex options>] |
| 341 |
|
| 342 |
=head1 DESCRIPTION |
| 343 |
|
| 344 |
LaTeX -- through the underlying TeX typesetting system -- produces |
| 345 |
beautifully typeset documents but has a macro language that is |
| 346 |
difficult to program. In particular, support for complex string |
| 347 |
manipulation is largely lacking. Perl is a popular general-purpose |
| 348 |
programming language whose forte is string manipulation. However, it |
| 349 |
has no typesetting capabilities whatsoever. |
| 350 |
|
| 351 |
Clearly, Perl's programmability could complement LaTeX's typesetting |
| 352 |
strengths. B<perltex> is the tool that enables a symbiosis between |
| 353 |
the two systems. All a user needs to do is compile a LaTeX document |
| 354 |
using B<perltex> instead of B<latex>. (B<perltex> is actually a |
| 355 |
wrapper for B<latex>, so no B<latex> functionality is lost.) If the |
| 356 |
document includes a C<\usepackage{perltex}> in its preamble, then |
| 357 |
C<\perlnewcommand> and C<\perlrenewcommand> macros will be made |
| 358 |
available. These behave just like LaTeX's C<\newcommand> and |
| 359 |
C<\renewcommand> except that the macro body contains Perl code instead |
| 360 |
of LaTeX code. |
| 361 |
|
| 362 |
=head1 OPTIONS |
| 363 |
|
| 364 |
B<perltex> accepts the following command-line options: |
| 365 |
|
| 366 |
=over 4 |
| 367 |
|
| 368 |
=item B<--help> |
| 369 |
|
| 370 |
Display basic usage information. |
| 371 |
|
| 372 |
=item B<--latex>=I<program> |
| 373 |
|
| 374 |
Specify a program to use instead of B<latex>. For example, |
| 375 |
C<--latex=pdflatex> would typeset the given document using |
| 376 |
B<pdflatex> instead of ordinary B<latex>. |
| 377 |
|
| 378 |
=item B<-->[B<no>]B<safe> |
| 379 |
|
| 380 |
Enable or disable sandboxing. With the default of B<--safe>, |
| 381 |
B<perltex> executes the code from a C<\perlnewcommand> or |
| 382 |
C<\perlrenewcommand> macro within a protected environment that |
| 383 |
prohibits ``unsafe'' operations such as accessing files or executing |
| 384 |
external programs. Specifying B<--nosafe> gives the LaTeX document |
| 385 |
I<carte blanche> to execute any arbitrary Perl code, including that |
| 386 |
which can harm the user's files. See L<Safe> for more information. |
| 387 |
|
| 388 |
=item B<--permit>=I<feature> |
| 389 |
|
| 390 |
Permit particular Perl operations to be performed. The B<--permit> |
| 391 |
option, which can be specified more than once on the command line, |
| 392 |
enables finer-grained control over the B<perltex> sandbox. See |
| 393 |
L<Opcode> for more information. |
| 394 |
|
| 395 |
=item B<--makesty> |
| 396 |
|
| 397 |
Generate a LaTeX style file called F<noperltex.sty>. Replacing the |
| 398 |
document's C<\usepackage{perltex}> line with C<\usepackage{noperltex}> |
| 399 |
produces the same output but does not require PerlTeX, making the |
| 400 |
document suitable for distribution to people who do not have PerlTeX |
| 401 |
installed. The disadvantage is that F<noperltex.sty> is specific to |
| 402 |
the document that produced it. Any changes to the document's PerlTeX |
| 403 |
macro definitions or macro invocations necessitates rerunning |
| 404 |
B<perltex> with the B<--makesty> option. |
| 405 |
|
| 406 |
=back |
| 407 |
|
| 408 |
These options are then followed by whatever options are normally |
| 409 |
passed to B<latex> (or whatever program was specified with |
| 410 |
C<--latex>), including, for instance, the name of the F<.tex> file to |
| 411 |
compile. |
| 412 |
|
| 413 |
=head1 EXAMPLES |
| 414 |
|
| 415 |
In its simplest form, B<perltex> is run just like B<latex>: |
| 416 |
|
| 417 |
perltex myfile.tex |
| 418 |
|
| 419 |
To use B<pdflatex> instead of regular B<latex>, use the B<--latex> |
| 420 |
option: |
| 421 |
|
| 422 |
perltex --latex=pdflatex myfile.tex |
| 423 |
|
| 424 |
If LaTeX gives a ``C<trapped by operation mask>'' error and you trust |
| 425 |
the F<.tex> file you're trying to compile not to execute malicious |
| 426 |
Perl code (e.g., because you wrote it yourself), you can disable |
| 427 |
B<perltex>'s safety mechansisms with B<--nosafe>: |
| 428 |
|
| 429 |
perltex --nosafe myfile.tex |
| 430 |
|
| 431 |
The following command gives documents only B<perltex>'s default |
| 432 |
permissions (C<:browse>) plus the ability to open files and invoke the |
| 433 |
C<time> command: |
| 434 |
|
| 435 |
perltex --permit=:browse --permit=:filesys_open |
| 436 |
--permit=time myfile.tex |
| 437 |
|
| 438 |
=head1 ENVIRONMENT |
| 439 |
|
| 440 |
B<perltex> honors the following environment variables: |
| 441 |
|
| 442 |
=over 4 |
| 443 |
|
| 444 |
=item PERLTEX |
| 445 |
|
| 446 |
Specify the filename of the LaTeX compiler. The LaTeX compiler |
| 447 |
defaults to ``C<latex>''. The C<PERLTEX> environment variable |
| 448 |
overrides this default, and the B<--latex> command-line option (see |
| 449 |
L</OPTIONS>) overrides that. |
| 450 |
|
| 451 |
=back |
| 452 |
|
| 453 |
=head1 FILES |
| 454 |
|
| 455 |
While compiling F<jobname.tex>, B<perltex> makes use of the following |
| 456 |
files: |
| 457 |
|
| 458 |
=over 4 |
| 459 |
|
| 460 |
=item F<jobname.lgpl> |
| 461 |
|
| 462 |
log file written by Perl; helpful for debugging Perl macros |
| 463 |
|
| 464 |
=item F<jobname.topl> |
| 465 |
|
| 466 |
information sent from LaTeX to Perl |
| 467 |
|
| 468 |
=item F<jobname.frpl> |
| 469 |
|
| 470 |
information sent from Perl to LaTeX |
| 471 |
|
| 472 |
=item F<jobname.tfpl> |
| 473 |
|
| 474 |
``flag'' file whose existence indicates that F<jobname.topl> contains |
| 475 |
valid data |
| 476 |
|
| 477 |
=item F<jobname.ffpl> |
| 478 |
|
| 479 |
``flag'' file whose existence indicates that F<jobname.frpl> contains |
| 480 |
valid data |
| 481 |
|
| 482 |
=item F<jobname.dfpl> |
| 483 |
|
| 484 |
``flag'' file whose existence indicates that F<jobname.ffpl> has been |
| 485 |
deleted |
| 486 |
|
| 487 |
=item F<noperltex-#.tex> |
| 488 |
|
| 489 |
file generated by F<noperltex.sty> for each PerlTeX macro invocation |
| 490 |
|
| 491 |
=back |
| 492 |
|
| 493 |
=head1 NOTES |
| 494 |
|
| 495 |
B<perltex>'s sandbox defaults to what L<Opcode> calls ``C<:browse>''. |
| 496 |
|
| 497 |
=head1 SEE ALSO |
| 498 |
|
| 499 |
latex(1), pdflatex(1), perl(1), Safe(3pm), Opcode(3pm) |
| 500 |
|
| 501 |
=head1 AUTHOR |
| 502 |
|
| 503 |
Scott Pakin, I<[email protected]> |