xref: /openbsd-src/lib/libcurses/tinfo/lib_termcap.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: lib_termcap.c,v 1.9 2003/03/18 16:55:54 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 #include <curses.priv.h>
37 
38 #include <termcap.h>
39 #include <tic.h>
40 
41 #define __INTERNAL_CAPS_VISIBLE
42 #include <term_entry.h>
43 
44 MODULE_ID("$From: lib_termcap.c,v 1.39 2000/12/10 02:56:30 tom Exp $")
45 
46 /*
47    some of the code in here was contributed by:
48    Magnus Bengtsson, d6mbeng@dtek.chalmers.se
49 */
50 
51 NCURSES_EXPORT_VAR(char *)
52 UP = 0;
53 NCURSES_EXPORT_VAR(char *)
54 BC = 0;
55 
56 /***************************************************************************
57  *
58  * tgetent(bufp, term)
59  *
60  * In termcap, this function reads in the entry for terminal `term' into the
61  * buffer pointed to by bufp. It must be called before any of the functions
62  * below are called.
63  * In this terminfo emulation, tgetent() simply calls setupterm() (which
64  * does a bit more than tgetent() in termcap does), and returns its return
65  * value (1 if successful, 0 if no terminal with the given name could be
66  * found, or -1 if no terminal descriptions have been installed on the
67  * system).  The bufp argument is ignored.
68  *
69  ***************************************************************************/
70 
71 NCURSES_EXPORT(int)
72 tgetent
73 (char *bufp GCC_UNUSED, const char *name)
74 {
75     int errcode;
76 
77     T((T_CALLED("tgetent()")));
78 
79     setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode);
80 
81     if (errcode == 1) {
82 
83 	if (cursor_left)
84 	    if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
85 		backspace_if_not_bs = cursor_left;
86 
87 	/* we're required to export these */
88 	if (pad_char != NULL)
89 	    PC = pad_char[0];
90 	if (cursor_up != NULL)
91 	    UP = cursor_up;
92 	if (backspace_if_not_bs != NULL)
93 	    BC = backspace_if_not_bs;
94 
95 	(void) baudrate();	/* sets ospeed as a side-effect */
96 
97 /* LINT_PREPRO
98 #if 0*/
99 #include <capdefaults.c>
100 /* LINT_PREPRO
101 #endif*/
102 
103     }
104     returnCode(errcode);
105 }
106 
107 /***************************************************************************
108  *
109  * tgetflag(str)
110  *
111  * Look up boolean termcap capability str and return its value (TRUE=1 if
112  * present, FALSE=0 if not).
113  *
114  ***************************************************************************/
115 
116 NCURSES_EXPORT(int)
117 tgetflag(NCURSES_CONST char *id)
118 {
119     int i;
120 
121     T((T_CALLED("tgetflag(%s)"), id));
122     if (cur_term != 0) {
123 	TERMTYPE *tp = &(cur_term->type);
124 	for_each_boolean(i, tp) {
125 	    const char *capname = ExtBoolname(tp, i, boolcodes);
126 	    if (!strncmp(id, capname, 2)) {
127 		/* setupterm forces invalid booleans to false */
128 		returnCode(tp->Booleans[i]);
129 	    }
130 	}
131     }
132     returnCode(0);		/* Solaris does this */
133 }
134 
135 /***************************************************************************
136  *
137  * tgetnum(str)
138  *
139  * Look up numeric termcap capability str and return its value, or -1 if
140  * not given.
141  *
142  ***************************************************************************/
143 
144 NCURSES_EXPORT(int)
145 tgetnum(NCURSES_CONST char *id)
146 {
147     int i;
148 
149     T((T_CALLED("tgetnum(%s)"), id));
150     if (cur_term != 0) {
151 	TERMTYPE *tp = &(cur_term->type);
152 	for_each_number(i, tp) {
153 	    const char *capname = ExtNumname(tp, i, numcodes);
154 	    if (!strncmp(id, capname, 2)) {
155 		if (!VALID_NUMERIC(tp->Numbers[i]))
156 		    returnCode(ABSENT_NUMERIC);
157 		returnCode(tp->Numbers[i]);
158 	    }
159 	}
160     }
161     returnCode(ABSENT_NUMERIC);
162 }
163 
164 /***************************************************************************
165  *
166  * tgetstr(str, area)
167  *
168  * Look up string termcap capability str and return a pointer to its value,
169  * or NULL if not given.
170  *
171  ***************************************************************************/
172 
173 NCURSES_EXPORT(char *)
174 tgetstr
175 (NCURSES_CONST char *id, char **area)
176 {
177     int i;
178 
179     T((T_CALLED("tgetstr(%s,%p)"), id, area));
180     if (cur_term != 0) {
181 	TERMTYPE *tp = &(cur_term->type);
182 	for_each_string(i, tp) {
183 	    const char *capname = ExtStrname(tp, i, strcodes);
184 	    if (!strncmp(id, capname, 2)) {
185 		TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(tp->Strings[i])));
186 		/* setupterm forces canceled strings to null */
187 		if (area != 0
188 		    && *area != 0
189 		    && VALID_STRING(tp->Strings[i])) {
190 		    (void) strlcpy(*area, tp->Strings[i], 1024);
191 		    *area += strlen(*area) + 1;
192 		}
193 		returnPtr(tp->Strings[i]);
194 	    }
195 	}
196     }
197     returnPtr(NULL);
198 }
199