How to list all functions in a C files using Perl

U

Uday

Hello All,

I've to rename all the functions in a C files. How to do that using Perl
script? Whatever I've done below I can put it in perl script, but for
some case it is not working...see below.

I tried to do it in a crude way (I'm not good in unit commands and shell
scripting), where I search for '{' at the beginning of the lines and
take the above line etc:

*grep* -i -B 1 "^{" cfile.c | *grep* -v "{" | *grep* -v "\-\-" | *grep*
-i "[a-z_]*[ ]*(" | *sed* -e 's/(.*$//' -e 's/^.* //' | *awk* '{print
"s/\\\<" $1 "\\\>/renamed_"$1"/"}'

1. fine { at first char get preceding line
2. Get rid of '{'
3. Get rid of '--'
4. Get lines with '[a-z_]*[ ]*(' -- funciton
5. remove blank lines
6. generate sed script. For example: s/\<function1\>/renamed_function1/

Given cfile.c:

function1 (int var1)
{
....code...
}
function2 (var2)
int var2;
{
....code...
}
function3 (int var3) {
....code...
}


Above command will work correctly for the function1, but for function2
since I search for '{' then pickup above line. Not sure how to make it
work for function3. At least in our files we always put '{' as first
character below function.


There would be some easier way to do it. How to make perl script to make
it work for the above 3 cases?


Appreciate your help.


thanks,
Uday
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top