home *** CD-ROM | disk | FTP | other *** search
-
- /*
- HEADER: CUG152.15;
- TITLE: A "C" program checker source code file;
- DATE: 09/04/85;
- DESCRIPTION:
- "The source code of a utility to check for matched braces and
- other essential elements by counting the numbers of braces,
- parenthesis, and comments in order to debug 'C' source code."
- KEYWORDS: debug, code, checker;
- FILENAME: CC.C;
- WARNINGS:
- "The typist of this heading does not have equipment to run
- this program and prepared this header from comments
- within the source code or companion files."
- The authors claim copyrights and authorize non-commercial
- use only."
- AUTHORS: T. Jennings
- David N. Smith;
- COMPILERS: CI/C86;
- REFERENCES:
- AUTHERS: " ";
- TITLE: " ";
- CITATION: " ";
- ENDREF
- */
-
- /****************************************************************
- * *
- * CC (C Checker) *
- * *
- * C Source Paren, bracket and comment Checker *
- * *
- * T. Jennings -- Sometime in 1983 *
- * *
- * *
- ****************************************************************/
-
- #include <stdio.h>
- #include <ctype.h>
-
- /* Very crude but very effective C source debugger. Counts the numbers of
- matching braces, parenthesis and comments, and displays them at the left edge
- of the screen. The best way to see what it does is to do it; try
-
- CC CC.C C Check CC.C
-
-
- Properly handles parens and brackets inside comments; they are ignored.
- */
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- int file;
- char c,lastc;
- int parens,brackets,comments;
- int line, col;
- char hdr[40];
-
- file= open(argv[1],0x8000);
- if (file == -1) {
- cprintf("File missing. Try CC <filename.ext> \r\n");
- exit();
- }
- brackets= parens= comments= 0;
- line= 0; col= 0;
- lastc= '\0';
-
- while (read(file,&c,1)) {
- if (c == 0x1a) break; /* stop if ^Z */
-
- if (col == 0) {
- sprintf(hdr,"%d: {%d} (%d) /*%d*/",line,brackets,parens,comments);
- while (strlen(hdr) < 23) /* gross but works */
- strcat(hdr," "); /* to hell with elegant, */
- cprintf("%s|",hdr); /* we got a job to do !*/
- } /* (real programmers dont ...) */
-
- /* Dont count parens and brackets that are inside comments. This of course
- assumes that comments are properly matched; in any case, that will be the
- first thing to look for. */
-
- if (comments <= 0) {
- if (c == '{') ++brackets;
- if (c == '(') ++parens;
- if (c == '}') --brackets;
- if (c == ')') --parens;
- }
-
- /* Now do comments. This properly handles nested comments, whether or
- not the compiler does is your responsibility */
-
- if ((c == '*') && (lastc == '/')) ++comments;
- if ((c == '/') && (lastc == '*')) --comments;
-
- ++col;
- if (c == 0x0a) { /* newline == New Line */
- col= 0; /* set column 0 */
- ++line;
- }
- putchar(c); /* display text */
- lastc= c; /* update last char */
- }
- cprintf("\r\n\r\n");
- if (brackets) cprintf("Unbalanced brackets\r\n");
- if (parens) cprintf("Unbalanced parenthesis\r\n");
- if (comments) cprintf("Unbalanced comments\r\n");
- }