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