From 4596ce3d4773c29be9a5a6ea5fc17a755b1a5c21 Mon Sep 17 00:00:00 2001 From: "Tim J. Robbins" Date: Thu, 25 Apr 2002 23:04:52 +0000 Subject: [PATCH] Some minor nits exposed by the Open Group's VSC Lite verification suite: - When an error occurs processing one file, we must continue to process the remaining files. - Convert trailing whitespace to tabs as appropriate. - Align backspace handling with what the verification suite expects. Reviewed by: mike --- usr.bin/unexpand/unexpand.c | 43 +++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/usr.bin/unexpand/unexpand.c b/usr.bin/unexpand/unexpand.c index 1df6c460f195..37ba49c7d239 100644 --- a/usr.bin/unexpand/unexpand.c +++ b/usr.bin/unexpand/unexpand.c @@ -68,7 +68,8 @@ main(argc, argv) int argc; char *argv[]; { - int ch; + int ch, failed; + char *filename; nstops = 1; tabstops[0] = 8; @@ -89,15 +90,19 @@ main(argc, argv) argc -= optind; argv += optind; - do { - if (argc > 0) { - if (freopen(argv[0], "r", stdin) == NULL) - err(1, "%s", argv[0]); - argc--, argv++; - } + failed = 0; + if (argc == 0) tabify(); - } while (argc > 0); - exit(0); + else { + while ((filename = *argv++) != NULL) { + if (freopen(filename, "r", stdin) == NULL) { + warn("%s", filename); + failed++; + } else + tabify(); + } + } + exit(failed != 0); } static void @@ -116,16 +121,10 @@ tabify() doneline = ocol = dcol = 0; while ((ch = getchar()) != EOF) { - if (ch == '\n') { - putchar('\n'); - doneline = ocol = dcol = 0; - continue; - } else if (ch == ' ' && !doneline) { + if (ch == ' ' && !doneline) { if (++dcol >= limit) doneline = 1; continue; - } else if (ch == '\b' && dcol > 0) { - dcol--; } else if (ch == '\t') { if (nstops == 1) { dcol = (1 + dcol / tabstops[0]) * @@ -168,7 +167,14 @@ tabify() ocol++; } - if (ch != ' ' || dcol > limit) { + if (ch == '\b') { + putchar('\b'); + if (ocol > 0) + ocol--, dcol--; + } else if (ch == '\n') { + putchar('\n'); + doneline = ocol = dcol = 0; + } else if (ch != ' ' || dcol > limit) { putchar(ch); ocol++, dcol++; } @@ -181,7 +187,8 @@ tabify() while ((ch = getchar()) != '\n' && ch != EOF) putchar(ch); if (ch == '\n') - ungetc(ch, stdin); + putchar('\n'); + doneline = ocol = dcol = 0; } } }