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
********************************
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