ncurses 4.2
[ncurses.git] / ncurses / lib_termcap.c
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.h>
41
42 MODULE_ID("$Id: lib_termcap.c,v 1.17 1998/02/11 12:13:54 tom Exp $")
43
44 /*
45    some of the code in here was contributed by:
46    Magnus Bengtsson, d6mbeng@dtek.chalmers.se
47 */
48
49 char PC;
50 char *UP;
51 char *BC;
52 short ospeed;
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
69 int tgetent(char *bufp GCC_UNUSED, const char *name)
70 {
71 int errcode;
72 #if defined(TERMIOS)
73 speed_t speed;
74 #endif
75
76         T(("calling tgetent"));
77         setupterm(name, STDOUT_FILENO, &errcode);
78
79         if (errcode != 1)
80                 return(errcode);
81
82         if (cursor_left)
83             if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
84                 backspace_if_not_bs = cursor_left;
85
86         /* we're required to export these */
87         if (pad_char != NULL)
88                 PC = pad_char[0];
89         if (cursor_up != NULL)
90                 UP = cursor_up;
91         if (backspace_if_not_bs != NULL)
92                 BC = backspace_if_not_bs;
93 #if defined(TERMIOS)
94         /*
95          * Back-convert to the funny speed encoding used by the old BSD
96          * curses library.  Method suggested by Andrey Chernov
97          * <ache@astral.msk.su>
98          */
99         if ((speed = cfgetospeed(&cur_term->Nttyb)) < 1)
100             ospeed = 1;         /* assume lowest non-hangup speed */
101         else
102         {
103                 const speed_t *sp;
104                 static const speed_t speeds[] = {
105 #ifdef B115200
106                         B115200,
107 #endif
108 #ifdef B57600
109                         B57600,
110 #endif
111 #ifdef B38400
112                         B38400,
113 #else
114 #ifdef EXTB
115                         EXTB,
116 #endif
117 #endif /* B38400 */
118 #ifdef B19200
119                         B19200,
120 #else
121 #ifdef EXTA
122                         EXTA,
123 #endif
124 #endif /* B19200 */
125                         B9600,
126                         B4800,
127                         B2400,
128                         B1800,
129                         B1200,
130                         B600,
131                         B300,
132                         B200,
133                         B150,
134                         B134,
135                         B110,
136                         B75,
137                         B50,
138                         B0,
139                 };
140 #define MAXSPEED        SIZEOF(speeds)
141
142                 for (sp = speeds; sp < speeds + MAXSPEED; sp++) {
143                         if (sp[0] <= speed) {
144                                 break;
145                         }
146                 }
147                 ospeed = MAXSPEED - (sp - speeds);
148         }
149 #else
150         ospeed = cur_term->Nttyb.sg_ospeed;
151 #endif
152
153 /* LINT_PREPRO
154 #if 0*/
155 #include <capdefaults.c>
156 /* LINT_PREPRO
157 #endif*/
158
159         return errcode;
160 }
161
162 /***************************************************************************
163  *
164  * tgetflag(str)
165  *
166  * Look up boolean termcap capability str and return its value (TRUE=1 if
167  * present, FALSE=0 if not).
168  *
169  ***************************************************************************/
170
171 int tgetflag(const char *id)
172 {
173 int i;
174
175         T(("tgetflag: %s", id));
176         if (cur_term != 0) {
177                 for (i = 0; i < BOOLCOUNT; i++)
178                         if (!strcmp(id, boolcodes[i]))
179                                 return cur_term->type.Booleans[i];
180         }
181         return ERR;
182 }
183
184 /***************************************************************************
185  *
186  * tgetnum(str)
187  *
188  * Look up numeric termcap capability str and return its value, or -1 if
189  * not given.
190  *
191  ***************************************************************************/
192
193 int tgetnum(const char *id)
194 {
195 int i;
196
197         T(("tgetnum: %s", id));
198         if (cur_term != 0) {
199                 for (i = 0; i < NUMCOUNT; i++)
200                         if (!strcmp(id, numcodes[i]))
201                                 return cur_term->type.Numbers[i];
202         }
203         return ERR;
204 }
205
206 /***************************************************************************
207  *
208  * tgetstr(str, area)
209  *
210  * Look up string termcap capability str and return a pointer to its value,
211  * or NULL if not given.
212  *
213  ***************************************************************************/
214
215 char *tgetstr(const char *id, char **area GCC_UNUSED)
216 {
217 int i;
218
219         T(("tgetstr: %s", id));
220         if (cur_term != 0) {
221                 for (i = 0; i < STRCOUNT; i++) {
222                         T(("trying %s", strcodes[i]));
223                         if (!strcmp(id, strcodes[i])) {
224                                 T(("found match : %s", cur_term->type.Strings[i]));
225                                 return cur_term->type.Strings[i];
226                         }
227                 }
228         }
229         return NULL;
230 }
231
232 /*
233  *      char *
234  *      tgoto(string, x, y)
235  *
236  *      Retained solely for upward compatibility.  Note the intentional
237  *      reversing of the last two arguments.
238  *
239  */
240
241 char *tgoto(const char *string, int x, int y)
242 {
243         return(tparm(string, y, x));
244 }