chiark / gitweb /
manpage
[xfonts-traditional.git] / update-xfonts-traditional
index e8ed7ab934b3a2e8575f5f55428d23b0ec8bbfbd..839cc1827cf678d595e3a43d1b8e7d6f6ad8c365 100755 (executable)
@@ -5,11 +5,13 @@ use IO::File;
 use Getopt::Long;
 use File::Glob qw(:glob);
 use Data::Dumper;
+use IO::Pipe;
+use File::Find;
 
 our $prefix="/usr/local";
 our $package='xfonts-traditional';
 our $sharedir="$prefix/share/$package";
-our @fontsdirs=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--";
@@ -18,12 +20,17 @@ our $mode;
 our %foundrymap;
 our $verbose=0;
 our $reportfh;
+our $foundryinfo;
 
 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;
@@ -78,7 +85,7 @@ sub processbdf ($$$$) {
        }
        if ($state eq 'idle' && m/^FONT\s+/) {
            die if defined $font;
-           return 0 unless m/^(FONT\s+)\-(\w+)\-/;
+           return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
            $font = $foundrymap{lc $2};
            return (0,'no foundry') unless defined $font;
            $_ = "FONT -$font-$'";
@@ -116,22 +123,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) {
@@ -139,6 +139,7 @@ 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/^\#/;
@@ -153,16 +154,72 @@ sub loadfoundries () {
     die "no foundry maps\n" unless %foundrymap;
 }
 
-sub processpcfgz ($$) {
-    my ($inpcfgz,$outpcfgz) = @_;
-    my $inh = new IO::File $inpcfgz, 'r', or die "$inpcfgz $!";
-    my $outh = new IO::File $outpcfgz, 'w' or die "$outpcfgz $!";
-    my $inpipe = new IO:Handle or die $!;
-    my $outpipe = new IO:Handle or die $!;
-    my $inchild = open $inpipe, "-|"; defined $inchild or die $!;
-    if (!$inchild) {
-       
-    opne 
+sub processpcfgz ($$$$) {
+    my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
+
+    my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
+    my ($usread,$uswrite);
+    my ($reader,$writer);
+    my @children;
+    foreach my $proc (['gunzip'], ['pcf2bdf'], [],
+                     ['bdftopcf'],['',qw(gzip -1)]) {
+       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 $!";
+           }
+           push @children, [ $child, $exe, defined $usread ];
+           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/;
+    if ($none) {
+       flush $uswrite or die $!;
+    } else {
+       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;
+           }
+       }
+       $!=0; waitpid($child, 0) == $child or die "$child $!";
+       !$? or ($?&~128)==$sigok or die "$exe [$child] $sigok $?";
+    }
+    return $r;
+}
 
 sub processfontdir ($) {
     my ($fontdir) = @_;
@@ -170,62 +227,94 @@ sub processfontdir ($) {
        die "$fontdir $!" unless $!==&ENOENT;
        return;
     }
+    my $changed = 0;
     my $olddone = do "$fontdir/$donefile";
     if (!$olddone) {
        die "$fontdir $! $@ " unless $!==&ENOENT;
+    }
+    if ($olddone && $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 = { };
+    my $newdone = { '' => $foundryinfo };
     my $log = new IO::File "$fontdir/$logfile", "w" 
        or die "$fontdir/$logfile $!";
-    my %found;
-    my $changed;
-    while ($!=0, my $dent = readdir FD) {
+    my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
+    my $updated=0;
+    my $reported=0;
+    my $anypcfs=0;
+
+    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/) {
-           $found{$dent} ||= 1;
+           $outfiles{$dent} |= 1;
            next;
        }
-       if (!stat $dent) {
+       if (!stat "$fontdir/$dent") {
            die "$fontdir $dent $!" unless $!==&ENOENT;
            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) {
-           $found{$dent} = 2;
+           $outfiles{$outdent} |= 2;
            $newdone->{$dent} = $stats;
            next;
        }
-       my $outdent = $fontprefix.$dent;
 
        my $r = processpcfgz("$fontdir/$dent",
                             "$fontdir/$outdent.new",
-                            $dent");
+                            $log, $dent);
        if ($r !~ m/^\d/) {
-           print $logfile "%s: unchanged - %s\n", $dent, $r;
+           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 $!";
-           $changed = 1;
+           $updated++;
+           $outfiles{$outdent} |= 3;
        }
-       $found{$outdent} = 2;
+       $newdone->{$dent} = $stats;
+       $changed = 1;
     }
-    die "$fontdir $!" if $!;
-    foreach my $olddent (keys %found) {
-       next if $found{olddent} != 1;
+    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 ($changed) {
+    if (!stat "$fontdir/fonts.dir") {
+       $!==&ENOENT or die "$fontdir $!";
+    } else {
        $!=0; $?=0; system 'mkfontdir',$fontdir;
        die "$fontdir $? $!" if $? or $!;
+    }
+    if (!$anypcfs) {
+       unlink "$fontdir/$logfile" 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 $!";
@@ -233,6 +322,22 @@ sub processfontdir ($) {
        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);
 }
 
 our $stdin = new IO::File '<&STDIN' or die $!;
@@ -240,6 +345,20 @@ 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,
+    );
+
+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;
@@ -251,6 +370,40 @@ define_mode('bdf-filter', sub {
     }
 });
 
+define_mode('process-pcf', sub {
+    die "need source and destination pcf.gz\n" if @ARGV!=2;
+    loadfoundries();
+    my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
+    if ($r !~ m/^\d/) {
+       print STDERR "pcf not processed: $r\n";
+       exit 2;
+    }
+});
+
+define_mode('process-fontdirs', sub {
+    die "need font dir(s)\n" unless @ARGV;
+    loadfoundries();
+    foreach my $d (@ARGV) {
+       processfontdir($d);
+    }
+});
+
+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;
 
@@ -259,12 +412,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