syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / keytab-lilo.pl
1 #!/usr/bin/perl
2 # --------------------------------------------------------------------------
3 # This program was taken from the LILO-20 distribution; only this header
4 # was added.
5 #
6 # LILO program code, documentation and auxiliary programs are
7 # Copyright 1992-1997 Werner Almesberger.
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms of parts of or the
11 # whole original or derived work are permitted provided that the
12 # original work is properly attributed to the author. The name of the
13 # author may not be used to endorse or promote products derived from
14 # this software without specific prior written permission. This work
15 # is provided "as is" and without any express or implied warranties.
16 # --------------------------------------------------------------------------
17
18 eval { use bytes; }; eval { binmode STDOUT; };
19
20 $DEFAULT_PATH = "/usr/lib/kbd/keytables";
21 $DEFAULT_MAP = "us";
22 $DEFAULT_EXT = ".map";
23
24 sub usage
25 {
26     print STDERR
27       "usage: $0 [ -p old_code=new_code ] ...\n".
28       (" "x(8+length $0))."[path]default_layout[.map] ] ".
29       "[path]kbd_layout[.map]\n";
30     exit 1;
31 }
32
33
34 while ($ARGV[0] eq "-p") {
35     shift(@ARGV);
36     &usage unless $ARGV[0] =~ /=/;
37     $table[eval($`)] = eval($');
38     shift(@ARGV);
39 }
40 &usage unless defined $ARGV[0];
41 load_map("def",defined $ARGV[1] ? $ARGV[0] : undef);
42 load_map("kbd",defined $ARGV[1] ? $ARGV[1] : $ARGV[0]);
43 &build_table("plain","shift","ctrl","altgr","shift_ctrl",
44   "altgr_ctrl","alt","shift_alt","ctrl_alt");
45 for ($i = 0; $i < 256; $i++) {
46     printf("%c",$table[$i] ? $table[$i] : $i) || die "print: $!";
47 }
48 close STDOUT || die "close: $!";
49
50
51 sub load_map
52 {
53     local ($pfx,$map) = @_;
54     local ($empty,$current);
55
56     $map = $DEFAULT_MAP unless defined $map;
57     $map = $DEFAULT_PATH."/".$map unless $map =~ m|/|;
58     $map .= $DEFAULT_EXT unless $map =~ m|/[^/]+\.[^/]+$|;
59     if (!open(FILE,"loadkeys -m $map |")) {
60         print STDERR "loadkeys -m $map: $!\n";
61         exit 1;
62     }
63     undef $current;
64     $empty = 1;
65     while (<FILE>) {
66         chop;
67         if (/^u_short\s+(\S+)_map\[\S+\]\s+=\s+{\s*$/) {
68             die "active at beginning of map" if defined $current;
69             $current = $pfx.":".$1;
70             next;
71         }
72         undef $current if /^};\s*$/;
73         next unless defined $current;
74         s/\s//g;
75         $map{$current} .= $_;
76         $empty = 0;
77     }
78     close FILE;
79     return unless $empty;
80     print STDERR "Keymap is empty\n";
81     exit 1;
82 }
83
84
85 sub build_table
86 {
87     local (@maps) = @_;
88     local (@tmp);
89
90     $set = 0;
91     for $map (@maps) {
92         $code = $set;
93         for (split(",",$map{"def:".$map})) {
94             die "bad map entry $_ (def, map $map)" unless /^0x\S\S(\S\S)$/;
95             $tmp[$code] = hex $1 unless $tmp[$code];
96             $code++;
97         }
98         $set += 256;
99     }
100     $set = 0;
101     for $map (@maps) {
102         $code = $set;
103         for (split(",",$map{"kbd:".$map})) {
104             die "bad map entry $_ (kbd, map $map)" unless /^0x\S\S(\S\S)$/;
105             $table[$tmp[$code]] = hex $1 unless $table[$tmp[$code]];
106             $code++;
107         }
108         $set += 256;
109     }
110     $table[0] = 0;
111 }