Debian Codegraph @

texmf-dist/doc/latex/examplep/wrfiles.pl

raw
1 #! /bin/sh
2 eval '(exit $?0)' && eval 'PERL_BADLANG=x;PATH="$PATH:.";export PERL_BADLANG\
3 ;exec perl -x -S -- "$0" ${1+"$@"};#'if 0;eval 'setenv PERL_BADLANG x\
4 ;setenv PATH "$PATH":.;exec perl -x -S -- "$0" $argv:q;#'.q
5 #!perl -w
6 +push@INC,'.';$0=~/(.*)/s;do(index($1,"/")<0?"./$1":$1);die$@if$@__END__+if 0
7 ;#Don't touch/remove lines 1--7: http://www.inf.bme.hu/~pts/Magic.Perl.Header
8 #
9 # wrfiles.pl -- write single example files
10 # by [email protected] at Mon Mar 15 17:01:41 CET 2004
11 #
12 use integer;
13 use strict;
14
15 #** @param $_[0] "config.ps", "config" or "pdftex.cfg" etc.
16 #** @param $_[1] argument of --progname
17 #** @param $_[2] argument of --format
18 #** @return the absoulte version of $_[0] -- or relative if in current dir
19 sub get_absname($$$) {
20 my $confrel=$_[0]; $confrel=~y@\\@/@;
21 my $confabs=$confrel;
22 if (substr($confrel,0,1)ne'/') {
23 if (index($confrel,'/')==-1 and (-f $confrel)) { # in current dir
24 return "./$confrel";
25 }
26 # Dat: need to append $PWD to: 0>index($confrel,'/') and (-f$confrel
27 # vvv $confrel and $_[1] are assumed not to contain weird characters
28 my $cmd="kpsewhich --must-exist --progname=$_[1] --format=\"$_[2]\" -- \"$confrel\" 2>&1";
29 $confabs=qx($cmd);
30 chomp $confabs;
31 }
32 # Dat: $confabs might begin with `./'
33 (length($confabs)<length($confrel)-1
34 # or substr($confabs,-length($confrel)-1,1) ne "/"
35 # or substr($confabs,-length($confrel)) ne $confrel # $confrel may contain `..'
36 or !-f $confabs) ? undef : $confabs
37 }
38
39 sub romannumeral($) {
40 my $N=$_[0]+0;
41 my $ret="";
42 if ($N>0) {
43 if ($N>=1000) { $ret="m"x($N%1000); $N=$N%1000 }
44 if ($N>=900) { $ret.="cm"; $N-=900 }
45 if ($N>=500) { $ret.="d"; $N-=500 }
46 if ($N>=400) { $ret.="cd"; $N-=400 }
47 while ($N>=100) { $ret.="c"; $N-=100 }
48 if ($N>=90) { $ret.="xc"; $N-=90 }
49 if ($N>=50) { $ret.="l"; $N-=50 }
50 if ($N>=40) { $ret.="xl"; $N-=40 }
51 while ($N>=10) { $ret.="x"; $N-=10 }
52 if ($N>=9) { $ret.="ix"; $N-=9 }
53 if ($N>=5) { $ret.="v"; $N-=5 }
54 if ($N>=4) { $ret.="iv"; $N-=4 }
55 while ($N>=1) { $ret.="i"; $N-=1 }
56 }
57 $ret
58 }
59
60 my $destdir='CDfiles';
61 my $default_D="\\documentclass{article}\n\n";
62 my $default_L="\\usepackage[latin2]{inputenc}
63 \\usepackage[T1]{fontenc}
64 \\usepackage[magyar]{babel}\n";
65 my $default_B="\n\\begin{document}\n\n";
66 my $default_E="\n\\end{document}\n";
67
68 my %to_coco=('0 '=>'p ','c '=>'< ','o '=>'> ','2 '=>' ');
69 my %to_class=('f '=>'F','v '=>'F','f!'=>'F','v!'=>'F','d '=>'D',
70 'l '=>'L','p '=>'P','t '=>'P','b '=>'B','< '=>'C','> '=>'C','x '=>'C',
71 ' '=>'C','e '=>'E',"\n\n"=>'Z','w '=>'C','% '=>'C','s '=>'C');
72
73 my %cleanup_files;
74 sub cleanup() { while (my($k,$v)=each%cleanup_files) { unlink $k if $v!=0 } }
75 $SIG{INT}=$SIG{TERM}=$SIG{HUP}=sub { cleanup; exit 1 };
76 END { cleanup }
77
78 my %had_files; # $had_files{$filename}++
79 sub do_code($$$) {
80 my($texfn,$lineno,$destfn)=@_;
81 die if $lineno<1;
82 my $texabs=get_absname($texfn,"latex","tex"); # adds .tex extension (but not needed)
83 die "$0: .tex file not found: $texfn\n" if !defined $texabs;
84 die unless open F, "< $texabs";
85 if (exists $had_files{$destfn}) {
86 if ($destfn=~s@^((?:(?:\d+|[a-zA-Z])_)?(?:\d+|[a-zA-Z]{1,5}))([_.])@$2@) {
87 # vvv change `1_2_foo.tex' to `1_2ii_foo.tex'. It won't conflict with
88 # test (1)
89 $destfn=$1.romannumeral(++$had_files{$1.$destfn}).$destfn;
90 die if exists $had_files{$destfn};
91 } else {
92 die "$0: multiple target file: $destfn ($texfn:$.)\n";
93 }
94 } else {
95 $had_files{$destfn}=1
96 }
97 $cleanup_files{"$destdir/$destfn"}++;
98 die unless open KI, "> $destdir/$destfn";
99 my $S;
100 my $L=$lineno;
101 while ($L-->0) { die "$0: file too short\n" if !defined($S=<F>) }
102 # Dat: now $S is the line of the original $lineno
103 chomp $S;
104 die "$0: not a code environment ($S) at $texfn:$lineno.\nMaybe wrong \\input{} or \\include{}?\n"
105 if $S!~m@\\begin{(pelda|peldak|kod|code)}(.*)@;
106 my $currenvir=$1;
107 $S=$2; $S=~s@[ \t\r\n]+\Z(?!\n)@@; # Dat: LaTeX also ignores this
108 my $going_p=1;
109 my %had_from_class;
110 print "Dumping env $currenvir from $texfn:$lineno to $destdir/$destfn\n";
111 if ($currenvir eq 'kod' or $currenvir eq 'pelda') {
112 # Dat: kod, pelda and peldak are legacy environments from lakk
113 die if !print KI $default_D.$default_L.$default_B;
114 while ($going_p) {
115 if (0==length$S) {
116 die "$0: unexpected EOF\n" if !defined($S=<F>);
117 $going_p=$currenvir eq 'kod' ? $S!~s@\\end\{kod\}.*@@s : $S!~s@\\end\{pelda\}.*@@s;
118 last if !$going_p and 0==length($S);
119 chomp $S;
120 }
121 die if !print KI "$S\n";
122 $S="";
123 }
124 die if !print KI $default_E;
125 } elsif ($currenvir eq 'code') {
126 my $oldclass='F';
127 while (1) {
128 if (0==length$S) {
129 die "$0: unexpected EOF\n" if !defined($S=<F>);
130 chomp $S;
131 $going_p=$S!~s@\\end\{code\}.*@@s;
132 $S="\n\n" if !$going_p and 0==length($S);
133 }
134 if (length($S)==0) { $S=" " }
135 elsif (length($S)==1) { $S.=" " }
136 do_end:
137 my $coco=substr($S,0,2); # code command
138 $S=substr($S,2);
139 ## print"[$coco][$S]\n";
140 $coco=$to_coco{$coco} if exists $to_coco{$coco};
141 my $curclass;
142 die "$0: unknown code command `$coco'\n" if !defined($curclass=$to_class{$coco});
143 my $oldclass0=$oldclass;
144 while ($oldclass ne $curclass) {
145 if ($oldclass eq 'F') {
146 $oldclass='G';
147 } elsif ($oldclass eq 'G') {
148 $oldclass='D';
149 } elsif ($oldclass eq 'D') {
150 die if !exists $had_from_class{'D'} and !print KI $default_D;
151 $oldclass='L';
152 } elsif ($oldclass eq 'L') {
153 die if !exists $had_from_class{'L'} and !print KI $default_L;
154 $oldclass='P';
155 } elsif ($oldclass eq 'P') {
156 $oldclass='B';
157 } elsif ($oldclass eq 'B') {
158 die if !exists $had_from_class{'B'} and !print KI $default_B;
159 $oldclass='C';
160 } elsif ($oldclass eq 'C') {
161 $oldclass='E';
162 } elsif ($oldclass eq 'E') {
163 die if !exists $had_from_class{'E'} and !print KI $default_E;
164 $oldclass='Z';
165 } elsif ($oldclass eq 'Z') {
166 die "cannot move from class $oldclass0 to $curclass\n";
167 }
168 }
169 ## print "($curclass)($coco)($S)\n";
170 if ($curclass eq 'F') {
171 die "$0: inconsistent filename: $destfn\n" if length($destfn)<length($S)
172 or substr($destfn,length($destfn)-length$S) ne $S; # test (1)
173 if ($coco eq 'v!' or $coco eq 'v ') { $oldclass='C'; $had_from_class{'E'}=1 }
174 } elsif ($coco eq '> ' or $coco eq '% ' or $coco eq 's ') {
175 } elsif ($curclass eq 'D' or $curclass eq 'L' or $curclass eq 'P'
176 or $curclass eq 'B' or $curclass eq 'E' or $curclass eq 'C') {
177 die if !print KI "$S\n";
178 $had_from_class{$curclass}=1;
179 } elsif ($curclass eq 'Z') {
180 last
181 } else { die }
182 if (!$going_p) { $S="\n\n"; goto do_end }
183 $S="";
184 }
185 } elsif ($currenvir eq 'peldak') {
186 die if !print KI $default_D.$default_L.$default_B;
187 if (!length$S) { $S="" if !defined($S=<F>) }
188 die "$0: opening brace expected, got: $S ($.)\n" if substr($S,0,1) ne '{';
189 $S=substr($S,1);
190 my $depth=1;
191 while (1) {
192 ##print "A$depth($S)\n";
193 if ($depth!=0) { # skip to-document-Sample part
194 $S="" if substr($S,0,1)eq'%';
195 my $T=$S; $S="";
196 my $C;
197 while ($T=~m@\G(\\.?|[%{}]|[^\\%{}]+)@sg) {
198 if ($depth==0) { $S.=$1 }
199 elsif ('%'eq($C=substr$1,0,1)) { last }
200 elsif ($C eq '{') { $depth++ }
201 elsif ($C eq '}') { $depth-- }
202 }
203 if ($depth==0 and 0!=length$S) {
204 die if !print KI "$S\n";
205 }
206 } else {
207 die if !print KI "$S\n";
208 }
209 last if !$going_p;
210 die "$0: unexpected EOF\n" if !defined($S=<F>);
211 chomp $S;
212 $going_p=$S!~s@\\end\{peldak\}.*@@s;
213 last if !$going_p and 0==length($S);
214 }
215 die if !print KI $default_E;
216 } else { die }
217 die unless close KI;
218 die unless close F;
219 $cleanup_files{"$destdir/$destfn"}--;
220 }
221
222 my $gendepth=0;
223 #** @param #_[1] bool: must exist?
224 sub do_aux($$);
225 sub do_aux($$) {
226 no strict 'refs';
227 my $auxfn=$_[0];
228 my $auxabs=get_absname($auxfn,"latex","tex");
229 if (!defined $auxabs) {
230 die "$0: .aux file not found: $auxfn\n" if $_[1];
231 return
232 }
233 my $F=\*{'FILE'.$gendepth++};
234 die unless open $F, "< $auxfn";
235 print "Processing $auxfn\n";
236 while (<$F>) {
237 if (/^\\\@gobble\{code:.*\}/) {
238 die "$0: syntax error ($.)\n" if !/^\\\@gobble\{code:([^:\}]+):(\d+):([^:\}]+)\}$/;
239 do_code($1,$2+0,$3);
240 } elsif (/^\\\@gobble\{(cd[dlbe]):(.*)\}/) {
241 my $ftype=uc($1);
242 die "$0: cannot open $ftype file: $2: $!\n" unless open CDL, "< $2";
243 print "Using $ftype file: $2\n";
244 my $S=join('',<CDL>);
245 $S=~s@\A[\n\r]+@@;
246 $S=~s@\s+\Z(?!\n)@\n@;
247 ## die $S;
248 if ($ftype eq 'CDD') { $default_D="$S\n" }
249 elsif ($ftype eq 'CDL') { $default_L=$S }
250 elsif ($ftype eq 'CDB') { $default_B="$S\n" }
251 elsif ($ftype eq 'CDE') { $default_E=$S }
252 else { die }
253 die unless close CDL;
254 } elsif (/^\\\@input\{/) {
255 die "$0: syntax error ($.)\n" if !/^\\\@input\{([^}]+)\}$/;
256 do_aux($1,0);
257 }
258 }
259 die unless close $F;
260 $gendepth--;
261 }
262
263 die "Usage: $0 <jobname>[.tex|.aux]\n" if @ARGV!=1;
264 my $fn=$ARGV[0];
265 $fn=~s@[.](tex|aux)\Z(?!\n)@@; # Imp: foo.bar.log?
266
267 select STDERR; $|=1; select STDOUT; $|=1;
268 if (-e $destdir) {
269 print STDERR "warning: destination dir exists: $destdir\n"
270 }
271 mkdir $destdir;
272 die unless -d $destdir;
273 do_aux "$ARGV[0].aux", 1;
274
275 __END__