xref: /netbsd-src/usr.bin/tput/tput.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: tput.c,v 1.22 2011/10/04 12:23:14 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.22 2011/10/04 12:23:14 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   usage(void) __dead;
57 static char **process(const char *, const char *, char **);
58 
59 int
60 main(int argc, char **argv)
61 {
62 	int ch, exitval, n;
63 	char *term;
64 	const char *p, *s;
65 	size_t pl;
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 				(void)printf("%s\n", longname());
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 		pl = strlen(p);
114 		if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
115 		    (pl <= 2 && (s = tgetstr(p, NULL)) != NULL))
116 			argv = process(p, s, argv);
117 		else if ((((n = tigetnum(p)) != -1 && n != -2 ) ||
118 			   (pl <= 2 && (n = tgetnum(p)) != -1)))
119 			(void)printf("%d\n", n);
120 		else {
121 			exitval = tigetflag(p);
122 			if (exitval == -1) {
123 				if (pl <= 2)
124 					exitval = !tgetflag(p);
125 				else
126 					exitval = 1;
127 			} else
128 				exitval = !exitval;
129 		}
130 
131 		if (argv == NULL)
132 			break;
133 	}
134 	return argv ? exitval : 2;
135 }
136 
137 static char **
138 process(const char *cap, const char *str, char **argv)
139 {
140 	static const char errfew[] =
141 	    "Not enough arguments (%d) for capability `%s'";
142 	static const char erresc[] =
143 	    "Unknown %% escape `%c' for capability `%s'";
144 	char c, l;
145 	const char *p;
146 	int arg_need, p1, p2, p3, p4, p5, p6, p7, p8, p9;
147 
148 	/* Count how many values we need for this capability. */
149 	arg_need = 0;
150 	p = str;
151 	while ((c = *p++) != '\0') {
152 		if (c != '%')
153 			continue;
154 		c = *p++;
155 		if (c == '\0')
156 			break;
157 		if (c != 'p')
158 			continue;
159 		c = *p++;
160 		if (c < '1' || c > '9')
161 			errx(2, erresc, c, cap);
162 		l = c - '0';
163 		if (l > arg_need)
164 			arg_need = l;
165 	}
166 
167 #define NEXT_ARG							      \
168 	{								      \
169 		if (*++argv == NULL || *argv[0] == '\0')		      \
170 			errx(2, errfew, 1, cap);			      \
171 	}
172 
173 	if (arg_need > 0) {
174 		NEXT_ARG;
175 		p1 = atoi(*argv);
176 	} else
177 		p1 = 0;
178 	if (arg_need > 1) {
179 		NEXT_ARG;
180 		p2 = atoi(*argv);
181 	} else
182 		p2 = 0;
183 	if (arg_need > 2) {
184 		NEXT_ARG;
185 		p3 = atoi(*argv);
186 	} else
187 		p3 = 0;
188 	if (arg_need > 3) {
189 		NEXT_ARG;
190 		p4 = atoi(*argv);
191 	} else
192 		p4 = 0;
193 	if (arg_need > 4) {
194 		NEXT_ARG;
195 		p5 = atoi(*argv);
196 	} else
197 		p5 = 0;
198 	if (arg_need > 5) {
199 		NEXT_ARG;
200 		p6 = atoi(*argv);
201 	} else
202 		p6 = 0;
203 	if (arg_need > 6) {
204 		NEXT_ARG;
205 		p7 = atoi(*argv);
206 	} else
207 		p7 = 0;
208 	if (arg_need > 7) {
209 		NEXT_ARG;
210 		p8 = atoi(*argv);
211 	} else
212 		p8 = 0;
213 	if (arg_need > 8) {
214 		NEXT_ARG;
215 		p9 = atoi(*argv);
216 	} else
217 		p9 = 0;
218 
219 	/* And print them. */
220 	(void)tputs(tparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9), 0, outc);
221 	return argv;
222 }
223 
224 static int
225 outc(int c)
226 {
227 	return putchar(c);
228 }
229 
230 static void
231 usage(void)
232 {
233 	(void)fprintf(stderr,
234 	    "Usage: %s [-T term] attribute [attribute-args] ...\n",
235 	    getprogname());
236 	exit(2);
237 }
238