MKkey_defs.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #! /bin/sh
  2. # $Id: MKkey_defs.sh,v 1.14 2003/12/06 17:10:09 tom Exp $
  3. ##############################################################################
  4. # Copyright (c) 2001-2002,2003 Free Software Foundation, Inc. #
  5. # #
  6. # Permission is hereby granted, free of charge, to any person obtaining a #
  7. # copy of this software and associated documentation files (the "Software"), #
  8. # to deal in the Software without restriction, including without limitation #
  9. # the rights to use, copy, modify, merge, publish, distribute, distribute #
  10. # with modifications, sublicense, and/or sell copies of the Software, and to #
  11. # permit persons to whom the Software is furnished to do so, subject to the #
  12. # following conditions: #
  13. # #
  14. # The above copyright notice and this permission notice shall be included in #
  15. # all copies or substantial portions of the Software. #
  16. # #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
  20. # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
  23. # DEALINGS IN THE SOFTWARE. #
  24. # #
  25. # Except as contained in this notice, the name(s) of the above copyright #
  26. # holders shall not be used in advertising or otherwise to promote the sale, #
  27. # use or other dealings in this Software without prior written #
  28. # authorization. #
  29. ##############################################################################
  30. #
  31. # MKkey_defs.sh -- generate function-key definitions for curses.h
  32. #
  33. # Author: Thomas E. Dickey 2001
  34. #
  35. # Extract function-key definitions from the Caps file
  36. #
  37. : ${AWK-awk}
  38. DATA=${1-Caps}
  39. data=data$$
  40. pass1=pass1_$$
  41. pass2=pass2_$$
  42. pass3=pass3_$$
  43. pass4=pass4_$$
  44. trap 'rm -f $data pass[1234]_$$' 0 1 2 5 15
  45. # change repeated tabs (used for readability) to single tabs (needed to make
  46. # awk see the right field alignment of the corresponding columns):
  47. if sort -k 6 $DATA >$data 2>/dev/null
  48. then
  49. # POSIX
  50. sed -e 's/[ ][ ]*/ /g' < $DATA |sort -n -k 6 >$data
  51. elif sort -n +5 $DATA >$data 2>/dev/null
  52. then
  53. # SunOS (and SVr4, marked as obsolete but still recognized)
  54. sed -e 's/[ ][ ]*/ /g' < $DATA |sort -n +5 >$data
  55. else
  56. echo "Your sort utility is broken. Please install one that works." >&2
  57. exit 1
  58. fi
  59. # add keys that we generate automatically:
  60. cat >>$data <<EOF
  61. key_resize kr1 str R1 KEY_RESIZE + ----- Terminal resize event
  62. key_event kv1 str V1 KEY_EVENT + ----- We were interrupted by an event
  63. EOF
  64. cat <<EOF
  65. /*
  66. * These definitions were generated by $0 $DATA
  67. */
  68. EOF
  69. # KEY_RESET
  70. maxkey=345
  71. for pass in 1 2 3 4
  72. do
  73. output=pass${pass}_$$
  74. ${AWK-awk} >$output <$data '
  75. function print_cols(text,cols) {
  76. printf "%s", text
  77. len = length(text);
  78. while (len < cols) {
  79. printf " "
  80. len += 8;
  81. }
  82. }
  83. function decode(keycode) {
  84. result = 0;
  85. if (substr(keycode, 1, 2) == "0x") {
  86. digits="0123456789abcdef";
  87. } else if (substr(keycode, 1, 1) == "0") {
  88. digits="01234567";
  89. } else {
  90. digits="0123456789";
  91. }
  92. while (length(keycode) != 0) {
  93. digit=substr(keycode, 1, 1);
  94. keycode=substr(keycode, 2);
  95. result = result * length(digits) + index(digits, digit) - 1;
  96. }
  97. return result;
  98. }
  99. BEGIN {
  100. maxkey='$maxkey';
  101. pass='$pass';
  102. key_max=1;
  103. bits=1;
  104. while (key_max < maxkey) {
  105. bits = bits + 1;
  106. key_max = (key_max * 2) + 1;
  107. }
  108. octal_fmt = sprintf ("%%0%do", (bits + 2) / 3 + 1);
  109. }
  110. /^$/ {next;}
  111. /^#/ {next;}
  112. /^capalias/ {next;}
  113. /^infoalias/ {next;}
  114. $5 != "-" && $6 != "-" {
  115. if ($6 == "+") {
  116. if (pass == 1 || pass == 2)
  117. next;
  118. thiskey=maxkey + 1;
  119. } else {
  120. if (pass == 3)
  121. next;
  122. thiskey=decode($6);
  123. }
  124. if (thiskey > maxkey)
  125. maxkey = thiskey;
  126. if (pass == 2 || pass == 3) {
  127. showkey=sprintf(octal_fmt, thiskey);
  128. if ($5 == "KEY_F(0)" ) {
  129. printf "#define "
  130. print_cols("KEY_F0", 16);
  131. print_cols(showkey, 16);
  132. print "/* Function keys. Space for 64 */";
  133. printf "#define "
  134. print_cols("KEY_F(n)", 16);
  135. print_cols("(KEY_F0+(n))", 16);
  136. print "/* Value of function key n */"
  137. } else {
  138. printf "#define "
  139. print_cols($5, 16);
  140. print_cols(showkey, 16);
  141. printf "/*"
  142. for (i = 8; i <= NF; i++)
  143. printf " %s", $i
  144. print " */"
  145. }
  146. }
  147. }
  148. END {
  149. if (pass == 1) {
  150. print maxkey;
  151. } else if (pass == 4) {
  152. print "";
  153. printf "#define ";
  154. print_cols("KEY_MAX", 16);
  155. result = sprintf (octal_fmt, key_max);
  156. print_cols(result, 16);
  157. printf "/* Maximum key value is ";
  158. printf octal_fmt, maxkey;
  159. print " */";
  160. }
  161. }
  162. '
  163. if test $pass = 1 ; then
  164. maxkey=`cat $pass1`
  165. fi
  166. done
  167. cat $pass2
  168. cat $pass3
  169. cat $pass4