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