Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2018-2023,2024 Thomas E. Dickey * |
| 3 | * Copyright 1998-2016,2017 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4 | * * |
| 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: * |
| 12 | * * |
| 13 | * The above copyright notice and this permission notice shall be included * |
| 14 | * in all copies or substantial portions of the Software. * |
| 15 | * * |
| 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. * |
| 23 | * * |
| 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 * |
| 27 | * authorization. * |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | /**************************************************************************** |
| 31 | * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * |
| 32 | * and: Eric S. Raymond <esr@snark.thyrsus.com> * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 33 | * and: Thomas E. Dickey 1996-on * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 34 | ****************************************************************************/ |
| 35 | |
| 36 | /* |
| 37 | * tput.c -- shellscript access to terminal capabilities |
| 38 | * |
| 39 | * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from |
| 40 | * Ross Ridge's mytinfo package. |
| 41 | */ |
| 42 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 43 | #include <tparm_type.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 44 | #include <clear_cmd.h> |
| 45 | #include <reset_cmd.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 46 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 47 | #include <transform.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 48 | #include <tty_settings.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 49 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 50 | MODULE_ID("$Id: tput.c,v 1.104 2024/04/20 22:20:51 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 51 | |
| 52 | #define PUTS(s) fputs(s, stdout) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 53 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 54 | const char *_nc_progname = "tput"; |
| 55 | |
| 56 | static bool opt_v = FALSE; /* quiet, do not show warnings */ |
| 57 | static bool opt_x = FALSE; /* clear scrollback if possible */ |
| 58 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 59 | static bool is_init = FALSE; |
| 60 | static bool is_reset = FALSE; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 61 | static bool is_clear = FALSE; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 62 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 63 | static GCC_NORETURN void |
| 64 | quit(int status, const char *fmt, ...) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 65 | { |
| 66 | va_list argp; |
| 67 | |
| 68 | va_start(argp, fmt); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 69 | fprintf(stderr, "%s: ", _nc_progname); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 70 | vfprintf(stderr, fmt, argp); |
| 71 | fprintf(stderr, "\n"); |
| 72 | va_end(argp); |
| 73 | ExitProgram(status); |
| 74 | } |
| 75 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 76 | static GCC_NORETURN void |
| 77 | usage(const char *optstring) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 78 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 79 | #define KEEP(s) s "\n" |
| 80 | static const char msg[] = |
| 81 | { |
| 82 | KEEP("") |
| 83 | KEEP("Options:") |
| 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") |
| 89 | KEEP("") |
| 90 | KEEP("Commands:") |
| 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\"") |
| 95 | }; |
| 96 | #undef KEEP |
| 97 | (void) fprintf(stderr, "Usage: %s [options] [command]\n", _nc_progname); |
| 98 | if (optstring != NULL) { |
| 99 | const char *s = msg; |
| 100 | while (*s != '\0') { |
| 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)) |
| 106 | break; |
| 107 | ++s; |
| 108 | } |
| 109 | } else { |
| 110 | fputs(msg, stderr); |
| 111 | } |
| 112 | ExitProgram(ErrUsage); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 113 | } |
| 114 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 115 | static char * |
| 116 | check_aliases(char *name, bool program) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 117 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 118 | static char my_init[] = "init"; |
| 119 | static char my_reset[] = "reset"; |
| 120 | static char my_clear[] = "clear"; |
| 121 | |
| 122 | char *result = name; |
| 123 | if ((is_init = same_program(name, program ? PROG_INIT : my_init))) |
| 124 | result = my_init; |
| 125 | if ((is_reset = same_program(name, program ? PROG_RESET : my_reset))) |
| 126 | result = my_reset; |
| 127 | if ((is_clear = same_program(name, program ? PROG_CLEAR : my_clear))) |
| 128 | result = my_clear; |
| 129 | return result; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static int |
| 133 | exit_code(int token, int value) |
| 134 | { |
| 135 | int result = 99; |
| 136 | |
| 137 | switch (token) { |
| 138 | case BOOLEAN: |
| 139 | result = !value; /* TRUE=0, FALSE=1 */ |
| 140 | break; |
| 141 | case NUMBER: |
| 142 | result = 0; /* always zero */ |
| 143 | break; |
| 144 | case STRING: |
| 145 | result = value; /* 0=normal, 1=missing */ |
| 146 | break; |
| 147 | } |
| 148 | return result; |
| 149 | } |
| 150 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 151 | /* |
| 152 | * Returns nonzero on error. |
| 153 | */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 154 | static int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 155 | tput_cmd(int fd, TTY * settings, int argc, char **argv, int *used) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 156 | { |
| 157 | NCURSES_CONST char *name; |
| 158 | char *s; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 159 | int status; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 160 | #if !PURE_TERMINFO |
| 161 | bool termcap = FALSE; |
| 162 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 163 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 164 | name = check_aliases(argv[0], FALSE); |
| 165 | *used = 1; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 166 | if (is_reset || is_init) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 167 | TTY oldmode = *settings; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 168 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 169 | int terasechar = -1; /* new erase character */ |
| 170 | int intrchar = -1; /* new interrupt character */ |
| 171 | int tkillchar = -1; /* new kill character */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 172 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 173 | if (is_reset) { |
| 174 | reset_start(stdout, TRUE, FALSE); |
| 175 | reset_tty_settings(fd, settings, FALSE); |
| 176 | } else { |
| 177 | reset_start(stdout, FALSE, TRUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 178 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 179 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 180 | #if HAVE_SIZECHANGE |
| 181 | { |
| 182 | NCURSES_INT2 my_rows = lines; |
| 183 | NCURSES_INT2 my_cols = columns; |
| 184 | set_window_size(fd, &my_rows, &my_cols); |
| 185 | lines = my_rows; |
| 186 | columns = my_cols; |
| 187 | } |
| 188 | #else |
| 189 | (void) fd; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 190 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 191 | set_control_chars(settings, terasechar, intrchar, tkillchar); |
| 192 | set_conversions(settings); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 193 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 194 | if (send_init_strings(fd, &oldmode)) { |
| 195 | reset_flush(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 196 | } |
| 197 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 198 | update_tty_settings(&oldmode, settings); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | if (strcmp(name, "longname") == 0) { |
| 203 | PUTS(longname()); |
| 204 | return 0; |
| 205 | } |
| 206 | #if !PURE_TERMINFO |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 207 | retry: |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 208 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 209 | if (strcmp(name, "clear") == 0) { |
| 210 | return (clear_cmd(opt_x) == ERR) ? ErrUsage : 0; |
| 211 | } else if ((status = tigetflag(name)) != -1) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 212 | return exit_code(BOOLEAN, status); |
| 213 | } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) { |
| 214 | (void) printf("%d\n", status); |
| 215 | return exit_code(NUMBER, 0); |
| 216 | } else if ((s = tigetstr(name)) == CANCELLED_STRING) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 217 | #if !PURE_TERMINFO |
| 218 | if (!termcap) { |
| 219 | const struct name_table_entry *np; |
| 220 | |
| 221 | termcap = TRUE; |
| 222 | if ((np = _nc_find_entry(name, _nc_get_hash_table(termcap))) != 0) { |
| 223 | switch (np->nte_type) { |
| 224 | case BOOLEAN: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 225 | name = boolnames[np->nte_index]; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 226 | break; |
| 227 | |
| 228 | case NUMBER: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 229 | name = numnames[np->nte_index]; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 230 | break; |
| 231 | |
| 232 | case STRING: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 233 | name = strnames[np->nte_index]; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | goto retry; |
| 237 | } |
| 238 | } |
| 239 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 240 | quit(ErrCapName, "unknown terminfo capability '%s'", name); |
| 241 | } else if (VALID_STRING(s)) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 242 | if (argc > 1) { |
| 243 | int k; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 244 | int narg; |
| 245 | int analyzed; |
| 246 | int provided; |
| 247 | int popcount; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 248 | long numbers[1 + NUM_PARM]; |
| 249 | char *strings[1 + NUM_PARM]; |
| 250 | char *p_is_s[NUM_PARM]; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 251 | TParams paramType; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 252 | |
| 253 | /* Nasty hack time. The tparm function needs to see numeric |
| 254 | * parameters as numbers, not as pointers to their string |
| 255 | * representations |
| 256 | */ |
| 257 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 258 | for (k = 1; (k < argc) && (k <= NUM_PARM); k++) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 259 | char *tmp = 0; |
| 260 | strings[k] = argv[k]; |
| 261 | numbers[k] = strtol(argv[k], &tmp, 0); |
| 262 | if (tmp == 0 || *tmp != 0) |
| 263 | numbers[k] = 0; |
| 264 | } |
| 265 | for (k = argc; k <= NUM_PARM; k++) { |
| 266 | numbers[k] = 0; |
| 267 | strings[k] = 0; |
| 268 | } |
| 269 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 270 | paramType = tparm_type(name); |
| 271 | #if NCURSES_XNAMES |
| 272 | /* |
| 273 | * If the capability is an extended one, analyze the string. |
| 274 | */ |
| 275 | if (paramType == Numbers) { |
| 276 | struct name_table_entry const *entry_ptr; |
| 277 | entry_ptr = _nc_find_type_entry(name, STRING, FALSE); |
| 278 | if (entry_ptr == NULL) { |
| 279 | paramType = Other; |
| 280 | } |
| 281 | } |
| 282 | #endif |
| 283 | |
| 284 | popcount = 0; |
| 285 | _nc_reset_tparm(NULL); |
| 286 | /* |
| 287 | * Count the number of numeric parameters which are provided. |
| 288 | */ |
| 289 | provided = 0; |
| 290 | for (narg = 1; narg < argc; ++narg) { |
| 291 | char *ending = NULL; |
| 292 | long check = strtol(argv[narg], &ending, 0); |
| 293 | if (check < 0 || ending == argv[narg] || *ending != '\0') |
| 294 | break; |
| 295 | provided = narg; |
| 296 | } |
| 297 | switch (paramType) { |
| 298 | case Str: |
| 299 | s = TPARM_1(s, strings[1]); |
| 300 | analyzed = 1; |
| 301 | if (provided == 0 && argc >= 1) |
| 302 | provided++; |
| 303 | break; |
| 304 | case Str_Str: |
| 305 | s = TPARM_2(s, strings[1], strings[2]); |
| 306 | analyzed = 2; |
| 307 | if (provided == 0 && argc >= 1) |
| 308 | provided++; |
| 309 | if (provided == 1 && argc >= 2) |
| 310 | provided++; |
| 311 | break; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 312 | case Num_Str: |
| 313 | s = TPARM_2(s, numbers[1], strings[2]); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 314 | analyzed = 2; |
| 315 | if (provided == 1 && argc >= 2) |
| 316 | provided++; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 317 | break; |
| 318 | case Num_Str_Str: |
| 319 | s = TPARM_3(s, numbers[1], strings[2], strings[3]); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 320 | analyzed = 3; |
| 321 | if (provided == 1 && argc >= 2) |
| 322 | provided++; |
| 323 | if (provided == 2 && argc >= 3) |
| 324 | provided++; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 325 | break; |
| 326 | case Numbers: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 327 | analyzed = _nc_tparm_analyze(NULL, s, p_is_s, &popcount); |
| 328 | #define myParam(n) numbers[n] |
| 329 | s = TIPARM_9(s, |
| 330 | myParam(1), |
| 331 | myParam(2), |
| 332 | myParam(3), |
| 333 | myParam(4), |
| 334 | myParam(5), |
| 335 | myParam(6), |
| 336 | myParam(7), |
| 337 | myParam(8), |
| 338 | myParam(9)); |
| 339 | #undef myParam |
| 340 | break; |
| 341 | case Other: |
| 342 | /* FALLTHRU */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 343 | default: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 344 | analyzed = _nc_tparm_analyze(NULL, s, p_is_s, &popcount); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 345 | #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n]) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 346 | s = TPARM_9(s, |
| 347 | myParam(1), |
| 348 | myParam(2), |
| 349 | myParam(3), |
| 350 | myParam(4), |
| 351 | myParam(5), |
| 352 | myParam(6), |
| 353 | myParam(7), |
| 354 | myParam(8), |
| 355 | myParam(9)); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 356 | #undef myParam |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 357 | break; |
| 358 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 359 | if (analyzed < popcount) { |
| 360 | analyzed = popcount; |
| 361 | } |
| 362 | if (opt_v && (analyzed != provided)) { |
| 363 | fprintf(stderr, "%s: %s parameters for \"%s\"\n", |
| 364 | _nc_progname, |
| 365 | (analyzed < provided ? "extra" : "missing"), |
| 366 | argv[0]); |
| 367 | } |
| 368 | *used += provided; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* use putp() in order to perform padding */ |
| 372 | putp(s); |
| 373 | return exit_code(STRING, 0); |
| 374 | } |
| 375 | return exit_code(STRING, 1); |
| 376 | } |
| 377 | |
| 378 | int |
| 379 | main(int argc, char **argv) |
| 380 | { |
| 381 | char *term; |
| 382 | int errret; |
| 383 | bool cmdline = TRUE; |
| 384 | int c; |
| 385 | char buf[BUFSIZ]; |
| 386 | int result = 0; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 387 | int fd; |
| 388 | int used; |
| 389 | TTY old_settings; |
| 390 | TTY tty_settings; |
| 391 | bool is_alias; |
| 392 | bool need_tty; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 393 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 394 | _nc_progname = check_aliases(_nc_rootname(argv[0]), TRUE); |
| 395 | is_alias = (is_clear || is_reset || is_init); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 396 | |
| 397 | term = getenv("TERM"); |
| 398 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 399 | while ((c = getopt(argc, argv, is_alias ? "T:Vvx" : "ST:Vvx")) != -1) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 400 | switch (c) { |
| 401 | case 'S': |
| 402 | cmdline = FALSE; |
| 403 | break; |
| 404 | case 'T': |
| 405 | use_env(FALSE); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 406 | use_tioctl(TRUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 407 | term = optarg; |
| 408 | break; |
| 409 | case 'V': |
| 410 | puts(curses_version()); |
| 411 | ExitProgram(EXIT_SUCCESS); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 412 | case 'v': /* verbose */ |
| 413 | opt_v = TRUE; |
| 414 | break; |
| 415 | case 'x': /* do not try to clear scrollback */ |
| 416 | opt_x = TRUE; |
| 417 | break; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 418 | default: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 419 | usage(is_alias ? "TVx" : NULL); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 420 | /* NOTREACHED */ |
| 421 | } |
| 422 | } |
| 423 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 424 | need_tty = ((is_reset || is_init) || |
| 425 | (optind < argc && |
| 426 | (!strcmp(argv[optind], "reset") || |
| 427 | !strcmp(argv[optind], "init")))); |
| 428 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 429 | /* |
| 430 | * Modify the argument list to omit the options we processed. |
| 431 | */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 432 | if (is_alias) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 433 | if (optind-- < argc) { |
| 434 | argc -= optind; |
| 435 | argv += optind; |
| 436 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 437 | argv[0] = strdup(_nc_progname); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 438 | } else { |
| 439 | argc -= optind; |
| 440 | argv += optind; |
| 441 | } |
| 442 | |
| 443 | if (term == 0 || *term == '\0') |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 444 | quit(ErrUsage, "No value for $TERM and no -T specified"); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 445 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 446 | fd = save_tty_settings(&tty_settings, need_tty); |
| 447 | old_settings = tty_settings; |
| 448 | |
| 449 | if (setupterm(term, fd, &errret) != OK && errret <= 0) |
| 450 | quit(ErrTermType, "unknown terminal \"%s\"", term); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 451 | |
| 452 | if (cmdline) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 453 | int code = 0; |
| 454 | if ((argc <= 0) && !is_alias) |
| 455 | usage(NULL); |
| 456 | while (argc > 0) { |
| 457 | tty_settings = old_settings; |
| 458 | code = tput_cmd(fd, &tty_settings, argc, argv, &used); |
| 459 | if (code != 0) |
| 460 | break; |
| 461 | argc -= used; |
| 462 | argv += used; |
| 463 | } |
| 464 | ExitProgram(code); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | while (fgets(buf, sizeof(buf), stdin) != 0) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 468 | size_t need = strlen(buf); |
| 469 | char **argvec = typeCalloc(char *, need + 1); |
| 470 | char **argnow; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 471 | int argnum = 0; |
| 472 | char *cp; |
| 473 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 474 | if (argvec == NULL) { |
| 475 | quit(ErrSystem(1), strerror(errno)); |
| 476 | } |
| 477 | |
| 478 | /* split the buffer into tokens */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 479 | for (cp = buf; *cp; cp++) { |
| 480 | if (isspace(UChar(*cp))) { |
| 481 | *cp = '\0'; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 482 | } else if (cp == buf || cp[-1] == '\0') { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 483 | argvec[argnum++] = cp; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 484 | if (argnum >= (int) need) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 485 | break; |
| 486 | } |
| 487 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 488 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 489 | argnow = argvec; |
| 490 | while (argnum > 0) { |
| 491 | int code; |
| 492 | tty_settings = old_settings; |
| 493 | code = tput_cmd(fd, &tty_settings, argnum, argnow, &used); |
| 494 | if (code != 0) { |
| 495 | if (result == 0) |
| 496 | result = ErrSystem(0); /* will return value >4 */ |
| 497 | ++result; |
| 498 | } |
| 499 | argnum -= used; |
| 500 | argnow += used; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 501 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 502 | free(argvec); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ExitProgram(result); |
| 506 | } |