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