r4846: do not keep outdated files here.
[Samba.git] / source / aparser / parsefn.awk
blob2bebd765e66aa309547a16f01e459cb17b0cabe2
1 # build parse functions for a parsed struct file
3 function elem_name(v, elem)
5 return v["UNION"]elem;
8 function parse_array(f, v, elnum, flags,
9 LOCAL, type, elem, array_len)
11 type = elements[elnum, "type"];
12 elem = elements[elnum, "elem"];
13 array_len = elements[elnum, "array_len"];
14 v["ELEM"] = elem_name(v, elem);
15 v["TYPE"] = type;
16 v["FLAGS"] = flags;
17 v["ARRAY_LEN"] = array_len;
19 if (array_len=="+") {
20 print_template(f,"prs_array_optional.tpl", v);
21 return;
24 if (array_len=="*") {
25 print_template(f,"prs_array_remainder.tpl", v);
26 return;
29 if (type == "wchar" || type == "uint16") {
30 if (match(array_len,"[0-9]") == 1) {
31 print_template(f, "prs_wstring_fixed.tpl", v);
32 } else {
33 print_template(f, "prs_wstring.tpl", v);
35 } else if (type == "uint8") {
36 if (match(array_len,"[0-9]") == 1) {
37 print_template(f, "prs_uint8s_fixed.tpl", v);
38 } else {
39 print_template(f, "prs_uint8s.tpl", v);
41 } else {
42 print_template(f, "prs_array.tpl", v);
47 function parse_element(f, v, elnum, flags,
48 LOCAL, type, elem)
50 if (elements[elnum,"nowire"] != "") {
51 return;
53 type = elements[elnum, "type"];
54 if (substr(type,1,1) == ".") return;
55 elem = elements[elnum, "elem"];
56 if (elements[elnum,"ptr"] == "") {
57 v["PTR"] = "\\&";
58 } else {
59 v["PTR"] = " ";
61 v["ELEM"] = elem_name(v, elem);
62 v["TYPE"] = type;
63 v["FLAGS"] = flags;
64 print_template(f, "prs_element.tpl", v);
67 function parse_union(f, v, elnum, flags,
68 LOCAL, i)
70 v["UNION"] = elements[elnum, "elem"];
71 v["SWITCH"] = elements[elnum, "switch"];
73 if (elements[elnum, "ptr"] == "1") {
74 v["UNION"] = v["UNION"]"->";
75 } else {
76 v["UNION"] = v["UNION"]".";
79 print_template(f, "union_start.tpl", v);
80 for (i=0;i<unions[elnum, "num_elems"];i++) {
81 v["CASE"] = elements[unions[elnum, i], "case"];
82 print_template(f, "prs_case.tpl", v);
83 if (elements[elnum, "ptr"] == "1") {
84 parse_scalars(f, v, unions[elnum, i], "PARSE_SCALARS");
85 parse_buffers(f, v, unions[elnum, i], "PARSE_BUFFERS");
86 } else {
87 if (flags == "PARSE_SCALARS") {
88 parse_scalars(f, v, unions[elnum, i], flags);
89 } else {
90 parse_buffers(f, v, unions[elnum, i], flags);
93 print_template(f, "prs_break.tpl", v);
95 v["UNION"] = "";
97 print_template(f, "union_end.tpl", v);
100 function parse_scalar(f, v, elnum, flags)
102 if (elements[elnum, "type"] == "union") {
103 parse_union(f, v, elnum, flags);
104 } else if (elements[elnum, "array_len"]!="") {
105 parse_array(f, v, elnum, flags);
106 } else {
107 parse_element(f, v, elnum, flags);
111 function parse_align2(f, v, elnum, flags,
112 LOCAL, elem)
114 elem = elements[elnum, "elem"];
115 v["OFFSET"] = elem_name(v, elem);
116 print_template(f, "prs_align2.tpl", v);
119 function parse_align4(f, v, elnum, flags,
120 LOCAL, elem)
122 elem = elements[elnum, "elem"];
123 v["OFFSET"] = elem_name(v, elem);
124 print_template(f, "prs_align4.tpl", v);
127 function parse_pointer(f, v, elnum, flags,
128 LOCAL, elem)
130 elem = elements[elnum, "elem"];
131 v["ELEM"] = elem_name(v, elem);
132 v["FLAGS"] = flags;
133 print_template(f, "prs_pointer.tpl", v);
136 function parse_scalar_fn(m, v, elnum,
137 LOCAL, elem, type)
139 elem = elements[elnum, "elem"];
140 type = elements[elnum, "type"]
141 xprintf(m, "%s %s", type, elem_name(v, elem));
142 if (type == "union") {
143 } else if (elements[elnum, "array_len"]!="") {
144 } else {
148 function parse_pointer_fn(f, v, elnum, flags,
149 LOCAL, elem)
151 elem = elements[elnum, "elem"];
152 v["ELEM"] = elem_name(v, elem);
153 v["FLAGS"] = flags;
154 xprintf(m, "%s\n", v["ELEM"]);
157 function parse_scalars_fn(m, v, elnum, flags)
159 if (elements[elnum, "type"] == ".align2") {
161 else if (elements[elnum, "type"] == ".align4") {
163 else if (elements[elnum, "ptr"] == "1") {
164 parse_pointer_fn(m, v, elnum, flags);
165 } else {
166 parse_scalar_fn(m, v, elnum, flags);
170 function parse_scalars(f, v, elnum, flags)
172 if (elements[elnum, "type"] == ".align2") {
173 parse_align2(f, v, elnum, flags);
175 else if (elements[elnum, "type"] == ".align4") {
176 parse_align4(f, v, elnum, flags);
178 else if (elements[elnum, "ptr"] == "1") {
179 parse_pointer(f, v, elnum, flags);
180 } else {
181 parse_scalar(f, v, elnum, flags);
185 function parse_buffers(f, v, elnum, flags,
186 LOCAL, elem, type)
188 elem = elements[elnum, "elem"];
189 type = elements[elnum, "type"];
190 v["ELEM"] = elem_name(v, elem);
191 if (elements[elnum, "type"] == ".align2") {
193 else if (elements[elnum, "type"] == ".align4") {
194 } else if (elements[elnum, "ptr"] == "1") {
195 print_template(f, "ifptr_start.tpl", v);
196 parse_scalar(f, v, elnum, "PARSE_SCALARS|PARSE_BUFFERS");
197 print_template(f, "ifptr_end.tpl", v);
198 } else {
199 parse_scalar(f, v, elnum, flags);
203 function struct_parser(f, m, v, struct_num,
204 LOCAL, i, n1, f1, num_elems)
206 f1 = -1;
207 num_elems = structs[struct_num, "num_elems"];
208 v["STRUCTNAME"] = structs[struct_num, "name"];
209 v["FUNCNAME"] = "io_" v["STRUCTNAME"];
210 print_template(f, "fn_start.tpl", v);
212 for (n1=0;n1<num_elems;n1++) {
213 if (elements[structs[struct_num, n1], "type"] == ".trailer") {
214 f1 = n1;
215 break;
219 # first all the structure pointers, scalars and arrays
220 for (i=0;i<n1;i++) {
221 parse_scalars(f, v, structs[struct_num, i], "PARSE_SCALARS");
224 print_template(f, "fn_mid.tpl", v);
226 # now the buffers
227 for (i=0;i<n1;i++) {
228 parse_buffers(f, v, structs[struct_num, i], "PARSE_BUFFERS");
231 # and any trailers
232 for (i=n1;i<num_elems;i++) {
233 parse_scalars(f, v, structs[struct_num, i], "PARSE_SCALARS");
234 parse_buffers(f, v, structs[struct_num, i], "PARSE_BUFFERS");
237 if (i > 0) {
238 print_template(f, "fn_end.tpl", v);
240 else {
241 print_template(f, "fn_end0.tpl", v);
244 if (f1 == -1)
245 return;
247 xprintf(m, "void fn_%s(\n", v["STRUCTNAME"]);
249 for (i=f1+1;i<num_elems;i++) {
250 parse_scalars_fn(m, v, structs[struct_num, i]);
251 if (i != num_elems-1)
252 xprintf(m, ", \n");
256 xprintf(m, ")\n{\n}\n");
259 function produce_parsers(f, m,
260 LOCAL, v, i)
262 v["MODULE"]=module;
264 print_template(f, "module_start.tpl", v);
266 for (i=0;i < num_structs;i++) {
267 struct_parser(f, m, v, i);
270 print_template(f, "module_end.tpl", v);