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
|
# build parse functions for a parsed struct file
function elem_name(v, elem)
{
return v["UNION"]elem;
}
function parse_array(f, v, elnum, flags,
LOCAL, type, elem)
{
type = elements[elnum, "type"];
elem = elements[elnum, "elem"];
v["ELEM"] = elem_name(v, elem);
v["TYPE"] = type;
v["FLAGS"] = flags;
v["ARRAY_LEN"] = elements[elnum, "array_len"];
if (type == "wchar") {
print_template(f, "prs_wstring.tpl", v);
} else {
print_template(f, "prs_array.tpl", v);
}
}
function parse_element(f, v, elnum, flags,
LOCAL, type, elem)
{
type = elements[elnum, "type"];
if (substr(type,1,1) == ".") return;
elem = elements[elnum, "elem"];
if (elements[elnum,"ptr"] == "") {
v["PTR"] = "\\&";
} else {
v["PTR"] = " ";
}
v["ELEM"] = elem_name(v, elem);
v["TYPE"] = type;
v["FLAGS"] = flags;
print_template(f, "prs_element.tpl", v);
}
function parse_union(f, v, elnum, flags,
LOCAL, i)
{
v["UNION"] = elements[elnum, "elem"];
v["SWITCH"] = elements[elnum, "switch"];
if (elements[elnum, "ptr"] == "1") {
v["UNION"] = v["UNION"]"->";
} else {
v["UNION"] = v["UNION"]".";
}
print_template(f, "union_start.tpl", v);
for (i=0;i<unions[elnum, "num_elems"];i++) {
v["CASE"] = elements[unions[elnum, i], "case"];
print_template(f, "prs_case.tpl", v);
if (elements[elnum, "ptr"] == "1") {
parse_scalars(f, v, unions[elnum, i], "PARSE_SCALARS");
parse_buffers(f, v, unions[elnum, i], "PARSE_BUFFERS");
} else {
if (flags == "PARSE_SCALARS") {
parse_scalars(f, v, unions[elnum, i], flags);
} else {
parse_buffers(f, v, unions[elnum, i], flags);
}
}
print_template(f, "prs_break.tpl", v);
}
v["UNION"] = "";
print_template(f, "union_end.tpl", v);
}
function parse_scalar(f, v, elnum, flags)
{
if (elements[elnum, "type"] == "union") {
parse_union(f, v, elnum, flags);
} else if (elements[elnum, "array_len"]!="") {
parse_array(f, v, elnum, flags);
} else {
parse_element(f, v, elnum, flags);
}
}
function parse_pointer(f, v, elnum, flags,
LOCAL, elem)
{
elem = elements[elnum, "elem"];
v["ELEM"] = elem_name(v, elem);
v["FLAGS"] = flags;
print_template(f, "prs_pointer.tpl", v);
}
function parse_scalars(f, v, elnum, flags)
{
if (elements[elnum, "ptr"] == "1") {
parse_pointer(f, v, elnum, flags);
} else {
parse_scalar(f, v, elnum, flags);
}
}
function parse_buffers(f, v, elnum, flags,
LOCAL, elem, type)
{
elem = elements[elnum, "elem"];
type = elements[elnum, "type"];
v["ELEM"] = elem_name(v, elem);
if (elements[elnum, "ptr"] == "1") {
print_template(f, "ifptr_start.tpl", v);
parse_scalar(f, v, elnum, "PARSE_SCALARS|PARSE_BUFFERS");
print_template(f, "ifptr_end.tpl", v);
} else {
parse_scalar(f, v, elnum, flags);
}
}
function struct_parser(f, v, struct_num,
LOCAL, i, n1)
{
v["STRUCTNAME"] = structs[struct_num, "name"];
v["FUNCNAME"] = "io_" v["STRUCTNAME"];
print_template(f, "fn_start.tpl", v);
for (n1=0;n1<structs[struct_num, "num_elems"];n1++) {
if (elements[structs[struct_num, n1], "type"] == ".trailer") break;
}
# first all the structure pointers, scalars and arrays
for (i=0;i<n1;i++) {
parse_scalars(f, v, structs[struct_num, i], "PARSE_SCALARS");
}
print_template(f, "fn_mid.tpl", v);
# now the buffers
for (i=0;i<n1;i++) {
parse_buffers(f, v, structs[struct_num, i], "PARSE_BUFFERS");
}
# and any trailers
for (i=n1;i<structs[struct_num, "num_elems"];i++) {
parse_scalars(f, v, structs[struct_num, i], "PARSE_SCALARS");
parse_buffers(f, v, structs[struct_num, i], "PARSE_BUFFERS");
}
print_template(f, "fn_end.tpl", v);
}
function produce_parsers(f,
LOCAL, v, i)
{
v["MODULE"]=module;
print_template(f, "module_start.tpl", v);
for (i=0;i < num_structs;i++) {
struct_parser(f, v, i);
}
print_template(f, "module_end.tpl", v);
}
|