hi,
i am a newbie and have to write a rather complicated script. Assume that i have a variable called x and a C source code file say file1.c (this are the inputs of the script) and i need to find the names of all the functions in the C file containing x.Take the following code as an example:

1.int main(void);
   2.void foo(void);
   3.void bar(void);
   4.
   5.int main(void)
   6.{
   7.int x;
   8.return 0;
   9.}
  10.
  11.void foo(void)
  12.{
  13.int x;
  14.}
  15.
  16.void bar(void)
  17.{
  18.int x;
  19.}

so i guess the output should be:


in 5. main(void) x occurs in line 7 as int x;

in 11.foo(void) x occurs in line 13 as int x

and in 16.bar(void) x occurs in line 18 as int x

Something similar to but not exactly as above. To print the name of the functions containing x is crucial.

i can find line number using grep -n and probably use sed -n '/{/,/}/p' file1.c to select blocks of code delimited by { and } but I'm not sure how to refine this further.

any help would be welcome.

thanks,
Sam

hi,
thanks for the reply..no i don't think i can use an external tool because they have a lot of other features that are not required...i just need a way to solve this problem using sed or awk.

thanks,
Sam

hi,

i was looking at ctags program and i still couldn't figure out how to derive the information i need from a tag file. Can you kindly explain how can i exactly use ctags and the ID utilities to find the function name for a given variable.

thanks,
Sam

double posted here

commented: Thanks for the heads-up +25

> and i need to find the names of all the functions in the C file containing x.
Is this the only thing you need?

C is hard to parse 1-line at a time using sed/awk/grep.
Eg.

int
foo ( void ) {
  int x;
}

int foo ( void )
{
  int x;
}

int foo (
   void
) {
  /* int x; */
  int a,
      x;
}

Unless you have a particular style in mind, which is followed in all the code, I don't see you being able to just regex your way through any bit of code.

ok...but so i guess regular expressions in sed/awk is not the answer. But i do need a solution.How do i use the ctags program to solve my problem?

thanks,
sam

nope..i still did not get the result that i want..by using ctags i could print the all function names for a file ..but there was nothing to determine the function name for a given variable.

You figured that out in 25 minutes?

Allowing for reading time between my post and your initial reading, then time to download and install it, and set up an example.

I think you tried the first obvious thing without really trying to understand it before bouncing the problem back at us with more "it doesn't work".

oh dear..no i had no idea about ctags or ID database before your post..and then when i did "which ctags" i was quiet surprised to find it already installed in usr/bin.

So,i did not have to install it again..and because there were no shortage of C source code files...all had to do was run ctags.

It did give me the list of functions with their line numbers.But again, how do i determine if a variable belongs to one of those functions listed in the tag file.

thanks,
Sam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.