git: d458eb8de12c - main - diff3: make the eflag logic easier to follow

From: Tom Jones <thj_at_FreeBSD.org>
Date: Tue, 05 Jul 2022 11:36:39 UTC
The branch main has been updated by thj:

URL: https://cgit.FreeBSD.org/src/commit/?id=d458eb8de12cc656b324f6094e6ed1eedc35625a

commit d458eb8de12cc656b324f6094e6ed1eedc35625a
Author:     Tom Jones <thj@FreeBSD.org>
AuthorDate: 2022-07-05 11:33:55 +0000
Commit:     Tom Jones <thj@FreeBSD.org>
CommitDate: 2022-07-05 11:36:11 +0000

    diff3: make the eflag logic easier to follow
    
    Discussed with: dim
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D35324
---
 usr.bin/diff3/diff3.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c
index 860af98d13e2..514e78c79ed6 100644
--- a/usr.bin/diff3/diff3.c
+++ b/usr.bin/diff3/diff3.c
@@ -112,6 +112,11 @@ struct diff {
 	struct range new;
 };
 
+#define EFLAG_NONE 	0
+#define EFLAG_OVERLAP 	1
+#define EFLAG_NOOVERLAP	2
+#define EFLAG_UNMERGED	3
+
 static size_t szchanges;
 
 static struct diff *d13;
@@ -313,7 +318,7 @@ merge(int m1, int m2)
 		/* first file is different from the others */
 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
 			/* stuff peculiar to 1st file */
-			if (eflag == 0) {
+			if (eflag == EFLAG_NONE) {
 				printf("====1\n");
 				change(1, &d1->old, false);
 				keep(2, &d1->new);
@@ -324,7 +329,7 @@ merge(int m1, int m2)
 		}
 		/* second file is different from others */
 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
-			if (eflag == 0) {
+			if (eflag == EFLAG_NONE) {
 				printf("====2\n");
 				keep(1, &d2->new);
 				change(3, &d2->new, false);
@@ -361,7 +366,7 @@ merge(int m1, int m2)
 			 * dup = 0 means all files differ
 			 * dup = 1 means files 1 and 2 identical
 			 */
-			if (eflag == 0) {
+			if (eflag == EFLAG_NONE) {
 				printf("====%s\n", dup ? "3" : "");
 				change(1, &d1->old, dup);
 				change(2, &d2->old, false);
@@ -530,9 +535,11 @@ repos(int nchar)
 static int
 edit(struct diff *diff, bool dup, int j, int difftype)
 {
-
-	if (((dup + 1) & eflag) == 0)
+	if (!(eflag == EFLAG_UNMERGED ||
+		(!dup && eflag == EFLAG_OVERLAP ) ||
+		(dup && eflag == EFLAG_NOOVERLAP))) {
 		return (j);
+	}
 	j++;
 	overlap[j] = !dup;
 	if (!dup)
@@ -607,7 +614,7 @@ edscript(int n)
 	if (iflag)
 		printf("w\nq\n");
 
-	exit(eflag == 0 ? overlapcnt : 0);
+	exit(eflag == EFLAG_NONE ? overlapcnt : 0);
 }
 
 /*
@@ -825,13 +832,13 @@ main(int argc, char **argv)
 	struct kevent *e;
 
 	nblabels = 0;
-	eflag = 0;
+	eflag = EFLAG_NONE;
 	oflag = 0;
 	diffargv[diffargc++] = __DECONST(char *, diffprog);
 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
 		switch (ch) {
 		case '3':
-			eflag = 2;
+			eflag = EFLAG_NOOVERLAP;
 			break;
 		case 'a':
 			diffargv[diffargc++] = __DECONST(char *, "-a");
@@ -840,10 +847,10 @@ main(int argc, char **argv)
 			Aflag = 1;
 			break;
 		case 'e':
-			eflag = 3;
+			eflag = EFLAG_UNMERGED;
 			break;
 		case 'E':
-			eflag = 3;
+			eflag = EFLAG_UNMERGED;
 			oflag = 1;
 			break;
 		case 'i':
@@ -864,11 +871,11 @@ main(int argc, char **argv)
 			Tflag = 1;
 			break;
 		case 'x':
-			eflag = 1;
+			eflag = EFLAG_OVERLAP;
 			break;
 		case 'X':
 			oflag = 1;
-			eflag = 1;
+			eflag = EFLAG_OVERLAP;
 			break;
 		case DIFFPROG_OPT:
 			diffprog = optarg;
@@ -889,7 +896,7 @@ main(int argc, char **argv)
 	argv += optind;
 
 	if (Aflag) {
-		eflag = 3;
+		eflag = EFLAG_UNMERGED;
 		oflag = 1;
 	}