From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 02/14] column: add --output-separator option
Date: Mon, 8 Oct 2012 08:08:08 +0100 [thread overview]
Message-ID: <1349680100-18120-3-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1349680100-18120-1-git-send-email-kerolasa@iki.fi>
The --output-separator option will allow user to define table column
separator. This will allow for example to write back same delimeter as
which was used as input separator, for example
column -t -s : -o : /etc/passwd
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
text-utils/column.1 | 2 ++
text-utils/column.c | 20 +++++++++++++++-----
2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/text-utils/column.1 b/text-utils/column.1
index 993f737..3727048 100644
--- a/text-utils/column.1
+++ b/text-utils/column.1
@@ -54,6 +54,8 @@ Columns are delimited with whitespace, by default, or with the characters
supplied using the separator. Table output is useful for pretty-printing.
.IP "\fB\-s, \-\-separator\fP \fIseparators\fP"
Specify possible table delimiters (default is whitespace).
+.IP "\fB\-o, \-\-output-separator\fP \fIseparators\fP"
+Specify table output delimiter (default is two whitespaces).
.IP "\fB\-x, \-\-fillrows\fP"
Fill columns before filling rows.
.IP "\fB\-h, \-\-help\fP"
diff --git a/text-utils/column.c b/text-utils/column.c
index d230979..33bc28d 100644
--- a/text-utils/column.c
+++ b/text-utils/column.c
@@ -76,7 +76,7 @@ static int input(FILE *fp, int *maxlength, wchar_t ***list, int *entries);
static void c_columnate(int maxlength, long termwidth, wchar_t **list, int entries);
static void r_columnate(int maxlength, long termwidth, wchar_t **list, int entries);
static wchar_t *local_wcstok(wchar_t *p, const wchar_t *separator, int greedy, wchar_t **wcstok_state);
-static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy);
+static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy, wchar_t *colsep);
static void print(wchar_t **list, int entries);
typedef struct _tbl {
@@ -98,6 +98,8 @@ static void __attribute__((__noreturn__)) usage(int rc)
" -c, --columns <width> width of output in number of characters\n"
" -t, --table create a table\n"
" -s, --separator <string> possible table delimiters\n"
+ " -o, --output-separator <string>\n"
+ " table output column separator, default is two spaces\n"
" -x, --fillrows fill rows before columns\n"));
fprintf(out, _("\nFor more information see column(1).\n"));
@@ -114,6 +116,7 @@ int main(int argc, char **argv)
int maxlength = 0; /* longest record */
wchar_t **list = NULL; /* array of pointers to records */
int greedy = 1;
+ wchar_t *colsep; /* table column output separator */
/* field separator for table option */
wchar_t default_separator[] = { '\t', ' ', 0 };
@@ -126,6 +129,7 @@ int main(int argc, char **argv)
{ "columns", 1, 0, 'c' },
{ "table", 0, 0, 't' },
{ "separator", 1, 0, 's' },
+ { "output-separator", 1, 0, 'o' },
{ "fillrows", 0, 0, 'x' },
{ NULL, 0, 0, 0 },
};
@@ -138,8 +142,9 @@ int main(int argc, char **argv)
termwidth = get_terminal_width();
if (termwidth <= 0)
termwidth = 80;
+ colsep = mbs_to_wcs(" ");
- while ((ch = getopt_long(argc, argv, "hVc:s:tx", longopts, NULL)) != -1)
+ while ((ch = getopt_long(argc, argv, "hVc:s:txo:", longopts, NULL)) != -1)
switch(ch) {
case 'h':
usage(EXIT_SUCCESS);
@@ -155,6 +160,10 @@ int main(int argc, char **argv)
separator = mbs_to_wcs(optarg);
greedy = 0;
break;
+ case 'o':
+ free(colsep);
+ colsep = mbs_to_wcs(optarg);
+ break;
case 't':
tflag = 1;
break;
@@ -186,7 +195,7 @@ int main(int argc, char **argv)
exit(eval);
if (tflag)
- maketbl(list, entries, separator, greedy);
+ maketbl(list, entries, separator, greedy, colsep);
else if (maxlength >= termwidth)
print(list, entries);
else if (xflag)
@@ -297,7 +306,7 @@ wchar_t *local_wcstok(wchar_t * p, const wchar_t * separator, int greedy,
return result;
}
-static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy)
+static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy, wchar_t *colsep)
{
TBL *t;
int cnt, i;
@@ -339,8 +348,9 @@ static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy)
for (t = tbl, cnt = 0; cnt < entries; ++cnt, ++t) {
for (coloff = 0; coloff < t->cols - 1; ++coloff) {
fputws(t->list[coloff], stdout);
- for (i = lens[coloff] - t->len[coloff] + 2; i > 0; i--)
+ for (i = lens[coloff] - t->len[coloff]; i > 0; i--)
putwchar(' ');
+ fputws(colsep, stdout);
}
if (coloff < t->cols) {
fputws(t->list[coloff], stdout);
--
1.7.12.2
next prev parent reply other threads:[~2012-10-08 7:08 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-08 7:08 [PATCH 00/14] various: clean ups and fixes Sami Kerola
2012-10-08 7:08 ` [PATCH 01/14] ddate: remove from util-linux Sami Kerola
2012-10-08 9:06 ` Petr Uzel
2012-10-08 7:08 ` Sami Kerola [this message]
2012-10-08 7:08 ` [PATCH 03/14] docs: fix column.1 groff syntax error Sami Kerola
2012-10-08 7:08 ` [PATCH 04/14] pg: refactor argument handing Sami Kerola
2012-10-08 7:08 ` [PATCH 05/14] pg: use libc error printing facilities Sami Kerola
2012-10-08 7:08 ` [PATCH 06/14] pg: add const qualifiers where suitable Sami Kerola
2012-10-08 7:08 ` [PATCH 07/14] pg: add noreturn function attributes Sami Kerola
2012-10-08 7:08 ` [PATCH 08/14] pg: use unistd.h STDOUT_FILENO Sami Kerola
2012-10-08 7:08 ` [PATCH 09/14] pg: do not turn off warnigns artificially Sami Kerola
2012-10-08 7:08 ` [PATCH 10/14] pg: fix coding style Sami Kerola
2012-10-08 7:08 ` [PATCH 11/14] more: " Sami Kerola
2012-10-08 7:08 ` [PATCH 12/14] more: align void in functions with prototypes, and remove void casts Sami Kerola
2012-10-08 7:08 ` [PATCH 13/14] more: remove few memory leaks Sami Kerola
2012-10-08 7:08 ` [PATCH 14/14] swapon: remove loop declaration [smatch scan] Sami Kerola
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1349680100-18120-3-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).