ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source4 / scripting / bin / gen_ntstatus.py
blob4d58731b525543164eff5172af49e37793ce6ec2
1 #!/usr/bin/env python3
4 # Unix SMB/CIFS implementation.
6 # HRESULT Error definitions
8 # Copyright (C) Noel Power <noel.power@suse.com> 2014
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # This program 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
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 import sys, io
25 from gen_error_common import ErrorDef, parseErrorDescriptions
27 def generateHeaderFile(out_file, errors):
28 out_file.write("/*\n")
29 out_file.write(" * Descriptions for errors generated from\n")
30 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
31 out_file.write(" */\n\n")
32 out_file.write("#ifndef _NTSTATUS_GEN_H\n")
33 out_file.write("#define _NTSTATUS_GEN_H\n")
34 for err in errors:
35 line = "#define %s NT_STATUS(%#x)\n" % (err.err_define, err.err_code)
36 out_file.write(line)
37 out_file.write("\n#endif /* _NTSTATUS_GEN_H */\n")
39 def generateSourceFile(out_file, errors):
40 out_file.write("/*\n")
41 out_file.write(" * Names for errors generated from\n")
42 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
43 out_file.write(" */\n")
45 out_file.write("static const nt_err_code_struct nt_errs[] = \n")
46 out_file.write("{\n")
47 for err in errors:
48 out_file.write("\t{ \"%s\", %s },\n" % (err.err_define, err.err_define))
49 out_file.write("{ 0, NT_STATUS(0) }\n")
50 out_file.write("};\n")
52 out_file.write("\n/*\n")
53 out_file.write(" * Descriptions for errors generated from\n")
54 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
55 out_file.write(" */\n")
57 out_file.write("static const nt_err_code_struct nt_err_desc[] = \n")
58 out_file.write("{\n")
59 for err in errors:
60 # Account for the possibility that some errors may not have descriptions
61 if err.err_string == "":
62 continue
63 out_file.write("\t{ N_(\"%s\"), %s },\n"%(err.err_string, err.err_define))
64 out_file.write("{ 0, NT_STATUS(0) }\n")
65 out_file.write("};")
67 def generatePythonFile(out_file, errors):
68 out_file.write("/*\n")
69 out_file.write(" * New descriptions for existing errors generated from\n")
70 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
71 out_file.write(" */\n")
72 out_file.write("#include \"lib/replace/system/python.h\"\n")
73 out_file.write("#include \"python/py3compat.h\"\n")
74 out_file.write("#include \"includes.h\"\n\n")
75 out_file.write("static struct PyModuleDef moduledef = {\n")
76 out_file.write("\tPyModuleDef_HEAD_INIT,\n")
77 out_file.write("\t.m_name = \"ntstatus\",\n")
78 out_file.write("\t.m_doc = \"NTSTATUS error defines\",\n")
79 out_file.write("\t.m_size = -1,\n")
80 out_file.write("};\n\n")
81 out_file.write("MODULE_INIT_FUNC(ntstatus)\n")
82 out_file.write("{\n")
83 out_file.write("\tPyObject *m;\n\n")
84 out_file.write("\tm = PyModule_Create(&moduledef);\n")
85 out_file.write("\tif (m == NULL)\n")
86 out_file.write("\t\treturn NULL;\n\n")
87 for err in errors:
88 line = """\tPyModule_AddObject(m, \"%s\",
89 \t\tPyLong_FromUnsignedLongLong(NT_STATUS_V(%s)));\n""" % (err.err_define, err.err_define)
90 out_file.write(line)
91 out_file.write("\n")
92 out_file.write("\treturn m;\n")
93 out_file.write("}\n")
96 def generateRustFile(out_file, errors):
97 out_file.write("/*\n")
98 out_file.write(" * Descriptions for errors generated from\n")
99 out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
100 out_file.write(" */\n\n")
101 out_file.write("use std::fmt;\n\n")
102 out_file.write("#[derive(PartialEq, Eq)]\n")
103 out_file.write("pub struct NTSTATUS(u32);\n\n")
104 for err in errors:
105 if err.err_define in ['NT_STATUS_OK', 'NT_STATUS_WAIT_0',
106 'NT_STATUS_ABANDONED_WAIT_0',
107 'NT_STATUS_FWP_TOO_MANY_BOOTTIME_FILTERS']:
108 out_file.write("#[allow(dead_code)]\n")
109 line = "pub const %s: NTSTATUS = NTSTATUS(%#x);\n" % (err.err_define, err.err_code)
110 out_file.write(line)
111 out_file.write("\nimpl NTSTATUS {\n")
112 out_file.write("\tfn description(&self) -> &str {\n")
113 out_file.write("\t\tmatch *self {\n")
114 for err in errors:
115 # Account for the possibility that some errors may not have descriptions
116 if err.err_string == "":
117 continue
118 if err.err_define not in ['NT_STATUS_OK', 'NT_STATUS_WAIT_0',
119 'NT_STATUS_ABANDONED_WAIT_0',
120 'NT_STATUS_FWP_TOO_MANY_BOOTTIME_FILTERS']:
121 out_file.write("\t\t\t%s => \"%s\",\n" % (err.err_define, err.err_string))
122 out_file.write("\t\t\t_ => \"Unknown NTSTATUS error code\",\n")
123 out_file.write("\t\t}\n");
124 out_file.write("\t}\n");
125 out_file.write("}\n\n");
126 out_file.write("impl fmt::Display for NTSTATUS {\n")
127 out_file.write("\tfn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {\n")
128 out_file.write("\t\twrite!(f, \"NTSTATUS({:#x}): {}\", self.0, self.description())\n")
129 out_file.write("\t}\n")
130 out_file.write("}\n\n");
131 out_file.write("impl fmt::Debug for NTSTATUS {\n")
132 out_file.write("\tfn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {\n")
133 out_file.write("\t\twrite!(f, \"NTSTATUS({:#x})\", self.0)\n")
134 out_file.write("\t}\n")
135 out_file.write("}\n\n");
136 out_file.write("impl std::error::Error for NTSTATUS {}\n")
138 def transformErrorName( error_name ):
139 if error_name.startswith("STATUS_"):
140 error_name = error_name.replace("STATUS_", "", 1)
141 elif error_name.startswith("RPC_NT_"):
142 error_name = error_name.replace("RPC_NT_", "RPC_", 1)
143 elif error_name.startswith("EPT_NT_"):
144 error_name = error_name.replace("EPT_NT_", "EPT_", 1)
145 return "NT_STATUS_" + error_name
147 # Very simple script to generate files nterr_gen.c & ntstatus_gen.h.
148 # These files contain generated definitions.
149 # This script takes four inputs:
150 # [1]: The name of the text file which is the content of an HTML table
151 # (e.g. the one found at http://msdn.microsoft.com/en-us/library/cc231200.aspx)
152 # copied and pasted.
153 # [2]: The name of the output generated header file with NTStatus #defines
154 # [3]: The name of the output generated source file with C arrays
155 # [4]: The name of the output generated python file
156 def main ():
157 input_file = None
159 if len(sys.argv) == 6:
160 input_file = sys.argv[1]
161 gen_headerfile_name = sys.argv[2]
162 gen_sourcefile_name = sys.argv[3]
163 gen_pythonfile_name = sys.argv[4]
164 gen_rustfile_name = sys.argv[5]
165 else:
166 print("usage: %s winerrorfile headerfile sourcefile pythonfile rustfile" % (sys.argv[0]))
167 sys.exit()
169 # read in the data
170 with io.open(input_file, "rt", encoding='utf8') as file_contents:
171 errors = parseErrorDescriptions(file_contents, False, transformErrorName)
173 # NT_STATUS_OK is a synonym of NT_STATUS_SUCCESS, and is very widely used
174 # throughout Samba. It must go first in the list to ensure that to ensure
175 # that code that previously found this error code in ‘special_errs’
176 # maintains the same behaviour by falling back to ‘nt_errs’.
177 ok_status = ErrorDef()
178 ok_status.err_code = 0
179 ok_status.err_define = 'NT_STATUS_OK'
180 errors.insert(0, ok_status)
182 print("writing new header file: %s" % gen_headerfile_name)
183 out_file = io.open(gen_headerfile_name, "wt", encoding='utf8')
184 generateHeaderFile(out_file, errors)
185 out_file.close()
186 print("writing new source file: %s" % gen_sourcefile_name)
187 out_file = io.open(gen_sourcefile_name, "wt", encoding='utf8')
188 generateSourceFile(out_file, errors)
189 out_file.close()
190 print("writing new python file: %s" % gen_pythonfile_name)
191 out_file = io.open(gen_pythonfile_name, "wt", encoding='utf8')
192 generatePythonFile(out_file, errors)
193 out_file.close()
194 print("writing new rust file: %s" % gen_rustfile_name)
195 out_file = io.open(gen_rustfile_name, "wt", encoding='utf8')
196 generateRustFile(out_file, errors)
197 out_file.close()
199 if __name__ == '__main__':
201 main()