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