3 # dselect - Debian package maintenance user interface
4 # mkcurkeys.pl - generate strings mapping key names to ncurses numbers
6 # Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
8 # This is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
24 use Scalar
::Util
qw(looks_like_number);
26 die 'usage: mkcurkeys.pl <filename> <curses.h>' if @ARGV != 2;
28 my (%over, %base, %name);
30 open(my $override_fh, '<', $ARGV[0]) or die $!;
31 while (<$override_fh>) {
33 /^#/ && next; # skip comments
34 /\S/ || next; # ignore blank lines
35 if (/^(\w+)\s+(\S.*\S)\s*$/) {
39 die "cannot parse line:\n$_\n";
51 open(my $header_fh, '<', $ARGV[1]) or die $!;
52 while (<$header_fh>) {
54 m/#define KEY_(\w+)\s+\d+\s+/p || next;
55 my $rhs = ${^POSTMATCH
};
57 $base{$k} = capit
($1);
58 $rhs =~ s/(\w)[\(\)]/$1/g;
59 $rhs =~ s/\w+ \((\w+)\)/$1/;
60 next unless $rhs =~ m{^/\* (\w[\w ]+\w) \*/$};
63 if ($name =~ s/^shifted /shift /) {
64 next if $name =~ m/ .* .* /;
66 next if $name =~ m/ .* /;
68 $name{$k} = capit
($name);
72 printf(<<'END') or die $!;
74 * WARNING - THIS FILE IS GENERATED AUTOMATICALLY - DO NOT EDIT
75 * It is generated by mkcurkeys.pl from <curses.h>
76 * and keyoverride. If you want to override things try adding
77 * them to keyoverride.
84 for my $i (33 .. 126) {
86 my $v = pack('C', $i);
87 if ($v eq ',') { $comma = $k; next; }
91 ## no critic (BuiltinFunctions::ProhibitReverseSortBlock)
93 looks_like_number
($a) ?
94 looks_like_number
($b) ?
$a <=> $b : -1
95 : looks_like_number
($b) ?
1 :
101 $v = $name{$k} if defined($name{$k});
102 $v = $over{$k} if defined($over{$k});
103 next if $v eq '[elide]';
107 for my $i (1 .. 63) {
108 p
("KEY_F($i)", "F$i");
113 print(<<'END') or die $!;
117 close(STDOUT
) or die $!;
126 while ($str =~ m/ (\w)/p) {
127 $o .= ${^PREMATCH
} . ' ';
131 $str = ${^POSTMATCH
};
142 $v =~ s/(["\\])/\\$1/g;
143 printf(" { %-15s \"%-20s },\n", $k . ',', $v . '"') or die $!;