home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / ident.c < prev    next >
C/C++ Source or Header  |  1992-02-19  |  6KB  |  226 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Copyright 1990, 1991 by Paul Eggert
  3.    Distributed under license by the Free Software Foundation, Inc.
  4.  
  5. This file is part of RCS.
  6.  
  7. RCS is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. RCS is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with RCS; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. Report problems and direct all questions to:
  22.  
  23.     rcs-bugs@cs.purdue.edu
  24.  
  25. */
  26.  
  27. /*
  28.  *                     RCS identification operation
  29.  */
  30.  
  31. /* $Log: ident.c,v $
  32.  * Revision 5.4  1992/01/24  18:44:19  eggert
  33.  * lint -> RCS_lint
  34.  *
  35.  * Revision 5.3  1991/09/10  22:15:46  eggert
  36.  * Open files with FOPEN_R, not FOPEN_R_WORK,
  37.  * because they might be executables, not working files.
  38.  *
  39.  * Revision 5.2  1991/08/19  03:13:55  eggert
  40.  * Report read errors immediately.
  41.  *
  42.  * Revision 5.1  1991/02/25  07:12:37  eggert
  43.  * Don't report empty keywords.  Check for I/O errors.
  44.  *
  45.  * Revision 5.0  1990/08/22  08:12:37  eggert
  46.  * Don't limit output to known keywords.
  47.  * Remove arbitrary limits and lint.  Ansify and Posixate.
  48.  *
  49.  * Revision 4.5  89/05/01  15:11:54  narten
  50.  * changed copyright header to reflect current distribution rules
  51.  *
  52.  * Revision 4.4  87/10/23  17:09:57  narten
  53.  * added exit(0) so exit return code would be non random
  54.  *
  55.  * Revision 4.3  87/10/18  10:23:55  narten
  56.  * Updating version numbers. Changes relative to 1.1 are actually relative
  57.  * to 4.1
  58.  *
  59.  * Revision 1.3  87/07/09  09:20:52  trinkle
  60.  * Added check to make sure there is at least one arg before comparing argv[1]
  61.  * with "-q".  This necessary on machines that don't allow dereferncing null
  62.  * pointers (i.e. Suns).
  63.  *
  64.  * Revision 1.2  87/03/27  14:21:47  jenkins
  65.  * Port to suns
  66.  *
  67.  * Revision 4.1  83/05/10  16:31:02  wft
  68.  * Added option -q and input from reading stdin.
  69.  * Marker matching is now done with trymatch() (independent of keywords).
  70.  *
  71.  * Revision 3.4  83/02/18  17:37:49  wft
  72.  * removed printing of new line after last file.
  73.  *
  74.  * Revision 3.3  82/12/04  12:48:55  wft
  75.  * Added LOCKER.
  76.  *
  77.  * Revision 3.2  82/11/28  18:24:17  wft
  78.  * removed Suffix; added ungetc to avoid skipping over trailing KDELIM.
  79.  *
  80.  * Revision 3.1  82/10/13  15:58:51  wft
  81.  * fixed type of variables receiving from getc() (char-->int).
  82. */
  83.  
  84. #include  "rcsbase.h"
  85.  
  86. static int match P((FILE*));
  87. static void scanfile P((FILE*,char const*,int));
  88.  
  89. static char const cmdusage[] =
  90.     "\nident usage: ident -{q} file ...";
  91.     
  92. mainProg(identId, "ident", "$Id: ident.c,v 5.4 1992/01/24 18:44:19 eggert Exp $")
  93. /*  Ident searches the named files for all occurrences
  94.  *  of the pattern $keyword:...$, where the keywords are
  95.  *  Author, Date, Header, Id, Log, RCSfile, Revision, Source, and State.
  96.  */
  97.  
  98. {
  99.    FILE *fp;
  100.    int quiet;
  101.    int status = EXIT_SUCCESS;
  102.  
  103.    if ((quiet  =  argc > 1 && strcmp("-q",argv[1])==0)) {
  104.         argc--; argv++;
  105.    }
  106.  
  107.    if (argc<2) {
  108.     if (isatty(0)) {
  109.             fprintf(stderr,"ident error: no input file%s\nident aborted\n", cmdusage);
  110.         exit(EXIT_FAILURE);
  111.         }
  112.     scanfile(stdin, (char*)0, quiet);
  113.    }
  114.  
  115.    while ( --argc > 0 ) {
  116.       if (!(fp = fopen(*++argv, FOPEN_R))) {
  117.      VOID fprintf(stderr,  "%s error: can't open %s\n", cmdid, *argv);
  118.      status = EXIT_FAILURE;
  119.       } else {
  120.      scanfile(fp, *argv, quiet);
  121.      if (argc>1) VOID putchar('\n');
  122.       }
  123.    }
  124.    if (ferror(stdout) || fclose(stdout)!=0) {
  125.       VOID fprintf(stderr,  "%s error: write error\n", cmdid);
  126.       status = EXIT_FAILURE;
  127.    }
  128.    exitmain(status);
  129. }
  130.  
  131. #if RCS_lint
  132.     exiting void identExit() { _exit(EXIT_FAILURE); }
  133. #endif
  134.  
  135.  
  136.     static void
  137. scanfile(file, name, quiet)
  138.     register FILE *file;
  139.     char const *name;
  140.     int quiet;
  141. /* Function: scan an open file with descriptor file for keywords.
  142.  * Return false if there's a read error.
  143.  */
  144. {
  145.    register int c;
  146.  
  147.    if (name)
  148.       VOID printf("%s:\n", name);
  149.    else
  150.       name = "input";
  151.    c = 0;
  152.    for (;;) {
  153.       if (c < 0) {
  154.      if (feof(file))
  155.         break;
  156.      if (ferror(file))
  157.         goto read_error;
  158.       }
  159.       if (c == KDELIM) {
  160.      if ((c = match(file)))
  161.         continue;
  162.      quiet = true;
  163.       }
  164.       c = getc(file);
  165.    }
  166.    if (!quiet)
  167.       VOID fprintf(stderr, "%s warning: no id keywords in %s\n", cmdid, name);
  168.    if (fclose(file) == 0)
  169.       return;
  170.  
  171.  read_error:
  172.    VOID fprintf(stderr, "%s error: %s: read error\n", cmdid, name);
  173.    exit(EXIT_FAILURE);
  174. }
  175.  
  176.  
  177.  
  178.     static int
  179. match(fp)   /* group substring between two KDELIM's; then do pattern match */
  180.    register FILE *fp;
  181. {
  182.    char line[BUFSIZ];
  183.    register int c;
  184.    register char * tp;
  185.  
  186.    tp = line;
  187.    while ((c = getc(fp)) != VDELIM) {
  188.       if (c < 0)
  189.      return c;
  190.       switch (ctab[c]) {
  191.      case LETTER: case Letter:
  192.         *tp++ = c;
  193.         if (tp < line+sizeof(line)-4)
  194.            break;
  195.         /* fall into */
  196.      default:
  197.         return c ? c : '\n'/* anything but 0 or KDELIM or EOF */;
  198.       }
  199.    }
  200.    if (tp == line)
  201.       return c;
  202.    *tp++ = c;
  203.    if ((c = getc(fp)) != ' ')
  204.       return c ? c : '\n';
  205.    *tp++ = c;
  206.    while( (c = getc(fp)) != KDELIM ) {
  207.       if (c < 0  &&  feof(fp) | ferror(fp))
  208.         return c;
  209.       switch (ctab[c]) {
  210.      default:
  211.         *tp++ = c;
  212.         if (tp < line+sizeof(line)-2)
  213.            break;
  214.         /* fall into */
  215.      case NEWLN: case UNKN:
  216.         return c ? c : '\n';
  217.       }
  218.    }
  219.    if (tp[-1] != ' ')
  220.       return c;
  221.    *tp++ = c;     /*append trailing KDELIM*/
  222.    *tp   = '\0';
  223.    VOID fprintf(stdout, "     %c%s\n", KDELIM, line);
  224.    return 0;
  225. }
  226.