xref: /src/contrib/ncurses/ncurses/tinfo/lib_ti.c (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
1 /****************************************************************************
2  * Copyright 2018-2024,2025 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
34  ****************************************************************************/
35 
36 #include <curses.priv.h>
37 
38 #include <tic.h>
39 
40 MODULE_ID("$Id: lib_ti.c,v 1.36 2025/11/23 18:58:04 tom Exp $")
41 
42 #if 0
43 static bool
44 same_name(const char *a, const char *b)
45 {
46     fprintf(stderr, "compare(%s,%s)\n", a, b);
47     return !strcmp(a, b);
48 }
49 #else
50 #define same_name(a,b) !strcmp(a,b)
51 #endif
52 
NCURSES_EXPORT(int)53 NCURSES_EXPORT(int)
54 NCURSES_SP_NAME(tigetflag) (NCURSES_SP_DCLx const char *str)
55 {
56     int result = ABSENT_BOOLEAN;
57 
58     T((T_CALLED("tigetflag(%p, %s)"), (void *) SP_PARM, str));
59     assert(result < 0);
60 
61     if (HasTInfoTerminal(SP_PARM)) {
62 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
63 	struct name_table_entry const *entry_ptr;
64 	int j = -1;
65 
66 	entry_ptr = _nc_find_type_entry(str, BOOLEAN, FALSE);
67 	if (entry_ptr != NULL) {
68 	    j = entry_ptr->nte_index;
69 	}
70 #if NCURSES_XNAMES
71 	else {
72 	    int i;
73 	    for_each_ext_boolean(i, tp) {
74 		const char *capname = ExtBoolname(tp, i, boolnames);
75 		if (same_name(str, capname)) {
76 		    j = i;
77 		    break;
78 		}
79 	    }
80 	}
81 #endif
82 	if (j >= 0) {
83 	    /* note: setupterm forces invalid booleans to false */
84 	    result = tp->Booleans[j];
85 	}
86     }
87 
88     returnCode(result);
89 }
90 
91 #if NCURSES_SP_FUNCS
92 NCURSES_EXPORT(int)
tigetflag(const char * str)93 tigetflag(const char *str)
94 {
95     return NCURSES_SP_NAME(tigetflag) (CURRENT_SCREEN, str);
96 }
97 #endif
98 
99 NCURSES_EXPORT(int)
tigetnum(NCURSES_SP_DCLx const char * str)100 NCURSES_SP_NAME(tigetnum) (NCURSES_SP_DCLx const char *str)
101 {
102     int result = CANCELLED_NUMERIC;	/* Solaris returns a -1 on error */
103 
104     T((T_CALLED("tigetnum(%p, %s)"), (void *) SP_PARM, str));
105     assert(result < 0);
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 != NULL) {
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
140 NCURSES_EXPORT(int)
tigetnum(const char * str)141 tigetnum(const char *str)
142 {
143     return NCURSES_SP_NAME(tigetnum) (CURRENT_SCREEN, str);
144 }
145 #endif
146 
147 NCURSES_EXPORT(char *)
tigetstr(NCURSES_SP_DCLx const char * str)148 NCURSES_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 != NULL) {
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
185 NCURSES_EXPORT(char *)
tigetstr(const char * str)186 tigetstr(const char *str)
187 {
188     return NCURSES_SP_NAME(tigetstr) (CURRENT_SCREEN, str);
189 }
190 #endif
191