home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 6
/
FreshFish_September1994.bin
/
new
/
dev
/
c
/
hce
/
examples
/
clib
/
ccheck
/
ccheck.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-02
|
23KB
|
900 lines
/*
* CCHECK.C -- Amiga Lattice C version 4/8/86
*
*
* Copyright: The Regents of the University of California
* [Note - since Steve Draper distributed cchk.c over
* Usenet, I assume it's effectively in the public
* domain. JCM
*
* Title: ccheck
*
* Purpose: To find and report all badly matched openers and
* closers plus assignment/equality confusions
* in a c source file.
*
*
* Author: Steve Draper, expanding cnest by Tom Anderson
*
* Usage: ccheck [-q] [-v] <filename1> <filename2> ...
*
* History (in reverse order to minimize reading time for updates):
*
* June 13, 1994 Jason Petty
* - Must get valid file name else exits with usage.
* Changes marked VANSOFT.
*
*
* April 8, 1986 Converted to Amiga Lattice C from from butchered text
* file. If any one can supply me with the original source
* I would appreciate it. IF-ELSE check seems flaky.
*
* Thom Althoff (BIX althoff)
* c/o American Broadcasting Companies
* 30 W. 67th St Video Tape Tech Support
* Floor B1
* New York City, N.Y. 10023
*
*
* June 18, 1985 Converted to Aztec C - removed BDS C code - Rick Moore
*
* January 9, 1983 Single BDS/UNIX source created
* as CCHECK.C -- Jeff Martin
*
* December 29, 1982 Adapted for BDS C --
* Jeff Martin at home.
*
* December 20, 1982 Converted to cchk --
* Steve Draper at UCSD
*
* December 9, 1982 Jeffrey Mogul at Stanford
* - checks for unclosed comment at end of file.
*
* December 3, 1982 creation date --
* Tom Anderson at microsof!fluke
*
*/
#include <stdio.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
#define SPACE 32
#define BRACE 1
#define SQBRAK 2
#define PAREN 3
#define IF 4
#define IFCOND 5
#define WHLCOND 6
#define THEN 7
#define ELSE 8
#define STACKSIZ 64
struct brak
{
int type, b_indent, b_ln;
} stack[STACKSIZ];
#define rmbrak(N) (top -= N, stackc -= N)
#define myungetc(C) ungetc(((C) == '\n' ? SPACE : (C)), infile)
#define VFLAG "-v"
#define QFLAG "-q"
#define SFLAG "-s"
int firsttime; /* This was a static in mygetchar() */
int mygetchar(), pr();
void checkelse(), newbrak(), checkcloser(), prtype();
void usage();
FILE *infile;
int ln, indent, commindent, stackc, commln;
int singlequoterr, oddsinglequote, bracecnt, parencnt, sqbrakcnt;
int errstatus, wstatus;
int errnmb, wnmb;
int verbose;
char *filename;
struct brak *top;
void usage(op) /* Added, VANSOFT. */
int op;
{
printf(
"\n\n\nCCHECK: Copyright - The Regents of the University of California.\n");
printf("Author: Steve Draper.\n\n");
printf("Purpose: To find and report all badly matched openers and\n");
printf(
"closers plus assignment/equality confusions in a c source file.\n\n");
printf("USAGE: CCHECK [-q] [-v] <filename1> <filename2> ...\n\n");
printf("Flags:\n");
printf("\t-q - Suppress warning messages.\n");
printf("\t-v - Verbose.\n\n\n");
if(!op)
exit(10); /* Exit error. */
}
main(argc, argv)
unsigned argc ;
char *argv[] ;
{
register int c;
int i;
int doubleqflag;
unsigned file_index;
wnmb = 0;
verbose = 0;
file_index = 1;
while (argc > 1 && argv[file_index][0] == '-')
{
if (strcmp(argv[file_index], VFLAG) == 0)
verbose++;
if (strcmp(argv[file_index], QFLAG) == 0)
wnmb = -2;
if (strcmp(argv[file_index], SFLAG) == 0)
wnmb = -2;
file_index++;
argc--;
}
do
{
/* INIT for each file */
firsttime = 1;
doubleqflag = 0;
errstatus = wstatus = 0;
ln = 1;
indent = 0;
commindent = 0;
singlequoterr = oddsinglequote = parencnt = sqbrakcnt = bracecnt = 0;
errnmb = 0;
if (wnmb > -2)
wnmb = 0;
newbrak(0);
if (argc == 1 || argc == 0)
{
usage(NULL); /* usage. Added, VANSOFT. */
}
else
{
if ((infile = fopen(argv[file_index], "r")) == (FILE * ) NULL)
{
usage(1); /* 1 = don't 'exit()' yet. (usage. Added,VANSOFT) */
fprintf(stdout, "%s: Can't access %s!\n\n",argv[0],argv[file_index]);
exit(10); /* exit error. */
}
filename = argv[file_index];
}
while ( ( c = mygetchar()) != EOF )
{
if (verbose == 2)
{
for (i = stackc; i > 0; i--)
{
printf("%c %d: type ", c, i);
prtype(stack[i].type);
printf(", indent %d, line %d.\n",stack[i].b_indent,stack[i].b_ln);
}
}
switch (c)
{
case ';':
ungetc(SPACE, infile);
while (top->type == ELSE)
rmbrak(1);
if (top->type == THEN)
{
rmbrak(1);
checkelse();
}
break;
case '!':
case '>':
case '<':
/* swallow legit. '=' chars */
c = mygetchar();
if (c != '=')
myungetc(c);
break;
case '=':
if ((top - 1)->type == IFCOND || (top - 1)->type == WHLCOND)
{
c = mygetchar();
if (c != '=')
{
myungetc(c);
if(pr(1))
printf("Assignment instead of equals in conditional,%d\n", ln);
}
}
break;
case '\n':
case SPACE:
c = mygetchar();
switch (c) {
case 'i':
/* if */
c = mygetchar();
if (c == 'f' && !isalpha(c = fgetc(infile)) && !isdigit(c))
{
ungetc(c, infile);
newbrak(IF);
while ((c = mygetchar()) == SPACE || c == '\n');
if (c != '(')
{
if (pr(1))
printf("Bad if (no condition) line %d.\n",ln);
rmbrak(1);
}
else
newbrak(IFCOND);
myungetc(c);
}
else
myungetc(c);
break;
case 'w':
/* while */
if ((c = mygetchar()) == 'h'
&& (c = mygetchar()) == 'i'
&& (c = mygetchar()) == 'l'
&& (c = mygetchar()) == 'e'
&& !isalpha(c = fgetc(infile)) && !isdigit(c))
{
ungetc(c, infile);
while ((c = mygetchar()) == SPACE || c == '\n');
if (c != '(')
{
if(pr(1))
printf("Bad while (no condition) line %d.\n",ln);
}
else
newbrak(WHLCOND);
myungetc(c);
}
else
myungetc(c);
break;
case 'e':
/* else */
myungetc(c);
checkelse();
break;
default:
myungetc(c);
break;
}
break;
case '*':
/* close comment ? */
c = mygetchar();
if (c != '/')
{
myungetc(c);
break;
}
if(pr(1))
printf("