home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / rcs-5.6.0.1.tar.gz / rcs-5.6.0.1.tar / rcs5.6.0.1 / src / merger.c < prev    next >
C/C++ Source or Header  |  1993-03-24  |  3KB  |  146 lines

  1. /* merger - three-way file merge internals */
  2.  
  3. /* Copyright 1991 by Paul Eggert
  4.    Distributed under license by the Free Software Foundation, Inc.
  5.  
  6. This file is part of RCS.
  7.  
  8. RCS is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. RCS is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with RCS; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. Report problems and direct all questions to:
  23.  
  24.     rcs-bugs@cs.purdue.edu
  25.  
  26. */
  27.  
  28. #include "rcsbase.h"
  29.  
  30. libId(mergerId, "$Id: merger.c,v 1.3.0.1 1993/03/25 04:25:07 eggert Exp $")
  31.  
  32.     static char const *
  33. normalize_arg(s, b)
  34.     char const *s;
  35.     char **b;
  36. /*
  37.  * If S looks like an option, prepend ./ to it.  Yield the result.
  38.  * Set *B to the address of any storage that was allocated..
  39.  */
  40. {
  41.     char *t;
  42.     switch (*s) {
  43.         case '-': case '+':
  44.             *b = t = testalloc(strlen(s) + 3);
  45.             VOID sprintf(t, ".%c%s", SLASH, s);
  46.             return t;
  47.         default:
  48.             *b = 0;
  49.             return s;
  50.     }
  51. }
  52.  
  53.     int
  54. merge(tostdout, label, argv)
  55.     int tostdout;
  56.     char const *const label[2];
  57.     char const *const argv[3];
  58. /*
  59.  * Do `merge [-p] -L l0 -L l1 a0 a1 a2',
  60.  * where TOSTDOUT specifies whether -p is present,
  61.  * LABEL gives l0 and l1, and ARGV gives a0, a1, and a2.
  62.  * Yield DIFF_SUCCESS or DIFF_FAILURE.
  63.  */
  64. {
  65.     register int i;
  66.     FILE *f;
  67.     RILE *rt;
  68.     char const *a[3], *t;
  69.     char *b[3];
  70.     int s;
  71. #if !DIFF3_BIN
  72.     char const *d[2];
  73. #endif
  74.  
  75.     for (i=3; 0<=--i; )
  76.         a[i] = normalize_arg(argv[i], &b[i]);
  77.  
  78. #if DIFF3_BIN
  79.     t = 0;
  80.     if (!tostdout)
  81.         t = maketemp(0);
  82.     s = run(
  83.         (char*)0, t,
  84. #        if DIFF3_A
  85.             /* GNU diff 2.1 or later.  We turn off -A for now.  */
  86.             DIFF3, "-amE", "-L", label[0], "-L", "", "-L", label[1],
  87. #        else
  88.             /* GNU diff 2.0 or earlier */
  89.             DIFF3, "-am", "-L", label[0], "-L", label[1],
  90. #        endif
  91.         a[0], a[1], a[2], (char*)0
  92.     );
  93.     switch (s) {
  94.         case DIFF_SUCCESS:
  95.             break;
  96.         case DIFF_FAILURE:
  97.             if (!quietflag)
  98.                 warn("overlaps during merge");
  99.             break;
  100.         default:
  101.             exiterr();
  102.     }
  103.     if (t) {
  104.         if (!(f = fopen(argv[0], FOPEN_W)))
  105.             efaterror(argv[0]);
  106.         if (!(rt = Iopen(t, FOPEN_R, (struct stat*)0)))
  107.             efaterror(t);
  108.         fastcopy(rt, f);
  109.         Ifclose(rt);
  110.         Ofclose(f);
  111.     }
  112. #else
  113.     for (i=0; i<2; i++)
  114.         switch (run(
  115.             (char*)0, d[i]=maketemp(i),
  116.             DIFF, a[i], a[2], (char*)0
  117.         )) {
  118.             case DIFF_FAILURE: case DIFF_SUCCESS: break;
  119.             default: exiterr();
  120.         }
  121.     t = maketemp(2);
  122.     s = run(
  123.         (char*)0, t,
  124.         DIFF3, "-E", d[0], d[1], a[0], a[1], a[2],
  125.         label[0], label[1], (char*)0
  126.     );
  127.     if (s != DIFF_SUCCESS) {
  128.         s = DIFF_FAILURE;
  129.         if (!quietflag)
  130.             warn("overlaps or other problems during merge");
  131.     }
  132.     if (!(f = fopen(t, "a")))
  133.         efaterror(t);
  134.     aputs(tostdout ? "1,$p\n" : "w\n",  f);
  135.     Ofclose(f);
  136.     if (run(t, (char*)0, ED, "-", a[0], (char*)0))
  137.         exiterr();
  138. #endif
  139.  
  140.     tempunlink();
  141.     for (i=3; 0<=--i; )
  142.         if (b[i])
  143.             tfree(b[i]);
  144.     return s;
  145. }
  146.