c-hdr-parser: add pwd include; delete garbage code; fix static function parse bug

This commit is contained in:
icst 2024-07-07 19:04:37 -04:00
parent a78935e012
commit 159c172082

View file

@ -27,7 +27,7 @@ FILE *open_c_header(const char *cc_cmd, const char *hdr_fn) {
if ( cmd == NULL ) return NULL;
cmd = cstr_append(cmd, cc_cmd);
if ( cmd == NULL ) return NULL;
cmd = cstr_append(cmd, " -E -");
cmd = cstr_append(cmd, " -I . -E -");
if ( cmd == NULL ) return NULL;
FILE *f = popen(cmd, "r");
@ -92,115 +92,6 @@ char ** parse_c_header_identkws(size_t *nidkws, int *c, FILE *f) {
return idkws;
}
#if 0
int64_t parse_c_header_int(int *c, FILE *f) {
size_t n=0;
char buf[32];
while ( (*c = fgetc(f)) != EOF ) {
if ( n >= sizeof(buf) ) return INT64_MAX;
if ( !isdigit(*c) ) break;
buf[n] = *c;
n++;
}
buf[n] = 0;
return strtol(buf, NULL, 0);
}
typedef struct {
size_t n;
char ** strs;
} arg_t;
arg_t * parse_c_header_args(size_t *nargs, FILE *f) {
*nargs = 0;
arg_t *args = NULL;
int c;
while ( (c = fgetc(f)) != EOF ) {
// consume anything that starts with '#' until EOF
if ( c == '#' ) {
while ( (c = fgetc(f)) != EOF && c != '\n' );
if ( c == EOF ) break;
}
ungetc(c, f);
int idkw_end;
size_t nidkws = 0;
char **idkws = parse_c_header_identkws(&nidkws, &idkw_end, f);
switch (idkw_end) {
default:
fprintf(stdout, "Unhandled '%c'\n", idkw_end);
goto ERROR;
case ')': break;
case ',': break;
case '*': break;
case '[':
printf("[%ld]\n", parse_c_header_int(&idkw_end, f));
break;
}
if ( nidkws > 0 ) {
args = realloc(args, (*nargs + 1) * sizeof(arg_t));
args[*nargs].n = nidkws;
args[*nargs].strs = idkws;
(*nargs)++;
}
}
EXIT:
return args;
ERROR:
*nargs = 0;
return NULL;
}
typedef struct stmt_s {
enum stmt_type {
STMT_TYPE_TYPE,
STMT_TYPE_VAR,
STMT_TYPE_FUNC,
} type;
size_t n;
char ** strs;
} stmt_t;
stmt_t parse_c_header_stmt(FILE *f) {
stmt_t stmt = { .n = 0, .strs = 0 };
int c;
while ( (c = fgetc(f)) != EOF ) {
// consume anything that starts with '#' until EOF
if ( c == '#' ) {
while ( (c = fgetc(f)) != EOF && c != '\n' );
if ( c == EOF ) break;
}
int idkw_end;
size_t nidkws = 0;
char **idkws = parse_c_header_identkws(&nidkws, &idkw_end, f);
switch (idkw_end) {
default:
break;
case '(':
case ')':
}
}
}
#endif
char * parse_c_header_decl_body(int *c, size_t *len, FILE *f) {
*len = 0;
@ -610,7 +501,13 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
}
// consume remainder of function prototype definition
while ( (c = fgetc(f)) != EOF && c != ';' );
while ( (c = fgetc(f)) != EOF && c != ';' && c != '{' );
if ( c == '{' ) {
// need to consume static function bodies here instead to avoid a weird bug
size_t body_len = 0;
char * body = parse_c_header_decl_body(&c, &body_len, f);
free( body );
}
if ( c == EOF ) break;
}
@ -677,105 +574,5 @@ int main(int argc, char *argv[]) {
puts(decl[n]);
}
#if 0
enum chp_state {
CHP_STATE_KEYWORD,
CHP_STATE_FUNC_ARGS,
} state = CHP_STATE_KEYWORD;
// TODO we may need to handle "" and '' containing '{' or '}'
size_t cb_level = 0;
int c;
while ( (c = fgetc(fhdr)) != EOF ) {
// consume anything that starts with '#' until EOF
if ( c == '#' ) {
while ( (c = fgetc(fhdr)) != EOF && c != '\n' );
if ( c == EOF ) break;
}
if ( c == '{' ) { cb_level++; }
if ( c == '}' ) {
cb_level--;
continue;
}
// basically just skip anything in curly braces
if ( cb_level > 0 ) continue;
//if ( isspace(c) ) continue;
//putchar(c);
if ( c == '_' || isalnum(c) ) {
// need to put the char back before call to avoid loosing the first char
ungetc(c, fhdr);
int idkw_end;
size_t nidkws = 0;
char **idkws = parse_c_header_identkws(&nidkws, &idkw_end, fhdr);
for (size_t n=0; n < nidkws; n++) puts(idkws[n]);
switch (idkw_end) {
default:
printf("!!! UNHANDLED '%c'\n", idkw_end);
break;
case '#':
case '{':
ungetc(idkw_end, fhdr);
break;
case ';': break;
case ',': break;
case '(':
/*
while ( idkw_end != ')' ) {
char **idkws = parse_c_header_identkws(&nidkws, &idkw_end, fhdr);
for (size_t n=0; n < nidkws; n++) {
putchar(' ');
fputs(idkws[n], stdout);
}
if ( idkw_end == '[' ) printf("[%ld]", parse_c_header_int(&idkw_end, fhdr));
if ( nidkws == 0 ) printf("!!! '%c'", idkw_end);
if ( idkw_end != '*' ) putchar('\n'); else fputs(" *", stdout);
}
*/
{
size_t nargs;
arg_t *args = parse_c_header_args(&nargs, fhdr);
for (size_t n=0; n < nargs; n++) {
puts("***");
for (size_t nn=0; nn < args[n].n; nn++) {
printf(" %s", args[n].strs[nn]);
}
}
}
// consume the rest
while ( (c = fgetc(fhdr)) != ';' );
state = CHP_STATE_FUNC_ARGS;
break;
case ')':
state = CHP_STATE_KEYWORD;
break;
case '[':
printf("[%ld]\n", parse_c_header_int(&idkw_end, fhdr));
break;
}
printf("----------> '%c'\n", idkw_end);
}
//if ( c == ';' ) putchar('\n');
}
#endif
return 0;
}