1 /****************************************************************************
2 * Copyright 2020,2021 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.47 2021/04/03 23:00:00 tom Exp $")
44 static GCC_NORETURN void usage(void);
46 const char *_nc_progname;
53 ExitProgram(EXIT_FAILURE);
63 do_tabs(int *tab_list)
69 while ((stop = *tab_list++) > 0) {
71 while (last++ < stop) {
77 if (stop <= max_cols) {
78 tputs(TIPARM_1(set_tab, stop), 1, putch);
88 decode_tabs(const char *tab_list)
90 int *result = typeCalloc(int, strlen(tab_list) + (unsigned) max_cols);
97 failed("decode_tabs");
99 while ((ch = *tab_list++) != '\0') {
100 if (isdigit(UChar(ch))) {
103 } else if (ch == ',') {
104 result[n] = value + prior;
105 if (n > 0 && result[n] <= result[n - 1]) {
107 "%s: tab-stops are not in increasing order: %d %d\n",
108 _nc_progname, value, result[n - 1]);
116 } else if (ch == '+') {
118 prior = result[n - 1];
124 * If there is only one value, then it is an option such as "-8".
126 if ((n == 0) && (value > 0)) {
129 while (n < max_cols - 1) {
136 * Add the last value, if any.
138 result[n++] = value + prior;
146 print_ruler(int *tab_list)
151 /* first print a readable ruler */
152 for (n = 0; n < max_cols; n += 10) {
153 int ch = 1 + (n / 10);
155 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
160 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
164 /* now, print '*' for each stop */
165 for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) {
166 int stop = tab_list[n];
168 while (++last < stop) {
169 if (last <= max_cols) {
175 if (last <= max_cols) {
182 while (++last <= max_cols)
188 * Write an '*' on each tabstop, to demonstrate whether it lines up with the
192 write_tabs(int *tab_list)
196 while ((stop = *tab_list++) > 0 && stop <= max_cols) {
197 fputs((stop == 1) ? "*" : "\t*", stdout);
199 /* also show a tab _past_ the stops */
201 fputs("\t+", stdout);
206 * Trim leading/trailing blanks, as well as blanks after a comma.
207 * Convert embedded blanks to commas.
210 trimmed_tab_list(const char *source)
212 char *result = strdup(source);
216 for (j = k = last = 0; result[j] != 0; ++j) {
217 int ch = UChar(result[j]);
221 } else if (isdigit(last) || last == ',') {
224 } else if (ch == ',') {
228 result[k++] = (char) last;
229 result[k++] = (char) ch;
239 comma_is_needed(const char *source)
244 size_t len = strlen(source);
246 result = (source[len - 1] != ',');
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.,
258 * are treated the same.
261 add_to_tab_list(char **append, const char *value)
263 char *result = *append;
264 char *copied = trimmed_tab_list(value);
266 if (copied != 0 && *copied != '\0') {
267 const char *comma = ",";
268 size_t need = 1 + strlen(copied);
272 else if (!comma_is_needed(*append))
275 need += strlen(comma);
277 need += strlen(*append);
279 result = malloc(need);
281 failed("add_to_tab_list");
285 _nc_STRCPY(result, *append, need);
288 _nc_STRCAT(result, comma, need);
289 _nc_STRCAT(result, copied, need);
298 * Check for illegal characters in the tab-list.
301 legal_tab_list(const char *tab_list)
305 if (tab_list != 0 && *tab_list != '\0') {
306 if (comma_is_needed(tab_list)) {
309 for (n = 0; tab_list[n] != '\0'; ++n) {
310 int ch = UChar(tab_list[n]);
312 if (!(isdigit(ch) || ch == ',' || ch == '+')) {
314 "%s: unexpected character found '%c'\n",
321 fprintf(stderr, "%s: trailing comma found '%s'\n", _nc_progname, tab_list);
325 fprintf(stderr, "%s: no tab-list given\n", _nc_progname);
332 skip_list(char *value)
334 while (*value != '\0' &&
335 (isdigit(UChar(*value)) ||
336 isspace(UChar(*value)) ||
337 strchr("+,", UChar(*value)) != 0)) {
346 #define DATA(s) s "\n"
347 static const char msg[] =
349 DATA("Usage: tabs [options] [tabstop-list]")
352 DATA(" -0 reset tabs")
353 DATA(" -8 set tabs to standard interval")
354 DATA(" -a Assembler, IBM S/370, first format")
355 DATA(" -a2 Assembler, IBM S/370, second format")
356 DATA(" -c COBOL, normal format")
357 DATA(" -c2 COBOL compact format")
358 DATA(" -c3 COBOL compact format extended")
359 DATA(" -d debug (show ruler with expected/actual tab positions)")
361 DATA(" -n no-op (do not modify terminal settings)")
364 DATA(" -u UNIVAC 1100 Assembler")
365 DATA(" -T name use terminal type 'name'")
366 DATA(" -V print version")
368 DATA("A tabstop-list is an ordered list of column numbers, e.g., 1,11,21")
369 DATA("or 1,+10,+10 which is the same.")
375 ExitProgram(EXIT_FAILURE);
379 main(int argc, char *argv[])
381 int rc = EXIT_FAILURE;
385 NCURSES_CONST char *term_name = 0;
387 const char *tab_list = 0;
391 _nc_progname = _nc_rootname(argv[0]);
393 fd = save_tty_settings(&tty_settings, FALSE);
395 if ((term_name = getenv("TERM")) == 0)
396 term_name = "ansi+tabs";
398 /* cannot use getopt, since some options are two-character */
399 for (n = 1; n < argc; ++n) {
400 char *option = argv[n];
403 while ((ch = *++option) != '\0') {
409 tab_list = "1,10,16,36,72";
411 /* Assembler, IBM S/370, first format */
414 tab_list = "1,10,16,40,72";
415 /* Assembler, IBM S/370, second format */
423 tab_list = "1,8,12,16,20,55";
425 /* COBOL, normal format */
428 tab_list = "1,6,10,14,49";
429 /* COBOL compact format */
432 tab_list = "1,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,67";
433 /* COBOL compact format extended */
437 case 'd': /* ncurses extension */
441 tab_list = "1,7,11,15,19,23";
444 case 'n': /* ncurses extension */
448 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
452 tab_list = "1,10,55";
456 tab_list = "1,12,20,44";
457 /* UNIVAC 1100 Assembler */
461 if (*++option != '\0') {
467 option += ((int) strlen(option)) - 1;
470 puts(curses_version());
471 ExitProgram(EXIT_SUCCESS);
473 if (isdigit(UChar(*option))) {
474 char *copy = strdup(option);
475 *skip_list(copy) = '\0';
477 option = skip_list(option) - 1;
486 while ((ch = *++option) != '\0') {
490 * The "+mXXX" option is unimplemented because only the long-obsolete
491 * att510d implements smgl, which is needed to support
496 /* special case of relative stops separated by spaces? */
497 if (option == argv[n] + 1) {
498 tab_list = add_to_tab_list(&append, argv[n]);
506 if (tab_list != (const char *) append) {
507 /* one of the predefined options was used */
512 tab_list = add_to_tab_list(&append, option);
517 setupterm(term_name, fd, (int *) 0);
519 max_cols = (columns > 0) ? columns : 80;
521 if (!VALID_STRING(clear_all_tabs)) {
523 "%s: terminal type '%s' cannot reset tabs\n",
524 _nc_progname, term_name);
525 } else if (!VALID_STRING(set_tab)) {
527 "%s: terminal type '%s' cannot set tabs\n",
528 _nc_progname, term_name);
529 } else if (legal_tab_list(tab_list)) {
530 int *list = decode_tabs(tab_list);
533 tputs(clear_all_tabs, 1, putch);
540 printf("tabs %s\n", tab_list);
547 printf("tabs %s\n", tab_list);