torture3: Fix bug 10687
[Samba.git] / source4 / scripting / bin / gen_hresult.py
blobf0453d11c8eeecb26867656deb836b2b1401fbb7
1 #!/usr/bin/env python
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 line.startswith("0x"):
54 newError = ErrorDef()
55 newError.err_code = content[0]
56 # escape the usual suspects
57 if len(content) > 1:
58 newError.err_string = escapeString(content[1])
59 newError.linenum = count
60 newError.isWinError = isWinError
61 Errors.append(newError)
62 else:
63 if len(Errors) == 0:
64 print "Error parsing file as line %d"%count
65 sys.exit()
66 err = Errors[-1]
67 if err.err_define == None:
68 err.err_define = "HRES_" + content[0]
69 else:
70 if len(content) > 0:
71 desc = escapeString(line.strip())
72 if len(desc):
73 if err.err_string == "":
74 err.err_string = desc
75 else:
76 err.err_string = err.err_string + " " + desc
77 count = count + 1
78 fileContents.close()
79 print "parsed %d lines generated %d error definitions"%(count,len(Errors))
81 def write_license(out_file):
82 out_file.write("/*\n")
83 out_file.write(" * Unix SMB/CIFS implementation.\n")
84 out_file.write(" *\n")
85 out_file.write(" * HRESULT Error definitions\n")
86 out_file.write(" *\n")
87 out_file.write(" * Copyright (C) Noel Power <noel.power@suse.com> 2014\n")
88 out_file.write(" *\n")
89 out_file.write(" * This program is free software; you can redistribute it and/or modify\n")
90 out_file.write(" * it under the terms of the GNU General Public License as published by\n")
91 out_file.write(" * the Free Software Foundation; either version 3 of the License, or\n")
92 out_file.write(" * (at your option) any later version.\n")
93 out_file.write(" *\n")
94 out_file.write(" * This program is distributed in the hope that it will be useful,\n")
95 out_file.write(" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n")
96 out_file.write(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n")
97 out_file.write(" * GNU General Public License for more details.\n")
98 out_file.write(" *\n")
99 out_file.write(" * You should have received a copy of the GNU General Public License\n")
100 out_file.write(" * along with this program. If not, see <http://www.gnu.org/licenses/>.\n")
101 out_file.write(" */\n")
102 out_file.write("\n")
104 def generateHeaderFile(out_file):
105 write_license(out_file)
106 out_file.write("#ifndef _HRESULT_H_\n")
107 out_file.write("#define _HRESULT_H_\n\n")
108 macro_magic = "#if defined(HAVE_IMMEDIATE_STRUCTURES)\n"
109 macro_magic += "typedef struct {uint32_t h;} HRESULT;\n"
110 macro_magic += "#define HRES_ERROR(x) ((HRESULT) { x })\n"
111 macro_magic += "#define HRES_ERROR_V(x) ((x).h)\n"
112 macro_magic += "#else\n"
113 macro_magic += "typedef uint32_t HRESULT;\n"
114 macro_magic += "#define HRES_ERROR(x) (x)\n"
115 macro_magic += "#define HRES_ERROR_V(x) (x)\n"
116 macro_magic += "#endif\n"
117 macro_magic += "\n"
118 macro_magic += "#define HRES_IS_OK(x) (HRES_ERROR_V(x) == 0)\n"
119 macro_magic += "#define HRES_IS_EQUAL(x,y) (HRES_ERROR_V(x) == HRES_ERROR_V(y))\n"
121 out_file.write(macro_magic)
122 out_file.write("\n\n")
123 out_file.write("/*\n")
124 out_file.write(" * The following error codes are autogenerated from [MS-ERREF]\n")
125 out_file.write(" * see http://msdn.microsoft.com/en-us/library/cc704587.aspx\n")
126 out_file.write(" */\n")
127 out_file.write("\n")
129 for err in Errors:
130 line = "#define {0:49} HRES_ERROR({1})\n".format(err.err_define ,err.err_code)
131 out_file.write(line)
132 out_file.write("\nconst char *hresult_errstr_const(HRESULT err_code);\n")
133 out_file.write("\n#define FACILITY_WIN32 0x0007\n")
134 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")
135 out_file.write("#define HRESULT_IS_LIKELY_WERR(x) ((HRES_ERROR_V(x) & 0xFFFF0000) == 0x80070000)\n")
136 out_file.write("\n\n\n#endif /*_HRESULT_H_*/")
139 def generateSourceFile(out_file):
140 write_license(out_file)
141 out_file.write("#include \"includes.h\"\n")
142 out_file.write("#include \"hresult.h\"\n")
143 out_file.write("/*\n")
144 out_file.write(" * The following error codes and descriptions are autogenerated from [MS-ERREF]\n")
145 out_file.write(" * see http://msdn.microsoft.com/en-us/library/cc704587.aspx\n")
146 out_file.write(" */\n")
147 out_file.write("\n")
148 out_file.write("static const struct {\n")
149 out_file.write(" HRESULT error_code;\n")
150 out_file.write(" const char *error_str;\n")
151 out_file.write("} hresult_errs[] = {\n")
153 for err in Errors:
154 out_file.write(" {\n")
155 if err.isWinError:
156 out_file.write(" HRESULT_FROM_WIN32(%s),\n"%err.err_define)
157 else:
158 out_file.write(" %s,\n"%err.err_define)
159 out_file.write(" \"%s\"\n"%err.err_string)
160 out_file.write(" },\n")
161 out_file.write("};\n")
162 out_file.write("\n")
163 out_file.write("const char *hresult_errstr_const(HRESULT err_code)\n")
164 out_file.write("{\n");
165 out_file.write(" const char *result = NULL;\n")
166 out_file.write(" int i;\n")
167 out_file.write(" for (i = 0; i < ARRAY_SIZE(hresult_errs); ++i) {\n")
168 out_file.write(" if (HRES_IS_EQUAL(err_code, hresult_errs[i].error_code)) {\n")
169 out_file.write(" result = hresult_errs[i].error_str;\n")
170 out_file.write(" break;\n")
171 out_file.write(" }\n")
172 out_file.write(" }\n")
173 out_file.write(" /* convert & check win32 error space? */\n")
174 out_file.write(" if (result == NULL && HRESULT_IS_LIKELY_WERR(err_code)) {\n")
175 out_file.write(" WERROR wErr = W_ERROR(WIN32_FROM_HRESULT(err_code));\n")
176 out_file.write(" result = get_friendly_werror_msg(wErr);\n")
177 out_file.write(" }\n")
178 out_file.write(" return result;\n")
179 out_file.write("};\n")
181 # Very simple script to generate files hresult.c & hresult.h
182 # The script simply takes a text file as input, format of input file is
183 # very simple and is just the content of a html table ( such as that found
184 # in http://msdn.microsoft.com/en-us/library/cc704587.aspx ) copied and
185 # pasted into a text file
187 def main ():
188 input_file1 = None;
189 filename = "hresult"
190 headerfile_name = filename + ".h"
191 sourcefile_name = filename + ".c"
192 if len(sys.argv) > 1:
193 input_file1 = sys.argv[1]
194 else:
195 print "usage: %s winerrorfile"%(sys.argv[0])
196 sys.exit()
198 parseErrorDescriptions(input_file1, False)
199 out_file = open(headerfile_name,"w")
200 generateHeaderFile(out_file)
201 out_file.close()
202 out_file = open(sourcefile_name,"w")
203 generateSourceFile(out_file)
205 if __name__ == '__main__':
207 main()