dsdb:schema: use NUMERIC_CMP in place of uint32_cmp
[Samba.git] / source4 / scripting / bin / gen_ntstatus.py
blob5e79378f148b590b8ca9e531138d5ad6341d0947
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")
95 def transformErrorName( error_name ):
96 if error_name.startswith("STATUS_"):
97 error_name = error_name.replace("STATUS_", "", 1)
98 elif error_name.startswith("RPC_NT_"):
99 error_name = error_name.replace("RPC_NT_", "RPC_", 1)
100 elif error_name.startswith("EPT_NT_"):
101 error_name = error_name.replace("EPT_NT_", "EPT_", 1)
102 return "NT_STATUS_" + error_name
104 # Very simple script to generate files nterr_gen.c & ntstatus_gen.h.
105 # These files contain generated definitions.
106 # This script takes four inputs:
107 # [1]: The name of the text file which is the content of an HTML table
108 # (e.g. the one found at http://msdn.microsoft.com/en-us/library/cc231200.aspx)
109 # copied and pasted.
110 # [2]: The name of the output generated header file with NTStatus #defines
111 # [3]: The name of the output generated source file with C arrays
112 # [4]: The name of the output generated python file
113 def main ():
114 input_file = None
116 if len(sys.argv) == 5:
117 input_file = sys.argv[1]
118 gen_headerfile_name = sys.argv[2]
119 gen_sourcefile_name = sys.argv[3]
120 gen_pythonfile_name = sys.argv[4]
121 else:
122 print("usage: %s winerrorfile headerfile sourcefile pythonfile" % (sys.argv[0]))
123 sys.exit()
125 # read in the data
126 with io.open(input_file, "rt", encoding='utf8') as file_contents:
127 errors = parseErrorDescriptions(file_contents, False, transformErrorName)
129 # NT_STATUS_OK is a synonym of NT_STATUS_SUCCESS, and is very widely used
130 # throughout Samba. It must go first in the list to ensure that to ensure
131 # that code that previously found this error code in ‘special_errs’
132 # maintains the same behaviour by falling back to ‘nt_errs’.
133 ok_status = ErrorDef()
134 ok_status.err_code = 0
135 ok_status.err_define = 'NT_STATUS_OK'
136 errors.insert(0, ok_status)
138 print("writing new header file: %s" % gen_headerfile_name)
139 out_file = io.open(gen_headerfile_name, "wt", encoding='utf8')
140 generateHeaderFile(out_file, errors)
141 out_file.close()
142 print("writing new source file: %s" % gen_sourcefile_name)
143 out_file = io.open(gen_sourcefile_name, "wt", encoding='utf8')
144 generateSourceFile(out_file, errors)
145 out_file.close()
146 print("writing new python file: %s" % gen_pythonfile_name)
147 out_file = io.open(gen_pythonfile_name, "wt", encoding='utf8')
148 generatePythonFile(out_file, errors)
149 out_file.close()
151 if __name__ == '__main__':
153 main()