Home | History | Annotate | Line # | Download | only in tput
tput.c revision 1.22
      1  1.22       roy /*	$NetBSD: tput.c,v 1.22 2011/10/04 12:23:14 roy Exp $	*/
      2   1.5       jtc 
      3   1.1       cgd /*-
      4   1.5       jtc  * Copyright (c) 1980, 1988, 1993
      5   1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.15       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.9     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.19     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
     35  1.19     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.5       jtc #if 0
     40   1.8       jtc static char sccsid[] = "@(#)tput.c	8.3 (Berkeley) 4/28/95";
     41   1.5       jtc #endif
     42  1.22       roy __RCSID("$NetBSD: tput.c,v 1.22 2011/10/04 12:23:14 roy Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.7       jtc #include <termios.h>
     46   1.5       jtc 
     47   1.5       jtc #include <err.h>
     48   1.1       cgd #include <stdio.h>
     49   1.5       jtc #include <stdlib.h>
     50  1.13      matt #include <string.h>
     51  1.20       roy #include <term.h>
     52   1.9     lukem #include <termcap.h>
     53   1.1       cgd #include <unistd.h>
     54   1.1       cgd 
     55  1.17  christos static int    outc(int);
     56  1.18     perry static void   usage(void) __dead;
     57  1.20       roy static char **process(const char *, const char *, char **);
     58   1.3       cgd 
     59   1.3       cgd int
     60  1.17  christos main(int argc, char **argv)
     61   1.1       cgd {
     62   1.3       cgd 	int ch, exitval, n;
     63  1.21       roy 	char *term;
     64  1.20       roy 	const char *p, *s;
     65  1.22       roy 	size_t pl;
     66   1.1       cgd 
     67   1.1       cgd 	term = NULL;
     68  1.10     lukem 	while ((ch = getopt(argc, argv, "T:")) != -1)
     69   1.1       cgd 		switch(ch) {
     70   1.1       cgd 		case 'T':
     71   1.1       cgd 			term = optarg;
     72   1.1       cgd 			break;
     73   1.1       cgd 		case '?':
     74   1.1       cgd 		default:
     75   1.1       cgd 			usage();
     76   1.1       cgd 		}
     77   1.1       cgd 	argc -= optind;
     78   1.1       cgd 	argv += optind;
     79   1.1       cgd 
     80   1.5       jtc 	if (!term && !(term = getenv("TERM")))
     81  1.17  christos 		errx(2, "No terminal type specified and no TERM "
     82  1.17  christos 		    "variable set in the environment.");
     83  1.20       roy 	setupterm(term, 0, NULL);
     84   1.3       cgd 	for (exitval = 0; (p = *argv) != NULL; ++argv) {
     85   1.5       jtc 		switch (*p) {
     86   1.1       cgd 		case 'c':
     87   1.1       cgd 			if (!strcmp(p, "clear"))
     88  1.20       roy 				p = "clear";
     89   1.1       cgd 			break;
     90   1.1       cgd 		case 'i':
     91  1.20       roy 			if (!strcmp(p, "init")) {
     92  1.20       roy 				s = tigetstr("is1");
     93  1.20       roy 				if (s != NULL)
     94  1.20       roy 					tputs(s, 0, outc);
     95  1.20       roy 				p = "is2";
     96  1.20       roy 			}
     97   1.1       cgd 			break;
     98   1.1       cgd 		case 'l':
     99   1.4       cgd 			if (!strcmp(p, "longname")) {
    100  1.21       roy 				(void)printf("%s\n", longname());
    101   1.4       cgd 				continue;
    102   1.4       cgd 			}
    103   1.4       cgd 			break;
    104   1.1       cgd 		case 'r':
    105  1.20       roy 			if (!strcmp(p, "reset")) {
    106  1.20       roy 				s = tigetstr("rs1");
    107  1.20       roy 				if (s != NULL)
    108  1.20       roy 					tputs(s, 0, outc);
    109  1.20       roy 				p = "rs2";
    110  1.20       roy 			}
    111   1.1       cgd 			break;
    112   1.1       cgd 		}
    113  1.22       roy 		pl = strlen(p);
    114  1.20       roy 		if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
    115  1.22       roy 		    (pl <= 2 && (s = tgetstr(p, NULL)) != NULL))
    116  1.20       roy 			argv = process(p, s, argv);
    117  1.22       roy 		else if ((((n = tigetnum(p)) != -1 && n != -2 ) ||
    118  1.22       roy 			   (pl <= 2 && (n = tgetnum(p)) != -1)))
    119   1.1       cgd 			(void)printf("%d\n", n);
    120  1.20       roy 		else {
    121  1.20       roy 			exitval = tigetflag(p);
    122  1.22       roy 			if (exitval == -1) {
    123  1.22       roy 				if (pl <= 2)
    124  1.22       roy 					exitval = !tgetflag(p);
    125  1.22       roy 				else
    126  1.22       roy 					exitval = 1;
    127  1.22       roy 			} else
    128  1.20       roy 				exitval = !exitval;
    129  1.20       roy 		}
    130   1.3       cgd 
    131   1.3       cgd 		if (argv == NULL)
    132   1.3       cgd 			break;
    133   1.1       cgd 	}
    134  1.17  christos 	return argv ? exitval : 2;
    135   1.1       cgd }
    136   1.1       cgd 
    137   1.3       cgd static char **
    138  1.20       roy process(const char *cap, const char *str, char **argv)
    139   1.3       cgd {
    140  1.14        is 	static const char errfew[] =
    141  1.17  christos 	    "Not enough arguments (%d) for capability `%s'";
    142  1.14        is 	static const char erresc[] =
    143  1.17  christos 	    "Unknown %% escape `%c' for capability `%s'";
    144  1.20       roy 	char c, l;
    145  1.20       roy 	const char *p;
    146  1.20       roy 	int arg_need, p1, p2, p3, p4, p5, p6, p7, p8, p9;
    147   1.5       jtc 
    148   1.5       jtc 	/* Count how many values we need for this capability. */
    149  1.20       roy 	arg_need = 0;
    150  1.20       roy 	p = str;
    151  1.20       roy 	while ((c = *p++) != '\0') {
    152  1.20       roy 		if (c != '%')
    153  1.20       roy 			continue;
    154  1.20       roy 		c = *p++;
    155  1.20       roy 		if (c == '\0')
    156  1.20       roy 			break;
    157  1.20       roy 		if (c != 'p')
    158  1.20       roy 			continue;
    159  1.20       roy 		c = *p++;
    160  1.20       roy 		if (c < '1' || c > '9')
    161  1.20       roy 			errx(2, erresc, c, cap);
    162  1.20       roy 		l = c - '0';
    163  1.20       roy 		if (l > arg_need)
    164  1.20       roy 			arg_need = l;
    165  1.20       roy 	}
    166  1.20       roy 
    167  1.20       roy #define NEXT_ARG							      \
    168  1.20       roy 	{								      \
    169  1.20       roy 		if (*++argv == NULL || *argv[0] == '\0')		      \
    170  1.20       roy 			errx(2, errfew, 1, cap);			      \
    171  1.20       roy 	}
    172  1.20       roy 
    173  1.20       roy 	if (arg_need > 0) {
    174  1.20       roy 		NEXT_ARG;
    175  1.20       roy 		p1 = atoi(*argv);
    176  1.20       roy 	} else
    177  1.20       roy 		p1 = 0;
    178  1.20       roy 	if (arg_need > 1) {
    179  1.20       roy 		NEXT_ARG;
    180  1.20       roy 		p2 = atoi(*argv);
    181  1.20       roy 	} else
    182  1.20       roy 		p2 = 0;
    183  1.20       roy 	if (arg_need > 2) {
    184  1.20       roy 		NEXT_ARG;
    185  1.20       roy 		p3 = atoi(*argv);
    186  1.20       roy 	} else
    187  1.20       roy 		p3 = 0;
    188  1.20       roy 	if (arg_need > 3) {
    189  1.20       roy 		NEXT_ARG;
    190  1.20       roy 		p4 = atoi(*argv);
    191  1.20       roy 	} else
    192  1.20       roy 		p4 = 0;
    193  1.20       roy 	if (arg_need > 4) {
    194  1.20       roy 		NEXT_ARG;
    195  1.20       roy 		p5 = atoi(*argv);
    196  1.20       roy 	} else
    197  1.20       roy 		p5 = 0;
    198  1.20       roy 	if (arg_need > 5) {
    199  1.20       roy 		NEXT_ARG;
    200  1.20       roy 		p6 = atoi(*argv);
    201  1.20       roy 	} else
    202  1.20       roy 		p6 = 0;
    203  1.20       roy 	if (arg_need > 6) {
    204  1.20       roy 		NEXT_ARG;
    205  1.20       roy 		p7 = atoi(*argv);
    206  1.20       roy 	} else
    207  1.20       roy 		p7 = 0;
    208  1.20       roy 	if (arg_need > 7) {
    209  1.20       roy 		NEXT_ARG;
    210  1.20       roy 		p8 = atoi(*argv);
    211  1.20       roy 	} else
    212  1.20       roy 		p8 = 0;
    213  1.20       roy 	if (arg_need > 8) {
    214  1.20       roy 		NEXT_ARG;
    215  1.20       roy 		p9 = atoi(*argv);
    216  1.20       roy 	} else
    217  1.20       roy 		p9 = 0;
    218   1.3       cgd 
    219   1.5       jtc 	/* And print them. */
    220  1.20       roy 	(void)tputs(tparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9), 0, outc);
    221  1.17  christos 	return argv;
    222   1.5       jtc }
    223   1.5       jtc 
    224  1.11     lukem static int
    225  1.20       roy outc(int c)
    226   1.5       jtc {
    227  1.17  christos 	return putchar(c);
    228   1.5       jtc }
    229   1.5       jtc 
    230   1.5       jtc static void
    231  1.17  christos usage(void)
    232   1.5       jtc {
    233  1.16       wiz 	(void)fprintf(stderr,
    234  1.17  christos 	    "Usage: %s [-T term] attribute [attribute-args] ...\n",
    235  1.16       wiz 	    getprogname());
    236  1.17  christos 	exit(2);
    237   1.1       cgd }
    238