[net.sources] Revised C Comment Checker

toma (12/10/82)

	Thanks to Jeffrey Mogul at Stanford for finding a bug in cnest
(c comment nesting checker, submitted a week ago to the net) - it now
detects comments that eat files clear to the end.  He also wrote a 
short manual page, which is included.  cnest has also been changed
to report the line number of the first '/*' in unclosed comment fields.

				Tom Anderson
				John Fluke Mfg.
				Ever-rot, Wa.

/*	Title:		cnest

	Purpose:	To find and report all nested or unclosed comments
			in a c source file.

	Author:		Tom Anderson

	Usage:		cnest <filename1> <filename2> ...

	History:	December 3, 1982	creation date
			December 9, 1982	Jeffrey Mogul at Stanford
			- checks for unclosed comment at end of file.
			December 10, 1982	Tom Anderson
			- revised to report the line number of the first
				begin comment field in unclosed comments
*/

#include <stdio.h>

#define TRUE 1
#define FALSE 0

main(argc,argv)
unsigned int argc ;
char *argv[] ;

{
    char c ;
    char prev_char;
    unsigned char in_comment;
    unsigned int line_number;
    unsigned int start_comment_line;
    unsigned int file_index;
    char status;
    FILE *chk_file;

    status = 0;
    file_index = 1;

    do {
	line_number = 1;
	in_comment = FALSE;
	prev_char = ' ';
	if (argc == 1)
	    chk_file = stdin;
	else
	{
	    if ((chk_file = fopen(argv[file_index],"r")) == (FILE *) NULL)
	    {
		fprintf(stderr,"%s: Can't access %s\n",argv[0],
		    argv[file_index]);
		status = 2;
		continue;
	    }
	}

	while ( ( c = fgetc(chk_file) ) !=  EOF )
	{
	    switch (c)
	    {
		case '\n':
		    line_number++;
		    break;
		case '/':
		    if (prev_char == '*')
		    {
			if (! in_comment) {
			    if (argc >= 2)
				fprintf(stderr,
				"%s: Line %d: Comment close without begin\n",
				argv[file_index],line_number);
			    else
				fprintf(stderr,
				"Line %d: Comment close without begin\n",
				line_number);
			    status = 1;
			}
			in_comment = FALSE;
		    }
		    break;
		case '*':
		    if (prev_char == '/')
		    {
			if (in_comment) {
			    if (argc >= 2)
				fprintf(stderr,
				"%s: Line %d: Unclosed comment begin\n",
				argv[file_index],start_comment_line);
			    else
				fprintf(stderr,
				"Line %d: Unclosed comment begin\n",
				start_comment_line);
			    status = 1;
			}
			in_comment = TRUE;
			start_comment_line = line_number;
		    }
		    break;
		default:
		    break;
	    }
	    prev_char = c;
	}
	if (in_comment) {	/* last comment never ended */
	    if (argc >=2)
		fprintf(stderr,
		"%s: Line %d: Unclosed comment begin\n",
		argv[file_index],start_comment_line);
	    else
		fprintf(stderr,
		"Line %d: Unclosed comment begin\n",
		start_comment_line);
	    status = 1;
	}
	fclose(chk_file);
    } while (++file_index < argc);
    exit (status);
}

>From microsof!decvax!ucbvax!mogul%Shasta@SU-Score Thu Dec  9 23:47:06 1982
Date: Thursday,  9 Dec 1982 11:44-PST
From: Jeff Mogul <ucbvax!mogul%Shasta@SU-Score>
Subject: Re: C Comment Checker


I also made up a short manual page:
(cnest.1:)
.TH CNEST I local
.SH NAME
cnest \- check for nested comments in C code
.SH SYNOPSIS
.B cnest [
files ...
.B ]
.SH DESCRIPTION
.I Cnest
checks for nested comments in C code, since these are not supported
by the C compiler and invariably cause obscure problems.
It complains about successive "begin comment" fields (/*), end
comment fields that do not follow a begin comment field, or
comment fields that are not closed before the end of the file.  If no input
filenames are specified, it reads from the standard input.
.SH SEE ALSO
cc(1)
.SH DIAGNOSTICS
If the program has no nested comments, no diagnostics will be printed.
Otherwise, you will see self-explanatory messages giving the line number
at which an error was detected.
.SH BUGS
Does not understand about C pre-processor commands, and so can get
confused by stuff that is #ifdef'ed out.  To get around this, you
can use the pipeline
.nf
	cc -E -C filename.c | cnest
.fi
to have the C pre-processor commands executed but without removing
the remaining comments.
.SH AUTHOR
Tom Anderson, John Fluke Mfg. Co., Inc.