1 /* Copyright (c) 2002 Hewlett-Packard under GPL version 2 or later */ 2 #include <string.h> 3 #ifdef __hpux 4 #include <strings.h> 5 #endif 6 #include <ctype.h> 7 8 /* Returned by gettoken() */ 9 #define T_BASE 256 10 #define T_ASSIGN (T_BASE + 0) 11 #define T_LOGICAL (T_BASE + 1) 12 /* non-commented nuline returned by ncss_Getchar() */ 13 #define T_NCNULINE (T_BASE + 1) 14 15 #define T_WORDS (T_BASE + 20) 16 #define T_IDENT (T_WORDS + 2) 17 #define T_IF (T_WORDS + 3) 18 #define T_WHILE (T_WORDS + 4) 19 #define T_CASE (T_WORDS + 5) 20 #define T_SWITCH (T_WORDS + 6) 21 #define T_FOR (T_WORDS + 7) 22 #define T_UNION (T_WORDS + 8) 23 #define T_STRUCT (T_WORDS + 9) 24 #define T_CLASS (T_WORDS + 10) 25 #define T_OPERATOR (T_WORDS + 11) 26 #define T_CONST (T_WORDS + 12) 27 #define T_NAMESPACE (T_WORDS + 13) 28 29 #define STREQUAL(a, b) (strcmp((a),(b)) == 0) 30 #define ZERO(x) memset(&x, 0, sizeof x) 31 32 #define SHIFT(n) argc -= (n); argv += (n) 33 34 /* values for stats_t.type */ 35 #define STATS_TOTAL 0 36 #define STATS_FILE 1 37 #define STATS_FUNCTION 2 38 #define STATS_CLASS 3 39 #define STATS_NAMESPACE 4 40 41 struct stats_t 42 { 43 char *name; 44 int nstatements; 45 int nfunctions; 46 int firstline; 47 int lastline; 48 int defline; 49 int nLines; 50 int nfor, nwhile, nswitch, ncase, nif; 51 int nand, nor, nq; 52 int nsemicolons; 53 struct stats_t *prev; 54 char type; 55 }; 56 57 typedef struct stats_t stats_t; 58 59 /* can only nest this many names, including Total and file name */ 60 #define MAXDEPTH 100 61 62 extern int Line, Linetokens, ncss_Line; 63 extern int Exit; 64 65 #ifndef TRUE 66 #define TRUE 1 67 #define FALSE 0 68 #endif 69 70 /* pmccabe.c - command-line options */ 71 extern int Cyco; 72 extern int Softbuild; 73 extern int Verbose; 74 extern int Pass1; 75 extern int Totals; 76 extern int Totalsonly; 77 extern int Files; 78 extern int Filesonly; 79 extern int Line; 80 extern int Ncss; 81 extern int Ncssfunction; 82 extern int Unbuf[256]; 83 extern int *Unptr; 84 85 /* cparse.c */ 86 int fancygettoken(char *buf, int classflag, int *line, int *nLine); 87 int toplevelstatement(stats_t *stats); 88 int findchar(char fc); 89 int maybeclass(void); 90 void findsemicolon(); 91 int getoverloadedop(char *buf); 92 int fancyfunction(char *buf, stats_t *fs, stats_t *fn); 93 void possiblefn(stats_t *stats, const char *name, int line1, int defline, int nLine1); 94 int prefunction(int *nstatements); 95 int countfunction(stats_t *fn); 96 void countword(stats_t *fn, int id); 97 98 /* dmain.c */ 99 int decomment(void); 100 int decomment_files(int argc, char *argv []); 101 int ncss_files(int argc, char *argv []); 102 103 /* gettoken.c */ 104 int matchcurly(); 105 int matchparen(void); 106 int skipws(void); 107 int getsimpleident(char *buf); 108 int gettoken(char *buf, int *line, int *nLine); 109 int gettoken2(char *buf, int *line, int *nLine); 110 void operatorident(char *s, int c); 111 int identify(const char *ident); 112 113 void Ungetc(int c); 114 void ncss_Ungetc(int c); 115 116 void Ungets(char *s); 117 void ncss_Ungets(char *s); 118 119 int Getchar(void); 120 int ncss_Getchar(void); 121 122 /* nmain.c */ 123 void file(char *fname, FILE *f); 124 void cycoprintstats(stats_t *fs, stats_t *fn); 125 void softbuildprintstats(stats_t *fs, stats_t *fn); 126 void printstats(stats_t *sp); 127 void fileerror(const char *s); 128 129 /* pmccabe.c */ 130 int main(int argc, char *argv []); 131 stats_t *stats_push(const char *name, int type); 132 stats_t *stats_current(void); 133 stats_t *stats_pop(stats_t *sp); 134 void stats_accumulate(stats_t *sp); 135 136 #define ISSPACE(c) ((c) == T_NCNULINE || (c) == '\n' \ 137 || (c) == '\t' || (c) == ' ') 138 139 #define ISIDENT1(c) (((c) >= 'a' && (c) <= 'z') \ 140 || ((c) >= 'A' && (c) <= 'Z') \ 141 || ((c) == '_')) 142 143 #define ISIDENT(c) ((ISIDENT1(c)) || ((c) >= '0' && (c) <= '9')) 144