2017-05-25 17:45:50 +00:00
|
|
|
/* $OpenBSD: diff3prog.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Caldera International Inc. 2001-2002.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code and documentation must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed or owned by Caldera
|
|
|
|
* International, Inc.
|
|
|
|
* 4. Neither the name of Caldera International, Inc. nor the names of other
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
|
|
|
|
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)diff3.c 8.1 (Berkeley) 6/6/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)diff3.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
|
|
|
#endif /* not lint */
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
#include <sys/procdesc.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <capsicum_helpers.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-12-17 09:13:48 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <inttypes.h>
|
2017-05-25 17:45:50 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "from" is first in range of changed lines; "to" is last+1
|
|
|
|
* from=to=line after point of insertion for added lines.
|
|
|
|
*/
|
|
|
|
struct range {
|
|
|
|
int from;
|
|
|
|
int to;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct diff {
|
2022-04-19 14:43:35 +00:00
|
|
|
#define DIFF_TYPE2 2
|
|
|
|
#define DIFF_TYPE3 3
|
|
|
|
int type;
|
|
|
|
#if DEBUG
|
|
|
|
char *line;
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/* Ranges as lines */
|
2017-05-25 17:45:50 +00:00
|
|
|
struct range old;
|
|
|
|
struct range new;
|
|
|
|
};
|
|
|
|
|
2022-07-05 11:33:55 +00:00
|
|
|
#define EFLAG_NONE 0
|
|
|
|
#define EFLAG_OVERLAP 1
|
|
|
|
#define EFLAG_NOOVERLAP 2
|
|
|
|
#define EFLAG_UNMERGED 3
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
static size_t szchanges;
|
|
|
|
|
|
|
|
static struct diff *d13;
|
|
|
|
static struct diff *d23;
|
|
|
|
/*
|
|
|
|
* "de" is used to gather editing scripts. These are later spewed out in
|
2022-04-19 13:47:07 +00:00
|
|
|
* reverse order. Its first element must be all zero, the "old" and "new"
|
|
|
|
* components of "de" contain line positions. Array overlap indicates which
|
|
|
|
* sections in "de" correspond to lines that are different in all three files.
|
2017-05-25 17:45:50 +00:00
|
|
|
*/
|
|
|
|
static struct diff *de;
|
|
|
|
static char *overlap;
|
|
|
|
static int overlapcnt;
|
|
|
|
static FILE *fp[3];
|
|
|
|
static int cline[3]; /* # of the last-read line in each file (0-2) */
|
|
|
|
/*
|
|
|
|
* The latest known correspondence between line numbers of the 3 files
|
|
|
|
* is stored in last[1-3];
|
|
|
|
*/
|
|
|
|
static int last[4];
|
|
|
|
static int Aflag, eflag, iflag, mflag, Tflag;
|
2021-08-20 21:50:20 +00:00
|
|
|
static int oflag; /* indicates whether to mark overlaps (-E or -X) */
|
2017-05-25 17:45:50 +00:00
|
|
|
static int strip_cr;
|
|
|
|
static char *f1mark, *f2mark, *f3mark;
|
2022-04-19 14:38:30 +00:00
|
|
|
static const char *oldmark = "<<<<<<<";
|
2022-04-19 14:43:35 +00:00
|
|
|
static const char *orgmark = "|||||||";
|
2022-04-19 14:38:30 +00:00
|
|
|
static const char *newmark = ">>>>>>>";
|
2022-08-01 13:46:09 +00:00
|
|
|
static const char *divider = "=======";
|
2017-05-25 17:45:50 +00:00
|
|
|
|
|
|
|
static bool duplicate(struct range *, struct range *);
|
2022-04-19 14:43:35 +00:00
|
|
|
static int edit(struct diff *, bool, int, int);
|
2017-05-25 17:45:50 +00:00
|
|
|
static char *getchange(FILE *);
|
|
|
|
static char *get_line(FILE *, size_t *);
|
|
|
|
static int readin(int fd, struct diff **);
|
|
|
|
static int skip(int, int, const char *);
|
|
|
|
static void change(int, struct range *, bool);
|
|
|
|
static void keep(int, struct range *);
|
|
|
|
static void merge(int, int);
|
2022-04-15 13:59:14 +00:00
|
|
|
static void prange(struct range *, bool);
|
2017-05-25 17:45:50 +00:00
|
|
|
static void repos(int);
|
|
|
|
static void edscript(int) __dead2;
|
2022-04-19 14:43:35 +00:00
|
|
|
static void Ascript(int) __dead2;
|
2022-04-19 15:20:24 +00:00
|
|
|
static void mergescript(int) __dead2;
|
2017-05-25 17:45:50 +00:00
|
|
|
static void increase(void);
|
2022-05-25 13:53:06 +00:00
|
|
|
static void usage(void);
|
2022-04-19 13:47:07 +00:00
|
|
|
static void printrange(FILE *, struct range *);
|
2017-05-25 17:45:50 +00:00
|
|
|
|
2022-05-25 13:53:06 +00:00
|
|
|
static const char diff3_version[] = "FreeBSD diff3 20220517";
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
enum {
|
|
|
|
DIFFPROG_OPT,
|
|
|
|
STRIPCR_OPT,
|
2022-05-25 13:53:06 +00:00
|
|
|
HELP_OPT,
|
|
|
|
VERSION_OPT
|
2017-05-25 17:45:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DIFF_PATH "/usr/bin/diff"
|
|
|
|
|
|
|
|
#define OPTIONS "3aAeEiL:mTxX"
|
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "ed", no_argument, NULL, 'e' },
|
|
|
|
{ "show-overlap", no_argument, NULL, 'E' },
|
|
|
|
{ "overlap-only", no_argument, NULL, 'x' },
|
|
|
|
{ "initial-tab", no_argument, NULL, 'T' },
|
|
|
|
{ "text", no_argument, NULL, 'a' },
|
|
|
|
{ "strip-trailing-cr", no_argument, NULL, STRIPCR_OPT },
|
|
|
|
{ "show-all", no_argument, NULL, 'A' },
|
|
|
|
{ "easy-only", no_argument, NULL, '3' },
|
|
|
|
{ "merge", no_argument, NULL, 'm' },
|
|
|
|
{ "label", required_argument, NULL, 'L' },
|
|
|
|
{ "diff-program", required_argument, NULL, DIFFPROG_OPT },
|
2022-05-25 13:53:06 +00:00
|
|
|
{ "help", no_argument, NULL, HELP_OPT},
|
|
|
|
{ "version", no_argument, NULL, VERSION_OPT}
|
2017-05-25 17:45:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2021-08-20 21:50:20 +00:00
|
|
|
fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L label1] [-L label2] "
|
|
|
|
"[-L label3] file1 file2 file3\n");
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
readin(int fd, struct diff **dd)
|
|
|
|
{
|
|
|
|
int a, b, c, d;
|
|
|
|
size_t i;
|
|
|
|
char kind, *p;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fdopen(fd, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
err(2, "fdopen");
|
2021-08-20 21:50:20 +00:00
|
|
|
for (i = 0; (p = getchange(f)); i++) {
|
2022-04-19 14:43:35 +00:00
|
|
|
#if DEBUG
|
|
|
|
(*dd)[i].line = strdup(p);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
if (i >= szchanges - 1)
|
|
|
|
increase();
|
2019-12-17 09:13:48 +00:00
|
|
|
a = b = (int)strtoimax(p, &p, 10);
|
2017-05-25 17:45:50 +00:00
|
|
|
if (*p == ',') {
|
|
|
|
p++;
|
2019-12-17 09:13:48 +00:00
|
|
|
b = (int)strtoimax(p, &p, 10);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
kind = *p++;
|
2019-12-17 09:13:48 +00:00
|
|
|
c = d = (int)strtoimax(p, &p, 10);
|
2021-08-20 21:50:20 +00:00
|
|
|
if (*p == ',') {
|
2017-05-25 17:45:50 +00:00
|
|
|
p++;
|
2019-12-17 09:13:48 +00:00
|
|
|
d = (int)strtoimax(p, &p, 10);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
if (kind == 'a')
|
|
|
|
a++;
|
|
|
|
if (kind == 'd')
|
|
|
|
c++;
|
|
|
|
b++;
|
|
|
|
d++;
|
|
|
|
(*dd)[i].old.from = a;
|
|
|
|
(*dd)[i].old.to = b;
|
|
|
|
(*dd)[i].new.from = c;
|
|
|
|
(*dd)[i].new.to = d;
|
|
|
|
}
|
|
|
|
if (i) {
|
2021-08-20 21:50:20 +00:00
|
|
|
(*dd)[i].old.from = (*dd)[i - 1].old.to;
|
|
|
|
(*dd)[i].new.from = (*dd)[i - 1].new.to;
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
diffexec(const char *diffprog, char **diffargv, int fd[])
|
|
|
|
{
|
2021-08-20 21:50:20 +00:00
|
|
|
int pd;
|
2017-05-25 17:45:50 +00:00
|
|
|
|
2021-08-20 21:50:20 +00:00
|
|
|
switch (pdfork(&pd, PD_CLOEXEC)) {
|
2017-05-25 17:45:50 +00:00
|
|
|
case 0:
|
|
|
|
close(fd[0]);
|
|
|
|
if (dup2(fd[1], STDOUT_FILENO) == -1)
|
|
|
|
err(2, "child could not duplicate descriptor");
|
|
|
|
close(fd[1]);
|
|
|
|
execvp(diffprog, diffargv);
|
|
|
|
err(2, "could not execute diff: %s", diffprog);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
err(2, "could not fork");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd[1]);
|
|
|
|
return (pd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
getchange(FILE *b)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
while ((line = get_line(b, NULL))) {
|
|
|
|
if (isdigit((unsigned char)line[0]))
|
|
|
|
return (line);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_line(FILE *b, size_t *n)
|
|
|
|
{
|
2019-12-17 11:03:32 +00:00
|
|
|
ssize_t len;
|
|
|
|
static char *buf = NULL;
|
|
|
|
static size_t bufsize = 0;
|
2017-05-25 17:45:50 +00:00
|
|
|
|
2019-12-17 11:03:32 +00:00
|
|
|
if ((len = getline(&buf, &bufsize, b)) < 0)
|
2017-05-25 17:45:50 +00:00
|
|
|
return (NULL);
|
|
|
|
|
2021-08-21 00:28:58 +00:00
|
|
|
if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) {
|
|
|
|
buf[len - 2] = '\n';
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
if (n != NULL)
|
|
|
|
*n = len;
|
2019-12-17 11:03:32 +00:00
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
merge(int m1, int m2)
|
|
|
|
{
|
|
|
|
struct diff *d1, *d2, *d3;
|
|
|
|
int j, t1, t2;
|
|
|
|
bool dup = false;
|
|
|
|
|
|
|
|
d1 = d13;
|
|
|
|
d2 = d23;
|
|
|
|
j = 0;
|
|
|
|
|
2022-05-25 13:41:43 +00:00
|
|
|
while (t1 = d1 < d13 + m1, t2 = d2 < d23 + m2, t1 || t2) {
|
2017-05-25 17:45:50 +00:00
|
|
|
/* first file is different from the others */
|
|
|
|
if (!t2 || (t1 && d1->new.to < d2->new.from)) {
|
|
|
|
/* stuff peculiar to 1st file */
|
2022-07-05 11:33:55 +00:00
|
|
|
if (eflag == EFLAG_NONE) {
|
2017-05-25 17:45:50 +00:00
|
|
|
printf("====1\n");
|
|
|
|
change(1, &d1->old, false);
|
|
|
|
keep(2, &d1->new);
|
|
|
|
change(3, &d1->new, false);
|
|
|
|
}
|
|
|
|
d1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* second file is different from others */
|
|
|
|
if (!t1 || (t2 && d2->new.to < d1->new.from)) {
|
2022-07-05 11:33:55 +00:00
|
|
|
if (eflag == EFLAG_NONE) {
|
2017-05-25 17:45:50 +00:00
|
|
|
printf("====2\n");
|
|
|
|
keep(1, &d2->new);
|
|
|
|
change(3, &d2->new, false);
|
|
|
|
change(2, &d2->old, false);
|
2022-04-19 14:43:35 +00:00
|
|
|
} else if (Aflag || mflag) {
|
|
|
|
// XXX-THJ: What does it mean for the second file to differ?
|
|
|
|
j = edit(d2, dup, j, DIFF_TYPE2);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
d2++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Merge overlapping changes in first file
|
|
|
|
* this happens after extension (see below).
|
|
|
|
*/
|
|
|
|
if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
|
|
|
|
d1[1].old.from = d1->old.from;
|
|
|
|
d1[1].new.from = d1->new.from;
|
|
|
|
d1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* merge overlapping changes in second */
|
|
|
|
if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
|
|
|
|
d2[1].old.from = d2->old.from;
|
|
|
|
d2[1].new.from = d2->new.from;
|
|
|
|
d2++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* stuff peculiar to third file or different in all */
|
|
|
|
if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
|
|
|
|
dup = duplicate(&d1->old, &d2->old);
|
|
|
|
/*
|
|
|
|
* dup = 0 means all files differ
|
|
|
|
* dup = 1 means files 1 and 2 identical
|
|
|
|
*/
|
2022-07-05 11:33:55 +00:00
|
|
|
if (eflag == EFLAG_NONE) {
|
2017-05-25 17:45:50 +00:00
|
|
|
printf("====%s\n", dup ? "3" : "");
|
|
|
|
change(1, &d1->old, dup);
|
|
|
|
change(2, &d2->old, false);
|
|
|
|
d3 = d1->old.to > d1->old.from ? d1 : d2;
|
|
|
|
change(3, &d3->new, false);
|
2022-04-19 14:43:35 +00:00
|
|
|
} else {
|
|
|
|
j = edit(d1, dup, j, DIFF_TYPE3);
|
|
|
|
}
|
|
|
|
dup = false;
|
2017-05-25 17:45:50 +00:00
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Overlapping changes from file 1 and 2; extend changes
|
|
|
|
* appropriately to make them coincide.
|
|
|
|
*/
|
|
|
|
if (d1->new.from < d2->new.from) {
|
|
|
|
d2->old.from -= d2->new.from - d1->new.from;
|
|
|
|
d2->new.from = d1->new.from;
|
|
|
|
} else if (d2->new.from < d1->new.from) {
|
|
|
|
d1->old.from -= d1->new.from - d2->new.from;
|
|
|
|
d1->new.from = d2->new.from;
|
|
|
|
}
|
|
|
|
if (d1->new.to > d2->new.to) {
|
|
|
|
d2->old.to += d1->new.to - d2->new.to;
|
|
|
|
d2->new.to = d1->new.to;
|
|
|
|
} else if (d2->new.to > d1->new.to) {
|
|
|
|
d1->old.to += d2->new.to - d1->new.to;
|
|
|
|
d1->new.to = d2->new.to;
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 14:43:35 +00:00
|
|
|
|
2022-04-19 15:20:24 +00:00
|
|
|
if (mflag)
|
|
|
|
mergescript(j);
|
|
|
|
else if (Aflag)
|
2022-04-19 14:43:35 +00:00
|
|
|
Ascript(j);
|
|
|
|
else if (eflag)
|
2017-05-25 17:45:50 +00:00
|
|
|
edscript(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The range of lines rold.from thru rold.to in file i is to be changed.
|
|
|
|
* It is to be printed only if it does not duplicate something to be
|
|
|
|
* printed later.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
change(int i, struct range *rold, bool dup)
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("%d:", i);
|
|
|
|
last[i] = rold->to;
|
2022-04-15 13:59:14 +00:00
|
|
|
prange(rold, false);
|
2017-05-25 17:45:50 +00:00
|
|
|
if (dup)
|
|
|
|
return;
|
|
|
|
i--;
|
|
|
|
skip(i, rold->from, NULL);
|
|
|
|
skip(i, rold->to, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
|
|
|
|
* n1.
|
|
|
|
*/
|
|
|
|
static void
|
2022-04-15 13:59:14 +00:00
|
|
|
prange(struct range *rold, bool delete)
|
2017-05-25 17:45:50 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (rold->to <= rold->from)
|
|
|
|
printf("%da\n", rold->from - 1);
|
|
|
|
else {
|
|
|
|
printf("%d", rold->from);
|
2021-08-20 21:50:20 +00:00
|
|
|
if (rold->to > rold->from + 1)
|
2017-05-25 17:45:50 +00:00
|
|
|
printf(",%d", rold->to - 1);
|
2022-04-15 13:59:14 +00:00
|
|
|
if (delete)
|
|
|
|
printf("d\n");
|
|
|
|
else
|
|
|
|
printf("c\n");
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No difference was reported by diff between file 1 (or 2) and file 3,
|
|
|
|
* and an artificial dummy difference (trange) must be ginned up to
|
|
|
|
* correspond to the change reported in the other file.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
keep(int i, struct range *rnew)
|
|
|
|
{
|
|
|
|
int delta;
|
|
|
|
struct range trange;
|
|
|
|
|
|
|
|
delta = last[3] - last[i];
|
|
|
|
trange.from = rnew->from - delta;
|
|
|
|
trange.to = rnew->to - delta;
|
|
|
|
change(i, &trange, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip to just before line number from in file "i". If "pr" is non-NULL,
|
|
|
|
* print all skipped stuff with string pr as a prefix.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
skip(int i, int from, const char *pr)
|
|
|
|
{
|
|
|
|
size_t j, n;
|
|
|
|
char *line;
|
|
|
|
|
|
|
|
for (n = 0; cline[i] < from - 1; n += j) {
|
|
|
|
if ((line = get_line(fp[i], &j)) == NULL)
|
|
|
|
errx(EXIT_FAILURE, "logic error");
|
|
|
|
if (pr != NULL)
|
2021-08-20 21:50:20 +00:00
|
|
|
printf("%s%s", Tflag == 1 ? "\t" : pr, line);
|
2017-05-25 17:45:50 +00:00
|
|
|
cline[i]++;
|
|
|
|
}
|
|
|
|
return ((int) n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 1 or 0 according as the old range (in file 1) contains exactly
|
|
|
|
* the same data as the new range (in file 2).
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
duplicate(struct range *r1, struct range *r2)
|
|
|
|
{
|
|
|
|
int c, d;
|
|
|
|
int nchar;
|
|
|
|
int nline;
|
|
|
|
|
|
|
|
if (r1->to-r1->from != r2->to-r2->from)
|
|
|
|
return (0);
|
|
|
|
skip(0, r1->from, NULL);
|
|
|
|
skip(1, r2->from, NULL);
|
|
|
|
nchar = 0;
|
2021-08-20 21:50:20 +00:00
|
|
|
for (nline = 0; nline < r1->to - r1->from; nline++) {
|
2017-05-25 17:45:50 +00:00
|
|
|
do {
|
|
|
|
c = getc(fp[0]);
|
|
|
|
d = getc(fp[1]);
|
2021-08-20 21:35:24 +00:00
|
|
|
if (c == -1 && d == -1)
|
|
|
|
break;
|
2021-08-20 21:50:20 +00:00
|
|
|
if (c == -1 || d == -1)
|
2017-05-25 17:45:50 +00:00
|
|
|
errx(EXIT_FAILURE, "logic error");
|
|
|
|
nchar++;
|
|
|
|
if (c != d) {
|
|
|
|
repos(nchar);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
} while (c != '\n');
|
|
|
|
}
|
|
|
|
repos(nchar);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
repos(int nchar)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
|
|
|
|
}
|
2021-08-20 21:50:20 +00:00
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
/*
|
|
|
|
* collect an editing script for later regurgitation
|
|
|
|
*/
|
|
|
|
static int
|
2022-04-19 14:43:35 +00:00
|
|
|
edit(struct diff *diff, bool dup, int j, int difftype)
|
2017-05-25 17:45:50 +00:00
|
|
|
{
|
2022-07-05 11:33:55 +00:00
|
|
|
if (!(eflag == EFLAG_UNMERGED ||
|
|
|
|
(!dup && eflag == EFLAG_OVERLAP ) ||
|
|
|
|
(dup && eflag == EFLAG_NOOVERLAP))) {
|
2017-05-25 17:45:50 +00:00
|
|
|
return (j);
|
2022-07-05 11:33:55 +00:00
|
|
|
}
|
2017-05-25 17:45:50 +00:00
|
|
|
j++;
|
|
|
|
overlap[j] = !dup;
|
|
|
|
if (!dup)
|
|
|
|
overlapcnt++;
|
2022-04-19 14:43:35 +00:00
|
|
|
|
|
|
|
de[j].type = difftype;
|
|
|
|
#if DEBUG
|
2022-05-25 13:45:45 +00:00
|
|
|
de[j].line = strdup(diff->line);
|
2022-04-19 14:43:35 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
de[j].old.from = diff->old.from;
|
|
|
|
de[j].old.to = diff->old.to;
|
2022-04-19 13:47:07 +00:00
|
|
|
de[j].new.from = diff->new.from;
|
|
|
|
de[j].new.to = diff->new.to;
|
2017-05-25 17:45:50 +00:00
|
|
|
return (j);
|
|
|
|
}
|
|
|
|
|
2022-04-19 13:47:07 +00:00
|
|
|
static void
|
|
|
|
printrange(FILE *p, struct range *r)
|
|
|
|
{
|
|
|
|
char *line = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
int i = 1;
|
|
|
|
ssize_t rlen = 0;
|
|
|
|
|
|
|
|
/* We haven't been asked to print anything */
|
|
|
|
if (r->from == r->to)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (r->from > r->to)
|
2022-08-01 13:14:25 +00:00
|
|
|
errx(EXIT_FAILURE, "invalid print range");
|
2022-04-19 13:47:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX-THJ: We read through all of the file for each range printed.
|
|
|
|
* This duplicates work and will probably impact performance on large
|
|
|
|
* files with lots of ranges.
|
|
|
|
*/
|
|
|
|
fseek(p, 0L, SEEK_SET);
|
|
|
|
while ((rlen = getline(&line, &len, p)) > 0) {
|
|
|
|
if (i >= r->from)
|
|
|
|
printf("%s", line);
|
|
|
|
if (++i > r->to - 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
/* regurgitate */
|
|
|
|
static void
|
|
|
|
edscript(int n)
|
|
|
|
{
|
2022-04-15 13:59:14 +00:00
|
|
|
bool delete;
|
2022-08-02 13:33:17 +00:00
|
|
|
struct range *new, *old;
|
2017-05-25 17:45:50 +00:00
|
|
|
|
|
|
|
for (; n > 0; n--) {
|
2022-08-02 13:33:17 +00:00
|
|
|
new = &de[n].new;
|
|
|
|
old = &de[n].old;
|
|
|
|
|
|
|
|
delete = (new->from == new->to);
|
2017-05-25 17:45:50 +00:00
|
|
|
if (!oflag || !overlap[n]) {
|
2022-08-02 13:33:17 +00:00
|
|
|
prange(old, delete);
|
2017-05-25 17:45:50 +00:00
|
|
|
} else {
|
2022-08-02 13:33:17 +00:00
|
|
|
printf("%da\n", old->to - 1);
|
2022-08-01 13:46:09 +00:00
|
|
|
printf("%s\n", divider);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
2022-08-02 13:33:17 +00:00
|
|
|
printrange(fp[2], new);
|
2022-04-15 13:59:14 +00:00
|
|
|
if (!oflag || !overlap[n]) {
|
|
|
|
if (!delete)
|
|
|
|
printf(".\n");
|
|
|
|
} else {
|
2022-04-19 14:38:30 +00:00
|
|
|
printf("%s %s\n.\n", newmark, f3mark);
|
2022-08-02 13:33:17 +00:00
|
|
|
printf("%da\n%s %s\n.\n", old->from - 1,
|
2022-04-19 14:38:30 +00:00
|
|
|
oldmark, f1mark);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iflag)
|
|
|
|
printf("w\nq\n");
|
|
|
|
|
2022-07-05 11:33:55 +00:00
|
|
|
exit(eflag == EFLAG_NONE ? overlapcnt : 0);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 14:43:35 +00:00
|
|
|
/*
|
|
|
|
* Output an edit script to turn mine into yours, when there is a conflict
|
|
|
|
* between the 3 files bracket the changes. Regurgitate the diffs in reverse
|
|
|
|
* order to allow the ed script to track down where the lines are as changes
|
|
|
|
* are made.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
Ascript(int n)
|
|
|
|
{
|
|
|
|
int startmark;
|
|
|
|
bool deletenew;
|
|
|
|
bool deleteold;
|
|
|
|
|
2022-08-01 13:48:35 +00:00
|
|
|
struct range *new, *old;
|
2022-04-19 14:43:35 +00:00
|
|
|
|
2022-08-01 13:48:35 +00:00
|
|
|
for (; n > 0; n--) {
|
|
|
|
new = &de[n].new;
|
|
|
|
old = &de[n].old;
|
|
|
|
deletenew = (new->from == new->to);
|
|
|
|
deleteold = (old->from == old->to);
|
2022-04-19 14:43:35 +00:00
|
|
|
|
|
|
|
if (de[n].type == DIFF_TYPE2) {
|
|
|
|
if (!oflag || !overlap[n]) {
|
2022-08-01 13:48:35 +00:00
|
|
|
prange(old, deletenew);
|
|
|
|
printrange(fp[2], new);
|
2022-04-19 14:43:35 +00:00
|
|
|
} else {
|
2022-08-02 13:23:10 +00:00
|
|
|
startmark = new->to;
|
2022-04-19 14:43:35 +00:00
|
|
|
|
|
|
|
if (!deletenew)
|
|
|
|
startmark--;
|
|
|
|
|
|
|
|
printf("%da\n", startmark);
|
|
|
|
printf("%s %s\n", newmark, f3mark);
|
|
|
|
|
|
|
|
printf(".\n");
|
|
|
|
|
|
|
|
printf("%da\n", startmark -
|
2022-08-01 13:48:35 +00:00
|
|
|
(new->to - new->from));
|
2022-04-19 14:43:35 +00:00
|
|
|
printf("%s %s\n", oldmark, f2mark);
|
|
|
|
if (!deleteold)
|
2022-08-01 13:48:35 +00:00
|
|
|
printrange(fp[1], old);
|
2022-08-01 13:46:09 +00:00
|
|
|
printf("%s\n.\n", divider);
|
2022-04-19 14:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (de[n].type == DIFF_TYPE3) {
|
2022-08-02 13:23:10 +00:00
|
|
|
startmark = old->to - 1;
|
|
|
|
|
2022-04-19 14:43:35 +00:00
|
|
|
if (!oflag || !overlap[n]) {
|
2022-08-01 13:48:35 +00:00
|
|
|
prange(old, deletenew);
|
|
|
|
printrange(fp[2], new);
|
2022-04-19 14:43:35 +00:00
|
|
|
} else {
|
|
|
|
printf("%da\n", startmark);
|
|
|
|
printf("%s %s\n", orgmark, f2mark);
|
|
|
|
|
|
|
|
if (deleteold) {
|
|
|
|
struct range r;
|
2022-08-01 13:48:35 +00:00
|
|
|
r.from = old->from-1;
|
|
|
|
r.to = new->to;
|
2022-04-19 14:43:35 +00:00
|
|
|
printrange(fp[1], &r);
|
|
|
|
} else
|
2022-08-01 13:48:35 +00:00
|
|
|
printrange(fp[1], old);
|
2022-04-19 14:43:35 +00:00
|
|
|
|
2022-08-01 13:46:09 +00:00
|
|
|
printf("%s\n", divider);
|
2022-08-01 13:48:35 +00:00
|
|
|
printrange(fp[2], new);
|
2022-04-19 14:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!oflag || !overlap[n]) {
|
|
|
|
if (!deletenew)
|
|
|
|
printf(".\n");
|
|
|
|
} else {
|
|
|
|
printf("%s %s\n.\n", newmark, f3mark);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go to the start of the conflict in original
|
|
|
|
* file and append lines
|
|
|
|
*/
|
|
|
|
printf("%da\n%s %s\n.\n",
|
2022-08-01 13:48:35 +00:00
|
|
|
startmark - (old->to - old->from),
|
2022-04-19 14:43:35 +00:00
|
|
|
oldmark, f1mark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iflag)
|
|
|
|
printf("w\nq\n");
|
|
|
|
|
|
|
|
exit(overlapcnt > 0);
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:20:24 +00:00
|
|
|
/*
|
|
|
|
* Output the merged file directly (don't generate an ed script). When
|
|
|
|
* regurgitating diffs we need to walk forward through the file and print any
|
|
|
|
* inbetween lines.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mergescript(int i)
|
|
|
|
{
|
2022-08-02 13:29:38 +00:00
|
|
|
struct range r, *new, *old;
|
2022-04-19 15:20:24 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
r.from = 1;
|
|
|
|
r.to = 1;
|
|
|
|
|
|
|
|
for (n = 1; n < i+1; n++) {
|
2022-08-02 13:29:38 +00:00
|
|
|
new = &de[n].new;
|
|
|
|
old = &de[n].old;
|
|
|
|
|
2022-04-19 15:20:24 +00:00
|
|
|
/* print any lines leading up to here */
|
2022-08-02 13:29:38 +00:00
|
|
|
r.to = old->from;
|
2022-04-19 15:20:24 +00:00
|
|
|
printrange(fp[0], &r);
|
|
|
|
|
|
|
|
if (de[n].type == DIFF_TYPE2) {
|
|
|
|
printf("%s %s\n", oldmark, f2mark);
|
2022-08-02 13:29:38 +00:00
|
|
|
printrange(fp[1], old);
|
2022-08-01 13:46:09 +00:00
|
|
|
printf("%s\n", divider);
|
2022-08-02 13:29:38 +00:00
|
|
|
printrange(fp[2], new);
|
2022-04-19 15:20:24 +00:00
|
|
|
printf("%s %s\n", newmark, f3mark);
|
|
|
|
} else if (de[n].type == DIFF_TYPE3) {
|
|
|
|
if (!oflag || !overlap[n]) {
|
2022-08-02 13:29:38 +00:00
|
|
|
printrange(fp[2], new);
|
2022-04-19 15:20:24 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
printf("%s %s\n", oldmark, f1mark);
|
2022-08-02 13:29:38 +00:00
|
|
|
printrange(fp[0], old);
|
2022-04-19 15:20:24 +00:00
|
|
|
|
|
|
|
printf("%s %s\n", orgmark, f2mark);
|
2022-08-02 13:29:38 +00:00
|
|
|
if (old->from == old->to) {
|
2022-04-19 15:20:24 +00:00
|
|
|
struct range or;
|
2022-08-02 13:29:38 +00:00
|
|
|
or.from = old->from - 1;
|
|
|
|
or.to = new->to;
|
2022-04-19 15:20:24 +00:00
|
|
|
printrange(fp[1], &or);
|
|
|
|
} else
|
2022-08-02 13:29:38 +00:00
|
|
|
printrange(fp[1], old);
|
2022-04-19 15:20:24 +00:00
|
|
|
|
2022-08-01 13:46:09 +00:00
|
|
|
printf("%s\n", divider);
|
2022-04-19 15:20:24 +00:00
|
|
|
|
2022-08-02 13:29:38 +00:00
|
|
|
printrange(fp[2], new);
|
2022-04-19 15:20:24 +00:00
|
|
|
printf("%s %s\n", newmark, f3mark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 13:29:38 +00:00
|
|
|
if (old->from == old->to)
|
|
|
|
r.from = new->to;
|
2022-04-19 15:20:24 +00:00
|
|
|
else
|
2022-08-02 13:29:38 +00:00
|
|
|
r.from = old->to;
|
2022-04-19 15:20:24 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Print from the final range to the end of 'myfile'. Any deletions or
|
|
|
|
* additions to this file should have been handled by now.
|
|
|
|
*
|
|
|
|
* If the ranges are the same we need to rewind a line.
|
|
|
|
* If the new range is 0 length (from == to), we need to use the old
|
|
|
|
* range.
|
|
|
|
*/
|
2022-08-02 13:29:38 +00:00
|
|
|
new = &de[n-1].new;
|
|
|
|
old = &de[n-1].old;
|
|
|
|
if ((old->from == new->from) &&
|
|
|
|
(old->to == new->to))
|
2022-04-19 15:20:24 +00:00
|
|
|
r.from--;
|
2022-08-02 13:29:38 +00:00
|
|
|
else if (new->from == new->to)
|
|
|
|
r.from = old->from;
|
2022-04-19 15:20:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the range is a 3 way merge then we need to skip a line in the
|
|
|
|
* trailing output.
|
|
|
|
*/
|
|
|
|
if (de[n-1].type == DIFF_TYPE3)
|
|
|
|
r.from++;
|
|
|
|
|
|
|
|
r.to = INT_MAX;
|
|
|
|
printrange(fp[0], &r);
|
|
|
|
exit(overlapcnt > 0);
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:45:50 +00:00
|
|
|
static void
|
|
|
|
increase(void)
|
|
|
|
{
|
|
|
|
struct diff *p;
|
|
|
|
char *q;
|
|
|
|
size_t newsz, incr;
|
|
|
|
|
|
|
|
/* are the memset(3) calls needed? */
|
|
|
|
newsz = szchanges == 0 ? 64 : 2 * szchanges;
|
|
|
|
incr = newsz - szchanges;
|
|
|
|
|
2021-08-20 21:35:24 +00:00
|
|
|
p = reallocarray(d13, newsz, sizeof(struct diff));
|
2017-05-25 17:45:50 +00:00
|
|
|
if (p == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
memset(p + szchanges, 0, incr * sizeof(struct diff));
|
|
|
|
d13 = p;
|
2021-08-20 21:35:24 +00:00
|
|
|
p = reallocarray(d23, newsz, sizeof(struct diff));
|
2017-05-25 17:45:50 +00:00
|
|
|
if (p == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
memset(p + szchanges, 0, incr * sizeof(struct diff));
|
|
|
|
d23 = p;
|
2021-08-20 21:35:24 +00:00
|
|
|
p = reallocarray(de, newsz, sizeof(struct diff));
|
2017-05-25 17:45:50 +00:00
|
|
|
if (p == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
memset(p + szchanges, 0, incr * sizeof(struct diff));
|
|
|
|
de = p;
|
2021-08-20 21:35:24 +00:00
|
|
|
q = reallocarray(overlap, newsz, sizeof(char));
|
2017-05-25 17:45:50 +00:00
|
|
|
if (q == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
memset(q + szchanges, 0, incr * sizeof(char));
|
|
|
|
overlap = q;
|
|
|
|
szchanges = newsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch, nblabels, status, m, n, kq, nke, nleft, i;
|
|
|
|
char *labels[] = { NULL, NULL, NULL };
|
|
|
|
const char *diffprog = DIFF_PATH;
|
|
|
|
char *file1, *file2, *file3;
|
2021-08-21 00:28:58 +00:00
|
|
|
char *diffargv[7];
|
2017-05-25 17:45:50 +00:00
|
|
|
int diffargc = 0;
|
|
|
|
int fd13[2], fd23[2];
|
|
|
|
int pd13, pd23;
|
|
|
|
cap_rights_t rights_ro;
|
|
|
|
struct kevent *e;
|
|
|
|
|
|
|
|
nblabels = 0;
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_NONE;
|
2017-05-25 17:45:50 +00:00
|
|
|
oflag = 0;
|
|
|
|
diffargv[diffargc++] = __DECONST(char *, diffprog);
|
|
|
|
while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case '3':
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_NOOVERLAP;
|
2017-05-25 17:45:50 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
diffargv[diffargc++] = __DECONST(char *, "-a");
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
Aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_UNMERGED;
|
2017-05-25 17:45:50 +00:00
|
|
|
break;
|
|
|
|
case 'E':
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_UNMERGED;
|
2017-05-25 17:45:50 +00:00
|
|
|
oflag = 1;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
oflag = 1;
|
|
|
|
if (nblabels >= 3)
|
|
|
|
errx(2, "too many file label options");
|
|
|
|
labels[nblabels++] = optarg;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
Aflag = 1;
|
|
|
|
oflag = 1;
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
Tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_OVERLAP;
|
2017-05-25 17:45:50 +00:00
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
oflag = 1;
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_OVERLAP;
|
2017-05-25 17:45:50 +00:00
|
|
|
break;
|
|
|
|
case DIFFPROG_OPT:
|
|
|
|
diffprog = optarg;
|
|
|
|
break;
|
|
|
|
case STRIPCR_OPT:
|
|
|
|
strip_cr = 1;
|
2021-08-21 00:28:58 +00:00
|
|
|
diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr");
|
2017-05-25 17:45:50 +00:00
|
|
|
break;
|
2022-05-25 13:53:06 +00:00
|
|
|
case HELP_OPT:
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
case VERSION_OPT:
|
|
|
|
printf("%s\n", diff3_version);
|
|
|
|
exit(0);
|
2017-05-25 17:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (Aflag) {
|
2022-07-05 11:33:55 +00:00
|
|
|
eflag = EFLAG_UNMERGED;
|
2017-05-25 17:45:50 +00:00
|
|
|
oflag = 1;
|
|
|
|
}
|
|
|
|
|
2022-05-25 13:53:06 +00:00
|
|
|
if (argc != 3) {
|
2017-05-25 17:45:50 +00:00
|
|
|
usage();
|
2022-05-25 13:53:06 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
2017-05-25 17:45:50 +00:00
|
|
|
|
|
|
|
if (caph_limit_stdio() == -1)
|
|
|
|
err(2, "unable to limit stdio");
|
|
|
|
|
|
|
|
cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
|
|
|
|
|
|
|
|
kq = kqueue();
|
|
|
|
if (kq == -1)
|
|
|
|
err(2, "kqueue");
|
|
|
|
|
|
|
|
e = malloc(2 * sizeof(struct kevent));
|
|
|
|
if (e == NULL)
|
|
|
|
err(2, "malloc");
|
|
|
|
|
|
|
|
/* TODO stdio */
|
|
|
|
file1 = argv[0];
|
|
|
|
file2 = argv[1];
|
|
|
|
file3 = argv[2];
|
|
|
|
|
|
|
|
if (oflag) {
|
2022-04-19 14:38:30 +00:00
|
|
|
asprintf(&f1mark, "%s",
|
2017-05-25 17:45:50 +00:00
|
|
|
labels[0] != NULL ? labels[0] : file1);
|
|
|
|
if (f1mark == NULL)
|
|
|
|
err(2, "asprintf");
|
2022-04-19 14:38:30 +00:00
|
|
|
asprintf(&f2mark, "%s",
|
2017-05-25 17:45:50 +00:00
|
|
|
labels[1] != NULL ? labels[1] : file2);
|
|
|
|
if (f2mark == NULL)
|
|
|
|
err(2, "asprintf");
|
2022-04-19 14:38:30 +00:00
|
|
|
asprintf(&f3mark, "%s",
|
2017-05-25 17:45:50 +00:00
|
|
|
labels[2] != NULL ? labels[2] : file3);
|
|
|
|
if (f3mark == NULL)
|
|
|
|
err(2, "asprintf");
|
|
|
|
}
|
|
|
|
fp[0] = fopen(file1, "r");
|
|
|
|
if (fp[0] == NULL)
|
|
|
|
err(2, "Can't open %s", file1);
|
2018-11-04 19:24:49 +00:00
|
|
|
if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
|
2017-05-25 17:45:50 +00:00
|
|
|
err(2, "unable to limit rights on: %s", file1);
|
|
|
|
|
|
|
|
fp[1] = fopen(file2, "r");
|
|
|
|
if (fp[1] == NULL)
|
|
|
|
err(2, "Can't open %s", file2);
|
2018-11-04 19:24:49 +00:00
|
|
|
if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
|
2017-05-25 17:45:50 +00:00
|
|
|
err(2, "unable to limit rights on: %s", file2);
|
|
|
|
|
|
|
|
fp[2] = fopen(file3, "r");
|
|
|
|
if (fp[2] == NULL)
|
|
|
|
err(2, "Can't open %s", file3);
|
2018-11-04 19:24:49 +00:00
|
|
|
if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
|
2017-05-25 17:45:50 +00:00
|
|
|
err(2, "unable to limit rights on: %s", file3);
|
|
|
|
|
|
|
|
if (pipe(fd13))
|
|
|
|
err(2, "pipe");
|
|
|
|
if (pipe(fd23))
|
|
|
|
err(2, "pipe");
|
|
|
|
|
|
|
|
diffargv[diffargc] = file1;
|
|
|
|
diffargv[diffargc + 1] = file3;
|
|
|
|
diffargv[diffargc + 2] = NULL;
|
|
|
|
|
|
|
|
nleft = 0;
|
|
|
|
pd13 = diffexec(diffprog, diffargv, fd13);
|
|
|
|
EV_SET(e + nleft , pd13, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
|
|
|
|
if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
|
|
|
|
err(2, "kevent1");
|
|
|
|
nleft++;
|
|
|
|
|
|
|
|
diffargv[diffargc] = file2;
|
|
|
|
pd23 = diffexec(diffprog, diffargv, fd23);
|
|
|
|
EV_SET(e + nleft , pd23, EVFILT_PROCDESC, EV_ADD, NOTE_EXIT, 0, NULL);
|
|
|
|
if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1)
|
|
|
|
err(2, "kevent2");
|
|
|
|
nleft++;
|
|
|
|
|
|
|
|
caph_cache_catpages();
|
2018-06-19 23:43:14 +00:00
|
|
|
if (caph_enter() < 0)
|
2017-05-25 17:45:50 +00:00
|
|
|
err(2, "unable to enter capability mode");
|
|
|
|
|
|
|
|
/* parse diffs */
|
|
|
|
increase();
|
|
|
|
m = readin(fd13[0], &d13);
|
|
|
|
n = readin(fd23[0], &d23);
|
|
|
|
|
|
|
|
/* waitpid cooked over pdforks */
|
|
|
|
while (nleft > 0) {
|
|
|
|
nke = kevent(kq, NULL, 0, e, nleft, NULL);
|
|
|
|
if (nke == -1)
|
|
|
|
err(2, "kevent");
|
|
|
|
for (i = 0; i < nke; i++) {
|
|
|
|
status = e[i].data;
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
|
2021-08-20 21:50:20 +00:00
|
|
|
errx(2, "diff exited abnormally");
|
2017-05-25 17:45:50 +00:00
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
errx(2, "diff killed by signal %d",
|
|
|
|
WTERMSIG(status));
|
|
|
|
}
|
|
|
|
nleft -= nke;
|
|
|
|
}
|
|
|
|
merge(m, n);
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|