1#!/bin/sh
2##############################################################################
3# Copyright 2020 Thomas E. Dickey                                            #
4# Copyright 1998-2019,2020 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# $Id: MKfallback.sh,v 1.25 2020/08/16 15:58:44 tom Exp $
31#
32# MKfallback.sh -- create fallback table for entry reads
33#
34# This script generates source code for a custom version of read_entry.c
35# that (instead of reading capabilities for an argument terminal type
36# from an on-disk terminfo tree) tries to match the type with one of a
37# specified list of types generated in.
38#
39
40terminfo_dir=$1
41shift
42
43terminfo_src=$1
44shift
45
46tic_path=$1
47test -z "$tic_path" && tic_path=tic
48shift
49
50infocmp_path=$1
51test -z "$infocmp_path" && infocmp_path=infocmp
52shift
53
54case "$tic_path" in #(vi
55/*)
56	tic_head=`echo "$tic_path" | sed -e 's,/[^/]*$,,'`
57	PATH=$tic_head:$PATH
58	export PATH
59	;;
60esac
61
62if test $# != 0 ; then
63	tmp_info=tmp_info
64	echo creating temporary terminfo directory... >&2
65
66	TERMINFO=`pwd`/$tmp_info
67	export TERMINFO
68
69	TERMINFO_DIRS=$TERMINFO:$terminfo_dir
70	export TERMINFO_DIRS
71
72	"$tic_path" -x "$terminfo_src" >&2
73else
74	tmp_info=
75fi
76
77cat <<EOF
78/* This file was generated by $0 */
79
80/*
81 * DO NOT EDIT THIS FILE BY HAND!
82 */
83
84#include <curses.priv.h>
85
86EOF
87
88if [ "$*" ]
89then
90	cat <<EOF
91#include <tic.h>
92
93/* fallback entries for: $* */
94EOF
95	for x in "$@"
96	do
97		echo "/* $x */"
98		"$infocmp_path" -E "$x" | sed -e 's/\<short\>/NCURSES_INT2/g'
99	done
100
101	cat <<EOF
102static const TERMTYPE2 fallbacks[$#] =
103{
104EOF
105	comma=""
106	for x in "$@"
107	do
108		echo "$comma /* $x */"
109		"$infocmp_path" -e "$x"
110		comma=","
111	done
112
113	cat <<EOF
114};
115
116EOF
117fi
118
119cat <<EOF
120NCURSES_EXPORT(const TERMTYPE2 *)
121_nc_fallback2 (const char *name GCC_UNUSED)
122{
123EOF
124
125if [ "$*" ]
126then
127	cat <<EOF
128    const TERMTYPE2	*tp;
129
130    for (tp = fallbacks;
131	 tp < fallbacks + sizeof(fallbacks)/sizeof(TERMTYPE2);
132	 tp++) {
133	if (_nc_name_match(tp->term_names, name, "|")) {
134	    return(tp);
135	}
136    }
137EOF
138else
139	echo "	/* the fallback list is empty */";
140fi
141
142cat <<EOF
143    return((const TERMTYPE2 *)0);
144}
145
146#if NCURSES_EXT_NUMBERS
147#undef _nc_fallback
148
149/*
150 * This entrypoint is used by tack 1.07
151 */
152NCURSES_EXPORT(const TERMTYPE *)
153_nc_fallback (const char *name)
154{
155    const TERMTYPE2 *tp = _nc_fallback2(name);
156    const TERMTYPE *result = 0;
157    if (tp != 0) {
158	static TERMTYPE temp;
159	_nc_export_termtype2(&temp, tp);
160	result = &temp;
161    }
162    return result;
163}
164#endif
165EOF
166
167if test -n "$tmp_info" ; then
168	echo removing temporary terminfo directory... >&2
169	rm -rf $tmp_info
170fi
171