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,152 +1,142 @@ -/* $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 * * Copyright (c) 1993,94 Winning Strategies, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Winning Strategies, Inc. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * 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 #include static void asa(FILE *); 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) err(1, "stdout"); exit(exval); } static void usage(void) { - fprintf(stderr, "usage: asa [file ...]\n"); exit(1); } 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 @@ -1,99 +1,110 @@ # # 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 + printf "1%s\n1%s\n" "$a" "$b" >infile + printf "\f%s\n\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_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 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 + atf_add_test_case unterminated }