NetBSD/gnu/usr.bin/rcs/lib/merger.c

151 lines
3.2 KiB
C
Raw Normal View History

1996-10-15 10:59:14 +04:00
/* $NetBSD: merger.c,v 1.4 1996/10/15 07:00:08 veego Exp $ */
1995-02-24 05:24:53 +03:00
/* three-way file merge internals */
1993-07-09 05:56:50 +04:00
1996-10-15 10:59:14 +04:00
/* Copyright 1991, 1992, 1993, 1994, 1995 Paul Eggert
1993-07-09 05:56:50 +04:00
Distributed under license by the Free Software Foundation, Inc.
This file is part of RCS.
RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
1996-10-15 10:59:14 +04:00
along with RCS; see the file COPYING.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1993-07-09 05:56:50 +04:00
Report problems and direct all questions to:
rcs-bugs@cs.purdue.edu
*/
#include "rcsbase.h"
1996-10-15 10:59:14 +04:00
libId(mergerId, "Id: merger.c,v 1.7 1995/06/16 06:19:24 eggert Exp")
1993-07-09 05:56:50 +04:00
1995-02-24 05:24:53 +03:00
static char const *normalize_arg P((char const*,char**));
1993-07-09 05:56:50 +04:00
static char const *
normalize_arg(s, b)
char const *s;
char **b;
/*
* If S looks like an option, prepend ./ to it. Yield the result.
1996-10-15 10:59:14 +04:00
* Set *B to the address of any storage that was allocated.
1993-07-09 05:56:50 +04:00
*/
{
char *t;
1996-10-15 10:59:14 +04:00
if (*s == '-') {
*b = t = testalloc(strlen(s) + 3);
VOID sprintf(t, ".%c%s", SLASH, s);
return t;
} else {
*b = 0;
return s;
1993-07-09 05:56:50 +04:00
}
}
int
1995-02-24 05:24:53 +03:00
merge(tostdout, edarg, label, argv)
1993-07-09 05:56:50 +04:00
int tostdout;
1995-02-24 05:24:53 +03:00
char const *edarg;
char const *const label[3];
1993-07-09 05:56:50 +04:00
char const *const argv[3];
/*
1995-02-24 05:24:53 +03:00
* Do `merge [-p] EDARG -L l0 -L l1 -L l2 a0 a1 a2',
1993-07-09 05:56:50 +04:00
* where TOSTDOUT specifies whether -p is present,
1995-02-24 05:24:53 +03:00
* EDARG gives the editing type (e.g. "-A", or null for the default),
* LABEL gives l0, l1 and l2, and ARGV gives a0, a1 and a2.
1993-07-09 05:56:50 +04:00
* Yield DIFF_SUCCESS or DIFF_FAILURE.
*/
{
register int i;
FILE *f;
RILE *rt;
char const *a[3], *t;
char *b[3];
int s;
#if !DIFF3_BIN
char const *d[2];
#endif
for (i=3; 0<=--i; )
a[i] = normalize_arg(argv[i], &b[i]);
1995-02-24 05:24:53 +03:00
if (!edarg)
1996-10-15 10:59:14 +04:00
edarg = "-E";
1993-07-09 05:56:50 +04:00
#if DIFF3_BIN
t = 0;
if (!tostdout)
t = maketemp(0);
s = run(
1995-02-24 05:24:53 +03:00
-1, t,
1996-10-15 10:59:14 +04:00
DIFF3, edarg, "-am",
"-L", label[0],
"-L", label[1],
1995-02-24 05:24:53 +03:00
"-L", label[2],
1993-07-09 05:56:50 +04:00
a[0], a[1], a[2], (char*)0
);
switch (s) {
case DIFF_SUCCESS:
break;
case DIFF_FAILURE:
1995-02-24 05:24:53 +03:00
warn("conflicts during merge");
1993-07-09 05:56:50 +04:00
break;
default:
exiterr();
}
if (t) {
1996-10-15 10:59:14 +04:00
if (!(f = fopenSafer(argv[0], "w")))
1993-07-09 05:56:50 +04:00
efaterror(argv[0]);
1996-10-15 10:59:14 +04:00
if (!(rt = Iopen(t, "r", (struct stat*)0)))
1993-07-09 05:56:50 +04:00
efaterror(t);
fastcopy(rt, f);
Ifclose(rt);
Ofclose(f);
}
#else
for (i=0; i<2; i++)
switch (run(
1995-02-24 05:24:53 +03:00
-1, d[i]=maketemp(i),
1993-07-09 05:56:50 +04:00
DIFF, a[i], a[2], (char*)0
)) {
case DIFF_FAILURE: case DIFF_SUCCESS: break;
1995-02-24 05:24:53 +03:00
default: faterror("diff failed");
1993-07-09 05:56:50 +04:00
}
t = maketemp(2);
s = run(
1995-02-24 05:24:53 +03:00
-1, t,
DIFF3, edarg, d[0], d[1], a[0], a[1], a[2],
label[0], label[2], (char*)0
1993-07-09 05:56:50 +04:00
);
if (s != DIFF_SUCCESS) {
s = DIFF_FAILURE;
1995-02-24 05:24:53 +03:00
warn("overlaps or other problems during merge");
1993-07-09 05:56:50 +04:00
}
1996-10-15 10:59:14 +04:00
if (!(f = fopenSafer(t, "a+")))
1993-07-09 05:56:50 +04:00
efaterror(t);
aputs(tostdout ? "1,$p\n" : "w\n", f);
1995-02-24 05:24:53 +03:00
Orewind(f);
aflush(f);
if (run(fileno(f), (char*)0, ED, "-", a[0], (char*)0))
1993-07-09 05:56:50 +04:00
exiterr();
1995-02-24 05:24:53 +03:00
Ofclose(f);
1993-07-09 05:56:50 +04:00
#endif
tempunlink();
for (i=3; 0<=--i; )
if (b[i])
tfree(b[i]);
return s;
}