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 '') {
131 $expr.= $op unless $op =~ m/[?]/;
134 process_file($value);
135 } elsif ($top eq '?') {
136 push @oplist, '?',':;'.!!$value;
137 } elsif ($top ne '-') {
144 } elsif ($op =~ m/[\(\{]/) {
145 if (@oplist>1) { $substr.= $esc; }
148 err("bad escape sequence $op\@\@ in perl part");
154 } else { # scanning text
155 if ($l =~ m/\@\@(.|\n)/) {
156 ($to,$op,$esc,$l) = ($`,$1,$&,$');
158 if ($op =~ m/[-< \$?\n]/) {
160 add_perl($op) if $op =~ m/[\$]/;
165 } elsif ($op =~ m/[\)\}\;\:]/) {
166 err("unmatched closing \@\@$op") unless @oplist;
168 if ($want =~ y/({/)}/) {
170 err("found \@\@$op but wanted \@\@$want")
172 } elsif ($want =~ m/^([:;]+)(1?)$/) {
173 ($want,$wasyes)=($1,$2);
174 err("found \@\@$op where not expected (wanted ".
175 join(' or ', map { "\@\@$_" } split //, $want).")")
176 unless $op =~ m/^[$want]$/;
177 '?' eq pop @oplist or die;
179 local ($l) = ($substr);
180 local ($substr,$expr,$to,$op);
186 push @oplist, '?',';'.!$wasyes;
189 die "internal /$want/ /$op/";
192 # Just finished a substr.
194 $substrs[$substri]= $substr;
195 print DEBUG "S $fn:$lno:S$substri\`$substr'\n";
196 $expr.= ' GenericPrepTemplate::output(' if $op eq '}';
197 $expr.= " GenericPrepTemplate::process_i($substri) ";
198 $expr.= " );\n" if $op eq '}';
203 } elsif ($op eq '#') {
204 $l =~ s/^[^\n]*//; $l =~ s/^\n//;
206 err("bad escape sequence \@\@$op in text part");
216 sub close_nesteds () {
219 $op= $oplist[$#oplist];
221 err("unterminated \@\@?") if $op eq '?';
224 if ($op =~ y/({/)}/) {
226 } elsif ($op =~ m/^[;:]+/) {
229 die "intern /@oplist/";
236 sub process_fh ($$) {
237 local ($fh,$fn) = @_;
238 local ($l,$lno, @oplist,$substr,$expr,$to,$op);
240 while (defined ($l= $fh->getline)) {
241 next if $l =~ m,^\#\! ?/\S+/gpt\b, && $.==1;
246 die "gpt: $fn: read error: $!" if $fh->error;
251 if (defined $outbuf) {
252 print DEBUG "O \`$outbuf'+\`$_[0]'\n";
255 print DEBUG "O w\`$_[0]'\n";
256 print STDOUT $_[0] or err("write stdout: $!");
262 local ($l,$fn,$lno) = ($substrs[$i], "$fn<sub#$i>", '');
263 local ($outbuf, @oplist,$substr,$expr,$to,$op);