xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_ti.c (revision 43c7dd6b597947c27cab4ebc5a67a8a3f5b7c58d)
10e3d5408SPeter Wemm /****************************************************************************
2*e1865124SBaptiste Daroussin  * Copyright 2018,2020 Thomas E. Dickey                                     *
3*e1865124SBaptiste Daroussin  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
40e3d5408SPeter Wemm  *                                                                          *
50e3d5408SPeter Wemm  * Permission is hereby granted, free of charge, to any person obtaining a  *
60e3d5408SPeter Wemm  * copy of this software and associated documentation files (the            *
70e3d5408SPeter Wemm  * "Software"), to deal in the Software without restriction, including      *
80e3d5408SPeter Wemm  * without limitation the rights to use, copy, modify, merge, publish,      *
90e3d5408SPeter Wemm  * distribute, distribute with modifications, sublicense, and/or sell       *
100e3d5408SPeter Wemm  * copies of the Software, and to permit persons to whom the Software is    *
110e3d5408SPeter Wemm  * furnished to do so, subject to the following conditions:                 *
120e3d5408SPeter Wemm  *                                                                          *
130e3d5408SPeter Wemm  * The above copyright notice and this permission notice shall be included  *
140e3d5408SPeter Wemm  * in all copies or substantial portions of the Software.                   *
150e3d5408SPeter Wemm  *                                                                          *
160e3d5408SPeter Wemm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
170e3d5408SPeter Wemm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
180e3d5408SPeter Wemm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
190e3d5408SPeter Wemm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
200e3d5408SPeter Wemm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
210e3d5408SPeter Wemm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
220e3d5408SPeter Wemm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
230e3d5408SPeter Wemm  *                                                                          *
240e3d5408SPeter Wemm  * Except as contained in this notice, the name(s) of the above copyright   *
250e3d5408SPeter Wemm  * holders shall not be used in advertising or otherwise to promote the     *
260e3d5408SPeter Wemm  * sale, use or other dealings in this Software without prior written       *
270e3d5408SPeter Wemm  * authorization.                                                           *
280e3d5408SPeter Wemm  ****************************************************************************/
290e3d5408SPeter Wemm 
300e3d5408SPeter Wemm /****************************************************************************
310e3d5408SPeter Wemm  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
320e3d5408SPeter Wemm  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
3306bfebdeSXin LI  *     and: Thomas E. Dickey                        1996-on                 *
340e3d5408SPeter Wemm  ****************************************************************************/
350e3d5408SPeter Wemm 
360e3d5408SPeter Wemm #include <curses.priv.h>
370e3d5408SPeter Wemm 
380e3d5408SPeter Wemm #include <tic.h>
390e3d5408SPeter Wemm 
40*e1865124SBaptiste Daroussin MODULE_ID("$Id: lib_ti.c,v 1.34 2020/02/02 23:34:34 tom Exp $")
410e3d5408SPeter Wemm 
4206bfebdeSXin LI #if 0
4306bfebdeSXin LI static bool
4406bfebdeSXin LI same_name(const char *a, const char *b)
4506bfebdeSXin LI {
4606bfebdeSXin LI     fprintf(stderr, "compare(%s,%s)\n", a, b);
4706bfebdeSXin LI     return !strcmp(a, b);
4806bfebdeSXin LI }
4906bfebdeSXin LI #else
5006bfebdeSXin LI #define same_name(a,b) !strcmp(a,b)
5106bfebdeSXin LI #endif
5206bfebdeSXin LI 
NCURSES_EXPORT(int)5306bfebdeSXin LI NCURSES_EXPORT(int)
54aae38d10SBaptiste Daroussin NCURSES_SP_NAME(tigetflag) (NCURSES_SP_DCLx const char *str)
5506bfebdeSXin LI {
5606bfebdeSXin LI     int result = ABSENT_BOOLEAN;
5706bfebdeSXin LI 
5806bfebdeSXin LI     T((T_CALLED("tigetflag(%p, %s)"), (void *) SP_PARM, str));
5906bfebdeSXin LI 
6006bfebdeSXin LI     if (HasTInfoTerminal(SP_PARM)) {
61aae38d10SBaptiste Daroussin 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
6206bfebdeSXin LI 	struct name_table_entry const *entry_ptr;
63aae38d10SBaptiste Daroussin 	int j = -1;
6406bfebdeSXin LI 
6506bfebdeSXin LI 	entry_ptr = _nc_find_type_entry(str, BOOLEAN, FALSE);
6606bfebdeSXin LI 	if (entry_ptr != 0) {
6706bfebdeSXin LI 	    j = entry_ptr->nte_index;
6806bfebdeSXin LI 	}
6906bfebdeSXin LI #if NCURSES_XNAMES
7006bfebdeSXin LI 	else {
7173f0a83dSXin LI 	    int i;
7206bfebdeSXin LI 	    for_each_ext_boolean(i, tp) {
7306bfebdeSXin LI 		const char *capname = ExtBoolname(tp, i, boolnames);
7406bfebdeSXin LI 		if (same_name(str, capname)) {
7506bfebdeSXin LI 		    j = i;
7606bfebdeSXin LI 		    break;
7706bfebdeSXin LI 		}
7806bfebdeSXin LI 	    }
7906bfebdeSXin LI 	}
8006bfebdeSXin LI #endif
8106bfebdeSXin LI 	if (j >= 0) {
8206bfebdeSXin LI 	    /* note: setupterm forces invalid booleans to false */
8306bfebdeSXin LI 	    result = tp->Booleans[j];
8406bfebdeSXin LI 	}
8506bfebdeSXin LI     }
8606bfebdeSXin LI 
8706bfebdeSXin LI     returnCode(result);
8806bfebdeSXin LI }
8906bfebdeSXin LI 
9006bfebdeSXin LI #if NCURSES_SP_FUNCS
917a69bbfbSPeter Wemm NCURSES_EXPORT(int)
tigetflag(const char * str)92aae38d10SBaptiste Daroussin tigetflag(const char *str)
930e3d5408SPeter Wemm {
9406bfebdeSXin LI     return NCURSES_SP_NAME(tigetflag) (CURRENT_SCREEN, str);
9506bfebdeSXin LI }
9606bfebdeSXin LI #endif
970e3d5408SPeter Wemm 
9806bfebdeSXin LI NCURSES_EXPORT(int)
NCURSES_SP_NAME(tigetnum)99aae38d10SBaptiste Daroussin NCURSES_SP_NAME(tigetnum) (NCURSES_SP_DCLx const char *str)
10006bfebdeSXin LI {
10106bfebdeSXin LI     int result = CANCELLED_NUMERIC;	/* Solaris returns a -1 on error */
1020e3d5408SPeter Wemm 
10306bfebdeSXin LI     T((T_CALLED("tigetnum(%p, %s)"), (void *) SP_PARM, str));
10406bfebdeSXin LI 
10506bfebdeSXin LI     if (HasTInfoTerminal(SP_PARM)) {
106aae38d10SBaptiste Daroussin 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
10706bfebdeSXin LI 	struct name_table_entry const *entry_ptr;
108aae38d10SBaptiste Daroussin 	int j = -1;
10906bfebdeSXin LI 
11006bfebdeSXin LI 	entry_ptr = _nc_find_type_entry(str, NUMBER, FALSE);
11106bfebdeSXin LI 	if (entry_ptr != 0) {
11206bfebdeSXin LI 	    j = entry_ptr->nte_index;
11306bfebdeSXin LI 	}
11406bfebdeSXin LI #if NCURSES_XNAMES
11506bfebdeSXin LI 	else {
11673f0a83dSXin LI 	    int i;
11706bfebdeSXin LI 	    for_each_ext_number(i, tp) {
11806bfebdeSXin LI 		const char *capname = ExtNumname(tp, i, numnames);
11906bfebdeSXin LI 		if (same_name(str, capname)) {
12006bfebdeSXin LI 		    j = i;
12106bfebdeSXin LI 		    break;
1220e3d5408SPeter Wemm 		}
1230e3d5408SPeter Wemm 	    }
1240e3d5408SPeter Wemm 	}
12506bfebdeSXin LI #endif
12606bfebdeSXin LI 	if (j >= 0) {
12706bfebdeSXin LI 	    if (VALID_NUMERIC(tp->Numbers[j]))
12806bfebdeSXin LI 		result = tp->Numbers[j];
12906bfebdeSXin LI 	    else
13006bfebdeSXin LI 		result = ABSENT_NUMERIC;
13106bfebdeSXin LI 	}
1320e3d5408SPeter Wemm     }
1330e3d5408SPeter Wemm 
13406bfebdeSXin LI     returnCode(result);
13506bfebdeSXin LI }
13606bfebdeSXin LI 
13706bfebdeSXin LI #if NCURSES_SP_FUNCS
1387a69bbfbSPeter Wemm NCURSES_EXPORT(int)
tigetnum(const char * str)139aae38d10SBaptiste Daroussin tigetnum(const char *str)
1400e3d5408SPeter Wemm {
14106bfebdeSXin LI     return NCURSES_SP_NAME(tigetnum) (CURRENT_SCREEN, str);
14206bfebdeSXin LI }
14306bfebdeSXin LI #endif
1440e3d5408SPeter Wemm 
14506bfebdeSXin LI NCURSES_EXPORT(char *)
NCURSES_SP_NAME(tigetstr)146aae38d10SBaptiste Daroussin NCURSES_SP_NAME(tigetstr) (NCURSES_SP_DCLx const char *str)
14706bfebdeSXin LI {
14806bfebdeSXin LI     char *result = CANCELLED_STRING;
1490e3d5408SPeter Wemm 
15006bfebdeSXin LI     T((T_CALLED("tigetstr(%p, %s)"), (void *) SP_PARM, str));
15106bfebdeSXin LI 
15206bfebdeSXin LI     if (HasTInfoTerminal(SP_PARM)) {
153aae38d10SBaptiste Daroussin 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
15406bfebdeSXin LI 	struct name_table_entry const *entry_ptr;
155aae38d10SBaptiste Daroussin 	int j = -1;
15606bfebdeSXin LI 
15706bfebdeSXin LI 	entry_ptr = _nc_find_type_entry(str, STRING, FALSE);
15806bfebdeSXin LI 	if (entry_ptr != 0) {
15906bfebdeSXin LI 	    j = entry_ptr->nte_index;
16006bfebdeSXin LI 	}
16106bfebdeSXin LI #if NCURSES_XNAMES
16206bfebdeSXin LI 	else {
16373f0a83dSXin LI 	    int i;
16406bfebdeSXin LI 	    for_each_ext_string(i, tp) {
16506bfebdeSXin LI 		const char *capname = ExtStrname(tp, i, strnames);
16606bfebdeSXin LI 		if (same_name(str, capname)) {
16706bfebdeSXin LI 		    j = i;
16806bfebdeSXin LI 		    break;
1690e3d5408SPeter Wemm 		}
1700e3d5408SPeter Wemm 	    }
1710e3d5408SPeter Wemm 	}
17206bfebdeSXin LI #endif
17306bfebdeSXin LI 	if (j >= 0) {
17406bfebdeSXin LI 	    /* note: setupterm forces cancelled strings to null */
17506bfebdeSXin LI 	    result = tp->Strings[j];
17606bfebdeSXin LI 	}
1770e3d5408SPeter Wemm     }
1780e3d5408SPeter Wemm 
17906bfebdeSXin LI     returnPtr(result);
18006bfebdeSXin LI }
18106bfebdeSXin LI 
18206bfebdeSXin LI #if NCURSES_SP_FUNCS
1837a69bbfbSPeter Wemm NCURSES_EXPORT(char *)
tigetstr(const char * str)184aae38d10SBaptiste Daroussin tigetstr(const char *str)
1850e3d5408SPeter Wemm {
18606bfebdeSXin LI     return NCURSES_SP_NAME(tigetstr) (CURRENT_SCREEN, str);
1870e3d5408SPeter Wemm }
18806bfebdeSXin LI #endif
189