2006-12-07 00:25:32 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
2017-03-01 13:11:59 +01:00
|
|
|
* Copyright (C) 2017 Karel Zak <kzak@redhat.com>
|
|
|
|
*
|
2006-12-07 00:25:32 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
2006-12-07 00:25:34 +01:00
|
|
|
#include <unistd.h>
|
2006-12-07 00:25:32 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-10-04 20:57:57 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2012-05-15 17:46:20 +02:00
|
|
|
#include "nls.h"
|
2011-01-25 22:44:52 +01:00
|
|
|
#include "c.h"
|
2017-01-19 17:27:37 -02:00
|
|
|
#include "widechar.h"
|
2011-04-02 18:30:40 +02:00
|
|
|
#include "xalloc.h"
|
2011-05-01 19:24:47 +02:00
|
|
|
#include "strutils.h"
|
2012-04-04 19:39:15 +02:00
|
|
|
#include "closestream.h"
|
2012-05-15 17:46:20 +02:00
|
|
|
#include "ttyutils.h"
|
2006-12-07 00:25:41 +01:00
|
|
|
|
2007-01-30 13:49:50 +01:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2006-12-07 00:25:41 +01:00
|
|
|
static wchar_t *mbs_to_wcs(const char *);
|
|
|
|
#else
|
2011-04-02 18:30:40 +02:00
|
|
|
#define mbs_to_wcs(s) xstrdup(s)
|
2006-12-07 00:25:41 +01:00
|
|
|
static char *mtsafe_strtok(char *, const char *, char **);
|
|
|
|
#define wcstok mtsafe_strtok
|
|
|
|
#endif
|
|
|
|
|
2010-10-09 19:59:50 +02:00
|
|
|
#define DEFCOLS 25
|
|
|
|
#define TAB 8
|
|
|
|
#define MAXLINELEN (LINE_MAX + 1)
|
|
|
|
|
|
|
|
typedef struct _tbl {
|
|
|
|
wchar_t **list;
|
|
|
|
int cols, *len;
|
|
|
|
} TBL;
|
|
|
|
|
2016-01-13 10:08:39 +01:00
|
|
|
|
2017-03-01 13:11:59 +01:00
|
|
|
enum {
|
|
|
|
COLUMN_MODE_FILLCOLS = 0,
|
|
|
|
COLUMN_MODE_FILLROWS,
|
|
|
|
COLUMN_MODE_TABLE,
|
|
|
|
COLUMN_MODE_SIMPLE
|
|
|
|
};
|
|
|
|
|
|
|
|
struct column_control {
|
2017-03-01 13:58:00 +01:00
|
|
|
int mode; /* COLUMN_MODE_* */
|
2017-03-01 15:05:47 +01:00
|
|
|
size_t termwidth;
|
2017-03-01 13:58:00 +01:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
wchar_t **ents; /* input entries */
|
|
|
|
size_t nents; /* number of entries */
|
|
|
|
size_t maxlength; /* longest input record (line) */
|
2017-03-01 13:11:59 +01:00
|
|
|
};
|
|
|
|
|
2017-03-02 14:38:04 +01:00
|
|
|
static int read_input(struct column_control *ctl, FILE *fp);
|
2017-03-01 13:58:00 +01:00
|
|
|
static void columnate_fillrows(struct column_control *ctl);
|
|
|
|
static void columnate_fillcols(struct column_control *ctl);
|
2017-03-02 14:38:04 +01:00
|
|
|
static void simple_print(struct column_control *ctl);
|
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
static wchar_t *local_wcstok(wchar_t *p, const wchar_t *separator, int greedy, wchar_t **wcstok_state);
|
|
|
|
static void maketbl(struct column_control *ctl, wchar_t *separator, int greedy, wchar_t *colsep);
|
2017-03-01 13:11:59 +01:00
|
|
|
|
2016-01-13 10:08:39 +01:00
|
|
|
#ifdef HAVE_WIDECHAR
|
|
|
|
/* Don't use wcswidth(), we need to ignore non-printable chars. */
|
|
|
|
static int width(const wchar_t *str)
|
|
|
|
{
|
|
|
|
int x, width = 0;
|
|
|
|
|
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
x = wcwidth(*str);
|
|
|
|
if (x > 0)
|
|
|
|
width += x;
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int width(const char *str)
|
|
|
|
{
|
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
if (isprint(*str))
|
|
|
|
width++;
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-04 20:57:57 +02:00
|
|
|
static void __attribute__((__noreturn__)) usage(int rc)
|
|
|
|
{
|
|
|
|
FILE *out = rc == EXIT_FAILURE ? stderr : stdout;
|
|
|
|
|
2013-01-22 23:27:13 +00:00
|
|
|
fputs(USAGE_HEADER, out);
|
2013-05-29 21:52:56 +02:00
|
|
|
fprintf(out, _(" %s [options] [<file>...]\n"), program_invocation_short_name);
|
2014-12-22 22:57:17 +01:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Columnate lists.\n"), out);
|
|
|
|
|
2013-01-22 23:27:13 +00:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
2017-01-17 13:11:27 +01:00
|
|
|
fputs(_(" -t, --table create a table\n"), out);
|
|
|
|
fputs(_(" -s, --separator <string> possible table delimiters\n"), out);
|
|
|
|
fputs(_(" -o, --output-separator <string> columns separator for table output\n"
|
|
|
|
" (default is two spaces)\n"), out);
|
|
|
|
fputs(_(" -c, --output-width <width> width of output in number of characters\n"), out);
|
|
|
|
fputs(_(" -x, --fillrows fill rows before columns\n"), out);
|
2013-01-22 23:27:13 +00:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("column(1)"));
|
2010-10-04 20:57:57 +02:00
|
|
|
|
|
|
|
exit(rc);
|
|
|
|
}
|
2011-05-01 19:15:21 +02:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2017-03-01 13:58:00 +01:00
|
|
|
struct column_control ctl = {
|
|
|
|
.mode = COLUMN_MODE_FILLCOLS,
|
|
|
|
.termwidth = 80
|
|
|
|
};
|
2017-03-01 13:11:59 +01:00
|
|
|
|
|
|
|
int ch;
|
2011-05-01 19:26:49 +02:00
|
|
|
unsigned int eval = 0; /* exit value */
|
2012-09-26 21:45:36 +01:00
|
|
|
int greedy = 1;
|
2012-10-08 08:08:08 +01:00
|
|
|
wchar_t *colsep; /* table column output separator */
|
2011-05-26 10:23:50 +02:00
|
|
|
|
2011-05-01 19:26:49 +02:00
|
|
|
/* field separator for table option */
|
|
|
|
wchar_t default_separator[] = { '\t', ' ', 0 };
|
|
|
|
wchar_t *separator = default_separator;
|
|
|
|
|
|
|
|
static const struct option longopts[] =
|
|
|
|
{
|
2017-02-11 20:23:26 +00:00
|
|
|
{ "columns", required_argument, NULL, 'c' }, /* deprecated */
|
|
|
|
{ "fillrows", no_argument, NULL, 'x' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "output-separator", required_argument, NULL, 'o' },
|
|
|
|
{ "output-width", required_argument, NULL, 'c' },
|
|
|
|
{ "separator", required_argument, NULL, 's' },
|
|
|
|
{ "table", no_argument, NULL, 't' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ NULL, 0, NULL, 0 },
|
2011-05-01 19:26:49 +02:00
|
|
|
};
|
|
|
|
|
2006-12-07 00:25:39 +01:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-04-04 19:39:15 +02:00
|
|
|
atexit(close_stdout);
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
ctl.termwidth = get_terminal_width(80);
|
2012-10-08 08:08:08 +01:00
|
|
|
colsep = mbs_to_wcs(" ");
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2012-10-08 08:08:08 +01:00
|
|
|
while ((ch = getopt_long(argc, argv, "hVc:s:txo:", longopts, NULL)) != -1)
|
2006-12-07 00:25:32 +01:00
|
|
|
switch(ch) {
|
2010-10-04 20:57:57 +02:00
|
|
|
case 'h':
|
2010-10-09 19:59:50 +02:00
|
|
|
usage(EXIT_SUCCESS);
|
|
|
|
break;
|
2011-05-01 19:09:54 +02:00
|
|
|
case 'V':
|
2014-09-28 20:51:39 +01:00
|
|
|
printf(UTIL_LINUX_VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
2006-12-07 00:25:32 +01:00
|
|
|
case 'c':
|
2017-03-01 13:58:00 +01:00
|
|
|
ctl.termwidth = strtou32_or_err(optarg, _("invalid columns argument"));
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
|
|
|
case 's':
|
2006-12-07 00:25:41 +01:00
|
|
|
separator = mbs_to_wcs(optarg);
|
2012-09-26 21:45:36 +01:00
|
|
|
greedy = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:08 +01:00
|
|
|
case 'o':
|
|
|
|
free(colsep);
|
|
|
|
colsep = mbs_to_wcs(optarg);
|
|
|
|
break;
|
2006-12-07 00:25:32 +01:00
|
|
|
case 't':
|
2017-03-01 13:11:59 +01:00
|
|
|
ctl.mode = COLUMN_MODE_TABLE;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
|
|
|
case 'x':
|
2017-03-01 13:11:59 +01:00
|
|
|
ctl.mode = COLUMN_MODE_FILLROWS;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-12-19 13:13:34 +01:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2011-05-01 19:15:21 +02:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (!*argv)
|
2017-03-02 14:38:04 +01:00
|
|
|
eval += read_input(&ctl, stdin);
|
2011-05-01 19:15:21 +02:00
|
|
|
else
|
2010-10-09 19:59:50 +02:00
|
|
|
for (; *argv; ++argv) {
|
2011-05-26 10:23:50 +02:00
|
|
|
FILE *fp;
|
|
|
|
|
2010-10-09 19:59:50 +02:00
|
|
|
if ((fp = fopen(*argv, "r")) != NULL) {
|
2017-03-02 14:38:04 +01:00
|
|
|
eval += read_input(&ctl, fp);
|
2011-05-01 19:26:49 +02:00
|
|
|
fclose(fp);
|
2010-10-09 19:59:50 +02:00
|
|
|
} else {
|
|
|
|
warn("%s", *argv);
|
2011-05-01 19:26:49 +02:00
|
|
|
eval += EXIT_FAILURE;
|
2010-10-09 19:59:50 +02:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
if (!ctl.nents)
|
2006-12-07 00:25:32 +01:00
|
|
|
exit(eval);
|
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
if (ctl.mode != COLUMN_MODE_TABLE && ctl.maxlength >= ctl.termwidth)
|
2017-03-01 13:11:59 +01:00
|
|
|
ctl.mode = COLUMN_MODE_SIMPLE;
|
|
|
|
|
|
|
|
switch (ctl.mode) {
|
|
|
|
case COLUMN_MODE_TABLE:
|
2017-03-01 13:58:00 +01:00
|
|
|
maketbl(&ctl, separator, greedy, colsep);
|
2017-03-01 13:11:59 +01:00
|
|
|
break;
|
|
|
|
case COLUMN_MODE_FILLCOLS:
|
2017-03-01 13:58:00 +01:00
|
|
|
columnate_fillcols(&ctl);
|
2017-03-01 13:11:59 +01:00
|
|
|
break;
|
|
|
|
case COLUMN_MODE_FILLROWS:
|
2017-03-01 13:58:00 +01:00
|
|
|
columnate_fillrows(&ctl);
|
2017-03-01 13:11:59 +01:00
|
|
|
break;
|
|
|
|
case COLUMN_MODE_SIMPLE:
|
2017-03-02 14:38:04 +01:00
|
|
|
simple_print(&ctl);
|
2017-03-01 13:11:59 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-05-01 19:20:25 +02:00
|
|
|
|
2017-03-01 13:11:59 +01:00
|
|
|
return eval == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
static void columnate_fillrows(struct column_control *ctl)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2017-03-01 15:05:47 +01:00
|
|
|
size_t chcnt, col, cnt, endcol, numcols;
|
2006-12-07 00:25:41 +01:00
|
|
|
wchar_t **lp;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
ctl->maxlength = (ctl->maxlength + TAB) & ~(TAB - 1);
|
|
|
|
numcols = ctl->termwidth / ctl->maxlength;
|
|
|
|
endcol = ctl->maxlength;
|
2017-03-01 15:05:47 +01:00
|
|
|
for (chcnt = col = 0, lp = ctl->ents;; ++lp) {
|
2006-12-07 00:25:41 +01:00
|
|
|
fputws(*lp, stdout);
|
2016-01-13 10:08:39 +01:00
|
|
|
chcnt += width(*lp);
|
2017-03-01 15:05:47 +01:00
|
|
|
if (!--ctl->nents)
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
|
|
|
if (++col == numcols) {
|
|
|
|
chcnt = col = 0;
|
2017-03-01 13:58:00 +01:00
|
|
|
endcol = ctl->maxlength;
|
2006-12-07 00:25:41 +01:00
|
|
|
putwchar('\n');
|
2006-12-07 00:25:32 +01:00
|
|
|
} else {
|
2006-12-07 00:25:34 +01:00
|
|
|
while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
|
2006-12-07 00:25:41 +01:00
|
|
|
putwchar('\t');
|
2006-12-07 00:25:32 +01:00
|
|
|
chcnt = cnt;
|
|
|
|
}
|
2017-03-01 13:58:00 +01:00
|
|
|
endcol += ctl->maxlength;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chcnt)
|
2006-12-07 00:25:41 +01:00
|
|
|
putwchar('\n');
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
static void columnate_fillcols(struct column_control *ctl)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2017-03-01 15:05:47 +01:00
|
|
|
size_t base, chcnt, cnt, col, endcol, numcols, numrows, row;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
ctl->maxlength = (ctl->maxlength + TAB) & ~(TAB - 1);
|
|
|
|
numcols = ctl->termwidth / ctl->maxlength;
|
2016-01-13 10:08:39 +01:00
|
|
|
if (!numcols)
|
2010-10-09 19:59:50 +02:00
|
|
|
numcols = 1;
|
2017-03-01 15:05:47 +01:00
|
|
|
numrows = ctl->nents / numcols;
|
|
|
|
if (ctl->nents % numcols)
|
2006-12-07 00:25:32 +01:00
|
|
|
++numrows;
|
|
|
|
|
|
|
|
for (row = 0; row < numrows; ++row) {
|
2017-03-01 13:58:00 +01:00
|
|
|
endcol = ctl->maxlength;
|
2006-12-07 00:25:32 +01:00
|
|
|
for (base = row, chcnt = col = 0; col < numcols; ++col) {
|
2017-03-01 15:05:47 +01:00
|
|
|
fputws(ctl->ents[base], stdout);
|
|
|
|
chcnt += width(ctl->ents[base]);
|
|
|
|
if ((base += numrows) >= ctl->nents)
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2006-12-07 00:25:34 +01:00
|
|
|
while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
|
2006-12-07 00:25:41 +01:00
|
|
|
putwchar('\t');
|
2006-12-07 00:25:32 +01:00
|
|
|
chcnt = cnt;
|
|
|
|
}
|
2017-03-01 13:58:00 +01:00
|
|
|
endcol += ctl->maxlength;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2006-12-07 00:25:41 +01:00
|
|
|
putwchar('\n');
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:38:04 +01:00
|
|
|
static void simple_print(struct column_control *ctl)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
|
|
|
int cnt;
|
2006-12-07 00:25:41 +01:00
|
|
|
wchar_t **lp;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
for (cnt = ctl->nents, lp = ctl->ents; cnt--; ++lp) {
|
2006-12-07 00:25:41 +01:00
|
|
|
fputws(*lp, stdout);
|
|
|
|
putwchar('\n');
|
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2017-02-12 00:19:33 +00:00
|
|
|
static wchar_t *local_wcstok(wchar_t *p, const wchar_t *separator, int greedy,
|
|
|
|
wchar_t **wcstok_state)
|
2012-09-26 21:45:36 +01:00
|
|
|
{
|
|
|
|
wchar_t *result;
|
|
|
|
if (greedy)
|
|
|
|
return wcstok(p, separator, wcstok_state);
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
if (*wcstok_state == NULL)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
p = *wcstok_state;
|
|
|
|
}
|
|
|
|
result = p;
|
|
|
|
p = wcspbrk(result, separator);
|
|
|
|
if (p == NULL)
|
|
|
|
*wcstok_state = NULL;
|
|
|
|
else {
|
|
|
|
*p = '\0';
|
|
|
|
*wcstok_state = p + 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-01 13:58:00 +01:00
|
|
|
static void maketbl(struct column_control *ctl, wchar_t *separator, int greedy, wchar_t *colsep)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
|
|
|
TBL *t;
|
2017-03-01 15:05:47 +01:00
|
|
|
size_t cnt;
|
2006-12-07 00:25:41 +01:00
|
|
|
wchar_t *p, **lp;
|
2011-05-01 19:16:29 +02:00
|
|
|
ssize_t *lens;
|
|
|
|
ssize_t maxcols = DEFCOLS, coloff;
|
2006-12-07 00:25:32 +01:00
|
|
|
TBL *tbl;
|
2006-12-07 00:25:41 +01:00
|
|
|
wchar_t **cols;
|
2012-09-26 21:45:36 +01:00
|
|
|
wchar_t *wcstok_state = NULL;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
t = tbl = xcalloc(ctl->nents, sizeof(TBL));
|
2011-05-01 19:08:08 +02:00
|
|
|
cols = xcalloc(maxcols, sizeof(wchar_t *));
|
2011-05-01 19:16:29 +02:00
|
|
|
lens = xcalloc(maxcols, sizeof(ssize_t));
|
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
for (lp = ctl->ents, cnt = 0; cnt < ctl->nents; ++cnt, ++lp, ++t) {
|
2011-05-01 19:16:29 +02:00
|
|
|
coloff = 0;
|
|
|
|
p = *lp;
|
2012-09-26 21:45:36 +01:00
|
|
|
while ((cols[coloff] = local_wcstok(p, separator, greedy, &wcstok_state)) != NULL) {
|
2010-10-09 19:59:50 +02:00
|
|
|
if (++coloff == maxcols) {
|
|
|
|
maxcols += DEFCOLS;
|
2011-05-01 19:16:29 +02:00
|
|
|
cols = xrealloc(cols, maxcols * sizeof(wchar_t *));
|
|
|
|
lens = xrealloc(lens, maxcols * sizeof(ssize_t));
|
|
|
|
/* zero fill only new memory */
|
2011-11-16 20:10:39 +01:00
|
|
|
memset(lens + (maxcols - DEFCOLS), 0,
|
|
|
|
DEFCOLS * sizeof(*lens));
|
2010-10-09 19:59:50 +02:00
|
|
|
}
|
2011-05-01 19:16:29 +02:00
|
|
|
p = NULL;
|
2010-10-09 19:59:50 +02:00
|
|
|
}
|
2011-05-01 19:08:08 +02:00
|
|
|
t->list = xcalloc(coloff, sizeof(wchar_t *));
|
|
|
|
t->len = xcalloc(coloff, sizeof(int));
|
2010-10-09 19:59:50 +02:00
|
|
|
for (t->cols = coloff; --coloff >= 0;) {
|
|
|
|
t->list[coloff] = cols[coloff];
|
2016-01-13 10:08:39 +01:00
|
|
|
t->len[coloff] = width(cols[coloff]);
|
2010-10-09 19:59:50 +02:00
|
|
|
if (t->len[coloff] > lens[coloff])
|
|
|
|
lens[coloff] = t->len[coloff];
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 19:16:29 +02:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
for (t = tbl, cnt = 0; cnt < ctl->nents; ++cnt, ++t) {
|
2010-10-09 19:59:50 +02:00
|
|
|
for (coloff = 0; coloff < t->cols - 1; ++coloff) {
|
|
|
|
fputws(t->list[coloff], stdout);
|
2016-01-13 10:08:39 +01:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2013-08-04 00:15:19 +01:00
|
|
|
wprintf(L"%*s", lens[coloff] - t->len[coloff], "");
|
2016-01-13 10:08:39 +01:00
|
|
|
#else
|
|
|
|
printf("%*s", (int) lens[coloff] - t->len[coloff], "");
|
|
|
|
#endif
|
2012-10-08 08:08:08 +01:00
|
|
|
fputws(colsep, stdout);
|
2010-10-09 19:59:50 +02:00
|
|
|
}
|
2010-11-04 22:22:49 +01:00
|
|
|
if (coloff < t->cols) {
|
|
|
|
fputws(t->list[coloff], stdout);
|
|
|
|
putwchar('\n');
|
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2011-05-01 19:20:25 +02:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
for (cnt = 0; cnt < ctl->nents; ++cnt) {
|
2011-05-01 19:20:25 +02:00
|
|
|
free((tbl+cnt)->list);
|
|
|
|
free((tbl+cnt)->len);
|
|
|
|
}
|
|
|
|
free(cols);
|
|
|
|
free(lens);
|
|
|
|
free(tbl);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 14:38:04 +01:00
|
|
|
static int read_input(struct column_control *ctl, FILE *fp)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2017-03-01 15:05:47 +01:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t bufsz = 0;
|
|
|
|
size_t maxents = 0;
|
2011-05-01 19:26:49 +02:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
do {
|
|
|
|
char *str, *p;
|
|
|
|
size_t len;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
if (getline(&buf, &bufsz, fp) < 0) {
|
2014-05-04 16:39:38 +01:00
|
|
|
if (feof(fp))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
err(EXIT_FAILURE, _("read failed"));
|
|
|
|
}
|
2017-03-01 15:05:47 +01:00
|
|
|
str = (char *) skip_space(buf);
|
|
|
|
if (str) {
|
|
|
|
p = strchr(str, '\n');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
if (!str || !*str)
|
2006-12-07 00:25:32 +01:00
|
|
|
continue;
|
2017-03-01 15:05:47 +01:00
|
|
|
|
|
|
|
switch (ctl->mode) {
|
|
|
|
case COLUMN_MODE_TABLE:
|
|
|
|
/* TODO: fill libsmartcols */
|
|
|
|
|
|
|
|
case COLUMN_MODE_FILLCOLS:
|
|
|
|
case COLUMN_MODE_FILLROWS:
|
|
|
|
if (ctl->nents <= maxents) {
|
|
|
|
maxents += 1000;
|
|
|
|
ctl->ents = xrealloc(ctl->ents,
|
|
|
|
maxents * sizeof(wchar_t *));
|
2010-10-09 19:59:49 +02:00
|
|
|
}
|
2017-03-01 15:05:47 +01:00
|
|
|
ctl->ents[ctl->nents] = mbs_to_wcs(str);
|
2017-03-02 14:04:47 +01:00
|
|
|
if (!ctl->ents[ctl->nents])
|
|
|
|
err(EXIT_FAILURE, _("read failed"));
|
2017-03-01 15:05:47 +01:00
|
|
|
len = width(ctl->ents[ctl->nents]);
|
|
|
|
if (ctl->maxlength < len)
|
|
|
|
ctl->maxlength = len;
|
|
|
|
ctl->nents++;
|
|
|
|
break;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2017-03-01 15:05:47 +01:00
|
|
|
} while (1);
|
2011-05-01 19:26:49 +02:00
|
|
|
|
2017-03-01 15:05:47 +01:00
|
|
|
return 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2007-01-30 13:49:50 +01:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2006-12-07 00:25:41 +01:00
|
|
|
static wchar_t *mbs_to_wcs(const char *s)
|
|
|
|
{
|
2011-01-23 15:40:19 +01:00
|
|
|
ssize_t n;
|
2006-12-07 00:25:41 +01:00
|
|
|
wchar_t *wcs;
|
|
|
|
|
|
|
|
n = mbstowcs((wchar_t *)0, s, 0);
|
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
2011-12-20 08:36:02 -05:00
|
|
|
wcs = xmalloc((n + 1) * sizeof(wchar_t));
|
2011-01-23 15:40:19 +01:00
|
|
|
n = mbstowcs(wcs, s, n + 1);
|
2014-01-14 17:53:22 +01:00
|
|
|
if (n < 0) {
|
|
|
|
free(wcs);
|
2006-12-07 00:25:41 +01:00
|
|
|
return NULL;
|
2014-01-14 17:53:22 +01:00
|
|
|
}
|
2006-12-07 00:25:41 +01:00
|
|
|
return wcs;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-01-30 13:49:50 +01:00
|
|
|
#ifndef HAVE_WIDECHAR
|
2006-12-07 00:25:41 +01:00
|
|
|
static char *mtsafe_strtok(char *str, const char *delim, char **ptr)
|
|
|
|
{
|
|
|
|
if (str == NULL) {
|
|
|
|
str = *ptr;
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
str += strspn(str, delim);
|
|
|
|
if (*str == '\0') {
|
|
|
|
*ptr = NULL;
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
char *token_end = strpbrk(str, delim);
|
|
|
|
if (token_end) {
|
|
|
|
*token_end = '\0';
|
|
|
|
*ptr = token_end + 1;
|
|
|
|
} else
|
|
|
|
*ptr = NULL;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|