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.0.1.2 $$Date: 92/06/08 17:26:31 $
39 # Revision 4.0.1.2 92/06/08 17:26:31 lwall
40 # patch20: s2p didn't output portable startup code
41 # patch20: added ... as variant on ..
42 # patch20: s2p didn't translate s/pat/\&/ or s/pat/\$/ or s/pat/\\1/ right
44 # Revision 4.0.1.1 91/06/07 12:19:18 lwall
45 # patch4: s2p now handles embedded newlines better and optimizes common idioms
47 # Revision 4.0 91/03/20 01:57:59 lwall
56 while ($ARGV[0] =~ /^-/) {
72 die "I don't recognize this switch: $_\n";
76 open(BODY,">/tmp/sperl$$") ||
77 &Die("Can't open temp file: $!\n");
80 if (!$assumen && !$assumep) {
81 print BODY &q(<<'EOT');
82 : while ($ARGV[0] =~ /^-/) {
89 : die "I don't recognize this switch: $_\\n";
95 print BODY &q(<<'EOT');
100 : $printit++ unless $nflag;
104 : $\ = "\n"; # automatically add newline on print
108 : while (chop($_ = <>)) {
119 # Wipe out surrounding whitespace.
123 # Perhaps it's a label/comment.
127 $label = &make_label($_);
130 if (/^(top|(re)?start|redo|begin(ning)|again|input)$/i) {
132 redo LINE; # Never referenced, so delete it if not a comment.
136 if ($lastlinewaslabel++) {
138 print BODY &tab, ";\n";
147 $lastlinewaslabel = '';
150 # Look for one or two address clauses
156 $addr1 = "\$. == $addr1" unless /^,/;
162 $addr1 = &fetchpat('/');
170 $addr2 = &fetchpat('/');
172 &Die("Invalid second address at line $.\n");
174 if ($addr2 =~ /^\d+$/) {
175 $addr1 .= "..$addr2";
178 $addr1 .= "...$addr2";
182 # Now we check for metacommands {, }, and ! and worry
186 # a { to keep vi happy
193 $else = "$r else $l\n";
198 if (s/^{//) { # a } to keep vi happy
205 if ($addr2 || $addr1) {
206 $space = ' ' x $shiftwidth;
210 $_ = &transmogrify();
213 # See if we can optimize to modifier form.
216 if ($_ !~ /[\n{}]/ && $rmaybe && !$change &&
217 $_ !~ / if / && $_ !~ / unless /) {
219 $_ = substr($_,$shiftwidth,1000);
221 $_ = "$if ($addr1) $l\n$change$_$rmaybe";
227 @lines = split(/\n/,$_);
229 unless (s/^ *<<--//) {
242 if ($lastlinewaslabel++) {
244 print BODY &tab, ";\n";
248 if ($appendseen || $tseen || !$assumen) {
249 $printit++ if $dseen || (!$assumen && !$assumep);
250 print BODY &q(<<'EOT');
258 : print if $printit++;
263 : { $printit++ unless $nflag; }
275 : if ($atext) { chop $atext; print $atext; $atext = ''; }
279 print BODY &q(<<'EOT');
287 open(HEAD,">/tmp/sperl2$$.c")
288 || &Die("Can't open temp file 2: $!\n");
289 print HEAD "#define PRINTIT\n" if $printit;
290 print HEAD "#define APPENDSEEN\n" if $appendseen;
291 print HEAD "#define TSEEN\n" if $tseen;
292 print HEAD "#define DSEEN\n" if $dseen;
293 print HEAD "#define ASSUMEN\n" if $assumen;
294 print HEAD "#define ASSUMEP\n" if $assumep;
295 print HEAD "#define TOPLABEL\n" if $toplabel;
296 print HEAD "#define SAWNEXT\n" if $sawnext;
297 if ($opens) {print HEAD "$opens\n";}
298 open(BODY,"/tmp/sperl$$")
299 || &Die("Can't reopen temp file: $!\n");
307 : eval 'exec $bin/perl -S \$0 \${1+"\$@"}'
308 : if \$running_under_some_shell;
311 open(BODY,"cc -E /tmp/sperl2$$.c |") ||
312 &Die("Can't reopen temp file: $!\n");
326 unlink "sperl$$", "sperl2$$", "sperl2$$.c";
333 "\t" x ($indent / 8) . ' ' x ($indent % 8);
335 sub make_filehandle {
338 if (!$seen{$fname}) {
339 $_ = "FH_" . $_ if /^\d/;
344 for ($tmp = "a"; $fhseen{"$_$tmp"}; $a++) {}
348 $opens .= &q(<<"EOT");
349 : open($_, '>$fname') || die "Can't create $fname: \$!";
358 $label =~ s/[^a-zA-Z0-9]/_/g;
359 if ($label =~ /^[0-9_]/) { $label = 'L' . $label; }
360 $label = substr($label,0,8);
362 # Could be a reserved word, so capitalize it.
363 substr($label,0,1) =~ y/a-z/A-Z/
364 if $label =~ /^[a-z]/;
373 chop($_ = &q(<<'EOT'));
384 chop($_ = &q(<<'EOT'));
388 : print if $printit++;
393 : { $printit++ unless $nflag; }
401 : <<--#ifdef APPENDSEEN
402 : if ($atext) {chop $atext; print $atext; $atext = '';}
415 $command = $space . "\$atext .= <<'End_Of_Text';\n<<--";
420 unless (s|\\$||) { $lastline = 1;}
421 s/^([ \t]*\n)/<><>$1/;
426 $_ = $command . "End_Of_Text";
431 if (/^c/) { $change = 1; }
432 $addr1 = 1 if $addr1 eq '';
433 $addr1 = '$iter = (' . $addr1 . ')';
435 " if (\$iter == 1) { print <<'End_Of_Text'; }\n<<--";
440 unless (s/\\$//) { $lastline = 1;}
442 s/^([ \t]*\n)/<><>$1/;
447 $_ = $command . "End_Of_Text";
451 chop($_ = &q(<<"EOT"));
453 : $space\$printit = 0;
463 $delim = substr($_,1,1);
467 for ($i = 2; $i < $len; $i++) {
468 $c = substr($_,$i,1);
471 substr($_, $i, 0) = '\\';
490 $_ = substr($_,0,--$len);
492 elsif (substr($_,$i,1) =~ /^[n]$/) {
496 substr($_,$i,1) =~ /^[(){}\w]$/) {
499 substr($_, $i, 1) = '';
502 substr($_,$i,1) =~ /^[<>]$/) {
503 substr($_,$i,1) = 'b';
505 elsif ($repl && substr($_,$i,1) =~ /^\d$/) {
506 substr($_,$i-1,1) = '$';
509 elsif ($c eq '&' && $repl) {
510 substr($_, $i, 0) = '$';
514 elsif ($c eq '$' && $repl) {
515 substr($_, $i, 0) = '\\';
519 elsif ($c eq '[' && !$repl) {
520 $i++ if substr($_,$i,1) eq '^';
521 $i++ if substr($_,$i,1) eq ']';
528 substr($_, $i, 1) = '\\t';
532 elsif (!$repl && index("()+",$c) >= 0) {
533 substr($_, $i, 0) = '\\';
538 &Die("Malformed substitution at line $.\n")
540 $pat = substr($_, 0, $repl + 1);
541 $repl = substr($_, $repl+1, $end-$repl-1);
542 $end = substr($_, $end + 1, 1000);
545 $subst = "$pat$repl$delim";
548 if ($end =~ s/^g//) {
552 if ($end =~ s/^p//) {
553 $cmd .= ' && (print)';
556 if ($end =~ s/^w[ \t]*//) {
557 $fh = &make_filehandle($end);
558 $cmd .= " && (print $fh \$_)";
562 &Die("Unrecognized substitution command".
563 "($end) at line $.\n");
565 chop ($_ = &q(<<"EOT"));
567 : $subst && \$tflag++$cmd;
582 $fh = &make_filehandle($_);
583 $_ = "print $fh \$_;";
591 $_ = "\$atext .= `cat $file 2>/dev/null`;";
596 $_ = 'print $1 if /^(.*)/;';
601 chop($_ = &q(<<'EOT'));
611 chop($_ = &q(<<'EOT'));
615 : chop if $len1 < length;
629 $_ = '$hold .= "\n"; $hold .= $_;';
639 $_ = '$_ .= "\n"; $_ .= $hold;';
644 $_ = '($_, $hold) = ($hold, $_);';
656 $lab = &make_label($_);
657 if ($lab eq $toplabel) {
666 $_ = 'next LINE if $tflag;';
674 $lab = &make_label($_);
675 $_ = q/if ($tflag) {$tflag = 0; /;
676 if ($lab eq $toplabel) {
686 s/abcdefghijklmnopqrstuvwxyz/a-z/g;
687 s/ABCDEFGHIJKLMNOPQRSTUVWXYZ/A-Z/g;
701 chop($_ = &q(<<'EOT'));
712 s/(\n)(.)/$1$space$2/g;
721 local($addr) = $outer;
723 local($prefix,$delim,$ch);
725 # Process pattern one potential delimiter at a time.
727 DELIM: while (s#^([^\]+(|)[\\/]*)([]+(|)[\\/])##) {
730 if ($delim eq '\\') {
733 $delim = '' if $ch =~ /^[(){}A-Za-mo-z]$/;
734 $ch = 'b' if $ch =~ /^[<>]$/;
737 elsif ($delim eq '[') {
739 s/^\^// && ($delim .= '^');
740 s/^]// && ($delim .= ']');
742 elsif ($delim eq ']') {
745 elsif ($inbracket || $delim ne $outer) {
746 $delim = '\\' . $delim;
750 if ($delim eq $outer && !$inbracket) {
762 $string =~ s/^:\t?//g;
767 $_[0] =~ s/_a-za-z0-9/\\w/ig;
768 $_[0] =~ s/a-z_a-z0-9/\\w/ig;
769 $_[0] =~ s/a-za-z_0-9/\\w/ig;
770 $_[0] =~ s/a-za-z0-9_/\\w/ig;
771 $_[0] =~ s/_0-9a-za-z/\\w/ig;
772 $_[0] =~ s/0-9_a-za-z/\\w/ig;
773 $_[0] =~ s/0-9a-z_a-z/\\w/ig;
774 $_[0] =~ s/0-9a-za-z_/\\w/ig;
775 $_[0] =~ s/\[\\w\]/\\w/g;
776 $_[0] =~ s/\[^\\w\]/\\W/g;
777 $_[0] =~ s/\[0-9\]/\\d/g;
778 $_[0] =~ s/\[^0-9\]/\\D/g;
779 $_[0] =~ s/\\d\\d\*/\\d+/g;
780 $_[0] =~ s/\\D\\D\*/\\D+/g;
781 $_[0] =~ s/\\w\\w\*/\\w+/g;
782 $_[0] =~ s/\\t\\t\*/\\t+/g;
783 $_[0] =~ s/(\[.[^]]*\])\1\*/$1+/g;
784 $_[0] =~ s/([\w\s!@#%^&-=,:;'"])\1\*/$1+/g;