1*84d9c625SLionel Sambuc /* $NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy Exp $ */
21999c518SAntoine Leca
31999c518SAntoine Leca /*-
41999c518SAntoine Leca * Copyright (c) 1980, 1988, 1993
51999c518SAntoine Leca * The Regents of the University of California. All rights reserved.
61999c518SAntoine Leca *
71999c518SAntoine Leca * Redistribution and use in source and binary forms, with or without
81999c518SAntoine Leca * modification, are permitted provided that the following conditions
91999c518SAntoine Leca * are met:
101999c518SAntoine Leca * 1. Redistributions of source code must retain the above copyright
111999c518SAntoine Leca * notice, this list of conditions and the following disclaimer.
121999c518SAntoine Leca * 2. Redistributions in binary form must reproduce the above copyright
131999c518SAntoine Leca * notice, this list of conditions and the following disclaimer in the
141999c518SAntoine Leca * documentation and/or other materials provided with the distribution.
151999c518SAntoine Leca * 3. Neither the name of the University nor the names of its contributors
161999c518SAntoine Leca * may be used to endorse or promote products derived from this software
171999c518SAntoine Leca * without specific prior written permission.
181999c518SAntoine Leca *
191999c518SAntoine Leca * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201999c518SAntoine Leca * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211999c518SAntoine Leca * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221999c518SAntoine Leca * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231999c518SAntoine Leca * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241999c518SAntoine Leca * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251999c518SAntoine Leca * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261999c518SAntoine Leca * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271999c518SAntoine Leca * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281999c518SAntoine Leca * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291999c518SAntoine Leca * SUCH DAMAGE.
301999c518SAntoine Leca */
311999c518SAntoine Leca
321999c518SAntoine Leca #include <sys/cdefs.h>
331999c518SAntoine Leca #ifndef lint
341999c518SAntoine Leca __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
351999c518SAntoine Leca The Regents of the University of California. All rights reserved.");
361999c518SAntoine Leca #endif /* not lint */
371999c518SAntoine Leca
381999c518SAntoine Leca #ifndef lint
391999c518SAntoine Leca #if 0
401999c518SAntoine Leca static char sccsid[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95";
411999c518SAntoine Leca #endif
42*84d9c625SLionel Sambuc __RCSID("$NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy Exp $");
431999c518SAntoine Leca #endif /* not lint */
441999c518SAntoine Leca
451999c518SAntoine Leca #include <termios.h>
461999c518SAntoine Leca
471999c518SAntoine Leca #include <err.h>
48*84d9c625SLionel Sambuc #include <errno.h>
49*84d9c625SLionel Sambuc #include <limits.h>
501999c518SAntoine Leca #include <stdio.h>
511999c518SAntoine Leca #include <stdlib.h>
521999c518SAntoine Leca #include <string.h>
53*84d9c625SLionel Sambuc #include <term_private.h>
541999c518SAntoine Leca #include <term.h>
551999c518SAntoine Leca #include <unistd.h>
561999c518SAntoine Leca
571999c518SAntoine Leca static void usage(void) __dead;
581999c518SAntoine Leca static char **process(const char *, const char *, char **);
591999c518SAntoine Leca
601999c518SAntoine Leca int
main(int argc,char ** argv)611999c518SAntoine Leca main(int argc, char **argv)
621999c518SAntoine Leca {
631999c518SAntoine Leca int ch, exitval, n;
641999c518SAntoine Leca char *term;
651999c518SAntoine Leca const char *p, *s;
661999c518SAntoine Leca size_t pl;
671999c518SAntoine Leca
681999c518SAntoine Leca term = NULL;
691999c518SAntoine Leca while ((ch = getopt(argc, argv, "T:")) != -1)
701999c518SAntoine Leca switch(ch) {
711999c518SAntoine Leca case 'T':
721999c518SAntoine Leca term = optarg;
731999c518SAntoine Leca break;
741999c518SAntoine Leca case '?':
751999c518SAntoine Leca default:
761999c518SAntoine Leca usage();
771999c518SAntoine Leca }
781999c518SAntoine Leca argc -= optind;
791999c518SAntoine Leca argv += optind;
801999c518SAntoine Leca
811999c518SAntoine Leca if (!term && !(term = getenv("TERM")))
821999c518SAntoine Leca errx(2, "No terminal type specified and no TERM "
831999c518SAntoine Leca "variable set in the environment.");
841999c518SAntoine Leca setupterm(term, 0, NULL);
851999c518SAntoine Leca for (exitval = 0; (p = *argv) != NULL; ++argv) {
861999c518SAntoine Leca switch (*p) {
871999c518SAntoine Leca case 'c':
881999c518SAntoine Leca if (!strcmp(p, "clear"))
891999c518SAntoine Leca p = "clear";
901999c518SAntoine Leca break;
911999c518SAntoine Leca case 'i':
921999c518SAntoine Leca if (!strcmp(p, "init")) {
931999c518SAntoine Leca s = tigetstr("is1");
941999c518SAntoine Leca if (s != NULL)
95*84d9c625SLionel Sambuc putp(s);
961999c518SAntoine Leca p = "is2";
971999c518SAntoine Leca }
981999c518SAntoine Leca break;
991999c518SAntoine Leca case 'l':
1001999c518SAntoine Leca if (!strcmp(p, "longname")) {
1011999c518SAntoine Leca (void)printf("%s\n", longname());
1021999c518SAntoine Leca continue;
1031999c518SAntoine Leca }
1041999c518SAntoine Leca break;
1051999c518SAntoine Leca case 'r':
1061999c518SAntoine Leca if (!strcmp(p, "reset")) {
1071999c518SAntoine Leca s = tigetstr("rs1");
1081999c518SAntoine Leca if (s != NULL)
109*84d9c625SLionel Sambuc putp(s);
1101999c518SAntoine Leca p = "rs2";
1111999c518SAntoine Leca }
1121999c518SAntoine Leca break;
1131999c518SAntoine Leca }
1141999c518SAntoine Leca pl = strlen(p);
1151999c518SAntoine Leca if (((s = tigetstr(p)) != NULL && s != (char *)-1) ||
1161999c518SAntoine Leca (pl <= 2 && (s = tgetstr(p, NULL)) != NULL))
1171999c518SAntoine Leca argv = process(p, s, argv);
1181999c518SAntoine Leca else if ((((n = tigetnum(p)) != -1 && n != -2 ) ||
1191999c518SAntoine Leca (pl <= 2 && (n = tgetnum(p)) != -1)))
1201999c518SAntoine Leca (void)printf("%d\n", n);
1211999c518SAntoine Leca else {
1221999c518SAntoine Leca exitval = tigetflag(p);
1231999c518SAntoine Leca if (exitval == -1) {
1241999c518SAntoine Leca if (pl <= 2)
1251999c518SAntoine Leca exitval = !tgetflag(p);
1261999c518SAntoine Leca else
1271999c518SAntoine Leca exitval = 1;
1281999c518SAntoine Leca } else
1291999c518SAntoine Leca exitval = !exitval;
1301999c518SAntoine Leca }
1311999c518SAntoine Leca
1321999c518SAntoine Leca if (argv == NULL)
1331999c518SAntoine Leca break;
1341999c518SAntoine Leca }
1351999c518SAntoine Leca return argv ? exitval : 2;
1361999c518SAntoine Leca }
1371999c518SAntoine Leca
1381999c518SAntoine Leca static char **
process(const char * cap,const char * str,char ** argv)1391999c518SAntoine Leca process(const char *cap, const char *str, char **argv)
1401999c518SAntoine Leca {
1411999c518SAntoine Leca static const char errfew[] =
1421999c518SAntoine Leca "Not enough arguments (%d) for capability `%s'";
1431999c518SAntoine Leca static const char erresc[] =
144*84d9c625SLionel Sambuc "Unknown %% escape (%s) for capability `%s'";
145*84d9c625SLionel Sambuc static const char errnum[] =
146*84d9c625SLionel Sambuc "Expected a numeric argument [%d] (%s) for capability `%s'";
147*84d9c625SLionel Sambuc static const char errcharlong[] =
148*84d9c625SLionel Sambuc "Platform does not fit a string into a long for capability '%s'";
149*84d9c625SLionel Sambuc int i, nparams, piss[TPARM_MAX];
150*84d9c625SLionel Sambuc long nums[TPARM_MAX];
151*84d9c625SLionel Sambuc char *strs[TPARM_MAX], *tmp;
1521999c518SAntoine Leca
1531999c518SAntoine Leca /* Count how many values we need for this capability. */
154*84d9c625SLionel Sambuc errno = 0;
155*84d9c625SLionel Sambuc memset(&piss, 0, sizeof(piss));
156*84d9c625SLionel Sambuc nparams = _ti_parm_analyse(str, piss, TPARM_MAX);
157*84d9c625SLionel Sambuc if (errno == EINVAL)
158*84d9c625SLionel Sambuc errx(2, erresc, str, cap);
159*84d9c625SLionel Sambuc
160*84d9c625SLionel Sambuc /* Create our arrays of integers and strings */
161*84d9c625SLionel Sambuc for (i = 0; i < nparams; i++) {
162*84d9c625SLionel Sambuc if (*++argv == NULL || *argv[0] == '\0')
163*84d9c625SLionel Sambuc errx(2, errfew, nparams, cap);
164*84d9c625SLionel Sambuc if (piss[i]) {
165*84d9c625SLionel Sambuc if (sizeof(char *) > sizeof(long) /* CONSTCOND */)
166*84d9c625SLionel Sambuc errx(2, errcharlong, cap);
167*84d9c625SLionel Sambuc strs[i] = *argv;
168*84d9c625SLionel Sambuc } else {
169*84d9c625SLionel Sambuc errno = 0;
170*84d9c625SLionel Sambuc nums[i] = strtol(*argv, &tmp, 0);
171*84d9c625SLionel Sambuc if ((errno == ERANGE &&
172*84d9c625SLionel Sambuc (nums[i] == LONG_MIN || nums[i] == LONG_MAX)) ||
173*84d9c625SLionel Sambuc (errno != 0 && nums[i] == 0) ||
174*84d9c625SLionel Sambuc tmp == str ||
175*84d9c625SLionel Sambuc *tmp != '\0')
176*84d9c625SLionel Sambuc errx(2, errnum, i + 1, *argv, cap);
177*84d9c625SLionel Sambuc }
1781999c518SAntoine Leca }
1791999c518SAntoine Leca
180*84d9c625SLionel Sambuc /* And output */
181*84d9c625SLionel Sambuc #define p(i) (i <= nparams ? \
182*84d9c625SLionel Sambuc (piss[i - 1] ? (long)strs[i - 1] : nums[i - 1]) : 0)
183*84d9c625SLionel Sambuc putp(tparm(str, p(1), p(2), p(3), p(4), p(5), p(6), p(7), p(8), p(9)));
1841999c518SAntoine Leca
1851999c518SAntoine Leca return argv;
1861999c518SAntoine Leca }
1871999c518SAntoine Leca
1881999c518SAntoine Leca static void
usage(void)1891999c518SAntoine Leca usage(void)
1901999c518SAntoine Leca {
1911999c518SAntoine Leca (void)fprintf(stderr,
1921999c518SAntoine Leca "Usage: %s [-T term] attribute [attribute-args] ...\n",
1931999c518SAntoine Leca getprogname());
1941999c518SAntoine Leca exit(2);
1951999c518SAntoine Leca }
196