s4:scripting: Remove obsolete references to function prototypes
[samba.git] / source4 / scripting / bin / gen_werror.py
1 #!/usr/bin/env python3
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, io
25 from gen_error_common import parseErrorDescriptions
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("/*\n")
41     out_file.write(" * Names for errors generated from\n")
42     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
43     out_file.write(" */\n")
44
45     for err in errors:
46         if (err.err_define == 'WERR_NERR_SUCCESS'):
47             continue
48         out_file.write(f'\t case {hex(err.err_code)}:\n')
49         out_file.write(f'\t\treturn \"{err.err_define}\";\n')
50         out_file.write(f'\t\tbreak;\n')
51
52 def generateFriendlySourceFile(out_file, errors):
53     out_file.write("/*\n")
54     out_file.write(" * Names for errors generated from\n")
55     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
56     out_file.write(" */\n")
57
58     for err in errors:
59         if (err.err_define == 'WERR_NERR_SUCCESS'):
60             continue
61         out_file.write(f'\tcase {hex(err.err_code)}:\n')
62         out_file.write(f'\t\treturn \"{err.err_string}\";\n')
63         out_file.write('\t\tbreak;\n')
64
65 def generatePythonFile(out_file, errors):
66     out_file.write("/*\n")
67     out_file.write(" * Errors generated from\n")
68     out_file.write(" * [MS-ERREF] https://msdn.microsoft.com/en-us/library/cc231199.aspx\n")
69     out_file.write(" */\n")
70     out_file.write("#include \"lib/replace/system/python.h\"\n")
71     out_file.write("#include \"python/py3compat.h\"\n")
72     out_file.write("#include \"includes.h\"\n\n")
73     out_file.write("static struct PyModuleDef moduledef = {\n")
74     out_file.write("\tPyModuleDef_HEAD_INIT,\n")
75     out_file.write("\t.m_name = \"werror\",\n")
76     out_file.write("\t.m_doc = \"WERROR defines\",\n")
77     out_file.write("\t.m_size = -1,\n")
78     out_file.write("};\n\n")
79     out_file.write("MODULE_INIT_FUNC(werror)\n")
80     out_file.write("{\n")
81     out_file.write("\tPyObject *m;\n\n")
82     out_file.write("\tm = PyModule_Create(&moduledef);\n")
83     out_file.write("\tif (m == NULL)\n")
84     out_file.write("\t\treturn NULL;\n\n")
85     for err in errors:
86         line = """\tPyModule_AddObject(m, \"%s\",
87                   \t\tPyLong_FromUnsignedLongLong(W_ERROR_V(%s)));\n""" % (err.err_define, err.err_define)
88         out_file.write(line)
89     out_file.write("\n")
90     out_file.write("\treturn m;\n")
91     out_file.write("}\n")
92
93 def transformErrorName( error_name ):
94     if error_name.startswith("WERR_"):
95         error_name = error_name.replace("WERR_", "", 1)
96     elif error_name.startswith("ERROR_"):
97         error_name = error_name.replace("ERROR_", "", 1)
98     return "WERR_" + error_name.upper()
99
100 # Script to generate files werror_gen.h, doserr_gen.c and
101 # py_werror.c.
102 #
103 # These files contain generated definitions for WERRs and
104 # their descriptions/names.
105 #
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 https://msdn.microsoft.com/en-us/library/cc231199.aspx)
109 #      copied and pasted.
110 # [2]: [[output werror_gen.h]]
111 # [3]: [[output doserr_gen.c]]
112 # [4]: [[output py_werror.c]]
113 def main():
114     if len(sys.argv) == 6:
115         input_file_name = sys.argv[1]
116         gen_headerfile_name = sys.argv[2]
117         gen_sourcefile_name = sys.argv[3]
118         gen_friendlysource_name = sys.argv[4]
119         gen_pythonfile_name = sys.argv[5]
120     else:
121         print("usage: %s winerrorfile headerfile sourcefile pythonfile" % sys.argv[0])
122         sys.exit()
123
124     input_file = io.open(input_file_name, "rt", encoding='utf8')
125     errors = parseErrorDescriptions(input_file, True, transformErrorName)
126     input_file.close()
127
128     print("writing new header file: %s" % gen_headerfile_name)
129     out_file = io.open(gen_headerfile_name, "wt", encoding='utf8')
130     generateHeaderFile(out_file, errors)
131     out_file.close()
132     print("writing new source file: %s" % gen_sourcefile_name)
133     out_file = io.open(gen_sourcefile_name, "wt", encoding='utf8')
134     generateSourceFile(out_file, errors)
135     out_file.close()
136     print("writing new source file: %s" % gen_friendlysource_name)
137     out_file = io.open(gen_friendlysource_name, "wt", encoding='utf8')
138     generateFriendlySourceFile(out_file, errors)
139     out_file.close()
140     print("writing new python file: %s" % gen_pythonfile_name)
141     out_file = io.open(gen_pythonfile_name, "wt", encoding='utf8')
142     generatePythonFile(out_file, errors)
143     out_file.close()
144
145 if __name__ == '__main__':
146
147     main()