home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / co.c < prev    next >
C/C++ Source or Header  |  1992-02-17  |  22KB  |  787 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990, 1991, 1992 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS checkout operation
  29.  */
  30. /*****************************************************************************
  31.  *                       check out revisions from RCS files
  32.  *****************************************************************************
  33.  */
  34.  
  35.  
  36. /* $Log: co.c,v $
  37.  * Revision 5.12  1992/02/17  23:02:08  eggert
  38.  * Add -T.
  39.  *
  40.  * Revision 5.11  1992/01/24  18:44:19  eggert
  41.  * Add support for bad_creat0.  lint -> RCS_lint
  42.  *
  43.  * Revision 5.10  1992/01/06  02:42:34  eggert
  44.  * Update usage string.
  45.  *
  46.  * Revision 5.9  1991/10/07  17:32:46  eggert
  47.  * -k affects just working file, not RCS file.
  48.  *
  49.  * Revision 5.8  1991/08/19  03:13:55  eggert
  50.  * Warn before removing somebody else's file.
  51.  * Add -M.  Fix co -j bugs.  Tune.
  52.  *
  53.  * Revision 5.7  1991/04/21  11:58:15  eggert
  54.  * Ensure that working file is newer than RCS file after co -[lu].
  55.  * Add -x, RCSINIT, MS-DOS support.
  56.  *
  57.  * Revision 5.6  1990/12/04  05:18:38  eggert
  58.  * Don't checkaccesslist() unless necessary.
  59.  * Use -I for prompts and -q for diagnostics.
  60.  *
  61.  * Revision 5.5  1990/11/01  05:03:26  eggert
  62.  * Fix -j.  Add -I.
  63.  *
  64.  * Revision 5.4  1990/10/04  06:30:11  eggert
  65.  * Accumulate exit status across files.
  66.  *
  67.  * Revision 5.3  1990/09/11  02:41:09  eggert
  68.  * co -kv yields a readonly working file.
  69.  *
  70.  * Revision 5.2  1990/09/04  08:02:13  eggert
  71.  * Standardize yes-or-no procedure.
  72.  *
  73.  * Revision 5.0  1990/08/22  08:10:02  eggert
  74.  * Permit multiple locks by same user.  Add setuid support.
  75.  * Remove compile-time limits; use malloc instead.
  76.  * Permit dates past 1999/12/31.  Switch to GMT.
  77.  * Make lock and temp files faster and safer.
  78.  * Ansify and Posixate.  Add -k, -V.  Remove snooping.  Tune.
  79.  *
  80.  * Revision 4.7  89/05/01  15:11:41  narten
  81.  * changed copyright header to reflect current distribution rules
  82.  *
  83.  * Revision 4.6  88/08/09  19:12:15  eggert
  84.  * Fix "co -d" core dump; rawdate wasn't always initialized.
  85.  * Use execv(), not system(); fix putchar('\0') and diagnose() botches; remove lint
  86.  *
  87.  * Revision 4.5  87/12/18  11:35:40  narten
  88.  * lint cleanups (from Guy Harris)
  89.  *
  90.  * Revision 4.4  87/10/18  10:20:53  narten
  91.  * Updating version numbers changes relative to 1.1, are actually
  92.  * relative to 4.2
  93.  *
  94.  * Revision 1.3  87/09/24  13:58:30  narten
  95.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  96.  * warnings)
  97.  *
  98.  * Revision 1.2  87/03/27  14:21:38  jenkins
  99.  * Port to suns
  100.  *
  101.  * Revision 4.2  83/12/05  13:39:48  wft
  102.  * made rewriteflag external.
  103.  *
  104.  * Revision 4.1  83/05/10  16:52:55  wft
  105.  * Added option -u and -f.
  106.  * Added handling of default branch.
  107.  * Replaced getpwuid() with getcaller().
  108.  * Removed calls to stat(); now done by pairfilenames().
  109.  * Changed and renamed rmoldfile() to rmworkfile().
  110.  * Replaced catchints() calls with restoreints(), unlink()--link() with rename();
  111.  *
  112.  * Revision 3.7  83/02/15  15:27:07  wft
  113.  * Added call to fastcopy() to copy remainder of RCS file.
  114.  *
  115.  * Revision 3.6  83/01/15  14:37:50  wft
  116.  * Added ignoring of interrupts while RCS file is renamed; this avoids
  117.  * deletion of RCS files during the unlink/link window.
  118.  *
  119.  * Revision 3.5  82/12/08  21:40:11  wft
  120.  * changed processing of -d to use DATEFORM; removed actual from
  121.  * call to preparejoin; re-fixed printing of done at the end.
  122.  *
  123.  * Revision 3.4  82/12/04  18:40:00  wft
  124.  * Replaced getdelta() with gettree(), SNOOPDIR with SNOOPFILE.
  125.  * Fixed printing of "done".
  126.  *
  127.  * Revision 3.3  82/11/28  22:23:11  wft
  128.  * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  129.  * %02d with %.2d, mode generation for working file with WORKMODE.
  130.  * Fixed nil printing. Fixed -j combined with -l and -p, and exit
  131.  * for non-existing revisions in preparejoin().
  132.  *
  133.  * Revision 3.2  82/10/18  20:47:21  wft
  134.  * Mode of working file is now maintained even for co -l, but write permission
  135.  * is removed.
  136.  * The working file inherits its mode from the RCS file, plus write permission
  137.  * for the owner. The write permission is not given if locking is strict and
  138.  * co does not lock.
  139.  * An existing working file without write permission is deleted automatically.
  140.  * Otherwise, co asks (empty answer: abort co).
  141.  * Call to getfullRCSname() added, check for write error added, call
  142.  * for getlogin() fixed.
  143.  *
  144.  * Revision 3.1  82/10/13  16:01:30  wft
  145.  * fixed type of variables receiving from getc() (char -> int).
  146.  * removed unused variables.
  147.  */
  148.  
  149.  
  150.  
  151.  
  152. #include "rcsbase.h"
  153.  
  154. static char const *getancestor P((char const*,char const*));
  155. static int buildjoin P((char const*));
  156. static int preparejoin P((void));
  157. static int rmlock P((struct hshentry const*));
  158. static int rmworkfile P((void));
  159. static void cleanup P((void));
  160.  
  161. static char const quietarg[] = "-q";
  162.  
  163. static char const *expandarg, *join, *suffixarg, *versionarg;
  164. static char const *joinlist[joinlength]; /* revisions to be joined */
  165. static FILE *neworkptr;
  166. static int exitstatus;
  167. static int forceflag;
  168. static int lastjoin;            /* index of last element in joinlist  */
  169. static int lockflag; /* -1 -> unlock, 0 -> do nothing, 1 -> lock */
  170. static int mtimeflag;
  171. static struct hshentries *gendeltas;    /* deltas to be generated    */
  172. static struct hshentry *targetdelta;    /* final delta to be generated    */
  173. static struct stat workstat;
  174.  
  175. mainProg(coId, "co", "$Id: co.c,v 5.12 1992/02/17 23:02:08 eggert Exp $")
  176. {
  177.     static char const cmdusage[] =
  178.         "\nco usage: co -{fIlMpqru}[rev] -ddate -jjoins -ksubst -sstate -w[who] -Vn -xsuff file ...";
  179.  
  180.     char *a, **newargv;
  181.     char const *author, *date, *rev, *state;
  182.     char const *joinfilename, *newdate, *neworkfilename;
  183.     int changelock;  /* 1 if a lock has been changed, -1 if error */
  184.     int expmode, r, tostdout, workstatstat;
  185.     int Ttimeflag;
  186.     struct buf numericrev;    /* expanded revision number    */
  187.     char finaldate[datesize];
  188.  
  189.     setrid();
  190.     author = date = rev = state = nil;
  191.     bufautobegin(&numericrev);
  192.     expmode = -1;
  193.     suffixes = X_DEFAULT;
  194.     tostdout = false;
  195.     Ttimeflag = false;
  196.  
  197.     argc = getRCSINIT(argc, argv, &newargv);
  198.     argv = newargv;
  199.     while (a = *++argv,  0<--argc && *a++=='-') {
  200.         switch (*a++) {
  201.  
  202.                 case 'r':
  203.         revno:
  204.             if (*a) {
  205.                 if (rev) warn("redefinition of revision number");
  206.                 rev = a;
  207.                         }
  208.                         break;
  209.  
  210.         case 'f':
  211.             forceflag=true;
  212.             goto revno;
  213.  
  214.                 case 'l':
  215.             if (lockflag < 0) {
  216.                                 warn("-l overrides -u.");
  217.                         }
  218.             lockflag = 1;
  219.                         goto revno;
  220.  
  221.                 case 'u':
  222.             if (0 < lockflag) {
  223.                                 warn("-l overrides -u.");
  224.                         }
  225.             lockflag = -1;
  226.                         goto revno;
  227.  
  228.                 case 'p':
  229.             tostdout = true;
  230.                         goto revno;
  231.  
  232.         case 'I':
  233.             interactiveflag = true;
  234.             goto revno;
  235.  
  236.                 case 'q':
  237.                         quietflag=true;
  238.                         goto revno;
  239.  
  240.                 case 'd':
  241.             if (date)
  242.                 redefined('d');
  243.             str2date(a, finaldate);
  244.                         date=finaldate;
  245.                         break;
  246.  
  247.                 case 'j':
  248.             if (*a) {
  249.                 if (join) redefined('j');
  250.                 join = a;
  251.                         }
  252.                         break;
  253.  
  254.         case 'M':
  255.             mtimeflag = true;
  256.             goto revno;
  257.  
  258.                 case 's':
  259.             if (*a) {
  260.                 if (state) redefined('s');
  261.                 state = a;
  262.                         }
  263.                         break;
  264.  
  265.         case 'T':
  266.             if (*a)
  267.                 goto unknown;
  268.             Ttimeflag = true;
  269.             break;
  270.  
  271.                 case 'w':
  272.             if (author) redefined('w');
  273.             if (*a)
  274.                 author = a;
  275.             else
  276.                 author = getcaller();
  277.                         break;
  278.  
  279.         case 'x':
  280.             suffixarg = *argv;
  281.             suffixes = a;
  282.             break;
  283.  
  284.         case 'V':
  285.             versionarg = *argv;
  286.             setRCSversion(versionarg);
  287.             break;
  288.  
  289.         case 'k':    /*  set keyword expand mode  */
  290.             expandarg = *argv;
  291.             if (0 <= expmode) redefined('k');
  292.             if (0 <= (expmode = str2expmode(a)))
  293.                 break;
  294.             /* fall into */
  295.                 default:
  296.         unknown:
  297.             faterror("unknown option: %s%s", *argv, cmdusage);
  298.  
  299.                 };
  300.         } /* end of option processing */
  301.  
  302.     if (argc<1) faterror("no input file%s", cmdusage);
  303.     if (tostdout)
  304. #        if text_equals_binary_stdio || text_work_stdio
  305.         workstdout = stdout;
  306. #        else
  307.         if (!(workstdout = fdopen(STDOUT_FILENO, FOPEN_W_WORK)))
  308.             efaterror("stdout");
  309. #        endif
  310.  
  311.         /* now handle all filenames */
  312.         do {
  313.     ffree();
  314.  
  315.     if (pairfilenames(argc, argv, lockflag?rcswriteopen:rcsreadopen, true, false)  <=  0)
  316.         continue;
  317.  
  318.         /* now RCSfilename contains the name of the RCS file, and finptr
  319.      * points at it.  workfilename contains the name of the working file.
  320.      * Also, RCSstat has been set.
  321.          */
  322.     diagnose("%s  -->  %s\n", RCSfilename,tostdout?"stdout":workfilename);
  323.  
  324.     workstatstat = -1;
  325.     if (tostdout) {
  326.         neworkfilename = 0;
  327.         neworkptr = workstdout;
  328.     } else {
  329.         workstatstat = stat(workfilename, &workstat);
  330.         neworkfilename = makedirtemp(1);
  331.         if (!(neworkptr = fopen(neworkfilename, FOPEN_W_WORK))) {
  332.             if (errno == EACCES)
  333.                 error("%s: parent directory isn't writable",
  334.                     workfilename
  335.                 );
  336.             else
  337.                 eerror(neworkfilename);
  338.             continue;
  339.         }
  340.     }
  341.  
  342.         gettree();  /* reads in the delta tree */
  343.  
  344.         if (Head==nil) {
  345.                 /* no revisions; create empty file */
  346.         diagnose("no revisions present; generating empty revision 0.0\n");
  347.         Ozclose(&fcopy);
  348.         if (workstatstat == 0)
  349.             if (!rmworkfile()) continue;
  350.         changelock = 0;
  351.         newdate = 0;
  352.                 /* Can't reserve a delta, so don't call addlock */
  353.         } else {
  354.                 if (rev!=nil) {
  355.                         /* expand symbolic revision number */
  356.             if (!expandsym(rev, &numericrev))
  357.                                 continue;
  358.         } else
  359.             switch (lockflag<0 ? findlock(false,&targetdelta) : 0) {
  360.                 default:
  361.                 continue;
  362.                 case 0:
  363.                 bufscpy(&numericrev, Dbranch?Dbranch:"");
  364.                 break;
  365.                 case 1:
  366.                 bufscpy(&numericrev, targetdelta->num);
  367.                 break;
  368.             }
  369.                 /* get numbers of deltas to be generated */
  370.         if (!(targetdelta=genrevs(numericrev.string,date,author,state,&gendeltas)))
  371.                         continue;
  372.                 /* check reservations */
  373.         changelock =
  374.             lockflag < 0 ?
  375.                 rmlock(targetdelta)
  376.             : lockflag == 0 ?
  377.                 0
  378.             :
  379.                 addlock(targetdelta);
  380.  
  381.         if (
  382.             changelock < 0 ||
  383.             changelock && !checkaccesslist() ||
  384.             dorewrite(lockflag, changelock) != 0
  385.         )
  386.             continue;
  387.  
  388.         if (0 <= expmode)
  389.             Expand = expmode;
  390.         if (0 < lockflag  &&  Expand == VAL_EXPAND) {
  391.             error("cannot combine -kv and -l");
  392.             continue;
  393.         }
  394.  
  395.                 if (join && !preparejoin()) continue;
  396.  
  397.         diagnose("revision %s%s\n",targetdelta->num,
  398.              0<lockflag ? " (locked)" :
  399.              lockflag<0 ? " (unlocked)" : "");
  400.  
  401.         /* Prepare to remove old working file if necessary.  */
  402.         if (workstatstat == 0)
  403.                         if (!rmworkfile()) continue;
  404.  
  405.                 /* skip description */
  406.                 getdesc(false); /* don't echo*/
  407.  
  408.         locker_expansion = 0 < lockflag;
  409.         joinfilename = buildrevision(
  410.             gendeltas, targetdelta,
  411.             join&&tostdout ? (FILE*)0 : neworkptr,
  412.             Expand!=OLD_EXPAND
  413.         );
  414. #        if !large_memory
  415.             if (fcopy == neworkptr)
  416.                 fcopy = 0;  /* Don't close it twice.  */
  417. #        endif
  418.         if_advise_access(changelock && gendeltas->first!=targetdelta,
  419.             finptr, MADV_SEQUENTIAL
  420.         );
  421.  
  422.         if (donerewrite(changelock, Ttimeflag) != 0)
  423.             continue;
  424.  
  425.         newdate = targetdelta->date;
  426.         if (join) {
  427.             newdate = 0;
  428.             if (!joinfilename) {
  429.                 aflush(neworkptr);
  430.                 joinfilename = neworkfilename;
  431.             }
  432.             if (!buildjoin(joinfilename))
  433.                 continue;
  434.         }
  435.         }
  436.     if (!tostdout) {
  437.         r = 0;
  438.         if (mtimeflag && newdate) {
  439.         if (!join)
  440.             aflush(neworkptr);
  441.         r = setfiledate(neworkfilename, newdate);
  442.         }
  443.         if (r == 0) {
  444.         ignoreints();
  445.         r = chnamemod(&neworkptr, neworkfilename, workfilename, 1,
  446.           WORKMODE(RCSstat.st_mode,
  447.             !(Expand==VAL_EXPAND || lockflag<=0&&StrictLocks)
  448.           )
  449.         );
  450.         keepdirtemp(neworkfilename);
  451.         restoreints();
  452.         }
  453.         if (r != 0) {
  454.         eerror(workfilename);
  455.         error("see %s", neworkfilename);
  456.         continue;
  457.         }
  458.         diagnose("done\n");
  459.     }
  460.         } while (cleanup(),
  461.                  ++argv, --argc >=1);
  462.  
  463.     tempunlink();
  464.     Ofclose(workstdout);
  465.     exitmain(exitstatus);
  466.  
  467. }       /* end of main (co) */
  468.  
  469.     static void
  470. cleanup()
  471. {
  472.     if (nerror) exitstatus = EXIT_FAILURE;
  473.     Izclose(&finptr);
  474.     ORCSclose();
  475. #    if !large_memory
  476.         if (fcopy!=workstdout) Ozclose(&fcopy);
  477. #    endif
  478.     if (neworkptr!=workstdout) Ozclose(&neworkptr);
  479.     dirtempunlink();
  480. }
  481.  
  482. #if RCS_lint
  483. #    define exiterr coExit
  484. #endif
  485.     exiting void
  486. exiterr()
  487. {
  488.     dirtempunlink();
  489.     tempunlink();
  490.     _exit(EXIT_FAILURE);
  491. }
  492.  
  493.  
  494. /*****************************************************************
  495.  * The following routines are auxiliary routines
  496.  *****************************************************************/
  497.  
  498.     static int
  499. rmworkfile()
  500. /* Function: prepares to remove workfilename, if it exists, and if
  501.  * it is read-only.
  502.  * Otherwise (file writable):
  503.  *   if !quietmode asks the user whether to really delete it (default: fail);
  504.  *   otherwise failure.
  505.  * Returns true if permission is gotten.
  506.  */
  507. {
  508.     if (workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH) && !forceflag) {
  509.         /* File is writable */
  510.         if (!yesorno(false, "writable %s exists%s; remove it? [ny](n): ",
  511.             workfilename,
  512.             myself(workstat.st_uid) ? "" : ", and you do not own it"
  513.         )) {
  514.         error(!quietflag && ttystdin()
  515.             ? "checkout aborted"
  516.             : "writable %s exists; checkout aborted", workfilename);
  517.         return false;
  518.             }
  519.         }
  520.     /* Actual unlink is done later by caller. */
  521.     return true;
  522. }
  523.  
  524.  
  525.     static int
  526. rmlock(delta)
  527.     struct hshentry const *delta;
  528. /* Function: removes the lock held by caller on delta.
  529.  * Returns -1 if someone else holds the lock,
  530.  * 0 if there is no lock on delta,
  531.  * and 1 if a lock was found and removed.
  532.  */
  533. {       register struct lock * next, * trail;
  534.     char const *num;
  535.         struct lock dummy;
  536.         int whomatch, nummatch;
  537.  
  538.         num=delta->num;
  539.         dummy.nextlock=next=Locks;
  540.         trail = &dummy;
  541.         while (next!=nil) {
  542.         whomatch = strcmp(getcaller(), next->login);
  543.                 nummatch=strcmp(num,next->delta->num);
  544.                 if ((whomatch==0) && (nummatch==0)) break;
  545.             /*found a lock on delta by caller*/
  546.                 if ((whomatch!=0)&&(nummatch==0)) {
  547.                     error("revision %s locked by %s; use co -r or rcs -u",num,next->login);
  548.                     return -1;
  549.                 }
  550.                 trail=next;
  551.                 next=next->nextlock;
  552.         }
  553.         if (next!=nil) {
  554.                 /*found one; delete it */
  555.                 trail->nextlock=next->nextlock;
  556.                 Locks=dummy.nextlock;
  557.                 next->delta->lockedby=nil; /* reset locked-by */
  558.                 return 1; /*success*/
  559.         } else  return 0; /*no lock on delta*/
  560. }
  561.  
  562.  
  563.  
  564.  
  565. /*****************************************************************
  566.  * The rest of the routines are for handling joins
  567.  *****************************************************************/
  568.  
  569.  
  570.     static char const *
  571. addjoin(joinrev)
  572.     char *joinrev;
  573. /* Add joinrev's number to joinlist, yielding address of char past joinrev,
  574.  * or nil if no such revision exists.
  575.  */
  576. {
  577.     register char *j;
  578.     register struct hshentry const *d;
  579.     char terminator;
  580.     struct buf numrev;
  581.     struct hshentries *joindeltas;
  582.  
  583.     j = joinrev;
  584.     for (;;) {
  585.         switch (*j++) {
  586.         default:
  587.             continue;
  588.         case 0:
  589.         case ' ': case '\t': case '\n':
  590.         case ':': case ',': case ';':
  591.             break;
  592.         }
  593.         break;
  594.     }
  595.     terminator = *--j;
  596.     *j = 0;
  597.     bufautobegin(&numrev);
  598.     d = 0;
  599.     if (expandsym(joinrev, &numrev))
  600.         d = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&joindeltas);
  601.     bufautoend(&numrev);
  602.     *j = terminator;
  603.     if (d) {
  604.         joinlist[++lastjoin] = d->num;
  605.         return j;
  606.     }
  607.     return nil;
  608. }
  609.  
  610.     static int
  611. preparejoin()
  612. /* Function: Parses a join list pointed to by join and places pointers to the
  613.  * revision numbers into joinlist.
  614.  */
  615. {
  616.     register char const *j;
  617.  
  618.         j=join;
  619.         lastjoin= -1;
  620.         for (;;) {
  621.                 while ((*j==' ')||(*j=='\t')||(*j==',')) j++;
  622.                 if (*j=='\0') break;
  623.                 if (lastjoin>=joinlength-2) {
  624.                         error("too many joins");
  625.                         return(false);
  626.                 }
  627.         if (!(j = addjoin(j))) return false;
  628.                 while ((*j==' ') || (*j=='\t')) j++;
  629.                 if (*j == ':') {
  630.                         j++;
  631.                         while((*j==' ') || (*j=='\t')) j++;
  632.                         if (*j!='\0') {
  633.                 if (!(j = addjoin(j))) return false;
  634.                         } else {
  635.                                 error("join pair incomplete");
  636.                                 return false;
  637.                         }
  638.                 } else {
  639.                         if (lastjoin==0) { /* first pair */
  640.                                 /* common ancestor missing */
  641.                                 joinlist[1]=joinlist[0];
  642.                                 lastjoin=1;
  643.                                 /*derive common ancestor*/
  644.                 if (!(joinlist[0] = getancestor(targetdelta->num,joinlist[1])))
  645.                                        return false;
  646.                         } else {
  647.                                 error("join pair incomplete");
  648.                                 return false;
  649.                         }
  650.                 }
  651.         }
  652.         if (lastjoin<1) {
  653.                 error("empty join");
  654.                 return false;
  655.         } else  return true;
  656. }
  657.  
  658.  
  659.  
  660.     static char const *
  661. getancestor(r1, r2)
  662.     char const *r1, *r2;
  663. /* Yield the common ancestor of r1 and r2 if successful, nil otherwise.
  664.  * Work reliably only if r1 and r2 are not branch numbers.
  665.  */
  666. {
  667.     static struct buf t1, t2;
  668.  
  669.     unsigned l1, l2, l3;
  670.     char const *r;
  671.  
  672.     l1 = countnumflds(r1);
  673.     l2 = countnumflds(r2);
  674.     if ((2<l1 || 2<l2)  &&  cmpnum(r1,r2)!=0) {
  675.         /* not on main trunk or identical */
  676.         l3 = 0;
  677.         while (cmpnumfld(r1, r2, l3+1)==0 && cmpnumfld(r1, r2, l3+2)==0)
  678.         l3 += 2;
  679.         /* This will terminate since r1 and r2 are not the same; see above. */
  680.         if (l3==0) {
  681.         /* no common prefix; common ancestor on main trunk */
  682.         VOID partialno(&t1, r1, l1>2 ? (unsigned)2 : l1);
  683.         VOID partialno(&t2, r2, l2>2 ? (unsigned)2 : l2);
  684.         r = cmpnum(t1.string,t2.string)<0 ? t1.string : t2.string;
  685.         if (cmpnum(r,r1)!=0 && cmpnum(r,r2)!=0)
  686.             return r;
  687.         } else if (cmpnumfld(r1, r2, l3+1)!=0)
  688.             return partialno(&t1,r1,l3);
  689.     }
  690.     error("common ancestor of %s and %s undefined", r1, r2);
  691.     return nil;
  692. }
  693.  
  694.  
  695.  
  696.     static int
  697. buildjoin(initialfile)
  698.     char const *initialfile;
  699. /* Function: merge pairs of elements in joinlist into initialfile
  700.  * If workstdout is set, copy result to stdout.
  701.  * All unlinking of initialfile, rev2, and rev3 should be done by tempunlink().
  702.  */
  703. {
  704.     struct buf commarg;
  705.     struct buf subs;
  706.     char const *rev2, *rev3;
  707.         int i;
  708.     char const *cov[10], *mergev[12];
  709.     char const **p;
  710.  
  711.     bufautobegin(&commarg);
  712.     bufautobegin(&subs);
  713.     rev2 = maketemp(0);
  714.     rev3 = maketemp(3); /* buildrevision() may use 1 and 2 */
  715.  
  716.     cov[0] = nil;
  717.     /* cov[1] setup below */
  718.     cov[2] = CO;
  719.     /* cov[3] setup below */
  720.     p = &cov[4];
  721.     if (expandarg) *p++ = expandarg;
  722.     if (suffixarg) *p++ = suffixarg;
  723.     if (versionarg) *p++ = versionarg;
  724.     *p++ = quietarg;
  725.     *p++ = RCSfilename;
  726.     *p = nil;
  727.  
  728.     mergev[0] = nil;
  729.     mergev[1] = nil;
  730.     mergev[2] = MERGE;
  731.     mergev[3] = mergev[5] = "-L";
  732.     /* rest of mergev setup below */
  733.  
  734.         i=0;
  735.         while (i<lastjoin) {
  736.                 /*prepare marker for merge*/
  737.                 if (i==0)
  738.             bufscpy(&subs, targetdelta->num);
  739.         else {
  740.             bufscat(&subs, ",");
  741.             bufscat(&subs, joinlist[i-2]);
  742.             bufscat(&subs, ":");
  743.             bufscat(&subs, joinlist[i-1]);
  744.         }
  745.         diagnose("revision %s\n",joinlist[i]);
  746.         bufscpy(&commarg, "-p");
  747.         bufscat(&commarg, joinlist[i]);
  748.         cov[1] = rev2;
  749.         cov[3] = commarg.string;
  750.         if (runv(cov))
  751.             goto badmerge;
  752.         diagnose("revision %s\n",joinlist[i+1]);
  753.         bufscpy(&commarg, "-p");
  754.         bufscat(&commarg, joinlist[i+1]);
  755.         cov[1] = rev3;
  756.         cov[3] = commarg.string;
  757.         if (runv(cov))
  758.             goto badmerge;
  759.         diagnose("merging...\n");
  760.         mergev[4] = subs.string;
  761.         mergev[6] = joinlist[i+1];
  762.         p = &mergev[7];
  763.         if (quietflag) *p++ = quietarg;
  764.         if (lastjoin<=i+2 && workstdout) *p++ = "-p";
  765.         *p++ = initialfile;
  766.         *p++ = rev2;
  767.         *p++ = rev3;
  768.         *p = nil;
  769.         switch (runv(mergev)) {
  770.             case DIFF_FAILURE: case DIFF_SUCCESS:
  771.             break;
  772.             default:
  773.             goto badmerge;
  774.         }
  775.                 i=i+2;
  776.         }
  777.     bufautoend(&commarg);
  778.     bufautoend(&subs);
  779.         return true;
  780.  
  781.     badmerge:
  782.     nerror++;
  783.     bufautoend(&commarg);
  784.     bufautoend(&subs);
  785.     return false;
  786. }
  787.