-
Notifications
You must be signed in to change notification settings - Fork 5
/
tree2dotx
executable file
·166 lines (144 loc) · 5.89 KB
/
tree2dotx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
#
# tree2dotx --- transfer a "tree"(such as the result of tree,calltree,cflow -b)
# to a picture discribed by DOT language(provided by Graphviz)
#
# Author: falcon <wuzhangjin@gmail.com>
# Update: 2007-11-14, 2015-3-19
# Usage:
#
# tree -L 2 -d /path/to/a/directory | bash tree2dotx | dot -Tsvg -o tree.svg
# cd /path/to/a/c/project/; calltree -gb -np -m *.c | bash tree2dotx | dot -Tsvg -o calltree.svg
# cd /path/to/a/c/project/; cflow -b -m setup_rw_floppy kernel/blk_drv/floppy.c | bash tree2dotx | dot -Tsvg -o cflow.svg
#
# Set the picture size, direction(LR=Left2Right,TB=Top2Bottom) and shape(diamond, circle, box)
size="1920,1080"
direction="LR"
shape="box"
# color, X11 color name: http://en.wikipedia.org/wiki/X11_color_names
fontcolor="blue"
fillcolor="Wheat"
# fontsize
fontsize=16
# Specify the symbols you not concern with space as decollator here
filterstr="";
# output: dot, flame
output="dot"
# Usage
function usage
{
echo ""
echo " $0 "
echo ""
echo " [ -f \"filter1 filter2 ...\" ]"
echo " [ -s size, ex: 1080,760; 1920,1080 ]"
echo " [ -d direction, ex: LR; TB ]"
echo " -h get help"
echo ""
}
while getopts "f:s:S:d:h:o:" opt;
do
case $opt in
f)
filterstr=$OPTARG
;;
s)
size=$OPTARG
;;
S)
shape=$OPTARG
;;
d)
direction=$OPTARG
;;
o)
output=$OPTARG
;;
h|?)
usage $0;
exit 1;
;;
esac
done
# Transfer the tree result to a file described in DOT language
grep -v ^$ | grep -v "^[0-9]* director" \
| sed -e "s/<.*>.*//g" | tr -d '\(' | tr -d '\)' | tr '|' ' ' \
| sed -e "s/ \[.*\].*//g" \
| awk '{if(NR==1) system("basename "$0); else printf("%s\n", $0);}' \
| awk -v fstr="$filterstr" '# function for filter the symbols you not concern
function need_filter(node) {
for ( i in farr ) {
if (match(node,farr[i]" ") == 1 || match(node,"^"farr[i]"$") == 1) {
return 1;
}
}
return 0;
}
BEGIN{
# Filternode array are used to record the symbols who have been filtered.
oldnodedepth = -1; oldnode = ""; nodep[-1] = ""; filter[nodep[-1]] = 0;
oldnodedepth_orig = -1; nodepre = 0; nodebase = 0; nodefirst = 0;
output = "'$output'";
#printf("output = %s\n", output);
# Store the symbols to an array farr
split(fstr,farr," ");
# print some setting info
if (output == "dot") {
printf("digraph G{\n");
printf("\trankdir='$direction';\n");
printf("\tsize=\"'$size'\";\n");
printf("\tnode [fontsize='$fontsize',fontcolor='$fontcolor',style=filled,fillcolor='$fillcolor',shape='$shape'];\n");
}
}{
# Get the node, and its depth(nodedepth)
# nodedepth = match($0, "[^| `]");
nodedepth = match($0, "[[:digit:]|[:alpha:]]|[[:alnum:]]");
node = substr($0,nodedepth);
# printf("%d %d %s \n", nodedepth, oldnodedepth_orig, node);
if (nodefirst == 1 && oldnodedepth_orig > 0) {
nodefirst = 0;
nodebase = nodedepth-oldnodedepth_orig;
}
if (nodedepth == 0)
nodedepth=1;
tmp = nodedepth;
# printf("pre=%d base=%d np=%d oldnp=%d node=%s \n", nodepre, nodebase, tmp, oldnodedepth_orig, node);
if (nodedepth != 0 && oldnodedepth_orig == -1) {
nodepre = nodedepth-1;
nodefirst = 1;
nodedepth = 0;
} else if (nodebase != 0) {
nodedepth = int((nodedepth-nodepre)/nodebase);
}
# if whose depth is 1 less than him, who is his parent
if (nodedepth-oldnodedepth == 1) {
nodep[nodedepth-1] = oldnode;
}
# for debugging
# printf("%d %s\n", nodedepth, node);
# printf("\t\"%s\";\n",node);
# print the vectors
if (oldnodedepth != -1) {
# if need filter or whose parent have been filter, not print it, and set the flat of filter to 1
if (need_filter(node) || filter[nodep[nodedepth-1]] == 1) {
filter[node] = 1;
# printf("node = %s, filter[node] = %d\n", node, filter[node]);
} else if (nodep[nodedepth-1] != "") {
if (output == "dot") {
printf("\t\"%s\" -> \"%s\";\n", nodep[nodedepth-1], node, nodep[nodedepth-1], node);
} else {
for (i = 0; i < nodedepth; i++)
printf("%s;", nodep[i]);
printf("%s 1\n", node);
}
# printf("\t\"%s\" -> \"%s\"[label=\"%s>%s\"];\n", nodep[nodedepth-1], node, nodep[nodedepth-1], node);
}
}
# save the old depth and the old node
oldnodedepth_orig = tmp;
oldnodedepth = nodedepth;
oldnode = node;
} END {
if (output == "dot")
printf("}");
}'