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