python: Use 'is' for identity when comparing against None
[Samba.git] / source4 / scripting / bin / gen_hresult.py
blob6a75c37eba148543867b813be269a3510fdc821c
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/>.
25 import sys, os.path, io, string
27 # parsed error data
28 Errors = []
30 # error data model
31 class ErrorDef:
33 def __init__(self):
34 self.err_code = ""
35 self.err_define = None
36 self.err_string = ""
37 self.isWinError = False
38 self.linenum = ""
40 def escapeString( input ):
41 output = input.replace('"','\\"')
42 output = output.replace("\\<","\\\\<")
43 output = output.replace('\t',"")
44 return output
46 def parseErrorDescriptions( input_file, isWinError ):
47 # read in the data
48 fileContents = open(input_file,"r")
49 count = 0;
50 for line in fileContents:
51 content = line.strip().split(None,1)
52 # start new error definition ?
53 if len(content) == 0:
54 continue
55 if line.startswith("0x"):
56 newError = ErrorDef()
57 newError.err_code = content[0]
58 # escape the usual suspects
59 if len(content) > 1:
60 newError.err_string = escapeString(content[1])
61 newError.linenum = count
62 newError.isWinError = isWinError
63 Errors.append(newError)
64 else:
65 if len(Errors) == 0:
66 print("Error parsing file as line %d"%count)
67 sys.exit()
68 err = Errors[-1]
69 if err.err_define is None:
70 err.err_define = "HRES_" + content[0]
71 else:
72 if len(content) > 0:
73 desc = escapeString(line.strip())
74 if len(desc):
75 if err.err_string == "":
76 err.err_string = desc
77 else:
78 err.err_string = err.err_string + " " + desc
79 count = count + 1
80 fileContents.close()
81 print("parsed %d lines generated %d error definitions"%(count,len(Errors)))
83 def write_license(out_file):
84 out_file.write("/*\n")
85 out_file.write(" * Unix SMB/CIFS implementation.\n")
86 out_file.write(" *\n")
87 out_file.write(" * HRESULT Error definitions\n")
88 out_file.write(" *\n")
89 out_file.write(" * Copyright (C) Noel Power <noel.power@suse.com> 2014\n")
90 out_file.write(" *\n")
91 out_file.write(" * This program is free software; you can redistribute it and/or modify\n")
92 out_file.write(" * it under the terms of the GNU General Public License as published by\n")
93 out_file.write(" * the Free Software Foundation; either version 3 of the License, or\n")
94 out_file.write(" * (at your option) any later version.\n")
95 out_file.write(" *\n")
96 out_file.write(" * This program is distributed in the hope that it will be useful,\n")
97 out_file.write(" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n")
98 out_file.write(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n")
99 out_file.write(" * GNU General Public License for more details.\n")
100 out_file.write(" *\n")
101 out_file.write(" * You should have received a copy of the GNU General Public License\n")
102 out_file.write(" * along with this program. If not, see <http://www.gnu.org/licenses/>.\n")
103 out_file.write(" */\n")
104 out_file.write("\n")
106 def generateHeaderFile(out_file):
107 write_license(out_file)
108 out_file.write("#ifndef _HRESULT_H_\n")
109 out_file.write("#define _HRESULT_H_\n\n")
110 macro_magic = "#if defined(HAVE_IMMEDIATE_STRUCTURES)\n"
111 macro_magic += "typedef struct {uint32_t h;} HRESULT;\n"
112 macro_magic += "#define HRES_ERROR(x) ((HRESULT) { x })\n"
113 macro_magic += "#define HRES_ERROR_V(x) ((x).h)\n"
114 macro_magic += "#else\n"
115 macro_magic += "typedef uint32_t HRESULT;\n"
116 macro_magic += "#define HRES_ERROR(x) (x)\n"
117 macro_magic += "#define HRES_ERROR_V(x) (x)\n"
118 macro_magic += "#endif\n"
119 macro_magic += "\n"
120 macro_magic += "#define HRES_IS_OK(x) (HRES_ERROR_V(x) == 0)\n"
121 macro_magic += "#define HRES_IS_EQUAL(x,y) (HRES_ERROR_V(x) == HRES_ERROR_V(y))\n"
123 out_file.write(macro_magic)
124 out_file.write("\n\n")
125 out_file.write("/*\n")
126 out_file.write(" * The following error codes are autogenerated from [MS-ERREF]\n")
127 out_file.write(" * see http://msdn.microsoft.com/en-us/library/cc704587.aspx\n")
128 out_file.write(" */\n")
129 out_file.write("\n")
131 for err in Errors:
132 line = "#define {0:49} HRES_ERROR({1})\n".format(err.err_define ,err.err_code)
133 out_file.write(line)
134 out_file.write("\nconst char *hresult_errstr_const(HRESULT err_code);\n")
135 out_file.write("\nconst char *hresult_errstr(HRESULT err_code);\n")
136 out_file.write("\n#define FACILITY_WIN32 0x0007\n")
137 out_file.write("#define WIN32_FROM_HRESULT(x) (HRES_ERROR_V(x) == 0 ? HRES_ERROR_V(x) : ~((FACILITY_WIN32 << 16) | 0x80000000) & HRES_ERROR_V(x))\n")
138 out_file.write("#define HRESULT_IS_LIKELY_WERR(x) ((HRES_ERROR_V(x) & 0xFFFF0000) == 0x80070000)\n")
139 out_file.write("#define HRESULT_FROM_WERROR(x) (HRES_ERROR(0x80070000 | W_ERROR_V(x)))\n")
140 out_file.write("\n\n\n#endif /*_HRESULT_H_*/")
143 def generateSourceFile(out_file):
144 write_license(out_file)
145 out_file.write("#include \"includes.h\"\n")
146 out_file.write("#include \"hresult.h\"\n")
147 out_file.write("/*\n")
148 out_file.write(" * The following error codes and descriptions are autogenerated from [MS-ERREF]\n")
149 out_file.write(" * see http://msdn.microsoft.com/en-us/library/cc704587.aspx\n")
150 out_file.write(" */\n")
151 out_file.write("\n")
152 out_file.write("static const struct {\n")
153 out_file.write(" HRESULT error_code;\n")
154 out_file.write(" const char *error_str;\n")
155 out_file.write(" const char *error_message;\n")
156 out_file.write("} hresult_errs[] = {\n")
158 for err in Errors:
159 out_file.write(" {\n")
160 if err.isWinError:
161 out_file.write(" HRESULT_FROM_WIN32(%s),\n"%err.err_define)
162 out_file.write(" \"HRESULT_FROM_WIN32(%s)\",\n"%err.err_define)
163 else:
164 out_file.write(" %s,\n"%err.err_define)
165 out_file.write(" \"%s\",\n"%err.err_define)
166 out_file.write(" \"%s\"\n"%err.err_string)
167 out_file.write(" },\n")
168 out_file.write("};\n")
169 out_file.write("\n")
170 out_file.write("const char *hresult_errstr_const(HRESULT err_code)\n")
171 out_file.write("{\n");
172 out_file.write(" const char *result = NULL;\n")
173 out_file.write(" int i;\n")
174 out_file.write(" for (i = 0; i < ARRAY_SIZE(hresult_errs); ++i) {\n")
175 out_file.write(" if (HRES_IS_EQUAL(err_code, hresult_errs[i].error_code)) {\n")
176 out_file.write(" result = hresult_errs[i].error_message;\n")
177 out_file.write(" break;\n")
178 out_file.write(" }\n")
179 out_file.write(" }\n")
180 out_file.write(" /* convert & check win32 error space? */\n")
181 out_file.write(" if (result == NULL && HRESULT_IS_LIKELY_WERR(err_code)) {\n")
182 out_file.write(" WERROR wErr = W_ERROR(WIN32_FROM_HRESULT(err_code));\n")
183 out_file.write(" result = get_friendly_werror_msg(wErr);\n")
184 out_file.write(" }\n")
185 out_file.write(" return result;\n")
186 out_file.write("};\n")
187 out_file.write("\n")
188 out_file.write("const char *hresult_errstr(HRESULT err_code)\n")
189 out_file.write("{\n");
190 out_file.write(" static char msg[22];\n")
191 out_file.write(" int i;\n")
192 out_file.write("\n")
193 out_file.write(" for (i = 0; i < ARRAY_SIZE(hresult_errs); i++) {\n")
194 out_file.write(" if (HRES_IS_EQUAL(err_code, hresult_errs[i].error_code)) {\n")
195 out_file.write(" return hresult_errs[i].error_str;\n")
196 out_file.write(" }\n")
197 out_file.write(" }\n")
198 out_file.write(" snprintf(msg, sizeof(msg), \"HRES code 0x%08x\", HRES_ERROR_V(err_code));\n")
199 out_file.write(" return msg;\n")
200 out_file.write("};\n")
202 # Very simple script to generate files hresult.c & hresult.h
203 # The script simply takes a text file as input, format of input file is
204 # very simple and is just the content of a html table ( such as that found
205 # in http://msdn.microsoft.com/en-us/library/cc704587.aspx ) copied and
206 # pasted into a text file
208 def main ():
209 input_file1 = None;
210 filename = "hresult"
211 headerfile_name = filename + ".h"
212 sourcefile_name = filename + ".c"
213 if len(sys.argv) > 1:
214 input_file1 = sys.argv[1]
215 else:
216 print("usage: %s winerrorfile"%(sys.argv[0]))
217 sys.exit()
219 parseErrorDescriptions(input_file1, False)
220 out_file = open(headerfile_name,"w")
221 generateHeaderFile(out_file)
222 out_file.close()
223 out_file = open(sourcefile_name,"w")
224 generateSourceFile(out_file)
226 if __name__ == '__main__':
228 main()