96611ae7eaa5847ac56af3dd2443546ab93884bd
[samba.git] / source4 / scripting / bin / gen_werror.py
1 #!/usr/bin/env python
2
3 #
4 # Unix SMB/CIFS implementation.
5 #
6 # WERROR error definition generation
7 #
8 # Copyright (C) Catalyst.Net Ltd. 2017
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
24 import sys, os.path, io, string
25 from gen_error_common import parseErrorDescriptions, ErrorDef
26
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] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
31     out_file.write(" */\n\n")
32     out_file.write("#ifndef _WERR_GEN_H\n")
33     out_file.write("#define _WERR_GEN_H\n")
34     for err in errors:
35         line = "#define %s W_ERROR(%s)\n" % (err.err_define, hex(err.err_code))
36         out_file.write(line)
37     out_file.write("\n#endif /* _WERR_GEN_H */\n")
38
39 def generateSourceFile(out_file, errors):
40     out_file.write("#include \"werror.h\"\n")
41
42     out_file.write("/*\n")
43     out_file.write(" * Names for errors generated from\n")
44     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
45     out_file.write(" */\n")
46
47     out_file.write("static const struct werror_code_struct dos_errs[] = \n")
48     out_file.write("{\n")
49     for err in errors:
50         out_file.write("\t{ \"%s\", %s },\n" % (err.err_define, err.err_define))
51     out_file.write("{ 0, W_ERROR(0) }\n")
52     out_file.write("};\n")
53
54     out_file.write("\n/*\n")
55     out_file.write(" * Descriptions for errors generated from\n")
56     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
57     out_file.write(" */\n")
58
59     out_file.write("static const struct werror_str_struct dos_err_strs[] = \n")
60     out_file.write("{\n")
61     for err in errors:
62         # Account for the possibility that some errors may not have descriptions
63         if err.err_string == "":
64             continue
65         out_file.write("\t{ %s, \"%s\" },\n"%(err.err_define, err.err_string))
66     out_file.write("\t{ W_ERROR(0), 0 }\n")
67     out_file.write("};")
68
69 def generatePythonFile(out_file, errors):
70     out_file.write("/*\n")
71     out_file.write(" * Errors generated from\n")
72     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
73     out_file.write(" */\n")
74     out_file.write("#include <Python.h>\n")
75     out_file.write("#include \"python/py3compat.h\"\n")
76     out_file.write("#include \"includes.h\"\n\n")
77     out_file.write("static inline PyObject *ndr_PyLong_FromUnsignedLongLong(unsigned long long v)\n");
78     out_file.write("{\n");
79     out_file.write("\tif (v > LONG_MAX) {\n");
80     out_file.write("\t\treturn PyLong_FromUnsignedLongLong(v);\n");
81     out_file.write("\t} else {\n");
82     out_file.write("\t\treturn PyInt_FromLong(v);\n");
83     out_file.write("\t}\n");
84     out_file.write("}\n\n");
85     # This is needed to avoid a missing prototype error from the C
86     # compiler. There is never a prototype for this function, it is a
87     # module loaded by python with dlopen() and found with dlsym().
88     out_file.write("static struct PyModuleDef moduledef = {\n")
89     out_file.write("\tPyModuleDef_HEAD_INIT,\n")
90     out_file.write("\t.m_name = \"werror\",\n")
91     out_file.write("\t.m_doc = \"WERROR defines\",\n")
92     out_file.write("\t.m_size = -1,\n")
93     out_file.write("};\n\n")
94     out_file.write("MODULE_INIT_FUNC(werror)\n")
95     out_file.write("{\n")
96     out_file.write("\tPyObject *m;\n\n")
97     out_file.write("\tm = PyModule_Create(&moduledef);\n");
98     out_file.write("\tif (m == NULL)\n");
99     out_file.write("\t\treturn NULL;\n\n");
100     for err in errors:
101         line = """\tPyModule_AddObject(m, \"%s\",
102                   \t\tndr_PyLong_FromUnsignedLongLong(W_ERROR_V(%s)));\n""" % (err.err_define, err.err_define)
103         out_file.write(line)
104     out_file.write("\n");
105     out_file.write("\treturn m;\n");
106     out_file.write("}\n");
107
108 def transformErrorName( error_name ):
109     if error_name.startswith("WERR_"):
110         error_name = error_name.replace("WERR_", "", 1)
111     elif error_name.startswith("ERROR_"):
112         error_name = error_name.replace("ERROR_", "", 1)
113     return "WERR_" + error_name.upper()
114
115 # Script to generate files werror_gen.h, doserr_gen.c and
116 # py_werror.c.
117 #
118 # These files contain generated definitions for WERRs and
119 # their descriptions/names.
120 #
121 # This script takes four inputs:
122 # [1]: The name of the text file which is the content of an HTML table
123 #      (e.g. the one found at https://msdn.microsoft.com/en-us/library/cc231199.aspx)
124 #      copied and pasted.
125 # [2]: [[output werror_gen.h]]
126 # [3]: [[output doserr_gen.c]]
127 # [4]: [[output py_werror.c]]
128 def main():
129     if len(sys.argv) == 5:
130         input_file_name = sys.argv[1]
131         gen_headerfile_name = sys.argv[2]
132         gen_sourcefile_name = sys.argv[3]
133         gen_pythonfile_name = sys.argv[4]
134     else:
135         print("usage: %s winerrorfile headerfile sourcefile pythonfile" % sys.argv[0])
136         sys.exit()
137
138     input_file = open(input_file_name, "r")
139     errors = parseErrorDescriptions(input_file, True, transformErrorName)
140     input_file.close()
141
142     print("writing new header file: %s" % gen_headerfile_name)
143     out_file = open(gen_headerfile_name, "w")
144     generateHeaderFile(out_file, errors)
145     out_file.close()
146     print("writing new source file: %s" % gen_sourcefile_name)
147     out_file = open(gen_sourcefile_name, "w")
148     generateSourceFile(out_file, errors)
149     out_file.close()
150     print("writing new python file: %s" % gen_pythonfile_name)
151     out_file = open(gen_pythonfile_name, "w")
152     generatePythonFile(out_file, errors)
153     out_file.close()
154
155 if __name__ == '__main__':
156
157     main()