1 /* $NetBSD: tput.c,v 1.14 2000/10/11 14:46:20 is 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95"; 45 #endif 46 __RCSID("$NetBSD: tput.c,v 1.14 2000/10/11 14:46:20 is Exp $"); 47 #endif /* not lint */ 48 49 #include <termios.h> 50 51 #include <err.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <termcap.h> 56 #include <unistd.h> 57 58 int main __P((int, char **)); 59 static int outc __P((int)); 60 static void prlongname __P((char *)); 61 static void setospeed __P((void)); 62 static void usage __P((void)); 63 static char **process __P((char *, char *, char **)); 64 65 int 66 main(argc, argv) 67 int argc; 68 char **argv; 69 { 70 int ch, exitval, n; 71 char *cptr, *p, *term, buf[1024], tbuf[1024]; 72 73 term = NULL; 74 while ((ch = getopt(argc, argv, "T:")) != -1) 75 switch(ch) { 76 case 'T': 77 term = optarg; 78 break; 79 case '?': 80 default: 81 usage(); 82 } 83 argc -= optind; 84 argv += optind; 85 86 if (!term && !(term = getenv("TERM"))) 87 errx(2, "no terminal type specified and no TERM environmental variable."); 88 if (tgetent(tbuf, term) != 1) 89 err(2, "tgetent failure"); 90 setospeed(); 91 for (exitval = 0; (p = *argv) != NULL; ++argv) { 92 switch (*p) { 93 case 'c': 94 if (!strcmp(p, "clear")) 95 p = "cl"; 96 break; 97 case 'i': 98 if (!strcmp(p, "init")) 99 p = "is"; 100 break; 101 case 'l': 102 if (!strcmp(p, "longname")) { 103 prlongname(tbuf); 104 continue; 105 } 106 break; 107 case 'r': 108 if (!strcmp(p, "reset")) 109 p = "rs"; 110 break; 111 } 112 cptr = buf; 113 if (tgetstr(p, &cptr)) 114 argv = process(p, buf, argv); 115 else if ((n = tgetnum(p)) != -1) 116 (void)printf("%d\n", n); 117 else 118 exitval = !tgetflag(p); 119 120 if (argv == NULL) 121 break; 122 } 123 exit(argv ? exitval : 2); 124 } 125 126 static void 127 prlongname(buf) 128 char *buf; 129 { 130 int savech; 131 char *p, *savep; 132 133 for (p = buf; *p && *p != ':'; ++p) 134 continue; 135 savech = *(savep = p); 136 for (*p = '\0'; p >= buf && *p != '|'; --p) 137 continue; 138 (void)printf("%s\n", p + 1); 139 *savep = savech; 140 } 141 142 static char ** 143 process(cap, str, argv) 144 char *cap, *str, **argv; 145 { 146 static const char errfew[] = 147 "not enough arguments (%d) for capability `%s'"; 148 static const char errmany[] = 149 "too many arguments (%d) for capability `%s'"; 150 static const char erresc[] = 151 "unknown %% escape `%c' for capability `%s'"; 152 char *cp; 153 int arg_need, arg_rows, arg_cols; 154 155 /* Count how many values we need for this capability. */ 156 for (cp = str, arg_need = 0; *cp != '\0'; cp++) 157 if (*cp == '%') 158 switch (*++cp) { 159 case 'd': 160 case '2': 161 case '3': 162 case '.': 163 case '+': 164 arg_need++; 165 break; 166 case '%': 167 case '>': 168 case 'i': 169 case 'r': 170 case 'n': 171 case 'B': 172 case 'D': 173 break; 174 default: 175 /* 176 * hpux has lot's of them, but we complain 177 */ 178 errx(2, erresc, *cp, cap); 179 } 180 181 /* And print them. */ 182 switch (arg_need) { 183 case 0: 184 (void)tputs(str, 1, outc); 185 break; 186 case 1: 187 arg_cols = 0; 188 189 if (*++argv == NULL || *argv[0] == '\0') 190 errx(2, errfew, 1, cap); 191 arg_rows = atoi(*argv); 192 193 (void)tputs(tgoto(str, arg_cols, arg_rows), 1, outc); 194 break; 195 case 2: 196 if (*++argv == NULL || *argv[0] == '\0') 197 errx(2, errfew, 2, cap); 198 arg_rows = atoi(*argv); 199 200 if (*++argv == NULL || *argv[0] == '\0') 201 errx(2, errfew, 2, cap); 202 arg_cols = atoi(*argv); 203 204 (void) tputs(tgoto(str, arg_cols, arg_rows), arg_rows, outc); 205 break; 206 207 default: 208 errx(2, errmany, arg_need, cap); 209 } 210 return (argv); 211 } 212 213 static void 214 setospeed() 215 { 216 #undef ospeed 217 extern short ospeed; 218 struct termios t; 219 220 if (tcgetattr(STDOUT_FILENO, &t) != -1) 221 ospeed = 0; 222 else 223 ospeed = cfgetospeed(&t); 224 } 225 226 static int 227 outc(c) 228 int c; 229 { 230 return (putchar(c)); 231 } 232 233 static void 234 usage() 235 { 236 (void)fprintf(stderr, "usage: tput [-T term] attribute ...\n"); 237 exit(1); 238 } 239