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