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

  1. /* Identify RCS keyword strings in 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: ident.c,v $
  31.  * Revision 5.7  1994/03/20 04:52:58  eggert
  32.  * Remove `exiting' from identExit.
  33.  *
  34.  * Revision 5.6  1993/11/09 17:40:15  eggert
  35.  * Add -V.
  36.  *
  37.  * Revision 5.5  1993/11/03 17:42:27  eggert
  38.  * Test for char == EOF, not char < 0.
  39.  *
  40.  * Revision 5.4  1992/01/24  18:44:19  eggert
  41.  * lint -> RCS_lint
  42.  *
  43.  * Revision 5.3  1991/09/10  22:15:46  eggert
  44.  * Open files with FOPEN_R, not FOPEN_R_WORK,
  45.  * because they might be executables, not working files.
  46.  *
  47.  * Revision 5.2  1991/08/19  03:13:55  eggert
  48.  * Report read errors immediately.
  49.  *
  50.  * Revision 5.1  1991/02/25  07:12:37  eggert
  51.  * Don't report empty keywords.  Check for I/O errors.
  52.  *
  53.  * Revision 5.0  1990/08/22  08:12:37  eggert
  54.  * Don't limit output to known keywords.
  55.  * Remove arbitrary limits and lint.  Ansify and Posixate.
  56.  *
  57.  * Revision 4.5  89/05/01  15:11:54  narten
  58.  * changed copyright header to reflect current distribution rules
  59.  * 
  60.  * Revision 4.4  87/10/23  17:09:57  narten
  61.  * added exit(0) so exit return code would be non random
  62.  * 
  63.  * Revision 4.3  87/10/18  10:23:55  narten
  64.  * Updating version numbers. Changes relative to 1.1 are actually relative
  65.  * to 4.1
  66.  * 
  67.  * Revision 1.3  87/07/09  09:20:52  trinkle
  68.  * Added check to make sure there is at least one arg before comparing argv[1]
  69.  * with "-q".  This necessary on machines that don't allow dereferncing null
  70.  * pointers (i.e. Suns).
  71.  * 
  72.  * Revision 1.2  87/03/27  14:21:47  jenkins
  73.  * Port to suns
  74.  * 
  75.  * Revision 4.1  83/05/10  16:31:02  wft
  76.  * Added option -q and input from reading stdin.
  77.  * Marker matching is now done with trymatch() (independent of keywords).
  78.  * 
  79.  * Revision 3.4  83/02/18  17:37:49  wft
  80.  * removed printing of new line after last file.
  81.  *
  82.  * Revision 3.3  82/12/04  12:48:55  wft
  83.  * Added LOCKER.
  84.  *
  85.  * Revision 3.2  82/11/28  18:24:17  wft
  86.  * removed Suffix; added ungetc to avoid skipping over trailing KDELIM.
  87.  *
  88.  * Revision 3.1  82/10/13  15:58:51  wft
  89.  * fixed type of variables receiving from getc() (char-->int).
  90. */
  91.  
  92. #include  "rcsbase.h"
  93.  
  94. static int match P((FILE*));
  95. static void scanfile P((FILE*,char const*,int));
  96.  
  97. mainProg(identId, "ident", "$Id: ident.c,v 5.7 1994/03/20 04:52:58 eggert Exp $")
  98. /*  Ident searches the named files for all occurrences
  99.  *  of the pattern $keyword: text $.
  100.  */
  101.  
  102. {
  103.    FILE *fp;
  104.    int quiet = 0;
  105.    int status = EXIT_SUCCESS;
  106.    char const *a;
  107.  
  108.    while ((a = *++argv)  &&  *a=='-')
  109.     while (*++a)
  110.         switch (*a) {
  111.         case 'q':
  112.             quiet = 1;
  113.             break;
  114.  
  115.         case 'V':
  116.             VOID printf("RCS version %s\n", RCS_version_string);
  117.             exitmain(0);
  118.  
  119.         default:
  120.             VOID fprintf(stderr,
  121.             "ident: usage: ident -{qV} [file...]\n"
  122.             );
  123.             exitmain(1);
  124.             break;
  125.         }
  126.  
  127.    if (!a)
  128.     scanfile(stdin, (char*)0, quiet);
  129.    else
  130.     do {
  131.         if (!(fp = fopen(a, FOPEN_R))) {
  132.         VOID fprintf(stderr,  "%s error: can't open %s\n", cmdid, a);
  133.         status = EXIT_FAILURE;
  134.         } else {
  135.         scanfile(fp, a, quiet);
  136.         if (argv[1]) VOID putchar('\n');
  137.         }
  138.     } while ((a = *++argv));
  139.  
  140.    if (ferror(stdout) || fclose(stdout)!=0) {
  141.       VOID fprintf(stderr,  "%s error: write error\n", cmdid);
  142.       status = EXIT_FAILURE;
  143.    }
  144.    exitmain(status);
  145. }
  146.  
  147. #if RCS_lint
  148.     void identExit() { _exit(EXIT_FAILURE); }
  149. #endif
  150.  
  151.  
  152.     static void
  153. scanfile(file, name, quiet)
  154.     register FILE *file;
  155.     char const *name;
  156.     int quiet;
  157. /* Function: scan an open file with descriptor file for keywords.
  158.  * Return false if there's a read error.
  159.  */
  160. {
  161.    register int c;
  162.  
  163.    if (name)
  164.       VOID printf("%s:\n", name);
  165.    else
  166.       name = "input";
  167.    c = 0;
  168.    for (;;) {
  169.       if (c == EOF) {
  170.      if (feof(file))
  171.         break;
  172.      if (ferror(file))
  173.         goto read_error;
  174.       }
  175.       if (c == KDELIM) {
  176.      if ((c = match(file)))
  177.         continue;
  178.      quiet = true;
  179.       }
  180.       c = getc(file);
  181.    }
  182.    if (!quiet)
  183.       VOID fprintf(stderr, "%s warning: no id keywords in %s\n", cmdid, name);
  184.    if (fclose(file) == 0)
  185.       return;
  186.  
  187.  read_error:
  188.    VOID fprintf(stderr, "%s error: %s: read error\n", cmdid, name);
  189.    exit(EXIT_FAILURE);
  190. }
  191.  
  192.  
  193.  
  194.     static int
  195. match(fp)   /* group substring between two KDELIM's; then do pattern match */
  196.    register FILE *fp;
  197. {
  198.    char line[BUFSIZ];
  199.    register int c;
  200.    register char * tp;
  201.  
  202.    tp = line;
  203.    while ((c = getc(fp)) != VDELIM) {
  204.       if (c == EOF  &&  feof(fp) | ferror(fp))
  205.      return c;
  206.       switch (ctab[c]) {
  207.      case LETTER: case Letter:
  208.         *tp++ = c;
  209.         if (tp < line+sizeof(line)-4)
  210.            break;
  211.         /* fall into */
  212.      default:
  213.         return c ? c : '\n'/* anything but 0 or KDELIM or EOF */;
  214.       }
  215.    }
  216.    if (tp == line)
  217.       return c;
  218.    *tp++ = c;
  219.    if ((c = getc(fp)) != ' ')
  220.       return c ? c : '\n';
  221.    *tp++ = c;
  222.    while( (c = getc(fp)) != KDELIM ) {
  223.       if (c == EOF  &&  feof(fp) | ferror(fp))
  224.         return c;
  225.       switch (ctab[c]) {
  226.      default:
  227.         *tp++ = c;
  228.         if (tp < line+sizeof(line)-2)
  229.            break;
  230.         /* fall into */
  231.      case NEWLN: case UNKN:
  232.         return c ? c : '\n';
  233.       }
  234.    }
  235.    if (tp[-1] != ' ')
  236.       return c;
  237.    *tp++ = c;     /*append trailing KDELIM*/
  238.    *tp   = '\0';
  239.    VOID fprintf(stdout, "     %c%s\n", KDELIM, line);
  240.    return 0;
  241. }
  242.  
  243.     void 
  244. exiterr()
  245. {
  246.     _exit(EXIT_FAILURE);
  247. }
  248.