1/****************************************************************************
2 * Copyright (c) 1998-2004,2005 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 * some of the code in here was contributed by:                             *
35 * Magnus Bengtsson, d6mbeng@dtek.chalmers.se (Nov'93)                      *
36 ****************************************************************************/
37
38#define __INTERNAL_CAPS_VISIBLE
39#include <curses.priv.h>
40
41#include <termcap.h>
42#include <tic.h>
43#include <ctype.h>
44
45#include <term_entry.h>
46
47MODULE_ID("$Id: lib_termcap.c,v 1.51 2005/07/16 23:12:51 tom Exp $")
48
49NCURSES_EXPORT_VAR(char *) UP = 0;
50NCURSES_EXPORT_VAR(char *) BC = 0;
51
52static char *fix_me = 0;	/* this holds the filtered sgr0 string */
53
54/***************************************************************************
55 *
56 * tgetent(bufp, term)
57 *
58 * In termcap, this function reads in the entry for terminal `term' into the
59 * buffer pointed to by bufp. It must be called before any of the functions
60 * below are called.
61 * In this terminfo emulation, tgetent() simply calls setupterm() (which
62 * does a bit more than tgetent() in termcap does), and returns its return
63 * value (1 if successful, 0 if no terminal with the given name could be
64 * found, or -1 if no terminal descriptions have been installed on the
65 * system).  The bufp argument is ignored.
66 *
67 ***************************************************************************/
68
69NCURSES_EXPORT(int)
70tgetent(char *bufp GCC_UNUSED, const char *name)
71{
72    int errcode;
73
74    START_TRACE();
75    T((T_CALLED("tgetent()")));
76
77    _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
78
79    PC = 0;
80    UP = 0;
81    BC = 0;
82    fix_me = 0;			/* don't free it - application may still use */
83
84    if (errcode == 1) {
85
86	if (cursor_left)
87	    if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
88		backspace_if_not_bs = cursor_left;
89
90	/* we're required to export these */
91	if (pad_char != NULL)
92	    PC = pad_char[0];
93	if (cursor_up != NULL)
94	    UP = cursor_up;
95	if (backspace_if_not_bs != NULL)
96	    BC = backspace_if_not_bs;
97
98	FreeIfNeeded(fix_me);
99	if ((fix_me = _nc_trim_sgr0(&(cur_term->type))) != 0) {
100	    if (!strcmp(fix_me, exit_attribute_mode)) {
101		if (fix_me != exit_attribute_mode) {
102		    free(fix_me);
103		}
104		fix_me = 0;
105	    }
106	}
107
108	(void) baudrate();	/* sets ospeed as a side-effect */
109
110/* LINT_PREPRO
111#if 0*/
112#include <capdefaults.c>
113/* LINT_PREPRO
114#endif*/
115
116    }
117    returnCode(errcode);
118}
119
120/***************************************************************************
121 *
122 * tgetflag(str)
123 *
124 * Look up boolean termcap capability str and return its value (TRUE=1 if
125 * present, FALSE=0 if not).
126 *
127 ***************************************************************************/
128
129NCURSES_EXPORT(int)
130tgetflag(NCURSES_CONST char *id)
131{
132    unsigned i;
133
134    T((T_CALLED("tgetflag(%s)"), id));
135    if (cur_term != 0) {
136	TERMTYPE *tp = &(cur_term->type);
137	for_each_boolean(i, tp) {
138	    const char *capname = ExtBoolname(tp, i, boolcodes);
139	    if (!strncmp(id, capname, 2)) {
140		/* setupterm forces invalid booleans to false */
141		returnCode(tp->Booleans[i]);
142	    }
143	}
144    }
145    returnCode(0);		/* Solaris does this */
146}
147
148/***************************************************************************
149 *
150 * tgetnum(str)
151 *
152 * Look up numeric termcap capability str and return its value, or -1 if
153 * not given.
154 *
155 ***************************************************************************/
156
157NCURSES_EXPORT(int)
158tgetnum(NCURSES_CONST char *id)
159{
160    unsigned i;
161
162    T((T_CALLED("tgetnum(%s)"), id));
163    if (cur_term != 0) {
164	TERMTYPE *tp = &(cur_term->type);
165	for_each_number(i, tp) {
166	    const char *capname = ExtNumname(tp, i, numcodes);
167	    if (!strncmp(id, capname, 2)) {
168		if (!VALID_NUMERIC(tp->Numbers[i]))
169		    returnCode(ABSENT_NUMERIC);
170		returnCode(tp->Numbers[i]);
171	    }
172	}
173    }
174    returnCode(ABSENT_NUMERIC);
175}
176
177/***************************************************************************
178 *
179 * tgetstr(str, area)
180 *
181 * Look up string termcap capability str and return a pointer to its value,
182 * or NULL if not given.
183 *
184 ***************************************************************************/
185
186NCURSES_EXPORT(char *)
187tgetstr(NCURSES_CONST char *id, char **area)
188{
189    unsigned i;
190    char *result = NULL;
191
192    T((T_CALLED("tgetstr(%s,%p)"), id, area));
193    if (cur_term != 0) {
194	TERMTYPE *tp = &(cur_term->type);
195	for_each_string(i, tp) {
196	    const char *capname = ExtStrname(tp, i, strcodes);
197	    if (!strncmp(id, capname, 2)) {
198		result = tp->Strings[i];
199		TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
200		/* setupterm forces canceled strings to null */
201		if (VALID_STRING(result)) {
202		    if (result == exit_attribute_mode
203			&& fix_me != 0) {
204			result = fix_me;
205			TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
206		    }
207		    if (area != 0
208			&& *area != 0) {
209			(void) strcpy(*area, result);
210			*area += strlen(*area) + 1;
211		    }
212		}
213		break;
214	    }
215	}
216    }
217    returnPtr(result);
218}
219