From 9f60a692170d3ada3f9d5d4e8399f4e975aaf208 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 27 Jun 2020 19:32:25 +0100 Subject: [PATCH] col: move option handling to separate function Mark --tabs and --spaces mutually exclusive in same go. Signed-off-by: Sami Kerola --- text-utils/col.c | 75 +++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/text-utils/col.c b/text-utils/col.c index 8103def498..b0820278d3 100644 --- a/text-utils/col.c +++ b/text-utils/col.c @@ -66,6 +66,7 @@ #include "widechar.h" #include "strutils.h" #include "closestream.h" +#include "optutils.h" #define BS '\b' /* backspace */ #define TAB '\t' /* tab */ @@ -339,26 +340,8 @@ static void flush_lines(struct col_ctl *ctl, int nflush) ctl->lines->l_prev = NULL; } -int main(int argc, char **argv) +static void parse_options(struct col_ctl *ctl, int argc, char **argv) { - struct col_ctl ctl = { - .compress_spaces = 1, - .last_set = CS_NORMAL, - .max_bufd_lines = 128 * 2, - }; - register wint_t ch; - CHAR *c = NULL; - CSET cur_set; /* current character set */ - LINE *l; /* current line */ - int extra_lines; /* # of lines above first line */ - int cur_col; /* current column */ - int cur_line; /* line number of current position */ - int max_line; /* max value of cur_line */ - int this_line; /* line l points to */ - int nflushd_lines; /* number of lines that were flushed */ - int adjust, opt, warned; - int ret = EXIT_SUCCESS; - static const struct option longopts[] = { { "no-backspaces", no_argument, NULL, 'b' }, { "fine", no_argument, NULL, 'f' }, @@ -370,35 +353,38 @@ int main(int argc, char **argv) { "help", no_argument, NULL, 'H' }, { NULL, 0, NULL, 0 } }; + static const ul_excl_t excl[] = { + { 'h', 'x' }, + { 0 } + }; + int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; + int opt; - setlocale(LC_ALL, ""); - bindtextdomain(PACKAGE, LOCALEDIR); - textdomain(PACKAGE); - close_stdout_atexit(); + while ((opt = getopt_long(argc, argv, "bfhl:pxVH", longopts, NULL)) != -1) { + err_exclusive_options(opt, longopts, excl, excl_st); - while ((opt = getopt_long(argc, argv, "bfhl:pxVH", longopts, NULL)) != -1) switch (opt) { case 'b': /* do not output backspaces */ - ctl.no_backspaces = 1; + ctl->no_backspaces = 1; break; case 'f': /* allow half forward line feeds */ - ctl.fine = 1; + ctl->fine = 1; break; case 'h': /* compress spaces into tabs */ - ctl.compress_spaces = 1; + ctl->compress_spaces = 1; break; case 'l': /* * Buffered line count, which is a value in half * lines e.g. twice the amount specified. */ - ctl.max_bufd_lines = strtou32_or_err(optarg, _("bad -l argument")) * 2; + ctl->max_bufd_lines = strtou32_or_err(optarg, _("bad -l argument")) * 2; break; case 'p': - ctl.pass_unknown_seqs = 1; + ctl->pass_unknown_seqs = 1; break; case 'x': /* do not compress spaces into tabs */ - ctl.compress_spaces = 0; + ctl->compress_spaces = 0; break; case 'V': @@ -408,17 +394,46 @@ int main(int argc, char **argv) default: errtryhelp(EXIT_FAILURE); } + } if (optind != argc) { warnx(_("bad usage")); errtryhelp(EXIT_FAILURE); } +} + +int main(int argc, char **argv) +{ + struct col_ctl ctl = { + .compress_spaces = 1, + .last_set = CS_NORMAL, + .max_bufd_lines = 128 * 2, + }; + register wint_t ch; + CHAR *c = NULL; + CSET cur_set; /* current character set */ + LINE *l; /* current line */ + int extra_lines; /* # of lines above first line */ + int cur_col; /* current column */ + int cur_line; /* line number of current position */ + int max_line; /* max value of cur_line */ + int this_line; /* line l points to */ + int nflushd_lines; /* number of lines that were flushed */ + int adjust, warned; + int ret = EXIT_SUCCESS; + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + close_stdout_atexit(); adjust = cur_col = extra_lines = warned = 0; cur_line = max_line = nflushd_lines = this_line = 0; cur_set = CS_NORMAL; ctl.lines = l = alloc_line(&ctl); + parse_options(&ctl, argc, argv); + while (feof(stdin) == 0) { errno = 0; if ((ch = getwchar()) == WEOF) { -- 2.47.3