mkcurkeys.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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
  19. # License along with this; if not, write to the Free Software
  20. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  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. $a+0 eq $a ?
  72. $b+0 eq $b ? $a <=> $b : -1
  73. : $b+0 eq $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; $v=','; &p;
  87. print(<<'END') || die $!;
  88. { -1, 0 }
  89. END
  90. close(STDOUT) || die $!;
  91. exit(0);
  92. sub capit {
  93. $o= ''; y/A-Z/a-z/; $_= " $_";
  94. while (m/ (\w)/) {
  95. $o .= $`.' ';
  96. $_ = $1;
  97. y/a-z/A-Z/;
  98. $o .= $_;
  99. $_ = $';
  100. }
  101. $_= $o.$_; s/^ //;
  102. }
  103. sub p {
  104. $v =~ s/["\\]/\\$&/g;
  105. printf(" { %-15s \"%-20s },\n",
  106. $k.',',
  107. $v.'"') || die $!;
  108. }