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 * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 3087707Smarkm#include <sys/cdefs.h> 3187707Smarkm 3287707Smarkm__FBSDID("$FreeBSD$"); 3387707Smarkm 341590Srgrimes#ifndef lint 3587707Smarkmstatic const char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1980, 1988, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 3887707Smarkm#endif 391590Srgrimes 401590Srgrimes#ifndef lint 4187707Smarkmstatic const char sccsid[] = "@(#)tput.c 8.2 (Berkeley) 3/19/94"; 4265428Simp#endif 431590Srgrimes 44200462Sdelphij#include <termios.h> 45200462Sdelphij 461590Srgrimes#include <err.h> 475080Sache#include <termcap.h> 481590Srgrimes#include <stdio.h> 491590Srgrimes#include <stdlib.h> 5078718Sdd#include <string.h> 511590Srgrimes#include <unistd.h> 521590Srgrimes 535080Sache#undef putchar 543031Sdg#define outc putchar 553031Sdg 5692922Simpstatic void prlongname(char *); 5792922Simpstatic void usage(void); 5892922Simpstatic char **process(const char *, char *, char **); 591590Srgrimes 601590Srgrimesint 61102944Sdwmalonemain(int argc, char **argv) 621590Srgrimes{ 631590Srgrimes int ch, exitval, n; 6487707Smarkm char *cptr, *term, buf[1024], tbuf[1024]; 6587707Smarkm const char *p; 661590Srgrimes 671590Srgrimes term = NULL; 6824360Simp while ((ch = getopt(argc, argv, "T:")) != -1) 691590Srgrimes switch(ch) { 701590Srgrimes case 'T': 711590Srgrimes term = optarg; 721590Srgrimes break; 731590Srgrimes case '?': 741590Srgrimes default: 751590Srgrimes usage(); 761590Srgrimes } 771590Srgrimes argc -= optind; 781590Srgrimes argv += optind; 791590Srgrimes 80163283Sru if (argc < 1) 81163283Sru usage(); 82163283Sru 831590Srgrimes if (!term && !(term = getenv("TERM"))) 841590Srgrimeserrx(2, "no terminal type specified and no TERM environmental variable."); 851590Srgrimes if (tgetent(tbuf, term) != 1) 8698218Stjr err(3, "tgetent failure"); 871590Srgrimes for (exitval = 0; (p = *argv) != NULL; ++argv) { 881590Srgrimes switch (*p) { 891590Srgrimes case 'c': 901590Srgrimes if (!strcmp(p, "clear")) 911590Srgrimes p = "cl"; 921590Srgrimes break; 931590Srgrimes case 'i': 941590Srgrimes if (!strcmp(p, "init")) 951590Srgrimes p = "is"; 961590Srgrimes break; 971590Srgrimes case 'l': 985080Sache if (!strcmp(p, "longname")) { 991590Srgrimes prlongname(tbuf); 1005080Sache continue; 1015080Sache } 1025080Sache break; 1031590Srgrimes case 'r': 1041590Srgrimes if (!strcmp(p, "reset")) 1051590Srgrimes p = "rs"; 1061590Srgrimes break; 1071590Srgrimes } 1081590Srgrimes cptr = buf; 1091590Srgrimes if (tgetstr(p, &cptr)) 1101590Srgrimes argv = process(p, buf, argv); 1111590Srgrimes else if ((n = tgetnum(p)) != -1) 1121590Srgrimes (void)printf("%d\n", n); 1131590Srgrimes else 1141590Srgrimes exitval = !tgetflag(p); 1151590Srgrimes } 1161590Srgrimes exit(exitval); 1171590Srgrimes} 1181590Srgrimes 1191590Srgrimesstatic void 120102944Sdwmaloneprlongname(char *buf) 1211590Srgrimes{ 1221590Srgrimes int savech; 1231590Srgrimes char *p, *savep; 1241590Srgrimes 1251590Srgrimes for (p = buf; *p && *p != ':'; ++p); 1261590Srgrimes savech = *(savep = p); 1271590Srgrimes for (*p = '\0'; p >= buf && *p != '|'; --p); 1281590Srgrimes (void)printf("%s\n", p + 1); 1291590Srgrimes *savep = savech; 1301590Srgrimes} 1311590Srgrimes 1321590Srgrimesstatic char ** 133102944Sdwmaloneprocess(const char *cap, char *str, char **argv) 1341590Srgrimes{ 13590165Skris static const char errfew[] = 1361590Srgrimes "not enough arguments (%d) for capability `%s'"; 13790165Skris static const char errmany[] = 1381590Srgrimes "too many arguments (%d) for capability `%s'"; 13990165Skris static const char erresc[] = 1401590Srgrimes "unknown %% escape `%c' for capability `%s'"; 1411590Srgrimes char *cp; 1421590Srgrimes int arg_need, arg_rows, arg_cols; 1431590Srgrimes 1441590Srgrimes /* Count how many values we need for this capability. */ 1451590Srgrimes for (cp = str, arg_need = 0; *cp != '\0'; cp++) 1461590Srgrimes if (*cp == '%') 1471590Srgrimes switch (*++cp) { 1481590Srgrimes case 'd': 1491590Srgrimes case '2': 1501590Srgrimes case '3': 1511590Srgrimes case '.': 1521590Srgrimes case '+': 1531590Srgrimes arg_need++; 1541590Srgrimes break; 1551590Srgrimes case '%': 1561590Srgrimes case '>': 1571590Srgrimes case 'i': 1581590Srgrimes case 'r': 1591590Srgrimes case 'n': 1601590Srgrimes case 'B': 1611590Srgrimes case 'D': 1621590Srgrimes break; 16397763Sache case 'p': 16497763Sache if (cp[1]) { 16597763Sache cp++; 16697763Sache break; 16797763Sache } 1681590Srgrimes default: 1691590Srgrimes /* 1701590Srgrimes * hpux has lot's of them, but we complain 1711590Srgrimes */ 17224542Sjmg warnx(erresc, *cp, cap); 1731590Srgrimes } 1741590Srgrimes 1751590Srgrimes /* And print them. */ 1761590Srgrimes switch (arg_need) { 1771590Srgrimes case 0: 1781590Srgrimes (void)tputs(str, 1, outc); 1791590Srgrimes break; 1801590Srgrimes case 1: 1811590Srgrimes arg_cols = 0; 1821590Srgrimes 1831590Srgrimes if (*++argv == NULL || *argv[0] == '\0') 1841590Srgrimes errx(2, errfew, 1, cap); 1851590Srgrimes arg_rows = atoi(*argv); 1861590Srgrimes 1871590Srgrimes (void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc); 1881590Srgrimes break; 1891590Srgrimes case 2: 1901590Srgrimes if (*++argv == NULL || *argv[0] == '\0') 1911590Srgrimes errx(2, errfew, 2, cap); 1921590Srgrimes arg_cols = atoi(*argv); 1931590Srgrimes 1941590Srgrimes if (*++argv == NULL || *argv[0] == '\0') 1951590Srgrimes errx(2, errfew, 2, cap); 1961590Srgrimes arg_rows = atoi(*argv); 1971590Srgrimes 1981590Srgrimes (void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc); 1991590Srgrimes break; 2001590Srgrimes 2011590Srgrimes default: 2021590Srgrimes errx(2, errmany, arg_need, cap); 2031590Srgrimes } 2041590Srgrimes return (argv); 2051590Srgrimes} 2061590Srgrimes 2071590Srgrimesstatic void 208102944Sdwmaloneusage(void) 2091590Srgrimes{ 2101590Srgrimes (void)fprintf(stderr, "usage: tput [-T term] attribute ...\n"); 21198218Stjr exit(2); 2121590Srgrimes} 213