xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_ti.c (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 /****************************************************************************
2  * Copyright 2018,2020 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.34 2020/02/02 23:34:34 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 
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 
60     if (HasTInfoTerminal(SP_PARM)) {
61 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
62 	struct name_table_entry const *entry_ptr;
63 	int j = -1;
64 
65 	entry_ptr = _nc_find_type_entry(str, BOOLEAN, FALSE);
66 	if (entry_ptr != 0) {
67 	    j = entry_ptr->nte_index;
68 	}
69 #if NCURSES_XNAMES
70 	else {
71 	    int i;
72 	    for_each_ext_boolean(i, tp) {
73 		const char *capname = ExtBoolname(tp, i, boolnames);
74 		if (same_name(str, capname)) {
75 		    j = i;
76 		    break;
77 		}
78 	    }
79 	}
80 #endif
81 	if (j >= 0) {
82 	    /* note: setupterm forces invalid booleans to false */
83 	    result = tp->Booleans[j];
84 	}
85     }
86 
87     returnCode(result);
88 }
89 
90 #if NCURSES_SP_FUNCS
91 NCURSES_EXPORT(int)
92 tigetflag(const char *str)
93 {
94     return NCURSES_SP_NAME(tigetflag) (CURRENT_SCREEN, str);
95 }
96 #endif
97 
98 NCURSES_EXPORT(int)
99 NCURSES_SP_NAME(tigetnum) (NCURSES_SP_DCLx const char *str)
100 {
101     int result = CANCELLED_NUMERIC;	/* Solaris returns a -1 on error */
102 
103     T((T_CALLED("tigetnum(%p, %s)"), (void *) SP_PARM, str));
104 
105     if (HasTInfoTerminal(SP_PARM)) {
106 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
107 	struct name_table_entry const *entry_ptr;
108 	int j = -1;
109 
110 	entry_ptr = _nc_find_type_entry(str, NUMBER, FALSE);
111 	if (entry_ptr != 0) {
112 	    j = entry_ptr->nte_index;
113 	}
114 #if NCURSES_XNAMES
115 	else {
116 	    int i;
117 	    for_each_ext_number(i, tp) {
118 		const char *capname = ExtNumname(tp, i, numnames);
119 		if (same_name(str, capname)) {
120 		    j = i;
121 		    break;
122 		}
123 	    }
124 	}
125 #endif
126 	if (j >= 0) {
127 	    if (VALID_NUMERIC(tp->Numbers[j]))
128 		result = tp->Numbers[j];
129 	    else
130 		result = ABSENT_NUMERIC;
131 	}
132     }
133 
134     returnCode(result);
135 }
136 
137 #if NCURSES_SP_FUNCS
138 NCURSES_EXPORT(int)
139 tigetnum(const char *str)
140 {
141     return NCURSES_SP_NAME(tigetnum) (CURRENT_SCREEN, str);
142 }
143 #endif
144 
145 NCURSES_EXPORT(char *)
146 NCURSES_SP_NAME(tigetstr) (NCURSES_SP_DCLx const char *str)
147 {
148     char *result = CANCELLED_STRING;
149 
150     T((T_CALLED("tigetstr(%p, %s)"), (void *) SP_PARM, str));
151 
152     if (HasTInfoTerminal(SP_PARM)) {
153 	TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
154 	struct name_table_entry const *entry_ptr;
155 	int j = -1;
156 
157 	entry_ptr = _nc_find_type_entry(str, STRING, FALSE);
158 	if (entry_ptr != 0) {
159 	    j = entry_ptr->nte_index;
160 	}
161 #if NCURSES_XNAMES
162 	else {
163 	    int i;
164 	    for_each_ext_string(i, tp) {
165 		const char *capname = ExtStrname(tp, i, strnames);
166 		if (same_name(str, capname)) {
167 		    j = i;
168 		    break;
169 		}
170 	    }
171 	}
172 #endif
173 	if (j >= 0) {
174 	    /* note: setupterm forces cancelled strings to null */
175 	    result = tp->Strings[j];
176 	}
177     }
178 
179     returnPtr(result);
180 }
181 
182 #if NCURSES_SP_FUNCS
183 NCURSES_EXPORT(char *)
184 tigetstr(const char *str)
185 {
186     return NCURSES_SP_NAME(tigetstr) (CURRENT_SCREEN, str);
187 }
188 #endif
189