xref: /netbsd-src/usr.bin/tput/tput.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: tput.c,v 1.20 2010/02/03 15:34:46 roy Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tput.c	8.3 (Berkeley) 4/28/95";
41 #endif
42 __RCSID("$NetBSD: tput.c,v 1.20 2010/02/03 15:34:46 roy Exp $");
43 #endif /* not lint */
44 
45 #include <termios.h>
46 
47 #include <err.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <term.h>
52 #include <termcap.h>
53 #include <unistd.h>
54 
55 static int    outc(int);
56 static void   prlongname(char *);
57 static void   usage(void) __dead;
58 static char **process(const char *, const char *, char **);
59 
60 int
61 main(int argc, char **argv)
62 {
63 	int ch, exitval, n;
64 	char *term, tbuf[1024];
65 	const char *p, *s;
66 
67 	term = NULL;
68 	while ((ch = getopt(argc, argv, "T:")) != -1)
69 		switch(ch) {
70 		case 'T':
71 			term = optarg;
72 			break;
73 		case '?':
74 		default:
75 			usage();
76 		}
77 	argc -= optind;
78 	argv += optind;
79 
80 	if (!term && !(term = getenv("TERM")))
81 		errx(2, "No terminal type specified and no TERM "
82 		    "variable set in the environment.");
83 	setupterm(term, 0, NULL);
84 	for (exitval = 0; (p = *argv) != NULL; ++argv) {
85 		switch (*p) {
86 		case 'c':
87 			if (!strcmp(p, "clear"))
88 				p = "clear";
89 			break;
90 		case 'i':
91 			if (!strcmp(p, "init")) {
92 				s = tigetstr("is1");
93 				if (s != NULL)
94 					tputs(s, 0, outc);
95 				p = "is2";
96 			}
97 			break;
98 		case 'l':
99 			if (!strcmp(p, "longname")) {
100 				prlongname(tbuf);
101 				continue;
102 			}
103 			break;
104 		case 'r':
105 			if (!strcmp(p, "reset")) {
106 				s = tigetstr("rs1");
107 				if (s != NULL)
108 					tputs(s, 0, outc);
109 				p = "rs2";
110 			}
111 			break;
112 		}
113 		if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
114 		    ((s = tgetstr(p, NULL)) != NULL))
115 			argv = process(p, s, argv);
116 		else if ((((n = tigetnum(p)) != -1 && n != -2 )||
117 			   (n = tgetnum(p)) != -1))
118 			(void)printf("%d\n", n);
119 		else {
120 			exitval = tigetflag(p);
121 			if (exitval == -1)
122 				exitval = !tgetflag(p);
123 			else
124 				exitval = !exitval;
125 		}
126 
127 		if (argv == NULL)
128 			break;
129 	}
130 	return argv ? exitval : 2;
131 }
132 
133 static void
134 prlongname(char *buf)
135 {
136 	int savech;
137 	char *p, *savep;
138 
139 	for (p = buf; *p && *p != ':'; ++p)
140 		continue;
141 	savech = *(savep = p);
142 	for (*p = '\0'; p >= buf && *p != '|'; --p)
143 		continue;
144 	(void)printf("%s\n", p + 1);
145 	*savep = savech;
146 }
147 
148 static char **
149 process(const char *cap, const char *str, char **argv)
150 {
151 	static const char errfew[] =
152 	    "Not enough arguments (%d) for capability `%s'";
153 	static const char erresc[] =
154 	    "Unknown %% escape `%c' for capability `%s'";
155 	char c, l;
156 	const char *p;
157 	int arg_need, p1, p2, p3, p4, p5, p6, p7, p8, p9;
158 
159 	/* Count how many values we need for this capability. */
160 	arg_need = 0;
161 	p = str;
162 	while ((c = *p++) != '\0') {
163 		if (c != '%')
164 			continue;
165 		c = *p++;
166 		if (c == '\0')
167 			break;
168 		if (c != 'p')
169 			continue;
170 		c = *p++;
171 		if (c < '1' || c > '9')
172 			errx(2, erresc, c, cap);
173 		l = c - '0';
174 		if (l > arg_need)
175 			arg_need = l;
176 	}
177 
178 #define NEXT_ARG							      \
179 	{								      \
180 		if (*++argv == NULL || *argv[0] == '\0')		      \
181 			errx(2, errfew, 1, cap);			      \
182 	}
183 
184 	if (arg_need > 0) {
185 		NEXT_ARG;
186 		p1 = atoi(*argv);
187 	} else
188 		p1 = 0;
189 	if (arg_need > 1) {
190 		NEXT_ARG;
191 		p2 = atoi(*argv);
192 	} else
193 		p2 = 0;
194 	if (arg_need > 2) {
195 		NEXT_ARG;
196 		p3 = atoi(*argv);
197 	} else
198 		p3 = 0;
199 	if (arg_need > 3) {
200 		NEXT_ARG;
201 		p4 = atoi(*argv);
202 	} else
203 		p4 = 0;
204 	if (arg_need > 4) {
205 		NEXT_ARG;
206 		p5 = atoi(*argv);
207 	} else
208 		p5 = 0;
209 	if (arg_need > 5) {
210 		NEXT_ARG;
211 		p6 = atoi(*argv);
212 	} else
213 		p6 = 0;
214 	if (arg_need > 6) {
215 		NEXT_ARG;
216 		p7 = atoi(*argv);
217 	} else
218 		p7 = 0;
219 	if (arg_need > 7) {
220 		NEXT_ARG;
221 		p8 = atoi(*argv);
222 	} else
223 		p8 = 0;
224 	if (arg_need > 8) {
225 		NEXT_ARG;
226 		p9 = atoi(*argv);
227 	} else
228 		p9 = 0;
229 
230 	/* And print them. */
231 	(void)tputs(tparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9), 0, outc);
232 	return argv;
233 }
234 
235 static int
236 outc(int c)
237 {
238 	return putchar(c);
239 }
240 
241 static void
242 usage(void)
243 {
244 	(void)fprintf(stderr,
245 	    "Usage: %s [-T term] attribute [attribute-args] ...\n",
246 	    getprogname());
247 	exit(2);
248 }
249