From nobody Wed Jan 17 18:28:36 2024 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 4TFZBs1f47z56P4j; Wed, 17 Jan 2024 18:28:37 +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 4TFZBr6MT2z4c84; Wed, 17 Jan 2024 18:28:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705516116; 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=YFxhqtY8rGaiszZlAVTBHSR7PzM+2iDF80TAHVmu0co=; b=DLO7NdcfkN6mZ3llDa9hbTLUNCQ5Vm6SVW9auAWo+Kr2MUx+3ZiIjg+aEnFxHVZA3dYBJ7 0OfMicVKyHoICq91g19wrJtgWNlQGnT5gqCYLgH4mPct+Vguj0nnI50pqVCd0w7ttMT0qA s8YeYsTZEUoWg//MwO+aNu8szoW1RS82n4GtO1QVHsHWM7Qure8RAiO5N6xFju8SmsKD79 cEF3mpXcVoWjf7/zgu1wiUKZoxWeuKa26uhVR96RdKS2NQCPceT23smNjLrnHLdb5oD1fR CMBKVuq5qaFQOLzh5VAjBMDUe2EU6lR7urrU4ScA4P2D6eiQMrtF4rUod7QCNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705516116; 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=YFxhqtY8rGaiszZlAVTBHSR7PzM+2iDF80TAHVmu0co=; b=V/U53rqL15vMvtaE4T75gIzdBJgzEFqbkJ2e+JDFbqFjC0aKq0b/ADGWEiwX36ILd1HnbG +ztib8y6zTW3nS32xPdLdKSDBhZeJjN9Nsr+E+ZN9kfbdU4RCregbNRKNSP7R5zX+FjeBF hCXjBMJgkEkEhtX0h9a7XPumxGTx2RbXlYX9XGl/o3PmSywOiR/frYRQcrZdfF/xqkk6Sg tOD2wSFK8NgFivUAIrVCtvIQgIXP+0YbRAUEP6Is2UDIdrwMAQSvOi+WJMwcqc2uBI6xR/ IsTMd8kzIevs1W/r9O3oR5gCpID1XswEU55cKLHzwMtjxBZQTwNccgBNY+aCKQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705516116; a=rsa-sha256; cv=none; b=wDJhza1uorq7YJKxLDumsFPDmqEqkpBreSTLEUKJ8eE8rCBTI8Ye9/GrCLVGrYvYsqkn1+ KzMa2+3jS/MZ2fB4U061LbVpVmSYZNu6VGyu10SX0tH5dtQIwkpmNvNClrU3dBbTobeDRi nRqihQp3//dc2XREXj+9eq0UdCNAj4X5ps9aBYrduObvutL/sQG9WkK1mXn+mSTVnm5iZM Uy+WCHohwYgTwr07dA5xbtggDVN9lSX22O2ULEbtFE8/wuVhD0iVZdJ7J8UkzkY9Obpsen EgHkEg0rhNIbXfSlP9FQ/RI1JWa0dQd4WOXgsenvhvUKJwoo5uweUS/hLSsGrg== 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 4TFZBr5QzCzZRT; Wed, 17 Jan 2024 18:28:36 +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 40HISaXP063585; Wed, 17 Jan 2024 18:28:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40HISaxP063582; Wed, 17 Jan 2024 18:28:36 GMT (envelope-from git) Date: Wed, 17 Jan 2024 18:28:36 GMT Message-Id: <202401171828.40HISaxP063582@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 93805b27eacc - stable/14 - asa: Rewrite to fix line termination issue. 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 93805b27eacceeccccf545081fdc1c82c8473bff Auto-Submitted: auto-generated The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=93805b27eacceeccccf545081fdc1c82c8473bff commit 93805b27eacceeccccf545081fdc1c82c8473bff Author: Dag-Erling Smørgrav AuthorDate: 2024-01-09 14:09:41 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-01-17 18:28:22 +0000 asa: Rewrite to fix line termination issue. The standard is somewhat unclear, but on the balance, I believe that the phrase “the rest of the input line” should be interpreted to mean the rest of the input line including the terminating newline if and only if there is one. This means the current implementation is incorrect on two points: - First, it suppresses the previous line's newline in the '1' case. - Second, it unconditionally emits a newline at the end of the output for non-empty input, even if the input did not end with a newline. Resolve this by rewriting the main loop. Instead of special-casing the first line and then assuming that every line ends with a newline, we remember how each line ends and emit that either at the beginning of the next line or at the end of the file except in the one case ('+') where the standard explicitly says not to. While here, try to reduce diff to upstream a little and update their RCS tag to reflect the fact that while we've diverged significantly from them, we've incorporated all their changes. Remove the useless second RCS tag. We also update the tests to account for the change in interpretation of the '1' case and add a test case for unterminated input. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D43326 (cherry picked from commit c2356a440db91c106867d45c94b3d6d7bc0e50f0) --- usr.bin/asa/asa.c | 102 +++++++++++++++++++----------------------- usr.bin/asa/tests/asa_test.sh | 15 ++++++- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/usr.bin/asa/asa.c b/usr.bin/asa/asa.c index 757278442a52..9839dbf8a722 100644 --- a/usr.bin/asa/asa.c +++ b/usr.bin/asa/asa.c @@ -1,4 +1,4 @@ -/* $NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $ */ +/* $NetBSD: asa.c,v 1.17 2016/09/05 00:40:28 sevan Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -32,13 +32,8 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#if 0 -#ifndef lint -__RCSID("$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $"); -#endif -#endif #include +#include #include #include #include @@ -50,38 +45,33 @@ static void usage(void) __dead2; int main(int argc, char *argv[]) { - int ch, exval; FILE *fp; - const char *fn; + int ch, exval; while ((ch = getopt(argc, argv, "")) != -1) { switch (ch) { - case '?': default: usage(); - /*NOTREACHED*/ } } argc -= optind; argv += optind; exval = 0; - if (argc == 0) + if (*argv == NULL) { asa(stdin); - else { - while ((fn = *argv++) != NULL) { - if (strcmp(fn, "-") == 0) { + } else { + do { + if (strcmp(*argv, "-") == 0) { asa(stdin); + } else if ((fp = fopen(*argv, "r")) == NULL) { + warn("%s", *argv); + exval = 1; } else { - if ((fp = fopen(fn, "r")) == NULL) { - warn("%s", fn); - exval = 1; - continue; - } asa(fp); fclose(fp); } - } + } while (*++argv != NULL); } if (fflush(stdout) != 0) @@ -93,7 +83,6 @@ main(int argc, char *argv[]) static void usage(void) { - fprintf(stderr, "usage: asa [file ...]\n"); exit(1); } @@ -101,52 +90,53 @@ usage(void) static void asa(FILE *f) { - size_t len; char *buf; + size_t len; + bool eol = false; - if ((buf = fgetln(f, &len)) != NULL) { - if (buf[len - 1] == '\n') - buf[--len] = '\0'; - /* special case the first line */ + while ((buf = fgetln(f, &len)) != NULL) { + /* in all cases but '+', terminate previous line, if any */ + if (buf[0] != '+' && eol) + putchar('\n'); + /* examine and translate the control character */ switch (buf[0]) { + default: + /* + * “It is suggested that implementations treat + * characters other than 0, 1, and '+' as + * in the absence of any compelling reason to do + * otherwise” (POSIX.1-2017) + */ + case ' ': + /* nothing */ + break; case '0': putchar('\n'); break; case '1': putchar('\f'); break; - } - - if (len > 1 && buf[0] && buf[1]) - printf("%.*s", (int)(len - 1), buf + 1); - - while ((buf = fgetln(f, &len)) != NULL) { - if (buf[len - 1] == '\n') - buf[--len] = '\0'; - switch (buf[0]) { - default: - case ' ': - putchar('\n'); - break; - case '0': - putchar('\n'); - putchar('\n'); - break; - case '1': - putchar('\f'); - break; - case '+': + case '+': + /* + * “If the '+' is the first character in the + * input, it shall be equivalent to .” + * (POSIX.1-2017) + */ + if (eol) putchar('\r'); - break; - } - - if (len > 1 && buf[0] && buf[1]) - printf("%.*s", (int)(len - 1), buf + 1); + break; } - - putchar('\n'); + /* trim newline if there is one */ + if ((eol = (buf[len - 1] == '\n'))) + --len; + /* print the rest of the input line */ + if (len > 1 && buf[0] && buf[1]) + fwrite(buf + 1, 1, len - 1, stdout); } - + /* terminate the last line, if any */ + if (eol) + putchar('\n'); + /* check for output errors */ if (ferror(stdout) != 0) err(1, "stdout"); } diff --git a/usr.bin/asa/tests/asa_test.sh b/usr.bin/asa/tests/asa_test.sh index 429342d530e4..91515bb55d95 100644 --- a/usr.bin/asa/tests/asa_test.sh +++ b/usr.bin/asa/tests/asa_test.sh @@ -38,8 +38,8 @@ one_head() { atf_set descr "First character on line is '1'" } one_body() { - printf " %s\n1%s\n" "$a" "$b" >infile - printf "%s\f%s\n" "$a" "$b" >outfile + printf "1%s\n1%s\n" "$a" "$b" >infile + printf "\f%s\n\f%s\n" "$a" "$b" >outfile atf_check_asa infile outfile } @@ -87,6 +87,16 @@ dashdash_body() { atf_check -o inline:"$a $b\n" asa -- -infile } +atf_test_case unterminated +unterminated_head() { + atf_set descr "Unterminated input" +} +unterminated_body() { + printf " %s\n %s" "$a" "$b" >infile + printf "%s\n%s" "$a" "$b" >outfile + atf_check_asa infile outfile +} + atf_init_test_cases() { atf_add_test_case space @@ -96,4 +106,5 @@ atf_init_test_cases() atf_add_test_case plus_top atf_add_test_case stdout atf_add_test_case dashdash + atf_add_test_case unterminated }