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