lib_termcap.c revision 50829
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#include <curses.priv.h>
35
36#include <termcap.h>
37#include <tic.h>
38
39#define __INTERNAL_CAPS_VISIBLE
40#include <term_entry.h>
41
42MODULE_ID("$Id: lib_termcap.c,v 1.28 1999/02/27 22:12:58 tom Exp $")
43
44/*
45   some of the code in here was contributed by:
46   Magnus Bengtsson, d6mbeng@dtek.chalmers.se
47*/
48
49char *UP;
50char *BC;
51
52#ifdef FREEBSD_NATIVE
53extern char _nc_termcap[];	/* buffer to copy out */
54#endif
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
71int tgetent(char *bufp GCC_UNUSED, const char *name)
72{
73int errcode;
74
75	T((T_CALLED("tgetent()")));
76
77	setupterm((NCURSES_CONST char *)name, STDOUT_FILENO, &errcode);
78
79	if (errcode == 1) {
80
81		if (cursor_left)
82		    if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
83			backspace_if_not_bs = cursor_left;
84
85		/* we're required to export these */
86		if (pad_char != NULL)
87			PC = pad_char[0];
88		if (cursor_up != NULL)
89			UP = cursor_up;
90		if (backspace_if_not_bs != NULL)
91			BC = backspace_if_not_bs;
92
93		(void) baudrate();	/* sets ospeed as a side-effect */
94
95/* LINT_PREPRO
96#if 0*/
97#include <capdefaults.c>
98/* LINT_PREPRO
99#endif*/
100
101	}
102#ifdef FREEBSD_NATIVE
103	/*
104	 * This is a REALLY UGLY hack. Basically, if we originate with
105	 * a termcap source, try and copy it out.
106	 */
107	if (bufp && _nc_termcap[0])
108		strncpy(bufp, _nc_termcap, 1024);
109#endif
110
111	returnCode(errcode);
112}
113
114/***************************************************************************
115 *
116 * tgetflag(str)
117 *
118 * Look up boolean termcap capability str and return its value (TRUE=1 if
119 * present, FALSE=0 if not).
120 *
121 ***************************************************************************/
122
123int tgetflag(NCURSES_CONST char *id)
124{
125int i;
126
127	T((T_CALLED("tgetflag(%s)"), id));
128	if (cur_term != 0) {
129	    TERMTYPE *tp = &(cur_term->type);
130	    for_each_boolean(i, tp) {
131		const char *capname = ExtBoolname(tp, i, boolcodes);
132		if (!strncmp(id, capname, 2)) {
133		    /* setupterm forces invalid booleans to false */
134		    returnCode(tp->Booleans[i]);
135		}
136	    }
137	}
138	returnCode(0);	/* Solaris does this */
139}
140
141/***************************************************************************
142 *
143 * tgetnum(str)
144 *
145 * Look up numeric termcap capability str and return its value, or -1 if
146 * not given.
147 *
148 ***************************************************************************/
149
150int tgetnum(NCURSES_CONST char *id)
151{
152int i;
153
154	T((T_CALLED("tgetnum(%s)"), id));
155	if (cur_term != 0) {
156	    TERMTYPE *tp = &(cur_term->type);
157	    for_each_number(i, tp) {
158		const char *capname = ExtNumname(tp, i, numcodes);
159		if (!strncmp(id, capname, 2)) {
160		    if (!VALID_NUMERIC(tp->Numbers[i]))
161			return -1;
162		    returnCode(tp->Numbers[i]);
163		}
164	    }
165	}
166	returnCode(ERR);
167}
168
169/***************************************************************************
170 *
171 * tgetstr(str, area)
172 *
173 * Look up string termcap capability str and return a pointer to its value,
174 * or NULL if not given.
175 *
176 ***************************************************************************/
177
178char *tgetstr(NCURSES_CONST char *id, char **area GCC_UNUSED)
179{
180int i;
181
182	T((T_CALLED("tgetstr(%s,%p)"), id, area));
183	if (cur_term != 0) {
184	    TERMTYPE *tp = &(cur_term->type);
185	    for_each_string(i, tp) {
186		const char *capname = ExtStrname(tp, i, strcodes);
187		T(("trying %s", capname));
188		if (!strncmp(id, capname, 2)) {
189		    T(("found match : %s", _nc_visbuf(tp->Strings[i])));
190		    /* setupterm forces cancelled strings to null */
191#ifdef FREEBSD_NATIVE
192		    if (*area && tp->Strings[i]) {
193			strcpy(*area, tp->Strings[i]);
194			*area += strlen(tp->Strings[i]) + 1;
195		    }
196#endif
197		    returnPtr(tp->Strings[i]);
198		}
199	    }
200	}
201	returnPtr(NULL);
202}
203
204/*
205 *	char *
206 *	tgoto(string, x, y)
207 *
208 *	Retained solely for upward compatibility.  Note the intentional
209 *	reversing of the last two arguments.
210 *
211 */
212
213char *tgoto(const char *string, int x, int y)
214{
215	T((T_CALLED("tgoto(%s,%d,%d)"), string, x, y));
216	returnPtr(tparm((NCURSES_CONST char *)string, y, x));
217}
218