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