xref: /freebsd/contrib/ncurses/include/MKkey_defs.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1#! /bin/sh
2# $Id: MKkey_defs.sh,v 1.21 2020/08/17 10:45:33 tom Exp $
3##############################################################################
4# Copyright 2019,2020 Thomas E. Dickey                                       #
5# Copyright 2001-2013,2017 Free Software Foundation, Inc.                    #
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#
38: ${AWK-awk}
39
40test $# = 0 && set Caps
41
42data=data$$
43pass1=pass1_$$
44pass2=pass2_$$
45pass3=pass3_$$
46pass4=pass4_$$
47trap 'rm -f $data pass[1234]_$$' EXIT INT QUIT TERM HUP
48
49# change repeated tabs (used for readability) to single tabs (needed to make
50# awk see the right field alignment of the corresponding columns):
51if sort -k 6 "$@" >$data 2>/dev/null
52then
53	# POSIX
54	sed -e 's/[	][	]*/	/g' "$@" |sort -n -k 6 >$data
55elif sort -n +5 "$@" >$data 2>/dev/null
56then
57	# SunOS (and SVr4, marked as obsolete but still recognized)
58	sed -e 's/[	][	]*/	/g' "$@" |sort -n +5 >$data
59else
60	echo "Your sort utility is broken.  Please install one that works." >&2
61	exit 1
62fi
63
64# add keys that we generate automatically:
65cat >>$data <<EOF
66key_resize	kr1	str	R1	KEY_RESIZE	+	NCURSES_EXT_FUNCS 	Terminal resize event
67EOF
68
69THIS=./`basename $0`
70
71cat <<EOF
72/*
73 * These definitions were generated by $THIS $*
74 */
75EOF
76
77# KEY_RESET
78maxkey=345
79
80for pass in 1 2 3 4
81do
82
83output=pass${pass}_$$
84
85${AWK-awk} >$output <$data '
86function print_cols(text,cols) {
87	printf "%s", text
88	len = length(text);
89	while (len < cols) {
90		printf "	"
91		len += 8;
92	}
93}
94function decode(keycode) {
95	result = 0;
96	if (substr(keycode, 1, 2) == "0x") {
97		digits="0123456789abcdef";
98	} else if (substr(keycode, 1, 1) == "0") {
99		digits="01234567";
100	} else {
101		digits="0123456789";
102	}
103	while (length(keycode) != 0) {
104		digit=substr(keycode, 1, 1);
105		keycode=substr(keycode, 2);
106		result = result * length(digits) + index(digits, digit) - 1;
107	}
108	return result;
109}
110
111BEGIN	{
112	maxkey='$maxkey';
113	pass='$pass';
114	key_max=1;
115	bits=1;
116	while (key_max < maxkey) {
117		bits = bits + 1;
118		key_max = (key_max * 2) + 1;
119	}
120	octal_fmt = sprintf ("%%0%do", (bits + 2) / 3 + 1);
121}
122
123/^$/		{next;}
124/^#/		{next;}
125/^capalias/	{next;}
126/^infoalias/	{next;}
127/^used_by/	{next;}
128/^userdef/	{next;}
129
130$5 != "-" && $6 != "-" {
131		if ($6 == "+") {
132			if (pass == 1 || pass == 2)
133				next;
134			thiskey=maxkey + 1;
135		} else {
136			if (pass == 3)
137				next;
138			thiskey=decode($6);
139		}
140		if (thiskey > maxkey)
141			maxkey = thiskey;
142		if (pass == 2 || pass == 3) {
143			showkey=sprintf(octal_fmt, thiskey);
144			ifdef = 0;
145			if (index($7,"NCURSES_") == 1) {
146				ifdef = 1;
147				printf "\n";
148				printf "#ifdef %s\n", $7;
149			}
150			if ($5 == "KEY_F(0)" ) {
151				printf "#define "
152				print_cols("KEY_F0", 16);
153				print_cols(showkey, 16);
154				print "/* Function keys.  Space for 64 */";
155				printf "#define "
156				print_cols("KEY_F(n)", 16);
157				print_cols("(KEY_F0+(n))", 16);
158				print "/* Value of function key n */"
159			} else {
160				printf "#define "
161				print_cols($5, 16);
162				print_cols(showkey, 16);
163				printf "/*"
164				for (i = 8; i <= NF; i++)
165					printf " %s", $i
166				print " */"
167			}
168			if (ifdef != 0) {
169				printf "#endif\n";
170			}
171		}
172	}
173END	{
174		if (pass == 1) {
175			print maxkey;
176		} else if (pass == 4) {
177			print "";
178			printf "#define ";
179			print_cols("KEY_MAX", 16);
180			result = sprintf (octal_fmt, key_max);
181			print_cols(result, 16);
182			printf "/* Maximum key value is ";
183			printf octal_fmt, maxkey;
184			print " */";
185		}
186	}
187'
188if test $pass = 1 ; then
189	maxkey=`cat $pass1`
190fi
191
192done
193
194cat $pass2
195cat $pass3
196cat $pass4
197