summaryrefslogtreecommitdiff
path: root/include/MKkey_defs.sh
blob: b3cb4f7f72cbd6b721a2d48a0d7e6dc5f5582814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#! /bin/sh
# $Id: MKkey_defs.sh,v 1.14 2003/12/06 17:10:09 tom Exp $
##############################################################################
# Copyright (c) 2001-2002,2003 Free Software Foundation, Inc.                #
#                                                                            #
# Permission is hereby granted, free of charge, to any person obtaining a    #
# copy of this software and associated documentation files (the "Software"), #
# to deal in the Software without restriction, including without limitation  #
# the rights to use, copy, modify, merge, publish, distribute, distribute    #
# with modifications, sublicense, and/or sell copies of the Software, and to #
# permit persons to whom the Software is furnished to do so, subject to the  #
# following conditions:                                                      #
#                                                                            #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software.                        #
#                                                                            #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
# DEALINGS IN THE SOFTWARE.                                                  #
#                                                                            #
# Except as contained in this notice, the name(s) of the above copyright     #
# holders shall not be used in advertising or otherwise to promote the sale, #
# use or other dealings in this Software without prior written               #
# authorization.                                                             #
##############################################################################
#
# MKkey_defs.sh -- generate function-key definitions for curses.h
#
# Author: Thomas E. Dickey 2001
#
# Extract function-key definitions from the Caps file
#
: ${AWK-awk}
DATA=${1-Caps}

data=data$$
pass1=pass1_$$
pass2=pass2_$$
pass3=pass3_$$
pass4=pass4_$$
trap 'rm -f $data pass[1234]_$$' 0 1 2 5 15

# change repeated tabs (used for readability) to single tabs (needed to make
# awk see the right field alignment of the corresponding columns):
if sort -k 6 $DATA >$data 2>/dev/null
then
	# POSIX
	sed -e 's/[	][	]*/	/g' < $DATA |sort -n -k 6 >$data
elif sort -n +5 $DATA >$data 2>/dev/null
then
	# SunOS (and SVr4, marked as obsolete but still recognized)
	sed -e 's/[	][	]*/	/g' < $DATA |sort -n +5 >$data
else
	echo "Your sort utility is broken.  Please install one that works." >&2
	exit 1
fi

# add keys that we generate automatically:
cat >>$data <<EOF
key_resize	kr1	str	R1	KEY_RESIZE	+	-----	Terminal resize event
key_event	kv1	str	V1	KEY_EVENT	+	-----	We were interrupted by an event
EOF

cat <<EOF
/*
 * These definitions were generated by $0 $DATA
 */
EOF

# KEY_RESET
maxkey=345

for pass in 1 2 3 4
do

output=pass${pass}_$$

${AWK-awk} >$output <$data '
function print_cols(text,cols) {
	printf "%s", text
	len = length(text);
	while (len < cols) {
		printf "	"
		len += 8;
	}
}
function decode(keycode) {
	result = 0;
	if (substr(keycode, 1, 2) == "0x") {
		digits="0123456789abcdef";
	} else if (substr(keycode, 1, 1) == "0") {
		digits="01234567";
	} else {
		digits="0123456789";
	}
	while (length(keycode) != 0) {
		digit=substr(keycode, 1, 1);
		keycode=substr(keycode, 2);
		result = result * length(digits) + index(digits, digit) - 1;
	}
	return result;
}

BEGIN	{
	maxkey='$maxkey';
	pass='$pass';
	key_max=1;
	bits=1;
	while (key_max < maxkey) {
		bits = bits + 1;
		key_max = (key_max * 2) + 1;
	}
	octal_fmt = sprintf ("%%0%do", (bits + 2) / 3 + 1);
}

/^$/		{next;}
/^#/		{next;}
/^capalias/	{next;}
/^infoalias/	{next;}

$5 != "-" && $6 != "-" {
		if ($6 == "+") {
			if (pass == 1 || pass == 2)
				next;
			thiskey=maxkey + 1;
		} else {
			if (pass == 3)
				next;
			thiskey=decode($6);
		}
		if (thiskey > maxkey)
			maxkey = thiskey;
		if (pass == 2 || pass == 3) {
			showkey=sprintf(octal_fmt, thiskey);
			if ($5 == "KEY_F(0)" ) {
				printf "#define "
				print_cols("KEY_F0", 16);
				print_cols(showkey, 16);
				print "/* Function keys.  Space for 64 */";
				printf "#define "
				print_cols("KEY_F(n)", 16);
				print_cols("(KEY_F0+(n))", 16);
				print "/* Value of function key n */"
			} else {
				printf "#define "
				print_cols($5, 16);
				print_cols(showkey, 16);
				printf "/*"
				for (i = 8; i <= NF; i++)
					printf " %s", $i
				print " */"
			}
		}
	}
END	{
		if (pass == 1) {
			print maxkey;
		} else if (pass == 4) {
			print "";
			printf "#define ";
			print_cols("KEY_MAX", 16);
			result = sprintf (octal_fmt, key_max);
			print_cols(result, 16);
			printf "/* Maximum key value is ";
			printf octal_fmt, maxkey;
			print " */";
		}
	}
'
if test $pass = 1 ; then
	maxkey=`cat $pass1`
fi

done

cat $pass2
cat $pass3
cat $pass4