home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / c-util.zip / CC.C next >
Text File  |  1984-04-15  |  3KB  |  84 lines

  1. /****************************************************************
  2. *                                                               *
  3. *                       CC (C Checker)                          *
  4. *                                                               *
  5. *            C Source Paren, bracket and comment Checker        *
  6. *                                                               *
  7. *                T. Jennings  -- Sometime in 1983               *
  8. *                                                               *
  9. *                                                               *
  10. ****************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14.  
  15. /* Very crude but very effective C source debugger. Counts the numbers of
  16. matching braces, parenthesis and comments, and displays them at the left edge
  17. of the screen. The best way to see what it does is to do it; try
  18.  
  19.         CC CC.C          C Check CC.C
  20.  
  21.  
  22. Properly handles parens and brackets inside comments; they are ignored.
  23. */
  24.  
  25. main(argc,argv)
  26. int argc;
  27. char **argv;
  28. {
  29. int file;
  30. char c,lastc;
  31. int parens,brackets,comments;
  32. int line, col;
  33. char hdr[40];
  34.  
  35.         file= open(argv[1],0x8000);
  36.         if (file == -1) {
  37.                 cprintf("File missing. Try CC <filename.ext> \r\n");
  38.                 exit();
  39.         }
  40.         brackets= parens= comments= 0;
  41.         line= 0; col= 0;
  42.         lastc= '\0';
  43.  
  44.         while (read(file,&c,1)) {
  45.                 if (c == 0x1a) break;                   /* stop if ^Z */
  46.  
  47.                 if (col == 0) {
  48.                         sprintf(hdr,"%d: {%d} (%d) /*%d*/",line,brackets,parens,comments);
  49.                         while (strlen(hdr) < 23)        /* gross but works */
  50.                                 strcat(hdr," ");        /* to hell with elegant, */
  51.                         cprintf("%s|",hdr);             /* we got a job to do !*/
  52.                 }                               /* (real programmers dont ...) */
  53.  
  54. /* Dont count parens and brackets that are inside comments. This of course
  55. assumes that comments are properly matched; in any case, that will be the
  56. first thing to look for. */
  57.  
  58.                 if (comments <= 0) {
  59.                         if (c == '{') ++brackets;
  60.                         if (c == '(') ++parens;
  61.                         if (c == '}') --brackets;
  62.                         if (c == ')') --parens;
  63.                 }
  64.  
  65. /* Now do comments. This properly handles nested comments, whether or
  66. not the compiler does is your responsibility */
  67.  
  68.                 if ((c == '*') && (lastc == '/')) ++comments;
  69.                 if ((c == '/') && (lastc == '*')) --comments;
  70.  
  71.                 ++col;
  72.                 if (c == 0x0a) {                /* newline == New Line */
  73.                         col= 0;                 /* set column 0 */
  74.                         ++line;
  75.                 }
  76.                 putchar(c);                     /* display text */
  77.                 lastc= c;                       /* update last char */
  78.         }
  79.         cprintf("\r\n\r\n");
  80.         if (brackets) cprintf("Unbalanced brackets\r\n");
  81.         if (parens) cprintf("Unbalanced parenthesis\r\n");
  82.         if (comments) cprintf("Unbalanced comments\r\n");
  83. }
  84.