1 : This forces SH files to create target in same directory as SH file.
2 : This is so that make depend always knows where to find SH derivatives.
4 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
8 if test ! -f config.sh; then
10 ln -s ../config.sh . || \
11 ln ../../config.sh . || \
12 ln ../../../config.sh . || \
13 (echo "Can't find config.sh."; exit 1)
18 echo "Extracting s2p (with variable substitutions)"
19 : This section of the file will have variable substitutions done on it.
20 : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
21 : Protect any dollar signs and backticks that you do not want interpreted
22 : by putting a backslash in front. You may delete these comments.
24 $spitshell >s2p <<!GROK!THIS!
27 eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
28 if \$running_under_some_shell;
33 : In the following dollars and backticks do not need the extra backslash.
34 $spitshell >>s2p <<'!NO!SUBS!'
36 # $RCSfile: s2p.SH,v $$Revision: 4.1 $$Date: 92/08/07 18:29:23 $
39 # Revision 4.1 92/08/07 18:29:23 lwall
41 # Revision 4.0.1.2 92/06/08 17:26:31 lwall
42 # patch20: s2p didn't output portable startup code
43 # patch20: added ... as variant on ..
44 # patch20: s2p didn't translate s/pat/\&/ or s/pat/\$/ or s/pat/\\1/ right
46 # Revision 4.0.1.1 91/06/07 12:19:18 lwall
47 # patch4: s2p now handles embedded newlines better and optimizes common idioms
49 # Revision 4.0 91/03/20 01:57:59 lwall
58 while ($ARGV[0] =~ /^-/) {
74 die "I don't recognize this switch: $_\n";
78 open(BODY,">/tmp/sperl$$") ||
79 &Die("Can't open temp file: $!\n");
82 if (!$assumen && !$assumep) {
83 print BODY &q(<<'EOT');
84 : while ($ARGV[0] =~ /^-/) {
91 : die "I don't recognize this switch: $_\\n";
97 print BODY &q(<<'EOT');
102 : $printit++ unless $nflag;
106 : $\ = "\n"; # automatically add newline on print
110 : while (chop($_ = <>)) {
121 # Wipe out surrounding whitespace.
125 # Perhaps it's a label/comment.
129 $label = &make_label($_);
132 if (/^(top|(re)?start|redo|begin(ning)|again|input)$/i) {
134 redo LINE; # Never referenced, so delete it if not a comment.
138 if ($lastlinewaslabel++) {
140 print BODY &tab, ";\n";
149 $lastlinewaslabel = '';
152 # Look for one or two address clauses
158 $addr1 = "\$. == $addr1" unless /^,/;
164 $addr1 = &fetchpat('/');
172 $addr2 = &fetchpat('/');
174 &Die("Invalid second address at line $.\n");
176 if ($addr2 =~ /^\d+$/) {
177 $addr1 .= "..$addr2";
180 $addr1 .= "...$addr2";
184 # Now we check for metacommands {, }, and ! and worry
188 # a { to keep vi happy
195 $else = "$r else $l\n";
200 if (s/^{//) { # a } to keep vi happy
207 if ($addr2 || $addr1) {
208 $space = ' ' x $shiftwidth;
212 $_ = &transmogrify();
215 # See if we can optimize to modifier form.
218 if ($_ !~ /[\n{}]/ && $rmaybe && !$change &&
219 $_ !~ / if / && $_ !~ / unless /) {
221 $_ = substr($_,$shiftwidth,1000);
223 $_ = "$if ($addr1) $l\n$change$_$rmaybe";
229 @lines = split(/\n/,$_);
231 unless (s/^ *<<--//) {
244 if ($lastlinewaslabel++) {
246 print BODY &tab, ";\n";
250 if ($appendseen || $tseen || !$assumen) {
251 $printit++ if $dseen || (!$assumen && !$assumep);
252 print BODY &q(<<'EOT');
260 : print if $printit++;
265 : { $printit++ unless $nflag; }
277 : if ($atext) { chop $atext; print $atext; $atext = ''; }
281 print BODY &q(<<'EOT');
289 open(HEAD,">/tmp/sperl2$$.c")
290 || &Die("Can't open temp file 2: $!\n");
291 print HEAD "#define PRINTIT\n" if $printit;
292 print HEAD "#define APPENDSEEN\n" if $appendseen;
293 print HEAD "#define TSEEN\n" if $tseen;
294 print HEAD "#define DSEEN\n" if $dseen;
295 print HEAD "#define ASSUMEN\n" if $assumen;
296 print HEAD "#define ASSUMEP\n" if $assumep;
297 print HEAD "#define TOPLABEL\n" if $toplabel;
298 print HEAD "#define SAWNEXT\n" if $sawnext;
299 if ($opens) {print HEAD "$opens\n";}
300 open(BODY,"/tmp/sperl$$")
301 || &Die("Can't reopen temp file: $!\n");
309 : eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
310 : if \$running_under_some_shell;
313 open(BODY,"cc -E /tmp/sperl2$$.c |") ||
314 &Die("Can't reopen temp file: $!\n");
328 unlink "sperl$$", "sperl2$$", "sperl2$$.c";
335 "\t" x ($indent / 8) . ' ' x ($indent % 8);
337 sub make_filehandle {
340 if (!$seen{$fname}) {
341 $_ = "FH_" . $_ if /^\d/;
346 for ($tmp = "a"; $fhseen{"$_$tmp"}; $a++) {}
350 $opens .= &q(<<"EOT");
351 : open($_, '>$fname') || die "Can't create $fname: \$!";
360 $label =~ s/[^a-zA-Z0-9]/_/g;
361 if ($label =~ /^[0-9_]/) { $label = 'L' . $label; }
362 $label = substr($label,0,8);
364 # Could be a reserved word, so capitalize it.
365 substr($label,0,1) =~ y/a-z/A-Z/
366 if $label =~ /^[a-z]/;
375 chop($_ = &q(<<'EOT'));
386 chop($_ = &q(<<'EOT'));
390 : print if $printit++;
395 : { $printit++ unless $nflag; }
403 : <<--#ifdef APPENDSEEN
404 : if ($atext) {chop $atext; print $atext; $atext = '';}
417 $command = $space . "\$atext .= <<'End_Of_Text';\n<<--";
422 unless (s|\\$||) { $lastline = 1;}
423 s/^([ \t]*\n)/<><>$1/;
428 $_ = $command . "End_Of_Text";
433 if (/^c/) { $change = 1; }
434 $addr1 = 1 if $addr1 eq '';
435 $addr1 = '$iter = (' . $addr1 . ')';
437 " if (\$iter == 1) { print <<'End_Of_Text'; }\n<<--";
442 unless (s/\\$//) { $lastline = 1;}
444 s/^([ \t]*\n)/<><>$1/;
449 $_ = $command . "End_Of_Text";
453 chop($_ = &q(<<"EOT"));
455 : $space\$printit = 0;
465 $delim = substr($_,1,1);
469 for ($i = 2; $i < $len; $i++) {
470 $c = substr($_,$i,1);
473 substr($_, $i, 0) = '\\';
492 $_ = substr($_,0,--$len);
494 elsif (substr($_,$i,1) =~ /^[n]$/) {
498 substr($_,$i,1) =~ /^[(){}\w]$/) {
501 substr($_, $i, 1) = '';
504 substr($_,$i,1) =~ /^[<>]$/) {
505 substr($_,$i,1) = 'b';
507 elsif ($repl && substr($_,$i,1) =~ /^\d$/) {
508 substr($_,$i-1,1) = '$';
511 elsif ($c eq '&' && $repl) {
512 substr($_, $i, 0) = '$';
516 elsif ($c eq '$' && $repl) {
517 substr($_, $i, 0) = '\\';
521 elsif ($c eq '[' && !$repl) {
522 $i++ if substr($_,$i,1) eq '^';
523 $i++ if substr($_,$i,1) eq ']';
530 substr($_, $i, 1) = '\\t';
534 elsif (!$repl && index("()+",$c) >= 0) {
535 substr($_, $i, 0) = '\\';
540 &Die("Malformed substitution at line $.\n")
542 $pat = substr($_, 0, $repl + 1);
543 $repl = substr($_, $repl+1, $end-$repl-1);
544 $end = substr($_, $end + 1, 1000);
547 $subst = "$pat$repl$delim";
550 if ($end =~ s/^g//) {
554 if ($end =~ s/^p//) {
555 $cmd .= ' && (print)';
558 if ($end =~ s/^w[ \t]*//) {
559 $fh = &make_filehandle($end);
560 $cmd .= " && (print $fh \$_)";
564 &Die("Unrecognized substitution command".
565 "($end) at line $.\n");
567 chop ($_ = &q(<<"EOT"));
569 : $subst && \$tflag++$cmd;
584 $fh = &make_filehandle($_);
585 $_ = "print $fh \$_;";
593 $_ = "\$atext .= `cat $file 2>/dev/null`;";
598 $_ = 'print $1 if /^(.*)/;';
603 chop($_ = &q(<<'EOT'));
613 chop($_ = &q(<<'EOT'));
617 : chop if $len1 < length;
631 $_ = '$hold .= "\n"; $hold .= $_;';
641 $_ = '$_ .= "\n"; $_ .= $hold;';
646 $_ = '($_, $hold) = ($hold, $_);';
658 $lab = &make_label($_);
659 if ($lab eq $toplabel) {
668 $_ = 'next LINE if $tflag;';
676 $lab = &make_label($_);
677 $_ = q/if ($tflag) {$tflag = 0; /;
678 if ($lab eq $toplabel) {
688 s/abcdefghijklmnopqrstuvwxyz/a-z/g;
689 s/ABCDEFGHIJKLMNOPQRSTUVWXYZ/A-Z/g;
703 chop($_ = &q(<<'EOT'));
714 s/(\n)(.)/$1$space$2/g;
723 local($addr) = $outer;
725 local($prefix,$delim,$ch);
727 # Process pattern one potential delimiter at a time.
729 DELIM: while (s#^([^\]+(|)[\\/]*)([]+(|)[\\/])##) {
732 if ($delim eq '\\') {
735 $delim = '' if $ch =~ /^[(){}A-Za-mo-z]$/;
736 $ch = 'b' if $ch =~ /^[<>]$/;
739 elsif ($delim eq '[') {
741 s/^\^// && ($delim .= '^');
742 s/^]// && ($delim .= ']');
744 elsif ($delim eq ']') {
747 elsif ($inbracket || $delim ne $outer) {
748 $delim = '\\' . $delim;
752 if ($delim eq $outer && !$inbracket) {
764 $string =~ s/^:\t?//g;
769 $_[0] =~ s/_a-za-z0-9/\\w/ig;
770 $_[0] =~ s/a-z_a-z0-9/\\w/ig;
771 $_[0] =~ s/a-za-z_0-9/\\w/ig;
772 $_[0] =~ s/a-za-z0-9_/\\w/ig;
773 $_[0] =~ s/_0-9a-za-z/\\w/ig;
774 $_[0] =~ s/0-9_a-za-z/\\w/ig;
775 $_[0] =~ s/0-9a-z_a-z/\\w/ig;
776 $_[0] =~ s/0-9a-za-z_/\\w/ig;
777 $_[0] =~ s/\[\\w\]/\\w/g;
778 $_[0] =~ s/\[^\\w\]/\\W/g;
779 $_[0] =~ s/\[0-9\]/\\d/g;
780 $_[0] =~ s/\[^0-9\]/\\D/g;
781 $_[0] =~ s/\\d\\d\*/\\d+/g;
782 $_[0] =~ s/\\D\\D\*/\\D+/g;
783 $_[0] =~ s/\\w\\w\*/\\w+/g;
784 $_[0] =~ s/\\t\\t\*/\\t+/g;
785 $_[0] =~ s/(\[.[^]]*\])\1\*/$1+/g;
786 $_[0] =~ s/([\w\s!@#%^&-=,:;'"])\1\*/$1+/g;