Home | History | Annotate | Line # | Download | only in tput
tput.c revision 1.26
      1 /*	$NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy 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\
     35  The Regents of the University of California.  All rights reserved.");
     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.26 2013/02/05 11:31:56 roy Exp $");
     43 #endif /* not lint */
     44 
     45 #include <termios.h>
     46 
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <limits.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <term_private.h>
     54 #include <term.h>
     55 #include <unistd.h>
     56 
     57 static void   usage(void) __dead;
     58 static char **process(const char *, const char *, char **);
     59 
     60 int
     61 main(int argc, char **argv)
     62 {
     63 	int ch, exitval, n;
     64 	char *term;
     65 	const char *p, *s;
     66 	size_t pl;
     67 
     68 	term = NULL;
     69 	while ((ch = getopt(argc, argv, "T:")) != -1)
     70 		switch(ch) {
     71 		case 'T':
     72 			term = optarg;
     73 			break;
     74 		case '?':
     75 		default:
     76 			usage();
     77 		}
     78 	argc -= optind;
     79 	argv += optind;
     80 
     81 	if (!term && !(term = getenv("TERM")))
     82 		errx(2, "No terminal type specified and no TERM "
     83 		    "variable set in the environment.");
     84 	setupterm(term, 0, NULL);
     85 	for (exitval = 0; (p = *argv) != NULL; ++argv) {
     86 		switch (*p) {
     87 		case 'c':
     88 			if (!strcmp(p, "clear"))
     89 				p = "clear";
     90 			break;
     91 		case 'i':
     92 			if (!strcmp(p, "init")) {
     93 				s = tigetstr("is1");
     94 				if (s != NULL)
     95 					putp(s);
     96 				p = "is2";
     97 			}
     98 			break;
     99 		case 'l':
    100 			if (!strcmp(p, "longname")) {
    101 				(void)printf("%s\n", longname());
    102 				continue;
    103 			}
    104 			break;
    105 		case 'r':
    106 			if (!strcmp(p, "reset")) {
    107 				s = tigetstr("rs1");
    108 				if (s != NULL)
    109 					putp(s);
    110 				p = "rs2";
    111 			}
    112 			break;
    113 		}
    114 		pl = strlen(p);
    115 		if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
    116 		    (pl <= 2 && (s = tgetstr(p, NULL)) != NULL))
    117 			argv = process(p, s, argv);
    118 		else if ((((n = tigetnum(p)) != -1 && n != -2 ) ||
    119 			   (pl <= 2 && (n = tgetnum(p)) != -1)))
    120 			(void)printf("%d\n", n);
    121 		else {
    122 			exitval = tigetflag(p);
    123 			if (exitval == -1) {
    124 				if (pl <= 2)
    125 					exitval = !tgetflag(p);
    126 				else
    127 					exitval = 1;
    128 			} else
    129 				exitval = !exitval;
    130 		}
    131 
    132 		if (argv == NULL)
    133 			break;
    134 	}
    135 	return argv ? exitval : 2;
    136 }
    137 
    138 static char **
    139 process(const char *cap, const char *str, char **argv)
    140 {
    141 	static const char errfew[] =
    142 	    "Not enough arguments (%d) for capability `%s'";
    143 	static const char erresc[] =
    144 	    "Unknown %% escape (%s) for capability `%s'";
    145 	static const char errnum[] =
    146 	    "Expected a numeric argument [%d] (%s) for capability `%s'";
    147 	static const char errcharlong[] =
    148 	    "Platform does not fit a string into a long for capability '%s'";
    149 	int i, nparams, piss[TPARM_MAX];
    150 	long nums[TPARM_MAX];
    151 	char *strs[TPARM_MAX], *tmp;
    152 
    153 	/* Count how many values we need for this capability. */
    154 	errno = 0;
    155 	memset(&piss, 0, sizeof(piss));
    156 	nparams = _ti_parm_analyse(str, piss, TPARM_MAX);
    157 	if (errno == EINVAL)
    158 		errx(2, erresc, str, cap);
    159 
    160 	/* Create our arrays of integers and strings */
    161 	for (i = 0; i < nparams; i++) {
    162 		if (*++argv == NULL || *argv[0] == '\0')
    163 			errx(2, errfew, nparams, cap);
    164 		if (piss[i]) {
    165 			if (sizeof(char *) > sizeof(long) /* CONSTCOND */)
    166 				errx(2, errcharlong, cap);
    167 			strs[i] = *argv;
    168 		} else {
    169 			errno = 0;
    170 			nums[i] = strtol(*argv, &tmp, 0);
    171 			if ((errno == ERANGE &&
    172 			    (nums[i] == LONG_MIN || nums[i] == LONG_MAX)) ||
    173 			    (errno != 0 && nums[i] == 0) ||
    174 			    tmp == str ||
    175 			    *tmp != '\0')
    176 				errx(2, errnum, i + 1, *argv, cap);
    177 		}
    178 	}
    179 
    180 	/* And output */
    181 #define p(i)	(i <= nparams ? \
    182 		    (piss[i - 1] ? (long)strs[i - 1] : nums[i - 1]) : 0)
    183 	putp(tparm(str, p(1), p(2), p(3), p(4), p(5), p(6), p(7), p(8), p(9)));
    184 
    185 	return argv;
    186 }
    187 
    188 static void
    189 usage(void)
    190 {
    191 	(void)fprintf(stderr,
    192 	    "Usage: %s [-T term] attribute [attribute-args] ...\n",
    193 	    getprogname());
    194 	exit(2);
    195 }
    196