tput.c revision 163283
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1980, 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
3487707Smarkm#include <sys/cdefs.h>
3587707Smarkm
3687707Smarkm__FBSDID("$FreeBSD: head/usr.bin/tput/tput.c 163283 2006-10-12 21:10:55Z ru $");
3787707Smarkm
381590Srgrimes#ifndef lint
3987707Smarkmstatic const char copyright[] =
401590Srgrimes"@(#) Copyright (c) 1980, 1988, 1993\n\
411590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
4287707Smarkm#endif
431590Srgrimes
441590Srgrimes#ifndef lint
4587707Smarkmstatic const char sccsid[] = "@(#)tput.c	8.2 (Berkeley) 3/19/94";
4665428Simp#endif
471590Srgrimes
485080Sache#include <termios.h>
491590Srgrimes
501590Srgrimes#include <err.h>
515080Sache#include <termcap.h>
521590Srgrimes#include <stdio.h>
531590Srgrimes#include <stdlib.h>
5478718Sdd#include <string.h>
551590Srgrimes#include <unistd.h>
561590Srgrimes
575080Sache#undef putchar
583031Sdg#define outc putchar
593031Sdg
6092922Simpstatic void   prlongname(char *);
6192922Simpstatic void   usage(void);
6292922Simpstatic char **process(const char *, char *, char **);
631590Srgrimes
641590Srgrimesint
65102944Sdwmalonemain(int argc, char **argv)
661590Srgrimes{
671590Srgrimes	int ch, exitval, n;
6887707Smarkm	char *cptr, *term, buf[1024], tbuf[1024];
6987707Smarkm	const char *p;
701590Srgrimes
711590Srgrimes	term = NULL;
7224360Simp	while ((ch = getopt(argc, argv, "T:")) != -1)
731590Srgrimes		switch(ch) {
741590Srgrimes		case 'T':
751590Srgrimes			term = optarg;
761590Srgrimes			break;
771590Srgrimes		case '?':
781590Srgrimes		default:
791590Srgrimes			usage();
801590Srgrimes		}
811590Srgrimes	argc -= optind;
821590Srgrimes	argv += optind;
831590Srgrimes
84163283Sru	if (argc < 1)
85163283Sru		usage();
86163283Sru
871590Srgrimes	if (!term && !(term = getenv("TERM")))
881590Srgrimeserrx(2, "no terminal type specified and no TERM environmental variable.");
891590Srgrimes	if (tgetent(tbuf, term) != 1)
9098218Stjr		err(3, "tgetent failure");
911590Srgrimes	for (exitval = 0; (p = *argv) != NULL; ++argv) {
921590Srgrimes		switch (*p) {
931590Srgrimes		case 'c':
941590Srgrimes			if (!strcmp(p, "clear"))
951590Srgrimes				p = "cl";
961590Srgrimes			break;
971590Srgrimes		case 'i':
981590Srgrimes			if (!strcmp(p, "init"))
991590Srgrimes				p = "is";
1001590Srgrimes			break;
1011590Srgrimes		case 'l':
1025080Sache			if (!strcmp(p, "longname")) {
1031590Srgrimes				prlongname(tbuf);
1045080Sache				continue;
1055080Sache			}
1065080Sache			break;
1071590Srgrimes		case 'r':
1081590Srgrimes			if (!strcmp(p, "reset"))
1091590Srgrimes				p = "rs";
1101590Srgrimes			break;
1111590Srgrimes		}
1121590Srgrimes		cptr = buf;
1131590Srgrimes		if (tgetstr(p, &cptr))
1141590Srgrimes			argv = process(p, buf, argv);
1151590Srgrimes		else if ((n = tgetnum(p)) != -1)
1161590Srgrimes			(void)printf("%d\n", n);
1171590Srgrimes		else
1181590Srgrimes			exitval = !tgetflag(p);
1191590Srgrimes	}
1201590Srgrimes	exit(exitval);
1211590Srgrimes}
1221590Srgrimes
1231590Srgrimesstatic void
124102944Sdwmaloneprlongname(char *buf)
1251590Srgrimes{
1261590Srgrimes	int savech;
1271590Srgrimes	char *p, *savep;
1281590Srgrimes
1291590Srgrimes	for (p = buf; *p && *p != ':'; ++p);
1301590Srgrimes	savech = *(savep = p);
1311590Srgrimes	for (*p = '\0'; p >= buf && *p != '|'; --p);
1321590Srgrimes	(void)printf("%s\n", p + 1);
1331590Srgrimes	*savep = savech;
1341590Srgrimes}
1351590Srgrimes
1361590Srgrimesstatic char **
137102944Sdwmaloneprocess(const char *cap, char *str, char **argv)
1381590Srgrimes{
13990165Skris	static const char errfew[] =
1401590Srgrimes	    "not enough arguments (%d) for capability `%s'";
14190165Skris	static const char errmany[] =
1421590Srgrimes	    "too many arguments (%d) for capability `%s'";
14390165Skris	static const char erresc[] =
1441590Srgrimes	    "unknown %% escape `%c' for capability `%s'";
1451590Srgrimes	char *cp;
1461590Srgrimes	int arg_need, arg_rows, arg_cols;
1471590Srgrimes
1481590Srgrimes	/* Count how many values we need for this capability. */
1491590Srgrimes	for (cp = str, arg_need = 0; *cp != '\0'; cp++)
1501590Srgrimes		if (*cp == '%')
1511590Srgrimes			    switch (*++cp) {
1521590Srgrimes			    case 'd':
1531590Srgrimes			    case '2':
1541590Srgrimes			    case '3':
1551590Srgrimes			    case '.':
1561590Srgrimes			    case '+':
1571590Srgrimes				    arg_need++;
1581590Srgrimes				    break;
1591590Srgrimes			    case '%':
1601590Srgrimes			    case '>':
1611590Srgrimes			    case 'i':
1621590Srgrimes			    case 'r':
1631590Srgrimes			    case 'n':
1641590Srgrimes			    case 'B':
1651590Srgrimes			    case 'D':
1661590Srgrimes				    break;
16797763Sache			    case 'p':
16897763Sache				    if (cp[1]) {
16997763Sache					cp++;
17097763Sache					break;
17197763Sache				    }
1721590Srgrimes			    default:
1731590Srgrimes				/*
1741590Srgrimes				 * hpux has lot's of them, but we complain
1751590Srgrimes				 */
17624542Sjmg				 warnx(erresc, *cp, cap);
1771590Srgrimes			    }
1781590Srgrimes
1791590Srgrimes	/* And print them. */
1801590Srgrimes	switch (arg_need) {
1811590Srgrimes	case 0:
1821590Srgrimes		(void)tputs(str, 1, outc);
1831590Srgrimes		break;
1841590Srgrimes	case 1:
1851590Srgrimes		arg_cols = 0;
1861590Srgrimes
1871590Srgrimes		if (*++argv == NULL || *argv[0] == '\0')
1881590Srgrimes			errx(2, errfew, 1, cap);
1891590Srgrimes		arg_rows = atoi(*argv);
1901590Srgrimes
1911590Srgrimes		(void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc);
1921590Srgrimes		break;
1931590Srgrimes	case 2:
1941590Srgrimes		if (*++argv == NULL || *argv[0] == '\0')
1951590Srgrimes			errx(2, errfew, 2, cap);
1961590Srgrimes		arg_cols = atoi(*argv);
1971590Srgrimes
1981590Srgrimes		if (*++argv == NULL || *argv[0] == '\0')
1991590Srgrimes			errx(2, errfew, 2, cap);
2001590Srgrimes		arg_rows = atoi(*argv);
2011590Srgrimes
2021590Srgrimes		(void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc);
2031590Srgrimes		break;
2041590Srgrimes
2051590Srgrimes	default:
2061590Srgrimes		errx(2, errmany, arg_need, cap);
2071590Srgrimes	}
2081590Srgrimes	return (argv);
2091590Srgrimes}
2101590Srgrimes
2111590Srgrimesstatic void
212102944Sdwmaloneusage(void)
2131590Srgrimes{
2141590Srgrimes	(void)fprintf(stderr, "usage: tput [-T term] attribute ...\n");
21598218Stjr	exit(2);
2161590Srgrimes}
217