Perl script to find where functions is called in c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to find where functions is called in c
# 1  
Old 12-27-2010
Perl script to find where functions is called in c

Hello,

I need to write a perl script to find where functions is called in c files..

The script should scan the file and find out the function names and then search to see where they are called...

Lets for now assume all functions are internal.

I don't know where to start Smilie

---------- Post updated at 06:10 AM ---------- Previous update was at 05:25 AM ----------

okay.. I managed to do it using this:
Code:
open FILE, "<test.h" or die $!;


while(<FILE>){
if(/static void ([^ \n\(]+)/)
{

print "function: $1\n";

$str=$1;
open FILE2, "<test.c" or die $!;
while(<FILE2>){
        if(!$sw && /(.+)\(.*\)/){
                $func_name=$1;
                $func_start=$.;
                $sw=1;
                next;
        }
        $c++ if $sw && /\{/;
        $c-- if $sw && /\}/; 
        if(/$str/){
                $print=1;
        }
        if($sw && $c==0){
                if($print){
                        
                        print "Caller function: $func_name\n";
                        $print=0;
                }
                $sw=0;
        }
}
close FILE2;


print "\n";
}
}

Now the problem is, I do not want to print the result, I want to write it in a certain part of the file.. for example, the h file has the format:
Code:
/* Function -------------------------------------------- 
* function name: function1
* caller: 
*
------------------------------------------------------------*/
static void function1(..);

I want the result to be written infront of the "caller: " field.
# 2  
Old 12-27-2010
Well, I use cscope, which is free. You need a state engine to parse C states, but you are only interested in locating calls and subroutines. You have to handle every C meta-character and expression correctly, so you know when you are in comments and quotations. Just assign the states numbers in an int or enum as you discover them. Beware advanced things like "XXX"\n"YYY" ( = "XXXYYY"), and \ at end of line! When you find a variable name, you need to decide if it is a call or a subroutine declaration or a non-subroutine declaration like int i. You have to worry about type names in typedefs and #defines, possibly in includes, so you must preprocess with cpp or cc -E and process the structured comments in that output that show where include file lines are. So easy, almost like writing a compiler!
# 3  
Old 01-13-2011
okay, I solved this but faced another problem..
It works great if the function declaration is on a single line:
Code:
void function1(arg1, arg2)

but doesn't work if its multiline:
Code:
void function1(arg1, 
                                    arg2, arg3)

# 4  
Old 01-13-2011
You could something like this:

Code:
# cat input
void function1(arg1, 
   arg33,
   arg2,arg3)

Code:
#!/usr/bin/perl

open FILE, "<".shift or die $!;

while (<FILE>) {
   chomp;
   if(/void ([^ \(])+/ || $f)
     {
     $f=1;
     $cF.=$_ ;
     last if ( /\)\s*$/ );
     }
   }

print "cF:".$cF."\n";

# 5  
Old 01-13-2011
I usually deal with such parsing tasks with a little state machine that normlizes the C into one string block with all white space down to one or zero spaces as allowed. The state machine parses bytes until the end of declaration, knowing when it is in comments and in quotes, and the depth of any (){}[] has cancelled down to 0. The block might get very big, so it might be good to abstract out whatever is in {}, as you must reparse inside {} to see what is declared there. The code needs to be preprocessed so macros are expanded, and the parser needs to keep track of the files and line numbers it is in via include: # line_number "file_name"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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... (3 Replies)
Discussion started by: cmdcmd
3 Replies

2. Shell Programming and Scripting

Mkdir fails when defined and called with functions.

In the below script i found that the alias mkdir_s is getting invoked from function configure() i.e the alias is kicking in. #!/bin/bash -e shopt -s expand_aliases alias mkdir=mkdir_s mkdir_s(){ if ]; then return else /usr/bin/mkdir "$1" return fi } configure() { mkdir -p... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. Programming

Perl: How to load some functions automaticaly, starting Perl inteructively (with -d -e 0)?

I would like to use Perl in 'interactive' mode (kind off), starting it by > perl -d -e 0; But I need to have some function be read on loading. Also, it should not be for anyone who starting Perl or use it any how. I did try to search, but not much result. I have try a file '.perldb':... (1 Reply)
Discussion started by: alex_5161
1 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Programming

Can perl get a pagename when called from iframe?

how? there is html-page with: <iframe> <!--#exec cgi="perl-script"--> </iframe> so in that perl-script need to delegate the name of html-page why? too lazy for ajax (3 Replies)
Discussion started by: tip78
3 Replies

6. UNIX for Dummies Questions & Answers

Please help me to find the functions for description

i have taken a screen shot of the question. http://www.magicscreenshot.com/MagicScreenshot/REAdbT956kE.png Please some one help me.:) (1 Reply)
Discussion started by: kjamsheed
1 Replies

7. UNIX for Dummies Questions & Answers

finding called functions recursively

I want to write a shell script which traverses a cpp file. Suppose there is function fncn_name6 .. which is called by fncn_name5 which in turn called by fncn_name4 and so on .. in a single cpp class. ie fncn_name1 { fncn_name2 { fncn_name3 } { fncn_name4 } } so fncn_name1 is... (2 Replies)
Discussion started by: ultimatix
2 Replies

8. Shell Programming and Scripting

perl functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
1 Replies

9. Shell Programming and Scripting

perl problem - why isn't 'die' being called?

last week i started learning perl, so have limited skill and knowledge. why isn't 'die' being called and the script exiting before the 'directory created' line? if (! -d "$logdir") { system "mkdir -p $logdir" || die print "\nERROR: release log directory creation failed - $logdir: $!\n";... (4 Replies)
Discussion started by: mjays
4 Replies

10. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies
Login or Register to Ask a Question