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 ../../config.sh . || \
11 ln ../../../config.sh . || \
12 (echo "Can't find config.sh."; exit 1)
17 echo "Extracting s2p (with variable substitutions)"
18 : This section of the file will have variable substitutions done on it.
19 : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
20 : Protect any dollar signs and backticks that you do not want interpreted
21 : by putting a backslash in front. You may delete these comments.
22 $spitshell >s2p <<!GROK!THIS!
28 : In the following dollars and backticks do not need the extra backslash.
29 $spitshell >>s2p <<'!NO!SUBS!'
31 # $Header: s2p.SH,v 3.0.1.5 90/10/16 11:32:40 lwall Locked $
34 # Revision 3.0.1.5 90/10/16 11:32:40 lwall
35 # patch29: s2p modernized
37 # Revision 3.0.1.4 90/08/09 05:50:43 lwall
38 # patch19: s2p didn't translate \n right
40 # Revision 3.0.1.3 90/03/01 10:31:21 lwall
41 # patch9: s2p didn't handle \< and \>
43 # Revision 3.0.1.2 89/11/17 15:51:27 lwall
44 # patch5: in s2p, line labels without a subsequent statement were done wrong
45 # patch5: s2p left residue in /tmp
47 # Revision 3.0.1.1 89/11/11 05:08:25 lwall
48 # patch2: in s2p, + within patterns needed backslashing
49 # patch2: s2p was printing out some debugging info to the output file
51 # Revision 3.0 89/10/18 15:35:02 lwall
54 # Revision 2.0.1.1 88/07/11 23:26:23 root
55 # patch2: s2p didn't put a proper prologue on output script
57 # Revision 2.0 88/06/05 00:15:55 root
58 # Baseline version 2.0.
66 while ($ARGV[0] =~ /^-/) {
82 die "I don't recognize this switch: $_\n";
86 open(BODY,">/tmp/sperl$$") ||
87 &Die("Can't open temp file: $!\n");
90 if (!$assumen && !$assumep) {
92 while ($ARGV[0] =~ /^-/) {
99 die "I don't recognize this switch: $_\\n";
111 $printit++ unless $nflag;
119 # Wipe out surrounding whitespace.
123 # Perhaps it's a label/comment.
127 $label = &make_label($_);
132 if ($lastlinewaslabel++) {
134 print BODY &tab, ";\n";
143 $lastlinewaslabel = '';
146 # Look for one or two address clauses
157 $addr1 = &fetchpat('/');
165 $addr2 = &fetchpat('/');
167 &Die("Invalid second address at line $.\n");
169 $addr1 .= " .. $addr2";
172 # Now we check for metacommands {, }, and ! and worry
176 # a { to keep vi happy
183 $else = "$r else $l\n";
188 if (s/^{//) { # a } to keep vi happy
195 if ($addr2 || $addr1) {
196 $space = ' ' x $shiftwidth;
200 $_ = &transmogrify();
203 # See if we can optimize to modifier form.
206 if ($_ !~ /[\n{}]/ && $rmaybe && !$change &&
207 $_ !~ / if / && $_ !~ / unless /) {
209 $_ = substr($_,$shiftwidth,1000);
211 $_ = "$if ($addr1) $l\n$change$_$rmaybe";
217 @lines = split(/\n/,$_);
219 unless (s/^ *<<--//) {
232 if ($lastlinewaslabel++) {
234 print BODY &tab, ";\n";
239 if ($appendseen || $tseen || !$assumen) {
240 $printit++ if $dseen || (!$assumen && !$assumep);
252 { $printit++ unless $nflag; }
264 if ($atext) { print $atext; $atext = ''; }
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 if ($opens) {print HEAD "$opens\n";}
282 open(BODY,"/tmp/sperl$$")
283 || &Die("Can't reopen temp file: $!\n");
291 eval 'exec $bin/perl -S \$0 \$*'
292 if \$running_under_some_shell;
295 open(BODY,"cc -E /tmp/sperl2$$.c |") ||
296 &Die("Can't reopen temp file: $!\n");
310 unlink "sperl$$", "sperl2$$", "sperl2$$.c";
317 "\t" x ($indent / 8) . ' ' x ($indent % 8);
319 sub make_filehandle {
324 substr($_,0,1) =~ y/a-z/A-Z/ if /^[a-z]/;
327 open($_,'>$fname') || die "Can't create $fname";
335 $label =~ s/[^a-zA-Z0-9]/_/g;
336 if ($label =~ /^[0-9_]/) { $label = 'L' . $label; }
337 $label = substr($label,0,8);
339 # Could be a reserved word, so capitalize it.
340 substr($label,0,1) =~ y/a-z/A-Z/
341 if $label =~ /^[a-z]/;
369 { $printit++ unless $nflag; }
377 <<--#ifdef APPENDSEEN
378 if ($atext) {print $atext; $atext = '';}
390 $command = $space . '$atext .=' . "\n<<--'";
395 unless (s|\\$||) { $lastline = 1;}
397 s/^([ \t]*\n)/<><>$1/;
402 $_ = $command . "';";
407 if (/^c/) { $change = 1; }
408 $addr1 = '$iter = (' . $addr1 . ')';
409 $command = $space . 'if ($iter == 1) { print'
415 unless (s/\\$//) { $lastline = 1;}
417 s/^([ \t]*\n)/<><>$1/;
422 $_ = $command . "';}";
428 $space\$printit = '';
437 $delim = substr($_,1,1);
441 for ($i = 2; $i < $len; $i++) {
442 $c = substr($_,$i,1);
445 substr($_, $i, 0) = '\\';
464 $_ = substr($_,0,--$len);
466 elsif (substr($_,$i,1) =~ /^[n]$/) {
470 substr($_,$i,1) =~ /^[(){}\w]$/) {
473 substr($_, $i, 1) = '';
476 substr($_,$i,1) =~ /^[<>]$/) {
477 substr($_,$i,1) = 'b';
480 elsif ($c eq '[' && !$repl) {
481 $i++ if substr($_,$i,1) eq '^';
482 $i++ if substr($_,$i,1) eq ']';
488 elsif (!$repl && index("()+",$c) >= 0) {
489 substr($_, $i, 0) = '\\';
494 &Die("Malformed substitution at line $.\n")
496 $pat = substr($_, 0, $repl + 1);
497 $repl = substr($_, $repl+1, $end-$repl-1);
498 $end = substr($_, $end + 1, 1000);
502 $repl =~ s/[\\]([0-9])/$dol$1/g;
503 $subst = "$pat$repl$delim";
506 if ($end =~ s/^g//) {
510 if ($end =~ s/^p//) {
511 $cmd .= ' && (print)';
514 if ($end =~ s/^w[ \t]*//) {
515 $fh = &make_filehandle($end);
516 $cmd .= " && (print $fh \$_)";
520 &Die("Unrecognized substitution command".
521 "($end) at line $.\n");
525 $subst && \$tflag++$cmd;
540 $fh = &make_filehandle($_);
541 $_ = "print $fh \$_;";
549 $_ = "\$atext .= `cat $file 2>/dev/null`;";
554 $_ = 'print $1 if /(^.*\n)/;';
583 $_ = '$hold .= $_ ? $_ : "\n";';
593 $_ = '$_ .= $hold ? $hold : "\n";';
598 $_ = '($_, $hold) = ($hold, $_);';
609 $lab = &make_label($_);
610 if ($lab eq $toplabel) {
619 $_ = 'next LINE if $tflag;';
626 $lab = &make_label($_);
627 $_ = q/if ($tflag) {$tflag = ''; /;
628 if ($lab eq $toplabel) {
638 $_ = 'print "$.\n";';
653 s/(\n)(.)/$1$space$2/g;
662 local($addr) = $outer;
664 local($prefix,$delim,$ch);
666 # Process pattern one potential delimiter at a time.
668 DELIM: while (s#^([^\]+(|)[\\/]*)([]+(|)[\\/])##) {
671 if ($delim eq '\\') {
674 $delim = '' if $ch =~ /^[(){}A-Za-mo-z]$/;
675 $ch = 'b' if $ch =~ /^[<>]$/;
678 elsif ($delim eq '[') {
680 s/^\^// && ($delim .= '^');
681 s/^]// && ($delim .= ']');
683 elsif ($delim eq ']') {
686 elsif ($inbracket || $delim ne $outer) {
687 $delim = '\\' . $delim;
691 if ($delim eq $outer && !$inbracket) {