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