3 package GenericPrepTemplate;
7 # use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10 # @EXPORT= qw(&process_input);
20 open DEBUG, ">/dev/null" or die "gpt: /dev/null: $!\n";
22 while (@main::ARGV =~ m/^-/) {
23 $_= shift @main::ARGV;
27 open DEBUG, ">&2" or die "gpt: debug to stderr: $!\n";
29 die "gpt: unknown option \`$_'\n";
34 $fn= '<command line>';
38 $fh->fdopen(fileno(STDIN),'r') or err("fdopen stdin: $!");
39 process_fh($fh,'<standard input>');
40 $fh->close or err("close stdin: $!\n");
42 for ($i=0; $i<@main::ARGV; $i++) {
43 $lno= '<arg#'.($i+1).'>';
44 $value= $main::ARGV[$i];
45 $fh= new IO::File $value,'r' or err("cannot open file \`$value': $!");
46 process_fh($fh,$value);
47 $fh->close or err("gpt: cannot close file \`$value': $!");
50 close STDOUT or die "gpt: close stdout: $!\n";
53 sub err ($) { die "gpt: $fn:$lno: $_[0]\n"; }
57 if (@oplist>1) { $substr.= $str; }
58 else { $expr.= $str; }
63 if (@oplist) { $substr.=$str; }
64 else { output($str); }
70 print DEBUG "E $fn:$lno:e\`$expr'\n";
73 eval $GenericPrepTemplate::expr;
76 chomp $@; err("error: $@");
78 print DEBUG "E $fn:$lno:v\`$value'\n";
82 sub process_input () {
83 my ($esc,$top,$want,$fh,$value,$wasyes);
85 print DEBUG "L $fn:$lno:",join('',@oplist),
86 ":`$l' e\`$expr' s\`$substr'\n";
87 if (@oplist & 1) { # scanning perl
88 if ($l =~ m/(.)\@\@/m || $l =~ m/^()\@\@/m) {
89 ($to,$op,$esc,$l) = ($`,$1,$&,$');
91 if ($op =~ m/[ 0-9a-zA-Z_\;\)\}?]/ || $op eq '') {
95 $expr.= $op unless $op =~ m/[?]/;
99 } elsif ($top eq '?') {
100 push @oplist, '?',':;'.!!$value;
101 } elsif ($top ne '-') {
108 } elsif ($op =~ m/[\(\{]/) {
109 if (@oplist>1) { $substr.= $esc; }
112 err("bad escape sequence $op\@\@ in perl part");
118 } else { # scanning text
119 if ($l =~ m/\@\@(.|\n)/) {
120 ($to,$op,$esc,$l) = ($`,$1,$&,$');
122 if ($op =~ m/[-< \$?]/) {
123 $substr.= $esc if @oplist;
125 } elsif ($op =~ m/[\)\}\;\:]/) {
126 err("unmatched closing \@\@$op") unless @oplist;
128 if ($want =~ y/({/)}/) {
130 err("found \@\@$op but wanted \@\@$want")
132 } elsif ($want =~ m/^([:;]+)(1?)$/) {
133 ($want,$wasyes)=($1,$2);
134 err("found \@\@$op where not expected (wanted ".
135 join(' or ', map { "\@\@$_" } split //, $want).")")
136 unless $op =~ m/^[$want]$/;
137 '?' eq pop @oplist or die;
139 local ($l) = ($substr);
140 local ($substr,$expr,$to,$op);
146 push @oplist, '?',';'.!$wasyes;
149 die "internal /$want/ /$op/";
152 # Just finished a substr.
154 $substrs[$substri]= $substr;
155 print DEBUG "S $fn:$lno:S$substri\`$substr'\n";
156 $expr.= ' GenericPrepTemplate::output(' if $op eq '}';
157 $expr.= " GenericPrepTemplate::process_i($substri) ";
158 $expr.= " );\n" if $op eq '}';
164 err("bad escape sequence \@\@$op in text part");
174 sub process_fh ($$) {
175 local ($fh,$fn) = @_;
176 local ($l,$lno, @oplist,$substr,$expr,$to,$op);
178 while (defined ($l= $fh->getline)) {
179 next if $l =~ m,^\#\! ?/\S+/gpt\b, && $.==1;
183 die "gpt: $fn: read error: $!" if $fh->error;
187 if (defined $outbuf) {
188 print DEBUG "O \`$outbuf'+\`$_[0]'\n";
191 print DEBUG "O w\`$_[0]'\n";
192 print STDOUT $_[0] or err("write stdout: $!");
198 local ($l,$fn,$lno) = ($substrs[$i], "$fn<sub#$i>", '');
199 local ($outbuf, @oplist,$substr,$expr,$to,$op);