Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 3 Aug 2010 22:45:24 GMT
From:      Benjamin Fiedler <bfiedler@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 181810 for review
Message-ID:  <201008032245.o73MjOge068287@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@181810?ac=10

Change 181810 by bfiedler@freebsd-7803 on 2010/08/03 22:45:14

	Support -a flag; --label option in progress

Affected files ...

.. //depot/projects/soc2010/bsdtextproc/diff3/diff3prog.c#4 edit

Differences ...

==== //depot/projects/soc2010/bsdtextproc/diff3/diff3prog.c#4 (text+ko) ====

@@ -76,6 +76,9 @@
 #include <unistd.h>
 #include <limits.h>
 #include <getopt.h>
+#include <errno.h>
+#include <wchar.h>
+#include <wctype.h>
 
 /* diff3 - 3-way differential file comparison */
 
@@ -118,16 +121,18 @@
 int  overlapcnt;
 FILE *fp[3];
 int cline[3];		/* # of the last-read line in each file (0-2) */
+char *diff_prog = NULL;
+char *labels[3] = {NULL};
 /*
  * the latest known correspondence between line numbers of the 3 files
  * is stored in last[1-3];
  */
 int last[4];
-int Aflag, eflag, iflag, Tflag;
+int aflag, Aflag, eflag, iflag, Tflag;
 int oflag;		/* indicates whether to mark overlaps (-E or -X)*/
 int strip_cr;
 int debug  = 0;
-char f1mark[40], f3mark[40];	/* markers for -E and -X */
+char f1mark[40], f2mark[40], f3mark[40];	/* markers for -E and -X */
 
 int duplicate(struct range *, struct range *);
 int edit(struct diff *, int, int);
@@ -139,6 +144,7 @@
 void change(int, struct range *, int);
 void keep(int, struct range *);
 void merge(int, int);
+static int asciifile(FILE *);
 void prange(struct range *);
 void repos(int);
 void separate(const char *);
@@ -159,13 +165,13 @@
 	{ "show-overlap", 	no_argument,		NULL,	'E' },
 	{ "overlap-only",	no_argument,		NULL,	'x' },
 	{ "initial-tab",	no_argument,		NULL,	'T' },
+	{ "text",		no_argument,		NULL,	'a' },
 
 	/* features to implement */
 	{ "show-all",		no_argument,		NULL,	'A' },
 	{ "easy-only", 		no_argument,		NULL,	'3' },
 	{ "merge",		no_argument,		NULL,	'm' },
 	{ "label",		required_argument,	NULL,	'L' },
-	{ "text",		no_argument,		NULL,	'a' },
 	{ "strip-trailing-cr",	no_argument,		NULL,	STRIPCR_OPT },
 	{ "diff-program",	required_argument,	NULL,	DIFFPROG_OPT },
 	{ "version",		no_argument,		NULL,	'v' },
@@ -185,6 +191,9 @@
 		case 'A':
 			Aflag = 1;
 			break;
+		case 'a':
+			aflag = 1;
+			break;
 		case 'E':
 			eflag = 3;
 			oflag = 1;
@@ -195,6 +204,12 @@
 		case 'i':
 			iflag = 1;
 			break;
+		case 'L':
+			oflag = 1;
+			for(i=0; i<3 && labels[i] != NULL; i++);
+			i = i <= 2 ? i : 2 ;
+			labels[i] = optarg;
+			break;
 		case 'T':
 			Tflag = 1;
 			break;
@@ -210,7 +225,9 @@
 		case STRIPCR_OPT:
 			strip_cr = 1;
 			break;
-			
+		case DIFFPROG_OPT:
+			diff_prog = optarg;
+			break;
 		}
 	}
 	argc -= optind;
@@ -221,9 +238,14 @@
 
 	if (oflag) {
 		(void)snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s",
-		    argc >= 6 ? argv[5] : argv[2]);
+		    labels[0] != NULL ? labels[0] :  
+				argc >= 6 ? argv[5] : argv[2]);
+		(void)snprintf(f2mark, sizeof(f2mark), "||||||| %s",
+		    labels[1] != NULL ? labels[1] :
+				argc >= 6 ? argv[4] : argv[3]);
 		(void)snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s",
-		    argc >= 7 ? argv[6] : argv[4]);
+		    labels[2] != NULL ? labels[2] :
+				argc >= 7 ? argv[6] : argv[4]);
 	}
 
 	increase();
@@ -344,6 +366,12 @@
 	d1 = d13;
 	d2 = d23;
 	j = 0;
+
+	if( (asciifile(fp[0]) && asciifile(fp[1]) && asciifile(fp[2]) ) == 0)
+	{
+		printf("Binary file detected; comparison failed\n");
+		exit(EXIT_FAILURE);
+	}
 	while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
 		if (debug) {
 			printf("%d,%d=%d,%d %d,%d=%d,%d\n",
@@ -435,6 +463,29 @@
 		edscript(j);
 }
 
+static int
+asciifile(FILE *f)
+{
+        wint_t   ch = L'\0';
+        size_t   i;
+
+        if (aflag || f == NULL)
+                return (1);
+
+        rewind(f);
+        errno = 0;
+        for (i = 0; i <= BUFSIZ; i++) {
+                if ((ch = fgetwc(f)) == WEOF) {
+                        if (errno == EILSEQ)
+                                return (0);
+                        break;
+                }
+                if (!iswspace(ch) && iswcntrl(ch))
+                        return (0);
+        }
+        return (1);
+}
+
 void
 separate(const char *s)
 {
@@ -506,12 +557,8 @@
 		if ((line = getline(fp[i], &j)) == NULL)
 			trouble();
 		if (pr != NULL)
-		{
-			if(Tflag == 1)
-				printf("\t%s", line);
-			else
-				printf("%s%s", pr, line);
-		}
+			printf("%s%s", Tflag == 1? "\t" : pr, line);
+
 		cline[i]++;
 	}
 	return ((int) n);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201008032245.o73MjOge068287>