1*433d6423SLionel Sambuc /* tget 1.0 - get termcap values Author: Kees J. Bot
2*433d6423SLionel Sambuc * 6 Mar 1994
3*433d6423SLionel Sambuc */
4*433d6423SLionel Sambuc #define nil 0
5*433d6423SLionel Sambuc #include <stdio.h>
6*433d6423SLionel Sambuc #include <stdlib.h>
7*433d6423SLionel Sambuc #include <string.h>
8*433d6423SLionel Sambuc #include <termcap.h>
9*433d6423SLionel Sambuc
usage(void)10*433d6423SLionel Sambuc void usage(void)
11*433d6423SLionel Sambuc {
12*433d6423SLionel Sambuc fprintf(stderr,
13*433d6423SLionel Sambuc "Usage: tget [-flag id] [-num id] [-str id] [-goto col line] [[-echo] string]\n"
14*433d6423SLionel Sambuc );
15*433d6423SLionel Sambuc exit(-1);
16*433d6423SLionel Sambuc }
17*433d6423SLionel Sambuc
main(int argc,char ** argv)18*433d6423SLionel Sambuc int main(int argc, char **argv)
19*433d6423SLionel Sambuc {
20*433d6423SLionel Sambuc char termbuf[1024];
21*433d6423SLionel Sambuc char string[256], *pstr;
22*433d6423SLionel Sambuc char *term;
23*433d6423SLionel Sambuc int i;
24*433d6423SLionel Sambuc int excode= 0;
25*433d6423SLionel Sambuc
26*433d6423SLionel Sambuc if ((term= getenv("TERM")) == nil) {
27*433d6423SLionel Sambuc fprintf(stderr, "tget: $TERM is not set\n");
28*433d6423SLionel Sambuc exit(-1);
29*433d6423SLionel Sambuc }
30*433d6423SLionel Sambuc
31*433d6423SLionel Sambuc if (tgetent(termbuf, term) != 1) {
32*433d6423SLionel Sambuc fprintf(stderr, "tget: no termcap entry for '%s'\n", term);
33*433d6423SLionel Sambuc exit(-1);
34*433d6423SLionel Sambuc }
35*433d6423SLionel Sambuc
36*433d6423SLionel Sambuc for (i= 1; i < argc; i++) {
37*433d6423SLionel Sambuc char *option= argv[i];
38*433d6423SLionel Sambuc char *id;
39*433d6423SLionel Sambuc
40*433d6423SLionel Sambuc if (option[0] != '-') {
41*433d6423SLionel Sambuc fputs(option, stdout);
42*433d6423SLionel Sambuc continue;
43*433d6423SLionel Sambuc }
44*433d6423SLionel Sambuc
45*433d6423SLionel Sambuc if (++i == argc) usage();
46*433d6423SLionel Sambuc id= argv[i];
47*433d6423SLionel Sambuc
48*433d6423SLionel Sambuc if (strcmp(option, "-flag") == 0) {
49*433d6423SLionel Sambuc excode= tgetflag(id) ? 0 : 1;
50*433d6423SLionel Sambuc } else
51*433d6423SLionel Sambuc if (strcmp(option, "-num") == 0) {
52*433d6423SLionel Sambuc int num;
53*433d6423SLionel Sambuc
54*433d6423SLionel Sambuc if ((num= tgetnum(id)) == -1) {
55*433d6423SLionel Sambuc excode= 1;
56*433d6423SLionel Sambuc } else {
57*433d6423SLionel Sambuc excode= 0;
58*433d6423SLionel Sambuc printf("%d", num);
59*433d6423SLionel Sambuc }
60*433d6423SLionel Sambuc } else
61*433d6423SLionel Sambuc if (strcmp(option, "-str") == 0) {
62*433d6423SLionel Sambuc char *str;
63*433d6423SLionel Sambuc
64*433d6423SLionel Sambuc if ((str= tgetstr(id, (pstr= string, &pstr))) == nil) {
65*433d6423SLionel Sambuc excode= 1;
66*433d6423SLionel Sambuc } else {
67*433d6423SLionel Sambuc excode= 0;
68*433d6423SLionel Sambuc tputs(str, 0, putchar);
69*433d6423SLionel Sambuc }
70*433d6423SLionel Sambuc } else
71*433d6423SLionel Sambuc if (strcmp(option, "-goto") == 0) {
72*433d6423SLionel Sambuc char *cm;
73*433d6423SLionel Sambuc int col, line;
74*433d6423SLionel Sambuc
75*433d6423SLionel Sambuc col= atoi(id);
76*433d6423SLionel Sambuc if (++i == argc) usage();
77*433d6423SLionel Sambuc line= atoi(argv[i]);
78*433d6423SLionel Sambuc
79*433d6423SLionel Sambuc if ((cm= tgetstr("cm", (pstr= string, &pstr))) == nil) {
80*433d6423SLionel Sambuc excode= 1;
81*433d6423SLionel Sambuc } else {
82*433d6423SLionel Sambuc excode= 0;
83*433d6423SLionel Sambuc tputs(tgoto(cm, col, line), 0, putchar);
84*433d6423SLionel Sambuc }
85*433d6423SLionel Sambuc } else
86*433d6423SLionel Sambuc if (strcmp(option, "-echo") == 0) {
87*433d6423SLionel Sambuc fputs(id, stdout);
88*433d6423SLionel Sambuc } else {
89*433d6423SLionel Sambuc usage();
90*433d6423SLionel Sambuc }
91*433d6423SLionel Sambuc }
92*433d6423SLionel Sambuc exit(excode);
93*433d6423SLionel Sambuc }
94