2006-12-06 17:25:32 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
/*
|
2013-01-26 10:07:51 -06:00
|
|
|
* 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
|
2006-12-06 17:25:41 -06:00
|
|
|
* added Native Language Support
|
|
|
|
* 1999-09-19 Bruno Haible <haible@clisp.cons.org>
|
|
|
|
* modified to work correctly in multi-byte locales
|
2006-12-06 17:25:39 -06:00
|
|
|
*/
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
#include <unistd.h>
|
2006-12-06 17:25:32 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-10-04 13:57:57 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2012-05-15 10:46:20 -05:00
|
|
|
#include "nls.h"
|
2006-12-06 17:25:41 -06:00
|
|
|
#include "widechar.h"
|
2011-01-25 15:44:52 -06:00
|
|
|
#include "c.h"
|
2011-04-02 11:30:40 -05:00
|
|
|
#include "xalloc.h"
|
2011-05-01 12:24:47 -05:00
|
|
|
#include "strutils.h"
|
2012-04-04 12:39:15 -05:00
|
|
|
#include "closestream.h"
|
2012-05-15 10:46:20 -05:00
|
|
|
#include "ttyutils.h"
|
2006-12-06 17:25:41 -06:00
|
|
|
|
2007-01-30 06:49:50 -06:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2006-12-06 17:25:41 -06:00
|
|
|
#define wcs_width(s) wcswidth(s,wcslen(s))
|
|
|
|
static wchar_t *mbs_to_wcs(const char *);
|
|
|
|
#else
|
|
|
|
#define wcs_width(s) strlen(s)
|
2011-04-02 11:30:40 -05:00
|
|
|
#define mbs_to_wcs(s) xstrdup(s)
|
2006-12-06 17:25:41 -06:00
|
|
|
static char *mtsafe_strtok(char *, const char *, char **);
|
|
|
|
#define wcstok mtsafe_strtok
|
|
|
|
#endif
|
|
|
|
|
2010-10-09 12:59:50 -05:00
|
|
|
#define DEFCOLS 25
|
|
|
|
#define TAB 8
|
|
|
|
#define DEFNUM 1000
|
|
|
|
#define MAXLINELEN (LINE_MAX + 1)
|
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
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);
|
2012-09-26 15:45:36 -05:00
|
|
|
static wchar_t *local_wcstok(wchar_t *p, const wchar_t *separator, int greedy, wchar_t **wcstok_state);
|
2012-10-08 02:08:08 -05:00
|
|
|
static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy, wchar_t *colsep);
|
2011-05-01 12:26:49 -05:00
|
|
|
static void print(wchar_t **list, int entries);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2010-10-09 12:59:50 -05:00
|
|
|
typedef struct _tbl {
|
|
|
|
wchar_t **list;
|
|
|
|
int cols, *len;
|
|
|
|
} TBL;
|
|
|
|
|
2010-10-04 13:57:57 -05:00
|
|
|
static void __attribute__((__noreturn__)) usage(int rc)
|
|
|
|
{
|
|
|
|
FILE *out = rc == EXIT_FAILURE ? stderr : stdout;
|
|
|
|
|
2013-01-22 17:27:13 -06:00
|
|
|
fputs(USAGE_HEADER, out);
|
2013-05-29 14:52:56 -05:00
|
|
|
fprintf(out, _(" %s [options] [<file>...]\n"), program_invocation_short_name);
|
2014-12-22 15:57:17 -06:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Columnate lists.\n"), out);
|
|
|
|
|
2013-01-22 17:27:13 -06:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
|
|
|
fputs(_(" -c, --columns <width> width of output in number of characters\n"), out);
|
|
|
|
fputs(_(" -t, --table create a table\n"), out);
|
|
|
|
fputs(_(" -s, --separator <string> possible table delimiters\n"), out);
|
2013-05-29 14:52:56 -05:00
|
|
|
fputs(_(" -o, --output-separator <string>\n"
|
|
|
|
" columns separator for table output; default is two spaces\n"), out);
|
2013-01-22 17:27:13 -06:00
|
|
|
fputs(_(" -x, --fillrows fill rows before columns\n"), out);
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("column(1)"));
|
2010-10-04 13:57:57 -05:00
|
|
|
|
|
|
|
exit(rc);
|
|
|
|
}
|
2011-05-01 12:15:21 -05:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2011-05-26 03:23:50 -05:00
|
|
|
int ch, tflag = 0, xflag = 0;
|
2011-09-10 09:48:12 -05:00
|
|
|
int i;
|
2012-05-15 10:46:20 -05:00
|
|
|
int termwidth = 80;
|
2011-05-01 12:26:49 -05:00
|
|
|
int entries = 0; /* number of records */
|
|
|
|
unsigned int eval = 0; /* exit value */
|
2011-05-26 03:23:50 -05:00
|
|
|
int maxlength = 0; /* longest record */
|
2011-05-01 12:26:49 -05:00
|
|
|
wchar_t **list = NULL; /* array of pointers to records */
|
2012-09-26 15:45:36 -05:00
|
|
|
int greedy = 1;
|
2012-10-08 02:08:08 -05:00
|
|
|
wchar_t *colsep; /* table column output separator */
|
2011-05-26 03:23:50 -05:00
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
/* field separator for table option */
|
|
|
|
wchar_t default_separator[] = { '\t', ' ', 0 };
|
|
|
|
wchar_t *separator = default_separator;
|
|
|
|
|
|
|
|
static const struct option longopts[] =
|
|
|
|
{
|
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "version", 0, 0, 'V' },
|
2012-08-14 11:27:09 -05:00
|
|
|
{ "columns", 1, 0, 'c' },
|
2011-05-01 12:26:49 -05:00
|
|
|
{ "table", 0, 0, 't' },
|
2012-08-14 11:27:09 -05:00
|
|
|
{ "separator", 1, 0, 's' },
|
2012-10-08 02:08:08 -05:00
|
|
|
{ "output-separator", 1, 0, 'o' },
|
2011-05-01 12:26:49 -05:00
|
|
|
{ "fillrows", 0, 0, 'x' },
|
|
|
|
{ NULL, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
2006-12-06 17:25:39 -06:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-04-04 12:39:15 -05:00
|
|
|
atexit(close_stdout);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2012-05-15 10:46:20 -05:00
|
|
|
termwidth = get_terminal_width();
|
|
|
|
if (termwidth <= 0)
|
|
|
|
termwidth = 80;
|
2012-10-08 02:08:08 -05:00
|
|
|
colsep = mbs_to_wcs(" ");
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2012-10-08 02:08:08 -05:00
|
|
|
while ((ch = getopt_long(argc, argv, "hVc:s:txo:", longopts, NULL)) != -1)
|
2006-12-06 17:25:32 -06:00
|
|
|
switch(ch) {
|
2010-10-04 13:57:57 -05:00
|
|
|
case 'h':
|
2010-10-09 12:59:50 -05:00
|
|
|
usage(EXIT_SUCCESS);
|
|
|
|
break;
|
2011-05-01 12:09:54 -05:00
|
|
|
case 'V':
|
2014-09-28 14:51:39 -05:00
|
|
|
printf(UTIL_LINUX_VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:25:32 -06:00
|
|
|
case 'c':
|
2012-05-15 10:46:20 -05:00
|
|
|
termwidth = strtou32_or_err(optarg, _("invalid columns argument"));
|
2006-12-06 17:25:32 -06:00
|
|
|
break;
|
|
|
|
case 's':
|
2006-12-06 17:25:41 -06:00
|
|
|
separator = mbs_to_wcs(optarg);
|
2012-09-26 15:45:36 -05:00
|
|
|
greedy = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
break;
|
2012-10-08 02:08:08 -05:00
|
|
|
case 'o':
|
|
|
|
free(colsep);
|
|
|
|
colsep = mbs_to_wcs(optarg);
|
|
|
|
break;
|
2006-12-06 17:25:32 -06:00
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2010-10-04 13:57:57 -05:00
|
|
|
usage(EXIT_FAILURE);
|
2011-05-01 12:15:21 -05:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (!*argv)
|
2011-05-01 12:26:49 -05:00
|
|
|
eval += input(stdin, &maxlength, &list, &entries);
|
2011-05-01 12:15:21 -05:00
|
|
|
else
|
2010-10-09 12:59:50 -05:00
|
|
|
for (; *argv; ++argv) {
|
2011-05-26 03:23:50 -05:00
|
|
|
FILE *fp;
|
|
|
|
|
2010-10-09 12:59:50 -05:00
|
|
|
if ((fp = fopen(*argv, "r")) != NULL) {
|
2011-05-01 12:26:49 -05:00
|
|
|
eval += input(fp, &maxlength, &list, &entries);
|
|
|
|
fclose(fp);
|
2010-10-09 12:59:50 -05:00
|
|
|
} else {
|
|
|
|
warn("%s", *argv);
|
2011-05-01 12:26:49 -05:00
|
|
|
eval += EXIT_FAILURE;
|
2010-10-09 12:59:50 -05:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!entries)
|
|
|
|
exit(eval);
|
|
|
|
|
|
|
|
if (tflag)
|
2012-10-08 02:08:08 -05:00
|
|
|
maketbl(list, entries, separator, greedy, colsep);
|
2006-12-06 17:25:32 -06:00
|
|
|
else if (maxlength >= termwidth)
|
2011-05-01 12:26:49 -05:00
|
|
|
print(list, entries);
|
2006-12-06 17:25:32 -06:00
|
|
|
else if (xflag)
|
2011-05-01 12:26:49 -05:00
|
|
|
c_columnate(maxlength, termwidth, list, entries);
|
2006-12-06 17:25:32 -06:00
|
|
|
else
|
2011-05-01 12:26:49 -05:00
|
|
|
r_columnate(maxlength, termwidth, list, entries);
|
2011-05-01 12:20:25 -05:00
|
|
|
|
2011-09-10 09:48:12 -05:00
|
|
|
for (i = 0; i < entries; i++)
|
2011-05-01 12:20:25 -05:00
|
|
|
free(list[i]);
|
|
|
|
free(list);
|
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
if (eval == 0)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
else
|
|
|
|
return EXIT_FAILURE;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
static void c_columnate(int maxlength, long termwidth, wchar_t **list, int entries)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
|
|
|
int chcnt, col, cnt, endcol, numcols;
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t **lp;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
|
|
|
maxlength = (maxlength + TAB) & ~(TAB - 1);
|
|
|
|
numcols = termwidth / maxlength;
|
|
|
|
endcol = maxlength;
|
|
|
|
for (chcnt = col = 0, lp = list;; ++lp) {
|
2006-12-06 17:25:41 -06:00
|
|
|
fputws(*lp, stdout);
|
|
|
|
chcnt += wcs_width(*lp);
|
2006-12-06 17:25:32 -06:00
|
|
|
if (!--entries)
|
|
|
|
break;
|
|
|
|
if (++col == numcols) {
|
|
|
|
chcnt = col = 0;
|
|
|
|
endcol = maxlength;
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\n');
|
2006-12-06 17:25:32 -06:00
|
|
|
} else {
|
2006-12-06 17:25:34 -06:00
|
|
|
while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\t');
|
2006-12-06 17:25:32 -06:00
|
|
|
chcnt = cnt;
|
|
|
|
}
|
|
|
|
endcol += maxlength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chcnt)
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\n');
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
static void r_columnate(int maxlength, long termwidth, wchar_t **list, int entries)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
|
|
|
int base, chcnt, cnt, col, endcol, numcols, numrows, row;
|
|
|
|
|
|
|
|
maxlength = (maxlength + TAB) & ~(TAB - 1);
|
|
|
|
numcols = termwidth / maxlength;
|
2006-12-06 17:25:34 -06:00
|
|
|
if (!numcols)
|
2010-10-09 12:59:50 -05:00
|
|
|
numcols = 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
numrows = entries / numcols;
|
|
|
|
if (entries % numcols)
|
|
|
|
++numrows;
|
|
|
|
|
|
|
|
for (row = 0; row < numrows; ++row) {
|
|
|
|
endcol = maxlength;
|
|
|
|
for (base = row, chcnt = col = 0; col < numcols; ++col) {
|
2006-12-06 17:25:41 -06:00
|
|
|
fputws(list[base], stdout);
|
|
|
|
chcnt += wcs_width(list[base]);
|
2006-12-06 17:25:32 -06:00
|
|
|
if ((base += numrows) >= entries)
|
|
|
|
break;
|
2006-12-06 17:25:34 -06:00
|
|
|
while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\t');
|
2006-12-06 17:25:32 -06:00
|
|
|
chcnt = cnt;
|
|
|
|
}
|
|
|
|
endcol += maxlength;
|
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
putwchar('\n');
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
static void print(wchar_t **list, int entries)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
|
|
|
int cnt;
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t **lp;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2006-12-06 17:25:41 -06:00
|
|
|
for (cnt = entries, lp = list; cnt--; ++lp) {
|
|
|
|
fputws(*lp, stdout);
|
|
|
|
putwchar('\n');
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2012-09-26 15:45:36 -05:00
|
|
|
wchar_t *local_wcstok(wchar_t * p, const wchar_t * separator, int greedy,
|
|
|
|
wchar_t ** wcstok_state)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-08 02:08:08 -05:00
|
|
|
static void maketbl(wchar_t **list, int entries, wchar_t *separator, int greedy, wchar_t *colsep)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
|
|
|
TBL *t;
|
2013-08-03 18:15:19 -05:00
|
|
|
int cnt;
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t *p, **lp;
|
2011-05-01 12:16:29 -05:00
|
|
|
ssize_t *lens;
|
|
|
|
ssize_t maxcols = DEFCOLS, coloff;
|
2006-12-06 17:25:32 -06:00
|
|
|
TBL *tbl;
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t **cols;
|
2012-09-26 15:45:36 -05:00
|
|
|
wchar_t *wcstok_state = NULL;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2011-05-01 12:08:08 -05:00
|
|
|
t = tbl = xcalloc(entries, sizeof(TBL));
|
|
|
|
cols = xcalloc(maxcols, sizeof(wchar_t *));
|
2011-05-01 12:16:29 -05:00
|
|
|
lens = xcalloc(maxcols, sizeof(ssize_t));
|
|
|
|
|
|
|
|
for (lp = list, cnt = 0; cnt < entries; ++cnt, ++lp, ++t) {
|
|
|
|
coloff = 0;
|
|
|
|
p = *lp;
|
2012-09-26 15:45:36 -05:00
|
|
|
while ((cols[coloff] = local_wcstok(p, separator, greedy, &wcstok_state)) != NULL) {
|
2010-10-09 12:59:50 -05:00
|
|
|
if (++coloff == maxcols) {
|
|
|
|
maxcols += DEFCOLS;
|
2011-05-01 12:16:29 -05:00
|
|
|
cols = xrealloc(cols, maxcols * sizeof(wchar_t *));
|
|
|
|
lens = xrealloc(lens, maxcols * sizeof(ssize_t));
|
|
|
|
/* zero fill only new memory */
|
2011-11-16 13:10:39 -06:00
|
|
|
memset(lens + (maxcols - DEFCOLS), 0,
|
|
|
|
DEFCOLS * sizeof(*lens));
|
2010-10-09 12:59:50 -05:00
|
|
|
}
|
2011-05-01 12:16:29 -05:00
|
|
|
p = NULL;
|
2010-10-09 12:59:50 -05:00
|
|
|
}
|
2011-05-01 12:08:08 -05:00
|
|
|
t->list = xcalloc(coloff, sizeof(wchar_t *));
|
|
|
|
t->len = xcalloc(coloff, sizeof(int));
|
2010-10-09 12:59:50 -05:00
|
|
|
for (t->cols = coloff; --coloff >= 0;) {
|
|
|
|
t->list[coloff] = cols[coloff];
|
|
|
|
t->len[coloff] = wcs_width(cols[coloff]);
|
|
|
|
if (t->len[coloff] > lens[coloff])
|
|
|
|
lens[coloff] = t->len[coloff];
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 12:16:29 -05:00
|
|
|
|
|
|
|
for (t = tbl, cnt = 0; cnt < entries; ++cnt, ++t) {
|
2010-10-09 12:59:50 -05:00
|
|
|
for (coloff = 0; coloff < t->cols - 1; ++coloff) {
|
|
|
|
fputws(t->list[coloff], stdout);
|
2013-08-03 18:15:19 -05:00
|
|
|
wprintf(L"%*s", lens[coloff] - t->len[coloff], "");
|
2012-10-08 02:08:08 -05:00
|
|
|
fputws(colsep, stdout);
|
2010-10-09 12:59:50 -05:00
|
|
|
}
|
2010-11-04 16:22:49 -05:00
|
|
|
if (coloff < t->cols) {
|
|
|
|
fputws(t->list[coloff], stdout);
|
|
|
|
putwchar('\n');
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2011-05-01 12:20:25 -05:00
|
|
|
|
|
|
|
for (cnt = 0; cnt < entries; ++cnt) {
|
|
|
|
free((tbl+cnt)->list);
|
|
|
|
free((tbl+cnt)->len);
|
|
|
|
}
|
|
|
|
free(cols);
|
|
|
|
free(lens);
|
|
|
|
free(tbl);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2011-05-01 12:26:49 -05:00
|
|
|
static int input(FILE *fp, int *maxlength, wchar_t ***list, int *entries)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2011-05-01 12:08:08 -05:00
|
|
|
static int maxentry = DEFNUM;
|
2011-05-01 12:26:49 -05:00
|
|
|
int len, lineno = 1, reportedline = 0, eval = 0;
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t *p, buf[MAXLINELEN];
|
2011-05-26 03:23:50 -05:00
|
|
|
wchar_t **local_list = *list;
|
2011-05-01 12:26:49 -05:00
|
|
|
int local_entries = *entries;
|
|
|
|
|
|
|
|
if (!local_list)
|
|
|
|
local_list = xcalloc(maxentry, sizeof(wchar_t *));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-05-04 10:39:38 -05:00
|
|
|
while (1) {
|
|
|
|
if (fgetws(buf, MAXLINELEN, fp) == NULL) {
|
|
|
|
if (feof(fp))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
err(EXIT_FAILURE, _("read failed"));
|
|
|
|
}
|
2011-05-01 12:15:21 -05:00
|
|
|
for (p = buf; *p && iswspace(*p); ++p)
|
|
|
|
;
|
2006-12-06 17:25:32 -06:00
|
|
|
if (!*p)
|
|
|
|
continue;
|
2010-10-09 12:59:48 -05:00
|
|
|
if (!(p = wcschr(p, '\n')) && !feof(fp)) {
|
2010-10-09 12:59:49 -05:00
|
|
|
if (reportedline < lineno) {
|
2010-10-09 12:59:50 -05:00
|
|
|
warnx(_("line %d is too long, output will be truncated"),
|
|
|
|
lineno);
|
2010-10-09 12:59:49 -05:00
|
|
|
reportedline = lineno;
|
|
|
|
}
|
2011-05-01 12:26:49 -05:00
|
|
|
eval = 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-09 12:59:49 -05:00
|
|
|
lineno++;
|
2013-07-07 14:54:48 -05:00
|
|
|
if (!feof(fp) && p)
|
2010-10-09 12:59:48 -05:00
|
|
|
*p = '\0';
|
2006-12-06 17:25:44 -06:00
|
|
|
len = wcs_width(buf); /* len = p - buf; */
|
2011-05-01 12:26:49 -05:00
|
|
|
if (*maxlength < len)
|
|
|
|
*maxlength = len;
|
|
|
|
if (local_entries == maxentry) {
|
2006-12-06 17:25:32 -06:00
|
|
|
maxentry += DEFNUM;
|
2011-05-01 12:26:49 -05:00
|
|
|
local_list = xrealloc(local_list,
|
|
|
|
(u_int)maxentry * sizeof(wchar_t *));
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2011-05-01 12:26:49 -05:00
|
|
|
local_list[local_entries++] = wcsdup(buf);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
2011-05-01 12:26:49 -05:00
|
|
|
|
|
|
|
*list = local_list;
|
|
|
|
*entries = local_entries;
|
|
|
|
|
|
|
|
return eval;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2007-01-30 06:49:50 -06:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2006-12-06 17:25:41 -06:00
|
|
|
static wchar_t *mbs_to_wcs(const char *s)
|
|
|
|
{
|
2011-01-23 08:40:19 -06:00
|
|
|
ssize_t n;
|
2006-12-06 17:25:41 -06:00
|
|
|
wchar_t *wcs;
|
|
|
|
|
|
|
|
n = mbstowcs((wchar_t *)0, s, 0);
|
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
2011-12-20 07:36:02 -06:00
|
|
|
wcs = xmalloc((n + 1) * sizeof(wchar_t));
|
2011-01-23 08:40:19 -06:00
|
|
|
n = mbstowcs(wcs, s, n + 1);
|
2014-01-14 10:53:22 -06:00
|
|
|
if (n < 0) {
|
|
|
|
free(wcs);
|
2006-12-06 17:25:41 -06:00
|
|
|
return NULL;
|
2014-01-14 10:53:22 -06:00
|
|
|
}
|
2006-12-06 17:25:41 -06:00
|
|
|
return wcs;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-01-30 06:49:50 -06:00
|
|
|
#ifndef HAVE_WIDECHAR
|
2006-12-06 17:25:41 -06: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
|