1 /****************************************************************************
2 * Copyright (c) 1998-2015,2016 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * tput.c -- shellscript access to terminal capabilities
38 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
39 * Ross Ridge's mytinfo package.
42 #include <tparm_type.h>
43 #include <reset_cmd.h>
46 #include <dump_entry.h>
49 #include <transform.h>
51 MODULE_ID("$Id: tput.c,v 1.56 2016/08/06 20:59:26 tom Exp $")
53 #define PUTS(s) fputs(s, stdout)
55 const char *_nc_progname = "tput";
57 static char *prg_name;
58 static bool is_init = FALSE;
59 static bool is_reset = FALSE;
62 quit(int status, const char *fmt,...)
67 fprintf(stderr, "%s: ", prg_name);
68 vfprintf(stderr, fmt, argp);
69 fprintf(stderr, "\n");
77 fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
78 ExitProgram(EXIT_FAILURE);
82 check_aliases(const char *name, bool program)
84 is_init = same_program(name, program ? PROG_INIT : "init");
85 is_reset = same_program(name, program ? PROG_RESET : "reset");
89 exit_code(int token, int value)
95 result = !value; /* TRUE=0, FALSE=1 */
98 result = 0; /* always zero */
101 result = value; /* 0=normal, 1=missing */
108 tput_cmd(int argc, char *argv[])
110 NCURSES_CONST char *name;
114 bool termcap = FALSE;
117 if ((name = argv[0]) == 0)
119 check_aliases(name, FALSE);
120 if (is_reset || is_init) {
121 reset_start(stdout, is_reset, is_init);
122 if (send_init_strings((TTY *) 0)) {
128 if (strcmp(name, "longname") == 0) {
135 if ((status = tigetflag(name)) != -1) {
136 return exit_code(BOOLEAN, status);
137 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
138 (void) printf("%d\n", status);
139 return exit_code(NUMBER, 0);
140 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
143 const struct name_table_entry *np;
146 if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
147 switch (np->nte_type) {
149 if (bool_from_termcap[np->nte_index])
150 name = boolnames[np->nte_index];
154 if (num_from_termcap[np->nte_index])
155 name = numnames[np->nte_index];
159 if (str_from_termcap[np->nte_index])
160 name = strnames[np->nte_index];
167 quit(4, "unknown terminfo capability '%s'", name);
168 } else if (s != ABSENT_STRING) {
172 long numbers[1 + NUM_PARM];
173 char *strings[1 + NUM_PARM];
174 char *p_is_s[NUM_PARM];
176 /* Nasty hack time. The tparm function needs to see numeric
177 * parameters as numbers, not as pointers to their string
181 for (k = 1; k < argc; k++) {
183 strings[k] = argv[k];
184 numbers[k] = strtol(argv[k], &tmp, 0);
185 if (tmp == 0 || *tmp != 0)
188 for (k = argc; k <= NUM_PARM; k++) {
193 switch (tparm_type(name)) {
195 s = TPARM_2(s, numbers[1], strings[2]);
198 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
202 (void) _nc_tparm_analyze(s, p_is_s, &ignored);
203 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
218 /* use putp() in order to perform padding */
220 return exit_code(STRING, 0);
222 return exit_code(STRING, 1);
226 main(int argc, char **argv)
235 check_aliases(prg_name = _nc_rootname(argv[0]), TRUE);
237 term = getenv("TERM");
239 while ((c = getopt(argc, argv, "ST:V")) != -1) {
249 puts(curses_version());
250 ExitProgram(EXIT_SUCCESS);
258 * Modify the argument list to omit the options we processed.
260 if (is_reset || is_init) {
261 if (optind-- < argc) {
271 if (term == 0 || *term == '\0')
272 quit(2, "No value for $TERM and no -T specified");
274 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0)
275 quit(3, "unknown terminal \"%s\"", term);
278 if ((argc <= 0) && !is_reset && !is_init)
280 ExitProgram(tput_cmd(argc, argv));
283 while (fgets(buf, sizeof(buf), stdin) != 0) {
284 char *argvec[16]; /* command, 9 parms, null, & slop */
288 /* crack the argument list into a dope vector */
289 for (cp = buf; *cp; cp++) {
290 if (isspace(UChar(*cp))) {
292 } else if (cp == buf || cp[-1] == 0) {
293 argvec[argnum++] = cp;
294 if (argnum >= (int) SIZEOF(argvec) - 1)
301 && tput_cmd(argnum, argvec) != 0) {
303 result = 4; /* will return value >4 */