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