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