mkcurkeys.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  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.
  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. use Scalar::Util qw(looks_like_number);
  21. $#ARGV == 1 || die ("usage: mkcurkeys.pl <filename> <curses.h>");
  22. open(OV,"<$ARGV[0]") || die $!;
  23. while (<OV>) {
  24. chomp;
  25. /^#/ && next; # skip comments
  26. /\S/ || next; # ignore blank lines
  27. /^(\w+)\s+(\S.*\S)\s*$/ || die ("cannot parse line:\n$_\n");
  28. $over{$1}= $2;
  29. $base{$1}= '';
  30. }
  31. close(OV);
  32. for ($i=1, $let='A'; $i<=26; $i++, $let++) {
  33. $name{$i}= "^$let";
  34. $base{$i}= '';
  35. }
  36. open(NCH,"<$ARGV[1]") || die $!;
  37. while (<NCH>) {
  38. s/\s+$//;
  39. m/#define KEY_(\w+)\s+\d+\s+/ || next;
  40. $rhs= $';
  41. $k= "KEY_$1";
  42. $_= $1;
  43. capit();
  44. $base{$k}= $_;
  45. $_= $rhs;
  46. s/(\w)[\(\)]/$1/g;
  47. s/\w+ \((\w+)\)/$1/;
  48. next unless m|^/\* (\w[\w ]+\w) \*/$|;
  49. $_= $1;
  50. s/ key$//;
  51. next if s/^shifted /shift / ? m/ .* .* / : m/ .* /;
  52. capit();
  53. $name{$k}= $_;
  54. }
  55. close(NCH);
  56. printf(<<'END') || die $!;
  57. /*
  58. * WARNING - THIS FILE IS GENERATED AUTOMATICALLY - DO NOT EDIT
  59. * It is generated by mkcurkeys.pl from <curses.h>
  60. * and keyoverride. If you want to override things try adding
  61. * them to keyoverride.
  62. */
  63. END
  64. for ($i=33; $i<=126; $i++) {
  65. $k= $i;
  66. $v= pack("C",$i);
  67. if ($v eq ',') { $comma=$k; next; }
  68. p();
  69. }
  70. for $k (sort {
  71. looks_like_number($a) ?
  72. looks_like_number($b) ? $a <=> $b : -1
  73. : looks_like_number($b) ? 1 :
  74. $a cmp $b
  75. } keys %base) {
  76. $v= $base{$k};
  77. $v= $name{$k} if defined($name{$k});
  78. $v= $over{$k} if defined($over{$k});
  79. next if $v eq '[elide]';
  80. p();
  81. }
  82. for ($i=1; $i<64; $i++) {
  83. $k= "KEY_F($i)"; $v= "F$i";
  84. p();
  85. }
  86. $k = $comma;
  87. $v = ',';
  88. p();
  89. print(<<'END') || die $!;
  90. { -1, 0 }
  91. END
  92. close(STDOUT) || die $!;
  93. exit(0);
  94. sub capit {
  95. $o= ''; y/A-Z/a-z/; $_= " $_";
  96. while (m/ (\w)/) {
  97. $o .= $`.' ';
  98. $_ = $1;
  99. y/a-z/A-Z/;
  100. $o .= $_;
  101. $_ = $';
  102. }
  103. $_= $o.$_; s/^ //;
  104. }
  105. sub p {
  106. $v =~ s/["\\]/\\$&/g;
  107. printf(" { %-15s \"%-20s },\n",
  108. $k.',',
  109. $v.'"') || die $!;
  110. }