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.24 2020/02/08 21:52:37 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
47shift
48
49infocmp_path=$1
50shift
51
52case $tic_path in #(vi
53/*)
54	tic_head=`echo "$tic_path" | sed -e 's,/[^/]*$,,'`
55	PATH=$tic_head:$PATH
56	export PATH
57	;;
58esac
59
60if test $# != 0 ; then
61	tmp_info=tmp_info
62	echo creating temporary terminfo directory... >&2
63
64	TERMINFO=`pwd`/$tmp_info
65	export TERMINFO
66
67	TERMINFO_DIRS=$TERMINFO:$terminfo_dir
68	export TERMINFO_DIRS
69
70	$tic_path -x $terminfo_src >&2
71else
72	tmp_info=
73fi
74
75cat <<EOF
76/* This file was generated by $0 */
77
78/*
79 * DO NOT EDIT THIS FILE BY HAND!
80 */
81
82#include <curses.priv.h>
83
84EOF
85
86if [ "$*" ]
87then
88	cat <<EOF
89#include <tic.h>
90
91/* fallback entries for: $* */
92EOF
93	for x in $*
94	do
95		echo "/* $x */"
96		$infocmp_path -E $x | sed -e 's/\<short\>/NCURSES_INT2/g'
97	done
98
99	cat <<EOF
100static const TERMTYPE2 fallbacks[$#] =
101{
102EOF
103	comma=""
104	for x in $*
105	do
106		echo "$comma /* $x */"
107		$infocmp_path -e $x
108		comma=","
109	done
110
111	cat <<EOF
112};
113
114EOF
115fi
116
117cat <<EOF
118NCURSES_EXPORT(const TERMTYPE2 *)
119_nc_fallback2 (const char *name GCC_UNUSED)
120{
121EOF
122
123if [ "$*" ]
124then
125	cat <<EOF
126    const TERMTYPE2	*tp;
127
128    for (tp = fallbacks;
129	 tp < fallbacks + sizeof(fallbacks)/sizeof(TERMTYPE2);
130	 tp++) {
131	if (_nc_name_match(tp->term_names, name, "|")) {
132	    return(tp);
133	}
134    }
135EOF
136else
137	echo "	/* the fallback list is empty */";
138fi
139
140cat <<EOF
141    return((const TERMTYPE2 *)0);
142}
143
144#if NCURSES_EXT_NUMBERS
145#undef _nc_fallback
146
147/*
148 * This entrypoint is used by tack 1.07
149 */
150NCURSES_EXPORT(const TERMTYPE *)
151_nc_fallback (const char *name)
152{
153    const TERMTYPE2 *tp = _nc_fallback2(name);
154    const TERMTYPE *result = 0;
155    if (tp != 0) {
156	static TERMTYPE temp;
157	_nc_export_termtype2(&temp, tp);
158	result = &temp;
159    }
160    return result;
161}
162#endif
163EOF
164
165if test -n "$tmp_info" ; then
166	echo removing temporary terminfo directory... >&2
167	rm -rf $tmp_info
168fi
169