How to find C programming functions using shell script?

I want to find c function definition with pattern with shell script by checking condition for each line:

data_type
functionname(param_list){
  ....
}

I knew cscope or ctag is usable for this task, but if there any ways to do without using them.

I am thinking of checking line condition like this

if  [[ $line == "functionname("* ]] && [[ $line == *"){" ]]; then
    echo "found"
else
   echo "not found"
fi

I knew it is not good.
What I want in condition check is something like

if [[ $line == "functionname(.*){" ]]; then

Output should be just echo yes if found or no if not found.

In general what you want to do cannot be done without actually writing a parser that understands all of the complexities of the C language. The single line that you are looking for:

functionname(param_list){

could appear anywhere within a line of C source code (not necessarily starting in position 1 nor ending at the end of a line and could include the return code type and the body of the function in addition to what you have shown, or could be spread across hundreds of lines of C source code before it gets to the body of the function definition.

Even if the group writing the kernel, utility, library, or single function you're looking for has strict function coding rules, there are occasionally exceptions that will be granted for various reasons for various functions.

1 Like

What in your other thread could not be adapted to satisfy your current need?

As a comment: many editors in development environments (IDE's) have that feature built in: list all functions,. Examples:
UltraEdit,
Linux geany editor.

So you may be trying to reinvent something you already have.

1 Like