xref: /netbsd/lib/libterminfo/ti.c (revision 6550d01e)
1 /* $NetBSD: ti.c,v 1.2 2010/02/04 09:46:26 roy Exp $ */
2 
3 /*
4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5  *
6  * This id is derived from software contributed to The NetBSD Foundation
7  * by Roy Marples.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source id must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: ti.c,v 1.2 2010/02/04 09:46:26 roy Exp $");
32 
33 #include <assert.h>
34 #include <string.h>
35 #include <term_private.h>
36 #include <term.h>
37 
38 int
39 ti_getflag(const TERMINAL *term, const char *id)
40 {
41 	ssize_t ind;
42 	size_t i;
43 	TERMUSERDEF *ud;
44 
45 	_DIAGASSERT(term != NULL);
46 	_DIAGASSERT(id != NULL);
47 
48 	ind = _ti_flagindex(id);
49 	if (ind != -1)
50 		return term->flags[ind];
51 	for (i = 0; i < term->_nuserdefs; i++) {
52 		ud = &term->_userdefs[i];
53 		if (ud->type == 'f' && strcmp(ud->id, id) == 0)
54 			return ud->flag;
55 	}
56 	return ABSENT_BOOLEAN;
57 }
58 
59 int
60 tigetflag(const char *id)
61 {
62 
63 	_DIAGASSERT(id != NULL);
64 	if (cur_term != NULL)
65 		return ti_getflag(cur_term, id);
66 	return ABSENT_BOOLEAN;
67 }
68 
69 int
70 ti_getnum(const TERMINAL *term, const char *id)
71 {
72 	ssize_t ind;
73 	size_t i;
74 	TERMUSERDEF *ud;
75 
76 	_DIAGASSERT(term != NULL);
77 	_DIAGASSERT(id != NULL);
78 
79 	ind = _ti_numindex(id);
80 	if (ind != -1) {
81 		if (!VALID_NUMERIC(term->nums[ind]))
82 			return ABSENT_NUMERIC;
83 		return term->nums[ind];
84 	}
85 	for (i = 0; i < term->_nuserdefs; i++) {
86 		ud = &term->_userdefs[i];
87 		if (ud->type == 'n' && strcmp(ud->id, id) == 0) {
88 			if (!VALID_NUMERIC(ud->num))
89 			    return ABSENT_NUMERIC;
90 			return ud->num;
91 		}
92 	}
93 	return CANCELLED_NUMERIC;
94 }
95 
96 int
97 tigetnum(const char *id)
98 {
99 
100 	_DIAGASSERT(id != NULL);
101 	if (cur_term != NULL)
102 		return ti_getnum(cur_term, id);
103 	return CANCELLED_NUMERIC;
104 }
105 
106 const char *
107 ti_getstr(const TERMINAL *term, const char *id)
108 {
109 	ssize_t ind;
110 	size_t i;
111 	TERMUSERDEF *ud;
112 
113 	_DIAGASSERT(term != NULL);
114 	_DIAGASSERT(id != NULL);
115 
116 	ind = _ti_strindex(id);
117 	if (ind != -1)
118 		return term->strs[ind];
119 	for (i = 0; i < term->_nuserdefs; i++) {
120 		ud = &term->_userdefs[i];
121 		if (ud->type == 's' && strcmp(ud->id, id) == 0)
122 			return ud->str;
123 	}
124 	return (const char *)CANCELLED_STRING;
125 }
126 
127 char *
128 tigetstr(const char *id)
129 {
130 
131 	_DIAGASSERT(id != NULL);
132 	if (cur_term != NULL)
133 		return __UNCONST(ti_getstr(cur_term, id));
134 	return (char *)CANCELLED_STRING;
135 }
136