-
Notifications
You must be signed in to change notification settings - Fork 46
/
gen_structinfo.awk
executable file
·212 lines (198 loc) · 3.76 KB
/
gen_structinfo.awk
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#! /usr/bin/awk -f
# vim:ts=4:sw=4
BEGIN {
brace = 0;
incomment = 0;
buffer_len = 0;
}
function printstruct(structname) {
printf "define(`ELEMENTSOF_%s', `%s')\n", structname, ELEMENTSOF[structname];
printf "define(`COUNTOF_%s', `%s')\n", structname, COUNTOF[structname];
printf "define(`SIZEOF_%s', `( %s )')\n", structname, SIZEOF[structname];
}
function countBrace(text, len, i, char, braceCount) {
len = length(text);
braceCount = 0;
for (i = 1; i <= len; ++i) {
char = substr(text, i, 1);
if (char == "{") {
braceCount = braceCount + 1;
}
else if (char == "}") {
braceCount = braceCount - 1;
}
}
return braceCount;
}
# multiline comment handling
{
# removes one line comment
gsub(/\/\*(.+?)\*\//, " ");
}
/\*\// {
if (incomment) {
sub(/.*\*\//, "");
incomment = 0;
}
}
incomment {
next;
}
/\/\*/ {
sub(/\/\*.*/, "");
incomment = 1;
# fall through
}
# skip file/line mark here to be faster
/^#/ {
next;
}
/^}.*;/ {
if (instruct) {
sub(";", "");
structname = instruct;
if (structname == 1 && $2) {
structname = $2;
}
if (structname in typedefs) {
structname = typedefs[structname];
}
sizeinfo = "";
elms = "";
for (i = 0; i in buffer; i ++) {
if (i) {
sizeinfo = sizeinfo " + ";
}
sizeinfo = sizeinfo "sizeof(((" structname "*)NULL)->" buffer[i] ")";
if (i == 0) {
elms = "\"" buffer[i] "\"";
}
else {
elms = elms "," "\"" buffer[i] "\"";
}
}
ELEMENTSOF[structname] = elms;
COUNTOF[structname] = i;
SIZEOF[structname] = sizeinfo;
printstruct(structname);
print "\n";
for (i in buffer) {
delete buffer[i];
}
buffer_len = 0;
instruct = 0;
}
next;
}
/.[{}]/ {
brace += countBrace($0);
}
{
if (brace == 1 && instruct) {
gsub(/(^[\t ]+|[\t ]+$)/, ""); # trim whitespaces
sub(/.*[{}]/, "");
gsub(/\[[^\]]+\]/, ""); # ignore [...]
gsub(/:[0-9]+/, ""); # ignore struct bit
if (match($0, /^[^(]*\([ ]*\*([^)]+)\)/)) {
sub(/ +/, "")
sub(/^[^(]*\(\*/, "");
sub(/\).*/, "");
# function pointer
buffer[buffer_len] = $0;
buffer_len ++;
}
else {
# process normal variables
# ignore any ()s
while (gsub(/(\([^)]*\))/, "")) {
}
if (match($0, /[()]/)) {
next;
}
# unsigned int *a, b; int c;
gsub(/[*]/, " ");
# unsigned int a, b; int c;
gsub(/ +/, " ");
# unsigned int a, b; int c;
gsub(/ *[,;]/, ";");
# unsigned int a; b; int c;
if (!match($0, /;/)) {
next;
}
# print "=DEBUG=" $0 "==";
split($0, chunks, ";");
# [unsigned int a, b, c]
for (i = 1; i in chunks; i ++) {
if (chunks[i] == "") {
delete chunks[i];
continue;
}
split(chunks[i], pieces, " ");
# [unsigned, int, a]
# [b]
# [c]
last_piece = "";
for (j = 1; j in pieces; j ++) {
last_piece = pieces[j];
delete pieces[j];
}
if (last_piece == "") {
# print "=ERROR=" chunks[i] "==";
delete chunks[i];
continue;
}
# a
# b
# c
buffer[buffer_len] = last_piece;
buffer_len ++;
delete chunks[i]
}
last_piece = "";
}
next;
}
}
/^typedef struct [^{]*;/ {
sub(";", "");
typename=$3;
newtypename=$4;
typedefs[typename] = newtypename;
if (ELEMENTSOF[typename]) {
ELEMENTSOF[newtypename] = ELEMENTSOF[typename];
COUNTOF[newtypename] = COUNTOF[typename];
sub(/.*/, SIZEOF[typename]);
gsub(typename, newtypename);
SIZEOF[newtypename] = $0;
printstruct(newtypename);
}
next;
}
/^typedef struct .*\{[^}]*$/ {
brace = countBrace($0);
if (brace > 0) {
instruct = 1;
}
else {
brace = 0;
instruct = 0;
}
for (i in buffer) {
delete buffer[i];
}
next;
}
/^struct .*\{.*/ {
brace = countBrace($0);
if (brace > 0) {
instruct = $2;
}
else {
brace = 0;
instruct = 0;
}
for (i in buffer) {
delete buffer[i];
}
next;
}