4 use File::Basename qw(&basename &dirname);
6 # List explicitly here the variables you want Configure to
7 # generate. Metaconfig only looks for shell variables, so you
8 # have to mention them as if they were shell variables, not
9 # %Config entries. Thus you write
11 # to ensure Configure will look for $Config{startperl}.
13 # This forces PL files to create target in same directory as PL file.
14 # This is so that make depend always knows where to find PL derivatives.
16 ($file = basename($0)) =~ s/\.PL$//;
18 if ($^O eq 'VMS' or $^O eq 'os2'); # "case-forgiving"
20 open OUT,">$file" or die "Can't create $file: $!";
22 print "Extracting $file (with variable substitutions)\n";
24 # In this section, perl variables will be expanded during extraction.
25 # You can use $Config{...} to use Configure variables.
27 print OUT <<"!GROK!THIS!";
29 eval 'exec perl -S \$0 "\$@"'
31 \$startperl = "$Config{startperl}";
34 # In the following, perl variables are not expanded during extraction.
36 print OUT <<'!NO!SUBS!';
38 # $RCSfile: s2p.PL,v $$Revision: 1.1.1.1 $$Date: 1997/01/11 12:49:38 $
41 # Revision 1.1.1.1 1997/01/11 12:49:38 mbeattie
49 while ($ARGV[0] =~ /^-/) {
65 die "I don't recognize this switch: $_\n";
69 open(BODY,">/tmp/sperl$$") ||
70 &Die("Can't open temp file: $!\n");
73 if (!$assumen && !$assumep) {
74 print BODY &q(<<'EOT');
75 : while ($ARGV[0] =~ /^-/) {
82 : die "I don't recognize this switch: $_\\n";
88 print BODY &q(<<'EOT');
93 : $printit++ unless $nflag;
97 : $\ = "\n"; # automatically add newline on print
101 : while (chop($_ = <>)) {
112 # Wipe out surrounding whitespace.
116 # Perhaps it's a label/comment.
120 $label = &make_label($_);
123 if (/^(top|(re)?start|redo|begin(ning)|again|input)$/i) {
125 redo LINE; # Never referenced, so delete it if not a comment.
129 if ($lastlinewaslabel++) {
131 print BODY &tab, ";\n";
140 $lastlinewaslabel = '';
143 # Look for one or two address clauses
149 $addr1 = "\$. == $addr1" unless /^,/;
155 $addr1 = &fetchpat('/');
163 $addr2 = &fetchpat('/');
165 &Die("Invalid second address at line $.\n");
167 if ($addr2 =~ /^\d+$/) {
168 $addr1 .= "..$addr2";
171 $addr1 .= "...$addr2";
175 # Now we check for metacommands {, }, and ! and worry
179 # a { to keep vi happy
186 $else = "$r else $l\n";
191 if (s/^{//) { # a } to keep vi happy
198 if ($addr2 || $addr1) {
199 $space = ' ' x $shiftwidth;
203 $_ = &transmogrify();
206 # See if we can optimize to modifier form.
209 if ($_ !~ /[\n{}]/ && $rmaybe && !$change &&
210 $_ !~ / if / && $_ !~ / unless /) {
212 $_ = substr($_,$shiftwidth,1000);
214 $_ = "$if ($addr1) $l\n$change$_$rmaybe";
220 @lines = split(/\n/,$_);
222 unless (s/^ *<<--//) {
235 if ($lastlinewaslabel++) {
237 print BODY &tab, ";\n";
241 if ($appendseen || $tseen || !$assumen) {
242 $printit++ if $dseen || (!$assumen && !$assumep);
243 print BODY &q(<<'EOT');
251 : print if $printit++;
256 : { $printit++ unless $nflag; }
268 : if ($atext) { chop $atext; print $atext; $atext = ''; }
272 print BODY &q(<<'EOT');
280 open(HEAD,">/tmp/sperl2$$.c")
281 || &Die("Can't open temp file 2: $!\n");
282 print HEAD "#define PRINTIT\n" if $printit;
283 print HEAD "#define APPENDSEEN\n" if $appendseen;
284 print HEAD "#define TSEEN\n" if $tseen;
285 print HEAD "#define DSEEN\n" if $dseen;
286 print HEAD "#define ASSUMEN\n" if $assumen;
287 print HEAD "#define ASSUMEP\n" if $assumep;
288 print HEAD "#define TOPLABEL\n" if $toplabel;
289 print HEAD "#define SAWNEXT\n" if $sawnext;
290 if ($opens) {print HEAD "$opens\n";}
291 open(BODY,"/tmp/sperl$$")
292 || &Die("Can't reopen temp file: $!\n");
300 : eval 'exec perl -S \$0 \${1+"\$@"}'
301 : if \$running_under_some_shell;
304 open(BODY,"cc -E /tmp/sperl2$$.c |") ||
305 &Die("Can't reopen temp file: $!\n");
319 unlink "sperl$$", "sperl2$$", "sperl2$$.c";
326 "\t" x ($indent / 8) . ' ' x ($indent % 8);
328 sub make_filehandle {
331 if (!$seen{$fname}) {
332 $_ = "FH_" . $_ if /^\d/;
337 for ($tmp = "a"; $fhseen{"$_$tmp"}; $a++) {}
341 $opens .= &q(<<"EOT");
342 : open($_, '>$fname') || die "Can't create $fname: \$!";
351 $label =~ s/[^a-zA-Z0-9]/_/g;
352 if ($label =~ /^[0-9_]/) { $label = 'L' . $label; }
353 $label = substr($label,0,8);
355 # Could be a reserved word, so capitalize it.
356 substr($label,0,1) =~ y/a-z/A-Z/
357 if $label =~ /^[a-z]/;
366 chop($_ = &q(<<'EOT'));
377 chop($_ = &q(<<'EOT'));
381 : print if $printit++;
386 : { $printit++ unless $nflag; }
394 : <<--#ifdef APPENDSEEN
395 : if ($atext) {chop $atext; print $atext; $atext = '';}
408 $command = $space . "\$atext .= <<'End_Of_Text';\n<<--";
413 unless (s|\\$||) { $lastline = 1;}
414 s/^([ \t]*\n)/<><>$1/;
419 $_ = $command . "End_Of_Text";
424 if (/^c/) { $change = 1; }
425 $addr1 = 1 if $addr1 eq '';
426 $addr1 = '$iter = (' . $addr1 . ')';
428 " if (\$iter == 1) { print <<'End_Of_Text'; }\n<<--";
433 unless (s/\\$//) { $lastline = 1;}
435 s/^([ \t]*\n)/<><>$1/;
440 $_ = $command . "End_Of_Text";
444 chop($_ = &q(<<"EOT"));
446 : $space\$printit = 0;
456 $delim = substr($_,1,1);
460 for ($i = 2; $i < $len; $i++) {
461 $c = substr($_,$i,1);
464 substr($_, $i, 0) = '\\';
483 $_ = substr($_,0,--$len);
485 elsif (substr($_,$i,1) =~ /^[n]$/) {
489 substr($_,$i,1) =~ /^[(){}\w]$/) {
492 substr($_, $i, 1) = '';
495 substr($_,$i,1) =~ /^[<>]$/) {
496 substr($_,$i,1) = 'b';
498 elsif ($repl && substr($_,$i,1) =~ /^\d$/) {
499 substr($_,$i-1,1) = '$';
502 elsif ($c eq '&' && $repl) {
503 substr($_, $i, 0) = '$';
507 elsif ($c eq '$' && $repl) {
508 substr($_, $i, 0) = '\\';
512 elsif ($c eq '[' && !$repl) {
513 $i++ if substr($_,$i,1) eq '^';
514 $i++ if substr($_,$i,1) eq ']';
521 substr($_, $i, 1) = '\\t';
525 elsif (!$repl && index("()+",$c) >= 0) {
526 substr($_, $i, 0) = '\\';
531 &Die("Malformed substitution at line $.\n")
533 $pat = substr($_, 0, $repl + 1);
534 $repl = substr($_, $repl+1, $end-$repl-1);
535 $end = substr($_, $end + 1, 1000);
538 $subst = "$pat$repl$delim";
541 if ($end =~ s/^g//) {
545 if ($end =~ s/^p//) {
546 $cmd .= ' && (print)';
549 if ($end =~ s/^w[ \t]*//) {
550 $fh = &make_filehandle($end);
551 $cmd .= " && (print $fh \$_)";
555 &Die("Unrecognized substitution command".
556 "($end) at line $.\n");
558 chop ($_ = &q(<<"EOT"));
560 : $subst && \$tflag++$cmd;
575 $fh = &make_filehandle($_);
576 $_ = "print $fh \$_;";
584 $_ = "\$atext .= `cat $file 2>/dev/null`;";
589 $_ = 'print $1 if /^(.*)/;';
594 chop($_ = &q(<<'EOT'));
604 chop($_ = &q(<<'EOT'));
608 : chop if $len1 < length;
622 $_ = '$hold .= "\n"; $hold .= $_;';
632 $_ = '$_ .= "\n"; $_ .= $hold;';
637 $_ = '($_, $hold) = ($hold, $_);';
649 $lab = &make_label($_);
650 if ($lab eq $toplabel) {
659 $_ = 'next LINE if $tflag;';
667 $lab = &make_label($_);
668 $_ = q/if ($tflag) {$tflag = 0; /;
669 if ($lab eq $toplabel) {
679 s/abcdefghijklmnopqrstuvwxyz/a-z/g;
680 s/ABCDEFGHIJKLMNOPQRSTUVWXYZ/A-Z/g;
694 chop($_ = &q(<<'EOT'));
705 s/(\n)(.)/$1$space$2/g;
714 local($addr) = $outer;
716 local($prefix,$delim,$ch);
718 # Process pattern one potential delimiter at a time.
720 DELIM: while (s#^([^\]+(|)[\\/]*)([]+(|)[\\/])##) {
723 if ($delim eq '\\') {
726 $delim = '' if $ch =~ /^[(){}A-Za-mo-z]$/;
727 $ch = 'b' if $ch =~ /^[<>]$/;
730 elsif ($delim eq '[') {
732 s/^\^// && ($delim .= '^');
733 s/^]// && ($delim .= ']');
735 elsif ($delim eq ']') {
738 elsif ($inbracket || $delim ne $outer) {
739 $delim = '\\' . $delim;
743 if ($delim eq $outer && !$inbracket) {
755 $string =~ s/^:\t?//g;
760 $_[0] =~ s/_a-za-z0-9/\\w/ig;
761 $_[0] =~ s/a-z_a-z0-9/\\w/ig;
762 $_[0] =~ s/a-za-z_0-9/\\w/ig;
763 $_[0] =~ s/a-za-z0-9_/\\w/ig;
764 $_[0] =~ s/_0-9a-za-z/\\w/ig;
765 $_[0] =~ s/0-9_a-za-z/\\w/ig;
766 $_[0] =~ s/0-9a-z_a-z/\\w/ig;
767 $_[0] =~ s/0-9a-za-z_/\\w/ig;
768 $_[0] =~ s/\[\\w\]/\\w/g;
769 $_[0] =~ s/\[^\\w\]/\\W/g;
770 $_[0] =~ s/\[0-9\]/\\d/g;
771 $_[0] =~ s/\[^0-9\]/\\D/g;
772 $_[0] =~ s/\\d\\d\*/\\d+/g;
773 $_[0] =~ s/\\D\\D\*/\\D+/g;
774 $_[0] =~ s/\\w\\w\*/\\w+/g;
775 $_[0] =~ s/\\t\\t\*/\\t+/g;
776 $_[0] =~ s/(\[.[^]]*\])\1\*/$1+/g;
777 $_[0] =~ s/([\w\s!@#%^&-=,:;'"])\1\*/$1+/g;
782 close OUT or die "Can't close $file: $!";
783 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
784 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';