4 # =====deeper=nesting===>>
7 # \n@@ \_@@ ;@@ )@@ }@@ \w@@
8 # <------------------------------------
10 # ------------------------------------>
12 # @@? @@\_ ignore include
16 # | processing processing
17 # `---> as TEXT as PERL
19 # @@; vA <------------------------------------
23 # || included text fragment as
24 # else || string value code to write out
26 # || <------------------------------------
28 # ------------------------------------>
29 # end of included text fragment
32 # <<====deeper=nesting====
35 package GenericPrepTemplate;
39 # use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
42 # @EXPORT= qw(&process_input);
52 open DEBUG, ">/dev/null" or die "gpt: /dev/null: $!\n";
54 while ($main::ARGV[0] =~ m/^-/) {
55 $_= shift @main::ARGV;
59 open DEBUG, ">&2" or die "gpt: debug to stderr: $!\n";
61 die "gpt: unknown option \`$_'\n";
66 $fn= '<command line>';
70 $fh->fdopen(fileno(STDIN),'r') or err("fdopen stdin: $!");
71 process_fh($fh,'<standard input>');
72 $fh->close or err("close stdin: $!\n");
74 for ($i=0; $i<@main::ARGV; $i++) {
75 $lno= '<arg#'.($i+1).'>';
76 process_file($main::ARGV[$i]);
79 close STDOUT or die "gpt: close stdout: $!\n";
82 sub process_file ($) {
84 $fh= new IO::File $value,'r' or err("cannot open file \`$value': $!");
85 process_fh($fh,$value);
86 $fh->close or err("gpt: cannot close file \`$value': $!");
89 sub err ($) { die "gpt: $fn:$lno: $_[0]\n"; }
93 if (@oplist>1) { $substr.= $str; }
94 else { $expr.= $str; }
99 if (@oplist) { $substr.=$str; }
100 else { output($str); }
106 print DEBUG "E $fn:$lno:e\`$expr'\n";
109 eval $GenericPrepTemplate::expr;
112 chomp $@; err("error: $@");
114 print DEBUG "E $fn:$lno:v\`$value'\n";
118 sub process_input () {
119 my ($esc,$top,$want,$fh,$value,$wasyes);
121 print DEBUG "L $fn:${lno}_",join('',@oplist),
122 "_`$l' e\`$expr' s\`$substr'\n";
123 if (@oplist & 1) { # scanning perl
124 if ($l =~ m/^()\@\@/ || $l =~ m/([^\000])\@\@/) {
125 ($to,$op,$esc,$l) = ($`,$1,$&,$');
127 if ($op =~ m/[ \t\n0-9a-zA-Z_\;\)\}?]/ || $op eq '') {
130 $op eq '?' or err("expected ?\@\@, got \@\@");
132 $op ne '?' or err("found ?\@\@ not after \@\@?");
136 $expr.= $op unless $op eq '?';
139 process_file($value);
140 } elsif ($top eq '?') {
141 push @oplist, '?',':;'.!!$value;
142 } elsif ($top ne '-') {
149 } elsif ($op =~ m/[\(\{]/) {
150 if (@oplist>1) { $substr.= $esc; }
153 err("bad escape sequence $op\@\@ in perl part");
159 } else { # scanning text
160 if ($l =~ m/\@\@(.|\n)/) {
161 ($to,$op,$esc,$l) = ($`,$1,$&,$');
163 if ($op =~ m/[-< \$?\n]/) {
165 add_perl($op) if $op =~ m/[\$]/;
170 } elsif ($op =~ m/[\)\}\;\:]/) {
171 err("unmatched closing \@\@$op") unless @oplist;
173 if ($want =~ y/({/)}/) {
175 err("found \@\@$op but wanted \@\@$want")
177 } elsif ($want =~ m/^([:;]+)(1?)$/) {
178 ($want,$wasyes)=($1,$2);
179 err("found \@\@$op where not expected (wanted ".
180 join(' or ', map { "\@\@$_" } split //, $want).")")
181 unless $op =~ m/^[$want]$/;
182 '?' eq pop @oplist or die;
184 local ($l) = ($substr);
185 local ($substr,$expr,$to,$op);
191 push @oplist, '?',';'.!$wasyes;
194 die "internal /$want/ /$op/";
197 # Just finished a substr.
199 $substrs[$substri]= $substr;
200 print DEBUG "S $fn:$lno:S$substri\`$substr'\n";
201 $expr.= ' GenericPrepTemplate::output(' if $op eq '}';
202 $expr.= " GenericPrepTemplate::process_i($substri) ";
203 $expr.= " );\n" if $op eq '}';
208 } elsif ($op eq '#') {
209 $l =~ s/^[^\n]*//; $l =~ s/^\n//;
211 err("bad escape sequence \@\@$op in text part");
221 sub close_nesteds () {
224 $op= $oplist[$#oplist];
226 err("unterminated \@\@?") if $op eq '?';
229 if ($op =~ y/({/)}/) {
231 } elsif ($op =~ m/^[;:]+/) {
234 die "intern /@oplist/";
241 sub process_fh ($$) {
242 local ($fh,$fn) = @_;
243 local ($l,$lno, @oplist,$substr,$expr,$to,$op);
245 while (defined ($l= $fh->getline)) {
246 next if $l =~ m,^\#\! ?/\S+/gpt\b, && $.==1;
251 die "gpt: $fn: read error: $!" if $fh->error;
256 if (defined $outbuf) {
257 print DEBUG "O \`$outbuf'+\`$_[0]'\n";
260 print DEBUG "O w\`$_[0]'\n";
261 print STDOUT $_[0] or err("write stdout: $!");
267 local ($l,$fn,$lno) = ($substrs[$i], "$fn<sub#$i>", '');
268 local ($outbuf, @oplist,$substr,$expr,$to,$op);