home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / rcssyn.c < prev    next >
C/C++ Source or Header  |  1992-01-24  |  16KB  |  656 lines

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