Awk to split c functions into multiple files.

This, at present, is overly simplistic and makes many stupid assumptions:

  • That the return type is short.
  • That there are no short global variables declared.
  • That everything is well-formatted so that the return type is always first column, and that there is a space between both function name and return type (as in, only spaces--no comments) and function name and open parenthesis of the arg list.


cat $1 | awk '
BEGIN {
functionNameFile="";
}
/^short/ {
/* assumes return type of short */
/* assumes space between return type, function name, and parm list */
functionNameFile=$2.".c";
print $0 > functionNameFile;
}
/^}/ {
print $0 >> functionNameFile;
functionNameFile="";
next;
}
{
if(functionNameFile != "") {
print $0 >> functionNameFile;
}
}
'