1 /* $NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy Exp $ */
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)tput.c 8.3 (Berkeley) 4/28/95";
42 __RCSID("$NetBSD: tput.c,v 1.26 2013/02/05 11:31:56 roy Exp $");
53 #include <term_private.h>
57 static void usage(void) __dead
;
58 static char **process(const char *, const char *, char **);
61 main(int argc
, char **argv
)
69 while ((ch
= getopt(argc
, argv
, "T:")) != -1)
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
) {
88 if (!strcmp(p
, "clear"))
92 if (!strcmp(p
, "init")) {
100 if (!strcmp(p
, "longname")) {
101 (void)printf("%s\n", longname());
106 if (!strcmp(p
, "reset")) {
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
);
122 exitval
= tigetflag(p
);
125 exitval
= !tgetflag(p
);
135 return argv
? exitval
: 2;
139 process(const char *cap
, const char *str
, char **argv
)
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
;
153 /* Count how many values we need for this capability. */
155 memset(&piss
, 0, sizeof(piss
));
156 nparams
= _ti_parm_analyse(str
, piss
, TPARM_MAX
);
158 errx(2, erresc
, str
, cap
);
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
);
165 if (sizeof(char *) > sizeof(long) /* CONSTCOND */)
166 errx(2, errcharlong
, cap
);
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) ||
176 errx(2, errnum
, i
+ 1, *argv
, cap
);
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)));
191 (void)fprintf(stderr
,
192 "Usage: %s [-T term] attribute [attribute-args] ...\n",