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, '.PL');
17 $file .= '.com' if $^O eq 'VMS';
19 open OUT,">$file" or die "Can't create $file: $!";
21 print "Extracting $file (with variable substitutions)\n";
23 # In this section, perl variables will be expanded during extraction.
24 # You can use $Config{...} to use Configure variables.
26 print OUT <<"!GROK!THIS!";
28 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
29 if \$running_under_some_shell;
30 \$startperl = "$Config{startperl}";
31 \$perlpath = "$Config{perlpath}";
34 # In the following, perl variables are not expanded during extraction.
36 print OUT <<'!NO!SUBS!';
39 # Modified September 26, 1993 to provide proper handling of years after 1999
40 # Tom Link <tml+@pitt.edu>
41 # University of Pittsburgh
43 # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
44 # Billy Constantine <wdconsta@cs.adelaide.edu.au> <billy@smug.adelaide.edu.au>
45 # University of Adelaide, Adelaide, South Australia
48 while ($ARGV[0] =~ /^[^-!(]/) {
51 @roots = ('.') unless @roots;
52 for (@roots) { $_ = "e($_); }
53 $roots = join(',', @roots);
61 s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
71 elsif ($_ eq 'follow') {
73 $decl = '%already_seen = ();';
74 $out .= &tab . '(not $already_seen{"$dev,$ino"}) &&';
75 $out .= "\n" . &tab . '(($already_seen{"$dev,$ino"} = !(-d _)) || 1)';
81 elsif ($_ eq 'name') {
83 $pat = &fileglob_to_re(shift);
84 $out .= '/' . $pat . "/";
86 elsif ($_ eq 'perm') {
88 die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
89 if ($onum =~ s/^-//) {
90 $onum = '0' . sprintf("%o", oct($onum) & 017777); # s/b 07777 ?
91 $out .= &tab . "((\$mode & $onum) == $onum)";
94 $onum = '0' . $onum unless $onum =~ /^0/;
95 $out .= &tab . "((\$mode & 0777) == $onum)";
98 elsif ($_ eq 'type') {
99 ($filetest = shift) =~ tr/s/S/;
100 $out .= &tab . "-$filetest _";
102 elsif ($_ eq 'print') {
103 $out .= &tab . 'print("$name\n")';
105 elsif ($_ eq 'print0') {
106 $out .= &tab . 'print("$name\0")';
108 elsif ($_ eq 'fstype') {
112 { $out .= '($dev < 0)'; }
114 { $out .= '($dev >= 0)'; }
116 elsif ($_ eq 'user') {
118 $out .= &tab . "(\$uid == \$uid{'$uname'})";
121 elsif ($_ eq 'group') {
123 $out .= &tab . "(\$gid == \$gid{'$gname'})";
126 elsif ($_ eq 'nouser') {
127 $out .= &tab . '!defined $uid{$uid}';
130 elsif ($_ eq 'nogroup') {
131 $out .= &tab . '!defined $gid{$gid}';
134 elsif ($_ eq 'links') {
135 $out .= &tab . '($nlink ' . &n(shift);
137 elsif ($_ eq 'inum') {
138 $out .= &tab . '($ino ' . &n(shift);
140 elsif ($_ eq 'size') {
141 $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
143 elsif ($_ eq 'atime') {
144 $out .= &tab . '(int(-A _) ' . &n(shift);
146 elsif ($_ eq 'mtime') {
147 $out .= &tab . '(int(-M _) ' . &n(shift);
149 elsif ($_ eq 'ctime') {
150 $out .= &tab . '(int(-C _) ' . &n(shift);
152 elsif ($_ eq 'exec') {
153 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
156 if (m#^(/bin/)?rm -f {}$#) {
158 $out .= &tab . 'unlink($_)';
161 $out .= &tab . '(unlink($_) || 1)';
164 elsif (m#^(/bin/)?rm {}$#) {
165 $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
168 for (@cmd) { s/'/\\'/g; }
170 $out .= &tab . "&exec(0, '@cmd')";
176 for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
178 for (@cmd) { s/'/\\'/g; }
180 $out .= &tab . "&exec(1, '@cmd')";
184 elsif ($_ eq 'prune') {
185 $out .= &tab . '($prune = 1)';
187 elsif ($_ eq 'xdev') {
188 $out .= &tab . '!($prune |= ($dev != $topdev))';
190 elsif ($_ eq 'newer') {
193 $newername = 'AGE_OF' . $file;
194 $newername =~ s/[^\w]/_/g;
195 $newername = "\$$newername";
196 $out .= "(-M _ < $newername)";
197 $initnewer .= "$newername = -M " . "e($file) . ";\n";
199 elsif ($_ eq 'eval') {
200 $prog = "e(shift);
201 $out .= &tab . "eval $prog";
203 elsif ($_ eq 'depth') {
208 $out .= &tab . "&ls";
211 elsif ($_ eq 'tar') {
213 die "-tar must have a filename argument\n" unless @ARGV;
219 $initfile .= "open($fh, " . "e($file) .
220 qq{) || die "Can't open $fh: \$!\\n";\n};
222 $flushall = "\n&tflushall;\n";
227 die "-$_ must have a filename argument\n" unless @ARGV;
231 $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
233 $initfile .= "open($fh, " . "e($file) .
234 qq{) || die "Can't open $fh: \$!\\n";\n};
236 $flushall = "\n&flushall;\n";
239 die "Unrecognized switch: -$_\n";
242 if ($ARGV[0] eq '-o') {
243 { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
244 $statdone = 0 if $indent == 1 && $delayedstat;
249 $out .= " &&" unless $ARGV[0] eq ')';
251 shift if $ARGV[0] eq '-a';
258 eval 'exec $perlpath -S \$0 \${1+"\$@"}'
259 if \$running_under_some_shell;
265 @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
266 @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
271 if ($inituser || $initls) {
272 print 'while (($name, $pw, $uid) = getpwent) {', "\n";
273 print ' $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
274 print ' $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
278 if ($initgroup || $initls) {
279 print 'while (($name, $pw, $gid) = getgrent) {', "\n";
280 print ' $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
281 print ' $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
285 print $initnewer, "\n" if $initnewer;
287 print $initfile, "\n" if $initfile;
289 $find = $depth ? "finddepth" : "find";
293 # Traverse desired filesystems
308 local($ok, @cmd) = @_;
309 foreach $word (@cmd) {
310 $word =~ s#{}#$name#g;
313 local($old) = select(STDOUT);
317 return 0 unless <STDIN> =~ /^y/;
329 print <<"INTERP", <<'END';
331 (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$sizemm,
332 \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
337 if (defined $blocks) {
338 $blocks = int(($blocks + 1) / 2);
341 $blocks = int(($size + 1023) / 1024);
344 if (-f _) { $perms = '-'; }
345 elsif (-d _) { $perms = 'd'; }
346 elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
347 elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
348 elsif (-p _) { $perms = 'p'; }
349 elsif (-S _) { $perms = 's'; }
350 else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
353 $tmp = $rwx[$tmpmode & 7];
355 $tmp = $rwx[$tmpmode & 7] . $tmp;
357 $tmp = $rwx[$tmpmode & 7] . $tmp;
358 substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
359 substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
360 substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
363 $user = $user{$uid} || $uid;
364 $group = $group{$gid} || $gid;
366 ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
367 $moname = $moname[$mon];
368 if (-M _ > 365.25 / 2) {
369 $timeyear = $year + 1900;
372 $timeyear = sprintf("%02d:%02d", $hour, $min);
375 printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
391 sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
398 print <<'START', <<"INTERP", <<'END';
403 if ($name eq 'TRAILER!!!') {
409 (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
410 \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
413 open(IN, "./$_\0") || do {
414 warn "Couldn't open $name: $!\n";
419 $text = readlink($_);
420 $size = 0 unless defined $text;
424 ($nm = $name) =~ s#^\./##;
428 sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
443 $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
444 $cpout{$fh} .= pack("SSSSSSSSLSLa*",
445 070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
446 length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
449 $cpout{$fh} .= $text;
452 &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
453 while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
455 $l = length($cpout{$fh});
464 while (length($cpout{$fh}) >= 5120) {
465 syswrite($fh,$cpout{$fh},5120);
467 substr($cpout{$fh}, 0, 5120) = '';
472 $name = 'TRAILER!!!';
473 foreach $fh (keys %cpout) {
475 $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
477 print $blocks{$fh} * 10, " blocks\n";
485 print <<'START', <<"INTERP", <<'END';
488 local($linkname,$header,$l,$slop);
489 local($linkflag) = "\0";
492 (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
493 \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
497 if ($linkname = $linkseen{$fh,$dev,$ino}) {
501 $linkseen{$fh,$dev,$ino} = $nm;
505 open(IN, "./$_\0") || do {
506 warn "Couldn't open $name: $!\n";
509 $size = 0 if $linkflag ne "\0";
512 $linkname = readlink($_);
513 $linkflag = 2 if defined $linkname;
518 $header = pack("a100a8a8a8a12a12a8a1a100",
520 sprintf("%6o ", $mode & 0777),
521 sprintf("%6o ", $uid & 0777777),
522 sprintf("%6o ", $gid & 0777777),
523 sprintf("%11o ", $size),
524 sprintf("%11o ", $mtime),
528 $l = length($header) % 512;
529 substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
530 substr($header, 154, 1) = "\0"; # blech
531 $tarout{$fh} .= $header;
532 $tarout{$fh} .= "\0" x (512 - $l) if $l;
534 &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
535 while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
536 $slop = length($tarout{$fh}) % 512;
537 $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
539 $l = length($tarout{$fh});
548 while (length($tarout{$fh}) >= 10240) {
549 syswrite($fh,$tarout{$fh},10240);
551 substr($tarout{$fh}, 0, 10240) = '';
558 foreach $fh (keys %tarout) {
559 $len = 10240 - length($tarout{$fh});
560 $len += 10240 if $len < 1024;
561 $tarout{$fh} .= "\0" x $len;
571 ############################################################################
576 $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
578 if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
583 $tabstring .= <<"ENDOFSTAT" . $tabstring;
584 (\$nlink || ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\))) &&
588 $tabstring .= <<"ENDOFSTAT" . $tabstring;
589 ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\)) &&
595 $tabstring =~ s/^\s+/ / if $out =~ /!$/;
602 $tmp =~ s#([./^\$()])#\\$1#g;
603 $tmp =~ s/([?*])/.$1/g;
610 $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
611 $n =~ s/ 0*(\d)/ $1/;
622 close OUT or die "Can't close $file: $!";
623 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
624 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';