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

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