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

  1. /* Extract RCS keyword string values from working 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: rcskeep.c,v $
  31.  * Revision 5.8  1994/03/17 14:05:48  eggert
  32.  * Remove lint.
  33.  *
  34.  * Revision 5.7  1993/11/09 17:40:15  eggert
  35.  * Use simpler timezone parsing strategy now that we're using ISO 8601 format.
  36.  *
  37.  * Revision 5.6  1993/11/03 17:42:27  eggert
  38.  * Scan for Name keyword.  Improve quality of diagnostics.
  39.  *
  40.  * Revision 5.5  1992/07/28  16:12:44  eggert
  41.  * Statement macro names now end in _.
  42.  *
  43.  * Revision 5.4  1991/08/19  03:13:55  eggert
  44.  * Tune.
  45.  *
  46.  * Revision 5.3  1991/04/21  11:58:25  eggert
  47.  * Shorten names to keep them distinct on shortname hosts.
  48.  *
  49.  * Revision 5.2  1990/10/04  06:30:20  eggert
  50.  * Parse time zone offsets; future RCS versions may output them.
  51.  *
  52.  * Revision 5.1  1990/09/20  02:38:56  eggert
  53.  * ci -k now checks dates more thoroughly.
  54.  *
  55.  * Revision 5.0  1990/08/22  08:12:53  eggert
  56.  * Retrieve old log message if there is one.
  57.  * Don't require final newline.
  58.  * Remove compile-time limits; use malloc instead.  Tune.
  59.  * Permit dates past 1999/12/31.  Ansify and Posixate.
  60.  *
  61.  * Revision 4.6  89/05/01  15:12:56  narten
  62.  * changed copyright header to reflect current distribution rules
  63.  * 
  64.  * Revision 4.5  88/08/09  19:13:03  eggert
  65.  * Remove lint and speed up by making FILE *fp local, not global.
  66.  * 
  67.  * Revision 4.4  87/12/18  11:44:21  narten
  68.  * more lint cleanups (Guy Harris)
  69.  * 
  70.  * Revision 4.3  87/10/18  10:35:50  narten
  71.  * Updating version numbers. Changes relative to 1.1 actually relative
  72.  * to 4.1
  73.  * 
  74.  * Revision 1.3  87/09/24  14:00:00  narten
  75.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  76.  * warnings)
  77.  * 
  78.  * Revision 1.2  87/03/27  14:22:29  jenkins
  79.  * Port to suns
  80.  * 
  81.  * Revision 4.1  83/05/10  16:26:44  wft
  82.  * Added new markers Id and RCSfile; extraction added.
  83.  * Marker matching with trymatch().
  84.  * 
  85.  * Revision 3.2  82/12/24  12:08:26  wft
  86.  * added missing #endif.
  87.  *
  88.  * Revision 3.1  82/12/04  13:22:41  wft
  89.  * Initial revision.
  90.  *
  91.  */
  92.  
  93. #include  "rcsbase.h"
  94.  
  95. libId(keepId, "$Id: rcskeep.c,v 5.8 1994/03/17 14:05:48 eggert Exp $")
  96.  
  97. static int badly_terminated P((void));
  98. static int checknum P((char const*));
  99. static int get0val P((int,RILE*,struct buf*,int));
  100. static int getval P((RILE*,struct buf*,int));
  101. static int keepdate P((RILE*));
  102. static int keepid P((int,RILE*,struct buf*));
  103. static int keeprev P((RILE*));
  104.  
  105. int prevkeys;
  106. struct buf prevauthor, prevdate, prevname, prevrev, prevstate;
  107.  
  108.     int
  109. getoldkeys(fp)
  110.     register RILE *fp;
  111. /* Function: Tries to read keyword values for author, date,
  112.  * revision number, and state out of the file fp.
  113.  * If fp is null, workname is opened and closed instead of using fp.
  114.  * The results are placed into
  115.  * prevauthor, prevdate, prevname, prevrev, prevstate.
  116.  * Aborts immediately if it finds an error and returns false.
  117.  * If it returns true, it doesn't mean that any of the
  118.  * values were found; instead, check to see whether the corresponding arrays
  119.  * contain the empty string.
  120.  */
  121. {
  122.     register int c;
  123.     char keyword[keylength+1];
  124.     register char * tp;
  125.     int needs_closing;
  126.     int prevname_found;
  127.  
  128.     if (prevkeys)
  129.     return true;
  130.  
  131.     needs_closing = false;
  132.     if (!fp) {
  133.     if (!(fp = Iopen(workname, FOPEN_R_WORK, (struct stat*)0))) {
  134.         eerror(workname);
  135.         return false;
  136.     }
  137.     needs_closing = true;
  138.     }
  139.  
  140.     /* initialize to empty */
  141.     bufscpy(&prevauthor, "");
  142.     bufscpy(&prevdate, "");
  143.     bufscpy(&prevname, "");  prevname_found = 0;
  144.     bufscpy(&prevrev, "");
  145.     bufscpy(&prevstate, "");
  146.  
  147.     c = '\0'; /* anything but KDELIM */
  148.     for (;;) {
  149.         if ( c==KDELIM) {
  150.         do {
  151.         /* try to get keyword */
  152.         tp = keyword;
  153.         for (;;) {
  154.             Igeteof_(fp, c, goto ok;)
  155.             switch (c) {
  156.             default:
  157.                 if (keyword+keylength <= tp)
  158.                 break;
  159.                 *tp++ = c;
  160.                 continue;
  161.  
  162.             case '\n': case KDELIM: case VDELIM:
  163.                 break;
  164.             }
  165.             break;
  166.         }
  167.         } while (c==KDELIM);
  168.             if (c!=VDELIM) continue;
  169.         *tp = c;
  170.         Igeteof_(fp, c, break;)
  171.         switch (c) {
  172.         case ' ': case '\t': break;
  173.         default: continue;
  174.         }
  175.  
  176.         switch (trymatch(keyword)) {
  177.             case Author:
  178.         if (!keepid(0, fp, &prevauthor))
  179.             return false;
  180.         c = 0;
  181.                 break;
  182.             case Date:
  183.         if (!(c = keepdate(fp)))
  184.             return false;
  185.                 break;
  186.             case Header:
  187.             case Id:
  188.         if (!(
  189.               getval(fp, (struct buf*)0, false) &&
  190.               keeprev(fp) &&
  191.               (c = keepdate(fp)) &&
  192.               keepid(c, fp, &prevauthor) &&
  193.               keepid(0, fp, &prevstate)
  194.         ))
  195.             return false;
  196.         /* Skip either ``who'' (new form) or ``Locker: who'' (old).  */
  197.         if (getval(fp, (struct buf*)0, true) &&
  198.             getval(fp, (struct buf*)0, true))
  199.             c = 0;
  200.         else if (nerror)
  201.             return false;
  202.         else
  203.             c = KDELIM;
  204.         break;
  205.             case Locker:
  206.         (void) getval(fp, (struct buf*)0, false);
  207.         c = 0;
  208.         break;
  209.             case Log:
  210.             case RCSfile:
  211.             case Source:
  212.         if (!getval(fp, (struct buf*)0, false))
  213.             return false;
  214.         c = 0;
  215.                 break;
  216.         case Name:
  217.         if (!getval(fp, &prevname, false))
  218.             return false;
  219.         if (*prevname.string)
  220.             checkssym(prevname.string);
  221.         prevname_found = 1;
  222.         c = 0;
  223.         break;
  224.             case Revision:
  225.         if (!keeprev(fp))
  226.             return false;
  227.         c = 0;
  228.                 break;
  229.             case State:
  230.         if (!keepid(0, fp, &prevstate))
  231.             return false;
  232.         c = 0;
  233.                 break;
  234.             default:
  235.                continue;
  236.             }
  237.         if (!c)
  238.         Igeteof_(fp, c, c=0;)
  239.         if (c != KDELIM) {
  240.         workerror("closing %c missing on keyword", KDELIM);
  241.         return false;
  242.         }
  243.         if (prevname_found &&
  244.         *prevauthor.string && *prevdate.string &&
  245.         *prevrev.string && *prevstate.string
  246.         )
  247.                 break;
  248.         }
  249.     Igeteof_(fp, c, break;)
  250.     }
  251.  
  252.  ok:
  253.     if (needs_closing)
  254.     Ifclose(fp);
  255.     else
  256.     Irewind(fp);
  257.     prevkeys = true;
  258.     return true;
  259. }
  260.  
  261.     static int
  262. badly_terminated()
  263. {
  264.     workerror("badly terminated keyword value");
  265.     return false;
  266. }
  267.  
  268.     static int
  269. getval(fp, target, optional)
  270.     register RILE *fp;
  271.     struct buf *target;
  272.     int optional;
  273. /* Reads a keyword value from FP into TARGET.
  274.  * Returns true if one is found, false otherwise.
  275.  * Does not modify target if it is 0.
  276.  * Do not report an error if OPTIONAL is set and KDELIM is found instead.
  277.  */
  278. {
  279.     int c;
  280.     Igeteof_(fp, c, return badly_terminated();)
  281.     return get0val(c, fp, target, optional);
  282. }
  283.  
  284.     static int
  285. get0val(c, fp, target, optional)
  286.     register int c;
  287.     register RILE *fp;
  288.     struct buf *target;
  289.     int optional;
  290. /* Reads a keyword value from C+FP into TARGET, perhaps OPTIONALly.
  291.  * Same as getval, except C is the lookahead character.
  292.  */
  293. {   register char * tp;
  294.     char const *tlim;
  295.     register int got1;
  296.  
  297.     if (target) {
  298.     bufalloc(target, 1);
  299.     tp = target->string;
  300.     tlim = tp + target->size;
  301.     } else
  302.     tlim = tp = 0;
  303.     got1 = false;
  304.     for (;;) {
  305.     switch (c) {
  306.         default:
  307.         got1 = true;
  308.         if (tp) {
  309.             *tp++ = c;
  310.             if (tlim <= tp)
  311.             tp = bufenlarge(target, &tlim);
  312.         }
  313.         break;
  314.  
  315.         case ' ':
  316.         case '\t':
  317.         if (tp) {
  318.             *tp = 0;
  319. #            ifdef KEEPTEST
  320.             VOID printf("getval: %s\n", target);
  321. #            endif
  322.         }
  323.         return got1;
  324.  
  325.         case KDELIM:
  326.         if (!got1 && optional)
  327.             return false;
  328.         /* fall into */
  329.         case '\n':
  330.         case 0:
  331.         return badly_terminated();
  332.     }
  333.     Igeteof_(fp, c, return badly_terminated();)
  334.     }
  335. }
  336.  
  337.  
  338.     static int
  339. keepdate(fp)
  340.     RILE *fp;
  341. /* Function: reads a date prevdate; checks format
  342.  * Return 0 on error, lookahead character otherwise.
  343.  */
  344. {
  345.     struct buf prevday, prevtime;
  346.     register int c;
  347.  
  348.     c = 0;
  349.     bufautobegin(&prevday);
  350.     if (getval(fp,&prevday,false)) {
  351.     bufautobegin(&prevtime);
  352.     if (getval(fp,&prevtime,false)) {
  353.         Igeteof_(fp, c, c=0;)
  354.         if (c) {
  355.         register char const *d = prevday.string, *t = prevtime.string;
  356.         bufalloc(&prevdate, strlen(d) + strlen(t) + 9);
  357.         VOID sprintf(prevdate.string, "%s%s %s%s",
  358.             /* Parse dates put out by old versions of RCS.  */
  359.               isdigit(d[0]) && isdigit(d[1]) && !isdigit(d[2])
  360.             ? "19" : "",
  361.             d, t,
  362.             strchr(t,'-') || strchr(t,'+')  ?  ""  :  "+0000"
  363.         );
  364.         }
  365.     }
  366.     bufautoend(&prevtime);
  367.     }
  368.     bufautoend(&prevday);
  369.     return c;
  370. }
  371.  
  372.     static int
  373. keepid(c, fp, b)
  374.     int c;
  375.     RILE *fp;
  376.     struct buf *b;
  377. /* Get previous identifier from C+FP into B.  */
  378. {
  379.     if (!c)
  380.         Igeteof_(fp, c, return false;)
  381.     if (!get0val(c, fp, b, false))
  382.         return false;
  383.     checksid(b->string);
  384.     return !nerror;
  385. }
  386.  
  387.     static int
  388. keeprev(fp)
  389.     RILE *fp;
  390. /* Get previous revision from FP into prevrev.  */
  391. {
  392.     return getval(fp,&prevrev,false) && checknum(prevrev.string);
  393. }
  394.  
  395.  
  396.     static int
  397. checknum(s)
  398.     char const *s;
  399. {
  400.     register char const *sp;
  401.     register int dotcount = 0;
  402.     for (sp=s; ; sp++) {
  403.     switch (*sp) {
  404.         case 0:
  405.         if (dotcount & 1)
  406.             return true;
  407.         else
  408.             break;
  409.         
  410.         case '.':
  411.         dotcount++;
  412.         continue;
  413.         
  414.         default:
  415.         if (isdigit(*sp))
  416.             continue;
  417.         break;
  418.     }
  419.     break;
  420.     }
  421.     workerror("%s is not a revision number", s);
  422.     return false;
  423. }
  424.  
  425.  
  426.  
  427. #ifdef KEEPTEST
  428.  
  429. /* Print the keyword values found.  */
  430.  
  431. char const cmdid[] ="keeptest";
  432.  
  433.     int
  434. main(argc, argv)
  435. int  argc; char  *argv[];
  436. {
  437.         while (*(++argv)) {
  438.         workname = *argv;
  439.         getoldkeys((RILE*)0);
  440.                 VOID printf("%s:  revision: %s, date: %s, author: %s, name: %s, state: %s\n",
  441.                 *argv, prevrev.string, prevdate.string, prevauthor.string, prevname.string, prevstate.string);
  442.     }
  443.     exitmain(EXIT_SUCCESS);
  444. }
  445. #endif
  446.