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
|
/* tget 1.0 - get termcap values Author: Kees J. Bot
* 6 Mar 1994
*/
#define nil 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termcap.h>
void usage(void)
{
fprintf(stderr,
"Usage: tget [-flag id] [-num id] [-str id] [-goto col line] [[-echo] string]\n"
);
exit(-1);
}
int main(int argc, char **argv)
{
char termbuf[1024];
char string[256], *pstr;
char *term;
int i;
int excode= 0;
if ((term= getenv("TERM")) == nil) {
fprintf(stderr, "tget: $TERM is not set\n");
exit(-1);
}
if (tgetent(termbuf, term) != 1) {
fprintf(stderr, "tget: no termcap entry for '%s'\n", term);
exit(-1);
}
for (i= 1; i < argc; i++) {
char *option= argv[i];
char *id;
if (option[0] != '-') {
fputs(option, stdout);
continue;
}
if (++i == argc) usage();
id= argv[i];
if (strcmp(option, "-flag") == 0) {
excode= tgetflag(id) ? 0 : 1;
} else
if (strcmp(option, "-num") == 0) {
int num;
if ((num= tgetnum(id)) == -1) {
excode= 1;
} else {
excode= 0;
printf("%d", num);
}
} else
if (strcmp(option, "-str") == 0) {
char *str;
if ((str= tgetstr(id, (pstr= string, &pstr))) == nil) {
excode= 1;
} else {
excode= 0;
tputs(str, 0, putchar);
}
} else
if (strcmp(option, "-goto") == 0) {
char *cm;
int col, line;
col= atoi(id);
if (++i == argc) usage();
line= atoi(argv[i]);
if ((cm= tgetstr("cm", (pstr= string, &pstr))) == nil) {
excode= 1;
} else {
excode= 0;
tputs(tgoto(cm, col, line), 0, putchar);
}
} else
if (strcmp(option, "-echo") == 0) {
fputs(id, stdout);
} else {
usage();
}
}
exit(excode);
}
|