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
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/>.
25 import xml
.etree
.ElementTree
as ET
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
56 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')
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
)
83 func
= name
.replace(" ", "_").lower()
92 'constant': (constant
== '1'),
93 'substitution': (substitution
== '1'),
94 'parm': (parm
== '1'),
96 'generated' : generated
,
97 'enumlist' : enumlist
,
99 'deprecated' : deprecated
,
100 'synonyms' : synonyms
}
103 # map doc attributes to a section of the generated function
104 context_dict
= {"G": "_GLOBAL", "S": "_LOCAL"}
108 "string" : "_STRING",
109 "integer" : "_INTEGER",
112 "boolean-auto" : "_INTEGER",
114 "bytes" : "_INTEGER",
115 "octal" : "_INTEGER",
116 "ustring" : "_STRING",
120 def generate_functions(path_in
, path_out
):
121 f
= open(path_out
, 'w')
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']:
128 if parameter
['synonym'] == "1":
130 if parameter
['generated'] == "0":
134 temp
= context_dict
.get(parameter
['context'])
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"
142 output_string
+= "_CONST"
143 if parameter
['parm']:
144 output_string
+= "_PARM"
145 temp
= param_type_dict
.get(parameter
['type'])
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')
159 'list' : 'const char **',
161 'boolean-auto' : 'int ',
162 'cmdlist' : 'const char **',
165 'ustring' : 'char *',
169 def make_s3_param_proto(path_in
, path_out
):
170 file_out
= open(path_out
, 'w')
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 for parameter
in iterate_all(path_in
):
177 # filter out parameteric options
178 if ':' in parameter
['name']:
180 if parameter
['synonym'] == "1":
182 if parameter
['generated'] == "0":
186 param_type
= mapping
.get(parameter
['type'])
187 if param_type
is None:
188 raise Exception(parameter
['name'] + " has an invalid context " + parameter
['context'])
189 output_string
+= param_type
190 output_string
+= "lp_%s" % parameter
['function']
193 if parameter
['parm']:
194 param
= "const struct share_params *p"
198 if parameter
['type'] == 'string' or parameter
['type'] == 'ustring':
199 if parameter
['substitution']:
200 if parameter
['context'] == 'G':
201 output_string
+= '(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub);\n'
202 elif parameter
['context'] == 'S':
203 output_string
+= '(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub, %s);\n' % param
205 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
207 if parameter
['context'] == 'G':
208 output_string
= 'const ' + output_string
+ '(void);\n'
209 elif parameter
['context'] == 'S':
210 output_string
= 'const ' + output_string
+ '(%s);\n' % param
212 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
214 if parameter
['context'] == 'G':
215 output_string
+= '(void);\n'
216 elif parameter
['context'] == 'S':
217 output_string
+= '(%s);\n' % param
219 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
221 file_out
.write(output_string
)
223 file_out
.write("\n#endif /* %s */\n\n" % header
)
228 def make_lib_proto(path_in
, path_out
):
229 file_out
= open(path_out
, 'w')
231 file_out
.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
232 for parameter
in iterate_all(path_in
):
233 # filter out parameteric options
234 if ':' in parameter
['name']:
236 if parameter
['synonym'] == "1":
238 if parameter
['generated'] == "0":
242 param_type
= mapping
.get(parameter
['type'])
243 if param_type
is None:
244 raise Exception(parameter
['name'] + " has an invalid context " + parameter
['context'])
245 output_string
+= param_type
247 output_string
+= "lpcfg_%s" % parameter
['function']
249 if parameter
['type'] == 'string' or parameter
['type'] == 'ustring':
250 if parameter
['substitution']:
251 if parameter
['context'] == 'G':
252 output_string
+= '(struct loadparm_context *, const struct loadparm_substitution *lp_sub, TALLOC_CTX *ctx);\n'
253 elif parameter
['context'] == 'S':
254 output_string
+= '(struct loadparm_service *, struct loadparm_service *, TALLOC_CTX *ctx);\n'
256 raise Exception(parameter
['name'] + " has an invalid context " + parameter
['context'])
258 if parameter
['context'] == 'G':
259 output_string
= 'const ' + output_string
+ '(struct loadparm_context *);\n'
260 elif parameter
['context'] == 'S':
261 output_string
= 'const ' + output_string
+ '(struct loadparm_service *, struct loadparm_service *);\n'
263 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
265 if parameter
['context'] == 'G':
266 output_string
+= '(struct loadparm_context *);\n'
267 elif parameter
['context'] == 'S':
268 output_string
+= '(struct loadparm_service *, struct loadparm_service *);\n'
270 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
272 file_out
.write(output_string
)
277 def get_header(path
):
278 header
= os
.path
.basename(path
).upper()
279 header
= header
.replace(".", "_").replace("\\", "_").replace("-", "_")
280 return "__%s__" % header
283 def make_param_defs(path_in
, path_out
, scope
):
284 file_out
= open(path_out
, 'w')
286 file_out
.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
287 header
= get_header(path_out
)
288 file_out
.write("#ifndef %s\n" % header
)
289 file_out
.write("#define %s\n\n" % header
)
290 if scope
== "GLOBAL":
291 file_out
.write("/**\n")
292 file_out
.write(" * This structure describes global (ie., server-wide) parameters.\n")
293 file_out
.write(" */\n")
294 file_out
.write("struct loadparm_global \n")
295 file_out
.write("{\n")
296 file_out
.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
297 elif scope
== "LOCAL":
298 file_out
.write("/**\n")
299 file_out
.write(" * This structure describes a single service.\n")
300 file_out
.write(" */\n")
301 file_out
.write("struct loadparm_service \n")
302 file_out
.write("{\n")
303 file_out
.write("\tbool autoloaded;\n")
305 for parameter
in iterate_all(path_in
):
306 # filter out parameteric options
307 if ':' in parameter
['name']:
309 if parameter
['synonym'] == "1":
312 if (scope
== "GLOBAL" and parameter
['context'] != "G" or
313 scope
== "LOCAL" and parameter
['context'] != "S"):
317 param_type
= mapping
.get(parameter
['type'])
318 if param_type
is None:
319 raise Exception(parameter
['name'] + " has an invalid context " + parameter
['context'])
320 output_string
+= param_type
322 output_string
+= " %s;\n" % parameter
['function']
323 file_out
.write(output_string
)
325 file_out
.write("LOADPARM_EXTRA_%sS\n" % scope
)
326 file_out
.write("};\n")
327 file_out
.write("\n#endif /* %s */\n\n" % header
)
333 "boolean" : "P_BOOL",
334 "boolean-rev" : "P_BOOLREV",
335 "boolean-auto" : "P_ENUM",
337 "string" : "P_STRING",
338 "integer" : "P_INTEGER",
341 "cmdlist" : "P_CMDLIST",
344 "ustring" : "P_USTRING",
348 def make_param_table(path_in
, path_out
):
349 file_out
= open(path_out
, 'w')
351 file_out
.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
352 header
= get_header(path_out
)
353 file_out
.write("#ifndef %s\n" % header
)
354 file_out
.write("#define %s\n\n" % header
)
356 file_out
.write("struct parm_struct parm_table[] = {\n")
358 for parameter
in iterate_all(path_in
):
359 # filter out parameteric options
360 if ':' in parameter
['name']:
362 if parameter
['context'] == 'G':
367 p_type
= type_dict
.get(parameter
['type'])
369 if parameter
['context'] == 'G':
373 offset
= "%s_VAR(%s)" % (temp
, parameter
['function'])
375 enumlist
= parameter
['enumlist']
376 handler
= parameter
['handler']
377 synonym
= parameter
['synonym']
378 deprecated
= parameter
['deprecated']
381 flags_list
.append("FLAG_SYNONYM")
382 if deprecated
== "1":
383 flags_list
.append("FLAG_DEPRECATED")
384 flags
= "|".join(flags_list
)
385 synonyms
= parameter
['synonyms']
387 file_out
.write("\t{\n")
388 file_out
.write("\t\t.label\t\t= \"%s\",\n" % parameter
['name'])
389 file_out
.write("\t\t.type\t\t= %s,\n" % p_type
)
390 file_out
.write("\t\t.p_class\t= %s,\n" % p_class
)
391 file_out
.write("\t\t.offset\t\t= %s,\n" % offset
)
392 file_out
.write("\t\t.special\t= %s,\n" % handler
)
393 file_out
.write("\t\t.enum_list\t= %s,\n" % enumlist
)
395 file_out
.write("\t\t.flags\t\t= %s,\n" % flags
)
396 file_out
.write("\t},\n")
398 if synonyms
is not None:
399 # for synonyms, we only list the synonym flag:
400 flags
= "FLAG_SYNONYM"
402 file_out
.write("\t{\n")
403 file_out
.write("\t\t.label\t\t= \"%s\",\n" % syn
.text
)
404 file_out
.write("\t\t.type\t\t= %s,\n" % p_type
)
405 file_out
.write("\t\t.p_class\t= %s,\n" % p_class
)
406 file_out
.write("\t\t.offset\t\t= %s,\n" % offset
)
407 file_out
.write("\t\t.special\t= %s,\n" % handler
)
408 file_out
.write("\t\t.enum_list\t= %s,\n" % enumlist
)
410 file_out
.write("\t\t.flags\t\t= %s,\n" % flags
)
411 file_out
.write("\t},\n")
413 file_out
.write("\n\t{ .label = NULL }\n")
414 file_out
.write("};\n")
415 file_out
.write("\n#endif /* %s */\n\n" % header
)
420 if options
.mode
== 'FUNCTIONS':
421 generate_functions(options
.filename
, options
.output
)
422 elif options
.mode
== 'S3PROTO':
423 make_s3_param_proto(options
.filename
, options
.output
)
424 elif options
.mode
== 'LIBPROTO':
425 make_lib_proto(options
.filename
, options
.output
)
426 elif options
.mode
== 'PARAMDEFS':
427 make_param_defs(options
.filename
, options
.output
, options
.scope
)
428 elif options
.mode
== 'PARAMTABLE':
429 make_param_table(options
.filename
, options
.output
)