b99bb498637d44de2c622b876c102176da454202
[p5sagit/p5-mst-13.2.git] / x2p / find2perl.PL
1 #!/usr/local/bin/perl
2
3 use Config;
4 use File::Basename qw(&basename &dirname);
5 use Cwd;
6
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
11 #  $startperl
12 # to ensure Configure will look for $Config{startperl}.
13
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.
16 $origdir = cwd;
17 chdir dirname($0);
18 $file = basename($0, '.PL');
19 $file .= '.com' if $^O eq 'VMS';
20
21 open OUT,">$file" or die "Can't create $file: $!";
22
23 print "Extracting $file (with variable substitutions)\n";
24
25 # In this section, perl variables will be expanded during extraction.
26 # You can use $Config{...} to use Configure variables.
27
28 print OUT <<"!GROK!THIS!";
29 $Config{startperl}
30     eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
31       if \$running_under_some_shell;
32 (my \$perlpath = <<'/../') =~ s/\\s*\\z//;
33 $Config{perlpath}
34 /../
35 !GROK!THIS!
36
37 # In the following, perl variables are not expanded during extraction.
38
39 print OUT <<'!NO!SUBS!';
40 use strict;
41 use vars qw/$statdone/;
42 use File::Spec::Functions 'curdir';
43 my $startperl = "#! $perlpath -w";
44
45 #
46 # Modified September 26, 1993 to provide proper handling of years after 1999
47 #   Tom Link <tml+@pitt.edu>
48 #   University of Pittsburgh
49 #
50 # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
51 #  Billy Constantine <wdconsta@cs.adelaide.edu.au> <billy@smug.adelaide.edu.au>
52 #  University of Adelaide, Adelaide, South Australia
53 #
54 # Modified 1999-06-10, 1999-07-07 to migrate to cleaner perl5 usage
55 #   Ken Pizzini <ken@halcyon.com>
56 #
57 # Modified 2000-01-28 to use the 'follow' option of File::Find
58
59 sub tab ();
60 sub n ($$);
61 sub fileglob_to_re ($);
62 sub quote ($);
63
64 my @roots = ();
65 while ($ARGV[0] =~ /^[^-!(]/) {
66     push(@roots, shift);
67 }
68 @roots = (curdir()) unless @roots;
69 for (@roots) { $_ = quote($_) }
70 my $roots = join(', ', @roots);
71
72 my $find = "find";
73 my $indent_depth = 1;
74 my $stat = 'lstat';
75 my $decl = '';
76 my $flushall = '';
77 my $initfile = '';
78 my $initnewer = '';
79 my $out = '';
80 my $declaresubs = "sub wanted;\n";
81 my %init = ();
82 my ($follow_in_effect,$Skip_And) = (0,0);
83 my $print_needed = 1;
84
85 while (@ARGV) {
86     $_ = shift;
87     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
88     if ($_ eq '(') {
89         $out .= tab . "(\n";
90         $indent_depth++;
91         next;
92     } elsif ($_ eq ')') {
93         --$indent_depth;
94         $out .= tab . ")";
95     } elsif ($_ eq 'follow') {
96         $follow_in_effect= 1;
97         $stat = 'stat';
98         $Skip_And= 1;
99     } elsif ($_ eq '!') {
100         $out .= tab . "!";
101         next;
102     } elsif ($_ eq 'name') {
103         $out .= tab . '/' . fileglob_to_re(shift) . "/s";
104     } elsif ($_ eq 'perm') {
105         my $onum = shift;
106         $onum =~ /^-?[0-7]+$/
107             || die "Malformed -perm argument: $onum\n";
108         $out .= tab;
109         if ($onum =~ s/^-//) {
110             $onum = sprintf("0%o", oct($onum) & 07777);
111             $out .= "((\$mode & $onum) == $onum)";
112         } else {
113             $onum =~ s/^0*/0/;
114             $out .= "((\$mode & 0777) == $onum)";
115         }
116     } elsif ($_ eq 'type') {
117         (my $filetest = shift) =~ tr/s/S/;
118         $out .= tab . "-$filetest _";
119     } elsif ($_ eq 'print') {
120         $out .= tab . 'print("$name\n")';
121         $print_needed = 0;
122     } elsif ($_ eq 'print0') {
123         $out .= tab . 'print("$name\0")';
124         $print_needed = 0;
125     } elsif ($_ eq 'fstype') {
126         my $type = shift;
127         $out .= tab;
128         if ($type eq 'nfs') {
129             $out .= '($dev < 0)';
130         } else {
131             $out .= '($dev >= 0)'; #XXX
132         }
133     } elsif ($_ eq 'user') {
134         my $uname = shift;
135         $out .= tab . "(\$uid == \$uid{'$uname'})";
136         $init{user} = 1;
137     } elsif ($_ eq 'group') {
138         my $gname = shift;
139         $out .= tab . "(\$gid == \$gid{'$gname'})";
140         $init{group} = 1;
141     } elsif ($_ eq 'nouser') {
142         $out .= tab . '!exists $uid{$uid}';
143         $init{user} = 1;
144     } elsif ($_ eq 'nogroup') {
145         $out .= tab . '!exists $gid{$gid}';
146         $init{group} = 1;
147     } elsif ($_ eq 'links') {
148         $out .= tab . n('$nlink', shift);
149     } elsif ($_ eq 'inum') {
150         $out .= tab . n('$ino', shift);
151     } elsif ($_ eq 'size') {
152         $_ = shift;
153         my $n = 'int(((-s _) + 511) / 512)';
154         if (s/c\z//) {
155             $n = 'int(-s _)';
156         } elsif (s/k\z//) {
157             $n = 'int(((-s _) + 1023) / 1024)';
158         }
159         $out .= tab . n($n, $_);
160     } elsif ($_ eq 'atime') {
161         $out .= tab . n('int(-A _)', shift);
162     } elsif ($_ eq 'mtime') {
163         $out .= tab . n('int(-M _)', shift);
164     } elsif ($_ eq 'ctime') {
165         $out .= tab . n('int(-C _)', shift);
166     } elsif ($_ eq 'exec') {
167         my @cmd = ();
168         while (@ARGV && $ARGV[0] ne ';')
169             { push(@cmd, shift) }
170         shift;
171         $out .= tab;
172         if ($cmd[0] =~m#^(?:(?:/usr)?/bin/)?rm$#
173                 && $cmd[$#cmd] eq '{}'
174                 && (@cmd == 2 || (@cmd == 3 && $cmd[1] eq '-f'))) {
175             if (@cmd == 2) {
176                 $out .= '(unlink($_) || warn "$name: $!\n")';
177             } elsif (!@ARGV) {
178                 $out .= 'unlink($_)';
179             } else {
180                 $out .= '(unlink($_) || 1)';
181             }
182         } else {
183             for (@cmd)
184                 { s/'/\\'/g }
185             { local $" = "','"; $out .= "doexec(0, '@cmd')"; }
186             $declaresubs .= "sub doexec (\$\@);\n";
187             $init{doexec} = 1;
188         }
189         $print_needed = 0;
190     } elsif ($_ eq 'ok') {
191         my @cmd = ();
192         while (@ARGV && $ARGV[0] ne ';')
193             { push(@cmd, shift) }
194         shift;
195         $out .= tab;
196         for (@cmd)
197             { s/'/\\'/g }
198         { local $" = "','"; $out .= "doexec(1, '@cmd')"; }
199         $declaresubs .= "sub doexec (\$\@);\n";
200         $init{doexec} = 1;
201         $print_needed = 0;
202     } elsif ($_ eq 'prune') {
203         $out .= tab . '($File::Find::prune = 1)';
204     } elsif ($_ eq 'xdev') {
205         $out .= tab . '!($File::Find::prune |= ($dev != $File::Find::topdev))'
206 ;
207     } elsif ($_ eq 'newer') {
208         my $file = shift;
209         my $newername = 'AGE_OF' . $file;
210         $newername =~ s/\W/_/g;
211         $newername = '$' . $newername;
212         $out .= tab . "(-M _ < $newername)";
213         $initnewer .= "my $newername = -M " . quote($file) . ";\n";
214     } elsif ($_ eq 'eval') {
215         my $prog = shift;
216         $prog =~ s/'/\\'/g;
217         $out .= tab . "eval {$prog}";
218         $print_needed = 0;
219     } elsif ($_ eq 'depth') {
220         $find = 'finddepth';
221         next;
222     } elsif ($_ eq 'ls') {
223         $out .= tab . "ls";
224         $declaresubs .= "sub ls ();\n";
225         $init{ls} = 1;
226         $print_needed = 0;
227     } elsif ($_ eq 'tar') {
228         die "-tar must have a filename argument\n" unless @ARGV;
229         my $file = shift;
230         my $fh = 'FH' . $file;
231         $fh =~ s/\W/_/g;
232         $out .= tab . "tar(*$fh, \$name)";
233         $flushall .= "tflushall;\n";
234         $declaresubs .= "sub tar;\nsub tflushall ();\n";
235         $initfile .= "open($fh, " . quote('> ' . $file) .
236                      qq{) || die "Can't open $fh: \$!\\n";\n};
237         $init{tar} = 1;
238     } elsif (/^(n?)cpio\z/) {
239         die "-$_ must have a filename argument\n" unless @ARGV;
240         my $file = shift;
241         my $fh = 'FH' . $file;
242         $fh =~ s/\W/_/g;
243         $out .= tab . "cpio(*$fh, \$name, '$1')";
244         $find = 'finddepth';
245         $flushall .= "cflushall;\n";
246         $declaresubs .= "sub cpio;\nsub cflushall ();\n";
247         $initfile .= "open($fh, " . quote('> ' . $file) .
248                      qq{) || die "Can't open $fh: \$!\\n";\n};
249         $init{cpio} = 1;
250     } else {
251         die "Unrecognized switch: -$_\n";
252     }
253
254     if (@ARGV) {
255         if ($ARGV[0] eq '-o') {
256             { local($statdone) = 1; $out .= "\n" . tab . "||\n"; }
257             $statdone = 0 if $indent_depth == 1 && exists $init{delayedstat};
258             $init{saw_or} = 1;
259             shift;
260         } else {
261             $out .= " &&" unless $Skip_And || $ARGV[0] eq ')';
262             $out .= "\n";
263             shift if $ARGV[0] eq '-a';
264         }
265     }
266 }
267
268 if ($print_needed) {
269     $out .= "\n" . tab . '&& print("$name\n")';
270 }
271
272
273 print <<"END";
274 $startperl
275     eval 'exec $perlpath -S \$0 \${1+"\$@"}'
276         if 0; #\$running_under_some_shell
277
278 use strict;
279 use File::Find ();
280
281 # Set the variable \$File::Find::dont_use_nlink if you're using AFS,
282 # since AFS cheats.
283
284 # for the convenience of &wanted calls, including -eval statements:
285 use vars qw/*name *dir *prune/;
286 *name   = *File::Find::name;
287 *dir    = *File::Find::dir;
288 *prune  = *File::Find::prune;
289
290 $declaresubs
291
292 END
293
294 if (exists $init{doexec}) {
295     print <<'END';
296 use Cwd ();
297 my $cwd = Cwd::cwd();
298
299 END
300 }  
301
302 if (exists $init{ls}) {
303     print <<'END';
304 my @rwx = qw(--- --x -w- -wx r-- r-x rw- rwx);
305 my @moname = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
306
307 END
308 }
309
310 if (exists $init{user} || exists $init{ls} || exists $init{tar}) {
311     print "my (%uid, %user);\n";
312     print "while (my (\$name, \$pw, \$uid) = getpwent) {\n";
313     print '    $uid{$name} = $uid{$uid} = $uid;', "\n"
314         if exists $init{user};
315     print '    $user{$uid} = $name unless exists $user{$uid};', "\n"
316         if exists $init{ls} || exists $init{tar};
317     print "}\n\n";
318 }
319
320 if (exists $init{group} || exists $init{ls} || exists $init{tar}) {
321     print "my (%gid, %group);\n";
322     print "while (my (\$name, \$pw, \$gid) = getgrent) {\n";
323     print '    $gid{$name} = $gid{$gid} = $gid;', "\n"
324         if exists $init{group};
325     print '    $group{$gid} = $name unless exists $group{$gid};', "\n"
326         if exists $init{ls} || exists $init{tar};
327     print "}\n\n";
328 }
329
330 print $initnewer, "\n" if $initnewer ne '';
331 print $initfile, "\n" if $initfile ne '';
332 $flushall .= "exit;\n";
333 if (exists $init{declarestat}) {
334     $out = <<'END' . $out;
335     my ($dev,$ino,$mode,$nlink,$uid,$gid);
336
337 END
338 }
339
340 if ( $follow_in_effect ) {
341 $out =~ s/lstat\(\$_\)/lstat(_)/;
342 print <<"END";
343 $decl
344 # Traverse desired filesystems
345 File::Find::$find( {wanted => \\&wanted, follow => 1}, $roots);
346 $flushall
347
348 sub wanted {
349 $out;
350 }
351
352 END
353 } else {
354 print <<"END";
355 $decl
356 # Traverse desired filesystems
357 File::Find::$find({wanted => \\&wanted}, $roots);
358 $flushall
359
360 sub wanted {
361 $out;
362 }
363
364 END
365 }
366
367 if (exists $init{doexec}) {
368     print <<'END';
369
370 sub doexec ($@) {
371     my $ok = shift;
372     my @command = @_; # copy so we don't try to s/// aliases to constants
373     for my $word (@command)
374         { $word =~ s#{}#$name#g }
375     if ($ok) {
376         my $old = select(STDOUT);
377         $| = 1;
378         print "@command";
379         select($old);
380         return 0 unless <STDIN> =~ /^y/;
381     }
382     chdir $cwd; #sigh
383     system @command;
384     chdir $File::Find::dir;
385     return !$?;
386 }
387
388 END
389 }
390
391 if (exists $init{ls}) {
392     print <<'INTRO', <<"SUB", <<'END';
393
394 sub sizemm {
395     my $rdev = shift;
396     sprintf("%3d, %3d", ($rdev >> 8) & 0xff, $rdev & 0xff);
397 }
398
399 sub ls () {
400     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
401 INTRO
402         \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
403 SUB
404     my $pname = $name;
405
406     $blocks
407         or $blocks = int(($size + 1023) / 1024);
408
409     my $perms = $rwx[$mode & 7];
410     $mode >>= 3;
411     $perms = $rwx[$mode & 7] . $perms;
412     $mode >>= 3;
413     $perms = $rwx[$mode & 7] . $perms;
414     substr($perms, 2, 1) =~ tr/-x/Ss/ if -u _;
415     substr($perms, 5, 1) =~ tr/-x/Ss/ if -g _;
416     substr($perms, 8, 1) =~ tr/-x/Tt/ if -k _;
417     if    (-f _) { $perms = '-' . $perms; }
418     elsif (-d _) { $perms = 'd' . $perms; }
419     elsif (-l _) { $perms = 'l' . $perms; $pname .= ' -> ' . readlink($_); }
420     elsif (-c _) { $perms = 'c' . $perms; $size = sizemm($rdev); }
421     elsif (-b _) { $perms = 'b' . $perms; $size = sizemm($rdev); }
422     elsif (-p _) { $perms = 'p' . $perms; }
423     elsif (-S _) { $perms = 's' . $perms; }
424     else         { $perms = '?' . $perms; }
425
426     my $user = $user{$uid} || $uid;
427     my $group = $group{$gid} || $gid;
428
429     my ($sec,$min,$hour,$mday,$mon,$timeyear) = localtime($mtime);
430     if (-M _ > 365.25 / 2) {
431         $timeyear += 1900;
432     } else {
433         $timeyear = sprintf("%02d:%02d", $hour, $min);
434     }
435
436     printf "%5lu %4ld %-10s %3d %-8s %-8s %8s %s %2d %5s %s\n",
437             $ino,
438                  $blocks,
439                       $perms,
440                             $nlink,
441                                 $user,
442                                      $group,
443                                           $size,
444                                               $moname[$mon],
445                                                  $mday,
446                                                      $timeyear,
447                                                          $pname;
448     1;
449 }
450
451 END
452 }
453
454
455 if (exists $init{cpio} || exists $init{tar}) {
456 print <<'END';
457
458 my %blocks = ();
459
460 sub flush {
461     my ($fh, $varref, $blksz) = @_;
462
463     while (length($$varref) >= $blksz) {
464         no strict qw/refs/;
465         syswrite($fh, $$varref, $blksz);
466         substr($$varref, 0, $blksz) = '';
467         ++$blocks{$fh};
468     }
469 }
470
471 END
472 }
473
474
475 if (exists $init{cpio}) {
476     print <<'INTRO', <<"SUB", <<'END';
477
478 my %cpout = ();
479 my %nc = ();
480
481 sub cpio {
482     my ($fh, $fname, $nc) = @_;
483     my $text = '';
484     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
485         $atime,$mtime,$ctime,$blksize,$blocks);
486     local (*IN);
487
488     if ( ! defined $fname ) {
489         $fname = 'TRAILER!!!';
490         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
491           $atime,$mtime,$ctime,$blksize,$blocks) = (0) x 13;
492     } else {
493         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
494 INTRO
495           \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
496 SUB
497         if (-f _) {
498             open(IN, "./$_\0") || do {
499                 warn "Couldn't open $fname: $!\n";
500                 return;
501             }
502         } else {
503             $text = readlink($_);
504             $size = 0 unless defined $text;
505         }
506     }
507
508     $fname =~ s#^\./##;
509     $nc{$fh} = $nc;
510     if ($nc eq 'n') {
511         $cpout{$fh} .=
512           sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
513             070707,
514             $dev & 0777777,
515             $ino & 0777777,
516             $mode & 0777777,
517             $uid & 0777777,
518             $gid & 0777777,
519             $nlink & 0777777,
520             $rdev & 0177777,
521             $mtime,
522             length($fname)+1,
523             $size,
524             $fname);
525     } else {
526         $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
527         $cpout{$fh} .= pack("SSSSSSSSLSLa*",
528             070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
529             length($fname)+1, $size,
530             $fname . (length($fname) & 1 ? "\0" : "\0\0"));
531     }
532
533     if ($text ne '') {
534         $cpout{$fh} .= $text;
535     } elsif ($size) {
536         my $l;
537         flush($fh, \$cpout{$fh}, 5120)
538             while ($l = length($cpout{$fh})) >= 5120;
539         while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
540             flush($fh, \$cpout{$fh}, 5120);
541             $l = length($cpout{$fh});
542         }
543         close IN;
544     }
545 }
546
547 sub cflushall () {
548     for my $fh (keys %cpout) {
549         cpio($fh, undef, $nc{$fh});
550         $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
551         flush($fh, \$cpout{$fh}, 5120);
552         print $blocks{$fh} * 10, " blocks\n";
553     }
554 }
555
556 END
557 }
558
559 if (exists $init{tar}) {
560     print <<'INTRO', <<"SUB", <<'END';
561
562 my %tarout = ();
563 my %linkseen = ();
564
565 sub tar {
566     my ($fh, $fname) = @_;
567     my $prefix = '';
568     my $typeflag = '0';
569     my $linkname;
570     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
571 INTRO
572         \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat(_);
573 SUB
574     local (*IN);
575
576     if ($nlink > 1) {
577         if ($linkname = $linkseen{$fh, $dev, $ino}) {
578             if (length($linkname) > 100) {
579                 warn "$0: omitting file with linkname ",
580                      "too long for tar output: $linkname\n";
581                 return;
582             }
583             $typeflag = '1';
584             $size = 0;
585         } else {
586             $linkseen{$fh, $dev, $ino} = $fname;
587         }
588     }
589     if ($typeflag eq '0') {
590         if (-f _) {
591             open(IN, "./$_\0") || do {
592                 warn "Couldn't open $fname: $!\n";
593                 return;
594             }
595         } else {
596             $linkname = readlink($_);
597             if (defined $linkname) { $typeflag = '2' }
598             elsif (-c _) { $typeflag = '3' }
599             elsif (-b _) { $typeflag = '4' }
600             elsif (-d _) { $typeflag = '5' }
601             elsif (-p _) { $typeflag = '6' }
602         }
603     }
604
605     if (length($fname) > 100) {
606         ($prefix, $fname) = ($fname =~ m#\A(.*?)/(.{,100})\Z(?!\n)#);
607         if (!defined($fname) || length($prefix) > 155) {
608             warn "$0: omitting file with name too long for tar output: ",
609                  $fname, "\n";
610             return;
611         }
612     }
613
614     $size = 0 if $typeflag ne '0';
615     my $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155",
616                         $fname,
617                         sprintf("%7o ", $mode &    0777),
618                         sprintf("%7o ", $uid  & 0777777),
619                         sprintf("%7o ", $gid  & 0777777),
620                         sprintf("%11o ", $size),
621                         sprintf("%11o ", $mtime),
622                         ' 'x8,
623                         $typeflag,
624                         defined $linkname ? $linkname : '',
625                         "ustar\0",
626                         "00",
627                         $user{$uid},
628                         $group{$gid},
629                         ($rdev >> 8) & 0xff,
630                         $rdev & 0xff,
631                         $prefix,
632                      );
633     substr($header, 148, 8) = sprintf("%7o ", unpack("%16C*", $header));
634     my $l = length($header) % 512;
635     $tarout{$fh} .= $header;
636     $tarout{$fh} .= "\0" x (512 - $l) if $l;
637
638     if ($size) {
639         flush($fh, \$tarout{$fh}, 10240)
640             while ($l = length($tarout{$fh})) >= 10240;
641         while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
642             my $slop = length($tarout{$fh}) % 512;
643             $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
644             flush($fh, \$tarout{$fh}, 10240);
645             $l = length($tarout{$fh});
646         }
647         close IN;
648     }
649 }
650
651 sub tflushall () {
652     my $len;
653     for my $fh (keys %tarout) {
654         $len = 10240 - length($tarout{$fh});
655         $len += 10240 if $len < 1024;
656         $tarout{$fh} .= "\0" x $len;
657         flush($fh, \$tarout{$fh}, 10240);
658     }
659 }
660
661 END
662 }
663
664 exit;
665
666 ############################################################################
667
668 sub tab () {
669     my $tabstring;
670
671     $tabstring = "\t" x ($indent_depth/2) . ' ' x ($indent_depth%2 * 4);
672     if (!$statdone) {
673         if ($_ =~ /^(?:name|print|prune|exec|ok|\(|\))/) {
674             $init{delayedstat} = 1;
675         } else {
676             my $statcall = '(($dev,$ino,$mode,$nlink,$uid,$gid) = '
677                          . $stat . '($_))';
678             if (exists $init{saw_or}) {
679                 $tabstring .= "(\$nlink || $statcall) &&\n" . $tabstring;
680             } else {
681                 $tabstring .= "$statcall &&\n" . $tabstring;
682             }
683             $statdone = 1;
684             $init{declarestat} = 1;
685         }
686     }
687     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
688     $tabstring;
689 }
690
691 sub fileglob_to_re ($) {
692     my $x = shift;
693     $x =~ s#([./^\$()+])#\\$1#g;
694     $x =~ s#([?*])#.$1#g;
695     "^$x\\z";
696 }
697
698 sub n ($$) {
699     my ($pre, $n) = @_;
700     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
701     $n =~ s/ 0*(\d)/ $1/;
702     "($pre $n)";
703 }
704
705 sub quote ($) {
706     my $string = shift;
707     $string =~ s/\\/\\\\/g;
708     $string =~ s/'/\\'/g;
709     "'$string'";
710 }
711
712 __END__
713
714 =head1 NAME
715
716 find2perl - translate find command lines to Perl code
717
718 =head1 SYNOPSIS
719
720         find2perl [paths] [predicates] | perl
721
722 =head1 DESCRIPTION
723
724 find2perl is a little translator to convert find command lines to
725 equivalent Perl code.  The resulting code is typically faster than
726 running find itself.
727
728 "paths" are a set of paths where find2perl will start its searches and
729 "predicates" are taken from the following list.
730
731 =over 4
732
733 =item C<! PREDICATE>
734
735 Negate the sense of the following predicate.  The C<!> must be passed as
736 a distinct argument, so it may need to be surrounded by whitespace and/or
737 quoted from interpretation by the shell using a backslash (just as with
738 using C<find(1)>).
739
740 =item C<( PREDICATES )>
741
742 Group the given PREDICATES.  The parentheses must be passed as distinct
743 arguments, so they may need to be surrounded by whitespace and/or
744 quoted from interpretation by the shell using a backslash (just as with
745 using C<find(1)>).
746
747 =item C<PREDICATE1 PREDICATE2>
748
749 True if _both_ PREDICATE1 and PREDICATE2 are true; PREDICATE2 is not
750 evaluated if PREDICATE1 is false.
751
752 =item C<PREDICATE1 -o PREDICATE2>
753
754 True if either one of PREDICATE1 or PREDICATE2 is true; PREDICATE2 is
755 not evaluated if PREDICATE1 is true.
756
757 =item C<-follow>
758
759 Follow (dereference) symlinks.  The checking of file attributes depends
760 on the position of the C<-follow> option. If it precedes the file
761 check option, an C<stat> is done which means the file check applies to the
762 file the symbolic link is pointing to. If C<-follow> option follows the
763 file check option, this now applies to the symbolic link itself, i.e.
764 an C<lstat> is done.
765
766 =item C<-depth>
767
768 Change directory traversal algorithm from breadth-first to depth-first.
769
770 =item C<-prune>
771
772 Do not descend into the directory currently matched.
773
774 =item C<-xdev>
775
776 Do not traverse mount points (prunes search at mount-point directories).
777
778 =item C<-name GLOB>
779
780 File name matches specified GLOB wildcard pattern.  GLOB may need to be
781 quoted to avoid interpretation by the shell (just as with using
782 C<find(1)>).
783
784 =item C<-perm PERM>
785
786 Low-order 9 bits of permission match octal value PERM.
787
788 =item C<-perm -PERM>
789
790 The bits specified in PERM are all set in file's permissions.
791
792 =item C<-type X>
793
794 The file's type matches perl's C<-X> operator.
795
796 =item C<-fstype TYPE>
797
798 Filesystem of current path is of type TYPE (only NFS/non-NFS distinction
799 is implemented).
800
801 =item C<-user USER>
802
803 True if USER is owner of file.
804
805 =item C<-group GROUP>
806
807 True if file's group is GROUP.
808
809 =item C<-nouser>
810
811 True if file's owner is not in password database.
812
813 =item C<-nogroup>
814
815 True if file's group is not in group database.
816
817 =item C<-inum INUM>
818
819 True file's inode number is INUM.
820
821 =item C<-links N>
822
823 True if (hard) link count of file matches N (see below).
824
825 =item C<-size N>
826
827 True if file's size matches N (see below) N is normally counted in
828 512-byte blocks, but a suffix of "c" specifies that size should be
829 counted in characters (bytes) and a suffix of "k" specifes that
830 size should be counted in 1024-byte blocks.
831
832 =item C<-atime N>
833
834 True if last-access time of file matches N (measured in days) (see
835 below).
836
837 =item C<-ctime N>
838
839 True if last-changed time of file's inode matches N (measured in days,
840 see below).
841
842 =item C<-mtime N>
843
844 True if last-modified time of file matches N (measured in days, see below).
845
846 =item C<-newer FILE>
847
848 True if last-modified time of file matches N.
849
850 =item C<-print>
851
852 Print out path of file (always true). If none of C<-exec>, C<-ls>,
853 C<-print0>, or C<-ok> is specified, then C<-print> will be added
854 implicitly.
855
856 =item C<-print0>
857
858 Like -print, but terminates with \0 instead of \n.
859
860 =item C<-exec OPTIONS ;>
861
862 exec() the arguments in OPTIONS in a subprocess; any occurrence of {} in
863 OPTIONS will first be substituted with the path of the current
864 file.  Note that the command "rm" has been special-cased to use perl's
865 unlink() function instead (as an optimization).  The C<;> must be passed as
866 a distinct argument, so it may need to be surrounded by whitespace and/or
867 quoted from interpretation by the shell using a backslash (just as with
868 using C<find(1)>).
869
870 =item C<-ok OPTIONS ;>
871
872 Like -exec, but first prompts user; if user's response does not begin
873 with a y, skip the exec.  The C<;> must be passed as
874 a distinct argument, so it may need to be surrounded by whitespace and/or
875 quoted from interpretation by the shell using a backslash (just as with
876 using C<find(1)>).
877
878 =item C<-eval EXPR>
879
880 Has the perl script eval() the EXPR.  
881
882 =item C<-ls>
883
884 Simulates C<-exec ls -dils {} ;>
885
886 =item C<-tar FILE>
887
888 Adds current output to tar-format FILE.
889
890 =item C<-cpio FILE>
891
892 Adds current output to old-style cpio-format FILE.
893
894 =item C<-ncpio FILE>
895
896 Adds current output to "new"-style cpio-format FILE.
897
898 =back
899
900 Predicates which take a numeric argument N can come in three forms:
901
902    * N is prefixed with a +: match values greater than N
903    * N is prefixed with a -: match values less than N
904    * N is not prefixed with either + or -: match only values equal to N
905
906 =head1 SEE ALSO
907
908 find
909
910 =cut
911 !NO!SUBS!
912
913 close OUT or die "Can't close $file: $!";
914 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
915 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
916 chdir $origdir;