I'm trying to make "a script" - essentially an awk command - to extract the prototypes of functions of C code in a .c file to generate automatically a header .h. I'm new with awk so I don't get all the details.
This is a sample of the source .c :
dict_t dictup(dict_t d, const char * key, const char * newval)
{
int i = dictlook(d, key);
if (i == DICT_NOT_FOUND) {
fprintf(stderr, "key \"%s\" doesn't exist.\n", key);
dictdump(d);
}
else {
strncpy(d.entry[i].val, newval, DICTENT_VALLENGTH);
}
return d;
}
dict_t* dictrm(dict_t* d, const char * key) {
int i = dictlook(d, key);
if (i == DICT_NOT_FOUND) {
fprintf(stderr, "key \"%s\" doesn't exist.\n", key);
dictdump(d);
}
else {
d->entry[i] = d->entry[--d.size];
}
if ( ((float)d->size)/d.maxsise < 0.25 ) {
d->maxsize /= 2;
d->entry = realloc(d->entry, d->maxsize*sizeof(dictent_t*));
}
return d;
}
And what I want to generate :
dict_t dictup(dict_t d, const char * key, const char *newval);
dict_t* dictrm(dict_t* d, const char * key);
My command with the full regex looks like this :
awk '/^[a-zA-Z*_]+[:space:]+[a-zA-Z*_]+[:space:]*\(.*?\)/{ print $0 }' dict3.c
But I don't get nothing with it. So I've tried to squeeze it just to see if I can come with something. I've tried this :
awk '/^[a-zA-Z*_]+[:space:]+[a-zA-Z*_]+/{ print $0 }' dict3.c
And I get that :
dictent_t* dictentcreate(const char * key, const char * val)
dict_t* dictcreate()
dict_t* dictadd(dict_t* d, const char * key, const char * val)
dict_t dictup(dict_t d, const char * key, const char * newval)
dict_t* dictrm(dict_t* d, const char * key) {
And it's source of lots of wonder !
- Why doesn't the first regex work?
- And why the second has catched some of the declarations, but not all? I assure you that there is no space before any declaration. I guess it didn't catch other part of the code like variables declarations because of the indentation.
- Third question, why has it catched all the line where I just need the expression?
- Last one, how can I add the
;
at the end of each regex?