home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / rcs4 / source / ident.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-08  |  6.4 KB  |  229 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Distributed under license by the Free Software Foundation, Inc.
  3.  
  4. This file is part of RCS.
  5.  
  6. RCS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. RCS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with RCS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. Report problems and direct all questions to:
  21.  
  22.     rcs-bugs@cs.purdue.edu
  23.  
  24. */
  25.  
  26. /*
  27.  *                     RCS identification operation
  28.  */
  29. #ifndef lint
  30. static char rcsid[]=
  31. "$Header: d:/rcs/rcs/ident.c 5.3 90/07/15 20:23:52 lfk Release $Purdue CS";
  32. #endif
  33.  
  34. /* $Log:    ident.c $
  35.  * Revision 5.3  90/07/15  20:23:52  lfk
  36.  * checked in with -k by ROOT_DOS at 91.02.07.11.44.47.
  37.  * 
  38.  * Revision 5.3  90/07/15  20:23:52  lfk
  39.  * Most major fixes added between rev 5.1 and rev 5.5:
  40.  *     signals fixed so they work on MS-DOS
  41.  *     Added MKS arguments code so argv can be large
  42.  *     added code to handle slashes a'la Unix
  43.  *     added more file extensions to system from MS-DOS
  44.  * 
  45.  * Revision 5.2  90/07/15  11:31:31  ROOT_DOS
  46.  * DOS version of RCS 4.0 checked in for MODS
  47.  * by lfk@athena.mit.edu
  48.  * Also update to MSC 6.0
  49.  * 
  50.  * Revision 4.5  89/05/01  15:11:54  narten
  51.  * changed copyright header to reflect current distribution rules
  52.  * 
  53.  * Revision 4.4  87/10/23  17:09:57  narten
  54.  * added exit(0) so exit return code would be non random
  55.  * 
  56.  * Revision 4.3  87/10/18  10:23:55  narten
  57.  * Updating version numbers. Changes relative to 1.1 are actually relative
  58.  * to 4.1
  59.  * 
  60.  * Revision 1.3  87/07/09  09:20:52  trinkle
  61.  * Added check to make sure there is at least one arg before comparing argv[1]
  62.  * with "-q".  This necessary on machines that don't allow dereferncing null
  63.  * pointers (i.e. Suns).
  64.  * 
  65.  * Revision 1.2  87/03/27  14:21:47  jenkins
  66.  * Port to suns
  67.  * 
  68.  * Revision 1.1  84/01/23  14:50:03  kcs
  69.  * Initial revision
  70.  * 
  71.  * Revision 4.1  83/05/10  16:31:02  wft
  72.  * Added option -q and input from reading stdin.
  73.  * Marker matching is now done with trymatch() (independent of keywords).
  74.  * 
  75.  * Revision 3.4  83/02/18  17:37:49  wft
  76.  * removed printing of new line after last file.
  77.  *
  78.  * Revision 3.3  82/12/04  12:48:55  wft
  79.  * Added LOCKER.
  80.  *
  81.  * Revision 3.2  82/11/28  18:24:17  wft
  82.  * removed Suffix; added ungetc to avoid skipping over trailing KDELIM.
  83.  *
  84.  * Revision 3.1  82/10/13  15:58:51  wft
  85.  * fixed type of variables receiving from getc() (char-->int).
  86. */
  87.  
  88. #include  "rcsbase.h"
  89. #define fflsbuf _flsbuf
  90. /* redefinition of _flsbuf in putc not needed */
  91. #ifndef lint
  92. static char rcsbaseid[] = RCSBASE;
  93. #endif
  94.  
  95. extern enum markers trymatch();
  96.  
  97. int quietflag;
  98.  
  99. #ifdef MKS
  100. main(int argc, char *argv[], char *env[])
  101. #else
  102. main (argc, argv)
  103. int argc;
  104. char * argv[];
  105. #endif /* MKS */
  106. /*  Ident searches the named files for all occurrences
  107.  *  of the pattern $keyword:...$, where the keywords are
  108.  *  Author, Date, Header, Id, Log, RCSfile, Revision, Source, and State.
  109.  */
  110.  
  111. {
  112.    FILE *fp, *fopen();
  113. #ifdef MKS
  114.     int z = 0;
  115.     int ARGC = 0;
  116.     char **tmp;
  117.     char *env_name;
  118. #    define MAXARGS 500        /* This means 500 items on the command line */
  119.     if (!(tmp = (char **)malloc(sizeof(char *)*(MAXARGS+1)))) {
  120.         fprintf(stderr, "%s: can't allocate space for arguments\n",argv[0]);
  121.         exit(1);
  122.     }
  123.     for ( z = 0 ; env[z] != NULL; z++) {    /* loop through environment */
  124.         if (*env[z] == '~') {    /* testing for entries begining with '~' */
  125.             *++env[z];            /* increment pointer to delete '~' */
  126.             tmp[ARGC++] = env[z];    /* add it to our new array */
  127.             if (z >= MAXARGS) {
  128.                 fprintf(stderr, "%s: can't handle any more arguments\n", argv[0]);
  129.                 goto list;
  130.             }
  131.         }
  132.         else if (*env[z] == '_') {    /* testing for entries begining with _ */
  133.             *++env[z] ; *++env[z];    /* move past the '_' and the '=' */
  134.             env_name = env[z];    /* copy the name */
  135.         }
  136.     }
  137. list:
  138.     if ( STREQ( (char *) argv[0] , env_name ) ) {    /* test name against startup args */
  139.         /* environment arguments meant for this program */
  140. #    ifdef DEBUG
  141.         printf("Using shell supplied args\n");
  142. #    endif
  143.         argc = ARGC;
  144.         tmp[ARGC] = NULL;    /* the terminal NULL */
  145.         argv = tmp;
  146.     }
  147. #    ifdef DEBUG
  148.     else 
  149.         printf("Using startup supplied args\n");
  150. #    endif /* debug */
  151. #endif /* MKS */
  152.  
  153.    quietflag = false;
  154.    if (argc > 1 && strcmp("-q",argv[1])==0) {
  155.         quietflag = true;
  156.         argc--; argv++;
  157.    }
  158.  
  159.    if (argc<2) {
  160.      if ((scanfile(stdin) == 0) && !quietflag)
  161.         VOID fprintf(stderr, "ident warning: no id keywords in input\n");
  162.     exit(0);
  163.    }
  164.  
  165.    while ( --argc > 0 ) {
  166. #ifdef MSDOS
  167.       if ( (fp = fopen(*++argv, "rb") ) == NULL ) {
  168. #else
  169.       if ( (fp = fopen(*++argv, "r") ) == NULL ) {
  170. #endif
  171.          VOID fprintf(stderr,  "ident error: can't open %s\n", *argv);
  172.          continue;
  173.       } else {
  174.          VOID printf( "%s:\n", *argv);   /*  print file name  */
  175.      if ((scanfile(fp) == 0) && !quietflag)
  176.         VOID fprintf(stderr, "ident warning: no id keywords in %s\n", *argv);
  177.      if (argc>1) putchar('\n');
  178.      VOID fclose(fp);
  179.       }
  180.    }
  181.    exit(0);
  182. }
  183.  
  184.  
  185. int scanfile(file)
  186. FILE * file;
  187. /* Function: scan an open file with descriptor file for keywords.
  188.  * Returns the number of matches.
  189.  */
  190. {
  191.    register int matchcount;
  192.    register int c;
  193.  
  194.  
  195.    matchcount = 0;
  196.    while( (c=getc(file)) != EOF) {
  197.       if ( (char)c==KDELIM)
  198.      matchcount += match(file);
  199.    }
  200.    return matchcount;
  201. }
  202.  
  203.  
  204.  
  205. match(fp)   /* group substring between two KDELIM's; then do pattern match */
  206. FILE *fp;
  207. {
  208.    char line[keyvallength];
  209.    register int c;
  210.    register char * tp;
  211.  
  212.    tp = line;
  213.    while( (c = getc(fp)) != KDELIM ) {
  214.       *tp++ = c;
  215.       if ( c==EOF || c=='\n' || tp>= line+keyvallength-2)
  216.          return(0);
  217.    }
  218.    *tp++ = c;     /*append trailing KDELIM*/
  219.    *tp   = '\0';
  220.    if (trymatch(line,true)!=Nomatch) {
  221.         VOID fprintf(stdout,"     $%s\n",line);
  222.         return(1);
  223.    } else {
  224.       /* no match; put trailing KDELIM back into input */
  225.       VOID ungetc(c,fp );
  226.       return(0);
  227.    }
  228. }
  229.