1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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 <clear_cmd.h>
44 #include <reset_cmd.h>
47 #include <dump_entry.h>
50 #include <transform.h>
51 #include <tty_settings.h>
53 MODULE_ID("$Id: tput.c,v 1.69 2017/01/21 17:40:51 tom Exp $")
55 #define PUTS(s) fputs(s, stdout)
57 const char *_nc_progname = "tput";
59 static char *prg_name;
60 static bool is_init = FALSE;
61 static bool is_reset = FALSE;
62 static bool is_clear = FALSE;
65 quit(int status, const char *fmt,...)
70 fprintf(stderr, "%s: ", prg_name);
71 vfprintf(stderr, fmt, argp);
72 fprintf(stderr, "\n");
80 fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name);
81 ExitProgram(EXIT_FAILURE);
85 check_aliases(char *name, bool program)
87 static char my_init[] = "init";
88 static char my_reset[] = "reset";
89 static char my_clear[] = "clear";
92 if ((is_init = same_program(name, program ? PROG_INIT : my_init)))
94 if ((is_reset = same_program(name, program ? PROG_RESET : my_reset)))
96 if ((is_clear = same_program(name, program ? PROG_CLEAR : my_clear)))
102 exit_code(int token, int value)
108 result = !value; /* TRUE=0, FALSE=1 */
111 result = 0; /* always zero */
114 result = value; /* 0=normal, 1=missing */
121 tput_cmd(int fd, TTY * saved_settings, int argc, char *argv[])
123 NCURSES_CONST char *name;
127 bool termcap = FALSE;
130 name = check_aliases(argv[0], FALSE);
131 if (is_reset || is_init) {
134 int terasechar = -1; /* new erase character */
135 int intrchar = -1; /* new interrupt character */
136 int tkillchar = -1; /* new kill character */
139 reset_start(stdout, TRUE, FALSE);
140 reset_tty_settings(fd, saved_settings);
142 reset_start(stdout, FALSE, TRUE);
146 set_window_size(fd, &lines, &columns);
150 set_control_chars(saved_settings, terasechar, intrchar, tkillchar);
151 set_conversions(saved_settings);
152 if (send_init_strings(fd, &oldmode)) {
156 update_tty_settings(&oldmode, saved_settings);
160 if (strcmp(name, "longname") == 0) {
167 if (strcmp(name, "clear") == 0) {
169 } else if ((status = tigetflag(name)) != -1) {
170 return exit_code(BOOLEAN, status);
171 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
172 (void) printf("%d\n", status);
173 return exit_code(NUMBER, 0);
174 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
177 const struct name_table_entry *np;
180 if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
181 switch (np->nte_type) {
183 name = boolnames[np->nte_index];
187 name = numnames[np->nte_index];
191 name = strnames[np->nte_index];
198 quit(4, "unknown terminfo capability '%s'", name);
199 } else if (s != ABSENT_STRING) {
203 long numbers[1 + NUM_PARM];
204 char *strings[1 + NUM_PARM];
205 char *p_is_s[NUM_PARM];
207 /* Nasty hack time. The tparm function needs to see numeric
208 * parameters as numbers, not as pointers to their string
212 for (k = 1; k < argc; k++) {
214 strings[k] = argv[k];
215 numbers[k] = strtol(argv[k], &tmp, 0);
216 if (tmp == 0 || *tmp != 0)
219 for (k = argc; k <= NUM_PARM; k++) {
224 switch (tparm_type(name)) {
226 s = TPARM_2(s, numbers[1], strings[2]);
229 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
233 (void) _nc_tparm_analyze(s, p_is_s, &ignored);
234 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
249 /* use putp() in order to perform padding */
251 return exit_code(STRING, 0);
253 return exit_code(STRING, 1);
257 main(int argc, char **argv)
268 prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
270 term = getenv("TERM");
272 while ((c = getopt(argc, argv, "ST:V")) != -1) {
282 puts(curses_version());
283 ExitProgram(EXIT_SUCCESS);
291 * Modify the argument list to omit the options we processed.
293 if (is_clear || is_reset || is_init) {
294 if (optind-- < argc) {
304 if (term == 0 || *term == '\0')
305 quit(2, "No value for $TERM and no -T specified");
307 fd = save_tty_settings(&tty_settings);
309 if (setupterm(term, fd, &errret) != OK && errret <= 0)
310 quit(3, "unknown terminal \"%s\"", term);
313 if ((argc <= 0) && !(is_clear || is_reset || is_init))
315 ExitProgram(tput_cmd(fd, &tty_settings, argc, argv));
318 while (fgets(buf, sizeof(buf), stdin) != 0) {
319 char *argvec[16]; /* command, 9 parms, null, & slop */
323 /* crack the argument list into a dope vector */
324 for (cp = buf; *cp; cp++) {
325 if (isspace(UChar(*cp))) {
327 } else if (cp == buf || cp[-1] == 0) {
328 argvec[argnum++] = cp;
329 if (argnum >= (int) SIZEOF(argvec) - 1)
336 && tput_cmd(fd, &tty_settings, argnum, argvec) != 0) {
338 result = 4; /* will return value >4 */