libcli: SMB2: Pure SMB2-only negprot fix to make us behave as a Windows client does.
[Samba.git] / script / generate_param.py
blobd79c13c7275eee887015b160d31ab5227d8e62a8
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>",
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
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']:
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['type'] == 'string' and not parameter['constant']:
190 if parameter['context'] == 'G':
191 output_string += '(struct loadparm_context *, TALLOC_CTX *ctx);\n'
192 elif parameter['context'] == 'S':
193 output_string += '(struct loadparm_service *, struct loadparm_service *, TALLOC_CTX *ctx);\n'
194 else:
195 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
196 else:
197 if parameter['context'] == 'G':
198 output_string += '(struct loadparm_context *);\n'
199 elif parameter['context'] == 'S':
200 output_string += '(struct loadparm_service *, struct loadparm_service *);\n'
201 else:
202 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
205 file_out.write(output_string)
206 finally:
207 file_out.close()
209 def get_header(path):
210 header = os.path.basename(path).upper()
211 header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
212 return "__%s__" % header
214 def make_param_defs(path_in, path_out, scope):
215 file_out = open(path_out, 'w')
216 try:
217 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
218 header = get_header(path_out)
219 file_out.write("#ifndef %s\n" % header)
220 file_out.write("#define %s\n\n" % header)
221 if scope == "GLOBAL":
222 file_out.write("/**\n")
223 file_out.write(" * This structure describes global (ie., server-wide) parameters.\n")
224 file_out.write(" */\n")
225 file_out.write("struct loadparm_global \n")
226 file_out.write("{\n")
227 file_out.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
228 file_out.write("\tchar * dnsdomain;\n")
229 elif scope == "LOCAL":
230 file_out.write("/**\n")
231 file_out.write(" * This structure describes a single service.\n")
232 file_out.write(" */\n")
233 file_out.write("struct loadparm_service \n")
234 file_out.write("{\n")
235 file_out.write("\tbool autoloaded;\n")
237 for parameter in iterate_all(path_in):
238 # filter out parameteric options
239 if ':' in parameter['name']:
240 continue
242 if (scope == "GLOBAL" and parameter['context'] != "G" or
243 scope == "LOCAL" and parameter['context'] != "S"):
244 continue
246 output_string = "\t"
247 param_type = mapping.get(parameter['type'])
248 if param_type is None:
249 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
250 output_string += param_type
252 output_string += " %s;\n" % parameter['function']
253 file_out.write(output_string)
255 file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
256 file_out.write("};\n")
257 file_out.write("\n#endif /* %s */\n\n" % header)
258 finally:
259 file_out.close()
261 if options.mode == 'FUNCTIONS':
262 generate_functions(options.filename, options.output)
263 elif options.mode == 'S3PROTO':
264 make_s3_param_proto(options.filename, options.output)
265 elif options.mode == 'LIBPROTO':
266 make_lib_proto(options.filename, options.output)
267 elif options.mode == 'PARAMDEFS':
268 make_param_defs(options.filename, options.output, options.scope)