5972c3
From 651c5d428c2ef103ee8c5b1a310d6f29f0304744 Mon Sep 17 00:00:00 2001
5972c3
From: Karel Zak <kzak@redhat.com>
5972c3
Date: Tue, 27 Mar 2018 10:40:13 +0200
5972c3
Subject: [PATCH] column: fix leading space characters bug
5972c3
5972c3
The bug has been introduced during column(1) rewrite. The function
5972c3
read_input() need to skip leading space only temporary to detect empty
5972c3
lines, but the rest of the code has to use the original buffer (line).
5972c3
I've tried to fix one of the symptoms by 5c7b67fbbf41c973ca8d49b1e8bdba22dbb917aa
5972c3
(alter), but this solution is unnecessary and too complex.
5972c3
5972c3
Changes:
5972c3
5972c3
* don't ignore leading space
5972c3
* remove unnecessary stuff introduced by 5c7b67fbbf41c973ca8d49b1e8bdba22dbb917aa
5972c3
* fix regression test with incorrect separator
5972c3
5972c3
Addresses: https://github.com/karelzak/util-linux/issues/575
5972c3
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1560283
5972c3
Signed-off-by: Karel Zak <kzak@redhat.com>
5972c3
---
5972c3
 tests/expected/column/table-input-separator-space |  2 +-
5972c3
 tests/ts/column/table                             |  2 +-
5972c3
 text-utils/column.c                               | 36 ++---------------------
5972c3
 4 files changed, 5 insertions(+), 38 deletions(-)
5972c3
5972c3
diff --git a/tests/expected/column/table-input-separator-space b/tests/expected/column/table-input-separator-space
5972c3
index 8a6513c11..25d9b5ab0 100644
5972c3
--- a/tests/expected/column/table-input-separator-space
5972c3
+++ b/tests/expected/column/table-input-separator-space
5972c3
@@ -1,5 +1,5 @@
5972c3
 AAA	BBBB	C	DDDD
5972c3
-BBB	CCCC	DDD
5972c3
+	BBB	CCCC	DDD
5972c3
 AA	BB		DD
5972c3
 AAAA	B	CC	D
5972c3
 AA		CC	DD
5972c3
diff --git a/tests/ts/column/table b/tests/ts/column/table
5972c3
index 27b52e7c8..5c89d5eaf 100755
5972c3
--- a/tests/ts/column/table
5972c3
+++ b/tests/ts/column/table
5972c3
@@ -37,7 +37,7 @@ $TS_CMD_COLUMN --separator ',' --table $TS_SELF/files/table-sep >> $TS_OUTPUT 2>
5972c3
 ts_finalize_subtest
5972c3
 
5972c3
 ts_init_subtest "input-separator-space"
5972c3
-$TS_CMD_COLUMN --separator ',' --table $TS_SELF/files/table-sep-space >> $TS_OUTPUT 2>&1
5972c3
+$TS_CMD_COLUMN --separator "$(echo -e '\t')" --table $TS_SELF/files/table-sep-space >> $TS_OUTPUT 2>&1
5972c3
 ts_finalize_subtest
5972c3
 
5972c3
 ts_init_subtest "long"
5972c3
diff --git a/text-utils/column.c b/text-utils/column.c
5972c3
index 89d46d280..195814328 100644
5972c3
--- a/text-utils/column.c
5972c3
+++ b/text-utils/column.c
5972c3
@@ -86,7 +86,6 @@ struct column_control {
5972c3
 	const char *tree_parent;
5972c3
 
5972c3
 	wchar_t *input_separator;
5972c3
-	char *input_separator_raw;
5972c3
 	const char *output_separator;
5972c3
 
5972c3
 	wchar_t	**ents;		/* input entries */
5972c3
@@ -96,7 +95,6 @@ struct column_control {
5972c3
 	unsigned int greedy :1,
5972c3
 		     json :1,
5972c3
 		     header_repeat :1,
5972c3
-		     input_sep_space : 1,	/* input separator contains space chars */
5972c3
 		     tab_noheadings :1;
5972c3
 };
5972c3
 
5972c3
@@ -470,19 +468,7 @@ static int read_input(struct column_control *ctl, FILE *fp)
5972c3
 	char *buf = NULL;
5972c3
 	size_t bufsz = 0;
5972c3
 	size_t maxents = 0;
5972c3
-	int rc = 0, is_space_sep = 0;
5972c3
-
5972c3
-	/* Check if columns separator contains spaces chars */
5972c3
-	if (ctl->mode == COLUMN_MODE_TABLE && ctl->input_separator_raw) {
5972c3
-		char *p;
5972c3
-
5972c3
-		for (p = ctl->input_separator_raw; *p; p++) {
5972c3
-			if (isspace(*p)) {
5972c3
-				is_space_sep = 1;
5972c3
-				break;
5972c3
-			}
5972c3
-		}
5972c3
-	}
5972c3
+	int rc = 0;
5972c3
 
5972c3
 	/* Read input */
5972c3
 	do {
5972c3
@@ -496,19 +482,6 @@ static int read_input(struct column_control *ctl, FILE *fp)
5972c3
 			err(EXIT_FAILURE, _("read failed"));
5972c3
 		}
5972c3
 		str = (char *) skip_space(buf);
5972c3
-
5972c3
-		/* The table columns separator could be a space. In this case
5972c3
-		 * don't skip the separator if at begin of the line. For example:
5972c3
-		 *
5972c3
-		 * echo -e "\tcol1\tcol2\nrow\t1\t2" \
5972c3
-		 *	| column -t -s "$(echo -e '\t')" --table-columns A,B,C
5972c3
-		 */
5972c3
-		if (is_space_sep && str > buf) {
5972c3
-			char *x = strpbrk(buf, ctl->input_separator_raw);
5972c3
-			if (x && x < str)
5972c3
-				str = x;
5972c3
-		}
5972c3
-
5972c3
 		if (str) {
5972c3
 			p = strchr(str, '\n');
5972c3
 			if (p)
5972c3
@@ -517,13 +490,13 @@ static int read_input(struct column_control *ctl, FILE *fp)
5972c3
 		if (!str || !*str)
5972c3
 			continue;
5972c3
 
5972c3
-		wcs = mbs_to_wcs(str);
5972c3
+		wcs = mbs_to_wcs(buf);
5972c3
 		if (!wcs) {
5972c3
 			/*
5972c3
 			 * Convert broken sequences to \x<hex> and continue.
5972c3
 			 */
5972c3
 			size_t tmpsz = 0;
5972c3
-			char *tmp = mbs_invalid_encode(str, &tmpsz);
5972c3
+			char *tmp = mbs_invalid_encode(buf, &tmpsz);
5972c3
 
5972c3
 			if (!tmp)
5972c3
 				err(EXIT_FAILURE, _("read failed"));
5972c3
@@ -720,7 +693,6 @@ int main(int argc, char **argv)
5972c3
 
5972c3
 	ctl.output_separator = "  ";
5972c3
 	ctl.input_separator = mbs_to_wcs("\t ");
5972c3
-	ctl.input_separator_raw = xstrdup("\t ");
5972c3
 
5972c3
 	while ((c = getopt_long(argc, argv, "c:dE:eH:hi:JN:n:O:o:p:R:r:s:T:tVW:x", longopts, NULL)) != -1) {
5972c3
 
5972c3
@@ -775,9 +747,7 @@ int main(int argc, char **argv)
5972c3
 			break;
5972c3
 		case 's':
5972c3
 			free(ctl.input_separator);
5972c3
-			free(ctl.input_separator_raw);
5972c3
 			ctl.input_separator = mbs_to_wcs(optarg);
5972c3
-			ctl.input_separator_raw = xstrdup(optarg);
5972c3
 			ctl.greedy = 0;
5972c3
 			break;
5972c3
 		case 'T':
5972c3
-- 
5972c3
2.14.3
5972c3