Page MenuHomeFreeBSD

D43326.id132332.diff
No OneTemporary

D43326.id132332.diff

diff --git a/usr.bin/asa/asa.c b/usr.bin/asa/asa.c
--- a/usr.bin/asa/asa.c
+++ b/usr.bin/asa/asa.c
@@ -50,13 +50,12 @@
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*/
@@ -101,52 +100,55 @@
static void
asa(FILE *f)
{
- size_t len;
char *buf;
+ size_t len;
+ char eol = 0;
- 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 == '\n')
+ 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 <space>
+ * 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 '+':
- putchar('\r');
+ case '+':
+ if (eol == 0) {
+ /*
+ * “If the '+' is the first character in
+ * the input, it shall be equivalent to
+ * <space>.” (POSIX.1-2017)
+ */
break;
}
-
- if (len > 1 && buf[0] && buf[1])
- printf("%.*s", (int)(len - 1), buf + 1);
+ putchar('\r');
+ break;
}
-
- putchar('\n');
+ /* trim newline if there is one */
+ if ((eol = buf[len - 1]) == '\n')
+ buf[--len] = '\0';
+ /* print the rest of the input line */
+ if (len > 1 && buf[0] && buf[1])
+ printf("%.*s", (int)(len - 1), buf + 1);
}
-
+ /* terminate the last line, if any */
+ if (eol == '\n')
+ 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
--- a/usr.bin/asa/tests/asa_test.sh
+++ b/usr.bin/asa/tests/asa_test.sh
@@ -38,8 +38,8 @@
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 @@
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_add_test_case plus_top
atf_add_test_case stdout
atf_add_test_case dashdash
+ atf_add_test_case unterminated
}

File Metadata

Mime Type
text/plain
Expires
Sun, Feb 16, 6:39 AM (13 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
16670312
Default Alt Text
D43326.id132332.diff (3 KB)

Event Timeline