4 use File::Basename qw(&basename &dirname);
7 # List explicitly here the variables you want Configure to
8 # generate. Metaconfig only looks for shell variables, so you
9 # have to mention them as if they were shell variables, not
10 # %Config entries. Thus you write
12 # to ensure Configure will look for $Config{startperl}.
14 # This forces PL files to create target in same directory as PL file.
15 # This is so that make depend always knows where to find PL derivatives.
18 $file = basename($0, '.PL');
19 $file .= '.com' if $^O eq 'VMS';
21 open OUT,">$file" or die "Can't create $file: $!";
23 print "Extracting $file (with variable substitutions)\n";
25 # In this section, perl variables will be expanded during extraction.
26 # You can use $Config{...} to use Configure variables.
28 print OUT <<"!GROK!THIS!";
30 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
31 if \$running_under_some_shell;
32 \$startperl = "$Config{startperl}";
33 \$perlpath = "$Config{perlpath}";
36 # In the following, perl variables are not expanded during extraction.
38 print OUT <<'!NO!SUBS!';
41 # Modified September 26, 1993 to provide proper handling of years after 1999
42 # Tom Link <tml+@pitt.edu>
43 # University of Pittsburgh
45 # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
46 # Billy Constantine <wdconsta@cs.adelaide.edu.au> <billy@smug.adelaide.edu.au>
47 # University of Adelaide, Adelaide, South Australia
50 while ($ARGV[0] =~ /^[^-!(]/) {
53 @roots = ('.') unless @roots;
54 for (@roots) { $_ = "e($_); }
55 $roots = join(',', @roots);
63 s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
73 elsif ($_ eq 'follow') {
75 $decl = '%already_seen = ();';
76 $out .= &tab . '(not $already_seen{"$dev,$ino"}) &&';
77 $out .= "\n" . &tab . '(($already_seen{"$dev,$ino"} = !(-d _)) || 1)';
83 elsif ($_ eq 'name') {
85 $pat = &fileglob_to_re(shift);
86 $out .= '/' . $pat . "/";
88 elsif ($_ eq 'perm') {
90 die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
91 if ($onum =~ s/^-//) {
92 $onum = '0' . sprintf("%o", oct($onum) & 017777); # s/b 07777 ?
93 $out .= &tab . "((\$mode & $onum) == $onum)";
96 $onum = '0' . $onum unless $onum =~ /^0/;
97 $out .= &tab . "((\$mode & 0777) == $onum)";
100 elsif ($_ eq 'type') {
101 ($filetest = shift) =~ tr/s/S/;
102 $out .= &tab . "-$filetest _";
104 elsif ($_ eq 'print') {
105 $out .= &tab . 'print("$name\n")';
107 elsif ($_ eq 'print0') {
108 $out .= &tab . 'print("$name\0")';
110 elsif ($_ eq 'fstype') {
114 { $out .= '($dev < 0)'; }
116 { $out .= '($dev >= 0)'; }
118 elsif ($_ eq 'user') {
120 $out .= &tab . "(\$uid == \$uid{'$uname'})";
123 elsif ($_ eq 'group') {
125 $out .= &tab . "(\$gid == \$gid{'$gname'})";
128 elsif ($_ eq 'nouser') {
129 $out .= &tab . '!defined $uid{$uid}';
132 elsif ($_ eq 'nogroup') {
133 $out .= &tab . '!defined $gid{$gid}';
136 elsif ($_ eq 'links') {
137 $out .= &tab . '($nlink ' . &n(shift);
139 elsif ($_ eq 'inum') {
140 $out .= &tab . '($ino ' . &n(shift);
142 elsif ($_ eq 'size') {
143 $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
145 elsif ($_ eq 'atime') {
146 $out .= &tab . '(int(-A _) ' . &n(shift);
148 elsif ($_ eq 'mtime') {
149 $out .= &tab . '(int(-M _) ' . &n(shift);
151 elsif ($_ eq 'ctime') {
152 $out .= &tab . '(int(-C _) ' . &n(shift);
154 elsif ($_ eq 'exec') {
155 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
158 if (m#^(/bin/)?rm -f {}$#) {
160 $out .= &tab . 'unlink($_)';
163 $out .= &tab . '(unlink($_) || 1)';
166 elsif (m#^(/bin/)?rm {}$#) {
167 $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
170 for (@cmd) { s/'/\\'/g; }
172 $out .= &tab . "&exec(0, '@cmd')";
178 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
180 for (@cmd) { s/'/\\'/g; }
182 $out .= &tab . "&exec(1, '@cmd')";
186 elsif ($_ eq 'prune') {
187 $out .= &tab . '($prune = 1)';
189 elsif ($_ eq 'xdev') {
190 $out .= &tab . '!($prune |= ($dev != $topdev))';
192 elsif ($_ eq 'newer') {
195 $newername = 'AGE_OF' . $file;
196 $newername =~ s/[^\w]/_/g;
197 $newername = "\$$newername";
198 $out .= "(-M _ < $newername)";
199 $initnewer .= "$newername = -M " . "e($file) . ";\n";
201 elsif ($_ eq 'eval') {
202 $prog = "e(shift);
203 $out .= &tab . "eval $prog";
205 elsif ($_ eq 'depth') {
210 $out .= &tab . "&ls";
213 elsif ($_ eq 'tar') {
215 die "-tar must have a filename argument\n" unless @ARGV;
221 $initfile .= "open($fh, " . "e($file) .
222 qq{) || die "Can't open $fh: \$!\\n";\n};
224 $flushall = "\n&tflushall;\n";
229 die "-$_ must have a filename argument\n" unless @ARGV;
233 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
235 $initfile .= "open($fh, " . "e($file) .
236 qq{) || die "Can't open $fh: \$!\\n";\n};
238 $flushall = "\n&flushall;\n";
241 die "Unrecognized switch: -$_\n";
244 if ($ARGV[0] eq '-o') {
245 { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
246 $statdone = 0 if $indent == 1 && $delayedstat;
251 $out .= " &&" unless $ARGV[0] eq ')';
253 shift if $ARGV[0] eq '-a';
260 eval 'exec $perlpath -S \$0 \${1+"\$@"}'
261 if \$running_under_some_shell;
267 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
268 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
273 if ($inituser || $initls) {
274 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
275 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
276 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
280 if ($initgroup || $initls) {
281 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
282 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
283 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
287 print $initnewer, "\n" if $initnewer;
289 print $initfile, "\n" if $initfile;
291 $find = $depth ? "finddepth" : "find";
295 # Traverse desired filesystems
310 local($ok, @cmd) = @_;
311 foreach $word (@cmd) {
312 $word =~ s#{}#$name#g;
315 local($old) = select(STDOUT);
319 return 0 unless <STDIN> =~ /^y/;
331 print <<"INTERP", <<'END';
333 (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$sizemm,
334 \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
339 if (defined $blocks) {
340 $blocks = int(($blocks + 1) / 2);
343 $blocks = int(($size + 1023) / 1024);
346 if (-f _) { $perms = '-'; }
347 elsif (-d _) { $perms = 'd'; }
348 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
349 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
350 elsif (-p _) { $perms = 'p'; }
351 elsif (-S _) { $perms = 's'; }
352 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
355 $tmp = $rwx[$tmpmode & 7];
357 $tmp = $rwx[$tmpmode & 7] . $tmp;
359 $tmp = $rwx[$tmpmode & 7] . $tmp;
360 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
361 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
362 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
365 $user = $user{$uid} || $uid;
366 $group = $group{$gid} || $gid;
368 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
369 $moname = $moname[$mon];
370 if (-M _ > 365.25 / 2) {
371 $timeyear = $year + 1900;
374 $timeyear = sprintf("%02d:%02d", $hour, $min);
377 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
393 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
400 print <<'START', <<"INTERP", <<'END';
405 if ($name eq 'TRAILER!!!') {
411 (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
412 \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
415 open(IN, "./$_\0") || do {
416 warn "Couldn't open $name: $!\n";
421 $text = readlink($_);
422 $size = 0 unless defined $text;
426 ($nm = $name) =~ s#^\./##;
430 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
445 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
446 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
447 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
448 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
451 $cpout{$fh} .= $text;
454 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
455 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
457 $l = length($cpout{$fh});
466 while (length($cpout{$fh}) >= 5120) {
467 syswrite($fh,$cpout{$fh},5120);
469 substr($cpout{$fh}, 0, 5120) = '';
474 $name = 'TRAILER!!!';
475 foreach $fh (keys %cpout) {
477 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
479 print $blocks{$fh} * 10, " blocks\n";
487 print <<'START', <<"INTERP", <<'END';
490 local($linkname,$header,$l,$slop);
491 local($linkflag) = "\0";
494 (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
495 \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
499 if ($linkname = $linkseen{$fh,$dev,$ino}) {
503 $linkseen{$fh,$dev,$ino} = $nm;
507 open(IN, "./$_\0") || do {
508 warn "Couldn't open $name: $!\n";
511 $size = 0 if $linkflag ne "\0";
514 $linkname = readlink($_);
515 $linkflag = 2 if defined $linkname;
520 $header = pack("a100a8a8a8a12a12a8a1a100",
522 sprintf("%6o ", $mode & 0777),
523 sprintf("%6o ", $uid & 0777777),
524 sprintf("%6o ", $gid & 0777777),
525 sprintf("%11o ", $size),
526 sprintf("%11o ", $mtime),
530 $l = length($header) % 512;
531 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
532 substr($header, 154, 1) = "\0"; # blech
533 $tarout{$fh} .= $header;
534 $tarout{$fh} .= "\0" x (512 - $l) if $l;
536 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
537 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
538 $slop = length($tarout{$fh}) % 512;
539 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
541 $l = length($tarout{$fh});
550 while (length($tarout{$fh}) >= 10240) {
551 syswrite($fh,$tarout{$fh},10240);
553 substr($tarout{$fh}, 0, 10240) = '';
560 foreach $fh (keys %tarout) {
561 $len = 10240 - length($tarout{$fh});
562 $len += 10240 if $len < 1024;
563 $tarout{$fh} .= "\0" x $len;
573 ############################################################################
578 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
580 if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
585 $tabstring .= <<"ENDOFSTAT" . $tabstring;
586 (\$nlink || ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\))) &&
590 $tabstring .= <<"ENDOFSTAT" . $tabstring;
591 ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\)) &&
597 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
604 $tmp =~ s#([./^\$()])#\\$1#g;
605 $tmp =~ s/([?*])/.$1/g;
612 $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
613 $n =~ s/ 0*(\d)/ $1/;
624 close OUT or die "Can't close $file: $!";
625 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
626 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';