How to list all functions in a C files using Perl

Discussion in 'Perl Misc' started by Uday, Jul 20, 2005.

  1. Uday

    Uday Guest

    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
     
    Uday, Jul 20, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,607
    Victor Bazarov
    Jun 21, 2004
  2. Fernando Rodriguez
    Replies:
    3
    Views:
    324
    David Eppstein
    Nov 21, 2003
  3. Ross A. Finlayson
    Replies:
    19
    Views:
    603
    Keith Thompson
    Mar 10, 2005
  4. Uday

    List of all functions in a C files

    Uday, Jul 20, 2005, in forum: C Programming
    Replies:
    15
    Views:
    1,273
    Richard Bos
    Jul 25, 2005
  5. Anders Eriksson

    List all files using FTP

    Anders Eriksson, Mar 6, 2008, in forum: Python
    Replies:
    4
    Views:
    301
    Giampaolo Rodola'
    Apr 30, 2008
Loading...

Share This Page