]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
col: move option handling to separate function
authorSami Kerola <kerolasa@iki.fi>
Sat, 27 Jun 2020 18:32:25 +0000 (19:32 +0100)
committerSami Kerola <kerolasa@iki.fi>
Fri, 11 Sep 2020 19:55:02 +0000 (20:55 +0100)
Mark --tabs and --spaces mutually exclusive in same go.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
text-utils/col.c

index 8103def49891491a72e5a801218a5d9f48f274dd..b0820278d396046b337aaf0263b70fa434fdf3f3 100644 (file)
@@ -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) {