home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs567s.zip / rcs / src / rlog.c < prev    next >
C/C++ Source or Header  |  1994-04-14  |  37KB  |  1,353 lines

  1. /* Print log messages and other information about 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: rlog.c,v $
  31.  * Revision 5.16  1994/04/13 16:30:34  eggert
  32.  * Fix bug; `rlog -lxxx' inverted the sense of -l.
  33.  *
  34.  * Revision 5.15  1994/03/17 14:05:48  eggert
  35.  * -d'<DATE' now excludes DATE; the new syntax -d'<=DATE' includes it.
  36.  * Emulate -V4's white space generation more precisely.
  37.  * Work around SVR4 stdio performance bug.  Remove lint.
  38.  *
  39.  * Revision 5.14  1993/11/09 17:40:15  eggert
  40.  * -V now prints version on stdout and exits.
  41.  *
  42.  * Revision 5.13  1993/11/03 17:42:27  eggert
  43.  * Add -N, -z.  Ignore -T.
  44.  *
  45.  * Revision 5.12  1992/07/28  16:12:44  eggert
  46.  * Don't miss B.0 when handling branch B.  Diagnose missing `,' in -r.
  47.  * Add -V.  Avoid `unsigned'.  Statement macro names now end in _.
  48.  *
  49.  * Revision 5.11  1992/01/24  18:44:19  eggert
  50.  * Don't duplicate unexpected_EOF's function.  lint -> RCS_lint
  51.  *
  52.  * Revision 5.10  1992/01/06  02:42:34  eggert
  53.  * Update usage string.
  54.  * while (E) ; -> while (E) continue;
  55.  *
  56.  * Revision 5.9  1991/09/17  19:07:40  eggert
  57.  * Getscript() didn't uncache partial lines.
  58.  *
  59.  * Revision 5.8  1991/08/19  03:13:55  eggert
  60.  * Revision separator is `:', not `-'.
  61.  * Check for missing and duplicate logs.  Tune.
  62.  * Permit log messages that do not end in newline (including empty logs).
  63.  *
  64.  * Revision 5.7  1991/04/21  11:58:31  eggert
  65.  * Add -x, RCSINIT, MS-DOS support.
  66.  *
  67.  * Revision 5.6  1991/02/26  17:07:17  eggert
  68.  * Survive RCS files with missing logs.
  69.  * strsave -> str_save (DG/UX name clash)
  70.  *
  71.  * Revision 5.5  1990/11/01  05:03:55  eggert
  72.  * Permit arbitrary data in logs and comment leaders.
  73.  *
  74.  * Revision 5.4  1990/10/04  06:30:22  eggert
  75.  * Accumulate exit status across files.
  76.  *
  77.  * Revision 5.3  1990/09/11  02:41:16  eggert
  78.  * Plug memory leak.
  79.  *
  80.  * Revision 5.2  1990/09/04  08:02:33  eggert
  81.  * Count RCS lines better.
  82.  *
  83.  * Revision 5.0  1990/08/22  08:13:48  eggert
  84.  * Remove compile-time limits; use malloc instead.  Add setuid support.
  85.  * Switch to GMT.
  86.  * Report dates in long form, to warn about dates past 1999/12/31.
  87.  * Change "added/del" message to make room for the longer dates.
  88.  * Don't generate trailing white space.  Add -V.  Ansify and Posixate.
  89.  *
  90.  * Revision 4.7  89/05/01  15:13:48  narten
  91.  * changed copyright header to reflect current distribution rules
  92.  * 
  93.  * Revision 4.6  88/08/09  19:13:28  eggert
  94.  * Check for memory exhaustion; don't access freed storage.
  95.  * Shrink stdio code size; remove lint.
  96.  * 
  97.  * Revision 4.5  87/12/18  11:46:38  narten
  98.  * more lint cleanups (Guy Harris)
  99.  * 
  100.  * Revision 4.4  87/10/18  10:41:12  narten
  101.  * Updating version numbers
  102.  * Changes relative to 1.1 actually relative to 4.2
  103.  * 
  104.  * Revision 1.3  87/09/24  14:01:10  narten
  105.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  106.  * warnings)
  107.  * 
  108.  * Revision 1.2  87/03/27  14:22:45  jenkins
  109.  * Port to suns
  110.  * 
  111.  * Revision 4.2  83/12/05  09:18:09  wft
  112.  * changed rewriteflag to external.
  113.  * 
  114.  * Revision 4.1  83/05/11  16:16:55  wft
  115.  * Added -b, updated getnumericrev() accordingly.
  116.  * Replaced getpwuid() with getcaller().
  117.  * 
  118.  * Revision 3.7  83/05/11  14:24:13  wft
  119.  * Added options -L and -R;
  120.  * Fixed selection bug with -l on multiple files.
  121.  * Fixed error on dates of the form -d'>date' (rewrote getdatepair()).
  122.  * 
  123.  * Revision 3.6  82/12/24  15:57:53  wft
  124.  * shortened output format.
  125.  *
  126.  * Revision 3.5  82/12/08  21:45:26  wft
  127.  * removed call to checkaccesslist(); used DATEFORM to format all dates;
  128.  * removed unused variables.
  129.  *
  130.  * Revision 3.4  82/12/04  13:26:25  wft
  131.  * Replaced getdelta() with gettree(); removed updating of field lockedby.
  132.  *
  133.  * Revision 3.3  82/12/03  14:08:20  wft
  134.  * Replaced getlogin with getpwuid(), %02d with %.2d, fancydate with PRINTDATE.
  135.  * Fixed printing of nil, removed printing of Suffix,
  136.  * added shortcut if no revisions are printed, disambiguated struct members.
  137.  *
  138.  * Revision 3.2  82/10/18  21:09:06  wft
  139.  * call to curdir replaced with getfullRCSname(),
  140.  * fixed call to getlogin(), cosmetic changes on output,
  141.  * changed conflicting long identifiers.
  142.  *
  143.  * Revision 3.1  82/10/13  16:07:56  wft
  144.  * fixed type of variables receiving from getc() (char -> int).
  145.  */
  146.  
  147.  
  148.  
  149. #include "rcsbase.h"
  150.  
  151. struct  lockers {                     /* lockers in locker option; stored   */
  152.      char const        * login;      /* lockerlist                */
  153.      struct     lockers * lockerlink;
  154.      }  ;
  155.  
  156. struct  stateattri {                  /* states in state option; stored in  */
  157.      char const        * status;     /* statelist                */
  158.      struct  stateattri * nextstate;
  159.      }  ;
  160.  
  161. struct  authors {                     /* login names in author option;      */
  162.      char const        * login;      /* stored in authorlist            */
  163.      struct     authors * nextauthor;
  164.      }  ;
  165.  
  166. struct Revpairs{                      /* revision or branch range in -r     */
  167.      int          numfld;     /* option; stored in revlist        */
  168.      char const        * strtrev;
  169.      char const        * endrev;
  170.      struct  Revpairs   * rnext;
  171.      } ;
  172.  
  173. struct Datepairs{                     /* date range in -d option; stored in */
  174.      struct Datepairs *dnext;
  175.      char               strtdate[datesize];   /* duelst and datelist      */
  176.      char               enddate[datesize];
  177.      char ne_date; /* datelist only; distinguishes < from <= */
  178.      };
  179.  
  180. static char extractdelta P((struct hshentry const*));
  181. static int checkrevpair P((char const*,char const*));
  182. static int extdate P((struct hshentry*));
  183. static struct hshentry const *readdeltalog P((void));
  184. static void cleanup P((void));
  185. static void exttree P((struct hshentry*));
  186. static void getauthor P((char*));
  187. static void getdatepair P((char*));
  188. static void getlocker P((char*));
  189. static void getnumericrev P((void));
  190. static void getrevpairs P((char*));
  191. static void getscript P((struct hshentry*));
  192. static void getstate P((char*));
  193. static void putabranch P((struct hshentry const*));
  194. static void putadelta P((struct hshentry const*,struct hshentry const*,int));
  195. static void putforest P((struct branchhead const*));
  196. static void putree P((struct hshentry const*));
  197. static void putrunk P((void));
  198. static void recentdate P((struct hshentry const*,struct Datepairs*));
  199. static void trunclocks P((void));
  200.  
  201. static char const *insDelFormat;
  202. static int branchflag;    /*set on -b */
  203. static int exitstatus;
  204. static int lockflag;
  205. static struct Datepairs *datelist, *duelst;
  206. static struct Revpairs *revlist, *Revlst;
  207. static struct authors *authorlist;
  208. static struct lockers *lockerlist;
  209. static struct stateattri *statelist;
  210.  
  211. static int onlyid;
  212. static char const *numrev2symbrev P((char const *numrev));
  213.  
  214.  
  215. mainProg(rlogId, "rlog", "$Id: rlog.c,v 5.16 1994/04/13 16:30:34 eggert Exp $")
  216. {
  217.     static char const cmdusage[] =
  218.         "\nrlog usage: rlog -{bhLNRtIZ} -ddates -l[lockers] -r[revs] -sstates -Vn -w[logins] -xsuff -zzone file ...";
  219.  
  220.     register FILE *out;
  221.     char *a, **newargv;
  222.     struct Datepairs *currdate;
  223.     char const *accessListString, *accessFormat;
  224.     char const *headFormat, *symbolFormat;
  225.     struct access const *curaccess;
  226.     struct assoc const *curassoc;
  227.     struct hshentry const *delta;
  228.     struct lock const *currlock;
  229.     int descflag, selectflag;
  230.     int onlylockflag;  /* print only files with locks */
  231.     int onlyRCSflag;  /* print only RCS pathname */
  232.     int onlylockers = 0;
  233.     int pre5;
  234.     int shownames;
  235.     int revno;
  236.  
  237.         descflag = selectflag = shownames = true;
  238.     onlylockflag = onlyRCSflag = false;
  239.     out = stdout;
  240.     suffixes = X_DEFAULT;
  241.  
  242.     argc = getRCSINIT(argc, argv, &newargv);
  243.     argv = newargv;
  244.     while (a = *++argv,  0<--argc && *a++=='-') {
  245.         switch (*a++) {
  246.  
  247.         case 'L':
  248.             onlylockflag = true;
  249.             break;
  250.  
  251.         case 'N':
  252.             shownames = false;
  253.             break;
  254.  
  255.         case 'R':
  256.             onlyRCSflag =true;
  257.             break;
  258.  
  259.         case 'I':
  260.             onlyid = (*a == 'I') ? 2 : 1;
  261.             break;
  262.  
  263.         case 'Z':
  264.             onlylockers = (*a == 'Z') ? 2 : 1;
  265.             break;
  266.  
  267.                 case 'l':
  268.                         lockflag = true;
  269.             getlocker(a);
  270.                         break;
  271.  
  272.                 case 'b':
  273.                         branchflag = true;
  274.                         break;
  275.  
  276.                 case 'r':
  277.             getrevpairs(a);
  278.                         break;
  279.  
  280.                 case 'd':
  281.             getdatepair(a);
  282.                         break;
  283.  
  284.                 case 's':
  285.             getstate(a);
  286.                         break;
  287.  
  288.                 case 'w':
  289.             getauthor(a);
  290.                         break;
  291.  
  292.                 case 'h':
  293.             descflag = false;
  294.                         break;
  295.  
  296.                 case 't':
  297.                         selectflag = false;
  298.                         break;
  299.  
  300.         case 'q':
  301.             /* This has no effect; it's here for consistency.  */
  302.             quietflag = true;
  303.             break;
  304.  
  305.         case 'x':
  306.             suffixes = a;
  307.             break;
  308.  
  309.         case 'z':
  310.             zone_set(a);
  311.             break;
  312.  
  313.         case 'T':
  314.             /* Ignore -T, so that RCSINIT can contain -T.  */
  315.             if (*a)
  316.                 goto unknown;
  317.             break;
  318.  
  319.         case 'V':
  320.             setRCSversion(*argv);
  321.             break;
  322.  
  323.                 default:
  324.         unknown:
  325.             error("unknown option: %s%s", *argv, cmdusage);
  326.  
  327.                 };
  328.         } /* end of option processing */
  329.  
  330.     if (! (descflag|selectflag)) {
  331.         warn("-t overrides -h.");
  332.         descflag = true;
  333.     }
  334.  
  335.     pre5 = RCSversion < VERSION(5);
  336.     if (pre5) {
  337.         accessListString = "\naccess list:   ";
  338.         accessFormat = "  %s";
  339.         headFormat = "RCS file:        %s;   Working file:    %s\nhead:           %s%s\nbranch:         %s%s\nlocks:         ";
  340.         insDelFormat = "  lines added/del: %ld/%ld";
  341.         symbolFormat = "  %s: %s;";
  342.     } else {
  343.         accessListString = "\naccess list:";
  344.         accessFormat = "\n\t%s";
  345.         headFormat = "RCS file: %s\nWorking file: %s\nhead:%s%s\nbranch:%s%s\nlocks:%s";
  346.         insDelFormat = "  lines: +%ld -%ld";
  347.         symbolFormat = "\n\t%s: %s";
  348.     }
  349.  
  350.     /* Now handle all pathnames.  */
  351.     if (nerror)
  352.       cleanup();
  353.     else if (argc < 1)
  354.       faterror("no input file%s", cmdusage);
  355.     else
  356.       for (;  0 < argc;  cleanup(), ++argv, --argc) {
  357.         ffree();
  358.  
  359.         if (pairnames(argc, argv, rcsreadopen, true, false)  <=  0)
  360.         continue;
  361.  
  362.         /*
  363.          * RCSname contains the name of the RCS file,
  364.          * and finptr the file descriptor;
  365.          * workname contains the name of the working file.
  366.              */
  367.  
  368.         /* Options 'Z' and 'ZZ' added to quickly get the relevant Data
  369.          * for locked File(s)/Revisions.
  370.          * Options 'I' and 'II' added to easily get Information about
  371.          * unlocked File(s) of the specified Revision(s). These Records
  372.          * could be used as an Entry of an Revision-List.
  373.          * The single-option Version prints Basenames, the double-option
  374.          * Version prints Filenames with the full path.
  375.          */
  376.         if (onlyid) {
  377.           if (Locks) {         /* Issue Warnings for locked Versions */
  378.         currlock = Locks;
  379.         aprintf (stderr, "Warning: there are Locks for File %s:\n",
  380.              onlyid == 1 ? RCSname : getfullRCSname());
  381.         while (currlock) {
  382.           aprintf (stderr, 
  383.                "\tFile: %s   Revision: %s [%s]  locked by: %s\n",
  384.                onlyid == 1 ? RCSname : getfullRCSname(), 
  385.                currlock->delta->num, 
  386.                numrev2symbrev (currlock->delta->num),
  387.                currlock->login);
  388.           currlock = currlock->nextlock;
  389.         }
  390.           }
  391.  
  392.           if (! Head) continue;
  393.           gettree ();
  394.           revno = 0;
  395.           getnumericrev ();
  396.           exttree (Head);
  397.           currdate = duelst;
  398.           while (currdate) {
  399.         VOID sprintf (currdate->strtdate,dateform,0,0,0,0,0,0);
  400.         recentdate (Head, currdate);
  401.         currdate = currdate->dnext;
  402.           }
  403.           revno = extdate (Head);
  404.           getdesc (false);
  405.           if (revno) {
  406.         while (! (delta = readdeltalog())->selector  ||  --revno) ;
  407.         if (delta->next && countnumflds(delta->num)==2) {
  408.           while (readdeltalog() != delta->next) ;
  409.         }
  410.         putrunk ();
  411.         putree (Head);
  412.           }
  413.           continue;
  414.         }
  415.  
  416.         if (onlylockers) {
  417.           currlock = Locks;
  418.           while (currlock) {
  419.         aprintf (out, "File: %s   Revision: %s [%s]  locked by: %s\n",
  420.              onlylockers == 1 ? RCSname : getfullRCSname(), 
  421.              currlock->delta->num, 
  422.              numrev2symbrev (currlock->delta->num),
  423.              currlock->login);
  424.         currlock = currlock->nextlock;
  425.           }
  426.           continue;
  427.         }
  428.  
  429.         /* Keep only those locks given by -l.  */
  430.         if (lockflag)
  431.         trunclocks();
  432.  
  433.             /* do nothing if -L is given and there are no locks*/
  434.         if (onlylockflag && !Locks)
  435.         continue;
  436.  
  437.         if ( onlyRCSflag ) {
  438.         aprintf(out, "%s\n", RCSname);
  439.         continue;
  440.         }
  441.  
  442.         /*
  443.         * Output the first character with putc, not printf.
  444.         * Otherwise, an SVR4 stdio bug buffers output inefficiently.
  445.         */
  446.         aputc_('\n', out)
  447.  
  448.         /*   print RCS pathname, working pathname and optional
  449.                  administrative information                         */
  450.             /* could use getfullRCSname() here, but that is very slow */
  451.         aprintf(out, headFormat, RCSname, workname,
  452.             Head ? " " : "",  Head ? Head->num : "",
  453.             Dbranch ? " " : "",  Dbranch ? Dbranch : "",
  454.             StrictLocks ? " strict" : ""
  455.         );
  456.             currlock = Locks;
  457.             while( currlock ) {
  458.         aprintf(out, symbolFormat, currlock->login,
  459.                                 currlock->delta->num);
  460.                 currlock = currlock->nextlock;
  461.             }
  462.             if (StrictLocks && pre5)
  463.                 aputs("  ;  strict" + (Locks?3:0), out);
  464.  
  465.         aputs(accessListString, out);      /*  print access list  */
  466.             curaccess = AccessList;
  467.             while(curaccess) {
  468.         aprintf(out, accessFormat, curaccess->login);
  469.                 curaccess = curaccess->nextaccess;
  470.             }
  471.  
  472.         if (shownames) {
  473.         aputs("\nsymbolic names:", out);   /*  print symbolic names   */
  474.         for (curassoc=Symbols; curassoc; curassoc=curassoc->nextassoc)
  475.             aprintf(out, symbolFormat, curassoc->symbol, curassoc->num);
  476.         }
  477.         if (pre5) {
  478.         aputs("\ncomment leader:  \"", out);
  479.         awrite(Comment.string, Comment.size, out);
  480.         afputc('\"', out);
  481.         }
  482.         if (!pre5  ||  Expand != KEYVAL_EXPAND)
  483.         aprintf(out, "\nkeyword substitution: %s",
  484.             expand_names[Expand]
  485.         );
  486.  
  487.             gettree();
  488.  
  489.         aprintf(out, "\ntotal revisions: %d", TotalDeltas);
  490.  
  491.         revno = 0;
  492.  
  493.         if (Head  &&  selectflag & descflag) {
  494.  
  495.         getnumericrev();    /* get numeric revision or branch names */
  496.  
  497.         exttree(Head);
  498.  
  499.         /*  get most recently date of the dates pointed by duelst  */
  500.         currdate = duelst;
  501.         while( currdate) {
  502.             VOID sprintf(currdate->strtdate,dateform,0,0,0,0,0,0);
  503.             recentdate(Head, currdate);
  504.             currdate = currdate->dnext;
  505.         }
  506.  
  507.         revno = extdate(Head);
  508.  
  509.         aprintf(out, ";\tselected revisions: %d", revno);
  510.         }
  511.  
  512.         afputc('\n',out);
  513.         if (descflag) {
  514.         aputs("description:\n", out);
  515.         getdesc(true);
  516.         }
  517.         if (revno) {
  518.         while (! (delta = readdeltalog())->selector  ||  --revno)
  519.             continue;
  520.         if (delta->next && countnumflds(delta->num)==2)
  521.             /* Read through delta->next to get its insertlns.  */
  522.             while (readdeltalog() != delta->next)
  523.             continue;
  524.         putrunk();
  525.         putree(Head);
  526.         }
  527.         aputs("=============================================================================\n",out);
  528.       }
  529.     Ofclose(out);
  530.     exitmain(exitstatus);
  531. }
  532.  
  533.     static void
  534. cleanup()
  535. {
  536.     if (nerror) exitstatus = EXIT_FAILURE;
  537.     Izclose(&finptr);
  538. }
  539.  
  540. #if RCS_lint
  541. #    define exiterr rlogExit
  542. #endif
  543.     void
  544. exiterr()
  545. {
  546.     _exit(EXIT_FAILURE);
  547. }
  548.  
  549.  
  550.  
  551.     static void
  552. putrunk()
  553. /*  function:  print revisions chosen, which are in trunk      */
  554.  
  555. {
  556.     register struct hshentry const *ptr;
  557.  
  558.     for (ptr = Head;  ptr;  ptr = ptr->next)
  559.         putadelta(ptr, ptr->next, true);
  560. }
  561.  
  562.  
  563.  
  564.     static void
  565. putree(root)
  566.     struct hshentry const *root;
  567. /*   function: print delta tree (not including trunk) in reverse
  568.                order on each branch                                        */
  569.  
  570. {
  571.     if (!root) return;
  572.  
  573.         putree(root->next);
  574.  
  575.         putforest(root->branches);
  576. }
  577.  
  578.  
  579.  
  580.  
  581.     static void
  582. putforest(branchroot)
  583.     struct branchhead const *branchroot;
  584. /*   function:  print branches that has the same direct ancestor    */
  585. {
  586.     if (!branchroot) return;
  587.  
  588.         putforest(branchroot->nextbranch);
  589.  
  590.         putabranch(branchroot->hsh);
  591.         putree(branchroot->hsh);
  592. }
  593.  
  594.  
  595.  
  596.  
  597.     static void
  598. putabranch(root)
  599.     struct hshentry const *root;
  600. /*   function  :  print one branch     */
  601.  
  602. {
  603.     if (!root) return;
  604.  
  605.         putabranch(root->next);
  606.  
  607.         putadelta(root, root, false);
  608. }
  609.  
  610.  
  611.  
  612.  
  613.  
  614.     static void
  615. putadelta(node,editscript,trunk)
  616.     register struct hshentry const *node, *editscript;
  617.     int trunk;
  618. /*  function: Print delta node if node->selector is set.        */
  619. /*      editscript indicates where the editscript is stored     */
  620. /*      trunk indicated whether this node is in trunk           */
  621. {
  622.     static char emptych[] = EMPTYLOG;
  623.  
  624.     register FILE *out;
  625.     char const *s;
  626.     size_t n;
  627.     struct branchhead const *newbranch;
  628.     struct buf branchnum;
  629.     char datebuf[datesize + zonelenmax];
  630.     int pre5 = RCSversion < VERSION(5);
  631.  
  632.     if (!node->selector)
  633.             return;
  634.  
  635.     out = stdout;
  636.  
  637.     if (onlyid) {
  638.       aprintf (out, 
  639.            "File: %s   Revision: %s [%s]  Date: %s   Author: %s  State: %s\n",
  640.            onlyid == 1 ? workname : getfullRCSname(),
  641.            node->num, numrev2symbrev (node->num),
  642.            date2str (node->date, datebuf), node->author, node->state);
  643.       return;
  644.     }
  645.  
  646.     aprintf(out,
  647.         "----------------------------\nrevision %s%s",
  648.         node->num,  pre5 ? "        " : ""
  649.     );
  650.         if ( node->lockedby )
  651.         aprintf(out, pre5+"\tlocked by: %s;", node->lockedby);
  652.  
  653.     aprintf(out, "\ndate: %s;  author: %s;  state: %s;",
  654.         date2str(node->date, datebuf),
  655.         node->author, node->state
  656.     );
  657.  
  658.         if ( editscript )
  659.            if(trunk)
  660.           aprintf(out, insDelFormat,
  661.                              editscript->deletelns, editscript->insertlns);
  662.            else
  663.           aprintf(out, insDelFormat,
  664.                              editscript->insertlns, editscript->deletelns);
  665.  
  666.         newbranch = node->branches;
  667.         if ( newbranch ) {
  668.        bufautobegin(&branchnum);
  669.        aputs("\nbranches:", out);
  670.            while( newbranch ) {
  671.         getbranchno(newbranch->hsh->num, &branchnum);
  672.         aprintf(out, "  %s;", branchnum.string);
  673.                 newbranch = newbranch->nextbranch;
  674.            }
  675.        bufautoend(&branchnum);
  676.         }
  677.  
  678.     afputc('\n', out);
  679.     s = node->log.string;
  680.     if (!(n = node->log.size)) {
  681.         s = emptych;
  682.         n = sizeof(emptych)-1;
  683.     }
  684.     awrite(s, n, out);
  685.     if (s[n-1] != '\n')
  686.         afputc('\n', out);
  687. }
  688.  
  689.  
  690.     static struct hshentry const *
  691. readdeltalog()
  692. /*  Function : get the log message and skip the text of a deltatext node.
  693.  *           Return the delta found.
  694.  *             Assumes the current lexeme is not yet in nexttok; does not
  695.  *             advance nexttok.
  696.  */
  697. {
  698.         register struct  hshentry  * Delta;
  699.     struct buf logbuf;
  700.     struct cbuf cb;
  701.  
  702.     if (eoflex())
  703.         fatserror("missing delta log");
  704.         nextlex();
  705.     if (!(Delta = getnum()))
  706.         fatserror("delta number corrupted");
  707.     getkeystring(Klog);
  708.     if (Delta->log.string)
  709.         fatserror("duplicate delta log");
  710.     bufautobegin(&logbuf);
  711.     cb = savestring(&logbuf);
  712.     Delta->log = bufremember(&logbuf, cb.size);
  713.  
  714.     ignorephrases(Ktext);
  715.     getkeystring(Ktext);
  716.         Delta->insertlns = Delta->deletelns = 0;
  717.         if ( Delta != Head)
  718.                 getscript(Delta);
  719.         else
  720.                 readstring();
  721.     return Delta;
  722. }
  723.  
  724.  
  725.     static void
  726. getscript(Delta)
  727. struct    hshentry   * Delta;
  728. /*   function:  read edit script of Delta and count how many lines added  */
  729. /*              and deleted in the script                                 */
  730.  
  731. {
  732.         int ed;   /*  editor command  */
  733.     declarecache;
  734.     register RILE *fin;
  735.         register  int   c;
  736.     register long i;
  737.     struct diffcmd dc;
  738.  
  739.     fin = finptr;
  740.     setupcache(fin);
  741.     initdiffcmd(&dc);
  742.     while (0  <=  (ed = getdiffcmd(fin,true,(FILE *)0,&dc)))
  743.         if (!ed)
  744.                  Delta->deletelns += dc.nlines;
  745.         else {
  746.                  /*  skip scripted lines  */
  747.          i = dc.nlines;
  748.          Delta->insertlns += i;
  749.          cache(fin);
  750.          do {
  751.              for (;;) {
  752.             cacheget_(c)
  753.             switch (c) {
  754.                 default:
  755.                 continue;
  756.                 case SDELIM:
  757.                 cacheget_(c)
  758.                 if (c == SDELIM)
  759.                     continue;
  760.                 if (--i)
  761.                     unexpected_EOF();
  762.                 nextc = c;
  763.                 uncache(fin);
  764.                 return;
  765.                 case '\n':
  766.                 break;
  767.             }
  768.             break;
  769.              }
  770.              ++rcsline;
  771.          } while (--i);
  772.          uncache(fin);
  773.             }
  774. }
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.     static void
  783. exttree(root)
  784. struct hshentry  *root;
  785. /*  function: select revisions , starting with root             */
  786.  
  787. {
  788.     struct branchhead const *newbranch;
  789.  
  790.     if (!root) return;
  791.  
  792.     root->selector = extractdelta(root);
  793.     root->log.string = 0;
  794.         exttree(root->next);
  795.  
  796.         newbranch = root->branches;
  797.         while( newbranch ) {
  798.             exttree(newbranch->hsh);
  799.             newbranch = newbranch->nextbranch;
  800.         }
  801. }
  802.  
  803.  
  804.  
  805.  
  806.     static void
  807. getlocker(argv)
  808. char    * argv;
  809. /*   function : get the login names of lockers from command line   */
  810. /*              and store in lockerlist.                           */
  811.  
  812. {
  813.         register char c;
  814.         struct   lockers   * newlocker;
  815.         argv--;
  816.     while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  817.         continue;
  818.         if (  c == '\0') {
  819.         lockerlist = 0;
  820.             return;
  821.         }
  822.  
  823.         while( c != '\0' ) {
  824.         newlocker = talloc(struct lockers);
  825.             newlocker->lockerlink = lockerlist;
  826.             newlocker->login = argv;
  827.             lockerlist = newlocker;
  828.         while ((c = *++argv) && c!=',' && c!=' ' && c!='\t' && c!='\n' && c!=';')
  829.         continue;
  830.             *argv = '\0';
  831.             if ( c == '\0' ) return;
  832.         while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  833.         continue;
  834.         }
  835. }
  836.  
  837.  
  838.  
  839.     static void
  840. getauthor(argv)
  841. char   *argv;
  842. /*   function:  get the author's name from command line   */
  843. /*              and store in authorlist                   */
  844.  
  845. {
  846.         register    c;
  847.         struct     authors  * newauthor;
  848.  
  849.         argv--;
  850.     while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  851.         continue;
  852.         if ( c == '\0' ) {
  853.         authorlist = talloc(struct authors);
  854.         authorlist->login = getusername(false);
  855.         authorlist->nextauthor = 0;
  856.             return;
  857.         }
  858.  
  859.         while( c != '\0' ) {
  860.         newauthor = talloc(struct authors);
  861.             newauthor->nextauthor = authorlist;
  862.             newauthor->login = argv;
  863.             authorlist = newauthor;
  864.         while ((c = *++argv) && c!=',' && c!=' ' && c!='\t' && c!='\n' && c!=';')
  865.         continue;
  866.             * argv = '\0';
  867.             if ( c == '\0') return;
  868.         while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  869.         continue;
  870.         }
  871. }
  872.  
  873.  
  874.  
  875.  
  876.     static void
  877. getstate(argv)
  878. char   * argv;
  879. /*   function :  get the states of revisions from command line  */
  880. /*               and store in statelist                         */
  881.  
  882. {
  883.         register  char  c;
  884.         struct    stateattri    *newstate;
  885.  
  886.         argv--;
  887.     while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  888.         continue;
  889.         if ( c == '\0'){
  890.         error("missing state attributes after -s options");
  891.             return;
  892.         }
  893.  
  894.         while( c != '\0' ) {
  895.         newstate = talloc(struct stateattri);
  896.             newstate->nextstate = statelist;
  897.             newstate->status = argv;
  898.             statelist = newstate;
  899.         while ((c = *++argv) && c!=',' && c!=' ' && c!='\t' && c!='\n' && c!=';')
  900.         continue;
  901.             *argv = '\0';
  902.             if ( c == '\0' ) return;
  903.         while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  904.         continue;
  905.         }
  906. }
  907.  
  908.  
  909.  
  910.     static void
  911. trunclocks()
  912. /*  Function:  Truncate the list of locks to those that are held by the  */
  913. /*             id's on lockerlist. Do not truncate if lockerlist empty.  */
  914.  
  915. {
  916.     struct lockers const *plocker;
  917.         struct lock *p, **pp;
  918.  
  919.     if (!lockerlist) return;
  920.  
  921.         /* shorten Locks to those contained in lockerlist */
  922.     for (pp = &Locks;  (p = *pp);  )
  923.         for (plocker = lockerlist;  ;  )
  924.         if (strcmp(plocker->login, p->login) == 0) {
  925.             pp = &p->nextlock;
  926.             break;
  927.         } else if (!(plocker = plocker->lockerlink)) {
  928.             *pp = p->nextlock;
  929.             break;
  930.         }
  931. }
  932.  
  933.  
  934.  
  935.     static void
  936. recentdate(root, pd)
  937.     struct hshentry const *root;
  938.     struct Datepairs *pd;
  939. /*  function:  Finds the delta that is closest to the cutoff date given by   */
  940. /*             pd among the revisions selected by exttree.                   */
  941. /*             Successively narrows down the interval given by pd,           */
  942. /*             and sets the strtdate of pd to the date of the selected delta */
  943. {
  944.     struct branchhead const *newbranch;
  945.  
  946.     if (!root) return;
  947.     if (root->selector) {
  948.              if ( cmpnum(root->date, pd->strtdate) >= 0 &&
  949.                   cmpnum(root->date, pd->enddate) <= 0)
  950.         VOID strcpy(pd->strtdate, root->date);
  951.         }
  952.  
  953.         recentdate(root->next, pd);
  954.         newbranch = root->branches;
  955.         while( newbranch) {
  956.            recentdate(newbranch->hsh, pd);
  957.            newbranch = newbranch->nextbranch;
  958.     }
  959. }
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.     static int
  967. extdate(root)
  968. struct  hshentry        * root;
  969. /*  function:  select revisions which are in the date range specified     */
  970. /*             in duelst  and datelist, start at root                     */
  971. /* Yield number of revisions selected, including those already selected.  */
  972. {
  973.     struct branchhead const *newbranch;
  974.     struct Datepairs const *pdate;
  975.     int revno, ne;
  976.  
  977.     if (!root)
  978.         return 0;
  979.  
  980.         if ( datelist || duelst) {
  981.             pdate = datelist;
  982.             while( pdate ) {
  983.         ne = pdate->ne_date;
  984.         if (!pdate->strtdate[0]||cmpnum(root->date,pdate->strtdate)>=ne)
  985.           if (!pdate->enddate[0]||cmpnum(pdate->enddate,root->date)>=ne)
  986.                         break;
  987.                 pdate = pdate->dnext;
  988.             }
  989.         if (!pdate) {
  990.                 pdate = duelst;
  991.         for (;;) {
  992.            if (!pdate) {
  993.             root->selector = false;
  994.             break;
  995.            }
  996.                    if ( cmpnum(root->date, pdate->strtdate) == 0)
  997.                       break;
  998.                    pdate = pdate->dnext;
  999.                 }
  1000.             }
  1001.         }
  1002.     revno = root->selector + extdate(root->next);
  1003.  
  1004.         newbranch = root->branches;
  1005.         while( newbranch ) {
  1006.        revno += extdate(newbranch->hsh);
  1007.            newbranch = newbranch->nextbranch;
  1008.         }
  1009.     return revno;
  1010. }
  1011.  
  1012.  
  1013.  
  1014.     static char
  1015. extractdelta(pdelta)
  1016.     struct hshentry const *pdelta;
  1017. /*  function:  compare information of pdelta to the authorlist, lockerlist,*/
  1018. /*             statelist, revlist and yield true if pdelta is selected.    */
  1019.  
  1020. {
  1021.     struct lock const *plock;
  1022.     struct stateattri const *pstate;
  1023.     struct authors const *pauthor;
  1024.     struct Revpairs const *prevision;
  1025.     int length;
  1026.  
  1027.     if ((pauthor = authorlist)) /* only certain authors wanted */
  1028.         while (strcmp(pauthor->login, pdelta->author) != 0)
  1029.         if (!(pauthor = pauthor->nextauthor))
  1030.             return false;
  1031.     if ((pstate = statelist)) /* only certain states wanted */
  1032.         while (strcmp(pstate->status, pdelta->state) != 0)
  1033.         if (!(pstate = pstate->nextstate))
  1034.             return false;
  1035.     if (lockflag) /* only locked revisions wanted */
  1036.         for (plock = Locks;  ;  plock = plock->nextlock)
  1037.         if (!plock)
  1038.             return false;
  1039.         else if (plock->delta == pdelta)
  1040.             break;
  1041.     if ((prevision = Revlst)) /* only certain revs or branches wanted */
  1042.         for (;;) {
  1043.                 length = prevision->numfld;
  1044.         if (
  1045.             countnumflds(pdelta->num) == length+(length&1) &&
  1046.             0 <= compartial(pdelta->num, prevision->strtrev, length) &&
  1047.             0 <= compartial(prevision->endrev, pdelta->num, length)
  1048.         )
  1049.              break;
  1050.         if (!(prevision = prevision->rnext))
  1051.             return false;
  1052.             }
  1053.     return true;
  1054. }
  1055.  
  1056.  
  1057.  
  1058.     static void
  1059. getdatepair(argv)
  1060.    char   * argv;
  1061. /*  function:  get time range from command line and store in datelist if    */
  1062. /*             a time range specified or in duelst if a time spot specified */
  1063.  
  1064. {
  1065.         register   char         c;
  1066.         struct     Datepairs    * nextdate;
  1067.     char const        * rawdate;
  1068.     int                     switchflag;
  1069.  
  1070.         argv--;
  1071.     while ((c = *++argv)==',' || c==' ' || c=='\t' || c=='\n' || c==';')
  1072.         continue;
  1073.         if ( c == '\0' ) {
  1074.         error("missing date/time after -d");
  1075.             return;
  1076.         }
  1077.  
  1078.         while( c != '\0' )  {
  1079.         switchflag = false;
  1080.         nextdate = talloc(struct Datepairs);
  1081.             if ( c == '<' ) {   /*   case: -d <date   */
  1082.                 c = *++argv;
  1083.         if (!(nextdate->ne_date = c!='='))
  1084.             c = *++argv;
  1085.                 (nextdate->strtdate)[0] = '\0';
  1086.         } else if (c == '>') { /* case: -d'>date' */
  1087.         c = *++argv;
  1088.         if (!(nextdate->ne_date = c!='='))
  1089.             c = *++argv;
  1090.         (nextdate->enddate)[0] = '\0';
  1091.         switchflag = true;
  1092.         } else {
  1093.                 rawdate = argv;
  1094.         while( c != '<' && c != '>' && c != ';' && c != '\0')
  1095.              c = *++argv;
  1096.                 *argv = '\0';
  1097.         if ( c == '>' ) switchflag=true;
  1098.         str2date(rawdate,
  1099.              switchflag ? nextdate->enddate : nextdate->strtdate);
  1100.         if ( c == ';' || c == '\0') {  /*  case: -d date  */
  1101.             VOID strcpy(nextdate->enddate,nextdate->strtdate);
  1102.                     nextdate->dnext = duelst;
  1103.                     duelst = nextdate;
  1104.             goto end;
  1105.         } else {
  1106.             /*   case:   -d date<  or -d  date>; see switchflag */
  1107.             int eq = argv[1]=='=';
  1108.             nextdate->ne_date = !eq;
  1109.             argv += eq;
  1110.             while ((c = *++argv) == ' ' || c=='\t' || c=='\n')
  1111.             continue;
  1112.             if ( c == ';' || c == '\0') {
  1113.             /* second date missing */
  1114.             if (switchflag)
  1115.                 *nextdate->strtdate= '\0';
  1116.             else
  1117.                 *nextdate->enddate= '\0';
  1118.             nextdate->dnext = datelist;
  1119.             datelist = nextdate;
  1120.             goto end;
  1121.             }
  1122.                 }
  1123.             }
  1124.             rawdate = argv;
  1125.         while( c != '>' && c != '<' && c != ';' && c != '\0')
  1126.          c = *++argv;
  1127.             *argv = '\0';
  1128.         str2date(rawdate,
  1129.              switchflag ? nextdate->strtdate : nextdate->enddate);
  1130.             nextdate->dnext = datelist;
  1131.         datelist = nextdate;
  1132.      end:
  1133.         if (RCSversion < VERSION(5))
  1134.         nextdate->ne_date = 0;
  1135.         if ( c == '\0')  return;
  1136.         while ((c = *++argv) == ';' || c == ' ' || c == '\t' || c =='\n')
  1137.         continue;
  1138.         }
  1139. }
  1140.  
  1141.  
  1142.  
  1143.     static void
  1144. getnumericrev()
  1145. /*  function:  get the numeric name of revisions which stored in revlist  */
  1146. /*             and then stored the numeric names in Revlst                */
  1147. /*             if branchflag, also add default branch                     */
  1148.  
  1149. {
  1150.         struct  Revpairs        * ptr, *pt;
  1151.     int n;
  1152.     struct buf s, e;
  1153.     char const *lrev;
  1154.     struct buf const *rstart, *rend;
  1155.  
  1156.     Revlst = 0;
  1157.         ptr = revlist;
  1158.     bufautobegin(&s);
  1159.     bufautobegin(&e);
  1160.         while( ptr ) {
  1161.         n = 0;
  1162.         rstart = &s;
  1163.         rend = &e;
  1164.  
  1165.         switch (ptr->numfld) {
  1166.  
  1167.           case 1: /* -rREV */
  1168.         if (expandsym(ptr->strtrev, &s)) {
  1169.             rend = &s;
  1170.             n = countnumflds(s.string);
  1171.             if (!n  &&  (lrev = tiprev())) {
  1172.             bufscpy(&s, lrev);
  1173.             n = countnumflds(lrev);
  1174.             }
  1175.                 }
  1176.         break;
  1177.  
  1178.           case 2: /* -rREV: */
  1179.         if (expandsym(ptr->strtrev, &s)) {
  1180.             bufscpy(&e, s.string);
  1181.             n = countnumflds(s.string);
  1182.             (n<2 ? e.string : strrchr(e.string,'.'))[0]  =  0;
  1183.                 }
  1184.         break;
  1185.  
  1186.           case 3: /* -r:REV */
  1187.         if (expandsym(ptr->endrev, &e)) {
  1188.             if ((n = countnumflds(e.string)) < 2)
  1189.             bufscpy(&s, ".0");
  1190.             else {
  1191.             bufscpy(&s, e.string);
  1192.             VOID strcpy(strrchr(s.string,'.'), ".0");
  1193.             }
  1194.                 }
  1195.         break;
  1196.  
  1197.           default: /* -rREV1:REV2 */
  1198.         if (
  1199.             expandsym(ptr->strtrev, &s)
  1200.             &&    expandsym(ptr->endrev, &e)
  1201.             &&    checkrevpair(s.string, e.string)
  1202.         ) {
  1203.             n = countnumflds(s.string);
  1204.             /* Swap if out of order.  */
  1205.             if (compartial(s.string,e.string,n) > 0) {
  1206.             rstart = &e;
  1207.             rend = &s;
  1208.             }
  1209.         }
  1210.         break;
  1211.         }
  1212.  
  1213.         if (n) {
  1214.         pt = ftalloc(struct Revpairs);
  1215.         pt->numfld = n;
  1216.         pt->strtrev = fstr_save(rstart->string);
  1217.         pt->endrev = fstr_save(rend->string);
  1218.                 pt->rnext = Revlst;
  1219.                 Revlst = pt;
  1220.         }
  1221.         ptr = ptr->rnext;
  1222.         }
  1223.         /* Now take care of branchflag */
  1224.     if (branchflag && (Dbranch||Head)) {
  1225.         pt = ftalloc(struct Revpairs);
  1226.         pt->strtrev = pt->endrev =
  1227.         Dbranch ? Dbranch : fstr_save(partialno(&s,Head->num,1));
  1228.         pt->rnext=Revlst; Revlst=pt;
  1229.         pt->numfld = countnumflds(pt->strtrev);
  1230.         }
  1231.     bufautoend(&s);
  1232.     bufautoend(&e);
  1233. }
  1234.  
  1235.  
  1236.  
  1237.     static int
  1238. checkrevpair(num1,num2)
  1239.     char const *num1, *num2;
  1240. /*  function:  check whether num1, num2 are legal pair,i.e.
  1241.     only the last field are different and have same number of
  1242.     fields( if length <= 2, may be different if first field)   */
  1243.  
  1244. {
  1245.     int length = countnumflds(num1);
  1246.  
  1247.     if (
  1248.             countnumflds(num2) != length
  1249.         ||    (2 < length  &&  compartial(num1, num2, length-1) != 0)
  1250.     ) {
  1251.         rcserror("invalid branch or revision pair %s : %s", num1, num2);
  1252.             return false;
  1253.         }
  1254.  
  1255.         return true;
  1256. }
  1257.  
  1258.  
  1259.  
  1260.     static void
  1261. getrevpairs(argv)
  1262. register     char    * argv;
  1263. /*  function:  get revision or branch range from command line, and   */
  1264. /*             store in revlist                                      */
  1265.  
  1266. {
  1267.         register    char    c;
  1268.         struct      Revpairs  * nextrevpair;
  1269.     int separator;
  1270.  
  1271.     c = *argv;
  1272.  
  1273.     /* Support old ambiguous '-' syntax; this will go away.  */
  1274.     if (strchr(argv,':'))
  1275.         separator = ':';
  1276.     else {
  1277.         if (strchr(argv,'-')  &&  VERSION(5) <= RCSversion)
  1278.         warn("`-' is obsolete in `-r%s'; use `:' instead", argv);
  1279.         separator = '-';
  1280.     }
  1281.  
  1282.     for (;;) {
  1283.         while (c==' ' || c=='\t' || c=='\n')
  1284.         c = *++argv;
  1285.         nextrevpair = talloc(struct Revpairs);
  1286.             nextrevpair->rnext = revlist;
  1287.             revlist = nextrevpair;
  1288.         nextrevpair->numfld = 1;
  1289.         nextrevpair->strtrev = argv;
  1290.         for (;;  c = *++argv) {
  1291.         switch (c) {
  1292.             default:
  1293.             continue;
  1294.             case '\0': case ' ': case '\t': case '\n':
  1295.             case ',': case ';':
  1296.             break;
  1297.             case ':': case '-':
  1298.             if (c == separator)
  1299.                 break;
  1300.             continue;
  1301.         }
  1302.         break;
  1303.         }
  1304.         *argv = '\0';
  1305.         while (c==' ' || c=='\t' || c=='\n')
  1306.         c = *++argv;
  1307.         if (c == separator) {
  1308.         while ((c = *++argv) == ' ' || c == '\t' || c =='\n')
  1309.             continue;
  1310.         nextrevpair->endrev = argv;
  1311.         for (;;  c = *++argv) {
  1312.             switch (c) {
  1313.             default:
  1314.                 continue;
  1315.             case '\0': case ' ': case '\t': case '\n':
  1316.             case ',': case ';':
  1317.                 break;
  1318.             case ':': case '-':
  1319.                 if (c == separator)
  1320.                 break;
  1321.                 continue;
  1322.             }
  1323.             break;
  1324.         }
  1325.         *argv = '\0';
  1326.         while (c==' ' || c=='\t' || c =='\n')
  1327.             c = *++argv;
  1328.         nextrevpair->numfld =
  1329.             !nextrevpair->endrev[0] ? 2 /* -rREV: */ :
  1330.             !nextrevpair->strtrev[0] ? 3 /* -r:REV */ :
  1331.             4 /* -rREV1:REV2 */;
  1332.             }
  1333.         if (!c)
  1334.         break;
  1335.         else if (c==',' || c==';')
  1336.         c = *++argv;
  1337.         else
  1338.         error("missing `,' near `%c%s'", c, argv+1);
  1339.     }
  1340. }
  1341.  
  1342. static char const *numrev2symbrev (numrev)
  1343. char const *numrev;
  1344. {
  1345.     struct assoc const *p;
  1346.  
  1347.     for (p = Symbols; p; p = p->nextassoc) {
  1348.         if (! strcmp (numrev, p->num)) return (p->symbol);
  1349.     }
  1350.  
  1351.     return ("");
  1352. }
  1353.