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, os.path, io, string
25 from gen_error_common import parseErrorDescriptions, ErrorDef
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")
35 line = "#define %s NT_STATUS(%s)\n" % (err.err_define, hex(err.err_code))
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")
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")
60 # Account for the possibility that some errors may not have descriptions
61 if err.err_string == "":
63 out_file.write("\t{ N_(\"%s\"), %s },\n"%(err.err_string, err.err_define))
64 out_file.write("{ 0, NT_STATUS(0) }\n")
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 <Python.h>\n")
73 out_file.write("#include \"includes.h\"\n\n")
74 out_file.write("static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)\n");
75 out_file.write("{\n");
76 out_file.write("\tif (v > LONG_MAX) {\n");
77 out_file.write("\t\treturn PyLong_FromUnsignedLongLong(v);\n");
78 out_file.write("\t} else {\n");
79 out_file.write("\t\treturn PyInt_FromLong(v);\n");
80 out_file.write("\t}\n");
81 out_file.write("}\n\n");
82 # This is needed to avoid a missing prototype error from the C
83 # compiler. There is never a prototype for this function, it is a
84 # module loaded by python with dlopen() and found with dlsym().
85 out_file.write("void initntstatus(void);\n")
86 out_file.write("void initntstatus(void)\n")
88 out_file.write("\tPyObject *m;\n\n")
89 out_file.write("\tm = Py_InitModule3(\"ntstatus\", NULL, \"NTSTATUS error defines\");\n");
90 out_file.write("\tif (m == NULL)\n");
91 out_file.write("\t\treturn;\n\n");
93 line = """\tPyModule_AddObject(m, \"%s\",
94 \t\tndr_PyLong_FromUnsignedLongLong(NT_STATUS_V(%s)));\n""" % (err.err_define, err.err_define)
96 out_file.write("}\n");
98 def transformErrorName( error_name ):
99 if error_name.startswith("STATUS_"):
100 error_name = error_name.replace("STATUS_", "", 1)
101 elif error_name.startswith("RPC_NT_"):
102 error_name = error_name.replace("RPC_NT_", "RPC_", 1)
103 elif error_name.startswith("EPT_NT_"):
104 error_name = error_name.replace("EPT_NT_", "EPT_", 1)
105 return "NT_STATUS_" + error_name
107 # Very simple script to generate files nterr_gen.c & ntstatus_gen.h.
108 # These files contain generated definitions.
109 # This script takes four inputs:
110 # [1]: The name of the text file which is the content of an HTML table
111 # (e.g. the one found at http://msdn.microsoft.com/en-us/library/cc231200.aspx)
113 # [2]: The name of the output generated header file with NTStatus #defines
114 # [3]: The name of the output generated source file with C arrays
115 # [4]: The name of the output generated python file
119 if len(sys.argv) == 5:
120 input_file = sys.argv[1]
121 gen_headerfile_name = sys.argv[2]
122 gen_sourcefile_name = sys.argv[3]
123 gen_pythonfile_name = sys.argv[4]
125 print("usage: %s winerrorfile headerfile sourcefile pythonfile" % (sys.argv[0]))
129 file_contents = open(input_file, "r")
131 errors = parseErrorDescriptions(file_contents, False, transformErrorName)
133 print("writing new header file: %s" % gen_headerfile_name)
134 out_file = open(gen_headerfile_name, "w")
135 generateHeaderFile(out_file, errors)
137 print("writing new source file: %s" % gen_sourcefile_name)
138 out_file = open(gen_sourcefile_name, "w")
139 generateSourceFile(out_file, errors)
141 print("writing new python file: %s" % gen_pythonfile_name)
142 out_file = open(gen_pythonfile_name, "w")
143 generatePythonFile(out_file, errors)
146 if __name__ == '__main__':