2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00: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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-12-06 22:58:23 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2001-12-02 23:52:47 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-10 21:13:08 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2001-12-10 21:13:08 +00:00
|
|
|
static char sccsid[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
|
2001-12-02 23:52:47 +00:00
|
|
|
#endif
|
2001-12-10 21:13:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2005-01-25 01:35:02 +00:00
|
|
|
#include <sys/param.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <limits.h>
|
2004-07-29 13:22:56 +00:00
|
|
|
#include <locale.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-11 13:08:12 +00:00
|
|
|
#include <unistd.h>
|
2004-07-29 13:22:56 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-04-03 18:03:29 +00:00
|
|
|
#define TAB 8
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void c_columnate(void);
|
|
|
|
static void input(FILE *);
|
|
|
|
static void maketbl(void);
|
|
|
|
static void print(void);
|
|
|
|
static void r_columnate(void);
|
|
|
|
static void usage(void);
|
|
|
|
static int width(const wchar_t *);
|
|
|
|
|
|
|
|
static int termwidth = 80; /* default terminal width */
|
|
|
|
|
|
|
|
static int entries; /* number of records */
|
|
|
|
static int eval; /* exit value */
|
|
|
|
static int maxlength; /* longest record */
|
|
|
|
static wchar_t **list; /* array of pointers to records */
|
|
|
|
static const wchar_t *separator = L"\t "; /* field separator for table option */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-07-28 15:25:15 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct winsize win;
|
|
|
|
FILE *fp;
|
|
|
|
int ch, tflag, xflag;
|
|
|
|
char *p;
|
2004-07-29 13:22:56 +00:00
|
|
|
const char *src;
|
|
|
|
wchar_t *newsep;
|
|
|
|
size_t seplen;
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
|
1998-12-06 22:58:23 +00:00
|
|
|
if ((p = getenv("COLUMNS")))
|
1994-05-27 12:33:43 +00:00
|
|
|
termwidth = atoi(p);
|
|
|
|
} else
|
|
|
|
termwidth = win.ws_col;
|
|
|
|
|
|
|
|
tflag = xflag = 0;
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'c':
|
|
|
|
termwidth = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
2004-07-29 13:22:56 +00:00
|
|
|
src = optarg;
|
|
|
|
seplen = mbsrtowcs(NULL, &src, 0, NULL);
|
|
|
|
if (seplen == (size_t)-1)
|
|
|
|
err(1, "bad separator");
|
|
|
|
newsep = malloc((seplen + 1) * sizeof(wchar_t));
|
|
|
|
if (newsep == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
mbsrtowcs(newsep, &src, seplen + 1, NULL);
|
|
|
|
separator = newsep;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (!*argv)
|
|
|
|
input(stdin);
|
|
|
|
else for (; *argv; ++argv)
|
1998-12-06 22:58:23 +00:00
|
|
|
if ((fp = fopen(*argv, "r"))) {
|
1994-05-27 12:33:43 +00:00
|
|
|
input(fp);
|
|
|
|
(void)fclose(fp);
|
|
|
|
} else {
|
|
|
|
warn("%s", *argv);
|
|
|
|
eval = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entries)
|
|
|
|
exit(eval);
|
|
|
|
|
2006-12-15 17:47:25 +00:00
|
|
|
maxlength = roundup(maxlength + 1, TAB);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (tflag)
|
|
|
|
maketbl();
|
|
|
|
else if (maxlength >= termwidth)
|
|
|
|
print();
|
|
|
|
else if (xflag)
|
|
|
|
c_columnate();
|
|
|
|
else
|
|
|
|
r_columnate();
|
|
|
|
exit(eval);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void
|
2002-07-28 15:25:15 +00:00
|
|
|
c_columnate(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int chcnt, col, cnt, endcol, numcols;
|
2004-07-29 13:22:56 +00:00
|
|
|
wchar_t **lp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
numcols = termwidth / maxlength;
|
|
|
|
endcol = maxlength;
|
|
|
|
for (chcnt = col = 0, lp = list;; ++lp) {
|
2004-07-29 13:22:56 +00:00
|
|
|
wprintf(L"%ls", *lp);
|
|
|
|
chcnt += width(*lp);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!--entries)
|
|
|
|
break;
|
|
|
|
if (++col == numcols) {
|
|
|
|
chcnt = col = 0;
|
|
|
|
endcol = maxlength;
|
2004-07-29 13:22:56 +00:00
|
|
|
putwchar('\n');
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
2006-12-15 17:47:25 +00:00
|
|
|
while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
|
2004-07-29 13:22:56 +00:00
|
|
|
(void)putwchar('\t');
|
1994-05-27 12:33:43 +00:00
|
|
|
chcnt = cnt;
|
|
|
|
}
|
|
|
|
endcol += maxlength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chcnt)
|
2004-07-29 13:22:56 +00:00
|
|
|
putwchar('\n');
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void
|
2002-07-28 15:25:15 +00:00
|
|
|
r_columnate(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int base, chcnt, cnt, col, endcol, numcols, numrows, row;
|
|
|
|
|
|
|
|
numcols = termwidth / maxlength;
|
|
|
|
numrows = entries / numcols;
|
|
|
|
if (entries % numcols)
|
|
|
|
++numrows;
|
|
|
|
|
|
|
|
for (row = 0; row < numrows; ++row) {
|
|
|
|
endcol = maxlength;
|
|
|
|
for (base = row, chcnt = col = 0; col < numcols; ++col) {
|
2004-07-29 13:22:56 +00:00
|
|
|
wprintf(L"%ls", list[base]);
|
|
|
|
chcnt += width(list[base]);
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((base += numrows) >= entries)
|
|
|
|
break;
|
2006-12-15 17:47:25 +00:00
|
|
|
while ((cnt = roundup(chcnt + 1, TAB)) <= endcol) {
|
2004-07-29 13:22:56 +00:00
|
|
|
(void)putwchar('\t');
|
1994-05-27 12:33:43 +00:00
|
|
|
chcnt = cnt;
|
|
|
|
}
|
|
|
|
endcol += maxlength;
|
|
|
|
}
|
2004-07-29 13:22:56 +00:00
|
|
|
putwchar('\n');
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void
|
2002-07-28 15:25:15 +00:00
|
|
|
print(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int cnt;
|
2004-07-29 13:22:56 +00:00
|
|
|
wchar_t **lp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
for (cnt = entries, lp = list; cnt--; ++lp)
|
2004-07-29 13:22:56 +00:00
|
|
|
(void)wprintf(L"%ls\n", *lp);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _tbl {
|
2004-07-29 13:22:56 +00:00
|
|
|
wchar_t **list;
|
1994-05-27 12:33:43 +00:00
|
|
|
int cols, *len;
|
|
|
|
} TBL;
|
|
|
|
#define DEFCOLS 25
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void
|
2002-07-28 15:25:15 +00:00
|
|
|
maketbl(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
TBL *t;
|
|
|
|
int coloff, cnt;
|
2004-07-29 13:22:56 +00:00
|
|
|
wchar_t *p, **lp;
|
1994-05-27 12:33:43 +00:00
|
|
|
int *lens, maxcols;
|
|
|
|
TBL *tbl;
|
2004-07-29 13:22:56 +00:00
|
|
|
wchar_t **cols;
|
|
|
|
wchar_t *last;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-07-24 14:13:34 +00:00
|
|
|
if ((t = tbl = calloc(entries, sizeof(TBL))) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
2004-07-29 13:22:56 +00:00
|
|
|
if ((cols = calloc((maxcols = DEFCOLS), sizeof(*cols))) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
2001-07-24 14:13:34 +00:00
|
|
|
if ((lens = calloc(maxcols, sizeof(int))) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
|
2004-07-29 13:22:56 +00:00
|
|
|
for (coloff = 0, p = *lp;
|
|
|
|
(cols[coloff] = wcstok(p, separator, &last));
|
1994-05-27 12:33:43 +00:00
|
|
|
p = NULL)
|
|
|
|
if (++coloff == maxcols) {
|
2006-09-19 22:11:43 +00:00
|
|
|
if (!(cols = realloc(cols, ((u_int)maxcols +
|
2015-12-29 11:24:41 +00:00
|
|
|
DEFCOLS) * sizeof(wchar_t *))) ||
|
1994-05-27 12:33:43 +00:00
|
|
|
!(lens = realloc(lens,
|
2006-09-19 22:11:43 +00:00
|
|
|
((u_int)maxcols + DEFCOLS) * sizeof(int))))
|
1994-05-27 12:33:43 +00:00
|
|
|
err(1, NULL);
|
|
|
|
memset((char *)lens + maxcols * sizeof(int),
|
|
|
|
0, DEFCOLS * sizeof(int));
|
|
|
|
maxcols += DEFCOLS;
|
|
|
|
}
|
2004-07-29 13:22:56 +00:00
|
|
|
if ((t->list = calloc(coloff, sizeof(*t->list))) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
2001-07-24 14:13:34 +00:00
|
|
|
if ((t->len = calloc(coloff, sizeof(int))) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (t->cols = coloff; --coloff >= 0;) {
|
|
|
|
t->list[coloff] = cols[coloff];
|
2004-07-29 13:22:56 +00:00
|
|
|
t->len[coloff] = width(cols[coloff]);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (t->len[coloff] > lens[coloff])
|
|
|
|
lens[coloff] = t->len[coloff];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
|
|
|
|
for (coloff = 0; coloff < t->cols - 1; ++coloff)
|
2004-07-29 13:22:56 +00:00
|
|
|
(void)wprintf(L"%ls%*ls", t->list[coloff],
|
|
|
|
lens[coloff] - t->len[coloff] + 2, L" ");
|
|
|
|
(void)wprintf(L"%ls\n", t->list[coloff]);
|
2017-12-28 05:33:44 +00:00
|
|
|
free(t->list);
|
|
|
|
free(t->len);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2017-12-28 05:33:44 +00:00
|
|
|
free(lens);
|
|
|
|
free(cols);
|
|
|
|
free(tbl);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFNUM 1000
|
|
|
|
#define MAXLINELEN (LINE_MAX + 1)
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void
|
2002-07-28 15:25:15 +00:00
|
|
|
input(FILE *fp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
static int maxentry;
|
|
|
|
int len;
|
2004-07-29 13:22:56 +00:00
|
|
|
wchar_t *p, buf[MAXLINELEN];
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (!list)
|
2004-07-29 13:22:56 +00:00
|
|
|
if ((list = calloc((maxentry = DEFNUM), sizeof(*list))) ==
|
2001-07-24 14:13:34 +00:00
|
|
|
NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
2004-07-29 13:22:56 +00:00
|
|
|
while (fgetws(buf, MAXLINELEN, fp)) {
|
|
|
|
for (p = buf; *p && iswspace(*p); ++p);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!*p)
|
|
|
|
continue;
|
2004-07-29 13:22:56 +00:00
|
|
|
if (!(p = wcschr(p, L'\n'))) {
|
1994-05-27 12:33:43 +00:00
|
|
|
warnx("line too long");
|
|
|
|
eval = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2004-07-29 13:22:56 +00:00
|
|
|
*p = L'\0';
|
|
|
|
len = width(buf);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (maxlength < len)
|
|
|
|
maxlength = len;
|
|
|
|
if (entries == maxentry) {
|
|
|
|
maxentry += DEFNUM;
|
|
|
|
if (!(list = realloc(list,
|
2004-07-29 13:22:56 +00:00
|
|
|
(u_int)maxentry * sizeof(*list))))
|
1994-05-27 12:33:43 +00:00
|
|
|
err(1, NULL);
|
|
|
|
}
|
2004-07-29 13:22:56 +00:00
|
|
|
list[entries] = malloc((wcslen(buf) + 1) * sizeof(wchar_t));
|
|
|
|
if (list[entries] == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
wcscpy(list[entries], buf);
|
|
|
|
entries++;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-29 13:22:56 +00:00
|
|
|
/* Like wcswidth(), but ignores non-printing characters. */
|
2011-11-06 08:14:34 +00:00
|
|
|
static int
|
2004-07-29 13:22:56 +00:00
|
|
|
width(const wchar_t *wcs)
|
|
|
|
{
|
|
|
|
int w, cw;
|
|
|
|
|
|
|
|
for (w = 0; *wcs != L'\0'; wcs++)
|
|
|
|
if ((cw = wcwidth(*wcs)) > 0)
|
|
|
|
w += cw;
|
|
|
|
return (w);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:34 +00:00
|
|
|
static void
|
2002-07-28 15:25:15 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
1997-06-30 06:37:15 +00:00
|
|
|
"usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|