1 /**************************************************************************** 2 * Copyright 2020 Thomas E. Dickey * 3 * Copyright 2008-2016,2017 Free Software Foundation, Inc. * 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: Thomas E. Dickey 2008 * 32 ****************************************************************************/ 33 34 /* 35 * tabs.c -- set terminal hard-tabstops 36 */ 37 38 #define USE_LIBTINFO 39 #include <progs.priv.h> 40 #include <tty_settings.h> 41 42 MODULE_ID("$Id: tabs.c,v 1.42 2020/02/02 23:34:34 tom Exp $") 43 44 static void usage(void) GCC_NORETURN; 45 46 const char *_nc_progname; 47 static int max_cols; 48 49 static void 50 failed(const char *s) 51 { 52 perror(s); 53 ExitProgram(EXIT_FAILURE); 54 } 55 56 static int 57 putch(int c) 58 { 59 return putchar(c); 60 } 61 62 static void 63 do_tabs(int *tab_list) 64 { 65 int last = 1; 66 int stop; 67 68 putchar('\r'); 69 while ((stop = *tab_list++) > 0) { 70 if (last < stop) { 71 while (last++ < stop) { 72 if (last > max_cols) 73 break; 74 putchar(' '); 75 } 76 } 77 if (stop <= max_cols) { 78 tputs(tparm(set_tab, stop), 1, putch); 79 last = stop; 80 } else { 81 break; 82 } 83 } 84 putchar('\r'); 85 } 86 87 static int * 88 decode_tabs(const char *tab_list) 89 { 90 int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols); 91 int n = 0; 92 int value = 0; 93 int prior = 0; 94 int ch; 95 96 if (result == 0) 97 failed("decode_tabs"); 98 99 while ((ch = *tab_list++) != '\0') { 100 if (isdigit(UChar(ch))) { 101 value *= 10; 102 value += (ch - '0'); 103 } else if (ch == ',') { 104 result[n] = value + prior; 105 if (n > 0 && result[n] <= result[n - 1]) { 106 fprintf(stderr, 107 "%s: tab-stops are not in increasing order: %d %d\n", 108 _nc_progname, value, result[n - 1]); 109 free(result); 110 result = 0; 111 break; 112 } 113 ++n; 114 value = 0; 115 prior = 0; 116 } else if (ch == '+') { 117 if (n) 118 prior = result[n - 1]; 119 } 120 } 121 122 if (result != 0) { 123 /* 124 * If there is only one value, then it is an option such as "-8". 125 */ 126 if ((n == 0) && (value > 0)) { 127 int step = value; 128 value = 1; 129 while (n < max_cols - 1) { 130 result[n++] = value; 131 value += step; 132 } 133 } 134 135 /* 136 * Add the last value, if any. 137 */ 138 result[n++] = value + prior; 139 result[n] = 0; 140 } 141 142 return result; 143 } 144 145 static void 146 print_ruler(int *tab_list) 147 { 148 int last = 0; 149 int stop; 150 int n; 151 152 /* first print a readable ruler */ 153 for (n = 0; n < max_cols; n += 10) { 154 int ch = 1 + (n / 10); 155 char buffer[20]; 156 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) 157 "----+----%c", 158 ((ch < 10) 159 ? (ch + '0') 160 : (ch + 'A' - 10))); 161 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer); 162 } 163 putchar('\n'); 164 165 /* now, print '*' for each stop */ 166 for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) { 167 stop = tab_list[n]; 168 while (++last < stop) { 169 if (last <= max_cols) { 170 putchar('-'); 171 } else { 172 break; 173 } 174 } 175 if (last <= max_cols) { 176 putchar('*'); 177 last = stop; 178 } else { 179 break; 180 } 181 } 182 while (++last <= max_cols) 183 putchar('-'); 184 putchar('\n'); 185 } 186 187 /* 188 * Write an '*' on each tabstop, to demonstrate whether it lines up with the 189 * ruler. 190 */ 191 static void 192 write_tabs(int *tab_list) 193 { 194 int stop; 195 196 while ((stop = *tab_list++) > 0 && stop <= max_cols) { 197 fputs((stop == 1) ? "*" : "\t*", stdout); 198 }; 199 /* also show a tab _past_ the stops */ 200 if (stop < max_cols) 201 fputs("\t+", stdout); 202 putchar('\n'); 203 } 204 205 /* 206 * Trim leading/trailing blanks, as well as blanks after a comma. 207 * Convert embedded blanks to commas. 208 */ 209 static char * 210 trimmed_tab_list(const char *source) 211 { 212 char *result = strdup(source); 213 int ch, j, k, last; 214 215 if (result != 0) { 216 for (j = k = last = 0; result[j] != 0; ++j) { 217 ch = UChar(result[j]); 218 if (isspace(ch)) { 219 if (last == '\0') { 220 continue; 221 } else if (isdigit(last) || last == ',') { 222 ch = ','; 223 } 224 } else if (ch == ',') { 225 ; 226 } else { 227 if (last == ',') 228 result[k++] = (char) last; 229 result[k++] = (char) ch; 230 } 231 last = ch; 232 } 233 result[k] = '\0'; 234 } 235 return result; 236 } 237 238 static bool 239 comma_is_needed(const char *source) 240 { 241 bool result = FALSE; 242 243 if (source != 0) { 244 size_t len = strlen(source); 245 if (len != 0) 246 result = (source[len - 1] != ','); 247 } else { 248 result = FALSE; 249 } 250 return result; 251 } 252 253 /* 254 * Add a command-line parameter to the tab-list. It can be blank- or comma- 255 * separated (or a mixture). For simplicity, empty tabs are ignored, e.g., 256 * tabs 1,,6,11 257 * tabs 1,6,11 258 * are treated the same. 259 */ 260 static const char * 261 add_to_tab_list(char **append, const char *value) 262 { 263 char *result = *append; 264 char *copied = trimmed_tab_list(value); 265 266 if (copied != 0 && *copied != '\0') { 267 const char *comma = ","; 268 size_t need = 1 + strlen(copied); 269 270 if (*copied == ',') 271 comma = ""; 272 else if (!comma_is_needed(*append)) 273 comma = ""; 274 275 need += strlen(comma); 276 if (*append != 0) 277 need += strlen(*append); 278 279 result = malloc(need); 280 if (result == 0) 281 failed("add_to_tab_list"); 282 283 *result = '\0'; 284 if (*append != 0) { 285 _nc_STRCPY(result, *append, need); 286 free(*append); 287 } 288 _nc_STRCAT(result, comma, need); 289 _nc_STRCAT(result, copied, need); 290 291 *append = result; 292 } 293 free(copied); 294 return result; 295 } 296 297 /* 298 * Check for illegal characters in the tab-list. 299 */ 300 static bool 301 legal_tab_list(const char *tab_list) 302 { 303 bool result = TRUE; 304 305 if (tab_list != 0 && *tab_list != '\0') { 306 if (comma_is_needed(tab_list)) { 307 int n, ch; 308 for (n = 0; tab_list[n] != '\0'; ++n) { 309 ch = UChar(tab_list[n]); 310 if (!(isdigit(ch) || ch == ',' || ch == '+')) { 311 fprintf(stderr, 312 "%s: unexpected character found '%c'\n", 313 _nc_progname, ch); 314 result = FALSE; 315 break; 316 } 317 } 318 } else { 319 fprintf(stderr, "%s: trailing comma found '%s'\n", _nc_progname, tab_list); 320 result = FALSE; 321 } 322 } else { 323 fprintf(stderr, "%s: no tab-list given\n", _nc_progname); 324 result = FALSE; 325 } 326 return result; 327 } 328 329 static char * 330 skip_list(char *value) 331 { 332 while (*value != '\0' && 333 (isdigit(UChar(*value)) || 334 isspace(UChar(*value)) || 335 strchr("+,", UChar(*value)) != 0)) { 336 ++value; 337 } 338 return value; 339 } 340 341 static void 342 usage(void) 343 { 344 #define DATA(s) s "\n" 345 static const char msg[] = 346 { 347 DATA("Usage: tabs [options] [tabstop-list]") 348 DATA("") 349 DATA("Options:") 350 DATA(" -0 reset tabs") 351 DATA(" -8 set tabs to standard interval") 352 DATA(" -a Assembler, IBM S/370, first format") 353 DATA(" -a2 Assembler, IBM S/370, second format") 354 DATA(" -c COBOL, normal format") 355 DATA(" -c2 COBOL compact format") 356 DATA(" -c3 COBOL compact format extended") 357 DATA(" -d debug (show ruler with expected/actual tab positions)") 358 DATA(" -f FORTRAN") 359 DATA(" -n no-op (do not modify terminal settings)") 360 DATA(" -p PL/I") 361 DATA(" -s SNOBOL") 362 DATA(" -u UNIVAC 1100 Assembler") 363 DATA(" -T name use terminal type 'name'") 364 DATA(" -V print version") 365 DATA("") 366 DATA("A tabstop-list is an ordered list of column numbers, e.g., 1,11,21") 367 DATA("or 1,+10,+10 which is the same.") 368 }; 369 #undef DATA 370 371 fflush(stdout); 372 fputs(msg, stderr); 373 ExitProgram(EXIT_FAILURE); 374 } 375 376 int 377 main(int argc, char *argv[]) 378 { 379 int rc = EXIT_FAILURE; 380 bool debug = FALSE; 381 bool no_op = FALSE; 382 int n, ch; 383 NCURSES_CONST char *term_name = 0; 384 char *append = 0; 385 const char *tab_list = 0; 386 TTY tty_settings; 387 int fd; 388 389 _nc_progname = _nc_rootname(argv[0]); 390 391 fd = save_tty_settings(&tty_settings, FALSE); 392 393 if ((term_name = getenv("TERM")) == 0) 394 term_name = "ansi+tabs"; 395 396 /* cannot use getopt, since some options are two-character */ 397 for (n = 1; n < argc; ++n) { 398 char *option = argv[n]; 399 switch (option[0]) { 400 case '-': 401 while ((ch = *++option) != '\0') { 402 switch (ch) { 403 case 'a': 404 switch (*++option) { 405 default: 406 case '\0': 407 tab_list = "1,10,16,36,72"; 408 option--; 409 /* Assembler, IBM S/370, first format */ 410 break; 411 case '2': 412 tab_list = "1,10,16,40,72"; 413 /* Assembler, IBM S/370, second format */ 414 break; 415 } 416 break; 417 case 'c': 418 switch (*++option) { 419 default: 420 case '\0': 421 tab_list = "1,8,12,16,20,55"; 422 option--; 423 /* COBOL, normal format */ 424 break; 425 case '2': 426 tab_list = "1,6,10,14,49"; 427 /* COBOL compact format */ 428 break; 429 case '3': 430 tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67"; 431 /* COBOL compact format extended */ 432 break; 433 } 434 break; 435 case 'd': /* ncurses extension */ 436 debug = TRUE; 437 break; 438 case 'f': 439 tab_list = "1,7,11,15,19,23"; 440 /* FORTRAN */ 441 break; 442 case 'n': /* ncurses extension */ 443 no_op = TRUE; 444 break; 445 case 'p': 446 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61"; 447 /* PL/I */ 448 break; 449 case 's': 450 tab_list = "1,10,55"; 451 /* SNOBOL */ 452 break; 453 case 'u': 454 tab_list = "1,12,20,44"; 455 /* UNIVAC 1100 Assembler */ 456 break; 457 case 'T': 458 ++n; 459 if (*++option != '\0') { 460 term_name = option; 461 } else { 462 term_name = argv[n]; 463 option--; 464 } 465 option += ((int) strlen(option)) - 1; 466 continue; 467 case 'V': 468 puts(curses_version()); 469 ExitProgram(EXIT_SUCCESS); 470 default: 471 if (isdigit(UChar(*option))) { 472 char *copy = strdup(option); 473 *skip_list(copy) = '\0'; 474 tab_list = copy; 475 option = skip_list(option) - 1; 476 } else { 477 usage(); 478 } 479 break; 480 } 481 } 482 break; 483 case '+': 484 while ((ch = *++option) != '\0') { 485 switch (ch) { 486 case 'm': 487 /* 488 * The "+mXXX" option is unimplemented because only the long-obsolete 489 * att510d implements smgl, which is needed to support 490 * this option. 491 */ 492 break; 493 default: 494 /* special case of relative stops separated by spaces? */ 495 if (option == argv[n] + 1) { 496 tab_list = add_to_tab_list(&append, argv[n]); 497 } 498 break; 499 } 500 } 501 break; 502 default: 503 if (append != 0) { 504 if (tab_list != (const char *) append) { 505 /* one of the predefined options was used */ 506 free(append); 507 append = 0; 508 } 509 } 510 tab_list = add_to_tab_list(&append, option); 511 break; 512 } 513 } 514 515 setupterm(term_name, fd, (int *) 0); 516 517 max_cols = (columns > 0) ? columns : 80; 518 519 if (!VALID_STRING(clear_all_tabs)) { 520 fprintf(stderr, 521 "%s: terminal type '%s' cannot reset tabs\n", 522 _nc_progname, term_name); 523 } else if (!VALID_STRING(set_tab)) { 524 fprintf(stderr, 525 "%s: terminal type '%s' cannot set tabs\n", 526 _nc_progname, term_name); 527 } else if (legal_tab_list(tab_list)) { 528 int *list = decode_tabs(tab_list); 529 530 if (!no_op) 531 tputs(clear_all_tabs, 1, putch); 532 533 if (list != 0) { 534 if (!no_op) 535 do_tabs(list); 536 if (debug) { 537 fflush(stderr); 538 printf("tabs %s\n", tab_list); 539 print_ruler(list); 540 write_tabs(list); 541 } 542 free(list); 543 } else if (debug) { 544 fflush(stderr); 545 printf("tabs %s\n", tab_list); 546 } 547 rc = EXIT_SUCCESS; 548 } 549 if (append != 0) 550 free(append); 551 ExitProgram(rc); 552 } 553