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

  1. /* RCS file syntactic analysis */
  2.  
  3. /******************************************************************************
  4.  *                       Syntax Analysis.
  5.  *                       Keyword table
  6.  *                       Testprogram: define SYNTEST
  7.  *                       Compatibility with Release 2: define COMPAT2=1
  8.  ******************************************************************************
  9.  */
  10.  
  11. /* Copyright 1982, 1988, 1989 Walter Tichy
  12.    Copyright 1990, 1991, 1992, 1993, 1994 Paul Eggert
  13.    Distributed under license by the Free Software Foundation, Inc.
  14.  
  15. This file is part of RCS.
  16.  
  17. RCS is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License as published by
  19. the Free Software Foundation; either version 2, or (at your option)
  20. any later version.
  21.  
  22. RCS is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with RCS; see the file COPYING.  If not, write to
  29. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  30.  
  31. Report problems and direct all questions to:
  32.  
  33.     rcs-bugs@cs.purdue.edu
  34.  
  35. */
  36.  
  37. /*
  38.  * $Log: rcssyn.c,v $
  39.  * Revision 5.13  1994/03/20 04:52:58  eggert
  40.  * Remove lint.
  41.  *
  42.  * Revision 5.12  1993/11/03 17:42:27  eggert
  43.  * Parse MKS RCS dates; ignore \r in diff control lines.
  44.  * Don't discard ignored phrases.  Improve quality of diagnostics.
  45.  *
  46.  * Revision 5.11  1992/07/28  16:12:44  eggert
  47.  * Avoid `unsigned'.  Statement macro names now end in _.
  48.  *
  49.  * Revision 5.10  1992/01/24  18:44:19  eggert
  50.  * Move put routines to rcsgen.c.
  51.  *
  52.  * Revision 5.9  1992/01/06  02:42:34  eggert
  53.  * ULONG_MAX/10 -> ULONG_MAX_OVER_10
  54.  * while (E) ; -> while (E) continue;
  55.  *
  56.  * Revision 5.8  1991/08/19  03:13:55  eggert
  57.  * Tune.
  58.  *
  59.  * Revision 5.7  1991/04/21  11:58:29  eggert
  60.  * Disambiguate names on shortname hosts.
  61.  * Fix errno bug.  Add MS-DOS support.
  62.  *
  63.  * Revision 5.6  1991/02/28  19:18:51  eggert
  64.  * Fix null termination bug in reporting keyword expansion.
  65.  *
  66.  * Revision 5.5  1991/02/25  07:12:44  eggert
  67.  * Check diff output more carefully; avoid overflow.
  68.  *
  69.  * Revision 5.4  1990/11/01  05:28:48  eggert
  70.  * When ignoring unknown phrases, copy them to the output RCS file.
  71.  * Permit arbitrary data in logs and comment leaders.
  72.  * Don't check for nontext on initial checkin.
  73.  *
  74.  * Revision 5.3  1990/09/20  07:58:32  eggert
  75.  * Remove the test for non-text bytes; it caused more pain than it cured.
  76.  *
  77.  * Revision 5.2  1990/09/04  08:02:30  eggert
  78.  * Parse RCS files with no revisions.
  79.  * Don't strip leading white space from diff commands.  Count RCS lines better.
  80.  *
  81.  * Revision 5.1  1990/08/29  07:14:06  eggert
  82.  * Add -kkvl.  Clean old log messages too.
  83.  *
  84.  * Revision 5.0  1990/08/22  08:13:44  eggert
  85.  * Try to parse future RCS formats without barfing.
  86.  * Add -k.  Don't require final newline.
  87.  * Remove compile-time limits; use malloc instead.
  88.  * Don't output branch keyword if there's no default branch,
  89.  * because RCS version 3 doesn't understand it.
  90.  * Tune.  Remove lint.
  91.  * Add support for ISO 8859.  Ansify and Posixate.
  92.  * Check that a newly checked-in file is acceptable as input to 'diff'.
  93.  * Check diff's output.
  94.  *
  95.  * Revision 4.6  89/05/01  15:13:32  narten
  96.  * changed copyright header to reflect current distribution rules
  97.  * 
  98.  * Revision 4.5  88/08/09  19:13:21  eggert
  99.  * Allow cc -R; remove lint.
  100.  * 
  101.  * Revision 4.4  87/12/18  11:46:16  narten
  102.  * more lint cleanups (Guy Harris)
  103.  * 
  104.  * Revision 4.3  87/10/18  10:39:36  narten
  105.  * Updating version numbers. Changes relative to 1.1 actually relative to
  106.  * 4.1
  107.  * 
  108.  * Revision 1.3  87/09/24  14:00:49  narten
  109.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  110.  * warnings)
  111.  * 
  112.  * Revision 1.2  87/03/27  14:22:40  jenkins
  113.  * Port to suns
  114.  * 
  115.  * Revision 4.1  83/03/28  11:38:49  wft
  116.  * Added parsing and printing of default branch.
  117.  * 
  118.  * Revision 3.6  83/01/15  17:46:50  wft
  119.  * Changed readdelta() to initialize selector and log-pointer.
  120.  * Changed puttree to check for selector==DELETE; putdtext() uses DELNUMFORM.
  121.  *
  122.  * Revision 3.5  82/12/08  21:58:58  wft
  123.  * renamed Commentleader to Commleader.
  124.  *
  125.  * Revision 3.4  82/12/04  13:24:40  wft
  126.  * Added routine gettree(), which updates keeplock after reading the
  127.  * delta tree.
  128.  *
  129.  * Revision 3.3  82/11/28  21:30:11  wft
  130.  * Reading and printing of Suffix removed; version COMPAT2 skips the
  131.  * Suffix for files of release 2 format. Fixed problems with printing nil.
  132.  *
  133.  * Revision 3.2  82/10/18  21:18:25  wft
  134.  * renamed putdeltatext to putdtext.
  135.  *
  136.  * Revision 3.1  82/10/11  19:45:11  wft
  137.  * made sure getc() returns into an integer.
  138.  */
  139.  
  140.  
  141.  
  142. /* version COMPAT2 reads files of the format of release 2 and 3, but
  143.  * generates files of release 3 format. Need not be defined if no
  144.  * old RCS files generated with release 2 exist.
  145.  */
  146.  
  147. #include "rcsbase.h"
  148.  
  149. libId(synId, "$Id: rcssyn.c,v 5.13 1994/03/20 04:52:58 eggert Exp $")
  150.  
  151. static char const *getkeyval P((char const*,enum tokens,int));
  152. static int getdelta P((void));
  153. static int strn2expmode P((char const*,size_t));
  154. static struct hshentry *getdnum P((void));
  155. static void badDiffOutput P((char const*)) exiting;
  156. static void diffLineNumberTooLarge P((char const*)) exiting;
  157. static void getsemi P((char const*));
  158.  
  159. /* keyword table */
  160.  
  161. char const
  162.     Kaccess[]   = "access",
  163.     Kauthor[]   = "author",
  164.     Kbranch[]   = "branch",
  165.     Kcomment[]  = "comment",
  166.     Kdate[]     = "date",
  167.     Kdesc[]     = "desc",
  168.     Kexpand[]   = "expand",
  169.     Khead[]     = "head",
  170.     Klocks[]    = "locks",
  171.     Klog[]      = "log",
  172.     Knext[]     = "next",
  173.     Kstate[]    = "state",
  174.     Kstrict[]   = "strict",
  175.     Ksymbols[]  = "symbols",
  176.     Ktext[]     = "text";
  177.  
  178. static char const
  179. #if COMPAT2
  180.     Ksuffix[]   = "suffix",
  181. #endif
  182.     K_branches[]= "branches";
  183.  
  184. static struct buf Commleader;
  185. struct cbuf Comment;
  186. struct cbuf Ignored;
  187. struct access   * AccessList;
  188. struct assoc    * Symbols;
  189. struct lock     * Locks;
  190. int          Expand;
  191. int               StrictLocks;
  192. struct hshentry * Head;
  193. char const      * Dbranch;
  194. int TotalDeltas;
  195.  
  196.  
  197.     static void
  198. getsemi(key)
  199.     char const *key;
  200. /* Get a semicolon to finish off a phrase started by KEY.  */
  201. {
  202.     if (!getlex(SEMI))
  203.         fatserror("missing ';' after '%s'", key);
  204. }
  205.  
  206.     static struct hshentry *
  207. getdnum()
  208. /* Get a delta number.  */
  209. {
  210.     register struct hshentry *delta = getnum();
  211.     if (delta && countnumflds(delta->num)&1)
  212.         fatserror("%s isn't a delta number", delta->num);
  213.     return delta;
  214. }
  215.  
  216.  
  217.     void
  218. getadmin()
  219. /* Read an <admin> and initialize the appropriate global variables.  */
  220. {
  221.     register char const *id;
  222.         struct access   * newaccess;
  223.         struct assoc    * newassoc;
  224.         struct lock     * newlock;
  225.         struct hshentry * delta;
  226.     struct access **LastAccess;
  227.     struct assoc **LastSymbol;
  228.     struct lock **LastLock;
  229.     struct buf b;
  230.     struct cbuf cb;
  231.  
  232.         TotalDeltas=0;
  233.  
  234.     getkey(Khead);
  235.     Head = getdnum();
  236.     getsemi(Khead);
  237.  
  238.     Dbranch = 0;
  239.     if (getkeyopt(Kbranch)) {
  240.         if ((delta = getnum()))
  241.             Dbranch = delta->num;
  242.         getsemi(Kbranch);
  243.         }
  244.  
  245.  
  246. #if COMPAT2
  247.         /* read suffix. Only in release 2 format */
  248.     if (getkeyopt(Ksuffix)) {
  249.                 if (nexttok==STRING) {
  250.             readstring(); nextlex(); /* Throw away the suffix.  */
  251.         } else if (nexttok==ID) {
  252.                         nextlex();
  253.                 }
  254.         getsemi(Ksuffix);
  255.         }
  256. #endif
  257.  
  258.     getkey(Kaccess);
  259.     LastAccess = &AccessList;
  260.     while ((id = getid())) {
  261.         newaccess = ftalloc(struct access);
  262.                 newaccess->login = id;
  263.         *LastAccess = newaccess;
  264.         LastAccess = &newaccess->nextaccess;
  265.         }
  266.     *LastAccess = 0;
  267.     getsemi(Kaccess);
  268.  
  269.     getkey(Ksymbols);
  270.     LastSymbol = &Symbols;
  271.         while ((id = getid())) {
  272.                 if (!getlex(COLON))
  273.             fatserror("missing ':' in symbolic name definition");
  274.                 if (!(delta=getnum())) {
  275.             fatserror("missing number in symbolic name definition");
  276.                 } else { /*add new pair to association list*/
  277.             newassoc = ftalloc(struct assoc);
  278.                         newassoc->symbol=id;
  279.             newassoc->num = delta->num;
  280.             *LastSymbol = newassoc;
  281.             LastSymbol = &newassoc->nextassoc;
  282.                 }
  283.         }
  284.     *LastSymbol = 0;
  285.     getsemi(Ksymbols);
  286.  
  287.     getkey(Klocks);
  288.     LastLock = &Locks;
  289.         while ((id = getid())) {
  290.                 if (!getlex(COLON))
  291.             fatserror("missing ':' in lock");
  292.         if (!(delta=getdnum())) {
  293.             fatserror("missing number in lock");
  294.                 } else { /*add new pair to lock list*/
  295.             newlock = ftalloc(struct lock);
  296.                         newlock->login=id;
  297.                         newlock->delta=delta;
  298.             *LastLock = newlock;
  299.             LastLock = &newlock->nextlock;
  300.                 }
  301.         }
  302.     *LastLock = 0;
  303.     getsemi(Klocks);
  304.  
  305.     if ((StrictLocks = getkeyopt(Kstrict)))
  306.         getsemi(Kstrict);
  307.  
  308.     clear_buf(&Comment);
  309.     if (getkeyopt(Kcomment)) {
  310.         if (nexttok==STRING) {
  311.             Comment = savestring(&Commleader);
  312.             nextlex();
  313.         }
  314.         getsemi(Kcomment);
  315.         }
  316.  
  317.     Expand = KEYVAL_EXPAND;
  318.     if (getkeyopt(Kexpand)) {
  319.         if (nexttok==STRING) {
  320.             bufautobegin(&b);
  321.             cb = savestring(&b);
  322.             if ((Expand = strn2expmode(cb.string,cb.size)) < 0)
  323.                 fatserror("unknown expand mode %.*s",
  324.                 (int)cb.size, cb.string
  325.                 );
  326.             bufautoend(&b);
  327.             nextlex();
  328.         }
  329.         getsemi(Kexpand);
  330.         }
  331.     Ignored = getphrases(Kdesc);
  332. }
  333.  
  334. char const *const expand_names[] = {
  335.     /* These must agree with *_EXPAND in rcsbase.h.  */
  336.     "kv","kvl","k","v","o",
  337.     0
  338. };
  339.  
  340.     int
  341. str2expmode(s)
  342.     char const *s;
  343. /* Yield expand mode corresponding to S, or -1 if bad.  */
  344. {
  345.     return strn2expmode(s, strlen(s));
  346. }
  347.  
  348.     static int
  349. strn2expmode(s, n)
  350.     char const *s;
  351.     size_t n;
  352. {
  353.     char const *const *p;
  354.  
  355.     for (p = expand_names;  *p;  ++p)
  356.         if (memcmp(*p,s,n) == 0  &&  !(*p)[n])
  357.             return p - expand_names;
  358.     return -1;
  359. }
  360.  
  361.  
  362.     void
  363. ignorephrases(key)
  364.     const char *key;
  365. /*
  366. * Ignore a series of phrases that do not start with KEY.
  367. * Stop when the next phrase starts with a token that is not an identifier,
  368. * or is KEY.
  369. */
  370. {
  371.     for (;;) {
  372.         nextlex();
  373.         if (nexttok != ID  ||  strcmp(NextString,key) == 0)
  374.             break;
  375.         warnignore();
  376.         hshenter=false;
  377.         for (;; nextlex()) {
  378.             switch (nexttok) {
  379.                 case SEMI: hshenter=true; break;
  380.                 case ID:
  381.                 case NUM: ffree1(NextString); continue;
  382.                 case STRING: readstring(); continue;
  383.                 default: continue;
  384.             }
  385.             break;
  386.         }
  387.     }
  388. }
  389.  
  390.  
  391.     static int
  392. getdelta()
  393. /* Function: reads a delta block.
  394.  * returns false if the current block does not start with a number.
  395.  */
  396. {
  397.         register struct hshentry * Delta, * num;
  398.     struct branchhead **LastBranch, *NewBranch;
  399.  
  400.     if (!(Delta = getdnum()))
  401.         return false;
  402.  
  403.         hshenter = false; /*Don't enter dates into hashtable*/
  404.     {
  405.       char const *date = getkeyval(Kdate, NUM, false);
  406.       /* Strip any leading "19" from dates generated by MKS RCS.  */
  407.       Delta->date = date + 2*(
  408.         date[0] == '1'  &&
  409.         date[1] == '9'  &&
  410.         isdigit(date[2])  &&
  411.         isdigit(date[3])  &&
  412.         date[4] == '.'
  413.       );
  414.     }
  415.         hshenter=true;    /*reset hshenter for revision numbers.*/
  416.  
  417.         Delta->author = getkeyval(Kauthor, ID, false);
  418.  
  419.         Delta->state = getkeyval(Kstate, ID, true);
  420.  
  421.     getkey(K_branches);
  422.     LastBranch = &Delta->branches;
  423.     while ((num = getdnum())) {
  424.         NewBranch = ftalloc(struct branchhead);
  425.                 NewBranch->hsh = num;
  426.         *LastBranch = NewBranch;
  427.         LastBranch = &NewBranch->nextbranch;
  428.         }
  429.     *LastBranch = 0;
  430.     getsemi(K_branches);
  431.  
  432.     getkey(Knext);
  433.     Delta->next = num = getdnum();
  434.     getsemi(Knext);
  435.     Delta->lockedby = 0;
  436.     Delta->log.string = 0;
  437.     Delta->selector = true;
  438.     Delta->ig = getphrases(Kdesc);
  439.         TotalDeltas++;
  440.         return (true);
  441. }
  442.  
  443.  
  444.     void
  445. gettree()
  446. /* Function: Reads in the delta tree with getdelta(), then
  447.  * updates the lockedby fields.
  448.  */
  449. {
  450.     struct lock const *currlock;
  451.  
  452.     while (getdelta())
  453.         continue;
  454.         currlock=Locks;
  455.         while (currlock) {
  456.                 currlock->delta->lockedby = currlock->login;
  457.                 currlock = currlock->nextlock;
  458.         }
  459. }
  460.  
  461.  
  462.     void
  463. getdesc(prdesc)
  464. int  prdesc;
  465. /* Function: read in descriptive text
  466.  * nexttok is not advanced afterwards.
  467.  * If prdesc is set, the text is printed to stdout.
  468.  */
  469. {
  470.  
  471.     getkeystring(Kdesc);
  472.         if (prdesc)
  473.                 printstring();  /*echo string*/
  474.         else    readstring();   /*skip string*/
  475. }
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.     static char const *
  483. getkeyval(keyword, token, optional)
  484.     char const *keyword;
  485.     enum tokens token;
  486.     int optional;
  487. /* reads a pair of the form
  488.  * <keyword> <token> ;
  489.  * where token is one of <id> or <num>. optional indicates whether
  490.  * <token> is optional. A pointer to
  491.  * the actual character string of <id> or <num> is returned.
  492.  */
  493. {
  494.     register char const *val = 0;
  495.  
  496.     getkey(keyword);
  497.         if (nexttok==token) {
  498.                 val = NextString;
  499.                 nextlex();
  500.         } else {
  501.         if (!optional)
  502.             fatserror("missing %s", keyword);
  503.         }
  504.     getsemi(keyword);
  505.         return(val);
  506. }
  507.  
  508.  
  509.     void
  510. unexpected_EOF()
  511. {
  512.     rcsfaterror("unexpected EOF in diff output");
  513. }
  514.  
  515.     void
  516. initdiffcmd(dc)
  517.     register struct diffcmd *dc;
  518. /* Initialize *dc suitably for getdiffcmd(). */
  519. {
  520.     dc->adprev = 0;
  521.     dc->dafter = 0;
  522. }
  523.  
  524.     static void
  525. badDiffOutput(buf)
  526.     char const *buf;
  527. {
  528.     rcsfaterror("bad diff output line: %s", buf);
  529. }
  530.  
  531.     static void
  532. diffLineNumberTooLarge(buf)
  533.     char const *buf;
  534. {
  535.     rcsfaterror("diff line number too large: %s", buf);
  536. }
  537.  
  538.     int
  539. getdiffcmd(finfile, delimiter, foutfile, dc)
  540.     RILE *finfile;
  541.     FILE *foutfile;
  542.     int delimiter;
  543.     struct diffcmd *dc;
  544. /* Get a editing command output by 'diff -n' from fin.
  545.  * The input is delimited by SDELIM if delimiter is set, EOF otherwise.
  546.  * Copy a clean version of the command to fout (if nonnull).
  547.  * Yield 0 for 'd', 1 for 'a', and -1 for EOF.
  548.  * Store the command's line number and length into dc->line1 and dc->nlines.
  549.  * Keep dc->adprev and dc->dafter up to date.
  550.  */
  551. {
  552.     register int c;
  553.     declarecache;
  554.     register FILE *fout;
  555.     register char *p;
  556.     register RILE *fin;
  557.     long line1, nlines, t;
  558.     char buf[BUFSIZ];
  559.  
  560.     fin = finfile;
  561.     fout = foutfile;
  562.     setupcache(fin); cache(fin);
  563.     cachegeteof_(c, { if (delimiter) unexpected_EOF(); return -1; } )
  564.     if (delimiter) {
  565.         if (c==SDELIM) {
  566.             cacheget_(c)
  567.             if (c==SDELIM) {
  568.                 buf[0] = c;
  569.                 buf[1] = 0;
  570.                 badDiffOutput(buf);
  571.             }
  572.             uncache(fin);
  573.             nextc = c;
  574.             if (fout)
  575.                 aprintf(fout, "%c%c", SDELIM, c);
  576.             return -1;
  577.         }
  578.     }
  579.     p = buf;
  580.     do {
  581.         if (buf+BUFSIZ-2 <= p) {
  582.             rcsfaterror("diff output command line too long");
  583.         }
  584.         *p++ = c;
  585.         cachegeteof_(c, unexpected_EOF();)
  586.     } while (c != '\n');
  587.     uncache(fin);
  588.     if (delimiter)
  589.         ++rcsline;
  590.     *p = '\0';
  591.     for (p = buf+1;  (c = *p++) == ' ';  )
  592.         continue;
  593.     line1 = 0;
  594.     while (isdigit(c)) {
  595.         if (
  596.             LONG_MAX/10 < line1  ||
  597.             (t = line1 * 10,   (line1 = t + (c - '0'))  <  t)
  598.         )
  599.             diffLineNumberTooLarge(buf);
  600.         c = *p++;
  601.     }
  602.     while (c == ' ')
  603.         c = *p++;
  604.     nlines = 0;
  605.     while (isdigit(c)) {
  606.         if (
  607.             LONG_MAX/10 < nlines  ||
  608.             (t = nlines * 10,   (nlines = t + (c - '0'))  <  t)
  609.         )
  610.             diffLineNumberTooLarge(buf);
  611.         c = *p++;
  612.     }
  613.     if (c == '\r')
  614.         c = *p++;
  615.     if (c || !nlines) {
  616.         badDiffOutput(buf);
  617.     }
  618.     if (line1+nlines < line1)
  619.         diffLineNumberTooLarge(buf);
  620.     switch (buf[0]) {
  621.         case 'a':
  622.         if (line1 < dc->adprev) {
  623.             rcsfaterror("backward insertion in diff output: %s", buf);
  624.         }
  625.         dc->adprev = line1 + 1;
  626.         break;
  627.         case 'd':
  628.         if (line1 < dc->adprev  ||  line1 < dc->dafter) {
  629.             rcsfaterror("backward deletion in diff output: %s", buf);
  630.         }
  631.         dc->adprev = line1;
  632.         dc->dafter = line1 + nlines;
  633.         break;
  634.         default:
  635.         badDiffOutput(buf);
  636.     }
  637.     if (fout) {
  638.         aprintf(fout, "%s\n", buf);
  639.     }
  640.     dc->line1 = line1;
  641.     dc->nlines = nlines;
  642.     return buf[0] == 'a';
  643. }
  644.  
  645.  
  646.  
  647. #ifdef SYNTEST
  648.  
  649. /* Input an RCS file and print its internal data structures.  */
  650.  
  651. char const cmdid[] = "syntest";
  652.  
  653.     int
  654. main(argc,argv)
  655. int argc; char * argv[];
  656. {
  657.  
  658.         if (argc<2) {
  659.         aputs("No input file\n",stderr);
  660.         exitmain(EXIT_FAILURE);
  661.         }
  662.     if (!(finptr = Iopen(argv[1], FOPEN_R, (struct stat*)0))) {
  663.         faterror("can't open input file %s", argv[1]);
  664.         }
  665.         Lexinit();
  666.         getadmin();
  667.     fdlock = STDOUT_FILENO;
  668.     putadmin();
  669.  
  670.         gettree();
  671.  
  672.         getdesc(true);
  673.  
  674.     nextlex();
  675.  
  676.     if (!eoflex()) {
  677.         fatserror("expecting EOF");
  678.         }
  679.     exitmain(EXIT_SUCCESS);
  680. }
  681.  
  682. void exiterr() { _exit(EXIT_FAILURE); }
  683.  
  684. #endif
  685.