home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / perl-5.003-base.tgz / perl-5.003-base.tar / fsf / perl / x2p / find2perl.PL < prev    next >
Perl Script  |  1996-03-25  |  13KB  |  607 lines

  1. #!/usr/local/bin/perl
  2.  
  3. use Config;
  4. use File::Basename qw(&basename &dirname);
  5.  
  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
  10. #  $startperl
  11. # to ensure Configure will look for $Config{startperl}.
  12.  
  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.
  15. chdir(dirname($0));
  16. ($file = basename($0)) =~ s/\.PL$//;
  17. $file =~ s/\.pl$//
  18.     if ($^O eq 'VMS' or $^O eq 'os2');  # "case-forgiving"
  19.  
  20. open OUT,">$file" or die "Can't create $file: $!";
  21.  
  22. print "Extracting $file (with variable substitutions)\n";
  23.  
  24. # In this section, perl variables will be expanded during extraction.
  25. # You can use $Config{...} to use Configure variables.
  26.  
  27. print OUT <<"!GROK!THIS!";
  28. $Config{'startperl'}
  29.     eval 'exec perl -S \$0 "\$@"'
  30.     if 0;
  31. \$startperl = "$Config{startperl}";
  32. !GROK!THIS!
  33.  
  34. # In the following, perl variables are not expanded during extraction.
  35.  
  36. print OUT <<'!NO!SUBS!';
  37. # Modified September 26, 1993 to provide proper handling of years after 1999
  38. #   Tom Link <tml+@pitt.edu>
  39. #   University of Pittsburgh
  40.  
  41. while ($ARGV[0] =~ /^[^-!(]/) {
  42.     push(@roots, shift);
  43. }
  44. @roots = ('.') unless @roots;
  45. for (@roots) { $_ = "e($_); }
  46. $roots = join(',', @roots);
  47.  
  48. $indent = 1;
  49.  
  50. while (@ARGV) {
  51.     $_ = shift;
  52.     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
  53.     if ($_ eq '(') {
  54.     $out .= &tab . "(\n";
  55.     $indent++;
  56.     next;
  57.     }
  58.     elsif ($_ eq ')') {
  59.     $indent--;
  60.     $out .= &tab . ")";
  61.     }
  62.     elsif ($_ eq '!') {
  63.     $out .= &tab . "!";
  64.     next;
  65.     }
  66.     elsif ($_ eq 'name') {
  67.     $out .= &tab;
  68.     $pat = &fileglob_to_re(shift);
  69.     $out .= '/' . $pat . "/";
  70.     }
  71.     elsif ($_ eq 'perm') {
  72.     $onum = shift;
  73.     die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
  74.     if ($onum =~ s/^-//) {
  75.         $onum = '0' . sprintf("%o", oct($onum) & 017777);    # s/b 07777 ?
  76.         $out .= &tab . "((\$mode & $onum) == $onum)";
  77.     }
  78.     else {
  79.         $onum = '0' . $onum unless $onum =~ /^0/;
  80.         $out .= &tab . "((\$mode & 0777) == $onum)";
  81.     }
  82.     }
  83.     elsif ($_ eq 'type') {
  84.     ($filetest = shift) =~ tr/s/S/;
  85.     $out .= &tab . "-$filetest _";
  86.     }
  87.     elsif ($_ eq 'print') {
  88.     $out .= &tab . 'print("$name\n")';
  89.     }
  90.     elsif ($_ eq 'print0') {
  91.     $out .= &tab . 'print("$name\0")';
  92.     }
  93.     elsif ($_ eq 'fstype') {
  94.     $out .= &tab;
  95.     $type = shift;
  96.     if ($type eq 'nfs')
  97.         { $out .= '($dev < 0)'; }
  98.     else
  99.         { $out .= '($dev >= 0)'; }
  100.     }
  101.     elsif ($_ eq 'user') {
  102.     $uname = shift;
  103.     $out .= &tab . "(\$uid == \$uid{'$uname'})";
  104.     $inituser++;
  105.     }
  106.     elsif ($_ eq 'group') {
  107.     $gname = shift;
  108.     $out .= &tab . "(\$gid == \$gid{'$gname'})";
  109.     $initgroup++;
  110.     }
  111.     elsif ($_ eq 'nouser') {
  112.     $out .= &tab . '!defined $uid{$uid}';
  113.     $inituser++;
  114.     }
  115.     elsif ($_ eq 'nogroup') {
  116.     $out .= &tab . '!defined $gid{$gid}';
  117.     $initgroup++;
  118.     }
  119.     elsif ($_ eq 'links') {
  120.     $out .= &tab . '($nlink ' . &n(shift);
  121.     }
  122.     elsif ($_ eq 'inum') {
  123.     $out .= &tab . '($ino ' . &n(shift);
  124.     }
  125.     elsif ($_ eq 'size') {
  126.     $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
  127.     }
  128.     elsif ($_ eq 'atime') {
  129.     $out .= &tab . '(int(-A _) ' . &n(shift);
  130.     }
  131.     elsif ($_ eq 'mtime') {
  132.     $out .= &tab . '(int(-M _) ' . &n(shift);
  133.     }
  134.     elsif ($_ eq 'ctime') {
  135.     $out .= &tab . '(int(-C _) ' . &n(shift);
  136.     }
  137.     elsif ($_ eq 'exec') {
  138.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  139.     shift;
  140.     $_ = "@cmd";
  141.     if (m#^(/bin/)?rm -f {}$#) {
  142.         if (!@ARGV) {
  143.         $out .= &tab . 'unlink($_)';
  144.         }
  145.         else {
  146.         $out .= &tab . '(unlink($_) || 1)';
  147.         }
  148.     }
  149.     elsif (m#^(/bin/)?rm {}$#) {
  150.         $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
  151.     }
  152.     else {
  153.         for (@cmd) { s/'/\\'/g; }
  154.         $" = "','";
  155.         $out .= &tab . "&exec(0, '@cmd')";
  156.         $" = ' ';
  157.         $initexec++;
  158.     }
  159.     }
  160.     elsif ($_ eq 'ok') {
  161.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  162.     shift;
  163.     for (@cmd) { s/'/\\'/g; }
  164.     $" = "','";
  165.     $out .= &tab . "&exec(1, '@cmd')";
  166.     $" = ' ';
  167.     $initexec++;
  168.     }
  169.     elsif ($_ eq 'prune') {
  170.     $out .= &tab . '($prune = 1)';
  171.     }
  172.     elsif ($_ eq 'xdev') {
  173.     $out .= &tab . '!($prune |= ($dev != $topdev))';
  174.     }
  175.     elsif ($_ eq 'newer') {
  176.     $out .= &tab;
  177.     $file = shift;
  178.     $newername = 'AGE_OF' . $file;
  179.     $newername =~ s/[^\w]/_/g;
  180.     $newername = '$' . $newername;
  181.     $out .= "(-M _ < $newername)";
  182.     $initnewer .= "$newername = -M " . "e($file) . ";\n";
  183.     }
  184.     elsif ($_ eq 'eval') {
  185.     $prog = "e(shift);
  186.     $out .= &tab . "eval $prog";
  187.     }
  188.     elsif ($_ eq 'depth') {
  189.     $depth++;
  190.     next;
  191.     }
  192.     elsif ($_ eq 'ls') {
  193.     $out .= &tab . "&ls";
  194.     $initls++;
  195.     }
  196.     elsif ($_ eq 'tar') {
  197.     $out .= &tab;
  198.     die "-tar must have a filename argument\n" unless @ARGV;
  199.     $file = shift;
  200.     $fh = 'FH' . $file;
  201.     $fh =~ s/[^\w]/_/g;
  202.     $out .= "&tar($fh)";
  203.     $file = '>' . $file;
  204.     $initfile .= "open($fh, " . "e($file) .
  205.       qq{) || die "Can't open $fh: \$!\\n";\n};
  206.     $inittar++;
  207.     $flushall = "\n&tflushall;\n";
  208.     }
  209.     elsif (/^n?cpio$/) {
  210.     $depth++;
  211.     $out .= &tab;
  212.     die "-$_ must have a filename argument\n" unless @ARGV;
  213.     $file = shift;
  214.     $fh = 'FH' . $file;
  215.     $fh =~ s/[^\w]/_/g;
  216.     $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
  217.     $file = '>' . $file;
  218.     $initfile .= "open($fh, " . "e($file) .
  219.       qq{) || die "Can't open $fh: \$!\\n";\n};
  220.     $initcpio++;
  221.     $flushall = "\n&flushall;\n";
  222.     }
  223.     else {
  224.     die "Unrecognized switch: -$_\n";
  225.     }
  226.     if (@ARGV) {
  227.     if ($ARGV[0] eq '-o') {
  228.         { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
  229.         $statdone = 0 if $indent == 1 && $delayedstat;
  230.         $saw_or++;
  231.         shift;
  232.     }
  233.     else {
  234.         $out .= " &&" unless $ARGV[0] eq ')';
  235.         $out .= "\n";
  236.         shift if $ARGV[0] eq '-a';
  237.     }
  238.     }
  239. }
  240.  
  241. print <<"END";
  242. $startperl
  243.  
  244. eval 'exec perl -S \$0 \${1+"\$@"}'
  245.     if \$running_under_some_shell;
  246.  
  247. END
  248.  
  249. if ($initls) {
  250.     print <<'END';
  251. @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  252. @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
  253.  
  254. END
  255. }
  256.  
  257. if ($inituser || $initls) {
  258.     print 'while (($name, $pw, $uid) = getpwent) {', "\n";
  259.     print '    $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
  260.     print '    $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
  261.     print "}\n\n";
  262. }
  263.  
  264. if ($initgroup || $initls) {
  265.     print 'while (($name, $pw, $gid) = getgrent) {', "\n";
  266.     print '    $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
  267.     print '    $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
  268.     print "}\n\n";
  269. }
  270.  
  271. print $initnewer, "\n" if $initnewer;
  272.  
  273. print $initfile, "\n" if $initfile;
  274.  
  275. $find = $depth ? "finddepth" : "find";
  276. print <<"END";
  277. require "$find.pl";
  278.  
  279. # Traverse desired filesystems
  280.  
  281. &$find($roots);
  282. $flushall
  283. exit;
  284.  
  285. sub wanted {
  286. $out;
  287. }
  288.  
  289. END
  290.  
  291. if ($initexec) {
  292.     print <<'END';
  293. sub exec {
  294.     local($ok, @cmd) = @_;
  295.     foreach $word (@cmd) {
  296.     $word =~ s#{}#$name#g;
  297.     }
  298.     if ($ok) {
  299.     local($old) = select(STDOUT);
  300.     $| = 1;
  301.     print "@cmd";
  302.     select($old);
  303.     return 0 unless <STDIN> =~ /^y/;
  304.     }
  305.     chdir $cwd;        # sigh
  306.     system @cmd;
  307.     chdir $dir;
  308.     return !$?;
  309. }
  310.  
  311. END
  312. }
  313.  
  314. if ($initls) {
  315.     print <<'END';
  316. sub ls {
  317.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
  318.       $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
  319.  
  320.     $pname = $name;
  321.  
  322.     if (defined $blocks) {
  323.     $blocks = int(($blocks + 1) / 2);
  324.     }
  325.     else {
  326.     $blocks = int(($size + 1023) / 1024);
  327.     }
  328.  
  329.     if    (-f _) { $perms = '-'; }
  330.     elsif (-d _) { $perms = 'd'; }
  331.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  332.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  333.     elsif (-p _) { $perms = 'p'; }
  334.     elsif (-S _) { $perms = 's'; }
  335.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  336.  
  337.     $tmpmode = $mode;
  338.     $tmp = $rwx[$tmpmode & 7];
  339.     $tmpmode >>= 3;
  340.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  341.     $tmpmode >>= 3;
  342.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  343.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  344.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  345.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  346.     $perms .= $tmp;
  347.  
  348.     $user = $user{$uid} || $uid;
  349.     $group = $group{$gid} || $gid;
  350.  
  351.     ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  352.     $moname = $moname[$mon];
  353.     if (-M _ > 365.25 / 2) {
  354.     $timeyear = $year + 1900;
  355.     }
  356.     else {
  357.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  358.     }
  359.  
  360.     printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  361.         $ino,
  362.          $blocks,
  363.               $perms,
  364.                 $nlink,
  365.                 $user,
  366.                      $group,
  367.                       $sizemm,
  368.                           $moname,
  369.                          $mday,
  370.                              $timeyear,
  371.                              $pname;
  372.     1;
  373. }
  374.  
  375. sub sizemm {
  376.     sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
  377. }
  378.  
  379. END
  380. }
  381.  
  382. if ($initcpio) {
  383. print <<'END';
  384. sub cpio {
  385.     local($nc,$fh) = @_;
  386.     local($text);
  387.  
  388.     if ($name eq 'TRAILER!!!') {
  389.     $text = '';
  390.     $size = 0;
  391.     }
  392.     else {
  393.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  394.       $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
  395.     if (-f _) {
  396.         open(IN, "./$_\0") || do {
  397.         warn "Couldn't open $name: $!\n";
  398.         return;
  399.         };
  400.     }
  401.     else {
  402.         $text = readlink($_);
  403.         $size = 0 unless defined $text;
  404.     }
  405.     }
  406.  
  407.     ($nm = $name) =~ s#^\./##;
  408.     $nc{$fh} = $nc;
  409.     if ($nc eq 'n') {
  410.     $cpout{$fh} .=
  411.       sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
  412.         070707,
  413.         $dev & 0777777,
  414.         $ino & 0777777,
  415.         $mode & 0777777,
  416.         $uid & 0777777,
  417.         $gid & 0777777,
  418.         $nlink & 0777777,
  419.         $rdev & 0177777,
  420.         $mtime,
  421.         length($nm)+1,
  422.         $size,
  423.         $nm);
  424.     }
  425.     else {
  426.     $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
  427.     $cpout{$fh} .= pack("SSSSSSSSLSLa*",
  428.         070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
  429.         length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
  430.     }
  431.     if ($text ne '') {
  432.     $cpout{$fh} .= $text;
  433.     }
  434.     elsif ($size) {
  435.     &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
  436.     while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
  437.         &flush($fh);
  438.         $l = length($cpout{$fh});
  439.     }
  440.     }
  441.     close IN;
  442. }
  443.  
  444. sub flush {
  445.     local($fh) = @_;
  446.  
  447.     while (length($cpout{$fh}) >= 5120) {
  448.     syswrite($fh,$cpout{$fh},5120);
  449.     ++$blocks{$fh};
  450.     substr($cpout{$fh}, 0, 5120) = '';
  451.     }
  452. }
  453.  
  454. sub flushall {
  455.     $name = 'TRAILER!!!';
  456.     foreach $fh (keys %cpout) {
  457.     &cpio($nc{$fh},$fh);
  458.     $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
  459.     &flush($fh);
  460.     print $blocks{$fh} * 10, " blocks\n";
  461.     }
  462. }
  463.  
  464. END
  465. }
  466.  
  467. if ($inittar) {
  468. print <<'END';
  469. sub tar {
  470.     local($fh) = @_;
  471.     local($linkname,$header,$l,$slop);
  472.     local($linkflag) = "\0";
  473.  
  474.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  475.       $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
  476.     $nm = $name;
  477.     if ($nlink > 1) {
  478.     if ($linkname = $linkseen{$fh,$dev,$ino}) {
  479.         $linkflag = 1;
  480.     }
  481.     else {
  482.         $linkseen{$fh,$dev,$ino} = $nm;
  483.     }
  484.     }
  485.     if (-f _) {
  486.     open(IN, "./$_\0") || do {
  487.         warn "Couldn't open $name: $!\n";
  488.         return;
  489.     };
  490.     $size = 0 if $linkflag ne "\0";
  491.     }
  492.     else {
  493.     $linkname = readlink($_);
  494.     $linkflag = 2 if defined $linkname;
  495.     $nm .= '/' if -d _;
  496.     $size = 0;
  497.     }
  498.  
  499.     $header = pack("a100a8a8a8a12a12a8a1a100",
  500.     $nm,
  501.     sprintf("%6o ", $mode & 0777),
  502.     sprintf("%6o ", $uid & 0777777),
  503.     sprintf("%6o ", $gid & 0777777),
  504.     sprintf("%11o ", $size),
  505.     sprintf("%11o ", $mtime),
  506.     "        ",
  507.     $linkflag,
  508.     $linkname);
  509.     $l = length($header) % 512;
  510.     substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
  511.     substr($header, 154, 1) = "\0";  # blech
  512.     $tarout{$fh} .= $header;
  513.     $tarout{$fh} .= "\0" x (512 - $l) if $l;
  514.     if ($size) {
  515.     &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
  516.     while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
  517.         $slop = length($tarout{$fh}) % 512;
  518.         $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
  519.         &tflush($fh);
  520.         $l = length($tarout{$fh});
  521.     }
  522.     }
  523.     close IN;
  524. }
  525.  
  526. sub tflush {
  527.     local($fh) = @_;
  528.  
  529.     while (length($tarout{$fh}) >= 10240) {
  530.     syswrite($fh,$tarout{$fh},10240);
  531.     ++$blocks{$fh};
  532.     substr($tarout{$fh}, 0, 10240) = '';
  533.     }
  534. }
  535.  
  536. sub tflushall {
  537.     local($len);
  538.  
  539.     foreach $fh (keys %tarout) {
  540.     $len = 10240 - length($tarout{$fh});
  541.     $len += 10240 if $len < 1024;
  542.     $tarout{$fh} .= "\0" x $len;
  543.     &tflush($fh);
  544.     }
  545. }
  546.  
  547. END
  548. }
  549.  
  550. exit;
  551.  
  552. ############################################################################
  553.  
  554. sub tab {
  555.     local($tabstring);
  556.  
  557.     $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
  558.     if (!$statdone) {
  559.     if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
  560.         $delayedstat++;
  561.     }
  562.     else {
  563.         if ($saw_or) {
  564.         $tabstring .= <<'ENDOFSTAT' . $tabstring;
  565. ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  566. ENDOFSTAT
  567.         }
  568.         else {
  569.         $tabstring .= <<'ENDOFSTAT' . $tabstring;
  570. (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  571. ENDOFSTAT
  572.         }
  573.         $statdone = 1;
  574.     }
  575.     }
  576.     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
  577.     $tabstring;
  578. }
  579.  
  580. sub fileglob_to_re {
  581.     local($tmp) = @_;
  582.  
  583.     $tmp =~ s#([./^\$()])#\\$1#g;
  584.     $tmp =~ s/([?*])/.$1/g;
  585.     "^$tmp\$";
  586. }
  587.  
  588. sub n {
  589.     local($n) = @_;
  590.  
  591.     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
  592.     $n =~ s/ 0*(\d)/ $1/;
  593.     $n . ')';
  594. }
  595.  
  596. sub quote {
  597.     local($string) = @_;
  598.     $string =~ s/'/\\'/;
  599.     "'$string'";
  600. }
  601. !NO!SUBS!
  602.  
  603. close OUT or die "Can't close $file: $!";
  604. chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
  605. exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
  606.