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

  1. /*
  2.  *                      RCS create/change operation
  3.  */
  4. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  5.    Copyright 1990, 1991, 1992 by Paul Eggert
  6.    Distributed under license by the Free Software Foundation, Inc.
  7.  
  8. This file is part of RCS.
  9.  
  10. RCS is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2, or (at your option)
  13. any later version.
  14.  
  15. RCS is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with RCS; see the file COPYING.  If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. Report problems and direct all questions to:
  25.  
  26.     rcs-bugs@cs.purdue.edu
  27.  
  28. */
  29.  
  30.  
  31.  
  32.  
  33. /* $Log: rcs.c,v $
  34.  * Revision 5.15  1992/02/17  23:02:20  eggert
  35.  * Add -T.
  36.  *
  37.  * Revision 5.14  1992/01/27  16:42:53  eggert
  38.  * Add -M.  Avoid invoking umask(); it's one less thing to configure.
  39.  * Add support for bad_creat0.  lint -> RCS_lint
  40.  *
  41.  * Revision 5.13  1992/01/06  02:42:34  eggert
  42.  * Avoid changing RCS file in common cases where no change can occur.
  43.  *
  44.  * Revision 5.12  1991/11/20  17:58:08  eggert
  45.  * Don't read the delta tree from a nonexistent RCS file.
  46.  *
  47.  * Revision 5.11  1991/10/07  17:32:46  eggert
  48.  * Remove lint.
  49.  *
  50.  * Revision 5.10  1991/08/19  23:17:54  eggert
  51.  * Add -m, -r$, piece tables.  Revision separator is `:', not `-'.  Tune.
  52.  *
  53.  * Revision 5.9  1991/04/21  11:58:18  eggert
  54.  * Add -x, RCSINIT, MS-DOS support.
  55.  *
  56.  * Revision 5.8  1991/02/25  07:12:38  eggert
  57.  * strsave -> str_save (DG/UX name clash)
  58.  * 0444 -> S_IRUSR|S_IRGRP|S_IROTH for portability
  59.  *
  60.  * Revision 5.7  1990/12/18  17:19:21  eggert
  61.  * Fix bug with multiple -n and -N options.
  62.  *
  63.  * Revision 5.6  1990/12/04  05:18:40  eggert
  64.  * Use -I for prompts and -q for diagnostics.
  65.  *
  66.  * Revision 5.5  1990/11/11  00:06:35  eggert
  67.  * Fix `rcs -e' core dump.
  68.  *
  69.  * Revision 5.4  1990/11/01  05:03:33  eggert
  70.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  71.  *
  72.  * Revision 5.3  1990/10/04  06:30:16  eggert
  73.  * Accumulate exit status across files.
  74.  *
  75.  * Revision 5.2  1990/09/04  08:02:17  eggert
  76.  * Standardize yes-or-no procedure.
  77.  *
  78.  * Revision 5.1  1990/08/29  07:13:51  eggert
  79.  * Remove unused setuid support.  Clean old log messages too.
  80.  *
  81.  * Revision 5.0  1990/08/22  08:12:42  eggert
  82.  * Don't lose names when applying -a option to multiple files.
  83.  * Remove compile-time limits; use malloc instead.  Add setuid support.
  84.  * Permit dates past 1999/12/31.  Make lock and temp files faster and safer.
  85.  * Ansify and Posixate.  Add -V.  Fix umask bug.  Make linting easier.  Tune.
  86.  * Yield proper exit status.  Check diff's output.
  87.  *
  88.  * Revision 4.11  89/05/01  15:12:06  narten
  89.  * changed copyright header to reflect current distribution rules
  90.  *
  91.  * Revision 4.10  88/11/08  16:01:54  narten
  92.  * didn't install previous patch correctly
  93.  *
  94.  * Revision 4.9  88/11/08  13:56:01  narten
  95.  * removed include <sysexits.h> (not needed)
  96.  * minor fix for -A option
  97.  *
  98.  * Revision 4.8  88/08/09  19:12:27  eggert
  99.  * Don't access freed storage.
  100.  * Use execv(), not system(); yield proper exit status; remove lint.
  101.  *
  102.  * Revision 4.7  87/12/18  11:37:17  narten
  103.  * lint cleanups (Guy Harris)
  104.  *
  105.  * Revision 4.6  87/10/18  10:28:48  narten
  106.  * Updating verison numbers. Changes relative to 1.1 are actually
  107.  * relative to 4.3
  108.  *
  109.  * Revision 1.4  87/09/24  13:58:52  narten
  110.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  111.  * warnings)
  112.  *
  113.  * Revision 1.3  87/03/27  14:21:55  jenkins
  114.  * Port to suns
  115.  *
  116.  * Revision 1.2  85/12/17  13:59:09  albitz
  117.  * Changed setstate to rcs_setstate because of conflict with random.o.
  118.  *
  119.  * Revision 4.3  83/12/15  12:27:33  wft
  120.  * rcs -u now breaks most recent lock if it can't find a lock by the caller.
  121.  *
  122.  * Revision 4.2  83/12/05  10:18:20  wft
  123.  * Added conditional compilation for sending mail.
  124.  * Alternatives: V4_2BSD, V6, USG, and other.
  125.  *
  126.  * Revision 4.1  83/05/10  16:43:02  wft
  127.  * Simplified breaklock(); added calls to findlock() and getcaller().
  128.  * Added option -b (default branch). Updated -s and -w for -b.
  129.  * Removed calls to stat(); now done by pairfilenames().
  130.  * Replaced most catchints() calls with restoreints().
  131.  * Removed check for exit status of delivermail().
  132.  * Directed all interactive output to stderr.
  133.  *
  134.  * Revision 3.9.1.1  83/12/02  22:08:51  wft
  135.  * Added conditional compilation for 4.2 sendmail and 4.1 delivermail.
  136.  *
  137.  * Revision 3.9  83/02/15  15:38:39  wft
  138.  * Added call to fastcopy() to copy remainder of RCS file.
  139.  *
  140.  * Revision 3.8  83/01/18  17:37:51  wft
  141.  * Changed sendmail(): now uses delivermail, and asks whether to break the lock.
  142.  *
  143.  * Revision 3.7  83/01/15  18:04:25  wft
  144.  * Removed putree(); replaced with puttree() in rcssyn.c.
  145.  * Combined putdellog() and scanlogtext(); deleted putdellog().
  146.  * Cleaned up diagnostics and error messages. Fixed problem with
  147.  * mutilated files in case of deletions in 2 files in a single command.
  148.  * Changed marking of selector from 'D' to DELETE.
  149.  *
  150.  * Revision 3.6  83/01/14  15:37:31  wft
  151.  * Added ignoring of interrupts while new RCS file is renamed;
  152.  * Avoids deletion of RCS files by interrupts.
  153.  *
  154.  * Revision 3.5  82/12/10  21:11:39  wft
  155.  * Removed unused variables, fixed checking of return code from diff,
  156.  * introduced variant COMPAT2 for skipping Suffix on -A files.
  157.  *
  158.  * Revision 3.4  82/12/04  13:18:20  wft
  159.  * Replaced getdelta() with gettree(), changed breaklock to update
  160.  * field lockedby, added some diagnostics.
  161.  *
  162.  * Revision 3.3  82/12/03  17:08:04  wft
  163.  * Replaced getlogin() with getpwuid(), flcose() with ffclose(),
  164.  * /usr/ucb/Mail with macro MAIL. Removed handling of Suffix (-x).
  165.  * fixed -u for missing revno. Disambiguated structure members.
  166.  *
  167.  * Revision 3.2  82/10/18  21:05:07  wft
  168.  * rcs -i now generates a file mode given by the umask minus write permission;
  169.  * otherwise, rcs keeps the mode, but removes write permission.
  170.  * I added a check for write error, fixed call to getlogin(), replaced
  171.  * curdir() with getfullRCSname(), cleaned up handling -U/L, and changed
  172.  * conflicting, long identifiers.
  173.  *
  174.  * Revision 3.1  82/10/13  16:11:07  wft
  175.  * fixed type of variables receiving from getc() (char -> int).
  176.  */
  177.  
  178.  
  179. #include "rcsbase.h"
  180.  
  181. struct  Lockrev {
  182.     char const *revno;
  183.         struct  Lockrev   * nextrev;
  184. };
  185.  
  186. struct  Symrev {
  187.     char const *revno;
  188.     char const *ssymbol;
  189.         int     override;
  190.         struct  Symrev  * nextsym;
  191. };
  192.  
  193. struct Message {
  194.     char const *revno;
  195.     struct cbuf message;
  196.     struct Message *nextmessage;
  197. };
  198.  
  199. struct  Status {
  200.     char const *revno;
  201.     char const *status;
  202.         struct  Status  * nextstatus;
  203. };
  204.  
  205. enum changeaccess {append, erase};
  206. struct chaccess {
  207.     char const *login;
  208.     enum changeaccess command;
  209.     struct chaccess *nextchaccess;
  210. };
  211.  
  212. struct delrevpair {
  213.     char const *strt;
  214.     char const *end;
  215.         int     code;
  216. };
  217.  
  218. static int breaklock P((struct hshentry const*));
  219. static int buildeltatext P((struct hshentries const*));
  220. static int doaccess P((void));
  221. static int doassoc P((void));
  222. static int dolocks P((void));
  223. static int domessages P((void));
  224. static int rcs_setstate P((char const*,char const*));
  225. static int removerevs P((void));
  226. static int sendmail P((char const*,char const*));
  227. static int setlock P((char const*));
  228. static struct Lockrev *rmnewlocklst P((struct Lockrev const*));
  229. static void buildtree P((void));
  230. static void cleanup P((void));
  231. static void getaccessor P((char*,enum changeaccess));
  232. static void getassoclst P((int,char*));
  233. static void getchaccess P((char const*,enum changeaccess));
  234. static void getdelrev P((char*));
  235. static void getmessage P((char*));
  236. static void getstates P((char*));
  237. static void scanlogtext P((struct hshentry*,int));
  238.  
  239. static struct buf numrev;
  240. static char const *headstate;
  241. static int chgheadstate, exitstatus, lockhead, unlockcaller;
  242. static int suppress_mail;
  243. static struct Lockrev *newlocklst, *rmvlocklst;
  244. static struct Message *messagelst, **nextmessage;
  245. static struct Status *statelst, **nextstate;
  246. static struct Symrev *assoclst, **nextassoc;
  247. static struct chaccess *chaccess, **nextchaccess;
  248. static struct delrevpair delrev;
  249. static struct hshentry *cuthead, *cuttail, *delstrt;
  250. static struct hshentries *gendeltas;
  251.  
  252. mainProg(rcsId, "rcs", "$Id: rcs.c,v 5.15 1992/02/17 23:02:20 eggert Exp $")
  253. {
  254.     static char const cmdusage[] =
  255.         "\nrcs usage: rcs -{ae}logins -Afile -{blu}[rev] -cstring -{iILqU} -ksubst -mrev:msg -{nN}name[:[rev]] -orange -sstate[:rev] -t[text] -Vn -xsuff file ...";
  256.  
  257.     char *a, **newargv, *textfile;
  258.     char const *branchsym, *commsyml;
  259.     int branchflag, changed, expmode, initflag, mode_already_ok;
  260.     int strictlock, strict_selected, textflag;
  261.     int keepRCStime, Ttimeflag;
  262.     size_t commsymlen;
  263.     struct buf branchnum;
  264.         struct  Lockrev *curlock,  * rmvlock, *lockpt;
  265.         struct  Status  * curstate;
  266.  
  267.     nosetid();
  268.  
  269.     nextassoc = &assoclst;
  270.     nextchaccess = &chaccess;
  271.     nextmessage = &messagelst;
  272.     nextstate = &statelst;
  273.     branchsym = commsyml = textfile = nil;
  274.     branchflag = strictlock = false;
  275.     bufautobegin(&branchnum);
  276.     commsymlen = 0;
  277.     curlock = rmvlock = nil;
  278.     expmode = -1;
  279.     suffixes = X_DEFAULT;
  280.         initflag= textflag = false;
  281.         strict_selected = 0;
  282.     Ttimeflag = false;
  283.  
  284.         /*  preprocessing command options    */
  285.     argc = getRCSINIT(argc, argv, &newargv);
  286.     argv = newargv;
  287.     while (a = *++argv,  0<--argc && *a++=='-') {
  288.         switch (*a++) {
  289.  
  290.         case 'i':   /*  initial version  */
  291.                         initflag = true;
  292.                         break;
  293.  
  294.                 case 'b':  /* change default branch */
  295.             if (branchflag) redefined('b');
  296.                         branchflag= true;
  297.             branchsym = a;
  298.                         break;
  299.  
  300.                 case 'c':   /*  change comment symbol   */
  301.             if (commsyml) redefined('c');
  302.             commsyml = a;
  303.             commsymlen = strlen(a);
  304.                         break;
  305.  
  306.                 case 'a':  /*  add new accessor   */
  307.             getaccessor(*argv+1, append);
  308.                         break;
  309.  
  310.                 case 'A':  /*  append access list according to accessfile  */
  311.             if (!*a) {
  312.                 error("missing file name after -A");
  313.                             break;
  314.                         }
  315.             *argv = a;
  316.             if (0 < pairfilenames(1,argv,rcsreadopen,true,false)) {
  317.                 while (AccessList) {
  318.                 getchaccess(str_save(AccessList->login),append);
  319.                 AccessList = AccessList->nextaccess;
  320.                 }
  321.                 Izclose(&finptr);
  322.                         }
  323.                         break;
  324.  
  325.                 case 'e':    /*  remove accessors   */
  326.             getaccessor(*argv+1, erase);
  327.                         break;
  328.  
  329.                 case 'l':    /*   lock a revision if it is unlocked   */
  330.             if (!*a) {
  331.                 /* Lock head or default branch.  */
  332.                             lockhead = true;
  333.                             break;
  334.                         }
  335.             lockpt = talloc(struct Lockrev);
  336.             lockpt->revno = a;
  337.                         lockpt->nextrev = nil;
  338.                         if ( curlock )
  339.                             curlock->nextrev = lockpt;
  340.                         else
  341.                             newlocklst = lockpt;
  342.                         curlock = lockpt;
  343.                         break;
  344.  
  345.                 case 'u':   /*  release lock of a locked revision   */
  346.             if (!*a) {
  347.                             unlockcaller=true;
  348.                             break;
  349.                         }
  350.             lockpt = talloc(struct Lockrev);
  351.             lockpt->revno = a;
  352.                         lockpt->nextrev = nil;
  353.                         if (rmvlock)
  354.                             rmvlock->nextrev = lockpt;
  355.                         else
  356.                             rmvlocklst = lockpt;
  357.                         rmvlock = lockpt;
  358.  
  359.                         curlock = rmnewlocklst(lockpt);
  360.                         break;
  361.  
  362.                 case 'L':   /*  set strict locking */
  363.             if (strict_selected) {
  364.                if (!strictlock)      /* Already selected -U? */
  365.                    warn("-L overrides -U.");
  366.                         }
  367.                         strictlock = true;
  368.             strict_selected = true;
  369.                         break;
  370.  
  371.                 case 'U':   /*  release strict locking */
  372.             if (strict_selected) {
  373.                if (strictlock)      /* Already selected -L? */
  374.                    warn("-L overrides -U.");
  375.                         }
  376.             strict_selected = true;
  377.                         break;
  378.  
  379.                 case 'n':    /*  add new association: error, if name exists */
  380.             if (!*a) {
  381.                 error("missing symbolic name after -n");
  382.                             break;
  383.                         }
  384.                         getassoclst(false, (*argv)+1);
  385.                         break;
  386.  
  387.                 case 'N':   /*  add or change association   */
  388.             if (!*a) {
  389.                 error("missing symbolic name after -N");
  390.                             break;
  391.                         }
  392.                         getassoclst(true, (*argv)+1);
  393.                         break;
  394.  
  395.         case 'm':   /*  change log message  */
  396.             getmessage(a);
  397.             break;
  398.  
  399.         case 'M':   /*  do not send mail */
  400.             suppress_mail = true;
  401.             break;
  402.  
  403.         case 'o':   /*  delete revisions  */
  404.             if (delrev.strt) redefined('o');
  405.             if (!*a) {
  406.                 error("missing revision range after -o");
  407.                             break;
  408.                         }
  409.                         getdelrev( (*argv)+1 );
  410.                         break;
  411.  
  412.                 case 's':   /*  change state attribute of a revision  */
  413.             if (!*a) {
  414.                 error("state missing after -s");
  415.                             break;
  416.                         }
  417.                         getstates( (*argv)+1);
  418.                         break;
  419.  
  420.                 case 't':   /*  change descriptive text   */
  421.                         textflag=true;
  422.             if (*a) {
  423.                 if (textfile) redefined('t');
  424.                 textfile = a;
  425.                         }
  426.                         break;
  427.  
  428.         case 'T':  /*  do not update last-mod time for minor changes */
  429.             if (*a)
  430.                 goto unknown;
  431.             Ttimeflag = true;
  432.             break;
  433.  
  434.         case 'I':
  435.             interactiveflag = true;
  436.             break;
  437.  
  438.                 case 'q':
  439.                         quietflag = true;
  440.                         break;
  441.  
  442.         case 'x':
  443.             suffixes = a;
  444.             break;
  445.  
  446.         case 'V':
  447.             setRCSversion(*argv);
  448.             break;
  449.  
  450.         case 'k':    /*  set keyword expand mode  */
  451.             if (0 <= expmode) redefined('k');
  452.             if (0 <= (expmode = str2expmode(a)))
  453.                 break;
  454.             /* fall into */
  455.                 default:
  456.         unknown:
  457.             faterror("unknown option: %s%s", *argv, cmdusage);
  458.                 };
  459.         }  /* end processing of options */
  460.  
  461.     if (argc<1) faterror("no input file%s", cmdusage);
  462.         if (nerror) {
  463.         diagnose("%s aborted\n",cmdid);
  464.         exitmain(EXIT_FAILURE);
  465.         }
  466.  
  467.         /* now handle all filenames */
  468.         do {
  469.     ffree();
  470.  
  471.         if ( initflag ) {
  472.         switch (pairfilenames(argc, argv, rcswriteopen, false, false)) {
  473.                 case -1: break;        /*  not exist; ok */
  474.                 case  0: continue;     /*  error         */
  475.                 case  1: error("file %s exists already", RCSfilename);
  476.                          continue;
  477.             }
  478.     }
  479.         else  {
  480.         switch (pairfilenames(argc, argv, rcswriteopen, true, false)) {
  481.                 case -1: continue;    /*  not exist      */
  482.                 case  0: continue;    /*  errors         */
  483.                 case  1: break;       /*  file exists; ok*/
  484.             }
  485.     }
  486.  
  487.  
  488.         /* now RCSfilename contains the name of the RCS file, and
  489.          * workfilename contains the name of the working file.
  490.          * if !initflag, finptr contains the file descriptor for the
  491.          * RCS file. The admin node is initialized.
  492.          */
  493.  
  494.     diagnose("RCS file: %s\n", RCSfilename);
  495.  
  496.     changed = initflag;
  497.     keepRCStime = Ttimeflag;
  498.     if (!initflag) {
  499.         if (!checkaccesslist()) continue;
  500.         gettree(); /* Read the delta tree.  */
  501.     }
  502.  
  503.         /*  update admin. node    */
  504.     if (strict_selected) {
  505.         changed  |=  StrictLocks ^ strictlock;
  506.         StrictLocks = strictlock;
  507.     }
  508.     if (
  509.         commsyml &&
  510.         (
  511.         commsymlen != Comment.size ||
  512.         memcmp(commsyml, Comment.string, commsymlen) != 0
  513.         )
  514.     ) {
  515.         Comment.string = commsyml;
  516.         Comment.size = strlen(commsyml);
  517.         changed = true;
  518.     }
  519.     if (0 <= expmode  &&  Expand != expmode) {
  520.         Expand = expmode;
  521.         changed = true;
  522.     }
  523.  
  524.         /* update default branch */
  525.     if (branchflag && expandsym(branchsym, &branchnum)) {
  526.         if (countnumflds(branchnum.string)) {
  527.         if (cmpnum(Dbranch, branchnum.string) != 0) {
  528.             Dbranch = branchnum.string;
  529.             changed = true;
  530.         }
  531.             } else
  532.         if (Dbranch) {
  533.             Dbranch = nil;
  534.             changed = true;
  535.         }
  536.     }
  537.  
  538.     changed |= doaccess();    /* Update access list.  */
  539.  
  540.     changed |= doassoc();    /* Update association list.  */
  541.  
  542.     changed |= dolocks();    /* Update locks.  */
  543.  
  544.     changed |= domessages();    /* Update log messages.  */
  545.  
  546.         /*  update state attribution  */
  547.         if (chgheadstate) {
  548.             /* change state of default branch or head */
  549.             if (Dbranch==nil) {
  550.                 if (Head==nil)
  551.              warn("can't change states in an empty tree");
  552.         else if (strcmp(Head->state, headstate) != 0) {
  553.             Head->state = headstate;
  554.             changed = true;
  555.         }
  556.         } else
  557.         changed |= rcs_setstate(Dbranch,headstate);
  558.         }
  559.     for (curstate = statelst;  curstate;  curstate = curstate->nextstatus)
  560.         changed |= rcs_setstate(curstate->revno,curstate->status);
  561.  
  562.         cuthead = cuttail = nil;
  563.     if (delrev.strt && removerevs()) {
  564.             /*  rebuild delta tree if some deltas are deleted   */
  565.             if ( cuttail )
  566.         VOID genrevs(cuttail->num, (char *)nil,(char *)nil,
  567.                  (char *)nil, &gendeltas);
  568.             buildtree();
  569.         changed = true;
  570.         keepRCStime = false;
  571.         }
  572.  
  573.     mode_already_ok = 0;
  574.     if (initflag) {
  575.         /* Set up RCSstat for an imaginary previous RCS file.  */
  576.         if (stat(workfilename, &RCSstat) != 0)
  577. #            if bad_creat0
  578.                 if (fstat(fdlock, &RCSstat) != 0)
  579.                     efaterror(RCSfilename);
  580. #            else
  581.                 mode_already_ok = 1;
  582. #            endif
  583.         RCSstat.st_nlink = 0;
  584.     }
  585.  
  586.     if (nerror)
  587.         continue;
  588.  
  589.     putadmin();
  590.         if ( Head )
  591.            puttree(Head, frewrite);
  592.     putdesc(textflag,textfile);
  593.  
  594.         if ( Head) {
  595.         if (delrev.strt || messagelst) {
  596.         if (!cuttail || buildeltatext(gendeltas)) {
  597.             advise_access(finptr, MADV_SEQUENTIAL);
  598.             scanlogtext((struct hshentry *)nil, false);
  599.                     /* copy rest of delta text nodes that are not deleted      */
  600.             changed = true;
  601.         }
  602.             }
  603.         }
  604.  
  605.     if (donerewrite(mode_already_ok ? -1 : changed, keepRCStime) != 0)
  606.         break;
  607.  
  608.     diagnose("done\n");
  609.     } while (cleanup(),
  610.                  ++argv, --argc >=1);
  611.  
  612.     tempunlink();
  613.     exitmain(exitstatus);
  614. }       /* end of main (rcs) */
  615.  
  616.     static void
  617. cleanup()
  618. {
  619.     if (nerror) exitstatus = EXIT_FAILURE;
  620.     Izclose(&finptr);
  621.     Ozclose(&fcopy);
  622.     ORCSclose();
  623.     dirtempunlink();
  624. }
  625.  
  626.     exiting void
  627. exiterr()
  628. {
  629.     dirtempunlink();
  630.     tempunlink();
  631.     _exit(EXIT_FAILURE);
  632. }
  633.  
  634.  
  635.     static void
  636. getassoclst(flag, sp)
  637. int     flag;
  638. char    * sp;
  639. /*  Function:   associate a symbolic name to a revision or branch,      */
  640. /*              and store in assoclst                                   */
  641.  
  642. {
  643.         struct   Symrev  * pt;
  644.     char const *temp;
  645.         int                c;
  646.  
  647.     while ((c = *++sp) == ' ' || c == '\t' || c =='\n')
  648.         continue;
  649.         temp = sp;
  650.     sp = checkid(sp, ':');  /*  check for invalid symbolic name  */
  651.     c = *sp;   *sp = '\0';
  652.         while( c == ' ' || c == '\t' || c == '\n')  c = *++sp;
  653.  
  654.         if ( c != ':' && c != '\0') {
  655.         error("invalid string %s after option -n or -N",sp);
  656.             return;
  657.         }
  658.  
  659.     pt = talloc(struct Symrev);
  660.         pt->ssymbol = temp;
  661.         pt->override = flag;
  662.     if (c == '\0')  /*  delete symbol  */
  663.             pt->revno = nil;
  664.         else {
  665.         while ((c = *++sp) == ' ' || c == '\n' || c == '\t')
  666.         continue;
  667.         pt->revno = sp;
  668.         }
  669.         pt->nextsym = nil;
  670.     *nextassoc = pt;
  671.     nextassoc = &pt->nextsym;
  672. }
  673.  
  674.  
  675.     static void
  676. getchaccess(login, command)
  677.     char const *login;
  678.     enum changeaccess command;
  679. {
  680.     register struct chaccess *pt;
  681.  
  682.     pt = talloc(struct chaccess);
  683.     pt->login = login;
  684.     pt->command = command;
  685.     pt->nextchaccess = nil;
  686.     *nextchaccess = pt;
  687.     nextchaccess = &pt->nextchaccess;
  688. }
  689.  
  690.  
  691.  
  692.     static void
  693. getaccessor(opt, command)
  694.     char *opt;
  695.     enum changeaccess command;
  696. /*   Function:  get the accessor list of options -e and -a,     */
  697. /*        and store in chaccess                */
  698.  
  699.  
  700. {
  701.         register c;
  702.     register char *sp;
  703.  
  704.     sp = opt;
  705.     while ((c = *++sp) == ' ' || c == '\n' || c == '\t' || c == ',')
  706.         continue;
  707.         if ( c == '\0') {
  708.         if (command == erase  &&  sp-opt == 1) {
  709.         getchaccess((char const*)nil, command);
  710.         return;
  711.         }
  712.         error("missing login name after option -a or -e");
  713.         return;
  714.         }
  715.  
  716.         while( c != '\0') {
  717.         getchaccess(sp, command);
  718.         sp = checkid(sp,',');
  719.         c = *sp;   *sp = '\0';
  720.                 while( c == ' ' || c == '\n' || c == '\t'|| c == ',')c =(*++sp);
  721.         }
  722. }
  723.  
  724.  
  725.     static void
  726. getmessage(option)
  727.     char *option;
  728. {
  729.     struct Message *pt;
  730.     struct cbuf cb;
  731.     char *m;
  732.  
  733.     if (!(m = strchr(option, ':'))) {
  734.         error("-m option lacks revision number");
  735.         return;
  736.     }
  737.     *m++ = 0;
  738.     cb = cleanlogmsg(m, strlen(m));
  739.     if (!cb.size) {
  740.         error("-m option lacks log message");
  741.         return;
  742.     }
  743.     pt = talloc(struct Message);
  744.     pt->revno = option;
  745.     pt->message = cb;
  746.     pt->nextmessage = 0;
  747.     *nextmessage = pt;
  748.     nextmessage = &pt->nextmessage;
  749. }
  750.  
  751.  
  752.     static void
  753. getstates(sp)
  754. char    *sp;
  755. /*   Function:  get one state attribute and the corresponding   */
  756. /*              revision and store in statelst                  */
  757.  
  758. {
  759.     char const *temp;
  760.         struct  Status  *pt;
  761.         register        c;
  762.  
  763.     while ((c = *++sp) ==' ' || c == '\t' || c == '\n')
  764.         continue;
  765.         temp = sp;
  766.     sp = checkid(sp,':');  /* check for invalid state attribute */
  767.     c = *sp;   *sp = '\0';
  768.         while( c == ' ' || c == '\t' || c == '\n' )  c = *++sp;
  769.  
  770.         if ( c == '\0' ) {  /*  change state of def. branch or Head  */
  771.             chgheadstate = true;
  772.             headstate  = temp;
  773.             return;
  774.         }
  775.         else if ( c != ':' ) {
  776.         error("missing ':' after state in option -s");
  777.             return;
  778.         }
  779.  
  780.     while ((c = *++sp) == ' ' || c == '\t' || c == '\n')
  781.         continue;
  782.     pt = talloc(struct Status);
  783.         pt->status     = temp;
  784.         pt->revno      = sp;
  785.         pt->nextstatus = nil;
  786.     *nextstate = pt;
  787.     nextstate = &pt->nextstatus;
  788. }
  789.  
  790.  
  791.  
  792.     static void
  793. getdelrev(sp)
  794. char    *sp;
  795. /*   Function:  get revision range or branch to be deleted,     */
  796. /*              and place in delrev                             */
  797. {
  798.         int    c;
  799.         struct  delrevpair      *pt;
  800.     int separator;
  801.  
  802.     pt = &delrev;
  803.     while ((c = (*++sp)) == ' ' || c == '\n' || c == '\t')
  804.         continue;
  805.  
  806.     /* Support old ambiguous '-' syntax; this will go away.  */
  807.     if (strchr(sp,':'))
  808.         separator = ':';
  809.     else {
  810.         if (strchr(sp,'-')  &&  VERSION(5) <= RCSversion)
  811.             warn("`-' is obsolete in `-o%s'; use `:' instead", sp);
  812.         separator = '-';
  813.     }
  814.  
  815.     if (c == separator) { /* -o:rev */
  816.         while ((c = (*++sp)) == ' ' || c == '\n' || c == '\t')
  817.         continue;
  818.             pt->strt = sp;    pt->code = 1;
  819.             while( c != ' ' && c != '\n' && c != '\t' && c != '\0') c =(*++sp);
  820.             *sp = '\0';
  821.         pt->end = nil;
  822.             return;
  823.         }
  824.         else {
  825.             pt->strt = sp;
  826.             while( c != ' ' && c != '\n' && c != '\t' && c != '\0'
  827.            && c != separator )  c = *++sp;
  828.             *sp = '\0';
  829.             while( c == ' ' || c == '\n' || c == '\t' )  c = *++sp;
  830.             if ( c == '\0' )  {  /*   -o rev or branch   */
  831.                 pt->end = nil;   pt->code = 0;
  832.                 return;
  833.             }
  834.         if (c != separator) {
  835.         faterror("invalid range %s %s after -o", pt->strt, sp);
  836.             }
  837.         while ((c = *++sp) == ' ' || c == '\n' || c == '\t')
  838.         continue;
  839.         if (!c) {  /* -orev: */
  840.                 pt->end = nil;   pt->code = 2;
  841.                 return;
  842.             }
  843.         }
  844.     /* -orev1:rev2 */
  845.     pt->end = sp;  pt->code = 3;
  846.         while( c!= ' ' && c != '\n' && c != '\t' && c != '\0') c = *++sp;
  847.         *sp = '\0';
  848. }
  849.  
  850.  
  851.  
  852.  
  853.     static void
  854. scanlogtext(delta,edit)
  855.     struct hshentry *delta;
  856.     int edit;
  857. /* Function: Scans delta text nodes up to and including the one given
  858.  * by delta, or up to last one present, if delta==nil.
  859.  * For the one given by delta (if delta!=nil), the log message is saved into
  860.  * delta->log if delta==cuttail; the text is edited if EDIT is set, else copied.
  861.  * Assumes the initial lexeme must be read in first.
  862.  * Does not advance nexttok after it is finished, except if delta==nil.
  863.  */
  864. {
  865.     struct hshentry const *nextdelta;
  866.     struct cbuf cb;
  867.  
  868.     for (;;) {
  869.         foutptr = 0;
  870.         if (eoflex()) {
  871.                     if(delta)
  872.             faterror("can't find delta for revision %s", delta->num);
  873.             return; /* no more delta text nodes */
  874.                 }
  875.         nextlex();
  876.         if (!(nextdelta=getnum()))
  877.             faterror("delta number corrupted");
  878.         if (nextdelta->selector) {
  879.             foutptr = frewrite;
  880.             aprintf(frewrite,DELNUMFORM,nextdelta->num,Klog);
  881.                 }
  882.         getkeystring(Klog);
  883.         if (nextdelta == cuttail) {
  884.             cb = savestring(&curlogbuf);
  885.             if (!delta->log.string)
  886.                 delta->log = cleanlogmsg(curlogbuf.string, cb.size);
  887.         } else if (nextdelta->log.string && nextdelta->selector) {
  888.             foutptr = 0;
  889.             readstring();
  890.             foutptr = frewrite;
  891.             putstring(foutptr, false, nextdelta->log, true);
  892.             afputc(nextc, foutptr);
  893.                 } else {readstring();
  894.                 }
  895.                 nextlex();
  896.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  897.             ignorephrase();
  898.         getkeystring(Ktext);
  899.  
  900.         if (delta==nextdelta)
  901.             break;
  902.         readstring(); /* skip over it */
  903.  
  904.     }
  905.     /* got the one we're looking for */
  906.     if (edit)
  907.         editstring((struct hshentry *)nil);
  908.     else
  909.         enterstring();
  910. }
  911.  
  912.  
  913.  
  914.     static struct Lockrev *
  915. rmnewlocklst(which)
  916.     struct Lockrev const *which;
  917. /*   Function:  remove lock to revision which->revno from newlocklst   */
  918.  
  919. {
  920.         struct  Lockrev   * pt, *pre;
  921.  
  922.         while( newlocklst && (! strcmp(newlocklst->revno, which->revno))){
  923.         struct Lockrev *pn = newlocklst->nextrev;
  924.         tfree(newlocklst);
  925.         newlocklst = pn;
  926.         }
  927.  
  928.         pt = pre = newlocklst;
  929.         while( pt ) {
  930.             if ( ! strcmp(pt->revno, which->revno) ) {
  931.                 pre->nextrev = pt->nextrev;
  932.         tfree(pt);
  933.         pt = pre->nextrev;
  934.             }
  935.             else {
  936.                 pre = pt;
  937.                 pt = pt->nextrev;
  938.             }
  939.         }
  940.         return pre;
  941. }
  942.  
  943.  
  944.  
  945.     static int
  946. doaccess()
  947. {
  948.     register struct chaccess *ch;
  949.     register struct access **p, *t;
  950.     register int changed = false;
  951.  
  952.     for (ch = chaccess;  ch;  ch = ch->nextchaccess) {
  953.         switch (ch->command) {
  954.         case erase:
  955.             if (!ch->login) {
  956.                 if (AccessList) {
  957.                 AccessList = nil;
  958.                 changed = true;
  959.                 }
  960.             } else
  961.                 for (p = &AccessList; (t = *p); p = &t->nextaccess)
  962.                 if (strcmp(ch->login, t->login) == 0) {
  963.                     *p = t->nextaccess;
  964.                     changed = true;
  965.                     break;
  966.                 }
  967.             break;
  968.         case append:
  969.             for (p = &AccessList;  ;  p = &t->nextaccess)
  970.                 if (!(t = *p)) {
  971.                     *p = t = ftalloc(struct access);
  972.                     t->login = ch->login;
  973.                     t->nextaccess = nil;
  974.                     changed = true;
  975.                     break;
  976.                 } else if (strcmp(ch->login, t->login) == 0)
  977.                     break;
  978.             break;
  979.         }
  980.     }
  981.     return changed;
  982. }
  983.  
  984.  
  985.     static int
  986. sendmail(Delta, who)
  987.     char const *Delta, *who;
  988. /*   Function:  mail to who, informing him that his lock on delta was
  989.  *   broken by caller. Ask first whether to go ahead. Return false on
  990.  *   error or if user decides not to break the lock.
  991.  */
  992. {
  993. #ifdef SENDMAIL
  994.     char const *messagefile;
  995.     int old1, old2, c;
  996.         FILE    * mailmess;
  997. #endif
  998.  
  999.  
  1000.     aprintf(stderr, "Revision %s is already locked by %s.\n", Delta, who);
  1001.     if (suppress_mail)
  1002.         return true;
  1003.     if (!yesorno(false, "Do you want to break the lock? [ny](n): "))
  1004.         return false;
  1005.  
  1006.         /* go ahead with breaking  */
  1007. #ifdef SENDMAIL
  1008.     messagefile = maketemp(0);
  1009.     if (!(mailmess = fopen(messagefile, "w"))) {
  1010.         efaterror(messagefile);
  1011.         }
  1012.  
  1013.     aprintf(mailmess, "Subject: Broken lock on %s\n\nYour lock on revision %s of file %s\nhas been broken by %s for the following reason:\n",
  1014.         basename(RCSfilename), Delta, getfullRCSname(), getcaller()
  1015.     );
  1016.     aputs("State the reason for breaking the lock:\n(terminate with single '.' or end of file)\n>> ", stderr);
  1017.  
  1018.         old1 = '\n';    old2 = ' ';
  1019.         for (; ;) {
  1020.         c = getcstdin();
  1021.         if (feof(stdin)) {
  1022.         aprintf(mailmess, "%c\n", old1);
  1023.                 break;
  1024.             }
  1025.             else if ( c == '\n' && old1 == '.' && old2 == '\n')
  1026.                 break;
  1027.             else {
  1028.         afputc(old1, mailmess);
  1029.                 old2 = old1;   old1 = c;
  1030.         if (c=='\n') aputs(">> ", stderr);
  1031.             }
  1032.         }
  1033.     Ozclose(&mailmess);
  1034.  
  1035.     if (run(messagefile, (char*)nil, SENDMAIL, who, (char*)nil))
  1036.         warn("Mail may have failed."),
  1037. #else
  1038.         warn("Mail notification of broken locks is not available."),
  1039. #endif
  1040.         warn("Please tell `%s' why you broke the lock.", who);
  1041.     return(true);
  1042. }
  1043.  
  1044.  
  1045.  
  1046.     static int
  1047. breaklock(delta)
  1048.     struct hshentry const *delta;
  1049. /* function: Finds the lock held by caller on delta,
  1050.  * and removes it.
  1051.  * Sends mail if a lock different from the caller's is broken.
  1052.  * Prints an error message if there is no such lock or error.
  1053.  */
  1054. {
  1055.     register struct lock *next, **trail;
  1056.     char const *num;
  1057.  
  1058.     num=delta->num;
  1059.     for (trail = &Locks;  (next = *trail);  trail = &next->nextlock)
  1060.         if (strcmp(num, next->delta->num) == 0) {
  1061.             if (
  1062.                 strcmp(getcaller(),next->login) != 0
  1063.                 &&    !sendmail(num, next->login)
  1064.             ) {
  1065.                 error("%s still locked by %s", num, next->login);
  1066.                 return false;
  1067.             }
  1068.             diagnose("%s unlocked\n", next->delta->num);
  1069.             *trail = next->nextlock;
  1070.             next->delta->lockedby = 0;
  1071.             return true;
  1072.                 }
  1073.     error("no lock set on revision %s", num);
  1074.     return false;
  1075. }
  1076.  
  1077.  
  1078.  
  1079.     static struct hshentry *
  1080. searchcutpt(object, length, store)
  1081.     char const *object;
  1082.     unsigned length;
  1083.     struct hshentries *store;
  1084. /*   Function:  Search store and return entry with number being object. */
  1085. /*              cuttail = nil, if the entry is Head; otherwise, cuttail */
  1086. /*              is the entry point to the one with number being object  */
  1087.  
  1088. {
  1089.     cuthead = nil;
  1090.     while (compartial(store->first->num, object, length)) {
  1091.         cuthead = store->first;
  1092.         store = store->rest;
  1093.     }
  1094.     return store->first;
  1095. }
  1096.  
  1097.  
  1098.  
  1099.     static int
  1100. branchpoint(strt, tail)
  1101. struct  hshentry        *strt,  *tail;
  1102. /*   Function: check whether the deltas between strt and tail    */
  1103. /*        are locked or branch point, return 1 if any is  */
  1104. /*        locked or branch point; otherwise, return 0 and */
  1105. /*        mark deleted                    */
  1106.  
  1107. {
  1108.         struct  hshentry    *pt;
  1109.     struct lock const *lockpt;
  1110.  
  1111.     for (pt = strt;  pt != tail;  pt = pt->next) {
  1112.             if ( pt->branches ){ /*  a branch point  */
  1113.         error("can't remove branch point %s", pt->num);
  1114.         return true;
  1115.             }
  1116.         for (lockpt = Locks;  lockpt;  lockpt = lockpt->nextlock)
  1117.         if (lockpt->delta == pt) {
  1118.             error("can't remove locked revision %s", pt->num);
  1119.             return true;
  1120.         }
  1121.         pt->selector = false;
  1122.         diagnose("deleting revision %s\n",pt->num);
  1123.         }
  1124.     return false;
  1125. }
  1126.  
  1127.  
  1128.  
  1129.     static int
  1130. removerevs()
  1131. /*   Function:  get the revision range to be removed, and place the     */
  1132. /*              first revision removed in delstrt, the revision before  */
  1133. /*              delstrt in cuthead( nil, if delstrt is head), and the   */
  1134. /*              revision after the last removed revision in cuttail(nil */
  1135. /*              if the last is a leaf                                   */
  1136.  
  1137. {
  1138.     struct  hshentry *target, *target2, *temp;
  1139.     unsigned length;
  1140.     int flag;
  1141.  
  1142.         flag = false;
  1143.     if (!expandsym(delrev.strt, &numrev)) return 0;
  1144.     target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1145.         if ( ! target ) return 0;
  1146.     if (cmpnum(target->num, numrev.string)) flag = true;
  1147.     length = countnumflds(numrev.string);
  1148.  
  1149.     if (delrev.code == 0) {  /*  -o  rev    or    -o  branch   */
  1150.         if (length & 1)
  1151.         temp=searchcutpt(target->num,length+1,gendeltas);
  1152.         else if (flag) {
  1153.         error("Revision %s doesn't exist.", numrev.string);
  1154.         return 0;
  1155.         }
  1156.         else
  1157.         temp = searchcutpt(numrev.string, length, gendeltas);
  1158.         cuttail = target->next;
  1159.             if ( branchpoint(temp, cuttail) ) {
  1160.                 cuttail = nil;
  1161.                 return 0;
  1162.             }
  1163.             delstrt = temp;     /* first revision to be removed   */
  1164.             return 1;
  1165.         }
  1166.  
  1167.     if (length & 1) {   /*  invalid branch after -o   */
  1168.         error("invalid branch range %s after -o", numrev.string);
  1169.             return 0;
  1170.         }
  1171.  
  1172.     if (delrev.code == 1) {  /*  -o  -rev   */
  1173.             if ( length > 2 ) {
  1174.                 temp = searchcutpt( target->num, length-1, gendeltas);
  1175.                 cuttail = target->next;
  1176.             }
  1177.             else {
  1178.                 temp = searchcutpt(target->num, length, gendeltas);
  1179.                 cuttail = target;
  1180.                 while( cuttail && ! cmpnumfld(target->num,cuttail->num,1) )
  1181.                     cuttail = cuttail->next;
  1182.             }
  1183.             if ( branchpoint(temp, cuttail) ){
  1184.                 cuttail = nil;
  1185.                 return 0;
  1186.             }
  1187.             delstrt = temp;
  1188.             return 1;
  1189.         }
  1190.  
  1191.     if (delrev.code == 2) {   /*  -o  rev-   */
  1192.             if ( length == 2 ) {
  1193.                 temp = searchcutpt(target->num, 1,gendeltas);
  1194.                 if ( flag)
  1195.                     cuttail = target;
  1196.                 else
  1197.                     cuttail = target->next;
  1198.             }
  1199.             else  {
  1200.                 if ( flag){
  1201.                     cuthead = target;
  1202.                     if ( !(temp = target->next) ) return 0;
  1203.                 }
  1204.                 else
  1205.                     temp = searchcutpt(target->num, length, gendeltas);
  1206.         getbranchno(temp->num, &numrev);  /* get branch number */
  1207.         target = genrevs(numrev.string, (char*)nil, (char*)nil, (char*)nil, &gendeltas);
  1208.             }
  1209.             if ( branchpoint( temp, cuttail ) ) {
  1210.                 cuttail = nil;
  1211.                 return 0;
  1212.             }
  1213.             delstrt = temp;
  1214.             return 1;
  1215.         }
  1216.  
  1217.         /*   -o   rev1-rev2   */
  1218.     if (!expandsym(delrev.end, &numrev)) return 0;
  1219.     if (
  1220.         length != countnumflds(numrev.string)
  1221.         ||    length>2 && compartial(numrev.string, target->num, length-1)
  1222.     ) {
  1223.         error("invalid revision range %s-%s", target->num, numrev.string);
  1224.             return 0;
  1225.         }
  1226.  
  1227.     target2 = genrevs(numrev.string,(char*)nil,(char*)nil,(char*)nil,&gendeltas);
  1228.         if ( ! target2 ) return 0;
  1229.  
  1230.         if ( length > 2) {  /* delete revisions on branches  */
  1231.             if ( cmpnum(target->num, target2->num) > 0) {
  1232.         if (cmpnum(target2->num, numrev.string))
  1233.                     flag = true;
  1234.                 else
  1235.                     flag = false;
  1236.                 temp = target;
  1237.                 target = target2;
  1238.                 target2 = temp;
  1239.             }
  1240.             if ( flag ) {
  1241.                 if ( ! cmpnum(target->num, target2->num) ) {
  1242.             error("Revisions %s-%s don't exist.", delrev.strt,delrev.end);
  1243.                     return 0;
  1244.                 }
  1245.                 cuthead = target;
  1246.                 temp = target->next;
  1247.             }
  1248.             else
  1249.                 temp = searchcutpt(target->num, length, gendeltas);
  1250.             cuttail = target2->next;
  1251.         }
  1252.         else { /*  delete revisions on trunk  */
  1253.             if ( cmpnum( target->num, target2->num) < 0 ) {
  1254.                 temp = target;
  1255.                 target = target2;
  1256.                 target2 = temp;
  1257.             }
  1258.             else
  1259.         if (cmpnum(target2->num, numrev.string))
  1260.                     flag = true;
  1261.                 else
  1262.                     flag = false;
  1263.             if ( flag ) {
  1264.                 if ( ! cmpnum(target->num, target2->num) ) {
  1265.             error("Revisions %s-%s don't exist.", delrev.strt, delrev.end);
  1266.                     return 0;
  1267.                 }
  1268.                 cuttail = target2;
  1269.             }
  1270.             else
  1271.                 cuttail = target2->next;
  1272.             temp = searchcutpt(target->num, length, gendeltas);
  1273.         }
  1274.         if ( branchpoint(temp, cuttail) )  {
  1275.             cuttail = nil;
  1276.             return 0;
  1277.         }
  1278.         delstrt = temp;
  1279.         return 1;
  1280. }
  1281.  
  1282.  
  1283.  
  1284.     static int
  1285. doassoc()
  1286. /*   Function: add or delete(if revno is nil) association    */
  1287. /*        which is stored in assoclst            */
  1288.  
  1289. {
  1290.     char const *p;
  1291.     int changed = false;
  1292.     struct Symrev const *curassoc;
  1293.     struct assoc **pre, *pt;
  1294.  
  1295.         /*  add new associations   */
  1296.     for (curassoc = assoclst;  curassoc;  curassoc = curassoc->nextsym) {
  1297.         char const *ssymbol = curassoc->ssymbol;
  1298.  
  1299.             if ( curassoc->revno == nil ) {  /* delete symbol  */
  1300.         for (pre = &Symbols;  ;  pre = &pt->nextassoc)
  1301.             if (!(pt = *pre)) {
  1302.             warn("can't delete nonexisting symbol %s", ssymbol);
  1303.             break;
  1304.             } else if (strcmp(pt->symbol, ssymbol) == 0) {
  1305.             *pre = pt->nextassoc;
  1306.             changed = true;
  1307.             break;
  1308.             }
  1309.         }
  1310.         else {
  1311.         if (curassoc->revno[0]) {
  1312.             p = 0;
  1313.             if (expandsym(curassoc->revno, &numrev))
  1314.             p = fstr_save(numrev.string);
  1315.         } else if (!(p = tiprev()))
  1316.             error("no latest revision to associate with symbol %s",
  1317.                 ssymbol
  1318.             );
  1319.         if (p)
  1320.             changed |= addsymbol(p, ssymbol, curassoc->override);
  1321.         }
  1322.         }
  1323.     return changed;
  1324. }
  1325.  
  1326.  
  1327.  
  1328.     static int
  1329. dolocks()
  1330. /* Function: remove lock for caller or first lock if unlockcaller is set;
  1331.  *           remove locks which are stored in rmvlocklst,
  1332.  *           add new locks which are stored in newlocklst,
  1333.  *           add lock for Dbranch or Head if lockhead is set.
  1334.  */
  1335. {
  1336.     struct Lockrev const *lockpt;
  1337.     struct hshentry *target;
  1338.     int changed = false, r;
  1339.  
  1340.     if (unlockcaller) { /*  find lock for caller  */
  1341.             if ( Head ) {
  1342.         if (Locks) {
  1343.             switch (findlock(true, &target)) {
  1344.               case 0:
  1345.             /* remove most recent lock */
  1346.             changed |= breaklock(Locks->delta);
  1347.             break;
  1348.               case 1:
  1349.             diagnose("%s unlocked\n",target->num);
  1350.             changed = true;
  1351.             break;
  1352.             }
  1353.         } else {
  1354.             warn("No locks are set.");
  1355.         }
  1356.             } else {
  1357.         warn("can't unlock an empty tree");
  1358.             }
  1359.         }
  1360.  
  1361.         /*  remove locks which are stored in rmvlocklst   */
  1362.     for (lockpt = rmvlocklst;  lockpt;  lockpt = lockpt->nextrev)
  1363.         if (expandsym(lockpt->revno, &numrev)) {
  1364.         target = genrevs(numrev.string, (char *)nil, (char *)nil, (char *)nil, &gendeltas);
  1365.                 if ( target )
  1366.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1367.             error("can't unlock nonexisting revision %s",lockpt->revno);
  1368.                    else
  1369.             changed |= breaklock(target);
  1370.                         /* breaklock does its own diagnose */
  1371.             }
  1372.  
  1373.         /*  add new locks which stored in newlocklst  */
  1374.     for (lockpt = newlocklst;  lockpt;  lockpt = lockpt->nextrev)
  1375.         changed |= setlock(lockpt->revno);
  1376.  
  1377.     if (lockhead) {  /*  lock default branch or head  */
  1378.             if (Dbranch) {
  1379.         changed |= setlock(Dbranch);
  1380.         } else if (Head) {
  1381.         if (0 <= (r = addlock(Head))) {
  1382.             changed |= r;
  1383.             if (r)
  1384.             diagnose("%s locked\n",Head->num);
  1385.         }
  1386.             } else {
  1387.         warn("can't lock an empty tree");
  1388.             }
  1389.         }
  1390.     return changed;
  1391. }
  1392.  
  1393.  
  1394.  
  1395.     static int
  1396. setlock(rev)
  1397.     char const *rev;
  1398. /* Function: Given a revision or branch number, finds the corresponding
  1399.  * delta and locks it for caller.
  1400.  */
  1401. {
  1402.         struct  hshentry *target;
  1403.     int r;
  1404.  
  1405.     if (expandsym(rev, &numrev)) {
  1406.         target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1407.                  (char*)nil, &gendeltas);
  1408.             if ( target )
  1409.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1410.             error("can't lock nonexisting revision %s", numrev.string);
  1411.                else
  1412.             if (0 <= (r = addlock(target))) {
  1413.             if (r)
  1414.                 diagnose("%s locked\n", target->num);
  1415.             return r;
  1416.             }
  1417.     }
  1418.     return 0;
  1419. }
  1420.  
  1421.  
  1422.     static int
  1423. domessages()
  1424. {
  1425.     struct hshentry *target;
  1426.     struct Message *p;
  1427.     int changed = false;
  1428.     size_t s;
  1429.  
  1430.     for (p = messagelst;  p;  p = p->nextmessage)
  1431.         if (
  1432.         expandsym(p->revno, &numrev)  &&
  1433.         (target = genrevs(
  1434.             numrev.string, (char*)0, (char*)0, (char*)0, &gendeltas
  1435.         )) &&
  1436.         (
  1437.             target->log.size != (s = p->message.size) ||
  1438.             memcmp(target->log.string, p->message.string, s) != 0
  1439.         )
  1440.         ) {
  1441.         target->log = p->message;
  1442.         changed = true;
  1443.         }
  1444.     return changed;
  1445. }
  1446.  
  1447.  
  1448.     static int
  1449. rcs_setstate(rev,status)
  1450.     char const *rev, *status;
  1451. /* Function: Given a revision or branch number, finds the corresponding delta
  1452.  * and sets its state to status.
  1453.  */
  1454. {
  1455.         struct  hshentry *target;
  1456.  
  1457.     if (expandsym(rev, &numrev)) {
  1458.         target = genrevs(numrev.string, (char*)nil, (char*)nil,
  1459.                  (char*)nil, &gendeltas);
  1460.             if ( target )
  1461.            if (!(countnumflds(numrev.string)&1) && cmpnum(target->num,numrev.string))
  1462.             error("can't set state of nonexisting revision %s to %s",
  1463.               numrev.string, status);
  1464.            else if (strcmp(target->state, status) != 0) {
  1465.                     target->state = status;
  1466.             return true;
  1467.            }
  1468.     }
  1469.     return false;
  1470. }
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.     static int
  1477. buildeltatext(deltas)
  1478.     struct hshentries const *deltas;
  1479. /*   Function:  put the delta text on frewrite and make necessary   */
  1480. /*              change to delta text                                */
  1481. {
  1482.     register FILE *fcut;    /* temporary file to rebuild delta tree */
  1483.     char const *cutfilename, *diffilename;
  1484.  
  1485.     cutfilename = nil;
  1486.     cuttail->selector = false;
  1487.     scanlogtext(deltas->first, false);
  1488.         if ( cuthead )  {
  1489.         cutfilename = maketemp(3);
  1490.         if (!(fcut = fopen(cutfilename, FOPEN_W_WORK))) {
  1491.         efaterror(cutfilename);
  1492.             }
  1493.  
  1494.         while (deltas->first != cuthead) {
  1495.         deltas = deltas->rest;
  1496.         scanlogtext(deltas->first, true);
  1497.             }
  1498.  
  1499.         snapshotedit(fcut);
  1500.         Ofclose(fcut);
  1501.         }
  1502.  
  1503.     while (deltas->first != cuttail)
  1504.         scanlogtext((deltas = deltas->rest)->first, true);
  1505.     finishedit((struct hshentry *)nil, (FILE*)0, true);
  1506.     Ozclose(&fcopy);
  1507.  
  1508.         if ( cuthead ) {
  1509.         diffilename = maketemp(0);
  1510.         switch (run((char*)nil,diffilename,
  1511.             DIFF DIFF_FLAGS, cutfilename, resultfile, (char*)nil
  1512.         )) {
  1513.         case DIFF_FAILURE: case DIFF_SUCCESS: break;
  1514.         default: faterror ("diff failed");
  1515.         }
  1516.         return putdtext(cuttail->num,cuttail->log,diffilename,frewrite,true);
  1517.     } else
  1518.         return putdtext(cuttail->num,cuttail->log,resultfile,frewrite,false);
  1519. }
  1520.  
  1521.  
  1522.  
  1523.     static void
  1524. buildtree()
  1525. /*   Function:  actually removes revisions whose selector field  */
  1526. /*        is false, and rebuilds the linkage of deltas.     */
  1527. /*              asks for reconfirmation if deleting last revision*/
  1528. {
  1529.     struct    hshentry   * Delta;
  1530.         struct  branchhead      *pt, *pre;
  1531.  
  1532.         if ( cuthead )
  1533.            if ( cuthead->next == delstrt )
  1534.                 cuthead->next = cuttail;
  1535.            else {
  1536.                 pre = pt = cuthead->branches;
  1537.                 while( pt && pt->hsh != delstrt )  {
  1538.                     pre = pt;
  1539.                     pt = pt->nextbranch;
  1540.                 }
  1541.                 if ( cuttail )
  1542.                     pt->hsh = cuttail;
  1543.                 else if ( pt == pre )
  1544.                     cuthead->branches = pt->nextbranch;
  1545.                 else
  1546.                     pre->nextbranch = pt->nextbranch;
  1547.             }
  1548.     else {
  1549.             if ( cuttail == nil && !quietflag) {
  1550.         if (!yesorno(false, "Do you really want to delete all revisions? [ny](n): ")) {
  1551.             error("No revision deleted");
  1552.             Delta = delstrt;
  1553.             while( Delta) {
  1554.             Delta->selector = true;
  1555.             Delta = Delta->next;
  1556.             }
  1557.             return;
  1558.         }
  1559.         }
  1560.             Head = cuttail;
  1561.     }
  1562.         return;
  1563. }
  1564.  
  1565. #if RCS_lint
  1566. /* This lets us lint everything all at once. */
  1567.  
  1568. char const cmdid[] = "";
  1569.  
  1570. #define go(p,e) {int p P((int,char**)); void e P((void)); if(*argv)return p(argc,argv);if(*argv[1])e();}
  1571.  
  1572.     int
  1573. main(argc, argv)
  1574.     int argc;
  1575.     char **argv;
  1576. {
  1577.     go(ciId,    ciExit);
  1578.     go(coId,    coExit);
  1579.     go(identId,    identExit);
  1580.     go(mergeId,    mergeExit);
  1581.     go(rcsId,    exiterr);
  1582.     go(rcscleanId,    rcscleanExit);
  1583.     go(rcsdiffId,    rdiffExit);
  1584.     go(rcsmergeId,    rmergeExit);
  1585.     go(rlogId,    rlogExit);
  1586.     return 0;
  1587. }
  1588. #endif
  1589.