s4:torture: Remove trailing spaces from smbtorture.c
[samba4-gss.git] / script / generate_param.py
blob50f2d12599ac760bd5ea70a2a6c0f9fcb6e81e41
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) 2014 Catalyst.Net Ltd
4 # Auto generate param_functions.c
6 # ** NOTE! The following LGPL license applies to the ldb
7 # ** library. This does NOT imply that all of Samba is released
8 # ** under the LGPL
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 # License as published by the Free Software Foundation; either
13 # version 3 of the License, or (at your option) any later version.
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 import os
25 import xml.etree.ElementTree as ET
26 import optparse
28 # parse command line arguments
29 parser = optparse.OptionParser()
30 parser.add_option("-f", "--file", dest="filename",
31 help="input file", metavar="FILE")
32 parser.add_option("-o", "--output", dest="output",
33 help='output file', metavar="FILE")
34 parser.add_option("--mode", type="choice", metavar="<FUNCTIONS|S3PROTO|LIBPROTO|PARAMDEFS|PARAMTABLE>",
35 choices=["FUNCTIONS", "S3PROTO", "LIBPROTO", "PARAMDEFS", "PARAMTABLE"], default="FUNCTIONS")
36 parser.add_option("--scope", metavar="<GLOBAL|LOCAL>",
37 choices=["GLOBAL", "LOCAL"], default="GLOBAL")
39 (options, args) = parser.parse_args()
41 if options.filename is None:
42 parser.error("No input file specified")
43 if options.output is None:
44 parser.error("No output file specified")
47 def iterate_all(path):
48 """Iterate and yield all the parameters.
50 :param path: path to parameters xml file
51 """
53 try:
54 with open(path, 'r') as p:
55 out = p.read()
56 except IOError as e:
57 raise Exception("Error opening parameters file")
59 # parse the parameters xml file
60 root = ET.fromstring(out)
61 for parameter in root:
62 name = parameter.attrib.get("name")
63 param_type = parameter.attrib.get("type")
64 context = parameter.attrib.get("context")
65 func = parameter.attrib.get("function")
66 synonym = parameter.attrib.get("synonym")
67 removed = parameter.attrib.get("removed")
68 generated = parameter.attrib.get("generated_function")
69 handler = parameter.attrib.get("handler")
70 enumlist = parameter.attrib.get("enumlist")
71 deprecated = parameter.attrib.get("deprecated")
72 synonyms = parameter.findall('synonym')
74 if removed == "1":
75 continue
77 constant = parameter.attrib.get("constant")
78 substitution = parameter.attrib.get("substitution")
79 parm = parameter.attrib.get("parm")
80 if name is None or param_type is None or context is None:
81 raise Exception("Error parsing parameter: " + name)
82 if func is None:
83 func = name.replace(" ", "_").lower()
84 if enumlist is None:
85 enumlist = "NULL"
86 if handler is None:
87 handler = "NULL"
88 yield {'name': name,
89 'type': param_type,
90 'context': context,
91 'function': func,
92 'constant': (constant == '1'),
93 'substitution': (substitution == '1'),
94 'parm': (parm == '1'),
95 'synonym' : synonym,
96 'generated' : generated,
97 'enumlist' : enumlist,
98 'handler' : handler,
99 'deprecated' : deprecated,
100 'synonyms' : synonyms }
103 # map doc attributes to a section of the generated function
104 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
105 param_type_dict = {
106 "boolean" : "_BOOL",
107 "list" : "_LIST",
108 "string" : "_STRING",
109 "integer" : "_INTEGER",
110 "enum" : "_INTEGER",
111 "char" : "_CHAR",
112 "boolean-auto" : "_INTEGER",
113 "cmdlist" : "_LIST",
114 "bytes" : "_INTEGER",
115 "octal" : "_INTEGER",
116 "ustring" : "_STRING",
120 def generate_functions(path_in, path_out):
121 f = open(path_out, 'w')
122 try:
123 f.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
124 for parameter in iterate_all(options.filename):
125 # filter out parameteric options
126 if ':' in parameter['name']:
127 continue
128 if parameter['synonym'] == "1":
129 continue
130 if parameter['generated'] == "0":
131 continue
133 output_string = "FN"
134 temp = context_dict.get(parameter['context'])
135 if temp is None:
136 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
137 output_string += temp
138 if parameter['type'] == "string" or parameter['type'] == "ustring":
139 if parameter['substitution']:
140 output_string += "_SUBSTITUTED"
141 else:
142 output_string += "_CONST"
143 if parameter['parm']:
144 output_string += "_PARM"
145 temp = param_type_dict.get(parameter['type'])
146 if temp is None:
147 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
148 output_string += temp
149 f.write(output_string + "(" + parameter['function'] + ", " + parameter['function'] + ')\n')
150 finally:
151 f.close()
154 mapping = {
155 'boolean' : 'bool ',
156 'string' : 'char *',
157 'integer' : 'int ',
158 'char' : 'char ',
159 'list' : 'const char **',
160 'enum' : 'int ',
161 'boolean-auto' : 'int ',
162 'cmdlist' : 'const char **',
163 'bytes' : 'int ',
164 'octal' : 'int ',
165 'ustring' : 'char *',
169 def make_s3_param_proto(path_in, path_out):
170 file_out = open(path_out, 'w')
171 try:
172 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
173 header = get_header(path_out)
174 file_out.write("#ifndef %s\n" % header)
175 file_out.write("#define %s\n\n" % header)
176 file_out.write("struct share_params;\n")
177 file_out.write("struct loadparm_substitution;\n")
178 for parameter in iterate_all(path_in):
179 # filter out parameteric options
180 if ':' in parameter['name']:
181 continue
182 if parameter['synonym'] == "1":
183 continue
184 if parameter['generated'] == "0":
185 continue
187 output_string = ""
188 param_type = mapping.get(parameter['type'])
189 if param_type is None:
190 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
191 output_string += param_type
192 output_string += "lp_%s" % parameter['function']
194 param = None
195 if parameter['parm']:
196 param = "const struct share_params *p"
197 else:
198 param = "int"
200 if parameter['type'] == 'string' or parameter['type'] == 'ustring':
201 if parameter['substitution']:
202 if parameter['context'] == 'G':
203 output_string += '(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub);\n'
204 elif parameter['context'] == 'S':
205 output_string += '(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub, %s);\n' % param
206 else:
207 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
208 else:
209 if parameter['context'] == 'G':
210 output_string = 'const ' + output_string + '(void);\n'
211 elif parameter['context'] == 'S':
212 output_string = 'const ' + output_string + '(%s);\n' % param
213 else:
214 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
215 else:
216 if parameter['context'] == 'G':
217 output_string += '(void);\n'
218 elif parameter['context'] == 'S':
219 output_string += '(%s);\n' % param
220 else:
221 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
223 file_out.write(output_string)
225 file_out.write("\n#endif /* %s */\n\n" % header)
226 finally:
227 file_out.close()
230 def make_lib_proto(path_in, path_out):
231 file_out = open(path_out, 'w')
232 try:
233 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
234 for parameter in iterate_all(path_in):
235 # filter out parameteric options
236 if ':' in parameter['name']:
237 continue
238 if parameter['synonym'] == "1":
239 continue
240 if parameter['generated'] == "0":
241 continue
243 output_string = ""
244 param_type = mapping.get(parameter['type'])
245 if param_type is None:
246 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
247 output_string += param_type
249 output_string += "lpcfg_%s" % parameter['function']
251 if parameter['type'] == 'string' or parameter['type'] == 'ustring':
252 if parameter['substitution']:
253 if parameter['context'] == 'G':
254 output_string += '(struct loadparm_context *, const struct loadparm_substitution *lp_sub, TALLOC_CTX *ctx);\n'
255 elif parameter['context'] == 'S':
256 output_string += '(struct loadparm_service *, struct loadparm_service *, TALLOC_CTX *ctx);\n'
257 else:
258 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
259 else:
260 if parameter['context'] == 'G':
261 output_string = 'const ' + output_string + '(struct loadparm_context *);\n'
262 elif parameter['context'] == 'S':
263 output_string = 'const ' + output_string + '(struct loadparm_service *, struct loadparm_service *);\n'
264 else:
265 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
266 else:
267 if parameter['context'] == 'G':
268 output_string += '(struct loadparm_context *);\n'
269 elif parameter['context'] == 'S':
270 output_string += '(struct loadparm_service *, struct loadparm_service *);\n'
271 else:
272 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
274 file_out.write(output_string)
275 finally:
276 file_out.close()
279 def get_header(path):
280 header = os.path.basename(path).upper()
281 header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
282 return "__%s__" % header
285 def make_param_defs(path_in, path_out, scope):
286 file_out = open(path_out, 'w')
287 try:
288 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
289 header = get_header(path_out)
290 file_out.write("#ifndef %s\n" % header)
291 file_out.write("#define %s\n\n" % header)
292 if scope == "GLOBAL":
293 file_out.write("/**\n")
294 file_out.write(" * This structure describes global (ie., server-wide) parameters.\n")
295 file_out.write(" */\n")
296 file_out.write("struct loadparm_global \n")
297 file_out.write("{\n")
298 file_out.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
299 elif scope == "LOCAL":
300 file_out.write("/**\n")
301 file_out.write(" * This structure describes a single service.\n")
302 file_out.write(" */\n")
303 file_out.write("struct loadparm_service \n")
304 file_out.write("{\n")
305 file_out.write("\tbool autoloaded;\n")
307 for parameter in iterate_all(path_in):
308 # filter out parameteric options
309 if ':' in parameter['name']:
310 continue
311 if parameter['synonym'] == "1":
312 continue
314 if (scope == "GLOBAL" and parameter['context'] != "G" or
315 scope == "LOCAL" and parameter['context'] != "S"):
316 continue
318 output_string = "\t"
319 param_type = mapping.get(parameter['type'])
320 if param_type is None:
321 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
322 output_string += param_type
324 output_string += " %s;\n" % parameter['function']
325 file_out.write(output_string)
327 file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
328 file_out.write("};\n")
329 file_out.write("\n#endif /* %s */\n\n" % header)
330 finally:
331 file_out.close()
334 type_dict = {
335 "boolean" : "P_BOOL",
336 "boolean-rev" : "P_BOOLREV",
337 "boolean-auto" : "P_ENUM",
338 "list" : "P_LIST",
339 "string" : "P_STRING",
340 "integer" : "P_INTEGER",
341 "enum" : "P_ENUM",
342 "char" : "P_CHAR",
343 "cmdlist" : "P_CMDLIST",
344 "bytes" : "P_BYTES",
345 "octal" : "P_OCTAL",
346 "ustring" : "P_USTRING",
350 def make_param_table(path_in, path_out):
351 file_out = open(path_out, 'w')
352 try:
353 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
354 header = get_header(path_out)
355 file_out.write("#ifndef %s\n" % header)
356 file_out.write("#define %s\n\n" % header)
358 file_out.write("struct parm_struct parm_table[] = {\n")
360 for parameter in iterate_all(path_in):
361 # filter out parameteric options
362 if ':' in parameter['name']:
363 continue
364 if parameter['context'] == 'G':
365 p_class = "P_GLOBAL"
366 else:
367 p_class = "P_LOCAL"
369 p_type = type_dict.get(parameter['type'])
371 if parameter['context'] == 'G':
372 temp = "GLOBAL"
373 else:
374 temp = "LOCAL"
375 offset = "%s_VAR(%s)" % (temp, parameter['function'])
377 enumlist = parameter['enumlist']
378 handler = parameter['handler']
379 synonym = parameter['synonym']
380 deprecated = parameter['deprecated']
381 flags_list = []
382 if synonym == "1":
383 flags_list.append("FLAG_SYNONYM")
384 if deprecated == "1":
385 flags_list.append("FLAG_DEPRECATED")
386 flags = "|".join(flags_list)
387 synonyms = parameter['synonyms']
389 file_out.write("\t{\n")
390 file_out.write("\t\t.label\t\t= \"%s\",\n" % parameter['name'])
391 file_out.write("\t\t.type\t\t= %s,\n" % p_type)
392 file_out.write("\t\t.p_class\t= %s,\n" % p_class)
393 file_out.write("\t\t.offset\t\t= %s,\n" % offset)
394 file_out.write("\t\t.special\t= %s,\n" % handler)
395 file_out.write("\t\t.enum_list\t= %s,\n" % enumlist)
396 if flags != "":
397 file_out.write("\t\t.flags\t\t= %s,\n" % flags)
398 file_out.write("\t},\n")
400 if synonyms is not None:
401 # for synonyms, we only list the synonym flag:
402 flags = "FLAG_SYNONYM"
403 for syn in synonyms:
404 file_out.write("\t{\n")
405 file_out.write("\t\t.label\t\t= \"%s\",\n" % syn.text)
406 file_out.write("\t\t.type\t\t= %s,\n" % p_type)
407 file_out.write("\t\t.p_class\t= %s,\n" % p_class)
408 file_out.write("\t\t.offset\t\t= %s,\n" % offset)
409 file_out.write("\t\t.special\t= %s,\n" % handler)
410 file_out.write("\t\t.enum_list\t= %s,\n" % enumlist)
411 if flags != "":
412 file_out.write("\t\t.flags\t\t= %s,\n" % flags)
413 file_out.write("\t},\n")
415 file_out.write("\n\t{ .label = NULL }\n")
416 file_out.write("};\n")
417 file_out.write("\n#endif /* %s */\n\n" % header)
418 finally:
419 file_out.close()
422 if options.mode == 'FUNCTIONS':
423 generate_functions(options.filename, options.output)
424 elif options.mode == 'S3PROTO':
425 make_s3_param_proto(options.filename, options.output)
426 elif options.mode == 'LIBPROTO':
427 make_lib_proto(options.filename, options.output)
428 elif options.mode == 'PARAMDEFS':
429 make_param_defs(options.filename, options.output, options.scope)
430 elif options.mode == 'PARAMTABLE':
431 make_param_table(options.filename, options.output)