chiark / gitweb /
wip packaging
[xfonts-traditional] / update-xfonts-traditional
CommitLineData
18d2cc90
IJ
1#!/usr/bin/perl -w
2use strict;
3use POSIX;
4use IO::File;
a2d36db5 5use Getopt::Long;
baaae949 6use File::Glob qw(:glob);
a94959e6 7use Data::Dumper;
71e1fff3 8use IO::Pipe;
1f0fee99 9use File::Find;
18d2cc90 10
1b17f3ec 11our $prefix="/usr/local";
a2d36db5 12our $package='xfonts-traditional';
1b17f3ec 13our $sharedir="$prefix/share/$package";
1f0fee99 14our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
baaae949 15our $donefile="$package.done";
a94959e6 16our $logfile="$package.log";
baaae949 17our $fontprefix="trad--";
a2d36db5
IJ
18our @rulespath;
19our $mode;
148a1d85
IJ
20our %foundrymap;
21our $verbose=0;
22our $reportfh;
23
24sub reportloaded {
25 return unless $verbose;
26 print $reportfh @_,"\n" or die $!;
27}
18d2cc90 28
4ee8efe0
IJ
29sub loadrules ($) {
30 my ($key) = @_;
18d2cc90 31 our %cache;
18d2cc90
IJ
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) {
148a1d85 38 reportloaded("rules: loaded ",$script);
18d2cc90
IJ
39 $cache{$key}=$f;
40 return $f;
41 }
42 die "$! $? $script" unless $! == &ENOENT;
43 }
44 return $cache{$key}=undef;
45}
46
a94959e6
IJ
47sub processbdf ($$$$) {
48 my ($inbdf,$outbdf,$logfile,$what) = @_;
4ee8efe0 49 my $state='idle';
148a1d85 50 my ($foundry,$font);
4ee8efe0 51 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
148a1d85 52 my $modified=0;
18d2cc90
IJ
53 while (<$inbdf>) {
54 if ($state eq 'bitmap' && $y==$h) {
148a1d85
IJ
55 $glyph = uc $glyph;
56 $glyph =~ s/\;$//;
57 local ($_) = $glyph;
58 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
4ee8efe0 59 my $rules= loadrules($key);
148a1d85 60 return (0,'no rules') if !$rules;
18d2cc90 61 $rules->();
148a1d85 62 $modified += ($_ ne $glyph);
18d2cc90 63 print $outbdf $_,"\n" or die $!
4ee8efe0 64 foreach split /\;/, $_; # /;
18d2cc90
IJ
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;";
4ee8efe0
IJ
71 $y++;
72 next;
18d2cc90 73 }
4ee8efe0 74 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
148a1d85
IJ
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";
4ee8efe0
IJ
80 }
81 if ($state eq 'idle' && m/^FONT\s+/) {
148a1d85 82 die if defined $font;
71e1fff3 83 return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
148a1d85
IJ
84 $font = $foundrymap{lc $2};
85 return (0,'no foundry') unless defined $font;
86 $_ = "FONT -$font-$'";
18d2cc90
IJ
87 }
88 if ($state eq 'idle' && m/^STARTCHAR\s/) {
148a1d85
IJ
89 die unless defined $foundry;
90 die unless defined $font;
91 return (0,'foundry != font') unless $foundry eq $font;
18d2cc90
IJ
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 }
4ee8efe0 106 print $outbdf $_ or die $!;
18d2cc90
IJ
107 }
108 die $! if $inbdf->error;
4ee8efe0 109 die $! if $outbdf->error or !$outbdf->flush;
18d2cc90 110 die unless $state eq 'idle';
148a1d85 111 if ($modified) {
a94959e6 112 printf $logfile "%s: %d glyphs changed\n", $what, $modified
148a1d85
IJ
113 or die $!;
114 } else {
a94959e6 115 printf $logfile "%s: unchanged - no rules matched\n", $what
148a1d85
IJ
116 or die $!;
117 }
118 return $modified;
18d2cc90
IJ
119}
120
148a1d85
IJ
121sub 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}
a2d36db5 143
71e1fff3
IJ
144sub 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;
be2a62a6 151 foreach my $proc (['gunzip'], ['pcf2bdf'], [],
71e1fff3
IJ
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}
30c6d632 210
baaae949
IJ
211sub processfontdir ($) {
212 my ($fontdir) = @_;
213 if (!opendir FD, $fontdir) {
214 die "$fontdir $!" unless $!==&ENOENT;
215 return;
216 }
c8ba454e 217 my $changed = 0;
a94959e6 218 my $olddone = do "$fontdir/$donefile";
30c6d632 219 if (!$olddone) {
baaae949 220 die "$fontdir $! $@ " unless $!==&ENOENT;
30c6d632 221 $olddone = { };
c8ba454e 222 $changed = 1;
baaae949 223 }
a94959e6
IJ
224 my $newdone = { };
225 my $log = new IO::File "$fontdir/$logfile", "w"
226 or die "$fontdir/$logfile $!";
71e1fff3 227 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
71e1fff3 228 my $updated=0;
1f0fee99 229 my $reported=0;
c8ba454e 230 my $anypcfs=0;
71e1fff3 231
71e1fff3
IJ
232 flush $reportfh or die $!;
233 while (my $dent = scalar readdir FD) {
a94959e6
IJ
234 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
235 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
236 next;
237 }
baaae949 238 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
1f0fee99
IJ
239 print $reportfh "processing $fontdir...\n" or die $!
240 unless $reported++;
baaae949 241 if ($dent =~ m/^\Q$fontprefix/) {
71e1fff3 242 $outfiles{$dent} |= 1;
baaae949
IJ
243 next;
244 }
71e1fff3 245 if (!stat "$fontdir/$dent") {
baaae949
IJ
246 die "$fontdir $dent $!" unless $!==&ENOENT;
247 next;
248 }
249 die "$fontdir $dent" unless -f _;
c8ba454e
IJ
250 $anypcfs++;
251
baaae949 252 my $stats = join ' ', ((stat _)[1,7,9,10]);
30c6d632 253 my $tdone = $olddone->{$dent};
71e1fff3 254 my $outdent = $fontprefix.$dent;
baaae949 255 if (defined $tdone && $tdone eq $stats) {
71e1fff3 256 $outfiles{$outdent} |= 2;
a94959e6 257 $newdone->{$dent} = $stats;
baaae949
IJ
258 next;
259 }
a94959e6 260
30c6d632
IJ
261 my $r = processpcfgz("$fontdir/$dent",
262 "$fontdir/$outdent.new",
71e1fff3 263 $log, $dent);
a94959e6 264 if ($r !~ m/^\d/) {
71e1fff3 265 printf $log "%s: unchanged - %s\n", $dent, $r;
a94959e6
IJ
266 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
267 } else {
268 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
269 or die "$fontdir $outdent $!";
71e1fff3
IJ
270 $updated++;
271 $outfiles{$outdent} |= 3;
a94959e6 272 }
71e1fff3 273 $newdone->{$dent} = $stats;
c8ba454e 274 $changed = 1;
a94959e6 275 }
71e1fff3
IJ
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 }
a94959e6
IJ
283 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
284 $changed = 1;
7f092ad2
IJ
285 $updated++;
286 }
287 if (!stat "$fontdir/fonts.dir") {
288 $!==&ENOENT or die "$fontdir $!";
289 } else {
290 $!=0; $?=0; system 'mkfontdir',$fontdir;
291 die "$fontdir $? $!" if $? or $!;
a94959e6 292 }
c8ba454e 293 if (!$anypcfs) {
71e1fff3
IJ
294 unlink "$fontdir/$logfile" or die "$fontdir $!";
295 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
296 } elsif ($changed) {
a94959e6
IJ
297 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
298 or die "$fontdir $!";
299 print $newdoneh Dumper($newdone) or die "$fontdir $!";
300 close $newdoneh or die "$fontdir $!";
301 rename "$fontdir/$donefile.new","$fontdir/$donefile"
302 or die "$fontdir $!";
303 }
1f0fee99
IJ
304 if ($reported || %$newdone || $affected || $updated) {
305 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
306 $fontdir, (scalar keys %$newdone), $affected, $updated;
307 }
308}
309
310sub processfonttree ($) {
311 my ($tree) = @_;
312 find({ follow => 1,
313 dangling_symlinks => 0,
314 no_chdir => 1,
315 wanted => sub {
316 return unless -d _;
317 processfontdir($File::Find::name);
318 }},
319 $tree);
a94959e6 320}
baaae949 321
148a1d85
IJ
322our $stdin = new IO::File '<&STDIN' or die $!;
323our $stdout = new IO::File '>&STDOUT' or die $!;
324our $stderr = new IO::File '>&STDERR' or die $!;
325$reportfh = $stdout;
326
1f0fee99
IJ
327our (@options)=(
328 'R|rules-include=s@' => \@rulespath,
329 'share-dir=s' => \$sharedir,
330 'verbose|v+' => \$verbose,
331 );
332
333sub define_mode ($$) {
334 my ($optname,$f) = @_;
335 push @options, $optname, sub {
336 die "only one mode may be specified\n" if defined $mode;
337 $mode=$f;
338 };
339}
340
148a1d85
IJ
341define_mode('bdf-filter', sub {
342 die "no arguments allowed with --bdf-filter\n" if @ARGV;
343 $reportfh = $stderr;
344 loadfoundries();
a94959e6 345 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
148a1d85
IJ
346 if ($r !~ m/^\d/) {
347 print STDERR "stdin not processed: $r\n";
348 exit 2;
349 }
a2d36db5
IJ
350});
351
71e1fff3
IJ
352define_mode('process-pcf', sub {
353 die "need source and destination pcf.gz\n" if @ARGV!=2;
354 loadfoundries();
355 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
356 if ($r !~ m/^\d/) {
357 print STDERR "pcf not processed: $r\n";
358 exit 2;
359 }
360});
361
1f0fee99 362define_mode('process-fontdirs', sub {
71e1fff3
IJ
363 die "need font dir(s)\n" unless @ARGV;
364 loadfoundries();
365 foreach my $d (@ARGV) {
366 processfontdir($d);
367 }
368});
369
1f0fee99
IJ
370define_mode('process-fonttrees', sub {
371 die "need font tree(s)\n" unless @ARGV;
372 loadfoundries();
373 foreach my $d (@ARGV) {
374 processfonttree($d);
375 }
376});
377
c8ba454e
IJ
378define_mode('update', sub {
379 die "no arguments allowed with --postinst\n" unless !@ARGV;
380 loadfoundries();
381 foreach my $d (@fonttrees) {
382 processfonttree($d);
383 }
384});
385
a2d36db5
IJ
386Getopt::Long::Configure(qw(bundling));
387GetOptions(@options) or exit 127;
388
db16069b 389push @rulespath, "$sharedir/rules";
a2d36db5
IJ
390
391die "need a mode\n" unless $mode;
392
393$mode->();