1#!/bin/sh
2# $Id: MKtermsort.sh,v 1.17 2022/02/05 20:39:41 tom Exp $
3#
4# MKtermsort.sh -- generate indirection vectors for the various sort methods
5#
6##############################################################################
7# Copyright 2020-2021,2022 Thomas E. Dickey                                  #
8# Copyright 1998-2015,2017 Free Software Foundation, Inc.                    #
9#                                                                            #
10# Permission is hereby granted, free of charge, to any person obtaining a    #
11# copy of this software and associated documentation files (the "Software"), #
12# to deal in the Software without restriction, including without limitation  #
13# the rights to use, copy, modify, merge, publish, distribute, distribute    #
14# with modifications, sublicense, and/or sell copies of the Software, and to #
15# permit persons to whom the Software is furnished to do so, subject to the  #
16# following conditions:                                                      #
17#                                                                            #
18# The above copyright notice and this permission notice shall be included in #
19# all copies or substantial portions of the Software.                        #
20#                                                                            #
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
22# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
23# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
24# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
25# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
26# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
27# DEALINGS IN THE SOFTWARE.                                                  #
28#                                                                            #
29# Except as contained in this notice, the name(s) of the above copyright     #
30# holders shall not be used in advertising or otherwise to promote the sale, #
31# use or other dealings in this Software without prior written               #
32# authorization.                                                             #
33##############################################################################
34#
35# The output of this script is C source for nine arrays that list three sort
36# orders for each of the three different classes of terminfo capabilities.
37#
38# keep the order independent of locale:
39if test "${LANGUAGE+set}"    = set; then LANGUAGE=C;    export LANGUAGE;    fi
40if test "${LANG+set}"        = set; then LANG=C;        export LANG;        fi
41if test "${LC_ALL+set}"      = set; then LC_ALL=C;      export LC_ALL;      fi
42if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
43if test "${LC_CTYPE+set}"    = set; then LC_CTYPE=C;    export LC_CTYPE;    fi
44if test "${LC_COLLATE+set}"  = set; then LC_COLLATE=C;  export LC_COLLATE;  fi
45#
46AWK=${1-awk}
47DATA=${2-../include/Caps}
48
49data=data$$
50trap 'rm -f $data; exit 1' 1 2 3 15
51sed -e 's/[	][	]*/	/g' < "$DATA" >$data
52DATA=$data
53
54cat <<EOF
55/*
56 * termsort.h --- sort order arrays for use by infocmp.
57 *
58 * Note: this file is generated using MKtermsort.sh, do not edit by hand.
59 */
60#ifndef _TERMSORT_H
61#define _TERMSORT_H 1
62#include <curses.h>
63
64#ifndef DUMP_ENTRY_H
65typedef unsigned PredType;
66typedef unsigned PredIdx;
67#endif
68
69EOF
70
71echo "static const PredIdx bool_terminfo_sort[] = {";
72$AWK <$DATA '
73BEGIN           {i = 0;}
74/^#/            {next;}
75$3 == "bool"    {printf("%s\t%d\n", $2, i++);}
76' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
77echo "};";
78echo "";
79
80echo "static const PredIdx num_terminfo_sort[] = {";
81$AWK <$DATA '
82BEGIN           {i = 0;}
83/^#/            {next;}
84$3 == "num"     {printf("%s\t%d\n", $2, i++);}
85' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
86echo "};";
87echo "";
88
89echo "static const PredIdx str_terminfo_sort[] = {";
90$AWK <$DATA '
91BEGIN           {i = 0;}
92/^#/            {next;}
93$3 == "str"     {printf("%s\t%d\n", $2, i++);}
94' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
95echo "};";
96echo "";
97
98echo "static const PredIdx bool_variable_sort[] = {";
99$AWK <$DATA '
100BEGIN           {i = 0;}
101/^#/            {next;}
102$3 == "bool"    {printf("%s\t%d\n", $1, i++);}
103' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
104echo "};";
105echo "";
106
107echo "static const PredIdx num_variable_sort[] = {";
108$AWK <$DATA '
109BEGIN           {i = 0;}
110/^#/            {next;}
111$3 == "num"     {printf("%s\t%d\n", $1, i++);}
112' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
113echo "};";
114echo "";
115
116echo "static const PredIdx str_variable_sort[] = {";
117$AWK <$DATA '
118BEGIN           {i = 0;}
119/^#/            {next;}
120$3 == "str"     {printf("%s\t%d\n", $1, i++);}
121' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
122echo "};";
123echo "";
124
125echo "static const PredIdx bool_termcap_sort[] = {";
126$AWK <$DATA '
127BEGIN           {i = 0;}
128/^#/            {next;}
129$3 == "bool"    {printf("%s\t%d\n", $4, i++);}
130' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
131echo "};";
132echo "";
133
134echo "static const PredIdx num_termcap_sort[] = {";
135$AWK <$DATA '
136BEGIN           {i = 0;}
137/^#/            {next;}
138$3 == "num"     {printf("%s\t%d\n", $4, i++);}
139' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
140echo "};";
141echo "";
142
143echo "static const PredIdx str_termcap_sort[] = {";
144$AWK <$DATA '
145BEGIN           {i = 0;}
146/^#/            {next;}
147$3 == "str"     {printf("%s\t%d\n", $4, i++);}
148' | sort | $AWK '{print "\t", $2, ",\t/* ", $1, " */";}';
149echo "};";
150echo "";
151
152echo "static const bool bool_from_termcap[] = {";
153$AWK <$DATA '
154BEGIN { count = 0; valid = 0; }
155$3 == "bool" && substr($7, 1, 1) == "-"       {print "\tFALSE,\t/* ", $2, " */"; count++; }
156$3 == "bool" && substr($7, 1, 1) == "Y"       {print "\tTRUE,\t/* ", $2, " */"; valid = count++; }
157END { printf "#define OK_bool_from_termcap %d\n", valid; }
158'
159echo "};";
160echo "";
161
162echo "static const bool num_from_termcap[] = {";
163$AWK <$DATA '
164BEGIN { count = 0; valid = 0; }
165$3 == "num" && substr($7, 1, 1) == "-"        {print "\tFALSE,\t/* ", $2, " */"; count++; }
166$3 == "num" && substr($7, 1, 1) == "Y"        {print "\tTRUE,\t/* ", $2, " */"; valid = count++; }
167END { printf "#define OK_num_from_termcap %d\n", valid; }
168'
169echo "};";
170echo "";
171
172echo "static const bool str_from_termcap[] = {";
173$AWK <$DATA '
174BEGIN { count = 0; valid = 0; }
175$3 == "str" && substr($7, 1, 1) == "-"        {print "\tFALSE,\t/* ", $2, " */"; count++; }
176$3 == "str" && substr($7, 1, 1) == "Y"        {print "\tTRUE,\t/* ", $2, " */"; valid = count++; }
177END { printf "#define OK_str_from_termcap %d\n", valid; }
178'
179
180cat <<EOF
181};
182
183#endif /* _TERMSORT_H */
184EOF
185
186rm -f $data
187