1 /**************************************************************************** 2 * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. * 3 * * 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: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 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. * 22 * * 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 * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 ****************************************************************************/ 33 34 /* 35 * tput.c -- shellscript access to terminal capabilities 36 * 37 * by Eric S. Raymond <esr@snark.thyrsus.com>, portions based on code from 38 * Ross Ridge's mytinfo package. 39 */ 40 41 #define USE_LIBTINFO 42 #include <progs.priv.h> 43 44 #if !PURE_TERMINFO 45 #include <termsort.c> 46 #endif 47 #include <transform.h> 48 49 MODULE_ID("$Id: tput.c,v 1.41 2007/10/13 20:34:16 tom Exp $") 50 51 #define PUTS(s) fputs(s, stdout) 52 #define PUTCHAR(c) putchar(c) 53 #define FLUSH fflush(stdout) 54 55 typedef enum { 56 Numbers = 0 57 ,Num_Str 58 ,Num_Str_Str 59 } TParams; 60 61 static char *prg_name; 62 static bool is_init = FALSE; 63 static bool is_reset = FALSE; 64 65 static void 66 quit(int status, const char *fmt,...) 67 { 68 va_list argp; 69 70 va_start(argp, fmt); 71 fprintf(stderr, "%s: ", prg_name); 72 vfprintf(stderr, fmt, argp); 73 fprintf(stderr, "\n"); 74 va_end(argp); 75 ExitProgram(status); 76 } 77 78 static void 79 usage(void) 80 { 81 fprintf(stderr, "usage: %s [-V] [-S] [-T term] capname\n", prg_name); 82 ExitProgram(EXIT_FAILURE); 83 } 84 85 static void 86 check_aliases(const char *name) 87 { 88 is_init = (strcmp(name, PROG_INIT) == 0); 89 is_reset = (strcmp(name, PROG_RESET) == 0); 90 } 91 92 /* 93 * Lookup the type of call we should make to tparm(). This ignores the actual 94 * terminfo capability (bad, because it is not extensible), but makes this 95 * code portable to platforms where sizeof(int) != sizeof(char *). 96 * 97 * FIXME: If we want extensibility, analyze the capability string as we do 98 * in tparm() to decide how to parse the varargs list. 99 */ 100 static TParams 101 tparm_type(const char *name) 102 { 103 #define TD(code, longname, ti, tc) {code,longname},{code,ti},{code,tc} 104 TParams result = Numbers; 105 /* *INDENT-OFF* */ 106 static const struct { 107 TParams code; 108 const char *name; 109 } table[] = { 110 TD(Num_Str, "pkey_key", "pfkey", "pk"), 111 TD(Num_Str, "pkey_local", "pfloc", "pl"), 112 TD(Num_Str, "pkey_xmit", "pfx", "px"), 113 TD(Num_Str, "plab_norm", "pln", "pn"), 114 TD(Num_Str_Str, "pkey_plab", "pfxl", "xl"), 115 }; 116 /* *INDENT-ON* */ 117 118 unsigned n; 119 for (n = 0; n < SIZEOF(table); n++) { 120 if (!strcmp(name, table[n].name)) { 121 result = table[n].code; 122 break; 123 } 124 } 125 return result; 126 } 127 128 static int 129 exit_code(int token, int value) 130 { 131 int result = 99; 132 133 switch (token) { 134 case BOOLEAN: 135 result = !value; /* TRUE=0, FALSE=1 */ 136 break; 137 case NUMBER: 138 result = 0; /* always zero */ 139 break; 140 case STRING: 141 result = value; /* 0=normal, 1=missing */ 142 break; 143 } 144 return result; 145 } 146 147 static int 148 tput(int argc, char *argv[]) 149 { 150 NCURSES_CONST char *name; 151 char *s; 152 int i, j, c; 153 int status; 154 FILE *f; 155 156 if ((name = argv[0]) == 0) 157 name = ""; 158 check_aliases(name); 159 if (is_reset || is_init) { 160 if (init_prog != 0) { 161 system(init_prog); 162 } 163 FLUSH; 164 165 if (is_reset && reset_1string != 0) { 166 PUTS(reset_1string); 167 } else if (init_1string != 0) { 168 PUTS(init_1string); 169 } 170 FLUSH; 171 172 if (is_reset && reset_2string != 0) { 173 PUTS(reset_2string); 174 } else if (init_2string != 0) { 175 PUTS(init_2string); 176 } 177 FLUSH; 178 179 #ifdef set_lr_margin 180 if (set_lr_margin != 0) { 181 PUTS(TPARM_2(set_lr_margin, 0, columns - 1)); 182 } else 183 #endif 184 #ifdef set_left_margin_parm 185 if (set_left_margin_parm != 0 186 && set_right_margin_parm != 0) { 187 PUTS(TPARM_1(set_left_margin_parm, 0)); 188 PUTS(TPARM_1(set_right_margin_parm, columns - 1)); 189 } else 190 #endif 191 if (clear_margins != 0 192 && set_left_margin != 0 193 && set_right_margin != 0) { 194 PUTS(clear_margins); 195 if (carriage_return != 0) { 196 PUTS(carriage_return); 197 } else { 198 PUTCHAR('\r'); 199 } 200 PUTS(set_left_margin); 201 if (parm_right_cursor) { 202 PUTS(TPARM_1(parm_right_cursor, columns - 1)); 203 } else { 204 for (i = 0; i < columns - 1; i++) { 205 PUTCHAR(' '); 206 } 207 } 208 PUTS(set_right_margin); 209 if (carriage_return != 0) { 210 PUTS(carriage_return); 211 } else { 212 PUTCHAR('\r'); 213 } 214 } 215 FLUSH; 216 217 if (init_tabs != 8) { 218 if (clear_all_tabs != 0 && set_tab != 0) { 219 for (i = 0; i < columns - 1; i += 8) { 220 if (parm_right_cursor) { 221 PUTS(TPARM_1(parm_right_cursor, 8)); 222 } else { 223 for (j = 0; j < 8; j++) 224 PUTCHAR(' '); 225 } 226 PUTS(set_tab); 227 } 228 FLUSH; 229 } 230 } 231 232 if (is_reset && reset_file != 0) { 233 f = fopen(reset_file, "r"); 234 if (f == 0) { 235 quit(4 + errno, "Can't open reset_file: '%s'", reset_file); 236 } 237 while ((c = fgetc(f)) != EOF) { 238 PUTCHAR(c); 239 } 240 fclose(f); 241 } else if (init_file != 0) { 242 f = fopen(init_file, "r"); 243 if (f == 0) { 244 quit(4 + errno, "Can't open init_file: '%s'", init_file); 245 } 246 while ((c = fgetc(f)) != EOF) { 247 PUTCHAR(c); 248 } 249 fclose(f); 250 } 251 FLUSH; 252 253 if (is_reset && reset_3string != 0) { 254 PUTS(reset_3string); 255 } else if (init_3string != 0) { 256 PUTS(init_3string); 257 } 258 FLUSH; 259 return 0; 260 } 261 262 if (strcmp(name, "longname") == 0) { 263 PUTS(longname()); 264 return 0; 265 } 266 #if !PURE_TERMINFO 267 { 268 const struct name_table_entry *np; 269 270 if ((np = _nc_find_entry(name, _nc_get_hash_table(1))) != 0) 271 switch (np->nte_type) { 272 case BOOLEAN: 273 if (bool_from_termcap[np->nte_index]) 274 name = boolnames[np->nte_index]; 275 break; 276 277 case NUMBER: 278 if (num_from_termcap[np->nte_index]) 279 name = numnames[np->nte_index]; 280 break; 281 282 case STRING: 283 if (str_from_termcap[np->nte_index]) 284 name = strnames[np->nte_index]; 285 break; 286 } 287 } 288 #endif 289 290 if ((status = tigetflag(name)) != -1) { 291 return exit_code(BOOLEAN, status); 292 } else if ((status = tigetnum(name)) != CANCELLED_NUMERIC) { 293 (void) printf("%d\n", status); 294 return exit_code(NUMBER, 0); 295 } else if ((s = tigetstr(name)) == CANCELLED_STRING) { 296 quit(4, "unknown terminfo capability '%s'", name); 297 } else if (s != ABSENT_STRING) { 298 if (argc > 1) { 299 int k; 300 int popcount; 301 long numbers[1 + NUM_PARM]; 302 char *strings[1 + NUM_PARM]; 303 char *p_is_s[NUM_PARM]; 304 305 /* Nasty hack time. The tparm function needs to see numeric 306 * parameters as numbers, not as pointers to their string 307 * representations 308 */ 309 310 for (k = 1; k < argc; k++) { 311 char *tmp = 0; 312 strings[k] = argv[k]; 313 numbers[k] = strtol(argv[k], &tmp, 0); 314 if (tmp == 0 || *tmp != 0) 315 numbers[k] = 0; 316 } 317 for (k = argc; k <= NUM_PARM; k++) { 318 numbers[k] = 0; 319 strings[k] = 0; 320 } 321 322 switch (tparm_type(name)) { 323 case Num_Str: 324 s = TPARM_2(s, numbers[1], strings[2]); 325 break; 326 case Num_Str_Str: 327 s = TPARM_3(s, numbers[1], strings[2], strings[3]); 328 break; 329 case Numbers: 330 default: 331 (void) _nc_tparm_analyze(s, p_is_s, &popcount); 332 #define myParam(n) (p_is_s[n - 1] != 0 ? ((long) strings[n]) : numbers[n]) 333 s = TPARM_9(s, 334 myParam(1), 335 myParam(2), 336 myParam(3), 337 myParam(4), 338 myParam(5), 339 myParam(6), 340 myParam(7), 341 myParam(8), 342 myParam(9)); 343 break; 344 } 345 } 346 347 /* use putp() in order to perform padding */ 348 putp(s); 349 return exit_code(STRING, 0); 350 } 351 return exit_code(STRING, 1); 352 } 353 354 int 355 main(int argc, char **argv) 356 { 357 char *term; 358 int errret; 359 bool cmdline = TRUE; 360 int c; 361 char buf[BUFSIZ]; 362 int result = 0; 363 364 check_aliases(prg_name = _nc_rootname(argv[0])); 365 366 term = getenv("TERM"); 367 368 while ((c = getopt(argc, argv, "ST:V")) != -1) { 369 switch (c) { 370 case 'S': 371 cmdline = FALSE; 372 break; 373 case 'T': 374 use_env(FALSE); 375 term = optarg; 376 break; 377 case 'V': 378 puts(curses_version()); 379 ExitProgram(EXIT_SUCCESS); 380 default: 381 usage(); 382 /* NOTREACHED */ 383 } 384 } 385 386 /* 387 * Modify the argument list to omit the options we processed. 388 */ 389 if (is_reset || is_init) { 390 if (optind-- < argc) { 391 argc -= optind; 392 argv += optind; 393 } 394 argv[0] = prg_name; 395 } else { 396 argc -= optind; 397 argv += optind; 398 } 399 400 if (term == 0 || *term == '\0') 401 quit(2, "No value for $TERM and no -T specified"); 402 403 if (setupterm(term, STDOUT_FILENO, &errret) != OK && errret <= 0) 404 quit(3, "unknown terminal \"%s\"", term); 405 406 if (cmdline) { 407 if ((argc <= 0) && !is_reset && !is_init) 408 usage(); 409 ExitProgram(tput(argc, argv)); 410 } 411 412 while (fgets(buf, sizeof(buf), stdin) != 0) { 413 char *argvec[16]; /* command, 9 parms, null, & slop */ 414 int argnum = 0; 415 char *cp; 416 417 /* crack the argument list into a dope vector */ 418 for (cp = buf; *cp; cp++) { 419 if (isspace(UChar(*cp))) { 420 *cp = '\0'; 421 } else if (cp == buf || cp[-1] == 0) { 422 argvec[argnum++] = cp; 423 if (argnum >= (int) SIZEOF(argvec) - 1) 424 break; 425 } 426 } 427 argvec[argnum] = 0; 428 429 if (argnum != 0 430 && tput(argnum, argvec) != 0) { 431 if (result == 0) 432 result = 4; /* will return value >4 */ 433 ++result; 434 } 435 } 436 437 ExitProgram(result); 438 } 439