xref: /original-bsd/usr.bin/tput/tput.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1988, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)tput.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <sys/termios.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 
22 main(argc, argv)
23 	int argc;
24 	char **argv;
25 {
26 	extern char *optarg;
27 	extern int optind;
28 	int ch, exitval, n, outc();
29 	char *cptr, *p, *term, buf[1024], tbuf[1024];
30 	char *getenv(), *tgetstr(), *realname();
31 
32 	term = NULL;
33 	while ((ch = getopt(argc, argv, "T:")) != EOF)
34 		switch(ch) {
35 		case 'T':
36 			term = optarg;
37 			break;
38 		case '?':
39 		default:
40 			usage();
41 		}
42 	argc -= optind;
43 	argv += optind;
44 
45 	if (!term && !(term = getenv("TERM"))) {
46 		(void)fprintf(stderr, "tput: no terminal type specified.\n");
47 		exit(2);
48 	}
49 	if (tgetent(tbuf, term) != 1) {
50 		(void)fprintf(stderr, "tput: tgetent failure.\n");
51 		exit(2);
52 	}
53 	setospeed();
54 	for (cptr = buf, exitval = 0; p = *argv; ++argv) {
55 		switch(*p) {
56 		case 'c':
57 			if (!strcmp(p, "clear"))
58 				p = "cl";
59 			break;
60 		case 'i':
61 			if (!strcmp(p, "init"))
62 				p = "is";
63 			break;
64 		case 'l':
65 			if (!strcmp(p, "longname"))
66 				prlongname(tbuf);
67 			continue;
68 		case 'r':
69 			if (!strcmp(p, "reset"))
70 				p = "rs";
71 			break;
72 		}
73 		if (tgetstr(p, &cptr))
74 			(void)tputs(buf, 1, outc);
75 		else if ((n = tgetnum(p)) != -1)
76 			(void)printf("%d\n", n);
77 		else
78 			exitval = !tgetflag(p);
79 	}
80 	exit(exitval);
81 }
82 
83 prlongname(buf)
84 	char *buf;
85 {
86 	register char *p;
87 	int savech;
88 	char *savep;
89 
90 	for (p = buf; *p && *p != ':'; ++p);
91 	savech = *(savep = p);
92 	for (*p = '\0'; p >= buf && *p != '|'; --p);
93 	(void)printf("%s\n", p + 1);
94 	*savep = savech;
95 }
96 
97 setospeed()
98 {
99 	extern int errno, ospeed;
100 	struct termios t;
101 	char *strerror();
102 
103 	if (tcgetattr(STDOUT_FILENO, &t) != -1)
104 		ospeed = 0;
105 	else
106 		ospeed = cfgetospeed(&t);
107 }
108 
109 outc(c)
110 	int c;
111 {
112 	putchar(c);
113 }
114 
115 usage()
116 {
117 	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
118 	exit(1);
119 }
120