static boolean tail (const char *cp) { boolean result = FALSE; register int len = 0; while (*cp != '\0' && tolower ((int) *cp) == tolower ((int) dbp [len])) cp++, len++; if (*cp == '\0' && !intoken (dbp [len])) { dbp += len; result = TRUE; } return result; }
int PF_funcs(void) { bool pfcnt; /* pascal/fortran functions found */ char *cp; char tok[MAXTOKEN]; for (pfcnt = NO;;) { lineftell = ftell(inf); if (!fgets(lbuf, sizeof(lbuf), inf)) return (pfcnt); ++lineno; lbp = lbuf; if (*lbp == '%') /* Ratfor escape to fortran */ ++lbp; for (; isspace(*lbp); ++lbp) continue; if (!*lbp) continue; switch (*lbp | ' ') { /* convert to lower-case */ case 'c': if (cicmp("complex") || cicmp("character")) takeprec(); break; case 'd': if (cicmp("double")) { for (; isspace(*lbp); ++lbp) continue; if (!*lbp) continue; if (cicmp("precision")) break; continue; } break; case 'i': if (cicmp("integer")) takeprec(); break; case 'l': if (cicmp("logical")) takeprec(); break; case 'r': if (cicmp("real")) takeprec(); break; } for (; isspace(*lbp); ++lbp) continue; if (!*lbp) continue; switch (*lbp | ' ') { case 'f': if (cicmp("function")) break; continue; case 'p': if (cicmp("program") || cicmp("procedure")) break; continue; case 's': if (cicmp("subroutine")) break; default: continue; } for (; isspace(*lbp); ++lbp) continue; if (!*lbp) continue; for (cp = lbp + 1; *cp && intoken(*cp); ++cp) continue; if (cp == lbp + 1) continue; *cp = EOS; (void)strlcpy(tok, lbp, sizeof(tok)); /* possible trunc */ getline(); /* process line for ex(1) */ pfnote(tok, lineno); pfcnt = YES; } /*NOTREACHED*/ }
static void parseArglist(const char *buf, char **arglist, char **vartype) { char *c, *start, *end; int level; if (NULL == buf || NULL == arglist) return; c = strdup(buf); /* parse argument list which can be missing like in "function ginit:integer;" */ if (NULL != (start = strchr(c, '('))) { for (level = 1, end = start + 1; level > 0; ++end) { if ('\0' == *end) break; else if ('(' == *end) ++ level; else if (')' == *end) -- level; } } else /* if no argument list was found, continue looking for a return value */ { start = "()"; end = c; } /* parse return type if requested by passing a non-NULL vartype argument */ if (NULL != vartype) { char *var, *var_start; *vartype = NULL; if (NULL != (var = strchr(end, ':'))) { var++; /* skip ':' */ while (isspace((int) *var)) ++var; if (starttoken(*var)) { var_start = var; var++; while (intoken(*var)) var++; if (endtoken(*var)) { *var = '\0'; *vartype = strdup(var_start); } } } } *end = '\0'; *arglist = strdup(start); eFree(c); }
/* * c_entries -- * read .c and .h files and call appropriate routines */ void c_entries(void) { int c; /* current character */ int level; /* brace level */ int token; /* if reading a token */ int t_def; /* if reading a typedef */ int t_level; /* typedef's brace level */ char *sp; /* buffer pointer */ char tok[MAXTOKEN]; /* token buffer */ lineftell = ftell(inf); sp = tok; token = t_def = NO; t_level = -1; level = 0; lineno = 1; while (GETC(!=, EOF)) { switch (c) { /* * Here's where it DOESN'T handle: { * foo(a) * { * #ifdef notdef * } * #endif * if (a) * puts("hello, world"); * } */ case '{': ++level; goto endtok; case '}': /* * if level goes below zero, try and fix * it, even though we've already messed up */ if (--level < 0) level = 0; goto endtok; case '\n': SETLINE; /* * the above 3 cases are similar in that they * are special characters that also end tokens. */ endtok: if (sp > tok) { *sp = EOS; token = YES; sp = tok; } else token = NO; continue; /* * We ignore quoted strings and character constants * completely. */ case '"': case '\'': skip_string(c); break; /* * comments can be fun; note the state is unchanged after * return, in case we found: * "foo() XX comment XX { int bar; }" */ case '/': if (GETC(==, '*') || c == '/') { skip_comment(c); continue; } (void)ungetc(c, inf); c = '/'; goto storec; /* hash marks flag #define's. */ case '#': if (sp == tok) { hash_entry(); break; } goto storec; /* * if we have a current token, parenthesis on * level zero indicates a function. */ case '(': if (!level && token) { int curline; if (sp != tok) *sp = EOS; /* * grab the line immediately, we may * already be wrong, for example, * foo\n * (arg1, */ getline(); curline = lineno; if (func_entry()) { ++level; pfnote(tok, curline); } break; } goto storec; /* * semi-colons indicate the end of a typedef; if we find a * typedef we search for the next semi-colon of the same * level as the typedef. Ignoring "structs", they are * tricky, since you can find: * * "typedef long time_t;" * "typedef unsigned int u_int;" * "typedef unsigned int u_int [10];" * * If looking at a typedef, we save a copy of the last token * found. Then, when we find the ';' we take the current * token if it starts with a valid token name, else we take * the one we saved. There's probably some reasonable * alternative to this... */ case ';': if (t_def && level == t_level) { t_def = NO; getline(); if (sp != tok) *sp = EOS; pfnote(tok, lineno); break; } goto storec; /* * store characters until one that can't be part of a token * comes along; check the current token against certain * reserved words. */ default: /* ignore whitespace */ if (c == ' ' || c == '\t') { int save = c; while (GETC(!=, EOF) && (c == ' ' || c == '\t')) ; if (c == EOF) return; (void)ungetc(c, inf); c = save; } storec: if (!intoken(c)) { if (sp == tok) break; *sp = EOS; if (tflag) { /* no typedefs inside typedefs */ if (!t_def && !memcmp(tok, "typedef",8)) { t_def = YES; t_level = level; break; } /* catch "typedef struct" */ if ((!t_def || t_level < level) && (!memcmp(tok, "struct", 7) || !memcmp(tok, "union", 6) || !memcmp(tok, "enum", 5))) { /* * get line immediately; * may change before '{' */ getline(); if (str_entry(c)) ++level; break; /* } */ } } sp = tok; } else if (sp != tok || begtoken(c)) { if (sp == tok + sizeof tok - 1) /* Too long -- truncate it */ *sp = EOS; else *sp++ = c; token = YES; } continue; } sp = tok; token = NO; }