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