xref: /netbsd-src/usr.bin/tput/tput.c (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
1 /*-
2  * Copyright (c) 1980, 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980, 1988 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)tput.c	5.7 (Berkeley) 6/7/90";*/
42 static char rcsid[] = "$Id: tput.c,v 1.2 1993/08/01 18:04:38 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <sys/termios.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 
49 main(argc, argv)
50 	int argc;
51 	char **argv;
52 {
53 	extern char *optarg;
54 	extern int optind;
55 	int ch, exitval, n, outc();
56 	char *cptr, *p, *term, buf[1024], tbuf[1024];
57 	char *getenv(), *tgetstr(), *realname();
58 
59 	term = NULL;
60 	while ((ch = getopt(argc, argv, "T:")) != EOF)
61 		switch(ch) {
62 		case 'T':
63 			term = optarg;
64 			break;
65 		case '?':
66 		default:
67 			usage();
68 		}
69 	argc -= optind;
70 	argv += optind;
71 
72 	if (!term && !(term = getenv("TERM"))) {
73 		(void)fprintf(stderr, "tput: no terminal type specified.\n");
74 		exit(2);
75 	}
76 	if (tgetent(tbuf, term) != 1) {
77 		(void)fprintf(stderr, "tput: tgetent failure.\n");
78 		exit(2);
79 	}
80 	setospeed();
81 	for (cptr = buf, exitval = 0; p = *argv; ++argv) {
82 		switch(*p) {
83 		case 'c':
84 			if (!strcmp(p, "clear"))
85 				p = "cl";
86 			break;
87 		case 'i':
88 			if (!strcmp(p, "init"))
89 				p = "is";
90 			break;
91 		case 'l':
92 			if (!strcmp(p, "longname"))
93 				prlongname(tbuf);
94 			continue;
95 		case 'r':
96 			if (!strcmp(p, "reset"))
97 				p = "rs";
98 			break;
99 		}
100 		if (tgetstr(p, &cptr))
101 			(void)tputs(buf, 1, outc);
102 		else if ((n = tgetnum(p)) != -1)
103 			(void)printf("%d\n", n);
104 		else
105 			exitval = !tgetflag(p);
106 	}
107 	exit(exitval);
108 }
109 
110 prlongname(buf)
111 	char *buf;
112 {
113 	register char *p;
114 	int savech;
115 	char *savep;
116 
117 	for (p = buf; *p && *p != ':'; ++p);
118 	savech = *(savep = p);
119 	for (*p = '\0'; p >= buf && *p != '|'; --p);
120 	(void)printf("%s\n", p + 1);
121 	*savep = savech;
122 }
123 
124 setospeed()
125 {
126 	extern int errno, ospeed;
127 	struct termios t;
128 	char *strerror();
129 
130 	if (tcgetattr(STDOUT_FILENO, &t) != -1)
131 		ospeed = 0;
132 	else
133 		ospeed = cfgetospeed(&t);
134 }
135 
136 outc(c)
137 	int c;
138 {
139 	putchar(c);
140 }
141 
142 usage()
143 {
144 	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
145 	exit(1);
146 }
147