From nobody Tue May 13 10:41:57 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4ZxY1x2MNcz5wg9X; Tue, 13 May 2025 10:41:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZxY1x1P1Bz3Z6X; Tue, 13 May 2025 10:41:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747132917; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=GEj/nC5DTcebvqWYtQqD6fjI2M48JgbP6yVfgAaRaLo=; b=b8aoFjjLEwACs0/Kz86HKlFAlnhi+miX8QVvDoTl6WW86Qwox+hewdgl6vsTNgWPSMEvvK vG+zrIu9H6saECrnsECGiOvlDPbePRev1yHmVy1GEh0UxuNyK3IBjtN2Be3/6Mhx9+bjGw Qxxi91wmxf1BNEoLCOyDIDpNMG+CBcBxcJhpf5KkCDIfTj7vrbPm8meuhbmx6I07Sc6uEO uFOWl2yDmoOH5UEN7BRfKhtCfuKNP5uNsbBylOgzUdNVT0bSF7AOng/X0wgxz3dyW91gJU lQxiwzFnxlERYQoNc0eme9ojVu8gOB6iTpZJboLJOqoT4VEa6z073aknCnrYGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747132917; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=GEj/nC5DTcebvqWYtQqD6fjI2M48JgbP6yVfgAaRaLo=; b=CsIlNKh7WlUxSBWyit12VHF9840wI2tTBVoZOqAiYwMicW1NxwEIPiGsZY0kSO5V9Fk6Kq Guc9/MAEPdN7bDtYWLUv+YHpnp6Ej6Eo7RN/bfCb04lIoMU89moKp8TpN8I8JQgfzyPriB eiGzpSlXBUTrM2LX6oiIbXjtYRtYNcZtXrJizaj9Ha94Rl/F/NKk9CLXY0ofAE6KRuHCny hgqapMd2uEdOnXcHXbVI6TihXv9w6mKb15Mq3AHc3fWQOi0Z8eKosoQzvK/+ZU328iR8v9 oKuNE7f1MHBec8A1bT8R48P5JBVB0o8utTz3XY1fUwqerg3scoUMmEQveTqu5A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1747132917; a=rsa-sha256; cv=none; b=uAjYCYse+cSC9qxiftsFOMGa3ML2vlBYPc8lsKo1Dc/8s+blegSMTUTP2Zrm/5IIrhwXbn bpZ+iIBLJOizOy71W1pQ9otjZazTq3D2QP/KhIYwblxKCfXFi55ip1YxqK7lGGmxdnLXip M/ArkUmG+vZSMIsoYpgL/23BK5ilo9MOpLZEZ7Bx4M1d6iysO3py0Um+EGL9TELnJbp4Ng b5dzbHA8/U3sh+jVhyCund7+RHzPW+TjMPGlPknHuB1SjRtYwF9S21VsukFUS8IbSl5NE5 /D9lWoHvxCD1JaM5loSVPNPUIR17CbRYi5G4y2KOMMrR0EVBTs1iwtHAQBA2pQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZxY1x0yGbzk4R; Tue, 13 May 2025 10:41:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 54DAfv4h044148; Tue, 13 May 2025 10:41:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 54DAfv0Y044145; Tue, 13 May 2025 10:41:57 GMT (envelope-from git) Date: Tue, 13 May 2025 10:41:57 GMT Message-Id: <202505131041.54DAfv0Y044145@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Lexi Winter Subject: git: 313713b24c6d - main - column(1): add -l flag List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: ivy X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 313713b24c6d2a3061972c4f431515c4f1b01c77 Auto-Submitted: auto-generated The branch main has been updated by ivy: URL: https://cgit.FreeBSD.org/src/commit/?id=313713b24c6d2a3061972c4f431515c4f1b01c77 commit 313713b24c6d2a3061972c4f431515c4f1b01c77 Author: Lexi Winter AuthorDate: 2025-05-13 10:28:59 +0000 Commit: Lexi Winter CommitDate: 2025-05-13 10:33:08 +0000 column(1): add -l flag the '-l ' flag limits the number of columns that column(1) will produce in -t mode. this is syntax-compatible with the same option in util-linux's column(1), but due to existing differences between the two implementations, it's not semantically compatible. as a side-effect, fix a pre-existing bug where empty fields could cause incorrect output: % echo ':' | column -ts: (null) while here, also fix a couple of minor existing issues. Reviewed by: des Approved by: des (mentor) Differential Revision: https://reviews.freebsd.org/D50290 --- usr.bin/column/column.1 | 11 ++++++++++- usr.bin/column/column.c | 45 ++++++++++++++++++++++++++++++++---------- usr.bin/column/tests/column.sh | 30 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/usr.bin/column/column.1 b/usr.bin/column/column.1 index 4481fccf2c3d..1baf8a916e55 100644 --- a/usr.bin/column/column.1 +++ b/usr.bin/column/column.1 @@ -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 May 13, 2025 .Dt COLUMN 1 .Os .Sh NAME @@ -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 @@ -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 diff --git a/usr.bin/column/column.c b/usr.bin/column/column.c index d1322312f8fa..aa9e38d560e4 100644 --- a/usr.bin/column/column.c +++ b/usr.bin/column/column.c @@ -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 */ @@ -68,7 +69,7 @@ main(int argc, char **argv) FILE *fp; int ch, tflag, xflag; char *p; - const char *src; + const char *errstr, *src; wchar_t *newsep; size_t seplen; @@ -81,17 +82,26 @@ 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); + termwidth = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr != NULL) + errx(1, "invalid terminal width \"%s\": %s", + optarg, errstr); + break; + case 'l': + tblcols = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr != NULL) + errx(1, "invalid max width \"%s\": %s", + optarg, errstr); break; case 's': src = optarg; seplen = mbsrtowcs(NULL, &src, 0, NULL); if (seplen == (size_t)-1) err(1, "bad separator"); - newsep = malloc((seplen + 1) * sizeof(wchar_t)); + newsep = calloc(seplen + 1, sizeof(wchar_t)); if (newsep == NULL) err(1, NULL); mbsrtowcs(newsep, &src, seplen + 1, NULL); @@ -110,6 +120,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) @@ -217,7 +230,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); @@ -226,9 +239,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) + /* nothing */ ; + for (coloff = 0; *p;) { + cols[coloff] = p; + if (++coloff == maxcols) { if (!(cols = realloc(cols, ((u_int)maxcols + DEFCOLS) * sizeof(wchar_t *))) || @@ -239,6 +254,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) @@ -319,8 +344,8 @@ width(const wchar_t *wcs) static void 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); } diff --git a/usr.bin/column/tests/column.sh b/usr.bin/column/tests/column.sh index c7c6502098d4..283dc88bff1a 100644 --- a/usr.bin/column/tests/column.sh +++ b/usr.bin/column/tests/column.sh @@ -144,6 +144,7 @@ seven.:eight.:nine ::zwei drei.. vier: +: END @@ -161,10 +162,39 @@ END atf_check diff expected output } +atf_test_case "ncols" +ncols_head() +{ + atf_set descr "column(1) with -t (table) and -s and -l options" +} + +ncols_body() +{ + cat >input <expected <