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/>.
28 import xml.etree.ElementTree as ET
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>",
39 choices=["FUNCTIONS", "S3PROTO", "LIBPROTO", "PARAMDEFS"], 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
59 raise Exception("Error opening parameters file")
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")
75 constant = parameter.attrib.get("constant")
76 parm = parameter.attrib.get("parm")
77 if name is None or param_type is None or context is None:
78 raise Exception("Error parsing parameter: " + name)
80 func = name.replace(" ", "_").lower()
85 'constant': (constant == '1'),
86 'parm': (parm == '1'),
88 'generated' : generated }
90 # map doc attributes to a section of the generated function
91 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
96 "integer" : "_INTEGER",
99 "boolean-auto" : "_INTEGER",
102 def generate_functions(path_in, path_out):
103 f = open(path_out, 'w')
105 f.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
106 for parameter in iterate_all(options.filename):
107 # filter out parameteric options
108 if ':' in parameter['name']:
110 if parameter['synonym'] == "1":
112 if parameter['generated'] == "0":
116 temp = context_dict.get(parameter['context'])
118 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
119 output_string += temp
120 if parameter['constant']:
121 output_string += "_CONST"
122 if parameter['parm']:
123 output_string += "_PARM"
124 temp = param_type_dict.get(parameter['type'])
126 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
127 output_string += temp
128 f.write(output_string + "(" + parameter['function'] +", " + parameter['function'] + ')\n')
137 'list' : 'const char **',
139 'boolean-auto' : 'int ',
142 def make_s3_param_proto(path_in, path_out):
143 file_out = open(path_out, 'w')
145 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
146 header = get_header(path_out)
147 file_out.write("#ifndef %s\n" % header)
148 file_out.write("#define %s\n\n" % header)
149 for parameter in iterate_all(path_in):
150 # filter out parameteric options
151 if ':' in parameter['name']:
153 if parameter['synonym'] == "1":
155 if parameter['generated'] == "0":
159 if parameter['constant']:
160 output_string += 'const '
161 param_type = mapping.get(parameter['type'])
162 if param_type is None:
163 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
164 output_string += param_type
165 output_string += "lp_%s" % parameter['function']
168 if parameter['parm']:
169 param = "const struct share_params *p"
173 if parameter['type'] == 'string' and not parameter['constant']:
174 if parameter['context'] == 'G':
175 output_string += '(TALLOC_CTX *ctx);\n'
176 elif parameter['context'] == 'S':
177 output_string += '(TALLOC_CTX *ctx, %s);\n' % param
179 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
181 if parameter['context'] == 'G':
182 output_string += '(void);\n'
183 elif parameter['context'] == 'S':
184 output_string += '(%s);\n' % param
186 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
188 file_out.write(output_string)
190 file_out.write("\n#endif /* %s */\n\n" % header)
195 def make_lib_proto(path_in, path_out):
196 file_out = open(path_out, 'w')
198 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
199 for parameter in iterate_all(path_in):
200 # filter out parameteric options
201 if ':' in parameter['name']:
203 if parameter['synonym'] == "1":
205 if parameter['generated'] == "0":
209 if parameter['constant']:
210 output_string += 'const '
211 param_type = mapping.get(parameter['type'])
212 if param_type is None:
213 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
214 output_string += param_type
216 output_string += "lpcfg_%s" % parameter['function']
218 if parameter['type'] == 'string' and not parameter['constant']:
219 if parameter['context'] == 'G':
220 output_string += '(struct loadparm_context *, TALLOC_CTX *ctx);\n'
221 elif parameter['context'] == 'S':
222 output_string += '(struct loadparm_service *, struct loadparm_service *, TALLOC_CTX *ctx);\n'
224 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
226 if parameter['context'] == 'G':
227 output_string += '(struct loadparm_context *);\n'
228 elif parameter['context'] == 'S':
229 output_string += '(struct loadparm_service *, struct loadparm_service *);\n'
231 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
234 file_out.write(output_string)
238 def get_header(path):
239 header = os.path.basename(path).upper()
240 header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
241 return "__%s__" % header
243 def make_param_defs(path_in, path_out, scope):
244 file_out = open(path_out, 'w')
246 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
247 header = get_header(path_out)
248 file_out.write("#ifndef %s\n" % header)
249 file_out.write("#define %s\n\n" % header)
250 if scope == "GLOBAL":
251 file_out.write("/**\n")
252 file_out.write(" * This structure describes global (ie., server-wide) parameters.\n")
253 file_out.write(" */\n")
254 file_out.write("struct loadparm_global \n")
255 file_out.write("{\n")
256 file_out.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
257 elif scope == "LOCAL":
258 file_out.write("/**\n")
259 file_out.write(" * This structure describes a single service.\n")
260 file_out.write(" */\n")
261 file_out.write("struct loadparm_service \n")
262 file_out.write("{\n")
263 file_out.write("\tbool autoloaded;\n")
265 for parameter in iterate_all(path_in):
266 # filter out parameteric options
267 if ':' in parameter['name']:
269 if parameter['synonym'] == "1":
272 if (scope == "GLOBAL" and parameter['context'] != "G" or
273 scope == "LOCAL" and parameter['context'] != "S"):
277 param_type = mapping.get(parameter['type'])
278 if param_type is None:
279 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
280 output_string += param_type
282 output_string += " %s;\n" % parameter['function']
283 file_out.write(output_string)
285 file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
286 file_out.write("};\n")
287 file_out.write("\n#endif /* %s */\n\n" % header)
291 if options.mode == 'FUNCTIONS':
292 generate_functions(options.filename, options.output)
293 elif options.mode == 'S3PROTO':
294 make_s3_param_proto(options.filename, options.output)
295 elif options.mode == 'LIBPROTO':
296 make_lib_proto(options.filename, options.output)
297 elif options.mode == 'PARAMDEFS':
298 make_param_defs(options.filename, options.output, options.scope)