1 /**************************************************************************** 2 * Copyright 2020-2023,2024 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.56 2024/12/07 22:10:45 tom Exp $") 43 44 static GCC_NORETURN void usage(void); 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 char * 63 skip_csi(char *value) 64 { 65 if (UChar(*value) == 0x9b) 66 ++value; 67 else if (!strncmp(value, "\033[", 2)) 68 value += 2; 69 return value; 70 } 71 72 /* 73 * If the terminal uses ANSI clear_all_tabs, then it is not necessary to first 74 * move to the left margin before clearing tabs. 75 */ 76 static bool 77 ansi_clear_tabs(void) 78 { 79 bool result = FALSE; 80 if (VALID_STRING(clear_all_tabs)) { 81 const char *param = skip_csi(clear_all_tabs); 82 if (!strcmp(param, "3g")) 83 result = TRUE; 84 } 85 return result; 86 } 87 88 static void 89 do_tabs(const int *tab_list) 90 { 91 int last = 1; 92 int stop; 93 bool first = TRUE; 94 95 while ((stop = *tab_list++) > 0) { 96 if (first) { 97 first = FALSE; 98 putchar('\r'); 99 } 100 if (last < stop) { 101 while (last++ < stop) { 102 if (last > max_cols) 103 break; 104 putchar(' '); 105 } 106 } 107 if (stop <= max_cols) { 108 tputs(set_tab, 1, putch); 109 last = stop; 110 } else { 111 break; 112 } 113 } 114 putchar('\r'); 115 } 116 117 /* 118 * Decode a list of tab-stops from a string, returning an array of integers. 119 * If the margin is positive (because the terminal does not support margins), 120 * work around this by adding the margin to the decoded values. 121 */ 122 static int * 123 decode_tabs(const char *tab_list, int margin) 124 { 125 int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols); 126 int n = 0; 127 int value = 0; 128 int prior = 0; 129 int ch; 130 131 if (result == NULL) 132 failed("decode_tabs"); 133 134 if (margin < 0) 135 margin = 0; 136 137 while ((ch = *tab_list++) != '\0') { 138 if (isdigit(UChar(ch))) { 139 value *= 10; 140 value += (ch - '0'); 141 if (value > max_cols) 142 value = max_cols; 143 } else if (ch == ',') { 144 result[n] = value + prior + margin; 145 if (n > 0 && result[n] <= result[n - 1]) { 146 fprintf(stderr, 147 "%s: tab-stops are not in increasing order: %d %d\n", 148 _nc_progname, value, result[n - 1]); 149 free(result); 150 result = NULL; 151 break; 152 } 153 ++n; 154 value = 0; 155 prior = 0; 156 } else if (ch == '+') { 157 if (n) 158 prior = result[n - 1]; 159 } 160 } 161 162 if (result != NULL) { 163 /* 164 * If there is only one value, then it is an option such as "-8". 165 */ 166 if ((n == 0) && (value > 0)) { 167 int step = value; 168 value = 1; 169 while (n < max_cols - 1) { 170 result[n++] = value + margin; 171 value += step; 172 } 173 } 174 175 /* 176 * Add the last value, if any. 177 */ 178 result[n++] = value + prior + margin; 179 result[n] = 0; 180 } 181 182 return result; 183 } 184 185 static void 186 print_ruler(const int *const tab_list, const char *new_line) 187 { 188 int last = 0; 189 int n; 190 191 /* first print a readable ruler */ 192 for (n = 0; n < max_cols; n += 10) { 193 int ch = 1 + (n / 10); 194 char buffer[20]; 195 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) 196 "----+----%c", 197 ((ch < 10) 198 ? (ch + '0') 199 : (ch + 'A' - 10))); 200 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer); 201 } 202 printf("%s", new_line); 203 204 /* now, print '*' for each stop */ 205 for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) { 206 int stop = tab_list[n]; 207 208 while (++last < stop) { 209 if (last <= max_cols) { 210 putchar('-'); 211 } else { 212 break; 213 } 214 } 215 if (last <= max_cols) { 216 putchar('*'); 217 last = stop; 218 } else { 219 break; 220 } 221 } 222 while (++last <= max_cols) 223 putchar('-'); 224 printf("%s", new_line); 225 } 226 227 /* 228 * Write an '*' on each tabstop, to demonstrate whether it lines up with the 229 * ruler. 230 */ 231 static void 232 write_tabs(const int *tab_list, const char *new_line) 233 { 234 int stop; 235 236 while ((stop = *tab_list++) > 0 && stop <= max_cols) { 237 fputs((stop == 1) ? "*" : "\t*", stdout); 238 }; 239 /* also show a tab _past_ the stops */ 240 if (stop < max_cols) 241 fputs("\t+", stdout); 242 fputs(new_line, stdout); 243 } 244 245 /* 246 * Trim leading/trailing blanks, as well as blanks after a comma. 247 * Convert embedded blanks to commas. 248 */ 249 static char * 250 trimmed_tab_list(const char *source) 251 { 252 char *result = strdup(source); 253 if (result != NULL) { 254 int j, k, last; 255 256 for (j = k = last = 0; result[j] != 0; ++j) { 257 int ch = UChar(result[j]); 258 if (isspace(ch)) { 259 if (last == '\0') { 260 continue; 261 } else if (isdigit(last) || last == ',') { 262 ch = ','; 263 } 264 } else if (ch == ',') { 265 ; 266 } else { 267 if (last == ',') 268 result[k++] = (char) last; 269 result[k++] = (char) ch; 270 } 271 last = ch; 272 } 273 result[k] = '\0'; 274 } 275 return result; 276 } 277 278 static bool 279 comma_is_needed(const char *source) 280 { 281 bool result = FALSE; 282 283 if (source != NULL) { 284 size_t len = strlen(source); 285 if (len != 0) 286 result = (source[len - 1] != ','); 287 } else { 288 result = FALSE; 289 } 290 return result; 291 } 292 293 /* 294 * Add a command-line parameter to the tab-list. It can be blank- or comma- 295 * separated (or a mixture). For simplicity, empty tabs are ignored, e.g., 296 * tabs 1,,6,11 297 * tabs 1,6,11 298 * are treated the same. 299 */ 300 static const char * 301 add_to_tab_list(char **append, const char *value) 302 { 303 char *result = *append; 304 char *copied = trimmed_tab_list(value); 305 306 if (copied != NULL && *copied != '\0') { 307 const char *comma = ","; 308 size_t need = 1 + strlen(copied); 309 310 if (*copied == ',') 311 comma = ""; 312 else if (!comma_is_needed(*append)) 313 comma = ""; 314 315 need += strlen(comma); 316 if (*append != NULL) 317 need += strlen(*append); 318 319 result = malloc(need); 320 if (result == NULL) 321 failed("add_to_tab_list"); 322 323 *result = '\0'; 324 if (*append != NULL) { 325 _nc_STRCPY(result, *append, need); 326 free(*append); 327 } 328 _nc_STRCAT(result, comma, need); 329 _nc_STRCAT(result, copied, need); 330 331 *append = result; 332 } 333 free(copied); 334 return result; 335 } 336 337 /* 338 * If the terminal supports it, (re)set the left margin and return true. 339 * Otherwise, return false. 340 */ 341 static bool 342 do_set_margin(int margin, bool no_op) 343 { 344 bool result = FALSE; 345 346 if (margin == 0) { /* 0 is special case for resetting */ 347 if (VALID_STRING(clear_margins)) { 348 result = TRUE; 349 if (!no_op) 350 tputs(clear_margins, 1, putch); 351 } 352 } else if (margin-- < 0) { /* margin will be 0-based from here on */ 353 result = TRUE; 354 } else if (VALID_STRING(set_left_margin)) { 355 result = TRUE; 356 if (!no_op) { 357 /* 358 * assuming we're on the first column of the line, move the cursor 359 * to the column at which we will set a margin. 360 */ 361 if (VALID_STRING(column_address)) { 362 tputs(TIPARM_1(column_address, margin), 1, putch); 363 } else if (margin >= 1) { 364 if (VALID_STRING(parm_right_cursor)) { 365 tputs(TIPARM_1(parm_right_cursor, margin), 1, putch); 366 } else { 367 while (margin-- > 0) 368 putch(' '); 369 } 370 } 371 tputs(set_left_margin, 1, putch); 372 } 373 } 374 #if defined(set_left_margin_parm) && defined(set_right_margin_parm) 375 else if (VALID_STRING(set_left_margin_parm)) { 376 result = TRUE; 377 if (!no_op) { 378 if (VALID_STRING(set_right_margin_parm)) { 379 tputs(TIPARM_1(set_left_margin_parm, margin), 1, putch); 380 } else { 381 tputs(TIPARM_2(set_left_margin_parm, margin, max_cols), 1, putch); 382 } 383 } 384 } 385 #endif 386 #if defined(set_lr_margin) 387 else if (VALID_STRING(set_lr_margin)) { 388 result = TRUE; 389 if (!no_op) { 390 tputs(TIPARM_2(set_lr_margin, margin, max_cols), 1, putch); 391 } 392 } 393 #endif 394 return result; 395 } 396 397 /* 398 * Check for illegal characters in the tab-list. 399 */ 400 static bool 401 legal_tab_list(const char *tab_list) 402 { 403 bool result = TRUE; 404 405 if (tab_list != NULL && *tab_list != '\0') { 406 if (comma_is_needed(tab_list)) { 407 int n; 408 409 for (n = 0; tab_list[n] != '\0'; ++n) { 410 int ch = UChar(tab_list[n]); 411 412 if (!(isdigit(ch) || ch == ',' || ch == '+')) { 413 fprintf(stderr, 414 "%s: unexpected character found '%c'\n", 415 _nc_progname, ch); 416 result = FALSE; 417 break; 418 } 419 } 420 } else { 421 fprintf(stderr, "%s: trailing comma found '%s'\n", _nc_progname, tab_list); 422 result = FALSE; 423 } 424 } else { 425 /* if no list given, default to "tabs -8" */ 426 } 427 return result; 428 } 429 430 static char * 431 skip_list(char *value) 432 { 433 while (*value != '\0' && 434 (isdigit(UChar(*value)) || 435 isspace(UChar(*value)) || 436 strchr("+,", UChar(*value)) != NULL)) { 437 ++value; 438 } 439 return value; 440 } 441 442 static void 443 usage(void) 444 { 445 #define DATA(s) s "\n" 446 static const char msg[] = 447 { 448 DATA("") 449 DATA("Options:") 450 DATA(" -0 reset tabs") 451 DATA(" -8 set tabs to standard interval") 452 DATA(" -a Assembler, IBM S/370, first format") 453 DATA(" -a2 Assembler, IBM S/370, second format") 454 DATA(" -c COBOL, normal format") 455 DATA(" -c2 COBOL compact format") 456 DATA(" -c3 COBOL compact format extended") 457 DATA(" -d debug (show ruler with expected/actual tab positions)") 458 DATA(" -f FORTRAN") 459 DATA(" -n no-op (do not modify terminal settings)") 460 DATA(" -p PL/I") 461 DATA(" -s SNOBOL") 462 DATA(" -u UNIVAC 1100 Assembler") 463 DATA(" -T name use terminal type 'name'") 464 DATA(" -V print version") 465 DATA("") 466 DATA("A tabstop-list is an ordered list of column numbers, e.g., 1,11,21") 467 DATA("or 1,+10,+10 which is the same.") 468 }; 469 #undef DATA 470 FILE *fp = stderr; 471 472 fflush(stdout); 473 fprintf(fp, "Usage: %s [options] [tabstop-list]\n", _nc_progname); 474 fputs(msg, fp); 475 ExitProgram(EXIT_FAILURE); 476 } 477 478 int 479 main(int argc, char *argv[]) 480 { 481 int rc = EXIT_FAILURE; 482 bool debug = FALSE; 483 bool no_op = FALSE; 484 bool change_tty = FALSE; 485 int n, ch; 486 NCURSES_CONST char *term_name = NULL; 487 char *append = NULL; 488 const char *tab_list = NULL; 489 const char *new_line = "\n"; 490 int margin = -1; 491 TTY tty_settings; 492 int fd; 493 494 _nc_progname = _nc_rootname(argv[0]); 495 496 if ((term_name = getenv("TERM")) == NULL) 497 term_name = "ansi+tabs"; 498 499 /* cannot use getopt, since some options are two-character */ 500 for (n = 1; n < argc; ++n) { 501 char *option = argv[n]; 502 switch (option[0]) { 503 case '-': 504 while ((ch = *++option) != '\0') { 505 switch (ch) { 506 case 'a': 507 switch (*++option) { 508 default: 509 case '\0': 510 tab_list = "1,10,16,36,72"; 511 option--; 512 /* Assembler, IBM S/370, first format */ 513 break; 514 case '2': 515 tab_list = "1,10,16,40,72"; 516 /* Assembler, IBM S/370, second format */ 517 break; 518 } 519 break; 520 case 'c': 521 switch (*++option) { 522 default: 523 case '\0': 524 tab_list = "1,8,12,16,20,55"; 525 option--; 526 /* COBOL, normal format */ 527 break; 528 case '2': 529 tab_list = "1,6,10,14,49"; 530 /* COBOL compact format */ 531 break; 532 case '3': 533 tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67"; 534 /* COBOL compact format extended */ 535 break; 536 } 537 break; 538 case 'd': /* ncurses extension */ 539 debug = TRUE; 540 break; 541 case 'f': 542 tab_list = "1,7,11,15,19,23"; 543 /* FORTRAN */ 544 break; 545 case 'n': /* ncurses extension */ 546 no_op = TRUE; 547 break; 548 case 'p': 549 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61"; 550 /* PL/I */ 551 break; 552 case 's': 553 tab_list = "1,10,55"; 554 /* SNOBOL */ 555 break; 556 case 'u': 557 tab_list = "1,12,20,44"; 558 /* UNIVAC 1100 Assembler */ 559 break; 560 case 'T': 561 ++n; 562 if (*++option != '\0') { 563 term_name = option; 564 } else { 565 term_name = argv[n]; 566 option--; 567 } 568 option += ((int) strlen(option)) - 1; 569 continue; 570 case 'V': 571 puts(curses_version()); 572 ExitProgram(EXIT_SUCCESS); 573 default: 574 if (isdigit(UChar(*option))) { 575 char *copy = strdup(option); 576 *skip_list(copy) = '\0'; 577 tab_list = copy; 578 option = skip_list(option) - 1; 579 } else { 580 usage(); 581 } 582 break; 583 } 584 } 585 break; 586 case '+': 587 if ((ch = *++option) != '\0') { 588 int digits = 0; 589 int number = 0; 590 591 switch (ch) { 592 case 'm': 593 /* 594 * The "+mXXX" option is unimplemented because only the long-obsolete 595 * att510d implements smgl, which is needed to support 596 * this option. 597 */ 598 while ((ch = *++option) != '\0') { 599 if (isdigit(UChar(ch))) { 600 ++digits; 601 number = number * 10 + (ch - '0'); 602 } else { 603 usage(); 604 } 605 } 606 if (digits == 0) 607 number = 10; 608 margin = number; 609 break; 610 default: 611 /* special case of relative stops separated by spaces? */ 612 if (option == argv[n] + 1) { 613 tab_list = add_to_tab_list(&append, argv[n]); 614 } 615 break; 616 } 617 } 618 break; 619 default: 620 if (append != NULL) { 621 if (tab_list != (const char *) append) { 622 /* one of the predefined options was used */ 623 free(append); 624 append = NULL; 625 } 626 } 627 tab_list = add_to_tab_list(&append, option); 628 break; 629 } 630 } 631 632 fd = save_tty_settings(&tty_settings, FALSE); 633 634 setupterm(term_name, fd, (int *) 0); 635 636 max_cols = (columns > 0) ? columns : 80; 637 if (margin > 0) 638 max_cols -= margin; 639 640 if (!VALID_STRING(clear_all_tabs)) { 641 fprintf(stderr, 642 "%s: terminal type '%s' cannot reset tabs\n", 643 _nc_progname, term_name); 644 } else if (!VALID_STRING(set_tab)) { 645 fprintf(stderr, 646 "%s: terminal type '%s' cannot set tabs\n", 647 _nc_progname, term_name); 648 } else if (legal_tab_list(tab_list)) { 649 int *list; 650 651 if (tab_list == NULL) 652 tab_list = add_to_tab_list(&append, "8"); 653 654 if (!no_op) { 655 #if defined(TERMIOS) && defined(OCRNL) 656 /* set tty modes to -ocrnl to allow \r */ 657 if (isatty(STDOUT_FILENO)) { 658 TTY new_settings = tty_settings; 659 new_settings.c_oflag &= (unsigned) ~OCRNL; 660 update_tty_settings(&tty_settings, &new_settings); 661 change_tty = TRUE; 662 new_line = "\r\n"; 663 } 664 #endif 665 666 if (!ansi_clear_tabs()) 667 putch('\r'); 668 tputs(clear_all_tabs, 1, putch); 669 } 670 671 if (margin >= 0) { 672 putch('\r'); 673 if (margin > 0) { 674 /* reset existing margin before setting margin, to reduce 675 * problems moving left of the current margin. 676 */ 677 if (do_set_margin(0, no_op)) 678 putch('\r'); 679 } 680 if (do_set_margin(margin, no_op)) 681 margin = -1; 682 } 683 684 list = decode_tabs(tab_list, margin); 685 686 if (list != NULL) { 687 if (!no_op) 688 do_tabs(list); 689 if (debug) { 690 fflush(stderr); 691 printf("tabs %s%s", tab_list, new_line); 692 print_ruler(list, new_line); 693 write_tabs(list, new_line); 694 } 695 free(list); 696 } else if (debug) { 697 fflush(stderr); 698 printf("tabs %s%s", tab_list, new_line); 699 } 700 if (!no_op) { 701 if (change_tty) { 702 restore_tty_settings(); 703 } 704 } 705 rc = EXIT_SUCCESS; 706 } 707 if (append != NULL) 708 free(append); 709 ExitProgram(rc); 710 } 711