1 /****************************************************************************
2 * Copyright 2018-2022,2023 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.102 2023/04/08 16:26:36 tom Exp $")
52 #define PUTS(s) fputs(s, stdout)
54 const char *_nc_progname = "tput";
56 static bool opt_v = FALSE; /* quiet, do not show warnings */
57 static bool opt_x = FALSE; /* clear scrollback if possible */
59 static bool is_init = FALSE;
60 static bool is_reset = FALSE;
61 static bool is_clear = FALSE;
63 static GCC_NORETURN void
64 quit(int status, const char *fmt, ...)
69 fprintf(stderr, "%s: ", _nc_progname);
70 vfprintf(stderr, fmt, argp);
71 fprintf(stderr, "\n");
76 static GCC_NORETURN void
77 usage(const char *optstring)
79 #define KEEP(s) s "\n"
80 static const char msg[] =
84 KEEP(" -S << read commands from standard input")
85 KEEP(" -T TERM use this instead of $TERM")
86 KEEP(" -V print curses-version")
87 KEEP(" -v verbose, show warnings")
88 KEEP(" -x do not try to clear scrollback")
91 KEEP(" clear clear the screen")
92 KEEP(" init initialize the terminal")
93 KEEP(" reset reinitialize the terminal")
94 KEEP(" capname unlike clear/init/reset, print value for capability \"capname\"")
97 (void) fprintf(stderr, "Usage: %s [options] [command]\n", _nc_progname);
98 if (optstring != NULL) {
101 fputc(UChar(*s), stderr);
102 if (!strncmp(s, " -", 3)) {
103 if (strchr(optstring, s[3]) == NULL)
104 s = strchr(s, '\n') + 1;
105 } else if (!strncmp(s, "\n\nC", 3))
112 ExitProgram(ErrUsage);
116 check_aliases(char *name, bool program)
118 static char my_init[] = "init";
119 static char my_reset[] = "reset";
120 static char my_clear[] = "clear";
123 if ((is_init = same_program(name, program ? PROG_INIT : my_init)))
125 if ((is_reset = same_program(name, program ? PROG_RESET : my_reset)))
127 if ((is_clear = same_program(name, program ? PROG_CLEAR : my_clear)))
133 exit_code(int token, int value)
139 result = !value; /* TRUE=0, FALSE=1 */
142 result = 0; /* always zero */
145 result = value; /* 0=normal, 1=missing */
152 * Returns nonzero on error.
155 tput_cmd(int fd, TTY * settings, int argc, char **argv, int *used)
157 NCURSES_CONST char *name;
161 bool termcap = FALSE;
164 name = check_aliases(argv[0], FALSE);
166 if (is_reset || is_init) {
167 TTY oldmode = *settings;
169 int terasechar = -1; /* new erase character */
170 int intrchar = -1; /* new interrupt character */
171 int tkillchar = -1; /* new kill character */
174 reset_start(stdout, TRUE, FALSE);
175 reset_tty_settings(fd, settings, FALSE);
177 reset_start(stdout, FALSE, TRUE);
181 set_window_size(fd, &lines, &columns);
185 set_control_chars(settings, terasechar, intrchar, tkillchar);
186 set_conversions(settings);
188 if (send_init_strings(fd, &oldmode)) {
192 update_tty_settings(&oldmode, settings);
196 if (strcmp(name, "longname") == 0) {
203 if (strcmp(name, "clear") == 0) {
204 return (clear_cmd(opt_x) == ERR) ? ErrUsage : 0;
205 } else if ((status = tigetflag(name)) != -1) {
206 return exit_code(BOOLEAN, status);
207 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) {
208 (void) printf("%d\n", status);
209 return exit_code(NUMBER, 0);
210 } else if ((s = tigetstr(name)) == CANCELLED_STRING) {
213 const struct name_table_entry *np;
216 if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) {
217 switch (np->nte_type) {
219 name = boolnames[np->nte_index];
223 name = numnames[np->nte_index];
227 name = strnames[np->nte_index];
234 quit(ErrCapName, "unknown terminfo capability '%s'", name);
235 } else if (VALID_STRING(s)) {
242 long numbers[1 + NUM_PARM];
243 char *strings[1 + NUM_PARM];
244 char *p_is_s[NUM_PARM];
247 /* Nasty hack time. The tparm function needs to see numeric
248 * parameters as numbers, not as pointers to their string
252 for (k = 1; (k < argc) && (k <= NUM_PARM); k++) {
254 strings[k] = argv[k];
255 numbers[k] = strtol(argv[k], &tmp, 0);
256 if (tmp == 0 || *tmp != 0)
259 for (k = argc; k <= NUM_PARM; k++) {
264 paramType = tparm_type(name);
267 * If the capability is an extended one, analyze the string.
269 if (paramType == Numbers) {
270 struct name_table_entry const *entry_ptr;
271 entry_ptr = _nc_find_type_entry(name, STRING, FALSE);
272 if (entry_ptr == NULL) {
279 _nc_reset_tparm(NULL);
281 * Count the number of numeric parameters which are provided.
284 for (narg = 1; narg < argc; ++narg) {
286 long check = strtol(argv[narg], &ending, 10);
287 if (check < 0 || ending == argv[narg] || *ending != '\0')
293 s = TPARM_1(s, strings[1]);
295 if (provided == 0 && argc >= 1)
299 s = TPARM_2(s, strings[1], strings[2]);
301 if (provided == 0 && argc >= 1)
303 if (provided == 1 && argc >= 2)
307 s = TPARM_2(s, numbers[1], strings[2]);
309 if (provided == 1 && argc >= 2)
313 s = TPARM_3(s, numbers[1], strings[2], strings[3]);
315 if (provided == 1 && argc >= 2)
317 if (provided == 2 && argc >= 3)
321 analyzed = _nc_tparm_analyze(NULL, s, p_is_s, &popcount);
322 #define myParam(n) numbers[n]
338 analyzed = _nc_tparm_analyze(NULL, s, p_is_s, &popcount);
339 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
353 if (analyzed < popcount) {
356 if (opt_v && (analyzed != provided)) {
357 fprintf(stderr, "%s: %s parameters for \"%s\"\n",
359 (analyzed < provided ? "extra" : "missing"),
365 /* use putp() in order to perform padding */
367 return exit_code(STRING, 0);
369 return exit_code(STRING, 1);
373 main(int argc, char **argv)
388 _nc_progname = check_aliases(_nc_rootname(argv[0]), TRUE);
389 is_alias = (is_clear || is_reset || is_init);
391 term = getenv("TERM");
393 while ((c = getopt(argc, argv, is_alias ? "T:Vvx" : "ST:Vvx")) != -1) {
404 puts(curses_version());
405 ExitProgram(EXIT_SUCCESS);
406 case 'v': /* verbose */
409 case 'x': /* do not try to clear scrollback */
413 usage(is_alias ? "TVx" : NULL);
418 need_tty = ((is_reset || is_init) ||
420 (!strcmp(argv[optind], "reset") ||
421 !strcmp(argv[optind], "init"))));
424 * Modify the argument list to omit the options we processed.
427 if (optind-- < argc) {
431 argv[0] = strdup(_nc_progname);
437 if (term == 0 || *term == '\0')
438 quit(ErrUsage, "No value for $TERM and no -T specified");
440 fd = save_tty_settings(&tty_settings, need_tty);
441 old_settings = tty_settings;
443 if (setupterm(term, fd, &errret) != OK && errret <= 0)
444 quit(ErrTermType, "unknown terminal \"%s\"", term);
448 if ((argc <= 0) && !is_alias)
451 tty_settings = old_settings;
452 code = tput_cmd(fd, &tty_settings, argc, argv, &used);
461 while (fgets(buf, sizeof(buf), stdin) != 0) {
462 size_t need = strlen(buf);
463 char **argvec = typeCalloc(char *, need + 1);
468 if (argvec == NULL) {
469 quit(ErrSystem(1), strerror(errno));
472 /* split the buffer into tokens */
473 for (cp = buf; *cp; cp++) {
474 if (isspace(UChar(*cp))) {
476 } else if (cp == buf || cp[-1] == '\0') {
477 argvec[argnum++] = cp;
478 if (argnum >= (int) need)
486 tty_settings = old_settings;
487 code = tput_cmd(fd, &tty_settings, argnum, argnow, &used);
490 result = ErrSystem(0); /* will return value >4 */