+sub processpcfgz ($$$$) {
+ my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
+ print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
+ my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
+ my ($usread,$uswrite);
+ my ($reader,$writer);
+ my @children;
+ my %ch;
+ foreach my $proc (['gunzip'], ['pcf2bdf'], [],
+ ['bdftopcf'],['',qw(gzip -1 -n)]) {
+ my $isfinal = (@$proc && $proc->[0] eq '');
+ if (!$isfinal) {
+ $reader = new IO::Handle or die $!;
+ $writer = new IO::Handle or die $!;
+ new IO::Pipe($reader,$writer) or die $!;
+ } else {
+ shift @$proc;
+ $reader = undef;
+ $writer = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
+ }
+ if (@$proc) {
+ my $exe = $proc->[0];
+ my $child = fork; defined $child or die $!;
+ if (!$child) {
+ open STDIN, '<&', $current or die $!;
+ open STDOUT, '>&', $writer or die $!;
+ if (!$isfinal) {
+ close $reader or die $!;
+ }
+ close $usread or die $! if $usread;
+ close $uswrite or die $! if $uswrite;
+ exec $exe @$proc or die "$exe $!";
+ }
+ my $ch = {
+ Pid => $child,
+ Exe => $exe,
+ Stage => (!$exe ? 'self' : defined $usread ? 'out' : 'in'),
+ SigOK => { },
+ };
+ push @children, $ch;
+ $ch{$exe} = $ch;
+ close $current or die $!;
+ close $writer or die $!;
+ $current = $reader;
+ } else {
+ $usread = $current;
+ $uswrite = $writer;
+ $current = $reader;
+ }
+ }
+ my $r = processbdf($usread,$uswrite,$logfile,$what);
+ my $none = $r !~ m/^\d/;
+
+ $ch{'gunzip'}{SigOK}{13} = 1;
+ # ... we never care if pcf2bdf didn't want all the output from gunzip
+
+ if ($none || !$r) {
+ # We're not going to install or use this so we can kill our
+ # input and output filters. We kill the input filters so that
+ # we don't risk waiting for them. (If the input filter died
+ # for some other reason then sending it a KILL now won't
+ # affect its exit status.) We kill the output filters (before
+ # we close the output pipe) so we don't produce messages from
+ # our output filters about corrupted data.
+ flush $uswrite or die $!;
+
+ foreach my $ch (@children) {
+ if ($ch->{Stage} ne 'self') {
+ kill 9, $ch->{Pid} or die "$ch->{Pid} $ch->{Exe} $!";
+ $ch->{SigOK}{9} = 1;
+ }
+ }
+ $ch{'pcf2bdf'}{SigOK}{13} = 1;
+ # ... we might not have read all the output from pcf2bdf, which is OK
+ }
+ close $uswrite or die $!;
+ close $usread or die $!;
+
+ foreach my $ch (@children) {
+ $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
+ die "$ch->{Pid} $ch->{Exe} $!";
+ $ch->{St} = $?;
+ }
+
+ my $st_isok = sub {
+ my ($ch) = @_;
+ my $st = $ch->{St};
+ return !$st || $ch->{SigOK}{($st & ~128)};
+ };
+
+ if ($tolerate_bad_fonts &&
+ $r eq 'no bdf data' &&
+ $st_isok->($ch{'gunzip'}) &&
+ ($ch{'pcf2bdf'}{St} & ~128) == 6)
+ {
+ $r = "pcf2bdf failed ($ch{'pcf2bdf'}{St})";
+ print STDERR "warning: $r: skipping $inpcfgz\n";
+ $ch{'pcf2bdf'}{SigOK}{6} = 1;
+ }
+ foreach my $ch (@children) {
+ if (!$st_isok->($ch)) {
+ die "update-xfonts-traditional:".
+ " $ch->{Exe} [$ch->{Pid}] for $inpcfgz".
+ " failed $ch->{St}".
+ " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
+ }
+ }
+ return $r;
+}
+
+sub processfontdir ($) {
+ my ($fontdir) = @_;
+ if (!opendir FD, $fontdir) {
+ die "$fontdir $!" unless $!==&ENOENT;
+ return;
+ }
+ my $changed = 0;
+ my $olddone;
+ if (!$always_reprocess) {
+ $olddone = do "$fontdir/$donefile";
+ if (!$olddone) {
+ die "$fontdir $! $@ " unless $!==&ENOENT;
+ } elsif ($olddone->{''} ne $foundryinfo) {
+ our $repro_reported;
+ print $reportfh "reprocessing fonts (rules updated)\n" or die $!
+ unless $repro_reported++;
+ $olddone = undef;
+ }
+ }
+ if (!$olddone) {
+ $olddone = { };
+ $changed = 1;
+ }
+ my $newdone = { '' => $foundryinfo };
+ my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
+ my $updated=0;
+ my $reported=0;
+ my $anypcfs=0;
+
+ my $logpath = "$fontdir/$logfile";
+ unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
+ my $log = new IO::File $logpath, ">>" or die "$logpath $!";
+
+ if (!$wanted_parallel) {
+ $wanted_parallel = Sys::CPU::cpu_count();
+ printf $reportfh "parallelism: %d\n", $wanted_parallel if $verbose>=2;
+ }
+ $wanted_parallel = 1 if $wanted_parallel < 1;
+
+ our %inprogress;
+
+ my $await = sub {
+ my $child = wait;
+ die $! unless defined $child;
+ my $job = $inprogress{$child};
+ die $child unless $job;
+
+ my $dent = $job->{Dent};
+ my $outdent = $job->{Outdent};
+ my $stats = $job->{Stats};
+ if ($?==0) {
+ $updated++;
+ $outfiles{$outdent} |= 3;
+ } elsif ($?==2*256) {
+ } else {
+ die "update-xfonts-traditional: processing of".
+ " $fontdir/$dent [$child] failed ($?)\n";
+ }
+ $newdone->{$dent} = $stats;
+ $changed = 1;
+ delete $inprogress{$child};
+ };
+
+ flush $reportfh or die $!;
+ while (my $dent = scalar readdir FD) {
+ if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
+ unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
+ next;
+ }
+ next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
+ print $reportfh "processing $fontdir...\n" or die $!
+ unless $reported++;
+ if ($dent =~ m/^\Q$fontprefix/) {
+ $outfiles{$dent} |= 1;
+ next;
+ }
+ if (!stat "$fontdir/$dent") {
+ die "$fontdir $dent $!" unless $!==&ENOENT;
+ next;
+ }
+ die "$fontdir $dent" unless -f _;
+ $anypcfs++;
+
+ my $stats = statsummary();
+ my $tdone = $olddone->{$dent};
+ my $outdent = $fontprefix.$dent;
+ if (defined $tdone && $tdone eq $stats) {
+ $outfiles{$outdent} |= 2;
+ $newdone->{$dent} = $stats;
+ next;
+ }
+
+ $await->() while scalar keys %inprogress >= $wanted_parallel;
+
+ my $child = fork; die unless defined $child;
+ if (!$child) {
+ my $r = processpcfgz("$fontdir/$dent",
+ "$fontdir/$outdent.new",
+ $log, $dent);
+ my $rc;
+ if ($r !~ m/^\d/) {
+ printf $log "%s: unchanged - %s\n", $dent, $r;
+ unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
+ $rc = 2;
+ } else {
+ rename "$fontdir/$outdent.new", "$fontdir/$outdent"
+ or die "$fontdir $outdent $!";
+ $rc = 0;
+ }
+ $log->flush or die "$logpath $!";
+ exit $rc;
+ }
+ $inprogress{$child} = {
+ Dent => $dent,
+ Outdent => $outdent,
+ Stats => $stats,
+ };
+ }
+ $await->() while scalar keys %inprogress;
+
+ my $affected=0;
+ foreach my $olddent (keys %outfiles) {
+ my $state = $outfiles{$olddent};
+ if ($state & 2) {
+ $affected++ if $state & 1;
+ next;
+ }
+ unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
+ $changed = 1;
+ $updated++;
+ }
+ if (!stat "$fontdir/fonts.dir") {
+ $!==&ENOENT or die "$fontdir $!";
+ } else {
+ $!=0; $?=0; system 'mkfontdir',$fontdir;
+ die "$fontdir $? $!" if $? or $!;
+ }
+ if (!$anypcfs) {
+ unlink "$logpath" or die "$fontdir $!";
+ unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
+ } elsif ($changed) {
+ my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
+ or die "$fontdir $!";
+ print $newdoneh Dumper($newdone) or die "$fontdir $!";
+ close $newdoneh or die "$fontdir $!";
+ rename "$fontdir/$donefile.new","$fontdir/$donefile"
+ or die "$fontdir $!";
+ }
+ if ($reported || %$newdone || $affected || $updated) {
+ printf " processed %s: %d pcfs, %d affected, %d updated.\n",
+ $fontdir, (scalar keys %$newdone), $affected, $updated;
+ }
+}
+
+sub processfonttree ($) {
+ my ($tree) = @_;
+ find({ follow => 1,
+ dangling_symlinks => 0,
+ no_chdir => 1,
+ wanted => sub {
+ return unless -d _;
+ processfontdir($File::Find::name);
+ }},
+ $tree);
+}
+