summaryrefslogtreecommitdiffstats
path: root/text-utils/column.c
diff options
context:
space:
mode:
authorKarel Zak2017-03-01 13:11:59 +0100
committerKarel Zak2017-05-02 12:17:59 +0200
commit7d07df62a22703f14f36114ae7df17d1efc82097 (patch)
tree06602605b62538d1c8a89256c874bb097f1acd24 /text-utils/column.c
parentcolumn: split old and new code (diff)
downloadkernel-qcow2-util-linux-7d07df62a22703f14f36114ae7df17d1efc82097.tar.gz
kernel-qcow2-util-linux-7d07df62a22703f14f36114ae7df17d1efc82097.tar.xz
kernel-qcow2-util-linux-7d07df62a22703f14f36114ae7df17d1efc82097.zip
columns: add control struct
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'text-utils/column.c')
-rw-r--r--text-utils/column.c56
1 files changed, 35 insertions, 21 deletions
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 <kzak@redhat.com>
+ *
* 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 <misiek@pld.ORG.PL>
- * added Native Language Support
- * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
- * modified to work correctly in multi-byte locales
- */
-
#include <sys/types.h>
#include <sys/ioctl.h>
@@ -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)