chiark / gitweb /
update-xfonts-traditional: Break out filter_st_isok (no functional change)
[xfonts-traditional.git] / update-xfonts-traditional
index 9d05e3a2fa466d983c83cdcafc5f1380428e0e9e..a2e81e0e3fa5ebaca61a606c68eec8f723f4a7d2 100755 (executable)
@@ -6,11 +6,13 @@ use Getopt::Long;
 use File::Glob qw(:glob);
 use Data::Dumper;
 use IO::Pipe;
+use File::Find;
+use Sys::CPU;
 
 our $prefix="/usr/local";
 our $package='xfonts-traditional';
 our $sharedir="$prefix/share/$package";
-our @fontparentdirs=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
+our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
 our $donefile="$package.done";
 our $logfile="$package.log";
 our $fontprefix="trad--";
@@ -19,12 +21,21 @@ our $mode;
 our %foundrymap;
 our $verbose=0;
 our $reportfh;
+our $foundryinfo;
+our %props;
+our $tolerate_bad_fonts=1;
+our $always_reprocess;
+our $wanted_parallel;
 
 sub reportloaded {
     return unless $verbose;
     print $reportfh @_,"\n" or die $!;
 }
 
+sub statsummary () {
+    return join ' ', ((stat _)[1,7,9,10]);
+}
+
 sub loadrules ($) {
     my ($key) = @_;
     our %cache;
@@ -49,14 +60,17 @@ sub processbdf ($$$$) {
     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/\;$//;
            local ($_) = $glyph;
            my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
            my $rules= loadrules($key);
-           return (0,'no rules') if !$rules;
+           return 'no rules' if !$rules;
            $rules->();
            $modified += ($_ ne $glyph);
            print $outbdf $_,"\n" or die $!
@@ -72,25 +86,29 @@ sub processbdf ($$$$) {
        }
        if ($state eq 'idle' && m/^FOUNDRY\s+/) {
            die if defined $foundry;
-           return (0,'foundry syntax') unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
+           return 'foundry syntax' unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
            $foundry = $foundrymap{lc $1};
-           return (0,'no foundry') unless defined $foundry;
+           return 'no foundry' unless defined $foundry;
            $_ = "FOUNDRY \"$foundry\"\n";
        }
        if ($state eq 'idle' && m/^FONT\s+/) {
            die if defined $font;
-           return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
+           return 'simple font name' unless m/^(FONT\s+)\-(\w+)\-/;
            $font = $foundrymap{lc $2};
-           return (0,'no foundry') unless defined $font;
+           return 'no foundry' unless defined $font;
            $_ = "FONT -$font-$'";
        }
        if ($state eq 'idle' && m/^STARTCHAR\s/) {
            die unless defined $foundry;
            die unless defined $font;
-           return (0,'foundry != font') unless $foundry eq $font;
+           return 'foundry != font' unless $foundry eq $font;
            $state='startchar';
            $w=undef;
        }
+       if (($state eq 'idle' || $state eq 'startchar') &&
+           m/^([A-Z_]+)\s+(.*\S)\s+$/) {
+           $props{$1}=$2;
+       }
        if ($state eq 'startchar') {
            if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
                ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
@@ -100,6 +118,9 @@ sub processbdf ($$$$) {
                $y=0;
                $glyph='';
                $state='bitmap';
+               $props{' 7bit'}=
+                   ($props{'CHARSET_REGISTRY'} =~ m/iso8859|utf|iso10646/i &&
+                    $props{'ENCODING'} <= 127);
            }
        }
        print $outbdf $_ or die $!;
@@ -107,6 +128,8 @@ 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
            or die $!;
@@ -117,22 +140,15 @@ sub processbdf ($$$$) {
     return $modified;
 }
 
-our (@options)=(
-    'R|rules-include=s@' => \@rulespath,
-    'share-dir=s' => \$sharedir,
-    'verbose|v+' => \$verbose,
-    );
-
-sub define_mode ($$) {
-    my ($optname,$f) = @_;
-    push @options, $optname, sub {
-       die "only one mode may be specified\n" if defined $mode;
-       $mode=$f;
-    };
-}
-
 sub loadfoundries () {
+    $foundryinfo = '';
     foreach my $path (@rulespath) {
+       if (!stat $path) {
+           die "$path $!" unless $!==&ENOENT;
+           next;
+       }
+       $foundryinfo .= statsummary().' '.$path."\0\n";
+
        my $p = "$path/foundries";
        my $f = new IO::File $p;
        if (!$f) {
@@ -140,10 +156,11 @@ sub loadfoundries () {
            print $reportfh "foundries: none in $p\n" or die $! if $verbose;
            next;
        }
+       stat $f or die $!;
        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;
@@ -154,15 +171,22 @@ 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) = @_;
-
+    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;
-    foreach my $proc (['zcat'], ['pcf2bdf'], [],
-                     ['bdftopcf'],['',qw(gzip -1)]) {
+    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 $!;
@@ -186,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;
@@ -198,25 +229,54 @@ sub processpcfgz ($$$$) {
     }
     my $r = processbdf($usread,$uswrite,$logfile,$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;
 }
@@ -227,19 +287,63 @@ sub processfontdir ($) {
        die "$fontdir $!" unless $!==&ENOENT;
        return;
     }
-    my $olddone = do "$fontdir/$donefile";
+    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) {
-       die "$fontdir $! $@ " unless $!==&ENOENT;
        $olddone = { };
+       $changed = 1;
     }
-    my $newdone = { };
-    my $log = new IO::File "$fontdir/$logfile", "w" 
-       or die "$fontdir/$logfile $!";
+    my $newdone = { '' => $foundryinfo };
     my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
-    my $changed;
     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};
+    };
 
-    print $reportfh "processing $fontdir...\n" or die $!;
     flush $reportfh or die $!;
     while (my $dent = scalar readdir FD) {
        if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
@@ -247,6 +351,8 @@ sub processfontdir ($) {
            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;
@@ -256,7 +362,9 @@ sub processfontdir ($) {
            next;
        }
        die "$fontdir $dent" unless -f _;
-       my $stats = join ' ', ((stat _)[1,7,9,10]);
+       $anypcfs++;
+
+       my $stats = statsummary();
        my $tdone = $olddone->{$dent};
        my $outdent = $fontprefix.$dent;
        if (defined $tdone && $tdone eq $stats) {
@@ -265,21 +373,34 @@ sub processfontdir ($) {
            next;
        }
 
-       my $r = processpcfgz("$fontdir/$dent",
-                            "$fontdir/$outdent.new",
-                            $log, $dent);
-       if ($r !~ m/^\d/) {
-           printf $log "%s: unchanged - %s\n", $dent, $r;
-           unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
-       } else {
-           rename "$fontdir/$outdent.new", "$fontdir/$outdent"
-               or die "$fontdir $outdent $!";
-           $updated++;
-           $outfiles{$outdent} |= 3;
-           $changed = 1;
+       $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;
        }
-       $newdone->{$dent} = $stats;
+       $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};
@@ -289,13 +410,18 @@ sub processfontdir ($) {
        }
        unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
        $changed = 1;
+       $updated++;
     }
-    if (!%$newdone) {
-       unlink "$fontdir/$logfile" or die "$fontdir $!";
-       unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
-    } elsif ($changed) {
+    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 $!";
@@ -303,8 +429,22 @@ sub processfontdir ($) {
        rename "$fontdir/$donefile.new","$fontdir/$donefile"
            or die "$fontdir $!";
     }
-    printf " processed %s: %d pcfs, %d affected, %d updated.\n",
-        $fontdir, (scalar keys %$newdone), $affected, $updated;
+    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);
 }
 
 our $stdin = new IO::File '<&STDIN' or die $!;
@@ -312,6 +452,23 @@ our $stdout = new IO::File '>&STDOUT' or die $!;
 our $stderr = new IO::File '>&STDERR' or die $!;
 $reportfh = $stdout;
 
+our (@options)=(
+    'R|rules-include=s@' => \@rulespath,
+    '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 ($$) {
+    my ($optname,$f) = @_;
+    push @options, $optname, sub {
+       die "only one mode may be specified\n" if defined $mode;
+       $mode=$f;
+    };
+}
+
 define_mode('bdf-filter', sub {
     die "no arguments allowed with --bdf-filter\n" if @ARGV;
     $reportfh = $stderr;
@@ -333,7 +490,7 @@ define_mode('process-pcf', sub {
     }
 });
 
-define_mode('process-fontdir', sub {
+define_mode('process-fontdirs', sub {
     die "need font dir(s)\n" unless @ARGV;
     loadfoundries();
     foreach my $d (@ARGV) {
@@ -341,6 +498,22 @@ define_mode('process-fontdir', sub {
     }
 });
 
+define_mode('process-fonttrees', sub {
+    die "need font tree(s)\n" unless @ARGV;
+    loadfoundries();
+    foreach my $d (@ARGV) {
+       processfonttree($d);
+    }
+});
+
+define_mode('update', sub {
+    die "no arguments allowed with --postinst\n" unless !@ARGV;
+    loadfoundries();
+    foreach my $d (@fonttrees) {
+       processfonttree($d);
+    }
+});
+
 Getopt::Long::Configure(qw(bundling));
 GetOptions(@options) or exit 127;
 
@@ -349,12 +522,3 @@ push @rulespath, "$sharedir/rules";
 die "need a mode\n" unless $mode;
 
 $mode->();
-
-#   70  zcat /usr/share/fonts/X11/misc/6x13.pcf.gz |pcf2bdf >in.bdf
-#   71  ./utility <in.bdf >out.bdf
-#   83  bdftopcf out.bdf >out.pcf
-#   84  gzip out.pcf 
-#   85  cp out.pcf.gz /usr/share/fonts/X11/misc/
-# really mkfontdir /usr/share/fonts/X11/misc/
-# xset fp rehash
-# xfontsel