xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_termcap.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /****************************************************************************
2  * Copyright (c) 1998 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  ****************************************************************************/
33 
34 /* $FreeBSD$ */
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("$Id: lib_termcap.c,v 1.29 1999/09/05 01:06:43 tom Exp $")
45 
46 /*
47    some of the code in here was contributed by:
48    Magnus Bengtsson, d6mbeng@dtek.chalmers.se
49 */
50 
51 char *UP;
52 char *BC;
53 
54 #ifdef FREEBSD_NATIVE
55 #undef	GCC_UNUSED
56 #define	GCC_UNUSED
57 extern char _nc_termcap[];	/* buffer to copy out */
58 #endif
59 
60 /***************************************************************************
61  *
62  * tgetent(bufp, term)
63  *
64  * In termcap, this function reads in the entry for terminal `term' into the
65  * buffer pointed to by bufp. It must be called before any of the functions
66  * below are called.
67  * In this terminfo emulation, tgetent() simply calls setupterm() (which
68  * does a bit more than tgetent() in termcap does), and returns its return
69  * value (1 if successful, 0 if no terminal with the given name could be
70  * found, or -1 if no terminal descriptions have been installed on the
71  * system).  The bufp argument is ignored.
72  *
73  ***************************************************************************/
74 
75 int tgetent(char *bufp GCC_UNUSED, const char *name)
76 {
77 int errcode;
78 
79 	T((T_CALLED("tgetent()")));
80 
81 	setupterm((NCURSES_CONST char *)name, STDOUT_FILENO, &errcode);
82 
83 	if (errcode == 1) {
84 
85 		if (cursor_left)
86 		    if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
87 			backspace_if_not_bs = cursor_left;
88 
89 		/* we're required to export these */
90 		if (pad_char != NULL)
91 			PC = pad_char[0];
92 		if (cursor_up != NULL)
93 			UP = cursor_up;
94 		if (backspace_if_not_bs != NULL)
95 			BC = backspace_if_not_bs;
96 
97 		(void) baudrate();	/* sets ospeed as a side-effect */
98 
99 /* LINT_PREPRO
100 #if 0*/
101 #include <capdefaults.c>
102 /* LINT_PREPRO
103 #endif*/
104 
105 	}
106 #ifdef FREEBSD_NATIVE
107 	/*
108 	 * This is a REALLY UGLY hack. Basically, if we originate with
109 	 * a termcap source, try and copy it out.
110 	 */
111 	if (bufp && _nc_termcap[0])
112 		strncpy(bufp, _nc_termcap, 1024);
113 #endif
114 
115 	returnCode(errcode);
116 }
117 
118 /***************************************************************************
119  *
120  * tgetflag(str)
121  *
122  * Look up boolean termcap capability str and return its value (TRUE=1 if
123  * present, FALSE=0 if not).
124  *
125  ***************************************************************************/
126 
127 int tgetflag(NCURSES_CONST char *id)
128 {
129 int i;
130 
131 	T((T_CALLED("tgetflag(%s)"), id));
132 	if (cur_term != 0) {
133 	    TERMTYPE *tp = &(cur_term->type);
134 	    for_each_boolean(i, tp) {
135 		const char *capname = ExtBoolname(tp, i, boolcodes);
136 		if (!strncmp(id, capname, 2)) {
137 		    /* setupterm forces invalid booleans to false */
138 		    returnCode(tp->Booleans[i]);
139 		}
140 	    }
141 	}
142 	returnCode(0);	/* Solaris does this */
143 }
144 
145 /***************************************************************************
146  *
147  * tgetnum(str)
148  *
149  * Look up numeric termcap capability str and return its value, or -1 if
150  * not given.
151  *
152  ***************************************************************************/
153 
154 int tgetnum(NCURSES_CONST char *id)
155 {
156 int i;
157 
158 	T((T_CALLED("tgetnum(%s)"), id));
159 	if (cur_term != 0) {
160 	    TERMTYPE *tp = &(cur_term->type);
161 	    for_each_number(i, tp) {
162 		const char *capname = ExtNumname(tp, i, numcodes);
163 		if (!strncmp(id, capname, 2)) {
164 		    if (!VALID_NUMERIC(tp->Numbers[i]))
165 			return -1;
166 		    returnCode(tp->Numbers[i]);
167 		}
168 	    }
169 	}
170 	returnCode(ERR);
171 }
172 
173 /***************************************************************************
174  *
175  * tgetstr(str, area)
176  *
177  * Look up string termcap capability str and return a pointer to its value,
178  * or NULL if not given.
179  *
180  ***************************************************************************/
181 
182 char *tgetstr(NCURSES_CONST char *id, char **area)
183 {
184 int i;
185 
186 	T((T_CALLED("tgetstr(%s,%p)"), id, area));
187 	if (cur_term != 0) {
188 	    TERMTYPE *tp = &(cur_term->type);
189 	    for_each_string(i, tp) {
190 		const char *capname = ExtStrname(tp, i, strcodes);
191 		T(("trying %s", capname));
192 		if (!strncmp(id, capname, 2)) {
193 		    T(("found match : %s", _nc_visbuf(tp->Strings[i])));
194 		    /* setupterm forces cancelled strings to null */
195 		    if (area != 0
196 		     && *area != 0
197 		     && VALID_STRING(tp->Strings[i])) {
198 			(void) strcpy(*area, tp->Strings[i]);
199 			*area += strlen(*area) + 1;
200 		    }
201 		    returnPtr(tp->Strings[i]);
202 		}
203 	    }
204 	}
205 	returnPtr(NULL);
206 }
207 
208 /*
209  *	char *
210  *	tgoto(string, x, y)
211  *
212  *	Retained solely for upward compatibility.  Note the intentional
213  *	reversing of the last two arguments.
214  *
215  */
216 
217 char *tgoto(const char *string, int x, int y)
218 {
219 	T((T_CALLED("tgoto(%s,%d,%d)"), string, x, y));
220 	returnPtr(tparm((NCURSES_CONST char *)string, y, x));
221 }
222