6 use File::Glob qw(:glob);
12 our $prefix="/usr/local";
13 our $package='xfonts-traditional';
14 our $sharedir="$prefix/share/$package";
15 our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
16 our $donefile="$package.done";
17 our $logfile="$package.log";
18 our $fontprefix="trad--";
26 our $tolerate_bad_fonts=1;
27 our $always_reprocess;
31 return unless $verbose;
32 print $reportfh @_,"\n" or die $!;
36 return join ' ', ((stat _)[1,7,9,10]);
44 foreach my $path (@rulespath) {
45 my $script="$path/$key.rules";
46 $!=0; $@=''; my $f = do $script;
48 reportloaded("rules: loaded ",$script);
52 die "$! $? $script" unless $! == &ENOENT;
54 return $cache{$key}=undef;
57 sub processbdf ($$$$) {
58 my ($inbdf,$outbdf,$logfh,$what) = @_;
61 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
67 if ($state eq 'bitmap' && $y==$h) {
71 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
72 my $rules= loadrules($key);
73 return 'no rules' if !$rules;
75 $modified += ($_ ne $glyph);
76 print $outbdf $_,"\n" or die $!
77 foreach split /\;/, $_; # /;
80 if ($state eq 'bitmap') {
81 m/^([0-9a-fA-F]+)\s+$/ or die $y;
82 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
87 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
88 die if defined $foundry;
89 return 'foundry syntax' unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
90 $foundry = $foundrymap{lc $1};
91 return 'no foundry' unless defined $foundry;
92 $_ = "FOUNDRY \"$foundry\"\n";
94 if ($state eq 'idle' && m/^FONT\s+/) {
96 return 'simple font name' unless m/^(FONT\s+)\-(\w+)\-/;
97 $font = $foundrymap{lc $2};
98 return 'no foundry' unless defined $font;
99 $_ = "FONT -$font-$'";
101 if ($state eq 'idle' && m/^STARTCHAR\s/) {
102 die unless defined $foundry;
103 die unless defined $font;
104 return 'foundry != font' unless $foundry eq $font;
108 if (($state eq 'idle' || $state eq 'startchar') &&
109 m/^([A-Z_]+)\s+(.*\S)\s+$/) {
112 if ($state eq 'startchar') {
113 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
114 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
116 if (m/^BITMAP\s+$/) {
117 die unless defined $w;
122 ($props{'CHARSET_REGISTRY'} =~ m/iso8859|utf|iso10646/i &&
123 $props{'ENCODING'} <= 127);
126 print $outbdf $_ or die $!;
128 die $! if $inbdf->error;
129 die $! if $outbdf->error or !$outbdf->flush;
130 die unless $state eq 'idle';
131 return 'no bdf data' # also special cased in processpcfgz
134 printf $logfh "%s: %d glyphs changed\n", $what, $modified
137 printf $logfh "%s: unchanged - no rules matched\n", $what
143 sub loadfoundries () {
145 foreach my $path (@rulespath) {
147 die "$path $!" unless $!==&ENOENT;
150 $foundryinfo .= statsummary().' '.$path."\0\n";
152 my $p = "$path/foundries";
153 my $f = new IO::File $p;
155 die "$p $!" unless $!==&ENOENT;
156 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
163 m/^([.0-9A-Za-z]+)\s+([.0-9A-Za-z]+)$/ or die;
165 next if exists $foundrymap{$k};
168 $f->error and die $!;
169 reportloaded('foundries: loaded ',$p);
171 die "no foundry maps\n" unless %foundrymap;
176 sub filter_st_isok ($) {
179 return !$st || $ch->{SigOK}{($st & ~128)};
182 sub process_filter ($$$$$$$$) {
183 my ($rr, $input, $output,$what,$logfh,
184 $procs, $sigpipeok, $after_hook) = @_;
185 my ($reader,$writer);
187 my ($usread,$uswrite);
189 my $current = $input;
191 foreach my $proc (@$procs) {
192 my $isfinal = (@$proc && $proc->[0] eq '');
194 $reader = new IO::Handle or die $!;
195 $writer = new IO::Handle or die $!;
196 new IO::Pipe($reader,$writer) or die $!;
203 my $exe = $proc->[0];
204 my $child = fork; defined $child or die $!;
206 open STDIN, '<&', $current or die $!;
207 open STDOUT, '>&', $writer or die $!;
209 close $reader or die $!;
211 close $usread or die $! if $usread;
212 close $uswrite or die $! if $uswrite;
213 exec $exe @$proc or die "$exe $!";
218 Stage => (!$exe ? 'self' : defined $usread ? 'out' : 'in'),
223 close $current or die $!;
224 close $writer or die $!;
232 $$rr = processbdf($usread,$uswrite,$logfh,$what);
233 my $none = $$rr !~ m/^\d/;
235 $ch{$_}{SigOK}{13} = 1 foreach @$sigpipeok;
237 if ($none || !$$rr) {
238 # We're not going to install or use this so we can kill our
239 # input and output filters. We kill the input filters so that
240 # we don't risk waiting for them. (If the input filter died
241 # for some other reason then sending it a KILL now won't
242 # affect its exit status.) We kill the output filters (before
243 # we close the output pipe) so we don't produce messages from
244 # our output filters about corrupted data.
245 flush $uswrite or die $!;
247 foreach my $ch (@children) {
248 if ($ch->{Stage} ne 'self') {
249 kill 9, $ch->{Pid} or die "$ch->{Pid} $ch->{Exe} $!";
253 $ch{'pcf2bdf'}{SigOK}{13} = 1;
254 # ... we might not have read all the output from pcf2bdf, which is OK
257 close $uswrite or die $!;
258 close $usread or die $!;
260 foreach my $ch (@children) {
261 $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
262 die "$ch->{Pid} $ch->{Exe} $!";
268 foreach my $ch (@children) {
269 if (!filter_st_isok($ch)) {
270 die "update-xfonts-traditional:".
271 " $ch->{Exe} [$ch->{Pid}] for $what".
273 " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
278 sub processpcfgz ($$$$) {
279 my ($inpcfgz,$outpcfgz,$logfh,$what) = @_;
280 print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
281 my $input = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
282 my $output = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
285 process_filter(\$r, $input, $output, $inpcfgz, $logfh,
287 ['gunzip'], ['pcf2bdf'],
289 ['bdftopcf'],['',qw(gzip -1 -n)]
292 # ... we never care if pcf2bdf didn't want all the data
294 if ($tolerate_bad_fonts &&
295 $r eq 'no bdf data' &&
296 filter_st_isok($ch{'gunzip'}) &&
297 ($ch{'pcf2bdf'}{St} & ~128) == 6)
299 $r = "pcf2bdf failed ($ch{'pcf2bdf'}{St})";
300 print STDERR "warning: $r: skipping $inpcfgz\n";
301 $ch{'pcf2bdf'}{SigOK}{6} = 1;
307 sub processfontdir ($) {
309 if (!opendir FD, $fontdir) {
310 die "$fontdir $!" unless $!==&ENOENT;
315 if (!$always_reprocess) {
316 $olddone = do "$fontdir/$donefile";
318 die "$fontdir $! $@ " unless $!==&ENOENT;
319 } elsif ($olddone->{''} ne $foundryinfo) {
321 print $reportfh "reprocessing fonts (rules updated)\n" or die $!
322 unless $repro_reported++;
330 my $newdone = { '' => $foundryinfo };
331 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
336 my $logpath = "$fontdir/$logfile";
337 unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
338 my $logfh = new IO::File $logpath, ">>" or die "$logpath $!";
340 if (!$wanted_parallel) {
341 $wanted_parallel = Sys::CPU::cpu_count();
342 printf $reportfh "parallelism: %d\n", $wanted_parallel if $verbose>=2;
344 $wanted_parallel = 1 if $wanted_parallel < 1;
350 die $! unless defined $child;
351 my $job = $inprogress{$child};
352 die $child unless $job;
354 my $dent = $job->{Dent};
355 my $outdent = $job->{Outdent};
356 my $stats = $job->{Stats};
359 $outfiles{$outdent} |= 3;
360 } elsif ($?==2*256) {
362 die "update-xfonts-traditional: processing of".
363 " $fontdir/$dent [$child] failed ($?)\n";
365 $newdone->{$dent} = $stats;
367 delete $inprogress{$child};
370 flush $reportfh or die $!;
371 while (my $dent = scalar readdir FD) {
372 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
373 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
376 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
377 print $reportfh "processing $fontdir...\n" or die $!
379 if ($dent =~ m/^\Q$fontprefix/) {
380 $outfiles{$dent} |= 1;
383 if (!stat "$fontdir/$dent") {
384 die "$fontdir $dent $!" unless $!==&ENOENT;
387 die "$fontdir $dent" unless -f _;
390 my $stats = statsummary();
391 my $tdone = $olddone->{$dent};
392 my $outdent = $fontprefix.$dent;
393 if (defined $tdone && $tdone eq $stats) {
394 $outfiles{$outdent} |= 2;
395 $newdone->{$dent} = $stats;
399 $await->() while scalar keys %inprogress >= $wanted_parallel;
401 my $child = fork; die unless defined $child;
403 my $r = processpcfgz("$fontdir/$dent",
404 "$fontdir/$outdent.new",
408 printf $logfh "%s: unchanged - %s\n", $dent, $r;
409 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
412 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
413 or die "$fontdir $outdent $!";
416 $logfh->flush or die "$logpath $!";
419 $inprogress{$child} = {
425 $await->() while scalar keys %inprogress;
428 foreach my $olddent (keys %outfiles) {
429 my $state = $outfiles{$olddent};
431 $affected++ if $state & 1;
434 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
438 if (!stat "$fontdir/fonts.dir") {
439 $!==&ENOENT or die "$fontdir $!";
441 $!=0; $?=0; system 'mkfontdir',$fontdir;
442 die "$fontdir $? $!" if $? or $!;
445 unlink "$logpath" or die "$fontdir $!";
446 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
448 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
449 or die "$fontdir $!";
450 print $newdoneh Dumper($newdone) or die "$fontdir $!";
451 close $newdoneh or die "$fontdir $!";
452 rename "$fontdir/$donefile.new","$fontdir/$donefile"
453 or die "$fontdir $!";
455 if ($reported || %$newdone || $affected || $updated) {
456 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
457 $fontdir, (scalar keys %$newdone), $affected, $updated;
461 sub processfonttree ($) {
464 dangling_symlinks => 0,
468 processfontdir($File::Find::name);
473 our $stdin = new IO::File '<&STDIN' or die $!;
474 our $stdout = new IO::File '>&STDOUT' or die $!;
475 our $stderr = new IO::File '>&STDERR' or die $!;
479 'R|rules-include=s@' => \@rulespath,
480 'share-dir=s' => \$sharedir,
481 'verbose|v+' => \$verbose,
482 'j|parallel=i' => \$wanted_parallel,
483 'always-reprocess!' => \$always_reprocess,
484 'tolerate-bad-fonts!' => \$tolerate_bad_fonts,
487 sub define_mode ($$) {
488 my ($optname,$f) = @_;
489 push @options, $optname, sub {
490 die "only one mode may be specified\n" if defined $mode;
495 define_mode('bdf-filter', sub {
496 die "no arguments allowed with --bdf-filter\n" if @ARGV;
499 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
501 print STDERR "stdin not processed: $r\n";
506 define_mode('process-pcf', sub {
507 die "need source and destination pcf.gz\n" if @ARGV!=2;
509 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
511 print STDERR "pcf not processed: $r\n";
516 define_mode('process-fontdirs', sub {
517 die "need font dir(s)\n" unless @ARGV;
519 foreach my $d (@ARGV) {
524 define_mode('process-fonttrees', sub {
525 die "need font tree(s)\n" unless @ARGV;
527 foreach my $d (@ARGV) {
532 define_mode('update', sub {
533 die "no arguments allowed with --postinst\n" unless !@ARGV;
535 foreach my $d (@fonttrees) {
540 Getopt::Long::Configure(qw(bundling));
541 GetOptions(@options) or exit 127;
543 push @rulespath, "$sharedir/rules";
545 die "need a mode\n" unless $mode;