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