| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
## This file is part of the Petri-nets packages. See file README for |
| 4 |
## copyright notice. |
| 5 |
|
| 6 |
use strict; |
| 7 |
use Digest::MD5 qw(md5_hex); |
| 8 |
use Getopt::Long; |
| 9 |
|
| 10 |
## |
| 11 |
## read options |
| 12 |
## |
| 13 |
|
| 14 |
Getopt::Long::Configure ("bundling"); |
| 15 |
|
| 16 |
my $keep = 0; |
| 17 |
my $force = 0; |
| 18 |
my $verbose = 0; |
| 19 |
|
| 20 |
GetOptions ("k|keep" => \$keep, |
| 21 |
"f|force" => \$force, |
| 22 |
"v|verbose" => \$verbose, |
| 23 |
"t|test" => \&Test, |
| 24 |
"d|delete" => \&Delete, |
| 25 |
"b|batch" => \&Batch, |
| 26 |
"h|help" => \&Help); |
| 27 |
|
| 28 |
&Help if scalar @ARGV > 1; |
| 29 |
|
| 30 |
## |
| 31 |
## Global variables |
| 32 |
## |
| 33 |
|
| 34 |
my $figure = shift @ARGV; |
| 35 |
my $basename = $figure; $basename =~ s/\.pn$//; |
| 36 |
my $jobname = $basename; $jobname =~ s/-fig\d+$//; |
| 37 |
my $data = ""; |
| 38 |
my %sum; |
| 39 |
|
| 40 |
## |
| 41 |
## Load checksums |
| 42 |
## |
| 43 |
|
| 44 |
if (-f "$jobname.sum") { |
| 45 |
open SUM, "$jobname.sum" |
| 46 |
or &Error ("could not open $jobname.sum -- $!"); |
| 47 |
while (<SUM>) { |
| 48 |
next unless /^(.+)\s+(.+)$/; |
| 49 |
$sum{$1} = $2; |
| 50 |
} |
| 51 |
close SUM; |
| 52 |
} |
| 53 |
|
| 54 |
## |
| 55 |
## Get preamble |
| 56 |
## |
| 57 |
|
| 58 |
if (-f "$jobname.pre") { |
| 59 |
# read the existing preamble |
| 60 |
my @lines; |
| 61 |
open PRE, "$jobname.pre" |
| 62 |
or &Error ("could not open $jobname.sum -- $!"); |
| 63 |
@lines = <PRE>; |
| 64 |
close PRE; |
| 65 |
$data .= join "", @lines; |
| 66 |
} else { |
| 67 |
# create a new preamble |
| 68 |
open TEX, "$jobname.tex" |
| 69 |
or &Error ("could not open $jobname.tex -- $!"); |
| 70 |
while (<TEX>) { |
| 71 |
$data .= $_; |
| 72 |
last if /\\begin\{document\}/; |
| 73 |
} |
| 74 |
close TEX; |
| 75 |
$data =~ s/(\\begin\{document\}).*$/$1\n/; |
| 76 |
$data .= "\\pagestyle\{empty\}\n"; |
| 77 |
# save it |
| 78 |
open PRE, ">$jobname.pre" |
| 79 |
or &Error ("could not create $jobname.sum -- $!"); |
| 80 |
print PRE $data; |
| 81 |
close PRE; |
| 82 |
} |
| 83 |
|
| 84 |
## |
| 85 |
## Read the figure |
| 86 |
## |
| 87 |
|
| 88 |
{ |
| 89 |
my $pre = 1; |
| 90 |
open TEX, $figure or &Error ("could not open $figure -- $!"); |
| 91 |
open PRE, ">>$jobname.pre" |
| 92 |
or &Error ("could not append $jobname.pre -- $!"); |
| 93 |
while (<TEX>) { |
| 94 |
$pre = 0 if /^\\begin\{petrinet\}/; |
| 95 |
print PRE if $pre; |
| 96 |
$data .= $_; |
| 97 |
} |
| 98 |
close TEX; |
| 99 |
close PRE; |
| 100 |
} |
| 101 |
|
| 102 |
## |
| 103 |
## Generate the EPDF |
| 104 |
## |
| 105 |
|
| 106 |
if (&SumChanged ($figure, "$data") or $force or (! -f "$basename.pdf")) { |
| 107 |
open TEX, ">$basename.tex" |
| 108 |
or &Error ("could not create file $basename.tex -- $!\n"); |
| 109 |
print TEX "$data\\end\{document\}\n"; |
| 110 |
close TEX; |
| 111 |
system "latex", "$basename.tex"; |
| 112 |
system "dvips", "-E", "-Ppdf", "-o", "$basename.eps", "$basename"; |
| 113 |
system "epstopdf", "$basename.eps"; |
| 114 |
} |
| 115 |
|
| 116 |
unlink ("$basename.tex", "$basename.log", "$basename.aux", |
| 117 |
"$basename.dvi", "$basename.eps", "$basename.pn") unless $keep; |
| 118 |
|
| 119 |
## |
| 120 |
## Save checksums |
| 121 |
## |
| 122 |
|
| 123 |
open SUM, ">$jobname.sum" |
| 124 |
or die "pn2pdf: could not create file $jobname.sum -- $!\n"; |
| 125 |
foreach my $file (sort keys %sum) { |
| 126 |
print SUM "$file $sum{$file}\n"; |
| 127 |
} |
| 128 |
close SUM; |
| 129 |
|
| 130 |
exit 0; |
| 131 |
|
| 132 |
## |
| 133 |
## Sub programs |
| 134 |
## |
| 135 |
|
| 136 |
sub Help { |
| 137 |
print <<"EOF"; |
| 138 |
Usage: pn2pdf [OPTIONS] |
| 139 |
Create EPDF files for Petri net figures. |
| 140 |
|
| 141 |
FIGURE process FIGURE |
| 142 |
-b, --batch FILE process the commands in FILE |
| 143 |
-k, --keep keep temporary files |
| 144 |
-f, --force force the rendering of all pictures |
| 145 |
-t, --test FILES create FILES for test purpose |
| 146 |
-d, --delete FILES delete FILES (may be patterns) |
| 147 |
-h, --help display this help and exit |
| 148 |
|
| 149 |
Report bugs to <pommereau\@univ-paris12.fr> |
| 150 |
EOF |
| 151 |
exit 0; } |
| 152 |
|
| 153 |
sub Error { |
| 154 |
foreach my $line (@_) { |
| 155 |
warn "pn2pdf: $line\n"; |
| 156 |
} |
| 157 |
exit 1; |
| 158 |
} |
| 159 |
|
| 160 |
sub Verbose { |
| 161 |
return unless $verbose; |
| 162 |
foreach my $line (@_) { |
| 163 |
warn "[pn2pdf] $line\n"; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
sub SumChanged { |
| 168 |
my ($file, $lines) = @_; |
| 169 |
my $md5 = md5_hex ($lines); |
| 170 |
if ($md5 eq $sum{$file}) { |
| 171 |
return 0; |
| 172 |
} else { |
| 173 |
$sum{$file} = $md5; |
| 174 |
return 1; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
sub Test { |
| 179 |
foreach my $file (@ARGV) { |
| 180 |
open FILE, ">$file" or &Error ("could not create $file -- $!"); |
| 181 |
close FILE; |
| 182 |
} |
| 183 |
exit 0; |
| 184 |
} |
| 185 |
|
| 186 |
sub Delete { |
| 187 |
foreach my $pattern (@ARGV) { |
| 188 |
unlink glob $pattern unless $keep; |
| 189 |
} |
| 190 |
exit 0; |
| 191 |
} |
| 192 |
|
| 193 |
sub Batch { |
| 194 |
my @lines; |
| 195 |
foreach my $file (@ARGV) { |
| 196 |
$file =~ s/(\.[a-z]{3})?$/.bpn/i; |
| 197 |
open FILE, $file or &Error ("could not open $file -- $!"); |
| 198 |
while (<FILE>) { |
| 199 |
next unless /^pn2pdf\s/; |
| 200 |
s/^pn2pdf\s/pn2pdf --keep / if $keep; |
| 201 |
s/^pn2pdf\s/pn2pdf --force / if $force; |
| 202 |
s/^pn2pdf\s/pn2pdf --verbose / if $verbose; |
| 203 |
push @lines, $_; |
| 204 |
} |
| 205 |
close FILE; |
| 206 |
} |
| 207 |
foreach (@lines) { |
| 208 |
system $_; |
| 209 |
} |
| 210 |
exit 0; |
| 211 |
} |