1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/* $OpenBSD: getterm.c,v 1.11 1996/09/03 16:04:57 deraadt Exp $ */
/*
* Copyright (c) 1996 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by SigmaSoft, Th. Lockert.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: getterm.c,v 1.11 1996/09/03 16:04:57 deraadt Exp $";
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include "term.h"
#include "term.private.h"
#include "pathnames.h"
#define PVECSIZ 32
#define MAXSIZE 256 /* Maximum allowed size of a terminal name field */
TERMINAL *cur_term;
char *_ti_buf;
char *UP;
char *BC;
char PC;
int LINES, COLS, TABSIZE;
char ttytype[MAXSIZE];
static int _ti_use_env = TRUE;
/*
* Internal routine to read in a terminal description.
* Currently supports reading from termcap files and databases
* only; should be extended to also support reading from binary
* terminfo files
*
* Will also set up global variables for compatibility with old
* termcap routines, as well as populate cur_term's capability
* variables. If called from the termcap tgetent() compatibility
* routine, it will copy the termcap entry into the buffer
* provided to that routine. Note that no other code in this
* library depends on the termcap entry being kept
*/
int
_ti_gettermcap(name)
const char *name;
{
register char *p;
register char *cp;
char *dummy;
char **fname;
char *home;
int i;
char pathbuf[MAXPATHLEN+1]; /* holds raw path of filenames */
char *pathvec[PVECSIZ]; /* to point to names in pathbuf */
char *termpath;
long num;
fname = pathvec;
p = pathbuf;
cp = getenv("TERMCAP");
/*
* TERMCAP can have one of two things in it. It can be the
* name of a file to use instead of /etc/termcap. In this
* case it better start with a "/". Or it can be an entry to
* use so we don't have to read the file. In this case it
* has to already have the newlines crunched out. If TERMCAP
* does not hold a file name then a path of names is searched
* instead. The path is found in the TERMPATH variable, or
* becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
*/
if (!cp || *cp != '/') { /* no TERMCAP or it holds an entry */
if ((termpath = getenv("TERMPATH")) != NULL)
strncpy(pathbuf, termpath, sizeof(pathbuf)-1);
else {
if ((home = getenv("HOME")) != NULL) {
/* set up default */
/* $HOME first */
strncpy(pathbuf, home, sizeof(pathbuf) - 1 -
strlen(_PATH_CAPDEF) - 1);
pathbuf[sizeof(pathbuf) - 1 - strlen(_PATH_CAPDEF) - 1] = '\0';
p += strlen(pathbuf); /* path, looking in */
*p++ = '/';
} /* if no $HOME look in current directory */
strncpy(p, _PATH_CAPDEF, sizeof(pathbuf) - 1 - (p - pathbuf));
}
}
else /* user-defined name in TERMCAP */
strncpy(pathbuf, cp, sizeof(pathbuf)-1); /* still can be tokenized */
pathbuf[sizeof(pathbuf)-1] = '\0';
*fname++ = pathbuf; /* tokenize path into vector of names */
while (*++p)
if (*p == ' ' || *p == ':') {
*p = '\0';
while (*++p)
if (*p != ' ' && *p != ':')
break;
if (*p == '\0')
break;
*fname++ = p;
if (fname >= pathvec + PVECSIZ) {
fname--;
break;
}
}
*fname = (char *) 0; /* mark end of vector */
if (cp && *cp && *cp != '/')
if (cgetset(cp) < 0)
return (-2);
dummy = NULL;
i = cgetent(&dummy, pathvec, (char *)name);
if (i == 0) {
char *s;
if ((s = home = strchr(dummy, ':')) == NULL) {
cur_term->name = strdup(name);
strncpy(ttytype, name, MAXSIZE - 1);
ttytype[MAXSIZE - 1] = '\0';
}
else {
strncpy(ttytype, dummy + (dummy[2] == '|' ? 3 : 0),
MIN(MAXSIZE - 1, s - dummy));
ttytype[MAXSIZE - 1] = '\0';
*home = '\0';
while (s > dummy && *s != '|')
s--;
if (s > dummy)
s++;
cur_term->name = strdup(s);
*home = ':';
}
for (i = 0; i < _ti_numcaps; i++) {
switch (_ti_captoidx[i].type) {
case TYPE_BOOL:
if (cgetcap(dummy, (char *)_ti_captoidx[i].name, ':') == NULL)
cur_term->bools[_ti_captoidx[i].idx] = 0;
else
cur_term->bools[_ti_captoidx[i].idx] = 1;
break;
case TYPE_NUM:
if (cgetnum(dummy, (char *)_ti_captoidx[i].name, &num) < 0)
cur_term->nums[_ti_captoidx[i].idx] = 0;
else
cur_term->nums[_ti_captoidx[i].idx] = (int)num;
break;
case TYPE_STR:
if (cgetstr(dummy, (char *)_ti_captoidx[i].name, &s) < 0)
cur_term->strs[_ti_captoidx[i].idx] = NULL;
else {
cur_term->strs[_ti_captoidx[i].idx] = _ti_captoinfo(s);
free(s);
}
break;
}
}
if (_ti_buf) {
strncpy(_ti_buf, dummy, 1023);
_ti_buf[1023] = '\0';
if ((cp = strrchr(_ti_buf, ':')) != NULL)
if (cp[1] != '\0')
cp[1] = '\0';
}
i = 0;
}
/* We are done with the returned termcap buffer now; free it */
if (dummy)
free(dummy);
/* we found a "tc" reference loop, return error */
if (i == -3)
return (-1);
return (i + 1);
}
/*
* Internal routine to read in a terminal description.
* Currently supports reading from termcap files and databases
* only; should be extended to also support reading from binary
* terminfo files
*
* Will also set up global variables for compatibility with old
* termcap routines, as well as populate cur_term's capability
* variables. If called from the termcap tgetent() compatibility
* routine, it will copy the termcap entry into the buffer
* provided to that routine. Note that no other code in this
* library depends on the termcap entry being kept
*/
int
_ti_getterminfo(name)
const char *name;
{
register char *p;
register char *cp;
char *dummy;
char **fname;
char *home;
int i;
char pathbuf[MAXPATHLEN+1]; /* holds raw path of filenames */
char *pathvec[PVECSIZ]; /* to point to names in pathbuf */
char *termpath;
long num;
fname = pathvec;
p = pathbuf;
/*
* TERMCAP can have one of two things in it. It can be the
* name of a file to use instead of /etc/termcap. In this
* case it better start with a "/". Or it can be an entry to
* use so we don't have to read the file. In this case it
* has to already have the newlines crunched out. If TERMCAP
* does not hold a file name then a path of names is searched
* instead. The path is found in the TERMINFO variable, or
* becomes "$HOME/.terminfo /usr/share/misc/terminfo" if no
* TERMINFO exists.
*/
if ((termpath = getenv("TERMINFO")) != NULL)
strncpy(pathbuf, termpath, sizeof(pathbuf) - 1);
else {
if ((home = getenv("HOME")) != NULL) {
/* set up default */
/* $HOME first */
strncpy(pathbuf, home, sizeof(pathbuf) - 1 -
strlen(_PATH_INFODEF) - 1);
pathbuf[sizeof(pathbuf) - 1 - strlen(_PATH_INFODEF) - 1] = '\0';
p += strlen(home); /* path, looking in */
*p++ = '/';
} /* if no $HOME look in current directory */
strncpy(p, _PATH_INFODEF, sizeof(pathbuf) - 1 - (p - pathbuf));
}
pathbuf[sizeof(pathbuf) - 1] = '\0';
*fname++ = pathbuf; /* tokenize path into vector of names */
while (*++p)
if (*p == ' ' || *p == ':') {
*p = '\0';
while (*++p)
if (*p != ' ' && *p != ':')
break;
if (*p == '\0')
break;
*fname++ = p;
if (fname >= pathvec + PVECSIZ) {
fname--;
break;
}
}
*fname = (char *) 0; /* mark end of vector */
(void) cgetset(NULL);
dummy = NULL;
i = cgetent(&dummy, pathvec, (char *)name);
if (i == 0) {
char *s;
if ((s = home = strchr(dummy, ':')) == NULL) {
cur_term->name = strdup(name);
strncpy(ttytype, name, MAXSIZE - 1);
ttytype[MAXSIZE - 1] = '\0';
}
else {
strncpy(ttytype, dummy + (dummy[2] == '|' ? 3 : 0),
MIN(MAXSIZE - 1, s - dummy));
ttytype[MAXSIZE - 1] = '\0';
*home = '\0';
while (s > dummy && *s != '|')
s--;
if (s > dummy)
s++;
cur_term->name = strdup(s);
*home = ':';
}
for (i = 0 ; i < _tBoolCnt ; i++) {
if (cgetcap(dummy, (char *)boolcodes[i], ':') == NULL)
cur_term->bools[i] = 0;
else
cur_term->bools[i] = 1;
}
for (i = 0 ; i < _tNumCnt ; i++) {
if (cgetnum(dummy, (char *)numcodes[i], &num) < 0)
cur_term->nums[i] = 0;
else
cur_term->nums[i] = (int)num;
}
for (i = 0 ; i < _tStrCnt ; i++) {
if (cgetstr(dummy, (char *)strcodes[i], &s) < 0)
cur_term->strs[i] = NULL;
else
cur_term->strs[i] = s;
}
if (_ti_buf) {
strncpy(_ti_buf, dummy, 1023);
_ti_buf[1023] = '\0';
if ((cp = strrchr(_ti_buf, ':')) != NULL)
if (cp[1] != '\0')
cp[1] = '\0';
}
i = 0;
}
/* We are done with the returned termcap buffer now; free it */
if (dummy)
free(dummy);
/* we found a "tc" reference loop, return error */
if (i == -3)
return (-1);
return (i + 1);
}
int
_ti_getterm(name)
const char *name;
{
#ifdef TIOCGWINSZ
struct winsize winsz;
#endif
int ret = 1;
char *s;
s = getenv("TERMCAP");
if (s && *s == '/')
s = NULL;
if (_ti_buf || s) {
if (_ti_gettermcap(name) != 1) {
del_curterm(cur_term);
if ((cur_term = calloc(sizeof(TERMINAL), 1)) == NULL)
errx(1, "No memory for terminal description");
ret = _ti_getterminfo(name);
}
}
else {
if (_ti_getterminfo(name) != 1) {
del_curterm(cur_term);
if ((cur_term = calloc(sizeof(TERMINAL), 1)) == NULL)
errx(1, "No memory for terminal description");
ret = _ti_gettermcap(name);
}
}
if (ret == 1) {
_ti_fillcap(cur_term);
UP = cursor_up;
BC = backspace_if_not_bs;
PC = pad_char ? pad_char[0] : '\0';
LINES = lines;
COLS = columns;
TABSIZE = init_tabs;
if (TABSIZE == 0)
TABSIZE = 8;
init_tabs = TABSIZE;
if (_ti_use_env) {
#ifdef TIOCGWINSZ
/*
* get the current window size, overrides entries in termcap
*/
if (ioctl(cur_term->fd, TIOCGWINSZ, &winsz) >= 0) {
if (winsz.ws_row > 0)
LINES = winsz.ws_row;
if (winsz.ws_col > 0)
COLS = winsz.ws_col;
}
#endif
/*
* LINES and COLS environment variables overrides any other
* method of getting the terminal window size
*/
if ((s = getenv("LINES")) != NULL)
LINES = atoi(s);
if ((s = getenv("COLS")) != NULL)
COLS = atoi(s);
}
lines = LINES;
columns = COLS;
}
return ret;
}
/*
* Allows the calling program to not have the window size or
* environment variables LINES and COLS override the termcap
* or terminfo lines/columns specifications
*/
void
use_env(flag)
int flag;
{
_ti_use_env = flag;
}
|