1/* $OpenBSD: lib_ti.c,v 1.8 2023/10/17 09:52:09 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright 2018,2020 Thomas E. Dickey                                     *
5 * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
6 *                                                                          *
7 * Permission is hereby granted, free of charge, to any person obtaining a  *
8 * copy of this software and associated documentation files (the            *
9 * "Software"), to deal in the Software without restriction, including      *
10 * without limitation the rights to use, copy, modify, merge, publish,      *
11 * distribute, distribute with modifications, sublicense, and/or sell       *
12 * copies of the Software, and to permit persons to whom the Software is    *
13 * furnished to do so, subject to the following conditions:                 *
14 *                                                                          *
15 * The above copyright notice and this permission notice shall be included  *
16 * in all copies or substantial portions of the Software.                   *
17 *                                                                          *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25 *                                                                          *
26 * Except as contained in this notice, the name(s) of the above copyright   *
27 * holders shall not be used in advertising or otherwise to promote the     *
28 * sale, use or other dealings in this Software without prior written       *
29 * authorization.                                                           *
30 ****************************************************************************/
31
32/****************************************************************************
33 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
34 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
35 *     and: Thomas E. Dickey                        1996-on                 *
36 ****************************************************************************/
37
38#include <curses.priv.h>
39
40#include <tic.h>
41
42MODULE_ID("$Id: lib_ti.c,v 1.8 2023/10/17 09:52:09 nicm Exp $")
43
44#if 0
45static bool
46same_name(const char *a, const char *b)
47{
48    fprintf(stderr, "compare(%s,%s)\n", a, b);
49    return !strcmp(a, b);
50}
51#else
52#define same_name(a,b) !strcmp(a,b)
53#endif
54
55NCURSES_EXPORT(int)
56NCURSES_SP_NAME(tigetflag) (NCURSES_SP_DCLx const char *str)
57{
58    int result = ABSENT_BOOLEAN;
59
60    T((T_CALLED("tigetflag(%p, %s)"), (void *) SP_PARM, str));
61
62    if (HasTInfoTerminal(SP_PARM)) {
63	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
64	struct name_table_entry const *entry_ptr;
65	int j = -1;
66
67	entry_ptr = _nc_find_type_entry(str, BOOLEAN, FALSE);
68	if (entry_ptr != 0) {
69	    j = entry_ptr->nte_index;
70	}
71#if NCURSES_XNAMES
72	else {
73	    int i;
74	    for_each_ext_boolean(i, tp) {
75		const char *capname = ExtBoolname(tp, i, boolnames);
76		if (same_name(str, capname)) {
77		    j = i;
78		    break;
79		}
80	    }
81	}
82#endif
83	if (j >= 0) {
84	    /* note: setupterm forces invalid booleans to false */
85	    result = tp->Booleans[j];
86	}
87    }
88
89    returnCode(result);
90}
91
92#if NCURSES_SP_FUNCS
93NCURSES_EXPORT(int)
94tigetflag(const char *str)
95{
96    return NCURSES_SP_NAME(tigetflag) (CURRENT_SCREEN, str);
97}
98#endif
99
100NCURSES_EXPORT(int)
101NCURSES_SP_NAME(tigetnum) (NCURSES_SP_DCLx const char *str)
102{
103    int result = CANCELLED_NUMERIC;	/* Solaris returns a -1 on error */
104
105    T((T_CALLED("tigetnum(%p, %s)"), (void *) SP_PARM, str));
106
107    if (HasTInfoTerminal(SP_PARM)) {
108	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
109	struct name_table_entry const *entry_ptr;
110	int j = -1;
111
112	entry_ptr = _nc_find_type_entry(str, NUMBER, FALSE);
113	if (entry_ptr != 0) {
114	    j = entry_ptr->nte_index;
115	}
116#if NCURSES_XNAMES
117	else {
118	    int i;
119	    for_each_ext_number(i, tp) {
120		const char *capname = ExtNumname(tp, i, numnames);
121		if (same_name(str, capname)) {
122		    j = i;
123		    break;
124		}
125	    }
126	}
127#endif
128	if (j >= 0) {
129	    if (VALID_NUMERIC(tp->Numbers[j]))
130		result = tp->Numbers[j];
131	    else
132		result = ABSENT_NUMERIC;
133	}
134    }
135
136    returnCode(result);
137}
138
139#if NCURSES_SP_FUNCS
140NCURSES_EXPORT(int)
141tigetnum(const char *str)
142{
143    return NCURSES_SP_NAME(tigetnum) (CURRENT_SCREEN, str);
144}
145#endif
146
147NCURSES_EXPORT(char *)
148NCURSES_SP_NAME(tigetstr) (NCURSES_SP_DCLx const char *str)
149{
150    char *result = CANCELLED_STRING;
151
152    T((T_CALLED("tigetstr(%p, %s)"), (void *) SP_PARM, str));
153
154    if (HasTInfoTerminal(SP_PARM)) {
155	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
156	struct name_table_entry const *entry_ptr;
157	int j = -1;
158
159	entry_ptr = _nc_find_type_entry(str, STRING, FALSE);
160	if (entry_ptr != 0) {
161	    j = entry_ptr->nte_index;
162	}
163#if NCURSES_XNAMES
164	else {
165	    int i;
166	    for_each_ext_string(i, tp) {
167		const char *capname = ExtStrname(tp, i, strnames);
168		if (same_name(str, capname)) {
169		    j = i;
170		    break;
171		}
172	    }
173	}
174#endif
175	if (j >= 0) {
176	    /* note: setupterm forces cancelled strings to null */
177	    result = tp->Strings[j];
178	}
179    }
180
181    returnPtr(result);
182}
183
184#if NCURSES_SP_FUNCS
185NCURSES_EXPORT(char *)
186tigetstr(const char *str)
187{
188    return NCURSES_SP_NAME(tigetstr) (CURRENT_SCREEN, str);
189}
190#endif
191