tput.c revision 87707
1/*-
2 * Copyright (c) 1980, 1988, 1993
3 *	The Regents of the University of California.  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#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.bin/tput/tput.c 87707 2001-12-11 23:39:38Z markm $");
37
38#ifndef lint
39static const char copyright[] =
40"@(#) Copyright (c) 1980, 1988, 1993\n\
41	The Regents of the University of California.  All rights reserved.\n";
42#endif
43
44#ifndef lint
45static const char sccsid[] = "@(#)tput.c	8.2 (Berkeley) 3/19/94";
46#endif
47
48#include <termios.h>
49
50#include <err.h>
51#include <termcap.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56
57#undef putchar
58#define outc putchar
59
60static void   prlongname __P((char *));
61static void   usage __P((void));
62static char **process __P((const char *, char *, char **));
63
64int
65main(argc, argv)
66	int argc;
67	char **argv;
68{
69	int ch, exitval, n;
70	char *cptr, *term, buf[1024], tbuf[1024];
71	const char *p;
72
73	term = NULL;
74	while ((ch = getopt(argc, argv, "T:")) != -1)
75		switch(ch) {
76		case 'T':
77			term = optarg;
78			break;
79		case '?':
80		default:
81			usage();
82		}
83	argc -= optind;
84	argv += optind;
85
86	if (!term && !(term = getenv("TERM")))
87errx(2, "no terminal type specified and no TERM environmental variable.");
88	if (tgetent(tbuf, term) != 1)
89		err(2, "tgetent failure");
90	for (exitval = 0; (p = *argv) != NULL; ++argv) {
91		switch (*p) {
92		case 'c':
93			if (!strcmp(p, "clear"))
94				p = "cl";
95			break;
96		case 'i':
97			if (!strcmp(p, "init"))
98				p = "is";
99			break;
100		case 'l':
101			if (!strcmp(p, "longname")) {
102				prlongname(tbuf);
103				continue;
104			}
105			break;
106		case 'r':
107			if (!strcmp(p, "reset"))
108				p = "rs";
109			break;
110		}
111		cptr = buf;
112		if (tgetstr(p, &cptr))
113			argv = process(p, buf, argv);
114		else if ((n = tgetnum(p)) != -1)
115			(void)printf("%d\n", n);
116		else
117			exitval = !tgetflag(p);
118	}
119	exit(exitval);
120}
121
122static void
123prlongname(buf)
124	char *buf;
125{
126	int savech;
127	char *p, *savep;
128
129	for (p = buf; *p && *p != ':'; ++p);
130	savech = *(savep = p);
131	for (*p = '\0'; p >= buf && *p != '|'; --p);
132	(void)printf("%s\n", p + 1);
133	*savep = savech;
134}
135
136static char **
137process(cap, str, argv)
138	const char *cap;
139	char *str, **argv;
140{
141	static char errfew[] =
142	    "not enough arguments (%d) for capability `%s'";
143	static char errmany[] =
144	    "too many arguments (%d) for capability `%s'";
145	static char erresc[] =
146	    "unknown %% escape `%c' for capability `%s'";
147	char *cp;
148	int arg_need, arg_rows, arg_cols;
149
150	/* Count how many values we need for this capability. */
151	for (cp = str, arg_need = 0; *cp != '\0'; cp++)
152		if (*cp == '%')
153			    switch (*++cp) {
154			    case 'd':
155			    case '2':
156			    case '3':
157			    case '.':
158			    case '+':
159				    arg_need++;
160				    break;
161			    case '%':
162			    case '>':
163			    case 'i':
164			    case 'r':
165			    case 'n':
166			    case 'B':
167			    case 'D':
168				    break;
169			    default:
170				/*
171				 * hpux has lot's of them, but we complain
172				 */
173				 warnx(erresc, *cp, cap);
174			    }
175
176	/* And print them. */
177	switch (arg_need) {
178	case 0:
179		(void)tputs(str, 1, outc);
180		break;
181	case 1:
182		arg_cols = 0;
183
184		if (*++argv == NULL || *argv[0] == '\0')
185			errx(2, errfew, 1, cap);
186		arg_rows = atoi(*argv);
187
188		(void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
189		break;
190	case 2:
191		if (*++argv == NULL || *argv[0] == '\0')
192			errx(2, errfew, 2, cap);
193		arg_cols = atoi(*argv);
194
195		if (*++argv == NULL || *argv[0] == '\0')
196			errx(2, errfew, 2, cap);
197		arg_rows = atoi(*argv);
198
199		(void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
200		break;
201
202	default:
203		errx(2, errmany, arg_need, cap);
204	}
205	return (argv);
206}
207
208static void
209usage()
210{
211	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
212	exit(1);
213}
214