chiark / gitweb /
update-xfonts-traditional: Rename log file handle variables to $logfh
[xfonts-traditional.git] / update-xfonts-traditional
index 619e2faca737ba00378e20d854de4a11d050a679..f0961774867f4747653f865eca0e2b24cd3aaac0 100755 (executable)
@@ -23,6 +23,8 @@ our $verbose=0;
 our $reportfh;
 our $foundryinfo;
 our %props;
+our $tolerate_bad_fonts=1;
+our $always_reprocess;
 our $wanted_parallel;
 
 sub reportloaded {
@@ -53,13 +55,15 @@ sub loadrules ($) {
 }
 
 sub processbdf ($$$$) {
-    my ($inbdf,$outbdf,$logfile,$what) = @_;
+    my ($inbdf,$outbdf,$logfh,$what) = @_;
     my $state='idle';
     my ($foundry,$font);
     my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
     my $modified=0;
     %props = ();
+    my $anyinput=0;
     while (<$inbdf>) {
+       $anyinput=1;
        if ($state eq 'bitmap' && $y==$h) {
            $glyph = uc $glyph;
            $glyph =~ s/\;$//;
@@ -124,11 +128,13 @@ sub processbdf ($$$$) {
     die $! if $inbdf->error;
     die $! if $outbdf->error or !$outbdf->flush;
     die unless $state eq 'idle';
+    return 'no bdf data' # also special cased in processpcfgz
+       if !$anyinput;
     if ($modified) {
-       printf $logfile "%s: %d glyphs changed\n", $what, $modified
+       printf $logfh "%s: %d glyphs changed\n", $what, $modified
            or die $!;
     } else {
-       printf $logfile "%s: unchanged - no rules matched\n", $what
+       printf $logfh "%s: unchanged - no rules matched\n", $what
            or die $!;
     }
     return $modified;
@@ -154,7 +160,7 @@ sub loadfoundries () {
        while (<$f>) {
            s/^\s*//; s/\s+$//;
            next if m/^\#/;
-           m/^(\w+)\s+(\w+)$/ or die;
+           m/^([.0-9A-Za-z]+)\s+([.0-9A-Za-z]+)$/ or die;
            my $k = lc $1;
            next if exists $foundrymap{$k};
            $foundrymap{$k}=$2;
@@ -165,13 +171,20 @@ sub loadfoundries () {
     die "no foundry maps\n" unless %foundrymap;
 }
 
+sub filter_st_isok ($) {
+    my ($ch) = @_;
+    my $st = $ch->{St};
+    return !$st || $ch->{SigOK}{($st & ~128)};
+}
+
 sub processpcfgz ($$$$) {
-    my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
+    my ($inpcfgz,$outpcfgz,$logfh,$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 '');
@@ -197,7 +210,14 @@ sub processpcfgz ($$$$) {
                close $uswrite or die $! if $uswrite;
                exec $exe @$proc or die "$exe $!";
            }
-           push @children, [ $child, $exe, defined $usread ];
+           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;
@@ -207,27 +227,56 @@ sub processpcfgz ($$$$) {
            $current = $reader;
        }
     }
-    my $r = processbdf($usread,$uswrite,$logfile,$what);
+    my $r = processbdf($usread,$uswrite,$logfh,$what);
     my $none = $r !~ m/^\d/;
-    if ($none) {
+
+    $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 $!;
-    } else {
-       close $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 $chinfo (@children) {
-       my ($child,$exe,$isoutput)=@$chinfo;
-       my $sigok = 0;
-       if ($none) {
-           if ($isoutput) {
-               $sigok = 9;
-               kill 9, $child or die "$child $!";
-           } else {
-               $sigok = 13;
-           }
+
+    foreach my $ch (@children) {
+       $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
+           die "$ch->{Pid} $ch->{Exe} $!";
+       $ch->{St} = $?;
+    }
+
+    if ($tolerate_bad_fonts &&
+       $r eq 'no bdf data' &&
+       filter_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 (!filter_st_isok($ch)) {
+           die "update-xfonts-traditional:".
+               " $ch->{Exe} [$ch->{Pid}] for $inpcfgz".
+               " failed $ch->{St}".
+               " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
        }
-       $!=0; waitpid($child, 0) == $child or die "$child $!";
-       !$? or ($?&~128)==$sigok or die "$exe [$child] $sigok $?";
     }
     return $r;
 }
@@ -239,14 +288,17 @@ sub processfontdir ($) {
        return;
     }
     my $changed = 0;
-    my $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;
+    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 = { };
@@ -260,7 +312,7 @@ sub processfontdir ($) {
 
     my $logpath = "$fontdir/$logfile";
     unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
-    my $log = new IO::File $logpath, ">>" or die "$logpath $!";
+    my $logfh = new IO::File $logpath, ">>" or die "$logpath $!";
 
     if (!$wanted_parallel) {
        $wanted_parallel = Sys::CPU::cpu_count();
@@ -284,7 +336,8 @@ sub processfontdir ($) {
            $outfiles{$outdent} |= 3;
        } elsif ($?==2*256) {
        } else {
-           die "[$child] $dent $outdent $?";
+           die "update-xfonts-traditional: processing of".
+               " $fontdir/$dent [$child] failed ($?)\n";
        }
        $newdone->{$dent} = $stats;
        $changed = 1;
@@ -326,10 +379,10 @@ sub processfontdir ($) {
        if (!$child) {
            my $r = processpcfgz("$fontdir/$dent",
                                 "$fontdir/$outdent.new",
-                                $log, $dent);
+                                $logfh, $dent);
            my $rc;
            if ($r !~ m/^\d/) {
-               printf $log "%s: unchanged - %s\n", $dent, $r;
+               printf $logfh "%s: unchanged - %s\n", $dent, $r;
                unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
                $rc = 2;
            } else {
@@ -337,7 +390,7 @@ sub processfontdir ($) {
                    or die "$fontdir $outdent $!";
                $rc = 0;
            }
-           $log->flush or die "$logpath $!";
+           $logfh->flush or die "$logpath $!";
            exit $rc;
        }
        $inprogress{$child} = {
@@ -404,6 +457,8 @@ our (@options)=(
     'share-dir=s' => \$sharedir,
     'verbose|v+' => \$verbose,
     'j|parallel=i' => \$wanted_parallel,
+    'always-reprocess!' => \$always_reprocess,
+    'tolerate-bad-fonts!' => \$tolerate_bad_fonts,
     );
 
 sub define_mode ($$) {