Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

column(1): add -l flag #1599

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion usr.bin/column/column.1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd July 29, 2004
.Dd February 18, 2025
.Dt COLUMN 1
.Os
.Sh NAME
Expand All @@ -35,6 +35,7 @@
.Nm
.Op Fl tx
.Op Fl c Ar columns
.Op Fl l Ar tblcols
.Op Fl s Ar sep
.Op Ar
.Sh DESCRIPTION
Expand All @@ -53,6 +54,14 @@ The options are as follows:
Output is formatted for a display
.Ar columns
wide.
.It Fl l
When used with
.Fl t ,
limit the table to
.Ar tblcols
columns in width.
The last column will contain the rest of the line,
including any delimiters.
.It Fl s
Specify a set of characters to be used to delimit columns for the
.Fl t
Expand Down
32 changes: 26 additions & 6 deletions usr.bin/column/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static void usage(void);
static int width(const wchar_t *);

static int termwidth = 80; /* default terminal width */
static int tblcols; /* number of table columns for -t */

static int entries; /* number of records */
static int eval; /* exit value */
Expand Down Expand Up @@ -81,11 +82,14 @@ main(int argc, char **argv)
termwidth = win.ws_col;

tflag = xflag = 0;
while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
while ((ch = getopt(argc, argv, "c:l:s:tx")) != -1)
switch(ch) {
case 'c':
termwidth = atoi(optarg);
break;
case 'l':
tblcols = atoi(optarg);
break;
case 's':
src = optarg;
seplen = mbsrtowcs(NULL, &src, 0, NULL);
Expand All @@ -110,6 +114,9 @@ main(int argc, char **argv)
argc -= optind;
argv += optind;

if (tblcols && !tflag)
errx(1, "the -l flag cannot be used without the -t flag");

if (!*argv)
input(stdin);
else for (; *argv; ++argv)
Expand Down Expand Up @@ -217,7 +224,7 @@ maketbl(void)
int *lens, maxcols;
TBL *tbl;
wchar_t **cols;
wchar_t *last;
wchar_t *s;

if ((t = tbl = calloc(entries, sizeof(TBL))) == NULL)
err(1, NULL);
Expand All @@ -226,9 +233,11 @@ maketbl(void)
if ((lens = calloc(maxcols, sizeof(int))) == NULL)
err(1, NULL);
for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
for (coloff = 0, p = *lp;
(cols[coloff] = wcstok(p, separator, &last));
p = NULL)
for (p = *lp; wcschr(separator, *p); ++p)
;
for (coloff = 0; *p;) {
cols[coloff] = p;

if (++coloff == maxcols) {
if (!(cols = realloc(cols, ((u_int)maxcols +
DEFCOLS) * sizeof(wchar_t *))) ||
Expand All @@ -239,6 +248,16 @@ maketbl(void)
0, DEFCOLS * sizeof(int));
maxcols += DEFCOLS;
}

if ((!tblcols || coloff < tblcols)
&& (s = wcspbrk(p, separator))) {
*s++ = L'\0';
while (*s && wcschr(separator, *s))
++s;
p = s;
} else
break;
}
if ((t->list = calloc(coloff, sizeof(*t->list))) == NULL)
err(1, NULL);
if ((t->len = calloc(coloff, sizeof(int))) == NULL)
Expand Down Expand Up @@ -321,6 +340,7 @@ usage(void)
{

(void)fprintf(stderr,
"usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
"usage: column [-tx] [-c columns] [-l tblcols]"
" [-s sep] [file ...]\n");
exit(1);
}
Loading