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";
53 open(my $header_fh, '<', $ARGV[1]) or die $!;
54 while (<$header_fh>) {
56 m/#define KEY_(\w+)\s+\d+\s+/p || next;
57 my $rhs = ${^POSTMATCH};
59 $base{$k} = capit($1);
60 $rhs =~ s/(\w)[\(\)]/$1/g;
61 $rhs =~ s/\w+ \((\w+)\)/$1/;
62 next unless $rhs =~ m{^/\* (\w[\w ]+\w) \*/$};
65 if ($name =~ s/^shifted /shift /) {
66 next if $name =~ m/ .* .* /;
68 next if $name =~ m/ .* /;
70 $name{$k} = capit($name);
74 printf(<<'END') or die $!;
76 * WARNING - THIS FILE IS GENERATED AUTOMATICALLY - DO NOT EDIT
77 * It is generated by mkcurkeys.pl from <curses.h>
78 * and keyoverride. If you want to override things try adding
79 * them to keyoverride.
86 for my $i (33 .. 126) {
89 if ($v eq ',') { $comma=$k; next; }
93 ## no critic (BuiltinFunctions::ProhibitReverseSortBlock)
95 looks_like_number($a) ?
96 looks_like_number($b) ? $a <=> $b : -1
97 : looks_like_number($b) ? 1 :
102 $v= $name{$k} if defined($name{$k});
103 $v= $over{$k} if defined($over{$k});
104 next if $v eq '[elide]';
108 for my $i (1 .. 63) {
109 p("KEY_F($i)", "F$i");
114 print(<<'END') or die $!;
118 close(STDOUT) or die $!;
127 while ($str =~ m/ (\w)/p) {
128 $o .= ${^PREMATCH} . ' ';
132 $str = ${^POSTMATCH};
143 $v =~ s/(["\\])/\\$1/g;
144 printf(" { %-15s \"%-20s },\n", $k . ',', $v . '"') or die $!;