Home | History | Annotate | Line # | Download | only in tput
tput.c revision 1.21
      1  1.21       roy /*	$NetBSD: tput.c,v 1.21 2011/10/04 11:02:32 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.21       roy __RCSID("$NetBSD: tput.c,v 1.21 2011/10/04 11:02:32 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.1       cgd 
     66   1.1       cgd 	term = NULL;
     67  1.10     lukem 	while ((ch = getopt(argc, argv, "T:")) != -1)
     68   1.1       cgd 		switch(ch) {
     69   1.1       cgd 		case 'T':
     70   1.1       cgd 			term = optarg;
     71   1.1       cgd 			break;
     72   1.1       cgd 		case '?':
     73   1.1       cgd 		default:
     74   1.1       cgd 			usage();
     75   1.1       cgd 		}
     76   1.1       cgd 	argc -= optind;
     77   1.1       cgd 	argv += optind;
     78   1.1       cgd 
     79   1.5       jtc 	if (!term && !(term = getenv("TERM")))
     80  1.17  christos 		errx(2, "No terminal type specified and no TERM "
     81  1.17  christos 		    "variable set in the environment.");
     82  1.20       roy 	setupterm(term, 0, NULL);
     83   1.3       cgd 	for (exitval = 0; (p = *argv) != NULL; ++argv) {
     84   1.5       jtc 		switch (*p) {
     85   1.1       cgd 		case 'c':
     86   1.1       cgd 			if (!strcmp(p, "clear"))
     87  1.20       roy 				p = "clear";
     88   1.1       cgd 			break;
     89   1.1       cgd 		case 'i':
     90  1.20       roy 			if (!strcmp(p, "init")) {
     91  1.20       roy 				s = tigetstr("is1");
     92  1.20       roy 				if (s != NULL)
     93  1.20       roy 					tputs(s, 0, outc);
     94  1.20       roy 				p = "is2";
     95  1.20       roy 			}
     96   1.1       cgd 			break;
     97   1.1       cgd 		case 'l':
     98   1.4       cgd 			if (!strcmp(p, "longname")) {
     99  1.21       roy 				(void)printf("%s\n", longname());
    100   1.4       cgd 				continue;
    101   1.4       cgd 			}
    102   1.4       cgd 			break;
    103   1.1       cgd 		case 'r':
    104  1.20       roy 			if (!strcmp(p, "reset")) {
    105  1.20       roy 				s = tigetstr("rs1");
    106  1.20       roy 				if (s != NULL)
    107  1.20       roy 					tputs(s, 0, outc);
    108  1.20       roy 				p = "rs2";
    109  1.20       roy 			}
    110   1.1       cgd 			break;
    111   1.1       cgd 		}
    112  1.20       roy 		if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
    113  1.20       roy 		    ((s = tgetstr(p, NULL)) != NULL))
    114  1.20       roy 			argv = process(p, s, argv);
    115  1.20       roy 		else if ((((n = tigetnum(p)) != -1 && n != -2 )||
    116  1.20       roy 			   (n = tgetnum(p)) != -1))
    117   1.1       cgd 			(void)printf("%d\n", n);
    118  1.20       roy 		else {
    119  1.20       roy 			exitval = tigetflag(p);
    120  1.20       roy 			if (exitval == -1)
    121  1.20       roy 				exitval = !tgetflag(p);
    122  1.20       roy 			else
    123  1.20       roy 				exitval = !exitval;
    124  1.20       roy 		}
    125   1.3       cgd 
    126   1.3       cgd 		if (argv == NULL)
    127   1.3       cgd 			break;
    128   1.1       cgd 	}
    129  1.17  christos 	return argv ? exitval : 2;
    130   1.1       cgd }
    131   1.1       cgd 
    132   1.3       cgd static char **
    133  1.20       roy process(const char *cap, const char *str, char **argv)
    134   1.3       cgd {
    135  1.14        is 	static const char errfew[] =
    136  1.17  christos 	    "Not enough arguments (%d) for capability `%s'";
    137  1.14        is 	static const char erresc[] =
    138  1.17  christos 	    "Unknown %% escape `%c' for capability `%s'";
    139  1.20       roy 	char c, l;
    140  1.20       roy 	const char *p;
    141  1.20       roy 	int arg_need, p1, p2, p3, p4, p5, p6, p7, p8, p9;
    142   1.5       jtc 
    143   1.5       jtc 	/* Count how many values we need for this capability. */
    144  1.20       roy 	arg_need = 0;
    145  1.20       roy 	p = str;
    146  1.20       roy 	while ((c = *p++) != '\0') {
    147  1.20       roy 		if (c != '%')
    148  1.20       roy 			continue;
    149  1.20       roy 		c = *p++;
    150  1.20       roy 		if (c == '\0')
    151  1.20       roy 			break;
    152  1.20       roy 		if (c != 'p')
    153  1.20       roy 			continue;
    154  1.20       roy 		c = *p++;
    155  1.20       roy 		if (c < '1' || c > '9')
    156  1.20       roy 			errx(2, erresc, c, cap);
    157  1.20       roy 		l = c - '0';
    158  1.20       roy 		if (l > arg_need)
    159  1.20       roy 			arg_need = l;
    160  1.20       roy 	}
    161  1.20       roy 
    162  1.20       roy #define NEXT_ARG							      \
    163  1.20       roy 	{								      \
    164  1.20       roy 		if (*++argv == NULL || *argv[0] == '\0')		      \
    165  1.20       roy 			errx(2, errfew, 1, cap);			      \
    166  1.20       roy 	}
    167  1.20       roy 
    168  1.20       roy 	if (arg_need > 0) {
    169  1.20       roy 		NEXT_ARG;
    170  1.20       roy 		p1 = atoi(*argv);
    171  1.20       roy 	} else
    172  1.20       roy 		p1 = 0;
    173  1.20       roy 	if (arg_need > 1) {
    174  1.20       roy 		NEXT_ARG;
    175  1.20       roy 		p2 = atoi(*argv);
    176  1.20       roy 	} else
    177  1.20       roy 		p2 = 0;
    178  1.20       roy 	if (arg_need > 2) {
    179  1.20       roy 		NEXT_ARG;
    180  1.20       roy 		p3 = atoi(*argv);
    181  1.20       roy 	} else
    182  1.20       roy 		p3 = 0;
    183  1.20       roy 	if (arg_need > 3) {
    184  1.20       roy 		NEXT_ARG;
    185  1.20       roy 		p4 = atoi(*argv);
    186  1.20       roy 	} else
    187  1.20       roy 		p4 = 0;
    188  1.20       roy 	if (arg_need > 4) {
    189  1.20       roy 		NEXT_ARG;
    190  1.20       roy 		p5 = atoi(*argv);
    191  1.20       roy 	} else
    192  1.20       roy 		p5 = 0;
    193  1.20       roy 	if (arg_need > 5) {
    194  1.20       roy 		NEXT_ARG;
    195  1.20       roy 		p6 = atoi(*argv);
    196  1.20       roy 	} else
    197  1.20       roy 		p6 = 0;
    198  1.20       roy 	if (arg_need > 6) {
    199  1.20       roy 		NEXT_ARG;
    200  1.20       roy 		p7 = atoi(*argv);
    201  1.20       roy 	} else
    202  1.20       roy 		p7 = 0;
    203  1.20       roy 	if (arg_need > 7) {
    204  1.20       roy 		NEXT_ARG;
    205  1.20       roy 		p8 = atoi(*argv);
    206  1.20       roy 	} else
    207  1.20       roy 		p8 = 0;
    208  1.20       roy 	if (arg_need > 8) {
    209  1.20       roy 		NEXT_ARG;
    210  1.20       roy 		p9 = atoi(*argv);
    211  1.20       roy 	} else
    212  1.20       roy 		p9 = 0;
    213   1.3       cgd 
    214   1.5       jtc 	/* And print them. */
    215  1.20       roy 	(void)tputs(tparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9), 0, outc);
    216  1.17  christos 	return argv;
    217   1.5       jtc }
    218   1.5       jtc 
    219  1.11     lukem static int
    220  1.20       roy outc(int c)
    221   1.5       jtc {
    222  1.17  christos 	return putchar(c);
    223   1.5       jtc }
    224   1.5       jtc 
    225   1.5       jtc static void
    226  1.17  christos usage(void)
    227   1.5       jtc {
    228  1.16       wiz 	(void)fprintf(stderr,
    229  1.17  christos 	    "Usage: %s [-T term] attribute [attribute-args] ...\n",
    230  1.16       wiz 	    getprogname());
    231  1.17  christos 	exit(2);
    232   1.1       cgd }
    233