pyldb: avoid segfault when adding an element with no name
[kai/samba-autobuild/.git] / source4 / scripting / bin / gen_ntstatus.py
1 #!/usr/bin/env python3
2
3 #
4 # Unix SMB/CIFS implementation.
5 #
6 # HRESULT Error definitions
7 #
8 # Copyright (C) Noel Power <noel.power@suse.com> 2014
9 #
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.
14 #
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.
19 #
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/>.
22 #
23 from __future__ import unicode_literals
24 # this file is a bin script and was not imported by any other modules
25 # so it should be fine to enable unicode string for python2
26
27 import sys, os.path, io, string
28 from gen_error_common import parseErrorDescriptions, ErrorDef
29
30 def generateHeaderFile(out_file, errors):
31     out_file.write("/*\n")
32     out_file.write(" * Descriptions for errors generated from\n")
33     out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
34     out_file.write(" */\n\n")
35     out_file.write("#ifndef _NTSTATUS_GEN_H\n")
36     out_file.write("#define _NTSTATUS_GEN_H\n")
37     for err in errors:
38         line = "#define %s NT_STATUS(%#x)\n" % (err.err_define, err.err_code)
39         out_file.write(line)
40     out_file.write("\n#endif /* _NTSTATUS_GEN_H */\n")
41
42 def generateSourceFile(out_file, errors):
43     out_file.write("/*\n")
44     out_file.write(" * Names for errors generated from\n")
45     out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
46     out_file.write(" */\n")
47
48     out_file.write("static const nt_err_code_struct nt_errs[] = \n")
49     out_file.write("{\n")
50     for err in errors:
51         out_file.write("\t{ \"%s\", %s },\n" % (err.err_define, err.err_define))
52     out_file.write("{ 0, NT_STATUS(0) }\n")
53     out_file.write("};\n")
54
55     out_file.write("\n/*\n")
56     out_file.write(" * Descriptions for errors generated from\n")
57     out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
58     out_file.write(" */\n")
59
60     out_file.write("static const nt_err_code_struct nt_err_desc[] = \n")
61     out_file.write("{\n")
62     for err in errors:
63         # Account for the possibility that some errors may not have descriptions
64         if err.err_string == "":
65             continue
66         out_file.write("\t{ N_(\"%s\"), %s },\n"%(err.err_string, err.err_define))
67     out_file.write("{ 0, NT_STATUS(0) }\n")
68     out_file.write("};")
69
70 def generatePythonFile(out_file, errors):
71     out_file.write("/*\n")
72     out_file.write(" * New descriptions for existing errors generated from\n")
73     out_file.write(" * [MS-ERREF] http://msdn.microsoft.com/en-us/library/cc704588.aspx\n")
74     out_file.write(" */\n")
75     out_file.write("#include <Python.h>\n")
76     out_file.write("#include \"python/py3compat.h\"\n")
77     out_file.write("#include \"includes.h\"\n\n")
78     out_file.write("static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)\n");
79     out_file.write("{\n");
80     out_file.write("\tif (v > LONG_MAX) {\n");
81     out_file.write("\t\treturn PyLong_FromUnsignedLongLong(v);\n");
82     out_file.write("\t} else {\n");
83     out_file.write("\t\treturn PyInt_FromLong(v);\n");
84     out_file.write("\t}\n");
85     out_file.write("}\n\n");
86     # This is needed to avoid a missing prototype error from the C
87     # compiler. There is never a prototype for this function, it is a
88     # module loaded by python with dlopen() and found with dlsym().
89     out_file.write("static struct PyModuleDef moduledef = {\n")
90     out_file.write("\tPyModuleDef_HEAD_INIT,\n")
91     out_file.write("\t.m_name = \"ntstatus\",\n")
92     out_file.write("\t.m_doc = \"NTSTATUS error defines\",\n")
93     out_file.write("\t.m_size = -1,\n")
94     out_file.write("};\n\n")
95     out_file.write("MODULE_INIT_FUNC(ntstatus)\n")
96     out_file.write("{\n")
97     out_file.write("\tPyObject *m;\n\n")
98     out_file.write("\tm = PyModule_Create(&moduledef);\n");
99     out_file.write("\tif (m == NULL)\n");
100     out_file.write("\t\treturn NULL;\n\n");
101     for err in errors:
102         line = """\tPyModule_AddObject(m, \"%s\", 
103                   \t\tndr_PyLong_FromUnsignedLongLong(NT_STATUS_V(%s)));\n""" % (err.err_define, err.err_define)
104         out_file.write(line)
105     out_file.write("\n");
106     out_file.write("\treturn m;\n");
107     out_file.write("}\n");
108
109 def transformErrorName( error_name ):
110     if error_name.startswith("STATUS_"):
111         error_name = error_name.replace("STATUS_", "", 1)
112     elif error_name.startswith("RPC_NT_"):
113         error_name = error_name.replace("RPC_NT_", "RPC_", 1)
114     elif error_name.startswith("EPT_NT_"):
115         error_name = error_name.replace("EPT_NT_", "EPT_", 1)
116     return "NT_STATUS_" + error_name
117
118 # Very simple script to generate files nterr_gen.c & ntstatus_gen.h.
119 # These files contain generated definitions.
120 # This script takes four inputs:
121 # [1]: The name of the text file which is the content of an HTML table
122 #      (e.g. the one found at http://msdn.microsoft.com/en-us/library/cc231200.aspx)
123 #      copied and pasted.
124 # [2]: The name of the output generated header file with NTStatus #defines
125 # [3]: The name of the output generated source file with C arrays
126 # [4]: The name of the output generated python file
127 def main ():
128     input_file = None;
129
130     if len(sys.argv) == 5:
131         input_file =  sys.argv[1]
132         gen_headerfile_name = sys.argv[2]
133         gen_sourcefile_name = sys.argv[3]
134         gen_pythonfile_name = sys.argv[4]
135     else:
136         print("usage: %s winerrorfile headerfile sourcefile pythonfile" % (sys.argv[0]))
137         sys.exit()
138
139     # read in the data
140     file_contents = io.open(input_file, "rt", encoding='utf8')
141
142     errors = parseErrorDescriptions(file_contents, False, transformErrorName)
143
144     print("writing new header file: %s" % gen_headerfile_name)
145     out_file = io.open(gen_headerfile_name, "wt", encoding='utf8')
146     generateHeaderFile(out_file, errors)
147     out_file.close()
148     print("writing new source file: %s" % gen_sourcefile_name)
149     out_file = io.open(gen_sourcefile_name, "wt", encoding='utf8')
150     generateSourceFile(out_file, errors)
151     out_file.close()
152     print("writing new python file: %s" % gen_pythonfile_name)
153     out_file = io.open(gen_pythonfile_name, "wt", encoding='utf8')
154     generatePythonFile(out_file, errors)
155     out_file.close()
156
157 if __name__ == '__main__':
158
159     main()