import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / aparser / parserel.awk
1 # build parse functions for a parsed struct file
2
3 function elem_name(v, elem)
4 {
5         return v["UNION"]elem;
6 }
7
8 function parse_array(f, v, elnum, flags,
9                      LOCAL, type, elem, array_len)
10 {
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;
18
19         if (array_len=="+") {
20           print_template(f,"prs_array_optional.tpl", v);
21           return;
22         }
23
24         if (array_len=="&") {
25           print_template(f,"prs_array_null.tpl", v);
26           return;
27         }
28
29         if (array_len=="*") {
30           print_template(f,"prs_array_remainder.tpl", v);
31           return;
32         }
33
34         if (type == "wchar" || type == "uint16") {
35                 if (match(array_len,"[0-9]") == 1) {
36                         print_template(f, "prs_wstring_fixed.tpl", v);
37                 } else {
38                         print_template(f, "prs_wstring.tpl", v);
39                 }
40         } else if (type == "uint8") {
41                 if (match(array_len,"[0-9]") == 1) {
42                         print_template(f, "prs_uint8s_fixed.tpl", v);
43                 } else {
44                         print_template(f, "prs_uint8s.tpl", v);
45                 }
46         } else {
47                 print_template(f, "prs_array.tpl", v);
48         }
49 }
50               
51
52 function parse_element(f, v, elnum, flags,
53                        LOCAL, type, elem)
54 {
55         if (elements[elnum,"nowire"] != "") {
56                 return;
57         }
58         type = elements[elnum, "type"];
59         if (substr(type,1,1) == ".") return;
60         elem = elements[elnum, "elem"];
61         if (elements[elnum,"ptr"] == "") {
62                 v["PTR"] = "\\&";
63         } else {
64                 v["PTR"] = " ";
65         }
66         v["ELEM"] = elem_name(v, elem);
67         v["TYPE"] = type;
68         v["FLAGS"] = flags;
69         print_template(f, "prs_element.tpl", v);
70 }
71
72 function parse_union(f, v, elnum, flags,
73                      LOCAL, i)
74 {
75         v["UNION"] = elements[elnum, "elem"];
76         v["SWITCH"] = elements[elnum, "switch"];
77
78         if (elements[elnum, "ptr"] == "1") {
79                 v["UNION"] = v["UNION"]"->";
80         } else {
81                 v["UNION"] = v["UNION"]".";
82         }
83
84         print_template(f, "union_start.tpl", v);
85         for (i=0;i<unions[elnum, "num_elems"];i++) {
86                 v["CASE"] = elements[unions[elnum, i], "case"];
87                 print_template(f, "prs_case.tpl", v);
88                 if (elements[elnum, "ptr"] == "1") {
89                         parse_scalars(f, v, unions[elnum, i], "PARSE_SCALARS");
90                         parse_buffers(f, v, unions[elnum, i], "PARSE_BUFFERS");
91                 } else {
92                         if (flags == "PARSE_SCALARS") {
93                                 parse_scalars(f, v, unions[elnum, i], flags);
94                         } else {
95                                 parse_buffers(f, v, unions[elnum, i], flags);
96                         }
97                 }
98                 print_template(f, "prs_break.tpl", v);
99         }
100         v["UNION"] = "";
101
102         print_template(f, "union_end.tpl", v);
103 }
104
105 function parse_scalar(f, v, elnum, flags)
106 {
107         if (elements[elnum, "type"] == "union") {
108                 parse_union(f, v, elnum, flags);
109         } else if (elements[elnum, "array_len"]!="") {
110                 parse_array(f, v, elnum, flags);
111         } else {
112                 parse_element(f, v, elnum, flags);
113         }
114 }
115
116 function parse_pointer(f, v, elnum, flags,
117                        LOCAL, elem)
118 {
119         elem = elements[elnum, "elem"];
120         v["ELEM"] = elem_name(v, elem);
121         v["FLAGS"] = flags;
122         print_template(f, "prs_pointer.tpl", v);
123 }
124
125 function parse_scalars(f, v, elnum, flags)
126 {
127         if (elements[elnum, "ptr"] == "1") {
128                 parse_pointer(f, v, elnum, flags);
129         } else {
130                 parse_scalar(f, v, elnum, flags);
131         }
132 }
133
134 function parse_buffers(f, v, elnum, flags,
135                       LOCAL, elem, type)
136 {
137         elem = elements[elnum, "elem"];
138         type = elements[elnum, "type"];
139         v["ELEM"] = elem_name(v, elem);
140         if (elements[elnum, "ptr"] == "1") {
141                 print_template(f, "ifptr_start.tpl", v);
142                 parse_scalar(f, v, elnum, "PARSE_SCALARS|PARSE_BUFFERS");
143                 print_template(f, "ifptr_end.tpl", v);
144         } else {
145                 parse_scalar(f, v, elnum, flags);
146         }
147 }
148
149 function struct_immediate(f, v, struct_num,
150                           LOCAL, i, n1, num_elems) 
151 {
152         num_elems = structs[struct_num, "num_elems"];
153         v["STRUCTNAME"] = structs[struct_num, "name"];
154         v["FUNCNAME"] = "io_" v["STRUCTNAME"];
155
156         print_template(f, "fn_i_start.tpl", v);
157
158         for (i=0;i<num_elems;i++) {
159                 parse_scalars(f, v, structs[struct_num, i], "PARSE_SCALARS");
160                 parse_buffers(f, v, structs[struct_num, i], "PARSE_BUFFERS");
161         }
162
163         print_template(f, "fn_i_end.tpl", v);
164 }
165
166
167 function struct_recursive(f, v, struct_num,
168                           LOCAL, i, n1, num_elems) 
169 {
170         num_elems = structs[struct_num, "num_elems"];
171         v["STRUCTNAME"] = structs[struct_num, "name"];
172         v["FUNCNAME"] = "io_" v["STRUCTNAME"];
173
174         print_template(f, "fn_start.tpl", v);
175
176 # first all the structure pointers, scalars and arrays
177         for (i=0;i<num_elems;i++) {
178                 parse_scalars(f, v, structs[struct_num, i], "PARSE_SCALARS");
179         }
180         
181         print_template(f, "fn_mid.tpl", v);
182         
183 # now the buffers
184         for (i=0;i<num_elems;i++) {
185                 parse_buffers(f, v, structs[struct_num, i], "PARSE_BUFFERS");
186         }
187
188         print_template(f, "fn_end.tpl", v);
189 }
190
191 function struct_parser(f, v, struct_num,
192                        LOCAL, i, n1, num_elems) 
193 {
194         if (structs[struct_num, "recurse"] == "True") {
195                 struct_recursive(f, v, struct_num);
196         } else {
197                 struct_immediate(f, v, struct_num);
198         }
199 }
200
201 function produce_relative(f,
202                          LOCAL, v, i)
203 {
204         v["MODULE"]=module;
205
206         print_template(f, "module_start.tpl", v);
207
208         for (i=0;i < num_structs;i++) {
209                 struct_parser(f, v, i);
210         }
211
212         print_template(f, "module_end.tpl", v);
213 }