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.
23 $spitshell >s2p <<!GROK!THIS!
29 : In the following dollars and backticks do not need the extra backslash.
30 $spitshell >>s2p <<'!NO!SUBS!'
32 # $RCSfile: s2p.SH,v $$Revision: 4.0.1.1 $$Date: 91/06/07 12:19:18 $
35 # Revision 4.0.1.1 91/06/07 12:19:18 lwall
36 # patch4: s2p now handles embedded newlines better and optimizes common idioms
38 # Revision 4.0 91/03/20 01:57:59 lwall
47 while ($ARGV[0] =~ /^-/) {
63 die "I don't recognize this switch: $_\n";
67 open(BODY,">/tmp/sperl$$") ||
68 &Die("Can't open temp file: $!\n");
71 if (!$assumen && !$assumep) {
72 print BODY &q(<<'EOT');
73 : while ($ARGV[0] =~ /^-/) {
80 : die "I don't recognize this switch: $_\\n";
86 print BODY &q(<<'EOT');
91 : $printit++ unless $nflag;
95 : $\ = "\n"; # automatically add newline on print
99 : while (chop($_ = <>)) {
110 # Wipe out surrounding whitespace.
114 # Perhaps it's a label/comment.
118 $label = &make_label($_);
121 if (/^(top|(re)?start|redo|begin(ning)|again|input)$/i) {
123 redo LINE; # Never referenced, so delete it if not a comment.
127 if ($lastlinewaslabel++) {
129 print BODY &tab, ";\n";
138 $lastlinewaslabel = '';
141 # Look for one or two address clauses
147 $addr1 = "\$. == $addr1" unless /^,/;
153 $addr1 = &fetchpat('/');
161 $addr2 = &fetchpat('/');
163 &Die("Invalid second address at line $.\n");
165 $addr1 .= " .. $addr2";
168 # Now we check for metacommands {, }, and ! and worry
172 # a { to keep vi happy
179 $else = "$r else $l\n";
184 if (s/^{//) { # a } to keep vi happy
191 if ($addr2 || $addr1) {
192 $space = ' ' x $shiftwidth;
196 $_ = &transmogrify();
199 # See if we can optimize to modifier form.
202 if ($_ !~ /[\n{}]/ && $rmaybe && !$change &&
203 $_ !~ / if / && $_ !~ / unless /) {
205 $_ = substr($_,$shiftwidth,1000);
207 $_ = "$if ($addr1) $l\n$change$_$rmaybe";
213 @lines = split(/\n/,$_);
215 unless (s/^ *<<--//) {
228 if ($lastlinewaslabel++) {
230 print BODY &tab, ";\n";
234 if ($appendseen || $tseen || !$assumen) {
235 $printit++ if $dseen || (!$assumen && !$assumep);
236 print BODY &q(<<'EOT');
244 : print if $printit++;
249 : { $printit++ unless $nflag; }
261 : if ($atext) { chop $atext; print $atext; $atext = ''; }
265 print BODY &q(<<'EOT');
273 open(HEAD,">/tmp/sperl2$$.c")
274 || &Die("Can't open temp file 2: $!\n");
275 print HEAD "#define PRINTIT\n" if $printit;
276 print HEAD "#define APPENDSEEN\n" if $appendseen;
277 print HEAD "#define TSEEN\n" if $tseen;
278 print HEAD "#define DSEEN\n" if $dseen;
279 print HEAD "#define ASSUMEN\n" if $assumen;
280 print HEAD "#define ASSUMEP\n" if $assumep;
281 print HEAD "#define TOPLABEL\n" if $toplabel;
282 print HEAD "#define SAWNEXT\n" if $sawnext;
283 if ($opens) {print HEAD "$opens\n";}
284 open(BODY,"/tmp/sperl$$")
285 || &Die("Can't reopen temp file: $!\n");
293 : eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
294 : if \$running_under_some_shell;
297 open(BODY,"cc -E /tmp/sperl2$$.c |") ||
298 &Die("Can't reopen temp file: $!\n");
312 unlink "sperl$$", "sperl2$$", "sperl2$$.c";
319 "\t" x ($indent / 8) . ' ' x ($indent % 8);
321 sub make_filehandle {
324 if (!$seen{$fname}) {
325 $_ = "FH_" . $_ if /^\d/;
330 for ($tmp = "a"; $fhseen{"$_$tmp"}; $a++) {}
334 $opens .= &q(<<"EOT");
335 : open($_, '>$fname') || die "Can't create $fname: \$!";
344 $label =~ s/[^a-zA-Z0-9]/_/g;
345 if ($label =~ /^[0-9_]/) { $label = 'L' . $label; }
346 $label = substr($label,0,8);
348 # Could be a reserved word, so capitalize it.
349 substr($label,0,1) =~ y/a-z/A-Z/
350 if $label =~ /^[a-z]/;
359 chop($_ = &q(<<'EOT'));
370 chop($_ = &q(<<'EOT'));
374 : print if $printit++;
379 : { $printit++ unless $nflag; }
387 : <<--#ifdef APPENDSEEN
388 : if ($atext) {chop $atext; print $atext; $atext = '';}
401 $command = $space . "\$atext .= <<'End_Of_Text';\n<<--";
406 unless (s|\\$||) { $lastline = 1;}
407 s/^([ \t]*\n)/<><>$1/;
412 $_ = $command . "End_Of_Text";
417 if (/^c/) { $change = 1; }
418 $addr1 = 1 if $addr1 eq '';
419 $addr1 = '$iter = (' . $addr1 . ')';
421 " if (\$iter == 1) { print <<'End_Of_Text'; }\n<<--";
426 unless (s/\\$//) { $lastline = 1;}
428 s/^([ \t]*\n)/<><>$1/;
433 $_ = $command . "End_Of_Text";
437 chop($_ = &q(<<"EOT"));
439 : $space\$printit = 0;
449 $delim = substr($_,1,1);
453 for ($i = 2; $i < $len; $i++) {
454 $c = substr($_,$i,1);
457 substr($_, $i, 0) = '\\';
476 $_ = substr($_,0,--$len);
478 elsif (substr($_,$i,1) =~ /^[n]$/) {
482 substr($_,$i,1) =~ /^[(){}\w]$/) {
485 substr($_, $i, 1) = '';
488 substr($_,$i,1) =~ /^[<>]$/) {
489 substr($_,$i,1) = 'b';
492 elsif ($c eq '[' && !$repl) {
493 $i++ if substr($_,$i,1) eq '^';
494 $i++ if substr($_,$i,1) eq ']';
501 substr($_, $i, 1) = '\\t';
505 elsif (!$repl && index("()+",$c) >= 0) {
506 substr($_, $i, 0) = '\\';
511 &Die("Malformed substitution at line $.\n")
513 $pat = substr($_, 0, $repl + 1);
514 $repl = substr($_, $repl+1, $end-$repl-1);
515 $end = substr($_, $end + 1, 1000);
520 $repl =~ s/[\\]([0-9])/$dol$1/g;
521 $subst = "$pat$repl$delim";
524 if ($end =~ s/^g//) {
528 if ($end =~ s/^p//) {
529 $cmd .= ' && (print)';
532 if ($end =~ s/^w[ \t]*//) {
533 $fh = &make_filehandle($end);
534 $cmd .= " && (print $fh \$_)";
538 &Die("Unrecognized substitution command".
539 "($end) at line $.\n");
541 chop ($_ = &q(<<"EOT"));
543 : $subst && \$tflag++$cmd;
558 $fh = &make_filehandle($_);
559 $_ = "print $fh \$_;";
567 $_ = "\$atext .= `cat $file 2>/dev/null`;";
572 $_ = 'print $1 if /^(.*)/;';
577 chop($_ = &q(<<'EOT'));
587 chop($_ = &q(<<'EOT'));
591 : chop if $len1 < length;
605 $_ = '$hold .= "\n"; $hold .= $_;';
615 $_ = '$_ .= "\n"; $_ .= $hold;';
620 $_ = '($_, $hold) = ($hold, $_);';
632 $lab = &make_label($_);
633 if ($lab eq $toplabel) {
642 $_ = 'next LINE if $tflag;';
650 $lab = &make_label($_);
651 $_ = q/if ($tflag) {$tflag = 0; /;
652 if ($lab eq $toplabel) {
662 s/abcdefghijklmnopqrstuvwxyz/a-z/g;
663 s/ABCDEFGHIJKLMNOPQRSTUVWXYZ/A-Z/g;
677 chop($_ = &q(<<'EOT'));
688 s/(\n)(.)/$1$space$2/g;
697 local($addr) = $outer;
699 local($prefix,$delim,$ch);
701 # Process pattern one potential delimiter at a time.
703 DELIM: while (s#^([^\]+(|)[\\/]*)([]+(|)[\\/])##) {
706 if ($delim eq '\\') {
709 $delim = '' if $ch =~ /^[(){}A-Za-mo-z]$/;
710 $ch = 'b' if $ch =~ /^[<>]$/;
713 elsif ($delim eq '[') {
715 s/^\^// && ($delim .= '^');
716 s/^]// && ($delim .= ']');
718 elsif ($delim eq ']') {
721 elsif ($inbracket || $delim ne $outer) {
722 $delim = '\\' . $delim;
726 if ($delim eq $outer && !$inbracket) {
738 $string =~ s/^:\t?//g;
743 $_[0] =~ s/_a-za-z0-9/\\w/ig;
744 $_[0] =~ s/a-z_a-z0-9/\\w/ig;
745 $_[0] =~ s/a-za-z_0-9/\\w/ig;
746 $_[0] =~ s/a-za-z0-9_/\\w/ig;
747 $_[0] =~ s/_0-9a-za-z/\\w/ig;
748 $_[0] =~ s/0-9_a-za-z/\\w/ig;
749 $_[0] =~ s/0-9a-z_a-z/\\w/ig;
750 $_[0] =~ s/0-9a-za-z_/\\w/ig;
751 $_[0] =~ s/\[\\w\]/\\w/g;
752 $_[0] =~ s/\[^\\w\]/\\W/g;
753 $_[0] =~ s/\[0-9\]/\\d/g;
754 $_[0] =~ s/\[^0-9\]/\\D/g;
755 $_[0] =~ s/\\d\\d\*/\\d+/g;
756 $_[0] =~ s/\\D\\D\*/\\D+/g;
757 $_[0] =~ s/\\w\\w\*/\\w+/g;
758 $_[0] =~ s/\\t\\t\*/\\t+/g;
759 $_[0] =~ s/(\[.[^]]*\])\1\*/$1+/g;
760 $_[0] =~ s/([\w\s!@#%^&-=,:;'"])\1\*/$1+/g;