1 /****************************************************************************
2 * Copyright 2020 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.45 2020/05/27 23:47:22 tom Exp $")
44 static void usage(void) GCC_NORETURN;
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)
152 /* first print a readable ruler */
153 for (n = 0; n < max_cols; n += 10) {
154 int ch = 1 + (n / 10);
156 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
161 printf("%.*s", ((max_cols - n) > 10) ? 10 : (max_cols - n), buffer);
165 /* now, print '*' for each stop */
166 for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++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 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)) {
308 for (n = 0; tab_list[n] != '\0'; ++n) {
309 ch = UChar(tab_list[n]);
310 if (!(isdigit(ch) || ch == ',' || ch == '+')) {
312 "%s: unexpected character found '%c'\n",
319 fprintf(stderr, "%s: trailing comma found '%s'\n", _nc_progname, tab_list);
323 fprintf(stderr, "%s: no tab-list given\n", _nc_progname);
330 skip_list(char *value)
332 while (*value != '\0' &&
333 (isdigit(UChar(*value)) ||
334 isspace(UChar(*value)) ||
335 strchr("+,", UChar(*value)) != 0)) {
344 #define DATA(s) s "\n"
345 static const char msg[] =
347 DATA("Usage: tabs [options] [tabstop-list]")
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)")
359 DATA(" -n no-op (do not modify terminal settings)")
362 DATA(" -u UNIVAC 1100 Assembler")
363 DATA(" -T name use terminal type 'name'")
364 DATA(" -V print version")
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.")
373 ExitProgram(EXIT_FAILURE);
377 main(int argc, char *argv[])
379 int rc = EXIT_FAILURE;
383 NCURSES_CONST char *term_name = 0;
385 const char *tab_list = 0;
389 _nc_progname = _nc_rootname(argv[0]);
391 fd = save_tty_settings(&tty_settings, FALSE);
393 if ((term_name = getenv("TERM")) == 0)
394 term_name = "ansi+tabs";
396 /* cannot use getopt, since some options are two-character */
397 for (n = 1; n < argc; ++n) {
398 char *option = argv[n];
401 while ((ch = *++option) != '\0') {
407 tab_list = "1,10,16,36,72";
409 /* Assembler, IBM S/370, first format */
412 tab_list = "1,10,16,40,72";
413 /* Assembler, IBM S/370, second format */
421 tab_list = "1,8,12,16,20,55";
423 /* COBOL, normal format */
426 tab_list = "1,6,10,14,49";
427 /* COBOL compact format */
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 */
435 case 'd': /* ncurses extension */
439 tab_list = "1,7,11,15,19,23";
442 case 'n': /* ncurses extension */
446 tab_list = "1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61";
450 tab_list = "1,10,55";
454 tab_list = "1,12,20,44";
455 /* UNIVAC 1100 Assembler */
459 if (*++option != '\0') {
465 option += ((int) strlen(option)) - 1;
468 puts(curses_version());
469 ExitProgram(EXIT_SUCCESS);
471 if (isdigit(UChar(*option))) {
472 char *copy = strdup(option);
473 *skip_list(copy) = '\0';
475 option = skip_list(option) - 1;
484 while ((ch = *++option) != '\0') {
488 * The "+mXXX" option is unimplemented because only the long-obsolete
489 * att510d implements smgl, which is needed to support
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]);
504 if (tab_list != (const char *) append) {
505 /* one of the predefined options was used */
510 tab_list = add_to_tab_list(&append, option);
515 setupterm(term_name, fd, (int *) 0);
517 max_cols = (columns > 0) ? columns : 80;
519 if (!VALID_STRING(clear_all_tabs)) {
521 "%s: terminal type '%s' cannot reset tabs\n",
522 _nc_progname, term_name);
523 } else if (!VALID_STRING(set_tab)) {
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);
531 tputs(clear_all_tabs, 1, putch);
538 printf("tabs %s\n", tab_list);
545 printf("tabs %s\n", tab_list);