home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OS9000 / APPS / rcs.lzh / rcs1 / rcskeep.c < prev    next >
Text File  |  1996-04-20  |  9KB  |  316 lines

  1. /*
  2.  *                     RCS keyword extraction
  3.  */
  4. #ifndef lint
  5. static char rcsid[]= "$Id: rcskeep.c_v 1.1 93/04/02 01:34:46 hiro Exp $ Purdue CS";
  6. #endif
  7. /*****************************************************************************
  8.  *                       main routine: getoldkeys()
  9.  *                       Testprogram: define KEEPTEST
  10.  *****************************************************************************
  11.  */
  12.  
  13. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  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 1, 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.  
  40. /* $Log:    rcskeep.c_v $
  41.  * Revision 1.1  93/04/02  01:34:46  hiro
  42.  * Initial revision
  43.  * 
  44.  * Revision 1.1  90/07/19  15:21:50  momo
  45.  * Initial revision
  46.  * 
  47.  * Revision 4.6  89/05/01  15:12:56  narten
  48.  * changed copyright header to reflect current distribution rules
  49.  * 
  50.  * Revision 4.5  88/11/08  12:01:05  narten
  51.  * changes from  eggert@sm.unisys.com (Paul Eggert)
  52.  * 
  53.  * Revision 4.5  88/08/09  19:13:03  eggert
  54.  * Remove lint and speed up by making FILE *fp local, not global.
  55.  * 
  56.  * Revision 4.4  87/12/18  11:44:21  narten
  57.  * more lint cleanups (Guy Harris)
  58.  * 
  59.  * Revision 4.3  87/10/18  10:35:50  narten
  60.  * Updating version numbers. Changes relative to 1.1 actually relative
  61.  * to 4.1
  62.  * 
  63.  * Revision 1.3  87/09/24  14:00:00  narten
  64.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  65.  * warnings)
  66.  * 
  67.  * Revision 1.2  87/03/27  14:22:29  jenkins
  68.  * Port to suns
  69.  * 
  70.  * Revision 1.1  84/01/23  14:50:30  kcs
  71.  * Initial revision
  72.  * 
  73.  * Revision 4.1  83/05/10  16:26:44  wft
  74.  * Added new markers Id and RCSfile; extraction added.
  75.  * Marker matching with trymatch().
  76.  * 
  77.  * Revision 3.2  82/12/24  12:08:26  wft
  78.  * added missing #endif.
  79.  *
  80.  * Revision 3.1  82/12/04  13:22:41  wft
  81.  * Initial revision.
  82.  *
  83.  */
  84.  
  85. /*
  86. #define KEEPTEST
  87. /* Testprogram; prints out the keyword values found. */
  88.  
  89. #include  "rcsbase.h"
  90. extern char * checkid();
  91. extern FILE * fopen();
  92. static int getval();
  93. extern enum markers trymatch();
  94.  
  95. #define IDLENGTH 30
  96. char prevauthor[IDLENGTH];
  97. char prevdate[datelength];
  98. char prevRCS[NCPFN];
  99. char prevrev[revlength];
  100. char prevsource[NCPPN];
  101. char prevstate [IDLENGTH];
  102. char prevlocker[IDLENGTH];
  103. char dummy[IDLENGTH];
  104.  
  105. getoldkeys(fname)
  106. char * fname;
  107. /* Function: Tries to read keyword values for author, date,
  108.  * revision number, RCS file, (both with and without path),
  109.  * state, and workfilename out of the file fname.
  110.  * The results are placed into
  111.  * prevauthor, prevdate, prevRCS, prevrev, prevsource, prevstate.
  112.  * Aborts immediately if it finds an error and returns false.
  113.  * If it returns true, it doesn't mean that any of the
  114.  * values were found; instead, check to see whether the corresponding arrays
  115.  * contain the empty string.
  116.  */
  117. {
  118.     register FILE *fp;
  119.     register int c;
  120.     char keyword[keylength+2];
  121.     register char * tp;
  122.     enum markers mresult;
  123.  
  124.     /* initialize to empty */
  125.     prevauthor[0]=prevsource[0]=prevstate[0]=prevdate[0]=prevrev[0]= '\0';
  126.  
  127.     if ( (fp = fopen(fname, "r") ) == NULL ) {
  128.        error("Can't open %s\n", fname);
  129.        return false;
  130.     }
  131.     while( (c=getc(fp)) != EOF) {
  132.         if ( c==KDELIM) {
  133.             /* try to get keyword */
  134.             tp = keyword;
  135.         while( (c=getc(fp))!=EOF && (tp< keyword+keylength) && (c!='\n')
  136.            && (c!=KDELIM) && (c!=VDELIM))
  137.           *tp++ = c;
  138.  
  139.             if (c==KDELIM) {VOID ungetc(c,fp);continue;}
  140.             if (c!=VDELIM) continue;
  141.         *tp++ = c;
  142.             *tp='\0';
  143.             while ((c=getc(fp))==' '||c=='\t'); /* skip blanks */
  144.             VOID ungetc(c,fp); /* needed for getval */
  145.  
  146.         switch (mresult=trymatch(keyword,true)) {
  147.             case Author:
  148.         if (getval(fp,prevauthor,IDLENGTH,true))
  149.                     if (!checkid(prevauthor, '\0')) goto errexit;
  150.                 break;
  151.             case Date:
  152.         if (!getprevdate(fp,true)) goto errexit;
  153.                 break;
  154.             case Header:
  155.             case Id:
  156.         if (mresult==Header) {
  157.             if (!getval(fp,prevsource,NCPPN,true)) break; /*unexpanded*/
  158.         } else {
  159.             if (!getval(fp,prevRCS,NCPFN,true))    break; /*unexpanded*/
  160.         }
  161.         if (!getval(fp,prevrev,revlength,false)) goto errexit;
  162.         if (!checknum(prevrev,-1)) {
  163.             error("Bad revision number");
  164.             goto errexit;
  165.         }
  166.         if (!getprevdate(fp,false)) goto errexit;
  167.         if (!getval(fp,prevauthor,IDLENGTH,false)) goto errexit;
  168.         if (!checkid(prevauthor, '\0')) goto errexit;
  169.         if (!getval(fp,prevstate,IDLENGTH,false)) goto errexit;
  170.         if (!checkid(prevstate, '\0')) goto errexit;
  171.         VOID getval(fp, dummy, IDLENGTH, true);    /* optional locker*/
  172.         VOID getval(fp, prevlocker,IDLENGTH,true); /* optional locker*/
  173.                 break;
  174.             case Locker:
  175.                 VOID getval(fp,prevlocker,IDLENGTH,true);
  176.         if (!checkid(prevlocker, '\0')) goto errexit;
  177.                 break;
  178.             case Log:
  179.         VOID getval(fp,prevRCS,NCPPN,true);
  180.                 break;
  181.             case RCSfile:
  182.                 VOID getval(fp,prevRCS,NCPFN,true);
  183.                 break;
  184.             case Revision:
  185.                 if (getval(fp,prevrev,revlength,true))
  186.                     if (!checknum(prevrev,-1)) {
  187.                         error("Bad revision number");
  188.                         goto errexit;
  189.                     }
  190.                 break;
  191.             case Source:
  192.                 VOID getval(fp,prevsource,NCPPN,true);
  193.                 break;
  194.             case State:
  195.                 if (getval(fp,prevstate,IDLENGTH,true))
  196.                     if (!checkid(prevstate, '\0')) goto errexit;
  197.                 break;
  198.             default:
  199.                continue;
  200.             }
  201.             if (getc(fp)!=KDELIM)
  202.                 warn("Closing %c missing on keyword",KDELIM);
  203.             if (prevauthor[0]!='\0'&&prevrev[0]!='\0'&&prevstate[0]!='\0'&&
  204.                 prevdate[0]!='\0' &&
  205.          ((prevsource[0]!='\0')||(prevRCS[0]!='\0'))){
  206.                 /* done; prevlocker is irrelevant */
  207.                 break;
  208.            }
  209.         }
  210.     }
  211.     VOID fclose(fp);
  212.     return true;
  213.  
  214. errexit:
  215.     prevauthor[0]=prevsource[0]=prevstate[0]=prevdate[0]=prevrev[0]= '\0';
  216.     VOID fclose(fp); return false;
  217. }
  218.  
  219.  
  220. static int getval(fp,target,maxchars,optional)
  221. register FILE *fp;
  222. char * target; int maxchars, optional;
  223. /* Function: Places a keyword value into target, but not more
  224.  * than maxchars characters. Prints an error if optional==false
  225.  * and there is no keyword. Returns true if one is found, false otherwise.
  226.  */
  227. {   register char * tp;
  228.     register int c;
  229.  
  230.     tp=target;
  231.     c=getc(fp);
  232.     if (c==KDELIM) {
  233.         if (!optional)
  234.             error("Missing keyword value");
  235.         VOID ungetc(c,fp);
  236.         return false;
  237.     } else {
  238.         while (!(c==' '||c=='\n'||c=='\t'||c==KDELIM||c==EOF)) {
  239.             if (tp-target>=maxchars-1) {
  240.                 error("keyword value too long");
  241.                 return false;
  242.             } else {
  243.                 *tp++ =c;
  244.                 c=getc(fp);
  245.             }
  246.         }
  247.         *tp= '\0';
  248. #       ifdef KEEPTEST
  249.         VOID printf("getval: %s\n",target);
  250. #       endif
  251.         while(c==' '||c=='\t') c=getc(fp); /* skip trailing blanks */
  252.     }
  253.     VOID ungetc(c,fp);
  254.     return true;
  255. }
  256.  
  257.  
  258. int getprevdate(fp,optional)
  259. FILE *fp;
  260. int optional;
  261. /* Function: reads a date prevdate; checks format
  262.  * If there is not date and optional==false, an error is printed.
  263.  * Returns false on error, true otherwise.
  264.  */
  265. {   char prevday[10];
  266.     char prevtime[10];
  267.  
  268.     prevday[0]=prevtime[0]='\0';
  269.     if (!getval(fp,prevday,9,optional)) return optional;
  270.     if (!getval(fp,prevtime,9,false)) return false;
  271.     /*process date */
  272.     prevday[2]=prevday[5]=prevday[8]=prevtime[2]=prevtime[5]='.';
  273.     prevday[9]='\0';
  274.     VOID strcpy(prevdate,prevday);
  275.     VOID strcat(prevdate,prevtime);
  276.     if (!checknum(prevdate,5)) {
  277.             error("Bad date: %s",prevdate);
  278.             prevdate[0]='\0';
  279.             return false;
  280.     }
  281.     return true;
  282. }
  283.  
  284. int checknum(sp,fields)
  285. register char * sp; int fields;
  286. {    register int dotcount;
  287.      if (sp==nil||*sp=='\0') return true;
  288.      dotcount=0;
  289.      while(*sp) {
  290.         if (*sp=='.') dotcount++;
  291.         elsif (ctab[*sp]!=DIGIT) return false;
  292.         sp++;
  293.      }
  294.      if (fields >= 0 && dotcount!=fields) return false;
  295.      return true;
  296. }
  297.  
  298.  
  299.  
  300. #ifdef KEEPTEST
  301. char * RCSfilename, * workfilename;
  302.  
  303. main(argc, argv)
  304. int  argc; char  *argv[];
  305. {
  306.     cmdid="keeptest";
  307.         while (*(++argv)) {
  308.                 if (getoldkeys(*argv))
  309.                 VOID printf("%s:  revision: %s, date: %s, author: %s, state: %s\n",
  310.                         *argv, prevrev, prevdate, prevauthor,prevstate);
  311.         VOID printf("Source: %s, RCSfile: %s\n",prevsource,prevRCS);
  312.     }
  313.     exit(0);
  314. }
  315. #endif
  316.