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