1*a6aba8c9Sroy /* $NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy Exp $ */
2fce6632bSjtc
361f28255Scgd /*-
4fce6632bSjtc * Copyright (c) 1980, 1988, 1993
5fce6632bSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32a04cd750Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
3598e5374cSlukem The Regents of the University of California. All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd
3861f28255Scgd #ifndef lint
39fce6632bSjtc #if 0
40f1df59adSjtc static char sccsid[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95";
41fce6632bSjtc #endif
42*a6aba8c9Sroy __RCSID("$NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd
451847eaedSjtc #include <termios.h>
46fce6632bSjtc
47fce6632bSjtc #include <err.h>
48a17592c0Sroy #include <errno.h>
49a17592c0Sroy #include <limits.h>
50fce6632bSjtc #include <stdio.h>
51fce6632bSjtc #include <stdlib.h>
52fcd0fb11Smatt #include <string.h>
53a17592c0Sroy #include <term_private.h>
5498eb8895Sroy #include <term.h>
55fce6632bSjtc #include <unistd.h>
5661f28255Scgd
578b0f9554Sperry static void usage(void) __dead;
5898eb8895Sroy static char **process(const char *, const char *, char **);
5903cf464dScgd
6003cf464dScgd int
main(int argc,char ** argv)61e8323719Schristos main(int argc, char **argv)
6261f28255Scgd {
6303cf464dScgd int ch, exitval, n;
649f4db5a1Sroy char *term;
6598eb8895Sroy const char *p, *s;
6626bd22acSroy size_t pl;
6761f28255Scgd
6861f28255Scgd term = NULL;
692486fca7Slukem while ((ch = getopt(argc, argv, "T:")) != -1)
7061f28255Scgd switch(ch) {
7161f28255Scgd case 'T':
7261f28255Scgd term = optarg;
7361f28255Scgd break;
7461f28255Scgd case '?':
7561f28255Scgd default:
7661f28255Scgd usage();
7761f28255Scgd }
7861f28255Scgd argc -= optind;
7961f28255Scgd argv += optind;
8061f28255Scgd
81fce6632bSjtc if (!term && !(term = getenv("TERM")))
82e8323719Schristos errx(2, "No terminal type specified and no TERM "
83e8323719Schristos "variable set in the environment.");
8498eb8895Sroy setupterm(term, 0, NULL);
8503cf464dScgd for (exitval = 0; (p = *argv) != NULL; ++argv) {
8661f28255Scgd switch (*p) {
8761f28255Scgd case 'c':
8861f28255Scgd if (!strcmp(p, "clear"))
8998eb8895Sroy p = "clear";
9061f28255Scgd break;
9161f28255Scgd case 'i':
9298eb8895Sroy if (!strcmp(p, "init")) {
9398eb8895Sroy s = tigetstr("is1");
9498eb8895Sroy if (s != NULL)
95*a6aba8c9Sroy putp(s);
9698eb8895Sroy p = "is2";
9798eb8895Sroy }
9861f28255Scgd break;
9961f28255Scgd case 'l':
100996d1050Scgd if (!strcmp(p, "longname")) {
1019f4db5a1Sroy (void)printf("%s\n", longname());
10261f28255Scgd continue;
103996d1050Scgd }
104996d1050Scgd break;
10561f28255Scgd case 'r':
10698eb8895Sroy if (!strcmp(p, "reset")) {
10798eb8895Sroy s = tigetstr("rs1");
10898eb8895Sroy if (s != NULL)
109*a6aba8c9Sroy putp(s);
11098eb8895Sroy p = "rs2";
11198eb8895Sroy }
11261f28255Scgd break;
11361f28255Scgd }
11426bd22acSroy pl = strlen(p);
11598eb8895Sroy if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
11626bd22acSroy (pl <= 2 && (s = tgetstr(p, NULL)) != NULL))
11798eb8895Sroy argv = process(p, s, argv);
11898eb8895Sroy else if ((((n = tigetnum(p)) != -1 && n != -2 ) ||
11926bd22acSroy (pl <= 2 && (n = tgetnum(p)) != -1)))
12061f28255Scgd (void)printf("%d\n", n);
12198eb8895Sroy else {
12298eb8895Sroy exitval = tigetflag(p);
12326bd22acSroy if (exitval == -1) {
12426bd22acSroy if (pl <= 2)
12561f28255Scgd exitval = !tgetflag(p);
12698eb8895Sroy else
12726bd22acSroy exitval = 1;
12826bd22acSroy } else
12998eb8895Sroy exitval = !exitval;
13098eb8895Sroy }
13103cf464dScgd
13203cf464dScgd if (argv == NULL)
13303cf464dScgd break;
13461f28255Scgd }
135e8323719Schristos return argv ? exitval : 2;
13661f28255Scgd }
13761f28255Scgd
138fce6632bSjtc static char **
process(const char * cap,const char * str,char ** argv)13998eb8895Sroy process(const char *cap, const char *str, char **argv)
140fce6632bSjtc {
141135600f9Sis static const char errfew[] =
142e8323719Schristos "Not enough arguments (%d) for capability `%s'";
143135600f9Sis static const char erresc[] =
144a17592c0Sroy "Unknown %% escape (%s) for capability `%s'";
145a17592c0Sroy static const char errnum[] =
146a17592c0Sroy "Expected a numeric argument [%d] (%s) for capability `%s'";
1472429e4f2Sroy static const char errcharlong[] =
1482429e4f2Sroy "Platform does not fit a string into a long for capability '%s'";
149a17592c0Sroy int i, nparams, piss[TPARM_MAX];
150a17592c0Sroy long nums[TPARM_MAX];
151a17592c0Sroy char *strs[TPARM_MAX], *tmp;
152fce6632bSjtc
153fce6632bSjtc /* Count how many values we need for this capability. */
154a17592c0Sroy errno = 0;
155a17592c0Sroy memset(&piss, 0, sizeof(piss));
156a17592c0Sroy nparams = _ti_parm_analyse(str, piss, TPARM_MAX);
157a17592c0Sroy if (errno == EINVAL)
158a17592c0Sroy errx(2, erresc, str, cap);
159a17592c0Sroy
160a17592c0Sroy /* Create our arrays of integers and strings */
161a17592c0Sroy for (i = 0; i < nparams; i++) {
162a17592c0Sroy if (*++argv == NULL || *argv[0] == '\0')
163a17592c0Sroy errx(2, errfew, nparams, cap);
1642429e4f2Sroy if (piss[i]) {
1652429e4f2Sroy if (sizeof(char *) > sizeof(long) /* CONSTCOND */)
1662429e4f2Sroy errx(2, errcharlong, cap);
167a17592c0Sroy strs[i] = *argv;
1682429e4f2Sroy } else {
169a17592c0Sroy errno = 0;
170a17592c0Sroy nums[i] = strtol(*argv, &tmp, 0);
171a17592c0Sroy if ((errno == ERANGE &&
172a17592c0Sroy (nums[i] == LONG_MIN || nums[i] == LONG_MAX)) ||
173a17592c0Sroy (errno != 0 && nums[i] == 0) ||
174a17592c0Sroy tmp == str ||
175a17592c0Sroy *tmp != '\0')
176a17592c0Sroy errx(2, errnum, i + 1, *argv, cap);
177a17592c0Sroy }
178fce6632bSjtc }
179fce6632bSjtc
180a17592c0Sroy /* And output */
181a17592c0Sroy #define p(i) (i <= nparams ? \
182a17592c0Sroy (piss[i - 1] ? (long)strs[i - 1] : nums[i - 1]) : 0)
183*a6aba8c9Sroy putp(tparm(str, p(1), p(2), p(3), p(4), p(5), p(6), p(7), p(8), p(9)));
18498eb8895Sroy
185e8323719Schristos return argv;
186fce6632bSjtc }
187fce6632bSjtc
18803cf464dScgd static void
usage(void)189e8323719Schristos usage(void)
19061f28255Scgd {
1913ce5fd49Swiz (void)fprintf(stderr,
192e8323719Schristos "Usage: %s [-T term] attribute [attribute-args] ...\n",
1933ce5fd49Swiz getprogname());
194e8323719Schristos exit(2);
19561f28255Scgd }
196