home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / examples / clib / ccheck / ccheck.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  23KB  |  900 lines

  1. /*
  2.  * CCHECK.C -- Amiga Lattice C version 4/8/86
  3.  *
  4.  *
  5.  * Copyright: The Regents of the University of California
  6.  *   [Note - since Steve Draper distributed cchk.c over
  7.  *    Usenet, I assume it's effectively in the public
  8.  *    domain. JCM
  9.  *
  10.  * Title:  ccheck
  11.  *
  12.  * Purpose: To find and report all badly matched openers and
  13.  *   closers plus assignment/equality confusions
  14.  *   in a c source file.
  15.  *
  16.  *
  17.  * Author:  Steve Draper, expanding cnest by Tom Anderson
  18.  *
  19.  * Usage:  ccheck [-q] [-v] <filename1> <filename2> ...
  20.  *
  21.  * History (in reverse order to minimize reading time for updates):
  22.  *
  23.  *   June 13, 1994 Jason Petty
  24.  *   - Must get valid file name else exits with usage.
  25.  *     Changes marked VANSOFT.
  26.  *
  27.  *
  28.  *   April 8, 1986  Converted to Amiga Lattice C from from butchered text
  29.  *                  file.  If any one can supply me with the original source
  30.  *                  I would appreciate it.  IF-ELSE check seems flaky.
  31.  *  
  32.  *                           Thom Althoff (BIX althoff)
  33.  *                       c/o American Broadcasting Companies
  34.  *                           30 W. 67th St Video Tape Tech Support 
  35.  *                           Floor B1
  36.  *                           New York City, N.Y. 10023
  37.  *
  38.  *
  39.  *   June 18, 1985  Converted to Aztec C - removed BDS C code  -  Rick Moore
  40.  *
  41.  *   January 9, 1983  Single BDS/UNIX source created
  42.  *      as CCHECK.C  -- Jeff Martin
  43.  *
  44.  *   December 29, 1982 Adapted for BDS C --
  45.  *       Jeff Martin at home.
  46.  *
  47.  *   December 20, 1982 Converted to cchk --
  48.  *       Steve Draper at UCSD
  49.  *
  50.  *   December 9, 1982 Jeffrey Mogul at Stanford
  51.  *    - checks for unclosed comment at end of file.
  52.  *
  53.  *   December 3, 1982 creation date --
  54.  *      Tom Anderson at microsof!fluke
  55.  *
  56.  */
  57.  
  58. #include <stdio.h>
  59. #include <ctype.h>
  60.  
  61. #define TRUE 1
  62. #define FALSE 0
  63. #define SPACE 32
  64.  
  65. #define BRACE 1
  66. #define SQBRAK  2
  67. #define PAREN 3
  68. #define IF 4
  69. #define IFCOND 5
  70. #define WHLCOND 6
  71. #define THEN 7
  72. #define ELSE 8
  73.  
  74. #define STACKSIZ 64
  75.  
  76. struct brak
  77. {
  78.     int type, b_indent, b_ln;
  79. } stack[STACKSIZ];
  80.  
  81. #define rmbrak(N) (top -= N, stackc -= N)
  82. #define myungetc(C)  ungetc(((C) == '\n' ? SPACE : (C)), infile)
  83.  
  84. #define VFLAG "-v"
  85. #define QFLAG "-q"
  86. #define SFLAG "-s"
  87.  
  88. int firsttime; /* This was a static in mygetchar() */
  89.  
  90. int mygetchar(), pr();
  91. void checkelse(), newbrak(), checkcloser(), prtype();
  92. void usage();
  93.  
  94. FILE *infile;
  95. int ln, indent, commindent, stackc, commln;
  96. int singlequoterr, oddsinglequote, bracecnt, parencnt, sqbrakcnt;
  97. int errstatus, wstatus;
  98. int errnmb, wnmb;
  99. int verbose;
  100. char *filename;
  101. struct brak *top;
  102.  
  103. void usage(op) /* Added, VANSOFT. */
  104. int op;
  105. {
  106.   printf(
  107.   "\n\n\nCCHECK: Copyright - The Regents of the University of California.\n");
  108.   printf("Author: Steve Draper.\n\n");
  109.   printf("Purpose: To find and report all badly matched openers and\n");
  110.   printf(
  111.   "closers plus assignment/equality confusions in a c source file.\n\n");
  112.  
  113.   printf("USAGE:  CCHECK [-q] [-v] <filename1> <filename2> ...\n\n");
  114.   printf("Flags:\n");
  115.   printf("\t-q   -  Suppress warning messages.\n");
  116.   printf("\t-v   -  Verbose.\n\n\n");
  117.  
  118.  if(!op)
  119.   exit(10); /* Exit error. */
  120. }
  121.  
  122. main(argc, argv)
  123. unsigned argc ;
  124. char *argv[] ;
  125. {
  126.     register int c;
  127.     int i;
  128.     int doubleqflag;
  129.     unsigned file_index;
  130.  
  131.     wnmb = 0;
  132.     verbose = 0;
  133.     file_index = 1;
  134.  
  135.     while (argc > 1  &&  argv[file_index][0] == '-')
  136.        {
  137.         if (strcmp(argv[file_index], VFLAG) == 0)
  138.             verbose++;
  139.         if (strcmp(argv[file_index], QFLAG) == 0)
  140.             wnmb = -2;
  141.         if (strcmp(argv[file_index], SFLAG) == 0)
  142.             wnmb = -2;
  143.         file_index++;
  144.         argc--;
  145.        }
  146.  
  147.     do
  148.      {
  149.         /* INIT for each file */
  150.         firsttime = 1;
  151.         doubleqflag = 0;
  152.         errstatus = wstatus = 0;
  153.         ln = 1;
  154.         indent = 0;
  155.         commindent = 0;
  156.         singlequoterr = oddsinglequote = parencnt = sqbrakcnt = bracecnt = 0;
  157.         errnmb = 0;
  158.         if (wnmb > -2)
  159.             wnmb = 0;
  160.         newbrak(0);
  161.  
  162.         if (argc == 1 || argc == 0)
  163.           {
  164.             usage(NULL); /* usage. Added, VANSOFT. */
  165.            }
  166.         else
  167.          {
  168.           if ((infile = fopen(argv[file_index], "r")) == (FILE * ) NULL)
  169.            {
  170.             usage(1); /* 1 = don't 'exit()' yet. (usage. Added,VANSOFT) */
  171.      fprintf(stdout, "%s: Can't access %s!\n\n",argv[0],argv[file_index]);
  172.             exit(10); /* exit error. */ 
  173.             }
  174.            filename = argv[file_index]; 
  175.           }
  176.  
  177.         while ( ( c = mygetchar()) !=  EOF )
  178.            {
  179.             if (verbose == 2)
  180.                {
  181.                 for (i = stackc; i > 0; i--)
  182.                   {
  183.             printf("%c %d: type ", c, i);
  184.             prtype(stack[i].type);
  185.             printf(", indent %d, line %d.\n",stack[i].b_indent,stack[i].b_ln);
  186.                   }
  187.                }
  188.  
  189.             switch (c)
  190.             {
  191.             case ';':
  192.                 ungetc(SPACE, infile);
  193.                 while (top->type == ELSE)
  194.                     rmbrak(1);
  195.                 if (top->type == THEN)
  196.                 {
  197.                     rmbrak(1);
  198.                     checkelse();
  199.                 }
  200.                 break;
  201.  
  202.             case '!':
  203.             case '>':
  204.             case '<':
  205.                 /* swallow legit. '=' chars */
  206.                 c = mygetchar();
  207.                 if (c != '=')
  208.                     myungetc(c);
  209.                 break;
  210.  
  211.             case '=':
  212.                 if ((top - 1)->type == IFCOND  ||  (top - 1)->type == WHLCOND)
  213.                    {
  214.                     c = mygetchar();
  215.                     if (c != '=')
  216.                       {
  217.                         myungetc(c);
  218.               if(pr(1))
  219.                printf("Assignment instead of equals in conditional,%d\n", ln);
  220.                     }
  221.                 }
  222.                 break;
  223.  
  224.             case '\n':
  225.             case SPACE:
  226.                 c = mygetchar();
  227.                 switch (c) {
  228.                 case 'i':
  229.                     /* if */
  230.                     c = mygetchar();
  231.                   if (c == 'f' && !isalpha(c = fgetc(infile)) && !isdigit(c))
  232.                       {
  233.                         ungetc(c, infile);
  234.                         newbrak(IF);
  235.                         while ((c = mygetchar()) == SPACE ||  c == '\n');
  236.                         if (c != '(')
  237.                         {
  238.                             if (pr(1))
  239.                                 printf("Bad if (no condition) line %d.\n",ln);
  240.                             rmbrak(1);
  241.                         }
  242.                         else
  243.                             newbrak(IFCOND);
  244.                             myungetc(c);
  245.                     }
  246.                         else
  247.                         myungetc(c);
  248.                     break;
  249.                 case 'w':
  250.                     /* while */
  251.                     if ((c = mygetchar()) == 'h'
  252.                       &&  (c = mygetchar()) == 'i'
  253.                       &&  (c = mygetchar()) == 'l'
  254.                       &&  (c = mygetchar()) == 'e'
  255.                       &&  !isalpha(c = fgetc(infile))  &&  !isdigit(c))
  256.                       {
  257.                         ungetc(c, infile);
  258.                         while ((c = mygetchar()) == SPACE ||  c == '\n');
  259.                         if (c != '(')
  260.                          {
  261.                         if(pr(1))
  262.                            printf("Bad while (no condition) line %d.\n",ln);
  263.                         }
  264.                         else
  265.                             newbrak(WHLCOND);
  266.                             myungetc(c);
  267.                     }
  268.                     else
  269.                         myungetc(c);
  270.                     break;
  271.                 case 'e':
  272.                     /* else */
  273.                     myungetc(c);
  274.                     checkelse();
  275.                     break;
  276.  
  277.                 default:
  278.                     myungetc(c);
  279.                     break;
  280.                 }
  281.                 break;
  282.  
  283.             case '*':
  284.                 /* close comment ? */
  285.                 c = mygetchar();
  286.                 if (c != '/')
  287.                 {
  288.                     myungetc(c);
  289.                     break;
  290.                 }
  291.  
  292.    if(pr(1))
  293.       printf("