Add externs to avoid multiple definitions, and then add missing definitions.
[rrq/maintain_lilo.git] / keytab-lilo.pl
1 #!/usr/bin/perl
2 $DEFAULT_MAP = "us";
3 $DEFAULT_EXT = ".kmap";
4
5 sub usage
6 {
7     print STDERR
8       "usage: $0 [ -p old_code=new_code ] ...\n".
9       (" "x(8+length $0))."[path]default_layout[.map] ] ".
10       "[path]kbd_layout[.map]\n";
11     exit 1;
12 }
13
14
15 while ($ARGV[0] eq "-p") {
16     shift(@ARGV);
17     &usage unless $ARGV[0] =~ /=/;
18     $table[eval($`)] = eval($');
19     shift(@ARGV);
20 }
21 &usage unless defined $ARGV[0];
22 load_map("def",defined $ARGV[1] ? $ARGV[0] : undef);
23 load_map("kbd",defined $ARGV[1] ? $ARGV[1] : $ARGV[0]);
24 &build_table("plain","shift","ctrl","altgr","shift_ctrl",
25   "altgr_ctrl","alt","shift_alt","ctrl_alt");
26 for ($i = 0; $i < 256; $i++) {
27     printf("%c",$table[$i] ? $table[$i] : $i) || die "print: $!";
28 }
29 close STDOUT || die "close: $!";
30
31
32 sub load_map
33 {
34     local ($pfx,$map) = @_;
35     local ($empty,$current);
36
37     $map = $DEFAULT_MAP unless defined $map;
38     $map .= $DEFAULT_EXT unless $map =~ m|/[^/]+\.[^/]+$|;
39     if (!open(FILE,"loadkeys -m $map |")) {
40         print STDERR "loadkeys -m $map: $!\n";
41         exit 1;
42     }
43     undef $current;
44     $empty = 1;
45     while (<FILE>) {
46         chop;
47         if (/^(static\s+)?u_short\s+(\S+)_map\[\S*\]\s+=\s+{\s*$/) {
48             die "active at beginning of map" if defined $current;
49             $current = $pfx.":".$2;
50             next;
51         }
52         undef $current if /^};\s*$/;
53         next unless defined $current;
54         s/\s//g;
55         $map{$current} .= $_;
56         $empty = 0;
57     }
58     close FILE;
59     return unless $empty;
60     print STDERR "Keymap is empty\n";
61     exit 1;
62 }
63
64
65 sub build_table
66 {
67     local (@maps) = @_;
68     local (@tmp);
69
70     $set = 0;
71     for $map (@maps) {
72         $code = $set;
73         for (split(",",$map{"def:".$map})) {
74             die "bad map entry $_ (def, map $map)" unless /^0x\S\S(\S\S)$/;
75             $tmp[$code] = hex $1 unless $tmp[$code];
76             $code++;
77         }
78         $set += 256;
79     }
80     $set = 0;
81     for $map (@maps) {
82         $code = $set;
83         for (split(",",$map{"kbd:".$map})) {
84             die "bad map entry $_ (kbd, map $map)" unless /^0x\S\S(\S\S)$/;
85             $table[$tmp[$code]] = hex $1 unless $table[$tmp[$code]];
86             $code++;
87         }
88         $set += 256;
89     }
90     $table[0] = 0;
91 }