chiark / gitweb /
better reporting; update mode
[xfonts-traditional.git] / update-xfonts-traditional
1 #!/usr/bin/perl -w
2 use strict;
3 use POSIX;
4 use IO::File;
5 use Getopt::Long;
6 use File::Glob qw(:glob);
7 use Data::Dumper;
8 use IO::Pipe;
9 use File::Find;
10
11 our $prefix="/usr/local";
12 our $package='xfonts-traditional';
13 our $sharedir="$prefix/share/$package";
14 our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
15 our $donefile="$package.done";
16 our $logfile="$package.log";
17 our $fontprefix="trad--";
18 our @rulespath;
19 our $mode;
20 our %foundrymap;
21 our $verbose=0;
22 our $reportfh;
23
24 sub reportloaded {
25     return unless $verbose;
26     print $reportfh @_,"\n" or die $!;
27 }
28
29 sub loadrules ($) {
30     my ($key) = @_;
31     our %cache;
32     my $fc=$cache{$key};  
33     return $fc if $fc;
34     foreach my $path (@rulespath) {
35         my $script="$path/$key.rules";
36         $!=0; $@=''; my $f = do $script;
37         if (defined $f) {
38             reportloaded("rules: loaded ",$script);
39             $cache{$key}=$f;
40             return $f;
41         }
42         die "$! $? $script" unless $! == &ENOENT;
43     }
44     return $cache{$key}=undef;
45 }
46
47 sub processbdf ($$$$) {
48     my ($inbdf,$outbdf,$logfile,$what) = @_;
49     my $state='idle';
50     my ($foundry,$font);
51     my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
52     my $modified=0;
53     while (<$inbdf>) {
54         if ($state eq 'bitmap' && $y==$h) {
55             $glyph = uc $glyph;
56             $glyph =~ s/\;$//;
57             local ($_) = $glyph;
58             my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
59             my $rules= loadrules($key);
60             return (0,'no rules') if !$rules;
61             $rules->();
62             $modified += ($_ ne $glyph);
63             print $outbdf $_,"\n" or die $!
64                 foreach split /\;/, $_; # /;
65             $state='idle';
66         }
67         if ($state eq 'bitmap') {
68             m/^([0-9a-fA-F]+)\s+$/ or die $y;
69             length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
70             $glyph .= "$1;";
71             $y++;
72             next;
73         }
74         if ($state eq 'idle' && m/^FOUNDRY\s+/) {
75             die if defined $foundry;
76             return (0,'foundry syntax') unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
77             $foundry = $foundrymap{lc $1};
78             return (0,'no foundry') unless defined $foundry;
79             $_ = "FOUNDRY \"$foundry\"\n";
80         }
81         if ($state eq 'idle' && m/^FONT\s+/) {
82             die if defined $font;
83             return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
84             $font = $foundrymap{lc $2};
85             return (0,'no foundry') unless defined $font;
86             $_ = "FONT -$font-$'";
87         }
88         if ($state eq 'idle' && m/^STARTCHAR\s/) {
89             die unless defined $foundry;
90             die unless defined $font;
91             return (0,'foundry != font') unless $foundry eq $font;
92             $state='startchar';
93             $w=undef;
94         }
95         if ($state eq 'startchar') {
96             if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
97                 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
98             }
99             if (m/^BITMAP\s+$/) {
100                 die unless defined $w;
101                 $y=0;
102                 $glyph='';
103                 $state='bitmap';
104             }
105         }
106         print $outbdf $_ or die $!;
107     }
108     die $! if $inbdf->error;
109     die $! if $outbdf->error or !$outbdf->flush;
110     die unless $state eq 'idle';
111     if ($modified) {
112         printf $logfile "%s: %d glyphs changed\n", $what, $modified
113             or die $!;
114     } else {
115         printf $logfile "%s: unchanged - no rules matched\n", $what
116             or die $!;
117     }
118     return $modified;
119 }
120
121 sub loadfoundries () {
122     foreach my $path (@rulespath) {
123         my $p = "$path/foundries";
124         my $f = new IO::File $p;
125         if (!$f) {
126             die "$p $!" unless $!==&ENOENT;
127             print $reportfh "foundries: none in $p\n" or die $! if $verbose;
128             next;
129         }
130         while (<$f>) {
131             s/^\s*//; s/\s+$//;
132             next if m/^\#/;
133             m/^(\w+)\s+(\w+)$/ or die;
134             my $k = lc $1;
135             next if exists $foundrymap{$k};
136             $foundrymap{$k}=$2;
137         }
138         $f->error and die $!;
139         reportloaded('foundries: loaded ',$p);
140     }
141     die "no foundry maps\n" unless %foundrymap;
142 }
143
144 sub processpcfgz ($$$$) {
145     my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
146
147     my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
148     my ($usread,$uswrite);
149     my ($reader,$writer);
150     my @children;
151     foreach my $proc (['zcat'], ['pcf2bdf'], [],
152                       ['bdftopcf'],['',qw(gzip -1)]) {
153         my $isfinal = (@$proc && $proc->[0] eq '');
154         if (!$isfinal) {
155             $reader = new IO::Handle or die $!;
156             $writer = new IO::Handle or die $!;
157             new IO::Pipe($reader,$writer) or die $!;
158         } else {
159             shift @$proc;
160             $reader = undef;
161             $writer = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
162         }
163         if (@$proc) {
164             my $exe = $proc->[0];
165             my $child = fork;  defined $child or die $!;
166             if (!$child) {
167                 open STDIN, '<&', $current or die $!;
168                 open STDOUT, '>&', $writer or die $!;
169                 if (!$isfinal) {
170                     close $reader or die $!;
171                 }
172                 close $usread or die $! if $usread;
173                 close $uswrite or die $! if $uswrite;
174                 exec $exe @$proc or die "$exe $!";
175             }
176             push @children, [ $child, $exe, defined $usread ];
177             close $current or die $!;
178             close $writer or die $!;
179             $current = $reader;
180         } else {
181             $usread = $current;
182             $uswrite = $writer;
183             $current = $reader;
184         }
185     }
186     my $r = processbdf($usread,$uswrite,$logfile,$what);
187     my $none = $r !~ m/^\d/;
188     if ($none) {
189         flush $uswrite or die $!;
190     } else {
191         close $uswrite or die $!;
192     }
193     close $usread or die $!;
194     foreach my $chinfo (@children) {
195         my ($child,$exe,$isoutput)=@$chinfo;
196         my $sigok = 0;
197         if ($none) {
198             if ($isoutput) {
199                 $sigok = 9;
200                 kill 9, $child or die "$child $!";
201             } else {
202                 $sigok = 13;
203             }
204         }
205         $!=0; waitpid($child, 0) == $child or die "$child $!";
206         !$? or ($?&~128)==$sigok or die "$exe [$child] $sigok $?";
207     }
208     return $r;
209 }
210
211 sub processfontdir ($) {
212     my ($fontdir) = @_;
213     if (!opendir FD, $fontdir) {
214         die "$fontdir $!" unless $!==&ENOENT;
215         return;
216     }
217     my $changed = 0;
218     my $olddone = do "$fontdir/$donefile";
219     if (!$olddone) {
220         die "$fontdir $! $@ " unless $!==&ENOENT;
221         $olddone = { };
222         $changed = 1;
223     }
224     my $newdone = { };
225     my $log = new IO::File "$fontdir/$logfile", "w" 
226         or die "$fontdir/$logfile $!";
227     my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
228     my $updated=0;
229     my $reported=0;
230     my $anypcfs=0;
231
232     flush $reportfh or die $!;
233     while (my $dent = scalar readdir FD) {
234         if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
235             unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
236             next;
237         }
238         next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
239         print $reportfh "processing $fontdir...\n" or die $!
240             unless $reported++;
241         if ($dent =~ m/^\Q$fontprefix/) {
242             $outfiles{$dent} |= 1;
243             next;
244         }
245         if (!stat "$fontdir/$dent") {
246             die "$fontdir $dent $!" unless $!==&ENOENT;
247             next;
248         }
249         die "$fontdir $dent" unless -f _;
250         $anypcfs++;
251
252         my $stats = join ' ', ((stat _)[1,7,9,10]);
253         my $tdone = $olddone->{$dent};
254         my $outdent = $fontprefix.$dent;
255         if (defined $tdone && $tdone eq $stats) {
256             $outfiles{$outdent} |= 2;
257             $newdone->{$dent} = $stats;
258             next;
259         }
260
261         my $r = processpcfgz("$fontdir/$dent",
262                              "$fontdir/$outdent.new",
263                              $log, $dent);
264         if ($r !~ m/^\d/) {
265             printf $log "%s: unchanged - %s\n", $dent, $r;
266             unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
267         } else {
268             rename "$fontdir/$outdent.new", "$fontdir/$outdent"
269                 or die "$fontdir $outdent $!";
270             $updated++;
271             $outfiles{$outdent} |= 3;
272         }
273         $newdone->{$dent} = $stats;
274         $changed = 1;
275     }
276     my $affected=0;
277     foreach my $olddent (keys %outfiles) {
278         my $state = $outfiles{$olddent};
279         if ($state & 2) {
280             $affected++ if $state & 1;
281             next;
282         }
283         unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
284         $changed = 1;
285     }
286     if (!$anypcfs) {
287         unlink "$fontdir/$logfile" or die "$fontdir $!";
288         unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
289     } elsif ($changed) {
290         $!=0; $?=0; system 'mkfontdir',$fontdir;
291         die "$fontdir $? $!" if $? or $!;
292         my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w' 
293             or die "$fontdir $!";
294         print $newdoneh Dumper($newdone) or die "$fontdir $!";
295         close $newdoneh or die "$fontdir $!";
296         rename "$fontdir/$donefile.new","$fontdir/$donefile"
297             or die "$fontdir $!";
298     }
299     if ($reported || %$newdone || $affected || $updated) {
300         printf " processed %s: %d pcfs, %d affected, %d updated.\n",
301             $fontdir, (scalar keys %$newdone), $affected, $updated;
302     }
303 }
304
305 sub processfonttree ($) {
306     my ($tree) = @_;
307     find({ follow => 1,
308            dangling_symlinks => 0,
309            no_chdir => 1,
310            wanted => sub {
311                return unless -d _;
312                processfontdir($File::Find::name);
313            }},
314          $tree);
315 }
316
317 our $stdin = new IO::File '<&STDIN' or die $!;
318 our $stdout = new IO::File '>&STDOUT' or die $!;
319 our $stderr = new IO::File '>&STDERR' or die $!;
320 $reportfh = $stdout;
321
322 our (@options)=(
323     'R|rules-include=s@' => \@rulespath,
324     'share-dir=s' => \$sharedir,
325     'verbose|v+' => \$verbose,
326     );
327
328 sub define_mode ($$) {
329     my ($optname,$f) = @_;
330     push @options, $optname, sub {
331         die "only one mode may be specified\n" if defined $mode;
332         $mode=$f;
333     };
334 }
335
336 define_mode('bdf-filter', sub {
337     die "no arguments allowed with --bdf-filter\n" if @ARGV;
338     $reportfh = $stderr;
339     loadfoundries();
340     my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
341     if ($r !~ m/^\d/) {
342         print STDERR "stdin not processed: $r\n";
343         exit 2;
344     }
345 });
346
347 define_mode('process-pcf', sub {
348     die "need source and destination pcf.gz\n" if @ARGV!=2;
349     loadfoundries();
350     my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
351     if ($r !~ m/^\d/) {
352         print STDERR "pcf not processed: $r\n";
353         exit 2;
354     }
355 });
356
357 define_mode('process-fontdirs', sub {
358     die "need font dir(s)\n" unless @ARGV;
359     loadfoundries();
360     foreach my $d (@ARGV) {
361         processfontdir($d);
362     }
363 });
364
365 define_mode('process-fonttrees', sub {
366     die "need font tree(s)\n" unless @ARGV;
367     loadfoundries();
368     foreach my $d (@ARGV) {
369         processfonttree($d);
370     }
371 });
372
373 define_mode('update', sub {
374     die "no arguments allowed with --postinst\n" unless !@ARGV;
375     loadfoundries();
376     foreach my $d (@fonttrees) {
377         processfonttree($d);
378     }
379 });
380
381 Getopt::Long::Configure(qw(bundling));
382 GetOptions(@options) or exit 127;
383
384 push @rulespath, "$sharedir/rules";
385
386 die "need a mode\n" unless $mode;
387
388 $mode->();