mkcurkeys.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/perl --
  2. #
  3. # dselect - Debian package maintenance user interface
  4. # mkcurkeys.pl - generate strings mapping key names to ncurses numbers
  5. #
  6. # Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  7. #
  8. # This is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as
  10. # published by the Free Software Foundation; either version 2,
  11. # or (at your option) any later version.
  12. #
  13. # This is distributed in the hope that it will be useful, but
  14. # 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.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. $#ARGV == 1 || die ("usage: mkcurkeys.pl <filename> <curses.h>");
  21. open(OV,"<$ARGV[0]") || die $!;
  22. while (<OV>) {
  23. chomp;
  24. /^#/ && next; # skip comments
  25. /\S/ || next; # ignore blank lines
  26. /^(\w+)\s+(\S.*\S)\s*$/ || die ("cannot parse line:\n$_\n");
  27. $over{$1}= $2;
  28. $base{$1}= '';
  29. }
  30. close(OV);
  31. for ($i=1, $let='A'; $i<=26; $i++, $let++) {
  32. $name{$i}= "^$let";
  33. $base{$i}= '';
  34. }
  35. open(NCH,"<$ARGV[1]") || die $!;
  36. while (<NCH>) {
  37. s/\s+$//;
  38. m/#define KEY_(\w+)\s+\d+\s+/ || next;
  39. $rhs= $';
  40. $k= "KEY_$1";
  41. $_= $1;
  42. &capit;
  43. $base{$k}= $_;
  44. $_= $rhs;
  45. s/(\w)[\(\)]/$1/g;
  46. s/\w+ \((\w+)\)/$1/;
  47. next unless m|^/\* (\w[\w ]+\w) \*/$|;
  48. $_= $1;
  49. s/ key$//;
  50. next if s/^shifted /shift / ? m/ .* .* / : m/ .* /;
  51. &capit;
  52. $name{$k}= $_;
  53. }
  54. close(NCH);
  55. printf(<<'END') || die $!;
  56. /*
  57. * WARNING - THIS FILE IS GENERATED AUTOMATICALLY - DO NOT EDIT
  58. * It is generated by mkcurkeys.pl from <curses.h>
  59. * and keyoverride. If you want to override things try adding
  60. * them to keyoverride.
  61. */
  62. END
  63. for ($i=33; $i<=126; $i++) {
  64. $k= $i;
  65. $v= pack("C",$i);
  66. if ($v eq ',') { $comma=$k; next; }
  67. &p;
  68. }
  69. for $k (sort {
  70. $a+0 eq $a ?
  71. $b+0 eq $b ? $a <=> $b : -1
  72. : $b+0 eq $b ? 1 :
  73. $a cmp $b
  74. } keys %base) {
  75. $v= $base{$k};
  76. $v= $name{$k} if defined($name{$k});
  77. $v= $over{$k} if defined($over{$k});
  78. next if $v eq '[elide]';
  79. &p;
  80. }
  81. for ($i=1; $i<64; $i++) {
  82. $k= "KEY_F($i)"; $v= "F$i";
  83. &p;
  84. }
  85. $k=$comma; $v=','; &p;
  86. print(<<'END') || die $!;
  87. { -1, 0 }
  88. END
  89. close(STDOUT) || die $!;
  90. exit(0);
  91. sub capit {
  92. $o= ''; y/A-Z/a-z/; $_= " $_";
  93. while (m/ (\w)/) {
  94. $o .= $`.' ';
  95. $_ = $1;
  96. y/a-z/A-Z/;
  97. $o .= $_;
  98. $_ = $';
  99. }
  100. $_= $o.$_; s/^ //;
  101. }
  102. sub p {
  103. $v =~ s/["\\]/\\$&/g;
  104. printf(" { %-15s \"%-20s },\n",
  105. $k.',',
  106. $v.'"') || die $!;
  107. }