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
35 self.err_define = None
37 self.isWinError = False
40 def escapeString( input ):
41 output = input.replace('"','\\"')
42 output = output.replace("\\<","\\\\<")
43 output = output.replace('\t',"")
46 def parseErrorDescriptions( input_file, isWinError ):
48 fileContents = open(input_file,"r")
50 for line in fileContents:
51 content = line.strip().split(None,1)
52 # start new error definition ?
55 if line.startswith("0x"):
57 newError.err_code = content[0]
58 # escape the usual suspects
60 newError.err_string = escapeString(content[1])
61 newError.linenum = count
62 newError.isWinError = isWinError
63 Errors.append(newError)
66 print "Error parsing file as line %d"%count
69 if err.err_define == None:
70 err.err_define = "HRES_" + content[0]
73 desc = escapeString(line.strip())
75 if err.err_string == "":
78 err.err_string = err.err_string + " " + desc
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")
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"
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")
132 line = "#define {0:49} HRES_ERROR({1})\n".format(err.err_define ,err.err_code)
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("\n\n\n#endif /*_HRESULT_H_*/")
142 def generateSourceFile(out_file):
143 write_license(out_file)
144 out_file.write("#include \"includes.h\"\n")
145 out_file.write("#include \"hresult.h\"\n")
146 out_file.write("/*\n")
147 out_file.write(" * The following error codes and descriptions are autogenerated from [MS-ERREF]\n")
148 out_file.write(" * see http://msdn.microsoft.com/en-us/library/cc704587.aspx\n")
149 out_file.write(" */\n")
151 out_file.write("static const struct {\n")
152 out_file.write(" HRESULT error_code;\n")
153 out_file.write(" const char *error_str;\n")
154 out_file.write(" const char *error_message;\n")
155 out_file.write("} hresult_errs[] = {\n")
158 out_file.write(" {\n")
160 out_file.write(" HRESULT_FROM_WIN32(%s),\n"%err.err_define)
161 out_file.write(" \"HRESULT_FROM_WIN32(%s)\",\n"%err.err_define)
163 out_file.write(" %s,\n"%err.err_define)
164 out_file.write(" \"%s\",\n"%err.err_define)
165 out_file.write(" \"%s\"\n"%err.err_string)
166 out_file.write(" },\n")
167 out_file.write("};\n")
169 out_file.write("const char *hresult_errstr_const(HRESULT err_code)\n")
170 out_file.write("{\n");
171 out_file.write(" const char *result = NULL;\n")
172 out_file.write(" int i;\n")
173 out_file.write(" for (i = 0; i < ARRAY_SIZE(hresult_errs); ++i) {\n")
174 out_file.write(" if (HRES_IS_EQUAL(err_code, hresult_errs[i].error_code)) {\n")
175 out_file.write(" result = hresult_errs[i].error_message;\n")
176 out_file.write(" break;\n")
177 out_file.write(" }\n")
178 out_file.write(" }\n")
179 out_file.write(" /* convert & check win32 error space? */\n")
180 out_file.write(" if (result == NULL && HRESULT_IS_LIKELY_WERR(err_code)) {\n")
181 out_file.write(" WERROR wErr = W_ERROR(WIN32_FROM_HRESULT(err_code));\n")
182 out_file.write(" result = get_friendly_werror_msg(wErr);\n")
183 out_file.write(" }\n")
184 out_file.write(" return result;\n")
185 out_file.write("};\n")
187 out_file.write("const char *hresult_errstr(HRESULT err_code)\n")
188 out_file.write("{\n");
189 out_file.write(" static char msg[20];\n")
190 out_file.write(" int i;\n")
192 out_file.write(" for (i = 0; i < ARRAY_SIZE(hresult_errs); i++) {\n")
193 out_file.write(" if (HRES_IS_EQUAL(err_code, hresult_errs[i].error_code)) {\n")
194 out_file.write(" return hresult_errs[i].error_str;\n")
195 out_file.write(" }\n")
196 out_file.write(" }\n")
197 out_file.write(" snprintf(msg, sizeof(msg), \"HRES code 0x%08x\", HRES_ERROR_V(err_code));\n")
198 out_file.write(" return msg;\n")
199 out_file.write("};\n")
201 # Very simple script to generate files hresult.c & hresult.h
202 # The script simply takes a text file as input, format of input file is
203 # very simple and is just the content of a html table ( such as that found
204 # in http://msdn.microsoft.com/en-us/library/cc704587.aspx ) copied and
205 # pasted into a text file
210 headerfile_name = filename + ".h"
211 sourcefile_name = filename + ".c"
212 if len(sys.argv) > 1:
213 input_file1 = sys.argv[1]
215 print "usage: %s winerrorfile"%(sys.argv[0])
218 parseErrorDescriptions(input_file1, False)
219 out_file = open(headerfile_name,"w")
220 generateHeaderFile(out_file)
222 out_file = open(sourcefile_name,"w")
223 generateSourceFile(out_file)
225 if __name__ == '__main__':