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