home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs567s.zip / rcs / src / merger.c < prev    next >
C/C++ Source or Header  |  1994-03-17  |  3KB  |  154 lines

  1. /* three-way file merge internals */
  2.  
  3. /* Copyright 1991, 1992, 1993, 1994 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.5 1994/03/17 14:05:48 eggert Exp $")
  31.  
  32.     static char const *normalize_arg P((char const*,char**));
  33.     static char const *
  34. normalize_arg(s, b)
  35.     char const *s;
  36.     char **b;
  37. /*
  38.  * If S looks like an option, prepend ./ to it.  Yield the result.
  39.  * Set *B to the address of any storage that was allocated..
  40.  */
  41. {
  42.     char *t;
  43.     switch (*s) {
  44.         case '-': case '+':
  45.             *b = t = testalloc(strlen(s) + 3);
  46.             VOID sprintf(t, ".%c%s", SLASH, s);
  47.             return t;
  48.         default:
  49.             *b = 0;
  50.             return s;
  51.     }
  52. }
  53.  
  54.     int
  55. merge(tostdout, edarg, label, argv)
  56.     int tostdout;
  57.     char const *edarg;
  58.     char const *const label[3];
  59.     char const *const argv[3];
  60. /*
  61.  * Do `merge [-p] EDARG -L l0 -L l1 -L l2 a0 a1 a2',
  62.  * where TOSTDOUT specifies whether -p is present,
  63.  * EDARG gives the editing type (e.g. "-A", or null for the default),
  64.  * LABEL gives l0, l1 and l2, and ARGV gives a0, a1 and a2.
  65.  * Yield DIFF_SUCCESS or DIFF_FAILURE.
  66.  */
  67. {
  68.     register int i;
  69.     FILE *f;
  70.     RILE *rt;
  71.     char const *a[3], *t;
  72.     char *b[3];
  73.     int s;
  74. #if !DIFF3_BIN
  75.     char const *d[2];
  76. #endif
  77.  
  78.     for (i=3; 0<=--i; )
  79.         a[i] = normalize_arg(argv[i], &b[i]);
  80.     
  81.     if (!edarg)
  82. #        if DIFF3_A
  83.             edarg = "-A";
  84. #        else
  85.             edarg = "-E";
  86. #        endif
  87.  
  88. #if DIFF3_BIN
  89.     t = 0;
  90.     if (!tostdout)
  91.         t = maketemp(0);
  92.     s = run(
  93.         -1, t,
  94.         DIFF3, edarg, "-am", "-L", label[0],
  95. #        if DIFF3_A
  96.             "-L", label[1],
  97. #        endif
  98.         "-L", label[2],
  99.         a[0], a[1], a[2], (char*)0
  100.     );
  101.     switch (s) {
  102.         case DIFF_SUCCESS:
  103.             break;
  104.         case DIFF_FAILURE:
  105.             warn("conflicts during merge");
  106.             break;
  107.         default:
  108.             exiterr();
  109.     }
  110.     if (t) {
  111.         if (!(f = fopen(argv[0], FOPEN_W)))
  112.             efaterror(argv[0]);
  113.         if (!(rt = Iopen(t, FOPEN_R, (struct stat*)0)))
  114.             efaterror(t);
  115.         fastcopy(rt, f);
  116.         Ifclose(rt);
  117.         Ofclose(f);
  118.     }
  119. #else
  120.     for (i=0; i<2; i++)
  121.         switch (run(
  122.             -1, d[i]=maketemp(i),
  123.             DIFF, a[i], a[2], (char*)0
  124.         )) {
  125.             case DIFF_FAILURE: case DIFF_SUCCESS: break;
  126.             default: faterror("diff failed");
  127.         }
  128.     t = maketemp(2);
  129.     s = run(
  130.         -1, t,
  131.         DIFF3, edarg, d[0], d[1], a[0], a[1], a[2],
  132.         label[0], label[2], (char*)0
  133.     );
  134.     if (s != DIFF_SUCCESS) {
  135.         s = DIFF_FAILURE;
  136.         warn("overlaps or other problems during merge");
  137.     }
  138.     if (!(f = fopen(t, "a+")))
  139.         efaterror(t);
  140.     aputs(tostdout ? "1,$p\n" : "w\n",  f);
  141.     Orewind(f);
  142.     aflush(f);
  143.     if (run(fileno(f), (char*)0, ED, "-", a[0], (char*)0))
  144.         exiterr();
  145.     Ofclose(f);
  146. #endif
  147.  
  148.     tempunlink();
  149.     for (i=3; 0<=--i; )
  150.         if (b[i])
  151.             tfree(b[i]);
  152.     return s;
  153. }
  154.