From nobody Wed May 21 00:30:08 2025 X-Original-To: dev-commits-src-all@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 4b2C4K3b65z5wFbq; Wed, 21 May 2025 00:30:09 +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 4b2C4J74nKz3mbb; Wed, 21 May 2025 00:30:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747787409; 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=YwszrmKP09VSu6NpmUOoFVSM4lRXGCcEskUteUHf3S8=; b=NukmVt5oyHp7+rPSTynqSxkPQivubf7PwEqFPw/PwSAKsWChlpGqGLmlDLmRacq9dV+yOR K3TDiD9L/HoUs4hlIe1I1/cl1U2b5Uf2xCL0p+rWbTQ3cA11aB2NpGs/dRibXdLeh4hLo2 cNZ/QHgFBrPczb7Xea6hBdwn11jeSBPRDnOYZ5IBQ5Xyu8tC/aaX1gxU0hF+chl5LEb/bn I4u5e2uK1ef9VsZ96UM5MocAvNRbMfvIWqbyTaSePurE17wXb1FPygkrgH3WjGVcYxHE0Y Oe4vaN16cWm5sBU9xO/a9rrIAz8lVWlwMUjP3of45UntPXAKXTuWaLYjib5aZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747787409; 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=YwszrmKP09VSu6NpmUOoFVSM4lRXGCcEskUteUHf3S8=; b=nQtqViandkAN6enwjlLU32aygkoKDnSBwVYZpidQ/8f+3FTZW0jVIowUx0vEY1U8/eOfd2 uiBY5wF3TEiTSsP7zwCyE0RY4EgaPYzbomSZevhJs6WVwJrqinnj6ry4N/qcUvWqKVkFr2 ESAEiFdm3rJM6BeKCsCvGc6yicVL7G9EIAbQM4RShD3ehYTdBoYXpDu0P5yPffhAkGmJ0L gVM/8GVH+D+aqDd+rA4EgqJ83afZgsKh5sQ9K9L07i9ntSThmuMg73BJaL9eRX+0e7pMPv CWY0CdxEkULNcShu38LugI3Rf0Lc2zkxAv02ylggmJgZRiXRFqYIeU0M6h+e1g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1747787409; a=rsa-sha256; cv=none; b=lSDR24X/Aij43zxVs1XZRqt1+NVB9OxDUDbBfrqQ64jDM/MSituwLqOMnM0G2hhOPX6Yxg NdWp5tYsqJRaURBQxQY8vPhNNi2pR9SJv2kpSaEvl6M9QQz1+sFqWBHmrcCkhaQt2F07ab NJbGw66ScAKK4QqaVFNuZXYpqyNIlRP0MuRp5QqAjEnIPcOs4R0VN0bhbwsttMCZia91Gi In3yIHMb3INHGDxGAtXBlE2+COqYE1ke/h/kk0r5TjyETu/Ed+DxXnVIINl20YZD5cDRtq K1YfOJD4sEjmwACRqf45XhCrnJ8YMnz2pSbHIi+g4rXOuon++BkhI6KJWf/5WA== 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 4b2C4J6VmXz1BS1; Wed, 21 May 2025 00:30:08 +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 54L0U8Gg026332; Wed, 21 May 2025 00:30:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 54L0U8tf026327; Wed, 21 May 2025 00:30:08 GMT (envelope-from git) Date: Wed, 21 May 2025 00:30:08 GMT Message-Id: <202505210030.54L0U8tf026327@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Lexi Winter Subject: git: c82e2174150a - stable/13 - column(1): add tests and -l flag List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/13 X-Git-Reftype: branch X-Git-Commit: c82e2174150a47cdc5bfa61e6383cfc6343c5389 Auto-Submitted: auto-generated The branch stable/13 has been updated by ivy: URL: https://cgit.FreeBSD.org/src/commit/?id=c82e2174150a47cdc5bfa61e6383cfc6343c5389 commit c82e2174150a47cdc5bfa61e6383cfc6343c5389 Author: Lexi Winter AuthorDate: 2025-05-07 09:27:20 +0000 Commit: Lexi Winter CommitDate: 2025-05-20 21:03:29 +0000 column(1): add tests and -l flag column(1): add tests Reviewed by: des Approved by: des (mentor) Differential Revision: https://reviews.freebsd.org/D49911 (cherry picked from commit 6f2b1b56ac3dd154bd98f5a7ea075abcb4356560) 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 (cherry picked from commit 313713b24c6d2a3061972c4f431515c4f1b01c77) --- etc/mtree/BSD.tests.dist | 2 + usr.bin/column/Makefile | 4 + usr.bin/column/column.1 | 11 ++- usr.bin/column/column.c | 45 +++++++--- usr.bin/column/tests/Makefile | 3 + usr.bin/column/tests/column.sh | 200 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 254 insertions(+), 11 deletions(-) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 4315fe9657c9..fa5c9e910049 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1009,6 +1009,8 @@ .. cmp .. + column + .. compress .. cpio diff --git a/usr.bin/column/Makefile b/usr.bin/column/Makefile index 1c304e2b3927..23933d9ef96f 100644 --- a/usr.bin/column/Makefile +++ b/usr.bin/column/Makefile @@ -1,5 +1,9 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 +.include PROG= column +HAS_TESTS= +SUBDIR.${MK_TESTS}= tests + .include diff --git a/usr.bin/column/column.1 b/usr.bin/column/column.1 index f9b05ccf2210..c9dff361cc9c 100644 --- a/usr.bin/column/column.1 +++ b/usr.bin/column/column.1 @@ -27,7 +27,7 @@ .\" .\" @(#)column.1 8.1 (Berkeley) 6/6/93 .\" -.Dd July 29, 2004 +.Dd May 13, 2025 .Dt COLUMN 1 .Os .Sh NAME @@ -37,6 +37,7 @@ .Nm .Op Fl tx .Op Fl c Ar columns +.Op Fl l Ar tblcols .Op Fl s Ar sep .Op Ar .Sh DESCRIPTION @@ -55,6 +56,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 a808113649bc..97fc83e6b589 100644 --- a/usr.bin/column/column.c +++ b/usr.bin/column/column.c @@ -67,6 +67,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 */ @@ -81,7 +82,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; @@ -94,17 +95,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); @@ -123,6 +133,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) @@ -230,7 +243,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); @@ -239,9 +252,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 *))) || @@ -252,6 +267,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) @@ -332,8 +357,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/Makefile b/usr.bin/column/tests/Makefile new file mode 100644 index 000000000000..40a7767f0dc0 --- /dev/null +++ b/usr.bin/column/tests/Makefile @@ -0,0 +1,3 @@ +ATF_TESTS_SH= column + +.include diff --git a/usr.bin/column/tests/column.sh b/usr.bin/column/tests/column.sh new file mode 100644 index 000000000000..283dc88bff1a --- /dev/null +++ b/usr.bin/column/tests/column.sh @@ -0,0 +1,200 @@ +# SPDX-License-Identifier: ISC +# +# Copyright (c) 2025 Lexi Winter +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +atf_test_case "basic" +basic_head() +{ + atf_set descr "Basic column(1) with default options" +} + +basic_body() +{ + cat >input.1 <input.2 <input.3 <expected <input.1 <input.2 <input.3 <expected <input.1 <input.2 <input.3 <expected <input <expected <input <expected <