494e23972a2dee8a968722886d08b40d6fcb9d6a
[garming/samba-autobuild/.git] / script / generate_param.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) 2014 Catalyst.Net Ltd
3 #
4 # Auto generate param_functions.c
5 #
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
9 #
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.
14 #
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.
19 #
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/>.
22 #
23
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
31
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")
42
43 (options, args) = parser.parse_args()
44
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")
49
50 def iterate_all(path):
51     """Iterate and yield all the parameters. 
52
53     :param path: path to parameters xml file
54     """
55
56     try:
57         p = open(path, 'r')
58     except IOError, e:
59         raise Exception("Error opening parameters file")
60     out = p.read()
61
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 removed == "1":
73             continue
74
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)
79         if func is None:
80             func = name.replace(" ", "_").lower()
81         yield {'name': name,
82                'type': param_type,
83                'context': context,
84                'function': func,
85                'constant': (constant == '1'),
86                'parm': (parm == '1'),
87                'synonym' : synonym,
88                'generated' : generated }
89
90 # map doc attributes to a section of the generated function
91 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
92 param_type_dict = {
93                     "boolean"      : "_BOOL",
94                     "list"         : "_LIST",
95                     "string"       : "_STRING",
96                     "integer"      : "_INTEGER",
97                     "enum"         : "_INTEGER",
98                     "char"         : "_CHAR",
99                     "boolean-auto" : "_INTEGER",
100                   }
101
102 def generate_functions(path_in, path_out):
103     f = open(path_out, 'w')
104     try:
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']:
109                 continue
110             if parameter['synonym'] == "1":
111                 continue
112             if parameter['generated'] == "0":
113                 continue
114
115             output_string = "FN"
116             temp = context_dict.get(parameter['context'])
117             if temp is None: 
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'])
125             if temp is None:
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')
129     finally:
130         f.close()
131
132 mapping = {
133             'boolean'      : 'bool ',
134             'string'       : 'char *',
135             'integer'      : 'int ',
136             'char'         : 'char ',
137             'list'         : 'const char **',
138             'enum'         : 'int ',
139             'boolean-auto' : 'int ',
140           }
141
142 def make_s3_param_proto(path_in, path_out):
143     file_out = open(path_out, 'w')
144     try:
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']:
152                 continue
153             if parameter['synonym'] == "1":
154                 continue
155             if parameter['generated'] == "0":
156                 continue
157
158             output_string = ""
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']
166
167             param = None
168             if parameter['parm']:
169                 param = "const struct share_params *p"
170             else:
171                 param = "int"
172
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
178                 else:
179                     raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
180             else:
181                 if parameter['context'] == 'G':
182                     output_string += '(void);\n'
183                 elif parameter['context'] == 'S':
184                     output_string += '(%s);\n' % param
185                 else:
186                     raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
187
188             file_out.write(output_string)
189
190         file_out.write("\n#endif /* %s */\n\n" % header)
191     finally:
192         file_out.close()
193
194
195 def make_lib_proto(path_in, path_out):
196     file_out = open(path_out, 'w')
197     try:
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']:
202                 continue
203             if parameter['synonym'] == "1":
204                 continue
205             if parameter['generated'] == "0":
206                 continue
207
208             output_string = ""
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
215
216             output_string += "lpcfg_%s" % parameter['function']
217
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'
223                 else:
224                     raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
225             else:
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'
230                 else:
231                     raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
232
233             
234             file_out.write(output_string)
235     finally:
236         file_out.close()
237
238 def get_header(path):
239     header = os.path.basename(path).upper()
240     header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
241     return "__%s__" % header
242
243 def make_param_defs(path_in, path_out, scope):
244     file_out = open(path_out, 'w')
245     try:
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")
264  
265         for parameter in iterate_all(path_in):
266             # filter out parameteric options
267             if ':' in parameter['name']:
268                 continue
269             if parameter['synonym'] == "1":
270                 continue
271
272             if (scope == "GLOBAL" and parameter['context'] != "G" or
273                 scope == "LOCAL" and parameter['context'] != "S"):
274                 continue
275
276             output_string = "\t"
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
281
282             output_string += "  %s;\n" % parameter['function']
283             file_out.write(output_string)
284
285         file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
286         file_out.write("};\n")
287         file_out.write("\n#endif /* %s */\n\n" % header)
288     finally:
289         file_out.close()
290
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)