Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Function call tree with gawk

Status
Not open for further replies.

ragnarocc

Programmer
Mar 9, 2009
3
US
I'm tring to create a Funcion call tree with gawk and I've parsed out the relocatables and functions name out of readelf into this file
********************************
A
1 C
B
1 C
C
1 E
E
R
1 R
main
1 A
1 B
1 C
1 R
***********************************
where a single input is the name of the function and all 2 field inputs after that are functions called by the lead function. My code thus far is
***********************************
BEGIN {Check = 0}
{
if(Check == 0 && $1 == "main")
{
Check = 1
print $1
next
}

if(Check == 1 && NF == 2)
{
Fun(1, $2, 0)
}

if(Check == 1 && NF == 1 && $1 != "main")
{
exit
}
}

function Fun (Tabs, SearchName, Recursive)
{
set = 0
for(i = 0; i < Tabs; ++i)
{
printf "\t"
}
print SearchName
while((getline NewLine < "test4") == 1) {
Split = split(NewLine, Array)
Times = 0
if(Split == 1 && SearchName == NewLine)
{
set = 1
Times = 1
}
if(set == 1 && Split== 2)
{
if(Array[2] == SearchName && Recursive == 0)
{
Fun((Tabs+1), Array[2], 1)
}
else if(Array[2] == SearchName && Recursive == 1)
{
return
}
else
{
Fun((Tabs+1), Array[2], 0)
}
}
if(set == 1 && Split == 1 && Times == 0)
{
return
}
}
}
**************************************
and outputs
**************************************
main
A
C
E
B
C
R
**************************************
Anyone got any ideas on how to fix it and/or shorten the code?

Thanks
 
Hi

As far as I understand :
Code:
awk 'NF==1{f=$1;next}{c[f]=c[f]" "$2}END{p("main")}function p(w,l,a,i){printf"%*s%s\n",l*2,"",w;split(c[w],a);for(i=1;i in a;i++)if(a[i]!=w)p(a[i],l+1)}' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
If you want to see the recursive calls you may replace this:
printf"%*s%s\n",l*2,"",w
with this:
printf"%*s%s%s\n",l*2,"",w,(c[w]==" "w?"+":"")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top