/****************************************************************
*								*
*		         CC (C CHECKER) Ver. 1.10		*
*								*
*		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 number
of matching braces, parentheses, 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.

02/05/84 - Ver. 1.10  Corrected bug.  When executed without an argument,
	   CC now prompts for correct syntax and gracefully exits to DOS.
	   Added sign-on.  Recompiled with Lattice Ver. 2.0 C Compiler.
					Harvey G. Lord
*/

main(argc,argv)
int argc;
char **argv;
{
int file;
char c, lastc;
int parens, brackets, comments;
int line, col;
char hdr[40];

	printf("CC Ver. 1.10 by T. Jennings  (MS DOS 2.xx)\n");
	if(argc != 2)
	{
		printf("Argument missing.  Execute CC <filename.ext>\n\n");
		exit();
	}
	file = open(argv[1],0x8000);
	if(file == -1)
	{
		printf("File missing.  Try CC <filename.ext>  \n\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 */
			printf("%s|",hdr);
		}
/* Don't 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)
	{
		col = 0;	/* newline == New Line; set column 0 */
		++line;
	}
	putchar(c);		/* display text			*/
	lastc = c;		/* Update last char		*/
	}
	printf("\n\n\n\n");
	if(brackets)
		printf("Unbalanced brackets\n\n");
	if(parens)
		printf("Unbalanced parentheses\n\n");
	if(comments)
		printf("Unbalanced comments\n\n");
}

