From 7d07df62a22703f14f36114ae7df17d1efc82097 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 1 Mar 2017 13:11:59 +0100 Subject: columns: add control struct Signed-off-by: Karel Zak --- text-utils/column.c | 56 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) (limited to 'text-utils/column.c') diff --git a/text-utils/column.c b/text-utils/column.c index 385245f01..12f712139 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -2,6 +2,8 @@ * Copyright (c) 1989, 1993, 1994 * The Regents of the University of California. All rights reserved. * + * Copyright (C) 2017 Karel Zak + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -30,14 +32,6 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - -/* - * 1999-02-22 Arkadiusz Miƛkiewicz - * added Native Language Support - * 1999-09-19 Bruno Haible - * modified to work correctly in multi-byte locales - */ - #include #include @@ -83,6 +77,18 @@ typedef struct _tbl { } TBL; +enum { + COLUMN_MODE_FILLCOLS = 0, + COLUMN_MODE_FILLROWS, + COLUMN_MODE_TABLE, + COLUMN_MODE_SIMPLE +}; + +struct column_control { + int mode; /* COLUMN_MODE_* */ +}; + + #ifdef HAVE_WIDECHAR /* Don't use wcswidth(), we need to ignore non-printable chars. */ static int width(const wchar_t *str) @@ -136,7 +142,9 @@ static void __attribute__((__noreturn__)) usage(int rc) int main(int argc, char **argv) { - int ch, tflag = 0, xflag = 0; + struct column_control ctl = { .mode = COLUMN_MODE_FILLCOLS }; + + int ch; int i; int termwidth = 80; int entries = 0; /* number of records */ @@ -191,10 +199,10 @@ int main(int argc, char **argv) colsep = mbs_to_wcs(optarg); break; case 't': - tflag = 1; + ctl.mode = COLUMN_MODE_TABLE; break; case 'x': - xflag = 1; + ctl.mode = COLUMN_MODE_FILLROWS; break; default: errtryhelp(EXIT_FAILURE); @@ -220,23 +228,29 @@ int main(int argc, char **argv) if (!entries) exit(eval); - if (tflag) + if (ctl.mode != COLUMN_MODE_TABLE && maxlength >= termwidth) + ctl.mode = COLUMN_MODE_SIMPLE; + + switch (ctl.mode) { + case COLUMN_MODE_TABLE: maketbl(list, entries, separator, greedy, colsep); - else if (maxlength >= termwidth) - print(list, entries); - else if (xflag) - columnate_fillrows(maxlength, termwidth, list, entries); - else + break; + case COLUMN_MODE_FILLCOLS: columnate_fillcols(maxlength, termwidth, list, entries); + break; + case COLUMN_MODE_FILLROWS: + columnate_fillrows(maxlength, termwidth, list, entries); + break; + case COLUMN_MODE_SIMPLE: + print(list, entries); + break; + } for (i = 0; i < entries; i++) free(list[i]); free(list); - if (eval == 0) - return EXIT_SUCCESS; - else - return EXIT_FAILURE; + return eval == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } static void columnate_fillrows(int maxlength, long termwidth, wchar_t **list, int entries) -- cgit v1.2.3-55-g7522