X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=progs%2Ftabs.c;h=4a6eb2415e7c973d0ce13d7eb3f93fbfa19055d1;hp=64f98bdc0714c06a426816406a6d902a11f8d360;hb=643ec2bf782cd02efafe3ccdeaea8920a404645e;hpb=0819b56c3096ed77dd36312b0c4e8f37e7d46c88;ds=sidebyside diff --git a/progs/tabs.c b/progs/tabs.c index 64f98bdc..4a6eb241 100644 --- a/progs/tabs.c +++ b/progs/tabs.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2008-2013,2015 Free Software Foundation, Inc. * + * Copyright 2020,2021 Thomas E. Dickey * + * Copyright 2008-2016,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -36,12 +37,13 @@ #define USE_LIBTINFO #include +#include -MODULE_ID("$Id: tabs.c,v 1.36 2015/04/18 22:20:21 James.Clarke Exp $") +MODULE_ID("$Id: tabs.c,v 1.47 2021/04/03 23:00:00 tom Exp $") -static void usage(void) GCC_NORETURN; +static GCC_NORETURN void usage(void); -static char *prg_name; +const char *_nc_progname; static int max_cols; static void @@ -73,7 +75,7 @@ do_tabs(int *tab_list) } } if (stop <= max_cols) { - tputs(tparm(set_tab, stop), 1, putch); + tputs(TIPARM_1(set_tab, stop), 1, putch); last = stop; } else { break; @@ -103,7 +105,7 @@ decode_tabs(const char *tab_list) if (n > 0 && result[n] <= result[n - 1]) { fprintf(stderr, "%s: tab-stops are not in increasing order: %d %d\n", - prg_name, value, result[n - 1]); + _nc_progname, value, result[n - 1]); free(result); result = 0; break; @@ -144,7 +146,6 @@ static void print_ruler(int *tab_list) { int last = 0; - int stop; int n; /* first print a readable ruler */ @@ -162,7 +163,8 @@ print_ruler(int *tab_list) /* now, print '*' for each stop */ for (n = 0, last = 0; (tab_list[n] > 0) && (last < max_cols); ++n) { - stop = tab_list[n]; + int stop = tab_list[n]; + while (++last < stop) { if (last <= max_cols) { putchar('-'); @@ -208,11 +210,11 @@ static char * trimmed_tab_list(const char *source) { char *result = strdup(source); - int ch, j, k, last; - if (result != 0) { + int j, k, last; + for (j = k = last = 0; result[j] != 0; ++j) { - ch = UChar(result[j]); + int ch = UChar(result[j]); if (isspace(ch)) { if (last == '\0') { continue; @@ -288,6 +290,7 @@ add_to_tab_list(char **append, const char *value) *append = result; } + free(copied); return result; } @@ -301,23 +304,25 @@ legal_tab_list(const char *tab_list) if (tab_list != 0 && *tab_list != '\0') { if (comma_is_needed(tab_list)) { - int n, ch; + int n; + for (n = 0; tab_list[n] != '\0'; ++n) { - ch = UChar(tab_list[n]); + int ch = UChar(tab_list[n]); + if (!(isdigit(ch) || ch == ',' || ch == '+')) { fprintf(stderr, "%s: unexpected character found '%c'\n", - prg_name, ch); + _nc_progname, ch); result = FALSE; break; } } } else { - fprintf(stderr, "%s: trailing comma found '%s'\n", prg_name, tab_list); + fprintf(stderr, "%s: trailing comma found '%s'\n", _nc_progname, tab_list); result = FALSE; } } else { - fprintf(stderr, "%s: no tab-list given\n", prg_name); + fprintf(stderr, "%s: no tab-list given\n", _nc_progname); result = FALSE; } return result; @@ -380,8 +385,12 @@ main(int argc, char *argv[]) NCURSES_CONST char *term_name = 0; char *append = 0; const char *tab_list = 0; + TTY tty_settings; + int fd; + + _nc_progname = _nc_rootname(argv[0]); - prg_name = _nc_rootname(argv[0]); + fd = save_tty_settings(&tty_settings, FALSE); if ((term_name = getenv("TERM")) == 0) term_name = "ansi+tabs"; @@ -452,7 +461,7 @@ main(int argc, char *argv[]) if (*++option != '\0') { term_name = option; } else { - term_name = argv[n++]; + term_name = argv[n]; option--; } option += ((int) strlen(option)) - 1; @@ -505,18 +514,18 @@ main(int argc, char *argv[]) } } - setupterm(term_name, STDOUT_FILENO, (int *) 0); + setupterm(term_name, fd, (int *) 0); max_cols = (columns > 0) ? columns : 80; if (!VALID_STRING(clear_all_tabs)) { fprintf(stderr, "%s: terminal type '%s' cannot reset tabs\n", - prg_name, term_name); + _nc_progname, term_name); } else if (!VALID_STRING(set_tab)) { fprintf(stderr, "%s: terminal type '%s' cannot set tabs\n", - prg_name, term_name); + _nc_progname, term_name); } else if (legal_tab_list(tab_list)) { int *list = decode_tabs(tab_list);