home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 186_01 / check.c < prev    next >
Text File  |  1985-11-14  |  5KB  |  170 lines

  1. /*
  2. HEADER:        CUG186.26;
  3. TITLE:        Display nesting level of C code;
  4. VERSION:    1.3;
  5. DATE:        06/06/1986;
  6. DESCRIPTION:    "Displays the nesting level number of each BEGIN/END group";
  7. KEYWORDS:    NESTING, BRACES;
  8. SYSTEM:        CP/M-80, V2.2;
  9. FILENAME:    CHECK.C;
  10. SEE-ALSO:    CHECK.COM
  11. AUTHORS:    Ted Rabenko, Richard Conn;
  12. COMPILERS:    C80;
  13. */
  14. /*  LCHECK by Richard Conn
  15.  
  16.         LCHECK displays to the user the nesting level number of each
  17. BEGIN/END ({/}) group, thereby helping him to identify problem areas
  18. in his C programs.  It recognizes quoted material and comments and
  19. ignores { and } within these.
  20.  
  21. 08/01/84 - Fixed bug that caused program to treat an \' or \" as a "live"
  22.            quote.  (Therefore, a statement such as "#define QUOTE '\''"
  23.            would screw up the level counter in the program.  Changed version
  24.            number to 1.2A.  (Don McCrady)
  25.  
  26. 06/06/86 - Removed terminal dependent functions. Prevent read past EOF
  27.        if in the middle of quoted strings or comments. Converted
  28.        for use with C80.  Changed revision level to 1.3. (Ted Rabenko)
  29. */
  30.  
  31. #define VERS    "1.3"  /* Version Number */
  32.  
  33. #include        "stdio.h"
  34.  
  35.  
  36. #define TWIDTH  132     /* Terminal width */
  37. #define ESC     0x1B    /* Escape control sequence */
  38. #define quote   0x27    /* Single Quote */
  39. #define dquote  0x22    /* Double Quote */
  40. #define bslash  0x5c    /* Backslash escape character */
  41. #define BS      0x08    /* Back Space Char */
  42. #define TAB     0x09    /* Tab Char */
  43. #define LF      0x0a    /* Line Feed Char */
  44. #define CR      0x0d    /* Carriage Return Char */
  45. #define ovfl    'Y'     /* Line Overflow */
  46. #define noovfl  'N'     /* No Line Overflow */
  47.  
  48. int     level, pos, nroutines;
  49.  
  50. main(argc,argv)
  51. int argc;
  52. char **argv;
  53. {
  54.         int chval, done;
  55.     FILE    in_fp;
  56.  
  57.         if (argc == 1) {
  58.                 printf("CHECK, Version %s\n",VERS);
  59.                 printf("Format of Command Line is --\n");
  60.                 printf("  CHECK filename.typ");
  61.                 exit(FALSE);
  62.                 }
  63.         if ((in_fp=fopen(argv[1],"r")) == NULL) {
  64.                 printf("Cannot Find File %s\n",argv[1]);
  65.                 exit(FALSE);
  66.                 }
  67.         printf("CHECK, Version %s -- File:  %s\n",VERS,argv[1]);
  68.         level = 0; nroutines = 0;  /* Init nesting level, routine count */
  69.         prlevel();  /* Print level number */
  70.         while ((chval=getit(in_fp)) != EOF)
  71.     {
  72.         switch (chval) {
  73.             case bslash: getnoeof(in_fp);
  74.                      break; /* Ignore escaped characters */
  75.  
  76.             case quote:  while(getnoeof(in_fp) != quote)
  77.                        ; /* If quote, flush to end quote */
  78.                      break;  
  79.  
  80.             case dquote: while(getnoeof(in_fp) != dquote)
  81.                        ; /* If dquote, flush to end */
  82.                      break; 
  83.  
  84.             case '/'  :        /* Possible comment */
  85.                                  if ((chval=getnoeof(in_fp)) == '*')
  86.                                            do {          /*Yes, it is a comment*/
  87.                                                while(getnoeof(in_fp) != '*')
  88.                            ; /* End comment? */
  89.                                        } while (getnoeof(in_fp) != '/');
  90.                      break;
  91.  
  92.             case '{'  :  level++;    /* BEGIN */
  93.                      break;
  94.  
  95.             case '}'  :  level--;    /* END */
  96.                      if (level == 0) {
  97.                        nroutines++;
  98.                        printf("\n** Routine %d **", nroutines);
  99.                      }
  100.             }
  101.     } 
  102.  
  103.     printf("\nProgram Level Check is ");
  104.     if (level == 0) printf("OK");
  105.         else printf("NOT OK");
  106.     printf("\nNumber of Routines Encountered: %d",--nroutines);
  107. }
  108.  
  109.  
  110. int getnoeof(fp)   /* test for EOF before return */
  111. FILE fp;
  112. {
  113.     int chval;
  114.     if ((chval = getit(fp)) == EOF)
  115.     {
  116.         printf("\npremature EOF\n");
  117.         exit();
  118.     }
  119.     return(chval);
  120. }
  121.  
  122. int getit(fp)  /* Get and Echo Character */
  123. FILE fp;
  124. {
  125.     int chval;
  126.     chval = getc(fp);
  127.     if ((pos >= TWIDTH) & (chval != CR)) prlevel(ovfl);
  128.     if (chval != EOF)
  129.          echo(chval);
  130.     return(chval);
  131. }
  132.  
  133. echo(chval)  /* Echo Char with tabulation */
  134. char chval;
  135. {
  136.     switch (chval) {
  137.         case TAB : putchar(' '); pos++;
  138.                while (pos%7 != 0) {
  139.                 putchar(' ');
  140.                 pos++;
  141.                 }
  142.                break;
  143.         case BS  : putchar(BS);
  144.                pos--;
  145.                break;
  146.         case LF  : prlevel(noovfl);
  147.                break;
  148.         case CR  : putchar(CR);
  149.                pos = 0;
  150.                break;
  151.         default  : if (chval >= ' ') {
  152.                 putchar(chval);
  153.                 pos++;
  154.                 }
  155.                break;
  156.         }
  157. }
  158.  
  159. prlevel(ovfl_flag)  /* Print Level Number and Set Col Count */
  160. char ovfl_flag;
  161. {
  162.     putchar(LF);
  163.     if (level < 10) printf(" %d",level);
  164.         else printf("%d",level);
  165.     if (ovfl_flag == 'Y') putchar('-');
  166.         else putchar(':');
  167.     putchar(' ');
  168.     pos = 5;
  169. }
  170.