xref: /netbsd-src/usr.bin/tput/tput.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
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.4 1994/03/19 07:42:18 cgd Exp $";
43 #endif /* not lint */
44 
45 #include <sys/termios.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <curses.h>
50 
51 static void   prlongname __P((char *));
52 static void   setospeed __P((void));
53 static void   outc	 __P((int));
54 static void   usage __P((void));
55 static char **process __P((char *, char *, char **));
56 
57 int
58 main(argc, argv)
59 	int argc;
60 	char **argv;
61 {
62 	extern char *optarg;
63 	extern int optind;
64 	int ch, exitval, n;
65 	char *cptr, *p, *term, buf[1024], tbuf[1024];
66 
67 	term = NULL;
68 	while ((ch = getopt(argc, argv, "T:")) != EOF)
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 		(void)fprintf(stderr, "tput: no terminal type specified.\n");
82 		exit(2);
83 	}
84 	if (tgetent(tbuf, term) != 1) {
85 		(void)fprintf(stderr, "tput: tgetent failure.\n");
86 		exit(2);
87 	}
88 	setospeed();
89 	for (exitval = 0; (p = *argv) != NULL; ++argv) {
90 		switch(*p) {
91 		case 'c':
92 			if (!strcmp(p, "clear"))
93 				p = "cl";
94 			break;
95 		case 'i':
96 			if (!strcmp(p, "init"))
97 				p = "is";
98 			break;
99 		case 'l':
100 			if (!strcmp(p, "longname")) {
101 				prlongname(tbuf);
102 				continue;
103 			}
104 			break;
105 		case 'r':
106 			if (!strcmp(p, "reset"))
107 				p = "rs";
108 			break;
109 		}
110 		cptr = buf;
111 		if (tgetstr(p, &cptr))
112 			argv = process(p, buf, argv);
113 		else if ((n = tgetnum(p)) != -1)
114 			(void)printf("%d\n", n);
115 		else
116 			exitval = !tgetflag(p);
117 
118 		if (argv == NULL)
119 			break;
120 	}
121 	exit(argv ? exitval : 2);
122 }
123 
124 static void
125 prlongname(buf)
126 	char *buf;
127 {
128 	register char *p;
129 	int savech;
130 	char *savep;
131 
132 	for (p = buf; *p && *p != ':'; ++p)
133 		continue;
134 	savech = *(savep = p);
135 	for (*p = '\0'; p >= buf && *p != '|'; --p)
136 		continue;
137 	(void)printf("%s\n", p + 1);
138 	*savep = savech;
139 }
140 
141 static void
142 setospeed()
143 {
144 #undef ospeed
145 	extern short ospeed;
146 	struct termios t;
147 
148 	if (tcgetattr(STDOUT_FILENO, &t) != -1)
149 		ospeed = 0;
150 	else
151 		ospeed = cfgetospeed(&t);
152 }
153 
154 static void
155 outc(c)
156 	int c;
157 {
158 	putchar(c);
159 }
160 
161 static void
162 usage()
163 {
164 	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
165 	exit(1);
166 }
167 
168 static char **
169 process(cap, str, argv)
170 	char *cap;
171 	char *str;
172 	char **argv;
173 {
174 	static char errfew[] =
175 		 "tput: Not enough arguments (%d) for capability `%s'\n";
176 	static char errmany[] =
177 		 "tput: Too many arguments (%d) for capability `%s'\n";
178 	static char erresc[] =
179 		 "tput: Unknown %% escape `%c' for capability `%s'\n";
180 	/*
181 	 * Count home many values we need for this capability.
182 	 */
183 	char *cp;
184 	int arg_need, arg_rows, arg_cols;
185 	for (cp = str, arg_need = 0; *cp; cp++)
186 		if (*cp == '%')
187 			    switch (*++cp) {
188 			    case 'd':
189 			    case '2':
190 			    case '3':
191 			    case '.':
192 			    case '+':
193 				    arg_need++;
194 				    break;
195 
196 			    case '%':
197 			    case '>':
198 			    case 'i':
199 			    case 'r':
200 			    case 'n':
201 			    case 'B':
202 			    case 'D':
203 				    break;
204 
205 			    default:
206 				/*
207 				 * hpux has lot's of them, but we complain
208 				 */
209 			        (void)fprintf(stderr, erresc, *cp, cap);
210 				return NULL;
211 			    }
212 
213 	/*
214 	 * And print them
215 	 */
216 	switch (arg_need) {
217 	case 0:
218 		(void) tputs(str, 1, outc);
219 		break;
220 
221 	case 1:
222 		arg_cols = 0;
223 
224 		argv++;
225 		if (!*argv || *argv[0] == '\0') {
226 			(void)fprintf(stderr, errfew, 1, cap);
227 			return NULL;
228 		}
229 		arg_rows = atoi(*argv);
230 
231 		(void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
232 		break;
233 
234 	case 2:
235 		argv++;
236 		if (!*argv || *argv[0] == '\0') {
237 			(void)fprintf(stderr, errfew, 2, cap);
238 			return NULL;
239 		}
240 		arg_cols = atoi(*argv);
241 
242 		argv++;
243 		if (!*argv || *argv[0] == '\0') {
244 			(void)fprintf(stderr, errfew, 2, cap);
245 			return NULL;
246 		}
247 		arg_rows = atoi(*argv);
248 
249 		(void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
250 		break;
251 
252 	default:
253 		(void)fprintf(stderr, errmany, arg_need, cap);
254 		return NULL;
255 	}
256 	return argv;
257 }
258