From nobody Sun Feb 18 17:41:39 2024 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 4TdCdw2Z6qz5BJV7; Sun, 18 Feb 2024 17:41:40 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TdCdw0SgFz4WGl; Sun, 18 Feb 2024 17:41:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708278100; 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=H7bcNdLBhMn4I2cYhDEBY0dzYZLeyoqrjJDOuRU/WEQ=; b=ris007VhP44oBpdYoBUj9u7EiGx6thNb9YaFwPt52ZN/HTkdhA6qyZBQnPOpp30kk2zXUT cbKPriBk7Tc4Vpa4k3PAKbyMSvz4eufB+vb4tRatpOKeas4w6wrTaMiwic7r5tuR2KE4J4 kWHWFLVVZyzogkALV9LRp+e48dMiLybjjAQpFzLicsbdKzhC3/HItn+wdVskf+6lCb2WdN gNdUiRQoNAClaUZiIVAddm59C5t8AifCKIb/sTmv1PuijA1otsel/XCX1cVAyxxMONwflJ 1JwDm67LL+9K1GELiP4pXQ+ZBXV8ULVIWdiXLTS32V0zt0MzMACW/ffEbkRcbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708278100; 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=H7bcNdLBhMn4I2cYhDEBY0dzYZLeyoqrjJDOuRU/WEQ=; b=uypmlCtIYxxHsWWwi/wyzKL3Tc74D/8aKur/88ewtpTQsWyaHdJwK0v4ui0DIXPpj5cOSz r38tQZiUH7PQmnd2nhAdpm/dX9yYL/qdiba1+gQfHeAtCoD6Yai+ZtcjQI4oiuFlR+EpyM StuPUdJrQo51OYeNHoSS4Cc3Jdor7avyF8bDZe1OyRDSgm5qe0TCsdvFyjF37isuBJW6hI 4M7aod3Ak8FTSC7hWWkM5YCAhk2CZX2my/PxfyTki49Ai4S4lMVL6BS3397xQq5J28/BUu xNLs8P6isfNgb5+Z+ozYh1uwux7EkY+F2f92eyNSX9nQYXlP/BEz56WcB8Ibrg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1708278100; a=rsa-sha256; cv=none; b=nCUOeuUoH+2dKZMomgakPpOvAwc5QbNYbmRQxIlS+Fsw01/IR7A09aGqYHaeR6XrS7S97q gkijwH3BPIadOEVP/F8q9T92dQ/5KGHpQrGQMEnuxwJjoa//zPhYO3eHHFulLzgrc4/JX3 qx1uezrqf8VjXEh068SV0b9fOzOMd4HgoPvZWNay8c+chLQt2LiiscOhvQcZjbT9/SsDYK c5M/O8uUNBG5lDD/jAzSJrSksk6Tyq15yd9D6iLO4f0DE8T4ow04KrrNkZjlqG01p1OSf0 5gSaAvufhgz8MVI18MI0exdPy0Pax6d1GHLuzZfiVJO/EAKrxZY8Bwohk8YDUg== 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 4TdCdv6KDnz114J; Sun, 18 Feb 2024 17:41:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41IHfdsE060685; Sun, 18 Feb 2024 17:41:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41IHfdSa060682; Sun, 18 Feb 2024 17:41:39 GMT (envelope-from git) Date: Sun, 18 Feb 2024 17:41:39 GMT Message-Id: <202402181741.41IHfdSa060682@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: a834edfccd14 - main - sdiff: Fix --expand-tabs and --tabsize. 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a834edfccd14a8c0f152a3b0078469af8e05f3fd Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=a834edfccd14a8c0f152a3b0078469af8e05f3fd commit a834edfccd14a8c0f152a3b0078469af8e05f3fd Author: Dag-Erling Smørgrav AuthorDate: 2024-02-18 17:39:28 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-02-18 17:39:50 +0000 sdiff: Fix --expand-tabs and --tabsize. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D43941 --- usr.bin/sdiff/sdiff.1 | 4 +--- usr.bin/sdiff/sdiff.c | 32 ++++++++++++++++++++++++-------- usr.bin/sdiff/tests/sdiff_test.sh | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/usr.bin/sdiff/sdiff.1 b/usr.bin/sdiff/sdiff.1 index ef9bb95a0990..ca6594c6479a 100644 --- a/usr.bin/sdiff/sdiff.1 +++ b/usr.bin/sdiff/sdiff.1 @@ -3,7 +3,7 @@ .\" Written by Raymond Lai . .\" Public domain. .\" -.Dd April 8, 2017 +.Dd February 16, 2024 .Dt SDIFF 1 .Os .Sh NAME @@ -117,8 +117,6 @@ Ignore all spaces. Ignore blank lines. .It Fl E -ignore-tab-expansion Treat tabs and eight spaces as the same. -.It Fl t -ignore-tabs -Ignore tabs. .It Fl H -speed-large-files Assume scattered small changes in a large file. .It Fl -ignore-file-name-case diff --git a/usr.bin/sdiff/sdiff.c b/usr.bin/sdiff/sdiff.c index c6a4c5f9c477..41284d54f869 100644 --- a/usr.bin/sdiff/sdiff.c +++ b/usr.bin/sdiff/sdiff.c @@ -71,6 +71,8 @@ static size_t file1ln, file2ln; /* line number of file1 and file2 */ static bool Iflag; /* ignore sets matching regexp */ static bool lflag; /* print only left column for identical lines */ static bool sflag; /* skip identical lines */ +static bool tflag; /* expand tabs */ +static int tabsize = 8; /* tab size */ FILE *outfp; /* file to save changes to */ const char *tmpdir; /* TMPDIR or /tmp */ @@ -126,7 +128,7 @@ static const char *help_msg[] = { "\t-d, --minimal: minimize diff size.", "\t-I RE, --ignore-matching-lines=RE: ignore changes whose line matches RE.", "\t-i, --ignore-case: do a case-insensitive comparison.", - "\t-t, --expand-tabs: sxpand tabs to spaces.", + "\t-t, --expand-tabs: expand tabs to spaces.", "\t-W, --ignore-all-spaces: ignore all spaces.", "\t--speed-large-files: assume large file with scattered changes.", "\t--strip-trailing-cr: strip trailing carriage return.", @@ -246,7 +248,6 @@ main(int argc, char **argv) case FCASE_IGNORE_OPT: case FCASE_SENSITIVE_OPT: case STRIPCR_OPT: - case TSIZE_OPT: case 'S': break; /* combine no-arg single switches */ @@ -256,7 +257,6 @@ main(int argc, char **argv) case 'd': case 'E': case 'i': - case 't': case 'W': flagc++; flagv = realloc(flagv, flagc + 2); @@ -286,6 +286,9 @@ main(int argc, char **argv) case 's': sflag = true; break; + case 't': + tflag = true; + break; case 'w': wval = strtonum(optarg, WIDTH_MIN, INT_MAX, &errstr); @@ -297,6 +300,11 @@ main(int argc, char **argv) printf("%s\n", help_msg[i]); exit(0); break; + case TSIZE_OPT: + tabsize = strtonum(optarg, 1, INT_MAX, &errstr); + if (errstr) + errx(2, "tabsize is %s: %s", errstr, optarg); + break; default: usage(); break; @@ -511,11 +519,11 @@ printcol(const char *s, size_t *col, const size_t col_max) * If rounding to next multiple of eight causes * an integer overflow, just return. */ - if (*col > SIZE_MAX - 8) + if (*col > SIZE_MAX - tabsize) return; /* Round to next multiple of eight. */ - new_col = (*col / 8 + 1) * 8; + new_col = (*col / tabsize + 1) * tabsize; /* * If printing the tab goes past the column @@ -523,12 +531,20 @@ printcol(const char *s, size_t *col, const size_t col_max) */ if (new_col > col_max) return; - *col = new_col; + + if (tflag) { + do { + putchar(' '); + } while (++*col < new_col); + } else { + putchar(*s); + *col = new_col; + } break; default: - ++(*col); + ++*col; + putchar(*s); } - putchar(*s); } } diff --git a/usr.bin/sdiff/tests/sdiff_test.sh b/usr.bin/sdiff/tests/sdiff_test.sh index 100fa1b123b0..c701c1704d3c 100755 --- a/usr.bin/sdiff/tests/sdiff_test.sh +++ b/usr.bin/sdiff/tests/sdiff_test.sh @@ -191,6 +191,23 @@ short_body() $(atf_get_srcdir)/d_input2 >/dev/null ; cat merge.out" } +atf_test_case tflag +tflag_head() +{ + atf_set "descr" "Checks tab expansion" +} +tflag_body() +{ + printf "a\tb\n" >a + printf "b\ta\n" >b + atf_check -s exit:1 -o match:$'a\tb' \ + sdiff a b + atf_check -s exit:1 -o match:"a {7}b" \ + sdiff -t a b + atf_check -s exit:1 -o match:"a {3}b" \ + sdiff -t --tabsize 4 a b +} + atf_init_test_cases() { atf_add_test_case flags @@ -203,4 +220,5 @@ atf_init_test_cases() atf_add_test_case dot atf_add_test_case stdin atf_add_test_case short + atf_add_test_case tflag }