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