chiark / gitweb /
processpcfgz: completely revamped subprocess data structures and error handling
[xfonts-traditional.git] / update-xfonts-traditional
index 65d1d086c333112bdeb5f0727f2e7a687fd49197..2e8c0d7888b628307175ad867f09f23822cd6e9f 100755 (executable)
@@ -7,6 +7,7 @@ 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';
@@ -22,6 +23,7 @@ our $verbose=0;
 our $reportfh;
 our $foundryinfo;
 our %props;
+our $wanted_parallel;
 
 sub reportloaded {
     return unless $verbose;
@@ -57,14 +59,16 @@ sub processbdf ($$$$) {
     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 $!
@@ -80,22 +84,22 @@ 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;
        }
@@ -122,6 +126,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 $!;
@@ -170,6 +176,7 @@ sub processpcfgz ($$$$) {
     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 '');
@@ -195,7 +202,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,25 +221,51 @@ 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} = $?;
+    }
+
+    my $st_isok = sub {
+       my ($ch) = @_;
+       my $st = $ch->{St};
+       return !$st || $ch->{SigOK}{($st & ~128)};
+    };
+
+    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";
        }
-       $!=0; waitpid($child, 0) == $child or die "$child $!";
-       !$? or ($?&~128)==$sigok or die "$exe [$child] $sigok $?";
     }
     return $r;
 }
@@ -251,13 +291,45 @@ sub processfontdir ($) {
        $changed = 1;
     }
     my $newdone = { '' => $foundryinfo };
-    my $log = new IO::File "$fontdir/$logfile", "w" 
-       or die "$fontdir/$logfile $!";
     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$/) {
@@ -287,21 +359,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;
+       $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;
-       $changed = 1;
+       $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};
@@ -320,7 +405,7 @@ sub processfontdir ($) {
        die "$fontdir $? $!" if $? or $!;
     }
     if (!$anypcfs) {
-       unlink "$fontdir/$logfile" or die "$fontdir $!";
+       unlink "$logpath" or die "$fontdir $!";
        unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
     } elsif ($changed) {
        my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w' 
@@ -357,6 +442,7 @@ our (@options)=(
     'R|rules-include=s@' => \@rulespath,
     'share-dir=s' => \$sharedir,
     'verbose|v+' => \$verbose,
+    'j|parallel=i' => \$wanted_parallel,
     );
 
 sub define_mode ($$) {