File: xmlreader

package info (click to toggle)
console-setup 1.244
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,124 kB
  • sloc: perl: 10,288; xml: 8,643; sh: 3,901; makefile: 777
file content (318 lines) | stat: -rwxr-xr-x 7,243 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/perl

#     xmlreader -- read xorg.xml file
#     Copyright © 2005 Anton Zinoviev <anton@lml.bas.bg>

#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.

#     If you have not received a copy of the GNU General Public License
#     along with this program, write to the Free Software Foundation, Inc.,
#     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use warnings 'all';
use strict;
use utf8;

my $file;
if ($ARGV[0]) {
    $file = $ARGV[0];
} else {
    $file = 'ckb/rules/xorg.xml';
}

sub debug {
    if (1) {
	print STDERR "@_";
    }
}

sub warning {
    print STDERR  "WARNING: @_";
}

sub print_arg {
}

sub print_xml;
sub print_xml {
    my @stream = @{$_[0]};
    while (@stream) {
	my $tag = shift @stream;
	my $arg = shift @stream;
	if ($tag eq 0) {
	    print STDERR $arg;
	} else {
	    my @substream = @{$arg};
	    print STDERR "<$tag";
	    print_arg shift(@substream);
	    print STDERR ">";
	    print_xml \@substream;
	    print STDERR "</$tag>\n";
	}
    }
}

use XML::Parser;

my $parser = new XML::Parser (Style => 'Tree');

my $tree = $parser->parsefile ($file);

my %models;
my %layouts;
my %variants;

sub parse_text {
    my $tree = $_[0];
    my $contents = '';
    shift @{$tree};
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 0) {
	    $contents = $contents . $arg;
	} else {
	    $contents = $contents . parse_text ($arg);
	}
    }
    return $contents;
}

sub parse_configItem {
    my $tree = $_[0];
    shift @{$tree};
    my $name;
    my $description;
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'name') {
	    $name = parse_text $arg;
	} elsif ($tag eq 'description') {
	    if (! %{$arg->[0]}) {
		$description = parse_text $arg;
	    }
	} elsif ($tag =~ /^(shortDescription|_description
                           |vendor|hwList|languageList|countryList)$/x) {
	} elsif ($tag eq 0) {
	    warning "configItem: Garbage in configItem: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "configItem: Unknown tag $tag, arg=$arg\n";
	}
    }
    $name = '' unless ($name);
    $description = $name unless ($description);
    return ($name, $description);
}

sub parse_model {
    my $tree = $_[0];
    shift @{$tree};
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'configItem') {
	    my ($name, $description) = parse_configItem $arg;
	    if ($name ne '') {
		$models{$description} = $name;
	    }
	} elsif ($tag eq 0) {
	    warning "model: Garbage in model: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "model: Unknown tag $tag, arg=$arg\n";
	}
    }
}

sub parse_modelList {
    my $tree = $_[0];
    shift @{$tree};
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'model') {
	    parse_model $arg;
	} elsif ($tag eq 0) {
	    warning "modelList: Garbage in modelList: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "modelList: Unknown tag $tag, arg=$arg\n";
	}
    }
}

sub parse_variant {
    my $layout = $_[0];
    my $tree = $_[1];
    shift @{$tree};
    my $name;
    my $description;
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'configItem') {
	    ($name, $description) = parse_configItem $arg;
	    if ($layout ne '' && $name ne '') {
		$variants{$layout}{$description} = $name;
	    }
	} elsif ($tag eq 0) {
	    warning "variant: Garbage in variant: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "variant: Unknown tag $tag, arg=$arg\n";
	}
    }
}

sub parse_variantList {
    my $name = $_[0];
    my $tree = $_[1];
    shift @{$tree};
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'variant') {
	    parse_variant $name, $arg;
	} elsif ($tag eq 0) {
	    warning "variantList: Garbage in variantList: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "variantList: Unknown tag $tag\n";
	    shift @{$arg};
	    print_xml $arg;
	}
    }
}

sub parse_layout {
    my $tree = $_[0];
    shift @{$tree};
    my $name;
    my $description;
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'configItem') {
	    ($name, $description) = parse_configItem $arg;
	    if ($name ne "") {
		$layouts{$description} = $name;
	    }
	} elsif ($tag eq 'variantList') {
	    if (! $name) {
		warning "layout: variantList before configItem\n";
		next;
	    }
	    parse_variantList $name, $arg;
	} elsif ($tag eq 0) {
	    warning "layout: Garbage in model: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "layout: Unknown tag $tag, arg=$arg\n";
	}
    }
}

sub parse_layoutList {
    my $tree = $_[0];
    shift @{$tree};
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'layout') {
	    parse_layout $arg;
	} elsif ($tag eq 0) {
	    warning "layoutList: Garbage in modelList: $arg.\n" if ($arg !~ /^\s*$/);
	} else {
	    warning "layoutList: Unknown tag $tag, arg=$arg\n";
	}
    }
}

sub parse_optionList {
}

sub parse_xkbConfigRegistry {
    my $tree = $_[0];
    shift @{$tree};
    while (@{$tree}) {
	my $tag = shift @{$tree};
	my $arg = shift @{$tree};
	if ($tag eq 'modelList') {
	    parse_modelList $arg;
	} elsif ($tag eq 'layoutList') {
	    parse_layoutList $arg;
	} elsif ($tag eq 'optionList') {
	    parse_optionList $arg;
	} elsif ($tag eq 0) {
	    warning "xkbConfigRegistry: Garbage in xkbConfigRegistry: $arg.\n"
		if ($arg !~ /^\s*$/);
	} else {
	    warning "xkbConfigRegistry: Unknown tag $tag, arg=$arg\n";
	}
    }
}

while (@{$tree}) {
    my $tag = shift @{$tree};
    my $arg = shift @{$tree};
    if ($tag eq 'xkbConfigRegistry') {
	parse_xkbConfigRegistry $arg;
    }
}

# Fixups for model names we need
my %modelvalues = map { $_ => 1 } values %models;
if (not exists $modelvalues{amiga}) {
    $models{'Amiga'} = 'amiga';
}
if (not exists $modelvalues{ataritt}) {
    $models{'Atari TT'} = 'ataritt';
}
if (not exists $modelvalues{sun4}) {
    $models{'Sun Type 4'} = 'sun4';
}
if (not exists $modelvalues{sun5}) {
    $models{'Sun Type 5'} = 'sun5';
}

print <<'EOT';
#!/usr/bin/perl -w

package KeyboardNames;

EOT

print "%models = (\n";
for my $x (sort keys %models) {
    my $y = $models{$x};
    $x =~ s/'//g;
    print "    '$x' => '$y',\n";
}
print ");\n\n";

print "%layouts = (\n";
for my $x (sort keys %layouts) {
    my $y = $layouts{$x};
    $x =~ s/'//g;
    print "    '$x' => '$y',\n";
}
print ");\n\n";

print "%variants = (\n";
for my $x (sort keys %variants) {
    my $y = $variants{$x};
    print "    '$x' => {\n";
    for my $z (sort keys %{$y}) {
	my $t = $y->{$z};
	$z =~ s/'//g;
	print "	'$z' => '$t',\n";
    }
    print "    },\n";
}
print ");\n\n";

print "1;\n";