6 # $Header: s2p.cmd,v 4.0 91/03/20 01:37:09 lwall Locked $
9 # Revision 4.0 91/03/20 01:37:09 lwall
12 # Revision 3.0.1.6 90/10/20 02:21:43 lwall
13 # patch37: changed some ". config.sh" to ". ./config.sh"
15 # Revision 3.0.1.5 90/10/16 11:32:40 lwall
16 # patch29: s2p modernized
18 # Revision 3.0.1.4 90/08/09 05:50:43 lwall
19 # patch19: s2p didn't translate \n right
21 # Revision 3.0.1.3 90/03/01 10:31:21 lwall
22 # patch9: s2p didn't handle \< and \>
24 # Revision 3.0.1.2 89/11/17 15:51:27 lwall
25 # patch5: in s2p, line labels without a subsequent statement were done wrong
26 # patch5: s2p left residue in /tmp
28 # Revision 3.0.1.1 89/11/11 05:08:25 lwall
29 # patch2: in s2p, + within patterns needed backslashing
30 # patch2: s2p was printing out some debugging info to the output file
32 # Revision 3.0 89/10/18 15:35:02 lwall
35 # Revision 2.0.1.1 88/07/11 23:26:23 root
36 # patch2: s2p didn't put a proper prologue on output script
38 # Revision 2.0 88/06/05 00:15:55 root
39 # Baseline version 2.0.
47 while ($ARGV[0] =~ /^-/) {
63 die "I don't recognize this switch: $_\n";
67 open(BODY,">sperl$$") ||
68 &Die("Can't open temp file: $!\n");
71 if (!$assumen && !$assumep) {
73 while ($ARGV[0] =~ /^-/) {
80 die "I don't recognize this switch: $_\\n";
92 $printit++ unless $nflag;
100 # Wipe out surrounding whitespace.
104 # Perhaps it's a label/comment.
108 $label = &make_label($_);
113 if ($lastlinewaslabel++) {
115 print BODY &tab, ";\n";
124 $lastlinewaslabel = '';
127 # Look for one or two address clauses
138 $addr1 = &fetchpat('/');
146 $addr2 = &fetchpat('/');
148 &Die("Invalid second address at line $.\n");
150 $addr1 .= " .. $addr2";
153 # Now we check for metacommands {, }, and ! and worry
157 # a { to keep vi happy
164 $else = "$r else $l\n";
169 if (s/^{//) { # a } to keep vi happy
176 if ($addr2 || $addr1) {
177 $space = ' ' x $shiftwidth;
181 $_ = &transmogrify();
184 # See if we can optimize to modifier form.
187 if ($_ !~ /[\n{}]/ && $rmaybe && !$change &&
188 $_ !~ / if / && $_ !~ / unless /) {
190 $_ = substr($_,$shiftwidth,1000);
192 $_ = "$if ($addr1) $l\n$change$_$rmaybe";
198 @lines = split(/\n/,$_);
200 unless (s/^ *<<--//) {
213 if ($lastlinewaslabel++) {
215 print BODY &tab, ";\n";
220 if ($appendseen || $tseen || !$assumen) {
221 $printit++ if $dseen || (!$assumen && !$assumep);
233 { $printit++ unless $nflag; }
245 if ($atext) { print $atext; $atext = ''; }
254 open(HEAD,">sperl2$$.c")
255 || &Die("Can't open temp file 2: $!\n");
256 print HEAD "#define PRINTIT\n" if ($printit);
257 print HEAD "#define APPENDSEEN\n" if ($appendseen);
258 print HEAD "#define TSEEN\n" if ($tseen);
259 print HEAD "#define DSEEN\n" if ($dseen);
260 print HEAD "#define ASSUMEN\n" if ($assumen);
261 print HEAD "#define ASSUMEP\n" if ($assumep);
262 if ($opens) {print HEAD "$opens\n";}
264 || &Die("Can't reopen temp file: $!\n");
272 eval 'exec $bin/perl -S \$0 \$*'
273 if \$running_under_some_shell;
276 open(BODY,"cc -E sperl2$$.c |") ||
277 &Die("Can't reopen temp file: $!\n");
290 unlink "sperl$$", "sperl2$$", "sperl2$$.c";
297 "\t" x ($indent / 8) . ' ' x ($indent % 8);
299 sub make_filehandle {
304 substr($_,0,1) =~ y/a-z/A-Z/ if /^[a-z]/;
307 open($_,'>$fname') || die "Can't create $fname";
315 $label =~ s/[^a-zA-Z0-9]/_/g;
316 if ($label =~ /^[0-9_]/) { $label = 'L' . $label; }
317 $label = substr($label,0,8);
319 # Could be a reserved word, so capitalize it.
320 substr($label,0,1) =~ y/a-z/A-Z/
321 if $label =~ /^[a-z]/;
349 { $printit++ unless $nflag; }
357 <<--#ifdef APPENDSEEN
358 if ($atext) {print $atext; $atext = '';}
370 $command = $space . '$atext .=' . "\n<<--'";
375 unless (s|\\$||) { $lastline = 1;}
377 s/^([ \t]*\n)/<><>$1/;
382 $_ = $command . "';";
387 if (/^c/) { $change = 1; }
388 $addr1 = '$iter = (' . $addr1 . ')';
389 $command = $space . 'if ($iter == 1) { print'
395 unless (s/\\$//) { $lastline = 1;}
397 s/^([ \t]*\n)/<><>$1/;
402 $_ = $command . "';}";
408 $space\$printit = '';
417 $delim = substr($_,1,1);
421 for ($i = 2; $i < $len; $i++) {
422 $c = substr($_,$i,1);
425 substr($_, $i, 0) = '\\';
444 $_ = substr($_,0,--$len);
446 elsif (substr($_,$i,1) =~ /^[n]$/) {
450 substr($_,$i,1) =~ /^[(){}\w]$/) {
453 substr($_, $i, 1) = '';
456 substr($_,$i,1) =~ /^[<>]$/) {
457 substr($_,$i,1) = 'b';
460 elsif ($c eq '[' && !$repl) {
461 $i++ if substr($_,$i,1) eq '^';
462 $i++ if substr($_,$i,1) eq ']';
468 elsif (!$repl && index("()+",$c) >= 0) {
469 substr($_, $i, 0) = '\\';
474 &Die("Malformed substitution at line $.\n")
476 $pat = substr($_, 0, $repl + 1);
477 $repl = substr($_, $repl+1, $end-$repl-1);
478 $end = substr($_, $end + 1, 1000);
482 $repl =~ s/[\\]([0-9])/$dol$1/g;
483 $subst = "$pat$repl$delim";
486 if ($end =~ s/^g//) {
490 if ($end =~ s/^p//) {
491 $cmd .= ' && (print)';
494 if ($end =~ s/^w[ \t]*//) {
495 $fh = &make_filehandle($end);
496 $cmd .= " && (print $fh \$_)";
500 &Die("Unrecognized substitution command".
501 "($end) at line $.\n");
505 $subst && \$tflag++$cmd;
520 $fh = &make_filehandle($_);
521 $_ = "print $fh \$_;";
529 $_ = "\$atext .= `cat $file 2>/dev/null`;";
534 $_ = 'print $1 if /(^.*\n)/;';
563 $_ = '$hold .= $_ ? $_ : "\n";';
573 $_ = '$_ .= $hold ? $hold : "\n";';
578 $_ = '($_, $hold) = ($hold, $_);';
589 $lab = &make_label($_);
590 if ($lab eq $toplabel) {
599 $_ = 'next LINE if $tflag;';
606 $lab = &make_label($_);
607 $_ = q/if ($tflag) {$tflag = ''; /;
608 if ($lab eq $toplabel) {
618 $_ = 'print "$.\n";';
633 s/(\n)(.)/$1$space$2/g;
642 local($addr) = $outer;
644 local($prefix,$delim,$ch);
646 # Process pattern one potential delimiter at a time.
648 DELIM: while (s#^([^\]+(|)[\\/]*)([]+(|)[\\/])##) {
651 if ($delim eq '\\') {
654 $delim = '' if $ch =~ /^[(){}A-Za-mo-z]$/;
655 $ch = 'b' if $ch =~ /^[<>]$/;
658 elsif ($delim eq '[') {
660 s/^\^// && ($delim .= '^');
661 s/^]// && ($delim .= ']');
663 elsif ($delim eq ']') {
666 elsif ($inbracket || $delim ne $outer) {
667 $delim = '\\' . $delim;
671 if ($delim eq $outer && !$inbracket) {