tput.c revision 65428
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 341590Srgrimes#ifndef lint 351590Srgrimesstatic char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1980, 1988, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 4165428Simp#if 0 421590Srgrimesstatic char sccsid[] = "@(#)tput.c 8.2 (Berkeley) 3/19/94"; 4365428Simp#endif 4465428Simpstatic char rcsid[] = 4565428Simp"$FreeBSD: head/usr.bin/tput/tput.c 65428 2000-09-04 06:09:54Z imp $"; 461590Srgrimes#endif /* not lint */ 471590Srgrimes 485080Sache#include <termios.h> 491590Srgrimes 501590Srgrimes#include <err.h> 515080Sache#include <termcap.h> 521590Srgrimes#include <stdio.h> 531590Srgrimes#include <stdlib.h> 541590Srgrimes#include <unistd.h> 551590Srgrimes 565080Sache#undef putchar 573031Sdg#define outc putchar 583031Sdg 591590Srgrimesstatic void prlongname __P((char *)); 601590Srgrimesstatic void usage __P((void)); 611590Srgrimesstatic char **process __P((char *, char *, char **)); 621590Srgrimes 631590Srgrimesint 641590Srgrimesmain(argc, argv) 651590Srgrimes int argc; 661590Srgrimes char **argv; 671590Srgrimes{ 681590Srgrimes int ch, exitval, n; 691590Srgrimes char *cptr, *p, *term, buf[1024], tbuf[1024]; 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 841590Srgrimes if (!term && !(term = getenv("TERM"))) 851590Srgrimeserrx(2, "no terminal type specified and no TERM environmental variable."); 861590Srgrimes if (tgetent(tbuf, term) != 1) 871590Srgrimes err(2, "tgetent failure"); 881590Srgrimes for (exitval = 0; (p = *argv) != NULL; ++argv) { 891590Srgrimes switch (*p) { 901590Srgrimes case 'c': 911590Srgrimes if (!strcmp(p, "clear")) 921590Srgrimes p = "cl"; 931590Srgrimes break; 941590Srgrimes case 'i': 951590Srgrimes if (!strcmp(p, "init")) 961590Srgrimes p = "is"; 971590Srgrimes break; 981590Srgrimes case 'l': 995080Sache if (!strcmp(p, "longname")) { 1001590Srgrimes prlongname(tbuf); 1015080Sache continue; 1025080Sache } 1035080Sache break; 1041590Srgrimes case 'r': 1051590Srgrimes if (!strcmp(p, "reset")) 1061590Srgrimes p = "rs"; 1071590Srgrimes break; 1081590Srgrimes } 1091590Srgrimes cptr = buf; 1101590Srgrimes if (tgetstr(p, &cptr)) 1111590Srgrimes argv = process(p, buf, argv); 1121590Srgrimes else if ((n = tgetnum(p)) != -1) 1131590Srgrimes (void)printf("%d\n", n); 1141590Srgrimes else 1151590Srgrimes exitval = !tgetflag(p); 1161590Srgrimes } 1171590Srgrimes exit(exitval); 1181590Srgrimes} 1191590Srgrimes 1201590Srgrimesstatic void 1211590Srgrimesprlongname(buf) 1221590Srgrimes char *buf; 1231590Srgrimes{ 1241590Srgrimes int savech; 1251590Srgrimes char *p, *savep; 1261590Srgrimes 1271590Srgrimes for (p = buf; *p && *p != ':'; ++p); 1281590Srgrimes savech = *(savep = p); 1291590Srgrimes for (*p = '\0'; p >= buf && *p != '|'; --p); 1301590Srgrimes (void)printf("%s\n", p + 1); 1311590Srgrimes *savep = savech; 1321590Srgrimes} 1331590Srgrimes 1341590Srgrimesstatic char ** 1351590Srgrimesprocess(cap, str, argv) 1361590Srgrimes char *cap, *str, **argv; 1371590Srgrimes{ 1381590Srgrimes static char errfew[] = 1391590Srgrimes "not enough arguments (%d) for capability `%s'"; 1401590Srgrimes static char errmany[] = 1411590Srgrimes "too many arguments (%d) for capability `%s'"; 1421590Srgrimes static char erresc[] = 1431590Srgrimes "unknown %% escape `%c' for capability `%s'"; 1441590Srgrimes char *cp; 1451590Srgrimes int arg_need, arg_rows, arg_cols; 1461590Srgrimes 1471590Srgrimes /* Count how many values we need for this capability. */ 1481590Srgrimes for (cp = str, arg_need = 0; *cp != '\0'; cp++) 1491590Srgrimes if (*cp == '%') 1501590Srgrimes switch (*++cp) { 1511590Srgrimes case 'd': 1521590Srgrimes case '2': 1531590Srgrimes case '3': 1541590Srgrimes case '.': 1551590Srgrimes case '+': 1561590Srgrimes arg_need++; 1571590Srgrimes break; 1581590Srgrimes case '%': 1591590Srgrimes case '>': 1601590Srgrimes case 'i': 1611590Srgrimes case 'r': 1621590Srgrimes case 'n': 1631590Srgrimes case 'B': 1641590Srgrimes case 'D': 1651590Srgrimes break; 1661590Srgrimes default: 1671590Srgrimes /* 1681590Srgrimes * hpux has lot's of them, but we complain 1691590Srgrimes */ 17024542Sjmg warnx(erresc, *cp, cap); 1711590Srgrimes } 1721590Srgrimes 1731590Srgrimes /* And print them. */ 1741590Srgrimes switch (arg_need) { 1751590Srgrimes case 0: 1761590Srgrimes (void)tputs(str, 1, outc); 1771590Srgrimes break; 1781590Srgrimes case 1: 1791590Srgrimes arg_cols = 0; 1801590Srgrimes 1811590Srgrimes if (*++argv == NULL || *argv[0] == '\0') 1821590Srgrimes errx(2, errfew, 1, cap); 1831590Srgrimes arg_rows = atoi(*argv); 1841590Srgrimes 1851590Srgrimes (void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc); 1861590Srgrimes break; 1871590Srgrimes case 2: 1881590Srgrimes if (*++argv == NULL || *argv[0] == '\0') 1891590Srgrimes errx(2, errfew, 2, cap); 1901590Srgrimes arg_cols = atoi(*argv); 1911590Srgrimes 1921590Srgrimes if (*++argv == NULL || *argv[0] == '\0') 1931590Srgrimes errx(2, errfew, 2, cap); 1941590Srgrimes arg_rows = atoi(*argv); 1951590Srgrimes 1961590Srgrimes (void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc); 1971590Srgrimes break; 1981590Srgrimes 1991590Srgrimes default: 2001590Srgrimes errx(2, errmany, arg_need, cap); 2011590Srgrimes } 2021590Srgrimes return (argv); 2031590Srgrimes} 2041590Srgrimes 2051590Srgrimesstatic void 2061590Srgrimesusage() 2071590Srgrimes{ 2081590Srgrimes (void)fprintf(stderr, "usage: tput [-T term] attribute ...\n"); 2091590Srgrimes exit(1); 2101590Srgrimes} 211