1 /****************************************************************************
2 * Copyright 2018-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * tput.c -- shellscript access to terminal capabilities
39 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from
40 * Ross Ridge's mytinfo package.
43 #include <tparm_type.h>
44 #include <clear_cmd.h>
45 #include <reset_cmd.h>
47 #include <transform.h>
48 #include <tty_settings.h>
50 MODULE_ID("$Id: tput.c,v 1.99 2022/02/26 23:19:31 tom Exp $")
52 #define PUTS(s) fputs(s, stdout)
54 const char *_nc_progname = "tput";
56 static bool is_init = FALSE;
57 static bool is_reset = FALSE;
58 static bool is_clear = FALSE;
60 static GCC_NORETURN void
61 quit(int status, const char *fmt, ...)
66 fprintf(stderr, "%s: ", _nc_progname);
67 vfprintf(stderr, fmt, argp);
68 fprintf(stderr, "\n");
73 static GCC_NORETURN void
74 usage(const char *optstring)
76 #define KEEP(s) s "\n"
77 static const char msg[] =
81 KEEP(" -S << read commands from standard input")
82 KEEP(" -T TERM use this instead of $TERM")
83 KEEP(" -V print curses-version")
84 KEEP(" -x do not try to clear scrollback")
87 KEEP(" clear clear the screen")
88 KEEP(" init initialize the terminal")
89 KEEP(" reset reinitialize the terminal")
90 KEEP(" capname unlike clear/init/reset, print value for capability \"capname\"")
93 (void) fprintf(stderr, "Usage: %s [options] [command]\n", _nc_progname);
94 if (optstring != NULL) {
97 fputc(UChar(*s), stderr);
98 if (!strncmp(s, " -", 3)) {
99 if (strchr(optstring, s[3]) == NULL)
100 s = strchr(s, '\n') + 1;
101 } else if (!strncmp(s, "\n\nC", 3))
108 ExitProgram(ErrUsage);
112 check_aliases(char *name, bool program)
114 static char my_init[] = "init";
115 static char my_reset[] = "reset";
116 static char my_clear[] = "clear";
119 if ((is_init = same_program(name, program ? PROG_INIT : my_init)))
121 if ((is_reset = same_program(name, program ? PROG_RESET : my_reset)))
123 if ((is_clear = same_program(name, program ? PROG_CLEAR : my_clear)))
129 exit_code(int token, int value)
135 result = !value; /* TRUE=0, FALSE=1 */
138 result = 0; /* always zero */
141 result = value; /* 0=normal, 1=missing */
148 * Returns nonzero on error.
151 tput_cmd(int fd, TTY * settings, bool opt_x, int argc, char **argv, int *used)
153 NCURSES_CONST char *name;
157 bool termcap = FALSE;
160 name = check_aliases(argv[0], FALSE);
162 if (is_reset || is_init) {
163 TTY oldmode = *settings;
165 int terasechar = -1; /* new erase character */
166 int intrchar = -1; /* new interrupt character */
167 int tkillchar = -1; /* new kill character */
170 reset_start(stdout, TRUE, FALSE);
171 reset_tty_settings(fd, settings, FALSE);
173 reset_start(stdout, FALSE, TRUE);
177 set_window_size(fd, &lines, &columns);
181 set_control_chars(settings, terasechar, intrchar, tkillchar);
182 set_conversions(settings);
184 if (send_init_strings(fd, &oldmode)) {
188 update_tty_settings(&oldmode, settings);
192 if (strcmp(name, "longname") == 0) {
199 if (strcmp(name, "clear") == 0) {
200 return (clear_cmd(opt_x) == ERR) ? ErrUsage : 0;
201 } else if ((status = tigetflag(name)) != -1) {
202 return exit_code(BOOLEAN, status);
203 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
204 (void) printf("%d\n", status);
205 return exit_code(NUMBER, 0);
206 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
209 const struct name_table_entry *np;
212 if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
213 switch (np->nte_type) {
215 name = boolnames[np->nte_index];
219 name = numnames[np->nte_index];
223 name = strnames[np->nte_index];
230 quit(ErrCapName, "unknown terminfo capability '%s'", name);
231 } else if (VALID_STRING(s)) {
236 long numbers[1 + NUM_PARM];
237 char *strings[1 + NUM_PARM];
238 char *p_is_s[NUM_PARM];
241 /* Nasty hack time. The tparm function needs to see numeric
242 * parameters as numbers, not as pointers to their string
246 for (k = 1; (k < argc) && (k <= NUM_PARM); k++) {
248 strings[k] = argv[k];
249 numbers[k] = strtol(argv[k], &tmp, 0);
250 if (tmp == 0 || *tmp != 0)
253 for (k = argc; k <= NUM_PARM; k++) {
258 paramType = tparm_type(name);
261 * If the capability is an extended one, analyze the string.
263 if (paramType == Numbers) {
264 struct name_table_entry const *entry_ptr;
265 entry_ptr = _nc_find_type_entry(name, STRING, FALSE);
266 if (entry_ptr == NULL) {
273 _nc_reset_tparm(NULL);
276 s = TPARM_2(s, numbers[1], strings[2]);
280 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
284 analyzed = _nc_tparm_analyze(NULL, s, p_is_s, &popcount);
285 #define myParam(n) numbers[n]
301 analyzed = _nc_tparm_analyze(NULL, s, p_is_s, &popcount);
302 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
316 if (analyzed < popcount) {
322 /* use putp() in order to perform padding */
324 return exit_code(STRING, 0);
326 return exit_code(STRING, 1);
330 main(int argc, char **argv)
342 bool opt_x = FALSE; /* clear scrollback if possible */
346 _nc_progname = check_aliases(_nc_rootname(argv[0]), TRUE);
347 is_alias = (is_clear || is_reset || is_init);
349 term = getenv("TERM");
351 while ((c = getopt(argc, argv, is_alias ? "T:Vx" : "ST:Vx")) != -1) {
362 puts(curses_version());
363 ExitProgram(EXIT_SUCCESS);
364 case 'x': /* do not try to clear scrollback */
368 usage(is_alias ? "TVx" : NULL);
373 need_tty = ((is_reset || is_init) ||
375 (!strcmp(argv[optind], "reset") ||
376 !strcmp(argv[optind], "init"))));
379 * Modify the argument list to omit the options we processed.
382 if (optind-- < argc) {
386 argv[0] = strdup(_nc_progname);
392 if (term == 0 || *term == '\0')
393 quit(ErrUsage, "No value for $TERM and no -T specified");
395 fd = save_tty_settings(&tty_settings, need_tty);
396 old_settings = tty_settings;
398 if (setupterm(term, fd, &errret) != OK && errret <= 0)
399 quit(ErrTermType, "unknown terminal \"%s\"", term);
403 if ((argc <= 0) && !is_alias)
406 tty_settings = old_settings;
407 code = tput_cmd(fd, &tty_settings, opt_x, argc, argv, &used);
416 while (fgets(buf, sizeof(buf), stdin) != 0) {
417 size_t need = strlen(buf);
418 char **argvec = typeCalloc(char *, need + 1);
423 if (argvec == NULL) {
424 quit(ErrSystem(1), strerror(errno));
427 /* split the buffer into tokens */
428 for (cp = buf; *cp; cp++) {
429 if (isspace(UChar(*cp))) {
431 } else if (cp == buf || cp[-1] == '\0') {
432 argvec[argnum++] = cp;
433 if (argnum >= (int) need)
441 tty_settings = old_settings;
442 code = tput_cmd(fd, &tty_settings, opt_x, argnum, argnow, &used);
445 result = ErrSystem(0); /* will return value >4 */