Fix a handful of bugs in unexpand(1): trunk
authordyoung <dyoung@NetBSD.org>
Fri, 13 Sep 2019 16:53:05 +0000
branchtrunk
changeset 454331 661d910e0fd7
parent 454330 029f0939d2ef
child 454332 b84f030acc95
Fix a handful of bugs in unexpand(1): 1. -a and -t were mutually exclusive when they should not be. 2. `unexpand -t n` did not treat a file like there were stops at n, 2 n, 3 n, 4 n, and so on. So expanded tabs after column 4 were not collapsed. 3. a debug fprintf wrote every tabstop set with `-t` to the standard error stream. TBD write some tests.
usr.bin/unexpand/unexpand.c
--- a/usr.bin/unexpand/unexpand.c	Fri Sep 13 14:19:13 2019 +0000
+++ b/usr.bin/unexpand/unexpand.c	Fri Sep 13 16:53:05 2019 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: unexpand.c,v 1.15 2016/02/03 05:32:14 christos Exp $	*/
+/*	$NetBSD: unexpand.c,v 1.16 2019/09/13 16:53:05 dyoung Exp $	*/
 
 /*-
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)unexpand.c	8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: unexpand.c,v 1.15 2016/02/03 05:32:14 christos Exp $");
+__RCSID("$NetBSD: unexpand.c,v 1.16 2019/09/13 16:53:05 dyoung Exp $");
 #endif /* not lint */
 
 /*
@@ -86,13 +86,9 @@
 	while ((c = getopt(argc, argv, "at:")) != -1) {
 		switch (c) {
 		case 'a':
-			if (nstops)
-				usage();
-			all++;
+			all = 1;
 			break;
 		case 't':
-			if (all)
-				usage();
 			while ((tab = strsep(&optarg, ", \t")) != NULL) {
 				if (*tab == '\0')
 					continue;
@@ -121,9 +117,6 @@
 	argc -= optind;
 	argv += optind;
 
-	for (i = 0; i < nstops; i++)
-		fprintf(stderr, "%lu %zu\n", i, tabstops[i]);
-
 	do {
 		if (argc > 0) {
 			if (freopen(argv[0], "r", stdin) == NULL)
@@ -150,8 +143,11 @@
 			dcol++;
 			continue;
 		} else if (*p == '\t') {
-			if (nstops == 0) {
-				dcol = (1 + dcol / DSTOP) * DSTOP;
+			if (nstops <= 1) {
+				size_t stop = (nstops == 0)
+				    ? DSTOP
+				    : tabstops[0];
+				dcol = (1 + dcol / stop) * stop;
 				continue;
 			} else {
 				for (n = 0; n < nstops &&
@@ -165,13 +161,16 @@
 		}
 
 		/* Output our tabs */
-		if (nstops == 0) {
-			while (((ocol + DSTOP) / DSTOP) <= (dcol / DSTOP)) {
+		if (nstops <= 1) {
+			size_t stop = (nstops == 0)
+			    ? DSTOP
+			    : tabstops[0];
+			while (((ocol + stop) / stop) <= (dcol / stop)) {
 				if (dcol - ocol < 2)
 					break;
 				if (putchar('\t') == EOF)
 					goto out;
-				ocol = (1 + ocol / DSTOP) * DSTOP;
+				ocol = (1 + ocol / stop) * stop;
 			}
 		} else {
 			for (n = 0; n < nstops && tabstops[n] <= ocol; n++)