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
54 with
open(path
, 'r') as p
:
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')
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 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']:
182 if parameter
['synonym'] == "1":
184 if parameter
['generated'] == "0":
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']
195 if parameter
['parm']:
196 param
= "const struct share_params *p"
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
207 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
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
214 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
216 if parameter
['context'] == 'G':
217 output_string
+= '(void);\n'
218 elif parameter
['context'] == 'S':
219 output_string
+= '(%s);\n' % param
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
)
230 def make_lib_proto(path_in
, path_out
):
231 file_out
= open(path_out
, 'w')
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']:
238 if parameter
['synonym'] == "1":
240 if parameter
['generated'] == "0":
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'
258 raise Exception(parameter
['name'] + " has an invalid context " + parameter
['context'])
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'
265 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
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'
272 raise Exception(parameter
['name'] + " has an invalid param type " + parameter
['type'])
274 file_out
.write(output_string
)
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')
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']:
311 if parameter
['synonym'] == "1":
314 if (scope
== "GLOBAL" and parameter
['context'] != "G" or
315 scope
== "LOCAL" and parameter
['context'] != "S"):
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
)
335 "boolean" : "P_BOOL",
336 "boolean-rev" : "P_BOOLREV",
337 "boolean-auto" : "P_ENUM",
339 "string" : "P_STRING",
340 "integer" : "P_INTEGER",
343 "cmdlist" : "P_CMDLIST",
346 "ustring" : "P_USTRING",
350 def make_param_table(path_in
, path_out
):
351 file_out
= open(path_out
, 'w')
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']:
364 if parameter
['context'] == 'G':
369 p_type
= type_dict
.get(parameter
['type'])
371 if parameter
['context'] == 'G':
375 offset
= "%s_VAR(%s)" % (temp
, parameter
['function'])
377 enumlist
= parameter
['enumlist']
378 handler
= parameter
['handler']
379 synonym
= parameter
['synonym']
380 deprecated
= parameter
['deprecated']
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
)
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"
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
)
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
)
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
)