ctdb-recoverd: LCP2 cleanups
[Samba.git] / script / generate_param.py
blob4e04b3a45bfabfbae61063bd8265216f5559a56e
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|S3PARAM|S3TABLE>",
39 choices=["FUNCTIONS", "S3PROTO", "LIBPROTO", "PARAMDEFS", "S3PARAM", "S3TABLE"], 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")
50 def iterate_all(path):
51 """Iterate and yield all the parameters.
53 :param path: path to parameters xml file
54 """
56 try:
57 p = open(path, 'r')
58 except IOError, e:
59 raise Exception("Error opening parameters file")
60 out = p.read()
62 # parse the parameters xml file
63 root = ET.fromstring(out)
64 for parameter in root:
65 name = parameter.attrib.get("name")
66 param_type = parameter.attrib.get("type")
67 context = parameter.attrib.get("context")
68 func = parameter.attrib.get("function")
69 synonym = parameter.attrib.get("synonym")
70 removed = parameter.attrib.get("removed")
71 generated = parameter.attrib.get("generated_function")
72 if synonym == "1" or removed == "1" or generated == "0":
73 continue
74 constant = parameter.attrib.get("constant")
75 parm = parameter.attrib.get("parm")
76 if name is None or param_type is None or context is None:
77 raise Exception("Error parsing parameter: " + name)
78 if func is None:
79 func = name.replace(" ", "_").lower()
80 yield {'name': name,
81 'type': param_type,
82 'context': context,
83 'function': func,
84 'constant': (constant == '1'),
85 'parm': (parm == '1')}
87 # map doc attributes to a section of the generated function
88 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
89 param_type_dict = {"boolean": "_BOOL", "list": "_LIST", "string": "_STRING",
90 "integer": "_INTEGER", "enum": "_INTEGER", "char" : "_CHAR",
91 "boolean-auto": "_INTEGER"}
93 def generate_functions(path_in, path_out):
94 f = open(path_out, 'w')
95 try:
96 f.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
97 for parameter in iterate_all(options.filename):
98 # filter out parameteric options
99 if ':' in parameter['name']:
100 continue
101 output_string = "FN"
102 temp = context_dict.get(parameter['context'])
103 if temp is None:
104 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
105 output_string += temp
106 if parameter['constant']:
107 output_string += "_CONST"
108 if parameter['parm']:
109 output_string += "_PARM"
110 temp = param_type_dict.get(parameter['type'])
111 if temp is None:
112 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
113 output_string += temp
114 f.write(output_string + "(" + parameter['function'] +", " + parameter['function'] + ')\n')
115 finally:
116 f.close()
118 mapping = {'boolean': 'bool ', 'string': 'char *', 'integer': 'int ', 'char': 'char ',
119 'list': 'const char **', 'enum': 'int ', 'boolean-auto': 'int '}
121 def make_s3_param_proto(path_in, path_out):
122 file_out = open(path_out, 'w')
123 try:
124 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
125 header = get_header(path_out)
126 file_out.write("#ifndef %s\n" % header)
127 file_out.write("#define %s\n\n" % header)
128 for parameter in iterate_all(path_in):
129 # filter out parameteric options
130 if ':' in parameter['name']:
131 continue
133 output_string = ""
134 if parameter['constant']:
135 output_string += 'const '
136 param_type = mapping.get(parameter['type'])
137 if param_type is None:
138 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
139 output_string += param_type
140 output_string += "lp_%s" % parameter['function']
142 param = None
143 if parameter['parm']:
144 param = "const struct share_params *p"
145 else:
146 param = "int"
148 if parameter['type'] == 'string' and not parameter['constant']:
149 if parameter['context'] == 'G':
150 output_string += '(TALLOC_CTX *ctx);\n'
151 elif parameter['context'] == 'S':
152 output_string += '(TALLOC_CTX *ctx, %s);\n' % param
153 else:
154 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
155 else:
156 if parameter['context'] == 'G':
157 output_string += '(void);\n'
158 elif parameter['context'] == 'S':
159 output_string += '(%s);\n' % param
160 else:
161 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
163 file_out.write(output_string)
165 file_out.write("\n#endif /* %s */\n\n" % header)
166 finally:
167 file_out.close()
170 def make_lib_proto(path_in, path_out):
171 file_out = open(path_out, 'w')
172 try:
173 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
174 for parameter in iterate_all(path_in):
175 # filter out parameteric options
176 if ':' in parameter['name']:
177 continue
179 output_string = ""
180 if parameter['constant'] or parameter['type'] == 'string':
181 output_string += 'const '
182 param_type = mapping.get(parameter['type'])
183 if param_type is None:
184 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
185 output_string += param_type
187 output_string += "lpcfg_%s" % parameter['function']
189 if parameter['context'] == 'G':
190 output_string += '(struct loadparm_context *);\n'
191 elif parameter['context'] == 'S':
192 output_string += '(struct loadparm_service *, struct loadparm_service *);\n'
193 else:
194 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
197 file_out.write(output_string)
198 finally:
199 file_out.close()
201 def get_header(path):
202 header = os.path.basename(path).upper()
203 header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
204 return "__%s__" % header
206 def make_param_defs(path_in, path_out, scope):
207 file_out = open(path_out, 'w')
208 try:
209 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
210 header = get_header(path_out)
211 file_out.write("#ifndef %s\n" % header)
212 file_out.write("#define %s\n\n" % header)
213 if scope == "GLOBAL":
214 file_out.write("/**\n")
215 file_out.write(" * This structure describes global (ie., server-wide) parameters.\n")
216 file_out.write(" */\n")
217 file_out.write("struct loadparm_global \n")
218 file_out.write("{\n")
219 file_out.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
220 file_out.write("\tchar * dnsdomain;\n")
221 elif scope == "LOCAL":
222 file_out.write("/**\n")
223 file_out.write(" * This structure describes a single service.\n")
224 file_out.write(" */\n")
225 file_out.write("struct loadparm_service \n")
226 file_out.write("{\n")
227 file_out.write("\tbool autoloaded;\n")
229 for parameter in iterate_all(path_in):
230 # filter out parameteric options
231 if ':' in parameter['name']:
232 continue
234 if (scope == "GLOBAL" and parameter['context'] != "G" or
235 scope == "LOCAL" and parameter['context'] != "S"):
236 continue
238 output_string = "\t"
239 param_type = mapping.get(parameter['type'])
240 if param_type is None:
241 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
242 output_string += param_type
244 output_string += " %s;\n" % parameter['function']
245 file_out.write(output_string)
247 file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
248 file_out.write("};\n")
249 file_out.write("\n#endif /* %s */\n\n" % header)
250 finally:
251 file_out.close()
253 def make_s3_param(path_in, path_out):
254 file_out = open(path_out, 'w')
255 try:
256 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
257 header = get_header(path_out)
258 file_out.write("#ifndef %s\n" % header)
259 file_out.write("#define %s\n\n" % header)
260 file_out.write("struct loadparm_s3_helpers\n")
261 file_out.write("{\n")
262 file_out.write("\tconst char * (*get_parametric)(struct loadparm_service *, const char *type, const char *option);\n")
263 file_out.write("\tstruct parm_struct * (*get_parm_struct)(const char *param_name);\n")
264 file_out.write("\tvoid * (*get_parm_ptr)(struct loadparm_service *service, struct parm_struct *parm);\n")
265 file_out.write("\tstruct loadparm_service * (*get_service)(const char *service_name);\n")
266 file_out.write("\tstruct loadparm_service * (*get_default_loadparm_service)(void);\n")
267 file_out.write("\tstruct loadparm_service * (*get_servicebynum)(int snum);\n")
268 file_out.write("\tint (*get_numservices)(void);\n")
269 file_out.write("\tbool (*load)(const char *filename);\n")
270 file_out.write("\tbool (*set_cmdline)(const char *pszParmName, const char *pszParmValue);\n")
271 file_out.write("\tvoid (*dump)(FILE *f, bool show_defaults, int maxtoprint);\n")
272 file_out.write("\tconst char * (*dnsdomain)(void);\n")
274 for parameter in iterate_all(path_in):
275 # filter out parameteric options
276 if ':' in parameter['name']:
277 continue
278 if parameter['context'] != 'G':
279 continue
280 # STRING isn't handle yet properly
281 if parameter['type'] == 'string' and not parameter['constant']:
282 continue
283 output_string = "\t"
284 if parameter['constant'] or parameter['type'] == 'string':
285 output_string += 'const '
286 param_type = mapping.get(parameter['type'])
287 if param_type is None:
288 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
289 output_string += param_type
291 output_string += " (*%s)(void);\n" % parameter['function']
292 file_out.write(output_string)
294 file_out.write("};\n")
295 file_out.write("\n#endif /* %s */\n\n" % header)
296 finally:
297 file_out.close()
299 def make_s3_param_ctx_table(path_in, path_out):
300 file_out = open(path_out, 'w')
301 try:
302 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
303 file_out.write("static const struct loadparm_s3_helpers s3_fns =\n")
304 file_out.write("{\n")
305 file_out.write("\t.get_parametric = lp_parm_const_string_service,\n")
306 file_out.write("\t.get_parm_struct = lp_get_parameter,\n")
307 file_out.write("\t.get_parm_ptr = lp_parm_ptr,\n")
308 file_out.write("\t.get_service = lp_service_for_s4_ctx,\n")
309 file_out.write("\t.get_servicebynum = lp_servicebynum_for_s4_ctx,\n")
310 file_out.write("\t.get_default_loadparm_service = lp_default_loadparm_service,\n")
311 file_out.write("\t.get_numservices = lp_numservices,\n")
312 file_out.write("\t.load = lp_load_for_s4_ctx,\n")
313 file_out.write("\t.set_cmdline = lp_set_cmdline,\n")
314 file_out.write("\t.dump = lp_dump,\n")
315 file_out.write("\t.dnsdomain = lp_dnsdomain,\n")
316 header = get_header(path_out)
318 for parameter in iterate_all(path_in):
319 # filter out parameteric options
320 if ':' in parameter['name']:
321 continue
322 if parameter['context'] != 'G':
323 continue
324 # STRING isn't handle yet properly
325 if parameter['type'] == 'string' and not parameter['constant']:
326 continue
327 output_string = "\t.%s" % parameter['function']
328 output_string += " = lp_%s,\n" % parameter['function']
329 file_out.write(output_string)
331 file_out.write("};")
332 finally:
333 file_out.close()
337 if options.mode == 'FUNCTIONS':
338 generate_functions(options.filename, options.output)
339 elif options.mode == 'S3PROTO':
340 make_s3_param_proto(options.filename, options.output)
341 elif options.mode == 'LIBPROTO':
342 make_lib_proto(options.filename, options.output)
343 elif options.mode == 'PARAMDEFS':
344 make_param_defs(options.filename, options.output, options.scope)
345 elif options.mode == 'S3PARAM':
346 make_s3_param(options.filename, options.output)
347 elif options.mode == 'S3TABLE':
348 make_s3_param_ctx_table(options.filename, options.output)