home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 152_01 / cc.c < prev    next >
Text File  |  1985-03-10  |  4KB  |  111 lines

  1.  
  2.   /*
  3. HEADER:                 CUG152.15;
  4. TITLE:                  A "C" program checker source code file;
  5. DATE:                   09/04/85;
  6. DESCRIPTION:
  7.    "The source code of a utility to check for matched braces and
  8.       other essential elements by counting the numbers of braces,
  9.       parenthesis, and comments in order to debug 'C' source code."
  10. KEYWORDS:               debug, code, checker;
  11. FILENAME:               CC.C;
  12. WARNINGS:
  13.    "The typist of this heading does not have equipment to run
  14.       this program and prepared this header from comments
  15.       within the source code or companion files."
  16.     The authors claim copyrights and authorize non-commercial
  17.       use only."
  18. AUTHORS:                T. Jennings
  19.                         David N. Smith;
  20. COMPILERS:              CI/C86;
  21. REFERENCES:
  22.     AUTHERS:            " ";
  23.     TITLE:              " ";
  24.     CITATION:           " ";
  25. ENDREF
  26. */
  27.  
  28. /****************************************************************
  29. *                                                               *
  30. *                       CC (C Checker)                          *
  31. *                                                               *
  32. *            C Source Paren, bracket and comment Checker        *
  33. *                                                               *
  34. *                T. Jennings  -- Sometime in 1983               *
  35. *                                                               *
  36. *                                                               *
  37. ****************************************************************/
  38.  
  39. #include <stdio.h>
  40. #include <ctype.h>
  41.  
  42. /* Very crude but very effective C source debugger. Counts the numbers of
  43. matching braces, parenthesis and comments, and displays them at the left edge
  44. of the screen. The best way to see what it does is to do it; try
  45.  
  46.         CC CC.C          C Check CC.C
  47.  
  48.  
  49. Properly handles parens and brackets inside comments; they are ignored.
  50. */
  51.  
  52. main(argc,argv)
  53. int argc;
  54. char **argv;
  55. {
  56. int file;
  57. char c,lastc;
  58. int parens,brackets,comments;
  59. int line, col;
  60. char hdr[40];
  61.  
  62.         file= open(argv[1],0x8000);
  63.         if (file == -1) {
  64.                 cprintf("File missing. Try CC <filename.ext> \r\n");
  65.                 exit();
  66.         }
  67.         brackets= parens= comments= 0;
  68.         line= 0; col= 0;
  69.         lastc= '\0';
  70.  
  71.         while (read(file,&c,1)) {
  72.                 if (c == 0x1a) break;                   /* stop if ^Z */
  73.  
  74.                 if (col == 0) {
  75.                         sprintf(hdr,"%d: {%d} (%d) /*%d*/",line,brackets,parens,comments);
  76.                         while (strlen(hdr) < 23)        /* gross but works */
  77.                                 strcat(hdr," ");        /* to hell with elegant, */
  78.                         cprintf("%s|",hdr);             /* we got a job to do !*/
  79.                 }                               /* (real programmers dont ...) */
  80.  
  81. /* Dont count parens and brackets that are inside comments. This of course
  82. assumes that comments are properly matched; in any case, that will be the
  83. first thing to look for. */
  84.  
  85.                 if (comments <= 0) {
  86.                         if (c == '{') ++brackets;
  87.                         if (c == '(') ++parens;
  88.                         if (c == '}') --brackets;
  89.                         if (c == ')') --parens;
  90.                 }
  91.  
  92. /* Now do comments. This properly handles nested comments, whether or
  93. not the compiler does is your responsibility */
  94.  
  95.                 if ((c == '*') && (lastc == '/')) ++comments;
  96.                 if ((c == '/') && (lastc == '*')) --comments;
  97.  
  98.                 ++col;
  99.                 if (c == 0x0a) {                /* newline == New Line */
  100.                         col= 0;                 /* set column 0 */
  101.                         ++line;
  102.                 }
  103.                 putchar(c);                     /* display text */
  104.                 lastc= c;                       /* update last char */
  105.         }
  106.         cprintf("\r\n\r\n");
  107.         if (brackets) cprintf("Unbalanced brackets\r\n");
  108.         if (parens) cprintf("Unbalanced parenthesis\r\n");
  109.         if (comments) cprintf("Unbalanced comments\r\n");
  110. }
  111.