s4/auth/tests: Fix kerberos test string size
[Samba.git] / script / generate_param.py
blob5fa3c193a2c0382ac54b8e6f2f9e4a4e6a7cac6b
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 errno
25 import os
26 import re
27 import subprocess
28 import xml.etree.ElementTree as ET
29 import sys
30 import optparse
32 # parse command line arguments
33 parser = optparse.OptionParser()
34 parser.add_option("-f", "--file", dest="filename",
35 help="input file", metavar="FILE")
36 parser.add_option("-o", "--output", dest="output",
37 help='output file', metavar="FILE")
38 parser.add_option("--mode", type="choice", metavar="<FUNCTIONS|S3PROTO|LIBPROTO|PARAMDEFS|PARAMTABLE>",
39 choices=["FUNCTIONS", "S3PROTO", "LIBPROTO", "PARAMDEFS", "PARAMTABLE"], default="FUNCTIONS")
40 parser.add_option("--scope", metavar="<GLOBAL|LOCAL>",
41 choices=["GLOBAL", "LOCAL"], default="GLOBAL")
43 (options, args) = parser.parse_args()
45 if options.filename is None:
46 parser.error("No input file specified")
47 if options.output is None:
48 parser.error("No output file specified")
51 def iterate_all(path):
52 """Iterate and yield all the parameters.
54 :param path: path to parameters xml file
55 """
57 try:
58 p = open(path, 'r')
59 except IOError as e:
60 raise Exception("Error opening parameters file")
61 out = p.read()
63 # parse the parameters xml file
64 root = ET.fromstring(out)
65 for parameter in root:
66 name = parameter.attrib.get("name")
67 param_type = parameter.attrib.get("type")
68 context = parameter.attrib.get("context")
69 func = parameter.attrib.get("function")
70 synonym = parameter.attrib.get("synonym")
71 removed = parameter.attrib.get("removed")
72 generated = parameter.attrib.get("generated_function")
73 handler = parameter.attrib.get("handler")
74 enumlist = parameter.attrib.get("enumlist")
75 deprecated = parameter.attrib.get("deprecated")
76 synonyms = parameter.findall('synonym')
78 if removed == "1":
79 continue
81 constant = parameter.attrib.get("constant")
82 parm = parameter.attrib.get("parm")
83 if name is None or param_type is None or context is None:
84 raise Exception("Error parsing parameter: " + name)
85 if func is None:
86 func = name.replace(" ", "_").lower()
87 if enumlist is None:
88 enumlist = "NULL"
89 if handler is None:
90 handler = "NULL"
91 yield {'name': name,
92 'type': param_type,
93 'context': context,
94 'function': func,
95 'constant': (constant == '1'),
96 'parm': (parm == '1'),
97 'synonym' : synonym,
98 'generated' : generated,
99 'enumlist' : enumlist,
100 'handler' : handler,
101 'deprecated' : deprecated,
102 'synonyms' : synonyms }
105 # map doc attributes to a section of the generated function
106 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
107 param_type_dict = {
108 "boolean" : "_BOOL",
109 "list" : "_LIST",
110 "string" : "_STRING",
111 "integer" : "_INTEGER",
112 "enum" : "_INTEGER",
113 "char" : "_CHAR",
114 "boolean-auto" : "_INTEGER",
115 "cmdlist" : "_LIST",
116 "bytes" : "_INTEGER",
117 "octal" : "_INTEGER",
118 "ustring" : "_STRING",
122 def generate_functions(path_in, path_out):
123 f = open(path_out, 'w')
124 try:
125 f.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
126 for parameter in iterate_all(options.filename):
127 # filter out parameteric options
128 if ':' in parameter['name']:
129 continue
130 if parameter['synonym'] == "1":
131 continue
132 if parameter['generated'] == "0":
133 continue
135 output_string = "FN"
136 temp = context_dict.get(parameter['context'])
137 if temp is None:
138 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
139 output_string += temp
140 if parameter['constant']:
141 output_string += "_CONST"
142 if parameter['parm']:
143 output_string += "_PARM"
144 temp = param_type_dict.get(parameter['type'])
145 if temp is None:
146 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
147 output_string += temp
148 f.write(output_string + "(" + parameter['function'] + ", " + parameter['function'] + ')\n')
149 finally:
150 f.close()
153 mapping = {
154 'boolean' : 'bool ',
155 'string' : 'char *',
156 'integer' : 'int ',
157 'char' : 'char ',
158 'list' : 'const char **',
159 'enum' : 'int ',
160 'boolean-auto' : 'int ',
161 'cmdlist' : 'const char **',
162 'bytes' : 'int ',
163 'octal' : 'int ',
164 'ustring' : 'char *',
168 def make_s3_param_proto(path_in, path_out):
169 file_out = open(path_out, 'w')
170 try:
171 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
172 header = get_header(path_out)
173 file_out.write("#ifndef %s\n" % header)
174 file_out.write("#define %s\n\n" % header)
175 for parameter in iterate_all(path_in):
176 # filter out parameteric options
177 if ':' in parameter['name']:
178 continue
179 if parameter['synonym'] == "1":
180 continue
181 if parameter['generated'] == "0":
182 continue
184 output_string = ""
185 if parameter['constant']:
186 output_string += 'const '
187 param_type = mapping.get(parameter['type'])
188 if param_type is None:
189 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
190 output_string += param_type
191 output_string += "lp_%s" % parameter['function']
193 param = None
194 if parameter['parm']:
195 param = "const struct share_params *p"
196 else:
197 param = "int"
199 if parameter['type'] == 'string' and not parameter['constant']:
200 if parameter['context'] == 'G':
201 output_string += '(TALLOC_CTX *ctx);\n'
202 elif parameter['context'] == 'S':
203 output_string += '(TALLOC_CTX *ctx, %s);\n' % param
204 else:
205 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
206 else:
207 if parameter['context'] == 'G':
208 output_string += '(void);\n'
209 elif parameter['context'] == 'S':
210 output_string += '(%s);\n' % param
211 else:
212 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
214 file_out.write(output_string)
216 file_out.write("\n#endif /* %s */\n\n" % header)
217 finally:
218 file_out.close()
221 def make_lib_proto(path_in, path_out):
222 file_out = open(path_out, 'w')
223 try:
224 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
225 for parameter in iterate_all(path_in):
226 # filter out parameteric options
227 if ':' in parameter['name']:
228 continue
229 if parameter['synonym'] == "1":
230 continue
231 if parameter['generated'] == "0":
232 continue
234 output_string = ""
235 if parameter['constant']:
236 output_string += 'const '
237 param_type = mapping.get(parameter['type'])
238 if param_type is None:
239 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
240 output_string += param_type
242 output_string += "lpcfg_%s" % parameter['function']
244 if parameter['type'] == 'string' and not parameter['constant']:
245 if parameter['context'] == 'G':
246 output_string += '(struct loadparm_context *, TALLOC_CTX *ctx);\n'
247 elif parameter['context'] == 'S':
248 output_string += '(struct loadparm_service *, struct loadparm_service *, TALLOC_CTX *ctx);\n'
249 else:
250 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
251 else:
252 if parameter['context'] == 'G':
253 output_string += '(struct loadparm_context *);\n'
254 elif parameter['context'] == 'S':
255 output_string += '(struct loadparm_service *, struct loadparm_service *);\n'
256 else:
257 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
259 file_out.write(output_string)
260 finally:
261 file_out.close()
264 def get_header(path):
265 header = os.path.basename(path).upper()
266 header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
267 return "__%s__" % header
270 def make_param_defs(path_in, path_out, scope):
271 file_out = open(path_out, 'w')
272 try:
273 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
274 header = get_header(path_out)
275 file_out.write("#ifndef %s\n" % header)
276 file_out.write("#define %s\n\n" % header)
277 if scope == "GLOBAL":
278 file_out.write("/**\n")
279 file_out.write(" * This structure describes global (ie., server-wide) parameters.\n")
280 file_out.write(" */\n")
281 file_out.write("struct loadparm_global \n")
282 file_out.write("{\n")
283 file_out.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
284 elif scope == "LOCAL":
285 file_out.write("/**\n")
286 file_out.write(" * This structure describes a single service.\n")
287 file_out.write(" */\n")
288 file_out.write("struct loadparm_service \n")
289 file_out.write("{\n")
290 file_out.write("\tbool autoloaded;\n")
292 for parameter in iterate_all(path_in):
293 # filter out parameteric options
294 if ':' in parameter['name']:
295 continue
296 if parameter['synonym'] == "1":
297 continue
299 if (scope == "GLOBAL" and parameter['context'] != "G" or
300 scope == "LOCAL" and parameter['context'] != "S"):
301 continue
303 output_string = "\t"
304 param_type = mapping.get(parameter['type'])
305 if param_type is None:
306 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
307 output_string += param_type
309 output_string += " %s;\n" % parameter['function']
310 file_out.write(output_string)
312 file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
313 file_out.write("};\n")
314 file_out.write("\n#endif /* %s */\n\n" % header)
315 finally:
316 file_out.close()
319 type_dict = {
320 "boolean" : "P_BOOL",
321 "boolean-rev" : "P_BOOLREV",
322 "boolean-auto" : "P_ENUM",
323 "list" : "P_LIST",
324 "string" : "P_STRING",
325 "integer" : "P_INTEGER",
326 "enum" : "P_ENUM",
327 "char" : "P_CHAR",
328 "cmdlist" : "P_CMDLIST",
329 "bytes" : "P_BYTES",
330 "octal" : "P_OCTAL",
331 "ustring" : "P_USTRING",
335 def make_param_table(path_in, path_out):
336 file_out = open(path_out, 'w')
337 try:
338 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
339 header = get_header(path_out)
340 file_out.write("#ifndef %s\n" % header)
341 file_out.write("#define %s\n\n" % header)
343 file_out.write("struct parm_struct parm_table[] = {\n")
345 for parameter in iterate_all(path_in):
346 # filter out parameteric options
347 if ':' in parameter['name']:
348 continue
349 if parameter['context'] == 'G':
350 p_class = "P_GLOBAL"
351 else:
352 p_class = "P_LOCAL"
354 p_type = type_dict.get(parameter['type'])
356 if parameter['context'] == 'G':
357 temp = "GLOBAL"
358 else:
359 temp = "LOCAL"
360 offset = "%s_VAR(%s)" % (temp, parameter['function'])
362 enumlist = parameter['enumlist']
363 handler = parameter['handler']
364 synonym = parameter['synonym']
365 deprecated = parameter['deprecated']
366 flags_list = []
367 if synonym == "1":
368 flags_list.append("FLAG_SYNONYM")
369 if deprecated == "1":
370 flags_list.append("FLAG_DEPRECATED")
371 flags = "|".join(flags_list)
372 synonyms = parameter['synonyms']
374 file_out.write("\t{\n")
375 file_out.write("\t\t.label\t\t= \"%s\",\n" % parameter['name'])
376 file_out.write("\t\t.type\t\t= %s,\n" % p_type)
377 file_out.write("\t\t.p_class\t= %s,\n" % p_class)
378 file_out.write("\t\t.offset\t\t= %s,\n" % offset)
379 file_out.write("\t\t.special\t= %s,\n" % handler)
380 file_out.write("\t\t.enum_list\t= %s,\n" % enumlist)
381 if flags != "":
382 file_out.write("\t\t.flags\t\t= %s,\n" % flags)
383 file_out.write("\t},\n")
385 if synonyms is not None:
386 # for synonyms, we only list the synonym flag:
387 flags = "FLAG_SYNONYM"
388 for syn in synonyms:
389 file_out.write("\t{\n")
390 file_out.write("\t\t.label\t\t= \"%s\",\n" % syn.text)
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 file_out.write("\n\t{NULL, P_BOOL, P_NONE, 0, NULL, NULL, 0}\n")
401 file_out.write("};\n")
402 file_out.write("\n#endif /* %s */\n\n" % header)
403 finally:
404 file_out.close()
407 if options.mode == 'FUNCTIONS':
408 generate_functions(options.filename, options.output)
409 elif options.mode == 'S3PROTO':
410 make_s3_param_proto(options.filename, options.output)
411 elif options.mode == 'LIBPROTO':
412 make_lib_proto(options.filename, options.output)
413 elif options.mode == 'PARAMDEFS':
414 make_param_defs(options.filename, options.output, options.scope)
415 elif options.mode == 'PARAMTABLE':
416 make_param_table(options.filename, options.output)