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

  1. /* Merge RCS revisions.  */
  2.  
  3. /* Copyright 1982, 1988, 1989 Walter Tichy
  4.    Copyright 1990, 1991, 1992, 1993, 1994 Paul Eggert
  5.    Distributed under license by the Free Software Foundation, Inc.
  6.  
  7. This file is part of RCS.
  8.  
  9. RCS is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13.  
  14. RCS is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with RCS; see the file COPYING.  If not, write to
  21. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. Report problems and direct all questions to:
  24.  
  25.     rcs-bugs@cs.purdue.edu
  26.  
  27. */
  28.  
  29. /*
  30.  * $Log: rcsmerge.c,v $
  31.  * Revision 5.13  1994/03/17 14:05:48  eggert
  32.  * Specify subprocess input via file descriptor, not file name.  Remove lint.
  33.  *
  34.  * Revision 5.12  1993/11/09 17:40:15  eggert
  35.  * -V now prints version on stdout and exits.  Don't print usage twice.
  36.  *
  37.  * Revision 5.11  1993/11/03 17:42:27  eggert
  38.  * Add -A, -E, -e, -z.  Ignore -T.  Allow up to three file labels.
  39.  * Pass -Vn to `co'.  Pass unexpanded revision name to `co', so that Name works.
  40.  *
  41.  * Revision 5.10  1992/07/28  16:12:44  eggert
  42.  * Add -V.
  43.  *
  44.  * Revision 5.9  1992/01/24  18:44:19  eggert
  45.  * lint -> RCS_lint
  46.  *
  47.  * Revision 5.8  1992/01/06  02:42:34  eggert
  48.  * Update usage string.
  49.  *
  50.  * Revision 5.7  1991/11/20  17:58:09  eggert
  51.  * Don't Iopen(f, "r+"); it's not portable.
  52.  *
  53.  * Revision 5.6  1991/08/19  03:13:55  eggert
  54.  * Add -r$.  Tune.
  55.  *
  56.  * Revision 5.5  1991/04/21  11:58:27  eggert
  57.  * Add -x, RCSINIT, MS-DOS support.
  58.  *
  59.  * Revision 5.4  1991/02/25  07:12:43  eggert
  60.  * Merging a revision to itself is no longer an error.
  61.  *
  62.  * Revision 5.3  1990/11/01  05:03:50  eggert
  63.  * Remove unneeded setid check.
  64.  *
  65.  * Revision 5.2  1990/09/04  08:02:28  eggert
  66.  * Check for I/O error when reading working file.
  67.  *
  68.  * Revision 5.1  1990/08/29  07:14:04  eggert
  69.  * Add -q.  Pass -L options to merge.
  70.  *
  71.  * Revision 5.0  1990/08/22  08:13:41  eggert
  72.  * Propagate merge's exit status.
  73.  * Remove compile-time limits; use malloc instead.
  74.  * Make lock and temp files faster and safer.  Ansify and Posixate.  Add -V.
  75.  * Don't use access().  Tune.
  76.  *
  77.  * Revision 4.5  89/05/01  15:13:16  narten
  78.  * changed copyright header to reflect current distribution rules
  79.  * 
  80.  * Revision 4.4  88/08/09  19:13:13  eggert
  81.  * Beware merging into a readonly file.
  82.  * Beware merging a revision to itself (no change).
  83.  * Use execv(), not system(); yield exit status like diff(1)'s.
  84.  * 
  85.  * Revision 4.3  87/10/18  10:38:02  narten
  86.  * Updating version numbers. Changes relative to version 1.1 
  87.  * actually relative to 4.1
  88.  * 
  89.  * Revision 1.3  87/09/24  14:00:31  narten
  90.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  91.  * warnings)
  92.  * 
  93.  * Revision 1.2  87/03/27  14:22:36  jenkins
  94.  * Port to suns
  95.  * 
  96.  * Revision 4.1  83/03/28  11:14:57  wft
  97.  * Added handling of default branch.
  98.  * 
  99.  * Revision 3.3  82/12/24  15:29:00  wft
  100.  * Added call to catchsig().
  101.  *
  102.  * Revision 3.2  82/12/10  21:32:02  wft
  103.  * Replaced getdelta() with gettree(); improved error messages.
  104.  *
  105.  * Revision 3.1  82/11/28  19:27:44  wft
  106.  * Initial revision.
  107.  *
  108.  */
  109. #include "rcsbase.h"
  110.  
  111. static char const co[] = CO;
  112.  
  113. mainProg(rcsmergeId, "rcsmerge", "$Id: rcsmerge.c,v 5.13 1994/03/17 14:05:48 eggert Exp $")
  114. {
  115.     static char const cmdusage[] =
  116.         "\nrcsmerge usage: rcsmerge -rrev1 [-rrev2] -ksubst -{pq}[rev] -Vn -xsuff -zzone file";
  117.     static char const quietarg[] = "-q";
  118.  
  119.     register int i;
  120.     char *a, **newargv;
  121.     char const *arg[3];
  122.     char const *rev[3], *xrev[3]; /*revision numbers*/
  123.     char const *edarg, *expandarg, *suffixarg, *versionarg, *zonearg;
  124.         int tostdout;
  125.     int status;
  126.     RILE *workptr;
  127.     struct buf commarg;
  128.     struct buf numericrev; /* holds expanded revision number */
  129.     struct hshentries *gendeltas; /* deltas to be generated */
  130.         struct hshentry * target;
  131.  
  132.     bufautobegin(&commarg);
  133.     bufautobegin(&numericrev);
  134.     edarg = rev[1] = rev[2] = 0;
  135.     status = 0; /* Keep lint happy.  */
  136.     tostdout = false;
  137.     expandarg = suffixarg = versionarg = zonearg = quietarg; /* no-op */
  138.     suffixes = X_DEFAULT;
  139.  
  140.     argc = getRCSINIT(argc, argv, &newargv);
  141.     argv = newargv;
  142.     while (a = *++argv,  0<--argc && *a++=='-') {
  143.         switch (*a++) {
  144.                 case 'p':
  145.                         tostdout=true;
  146.             goto revno;
  147.  
  148.         case 'q':
  149.             quietflag = true;
  150.         revno:
  151.             if (!*a)
  152.                 break;
  153.                         /* falls into -r */
  154.                 case 'r':
  155.             if (!rev[1])
  156.                 rev[1] = a;
  157.             else if (!rev[2])
  158.                 rev[2] = a;
  159.             else
  160.                 error("too many revision numbers");
  161.                         break;
  162.  
  163.         case 'A': case 'E': case 'e':
  164.             if (*a)
  165.                 goto unknown;
  166.             edarg = *argv;
  167.             break;
  168.  
  169.         case 'x':
  170.             suffixarg = *argv;
  171.             suffixes = a;
  172.             break;
  173.         case 'z':
  174.             zonearg = *argv;
  175.             zone_set(a);
  176.             break;
  177.         case 'T':
  178.             /* Ignore -T, so that RCSINIT can contain -T.  */
  179.             if (*a)
  180.                 goto unknown;
  181.             break;
  182.         case 'V':
  183.             versionarg = *argv;
  184.             setRCSversion(versionarg);
  185.             break;
  186.  
  187.         case 'k':
  188.             expandarg = *argv;
  189.             if (0 <= str2expmode(expandarg+2))
  190.                 break;
  191.             /* fall into */
  192.                 default:
  193.         unknown:
  194.             error("unknown option: %s%s", *argv, cmdusage);
  195.                 };
  196.         } /* end of option processing */
  197.  
  198.     if (!rev[1]) faterror("no base revision number given");
  199.  
  200.     /* Now handle all pathnames.  */
  201.  
  202.     if (!nerror) {
  203.         if (argc < 1)
  204.         faterror("no input file%s", cmdusage);
  205.         if (0 < pairnames(argc, argv, rcsreadopen, true, false)) {
  206.  
  207.                 if (argc>2  ||  (argc==2 && argv[1]))
  208.                         warn("Excess arguments ignored.");
  209.         diagnose("RCS file: %s\n", RCSname);
  210.         if (!(workptr = Iopen(workname, FOPEN_R_WORK, (struct stat*)0)))
  211.             efaterror(workname);
  212.  
  213.                 gettree();  /* reads in the delta tree */
  214.  
  215.         if (!Head) rcsfaterror("no revisions present");
  216.  
  217.         if (!*rev[1])
  218.             rev[1]  =  Dbranch ? Dbranch : Head->num;
  219.         if (!fexpandsym(rev[1], &numericrev, workptr))
  220.             goto end;
  221.         if (!(target=genrevs(numericrev.string, (char *)0, (char *)0, (char*)0, &gendeltas))) goto end;
  222.         xrev[1] = target->num;
  223.         if (!rev[2] || !*rev[2])
  224.             rev[2]  =  Dbranch ? Dbranch : Head->num;
  225.         if (!fexpandsym(rev[2], &numericrev, workptr))
  226.             goto end;
  227.         if (!(target=genrevs(numericrev.string, (char *)0, (char *)0, (char *)0, &gendeltas))) goto end;
  228.         xrev[2] = target->num;
  229.  
  230.         if (strcmp(xrev[1],xrev[2]) == 0) {
  231.             if (tostdout) {
  232.                 FILE *o;
  233. #                if text_equals_binary_stdio || text_work_stdio
  234.                     o = stdout;
  235. #                else
  236.                     if (!(o=fdopen(STDOUT_FILENO,FOPEN_W_WORK)))
  237.                     efaterror("standard output");
  238. #                endif
  239.                 fastcopy(workptr,o);
  240.                 Ofclose(o);
  241.             }
  242.             goto end;
  243.         }
  244.         Izclose(&workptr);
  245.  
  246.         for (i=1; i<=2; i++) {
  247.             diagnose("retrieving revision %s\n", xrev[i]);
  248.             bufscpy(&commarg, "-p");
  249.             bufscat(&commarg, rev[i]); /* not xrev[i], for $Name's sake */
  250.             if (run(
  251.                 -1,
  252.                 /* Do not collide with merger.c maketemp().  */
  253.                 arg[i] = maketemp(i+2),
  254.                 co, quietarg, commarg.string,
  255.                 expandarg, suffixarg, versionarg, zonearg,
  256.                 RCSname, (char*)0
  257.             ))
  258.                 rcsfaterror("co failed");
  259.         }
  260.         diagnose("Merging differences between %s and %s into %s%s\n",
  261.              xrev[1], xrev[2], workname,
  262.                          tostdout?"; result to stdout":"");
  263.  
  264.         arg[0] = xrev[0] = workname;
  265.         status = merge(tostdout, edarg, xrev, arg);
  266.         }
  267.         }
  268.  
  269. end:
  270.     Izclose(&workptr);
  271.     tempunlink();
  272.     exitmain(nerror ? DIFF_TROUBLE : status);
  273. }
  274.  
  275. #if RCS_lint
  276. #    define exiterr rmergeExit
  277. #endif
  278.     void
  279. exiterr()
  280. {
  281.     tempunlink();
  282.     _exit(DIFF_TROUBLE);
  283. }
  284.