1 /****************************************************************************
2 * Copyright 2020-2022,2023 Thomas E. Dickey *
3 * Copyright 2008-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: Thomas E. Dickey 2008 *
32 ****************************************************************************/
35 * tabs.c -- set terminal hard-tabstops
39 #include <progs.priv.h>
40 #include <tty_settings.h>
42 MODULE_ID("$Id: tabs.c,v 1.52 2023/05/27 20:13:10 tom Exp $")
44 static GCC_NORETURN void usage(void);
46 const char *_nc_progname;
53 ExitProgram(EXIT_FAILURE);
65 if (UChar(*value) == 0x9b)
67 else if (!strncmp(value, "\033[", 2))
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.
80 if (VALID_STRING(clear_all_tabs)) {
81 char *param = skip_csi(clear_all_tabs);
82 if (!strcmp(param, "3g"))
89 do_tabs(int *tab_list)
95 while ((stop = *tab_list++) > 0) {
101 while (last++ < stop) {
107 if (stop <= max_cols) {
108 tputs(set_tab, 1, putch);
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.
123 decode_tabs(const char *tab_list, int margin)
125 int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols);
132 failed("decode_tabs");
137 while ((ch = *tab_list++) != '\0') {
138 if (isdigit(UChar(ch))) {
141 if (value > max_cols)
143 } else if (ch == ',') {
144 result[n] = value + prior + margin;
145 if (n > 0 && result[n] <= result[n - 1]) {
147 "%s: tab-stops are not in increasing order: %d %d\n",
148 _nc_progname, value, result[n - 1]);
156 } else if (ch == '+') {
158 prior = result[n - 1];
164 * If there is only one value, then it is an option such as "-8".
166 if ((n == 0) && (value > 0)) {
169 while (n < max_cols - 1) {
170 result[n++] = value + margin;
176 * Add the last value, if any.
178 result[n++] = value + prior + margin;
186 print_ruler(const int *const tab_list, const char *new_line)
191 /* first print a readable ruler */
192 for (n = 0; n < max_cols; n += 10) {
193 int ch = 1 + (n / 10);
195 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
200 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
202 printf("%s", new_line);
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];
208 while (++last < stop) {
209 if (last <= max_cols) {
215 if (last <= max_cols) {
222 while (++last <= max_cols)
224 printf("%s", new_line);
228 * Write an '*' on each tabstop, to demonstrate whether it lines up with the
232 write_tabs(int *tab_list, const char *new_line)
236 while ((stop = *tab_list++) > 0 && stop <= max_cols) {
237 fputs((stop == 1) ? "*" : "\t*", stdout);
239 /* also show a tab _past_ the stops */
241 fputs("\t+", stdout);
242 fputs(new_line, stdout);
246 * Trim leading/trailing blanks, as well as blanks after a comma.
247 * Convert embedded blanks to commas.
250 trimmed_tab_list(const char *source)
252 char *result = strdup(source);
256 for (j = k = last = 0; result[j] != 0; ++j) {
257 int ch = UChar(result[j]);
261 } else if (isdigit(last) || last == ',') {
264 } else if (ch == ',') {
268 result[k++] = (char) last;
269 result[k++] = (char) ch;
279 comma_is_needed(const char *source)
284 size_t len = strlen(source);
286 result = (source[len - 1] != ',');
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.,
298 * are treated the same.
301 add_to_tab_list(char **append, const char *value)
303 char *result = *append;
304 char *copied = trimmed_tab_list(value);
306 if (copied != 0 && *copied != '\0') {
307 const char *comma = ",";
308 size_t need = 1 + strlen(copied);
312 else if (!comma_is_needed(*append))
315 need += strlen(comma);
317 need += strlen(*append);
319 result = malloc(need);
321 failed("add_to_tab_list");
325 _nc_STRCPY(result, *append, need);
328 _nc_STRCAT(result, comma, need);
329 _nc_STRCAT(result, copied, need);
338 * If the terminal supports it, (re)set the left margin and return true.
339 * Otherwise, return false.
342 do_set_margin(int margin, bool no_op)
346 if (margin == 0) { /* 0 is special case for resetting */
347 if (VALID_STRING(clear_margins)) {
350 tputs(clear_margins, 1, putch);
352 } else if (margin-- < 0) { /* margin will be 0-based from here on */
354 } else if (VALID_STRING(set_left_margin)) {
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.
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);
371 tputs(set_left_margin, 1, putch);
373 } else if (VALID_STRING(set_left_margin_parm)) {
376 if (VALID_STRING(set_right_margin_parm)) {
377 tputs(TIPARM_1(set_left_margin_parm, margin), 1, putch);
379 tputs(TIPARM_2(set_left_margin_parm, margin, max_cols), 1, putch);
382 } else if (VALID_STRING(set_lr_margin)) {
385 tputs(TIPARM_2(set_lr_margin, margin, max_cols), 1, putch);
392 * Check for illegal characters in the tab-list.
395 legal_tab_list(const char *tab_list)
399 if (tab_list != 0 && *tab_list != '\0') {
400 if (comma_is_needed(tab_list)) {
403 for (n = 0; tab_list[n] != '\0'; ++n) {
404 int ch = UChar(tab_list[n]);
406 if (!(isdigit(ch) || ch == ',' || ch == '+')) {
408 "%s: unexpected character found '%c'\n",
415 fprintf(stderr, "%s: trailing comma found '%s'\n", _nc_progname, tab_list);
419 /* if no list given, default to "tabs -8" */
425 skip_list(char *value)
427 while (*value != '\0' &&
428 (isdigit(UChar(*value)) ||
429 isspace(UChar(*value)) ||
430 strchr("+,", UChar(*value)) != 0)) {
439 #define DATA(s) s "\n"
440 static const char msg[] =
442 DATA("Usage: tabs [options] [tabstop-list]")
445 DATA(" -0 reset tabs")
446 DATA(" -8 set tabs to standard interval")
447 DATA(" -a Assembler, IBM S/370, first format")
448 DATA(" -a2 Assembler, IBM S/370, second format")
449 DATA(" -c COBOL, normal format")
450 DATA(" -c2 COBOL compact format")
451 DATA(" -c3 COBOL compact format extended")
452 DATA(" -d debug (show ruler with expected/actual tab positions)")
454 DATA(" -n no-op (do not modify terminal settings)")
457 DATA(" -u UNIVAC 1100 Assembler")
458 DATA(" -T name use terminal type 'name'")
459 DATA(" -V print version")
461 DATA("A tabstop-list is an ordered list of column numbers, e.g., 1,11,21")
462 DATA("or 1,+10,+10 which is the same.")
468 ExitProgram(EXIT_FAILURE);
472 main(int argc, char *argv[])
474 int rc = EXIT_FAILURE;
477 bool change_tty = FALSE;
479 NCURSES_CONST char *term_name = 0;
481 const char *tab_list = 0;
482 const char *new_line = "\n";
487 _nc_progname = _nc_rootname(argv[0]);
489 if ((term_name = getenv("TERM")) == 0)
490 term_name = "ansi+tabs";
492 /* cannot use getopt, since some options are two-character */
493 for (n = 1; n < argc; ++n) {
494 char *option = argv[n];
497 while ((ch = *++option) != '\0') {
503 tab_list = "1,10,16,36,72";
505 /* Assembler, IBM S/370, first format */
508 tab_list = "1,10,16,40,72";
509 /* Assembler, IBM S/370, second format */
517 tab_list = "1,8,12,16,20,55";
519 /* COBOL, normal format */
522 tab_list = "1,6,10,14,49";
523 /* COBOL compact format */
526 tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67";
527 /* COBOL compact format extended */
531 case 'd': /* ncurses extension */
535 tab_list = "1,7,11,15,19,23";
538 case 'n': /* ncurses extension */
542 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
546 tab_list = "1,10,55";
550 tab_list = "1,12,20,44";
551 /* UNIVAC 1100 Assembler */
555 if (*++option != '\0') {
561 option += ((int) strlen(option)) - 1;
564 puts(curses_version());
565 ExitProgram(EXIT_SUCCESS);
567 if (isdigit(UChar(*option))) {
568 char *copy = strdup(option);
569 *skip_list(copy) = '\0';
571 option = skip_list(option) - 1;
580 if ((ch = *++option) != '\0') {
587 * The "+mXXX" option is unimplemented because only the long-obsolete
588 * att510d implements smgl, which is needed to support
591 while ((ch = *++option) != '\0') {
592 if (isdigit(UChar(ch))) {
594 number = number * 10 + (ch - '0');
604 /* special case of relative stops separated by spaces? */
605 if (option == argv[n] + 1) {
606 tab_list = add_to_tab_list(&append, argv[n]);
614 if (tab_list != (const char *) append) {
615 /* one of the predefined options was used */
620 tab_list = add_to_tab_list(&append, option);
625 fd = save_tty_settings(&tty_settings, FALSE);
627 setupterm(term_name, fd, (int *) 0);
629 max_cols = (columns > 0) ? columns : 80;
633 if (!VALID_STRING(clear_all_tabs)) {
635 "%s: terminal type '%s' cannot reset tabs\n",
636 _nc_progname, term_name);
637 } else if (!VALID_STRING(set_tab)) {
639 "%s: terminal type '%s' cannot set tabs\n",
640 _nc_progname, term_name);
641 } else if (legal_tab_list(tab_list)) {
644 if (tab_list == NULL)
645 tab_list = add_to_tab_list(&append, "8");
648 #if defined(TERMIOS) && defined(OCRNL)
649 /* set tty modes to -ocrnl to allow \r */
650 if (isatty(STDOUT_FILENO)) {
651 TTY new_settings = tty_settings;
652 new_settings.c_oflag &= (unsigned) ~OCRNL;
653 update_tty_settings(&tty_settings, &new_settings);
659 if (!ansi_clear_tabs())
661 tputs(clear_all_tabs, 1, putch);
667 /* reset existing margin before setting margin, to reduce
668 * problems moving left of the current margin.
670 if (do_set_margin(0, no_op))
673 if (do_set_margin(margin, no_op))
677 list = decode_tabs(tab_list, margin);
684 printf("tabs %s%s", tab_list, new_line);
685 print_ruler(list, new_line);
686 write_tabs(list, new_line);
691 printf("tabs %s%s", tab_list, new_line);
695 restore_tty_settings();