From nobody Mon Jul 03 15:14:55 2023 X-Original-To: dev-commits-src-branches@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 4QvqGm1r52z4lDRC; Mon, 3 Jul 2023 15:14:56 +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 4QvqGm18JVz44pK; Mon, 3 Jul 2023 15:14:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688397296; 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=fQJHjnDxIAD/lodPRoO6G3TZZyk0OCcxIb5ZdKSMOGc=; b=hWxVi7wYVdibxvbxoz+vAUvdEvkaW4SoZ3YOv9FUgLGwSEaPbzeBgYgbulkfn3VWZUxITp ImqjyleXcgtN4vvtWzR5TwIBNbbMCnOrrTk+AGO2VstHmirBfpXr8oNwTn5G9EjdvUYK/S QGMMWJkmz7xpWNQPzMhvz8pcBsAVzlPr0vOiXF4P3cNah4zaDnrkfllJvRZWPNN5q9RhhR QhpGkd92HdCqFFs0JEKMvM8AX3VjU/OiCBASLx9TRQ4YRxVyyhCNKm/tkuVie3jv5bDVqo PjEBH63wbv2EzYbw1FZ8kfm1NlQA6uP6wYuhTCySIefBmPtM/6Sl4LzVq0n6Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688397296; 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=fQJHjnDxIAD/lodPRoO6G3TZZyk0OCcxIb5ZdKSMOGc=; b=yJcCPsBGFPGVsDO5N2vCnYYRNAIqEV3ZYzJYo7U3S2A0XZPA2UIud80gaFeqyzwLtF5c/o gygPnzh4sVX4RnNR2AFY35pVTNkVveewqHSqlmQO6vAchjnJyH9V1YWpVv0w5rsRZU2tNZ IOhCd+nc+d1MWeA563yikvz1rqw5PPevU5mUupcTBarkT0l9jitk31KulZDwgCYMCOFs8a gmuh765B1AEvmyAqyCN5T3GrMRkK23ML8YRNRuHVlnmam7rvlfGTjjpzGUuIDqvnWlK9u6 wjUfiKGBYn32y83OFseKnfk2w0k+lZubc35+sHZ9XMAdLuaFJCkR32kBrVIQDg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1688397296; a=rsa-sha256; cv=none; b=t7aBl0zpNGFhEOzxZvojoNzw1vJphpHiB2FpaBEOl418xYlu3skUGimhceU3by2XbNcyrh rK0qt/AxhkYNIBTukLrWs+zthl0QaiDjpKW+NwSvYwWMyZ74b3FlgvOCHsAuqGBHudDaJw gmVdK8rg4X6hPHJWwvsbc6tv/ancxPZV6p308GyWf83xaHfy9g0rtcgD6GThOkfIfpfu7J ctWFtjURNzUsOwf/MNjMvU1cuGZINEQNyk1NT2zZ15b3dLWf5QcuvU/1La393RMeDUDOMl 1zipKn2y5wCZ09EjAsXqaIbWM+PY0tAVQhDQyp8EtN/+/sKqa9oqeYn1OuQctg== 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 4QvqGm0BlwzvKC; Mon, 3 Jul 2023 15:14:56 +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 363FEtIn017852; Mon, 3 Jul 2023 15:14:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 363FEtrp017851; Mon, 3 Jul 2023 15:14:55 GMT (envelope-from git) Date: Mon, 3 Jul 2023 15:14:55 GMT Message-Id: <202307031514.363FEtrp017851@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: 77d9e708c07b - stable/13 - asa: Error out if writing to stdout failed. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/13 X-Git-Reftype: branch X-Git-Commit: 77d9e708c07b7bbbebc8e729f8fb339fb99c2d99 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=77d9e708c07b7bbbebc8e729f8fb339fb99c2d99 commit 77d9e708c07b7bbbebc8e729f8fb339fb99c2d99 Author: Dag-Erling Smørgrav AuthorDate: 2023-06-15 19:23:19 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-07-03 15:13:11 +0000 asa: Error out if writing to stdout failed. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D40562 (cherry picked from commit 9e379f9639d51442ad1d5e1800c687ef2c954901) asa: Read from stdin if *argv is "-". MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D40563 (cherry picked from commit f08f90e6987775f88d25efbd8762c361819f40ba) asa: Add some unit tests. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D40564 (cherry picked from commit b7412da2e122d9274341ef840c6918409bc523b3) --- etc/mtree/BSD.tests.dist | 2 + usr.bin/asa/Makefile | 5 ++- usr.bin/asa/asa.c | 25 ++++++++--- usr.bin/asa/tests/Makefile | 4 ++ usr.bin/asa/tests/asa_test.sh | 99 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 9 deletions(-) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index eb62812bfcd8..35d8e9505b9a 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -886,6 +886,8 @@ usr.bin apply .. + asa + .. awk .. basename diff --git a/usr.bin/asa/Makefile b/usr.bin/asa/Makefile index c2a221ae027b..a29db0f31781 100644 --- a/usr.bin/asa/Makefile +++ b/usr.bin/asa/Makefile @@ -1,6 +1,7 @@ -# $NetBSD: Makefile,v 1.2 1995/03/25 18:04:51 glass Exp $ -# $FreeBSD$ +.include PROG= asa +HAS_TESTS= +SUBDIR.${MK_TESTS}= tests .include diff --git a/usr.bin/asa/asa.c b/usr.bin/asa/asa.c index 22174ed340cd..a6c3d7d7c1e5 100644 --- a/usr.bin/asa/asa.c +++ b/usr.bin/asa/asa.c @@ -43,6 +43,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include static void asa(FILE *); @@ -71,16 +72,23 @@ main(int argc, char *argv[]) asa(stdin); else { while ((fn = *argv++) != NULL) { - if ((fp = fopen(fn, "r")) == NULL) { - warn("%s", fn); - exval = 1; - continue; - } - asa(fp); - fclose(fp); + if (strcmp(fn, "-") == 0) { + asa(stdin); + } else { + if ((fp = fopen(fn, "r")) == NULL) { + warn("%s", fn); + exval = 1; + continue; + } + asa(fp); + fclose(fp); + } } } + if (fflush(stdout) != 0) + err(1, "stdout"); + exit(exval); } @@ -140,4 +148,7 @@ asa(FILE *f) putchar('\n'); } + + if (ferror(stdout) != 0) + err(1, "stdout"); } diff --git a/usr.bin/asa/tests/Makefile b/usr.bin/asa/tests/Makefile new file mode 100644 index 000000000000..c8c0cde1b3a2 --- /dev/null +++ b/usr.bin/asa/tests/Makefile @@ -0,0 +1,4 @@ +PACKAGE= tests +ATF_TESTS_SH= asa_test + +.include diff --git a/usr.bin/asa/tests/asa_test.sh b/usr.bin/asa/tests/asa_test.sh new file mode 100644 index 000000000000..429342d530e4 --- /dev/null +++ b/usr.bin/asa/tests/asa_test.sh @@ -0,0 +1,99 @@ +# +# Copyright (c) 2023 Klara, Inc. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +a="The magic words are" +b="Squeamish Ossifrage" + +atf_check_asa() { + atf_check -o file:"$2" asa "$1" + atf_check -o file:"$2" asa <"$1" + atf_check -o file:"$2" asa - <"$1" +} + +atf_test_case space +space_head() { + atf_set descr "First character on line is ' '" +} +space_body() { + printf " %s\n %s\n" "$a" "$b" >infile + printf "%s\n%s\n" "$a" "$b" >outfile + atf_check_asa infile outfile +} + +atf_test_case zero +zero_head() { + atf_set descr "First character on line is '0'" +} +zero_body() { + printf " %s\n0%s\n" "$a" "$b" >infile + printf "%s\n\n%s\n" "$a" "$b" >outfile + atf_check_asa infile outfile +} + +atf_test_case one +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 + atf_check_asa infile outfile +} + +atf_test_case plus +plus_head() { + atf_set descr "First character on line is '+'" +} +plus_body() { + printf " %s\n+%s\n" "$a" "$b" >infile + printf "%s\r%s\n" "$a" "$b" >outfile + atf_check_asa infile outfile +} + +atf_test_case plus_top +plus_top_head() { + atf_set descr "First character in input is '+'" +} +plus_top_body() { + printf "+%s\n+%s\n" "$a" "$b" >infile + printf "%s\r%s\n" "$a" "$b" >outfile + atf_check_asa infile outfile +} + +atf_test_case stdout +stdout_head() { + atf_set descr "Failure to write to stdout" +} +stdout_body() { + ( + trap "" PIPE + echo " $a $b" | asa 2>stderr + echo $? >result + ) | true + atf_check -o inline:"1\n" cat result + atf_check -o match:"stdout" cat stderr +} + +atf_test_case dashdash +dashdash_head() { + atf_set descr "Use -- to end options" +} +dashdash_body() { + echo " $a $b" >-infile + atf_check -s not-exit:0 -e match:"illegal option" asa -infile + atf_check -o inline:"$a $b\n" asa -- -infile +} + +atf_init_test_cases() +{ + atf_add_test_case space + atf_add_test_case zero + atf_add_test_case one + atf_add_test_case plus + atf_add_test_case plus_top + atf_add_test_case stdout + atf_add_test_case dashdash +}