xref: /original-bsd/usr.bin/tput/tput.c (revision 780b584d)
1 /*
2  * Copyright (c) 1980, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)tput.c	5.4 (Berkeley) 10/31/89";
26 #endif /* not lint */
27 
28 #include <sys/termios.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 
32 main(argc, argv)
33 	int argc;
34 	char **argv;
35 {
36 	extern char *optarg;
37 	extern int optind;
38 	int ch, exitval, n, outc();
39 	char *cptr, *p, *term, buf[1024], tbuf[1024];
40 	char *getenv(), *tgetstr(), *realname();
41 
42 	term = NULL;
43 	while ((ch = getopt(argc, argv, "T:")) != EOF)
44 		switch(ch) {
45 		case 'T':
46 			term = optarg;
47 			break;
48 		case '?':
49 		default:
50 			usage();
51 		}
52 	argc -= optind;
53 	argv += optind;
54 
55 	if (!term && !(term = getenv("TERM"))) {
56 		(void)fprintf(stderr, "tput: no terminal type specified.\n");
57 		exit(2);
58 	}
59 	if (tgetent(tbuf, term) != 1) {
60 		(void)fprintf(stderr, "tput: tgetent failure.\n");
61 		exit(2);
62 	}
63 	setospeed();
64 	for (cptr = buf; p = *argv; ++argv) {
65 		exitval = 0;
66 		switch(*p) {
67 		case 'c':
68 			if (!strcmp(p, "clear"))
69 				p = "cl";
70 			break;
71 		case 'i':
72 			if (!strcmp(p, "init"))
73 				p = "is";
74 			break;
75 		case 'l':
76 			if (!strcmp(p, "longname"))
77 				prlongname(tbuf);
78 			continue;
79 		case 'r':
80 			if (!strcmp(p, "reset"))
81 				p = "rs";
82 			break;
83 		}
84 		if (tgetstr(p, &cptr))
85 			(void)tputs(buf, 1, outc);
86 		else if ((n = tgetnum(p)) != -1)
87 			(void)printf("%d\n", n);
88 		else
89 			exitval = !tgetflag(p);
90 	}
91 	exit(exitval);
92 }
93 
94 prlongname(buf)
95 	char *buf;
96 {
97 	register char *p;
98 	int savech;
99 	char *savep;
100 
101 	for (p = buf; *p && *p != ':'; ++p);
102 	savech = *(savep = p);
103 	for (*p = '\0'; p >= buf && *p != '|'; --p);
104 	(void)printf("%s\n", p + 1);
105 	*savep = savech;
106 }
107 
108 setospeed()
109 {
110 	extern int errno, ospeed;
111 	struct termios t;
112 	char *strerror();
113 
114 	if (tcgetattr(STDOUT_FILENO, &t) == -1) {
115 		(void)fprintf(stderr, "tput: %s\n", strerror(errno));
116 		exit(1);
117 	}
118 	ospeed = cfgetospeed(&t);
119 }
120 
121 outc(c)
122 	int c;
123 {
124 	putchar(c);
125 }
126 
127 usage()
128 {
129 	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
130 	exit(1);
131 }
132